2004.07.26 12:23 "[Tiff] Saving multiple images to a tiff file...", by Sanjay Gupta

2004.07.29 16:29 "Re: [Tiff] Saving multiple images to a tiff file...", by Andrey Kiselev

I have a set of pixel data, which i want to store in a tiff file. All the elements from set of pixel data needs to be saved in the same file, thereby creating a tiff file with multiple images.

I have the following code,

for (k = 0; k < frameCount; k++) {
                data = image->frameDimensionList [j].frames [k];
        TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, data.Width);
        TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, data.Height);


        TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 4); /* Hardcoded 32bbps*/
        TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);

        linebytes =  data.Stride;
        TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize (tiff, linebytes));

        /*write data*/
        for (i = 0; i < data.Height; i++) {
                if (TIFFWriteScanline (tiff, data.Scan0 + i * data.Stride, i, 0) < 0) /*Scan0 points to the char * for pixels*/

        TIFFWriteDirectory (tiff);

Now when i try to reload the image from my program, the saved file is not recognized as a valid tiff file. The problem what i am seeing is, my program is not saving the correct dircount for the tiff file, and while reading the tiff image, some junk value is read, because of which loading of image doesnt work. What else needs to be done in my above code, to make it work properly. The above piece of code works fine it there is only 1 image which needs to go in a tiff file.


I have attached a sample code to create the multipage TIFF. Note, that the following imortant peace of code missed in your source:

        /* We are writing single page of the multipage file */
        /* Set the page number */
        TIFFSetField(out, TIFFTAG_PAGENUMBER, page, NPAGES);

I have posted that source here several times, so don't forget to search the list archive from time to time: Joris has made a very good job to set up powerful Web interface for our archive!


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

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

#define XSIZE 256
#define YSIZE 256
#define NPAGES 10

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 page;

    unsigned char array[XSIZE * YSIZE];

    for (j = 0; j < YSIZE; j++)
            for(i = 0; i < XSIZE; i++)
                    array[j * XSIZE + i] = (unsigned char)(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 = 8; /* Bits per sample */

    for (page = 0; page < NPAGES; page++)
        TIFFSetField(out, TIFFTAG_IMAGEWIDTH, image_width / spp);
        TIFFSetField(out, TIFFTAG_IMAGELENGTH, image_height);
        TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bpp);
        TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
        TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photo);
        /* 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);

        /* We are writing single page of the multipage file */
        /* Set the page number */
        TIFFSetField(out, TIFFTAG_PAGENUMBER, page, NPAGES);

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



    return 0;