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