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 namespace firevision { 00029 #if 0 /* just to make Emacs auto-indent happy */ 00030 } 00031 #endif 00032 00033 00034 #define RGB2YUV(r, g, b, y, u, v) { \ 00035 y = (306*r + 601*g + 117*b) >> 10; \ 00036 u = ((-172*r - 340*g + 512*b) >> 10) + 128; \ 00037 v = ((512*r - 429*g - 83*b) >> 10) + 128; \ 00038 y = y < 0 ? 0 : y; \ 00039 u = u < 0 ? 0 : u; \ 00040 v = v < 0 ? 0 : v; \ 00041 y = y > 255 ? 255 : y; \ 00042 u = u > 255 ? 255 : u; \ 00043 v = v > 255 ? 255 : v; } 00044 00045 /* Alternative from libdc1394 00046 y = (306*r + 601*g + 117*b) >> 10; \ 00047 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\ 00048 v = ((512*r - 429*g - 83*b) >> 10) + 128;\ 00049 00050 Original: 00051 y = ((9798*(r) + 19235*(g) + 3736*(b)) >> 15); \ 00052 u = ((-4784*(r) - 9437*(g) + 14221*(b)) >> 15) + 128; \ 00053 v = ((20218*(r) - 16941*(g) - 3277*(b)) >> 15) + 128; \ 00054 00055 */ 00056 00057 00058 void rgb_to_yuy2(const unsigned char *RGB, unsigned char *YUV, 00059 unsigned int width, unsigned int height); 00060 00061 00062 /** RGB to YUV Conversion 00063 * 00064 * Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16 00065 * Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128 00066 * Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128 00067 * 00068 * Values have to be clamped to keep them in the [0-255] range. 00069 * Rumour has it that the valid range is actually a subset of [0-255] (fourcc.org mentions an RGB range 00070 * of [16-235]) but clamping the values into [0-255] seems to produce acceptable results. 00071 * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel 00072 * (thus this is a 24bit RGB with one byte per color) line by line. 00073 * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after 00074 * line 00075 * @param width Width of the image contained in the RGB buffer 00076 * @param height Height of the image contained in the RGB buffer 00077 */ 00078 void rgb_to_yuv411packed_plainc(const unsigned char *RGB, unsigned char *YUV, 00079 unsigned int width, unsigned int height); 00080 00081 /* Convert a line of a RGB buffer to a line in a planar YUV422 buffer, see above for general 00082 * notes about color space conversion from RGB to YUV 00083 * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel 00084 * (thus this is a 24bit RGB with one byte per color) line by line. 00085 * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after 00086 * line 00087 * @param width Width of the image contained in the RGB buffer 00088 * @param height Height of the image contained in the RGB buffer 00089 * @param rgb_line the index of the line to be converted 00090 * @param yuv_line the index of the line to convert to in the YUV buffer 00091 */ 00092 void convert_line_rgb_to_yuv422planar(const unsigned char *RGB, unsigned char *YUV, 00093 unsigned int width, unsigned int height, 00094 unsigned int rgb_line, unsigned int yuv_line); 00095 00096 00097 /* Convert an RGB buffer to a planar YUV422 buffer, see above for general notes about color space 00098 * conversion from RGB to YUV 00099 * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel 00100 * (thus this is a 24bit RGB with one byte per color) line by line. 00101 * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after 00102 * line 00103 * @param width Width of the image contained in the RGB buffer 00104 * @param height Height of the image contained in the RGB buffer 00105 */ 00106 void rgb_to_yuv422planar_plainc(const unsigned char *RGB, unsigned char *YUV, 00107 unsigned int width, unsigned int height); 00108 00109 /* Convert a line of a RGB buffer to a line in a packed YUV422 buffer, see above for general 00110 * notes about color space conversion from RGB to YUV 00111 * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel 00112 * (thus this is a 24bit RGB with one byte per color) line by line. 00113 * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after 00114 * line 00115 * @param width Width of the image contained in the RGB buffer 00116 * @param height Height of the image contained in the RGB buffer 00117 * @param rgb_line the index of the line to be converted 00118 * @param yuv_line the index of the line to convert to in the YUV buffer 00119 */ 00120 void convert_line_rgb_to_yuv422packed(const unsigned char *RGB, unsigned char *YUV, 00121 unsigned int width, unsigned int height, 00122 unsigned int rgb_line, unsigned int yuv_line); 00123 00124 /* Convert an RGB buffer to a packed YUV422 buffer, see above for general notes about color space 00125 * conversion from RGB to YUV 00126 * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel 00127 * (thus this is a 24bit RGB with one byte per color) line by line. 00128 * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after 00129 * line 00130 * @param width Width of the image contained in the RGB buffer 00131 * @param height Height of the image contained in the RGB buffer 00132 */ 00133 void rgb_to_yuv422packed_plainc(const unsigned char *RGB, unsigned char *YUV, 00134 unsigned int width, unsigned int height); 00135 00136 /* Convert an BGR buffer to a planar YUV422 buffer, see above for general notes about color space 00137 * conversion from RGB to YUV 00138 * @param RGB unsigned char array that contains the pixels, pixel after pixel, 3 bytes per pixel 00139 * (thus this is a 24bit RGB with one byte per color) line by line. 00140 * @param YUV where the YUV output will be written to, will have 4 pixels in 6 byte macro pixel, line after 00141 * line 00142 * @param width Width of the image contained in the RGB buffer 00143 * @param height Height of the image contained in the RGB buffer 00144 */ 00145 void bgr_to_yuv422planar_plainc(const unsigned char *BGR, unsigned char *YUV, 00146 unsigned int width, unsigned int height); 00147 00148 00149 } // end namespace firevision 00150 00151 #endif