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 "sk_tool_utils.h"
     10 
     11 #include "SkArithmeticImageFilter.h"
     12 #include "SkBlurImageFilter.h"
     13 #include "SkColorFilter.h"
     14 #include "SkColorFilterImageFilter.h"
     15 #include "SkColorMatrixFilter.h"
     16 #include "SkImage.h"
     17 #include "SkImageSource.h"
     18 #include "SkMatrixConvolutionImageFilter.h"
     19 #include "SkMergeImageFilter.h"
     20 #include "SkMorphologyImageFilter.h"
     21 #include "SkOffsetImageFilter.h"
     22 #include "SkReadBuffer.h"
     23 #include "SkSpecialImage.h"
     24 #include "SkSpecialSurface.h"
     25 #include "SkWriteBuffer.h"
     26 #include "SkXfermodeImageFilter.h"
     27 
     28 class ImageFiltersGraphGM : public skiagm::GM {
     29 public:
     30     ImageFiltersGraphGM() {}
     31 
     32 protected:
     33 
     34     SkString onShortName() override {
     35         return SkString("imagefiltersgraph");
     36     }
     37 
     38     SkISize onISize() override { return SkISize::Make(600, 150); }
     39 
     40     void onOnceBeforeDraw() override {
     41         fImage = SkImage::MakeFromBitmap(
     42             sk_tool_utils::create_string_bitmap(100, 100, SK_ColorWHITE, 20, 70, 96, "e"));
     43     }
     44 
     45     void onDraw(SkCanvas* canvas) override {
     46         canvas->clear(SK_ColorBLACK);
     47         {
     48             sk_sp<SkImageFilter> bitmapSource(SkImageSource::Make(fImage));
     49             sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorRED,
     50                                                                   SkBlendMode::kSrcIn));
     51             sk_sp<SkImageFilter> blur(SkBlurImageFilter::Make(4.0f, 4.0f, std::move(bitmapSource)));
     52             sk_sp<SkImageFilter> erode(SkErodeImageFilter::Make(4, 4, blur));
     53             sk_sp<SkImageFilter> color(SkColorFilterImageFilter::Make(std::move(cf),
     54                                                                       std::move(erode)));
     55             sk_sp<SkImageFilter> merge(SkMergeImageFilter::Make(blur, color,
     56                                                                 SkBlendMode::kSrcOver));
     57 
     58             SkPaint paint;
     59             paint.setImageFilter(std::move(merge));
     60             canvas->drawPaint(paint);
     61             canvas->translate(SkIntToScalar(100), 0);
     62         }
     63         {
     64             sk_sp<SkImageFilter> morph(SkDilateImageFilter::Make(5, 5, nullptr));
     65 
     66             SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
     67                                     0, SK_Scalar1, 0, 0, 0,
     68                                     0, 0, SK_Scalar1, 0, 0,
     69                                     0, 0, 0, 0.5f, 0 };
     70 
     71             sk_sp<SkColorFilter> matrixFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
     72             sk_sp<SkImageFilter> colorMorph(SkColorFilterImageFilter::Make(std::move(matrixFilter),
     73                                                                            std::move(morph)));
     74             SkPaint paint;
     75             paint.setImageFilter(SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver,
     76                                                              std::move(colorMorph)));
     77 
     78             DrawClippedImage(canvas, fImage.get(), paint);
     79             canvas->translate(SkIntToScalar(100), 0);
     80         }
     81         {
     82             SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
     83                                     0, SK_Scalar1, 0, 0, 0,
     84                                     0, 0, SK_Scalar1, 0, 0,
     85                                     0, 0, 0, 0.5f, 0 };
     86             sk_sp<SkColorFilter> matrixCF(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
     87             sk_sp<SkImageFilter> matrixFilter(SkColorFilterImageFilter::Make(std::move(matrixCF),
     88                                                                              nullptr));
     89             sk_sp<SkImageFilter> offsetFilter(SkOffsetImageFilter::Make(10.0f, 10.f,
     90                                                                         matrixFilter));
     91 
     92             SkPaint paint;
     93             paint.setImageFilter(SkArithmeticImageFilter::Make(
     94                     0, 1, 1, 0, true, std::move(matrixFilter), std::move(offsetFilter), nullptr));
     95 
     96             DrawClippedImage(canvas, fImage.get(), paint);
     97             canvas->translate(SkIntToScalar(100), 0);
     98         }
     99         {
    100             sk_sp<SkImageFilter> blur(SkBlurImageFilter::Make(SkIntToScalar(10),
    101                                                               SkIntToScalar(10),
    102                                                               nullptr));
    103 
    104             SkImageFilter::CropRect cropRect(SkRect::MakeWH(SkIntToScalar(95), SkIntToScalar(100)));
    105             SkPaint paint;
    106             paint.setImageFilter(
    107                 SkXfermodeImageFilter::Make(SkBlendMode::kSrcIn, std::move(blur), nullptr,
    108                                             &cropRect));
    109             DrawClippedImage(canvas, fImage.get(), paint);
    110             canvas->translate(SkIntToScalar(100), 0);
    111         }
    112         {
    113             // Dilate -> matrix convolution.
    114             // This tests that a filter using asFragmentProcessor (matrix
    115             // convolution) correctly handles a non-zero source offset
    116             // (supplied by the dilate).
    117             sk_sp<SkImageFilter> dilate(SkDilateImageFilter::Make(5, 5, nullptr));
    118 
    119             SkScalar kernel[9] = {
    120                 SkIntToScalar(-1), SkIntToScalar( -1 ), SkIntToScalar(-1),
    121                 SkIntToScalar(-1), SkIntToScalar(  7 ), SkIntToScalar(-1),
    122                 SkIntToScalar(-1), SkIntToScalar( -1 ), SkIntToScalar(-1),
    123             };
    124             SkISize kernelSize = SkISize::Make(3, 3);
    125             SkScalar gain = 1.0f, bias = SkIntToScalar(0);
    126             SkIPoint kernelOffset = SkIPoint::Make(1, 1);
    127             auto tileMode = SkMatrixConvolutionImageFilter::kClamp_TileMode;
    128             bool convolveAlpha = false;
    129             sk_sp<SkImageFilter> convolve(SkMatrixConvolutionImageFilter::Make(kernelSize,
    130                                                                                kernel,
    131                                                                                gain,
    132                                                                                bias,
    133                                                                                kernelOffset,
    134                                                                                tileMode,
    135                                                                                convolveAlpha,
    136                                                                                std::move(dilate)));
    137 
    138             SkPaint paint;
    139             paint.setImageFilter(std::move(convolve));
    140             DrawClippedImage(canvas, fImage.get(), paint);
    141             canvas->translate(SkIntToScalar(100), 0);
    142         }
    143         {
    144             // Test that crop offsets are absolute, not relative to the parent's crop rect.
    145             sk_sp<SkColorFilter> cf1(SkColorFilter::MakeModeFilter(SK_ColorBLUE,
    146                                                                    SkBlendMode::kSrcIn));
    147             sk_sp<SkColorFilter> cf2(SkColorFilter::MakeModeFilter(SK_ColorGREEN,
    148                                                                    SkBlendMode::kSrcIn));
    149             SkImageFilter::CropRect outerRect(SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(10),
    150                                                                SkIntToScalar(80), SkIntToScalar(80)));
    151             SkImageFilter::CropRect innerRect(SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20),
    152                                                                SkIntToScalar(60), SkIntToScalar(60)));
    153             sk_sp<SkImageFilter> color1(SkColorFilterImageFilter::Make(std::move(cf1),
    154                                                                        nullptr,
    155                                                                        &outerRect));
    156             sk_sp<SkImageFilter> color2(SkColorFilterImageFilter::Make(std::move(cf2),
    157                                                                        std::move(color1),
    158                                                                        &innerRect));
    159 
    160             SkPaint paint;
    161             paint.setImageFilter(std::move(color2));
    162             paint.setColor(SK_ColorRED);
    163             canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), paint);
    164             canvas->translate(SkIntToScalar(100), 0);
    165         }
    166     }
    167 
    168 private:
    169     static void DrawClippedImage(SkCanvas* canvas, const SkImage* image, const SkPaint& paint) {
    170         canvas->save();
    171         canvas->clipRect(SkRect::MakeIWH(image->width(), image->height()));
    172         canvas->drawImage(image, 0, 0, &paint);
    173         canvas->restore();
    174     }
    175 
    176     sk_sp<SkImage> fImage;
    177 
    178     typedef GM INHERITED;
    179 };
    180 
    181 ///////////////////////////////////////////////////////////////////////////////
    182 
    183 DEF_GM(return new ImageFiltersGraphGM;)
    184