Reformat whole codebase with astyle.options (#128)
[openjpeg.git] / src / lib / openjp2 / t1_generate_luts.c
index cba7245d28f5491b91438bc96336c832f523ac88..2f7505d8fa4678f99acfd1c41b37f929b95bae74 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * The copyright in this software is being made available under the 2-clauses 
- * BSD License, included below. This software may be subject to other third 
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
  * party and contributor rights, including patent rights, and no such rights
  * are granted under this license.
  *
@@ -8,7 +8,7 @@
  * Copyright (c) 2002-2014, Professor Benoit Macq
  * Copyright (c) 2001-2003, David Janssens
  * Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2003-2007, Francois-Olivier Devaux 
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
  * Copyright (c) 2003-2014, Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
 
 #include "opj_includes.h"
 
-static int t1_init_ctxno_zc(int f, int orient) {
-       int h, v, d, n, t, hv;
-       h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
-       v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
-       d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
-       n = 0;
-       t = 0;
-       hv = 0;
+static int t1_init_ctxno_zc(unsigned int f, unsigned int orient)
+{
+    int h, v, d, n, t, hv;
+    h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
+    v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
+    d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((
+                f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
+    n = 0;
+    t = 0;
+    hv = 0;
 
-       switch (orient) {
-               case 2:
-                       t = h;
-                       h = v;
-                       v = t;
-               case 0:
-               case 1:
-                       if (!h) {
-                               if (!v) {
-                                       if (!d)
-                                               n = 0;
-                                       else if (d == 1)
-                                               n = 1;
-                                       else
-                                               n = 2;
-                               } else if (v == 1) {
-                                       n = 3;
-                               } else {
-                                       n = 4;
-                               }
-                       } else if (h == 1) {
-                               if (!v) {
-                                       if (!d)
-                                               n = 5;
-                                       else
-                                               n = 6;
-                               } else {
-                                       n = 7;
-                               }
-                       } else
-                               n = 8;
-                       break;
-               case 3:
-                       hv = h + v;
-                       if (!d) {
-                               if (!hv) {
-                                       n = 0;
-                               } else if (hv == 1) {
-                                       n = 1;
-                               } else {
-                                       n = 2;
-                               }
-                       } else if (d == 1) {
-                               if (!hv) {
-                                       n = 3;
-                               } else if (hv == 1) {
-                                       n = 4;
-                               } else {
-                                       n = 5;
-                               }
-                       } else if (d == 2) {
-                               if (!hv) {
-                                       n = 6;
-                               } else {
-                                       n = 7;
-                               }
-                       } else {
-                               n = 8;
-                       }
-                       break;
-       }
+    switch (orient) {
+    case 2:
+        t = h;
+        h = v;
+        v = t;
+    /* fall through */
+    case 0:
+    case 1:
+        if (!h) {
+            if (!v) {
+                if (!d) {
+                    n = 0;
+                } else if (d == 1) {
+                    n = 1;
+                } else {
+                    n = 2;
+                }
+            } else if (v == 1) {
+                n = 3;
+            } else {
+                n = 4;
+            }
+        } else if (h == 1) {
+            if (!v) {
+                if (!d) {
+                    n = 5;
+                } else {
+                    n = 6;
+                }
+            } else {
+                n = 7;
+            }
+        } else {
+            n = 8;
+        }
+        break;
+    case 3:
+        hv = h + v;
+        if (!d) {
+            if (!hv) {
+                n = 0;
+            } else if (hv == 1) {
+                n = 1;
+            } else {
+                n = 2;
+            }
+        } else if (d == 1) {
+            if (!hv) {
+                n = 3;
+            } else if (hv == 1) {
+                n = 4;
+            } else {
+                n = 5;
+            }
+        } else if (d == 2) {
+            if (!hv) {
+                n = 6;
+            } else {
+                n = 7;
+            }
+        } else {
+            n = 8;
+        }
+        break;
+    }
 
-       return (T1_CTXNO_ZC + n);
+    return (T1_CTXNO_ZC + n);
 }
 
-static int t1_init_ctxno_sc(int f) {
-       int hc, vc, n;
-       n = 0;
+static int t1_init_ctxno_sc(unsigned int f)
+{
+    int hc, vc, n;
+    n = 0;
 
-       hc = opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
-                               T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
-                       1) - opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
-                                       (T1_SIG_E | T1_SGN_E)) +
-                               ((f & (T1_SIG_W | T1_SGN_W)) ==
-                                (T1_SIG_W | T1_SGN_W)), 1);
+    hc = opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
+                      T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
+                     1) - opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
+                                       (T1_SIG_E | T1_SGN_E)) +
+                                      ((f & (T1_SIG_W | T1_SGN_W)) ==
+                                       (T1_SIG_W | T1_SGN_W)), 1);
 
