blob: 5615469e07d28d94c6a546aedae7f95392e78396 [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
senorblanco32eaa892016-04-21 06:49:15 -070016class SkBitmap;
17
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000018/*! \class SkMatrixConvolutionImageFilter
19 Matrix convolution image filter. This filter applies an NxM image
20 processing kernel to a given input image. This can be used to produce
21 effects such as sharpening, blurring, edge detection, etc.
22 */
23
senorblanco@chromium.org377c14a2013-02-04 22:57:21 +000024class SK_API SkMatrixConvolutionImageFilter : public SkImageFilter {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000025public:
26 /*! \enum TileMode */
27 enum TileMode {
joshualittac977922014-07-22 09:52:11 -070028 kClamp_TileMode = 0, /*!< Clamp to the image's edge pixels. */
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000029 kRepeat_TileMode, /*!< Wrap around to the image's opposite edge. */
30 kClampToBlack_TileMode, /*!< Fill with transparent black. */
joshualittac977922014-07-22 09:52:11 -070031 kMax_TileMode = kClampToBlack_TileMode
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000032 };
33
robertphillipsdada4dd2016-04-13 04:54:36 -070034 ~SkMatrixConvolutionImageFilter() override;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000035
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000036 /** Construct a matrix convolution image filter.
37 @param kernelSize The kernel size in pixels, in each dimension (N by M).
38 @param kernel The image processing kernel. Must contain N * M
39 elements, in row order.
40 @param gain A scale factor applied to each pixel after
41 convolution. This can be used to normalize the
42 kernel, if it does not sum to 1.
43 @param bias A bias factor added to each pixel after convolution.
44 @param kernelOffset An offset applied to each pixel coordinate before
45 convolution. This can be used to center the kernel
46 over the image (e.g., a 3x3 kernel should have an
47 offset of {1, 1}).
48 @param tileMode How accesses outside the image are treated. (@see
49 TileMode).
50 @param convolveAlpha If true, all channels are convolved. If false,
51 only the RGB channels are convolved, and
52 alpha is copied from the source image.
53 @param input The input image filter. If NULL, the src bitmap
54 passed to filterImage() is used instead.
55 @param cropRect The rectangle to which the output processing will be limited.
56 */
robertphillipsef6a47b2016-04-08 08:01:20 -070057 static sk_sp<SkImageFilter> Make(const SkISize& kernelSize,
58 const SkScalar* kernel,
59 SkScalar gain,
60 SkScalar bias,
61 const SkIPoint& kernelOffset,
62 TileMode tileMode,
63 bool convolveAlpha,
64 sk_sp<SkImageFilter> input,
65 const CropRect* cropRect = nullptr);
66
67 SK_TO_STRING_OVERRIDE()
68 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMatrixConvolutionImageFilter)
69
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000070protected:
commit-bot@chromium.orgbd0be252014-05-15 15:40:41 +000071 SkMatrixConvolutionImageFilter(const SkISize& kernelSize,
72 const SkScalar* kernel,
73 SkScalar gain,
74 SkScalar bias,
75 const SkIPoint& kernelOffset,
76 TileMode tileMode,
77 bool convolveAlpha,
robertphillipsef6a47b2016-04-08 08:01:20 -070078 sk_sp<SkImageFilter> input,
senorblanco24e06d52015-03-18 12:11:33 -070079 const CropRect* cropRect);
mtklein36352bf2015-03-25 18:17:31 -070080 void flatten(SkWriteBuffer&) const override;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000081
robertphillipsdada4dd2016-04-13 04:54:36 -070082 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
83 SkIPoint* offset) const override;
Matt Sarett6d72ed92017-04-10 16:35:33 -040084 sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override;
senorblancoe5e79842016-03-21 14:51:59 -070085 SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&, MapDirection) const override;
senorblanco6db0a7b2016-04-01 16:41:10 -070086 bool affectsTransparentBlack() const override;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000087
88private:
89 SkISize fKernelSize;
90 SkScalar* fKernel;
91 SkScalar fGain;
92 SkScalar fBias;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000093 SkIPoint fKernelOffset;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000094 TileMode fTileMode;
senorblanco@chromium.org8640d502012-09-25 14:32:42 +000095 bool fConvolveAlpha;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000096
senorblanco@chromium.org8640d502012-09-25 14:32:42 +000097 template <class PixelFetcher, bool convolveAlpha>
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000098 void filterPixels(const SkBitmap& src,
99 SkBitmap* result,
100 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000101 const SkIRect& bounds) const;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000102 template <class PixelFetcher>
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000103 void filterPixels(const SkBitmap& src,
104 SkBitmap* result,
105 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000106 const SkIRect& bounds) const;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000107 void filterInteriorPixels(const SkBitmap& src,
108 SkBitmap* result,
109 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000110 const SkIRect& bounds) const;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000111 void filterBorderPixels(const SkBitmap& src,
112 SkBitmap* result,
113 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000114 const SkIRect& bounds) const;
robertphillipsef6a47b2016-04-08 08:01:20 -0700115
116 typedef SkImageFilter INHERITED;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000117};
118
119#endif