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 | |
| 31 | /** Construct a matrix convolution image filter. |
| 32 | @param kernelSize The kernel size in pixels, in each dimension (N by M). |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 33 | @param kernel The image processing kernel. Must contain N * M |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 34 | elements, in row order. |
| 35 | @param gain A scale factor applied to each pixel after |
| 36 | convolution. This can be used to normalize the |
| 37 | kernel, if it does not sum to 1. |
| 38 | @param bias A bias factor added to each pixel after convolution. |
| 39 | @param target An offset applied to each pixel coordinate before |
| 40 | convolution. This can be used to center the kernel |
| 41 | over the image (e.g., a 3x3 kernel should have a |
| 42 | target of {1, 1}). |
| 43 | @param tileMode How accesses outside the image are treated. (@see |
| 44 | TileMode). |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 45 | @param convolveAlpha If true, all channels are convolved. If false, |
| 46 | only the RGB channels are convolved, and |
| 47 | alpha is copied from the source image. |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 48 | @param input The input image filter. If NULL, the src bitmap |
| 49 | passed to filterImage() is used instead. |
| 50 | */ |
| 51 | |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 52 | SkMatrixConvolutionImageFilter(const SkISize& kernelSize, const SkScalar* kernel, SkScalar gain, SkScalar bias, const SkIPoint& target, TileMode tileMode, bool convolveAlpha, SkImageFilter* input = NULL); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 53 | virtual ~SkMatrixConvolutionImageFilter(); |
| 54 | |
| 55 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMatrixConvolutionImageFilter) |
| 56 | |
| 57 | protected: |
| 58 | SkMatrixConvolutionImageFilter(SkFlattenableReadBuffer& buffer); |
| 59 | virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
| 60 | |
| 61 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&, |
| 62 | SkBitmap* result, SkIPoint* loc) SK_OVERRIDE; |
| 63 | |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 64 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 65 | virtual bool asNewEffect(GrEffectRef**, GrTexture*) const SK_OVERRIDE; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 68 | private: |
| 69 | SkISize fKernelSize; |
| 70 | SkScalar* fKernel; |
| 71 | SkScalar fGain; |
| 72 | SkScalar fBias; |
| 73 | SkIPoint fTarget; |
| 74 | TileMode fTileMode; |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 75 | bool fConvolveAlpha; |
senorblanco@chromium.org | 377c14a | 2013-02-04 22:57:21 +0000 | [diff] [blame^] | 76 | typedef SkImageFilter INHERITED; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 77 | |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 78 | template <class PixelFetcher, bool convolveAlpha> |
| 79 | void filterPixels(const SkBitmap& src, SkBitmap* result, const SkIRect& rect); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 80 | template <class PixelFetcher> |
| 81 | void filterPixels(const SkBitmap& src, SkBitmap* result, const SkIRect& rect); |
| 82 | void filterInteriorPixels(const SkBitmap& src, SkBitmap* result, const SkIRect& rect); |
| 83 | void filterBorderPixels(const SkBitmap& src, SkBitmap* result, const SkIRect& rect); |
| 84 | }; |
| 85 | |
| 86 | #endif |