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) 2013 Google Inc. 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 #include "platform/graphics/filters/FEColorMatrix.h"
26
27 #include "SkColorFilterImageFilter.h"
28 #include "SkColorMatrixFilter.h"
29 #include "platform/graphics/GraphicsContext.h"
30 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
31 #include "platform/graphics/skia/NativeImageSkia.h"
32 #include "platform/text/TextStream.h"
33 #include "wtf/MathExtras.h"
34 #include "wtf/Uint8ClampedArray.h"
35
36 namespace WebCore {
37
FEColorMatrix(Filter * filter,ColorMatrixType type,const Vector<float> & values)38 FEColorMatrix::FEColorMatrix(Filter* filter, ColorMatrixType type, const Vector<float>& values)
39 : FilterEffect(filter)
40 , m_type(type)
41 , m_values(values)
42 {
43 }
44
create(Filter * filter,ColorMatrixType type,const Vector<float> & values)45 PassRefPtr<FEColorMatrix> FEColorMatrix::create(Filter* filter, ColorMatrixType type, const Vector<float>& values)
46 {
47 return adoptRef(new FEColorMatrix(filter, type, values));
48 }
49
type() const50 ColorMatrixType FEColorMatrix::type() const
51 {
52 return m_type;
53 }
54
setType(ColorMatrixType type)55 bool FEColorMatrix::setType(ColorMatrixType type)
56 {
57 if (m_type == type)
58 return false;
59 m_type = type;
60 return true;
61 }
62
values() const63 const Vector<float>& FEColorMatrix::values() const
64 {
65 return m_values;
66 }
67
setValues(const Vector<float> & values)68 bool FEColorMatrix::setValues(const Vector<float> &values)
69 {
70 if (m_values == values)
71 return false;
72 m_values = values;
73 return true;
74 }
75
matrix(float & red,float & green,float & blue,float & alpha,const Vector<float> & values)76 inline void matrix(float& red, float& green, float& blue, float& alpha, const Vector<float>& values)
77 {
78 float r = values[0] * red + values[1] * green + values[2] * blue + values[3] * alpha + values[4] * 255;
79 float g = values[5] * red + values[6] * green + values[7] * blue + values[8] * alpha + values[9] * 255;
80 float b = values[10] * red + values[11] * green + values[12] * blue + values[13] * alpha + values[14] * 255;
81 float a = values[15] * red + values[16] * green + values[17] * blue + values[18] * alpha + values[19] * 255;
82
83 red = r;
84 green = g;
85 blue = b;
86 alpha = a;
87 }
88
saturateAndHueRotate(float & red,float & green,float & blue,const float * components)89 inline void saturateAndHueRotate(float& red, float& green, float& blue, const float* components)
90 {
91 float r = red * components[0] + green * components[1] + blue * components[2];
92 float g = red * components[3] + green * components[4] + blue * components[5];
93 float b = red * components[6] + green * components[7] + blue * components[8];
94
95 red = r;
96 green = g;
97 blue = b;
98 }
99
luminance(float & red,float & green,float & blue,float & alpha)100 inline void luminance(float& red, float& green, float& blue, float& alpha)
101 {
102 alpha = 0.2125 * red + 0.7154 * green + 0.0721 * blue;
103 red = 0;
104 green = 0;
105 blue = 0;
106 }
107
108 template<ColorMatrixType filterType>
effectType(Uint8ClampedArray * pixelArray,const Vector<float> & values)109 void effectType(Uint8ClampedArray* pixelArray, const Vector<float>& values)
110 {
111 unsigned pixelArrayLength = pixelArray->length();
112 float components[9];
113
114 if (filterType == FECOLORMATRIX_TYPE_SATURATE)
115 FEColorMatrix::calculateSaturateComponents(components, values[0]);
116 else if (filterType == FECOLORMATRIX_TYPE_HUEROTATE)
117 FEColorMatrix::calculateHueRotateComponents(components, values[0]);
118
119 for (unsigned pixelByteOffset = 0; pixelByteOffset < pixelArrayLength; pixelByteOffset += 4) {
120 float red = pixelArray->item(pixelByteOffset);
121 float green = pixelArray->item(pixelByteOffset + 1);
122 float blue = pixelArray->item(pixelByteOffset + 2);
123 float alpha = pixelArray->item(pixelByteOffset + 3);
124
125 switch (filterType) {
126 case FECOLORMATRIX_TYPE_MATRIX:
127 matrix(red, green, blue, alpha, values);
128 break;
129 case FECOLORMATRIX_TYPE_SATURATE:
130 case FECOLORMATRIX_TYPE_HUEROTATE:
131 saturateAndHueRotate(red, green, blue, components);
132 break;
133 case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
134 luminance(red, green, blue, alpha);
135 break;
136 }
137
138 pixelArray->set(pixelByteOffset, red);
139 pixelArray->set(pixelByteOffset + 1, green);
140 pixelArray->set(pixelByteOffset + 2, blue);
141 pixelArray->set(pixelByteOffset + 3, alpha);
142 }
143 }
144
applySoftware()145 void FEColorMatrix::applySoftware()
146 {
147 FilterEffect* in = inputEffect(0);
148
149 ImageBuffer* resultImage = createImageBufferResult();
150 if (!resultImage)
151 return;
152
153 resultImage->context()->drawImageBuffer(in->asImageBuffer(), drawingRegionOfInputImage(in->absolutePaintRect()));
154
155 IntRect imageRect(IntPoint(), absolutePaintRect().size());
156 RefPtr<Uint8ClampedArray> pixelArray = resultImage->getUnmultipliedImageData(imageRect);
157
158 switch (m_type) {
159 case FECOLORMATRIX_TYPE_UNKNOWN:
160 break;
161 case FECOLORMATRIX_TYPE_MATRIX:
162 effectType<FECOLORMATRIX_TYPE_MATRIX>(pixelArray.get(), m_values);
163 break;
164 case FECOLORMATRIX_TYPE_SATURATE:
165 effectType<FECOLORMATRIX_TYPE_SATURATE>(pixelArray.get(), m_values);
166 break;
167 case FECOLORMATRIX_TYPE_HUEROTATE:
168 effectType<FECOLORMATRIX_TYPE_HUEROTATE>(pixelArray.get(), m_values);
169 break;
170 case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
171 effectType<FECOLORMATRIX_TYPE_LUMINANCETOALPHA>(pixelArray.get(), m_values);
172 setIsAlphaImage(true);
173 break;
174 }
175
176 resultImage->putByteArray(Unmultiplied, pixelArray.get(), imageRect.size(), imageRect, IntPoint());
177 }
178
saturateMatrix(float s,SkScalar matrix[20])179 static void saturateMatrix(float s, SkScalar matrix[20])
180 {
181 matrix[0] = 0.213f + 0.787f * s;
182 matrix[1] = 0.715f - 0.715f * s;
183 matrix[2] = 0.072f - 0.072f * s;
184 matrix[3] = matrix[4] = 0;
185 matrix[5] = 0.213f - 0.213f * s;
186 matrix[6] = 0.715f + 0.285f * s;
187 matrix[7] = 0.072f - 0.072f * s;
188 matrix[8] = matrix[9] = 0;
189 matrix[10] = 0.213f - 0.213f * s;
190 matrix[11] = 0.715f - 0.715f * s;
191 matrix[12] = 0.072f + 0.928f * s;
192 matrix[13] = matrix[14] = 0;
193 matrix[15] = matrix[16] = matrix[17] = 0;
194 matrix[18] = 1;
195 matrix[19] = 0;
196 }
197
hueRotateMatrix(float hue,SkScalar matrix[20])198 static void hueRotateMatrix(float hue, SkScalar matrix[20])
199 {
200 float cosHue = cosf(hue * piFloat / 180);
201 float sinHue = sinf(hue * piFloat / 180);
202 matrix[0] = 0.213f + cosHue * 0.787f - sinHue * 0.213f;
203 matrix[1] = 0.715f - cosHue * 0.715f - sinHue * 0.715f;
204 matrix[2] = 0.072f - cosHue * 0.072f + sinHue * 0.928f;
205 matrix[3] = matrix[4] = 0;
206 matrix[5] = 0.213f - cosHue * 0.213f + sinHue * 0.143f;
207 matrix[6] = 0.715f + cosHue * 0.285f + sinHue * 0.140f;
208 matrix[7] = 0.072f - cosHue * 0.072f - sinHue * 0.283f;
209 matrix[8] = matrix[9] = 0;
210 matrix[10] = 0.213f - cosHue * 0.213f - sinHue * 0.787f;
211 matrix[11] = 0.715f - cosHue * 0.715f + sinHue * 0.715f;
212 matrix[12] = 0.072f + cosHue * 0.928f + sinHue * 0.072f;
213 matrix[13] = matrix[14] = 0;
214 matrix[15] = matrix[16] = matrix[17] = 0;
215 matrix[18] = 1;
216 matrix[19] = 0;
217 }
218
luminanceToAlphaMatrix(SkScalar matrix[20])219 static void luminanceToAlphaMatrix(SkScalar matrix[20])
220 {
221 memset(matrix, 0, 20 * sizeof(SkScalar));
222 matrix[15] = 0.2125f;
223 matrix[16] = 0.7154f;
224 matrix[17] = 0.0721f;
225 }
226
createColorFilter(ColorMatrixType type,const float * values)227 static SkColorFilter* createColorFilter(ColorMatrixType type, const float* values)
228 {
229 SkScalar matrix[20];
230 switch (type) {
231 case FECOLORMATRIX_TYPE_UNKNOWN:
232 break;
233 case FECOLORMATRIX_TYPE_MATRIX:
234 for (int i = 0; i < 20; ++i)
235 matrix[i] = values[i];
236
237 matrix[4] *= SkScalar(255);
238 matrix[9] *= SkScalar(255);
239 matrix[14] *= SkScalar(255);
240 matrix[19] *= SkScalar(255);
241 break;
242 case FECOLORMATRIX_TYPE_SATURATE:
243 saturateMatrix(values[0], matrix);
244 break;
245 case FECOLORMATRIX_TYPE_HUEROTATE:
246 hueRotateMatrix(values[0], matrix);
247 break;
248 case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
249 luminanceToAlphaMatrix(matrix);
250 break;
251 }
252 return new SkColorMatrixFilter(matrix);
253 }
254
applySkia()255 bool FEColorMatrix::applySkia()
256 {
257 ImageBuffer* resultImage = createImageBufferResult();
258 if (!resultImage)
259 return false;
260
261 FilterEffect* in = inputEffect(0);
262
263 SkRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
264
265 SkAutoTUnref<SkColorFilter> filter(createColorFilter(m_type, m_values.data()));
266
267 RefPtr<Image> image = in->asImageBuffer()->copyImage(DontCopyBackingStore);
268 RefPtr<NativeImageSkia> nativeImage = image->nativeImageForCurrentFrame();
269 if (!nativeImage)
270 return false;
271
272 SkPaint paint;
273 paint.setColorFilter(filter);
274 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
275 resultImage->context()->drawBitmap(nativeImage->bitmap(), drawingRegion.fLeft, drawingRegion.fTop, &paint);
276 return true;
277 }
278
createImageFilter(SkiaImageFilterBuilder * builder)279 PassRefPtr<SkImageFilter> FEColorMatrix::createImageFilter(SkiaImageFilterBuilder* builder)
280 {
281 RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
282 SkAutoTUnref<SkColorFilter> filter(createColorFilter(m_type, m_values.data()));
283 SkImageFilter::CropRect rect = getCropRect(builder->cropOffset());
284 return adoptRef(SkColorFilterImageFilter::Create(filter, input.get(), &rect));
285 }
286
operator <<(TextStream & ts,const ColorMatrixType & type)287 static TextStream& operator<<(TextStream& ts, const ColorMatrixType& type)
288 {
289 switch (type) {
290 case FECOLORMATRIX_TYPE_UNKNOWN:
291 ts << "UNKNOWN";
292 break;
293 case FECOLORMATRIX_TYPE_MATRIX:
294 ts << "MATRIX";
295 break;
296 case FECOLORMATRIX_TYPE_SATURATE:
297 ts << "SATURATE";
298 break;
299 case FECOLORMATRIX_TYPE_HUEROTATE:
300 ts << "HUEROTATE";
301 break;
302 case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
303 ts << "LUMINANCETOALPHA";
304 break;
305 }
306 return ts;
307 }
308
externalRepresentation(TextStream & ts,int indent) const309 TextStream& FEColorMatrix::externalRepresentation(TextStream& ts, int indent) const
310 {
311 writeIndent(ts, indent);
312 ts << "[feColorMatrix";
313 FilterEffect::externalRepresentation(ts);
314 ts << " type=\"" << m_type << "\"";
315 if (!m_values.isEmpty()) {
316 ts << " values=\"";
317 Vector<float>::const_iterator ptr = m_values.begin();
318 const Vector<float>::const_iterator end = m_values.end();
319 while (ptr < end) {
320 ts << *ptr;
321 ++ptr;
322 if (ptr < end)
323 ts << " ";
324 }
325 ts << "\"";
326 }
327 ts << "]\n";
328 inputEffect(0)->externalRepresentation(ts, indent + 1);
329 return ts;
330 }
331
332 } // namespace WebCore
333