-
Notifications
You must be signed in to change notification settings - Fork 7
/
color_lib.h
277 lines (225 loc) · 6.97 KB
/
color_lib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#pragma once
// Mini-library of color conversion functions for older broadcast standards
// Copyright Jamie Dickson, 2020.
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
// Use POSIX M_PI if available
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// RGB Coefficients per specification are fixed precision
// Fun trivia: CO_R + CO_G + CO_B == 1
#define CO_R_601 0.299
#define CO_B_601 0.114
#define CO_G_601 (1.0 - CO_R_601 - CO_B_601)
#define CO_R_709 0.2126
#define CO_B_709 0.0722
#define CO_G_709 (1.0 - CO_R_709 - CO_B_709)
#define CO_R_2020 0.2627
#define CO_B_2020 0.0593
#define CO_G_2020 (1.0 - CO_R_2020 - CO_B_2020)
typedef enum color_rec {
REC_601 = 0,
REC_709 = 1,
REC_2020 = 2,
} COLOR_REC_T;
struct color_coeff {
double r;
double g;
double b;
double u_max;
double v_max;
};
// Max and Min values as defined in BT 601 for Chroma. Luma may vary for NTSC-J.
#define MAX_IRE 130.8333
#define MIN_IRE 23.3025
// U V reduction factors for NTSC video
#define U_REDUCTION 0.492111
#define V_REDUCTION 0.877283
void pop_rec_const(COLOR_REC_T rec, struct color_coeff *co)
{
memset(co, 0, sizeof(struct color_coeff));
switch(rec) {
case REC_709:
co->r = CO_R_709;
co->g = CO_G_709;
co->b = CO_B_709;
case REC_2020:
co->r = CO_R_2020;
co->g = CO_G_2020;
co->b = CO_B_2020;
default: // 601 as default
co->r = CO_R_601;
co->g = CO_G_601;
co->b = CO_B_601;
}
co->u_max = 0.436;//(co->g + co->r);
co->v_max = 0.615;//(co->g + co->b);
}
double ire_to_mv(double ire)
{
return (ire * 7.14);
}
double mv_to_ire(double mv)
{
return (mv / 7.14);
}
double deg_to_rad(double deg)
{
return deg * (M_PI / 180.0);
}
double rad_to_deg(double rad)
{
return rad * 180.0 / M_PI;
}
double uv_to_deg(double u, double v)
{
double ang = atan2(v, u);
return rad_to_deg(ang) + 180.0;
}
// Quantifies a normalized y value to ntsc-j standards
uint8_t y_quant_j(double y)
{
int32_t y1 = round(y * 235.0);
if (y1 < 0) y1 = 0;
if (y1 > 255) y1 = 255;
return y1;
}
// Quantifies a normalized y value to ntsc standards
uint8_t y_quant(double y)
{
int32_t y1 = round((y * 219.0) + 16.0);
if (y1 < 0) y1 = 0;
if (y1 > 255) y1 = 255;
return y1;
}
// Quantifies a normalized pb or pr value to rec 601 standards
uint8_t c_quant_601(double c)
{
int32_t c1 = round((c * 219.0) + 16.0);
if (c1 < 0) c1 = 0;
if (c1 > 255) c1 = 255;
return c1;
}
// Quantifies a normalized pb or pr value to rec 709 standards
uint8_t c_quant_709(double c)
{
int32_t c1 = round((c * 224.0) + 16.0);
if (c1 < 0) c1 = 0;
if (c1 > 255) c1 = 255;
return c1;
}
// Standard split chroma to reduced u and v
static void c_to_uv(double c, double *u, double *v)
{
*v = sin(c);
*u = cos(c);
}
// Standard RGB to luma generation for most color spaces
static double rgb_to_y(struct color_coeff *co, double r, double g, double b)
{
return (co->r * r) + (co->g * g) + (co->b * b);
}
// Convert standard RGB to un-reduced YUV
static void rgb_to_yuv(struct color_coeff *co, double r, double g, double b, double *y, double *u, double *v)
{
*y = rgb_to_y(co, r, g, b);
*u = (co->u_max * b) - (co->r * r) - (co->g * g);
*v = (co->v_max * r) - (co->g * g) - (co->b * b);
}
// Convert un-reduced YUV to RGB
static void yuv_to_rgb(struct color_coeff *co, double y, double u, double v, double *r, double *g, double *b)
{
*r = y + v * ((1.0 - co->r) / co->v_max);
*g = y - u * ((co->b * (1.0 - co->b)) / (co->u_max * co->g)) - v * ((co->r * (1.0 - co->r)) / (co->v_max * co->g));
*b = y + u * ((1.0 - co->b) / co->u_max);
}
void rgb_to_ycbcr(struct color_coeff *co, double r, double g, double b, double *y, double *cb, double *cr)
{
*y = rgb_to_y(co, r, g, b);
*cb = b - (co->r * r) - (co->g * g);
*cr = r - (co->g * g) - (co->b * b);
}
void ycbcr_to_rgb(struct color_coeff *co, double y, double cb, double cr, double *r, double *g, double *b)
{
*r = y + cr * ((1.0 - co->r));
*g = y - cb * ((co->b * (1.0 - co->b)) / (co->g)) - cr * ((co->r * (1.0 - co->r)) / (co->g));
*b = y + cb * ((1.0 - co->b));
}
void desaturate_rgb(double factor, double *r, double *g, double *b)
{
double L = 0.3 * *r + 0.6 * *g + 0.1 * *b;
*r = *r + factor * (L - *r);
*g = *g + factor * (L - *g);
*b = *b + factor * (L - *b);
}
void desaturate_rgb_linear(struct color_coeff *co, double factor, double *r, double *g, double *b)
{
double y = rgb_to_y(co, *r, *g, *b);
*r = (*r * factor) + (y * (1.0 - factor));
*g = (*g * factor) + (y * (1.0 - factor));
*b = (*b * factor) + (y * (1.0 - factor));
}
// // Convert RGB to YCbCr
// void rgb_to_ycbcr(struct color_coeff *co, double r, double g, double b, double *y, double *cb, double *cr)
// {
// *y = rgb_to_y(co, r, g, b);
// *cb = 0.5 * ((b - *y) / (1.0 - co->b));
// *cr = 0.5 * ((r - *y) / (1.0 - co->r));
// }
// // This does not assume a bit size, so scaling and reductions should be done afterwards
// void ycbcr_to_rgb(struct color_coeff *co, double y, double cb, double cr, double *r, double *g, double *b)
// {
// *r = y + (co->v_max * 2.0) * cr;
// *g = y - ((co->u_max * 2.0) * co->b) / (co->g * cb) - ((co->v_max * 2.0) * co->r) / (co->g * cr);
// *b = y + (co->u_max * 2.0) * cb;
// }
// Creates a standard rec 601 U and V reduced value for composite video from (B-Y) and (R-Y)
static void uv_reduce(double *u, double *v)
{
// For compatible with 50's era television recievers, it was anecodtally discovered that the
// color difference signals must not have a possible excursion of greater than 33.3% above white
// or below black. This means a maximum positive excursion of 100IRE + ((1/3) * 92.5 IRE) =
// 130.8025 IRE and a minimum of 7.5IRE - ((1/3) * 92.5) = 23.3025IRE. The goal is to make the
// yellow and cyan bars of 75% color bars equal to 100 IRE.
*u = U_REDUCTION * *u;
*v = V_REDUCTION * *v;
}
static void uv_to_iq(double u, double v, double *i, double *q)
{
// IQ is effectively UV rotated about 33 degrees for ancient bandwidth and fidelity reasons,
// with some attempts made to align the signal to have more fidelity with skin tones. Contrary
// to popular belief, IQ is not used for most modern-ish composite signals, but rather UV is
// instead.
*i = (u * sin(deg_to_rad(33.0)) + (v * cos(deg_to_rad(33.0))));
*q = (u * cos(deg_to_rad(33.0)) + (v * sin(deg_to_rad(33.0))));
}
// Angle + Vector to X Y coordinates
void av_to_xy(double angle, double vector, double *x, double *y)
{
*x = vector * cos(deg_to_rad(angle));
*y = vector * sin(deg_to_rad(angle));
}
// Generate the maximum possible vector for a given angle
double angle_max_vector(struct color_coeff *co, double angle)
{
double u, v;
u = (255.0 * co->u_max) * cos(deg_to_rad(angle));
v = (255.0 * co->v_max) * sin(deg_to_rad(angle));
u *= U_REDUCTION;
v *= V_REDUCTION;
return sqrt((u * u) + (v * v));
}
// Returns the reduced vector after applying NTSC U and V reduction for a given angle.
double av_reduce(double angle, double vector)
{
double v1 = 0;
double x, y;
av_to_xy(angle, vector, &x, &y);
uv_reduce(&x, &y);
v1 = sqrt((x * x) + (y * y));
return v1;
}