SkMatrixConvolutionImageFilter.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifndef SkMatrixConvolutionImageFilter_DEFINED
  8. #define SkMatrixConvolutionImageFilter_DEFINED
  9. #include "SkFlattenable.h"
  10. #include "SkImageFilter.h"
  11. #include "SkScalar.h"
  12. #include "SkSize.h"
  13. #include "SkPoint.h"
  14. class SkBitmap;
  15. /*! \class SkMatrixConvolutionImageFilter
  16. Matrix convolution image filter. This filter applies an NxM image
  17. processing kernel to a given input image. This can be used to produce
  18. effects such as sharpening, blurring, edge detection, etc.
  19. */
  20. class SK_API SkMatrixConvolutionImageFilter : public SkImageFilter {
  21. public:
  22. /*! \enum TileMode */
  23. enum TileMode {
  24. kClamp_TileMode = 0, /*!< Clamp to the image's edge pixels. */
  25. kRepeat_TileMode, /*!< Wrap around to the image's opposite edge. */
  26. kClampToBlack_TileMode, /*!< Fill with transparent black. */
  27. kLast_TileMode = kClampToBlack_TileMode,
  28. // TODO: remove kMax - it is non-standard but used by Chromium!
  29. kMax_TileMode = kClampToBlack_TileMode
  30. };
  31. ~SkMatrixConvolutionImageFilter() override;
  32. /** Construct a matrix convolution image filter.
  33. @param kernelSize The kernel size in pixels, in each dimension (N by M).
  34. @param kernel The image processing kernel. Must contain N * M
  35. elements, in row order.
  36. @param gain A scale factor applied to each pixel after
  37. convolution. This can be used to normalize the
  38. kernel, if it does not sum to 1.
  39. @param bias A bias factor added to each pixel after convolution.
  40. @param kernelOffset An offset applied to each pixel coordinate before
  41. convolution. This can be used to center the kernel
  42. over the image (e.g., a 3x3 kernel should have an
  43. offset of {1, 1}).
  44. @param tileMode How accesses outside the image are treated. (@see
  45. TileMode).
  46. @param convolveAlpha If true, all channels are convolved. If false,
  47. only the RGB channels are convolved, and
  48. alpha is copied from the source image.
  49. @param input The input image filter. If NULL, the src bitmap
  50. passed to filterImage() is used instead.
  51. @param cropRect The rectangle to which the output processing will be limited.
  52. */
  53. static sk_sp<SkImageFilter> Make(const SkISize& kernelSize,
  54. const SkScalar* kernel,
  55. SkScalar gain,
  56. SkScalar bias,
  57. const SkIPoint& kernelOffset,
  58. TileMode tileMode,
  59. bool convolveAlpha,
  60. sk_sp<SkImageFilter> input,
  61. const CropRect* cropRect = nullptr);
  62. protected:
  63. SkMatrixConvolutionImageFilter(const SkISize& kernelSize,
  64. const SkScalar* kernel,
  65. SkScalar gain,
  66. SkScalar bias,
  67. const SkIPoint& kernelOffset,
  68. TileMode tileMode,
  69. bool convolveAlpha,
  70. sk_sp<SkImageFilter> input,
  71. const CropRect* cropRect);
  72. void flatten(SkWriteBuffer&) const override;
  73. sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
  74. SkIPoint* offset) const override;
  75. sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override;
  76. SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix& ctm,
  77. MapDirection, const SkIRect* inputRect) const override;
  78. bool affectsTransparentBlack() const override;
  79. private:
  80. SK_FLATTENABLE_HOOKS(SkMatrixConvolutionImageFilter)
  81. SkISize fKernelSize;
  82. SkScalar* fKernel;
  83. SkScalar fGain;
  84. SkScalar fBias;
  85. SkIPoint fKernelOffset;
  86. TileMode fTileMode;
  87. bool fConvolveAlpha;
  88. template <class PixelFetcher, bool convolveAlpha>
  89. void filterPixels(const SkBitmap& src,
  90. SkBitmap* result,
  91. SkIVector& offset,
  92. const SkIRect& rect,
  93. const SkIRect& bounds) const;
  94. template <class PixelFetcher>
  95. void filterPixels(const SkBitmap& src,
  96. SkBitmap* result,
  97. SkIVector& offset,
  98. const SkIRect& rect,
  99. const SkIRect& bounds) const;
  100. void filterInteriorPixels(const SkBitmap& src,
  101. SkBitmap* result,
  102. SkIVector& offset,
  103. const SkIRect& rect,
  104. const SkIRect& bounds) const;
  105. void filterBorderPixels(const SkBitmap& src,
  106. SkBitmap* result,
  107. SkIVector& offset,
  108. const SkIRect& rect,
  109. const SkIRect& bounds) const;
  110. typedef SkImageFilter INHERITED;
  111. };
  112. #endif