Understanding setFromAlignedPixels()

I would like to know what is this for…
Also I would like to know why memcpy is done in that way (row per row)… wouldn’t it be ok to do it all at once? what is the stride and why is it needed?

Thanks for any info!

template<typename PixelType>
void ofPixels_<PixelType>::setFromAlignedPixels(const PixelType * newPixels, int width, int height, int channels, int stride) {
	allocate(width, height, channels);
	int dstStride = width * getBytesPerPixel();
	const unsigned char* src = (unsigned char*) newPixels;
	unsigned char* dst =  (unsigned char*) pixels;
	for(int i = 0; i < height; i++) {
		memcpy(dst, src, dstStride);
		src += stride;
		dst += dstStride;

some times images are padded for performance meaning that there’s some extra data at the end of each row to make the number of bytes multiple of some power of 2. the stride is just the total size of the image including the padding. unless you are using some device or video format that returns that kind of image you shouldn’t need it

1 Like

Gracias Arturo!
so it is used when loading images to graphics card to fit the power of two rule for better performance? hmm ok…
so if i want to convert a non pow2 (lets say 100,120) image to pow2 i must do something like:

nonPowImg.setFromAlignedPixels( imgPow, 255, 255, 4, 100);

is that correct?

no it’s actually the opposite, it’s used when you get an image that it’s padded to a power of 2 to unpad it to it’s real size

ah ok … i see! thanks!!

Hi again!
Is there any method to turn a non pow 2 img to a pow 2?
I mean just the opposite of setFromAlignedPixels() a method that would create those padded extra pixels.

i guess ofPixels::pasteInto + ofNextPow2. there’s nothing to resize the pixels container without resizing the actual image but most of the times is faster to copy into a different memory area, mostly if you are doing it every frame

Thank you for the fast response!
Something like this?

	ofPixels pix;		// original pixels (non pow2)
	ofPixels pixPow;	// destination pixels. (pow2)
						// we will put the original pixels here with an added padding
	pixPow.allocate(ofNextPow2(pix.getWidth()), ofNextPow2(pix.getHeight()), 4);
	pixPow.pasteInto(pix, pix.getWidth(), pix.getHeight());

the last line should be:


oh! I’m so confused with this two methods (setFromAlignedPixels and pasteInto) I expect the opposite… anyways thank you so much for the tips!
You just saved me tons of hours confused in front of the screen!!

just read it as if it were english : )

pix, pasteInto pixPow at 0,0
pix, setFromExternalPixels otherPix...

where the method is an imperative verb

1 Like

I had no idea this was in the API, I came across this with .NET Bitmaps, as those are always padded to have multiple of 4 rows, more info here. When sending them to OF sometimes they got skewed!


1 Like