Opened 17 years ago

Last modified 17 years ago

#1732 closed defect

Possible error with Imagine Palettized images (rounding vs truncating) — at Version 1

Reported by: mrosen Owned by: warmerdam
Priority: normal Milestone: 1.4.3
Component: GDAL_Raster Version: unspecified
Severity: normal Keywords: hfa
Cc:

Description (last modified by warmerdam)

I was chasing down an issue with GDAL's support for palettized Imagine files and think I found a defect in HFARasterBand. In the construtor we convert from a 0..1 double to a 0..255 short. But we do so by truncation:

            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 );

Currently, this code suffers from two difficulties: (a) it gives different answers on different platforms (b) rounding rather than truncation would seem to be the natural way to map from [0..1] to [0..255].

The first problem is probably unavoidable since floating point arithmetic is involved. Solving the second problem can be solved by something like "(short)((padfRed[iColor]*255+0.5))".

I tried this and found that the input table did seem more correct (true grey palette in repro below). However, then a subsquent call (from the application layer) to the band's GetMetadataItem("STATISTICS_MEAN") returns an empty string (where it did not prior to the change ... spurious?)

REPRO

I'm attaching a small repro image "grey.img." Despite the name, it contains an RGB lookup table. Under linux/gcc3.2 the lookup table is not quite grey; under win32/vc8 it creates slightly different not-quite grey lookup table. I've confirmed the differences are due to FP rounding. Looking at the values read from the table and the name of the file, the obvious intent is that the table be an RGB palette that implements gray scale. Unfortunately, I used application level printf() calls to dump the entries in the table.

A short demo of the FP rounding issue based on the second entry in the CLUT:

	double df = 0.0039215686274509803; // taken from CLUT, entry #2.
	short s;
	s = (short)((df  * 255)+0.5);
	printf ("df = %lf, s = %d\n", df, s);  // linux/gcc32, s==0; win32/vc8 s==1
	return 0;

I verified that ArcMap 9.2 will read this image and indicates a true greyscale palette!

Change History (2)

by mrosen, 17 years ago

Attachment: grey.img added

repro image for 1732

comment:1 by warmerdam, 17 years ago

Component: defaultGDAL_Raster
Description: modified (diff)
Keywords: hfa added
Milestone: 1.5.0
Status: newassigned
Note: See TracTickets for help on using tickets.