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