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 700
     12 #define HEIGHT 560
     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.allocN32Pixels(135, 135);
     30         SkCanvas canvas(fBitmap);
     31         canvas.clear(0x0);
     32         SkPaint paint;
     33         paint.setAntiAlias(true);
     34         sk_tool_utils::set_portable_typeface(&paint);
     35         const char* str1 = "ABC";
     36         const char* str2 = "XYZ";
     37         paint.setColor(0xFFFFFFFF);
     38         paint.setTextSize(64);
     39         canvas.drawText(str1, strlen(str1), 10, 55, paint);
     40         canvas.drawText(str2, strlen(str2), 10, 110, paint);
     41     }
     42 
     43     virtual SkISize onISize() {
     44         return SkISize::Make(WIDTH, HEIGHT);
     45     }
     46 
     47     void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
     48         canvas->save();
     49         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
     50         canvas->clipRect(SkRect::MakeWH(
     51           SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
     52         canvas->drawBitmap(fBitmap, 0, 0, &paint);
     53         canvas->restore();
     54     }
     55 
     56     virtual void onDraw(SkCanvas* canvas) {
     57         if (!fOnce) {
     58             make_bitmap();
     59             fOnce = true;
     60         }
     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         SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(25, 20, 100, 80));
     73 
     74         for (unsigned j = 0; j < 4; ++j) {
     75             for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
     76                 const SkImageFilter::CropRect* cr = j & 0x02 ? &cropRect : NULL;
     77                 if (j & 0x01) {
     78                     paint.setImageFilter(SkErodeImageFilter::Create(
     79                         samples[i].fRadiusX,
     80                         samples[i].fRadiusY,
     81                         NULL,
     82                         cr))->unref();
     83                 } else {
     84                     paint.setImageFilter(SkDilateImageFilter::Create(
     85                         samples[i].fRadiusX,
     86                         samples[i].fRadiusY,
     87                         NULL,
     88                         cr))->unref();
     89                 }
     90                 drawClippedBitmap(canvas, paint, i * 140, j * 140);
     91             }
     92         }
     93     }
     94 
     95 private:
     96     typedef GM INHERITED;
     97     SkBitmap fBitmap;
     98     bool fOnce;
     99 };
    100 
    101 //////////////////////////////////////////////////////////////////////////////
    102 
    103 static GM* MyFactory(void*) { return new MorphologyGM; }
    104 static GMRegistry reg(MyFactory);
    105 
    106 }
    107