[Gdal-dev] Creating an empty ColorTable in C++

Frank Warmerdam warmerdam at pobox.com
Tue Aug 29 10:18:44 EDT 2006

Roger Bivand wrote:
> I'm trying to replicate some of the inner logic of rgb2cpt.py in a C++ 
> function from R. I'm stuck on how to declare/initialize a GDALColorTable 
> *ct for GDALComputeMedianCutPCT() and GDALDitherRGB2PCT() - I'm using:
>   GDALColorTable *ct = pRasterBand->GetColorTable();
> where pRasterBand is the red band of the input data set. This obviously 
> upsets GDALComputeMedianCutPCT() which seems to want a naked ColorTable, 
> and seg.faults:
> #0  0xb798bf3a in GDALColorTable::SetColorEntry ()
>    from /usr/local/lib/libgdal.so.1
> #1  0xb798bfe5 in GDALSetColorEntry () from /usr/local/lib/libgdal.so.1
> #2  0xb79b742e in GDALComputeMedianCutPCT () from /usr/local/lib/libgdal.so.1


GDALComputeMedianCutPCT() wants a GDALColorTableH.  You can create a new
clear color table for it using GDALCreateColorTable(GPI_RGB).

I suspect that the GetColorTable() is returning NULL which is causing the
crash later on.

Also, I notice you were using the C++ method to fetch the color table.
It is up to you, but if you were to use only the C API in an R interface
to GDAL it would make things much less "version fragile".  That is, new
versions of GDAL could be substituted without necessarily having to
recompile the R wrappers as the C API is much more ABI stable than the
C++ API.

Best regards,
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGeo, http://osgeo.org

More information about the Gdal-dev mailing list