Advertisement
uopspop

BMPfile01(function)

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