Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/tools/fax2tiff.c

raw
1 /*
2 * Copyright (c) 1990-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 /*
26 * Convert a CCITT Group 3 or 4 FAX file to TIFF Group 3 or 4 format.
27 */
28 #include "libport.h"
29 #include "tif_config.h"
30
31 #include <stdio.h>
32 #include <stdlib.h> /* should have atof & getopt */
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #ifdef HAVE_FCNTL_H
39 #include <fcntl.h>
40 #endif
41
42 #ifdef HAVE_IO_H
43 #include <io.h>
44 #endif
45
46 #include "tiffiop.h"
47
48 #ifndef EXIT_SUCCESS
49 #define EXIT_SUCCESS 0
50 #endif
51 #ifndef EXIT_FAILURE
52 #define EXIT_FAILURE 1
53 #endif
54
55 #define TIFFhowmany8(x) \
56 (((x)&0x07) ? ((uint32_t)(x) >> 3) + 1 : (uint32_t)(x) >> 3)
57
58 TIFF *faxTIFF;
59 char *rowbuf;
60 char *refbuf;
61
62 uint32_t xsize = 1728;
63 int verbose;
64 int stretch;
65 uint16_t badfaxrun;
66 uint32_t badfaxlines;
67 int compression_in;
68
69 int copyFaxFile(TIFF *tifin, TIFF *tifout);
70 static void usage(int code);
71
72 /*
73 Struct to carry client data. Note that it does not appear that the client
74 data is actually used in this program.
75 */
76 typedef union _FAX_Client_Data
77 {
78 thandle_t fh; /* Operating system file handle */
79 int fd; /* Integer file descriptor */
80 } FAX_Client_Data;
81
82 int main(int argc, char *argv[])
83 {
84 FILE *in;
85 TIFF *out = NULL;
86 FAX_Client_Data client_data;
87 TIFFErrorHandler whandler = NULL;
88 compression_in = COMPRESSION_CCITTFAX3;
89 int compression_out = COMPRESSION_CCITTFAX3;
90 int fillorder_in = FILLORDER_LSB2MSB;
91 int fillorder_out = FILLORDER_LSB2MSB;
92 uint32_t group3options_in = 0; /* 1d-encoded */
93 uint32_t group3options_out = 0; /* 1d-encoded */
94 uint32_t group4options_in = 0; /* compressed */
95 uint32_t group4options_out = 0; /* compressed */
96 uint32_t defrowsperstrip = (uint32_t)0;
97 uint32_t rowsperstrip;
98 int photometric_in = PHOTOMETRIC_MINISWHITE;
99 int photometric_out = PHOTOMETRIC_MINISWHITE;
100 int mode = FAXMODE_CLASSF;
101 int rows;
102 int c;
103 int pn, npages;
104 float resY = 196.0;
105
106 #if !HAVE_DECL_OPTARG
107 extern int optind;
108 extern char *optarg;
109 #endif
110
111 while ((c = getopt(argc, argv, "R:X:o:r:1234ABLMPUW5678abcflmprsuvwzh")) !=
112 -1)
113 switch (c)
114 {
115 /* input-related options */
116 case '3': /* input is g3-encoded */
117 compression_in = COMPRESSION_CCITTFAX3;
118 break;
119 case '4': /* input is g4-encoded */
120 compression_in = COMPRESSION_CCITTFAX4;
121 break;
122 case 'U': /* input is uncompressed (g3 and g4) */
123 group3options_in |= GROUP3OPT_UNCOMPRESSED;
124 group4options_in |= GROUP4OPT_UNCOMPRESSED;
125 break;
126 case '1': /* input is 1d-encoded (g3 only) */
127 group3options_in &= ~GROUP3OPT_2DENCODING;
128 break;
129 case '2': /* input is 2d-encoded (g3 only) */
130 group3options_in |= GROUP3OPT_2DENCODING;
131 break;
132 case 'P': /* input has not-aligned EOL (g3 only) */
133 group3options_in &= ~GROUP3OPT_FILLBITS;
134 break;
135 case 'A': /* input has aligned EOL (g3 only) */
136 group3options_in |= GROUP3OPT_FILLBITS;
137 break;
138 case 'W': /* input has 0 mean white */
139 photometric_in = PHOTOMETRIC_MINISWHITE;
140 break;
141 case 'B': /* input has 0 mean black */
142 photometric_in = PHOTOMETRIC_MINISBLACK;
143 break;
144 case 'L': /* input has lsb-to-msb fillorder */
145 fillorder_in = FILLORDER_LSB2MSB;
146 break;
147 case 'M': /* input has msb-to-lsb fillorder */
148 fillorder_in = FILLORDER_MSB2LSB;
149 break;
150 case 'R': /* input resolution */
151 resY = (float)atof(optarg);
152 break;
153 case 'X': /* input width */
154 xsize = (uint32_t)atoi(optarg);
155 if (xsize < 1 || xsize > 10000)
156 {
157 fprintf(stderr,
158 "%s: The input width %s is not reasonable\n",
159 argv[0], optarg);
160 return EXIT_FAILURE;
161 }
162 break;
163
164 /* output-related options */
165 case '7': /* generate g3-encoded output */
166 compression_out = COMPRESSION_CCITTFAX3;
167 break;
168 case '8': /* generate g4-encoded output */
169 compression_out = COMPRESSION_CCITTFAX4;
170 break;
171 case 'u': /* generate uncompressed output (g3 and g4) */
172 group3options_out |= GROUP3OPT_UNCOMPRESSED;
173 group4options_out |= GROUP4OPT_UNCOMPRESSED;
174 break;
175 case '5': /* generate 1d-encoded output (g3 only) */
176 group3options_out &= ~GROUP3OPT_2DENCODING;
177 break;
178 case '6': /* generate 2d-encoded output (g3 only) */
179 group3options_out |= GROUP3OPT_2DENCODING;
180 break;
181 case 'c': /* generate "classic" g3 format */
182 mode = FAXMODE_CLASSIC;
183 break;
184 case 'f': /* generate Class F format */
185 mode = FAXMODE_CLASSF;
186 break;
187 case 'm': /* output's fillorder is msb-to-lsb */
188 fillorder_out = FILLORDER_MSB2LSB;
189 break;
190 case 'l': /* output's fillorder is lsb-to-msb */
191 fillorder_out = FILLORDER_LSB2MSB;
192 break;
193 case 'o':
194 out = TIFFOpen(optarg, "w");
195 if (out == NULL)
196 {
197 fprintf(stderr, "%s: Can not create or open %s\n", argv[0],
198 optarg);
199 return EXIT_FAILURE;
200 }
201 break;
202 case 'a': /* generate EOL-aligned output (g3 only) */
203 group3options_out |= GROUP3OPT_FILLBITS;
204 break;
205 case 'p': /* generate not EOL-aligned output (g3 only) */
206 group3options_out &= ~GROUP3OPT_FILLBITS;
207 break;
208 case 'r': /* rows/strip */
209 defrowsperstrip = atol(optarg);
210 break;
211 case 's': /* stretch image by dup'ng scanlines */
212 stretch = 1;
213 break;
214 case 'w': /* undocumented -- for testing */
215 photometric_out = PHOTOMETRIC_MINISWHITE;
216 break;
217 case 'b': /* undocumented -- for testing */
218 photometric_out = PHOTOMETRIC_MINISBLACK;
219 break;
220 case 'z': /* undocumented -- for testing */
221 compression_out = COMPRESSION_LZW;
222 break;
223 case 'v': /* -v for info */
224 verbose++;
225 break;
226 case 'h':
227 usage(EXIT_SUCCESS);
228 break;
229 case '?':
230 usage(EXIT_FAILURE);
231 /*NOTREACHED*/
232 }
233 npages = argc - optind;
234 if (npages < 1)
235 usage(EXIT_FAILURE);
236
237 rowbuf = _TIFFmalloc(TIFFhowmany8(xsize));
238 refbuf = _TIFFmalloc(TIFFhowmany8(xsize));
239 if (rowbuf == NULL || refbuf == NULL)
240 {
241 TIFFClose(out);
242 if (rowbuf)
243 _TIFFfree(rowbuf);
244 if (refbuf)
245 _TIFFfree(refbuf);
246 fprintf(stderr, "%s: Not enough memory\n", argv[0]);
247 return (EXIT_FAILURE);
248 }
249
250 if (out == NULL)
251 {
252 out = TIFFOpen("fax.tif", "w");
253 if (out == NULL)
254 {
255 fprintf(stderr, "%s: Can not create fax.tif\n", argv[0]);
256 TIFFClose(out);
257 if (rowbuf)
258 _TIFFfree(rowbuf);
259 if (refbuf)
260 _TIFFfree(refbuf);
261 return (EXIT_FAILURE);
262 }
263 }
264
265 faxTIFF = TIFFClientOpen(
266 "(FakeInput)", "w",
267 /* TIFFClientOpen() fails if we don't set existing value here */
268 TIFFClientdata(out), TIFFGetReadProc(out), TIFFGetWriteProc(out),
269 TIFFGetSeekProc(out), TIFFGetCloseProc(out), TIFFGetSizeProc(out),
270 TIFFGetMapFileProc(out), TIFFGetUnmapFileProc(out));
271 if (faxTIFF == NULL)
272 {
273 fprintf(stderr, "%s: Can not create fake input file\n", argv[0]);
274 TIFFClose(out);
275 if (rowbuf)
276 _TIFFfree(rowbuf);
277 if (refbuf)
278 _TIFFfree(refbuf);
279 return (EXIT_FAILURE);
280 }
281 TIFFSetMode(faxTIFF, O_RDONLY);
282 TIFFSetField(faxTIFF, TIFFTAG_IMAGEWIDTH, xsize);
283 TIFFSetField(faxTIFF, TIFFTAG_SAMPLESPERPIXEL, 1);
284 TIFFSetField(faxTIFF, TIFFTAG_BITSPERSAMPLE, 1);
285 TIFFSetField(faxTIFF, TIFFTAG_FILLORDER, fillorder_in);
286 TIFFSetField(faxTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
287 TIFFSetField(faxTIFF, TIFFTAG_PHOTOMETRIC, photometric_in);
288 TIFFSetField(faxTIFF, TIFFTAG_YRESOLUTION, resY);
289 TIFFSetField(faxTIFF, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
290
291 /* NB: this must be done after directory info is setup */
292 TIFFSetField(faxTIFF, TIFFTAG_COMPRESSION, compression_in);
293 if (compression_in == COMPRESSION_CCITTFAX3)
294 TIFFSetField(faxTIFF, TIFFTAG_GROUP3OPTIONS, group3options_in);
295 else if (compression_in == COMPRESSION_CCITTFAX4)
296 TIFFSetField(faxTIFF, TIFFTAG_GROUP4OPTIONS, group4options_in);
297 for (pn = 0; optind < argc; pn++, optind++)
298 {
299 in = fopen(argv[optind], "rb");
300 if (in == NULL)
301 {
302 fprintf(stderr, "%s: %s: Can not open\n", argv[0], argv[optind]);
303 continue;
304 }
305 #if defined(_WIN32) && defined(USE_WIN32_FILEIO)
306 client_data.fh = (thandle_t)_get_osfhandle(fileno(in));
307 #else
308 client_data.fd = fileno(in);
309 #endif
310 TIFFSetClientdata(faxTIFF, client_data.fh);
311 TIFFSetFileName(faxTIFF, (const char *)argv[optind]);
312 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, xsize);
313 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 1);
314 TIFFSetField(out, TIFFTAG_COMPRESSION, compression_out);
315 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric_out);
316 TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
317 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
318 switch (compression_out)
319 {
320 /* g3 */
321 case COMPRESSION_CCITTFAX3:
322 TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, group3options_out);
323 TIFFSetField(out, TIFFTAG_FAXMODE, mode);
324 rowsperstrip =
325 (defrowsperstrip) ? defrowsperstrip : (uint32_t)-1L;
326 break;
327
328 /* g4 */
329 case COMPRESSION_CCITTFAX4:
330 TIFFSetField(out, TIFFTAG_GROUP4OPTIONS, group4options_out);
331 TIFFSetField(out, TIFFTAG_FAXMODE, mode);
332 rowsperstrip =
333 (defrowsperstrip) ? defrowsperstrip : (uint32_t)-1L;
334 break;
335
336 default:
337 rowsperstrip = (defrowsperstrip) ? defrowsperstrip
338 : TIFFDefaultStripSize(out, 0);
339 }
340 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
341 TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
342 TIFFSetField(out, TIFFTAG_FILLORDER, fillorder_out);
343 TIFFSetField(out, TIFFTAG_SOFTWARE, "fax2tiff");
344 TIFFSetField(out, TIFFTAG_XRESOLUTION, 204.0);
345 if (!stretch)
346 {
347 TIFFGetField(faxTIFF, TIFFTAG_YRESOLUTION, &resY);
348 TIFFSetField(out, TIFFTAG_YRESOLUTION, resY);
349 }
350 else
351 TIFFSetField(out, TIFFTAG_YRESOLUTION, 196.);
352 TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
353 TIFFSetField(out, TIFFTAG_PAGENUMBER, pn, npages);
354
355 if (!verbose)
356 whandler = TIFFSetWarningHandler(NULL);
357 rows = copyFaxFile(faxTIFF, out);
358 fclose(in);
359 if (!verbose)
360 (void)TIFFSetWarningHandler(whandler);
361
362 TIFFSetField(out, TIFFTAG_IMAGELENGTH, rows);
363
364 if (verbose)
365 {
366 fprintf(stderr, "%s:\n", argv[optind]);
367 fprintf(stderr, "%d rows in input\n", rows);
368 fprintf(stderr, "%" PRIu32 " total bad rows\n", badfaxlines);
369 fprintf(stderr, "%" PRIu16 " max consecutive bad rows\n",
370 badfaxrun);
371 }
372 if (compression_out == COMPRESSION_CCITTFAX3 && mode == FAXMODE_CLASSF)
373 {
374 TIFFSetField(out, TIFFTAG_BADFAXLINES, badfaxlines);
375 TIFFSetField(out, TIFFTAG_CLEANFAXDATA,
376 badfaxlines ? CLEANFAXDATA_REGENERATED
377 : CLEANFAXDATA_CLEAN);
378 TIFFSetField(out, TIFFTAG_CONSECUTIVEBADFAXLINES, badfaxrun);
379 }
380 TIFFWriteDirectory(out);
381 }
382 TIFFClose(out);
383 _TIFFfree(rowbuf);
384 _TIFFfree(refbuf);
385 return (EXIT_SUCCESS);
386 }
387
388 int copyFaxFile(TIFF *tifin, TIFF *tifout)
389 {
390 uint32_t row;
391 uint32_t linesize = TIFFhowmany8(xsize);
392 uint16_t badrun;
393 int ok;
394
395 tifin->tif_rawdatasize = (tmsize_t)TIFFGetFileSize(tifin);
396 if (tifin->tif_rawdatasize == 0)
397 {
398 TIFFError(tifin->tif_name, "Empty input file");
399 return (0);
400 }
401 tifin->tif_rawdata = _TIFFmalloc(tifin->tif_rawdatasize);
402 if (tifin->tif_rawdata == NULL)
403 {
404 TIFFError(tifin->tif_name, "Not enough memory");
405 return (0);
406 }
407 if (!ReadOK(tifin, tifin->tif_rawdata, tifin->tif_rawdatasize))
408 {
409 TIFFError(tifin->tif_name, "Read error at scanline 0");
410 _TIFFfree(tifin->tif_rawdata);
411 return (0);
412 }
413 tifin->tif_rawcp = tifin->tif_rawdata;
414 tifin->tif_rawcc = tifin->tif_rawdatasize;
415
416 (*tifin->tif_setupdecode)(tifin);
417 (*tifin->tif_predecode)(tifin, (tsample_t)0);
418 tifin->tif_row = 0;
419 badfaxlines = 0;
420 badfaxrun = 0;
421
422 _TIFFmemset(refbuf, 0, linesize);
423 row = 0;
424 badrun = 0; /* current run of bad lines */
425 while (tifin->tif_rawcc > 0)
426 {
427 ok = (*tifin->tif_decoderow)(tifin, (tdata_t)rowbuf, linesize, 0);
428 if (ok < 1)
429 {
430 if (compression_in == COMPRESSION_CCITTFAX4)
431 {
432 /* This is probably EOFB, but if it's corrupt data, then we
433 * can't continue, anyway. */
434 break;
435 }
436 badfaxlines++;
437 badrun++;
438 /* regenerate line from previous good line */
439 _TIFFmemcpy(rowbuf, refbuf, linesize);
440 }
441 else
442 {
443 if (badrun > badfaxrun)
444 badfaxrun = badrun;
445 badrun = 0;
446 _TIFFmemcpy(refbuf, rowbuf, linesize);
447 }
448 tifin->tif_row++;
449
450 if (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0)
451 {
452 fprintf(stderr, "%s: Write error at row %" PRIu32 ".\n",
453 tifout->tif_name, row);
454 break;
455 }
456 row++;
457 if (stretch)
458 {
459 if (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0)
460 {
461 fprintf(stderr, "%s: Write error at row %" PRIu32 ".\n",
462 tifout->tif_name, row);
463 break;
464 }
465 row++;
466 }
467 }
468 if (badrun > badfaxrun)
469 badfaxrun = badrun;
470 _TIFFfree(tifin->tif_rawdata);
471 return (row);
472 }
473
474 static const char usage_info[] =
475 "Create a TIFF Class F fax file from raw fax data\n\n"
476 "usage: fax2tiff [options] input.raw...\n"
477 "where options are:\n"
478 " -3 input data is G3-encoded [default]\n"
479 " -4 input data is G4-encoded\n"
480 " -U input data is uncompressed (G3 or G4)\n"
481 " -1 input data is 1D-encoded (G3 only) [default]\n"
482 " -2 input data is 2D-encoded (G3 only)\n"
483 " -P input is not EOL-aligned (G3 only) [default]\n"
484 " -A input is EOL-aligned (G3 only)\n"
485 " -M input data has MSB2LSB bit order\n"
486 " -L input data has LSB2MSB bit order [default]\n"
487 " -B input data has min 0 means black\n"
488 " -b input data has min 0 means black (same as -B)\n"
489 " -W input data has min 0 means white [default]\n"
490 " -w input data has min 0 means white (same as -W)\n"
491 " -R # input data has # resolution (lines/inch) [default is "
492 "196]\n"
493 " -X # input data has # width [default is "
494 "1728]\n"
495 "\n"
496 " -o out.tif write output to out.tif\n"
497 #ifdef CCITT_SUPPORT
498 " -7 generate G3-encoded output [default]\n"
499 " -8 generate G4-encoded output\n"
500 " -u generate uncompressed output (G3 or G4)\n"
501 " -5 generate 1D-encoded output (G3 only)\n"
502 " -6 generate 2D-encoded output (G3 only) [default]\n"
503 " -p generate not EOL-aligned output (G3 only)\n"
504 " -a generate EOL-aligned output (G3 only) [default]\n"
505 #endif
506 " -c generate \"classic\" TIFF format\n"
507 " -f generate TIFF Class F (TIFF/F) format [default]\n"
508 " -m output fill order is MSB2LSB\n"
509 " -l output fill order is LSB2MSB [default]\n"
510 " -r # make each strip have no more than # rows\n"
511 " -s stretch image by duplicating scanlines\n"
512 " -v print information about conversion work\n"
513 #ifdef LZW_SUPPORT
514 " -z generate LZW compressed output\n"
515 #endif
516 ;
517
518 static void usage(int code)
519 {
520 FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr;
521
522 fprintf(out, "%s\n\n", TIFFGetVersion());
523 fprintf(out, "%s", usage_info);
524 exit(code);
525 }