-       vc = opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
-                               T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
-                       1) - opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
-                                       (T1_SIG_N | T1_SGN_N)) +
-                               ((f & (T1_SIG_S | T1_SGN_S)) ==
-                                (T1_SIG_S | T1_SGN_S)), 1);
+    vc = opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
+                      T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
+                     1) - opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
+                                       (T1_SIG_N | T1_SGN_N)) +
+                                      ((f & (T1_SIG_S | T1_SGN_S)) ==
+                                       (T1_SIG_S | T1_SGN_S)), 1);
 
-       if (hc < 0) {
-               hc = -hc;
-               vc = -vc;
-       }
-       if (!hc) {
-               if (vc == -1)
-                       n = 1;
-               else if (!vc)
-                       n = 0;
-               else
-                       n = 1;
-       } else if (hc == 1) {
-               if (vc == -1)
-                       n = 2;
-               else if (!vc)
-                       n = 3;
-               else
-                       n = 4;
-       }
+    if (hc < 0) {
+        hc = -hc;
+        vc = -vc;
+    }
+    if (!hc) {
+        if (vc == -1) {
+            n = 1;
+        } else if (!vc) {
+            n = 0;
+        } else {
+            n = 1;
+        }
+    } else if (hc == 1) {
+        if (vc == -1) {
+            n = 2;
+        } else if (!vc) {
+            n = 3;
+        } else {
+            n = 4;
+        }
+    }
 
-       return (T1_CTXNO_SC + n);
+    return (T1_CTXNO_SC + n);
 }
 
-static int t1_init_spb(int f) {
-       int hc, vc, n;
+static int t1_init_spb(unsigned int f)
+{
+    int hc, vc, n;
 
-       hc = opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
-                               T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
-                       1) - opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
-                                       (T1_SIG_E | T1_SGN_E)) +
-                               ((f & (T1_SIG_W | T1_SGN_W)) ==
-                                (T1_SIG_W | T1_SGN_W)), 1);
+    hc = opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
+                      T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
+                     1) - opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
+                                       (T1_SIG_E | T1_SGN_E)) +
+                                      ((f & (T1_SIG_W | T1_SGN_W)) ==
+                                       (T1_SIG_W | T1_SGN_W)), 1);
 
-       vc = opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
-                               T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
-                       1) - opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
-                                       (T1_SIG_N | T1_SGN_N)) +
-                               ((f & (T1_SIG_S | T1_SGN_S)) ==
-                                (T1_SIG_S | T1_SGN_S)), 1);
+    vc = opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
+                      T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
+                     1) - opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
+                                       (T1_SIG_N | T1_SGN_N)) +
+                                      ((f & (T1_SIG_S | T1_SGN_S)) ==
+                                       (T1_SIG_S | T1_SGN_S)), 1);
 
-       if (!hc && !vc)
-               n = 0;
-       else
-               n = (!(hc > 0 || (!hc && vc > 0)));
+    if (!hc && !vc) {
+        n = 0;
+    } else {
+        n = (!(hc > 0 || (!hc && vc > 0)));
+    }
 
-       return n;
+    return n;
 }
 
