home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsf / libtiff / c / mkg3states < prev    next >
Encoding:
Text File  |  1995-10-12  |  9.0 KB  |  428 lines

  1. /* "$Header: /usr/people/sam/tiff/libtiff/RCS/mkg3states.c,v 1.32 1995/10/10 00:13:13 sam Exp $ */
  2.  
  3. /*
  4.  * Copyright (c) 1991-1995 Sam Leffler
  5.  * Copyright (c) 1991-1995 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.  
  337. void
  338. WriteTable(FILE* fd, const TIFFFaxTabEnt* T, int Size, const char* name)
  339. {
  340.     int i;
  341.     char* sep;
  342.  
  343.     fprintf(fd, "%s %s TIFFFaxTabEnt %s[%d] = {",
  344.     storage_class, const_class, name, Size);
  345.     if (packoutput) {
  346.     sep = "\n";
  347.     for (i = 0; i < Size; i++) {
  348.         fprintf(fd, "%s%d,%d,%d", sep, T->State, T->Width, T->Param);
  349.         if (((i+1) % 12) == 0)
  350.             sep = ",\n";
  351.         else
  352.             sep = ",";
  353.         T++;
  354.     }
  355.     } else {
  356.     sep = "\n ";
  357.     for (i = 0; i < Size; i++) {
  358.         fprintf(fd, "%s%3d,%3d,%4d", sep, T->State, T->Width, T->Param);
  359.         if (((i+1) % 6) == 0)
  360.             sep = ",\n ";
  361.         else
  362.             sep = ",";
  363.         T++;
  364.     }
  365.     }
  366.     fprintf(fd, "\n};\n");
  367. }
  368.  
  369. /* initialise the huffman code tables */
  370. int
  371. main(int argc, char* argv[])
  372. {
  373.     FILE* fd;
  374.     char* outputfile;
  375.     int c;
  376.     extern int optind;
  377.     extern char* optarg;
  378.  
  379.     while ((c = getopt(argc, argv, "c:s:p")) != -1)
  380.     switch (c) {
  381.     case 'c':
  382.         const_class = optarg;
  383.         break;
  384.     case 's':
  385.         storage_class = optarg;
  386.         break;
  387.     case 'p':
  388.         packoutput = 0;
  389.         break;
  390.     case '?':
  391.         fprintf(stderr, "usage: %s [-c const] [-s storage] [-p] file\n",
  392.         argv[0]);
  393.         return (-1);
  394.     }
  395.     outputfile = optind < argc ? argv[optind] : "g3states.h";
  396.     fd = fopen(outputfile, "w");
  397.     if (fd == NULL) {
  398.     fprintf(stderr, "%s: %s: Cannot create output file.\n",
  399.         argv[0], outputfile);
  400.     return (-2);
  401.     }
  402.     FillTable(MainTable, 7, Pass, S_Pass);
  403.     FillTable(MainTable, 7, Horiz, S_Horiz);
  404.     FillTable(MainTable, 7, V0, S_V0);
  405.     FillTable(MainTable, 7, VR, S_VR);
  406.     FillTable(MainTable, 7, VL, S_VL);
  407.     FillTable(MainTable, 7, Ext, S_Ext);
  408.     FillTable(MainTable, 7, EOLV, S_EOL);
  409.     FillTable(WhiteTable, 12, MakeUpW, S_MakeUpW);
  410.     FillTable(WhiteTable, 12, MakeUp, S_MakeUp);
  411.     FillTable(WhiteTable, 12, TermW, S_TermW);
  412.     FillTable(WhiteTable, 12, EOLH, S_EOL);
  413.     FillTable(BlackTable, 13, MakeUpB, S_MakeUpB);
  414.     FillTable(BlackTable, 13, MakeUp, S_MakeUp);
  415.     FillTable(BlackTable, 13, TermB, S_TermB);
  416.     FillTable(BlackTable, 13, EOLH, S_EOL);
  417.  
  418.     fprintf(fd, "/* WARNING, this file was automatically generated by the\n");
  419.     fprintf(fd, "    mkg3states program */\n");
  420.     fprintf(fd, "#include \"tiff.h\"\n");
  421.     fprintf(fd, "#include \"tif_fax3.h\"\n");
  422.     WriteTable(fd, MainTable, 128, "TIFFFaxMainTable");
  423.     WriteTable(fd, WhiteTable, 4096, "TIFFFaxWhiteTable");
  424.     WriteTable(fd, BlackTable, 8192, "TIFFFaxBlackTable");
  425.     fclose(fd);
  426.     return (0);
  427. }
  428.