A program that generates BMP in Linux

Source: Internet
Author: User
Many people may be confused about memory alignment. Recently, I helped them write test programs and used a BMP
Generator, write a relatively simple version, only for 24-bit real color, now the code is published

# Include <iostream>
Using namespace STD;
Typedef long bool;
Typedef long;
Typedef unsigned char byte;
Typedef unsigned long DWORD;
Typedef unsigned short word;
Typedef struct {
Word bftype; // 2
DWORD bfsize; // 4
Word bfreserved1; // 2
Word bfreserved2; // 2
DWORD bfoffbits; // 4
}__ Attribute _ (packed) filehead;
Typedef struct {
DWORD bisize; // 4
Long biwidth; // 4
Long biheight; // 4
Word biplanes; // 2
Word bibitcount; // 2
DWORD bicompress; // 4
DWORD bisizeimage; // 4
Long bixpelspermeter; // 4
Long biypelspermeter; // 4
DWORD biclrused; // 4
DWORD biclrimportant; // 4
}__ Attribute _ (packed) infohead;
/* Typedef struct
{
Unsigned char rgbblue;
Unsigned char rgbgreen;
Unsigned char rgbred;
Unsigned char rgbreserved;
} Rgbquad; // It may be useless */
Typedef struct
{
Byte B;
Byte g;
Byte R;
} Rgb_data; // RGB type
Int BMP _generator (char * filename, int width, int height, unsigned char * Data)
{
Filehead BMP _head;
Infohead BMP _info;
Int size = width * height * 3;
// Test Data
/* Rgb_test BMP _data [width] [height];
Int I, J;
For (I = 0; I <width; I ++)
For (j = 0; j {
BMP _data [I] [J]. G = BMP _data [I] [J]. B = 0;
BMP _data [I] [J]. r = 0xff;
}
*/
BMP _head.bftype = 0x4d42;
BMP _head.bfsize = size + sizeof (filehead) + sizeof (infohead); // 24 + head + info
No Quad
BMP _head.bfreserved1 = BMP _head.bfreserved2 = 0;
BMP _head.bfoffbits = bmp_head.bfSize-size;
// Finish the initial of head
BMP _info.bisize = 40;
BMP _info.biwidth = width;
BMP _info.biheight = height;
BMP _info.biplanes = 1;
BMP _info.bibitcount = 24;
BMP _info.bicompress = 0;
BMP _info.bisizeimage = size;
BMP _info.bixpelspermeter = 0;
BMP _info.biypelspermeter = 0;
BMP _info.biclrused = 0;
BMP _info.biclrimportant = 0;
// Finish the initial of infohead;
// Copy the data

// Fstream file3 (filename );
File * FP;
If (! (FP = fopen (filename, "WB") return 0;
// Zhunan 2.6.00: 58
Fwrite (& BMP _head, 1, sizeof (filehead), FP );
Fwrite (& BMP _info, 1, sizeof (infohead), FP );
Fwrite (data, 1, size, FP );
Fclose (FP );

Return 1;

}

Int main (INT argc, char ** argv)
{
Int I, J;
Rgb_data buffer [512] [512];
// Cout <"Usage: BMP _generator width height" <Endl;
Cout <"programmed by zhunan" <Endl;
Cout <"API guide:" <Endl;
Cout <"BMP _generator (char * filename, int width, int height, unsigned
Char * data); "<Endl;
Memset (buffer, 0, sizeof (buffer ));
// Please edit width and height here
// Zhunan testdata
// Hard Coding
For (I = 0; I <256; I ++)
For (j = 0; j <256; j ++)
{
// Buffer [I] [J]. G = buffer [I] [J]. B = 0x00;
Buffer [I] [J]. r = 0xff;
}
BMP _generator ("/home/zhunan/1234.bmp", 512,512, (byte *) buffer );
Return 1;
}

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.