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