180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2012 Google Inc.
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "gm.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkMorphologyImageFilter.h"
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
110a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger#define WIDTH 700
120a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger#define HEIGHT 560
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querunamespace skiagm {
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass MorphologyGM : public GM {
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    MorphologyGM() {
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->setBGColor(0xFF000000);
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fOnce = false;
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual SkString onShortName() {
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return SkString("morphology");
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void make_bitmap() {
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 135, 135);
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fBitmap.allocPixels();
310a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        SkBitmapDevice device(fBitmap);
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkCanvas canvas(&device);
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas.clear(0x0);
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPaint paint;
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setAntiAlias(true);
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const char* str1 = "ABC";
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const char* str2 = "XYZ";
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setColor(0xFFFFFFFF);
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setTextSize(64);
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas.drawText(str1, strlen(str1), 10, 55, paint);
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas.drawText(str2, strlen(str2), 10, 110, paint);
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual SkISize onISize() {
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return make_isize(WIDTH, HEIGHT);
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
470a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger
480a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
490a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        canvas->save();
500a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
510a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        canvas->clipRect(SkRect::MakeWH(
520a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger          SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
530a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        canvas->drawBitmap(fBitmap, 0, 0, &paint);
540a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        canvas->restore();
550a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    }
560a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void onDraw(SkCanvas* canvas) {
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (!fOnce) {
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            make_bitmap();
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            fOnce = true;
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        struct {
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            int fWidth, fHeight;
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            int fRadiusX, fRadiusY;
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        } samples[] = {
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            { 140, 140,   0,   0 },
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            { 140, 140,   0,   2 },
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            { 140, 140,   2,   0 },
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            { 140, 140,   2,   2 },
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            {  24,  24,  25,  25 },
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        };
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPaint paint;
730a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80));
740a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger
750a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        for (unsigned j = 0; j < 4; ++j) {
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
770a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : NULL;
780a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                if (j & 0x01) {
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    paint.setImageFilter(new SkErodeImageFilter(
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                        samples[i].fRadiusX,
810a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                        samples[i].fRadiusY,
820a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                        NULL,
830a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                        cr))->unref();
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                } else {
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    paint.setImageFilter(new SkDilateImageFilter(
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                        samples[i].fRadiusX,
870a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                        samples[i].fRadiusY,
880a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                        NULL,
890a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                        cr))->unref();
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                }
910a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                drawClippedBitmap(canvas, paint, i * 140, j * 140);
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef GM INHERITED;
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkBitmap fBitmap;
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool fOnce;
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//////////////////////////////////////////////////////////////////////////////
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic GM* MyFactory(void*) { return new MorphologyGM; }
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic GMRegistry reg(MyFactory);
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
108