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 <seg@haxxed.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
33 #include "opj_includes.h"
36 static int t1_init_dec_ctxno_zc(int f, int orient) {
37 int h, v, d, n, t, hv;
39 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
40 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
41 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
105 return (T1_CTXNO_ZC + n);
108 static int t1_init_enc_ctxno_zc(int f, int orient) {
109 int h, v, d, n, t, hv;
111 h = ((f & T1_SIGMA_3) != 0) + ((f & T1_SIGMA_5) != 0);
112 v = ((f & T1_SIGMA_1) != 0) + ((f & T1_SIGMA_7) != 0);
113 d = ((f & T1_SIGMA_0) != 0) + ((f & T1_SIGMA_2) != 0) + ((f & T1_SIGMA_8) != 0) + ((f & T1_SIGMA_6) != 0);
152 } else if (hv == 1) {
160 } else if (hv == 1) {
177 return (T1_CTXNO_ZC + n);
180 static int t1_init_dec_ctxno_sc(int f) {
184 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
185 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
186 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
187 (T1_SIG_E | T1_SGN_E)) +
188 ((f & (T1_SIG_W | T1_SGN_W)) ==
189 (T1_SIG_W | T1_SGN_W)), 1);
191 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
192 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
193 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
194 (T1_SIG_N | T1_SGN_N)) +
195 ((f & (T1_SIG_S | T1_SGN_S)) ==
196 (T1_SIG_S | T1_SGN_S)), 1);
209 } else if (hc == 1) {
218 return (T1_CTXNO_SC + n);
221 static int t1_init_enc_ctxno_sc(int f) {
225 hc = int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
226 T1_LUT_SIG_E) + ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) == T1_LUT_SIG_W),
227 1) - int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
228 (T1_LUT_SIG_E | T1_LUT_SGN_E)) +
229 ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) ==
230 (T1_LUT_SIG_W | T1_LUT_SGN_W)), 1);
232 vc = int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
233 T1_LUT_SIG_N) + ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) == T1_LUT_SIG_S),
234 1) - int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
235 (T1_LUT_SIG_N | T1_LUT_SGN_N)) +
236 ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) ==
237 (T1_LUT_SIG_S | T1_LUT_SGN_S)), 1);
250 } else if (hc == 1) {
259 return (T1_CTXNO_SC + n);
262 static int t1_init_dec_spb(int f) {
265 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
266 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
267 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
268 (T1_SIG_E | T1_SGN_E)) +
269 ((f & (T1_SIG_W | T1_SGN_W)) ==
270 (T1_SIG_W | T1_SGN_W)), 1);
272 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
273 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
274 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
275 (T1_SIG_N | T1_SGN_N)) +
276 ((f & (T1_SIG_S | T1_SGN_S)) ==
277 (T1_SIG_S | T1_SGN_S)), 1);
282 n = (!(hc > 0 || (!hc && vc > 0)));
287 static int t1_init_enc_spb(int f) {
290 hc = int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
291 T1_LUT_SIG_E) + ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) == T1_LUT_SIG_W),
292 1) - int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
293 (T1_LUT_SIG_E | T1_LUT_SGN_E)) +
294 ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) ==
295 (T1_LUT_SIG_W | T1_LUT_SGN_W)), 1);
297 vc = int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
298 T1_LUT_SIG_N) + ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) == T1_LUT_SIG_S),
299 1) - int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
300 (T1_LUT_SIG_N | T1_LUT_SGN_N)) +
301 ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) ==
302 (T1_LUT_SIG_S | T1_LUT_SGN_S)), 1);
307 n = (!(hc > 0 || (!hc && vc > 0)));
312 void dump_array16(int array[],int size){
315 for (i = 0; i < size; ++i) {
316 printf("0x%04x, ", array[i]);
320 printf("0x%04x\n};\n\n", array[size]);
327 int lut_dec_ctxno_zc[1024];
328 int lut_enc_ctxno_zc[2048];
329 int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];
330 int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
331 int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
332 int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
334 printf("/* This file was automatically generated by t1_generate_luts.c */\n\n");
336 /* lut_dec_ctxno_zc */
337 for (j = 0; j < 4; ++j) {
338 for (i = 0; i < 256; ++i) {
342 } else if (orient == 1) {
345 lut_dec_ctxno_zc[(orient << 8) | i] = t1_init_dec_ctxno_zc(i, j);
349 printf("static char lut_dec_ctxno_zc[1024] = {\n ");
350 for (i = 0; i < 1023; ++i) {
351 printf("%i, ", lut_dec_ctxno_zc[i]);
355 printf("%i\n};\n\n", lut_dec_ctxno_zc[1023]);
357 /* lut_enc_ctxno_zc */
358 for (j = 0; j < 4; ++j) {
359 for (i = 0; i < 512; ++i) {
363 } else if (orient == 1) {
366 lut_enc_ctxno_zc[(orient << 9) | i] = t1_init_enc_ctxno_zc(i, j);
370 printf("static char lut_enc_ctxno_zc[2048] = {\n ");
371 for (i = 0; i < 2047; ++i) {
372 printf("%i, ", lut_enc_ctxno_zc[i]);
376 printf("%i\n};\n\n", lut_enc_ctxno_zc[2047]);
378 /* lut_dec_ctxno_sc */
379 printf("static char lut_dec_ctxno_sc[256] = {\n ");
380 for (i = 0; i < 255; ++i) {
381 printf("0x%x, ", t1_init_dec_ctxno_sc(i << 4));
385 printf("0x%x\n};\n\n", t1_init_dec_ctxno_sc(255 << 4));
387 /* lut_enc_ctxno_sc */
388 printf("static char lut_enc_ctxno_sc[256] = {\n ");
389 for (i = 0; i < 255; ++i) {
390 printf("0x%x, ", t1_init_enc_ctxno_sc(i));
394 printf("0x%x\n};\n\n", t1_init_enc_ctxno_sc(255));
397 printf("static char lut_dec_spb[256] = {\n ");
398 for (i = 0; i < 255; ++i) {
399 printf("%i, ", t1_init_dec_spb(i << 4));
403 printf("%i\n};\n\n", t1_init_dec_spb(255 << 4));
406 printf("static char lut_enc_spb[256] = {\n ");
407 for (i = 0; i < 255; ++i) {
408 printf("%i, ", t1_init_enc_spb(i));
412 printf("%i\n};\n\n", t1_init_enc_spb(255));
414 /* FIXME FIXME FIXME */
415 /* fprintf(stdout,"nmsedec luts:\n"); */
416 for (i = 0; i < (1 << T1_NMSEDEC_BITS); ++i) {
417 t = i / pow(2, T1_NMSEDEC_FRACBITS);
422 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
423 lut_nmsedec_sig0[i] =
425 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
427 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
434 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
435 lut_nmsedec_ref0[i] =
437 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
440 printf("static short lut_nmsedec_sig[1 << T1_NMSEDEC_BITS] = {\n ");
441 dump_array16(lut_nmsedec_sig, 1 << T1_NMSEDEC_BITS);
443 printf("static short lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS] = {\n ");
444 dump_array16(lut_nmsedec_sig0, 1 << T1_NMSEDEC_BITS);
446 printf("static short lut_nmsedec_ref[1 << T1_NMSEDEC_BITS] = {\n ");
447 dump_array16(lut_nmsedec_ref, 1 << T1_NMSEDEC_BITS);
449 printf("static short lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS] = {\n ");
450 dump_array16(lut_nmsedec_ref0, 1 << T1_NMSEDEC_BITS);