t1_generate_luts.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
  15. * Copyright (c) 2012, Carl Hetherington
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #include "opj_includes.h"
  40. // defined elsewhere
  41. extern OPJ_BOOL vlc_init_tables();
  42. extern OPJ_BOOL vlc_tables_initialized;
  43. extern int vlc_tbl0[1024];
  44. extern int vlc_tbl1[1024];
  45. static int t1_init_ctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient)
  46. {
  47. int h, v, d, n, t, hv;
  48. n = 0;
  49. h = ((f & T1_SIGMA_3) != 0) + ((f & T1_SIGMA_5) != 0);
  50. v = ((f & T1_SIGMA_1) != 0) + ((f & T1_SIGMA_7) != 0);
  51. d = ((f & T1_SIGMA_0) != 0) + ((f & T1_SIGMA_2) != 0) + ((
  52. f & T1_SIGMA_8) != 0) + ((f & T1_SIGMA_6) != 0);
  53. switch (orient) {
  54. case 2:
  55. t = h;
  56. h = v;
  57. v = t;
  58. case 0:
  59. case 1:
  60. if (!h) {
  61. if (!v) {
  62. if (!d) {
  63. n = 0;
  64. } else if (d == 1) {
  65. n = 1;
  66. } else {
  67. n = 2;
  68. }
  69. } else if (v == 1) {
  70. n = 3;
  71. } else {
  72. n = 4;
  73. }
  74. } else if (h == 1) {
  75. if (!v) {
  76. if (!d) {
  77. n = 5;
  78. } else {
  79. n = 6;
  80. }
  81. } else {
  82. n = 7;
  83. }
  84. } else {
  85. n = 8;
  86. }
  87. break;
  88. case 3:
  89. hv = h + v;
  90. if (!d) {
  91. if (!hv) {
  92. n = 0;
  93. } else if (hv == 1) {
  94. n = 1;
  95. } else {
  96. n = 2;
  97. }
  98. } else if (d == 1) {
  99. if (!hv) {
  100. n = 3;
  101. } else if (hv == 1) {
  102. n = 4;
  103. } else {
  104. n = 5;
  105. }
  106. } else if (d == 2) {
  107. if (!hv) {
  108. n = 6;
  109. } else {
  110. n = 7;
  111. }
  112. } else {
  113. n = 8;
  114. }
  115. break;
  116. }
  117. return (T1_CTXNO_ZC + n);
  118. }
  119. static int t1_init_ctxno_sc(OPJ_UINT32 f)
  120. {
  121. int hc, vc, n;
  122. n = 0;
  123. hc = opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
  124. T1_LUT_SIG_E) + ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) == T1_LUT_SIG_W),
  125. 1) - opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
  126. (T1_LUT_SIG_E | T1_LUT_SGN_E)) +
  127. ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) ==
  128. (T1_LUT_SIG_W | T1_LUT_SGN_W)), 1);
  129. vc = opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
  130. T1_LUT_SIG_N) + ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) == T1_LUT_SIG_S),
  131. 1) - opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
  132. (T1_LUT_SIG_N | T1_LUT_SGN_N)) +
  133. ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) ==
  134. (T1_LUT_SIG_S | T1_LUT_SGN_S)), 1);
  135. if (hc < 0) {
  136. hc = -hc;
  137. vc = -vc;
  138. }
  139. if (!hc) {
  140. if (vc == -1) {
  141. n = 1;
  142. } else if (!vc) {
  143. n = 0;
  144. } else {
  145. n = 1;
  146. }
  147. } else if (hc == 1) {
  148. if (vc == -1) {
  149. n = 2;
  150. } else if (!vc) {
  151. n = 3;
  152. } else {
  153. n = 4;
  154. }
  155. }
  156. return (T1_CTXNO_SC + n);
  157. }
  158. static int t1_init_spb(OPJ_UINT32 f)
  159. {
  160. int hc, vc, n;
  161. hc = opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
  162. T1_LUT_SIG_E) + ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) == T1_LUT_SIG_W),
  163. 1) - opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
  164. (T1_LUT_SIG_E | T1_LUT_SGN_E)) +
  165. ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) ==
  166. (T1_LUT_SIG_W | T1_LUT_SGN_W)), 1);
  167. vc = opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
  168. T1_LUT_SIG_N) + ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) == T1_LUT_SIG_S),
  169. 1) - opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
  170. (T1_LUT_SIG_N | T1_LUT_SGN_N)) +
  171. ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) ==
  172. (T1_LUT_SIG_S | T1_LUT_SGN_S)), 1);
  173. if (!hc && !vc) {
  174. n = 0;
  175. } else {
  176. n = (!(hc > 0 || (!hc && vc > 0)));
  177. }
  178. return n;
  179. }
  180. static void dump_array16(int array[], int size)
  181. {
  182. int i;
  183. --size;
  184. for (i = 0; i < size; ++i) {
  185. printf("0x%04x,", array[i]);
  186. if (!((i + 1) & 0x7)) {
  187. printf("\n ");
  188. } else {
  189. printf(" ");
  190. }
  191. }
  192. printf("0x%04x\n};\n\n", array[size]);
  193. }
  194. int main(int argc, char **argv)
  195. {
  196. unsigned int i, j;
  197. double u, v, t;
  198. int lut_ctxno_zc[2048];
  199. int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];
  200. int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
  201. int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
  202. int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
  203. (void)argc;
  204. (void)argv;
  205. printf("/* This file was automatically generated by t1_generate_luts.c */\n\n");
  206. /* lut_ctxno_zc */
  207. for (j = 0; j < 4; ++j) {
  208. for (i = 0; i < 512; ++i) {
  209. OPJ_UINT32 orient = j;
  210. if (orient == 2) {
  211. orient = 1;
  212. } else if (orient == 1) {
  213. orient = 2;
  214. }
  215. lut_ctxno_zc[(orient << 9) | i] = t1_init_ctxno_zc(i, j);
  216. }
  217. }
  218. printf("static const OPJ_BYTE lut_ctxno_zc[2048] = {\n ");
  219. for (i = 0; i < 2047; ++i) {
  220. printf("%i,", lut_ctxno_zc[i]);
  221. if (!((i + 1) & 0x1f)) {
  222. printf("\n ");
  223. } else {
  224. printf(" ");
  225. }
  226. }
  227. printf("%i\n};\n\n", lut_ctxno_zc[2047]);
  228. /* lut_ctxno_sc */
  229. printf("static const OPJ_BYTE lut_ctxno_sc[256] = {\n ");
  230. for (i = 0; i < 255; ++i) {
  231. printf("0x%x,", t1_init_ctxno_sc(i));
  232. if (!((i + 1) & 0xf)) {
  233. printf("\n ");
  234. } else {
  235. printf(" ");
  236. }
  237. }
  238. printf("0x%x\n};\n\n", t1_init_ctxno_sc(255));
  239. /* lut_spb */
  240. printf("static const OPJ_BYTE lut_spb[256] = {\n ");
  241. for (i = 0; i < 255; ++i) {
  242. printf("%i,", t1_init_spb(i));
  243. if (!((i + 1) & 0x1f)) {
  244. printf("\n ");
  245. } else {
  246. printf(" ");
  247. }
  248. }
  249. printf("%i\n};\n\n", t1_init_spb(255));
  250. /* FIXME FIXME FIXME */
  251. /* fprintf(stdout,"nmsedec luts:\n"); */
  252. for (i = 0U; i < (1U << T1_NMSEDEC_BITS); ++i) {
  253. t = i / pow(2, T1_NMSEDEC_FRACBITS);
  254. u = t;
  255. v = t - 1.5;
  256. lut_nmsedec_sig[i] =
  257. opj_int_max(0,
  258. (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
  259. T1_NMSEDEC_FRACBITS) * 8192.0));
  260. lut_nmsedec_sig0[i] =
  261. opj_int_max(0,
  262. (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
  263. T1_NMSEDEC_FRACBITS) * 8192.0));
  264. u = t - 1.0;
  265. if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
  266. v = t - 1.5;
  267. } else {
  268. v = t - 0.5;
  269. }
  270. lut_nmsedec_ref[i] =
  271. opj_int_max(0,
  272. (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
  273. T1_NMSEDEC_FRACBITS) * 8192.0));
  274. lut_nmsedec_ref0[i] =
  275. opj_int_max(0,
  276. (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
  277. T1_NMSEDEC_FRACBITS) * 8192.0));
  278. }
  279. printf("static const OPJ_INT16 lut_nmsedec_sig[1U << T1_NMSEDEC_BITS] = {\n ");
  280. dump_array16(lut_nmsedec_sig, 1U << T1_NMSEDEC_BITS);
  281. printf("static const OPJ_INT16 lut_nmsedec_sig0[1U << T1_NMSEDEC_BITS] = {\n ");
  282. dump_array16(lut_nmsedec_sig0, 1U << T1_NMSEDEC_BITS);
  283. printf("static const OPJ_INT16 lut_nmsedec_ref[1U << T1_NMSEDEC_BITS] = {\n ");
  284. dump_array16(lut_nmsedec_ref, 1U << T1_NMSEDEC_BITS);
  285. printf("static const OPJ_INT16 lut_nmsedec_ref0[1U << T1_NMSEDEC_BITS] = {\n ");
  286. dump_array16(lut_nmsedec_ref0, 1U << T1_NMSEDEC_BITS);
  287. vlc_tables_initialized = vlc_init_tables();
  288. printf("static const OPJ_UINT16 vlc_tbl0[1024] = {\n ");
  289. dump_array16(vlc_tbl0, 1024);
  290. printf("static const OPJ_UINT16 vlc_tbl1[1024] = {\n ");
  291. dump_array16(vlc_tbl1, 1024);
  292. return 0;
  293. }