|
|
1.1 ! root 1: /* Copyright (c) 1989, 1990 AT&T --- All Rights Reserved. */ ! 2: /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T. */ ! 3: /* The copyright notice does not imply actual or intended publication. */ ! 4: /* AUTHORS: */ ! 5: /* H. S. Baird - ATT-BL MH - first versions */ ! 6: /* CCITT Group 3 FAX compression codes */ ! 7: #define EOLSTRING "000000000001" ! 8: #define EOLLENGTH 12 /* length of EOLSTRING */ ! 9: #define MAXCODELEN 24 ! 10: ! 11: #define DST_EOL -3 ! 12: ! 13: /* translate a runlength value <=2560 to an index into the Huffman code table; ! 14: if the index is >=64, then must repeat for (r%64) */ ! 15: #define rtoi(r) (((r)<64)? (r): 64+((r)/64)) ! 16: /* translate an index into the Huffman code table to a runlength value */ ! 17: #define itor(i) (((i)<64)? (i): (((i)>64)? (((i)-64)*64): DST_EOL)) ! 18: ! 19: /* Black run code table */ ! 20: static char *codeblk[] = { ! 21: /* code run-length value */ ! 22: "0000110111", /* 0, */ ! 23: "010", /* 1, */ ! 24: "11", /* 2, */ ! 25: "10", /* 3, */ ! 26: "011", /* 4, */ ! 27: "0011", /* 5, */ ! 28: "0010", /* 6, */ ! 29: "00011", /* 7, */ ! 30: "000101", /* 8, */ ! 31: "000100", /* 9, */ ! 32: "0000100", /* 10, */ ! 33: "0000101", /* 11, */ ! 34: "0000111", /* 12, */ ! 35: "00000100", /* 13, */ ! 36: "00000111", /* 14, */ ! 37: "000011000", /* 15, */ ! 38: "0000010111", /* 16, */ ! 39: "0000011000", /* 17, */ ! 40: "0000001000", /* 18, */ ! 41: "00001100111", /* 19, */ ! 42: "00001101000", /* 20, */ ! 43: "00001101100", /* 21, */ ! 44: "00000110111", /* 22, */ ! 45: "00000101000", /* 23, */ ! 46: "00000010111", /* 24, */ ! 47: "00000011000", /* 25, */ ! 48: "000011001010", /* 26, */ ! 49: "000011001011", /* 27, */ ! 50: "000011001100", /* 28, */ ! 51: "000011001101", /* 29, */ ! 52: "000001101000", /* 30, */ ! 53: "000001101001", /* 31, */ ! 54: "000001101010", /* 32, */ ! 55: "000001101011", /* 33, */ ! 56: "000011010010", /* 34, */ ! 57: "000011010011", /* 35, */ ! 58: "000011010100", /* 36, */ ! 59: "000011010101", /* 37, */ ! 60: "000011010110", /* 38, */ ! 61: "000011010111", /* 39, */ ! 62: "000001101100", /* 40, */ ! 63: "000001101101", /* 41, */ ! 64: "000011011010", /* 42, */ ! 65: "000011011011", /* 43, */ ! 66: "000001010100", /* 44, */ ! 67: "000001010101", /* 45, */ ! 68: "000001010110", /* 46, */ ! 69: "000001010111", /* 47, */ ! 70: "000001100100", /* 48, */ ! 71: "000001100101", /* 49, */ ! 72: "000001010010", /* 50, */ ! 73: "000001010011", /* 51, */ ! 74: "000000100100", /* 52, */ ! 75: "000000110111", /* 53, */ ! 76: "000000111000", /* 54, */ ! 77: "000000100111", /* 55, */ ! 78: "000000101000", /* 56, */ ! 79: "000001011000", /* 57, */ ! 80: "000001011001", /* 58, */ ! 81: "000000101011", /* 59, */ ! 82: "000000101100", /* 60, */ ! 83: "000001011010", /* 61, */ ! 84: "000001100110", /* 62, */ ! 85: "000001100111", /* 63 */ ! 86: EOLSTRING, /* EOL */ ! 87: "0000001111", /* 64, */ ! 88: "000011001000", /* 128, */ ! 89: "000011001001", /* 192, */ ! 90: "000001011011", /* 256, */ ! 91: "000000110011", /* 320, */ ! 92: "000000110100", /* 384, */ ! 93: "000000110101", /* 448, */ ! 94: "0000001101100", /* 512, */ ! 95: "0000001101101", /* 576, */ ! 96: "0000001001010", /* 640, */ ! 97: "0000001001011", /* 704, */ ! 98: "0000001001100", /* 768, */ ! 99: "0000001001101", /* 832, */ ! 100: "0000001110010", /* 896, */ ! 101: "0000001110011", /* 960, */ ! 102: "0000001110100", /* 1024, */ ! 103: "0000001110101", /* 1088, */ ! 104: "0000001110110", /* 1152, */ ! 105: "0000001110111", /* 1216, */ ! 106: "0000001010010", /* 1280, */ ! 107: "0000001010011", /* 1344, */ ! 108: "0000001010100", /* 1408, */ ! 109: "0000001010101", /* 1472, */ ! 110: "0000001011010", /* 1536, */ ! 111: "0000001011011", /* 1600, */ ! 112: "0000001100100", /* 1664, */ ! 113: "0000001100101", /* 1728 */ ! 114: /* extended length: */ ! 115: "00000001000", /* 1792, */ ! 116: "00000001100", /* 1856, */ ! 117: "00000001101", /* 1920, */ ! 118: "000000010010", /* 1984, */ ! 119: "000000010011", /* 2048, */ ! 120: "000000010100", /* 2112, */ ! 121: "000000010101", /* 2176, */ ! 122: "000000010110", /* 2240, */ ! 123: "000000010111", /* 2304, */ ! 124: "000000011100", /* 2368, */ ! 125: "000000011101", /* 2432, */ ! 126: "000000011110", /* 2496, */ ! 127: "000000011111", /* 2560 */ ! 128: NULL /* are there codes beyond 2560? */ ! 129: }; ! 130: /* No. bits in the codes in the above table */ ! 131: static short bitcblk[] = { ! 132: 10,3,2,2,3,4,4,5,6,6, /* 0 - 9 */ ! 133: 7,7,7,8,8,9,10,10,10,11, /* 10 - 19 */ ! 134: 11,11,11,11,11,11,12,12,12,12, /* 20 - 29 */ ! 135: 12,12,12,12,12,12,12,12,12,12, /* 30 - 39 */ ! 136: 12,12,12,12,12,12,12,12,12,12, /* 40 - 49 */ ! 137: 12,12,12,12,12,12,12,12,12,12, /* 50 - 59 */ ! 138: 12,12,12,12, /* 60 - 63 */ ! 139: 12, /* EOL */ ! 140: 10, /* 64 */ ! 141: 12,12,12,12,12,12, /* 128 - 448 */ ! 142: 13,13,13,13,13,13,13,13,13, /* 512 - */ ! 143: 13,13,13,13,13,13,13,13,13, ! 144: 13,13, /* - 1728 */ ! 145: 11,11,11, /* 1792 - 1920 */ ! 146: 12,12,12,12,12,12,12,12,12,12, /* 1984 - 2560 */ ! 147: /* for codes over 2560 */ ! 148: 14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14, ! 149: -1 ! 150: }; ! 151: ! 152: /* White run code table */ ! 153: static char *codewht[] = { ! 154: /* code run-length value */ ! 155: "00110101", /* 0, */ ! 156: "000111", /* 1, */ ! 157: "0111", /* 2, */ ! 158: "1000", /* 3, */ ! 159: "1011", /* 4, */ ! 160: "1100", /* 5, */ ! 161: "1110", /* 6, */ ! 162: "1111", /* 7, */ ! 163: "10011", /* 8, */ ! 164: "10100", /* 9, */ ! 165: "00111", /* 10, */ ! 166: "01000", /* 11, */ ! 167: "001000", /* 12, */ ! 168: "000011", /* 13, */ ! 169: "110100", /* 14, */ ! 170: "110101", /* 15, */ ! 171: "101010", /* 16, */ ! 172: "101011", /* 17, */ ! 173: "0100111", /* 18, */ ! 174: "0001100", /* 19, */ ! 175: "0001000", /* 20, */ ! 176: "0010111", /* 21, */ ! 177: "0000011", /* 22, */ ! 178: "0000100", /* 23, */ ! 179: "0101000", /* 24, */ ! 180: "0101011", /* 25, */ ! 181: "0010011", /* 26, */ ! 182: "0100100", /* 27, */ ! 183: "0011000", /* 28, */ ! 184: "00000010", /* 29, */ ! 185: "00000011", /* 30, */ ! 186: "00011010", /* 31, */ ! 187: "00011011", /* 32, */ ! 188: "00010010", /* 33, */ ! 189: "00010011", /* 34, */ ! 190: "00010100", /* 35, */ ! 191: "00010101", /* 36, */ ! 192: "00010110", /* 37, */ ! 193: "00010111", /* 38, */ ! 194: "00101000", /* 39, */ ! 195: "00101001", /* 40, */ ! 196: "00101010", /* 41, */ ! 197: "00101011", /* 42, */ ! 198: "00101100", /* 43, */ ! 199: "00101101", /* 44, */ ! 200: "00000100", /* 45, */ ! 201: "00000101", /* 46, */ ! 202: "00001010", /* 47, */ ! 203: "00001011", /* 48, */ ! 204: "01010010", /* 49, */ ! 205: "01010011", /* 50, */ ! 206: "01010100", /* 51, */ ! 207: "01010101", /* 52, */ ! 208: "00100100", /* 53, */ ! 209: "00100101", /* 54, */ ! 210: "01011000", /* 55, */ ! 211: "01011001", /* 56, */ ! 212: "01011010", /* 57, */ ! 213: "01011011", /* 58, */ ! 214: "01001010", /* 59, */ ! 215: "01001011", /* 60, */ ! 216: "00110010", /* 61, */ ! 217: "00110011", /* 62, */ ! 218: "00110100", /* 63 */ ! 219: EOLSTRING, /* EOL */ ! 220: "11011", /* 64, */ ! 221: "10010", /* 128, */ ! 222: "010111", /* 192, */ ! 223: "0110111", /* 256, */ ! 224: "00110110", /* 320, */ ! 225: "00110111", /* 384, */ ! 226: "01100100", /* 448, */ ! 227: "01100101", /* 512, */ ! 228: "01101000", /* 576, */ ! 229: "01100111", /* 640, */ ! 230: "011001100", /* 704, */ ! 231: "011001101", /* 768, */ ! 232: "011010010", /* 832, */ ! 233: "011010011", /* 896, */ ! 234: "011010100", /* 960, */ ! 235: "011010101", /* 1024, */ ! 236: "011010110", /* 1088, */ ! 237: "011010111", /* 1152, */ ! 238: "011011000", /* 1216, */ ! 239: "011011001", /* 1280, */ ! 240: "011011010", /* 1344, */ ! 241: "011011011", /* 1408, */ ! 242: "010011000", /* 1472, */ ! 243: "010011001", /* 1536, */ ! 244: "010011010", /* 1600, */ ! 245: "011000", /* 1664, */ ! 246: "010011011", /* 1728 */ ! 247: /* extended length: */ ! 248: "00000001000", /* 1792, */ ! 249: "00000001100", /* 1856, */ ! 250: "00000001101", /* 1920, */ ! 251: "000000010010", /* 1984, */ ! 252: "000000010011", /* 2048, */ ! 253: "000000010100", /* 2112, */ ! 254: "000000010101", /* 2176, */ ! 255: "000000010110", /* 2240, */ ! 256: "000000010111", /* 2304, */ ! 257: "000000011100", /* 2368, */ ! 258: "000000011101", /* 2432, */ ! 259: "000000011110", /* 2496, */ ! 260: "000000011111", /* 2560 */ ! 261: NULL /* are there codes beyond 2560? */ ! 262: }; ! 263: /* no. bits in the codes in the above table */ ! 264: static short bitcwht[] = { ! 265: 8,6,4,4,4,4,4,4,5,5, /* 0 - 9 */ ! 266: 5,5,6,6,6,6,6,6,7,7, /* 10 - 19 */ ! 267: 7,7,7,7,7,7,7,7,7,8, /* 20 - 29 */ ! 268: 8,8,8,8,8,8,8,8,8,8, /* 30 - 39 */ ! 269: 8,8,8,8,8,8,8,8,8,8, /* 40 - 49 */ ! 270: 8,8,8,8,8,8,8,8,8,8, /* 50 - 59 */ ! 271: 8,8,8,8, /* 60 - 63 */ ! 272: 12, /* EOL */ ! 273: 5,5, /* 64,128 */ ! 274: 6, /* 192 */ ! 275: 7, /* 256 */ ! 276: 8,8,8,8,8,8, /* 320 - 640 */ ! 277: 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, /* 704 - 1600 */ ! 278: 6, /* 1664 */ ! 279: 9, /* 1728 */ ! 280: 11,11,11, /* 1792,1856,1920 */ ! 281: 12,12,12,12,12,12,12,12,12,12, /* 1984 - 2560 */ ! 282: /* for codes over 2560 */ ! 283: 14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14, ! 284: -1 ! 285: }; ! 286: ! 287: /* 2-D codes (indices into code2d[] table) */ ! 288: #define i2D_V0 0 ! 289: #define i2D_VR1 1 ! 290: #define i2D_VR2 2 ! 291: #define i2D_VR3 3 ! 292: #define i2D_VL1 4 ! 293: #define i2D_VL2 5 ! 294: #define i2D_VL3 6 ! 295: #define i2D_PASS 7 ! 296: #define i2D_HORIZ 8 ! 297: #define i2D_EOL 9 ! 298: ! 299: static char *code2d[] = { ! 300: "1", /* V0 */ ! 301: "011", /* VR1 */ ! 302: "000011", /* VR2 */ ! 303: "0000011", /* VR3 */ ! 304: "010", /* VL1 */ ! 305: "000010", /* VL2 */ ! 306: "0000010", /* VL3 */ ! 307: "0001", /* PASS */ ! 308: "001", /* HORIZ */ ! 309: EOLSTRING, /* EOL */ ! 310: NULL }; ! 311: static short bitc2d[] = { 1,3,6,7,3,6,7,4,3,12 }; ! 312: ! 313: #define EOL0STRING "0000000000010" ! 314: #define EOL1STRING "0000000000011" ! 315: #define EOFB "000000000001000000000001" ! 316: ! 317: static char *spare1d[] = { ! 318: "000000001", /* 0, */ ! 319: "0000000001", /* 0, */ ! 320: "00000000001", /* 0 */ ! 321: NULL }; ! 322: static char *spare2d[] = { ! 323: "0000001", /* 0, */ ! 324: "00000001", /* 0, */ ! 325: "000000001", /* 0, */ ! 326: "0000000001", /* 0, */ ! 327: "00000000001", /* 0 */ ! 328: NULL }; ! 329: ! 330: /* State-transition table for decoding CCITT G3-1 */ ! 331: ! 332: /* bit colors; also, starting index in table of 1-D codes*/ ! 333: #define DST_color short ! 334: #define DST_white 0 ! 335: #define DST_black 1 ! 336: ! 337: #define flip_color(c) ((c)? 0: 1) ! 338: ! 339: #define DST_2d 2 /* starting index in table of 2-D codes */ ! 340: ! 341: #define DST_state short /* state-id: index into DST_tbl.e[] */ ! 342: #define DST_state_NULL (-1) ! 343: #define DST_state_ERROR (-2) ! 344: ! 345: #define DST_action int ! 346: #define DST_action_NULL (-1) ! 347: #define DST_action_ERROR (-2) ! 348: ! 349: /* transition in finite-state machine */ ! 350: typedef struct DST_transit { ! 351: DST_action a; /* action to perform */ ! 352: DST_state s; /* next state */ ! 353: } DST_transit; ! 354: typedef struct DST_entry { ! 355: char p[MAXCODELEN+1]; /* code prefix so far (in ASCII) */ ! 356: short l; /* strlen(.p) */ ! 357: short z; /* no. of trailing "0"'s in .p */ ! 358: DST_transit t[2]; /* two transitions: on 0 & 1 */ ! 359: } DST_entry; ! 360: typedef struct DST_table { ! 361: int mny; /* no. entries so far */ ! 362: DST_entry *e; /* array in malloc space: ! 363: e[DST_white] starts white 1-D codes; ! 364: e[DST_black] starts black 1-D codes; ! 365: e[DST_2d] starts 2-D codes */ ! 366: } DST_table; ! 367: typedef struct DST_context { ! 368: DST_color c; /* current run-color */ ! 369: int l; /* length of current code in bits: 0..(len-1) */ ! 370: DST_table *t; /* table */ ! 371: DST_state s; /* current state */ ! 372: DST_transit tr; /* current state/action */ ! 373: } DST_context; ! 374: ! 375: ! 376: DST_table *ccitt_table(); ! 377: RLE_Line *g31_to_rlel(); ! 378: int rlel_to_g31(); ! 379: RLE_Line *g32_to_rlel(); ! 380: int rlel_to_g32(); ! 381: RLE_Line *g4_to_rlel(); ! 382: int rlel_to_g4();
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.