scfdgen.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Copyright (C) 1992, 1994, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: scfdgen.c,v 1.2 2000/09/19 19:00:48 lpd Exp $ */
  16. /* Generate the CCITTFaxDecode tables */
  17. #include "stdio_.h" /* includes std.h */
  18. #include "scf.h"
  19. #include "malloc_.h"
  20. #include "memory_.h"
  21. typedef void (*cfd_node_proc) (P6(cfd_node *, cfd_node *,
  22. uint, int, int, int));
  23. typedef void (*cfd_enum_proc) (P4(cfd_node_proc,
  24. cfd_node *, cfd_node *, int));
  25. private void cfd_build_tree(P4(cfd_node *, cfd_enum_proc, int, FILE *));
  26. private void cfd_enumerate_white(P4(cfd_node_proc,
  27. cfd_node *, cfd_node *, int));
  28. private void cfd_enumerate_black(P4(cfd_node_proc,
  29. cfd_node *, cfd_node *, int));
  30. private void cfd_enumerate_2d(P4(cfd_node_proc,
  31. cfd_node *, cfd_node *, int));
  32. private void cfd_enumerate_uncompressed(P4(cfd_node_proc,
  33. cfd_node *, cfd_node *, int));
  34. main()
  35. {
  36. FILE *out = fopen("scfdtab.c", "w");
  37. cfd_node area[1 << max(cfd_white_initial_bits, cfd_black_initial_bits)];
  38. fputs("/* Copyright (C) 1992, 1993, 1998, 1999 Aladdin Enterprises. All rights reserved. */\n\n", out);
  39. fputs("/* $Id: scfdgen.c,v 1.2 2000/09/19 19:00:48 lpd Exp $ */\n", out);
  40. fputs("/* Tables for CCITTFaxDecode filter. */\n\n", out);
  41. fputs("/* This file was generated automatically. It is governed by the same terms */\n", out);
  42. fputs("/* as the files scfetab.c and scfdgen.c from which it was derived. */\n", out);
  43. fputs("/* Consult those files for the licensing terms and conditions. */\n\n", out);
  44. fputs("#include \"std.h\"\n", out);
  45. fputs("#include \"scommon.h\"\t\t/* for scf.h */\n", out);
  46. fputs("#include \"scf.h\"\n\n", out);
  47. fputs("/* White decoding table. */\n", out);
  48. fputs("const cfd_node cf_white_decode[] = {\n", out);
  49. cfd_build_tree(area, cfd_enumerate_white, cfd_white_initial_bits, out);
  50. fputs("\n};\n\n", out);
  51. fputs("/* Black decoding table. */\n", out);
  52. fputs("const cfd_node cf_black_decode[] = {\n", out);
  53. cfd_build_tree(area, cfd_enumerate_black, cfd_black_initial_bits, out);
  54. fputs("\n};\n\n", out);
  55. fputs("/* 2-D decoding table. */\n", out);
  56. fputs("const cfd_node cf_2d_decode[] = {\n", out);
  57. cfd_build_tree(area, cfd_enumerate_2d, cfd_2d_initial_bits, out);
  58. fputs("\n};\n\n", out);
  59. fputs("/* Uncompresssed decoding table. */\n", out);
  60. fputs("const cfd_node cf_uncompressed_decode[] = {\n", out);
  61. cfd_build_tree(area, cfd_enumerate_uncompressed, cfd_uncompressed_initial_bits, out);
  62. fputs("\n};\n\n", out);
  63. fputs("/* Dummy executable code to pacify compilers. */\n", out);
  64. fputs("void scfdtab_dummy(P0());\n", out);
  65. fputs("void\nscfdtab_dummy()\n{\n}\n", out);
  66. fclose(out);
  67. return 0;
  68. }
  69. /* Initialize first-level leaves, count second-level nodes. */
  70. private void
  71. cfd_count_nodes(cfd_node * tree, cfd_node * ignore_extn,
  72. uint code, int code_length, int run_length, int initial_bits)
  73. {
  74. if (code_length <= initial_bits) {
  75. /* Initialize one or more first-level leaves. */
  76. int sh = initial_bits - code_length;
  77. cfd_node *np = &tree[code << sh];
  78. int i;
  79. for (i = 1 << sh; i > 0; i--, np++)
  80. np->run_length = run_length,
  81. np->code_length = code_length;
  82. } else {
  83. /* Note the need for a second level. */
  84. cfd_node *np = &tree[code >> (code_length - initial_bits)];
  85. np->code_length = max(np->code_length, code_length);
  86. }
  87. }
  88. /* Initialize second-level nodes. */
  89. private void
  90. cfd_init2_nodes(cfd_node * tree, cfd_node * extn,
  91. uint code, int code_length, int run_length, int initial_bits)
  92. {
  93. int xbits = code_length - initial_bits;
  94. int xrep;
  95. cfd_node *np1, *np2;
  96. int i;
  97. if (xbits <= 0)
  98. return;
  99. np1 = &tree[code >> xbits];
  100. np2 = &extn[np1->run_length - (1 << initial_bits)];
  101. xrep = np1->code_length - code_length;
  102. i = 1 << xrep;
  103. np2 += (code & ((1 << xbits) - 1)) * i;
  104. for (; i > 0; i--, np2++)
  105. np2->run_length = run_length,
  106. np2->code_length = xbits;
  107. }
  108. /* Enumerate all the relevant white or black codes. */
  109. private void
  110. cfd_enumerate_codes(cfd_node_proc proc, cfd_node * tree, cfd_node * extn,
  111. int initial_bits, const cfe_run * tt, const cfe_run * mut)
  112. {
  113. int i;
  114. const cfe_run *ep;
  115. for (i = 0, ep = tt; i < 64; i++, ep++)
  116. (*proc) (tree, extn, ep->code, ep->code_length, i, initial_bits);
  117. for (i = 1, ep = mut + 1; i < 41; i++, ep++)
  118. (*proc) (tree, extn, ep->code, ep->code_length, i << 6, initial_bits);
  119. (*proc) (tree, extn,
  120. cf1_run_uncompressed.code, cf1_run_uncompressed.code_length,
  121. run_uncompressed, initial_bits);
  122. (*proc) (tree, extn,
  123. 0, run_eol_code_length - 1,
  124. run_zeros, initial_bits);
  125. }
  126. private void
  127. cfd_enumerate_white(cfd_node_proc proc, cfd_node * tree, cfd_node * extn,
  128. int initial_bits)
  129. {
  130. cfd_enumerate_codes(proc, tree, extn, initial_bits,
  131. cf_white_runs.termination, cf_white_runs.make_up);
  132. }
  133. private void
  134. cfd_enumerate_black(cfd_node_proc proc, cfd_node * tree, cfd_node * extn,
  135. int initial_bits)
  136. {
  137. cfd_enumerate_codes(proc, tree, extn, initial_bits,
  138. cf_black_runs.termination, cf_black_runs.make_up);
  139. }
  140. /* Enumerate the 2-D codes. */
  141. private void
  142. cfd_enumerate_2d(cfd_node_proc proc, cfd_node * tree, cfd_node * extn,
  143. int initial_bits)
  144. {
  145. int i;
  146. const cfe_run *ep;
  147. (*proc) (tree, extn, cf2_run_pass.code, cf2_run_pass.code_length,
  148. run2_pass, initial_bits);
  149. (*proc) (tree, extn, cf2_run_horizontal.code, cf2_run_horizontal.code_length,
  150. run2_horizontal, initial_bits);
  151. for (i = 0; i < countof(cf2_run_vertical); i++) {
  152. ep = &cf2_run_vertical[i];
  153. (*proc) (tree, extn, ep->code, ep->code_length, i, initial_bits);
  154. }
  155. (*proc) (tree, extn, cf2_run_uncompressed.code, cf2_run_uncompressed.code_length,
  156. run_uncompressed, initial_bits);
  157. (*proc) (tree, extn, 0, run_eol_code_length - 1, run_zeros, initial_bits);
  158. }
  159. /* Enumerate the uncompressed codes. */
  160. private void
  161. cfd_enumerate_uncompressed(cfd_node_proc proc, cfd_node * tree, cfd_node * extn,
  162. int initial_bits)
  163. {
  164. int i;
  165. const cfe_run *ep;
  166. for (i = 0; i < countof(cf_uncompressed); i++) {
  167. ep = &cf_uncompressed[i];
  168. (*proc) (tree, extn, ep->code, ep->code_length, i, initial_bits);
  169. }
  170. for (i = 0; i < countof(cf_uncompressed_exit); i++) {
  171. ep = &cf_uncompressed_exit[i];
  172. (*proc) (tree, extn, ep->code, ep->code_length, i, initial_bits);
  173. }
  174. }
  175. /* Build and write out the table. */
  176. private void
  177. cfd_build_tree(cfd_node * tree, cfd_enum_proc enum_proc, int initial_bits,
  178. FILE * f)
  179. {
  180. cfd_node *np;
  181. const char *prev = "";
  182. int i, next;
  183. cfd_node *extn;
  184. memset(tree, 0, sizeof(cfd_node) << initial_bits);
  185. /* Construct and write the first level of the tree. */
  186. (*enum_proc) (cfd_count_nodes, tree, (cfd_node *) 0, initial_bits);
  187. next = 0;
  188. for (i = 0, np = tree; i < 1 << initial_bits; i++, np++) {
  189. if (np->code_length > initial_bits) { /* second level needed */
  190. np->run_length = next + (1 << initial_bits);
  191. next += 1 << (np->code_length - initial_bits);
  192. }
  193. fprintf(f, "%s\t{ %d, %d }", prev, np->run_length, np->code_length);
  194. prev = ",\n";
  195. }
  196. /* Construct and write the second level. */
  197. extn = (cfd_node *) malloc(sizeof(cfd_node) * next);
  198. for (i = 0, np = extn; i < next; i++, np++)
  199. np->run_length = run_error,
  200. np->code_length = 0;
  201. (*enum_proc) (cfd_init2_nodes, tree, extn, initial_bits);
  202. for (i = 0, np = extn; i < next; i++, np++)
  203. fprintf(f, ",\n\t{ %d, %d }", np->run_length, np->code_length);
  204. free((char *)extn);
  205. }