| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org> | 2 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org> |
| 3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org> | 3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org> |
| 4 * Copyright (C) 2005 Eric Seidel <eric@webkit.org> | 4 * Copyright (C) 2005 Eric Seidel <eric@webkit.org> |
| 5 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> | 5 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> |
| 6 * Copyright (C) 2013 Google Inc. All rights reserved. | 6 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 7 * | 7 * |
| 8 * This library is free software; you can redistribute it and/or | 8 * This library is free software; you can redistribute it and/or |
| 9 * modify it under the terms of the GNU Library General Public | 9 * modify it under the terms of the GNU Library General Public |
| 10 * License as published by the Free Software Foundation; either | 10 * License as published by the Free Software Foundation; either |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "config.h" | 24 #include "config.h" |
| 25 #include "platform/graphics/filters/FEColorMatrix.h" | 25 #include "platform/graphics/filters/FEColorMatrix.h" |
| 26 | 26 |
| 27 #include "SkColorFilterImageFilter.h" | 27 #include "SkColorFilterImageFilter.h" |
| 28 #include "SkColorMatrixFilter.h" | 28 #include "SkColorMatrixFilter.h" |
| 29 #include "platform/graphics/filters/SkiaImageFilterBuilder.h" | 29 #include "platform/graphics/filters/SkiaImageFilterBuilder.h" |
| 30 #include "platform/text/TextStream.h" | 30 #include "platform/text/TextStream.h" |
| 31 | 31 |
| 32 namespace blink { | 32 namespace blink { |
| 33 | 33 |
| 34 static const unsigned kColorMatrixSize = 20; |
| 35 |
| 34 FEColorMatrix::FEColorMatrix(Filter* filter, ColorMatrixType type, const Vector<
float>& values) | 36 FEColorMatrix::FEColorMatrix(Filter* filter, ColorMatrixType type, const Vector<
float>& values) |
| 35 : FilterEffect(filter) | 37 : FilterEffect(filter) |
| 36 , m_type(type) | 38 , m_type(type) |
| 37 , m_values(values) | 39 , m_values(values) |
| 38 { | 40 { |
| 39 } | 41 } |
| 40 | 42 |
| 41 PassRefPtrWillBeRawPtr<FEColorMatrix> FEColorMatrix::create(Filter* filter, Colo
rMatrixType type, const Vector<float>& values) | 43 PassRefPtrWillBeRawPtr<FEColorMatrix> FEColorMatrix::create(Filter* filter, Colo
rMatrixType type, const Vector<float>& values) |
| 42 { | 44 { |
| 43 return adoptRefWillBeNoop(new FEColorMatrix(filter, type, values)); | 45 return adoptRefWillBeNoop(new FEColorMatrix(filter, type, values)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 56 return true; | 58 return true; |
| 57 } | 59 } |
| 58 | 60 |
| 59 const Vector<float>& FEColorMatrix::values() const | 61 const Vector<float>& FEColorMatrix::values() const |
| 60 { | 62 { |
| 61 return m_values; | 63 return m_values; |
| 62 } | 64 } |
| 63 | 65 |
| 64 bool FEColorMatrix::setValues(const Vector<float> &values) | 66 bool FEColorMatrix::setValues(const Vector<float> &values) |
| 65 { | 67 { |
| 66 ASSERT(values.size() == 20); | |
| 67 if (m_values == values) | 68 if (m_values == values) |
| 68 return false; | 69 return false; |
| 69 m_values = values; | 70 m_values = values; |
| 70 return true; | 71 return true; |
| 71 } | 72 } |
| 72 | 73 |
| 73 static void saturateMatrix(float s, SkScalar matrix[20]) | 74 static void saturateMatrix(float s, SkScalar matrix[kColorMatrixSize]) |
| 74 { | 75 { |
| 75 matrix[0] = 0.213f + 0.787f * s; | 76 matrix[0] = 0.213f + 0.787f * s; |
| 76 matrix[1] = 0.715f - 0.715f * s; | 77 matrix[1] = 0.715f - 0.715f * s; |
| 77 matrix[2] = 0.072f - 0.072f * s; | 78 matrix[2] = 0.072f - 0.072f * s; |
| 78 matrix[3] = matrix[4] = 0; | 79 matrix[3] = matrix[4] = 0; |
| 79 matrix[5] = 0.213f - 0.213f * s; | 80 matrix[5] = 0.213f - 0.213f * s; |
| 80 matrix[6] = 0.715f + 0.285f * s; | 81 matrix[6] = 0.715f + 0.285f * s; |
| 81 matrix[7] = 0.072f - 0.072f * s; | 82 matrix[7] = 0.072f - 0.072f * s; |
| 82 matrix[8] = matrix[9] = 0; | 83 matrix[8] = matrix[9] = 0; |
| 83 matrix[10] = 0.213f - 0.213f * s; | 84 matrix[10] = 0.213f - 0.213f * s; |
| 84 matrix[11] = 0.715f - 0.715f * s; | 85 matrix[11] = 0.715f - 0.715f * s; |
| 85 matrix[12] = 0.072f + 0.928f * s; | 86 matrix[12] = 0.072f + 0.928f * s; |
| 86 matrix[13] = matrix[14] = 0; | 87 matrix[13] = matrix[14] = 0; |
| 87 matrix[15] = matrix[16] = matrix[17] = 0; | 88 matrix[15] = matrix[16] = matrix[17] = 0; |
| 88 matrix[18] = 1; | 89 matrix[18] = 1; |
| 89 matrix[19] = 0; | 90 matrix[19] = 0; |
| 90 } | 91 } |
| 91 | 92 |
| 92 static void hueRotateMatrix(float hue, SkScalar matrix[20]) | 93 static void hueRotateMatrix(float hue, SkScalar matrix[kColorMatrixSize]) |
| 93 { | 94 { |
| 94 float cosHue = cosf(hue * piFloat / 180); | 95 float cosHue = cosf(hue * piFloat / 180); |
| 95 float sinHue = sinf(hue * piFloat / 180); | 96 float sinHue = sinf(hue * piFloat / 180); |
| 96 matrix[0] = 0.213f + cosHue * 0.787f - sinHue * 0.213f; | 97 matrix[0] = 0.213f + cosHue * 0.787f - sinHue * 0.213f; |
| 97 matrix[1] = 0.715f - cosHue * 0.715f - sinHue * 0.715f; | 98 matrix[1] = 0.715f - cosHue * 0.715f - sinHue * 0.715f; |
| 98 matrix[2] = 0.072f - cosHue * 0.072f + sinHue * 0.928f; | 99 matrix[2] = 0.072f - cosHue * 0.072f + sinHue * 0.928f; |
| 99 matrix[3] = matrix[4] = 0; | 100 matrix[3] = matrix[4] = 0; |
| 100 matrix[5] = 0.213f - cosHue * 0.213f + sinHue * 0.143f; | 101 matrix[5] = 0.213f - cosHue * 0.213f + sinHue * 0.143f; |
| 101 matrix[6] = 0.715f + cosHue * 0.285f + sinHue * 0.140f; | 102 matrix[6] = 0.715f + cosHue * 0.285f + sinHue * 0.140f; |
| 102 matrix[7] = 0.072f - cosHue * 0.072f - sinHue * 0.283f; | 103 matrix[7] = 0.072f - cosHue * 0.072f - sinHue * 0.283f; |
| 103 matrix[8] = matrix[9] = 0; | 104 matrix[8] = matrix[9] = 0; |
| 104 matrix[10] = 0.213f - cosHue * 0.213f - sinHue * 0.787f; | 105 matrix[10] = 0.213f - cosHue * 0.213f - sinHue * 0.787f; |
| 105 matrix[11] = 0.715f - cosHue * 0.715f + sinHue * 0.715f; | 106 matrix[11] = 0.715f - cosHue * 0.715f + sinHue * 0.715f; |
| 106 matrix[12] = 0.072f + cosHue * 0.928f + sinHue * 0.072f; | 107 matrix[12] = 0.072f + cosHue * 0.928f + sinHue * 0.072f; |
| 107 matrix[13] = matrix[14] = 0; | 108 matrix[13] = matrix[14] = 0; |
| 108 matrix[15] = matrix[16] = matrix[17] = 0; | 109 matrix[15] = matrix[16] = matrix[17] = 0; |
| 109 matrix[18] = 1; | 110 matrix[18] = 1; |
| 110 matrix[19] = 0; | 111 matrix[19] = 0; |
| 111 } | 112 } |
| 112 | 113 |
| 113 static void luminanceToAlphaMatrix(SkScalar matrix[20]) | 114 static void luminanceToAlphaMatrix(SkScalar matrix[kColorMatrixSize]) |
| 114 { | 115 { |
| 115 memset(matrix, 0, 20 * sizeof(SkScalar)); | 116 memset(matrix, 0, kColorMatrixSize * sizeof(SkScalar)); |
| 116 matrix[15] = 0.2125f; | 117 matrix[15] = 0.2125f; |
| 117 matrix[16] = 0.7154f; | 118 matrix[16] = 0.7154f; |
| 118 matrix[17] = 0.0721f; | 119 matrix[17] = 0.0721f; |
| 119 } | 120 } |
| 120 | 121 |
| 121 static SkColorFilter* createColorFilter(ColorMatrixType type, const float* value
s) | 122 static SkColorFilter* createColorFilter(ColorMatrixType type, const Vector<float
>& values) |
| 122 { | 123 { |
| 123 SkScalar matrix[20]; | 124 // Use defaults if values contains too few values. See SVGFEColorMatrixEleme
nt::build |
| 125 SkScalar matrix[kColorMatrixSize]; |
| 126 memset(matrix, 0, kColorMatrixSize * sizeof(SkScalar)); |
| 127 matrix[0] = matrix[6] = matrix[12] = matrix[18] = 1; |
| 128 |
| 124 switch (type) { | 129 switch (type) { |
| 125 case FECOLORMATRIX_TYPE_UNKNOWN: | 130 case FECOLORMATRIX_TYPE_UNKNOWN: |
| 126 break; | 131 break; |
| 127 case FECOLORMATRIX_TYPE_MATRIX: | 132 case FECOLORMATRIX_TYPE_MATRIX: |
| 128 for (int i = 0; i < 20; ++i) | 133 if (values.size() >= kColorMatrixSize) { |
| 129 matrix[i] = values[i]; | 134 for (unsigned i = 0; i < kColorMatrixSize; ++i) |
| 130 | 135 matrix[i] = values[i]; |
| 136 } |
| 131 matrix[4] *= SkScalar(255); | 137 matrix[4] *= SkScalar(255); |
| 132 matrix[9] *= SkScalar(255); | 138 matrix[9] *= SkScalar(255); |
| 133 matrix[14] *= SkScalar(255); | 139 matrix[14] *= SkScalar(255); |
| 134 matrix[19] *= SkScalar(255); | 140 matrix[19] *= SkScalar(255); |
| 135 break; | 141 break; |
| 136 case FECOLORMATRIX_TYPE_SATURATE: | 142 case FECOLORMATRIX_TYPE_SATURATE: |
| 137 saturateMatrix(values[0], matrix); | 143 if (values.size()) |
| 144 saturateMatrix(values[0], matrix); |
| 138 break; | 145 break; |
| 139 case FECOLORMATRIX_TYPE_HUEROTATE: | 146 case FECOLORMATRIX_TYPE_HUEROTATE: |
| 140 hueRotateMatrix(values[0], matrix); | 147 if (values.size()) |
| 148 hueRotateMatrix(values[0], matrix); |
| 141 break; | 149 break; |
| 142 case FECOLORMATRIX_TYPE_LUMINANCETOALPHA: | 150 case FECOLORMATRIX_TYPE_LUMINANCETOALPHA: |
| 143 luminanceToAlphaMatrix(matrix); | 151 luminanceToAlphaMatrix(matrix); |
| 144 break; | 152 break; |
| 145 } | 153 } |
| 146 return SkColorMatrixFilter::Create(matrix); | 154 return SkColorMatrixFilter::Create(matrix); |
| 147 } | 155 } |
| 148 | 156 |
| 149 bool FEColorMatrix::affectsTransparentPixels() | 157 bool FEColorMatrix::affectsTransparentPixels() |
| 150 { | 158 { |
| 151 // Because the input pixels are premultiplied, the only way clear pixels can
be | 159 // Because the input pixels are premultiplied, the only way clear pixels can
be |
| 152 // painted is if the additive component for the alpha is not 0. | 160 // painted is if the additive component for the alpha is not 0. |
| 153 return m_type == FECOLORMATRIX_TYPE_MATRIX && m_values[19] > 0; | 161 return m_type == FECOLORMATRIX_TYPE_MATRIX && m_values.size() >= kColorMatri
xSize && m_values[19] > 0; |
| 154 } | 162 } |
| 155 | 163 |
| 156 PassRefPtr<SkImageFilter> FEColorMatrix::createImageFilter(SkiaImageFilterBuilde
r* builder) | 164 PassRefPtr<SkImageFilter> FEColorMatrix::createImageFilter(SkiaImageFilterBuilde
r* builder) |
| 157 { | 165 { |
| 158 RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpa
ce())); | 166 RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpa
ce())); |
| 159 SkAutoTUnref<SkColorFilter> filter(createColorFilter(m_type, m_values.data()
)); | 167 SkAutoTUnref<SkColorFilter> filter(createColorFilter(m_type, m_values)); |
| 160 SkImageFilter::CropRect rect = getCropRect(builder->cropOffset()); | 168 SkImageFilter::CropRect rect = getCropRect(builder->cropOffset()); |
| 161 return adoptRef(SkColorFilterImageFilter::Create(filter, input.get(), &rect)
); | 169 return adoptRef(SkColorFilterImageFilter::Create(filter, input.get(), &rect)
); |
| 162 } | 170 } |
| 163 | 171 |
| 164 static TextStream& operator<<(TextStream& ts, const ColorMatrixType& type) | 172 static TextStream& operator<<(TextStream& ts, const ColorMatrixType& type) |
| 165 { | 173 { |
| 166 switch (type) { | 174 switch (type) { |
| 167 case FECOLORMATRIX_TYPE_UNKNOWN: | 175 case FECOLORMATRIX_TYPE_UNKNOWN: |
| 168 ts << "UNKNOWN"; | 176 ts << "UNKNOWN"; |
| 169 break; | 177 break; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 200 ts << " "; | 208 ts << " "; |
| 201 } | 209 } |
| 202 ts << "\""; | 210 ts << "\""; |
| 203 } | 211 } |
| 204 ts << "]\n"; | 212 ts << "]\n"; |
| 205 inputEffect(0)->externalRepresentation(ts, indent + 1); | 213 inputEffect(0)->externalRepresentation(ts, indent + 1); |
| 206 return ts; | 214 return ts; |
| 207 } | 215 } |
| 208 | 216 |
| 209 } // namespace blink | 217 } // namespace blink |
| OLD | NEW |