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