Advertisement
uopspop

BMPfile01(Basic)

Apr 15th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. // create a BMP file
  2. // give a color to every pixels of the BMP file
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. typedef struct{
  8.     unsigned short int signature;
  9.     unsigned int size;
  10.     unsigned short int reserved[2];
  11.     unsigned int offset;
  12. }FileHeader;
  13.  
  14. typedef struct{
  15.     unsigned int headerSize;
  16.     int width, height;
  17.     unsigned short int plane;
  18.     unsigned short int bits;
  19.     unsigned int compression;
  20.     unsigned int imageSize;
  21.     int xPixel;
  22.     int yPixel;
  23.     unsigned int colorTable;
  24.     unsigned int colorCount;
  25.    
  26. }InfoHeader;
  27.  
  28.  
  29. int main(){
  30.    
  31. // create a bmp-format file
  32.     int width = 1024, height = 1024;
  33.     FileHeader fileHeader = {
  34.         0x4d42, 54 + width * height * 4, {0, 0}, 54
  35.     };
  36.     InfoHeader infoHeader = {
  37.         40, width, height, 1, 32, 0, 64, 4, 4, 4, 4
  38.     };
  39.    
  40.     FILE *fp;
  41.     fp = fopen("myBMP05.bmp","wb");
  42.    
  43.     // write fileHeader into the file
  44.     fwrite(&fileHeader.signature,sizeof(unsigned short int),1,fp); 
  45.     fwrite(&fileHeader.size,sizeof(unsigned int),1,fp);
  46.     fwrite(&fileHeader.reserved,sizeof(unsigned short int),2,fp);  
  47.     fwrite(&fileHeader.offset,sizeof(unsigned int),1,fp);  
  48.  
  49.  
  50.     // write infoHeader into the file
  51.     fwrite(&infoHeader.headerSize,sizeof(unsigned int),1,fp);   // headerSize
  52.     fwrite(&infoHeader.width,sizeof(int),1,fp); // width
  53.     fwrite(&infoHeader.height,sizeof(int),1,fp); // height 
  54.     fwrite(&infoHeader.plane,sizeof(unsigned short int),1,fp); // plane
  55.     fwrite(&infoHeader.bits,sizeof(unsigned short int),1,fp); // bits
  56.     fwrite(&infoHeader.compression,sizeof(unsigned int),1,fp); // compression
  57.     fwrite(&infoHeader.imageSize,sizeof(unsigned int),1,fp);  // imageSize
  58.     fwrite(&infoHeader.xPixel,sizeof(int),1,fp); // xPixel
  59.     fwrite(&infoHeader.yPixel,sizeof(int),1,fp); // yPixel 
  60.     fwrite(&infoHeader.colorTable,sizeof(unsigned int),1,fp); // colorTable
  61.     fwrite(&infoHeader.colorCount,sizeof(unsigned int),1,fp); // colorCount
  62.  
  63.  
  64.  
  65. // put color in the file
  66.                         // BGRA
  67. unsigned char color[4] = {0,125,255,0};
  68. for (int i = 0; i < width; i++) {
  69.     for (int j = 0; j < height; j++) {
  70.         fwrite(color,sizeof(unsigned char),4,fp);
  71.     }
  72. }
  73.    
  74.     fclose(fp);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement