2005.06.07 13:11 "[Tiff] simple image copy", by Nenad Nesic

2005.06.07 17:48 "Re: [Tiff] simple image copy", by Andrey Kiselev

I´m totally new to libtiff and I´m happy about that it is now working in my Visual Studio C++. I just wanna copy the content of an image and save it as another file. How can I do this? I tried this one:

 TIFF* tif=TIFFOpen("e:/input.tif","r");
 TIFF* tif2=TIFFOpen("e:/output.tif","w");

but it doesn´t work.
Is there a function for copying data/image?


That is not trivial to copy arbitrary TIFF file to another one. Look at the tiffcp utility sources. This is largest utility in the whole package and it was designed just to copy contents of the one file to another one. (Besides of copying it can change the file layout, that adds complexity to the task).

I have attached the simple code you can start from. It works for pixel interleaved stripped images, but do not forget about other sorts of possible TIFFs.

Best regards,

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

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

int main (int argc, char **argv)
    unsigned int i;
    TIFF *tif, *out;
    uint16 spp, bpp, photo;
    uint32 image_width, image_height, scansize;
    char *buf;

    if(argc < 3)
        fprintf(stderr, "\nUSAGE: tiffcopy infile.tif outfile.tif\n");
        return 1;

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

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

    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &image_width);
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &image_height);
    TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photo);
    scansize = TIFFScanlineSize(tif);
    buf = _TIFFmalloc(scansize * image_height);
    for (i = 0; i < image_height; i++)
        TIFFReadScanline(tif, buf + i * scansize, i, 0);

    TIFFSetField(out, TIFFTAG_IMAGEWIDTH, image_width);
    TIFFSetField(out, TIFFTAG_IMAGELENGTH, image_height);
    TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photo);
    TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, 0));
    for (i = 0; i < image_height; i++)
        TIFFWriteScanline(out, buf + i * scansize, i, 0);


    return 0;