Merge pull request #559 from szukw000/cmyk-cielab-esycc
[openjpeg.git] / src / lib / openjp2 / mqc.c
index 5fcbe6fdd8912f4d0980ac0aa185ca9df0d87fa9..7e0f5637bf80470f18f13ef8d8ef10a62ca29879 100644 (file)
@@ -1,9 +1,15 @@
 /*
- * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
- * Copyright (c) 2002-2007, Professor Benoit Macq
+ * 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.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * 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 and Antonin Descampe
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux 
+ * Copyright (c) 2003-2014, Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
  * All rights reserved.
@@ -196,7 +202,7 @@ static opj_mqc_state_t mqc_states[47 * 2] = {
 ==========================================================
 */
 
-void opj_mqc_byteout(opj_mqc_t *mqc) {
+static void opj_mqc_byteout(opj_mqc_t *mqc) {
        if (*mqc->bp == 0xff) {
                mqc->bp++;
                *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
@@ -226,7 +232,7 @@ void opj_mqc_byteout(opj_mqc_t *mqc) {
        }
 }
 
-void opj_mqc_renorme(opj_mqc_t *mqc) {
+static void opj_mqc_renorme(opj_mqc_t *mqc) {
        do {
                mqc->a <<= 1;
                mqc->c <<= 1;
@@ -237,7 +243,7 @@ void opj_mqc_renorme(opj_mqc_t *mqc) {
        } while ((mqc->a & 0x8000) == 0);
 }
 
-void opj_mqc_codemps(opj_mqc_t *mqc) {
+static void opj_mqc_codemps(opj_mqc_t *mqc) {
        mqc->a -= (*mqc->curctx)->qeval;
        if ((mqc->a & 0x8000) == 0) {
                if (mqc->a < (*mqc->curctx)->qeval) {
@@ -252,7 +258,7 @@ void opj_mqc_codemps(opj_mqc_t *mqc) {
        }
 }
 
-void opj_mqc_codelps(opj_mqc_t *mqc) {
+static void opj_mqc_codelps(opj_mqc_t *mqc) {
        mqc->a -= (*mqc->curctx)->qeval;
        if (mqc->a < (*mqc->curctx)->qeval) {
                mqc->c += (*mqc->curctx)->qeval;
@@ -263,7 +269,7 @@ void opj_mqc_codelps(opj_mqc_t *mqc) {
        opj_mqc_renorme(mqc);
 }
 
-void opj_mqc_setbits(opj_mqc_t *mqc) {
+static void opj_mqc_setbits(opj_mqc_t *mqc) {
        OPJ_UINT32 tempc = mqc->c + mqc->a;
        mqc->c |= 0xffff;
        if (mqc->c >= tempc) {
@@ -274,10 +280,10 @@ void opj_mqc_setbits(opj_mqc_t *mqc) {
 static INLINE OPJ_INT32 opj_mqc_mpsexchange(opj_mqc_t *const mqc) {
        OPJ_INT32 d;
        if (mqc->a < (*mqc->curctx)->qeval) {
-               d = 1 - (*mqc->curctx)->mps;
+               d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
                *mqc->curctx = (*mqc->curctx)->nlps;
        } else {
-               d = (*mqc->curctx)->mps;
+               d = (OPJ_INT32)(*mqc->curctx)->mps;
                *mqc->curctx = (*mqc->curctx)->nmps;
        }
        
@@ -288,11 +294,11 @@ static INLINE OPJ_INT32 opj_mqc_lpsexchange(opj_mqc_t *const mqc) {
        OPJ_INT32 d;
        if (mqc->a < (*mqc->curctx)->qeval) {
                mqc->a = (*mqc->curctx)->qeval;
-               d = (*mqc->curctx)->mps;
+               d = (OPJ_INT32)(*mqc->curctx)->mps;
                *mqc->curctx = (*mqc->curctx)->nmps;
        } else {
                mqc->a = (*mqc->curctx)->qeval;
-               d = 1 - (*mqc->curctx)->mps;
+               d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
                *mqc->curctx = (*mqc->curctx)->nlps;
        }
        
@@ -356,7 +362,9 @@ static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc) {
 opj_mqc_t* opj_mqc_create(void) {
        opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
 #ifdef MQC_PERF_OPT
-       mqc->buffer = NULL;
+       if (mqc) {
+               mqc->buffer = NULL;
+       }
 #endif
        return mqc;
 }
@@ -364,7 +372,9 @@ opj_mqc_t* opj_mqc_create(void) {
 void opj_mqc_destroy(opj_mqc_t *mqc) {
        if(mqc) {
 #ifdef MQC_PERF_OPT
-               opj_free(mqc->buffer);
+               if (mqc->buffer) {
+                       opj_free(mqc->buffer);
+               }
 #endif
                opj_free(mqc);
        }
@@ -441,7 +451,7 @@ OPJ_UINT32 opj_mqc_bypass_flush_enc(opj_mqc_t *mqc) {
        if (mqc->ct != 0) {
                while (mqc->ct > 0) {
                        mqc->ct--;
-                       mqc->c += bit_padding << mqc->ct;
+                       mqc->c += (OPJ_UINT32)(bit_padding << mqc->ct);
                        bit_padding = (bit_padding + 1) & 0x01;
                }
                mqc->bp++;
@@ -464,11 +474,11 @@ OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc) {
        OPJ_UINT32 correction = 1;
        
        /* <flush part> */
-       OPJ_INT32 n = 27 - 15 - mqc->ct;
+       OPJ_INT32 n = (OPJ_INT32)(27 - 15 - mqc->ct);
        mqc->c <<= mqc->ct;
        while (n > 0) {
                opj_mqc_byteout(mqc);
-               n -= mqc->ct;
+               n -= (OPJ_INT32)mqc->ct;
                mqc->c <<= mqc->ct;
        }
        opj_mqc_byteout(mqc);
@@ -489,13 +499,13 @@ void opj_mqc_restart_init_enc(opj_mqc_t *mqc) {
 }
 
 void opj_mqc_erterm_enc(opj_mqc_t *mqc) {
-       OPJ_INT32 k = 11 - mqc->ct + 1;
+       OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1);
        
        while (k > 0) {
                mqc->c <<= mqc->ct;
                mqc->ct = 0;
                opj_mqc_byteout(mqc);
-               k -= mqc->ct;
+               k -= (OPJ_INT32)mqc->ct;
        }
        
        if (*mqc->bp != 0xff) {
@@ -518,7 +528,7 @@ OPJ_BOOL opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len) {
        mqc->end = bp + len;
        mqc->bp = bp;
        if (len==0) mqc->c = 0xff << 16;
-       else mqc->c = *mqc->bp << 16;
+       else mqc->c = (OPJ_UINT32)(*mqc->bp << 16);
 
 #ifdef MQC_PERF_OPT /* TODO_MSD: check this option and put in experimental */
        {
@@ -583,7 +593,7 @@ OPJ_INT32 opj_mqc_decode(opj_mqc_t *const mqc) {
                        d = opj_mqc_mpsexchange(mqc);
                        opj_mqc_renormd(mqc);
                } else {
-                       d = (*mqc->curctx)->mps;
+                       d = (OPJ_INT32)(*mqc->curctx)->mps;
                }
        }
 
@@ -598,7 +608,7 @@ void opj_mqc_resetstates(opj_mqc_t *mqc) {
 }
 
 void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, OPJ_INT32 prob) {
-       mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
+       mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)];
 }