| 1 |
/* |
| 2 |
* Copyright 2012 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkFont.h" |
| 12 |
#include "include/core/SkImageFilter.h" |
| 13 |
#include "include/core/SkPaint.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkSize.h" |
| 17 |
#include "include/core/SkString.h" |
| 18 |
#include "include/core/SkTypeface.h" |
| 19 |
#include "include/core/SkTypes.h" |
| 20 |
#include "include/effects/SkImageFilters.h" |
| 21 |
#include "tools/ToolUtils.h" |
| 22 |
#include "tools/fonts/FontToolUtils.h" |
| 23 |
|
| 24 |
#define WIDTH 700 |
| 25 |
#define HEIGHT 560 |
| 26 |
|
| 27 |
namespace skiagm { |
| 28 |
|
| 29 |
class MorphologyGM : public GM { |
| 30 |
public: |
| 31 |
MorphologyGM() { |
| 32 |
this->setBGColor(0xFF000000); |
| 33 |
} |
| 34 |
|
| 35 |
protected: |
| 36 |
SkString getName() const override { return SkString("morphology"); } |
| 37 |
|
| 38 |
void onOnceBeforeDraw() override { |
| 39 |
auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(135, 135)); |
| 40 |
|
| 41 |
SkFont font(ToolUtils::DefaultPortableTypeface(), 64.0f); |
| 42 |
SkPaint paint; |
| 43 |
paint.setColor(0xFFFFFFFF); |
| 44 |
surf->getCanvas()->drawString("ABC", 10, 55, font, paint); |
| 45 |
surf->getCanvas()->drawString("XYZ", 10, 110, font, paint); |
| 46 |
|
| 47 |
fImage = surf->makeImageSnapshot(); |
| 48 |
} |
| 49 |
|
| 50 |
SkISize getISize() override { return SkISize::Make(WIDTH, HEIGHT); } |
| 51 |
|
| 52 |
void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) { |
| 53 |
canvas->save(); |
| 54 |
canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 55 |
canvas->clipIRect(fImage->bounds()); |
| 56 |
canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint); |
| 57 |
canvas->restore(); |
| 58 |
} |
| 59 |
|
| 60 |
void onDraw(SkCanvas* canvas) override { |
| 61 |
struct { |
| 62 |
int fWidth, fHeight; |
| 63 |
int fRadiusX, fRadiusY; |
| 64 |
} samples[] = { |
| 65 |
{ 140, 140, 0, 0 }, |
| 66 |
{ 140, 140, 0, 2 }, |
| 67 |
{ 140, 140, 2, 0 }, |
| 68 |
{ 140, 140, 2, 2 }, |
| 69 |
{ 24, 24, 25, 25 }, |
| 70 |
}; |
| 71 |
SkPaint paint; |
| 72 |
SkIRect cropRect = SkIRect::MakeXYWH(25, 20, 100, 80); |
| 73 |
|
| 74 |
for (unsigned j = 0; j < 4; ++j) { |
| 75 |
for (unsigned i = 0; i < std::size(samples); ++i) { |
| 76 |
const SkIRect* cr = j & 0x02 ? &cropRect : nullptr; |
| 77 |
if (j & 0x01) { |
| 78 |
paint.setImageFilter(SkImageFilters::Erode( |
| 79 |
samples[i].fRadiusX, samples[i].fRadiusY, nullptr, cr)); |
| 80 |
} else { |
| 81 |
paint.setImageFilter(SkImageFilters::Dilate( |
| 82 |
samples[i].fRadiusX, samples[i].fRadiusY, nullptr, cr)); |
| 83 |
} |
| 84 |
this->drawClippedBitmap(canvas, paint, i * 140, j * 140); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
private: |
| 90 |
sk_sp<SkImage> fImage; |
| 91 |
|
| 92 |
using INHERITED = GM; |
| 93 |
}; |
| 94 |
|
| 95 |
////////////////////////////////////////////////////////////////////////////// |
| 96 |
|
| 97 |
DEF_GM(return new MorphologyGM;) |
| 98 |
|
| 99 |
} // namespace skiagm |