1 /*
2 * Copyright 2021 The LibYuv Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 // This utility computes values needed to generate yuvconstants based on
16 // white point values.
17 // The yuv formulas are tuned for 8 bit YUV channels.
18
19 // For those MCs that can be represented as kr and kb:
20 // Full range
21 // float M[3][3]
22 // {{1,0,2*(1-kr)},{1,-((2*kb)/((2-kb)*(1-kb-kr))),-((2*kr)/((2-kr)*(1-kb-kr)))},{1,2*(1-kb),0}};
23 // float B[3]
24 // {1+(256*(1-kr))/255,1-(256*kb)/(255*(2-kb)*(1-kb-kr))-(256*kr)/(255*(2-kr)*(1-kb-kr)),1+(256*(1-kb))/255};
25 // Limited range
26 // float M[3][3]
27 // {{85/73,0,255/112-(255*kr)/112},{85/73,-((255*kb)/(112*(2-kb)*(1-kb-kr))),-((255*kr)/(112*(2-kr)*(1-kb-kr)))},{85/73,255/112-(255*kb)/112,0}};
28 // float B[3]
29 // {77662/43435-(1537*kr)/1785,203/219-(1537*kb)/(1785*(2-kb)*(1-kb-kr))-(1537*kr)/(1785*(2-kr)*(1-kb-kr)),77662/43435-(1537*kb)/1785};
30
31 // mc bt
32 // 1 bt.709 KR = 0.2126; KB = 0.0722
33 // 4 fcc KR = 0.30; KB = 0.11
34 // 6 bt.601 KR = 0.299; KB = 0.114
35 // 7 SMPTE 240M KR = 0.212; KB = 0.087
36 // 10 bt2020 KR = 0.2627; KB = 0.0593
37
38 // BT.709 full range YUV to RGB reference
39 // R = Y + V * 1.5748
40 // G = Y - U * 0.18732 - V * 0.46812
41 // B = Y + U * 1.8556
42 // KR = 0.2126
43 // KB = 0.0722
44
45 // https://mymusing.co/bt601-yuv-to-rgb-conversion-color/
46
47 // // Y contribution to R,G,B. Scale and bias.
48 // #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
49 // #define YB 32 /* 64 / 2 */
50 //
51 // // U and V contributions to R,G,B.
52 // #define UB 113 /* round(1.77200 * 64) */
53 // #define UG 22 /* round(0.34414 * 64) */
54 // #define VG 46 /* round(0.71414 * 64) */
55 // #define VR 90 /* round(1.40200 * 64) */
56 //
57 // // Bias values to round, and subtract 128 from U and V.
58 // #define BB (-UB * 128 + YB)
59 // #define BG (UG * 128 + VG * 128 + YB)
60 // #define BR (-VR * 128 + YB)
61
round(float v)62 int round(float v) {
63 return (int)(v + 0.5);
64 }
65
main(int argc,const char * argv[])66 int main(int argc, const char* argv[]) {
67 if (argc < 2) {
68 printf("color kr kb\n");
69 return -1;
70 }
71 float kr = atof(argv[1]);
72 float kb = atof(argv[2]);
73 float kg = 1 - kr - kb;
74
75 float vr = 2 * (1 - kr);
76 float ug = 2 * ((1 - kb) * kb / kg);
77 float vg = 2 * ((1 - kr) * kr / kg);
78 float ub = 2 * (1 - kb);
79
80 printf("Full range\n");
81 printf("R = Y + V * %5f\n", vr);
82 printf("G = Y - U * %6f - V * %6f\n", ug, vg);
83 printf("B = Y + U * %5f\n", ub);
84
85 printf("KR = %4f; ", kr);
86 printf("KB = %4f\n", kb);
87 // printf("KG = %4f\n", kg);
88 // #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
89 // #define YB 32 /* 64 / 2 */
90 //
91 // // U and V contributions to R,G,B.
92
93 printf("UB %-3d /* round(%f * 64) */\n", round(ub * 64), ub);
94 printf("UG %-3d /* round(%f * 64) */\n", round(ug * 64), ug);
95 printf("VG %-3d /* round(%f * 64) */\n", round(vg * 64), vg);
96 printf("VR %-3d /* round(%f * 64) */\n", round(vr * 64), vr);
97
98 vr = 255.f / 224.f * 2 * (1 - kr);
99 ug = 255.f / 224.f * 2 * ((1 - kb) * kb / kg);
100 vg = 255.f / 224.f * 2 * ((1 - kr) * kr / kg);
101 ub = 255.f / 224.f * 2 * (1 - kb);
102
103 printf("Limited range\n");
104 printf("R = (Y - 16) * 1.164 + V * %5f\n", vr);
105 printf("G = (Y - 16) * 1.164 - U * %6f - V * %6f\n", ug, vg);
106 printf("B = (Y - 16) * 1.164 + U * %5f\n", ub);
107
108 // printf("KG = %4f\n", kg);
109 // #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
110 // #define YB 32 /* 64 / 2 */
111 //
112 // // U and V contributions to R,G,B.
113
114 printf("UB %-3d /* round(%f * 64) */\n", round(ub * 64), ub);
115 printf("UG %-3d /* round(%f * 64) */\n", round(ug * 64), ug);
116 printf("VG %-3d /* round(%f * 64) */\n", round(vg * 64), vg);
117 printf("VR %-3d /* round(%f * 64) */\n", round(vr * 64), vr);
118
119 return 0;
120 }
121