OLD | NEW |
(Empty) | |
| 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" |
| 16 |
| 17 static SkImage* make_image(SkCanvas* rootCanvas) { |
| 18 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 19 SkAutoTUnref<SkSurface> surface(rootCanvas->newSurface(info)); |
| 20 if (!surface) { |
| 21 surface.reset(SkSurface::NewRaster(info)); |
| 22 } |
| 23 |
| 24 SkPaint paint; |
| 25 paint.setAntiAlias(true); |
| 26 paint.setColor(SK_ColorRED); |
| 27 surface->getCanvas()->drawCircle(50, 50, 50, paint); |
| 28 return surface->newImageSnapshot(); |
| 29 } |
| 30 |
| 31 typedef SkImageFilter* (*ImageFilterFactory)(); |
| 32 |
| 33 // +[]{...} did not work on windows (VS) |
| 34 // (ImageFilterFactory)[]{...} did not work on linux (gcc) |
| 35 // hence this cast function |
| 36 template <typename T> ImageFilterFactory IFCCast(T arg) { return arg; } |
| 37 |
| 38 // Show the effect of localmatriximagefilter with various matrices, on various f
ilters |
| 39 class LocalMatrixImageFilterGM : public skiagm::GM { |
| 40 public: |
| 41 LocalMatrixImageFilterGM() {} |
| 42 |
| 43 protected: |
| 44 SkString onShortName() override { |
| 45 return SkString("localmatriximagefilter"); |
| 46 } |
| 47 |
| 48 SkISize onISize() override { |
| 49 return SkISize::Make(640, 640); |
| 50 } |
| 51 |
| 52 static void show_image(SkCanvas* canvas, SkImage* image, SkImageFilter* filt
er) { |
| 53 SkPaint paint; |
| 54 paint.setStyle(SkPaint::kStroke_Style); |
| 55 SkRect r = SkRect::MakeIWH(image->width(), image->height()).makeOutset(S
K_ScalarHalf, |
| 56 S
K_ScalarHalf); |
| 57 canvas->drawRect(r, paint); |
| 58 |
| 59 paint.setStyle(SkPaint::kFill_Style); |
| 60 paint.setImageFilter(filter); |
| 61 canvas->drawImage(image, 0, 0, &paint); |
| 62 } |
| 63 |
| 64 void onDraw(SkCanvas* canvas) override { |
| 65 SkAutoTUnref<SkImage> image0(make_image(canvas)); |
| 66 |
| 67 const ImageFilterFactory factories[] = { |
| 68 IFCCast([]{ return SkBlurImageFilter::Create(8, 8); }), |
| 69 IFCCast([]{ return SkDilateImageFilter::Create(8, 8); }), |
| 70 IFCCast([]{ return SkErodeImageFilter::Create(8, 8); }), |
| 71 IFCCast([]{ return SkOffsetImageFilter::Create(8, 8); }), |
| 72 }; |
| 73 |
| 74 const SkMatrix matrices[] = { |
| 75 SkMatrix::MakeScale(SK_ScalarHalf, SK_ScalarHalf), |
| 76 SkMatrix::MakeScale(2, 2), |
| 77 SkMatrix::MakeTrans(10, 10) |
| 78 }; |
| 79 |
| 80 const SkScalar spacer = image0->width() * 3.0f / 2; |
| 81 |
| 82 canvas->translate(40, 40); |
| 83 for (auto&& factory : factories) { |
| 84 SkAutoTUnref<SkImageFilter> filter(factory()); |
| 85 |
| 86 canvas->save(); |
| 87 show_image(canvas, image0, filter); |
| 88 for (const auto& matrix : matrices) { |
| 89 SkAutoTUnref<SkImageFilter> localFilter( |
| 90 SkImageFilter::CreateLocalMatrixFilter(matrix, filter)); |
| 91 canvas->translate(spacer, 0); |
| 92 show_image(canvas, image0, localFilter); |
| 93 } |
| 94 canvas->restore(); |
| 95 canvas->translate(0, spacer); |
| 96 } |
| 97 } |
| 98 |
| 99 private: |
| 100 typedef GM INHERITED; |
| 101 }; |
| 102 DEF_GM( return new LocalMatrixImageFilterGM; ) |
| 103 |
OLD | NEW |