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 SkMorphologyImageFilter_DEFINED
9 #define SkMorphologyImageFilter_DEFINED
10 
11 #include "SkColor.h"
12 #include "SkImageFilter.h"
13 #include "SkSize.h"
14 
15 ///////////////////////////////////////////////////////////////////////////////
16 class SK_API SkMorphologyImageFilter : public SkImageFilter {
17 public:
18     SkRect computeFastBounds(const SkRect& src) const override;
19     SkIRect onFilterNodeBounds(const SkIRect& src, const SkMatrix&, MapDirection) const override;
20 
21     /**
22      * All morphology procs have the same signature: src is the source buffer, dst the
23      * destination buffer, radius is the morphology radius, width and height are the bounds
24      * of the destination buffer (in pixels), and srcStride and dstStride are the
25      * number of pixels per row in each buffer. All buffers are 8888.
26      */
27 
28     typedef void (*Proc)(const SkPMColor* src, SkPMColor* dst, int radius,
29                          int width, int height, int srcStride, int dstStride);
30 
31 protected:
32     enum Op {
33         kErode_Op,
34         kDilate_Op,
35     };
36 
37     virtual Op op() const = 0;
38 
39     SkMorphologyImageFilter(int radiusX, int radiusY,
40                             sk_sp<SkImageFilter> input,
41                             const CropRect* cropRect);
42     sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source,
43                                         const Context&,
44                                         SkIPoint* offset) const override;
45     void flatten(SkWriteBuffer&) const override;
46 
radius()47     SkISize radius() const { return fRadius; }
48 
49 private:
50     SkISize  fRadius;
51 
52     typedef SkImageFilter INHERITED;
53 };
54 
55 ///////////////////////////////////////////////////////////////////////////////
56 class SK_API SkDilateImageFilter : public SkMorphologyImageFilter {
57 public:
58     static sk_sp<SkImageFilter> Make(int radiusX, int radiusY,
59                                      sk_sp<SkImageFilter> input,
60                                      const CropRect* cropRect = nullptr);
61 
62     SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDilateImageFilter)63     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDilateImageFilter)
64 
65 protected:
66     Op op() const override { return kDilate_Op; }
67 
68 private:
SkDilateImageFilter(int radiusX,int radiusY,sk_sp<SkImageFilter> input,const CropRect * cropRect)69     SkDilateImageFilter(int radiusX, int radiusY,
70                         sk_sp<SkImageFilter> input,
71                         const CropRect* cropRect)
72         : INHERITED(radiusX, radiusY, input, cropRect) {}
73 
74     typedef SkMorphologyImageFilter INHERITED;
75 };
76 
77 ///////////////////////////////////////////////////////////////////////////////
78 class SK_API SkErodeImageFilter : public SkMorphologyImageFilter {
79 public:
80     static sk_sp<SkImageFilter> Make(int radiusX, int radiusY,
81                                      sk_sp<SkImageFilter> input,
82                                      const CropRect* cropRect = nullptr);
83 
84     SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkErodeImageFilter)85     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkErodeImageFilter)
86 
87 protected:
88     Op op() const override { return kErode_Op; }
89 
90 private:
SkErodeImageFilter(int radiusX,int radiusY,sk_sp<SkImageFilter> input,const CropRect * cropRect)91     SkErodeImageFilter(int radiusX, int radiusY,
92                        sk_sp<SkImageFilter> input, const CropRect* cropRect)
93         : INHERITED(radiusX, radiusY, input, cropRect) {}
94 
95     typedef SkMorphologyImageFilter INHERITED;
96 };
97 
98 #endif
99