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 <[email protected]>
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
40 #include "opj_includes.h"
41
42 // defined elsewhere
43 extern OPJ_BOOL vlc_init_tables();
44 extern OPJ_BOOL vlc_tables_initialized;
45 extern int vlc_tbl0[1024];
46 extern int vlc_tbl1[1024];
47
t1_init_ctxno_zc(OPJ_UINT32 f,OPJ_UINT32 orient)48 static int t1_init_ctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient)
49 {
50 int h, v, d, n, t, hv;
51 n = 0;
52 h = ((f & T1_SIGMA_3) != 0) + ((f & T1_SIGMA_5) != 0);
53 v = ((f & T1_SIGMA_1) != 0) + ((f & T1_SIGMA_7) != 0);
54 d = ((f & T1_SIGMA_0) != 0) + ((f & T1_SIGMA_2) != 0) + ((
55 f & T1_SIGMA_8) != 0) + ((f & T1_SIGMA_6) != 0);
56
57 switch (orient) {
58 case 2:
59 t = h;
60 h = v;
61 v = t;
62 case 0:
63 case 1:
64 if (!h) {
65 if (!v) {
66 if (!d) {
67 n = 0;
68 } else if (d == 1) {
69 n = 1;
70 } else {
71 n = 2;
72 }
73 } else if (v == 1) {
74 n = 3;
75 } else {
76 n = 4;
77 }
78 } else if (h == 1) {
79 if (!v) {
80 if (!d) {
81 n = 5;
82 } else {
83 n = 6;
84 }
85 } else {
86 n = 7;
87 }
88 } else {
89 n = 8;
90 }
91 break;
92 case 3:
93 hv = h + v;
94 if (!d) {
95 if (!hv) {
96 n = 0;
97 } else if (hv == 1) {
98 n = 1;
99 } else {
100 n = 2;
101 }
102 } else if (d == 1) {
103 if (!hv) {
104 n = 3;
105 } else if (hv == 1) {
106 n = 4;
107 } else {
108 n = 5;
109 }
110 } else if (d == 2) {
111 if (!hv) {
112 n = 6;
113 } else {
114 n = 7;
115 }
116 } else {
117 n = 8;
118 }
119 break;
120 }
121
122 return (T1_CTXNO_ZC + n);
123 }
124
t1_init_ctxno_sc(OPJ_UINT32 f)125 static int t1_init_ctxno_sc(OPJ_UINT32 f)
126 {
127 int hc, vc, n;
128 n = 0;
129
130 hc = opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
131 T1_LUT_SIG_E) + ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) == T1_LUT_SIG_W),
132 1) - opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
133 (T1_LUT_SIG_E | T1_LUT_SGN_E)) +
134 ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) ==
135 (T1_LUT_SIG_W | T1_LUT_SGN_W)), 1);
136
137 vc = opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
138 T1_LUT_SIG_N) + ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) == T1_LUT_SIG_S),
139 1) - opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
140 (T1_LUT_SIG_N | T1_LUT_SGN_N)) +
141 ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) ==
142 (T1_LUT_SIG_S | T1_LUT_SGN_S)), 1);
143
144 if (hc < 0) {
145 hc = -hc;
146 vc = -vc;
147 }
148 if (!hc) {
149 if (vc == -1) {
150 n = 1;
151 } else if (!vc) {
152 n = 0;
153 } else {
154 n = 1;
155 }
156 } else if (hc == 1) {
157 if (vc == -1) {
158 n = 2;
159 } else if (!vc) {
160 n = 3;
161 } else {
162 n = 4;
163 }
164 }
165
166 return (T1_CTXNO_SC + n);
167 }
168
t1_init_spb(OPJ_UINT32 f)169 static int t1_init_spb(OPJ_UINT32 f)
170 {
171 int hc, vc, n;
172
173 hc = opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
174 T1_LUT_SIG_E) + ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) == T1_LUT_SIG_W),
175 1) - opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
176 (T1_LUT_SIG_E | T1_LUT_SGN_E)) +
177 ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) ==
178 (T1_LUT_SIG_W | T1_LUT_SGN_W)), 1);
179
180 vc = opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
181 T1_LUT_SIG_N) + ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) == T1_LUT_SIG_S),
182 1) - opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
183 (T1_LUT_SIG_N | T1_LUT_SGN_N)) +
184 ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) ==
185 (T1_LUT_SIG_S | T1_LUT_SGN_S)), 1);
186
187 if (!hc && !vc) {
188 n = 0;
189 } else {
190 n = (!(hc > 0 || (!hc && vc > 0)));
191 }
192
193 return n;
194 }
195
dump_array16(int array[],int size)196 static void dump_array16(int array[], int size)
197 {
198 int i;
199 --size;
200 for (i = 0; i < size; ++i) {
201 printf("0x%04x,", array[i]);
202 if (!((i + 1) & 0x7)) {
203 printf("\n ");
204 } else {
205 printf(" ");
206 }
207 }
208 printf("0x%04x\n};\n\n", array[size]);
209 }
210
main(int argc,char ** argv)211 int main(int argc, char **argv)
212 {
213 unsigned int i, j;
214 double u, v, t;
215
216 int lut_ctxno_zc[2048];
217 int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];
218 int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
219 int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
220 int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
221 (void)argc;
222 (void)argv;
223
224 printf("/* This file was automatically generated by t1_generate_luts.c */\n\n");
225
226 /* lut_ctxno_zc */
227 for (j = 0; j < 4; ++j) {
228 for (i = 0; i < 512; ++i) {
229 OPJ_UINT32 orient = j;
230 if (orient == 2) {
231 orient = 1;
232 } else if (orient == 1) {
233 orient = 2;
234 }
235 lut_ctxno_zc[(orient << 9) | i] = t1_init_ctxno_zc(i, j);
236 }
237 }
238
239 printf("static const OPJ_BYTE lut_ctxno_zc[2048] = {\n ");
240 for (i = 0; i < 2047; ++i) {
241 printf("%i,", lut_ctxno_zc[i]);
242 if (!((i + 1) & 0x1f)) {
243 printf("\n ");
244 } else {
245 printf(" ");
246 }
247 }
248 printf("%i\n};\n\n", lut_ctxno_zc[2047]);
249
250 /* lut_ctxno_sc */
251 printf("static const OPJ_BYTE lut_ctxno_sc[256] = {\n ");
252 for (i = 0; i < 255; ++i) {
253 printf("0x%x,", t1_init_ctxno_sc(i));
254 if (!((i + 1) & 0xf)) {
255 printf("\n ");
256 } else {
257 printf(" ");
258 }
259 }
260 printf("0x%x\n};\n\n", t1_init_ctxno_sc(255));
261
262 /* lut_spb */
263 printf("static const OPJ_BYTE lut_spb[256] = {\n ");
264 for (i = 0; i < 255; ++i) {
265 printf("%i,", t1_init_spb(i));
266 if (!((i + 1) & 0x1f)) {
267 printf("\n ");
268 } else {
269 printf(" ");
270 }
271 }
272 printf("%i\n};\n\n", t1_init_spb(255));
273
274 /* FIXME FIXME FIXME */
275 /* fprintf(stdout,"nmsedec luts:\n"); */
276 for (i = 0U; i < (1U << T1_NMSEDEC_BITS); ++i) {
277 t = i / pow(2, T1_NMSEDEC_FRACBITS);
278 u = t;
279 v = t - 1.5;
280 lut_nmsedec_sig[i] =
281 opj_int_max(0,
282 (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
283 T1_NMSEDEC_FRACBITS) * 8192.0));
284 lut_nmsedec_sig0[i] =
285 opj_int_max(0,
286 (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
287 T1_NMSEDEC_FRACBITS) * 8192.0));
288 u = t - 1.0;
289 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
290 v = t - 1.5;
291 } else {
292 v = t - 0.5;
293 }
294 lut_nmsedec_ref[i] =
295 opj_int_max(0,
296 (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
297 T1_NMSEDEC_FRACBITS) * 8192.0));
298 lut_nmsedec_ref0[i] =
299 opj_int_max(0,
300 (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
301 T1_NMSEDEC_FRACBITS) * 8192.0));
302 }
303
304 printf("static const OPJ_INT16 lut_nmsedec_sig[1U << T1_NMSEDEC_BITS] = {\n ");
305 dump_array16(lut_nmsedec_sig, 1U << T1_NMSEDEC_BITS);
306
307 printf("static const OPJ_INT16 lut_nmsedec_sig0[1U << T1_NMSEDEC_BITS] = {\n ");
308 dump_array16(lut_nmsedec_sig0, 1U << T1_NMSEDEC_BITS);
309
310 printf("static const OPJ_INT16 lut_nmsedec_ref[1U << T1_NMSEDEC_BITS] = {\n ");
311 dump_array16(lut_nmsedec_ref, 1U << T1_NMSEDEC_BITS);
312
313 printf("static const OPJ_INT16 lut_nmsedec_ref0[1U << T1_NMSEDEC_BITS] = {\n ");
314 dump_array16(lut_nmsedec_ref0, 1U << T1_NMSEDEC_BITS);
315
316 vlc_tables_initialized = vlc_init_tables();
317 printf("static const OPJ_UINT16 vlc_tbl0[1024] = {\n ");
318 dump_array16(vlc_tbl0, 1024);
319 printf("static const OPJ_UINT16 vlc_tbl1[1024] = {\n ");
320 dump_array16(vlc_tbl1, 1024);
321
322 return 0;
323 }
324