OSDN Git Service

Merge Webkit at r70949: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / platform / graphics / filters / FEComposite.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  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include "config.h"
25
26 #if ENABLE(FILTERS)
27 #include "FEComposite.h"
28
29 #include "Filter.h"
30 #include "GraphicsContext.h"
31 #include "ImageData.h"
32
33 namespace WebCore {
34
35 FEComposite::FEComposite(const CompositeOperationType& type, float k1, float k2, float k3, float k4)
36     : FilterEffect()
37     , m_type(type)
38     , m_k1(k1)
39     , m_k2(k2)
40     , m_k3(k3)
41     , m_k4(k4)
42 {
43 }
44
45 PassRefPtr<FEComposite> FEComposite::create(const CompositeOperationType& type, float k1, float k2, float k3, float k4)
46 {
47     return adoptRef(new FEComposite(type, k1, k2, k3, k4));
48 }
49
50 CompositeOperationType FEComposite::operation() const
51 {
52     return m_type;
53 }
54
55 void FEComposite::setOperation(CompositeOperationType type)
56 {
57     m_type = type;
58 }
59
60 float FEComposite::k1() const
61 {
62     return m_k1;
63 }
64
65 void FEComposite::setK1(float k1)
66 {
67     m_k1 = k1;
68 }
69
70 float FEComposite::k2() const
71 {
72     return m_k2;
73 }
74
75 void FEComposite::setK2(float k2)
76 {
77     m_k2 = k2;
78 }
79
80 float FEComposite::k3() const
81 {
82     return m_k3;
83 }
84
85 void FEComposite::setK3(float k3)
86 {
87     m_k3 = k3;
88 }
89
90 float FEComposite::k4() const
91 {
92     return m_k4;
93 }
94
95 void FEComposite::setK4(float k4)
96 {
97     m_k4 = k4;
98 }
99
100 inline void arithmetic(const ByteArray* srcPixelArrayA, ByteArray* srcPixelArrayB,
101                        float k1, float k2, float k3, float k4)
102 {
103     float scaledK1 = k1 / 255.f;
104     float scaledK4 = k4 * 255.f;
105     unsigned pixelArrayLength = srcPixelArrayA->length();
106     for (unsigned pixelOffset = 0; pixelOffset < pixelArrayLength; pixelOffset += 4) {
107         for (unsigned channel = 0; channel < 4; ++channel) {
108             unsigned char i1 = srcPixelArrayA->get(pixelOffset + channel);
109             unsigned char i2 = srcPixelArrayB->get(pixelOffset + channel);
110
111             double result = scaledK1 * i1 * i2 + k2 * i1 + k3 * i2 + scaledK4;
112             srcPixelArrayB->set(pixelOffset + channel, result);
113         }
114     }
115 }
116     
117 void FEComposite::determineAbsolutePaintRect(Filter* filter)
118 {
119     switch (m_type) {
120     case FECOMPOSITE_OPERATOR_IN:
121     case FECOMPOSITE_OPERATOR_ATOP:
122         // For In and Atop the first effect just influences the result of
123         // the second effect. So just use the absolute paint rect of the second effect here.
124         setAbsolutePaintRect(inputEffect(1)->absolutePaintRect());
125         return;
126     case FECOMPOSITE_OPERATOR_ARITHMETIC:
127         // Arithmetic may influnce the compele filter primitive region. So we can't
128         // optimize the paint region here.
129         setAbsolutePaintRect(maxEffectRect());
130         return;
131     default:
132         // Take the union of both input effects.
133         FilterEffect::determineAbsolutePaintRect(filter);
134         return;
135     }
136 }
137
138 void FEComposite::apply(Filter* filter)
139 {
140     FilterEffect* in = inputEffect(0);
141     FilterEffect* in2 = inputEffect(1);
142     in->apply(filter);
143     in2->apply(filter);
144     if (!in->resultImage() || !in2->resultImage())
145         return;
146
147     GraphicsContext* filterContext = effectContext(filter);
148     if (!filterContext)
149         return;
150
151     FloatRect srcRect = FloatRect(0, 0, -1, -1);
152     switch (m_type) {
153     case FECOMPOSITE_OPERATOR_OVER:
154         filterContext->drawImageBuffer(in2->resultImage(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in2->absolutePaintRect()));
155         filterContext->drawImageBuffer(in->resultImage(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in->absolutePaintRect()));
156         break;
157     case FECOMPOSITE_OPERATOR_IN:
158         filterContext->save();
159         filterContext->clipToImageBuffer(in2->resultImage(), drawingRegionOfInputImage(in2->absolutePaintRect()));
160         filterContext->drawImageBuffer(in->resultImage(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in->absolutePaintRect()));
161         filterContext->restore();
162         break;
163     case FECOMPOSITE_OPERATOR_OUT:
164         filterContext->drawImageBuffer(in->resultImage(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in->absolutePaintRect()));
165         filterContext->drawImageBuffer(in2->resultImage(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in2->absolutePaintRect()), srcRect, CompositeDestinationOut);
166         break;
167     case FECOMPOSITE_OPERATOR_ATOP:
168         filterContext->drawImageBuffer(in2->resultImage(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in2->absolutePaintRect()));
169         filterContext->drawImageBuffer(in->resultImage(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in->absolutePaintRect()), srcRect, CompositeSourceAtop);
170         break;
171     case FECOMPOSITE_OPERATOR_XOR:
172         filterContext->drawImageBuffer(in2->resultImage(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in2->absolutePaintRect()));
173         filterContext->drawImageBuffer(in->resultImage(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in->absolutePaintRect()), srcRect, CompositeXOR);
174         break;
175     case FECOMPOSITE_OPERATOR_ARITHMETIC: {
176         IntRect effectADrawingRect = requestedRegionOfInputImageData(in->absolutePaintRect());
177         RefPtr<ImageData> srcImageData = in->resultImage()->getPremultipliedImageData(effectADrawingRect);
178         ByteArray* srcPixelArrayA = srcImageData->data()->data();
179
180         IntRect effectBDrawingRect = requestedRegionOfInputImageData(in2->absolutePaintRect());
181         RefPtr<ImageData> imageData = in2->resultImage()->getPremultipliedImageData(effectBDrawingRect);
182         ByteArray* srcPixelArrayB = imageData->data()->data();
183
184         arithmetic(srcPixelArrayA, srcPixelArrayB, m_k1, m_k2, m_k3, m_k4);
185         resultImage()->putPremultipliedImageData(imageData.get(), IntRect(IntPoint(), resultImage()->size()), IntPoint());
186         }
187         break;
188     default:
189         break;
190     }
191 }
192
193 void FEComposite::dump()
194 {
195 }
196
197 static TextStream& operator<<(TextStream& ts, const CompositeOperationType& type)
198 {
199     switch (type) {
200     case FECOMPOSITE_OPERATOR_UNKNOWN:
201         ts << "UNKNOWN";
202         break;
203     case FECOMPOSITE_OPERATOR_OVER:
204         ts << "OVER";
205         break;
206     case FECOMPOSITE_OPERATOR_IN:
207         ts << "IN";
208         break;
209     case FECOMPOSITE_OPERATOR_OUT:
210         ts << "OUT";
211         break;
212     case FECOMPOSITE_OPERATOR_ATOP:
213         ts << "ATOP";
214         break;
215     case FECOMPOSITE_OPERATOR_XOR:
216         ts << "XOR";
217         break;
218     case FECOMPOSITE_OPERATOR_ARITHMETIC:
219         ts << "ARITHMETIC";
220         break;
221     }
222     return ts;
223 }
224
225 TextStream& FEComposite::externalRepresentation(TextStream& ts, int indent) const
226 {
227     writeIndent(ts, indent);
228     ts << "[feComposite";
229     FilterEffect::externalRepresentation(ts);
230     ts << " operation=\"" << m_type << "\"";
231     if (m_type == FECOMPOSITE_OPERATOR_ARITHMETIC)
232         ts << " k1=\"" << m_k1 << "\" k2=\"" << m_k2 << "\" k3=\"" << m_k3 << "\" k4=\"" << m_k4 << "\"";
233     ts << "]\n";
234     inputEffect(0)->externalRepresentation(ts, indent + 1);
235     inputEffect(1)->externalRepresentation(ts, indent + 1);
236     return ts;
237 }
238
239 } // namespace WebCore
240
241 #endif // ENABLE(FILTERS)