2004.03.24 23:25 "[Tiff] Jpeg compressing a file", by Carl Collin

2004.03.29 13:01 "Re: [Tiff] Jpeg compressing a file", by Andrey Kiselev

it seems to work for some files, however, the files we are using are giving these errors, and they are spitting out alot of them... Any ideas?

JPEGSetupEncode: PhotometricInterpretation 3 not allowed for JPEG

It is not possible to use paletted images with JPEG compression. But you can convert that image to RGBA (using TIFFRGBAImage interface) and compress afterwards.

test.tif: Compression algorithm does not support random access.

This means that compression algorithm does not support random access :-)

Try the attached code. Most cases should be handled by the libtiff itself, but remember, that it is _simple_ solution as well, and it will not work in complex cases.

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 *tif, *out;
    uint32 image_width, image_height;
    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);
    buf = (char *)_TIFFmalloc(image_width * image_height * sizeof(uint32));
    TIFFReadRGBAImageOriented(tif, image_width, image_height, (u_long *)buf, ORIENTATION_TOPLEFT, 0);

    TIFFSetField(out, TIFFTAG_IMAGEWIDTH, image_width);
    TIFFSetField(out, TIFFTAG_IMAGELENGTH, image_height);
    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 4);
    TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);
    TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, 0));
    for (i = 0; i < image_height; i++)
        TIFFWriteScanline(out, buf + i * image_width * sizeof(uint32), i, 0);

    _TIFFfree(buf);
    TIFFClose(out);
    TIFFClose(tif);

    return 0;
}