bitmap - Load BMP pixel data into array C -
i'm trying read pixel data bitmap array of structured (which contains r, g , b ints, colours). i'm using 12x12px image testing (because believe bmp specification says pixel data padded zeros @ end of each line make word-aligned. 12px width automatically word aligned.
here code i'm using. infoheader
, fileheader
parsed perfectly, , 12x12 2d array of image
s im
created. printing rgb data each pixel prints rubbish.
typedef struct __attribute__((__packed__)) { unsigned char b; unsigned char g; unsigned char r; } image; int main(void) { fileheader fh; infoheader ih; file *img = fopen("pic.bmp", "rb"); fread(&fh, sizeof(unsigned char), sizeof(fileheader), img); fread(&ih, sizeof(unsigned char), sizeof(infoheader), img); image im[ih.width][ih.height]; fseek(img, fh.imagedataoffset, 0); int i, j; (i = ih.height-1; >= 0; i--) { (j = 0; j < ih.width; j++) { fread(&im[i][j], sizeof(unsigned char), sizeof(image), img); printf("%x%x%x ", im[i][j].r, im[i][j].g, im[i][j].b); } printf("\n"); } printf("w = %d, h = %d\n", ih.width, ih.height); }
however, 12x12px white bitmap, outputs:
ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff ffffff 000 ca800 ff574b 007f 001 000 b000 574bc9 07fff 44c40 ff8690 007f 9077b0 7fff86 8000 574bca 07fff 0a00 ff7482 007f 4bc950 7fff57 2f00 86906d 07fff 60a50 ff8690 007f 4bc9d0 7fff57 ee00 87418 000 c9300 ff574b 007f 4bc8f0 7fff57 700 868b3c 07fff 400 000 000 820a0 7fff74 3000 574bc9 07fff 6000 1877 000 0100 000 000 004 000 1000 000 000 820a0 7fff74 000 0010 000 6000 1874 000 4bc8d0 7fff57 b300 868b31 000 38b30 051f9 000 000 000 000 000 000 010 000 000 745069 018 f700 ffffff 181a4 080 000 000 01f5 0140 000 000
can see i'm doing wrong? alignment issue?
thanks
edit:
this screenshot of bottom of bitmap file. there seems 144 (12x12) white pixels (highlighted). last 2 zeros padding. still doesn't make sense program doesn't parse though.
ok turns out saved blank bmp 8bit instead of 24bit. 8bit uses colour table, , each pixel references entry in colour table data. 24bit bmp uses 3 bytes per pixel, , each byte represents r, g , b value, doesn't need colour table.
Comments
Post a Comment