Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // create a BMP file
- // give a color to every pixels of the BMP file
- #include <iostream>
- using namespace std;
- typedef struct{
- unsigned short int signature;
- unsigned int size;
- unsigned short int reserved[2];
- unsigned int offset;
- }FileHeader;
- typedef struct{
- unsigned int headerSize;
- int width, height;
- unsigned short int plane;
- unsigned short int bits;
- unsigned int compression;
- unsigned int imageSize;
- int xPixel;
- int yPixel;
- unsigned int colorTable;
- unsigned int colorCount;
- }InfoHeader;
- int main(){
- // create a bmp-format file
- int width = 1024, height = 1024;
- FileHeader fileHeader = {
- 0x4d42, 54 + width * height * 4, {0, 0}, 54
- };
- InfoHeader infoHeader = {
- 40, width, height, 1, 32, 0, 64, 4, 4, 4, 4
- };
- FILE *fp;
- fp = fopen("myBMP05.bmp","wb");
- // write fileHeader into the file
- fwrite(&fileHeader.signature,sizeof(unsigned short int),1,fp);
- fwrite(&fileHeader.size,sizeof(unsigned int),1,fp);
- fwrite(&fileHeader.reserved,sizeof(unsigned short int),2,fp);
- fwrite(&fileHeader.offset,sizeof(unsigned int),1,fp);
- // write infoHeader into the file
- fwrite(&infoHeader.headerSize,sizeof(unsigned int),1,fp); // headerSize
- fwrite(&infoHeader.width,sizeof(int),1,fp); // width
- fwrite(&infoHeader.height,sizeof(int),1,fp); // height
- fwrite(&infoHeader.plane,sizeof(unsigned short int),1,fp); // plane
- fwrite(&infoHeader.bits,sizeof(unsigned short int),1,fp); // bits
- fwrite(&infoHeader.compression,sizeof(unsigned int),1,fp); // compression
- fwrite(&infoHeader.imageSize,sizeof(unsigned int),1,fp); // imageSize
- fwrite(&infoHeader.xPixel,sizeof(int),1,fp); // xPixel
- fwrite(&infoHeader.yPixel,sizeof(int),1,fp); // yPixel
- fwrite(&infoHeader.colorTable,sizeof(unsigned int),1,fp); // colorTable
- fwrite(&infoHeader.colorCount,sizeof(unsigned int),1,fp); // colorCount
- // put color in the file
- // BGRA
- unsigned char color[4] = {0,125,255,0};
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- fwrite(color,sizeof(unsigned char),4,fp);
- }
- }
- fclose(fp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement