CreateStaticPixmap - Pitch Parameter?

BlitzMax Forums/BlitzMax Programming/CreateStaticPixmap - Pitch Parameter?

Gabriel(Posted 2006) [#1]
I'm trying to use CreateStaticPixmap() to grab a pixmap from an image loaded with devIL, but I'm a bit hazy on exactly what Blitz is expecting the pitch parameter to be. I'm guessing that it's BYTES multiplied by width, but I'm not 100% sure. Is that right or should it be bits?


Byteemoz(Posted 2006) [#2]
It's the number of bytes per line (BytesPerPixel*WidthOfPixmap).
-- Byteemoz


Gabriel(Posted 2006) [#3]
Thanks, I thought it was, but some of these functions want bits per pixel and some want bytes, so I wanted to make sure.


ImaginaryHuman(Posted 2006) [#4]
Pitch is NOT the number of bytes per line. Sometimes that is true, sometimes it isn't.

Pitch is the total number of bytes, rounded up to the nearest multiple of 4 (or whatever the alignment is), that is needed to contain a single row of pixels.

When you create the pixmap, if the `alignment` is 4, that means you want the start of each row to begin on a 4-byte aligned memory address. The amount of bytes per row needed to achieve this is the Pitch, which may be more than the total number of bytes taken up by actual pixel data.

With Alignment of 1, LIMINENCE format, Pitch=Width*BytesPerPixel

With Alignment of 4, LIMINENCE format, Pitch=Width*BytesPerPixel rounded up to the nearest multiple of 4

You'll notice, though, that if you have a RGBA or ARGB format pixmap, because each pixel is a whole 4 bytes, it is ALWAYS aligned (regardless of whether alignment is 1 or 4), so Pitch WILL be the same as Width*BytesPerPixel.

The Pitch takes into account the requested alignment, which is usually 4. It's how many bytes you'd need to add to an address in the pixmap in order to get to the same X coordinate on the next row. Obviously if you have a 3x3 pixmap in RGB format, that's only 9 bytes of pixel data per row, so if alignment is 4 it will be increased to 12 bytes per row. So you'd have to add 12 to the pointer to get to the same X coord on the next row. That is, unless align=1, in which case you'd add 9.

A pitch is also an absolutely necessary device for allowing the system to handle `windows within pixmaps` and static pixmaps. To have a window within a pixmap, where the window is smaller than the original pixmap, you have to know how many extra pixels within the original you have to skip at the end of the window's row in order to get to the start of the next window's row. That's obviously going to be the original pixmap's pitch minus the window pixmap's pitch. Without a pitch you wouldn't be able to have windows within pixmaps - generally it defines the difference between where the pixels stop and where the memory space for the row stops.

Generally you'll want to use alignment of 4 for your pixmaps if you have a say in it, because this is more efficient for reading/writing the data. It also probably makes it faster for uploading textures or grabbing the backbuffer.


Byteemoz(Posted 2006) [#5]
Hmmm ... does devIL respect these rules, too?
For example if you load an 101 by 101 pixels image with 1 byte per pixel will it use 104 bytes per line or will it return all 10201 pixels without any padding or alignment?
In the second case Pitch would be 101 (bytes) regardless of how the pixmap-functions usually calculate it...
-- Byteemoz


ImaginaryHuman(Posted 2006) [#6]
Test it.