Invert alpha channel in ofImage

Hey,

Could anyone provide me with an example of how can I programatically invert the alpha channel of ofImage?

Thanks in advance, M

Hi,

There’s probably a neat way to do this with one of the OF classes, but just flipping the alpha byte can be done like this:

		ofImage myImage;
		myImage.load("ImageWithAlphaChannel.png");
		int w = myImage.getWidth();
		int h = myImage.getHeight();
		int nChannels = myImage.getPixels().getNumChannels();
		for( int y = 0; y < h; y++ )
		{
			for( int x = 0; x < w; x++ )
			{
				int tmpIndex = ((y*w)+x) * nChannels;
				myImage.getPixels()[ tmpIndex + 3 ] = 255 - myImage.getPixels()[ tmpIndex + 3 ]; // has to be a 4 channel image or this will crash
			}
		}
		myImage.update(); // We've changed the pixels and ofImage doesn't know about it, so make sure we update the texture