V10/cmd/bcp/CCITT.h

Compare this file to the similar file:
Show the results in this format:

/* Copyright (c) 1989, 1990 AT&T --- All Rights Reserved.              */
/* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T.                */
/* The copyright notice does not imply actual or intended publication. */
/* AUTHORS:                                            */
/*     H. S. Baird - ATT-BL MH - first versions        */
/* CCITT Group 3 FAX compression codes */
#define EOLSTRING	"000000000001"
#define EOLLENGTH	12		/* length of EOLSTRING */
#define MAXCODELEN	24

#define DST_EOL -3

/* translate a runlength value <=2560 to an index into the Huffman code table;
   if the index is >=64, then must repeat for (r%64) */
#define rtoi(r) (((r)<64)? (r): 64+((r)/64))
/* translate an index into the Huffman code table to a runlength value */
#define itor(i) (((i)<64)? (i): (((i)>64)? (((i)-64)*64): DST_EOL))

/* Black run code table */
  static char *codeblk[] = {
	/* code			run-length value */
	"0000110111",		/* 0, */
	"010",			/* 1, */
	"11",			/* 2, */
	"10",			/* 3, */
	"011",			/* 4, */
	"0011",			/* 5, */
	"0010",			/* 6, */
	"00011",		/* 7, */
	"000101",		/* 8, */
	"000100",		/* 9, */
	"0000100",		/* 10, */
	"0000101",		/* 11, */
	"0000111",		/* 12, */
	"00000100",		/* 13, */
	"00000111",		/* 14, */
	"000011000",		/* 15, */
	"0000010111",		/* 16, */
	"0000011000",		/* 17, */
	"0000001000",		/* 18, */
	"00001100111",		/* 19, */
	"00001101000",		/* 20, */
	"00001101100",		/* 21, */
	"00000110111",		/* 22, */
	"00000101000",		/* 23, */
	"00000010111",		/* 24, */
	"00000011000",		/* 25, */
	"000011001010",		/* 26, */
	"000011001011",		/* 27, */
	"000011001100",		/* 28, */
	"000011001101",		/* 29, */
	"000001101000",		/* 30, */
	"000001101001",		/* 31, */
	"000001101010",		/* 32, */
	"000001101011",		/* 33, */
	"000011010010",		/* 34, */
	"000011010011",		/* 35, */
	"000011010100",		/* 36, */
	"000011010101",		/* 37, */
	"000011010110",		/* 38, */
	"000011010111",		/* 39, */
	"000001101100",		/* 40, */
	"000001101101",		/* 41, */
	"000011011010",		/* 42, */
	"000011011011",		/* 43, */
	"000001010100",		/* 44, */
	"000001010101",		/* 45, */
	"000001010110",		/* 46, */
	"000001010111",		/* 47, */
	"000001100100",		/* 48, */
	"000001100101",		/* 49, */
	"000001010010",		/* 50, */
	"000001010011",		/* 51, */
	"000000100100",		/* 52, */
	"000000110111",		/* 53, */
	"000000111000",		/* 54, */
	"000000100111",		/* 55, */
	"000000101000",		/* 56, */
	"000001011000",		/* 57, */
	"000001011001",		/* 58, */
	"000000101011",		/* 59, */
	"000000101100",		/* 60, */
	"000001011010",		/* 61, */
	"000001100110",		/* 62, */
	"000001100111",		/* 63  */
	EOLSTRING,		/* EOL */
	"0000001111",		/* 64, */
	"000011001000",		/* 128, */
	"000011001001",		/* 192, */
	"000001011011",		/* 256, */
	"000000110011",		/* 320, */
	"000000110100",		/* 384, */
	"000000110101",		/* 448, */
	"0000001101100",	/* 512, */
	"0000001101101",	/* 576, */
	"0000001001010",	/* 640, */
	"0000001001011",	/* 704, */
	"0000001001100",	/* 768, */
	"0000001001101",	/* 832, */
	"0000001110010",	/* 896, */
	"0000001110011",	/* 960, */
	"0000001110100",	/* 1024, */
	"0000001110101",	/* 1088, */
	"0000001110110",	/* 1152, */
	"0000001110111",	/* 1216, */
	"0000001010010",	/* 1280, */
	"0000001010011",	/* 1344, */
	"0000001010100",	/* 1408, */
	"0000001010101",	/* 1472, */
	"0000001011010",	/* 1536, */
	"0000001011011",	/* 1600, */
	"0000001100100",	/* 1664, */
	"0000001100101",	/* 1728  */
				/* extended length: */
	"00000001000",		/* 1792, */
	"00000001100",		/* 1856, */
	"00000001101",		/* 1920, */
	"000000010010",		/* 1984, */
	"000000010011",		/* 2048, */
	"000000010100",		/* 2112, */
	"000000010101",		/* 2176, */
	"000000010110",		/* 2240, */
	"000000010111",		/* 2304, */
	"000000011100",		/* 2368, */
	"000000011101",		/* 2432, */
	"000000011110",		/* 2496, */
	"000000011111",		/* 2560  */
	NULL			/* are there codes beyond 2560? */
	};
/* No. bits in the codes in the above table */
  static short bitcblk[] = {
	10,3,2,2,3,4,4,5,6,6,		/*  0 -  9 */
	7,7,7,8,8,9,10,10,10,11,	/* 10 - 19 */
	11,11,11,11,11,11,12,12,12,12,	/* 20 - 29 */
	12,12,12,12,12,12,12,12,12,12,	/* 30 - 39 */
	12,12,12,12,12,12,12,12,12,12,	/* 40 - 49 */
	12,12,12,12,12,12,12,12,12,12,	/* 50 - 59 */
	12,12,12,12,			/* 60 - 63 */
	12,				/* EOL */
	10,				/* 64 */
	12,12,12,12,12,12,		/* 128 - 448 */
	13,13,13,13,13,13,13,13,13,	/* 512 - */
	13,13,13,13,13,13,13,13,13,
	13,13, 				/* - 1728 */
	11,11,11,			/* 1792 - 1920 */
	12,12,12,12,12,12,12,12,12,12,	/* 1984 - 2560 */
	/* for codes over 2560 */
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	-1
	};

/* White run code table */
  static char *codewht[] = {
	/* code			run-length value */
	"00110101",		/* 0, */
	"000111",		/* 1, */
	"0111",			/* 2, */
	"1000",			/* 3, */
	"1011",			/* 4, */
	"1100",			/* 5, */
	"1110",			/* 6, */
	"1111",			/* 7, */
	"10011",		/* 8, */
	"10100",		/* 9, */
	"00111",		/* 10, */
	"01000",		/* 11, */
	"001000",		/* 12, */
	"000011",		/* 13, */
	"110100",		/* 14, */
	"110101",		/* 15, */
	"101010",		/* 16, */
	"101011",		/* 17, */
	"0100111",		/* 18, */
	"0001100",		/* 19, */
	"0001000",		/* 20, */
	"0010111",		/* 21, */
	"0000011",		/* 22, */
	"0000100",		/* 23, */
	"0101000",		/* 24, */
	"0101011",		/* 25, */
	"0010011",		/* 26, */
	"0100100",		/* 27, */
	"0011000",		/* 28, */
	"00000010",		/* 29, */
	"00000011",		/* 30, */
	"00011010",		/* 31, */
	"00011011",		/* 32, */
	"00010010",		/* 33, */
	"00010011",		/* 34, */
	"00010100",		/* 35, */
	"00010101",		/* 36, */
	"00010110",		/* 37, */
	"00010111",		/* 38, */
	"00101000",		/* 39, */
	"00101001",		/* 40, */
	"00101010",		/* 41, */
	"00101011",		/* 42, */
	"00101100",		/* 43, */
	"00101101",		/* 44, */
	"00000100",		/* 45, */
	"00000101",		/* 46, */
	"00001010",		/* 47, */
	"00001011",		/* 48, */
	"01010010",		/* 49, */
	"01010011",		/* 50, */
	"01010100",		/* 51, */
	"01010101",		/* 52, */
	"00100100",		/* 53, */
	"00100101",		/* 54, */
	"01011000",		/* 55, */
	"01011001",		/* 56, */
	"01011010",		/* 57, */
	"01011011",		/* 58, */
	"01001010",		/* 59, */
	"01001011",		/* 60, */
	"00110010",		/* 61, */
	"00110011",		/* 62, */
	"00110100",		/* 63  */
	EOLSTRING,		/* EOL */
	"11011",		/* 64, */
	"10010",		/* 128, */
	"010111",		/* 192, */
	"0110111",		/* 256, */
	"00110110",		/* 320, */
	"00110111",		/* 384, */
	"01100100",		/* 448, */
	"01100101",		/* 512, */
	"01101000",		/* 576, */
	"01100111",		/* 640, */
	"011001100",		/* 704, */
	"011001101",		/* 768, */
	"011010010",		/* 832, */
	"011010011",		/* 896, */
	"011010100",		/* 960, */
	"011010101",		/* 1024, */
	"011010110",		/* 1088, */
	"011010111",		/* 1152, */
	"011011000",		/* 1216, */
	"011011001",		/* 1280, */
	"011011010",		/* 1344, */
	"011011011",		/* 1408, */
	"010011000",		/* 1472, */
	"010011001",		/* 1536, */
	"010011010",		/* 1600, */
	"011000",		/* 1664, */
	"010011011",		/* 1728  */
				/* extended length: */
	"00000001000",		/* 1792, */
	"00000001100",		/* 1856, */
	"00000001101",		/* 1920, */
	"000000010010",		/* 1984, */
	"000000010011",		/* 2048, */
	"000000010100",		/* 2112, */
	"000000010101",		/* 2176, */
	"000000010110",		/* 2240, */
	"000000010111",		/* 2304, */
	"000000011100",		/* 2368, */
	"000000011101",		/* 2432, */
	"000000011110",		/* 2496, */
	"000000011111",		/* 2560  */
	NULL			/* are there codes beyond 2560? */
	};
