Save the data in rgb565 format as a bitmap

Source: Internet
Author: User

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;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.