epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkColorMatrix.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 9 | #include "SkFlattenableBuffers.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | |
| 11 | #define kRScale 0 |
| 12 | #define kGScale 6 |
| 13 | #define kBScale 12 |
| 14 | #define kAScale 18 |
| 15 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 16 | void SkColorMatrix::setIdentity() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | memset(fMat, 0, sizeof(fMat)); |
| 18 | fMat[kRScale] = fMat[kGScale] = fMat[kBScale] = fMat[kAScale] = SK_Scalar1; |
| 19 | } |
| 20 | |
| 21 | void SkColorMatrix::setScale(SkScalar rScale, SkScalar gScale, SkScalar bScale, |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 22 | SkScalar aScale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | 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.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 32 | void SkColorMatrix::setRotate(Axis axis, SkScalar degrees) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | SkScalar S, C; |
| 34 | |
| 35 | S = SkScalarSinCos(SkDegreesToRadians(degrees), &C); |
| 36 | |
| 37 | this->setSinCos(axis, S, C); |
| 38 | } |
| 39 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 40 | void SkColorMatrix::setSinCos(Axis axis, SkScalar sine, SkScalar cosine) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | SkASSERT((unsigned)axis < 3); |
| 42 | |
| 43 | static const uint8_t gRotateIndex[] = { |
| 44 | 6, 7, 11, 12, |
reed@android.com | 1d15d37 | 2009-11-10 17:58:47 +0000 | [diff] [blame] | 45 | 0, 10, 2, 12, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | 0, 1, 5, 6, |
| 47 | }; |
| 48 | const uint8_t* index = gRotateIndex + axis * 4; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 49 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | 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.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 57 | void SkColorMatrix::preRotate(Axis axis, SkScalar degrees) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | SkColorMatrix tmp; |
| 59 | tmp.setRotate(axis, degrees); |
| 60 | this->preConcat(tmp); |
| 61 | } |
| 62 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 63 | void SkColorMatrix::postRotate(Axis axis, SkScalar degrees) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | SkColorMatrix tmp; |
| 65 | tmp.setRotate(axis, degrees); |
| 66 | this->postConcat(tmp); |
| 67 | } |
| 68 | |
| 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | |
| 71 | void SkColorMatrix::setConcat(const SkColorMatrix& matA, |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 72 | const SkColorMatrix& matB) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | SkScalar tmp[20]; |
| 74 | SkScalar* result = fMat; |
| 75 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 76 | if (&matA == this || &matB == this) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | result = tmp; |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 78 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 79 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | const SkScalar* a = matA.fMat; |
| 81 | const SkScalar* b = matB.fMat; |
| 82 | |
| 83 | int index = 0; |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 84 | for (int j = 0; j < 20; j += 5) { |
| 85 | for (int i = 0; i < 4; i++) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 86 | result[index++] = SkScalarMul(a[j + 0], b[i + 0]) + |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 97 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 98 | if (fMat != result) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | memcpy(fMat, result, sizeof(fMat)); |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 100 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 105 | static void setrow(SkScalar row[], SkScalar r, SkScalar g, SkScalar b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | row[0] = r; |
| 107 | row[1] = g; |
| 108 | row[2] = b; |
| 109 | } |
| 110 | |
| 111 | static const SkScalar kHueR = SkFloatToScalar(0.213f); |
| 112 | static const SkScalar kHueG = SkFloatToScalar(0.715f); |
| 113 | static const SkScalar kHueB = SkFloatToScalar(0.072f); |
| 114 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 115 | void SkColorMatrix::setSaturation(SkScalar sat) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | 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 | |
| 128 | static const SkScalar kR2Y = SkFloatToScalar(0.299f); |
| 129 | static const SkScalar kG2Y = SkFloatToScalar(0.587f); |
| 130 | static const SkScalar kB2Y = SkFloatToScalar(0.114f); |
| 131 | |
| 132 | static const SkScalar kR2U = SkFloatToScalar(-0.16874f); |
| 133 | static const SkScalar kG2U = SkFloatToScalar(-0.33126f); |
| 134 | static const SkScalar kB2U = SkFloatToScalar(0.5f); |
| 135 | |
| 136 | static const SkScalar kR2V = SkFloatToScalar(0.5f); |
| 137 | static const SkScalar kG2V = SkFloatToScalar(-0.41869f); |
| 138 | static const SkScalar kB2V = SkFloatToScalar(-0.08131f); |
| 139 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 140 | void SkColorMatrix::setRGB2YUV() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | memset(fMat, 0, sizeof(fMat)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 142 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | 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 | |
| 149 | static const SkScalar kV2R = SkFloatToScalar(1.402f); |
| 150 | static const SkScalar kU2G = SkFloatToScalar(-0.34414f); |
| 151 | static const SkScalar kV2G = SkFloatToScalar(-0.71414f); |
| 152 | static const SkScalar kU2B = SkFloatToScalar(1.772f); |
| 153 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 154 | void SkColorMatrix::setYUV2RGB() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | memset(fMat, 0, sizeof(fMat)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 156 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | 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 | } |