remove whitespace at eol
[goodguy/history.git] / cinelerra-5.1 / cinelerra / cicolors.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 CICOLORS_H
23 #define CICOLORS_H
24
25 // Duplicate filename in guicast
26
27 #include "clip.h"
28 #include "vframe.inc"
29
30 #include <stdint.h>
31
32 // Compression coefficients straight out of jpeglib
33 #define R_TO_Y    0.29900
34 #define G_TO_Y    0.58700
35 #define B_TO_Y    0.11400
36
37 #define R_TO_U    -0.16874
38 #define G_TO_U    -0.33126
39 #define B_TO_U    0.50000
40
41 #define R_TO_V    0.50000
42 #define G_TO_V    -0.41869
43 #define B_TO_V    -0.08131
44
45 // Decompression coefficients straight out of jpeglib
46 #define V_TO_R    1.40200
47 #define V_TO_G    -0.71414
48
49 #define U_TO_G    -0.34414
50 #define U_TO_B    1.77200
51
52
53 class YUV
54 {
55 public:
56         YUV();
57         ~YUV();
58
59         inline void rgb_to_yuv_8(int &y, int &u, int &v)
60         {
61                 int r = y;
62                 int g = u;
63                 int b = v;
64                 y = (rtoy_tab_8[r] + gtoy_tab_8[g] + btoy_tab_8[b]) >> 8;
65                 u = (rtou_tab_8[r] + gtou_tab_8[g] + btou_tab_8[b]) >> 8;
66                 v = (rtov_tab_8[r] + gtov_tab_8[g] + btov_tab_8[b]) >> 8;
67         };
68
69         inline void rgb_to_yuv_8(int r, int g, int b, int &y, int &u, int &v)
70         {
71                 y = (rtoy_tab_8[r] + gtoy_tab_8[g] + btoy_tab_8[b]) >> 8;
72                 u = (rtou_tab_8[r] + gtou_tab_8[g] + btou_tab_8[b]) >> 8;
73                 v = (rtov_tab_8[r] + gtov_tab_8[g] + btov_tab_8[b]) >> 8;
74         };
75
76         inline void rgb_to_yuv_8(int r, int g, int b, unsigned char &y, unsigned char &u, unsigned char &v)
77         {
78                 y = (rtoy_tab_8[r] + gtoy_tab_8[g] + btoy_tab_8[b]) >> 8;
79                 u = (rtou_tab_8[r] + gtou_tab_8[g] + btou_tab_8[b]) >> 8;
80                 v = (rtov_tab_8[r] + gtov_tab_8[g] + btov_tab_8[b]) >> 8;
81         };
82
83         static inline void rgb_to_yuv_f(float r, float g, float b, float &y, float &u, float &v)
84         {
85                 y = r * R_TO_Y + g * G_TO_Y + b * B_TO_Y;
86                 u = r * R_TO_U + g * G_TO_U + b * B_TO_U;
87                 v = r * R_TO_V + g * G_TO_V + b * B_TO_V;
88         };
89
90         inline void rgb_to_yuv_16(int r, int g, int b, int &y, int &u, int &v)
91         {
92                 y = (rtoy_tab_16[r] + gtoy_tab_16[g] + btoy_tab_16[b]) >> 8;
93                 u = (rtou_tab_16[r] + gtou_tab_16[g] + btou_tab_16[b]) >> 8;
94                 v = (rtov_tab_16[r] + gtov_tab_16[g] + btov_tab_16[b]) >> 8;
95         };
96
97 // For easier programming.  Doesn't do anything.
98         inline void rgb_to_yuv_8(float r, float g, float b, float &y, float &u, float &v)
99         {
100         };
101
102         inline void rgb_to_yuv_16(float r, float g, float b, float &y, float &u, float &v)
103         {
104         };
105
106         static inline void rgb_to_yuv_f(int r, int g, int b, int &y, int &u, int &v)
107         {
108         };
109
110         inline void yuv_to_rgb_8(int &r, int &g, int &b)
111         {
112                 int y = r;
113                 int u = g;
114                 int v = b;
115                 y = (y << 8) | y;
116                 r = (y + vtor_tab_8[v]) >> 8;
117                 g = (y + utog_tab_8[u] + vtog_tab_8[v]) >> 8;
118                 b = (y + utob_tab_8[u]) >> 8;
119
120                 CLAMP(r, 0, 0xff);
121                 CLAMP(g, 0, 0xff);
122                 CLAMP(b, 0, 0xff);
123         };
124         inline void yuv_to_rgb_8(int &r, int &g, int &b, int y, int u, int v)
125         {
126                 y = (y << 8) | y;
127                 r = (y + vtor_tab_8[v]) >> 8;
128                 g = (y + utog_tab_8[u] + vtog_tab_8[v]) >> 8;
129                 b = (y + utob_tab_8[u]) >> 8;
130
131                 CLAMP(r, 0, 0xff);
132                 CLAMP(g, 0, 0xff);
133                 CLAMP(b, 0, 0xff);
134         };
135
136         static inline void yuv_to_rgb_f(float &r, float &g, float &b, float y, float u, float v)
137         {
138                 r = y + V_TO_R * v;
139                 g = y + U_TO_G * u + V_TO_G * v;
140                 b = y + U_TO_B * u;
141         };
142
143         inline void rgb_to_yuv_16(int r, int g, int b, uint16_t &y, uint16_t &u, uint16_t &v)
144         {
145                 y = (rtoy_tab_16[r] + gtoy_tab_16[g] + btoy_tab_16[b]) >> 8;
146                 u = (rtou_tab_16[r] + gtou_tab_16[g] + btou_tab_16[b]) >> 8;
147                 v = (rtov_tab_16[r] + gtov_tab_16[g] + btov_tab_16[b]) >> 8;
148         };
149
150         inline void yuv_to_rgb_16(int &r, int &g, int &b, int y, int u, int v)
151         {
152                 y = (y << 8) | y;
153                 r = (y + vtor_tab_16[v]) >> 8;
154                 g = (y + utog_tab_16[u] + vtog_tab_16[v]) >> 8;
155                 b = (y + utob_tab_16[u]) >> 8;
156
157                 CLAMP(r, 0, 0xffff);
158                 CLAMP(g, 0, 0xffff);
159                 CLAMP(b, 0, 0xffff);
160         };
161
162 // For easier programming.  Doesn't do anything.
163         inline void yuv_to_rgb_8(float &r, float &g, float &b, float y, float u, float v)
164         {
165         };
166
167 // For easier programming.  Doesn't do anything.
168         inline void yuv_to_rgb_16(float &r, float &g, float &b, float y, float u, float v)
169         {
170         };
171
172         static inline void yuv_to_rgb_f(int &r, int &g, int &b, int y, int u, int v)
173         {
174         };
175
176 private:
177         int rtoy_tab_8[0x100], gtoy_tab_8[0x100], btoy_tab_8[0x100];
178         int rtou_tab_8[0x100], gtou_tab_8[0x100], btou_tab_8[0x100];
179         int rtov_tab_8[0x100], gtov_tab_8[0x100], btov_tab_8[0x100];
180
181         int vtor_tab_8[0x100], vtog_tab_8[0x100];
182         int utog_tab_8[0x100], utob_tab_8[0x100];
183         int *vtor_8, *vtog_8, *utog_8, *utob_8;
184
185         int rtoy_tab_16[0x10000], gtoy_tab_16[0x10000], btoy_tab_16[0x10000];
186         int rtou_tab_16[0x10000], gtou_tab_16[0x10000], btou_tab_16[0x10000];
187         int rtov_tab_16[0x10000], gtov_tab_16[0x10000], btov_tab_16[0x10000];
188
189         int vtor_tab_16[0x10000], vtog_tab_16[0x10000];
190         int utog_tab_16[0x10000], utob_tab_16[0x10000];
191         int *vtor_16, *vtog_16, *utog_16, *utob_16;
192 };
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 class HSV
208 {
209 public:
210         HSV();
211     ~HSV();
212
213 // All units are 0 - 1
214         static int rgb_to_hsv(float r, float g, float b, float &h, float &s, float &v);
215         static int hsv_to_rgb(float &r, float &g, float &b, float h, float s, float v);
216
217 // YUV units are 0 - max.  HSV units are 0 - 1
218         static int yuv_to_hsv(int y, int u, int v, float &h, float &s, float &va, int max);
219         static int hsv_to_yuv(int &y, int &u, int &v, float h, float s, float va, int max);
220 // Dummies for macros
221         static int yuv_to_hsv(float y, float u, float v, float &h, float &s, float &va, float max) { return 0; };
222         static int hsv_to_yuv(float &y, float &u, float &v, float h, float s, float va, float max) { return 0; };
223         static YUV yuv_static;
224 };
225
226
227 #endif