senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +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 | #include "SkCanvas.h" |
| 10 | #include "SkColorMatrixFilter.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkShader.h" |
| 13 | |
| 14 | #include "SkBlurImageFilter.h" |
| 15 | #include "SkColorFilterImageFilter.h" |
| 16 | |
| 17 | #define FILTER_WIDTH SkIntToScalar(30) |
| 18 | #define FILTER_HEIGHT SkIntToScalar(30) |
| 19 | #define MARGIN SkIntToScalar(10) |
| 20 | |
| 21 | static SkImageFilter* make_blur(float amount, SkImageFilter* input = NULL) { |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 22 | return SkBlurImageFilter::Create(amount, amount, input); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | static SkImageFilter* make_brightness(float amount, SkImageFilter* input = NULL) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 26 | SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255)); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 27 | SkScalar matrix[20] = { 1, 0, 0, 0, amount255, |
| 28 | 0, 1, 0, 0, amount255, |
| 29 | 0, 0, 1, 0, amount255, |
| 30 | 0, 0, 0, 1, 0 }; |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 31 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 32 | return SkColorFilterImageFilter::Create(filter, input); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static SkImageFilter* make_grayscale(SkImageFilter* input = NULL) { |
| 36 | SkScalar matrix[20]; |
| 37 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 38 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 39 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 40 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 41 | matrix[18] = 1.0f; |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 42 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 43 | return SkColorFilterImageFilter::Create(filter, input); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static SkImageFilter* make_mode_blue(SkImageFilter* input = NULL) { |
| 47 | SkAutoTUnref<SkColorFilter> filter( |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame^] | 48 | SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrc_Mode)); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 49 | return SkColorFilterImageFilter::Create(filter, input); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | class ColorFilterImageFilterGM : public skiagm::GM { |
| 53 | public: |
| 54 | ColorFilterImageFilterGM () {} |
| 55 | |
| 56 | protected: |
| 57 | |
| 58 | virtual SkString onShortName() { |
| 59 | return SkString("colorfilterimagefilter"); |
| 60 | } |
| 61 | |
| 62 | void drawClippedRect(SkCanvas* canvas, const SkRect& r, const SkPaint& paint, float outset = 0.0f) { |
| 63 | canvas->save(); |
| 64 | SkRect clip(r); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 65 | clip.outset(outset, outset); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 66 | canvas->clipRect(clip); |
| 67 | canvas->drawRect(r, paint); |
| 68 | canvas->restore(); |
| 69 | } |
| 70 | |
| 71 | virtual SkISize onISize() { return SkISize::Make(400, 100); } |
| 72 | |
| 73 | virtual void onDraw(SkCanvas* canvas) { |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 74 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 75 | SkRect r = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT); |
| 76 | SkPaint paint; |
| 77 | paint.setColor(SK_ColorRED); |
| 78 | canvas->save(); |
| 79 | for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.2f) { |
| 80 | SkAutoTUnref<SkImageFilter> dim(make_brightness(-brightness)); |
| 81 | SkAutoTUnref<SkImageFilter> bright(make_brightness(brightness, dim)); |
| 82 | paint.setImageFilter(bright); |
| 83 | drawClippedRect(canvas, r, paint); |
| 84 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 85 | } |
| 86 | canvas->restore(); |
| 87 | canvas->translate(0, FILTER_HEIGHT + MARGIN); |
| 88 | { |
| 89 | SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f)); |
| 90 | SkAutoTUnref<SkImageFilter> grayscale(make_grayscale(brightness)); |
| 91 | paint.setImageFilter(grayscale); |
| 92 | drawClippedRect(canvas, r, paint); |
| 93 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 94 | } |
| 95 | { |
| 96 | SkAutoTUnref<SkImageFilter> grayscale(make_grayscale()); |
| 97 | SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f, grayscale)); |
| 98 | paint.setImageFilter(brightness); |
| 99 | drawClippedRect(canvas, r, paint); |
| 100 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 101 | } |
| 102 | { |
| 103 | SkAutoTUnref<SkImageFilter> blue(make_mode_blue()); |
| 104 | SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f, blue)); |
| 105 | paint.setImageFilter(brightness); |
| 106 | drawClippedRect(canvas, r, paint); |
| 107 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 108 | } |
| 109 | { |
| 110 | SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f)); |
| 111 | SkAutoTUnref<SkImageFilter> blue(make_mode_blue(brightness)); |
| 112 | paint.setImageFilter(blue); |
| 113 | drawClippedRect(canvas, r, paint); |
| 114 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 115 | } |
| 116 | { |
| 117 | SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f)); |
| 118 | SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.5f, blur)); |
| 119 | paint.setImageFilter(brightness); |
| 120 | drawClippedRect(canvas, r, paint, 3); |
| 121 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 122 | } |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame^] | 123 | { |
| 124 | SkAutoTUnref<SkImageFilter> blue(make_mode_blue()); |
| 125 | paint.setImageFilter(blue.get()); |
| 126 | drawClippedRect(canvas, r, paint, 5); |
| 127 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 128 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | private: |
| 132 | typedef GM INHERITED; |
| 133 | }; |
| 134 | |
| 135 | ////////////////////////////////////////////////////////////////////////////// |
| 136 | |
| 137 | static skiagm::GM* MyFactory(void*) { return new ColorFilterImageFilterGM; } |
| 138 | static skiagm::GMRegistry reg(MyFactory); |