2012-03-02 21:05:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkMorphologyImageFilter.h"
|
|
|
|
|
2013-09-17 13:41:43 +00:00
|
|
|
#define WIDTH 700
|
|
|
|
#define HEIGHT 560
|
2012-03-02 21:05:45 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class MorphologyGM : public GM {
|
|
|
|
public:
|
|
|
|
MorphologyGM() {
|
|
|
|
this->setBGColor(0xFF000000);
|
|
|
|
fOnce = false;
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-03-02 21:05:45 +00:00
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("morphology");
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_bitmap() {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBitmap.allocN32Pixels(135, 135);
|
2013-08-29 11:54:56 +00:00
|
|
|
SkBitmapDevice device(fBitmap);
|
2012-03-02 21:05:45 +00:00
|
|
|
SkCanvas canvas(&device);
|
|
|
|
canvas.clear(0x0);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
const char* str1 = "ABC";
|
|
|
|
const char* str2 = "XYZ";
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
|
|
|
paint.setTextSize(64);
|
|
|
|
canvas.drawText(str1, strlen(str1), 10, 55, paint);
|
|
|
|
canvas.drawText(str2, strlen(str2), 10, 110, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
|
|
|
return make_isize(WIDTH, HEIGHT);
|
|
|
|
}
|
2013-09-17 13:41:43 +00:00
|
|
|
|
|
|
|
void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
canvas->clipRect(SkRect::MakeWH(
|
|
|
|
SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
|
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, &paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:05:45 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
if (!fOnce) {
|
|
|
|
make_bitmap();
|
|
|
|
fOnce = true;
|
|
|
|
}
|
|
|
|
struct {
|
2012-04-10 17:25:44 +00:00
|
|
|
int fWidth, fHeight;
|
2012-03-02 21:05:45 +00:00
|
|
|
int fRadiusX, fRadiusY;
|
|
|
|
} samples[] = {
|
2012-04-10 17:25:44 +00:00
|
|
|
{ 140, 140, 0, 0 },
|
|
|
|
{ 140, 140, 0, 2 },
|
|
|
|
{ 140, 140, 2, 0 },
|
|
|
|
{ 140, 140, 2, 2 },
|
|
|
|
{ 24, 24, 25, 25 },
|
2012-03-02 21:05:45 +00:00
|
|
|
};
|
|
|
|
SkPaint paint;
|
2013-10-10 13:51:19 +00:00
|
|
|
SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80));
|
2013-09-17 13:41:43 +00:00
|
|
|
|
|
|
|
for (unsigned j = 0; j < 4; ++j) {
|
2012-04-10 17:25:44 +00:00
|
|
|
for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
|
2013-10-10 13:51:19 +00:00
|
|
|
const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : NULL;
|
2013-09-17 13:41:43 +00:00
|
|
|
if (j & 0x01) {
|
2012-04-10 17:25:44 +00:00
|
|
|
paint.setImageFilter(new SkErodeImageFilter(
|
|
|
|
samples[i].fRadiusX,
|
2013-09-17 13:41:43 +00:00
|
|
|
samples[i].fRadiusY,
|
|
|
|
NULL,
|
|
|
|
cr))->unref();
|
2012-04-10 17:25:44 +00:00
|
|
|
} else {
|
|
|
|
paint.setImageFilter(new SkDilateImageFilter(
|
|
|
|
samples[i].fRadiusX,
|
2013-09-17 13:41:43 +00:00
|
|
|
samples[i].fRadiusY,
|
|
|
|
NULL,
|
|
|
|
cr))->unref();
|
2012-04-10 17:25:44 +00:00
|
|
|
}
|
2013-09-17 13:41:43 +00:00
|
|
|
drawClippedBitmap(canvas, paint, i * 140, j * 140);
|
2012-03-02 21:05:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-03-02 21:05:45 +00:00
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
bool fOnce;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new MorphologyGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|