Add Russian translation provided by Валерий Крувялис <valkru@mail.ru>
[xiph-mirror.git] / w3d / yuv.c
blob234564f105d37202c555ade2d7343bf76d616477
1 #include "yuv.h"
3 /*#define TARKIN_YUV_EXACT*/
4 #define TARKIN_YUV_LXY
7 static inline
8 uint8_t CLAMP(int16_t x)
10 return ((x > 255) ? 255 : (x < 0) ? 0 : x);
15 void rgb24_to_yuv (uint8_t *rgb, Wavelet3DBuf *yuv [], uint32_t frame)
17 int count = yuv[0]->width * yuv[0]->height;
18 int16_t *y = yuv[0]->data + frame * count;
19 int16_t *u = yuv[1]->data + frame * count;
20 int16_t *v = yuv[2]->data + frame * count;
21 int i;
23 #if defined(TARKIN_YUV_EXACT)
24 for (i=0; i<count; i++, rgb+=3) {
25 y [i] = ((int16_t) 77 * rgb [0] + 150 * rgb [1] + 29 * rgb [2]) / 256;
26 u [i] = ((int16_t) -44 * rgb [0] - 87 * rgb [1] + 131 * rgb [2]) / 256;
27 v [i] = ((int16_t) 131 * rgb [0] - 110 * rgb [1] - 21 * rgb [2]) / 256;
29 #elif defined(TARKIN_YUV_LXY)
30 for (i=0; i<count; i++, rgb+=3) {
31 y [i] = ((int16_t) 55 * rgb [0] + 183 * rgb [1] + 18 * rgb [2]) / 256;
32 u [i] = rgb [0] - y [i];
33 v [i] = rgb [2] - y [i];
35 #else
36 for (i=0; i<count; i++, rgb+=3) {
37 v [i] = rgb [0] - rgb [1];
38 u [i] = rgb [2] - rgb [1];
39 y [i] = rgb [1] + (u [i] + v [i]) / 4;
41 #endif
45 void yuv_to_rgb24 (Wavelet3DBuf *yuv [], uint8_t *rgb, uint32_t frame)
47 int count = yuv[0]->width * yuv[0]->height;
48 int16_t *y = yuv[0]->data + frame * count;
49 int16_t *u = yuv[1]->data + frame * count;
50 int16_t *v = yuv[2]->data + frame * count;
51 int i;
53 #if defined(TARKIN_YUV_EXACT)
54 for (i=0; i<count; i++, rgb+=3) {
55 rgb [0] = CLAMP(y [i] + 1.371 * v [i]);
56 rgb [1] = CLAMP(y [i] - 0.698 * v [i] - 0.336 * u [i]);
57 rgb [2] = CLAMP(y [i] + 1.732 * u [i]);
59 #elif defined(TARKIN_YUV_LXY)
60 for (i=0; i<count; i++, rgb+=3) {
61 rgb [1] = CLAMP(y [i] - (77 * u [i] + 25 * v [i]) / 256);
62 rgb [0] = CLAMP(y [i] + u [i]);
63 rgb [2] = CLAMP(y [i] + v [i]);
65 #else
66 for (i=0; i<count; i++, rgb+=3) {
67 rgb [1] = CLAMP(y [i] - (u [i] + v [i]) / 4);
68 rgb [2] = CLAMP(u [i] + rgb [1]);
69 rgb [0] = CLAMP(v [i] + rgb [1]);
71 #endif
75 void rgb32_to_yuv (uint8_t *rgb, Wavelet3DBuf *yuv [], uint32_t frame)
77 int count = yuv[0]->width * yuv[0]->height;
78 int16_t *y = yuv[0]->data + frame * count;
79 int16_t *u = yuv[1]->data + frame * count;
80 int16_t *v = yuv[2]->data + frame * count;
81 int i;
83 #if defined(TARKIN_YUV_EXACT)
84 for (i=0; i<count; i++, rgb+=4) {
85 y [i] = ((int16_t) 77 * rgb [0] + 150 * rgb [1] + 29 * rgb [2]) / 256;
86 u [i] = ((int16_t) -44 * rgb [0] - 87 * rgb [1] + 131 * rgb [2]) / 256;
87 v [i] = ((int16_t) 131 * rgb [0] - 110 * rgb [1] - 21 * rgb [2]) / 256;
89 #elif defined(TARKIN_YUV_LXY)
90 for (i=0; i<count; i++, rgb+=4) {
91 y [i] = ((int16_t) 55 * rgb [0] + 183 * rgb [1] + 18 * rgb [2]) / 256;
92 u [i] = rgb [0] - y [i];
93 v [i] = rgb [2] - y [i];
95 #else
96 for (i=0; i<count; i++, rgb+=4) {
97 v [i] = rgb [0] - rgb [1];
98 u [i] = rgb [2] - rgb [1];
99 y [i] = rgb [1] + (u [i] + v [i]) / 4;
101 #endif
105 void yuv_to_rgb32 (Wavelet3DBuf *yuv [], uint8_t *rgb, uint32_t frame)
107 int count = yuv[0]->width * yuv[0]->height;
108 int16_t *y = yuv[0]->data + frame * count;
109 int16_t *u = yuv[1]->data + frame * count;
110 int16_t *v = yuv[2]->data + frame * count;
111 int i;
113 #if defined(TARKIN_YUV_EXACT)
114 for (i=0; i<count; i++, rgb+=4) {
115 rgb [0] = CLAMP(y [i] + 1.371 * v [i]);
116 rgb [1] = CLAMP(y [i] - 0.698 * v [i] - 0.336 * u [i]);
117 rgb [2] = CLAMP(y [i] + 1.732 * u [i]);
119 #elif defined(TARKIN_YUV_LXY)
120 for (i=0; i<count; i++, rgb+=4) {
121 rgb [1] = CLAMP(y [i] - (77 * u [i] + 25 * v [i]) / 256);
122 rgb [0] = CLAMP(y [i] + u [i]);
123 rgb [2] = CLAMP(y [i] + v [i]);
125 #else
126 for (i=0; i<count; i++, rgb+=4) {
127 rgb [1] = CLAMP(y [i] - (u [i] + v [i]) / 4);
128 rgb [2] = CLAMP(u [i] + rgb [1]);
129 rgb [0] = CLAMP(v [i] + rgb [1]);
131 #endif
135 void rgba_to_yuv (uint8_t *rgba, Wavelet3DBuf *yuva [], uint32_t frame)
137 int count = yuva[0]->width * yuva[0]->height;
138 int16_t *y = yuva[0]->data + frame * count;
139 int16_t *u = yuva[1]->data + frame * count;
140 int16_t *v = yuva[2]->data + frame * count;
141 int16_t *a = yuva[3]->data + frame * count;
142 int i;
144 #if defined(TARKIN_YUV_EXACT)
145 for (i=0; i<count; i++, rgba+=4) {
146 y [i] = ((int16_t) 77 * rgba [0] + 150 * rgba [1] + 29 * rgba [2]) / 256;
147 u [i] = ((int16_t) -44 * rgba [0] - 87 * rgba [1] + 131 * rgba [2]) / 256;
148 v [i] = ((int16_t) 131 * rgba [0] - 110 * rgba [1] - 21 * rgba [2]) / 256;
149 a [i] = rgba [3];
151 #elif defined(TARKIN_YUV_LXY)
152 for (i=0; i<count; i++, rgba+=4) {
153 y [i] = ((int16_t) 55 * rgba [0] + 183 * rgba [1] + 18 * rgba [2]) / 256;
154 u [i] = rgba [0] - y [i];
155 v [i] = rgba [2] - y [i];
156 a [i] = rgba [3];
158 #else
159 for (i=0; i<count; i++, rgba+=4) {
160 v [i] = rgba [0] - rgba [1];
161 u [i] = rgba [2] - rgba [1];
162 y [i] = rgba [1] + (u [i] + v [i]) / 4;
163 a [i] = rgba [3];
165 #endif
169 void yuv_to_rgba (Wavelet3DBuf *yuva [], uint8_t *rgba, uint32_t frame)
171 int count = yuva[0]->width * yuva[0]->height;
172 int16_t *y = yuva[0]->data + frame * count;
173 int16_t *u = yuva[1]->data + frame * count;
174 int16_t *v = yuva[2]->data + frame * count;
175 int16_t *a = yuva[3]->data + frame * count;
176 int i;
178 #if defined(TARKIN_YUV_EXACT)
179 for (i=0; i<count; i++, rgba+=4) {
180 rgba [0] = CLAMP(y [i] + 1.371 * v [i]);
181 rgba [1] = CLAMP(y [i] - 0.698 * v [i] - 0.336 * u [i]);
182 rgba [2] = CLAMP(y [i] + 1.732 * u [i]);
183 rgba [3] = a [i];
185 #elif defined(TARKIN_YUV_LXY)
186 for (i=0; i<count; i++, rgba+=4) {
187 rgba [1] = CLAMP(y [i] - (77 * u [i] + 25 * v [i]) / 256);
188 rgba [0] = CLAMP(y [i] + u [i]);
189 rgba [2] = CLAMP(y [i] + v [i]);
190 rgba [3] = a [i];
192 #else
193 for (i=0; i<count; i++, rgba+=4) {
194 rgba [1] = CLAMP(y [i] - (u [i] + v [i]) / 4);
195 rgba [2] = CLAMP(u [i] + rgba [1]);
196 rgba [0] = CLAMP(v [i] + rgba [1]);
197 rgba [3] = a [i];
199 #endif
202 void grayscale_to_y (uint8_t *rgba, Wavelet3DBuf *y [], uint32_t frame)
204 int count = y[0]->width * y[0]->height;
205 int16_t *_y = y[0]->data + frame * count;
206 int i;
208 for (i=0; i<count; i++)
209 _y [i] = rgba [i];
213 void y_to_grayscale (Wavelet3DBuf *y [], uint8_t *rgba, uint32_t frame)
215 int count = y[0]->width * y[0]->height;
216 int16_t *_y = y[0]->data + frame * count;
217 int i;
219 for (i=0; i<count; i++)
220 rgba [i] = CLAMP(_y[i]);