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 22 class SK_API SkMatrixConvolutionImageFilter : public SkImageFilter { 23 public: 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 virtual ~SkMatrixConvolutionImageFilter(); 32 33 /** Construct a matrix convolution image filter. 34 @param kernelSize The kernel size in pixels, in each dimension (N by M). 35 @param kernel The image processing kernel. Must contain N * M 36 elements, in row order. 37 @param gain A scale factor applied to each pixel after 38 convolution. This can be used to normalize the 39 kernel, if it does not sum to 1. 40 @param bias A bias factor added to each pixel after convolution. 41 @param kernelOffset An offset applied to each pixel coordinate before 42 convolution. This can be used to center the kernel 43 over the image (e.g., a 3x3 kernel should have an 44 offset of {1, 1}). 45 @param tileMode How accesses outside the image are treated. (@see 46 TileMode). 47 @param convolveAlpha If true, all channels are convolved. If false, 48 only the RGB channels are convolved, and 49 alpha is copied from the source image. 50 @param input The input image filter. If NULL, the src bitmap 51 passed to filterImage() is used instead. 52 @param cropRect The rectangle to which the output processing will be limited. 53 */ 54 static SkMatrixConvolutionImageFilter* Create(const SkISize& kernelSize, 55 const SkScalar* kernel, 56 SkScalar gain, 57 SkScalar bias, 58 const SkIPoint& kernelOffset, 59 TileMode tileMode, 60 bool convolveAlpha, 61 SkImageFilter* input = NULL, 62 const CropRect* cropRect = NULL) { 63 return SkNEW_ARGS(SkMatrixConvolutionImageFilter, (kernelSize, kernel, gain, bias, 64 kernelOffset, tileMode, convolveAlpha, 65 input, cropRect)); 66 } 67 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 SkImageFilter* input, 79 const CropRect* cropRect); 80 explicit SkMatrixConvolutionImageFilter(SkReadBuffer& buffer); 81 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; 82 83 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, 84 SkBitmap* result, SkIPoint* loc) const SK_OVERRIDE; 85 virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*) const SK_OVERRIDE; 86 87 88 #if SK_SUPPORT_GPU 89 virtual bool asNewEffect(GrEffectRef** effect, 90 GrTexture*, 91 const SkMatrix& ctm, 92 const SkIRect& bounds) const SK_OVERRIDE; 93 #endif 94 95 private: 96 SkISize fKernelSize; 97 SkScalar* fKernel; 98 SkScalar fGain; 99 SkScalar fBias; 100 SkIPoint fKernelOffset; 101 TileMode fTileMode; 102 bool fConvolveAlpha; 103 typedef SkImageFilter INHERITED; 104 105 template <class PixelFetcher, bool convolveAlpha> 106 void filterPixels(const SkBitmap& src, 107 SkBitmap* result, 108 const SkIRect& rect, 109 const SkIRect& bounds) const; 110 template <class PixelFetcher> 111 void filterPixels(const SkBitmap& src, 112 SkBitmap* result, 113 const SkIRect& rect, 114 const SkIRect& bounds) const; 115 void filterInteriorPixels(const SkBitmap& src, 116 SkBitmap* result, 117 const SkIRect& rect, 118 const SkIRect& bounds) const; 119 void filterBorderPixels(const SkBitmap& src, 120 SkBitmap* result, 121 const SkIRect& rect, 122 const SkIRect& bounds) const; 123 }; 124 125 #endif 126