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