[Gdal-dev] Adding GDALCreateColorRamp() to the GDAL C API

Ivan Lucena ivan.lucena at pmldnet.com
Tue May 8 01:36:52 EDT 2007


Hi there,

Someone from the list has pointed out a bug in the RST driver that happens
when converting a thematic raster image (color table + category names) from
RST to HFA:

   gdal_translate -of hfa thematic1.rst thematic.img
   Input file size is 800, 600
   0...10...20...30...40...50...60...70...80...90...100 - done.
   ERROR 1: iRow (-1) out of range.

That problem only happens when the associated color table file is missing (a
.smp file). It is unusual but it happens. So, in order to fix that problem I
have developed a function to automatically generate color table.

But instead of placing that function in the RST driver I asked Frank if we
should put that function for general use in the GDALColorTable and in the C
API. 

He agreed and he also suggested some changes that made it even more generic
and useful.

http://trac.osgeo.org/gdal/browser/trunk/gdal/gcore/gdalcolortable.cpp#L299 

What GDALCreateColorRamp() does is to create a linear distribution of values
in Red, Green, Blue and Alpha from the initial color to end color, starting
from one index to another anywhere in the color table.

   GDALColorEntry sColor1;
   sColor1.c1 = (short) ( 255 );
   sColor2.c2 = (short) ( 0 );
   sColor3.c3 = (short) ( 0 );
   sColor4.c4 = (short) ( 255 );

   GDALColorEntry sColor2;
   sColor2.c1 = (short) ( 0 );
   sColor2.c2 = (short) ( 0 );
   sColor2.c3 = (short) ( 255 );
   sColor2.c4 = (short) ( 255 );

  poColorTable->CreateColorRamp( 0, &sColor1, 32, &sColor2 );

It can also be used to create several ramp segments into the same color
table as follows:

   GDALColorEntry sColor1;
   sColor1.c1 = (short) ( 0 );
   sColor1.c2 = (short) ( 255 );
   sColor1.c3 = (short) ( 0 );
   sColor1.c4 = (short) ( 255 );

   GDALColorEntry sColor2;
   sColor2.c1 = (short) ( 100 );
   sColor2.c2 = (short) ( 0 );
   sColor2.c3 = (short) ( 255 );
   sColor2.c4 = (short) ( 255 );

   GDALColorEntry sColor3;
   sColor3.c1 = (short) ( 0 );
   sColor3.c2 = (short) ( 0 );
   sColor3.c3 = (short) ( 80 );
   sColor3.c4 = (short) ( 255 );

   poColorTable->CreateColorRamp( 0, &sColor1, 128, &sColor2 );
   poColorTable->CreateColorRamp( 129, &sColor2, 255, &sColor3 );

I hope that you will find it useful. There are others techniques that I
would like to explorer so any suggestion at this point, or anytime actually,
will be welcome.

With the best regards to all,

Ivan Lucena





More information about the Gdal-dev mailing list