1 /* $Id: fax2tiff.c,v 1.25 2016-10-25 22:22:45 erouault Exp $ */
4 * Copyright (c) 1990-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Convert a CCITT Group 3 or 4 FAX file to TIFF Group 3 or 4 format.
30 #include "tif_config.h"
33 #include <stdlib.h> /* should have atof & getopt */
54 # define EXIT_SUCCESS 0
57 # define EXIT_FAILURE 1
60 #define TIFFhowmany8(x) (((x)&0x07)?((uint32)(x)>>3)+1:(uint32)(x)>>3)
72 int copyFaxFile(TIFF* tifin, TIFF* tifout);
73 static void usage(void);
76 Struct to carry client data. Note that it does not appear that the client
77 data is actually used in this program.
79 typedef struct _FAX_Client_Data
81 #if defined(_WIN32) && defined(USE_WIN32_FILEIO)
82 intptr_t fh; /* Operating system file handle */
84 int fd; /* Integer file descriptor */
90 main(int argc, char* argv[])
94 FAX_Client_Data client_data;
95 TIFFErrorHandler whandler = NULL;
96 int compression_in = COMPRESSION_CCITTFAX3;
97 int compression_out = COMPRESSION_CCITTFAX3;
98 int fillorder_in = FILLORDER_LSB2MSB;
99 int fillorder_out = FILLORDER_LSB2MSB;
100 uint32 group3options_in = 0; /* 1d-encoded */
101 uint32 group3options_out = 0; /* 1d-encoded */
102 uint32 group4options_in = 0; /* compressed */
103 uint32 group4options_out = 0; /* compressed */
104 uint32 defrowsperstrip = (uint32) 0;
106 int photometric_in = PHOTOMETRIC_MINISWHITE;
107 int photometric_out = PHOTOMETRIC_MINISWHITE;
108 int mode = FAXMODE_CLASSF;
114 #if !HAVE_DECL_OPTARG
119 while ((c = getopt(argc, argv, "R:X:o:r:1234ABLMPUW5678abcflmprsuvwz?")) != -1)
121 /* input-related options */
122 case '3': /* input is g3-encoded */
123 compression_in = COMPRESSION_CCITTFAX3;
125 case '4': /* input is g4-encoded */
126 compression_in = COMPRESSION_CCITTFAX4;
128 case 'U': /* input is uncompressed (g3 and g4) */
129 group3options_in |= GROUP3OPT_UNCOMPRESSED;
130 group4options_in |= GROUP4OPT_UNCOMPRESSED;
132 case '1': /* input is 1d-encoded (g3 only) */
133 group3options_in &= ~GROUP3OPT_2DENCODING;
135 case '2': /* input is 2d-encoded (g3 only) */
136 group3options_in |= GROUP3OPT_2DENCODING;
138 case 'P': /* input has not-aligned EOL (g3 only) */
139 group3options_in &= ~GROUP3OPT_FILLBITS;
141 case 'A': /* input has aligned EOL (g3 only) */
142 group3options_in |= GROUP3OPT_FILLBITS;
144 case 'W': /* input has 0 mean white */
145 photometric_in = PHOTOMETRIC_MINISWHITE;
147 case 'B': /* input has 0 mean black */
148 photometric_in = PHOTOMETRIC_MINISBLACK;
150 case 'L': /* input has lsb-to-msb fillorder */
151 fillorder_in = FILLORDER_LSB2MSB;
153 case 'M': /* input has msb-to-lsb fillorder */
154 fillorder_in = FILLORDER_MSB2LSB;
156 case 'R': /* input resolution */
157 resY = (float) atof(optarg);
159 case 'X': /* input width */
160 xsize = (uint32) atoi(optarg);
163 /* output-related options */
164 case '7': /* generate g3-encoded output */
165 compression_out = COMPRESSION_CCITTFAX3;
167 case '8': /* generate g4-encoded output */
168 compression_out = COMPRESSION_CCITTFAX4;
170 case 'u': /* generate uncompressed output (g3 and g4) */
171 group3options_out |= GROUP3OPT_UNCOMPRESSED;
172 group4options_out |= GROUP4OPT_UNCOMPRESSED;
174 case '5': /* generate 1d-encoded output (g3 only) */
175 group3options_out &= ~GROUP3OPT_2DENCODING;
177 case '6': /* generate 2d-encoded output (g3 only) */
178 group3options_out |= GROUP3OPT_2DENCODING;
180 case 'c': /* generate "classic" g3 format */
181 mode = FAXMODE_CLASSIC;
183 case 'f': /* generate Class F format */
184 mode = FAXMODE_CLASSF;
186 case 'm': /* output's fillorder is msb-to-lsb */
187 fillorder_out = FILLORDER_MSB2LSB;
189 case 'l': /* output's fillorder is lsb-to-msb */
190 fillorder_out = FILLORDER_LSB2MSB;
193 out = TIFFOpen(optarg, "w");
196 "%s: Can not create or open %s\n",
201 case 'a': /* generate EOL-aligned output (g3 only) */
202 group3options_out |= GROUP3OPT_FILLBITS;
204 case 'p': /* generate not EOL-aligned output (g3 only) */
205 group3options_out &= ~GROUP3OPT_FILLBITS;
207 case 'r': /* rows/strip */
208 defrowsperstrip = atol(optarg);
210 case 's': /* stretch image by dup'ng scanlines */
213 case 'w': /* undocumented -- for testing */
214 photometric_out = PHOTOMETRIC_MINISWHITE;
216 case 'b': /* undocumented -- for testing */
217 photometric_out = PHOTOMETRIC_MINISBLACK;
219 case 'z': /* undocumented -- for testing */
220 compression_out = COMPRESSION_LZW;
222 case 'v': /* -v for info */
229 npages = argc - optind;
233 rowbuf = _TIFFmalloc(TIFFhowmany8(xsize));
234 refbuf = _TIFFmalloc(TIFFhowmany8(xsize));
235 if (rowbuf == NULL || refbuf == NULL) {
236 fprintf(stderr, "%s: Not enough memory\n", argv[0]);
237 return (EXIT_FAILURE);
241 out = TIFFOpen("fax.tif", "w");
243 fprintf(stderr, "%s: Can not create fax.tif\n",
245 return (EXIT_FAILURE);
249 faxTIFF = TIFFClientOpen("(FakeInput)", "w",
250 /* TIFFClientOpen() fails if we don't set existing value here */
252 TIFFGetReadProc(out), TIFFGetWriteProc(out),
253 TIFFGetSeekProc(out), TIFFGetCloseProc(out),
254 TIFFGetSizeProc(out), TIFFGetMapFileProc(out),
255 TIFFGetUnmapFileProc(out));
256 if (faxTIFF == NULL) {
257 fprintf(stderr, "%s: Can not create fake input file\n",
259 return (EXIT_FAILURE);
261 TIFFSetMode(faxTIFF, O_RDONLY);
262 TIFFSetField(faxTIFF, TIFFTAG_IMAGEWIDTH, xsize);
263 TIFFSetField(faxTIFF, TIFFTAG_SAMPLESPERPIXEL, 1);
264 TIFFSetField(faxTIFF, TIFFTAG_BITSPERSAMPLE, 1);
265 TIFFSetField(faxTIFF, TIFFTAG_FILLORDER, fillorder_in);
266 TIFFSetField(faxTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
267 TIFFSetField(faxTIFF, TIFFTAG_PHOTOMETRIC, photometric_in);
268 TIFFSetField(faxTIFF, TIFFTAG_YRESOLUTION, resY);
269 TIFFSetField(faxTIFF, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
271 /* NB: this must be done after directory info is setup */
272 TIFFSetField(faxTIFF, TIFFTAG_COMPRESSION, compression_in);
273 if (compression_in == COMPRESSION_CCITTFAX3)
274 TIFFSetField(faxTIFF, TIFFTAG_GROUP3OPTIONS, group3options_in);
275 else if (compression_in == COMPRESSION_CCITTFAX4)
276 TIFFSetField(faxTIFF, TIFFTAG_GROUP4OPTIONS, group4options_in);
277 for (pn = 0; optind < argc; pn++, optind++) {
278 in = fopen(argv[optind], "rb");
281 "%s: %s: Can not open\n", argv[0], argv[optind]);
284 #if defined(_WIN32) && defined(USE_WIN32_FILEIO)
285 client_data.fh = _get_osfhandle(fileno(in));
287 client_data.fd = fileno(in);
289 TIFFSetClientdata(faxTIFF, (thandle_t) &client_data);
290 TIFFSetFileName(faxTIFF, (const char*)argv[optind]);
291 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
292 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 1);
293 TIFFSetField(out, TIFFTAG_COMPRESSION, compression_out);
294 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric_out);
295 TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
296 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
297 switch (compression_out) {
299 case COMPRESSION_CCITTFAX3:
300 TIFFSetField(out, TIFFTAG_GROUP3OPTIONS,
302 TIFFSetField(out, TIFFTAG_FAXMODE, mode);
304 (defrowsperstrip)?defrowsperstrip:(uint32)-1L;
308 case COMPRESSION_CCITTFAX4:
309 TIFFSetField(out, TIFFTAG_GROUP4OPTIONS,
311 TIFFSetField(out, TIFFTAG_FAXMODE, mode);
313 (defrowsperstrip)?defrowsperstrip:(uint32)-1L;
317 rowsperstrip = (defrowsperstrip) ?
318 defrowsperstrip : TIFFDefaultStripSize(out, 0);
320 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
321 TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
322 TIFFSetField(out, TIFFTAG_FILLORDER, fillorder_out);
323 TIFFSetField(out, TIFFTAG_SOFTWARE, "fax2tiff");
324 TIFFSetField(out, TIFFTAG_XRESOLUTION, 204.0);
326 TIFFGetField(faxTIFF, TIFFTAG_YRESOLUTION, &resY);
327 TIFFSetField(out, TIFFTAG_YRESOLUTION, resY);
329 TIFFSetField(out, TIFFTAG_YRESOLUTION, 196.);
330 TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
331 TIFFSetField(out, TIFFTAG_PAGENUMBER, pn, npages);
334 whandler = TIFFSetWarningHandler(NULL);
335 rows = copyFaxFile(faxTIFF, out);
338 (void) TIFFSetWarningHandler(whandler);
340 TIFFSetField(out, TIFFTAG_IMAGELENGTH, rows);
343 fprintf(stderr, "%s:\n", argv[optind]);
344 fprintf(stderr, "%d rows in input\n", rows);
345 fprintf(stderr, "%ld total bad rows\n",
347 fprintf(stderr, "%d max consecutive bad rows\n", badfaxrun);
349 if (compression_out == COMPRESSION_CCITTFAX3 &&
350 mode == FAXMODE_CLASSF) {
351 TIFFSetField(out, TIFFTAG_BADFAXLINES, badfaxlines);
352 TIFFSetField(out, TIFFTAG_CLEANFAXDATA, badfaxlines ?
353 CLEANFAXDATA_REGENERATED : CLEANFAXDATA_CLEAN);
354 TIFFSetField(out, TIFFTAG_CONSECUTIVEBADFAXLINES, badfaxrun);
356 TIFFWriteDirectory(out);
361 return (EXIT_SUCCESS);
365 copyFaxFile(TIFF* tifin, TIFF* tifout)
368 uint32 linesize = TIFFhowmany8(xsize);
372 tifin->tif_rawdatasize = (tmsize_t)TIFFGetFileSize(tifin);
373 tifin->tif_rawdata = _TIFFmalloc(tifin->tif_rawdatasize);
374 if (tifin->tif_rawdata == NULL) {
375 TIFFError(tifin->tif_name, "Not enough memory");
378 if (!ReadOK(tifin, tifin->tif_rawdata, tifin->tif_rawdatasize)) {
379 TIFFError(tifin->tif_name, "Read error at scanline 0");
382 tifin->tif_rawcp = tifin->tif_rawdata;
383 tifin->tif_rawcc = tifin->tif_rawdatasize;
385 (*tifin->tif_setupdecode)(tifin);
386 (*tifin->tif_predecode)(tifin, (tsample_t) 0);
391 _TIFFmemset(refbuf, 0, linesize);
393 badrun = 0; /* current run of bad lines */
394 while (tifin->tif_rawcc > 0) {
395 ok = (*tifin->tif_decoderow)(tifin, (tdata_t) rowbuf,
400 /* regenerate line from previous good line */
401 _TIFFmemcpy(rowbuf, refbuf, linesize);
403 if (badrun > badfaxrun)
406 _TIFFmemcpy(refbuf, rowbuf, linesize);
410 if (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0) {
411 fprintf(stderr, "%s: Write error at row %ld.\n",
412 tifout->tif_name, (long) row);
417 if (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0) {
418 fprintf(stderr, "%s: Write error at row %ld.\n",
419 tifout->tif_name, (long) row);
425 if (badrun > badfaxrun)
427 _TIFFfree(tifin->tif_rawdata);
432 "usage: fax2tiff [options] input.raw...",
433 "where options are:",
434 " -3 input data is G3-encoded [default]",
435 " -4 input data is G4-encoded",
436 " -U input data is uncompressed (G3 or G4)",
437 " -1 input data is 1D-encoded (G3 only) [default]",
438 " -2 input data is 2D-encoded (G3 only)",
439 " -P input is not EOL-aligned (G3 only) [default]",
440 " -A input is EOL-aligned (G3 only)",
441 " -M input data has MSB2LSB bit order",
442 " -L input data has LSB2MSB bit order [default]",
443 " -B input data has min 0 means black",
444 " -W input data has min 0 means white [default]",
445 " -R # input data has # resolution (lines/inch) [default is 196]",
446 " -X # input data has # width [default is 1728]",
448 " -o out.tif write output to out.tif",
449 " -7 generate G3-encoded output [default]",
450 " -8 generate G4-encoded output",
451 " -u generate uncompressed output (G3 or G4)",
452 " -5 generate 1D-encoded output (G3 only)",
453 " -6 generate 2D-encoded output (G3 only) [default]",
454 " -p generate not EOL-aligned output (G3 only)",
455 " -a generate EOL-aligned output (G3 only) [default]",
456 " -c generate \"classic\" TIFF format",
457 " -f generate TIFF Class F (TIFF/F) format [default]",
458 " -m output fill order is MSB2LSB",
459 " -l output fill order is LSB2MSB [default]",
460 " -r # make each strip have no more than # rows",
461 " -s stretch image by duplicating scanlines",
462 " -v print information about conversion work",
463 " -z generate LZW compressed output",
474 fprintf(stderr, "%s\n\n", TIFFGetVersion());
475 for (i = 0; stuff[i] != NULL; i++)
476 fprintf(stderr, "%s\n", stuff[i]);
480 /* vim: set ts=8 sts=8 sw=8 noet: */