blob: 5b02fbaab59bcf1ce4ba4c01524d9df5113ddf06 [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 {
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.org5faa2dc2012-09-18 20:32:34 +000031 virtual ~SkMatrixConvolutionImageFilter();
32
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000033 /** 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.org5faa2dc2012-09-18 20:32:34 +000068 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMatrixConvolutionImageFilter)
69
70protected:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000071 SkMatrixConvolutionImageFilter(SkReadBuffer& buffer);
72 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000073
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000074 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000075 SkBitmap* result, SkIPoint* loc) const SK_OVERRIDE;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000076
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000077#if SK_SUPPORT_GPU
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000078 virtual bool asNewEffect(GrEffectRef** effect,
79 GrTexture*,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000080 const SkMatrix& ctm,
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000081 const SkIRect& bounds) const SK_OVERRIDE;
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000082#endif
83
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000084#ifdef SK_SUPPORT_LEGACY_PUBLICEFFECTCONSTRUCTORS
85public:
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.org5faa2dc2012-09-18 20:32:34 +000097private:
98 SkISize fKernelSize;
99 SkScalar* fKernel;
100 SkScalar fGain;
101 SkScalar fBias;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000102 SkIPoint fKernelOffset;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000103 TileMode fTileMode;
senorblanco@chromium.org8640d502012-09-25 14:32:42 +0000104 bool fConvolveAlpha;
senorblanco@chromium.org377c14a2013-02-04 22:57:21 +0000105 typedef SkImageFilter INHERITED;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000106
senorblanco@chromium.org8640d502012-09-25 14:32:42 +0000107 template <class PixelFetcher, bool convolveAlpha>
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000108 void filterPixels(const SkBitmap& src,
109 SkBitmap* result,
110 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000111 const SkIRect& bounds) const;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000112 template <class PixelFetcher>
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000113 void filterPixels(const SkBitmap& src,
114 SkBitmap* result,
115 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000116 const SkIRect& bounds) const;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000117 void filterInteriorPixels(const SkBitmap& src,
118 SkBitmap* result,
119 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000120 const SkIRect& bounds) const;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000121 void filterBorderPixels(const SkBitmap& src,
122 SkBitmap* result,
123 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000124 const SkIRect& bounds) const;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000125};
126
127#endif