1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/effects/SkColorMatrix.h"
9 #include "include/private/SkFloatingPoint.h"
10
11 enum {
12 kR_Scale = 0,
13 kG_Scale = 6,
14 kB_Scale = 12,
15 kA_Scale = 18,
16
17 kR_Trans = 4,
18 kG_Trans = 9,
19 kB_Trans = 14,
20 kA_Trans = 19,
21 };
22
set_concat(float result[20],const float outer[20],const float inner[20])23 static void set_concat(float result[20], const float outer[20], const float inner[20]) {
24 float tmp[20];
25 float* target;
26
27 if (outer == result || inner == result) {
28 target = tmp; // will memcpy answer when we're done into result
29 } else {
30 target = result;
31 }
32
33 int index = 0;
34 for (int j = 0; j < 20; j += 5) {
35 for (int i = 0; i < 4; i++) {
36 target[index++] = outer[j + 0] * inner[i + 0] +
37 outer[j + 1] * inner[i + 5] +
38 outer[j + 2] * inner[i + 10] +
39 outer[j + 3] * inner[i + 15];
40 }
41 target[index++] = outer[j + 0] * inner[4] +
42 outer[j + 1] * inner[9] +
43 outer[j + 2] * inner[14] +
44 outer[j + 3] * inner[19] +
45 outer[j + 4];
46 }
47
48 if (target != result) {
49 memcpy(result, target, 20 * sizeof(float));
50 }
51 }
52
setIdentity()53 void SkColorMatrix::setIdentity() {
54 memset(fMat, 0, sizeof(fMat));
55 fMat[kR_Scale] = fMat[kG_Scale] = fMat[kB_Scale] = fMat[kA_Scale] = 1;
56 }
57
setScale(float rScale,float gScale,float bScale,float aScale)58 void SkColorMatrix::setScale(float rScale, float gScale, float bScale, float aScale) {
59 memset(fMat, 0, sizeof(fMat));
60 fMat[kR_Scale] = rScale;
61 fMat[kG_Scale] = gScale;
62 fMat[kB_Scale] = bScale;
63 fMat[kA_Scale] = aScale;
64 }
65
postTranslate(float dr,float dg,float db,float da)66 void SkColorMatrix::postTranslate(float dr, float dg, float db, float da) {
67 fMat[kR_Trans] += dr;
68 fMat[kG_Trans] += dg;
69 fMat[kB_Trans] += db;
70 fMat[kA_Trans] += da;
71 }
72
73 ///////////////////////////////////////////////////////////////////////////////
74
setRotate(Axis axis,float degrees)75 void SkColorMatrix::setRotate(Axis axis, float degrees) {
76 float r = sk_float_degrees_to_radians(degrees);
77 this->setSinCos(axis, sk_float_sin(r), sk_float_cos(r));
78 }
79
setSinCos(Axis axis,float sine,float cosine)80 void SkColorMatrix::setSinCos(Axis axis, float sine, float cosine) {
81 SkASSERT((unsigned)axis < 3);
82
83 static const uint8_t gRotateIndex[] = {
84 6, 7, 11, 12,
85 0, 10, 2, 12,
86 0, 1, 5, 6,
87 };
88 const uint8_t* index = gRotateIndex + axis * 4;
89
90 this->setIdentity();
91 fMat[index[0]] = cosine;
92 fMat[index[1]] = sine;
93 fMat[index[2]] = -sine;
94 fMat[index[3]] = cosine;
95 }
96
preRotate(Axis axis,float degrees)97 void SkColorMatrix::preRotate(Axis axis, float degrees) {
98 SkColorMatrix tmp;
99 tmp.setRotate(axis, degrees);
100 this->preConcat(tmp);
101 }
102
postRotate(Axis axis,float degrees)103 void SkColorMatrix::postRotate(Axis axis, float degrees) {
104 SkColorMatrix tmp;
105 tmp.setRotate(axis, degrees);
106 this->postConcat(tmp);
107 }
108
setConcat(const SkColorMatrix & matA,const SkColorMatrix & matB)109 void SkColorMatrix::setConcat(const SkColorMatrix& matA, const SkColorMatrix& matB) {
110 set_concat(fMat, matA.fMat, matB.fMat);
111 }
112
113 ///////////////////////////////////////////////////////////////////////////////
114
setrow(float row[],float r,float g,float b)115 static void setrow(float row[], float r, float g, float b) {
116 row[0] = r;
117 row[1] = g;
118 row[2] = b;
119 }
120
121 static const float kHueR = 0.213f;
122 static const float kHueG = 0.715f;
123 static const float kHueB = 0.072f;
124
setSaturation(float sat)125 void SkColorMatrix::setSaturation(float sat) {
126 memset(fMat, 0, sizeof(fMat));
127
128 const float R = kHueR * (1 - sat);
129 const float G = kHueG * (1 - sat);
130 const float B = kHueB * (1 - sat);
131
132 setrow(fMat + 0, R + sat, G, B);
133 setrow(fMat + 5, R, G + sat, B);
134 setrow(fMat + 10, R, G, B + sat);
135 fMat[kA_Scale] = 1;
136 }
137
138 static const float kR2Y = 0.299f;
139 static const float kG2Y = 0.587f;
140 static const float kB2Y = 0.114f;
141
142 static const float kR2U = -0.16874f;
143 static const float kG2U = -0.33126f;
144 static const float kB2U = 0.5f;
145
146 static const float kR2V = 0.5f;
147 static const float kG2V = -0.41869f;
148 static const float kB2V = -0.08131f;
149
setRGB2YUV()150 void SkColorMatrix::setRGB2YUV() {
151 memset(fMat, 0, sizeof(fMat));
152
153 setrow(fMat + 0, kR2Y, kG2Y, kB2Y);
154 setrow(fMat + 5, kR2U, kG2U, kB2U);
155 setrow(fMat + 10, kR2V, kG2V, kB2V);
156 fMat[kA_Scale] = 1;
157 }
158
159 static const float kV2R = 1.402f;
160 static const float kU2G = -0.34414f;
161 static const float kV2G = -0.71414f;
162 static const float kU2B = 1.772f;
163
setYUV2RGB()164 void SkColorMatrix::setYUV2RGB() {
165 memset(fMat, 0, sizeof(fMat));
166
167 setrow(fMat + 0, 1, 0, kV2R);
168 setrow(fMat + 5, 1, kU2G, kV2G);
169 setrow(fMat + 10, 1, kU2B, 0);
170 fMat[kA_Scale] = 1;
171 }
172