2014.01.18 18:24 "[Tiff] Adding alpha to palette files", by John Hawley

2014.01.19 10:27 "Re: [Tiff] Adding alpha to palette files", by Even Rouault

Le samedi 18 janvier 2014 19:24:56, John Hawley a écrit:

Ok - I'm out of my comfort zone here and looking for a bit of guidance:

I'm in the process of writing a 3D dynamic mapping app which uses geotiff files as a data source. The dynamic nature of the mapping process means I am frequently re-writing numerous Geotiff as more data becomes available - so far so good - I've managed to automatically write these files using an 8-bit palette colour scheme and these are successfully read by gdal. My problem is that I really want the areas which have no data to be transparent i.e. I need to set an alpha channel. Now I've managed to understand from the documentation that I probably need to set EXTRASAMPLES_ASSOCALPHA and set my samples/pixel to 2?. However, I'm not sure how to actually add the alpha data. A snippet of sample code to go about this would be very helpful

John,

A possible solution for this (if you want to stick to paletted images), and that should be correctly handled by GDAL readers (e.g. QGIS), is to reserve one entry of the palette for the transparency, and declare that value in the specific GDAL_NODATA (42113) tag.

This will also be compatible with non-GDAL readers that will ignore the GDAL_NODATA tag.

/************************************************************************/
/*                          GTiffTagExtender()                          */
/*                                                                      */
/*      Install tags specially known to GDAL.                           */
/************************************************************************/

static TIFFExtendProc _ParentExtender = NULL;

static void GTiffTagExtender(TIFF *tif)

{
    static const TIFFFieldInfo xtiffFieldInfo[] = {
        { TIFFTAG_GDAL_NODATA, -1,-1, TIFF_ASCII, FIELD_CUSTOM,
          TRUE, FALSE, (char*) "GDALNoDataValue" },
    };

    if (_ParentExtender)
        (*_ParentExtender)(tif);

    TIFFMergeFieldInfo( tif, xtiffFieldInfo,
                        sizeof(xtiffFieldInfo) / sizeof(xtiffFieldInfo[0]) );
}

if 0 is the entry for transparency:

TIFFSetField( hTIFF, TIFFTAG_GDAL_NODATA, "0" ); (yes, as a string)

Even

--
Geospatial professional services
http://even.rouault.free.fr/services.html