Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEConvolveMatrixElement.cpp
1 /*
2  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #include "core/svg/SVGFEConvolveMatrixElement.h"
23
24 #include "SVGNames.h"
25 #include "platform/graphics/filters/FilterEffect.h"
26 #include "core/svg/SVGElementInstance.h"
27 #include "core/svg/SVGParserUtilities.h"
28 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
29 #include "platform/geometry/FloatPoint.h"
30 #include "platform/geometry/IntPoint.h"
31 #include "platform/geometry/IntSize.h"
32
33 namespace WebCore {
34
35 template<> const SVGEnumerationStringEntries& getStaticStringEntries<EdgeModeType>()
36 {
37     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
38     if (entries.isEmpty()) {
39         entries.append(std::make_pair(EDGEMODE_DUPLICATE, "duplicate"));
40         entries.append(std::make_pair(EDGEMODE_WRAP, "wrap"));
41         entries.append(std::make_pair(EDGEMODE_NONE, "none"));
42     }
43     return entries;
44 }
45
46 inline SVGFEConvolveMatrixElement::SVGFEConvolveMatrixElement(Document& document)
47     : SVGFilterPrimitiveStandardAttributes(SVGNames::feConvolveMatrixTag, document)
48     , m_bias(SVGAnimatedNumber::create(this, SVGNames::biasAttr, SVGNumber::create()))
49     , m_divisor(SVGAnimatedNumber::create(this, SVGNames::divisorAttr, SVGNumber::create()))
50     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
51     , m_edgeMode(SVGAnimatedEnumeration<EdgeModeType>::create(this, SVGNames::edgeModeAttr, EDGEMODE_DUPLICATE))
52     , m_kernelMatrix(SVGAnimatedNumberList::create(this, SVGNames::kernelMatrixAttr, SVGNumberList::create()))
53     , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::kernelUnitLengthAttr))
54     , m_order(SVGAnimatedIntegerOptionalInteger::create(this, SVGNames::orderAttr))
55     , m_preserveAlpha(SVGAnimatedBoolean::create(this, SVGNames::preserveAlphaAttr, SVGBoolean::create()))
56     , m_targetX(SVGAnimatedInteger::create(this, SVGNames::targetXAttr, SVGInteger::create()))
57     , m_targetY(SVGAnimatedInteger::create(this, SVGNames::targetYAttr, SVGInteger::create()))
58 {
59     ScriptWrappable::init(this);
60
61     addToPropertyMap(m_preserveAlpha);
62     addToPropertyMap(m_divisor);
63     addToPropertyMap(m_bias);
64     addToPropertyMap(m_kernelUnitLength);
65     addToPropertyMap(m_kernelMatrix);
66     addToPropertyMap(m_in1);
67     addToPropertyMap(m_edgeMode);
68     addToPropertyMap(m_order);
69     addToPropertyMap(m_targetX);
70     addToPropertyMap(m_targetY);
71 }
72
73 PassRefPtr<SVGFEConvolveMatrixElement> SVGFEConvolveMatrixElement::create(Document& document)
74 {
75     return adoptRef(new SVGFEConvolveMatrixElement(document));
76 }
77
78 bool SVGFEConvolveMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
79 {
80     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
81     if (supportedAttributes.isEmpty()) {
82         supportedAttributes.add(SVGNames::inAttr);
83         supportedAttributes.add(SVGNames::orderAttr);
84         supportedAttributes.add(SVGNames::kernelMatrixAttr);
85         supportedAttributes.add(SVGNames::edgeModeAttr);
86         supportedAttributes.add(SVGNames::divisorAttr);
87         supportedAttributes.add(SVGNames::biasAttr);
88         supportedAttributes.add(SVGNames::targetXAttr);
89         supportedAttributes.add(SVGNames::targetYAttr);
90         supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
91         supportedAttributes.add(SVGNames::preserveAlphaAttr);
92     }
93     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
94 }
95
96 void SVGFEConvolveMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
97 {
98     if (!isSupportedAttribute(name)) {
99         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
100         return;
101     }
102
103     SVGParsingError parseError = NoError;
104
105     if (name == SVGNames::inAttr)
106         m_in1->setBaseValueAsString(value, parseError);
107     else if (name == SVGNames::divisorAttr)
108         m_divisor->setBaseValueAsString(value, parseError);
109     else if (name == SVGNames::biasAttr)
110         m_bias->setBaseValueAsString(value, parseError);
111     else if (name == SVGNames::kernelUnitLengthAttr)
112         m_kernelUnitLength->setBaseValueAsString(value, parseError);
113     else if (name == SVGNames::kernelMatrixAttr)
114         m_kernelMatrix->setBaseValueAsString(value, parseError);
115     else if (name == SVGNames::preserveAlphaAttr)
116         m_preserveAlpha->setBaseValueAsString(value, parseError);
117     else if (name == SVGNames::edgeModeAttr)
118         m_edgeMode->setBaseValueAsString(value, parseError);
119     else if (name == SVGNames::targetXAttr)
120         m_targetX->setBaseValueAsString(value, parseError);
121     else if (name == SVGNames::targetYAttr)
122         m_targetY->setBaseValueAsString(value, parseError);
123     else if (name == SVGNames::orderAttr) {
124         m_order->setBaseValueAsString(value, parseError);
125         if (parseError == NoError && (orderX()->baseValue()->value() < 1 || orderY()->baseValue()->value() < 1)) {
126             document().accessSVGExtensions().reportWarning(
127                 "feConvolveMatrix: problem parsing order=\"" + value
128                 + "\". Filtered element will not be displayed.");
129         }
130     } else
131         ASSERT_NOT_REACHED();
132
133     reportAttributeParsingError(parseError, name, value);
134 }
135
136 bool SVGFEConvolveMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
137 {
138     FEConvolveMatrix* convolveMatrix = static_cast<FEConvolveMatrix*>(effect);
139     if (attrName == SVGNames::edgeModeAttr)
140         return convolveMatrix->setEdgeMode(m_edgeMode->currentValue()->enumValue());
141     if (attrName == SVGNames::divisorAttr)
142         return convolveMatrix->setDivisor(m_divisor->currentValue()->value());
143     if (attrName == SVGNames::biasAttr)
144         return convolveMatrix->setBias(m_bias->currentValue()->value());
145     if (attrName == SVGNames::targetXAttr)
146         return convolveMatrix->setTargetOffset(IntPoint(m_targetX->currentValue()->value(), m_targetY->currentValue()->value()));
147     if (attrName == SVGNames::targetYAttr)
148         return convolveMatrix->setTargetOffset(IntPoint(m_targetX->currentValue()->value(), m_targetY->currentValue()->value()));
149     if (attrName == SVGNames::kernelUnitLengthAttr)
150         return convolveMatrix->setKernelUnitLength(FloatPoint(kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value()));
151     if (attrName == SVGNames::preserveAlphaAttr)
152         return convolveMatrix->setPreserveAlpha(m_preserveAlpha->currentValue()->value());
153
154     ASSERT_NOT_REACHED();
155     return false;
156 }
157
158 void SVGFEConvolveMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
159 {
160     if (!isSupportedAttribute(attrName)) {
161         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
162         return;
163     }
164
165     SVGElementInstance::InvalidationGuard invalidationGuard(this);
166
167     if (attrName == SVGNames::edgeModeAttr
168         || attrName == SVGNames::divisorAttr
169         || attrName == SVGNames::biasAttr
170         || attrName == SVGNames::targetXAttr
171         || attrName == SVGNames::targetYAttr
172         || attrName == SVGNames::kernelUnitLengthAttr
173         || attrName == SVGNames::preserveAlphaAttr) {
174         primitiveAttributeChanged(attrName);
175         return;
176     }
177
178     if (attrName == SVGNames::inAttr
179         || attrName == SVGNames::orderAttr
180         || attrName == SVGNames::kernelMatrixAttr) {
181         invalidate();
182         return;
183     }
184
185     ASSERT_NOT_REACHED();
186 }
187
188 PassRefPtr<FilterEffect> SVGFEConvolveMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
189 {
190     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
191
192     if (!input1)
193         return nullptr;
194
195     int orderXValue = orderX()->currentValue()->value();
196     int orderYValue = orderY()->currentValue()->value();
197     if (!hasAttribute(SVGNames::orderAttr)) {
198         orderXValue = 3;
199         orderYValue = 3;
200     }
201     // Spec says order must be > 0. Bail if it is not.
202     if (orderXValue < 1 || orderYValue < 1)
203         return nullptr;
204     RefPtr<SVGNumberList> kernelMatrix = this->m_kernelMatrix->currentValue();
205     size_t kernelMatrixSize = kernelMatrix->length();
206     // The spec says this is a requirement, and should bail out if fails
207     if (orderXValue * orderYValue != static_cast<int>(kernelMatrixSize))
208         return nullptr;
209
210     int targetXValue = m_targetX->currentValue()->value();
211     int targetYValue = m_targetY->currentValue()->value();
212     if (hasAttribute(SVGNames::targetXAttr) && (targetXValue < 0 || targetXValue >= orderXValue))
213         return nullptr;
214     // The spec says the default value is: targetX = floor ( orderX / 2 ))
215     if (!hasAttribute(SVGNames::targetXAttr))
216         targetXValue = static_cast<int>(floorf(orderXValue / 2));
217     if (hasAttribute(SVGNames::targetYAttr) && (targetYValue < 0 || targetYValue >= orderYValue))
218         return nullptr;
219     // The spec says the default value is: targetY = floor ( orderY / 2 ))
220     if (!hasAttribute(SVGNames::targetYAttr))
221         targetYValue = static_cast<int>(floorf(orderYValue / 2));
222
223     // Spec says default kernelUnitLength is 1.0, and a specified length cannot be 0.
224     // FIXME: Why is this cast from float -> int -> float?
225     int kernelUnitLengthXValue = kernelUnitLengthX()->currentValue()->value();
226     int kernelUnitLengthYValue = kernelUnitLengthY()->currentValue()->value();
227     if (!hasAttribute(SVGNames::kernelUnitLengthAttr)) {
228         kernelUnitLengthXValue = 1;
229         kernelUnitLengthYValue = 1;
230     }
231     if (kernelUnitLengthXValue <= 0 || kernelUnitLengthYValue <= 0)
232         return nullptr;
233
234     float divisorValue = m_divisor->currentValue()->value();
235     if (hasAttribute(SVGNames::divisorAttr) && !divisorValue)
236         return nullptr;
237     if (!hasAttribute(SVGNames::divisorAttr)) {
238         for (size_t i = 0; i < kernelMatrixSize; ++i)
239             divisorValue += kernelMatrix->at(i)->value();
240         if (!divisorValue)
241             divisorValue = 1;
242     }
243
244     RefPtr<FilterEffect> effect = FEConvolveMatrix::create(filter,
245                     IntSize(orderXValue, orderYValue), divisorValue,
246                     m_bias->currentValue()->value(), IntPoint(targetXValue, targetYValue), m_edgeMode->currentValue()->enumValue(),
247                     FloatPoint(kernelUnitLengthXValue, kernelUnitLengthYValue), m_preserveAlpha->currentValue()->value(), m_kernelMatrix->currentValue()->toFloatVector());
248     effect->inputEffects().append(input1);
249     return effect.release();
250 }
251
252 } // namespace WebCore