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

mighty_duck msiljegovic at gmail.com
Wed Oct 3 12:03:27 PDT 2012


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!!!!

Thanks in advance...



--
View this message in context: http://osgeo-org.1560.n6.nabble.com/How-to-add-alpha-channel-to-tiff-image-C-code-tp5006218.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list