/* no. bits in the codes in the above table */
  static short bitcwht[] = {
	8,6,4,4,4,4,4,4,5,5,		/*  0 -  9 */
	5,5,6,6,6,6,6,6,7,7,		/* 10 - 19 */
	7,7,7,7,7,7,7,7,7,8,		/* 20 - 29 */
	8,8,8,8,8,8,8,8,8,8,		/* 30 - 39 */
	8,8,8,8,8,8,8,8,8,8,		/* 40 - 49 */
	8,8,8,8,8,8,8,8,8,8,		/* 50 - 59 */
	8,8,8,8,			/* 60 - 63 */
	12,				/* EOL */
	5,5,				/* 64,128 */
	6,				/* 192 */
	7,				/* 256 */
	8,8,8,8,8,8,			/* 320 - 640 */
	9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,	/* 704 - 1600 */
	6,				/* 1664 */
	9,				/* 1728 */
	11,11,11,			/* 1792,1856,1920 */
	12,12,12,12,12,12,12,12,12,12,	/* 1984 - 2560 */
	/* for codes over 2560 */
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	-1
	};

/* 2-D codes (indices into code2d[] table) */
#define i2D_V0 0
#define i2D_VR1 1
#define i2D_VR2 2
#define i2D_VR3 3
#define i2D_VL1 4
#define i2D_VL2 5
#define i2D_VL3 6
#define i2D_PASS 7
#define i2D_HORIZ 8
#define i2D_EOL 9

  static char *code2d[] = {
	"1",			/* V0 */
	"011",			/* VR1 */
	"000011",		/* VR2 */
	"0000011",		/* VR3 */
	"010",			/* VL1 */
	"000010",		/* VL2 */
	"0000010",		/* VL3 */
	"0001",			/* PASS */
	"001",			/* HORIZ */
	EOLSTRING,		/* EOL */
	NULL };
  static short bitc2d[] = { 1,3,6,7,3,6,7,4,3,12 };

#define EOL0STRING	"0000000000010"
#define EOL1STRING	"0000000000011"
#define EOFB		"000000000001000000000001"

  static char *spare1d[] = {
	"000000001",		/* 0, */
	"0000000001",		/* 0, */
	"00000000001",		/* 0  */
	NULL };
  static char *spare2d[] = {
	"0000001",		/* 0, */
	"00000001",		/* 0, */
	"000000001",		/* 0, */
	"0000000001",		/* 0, */
	"00000000001",		/* 0  */
	NULL };

/* State-transition table for decoding CCITT G3-1 */

/* bit colors; also, starting index in table of 1-D codes*/
#define DST_color short
#define DST_white 0
#define DST_black 1

#define flip_color(c) ((c)? 0: 1)

#define DST_2d 2	/* starting index in table of 2-D codes */

#define DST_state short	/* state-id: index into DST_tbl.e[] */
#define DST_state_NULL (-1)
#define DST_state_ERROR (-2)

#define DST_action int
#define DST_action_NULL (-1)
#define DST_action_ERROR (-2)

/* transition in finite-state machine */
typedef struct DST_transit {
	DST_action a;	/* action to perform */
	DST_state s;	/* next state */
	} DST_transit;
typedef struct DST_entry {
	char p[MAXCODELEN+1];	/* code prefix so far (in ASCII) */
	short l;		/* strlen(.p) */
	short z;		/* no. of trailing "0"'s in .p */
	DST_transit t[2];	/* two transitions: on 0 & 1 */
	} DST_entry;
typedef struct DST_table {
	int mny;	/* no. entries so far */
	DST_entry *e;	/* array in malloc space:
				e[DST_white] starts white 1-D codes;
				e[DST_black] starts black 1-D codes;
				e[DST_2d] starts 2-D codes */
	} DST_table;
typedef struct DST_context {
	DST_color c;	/* current run-color */
	int l;		/* length of current code in bits: 0..(len-1) */
	DST_table *t;	/* table */
	DST_state s;	/* current state */
	DST_transit tr;	/* current state/action */
	} DST_context;


DST_table *ccitt_table();
RLE_Line *g31_to_rlel();
int rlel_to_g31();
RLE_Line *g32_to_rlel();
int rlel_to_g32();
RLE_Line *g4_to_rlel();
int rlel_to_g4();