Reputation: 3228
I want to read the RGB values of all pixels from a BMP file. I have code in c++, which look like:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
FILE *streamIn;
streamIn = fopen("./Untitled.bmp", "r");
if (streamIn == (FILE *)0) {
printf("File opening error ocurred. Exiting program.\n");
return 0;
}
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, streamIn);
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int image[width*height][3];
int i = 0;
for(i=0;i<width*height;i++) {
image[i][2] = getc(streamIn);
image[i][1] = getc(streamIn);
image[i][0] = getc(streamIn);
printf("pixel %d : [%d,%d,%d]\n",i+1,image[i][0],image[i][1],image[i][2]);
}
fclose(streamIn);
return 0;
}
and an image like this (grid overlaid):
which are 6x12 pixels file with two colors - black and white.
I try to find out why after executing the above code with the image as a parameter I don't get just pixels with RGB: [0,0,0] and [255,255,255] but also [0,248,0], [7,224,0] and others.
Hex dump of that file is:
0000-0010: 42 4d 9a 01-00 00 00 00-00 00 7a 00-00 00 6c 00 BM...... ..z...l.
0000-0020: 00 00 08 00-00 00 0c 00-00 00 01 00-18 00 00 00 ........ ........
0000-0030: 00 00 20 01-00 00 13 0b-00 00 13 0b-00 00 00 00 ........ ........
0000-0040: 00 00 00 00-00 00 42 47-52 73 00 00-00 00 00 00 ......BG Rs......
0000-0050: 00 00 00 00-00 00 00 00-00 00 00 00-00 00 00 00 ........ ........
0000-0060: 00 00 00 00-00 00 00 00-00 00 00 00-00 00 00 00 ........ ........
0000-0070: 00 00 00 00-00 00 00 00-00 00 02 00-00 00 00 00 ........ ........
0000-0080: 00 00 00 00-00 00 00 00-00 00 ff ff-ff ff ff ff ........ ........
0000-0090: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-00a0: ff ff ff ff-ff 00 00 00-00 00 00 00-00 00 00 00 ........ ........
0000-00b0: 00 00 00 00-00 00 00 ff-ff ff ff ff-ff 00 00 00 ........ ........
0000-00c0: 00 00 00 00-00 00 00 00-00 00 00 00-00 00 00 ff ........ ........
0000-00d0: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-00e0: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-00f0: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-0100: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-0110: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-0120: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-0130: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-0140: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-0150: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-0160: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-0170: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-0180: ff ff ff ff-ff ff ff ff-ff ff ff ff-ff ff ff ff ........ ........
0000-0190: ff ff 00 00-00 ff ff ff-00 00 00 ff-ff ff 00 00 ........ ........
0000-019a: 00 ff ff ff-00 00 00 ff-ff ff ........ ..
File size is 410 bytes. It should be 270 (6*12*3 + 54). That means that there is some extra information in this file.
Upvotes: 2
Views: 4294
Reputation: 7204
Your Bitmap file consists:
Bitmap File Header: 14 Bytes
42 4d 9a 01-00 00 00 00-00 00 7a 00-00 00
The last 4 bytes is the offset of where rgb values begin, 7A = after 122 Bytes
DIB header
The first 4 bytes
is the size:
6c 00 00 00 -> 108 Bytes so BITMAPV4HEADER
The next 8 Bytes are the width and height in pixels:
08 00-00 00 0c 00-00 00
width height each 4 Bytes
So your bitmap is not 6x12
but 8x12
! There is no need for padding because 8x3 = 24 which is multiple of 4.
The actual pixels are from 0x7A - 0x199
Beginning from the last row and going upwards, from left to right, the bitmap is:
The file size is correct:
file size = Bitmap File Header + DIB header + rgb Bytes = 14 + 108 + 8 * 12 * 3 = 410
To read a specific pixel in your image:
Load the whole bitmap file
char *buff;
char rgb[8 * 12 * 3]; //for simplicity
fptr = fopen("....bmp", "rb");
fseek(fptr, 0, SEEK_END);
int file_Size = ftell(fptr);
fseek(fptr, 0, SEEK_SET);
buff = malloc(file_Size * sizeof(char));
fread(buff, sizeof(char), file_Size, fptr);
memmove(rgb, buff[0x7A], 8 * 12 * 3);
Now rgb
contains the actual pixels but the order is left->right and down->up meaning the first pixel is the one at the left bottom of the image.
Upvotes: 5