senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 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 | |
| 10 | #include "SkBitmapSource.h" |
| 11 | #include "SkBlurImageFilter.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 12 | #include "SkColorFilter.h" |
senorblanco@chromium.org | 985fa79 | 2012-10-24 15:14:26 +0000 | [diff] [blame] | 13 | #include "SkColorMatrixFilter.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 14 | #include "SkColorFilterImageFilter.h" |
senorblanco@chromium.org | 4a9a612 | 2012-12-04 14:18:50 +0000 | [diff] [blame] | 15 | #include "SkMergeImageFilter.h" |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 16 | #include "SkMorphologyImageFilter.h" |
senorblanco@chromium.org | 350b4d5 | 2013-08-01 14:59:05 +0000 | [diff] [blame] | 17 | #include "SkXfermodeImageFilter.h" |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 18 | |
| 19 | #include "SkTestImageFilters.h" |
| 20 | |
| 21 | /////////////////////////////////////////////////////////////////////////////// |
| 22 | |
| 23 | class ImageFiltersGraphGM : public skiagm::GM { |
| 24 | public: |
| 25 | ImageFiltersGraphGM() : fInitialized(false) {} |
| 26 | |
| 27 | protected: |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 28 | virtual SkString onShortName() { |
| 29 | return SkString("imagefiltersgraph"); |
| 30 | } |
| 31 | |
| 32 | void make_bitmap() { |
| 33 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 34 | fBitmap.allocPixels(); |
robertphillips@google.com | 9b051a3 | 2013-08-20 20:06:40 +0000 | [diff] [blame^] | 35 | SkDevice device(fBitmap); |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 36 | SkCanvas canvas(&device); |
| 37 | canvas.clear(0x00000000); |
| 38 | SkPaint paint; |
| 39 | paint.setAntiAlias(true); |
| 40 | paint.setColor(0xFFFFFFFF); |
| 41 | paint.setTextSize(SkIntToScalar(96)); |
| 42 | const char* str = "e"; |
| 43 | canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint); |
| 44 | } |
| 45 | |
senorblanco@chromium.org | 985fa79 | 2012-10-24 15:14:26 +0000 | [diff] [blame] | 46 | virtual SkISize onISize() { return SkISize::Make(200, 100); } |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 47 | |
| 48 | virtual void onDraw(SkCanvas* canvas) { |
| 49 | if (!fInitialized) { |
| 50 | this->make_bitmap(); |
| 51 | fInitialized = true; |
| 52 | } |
| 53 | canvas->clear(0x00000000); |
senorblanco@chromium.org | 985fa79 | 2012-10-24 15:14:26 +0000 | [diff] [blame] | 54 | { |
| 55 | SkAutoTUnref<SkImageFilter> bitmapSource(new SkBitmapSource(fBitmap)); |
| 56 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, |
| 57 | SkXfermode::kSrcIn_Mode)); |
| 58 | SkAutoTUnref<SkImageFilter> blur(new SkBlurImageFilter(4.0f, 4.0f, bitmapSource)); |
| 59 | SkAutoTUnref<SkImageFilter> erode(new SkErodeImageFilter(4, 4, blur)); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 60 | SkAutoTUnref<SkImageFilter> color(SkColorFilterImageFilter::Create(cf, erode)); |
senorblanco@chromium.org | 985fa79 | 2012-10-24 15:14:26 +0000 | [diff] [blame] | 61 | SkAutoTUnref<SkImageFilter> merge(new SkMergeImageFilter(blur, color)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 62 | |
senorblanco@chromium.org | 985fa79 | 2012-10-24 15:14:26 +0000 | [diff] [blame] | 63 | SkPaint paint; |
| 64 | paint.setImageFilter(merge); |
| 65 | canvas->drawPaint(paint); |
| 66 | } |
| 67 | { |
| 68 | SkAutoTUnref<SkImageFilter> morph(new SkDilateImageFilter(5, 5)); |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 69 | |
senorblanco@chromium.org | 985fa79 | 2012-10-24 15:14:26 +0000 | [diff] [blame] | 70 | SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0, |
| 71 | 0, SK_Scalar1, 0, 0, 0, |
| 72 | 0, 0, SK_Scalar1, 0, 0, |
| 73 | 0, 0, 0, SkFloatToScalar(0.5f), 0 }; |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 74 | |
senorblanco@chromium.org | 985fa79 | 2012-10-24 15:14:26 +0000 | [diff] [blame] | 75 | SkAutoTUnref<SkColorFilter> matrixFilter(new SkColorMatrixFilter(matrix)); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 76 | SkAutoTUnref<SkImageFilter> colorMorph(SkColorFilterImageFilter::Create(matrixFilter, morph)); |
senorblanco@chromium.org | 350b4d5 | 2013-08-01 14:59:05 +0000 | [diff] [blame] | 77 | SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
| 78 | SkAutoTUnref<SkImageFilter> blendColor(new SkXfermodeImageFilter(mode, colorMorph)); |
senorblanco@chromium.org | 985fa79 | 2012-10-24 15:14:26 +0000 | [diff] [blame] | 79 | |
| 80 | SkPaint paint; |
| 81 | paint.setImageFilter(blendColor); |
| 82 | canvas->drawBitmap(fBitmap, 100, 0, &paint); |
| 83 | } |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private: |
| 87 | typedef GM INHERITED; |
| 88 | SkBitmap fBitmap; |
| 89 | bool fInitialized; |
| 90 | }; |
| 91 | |
| 92 | /////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | static skiagm::GM* MyFactory(void*) { return new ImageFiltersGraphGM; } |
| 95 | static skiagm::GMRegistry reg(MyFactory); |