2004.05.21 19:30 "[Tiff] Re: Un-premultiplying the color data", by Chris Cox
Here is what I am trying in my code to un-premultiply the color data in the image attached with this message:
source: is the buffer returned by the libtiff functions.
dest: is the destination buffer
the image is: contig, strip, CMYK, 8-bits per channel, with one associated alpha channelsince i have to gather the color values in my dest buffer in the order KCMY, that is why i take dest[0] = source[3] and so on.
alpha = source[4];
if(alpha != 0)
{
dest[0] = ( (source[3]*255) + alpha/2 )/alpha;
dest[1] = ( (source[0]*255) + alpha/2 )/alpha;
dest[2] = ( (source[1]*255) + alpha/2 )/alpha;
dest[3] = ( (source[2]*255) + alpha/2 )/alpha;
}
It would run faster is you used a lookup table for the divide and multiply by 255.
Divides are slow on all modern CPUs, and even a 64k lookup table is faster -- unless you've got someone who can write really good SSE or AltiVec code.
Also, using a lookup table would avoid divides by zero, and you can special case top == 0 (which should return zero) and bottom <= top (which should return 255) without any branches.
Also, CMYK is white matted - what you're doing is black matting and only suitable for RGB data.
To white matte:
out_matted = 255 - (alpha * in + 127) / 255;
To un-white matte:
out = 255 - ( (255* (255 - in_matted) + (alpha/2)) / alpha );
There are two questions here:
- How do you know come to know about the color information, if alpha had been zero?
You don't. If the area is fully transparent, there is no color information.
- Is the procedure above correct?
Nope.
Chris