rgbyuv.h

00001
00002 /****************************************************************************
00003  *  rgbyuv.h - RGB to YUV conversion - specific methods, macros and constants
00004  *
00005  *  Created: Sat Aug 12 15:21:39 2006
00006  *  based on colorspaces.h from Tue Feb 23 13:49:38 2005
00007  *  Copyright  2005-2006  Tim Niemueller [www.niemueller.de]
00008  *
00009  ****************************************************************************/
00010
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version. A runtime exception applies to
00015  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00016  *
00017  *  This program is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU Library General Public License for more details.
00021  *
00022  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00023  */
00024
00025 #ifndef __FIREVISION_UTILS_COLOR_RGBYUV_H
00026 #define __FIREVISION_UTILS_COLOR_RGBYUV_H
00027 
00028
00029 #define RGB2YUV(r, g, b, y, u, v) {                             \
00030     y = (306*r + 601*g + 117*b)  >> 10;                         \
00031     u = ((-172*r - 340*g + 512*b) >> 10)  + 128;                \
00032     v = ((512*r - 429*g - 83*b) >> 10) + 128;                   \
00033     y = y < 0 ? 0 : y;                                          \
00034     u = u < 0 ? 0 : u;                                          \
00035     v = v < 0 ? 0 : v;                                          \
00036     y = y > 255 ? 255 : y;                                      \
00037     u = u > 255 ? 255 : u;                                      \
00038     v = v > 255 ? 255 : v; }
00039 
00040 /* Alternative from libdc1394
00041   y = (306*r + 601*g + 117*b)  >> 10;                   \
00042   u = ((-172*r - 340*g + 512*b) >> 10)  + 128;\
00043   v = ((512*r - 429*g - 83*b) >> 10) + 128;\
00044 
00045   Original:
00046   y = ((9798*(r) + 19235*(g) + 3736*(b))  >> 15);               \
00047   u = ((-4784*(r) - 9437*(g) + 14221*(b)) >> 15) + 128;         \
00048   v = ((20218*(r) - 16941*(g) - 3277*(b)) >> 15) + 128;         \
00049 
00050 */
00051
00052
00053 void rgb_to_yuy2(const unsigned char *RGB, unsigned char *YUV,
00054                  unsigned int width, unsigned int height);
00055
00056 
00057 /** RGB to YUV Conversion
00058  *
00059  * Y  =      (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
00060  * Cr = V =  (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
00061  * Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128
00062  *
00063  * Values have to be clamped to keep them in the [0-255] range.
00064  * Rumour has it that the valid range is actually a subset of [0-255] (fourcc.org mentions an RGB range
00065  * of [16-235]) but clamping the values into [0-255] seems to produce acceptable results.
00066  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
00067  *            (thus this is a 24bit RGB with one byte per color) line by line.
00068  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
00069  *            line
00070  * @param width Width of the image contained in the RGB buffer
00071  * @param height Height of the image contained in the RGB buffer
00072  */
00073 void rgb_to_yuv411packed_plainc(const unsigned char *RGB, unsigned char *YUV,
00074                                 unsigned int width, unsigned int height);
00075
00076 /* Convert a line of a RGB buffer to a line in a planar YUV422 buffer, see above for general
00077  * notes about color space conversion from RGB to YUV
00078  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
00079  *            (thus this is a 24bit RGB with one byte per color) line by line.
00080  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
00081  *            line
00082  * @param width Width of the image contained in the RGB buffer
00083  * @param height Height of the image contained in the RGB buffer
00084  * @param rgb_line the index of the line to be converted
00085  * @param yuv_line the index of the line to convert to in the YUV buffer
00086  */
00087 void convert_line_rgb_to_yuv422planar(const unsigned char *RGB, unsigned char *YUV,
00088                                       unsigned int width, unsigned int height,
00089                                       unsigned int rgb_line, unsigned int yuv_line);
00090
00091
00092 /* Convert an RGB buffer to a planar YUV422 buffer, see above for general notes about color space
00093  * conversion from RGB to YUV
00094  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
00095  *            (thus this is a 24bit RGB with one byte per color) line by line.
00096  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
00097  *            line
00098  * @param width Width of the image contained in the RGB buffer
00099  * @param height Height of the image contained in the RGB buffer
00100  */
00101 void rgb_to_yuv422planar_plainc(const unsigned char *RGB, unsigned char *YUV,
00102                                 unsigned int width, unsigned int height);
00103
00104 /* Convert a line of a RGB buffer to a line in a packed YUV422 buffer, see above for general
00105  * notes about color space conversion from RGB to YUV
00106  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
00107  *            (thus this is a 24bit RGB with one byte per color) line by line.
00108  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
00109  *            line
00110  * @param width Width of the image contained in the RGB buffer
00111  * @param height Height of the image contained in the RGB buffer
00112  * @param rgb_line the index of the line to be converted
00113  * @param yuv_line the index of the line to convert to in the YUV buffer
00114  */
00115 void convert_line_rgb_to_yuv422packed(const unsigned char *RGB, unsigned char *YUV,
00116                                       unsigned int width, unsigned int height,
00117                                       unsigned int rgb_line, unsigned int yuv_line);
00118
00119 /* Convert an RGB buffer to a packed YUV422 buffer, see above for general notes about color space
00120  * conversion from RGB to YUV
00121  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
00122  *            (thus this is a 24bit RGB with one byte per color) line by line.
00123  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
00124  *            line
00125  * @param width Width of the image contained in the RGB buffer
00126  * @param height Height of the image contained in the RGB buffer
00127  */
00128 void rgb_to_yuv422packed_plainc(const unsigned char *RGB, unsigned char *YUV,
00129                                 unsigned int width, unsigned int height);
00130
00131 /* Convert an BGR buffer to a planar YUV422 buffer, see above for general notes about color space
00132  * conversion from RGB to YUV
00133  * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel
00134  *            (thus this is a 24bit RGB with one byte per color) line by line.
00135  * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after
00136  *            line
00137  * @param width Width of the image contained in the RGB buffer
00138  * @param height Height of the image contained in the RGB buffer
00139  */
00140 void bgr_to_yuv422planar_plainc(const unsigned char *BGR, unsigned char *YUV,
00141                                 unsigned int width, unsigned int height);
00142
00143
00144
00145
00146 #endif