C + + creates BMP files and writes data

Source: Internet
Author: User
Tags bmp image

Reference: HTTP://ZHIDAO.BAIDU.COM/LINK?URL=K0X3GJIIADHEJ3UHEJ8RTCEKRAPKN2B6DRKCEPKVSPNFEBRM345LTX7_VUJQDS9QYA-0MOTBMBLCOM-H_MD7SA

Reference: http://blog.csdn.net/lanbing510/article/details/8176231


Recently want to look at your own generated image, want to output to a BMP file.

1. bmp File Overview

BMP (Bitmap-file) graphic file, also known as bitmap file, is the graphics file format used by Windows, all image processing software running in Windows environment supports BMP image file format. Each image-drawing operation within the Windows system is based on BMP. A BMP file consists of four parts:

    • Bitmap file Header
    • Bitmap information Segment
    • Palette
    • Bitmap data

                A BMP file, which can be expressed in code, is as follows:

                typedef struct tagbitmap_file{

                Bitmapfileheader Bitmapheader;
                Bitmapinfoheader Bitmapinfoheader;
                PaletteEntry palette[256];
                UCHAR *buffer;

                } bitmap_file;
                Ii. writing of BMP files


                Writes a 32-bit data to BMP.

                #pragma pack (2)//must be written, otherwise sizeof cannot get the correct result typedef unsigned char byte;typedef unsigned short word;typedef unsigned long DWORD ; typedef long LONG;TYPEDEF struct {WORD Bftype;dword bfsize; WORD bfReserved1; WORD Bfreserved2;dword bfoffbits;} bitmapfileheader;typedef struct {DWORD bisize; LONG Biwidth; LONG Biheight; WORD biplanes; WORD Bibitcount;dword Bicompression;dword biSizeImage; LONG Bixpelspermeter; LONG Biypelspermeter;dword Biclrused;dword biclrimportant;} Bitmapinfoheader;void savebitmap (int w, int h,unsigned char *pdata,int ndatasize) {//Define BMP sizeconst int height = w; const int width = h;const int size = ndatasize;double x, y;int index;//part.1 Create Bitmap File headerbitmapfileheader F Ileheader;fileheader.bftype = 0x4d42;fileheader.bfreserved1 = 0;fileheader.bfreserved2 = 0;fileHeader.bfSize = sizeof ( Bitmapfileheader) + sizeof (bitmapinfoheader) + size;fileheader.bfoffbits = sizeof (Bitmapfileheader) + sizEOF (Bitmapinfoheader);//part.2 Create Bitmap Info headerbitmapinfoheader bitmapheader = {0};bitmapheader.bisize = Sizeo f (bitmapinfoheader); bitmapheader.biheight =-height;bitmapheader.biwidth = Width;bitmapheader.biplanes = 1; Bitmapheader.bibitcount = 32;bitmapheader.bisizeimage = size;bitmapheader.bicompression = 0; bi_rgb//Write to Filefile *output = fopen ("Output.bmp", "WB"), if (output = = NULL) {printf ("Cannot open file!\n");} Else{fwrite (&fileheader, sizeof (Bitmapfileheader), 1, output); fwrite (&bitmapheader, sizeof ( Bitmapinfoheader), 1, Output), fwrite (pData, size, 1, output); fclose (output);}}

                #pragma pack (2) must be added. The Bftype is 2 bytes, corresponding to "BM", the latter 4 bytes is the file size, and corresponds to 4 bytes. If you do not set the alignment, and by default 8 bytes or 4 bytes corresponding, these properties are misplaced, and others are read by the standard, the alignment is different, natural error. In general, programming does not consider these problems, because the reading and saving are done by you personally, the alignment is the same, naturally not wrong. The images you generate here may also be used by others, and it is natural to strictly abide by the standards.

                Writes a sinusoidal curve to a 24-bit BMP.

                void Savebitmap () {//Define BMP sizeconst int height = 600;const int width = 800;const int size = Height * Width * 3;doubl e x, Y;int index;//part.1 Create Bitmap File headerbitmapfileheader fileheader;fileheader.bftype = 0x4d42;fileheader.bfr eserved1 = 0;fileheader.bfreserved2 = 0;fileheader.bfsize = sizeof (Bitmapfileheader) + sizeof (bitmapinfoheader) + size; fileheader.bfoffbits = sizeof (Bitmapfileheader) + sizeof (bitmapinfoheader);//part.2 Create Bitmap Info Headerbitmapinfoheader Bitmapheader = {0};bitmapheader.bisize = sizeof (bitmapinfoheader); bitmapheader.biheight = Height;bitmapheader.biwidth = Width;bitmapheader.biplanes = 3;bitmapheader.bibitcount = 24;bitmapHeader.biSizeImage = size;bitmapheader.bicompression = 0; bi_rgb//part.3 Create databyte *bits = (BYTE *) malloc (size);//Clearmemset (Bits, 0xFF, size);//Sin graphfor (x = 0; x < 800; x + = 0.5) {y = sin (x/100.0) * + 300;index = (int) y * * 3 + (int) x * 3;bits[index + 0] = 255;//Bluebits[index +   1] = 0;Greenbits[index + 2] = 0; red}//Write to Filefile *output = fopen ("Output.bmp", "WB"), if (output = = NULL) {printf ("Cannot open file!\n");} Else{fwrite (&fileheader, sizeof (Bitmapfileheader), 1, output); fwrite (&bitmapheader, sizeof ( Bitmapinfoheader), 1, Output), fwrite (Bits, size, 1, output); fclose (output);}}

                Note the point:

                The height of the image, measured in pixels. This value, in addition to describing the height of the image, has another use to indicate whether the image is a backward bitmap or a forward bitmap. If the value is a positive number, the image is inverted, that is, the first line of data is actually the last line of the image, and if the value is a negative number, the image is positive. Most BMP files are inverted bitmaps, that is, when the height value is a positive number.

                C + + creates BMP files and writes data

                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.