8Bit Grayscale Bitmap

This is a discussion on 8Bit Grayscale Bitmap within the C Programming forums, part of the General Programming Boards category; Hello everyone, I cannot seem to read in an 8bit grayscale image into an array. Here is my code. Code: ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    1

    8Bit Grayscale Bitmap

    Hello everyone,

    I cannot seem to read in an 8bit grayscale image into an array. Here is my code.

    Code:
    #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);
    
    
        for(i=0; i<Header.biWidth; i++){            // Not right, but unsure how to fix
            for(j=0; j<Header.biHeight; j++){
                DataBuff[i][j] = getc(BMPFile);
                printf("%02d ",DataBuff[i][j]);
            }
        }
    
    
        fclose(BMPFile);
    
    
        return 0;
    }
    Header 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

    Thanks,

    Shinobii

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    You need to skip OffBits (1078) bytes from (I believe) the beginning of the file before reading the bitmap data. Also, keep in mind that the bitmap is stored "upside down", where the last row is stored first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 each 8bit chars from a 32bit long int
    By Brucewd in forum C Programming
    Replies: 19
    Last Post: 08-02-2011, 06:27 PM
  2. Folders in 8bit
    By epi_jimenez in forum C Programming
    Replies: 3
    Last Post: 04-10-2009, 10:44 AM
  3. How to make a GrayScale filter?
    By Raigne in forum Game Programming
    Replies: 11
    Last Post: 02-25-2008, 04:52 AM
  4. 8Bit Bitmaps Problem
    By cboard_member in forum Game Programming
    Replies: 3
    Last Post: 08-19-2005, 05:55 PM
  5. Grayscale and mode 13h
    By SMurf in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 05-08-2002, 12:36 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21