-static void dump_array16(int array[],int size){
-       int i;
-       --size;
-       for (i = 0; i < size; ++i) {
-               printf("0x%04x, ", array[i]);
-               if(!((i+1)&0x7))
-                       printf("\n  ");
-       }
-       printf("0x%04x\n};\n\n", array[size]);
+static void dump_array16(int array[], int size)
+{
+    int i;
+    --size;
+    for (i = 0; i < size; ++i) {
+        printf("0x%04x, ", array[i]);
+        if (!((i + 1) & 0x7)) {
+            printf("\n  ");
+        }
+    }
+    printf("0x%04x\n};\n\n", array[size]);
 }
 
 int main(int argc, char **argv)
 {
-       int i, j;
-       double u, v, t;
+    unsigned int i, j;
+    double u, v, t;
 
-       int lut_ctxno_zc[1024];
-       int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];
-       int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
-       int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
-       int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
-  (void)argc; (void)argv;
+    int lut_ctxno_zc[1024];
+    int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];
+    int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
+    int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
+    int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
+    (void)argc;
+    (void)argv;
 
-       printf("/* This file was automatically generated by t1_generate_luts.c */\n\n");
+    printf("/* This file was automatically generated by t1_generate_luts.c */\n\n");
 
-       /* lut_ctxno_zc */
-       for (j = 0; j < 4; ++j) {
-               for (i = 0; i < 256; ++i) {
-                       int orient = j;
-                       if (orient == 2) {
-                               orient = 1;
-                       } else if (orient == 1) {
-                               orient = 2;
-                       }
-                       lut_ctxno_zc[(orient << 8) | i] = t1_init_ctxno_zc(i, j);
-               }
-       }
+    /* lut_ctxno_zc */
+    for (j = 0U; j < 4U; ++j) {
+        for (i = 0U; i < 256U; ++i) {
+            unsigned int orient = j;
+            if (orient == 2U) {
+                orient = 1U;
+            } else if (orient == 1U) {
+                orient = 2U;
+            }
+            lut_ctxno_zc[(orient << 8) | i] = t1_init_ctxno_zc(i, j);
+        }
+    }
 
-       printf("static OPJ_BYTE lut_ctxno_zc[1024] = {\n  ");
-       for (i = 0; i < 1023; ++i) {
-               printf("%i, ", lut_ctxno_zc[i]);
-               if(!((i+1)&0x1f))
-                       printf("\n  ");
-       }
-       printf("%i\n};\n\n", lut_ctxno_zc[1023]);
+    printf("static const OPJ_BYTE lut_ctxno_zc[1024] = {\n  ");
+    for (i = 0U; i < 1023U; ++i) {
+        printf("%i, ", lut_ctxno_zc[i]);
+        if (!((i + 1U) & 0x1fU)) {
+            printf("\n  ");
+        }
+    }
+    printf("%i\n};\n\n", lut_ctxno_zc[1023]);
 
-       /* lut_ctxno_sc */
-       printf("static OPJ_BYTE lut_ctxno_sc[256] = {\n  ");
-       for (i = 0; i < 255; ++i) {
-               printf("0x%x, ", t1_init_ctxno_sc(i << 4));
-               if(!((i+1)&0xf))
-                       printf("\n  ");
-       }
-       printf("0x%x\n};\n\n", t1_init_ctxno_sc(255 << 4));
+    /* lut_ctxno_sc */
+    printf("static const OPJ_BYTE lut_ctxno_sc[256] = {\n  ");
+    for (i = 0U; i < 255U; ++i) {
+        printf("0x%x, ", t1_init_ctxno_sc(i << 4));
+        if (!((i + 1U) & 0xfU)) {
+            printf("\n  ");
+        }
+    }
+    printf("0x%x\n};\n\n", t1_init_ctxno_sc(255U << 4));
 
-       /* lut_spb */
-       printf("static OPJ_BYTE lut_spb[256] = {\n  ");
-       for (i = 0; i < 255; ++i) {
-               printf("%i, ", t1_init_spb(i << 4));
-               if(!((i+1)&0x1f))
-                       printf("\n  ");
-       }
-       printf("%i\n};\n\n", t1_init_spb(255 << 4));
+    /* lut_spb */
+    printf("static const OPJ_BYTE lut_spb[256] = {\n  ");
+    for (i = 0U; i < 255U; ++i) {
+        printf("%i, ", t1_init_spb(i << 4));
+        if (!((i + 1U) & 0x1fU)) {
+            printf("\n  ");
+        }
+    }
+    printf("%i\n};\n\n", t1_init_spb(255U << 4));
 
