2 * Misc image convertion routines
3 * Copyright (c) 2001 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* XXX: totally non optimized */
26 static void yuv422_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
27 UINT8 *src, int width, int height)
32 for(y=0;y<height;y+=2) {
33 for(x=0;x<width;x+=2) {
43 for(x=0;x<width;x+=2) {
53 #define ONE_HALF (1 << (SCALEBITS - 1))
54 #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
56 static void rgb24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
57 UINT8 *src, int width, int height)
59 int wrap, wrap3, x, y;
60 int r, g, b, r1, g1, b1;
66 for(y=0;y<height;y+=2) {
67 for(x=0;x<width;x+=2) {
74 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
75 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
82 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
83 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
93 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
94 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
101 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
102 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
104 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
105 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
106 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
107 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
119 static void bgr24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
120 UINT8 *src, int width, int height)
122 int wrap, wrap3, x, y;
123 int r, g, b, r1, g1, b1;
129 for(y=0;y<height;y+=2) {
130 for(x=0;x<width;x+=2) {
137 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
138 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
145 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
146 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
156 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
157 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
164 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
165 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
167 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
168 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
169 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
170 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
182 int img_convert_to_yuv420(UINT8 *img_out, UINT8 *img,
183 int pix_fmt, int width, int height)
190 size = width * height;
191 size_out = (size * 3) / 2;
193 picture[1] = pict + size;
194 picture[2] = picture[1] + (size / 4);
197 case PIX_FMT_YUV420P:
198 memcpy(pict, img, size_out);
201 yuv422_to_yuv420p(picture[0], picture[1], picture[2],
205 rgb24_to_yuv420p(picture[0], picture[1], picture[2],
209 bgr24_to_yuv420p(picture[0], picture[1], picture[2],