File: g3.c

package info (click to toggle)
xli 1.17.0+20061110-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,540 kB
  • sloc: ansic: 25,840; makefile: 11
file content (476 lines) | stat: -rw-r--r-- 10,792 bytes parent folder | download | duplicates (3)
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
/** g3.c - read a Group 3 FAX file and product a bitmap
 **
 ** Adapted from Paul Haeberli's <paul@manray.sgi.com> G3 to Portable Bitmap 
 ** code.
 **
 ** modified by jimf on 09.18.90 to fail on any load error.  this was done
 ** to cut down on the false positives caused by a lack of any read ID
 ** string.  the old errors are currently ifdef'ed out -- if you want 'em
 ** define ALLOW_G3_ERRORS.
 **/

/* Edit History

04/15/91   8 nazgul	Sanity check line widths
			Doing a zclose on all failures
04/14/91   1 schulert	add <sys/types> for SYSV systems
04/13/91   6 nazgul	Handle reinvocation on the same file
04/13/91   5 nazgul	Bug fix to retry with bitreversed, and do not double allocate on multiple calls
04/12/91   4 nazgul	Spot faxes that do not have a 000 header
			Handle faxes that have the bytes in the wrong order
07/03/90   2 nazgul	Added recovery for premature EOF
*/

#include "xli.h"
#include <sys/types.h>
#include <sys/file.h>
#include "g3.h"
#include "imagetypes.h"

/****
 **
 ** Local defines
 **
 ****/

#define BITS_TO_BYTES(bits)	(((bits)+7)/8)	/* Bytes to contain bits */
#define TABSIZE(tab) (sizeof(tab)/sizeof(struct tableentry))
#ifdef VMS
#define cols vmscols
#endif

/****
 **
 ** Local variables
 **
 ****/

static int g3_eof = 0;
static int g3_eols;
static int g3_rawzeros;
static int g3_Xrawzeros;
static int	maxlinelen;
static int	rows, cols;
static char *g3_error = NULL;
static int g3_verb;
static int curbit;
static int firstTime = 1;

#define MAX_ERRORS	20

/****
 **
 ** Local tables
 **
 ****/

tableentry *whash[HASHSIZE];
tableentry *bhash[HASHSIZE];

static int g3_skiptoeol(ZFILE *fd);
static int g3_rawgetbit(ZFILE *fd);


static int g3_addtohash(tableentry **hash, tableentry *te, int n, int a,
	int b)
{
	unsigned int pos;

	while (n--) {
		pos = ((te->length+a)*(te->code+b))%HASHSIZE;
		if (hash[pos] != 0) {
			g3_error = "G3: Hash collision during initialization.";
			return(-1);
			}
		hash[pos] = te;
		te++;
	}

	return 0;
}


static int g3_bitson(bit *b, int c, int n)
{
	int	i, col;
	bit	*bP;
	static int	bitmask[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };

	bP = b;
	col = c;
	bP+=(c/8);
	i = (c - ((c/8)*8));
	while(col <= (c+n)) { 
		for(;col <= (c+n) && i < 8; i++) {
			*bP |= bitmask[i];
			col++;
			}
		i = 0;
		bP++;
		}
	return(0);
}


static tableentry	*g3_hashfind(tableentry **hash, int length, int code, int a, int b)
{
	unsigned int pos;
	tableentry *te;

	pos = ((length+a)*(code+b))%HASHSIZE;
	if (pos >= HASHSIZE) {
		g3_error = "G3: Bad hash position";
		return(NULL);
		}
	te = hash[pos];
	return ((te && te->length == length && te->code == code) ? te : 0);
}


static int g3_getfaxrow(ZFILE *fd, byte *bitrow)
{
        int col;
	int curlen, curcode, nextbit;
	int count, color;
	tableentry *te;

	/* First make the whole row white... */
	bzero((char *) bitrow, maxlinelen); /* was memset -- jimf 09.11.90 */

	col = 0;
	g3_rawzeros = 0;
	curlen = 0;
	curcode = 0;
	color = 1;
	count = 0;
	while (!g3_eof) {
		if (col >= MAXCOLS) {
			g3_error = "G3: Input row is too long, skipping to EOL";
			g3_skiptoeol(fd);
			return (col); 
			}
		do {
			if (g3_eof) return 0;
			if (g3_rawzeros >= 11) {
				nextbit = g3_rawgetbit(fd);
				if (nextbit==1) {
					if ( col == 0 )
						/* 6 consecutive EOLs mean end of document */
						g3_eof = (++g3_eols >= 5);
					else
						g3_eols = 0;

					return (col); 
					}
				}
			else
				nextbit = g3_rawgetbit(fd);

			curcode = (curcode<<1) + nextbit; 
			curlen++;
			} while (curcode <= 0);

		/* No codewords are greater than 13 bytes */
		if (curlen > 13) {
			g3_error = "G3: Bad code word, skipping to EOL";
			g3_skiptoeol(fd);
			return (col);
			}
		if (color) {
			/* White codewords are at least 4 bits long */
			if (curlen < 4)
				continue;
			te = g3_hashfind(whash, curlen, curcode, WHASHA, WHASHB);
			}
		else {
			/* Black codewords are at least 2 bits long */
			if (curlen < 2)
				continue;
			te = g3_hashfind(bhash, curlen, curcode, BHASHA, BHASHB);
		}
		if (!te)
			continue;
		switch (te->tabid) {
			case TWTABLE:
			case TBTABLE:
				count += te->count;
				if (col+count > MAXCOLS) 
					count = MAXCOLS-col;
				if (count > 0) {
					if (color) {
						col += count;
						count = 0;
						}
					else
						g3_bitson(bitrow, col, count);
					}
				curcode = 0;
				curlen = 0;
				color = !color;
				break;
			case MWTABLE:
			case MBTABLE:
				count += te->count;
				curcode = 0;
				curlen = 0;
				break;
			case EXTABLE:
				count += te->count;
				curcode = 0;
				curlen = 0;
				break;
			default:
				g3_error = "G3: Bad table id from table entry";
				return(-1);
			}
		}
	return (0);
}


static int g3_skiptoeol(ZFILE *fd)
{
	int maxbits = 2 * MAXCOLS;
	while (g3_rawzeros<11 && !g3_eof && maxbits--)
		(void) g3_rawgetbit(fd);
	if(maxbits)
	{
		maxbits = MAXCOLS/2;
		while(!g3_rawgetbit(fd) && !g3_eof && maxbits--);
	}
	if (!maxbits)
		g3_error = "G3: unable to skip to eol";
	return(0);
}


static int g3_rawgetbit(ZFILE *fd)
{
	int	b;
	static int	shdata;

	if (curbit >= 8) {
		shdata = zgetc(fd);
		if (shdata == EOF) {
			g3_eols = 5;
			g3_eof = 1;
			g3_error = "G3: Premature EOF";
			return(0);
			}
		curbit = 0;
		}
	if (shdata & bmask[curbit]) {
		g3_Xrawzeros = g3_rawzeros;
		g3_rawzeros = 0;
		b = 1;
		}
	else {
		g3_rawzeros++;
		b = 0;
		}
	curbit++;
    return b;
}


/* All G3 images begin with a G3 EOL codeword which is eleven binary 0's
 * followed by one binary 1.  There could be up to 15 0' so that the image
 * starts on a char boundary.
 */
/*
 * They are all *supposed* to, but in fact some don't.  In fact pbmtog3 doesn't seem
 * to generate them.  So if that fails, we'll also try reading a line and seeing if
 * we get any errors.  Note that this means we had to move the call to g3_ident
 * to after the hash table init.  -nazgul
 */

/* Return TRUE if g3 image */
static boolean	g3_ident(ZFILE *fd)
{
    int		ret = FALSE, col1, col2, col3, i;
    byte	*tmpline = NULL;
    int		reverse = 0;
    
    g3_verb = 0;
    col1 = col2 = col3 = 0;
    tmpline = (byte *) lmalloc(maxlinelen);

    /* In case this got reset by a previous pass through here */
    for (i = 0; i < 8; ++i) {
	bmask[7-i] = 1 << i;
    }

tryagain:
    if (!zrewind(fd)) {
		lfree(tmpline);
		return FALSE;
    }
    curbit = 8;
    g3_Xrawzeros = g3_rawzeros = 0;
    g3_error = NULL;
    g3_eof = g3_eols = rows = cols = 0;
    
    /* If we have the zeros we're off to a good start, otherwise,
     * skip some lines
     */
    for (g3_rawzeros = 0; !g3_eof && !g3_rawgetbit(fd) && g3_rawzeros < 16;);
    if (g3_eof || g3_Xrawzeros < 11 || g3_Xrawzeros > 15) {
	if (g3_eof || !zrewind(fd)) {
	    lfree(tmpline);
	    return FALSE;
	}
	curbit = 8;
	g3_Xrawzeros = g3_rawzeros = 0;
	g3_error = NULL;
	g3_eof = g3_eols = rows = cols = 0;
	g3_skiptoeol(fd);
	if (!g3_error) g3_skiptoeol(fd);
	if (!g3_error) g3_skiptoeol(fd);
	if (!g3_error) g3_skiptoeol(fd);
    }

    /* Now get three lines and make sure they are the same length.  If not
     * give up.  Note that it is possible for this to give false positives
     * (value.o on a Sun IPC did) but it's unlikely enough that I think
     * we're okay.
     */
    if (!g3_error) col1 = g3_getfaxrow(fd, tmpline);
    if (col1 > 0 && !g3_error) col2 = g3_getfaxrow(fd, tmpline);
    if (col1 > 0 && !g3_error) col3 = g3_getfaxrow(fd, tmpline);
    if (!g3_error && col1 > 0 && col1 == col2 && col2 == col3) ret = TRUE;
	else ret = FALSE;
    /* if (ret) printf("%d = %d\n", col1, col2); */

    /* This bogus hack is to accomodate some fax modems which apparently
     * use a chip with a different byte order.  We simply try again with
     * the table reversed.
     */
    if (!ret && !reverse) {
	for (i = 0; i < 8; ++i) {
	    bmask[i] = 1 << i;
	}
	reverse = 1;
	goto tryagain;
    }
    lfree(tmpline);

    /* Reset file so we don't loose lines */
    if (!zrewind(fd)) {
	return FALSE;
    }
    curbit = 8;
    g3_Xrawzeros = g3_rawzeros = 0;
    g3_error = NULL;
    g3_eof = g3_eols = rows = cols = 0;
    
    return(ret);
}


Image	*g3Load(char *fullname, ImageOptions *image_ops, boolean verbose)
{
	ZFILE	*fd;
	char	*name = image_ops->name;
	Image	*image;
	int i, col;
	byte	*currline;

	if ((fd = zopen(fullname)) == NULL) {
		perror("g3Load");
		return(NULL);
	}

	if (firstTime) {
	    firstTime = 0;
	    
	    /* Initialize and load the hash tables */
	    for ( i = 0; i < HASHSIZE; ++i )
	      whash[i] = bhash[i] = (tableentry *) 0;
	    g3_addtohash(whash, twtable, TABSIZE(twtable), WHASHA, WHASHB);
	    g3_addtohash(whash, mwtable, TABSIZE(mwtable), WHASHA, WHASHB);
	    g3_addtohash(whash, extable, TABSIZE(extable), WHASHA, WHASHB);
	    g3_addtohash(bhash, tbtable, TABSIZE(tbtable), BHASHA, BHASHB);
	    g3_addtohash(bhash, mbtable, TABSIZE(mbtable), BHASHA, BHASHB);
	    g3_addtohash(bhash, extable, TABSIZE(extable), BHASHA, BHASHB);
	}
	
	/* Calulate the number of bytes needed for maximum number of columns 
	 * (bits), create a temprary storage area for it.
	 */
	maxlinelen = BITS_TO_BYTES(MAXCOLS);

	if (!g3_ident(fd)) {
	    zclose(fd);
	    return(NULL);
	}
	g3_verb = verbose;

	znocache(fd);
	image = newBitImage(MAXCOLS, MAXROWS);

	currline = image->data;
	cols = 0;
	for (rows = 0; rows < MAXROWS; ++rows) {
		col = g3_getfaxrow(fd, currline);
		if (col < 0) {
		  freeImage(image);
		  zclose(fd);
		  return(NULL);
		}
		if (g3_eof)
			break;
		if (col > cols)
			cols = col;
		currline += BITS_TO_BYTES(cols);
		}

	image->title= dupString(name);
	image->width = cols;
	image->height = rows;
	if (!image->width || !image->height) { /* sanity check */
		zclose(fd);
		freeImage(image);
		return(NULL);
	}

	if(verbose)
		printf("%s is a %dx%d G3 FAX image.\n", name, image->width, image->height);
	read_trail_opt(image_ops,fd,image,verbose);
	zclose(fd);
    return(image);
}


boolean	g3Ident(char *fullname, char *name)
{
	ZFILE	*fd;
	int i,retv;

	if ((fd = zopen(fullname)) == NULL) {
		perror("g3Ident");
		return(0);
	}

	/*
	 * Init hash table
	 */
	if (firstTime) {
	    firstTime = 0;
	    
	    /* Initialize and load the hash tables */
	    for ( i = 0; i < HASHSIZE; ++i )
	      whash[i] = bhash[i] = (tableentry *) 0;
	    g3_addtohash(whash, twtable, TABSIZE(twtable), WHASHA, WHASHB);
	    g3_addtohash(whash, mwtable, TABSIZE(mwtable), WHASHA, WHASHB);
	    g3_addtohash(whash, extable, TABSIZE(extable), WHASHA, WHASHB);
	    g3_addtohash(bhash, tbtable, TABSIZE(tbtable), BHASHA, BHASHB);
	    g3_addtohash(bhash, mbtable, TABSIZE(mbtable), BHASHA, BHASHB);
	    g3_addtohash(bhash, extable, TABSIZE(extable), BHASHA, BHASHB);
	}
	
	maxlinelen = BITS_TO_BYTES(MAXCOLS);
	if((retv = g3_ident(fd)))
		printf("%s is a G3 FAX image.\n", name);
	zclose(fd);
	return retv;
}