AWARE [SYSTEMS] Imaging expertise for the Delphi developer
AWare Systems, Imaging expertise for the Delphi developer, Home TIFF and LibTiff Mailing List Archive

LibTiff Mailing List

TIFF and LibTiff Mailing List Archive
January 2004

Previous Thread
Next Thread

Previous by Thread
Next by Thread

Previous by Date
Next by Date

Contact

The TIFF Mailing List Homepage
This list is run by Frank Warmerdam
Archive maintained by AWare Systems



Valid HTML 4.01!



Thread

2004.01.03 23:17 "Resampled TIFF doesn't open in viewer", by Pushkar Pradhan
2004.01.05 20:09 "Re: Resampled TIFF doesn't open in viewer", by Andrey Kiselev

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

Hi,

On Sat, Jan 03, 2004 at 05:17:41PM -0600, Pushkar Pradhan wrote:
> 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.

> Magic: 0x4949 <little-endian> Version: 0x2a
> Directory 0: offset 484388 (0x76424) next 0 (0)
> ImageWidth (256) SHORT (3) 1<1242>
> ImageLength (257) SHORT (3) 1<1154>
> BitsPerSample (258) SHORT (3) 1<32>
> Compression (259) SHORT (3) 1<1>
> Photometric (262) SHORT (3) 1<1>
> StripOffsets (273) LONG (4) 45<8 32300 64592 96884 129176 161468 193760
> 226052 258344 290636 322928 355220 387512 419804 452096 0 0 0 0 0 0 0 0 0
                                                         ^^^^^^^^^^^^^^^^^^
> ...>
> SamplesPerPixel (277) SHORT (3) 1<1>
> RowsPerStrip (278) SHORT (3) 1<26>
> StripByteCounts (279) LONG (4) 45<32292 32292 32292 32292 32292 32292 32292
> 32292 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;
}