summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/filters/fe_color_matrix.cc
blob: aa24b6a28ab8cd57a8735a83defa5d0625331370 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
 * Copyright (C) 2005 Eric Seidel <eric@webkit.org>
 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "third_party/blink/renderer/platform/graphics/filters/fe_color_matrix.h"

#include "third_party/blink/renderer/platform/graphics/filters/paint_filter_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/text_stream.h"
#include "third_party/skia/include/effects/SkColorFilterImageFilter.h"
#include "third_party/skia/include/effects/SkColorMatrixFilter.h"

namespace blink {

static const unsigned kColorMatrixSize = 20;

FEColorMatrix::FEColorMatrix(Filter* filter,
                             ColorMatrixType type,
                             Vector<float> values)
    : FilterEffect(filter), type_(type), values_(std::move(values)) {}

ColorMatrixType FEColorMatrix::GetType() const {
  return type_;
}

bool FEColorMatrix::SetType(ColorMatrixType type) {
  if (type_ == type)
    return false;
  type_ = type;
  return true;
}

const Vector<float>& FEColorMatrix::Values() const {
  return values_;
}

bool FEColorMatrix::SetValues(Vector<float> values) {
  if (values_ == values)
    return false;
  values_ = std::move(values);
  return true;
}

static void SaturateMatrix(float s, float matrix[kColorMatrixSize]) {
  matrix[0] = 0.213f + 0.787f * s;
  matrix[1] = 0.715f - 0.715f * s;
  matrix[2] = 0.072f - 0.072f * s;
  matrix[3] = matrix[4] = 0;
  matrix[5] = 0.213f - 0.213f * s;
  matrix[6] = 0.715f + 0.285f * s;
  matrix[7] = 0.072f - 0.072f * s;
  matrix[8] = matrix[9] = 0;
  matrix[10] = 0.213f - 0.213f * s;
  matrix[11] = 0.715f - 0.715f * s;
  matrix[12] = 0.072f + 0.928f * s;
  matrix[13] = matrix[14] = 0;
  matrix[15] = matrix[16] = matrix[17] = 0;
  matrix[18] = 1;
  matrix[19] = 0;
}

static void HueRotateMatrix(float hue, float matrix[kColorMatrixSize]) {
  float cos_hue = cosf(hue * kPiFloat / 180);
  float sin_hue = sinf(hue * kPiFloat / 180);
  matrix[0] = 0.213f + cos_hue * 0.787f - sin_hue * 0.213f;
  matrix[1] = 0.715f - cos_hue * 0.715f - sin_hue * 0.715f;
  matrix[2] = 0.072f - cos_hue * 0.072f + sin_hue * 0.928f;
  matrix[3] = matrix[4] = 0;
  matrix[5] = 0.213f - cos_hue * 0.213f + sin_hue * 0.143f;
  matrix[6] = 0.715f + cos_hue * 0.285f + sin_hue * 0.140f;
  matrix[7] = 0.072f - cos_hue * 0.072f - sin_hue * 0.283f;
  matrix[8] = matrix[9] = 0;
  matrix[10] = 0.213f - cos_hue * 0.213f - sin_hue * 0.787f;
  matrix[11] = 0.715f - cos_hue * 0.715f + sin_hue * 0.715f;
  matrix[12] = 0.072f + cos_hue * 0.928f + sin_hue * 0.072f;
  matrix[13] = matrix[14] = 0;
  matrix[15] = matrix[16] = matrix[17] = 0;
  matrix[18] = 1;
  matrix[19] = 0;
}

static void LuminanceToAlphaMatrix(float matrix[kColorMatrixSize]) {
  memset(matrix, 0, kColorMatrixSize * sizeof(float));
  matrix[15] = 0.2125f;
  matrix[16] = 0.7154f;
  matrix[17] = 0.0721f;
}

static sk_sp<SkColorFilter> CreateColorFilter(ColorMatrixType type,
                                              const Vector<float>& values) {
  // Use defaults if values contains too few/many values.
  float matrix[kColorMatrixSize];
  memset(matrix, 0, kColorMatrixSize * sizeof(float));
  matrix[0] = matrix[6] = matrix[12] = matrix[18] = 1;

  switch (type) {
    case FECOLORMATRIX_TYPE_UNKNOWN:
      break;
    case FECOLORMATRIX_TYPE_MATRIX:
      if (values.size() == kColorMatrixSize) {
        for (unsigned i = 0; i < kColorMatrixSize; ++i)
          matrix[i] = values[i];
      }
      break;
    case FECOLORMATRIX_TYPE_SATURATE:
      if (values.size() == 1)
        SaturateMatrix(values[0], matrix);
      break;
    case FECOLORMATRIX_TYPE_HUEROTATE:
      if (values.size() == 1)
        HueRotateMatrix(values[0], matrix);
      break;
    case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
      LuminanceToAlphaMatrix(matrix);
      break;
  }
  return SkColorFilters::Matrix(matrix);
}

bool FEColorMatrix::AffectsTransparentPixels() const {
  // Because the input pixels are premultiplied, the only way clear pixels can
  // be painted is if the additive component for the alpha is not 0.
  return type_ == FECOLORMATRIX_TYPE_MATRIX &&
         values_.size() >= kColorMatrixSize && values_[19] > 0;
}

sk_sp<PaintFilter> FEColorMatrix::CreateImageFilter() {
  sk_sp<PaintFilter> input(paint_filter_builder::Build(
      InputEffect(0), OperatingInterpolationSpace()));
  sk_sp<SkColorFilter> filter = CreateColorFilter(type_, values_);
  PaintFilter::CropRect rect = GetCropRect();
  return sk_make_sp<ColorFilterPaintFilter>(std::move(filter), std::move(input),
                                            &rect);
}

static WTF::TextStream& operator<<(WTF::TextStream& ts,
                                   const ColorMatrixType& type) {
  switch (type) {
    case FECOLORMATRIX_TYPE_UNKNOWN:
      ts << "UNKNOWN";
      break;
    case FECOLORMATRIX_TYPE_MATRIX:
      ts << "MATRIX";
      break;
    case FECOLORMATRIX_TYPE_SATURATE:
      ts << "SATURATE";
      break;
    case FECOLORMATRIX_TYPE_HUEROTATE:
      ts << "HUEROTATE";
      break;
    case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
      ts << "LUMINANCETOALPHA";
      break;
  }
  return ts;
}

static bool ValuesIsValidForType(ColorMatrixType type,
                                 const Vector<float>& values) {
  switch (type) {
    case FECOLORMATRIX_TYPE_MATRIX:
      return values.size() == kColorMatrixSize;
    case FECOLORMATRIX_TYPE_SATURATE:
    case FECOLORMATRIX_TYPE_HUEROTATE:
      return values.size() == 1;
    case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
      return values.size() == 0;
    case FECOLORMATRIX_TYPE_UNKNOWN:
      break;
  }
  NOTREACHED();
  return false;
}

WTF::TextStream& FEColorMatrix::ExternalRepresentation(WTF::TextStream& ts,
                                                       int indent) const {
  WriteIndent(ts, indent);
  ts << "[feColorMatrix";
  FilterEffect::ExternalRepresentation(ts);
  ts << " type=\"" << type_ << "\"";
  if (!values_.IsEmpty() && ValuesIsValidForType(type_, values_)) {
    ts << " values=\"";
    Vector<float>::const_iterator ptr = values_.begin();
    const Vector<float>::const_iterator end = values_.end();
    while (ptr < end) {
      ts << *ptr;
      ++ptr;
      if (ptr < end)
        ts << " ";
    }
    ts << "\"";
  }
  ts << "]\n";
  InputEffect(0)->ExternalRepresentation(ts, indent + 1);
  return ts;
}

}  // namespace blink