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