kusano 7d535a
/* $Id: fax2tiff.c,v 1.22 2010-03-10 18:56:49 bfriesen Exp $ */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Copyright (c) 1990-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
/* 
kusano 7d535a
 * Convert a CCITT Group 3 or 4 FAX file to TIFF Group 3 or 4 format.
kusano 7d535a
 */
kusano 7d535a
#include "tif_config.h"
kusano 7d535a
kusano 7d535a
#include <stdio.h></stdio.h>
kusano 7d535a
#include <stdlib.h>		/* should have atof & getopt */</stdlib.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 "tiffiop.h"
kusano 7d535a
kusano 7d535a
#ifndef EXIT_SUCCESS
kusano 7d535a
# define EXIT_SUCCESS	0
kusano 7d535a
#endif
kusano 7d535a
#ifndef EXIT_FAILURE
kusano 7d535a
# define EXIT_FAILURE	1
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#define TIFFhowmany8(x) (((x)&0x07)?((uint32)(x)>>3)+1:(uint32)(x)>>3)
kusano 7d535a
kusano 7d535a
TIFF	*faxTIFF;
kusano 7d535a
char	*rowbuf;
kusano 7d535a
char	*refbuf;
kusano 7d535a
kusano 7d535a
uint32	xsize = 1728;
kusano 7d535a
int	verbose;
kusano 7d535a
int	stretch;
kusano 7d535a
uint16	badfaxrun;
kusano 7d535a
uint32	badfaxlines;
kusano 7d535a
kusano 7d535a
int	copyFaxFile(TIFF* tifin, TIFF* tifout);
kusano 7d535a
static	void usage(void);
kusano 7d535a
kusano 7d535a
int
kusano 7d535a
main(int argc, char* argv[])
kusano 7d535a
{
kusano 7d535a
	FILE *in;
kusano 7d535a
	TIFF *out = NULL;
kusano 7d535a
	TIFFErrorHandler whandler = NULL;
kusano 7d535a
	int compression_in = COMPRESSION_CCITTFAX3;
kusano 7d535a
	int compression_out = COMPRESSION_CCITTFAX3;
kusano 7d535a
	int fillorder_in = FILLORDER_LSB2MSB;
kusano 7d535a
	int fillorder_out = FILLORDER_LSB2MSB;
kusano 7d535a
	uint32 group3options_in = 0;	/* 1d-encoded */
kusano 7d535a
	uint32 group3options_out = 0;	/* 1d-encoded */
kusano 7d535a
	uint32 group4options_in = 0;	/* compressed */
kusano 7d535a
	uint32 group4options_out = 0;	/* compressed */
kusano 7d535a
	uint32 defrowsperstrip = (uint32) 0;
kusano 7d535a
	uint32 rowsperstrip;
kusano 7d535a
	int photometric_in = PHOTOMETRIC_MINISWHITE;
kusano 7d535a
	int photometric_out = PHOTOMETRIC_MINISWHITE;
kusano 7d535a
	int mode = FAXMODE_CLASSF;
kusano 7d535a
	int rows;
kusano 7d535a
	int c;
kusano 7d535a
	int pn, npages;
kusano 7d535a
	float resY = 196.0;
kusano 7d535a
	extern int optind;
kusano 7d535a
	extern char* optarg;
kusano 7d535a
kusano 7d535a
kusano 7d535a
	while ((c = getopt(argc, argv, "R:X:o:1234ABLMPUW5678abcflmprsuvwz?")) != -1)
kusano 7d535a
		switch (c) {
kusano 7d535a
			/* input-related options */
kusano 7d535a
		case '3':		/* input is g3-encoded */
kusano 7d535a
			compression_in = COMPRESSION_CCITTFAX3;
kusano 7d535a
			break;
kusano 7d535a
		case '4':		/* input is g4-encoded */
kusano 7d535a
			compression_in = COMPRESSION_CCITTFAX4;
kusano 7d535a
			break;
kusano 7d535a
		case 'U':		/* input is uncompressed (g3 and g4) */
kusano 7d535a
			group3options_in |= GROUP3OPT_UNCOMPRESSED;
kusano 7d535a
			group4options_in |= GROUP4OPT_UNCOMPRESSED;
kusano 7d535a
			break;
kusano 7d535a
		case '1':		/* input is 1d-encoded (g3 only) */
kusano 7d535a
			group3options_in &= ~GROUP3OPT_2DENCODING;
kusano 7d535a
			break;
kusano 7d535a
		case '2':		/* input is 2d-encoded (g3 only) */
kusano 7d535a
			group3options_in |= GROUP3OPT_2DENCODING;
kusano 7d535a
			break;
kusano 7d535a
		case 'P':	/* input has not-aligned EOL (g3 only) */
kusano 7d535a
			group3options_in &= ~GROUP3OPT_FILLBITS;
kusano 7d535a
			break;
kusano 7d535a
		case 'A':		/* input has aligned EOL (g3 only) */
kusano 7d535a
			group3options_in |= GROUP3OPT_FILLBITS;
kusano 7d535a
			break;
kusano 7d535a
		case 'W':		/* input has 0 mean white */
kusano 7d535a
			photometric_in = PHOTOMETRIC_MINISWHITE;
kusano 7d535a
			break;
kusano 7d535a
		case 'B':		/* input has 0 mean black */
kusano 7d535a
			photometric_in = PHOTOMETRIC_MINISBLACK;
kusano 7d535a
			break;
kusano 7d535a
		case 'L':		/* input has lsb-to-msb fillorder */
kusano 7d535a
			fillorder_in = FILLORDER_LSB2MSB;
kusano 7d535a
			break;
kusano 7d535a
		case 'M':		/* input has msb-to-lsb fillorder */
kusano 7d535a
			fillorder_in = FILLORDER_MSB2LSB;
kusano 7d535a
			break;
kusano 7d535a
		case 'R':		/* input resolution */
kusano 7d535a
			resY = (float) atof(optarg);
kusano 7d535a
			break;
kusano 7d535a
		case 'X':		/* input width */
kusano 7d535a
			xsize = (uint32) atoi(optarg);
kusano 7d535a
			break;
kusano 7d535a
kusano 7d535a
			/* output-related options */
kusano 7d535a
		case '7':		/* generate g3-encoded output */
kusano 7d535a
			compression_out = COMPRESSION_CCITTFAX3;
kusano 7d535a
			break;
kusano 7d535a
		case '8':		/* generate g4-encoded output */
kusano 7d535a
			compression_out = COMPRESSION_CCITTFAX4;
kusano 7d535a
			break;
kusano 7d535a
		case 'u':	/* generate uncompressed output (g3 and g4) */
kusano 7d535a
			group3options_out |= GROUP3OPT_UNCOMPRESSED;
kusano 7d535a
			group4options_out |= GROUP4OPT_UNCOMPRESSED;
kusano 7d535a
			break;
kusano 7d535a
		case '5':	/* generate 1d-encoded output (g3 only) */
kusano 7d535a
			group3options_out &= ~GROUP3OPT_2DENCODING;
kusano 7d535a
			break;
kusano 7d535a
		case '6':	/* generate 2d-encoded output (g3 only) */
kusano 7d535a
			group3options_out |= GROUP3OPT_2DENCODING;
kusano 7d535a
			break;
kusano 7d535a
		case 'c':		/* generate "classic" g3 format */
kusano 7d535a
			mode = FAXMODE_CLASSIC;
kusano 7d535a
			break;
kusano 7d535a
		case 'f':		/* generate Class F format */
kusano 7d535a
			mode = FAXMODE_CLASSF;
kusano 7d535a
			break;
kusano 7d535a
		case 'm':		/* output's fillorder is msb-to-lsb */
kusano 7d535a
			fillorder_out = FILLORDER_MSB2LSB;
kusano 7d535a
			break;
kusano 7d535a
		case 'l':		/* output's fillorder is lsb-to-msb */
kusano 7d535a
			fillorder_out = FILLORDER_LSB2MSB;
kusano 7d535a
			break;
kusano 7d535a
		case 'o':
kusano 7d535a
			out = TIFFOpen(optarg, "w");
kusano 7d535a
			if (out == NULL) {
kusano 7d535a
				fprintf(stderr,
kusano 7d535a
				    "%s: Can not create or open %s\n",
kusano 7d535a
				    argv[0], optarg);
kusano 7d535a
				return EXIT_FAILURE;
kusano 7d535a
			}
kusano 7d535a
			break;
kusano 7d535a
		case 'a':	/* generate EOL-aligned output (g3 only) */
kusano 7d535a
			group3options_out |= GROUP3OPT_FILLBITS;
kusano 7d535a
			break;
kusano 7d535a
		case 'p':	/* generate not EOL-aligned output (g3 only) */
kusano 7d535a
			group3options_out &= ~GROUP3OPT_FILLBITS;
kusano 7d535a
			break;
kusano 7d535a
		case 'r':		/* rows/strip */
kusano 7d535a
			defrowsperstrip = atol(optarg);
kusano 7d535a
			break;
kusano 7d535a
		case 's':		/* stretch image by dup'ng scanlines */
kusano 7d535a
			stretch = 1;
kusano 7d535a
			break;
kusano 7d535a
		case 'w':		/* undocumented -- for testing */
kusano 7d535a
			photometric_out = PHOTOMETRIC_MINISWHITE;
kusano 7d535a
			break;
kusano 7d535a
		case 'b':		/* undocumented -- for testing */
kusano 7d535a
			photometric_out = PHOTOMETRIC_MINISBLACK;
kusano 7d535a
			break;
kusano 7d535a
		case 'z':		/* undocumented -- for testing */
kusano 7d535a
			compression_out = COMPRESSION_LZW;
kusano 7d535a
			break;
kusano 7d535a
		case 'v':		/* -v for info */
kusano 7d535a
			verbose++;
kusano 7d535a
			break;
kusano 7d535a
		case '?':
kusano 7d535a
			usage();
kusano 7d535a
			/*NOTREACHED*/
kusano 7d535a
		}
kusano 7d535a
	npages = argc - optind;
kusano 7d535a
	if (npages < 1)
kusano 7d535a
		usage();
kusano 7d535a
kusano 7d535a
	rowbuf = _TIFFmalloc(TIFFhowmany8(xsize));
kusano 7d535a
	refbuf = _TIFFmalloc(TIFFhowmany8(xsize));
kusano 7d535a
	if (rowbuf == NULL || refbuf == NULL) {
kusano 7d535a
		fprintf(stderr, "%s: Not enough memory\n", argv[0]);
kusano 7d535a
		return (EXIT_FAILURE);
kusano 7d535a
	}
kusano 7d535a
kusano 7d535a
	if (out == NULL) {
kusano 7d535a
		out = TIFFOpen("fax.tif", "w");
kusano 7d535a
		if (out == NULL) {
kusano 7d535a
			fprintf(stderr, "%s: Can not create fax.tif\n",
kusano 7d535a
			    argv[0]);
kusano 7d535a
			return (EXIT_FAILURE);
kusano 7d535a
		}
kusano 7d535a
	}
kusano 7d535a
		
kusano 7d535a
	faxTIFF = TIFFClientOpen("(FakeInput)", "w",
kusano 7d535a
	/* TIFFClientOpen() fails if we don't set existing value here */
kusano 7d535a
				 TIFFClientdata(out),
kusano 7d535a
				 TIFFGetReadProc(out), TIFFGetWriteProc(out),
kusano 7d535a
				 TIFFGetSeekProc(out), TIFFGetCloseProc(out),
kusano 7d535a
				 TIFFGetSizeProc(out), TIFFGetMapFileProc(out),
kusano 7d535a
				 TIFFGetUnmapFileProc(out));
kusano 7d535a
	if (faxTIFF == NULL) {
kusano 7d535a
		fprintf(stderr, "%s: Can not create fake input file\n",
kusano 7d535a
		    argv[0]);
kusano 7d535a
		return (EXIT_FAILURE);
kusano 7d535a
	}
kusano 7d535a
	TIFFSetMode(faxTIFF, O_RDONLY);
kusano 7d535a
	TIFFSetField(faxTIFF, TIFFTAG_IMAGEWIDTH,	xsize);
kusano 7d535a
	TIFFSetField(faxTIFF, TIFFTAG_SAMPLESPERPIXEL,	1);
kusano 7d535a
	TIFFSetField(faxTIFF, TIFFTAG_BITSPERSAMPLE,	1);
kusano 7d535a
	TIFFSetField(faxTIFF, TIFFTAG_FILLORDER,	fillorder_in);
kusano 7d535a
	TIFFSetField(faxTIFF, TIFFTAG_PLANARCONFIG,	PLANARCONFIG_CONTIG);
kusano 7d535a
	TIFFSetField(faxTIFF, TIFFTAG_PHOTOMETRIC,	photometric_in);
kusano 7d535a
	TIFFSetField(faxTIFF, TIFFTAG_YRESOLUTION,	resY);
kusano 7d535a
	TIFFSetField(faxTIFF, TIFFTAG_RESOLUTIONUNIT,	RESUNIT_INCH);
kusano 7d535a
	
kusano 7d535a
	/* NB: this must be done after directory info is setup */
kusano 7d535a
	TIFFSetField(faxTIFF, TIFFTAG_COMPRESSION, compression_in);
kusano 7d535a
	if (compression_in == COMPRESSION_CCITTFAX3)
kusano 7d535a
		TIFFSetField(faxTIFF, TIFFTAG_GROUP3OPTIONS, group3options_in);
kusano 7d535a
	else if (compression_in == COMPRESSION_CCITTFAX4)
kusano 7d535a
		TIFFSetField(faxTIFF, TIFFTAG_GROUP4OPTIONS, group4options_in);
kusano 7d535a
	for (pn = 0; optind < argc; pn++, optind++) {
kusano 7d535a
		in = fopen(argv[optind], "rb");
kusano 7d535a
		if (in == NULL) {
kusano 7d535a
			fprintf(stderr,
kusano 7d535a
			    "%s: %s: Can not open\n", argv[0], argv[optind]);
kusano 7d535a
			continue;
kusano 7d535a
		}
kusano 7d535a
#if defined(_WIN32) && defined(USE_WIN32_FILEIO)
kusano 7d535a
                TIFFSetClientdata(faxTIFF, (thandle_t)_get_osfhandle(fileno(in)));
kusano 7d535a
#else
kusano 7d535a
                TIFFSetClientdata(faxTIFF, (thandle_t)fileno(in));
kusano 7d535a
#endif
kusano 7d535a
		TIFFSetFileName(faxTIFF, (const char*)argv[optind]);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 1);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_COMPRESSION, compression_out);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric_out);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
