如何将yuyv转换为rgb代码到yuv420转换为rgb

时间:2016-06-01 06:55:06

标签: c yuv

我一直在尝试找到yuv420到rgb的代码,但没有什么对我有用。我已经有一个代码为yuyv到rgb正在工作。所以我想在这段代码中进行更改,以便它可以像yuv一样工作到rgb。

这是yuyv到rgb代码:

static void YUV2RGB(const unsigned char y, const unsigned char u,const unsigned char v, unsigned char* r,
                unsigned char* g, unsigned char* b)
{
   const int y2 = (int)y;
   const int u2 = (int)u - 128;
   const int v2 = (int)v - 128;
   //std::cerr << "YUV=("<<y2<<","<<u2<<","<<v2<<")"<<std::endl;

   // This is the normal YUV conversion, but
   // appears to be incorrect for the firewire cameras
   //   int r2 = y2 + ( (v2*91947) >> 16);
   //   int g2 = y2 - ( ((u2*22544) + (v2*46793)) >> 16 );
   //   int b2 = y2 + ( (u2*115999) >> 16);
   // This is an adjusted version (UV spread out a bit)

   int r2 = y2 + ((v2 * 37221) >> 15);
   int g2 = y2 - (((u2 * 12975) + (v2 * 18949)) >> 15);
   int b2 = y2 + ((u2 * 66883) >> 15);
   //std::cerr << "   RGB=("<<r2<<","<<g2<<","<<b2<<")"<<std::endl;

   // Cap the values.
   *r = CLIPVALUE(r2);
   *g = CLIPVALUE(g2);
   *b = CLIPVALUE(b2);
}


static void yuyv2rgb(char *YUV, char *RGB, int NumPixels)
{
   int i, j;
   unsigned char y0, y1, u, v;
   unsigned char r, g, b;

   for (i = 0, j = 0; i < (NumPixels << 1); i += 4, j += 6)
   {
      y0 = (unsigned char)YUV[i + 0];
      u = (unsigned char)YUV[i + 1];
      y1 = (unsigned char)YUV[i + 2];
      v = (unsigned char)YUV[i + 3];
      YUV2RGB(y0, u, v, &r, &g, &b);
      RGB[j + 0] = r;
      RGB[j + 1] = g;
      RGB[j + 2] = b;
      YUV2RGB(y1, u, v, &r, &g, &b);
      RGB[j + 3] = r;
      RGB[j + 4] = g;
      RGB[j + 5] = b;
   }
}

我想将此代码转换为yuv420转换为rgb这就是为什么我想知道我应该做出的改变以使其工作。 谢谢

0 个答案:

没有答案
相关问题