yuv.h

00001
00002 /***************************************************************************
00003  *  yuv.h - YUV specific methods, macros and constants
00004  *
00005  *  Created: Sat Aug 12 14:36:28 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_YUV_H
00026 #define __FIREVISION_UTILS_COLOR_YUV_H
00027 
00028
00029 #define YUV422PA_MACROPIXEL_AT(YUV, width, x, y) ((unsigned char*)YUV + (y)*(width)*2 + ((x)-((x)%2))*2)
00030 
00031 #define YUV422_PLANAR_Y_AT(YUV, width, x, y)    \
00032   *(YUV + (y) * (width) + (x));
00033 
00034 #define YUV422_PLANAR_U_AT(YUV, width, height, x, y)            \
00035   *(YUV + ((width) * (height)) + (((y) * (width) + (x))/ 2));
00036 
00037 #define YUV422_PLANAR_V_AT(YUV, width, height, x, y)                    \
00038   *(YUV + ((width) * (height)) + (((width) * (height) + (y) * (width) + (x)) / 2)); \
00039 
00040 #define YUV422_PLANAR_YUV(YUV, width, height, x, y, yp, up, vp)         \
00041   {                                                                     \
00042     yp = YUV422_PLANAR_Y_AT(YUV, width, x, y);                          \
00043     up = YUV422_PLANAR_U_AT(YUV, width, height, x, y);                  \
00044     vp = YUV422_PLANAR_V_AT(YUV, width, height, x, y);                  \
00045   }
00046 
00047 #define YUV422_PLANAR_U_PLANE(YUV, width, height) (YUV + (width) * (height))
00048 #define YUV422_PLANAR_V_PLANE(YUV, width, height) (YUV + ((width) * (height)) + ((width) * (height) / 2))
00049 
00050 /** YUV pixel. */
00051 typedef struct YUV_t_struct{
00052   unsigned char Y;      /**< Y component */
00053   unsigned char U;      /**< U component */
00054   unsigned char V;      /**< V component */
00055 
00056   /** Standard constructor
00057    * @param y Y component
00058    * @param u U component
00059    * @param v V component
00060    */
00061   YUV_t_struct(unsigned char y = 127, unsigned char u = 127, unsigned char v = 127)
00062   {
00063     Y = y;
00064     U = u;
00065     V = v;
00066   }
00067
00068   static YUV_t_struct white()   { return YUV_t_struct(255, 127, 127); } /**< @return white color */
00069   static YUV_t_struct black()   { return YUV_t_struct(  0, 127, 127); } /**< @return black color */
00070   static YUV_t_struct green()   { return YUV_t_struct( 64,  95,  85); } /**< @return green color */
00071   static YUV_t_struct cyan()    { return YUV_t_struct(178, 170,   0); } /**< @return cyan color */
00072   static YUV_t_struct magenta() { return YUV_t_struct(105, 212, 234); } /**< @return magenta color */
00073   static YUV_t_struct gray()    { return YUV_t_struct(127, 127, 127); } /**< @return gray color */
00074   static YUV_t_struct orange()  { return YUV_t_struct(150,  43, 202); } /**< @return orange color */
00075   static YUV_t_struct yellow()  { return YUV_t_struct(245,   0, 148); } /**< @return yellow color */
00076   static YUV_t_struct blue()    { return YUV_t_struct( 29, 255, 107); } /**< @return blue color */
00077   static YUV_t_struct red()     { return YUV_t_struct( 75,  85, 255); } /**< @return red color */
00078 } YUV_t;
00079
00080 
00081 /** Convert IYU1 to IYU2
00082  * @param src src buffer
00083  * @param dest destination buffer
00084  * @param width image width
00085  * @param height image height
00086  */
00087 void iyu1_to_yuy2(const unsigned char *src, unsigned char *dest,
00088                   unsigned int width, unsigned int height);
00089
00090 
00091 /** 8-Bit gray to YUY2 conversion
00092  * This function takes the gray value as Y and sets U and V to 128.
00093  */
00094 void gray8_to_yuy2(const unsigned char *src, unsigned char *dest,
00095                    unsigned int width, unsigned int height);
00096
00097 
00098 /** 8-Bit gray to YUV422_PLANAR
00099  */
00100 void gray8_to_yuv422planar_plainc(const unsigned char *src, unsigned char *dst,
00101                                   unsigned int width, unsigned int height);
00102
00103 
00104 /** Copy part of the U anv V planes of a YUV422planar image to another
00105  */
00106 void yuv422planar_copy_uv(const unsigned char *src, unsigned char *dst,
00107                           unsigned int width, unsigned int height,
00108                           unsigned int x, unsigned int y,
00109                           unsigned int copy_width, unsigned int copy_height);
00110
00111
00112 
00113 /** Convert YUV422_PLANAR images to YUV422_PACKED
00114  */
00115 void yuv422planar_to_yuv422packed(const unsigned char *planar, unsigned char *packed,
00116                                   unsigned int width, unsigned int height);
00117 
00118 /** Convert YUV422_PLANAR_QUARTER images to YUV422_PACKED
00119  */
00120 void yuv422planar_quarter_to_yuv422packed(const unsigned char *planar,
00121                                           unsigned char *packed,
00122                                           const unsigned int width,
00123                                           const unsigned int height);
00124
00125 
00126 /** Convert YUV422_PACKED images to YUV422_PLANAR
00127  */
00128 void yuv422packed_to_yuv422planar(const unsigned char *packed, unsigned char *planar,
00129                                   unsigned int width, unsigned int height);
00130 
00131 /** Convert YUY2 images to YUV422_PLANAR
00132  */
00133 void yuy2_to_yuv422planar(const unsigned char *packed, unsigned char *planar,
00134                                   unsigned int width, unsigned int height);
00135 
00136 /** Convert YUY2 images to quarter-sized YUV422_PLANAR buffer.
00137  */
00138 void yuy2_to_yuv422planar_quarter(const unsigned char *packed, unsigned char *planar,
00139                                   const unsigned int width, const unsigned int height);
00140 
00141 /** Convert YVY2 images to YUV422_PLANAR
00142  */
00143 void yvy2_to_yuv422planar(const unsigned char *packed, unsigned char *planar,
00144                                   unsigned int width, unsigned int height);
00145 
00146 /** Convert YUV444_PACKED images to YUV422_PLANAR
00147  */
00148 void yuv444packed_to_yuv422planar(const unsigned char *yuv444, unsigned char *yuv422,
00149                                   unsigned int width, unsigned int height);
00150
00151 void yuv444packed_to_yuv422packed(const unsigned char *yuv444, unsigned char *yuv422,
00152                                   unsigned int width, unsigned int height);
00153
00154 void yvu444packed_to_yuv422planar(const unsigned char *yuv444, unsigned char *yuv422,
00155                                   unsigned int width, unsigned int height);
00156
00157 void yvu444packed_to_yuv422packed(const unsigned char *yuv444, unsigned char *yuv422,
00158                                   unsigned int width, unsigned int height);
00159
00160
00161
00162 void yuv422planar_erase_y_plane(unsigned char *yuv, unsigned int width, unsigned int height);
00163
00164
00165 void yuv422planar_erase_u_plane(unsigned char *yuv, unsigned int width, unsigned int height);
00166
00167
00168 void yuv422planar_erase_v_plane(unsigned char *yuv, unsigned int width, unsigned int height);
00169
00170
00171 void grayscale_yuv422packed(const unsigned char *src,   unsigned char *dst,
00172                             unsigned int   width, unsigned int   height);
00173
00174
00175 void grayscale_yuv422planar(const unsigned char *src, unsigned char *dst,
00176                             unsigned int width, unsigned int height);
00177
00178
00179 inline void
00180 convert_line_yuv422planar_to_yuv444packed(const unsigned char *src, unsigned char *dst,
00181                                           unsigned int width, unsigned int height,
00182                                           unsigned int src_line, unsigned int dst_line)
00183 {
00184   register unsigned int i = 0;
00185   register YUV_t *y1, *y2;
00186   register const unsigned char *yp, *up, *vp;
00187
00188   yp = src + (width * src_line);
00189   up = YUV422_PLANAR_U_PLANE(src, width, height) + (width * src_line / 2);
00190   vp = YUV422_PLANAR_V_PLANE(src, width, height) + (width * src_line / 2);
00191
00192   dst += 3 * width * dst_line;
00193
00194   while (i < width) {
00195     y1 = (YUV_t *)dst;
00196     dst += 3;
00197     y2 = (YUV_t *)dst;
00198     dst += 3;
00199
00200     y1->Y = *yp++;
00201     y1->U = *up;
00202     y1->V = *vp;
00203
00204     y2->Y = *yp++;
00205     y2->U = *up++;
00206     y2->V = *vp++;
00207
00208     i += 2;
00209   }
00210 }
00211
00212 #endif