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 #if 0 /* just to make Emacs auto-indent happy */
30 }
31 #endif
32 
33 
34 #define RGB2YUV(r, g, b, y, u, v) { \
35  y = (306*r + 601*g + 117*b) >> 10; \
36  u = ((-172*r - 340*g + 512*b) >> 10) + 128; \
37  v = ((512*r - 429*g - 83*b) >> 10) + 128; \
38  y = y < 0 ? 0 : y; \
39  u = u < 0 ? 0 : u; \
40  v = v < 0 ? 0 : v; \
41  y = y > 255 ? 255 : y; \
42  u = u > 255 ? 255 : u; \
43  v = v > 255 ? 255 : v; }
44 
45 /* Alternative from libdc1394
46  y = (306*r + 601*g + 117*b) >> 10; \
47  u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
48  v = ((512*r - 429*g - 83*b) >> 10) + 128;\
49 
50  Original:
51  y = ((9798*(r) + 19235*(g) + 3736*(b)) >> 15); \
52  u = ((-4784*(r) - 9437*(g) + 14221*(b)) >> 15) + 128; \
53  v = ((20218*(r) - 16941*(g) - 3277*(b)) >> 15) + 128; \
54 
55 */
56 
57 
58 void rgb_to_yuy2(const unsigned char *RGB, unsigned char *YUV,
59  unsigned int width, unsigned int height);
60 
61 
62 /** RGB to YUV Conversion
63  *
64  * Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
65  * Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
66  * Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128
67  *
68  * Values have to be clamped to keep them in the [0-255] range.
69  * Rumour has it that the valid range is actually a subset of [0-255] (fourcc.org mentions an RGB range
70  * of [16-235]) but clamping the values into [0-255] seems to produce acceptable results.
71  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
72  * (thus this is a 24bit RGB with one byte per color) line by line.
73  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
74  * line
75  * @param width Width of the image contained in the RGB buffer
76  * @param height Height of the image contained in the RGB buffer
77  */
78 void rgb_to_yuv411packed_plainc(const unsigned char *RGB, unsigned char *YUV,
79  unsigned int width, unsigned int height);
80 
81 /* Convert a line of a RGB buffer to a line in a planar YUV422 buffer, see above for general
82  * notes about color space conversion from RGB to YUV
83  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
84  * (thus this is a 24bit RGB with one byte per color) line by line.
85  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
86  * line
87  * @param width Width of the image contained in the RGB buffer
88  * @param height Height of the image contained in the RGB buffer
89  * @param rgb_line the index of the line to be converted
90  * @param yuv_line the index of the line to convert to in the YUV buffer
91  */
92 void convert_line_rgb_to_yuv422planar(const unsigned char *RGB, unsigned char *YUV,
93  unsigned int width, unsigned int height,
94  unsigned int rgb_line, unsigned int yuv_line);
95 
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, unsigned char *YUV,
107  unsigned int width, unsigned int height);
108 
109 /* Convert a planar RGB buffer to a packed YUV422 buffer.
110  * See above for general notes about color space
111  * conversion from RGB to YUV
112  * @param RGB unsigned char array that contains the color planes
113  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
114  * line
115  * @param width Width of the image contained in the RGB buffer
116  * @param height Height of the image contained in the RGB buffer
117  */
118 void rgb_planar_to_yuv422packed_plainc(const unsigned char *rgb_planar, unsigned char *YUV,
119  unsigned int width, unsigned int height);
120 
121 /* Convert a line of a RGB buffer to a line in a packed YUV422 buffer, see above for general
122  * notes about color space conversion from RGB to YUV
123  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
124  * (thus this is a 24bit RGB with one byte per color) line by line.
125  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
126  * line
127  * @param width Width of the image contained in the RGB buffer
128  * @param height Height of the image contained in the RGB buffer
129  * @param rgb_line the index of the line to be converted
130  * @param yuv_line the index of the line to convert to in the YUV buffer
131  */
132 void convert_line_rgb_to_yuv422packed(const unsigned char *RGB, unsigned char *YUV,
133  unsigned int width, unsigned int height,
134  unsigned int rgb_line, unsigned int yuv_line);
135 
136 /* Convert an RGB buffer to a packed YUV422 buffer, see above for general notes about color space
137  * conversion from RGB to YUV
138  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
139  * (thus this is a 24bit RGB with one byte per color) line by line.
140  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
141  * line
142  * @param width Width of the image contained in the RGB buffer
143  * @param height Height of the image contained in the RGB buffer
144  */
145 void rgb_to_yuv422packed_plainc(const unsigned char *RGB, unsigned char *YUV,
146  unsigned int width, unsigned int height);
147 
148 /* Convert an BGR buffer to a planar YUV422 buffer, see above for general notes about color space
149  * conversion from RGB to YUV
150  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
151  * (thus this is a 24bit RGB with one byte per color) line by line.
152  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
153  * line
154  * @param width Width of the image contained in the RGB buffer
155  * @param height Height of the image contained in the RGB buffer
156  */
157 void bgr_to_yuv422planar_plainc(const unsigned char *BGR, unsigned char *YUV,
158  unsigned int width, unsigned int height);
159 
160 
161 } // end namespace firevision
162 
163 #endif