Posts Tagged BMP

BMP and 32-bit alignment

I use bitmaps for my 3D graphics programs since they have a very simple file format that I can write a parser for in a few hours. There was a bmp class that I had lying around from a long time ago that was used to load textures to render on OpenGL primitives. Since textures are usually square and powers of 2, you can be a little sloppy and still have the code work, like flipping the rows and columns. The picture will only be flipped or rotated 90 degrees. You can catch that if you billboard an object, but not if you have some texture like an asteroid, which was what I was using it for. The flipping of the rows and columns didn’t trip me up, but it was the 32-bit alignment that did. Since the texture is a power of 2 in width/length, it will definitely be 32-bit aligned, so you don’t have to worry about padding. Since I’m using 24-bit color, 8 bits per color channel it is possible for me to be misaligned if my “bytes times (width times column)” isn’t divisible by 4. 24 bits or 3 bytes does not divide by 4, so the width or column has to be divisible by 4. When I tried to load a 131 by 23 pixel image, my read and write functions for the bmp failed miserably. It was only after some more reading that I realized I had to pack extra bytes to make it 32-bit aligned.

32-bit alignment is a big issue for performance since the CPU bus handles data in chunks of 32 and now more commonly 64 bits.

Now, back to the whole point of talking about bitmaps. I’m using a bitmap loader to test my PNG writing code since making frames for a movie take a lot of space unless you use compression.  Hard drive space is cheap, but not that cheap.  I love PNGs since they are royalty free and work well.

, , ,

No Comments