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 SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&, MapDirection) const override; 85 bool affectsTransparentBlack() const override; 86 87 private: 88 SkISize fKernelSize; 89 SkScalar* fKernel; 90 SkScalar fGain; 91 SkScalar fBias; 92 SkIPoint fKernelOffset; 93 TileMode fTileMode; 94 bool fConvolveAlpha; 95 96 template <class PixelFetcher, bool convolveAlpha> 97 void filterPixels(const SkBitmap& src, 98 SkBitmap* result, 99 const SkIRect& rect, 100 const SkIRect& bounds) const; 101 template <class PixelFetcher> 102 void filterPixels(const SkBitmap& src, 103 SkBitmap* result, 104 const SkIRect& rect, 105 const SkIRect& bounds) const; 106 void filterInteriorPixels(const SkBitmap& src, 107 SkBitmap* result, 108 const SkIRect& rect, 109 const SkIRect& bounds) const; 110 void filterBorderPixels(const SkBitmap& src, 111 SkBitmap* result, 112 const SkIRect& rect, 113 const SkIRect& bounds) const; 114 115 typedef SkImageFilter INHERITED; 116 }; 117 118 #endif 119