kusano 7d535a
		switch (compression_out) {
kusano 7d535a
			/* g3 */
kusano 7d535a
			case COMPRESSION_CCITTFAX3:
kusano 7d535a
			TIFFSetField(out, TIFFTAG_GROUP3OPTIONS,
kusano 7d535a
				     group3options_out);
kusano 7d535a
			TIFFSetField(out, TIFFTAG_FAXMODE, mode);
kusano 7d535a
			rowsperstrip =
kusano 7d535a
				(defrowsperstrip)?defrowsperstrip:(uint32)-1L;
kusano 7d535a
			break;
kusano 7d535a
kusano 7d535a
			/* g4 */
kusano 7d535a
			case COMPRESSION_CCITTFAX4:
kusano 7d535a
			TIFFSetField(out, TIFFTAG_GROUP4OPTIONS,
kusano 7d535a
				     group4options_out);
kusano 7d535a
			TIFFSetField(out, TIFFTAG_FAXMODE, mode);
kusano 7d535a
			rowsperstrip =
kusano 7d535a
				(defrowsperstrip)?defrowsperstrip:(uint32)-1L;
kusano 7d535a
			break;
kusano 7d535a
kusano 7d535a
			default:
kusano 7d535a
			rowsperstrip = (defrowsperstrip) ?
kusano 7d535a
				defrowsperstrip : TIFFDefaultStripSize(out, 0);
kusano 7d535a
		}
