kusano 7d535a
/* $Id: ras2tiff.c,v 1.18 2010-03-10 18:56:49 bfriesen Exp $ */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Copyright (c) 1988-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 "tif_config.h"
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 <ctype.h></ctype.h>
kusano 7d535a
kusano 7d535a
#ifdef HAVE_UNISTD_H
kusano 7d535a
# include <unistd.h></unistd.h>
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#ifdef NEED_LIBPORT
kusano 7d535a
# include "libport.h"
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#include "rasterfile.h"
kusano 7d535a
#include "tiffio.h"
kusano 7d535a
kusano 7d535a
#ifndef howmany
kusano 7d535a
#define	howmany(x, y)	(((x)+((y)-1))/(y))
kusano 7d535a
#endif
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	uint16 compression = (uint16) -1;
kusano 7d535a
static	int jpegcolormode = JPEGCOLORMODE_RGB;
kusano 7d535a
static	int quality = 75;		/* JPEG quality */
kusano 7d535a
static	uint16 predictor = 0;
kusano 7d535a
kusano 7d535a
static void usage(void);
kusano 7d535a
static	int processCompressOptions(char*);
kusano 7d535a
kusano 7d535a
int
kusano 7d535a
main(int argc, char* argv[])
kusano 7d535a
{
kusano 7d535a
	unsigned char* buf;
kusano 7d535a
	long row;
kusano 7d535a
	tsize_t linebytes, scanline;
kusano 7d535a
	TIFF *out;
kusano 7d535a
	FILE *in;
kusano 7d535a
	struct rasterfile h;
kusano 7d535a
	uint16 photometric;
kusano 7d535a
	uint16 config = PLANARCONFIG_CONTIG;
kusano 7d535a
	uint32 rowsperstrip = (uint32) -1;
kusano 7d535a
	int c;
kusano 7d535a
	extern int optind;
kusano 7d535a
	extern char* optarg;
kusano 7d535a
kusano 7d535a
	while ((c = getopt(argc, argv, "c:r:h")) != -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 'r':		/* rows/strip */
kusano 7d535a
			rowsperstrip = atoi(optarg);
kusano 7d535a
			break;
kusano 7d535a
		case 'h':
kusano 7d535a
			usage();
kusano 7d535a
			/*NOTREACHED*/
kusano 7d535a
		}
kusano 7d535a
	if (argc - optind != 2)
kusano 7d535a
		usage();
kusano 7d535a
	in = fopen(argv[optind], "rb");
kusano 7d535a
	if (in == NULL) {
kusano 7d535a
		fprintf(stderr, "%s: Can not open.\n", argv[optind]);
kusano 7d535a
		return (-1);
kusano 7d535a
	}
kusano 7d535a
	if (fread(&h, sizeof (h), 1, in) != 1) {
kusano 7d535a
		fprintf(stderr, "%s: Can not read header.\n", argv[optind]);
kusano 7d535a
		fclose(in);
kusano 7d535a
		return (-2);
kusano 7d535a
	}
kusano 7d535a
	if (strcmp(h.ras_magic, RAS_MAGIC) == 0) {
kusano 7d535a
#ifndef WORDS_BIGENDIAN
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_width);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_height);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_depth);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_length);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_type);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_maptype);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_maplength);
kusano 7d535a
#endif
kusano 7d535a
	} else if (strcmp(h.ras_magic, RAS_MAGIC_INV) == 0) {
kusano 7d535a
#ifdef WORDS_BIGENDIAN
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_width);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_height);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_depth);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_length);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_type);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_maptype);
kusano 7d535a
			TIFFSwabLong((uint32 *)&h.ras_maplength);
kusano 7d535a
#endif
kusano 7d535a
	} else {
kusano 7d535a
		fprintf(stderr, "%s: Not a rasterfile.\n", argv[optind]);
kusano 7d535a
		fclose(in);
kusano 7d535a
		return (-3);
kusano 7d535a
	}
kusano 7d535a
	out = TIFFOpen(argv[optind+1], "w");
kusano 7d535a
	if (out == NULL)
kusano 7d535a
	{
kusano 7d535a
		fclose(in);
kusano 7d535a
		return (-4);
kusano 7d535a
	}
kusano 7d535a
	TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) h.ras_width);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h.ras_height);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, h.ras_depth > 8 ? 3 : 1);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, h.ras_depth > 1 ? 8 : 1);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
