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