C ++:. Bmp for byte array in file

Yes, I went through other questions related to this, but I found them not very useful. They helped, but I'm still a little confused. So what I need to do:

We have a 132x65 screen. I have 132x65.bmp. I want to go through .bmp and split it into small 1x8 columns to get the binary of this 32 bit column. Then do it 132 times, and do it 9 times. Everything that is not white should be considered a little. Example:

If there is any color in the upper left pixel of the image that is not white, but 7 pixels below white, then this will be the first element of the array, hexadecimal of this number, so the array will look like this :: array [] = {0x01}, and then it will continue to populate these 132 columns and then repeat this for 9 “sections” of rows. And the result of the file will be ONLY this array in a separate file.

I understand the header format for this, I read the wiki article on .bmp file formats, my main problem is that I really don’t know how to interact with .bmp when I really want it to go inside and interact with every pixel from Images. I really don't need all of this, but maybe just an example of capturing each pixel from .bmp and outputting the color of the pixel to a file or something like that. My C ++ is a bit rusty (doing java and javscript lately).

+1
source share
2 answers

If you want to read a BMP of a known format and do not care about how this is done (i.e., for internal use only), you can simply take a BMP, ignore the header and use it as an array of pixels. It is saved line by line, starting from the bottom left corner. There are a few details for how they are packaged, but in my experience, if you take a 32-bit image, you can completely ignore it.

As a really simple example:

unsigned int *buffer; void readfile() { FILE *f = fopen("file.bmp", "rb"); buffer = new unsigned int[132*65]; fseek(f, 54); fread(buffer, 132*65*4, 1, f); fclose(f); } unsigned int getpixel(int x, int y) { //assuming your x/y starts from top left, like I usually do return buffer[(64 - y) * 132 + x]; } 
+4
source

I had the same problem, but after reading the description of the BMP file format, I wrote a function that reads the .BMP file and stores it in an array. Perhaps this feature may help you:

 unsigned int PIC::BinToNum(char *b,int bytes) { unsigned int tmpx = 0; unsigned int pw = 1; for(int i=0;i<bytes;i++) { tmpx += ((unsigned char)b[i]* pw); pw = pw * 256; } return tmpx; } int PIC::Open(const char *path) { int pad = 0; unsigned int sof = 0; unsigned int tx = 0; char tmp[4] = {0,0,0,0}; fstream file; file.open(path,ios::in); if(file.fail()) { width=height=ColorBits=size=0; return -1; } else { file.seekg(0,ios::beg); file.read(tmp,2); if(!(tmp[0] == 66 && tmp[1] == 77)) { width=height=ColorBits=size=0; return 0; } else { file.seekg(2,ios::beg); // 0x2 size file.read(tmp,4); size = BinToNum(tmp,4); file.seekg(18,ios::beg); // 0x12 width file.read(tmp,4); width = BinToNum(tmp,4); file.seekg(22,ios::beg); // 0x16 height file.read(tmp,4); height = BinToNum(tmp,4); file.seekg(28,ios::beg); // 0x1C Bits per Pixel file.read(tmp,2); ColorBits = BinToNum(tmp,2); file.seekg(10,ios::beg); // 0x0A start offset file.read(tmp,4); sof=BinToNum(tmp,4); file.seekg(34,ios::beg); // 0x22 Padding file.read(tmp,4); pad = BinToNum(tmp,4); pad = (int)(pad / height); // Compute Spacing in each row pad = pad - (width*ColorBits/8); // Initialize Matrix// matrix = new(unsigned int[height*width]); for(int h=height-1;h>=0;h--) { for(int w=0;w<=width-1;w++) { file.seekg(sof,ios::beg); file.read(tmp,(int)(ColorBits/8)); tx = BinToNum(tmp,(int)(ColorBits/8)); matrix[(h*width)+w] = tx; sof+=(int)(ColorBits/8); } sof +=pad; } } } file.close(); return 1; } Note:This functions is member of a class that i named it "PIC"... 
+1
source

All Articles