yuv422.cpp
Go to the documentation of this file.
00001 #include "yuv422.h"
00002 
00003 #define CLIP_CHAR(c) ((c)>255?255:(c)<0?0:(c))
00004 
00005 namespace image_proc {
00006 
00007 void yuv422ToGray(const cv::Mat& yuv, cv::Mat& gray)
00008 {
00009   unsigned width = gray.cols;
00010   unsigned height = gray.rows;
00011   unsigned gray_skip = gray.step[0] - width;
00012   unsigned yuv_skip = yuv.step[0] - width*2;
00013   unsigned char* gray_buffer = gray.datastart;
00014   const unsigned char* yuv_buffer = yuv.datastart;
00015 
00016   // u y1 v y2
00017   for( unsigned yIdx = 0; yIdx < height;
00018        ++yIdx, gray_buffer += gray_skip, yuv_buffer += yuv_skip )
00019   {
00020     for( unsigned xIdx = 0; xIdx < width;
00021          ++xIdx, ++gray_buffer, yuv_buffer += 2 )
00022     {
00023       *gray_buffer = yuv_buffer[1];
00024     }
00025   }
00026 }
00027 
00028 void yuv422ToColor(const cv::Mat& yuv, cv::Mat& color)
00029 {
00030   unsigned width = color.cols;
00031   unsigned height = color.rows;
00032   unsigned bgr_skip = color.step[0] - width*3;
00033   unsigned yuv_skip = yuv.step[0] - width*2;
00034   unsigned char* bgr_buffer = color.datastart;
00035   const unsigned char* yuv_buffer = yuv.datastart;
00036 
00037   // 0  1   2  3
00038   // u  y1  v  y2
00039   for( unsigned yIdx = 0; yIdx < height;
00040        ++yIdx, bgr_buffer += bgr_skip, yuv_buffer += yuv_skip )
00041   {
00042     for( unsigned xIdx = 0; xIdx < width;
00043          xIdx += 2, bgr_buffer += 6, yuv_buffer += 4 )
00044     {
00045       int v = yuv_buffer[2] - 128;
00046       int u = yuv_buffer[0] - 128;
00047 
00048       bgr_buffer[0] =  CLIP_CHAR (yuv_buffer[1] + ((u * 33292 + 8192 ) >> 14));
00049       bgr_buffer[1] =  CLIP_CHAR (yuv_buffer[1] + ((v * -9519 - u * 6472 + 8192 ) >> 14));
00050       bgr_buffer[2] =  CLIP_CHAR (yuv_buffer[1] + ((v * 18678 + 8192 ) >> 14));
00051 
00052       bgr_buffer[3] =  CLIP_CHAR (yuv_buffer[3] + ((u * 33292 + 8192 ) >> 14));
00053       bgr_buffer[4] =  CLIP_CHAR (yuv_buffer[3] + ((v * -9519 - u * 6472 + 8192 ) >> 14));
00054       bgr_buffer[5] =  CLIP_CHAR (yuv_buffer[3] + ((v * 18678 + 8192 ) >> 14));
00055     }
00056   }
00057 }
00058 
00059 } // namespace image_proc


image_proc
Author(s): Patrick Mihelich, Kurt Konolige, Jeremy Leibs
autogenerated on Fri Jan 3 2014 11:24:35