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 fWidth, fHeight;
     54             int fRadiusX, fRadiusY;
     55         } samples[] = {
     56             { 140, 140,   0,   0 },
     57             { 140, 140,   0,   2 },
     58             { 140, 140,   2,   0 },
     59             { 140, 140,   2,   2 },
     60             {  24,  24,  25,  25 },
     61         };
     62         SkPaint paint;
     63         for (unsigned j = 0; j < 2; ++j) {
     64             for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
     65                 SkScalar x = SkIntToScalar(i * 140), y = SkIntToScalar(j * 140);
     66                 if (j) {
     67                     paint.setImageFilter(new SkErodeImageFilter(
     68                         samples[i].fRadiusX,
     69                         samples[i].fRadiusY))->unref();
     70                 } else {
     71                     paint.setImageFilter(new SkDilateImageFilter(
     72                         samples[i].fRadiusX,
     73                         samples[i].fRadiusY))->unref();
     74                 }
     75                 SkRect bounds = SkRect::MakeXYWH(
     76                     x,
     77                     y,
     78                     SkIntToScalar(samples[i].fWidth),
     79                     SkIntToScalar(samples[i].fHeight));
     80                 canvas->saveLayer(&bounds, &paint);
     81                 canvas->drawBitmap(fBitmap, x, y);
     82                 canvas->restore();
     83             }
     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