[Gdal-dev] 8-bit grayscale

Andrey Kiselev dron at ak4719.spb.edu
Sat Jul 3 06:02:40 EDT 2004


Hello, Michele,

On Thu, Jul 01, 2004 at 11:04:58AM +0200, Sanges Michele wrote:
> - when the method GetColorInterpretation() returns "Undefined" (for
> example, the "/aig/dblbnd.adf" test file), in order to represent the
> dataset in an 8?bit grayscale, I used the following conversion:
> 
> int nRasterCnt = poDataset->GetRasterCount();
> int nRasterXSize =  poDataset->GetRasterXSize();
> int nRasterYSize =  poDataset->GetRasterYSize();
> double adfMinMax[2];
> int bGotMin, bGotMax;
> adfMinMax[0]=poBand->GetMinimum(&bGotMin);
> adfMinMax[1]=poBand->GetMaximum(&bGotMax);

Don't forget to check bGotMin and bGotMax before using min/max values.

> poBand->RasterIO(GF_Read, 0, 0, nXSize, nYSize, pImageData, nXSize, nYSize, poBand->GetRasterDataType(), 0, 0 );
> 
> for (int nByte = 0; nByte < nRasterCnt*nRasterXSize*nRasterYSize; nByte++)
> {
> 	// convert in 0..255 range /(8?bit grayscale)
> 	int nApp = (255 / (adfMinMax[1] - adfMinMax[0])) * (*(pImageData + nByte) - adfMinMax[0]); 
> 
> 	if (nApp < 0)
> 	{
> 		nApp = 0;
> 	}
> 	else if  (nApp > 255)
> 	{
> 		nApp = 255;
> 	}
> 
> 	*(pData + nByte) =  nApp;
> }
> 
> Fl_RGB_Image * RGBImg = new Fl_RGB_Image((uchar*)(pData), nRasterXSize, nRasterYSize, poDataset->GetRasterCount());
> 
> Using this conversion, the dblbnd.adf file is represented too dark,
> probabily because the dataset has many low values. I can solve this
> problem reducing the range of 8 bit grayscale to [N..255] (N > 0), but
> how do you generally converted the dataset?

It is not possible to wrap an arbitrary dataset into 8-bit. Any good
viewer for non-8bit data should have several scaling algorithms: linear
stretch (as in your code), equalization, logarithmic stretch. User can
select one of them which will be best suited for particular dataset. We
don't know, what parts of image are interesting for user. Ocean and land
on the single space image will have significantly different histograms
and user will use different methods to highlight water or land depending
on his interests.

						Regards,
						Andrey

-- 
Andrey V. Kiselev
Home phone:  +7 812 5274898  ICQ# 26871517



More information about the Gdal-dev mailing list