Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / filters / FEMorphology.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4  * Copyright (C) 2005 Eric Seidel <eric@webkit.org>
5  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
6  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
7  * Copyright (C) 2013 Google Inc. All rights reserved.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB.  If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24
25 #include "config.h"
26 #include "platform/graphics/filters/FEMorphology.h"
27
28 #include "SkMorphologyImageFilter.h"
29 #include "platform/graphics/filters/Filter.h"
30 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
31 #include "platform/text/TextStream.h"
32
33 namespace blink {
34
35 FEMorphology::FEMorphology(Filter* filter, MorphologyOperatorType type, float radiusX, float radiusY)
36     : FilterEffect(filter)
37     , m_type(type)
38     , m_radiusX(radiusX)
39     , m_radiusY(radiusY)
40 {
41 }
42
43 PassRefPtr<FEMorphology> FEMorphology::create(Filter* filter, MorphologyOperatorType type, float radiusX, float radiusY)
44 {
45     return adoptRef(new FEMorphology(filter, type, radiusX, radiusY));
46 }
47
48 MorphologyOperatorType FEMorphology::morphologyOperator() const
49 {
50     return m_type;
51 }
52
53 bool FEMorphology::setMorphologyOperator(MorphologyOperatorType type)
54 {
55     if (m_type == type)
56         return false;
57     m_type = type;
58     return true;
59 }
60
61 float FEMorphology::radiusX() const
62 {
63     return m_radiusX;
64 }
65
66 bool FEMorphology::setRadiusX(float radiusX)
67 {
68     if (m_radiusX == radiusX)
69         return false;
70     m_radiusX = radiusX;
71     return true;
72 }
73
74 float FEMorphology::radiusY() const
75 {
76     return m_radiusY;
77 }
78
79 FloatRect FEMorphology::mapRect(const FloatRect& rect, bool)
80 {
81     FloatRect result = rect;
82     result.inflateX(filter()->applyHorizontalScale(m_radiusX));
83     result.inflateY(filter()->applyVerticalScale(m_radiusY));
84     return result;
85 }
86
87 bool FEMorphology::setRadiusY(float radiusY)
88 {
89     if (m_radiusY == radiusY)
90         return false;
91     m_radiusY = radiusY;
92     return true;
93 }
94
95 PassRefPtr<SkImageFilter> FEMorphology::createImageFilter(SkiaImageFilterBuilder* builder)
96 {
97     RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
98     SkScalar radiusX = SkFloatToScalar(filter()->applyHorizontalScale(m_radiusX));
99     SkScalar radiusY = SkFloatToScalar(filter()->applyVerticalScale(m_radiusY));
100     SkImageFilter::CropRect rect = getCropRect(builder->cropOffset());
101     if (m_type == FEMORPHOLOGY_OPERATOR_DILATE)
102         return adoptRef(SkDilateImageFilter::Create(radiusX, radiusY, input.get(), &rect));
103     return adoptRef(SkErodeImageFilter::Create(radiusX, radiusY, input.get(), &rect));
104 }
105
106 static TextStream& operator<<(TextStream& ts, const MorphologyOperatorType& type)
107 {
108     switch (type) {
109     case FEMORPHOLOGY_OPERATOR_UNKNOWN:
110         ts << "UNKNOWN";
111         break;
112     case FEMORPHOLOGY_OPERATOR_ERODE:
113         ts << "ERODE";
114         break;
115     case FEMORPHOLOGY_OPERATOR_DILATE:
116         ts << "DILATE";
117         break;
118     }
119     return ts;
120 }
121
122 TextStream& FEMorphology::externalRepresentation(TextStream& ts, int indent) const
123 {
124     writeIndent(ts, indent);
125     ts << "[feMorphology";
126     FilterEffect::externalRepresentation(ts);
127     ts << " operator=\"" << morphologyOperator() << "\" "
128         << "radius=\"" << radiusX() << ", " << radiusY() << "\"]\n";
129     inputEffect(0)->externalRepresentation(ts, indent + 1);
130     return ts;
131 }
132
133 } // namespace blink