Upstream version 5.34.104.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 // Animated property definitions
36 DEFINE_ANIMATED_ENUMERATION(SVGFEConvolveMatrixElement, SVGNames::edgeModeAttr, EdgeMode, edgeMode, EdgeModeType)
37
38 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEConvolveMatrixElement)
39     REGISTER_LOCAL_ANIMATED_PROPERTY(edgeMode)
40     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
41 END_REGISTER_ANIMATED_PROPERTIES
42
43 inline SVGFEConvolveMatrixElement::SVGFEConvolveMatrixElement(Document& document)
44     : SVGFilterPrimitiveStandardAttributes(SVGNames::feConvolveMatrixTag, document)
45     , m_bias(SVGAnimatedNumber::create(this, SVGNames::biasAttr, SVGNumber::create()))
46     , m_divisor(SVGAnimatedNumber::create(this, SVGNames::divisorAttr, SVGNumber::create()))
47     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
48     , m_kernelMatrix(SVGAnimatedNumberList::create(this, SVGNames::kernelMatrixAttr, SVGNumberList::create()))
49     , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::kernelUnitLengthAttr))
50     , m_order(SVGAnimatedIntegerOptionalInteger::create(this, SVGNames::orderAttr))
51     , m_preserveAlpha(SVGAnimatedBoolean::create(this, SVGNames::preserveAlphaAttr, SVGBoolean::create()))
52     , m_targetX(SVGAnimatedInteger::create(this, SVGNames::targetXAttr, SVGInteger::create()))
53     , m_targetY(SVGAnimatedInteger::create(this, SVGNames::targetYAttr, SVGInteger::create()))
54     , m_edgeMode(EDGEMODE_DUPLICATE)
55 {
56     ScriptWrappable::init(this);
57
58     addToPropertyMap(m_preserveAlpha);
59     addToPropertyMap(m_divisor);
60     addToPropertyMap(m_bias);
61     addToPropertyMap(m_kernelUnitLength);
62     addToPropertyMap(m_kernelMatrix);
63     addToPropertyMap(m_in1);
64     addToPropertyMap(m_order);
65     addToPropertyMap(m_targetX);
66     addToPropertyMap(m_targetY);
67     registerAnimatedPropertiesForSVGFEConvolveMatrixElement();
68 }
69
70 PassRefPtr<SVGFEConvolveMatrixElement> SVGFEConvolveMatrixElement::create(Document& document)
71 {
72     return adoptRef(new SVGFEConvolveMatrixElement(document));
73 }
74
75 bool SVGFEConvolveMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
76 {
77     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
78     if (supportedAttributes.isEmpty()) {
79         supportedAttributes.add(SVGNames::inAttr);
80         supportedAttributes.add(SVGNames::orderAttr);
81         supportedAttributes.add(SVGNames::kernelMatrixAttr);
82         supportedAttributes.add(SVGNames::edgeModeAttr);
83         supportedAttributes.add(SVGNames::divisorAttr);
84         supportedAttributes.add(SVGNames::biasAttr);
85         supportedAttributes.add(SVGNames::targetXAttr);
86         supportedAttributes.add(SVGNames::targetYAttr);
87         supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
88         supportedAttributes.add(SVGNames::preserveAlphaAttr);
89     }
90     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
91 }
92
93 void SVGFEConvolveMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
94 {
95     if (!isSupportedAttribute(name)) {
96         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
97         return;
98     }
99
100     if (name == SVGNames::edgeModeAttr) {
101         EdgeModeType propertyValue = SVGPropertyTraits<EdgeModeType>::fromString(value);
102         if (propertyValue > 0)
103             setEdgeModeBaseValue(propertyValue);
104         else
105             document().accessSVGExtensions()->reportWarning(
106                 "feConvolveMatrix: problem parsing edgeMode=\"" + value
107                 + "\". Filtered element will not be displayed.");
108         return;
109     }
110
111     SVGParsingError parseError = NoError;
112
113     if (name == SVGNames::inAttr)
114         m_in1->setBaseValueAsString(value, parseError);
115     else if (name == SVGNames::divisorAttr)
116         m_divisor->setBaseValueAsString(value, parseError);
117     else if (name == SVGNames::biasAttr)
118         m_bias->setBaseValueAsString(value, parseError);
119     else if (name == SVGNames::kernelUnitLengthAttr)
120         m_kernelUnitLength->setBaseValueAsString(value, parseError);
121     else if (name == SVGNames::kernelMatrixAttr)
122         m_kernelMatrix->setBaseValueAsString(value, parseError);
123     else if (name == SVGNames::preserveAlphaAttr)
124         m_preserveAlpha->setBaseValueAsString(value, parseError);
125     else if (name == SVGNames::targetXAttr)
126         m_targetX->setBaseValueAsString(value, parseError);
127     else if (name == SVGNames::targetYAttr)
128         m_targetY->setBaseValueAsString(value, parseError);
129     else if (name == SVGNames::orderAttr) {
130         m_order->setBaseValueAsString(value, parseError);
131         if (parseError == NoError && (orderX()->baseValue()->value() < 1 || orderY()->baseValue()->value() < 1)) {
132             document().accessSVGExtensions()->reportWarning(
133                 "feConvolveMatrix: problem parsing order=\"" + value
134                 + "\". Filtered element will not be displayed.");
135         }
136     } else
137         ASSERT_NOT_REACHED();
138
139     reportAttributeParsingError(parseError, name, value);
140 }
141
142 bool SVGFEConvolveMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
143 {
144     FEConvolveMatrix* convolveMatrix = static_cast<FEConvolveMatrix*>(effect);
145     if (attrName == SVGNames::edgeModeAttr)
146         return convolveMatrix->setEdgeMode(edgeModeCurrentValue());
147     if (attrName == SVGNames::divisorAttr)
148         return convolveMatrix->setDivisor(m_divisor->currentValue()->value());
149     if (attrName == SVGNames::biasAttr)
150         return convolveMatrix->setBias(m_bias->currentValue()->value());
151     if (attrName == SVGNames::targetXAttr)
152         return convolveMatrix->setTargetOffset(IntPoint(m_targetX->currentValue()->value(), m_targetY->currentValue()->value()));
153     if (attrName == SVGNames::targetYAttr)
154         return convolveMatrix->setTargetOffset(IntPoint(m_targetX->currentValue()->value(), m_targetY->currentValue()->value()));
155     if (attrName == SVGNames::kernelUnitLengthAttr)
156         return convolveMatrix->setKernelUnitLength(FloatPoint(kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value()));
157     if (attrName == SVGNames::preserveAlphaAttr)
158         return convolveMatrix->setPreserveAlpha(m_preserveAlpha->currentValue()->value());
159
160     ASSERT_NOT_REACHED();
161     return false;
162 }
163
164 void SVGFEConvolveMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
165 {
166     if (!isSupportedAttribute(attrName)) {
167         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
168         return;
169     }
170
171     SVGElementInstance::InvalidationGuard invalidationGuard(this);
172
173     if (attrName == SVGNames::edgeModeAttr
174         || attrName == SVGNames::divisorAttr
175         || attrName == SVGNames::biasAttr
176         || attrName == SVGNames::targetXAttr
177         || attrName == SVGNames::targetYAttr
178         || attrName == SVGNames::kernelUnitLengthAttr
179         || attrName == SVGNames::preserveAlphaAttr) {
180         primitiveAttributeChanged(attrName);
181         return;
182     }
183
184     if (attrName == SVGNames::inAttr
185         || attrName == SVGNames::orderAttr
186         || attrName == SVGNames::kernelMatrixAttr) {
187         invalidate();
188         return;
189     }
190
191     ASSERT_NOT_REACHED();
192 }
193
194 PassRefPtr<FilterEffect> SVGFEConvolveMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
195 {
196     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
197
198     if (!input1)
199         return 0;
200
201     int orderXValue = orderX()->currentValue()->value();
202     int orderYValue = orderY()->currentValue()->value();
203     if (!hasAttribute(SVGNames::orderAttr)) {
204         orderXValue = 3;
205         orderYValue = 3;
206     }
207     // Spec says order must be > 0. Bail if it is not.
208     if (orderXValue < 1 || orderYValue < 1)
209         return 0;
210     RefPtr<SVGNumberList> kernelMatrix = this->m_kernelMatrix->currentValue();
211     size_t kernelMatrixSize = kernelMatrix->numberOfItems();
212     // The spec says this is a requirement, and should bail out if fails
213     if (orderXValue * orderYValue != static_cast<int>(kernelMatrixSize))
214         return 0;
215
216     int targetXValue = m_targetX->currentValue()->value();
217     int targetYValue = m_targetY->currentValue()->value();
218     if (hasAttribute(SVGNames::targetXAttr) && (targetXValue < 0 || targetXValue >= orderXValue))
219         return 0;
220     // The spec says the default value is: targetX = floor ( orderX / 2 ))
221     if (!hasAttribute(SVGNames::targetXAttr))
222         targetXValue = static_cast<int>(floorf(orderXValue / 2));
223     if (hasAttribute(SVGNames::targetYAttr) && (targetYValue < 0 || targetYValue >= orderYValue))
224         return 0;
225     // The spec says the default value is: targetY = floor ( orderY / 2 ))
226     if (!hasAttribute(SVGNames::targetYAttr))
227         targetYValue = static_cast<int>(floorf(orderYValue / 2));
228
229     // Spec says default kernelUnitLength is 1.0, and a specified length cannot be 0.
230     // FIXME: Why is this cast from float -> int -> float?
231     int kernelUnitLengthXValue = kernelUnitLengthX()->currentValue()->value();
232     int kernelUnitLengthYValue = kernelUnitLengthY()->currentValue()->value();
233     if (!hasAttribute(SVGNames::kernelUnitLengthAttr)) {
234         kernelUnitLengthXValue = 1;
235         kernelUnitLengthYValue = 1;
236     }
237     if (kernelUnitLengthXValue <= 0 || kernelUnitLengthYValue <= 0)
238         return 0;
239
240     float divisorValue = m_divisor->currentValue()->value();
241     if (hasAttribute(SVGNames::divisorAttr) && !divisorValue)
242         return 0;
243     if (!hasAttribute(SVGNames::divisorAttr)) {
244         for (size_t i = 0; i < kernelMatrixSize; ++i)
245             divisorValue += kernelMatrix->at(i)->value();
246         if (!divisorValue)
247             divisorValue = 1;
248     }
249
250     RefPtr<FilterEffect> effect = FEConvolveMatrix::create(filter,
251                     IntSize(orderXValue, orderYValue), divisorValue,
252                     m_bias->currentValue()->value(), IntPoint(targetXValue, targetYValue), edgeModeCurrentValue(),
253                     FloatPoint(kernelUnitLengthXValue, kernelUnitLengthYValue), m_preserveAlpha->currentValue()->value(), m_kernelMatrix->currentValue()->toFloatVector());
254     effect->inputEffects().append(input1);
255     return effect.release();
256 }
257
258 } // namespace WebCore