Bitmap parsing
Author |
Message |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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;
}
|
Description: |
The image I've been testing with. |
|
Filesize: |
7.47 KB |
Viewed: |
15819 Time(s) |
![image.bmp image.bmp](uploads/attachments/image_135.bmp)
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: 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.
|
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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.
|
|
|
|
|
![](images/spacer.gif) |
Alexander_
|
Posted: 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)
|
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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.
|
|
|
|
|
![](images/spacer.gif) |
|
|