senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 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 | #ifndef SkMatrixConvolutionImageFilter_DEFINED |
| 9 | #define SkMatrixConvolutionImageFilter_DEFINED |
| 10 | |
senorblanco@chromium.org | 377c14a | 2013-02-04 22:57:21 +0000 | [diff] [blame] | 11 | #include "SkImageFilter.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 12 | #include "SkScalar.h" |
| 13 | #include "SkSize.h" |
| 14 | #include "SkPoint.h" |
| 15 | |
senorblanco | 32eaa89 | 2016-04-21 06:49:15 -0700 | [diff] [blame] | 16 | class SkBitmap; |
| 17 | |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 18 | /*! \class SkMatrixConvolutionImageFilter |
| 19 | Matrix convolution image filter. This filter applies an NxM image |
| 20 | processing kernel to a given input image. This can be used to produce |
| 21 | effects such as sharpening, blurring, edge detection, etc. |
| 22 | */ |
| 23 | |
senorblanco@chromium.org | 377c14a | 2013-02-04 22:57:21 +0000 | [diff] [blame] | 24 | class SK_API SkMatrixConvolutionImageFilter : public SkImageFilter { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 25 | public: |
| 26 | /*! \enum TileMode */ |
| 27 | enum TileMode { |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 28 | kClamp_TileMode = 0, /*!< Clamp to the image's edge pixels. */ |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 29 | kRepeat_TileMode, /*!< Wrap around to the image's opposite edge. */ |
| 30 | kClampToBlack_TileMode, /*!< Fill with transparent black. */ |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 31 | kMax_TileMode = kClampToBlack_TileMode |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
robertphillips | dada4dd | 2016-04-13 04:54:36 -0700 | [diff] [blame] | 34 | ~SkMatrixConvolutionImageFilter() override; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 36 | /** Construct a matrix convolution image filter. |
| 37 | @param kernelSize The kernel size in pixels, in each dimension (N by M). |
| 38 | @param kernel The image processing kernel. Must contain N * M |
| 39 | elements, in row order. |
| 40 | @param gain A scale factor applied to each pixel after |
| 41 | convolution. This can be used to normalize the |
| 42 | kernel, if it does not sum to 1. |
| 43 | @param bias A bias factor added to each pixel after convolution. |
| 44 | @param kernelOffset An offset applied to each pixel coordinate before |
| 45 | convolution. This can be used to center the kernel |
| 46 | over the image (e.g., a 3x3 kernel should have an |
| 47 | offset of {1, 1}). |
| 48 | @param tileMode How accesses outside the image are treated. (@see |
| 49 | TileMode). |
| 50 | @param convolveAlpha If true, all channels are convolved. If false, |
| 51 | only the RGB channels are convolved, and |
| 52 | alpha is copied from the source image. |
| 53 | @param input The input image filter. If NULL, the src bitmap |
| 54 | passed to filterImage() is used instead. |
| 55 | @param cropRect The rectangle to which the output processing will be limited. |
| 56 | */ |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 57 | static sk_sp<SkImageFilter> Make(const SkISize& kernelSize, |
| 58 | const SkScalar* kernel, |
| 59 | SkScalar gain, |
| 60 | SkScalar bias, |
| 61 | const SkIPoint& kernelOffset, |
| 62 | TileMode tileMode, |
| 63 | bool convolveAlpha, |
| 64 | sk_sp<SkImageFilter> input, |
| 65 | const CropRect* cropRect = nullptr); |
| 66 | |
| 67 | SK_TO_STRING_OVERRIDE() |
| 68 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMatrixConvolutionImageFilter) |
| 69 | |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 70 | protected: |
commit-bot@chromium.org | bd0be25 | 2014-05-15 15:40:41 +0000 | [diff] [blame] | 71 | SkMatrixConvolutionImageFilter(const SkISize& kernelSize, |
| 72 | const SkScalar* kernel, |
| 73 | SkScalar gain, |
| 74 | SkScalar bias, |
| 75 | const SkIPoint& kernelOffset, |
| 76 | TileMode tileMode, |
| 77 | bool convolveAlpha, |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 78 | sk_sp<SkImageFilter> input, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 79 | const CropRect* cropRect); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 80 | void flatten(SkWriteBuffer&) const override; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 81 | |
robertphillips | dada4dd | 2016-04-13 04:54:36 -0700 | [diff] [blame] | 82 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, |
| 83 | SkIPoint* offset) const override; |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 84 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override; |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 85 | SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&, MapDirection) const override; |
senorblanco | 6db0a7b | 2016-04-01 16:41:10 -0700 | [diff] [blame] | 86 | bool affectsTransparentBlack() const override; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | SkISize fKernelSize; |
| 90 | SkScalar* fKernel; |
| 91 | SkScalar fGain; |
| 92 | SkScalar fBias; |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 93 | SkIPoint fKernelOffset; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 94 | TileMode fTileMode; |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 95 | bool fConvolveAlpha; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 96 | |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 97 | template <class PixelFetcher, bool convolveAlpha> |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 98 | void filterPixels(const SkBitmap& src, |
| 99 | SkBitmap* result, |
| 100 | const SkIRect& rect, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 101 | const SkIRect& bounds) const; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 102 | template <class PixelFetcher> |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 103 | void filterPixels(const SkBitmap& src, |
| 104 | SkBitmap* result, |
| 105 | const SkIRect& rect, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 106 | const SkIRect& bounds) const; |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 107 | void filterInteriorPixels(const SkBitmap& src, |
| 108 | SkBitmap* result, |
| 109 | const SkIRect& rect, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 110 | const SkIRect& bounds) const; |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 111 | void filterBorderPixels(const SkBitmap& src, |
| 112 | SkBitmap* result, |
| 113 | const SkIRect& rect, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 114 | const SkIRect& bounds) const; |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 115 | |
| 116 | typedef SkImageFilter INHERITED; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | #endif |