2 * Copyright 2012 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkMatrixConvolutionImageFilter_DEFINED
9 #define SkMatrixConvolutionImageFilter_DEFINED
11 #include "SkImageFilter.h"
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.
22 class SK_API SkMatrixConvolutionImageFilter : public SkImageFilter {
26 kClamp_TileMode = 0, /*!< 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 kMax_TileMode = kClampToBlack_TileMode
32 virtual ~SkMatrixConvolutionImageFilter();
34 /** 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
46 @param tileMode How accesses outside the image are treated. (@see
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.
55 static SkMatrixConvolutionImageFilter* Create(const SkISize& kernelSize,
56 const SkScalar* kernel,
59 const SkIPoint& kernelOffset,
62 SkImageFilter* input = NULL,
63 const CropRect* cropRect = NULL) {
64 return SkNEW_ARGS(SkMatrixConvolutionImageFilter, (kernelSize, kernel, gain, bias,
65 kernelOffset, tileMode, convolveAlpha,
69 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMatrixConvolutionImageFilter)
72 SkMatrixConvolutionImageFilter(const SkISize& kernelSize,
73 const SkScalar* kernel,
76 const SkIPoint& kernelOffset,
80 const CropRect* cropRect);
81 explicit SkMatrixConvolutionImageFilter(SkReadBuffer& buffer);
82 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
84 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
85 SkBitmap* result, SkIPoint* loc) const SK_OVERRIDE;
86 virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*) const SK_OVERRIDE;
90 virtual bool asNewEffect(GrEffect** effect,
93 const SkIRect& bounds) const SK_OVERRIDE;
101 SkIPoint fKernelOffset;
104 typedef SkImageFilter INHERITED;
106 template <class PixelFetcher, bool convolveAlpha>
107 void filterPixels(const SkBitmap& src,
110 const SkIRect& bounds) const;
111 template <class PixelFetcher>
112 void filterPixels(const SkBitmap& src,
115 const SkIRect& bounds) const;
116 void filterInteriorPixels(const SkBitmap& src,
119 const SkIRect& bounds) const;
120 void filterBorderPixels(const SkBitmap& src,
123 const SkIRect& bounds) const;