2
0

mkg3states.c 9.3 KB

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