Search code examples
bitmapgrayscale

8Bit Grayscale Bitmap to Array


I am wondering, from here how would I go about storing the pixel values into a matrix? I would like to print out the intensity (grayscale) value.

Example Array[5] = 4 Or Array[3][1] = 17

I really have no idea how to go about this. All the examples I see seem way to complicated, is there a simple way to do this?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct BitMap
{
short Type;
long Size;
short Reserve1;
short Reserve2;
long OffBits;
long biSize;
long biWidth;
long biHeight;
short biPlanes;
short biBitCount;
long biCompression;
long biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
long biClrUsed;
long biClrImportant;
}Header;

int main( void ){

    FILE *BMPFile = fopen ("MriHotrod.bmp", "rb");

    if(BMPFile == NULL){
        return;
    }

    unsigned char DataBuff[128*128];
    int i;
    int j;

    memset(&Header, 0, sizeof(Header));

    fread(&Header.Type, 2, 1, BMPFile);
    fread(&Header.Size, 4, 1, BMPFile);
    fread(&Header.Reserve1, 2, 1, BMPFile);
    fread(&Header.Reserve2, 2, 1, BMPFile);
    fread(&Header.OffBits, 4, 1, BMPFile);
    fread(&Header.biSize, 4, 1, BMPFile);
    fread(&Header.biWidth, 4, 1, BMPFile);
    fread(&Header.biHeight, 4, 1, BMPFile);
    fread(&Header.biPlanes, 2, 1, BMPFile);
    fread(&Header.biBitCount, 2, 1, BMPFile);
    fread(&Header.biCompression, 4, 1, BMPFile);
    fread(&Header.biSizeImage, 4, 1, BMPFile);
    fread(&Header.biXPelsPerMeter, 4, 1, BMPFile);
    fread(&Header.biYPelsPerMeter, 4, 1, BMPFile);
    fread(&Header.biClrUsed, 4, 1, BMPFile);
    fread(&Header.biClrImportant, 4, 1, BMPFile);

printf("\nType:%hd\n", Header.Type);
printf("Size:%ld\n", Header.Size);
printf("Reserve1:%hd\n", Header.Reserve1);
printf("Reserve2:%hd\n", Header.Reserve2);
printf("OffBits:%ld\n", Header.OffBits);
printf("biSize:%ld\n", Header.biSize);
printf("Width:%ld\n", Header.biWidth);
printf("Height:%ld\n", Header.biHeight);
printf("biPlanes:%hd\n", Header.biPlanes);
printf("biBitCount:%hd\n", Header.biBitCount);
printf("biCompression:%ld\n", Header.biCompression);
printf("biSizeImage:%ld\n", Header.biSizeImage);
printf("biXPelsPerMeter:%ld\n", Header.biXPelsPerMeter);
printf("biYPelsPerMeter:%ld\n", Header.biYPelsPerMeter);
printf("biClrUsed:%ld\n", Header.biClrUsed);
printf("biClrImportant:%ld\n\n", Header.biClrImportant);

fread(DataBuff, sizeof(DataBuff), 128*128, BMPFile);

for(i=0; i<20; i++){
    printf("%d\n", DataBuff[i]);
}

fclose(BMPFile);

return 0;
}

Output
Type:19778
Size:17462
Reserve1:0
Reserve2:0
OffBits:1078
biSize:40
Width:128
Height:128
biPlanes:1
biBitCount:8
biCompression:0
biSizeImage:0
biXPelsPerMeter:0
biYPelsPerMeter:0
biClrUsed:256
biClrImportant:0

0
0
0
0
1
1
1
0
2
2
2
0
3
3
3
0
4
4
4
0

I think the header info is correct, and the number of elements is correct (16384). I am just unsure how to print out the pixel values. The above is clearly not right...

Does this have to do with padding?

Thanks!


Solution

  • You need to read your pixels starting from the offset specified in BMPFile.OffBits.

    There's also a 'Colour Table' in the BMP header - each pixel is an index in to this (IIRC!), so it serves as a colour lookup table. Wikipedia advises that the colour table is mandatory for 8BPP (or less) images.

    Also: Since your image is 8 bits per pixel, you will need to convert from RGB in to greyscale by extracting the RGB components and combining them.

    Also Also: Why do you say your results are 'obviously wrong'? Remember that .bmp's are stored upside-down in the file.

    Which OS are you on? Windows provides functions to do this for you.