File: imgtofax.c

package info (click to toggle)
hylafax 3:6.0.6-8.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 9,508 kB
  • sloc: sh: 15,371; ansic: 13,242; makefile: 1,545; cpp: 781; awk: 529
file content (379 lines) | stat: -rw-r--r-- 9,970 bytes parent folder | download | duplicates (5)
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
/*	$Id$ */
/*
 * Copyright (c) 1990-1996 Sam Leffler
 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
 * HylaFAX is a trademark of Silicon Graphics
 *
 * 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.
 */
/*
 * Prepare SGI image files for transmission as facsimile.
 * Each image is scaled to fit a standard page, sharpened
 * with a hipass filter, gamma warp'd, and then dithered
 * to a bilevel image.
 *
 * Output is a single TIFF Class F file with each image as
 * a separate page/directory.
 *
 * Derived from Paul Haeberli's imgtofax program.
 */
#include <stdio.h>
#include <math.h>
#include <image.h>
#include "hipass.h"
#include "izoom.h"
#include "lut.h"
#include "lum.h"
#include "tiffio.h"
#include <stdarg.h>
#include "PageSize.h"

#define GAMMAVAL        0.8

#define	DEF_MARGIN	14
#define	DEF_TOPMARGIN	0
#define	DEF_BOTMARGIN	0

static	short sbuf[4096];
static	IMAGE* iimage;
static	highpass *hp;
static	zoom* zm;
static	TIFF* tif;
static	lut* lookup;
static	int npages;

static	int pixelWidth;
static	int pixelHeight;
static	int leftMargin		= DEF_MARGIN;
static	int rightMargin		= DEF_MARGIN;
static	int topMargin		= DEF_TOPMARGIN;
static	int bottomMargin	= DEF_BOTMARGIN;
static	int pageWidth;
static	int pageHeight;
static	float pageres	= 196.;		/* default is medium res */
static	long g3opts	= GROUP3OPT_FILLBITS;

static void
fatal(char* va_alist, ...)
#define	fmt va_alist
{
    va_list ap;
    va_start(ap, fmt);
    fprintf(stderr, "sgi2fax: ");
    vfprintf(stderr, fmt, ap);
    fprintf(stderr, ".\n");
    va_end(ap);
    exit(-1);
}
#undef fmt

static void
getimgrow(buf,y)
short *buf;
int y;
{
    getbwrow(iimage,buf,iimage->ysize-1-y);
}

static void
getzrow(buf,y)
short *buf;
int y;
{
    getzoomrow(zm,buf,y);
}

static void
tofax(short* wp, int n)
{
#define MAXXSIZE	2432
    unsigned char row[(MAXXSIZE+7) &~ 7], *rp = row;
    int bit = 0x80;

    memset(row, 0, sizeof (row));
    while (n-- > 0) {
	if (*wp++ < 128)
	    *rp |= bit;
	if ((bit >>= 1) == 0) {
	    rp++;
	    bit = 0x80;
	}
    }
    (void) TIFFWriteScanline(tif, row,
	TIFFCurrentRow(tif) == (uint32) -1 ? 0 : TIFFCurrentRow(tif), 0);
}

static float transfunc(f)
float f;
{
    return pow(f,GAMMAVAL);
}

/* 
 *      dithering stuff follows
 *
 */
#define MATSIZE88

#define XSIZE   8
#define YSIZE   8

#ifdef notdef
static short Xdithmat[YSIZE][XSIZE] = {         /* 8x8 floyd-steinberg */
        0,      8,      36,     44,     2,      10,     38,     46,
        16,     24,     52,     60,     18,     26,     54,     62,
        32,     40,     4,      12,     34,     42,     6,      14,
        48,     56,     20,     28,     50,     58,     22,     30,
        3,      11,     39,     47,     1,      9,      37,     45,
        19,     27,     55,     63,     17,     25,     53,     61,
        35,     43,     7,      15,     33,     41,     5,      13,
        51,     59,     23,     31,     49,     57,     21,     29,
};
#endif
static short dithmat[YSIZE][XSIZE] = {          /* halftone dots */
        3,      17,     55,     63,     61,     47,     9,      1,
        15,     29,     39,     51,     49,     35,     25,     13,
        40,     32,     26,     20,     22,     30,     36,     42,
        56,     44,     10,     4,      6,      18,     52,     58,
        60,     46,     8,      0,      2,      16,     54,     62,
        48,     34,     24,     12,     14,     28,     38,     50,
        23,     31,     37,     43,     41,     33,     27,     21,
        7,      19,     53,     59,     57,     45,     11,     5,
};

#define TOTAL           (XSIZE*YSIZE)

static ditherrow(buf,y,n)
short *buf;
int y, n;
{
    int r, val;
    int rshades, rmaxbits;
    short *rdith;

    rdith = &dithmat[y%YSIZE][0];
    rshades = TOTAL+1;
    rmaxbits = ((rshades-1)/TOTAL);
    while(n--) {
        r = *buf;
        val = (rshades*r)/255;
        if(val>=TOTAL) 
            *buf++ = 255;
        else if(val>rdith[n%XSIZE])
            *buf++ = 255;
        else
            *buf++ = 0;
    }
}

static void
blankSpace(int nrows)
{
    setrow(sbuf,255,pixelWidth);
    while (nrows-- > 0)
        tofax(sbuf,pixelWidth);
}

