Guest User

Reading BMP file pixels data using C++.

a guest
Nov 22nd, 2014
185
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5. inline int closestPadded(int width) //not in the mood to add this shit too, in short - width of bmp files has to be normalized to this.
  6. {
  7.     return 4*((width*3 + 3) / 4);
  8. }
  9.  
  10. struct Pixel //much better than dealing with coordinates!
  11. {
  12.     int r=0;
  13.     int g=0;
  14.     int b=0;
  15.  
  16. };
  17.    ostream & operator<< (ostream &wyjscie, const Pixel &s) {
  18.    return wyjscie << "\n--------"<<"\nR: " <<s.r << endl << "G: " << s.g << endl << "B: " << s.b;
  19.  } //lets add operator overloading so we can use cout << on our pixel
  20. int main()
  21. {
  22.     ifstream file("lena_1.bmp"); //it only reads files in which width is divisible by 4 (eg. 512). Feel warned. Only works on bmp files in 24 bit RGB format (tested on ones generated/edited with Gimp)
  23.     char * header = new char[54]; //54 bytes - header of the file.
  24.     if (file)
  25.     {
  26.         file.read(header, 54);
  27.     }
  28.     int width = *(int*)&header[18];
  29.     int height = *(int*)&header[22];
  30.     cout << width << "x" << height << endl;
  31.     int image_size = 3 * width * height;
  32.     char trashcan[68];
  33.     file.read(trashcan, 68); //no idea why but it seems next 68 bytes are useless, we dont want them!
  34.     unsigned char * data = new unsigned char[image_size]; //unsigned char cuz having 255 depicted as -1 is retarded
  35.     file.read(reinterpret_cast<char *>(data), image_size);
  36.  
  37.     for (int i=0; i<image_size; i+=3) //swap retarded bgr format to rgb...!
  38.     {
  39.         unsigned char temp = data[i];
  40.         data[i] = data[i+2];
  41.         data[i+2]=temp;
  42.     }
  43.     vector< vector<Pixel> > matrix(width); //apparently bmp files go as BGR from the BOTTOM, then from left to right and finally to the top. Which is retarded so we will transform this into standarized 2d matrix with [x][y] coordinates.
  44.     for (int i=0; i<width; i++)
  45.     {
  46.         vector<Pixel> row(height);
  47.         matrix[i]=row;
  48.     } //512x512 grid
  49.  
  50.  
  51.     int for_height=height-1;
  52.     int for_width=0;
  53.     for (int i=0; i < image_size; i+=3)
  54.     {
  55.         if (for_width>width-1) //change rows when necessary
  56.         {
  57.         for_height--;
  58.         for_width=0;
  59.         }
  60.         matrix[for_width][for_height].r = (int)data[i];
  61.         matrix[for_width][for_height].g = (int)data[i+1];
  62.         matrix[for_width][for_height].b = (int)data[i+2];
  63.         for_width++;
  64.     }
  65.     //some minor tests to see if this works properly
  66.     cout << matrix[0][511];
  67.     cout << matrix[1][511];
  68.     cout << matrix[0][0];
  69.     cout << matrix[1][0];
  70.     cout << matrix[2][0];
  71.  
  72.     return 0;
  73.  
  74. }
RAW Paste Data