AWARE SYSTEMS
TIFF and LibTiff Mail List Archive

Thread

2003.03.18 22:58 "Grayscale 16-bit images", by Claus Ekstroem
2003.03.19 09:59 "Re: Grayscale 16-bit images", by Andrey Kiselev

2003.03.19 09:59 "Re: Grayscale 16-bit images", by Andrey Kiselev

I'm trying to read a 16-bit grayscale image:

I figure reading the file as a RGBAImage is the wrong approach, so could someone mail me a couple of lines of code to read and access the individual pixels?

You should use something like:

TIFF *tif;
uint16 spp, bpp, photo;
uint32 width, height;
char *buf;

tif = TIFFOpen(filename, "r");
if (!tif)
{
        fprintf (stderr, "Can't open %s for reading\n", filename);
      return 2;
}

TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &height);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bpp);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &spp);
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photo);
linesize = TIFFScanlineSize(tif);
buf = malloc(linesize * height);
for (i = 0; i < height; i++)
    TIFFReadScanline(tif, &buf[i * linesize], i, 0);

TIFFClose(tif);

The whole image will be read into buffer. After this you can access individual pixels according bpp and spp values, i.e. for 16 bit unsigned integer: ((uint16 *)buf)[i].

Regards,
Andrey

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