15faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org/*
25faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org * Copyright 2012 The Android Open Source Project
35faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org *
45faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org * Use of this source code is governed by a BSD-style license that can be
55faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org * found in the LICENSE file.
65faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org */
75faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
85faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org#ifndef SkMatrixConvolutionImageFilter_DEFINED
95faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org#define SkMatrixConvolutionImageFilter_DEFINED
105faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
11377c14a1e648f4427bd11474fad8ac264d98aff2senorblanco@chromium.org#include "SkImageFilter.h"
125faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org#include "SkScalar.h"
135faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org#include "SkSize.h"
145faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org#include "SkPoint.h"
155faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
165faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org/*! \class SkMatrixConvolutionImageFilter
175faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    Matrix convolution image filter.  This filter applies an NxM image
185faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    processing kernel to a given input image.  This can be used to produce
195faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    effects such as sharpening, blurring, edge detection, etc.
205faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org */
215faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
22377c14a1e648f4427bd11474fad8ac264d98aff2senorblanco@chromium.orgclass SK_API SkMatrixConvolutionImageFilter : public SkImageFilter {
235faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgpublic:
245faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    /*! \enum TileMode */
255faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    enum TileMode {
265faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org      kClamp_TileMode,         /*!< Clamp to the image's edge pixels. */
275faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org      kRepeat_TileMode,        /*!< Wrap around to the image's opposite edge. */
285faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org      kClampToBlack_TileMode,  /*!< Fill with transparent black. */
295faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    };
305faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
31cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    virtual ~SkMatrixConvolutionImageFilter();
32cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org
335faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    /** Construct a matrix convolution image filter.
34cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        @param kernelSize     The kernel size in pixels, in each dimension (N by M).
35cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        @param kernel         The image processing kernel.  Must contain N * M
36cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              elements, in row order.
37cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        @param gain           A scale factor applied to each pixel after
38cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              convolution.  This can be used to normalize the
39cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              kernel, if it does not sum to 1.
40cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        @param bias           A bias factor added to each pixel after convolution.
41cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        @param kernelOffset   An offset applied to each pixel coordinate before
42cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              convolution.  This can be used to center the kernel
43cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              over the image (e.g., a 3x3 kernel should have an
44cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              offset of {1, 1}).
45cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        @param tileMode       How accesses outside the image are treated.  (@see
46cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              TileMode).
478640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org        @param convolveAlpha  If true, all channels are convolved.  If false,
48cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              only the RGB channels are convolved, and
49cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              alpha is copied from the source image.
50cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        @param input          The input image filter.  If NULL, the src bitmap
51cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              passed to filterImage() is used instead.
52cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        @param cropRect       The rectangle to which the output processing will be limited.
535faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    */
54cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    static SkMatrixConvolutionImageFilter* Create(const SkISize& kernelSize,
55cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                  const SkScalar* kernel,
56cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                  SkScalar gain,
57cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                  SkScalar bias,
58cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                  const SkIPoint& kernelOffset,
59cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                  TileMode tileMode,
60cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                  bool convolveAlpha,
61cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                  SkImageFilter* input = NULL,
62cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                  const CropRect* cropRect = NULL) {
63cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        return SkNEW_ARGS(SkMatrixConvolutionImageFilter, (kernelSize, kernel, gain, bias,
64cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                           kernelOffset, tileMode, convolveAlpha,
65cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                           input, cropRect));
66cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    }
675faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
685faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMatrixConvolutionImageFilter)
695faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
705faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgprotected:
71bd0be25074e53a6d1abc284562568c9745191984commit-bot@chromium.org    SkMatrixConvolutionImageFilter(const SkISize& kernelSize,
72bd0be25074e53a6d1abc284562568c9745191984commit-bot@chromium.org                                   const SkScalar* kernel,
73bd0be25074e53a6d1abc284562568c9745191984commit-bot@chromium.org                                   SkScalar gain,
74bd0be25074e53a6d1abc284562568c9745191984commit-bot@chromium.org                                   SkScalar bias,
75bd0be25074e53a6d1abc284562568c9745191984commit-bot@chromium.org                                   const SkIPoint& kernelOffset,
76bd0be25074e53a6d1abc284562568c9745191984commit-bot@chromium.org                                   TileMode tileMode,
77bd0be25074e53a6d1abc284562568c9745191984commit-bot@chromium.org                                   bool convolveAlpha,
78bd0be25074e53a6d1abc284562568c9745191984commit-bot@chromium.org                                   SkImageFilter* input,
79bd0be25074e53a6d1abc284562568c9745191984commit-bot@chromium.org                                   const CropRect* cropRect);
80bd0be25074e53a6d1abc284562568c9745191984commit-bot@chromium.org    explicit SkMatrixConvolutionImageFilter(SkReadBuffer& buffer);
818b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
825faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
834cb543d6057b692e1099e9f115155f0bf323a0c8senorblanco@chromium.org    virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
84ae761f7545d8ebf181d220169afac2056b057b8ccommit-bot@chromium.org                               SkBitmap* result, SkIPoint* loc) const SK_OVERRIDE;
850a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org    virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*) const SK_OVERRIDE;
860a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org
875faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
883bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org#if SK_SUPPORT_GPU
897938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    virtual bool asNewEffect(GrEffectRef** effect,
907938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                             GrTexture*,
914cb543d6057b692e1099e9f115155f0bf323a0c8senorblanco@chromium.org                             const SkMatrix& ctm,
927938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                             const SkIRect& bounds) const SK_OVERRIDE;
933bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org#endif
943bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
955faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgprivate:
965faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    SkISize   fKernelSize;
975faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    SkScalar* fKernel;
985faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    SkScalar  fGain;
995faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    SkScalar  fBias;
100cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    SkIPoint  fKernelOffset;
1015faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    TileMode  fTileMode;
1028640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    bool      fConvolveAlpha;
103377c14a1e648f4427bd11474fad8ac264d98aff2senorblanco@chromium.org    typedef SkImageFilter INHERITED;
1045faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
1058640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    template <class PixelFetcher, bool convolveAlpha>
1067938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    void filterPixels(const SkBitmap& src,
1077938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                      SkBitmap* result,
1087938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                      const SkIRect& rect,
109ae761f7545d8ebf181d220169afac2056b057b8ccommit-bot@chromium.org                      const SkIRect& bounds) const;
1105faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    template <class PixelFetcher>
1117938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    void filterPixels(const SkBitmap& src,
1127938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                      SkBitmap* result,
1137938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                      const SkIRect& rect,
114ae761f7545d8ebf181d220169afac2056b057b8ccommit-bot@chromium.org                      const SkIRect& bounds) const;
1157938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    void filterInteriorPixels(const SkBitmap& src,
1167938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                              SkBitmap* result,
1177938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                              const SkIRect& rect,
118ae761f7545d8ebf181d220169afac2056b057b8ccommit-bot@chromium.org                              const SkIRect& bounds) const;
1197938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    void filterBorderPixels(const SkBitmap& src,
1207938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                            SkBitmap* result,
1217938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                            const SkIRect& rect,
122ae761f7545d8ebf181d220169afac2056b057b8ccommit-bot@chromium.org                            const SkIRect& bounds) const;
1235faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org};
1245faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
1255faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org#endif
126