Fawkes API  Fawkes Development Version
rgbyuv.h
1 
2 /****************************************************************************
3  * rgbyuv.h - RGB to YUV conversion - specific methods, macros and constants
4  *
5  * Created: Sat Aug 12 15:21:39 2006
6  * based on colorspaces.h from Tue Feb 23 13:49:38 2005
7  * Copyright 2005-2006 Tim Niemueller [www.niemueller.de]
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #ifndef FIREVISION_UTILS_COLOR_RGBYUV_H_
26 #define FIREVISION_UTILS_COLOR_RGBYUV_H_
27 
28 namespace firevision {
29 
30 #define RGB2YUV(r, g, b, y, u, v) \
31  { \
32  y = (306 * r + 601 * g + 117 * b) >> 10; \
33  u = ((-172 * r - 340 * g + 512 * b) >> 10) + 128; \
34  v = ((512 * r - 429 * g - 83 * b) >> 10) + 128; \
35  y = y < 0 ? 0 : y; \
36  u = u < 0 ? 0 : u; \
37  v = v < 0 ? 0 : v; \
38  y = y > 255 ? 255 : y; \
39  u = u > 255 ? 255 : u; \
40  v = v > 255 ? 255 : v; \
41  }
42 
43 /* Alternative from libdc1394
44  y = (306*r + 601*g + 117*b) >> 10; \
45  u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
46  v = ((512*r - 429*g - 83*b) >> 10) + 128;\
47 
48  Original:
49  y = ((9798*(r) + 19235*(g) + 3736*(b)) >> 15); \
50  u = ((-4784*(r) - 9437*(g) + 14221*(b)) >> 15) + 128; \
51  v = ((20218*(r) - 16941*(g) - 3277*(b)) >> 15) + 128; \
52 
53 */
54 
55 void
56 rgb_to_yuy2(const unsigned char *RGB, unsigned char *YUV, unsigned int width, unsigned int height);
57 
58 /** RGB to YUV Conversion
59  *
60  * Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
61  * Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
62  * Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128
63  *
64  * Values have to be clamped to keep them in the [0-255] range.
65  * Rumour has it that the valid range is actually a subset of [0-255] (fourcc.org mentions an RGB range
66  * of [16-235]) but clamping the values into [0-255] seems to produce acceptable results.
67  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
68  * (thus this is a 24bit RGB with one byte per color) line by line.
69  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
70  * line
71  * @param width Width of the image contained in the RGB buffer
72  * @param height Height of the image contained in the RGB buffer
73  */
74 void rgb_to_yuv411packed_plainc(const unsigned char *RGB,
75  unsigned char * YUV,
76  unsigned int width,
77  unsigned int height);
78 
79 /* Convert a line of a RGB buffer to a line in a planar YUV422 buffer, see above for general
80  * notes about color space conversion from RGB to YUV
81  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
82  * (thus this is a 24bit RGB with one byte per color) line by line.
83  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
84  * line
85  * @param width Width of the image contained in the RGB buffer
86  * @param height Height of the image contained in the RGB buffer
87  * @param rgb_line the index of the line to be converted
88  * @param yuv_line the index of the line to convert to in the YUV buffer
89  */
90 void convert_line_rgb_to_yuv422planar(const unsigned char *RGB,
91  unsigned char * YUV,
92  unsigned int width,
93  unsigned int height,
94  unsigned int rgb_line,
95  unsigned int yuv_line);
96 
97 /* Convert an RGB buffer to a planar YUV422 buffer, see above for general notes about color space
98  * conversion from RGB to YUV
99  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
100  * (thus this is a 24bit RGB with one byte per color) line by line.
101  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
102  * line
103  * @param width Width of the image contained in the RGB buffer
104  * @param height Height of the image contained in the RGB buffer
105  */
106 void rgb_to_yuv422planar_plainc(const unsigned char *RGB,
107  unsigned char * YUV,
108  unsigned int width,
109  unsigned int height);
110 
111 /* Convert a planar RGB buffer to a packed YUV422 buffer.
112  * See above for general notes about color space
113  * conversion from RGB to YUV
114  * @param RGB unsigned char array that contains the color planes
115  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
116  * line
117  * @param width Width of the image contained in the RGB buffer
118  * @param height Height of the image contained in the RGB buffer
119  */
120 void rgb_planar_to_yuv422packed_plainc(const unsigned char *rgb_planar,
121  unsigned char * YUV,
122  unsigned int width,
123  unsigned int height);
124 
125 /* Convert a line of a RGB buffer to a line in a packed YUV422 buffer, see above for general
126  * notes about color space conversion from RGB to YUV
127  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
128  * (thus this is a 24bit RGB with one byte per color) line by line.
129  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
130  * line
131  * @param width Width of the image contained in the RGB buffer
132  * @param height Height of the image contained in the RGB buffer
133  * @param rgb_line the index of the line to be converted
134  * @param yuv_line the index of the line to convert to in the YUV buffer
135  */
136 void convert_line_rgb_to_yuv422packed(const unsigned char *RGB,
137  unsigned char * YUV,
138  unsigned int width,
139  unsigned int height,
140  unsigned int rgb_line,
141  unsigned int yuv_line);
142 
143 /* Convert an RGB buffer to a packed YUV422 buffer, see above for general notes about color space
144  * conversion from RGB to YUV
145  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
146  * (thus this is a 24bit RGB with one byte per color) line by line.
147  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
148  * line
149  * @param width Width of the image contained in the RGB buffer
150  * @param height Height of the image contained in the RGB buffer
151  */
152 void rgb_to_yuv422packed_plainc(const unsigned char *RGB,
153  unsigned char * YUV,
154  unsigned int width,
155  unsigned int height);
156 
157 /* Convert an BGR buffer to a planar YUV422 buffer, see above for general notes about color space
158  * conversion from RGB to YUV
159  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
160  * (thus this is a 24bit RGB with one byte per color) line by line.
161  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
162  * line
163  * @param width Width of the image contained in the RGB buffer
164  * @param height Height of the image contained in the RGB buffer
165  */
166 void bgr_to_yuv422planar_plainc(const unsigned char *BGR,
167  unsigned char * YUV,
168  unsigned int width,
169  unsigned int height);
170 
171 } // end namespace firevision
172 
173 #endif