8a4b6f9fa7860d62fb660e5249220803f02dadd0
[WebKit-https.git] / Source / WebCore / svg / SVGFEComponentTransferElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22
23 #if ENABLE(SVG) && ENABLE(FILTERS)
24 #include "SVGFEComponentTransferElement.h"
25
26 #include "Attr.h"
27 #include "FilterEffect.h"
28 #include "SVGFEFuncAElement.h"
29 #include "SVGFEFuncBElement.h"
30 #include "SVGFEFuncGElement.h"
31 #include "SVGFEFuncRElement.h"
32 #include "SVGFilterBuilder.h"
33 #include "SVGNames.h"
34
35 namespace WebCore {
36
37 // Animated property definitions
38 DEFINE_ANIMATED_STRING(SVGFEComponentTransferElement, SVGNames::inAttr, In1, in1)
39
40 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEComponentTransferElement)
41     REGISTER_LOCAL_ANIMATED_PROPERTY(in1)
42     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
43 END_REGISTER_ANIMATED_PROPERTIES
44
45 inline SVGFEComponentTransferElement::SVGFEComponentTransferElement(const QualifiedName& tagName, Document* document)
46     : SVGFilterPrimitiveStandardAttributes(tagName, document)
47 {
48     ASSERT(hasTagName(SVGNames::feComponentTransferTag));
49     registerAnimatedPropertiesForSVGFEComponentTransferElement();
50 }
51
52 PassRefPtr<SVGFEComponentTransferElement> SVGFEComponentTransferElement::create(const QualifiedName& tagName, Document* document)
53 {
54     return adoptRef(new SVGFEComponentTransferElement(tagName, document));
55 }
56
57 bool SVGFEComponentTransferElement::isSupportedAttribute(const QualifiedName& attrName)
58 {
59     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
60     if (supportedAttributes.isEmpty())
61         supportedAttributes.add(SVGNames::inAttr);
62     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
63 }
64
65 void SVGFEComponentTransferElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
66 {
67     if (!isSupportedAttribute(name)) {
68         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
69         return;
70     }
71
72     if (name == SVGNames::inAttr) {
73         setIn1BaseValue(value);
74         return;
75     }
76
77     ASSERT_NOT_REACHED();
78 }
79
80 PassRefPtr<FilterEffect> SVGFEComponentTransferElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
81 {
82     FilterEffect* input1 = filterBuilder->getEffectById(in1());
83     
84     if (!input1)
85         return 0;
86
87     ComponentTransferFunction red;
88     ComponentTransferFunction green;
89     ComponentTransferFunction blue;
90     ComponentTransferFunction alpha;
91     
92     for (Node* node = firstChild(); node; node = node->nextSibling()) {
93         if (node->hasTagName(SVGNames::feFuncRTag))
94             red = static_cast<SVGFEFuncRElement*>(node)->transferFunction();
95         else if (node->hasTagName(SVGNames::feFuncGTag))
96             green = static_cast<SVGFEFuncGElement*>(node)->transferFunction();
97         else if (node->hasTagName(SVGNames::feFuncBTag))
98             blue = static_cast<SVGFEFuncBElement*>(node)->transferFunction();
99         else if (node->hasTagName(SVGNames::feFuncATag))
100             alpha = static_cast<SVGFEFuncAElement*>(node)->transferFunction();
101     }
102     
103     RefPtr<FilterEffect> effect = FEComponentTransfer::create(filter, red, green, blue, alpha);
104     effect->inputEffects().append(input1);
105     return effect.release();
106 }
107
108 }
109
110 #endif