roentgen b75cab
/* $Id: sgi2tiff.c,v 1.6 2010-03-10 18:56:49 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1991-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and 
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 * 
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
roentgen b75cab
 * 
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
#include <stdlib.h></stdlib.h>
roentgen b75cab
#include <string.h></string.h>
roentgen b75cab
#include <gl image.h=""></gl>
roentgen b75cab
#include <ctype.h></ctype.h>
roentgen b75cab
roentgen b75cab
#include "tiffio.h"
roentgen b75cab
roentgen b75cab
#define	streq(a,b)	(strcmp(a,b) == 0)
roentgen b75cab
#define	strneq(a,b,n)	(strncmp(a,b,n) == 0)
roentgen b75cab
roentgen b75cab
static	short config = PLANARCONFIG_CONTIG;
roentgen b75cab
static	uint16 compression = COMPRESSION_PACKBITS;
roentgen b75cab
static	uint16 predictor = 0;
roentgen b75cab
static	uint16 fillorder = 0;
roentgen b75cab
static	uint32 rowsperstrip = (uint32) -1;
roentgen b75cab
static	int jpegcolormode = JPEGCOLORMODE_RGB;
roentgen b75cab
static	int quality = 75;		/* JPEG quality */
roentgen b75cab
static	uint16 photometric;
roentgen b75cab
roentgen b75cab
static	void usage(void);
roentgen b75cab
static	int cpContig(IMAGE*, TIFF*);
roentgen b75cab
static	int cpSeparate(IMAGE*, TIFF*);
roentgen b75cab
static	int processCompressOptions(char*);
roentgen b75cab
roentgen b75cab
/* XXX image library has no prototypes */
roentgen b75cab
extern	IMAGE* iopen(const char*, const char*);
roentgen b75cab
extern	void iclose(IMAGE*);
roentgen b75cab
extern	void getrow(IMAGE*, short*, int, int);
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
main(int argc, char* argv[])
roentgen b75cab
{
roentgen b75cab
	IMAGE *in;
roentgen b75cab
	TIFF *out;
roentgen b75cab
	int c;
roentgen b75cab
	extern int optind;
roentgen b75cab
	extern char* optarg;
roentgen b75cab
roentgen b75cab
	while ((c = getopt(argc, argv, "c:p:r:")) != -1)
roentgen b75cab
		switch (c) {
roentgen b75cab
		case 'c':		/* compression scheme */
roentgen b75cab
			if (!processCompressOptions(optarg))
roentgen b75cab
				usage();
roentgen b75cab
			break;
roentgen b75cab
		case 'f':		/* fill order */
roentgen b75cab
			if (streq(optarg, "lsb2msb"))
roentgen b75cab
				fillorder = FILLORDER_LSB2MSB;
roentgen b75cab
			else if (streq(optarg, "msb2lsb"))
roentgen b75cab
				fillorder = FILLORDER_MSB2LSB;
roentgen b75cab
			else
roentgen b75cab
				usage();
roentgen b75cab
			break;
roentgen b75cab
		case 'p':		/* planar configuration */
roentgen b75cab
			if (streq(optarg, "separate"))
roentgen b75cab
				config = PLANARCONFIG_SEPARATE;
roentgen b75cab
			else if (streq(optarg, "contig"))
roentgen b75cab
				config = PLANARCONFIG_CONTIG;
roentgen b75cab
			else
roentgen b75cab
				usage();
roentgen b75cab
			break;
roentgen b75cab
		case 'r':		/* rows/strip */
roentgen b75cab
			rowsperstrip = atoi(optarg);
roentgen b75cab
			break;
roentgen b75cab
		case '?':
roentgen b75cab
			usage();
roentgen b75cab
			/*NOTREACHED*/
roentgen b75cab
		}
roentgen b75cab
	if (argc - optind != 2)
roentgen b75cab
		usage();
roentgen b75cab
	in = iopen(argv[optind], "r");
roentgen b75cab
	if (in == NULL)
roentgen b75cab
		return (-1);
roentgen b75cab
	out = TIFFOpen(argv[optind+1], "w");
roentgen b75cab
	if (out == NULL)
roentgen b75cab
		return (-2);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) in->xsize);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) in->ysize);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
roentgen b75cab
	if (in->zsize == 1)
roentgen b75cab
		photometric = PHOTOMETRIC_MINISBLACK;
roentgen b75cab
	else
roentgen b75cab
		photometric = PHOTOMETRIC_RGB;
roentgen b75cab
	switch (compression) {
roentgen b75cab
	case COMPRESSION_JPEG:
roentgen b75cab
		if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB)
roentgen b75cab
			photometric = PHOTOMETRIC_YCBCR;
roentgen b75cab
		TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
roentgen b75cab
		TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
roentgen b75cab
		break;
roentgen b75cab
	case COMPRESSION_LZW:
roentgen b75cab
	case COMPRESSION_DEFLATE:
