Guest User

Untitled

a guest
Feb 1st, 2012
6,231
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum ImageType {
  2.     ImageType_RGB24 = 1,
  3.     ImageType_GRAY8 = 2,
  4.     ImageType_JPEG  = 3
  5. };
  6.  
  7. struct Image {
  8.     Image() : size_x(0), size_y(0), type(ImageType_GRAY8)
  9.     {}
  10.  
  11.     int size_x;
  12.     int size_y;
  13.  
  14.     int type; // @see ImageType
  15.  
  16.     std::vector<uint8x> pixels;
  17. };
  18.  
  19. static inline
  20. void saturate(int& value, int min_val, int max_val)
  21. {
  22.   if (value < min_val) value = min_val;
  23.   if (value > max_val) value = max_val;
  24. }
  25.  
  26. void convert_YUYV_to_RGB24(int size_x, int size_y, const uint8x* YUYV_ptr, Image& out)
  27. {
  28.   const int K1 = int(1.402f * (1 << 16));
  29.   const int K2 = int(0.714f * (1 << 16));
  30.   const int K3 = int(0.334f * (1 << 16));
  31.   const int K4 = int(1.772f * (1 << 16));
  32.  
  33.   // convert to RGB24
  34.   out.size_x = size_x;
  35.   out.size_y = size_y;
  36.   out.type = ImageType_RGB24;
  37.  
  38.   out.pixels.resize(size_x * size_y * 3); // 3 bytes per RGB24 pixel
  39.  
  40.   typedef uint8x T;
  41.   T* out_ptr = &out.pixels[0];
  42.   const int pitch = size_x * 2; // 2 bytes per one YU-YV pixel
  43.  
  44.   for (int y=0; y<size_y; y++) {
  45.     const uint8x* src = YUYV_ptr + pitch * y;
  46.     for (int x=0; x<size_x*2; x+=4) { // Y1 U Y2 V
  47.       uint8x Y1 = src[x + 0];
  48.       uint8x U  = src[x + 1];
  49.       uint8x Y2 = src[x + 2];
  50.       uint8x V  = src[x + 3];
  51.  
  52.       int8x uf = U - 128;
  53.       int8x vf = V - 128;
  54.  
  55.       int R = Y1 + (K1*vf >> 16);
  56.       int G = Y1 - (K2*vf >> 16) - (K3*uf >> 16);
  57.       int B = Y1 + (K4*uf >> 16);
  58.  
  59.       saturate(R, 0, 255);
  60.       saturate(G, 0, 255);
  61.       saturate(B, 0, 255);
  62.  
  63.       *out_ptr++ = T(R);
  64.       *out_ptr++ = T(G);
  65.       *out_ptr++ = T(B);
  66.  
  67.       R = Y2 + (K1*vf >> 16);
  68.       G = Y2 - (K2*vf >> 16) - (K3*uf >> 16);
  69.       B = Y2 + (K4*uf >> 16);
  70.  
  71.       saturate(R, 0, 255);
  72.       saturate(G, 0, 255);
  73.       saturate(B, 0, 255);
  74.  
  75.       *out_ptr++ = T(R);
  76.       *out_ptr++ = T(G);
  77.       *out_ptr++ = T(B);
  78.     }
  79.  
  80.   }
  81. }
RAW Paste Data