1/*
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
11#include "SkImageFilter.h"
12#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
22class SK_API SkMatrixConvolutionImageFilter : public SkImageFilter {
23public:
24    /*! \enum TileMode */
25    enum TileMode {
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
30    };
31
32    virtual ~SkMatrixConvolutionImageFilter();
33
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
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    */
55    static SkMatrixConvolutionImageFilter* Create(const SkISize& kernelSize,
56                                                  const SkScalar* kernel,
57                                                  SkScalar gain,
58                                                  SkScalar bias,
59                                                  const SkIPoint& kernelOffset,
60                                                  TileMode tileMode,
61                                                  bool convolveAlpha,
62                                                  SkImageFilter* input = NULL,
63                                                  const CropRect* cropRect = NULL,
64                                                  uint32_t uniqueID = 0);
65
66    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMatrixConvolutionImageFilter)
67
68protected:
69    SkMatrixConvolutionImageFilter(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,
77                                   const CropRect* cropRect,
78                                   uint32_t uniqueID);
79#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
80    explicit SkMatrixConvolutionImageFilter(SkReadBuffer& buffer);
81#endif
82    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
83
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;
87
88
89#if SK_SUPPORT_GPU
90    virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const SkMatrix&,
91                                     const SkIRect& bounds) const SK_OVERRIDE;
92#endif
93
94private:
95    SkISize   fKernelSize;
96    SkScalar* fKernel;
97    SkScalar  fGain;
98    SkScalar  fBias;
99    SkIPoint  fKernelOffset;
100    TileMode  fTileMode;
101    bool      fConvolveAlpha;
102    typedef SkImageFilter INHERITED;
103
104    template <class PixelFetcher, bool convolveAlpha>
105    void filterPixels(const SkBitmap& src,
106                      SkBitmap* result,
107                      const SkIRect& rect,
108                      const SkIRect& bounds) const;
109    template <class PixelFetcher>
110    void filterPixels(const SkBitmap& src,
111                      SkBitmap* result,
112                      const SkIRect& rect,
113                      const SkIRect& bounds) const;
114    void filterInteriorPixels(const SkBitmap& src,
115                              SkBitmap* result,
116                              const SkIRect& rect,
117                              const SkIRect& bounds) const;
118    void filterBorderPixels(const SkBitmap& src,
119                            SkBitmap* result,
120                            const SkIRect& rect,
121                            const SkIRect& bounds) const;
122};
123
124#endif
125