1 Byte Alignment vs 4 Byte Alignment (Pixel Packing)

3k Views Asked by At

I am reading the OpenGL SuperBible for OpenGL 3.x. I am having a hard time understanding the whole "pixel packing concept." I get that typically a 199px wide image would require 597 bytes [(199 * 3)3 for each color channel RGB]. My first question is why would this only sometimes be right, the author says this only woks with a 4byte alignment system. He goes on to say that an extra three bytes will be added to make it divisible by four easily, which I don't understand either. So really my question is what is the significance of a 4 byte alignment -- what does that actually mean?? The author then says the alternative is 1-byte alignment, which I don't understand either.

The author says that .TGA is 1 byte aligned or "tight" and .bmp is 4 byte aligned.

What is a 4 byte alignment, a 1 byte alignment, and why should I use one over the other? When should I use .tga or .bmp for texturing?

2

There are 2 best solutions below

0
Ed Abucay On BEST ANSWER

Well it is about data alignment. It differs on different architectures and memory management. For example when you code a in 32-bit processor the default padding on a structure / int is 4-byte. In 64-bit is 8-byte. When you want to override the default padding on a code structure you can use #pragma pack (on visual studio), attribute((packed)) (gcc).

Look here for additional reference:

4
Ben Voigt On

To make matters worse, your description doesn't differentiate pixel alignment vs scan line alignment.

A 199 pixel image with 24-bit color indeed requires 597 bytes. But the second scan line will start at offset 600, because it has to be aligned to a 4 byte boundary, and 600 is the first 4 byte boundary that doesn't overlap the first scan line.

This means that instead of each pixel starting at byte

(x + y * width) * 3

Each row will start at byte

y * 600

and each pixel at byte

y * 600 + x * 3

But why not just make your textures a power of 2, which is supported on all cards in all OpenGL versions and doesn't cause alignment questions? You use texture coordinates to make sure that the padding isn't ever processed or rendered.