Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / filters / FEColorMatrix.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) 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/filters/SkiaImageFilterBuilder.h"
30 #include "platform/text/TextStream.h"
31
32 namespace blink {
33
34 FEColorMatrix::FEColorMatrix(Filter* filter, ColorMatrixType type, const Vector<float>& values)
35     : FilterEffect(filter)
36     , m_type(type)
37     , m_values(values)
38 {
39 }
40
41 PassRefPtr<FEColorMatrix> FEColorMatrix::create(Filter* filter, ColorMatrixType type, const Vector<float>& values)
42 {
43     return adoptRef(new FEColorMatrix(filter, type, values));
44 }
45
46 ColorMatrixType FEColorMatrix::type() const
47 {
48     return m_type;
49 }
50
51 bool FEColorMatrix::setType(ColorMatrixType type)
52 {
53     if (m_type == type)
54         return false;
55     m_type = type;
56     return true;
57 }
58
59 const Vector<float>& FEColorMatrix::values() const
60 {
61     return m_values;
62 }
63
64 bool FEColorMatrix::setValues(const Vector<float> &values)
65 {
66     if (m_values == values)
67         return false;
68     m_values = values;
69     return true;
70 }
71
72 static void saturateMatrix(float s, SkScalar matrix[20])
73 {
74     matrix[0] = 0.213f + 0.787f * s;
75     matrix[1] = 0.715f - 0.715f * s;
76     matrix[2] = 0.072f - 0.072f * s;
77     matrix[3] = matrix[4] = 0;
78     matrix[5] = 0.213f - 0.213f * s;
79     matrix[6] = 0.715f + 0.285f * s;
80     matrix[7] = 0.072f - 0.072f * s;
81     matrix[8] = matrix[9] = 0;
82     matrix[10] = 0.213f - 0.213f * s;
83     matrix[11] = 0.715f - 0.715f * s;
84     matrix[12] = 0.072f + 0.928f * s;
85     matrix[13] = matrix[14] = 0;
86     matrix[15] = matrix[16] = matrix[17] = 0;
87     matrix[18] = 1;
88     matrix[19] = 0;
89 }
90
91 static void hueRotateMatrix(float hue, SkScalar matrix[20])
92 {
93     float cosHue = cosf(hue * piFloat / 180);
94     float sinHue = sinf(hue * piFloat / 180);
95     matrix[0] = 0.213f + cosHue * 0.787f - sinHue * 0.213f;
96     matrix[1] = 0.715f - cosHue * 0.715f - sinHue * 0.715f;
97     matrix[2] = 0.072f - cosHue * 0.072f + sinHue * 0.928f;
98     matrix[3] = matrix[4] = 0;
99     matrix[5] = 0.213f - cosHue * 0.213f + sinHue * 0.143f;
100     matrix[6] = 0.715f + cosHue * 0.285f + sinHue * 0.140f;
101     matrix[7] = 0.072f - cosHue * 0.072f - sinHue * 0.283f;
102     matrix[8] = matrix[9] = 0;
103     matrix[10] = 0.213f - cosHue * 0.213f - sinHue * 0.787f;
104     matrix[11] = 0.715f - cosHue * 0.715f + sinHue * 0.715f;
105     matrix[12] = 0.072f + cosHue * 0.928f + sinHue * 0.072f;
106     matrix[13] = matrix[14] = 0;
107     matrix[15] = matrix[16] = matrix[17] = 0;
108     matrix[18] = 1;
109     matrix[19] = 0;
110 }
111
112 static void luminanceToAlphaMatrix(SkScalar matrix[20])
113 {
114     memset(matrix, 0, 20 * sizeof(SkScalar));
115     matrix[15] = 0.2125f;
116     matrix[16] = 0.7154f;
117     matrix[17] = 0.0721f;
118 }
119
120 static SkColorFilter* createColorFilter(ColorMatrixType type, const float* values)
121 {
122     SkScalar matrix[20];
123     switch (type) {
124     case FECOLORMATRIX_TYPE_UNKNOWN:
125         break;
126     case FECOLORMATRIX_TYPE_MATRIX:
127         for (int i = 0; i < 20; ++i)
128             matrix[i] = values[i];
129
130         matrix[4] *= SkScalar(255);
131         matrix[9] *= SkScalar(255);
132         matrix[14] *= SkScalar(255);
133         matrix[19] *= SkScalar(255);
134         break;
135     case FECOLORMATRIX_TYPE_SATURATE:
136         saturateMatrix(values[0], matrix);
137         break;
138     case FECOLORMATRIX_TYPE_HUEROTATE:
139         hueRotateMatrix(values[0], matrix);
140         break;
141     case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
142         luminanceToAlphaMatrix(matrix);
143         break;
144     }
145     return SkColorMatrixFilter::Create(matrix);
146 }
147
148 bool FEColorMatrix::affectsTransparentPixels()
149 {
150     // Because the input pixels are premultiplied, the only way clear pixels can be
151     // painted is if the additive component for the alpha is not 0.
152     return m_type == FECOLORMATRIX_TYPE_MATRIX && m_values[19] > 0;
153 }
154
155 PassRefPtr<SkImageFilter> FEColorMatrix::createImageFilter(SkiaImageFilterBuilder* builder)
156 {
157     RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
158     SkAutoTUnref<SkColorFilter> filter(createColorFilter(m_type, m_values.data()));
159     SkImageFilter::CropRect rect = getCropRect(builder->cropOffset());
160     return adoptRef(SkColorFilterImageFilter::Create(filter, input.get(), &rect));
161 }
162
163 static TextStream& operator<<(TextStream& ts, const ColorMatrixType& type)
164 {
165     switch (type) {
166     case FECOLORMATRIX_TYPE_UNKNOWN:
167         ts << "UNKNOWN";
168         break;
169     case FECOLORMATRIX_TYPE_MATRIX:
170         ts << "MATRIX";
171         break;
172     case FECOLORMATRIX_TYPE_SATURATE:
173         ts << "SATURATE";
174         break;
175     case FECOLORMATRIX_TYPE_HUEROTATE:
176         ts << "HUEROTATE";
177         break;
178     case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
179         ts << "LUMINANCETOALPHA";
180         break;
181     }
182     return ts;
183 }
184
185 TextStream& FEColorMatrix::externalRepresentation(TextStream& ts, int indent) const
186 {
187     writeIndent(ts, indent);
188     ts << "[feColorMatrix";
189     FilterEffect::externalRepresentation(ts);
190     ts << " type=\"" << m_type << "\"";
191     if (!m_values.isEmpty()) {
192         ts << " values=\"";
193         Vector<float>::const_iterator ptr = m_values.begin();
194         const Vector<float>::const_iterator end = m_values.end();
195         while (ptr < end) {
196             ts << *ptr;
197             ++ptr;
198             if (ptr < end)
199                 ts << " ";
200         }
201         ts << "\"";
202     }
203     ts << "]\n";
204     inputEffect(0)->externalRepresentation(ts, indent + 1);
205     return ts;
206 }
207
208 } // namespace blink