blob: 6c559675d621bb1bf6844cc564aa16754d47f942 [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
robertphillipsdada4dd2016-04-13 04:54:36 -070032 ~SkMatrixConvolutionImageFilter() override;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000033
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 */
robertphillipsef6a47b2016-04-08 08:01:20 -070055 static sk_sp<SkImageFilter> Make(const SkISize& kernelSize,
56 const SkScalar* kernel,
57 SkScalar gain,
58 SkScalar bias,
59 const SkIPoint& kernelOffset,
60 TileMode tileMode,
61 bool convolveAlpha,
62 sk_sp<SkImageFilter> input,
63 const CropRect* cropRect = nullptr);
64
65 SK_TO_STRING_OVERRIDE()
66 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMatrixConvolutionImageFilter)
67
68#ifdef SK_SUPPORT_LEGACY_IMAGEFILTER_PTR
reed5e1ddb12015-12-21 08:52:45 -080069 static SkImageFilter* Create(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 = NULL,
robertphillipsef6a47b2016-04-08 08:01:20 -070077 const CropRect* cropRect = NULL) {
78 return Make(kernelSize, kernel, gain, bias, kernelOffset, tileMode, convolveAlpha,
79 sk_ref_sp<SkImageFilter>(input), cropRect).release();
80 }
81#endif
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000082
83protected:
commit-bot@chromium.orgbd0be252014-05-15 15:40:41 +000084 SkMatrixConvolutionImageFilter(const SkISize& kernelSize,
85 const SkScalar* kernel,
86 SkScalar gain,
87 SkScalar bias,
88 const SkIPoint& kernelOffset,
89 TileMode tileMode,
90 bool convolveAlpha,
robertphillipsef6a47b2016-04-08 08:01:20 -070091 sk_sp<SkImageFilter> input,
senorblanco24e06d52015-03-18 12:11:33 -070092 const CropRect* cropRect);
mtklein36352bf2015-03-25 18:17:31 -070093 void flatten(SkWriteBuffer&) const override;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000094
robertphillipsdada4dd2016-04-13 04:54:36 -070095 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
96 SkIPoint* offset) const override;
senorblancoe5e79842016-03-21 14:51:59 -070097 SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&, MapDirection) const override;
senorblanco6db0a7b2016-04-01 16:41:10 -070098 bool affectsTransparentBlack() const override;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000099
100private:
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.org5faa2dc2012-09-18 20:32:34 +0000108
senorblanco@chromium.org8640d502012-09-25 14:32:42 +0000109 template <class PixelFetcher, bool convolveAlpha>
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000110 void filterPixels(const SkBitmap& src,
111 SkBitmap* result,
112 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000113 const SkIRect& bounds) const;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000114 template <class PixelFetcher>
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000115 void filterPixels(const SkBitmap& src,
116 SkBitmap* result,
117 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000118 const SkIRect& bounds) const;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000119 void filterInteriorPixels(const SkBitmap& src,
120 SkBitmap* result,
121 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000122 const SkIRect& bounds) const;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000123 void filterBorderPixels(const SkBitmap& src,
124 SkBitmap* result,
125 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000126 const SkIRect& bounds) const;
robertphillipsef6a47b2016-04-08 08:01:20 -0700127
128 typedef SkImageFilter INHERITED;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000129};
130
131#endif