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