roentgen b75cab
/* "$Id: mkg3states.c,v 1.11 2010-03-10 18:56:48 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1991-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and 
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 * 
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
roentgen b75cab
 * 
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
/* Initialise fax decoder tables
roentgen b75cab
 * Decoder support is derived, with permission, from the code
roentgen b75cab
 * in Frank Cringle's viewfax program;
roentgen b75cab
 *      Copyright (C) 1990, 1995  Frank D. Cringle.
roentgen b75cab
 */
roentgen b75cab
#include "tif_config.h"
roentgen b75cab
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
#include <stdlib.h></stdlib.h>
roentgen b75cab
#include <string.h></string.h>
roentgen b75cab
roentgen b75cab
#ifdef HAVE_UNISTD_H
roentgen b75cab
# include <unistd.h></unistd.h>
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#include "tif_fax3.h"
roentgen b75cab
roentgen b75cab
#ifndef HAVE_GETOPT
roentgen b75cab
extern int getopt(int, char**, char*);
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#define	streq(a,b)	(strcmp(a,b) == 0)
roentgen b75cab
roentgen b75cab
/* NB: can't use names in tif_fax3.h 'cuz they are declared const */
roentgen b75cab
TIFFFaxTabEnt MainTable[128];
roentgen b75cab
TIFFFaxTabEnt WhiteTable[4096];
roentgen b75cab
TIFFFaxTabEnt BlackTable[8192];
roentgen b75cab
roentgen b75cab
struct proto {
roentgen b75cab
    uint16 code;		/* right justified, lsb-first, zero filled */
roentgen b75cab
    uint16 val;		/* (pixel count)<<4 + code width  */
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto Pass[] = {
roentgen b75cab
{ 0x0008, 4 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto Horiz[]  = {
roentgen b75cab
{ 0x0004, 3 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto V0[]  = {
roentgen b75cab
{ 0x0001, 1 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto VR[]  = {
roentgen b75cab
{ 0x0006, (1<<4)+3 },
roentgen b75cab
{ 0x0030, (2<<4)+6 },
roentgen b75cab
{ 0x0060, (3<<4)+7 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto VL[]  = {
roentgen b75cab
{ 0x0002, (1<<4)+3 },
roentgen b75cab
{ 0x0010, (2<<4)+6 },
roentgen b75cab
{ 0x0020, (3<<4)+7 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto Ext[]  = {
roentgen b75cab
{ 0x0040, 7 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto EOLV[]  = {
roentgen b75cab
{ 0x0000, 7 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto MakeUpW[] = {
roentgen b75cab
{ 0x001b, 1029 },
roentgen b75cab
{ 0x0009, 2053 },
roentgen b75cab
{ 0x003a, 3078 },
roentgen b75cab
{ 0x0076, 4103 },
roentgen b75cab
{ 0x006c, 5128 },
roentgen b75cab
{ 0x00ec, 6152 },
roentgen b75cab
{ 0x0026, 7176 },
roentgen b75cab
{ 0x00a6, 8200 },
roentgen b75cab
{ 0x0016, 9224 },
roentgen b75cab
{ 0x00e6, 10248 },
roentgen b75cab
{ 0x0066, 11273 },
roentgen b75cab
{ 0x0166, 12297 },
roentgen b75cab
{ 0x0096, 13321 },
roentgen b75cab
{ 0x0196, 14345 },
roentgen b75cab
{ 0x0056, 15369 },
roentgen b75cab
{ 0x0156, 16393 },
roentgen b75cab
{ 0x00d6, 17417 },
roentgen b75cab
{ 0x01d6, 18441 },
roentgen b75cab
{ 0x0036, 19465 },
roentgen b75cab
{ 0x0136, 20489 },
roentgen b75cab
{ 0x00b6, 21513 },
roentgen b75cab
{ 0x01b6, 22537 },
roentgen b75cab
{ 0x0032, 23561 },
roentgen b75cab
{ 0x0132, 24585 },
roentgen b75cab
{ 0x00b2, 25609 },
roentgen b75cab
{ 0x0006, 26630 },
roentgen b75cab
{ 0x01b2, 27657 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto MakeUpB[] = {
roentgen b75cab
{ 0x03c0, 1034 },
roentgen b75cab
{ 0x0130, 2060 },
roentgen b75cab
{ 0x0930, 3084 },
roentgen b75cab
{ 0x0da0, 4108 },
roentgen b75cab
{ 0x0cc0, 5132 },
roentgen b75cab
{ 0x02c0, 6156 },
roentgen b75cab
{ 0x0ac0, 7180 },
roentgen b75cab
{ 0x06c0, 8205 },
roentgen b75cab
{ 0x16c0, 9229 },
roentgen b75cab
{ 0x0a40, 10253 },
roentgen b75cab
{ 0x1a40, 11277 },
roentgen b75cab
{ 0x0640, 12301 },
roentgen b75cab
{ 0x1640, 13325 },
roentgen b75cab
{ 0x09c0, 14349 },
roentgen b75cab
{ 0x19c0, 15373 },
roentgen b75cab
{ 0x05c0, 16397 },
roentgen b75cab
{ 0x15c0, 17421 },
roentgen b75cab
{ 0x0dc0, 18445 },
roentgen b75cab
{ 0x1dc0, 19469 },
roentgen b75cab
{ 0x0940, 20493 },
roentgen b75cab
{ 0x1940, 21517 },
roentgen b75cab
{ 0x0540, 22541 },
roentgen b75cab
{ 0x1540, 23565 },
roentgen b75cab
{ 0x0b40, 24589 },
roentgen b75cab
{ 0x1b40, 25613 },
roentgen b75cab
{ 0x04c0, 26637 },
roentgen b75cab
{ 0x14c0, 27661 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto MakeUp[] = {
roentgen b75cab
{ 0x0080, 28683 },
roentgen b75cab
{ 0x0180, 29707 },
roentgen b75cab
{ 0x0580, 30731 },
roentgen b75cab
{ 0x0480, 31756 },
roentgen b75cab
{ 0x0c80, 32780 },
roentgen b75cab
{ 0x0280, 33804 },
roentgen b75cab
{ 0x0a80, 34828 },
roentgen b75cab
{ 0x0680, 35852 },
roentgen b75cab
{ 0x0e80, 36876 },
roentgen b75cab
{ 0x0380, 37900 },
roentgen b75cab
{ 0x0b80, 38924 },
roentgen b75cab
{ 0x0780, 39948 },
roentgen b75cab
{ 0x0f80, 40972 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto TermW[] = {
roentgen b75cab
{ 0x00ac, 8 },
roentgen b75cab
{ 0x0038, 22 },
roentgen b75cab
{ 0x000e, 36 },
roentgen b75cab
{ 0x0001, 52 },
roentgen b75cab
{ 0x000d, 68 },
roentgen b75cab
{ 0x0003, 84 },
roentgen b75cab
{ 0x0007, 100 },
roentgen b75cab
{ 0x000f, 116 },
roentgen b75cab
{ 0x0019, 133 },
roentgen b75cab
{ 0x0005, 149 },
roentgen b75cab
{ 0x001c, 165 },
roentgen b75cab
{ 0x0002, 181 },
roentgen b75cab
{ 0x0004, 198 },
roentgen b75cab
{ 0x0030, 214 },
roentgen b75cab
{ 0x000b, 230 },
roentgen b75cab
{ 0x002b, 246 },
roentgen b75cab
{ 0x0015, 262 },
roentgen b75cab
{ 0x0035, 278 },
roentgen b75cab
{ 0x0072, 295 },
roentgen b75cab
{ 0x0018, 311 },
roentgen b75cab
{ 0x0008, 327 },
roentgen b75cab
{ 0x0074, 343 },
roentgen b75cab
{ 0x0060, 359 },
roentgen b75cab
{ 0x0010, 375 },
roentgen b75cab
{ 0x000a, 391 },
roentgen b75cab
{ 0x006a, 407 },
roentgen b75cab
{ 0x0064, 423 },
roentgen b75cab
{ 0x0012, 439 },
roentgen b75cab
{ 0x000c, 455 },
roentgen b75cab
{ 0x0040, 472 },
roentgen b75cab
{ 0x00c0, 488 },
roentgen b75cab
{ 0x0058, 504 },
roentgen b75cab
{ 0x00d8, 520 },
roentgen b75cab
{ 0x0048, 536 },
roentgen b75cab
{ 0x00c8, 552 },
roentgen b75cab
{ 0x0028, 568 },
roentgen b75cab
{ 0x00a8, 584 },
roentgen b75cab
{ 0x0068, 600 },
roentgen b75cab
{ 0x00e8, 616 },
roentgen b75cab
{ 0x0014, 632 },
roentgen b75cab
{ 0x0094, 648 },
roentgen b75cab
{ 0x0054, 664 },
roentgen b75cab
{ 0x00d4, 680 },
roentgen b75cab
{ 0x0034, 696 },
roentgen b75cab
{ 0x00b4, 712 },
roentgen b75cab
{ 0x0020, 728 },
roentgen b75cab
{ 0x00a0, 744 },
roentgen b75cab
{ 0x0050, 760 },
roentgen b75cab
{ 0x00d0, 776 },
roentgen b75cab
{ 0x004a, 792 },
roentgen b75cab
{ 0x00ca, 808 },
roentgen b75cab
{ 0x002a, 824 },
roentgen b75cab
{ 0x00aa, 840 },
roentgen b75cab
{ 0x0024, 856 },
roentgen b75cab
{ 0x00a4, 872 },
roentgen b75cab
{ 0x001a, 888 },
roentgen b75cab
{ 0x009a, 904 },
roentgen b75cab
{ 0x005a, 920 },
roentgen b75cab
{ 0x00da, 936 },
roentgen b75cab
{ 0x0052, 952 },
roentgen b75cab
{ 0x00d2, 968 },
roentgen b75cab
{ 0x004c, 984 },
roentgen b75cab
{ 0x00cc, 1000 },
roentgen b75cab
{ 0x002c, 1016 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto TermB[] = {
roentgen b75cab
{ 0x03b0, 10 },
roentgen b75cab
{ 0x0002, 19 },
roentgen b75cab
{ 0x0003, 34 },
roentgen b75cab
{ 0x0001, 50 },
roentgen b75cab
{ 0x0006, 67 },
roentgen b75cab
{ 0x000c, 84 },
roentgen b75cab
{ 0x0004, 100 },
roentgen b75cab
{ 0x0018, 117 },
roentgen b75cab
{ 0x0028, 134 },
roentgen b75cab
{ 0x0008, 150 },
roentgen b75cab
{ 0x0010, 167 },
roentgen b75cab
{ 0x0050, 183 },
roentgen b75cab
{ 0x0070, 199 },
roentgen b75cab
{ 0x0020, 216 },
roentgen b75cab
{ 0x00e0, 232 },
roentgen b75cab
{ 0x0030, 249 },
roentgen b75cab
{ 0x03a0, 266 },
roentgen b75cab
{ 0x0060, 282 },
roentgen b75cab
{ 0x0040, 298 },
roentgen b75cab
{ 0x0730, 315 },
roentgen b75cab
{ 0x00b0, 331 },
roentgen b75cab
{ 0x01b0, 347 },
roentgen b75cab
{ 0x0760, 363 },
roentgen b75cab
{ 0x00a0, 379 },
roentgen b75cab
{ 0x0740, 395 },
roentgen b75cab
{ 0x00c0, 411 },
roentgen b75cab
{ 0x0530, 428 },
roentgen b75cab
{ 0x0d30, 444 },
roentgen b75cab
{ 0x0330, 460 },
roentgen b75cab
{ 0x0b30, 476 },
roentgen b75cab
{ 0x0160, 492 },
roentgen b75cab
{ 0x0960, 508 },
roentgen b75cab
{ 0x0560, 524 },
roentgen b75cab
{ 0x0d60, 540 },
roentgen b75cab
{ 0x04b0, 556 },
roentgen b75cab
{ 0x0cb0, 572 },
roentgen b75cab
{ 0x02b0, 588 },
roentgen b75cab
{ 0x0ab0, 604 },
roentgen b75cab
{ 0x06b0, 620 },
roentgen b75cab
{ 0x0eb0, 636 },
roentgen b75cab
{ 0x0360, 652 },
roentgen b75cab
{ 0x0b60, 668 },
roentgen b75cab
{ 0x05b0, 684 },
roentgen b75cab
{ 0x0db0, 700 },
roentgen b75cab
{ 0x02a0, 716 },
roentgen b75cab
{ 0x0aa0, 732 },
roentgen b75cab
{ 0x06a0, 748 },
roentgen b75cab
{ 0x0ea0, 764 },
roentgen b75cab
{ 0x0260, 780 },
roentgen b75cab
{ 0x0a60, 796 },
roentgen b75cab
{ 0x04a0, 812 },
roentgen b75cab
{ 0x0ca0, 828 },
roentgen b75cab
{ 0x0240, 844 },
roentgen b75cab
{ 0x0ec0, 860 },
roentgen b75cab
{ 0x01c0, 876 },
roentgen b75cab
{ 0x0e40, 892 },
roentgen b75cab
{ 0x0140, 908 },
roentgen b75cab
{ 0x01a0, 924 },
roentgen b75cab
{ 0x09a0, 940 },
roentgen b75cab
{ 0x0d40, 956 },
roentgen b75cab
{ 0x0340, 972 },
roentgen b75cab
{ 0x05a0, 988 },
roentgen b75cab
{ 0x0660, 1004 },
roentgen b75cab
{ 0x0e60, 1020 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static struct proto EOLH[] = {
roentgen b75cab
{ 0x0000, 11 },
roentgen b75cab
{ 0, 0 }
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
FillTable(TIFFFaxTabEnt *T, int Size, struct proto *P, int State)
roentgen b75cab
{
roentgen b75cab
    int limit = 1 << Size;
roentgen b75cab
roentgen b75cab
    while (P->val) {
roentgen b75cab
	int width = P->val & 15;
roentgen b75cab
	int param = P->val >> 4;
roentgen b75cab
	int incr = 1 << width;
roentgen b75cab
	int code;
roentgen b75cab
	for (code = P->code; code < limit; code += incr) {
roentgen b75cab
	    TIFFFaxTabEnt *E = T+code;
roentgen b75cab
	    E->State = State;
roentgen b75cab
	    E->Width = width;
roentgen b75cab
	    E->Param = param;
roentgen b75cab
	}
roentgen b75cab
	P++;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static	char* storage_class = "";
roentgen b75cab
static	char* const_class = "";
roentgen b75cab
static	int packoutput = 1;
roentgen b75cab
static	char* prebrace = "";
roentgen b75cab
static	char* postbrace = "";
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
WriteTable(FILE* fd, const TIFFFaxTabEnt* T, int Size, const char* name)
roentgen b75cab
{
roentgen b75cab
    int i;
roentgen b75cab
    char* sep;
roentgen b75cab
roentgen b75cab
    fprintf(fd, "%s %s TIFFFaxTabEnt %s[%d] = {",
roentgen b75cab
	storage_class, const_class, name, Size);
roentgen b75cab
    if (packoutput) {
roentgen b75cab
	sep = "\n";
roentgen b75cab
	for (i = 0; i < Size; i++) {
roentgen b75cab
	    fprintf(fd, "%s%s%d,%d,%d%s",
roentgen b75cab
		sep, prebrace, T->State, T->Width, (int) T->Param, postbrace);
roentgen b75cab
	    if (((i+1) % 10) == 0)
roentgen b75cab
		    sep = ",\n";
roentgen b75cab
	    else
roentgen b75cab
		    sep = ",";
roentgen b75cab
	    T++;
roentgen b75cab
	}
roentgen b75cab
    } else {
roentgen b75cab
	sep = "\n ";
roentgen b75cab
	for (i = 0; i < Size; i++) {
roentgen b75cab
	    fprintf(fd, "%s%s%3d,%3d,%4d%s",
roentgen b75cab
		sep, prebrace, T->State, T->Width, (int) T->Param, postbrace);
roentgen b75cab
	    if (((i+1) % 6) == 0)
roentgen b75cab
		    sep = ",\n ";
roentgen b75cab
	    else
roentgen b75cab
		    sep = ",";
roentgen b75cab
	    T++;
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
    fprintf(fd, "\n};\n");
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* initialise the huffman code tables */
roentgen b75cab
int
roentgen b75cab
main(int argc, char* argv[])
roentgen b75cab
{
roentgen b75cab
    FILE* fd;
roentgen b75cab
    char* outputfile;
roentgen b75cab
    int c;
roentgen b75cab
    extern int optind;
roentgen b75cab
    extern char* optarg;
roentgen b75cab
roentgen b75cab
    while ((c = getopt(argc, argv, "c:s:bp")) != -1)
roentgen b75cab
	switch (c) {
roentgen b75cab
	case 'c':
roentgen b75cab
	    const_class = optarg;
roentgen b75cab
	    break;
roentgen b75cab
	case 's':
roentgen b75cab
	    storage_class = optarg;
roentgen b75cab
	    break;
roentgen b75cab
	case 'p':
roentgen b75cab
	    packoutput = 0;
roentgen b75cab
	    break;
roentgen b75cab
	case 'b':
roentgen b75cab
	    prebrace = "{";
roentgen b75cab
	    postbrace = "}";
roentgen b75cab
	    break;
roentgen b75cab
	case '?':
roentgen b75cab
	    fprintf(stderr,
roentgen b75cab
		"usage: %s [-c const] [-s storage] [-p] [-b] file\n",
roentgen b75cab
		argv[0]);
roentgen b75cab
	    return (-1);
roentgen b75cab
	}
roentgen b75cab
    outputfile = optind < argc ? argv[optind] : "g3states.h";
roentgen b75cab
    fd = fopen(outputfile, "w");
roentgen b75cab
    if (fd == NULL) {
roentgen b75cab
	fprintf(stderr, "%s: %s: Cannot create output file.\n",
roentgen b75cab
	    argv[0], outputfile);
roentgen b75cab
	return (-2);
roentgen b75cab
    }
roentgen b75cab
    FillTable(MainTable, 7, Pass, S_Pass);
roentgen b75cab
    FillTable(MainTable, 7, Horiz, S_Horiz);
roentgen b75cab
    FillTable(MainTable, 7, V0, S_V0);
roentgen b75cab
    FillTable(MainTable, 7, VR, S_VR);
roentgen b75cab
    FillTable(MainTable, 7, VL, S_VL);
roentgen b75cab
    FillTable(MainTable, 7, Ext, S_Ext);
roentgen b75cab
    FillTable(MainTable, 7, EOLV, S_EOL);
roentgen b75cab
    FillTable(WhiteTable, 12, MakeUpW, S_MakeUpW);
roentgen b75cab
    FillTable(WhiteTable, 12, MakeUp, S_MakeUp);
roentgen b75cab
    FillTable(WhiteTable, 12, TermW, S_TermW);
roentgen b75cab
    FillTable(WhiteTable, 12, EOLH, S_EOL);
roentgen b75cab
    FillTable(BlackTable, 13, MakeUpB, S_MakeUpB);
roentgen b75cab
    FillTable(BlackTable, 13, MakeUp, S_MakeUp);
roentgen b75cab
    FillTable(BlackTable, 13, TermB, S_TermB);
roentgen b75cab
    FillTable(BlackTable, 13, EOLH, S_EOL);
roentgen b75cab
roentgen b75cab
    fprintf(fd, "/* WARNING, this file was automatically generated by the\n");
roentgen b75cab
    fprintf(fd, "    mkg3states program */\n");
roentgen b75cab
    fprintf(fd, "#include \"tiff.h\"\n");
roentgen b75cab
    fprintf(fd, "#include \"tif_fax3.h\"\n");
roentgen b75cab
    WriteTable(fd, MainTable, 128, "TIFFFaxMainTable");
roentgen b75cab
    WriteTable(fd, WhiteTable, 4096, "TIFFFaxWhiteTable");
roentgen b75cab
    WriteTable(fd, BlackTable, 8192, "TIFFFaxBlackTable");
roentgen b75cab
    fclose(fd);
roentgen b75cab
    return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */