[gdal-dev] How to add alpha channel to tiff image - C++ code

Even Rouault even.rouault at mines-paris.org
Wed Oct 3 12:10:20 PDT 2012


Le mercredi 03 octobre 2012 21:03:27, mighty_duck a écrit :
> Hi all,
> 
> I  have the following code:
> 
> bool grImageWarper::createDstRaster(const std::string &output,
>                                     GDALDatasetH hSrcDS,
>                                     GDALDatasetH &hDstDS,
>                                     int resX,
>                                     int resY,
>                                     double *dGeoRefTransform,
>                                     bool zeroAsTransparent,
>                                     const std::string &compression,
>                                     const std::string &projection)
> {
>     GDALDriverH driver = GDALGetDriverByName("GTiff");
> 
> 	CPLSetConfigOption("GDAL_TIFF_INTERNAL_MASK", "YES");
> 	CPLSetConfigOption("PROFILE", "GeoTIFF");
> 
>     if (driver == NULL)
>     {
>         return(false);
>     }
>     char **compressOption = NULL;
>     compressOption = CSLSetNameValue(compressOption, "COMPRESS",
> compression.c_str());
>     hDstDS = GDALCreate(driver, output.c_str(), resX, resY,
>                         GDALGetRasterCount(hSrcDS),
> GDALGetRasterDataType(GDALGetRasterBand(hSrcDS, 1)),
>                         compressOption);
>     if (hDstDS == NULL)
>     {
>         return(false);
>     }
> 
>     if (CE_None != GDALSetGeoTransform(hDstDS, dGeoRefTransform))
>     {
>         return(false);
>     }
> 
>     if (!projection.empty())
>     {
>         OGRSpatialReference targetSR;
>         if (strStartsWith(projection, "EPSG", CASE_INSENSITIVE))
>         {
>             std::string epsg = projection.substr(projection.find(":") + 1);
>             targetSR.importFromEPSG(simpleLexicalCast<int>(epsg));
>         }
>         else
>         {
>             targetSR.importFromProj4(projection.c_str());
>         }
> 
>         char *wkt = NULL;
>         OGRErr err = targetSR.exportToWkt(&wkt);
>         if (err != CE_None || GDALSetProjection(hDstDS, wkt) != CE_None)
>         {
>             OGRFree(wkt);
>             return(false);
>         }
>         OGRFree(wkt);
>     }
> 
>     for (int i = 0; i < GDALGetRasterCount(hSrcDS); ++i)
>     {
>         GDALRasterBandH hSrcBand = GDALGetRasterBand(hSrcDS, i + 1);
>         GDALRasterBandH hDstBand = GDALGetRasterBand(hDstDS, i + 1);
> 
>         GDALColorTableH cTable = GDALGetRasterColorTable(hSrcBand);
> 		GDALSetRasterColorInterpretation(hDstBand, GCI_AlphaBand);
>         if (cTable)
>         {
>             GDALSetRasterColorTable(hDstBand, cTable);
>         }
> 
>         int success;
>         double noData = GDALGetRasterNoDataValue(hSrcBand, &success);
> 		 std::cout << noData << std::endl;
>         if (success)
>         {
>             GDALSetRasterNoDataValue(hDstBand, noData);
>         }
>         else if (zeroAsTransparent)
>         {
>             GDALSetRasterNoDataValue(hDstBand, 0);
>         }
>     }
> 
>     return(true);
> }
> 
> This could produce output tiff raster file. However, I'm unable to add
> alpha channel to it.
> 
> Do you know how I could achieve it, I tried several options with
> CreateMaskBand, setting
> 
> CPLSetConfigOption("GDAL_TIFF_INTERNAL_MASK", "YES");
> 	CPLSetConfigOption("PROFILE", "GeoTIFF");
> 
> but without success!!!!

CreateMaskBand() creates a mask band, which is a different (but close) concept 
with alpha band. For a RGB image, if you want an alpha channel, you need to 
specify 4 as the band count for GDALCreate(). The alpha band will be the 4th 
one. If you use a alpha band, don't set a no data value on the other bands. 
Only edit the alpha band.


More information about the gdal-dev mailing list