1 /*
2 * Copyright 2019 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/core/SkMatrix44.h"
9 #include "src/core/SkYUVMath.h"
10
11 // in SkColorMatrix order (row-major)
12 // Created by running SkColorMatrix_DumpYUVMatrixTables()
13
14 const float Rec709_rgb_to_yuv[] = {
15 0.182586f, 0.614231f, 0.062007f, 0.000000f, 0.062745f,
16 -0.100644f, -0.338572f, 0.439216f, 0.000000f, 0.501961f,
17 0.439216f, -0.398942f, -0.040274f, 0.000000f, 0.501961f,
18 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
19 };
20 const float Rec709_yuv_to_rgb[] = {
21 1.164384f, 0.000000f, 1.792741f, 0.000000f, -0.972945f,
22 1.164384f, -0.213249f, -0.532909f, 0.000000f, 0.301483f,
23 1.164384f, 2.112402f, 0.000000f, 0.000000f, -1.133402f,
24 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
25 };
26 const float Rec601_rgb_to_yuv[] = {
27 0.256788f, 0.504129f, 0.097906f, 0.000000f, 0.062745f,
28 -0.148223f, -0.290993f, 0.439216f, 0.000000f, 0.501961f,
29 0.439216f, -0.367788f, -0.071427f, 0.000000f, 0.501961f,
30 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
31 };
32 const float Rec601_yuv_to_rgb[] = {
33 1.164384f, 0.000000f, 1.596027f, 0.000000f, -0.874202f,
34 1.164384f, -0.391762f, -0.812968f, 0.000000f, 0.531668f,
35 1.164384f, 2.017232f, 0.000000f, 0.000000f, -1.085631f,
36 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
37 };
38 const float JPEG_rgb_to_yuv[] = {
39 0.299000f, 0.587000f, 0.114000f, 0.000000f, 0.000000f,
40 -0.168736f, -0.331264f, 0.500000f, 0.000000f, 0.501961f,
41 0.500000f, -0.418688f, -0.081312f, 0.000000f, 0.501961f,
42 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
43 };
44 const float JPEG_yuv_to_rgb[] = {
45 1.000000f, 0.000000f, 1.402000f, 0.000000f, -0.703749f,
46 1.000000f, -0.344136f, -0.714136f, 0.000000f, 0.531211f,
47 1.000000f, 1.772000f, 0.000000f, 0.000000f, -0.889475f,
48 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
49 };
50
51 static_assert(kJPEG_SkYUVColorSpace == 0, "");
52 static_assert(kRec601_SkYUVColorSpace == 1, "");
53 static_assert(kRec709_SkYUVColorSpace == 2, "");
54
55 const float* yuv_to_rgb_array[] = {
56 JPEG_yuv_to_rgb,
57 Rec601_yuv_to_rgb,
58 Rec709_yuv_to_rgb,
59 };
60
61 const float* rgb_to_yuv_array[] = {
62 JPEG_rgb_to_yuv,
63 Rec601_rgb_to_yuv,
64 Rec709_rgb_to_yuv,
65 };
66
67 constexpr size_t kSizeOfColorMatrix = 20 * sizeof(float);
68
SkColorMatrix_RGB2YUV(SkYUVColorSpace cs,float m[20])69 void SkColorMatrix_RGB2YUV(SkYUVColorSpace cs, float m[20]) {
70 if ((unsigned)cs < (unsigned)kIdentity_SkYUVColorSpace) {
71 memcpy(m, rgb_to_yuv_array[(unsigned)cs], kSizeOfColorMatrix);
72 } else {
73 memset(m, 0, kSizeOfColorMatrix);
74 m[0] = m[6] = m[12] = m[18] = 1;
75 }
76 }
77
SkColorMatrix_YUV2RGB(SkYUVColorSpace cs,float m[20])78 void SkColorMatrix_YUV2RGB(SkYUVColorSpace cs, float m[20]) {
79 if ((unsigned)cs < (unsigned)kIdentity_SkYUVColorSpace) {
80 memcpy(m, yuv_to_rgb_array[(unsigned)cs], kSizeOfColorMatrix);
81 } else {
82 memset(m, 0, kSizeOfColorMatrix);
83 m[0] = m[6] = m[12] = m[18] = 1;
84 }
85 }
86
87 ///////////////////////////////////////////////////////////////////////////////////////////////////
88
89 // we just drop the alpha rol/col from the colormatrix
90 // output is | tr |
91 // | 3x3 tg |
92 // | tb |
93 // | 0 0 0 1 |
colormatrix_to_matrix44(const float src[20],SkMatrix44 * dst)94 static void colormatrix_to_matrix44(const float src[20], SkMatrix44* dst) {
95 for (int r = 0; r < 3; ++r) {
96 for (int c = 0; c < 3; ++c) {
97 dst->set(r, c, src[r*5 + c]);
98 }
99 dst->set(r, 3, src[r*5 + 4]);
100 }
101 dst->set(3, 0, 0);
102 dst->set(3, 1, 0);
103 dst->set(3, 2, 0);
104 dst->set(3, 3, 1);
105 }
106
107 // input: ignore the bottom row
108 // output: inject identity row/column for alpha
matrix44_to_colormatrix(const SkMatrix44 & src,float dst[20])109 static void matrix44_to_colormatrix(const SkMatrix44& src, float dst[20]) {
110 for (int r = 0; r < 3; ++r) {
111 for (int c = 0; c < 3; ++c) {
112 dst[r*5 + c] = src.get(r, c);
113 }
114 dst[r*5 + 3] = 0; // scale alpha
115 dst[r*5 + 4] = src.get(r, 3); // translate
116 }
117 dst[15] = dst[16] = dst[17] = dst[19] = 0;
118 dst[18] = 1;
119 }
120
scale3(float m[],float s)121 static void scale3(float m[], float s) {
122 for (int i = 0; i < 3; ++i) {
123 m[i] *= s;
124 }
125 }
126
127 namespace {
128 struct YUVCoeff {
129 float Kr, Kb;
130 float Cr, Cb;
131 float scaleY, addY;
132 float scaleUV;
133 };
134 } // namespace
135
136 const YUVCoeff gCoeff[] = {
137 // kJPEG_SkYUVColorSpace
138 { 0.299f, 0.114f, 1/1.772f, 1/1.402f, 1, 0, 1, },
139
140 // kRec601_SkYUVColorSpace
141 { 0.299f, 0.114f, 1/1.772f, 1/1.402f, 219/255.f, 16/255.f, 224/255.f, },
142
143 // kRec709_SkYUVColorSpace
144 { 0.2126f, 0.0722f, 1/1.8556f, 1/1.5748f, 219/255.f, 16/255.f, 224/255.f, },
145 };
146
make_rgb_to_yuv_matrix(float mx[20],const YUVCoeff & c)147 static void make_rgb_to_yuv_matrix(float mx[20], const YUVCoeff& c) {
148 const float Kr = c.Kr;
149 const float Kb = c.Kb;
150 const float Kg = 1.0f - Kr - Kb;
151
152 float m[20] = {
153 Kr, Kg, Kb, 0, c.addY,
154 -Kr, -Kg, 1-Kb, 0, 128/255.f,
155 1-Kr, -Kg, -Kb, 0, 128/255.f,
156 0, 0, 0, 1, 0,
157 };
158 memcpy(mx, m, sizeof(m));
159 scale3(mx + 0, c.scaleY);
160 scale3(mx + 5, c.Cr * c.scaleUV);
161 scale3(mx + 10, c.Cb * c.scaleUV);
162 }
163
dump(const float m[20],SkYUVColorSpace cs,bool rgb2yuv)164 static void dump(const float m[20], SkYUVColorSpace cs, bool rgb2yuv) {
165 const char* names[] = {
166 "JPEG", "Rec601", "Rec709",
167 };
168 const char* dirnames[] = {
169 "yuv_to_rgb", "rgb_to_yuv",
170 };
171 SkDebugf("const float %s_%s[] = {\n", names[cs], dirnames[rgb2yuv]);
172 for (int i = 0; i < 4; ++i) {
173 SkDebugf(" ");
174 for (int j = 0; j < 5; ++j) {
175 SkDebugf(" %9.6ff,", m[i * 5 + j]);
176 }
177 SkDebugf("\n");
178 }
179 SkDebugf("};\n");
180 }
181
182 // Used to create the prebuilt tables for each colorspace.
183 // Don't remove this function, in case we want to recompute those tables in the future.
SkColorMatrix_DumpYUVMatrixTables()184 void SkColorMatrix_DumpYUVMatrixTables() {
185 for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace}) {
186 float m[20];
187 make_rgb_to_yuv_matrix(m, gCoeff[(unsigned)cs]);
188 dump(m, cs, true);
189 SkMatrix44 m44, im44;
190 colormatrix_to_matrix44(m, &m44);
191 float im[20];
192 #ifdef SK_DEBUG
193 // be sure our coversion between matrix44 and colormatrix is perfect
194 matrix44_to_colormatrix(m44, im);
195 SkASSERT(memcmp(m, im, sizeof(im)) == 0);
196 #endif
197 SkAssertResult(m44.invert(&im44));
198 matrix44_to_colormatrix(im44, im);
199 dump(im, cs, false);
200 }
201 }
202