[gdal-dev] GDAL alpha channels
Craig Bruce
csbruce at cubewerx.com
Wed May 30 12:27:14 PDT 2012
Frank Warmerdam <warmerdam at pobox.com> wrote:
> because it is hard to hit all the appropriate code paths safely and
> partly because the result will be lossy.
It depends on what you mean by "lossy". The value returned through the
GDAL interface won't be the literal sample value that is in a TIFF file
with associated alphas, but that literal value can be recovered losslessly
if the user should care to do so. To demonstrate, I wrote the following C
test program to verify that all 16-bit pre-multiplied sample values can be
re-created from un-pre-multiplied values without any quantization errors:
#include <stdio.h>
int main()
{
int gray, preGray, unGray, reGray, alpha;
double alphaMult, unAlphaMult;
for (alpha=0x0000; alpha <= 0xFFFF; alpha++) {
alphaMult = (double) alpha / (double) 0xFFFF;
unAlphaMult = (alpha == 0) ? 0.0 : (double) 0xFFFF / (double) alpha;
for (gray=0x0000; gray <= 0xFFFF; gray++) {
preGray = (int) (gray * alphaMult + 0.5);
unGray = (int) (preGray * unAlphaMult + 0.5);
reGray = (int) (unGray * alphaMult + 0.5);
if (reGray != preGray) {
printf("alpha=0x%04X, gray=0x%04X: preGray=0x%04X, unGray="
"0x%04X, reGray=0x%04X\n", alpha, gray, preGray, unGray, reGray);
return(0);
}
}
}
printf("there are no quantization problems\n");
}
> Going forward GDAL should do one of two things:
>
> 1) provide a metadata item so applications can know those rare
> circumstances when alpha is pre-multiplied.
>
> 2) un-premultiply premultiplied alpha when encountered.
It might be better to do both, which is what GDAL does with the
pixel-is-area vs. pixel-is-point issue. The GDAL interface always uses
pixel-is-area (according to the documentation I read) but it also supplies
an AREA_OR_POINT metadata item so a user can re-interpret the images in
that way if they so choose. An ALPHA_MODEL metadata item could be added
for users who are so inclined. Does any GDAL format other than TIFF use
pre-multiplied alphas?
As Even Rouault poins out, writing TIFF alpha channels is messed up
as well. After verifying that various viewers can handle unassociated
alpha channels properly, I switched my own TIFF generator to always write
TIFFs that way since associated alphas are so problematic.
--------------------------+----------------------+--------------------------
Dr. Craig S. Bruce | Ph 819-771-8303 x205 | CubeWerx Inc.
Senior Software Developer | Fax 819-771-8388 | Gatineau, Québec, Canada
csbruce at cubewerx.com | http://csbruce.com/ | http://www.cubewerx.com/
--------------------------+----------------------+--------------------------
"The process was complete, utter, unadulterated bullshit."
-- Tim Bray, commenting on OOXML at ISO
More information about the gdal-dev
mailing list