summaryrefslogtreecommitdiff
path: root/src/image/mkg3states.c
blob: c9c4b7fc73eea83818aed9ba39fda39c12f37102 (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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/* $Xorg: mkg3states.c,v 1.3 2000/08/17 19:46:41 cpqbld Exp $ */
/*
 * Copyright (c) 1991, 1992 Sam Leffler
 * Copyright (c) 1991, 1992 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.
 */
/* $XFree86: xc/lib/lbxutil/image/mkg3states.c,v 1.4 2001/01/17 19:43:35 dawes Exp $ */

/*
 * Program to construct Group 3 & Group 4 decoding tables.
 *
 * This code is derived from code by Michael P. Marking.  In
 * particular, the algorithms to generate the null_mode and
 * horiz_mode state tables are his.  See the comments below
 * for more information.
 *
 * BEGIN (from the original source)
 LEGAL
 *	Copyright 1989, 1990 Michael P. Marking, Post Office Box 8039,
 *	Scottsdale, Arizona 85252-8039. All rights reserved.
 *
 *	License is granted by the copyright holder to distribute and use this
 *	code without payment of royalties or the necessity of notification as
 *	long as this notice (all the text under "LEGAL") is included.
 *
 *	This program is offered without any warranty of any kind. It includes
 *	no warranty of merchantability or fitness for any purpose. Testing and
 *	suitability for any use are the sole responsibility of the user.
 *
 INFORMATION
 *	Although there is no support offered with this program, the author will
 *	endeavor to correct errors. Updates will also be made available from
 *	time to time.
 *
 *	Contact: Michael P. Marking, Post Office Box 8039, Scottsdale, Arizona
 *	85252-8039 USA. Replies are not guaranteed to be swift. Beginning
 *	July 1990, e-mail may be sent to uunet!ipel!marking.
 *
 *	Also beginning in July 1990, this code will be archived at the
 *	ipel!phoenix BBS in file g3g4.zoo. The 24-hour telephone number
 *	for 300/1200/2400 is (602)274-0462. When logging in, specify user
 *	"public", system "bbs", and password "public".
 *
 *	This code is also available from the C Users Group in volume 317.
 *
 * END (from the original source)
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifndef TRUE
#define	TRUE	1
#define	FALSE	0
#endif

#define WHITE	0
#define BLACK	1

/*
 * G3 2D and G4 decoding modes.  Note that
 * the vertical modes are ordered so that
 * (mode - MODE_VERT_V0) gives the vertical
 * adjustment for the b1 parameter.
 */
#define MODE_NULL	0
#define MODE_PASS	1
#define MODE_HORIZ	2
#define MODE_VERT_VL3	3
#define MODE_VERT_VL2	4
#define MODE_VERT_VL1	5
#define MODE_VERT_V0	6
#define MODE_VERT_VR1	7
#define MODE_VERT_VR2	8
#define MODE_VERT_VR3	9
#define MODE_UNCOMP	10
#define MODE_ERROR	11
#define MODE_ERROR_1	12

unsigned long append_0 ( unsigned long prefix );
unsigned long append_1 ( unsigned long prefix );

unsigned long append_0 (unsigned long prefix)
{
    return (prefix + (1L<<16));
}

unsigned long append_1 (unsigned long prefix)
{
    static unsigned short prefix_bit[16] = {
	0x8000, 0x4000, 0x2000, 0x1000,
	0x0800, 0x0400, 0x0200, 0x0100,
	0x0080, 0x0040, 0x0020, 0x0010,
	0x0008, 0x0004, 0x0002, 0x0001
    };
    unsigned char len = (prefix >> 16) & 0xf;
    return (append_0(prefix) + prefix_bit[len]);
}

#define	G3CODES
#include "lbxfax.h"
#include "lbxbwcodes.h"

short search_table ( unsigned long prefix, tableentry *tab, int n );
short white_run_length ( unsigned long prefix );
short black_run_length ( unsigned long prefix );
short horiz_mode_code_black ( short runlen );
short horiz_mode_code_white ( short runlen );
void write_define ( FILE *fd, char *name, int value, char *comment );
void write_preamble ( FILE *fd );
void extern_table ( FILE *fd, char *name );
void write_tables ( FILE *fd );
short find_null_mode_prefix ( long prefix );
short find_horiz_mode_prefix ( long prefix, int color );
short find_uncomp_mode_prefix ( long prefix );
short null_mode_type ( long prefix );
short uncomp_mode_type ( long prefix );
void build_null_mode_tables ( void );
void build_horiz_mode_tables ( void );
void build_uncomp_mode_tables ( void );

short search_table (unsigned long prefix, tableentry *tab, int n)
{
    unsigned short len = (prefix >> 16) & 0xf;
    unsigned short code = (prefix & 0xffff) >> (16 - len);

    while (n-- > 0) {
	if (tab->length == len && tab->code == code)
	    return ((short) tab->runlen);
	tab++;
    }
    return (G3CODE_INCOMP);
}

#define	NCODES(a)	(sizeof (a) / sizeof (a[0]))
short white_run_length (unsigned long prefix)
{
    return (search_table(prefix, TIFFFaxWhiteCodes, NCODES(TIFFFaxWhiteCodes)));
}

short black_run_length (unsigned long prefix)
{
    return (search_table(prefix, TIFFFaxBlackCodes, NCODES(TIFFFaxBlackCodes)));
}
#undef NCODES

#define MAX_NULLPREFIX	200	/* max # of null-mode prefixes */
typedef	unsigned char NullModeTable[MAX_NULLPREFIX][256];
#define MAX_HORIZPREFIX	250	/* max # of incomplete 1-D prefixes */
typedef	unsigned char HorizModeTable[MAX_HORIZPREFIX][256];

  /* the bit string corresponding to this row of the decoding table */
long	null_mode_prefix[MAX_NULLPREFIX];
NullModeTable null_mode;		/* MODE_*, indexed by bit and byte */
NullModeTable null_mode_next_state;	/* next row of decoding tables to use */
  /* number of prefixes or rows in the G4 decoding tables */
short	null_mode_prefix_count = 0;

void write_null_mode_table ( FILE *fd, NullModeTable table, char *name );
void write_horiz_mode_table ( FILE *fd, HorizModeTable table, char *name );

/*
 * 2D uncompressed mode codes.  Note
 * that two groups of codes are arranged
 * so that the decoder can caluclate the
 * length of the run by subtracting the
 * code from a known base value.
 */
#define	UNCOMP_INCOMP	0
/* runs of [0]*1 */
#define	UNCOMP_RUN0	1
#define	UNCOMP_RUN1	2
#define	UNCOMP_RUN2	3
#define	UNCOMP_RUN3	4
#define	UNCOMP_RUN4	5
#define	UNCOMP_RUN5	6
#define	UNCOMP_RUN6	7
/* runs of [0]* w/ terminating color */
#define	UNCOMP_TRUN0	8
#define	UNCOMP_TRUN1	9
#define	UNCOMP_TRUN2	10
#define	UNCOMP_TRUN3	11
#define	UNCOMP_TRUN4	12
/* special code for unexpected EOF */
#define	UNCOMP_EOF	13
/* invalid code encountered */
#define	UNCOMP_INVALID	14

long	uncomp_mode_prefix[MAX_NULLPREFIX];
NullModeTable uncomp_mode;
NullModeTable uncomp_mode_next_state;
short	uncomp_mode_prefix_count = 0;

/*
 * Decoding action values for horiz_mode.
 */
#define ACT_INCOMP	0		/* incompletely decoded code */
#define ACT_INVALID	1		/* invalide code */
#define	ACT_WRUNT	2		/* terminating white run code */
#define	ACT_WRUN	65		/* non-terminating white run code */
#define	ACT_BRUNT	106		/* terminating black run code */
#define	ACT_BRUN	169		/* non-terminating black run code */
#define ACT_EOL		210		/* end-of-line code */
HorizModeTable horiz_mode;

short horiz_mode_code_black (short runlen)
{
    return (runlen < 64 ? runlen + ACT_BRUNT : (runlen / 64) + ACT_BRUN);
}

short horiz_mode_code_white (short runlen)
{
    return (runlen < 64 ? runlen + ACT_WRUNT : (runlen / 64) + ACT_WRUN);
}

/*
 * If the corresponding horiz_mode entry is ACT_INCOMP
 * this entry is a row number for decoding the next byte;
 * otherwise, it is the bit number with which to continue
 * decoding the next codeword.
 */
HorizModeTable horiz_mode_next_state;
		/* prefixes corresponding to the rows of the decoding table */
long	horiz_mode_prefix[MAX_HORIZPREFIX];
		/* color of next run, BLACK or WHITE */
char	horiz_mode_color[MAX_HORIZPREFIX];
short	horiz_mode_prefix_count = 0;

static	unsigned char bit_mask[8] =
    { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };


int	verbose = FALSE;
char	*storage_class = "";

int
main (int argc, char *argv[])
{
    while (argc > 1 && argv[1][0] == '-') {
	if (strcmp(argv[1], "-v") == 0) {
	    verbose = TRUE;
	    argc--, argv++;
	} else if (strcmp(argv[1], "-c") == 0) {
	    storage_class = "const ";
	    argc--, argv++;
	}
    }
    build_null_mode_tables();		/* null mode decoding tables */
    if (verbose) {
	fprintf(stderr, "%d null mode prefixes defined\n",
	    (int) null_mode_prefix_count);
	fprintf(stderr, "building uncompressed mode scripts...\n");
    }
    build_uncomp_mode_tables();		/* uncompressed mode decoding tables */
    if (verbose) {
	fprintf(stderr, "%d uncompressed mode prefixes defined\n",
	    (int) uncomp_mode_prefix_count);
	fprintf(stderr, "building 1D scripts...\n");
    }
    build_horiz_mode_tables();		/* 1D decoding tables */
    if (verbose)
	fprintf(stderr, "%d incomplete prefixes defined\n",
	    (int) horiz_mode_prefix_count);
    write_tables(stdout);
    exit(0);
}

void write_null_mode_table (fd, table, name)
FILE *fd;
NullModeTable table;
char *name;
{
    int i, j;
    char* outersep;
    char* sep;

    fprintf(fd, "%sunsigned char\t%s[%d][256] = {", storage_class,
	name, (int) null_mode_prefix_count);
    outersep = "";
    for (i = 0; i < null_mode_prefix_count; i++) {
	fprintf(fd, "%s\n/* prefix %d */ {\n", outersep, i);
	sep = "    ";
	for (j = 0; j < 256; j++) {
	    fprintf(fd, "%s%2d", sep, (int) table[i][j]);
	    if (((j+1) % 16) == 0) {
		fprintf(fd, ", /* %3d-%3d */\n", j-15, j);
		sep = "    ";
	    } else
		sep = ",";
	}
	fprintf(fd, "}");
	outersep = ",";
    }
    fprintf(fd, "\n};\n");
}

void 
write_horiz_mode_table (FILE *fd, HorizModeTable table, char *name)
{
    int i, j;
    char* outersep;
    char* sep;

    fprintf(fd, "%s unsigned char\t%s[%d][256] = {", storage_class,
	name, (int) horiz_mode_prefix_count);
    outersep = "";
    for (i = 0; i < horiz_mode_prefix_count; i++) {
	fprintf(fd, "%s\n/* prefix %d */ {\n", outersep, i);
	sep = "    ";
	for (j = 0; j < 256; j++) {
	    fprintf(fd, "%s%3d", sep, (int) table[i][j]);
	    if (((j+1) % 14) == 0) {
		fprintf(fd, ", /* %3d-%3d */\n", j-13, j);
		sep = "    ";
	    } else
		sep = ",";
	}
	fprintf(fd, "\n}");
	outersep = ",";
    }
    fprintf(fd, "\n};\n");
}

void
write_define(FILE *fd, char *name, int value, char *comment)
{
    fprintf(fd, "#define\t%s\t%d", name, value);
    if (comment)
	fprintf(fd, "\t/* %s */", comment);
    fprintf(fd, "\n");
}

void
write_preamble(FILE *fd)
{
    fprintf(fd, "%s\n",
"/* DO NOT EDIT THIS FILE, IT WAS AUTOMATICALLY CREATED BY mkg3state */");
    write_define(fd, "ACT_INCOMP", ACT_INCOMP, "incompletely decoded code");
    write_define(fd, "ACT_INVALID", ACT_INVALID, "invalide code");
    write_define(fd, "ACT_WRUNT", ACT_WRUNT, "terminating white run code");
    write_define(fd, "ACT_WRUN", ACT_WRUN, "non-terminating white run code");
    write_define(fd, "ACT_BRUNT", ACT_BRUNT, "terminating black run code");
    write_define(fd, "ACT_BRUN", ACT_BRUN, "non-terminating black run code");
    write_define(fd, "ACT_EOL", ACT_EOL, "end-of-line code");
    fprintf(fd, "\n");
    fprintf(fd, "/* modes that the decoder can be in */\n");
    write_define(fd, "MODE_NULL", MODE_NULL, NULL);
    write_define(fd, "MODE_PASS", MODE_PASS, NULL);
    write_define(fd, "MODE_HORIZ", MODE_HORIZ, NULL);
    write_define(fd, "MODE_VERT_V0", MODE_VERT_V0, NULL);
    write_define(fd, "MODE_VERT_VR1", MODE_VERT_VR1, NULL);
    write_define(fd, "MODE_VERT_VR2", MODE_VERT_VR2, NULL);
    write_define(fd, "MODE_VERT_VR3", MODE_VERT_VR3, NULL);
    write_define(fd, "MODE_VERT_VL1", MODE_VERT_VL1, NULL);
    write_define(fd, "MODE_VERT_VL2", MODE_VERT_VL2, NULL);
    write_define(fd, "MODE_VERT_VL3", MODE_VERT_VL3, NULL);
    write_define(fd, "MODE_UNCOMP", MODE_UNCOMP, NULL);
    write_define(fd, "MODE_ERROR", MODE_ERROR, NULL);
    write_define(fd, "MODE_ERROR_1", MODE_ERROR_1, NULL);
    fprintf(fd, "\n");
    fprintf(fd, "#define\tRUNLENGTH(ix)	(TIFFFaxWhiteCodes[ix].runlen)\n");
    fprintf(fd, "\n");
    write_define(fd, "UNCOMP_INCOMP", UNCOMP_INCOMP, NULL);
    fprintf(fd, "/* runs of [0]*1 */\n");
    write_define(fd, "UNCOMP_RUN0", UNCOMP_RUN0, NULL);
    write_define(fd, "UNCOMP_RUN1", UNCOMP_RUN1, NULL);
    write_define(fd, "UNCOMP_RUN2", UNCOMP_RUN2, NULL);
    write_define(fd, "UNCOMP_RUN3", UNCOMP_RUN3, NULL);
    write_define(fd, "UNCOMP_RUN4", UNCOMP_RUN4, NULL);
    write_define(fd, "UNCOMP_RUN5", UNCOMP_RUN5, NULL);
    write_define(fd, "UNCOMP_RUN6", UNCOMP_RUN6, NULL);
    fprintf(fd, "/* runs of [0]* w/ terminating color */\n");
    write_define(fd, "UNCOMP_TRUN0", UNCOMP_TRUN0, NULL);
    write_define(fd, "UNCOMP_TRUN1", UNCOMP_TRUN1, NULL);
    write_define(fd, "UNCOMP_TRUN2", UNCOMP_TRUN2, NULL);
    write_define(fd, "UNCOMP_TRUN3", UNCOMP_TRUN3, NULL);
    write_define(fd, "UNCOMP_TRUN4", UNCOMP_TRUN4, NULL);
    fprintf(fd, "/* special code for unexpected EOF */\n");
    write_define(fd, "UNCOMP_EOF", UNCOMP_EOF, NULL);
    fprintf(fd, "/* invalid code encountered */\n");
    write_define(fd, "UNCOMP_INVALID", UNCOMP_INVALID, NULL);
    fprintf(fd, "/* codes >= terminate uncompress mode */\n");
    fprintf(fd, "#define\tUNCOMP_EXIT	UNCOMP_TRUN0\n");
    fprintf(fd, "\n");
}

void
extern_table(FILE *fd, char *name)
{
    fprintf(fd, "extern\t%sunsigned char %s[][256];\n", storage_class, name);
}

void
write_tables(FILE *fd)
{
    write_preamble(fd);
    write_null_mode_table(fd, null_mode, "TIFFFax2DMode");
    write_null_mode_table(fd, null_mode_next_state, "TIFFFax2DNextState");
    write_null_mode_table(fd, uncomp_mode, "TIFFFaxUncompAction");
    write_null_mode_table(fd, uncomp_mode_next_state, "TIFFFaxUncompNextState");
    write_horiz_mode_table(fd, horiz_mode, "TIFFFax1DAction");
    write_horiz_mode_table(fd, horiz_mode_next_state, "TIFFFax1DNextState");
}

short 
find_null_mode_prefix (long prefix)
{
    short j1;

    if (prefix == 0L)
	return (0);
    for (j1 = 8; j1 < null_mode_prefix_count; j1++)
	if (prefix == null_mode_prefix[j1])
		return (j1);
    if (null_mode_prefix_count == MAX_NULLPREFIX) {
	fprintf(stderr, "ERROR: null mode prefix table overflow\n");
	exit(1);
    }
    if (verbose)
	fprintf(stderr, "adding null mode prefix[%d] 0x%lx\n",
	    (int) null_mode_prefix_count, prefix);
    null_mode_prefix[null_mode_prefix_count++] = prefix;
    return (null_mode_prefix_count-1);
}

short 
find_horiz_mode_prefix (long prefix, int color)
{
    short j1;

    for (j1 = 0; j1 < horiz_mode_prefix_count; j1++)
	if (prefix == horiz_mode_prefix[j1] && horiz_mode_color[j1] == color)
	    return (j1);
    /*
     * It wasn't found, so add it to the tables, but first, is there room?
     */
    if (horiz_mode_prefix_count == MAX_HORIZPREFIX) {
	fprintf(stderr, "ERROR: 1D prefix table overflow\n");
	exit(1);
    }
    /* OK, there's room... */
    if (verbose)
	fprintf(stderr, "\nhoriz mode prefix %d, color %c = 0x%lx ",
	    (int) horiz_mode_prefix_count, "WB"[color], prefix);
    horiz_mode_prefix[horiz_mode_prefix_count] = prefix;
    horiz_mode_color[horiz_mode_prefix_count] = color;
    horiz_mode_prefix_count++;
    return (horiz_mode_prefix_count - 1);
}

short 
find_uncomp_mode_prefix (long prefix)
{
    short j1;

    if (prefix == 0L)
	return (0);
    for (j1 = 8; j1 < uncomp_mode_prefix_count; j1++)
	if (prefix == uncomp_mode_prefix[j1])
		return (j1);
    if (uncomp_mode_prefix_count == MAX_NULLPREFIX) {
	fprintf(stderr, "ERROR: uncomp mode prefix table overflow\n");
	exit(1);
    }
    if (verbose)
	fprintf(stderr, "adding uncomp mode prefix[%d] 0x%lx\n",
	    (int) uncomp_mode_prefix_count, prefix);
    uncomp_mode_prefix[uncomp_mode_prefix_count++] = prefix;
    return (uncomp_mode_prefix_count-1);
}

short 
null_mode_type (long prefix)
{
    switch (prefix) {
    case 0x18000L: return (MODE_VERT_V0);	/* 1 */
    case 0x36000L: return (MODE_VERT_VR1);	/* 011 */
    case 0x34000L: return (MODE_VERT_VL1);	/* 010 */
    case 0x32000L: return (MODE_HORIZ);		/* 001 */
    case 0x41000L: return (MODE_PASS);		/* 0001 */
    case 0x60C00L: return (MODE_VERT_VR2);	/* 0000 11 */
    case 0x60800L: return (MODE_VERT_VL2);	/* 0000 10 */
    case 0x70600L: return (MODE_VERT_VR3);	/* 0000 011 */
    case 0x70400L: return (MODE_VERT_VL3);	/* 0000 010 */
    case 0x80200L: return (MODE_ERROR);		/* 0000 0010 */
    case 0x90300L: return (MODE_ERROR);		/* 0000 0011 0 */
    case 0xA0380L: return (MODE_ERROR);		/* 0000 0011 10 */
    case 0xA03C0L: return (MODE_UNCOMP);	/* 0000 0011 11 */
    /*
     * Under the assumption that there are no
     * errors in the file, then this bit string
     * can only be the beginning of an EOL code.
     */
    case 0x70000L: return (MODE_ERROR_1);	/* 0000 000 */
    }
    return (-1);
}

short 
uncomp_mode_type (long prefix)
{
    short code;
    short len;
    switch (prefix) {
    case 0x18000L: return (UNCOMP_RUN1);	/* 1 */
    case 0x24000L: return (UNCOMP_RUN2);	/* 01 */
    case 0x32000L: return (UNCOMP_RUN3);	/* 001 */
    case 0x41000L: return (UNCOMP_RUN4);	/* 0001 */
    case 0x50800L: return (UNCOMP_RUN5);	/* 0000 1 */
    case 0x60400L: return (UNCOMP_RUN6);	/* 0000 01 */
    case 0x70200L: return (UNCOMP_TRUN0);	/* 0000 001 */
    case 0x80100L: return (UNCOMP_TRUN1);	/* 0000 0001 */
    case 0x90080L: return (UNCOMP_TRUN2);	/* 0000 0000 1 */
    case 0xA0040L: return (UNCOMP_TRUN3);	/* 0000 0000 01 */
    case 0xB0020L: return (UNCOMP_TRUN4);	/* 0000 0000 001 */
    }
    code = prefix & 0xffffL;
    len = (prefix >> 16) & 0xf;
    return ((code || len > 10) ? UNCOMP_INVALID : -1);
}

#define	BASESTATE(b)	((unsigned char) ((b) & 0x7))

void
build_null_mode_tables(void)
{
    short prefix;

    /*
     * Note: the first eight entries correspond to
     * a null prefix and starting bit numbers 0-7.
     */
    null_mode_prefix_count = 8;
    for (prefix = 0; prefix < null_mode_prefix_count; prefix++) {
	short byte;
	for (byte = 0; byte < 256; byte++) {
	    short firstbit;
	    short bit;
	    long curprefix;
	    char found_code = FALSE;

	    if (prefix < 8) {
		curprefix = 0L;
		firstbit = prefix;
	    } else {
		curprefix = null_mode_prefix[prefix];
		firstbit = 0;
	    }
	    for (bit = firstbit; bit < 8 && !found_code; bit++) {
		short mode;

		if (bit_mask[bit] & byte)
		    curprefix = append_1(curprefix);
		else
		    curprefix = append_0(curprefix);
		switch (mode = null_mode_type(curprefix)) {
		case MODE_PASS:
		case MODE_HORIZ:
		case MODE_VERT_V0:
		case MODE_VERT_VR1:
		case MODE_VERT_VR2:
		case MODE_VERT_VR3:
		case MODE_VERT_VL1:
		case MODE_VERT_VL2:
		case MODE_VERT_VL3:
		case MODE_UNCOMP:
		case MODE_ERROR:
		case MODE_ERROR_1:
		    /*
		     * NOTE: if the bit number is 8, then the table
		     * entry will be zero, which indicates a new byte
		     * is to be fetched during the decoding process
		     */
		    found_code = TRUE;
		    null_mode[prefix][byte] = (unsigned char) mode;
		    null_mode_next_state[prefix][byte] = BASESTATE(bit+1);
		    break;
		}
	    }
	    if (!found_code) {
		null_mode_next_state[prefix][byte] = (unsigned char)
		    find_null_mode_prefix(curprefix);
		/*
		 * This indicates to the decoder that
		 * no valid code has yet been identified.
		 */
		null_mode[prefix][byte] = MODE_NULL;
	    }
	}
    }
}

void
build_horiz_mode_tables(void)
{
    unsigned short byte;
    short prefix;

    /*
     * The first 8 are for white,
     * the second 8 are for black,
     * beginning with bits 0-7.
     */
    horiz_mode_prefix_count = 16;
    for (prefix = 0; prefix < horiz_mode_prefix_count; prefix++)
	for (byte = 0; byte < 256; byte++) {
	    short bits_digested = 0;
	    short bit;
	    short firstbit;
	    char color;
	    unsigned long curprefix;

	    if (prefix < 8) {
		color = WHITE;
		curprefix = 0L;
		firstbit = prefix;
	    } else if (prefix < 16) {
		color = BLACK;
		curprefix = 0L;
		firstbit = prefix - 8;
	    } else {
		color = horiz_mode_color[prefix];
		curprefix = horiz_mode_prefix[prefix];
		firstbit = 0;
	    }
	    for (bit = firstbit; bit < 8 && !bits_digested; bit++) {
		if (bit_mask[bit] & byte)
		    curprefix = append_1(curprefix);
		else
		    curprefix = append_0(curprefix);
		/*
		 * The following conversion allows for arbitrary strings of
		 * zeroes to precede the end-of-line code 0000 0000 0001.
		 * It assumes no errors in the data, and is based on
		 * the assumption that the code replaced (12 consecutive
		 * zeroes) can only be "legally" encountered before the
		 * end-of-line code.  This assumption is valid only for
		 * a Group 3 image; the combination will never occur
		 * in horizontal mode in a proper Group 4 image.
		 */
		if (curprefix == 0xC0000L)
		    curprefix = 0xB0000L;
		if (color == WHITE) {
		    short runlength = white_run_length(curprefix);

		    if (runlength == G3CODE_INVALID) {
			horiz_mode[prefix][byte] = (unsigned char) ACT_INVALID;
			horiz_mode_next_state[prefix][byte] = (unsigned char) bit;
			bits_digested = bit + 1;
		    } else if (runlength == G3CODE_EOL) { /* Group 3 only */
			horiz_mode[prefix][byte] = (unsigned char) ACT_EOL;
			horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
			bits_digested = bit + 1;
		    } else if (runlength != G3CODE_INCOMP) {
			horiz_mode[prefix][byte] = (unsigned char)
			    horiz_mode_code_white(runlength);
			horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
			bits_digested = bit + 1;
		    }
		} else {		/* color == BLACK */
		    short runlength = black_run_length(curprefix);

		    if (runlength == G3CODE_INVALID) {
			horiz_mode[prefix][byte] = (unsigned char) ACT_INVALID;
			horiz_mode_next_state[prefix][byte] = (unsigned char) (bit+8);
			bits_digested = bit + 1;
		    } else if (runlength == G3CODE_EOL) { /* Group 3 only */
			horiz_mode[prefix][byte] = (unsigned char) ACT_EOL;
			horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
			bits_digested = bit + 1;
		    } else if (runlength != G3CODE_INCOMP) {
			horiz_mode[prefix][byte] = (unsigned char)
			    horiz_mode_code_black(runlength);
			horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
			bits_digested = bit + 1;
		    }
		}
	    }
	    if (!bits_digested) {	/* no codewords after examining byte */
		horiz_mode[prefix][byte] = (unsigned char) ACT_INCOMP;
		horiz_mode_next_state[prefix][byte] = (unsigned char)
		    find_horiz_mode_prefix(curprefix, color);
	    }
	}
}

void
build_uncomp_mode_tables(void)
{
    short prefix;

    /*
     * Note: the first eight entries correspond to
     * a null prefix and starting bit numbers 0-7.
     */
    uncomp_mode_prefix_count = 8;
    for (prefix = 0; prefix < uncomp_mode_prefix_count; prefix++) {
	short byte;
	for (byte = 0; byte < 256; byte++) {
	    short firstbit;
	    short bit;
	    long curprefix;
	    char found_code = FALSE;

	    if (prefix < 8) {
		curprefix = 0L;
		firstbit = prefix;
	    } else {
		curprefix = uncomp_mode_prefix[prefix];
		firstbit = 0;
	    }
	    for (bit = firstbit; bit < 8 && !found_code; bit++) {
		short mode;

		if (bit_mask[bit] & byte)
		    curprefix = append_1(curprefix);
		else
		    curprefix = append_0(curprefix);
		mode = uncomp_mode_type(curprefix);
		if (mode != -1) {
		    /*
		     * NOTE: if the bit number is 8, then the table
		     * entry will be zero, which indicates a new byte
		     * is to be fetched during the decoding process
		     */
		    found_code = TRUE;
		    uncomp_mode[prefix][byte] = (unsigned char) mode;
		    uncomp_mode_next_state[prefix][byte] = BASESTATE(bit+1);
		    break;
		}
	    }
	    if (!found_code) {
		uncomp_mode_next_state[prefix][byte] = (unsigned char)
		    find_uncomp_mode_prefix(curprefix);
		/*
		 * This indicates to the decoder that
		 * no valid code has yet been identified.
		 */
		uncomp_mode[prefix][byte] = UNCOMP_INCOMP;
	    }
	}
    }
}