[gdal-dev] The use of GDALColorTable
Frank Warmerdam
warmerdam at pobox.com
Thu Jul 9 20:59:31 EDT 2009
Belaid MOA wrote:
> Greetings everyone,
> I noticed that some of the GTiff images I am working with are single
> bands and have a ColorInterp=Palette (they are colored when viewed in
> tiff viewer). So, I assume they are using a Color Table to achieve that.
> Am I right? Where can I find some tutorials or C/C++ examples using
> GDALColorTable to associate a color table with a single band image?
Belaid,
The following is an example of creating a color table:
// Allocate memory for colour table and read it
if ( poDS->sInfoHeader.iClrUsed )
poDS->nColorTableSize = poDS->sInfoHeader.iClrUsed;
else
poDS->nColorTableSize = 1 << poDS->sInfoHeader.iBitCount;
poDS->pabyColorTable =
(GByte *)VSIMalloc2( poDS->nColorElems, poDS->nColorTableSize );
if (poDS->pabyColorTable == NULL)
{
CPLError(CE_Failure, CPLE_OutOfMemory, "Color palette will be
ignored");
poDS->nColorTableSize = 0;
break;
}
VSIFSeekL( poDS->fp, BFH_SIZE + poDS->sInfoHeader.iSize, SEEK_SET );
VSIFReadL( poDS->pabyColorTable, poDS->nColorElems,
poDS->nColorTableSize, poDS->fp );
GDALColorEntry oEntry;
poDS->poColorTable = new GDALColorTable();
for( i = 0; i < poDS->nColorTableSize; i++ )
{
oEntry.c1 = poDS->pabyColorTable[i * poDS->nColorElems + 2];
// Red
oEntry.c2 = poDS->pabyColorTable[i * poDS->nColorElems + 1];
// Green
oEntry.c3 = poDS->pabyColorTable[i * poDS->nColorElems];
// Blue
oEntry.c4 = 255;
poDS->poColorTable->SetColorEntry( i, &oEntry );
}
The color table can be associated with a band using:
GDALSetColorTable(GDALRasterBandH,GDALColorTableH);
In the case of creating a GeoTIFF file, you may need to
pass the creation option PHOTOMETRIC=PALETTE to ensure the
created file has a palette.
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 | Geospatial Programmer for Rent
More information about the gdal-dev
mailing list