[Gdal-dev] How to set a ColorTable from scratch?

Joaquim Luis jluis at ualg.pt
Thu Jun 22 15:13:05 EDT 2006


Frank Warmerdam wrote:
> Joaquim Luis wrote:
>> Hi,
>> I finally learned how to use the Create() method. However, I didn't 
>> find out how to create a new colortable.
>> There is the GDALSetRasterColorTable(GDALRasterBandH, 
>> GDALColorTableH) but how do we fill the
>> GDALColorTableH?
>
> Joaquim ,
>
> The following code from gdal/alg/gdalmediancut.cpp shows adding
> a bunch of entries to a color table.
>
>     for (i = 0, ptr = usedboxes; ptr != NULL; ++i, ptr = ptr->next)
>     {
>         GDALColorEntry    sEntry;
>
>         sEntry.c1 = (GByte) (((ptr->rmin + ptr->rmax) << COLOR_SHIFT) 
> / 2);
>         sEntry.c2 = (GByte) (((ptr->gmin + ptr->gmax) << COLOR_SHIFT) 
> / 2);
>         sEntry.c3 = (GByte) (((ptr->bmin + ptr->bmax) << COLOR_SHIFT) 
> / 2);
>         GDALSetColorEntry( hColorTable, i, &sEntry );
>     }
>
Frank,
I had already came across similar code and I wanted to try it, though 
expecting a crash as effectively
I just had.
The point (that is what I think) is who initializes hColorTable? I mean 
how do we allocate memory to it?
Currently I just have
GDALColorTableH    hColorTable;
GDALColorEntry    sEntry;

for (i = 0; i < nColors; i++) {
                sEntry.c1 = (short)(ptr_d[i] * 255);
                sEntry.c2 = (short)(ptr_d[i+nColors] * 255);
                sEntry.c3 = (short)(ptr_d[i+2*nColors] * 255);
                sEntry.c4 = (short)255;
                GDALSetColorEntry( hColorTable, i, &sEntry );

}

but it crashes at the first call to
    GDALSetColorEntry( hColorTable, i, &sEntry );

Joaquim Luis



More information about the Gdal-dev mailing list