core/vidl_vil1/vidl_vil1_yuv_2_rgb.h

Go to the documentation of this file.
00001 // This is core/vidl_vil1/vidl_vil1_yuv_2_rgb.h
00002 #ifndef vidl_vil1_yuv_2_rgb_h_
00003 #define vidl_vil1_yuv_2_rgb_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author fsm
00010 //
00011 // this file has been copied over from oxl/oxp and optimized a little bit.
00012 // i experimented with  a vul_timer to shave as many milliseconds off as possible.
00013 // l.e.galup  7-15-02
00014 //
00015 // \verbatim
00016 // RGB to YUV Conversion
00017 //
00018 //      Y  =      (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
00019 //      Cr = V =  (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
00020 //      Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128
00021 //
00022 // YUV to RGB Conversion
00023 //
00024 //      B = 1.164(Y - 16)                  + 2.018(U - 128)
00025 //      G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
00026 //      R = 1.164(Y - 16) + 1.596(V - 128)
00027 
00028 // Alternative:
00029 //      Y = 0.299R + 0.587G + 0.114B
00030 //      U'= (B-Y)*0.565
00031 //      V'= (R-Y)*0.713
00032 //
00033 // with reciprocal versions:
00034 //
00035 //      R = Y + 1.403V'
00036 //      G = Y - 0.344U' - 0.714V'
00037 //      B = Y + 1.770U'
00038 //
00039 // \endverbatim
00040 
00041 #include <vil1/vil1_rgb_byte.h>
00042 
00043 const int c1164 = int(1.164 * 1024);
00044 const int c1596 = int(1.596 * 1024);
00045 const int c0813 = int(0.813 * 1024);
00046 const int c0391 = int(0.391 * 1024);
00047 const int c2018 = int(2.018 * 1024);
00048 
00049 inline
00050 unsigned char vidl_vil1_yuv_2_rgb_byte_clamp(int x)
00051 {
00052   x = x >> 10;
00053   if (x < 0) return 0;
00054   if (x > 255) return 255;
00055   return (unsigned char) x;
00056 }
00057 
00058 inline
00059 void vidl_vil1_yuv_2_rgb(unsigned char y, unsigned char u, unsigned char v, unsigned char* rgb)
00060 {
00061   // int ym16 = c1164*(y-16); replacing this into the below actually takes longer. go figure.
00062   rgb[0] = vidl_vil1_yuv_2_rgb_byte_clamp(c1164*(y - 16) + c1596 * (v - 128));
00063   rgb[1] = vidl_vil1_yuv_2_rgb_byte_clamp(c1164*(y - 16) - c0813 * (v - 128) - c0391 * (u - 128));
00064   rgb[2] = vidl_vil1_yuv_2_rgb_byte_clamp(c1164*(y - 16) + c2018 * (u - 128));
00065 }
00066 
00067 inline
00068 void vidl_vil1_yuv_2_rgb(unsigned char y, unsigned char u, unsigned char v, vil1_rgb_byte &vrgb)
00069 {
00070   vrgb.r = vidl_vil1_yuv_2_rgb_byte_clamp(c1164 * (y - 16) + c1596 * (v - 128));
00071   vrgb.g = vidl_vil1_yuv_2_rgb_byte_clamp(c1164 * (y - 16) - c0813 * (v - 128) - c0391 * (u - 128));
00072   vrgb.b = vidl_vil1_yuv_2_rgb_byte_clamp(c1164 * (y - 16) + c2018 * (u - 128));
00073 }
00074 
00075 #endif // vidl_vil1_yuv_2_rgb_h_

Generated on Tue Dec 2 05:09:13 2008 for core/vidl_vil1 by  doxygen 1.5.1