2005.05.14 07:44 "[Tiff] Gray scale image", by Sivasundaram Suharnan

2005.05.16 04:59 "Re: [Tiff] Gray scale image", by Andrey Kiselev

I would like to use 16 bit gray scale tiff images,

I wounder how the gray scale color is assigned.

It works in the same way as 8-bit TIFFs. I have attached a simple example of the code to create 16-bit image.

Regards,
Andrey

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

#include <stdio.h>
#include "tiffio.h"

#define XSIZE 256
#define YSIZE 256

int main (int argc, char **argv)
{
    uint32 image_width, image_height;
    float xres, yres;
    uint16 spp, bpp, photo, res_unit;
    TIFF *out;
    int i, j;

    uint16 array[XSIZE * YSIZE];

    for (j = 0; j < YSIZE; j++)
            for(i = 0; i < XSIZE; i++)
                    array[j * XSIZE + i] = i * j;

    out = TIFFOpen("out.tif", "w");
    if (!out)
    {
            fprintf (stderr, "Can't open %s for writing\n", argv[1]);
            return 1;
    }
    image_width = XSIZE;
    image_height = YSIZE;
    spp = 1; /* Samples per pixel */
    bpp = 16; /* Bits per sample */
    photo = PHOTOMETRIC_MINISBLACK;
    TIFFSetField(out, TIFFTAG_IMAGEWIDTH, image_width / spp);
    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bpp);
    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
    TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photo);
    TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
    /* It is good to set resolutions too (but it is not nesessary) */
    xres = yres = 100;
    res_unit = RESUNIT_INCH;
    TIFFSetField(out, TIFFTAG_XRESOLUTION, xres);
    TIFFSetField(out, TIFFTAG_YRESOLUTION, yres);
    TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, res_unit);

    for (j = 0; j < image_height; j++)
        TIFFWriteScanline(out, &array[j * image_width], j, 0);

    TIFFClose(out);

    return 0;
}