// START_YUV2RGB
unsigned char* rgb_image = new unsigned char[nYStride * nHeight * 3];
double r;
double g;
double b;
int yValue;
int uValue;
int vValue;
Gray
//for (int j = 0; j < nHeight;j++)
//{
// for (int i = 0; i < nYStride;i++)
// {
// yValue = pY[j * nYStride + i ];
// r = yValue;
// g = yValue;
// b = yValue;
// //This prevents colour distortions in your rgb image
// if (r < 0) r = 0;
// else if (r > 255) r = 255;
// if (g < 0) g = 0;
// else if (g > 255) g = 255;
// if (b < 0) b = 0;
// else if (b > 255) b = 255;
// //
// rgb_image[nYStride * nHeight * 3 - j * nYStride * 3 - (nYStride- i)*3] = (unsigned char)r;
// rgb_image[nYStride * nHeight * 3 - j * nYStride * 3 - (nYStride- i)*3 - 1] = (unsigned char)g;
// rgb_image[nYStride * nHeight * 3 - j * nYStride * 3 - (nYStride- i)*3 - 2] = (unsigned char)b;
// }
//}
// Color
for (int j = 0; j < nHeight/2; j++)
{
for (int i = 0; i < nYStride/2; i++)
{
uValue = pU[j * nYStride/2 + i];
vValue = pV[j * nYStride/2 + i];
//Pixel_1
yValue =pY[2*j * nYStride + 2*i];
r = yValue + (1.370705 * (vValue-128));
g = yValue - (0.698001 * (vValue-128)) - (0.337633 * (uValue-128));
b = yValue + (1.732446 * (uValue-128));
//This prevents colour distortions in your rgb image
if (r < 0) r = 0;
else if (r > 255) r = 255;
if (g < 0) g = 0;
else if (g > 255) g = 255;
if (b < 0) b = 0;
else if (b > 255) b = 255;
rgb_image[nYStride * nHeight * 3 - (2*j * nYStride + nYStride - 2*i)*3] = (unsigned char)b;
rgb_image[nYStride * nHeight * 3 - (2*j * nYStride + nYStride - 2*i)*3 + 1] = (unsigned char)g;
rgb_image[nYStride * nHeight * 3 - (2*j * nYStride + nYStride - 2*i)*3 + 2] = (unsigned char)r;
//Pixel_2
yValue =pY[2*j * nYStride + 2*i + 1];
r = yValue + (1.370705 * (vValue-128));
g = yValue - (0.698001 * (vValue-128)) - (0.337633 * (uValue-128));
b = yValue + (1.732446 * (uValue-128));
//This prevents colour distortions in your rgb image
if (r < 0) r = 0;
else if (r > 255) r = 255;
if (g < 0) g = 0;
else if (g > 255) g = 255;
if (b < 0) b = 0;
else if (b > 255) b = 255;
rgb_image[nYStride * nHeight * 3 - (2*j * nYStride + nYStride - 2*i - 1)*3] = (unsigned char)b;
rgb_image[nYStride * nHeight * 3 - (2*j * nYStride + nYStride - 2*i - 1)*3 + 1] = (unsigned char)g;
rgb_image[nYStride * nHeight * 3 - (2*j * nYStride + nYStride - 2*i - 1)*3 + 2] = (unsigned char)r;
//Pixel_3
yValue =pY[(2*j +1) * nYStride + 2*i];
r = yValue + (1.370705 * (vValue-128));
g = yValue - (0.698001 * (vValue-128)) - (0.337633 * (uValue-128));
b = yValue + (1.732446 * (uValue-128));
//This prevents colour distortions in your rgb image
if (r < 0) r = 0;
else if (r > 255) r = 255;
if (g < 0) g = 0;
else if (g > 255) g = 255;
if (b < 0) b = 0;
else if (b > 255) b = 255;
rgb_image[nYStride * nHeight * 3 - ((2*j +1) * nYStride + nYStride - 2*i)*3] = (unsigned char)b;
rgb_image[nYStride * nHeight * 3 - ((2*j +1) * nYStride + nYStride - 2*i)*3 + 1] = (unsigned char)g;
rgb_image[nYStride * nHeight * 3 - ((2*j +1) * nYStride + nYStride - 2*i)*3 + 2] = (unsigned char)r;
//Pixel_4
yValue =pY[(2*j +1) * nYStride + 2*i + 1];
r = yValue + (1.370705 * (vValue-128));
g = yValue - (0.698001 * (vValue-128)) - (0.337633 * (uValue-128));
b = yValue + (1.732446 * (uValue-128));
//This prevents colour distortions in your rgb image
if (r < 0) r = 0;
else if (r > 255) r = 255;
if (g < 0) g = 0;
else if (g > 255) g = 255;
if (b < 0) b = 0;
else if (b > 255) b = 255;
rgb_image[nYStride * nHeight * 3 - ((2*j +1) * nYStride + nYStride - 2*i - 1)*3] = (unsigned char)b;
rgb_image[nYStride * nHeight * 3 - ((2*j +1) * nYStride + nYStride - 2*i - 1)*3 + 1] = (unsigned char)g;
rgb_image[nYStride * nHeight * 3 - ((2*j +1) * nYStride + nYStride - 2*i - 1)*3 + 2] = (unsigned char)r;
}
}
// End_YUV2RGB
const char* filename=NULL;
char temp[20] = {0};
sprintf(temp,"%llu",nStamp);
string s1 = "E:/Picture/";
string s3 = ".bmp";
string s4 = temp;
string src = s1 + s4 + s3;
filename=src.c_str();
//START_RGB2BMP
MyBITMAPFILEHEADER bfh;
MyBITMAPINFOHEADER bih;
/* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */
unsigned short bfType=0x4d42;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfSize = 2+sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+nYStride*nHeight*3;
bfh.bfOffBits = 0x36;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = nYStride;
bih.biHeight = nHeight;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = 0;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 5000;
bih.biYPelsPerMeter = 5000;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
FILE *file = fopen(filename, "wb");
if (!file)
{
printf("Could not write file\n");
return;
}
/*Write headers*/
fwrite(&bfType,sizeof(bfType),1,file);
fwrite(&bfh,sizeof(bfh),1, file);
fwrite(&bih,sizeof(bih),1, file);
fwrite(rgb_image,nYStride*nHeight*3,1,file);
fclose(file);
//End_RGB2BMP