roentgen b75cab
		if (predictor != 0)
roentgen b75cab
			TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
roentgen b75cab
	if (fillorder != 0)
roentgen b75cab
		TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, in->zsize);
roentgen b75cab
	if (in->zsize > 3) {
roentgen b75cab
	    uint16 v[1];
roentgen b75cab
	    v[0] = EXTRASAMPLE_UNASSALPHA;
roentgen b75cab
	    TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, v);
roentgen b75cab
	}
roentgen b75cab
	TIFFSetField(out, TIFFTAG_MINSAMPLEVALUE, (uint16) in->min);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_MAXSAMPLEVALUE, (uint16) in->max);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
roentgen b75cab
	if (config != PLANARCONFIG_SEPARATE)
roentgen b75cab
		TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
roentgen b75cab
		    TIFFDefaultStripSize(out, rowsperstrip));
roentgen b75cab
	else			/* force 1 row/strip for library limitation */
roentgen b75cab
		TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 1L);
roentgen b75cab
	if (in->name[0] != '\0')
roentgen b75cab
		TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, in->name);
roentgen b75cab
	if (config == PLANARCONFIG_CONTIG)
roentgen b75cab
		cpContig(in, out);
roentgen b75cab
	else
roentgen b75cab
		cpSeparate(in, out);
roentgen b75cab
	(void) iclose(in);
roentgen b75cab
	(void) TIFFClose(out);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
processCompressOptions(char* opt)
roentgen b75cab
{
roentgen b75cab
	if (streq(opt, "none"))
roentgen b75cab
		compression = COMPRESSION_NONE;
roentgen b75cab
	else if (streq(opt, "packbits"))
roentgen b75cab
		compression = COMPRESSION_PACKBITS;
roentgen b75cab
	else if (strneq(opt, "jpeg", 4)) {
roentgen b75cab
		char* cp = strchr(opt, ':');
roentgen b75cab
roentgen b75cab
                defcompression = COMPRESSION_JPEG;
roentgen b75cab
                while( cp )
roentgen b75cab
                {
roentgen b75cab
                    if (isdigit((int)cp[1]))
roentgen b75cab
			quality = atoi(cp+1);
roentgen b75cab
                    else if (cp[1] == 'r' )
roentgen b75cab
			jpegcolormode = JPEGCOLORMODE_RAW;
roentgen b75cab
                    else
roentgen b75cab
                        usage();
roentgen b75cab
roentgen b75cab
                    cp = strchr(cp+1,':');
roentgen b75cab
                }
roentgen b75cab
	} else if (strneq(opt, "lzw", 3)) {
roentgen b75cab
		char* cp = strchr(opt, ':');
roentgen b75cab
		if (cp)
roentgen b75cab
			predictor = atoi(cp+1);
roentgen b75cab
		compression = COMPRESSION_LZW;
roentgen b75cab
	} else if (strneq(opt, "zip", 3)) {
roentgen b75cab
		char* cp = strchr(opt, ':');
roentgen b75cab
		if (cp)
roentgen b75cab
			predictor = atoi(cp+1);
roentgen b75cab
		compression = COMPRESSION_DEFLATE;
roentgen b75cab
	} else
roentgen b75cab
		return (0);
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
cpContig(IMAGE* in, TIFF* out)
roentgen b75cab
{
roentgen b75cab
	tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out));
roentgen b75cab
	short *r = NULL;
roentgen b75cab
	int x, y;