kusano 7d535a
		TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_FILLORDER, fillorder_out);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_SOFTWARE, "fax2tiff");
kusano 7d535a
		TIFFSetField(out, TIFFTAG_XRESOLUTION, 204.0);
kusano 7d535a
		if (!stretch) {
kusano 7d535a
			TIFFGetField(faxTIFF, TIFFTAG_YRESOLUTION, &resY);
kusano 7d535a
			TIFFSetField(out, TIFFTAG_YRESOLUTION, resY);
kusano 7d535a
		} else
kusano 7d535a
			TIFFSetField(out, TIFFTAG_YRESOLUTION, 196.);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
kusano 7d535a
		TIFFSetField(out, TIFFTAG_PAGENUMBER, pn, npages);
kusano 7d535a
kusano 7d535a
		if (!verbose)
kusano 7d535a
		    whandler = TIFFSetWarningHandler(NULL);
kusano 7d535a
		rows = copyFaxFile(faxTIFF, out);
kusano 7d535a
		fclose(in);
kusano 7d535a
		if (!verbose)
kusano 7d535a
		    (void) TIFFSetWarningHandler(whandler);
kusano 7d535a
kusano 7d535a
		TIFFSetField(out, TIFFTAG_IMAGELENGTH, rows);
kusano 7d535a
kusano 7d535a
		if (verbose) {
kusano 7d535a
			fprintf(stderr, "%s:\n", argv[optind]);
kusano 7d535a
			fprintf(stderr, "%d rows in input\n", rows);
kusano 7d535a
			fprintf(stderr, "%ld total bad rows\n",
kusano 7d535a
			    (long) badfaxlines);
kusano 7d535a
			fprintf(stderr, "%d max consecutive bad rows\n", badfaxrun);
kusano 7d535a
		}
