SkMatrixConvolutionImageFilter.h revision 0a5c233e3b911232c0d6f9a88ded99ecf88b8a97
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, /*!< 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 70protected: 71 SkMatrixConvolutionImageFilter(SkReadBuffer& buffer); 72 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; 73 74 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, 75 SkBitmap* result, SkIPoint* loc) const SK_OVERRIDE; 76 virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*) const SK_OVERRIDE; 77 78 79#if SK_SUPPORT_GPU 80 virtual bool asNewEffect(GrEffectRef** effect, 81 GrTexture*, 82 const SkMatrix& ctm, 83 const SkIRect& bounds) const SK_OVERRIDE; 84#endif 85 86#ifdef SK_SUPPORT_LEGACY_PUBLICEFFECTCONSTRUCTORS 87public: 88#endif 89 SkMatrixConvolutionImageFilter(const SkISize& kernelSize, 90 const SkScalar* kernel, 91 SkScalar gain, 92 SkScalar bias, 93 const SkIPoint& kernelOffset, 94 TileMode tileMode, 95 bool convolveAlpha, 96 SkImageFilter* input = NULL, 97 const CropRect* cropRect = NULL); 98 99private: 100 SkISize fKernelSize; 101 SkScalar* fKernel; 102 SkScalar fGain; 103 SkScalar fBias; 104 SkIPoint fKernelOffset; 105 TileMode fTileMode; 106 bool fConvolveAlpha; 107 typedef SkImageFilter INHERITED; 108 109 template <class PixelFetcher, bool convolveAlpha> 110 void filterPixels(const SkBitmap& src, 111 SkBitmap* result, 112 const SkIRect& rect, 113 const SkIRect& bounds) const; 114 template <class PixelFetcher> 115 void filterPixels(const SkBitmap& src, 116 SkBitmap* result, 117 const SkIRect& rect, 118 const SkIRect& bounds) const; 119 void filterInteriorPixels(const SkBitmap& src, 120 SkBitmap* result, 121 const SkIRect& rect, 122 const SkIRect& bounds) const; 123 void filterBorderPixels(const SkBitmap& src, 124 SkBitmap* result, 125 const SkIRect& rect, 126 const SkIRect& bounds) const; 127}; 128 129#endif 130