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