2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkColorMatrix.h"
|
2014-01-30 18:58:24 +00:00
|
|
|
#include "SkReadBuffer.h"
|
|
|
|
#include "SkWriteBuffer.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#define kRScale 0
|
|
|
|
#define kGScale 6
|
|
|
|
#define kBScale 12
|
|
|
|
#define kAScale 18
|
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
void SkColorMatrix::setIdentity() {
|
2008-12-17 15:59:43 +00:00
|
|
|
memset(fMat, 0, sizeof(fMat));
|
|
|
|
fMat[kRScale] = fMat[kGScale] = fMat[kBScale] = fMat[kAScale] = SK_Scalar1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkColorMatrix::setScale(SkScalar rScale, SkScalar gScale, SkScalar bScale,
|
2011-05-04 00:20:09 +00:00
|
|
|
SkScalar aScale) {
|
2008-12-17 15:59:43 +00:00
|
|
|
memset(fMat, 0, sizeof(fMat));
|
|
|
|
fMat[kRScale] = rScale;
|
|
|
|
fMat[kGScale] = gScale;
|
|
|
|
fMat[kBScale] = bScale;
|
|
|
|
fMat[kAScale] = aScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
void SkColorMatrix::setRotate(Axis axis, SkScalar degrees) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar S, C;
|
|
|
|
|
|
|
|
S = SkScalarSinCos(SkDegreesToRadians(degrees), &C);
|
|
|
|
|
|
|
|
this->setSinCos(axis, S, C);
|
|
|
|
}
|
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
void SkColorMatrix::setSinCos(Axis axis, SkScalar sine, SkScalar cosine) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT((unsigned)axis < 3);
|
|
|
|
|
|
|
|
static const uint8_t gRotateIndex[] = {
|
|
|
|
6, 7, 11, 12,
|
2009-11-10 17:58:47 +00:00
|
|
|
0, 10, 2, 12,
|
2008-12-17 15:59:43 +00:00
|
|
|
0, 1, 5, 6,
|
|
|
|
};
|
|
|
|
const uint8_t* index = gRotateIndex + axis * 4;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
this->setIdentity();
|
|
|
|
fMat[index[0]] = cosine;
|
|
|
|
fMat[index[1]] = sine;
|
|
|
|
fMat[index[2]] = -sine;
|
|
|
|
fMat[index[3]] = cosine;
|
|
|
|
}
|
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
void SkColorMatrix::preRotate(Axis axis, SkScalar degrees) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkColorMatrix tmp;
|
|
|
|
tmp.setRotate(axis, degrees);
|
|
|
|
this->preConcat(tmp);
|
|
|
|
}
|
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
void SkColorMatrix::postRotate(Axis axis, SkScalar degrees) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkColorMatrix tmp;
|
|
|
|
tmp.setRotate(axis, degrees);
|
|
|
|
this->postConcat(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void SkColorMatrix::setConcat(const SkColorMatrix& matA,
|
2011-05-04 00:20:09 +00:00
|
|
|
const SkColorMatrix& matB) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar tmp[20];
|
|
|
|
SkScalar* result = fMat;
|
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
if (&matA == this || &matB == this) {
|
2008-12-17 15:59:43 +00:00
|
|
|
result = tmp;
|
2011-05-04 00:20:09 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
const SkScalar* a = matA.fMat;
|
|
|
|
const SkScalar* b = matB.fMat;
|
|
|
|
|
|
|
|
int index = 0;
|
2011-05-04 00:20:09 +00:00
|
|
|
for (int j = 0; j < 20; j += 5) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2012-08-23 18:09:54 +00:00
|
|
|
result[index++] = SkScalarMul(a[j + 0], b[i + 0]) +
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalarMul(a[j + 1], b[i + 5]) +
|
|
|
|
SkScalarMul(a[j + 2], b[i + 10]) +
|
|
|
|
SkScalarMul(a[j + 3], b[i + 15]);
|
|
|
|
}
|
|
|
|
result[index++] = SkScalarMul(a[j + 0], b[4]) +
|
|
|
|
SkScalarMul(a[j + 1], b[9]) +
|
|
|
|
SkScalarMul(a[j + 2], b[14]) +
|
|
|
|
SkScalarMul(a[j + 3], b[19]) +
|
|
|
|
a[j + 4];
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
if (fMat != result) {
|
2008-12-17 15:59:43 +00:00
|
|
|
memcpy(fMat, result, sizeof(fMat));
|
2011-05-04 00:20:09 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
static void setrow(SkScalar row[], SkScalar r, SkScalar g, SkScalar b) {
|
2008-12-17 15:59:43 +00:00
|
|
|
row[0] = r;
|
|
|
|
row[1] = g;
|
|
|
|
row[2] = b;
|
|
|
|
}
|
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
static const SkScalar kHueR = 0.213f;
|
|
|
|
static const SkScalar kHueG = 0.715f;
|
|
|
|
static const SkScalar kHueB = 0.072f;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
void SkColorMatrix::setSaturation(SkScalar sat) {
|
2008-12-17 15:59:43 +00:00
|
|
|
memset(fMat, 0, sizeof(fMat));
|
|
|
|
|
|
|
|
const SkScalar R = SkScalarMul(kHueR, SK_Scalar1 - sat);
|
|
|
|
const SkScalar G = SkScalarMul(kHueG, SK_Scalar1 - sat);
|
|
|
|
const SkScalar B = SkScalarMul(kHueB, SK_Scalar1 - sat);
|
|
|
|
|
|
|
|
setrow(fMat + 0, R + sat, G, B);
|
|
|
|
setrow(fMat + 5, R, G + sat, B);
|
|
|
|
setrow(fMat + 10, R, G, B + sat);
|
|
|
|
fMat[18] = SK_Scalar1;
|
|
|
|
}
|
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
static const SkScalar kR2Y = 0.299f;
|
|
|
|
static const SkScalar kG2Y = 0.587f;
|
|
|
|
static const SkScalar kB2Y = 0.114f;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
static const SkScalar kR2U = -0.16874f;
|
|
|
|
static const SkScalar kG2U = -0.33126f;
|
|
|
|
static const SkScalar kB2U = 0.5f;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
static const SkScalar kR2V = 0.5f;
|
|
|
|
static const SkScalar kG2V = -0.41869f;
|
|
|
|
static const SkScalar kB2V = -0.08131f;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
void SkColorMatrix::setRGB2YUV() {
|
2008-12-17 15:59:43 +00:00
|
|
|
memset(fMat, 0, sizeof(fMat));
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
setrow(fMat + 0, kR2Y, kG2Y, kB2Y);
|
|
|
|
setrow(fMat + 5, kR2U, kG2U, kB2U);
|
|
|
|
setrow(fMat + 10, kR2V, kG2V, kB2V);
|
|
|
|
fMat[18] = SK_Scalar1;
|
|
|
|
}
|
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
static const SkScalar kV2R = 1.402f;
|
|
|
|
static const SkScalar kU2G = -0.34414f;
|
|
|
|
static const SkScalar kV2G = -0.71414f;
|
|
|
|
static const SkScalar kU2B = 1.772f;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-05-04 00:20:09 +00:00
|
|
|
void SkColorMatrix::setYUV2RGB() {
|
2008-12-17 15:59:43 +00:00
|
|
|
memset(fMat, 0, sizeof(fMat));
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
setrow(fMat + 0, SK_Scalar1, 0, kV2R);
|
|
|
|
setrow(fMat + 5, SK_Scalar1, kU2G, kV2G);
|
|
|
|
setrow(fMat + 10, SK_Scalar1, kU2B, 0);
|
|
|
|
fMat[18] = SK_Scalar1;
|
|
|
|
}
|