add view thumbnail size pref, rework vicon view popup, add view popup zoom scale
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bccolors.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifndef __BCCOLORS_H__
23 #define __BCCOLORS_H__
24
25 // Duplicate filename in guicast
26
27 #include "bccolors.inc"
28 #include "clip.h"
29 #include "vframe.inc"
30
31 #include <stdint.h>
32
33
34 /*
35 Digital YCbCr is derived from analog RGB as follows:
36 RGB are 0..1 (gamma corrected supposedly),
37 normalized equations:
38 Y  Py =   Kr * R  +  Kg * G  +  Kb * B
39 U  Pb = - 0.5*Kr/(1-Kb)*R - 0.5*Kg/(1-Kb)*G + 0.5*B
40 V  Pr =   0.5*R - 0.5*Kg/(1-Kr)*G - 0.5*Kb/(1-Kr)*B
41 inverse:
42    R = Py + Pr * 2*(1-Kr)
43    G = Py - Pr * 2*Kr*(1-Kr)/Kg - Pb * 2*Kb*(1-Kb)/Kg
44    B = Py                       + Pb * 2*(1-Kb)
45
46 bt601, white vector= Kr=0.299,Kg=0.587,Kb=0.114
47    Py = + 0.299000*R + 0.587000*G + 0.114000*B
48    Pb = - 0.168736*R - 0.331264*G + 0.500000*B
49    Pr = + 0.500000*R - 0.418698*G - 0.081312*B
50 inverse:
51    R  = Py + 1.402000*Pr
52    G  = Py - 0.714136*Pr - 0.344136*Pb
53    B  = Py               + 1.772000*Pb
54
55 equations zeroed at (0,128,128), range (255,255,255)
56 bt.601  0..255
57    Y  = 0   + 0.299000*R + 0.587000*G + 0.114000*B
58    Cb = 128 - 0.168736*R - 0.331264*G + 0.500000*B
59    Cr = 128 + 0.500000*R - 0.418698*G - 0.081312*B
60 inverse:
61    R  = Y + 1.402000*(Cr-128)
62    G  = Y - 0.714136*(Cr-128) - 0.344136*(Cb-128)
63    B  = Y                     + 1.772000*(Cb-128)
64
65 equations zeroed at (16,128,128), range (219,224,224)
66 Y,Cb,Cr = (16,128,128) + (219*Py,224*Pb,224*Pr)
67
68 bt.601  16..235
69    Y  = 16  + 0.256788*R + 0.504129*G + 0.097906*B
70    Cb = 128 - 0.148227*R - 0.290992*G + 0.439216*B
71    Cr = 128 + 0.439216*R - 0.367789*G - 0.071426*B
72 inverse:
73    R  = (Y-16)*1.164384 + 1.596027*(Cr-128)
74    G  = (Y-16)*1.164384 - 0.812968*(Cr-128) - 0.391762*(Cb-128)
75    B  = (Y-16)*1.164384                     + 2.017232*(Cb-128)
76
77 bt.709, white vector= Kr=0.2126,Kg=0.7152,Kb=0.0722
78    Py = + 0.212600*R + 0.715200*G + 0.072200*B
79    Pb = - 0.114572*R - 0.385428*G + 0.500000*B
80    Pr = + 0.500000*R - 0.454153*G - 0.045847*B
81
82 equations zeroed at (0,128,128), range (255,255,255)
83 bt.709  0..255
84    Y  = 0   + 0.212600*R + 0.715200*G + 0.072200*B 
85    Cb = 128 - 0.114572*R - 0.385428*G + 0.500000*B
86    Cr = 128 + 0.500000*R - 0.454153*G - 0.045847*B
87 inverse:
88    R = Y + 1.574800*(Cr-128)
89    G = Y - 0.468124*(Cr-128) - 0.187324*(Cb-128)
90    B = Y                     + 1.855600*(Cb-128)
91
92 equations zeroed at (16,128,128), range (219,224,224)
93 Y,Cb,Cr = (16,128,128) + (219*Py,224*Pb,224*Pr)
94    Y  = 16  + 0.182586*R + 0.614231*G + 0.062007*B
95    Cb = 128 - 0.100644*R - 0.338572*G + 0.439216*B
96    Cr = 128 + 0.439216*R - 0.398942*G - 0.040276*B
97 inverse:
98    R = (Y-16)*1.164384 + 1.792741*(Cr-128)
99    G = (Y-16)*1.164384 - 0.532909*(Cr-128) - 0.213249*(Cb-128)
100    B = (Y-16)*1.164384                     + 2.112402*(Cb-128)
101
102 */
103 // white vector normalized, so:
104 //  Kg = 1 - Kr - Kb
105
106 #define BT601_Kr 0.299
107 #define BT601_Kb 0.114
108
109 #define BT709_Kr 0.2126
110 #define BT709_Kb 0.0722
111
112 #define BT2020_Kr 0.2627
113 #define BT2020_Kb 0.0593
114
115 class YUV
116 {
117         int mpeg, yzero, uvzero;
118         int ymin8, ymax8, ymin16, ymax16;
119         int uvmin8, uvmax8, uvmin16, uvmax16;
120         double Kr, Kg, Kb;
121         float yminf, ymaxf, yrangef;
122         float uvminf, uvmaxf, uvrangef;
123         float r_to_y, g_to_y, b_to_y;
124         float r_to_u, g_to_u, b_to_u;
125         float r_to_v, g_to_v, b_to_v;
126         float v_to_r, v_to_g;
127         float u_to_g, u_to_b;
128         int *tab;
129         float *tabf;
130
131         void init(double Kr, double Kb, int mpeg);
132         void init_tables(int len,
133                 int *rtoy, int *rtou, int *rtov,
134                 int *gtoy, int *gtou, int *gtov,
135                 int *btoy, int *btou, int *btov,
136                 int *ytab, int *vtor, int *vtog, int *utog, int *utob);
137         void init_tables(int len,
138                 float *vtorf, float *vtogf, float *utogf, float *utobf);
139
140 // dont use pointers,
141 //  offsets do not require indirect access
142 #define rtoy16 (tab+0x00000)
143 #define gtoy16 (tab+0x10000)
144 #define btoy16 (tab+0x20000)
145 #define rtou16 (tab+0x30000)
146 #define gtou16 (tab+0x40000)
147 #define btou16 (tab+0x50000)
148 #define rtov16 (tab+0x60000)
149 #define gtov16 (tab+0x70000)
150 #define btov16 (tab+0x80000)
151 #define ytab16 (tab+0x90000)
152 #define vtor16 (tab+0xa0000)
153 #define vtog16 (tab+0xb0000)
154 #define utog16 (tab+0xc0000)
155 #define utob16 (tab+0xd0000)
156
157 #define rtoy8 (tab+0xe0000)
158 #define gtoy8 (tab+0xe0100)
159 #define btoy8 (tab+0xe0200)
160 #define rtou8 (tab+0xe0300)
161 #define gtou8 (tab+0xe0400)
162 #define btou8 (tab+0xe0500)
163 #define rtov8 (tab+0xe0600)
164 #define gtov8 (tab+0xe0700)
165 #define btov8 (tab+0xe0800)
166 #define ytab8 (tab+0xe0900)
167 #define vtor8 (tab+0xe0a00)
168 #define vtog8 (tab+0xe0b00)
169 #define utog8 (tab+0xe0c00)
170 #define utob8 (tab+0xe0d00)
171
172 #define vtor16f (tabf+0x00000)
173 #define vtog16f (tabf+0x10000)
174 #define utog16f (tabf+0x20000)
175 #define utob16f (tabf+0x30000)
176
177 #define vtor8f (tabf+0x40000)
178 #define vtog8f (tabf+0x40100)
179 #define utog8f (tabf+0x40200)
180 #define utob8f (tabf+0x40300)
181
182 public:
183         YUV();
184         ~YUV();
185         void yuv_set_colors(int color_space, int color_range);
186         inline int is_mpeg() { return mpeg; }
187
188         static YUV yuv;
189         static float yuv_to_rgb_matrix[9];
190         static float rgb_to_yuv_matrix[9];
191         static float *const rgb_to_y_vector;
192         inline float get_yminf() { return yminf; }
193
194 #define YUV_rgb_to_yuv_8(r,g,b, y,u,v) \
195         y = iclip((rtoy8[r] + gtoy8[g] + btoy8[b] +  yzero) >> 16,  ymin8,  ymax8); \
196         u = iclip((rtou8[r] + gtou8[g] + btou8[b] + uvzero) >> 16, uvmin8, uvmax8); \
197         v = iclip((rtov8[r] + gtov8[g] + btov8[b] + uvzero) >> 16, uvmin8, uvmax8)
198
199         bc_always_inline void rgb_to_yuv_8(int r, int g, int b, int &y, int &u, int &v) {
200                 YUV_rgb_to_yuv_8(r,g,b, y,u,v);
201         }
202         bc_always_inline void rgb_to_yuv_8(int r, int g, int b, uint8_t &y, uint8_t &u, uint8_t &v) {
203                 YUV_rgb_to_yuv_8(r,g,b, y,u,v);
204         }
205         bc_always_inline void rgb_to_yuv_8(int &y, int &u, int &v) {
206                 int r = y, g = u, b = v;  YUV_rgb_to_yuv_8(r, g, b, y, u, v);
207         }
208
209 #define YUV_rgb_to_yuv_16(r,g,b, y,u,v) \
210         y = iclip((rtoy16[r] + gtoy16[g] + btoy16[b] +  yzero) >> 8,  ymin16,  ymax16); \
211         u = iclip((rtou16[r] + gtou16[g] + btou16[b] + uvzero) >> 8, uvmin16, uvmax16); \
212         v = iclip((rtov16[r] + gtov16[g] + btov16[b] + uvzero) >> 8, uvmin16, uvmax16)
213
214         bc_always_inline void rgb_to_yuv_16(int r, int g, int b, int &y, int &u, int &v) {
215                 YUV_rgb_to_yuv_16(r,g,b, y,u,v);
216         }
217         bc_always_inline void rgb_to_yuv_16(int r, int g, int b, uint16_t &y, uint16_t &u, uint16_t &v) {
218                 YUV_rgb_to_yuv_16(r,g,b, y,u,v);
219         }
220         bc_always_inline void rgb_to_yuv_16(int &y, int &u, int &v) {
221                 int r = y, g = u, b = v;
222                 YUV_rgb_to_yuv_16(r, g, b, y, u, v);
223         }
224
225         bc_always_inline void rgb_to_yuv_f(float r, float g, float b, float &y, float &u, float &v) {
226                 y = r * r_to_y + g * g_to_y + b * b_to_y + yminf;
227                 u = r * r_to_u + g * g_to_u + b * b_to_u;
228                 v = r * r_to_v + g * g_to_v + b * b_to_v;
229         }
230         bc_always_inline void rgb_to_yuv_f(float r, float g, float b, uint8_t &y, uint8_t &u, uint8_t &v) {
231                 int ir = iclip(r*0x100, 0, 0xff);
232                 int ig = iclip(g*0x100, 0, 0xff);
233                 int ib = iclip(b*0x100, 0, 0xff);
234                 rgb_to_yuv_8(ir,ig,ib, y,u,v);
235         }
236         bc_always_inline void rgb_to_yuv_f(float r, float g, float b, uint16_t &y, uint16_t &u, uint16_t &v) {
237                 int ir = iclip(r*0x10000, 0, 0xffff);
238                 int ig = iclip(g*0x10000, 0, 0xffff);
239                 int ib = iclip(b*0x10000, 0, 0xffff);
240                 rgb_to_yuv_16(ir,ig,ib, y,u,v);
241         }
242
243 #define YUV_yuv_to_rgb_8(r,g,b, y,u,v) \
244         r = iclip((ytab8[y] + vtor8[v]) >> 16, 0, 0xff); \
245         g = iclip((ytab8[y] + utog8[u] + vtog8[v]) >> 16, 0, 0xff); \
246         b = iclip((ytab8[y] + utob8[u]) >> 16, 0, 0xff)
247
248         bc_always_inline void yuv_to_rgb_8(int &r, int &g, int &b, int y, int u, int v) {
249                 YUV_yuv_to_rgb_8(r,g,b, y,u,v);
250         }
251         bc_always_inline void yuv_to_rgb_8(uint8_t &r, uint8_t &g, uint8_t &b, int y, int u, int v) {
252                 YUV_yuv_to_rgb_8(r,g,b, y,u,v);
253         }
254         bc_always_inline void yuv_to_rgb_8(int &r, int &g, int &b) {
255                 int y = r, u = g, v = b;  YUV_yuv_to_rgb_8(r,g,b, y,u,v);
256         }
257         bc_always_inline void yuv_to_rgb_8(float &r, float &g, float &b, int y, int u, int v) {
258                 int ir, ig, ib;  yuv_to_rgb_8(ir,ig,ib, y,u,v);
259                 float s = 1/255.f;  r = s*ir;  g = s*ig;  b = s*ib;
260         }
261
262 #define YUV_yuv_to_rgb_16(r,g,b, y,u,v) \
263         r = iclip((ytab16[y] + vtor16[v]) >> 8, 0, 0xffff); \
264         g = iclip((ytab16[y] + utog16[u] + vtog16[v]) >> 8, 0, 0xffff); \
265         b = iclip((ytab16[y] + utob16[u]) >> 8, 0, 0xffff)
266
267         bc_always_inline void yuv_to_rgb_16(int &r, int &g, int &b, int y, int u, int v) {
268                 YUV_yuv_to_rgb_16(r,g,b, y,u,v);
269         }
270         bc_always_inline void yuv_to_rgb_16(uint16_t &r, uint16_t &g, uint16_t &b, int y, int u, int v) {
271                 YUV_yuv_to_rgb_16(r,g,b, y,u,v);
272         }
273         bc_always_inline void yuv_to_rgb_16(int &r, int &g, int &b) {
274                 int y = r, u = g, v = b;  YUV_yuv_to_rgb_16(r,g,b, y,u,v);
275         }
276         bc_always_inline void yuv_to_rgb_16(float &r, float &g, float &b, int y, int u, int v) {
277                 int ir, ig, ib;  YUV_yuv_to_rgb_16(ir,ig,ib, y,u,v);
278                 float s = 1/65535.f;  r = s*ir;  g = s*ig;  b = s*ib;
279         }
280
281         bc_always_inline void yuv_to_rgb_f(float &r, float &g, float &b, float y, float u, float v) {
282                 y = (y-yminf) / yrangef;
283                 r = y + v_to_r * v;
284                 g = y + u_to_g * u + v_to_g * v;
285                 b = y + u_to_b * u;
286         }
287         bc_always_inline void yuv_to_rgb_f(float &r, float &g, float &b, uint8_t &y, uint8_t &u, uint8_t &v) {
288                 yuv_to_rgb_8(r,g,b, y,u,v);
289         }
290         bc_always_inline void yuv_to_rgb_f(float &r, float &g, float &b, uint16_t &y, uint16_t &u, uint16_t &v) {
291                 yuv_to_rgb_16(r,g,b, y,u,v);
292         }
293
294         bc_always_inline int rgb_to_y_8(int r, int g, int b) {
295                 return (rtoy8[r] + gtoy8[g] + btoy8[b] + yzero) >> 16;
296         }
297         bc_always_inline int rgb_to_y_16(int r, int g, int b) {
298                 return (rtoy16[r] + gtoy16[g] + btoy16[b] + yzero) >> 8;
299         }
300         bc_always_inline float rgb_to_y_f(float r, float g, float b) {
301                 return r * r_to_y + g * g_to_y + b * b_to_y + yminf;
302         }
303
304 // For easier programming.  Doesn't do anything.
305 // unused cases in macro expansions, mismatched argument types
306         inline void yuv_to_rgb_8(float &r, float &g, float &b, float y, float u, float v) {}
307         inline void yuv_to_rgb_16(float &r, float &g, float &b, float y, float u, float v) {}
308         inline void yuv_to_rgb_f(int &r, int &g, int &b, int y, int u, int v) {}
309         inline void yuv_to_rgb_f(uint8_t &r, uint8_t &g, uint8_t &b, int&y, int u, int v) {}
310         inline void yuv_to_rgb_f(uint16_t &r, uint16_t &g, uint16_t &b, int y, int u, int v) {}
311
312         inline void rgb_to_yuv_8(float r, float g, float b, float &y, float &u, float &v) {}
313         inline void rgb_to_yuv_16(float r, float g, float b, float &y, float &u, float &v) {}
314         inline void rgb_to_yuv_f(int r, int g, int b, int &y, int &u, int &v) {}
315         inline void rgb_to_yuv_f(uint8_t r, uint8_t g, uint8_t b, int &y, int &u, int &v) {}
316         inline void rgb_to_yuv_f(uint16_t r, uint16_t g, uint16_t b, int &y, int &u, int &v) {}
317 };
318
319
320 class HSV
321 {
322 public:
323         HSV();
324         ~HSV();
325
326 // All units are 0 - 1
327         static int rgb_to_hsv(float r, float g, float b, float &h, float &s, float &v);
328         static int hsv_to_rgb(float &r, float &g, float &b, float h, float s, float v);
329
330 // YUV units are 0 - max.  HSV units are 0 - 1
331         static int yuv_to_hsv(int y, int u, int v, float &h, float &s, float &va, int max);
332         static int hsv_to_yuv(int &y, int &u, int &v, float h, float s, float va, int max);
333 // Dummies for macros
334         static int yuv_to_hsv(float y, float u, float v, float &h, float &s, float &va, float max) { return 0; };
335         static int hsv_to_yuv(float &y, float &u, float &v, float h, float s, float va, float max) { return 0; };
336 };
337
338
339 // standard colors
340 #define BLACK   0x000000
341 #define WHITE   0xFFFFFF
342
343 #define LTBLUE  0x9090FF
344 #define BLUE    0x0000FF
345 #define DKBLUE  0x000090
346
347 #define LTPINK  0xFFC0C0
348 #define PINK    0xFF8080
349 #define RED     0xFF0000
350
351 #define LTGREEN 0xC0FFC0
352 #define GREEN   0x00FF00
353 #define DKGREEN 0x009000
354
355 #define YELLOW  0xFFFF00
356 #define LTYELLOW 0xFFFFA0
357 #define MEYELLOW 0xFFFF00
358 #define MDYELLOW 0xFFFFD2
359 #define DKYELLOW 0xFFFFB4
360
361 #define LTCYAN  0x00CBCB
362 #define MECYAN  0x009696
363 #define MDCYAN  0x007E7E
364 #define DKCYAN  0x004949
365
366 #define LTPURPLE 0xFFC0FF
367 #define MEPURPLE 0xFF00FF
368 #define MDPURPLE 0xC000C0
369 #define DKPURPLE 0xA000A0
370
371 #define LTGREY  0xE0E0E0
372 #define MEGREY  0xAFAFAF
373 #define DMGREY  0x999999
374 #define MDGREY  0x7D7D7D
375 #define DKGREY  0x4B4B4B
376
377 #define BLOND   0xb4b487
378 #define SLBLUE  0x6040c0
379
380 #define MNGREY  0xe6e6e6
381 #define FGGREY  0xe3e3e3
382 #define MNBLUE  0x003cff
383 #define ORANGE  0xffdd76
384 #define FTGREY  0xbcbcbc
385
386 #endif