roentgen b75cab
roentgen b75cab
	if (in->zsize == 3) {
roentgen b75cab
		short *g, *b;
roentgen b75cab
roentgen b75cab
		r = (short *)_TIFFmalloc(3 * in->xsize * sizeof (short));
roentgen b75cab
		g = r + in->xsize;
roentgen b75cab
		b = g + in->xsize;
roentgen b75cab
		for (y = in->ysize-1; y >= 0; y--) {
roentgen b75cab
			uint8* pp = (uint8*) buf;
roentgen b75cab
roentgen b75cab
			getrow(in, r, y, 0);
roentgen b75cab
			getrow(in, g, y, 1);
roentgen b75cab
			getrow(in, b, y, 2);
roentgen b75cab
			for (x = 0; x < in->xsize; x++) {
roentgen b75cab
				pp[0] = r[x];
roentgen b75cab
				pp[1] = g[x];
roentgen b75cab
				pp[2] = b[x];
roentgen b75cab
				pp += 3;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
roentgen b75cab
				goto bad;
roentgen b75cab
		}
roentgen b75cab
	} else if (in->zsize == 4) {
roentgen b75cab
		short *g, *b, *a;
roentgen b75cab
roentgen b75cab
		r = (short *)_TIFFmalloc(4 * in->xsize * sizeof (short));
roentgen b75cab
		g = r + in->xsize;
roentgen b75cab
		b = g + in->xsize;
roentgen b75cab
		a = b + in->xsize;
roentgen b75cab
		for (y = in->ysize-1; y >= 0; y--) {
roentgen b75cab
			uint8* pp = (uint8*) buf;
roentgen b75cab
roentgen b75cab
			getrow(in, r, y, 0);
roentgen b75cab
			getrow(in, g, y, 1);
roentgen b75cab
			getrow(in, b, y, 2);
roentgen b75cab
			getrow(in, a, y, 3);
roentgen b75cab
			for (x = 0; x < in->xsize; x++) {
roentgen b75cab
				pp[0] = r[x];
roentgen b75cab
				pp[1] = g[x];
roentgen b75cab
				pp[2] = b[x];
roentgen b75cab
				pp[3] = a[x];
roentgen b75cab
				pp += 4;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
roentgen b75cab
				goto bad;
roentgen b75cab
		}
roentgen b75cab
	} else {
roentgen b75cab
		uint8* pp = (uint8*) buf;
roentgen b75cab
roentgen b75cab
		r = (short *)_TIFFmalloc(in->xsize * sizeof (short));
roentgen b75cab
		for (y = in->ysize-1; y >= 0; y--) {
roentgen b75cab
			getrow(in, r, y, 0);
roentgen b75cab
			for (x = in->xsize-1; x >= 0; x--)
roentgen b75cab
				pp[x] = r[x];
roentgen b75cab
			if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
roentgen b75cab
				goto bad;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	if (r)
roentgen b75cab
		_TIFFfree(r);
roentgen b75cab
	_TIFFfree(buf);
roentgen b75cab
	return (1);
roentgen b75cab
bad:
roentgen b75cab
	if (r)
roentgen b75cab
		_TIFFfree(r);
roentgen b75cab
	_TIFFfree(buf);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
cpSeparate(IMAGE* in, TIFF* out)
roentgen b75cab
{
roentgen b75cab
	tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out));
roentgen b75cab
	short *r = (short *)_TIFFmalloc(in->xsize * sizeof (short));
roentgen b75cab
	uint8* pp = (uint8*) buf;
roentgen b75cab
	int x, y, z;
roentgen b75cab
roentgen b75cab
	for (z = 0; z < in->zsize; z++) {
roentgen b75cab
		for (y = in->ysize-1; y >= 0; y--) {
roentgen b75cab
			getrow(in, r, y, z);
roentgen b75cab
			for (x = 0; x < in->xsize; x++)
roentgen b75cab
				pp[x] = r[x];
roentgen b75cab
			if (TIFFWriteScanline(out, buf, in->ysize-y-1, z) < 0)
roentgen b75cab
				goto bad;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	_TIFFfree(r);
roentgen b75cab
	_TIFFfree(buf);
roentgen b75cab
	return (1);
roentgen b75cab
bad:
roentgen b75cab
	_TIFFfree(r);
roentgen b75cab
	_TIFFfree(buf);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
char* stuff[] = {
roentgen b75cab
"usage: sgi2tiff [options] input.rgb output.tif",
roentgen b75cab
"where options are:",
roentgen b75cab
" -r #		make each strip have no more than # rows",
roentgen b75cab
"",
roentgen b75cab
" -p contig	pack samples contiguously (e.g. RGBRGB...)",
roentgen b75cab
" -p separate	store samples separately (e.g. RRR...GGG...BBB...)",
roentgen b75cab
"",
roentgen b75cab
" -f lsb2msb	force lsb-to-msb FillOrder for output",
roentgen b75cab
" -f msb2lsb	force msb-to-lsb FillOrder for output",
roentgen b75cab
"",
roentgen b75cab
" -c lzw[:opts]	compress output with Lempel-Ziv & Welch encoding",
roentgen b75cab
" -c zip[:opts]	compress output with deflate encoding",
roentgen b75cab
" -c jpeg[:opts]compress output with JPEG encoding",
roentgen b75cab
" -c packbits	compress output with packbits encoding",
roentgen b75cab
" -c none	use no compression algorithm on output",
roentgen b75cab
"",
roentgen b75cab
"JPEG options:",
roentgen b75cab
" #		set compression quality level (0-100, default 75)",
roentgen b75cab
" r		output color image as RGB rather than YCbCr",
roentgen b75cab
"",
roentgen b75cab
"LZW and deflate options:",
roentgen b75cab
" #		set predictor value",
roentgen b75cab
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
roentgen b75cab
NULL
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
usage(void)
roentgen b75cab
{
roentgen b75cab
	char buf[BUFSIZ];
roentgen b75cab
	int i;
roentgen b75cab
roentgen b75cab
	setbuf(stderr, buf);
roentgen b75cab
	for (i = 0; stuff[i] != NULL; i++)
roentgen b75cab
		fprintf(stderr, "%s\n", stuff[i]);
roentgen b75cab
	exit(-1);
roentgen b75cab
}
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */