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