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 "FEComponentTransfer.h"
28
29 #include "Filter.h"
30 #include "GraphicsContext.h"
31 #include "RenderTreeAsText.h"
32 #include "TextStream.h"
33
34 #include <wtf/ByteArray.h>
35 #include <wtf/MathExtras.h>
36 #include <wtf/StdLibExtras.h>
37
38 namespace WebCore {
39
40 typedef void (*TransferType)(unsigned char*, const ComponentTransferFunction&);
41
FEComponentTransfer(Filter * filter,const ComponentTransferFunction & redFunc,const ComponentTransferFunction & greenFunc,const ComponentTransferFunction & blueFunc,const ComponentTransferFunction & alphaFunc)42 FEComponentTransfer::FEComponentTransfer(Filter* filter, const ComponentTransferFunction& redFunc, const ComponentTransferFunction& greenFunc,
43 const ComponentTransferFunction& blueFunc, const ComponentTransferFunction& alphaFunc)
44 : FilterEffect(filter)
45 , m_redFunc(redFunc)
46 , m_greenFunc(greenFunc)
47 , m_blueFunc(blueFunc)
48 , m_alphaFunc(alphaFunc)
49 {
50 }
51
create(Filter * filter,const ComponentTransferFunction & redFunc,const ComponentTransferFunction & greenFunc,const ComponentTransferFunction & blueFunc,const ComponentTransferFunction & alphaFunc)52 PassRefPtr<FEComponentTransfer> FEComponentTransfer::create(Filter* filter, const ComponentTransferFunction& redFunc,
53 const ComponentTransferFunction& greenFunc, const ComponentTransferFunction& blueFunc, const ComponentTransferFunction& alphaFunc)
54 {
55 return adoptRef(new FEComponentTransfer(filter, redFunc, greenFunc, blueFunc, alphaFunc));
56 }
57
redFunction() const58 ComponentTransferFunction FEComponentTransfer::redFunction() const
59 {
60 return m_redFunc;
61 }
62
setRedFunction(const ComponentTransferFunction & func)63 void FEComponentTransfer::setRedFunction(const ComponentTransferFunction& func)
64 {
65 m_redFunc = func;
66 }
67
greenFunction() const68 ComponentTransferFunction FEComponentTransfer::greenFunction() const
69 {
70 return m_greenFunc;
71 }
72
setGreenFunction(const ComponentTransferFunction & func)73 void FEComponentTransfer::setGreenFunction(const ComponentTransferFunction& func)
74 {
75 m_greenFunc = func;
76 }
77
blueFunction() const78 ComponentTransferFunction FEComponentTransfer::blueFunction() const
79 {
80 return m_blueFunc;
81 }
82
setBlueFunction(const ComponentTransferFunction & func)83 void FEComponentTransfer::setBlueFunction(const ComponentTransferFunction& func)
84 {
85 m_blueFunc = func;
86 }
87
alphaFunction() const88 ComponentTransferFunction FEComponentTransfer::alphaFunction() const
89 {
90 return m_alphaFunc;
91 }
92
setAlphaFunction(const ComponentTransferFunction & func)93 void FEComponentTransfer::setAlphaFunction(const ComponentTransferFunction& func)
94 {
95 m_alphaFunc = func;
96 }
97
identity(unsigned char *,const ComponentTransferFunction &)98 static void identity(unsigned char*, const ComponentTransferFunction&)
99 {
100 }
101
table(unsigned char * values,const ComponentTransferFunction & transferFunction)102 static void table(unsigned char* values, const ComponentTransferFunction& transferFunction)
103 {
104 const Vector<float>& tableValues = transferFunction.tableValues;
105 unsigned n = tableValues.size();
106 if (n < 1)
107 return;
108 for (unsigned i = 0; i < 256; ++i) {
109 double c = i / 255.0;
110 unsigned k = static_cast<unsigned>(c * (n - 1));
111 double v1 = tableValues[k];
112 double v2 = tableValues[std::min((k + 1), (n - 1))];
113 double val = 255.0 * (v1 + (c * (n - 1) - k) * (v2 - v1));
114 val = std::max(0.0, std::min(255.0, val));
115 values[i] = static_cast<unsigned char>(val);
116 }
117 }
118
discrete(unsigned char * values,const ComponentTransferFunction & transferFunction)119 static void discrete(unsigned char* values, const ComponentTransferFunction& transferFunction)
120 {
121 const Vector<float>& tableValues = transferFunction.tableValues;
122 unsigned n = tableValues.size();
123 if (n < 1)
124 return;
125 for (unsigned i = 0; i < 256; ++i) {
126 unsigned k = static_cast<unsigned>((i * n) / 255.0);
127 k = std::min(k, n - 1);
128 double val = 255 * tableValues[k];
129 val = std::max(0.0, std::min(255.0, val));
130 values[i] = static_cast<unsigned char>(val);
131 }
132 }
133
linear(unsigned char * values,const ComponentTransferFunction & transferFunction)134 static void linear(unsigned char* values, const ComponentTransferFunction& transferFunction)
135 {
136 for (unsigned i = 0; i < 256; ++i) {
137 double val = transferFunction.slope * i + 255 * transferFunction.intercept;
138 val = std::max(0.0, std::min(255.0, val));
139 values[i] = static_cast<unsigned char>(val);
140 }
141 }
142
gamma(unsigned char * values,const ComponentTransferFunction & transferFunction)143 static void gamma(unsigned char* values, const ComponentTransferFunction& transferFunction)
144 {
145 for (unsigned i = 0; i < 256; ++i) {
146 double exponent = transferFunction.exponent; // RCVT doesn't like passing a double and a float to pow, so promote this to double
147 double val = 255.0 * (transferFunction.amplitude * pow((i / 255.0), exponent) + transferFunction.offset);
148 val = std::max(0.0, std::min(255.0, val));
149 values[i] = static_cast<unsigned char>(val);
150 }
151 }
152
apply()153 void FEComponentTransfer::apply()
154 {
155 if (hasResult())
156 return;
157 FilterEffect* in = inputEffect(0);
158 in->apply();
159 if (!in->hasResult())
160 return;
161
162 ByteArray* pixelArray = createUnmultipliedImageResult();
163 if (!pixelArray)
164 return;
165
166 unsigned char rValues[256], gValues[256], bValues[256], aValues[256];
167 for (unsigned i = 0; i < 256; ++i)
168 rValues[i] = gValues[i] = bValues[i] = aValues[i] = i;
169 unsigned char* tables[] = { rValues, gValues, bValues, aValues };
170 ComponentTransferFunction transferFunction[] = {m_redFunc, m_greenFunc, m_blueFunc, m_alphaFunc};
171 TransferType callEffect[] = {identity, identity, table, discrete, linear, gamma};
172
173 for (unsigned channel = 0; channel < 4; channel++) {
174 ASSERT(static_cast<size_t>(transferFunction[channel].type) < WTF_ARRAY_LENGTH(callEffect));
175 (*callEffect[transferFunction[channel].type])(tables[channel], transferFunction[channel]);
176 }
177
178 IntRect drawingRect = requestedRegionOfInputImageData(in->absolutePaintRect());
179 in->copyUnmultipliedImage(pixelArray, drawingRect);
180
181 unsigned pixelArrayLength = pixelArray->length();
182 for (unsigned pixelOffset = 0; pixelOffset < pixelArrayLength; pixelOffset += 4) {
183 for (unsigned channel = 0; channel < 4; ++channel) {
184 unsigned char c = pixelArray->get(pixelOffset + channel);
185 pixelArray->set(pixelOffset + channel, tables[channel][c]);
186 }
187 }
188 }
189
dump()190 void FEComponentTransfer::dump()
191 {
192 }
193
operator <<(TextStream & ts,const ComponentTransferType & type)194 static TextStream& operator<<(TextStream& ts, const ComponentTransferType& type)
195 {
196 switch (type) {
197 case FECOMPONENTTRANSFER_TYPE_UNKNOWN:
198 ts << "UNKNOWN";
199 break;
200 case FECOMPONENTTRANSFER_TYPE_IDENTITY:
201 ts << "IDENTITY";
202 break;
203 case FECOMPONENTTRANSFER_TYPE_TABLE:
204 ts << "TABLE";
205 break;
206 case FECOMPONENTTRANSFER_TYPE_DISCRETE:
207 ts << "DISCRETE";
208 break;
209 case FECOMPONENTTRANSFER_TYPE_LINEAR:
210 ts << "LINEAR";
211 break;
212 case FECOMPONENTTRANSFER_TYPE_GAMMA:
213 ts << "GAMMA";
214 break;
215 }
216 return ts;
217 }
218
operator <<(TextStream & ts,const ComponentTransferFunction & function)219 static TextStream& operator<<(TextStream& ts, const ComponentTransferFunction& function)
220 {
221 ts << "type=\"" << function.type
222 << "\" slope=\"" << function.slope
223 << "\" intercept=\"" << function.intercept
224 << "\" amplitude=\"" << function.amplitude
225 << "\" exponent=\"" << function.exponent
226 << "\" offset=\"" << function.offset << "\"";
227 return ts;
228 }
229
externalRepresentation(TextStream & ts,int indent) const230 TextStream& FEComponentTransfer::externalRepresentation(TextStream& ts, int indent) const
231 {
232 writeIndent(ts, indent);
233 ts << "[feComponentTransfer";
234 FilterEffect::externalRepresentation(ts);
235 ts << " \n";
236 writeIndent(ts, indent + 2);
237 ts << "{red: " << m_redFunc << "}\n";
238 writeIndent(ts, indent + 2);
239 ts << "{green: " << m_greenFunc << "}\n";
240 writeIndent(ts, indent + 2);
241 ts << "{blue: " << m_blueFunc << "}\n";
242 writeIndent(ts, indent + 2);
243 ts << "{alpha: " << m_alphaFunc << "}]\n";
244 inputEffect(0)->externalRepresentation(ts, indent + 1);
245 return ts;
246 }
247
248 } // namespace WebCore
249
250 #endif // ENABLE(FILTERS)
251