4 * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
25 // Duplicate filename in guicast
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
37 #define R_TO_U -0.16874
38 #define G_TO_U -0.33126
39 #define B_TO_U 0.50000
41 #define R_TO_V 0.50000
42 #define G_TO_V -0.41869
43 #define B_TO_V -0.08131
45 // Decompression coefficients straight out of jpeglib
46 #define V_TO_R 1.40200
47 #define V_TO_G -0.71414
49 #define U_TO_G -0.34414
50 #define U_TO_B 1.77200
59 inline void rgb_to_yuv_8(int &y, int &u, int &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;
69 inline void rgb_to_yuv_8(int r, int g, int b, int &y, int &u, int &v)
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;
76 inline void rgb_to_yuv_8(int r, int g, int b, unsigned char &y, unsigned char &u, unsigned char &v)
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;
83 static inline void rgb_to_yuv_f(float r, float g, float b, float &y, float &u, float &v)
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;
90 inline void rgb_to_yuv_16(int r, int g, int b, int &y, int &u, int &v)
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;
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)
102 inline void rgb_to_yuv_16(float r, float g, float b, float &y, float &u, float &v)
106 static inline void rgb_to_yuv_f(int r, int g, int b, int &y, int &u, int &v)
110 inline void yuv_to_rgb_8(int &r, int &g, int &b)
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;
124 inline void yuv_to_rgb_8(int &r, int &g, int &b, int y, int u, int v)
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;
136 static inline void yuv_to_rgb_f(float &r, float &g, float &b, float y, float u, float v)
139 g = y + U_TO_G * u + V_TO_G * v;
143 inline void rgb_to_yuv_16(int r, int g, int b, uint16_t &y, uint16_t &u, uint16_t &v)
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;
150 inline void yuv_to_rgb_16(int &r, int &g, int &b, int y, int u, int v)
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;
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)
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)
172 static inline void yuv_to_rgb_f(int &r, int &g, int &b, int y, int u, int v)
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];
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;
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];
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;
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);
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;