2004.12.12 10:44 "[Tiff] Question about colormap", by Artem Y. Pervin

2004.12.14 21:06 "Re: [Tiff] Question about colormap", by Andrey Kiselev

Artem,

I`ve a little question about TIFF`s colormap.

Suppose I got RGBA raster (width*height) somehow, and now wish to store it in a file, using just one byte per pixel and a colormap. Is there are any issues (I failed to find :( ) about working with palettes in libtiff?

Actually I`m interesting in how to generate a colormap. Suppose I know every different color in my raster, and amount of these colors is always less than 255, so its not gonna be a big problem to make such colormap, but how would I store it into TIFF, and then how to store original raster?

You can find the sample code attached (this utility creates gray colored TIFF, but it has color map). Note, that TIFF palettes contain more than 256 colors.

С уважением,
Первин Артем

Not sure that this makes sence for majority of the list readers ;-)

Andrey

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

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

#define XSIZE 16
#define YSIZE 16
#define NCOLORS ((XSIZE)*(YSIZE))

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

    char array[XSIZE * YSIZE];

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

    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 = 8; /* Bits per sample */
    photo = PHOTOMETRIC_PALETTE;
    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);

    /* Allocate and fill color tables */
    red_tbl = (uint16*) _TIFFmalloc(1<<bpp * sizeof(uint16));
    green_tbl = (uint16*) _TIFFmalloc(1<<bpp * sizeof(uint16));
    blue_tbl = (uint16*) _TIFFmalloc(1<<bpp * sizeof(uint16));
    if (!red_tbl || !green_tbl || !blue_tbl)
    {
        fprintf(stderr, "Can't allocate space for color component tables.");
        return 2;
    }
    for(i = 0; i < NCOLORS; i++)
    {
        red_tbl[i] = 257 * i;
        green_tbl[i] = 257 * i;
        blue_tbl[i] = 257 * i;
    }
    TIFFSetField(out, TIFFTAG_COLORMAP, red_tbl, green_tbl, blue_tbl);

    TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
    /* If you need compression*/
    /* TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE); */
    TIFFSetField(out,TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, 0));
    /* 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);

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

    _TIFFfree(red_tbl);
    _TIFFfree(green_tbl);
    _TIFFfree(blue_tbl);
    TIFFClose(out);

    return 0;
}