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

Roger Bivand Roger.Bivand at nhh.no
Tue Aug 29 15:07:20 EDT 2006

On Tue, 29 Aug 2006, Frank Warmerdam wrote:

> 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
> Roger,
> GDALComputeMedianCutPCT() wants a GDALColorTableH.  You can create a new
> clear color table for it using GDALCreateColorTable(GPI_RGB).


Thanks, that helped a little.

My plan was to pass from R to GDAL the handles to two data sets, the 
output one of which would get populated by the color index band and the 
color table. I now see a clean error exit:

Error in rgdal:::SGDF2RGB(xx) : 
        GDAL Error 6: SetColorTable() not supported for existing TIFF files.

which suggests that I should rather initiate the output dataset in my 
C/C++ code.

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

I'm very much building on and maintaining Tim Keitt's elegant C++ code, 
which is written C++ style; as is evident C++ is not a language I enjoy, 
but I would not want to have to rewrite Tim's earlier work. So the code 
has stayed a mixture of elegance where I haven't done anything and 
muddling through where I have written things.

The idea of having a rapid RGB display mechanism for three-band objects 
was Tim's, but I thought I'd have a look. I may end up just writing a file 
for rgb2pct.py to convert from the R level.

Best wishes,


> Best regards,

Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no

More information about the Gdal-dev mailing list