Advertisement
thecplusplusguy

Save a bmp image from C++

Jul 6th, 2012
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. //Implemented by: http://www.youtube.com/user/thecplusplusguy
  2. //pure C++, compile, run, and it creates a bmp image (in the same folder, where the executable is), just as a siple demonstration
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <ctime>
  7. using namespace std;
  8.  
  9. struct bmpfile_magic {
  10.   unsigned char magic[2];
  11. };
  12.  
  13. struct bmpfile_header {
  14.   int filesz;
  15.   short creator1;
  16.   short creator2;
  17.   int bmp_offset;
  18. };
  19.  
  20. typedef struct {
  21.   int header_sz;
  22.   int width;
  23.   int height;
  24.   short nplanes;
  25.   short bitspp;
  26.   int compress_type;
  27.   int bmp_bytesz;
  28.   int hres;
  29.   int vres;
  30.   int ncolors;
  31.   int nimpcolors;
  32. } BITMAPINFOHEADER;
  33.  
  34. const int WIDTH=1023;
  35. const int HEIGHT=1023;
  36. const int BPP=32;
  37.  
  38. int main()
  39. {
  40.     srand(time(0));
  41.     std::ofstream out("ki.bmp",std::ios::binary);
  42.     bmpfile_magic bm={'B','M'};
  43.     bmpfile_header bh={54+((BPP*WIDTH)/8)*HEIGHT,0,0,54};
  44.     BITMAPINFOHEADER bhi={40,WIDTH,HEIGHT,1,BPP,0,((BPP*WIDTH)/8)*HEIGHT,2750,2750,0,0};
  45.     out.write((char*)&bm,sizeof(bm));
  46.     out.write((char*)&bh,sizeof(bh));
  47.     out.write((char*)&bhi,sizeof(bhi));
  48.     for(int i=0;i<HEIGHT;i++)
  49.         for(int j=0;j<WIDTH;j++)
  50.         {
  51.                 out.put((char)(i/4));
  52.                 out.put((char)(j/4));
  53.                 out.put((char)((i+j)/8));
  54.                 out.put((char)0);
  55.         }
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement