I had a problem loading TIFF's with alpha channels in Imlib2 as they seemed to be coming out very dark. I noticed in the source code that, on saving, there was a comment about premultiplying if there's an alpha component but no corrollary in the loading method.
/* TIFF makes you pre-mutiply the rgb components by alpha */
a = (pixel >> 24) & 0xff;
alpha_factor = ((double)a / 255.0);
r *= alpha_factor;
g *= alpha_factor;
b *= alpha_factor;
But surely that would mean that our images would be too bright since, in loading, we'd divide by this alpha factor. In my defence I was heavily dosed up on cold medicine at the time but I eventually twigged that if the alpha was, say, 150 then 150/255 is smaller than 1 and therefore dividing would actually make it larger and hence brighter. One swift patch later and everything is hunky dory.
for (j = 0; j < w; j++)
+ int a, r, g, b;
+ a = TIFFGetA(pixel_value);
+ r = TIFFGetR(pixel_value);
+ g = TIFFGetG(pixel_value);
+ b = TIFFGetB(pixel_value);
pixel_value = (*(pixel++));
- (*(buffer_pixel++)) =
- (TIFFGetA(pixel_value) << 24) |
- (TIFFGetR(pixel_value) << 16) | (TIFFGetG(pixel_value) << 8) |
+ if ((a > 0) && (a < 255))
+ r = (r * 255) / a;
+ g = (g * 255) / a;
+ b = (b * 255) / a;
+ (*(buffer_pixel++)) = (a << 24) | (r << 16) | (g << 8) | b;
Or is it? A user was still reporting the same bug - images were too dark. Again it took a while for the lightbulb to turn on depsite extensive tracing in Totalview and GDB. The problem was that I'd stretched the image to fit a new aspect ration and then composited it onto a black background of the original size to make the black bars thus producing a darker image. Doh. Turning off alpha blending fixed it.