Write a function used to store a picture for BMP format
Code is as follows:
Void WriteRGBBMPFile (char * filename, BYTE * imageRed [2000], BYTE * imageGreen [2000], BYTE * imageBlue [2000], int w1, int w2, int h1, int h2)//a part of the output images as RGB image file to BMP format
{
The FILE * fp=fopen (filename, "wb");
If (fp==NULL)
{
Printf (" unsuccessfully open % s ", filename);
return;
}
Int width=Max (w1 and w2) - min (w1 and w2) + 1;
Int height=Max (h1, h2) - min (h1, h2) + 1;
Int wh=width * 3;
If (wh % 4!=0)
Wh +=wh % 4;
BITMAPFILEHEADER fh.
Fh. BfOffBits=sizeof (BITMAPFILEHEADER) + sizeof (BITMAPFILEHEADER);
Fh. BfReserved1=fh. BfReserved2=0;
Fh. BfSize=sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + height * wh;
Fh. BfType=0 x4d42;
Fwrite (& amp; Fh, 1, sizeof (BITMAPFILEHEADER), fp);
BITMAPINFOHEADER fin;
Fin. BiBitCount=24;
Fin. BiClrImportant=0;
Fin. The biClrUsed=0;
Fin. BiCompression=0;
Fin. BiHeight=height;
Fin. BiPlanes=1;
Fin. BiSize=40;
Fin. BiSizeImage=height * wh;
Fin. BiWidth=width;
Fin. BiXPelsPerMeter=0;
Fin. BiYPelsPerMeter=0;
Fwrite (& amp; Fin, 1, sizeof (BITMAPINFOHEADER), fp);
for (int i=0; I & lt; Height; I++)
{
For (int j=0; J & lt; Width; J++)
{
Fwrite (imageBlue [h2 -i] + j + w1, 1, 1, fp);
Fwrite (imageGreen [h2 -i] + j + w1, 1, 1, fp);
Fwrite (imageRed [h2 -i] + j + w1, 1, 1, fp);
}
If ((width * 3) % 4!=0)
{
BYTE XTT=0;
For (int jj=0; Jj & lt; 4 - (width * 3) % 4; Jj++)
Fwrite (& amp; XTT, 1, 1, fp);
}
}
}
Change a code execution results can be normal output BMP images, can perfect opening in the drawing, as shown in figure
But when the image is opened in PS will appear the following situation
And then I in drawing software will save as BMP images, PS can be perfectly open,
Using the binary editor to view the files and found two file in BMP file header is slightly different, but using code generation of the original documents must be no problem, so drawing software in the save as it will change the image, what is this principle, can use PS perfect open code generated source files,