Lazy Foo' Productions

Texture Blitting and Texture Padding

Last Updated 10/04/14

Blitting is the process of copying pixels from one image to another. In this tutorial, we're going to two halves of an image and combine them by blitting the
pixel data. We'll also have to manually pad the pixel, since DevIL can't do it for us this time.

Here are some new 32bit functions to do some new pixel operations. createPixels32() creates a blank set of pixels for us to use, copyPixels32() copies a set of external pixels, and
padPixels32() pads the member pixels to be power of two.

The function createPixels32() is pretty simple. First it makes sure that a valid set of image dimensions were given. Then it frees any existing texture data because we don't want
to create a conflict between new and previous texture data.

After that, we allocate the pixel data and set the LTexture attributes.

copyPixels32() works similarly to createPixels32() only it does a memcpy() on an external pixel pointer. Since this is RGBA pixel data with 4 bytes per pixel, the size of the
memory being copied is number of pixels times four.

Before we go over padPixels32(), let's go over blitPixels32() and how pixel blitting works at a byte level.

First we make sure there are pixels to blit with. After that we have a call to memcpy() that copies "sPixels" (source pixels) to "dPixels" (destination pixels) row by row.

Now how do the equations in memcpy() work? Let's say we wanted to copy the pixels on the left to the pixels on the right at the bottom right corner:

We're blitting the pixels in the top left corner so the offset is x = 1, y = 1. For the first iteration of the loop "i" will equal 0. This means for the first loop we'll get:
dPixels[ ( 0 + 1 ) * 4 + 1 ] and sPixels[ 0 * 3 ]

So the first row of 3 sPixels gets copied starting at dPixels[ 5 ] which is what you would expect.

So source pixels starts copying at 3 to destination pixels at 9. I'm sure you could figure out the last row.

Two things to note: 1) Remember that since it's 32bit pixels, we copy imageWidth * 4 bytes per pixels when copying pixel rows. 2) Also it's the texture width not the image width
that we use to calculate how many pixels there are per row.