static void
imgtofax(char* input, int pn)
{
    int ixsize, iysize;
    int oxsize, oysize;
    int ymargin;
    int y;

    TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (long) pixelWidth);
    TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (long) pixelHeight);
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, -1L);
    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 1);
    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
    TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX3);
    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
    TIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_LSB2MSB);
    TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
    TIFFSetField(tif, TIFFTAG_XRESOLUTION, 204.);
    TIFFSetField(tif, TIFFTAG_YRESOLUTION, pageres);
    TIFFSetField(tif, TIFFTAG_PAGENUMBER, pn, npages);
    TIFFSetField(tif, TIFFTAG_CLEANFAXDATA, CLEANFAXDATA_CLEAN);
    { char buf[1024];
      snprintf(buf, sizeof(buf) - 1, "Ditherered B&W version of %s", input);
      TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, buf);
    }
    TIFFSetField(tif, TIFFTAG_SOFTWARE, "sgi2fax");
    TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
    TIFFSetField(tif, TIFFTAG_GROUP3OPTIONS, g3opts);

/* calculate the zoom factor */
    ixsize = iimage->xsize;
    iysize = iimage->ysize;
    oxsize = pageWidth;
    oysize = (iysize*pageWidth)/ixsize;		/* maintain aspect ratio */
    if (pageres == 98.)
	oysize /= 2;

/* set up the filters */
    zm = newzoom(getimgrow, ixsize, iysize, oxsize, oysize, TRIANGLE, 1.0);
    hp = newhp(getzrow, oxsize, oysize, 2.0);

    ymargin = (pageHeight-oysize)/2;
    if(ymargin<0)
        ymargin = 0;
    blankSpace(topMargin + ymargin);
    for(y=0; y<oysize; y++) {
        hpgetrow(hp, sbuf, y); 
        applylut(lookup, sbuf, oxsize);
        ditherrow(sbuf, y, oxsize);
        tofax(sbuf, pixelWidth);
    }
    blankSpace(bottomMargin + ymargin);

    freezoom(zm);
    freehp(hp);

    TIFFWriteDirectory(tif);
}

static void
usage()
{
    fprintf(stderr, "usage: sgi2fax %s %s %s %s %s %s %s [-12] file ...\n",
	"[-h height]",
	"[-w width]",
	"[-v vres]",
	"[-o output]",
	"[-r %red]",
	"[-g %green]",
	"[-b %blue]",
	""
    );
    exit(-1);
}

#define	CVT(x)	(((x)*255)/100)

main(argc, argv)
    int argc;
    char **argv;
{
    extern int optind;
    extern char* optarg;
    char* output = "sgi.fax";
    int c;
    float w, h;
    struct pageSizeInfo* info;

    info = getPageSize("default");
    w = getPageWidth(info);
    h = getPageHeight(info);
    delPageSize(info);
    while ((c = Sys::getopt(argc, argv, "o:r:g:b:h:s:v:w:12")) != -1)
	switch (c) {
	case '1':
	    g3opts &= ~GROUP3OPT_2DENCODING;
	    break;
	case '2':
	    g3opts |= GROUP3OPT_2DENCODING;
	    break;
	case 'r':			/* %red illumination */
	    _RILUM = CVT(atoi(optarg));
	    break;
	case 'g':			/* %green illumination */
	    _GILUM = CVT(atoi(optarg));
	    break;
	case 'b':			/* %blue illumination */
	    _BILUM = CVT(atoi(optarg));
	    break;
	case 'o':			/* output file */
	    output = optarg;
	    break;
	case 's':			/* page size */
	    info = getPageSize(optarg);
	    if (!info) {
		fprintf(stderr, "%s: Unknown page size \"%s\".\n",
		    argv[0], optarg);
		exit(-1);
	    }
	    w = getPageWidth(info);
	    h = getPageHeight(info);
	    delPageSize(info);
	    break;
	case 'v':			/* vertical resolution (lines/inch) */
	    pageres = atof(optarg);
	    /* XXX force acceptable resolutions */
	    if (pageres < 120.)
		pageres = 98.;
	    else
		pageres = 196.;
	    break;
	case 'w':			/* page width (mm) */
	    w = atof(optarg);
	    break;
	case 'h':			/* page height (mm) */
	    h = atof(optarg);
	    break;
	case '?':
	    usage();
	}
    if (argc - optind < 1)
	usage();

    /* XXX force known sizes */
    if (w > 280)
	pixelWidth = 2432;
    else if (w > 230)
	pixelWidth = 2048;
    else
	pixelWidth = 1728;
    if (h > 350)
	pixelHeight = 2810;
    else if (h > 280)
	pixelHeight = 2292;
    else
	pixelHeight = 2166;

    if (pageres == 98.) {
	pixelHeight /= 2;
	topMargin /= 2;
	bottomMargin /= 2;
    }
    pageWidth = pixelWidth - (leftMargin + rightMargin);
    pageHeight = pixelHeight - (topMargin + bottomMargin);

/* open the output file */
    tif = TIFFOpen(output, "w");
    if (!tif)
	fatal("%s: Can not create output file", output);

    lookup = makelut(transfunc,256,256,0);

    npages = argc - optind;
    for (c = 0; optind < argc; c++, optind++) {
	iimage = iopen(argv[optind], "r");
	if (!iimage) {
	    fprintf(stderr, "sgi2fax: %s: Can not open\n", argv[optind]);
	    continue;
	}
	imgtofax(argv[optind], c);
    }
    TIFFClose(tif);
    freelut(lookup);
    exit(0);
}