Home | History | Annotate | Download | only in effects
      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 SkBitmap;
     17 
     18 /*! \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 
     24 class SK_API SkMatrixConvolutionImageFilter : public SkImageFilter {
     25 public:
     26     /*! \enum TileMode */
     27     enum TileMode {
     28       kClamp_TileMode = 0,         /*!< Clamp to the image's edge pixels. */
     29       kRepeat_TileMode,        /*!< Wrap around to the image's opposite edge. */
     30       kClampToBlack_TileMode,  /*!< Fill with transparent black. */
     31       kMax_TileMode = kClampToBlack_TileMode
     32     };
     33 
     34     ~SkMatrixConvolutionImageFilter() override;
     35 
     36     /** 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     */
     57     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 
     70 protected:
     71     SkMatrixConvolutionImageFilter(const SkISize& kernelSize,
     72                                    const SkScalar* kernel,
     73                                    SkScalar gain,
     74                                    SkScalar bias,
     75                                    const SkIPoint& kernelOffset,
     76                                    TileMode tileMode,
     77                                    bool convolveAlpha,
     78                                    sk_sp<SkImageFilter> input,
     79                                    const CropRect* cropRect);
     80     void flatten(SkWriteBuffer&) const override;
     81 
     82     sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
     83                                         SkIPoint* offset) const override;
     84     sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override;
     85     SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&, MapDirection) const override;
     86     bool affectsTransparentBlack() const override;
     87 
     88 private:
     89     SkISize   fKernelSize;
     90     SkScalar* fKernel;
     91     SkScalar  fGain;
     92     SkScalar  fBias;
     93     SkIPoint  fKernelOffset;
     94     TileMode  fTileMode;
     95     bool      fConvolveAlpha;
     96 
     97     template <class PixelFetcher, bool convolveAlpha>
     98     void filterPixels(const SkBitmap& src,
     99                       SkBitmap* result,
    100                       const SkIRect& rect,
    101                       const SkIRect& bounds) const;
    102     template <class PixelFetcher>
    103     void filterPixels(const SkBitmap& src,
    104                       SkBitmap* result,
    105                       const SkIRect& rect,
    106                       const SkIRect& bounds) const;
    107     void filterInteriorPixels(const SkBitmap& src,
    108                               SkBitmap* result,
    109                               const SkIRect& rect,
    110                               const SkIRect& bounds) const;
    111     void filterBorderPixels(const SkBitmap& src,
    112                             SkBitmap* result,
    113                             const SkIRect& rect,
    114                             const SkIRect& bounds) const;
    115 
    116     typedef SkImageFilter INHERITED;
    117 };
    118 
    119 #endif
    120