title: Reading BMP file - Getting data out C/C++
body: <p>Here's my first post, and first question. <br/><br/>
  Why shouldn't I use:<code>bmpFile.read((char*)(&amp;bmpImageData),bmpImageDataSize);</code> to read data block from BMP file, and how should I do this properly (I know about BGR triplets, but as for now I do not really care about them existing) EDIT: Maybe I should clarify something - as the code is right now it compiles pretty well, but stops working on the line provided higher.</p>
  
  <pre><code>#include "stdafx.h"
  #include "mybmp.h"
  
  using namespace std;
  int _tmain(int argc, _TCHAR* argv[])
  {
      filebuf bmpBuff;
      ifstream bmpFile("test.bmp", ios::binary);
      bmpBuff.open("test_zapis.bmp", ios::binary | ios::out);
      ostream bmpSaved(&amp;bmpBuff);
  
      unsigned long bmpSizeBuffer;
  
  
  
      bmpFile.seekg(2, ios::beg); // Missing BMP, DIB identification for good byte adjustment
      mybmp bmpHeader;
      bmpFile.read((char*)(&amp;bmpHeader),sizeof(mybmp));
      if(bmpHeader.FReadFileSize()&gt;0)
      {
          unsigned long bmpImageDataSize = bmpHeader.FReadFileSize()-bmpHeader.FReadOffset(); // Reading ImageData size
          char* bmpImageData = new char[bmpImageDataSize];
  
          bmpFile.seekg(bmpHeader.FReadOffset(),ios::beg); // Positioning pointer to ImageData start point
          bmpFile.read((char*)(&amp;bmpImageData),bmpImageDataSize); // This breaks down // Reading ImageData to bmpImageData buffer
  
          // Fun with buffer //
          for(int i = 0; i &lt; bmpImageDataSize; i++)
          {
              bmpImageData[i]++;
          }
          // Saving (...) //
      }
      else
      {
          cout &lt;&lt; "Plik nie zostal wczytany"&lt;&lt;endl;
      }
  
  
  
      return 0;
  }
  </code></pre>
  
  <p>My mybmp.h header:</p>
  
  <pre><code>    #pragma pack(push,1)
  class mybmp
  {
      unsigned long fileSize; // BMP overall filesize in bytes
      unsigned long reserved; // filled with 0
      unsigned long fileOffset; // Offset before Raster Data
      //---------------------------//
      unsigned long size; // Size of InfoHeader = 40
      unsigned long width; // overall image width
      unsigned long height; // overall image height;
      unsigned short planes; // = 1;
      unsigned short bitCounts; // Bits per Pixel
      unsigned long compression; // Type of compression
      unsigned long typeOfImage; // compressed size of Image. 0 if compression parameter = 0;
      unsigned long xPixelsPerM; // horizontal resolution - Pixels/Meter
      unsigned long yPixelsPerM; // vertical resolution - Pixels/Meter
      unsigned long colorsUsed; // Number of colors actually used
      unsigned long colorsImportant; // Number of important colors, 0 if all
      //--------------------------//
  
  public:
      mybmp(void);
      unsigned long FReadFileSize();
      void FPrintObject();
      unsigned long FReadOffset();
      ~mybmp(void);
  };
  
  #pragma pack(pop)
  </code></pre>