| 1 | /* "$Header$ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1991-1997 Sam Leffler |
| 5 | * Copyright (c) 1991-1997 Silicon Graphics, Inc. |
| 6 | * |
| 7 | * Permission to use, copy, modify, distribute, and sell this software and |
| 8 | * its documentation for any purpose is hereby granted without fee, provided |
| 9 | * that (i) the above copyright notices and this permission notice appear in |
| 10 | * all copies of the software and related documentation, and (ii) the names of |
| 11 | * Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 12 | * publicity relating to the software without the specific, prior written |
| 13 | * permission of Sam Leffler and Silicon Graphics. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 17 | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 18 | * |
| 19 | * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 20 | * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 21 | * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 22 | * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 23 | * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 24 | * OF THIS SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | /* Initialise fax decoder tables |
| 28 | * Decoder support is derived, with permission, from the code |
| 29 | * in Frank Cringle's viewfax program; |
| 30 | * Copyright (C) 1990, 1995 Frank D. Cringle. |
| 31 | */ |
| 32 | #if defined(unix) || defined(__unix) |
| 33 | #include "port.h" |
| 34 | #else |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #endif |
| 39 | |
| 40 | #include "tif_fax3.h" |
| 41 | |
| 42 | #define streq(a,b) (strcmp(a,b) == 0) |
| 43 | |
| 44 | /* NB: can't use names in tif_fax3.h 'cuz they are declared const */ |
| 45 | TIFFFaxTabEnt MainTable[128]; |
| 46 | TIFFFaxTabEnt WhiteTable[4096]; |
| 47 | TIFFFaxTabEnt BlackTable[8192]; |
| 48 | |
| 49 | struct proto { |
| 50 | uint16 code; /* right justified, lsb-first, zero filled */ |
| 51 | uint16 val; /* (pixel count)<<4 + code width */ |
| 52 | }; |
| 53 | |
| 54 | static struct proto Pass[] = { |
| 55 | { 0x0008, 4 }, |
| 56 | { 0, 0 } |
| 57 | }; |
| 58 | |
| 59 | static struct proto Horiz[] = { |
| 60 | { 0x0004, 3 }, |
| 61 | { 0, 0 } |
| 62 | }; |
| 63 | |
| 64 | static struct proto V0[] = { |
| 65 | { 0x0001, 1 }, |
| 66 | { 0, 0 } |
| 67 | }; |
| 68 | |
| 69 | static struct proto VR[] = { |
| 70 | { 0x0006, (1<<4)+3 }, |
| 71 | { 0x0030, (2<<4)+6 }, |
| 72 | { 0x0060, (3<<4)+7 }, |
| 73 | { 0, 0 } |
| 74 | }; |
| 75 | |
| 76 | static struct proto VL[] = { |
| 77 | { 0x0002, (1<<4)+3 }, |
| 78 | { 0x0010, (2<<4)+6 }, |
| 79 | { 0x0020, (3<<4)+7 }, |
| 80 | { 0, 0 } |
| 81 | }; |
| 82 | |
| 83 | static struct proto Ext[] = { |
| 84 | { 0x0040, 7 }, |
| 85 | { 0, 0 } |
| 86 | }; |
| 87 | |
| 88 | static struct proto EOLV[] = { |
| 89 | { 0x0000, 7 }, |
| 90 | { 0, 0 } |
| 91 | }; |
| 92 | |
| 93 | static struct proto MakeUpW[] = { |
| 94 | { 0x001b, 1029 }, |
| 95 | { 0x0009, 2053 }, |
| 96 | { 0x003a, 3078 }, |
| 97 | { 0x0076, 4103 }, |
| 98 | { 0x006c, 5128 }, |
| 99 | { 0x00ec, 6152 }, |
| 100 | { 0x0026, 7176 }, |
| 101 | { 0x00a6, 8200 }, |
| 102 | { 0x0016, 9224 }, |
| 103 | { 0x00e6, 10248 }, |
| 104 | { 0x0066, 11273 }, |
| 105 | { 0x0166, 12297 }, |
| 106 | { 0x0096, 13321 }, |
| 107 | { 0x0196, 14345 }, |
| 108 | { 0x0056, 15369 }, |
| 109 | { 0x0156, 16393 }, |
| 110 | { 0x00d6, 17417 }, |
| 111 | { 0x01d6, 18441 }, |
| 112 | { 0x0036, 19465 }, |
| 113 | { 0x0136, 20489 }, |
| 114 | { 0x00b6, 21513 }, |
| 115 | { 0x01b6, 22537 }, |
| 116 | { 0x0032, 23561 }, |
| 117 | { 0x0132, 24585 }, |
| 118 | { 0x00b2, 25609 }, |
| 119 | { 0x0006, 26630 }, |
| 120 | { 0x01b2, 27657 }, |
| 121 | { 0, 0 } |
| 122 | }; |
| 123 | |
| 124 | static struct proto MakeUpB[] = { |
| 125 | { 0x03c0, 1034 }, |
| 126 | { 0x0130, 2060 }, |
| 127 | { 0x0930, 3084 }, |
| 128 | { 0x0da0, 4108 }, |
| 129 | { 0x0cc0, 5132 }, |
| 130 | { 0x02c0, 6156 }, |
| 131 | { 0x0ac0, 7180 }, |
| 132 | { 0x06c0, 8205 }, |
| 133 | { 0x16c0, 9229 }, |
| 134 | { 0x0a40, 10253 }, |
| 135 | { 0x1a40, 11277 }, |
| 136 | { 0x0640, 12301 }, |
| 137 | { 0x1640, 13325 }, |
| 138 | { 0x09c0, 14349 }, |
| 139 | { 0x19c0, 15373 }, |
| 140 | { 0x05c0, 16397 }, |
| 141 | { 0x15c0, 17421 }, |
| 142 | { 0x0dc0, 18445 }, |
| 143 | { 0x1dc0, 19469 }, |
| 144 | { 0x0940, 20493 }, |
| 145 | { 0x1940, 21517 }, |
| 146 | { 0x0540, 22541 }, |
| 147 | { 0x1540, 23565 }, |
| 148 | { 0x0b40, 24589 }, |
| 149 | { 0x1b40, 25613 }, |
| 150 | { 0x04c0, 26637 }, |
| 151 | { 0x14c0, 27661 }, |
| 152 | { 0, 0 } |
| 153 | }; |
| 154 | |
| 155 | static struct proto MakeUp[] = { |
| 156 | { 0x0080, 28683 }, |
| 157 | { 0x0180, 29707 }, |
| 158 | { 0x0580, 30731 }, |
| 159 | { 0x0480, 31756 }, |
| 160 | { 0x0c80, 32780 }, |
| 161 | { 0x0280, 33804 }, |
| 162 | { 0x0a80, 34828 }, |
| 163 | { 0x0680, 35852 }, |
| 164 | { 0x0e80, 36876 }, |
| 165 | { 0x0380, 37900 }, |
| 166 | { 0x0b80, 38924 }, |
| 167 | { 0x0780, 39948 }, |
| 168 | { 0x0f80, 40972 }, |
| 169 | { 0, 0 } |
| 170 | }; |
| 171 | |
| 172 | static struct proto TermW[] = { |
| 173 | { 0x00ac, 8 }, |
| 174 | { 0x0038, 22 }, |
| 175 | { 0x000e, 36 }, |
| 176 | { 0x0001, 52 }, |
| 177 | { 0x000d, 68 }, |
| 178 | { 0x0003, 84 }, |
| 179 | { 0x0007, 100 }, |
| 180 | { 0x000f, 116 }, |
| 181 | { 0x0019, 133 }, |
| 182 | { 0x0005, 149 }, |
| 183 | { 0x001c, 165 }, |
| 184 | { 0x0002, 181 }, |
| 185 | { 0x0004, 198 }, |
| 186 | { 0x0030, 214 }, |
| 187 | { 0x000b, 230 }, |
| 188 | { 0x002b, 246 }, |
| 189 | { 0x0015, 262 }, |
| 190 | { 0x0035, 278 }, |
| 191 | { 0x0072, 295 }, |
| 192 | { 0x0018, 311 }, |
| 193 | { 0x0008, 327 }, |
| 194 | { 0x0074, 343 }, |
| 195 | { 0x0060, 359 }, |
| 196 | { 0x0010, 375 }, |
| 197 | { 0x000a, 391 }, |
| 198 | { 0x006a, 407 }, |
| 199 | { 0x0064, 423 }, |
| 200 | { 0x0012, 439 }, |
| 201 | { 0x000c, 455 }, |
| 202 | { 0x0040, 472 }, |
| 203 | { 0x00c0, 488 }, |
| 204 | { 0x0058, 504 }, |
| 205 | { 0x00d8, 520 }, |
| 206 | { 0x0048, 536 }, |
| 207 | { 0x00c8, 552 }, |
| 208 | { 0x0028, 568 }, |
| 209 | { 0x00a8, 584 }, |
| 210 | { 0x0068, 600 }, |
| 211 | { 0x00e8, 616 }, |
| 212 | { 0x0014, 632 }, |
| 213 | { 0x0094, 648 }, |
| 214 | { 0x0054, 664 }, |
| 215 | { 0x00d4, 680 }, |
| 216 | { 0x0034, 696 }, |
| 217 | { 0x00b4, 712 }, |
| 218 | { 0x0020, 728 }, |
| 219 | { 0x00a0, 744 }, |
| 220 | { 0x0050, 760 }, |
| 221 | { 0x00d0, 776 }, |
| 222 | { 0x004a, 792 }, |
| 223 | { 0x00ca, 808 }, |
| 224 | { 0x002a, 824 }, |
| 225 | { 0x00aa, 840 }, |
| 226 | { 0x0024, 856 }, |
| 227 | { 0x00a4, 872 }, |
| 228 | { 0x001a, 888 }, |
| 229 | { 0x009a, 904 }, |
| 230 | { 0x005a, 920 }, |
| 231 | { 0x00da, 936 }, |
| 232 | { 0x0052, 952 }, |
| 233 | { 0x00d2, 968 }, |
| 234 | { 0x004c, 984 }, |
| 235 | { 0x00cc, 1000 }, |
| 236 | { 0x002c, 1016 }, |
| 237 | { 0, 0 } |
| 238 | }; |
| 239 | |
| 240 | static struct proto TermB[] = { |
| 241 | { 0x03b0, 10 }, |
| 242 | { 0x0002, 19 }, |
| 243 | { 0x0003, 34 }, |
| 244 | { 0x0001, 50 }, |
| 245 | { 0x0006, 67 }, |
| 246 | { 0x000c, 84 }, |
| 247 | { 0x0004, 100 }, |
| 248 | { 0x0018, 117 }, |
| 249 | { 0x0028, 134 }, |
| 250 | { 0x0008, 150 }, |
| 251 | { 0x0010, 167 }, |
| 252 | { 0x0050, 183 }, |
| 253 | { 0x0070, 199 }, |
| 254 | { 0x0020, 216 }, |
| 255 | { 0x00e0, 232 }, |
| 256 | { 0x0030, 249 }, |
| 257 | { 0x03a0, 266 }, |
| 258 | { 0x0060, 282 }, |
| 259 | { 0x0040, 298 }, |
| 260 | { 0x0730, 315 }, |
| 261 | { 0x00b0, 331 }, |
| 262 | { 0x01b0, 347 }, |
| 263 | { 0x0760, 363 }, |
| 264 | { 0x00a0, 379 }, |
| 265 | { 0x0740, 395 }, |
| 266 | { 0x00c0, 411 }, |
| 267 | { 0x0530, 428 }, |
| 268 | { 0x0d30, 444 }, |
| 269 | { 0x0330, 460 }, |
| 270 | { 0x0b30, 476 }, |
| 271 | { 0x0160, 492 }, |
| 272 | { 0x0960, 508 }, |
| 273 | { 0x0560, 524 }, |
| 274 | { 0x0d60, 540 }, |
| 275 | { 0x04b0, 556 }, |
| 276 | { 0x0cb0, 572 }, |
| 277 | { 0x02b0, 588 }, |
| 278 | { 0x0ab0, 604 }, |
| 279 | { 0x06b0, 620 }, |
| 280 | { 0x0eb0, 636 }, |
| 281 | { 0x0360, 652 }, |
| 282 | { 0x0b60, 668 }, |
| 283 | { 0x05b0, 684 }, |
| 284 | { 0x0db0, 700 }, |
| 285 | { 0x02a0, 716 }, |
| 286 | { 0x0aa0, 732 }, |
| 287 | { 0x06a0, 748 }, |
| 288 | { 0x0ea0, 764 }, |
| 289 | { 0x0260, 780 }, |
| 290 | { 0x0a60, 796 }, |
| 291 | { 0x04a0, 812 }, |
| 292 | { 0x0ca0, 828 }, |
| 293 | { 0x0240, 844 }, |
| 294 | { 0x0ec0, 860 }, |
| 295 | { 0x01c0, 876 }, |
| 296 | { 0x0e40, 892 }, |
| 297 | { 0x0140, 908 }, |
| 298 | { 0x01a0, 924 }, |
| 299 | { 0x09a0, 940 }, |
| 300 | { 0x0d40, 956 }, |
| 301 | { 0x0340, 972 }, |
| 302 | { 0x05a0, 988 }, |
| 303 | { 0x0660, 1004 }, |
| 304 | { 0x0e60, 1020 }, |
| 305 | { 0, 0 } |
| 306 | }; |
| 307 | |
| 308 | static struct proto EOLH[] = { |
| 309 | { 0x0000, 11 }, |
| 310 | { 0, 0 } |
| 311 | }; |
| 312 | |
| 313 | static void |
| 314 | FillTable(TIFFFaxTabEnt *T, int Size, struct proto *P, int State) |
| 315 | { |
| 316 | int limit = 1 << Size; |
| 317 | |
| 318 | while (P->val) { |
| 319 | int width = P->val & 15; |
| 320 | int param = P->val >> 4; |
| 321 | int incr = 1 << width; |
| 322 | int code; |
| 323 | for (code = P->code; code < limit; code += incr) { |
| 324 | TIFFFaxTabEnt *E = T+code; |
| 325 | E->State = State; |
| 326 | E->Width = width; |
| 327 | E->Param = param; |
| 328 | } |
| 329 | P++; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | static char* storage_class = ""; |
| 334 | static char* const_class = ""; |
| 335 | static int packoutput = 1; |
| 336 | static char* prebrace = ""; |
| 337 | static char* postbrace = ""; |
| 338 | |
| 339 | void |
| 340 | WriteTable(FILE* fd, const TIFFFaxTabEnt* T, int Size, const char* name) |
| 341 | { |
| 342 | int i; |
| 343 | char* sep; |
| 344 | |
| 345 | fprintf(fd, "%s %s TIFFFaxTabEnt %s[%d] = {", |
| 346 | storage_class, const_class, name, Size); |
| 347 | if (packoutput) { |
| 348 | sep = "\n"; |
| 349 | for (i = 0; i < Size; i++) { |
| 350 | fprintf(fd, "%s%s%d,%d,%d%s", |
| 351 | sep, prebrace, T->State, T->Width, (int) T->Param, postbrace); |
| 352 | if (((i+1) % 12) == 0) |
| 353 | sep = ",\n"; |
| 354 | else |
| 355 | sep = ","; |
| 356 | T++; |
| 357 | } |
| 358 | } else { |
| 359 | sep = "\n "; |
| 360 | for (i = 0; i < Size; i++) { |
| 361 | fprintf(fd, "%s%s%3d,%3d,%4d%s", |
| 362 | sep, prebrace, T->State, T->Width, (int) T->Param, postbrace); |
| 363 | if (((i+1) % 6) == 0) |
| 364 | sep = ",\n "; |
| 365 | else |
| 366 | sep = ","; |
| 367 | T++; |
| 368 | } |
| 369 | } |
| 370 | fprintf(fd, "\n};\n"); |
| 371 | } |
| 372 | |
| 373 | /* initialise the huffman code tables */ |
| 374 | int |
| 375 | main(int argc, char* argv[]) |
| 376 | { |
| 377 | FILE* fd; |
| 378 | char* outputfile; |
| 379 | int c; |
| 380 | extern int optind; |
| 381 | extern char* optarg; |
| 382 | |
| 383 | while ((c = getopt(argc, argv, "c:s:bp")) != -1) |
| 384 | switch (c) { |
| 385 | case 'c': |
| 386 | const_class = optarg; |
| 387 | break; |
| 388 | case 's': |
| 389 | storage_class = optarg; |
| 390 | break; |
| 391 | case 'p': |
| 392 | packoutput = 0; |
| 393 | break; |
| 394 | case 'b': |
| 395 | prebrace = "{"; |
| 396 | postbrace = "}"; |
| 397 | break; |
| 398 | case '?': |
| 399 | fprintf(stderr, |
| 400 | "usage: %s [-c const] [-s storage] [-p] [-b] file\n", |
| 401 | argv[0]); |
| 402 | return (-1); |
| 403 | } |
| 404 | outputfile = optind < argc ? argv[optind] : "g3states.h"; |
| 405 | fd = fopen(outputfile, "w"); |
| 406 | if (fd == NULL) { |
| 407 | fprintf(stderr, "%s: %s: Cannot create output file.\n", |
| 408 | argv[0], outputfile); |
| 409 | return (-2); |
| 410 | } |
| 411 | FillTable(MainTable, 7, Pass, S_Pass); |
| 412 | FillTable(MainTable, 7, Horiz, S_Horiz); |
| 413 | FillTable(MainTable, 7, V0, S_V0); |
| 414 | FillTable(MainTable, 7, VR, S_VR); |
| 415 | FillTable(MainTable, 7, VL, S_VL); |
| 416 | FillTable(MainTable, 7, Ext, S_Ext); |
| 417 | FillTable(MainTable, 7, EOLV, S_EOL); |
| 418 | FillTable(WhiteTable, 12, MakeUpW, S_MakeUpW); |
| 419 | FillTable(WhiteTable, 12, MakeUp, S_MakeUp); |
| 420 | FillTable(WhiteTable, 12, TermW, S_TermW); |
| 421 | FillTable(WhiteTable, 12, EOLH, S_EOL); |
| 422 | FillTable(BlackTable, 13, MakeUpB, S_MakeUpB); |
| 423 | FillTable(BlackTable, 13, MakeUp, S_MakeUp); |
| 424 | FillTable(BlackTable, 13, TermB, S_TermB); |
| 425 | FillTable(BlackTable, 13, EOLH, S_EOL); |
| 426 | |
| 427 | fprintf(fd, "/* WARNING, this file was automatically generated by the\n"); |
| 428 | fprintf(fd, " mkg3states program */\n"); |
| 429 | fprintf(fd, "#include \"tiff.h\"\n"); |
| 430 | fprintf(fd, "#include \"tif_fax3.h\"\n"); |
| 431 | WriteTable(fd, MainTable, 128, "TIFFFaxMainTable"); |
| 432 | WriteTable(fd, WhiteTable, 4096, "TIFFFaxWhiteTable"); |
| 433 | WriteTable(fd, BlackTable, 8192, "TIFFFaxBlackTable"); |
| 434 | fclose(fd); |
| 435 | return (0); |
| 436 | } |