blob: d6cb9405fc6f3af8ae399bed4138255b5eee4996 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkColorMatrix.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00009#include "SkFlattenableBuffers.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010
11#define kRScale 0
12#define kGScale 6
13#define kBScale 12
14#define kAScale 18
15
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000016void SkColorMatrix::setIdentity() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000017 memset(fMat, 0, sizeof(fMat));
18 fMat[kRScale] = fMat[kGScale] = fMat[kBScale] = fMat[kAScale] = SK_Scalar1;
19}
20
21void SkColorMatrix::setScale(SkScalar rScale, SkScalar gScale, SkScalar bScale,
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000022 SkScalar aScale) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 memset(fMat, 0, sizeof(fMat));
24 fMat[kRScale] = rScale;
25 fMat[kGScale] = gScale;
26 fMat[kBScale] = bScale;
27 fMat[kAScale] = aScale;
28}
29
30///////////////////////////////////////////////////////////////////////////////
31
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000032void SkColorMatrix::setRotate(Axis axis, SkScalar degrees) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 SkScalar S, C;
34
35 S = SkScalarSinCos(SkDegreesToRadians(degrees), &C);
36
37 this->setSinCos(axis, S, C);
38}
39
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000040void SkColorMatrix::setSinCos(Axis axis, SkScalar sine, SkScalar cosine) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 SkASSERT((unsigned)axis < 3);
42
43 static const uint8_t gRotateIndex[] = {
44 6, 7, 11, 12,
reed@android.com1d15d372009-11-10 17:58:47 +000045 0, 10, 2, 12,
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 0, 1, 5, 6,
47 };
48 const uint8_t* index = gRotateIndex + axis * 4;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 this->setIdentity();
51 fMat[index[0]] = cosine;
52 fMat[index[1]] = sine;
53 fMat[index[2]] = -sine;
54 fMat[index[3]] = cosine;
55}
56
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000057void SkColorMatrix::preRotate(Axis axis, SkScalar degrees) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 SkColorMatrix tmp;
59 tmp.setRotate(axis, degrees);
60 this->preConcat(tmp);
61}
62
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000063void SkColorMatrix::postRotate(Axis axis, SkScalar degrees) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 SkColorMatrix tmp;
65 tmp.setRotate(axis, degrees);
66 this->postConcat(tmp);
67}
68
69///////////////////////////////////////////////////////////////////////////////
70
71void SkColorMatrix::setConcat(const SkColorMatrix& matA,
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000072 const SkColorMatrix& matB) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 SkScalar tmp[20];
74 SkScalar* result = fMat;
75
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000076 if (&matA == this || &matB == this) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 result = tmp;
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000078 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000079
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 const SkScalar* a = matA.fMat;
81 const SkScalar* b = matB.fMat;
82
83 int index = 0;
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000084 for (int j = 0; j < 20; j += 5) {
85 for (int i = 0; i < 4; i++) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086 result[index++] = SkScalarMul(a[j + 0], b[i + 0]) +
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 SkScalarMul(a[j + 1], b[i + 5]) +
88 SkScalarMul(a[j + 2], b[i + 10]) +
89 SkScalarMul(a[j + 3], b[i + 15]);
90 }
91 result[index++] = SkScalarMul(a[j + 0], b[4]) +
92 SkScalarMul(a[j + 1], b[9]) +
93 SkScalarMul(a[j + 2], b[14]) +
94 SkScalarMul(a[j + 3], b[19]) +
95 a[j + 4];
96 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000097
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000098 if (fMat != result) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 memcpy(fMat, result, sizeof(fMat));
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +0000100 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101}
102
103///////////////////////////////////////////////////////////////////////////////
104
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +0000105static void setrow(SkScalar row[], SkScalar r, SkScalar g, SkScalar b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 row[0] = r;
107 row[1] = g;
108 row[2] = b;
109}
110
111static const SkScalar kHueR = SkFloatToScalar(0.213f);
112static const SkScalar kHueG = SkFloatToScalar(0.715f);
113static const SkScalar kHueB = SkFloatToScalar(0.072f);
114
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +0000115void SkColorMatrix::setSaturation(SkScalar sat) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 memset(fMat, 0, sizeof(fMat));
117
118 const SkScalar R = SkScalarMul(kHueR, SK_Scalar1 - sat);
119 const SkScalar G = SkScalarMul(kHueG, SK_Scalar1 - sat);
120 const SkScalar B = SkScalarMul(kHueB, SK_Scalar1 - sat);
121
122 setrow(fMat + 0, R + sat, G, B);
123 setrow(fMat + 5, R, G + sat, B);
124 setrow(fMat + 10, R, G, B + sat);
125 fMat[18] = SK_Scalar1;
126}
127
128static const SkScalar kR2Y = SkFloatToScalar(0.299f);
129static const SkScalar kG2Y = SkFloatToScalar(0.587f);
130static const SkScalar kB2Y = SkFloatToScalar(0.114f);
131
132static const SkScalar kR2U = SkFloatToScalar(-0.16874f);
133static const SkScalar kG2U = SkFloatToScalar(-0.33126f);
134static const SkScalar kB2U = SkFloatToScalar(0.5f);
135
136static const SkScalar kR2V = SkFloatToScalar(0.5f);
137static const SkScalar kG2V = SkFloatToScalar(-0.41869f);
138static const SkScalar kB2V = SkFloatToScalar(-0.08131f);
139
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +0000140void SkColorMatrix::setRGB2YUV() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 memset(fMat, 0, sizeof(fMat));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 setrow(fMat + 0, kR2Y, kG2Y, kB2Y);
144 setrow(fMat + 5, kR2U, kG2U, kB2U);
145 setrow(fMat + 10, kR2V, kG2V, kB2V);
146 fMat[18] = SK_Scalar1;
147}
148
149static const SkScalar kV2R = SkFloatToScalar(1.402f);
150static const SkScalar kU2G = SkFloatToScalar(-0.34414f);
151static const SkScalar kV2G = SkFloatToScalar(-0.71414f);
152static const SkScalar kU2B = SkFloatToScalar(1.772f);
153
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +0000154void SkColorMatrix::setYUV2RGB() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 memset(fMat, 0, sizeof(fMat));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000156
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 setrow(fMat + 0, SK_Scalar1, 0, kV2R);
158 setrow(fMat + 5, SK_Scalar1, kU2G, kV2G);
159 setrow(fMat + 10, SK_Scalar1, kU2B, 0);
160 fMat[18] = SK_Scalar1;
161}