[Gdal-dev] color table

Frank Warmerdam warmerdam at pobox.com
Thu May 29 17:47:57 EDT 2003


Xiaodong Zhang wrote:
> Hi,
> 
> I want to use GDALRasterBand::SetColorTable(GDAL ColorTable * ctb) to 
> attach a color table to the image I created. How can I define the color 
> table or if I have a predefined color table (3 columns, each column for 
> R, G, B respectively), how can import that color table. Thanks.

Hi,

This is example code for creating a GDALColorTable object in C++.  The source
data in this case uses normalized color values (0.0 to 1.0) so it has to be
scaled to 0 to 255.  If you don't have alpha (transparency) values make sure
you set all alpha values to 255 for opaque.

         poCT = new GDALColorTable();
         for( int iColor = 0; iColor < nColors; iColor++ )
         {
             GDALColorEntry   sEntry;

             sEntry.c1 = (short) (padfRed[iColor]   * 255);
             sEntry.c2 = (short) (padfGreen[iColor] * 255);
             sEntry.c3 = (short) (padfBlue[iColor]  * 255);
             sEntry.c4 = (short) (padfAlpha[iColor]  * 255);
             poCT->SetColorEntry( iColor, &sEntry );
         }

Note that many drivers do not allow you to update a colortable in place.

Good luck,

-- 
---------------------------------------+--------------------------------------
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    | Geospatial Programmer for Rent





More information about the Gdal-dev mailing list