Change 'restrict' define to 'OPJ_RESTRICT' (#816)
[openjpeg.git] / src / lib / openjp2 / t1.c
index 088835456a7ab69f8c89bd4b6f78f62fdeb09bdb..cb5a1cefd30d460f4d307bea686d39e64a0ea105 100644 (file)
@@ -295,7 +295,7 @@ static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
                                     OPJ_UINT32 roishift,
                                     OPJ_UINT32 cblksty);
 
-OPJ_BOOL opj_t1_allocate_buffers(   opj_t1_t *t1,
+static OPJ_BOOL opj_t1_allocate_buffers(   opj_t1_t *t1,
                                     OPJ_UINT32 w,
                                     OPJ_UINT32 h);
 
@@ -305,25 +305,25 @@ OPJ_BOOL opj_t1_allocate_buffers(   opj_t1_t *t1,
 
 /* ----------------------------------------------------------------------- */
 
-OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
+static OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
        return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
 }
 
-OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
+static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
        return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
 }
 
-OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
+static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
        OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
        OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
        return (tmp2);
 }
 
-OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
+static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
        return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
 }
 
-OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
+static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
        if (bitpos > 0) {
                return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
        }
@@ -331,7 +331,7 @@ OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
        return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
 }
 
-OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
+static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
        if (bitpos > 0) {
                return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
        }
@@ -339,7 +339,7 @@ OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
     return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
 }
 
-void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
+static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
        opj_flag_t *np = flagsp - stride;
        opj_flag_t *sp = flagsp + stride;
 
@@ -363,7 +363,7 @@ void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
        sp[1]  |= T1_SIG_NW;
 }
 
-void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
+static void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
                                 opj_flag_t *flagsp,
                                 OPJ_INT32 *datap,
                                 OPJ_UINT32 orient,
@@ -381,7 +381,7 @@ void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
        
        flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
        if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
-               v = opj_int_abs(*datap) & one ? 1 : 0;
+               v = (opj_int_abs(*datap) & one) ? 1 : 0;
                opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));       /* ESSAI */
                if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
                        opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
@@ -427,7 +427,7 @@ static INLINE void opj_t1_dec_sigpass_step_raw(
         }
 }      
 
