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