• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <math.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 // This utility computes values needed to generate yuvconstants based on
17 // white point values.
18 // The yuv formulas are tuned for 8 bit YUV channels.
19 
20 // See Also
21 // https://mymusing.co/bt601-yuv-to-rgb-conversion-color/
22 
23 // BT.709 full range YUV to RGB reference
24 //  R = Y               + V * 1.5748
25 //  G = Y - U * 0.18732 - V * 0.46812
26 //  B = Y + U * 1.8556
27 //  KR = 0.2126
28 //  KB = 0.0722
29 
30 // // Y contribution to R,G,B.  Scale and bias.
31 // #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
32 // #define YB 32    /* 64 / 2 */
33 //
34 // // U and V contributions to R,G,B.
35 // #define UB 113 /* round(1.77200 * 64) */
36 // #define UG 22  /* round(0.34414 * 64) */
37 // #define VG 46  /* round(0.71414 * 64) */
38 // #define VR 90  /* round(1.40200 * 64) */
39 //
40 // // Bias values to round, and subtract 128 from U and V.
41 // #define BB (-UB * 128 + YB)
42 // #define BG (UG * 128 + VG * 128 + YB)
43 // #define BR (-VR * 128 + YB)
44 
main(int argc,const char * argv[])45 int main(int argc, const char* argv[]) {
46   if (argc < 2) {
47     printf("yuvconstants Kr Kb\n");
48     printf("  MC BT          KR = 0.2126; KB = 0.0722\n");
49     printf("  1  BT.709      KR = 0.2126; KB = 0.0722\n");
50     printf("  4  FCC         KR = 0.30;   KB = 0.11\n");
51     printf("  6  BT.601      KR = 0.299;  KB = 0.114\n");
52     printf("  7  SMPTE 240M  KR = 0.212;  KB = 0.087\n");
53     printf("  9  BT.2020     KR = 0.2627; KB = 0.0593\n");
54     return -1;
55   }
56   float kr = atof(argv[1]);
57   float kb = atof(argv[2]);
58   float kg = 1 - kr - kb;
59 
60   float vr = 2 * (1 - kr);
61   float ug = 2 * ((1 - kb) * kb / kg);
62   float vg = 2 * ((1 - kr) * kr / kg);
63   float ub = 2 * (1 - kb);
64 
65   printf("Full range\n");
66   printf("R = Y                + V * %5f\n", vr);
67   printf("G = Y - U * %6f - V * %6f\n", ug, vg);
68   printf("B = Y + U * %5f\n", ub);
69 
70   printf("KR = %4f; ", kr);
71   printf("KB = %4f\n", kb);
72   // printf("KG = %4f\n", kg);
73   // #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
74   // #define YB 32    /* 64 / 2 */
75   //
76   // // U and V contributions to R,G,B.
77 
78   printf("UB %-3.0f /* round(%f * 64 = %8.4f) */\n", round(ub * 64), ub, ub * 64);
79   printf("UG %-3.0f /* round(%f * 64 = %8.4f) */\n", round(ug * 64), ug, ug * 64);
80   printf("VG %-3.0f /* round(%f * 64 = %8.4f) */\n", round(vg * 64), vg, vg * 64);
81   printf("VR %-3.0f /* round(%f * 64 = %8.4f) */\n", round(vr * 64), vr, vr * 64);
82 
83   vr = 255.f / 224.f * 2 * (1 - kr);
84   ug = 255.f / 224.f * 2 * ((1 - kb) * kb / kg);
85   vg = 255.f / 224.f * 2 * ((1 - kr) * kr / kg);
86   ub = 255.f / 224.f * 2 * (1 - kb);
87 
88   printf("\nLimited range\n");
89   printf("R = (Y - 16) * 1.164                + V * %5f\n", vr);
90   printf("G = (Y - 16) * 1.164 - U * %6f - V * %6f\n", ug, vg);
91   printf("B = (Y - 16) * 1.164 + U * %5f\n", ub);
92 
93   // printf("KG = %4f\n", kg);
94   // #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
95   // #define YB 32    /* 64 / 2 */
96   //
97   // // U and V contributions to R,G,B.
98 
99   printf("UB %-3.0f /* round(%f * 64 = %8.4f) */\n", round(ub * 64), ub, ub * 64);
100   printf("UG %-3.0f /* round(%f * 64 = %8.4f) */\n", round(ug * 64), ug, ug * 64);
101   printf("VG %-3.0f /* round(%f * 64 = %8.4f) */\n", round(vg * 64), vg, vg * 64);
102   printf("VR %-3.0f /* round(%f * 64 = %8.4f) */\n", round(vr * 64), vr, vr * 64);
103 
104   return 0;
105 }
106