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 
     57             SkPaint paint;
     58             paint.setImageFilter(std::move(merge));
     59             canvas->drawPaint(paint);
     60             canvas->translate(SkIntToScalar(100), 0);
     61         }
     62         {
     63             sk_sp<SkImageFilter> morph(SkDilateImageFilter::Make(5, 5, nullptr));
     64 
     65             SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
     66                                     0, SK_Scalar1, 0, 0, 0,
     67                                     0, 0, SK_Scalar1, 0, 0,
     68                                     0, 0, 0, 0.5f, 0 };
     69 
     70             sk_sp<SkColorFilter> matrixFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
     71             sk_sp<SkImageFilter> colorMorph(SkColorFilterImageFilter::Make(std::move(matrixFilter),
     72                                                                            std::move(morph)));
     73             SkPaint paint;
     74             paint.setImageFilter(SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver,
     75                                                              std::move(colorMorph)));
     76 
     77             DrawClippedImage(canvas, fImage.get(), paint);
     78             canvas->translate(SkIntToScalar(100), 0);
     79         }
     80         {
     81             SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
     82                                     0, SK_Scalar1, 0, 0, 0,
     83                                     0, 0, SK_Scalar1, 0, 0,
     84                                     0, 0, 0, 0.5f, 0 };
     85             sk_sp<SkColorFilter> matrixCF(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
     86             sk_sp<SkImageFilter> matrixFilter(SkColorFilterImageFilter::Make(std::move(matrixCF),
     87                                                                              nullptr));
     88             sk_sp<SkImageFilter> offsetFilter(SkOffsetImageFilter::Make(10.0f, 10.f,
     89                                                                         matrixFilter));
     90 
     91             SkPaint paint;
     92             paint.setImageFilter(SkArithmeticImageFilter::Make(
     93                     0, 1, 1, 0, true, std::move(matrixFilter), std::move(offsetFilter), nullptr));
     94 
     95             DrawClippedImage(canvas, fImage.get(), paint);
     96             canvas->translate(SkIntToScalar(100), 0);
     97         }
     98         {
     99             sk_sp<SkImageFilter> blur(SkBlurImageFilter::Make(SkIntToScalar(10),
    100                                                               SkIntToScalar(10),
    101                                                               nullptr));
    102 
    103             SkImageFilter::CropRect cropRect(SkRect::MakeWH(SkIntToScalar(95), SkIntToScalar(100)));
    104             SkPaint paint;
    105             paint.setImageFilter(
    106                 SkXfermodeImageFilter::Make(SkBlendMode::kSrcIn, std::move(blur), nullptr,
    107                                             &cropRect));
    108             DrawClippedImage(canvas, fImage.get(), paint);
    109             canvas->translate(SkIntToScalar(100), 0);
    110         }
    111         {
    112             // Dilate -> matrix convolution.
    113             // This tests that a filter using asFragmentProcessor (matrix
    114             // convolution) correctly handles a non-zero source offset
    115             // (supplied by the dilate).
    116             sk_sp<SkImageFilter> dilate(SkDilateImageFilter::Make(5, 5, nullptr));
    117 
    118             SkScalar kernel[9] = {
    119                 SkIntToScalar(-1), SkIntToScalar( -1 ), SkIntToScalar(-1),
    120                 SkIntToScalar(-1), SkIntToScalar(  7 ), SkIntToScalar(-1),
    121                 SkIntToScalar(-1), SkIntToScalar( -1 ), SkIntToScalar(-1),
    122             };
    123             SkISize kernelSize = SkISize::Make(3, 3);
    124             SkScalar gain = 1.0f, bias = SkIntToScalar(0);
    125             SkIPoint kernelOffset = SkIPoint::Make(1, 1);
    126             auto tileMode = SkMatrixConvolutionImageFilter::kClamp_TileMode;
    127             bool convolveAlpha = false;
    128             sk_sp<SkImageFilter> convolve(SkMatrixConvolutionImageFilter::Make(kernelSize,
    129                                                                                kernel,
    130                                                                                gain,
    131                                                                                bias,
    132                                                                                kernelOffset,
    133                                                                                tileMode,
    134                                                                                convolveAlpha,
    135                                                                                std::move(dilate)));
    136 
    137             SkPaint paint;
    138             paint.setImageFilter(std::move(convolve));
    139             DrawClippedImage(canvas, fImage.get(), paint);
    140             canvas->translate(SkIntToScalar(100), 0);
    141         }
    142         {
    143             // Test that crop offsets are absolute, not relative to the parent's crop rect.
    144             sk_sp<SkColorFilter> cf1(SkColorFilter::MakeModeFilter(SK_ColorBLUE,
    145                                                                    SkBlendMode::kSrcIn));
    146             sk_sp<SkColorFilter> cf2(SkColorFilter::MakeModeFilter(SK_ColorGREEN,
    147                                                                    SkBlendMode::kSrcIn));
    148             SkImageFilter::CropRect outerRect(SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(10),
    149                                                                SkIntToScalar(80), SkIntToScalar(80)));
    150             SkImageFilter::CropRect innerRect(SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20),
    151                                                                SkIntToScalar(60), SkIntToScalar(60)));
    152             sk_sp<SkImageFilter> color1(SkColorFilterImageFilter::Make(std::move(cf1),
    153                                                                        nullptr,
    154                                                                        &outerRect));
    155             sk_sp<SkImageFilter> color2(SkColorFilterImageFilter::Make(std::move(cf2),
    156                                                                        std::move(color1),
    157                                                                        &innerRect));
    158 
    159             SkPaint paint;
    160             paint.setImageFilter(std::move(color2));
    161             paint.setColor(SK_ColorRED);
    162             canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), paint);
    163             canvas->translate(SkIntToScalar(100), 0);
    164         }
    165     }
    166 
    167 private:
    168     static void DrawClippedImage(SkCanvas* canvas, const SkImage* image, const SkPaint& paint) {
    169         canvas->save();
    170         canvas->clipRect(SkRect::MakeIWH(image->width(), image->height()));
    171         canvas->drawImage(image, 0, 0, &paint);
    172         canvas->restore();
    173     }
    174 
    175     sk_sp<SkImage> fImage;
    176 
    177     typedef GM INHERITED;
    178 };
    179 
    180 ///////////////////////////////////////////////////////////////////////////////
    181 
    182 DEF_GM(return new ImageFiltersGraphGM;)
    183