2016-10-25 18:22:45 -04:00
|
|
|
/* $Id: fax2tiff.c,v 1.25 2016-10-25 22:22:45 erouault Exp $ */
|
1999-07-27 17:50:26 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1990-1997 Sam Leffler
|
|
|
|
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and
|
|
|
|
* its documentation for any purpose is hereby granted without fee, provided
|
|
|
|
* that (i) the above copyright notices and this permission notice appear in
|
|
|
|
* all copies of the software and related documentation, and (ii) the names of
|
|
|
|
* Sam Leffler and Silicon Graphics may not be used in any advertising or
|
|
|
|
* publicity relating to the software without the specific, prior written
|
|
|
|
* permission of Sam Leffler and Silicon Graphics.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
|
|
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
|
|
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
|
|
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
|
|
* OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2003-02-03 11:54:22 -05:00
|
|
|
* Convert a CCITT Group 3 or 4 FAX file to TIFF Group 3 or 4 format.
|
1999-07-27 17:50:26 -04:00
|
|
|
*/
|
2004-09-01 14:43:33 -04:00
|
|
|
#include "tif_config.h"
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h> /* should have atof & getopt */
|
2004-09-01 14:43:33 -04:00
|
|
|
|
2004-09-02 10:36:33 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-09-02 10:27:49 -04:00
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2004-09-02 10:36:33 -04:00
|
|
|
#ifdef HAVE_FCNTL_H
|
2004-09-01 14:43:33 -04:00
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2004-09-02 10:36:33 -04:00
|
|
|
#ifdef HAVE_IO_H
|
2004-09-01 14:43:33 -04:00
|
|
|
# include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-22 15:53:07 -05:00
|
|
|
#ifdef NEED_LIBPORT
|
|
|
|
# include "libport.h"
|
|
|
|
#endif
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
#include "tiffiop.h"
|
|
|
|
|
|
|
|
#ifndef EXIT_SUCCESS
|
2004-09-02 10:36:33 -04:00
|
|
|
# define EXIT_SUCCESS 0
|
1999-07-27 17:50:26 -04:00
|
|
|
#endif
|
|
|
|
#ifndef EXIT_FAILURE
|
2004-09-02 10:36:33 -04:00
|
|
|
# define EXIT_FAILURE 1
|
1999-07-27 17:50:26 -04:00
|
|
|
#endif
|
|
|
|
|
2005-10-31 09:48:59 -05:00
|
|
|
#define TIFFhowmany8(x) (((x)&0x07)?((uint32)(x)>>3)+1:(uint32)(x)>>3)
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFF *faxTIFF;
|
2003-02-27 10:04:46 -05:00
|
|
|
char *rowbuf;
|
|
|
|
char *refbuf;
|
1999-07-27 17:50:26 -04:00
|
|
|
|
2005-10-31 09:48:59 -05:00
|
|
|
uint32 xsize = 1728;
|
1999-07-27 17:50:26 -04:00
|
|
|
int verbose;
|
|
|
|
int stretch;
|
|
|
|
uint16 badfaxrun;
|
|
|
|
uint32 badfaxlines;
|
|
|
|
|
|
|
|
int copyFaxFile(TIFF* tifin, TIFF* tifout);
|
|
|
|
static void usage(void);
|
|
|
|
|
2015-08-23 11:40:45 -04:00
|
|
|
/*
|
|
|
|
Struct to carry client data. Note that it does not appear that the client
|
|
|
|
data is actually used in this program.
|
|
|
|
*/
|
|
|
|
typedef struct _FAX_Client_Data
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && defined(USE_WIN32_FILEIO)
|
|
|
|
intptr_t fh; /* Operating system file handle */
|
|
|
|
#else
|
|
|
|
int fd; /* Integer file descriptor */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} FAX_Client_Data;
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
FILE *in;
|
|
|
|
TIFF *out = NULL;
|
2015-08-23 11:40:45 -04:00
|
|
|
FAX_Client_Data client_data;
|
2004-09-01 14:43:33 -04:00
|
|
|
TIFFErrorHandler whandler = NULL;
|
2003-02-03 11:54:22 -05:00
|
|
|
int compression_in = COMPRESSION_CCITTFAX3;
|
|
|
|
int compression_out = COMPRESSION_CCITTFAX3;
|
|
|
|
int fillorder_in = FILLORDER_LSB2MSB;
|
|
|
|
int fillorder_out = FILLORDER_LSB2MSB;
|
|
|
|
uint32 group3options_in = 0; /* 1d-encoded */
|
|
|
|
uint32 group3options_out = 0; /* 1d-encoded */
|
|
|
|
uint32 group4options_in = 0; /* compressed */
|
|
|
|
uint32 group4options_out = 0; /* compressed */
|
2005-10-31 08:40:38 -05:00
|
|
|
uint32 defrowsperstrip = (uint32) 0;
|
|
|
|
uint32 rowsperstrip;
|
2003-02-03 11:54:22 -05:00
|
|
|
int photometric_in = PHOTOMETRIC_MINISWHITE;
|
|
|
|
int photometric_out = PHOTOMETRIC_MINISWHITE;
|
1999-07-27 17:50:26 -04:00
|
|
|
int mode = FAXMODE_CLASSF;
|
|
|
|
int rows;
|
|
|
|
int c;
|
|
|
|
int pn, npages;
|
2002-04-22 03:32:24 -04:00
|
|
|
float resY = 196.0;
|
2015-06-20 21:09:09 -04:00
|
|
|
|
|
|
|
#if !HAVE_DECL_OPTARG
|
1999-07-27 17:50:26 -04:00
|
|
|
extern int optind;
|
|
|
|
extern char* optarg;
|
2015-06-20 21:09:09 -04:00
|
|
|
#endif
|
1999-07-27 17:50:26 -04:00
|
|
|
|
2016-10-25 18:22:45 -04:00
|
|
|
while ((c = getopt(argc, argv, "R:X:o:r:1234ABLMPUW5678abcflmprsuvwz?")) != -1)
|
1999-07-27 17:50:26 -04:00
|
|
|
switch (c) {
|
|
|
|
/* input-related options */
|
2003-02-03 11:54:22 -05:00
|
|
|
case '3': /* input is g3-encoded */
|
|
|
|
compression_in = COMPRESSION_CCITTFAX3;
|
|
|
|
break;
|
|
|
|
case '4': /* input is g4-encoded */
|
|
|
|
compression_in = COMPRESSION_CCITTFAX4;
|
|
|
|
break;
|
|
|
|
case 'U': /* input is uncompressed (g3 and g4) */
|
|
|
|
group3options_in |= GROUP3OPT_UNCOMPRESSED;
|
|
|
|
group4options_in |= GROUP4OPT_UNCOMPRESSED;
|
|
|
|
break;
|
|
|
|
case '1': /* input is 1d-encoded (g3 only) */
|
|
|
|
group3options_in &= ~GROUP3OPT_2DENCODING;
|
|
|
|
break;
|
|
|
|
case '2': /* input is 2d-encoded (g3 only) */
|
|
|
|
group3options_in |= GROUP3OPT_2DENCODING;
|
|
|
|
break;
|
|
|
|
case 'P': /* input has not-aligned EOL (g3 only) */
|
|
|
|
group3options_in &= ~GROUP3OPT_FILLBITS;
|
|
|
|
break;
|
|
|
|
case 'A': /* input has aligned EOL (g3 only) */
|
|
|
|
group3options_in |= GROUP3OPT_FILLBITS;
|
2002-04-22 03:32:24 -04:00
|
|
|
break;
|
|
|
|
case 'W': /* input has 0 mean white */
|
2003-02-03 11:54:22 -05:00
|
|
|
photometric_in = PHOTOMETRIC_MINISWHITE;
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
|
|
|
case 'B': /* input has 0 mean black */
|
2003-02-03 11:54:22 -05:00
|
|
|
photometric_in = PHOTOMETRIC_MINISBLACK;
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
|
|
|
case 'L': /* input has lsb-to-msb fillorder */
|
2003-02-03 11:54:22 -05:00
|
|
|
fillorder_in = FILLORDER_LSB2MSB;
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
|
|
|
case 'M': /* input has msb-to-lsb fillorder */
|
2003-02-03 11:54:22 -05:00
|
|
|
fillorder_in = FILLORDER_MSB2LSB;
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
|
|
|
case 'R': /* input resolution */
|
2004-06-04 07:50:09 -04:00
|
|
|
resY = (float) atof(optarg);
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
2003-02-27 10:04:46 -05:00
|
|
|
case 'X': /* input width */
|
2005-10-31 09:48:59 -05:00
|
|
|
xsize = (uint32) atoi(optarg);
|
2003-02-27 10:04:46 -05:00
|
|
|
break;
|
1999-07-27 17:50:26 -04:00
|
|
|
|
|
|
|
/* output-related options */
|
2003-02-03 11:54:22 -05:00
|
|
|
case '7': /* generate g3-encoded output */
|
|
|
|
compression_out = COMPRESSION_CCITTFAX3;
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
2003-02-03 11:54:22 -05:00
|
|
|
case '8': /* generate g4-encoded output */
|
|
|
|
compression_out = COMPRESSION_CCITTFAX4;
|
|
|
|
break;
|
|
|
|
case 'u': /* generate uncompressed output (g3 and g4) */
|
|
|
|
group3options_out |= GROUP3OPT_UNCOMPRESSED;
|
|
|
|
group4options_out |= GROUP4OPT_UNCOMPRESSED;
|
|
|
|
break;
|
|
|
|
case '5': /* generate 1d-encoded output (g3 only) */
|
|
|
|
group3options_out &= ~GROUP3OPT_2DENCODING;
|
|
|
|
break;
|
|
|
|
case '6': /* generate 2d-encoded output (g3 only) */
|
|
|
|
group3options_out |= GROUP3OPT_2DENCODING;
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
|
|
|
case 'c': /* generate "classic" g3 format */
|
|
|
|
mode = FAXMODE_CLASSIC;
|
|
|
|
break;
|
|
|
|
case 'f': /* generate Class F format */
|
|
|
|
mode = FAXMODE_CLASSF;
|
|
|
|
break;
|
|
|
|
case 'm': /* output's fillorder is msb-to-lsb */
|
2003-02-03 11:54:22 -05:00
|
|
|
fillorder_out = FILLORDER_MSB2LSB;
|
|
|
|
break;
|
|
|
|
case 'l': /* output's fillorder is lsb-to-msb */
|
|
|
|
fillorder_out = FILLORDER_LSB2MSB;
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
out = TIFFOpen(optarg, "w");
|
2003-02-27 10:04:46 -05:00
|
|
|
if (out == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: Can not create or open %s\n",
|
|
|
|
argv[0], optarg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
2003-02-03 11:54:22 -05:00
|
|
|
case 'a': /* generate EOL-aligned output (g3 only) */
|
|
|
|
group3options_out |= GROUP3OPT_FILLBITS;
|
|
|
|
break;
|
|
|
|
case 'p': /* generate not EOL-aligned output (g3 only) */
|
|
|
|
group3options_out &= ~GROUP3OPT_FILLBITS;
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
2005-10-31 08:40:38 -05:00
|
|
|
case 'r': /* rows/strip */
|
|
|
|
defrowsperstrip = atol(optarg);
|
|
|
|
break;
|
1999-07-27 17:50:26 -04:00
|
|
|
case 's': /* stretch image by dup'ng scanlines */
|
|
|
|
stretch = 1;
|
|
|
|
break;
|
|
|
|
case 'w': /* undocumented -- for testing */
|
2003-02-03 11:54:22 -05:00
|
|
|
photometric_out = PHOTOMETRIC_MINISWHITE;
|
|
|
|
break;
|
|
|
|
case 'b': /* undocumented -- for testing */
|
|
|
|
photometric_out = PHOTOMETRIC_MINISBLACK;
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
|
|
|
case 'z': /* undocumented -- for testing */
|
2003-02-03 11:54:22 -05:00
|
|
|
compression_out = COMPRESSION_LZW;
|
1999-07-27 17:50:26 -04:00
|
|
|
break;
|
|
|
|
case 'v': /* -v for info */
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2002-04-22 03:32:24 -04:00
|
|
|
npages = argc - optind;
|
|
|
|
if (npages < 1)
|
|
|
|
usage();
|
|
|
|
|
2005-10-31 09:48:59 -05:00
|
|
|
rowbuf = _TIFFmalloc(TIFFhowmany8(xsize));
|
|
|
|
refbuf = _TIFFmalloc(TIFFhowmany8(xsize));
|
2003-02-27 10:04:46 -05:00
|
|
|
if (rowbuf == NULL || refbuf == NULL) {
|
|
|
|
fprintf(stderr, "%s: Not enough memory\n", argv[0]);
|
|
|
|
return (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
if (out == NULL) {
|
|
|
|
out = TIFFOpen("fax.tif", "w");
|
2003-02-27 10:04:46 -05:00
|
|
|
if (out == NULL) {
|
|
|
|
fprintf(stderr, "%s: Can not create fax.tif\n",
|
|
|
|
argv[0]);
|
1999-07-27 17:50:26 -04:00
|
|
|
return (EXIT_FAILURE);
|
2003-02-27 10:04:46 -05:00
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
2002-04-22 03:32:24 -04:00
|
|
|
|
|
|
|
faxTIFF = TIFFClientOpen("(FakeInput)", "w",
|
|
|
|
/* TIFFClientOpen() fails if we don't set existing value here */
|
2004-09-01 14:43:33 -04:00
|
|
|
TIFFClientdata(out),
|
|
|
|
TIFFGetReadProc(out), TIFFGetWriteProc(out),
|
|
|
|
TIFFGetSeekProc(out), TIFFGetCloseProc(out),
|
|
|
|
TIFFGetSizeProc(out), TIFFGetMapFileProc(out),
|
|
|
|
TIFFGetUnmapFileProc(out));
|
2003-02-27 10:04:46 -05:00
|
|
|
if (faxTIFF == NULL) {
|
|
|
|
fprintf(stderr, "%s: Can not create fake input file\n",
|
|
|
|
argv[0]);
|
2002-04-22 03:32:24 -04:00
|
|
|
return (EXIT_FAILURE);
|
2003-02-27 10:04:46 -05:00
|
|
|
}
|
2004-09-01 14:43:33 -04:00
|
|
|
TIFFSetMode(faxTIFF, O_RDONLY);
|
2003-02-27 10:04:46 -05:00
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_IMAGEWIDTH, xsize);
|
2002-04-22 03:32:24 -04:00
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_SAMPLESPERPIXEL, 1);
|
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_BITSPERSAMPLE, 1);
|
2003-02-03 11:54:22 -05:00
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_FILLORDER, fillorder_in);
|
2002-04-22 03:32:24 -04:00
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
|
2003-02-03 11:54:22 -05:00
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_PHOTOMETRIC, photometric_in);
|
2002-04-22 03:32:24 -04:00
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_YRESOLUTION, resY);
|
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
|
|
|
|
|
1999-07-27 17:50:26 -04:00
|
|
|
/* NB: this must be done after directory info is setup */
|
2003-02-03 11:54:22 -05:00
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_COMPRESSION, compression_in);
|
|
|
|
if (compression_in == COMPRESSION_CCITTFAX3)
|
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_GROUP3OPTIONS, group3options_in);
|
|
|
|
else if (compression_in == COMPRESSION_CCITTFAX4)
|
|
|
|
TIFFSetField(faxTIFF, TIFFTAG_GROUP4OPTIONS, group4options_in);
|
1999-07-27 17:50:26 -04:00
|
|
|
for (pn = 0; optind < argc; pn++, optind++) {
|
2006-04-20 08:36:23 -04:00
|
|
|
in = fopen(argv[optind], "rb");
|
1999-07-27 17:50:26 -04:00
|
|
|
if (in == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %s: Can not open\n", argv[0], argv[optind]);
|
|
|
|
continue;
|
|
|
|
}
|
2006-03-21 11:37:51 -05:00
|
|
|
#if defined(_WIN32) && defined(USE_WIN32_FILEIO)
|
2015-08-23 11:40:45 -04:00
|
|
|
client_data.fh = _get_osfhandle(fileno(in));
|
2006-03-21 11:37:51 -05:00
|
|
|
#else
|
2015-08-23 11:40:45 -04:00
|
|
|
client_data.fd = fileno(in);
|
2006-03-21 11:37:51 -05:00
|
|
|
#endif
|
2015-08-23 11:40:45 -04:00
|
|
|
TIFFSetClientdata(faxTIFF, (thandle_t) &client_data);
|
2004-09-01 14:43:33 -04:00
|
|
|
TIFFSetFileName(faxTIFF, (const char*)argv[optind]);
|
2003-02-27 10:04:46 -05:00
|
|
|
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 1);
|
2003-02-03 11:54:22 -05:00
|
|
|
TIFFSetField(out, TIFFTAG_COMPRESSION, compression_out);
|
|
|
|
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric_out);
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
|
|
|
|
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2003-02-03 11:54:22 -05:00
|
|
|
switch (compression_out) {
|
|
|
|
/* g3 */
|
|
|
|
case COMPRESSION_CCITTFAX3:
|
2005-10-31 08:40:38 -05:00
|
|
|
TIFFSetField(out, TIFFTAG_GROUP3OPTIONS,
|
|
|
|
group3options_out);
|
2003-02-03 11:54:22 -05:00
|
|
|
TIFFSetField(out, TIFFTAG_FAXMODE, mode);
|
2005-10-31 08:40:38 -05:00
|
|
|
rowsperstrip =
|
|
|
|
(defrowsperstrip)?defrowsperstrip:(uint32)-1L;
|
2003-02-03 11:54:22 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* g4 */
|
|
|
|
case COMPRESSION_CCITTFAX4:
|
2005-10-31 08:40:38 -05:00
|
|
|
TIFFSetField(out, TIFFTAG_GROUP4OPTIONS,
|
|
|
|
group4options_out);
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFFSetField(out, TIFFTAG_FAXMODE, mode);
|
2005-10-31 08:40:38 -05:00
|
|
|
rowsperstrip =
|
|
|
|
(defrowsperstrip)?defrowsperstrip:(uint32)-1L;
|
2003-02-03 11:54:22 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2005-10-31 08:40:38 -05:00
|
|
|
rowsperstrip = (defrowsperstrip) ?
|
|
|
|
defrowsperstrip : TIFFDefaultStripSize(out, 0);
|
2003-02-03 11:54:22 -05:00
|
|
|
}
|
2005-10-31 08:40:38 -05:00
|
|
|
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
|
2003-02-03 11:54:22 -05:00
|
|
|
TIFFSetField(out, TIFFTAG_FILLORDER, fillorder_out);
|
1999-07-27 17:50:26 -04:00
|
|
|
TIFFSetField(out, TIFFTAG_SOFTWARE, "fax2tiff");
|
|
|
|
TIFFSetField(out, TIFFTAG_XRESOLUTION, 204.0);
|
|
|
|
if (!stretch) {
|
2002-04-22 03:32:24 -04:00
|
|
|
TIFFGetField(faxTIFF, TIFFTAG_YRESOLUTION, &resY);
|
|
|
|
TIFFSetField(out, TIFFTAG_YRESOLUTION, resY);
|
1999-07-27 17:50:26 -04:00
|
|
|
} else
|
|
|
|
TIFFSetField(out, TIFFTAG_YRESOLUTION, 196.);
|
|
|
|
TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
|
2003-05-25 01:56:26 -04:00
|
|
|
TIFFSetField(out, TIFFTAG_PAGENUMBER, pn, npages);
|
1999-07-27 17:50:26 -04:00
|
|
|
|
|
|
|
if (!verbose)
|
|
|
|
whandler = TIFFSetWarningHandler(NULL);
|
|
|
|
rows = copyFaxFile(faxTIFF, out);
|
|
|
|
fclose(in);
|
|
|
|
if (!verbose)
|
|
|
|
(void) TIFFSetWarningHandler(whandler);
|
|
|
|
|
|
|
|
TIFFSetField(out, TIFFTAG_IMAGELENGTH, rows);
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
fprintf(stderr, "%s:\n", argv[optind]);
|
|
|
|
fprintf(stderr, "%d rows in input\n", rows);
|
|
|
|
fprintf(stderr, "%ld total bad rows\n",
|
|
|
|
(long) badfaxlines);
|
|
|
|
fprintf(stderr, "%d max consecutive bad rows\n", badfaxrun);
|
|
|
|
}
|
2003-02-03 11:54:22 -05:00
|
|
|
if (compression_out == COMPRESSION_CCITTFAX3 &&
|
1999-07-27 17:50:26 -04:00
|
|
|
mode == FAXMODE_CLASSF) {
|
|
|
|
TIFFSetField(out, TIFFTAG_BADFAXLINES, badfaxlines);
|
|
|
|
TIFFSetField(out, TIFFTAG_CLEANFAXDATA, badfaxlines ?
|
|
|
|
CLEANFAXDATA_REGENERATED : CLEANFAXDATA_CLEAN);
|
|
|
|
TIFFSetField(out, TIFFTAG_CONSECUTIVEBADFAXLINES, badfaxrun);
|
|
|
|
}
|
|
|
|
TIFFWriteDirectory(out);
|
|
|
|
}
|
|
|
|
TIFFClose(out);
|
2003-02-27 10:04:46 -05:00
|
|
|
_TIFFfree(rowbuf);
|
|
|
|
_TIFFfree(refbuf);
|
1999-07-27 17:50:26 -04:00
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
copyFaxFile(TIFF* tifin, TIFF* tifout)
|
|
|
|
{
|
|
|
|
uint32 row;
|
2005-10-31 09:48:59 -05:00
|
|
|
uint32 linesize = TIFFhowmany8(xsize);
|
1999-07-27 17:50:26 -04:00
|
|
|
uint16 badrun;
|
|
|
|
int ok;
|
|
|
|
|
2009-01-22 16:12:45 -05:00
|
|
|
tifin->tif_rawdatasize = (tmsize_t)TIFFGetFileSize(tifin);
|
1999-07-27 17:50:26 -04:00
|
|
|
tifin->tif_rawdata = _TIFFmalloc(tifin->tif_rawdatasize);
|
2003-02-27 10:04:46 -05:00
|
|
|
if (tifin->tif_rawdata == NULL) {
|
2006-03-17 09:58:57 -05:00
|
|
|
TIFFError(tifin->tif_name, "Not enough memory");
|
2003-02-27 10:04:46 -05:00
|
|
|
return (0);
|
|
|
|
}
|
1999-07-27 17:50:26 -04:00
|
|
|
if (!ReadOK(tifin, tifin->tif_rawdata, tifin->tif_rawdatasize)) {
|
2006-03-17 09:58:57 -05:00
|
|
|
TIFFError(tifin->tif_name, "Read error at scanline 0");
|
1999-07-27 17:50:26 -04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
tifin->tif_rawcp = tifin->tif_rawdata;
|
|
|
|
tifin->tif_rawcc = tifin->tif_rawdatasize;
|
|
|
|
|
|
|
|
(*tifin->tif_setupdecode)(tifin);
|
|
|
|
(*tifin->tif_predecode)(tifin, (tsample_t) 0);
|
|
|
|
tifin->tif_row = 0;
|
|
|
|
badfaxlines = 0;
|
|
|
|
badfaxrun = 0;
|
|
|
|
|
2005-10-31 09:48:59 -05:00
|
|
|
_TIFFmemset(refbuf, 0, linesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
row = 0;
|
|
|
|
badrun = 0; /* current run of bad lines */
|
|
|
|
while (tifin->tif_rawcc > 0) {
|
2000-01-28 16:18:16 -05:00
|
|
|
ok = (*tifin->tif_decoderow)(tifin, (tdata_t) rowbuf,
|
2005-10-31 09:48:59 -05:00
|
|
|
linesize, 0);
|
1999-07-27 17:50:26 -04:00
|
|
|
if (!ok) {
|
|
|
|
badfaxlines++;
|
|
|
|
badrun++;
|
|
|
|
/* regenerate line from previous good line */
|
2005-10-31 09:48:59 -05:00
|
|
|
_TIFFmemcpy(rowbuf, refbuf, linesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
} else {
|
|
|
|
if (badrun > badfaxrun)
|
|
|
|
badfaxrun = badrun;
|
|
|
|
badrun = 0;
|
2005-10-31 09:48:59 -05:00
|
|
|
_TIFFmemcpy(refbuf, rowbuf, linesize);
|
1999-07-27 17:50:26 -04:00
|
|
|
}
|
|
|
|
tifin->tif_row++;
|
|
|
|
|
|
|
|
if (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0) {
|
|
|
|
fprintf(stderr, "%s: Write error at row %ld.\n",
|
|
|
|
tifout->tif_name, (long) row);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
row++;
|
|
|
|
if (stretch) {
|
|
|
|
if (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0) {
|
|
|
|
fprintf(stderr, "%s: Write error at row %ld.\n",
|
|
|
|
tifout->tif_name, (long) row);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (badrun > badfaxrun)
|
|
|
|
badfaxrun = badrun;
|
|
|
|
_TIFFfree(tifin->tif_rawdata);
|
|
|
|
return (row);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* stuff[] = {
|
2003-02-03 11:54:22 -05:00
|
|
|
"usage: fax2tiff [options] input.raw...",
|
1999-07-27 17:50:26 -04:00
|
|
|
"where options are:",
|
2003-02-03 11:54:22 -05:00
|
|
|
" -3 input data is G3-encoded [default]",
|
|
|
|
" -4 input data is G4-encoded",
|
2003-02-27 10:04:46 -05:00
|
|
|
" -U input data is uncompressed (G3 or G4)",
|
2003-02-03 11:54:22 -05:00
|
|
|
" -1 input data is 1D-encoded (G3 only) [default]",
|
|
|
|
" -2 input data is 2D-encoded (G3 only)",
|
|
|
|
" -P input is not EOL-aligned (G3 only) [default]",
|
|
|
|
" -A input is EOL-aligned (G3 only)",
|
1999-07-27 17:50:26 -04:00
|
|
|
" -M input data has MSB2LSB bit order",
|
2003-02-03 11:54:22 -05:00
|
|
|
" -L input data has LSB2MSB bit order [default]",
|
|
|
|
" -B input data has min 0 means black",
|
|
|
|
" -W input data has min 0 means white [default]",
|
|
|
|
" -R # input data has # resolution (lines/inch) [default is 196]",
|
2003-02-27 10:04:46 -05:00
|
|
|
" -X # input data has # width [default is 1728]",
|
1999-07-27 17:50:26 -04:00
|
|
|
"",
|
|
|
|
" -o out.tif write output to out.tif",
|
2003-02-03 11:54:22 -05:00
|
|
|
" -7 generate G3-encoded output [default]",
|
|
|
|
" -8 generate G4-encoded output",
|
2003-02-27 10:04:46 -05:00
|
|
|
" -u generate uncompressed output (G3 or G4)",
|
2003-02-03 11:54:22 -05:00
|
|
|
" -5 generate 1D-encoded output (G3 only)",
|
|
|
|
" -6 generate 2D-encoded output (G3 only) [default]",
|
|
|
|
" -p generate not EOL-aligned output (G3 only)",
|
|
|
|
" -a generate EOL-aligned output (G3 only) [default]",
|
|
|
|
" -c generate \"classic\" TIFF format",
|
|
|
|
" -f generate TIFF Class F (TIFF/F) format [default]",
|
|
|
|
" -m output fill order is MSB2LSB",
|
|
|
|
" -l output fill order is LSB2MSB [default]",
|
2005-10-31 08:40:38 -05:00
|
|
|
" -r # make each strip have no more than # rows",
|
1999-07-27 17:50:26 -04:00
|
|
|
" -s stretch image by duplicating scanlines",
|
|
|
|
" -v print information about conversion work",
|
2004-07-24 15:03:16 -04:00
|
|
|
" -z generate LZW compressed output",
|
1999-07-27 17:50:26 -04:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
setbuf(stderr, buf);
|
2003-03-12 09:05:05 -05:00
|
|
|
fprintf(stderr, "%s\n\n", TIFFGetVersion());
|
1999-07-27 17:50:26 -04:00
|
|
|
for (i = 0; stuff[i] != NULL; i++)
|
|
|
|
fprintf(stderr, "%s\n", stuff[i]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2004-09-01 14:43:33 -04:00
|
|
|
|
|
|
|
/* vim: set ts=8 sts=8 sw=8 noet: */
|
2010-03-10 13:56:48 -05:00
|
|
|
/*
|
|
|
|
* Local Variables:
|
|
|
|
* mode: c
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* fill-column: 78
|
|
|
|
* End:
|
|
|
|
*/
|