Bmp header struct, problem with word align...

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Bmp header struct, problem with word align...

Post by ne0h »

Hi,
I'm writing a BMP library ( to load BMP files ),
but I'm getting some error with this:

Code: Select all

typedef struct
{
    u16 magic_value; /** "BM" **/
    u32 file_size; /** the size of the BMP file in bytes **/
    u32 reserved; /** Reserved **/
    u32 rawdataoffset; /** Offset where bmp data can be found **/
} BmpHeader;
This is the BMP main header structure, and I've tried to load it with this:

Code: Select all

[...]
BmpHeader Bmp_Header;
sceIoRead(fd, &Bmp_Header, sizeof(BmpHeader))
[...]
But when I try to print infos, I get a error because the 3th and the 4th bytes are "jumped", I know that this is probably because GCC what's to word align the structure, so I've tried to printf the size of the structure with sizeof() and I get 16bytes instead of 14...
So can I write a right struct?
I've tried with __attribute__((packed)), but I still get 16 bytes...
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Make sure you're using the packed attribute in the right place. It can be tricky with a typedef:

Code: Select all

typedef struct
{
(blahblah)
} __attribute__ ((packed)) BmpHeader;
Does that help?
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

....mmm.
I've put the attrib in the same place ( if I put it after BmpHeader Gcc has said me that attribute are ignored ) but now works, instead of the last build... :(
Thanks ooPo! :)
Post Reply