roentgen b75cab
/* $Id: ppm2tiff.c,v 1.16 2010-04-10 19:22:34 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 "tif_config.h"
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 <ctype.h></ctype.h>
roentgen b75cab
roentgen b75cab
#ifdef HAVE_UNISTD_H
roentgen b75cab
# include <unistd.h></unistd.h>
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifdef HAVE_FCNTL_H
roentgen b75cab
# include <fcntl.h></fcntl.h>
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifdef HAVE_IO_H
roentgen b75cab
# include <io.h></io.h>
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifdef NEED_LIBPORT
roentgen b75cab
# include "libport.h"
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#include "tiffio.h"
roentgen b75cab
roentgen b75cab
#ifndef HAVE_GETOPT
roentgen b75cab
extern int getopt(int, char**, char*);
roentgen b75cab
#endif
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	uint16 compression = COMPRESSION_PACKBITS;
roentgen b75cab
static	uint16 predictor = 0;
roentgen b75cab
static	int quality = 75;	/* JPEG quality */
roentgen b75cab
static	int jpegcolormode = JPEGCOLORMODE_RGB;
roentgen b75cab
static  uint32 g3opts;
roentgen b75cab
roentgen b75cab
static	void usage(void);
roentgen b75cab
static	int processCompressOptions(char*);
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
BadPPM(char* file)
roentgen b75cab
{
roentgen b75cab
	fprintf(stderr, "%s: Not a PPM file.\n", file);
roentgen b75cab
	exit(-2);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
main(int argc, char* argv[])
roentgen b75cab
{
roentgen b75cab
	uint16 photometric = 0;
roentgen b75cab
	uint32 rowsperstrip = (uint32) -1;
roentgen b75cab
	double resolution = -1;
roentgen b75cab
	unsigned char *buf = NULL;
roentgen b75cab
	tsize_t linebytes = 0;
roentgen b75cab
	uint16 spp = 1;
roentgen b75cab
	uint16 bpp = 8;
roentgen b75cab
	TIFF *out;
roentgen b75cab
	FILE *in;
roentgen b75cab
	unsigned int w, h, prec, row;
roentgen b75cab
	char *infile;
roentgen b75cab
	int c;
roentgen b75cab
	extern int optind;
roentgen b75cab
	extern char* optarg;
roentgen b75cab
roentgen b75cab
	if (argc < 2) {
roentgen b75cab
	    fprintf(stderr, "%s: Too few arguments\n", argv[0]);
roentgen b75cab
	    usage();
roentgen b75cab
	}
roentgen b75cab
	while ((c = getopt(argc, argv, "c:r: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 'r':		/* rows/strip */
roentgen b75cab
			rowsperstrip = atoi(optarg);
roentgen b75cab
			break;
roentgen b75cab
		case 'R':		/* resolution */
roentgen b75cab
			resolution = atof(optarg);
roentgen b75cab
			break;
roentgen b75cab
		case '?':
roentgen b75cab
			usage();
roentgen b75cab
			/*NOTREACHED*/
roentgen b75cab
		}
roentgen b75cab
roentgen b75cab
	if (optind + 2 < argc) {
roentgen b75cab
	    fprintf(stderr, "%s: Too many arguments\n", argv[0]);
roentgen b75cab
	    usage();
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * If only one file is specified, read input from
roentgen b75cab
	 * stdin; otherwise usage is: ppm2tiff input output.
roentgen b75cab
	 */
roentgen b75cab
	if (argc - optind > 1) {
roentgen b75cab
		infile = argv[optind++];
roentgen b75cab
		in = fopen(infile, "rb");
roentgen b75cab
		if (in == NULL) {
roentgen b75cab
			fprintf(stderr, "%s: Can not open.\n", infile);
roentgen b75cab
			return (-1);
roentgen b75cab
		}
roentgen b75cab
	} else {
roentgen b75cab
		infile = "<stdin>";</stdin>
roentgen b75cab
		in = stdin;
roentgen b75cab
#if defined(HAVE_SETMODE) && defined(O_BINARY)
roentgen b75cab
		setmode(fileno(stdin), O_BINARY);
roentgen b75cab
#endif
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (fgetc(in) != 'P')
roentgen b75cab
		BadPPM(infile);
roentgen b75cab
	switch (fgetc(in)) {
roentgen b75cab
		case '4':			/* it's a PBM file */
roentgen b75cab
			bpp = 1;
roentgen b75cab
			spp = 1;
roentgen b75cab
			photometric = PHOTOMETRIC_MINISWHITE;
roentgen b75cab
			break;
roentgen b75cab
		case '5':			/* it's a PGM file */
roentgen b75cab
			bpp = 8;
roentgen b75cab
			spp = 1;
roentgen b75cab
			photometric = PHOTOMETRIC_MINISBLACK;
roentgen b75cab
			break;
roentgen b75cab
		case '6':			/* it's a PPM file */
roentgen b75cab
			bpp = 8;
roentgen b75cab
			spp = 3;
roentgen b75cab
			photometric = PHOTOMETRIC_RGB;
roentgen b75cab
			if (compression == COMPRESSION_JPEG &&
roentgen b75cab
			    jpegcolormode == JPEGCOLORMODE_RGB)
roentgen b75cab
				photometric = PHOTOMETRIC_YCBCR;
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			BadPPM(infile);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/* Parse header */
roentgen b75cab
	while(1) {
roentgen b75cab
		if (feof(in))
roentgen b75cab
			BadPPM(infile);
roentgen b75cab
		c = fgetc(in);
roentgen b75cab
		/* Skip whitespaces (blanks, TABs, CRs, LFs) */
roentgen b75cab
		if (strchr(" \t\r\n", c))
roentgen b75cab
			continue;
roentgen b75cab
roentgen b75cab
		/* Check for comment line */
roentgen b75cab
		if (c == '#') {
roentgen b75cab
			do {
roentgen b75cab
			    c = fgetc(in);
roentgen b75cab
			} while(!(strchr("\r\n", c) || feof(in)));
roentgen b75cab
			continue;
roentgen b75cab
		}
roentgen b75cab
roentgen b75cab
		ungetc(c, in);
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
	switch (bpp) {
roentgen b75cab
	case 1:
roentgen b75cab
		if (fscanf(in, " %u %u", &w, &h) != 2)
roentgen b75cab
			BadPPM(infile);
roentgen b75cab
		if (fgetc(in) != '\n')
roentgen b75cab
			BadPPM(infile);
roentgen b75cab
		break;
roentgen b75cab
	case 8:
roentgen b75cab
		if (fscanf(in, " %u %u %u", &w, &h, &prec) != 3)
roentgen b75cab
			BadPPM(infile);
roentgen b75cab
		if (fgetc(in) != '\n' || prec != 255)
roentgen b75cab
			BadPPM(infile);
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
	out = TIFFOpen(argv[optind], "w");
roentgen b75cab
	if (out == NULL)
roentgen b75cab
		return (-4);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) w);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bpp);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
roentgen b75cab
	switch (compression) {
roentgen b75cab
	case COMPRESSION_JPEG:
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
        case COMPRESSION_CCITTFAX3:
roentgen b75cab
		TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts);
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
	switch (bpp) {
roentgen b75cab
		case 1:
roentgen b75cab
			linebytes = (spp * w + (8 - 1)) / 8;
roentgen b75cab
			if (rowsperstrip == (uint32) -1) {
roentgen b75cab
				TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, h);
roentgen b75cab
			} else {
roentgen b75cab
				TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
roentgen b75cab
				    TIFFDefaultStripSize(out, rowsperstrip));
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case 8:
roentgen b75cab
			linebytes = spp * w;
roentgen b75cab
			TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
roentgen b75cab
			    TIFFDefaultStripSize(out, rowsperstrip));
roentgen b75cab
			break;
roentgen b75cab
	}
roentgen b75cab
	if (TIFFScanlineSize(out) > linebytes)
roentgen b75cab
		buf = (unsigned char *)_TIFFmalloc(linebytes);
roentgen b75cab
	else
roentgen b75cab
		buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
roentgen b75cab
	if (resolution > 0) {
roentgen b75cab
		TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
roentgen b75cab
		TIFFSetField(out, TIFFTAG_YRESOLUTION, resolution);
roentgen b75cab
		TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
roentgen b75cab
	}
roentgen b75cab
	for (row = 0; row < h; row++) {
roentgen b75cab
		if (fread(buf, linebytes, 1, in) != 1) {
roentgen b75cab
			fprintf(stderr, "%s: scanline %lu: Read error.\n",
roentgen b75cab
			    infile, (unsigned long) row);
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
		if (TIFFWriteScanline(out, buf, row, 0) < 0)
roentgen b75cab
			break;
roentgen b75cab
	}
roentgen b75cab
	(void) TIFFClose(out);
roentgen b75cab
	if (buf)
roentgen b75cab
		_TIFFfree(buf);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
processG3Options(char* cp)
roentgen b75cab
{
roentgen b75cab
	g3opts = 0;
roentgen b75cab
        if( (cp = strchr(cp, ':')) ) {
roentgen b75cab
                do {
roentgen b75cab
                        cp++;
roentgen b75cab
                        if (strneq(cp, "1d", 2))
roentgen b75cab
                                g3opts &= ~GROUP3OPT_2DENCODING;
roentgen b75cab
                        else if (strneq(cp, "2d", 2))
roentgen b75cab
                                g3opts |= GROUP3OPT_2DENCODING;
roentgen b75cab
                        else if (strneq(cp, "fill", 4))
roentgen b75cab
                                g3opts |= GROUP3OPT_FILLBITS;
roentgen b75cab
                        else
roentgen b75cab
                                usage();
roentgen b75cab
                } while( (cp = strchr(cp, ':')) );
roentgen b75cab
        }
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
                compression = 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, "g3", 2)) {
roentgen b75cab
		processG3Options(opt);
roentgen b75cab
		compression = COMPRESSION_CCITTFAX3;
roentgen b75cab
	} else if (streq(opt, "g4")) {
roentgen b75cab
		compression = COMPRESSION_CCITTFAX4;
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
char* stuff[] = {
roentgen b75cab
"usage: ppm2tiff [options] input.ppm output.tif",
roentgen b75cab
"where options are:",
roentgen b75cab
" -r #		make each strip have no more than # rows",
roentgen b75cab
" -R #		set x&y resolution (dpi)",
roentgen b75cab
"",
roentgen b75cab
" -c jpeg[:opts]  compress output with JPEG encoding",
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 packbits	compress output with packbits encoding (the default)",
roentgen b75cab
" -c g3[:opts]  compress output with CCITT Group 3 encoding",
roentgen b75cab
" -c g4         compress output with CCITT Group 4 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
"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
        fprintf(stderr, "%s\n\n", TIFFGetVersion());
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
/* vim: set ts=8 sts=8 sw=8 noet: */
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
 */