views:

582

answers:

4

I have two images. One of them is just plain white and has some areas with alpha transparency. It is intended to be a mask for making another image transparent. The other image, full colored and also PNG, has no alpha applied anywhere.

So I want to add the alpha values of the mask image to the alpha values of the other. Both images have the exact same size. It would be just a matter of looping through the pixels, I guess. Any idea how that would look in detail?

A: 
Mark Thalman
good idea! Unfortunately image manipulation on iPhone OS is very different. They premultiply the alpha from an PNG with "pngcrunch" upon building the project. That's done for performance. But then it's horrible to get alpha out of there, like I figured out...
Thanks
I didn't think that getting the alpha out of the PNG in my iPhone app was bad at all. If I get some time I'll post the code.
Mark Thalman
Thanks: There is no such thing as “premultiplying the alpha”. Premultiplication is done *to the colors*, *by the alpha*. Longer explanation here: http://stackoverflow.com/questions/870690/just-in-theory-how-is-the-alpha-component-premultiplied-into-the-other-component/871416#871416 So, the alpha is unchanged, so as long as you don't do anything to the color components, you'll be fine.
Peter Hosey
+1  A: 

Check out Alpha compositing. http://en.wikipedia.org/wiki/Alpha_compositing It looks like you're trying to perform A out B.

How would you ship around the problem with premultiplied alpha in iPhone OS? That's the actual problem I think. I know how to read out the data, but don't know how to put it back into the image.
Thanks
+1  A: 

"clemahieu" is right -- You don't have to worry about premultiplied alpha in this case, because you're doing high-level operations, and the actual pixel format is a low-level detail.

If you simply composite the white/alpha image over the color/no alpha image (in a color/alpha buffer) using the correct compositing operator, you'll get exactly what you want.

-Wil

Wil Shipley
A: 

CGImageRef resultImage = CGImageCreateWithMask([imageA CGImage],[imageB CGImage]); UIImage* img= [UIImage imageWithCGImage:resultImage]; CGImageRelease(resultImage); return img;

Meir Assayag