blob: 59af83e025275ed29b934fa624151481602f784a [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
31 /** Construct a matrix convolution image filter.
32 @param kernelSize The kernel size in pixels, in each dimension (N by M).
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000033 @param kernel The image processing kernel. Must contain N * M
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000034 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.org8640d502012-09-25 14:32:42 +000045 @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.org5faa2dc2012-09-18 20:32:34 +000048 @param input The input image filter. If NULL, the src bitmap
49 passed to filterImage() is used instead.
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000050 @param cropRect The rectangle to which the output processing will be limited.
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000051 */
52
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000053 SkMatrixConvolutionImageFilter(const SkISize& kernelSize,
54 const SkScalar* kernel,
55 SkScalar gain,
56 SkScalar bias,
57 const SkIPoint& target,
58 TileMode tileMode,
59 bool convolveAlpha,
60 SkImageFilter* input = NULL,
61 const CropRect* cropRect = NULL);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000062 virtual ~SkMatrixConvolutionImageFilter();
63
64 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMatrixConvolutionImageFilter)
65
66protected:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000067 SkMatrixConvolutionImageFilter(SkReadBuffer& buffer);
68 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000069
70 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000071 SkBitmap* result, SkIPoint* loc) const SK_OVERRIDE;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000072
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000073#if SK_SUPPORT_GPU
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000074 virtual bool asNewEffect(GrEffectRef** effect,
75 GrTexture*,
76 const SkMatrix& matrix,
77 const SkIRect& bounds) const SK_OVERRIDE;
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000078#endif
79
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000080private:
81 SkISize fKernelSize;
82 SkScalar* fKernel;
83 SkScalar fGain;
84 SkScalar fBias;
85 SkIPoint fTarget;
86 TileMode fTileMode;
senorblanco@chromium.org8640d502012-09-25 14:32:42 +000087 bool fConvolveAlpha;
senorblanco@chromium.org377c14a2013-02-04 22:57:21 +000088 typedef SkImageFilter INHERITED;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000089
senorblanco@chromium.org8640d502012-09-25 14:32:42 +000090 template <class PixelFetcher, bool convolveAlpha>
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000091 void filterPixels(const SkBitmap& src,
92 SkBitmap* result,
93 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000094 const SkIRect& bounds) const;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000095 template <class PixelFetcher>
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000096 void filterPixels(const SkBitmap& src,
97 SkBitmap* result,
98 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000099 const SkIRect& bounds) const;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000100 void filterInteriorPixels(const SkBitmap& src,
101 SkBitmap* result,
102 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000103 const SkIRect& bounds) const;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000104 void filterBorderPixels(const SkBitmap& src,
105 SkBitmap* result,
106 const SkIRect& rect,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000107 const SkIRect& bounds) const;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000108};
109
110#endif