Blame tools/tiff2rgba.c

Packit 7838c8
/* $Id: tiff2rgba.c,v 1.22 2016-08-15 20:06:41 erouault Exp $ */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Copyright (c) 1991-1997 Sam Leffler
Packit 7838c8
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
Packit 7838c8
 *
Packit 7838c8
 * Permission to use, copy, modify, distribute, and sell this software and 
Packit 7838c8
 * its documentation for any purpose is hereby granted without fee, provided
Packit 7838c8
 * that (i) the above copyright notices and this permission notice appear in
Packit 7838c8
 * all copies of the software and related documentation, and (ii) the names of
Packit 7838c8
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
Packit 7838c8
 * publicity relating to the software without the specific, prior written
Packit 7838c8
 * permission of Sam Leffler and Silicon Graphics.
Packit 7838c8
 *
Packit 7838c8
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
Packit 7838c8
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
Packit 7838c8
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit 7838c8
 *
Packit 7838c8
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
Packit 7838c8
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
Packit 7838c8
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Packit 7838c8
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
Packit 7838c8
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
Packit 7838c8
 * OF THIS SOFTWARE.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#include "tif_config.h"
Packit 7838c8
Packit 7838c8
#include <stdio.h>
Packit 7838c8
#include <string.h>
Packit 7838c8
#include <stdlib.h>
Packit 7838c8
Packit 7838c8
#ifdef HAVE_UNISTD_H
Packit 7838c8
# include <unistd.h>
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
#ifdef NEED_LIBPORT
Packit 7838c8
# include "libport.h"
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
#include "tiffiop.h"
Packit 7838c8
#include "tiffio.h"
Packit 7838c8
Packit 7838c8
#define	streq(a,b)	(strcmp(a,b) == 0)
Packit 7838c8
#define	CopyField(tag, v) \
Packit 7838c8
    if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
