Smarter way of drawing chunks of images?

I thought I was pretty clever, drawing chunks of 5 images that were loaded into memory through this method, but the load is pretty heavy. Can someone point me to a more efficient method to achieve a similar result?

void testApp::setup(){	   
	for (int i = 0; i < IMAGES_NUM; i++) {  
		pixels = images[i].getPixels();		  
		allpixels[i] = pixels;  
		printf("reference: %p\n", pixels);  
void testApp::update(){  
	height = ofRandom(0, IMAGE_HEIGHT);  
	offset = (int) ofRandom(0, IMAGE_HEIGHT - height);  
	unsigned char * target = allpixels[(int) ofRandom(0, IMAGES_NUM)];  
	image.setFromPixels(target+offset*IMAGE_WIDTH*3, IMAGE_WIDTH, height, OF_IMAGE_COLOR, true);  
void testApp::draw(){  
	float x = 0;  
	float y = offset;  

Oh and I realised that saving the image references in the allpixels array shouldn’t improve much (or anything) on just running getPixels() on the image array, correct? :-[

The allpixels array probably doesn’t speed it up noticeably, but it isn’t that bad either.

Can you post an image of the output? If it does what I think it does you may be able to simply draw a quad with random texture coordinates to get the same effect.

Ah… yes! that indeed is exactly what I meant. That would definitely be a smarter way. As a processing guy, I haven’t worked with textures often… Thanks!

Thing is that in the mean time, I went back to some old code that one 'd think to be much slower as it iterates pixels, but at the same time enables me to jump, speeding up the process and providing an additional means of (pattern) blending, see the latest four tests here:

void testApp::update(){  
	block_height = (int) ofRandom(0, IMAGE_HEIGHT);  
	block_yoffset = (int) ofRandom(0, IMAGE_HEIGHT - block_height);  
	block_bytelength = block_height*IMAGE_WIDTH*3;  
	block_byteoffset = block_yoffset*IMAGE_WIDTH*3;  
	pixels = image.getPixels();  
	unsigned char * target = allpixels[(int) ofRandom(0, IMAGES_NUM)];  
	float alpha = .4;//ofRandom(0, 1);  
	float incr = 3;  
	for (int i = 0; i < block_bytelength; i += incr*3) {  
		pixels[block_byteoffset+i+0] = alpha*target[block_byteoffset+i+0] + (1-alpha)*pixels[block_byteoffset+i+0];  
		pixels[block_byteoffset+i+1] = alpha*target[block_byteoffset+i+1] + (1-alpha)*pixels[block_byteoffset+i+1];  
		pixels[block_byteoffset+i+2] = alpha*target[block_byteoffset+i+2] + (1-alpha)*pixels[block_byteoffset+i+2];  
	image.setFromPixels(pixels, IMAGE_WIDTH, IMAGE_HEIGHT, OF_IMAGE_COLOR, true);  
void testApp::draw(){  
	image.draw(0, 0);  

Any ideas if such approach can also be sped up? :smiley:
The cpu load isn’t that bad atm though, 20-30% at ye olde Macbook, but optimal = better.


Right, this will be a bit more tricky. You may be able to use FBOs and shaders to re-arrange the pixels. pass the texture you want to use as a uniform to a shader and then just do the same kind of thing, where you offset the input texture by some specific amount.

The other thing you can do is try openMP to run your current code in two threads.

Usually one loop through an image isn’t slow enough to affect performance that much, but these are the options I can think of if you need more speed.