Consolidated some calls to mqc_setcurctx. Cleaned up t1_generate_luts to output the...
[openjpeg.git] / libopenjpeg / t1_generate_luts.c
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 <seg@haxxed.com>
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
33 #include "opj_includes.h"
34 #include <math.h>
35
36 static int t1_init_ctxno_zc(int f, int orient) {
37         int h, v, d, n, t, hv;
38         n = 0;
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);
42
43         switch (orient) {
44                 case 2:
45                         t = h;
46                         h = v;
47                         v = t;
48                 case 0:
49                 case 1:
50                         if (!h) {
51                                 if (!v) {
52                                         if (!d)
53                                                 n = 0;
54                                         else if (d == 1)
55                                                 n = 1;
56                                         else
57                                                 n = 2;
58                                 } else if (v == 1) {
59                                         n = 3;
60                                 } else {
61                                         n = 4;
62                                 }
63                         } else if (h == 1) {
64                                 if (!v) {
65                                         if (!d)
66                                                 n = 5;
67                                         else
68                                                 n = 6;
69                                 } else {
70                                         n = 7;
71                                 }
72                         } else
73                                 n = 8;
74                         break;
75                 case 3:
76                         hv = h + v;
77                         if (!d) {
78                                 if (!hv) {
79                                         n = 0;
80                                 } else if (hv == 1) {
81                                         n = 1;
82                                 } else {
83                                         n = 2;
84                                 }
85                         } else if (d == 1) {
86                                 if (!hv) {
87                                         n = 3;
88                                 } else if (hv == 1) {
89                                         n = 4;
90                                 } else {
91                                         n = 5;
92                                 }
93                         } else if (d == 2) {
94                                 if (!hv) {
95                                         n = 6;
96                                 } else {
97                                         n = 7;
98                                 }
99                         } else {
100                                 n = 8;
101                         }
102                         break;
103         }
104
105         return (T1_CTXNO_ZC + n);
106 }
107
108 static int t1_init_ctxno_sc(int f) {
109         int hc, vc, n;
110         n = 0;
111
112         hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
113                                 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
114                         1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
115                                         (T1_SIG_E | T1_SGN_E)) +
116                                 ((f & (T1_SIG_W | T1_SGN_W)) ==
117                                  (T1_SIG_W | T1_SGN_W)), 1);
118
119         vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
120                                 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
121                         1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
122                                         (T1_SIG_N | T1_SGN_N)) +
123                                 ((f & (T1_SIG_S | T1_SGN_S)) ==
124                                  (T1_SIG_S | T1_SGN_S)), 1);
125
126         if (hc < 0) {
127                 hc = -hc;
128                 vc = -vc;
129         }
130         if (!hc) {
131                 if (vc == -1)
132                         n = 1;
133                 else if (!vc)
134                         n = 0;
135                 else
136                         n = 1;
137         } else if (hc == 1) {
138                 if (vc == -1)
139                         n = 2;
140                 else if (!vc)
141                         n = 3;
142                 else
143                         n = 4;
144         }
145
146         return (T1_CTXNO_SC + n);
147 }
148
149 static int t1_init_ctxno_mag(int f) {
150         int n;
151         if (!(f & T1_REFINE))
152                 n = (f & (T1_SIG_OTH)) ? 1 : 0;
153         else
154                 n = 2;
155
156         return (T1_CTXNO_MAG + n);
157 }
158
159 static int t1_init_spb(int f) {
160         int hc, vc, n;
161
162         hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
163                                 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
164                         1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
165                                         (T1_SIG_E | T1_SGN_E)) +
166                                 ((f & (T1_SIG_W | T1_SGN_W)) ==
167                                  (T1_SIG_W | T1_SGN_W)), 1);
168
169         vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
170                                 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
171                         1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
172                                         (T1_SIG_N | T1_SGN_N)) +
173                                 ((f & (T1_SIG_S | T1_SGN_S)) ==
174                                  (T1_SIG_S | T1_SGN_S)), 1);
175
176         if (!hc && !vc)
177                 n = 0;
178         else
179                 n = (!(hc > 0 || (!hc && vc > 0)));
180
181         return n;
182 }
183
184 void dump_array16(int array[],int size){
185         int i;
186         --size;
187         for (i = 0; i < size; ++i) {
188                 printf("0x%04x, ", array[i]);
189                 if(!((i+1)&0x7))
190                         printf("\n  ");
191         }
192         printf("0x%04x\n};\n\n", array[size]);
193 }
194
195 int main(){
196         int i, j;
197         double u, v, t;
198
199         int lut_ctxno_zc[1024];
200         int lut_ctxno_mag[4096];
201         int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];
202         int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
203         int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
204         int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
205
206         printf("/* This file was automatically generated by t1_generate_luts.c */\n\n");
207
208         // lut_ctxno_zc
209         for (j = 0; j < 4; ++j) {
210                 for (i = 0; i < 256; ++i) {
211                         lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
212                 }
213         }
214
215         printf("static char lut_ctxno_zc[1024] = {\n  ");
216         for (i = 0; i < 1023; ++i) {
217                 printf("%i, ", lut_ctxno_zc[i]);
218                 if(!((i+1)&0x1f))
219                         printf("\n  ");
220         }
221         printf("%i\n};\n\n", lut_ctxno_zc[1023]);
222
223         // lut_ctxno_sc
224         printf("static char lut_ctxno_sc[256] = {\n  ");
225         for (i = 0; i < 255; ++i) {
226                 printf("0x%x, ", t1_init_ctxno_sc(i << 4));
227                 if(!((i+1)&0xf))
228                         printf("\n  ");
229         }
230         printf("0x%x\n};\n\n", t1_init_ctxno_sc(255 << 4));
231
232         // lut_ctxno_mag
233         for (j = 0; j < 2; ++j) {
234                 for (i = 0; i < 2048; ++i) {
235                         lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
236                 }
237         }
238
239         printf("static char lut_ctxno_mag[4096] = {\n  ");
240         for (i = 0; i < 4095; ++i) {
241                 printf("%i, ", lut_ctxno_mag[i]);
242                 if(!((i+1)&0xf))
243                         printf("\n  ");
244         }
245         printf("%i\n};\n\n", lut_ctxno_mag[4095]);
246
247         // lut_spb
248         printf("static char lut_spb[256] = {\n  ");
249         for (i = 0; i < 255; ++i) {
250                 printf("%i, ", t1_init_spb(i << 4));
251                 if(!((i+1)&0x1f))
252                         printf("\n  ");
253         }
254         printf("%i\n};\n\n", t1_init_spb(255 << 4));
255
256         /* FIXME FIXME FIXME */
257         /* fprintf(stdout,"nmsedec luts:\n"); */
258         for (i = 0; i < (1 << T1_NMSEDEC_BITS); ++i) {
259                 t = i / pow(2, T1_NMSEDEC_FRACBITS);
260                 u = t;
261                 v = t - 1.5;
262                 lut_nmsedec_sig[i] = 
263                         int_max(0, 
264                                         (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
265                 lut_nmsedec_sig0[i] =
266                         int_max(0,
267                                         (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
268                 u = t - 1.0;
269                 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
270                         v = t - 1.5;
271                 } else {
272                         v = t - 0.5;
273                 }
274                 lut_nmsedec_ref[i] =
275                         int_max(0,
276                                         (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
277                 lut_nmsedec_ref0[i] =
278                         int_max(0,
279                                         (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
280         }
281
282         printf("static short lut_nmsedec_sig[1 << T1_NMSEDEC_BITS] = {\n  ");
283         dump_array16(&lut_nmsedec_sig, 1 << T1_NMSEDEC_BITS);
284
285         printf("static short lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS] = {\n  ");
286         dump_array16(&lut_nmsedec_sig0, 1 << T1_NMSEDEC_BITS);
287
288         printf("static short lut_nmsedec_ref[1 << T1_NMSEDEC_BITS] = {\n  ");
289         dump_array16(&lut_nmsedec_ref, 1 << T1_NMSEDEC_BITS);
290
291         printf("static short lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS] = {\n  ");
292         dump_array16(&lut_nmsedec_ref0, 1 << T1_NMSEDEC_BITS);
293
294         return 0;
295 }