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