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