Packit 7838c8
Packit 7838c8
#ifndef howmany
Packit 7838c8
#define	howmany(x, y)	(((x)+((y)-1))/(y))
Packit 7838c8
#endif
Packit 7838c8
#define	roundup(x, y)	(howmany(x,y)*((uint32)(y)))
Packit 7838c8
Packit 7838c8
uint16 compression = COMPRESSION_PACKBITS;
Packit 7838c8
uint32 rowsperstrip = (uint32) -1;
Packit 7838c8
int process_by_block = 0; /* default is whole image at once */
Packit 7838c8
int no_alpha = 0;
Packit 7838c8
int bigtiff_output = 0;
Packit 7838c8
Packit 7838c8
Packit 7838c8
static int tiffcvt(TIFF* in, TIFF* out);
Packit 7838c8
static void usage(int code);
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
main(int argc, char* argv[])
Packit 7838c8
{
Packit 7838c8
	TIFF *in, *out;
Packit 7838c8
	int c;
Packit 7838c8
#if !HAVE_DECL_OPTARG
Packit 7838c8
	extern int optind;
Packit 7838c8
	extern char *optarg;
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
	while ((c = getopt(argc, argv, "c:r:t:bn8")) != -1)
Packit 7838c8
		switch (c) {
Packit 7838c8
			case 'b':
Packit 7838c8
				process_by_block = 1;
Packit 7838c8
				break;
Packit 7838c8
Packit 7838c8
			case 'c':
Packit 7838c8
				if (streq(optarg, "none"))
Packit 7838c8
					compression = COMPRESSION_NONE;
Packit 7838c8
				else if (streq(optarg, "packbits"))
Packit 7838c8
					compression = COMPRESSION_PACKBITS;
Packit 7838c8
				else if (streq(optarg, "lzw"))
Packit 7838c8
					compression = COMPRESSION_LZW;
Packit 7838c8
				else if (streq(optarg, "jpeg"))
Packit 7838c8
					compression = COMPRESSION_JPEG;
Packit 7838c8
				else if (streq(optarg, "zip"))
Packit 7838c8
					compression = COMPRESSION_DEFLATE;
Packit 7838c8
				else
Packit 7838c8
					usage(-1);
Packit 7838c8
				break;
Packit 7838c8
Packit 7838c8
			case 'r':
Packit 7838c8
				rowsperstrip = atoi(optarg);
Packit 7838c8
				break;
Packit 7838c8
Packit 7838c8
			case 't':
Packit 7838c8
				rowsperstrip = atoi(optarg);
Packit 7838c8
				break;
Packit 7838c8
Packit 7838c8
			case 'n':
Packit 7838c8
				no_alpha = 1;
Packit 7838c8
				break;
Packit 7838c8
Packit 7838c8
			case '8':
Packit 7838c8
				bigtiff_output = 1;
Packit 7838c8
				break;
Packit 7838c8
Packit 7838c8
			case '?':
Packit 7838c8
				usage(0);
Packit 7838c8
				/*NOTREACHED*/
Packit 7838c8
		}
Packit 7838c8
Packit 7838c8
	if (argc - optind < 2)
Packit 7838c8
		usage(-1);
Packit 7838c8
Packit 7838c8
	out = TIFFOpen(argv[argc-1], bigtiff_output?"w8":"w");
Packit 7838c8
	if (out == NULL)
Packit 7838c8
		return (-2);
Packit 7838c8
Packit 7838c8
	for (; optind < argc-1; optind++) {
Packit 7838c8
		in = TIFFOpen(argv[optind], "r");
Packit 7838c8
		if (in != NULL) {
Packit 7838c8
			do {
Packit 7838c8
				if (!tiffcvt(in, out) ||
Packit 7838c8
				    !TIFFWriteDirectory(out)) {
Packit 7838c8
					(void) TIFFClose(out);
Packit 7838c8
					(void) TIFFClose(in);
Packit 7838c8
					return (1);
Packit 7838c8
				}
Packit 7838c8
			} while (TIFFReadDirectory(in));
Packit 7838c8
			(void) TIFFClose(in);
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
	(void) TIFFClose(out);
Packit 7838c8
	return (0);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
cvt_by_tile( TIFF *in, TIFF *out )
Packit 7838c8
Packit 7838c8
{
Packit 7838c8
    uint32* raster;			/* retrieve RGBA image */
Packit 7838c8
    uint32  width, height;		/* image width & height */
Packit 7838c8
    uint32  tile_width, tile_height;
Packit 7838c8
    uint32  row, col;
Packit 7838c8
    uint32  *wrk_line;
Packit 7838c8
    int	    ok = 1;
Packit 7838c8
    uint32  rastersize, wrk_linesize;
Packit 7838c8
Packit 7838c8
    TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
Packit 7838c8
    TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
Packit 7838c8
Packit 7838c8
    if( !TIFFGetField(in, TIFFTAG_TILEWIDTH, &tile_width)
Packit 7838c8
        || !TIFFGetField(in, TIFFTAG_TILELENGTH, &tile_height) ) {
Packit 7838c8
        TIFFError(TIFFFileName(in), "Source image not tiled");
Packit 7838c8
        return (0);
Packit 7838c8
    }
Packit 7838c8
    
Packit 7838c8
    TIFFSetField(out, TIFFTAG_TILEWIDTH, tile_width );
Packit 7838c8
    TIFFSetField(out, TIFFTAG_TILELENGTH, tile_height );
Packit 7838c8
Packit 7838c8
    /*
Packit 7838c8
     * Allocate tile buffer
Packit 7838c8
     */
Packit 7838c8
    rastersize = tile_width * tile_height * sizeof (uint32);
Packit 7838c8
    if (tile_width != (rastersize / tile_height) / sizeof( uint32))
Packit 7838c8
    {
Packit 7838c8
	TIFFError(TIFFFileName(in), "Integer overflow when calculating raster buffer");
Packit 7838c8
	exit(-1);
Packit 7838c8
    }
Packit 7838c8
    raster = (uint32*)_TIFFmalloc(rastersize);
Packit 7838c8
    if (raster == 0) {
Packit 7838c8
        TIFFError(TIFFFileName(in), "No space for raster buffer");
Packit 7838c8
        return (0);
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    /*
Packit 7838c8
     * Allocate a scanline buffer for swapping during the vertical
Packit 7838c8
     * mirroring pass.
Packit 7838c8
     */
Packit 7838c8
    wrk_linesize = tile_width * sizeof (uint32);
Packit 7838c8
    if (tile_width != wrk_linesize / sizeof (uint32))
Packit 7838c8
    {
Packit 7838c8
        TIFFError(TIFFFileName(in), "Integer overflow when calculating wrk_line buffer");
Packit 7838c8
	exit(-1);
Packit 7838c8
    }
Packit 7838c8
    wrk_line = (uint32*)_TIFFmalloc(wrk_linesize);
Packit 7838c8
    if (!wrk_line) {
Packit 7838c8
        TIFFError(TIFFFileName(in), "No space for raster scanline buffer");
Packit 7838c8
        ok = 0;
Packit 7838c8
    }
Packit 7838c8
    
Packit 7838c8
    /*
Packit 7838c8
     * Loop over the tiles.
Packit 7838c8
     */
Packit 7838c8
    for( row = 0; ok && row < height; row += tile_height )
Packit 7838c8
    {
Packit 7838c8
        for( col = 0; ok && col < width; col += tile_width )
Packit 7838c8
        {
Packit 7838c8
            uint32 i_row;
Packit 7838c8
Packit 7838c8
            /* Read the tile into an RGBA array */
Packit 7838c8
            if (!TIFFReadRGBATile(in, col, row, raster)) {
Packit 7838c8
                ok = 0;
Packit 7838c8
                break;
Packit 7838c8
            }
Packit 7838c8
Packit 7838c8
Packit 7838c8
	    /*
Packit 7838c8
	     * XXX: raster array has 4-byte unsigned integer type, that is why
Packit 7838c8
	     * we should rearrange it here.
Packit 7838c8
	     */
Packit 7838c8
#if HOST_BIGENDIAN
Packit 7838c8
	    TIFFSwabArrayOfLong(raster, tile_width * tile_height);
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
            /*
Packit 7838c8
             * For some reason the TIFFReadRGBATile() function chooses the
Packit 7838c8
             * lower left corner as the origin.  Vertically mirror scanlines.
Packit 7838c8
             */
Packit 7838c8
            for( i_row = 0; i_row < tile_height / 2; i_row++ )
Packit 7838c8
            {
Packit 7838c8
                uint32	*top_line, *bottom_line;
Packit 7838c8
Packit 7838c8
                top_line = raster + tile_width * i_row;
Packit 7838c8
                bottom_line = raster + tile_width * (tile_height-i_row-1);
Packit 7838c8
Packit 7838c8
                _TIFFmemcpy(wrk_line, top_line, 4*tile_width);
Packit 7838c8
                _TIFFmemcpy(top_line, bottom_line, 4*tile_width);
Packit 7838c8
                _TIFFmemcpy(bottom_line, wrk_line, 4*tile_width);
Packit 7838c8
            }
Packit 7838c8
Packit 7838c8
            /*
Packit 7838c8
             * Write out the result in a tile.
Packit 7838c8
             */
Packit 7838c8
Packit 7838c8
            if( TIFFWriteEncodedTile( out,
Packit 7838c8
                                      TIFFComputeTile( out, col, row, 0, 0),
Packit 7838c8
                                      raster,
Packit 7838c8
                                      4 * tile_width * tile_height ) == -1 )
Packit 7838c8
            {
Packit 7838c8
                ok = 0;
Packit 7838c8
                break;
Packit 7838c8
            }
Packit 7838c8
        }
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    _TIFFfree( raster );
Packit 7838c8
    _TIFFfree( wrk_line );
Packit 7838c8
Packit 7838c8
    return ok;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
cvt_by_strip( TIFF *in, TIFF *out )
Packit 7838c8
Packit 7838c8
{
Packit 7838c8
    uint32* raster;			/* retrieve RGBA image */
Packit 7838c8
    uint32  width, height;		/* image width & height */
Packit 7838c8
    uint32  row;
Packit 7838c8
    uint32  *wrk_line;
Packit 7838c8
    int	    ok = 1;
Packit 7838c8
    uint32  rastersize, wrk_linesize;
Packit 7838c8
Packit 7838c8
    TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
Packit 7838c8
    TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
Packit 7838c8
Packit 7838c8
    if( !TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip) ) {
Packit 7838c8
        TIFFError(TIFFFileName(in), "Source image not in strips");
Packit 7838c8
        return (0);
Packit 7838c8
    }
Packit 7838c8
    
Packit 7838c8
    TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
Packit 7838c8
Packit 7838c8
    /*
Packit 7838c8
     * Allocate strip buffer
Packit 7838c8
     */
Packit 7838c8
    rastersize = width * rowsperstrip * sizeof (uint32);
Packit 7838c8
    if (width != (rastersize / rowsperstrip) / sizeof( uint32))
Packit 7838c8
    {
Packit 7838c8
	TIFFError(TIFFFileName(in), "Integer overflow when calculating raster buffer");
Packit 7838c8
	exit(-1);
Packit 7838c8
    }
Packit 7838c8
    raster = (uint32*)_TIFFmalloc(rastersize);
Packit 7838c8
    if (raster == 0) {
Packit 7838c8
        TIFFError(TIFFFileName(in), "No space for raster buffer");
Packit 7838c8
        return (0);
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    /*
Packit 7838c8
     * Allocate a scanline buffer for swapping during the vertical
Packit 7838c8
     * mirroring pass.
Packit 7838c8
     */
Packit 7838c8
    wrk_linesize = width * sizeof (uint32);
Packit 7838c8
    if (width != wrk_linesize / sizeof (uint32))
Packit 7838c8
    {
Packit 7838c8
        TIFFError(TIFFFileName(in), "Integer overflow when calculating wrk_line buffer");
Packit 7838c8
	exit(-1);
Packit 7838c8
    }
Packit 7838c8
    wrk_line = (uint32*)_TIFFmalloc(wrk_linesize);
Packit 7838c8
    if (!wrk_line) {
Packit 7838c8
        TIFFError(TIFFFileName(in), "No space for raster scanline buffer");
Packit 7838c8
        ok = 0;
Packit 7838c8
    }
Packit 7838c8
    
Packit 7838c8
    /*
Packit 7838c8
     * Loop over the strips.
Packit 7838c8
     */
Packit 7838c8
    for( row = 0; ok && row < height; row += rowsperstrip )
Packit 7838c8
    {
Packit 7838c8
        int	rows_to_write, i_row;
Packit 7838c8
Packit 7838c8
        /* Read the strip into an RGBA array */
Packit 7838c8
        if (!TIFFReadRGBAStrip(in, row, raster)) {
Packit 7838c8
            ok = 0;
Packit 7838c8
            break;
Packit 7838c8
        }
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * XXX: raster array has 4-byte unsigned integer type, that is why
Packit 7838c8
	 * we should rearrange it here.
Packit 7838c8
	 */
Packit 7838c8
#if HOST_BIGENDIAN
Packit 7838c8
	TIFFSwabArrayOfLong(raster, width * rowsperstrip);
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
        /*
Packit 7838c8
         * Figure out the number of scanlines actually in this strip.
Packit 7838c8
         */
Packit 7838c8
        if( row + rowsperstrip > height )
Packit 7838c8
            rows_to_write = height - row;
Packit 7838c8
        else
Packit 7838c8
            rows_to_write = rowsperstrip;
Packit 7838c8
Packit 7838c8
        /*
Packit 7838c8
         * For some reason the TIFFReadRGBAStrip() function chooses the
Packit 7838c8
         * lower left corner as the origin.  Vertically mirror scanlines.
Packit 7838c8
         */
Packit 7838c8
Packit 7838c8
        for( i_row = 0; i_row < rows_to_write / 2; i_row++ )
Packit 7838c8
        {
Packit 7838c8
            uint32	*top_line, *bottom_line;
Packit 7838c8
Packit 7838c8
            top_line = raster + width * i_row;
Packit 7838c8
            bottom_line = raster + width * (rows_to_write-i_row-1);
Packit 7838c8
Packit 7838c8
            _TIFFmemcpy(wrk_line, top_line, 4*width);
Packit 7838c8
            _TIFFmemcpy(top_line, bottom_line, 4*width);
Packit 7838c8
            _TIFFmemcpy(bottom_line, wrk_line, 4*width);
Packit 7838c8
        }
Packit 7838c8
Packit 7838c8
        /*
Packit 7838c8
         * Write out the result in a strip
Packit 7838c8
         */
Packit 7838c8
Packit 7838c8
        if( TIFFWriteEncodedStrip( out, row / rowsperstrip, raster,
Packit 7838c8
                                   4 * rows_to_write * width ) == -1 )
Packit 7838c8
        {
Packit 7838c8
            ok = 0;
Packit 7838c8
            break;
Packit 7838c8
        }
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    _TIFFfree( raster );
Packit 7838c8
    _TIFFfree( wrk_line );
Packit 7838c8
Packit 7838c8
    return ok;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * cvt_whole_image()
Packit 7838c8
 *
Packit 7838c8
 * read the whole image into one big RGBA buffer and then write out
Packit 7838c8
 * strips from that.  This is using the traditional TIFFReadRGBAImage()
Packit 7838c8
 * API that we trust.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
cvt_whole_image( TIFF *in, TIFF *out )
Packit 7838c8
Packit 7838c8
{
Packit 7838c8
    uint32* raster;			/* retrieve RGBA image */
Packit 7838c8
    uint32  width, height;		/* image width & height */
Packit 7838c8
    uint32  row;
Packit 7838c8
    size_t pixel_count;
Packit 7838c8
        
Packit 7838c8
    TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
Packit 7838c8
    TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
Packit 7838c8
    pixel_count = width * height;
Packit 7838c8
Packit 7838c8
    /* XXX: Check the integer overflow. */
Packit 7838c8
    if (!width || !height || pixel_count / width != height) {
Packit 7838c8
        TIFFError(TIFFFileName(in),
Packit 7838c8
		  "Malformed input file; can't allocate buffer for raster of %lux%lu size",
Packit 7838c8
		  (unsigned long)width, (unsigned long)height);
Packit 7838c8
        return 0;
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
Packit 7838c8
    TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
Packit 7838c8
Packit 7838c8
    raster = (uint32*)_TIFFCheckMalloc(in, pixel_count, sizeof(uint32), "raster buffer");
Packit 7838c8
    if (raster == 0) {
Packit 7838c8
        TIFFError(TIFFFileName(in), "Failed to allocate buffer (%lu elements of %lu each)",
Packit 7838c8
		  (unsigned long)pixel_count, (unsigned long)sizeof(uint32));
Packit 7838c8
        return (0);
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    /* Read the image in one chunk into an RGBA array */
Packit 7838c8
    if (!TIFFReadRGBAImageOriented(in, width, height, raster,
Packit 7838c8
                                   ORIENTATION_TOPLEFT, 0)) {
Packit 7838c8
        _TIFFfree(raster);
Packit 7838c8
        return (0);
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    /*
Packit 7838c8
     * XXX: raster array has 4-byte unsigned integer type, that is why
Packit 7838c8
     * we should rearrange it here.
Packit 7838c8
     */
Packit 7838c8
#if HOST_BIGENDIAN
Packit 7838c8
    TIFFSwabArrayOfLong(raster, width * height);
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
    /*
Packit 7838c8
     * Do we want to strip away alpha components?
Packit 7838c8
     */
Packit 7838c8
    if (no_alpha)
Packit 7838c8
    {
Packit 7838c8
        size_t count = pixel_count;
Packit 7838c8
        unsigned char *src, *dst;
Packit 7838c8
Packit 7838c8
	src = dst = (unsigned char *) raster;
Packit 7838c8
        while (count > 0)
Packit 7838c8
        {
Packit 7838c8
	    *(dst++) = *(src++);
Packit 7838c8
	    *(dst++) = *(src++);
Packit 7838c8
	    *(dst++) = *(src++);
Packit 7838c8
	    src++;
Packit 7838c8
	    count--;
Packit 7838c8
        }
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    /*
Packit 7838c8
     * Write out the result in strips
Packit 7838c8
     */
Packit 7838c8
    for (row = 0; row < height; row += rowsperstrip)
Packit 7838c8
    {
Packit 7838c8
        unsigned char * raster_strip;
Packit 7838c8
        int	rows_to_write;
Packit 7838c8
        int	bytes_per_pixel;
Packit 7838c8
Packit 7838c8
        if (no_alpha)
Packit 7838c8
        {
Packit 7838c8
            raster_strip = ((unsigned char *) raster) + 3 * row * width;
Packit 7838c8
            bytes_per_pixel = 3;
Packit 7838c8
        }
Packit 7838c8
        else
Packit 7838c8
        {
Packit 7838c8
            raster_strip = (unsigned char *) (raster + row * width);
Packit 7838c8
            bytes_per_pixel = 4;
Packit 7838c8
        }
Packit 7838c8
Packit 7838c8
        if( row + rowsperstrip > height )
Packit 7838c8
            rows_to_write = height - row;
Packit 7838c8
        else
Packit 7838c8
            rows_to_write = rowsperstrip;
Packit 7838c8
Packit 7838c8
        if( TIFFWriteEncodedStrip( out, row / rowsperstrip, raster_strip,
Packit 7838c8
                             bytes_per_pixel * rows_to_write * width ) == -1 )
Packit 7838c8
        {
Packit 7838c8
            _TIFFfree( raster );
Packit 7838c8
            return 0;
Packit 7838c8
        }
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    _TIFFfree( raster );
Packit 7838c8
Packit 7838c8
    return 1;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
tiffcvt(TIFF* in, TIFF* out)
Packit 7838c8
{
Packit 7838c8
	uint32 width, height;		/* image width & height */
Packit 7838c8
	uint16 shortv;
Packit 7838c8
	float floatv;
Packit 7838c8
	char *stringv;
Packit 7838c8
	uint32 longv;
Packit 7838c8
        uint16 v[1];
Packit 7838c8
Packit 7838c8
	TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
Packit 7838c8
	TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
Packit 7838c8
Packit 7838c8
	CopyField(TIFFTAG_SUBFILETYPE, longv);
Packit 7838c8
	TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
Packit 7838c8
	TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
Packit 7838c8
	TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
Packit 7838c8
	TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
Packit 7838c8
	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
Packit 7838c8
Packit 7838c8
	CopyField(TIFFTAG_FILLORDER, shortv);
Packit 7838c8
	TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
Packit 7838c8
Packit 7838c8
        if( no_alpha )
Packit 7838c8
            TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
Packit 7838c8
        else
Packit 7838c8
            TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 4);
Packit 7838c8
Packit 7838c8
        if( !no_alpha )
Packit 7838c8
        {
Packit 7838c8
            v[0] = EXTRASAMPLE_ASSOCALPHA;
Packit 7838c8
            TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, v);
Packit 7838c8
        }
Packit 7838c8
Packit 7838c8
	CopyField(TIFFTAG_XRESOLUTION, floatv);
Packit 7838c8
	CopyField(TIFFTAG_YRESOLUTION, floatv);
Packit 7838c8
	CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
Packit 7838c8
	TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
Packit 7838c8
	TIFFSetField(out, TIFFTAG_SOFTWARE, TIFFGetVersion());
Packit 7838c8
	CopyField(TIFFTAG_DOCUMENTNAME, stringv);
Packit 7838c8
Packit 7838c8
        if( process_by_block && TIFFIsTiled( in ) )
Packit 7838c8
            return( cvt_by_tile( in, out ) );
Packit 7838c8
        else if( process_by_block )
Packit 7838c8
            return( cvt_by_strip( in, out ) );
Packit 7838c8
        else
Packit 7838c8
            return( cvt_whole_image( in, out ) );
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static char* stuff[] = {
Packit 7838c8
    "usage: tiff2rgba [-c comp] [-r rows] [-b] [-n] [-8] input... output",
Packit 7838c8
    "where comp is one of the following compression algorithms:",
Packit 7838c8
    " jpeg\t\tJPEG encoding",
Packit 7838c8
    " zip\t\tZip/Deflate encoding",
Packit 7838c8
    " lzw\t\tLempel-Ziv & Welch encoding",
Packit 7838c8
    " packbits\tPackBits encoding",
Packit 7838c8
    " none\t\tno compression",
Packit 7838c8
    "and the other options are:",
Packit 7838c8
    " -r\trows/strip",
Packit 7838c8
    " -b (progress by block rather than as a whole image)",
Packit 7838c8
    " -n don't emit alpha component.",
Packit 7838c8
    " -8 write BigTIFF file instead of ClassicTIFF",
Packit 7838c8
    NULL
Packit 7838c8
};
Packit 7838c8
Packit 7838c8
static void
Packit 7838c8
usage(int code)
Packit 7838c8
{
Packit 7838c8
	char buf[BUFSIZ];
Packit 7838c8
	int i;
Packit 7838c8
Packit 7838c8
	setbuf(stderr, buf);
Packit 7838c8
        fprintf(stderr, "%s\n\n", TIFFGetVersion());
Packit 7838c8
	for (i = 0; stuff[i] != NULL; i++)
Packit 7838c8
		fprintf(stderr, "%s\n", stuff[i]);
Packit 7838c8
	exit(code);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/* vim: set ts=8 sts=8 sw=8 noet: */
Packit 7838c8
/*
Packit 7838c8
 * Local Variables:
Packit 7838c8
 * mode: c
Packit 7838c8
 * c-basic-offset: 8
Packit 7838c8
 * fill-column: 78
Packit 7838c8
 * End:
Packit 7838c8
 */