Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the real RGBA or ARGB color values without premultiplied alpha?

I'm creating an bitmap context using CGBitmapContextCreate with the kCGImageAlphaPremultipliedFirst option.

I made a 5 x 5 test image with some major colors (pure red, green, blue, white, black), some mixed colors (i.e. purple) combined with some alpha variations. Every time when the alpha component is not 255, the color value is wrong.

I found that I could re-calculate the color when I do something like:

almostCorrectRed = wrongRed * (255 / alphaValue);
almostCorrectGreen = wrongGreen * (255 / alphaValue);
almostCorrectBlue = wrongBlue * (255 / alphaValue);

But the problem is, that my calculations are sometimes off by 3 or even more. So for example I get a value of 242 instead of 245 for green, and I am 100% sure that it must be exactly 245. Alpha is 128.

Then, for the exact same color just with different alpha opacity in the PNG bitmap, I get alpha = 255 and green = 245 as it should be.

If alpha is 0, then red, green and blue are also 0. Here all data is lost and I can't figure out the color of the pixel.

How can I avoid or undo this alpha premultiplication alltogether so that I can modify pixels in my image based on the true R G B pixel values as they were when the image was created in Photoshop? How can I recover the original values for R, G, B and A?


Background info (probably not necessary for this question):

What I'm doing is this: I take a UIImage, draw it to a bitmap context in order to perform some simple image manipulation algorithms on it, shifting the color of each pixel depending on what color it was before. Nothing really special. But my code needs the real colors. When a pixel is transparent (meaning it has alpha less than 255) my algorithm shouldn't care about this, it should just modify R,G,B as needed while Alpha remains at whatever it is. Sometimes though it will shift alpha up or down too. But I see them as two separate things. Alpha contorls transparency, while R G B control the color.

like image 885
Proud Member Avatar asked Jun 18 '11 12:06

Proud Member


People also ask

How do you convert RGBA to Argb?

(pixel << 24) | (pixel >> 8) rotates a 32-bit integer 8 bits to the right, which would convert a 32-bit RGBA value to ARGB. This works because: pixel << 24 discards the RGB portion of RGBA off the left side, resulting in A000 . pixel >> 8 discards the A portion of RGBA off the right side, resulting in 0RGB .

What is Alpha in Argb?

Alpha indicates how opaque each pixel is and allows an image to be combined over others using alpha compositing. rgba(red, green, blue, alpha) The alpha value is declared as a decimal number from 0 to 1, where 0 is fully transparent and 1 is fully opaque.

What is Premultiplied Alpha?

“Premultiplied alpha” or a “premultiplied image” means that the color image was masked (multiplied) by its own alpha channel; it has already been masked. Images with hand-painted masks are normally saved unmasked.

What is Argb color code?

RGB Value. Each parameter (red, green, and blue) defines the intensity of the color between 0 and 255. For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255) and the others are set to 0.


1 Answers

This is a fundamental problem with premultiplication in an integral type:

  • 245 * (128/255) = 122.98
  • 122.98 truncated to an integer = 122
  • 122 * (255/128) = 243.046875

I'm not sure why you're getting 242 instead of 243, but this problem remains either way, and it gets worse the lower the alpha goes.

The solution is to use floating-point components instead. The Quartz 2D Programming Guide gives the full details of the format you'll need to use.

Important point: You'd need to use floating-point from the creation of the original image (and I don't think it's even possible to save such an image as PNG; you might have to use TIFF). An image that was already premultiplied in an integral type has already lost that precision; there is no getting it back.

The zero-alpha case is the extreme version of this, to such an extent that even floating-point cannot help you. Anything times zero (alpha) is zero, and there is no recovering the original unpremultiplied value from that point.

like image 193
Peter Hosey Avatar answered Oct 23 '22 05:10

Peter Hosey