Home | History | Annotate | Download | only in gm
      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.h"
      9 #include "SkMorphologyImageFilter.h"
     10 
     11 #define WIDTH 640
     12 #define HEIGHT 480
     13 
     14 namespace skiagm {
     15 
     16 class MorphologyGM : public GM {
     17 public:
     18     MorphologyGM() {
     19         this->setBGColor(0xFF000000);
     20         fOnce = false;
     21     }
     22 
     23 protected:
     24     virtual SkString onShortName() {
     25         return SkString("morphology");
     26     }
     27 
     28     void make_bitmap() {
     29         fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 135, 135);
     30         fBitmap.allocPixels();
     31         SkDevice device(fBitmap);
     32         SkCanvas canvas(&device);
     33         canvas.clear(0x0);
     34         SkPaint paint;
     35         paint.setAntiAlias(true);
     36         const char* str1 = "ABC";
     37         const char* str2 = "XYZ";
     38         paint.setColor(0xFFFFFFFF);
     39         paint.setTextSize(64);
     40         canvas.drawText(str1, strlen(str1), 10, 55, paint);
     41         canvas.drawText(str2, strlen(str2), 10, 110, paint);
     42     }
     43 
     44     virtual SkISize onISize() {
     45         return make_isize(WIDTH, HEIGHT);
     46     }
     47     virtual void onDraw(SkCanvas* canvas) {
     48         if (!fOnce) {
     49             make_bitmap();
     50             fOnce = true;
     51         }
     52         struct {
     53             int fRadiusX, fRadiusY;
     54             bool erode;
     55             SkScalar fX, fY;
     56         } samples[] = {
     57             { 0, 0, false, 0,   0 },
     58             { 0, 2, false, 140, 0 },
     59             { 2, 0, false, 280, 0 },
     60             { 2, 2, false, 420, 0 },
     61             { 0, 0, true,  0,   140 },
     62             { 0, 2, true,  140, 140 },
     63             { 2, 0, true,  280, 140 },
     64             { 2, 2, true,  420, 140 },
     65         };
     66         const char* str = "The quick brown fox jumped over the lazy dog.";
     67         SkPaint paint;
     68         for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
     69             if (samples[i].erode) {
     70                 paint.setImageFilter(new SkErodeImageFilter(
     71                     samples[i].fRadiusX,
     72                     samples[i].fRadiusY))->unref();
     73             } else {
     74                 paint.setImageFilter(new SkDilateImageFilter(
     75                     samples[i].fRadiusX,
     76                     samples[i].fRadiusY))->unref();
     77             }
     78             SkRect bounds = SkRect::MakeXYWH(samples[i].fX,
     79                                              samples[i].fY,
     80                                              140, 140);
     81             canvas->saveLayer(&bounds, &paint);
     82             canvas->drawBitmap(fBitmap, samples[i].fX, samples[i].fY);
     83             canvas->restore();
     84         }
     85     }
     86 
     87 private:
     88     typedef GM INHERITED;
     89     SkBitmap fBitmap;
     90     bool fOnce;
     91 };
     92 
     93 //////////////////////////////////////////////////////////////////////////////
     94 
     95 static GM* MyFactory(void*) { return new MorphologyGM; }
     96 static GMRegistry reg(MyFactory);
     97 
     98 }
     99