Home | History | Annotate | Download | only in libopenjpeg20
      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 (at) haxxed.com>
     15  * Copyright (c) 2012, Carl Hetherington
     16  * All rights reserved.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
     28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include "opj_includes.h"
     41 
     42 static int t1_init_ctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient)
     43 {
     44     int h, v, d, n, t, hv;
     45     n = 0;
     46     h = ((f & T1_SIGMA_3) != 0) + ((f & T1_SIGMA_5) != 0);
     47     v = ((f & T1_SIGMA_1) != 0) + ((f & T1_SIGMA_7) != 0);
     48     d = ((f & T1_SIGMA_0) != 0) + ((f & T1_SIGMA_2) != 0) + ((
     49                 f & T1_SIGMA_8) != 0) + ((f & T1_SIGMA_6) != 0);
     50 
     51     switch (orient) {
     52     case 2:
     53         t = h;
     54         h = v;
     55         v = t;
     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                 }
     67             } else if (v == 1) {
     68                 n = 3;
     69             } else {
     70                 n = 4;
     71             }
     72         } else if (h == 1) {
     73             if (!v) {
     74                 if (!d) {
     75                     n = 5;
     76                 } else {
     77                     n = 6;
     78                 }
     79             } else {
     80                 n = 7;
     81             }
     82         } else {
     83             n = 8;
     84         }
     85         break;
     86     case 3:
     87         hv = h + v;
     88         if (!d) {
     89             if (!hv) {
     90                 n = 0;
     91             } else if (hv == 1) {
     92                 n = 1;
     93             } else {
     94                 n = 2;
     95             }
     96         } else if (d == 1) {
     97             if (!hv) {
     98                 n = 3;
     99             } else if (hv == 1) {
    100                 n = 4;
    101             } else {
    102                 n = 5;
    103             }
    104         } else if (d == 2) {
    105             if (!hv) {
    106                 n = 6;
    107             } else {
    108                 n = 7;
    109             }
    110         } else {
    111             n = 8;
    112         }
    113         break;
    114     }
    115 
    116     return (T1_CTXNO_ZC + n);
    117 }
    118 
    119 static int t1_init_ctxno_sc(OPJ_UINT32 f)
    120 {
    121     int hc, vc, n;
    122     n = 0;
    123 
    124     hc = opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
    125                       T1_LUT_SIG_E) + ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) == T1_LUT_SIG_W),
    126                      1) - opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
    127                                        (T1_LUT_SIG_E | T1_LUT_SGN_E)) +
    128                                       ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) ==
    129                                        (T1_LUT_SIG_W | T1_LUT_SGN_W)), 1);
    130 
    131     vc = opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
    132                       T1_LUT_SIG_N) + ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) == T1_LUT_SIG_S),
    133                      1) - opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
    134                                        (T1_LUT_SIG_N | T1_LUT_SGN_N)) +
    135                                       ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) ==
    136                                        (T1_LUT_SIG_S | T1_LUT_SGN_S)), 1);
    137 
    138     if (hc < 0) {
    139         hc = -hc;
    140         vc = -vc;
    141     }
    142     if (!hc) {
    143         if (vc == -1) {
    144             n = 1;
    145         } else if (!vc) {
    146             n = 0;
    147         } else {
    148             n = 1;
    149         }
    150     } else if (hc == 1) {
    151         if (vc == -1) {
    152             n = 2;
    153         } else if (!vc) {
    154             n = 3;
    155         } else {
    156             n = 4;
    157         }
    158     }
    159 
    160     return (T1_CTXNO_SC + n);
    161 }
    162 
    163 static int t1_init_spb(OPJ_UINT32 f)
    164 {
    165     int hc, vc, n;
    166 
    167     hc = opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
    168                       T1_LUT_SIG_E) + ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) == T1_LUT_SIG_W),
    169                      1) - opj_int_min(((f & (T1_LUT_SIG_E | T1_LUT_SGN_E)) ==
    170                                        (T1_LUT_SIG_E | T1_LUT_SGN_E)) +
    171                                       ((f & (T1_LUT_SIG_W | T1_LUT_SGN_W)) ==
    172                                        (T1_LUT_SIG_W | T1_LUT_SGN_W)), 1);
    173 
    174     vc = opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
    175                       T1_LUT_SIG_N) + ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) == T1_LUT_SIG_S),
    176                      1) - opj_int_min(((f & (T1_LUT_SIG_N | T1_LUT_SGN_N)) ==
    177                                        (T1_LUT_SIG_N | T1_LUT_SGN_N)) +
    178                                       ((f & (T1_LUT_SIG_S | T1_LUT_SGN_S)) ==
    179                                        (T1_LUT_SIG_S | T1_LUT_SGN_S)), 1);
    180 
    181     if (!hc && !vc) {
    182         n = 0;
    183     } else {
    184         n = (!(hc > 0 || (!hc && vc > 0)));
    185     }
    186 
    187     return n;
    188 }
    189 
    190 static void dump_array16(int array[], int size)
    191 {
    192     int i;
    193     --size;
    194     for (i = 0; i < size; ++i) {
    195         printf("0x%04x,", array[i]);
    196         if (!((i + 1) & 0x7)) {
    197             printf("\n    ");
    198         } else {
    199             printf(" ");
    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[2048];
    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 = 0; j < 4; ++j) {
    222         for (i = 0; i < 512; ++i) {
    223             OPJ_UINT32 orient = j;
    224             if (orient == 2) {
    225                 orient = 1;
    226             } else if (orient == 1) {
    227                 orient = 2;
    228             }
    229             lut_ctxno_zc[(orient << 9) | i] = t1_init_ctxno_zc(i, j);
    230         }
    231     }
    232 
    233     printf("static const OPJ_BYTE lut_ctxno_zc[2048] = {\n    ");
    234     for (i = 0; i < 2047; ++i) {
    235         printf("%i,", lut_ctxno_zc[i]);
    236         if (!((i + 1) & 0x1f)) {
    237             printf("\n    ");
    238         } else {
    239             printf(" ");
    240         }
    241     }
    242     printf("%i\n};\n\n", lut_ctxno_zc[2047]);
    243 
    244     /* lut_ctxno_sc */
    245     printf("static const OPJ_BYTE lut_ctxno_sc[256] = {\n    ");
    246     for (i = 0; i < 255; ++i) {
    247         printf("0x%x,", t1_init_ctxno_sc(i));
    248         if (!((i + 1) & 0xf)) {
    249             printf("\n    ");
    250         } else {
    251             printf(" ");
    252         }
    253     }
    254     printf("0x%x\n};\n\n", t1_init_ctxno_sc(255));
    255 
    256     /* lut_spb */
    257     printf("static const OPJ_BYTE lut_spb[256] = {\n    ");
    258     for (i = 0; i < 255; ++i) {
    259         printf("%i,", t1_init_spb(i));
    260         if (!((i + 1) & 0x1f)) {
    261             printf("\n    ");
    262         } else {
    263             printf(" ");
    264         }
    265     }
    266     printf("%i\n};\n\n", t1_init_spb(255));
    267 
    268     /* FIXME FIXME FIXME */
    269     /* fprintf(stdout,"nmsedec luts:\n"); */
    270     for (i = 0U; i < (1U << T1_NMSEDEC_BITS); ++i) {
    271         t = i / pow(2, T1_NMSEDEC_FRACBITS);
    272         u = t;
    273         v = t - 1.5;
    274         lut_nmsedec_sig[i] =
    275             opj_int_max(0,
    276                         (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
    277                                 T1_NMSEDEC_FRACBITS) * 8192.0));
    278         lut_nmsedec_sig0[i] =
    279             opj_int_max(0,
    280                         (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
    281                                 T1_NMSEDEC_FRACBITS) * 8192.0));
    282         u = t - 1.0;
    283         if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
    284             v = t - 1.5;
    285         } else {
    286             v = t - 0.5;
    287         }
    288         lut_nmsedec_ref[i] =
    289             opj_int_max(0,
    290                         (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
    291                                 T1_NMSEDEC_FRACBITS) * 8192.0));
    292         lut_nmsedec_ref0[i] =
    293             opj_int_max(0,
    294                         (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
    295                                 T1_NMSEDEC_FRACBITS) * 8192.0));
    296     }
    297 
    298     printf("static const OPJ_INT16 lut_nmsedec_sig[1U << T1_NMSEDEC_BITS] = {\n    ");
    299     dump_array16(lut_nmsedec_sig, 1U << T1_NMSEDEC_BITS);
    300 
    301     printf("static const OPJ_INT16 lut_nmsedec_sig0[1U << T1_NMSEDEC_BITS] = {\n    ");
    302     dump_array16(lut_nmsedec_sig0, 1U << T1_NMSEDEC_BITS);
    303 
    304     printf("static const OPJ_INT16 lut_nmsedec_ref[1U << T1_NMSEDEC_BITS] = {\n    ");
    305     dump_array16(lut_nmsedec_ref, 1U << T1_NMSEDEC_BITS);
    306 
    307     printf("static const OPJ_INT16 lut_nmsedec_ref0[1U << T1_NMSEDEC_BITS] = {\n    ");
    308     dump_array16(lut_nmsedec_ref0, 1U << T1_NMSEDEC_BITS);
    309 
    310     return 0;
    311 }
    312