-INLINE void opj_t1_dec_sigpass_step_mqc(
+static INLINE void opj_t1_dec_sigpass_step_mqc(
                 opj_t1_t *t1,
                 opj_flag_t *flagsp,
                 OPJ_INT32 *datap,
@@ -451,7 +451,7 @@ INLINE void opj_t1_dec_sigpass_step_mqc(
         }
 }                               /* VSC and  BYPASS by Antonin */
 
-INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
+static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
                 opj_t1_t *t1,
                 opj_flag_t *flagsp,
                 OPJ_INT32 *datap,
@@ -478,7 +478,7 @@ INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
 
 
 
-void opj_t1_enc_sigpass(opj_t1_t *t1,
+static void opj_t1_enc_sigpass(opj_t1_t *t1,
                         OPJ_INT32 bpno,
                         OPJ_UINT32 orient,
                         OPJ_INT32 *nmsedec,
@@ -510,7 +510,7 @@ void opj_t1_enc_sigpass(opj_t1_t *t1,
        }
 }
 
-void opj_t1_dec_sigpass_raw(
+static void opj_t1_dec_sigpass_raw(
                 opj_t1_t *t1,
                 OPJ_INT32 bpno,
                 OPJ_INT32 orient,
@@ -537,7 +537,7 @@ void opj_t1_dec_sigpass_raw(
         }
 }                               /* VSC and  BYPASS by Antonin */
 
-void opj_t1_dec_sigpass_mqc(
+static void opj_t1_dec_sigpass_mqc(
                 opj_t1_t *t1,
                 OPJ_INT32 bpno,
                 OPJ_INT32 orient)
@@ -580,7 +580,7 @@ void opj_t1_dec_sigpass_mqc(
         }
 }                               /* VSC and  BYPASS by Antonin */
 
-void opj_t1_dec_sigpass_mqc_vsc(
+static void opj_t1_dec_sigpass_mqc_vsc(
                 opj_t1_t *t1,
                 OPJ_INT32 bpno,
                 OPJ_INT32 orient)
@@ -608,7 +608,7 @@ void opj_t1_dec_sigpass_mqc_vsc(
 
 
 
-void opj_t1_enc_refpass_step(   opj_t1_t *t1,
+static void opj_t1_enc_refpass_step(   opj_t1_t *t1,
                                 opj_flag_t *flagsp,
                                 OPJ_INT32 *datap,
                                 OPJ_INT32 bpno,
@@ -625,7 +625,7 @@ void opj_t1_enc_refpass_step(   opj_t1_t *t1,
        flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
                *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
-               v = opj_int_abs(*datap) & one ? 1 : 0;
+               v = (opj_int_abs(*datap) & one) ? 1 : 0;
                opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
                if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
                        opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
@@ -636,7 +636,7 @@ void opj_t1_enc_refpass_step(   opj_t1_t *t1,
        }
 }
 
-INLINE void opj_t1_dec_refpass_step_raw(
+static INLINE void opj_t1_dec_refpass_step_raw(
                 opj_t1_t *t1,
                 opj_flag_t *flagsp,
                 OPJ_INT32 *datap,
@@ -657,7 +657,7 @@ INLINE void opj_t1_dec_refpass_step_raw(
         }
 }                               /* VSC and  BYPASS by Antonin  */
 
-INLINE void opj_t1_dec_refpass_step_mqc(
+static INLINE void opj_t1_dec_refpass_step_mqc(
                 opj_t1_t *t1,
                 opj_flag_t *flagsp,
                 OPJ_INT32 *datap,
@@ -678,7 +678,7 @@ INLINE void opj_t1_dec_refpass_step_mqc(
                 }
 }                               /* VSC and  BYPASS by Antonin  */
 
-INLINE void opj_t1_dec_refpass_step_mqc_vsc(
+static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
                 opj_t1_t *t1,
                 opj_flag_t *flagsp,
                 OPJ_INT32 *datap,
@@ -701,7 +701,7 @@ INLINE void opj_t1_dec_refpass_step_mqc_vsc(
 }                               /* VSC and  BYPASS by Antonin  */
 
 
-void opj_t1_enc_refpass(
+static void opj_t1_enc_refpass(
                opj_t1_t *t1,
                OPJ_INT32 bpno,
                OPJ_INT32 *nmsedec,
@@ -731,7 +731,7 @@ void opj_t1_enc_refpass(
        }
 }
 
-void opj_t1_dec_refpass_raw(
+static void opj_t1_dec_refpass_raw(
                 opj_t1_t *t1,
                 OPJ_INT32 bpno,
                 OPJ_INT32 cblksty)
@@ -758,7 +758,7 @@ void opj_t1_dec_refpass_raw(
         }
 }                               /* VSC and  BYPASS by Antonin */
 
-void opj_t1_dec_refpass_mqc(
+static void opj_t1_dec_refpass_mqc(
                 opj_t1_t *t1,
                 OPJ_INT32 bpno)
 {
@@ -800,7 +800,7 @@ void opj_t1_dec_refpass_mqc(
         }
 }                               /* VSC and  BYPASS by Antonin */
 
-void opj_t1_dec_refpass_mqc_vsc(
+static void opj_t1_dec_refpass_mqc_vsc(
                 opj_t1_t *t1,
                 OPJ_INT32 bpno)
 {
@@ -827,7 +827,7 @@ void opj_t1_dec_refpass_mqc_vsc(
 }                               /* VSC and  BYPASS by Antonin */
 
 
-void opj_t1_enc_clnpass_step(
+static void opj_t1_enc_clnpass_step(
                opj_t1_t *t1,
                opj_flag_t *flagsp,
                OPJ_INT32 *datap,
@@ -849,7 +849,7 @@ void opj_t1_enc_clnpass_step(
        }
        if (!(*flagsp & (T1_SIG | T1_VISIT))) {
                opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
-               v = opj_int_abs(*datap) & one ? 1 : 0;
+               v = (opj_int_abs(*datap) & one) ? 1 : 0;
                opj_mqc_encode(mqc, (OPJ_UINT32)v);
                if (v) {
 LABEL_PARTIAL:
@@ -937,7 +937,7 @@ LABEL_PARTIAL:
        *flagsp &= ~T1_VISIT;
 }
 
-void opj_t1_enc_clnpass(
+static void opj_t1_enc_clnpass(
                opj_t1_t *t1,
                OPJ_INT32 bpno,
                OPJ_UINT32 orient,
@@ -1161,7 +1161,7 @@ static OPJ_FLOAT64 opj_t1_getwmsedec(
        return wmsedec;
 }
 
-OPJ_BOOL opj_t1_allocate_buffers(
+static OPJ_BOOL opj_t1_allocate_buffers(
                opj_t1_t *t1,
                OPJ_UINT32 w,
                OPJ_UINT32 h)
@@ -1180,7 +1180,10 @@ OPJ_BOOL opj_t1_allocate_buffers(
                        }
                        t1->datasize=datasize;
                }
-               memset(t1->data,0,datasize * sizeof(OPJ_INT32));
+               /* memset first arg is declared to never be null by gcc */
+               if (t1->data != NULL) {
+                       memset(t1->data,0,datasize * sizeof(OPJ_INT32));
+               }
        }
        t1->flags_stride=w+2;
        flagssize=t1->flags_stride * (h+2);
@@ -1280,14 +1283,14 @@ OPJ_BOOL opj_t1_decode_cblks(   opj_t1_t* t1,
                opj_tcd_resolution_t* res = &tilec->resolutions[resno];
 
                for (bandno = 0; bandno < res->numbands; ++bandno) {
-                       opj_tcd_band_t* restrict band = &res->bands[bandno];
+                       opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
 
                        for (precno = 0; precno < res->pw * res->ph; ++precno) {
                                opj_tcd_precinct_t* precinct = &band->precincts[precno];
 
                                for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
                                        opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
-                                       OPJ_INT32* restrict datap;
+                                       OPJ_INT32* OPJ_RESTRICT datap;
                                        OPJ_UINT32 cblk_w, cblk_h;
                                        OPJ_INT32 x, y;
                                        OPJ_UINT32 i, j;
@@ -1330,7 +1333,7 @@ OPJ_BOOL opj_t1_decode_cblks(   opj_t1_t* t1,
                                                }
                                        }
                                        if (tccp->qmfbid == 1) {
-                        OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
+                        OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
                                                for (j = 0; j < cblk_h; ++j) {
                                                        for (i = 0; i < cblk_w; ++i) {
                                                                OPJ_INT32 tmp = datap[(j * cblk_w) + i];
@@ -1338,9 +1341,9 @@ OPJ_BOOL opj_t1_decode_cblks(   opj_t1_t* t1,
                                                        }
                                                }
                                        } else {                /* if (tccp->qmfbid == 0) */
-                        OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
+                        OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
                                                for (j = 0; j < cblk_h; ++j) {
-                            OPJ_FLOAT32* restrict tiledp2 = tiledp;
+                            OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
                                                        for (i = 0; i < cblk_w; ++i) {
                                 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
                                 *tiledp2 = tmp;
@@ -1358,7 +1361,7 @@ OPJ_BOOL opj_t1_decode_cblks(   opj_t1_t* t1,
 }
 
 
-OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
+static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
                             opj_tcd_cblk_dec_t* cblk,
                             OPJ_UINT32 orient,
                             OPJ_UINT32 roishift,
@@ -1405,7 +1408,7 @@ OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
             }
                }
 
-               for (passno = 0; passno < seg->real_num_passes; ++passno) {
+               for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
             switch (passtype) {
                 case 0:
                     if (type == T1_TYPE_RAW) {
@@ -1472,7 +1475,7 @@ OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
                        opj_tcd_resolution_t *res = &tilec->resolutions[resno];
 
                        for (bandno = 0; bandno < res->numbands; ++bandno) {
-                               opj_tcd_band_t* restrict band = &res->bands[bandno];
+                               opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
 
                                for (precno = 0; precno < res->pw * res->ph; ++precno) {
@@ -1480,7 +1483,7 @@ OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
 
                                        for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
                                                opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
-                                               OPJ_INT32* restrict tiledp;
+                                               OPJ_INT32* OPJ_RESTRICT tiledp;
                                                OPJ_UINT32 cblk_w;
                                                OPJ_UINT32 cblk_h;
                                                OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
@@ -1514,7 +1517,7 @@ OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
                                                if (tccp->qmfbid == 1) {
                                                        for (j = 0; j < cblk_h; ++j) {
                                                                for (i = 0; i < cblk_w; ++i) {
-                                                                       tiledp[tileIndex] <<= T1_NMSEDEC_FRACBITS;
+                                                                       tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
                                                                        tileIndex++;
                                                                }
                                                                tileIndex += tileLineAdvance;
@@ -1556,7 +1559,7 @@ OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
 }
 
 /** mod fixed_quality */
-void opj_t1_encode_cblk(opj_t1_t *t1,
+static void opj_t1_encode_cblk(opj_t1_t *t1,
                         opj_tcd_cblk_enc_t* cblk,
                         OPJ_UINT32 orient,
                         OPJ_UINT32 compno,
@@ -1696,7 +1699,7 @@ void opj_t1_encode_cblk(opj_t1_t *t1,
 }
 
 #if 0
-void opj_t1_dec_refpass_step(   opj_t1_t *t1,
+static void opj_t1_dec_refpass_step(   opj_t1_t *t1,
                                 opj_flag_t *flagsp,
                                 OPJ_INT32 *datap,
                                 OPJ_INT32 poshalf,
@@ -1728,7 +1731,7 @@ void opj_t1_dec_refpass_step(   opj_t1_t *t1,
 
 
 #if 0
-void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
+static void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
                                 opj_flag_t *flagsp,
                                 OPJ_INT32 *datap,
                                 OPJ_UINT32 orient,