The key to generating a BMP file is to configure the file header, information header, and the corresponding palette. In rgb565 format, a pixel is represented by a word (two bytes), followed by r-5 bits G-6 bits B-5 bits.
In fact, it is better to set the file header and the corresponding information header step by step. The key is to set the color palette. According
# Define rgb565_mask_red 0xf800 # define rgb565_mask_green 0x07e0 # define rgb565_mask_blue 0x001f
You can combine mask and displacement operations to render the entire graph.
Bitmapfileheader BFH;
// Memset (& BFH, 0, sizeof (bitmapfileheader ));
BFH. bftype = 0x4d42; // "BM"
BFH. bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + sizeof (rgbquad) * 3; // + dwrgbquadsize;
BFH. bfsize = BFH. bfoffbits + imagesize; // + dwrgbquadsize; // 614454
BFH. bfreserved1 = 0;
BFH. bfreserved2 = 0;
Bitmapinfo * BMP info = (bitmapinfo *) malloc (sizeof (bitmapinfo) + sizeof (rgbquad) * 3 );
Memset (BMP info, 0, sizeof (bitmapinfo) + sizeof (rgbquad) * 3 );
BMP Info-> bmiheader. bisize = sizeof (bitmapinfoheader );
BMP Info-> bmiheader. biwidth = width;
BMP Info-> bmiheader. biheight =-height;
BMP Info-> bmiheader. bibitcount = bitcount;
BMP Info-> bmiheader. biplanes = 1;
BMP Info-> bmiheader. bicompression = bi_bitfields; // bitcompression; // 3
BMP Info-> bmicolors [0]. rgbblue = 0;
BMP Info-> bmicolors [0]. rgbgreen = 0xf8;
BMP Info-> bmicolors [0]. rgbred = 0;
BMP Info-> bmicolors [0]. rgbreserved = 0;
BMP Info-> bmicolors [1]. rgbblue = 0xe0;
BMP Info-> bmicolors [1]. rgbgreen = 0x07;
BMP Info-> bmicolors [1]. rgbred = 0;
BMP Info-> bmicolors [1]. rgbreserved = 0;
BMP Info-> bmicolors [2]. rgbblue = 0x1f;
BMP Info-> bmicolors [2]. rgbgreen = 0;
BMP Info-> bmicolors [2]. rgbred = 0;
BMP Info-> bmicolors [2]. rgbreserved = 0;
File * finput;
Finput = _ tfopen (L "a.bmp", l "W + B ");
Fwrite (& BFH, sizeof (bitmapfileheader), 1, finput );
Fwrite (BMP info, sizeof (bitmapinfo) + 3 * sizeof (rgbquad), 1, finput );
Fwrite (pbufferout, imagesize, 1, finput );
Fclose (finput );
Free (BMP info );
Finput = NULL;