[uug] reading bmp header

AJ ONeal coolaj86 at gmail.com
Thu Oct 28 18:22:38 MDT 2010


I used the
#prama pack(2)
that the original author used and now the first part of the header comes out
perfect.

hexdump exposes the magic number as 42 4D
instead I see 4D 42
but if I make it two chars rather than one short I do get 42 4D

But some of the other values were still coming out wonky so I ran hexdump
and compared it to the bmp reference on wikipedia:

0000000 *42 4d* 36 d2 0f 00 *00 00* 00 00 *36 00 00 00* 28 00
0000010 00 00 *d0 02 00 00* 20 fe ff ff *01 00* 18 00 *00 00*
0000020 *00 00* 00 00 00 00 *13 0b 00 00* 13 0b 00 00 *00 00*
0000030 *00 00* 00 00 00 00 00 00 00 00 00 00 00 00 00 00


d0 02 00 00
reads as 720 (02d0)
20 fe ff ff
reads as 4294966816 (FFFFFE20)

but should be 480  - which is 01E0 - so probably written as E0 01 00 00
Huh?!?!?

I've resized the bitmap in two different programs (OSX paintbrush and image
viewer) and they both output bad values like that!

Give that the total size, header size, width, and depth of the picture are
all correct,
I suppose I can ignore the height and the image size and get the job done.
In fact, it seems odd that too much information would be supplied in the
first place.

Am I missing something? What's going on here?
Is it a de facto standard to dump random data from memory into the
"unnecessary" fields?


AJ ONeal

On Thu, Oct 28, 2010 at 2:12 PM, Stuart Jansen <sjansen at buscaluz.org> wrote:

> On Thu, 2010-10-28 at 13:18 -0600, AJ ONeal wrote:
> > I'm trying to read a bmp header, but the bytes seem to be coming in in
> > the wrong order (endianness).
>
> You're incorrectly assuming that structs are packed as tightly as
> possible. In fact, they're padded. I've included code below to
> demonstrate this.
>
>
> http://en.wikipedia.org/wiki/Data_structure_alignment#Typical_alignment_of_C_structs_on_x86
>
> If you don't care about anything other than gcc, it's possible to pack
> structs.
>
> http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/
>
> > How can I get all of the values I'm reading in from the header in the
> > file in reverse order?
>
> For correctness, you should be converting from little endian to host
> byte order. On x86 this is a noop, but still a good habit.
>
> man endian
>
> ===
> #include <stdint.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> typedef struct
> {
>    uint16_t type; // uint8_t magic[2]
>    uint32_t size;
>    uint16_t reserved1;
>    uint16_t reserved2;
>    uint32_t offset;
> } Header;
>
> int main(int argc, char *argv[]) {
>    Header h = {0};
>    printf("%p - %p = %d\n", &(h.size), &(h.type),
>                             (void *)&(h.size) - (void *)&(h.type));
> }
> ===
>
> --------------------
> BYU Unix Users Group
> http://uug.byu.edu/
>
> The opinions expressed in this message are the responsibility of their
> author.  They are not endorsed by BYU, the BYU CS Department or BYU-UUG.
> ___________________________________________________________________
> List Info (unsubscribe here): http://uug.byu.edu/mailman/listinfo/uug-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://uug.byu.edu/pipermail/uug-list/attachments/20101028/d520560f/attachment-0003.html 


More information about the uug-list mailing list