2004.01.03 23:17 "[Tiff] Resampled TIFF doesn't open in viewer", by Pushkar Pradhan

2004.01.05 20:09 "Re: [Tiff] Resampled TIFF doesn't open in viewer", by Andrey Kiselev

Hi,

I resample a TIFF image (striped, 8 bits/sample, separate image planes, 1 band, width: 621, height: 577, rows/strip 13) by a factor of 2 using Nearest Neighbor method. Thus image width and height increase by 2. Moreover, I make the o/p format IEEE FP.

However, my viewer is unable to open the image (reports it's own errors). I correctly set the rows/strip tag to 26 as it is 13 in the input image.

You can set _any_ RowsPerStrip value for output image, because this parameter completely unrelated to the input one, it is only determined by the output image characteristics.

I have attached the sample code to read 8-bit image, process it and write out in form of floating point values.

I don't think I have to worry about stripbytecounts and stripoffsets as they cannot be set. But do I need to read these tags from the input image and use it somehow?

No, they are used internally by libtiff.

StripOffsets (273) LONG (4) 45 <8 32300 64592 96884 129176 161468 193760226052 258344 290636 322928 355220 387512 419804 452096 0 0 0 0 0 0 0 0 0

StripByteCounts (279) LONG (4) 45 <32292 32292 32292 32292 32292 32292 3229232292 32292 32292 32292 32292 32292 32292 32292 0 0 0 0 0 0 0 0 0 ...>

Looks like something goes wrong in your code. Please, chack out attached example.

Andrey

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

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

int main (int argc, char **argv)
{
    unsigned int    i;
    TIFF            *in, *out;
    uint16          spp, bpp, photo;
    uint32          image_width, image_height, npixels;
    tsize_t         instripsize, outstripsize;
    tstrip_t        instrips, outstrips;
    u_char          *inbuf, *outbuf;

    if (argc < 3)
    {
        fprintf(stdout, "Usage: %s infile.tif outfile.tif\n", argv[0]);
        return 1;
    }

/****************************************************************************/
/*  Open input file and get image characteristics.                          */
/****************************************************************************/

    in = TIFFOpen(argv[1], "r");
    if (!in)
    {
        fprintf (stderr, "Can't open %s for reading\n", argv[1]);
        return 2;
    }

    TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &image_width);
    TIFFGetField(in, TIFFTAG_IMAGELENGTH, &image_height);
    TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bpp);
    TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &spp);
    TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photo);

    if (bpp != 8)
    {
        fprintf (stderr, "Only 8bit input data supported.\n");
        TIFFClose(in);
        return 3;
    }

    instripsize = TIFFStripSize(in);
    npixels = image_width * image_height * spp;
    instrips = TIFFNumberOfStrips(in);
    inbuf = malloc(npixels * bpp/8); /* Should be equal to iinnstripsize*instrips */
    outbuf = malloc(npixels * sizeof(float));

/****************************************************************************/
/*  Read input data. We will read the full image in memory.                 */
/****************************************************************************/

    for (i = 0; i < instrips; i++)
        TIFFReadEncodedStrip(in, i, (tdata_t)(inbuf + i * instripsize),
                             instripsize);

/****************************************************************************/
/*  Perform processing. In our example we just multiply band values         */
/*  and their band numbers (1 based).                                       */
/****************************************************************************/

    for (i = 0; i < npixels; i++)
        ((float *)outbuf)[i] = (float)inbuf[i] * (float)(i % spp + 1);

/****************************************************************************/
/*  Open output file and prepare it for writing.                            */
/****************************************************************************/

    out = TIFFOpen(argv[2], "w");
    if (!in)
    {
        fprintf (stderr, "Can't open %s for writing\n", argv[2]);
        return 4;
    }

    TIFFSetField(out, TIFFTAG_IMAGEWIDTH, image_width);
    TIFFSetField(out, TIFFTAG_IMAGELENGTH, image_height);
    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, sizeof(float) * 8);
    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
    TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photo);
    TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
    TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, 0));
    outstripsize = TIFFStripSize(out);
    outstrips = TIFFNumberOfStrips(out);

/****************************************************************************/
/*  Write data out.                                                         */
/****************************************************************************/

    for (i = 0; i < outstrips; i++)
        TIFFWriteEncodedStrip(out, i, (tdata_t)(outbuf + i * outstripsize),
                              outstripsize);


/****************************************************************************/
/*  Clean all and exit.                                                     */
/****************************************************************************/

    _TIFFfree(inbuf);
    _TIFFfree(outbuf);
    TIFFClose(out);
    TIFFClose(in);

    return 0;
}