-       /* FIXME FIXME FIXME */
-       /* fprintf(stdout,"nmsedec luts:\n"); */
-       for (i = 0; i < (1 << T1_NMSEDEC_BITS); ++i) {
-               t = i / pow(2, T1_NMSEDEC_FRACBITS);
-               u = t;
-               v = t - 1.5;
-               lut_nmsedec_sig[i] = 
-                       opj_int_max(0, 
-                                       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
-               lut_nmsedec_sig0[i] =
-                       opj_int_max(0,
-                                       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
-               u = t - 1.0;
-               if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
-                       v = t - 1.5;
-               } else {
-                       v = t - 0.5;
-               }
-               lut_nmsedec_ref[i] =
-                       opj_int_max(0,
-                                       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
-               lut_nmsedec_ref0[i] =
-                       opj_int_max(0,
-                                       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
-       }
+    /* FIXME FIXME FIXME */
+    /* fprintf(stdout,"nmsedec luts:\n"); */
+    for (i = 0U; i < (1U << T1_NMSEDEC_BITS); ++i) {
+        t = i / pow(2, T1_NMSEDEC_FRACBITS);
+        u = t;
+        v = t - 1.5;
+        lut_nmsedec_sig[i] =
+            opj_int_max(0,
+                        (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
+                                T1_NMSEDEC_FRACBITS) * 8192.0));
+        lut_nmsedec_sig0[i] =
+            opj_int_max(0,
+                        (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
+                                T1_NMSEDEC_FRACBITS) * 8192.0));
+        u = t - 1.0;
+        if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
+            v = t - 1.5;
+        } else {
+            v = t - 0.5;
+        }
+        lut_nmsedec_ref[i] =
+            opj_int_max(0,
+                        (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
+                                T1_NMSEDEC_FRACBITS) * 8192.0));
+        lut_nmsedec_ref0[i] =
+            opj_int_max(0,
+                        (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
+                                T1_NMSEDEC_FRACBITS) * 8192.0));
+    }
 
-       printf("static OPJ_INT16 lut_nmsedec_sig[1 << T1_NMSEDEC_BITS] = {\n  ");
-       dump_array16(lut_nmsedec_sig, 1 << T1_NMSEDEC_BITS);
+    printf("static const OPJ_INT16 lut_nmsedec_sig[1U << T1_NMSEDEC_BITS] = {\n  ");
+    dump_array16(lut_nmsedec_sig, 1U << T1_NMSEDEC_BITS);
 
-       printf("static OPJ_INT16 lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS] = {\n  ");
-       dump_array16(lut_nmsedec_sig0, 1 << T1_NMSEDEC_BITS);
+    printf("static const OPJ_INT16 lut_nmsedec_sig0[1U << T1_NMSEDEC_BITS] = {\n  ");
+    dump_array16(lut_nmsedec_sig0, 1U << T1_NMSEDEC_BITS);
 
-       printf("static OPJ_INT16 lut_nmsedec_ref[1 << T1_NMSEDEC_BITS] = {\n  ");
-       dump_array16(lut_nmsedec_ref, 1 << T1_NMSEDEC_BITS);
+    printf("static const OPJ_INT16 lut_nmsedec_ref[1U << T1_NMSEDEC_BITS] = {\n  ");
+    dump_array16(lut_nmsedec_ref, 1U << T1_NMSEDEC_BITS);
 
-       printf("static OPJ_INT16 lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS] = {\n  ");
-       dump_array16(lut_nmsedec_ref0, 1 << T1_NMSEDEC_BITS);
+    printf("static const OPJ_INT16 lut_nmsedec_ref0[1U << T1_NMSEDEC_BITS] = {\n  ");
+    dump_array16(lut_nmsedec_ref0, 1U << T1_NMSEDEC_BITS);
 
-       return 0;
+    return 0;
 }