t1_generate_luts.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * Copyright (c) 2007, Callum Lerwick <[email protected]>
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "opj_includes.h"
  33. #include <math.h>
  34. static int t1_init_ctxno_zc(int f, int orient) {
  35. int h, v, d, n, t, hv;
  36. n = 0;
  37. h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
  38. v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
  39. d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
  40. switch (orient) {
  41. case 2:
  42. t = h;
  43. h = v;
  44. v = t;
  45. case 0:
  46. case 1:
  47. if (!h) {
  48. if (!v) {
  49. if (!d)
  50. n = 0;
  51. else if (d == 1)
  52. n = 1;
  53. else
  54. n = 2;
  55. } else if (v == 1) {
  56. n = 3;
  57. } else {
  58. n = 4;
  59. }
  60. } else if (h == 1) {
  61. if (!v) {
  62. if (!d)
  63. n = 5;
  64. else
  65. n = 6;
  66. } else {
  67. n = 7;
  68. }
  69. } else
  70. n = 8;
  71. break;
  72. case 3:
  73. hv = h + v;
  74. if (!d) {
  75. if (!hv) {
  76. n = 0;
  77. } else if (hv == 1) {
  78. n = 1;
  79. } else {
  80. n = 2;
  81. }
  82. } else if (d == 1) {
  83. if (!hv) {
  84. n = 3;
  85. } else if (hv == 1) {
  86. n = 4;
  87. } else {
  88. n = 5;
  89. }
  90. } else if (d == 2) {
  91. if (!hv) {
  92. n = 6;
  93. } else {
  94. n = 7;
  95. }
  96. } else {
  97. n = 8;
  98. }
  99. break;
  100. }
  101. return (T1_CTXNO_ZC + n);
  102. }
  103. static int t1_init_ctxno_sc(int f) {
  104. int hc, vc, n;
  105. n = 0;
  106. hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
  107. T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
  108. 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
  109. (T1_SIG_E | T1_SGN_E)) +
  110. ((f & (T1_SIG_W | T1_SGN_W)) ==
  111. (T1_SIG_W | T1_SGN_W)), 1);
  112. vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
  113. T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
  114. 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
  115. (T1_SIG_N | T1_SGN_N)) +
  116. ((f & (T1_SIG_S | T1_SGN_S)) ==
  117. (T1_SIG_S | T1_SGN_S)), 1);
  118. if (hc < 0) {
  119. hc = -hc;
  120. vc = -vc;
  121. }
  122. if (!hc) {
  123. if (vc == -1)
  124. n = 1;
  125. else if (!vc)
  126. n = 0;
  127. else
  128. n = 1;
  129. } else if (hc == 1) {
  130. if (vc == -1)
  131. n = 2;
  132. else if (!vc)
  133. n = 3;
  134. else
  135. n = 4;
  136. }
  137. return (T1_CTXNO_SC + n);
  138. }
  139. static int t1_init_spb(int f) {
  140. int hc, vc, n;
  141. hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
  142. T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
  143. 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
  144. (T1_SIG_E | T1_SGN_E)) +
  145. ((f & (T1_SIG_W | T1_SGN_W)) ==
  146. (T1_SIG_W | T1_SGN_W)), 1);
  147. vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
  148. T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
  149. 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
  150. (T1_SIG_N | T1_SGN_N)) +
  151. ((f & (T1_SIG_S | T1_SGN_S)) ==
  152. (T1_SIG_S | T1_SGN_S)), 1);
  153. if (!hc && !vc)
  154. n = 0;
  155. else
  156. n = (!(hc > 0 || (!hc && vc > 0)));
  157. return n;
  158. }
  159. void dump_array16(int array[],int size){
  160. int i;
  161. --size;
  162. for (i = 0; i < size; ++i) {
  163. printf("0x%04x, ", array[i]);
  164. if(!((i+1)&0x7))
  165. printf("\n ");
  166. }
  167. printf("0x%04x\n};\n\n", array[size]);
  168. }
  169. int main(){
  170. int i, j;
  171. double u, v, t;
  172. int lut_ctxno_zc[1024];
  173. int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];
  174. int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
  175. int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
  176. int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
  177. printf("/* This file was automatically generated by t1_generate_luts.c */\n\n");
  178. // lut_ctxno_zc
  179. for (j = 0; j < 4; ++j) {
  180. for (i = 0; i < 256; ++i) {
  181. int orient = j;
  182. if (orient == 2) {
  183. orient = 1;
  184. } else if (orient == 1) {
  185. orient = 2;
  186. }
  187. lut_ctxno_zc[(orient << 8) | i] = t1_init_ctxno_zc(i, j);
  188. }
  189. }
  190. printf("static char lut_ctxno_zc[1024] = {\n ");
  191. for (i = 0; i < 1023; ++i) {
  192. printf("%i, ", lut_ctxno_zc[i]);
  193. if(!((i+1)&0x1f))
  194. printf("\n ");
  195. }
  196. printf("%i\n};\n\n", lut_ctxno_zc[1023]);
  197. // lut_ctxno_sc
  198. printf("static char lut_ctxno_sc[256] = {\n ");
  199. for (i = 0; i < 255; ++i) {
  200. printf("0x%x, ", t1_init_ctxno_sc(i << 4));
  201. if(!((i+1)&0xf))
  202. printf("\n ");
  203. }
  204. printf("0x%x\n};\n\n", t1_init_ctxno_sc(255 << 4));
  205. // lut_spb
  206. printf("static char lut_spb[256] = {\n ");
  207. for (i = 0; i < 255; ++i) {
  208. printf("%i, ", t1_init_spb(i << 4));
  209. if(!((i+1)&0x1f))
  210. printf("\n ");
  211. }
  212. printf("%i\n};\n\n", t1_init_spb(255 << 4));
  213. /* FIXME FIXME FIXME */
  214. /* fprintf(stdout,"nmsedec luts:\n"); */
  215. for (i = 0; i < (1 << T1_NMSEDEC_BITS); ++i) {
  216. t = i / pow(2, T1_NMSEDEC_FRACBITS);
  217. u = t;
  218. v = t - 1.5;
  219. lut_nmsedec_sig[i] =
  220. int_max(0,
  221. (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
  222. lut_nmsedec_sig0[i] =
  223. int_max(0,
  224. (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
  225. u = t - 1.0;
  226. if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
  227. v = t - 1.5;
  228. } else {
  229. v = t - 0.5;
  230. }
  231. lut_nmsedec_ref[i] =
  232. int_max(0,
  233. (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
  234. lut_nmsedec_ref0[i] =
  235. int_max(0,
  236. (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
  237. }
  238. printf("static short lut_nmsedec_sig[1 << T1_NMSEDEC_BITS] = {\n ");
  239. dump_array16(lut_nmsedec_sig, 1 << T1_NMSEDEC_BITS);
  240. printf("static short lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS] = {\n ");
  241. dump_array16(lut_nmsedec_sig0, 1 << T1_NMSEDEC_BITS);
  242. printf("static short lut_nmsedec_ref[1 << T1_NMSEDEC_BITS] = {\n ");
  243. dump_array16(lut_nmsedec_ref, 1 << T1_NMSEDEC_BITS);
  244. printf("static short lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS] = {\n ");
  245. dump_array16(lut_nmsedec_ref0, 1 << T1_NMSEDEC_BITS);
  246. return 0;
  247. }