blob: e24d82329442c70818beef9b47703cd475233d80 [file] [log] [blame]
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +00001/*
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.org377c14a2013-02-04 22:57:21 +000011#include "SkImageFilter.h"
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000012#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.org377c14a2013-02-04 22:57:21 +000022class SK_API SkMatrixConvolutionImageFilter : public SkImageFilter {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000023public:
24 /*! \enum TileMode */
25 enum TileMode {
joshualittac977922014-07-22 09:52:11 -070026 kClamp_TileMode = 0, /*!< Clamp to the image's edge pixels. */
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000027 kRepeat_TileMode, /*!< Wrap around to the image's opposite edge. */
28 kClampToBlack_TileMode, /*!< Fill with transparent black. */
joshualittac977922014-07-22 09:52:11 -070029 kMax_TileMode = kClampToBlack_TileMode
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000030 };
31
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000032 virtual ~SkMatrixConvolutionImageFilter();
33
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000034 /** 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 */
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,
senorblanco5e5f9482014-08-26 12:27:12 -070063 const CropRect* cropRect = NULL,
64 uint32_t uniqueID = 0) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000065 return SkNEW_ARGS(SkMatrixConvolutionImageFilter, (kernelSize, kernel, gain, bias,
66 kernelOffset, tileMode, convolveAlpha,
senorblanco5e5f9482014-08-26 12:27:12 -070067 input, cropRect, uniqueID));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000068 }
69
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000070 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMatrixConvolutionImageFilter)
71
72protected:
commit-bot@chromium.orgbd0be252014-05-15 15:40:41 +000073 SkMatrixConvolutionImageFilter(const SkISize& kernelSize,
74 const SkScalar* kernel,
75 SkScalar gain,
76 SkScalar bias,
77 const SkIPoint& kernelOffset,
78 TileMode tileMode,
79 bool convolveAlpha,
80 SkImageFilter* input,
senorblanco5e5f9482014-08-26 12:27:12 -070081 const CropRect* cropRect,
82 uint32_t uniqueID);
reed9fa60da2014-08-21 07:59:51 -070083#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
commit-bot@chromium.orgbd0be252014-05-15 15:40:41 +000084 explicit SkMatrixConvolutionImageFilter(SkReadBuffer& buffer);
reed9fa60da2014-08-21 07:59:51 -070085#endif
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000086 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000087
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000088 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000089 SkBitmap* result, SkIPoint* loc) const SK_OVERRIDE;
senorblanco@chromium.org0a5c2332014-04-29 15:20:39 +000090 virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*) const SK_OVERRIDE;
91
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000092
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000093#if SK_SUPPORT_GPU
bsalomon97b9ab72014-07-08 06:52:35 -070094 virtual bool asNewEffect(GrEffect** effect,
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000095 GrTexture*,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000096 const SkMatrix& ctm,
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000097 const SkIRect& bounds) const SK_OVERRIDE;
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000098#endif
99
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000100private:
101 SkISize fKernelSize;
102 SkScalar* fKernel;
103 SkScalar fGain;
104 SkScalar fBias;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000105 SkIPoint fKernelOffset;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000106 TileMode fTileMode;
senorblanco@chromium.org8640d502012-09-25 14:32:42 +0000107 bool fConvolveAlpha;
senorblanco@chromium.org377c14a2013-02-04 22:57:21 +0000108 typedef SkImageFilter INHERITED;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000109
senorblanco@chromium.org8640d502012-09-25 14:32:42 +0000110 template <class PixelFetcher, bool convolveAlpha>
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000111 void filterPixels(const SkBitmap& src,
112 SkBitmap* result,
113 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000114 const SkIRect& bounds) const;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000115 template <class PixelFetcher>
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000116 void filterPixels(const SkBitmap& src,
117 SkBitmap* result,
118 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000119 const SkIRect& bounds) const;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000120 void filterInteriorPixels(const SkBitmap& src,
121 SkBitmap* result,
122 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000123 const SkIRect& bounds) const;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000124 void filterBorderPixels(const SkBitmap& src,
125 SkBitmap* result,
126 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000127 const SkIRect& bounds) const;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000128};
129
130#endif