senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "SkBlurImageFilter.h" |
| 11 | #include "SkColorFilterImageFilter.h" |
| 12 | #include "SkModeColorFilter.h" |
| 13 | #include "SkMorphologyImageFilter.h" |
| 14 | #include "SkOffsetImageFilter.h" |
| 15 | #include "SkSurface.h" |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 16 | #include "sk_tool_utils.h" |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 17 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 18 | static sk_sp<SkImage> make_image(SkCanvas* rootCanvas) { |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 19 | SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
Cary Clark | a24712e | 2018-09-05 18:41:40 +0000 | [diff] [blame] | 20 | auto surface(sk_tool_utils::makeSurface(rootCanvas, info)); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 21 | |
| 22 | SkPaint paint; |
| 23 | paint.setAntiAlias(true); |
| 24 | paint.setColor(SK_ColorRED); |
| 25 | surface->getCanvas()->drawCircle(50, 50, 50, paint); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 26 | return surface->makeImageSnapshot(); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Stephen White | 930f69e | 2017-01-12 17:15:50 -0500 | [diff] [blame] | 29 | static void show_image(SkCanvas* canvas, SkImage* image, sk_sp<SkImageFilter> filter) { |
| 30 | SkPaint paint; |
| 31 | paint.setStyle(SkPaint::kStroke_Style); |
| 32 | SkRect r = SkRect::MakeIWH(image->width(), image->height()).makeOutset(SK_ScalarHalf, |
| 33 | SK_ScalarHalf); |
| 34 | canvas->drawRect(r, paint); |
| 35 | |
| 36 | paint.setStyle(SkPaint::kFill_Style); |
| 37 | paint.setImageFilter(filter); |
| 38 | canvas->drawImage(image, 0, 0, &paint); |
| 39 | } |
| 40 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 41 | typedef sk_sp<SkImageFilter> (*ImageFilterFactory)(); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 42 | |
| 43 | // +[]{...} did not work on windows (VS) |
| 44 | // (ImageFilterFactory)[]{...} did not work on linux (gcc) |
| 45 | // hence this cast function |
| 46 | template <typename T> ImageFilterFactory IFCCast(T arg) { return arg; } |
| 47 | |
| 48 | // Show the effect of localmatriximagefilter with various matrices, on various filters |
Stephen White | 930f69e | 2017-01-12 17:15:50 -0500 | [diff] [blame] | 49 | DEF_SIMPLE_GM(localmatriximagefilter, canvas, 640, 640) { |
| 50 | sk_sp<SkImage> image0(make_image(canvas)); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 51 | |
Stephen White | 930f69e | 2017-01-12 17:15:50 -0500 | [diff] [blame] | 52 | const ImageFilterFactory factories[] = { |
| 53 | IFCCast([]{ return SkBlurImageFilter::Make(8, 8, nullptr); }), |
| 54 | IFCCast([]{ return SkDilateImageFilter::Make(8, 8, nullptr); }), |
| 55 | IFCCast([]{ return SkErodeImageFilter::Make(8, 8, nullptr); }), |
| 56 | IFCCast([]{ return SkOffsetImageFilter::Make(8, 8, nullptr); }), |
| 57 | }; |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 58 | |
Stephen White | 930f69e | 2017-01-12 17:15:50 -0500 | [diff] [blame] | 59 | const SkMatrix matrices[] = { |
| 60 | SkMatrix::MakeScale(SK_ScalarHalf, SK_ScalarHalf), |
| 61 | SkMatrix::MakeScale(2, 2), |
| 62 | SkMatrix::MakeTrans(10, 10) |
| 63 | }; |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 64 | |
Stephen White | 930f69e | 2017-01-12 17:15:50 -0500 | [diff] [blame] | 65 | const SkScalar spacer = image0->width() * 3.0f / 2; |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 66 | |
Stephen White | 930f69e | 2017-01-12 17:15:50 -0500 | [diff] [blame] | 67 | canvas->translate(40, 40); |
| 68 | for (auto&& factory : factories) { |
| 69 | sk_sp<SkImageFilter> filter(factory()); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 70 | |
Stephen White | 930f69e | 2017-01-12 17:15:50 -0500 | [diff] [blame] | 71 | canvas->save(); |
| 72 | show_image(canvas, image0.get(), filter); |
| 73 | for (const auto& matrix : matrices) { |
| 74 | sk_sp<SkImageFilter> localFilter(filter->makeWithLocalMatrix(matrix)); |
| 75 | canvas->translate(spacer, 0); |
| 76 | show_image(canvas, image0.get(), std::move(localFilter)); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 77 | } |
Stephen White | 930f69e | 2017-01-12 17:15:50 -0500 | [diff] [blame] | 78 | canvas->restore(); |
| 79 | canvas->translate(0, spacer); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 80 | } |
Stephen White | 930f69e | 2017-01-12 17:15:50 -0500 | [diff] [blame] | 81 | } |