[Geotiff] 16 bits per pixel images

Dieter Stüken stueken at conterra.de
Tue Sep 6 03:35:43 EDT 2005


Barry A. Rich wrote:
> I'm looking for guidance on how to handle images with 16 bits per pixel.
> 
> If the image has 16 bits per pixel, I'm assuming the following buffer 
> would be allocated to read one scan line:
> 
>     unsigned short* ScanlineBuf = (unsigned short*) malloc(
>     TIFFScanlineSize( tiff ) );
> 
> Each scan line would then be read as follows:
> 
>     TIFFReadScanline( tiff, ScanlineBuf, row, 0 );
> 
> I'm assuming ScanlineBuf is a series of 16-bit red, green, and blue 
> colors. Can someone verify that and tell me how to convert the 16-bit 
> colors to 8-bit values?

Please don't assume something, but verify additional tiff tags, too:

is TIFFTAG_SAMPLESPERPIXEL really 3 (using 3 colors for RGB) or is it
1, which indicates a gray scale image?

verify TIFFTAG_PHOTOMETRIC, if it is PHOTOMETRIC_RGB.

Also look at TIFFTAG_PLANARCONFIG if your pixel are organized contiguous
as sequences of RGBRGB... Else your image may be of type PLANARCONFIG_SEPARATE.
In this case your scanline will contain data of one color only, depending on
which sample you select (last parameter of TIFFReadScanline)

The value returned by TIFFScanlineSize() takes into account all that, so your
code example seems OK. But you should verify those settings before analyzing
the scanline content.

As you posted this question into the geotiff mailing list instead of the TIFF mailing
list, I assume your image is not a picture of your family, but some geographic or
sattelite data insted. So I suspect you may have a 16bit gray scale image.

But anyway:

Color reduction of 16bit values to 8bit values can be performed by dividing by 256.
Conversion from 8bit to 16bit is often performed by multiplying with 256 (or shifting
by 8 bits). This however maps the 8bit value of 255 (0xff) to 65280 (0xff00), which
is not white, as expected. Instead you should multiply by 257 instead to get:

0x00 -> 0x0000
0x01 -> 0x0101
0x02 -> 0x0202
...
0xff -> 0xffff

which maps white to white again.

Please note that if you get an 8bit palette image with color map, the color map entries
are 16bit!

Dieter.
-- 
Dieter Stüken, con terra GmbH, Münster
     stueken at conterra.de
     http://www.conterra.de/
     (0)251-7474-501


More information about the Geotiff mailing list