kusano 7d535a
	if (h.ras_maptype != RMT_NONE) {
kusano 7d535a
		uint16* red;
kusano 7d535a
		register uint16* map;
kusano 7d535a
		register int i, j;
kusano 7d535a
		int mapsize;
kusano 7d535a
kusano 7d535a
		buf = (unsigned char *)_TIFFmalloc(h.ras_maplength);
kusano 7d535a
		if (buf == NULL) {
kusano 7d535a
			fprintf(stderr, "No space to read in colormap.\n");
kusano 7d535a
			return (-5);
kusano 7d535a
		}
kusano 7d535a
		if (fread(buf, h.ras_maplength, 1, in) != 1) {
kusano 7d535a
			fprintf(stderr, "%s: Read error on colormap.\n",
kusano 7d535a
			    argv[optind]);
kusano 7d535a
			return (-6);
kusano 7d535a
		}
kusano 7d535a
		mapsize = 1<
kusano 7d535a
		if (h.ras_maplength > mapsize*3) {
kusano 7d535a
			fprintf(stderr,
kusano 7d535a
			    "%s: Huh, %ld colormap entries, should be %d?\n",
kusano 7d535a
			    argv[optind], h.ras_maplength, mapsize*3);
kusano 7d535a
			return (-7);
kusano 7d535a
		}
kusano 7d535a
		red = (uint16*)_TIFFmalloc(mapsize * 3 * sizeof (uint16));
kusano 7d535a
		if (red == NULL) {
kusano 7d535a
			fprintf(stderr, "No space for colormap.\n");
kusano 7d535a
			return (-8);
kusano 7d535a
		}
kusano 7d535a
		map = red;
kusano 7d535a
		for (j = 0; j < 3; j++) {
kusano 7d535a
#define	SCALE(x)	(((x)*((1L<<16)-1))/255)
kusano 7d535a
			for (i = h.ras_maplength/3; i-- > 0;)
kusano 7d535a
				*map++ = SCALE(*buf++);
kusano 7d535a
			if ((i = h.ras_maplength/3) < mapsize) {
kusano 7d535a
				i = mapsize - i;
kusano 7d535a
				_TIFFmemset(map, 0, i*sizeof (uint16));
kusano 7d535a
				map += i;
kusano 7d535a
			}
kusano 7d535a
		}
kusano 7d535a
		TIFFSetField(out, TIFFTAG_COLORMAP,
kusano 7d535a
		     red, red + mapsize, red + 2*mapsize);
kusano 7d535a
		photometric = PHOTOMETRIC_PALETTE;
kusano 7d535a
		if (compression == (uint16) -1)
kusano 7d535a
			compression = COMPRESSION_PACKBITS;
kusano 7d535a
		TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
kusano 7d535a
	} else {
kusano 7d535a
		/* XXX this is bogus... */
kusano 7d535a
		photometric = h.ras_depth == 24 ?
kusano 7d535a
		    PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK;
kusano 7d535a
		if (compression == (uint16) -1)
kusano 7d535a
			compression = COMPRESSION_LZW;
kusano 7d535a
		TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
kusano 7d535a
	}
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
	linebytes = ((h.ras_depth*h.ras_width+15) >> 3) &~ 1;
kusano 7d535a
	scanline = TIFFScanlineSize(out);
kusano 7d535a
	if (scanline > linebytes) {
kusano 7d535a
		buf = (unsigned char *)_TIFFmalloc(scanline);
kusano 7d535a
		_TIFFmemset(buf+linebytes, 0, scanline-linebytes);
kusano 7d535a
	} else
kusano 7d535a
		buf = (unsigned char *)_TIFFmalloc(linebytes);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
kusano 7d535a
	    TIFFDefaultStripSize(out, rowsperstrip));
kusano 7d535a
	for (row = 0; row < h.ras_height; row++) {
kusano 7d535a
		if (fread(buf, linebytes, 1, in) != 1) {
kusano 7d535a
			fprintf(stderr, "%s: scanline %ld: Read error.\n",
kusano 7d535a
			    argv[optind], row);
kusano 7d535a
			break;
kusano 7d535a
		}
kusano 7d535a
		if (h.ras_type == RT_STANDARD && h.ras_depth == 24) {
kusano 7d535a
			tsize_t cc = h.ras_width;
kusano 7d535a
			unsigned char* cp = buf;
kusano 7d535a
#define	SWAP(a,b)	{ unsigned char t = (a); (a) = (b); (b) = t; }
kusano 7d535a
			do {
kusano 7d535a
				SWAP(cp[0], cp[2]);
kusano 7d535a
				cp += 3;
kusano 7d535a
			} while (--cc);
kusano 7d535a
		}
kusano 7d535a
		if (TIFFWriteScanline(out, buf, row, 0) < 0)
kusano 7d535a
			break;
kusano 7d535a
	}
kusano 7d535a
	(void) TIFFClose(out);
kusano 7d535a
	fclose(in);
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
                compression = 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
char* stuff[] = {
kusano 7d535a
"usage: ras2tiff [options] input.ras output.tif",
kusano 7d535a
"where options are:",
kusano 7d535a
" -r #		make each strip have no more than # rows",
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
"For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. quality",
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
" -h		this help message",
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
        fprintf(stderr, "%s\n\n", TIFFGetVersion());
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
/* vim: set ts=8 sts=8 sw=8 noet: */
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
 */