Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- 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.
- {
- return 4*((width*3 + 3) / 4);
- }
- struct Pixel //much better than dealing with coordinates!
- {
- int r=0;
- int g=0;
- int b=0;
- };
- ostream & operator<< (ostream &wyjscie, const Pixel &s) {
- return wyjscie << "\n--------"<<"\nR: " <<s.r << endl << "G: " << s.g << endl << "B: " << s.b;
- } //lets add operator overloading so we can use cout << on our pixel
- int main()
- {
- 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)
- char * header = new char[54]; //54 bytes - header of the file.
- if (file)
- {
- file.read(header, 54);
- }
- int width = *(int*)&header[18];
- int height = *(int*)&header[22];
- cout << width << "x" << height << endl;
- int image_size = 3 * width * height;
- char trashcan[68];
- file.read(trashcan, 68); //no idea why but it seems next 68 bytes are useless, we dont want them!
- unsigned char * data = new unsigned char[image_size]; //unsigned char cuz having 255 depicted as -1 is retarded
- file.read(reinterpret_cast<char *>(data), image_size);
- for (int i=0; i<image_size; i+=3) //swap retarded bgr format to rgb...!
- {
- unsigned char temp = data[i];
- data[i] = data[i+2];
- data[i+2]=temp;
- }
- 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.
- for (int i=0; i<width; i++)
- {
- vector<Pixel> row(height);
- matrix[i]=row;
- } //512x512 grid
- int for_height=height-1;
- int for_width=0;
- for (int i=0; i < image_size; i+=3)
- {
- if (for_width>width-1) //change rows when necessary
- {
- for_height--;
- for_width=0;
- }
- matrix[for_width][for_height].r = (int)data[i];
- matrix[for_width][for_height].g = (int)data[i+1];
- matrix[for_width][for_height].b = (int)data[i+2];
- for_width++;
- }
- //some minor tests to see if this works properly
- cout << matrix[0][511];
- cout << matrix[1][511];
- cout << matrix[0][0];
- cout << matrix[1][0];
- cout << matrix[2][0];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement