summaryrefslogtreecommitdiffstats
path: root/kfaxview/libkfaximage/faxexpand.cpp
blob: 9c4b40829b7df8cb78805e3516eab9e6fe7b1d43 (plain)
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/* Expand one page of fax data
   Copyright (C) 1990, 1995  Frank D. Cringle.

This file is part of viewfax - g3/g4 fax processing software.
     
viewfax is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
     
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
     
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>

#include <kdebug.h>

#include "faxexpand.h"

//Uncomment this for verbose debug output
//#define DEBUG_FAX
#define verbose false

pagenode::pagenode()
{
}

/* Note that NeedBits() only works for n <= 16 */
#define NeedBits(n) do {						\
    if (BitsAvail < (n)) {						\
	BitAcc |= *sp++ << BitsAvail;					\
	BitsAvail += 16;						\
    }									\
} while (0)
#define GetBits(n)	(BitAcc & ((1<<(n))-1))
#define ClrBits(n) do {							\
    BitAcc >>= (n);							\
    BitsAvail -= (n);							\
} while (0)

#ifdef DEBUG_FAX
#define DEBUG_SHOW putchar(BitAcc & (1 << t) ? '1' : '0')
#define LOOKUP(wid,tab)	do {						\
    int t;								\
    NeedBits(wid);							\
    TabEnt = tab + GetBits(wid);					\
    printf("%08lX/%d: %s%5d\t", BitAcc, BitsAvail,			\
	   StateNames[TabEnt->State], TabEnt->Param);			\
    for (t = 0; t < TabEnt->Width; t++)					\
	DEBUG_SHOW;							\
    putchar('\n');							\
    fflush(stdout);							\
    ClrBits(TabEnt->Width);						\
} while (0)

#define SETVAL(x) do {							\
    *pa++ = RunLength + (x);						\
    printf("SETVAL: %d\t%d\n", RunLength + (x), a0);			\
    a0 += x;								\
    RunLength = 0;							\
} while (0)

const char *StateNames[] = {
    "Null   ",
    "Pass   ",
    "Horiz  ",
    "V0     ",
    "VR     ",
    "VL     ",
    "Ext    ",
    "TermW  ",
    "TermB  ",
    "MakeUpW",
    "MakeUpB",
    "MakeUp ",
    "EOL    ",
};

#else
#define LOOKUP(wid,tab)	do {						\
    NeedBits(wid);							\
    TabEnt = tab + GetBits(wid);					\
    ClrBits(TabEnt->Width);						\
} while (0)

#define SETVAL(x) do {							\
    *pa++ = RunLength + (x);						\
    a0 += x;								\
    RunLength = 0;							\
} while (0)
#endif

#define dumpruns(runs) do {						\
    printf("-------------------- %d\n", LineNum);			\
    for (pa = runs, a0 = 0; a0 < lastx; a0 += *pa++)			\
	printf("%4d %d\n", a0, *pa);					\
} while (0)

#define EndOfData(pn)	(sp >= pn->data + pn->length/sizeof(*pn->data))

/* This macro handles coding errors in G3 data.
   We redefine it below for the G4 case */
#define SKIP_EOL do {							\
    while (!EndOfData(pn)) {						\
	NeedBits(11);							\
	if (GetBits(11) == 0)						\
	    break;							\
	ClrBits(1);							\
    }									\
    ClrBits(11);							\
    goto EOL;								\
} while (0)
#define eol2lab	EOL2:

/* the line expanders are written as macros so that they can be reused
   (twice each) but still have direct access to the local variables of
   the "calling" function */
#define expand1d() do {							\
    while (a0 < lastx) {						\
	int done = 0;							\
	while (!done) {		/* white first */			\
	    LOOKUP(12, WhiteTable);					\
	    switch (TabEnt->State) {					\
	    case S_EOL:							\
		EOLcnt = 1;						\
		goto EOL;						\
	    case S_TermW:						\
		SETVAL(TabEnt->Param);					\
		done = 1;						\
		break;							\
	    case S_MakeUpW:						\
	    case S_MakeUp:						\
		a0 += TabEnt->Param;					\
		RunLength += TabEnt->Param;				\
		break;							\
	    case S_Ext:							\
		unexpected("Extension code", LineNum);			\
		SKIP_EOL;						\
		break;							\
	    default:							\
		unexpected("WhiteTable", LineNum);			\
		SKIP_EOL;						\
		break;							\
	    }								\
	}								\
	done = a0 >= lastx;						\
	while (!done) {		/* then black */			\
	    LOOKUP(13, BlackTable);					\
	    switch (TabEnt->State) {					\
	    case S_EOL:							\
		EOLcnt = 1;						\
		goto EOL;						\
	    case S_TermB:						\
		SETVAL(TabEnt->Param);					\
		done = 1;						\
		break;							\
	    case S_MakeUpB:						\
	    case S_MakeUp:						\
		a0 += TabEnt->Param;					\
		RunLength += TabEnt->Param;				\
		break;							\
	    case S_Ext:							\
		unexpected("Extension code", LineNum);			\
		SKIP_EOL;						\
		break;							\
	    default:							\
		unexpected("BlackTable", LineNum);			\
		SKIP_EOL;						\
		break;							\
	    }								\
	}								\
    }									\
 EOL: ;									\
} while (0)

#define CHECK_b1 do {							\
    if (pa != thisrun) while (b1 <= a0 && b1 < lastx) {			\
	b1 += pb[0] + pb[1];						\
	pb += 2;							\
    }									\
} while (0)

#define expand2d(eolab) do {						\
    while (a0 < lastx) {						\
	LOOKUP(7, MainTable);						\
	switch (TabEnt->State) {					\
	case S_Pass:							\
	    CHECK_b1;							\
	    b1 += *pb++;						\
	    RunLength += b1 - a0;					\
	    a0 = b1;							\
	    b1 += *pb++;						\
	    break;							\
	case S_Horiz:							\
	    if ((pa-run0)&1) {						\
		int done = 0;						\
		while (!done) { /* black first */			\
		    LOOKUP(13, BlackTable);				\
		    switch (TabEnt->State) {				\
		    case S_TermB:					\
			SETVAL(TabEnt->Param);				\
			done = 1;					\
			break;						\
		    case S_MakeUpB:					\
		    case S_MakeUp:					\
			a0 += TabEnt->Param;				\
			RunLength += TabEnt->Param;			\
			break;						\
		    default:						\
			unexpected("BlackTable", LineNum);		\
			SKIP_EOL;					\
			break;						\
		    }							\
		}							\
		done = 0;						\
		while (!done) { /* then white */			\
		    LOOKUP(12, WhiteTable);				\
		    switch (TabEnt->State) {				\
		    case S_TermW:					\
			SETVAL(TabEnt->Param);				\
			done = 1;					\
			break;						\
		    case S_MakeUpW:					\
		    case S_MakeUp:					\
			a0 += TabEnt->Param;				\
			RunLength += TabEnt->Param;			\
			break;						\
		    default:						\
			unexpected("WhiteTable", LineNum);		\
			SKIP_EOL;					\
			break;						\
		    }							\
		}							\
	    }								\
	    else {							\
		int done = 0;						\
		while (!done) { /* white first */			\
		    LOOKUP(12, WhiteTable);				\
		    switch (TabEnt->State) {				\
		    case S_TermW:					\
			SETVAL(TabEnt->Param);				\
			done = 1;					\
			break;						\
		    case S_MakeUpW:					\
		    case S_MakeUp:					\
			a0 += TabEnt->Param;				\
			RunLength += TabEnt->Param;			\
			break;						\
		    default:						\
			unexpected("WhiteTable", LineNum);		\
			SKIP_EOL;					\
			break;						\
		    }							\
		}							\
		done = 0;						\
		while (!done) { /* then black */			\
		    LOOKUP(13, BlackTable);				\
		    switch (TabEnt->State) {				\
		    case S_TermB:					\
			SETVAL(TabEnt->Param);				\
			done = 1;					\
			break;						\
		    case S_MakeUpB:					\
		    case S_MakeUp:					\
			a0 += TabEnt->Param;				\
			RunLength += TabEnt->Param;			\
			break;						\
		    default:						\
			unexpected("BlackTable", LineNum);		\
			SKIP_EOL;					\
			break;						\
		    }							\
		}							\
	    }								\
	    CHECK_b1;							\
	    break;							\
	case S_V0:							\
	    CHECK_b1;							\
	    SETVAL(b1 - a0);						\
	    b1 += *pb++;						\
	    break;							\
	case S_VR:							\
	    CHECK_b1;							\
	    SETVAL(b1 - a0 + TabEnt->Param);				\
	    b1 += *pb++;						\
	    break;							\
	case S_VL:							\
	    CHECK_b1;							\
	    SETVAL(b1 - a0 - TabEnt->Param);				\
	    b1 -= *--pb;						\
	    break;							\
	case S_Ext:							\
	    *pa++ = lastx - a0;						\
	    if (verbose)						\
		kdDebug() << "Line " << LineNum << ": extension code\n";\
	    SKIP_EOL;							\
	    break;							\
	case S_EOL:							\
	    *pa++ = lastx - a0;						\
	    NeedBits(4);						\
	    if (GetBits(4) && verbose) /* already seen 7 zeros */	\
		kdDebug() << "Line " << LineNum << ": Bad EOL\n";	\
	    ClrBits(4);							\
	    EOLcnt = 1;							\
	    goto eolab;							\
	    break;							\
	default:							\
	    unexpected("MainTable", LineNum);				\
	    SKIP_EOL;							\
	    break;							\
	}								\
    }									\
    if (RunLength) {							\
	if (RunLength + a0 < lastx) {					\
	    /* expect a final V0 */					\
	    NeedBits(1);						\
	    if (!GetBits(1)) {						\
		unexpected("MainTable", LineNum);			\
		SKIP_EOL;						\
	    }								\
	    ClrBits(1);							\
	}								\
	SETVAL(0);							\
    }									\
 eol2lab ;								\
} while (0)

static void
unexpected(const char *what, int LineNum)
{
    if (verbose)
	kdError() << "Line " << LineNum << ": Unexpected state in "
		<< what << endl;
}

/* Expand tiff modified huffman data (g3-1d without EOLs) */
void
MHexpand(struct pagenode *pn, drawfunc df)
{
    int a0;			/* reference element */
    int lastx;			/* copy line width to register */
    t32bits BitAcc;		/* bit accumulator */
    int BitsAvail;		/* # valid bits in BitAcc */
    int RunLength;		/* Length of current run */
    t16bits *sp;		/* pointer into compressed data */
    pixnum *pa;			/* pointer into new line */
    int EOLcnt;			/* number of consecutive EOLs */
    int	LineNum;		/* line number */
    pixnum *runs;		/* list of run lengths */
    struct tabent *TabEnt;

    sp = pn->data;
    BitAcc = 0;
    BitsAvail = 0;
    lastx = pn->size.width();
    runs = (pixnum *) malloc(lastx * sizeof(pixnum));
    for (LineNum = 0; LineNum < pn->rowsperstrip; ) {
#ifdef DEBUG_FAX
	printf("\nBitAcc=%08lX, BitsAvail = %d\n", BitAcc, BitsAvail);
	printf("-------------------- %d\n", LineNum);
	fflush(stdout);
#endif
	RunLength = 0;
	pa = runs;
	a0 = 0;
	EOLcnt = 0;
	if (BitsAvail & 7)	/* skip to byte boundary */
	    ClrBits(BitsAvail & 7);
	expand1d();
	if (RunLength)
	    SETVAL(0);
	if (a0 != lastx) {
	    if (verbose)
		kdWarning() << "Line " << LineNum << ": length is "
			<< a0 << " (expected "<< lastx << ")\n";
	    while (a0 > lastx)
		a0 -= *--pa;
	    if (a0 < lastx) {
		if ((pa - runs) & 1)
		    SETVAL(0);
		SETVAL(lastx - a0);
	    }
	}
	(*df)(runs, LineNum++, pn);
    }
    free(runs);
}

/* Expand group-3 1-dimensional data */
void
g31expand(struct pagenode *pn, drawfunc df)
{
    int a0;			/* reference element */
    int lastx;			/* copy line width to register */
    t32bits BitAcc;		/* bit accumulator */
    int BitsAvail;		/* # valid bits in BitAcc */
    int RunLength;		/* Length of current run */
    t16bits *sp;		/* pointer into compressed data */
    pixnum *pa;			/* pointer into new line */
    int EOLcnt;			/* number of consecutive EOLs */
    int	LineNum;		/* line number */
    pixnum *runs;		/* list of run lengths */
    struct tabent *TabEnt;

    sp = pn->data;
    BitAcc = 0;
    BitsAvail = 0;
    lastx = pn->size.width();
    runs = (pixnum *) malloc(lastx * sizeof(pixnum));
    EOLcnt = 0;
    for (LineNum = 0; LineNum < pn->rowsperstrip; ) {
#ifdef DEBUG_FAX
	printf("\nBitAcc=%08lX, BitsAvail = %d\n", BitAcc, BitsAvail);
	printf("-------------------- %d\n", LineNum);
	fflush(stdout);
#endif
	if (EOLcnt == 0)
	    while (!EndOfData(pn)) {
		/* skip over garbage after a coding error */
		NeedBits(11);
		if (GetBits(11) == 0)
		    break;
		ClrBits(1);
	    }
	for (EOLcnt = 1; !EndOfData(pn); EOLcnt++) {
	    /* we have seen 11 zeros, which implies EOL,
	       skip possible fill bits too */
	    while (1) {
		NeedBits(8);
		if (GetBits(8))
		    break;
		ClrBits(8);
	    }
	    while (GetBits(1) == 0)
		ClrBits(1);
	    ClrBits(1);		/* the eol flag */
	    NeedBits(11);
	    if (GetBits(11))
		break;
	    ClrBits(11);
	}
	if (EOLcnt > 1 && EOLcnt != 6 && verbose)
	    kdError() << "Line " << LineNum << ": bad RTC (" << EOLcnt << " EOLs)\n";
	if (EOLcnt >= 6 || EndOfData(pn)) {
	    free(runs);
	    return;
	}
	RunLength = 0;
	pa = runs;
	a0 = 0;
	EOLcnt = 0;
	expand1d();
	if (RunLength)
	    SETVAL(0);
	if (a0 != lastx) {
	    if (verbose)
		kdWarning() << "Line " << LineNum << ": length is "
			<< a0 << " (expected "<< lastx << ")\n";
	    while (a0 > lastx)
		a0 -= *--pa;
	    if (a0 < lastx) {
		if ((pa - runs) & 1)
		    SETVAL(0);
		SETVAL(lastx - a0);
	    }
	}
	(*df)(runs, LineNum++, pn);
    }
    free(runs);
}

/* Expand group-3 2-dimensional data */
void
g32expand(struct pagenode *pn, drawfunc df)
{
    int RunLength;		/* Length of current run */
    int a0;			/* reference element */
    int b1;			/* next change on previous line */
    int lastx = pn->size.width();/* copy line width to register */
    pixnum *run0, *run1;	/* run length arrays */
    pixnum *thisrun, *pa, *pb;	/* pointers into runs */
    t16bits *sp;		/* pointer into compressed data */
    t32bits BitAcc;		/* bit accumulator */
    int BitsAvail;		/* # valid bits in BitAcc */
    int EOLcnt;			/* number of consecutive EOLs */
    int	refline = 0;		/* 1D encoded reference line */
    int	LineNum;		/* line number */
    struct tabent *TabEnt;

    sp = pn->data;
    BitAcc = 0;
    BitsAvail = 0;
    /* allocate space for 2 runlength arrays */
    run0 = (pixnum *) malloc(2 * ((lastx+5)&~1) * sizeof(pixnum));
    run1 = run0 + ((lastx+5)&~1);
    run1[0] = lastx;
    run1[1] = 0;
    EOLcnt = 0;
    for (LineNum = 0; LineNum < pn->rowsperstrip; ) {
#ifdef DEBUG_FAX
	printf("\nBitAcc=%08lX, BitsAvail = %d\n", BitAcc, BitsAvail);
	printf("-------------------- %d\n", LineNum);
	fflush(stdout);
#endif
	if (EOLcnt == 0)
	    while (!EndOfData(pn)) {
		/* skip over garbage after a coding error */
		NeedBits(11);
		if (GetBits(11) == 0)
		    break;
		ClrBits(1);
	    }
	for (EOLcnt = 1; !EndOfData(pn); EOLcnt++) {
	    /* we have seen 11 zeros, which implies EOL,
	       skip possible fill bits too */
	    while (1) {
		NeedBits(8);
		if (GetBits(8))
		    break;
		ClrBits(8);
	    }
	    while (GetBits(1) == 0)
		ClrBits(1);
	    ClrBits(1);		/* the eol flag */
	    NeedBits(12);
	    refline = GetBits(1); /* 1D / 2D flag */
	    ClrBits(1);
	    if (GetBits(11))
		break;
	    ClrBits(11);
	}
	if (EOLcnt > 1 && EOLcnt != 6 && verbose)
	    kdError() << "Line " << LineNum << ": bad RTC (" << EOLcnt << " EOLs)\n";
	if (EOLcnt >= 6 || EndOfData(pn)) {
	    free(run0);
	    return;
	}
	if (LineNum == 0 && refline == 0 && verbose)
	    kdDebug() << "First line is 2-D encoded\n";
	RunLength = 0;
	if (LineNum & 1) {
	    pa = run1;
	    pb = run0;
	}
	else {
	    pa = run0;
	    pb = run1;
	}
	thisrun = pa;
	EOLcnt = 0;
	a0 = 0;
	b1 = *pb++;

	if (refline) {
	    expand1d();
	}
	else {
	    expand2d(EOL2);
	}
	if (RunLength)
	    SETVAL(0);
	if (a0 != lastx) {
	    if (verbose)
		kdWarning() << "Line " << LineNum << ": length is "
			<< a0 << " (expected "<< lastx << ")\n";
	    while (a0 > lastx)
		a0 -= *--pa;
	    if (a0 < lastx) {
		if ((pa - run0) & 1)
		    SETVAL(0);
		SETVAL(lastx - a0);
	    }
	}
	SETVAL(0);	/* imaginary change at end of line for reference */
	(*df)(thisrun, LineNum++, pn);
    }
    free(run0);
}

/* Redefine the "skip to eol" macro.  We cannot recover from coding
   errors in G4 data */
#undef SKIP_EOL
#undef eol2lab
#define SKIP_EOL do {							\
    if (verbose)							\
	kdError() << "Line " << LineNum << ": G4 coding error\n";       \
    free(run0);								\
    return;								\
} while (0)
#define eol2lab

/* Expand group-4 data */
void
g4expand(struct pagenode *pn, drawfunc df)
{
    int RunLength;		/* Length of current run */
    int a0;			/* reference element */
    int b1;			/* next change on previous line */
    int lastx = pn->size.width();/* copy line width to register */
    pixnum *run0, *run1;	/* run length arrays */
    pixnum *thisrun, *pa, *pb;	/* pointers into runs */
    t16bits *sp;		/* pointer into compressed data */
    t32bits BitAcc;		/* bit accumulator */
    int BitsAvail;		/* # valid bits in BitAcc */
    int	LineNum;		/* line number */
    int EOLcnt;
    struct tabent *TabEnt;

    sp = pn->data;
    BitAcc = 0;
    BitsAvail = 0;
    /* allocate space for 2 runlength arrays */
    run0 = (pixnum *) malloc(2 * ((lastx+5)&~1) * sizeof(pixnum));
    run1 = run0 + ((lastx+5)&~1);
    run1[0] = lastx;		/* initial reference line */
    run1[1] = 0;

    for (LineNum = 0; LineNum < pn->rowsperstrip; ) {
#ifdef DEBUG_FAX
	printf("\nBitAcc=%08lX, BitsAvail = %d\n", BitAcc, BitsAvail);
	printf("-------------------- %d\n", LineNum);
	fflush(stdout);
#endif
	RunLength = 0;
	if (LineNum & 1) {
	    pa = run1;
	    pb = run0;
	}
	else {
	    pa = run0;
	    pb = run1;
	}
	thisrun = pa;
	a0 = 0;
	b1 = *pb++;
	expand2d(EOFB);
	if (a0 < lastx) {
	    if ((pa - run0) & 1)
		SETVAL(0);
	    SETVAL(lastx - a0);
	}
	SETVAL(0);	/* imaginary change at end of line for reference */
	(*df)(thisrun, LineNum++, pn);
	continue;
    EOFB:
	NeedBits(13);
	if (GetBits(13) != 0x1001 && verbose)
	    kdError() << "Bad RTC\n";
	break;
    }
    free(run0);
}

static const unsigned char zerotab[256] = {
	0x88, 0x07, 0x16, 0x06, 0x25, 0x05, 0x15, 0x05,
	0x34, 0x04, 0x14, 0x04, 0x24, 0x04, 0x14, 0x04,
	0x43, 0x03, 0x13, 0x03, 0x23, 0x03, 0x13, 0x03,
	0x33, 0x03, 0x13, 0x03, 0x23, 0x03, 0x13, 0x03,
	0x52, 0x02, 0x12, 0x02, 0x22, 0x02, 0x12, 0x02,
	0x32, 0x02, 0x12, 0x02, 0x22, 0x02, 0x12, 0x02,
	0x42, 0x02, 0x12, 0x02, 0x22, 0x02, 0x12, 0x02,
	0x32, 0x02, 0x12, 0x02, 0x22, 0x02, 0x12, 0x02,
	0x61, 0x01, 0x11, 0x01, 0x21, 0x01, 0x11, 0x01,
	0x31, 0x01, 0x11, 0x01, 0x21, 0x01, 0x11, 0x01,
	0x41, 0x01, 0x11, 0x01, 0x21, 0x01, 0x11, 0x01,
	0x31, 0x01, 0x11, 0x01, 0x21, 0x01, 0x11, 0x01,
	0x51, 0x01, 0x11, 0x01, 0x21, 0x01, 0x11, 0x01,
	0x31, 0x01, 0x11, 0x01, 0x21, 0x01, 0x11, 0x01,
	0x41, 0x01, 0x11, 0x01, 0x21, 0x01, 0x11, 0x01,
	0x31, 0x01, 0x11, 0x01, 0x21, 0x01, 0x11, 0x01,
	0x70, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x30, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x40, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x30, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x50, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x30, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x40, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x30, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x60, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x30, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x40, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x30, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x50, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x30, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x40, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00,
	0x30, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x00
};

#define check(v) do {							\
    prezeros = zerotab[v];						\
    postzeros = prezeros & 15;						\
    prezeros >>= 4;							\
    if (prezeros == 8) {						\
	zeros += 8;							\
	continue;							\
    }									\
    if (zeros + prezeros < 11) {					\
	empty = 0;							\
	zeros = postzeros;						\
	continue;							\
    }									\
    zeros = postzeros;							\
    if (empty)								\
	EOLcnt++;							\
    lines++;								\
    empty = 1;								\
} while (0)

/* count fax lines */
int
G3count(struct pagenode *pn, int twoD)
{
    t16bits *p = pn->data;
    t16bits *end = p + pn->length/sizeof(*p);
    int lines = 0;		/* lines seen so far */
    int zeros = 0;		/* number of consecutive zero bits seen */
    int EOLcnt = 0;		/* number of consecutive EOLs seen */
    int empty = 1;		/* empty line */
    int prezeros, postzeros;

    while (p < end && EOLcnt < 6) {
	t16bits bits = *p++;
	check(bits&255);
	if (twoD && (prezeros + postzeros == 7)) {
	    if (postzeros || ((bits & 0x100) == 0))
		zeros--;
	}
	check(bits>>8);
	if (twoD && (prezeros + postzeros == 7)) {
	    if (postzeros || ((p < end) && ((*p & 1) == 0)))
		zeros--;
	}
    }
    return lines - EOLcnt;	/* don't count trailing EOLs */
}