kusano 7d535a
		if (compression_out == COMPRESSION_CCITTFAX3 &&
kusano 7d535a
		    mode == FAXMODE_CLASSF) {
kusano 7d535a
			TIFFSetField(out, TIFFTAG_BADFAXLINES, badfaxlines);
kusano 7d535a
			TIFFSetField(out, TIFFTAG_CLEANFAXDATA, badfaxlines ?
kusano 7d535a
			    CLEANFAXDATA_REGENERATED : CLEANFAXDATA_CLEAN);
kusano 7d535a
			TIFFSetField(out, TIFFTAG_CONSECUTIVEBADFAXLINES, badfaxrun);
kusano 7d535a
		}
kusano 7d535a
		TIFFWriteDirectory(out);
kusano 7d535a
	}
kusano 7d535a
	TIFFClose(out);
kusano 7d535a
	_TIFFfree(rowbuf);
kusano 7d535a
	_TIFFfree(refbuf);
kusano 7d535a
	return (EXIT_SUCCESS);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
int
kusano 7d535a
copyFaxFile(TIFF* tifin, TIFF* tifout)
kusano 7d535a
{
kusano 7d535a
	uint32 row;
kusano 7d535a
	uint32 linesize = TIFFhowmany8(xsize);
kusano 7d535a
	uint16 badrun;
kusano 7d535a
	int ok;
kusano 7d535a
kusano 7d535a
	tifin->tif_rawdatasize = (tmsize_t)TIFFGetFileSize(tifin);
kusano 7d535a
	tifin->tif_rawdata = _TIFFmalloc(tifin->tif_rawdatasize);
kusano 7d535a
	if (tifin->tif_rawdata == NULL) {
kusano 7d535a
		TIFFError(tifin->tif_name, "Not enough memory");
kusano 7d535a
		return (0);
kusano 7d535a
	}
kusano 7d535a
	if (!ReadOK(tifin, tifin->tif_rawdata, tifin->tif_rawdatasize)) {
kusano 7d535a
		TIFFError(tifin->tif_name, "Read error at scanline 0");
kusano 7d535a
		return (0);
kusano 7d535a
	}
kusano 7d535a
	tifin->tif_rawcp = tifin->tif_rawdata;
kusano 7d535a
	tifin->tif_rawcc = tifin->tif_rawdatasize;
kusano 7d535a
kusano 7d535a
	(*tifin->tif_setupdecode)(tifin);
kusano 7d535a
	(*tifin->tif_predecode)(tifin, (tsample_t) 0);
kusano 7d535a
	tifin->tif_row = 0;
kusano 7d535a
	badfaxlines = 0;
kusano 7d535a
	badfaxrun = 0;
kusano 7d535a
kusano 7d535a
	_TIFFmemset(refbuf, 0, linesize);
kusano 7d535a
	row = 0;
kusano 7d535a
	badrun = 0;		/* current run of bad lines */
kusano 7d535a
	while (tifin->tif_rawcc > 0) {
kusano 7d535a
		ok = (*tifin->tif_decoderow)(tifin, (tdata_t) rowbuf, 
kusano 7d535a
					     linesize, 0);
kusano 7d535a
		if (!ok) {
kusano 7d535a
			badfaxlines++;
kusano 7d535a
			badrun++;
kusano 7d535a
			/* regenerate line from previous good line */
kusano 7d535a
			_TIFFmemcpy(rowbuf, refbuf, linesize);
kusano 7d535a
		} else {
kusano 7d535a
			if (badrun > badfaxrun)
kusano 7d535a
				badfaxrun = badrun;
kusano 7d535a
			badrun = 0;
kusano 7d535a
			_TIFFmemcpy(refbuf, rowbuf, linesize);
kusano 7d535a
		}
kusano 7d535a
		tifin->tif_row++;
kusano 7d535a
kusano 7d535a
		if (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0) {
kusano 7d535a
			fprintf(stderr, "%s: Write error at row %ld.\n",
kusano 7d535a
			    tifout->tif_name, (long) row);
kusano 7d535a
			break;
kusano 7d535a
		}
kusano 7d535a
		row++;
kusano 7d535a
		if (stretch) {
kusano 7d535a
			if (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0) {
kusano 7d535a
				fprintf(stderr, "%s: Write error at row %ld.\n",
kusano 7d535a
				    tifout->tif_name, (long) row);
kusano 7d535a
				break;
kusano 7d535a
			}
kusano 7d535a
			row++;
kusano 7d535a
		}
kusano 7d535a
	}
kusano 7d535a
	if (badrun > badfaxrun)
kusano 7d535a
		badfaxrun = badrun;
kusano 7d535a
	_TIFFfree(tifin->tif_rawdata);
kusano 7d535a
	return (row);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
char* stuff[] = {
kusano 7d535a
"usage: fax2tiff [options] input.raw...",
kusano 7d535a
"where options are:",
kusano 7d535a
" -3		input data is G3-encoded		[default]",
kusano 7d535a
" -4		input data is G4-encoded",
kusano 7d535a
" -U		input data is uncompressed (G3 or G4)",
kusano 7d535a
" -1		input data is 1D-encoded (G3 only)	[default]",
kusano 7d535a
" -2		input data is 2D-encoded (G3 only)",
kusano 7d535a
" -P		input is not EOL-aligned (G3 only)	[default]",
kusano 7d535a
" -A		input is EOL-aligned (G3 only)",
kusano 7d535a
" -M		input data has MSB2LSB bit order",
kusano 7d535a
" -L		input data has LSB2MSB bit order	[default]",
kusano 7d535a
" -B		input data has min 0 means black",
kusano 7d535a
" -W		input data has min 0 means white	[default]",
kusano 7d535a
" -R #		input data has # resolution (lines/inch) [default is 196]",
kusano 7d535a
" -X #		input data has # width			[default is 1728]",
kusano 7d535a
"",
kusano 7d535a
" -o out.tif	write output to out.tif",
kusano 7d535a
" -7		generate G3-encoded output		[default]",
kusano 7d535a
" -8		generate G4-encoded output",
kusano 7d535a
" -u		generate uncompressed output (G3 or G4)",
kusano 7d535a
" -5		generate 1D-encoded output (G3 only)",
kusano 7d535a
" -6		generate 2D-encoded output (G3 only)	[default]",
kusano 7d535a
" -p		generate not EOL-aligned output (G3 only)",
kusano 7d535a
" -a		generate EOL-aligned output (G3 only)	[default]",
kusano 7d535a
" -c		generate \"classic\" TIFF format",
kusano 7d535a
" -f		generate TIFF Class F (TIFF/F) format	[default]",
kusano 7d535a
" -m		output fill order is MSB2LSB",
kusano 7d535a
" -l		output fill order is LSB2MSB		[default]",
kusano 7d535a
" -r #		make each strip have no more than # rows",
kusano 7d535a
" -s		stretch image by duplicating scanlines",
kusano 7d535a
" -v		print information about conversion work",
kusano 7d535a
" -z		generate LZW compressed output",
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(EXIT_FAILURE);
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
 */