Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Bitmap parsing
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: Sun Apr 17, 2011 7:42 pm   Post subject: Bitmap parsing

I'm working on a program to manipulate bitmap files. So far, all I've done is read in the headers (file header + bitmap info header). The data reads in correctly until the height field is read. My test image is 50x50. Width is correctly read as 50, whereas height is read as -50. All following fields are 0, but shouldn't be. Anyone know what's up?


The code is a bit messy. I'll clean it up when it works.
c:

#include <stdio.h>

        typedef struct {
                char verifier[2];
                unsigned int size;
                unsigned short int reserved1, reserved2;
                unsigned int offset;
        } HEADER;
       
        typedef struct {
                unsigned int size;               /* Header size in bytes      */
        signed int width,height;                /* Width and height of image */
        unsigned short int planes;       /* Number of colour planes   */
        unsigned short int bits;         /* Bits per pixel            */
        unsigned int compression;        /* Compression type          */
        unsigned int imagesize;          /* Image size in bytes       */
        int xresolution,yresolution;     /* Pixels per meter          */
        unsigned int ncolors;           /* Number of colors         */
        unsigned int importantcolors;   /* Important colors         */
        } INFOHEADER;

int main(){
        FILE *imgptr;

        HEADER header;
        INFOHEADER infoHeader;

        //Open the image
        if (!(imgptr = fopen ("image.bmp", "r"))) {
                fprintf (stderr, "Image failed to load\n");
                return 1;
        }
       
       
        //read the file header
        fread (header.verifier, 1, 1, imgptr);
        fread (header.verifier+1, 1, 1 , imgptr);
        printf ("Filetype: %c%c\n", header.verifier[0], header.verifier[1]);
        fread (&(header.size), 1, 4, imgptr);
        printf ("Size: %u\n", header.size);
        fread (&(header.reserved1), 1, 2, imgptr);
        fread (&(header.reserved2), 1, 2, imgptr);
        fread (&(header.offset), 1, 4, imgptr);
       
        //read the infoheader.
        fread (&(infoHeader.size), 1, 4, imgptr);
        //printf ("%hu\n", infoHeader.size);
        fread (&(infoHeader.width), 1, 4, imgptr);
        fread (&(infoHeader.height), 1, 4, imgptr);
        printf ("Width: %d\nHeight: %d\n", infoHeader.width, infoHeader.height);
        fread (&(infoHeader.planes), 1, 2, imgptr);
        fread (&(infoHeader.bits), 1, 2, imgptr);
        fread (&(infoHeader.compression), 1, 4, imgptr);
        printf ("Compression: %hd\n", infoHeader.compression);
        fread (&(infoHeader.imagesize), 1, 4, imgptr);
        printf ("Image size: %hd\n", infoHeader.imagesize);
        fread (&(infoHeader.xresolution), 1, 4, imgptr);
        fread (&(infoHeader.yresolution), 1, 4, imgptr);
        fread (&(infoHeader.ncolors), 1, 4, imgptr);
        printf ("Colors: %hd\n", infoHeader.ncolors);
        fread (&(infoHeader.importantcolors), 1, 4, imgptr);
        return 0;
}



image.bmp
 Description:
The image I've been testing with.
 Filesize:  7.47 KB
 Viewed:  14769 Time(s)

image.bmp


Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Sun Apr 17, 2011 8:36 pm   Post subject: RE:Bitmap parsing

On the Wikipedia page for the BMP file format, I found:

Wikipedia wrote:
ImageHeight is expressed in pixels. The absolute value is necessary because ImageHeight can be negative.


This is in the section entitled "Pixel Storage". Just take the absolute value.
Insectoid




PostPosted: Sun Apr 17, 2011 8:40 pm   Post subject: RE:Bitmap parsing

Ah, that makes sense.

Looked at the file in a hex editor and confirmed that the height is indeed negative, and all following fields are indeed zero. So my code's working fine.
Alexander_




PostPosted: Sat May 14, 2011 6:46 pm   Post subject: RE:Bitmap parsing

A little late, although I hope it could be useful to you.

The pixel array of the bitmap is stored in reverse (according to the image) order and will cause the rendered image (if directly taken from the array) to be rendered upside down.

A negative height can just mean for it to be read right side up (and you should handle this fact in your code if you wish to cover all edge cases)
Insectoid




PostPosted: Sat May 14, 2011 8:17 pm   Post subject: RE:Bitmap parsing

Yeah, you're a bit late. I've figured this all out and finished the project.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: