Guest User

BMP file writing

a guest
Feb 12th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. // mimeType = "image/bmp";
  2.  
  3. unsigned char file[14] = {
  4.     'B','M', // magic
  5.     0,0,0,0, // size in bytes
  6.     0,0, // app data
  7.     0,0, // app data
  8.     40+14,0,0,0 // start of data offset
  9. };
  10. unsigned char info[40] = {
  11.     40,0,0,0, // info hd size
  12.     0,0,0,0, // width
  13.     0,0,0,0, // heigth
  14.     1,0, // number color planes
  15.     24,0, // bits per pixel
  16.     0,0,0,0, // compression is none
  17.     0,0,0,0, // image bits size
  18.     0x13,0x0B,0,0, // horz resoluition in pixel / m
  19.     0x13,0x0B,0,0, // vert resolutions (0x03C3 = 96 dpi, 0x0B13 = 72 dpi)
  20.     0,0,0,0, // #colors in pallete
  21.     0,0,0,0, // #important colors
  22.     };
  23.  
  24. int w=waterfallWidth;
  25. int h=waterfallHeight;
  26.  
  27. int padSize  = (4-w%4)%4;
  28. int sizeData = w*h*3 + h*padSize;
  29. int sizeAll  = sizeData + sizeof(file) + sizeof(info);
  30.  
  31. file[ 2] = (unsigned char)( sizeAll    );
  32. file[ 3] = (unsigned char)( sizeAll>> 8);
  33. file[ 4] = (unsigned char)( sizeAll>>16);
  34. file[ 5] = (unsigned char)( sizeAll>>24);
  35.  
  36. info[ 4] = (unsigned char)( w   );
  37. info[ 5] = (unsigned char)( w>> 8);
  38. info[ 6] = (unsigned char)( w>>16);
  39. info[ 7] = (unsigned char)( w>>24);
  40.  
  41. info[ 8] = (unsigned char)( h    );
  42. info[ 9] = (unsigned char)( h>> 8);
  43. info[10] = (unsigned char)( h>>16);
  44. info[11] = (unsigned char)( h>>24);
  45.  
  46. info[24] = (unsigned char)( sizeData    );
  47. info[25] = (unsigned char)( sizeData>> 8);
  48. info[26] = (unsigned char)( sizeData>>16);
  49. info[27] = (unsigned char)( sizeData>>24);
  50.  
  51. stream.write( (char*)file, sizeof(file) );
  52. stream.write( (char*)info, sizeof(info) );
  53.  
  54. unsigned char pad[3] = {0,0,0};
  55.  
  56. for ( int y=0; y<h; y++ )
  57. {
  58.     for ( int x=0; x<w; x++ )
  59.     {
  60.         long red = lround( 255.0 * waterfall[x][y] );
  61.         if ( red < 0 ) red=0;
  62.         if ( red > 255 ) red=255;
  63.         long green = red;
  64.         long blue = red;
  65.  
  66.         unsigned char pixel[3];
  67.         pixel[0] = blue;
  68.         pixel[1] = green;
  69.         pixel[2] = red;
  70.  
  71.         stream.write( (char*)pixel, 3 );
  72.     }
  73.     stream.write( (char*)pad, padSize );
  74. }
Advertisement
Add Comment
Please, Sign In to add comment