multiply an image/ofPixels [solved]

hi all …

i’m trying to find an efficient way of multiplying all the pixels in an ofPixels array/image.
basically i need to multiply the ofxPixels array/image by an opacity parameter.

i wonder what it should “be” like … something like :

ofPixels pixels, newPixels;
float opacity = 0.5f;
newPixels = pixels * opacity;

is there an efficient way to do that or i need to run all over all the pixels on the ofPixels array and multiply each pixel ?

does it make sense ? any ideas ?

thanks !


I think it’s a nice idea to have an easy way to multiply all pixels at once, but right now there isn’t. You have to loop over them and do it there.

ok … thanks a lot joshua !
by the way i got into a parallel question … what would be faster ?

* to draw into a fbo with an ofSetColor(opacity), then try to copy the texture ?
* to acces all the pixels and multiply them by the opacity ?

what do you think ?

and in terms of one by one access … do you know if there is any kind of c++ object that could help and do that faster then me with simple for(allPixels){do stuff} ?


Drawing them into a fbo with and then copying the texture back isn’t going to be faster because it involves sending all the data to the graphics card and then pulling it back and I don’t think that’s what you want to do. Generally speaking the compiler is going to spit out something faster for that kind of operation that what you could do by hand. Unless your pixel array is really really big even using CUDA or something like that isn’t going to be faster because you’re still sending to the graphics cards and then getting it back and that’s slower. As long as you make sure your texture is in cache then it should be super fast.

Understood ! Thanks a lot !

Hi Joshua …

Well i’ve been trying both strategies to establish my solution to my quesiton :

  • first one to getPixels from 2 images, and then run across them to make some operation between them.

  • second one, us an Fbo to make the same operation but rendering offscreen.

My results are that it’s much faster to work graphically then single pixel by pixel operations as I suspected.
Even that we’ve to upload the textures to the graphic card, the results are much much faster … ?¿

Thanks !