![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Trying to read BMP files, header information is wrong / offsetI am trying to load BMP file information and im using the following structs:
CPP / C++ / C Code:
I've checked many pages of documentation and they are all consistent (of course) and I've done a sizeof() for each element and they're all the right sizes. I read the 2 headers with these 2 lines: CPP / C++ / C Code:
But the information is wrong. The type is 19778 which is correct. The filesize is wrong, I tried on 2 files, one made by MSPaint and the other by photshop. One had a rediculously large number, the other one was 0. Typically I get 0's for the 2 reserved words in the file header, and the data offset is again usualy rediculously large. In the image header, the bits per pixel is always stored in the number of planes field that should be before it. So it kinda looks like im jumping ahead 2 extra words somewhere, but can't figure it out, does anyone have any nice debugging ideas ? |
|||
|
#2
|
|||
|
|||
Re: Trying to read BMP files, header information is wrong / offsetQuote:
Now, different compilers have different ways of letting you try to tell the compiler how to pack a struct. This is implementation-dependent, but I have found the following works for all of my recent GNU, Microsoft and Borland compilers: CPP / C++ / C Code:
Look at the sizes of the headers with the pragma in place. For my compilers without the pragma: Code:
With the pragma: Code:
(The correct size is 14.) Regards, Dave |
|
#3
|
|||
|
|||
Re: Trying to read BMP files, header information is wrong / offsetGreat, works now !
Never worked with structs much. Didn't know it would randomly pack stuff for me withought asking Thanks a lot |
|
#4
|
|||
|
|||
Re: Trying to read BMP files, header information is wrong / offsetQuote:
Well, it may be arbitrary, but it isn't necessarily random. Compilers are free to pick whatever they think is the "best" way to pack structs. (Usually they pick word boundaries that make it faster at the expense of a few bytes extra storage space. That mayb OK "most of the time," but plays havoc when you need to read headers all at once like you have done.) A reminder: using the #pragma may not work the same with all compilers, so if you have to supply source code for outside (customer) use, there is a risk that it won't work. However, once you have found that it works for you, then it's OK until and unless you change compilers. Then you test again. (Or, maybe, you could put sizeof() tests at the very beginning of your code and leave them there so that the program would abort if it couldn't pack them correctly.) The Standard way (not implementation-dependent) is to read each item a byte at a time and pack them into the individual variables as you go. I don't have any qualms about using these pragmas for my own stuff or stuff for internal use, since I always test them against any new or updated compiler that I install. Regards, Dave |
|
#5
|
|||
|
|||
Re: Trying to read BMP files, header information is wrong / offsetgreats thanks for the tips. Sound advice
|
|
#6
|
|||
|
|||
Re: Trying to read BMP files, header information is wrong / offsetNot benefit so much from this discussion because i don't know what is pack a struct.
I know structure. A billion thanks for your help. |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pcap programming in C++ | goal86sg | C++ Forum | 2 | 25-Jan-2006 14:02 |
| Bloodshed Dev C++ Project Options | JdS | C++ Forum | 6 | 11-Nov-2005 17:23 |
| Writing good header files | mercury | C++ Forum | 2 | 15-May-2005 10:41 |
| Apache2 config issues | monev | Apache Web Server Forum | 2 | 28-Jun-2004 06:19 |
| Can't view pages from another machine on the Intranet | aevans | Apache Web Server Forum | 9 | 14-May-2004 02:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The