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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2012-03-02 21:05:45 +00:00
|
|
|
|
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);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-03-02 21:05:45 +00:00
|
|
|
protected:
|
2016-03-25 11:49:22 +00:00
|
|
|
SkString onShortName() override {
|
2012-03-02 21:05:45 +00:00
|
|
|
return SkString("morphology");
|
|
|
|
}
|
|
|
|
|
2016-03-25 11:49:22 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2021-01-24 13:57:23 +00:00
|
|
|
auto surf = SkSurface::MakeRasterN32Premul(135, 135);
|
2019-01-08 14:38:02 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 64.0f);
|
2012-03-02 21:05:45 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
2021-01-24 13:57:23 +00:00
|
|
|
surf->getCanvas()->drawString("ABC", 10, 55, font, paint);
|
|
|
|
surf->getCanvas()->drawString("XYZ", 10, 110, font, paint);
|
|
|
|
|
|
|
|
fImage = surf->makeImageSnapshot();
|
2012-03-02 21:05:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 11:49:22 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(WIDTH, HEIGHT);
|
2012-03-02 21:05:45 +00:00
|
|
|
}
|
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));
|
2021-01-24 13:57:23 +00:00
|
|
|
canvas->clipIRect(fImage->bounds());
|
|
|
|
canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint);
|
2013-09-17 13:41:43 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-03-25 11:49:22 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-03-02 21:05:45 +00:00
|
|
|
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;
|
2019-08-02 19:21:23 +00:00
|
|
|
SkIRect cropRect = SkIRect::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) {
|
2019-08-02 19:21:23 +00:00
|
|
|
const SkIRect* cr = j & 0x02 ? &cropRect : nullptr;
|
2013-09-17 13:41:43 +00:00
|
|
|
if (j & 0x01) {
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Erode(
|
|
|
|
samples[i].fRadiusX, samples[i].fRadiusY, nullptr, cr));
|
2012-04-10 17:25:44 +00:00
|
|
|
} else {
|
2019-08-05 14:24:49 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Dilate(
|
2019-08-02 19:21:23 +00:00
|
|
|
samples[i].fRadiusX, samples[i].fRadiusY, nullptr, cr));
|
2012-04-10 17:25:44 +00:00
|
|
|
}
|
2016-03-25 11:49:22 +00:00
|
|
|
this->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:
|
2021-01-24 13:57:23 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
2016-03-25 11:49:22 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2012-03-02 21:05:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-03-25 11:49:22 +00:00
|
|
|
DEF_GM(return new MorphologyGM;)
|
2012-03-02 21:05:45 +00:00
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|