4,499
社区成员




void ConvertYUVToRGB(unsigned char *yuv, unsigned char *rgb, int width, int height)
{
int R,G,B;
int Y,U,V;
int x,y;
int nWidth = width>>1;
unsigned char *yuv420[3];
yuv420[0] = yuv;
yuv420[1] = yuv + width*height;
yuv420[2] = yuv + width*height*5/4;
//色度信号宽度
for (y=0;y<height;y++)
{
for (x=0;x<width;x++)
{
Y = *(yuv420[0] + y*width + x);
U = *(yuv420[1] + ((y>>1)*nWidth) + (x>>1));
V = *(yuv420[2] + ((y>>1)*nWidth) + (x>>1));
B= 1.164 * (Y - 16) + 2.018 * (U - 128);
G= 1.164 * (Y - 16) - 0.38 * (U - 128) - 0.813 * (V - 128);
R= 1.164 * (Y - 16) ;
//防止越界
if (R>255)R=255;
if (R<0)R=0;
if (G>255)G=255;
if (G<0)G=0;
if (B>255)B=255;
if (B<0)B=0;
*(rgb + ((height-y-1)*width + x)*3) = R;
*(rgb + ((height-y-1)*width + x)*3 + 1) = G;
*(rgb + ((height-y-1)*width + x)*3 + 2) = B;
}
}
}
void RGB2BMP(unsigned char *rgb_buffer,int nWidth,int nHeight,FILE*fp)
{
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef struct
{
long imageSize;
long blank;
long startPosition;
}BmpHead;
typedef struct
{
long Length;
long width;
long height;
WORD colorPlane;
WORD bitColor;
long zipFormat;
long realSize;
long xPels;
long yPels;
long colorUse;
long colorImportant;
}InfoHead;
typedef struct
{
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
}RGBMixPlate;
BmpHead m_BMPHeader;
InfoHead m_BMPInfoHeader;
char bfType[2]={'B','M'};
m_BMPHeader.imageSize=3*nWidth*nHeight+54;
m_BMPHeader.blank=0;
m_BMPHeader.startPosition=54;
fwrite(bfType,1,sizeof(bfType),fp);
fwrite(&m_BMPHeader.imageSize,1,sizeof(m_BMPHeader.imageSize),fp);
fwrite(&m_BMPHeader.blank,1,sizeof(m_BMPHeader.blank),fp);
fwrite(&m_BMPHeader.startPosition,1,sizeof(m_BMPHeader.startPosition),fp);
m_BMPInfoHeader.Length=40;
m_BMPInfoHeader.width=nWidth;
m_BMPInfoHeader.height=nHeight;
m_BMPInfoHeader.colorPlane=1;
m_BMPInfoHeader.bitColor=24;
m_BMPInfoHeader.zipFormat=0;
m_BMPInfoHeader.realSize=3*nWidth*nHeight;
m_BMPInfoHeader.xPels=0;
m_BMPInfoHeader.yPels=0;
m_BMPInfoHeader.colorUse=0;
m_BMPInfoHeader.colorImportant=0;
fwrite(&m_BMPInfoHeader.Length,1,sizeof(m_BMPInfoHeader.Length),fp);
fwrite(&m_BMPInfoHeader.width,1,sizeof(m_BMPInfoHeader.width),fp);
fwrite(&m_BMPInfoHeader.height,1,sizeof(m_BMPInfoHeader.height),fp);
fwrite(&m_BMPInfoHeader.colorPlane,1,sizeof(m_BMPInfoHeader.colorPlane),fp);
fwrite(&m_BMPInfoHeader.bitColor,1,sizeof(m_BMPInfoHeader.bitColor),fp);
fwrite(&m_BMPInfoHeader.zipFormat,1,sizeof(m_BMPInfoHeader.zipFormat),fp);
fwrite(&m_BMPInfoHeader.realSize,1,sizeof(m_BMPInfoHeader.realSize),fp);
fwrite(&m_BMPInfoHeader.xPels,1,sizeof(m_BMPInfoHeader.xPels),fp);
fwrite(&m_BMPInfoHeader.yPels,1,sizeof(m_BMPInfoHeader.yPels),fp);
fwrite(&m_BMPInfoHeader.colorUse,1,sizeof(m_BMPInfoHeader.colorUse),fp);
fwrite(&m_BMPInfoHeader.colorImportant,1,sizeof(m_BMPInfoHeader.colorImportant),fp);
fwrite(rgb_buffer,3*nWidth*nHeight,1,fp);
}
生成的bmp图片有的地方一片模糊的红 有的一片模糊的蓝