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 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 21 | static SkImageFilter* make_blur(float amount, SkImageFilter* input = nullptr) { |
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 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | static SkImageFilter* make_brightness(float amount, SkImageFilter* input = nullptr) { |
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 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | static SkImageFilter* make_grayscale(SkImageFilter* input = nullptr) { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 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 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 46 | static SkImageFilter* make_mode_blue(SkImageFilter* input = nullptr) { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 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 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame^] | 52 | static void drawClippedRect(SkCanvas* canvas, |
| 53 | const SkRect& r, |
| 54 | const SkPaint& paint, |
| 55 | float outset = 0.0f) { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 56 | canvas->save(); |
| 57 | SkRect clip(r); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 58 | clip.outset(outset, outset); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 59 | canvas->clipRect(clip); |
| 60 | canvas->drawRect(r, paint); |
| 61 | canvas->restore(); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame^] | 62 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 63 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame^] | 64 | DEF_SIMPLE_GM(colorfilterimagefilter, canvas, 400, 100){ |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 65 | SkRect r = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT); |
| 66 | SkPaint paint; |
| 67 | paint.setColor(SK_ColorRED); |
| 68 | canvas->save(); |
| 69 | for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.2f) { |
| 70 | SkAutoTUnref<SkImageFilter> dim(make_brightness(-brightness)); |
| 71 | SkAutoTUnref<SkImageFilter> bright(make_brightness(brightness, dim)); |
| 72 | paint.setImageFilter(bright); |
| 73 | drawClippedRect(canvas, r, paint); |
| 74 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 75 | } |
| 76 | canvas->restore(); |
| 77 | canvas->translate(0, FILTER_HEIGHT + MARGIN); |
| 78 | { |
| 79 | SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f)); |
| 80 | SkAutoTUnref<SkImageFilter> grayscale(make_grayscale(brightness)); |
| 81 | paint.setImageFilter(grayscale); |
| 82 | drawClippedRect(canvas, r, paint); |
| 83 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 84 | } |
| 85 | { |
| 86 | SkAutoTUnref<SkImageFilter> grayscale(make_grayscale()); |
| 87 | SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f, grayscale)); |
| 88 | paint.setImageFilter(brightness); |
| 89 | drawClippedRect(canvas, r, paint); |
| 90 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 91 | } |
| 92 | { |
| 93 | SkAutoTUnref<SkImageFilter> blue(make_mode_blue()); |
| 94 | SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f, blue)); |
| 95 | paint.setImageFilter(brightness); |
| 96 | drawClippedRect(canvas, r, paint); |
| 97 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 98 | } |
| 99 | { |
| 100 | SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f)); |
| 101 | SkAutoTUnref<SkImageFilter> blue(make_mode_blue(brightness)); |
| 102 | paint.setImageFilter(blue); |
| 103 | drawClippedRect(canvas, r, paint); |
| 104 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 105 | } |
| 106 | { |
| 107 | SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f)); |
| 108 | SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.5f, blur)); |
| 109 | paint.setImageFilter(brightness); |
| 110 | drawClippedRect(canvas, r, paint, 3); |
| 111 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 112 | } |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 113 | { |
| 114 | SkAutoTUnref<SkImageFilter> blue(make_mode_blue()); |
| 115 | paint.setImageFilter(blue.get()); |
| 116 | drawClippedRect(canvas, r, paint, 5); |
| 117 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 118 | } |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame^] | 119 | } |