[trunk] JP3D: convert from DOS to UNIX eol
authorMathieu Malaterre <mathieu.malaterre@gmail.com>
Wed, 2 Jan 2013 18:19:56 +0000 (18:19 +0000)
committerMathieu Malaterre <mathieu.malaterre@gmail.com>
Wed, 2 Jan 2013 18:19:56 +0000 (18:19 +0000)
32 files changed:
src/lib/openjp3d/bio.c
src/lib/openjp3d/bio.h
src/lib/openjp3d/cio.c
src/lib/openjp3d/cio.h
src/lib/openjp3d/dwt.h
src/lib/openjp3d/event.c
src/lib/openjp3d/event.h
src/lib/openjp3d/jp3d.c
src/lib/openjp3d/jp3d.h
src/lib/openjp3d/jp3d_lib.c
src/lib/openjp3d/jp3d_lib.h
src/lib/openjp3d/mct.c
src/lib/openjp3d/mct.h
src/lib/openjp3d/mqc.c
src/lib/openjp3d/mqc.h
src/lib/openjp3d/openjp3d.c
src/lib/openjp3d/openjp3d.h
src/lib/openjp3d/pi.c
src/lib/openjp3d/pi.h
src/lib/openjp3d/raw.c
src/lib/openjp3d/raw.h
src/lib/openjp3d/t1.c
src/lib/openjp3d/t1.h
src/lib/openjp3d/t1_3d.c
src/lib/openjp3d/t1_3d.h
src/lib/openjp3d/t2.c
src/lib/openjp3d/t2.h
src/lib/openjp3d/tcd.c
src/lib/openjp3d/tgt.c
src/lib/openjp3d/tgt.h
src/lib/openjp3d/volume.c
src/lib/openjp3d/volume.h

index bc3b1138bc403eec8a39fec264e3f759148889a5..57e909b900a70e88a67e701c4759d799e01875c1 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/** @defgroup BIO BIO - Individual bit input-output stream */\r
-/*@{*/\r
-\r
-/** @name Local static functions */\r
-/*@{*/\r
-\r
-/**\r
-Write a bit\r
-@param bio BIO handle\r
-@param b Bit to write (0 or 1)\r
-*/\r
-static void bio_putbit(opj_bio_t *bio, int b);\r
-/**\r
-Read a bit\r
-@param bio BIO handle\r
-@return Returns the read bit\r
-*/\r
-static int bio_getbit(opj_bio_t *bio);\r
-/**\r
-Write a byte\r
-@param bio BIO handle\r
-@return Returns 0 if successful, returns 1 otherwise\r
-*/\r
-static int bio_byteout(opj_bio_t *bio);\r
-/**\r
-Read a byte\r
-@param bio BIO handle\r
-@return Returns 0 if successful, returns 1 otherwise\r
-*/\r
-static int bio_bytein(opj_bio_t *bio);\r
-\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-\r
-/* \r
-==========================================================\r
-   local functions\r
-==========================================================\r
-*/\r
-\r
-static int bio_byteout(opj_bio_t *bio) {\r
-       bio->buf = (bio->buf << 8) & 0xffff;\r
-       bio->ct = bio->buf == 0xff00 ? 7 : 8;\r
-       if (bio->bp >= bio->end) {\r
-               return 1;\r
-       }\r
-       *bio->bp++ = bio->buf >> 8;\r
-       return 0;\r
-}\r
-\r
-static int bio_bytein(opj_bio_t *bio) {\r
-       bio->buf = (bio->buf << 8) & 0xffff;\r
-       bio->ct = bio->buf == 0xff00 ? 7 : 8;\r
-       if (bio->bp >= bio->end) {\r
-               return 1;\r
-       }\r
-       bio->buf |= *bio->bp++;\r
-       return 0;\r
-}\r
-\r
-static void bio_putbit(opj_bio_t *bio, int b) {\r
-       if (bio->ct == 0) {\r
-               bio_byteout(bio);\r
-       }\r
-       bio->ct--;\r
-       bio->buf |= b << bio->ct;\r
-}\r
-\r
-/* MOD antonin */\r
-static int bio_getbit(opj_bio_t *bio) {\r
-/* DOM */\r
-       if (bio->ct == 0) {\r
-               bio_bytein(bio);\r
-       }\r
-       bio->ct--;\r
-       return (bio->buf >> bio->ct) & 1;\r
-}\r
-\r
-/* \r
-==========================================================\r
-   Bit Input/Output interface\r
-==========================================================\r
-*/\r
-\r
-opj_bio_t* bio_create() {\r
-       opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t));\r
-       return bio;\r
-}\r
-\r
-void bio_destroy(opj_bio_t *bio) {\r
-       if(bio) {\r
-               opj_free(bio);\r
-       }\r
-}\r
-\r
-int bio_numbytes(opj_bio_t *bio) {\r
-       return (bio->bp - bio->start);\r
-}\r
-\r
-void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len) {\r
-       bio->start = bp;\r
-       bio->end = bp + len;\r
-       bio->bp = bp;\r
-       bio->buf = 0;\r
-       bio->ct = 8;\r
-}\r
-\r
-void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len) {\r
-       bio->start = bp;\r
-       bio->end = bp + len;\r
-       bio->bp = bp;\r
-       bio->buf = 0;\r
-       bio->ct = 0;\r
-}\r
-\r
-void bio_write(opj_bio_t *bio, int v, int n) {\r
-       int i;\r
-       for (i = n - 1; i >= 0; i--) {\r
-               bio_putbit(bio, (v >> i) & 1);\r
-       }\r
-}\r
-\r
-int bio_read(opj_bio_t *bio, int n) {\r
-       int i, v;\r
-       v = 0;\r
-       for (i = n - 1; i >= 0; i--) {\r
-               v += bio_getbit(bio) << i;\r
-       }\r
-       return v;\r
-}\r
-\r
-int bio_flush(opj_bio_t *bio) {\r
-       bio->ct = 0;\r
-       if (bio_byteout(bio)) {\r
-               return 1;\r
-       }\r
-       if (bio->ct == 7) {\r
-               bio->ct = 0;\r
-               if (bio_byteout(bio)) {\r
-                       return 1;\r
-               }\r
-       }\r
-       return 0;\r
-}\r
-\r
-int bio_inalign(opj_bio_t *bio) {\r
-       bio->ct = 0;\r
-       if ((bio->buf & 0xff) == 0xff) {\r
-               if (bio_bytein(bio)) {\r
-                       return 1;\r
-               }\r
-               bio->ct = 0;\r
-       }\r
-       return 0;\r
-}\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup BIO BIO - Individual bit input-output stream */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+/**
+Write a bit
+@param bio BIO handle
+@param b Bit to write (0 or 1)
+*/
+static void bio_putbit(opj_bio_t *bio, int b);
+/**
+Read a bit
+@param bio BIO handle
+@return Returns the read bit
+*/
+static int bio_getbit(opj_bio_t *bio);
+/**
+Write a byte
+@param bio BIO handle
+@return Returns 0 if successful, returns 1 otherwise
+*/
+static int bio_byteout(opj_bio_t *bio);
+/**
+Read a byte
+@param bio BIO handle
+@return Returns 0 if successful, returns 1 otherwise
+*/
+static int bio_bytein(opj_bio_t *bio);
+
+/*@}*/
+
+/*@}*/
+
+
+/* 
+==========================================================
+   local functions
+==========================================================
+*/
+
+static int bio_byteout(opj_bio_t *bio) {
+       bio->buf = (bio->buf << 8) & 0xffff;
+       bio->ct = bio->buf == 0xff00 ? 7 : 8;
+       if (bio->bp >= bio->end) {
+               return 1;
+       }
+       *bio->bp++ = bio->buf >> 8;
+       return 0;
+}
+
+static int bio_bytein(opj_bio_t *bio) {
+       bio->buf = (bio->buf << 8) & 0xffff;
+       bio->ct = bio->buf == 0xff00 ? 7 : 8;
+       if (bio->bp >= bio->end) {
+               return 1;
+       }
+       bio->buf |= *bio->bp++;
+       return 0;
+}
+
+static void bio_putbit(opj_bio_t *bio, int b) {
+       if (bio->ct == 0) {
+               bio_byteout(bio);
+       }
+       bio->ct--;
+       bio->buf |= b << bio->ct;
+}
+
+/* MOD antonin */
+static int bio_getbit(opj_bio_t *bio) {
+/* DOM */
+       if (bio->ct == 0) {
+               bio_bytein(bio);
+       }
+       bio->ct--;
+       return (bio->buf >> bio->ct) & 1;
+}
+
+/* 
+==========================================================
+   Bit Input/Output interface
+==========================================================
+*/
+
+opj_bio_t* bio_create() {
+       opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t));
+       return bio;
+}
+
+void bio_destroy(opj_bio_t *bio) {
+       if(bio) {
+               opj_free(bio);
+       }
+}
+
+int bio_numbytes(opj_bio_t *bio) {
+       return (bio->bp - bio->start);
+}
+
+void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len) {
+       bio->start = bp;
+       bio->end = bp + len;
+       bio->bp = bp;
+       bio->buf = 0;
+       bio->ct = 8;
+}
+
+void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len) {
+       bio->start = bp;
+       bio->end = bp + len;
+       bio->bp = bp;
+       bio->buf = 0;
+       bio->ct = 0;
+}
+
+void bio_write(opj_bio_t *bio, int v, int n) {
+       int i;
+       for (i = n - 1; i >= 0; i--) {
+               bio_putbit(bio, (v >> i) & 1);
+       }
+}
+
+int bio_read(opj_bio_t *bio, int n) {
+       int i, v;
+       v = 0;
+       for (i = n - 1; i >= 0; i--) {
+               v += bio_getbit(bio) << i;
+       }
+       return v;
+}
+
+int bio_flush(opj_bio_t *bio) {
+       bio->ct = 0;
+       if (bio_byteout(bio)) {
+               return 1;
+       }
+       if (bio->ct == 7) {
+               bio->ct = 0;
+               if (bio_byteout(bio)) {
+                       return 1;
+               }
+       }
+       return 0;
+}
+
+int bio_inalign(opj_bio_t *bio) {
+       bio->ct = 0;
+       if ((bio->buf & 0xff) == 0xff) {
+               if (bio_bytein(bio)) {
+                       return 1;
+               }
+               bio->ct = 0;
+       }
+       return 0;
+}
index 2c91342f7af3bf11f34479498bdd1bb582cc22e4..183eb0a1f59a824278d67a7dd60e1565113181e4 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __BIO_H\r
-#define __BIO_H\r
-/** \r
-@file bio.h\r
-@brief Implementation of an individual bit input-output (BIO)\r
-\r
-The functions in BIO.C have for goal to realize an individual bit input - output.\r
-*/\r
-\r
-/** @defgroup BIO BIO - Individual bit input-output stream */\r
-/*@{*/\r
-\r
-/**\r
-Individual bit input-output stream (BIO)\r
-*/\r
-typedef struct opj_bio {\r
-/** pointer to the start of the buffer */\r
-       unsigned char *start;   \r
-/** pointer to the end of the buffer */\r
-       unsigned char *end;             \r
-/** pointer to the present position in the buffer */\r
-       unsigned char *bp;              \r
-/** temporary place where each byte is read or written */\r
-       unsigned int buf;               \r
-/** coder : number of bits free to write. decoder : number of bits read */\r
-       int ct;                                 \r
-} opj_bio_t;\r
-\r
-/** @name Funciones generales */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Create a new BIO handle \r
-@return Returns a new BIO handle if successful, returns NULL otherwise\r
-*/\r
-opj_bio_t* bio_create(void);\r
-/**\r
-Destroy a previously created BIO handle\r
-@param bio BIO handle to destroy\r
-*/\r
-void bio_destroy(opj_bio_t *bio);\r
-/**\r
-Number of bytes written.\r
-@param bio BIO handle\r
-@return Returns the number of bytes written\r
-*/\r
-int bio_numbytes(opj_bio_t *bio);\r
-/**\r
-Init encoder\r
-@param bio BIO handle\r
-@param bp Output buffer\r
-@param len Output buffer length \r
-*/\r
-void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len);\r
-/**\r
-Init decoder\r
-@param bio BIO handle\r
-@param bp Input buffer\r
-@param len Input buffer length \r
-*/\r
-void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len);\r
-/**\r
-Write bits\r
-@param bio BIO handle\r
-@param v Value of bits\r
-@param n Number of bits to write\r
-*/\r
-void bio_write(opj_bio_t *bio, int v, int n);\r
-/**\r
-Read bits\r
-@param bio BIO handle\r
-@param n Number of bits to read \r
-@return Returns the corresponding read number\r
-*/\r
-int bio_read(opj_bio_t *bio, int n);\r
-/**\r
-Flush bits\r
-@param bio BIO handle\r
-@return Returns 1 if successful, returns 0 otherwise\r
-*/\r
-int bio_flush(opj_bio_t *bio);\r
-/**\r
-Passes the ending bits (coming from flushing)\r
-@param bio BIO handle\r
-@return Returns 1 if successful, returns 0 otherwise\r
-*/\r
-int bio_inalign(opj_bio_t *bio);\r
-/**\r
-Read a bit\r
-@param bio BIO handle\r
-@return Returns the read bit\r
-*/\r
-/* MOD antonin */\r
-/*int bio_getbit(opj_bio_t *bio);*/\r
-/* DOM */\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __BIO_H */\r
-\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __BIO_H
+#define __BIO_H
+/** 
+@file bio.h
+@brief Implementation of an individual bit input-output (BIO)
+
+The functions in BIO.C have for goal to realize an individual bit input - output.
+*/
+
+/** @defgroup BIO BIO - Individual bit input-output stream */
+/*@{*/
+
+/**
+Individual bit input-output stream (BIO)
+*/
+typedef struct opj_bio {
+/** pointer to the start of the buffer */
+       unsigned char *start;   
+/** pointer to the end of the buffer */
+       unsigned char *end;             
+/** pointer to the present position in the buffer */
+       unsigned char *bp;              
+/** temporary place where each byte is read or written */
+       unsigned int buf;               
+/** coder : number of bits free to write. decoder : number of bits read */
+       int ct;                                 
+} opj_bio_t;
+
+/** @name Funciones generales */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a new BIO handle 
+@return Returns a new BIO handle if successful, returns NULL otherwise
+*/
+opj_bio_t* bio_create(void);
+/**
+Destroy a previously created BIO handle
+@param bio BIO handle to destroy
+*/
+void bio_destroy(opj_bio_t *bio);
+/**
+Number of bytes written.
+@param bio BIO handle
+@return Returns the number of bytes written
+*/
+int bio_numbytes(opj_bio_t *bio);
+/**
+Init encoder
+@param bio BIO handle
+@param bp Output buffer
+@param len Output buffer length 
+*/
+void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len);
+/**
+Init decoder
+@param bio BIO handle
+@param bp Input buffer
+@param len Input buffer length 
+*/
+void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len);
+/**
+Write bits
+@param bio BIO handle
+@param v Value of bits
+@param n Number of bits to write
+*/
+void bio_write(opj_bio_t *bio, int v, int n);
+/**
+Read bits
+@param bio BIO handle
+@param n Number of bits to read 
+@return Returns the corresponding read number
+*/
+int bio_read(opj_bio_t *bio, int n);
+/**
+Flush bits
+@param bio BIO handle
+@return Returns 1 if successful, returns 0 otherwise
+*/
+int bio_flush(opj_bio_t *bio);
+/**
+Passes the ending bits (coming from flushing)
+@param bio BIO handle
+@return Returns 1 if successful, returns 0 otherwise
+*/
+int bio_inalign(opj_bio_t *bio);
+/**
+Read a bit
+@param bio BIO handle
+@return Returns the read bit
+*/
+/* MOD antonin */
+/*int bio_getbit(opj_bio_t *bio);*/
+/* DOM */
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __BIO_H */
+
index 4e99c847ddc3bb86509298d0b620cd24349f08d1..10bd2af329bbe9faf64f512f4cab4b68b95bf592 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {\r
-       opj_cp_t *cp = NULL;\r
-       opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));\r
-       if(!cio) return NULL;\r
-       cio->cinfo = cinfo;\r
-       if(buffer && length) {\r
-               /* wrap a user buffer containing the encoded image */\r
-               cio->openmode = OPJ_STREAM_READ;\r
-               cio->buffer = buffer;\r
-               cio->length = length;\r
-       }\r
-       else if(!buffer && !length && cinfo) {\r
-               /* allocate a buffer for the encoded image */\r
-               cio->openmode = OPJ_STREAM_WRITE;\r
-               switch(cinfo->codec_format) {\r
-                       case CODEC_J3D:\r
-                       case CODEC_J2K:\r
-                               cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp;\r
-                               break;\r
-                       default:\r
-                               opj_free(cio);\r
-                               return NULL;\r
-               }\r
-               cio->length = cp->tdx * cp->tdy * cp->tdz * cp->tw * cp->th * cp->tl * 4;\r
-               cio->buffer = (unsigned char *)opj_malloc(cio->length);\r
-               if(!cio->buffer) {\r
-                       opj_free(cio);\r
-                       return NULL;\r
-               }\r
-       }\r
-       else {\r
-               opj_free(cio);\r
-               return NULL;\r
-       }\r
-\r
-       /* Initialize byte IO */\r
-       cio->start = cio->buffer;\r
-       cio->end = cio->buffer + cio->length;\r
-       cio->bp = cio->buffer;\r
-\r
-       return cio;\r
-}\r
-\r
-void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {\r
-       if(cio) {\r
-               if(cio->openmode == OPJ_STREAM_WRITE) {\r
-                       /* destroy the allocated buffer */\r
-                       opj_free(cio->buffer);\r
-               }\r
-               /* destroy the cio */\r
-               opj_free(cio);\r
-       }\r
-}\r
-\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/*\r
- * Get position in byte stream.\r
- */\r
-int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {\r
-       return cio->bp - cio->start;\r
-}\r
-\r
-/*\r
- * Set position in byte stream.\r
- *\r
- * pos : position, in number of bytes, from the beginning of the stream\r
- */\r
-void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {\r
-       cio->bp = cio->start + pos;\r
-}\r
-\r
-/*\r
- * Number of bytes left before the end of the stream.\r
- */\r
-int cio_numbytesleft(opj_cio_t *cio) {\r
-       return cio->end - cio->bp;\r
-}\r
-\r
-/*\r
- * Get pointer to the current position in the stream.\r
- */\r
-unsigned char *cio_getbp(opj_cio_t *cio) {\r
-       return cio->bp;\r
-}\r
-\r
-/*\r
- * Write a byte.\r
- */\r
-static bool cio_byteout(opj_cio_t *cio, unsigned char v) {\r
-       if (cio->bp >= cio->end) {\r
-               opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");\r
-               return false;\r
-       }\r
-       *cio->bp++ = v;\r
-       return true;\r
-}\r
-\r
-/*\r
- * Read a byte.\r
- */\r
-static unsigned char cio_bytein(opj_cio_t *cio) {\r
-       if (cio->bp >= cio->end) {\r
-               opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");\r
-               return 0;\r
-       }\r
-       return *cio->bp++;\r
-}\r
-\r
-/*\r
- * Write some bytes.\r
- *\r
- * v : value to write\r
- * n : number of bytes to write\r
- */\r
-unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {\r
-       int i;\r
-       for (i = n - 1; i >= 0; i--) {\r
-               if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )\r
-                       return 0;\r
-       }\r
-       return n;\r
-}\r
-\r
-/*\r
- * Read some bytes.\r
- *\r
- * n : number of bytes to read\r
- *\r
- * return : value of the n bytes read\r
- */\r
-unsigned int cio_read(opj_cio_t *cio, int n) {\r
-       int i;\r
-       unsigned int v;\r
-       v = 0;\r
-       for (i = n - 1; i >= 0; i--) {\r
-               v += cio_bytein(cio) << (i << 3);\r
-       }\r
-       return v;\r
-}\r
-\r
-/* \r
- * Skip some bytes.\r
- *\r
- * n : number of bytes to skip\r
- */\r
-void cio_skip(opj_cio_t *cio, int n) {\r
-       cio->bp += n;\r
-}\r
-\r
-/*\r
- * Write some bytes.\r
- *\r
- * v : value to write\r
- * n : number of bytes to write\r
- */\r
-int cio_write_int(opj_cio_t *cio, int v, int n) {\r
-       int i;\r
-       for (i = n - 1; i >= 0; i--) {\r
-               if( !cio_byteout(cio, (char) ((v >> (i << 3)) & 0xff)) )\r
-                       return 0;\r
-       }\r
-       return n;\r
-}\r
-\r
-/*\r
- * Read some bytes.\r
- *\r
- * n : number of bytes to read\r
- *\r
- * return : value of the n bytes read\r
- */\r
-int cio_read_int(opj_cio_t *cio, int n) {\r
-       int i;\r
-       int v;\r
-       v = 0;\r
-       for (i = n - 1; i >= 0; i--) {\r
-               v += cio_bytein(cio) << (i << 3);\r
-       }\r
-       return v;\r
-}\r
-\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/* ----------------------------------------------------------------------- */
+
+opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
+       opj_cp_t *cp = NULL;
+       opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
+       if(!cio) return NULL;
+       cio->cinfo = cinfo;
+       if(buffer && length) {
+               /* wrap a user buffer containing the encoded image */
+               cio->openmode = OPJ_STREAM_READ;
+               cio->buffer = buffer;
+               cio->length = length;
+       }
+       else if(!buffer && !length && cinfo) {
+               /* allocate a buffer for the encoded image */
+               cio->openmode = OPJ_STREAM_WRITE;
+               switch(cinfo->codec_format) {
+                       case CODEC_J3D:
+                       case CODEC_J2K:
+                               cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp;
+                               break;
+                       default:
+                               opj_free(cio);
+                               return NULL;
+               }
+               cio->length = cp->tdx * cp->tdy * cp->tdz * cp->tw * cp->th * cp->tl * 4;
+               cio->buffer = (unsigned char *)opj_malloc(cio->length);
+               if(!cio->buffer) {
+                       opj_free(cio);
+                       return NULL;
+               }
+       }
+       else {
+               opj_free(cio);
+               return NULL;
+       }
+
+       /* Initialize byte IO */
+       cio->start = cio->buffer;
+       cio->end = cio->buffer + cio->length;
+       cio->bp = cio->buffer;
+
+       return cio;
+}
+
+void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
+       if(cio) {
+               if(cio->openmode == OPJ_STREAM_WRITE) {
+                       /* destroy the allocated buffer */
+                       opj_free(cio->buffer);
+               }
+               /* destroy the cio */
+               opj_free(cio);
+       }
+}
+
+
+/* ----------------------------------------------------------------------- */
+
+/*
+ * Get position in byte stream.
+ */
+int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
+       return cio->bp - cio->start;
+}
+
+/*
+ * Set position in byte stream.
+ *
+ * pos : position, in number of bytes, from the beginning of the stream
+ */
+void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
+       cio->bp = cio->start + pos;
+}
+
+/*
+ * Number of bytes left before the end of the stream.
+ */
+int cio_numbytesleft(opj_cio_t *cio) {
+       return cio->end - cio->bp;
+}
+
+/*
+ * Get pointer to the current position in the stream.
+ */
+unsigned char *cio_getbp(opj_cio_t *cio) {
+       return cio->bp;
+}
+
+/*
+ * Write a byte.
+ */
+static bool cio_byteout(opj_cio_t *cio, unsigned char v) {
+       if (cio->bp >= cio->end) {
+               opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
+               return false;
+       }
+       *cio->bp++ = v;
+       return true;
+}
+
+/*
+ * Read a byte.
+ */
+static unsigned char cio_bytein(opj_cio_t *cio) {
+       if (cio->bp >= cio->end) {
+               opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
+               return 0;
+       }
+       return *cio->bp++;
+}
+
+/*
+ * Write some bytes.
+ *
+ * v : value to write
+ * n : number of bytes to write
+ */
+unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {
+       int i;
+       for (i = n - 1; i >= 0; i--) {
+               if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
+                       return 0;
+       }
+       return n;
+}
+
+/*
+ * Read some bytes.
+ *
+ * n : number of bytes to read
+ *
+ * return : value of the n bytes read
+ */
+unsigned int cio_read(opj_cio_t *cio, int n) {
+       int i;
+       unsigned int v;
+       v = 0;
+       for (i = n - 1; i >= 0; i--) {
+               v += cio_bytein(cio) << (i << 3);
+       }
+       return v;
+}
+
+/* 
+ * Skip some bytes.
+ *
+ * n : number of bytes to skip
+ */
+void cio_skip(opj_cio_t *cio, int n) {
+       cio->bp += n;
+}
+
+/*
+ * Write some bytes.
+ *
+ * v : value to write
+ * n : number of bytes to write
+ */
+int cio_write_int(opj_cio_t *cio, int v, int n) {
+       int i;
+       for (i = n - 1; i >= 0; i--) {
+               if( !cio_byteout(cio, (char) ((v >> (i << 3)) & 0xff)) )
+                       return 0;
+       }
+       return n;
+}
+
+/*
+ * Read some bytes.
+ *
+ * n : number of bytes to read
+ *
+ * return : value of the n bytes read
+ */
+int cio_read_int(opj_cio_t *cio, int n) {
+       int i;
+       int v;
+       v = 0;
+       for (i = n - 1; i >= 0; i--) {
+               v += cio_bytein(cio) << (i << 3);
+       }
+       return v;
+}
+
index b7b0c416cb2d22ee2d737d2900a832cf9ebb059d..aee5b3304d3de70277c66d57ba516d42520b2273 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __CIO_H\r
-#define __CIO_H\r
-/**\r
-@file cio.h\r
-@brief Implementation of a byte input-output process (CIO)\r
-\r
-The functions in CIO.C have for goal to realize a byte input / output process.\r
-*/\r
-\r
-/** @defgroup CIO CIO - byte input-output stream */\r
-/*@{*/\r
-\r
-/** @name Funciones generales (see also openjp3d.h) */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Number of bytes left before the end of the stream\r
-@param cio CIO handle\r
-@return Returns the number of bytes before the end of the stream\r
-*/\r
-int cio_numbytesleft(opj_cio_t *cio);\r
-/**\r
-Get pointer to the current position in the stream\r
-@param cio CIO handle\r
-@return Returns a pointer to the current position\r
-*/\r
-unsigned char *cio_getbp(opj_cio_t *cio);\r
-/**\r
-Write some bytes\r
-@param cio CIO handle\r
-@param v Value to write\r
-@param n Number of bytes to write\r
-@return Returns the number of bytes written or 0 if an error occured\r
-*/\r
-unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n);\r
-/**\r
-Read some bytes\r
-@param cio CIO handle\r
-@param n Number of bytes to read\r
-@return Returns the value of the n bytes read\r
-*/\r
-unsigned int cio_read(opj_cio_t *cio, int n);\r
-/**\r
-Skip some bytes\r
-@param cio CIO handle\r
-@param n Number of bytes to skip\r
-*/\r
-void cio_skip(opj_cio_t *cio, int n);\r
-/**\r
-Write some bytes\r
-@param cio CIO handle\r
-@param v Signed integer value to write\r
-@param n Number of bytes to write\r
-@return Returns the number of bytes written or 0 if an error occured\r
-*/\r
-int cio_write_int(opj_cio_t *cio, int v, int n);\r
-/**\r
-Read some bytes\r
-@param cio CIO handle\r
-@param n Number of bytes to read\r
-@return Returns the value of the n bytes read\r
-*/\r
-int cio_read_int(opj_cio_t *cio, int n);\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __CIO_H */\r
-\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __CIO_H
+#define __CIO_H
+/**
+@file cio.h
+@brief Implementation of a byte input-output process (CIO)
+
+The functions in CIO.C have for goal to realize a byte input / output process.
+*/
+
+/** @defgroup CIO CIO - byte input-output stream */
+/*@{*/
+
+/** @name Funciones generales (see also openjp3d.h) */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Number of bytes left before the end of the stream
+@param cio CIO handle
+@return Returns the number of bytes before the end of the stream
+*/
+int cio_numbytesleft(opj_cio_t *cio);
+/**
+Get pointer to the current position in the stream
+@param cio CIO handle
+@return Returns a pointer to the current position
+*/
+unsigned char *cio_getbp(opj_cio_t *cio);
+/**
+Write some bytes
+@param cio CIO handle
+@param v Value to write
+@param n Number of bytes to write
+@return Returns the number of bytes written or 0 if an error occured
+*/
+unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n);
+/**
+Read some bytes
+@param cio CIO handle
+@param n Number of bytes to read
+@return Returns the value of the n bytes read
+*/
+unsigned int cio_read(opj_cio_t *cio, int n);
+/**
+Skip some bytes
+@param cio CIO handle
+@param n Number of bytes to skip
+*/
+void cio_skip(opj_cio_t *cio, int n);
+/**
+Write some bytes
+@param cio CIO handle
+@param v Signed integer value to write
+@param n Number of bytes to write
+@return Returns the number of bytes written or 0 if an error occured
+*/
+int cio_write_int(opj_cio_t *cio, int v, int n);
+/**
+Read some bytes
+@param cio CIO handle
+@param n Number of bytes to read
+@return Returns the value of the n bytes read
+*/
+int cio_read_int(opj_cio_t *cio, int n);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __CIO_H */
+
index 3fa781b101761c08ab512d6eb4eeda47806aec15..9ad6237cc5f1b9bedd78e1ec03925bfcea92d785 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * Copyrigth (c) 2006, M�nica D�ez, LPI-UVA, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __DWT_H\r
-#define __DWT_H\r
-/**\r
-@file dwt.h\r
-@brief Implementation of a discrete wavelet transform (DWT)\r
-\r
-The functions in DWT.C have for goal to realize forward and inverse discret wavelet\r
-transform with filter 5-3 (reversible) and filter 9-7 (irreversible). The functions in\r
-DWT.C are used by some function in TCD.C.\r
-*/\r
-\r
-/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */\r
-/*@{*/\r
-\r
-/**\r
-DCCS-LIWT properties\r
-*/\r
-\r
-\r
-typedef struct opj_wtfilt {\r
-       double *LPS;\r
-       int lenLPS;\r
-       double *HPS;\r
-       int lenHPS;\r
-} opj_wtfilt_t;\r
-/** @name Funciones generales */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Forward 5-3 wavelet tranform in 3-D. \r
-Apply a reversible DWT transform to a component of an volume.\r
-@param tilec Tile component information (current tile)\r
-@param dwtid Number of identification of wavelet kernel(s) used in DWT in each direction\r
-*/\r
-void dwt_encode(opj_tcd_tilecomp_t * tilec, int dwtid[3]);\r
-/**\r
-Inverse 5-3 wavelet tranform in 3-D.\r
-Apply a reversible inverse DWT transform to a component of an volume.\r
-@param tilec Tile component information (current tile)\r
-@param stops Number of decoded resolution levels in each dimension\r
-@param dwtid Number of identification of wavelet kernel(s) used in DWT in each dimension\r
-*/\r
-void dwt_decode(opj_tcd_tilecomp_t * tilec, int stops[3], int dwtid[3]);\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Get the gain of a subband for the reversible 3-D DWT.\r
-@param orient Number that identifies the subband (0->LLL, 1->HLL, 2->LHL, 3->HHL, 4->LLH, 5->HLH, 6->LHH, 7->HHH)\r
-@param reversible Wavelet transformation type\r
-@return Returns 0 if orient = 0, returns 1 if orient = 1,2 or 4, returns 2 if orient = 3,5 or 6, returns 3 otherwise\r
-*/\r
-int dwt_getgain(int orient, int reversible);\r
-/**\r
-Get the norm of a wavelet function of a subband at a specified level for the reversible 5-3 DWT or irreversible 9-7 in 3-D.\r
-@param orient Band of the wavelet function\r
-@param level Levels of the wavelet function in X,Y,Z axis\r
-@param dwtid Wavelet transformation identifier\r
-@return Returns the norm of the wavelet function\r
-*/\r
-double dwt_getnorm(int orient, int level[3], int dwtid[3]);\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Calcula el valor del escal�n de cuantificaci�n correspondiente a cada subbanda.\r
-@param tccp Tile component coding parameters\r
-@param prec Precision of data\r
-*/\r
-void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec);\r
-/*@}*/\r
-/*@}*/\r
-\r
-#endif /* __DWT_H */\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyrigth (c) 2006, M�nica D�ez, LPI-UVA, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __DWT_H
+#define __DWT_H
+/**
+@file dwt.h
+@brief Implementation of a discrete wavelet transform (DWT)
+
+The functions in DWT.C have for goal to realize forward and inverse discret wavelet
+transform with filter 5-3 (reversible) and filter 9-7 (irreversible). The functions in
+DWT.C are used by some function in TCD.C.
+*/
+
+/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
+/*@{*/
+
+/**
+DCCS-LIWT properties
+*/
+
+
+typedef struct opj_wtfilt {
+       double *LPS;
+       int lenLPS;
+       double *HPS;
+       int lenHPS;
+} opj_wtfilt_t;
+/** @name Funciones generales */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Forward 5-3 wavelet tranform in 3-D. 
+Apply a reversible DWT transform to a component of an volume.
+@param tilec Tile component information (current tile)
+@param dwtid Number of identification of wavelet kernel(s) used in DWT in each direction
+*/
+void dwt_encode(opj_tcd_tilecomp_t * tilec, int dwtid[3]);
+/**
+Inverse 5-3 wavelet tranform in 3-D.
+Apply a reversible inverse DWT transform to a component of an volume.
+@param tilec Tile component information (current tile)
+@param stops Number of decoded resolution levels in each dimension
+@param dwtid Number of identification of wavelet kernel(s) used in DWT in each dimension
+*/
+void dwt_decode(opj_tcd_tilecomp_t * tilec, int stops[3], int dwtid[3]);
+/* ----------------------------------------------------------------------- */
+/**
+Get the gain of a subband for the reversible 3-D DWT.
+@param orient Number that identifies the subband (0->LLL, 1->HLL, 2->LHL, 3->HHL, 4->LLH, 5->HLH, 6->LHH, 7->HHH)
+@param reversible Wavelet transformation type
+@return Returns 0 if orient = 0, returns 1 if orient = 1,2 or 4, returns 2 if orient = 3,5 or 6, returns 3 otherwise
+*/
+int dwt_getgain(int orient, int reversible);
+/**
+Get the norm of a wavelet function of a subband at a specified level for the reversible 5-3 DWT or irreversible 9-7 in 3-D.
+@param orient Band of the wavelet function
+@param level Levels of the wavelet function in X,Y,Z axis
+@param dwtid Wavelet transformation identifier
+@return Returns the norm of the wavelet function
+*/
+double dwt_getnorm(int orient, int level[3], int dwtid[3]);
+/* ----------------------------------------------------------------------- */
+/**
+Calcula el valor del escal�n de cuantificaci�n correspondiente a cada subbanda.
+@param tccp Tile component coding parameters
+@param prec Precision of data
+*/
+void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec);
+/*@}*/
+/*@}*/
+
+#endif /* __DWT_H */
index 4c86c332a082cf57f4910c4864b1f245aaeda6a4..f9e4da0e49391861d953cfd0fe0a367ebe9db517 100644 (file)
-/*\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/* ==========================================================\r
-//   Utility functions\r
-// ==========================================================*/\r
-\r
-#ifndef _WIN32\r
-static char*\r
-i2a(unsigned i, char *a, unsigned r) {\r
-       if (i/r > 0) a = i2a(i/r,a,r);\r
-       *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];\r
-       return a+1;\r
-}\r
-\r
-/** \r
- Transforms integer i into an ascii string and stores the result in a; \r
- string is encoded in the base indicated by r.\r
- @param i Number to be converted\r
- @param a String result\r
- @param r Base of value; must be in the range 2 - 36\r
- @return Returns a\r
-*/\r
-static char *\r
-_itoa(int i, char *a, int r) {\r
-       r = ((r < 2) || (r > 36)) ? 10 : r;\r
-       if(i < 0) {\r
-               *a = '-';\r
-               *i2a(-i, a+1, r) = 0;\r
-       }\r
-       else *i2a(i, a, r) = 0;\r
-       return a;\r
-}\r
-\r
-#endif /* !_WIN32 */\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) {\r
-       if(cinfo) {\r
-               opj_event_mgr_t *previous = cinfo->event_mgr;\r
-               cinfo->event_mgr = event_mgr;\r
-               cinfo->client_data = context;\r
-               return previous;\r
-       }\r
-\r
-       return NULL;\r
-}\r
-\r
-bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) {\r
-#define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */\r
-       opj_msg_callback msg_handler = NULL;\r
-\r
-       opj_event_mgr_t *event_mgr = cinfo->event_mgr;\r
-       if(event_mgr != NULL) {\r
-               switch(event_type) {\r
-                       case EVT_ERROR:\r
-                               msg_handler = event_mgr->error_handler;\r
-                               break;\r
-                       case EVT_WARNING:\r
-                               msg_handler = event_mgr->warning_handler;\r
-                               break;\r
-                       case EVT_INFO:\r
-                               msg_handler = event_mgr->info_handler;\r
-                               break;\r
-                       default:\r
-                               break;\r
-               }\r
-               if(msg_handler == NULL) {\r
-                       return false;\r
-               }\r
-       } else {\r
-               return false;\r
-       }\r
-\r
-       if ((fmt != NULL) && (event_mgr != NULL)) {\r
-               va_list arg;\r
-               int str_length, i, j;\r
-               char message[MSG_SIZE];\r
-               memset(message, 0, MSG_SIZE);\r
-               /* initialize the optional parameter list */\r
-               va_start(arg, fmt);\r
-               /* check the length of the format string */\r
-               str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt);\r
-               /* parse the format string and put the result in 'message' */\r
-               for (i = 0, j = 0; i < str_length; ++i) {\r
-                       if (fmt[i] == '%') {\r
-                               if (i + 1 < str_length) {\r
-                                       switch(tolower(fmt[i + 1])) {\r
-                                               case '%' :\r
-                                                       message[j++] = '%';\r
-                                                       break;\r
-                                               case 'o' : /* octal numbers */\r
-                                               {\r
-                                                       char tmp[16];\r
-                                                       _itoa(va_arg(arg, int), tmp, 8);\r
-                                                       strcat(message, tmp);\r
-                                                       j += strlen(tmp);\r
-                                                       ++i;\r
-                                                       break;\r
-                                               }\r
-                                               case 'i' : /* decimal numbers */\r
-                                               case 'd' :\r
-                                               {\r
-                                                       char tmp[16];\r
-                                                       _itoa(va_arg(arg, int), tmp, 10);\r
-                                                       strcat(message, tmp);\r
-                                                       j += strlen(tmp);\r
-                                                       ++i;\r
-                                                       break;\r
-                                               }\r
-                                               case 'x' : /* hexadecimal numbers */\r
-                                               {\r
-                                                       char tmp[16];\r
-                                                       _itoa(va_arg(arg, int), tmp, 16);\r
-                                                       strcat(message, tmp);\r
-                                                       j += strlen(tmp);\r
-                                                       ++i;\r
-                                                       break;\r
-                                               }\r
-                                               case 's' : /* strings */\r
-                                               {\r
-                                                       char *tmp = va_arg(arg, char*);\r
-                                                       strcat(message, tmp);\r
-                                                       j += strlen(tmp);\r
-                                                       ++i;\r
-                                                       break;\r
-                                               }\r
-                                               case 'f' :      /* floats */\r
-                                               {\r
-                                                       char tmp[16];\r
-                                                       double value = va_arg(arg, double);\r
-                                                       sprintf(tmp, "%f", value);\r
-                                                       strcat(message, tmp);\r
-                                                       j += strlen(tmp);\r
-                                                       ++i;\r
-                                                       break;\r
-                                               }\r
-                                       };\r
-                               } else {\r
-                                       message[j++] = fmt[i];\r
-                               }\r
-                       } else {\r
-                               message[j++] = fmt[i];\r
-                       };\r
-               }\r
-               /* deinitialize the optional parameter list */\r
-               va_end(arg);\r
-\r
-               /* output the message to the user program */\r
-               msg_handler(message, cinfo->client_data);\r
-       }\r
-\r
-       return true;\r
-}\r
-\r
+/*
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/* ==========================================================
+//   Utility functions
+// ==========================================================*/
+
+#ifndef _WIN32
+static char*
+i2a(unsigned i, char *a, unsigned r) {
+       if (i/r > 0) a = i2a(i/r,a,r);
+       *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
+       return a+1;
+}
+
+/** 
+ Transforms integer i into an ascii string and stores the result in a; 
+ string is encoded in the base indicated by r.
+ @param i Number to be converted
+ @param a String result
+ @param r Base of value; must be in the range 2 - 36
+ @return Returns a
+*/
+static char *
+_itoa(int i, char *a, int r) {
+       r = ((r < 2) || (r > 36)) ? 10 : r;
+       if(i < 0) {
+               *a = '-';
+               *i2a(-i, a+1, r) = 0;
+       }
+       else *i2a(i, a, r) = 0;
+       return a;
+}
+
+#endif /* !_WIN32 */
+
+/* ----------------------------------------------------------------------- */
+
+opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) {
+       if(cinfo) {
+               opj_event_mgr_t *previous = cinfo->event_mgr;
+               cinfo->event_mgr = event_mgr;
+               cinfo->client_data = context;
+               return previous;
+       }
+
+       return NULL;
+}
+
+bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) {
+#define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
+       opj_msg_callback msg_handler = NULL;
+
+       opj_event_mgr_t *event_mgr = cinfo->event_mgr;
+       if(event_mgr != NULL) {
+               switch(event_type) {
+                       case EVT_ERROR:
+                               msg_handler = event_mgr->error_handler;
+                               break;
+                       case EVT_WARNING:
+                               msg_handler = event_mgr->warning_handler;
+                               break;
+                       case EVT_INFO:
+                               msg_handler = event_mgr->info_handler;
+                               break;
+                       default:
+                               break;
+               }
+               if(msg_handler == NULL) {
+                       return false;
+               }
+       } else {
+               return false;
+       }
+
+       if ((fmt != NULL) && (event_mgr != NULL)) {
+               va_list arg;
+               int str_length, i, j;
+               char message[MSG_SIZE];
+               memset(message, 0, MSG_SIZE);
+               /* initialize the optional parameter list */
+               va_start(arg, fmt);
+               /* check the length of the format string */
+               str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt);
+               /* parse the format string and put the result in 'message' */
+               for (i = 0, j = 0; i < str_length; ++i) {
+                       if (fmt[i] == '%') {
+                               if (i + 1 < str_length) {
+                                       switch(tolower(fmt[i + 1])) {
+                                               case '%' :
+                                                       message[j++] = '%';
+                                                       break;
+                                               case 'o' : /* octal numbers */
+                                               {
+                                                       char tmp[16];
+                                                       _itoa(va_arg(arg, int), tmp, 8);
+                                                       strcat(message, tmp);
+                                                       j += strlen(tmp);
+                                                       ++i;
+                                                       break;
+                                               }
+                                               case 'i' : /* decimal numbers */
+                                               case 'd' :
+                                               {
+                                                       char tmp[16];
+                                                       _itoa(va_arg(arg, int), tmp, 10);
+                                                       strcat(message, tmp);
+                                                       j += strlen(tmp);
+                                                       ++i;
+                                                       break;
+                                               }
+                                               case 'x' : /* hexadecimal numbers */
+                                               {
+                                                       char tmp[16];
+                                                       _itoa(va_arg(arg, int), tmp, 16);
+                                                       strcat(message, tmp);
+                                                       j += strlen(tmp);
+                                                       ++i;
+                                                       break;
+                                               }
+                                               case 's' : /* strings */
+                                               {
+                                                       char *tmp = va_arg(arg, char*);
+                                                       strcat(message, tmp);
+                                                       j += strlen(tmp);
+                                                       ++i;
+                                                       break;
+                                               }
+                                               case 'f' :      /* floats */
+                                               {
+                                                       char tmp[16];
+                                                       double value = va_arg(arg, double);
+                                                       sprintf(tmp, "%f", value);
+                                                       strcat(message, tmp);
+                                                       j += strlen(tmp);
+                                                       ++i;
+                                                       break;
+                                               }
+                                       };
+                               } else {
+                                       message[j++] = fmt[i];
+                               }
+                       } else {
+                               message[j++] = fmt[i];
+                       };
+               }
+               /* deinitialize the optional parameter list */
+               va_end(arg);
+
+               /* output the message to the user program */
+               msg_handler(message, cinfo->client_data);
+       }
+
+       return true;
+}
+
index 8f45452b684b7ae7ad12680b836872a05a130bea..4b803b5601012bcdb7fb7d47138eaba4f75f36e7 100644 (file)
@@ -1,58 +1,58 @@
-/*\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-#ifndef __EVENT_H\r
-#define __EVENT_H\r
-/**\r
-@file event.h\r
-@brief Implementation of a event callback system\r
-\r
-The functions in EVENT.C have for goal to send output messages (errors, warnings, debug) to the user.\r
-*/\r
-\r
-#define EVT_ERROR      1       /**< Error event type */\r
-#define EVT_WARNING    2       /**< Warning event type */\r
-#define EVT_INFO       4       /**< Debug event type */\r
-\r
-/** @defgroup EVENT EVENT - Implementation of a event callback system */\r
-/*@{*/\r
-\r
-/** @name Funciones generales (see also openjp3d.h) */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Write formatted data to a string and send the string to a user callback. \r
-@param cinfo Codec context info\r
-@param event_type Event type or callback to use to send the message\r
-@param fmt Format-control string (plus optionnal arguments)\r
-@return Returns true if successful, returns false otherwise\r
-*/\r
-bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...);\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __EVENT_H */\r
+/*
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __EVENT_H
+#define __EVENT_H
+/**
+@file event.h
+@brief Implementation of a event callback system
+
+The functions in EVENT.C have for goal to send output messages (errors, warnings, debug) to the user.
+*/
+
+#define EVT_ERROR      1       /**< Error event type */
+#define EVT_WARNING    2       /**< Warning event type */
+#define EVT_INFO       4       /**< Debug event type */
+
+/** @defgroup EVENT EVENT - Implementation of a event callback system */
+/*@{*/
+
+/** @name Funciones generales (see also openjp3d.h) */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Write formatted data to a string and send the string to a user callback. 
+@param cinfo Codec context info
+@param event_type Event type or callback to use to send the message
+@param fmt Format-control string (plus optionnal arguments)
+@return Returns true if successful, returns false otherwise
+*/
+bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __EVENT_H */
index bbdf0dc558a15322aa3e0ebe5799411b3226010f..1b940e70b0a806000e44e21447cd494493e0e446 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/** @defgroup J3D J3D - JPEG-2000 PART 10 codestream reader/writer */\r
-/*@{*/\r
-\r
-/** @name Funciones locales */\r
-/*@{*/\r
-\r
-/**\r
-Write the SOC marker (Start Of Codestream)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_soc(opj_j3d_t *j3d);\r
-/**\r
-Read the SOC marker (Start of Codestream)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_soc(opj_j3d_t *j3d);\r
-/**\r
-Write the SIZ marker (2D volume and tile size)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_siz(opj_j3d_t *j3d);\r
-/**\r
-Read the SIZ marker (2D volume and tile size)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_siz(opj_j3d_t *j3d);\r
-/**\r
-Write the NSI marker (3rd volume and tile size)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_nsi(opj_j3d_t *j3d);\r
-/**\r
-Read the NSI marker (3rd volume and tile size)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_nsi(opj_j3d_t *j3d);\r
-/**\r
-Write the COM marker (comment)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_com(opj_j3d_t *j3d);\r
-/**\r
-Read the COM marker (comment)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_com(opj_j3d_t *j3d);\r
-/**\r
-Write the value concerning the specified component in the marker COD and COC\r
-@param j3d J3D handle\r
-@param compno Number of the component concerned by the information written\r
-*/\r
-static void j3d_write_cox(opj_j3d_t *j3d, int compno);\r
-/**\r
-Read the value concerning the specified component in the marker COD and COC\r
-@param j3d J3D handle\r
-@param compno Number of the component concerned by the information read\r
-*/\r
-static void j3d_read_cox(opj_j3d_t *j3d, int compno);\r
-/**\r
-Write the COD marker (coding style default)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_cod(opj_j3d_t *j3d);\r
-/**\r
-Read the COD marker (coding style default)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_cod(opj_j3d_t *j3d);\r
-/**\r
-Write the COC marker (coding style component)\r
-@param j3d J3D handle\r
-@param compno Number of the component concerned by the information written\r
-*/\r
-static void j3d_write_coc(opj_j3d_t *j3d, int compno);\r
-/**\r
-Read the COC marker (coding style component)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_coc(opj_j3d_t *j3d);\r
-/**\r
-Write the value concerning the specified component in the marker QCD and QCC\r
-@param j3d J3D handle\r
-@param compno Number of the component concerned by the information written\r
-*/\r
-static void j3d_write_qcx(opj_j3d_t *j3d, int compno);\r
-/**\r
-Read the value concerning the specified component in the marker QCD and QCC\r
-@param j3d J3D handle\r
-@param compno Number of the component concern by the information read\r
-@param len Length of the information in the QCX part of the marker QCD/QCC\r
-*/\r
-static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len);\r
-/**\r
-Write the QCD marker (quantization default)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_qcd(opj_j3d_t *j3d);\r
-/**\r
-Read the QCD marker (quantization default)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_qcd(opj_j3d_t *j3d);\r
-/**\r
-Write the QCC marker (quantization component)\r
-@param j3d J3D handle\r
-@param compno Number of the component concerned by the information written\r
-*/\r
-static void j3d_write_qcc(opj_j3d_t *j3d, int compno);\r
-/**\r
-Read the QCC marker (quantization component)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_qcc(opj_j3d_t *j3d);\r
-/**\r
-Write the POC marker (progression order change)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_poc(opj_j3d_t *j3d);\r
-/**\r
-Read the POC marker (progression order change)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_poc(opj_j3d_t *j3d);\r
-/**\r
-Read the CRG marker (component registration)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_crg(opj_j3d_t *j3d);\r
-/**\r
-Read the TLM marker (tile-part lengths)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_tlm(opj_j3d_t *j3d);\r
-/**\r
-Read the PLM marker (packet length, main header)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_plm(opj_j3d_t *j3d);\r
-/**\r
-Read the PLT marker (packet length, tile-part header)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_plt(opj_j3d_t *j3d);\r
-/**\r
-Read the PPM marker (packet packet headers, main header)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_ppm(opj_j3d_t *j3d);\r
-/**\r
-Read the PPT marker (packet packet headers, tile-part header)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_ppt(opj_j3d_t *j3d);\r
-/**\r
-Write the SOT marker (start of tile-part)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_sot(opj_j3d_t *j3d);\r
-/**\r
-Read the SOT marker (start of tile-part)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_sot(opj_j3d_t *j3d);\r
-/**\r
-Write the SOD marker (start of data)\r
-@param j3d J3D handle\r
-@param tile_coder Pointer to a TCD handle\r
-*/\r
-static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder);\r
-/**\r
-Read the SOD marker (start of data)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_sod(opj_j3d_t *j3d);\r
-/**\r
-Write the RGN marker (region-of-interest)\r
-@param j3d J3D handle\r
-@param compno Number of the component concerned by the information written\r
-@param tileno Number of the tile concerned by the information written\r
-*/\r
-static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno);\r
-/**\r
-Read the RGN marker (region-of-interest)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_rgn(opj_j3d_t *j3d);\r
-/**\r
-Write the EOC marker (end of codestream)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_eoc(opj_j3d_t *j3d);\r
-/**\r
-Read the EOC marker (end of codestream)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_eoc(opj_j3d_t *j3d);\r
-/**\r
-Read an unknown marker\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_unk(opj_j3d_t *j3d);\r
-/**\r
-Write the CAP marker (extended capabilities)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_cap(opj_j3d_t *j3d);\r
-/**\r
-Read the CAP marker (extended capabilities)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_cap(opj_j3d_t *j3d);\r
-/**\r
-Write the DCO marker (Variable DC offset)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_dco(opj_j3d_t *j3d);\r
-/**\r
-Read the DCO marker (Variable DC offset)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_dco(opj_j3d_t *j3d);\r
-/**\r
-Write the ATK marker (arbitrary transformation kernel)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_atk(opj_j3d_t *j3d);\r
-/**\r
-Read the ATK marker (arbitrary transformation kernel)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_atk(opj_j3d_t *j3d);\r
-/**\r
-Write the CBD marker (component bit depth definition)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_cbd(opj_j3d_t *j3d);\r
-/**\r
-Read the CBD marker (component bit depth definition)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_cbd(opj_j3d_t *j3d);\r
-/**\r
-Write the MCT marker (multiple component transfomation definition)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_mct(opj_j3d_t *j3d);\r
-/**\r
-Read the MCT marker (multiple component transfomation definition)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_mct(opj_j3d_t *j3d);\r
-/**\r
-Write the MCC marker (multiple component transfomation collection)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_mcc(opj_j3d_t *j3d);\r
-/**\r
-Read the MCC marker (multiple component transfomation collection)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_mcc(opj_j3d_t *j3d);\r
-/**\r
-Write the MCO marker (multiple component transfomation ordering)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_mco(opj_j3d_t *j3d);\r
-/**\r
-Read the MCO marker (multiple component transfomation ordering)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_mco(opj_j3d_t *j3d);\r
-/**\r
-Write the NLT marker (non-linearity point transformation)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_write_nlt(opj_j3d_t *j3d);\r
-/**\r
-Read the NLT marker (non-linearity point transformation)\r
-@param j3d J3D handle\r
-*/\r
-static void j3d_read_nlt(opj_j3d_t *j3d);\r
-/*@}*/\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-static void j3d_dump_volume(FILE *fd, opj_volume_t * vol) {\r
-       int compno;\r
-       fprintf(fd, "volume {\n");\r
-       fprintf(fd, "  x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", vol->x0, vol->y0, vol->z0,vol->x1, vol->y1,  vol->z1);\r
-       fprintf(fd, "  numcomps=%d\n", vol->numcomps);\r
-       for (compno = 0; compno < vol->numcomps; compno++) {\r
-               opj_volume_comp_t *comp = &vol->comps[compno];\r
-               fprintf(fd, "  comp %d {\n", compno);\r
-               fprintf(fd, "    dx=%d, dy=%d, dz=%d\n", comp->dx, comp->dy, comp->dz);\r
-               fprintf(fd, "    prec=%d\n", comp->prec);\r
-               fprintf(fd, "    sgnd=%d\n", comp->sgnd);\r
-               fprintf(fd, "  }\n");\r
-       }\r
-       fprintf(fd, "}\n");\r
-}\r
-\r
-static void j3d_dump_cp(FILE *fd, opj_volume_t * vol, opj_cp_t * cp) {\r
-       int tileno, compno, layno, bandno, resno, numbands;\r
-       fprintf(fd, "coding parameters {\n");\r
-       fprintf(fd, "  tx0=%d, ty0=%d, tz0=%d\n", cp->tx0, cp->ty0, cp->tz0);\r
-       fprintf(fd, "  tdx=%d, tdy=%d, tdz=%d\n", cp->tdx, cp->tdy, cp->tdz);\r
-       fprintf(fd, "  tw=%d, th=%d, tl=%d\n", cp->tw, cp->th, cp->tl);\r
-       fprintf(fd, "  transform format: %d\n", cp->transform_format);\r
-       fprintf(fd, "  encoding format: %d\n", cp->encoding_format);\r
-       for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {\r
-               opj_tcp_t *tcp = &cp->tcps[tileno];\r
-               fprintf(fd, "  tile %d {\n", tileno);\r
-               fprintf(fd, "    csty=%x\n", tcp->csty);\r
-               fprintf(fd, "    prg=%d\n", tcp->prg);\r
-               fprintf(fd, "    numlayers=%d\n", tcp->numlayers);\r
-               fprintf(fd, "    mct=%d\n", tcp->mct);\r
-               fprintf(fd, "    rates=");\r
-               for (layno = 0; layno < tcp->numlayers; layno++) {\r
-                       fprintf(fd, "%f ", tcp->rates[layno]);\r
-               }\r
-               fprintf(fd, "\n");\r
-               fprintf(fd, "    first=%d\n", tcp->first);\r
-               for (compno = 0; compno < vol->numcomps; compno++) {\r
-                       opj_tccp_t *tccp = &tcp->tccps[compno];\r
-                       fprintf(fd, "    comp %d {\n", compno);\r
-                       fprintf(fd, "      csty=%x\n", tccp->csty);\r
-                       fprintf(fd, "      numresx=%d, numresy=%d, numresz=%d\n", tccp->numresolution[0], tccp->numresolution[1], tccp->numresolution[2]);\r
-                       fprintf(fd, "      cblkw=%d, cblkh=%d, cblkl=%d\n", tccp->cblk[0], tccp->cblk[1], tccp->cblk[2]);\r
-                       fprintf(fd, "      cblksty=%x\n", tccp->cblksty);\r
-                       fprintf(fd, "      qntsty=%d\n", tccp->qntsty);\r
-                       fprintf(fd, "      numgbits=%d\n", tccp->numgbits);\r
-                       fprintf(fd, "      roishift=%d\n", tccp->roishift);\r
-                       fprintf(fd, "      reversible=%d\n", tccp->reversible);\r
-                       fprintf(fd, "      dwtidx=%d dwtidy=%d dwtidz=%d\n", tccp->dwtid[0], tccp->dwtid[1], tccp->dwtid[2]);\r
-                       if (tccp->atk != NULL) {\r
-                fprintf(fd, "      atk.index=%d\n", tccp->atk->index);\r
-                               fprintf(fd, "      atk.coeff_typ=%d\n", tccp->atk->coeff_typ);\r
-                               fprintf(fd, "      atk.filt_cat=%d\n", tccp->atk->filt_cat);\r
-                               fprintf(fd, "      atk.exten=%d\n", tccp->atk->exten);\r
-                               fprintf(fd, "      atk.minit=%d\n", tccp->atk->minit);\r
-                               fprintf(fd, "      atk.wt_typ=%d\n", tccp->atk->wt_typ);\r
-                       }\r
-                       fprintf(fd, "      stepsizes of bands=");\r
-            numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 :\r
-                       ( (cp->transform_format == TRF_2D_DWT) ? (tccp->numresolution[0] * 3 - 2) :\r
-                               (tccp->numresolution[0] * 7 - 6) - 4 *(tccp->numresolution[0] - tccp->numresolution[2]) );\r
-                       for (bandno = 0; bandno < numbands; bandno++) {\r
-                               fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,tccp->stepsizes[bandno].expn);\r
-                       }\r
-                       fprintf(fd, "\n");\r
-                       \r
-                       if (tccp->csty & J3D_CCP_CSTY_PRT) {\r
-                               fprintf(fd, "      prcw=");\r
-                               for (resno = 0; resno < tccp->numresolution[0]; resno++) {\r
-                                       fprintf(fd, "%d ", tccp->prctsiz[0][resno]);\r
-                               }\r
-                               fprintf(fd, "\n");\r
-                               fprintf(fd, "      prch=");\r
-                               for (resno = 0; resno < tccp->numresolution[0]; resno++) {\r
-                                       fprintf(fd, "%d ", tccp->prctsiz[1][resno]);\r
-                               }\r
-                               fprintf(fd, "\n");\r
-                               fprintf(fd, "      prcl=");\r
-                               for (resno = 0; resno < tccp->numresolution[0]; resno++) {\r
-                                       fprintf(fd, "%d ", tccp->prctsiz[2][resno]);\r
-                               }\r
-                               fprintf(fd, "\n");\r
-                       }\r
-                       fprintf(fd, "    }\n");\r
-               }\r
-               fprintf(fd, "  }\n");\r
-       }\r
-       fprintf(fd, "}\n");\r
-}\r
-\r
-/* ----------------------------------------------------------------------- \r
-Extended capabilities\r
-------------------------------------------------------------------------*/\r
-\r
-static void j3d_write_cap(opj_j3d_t *j3d){\r
-       int len,lenp;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       cio_write(cio, J3D_MS_CAP, 2);  /* CAP */\r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);\r
-       cio_write(cio,J3D_CAP_10, 4); \r
-  if( J3D_CAP_10 )\r
-    {\r
-    cio_write(cio, 0x0, 2); \r
-    }\r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);         /* Lsiz */\r
-       cio_seek(cio, lenp + len);\r
-\r
-}\r
-static void j3d_read_cap(opj_j3d_t *j3d){\r
-       int len, Cap;\r
-       opj_cio_t *cio = j3d->cio;\r
-       /*cio_read(cio, 2);      CAP */\r
-       len = cio_read(cio, 2);\r
-       Cap = cio_read(cio, 4);\r
-  if(Cap) {\r
-    cio_read(cio, 2);\r
-  }\r
-  assert( len == 2 + 4 + 2 );\r
-}\r
-static void j3d_write_nsi(opj_j3d_t *j3d) {\r
-       int i;\r
-       int lenp, len;\r
-  int ndim = 3;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       opj_volume_t *volume = j3d->volume;\r
-       opj_cp_t *cp = j3d->cp;\r
-       \r
-       cio_write(cio, J3D_MS_NSI, 2);  /* NSI */\r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);\r
-       cio_write(cio, ndim, 1);        /* Ndim */\r
-       cio_write(cio, volume->z1, 4);  /* Zsiz */\r
-       cio_write(cio, volume->z0, 4);  /* Z0siz */\r
-       cio_write(cio, cp->tdz, 4);             /* ZTsiz */\r
-       cio_write(cio, cp->tz0, 4);             /* ZT0siz */\r
-       for (i = 0; i < volume->numcomps; i++) {\r
-               cio_write(cio, volume->comps[i].dz, 1); /* ZRsiz_i */\r
-       }\r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);         /* Lsiz */\r
-       cio_seek(cio, lenp + len);\r
-}\r
-\r
-static void j3d_read_nsi(opj_j3d_t *j3d) {\r
-  int ndim;\r
-       int len, i;\r
-       \r
-       opj_cio_t *cio = j3d->cio;\r
-       opj_volume_t *volume = j3d->volume;\r
-       opj_cp_t *cp = j3d->cp;\r
-       \r
-       len = cio_read(cio, 2);                 /* Lnsi */\r
-       ndim = cio_read(cio, 1);                        /* Ndim */\r
-  assert( ndim == 3 );\r
-       volume->z1 = cio_read(cio, 4);  /* Zsiz */\r
-       volume->z0 = cio_read(cio, 4);  /* Z0siz */\r
-       cp->tdz = cio_read(cio, 4);             /* ZTsiz */\r
-       cp->tz0 = cio_read(cio, 4);             /* ZT0siz */\r
-       for (i = 0; i < volume->numcomps; i++) {\r
-               volume->comps[i].dz = cio_read(cio, 1); /* ZRsiz_i */\r
-       }\r
-       \r
-       /*Initialization of volume*/\r
-       cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);\r
-       cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);\r
-       cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);\r
-       cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));\r
-       cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));\r
-       cp->tileno_size = 0;\r
-       \r
-       for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {\r
-               cp->tcps[i].POC = 0;\r
-               cp->tcps[i].numpocs = 0;\r
-               cp->tcps[i].first = 1;\r
-       }\r
-       \r
-       /* Initialization for PPM marker (Packets header)*/\r
-       cp->ppm = 0;\r
-       cp->ppm_data = NULL;\r
-       cp->ppm_data_first = NULL;\r
-       cp->ppm_previous = 0;\r
-       cp->ppm_store = 0;\r
-       \r
-       j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);\r
-       for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {\r
-               cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);\r
-       }\r
-       j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));\r
-       j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));\r
-       j3d->state = J3D_STATE_MH;\r
-       \r
-}\r
-static void j3d_write_dco(opj_j3d_t *j3d){\r
-       int lenp, len, i;\r
-       int dcotype;    \r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       opj_volume_t *volume = j3d->volume;\r
-       opj_cp_t *cp = j3d->cp;\r
-       \r
-       dcotype = 1; /* Offsets are 16bit signed integers Table A21 15444-2 */\r
-       cio_write(cio, J3D_MS_DCO, 2);  /* DCO */\r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);\r
-       cio_write(cio, dcotype, 1);     \r
-       if (dcotype == 0) {\r
-               for (i = 0; i < volume->numcomps; i++) \r
-                       cio_write(cio, volume->comps[i].dcoffset, 1);   /* SPdco_i */\r
-       } else if (dcotype == 1) {\r
-               for (i = 0; i < volume->numcomps; i++){ \r
-                       cio_write(cio, volume->comps[i].dcoffset, 1);   /* SPdco_i */\r
-                       opj_event_msg(j3d->cinfo, EVT_INFO, "dcotype %d DCO %d \n",dcotype,volume->comps[i].dcoffset);\r
-               }\r
-       }\r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);         /* Ldco */\r
-       cio_seek(cio, lenp + len);\r
-\r
-}\r
-static void j3d_read_dco(opj_j3d_t *j3d){\r
-       int len, i;\r
-       int dcotype;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       opj_volume_t *volume = j3d->volume;\r
-       opj_cp_t *cp = j3d->cp;\r
-       \r
-       len = cio_read(cio, 2);                 /* Lsiz */\r
-       dcotype = cio_read(cio, 1); /*offset 8bit unsigned / 16bit signed integers*/\r
-       if (dcotype == 0) {\r
-               for (i = 0; i < volume->numcomps; i++) {\r
-                       volume->comps[i].dcoffset = cio_read(cio, 1);\r
-                       if (volume->comps[i].dcoffset > 128) \r
-                               volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;\r
-               }\r
-       } else if (dcotype == 1) {\r
-               for (i = 0; i < volume->numcomps; i++) {\r
-                       volume->comps[i].dcoffset = cio_read(cio, 1);\r
-                       if (volume->comps[i].dcoffset > 128) \r
-                               volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;\r
-               }\r
-       }\r
-       \r
-}\r
-static void j3d_write_atk(opj_j3d_t *j3d){\r
-       int lenp, len, s, k;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       opj_volume_t *volume = j3d->volume;\r
-       opj_atk_t *atk = j3d->cp->tcps->tccps->atk;\r
-       \r
-       cio_write(cio, J3D_MS_ATK, 2);  /* ATK */\r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);                               \r
-       cio_write(cio, atk->index + (atk->coeff_typ << 8) + (atk->filt_cat << 11) \r
-               + (atk->wt_typ << 12) + (atk->minit << 13) + (atk->exten << 14), 2);                    /* Satk */\r
-    if (atk->wt_typ == J3D_ATK_IRR) \r
-               cio_write(cio,(unsigned int) (atk->Katk * 8192.0), 1 << atk->coeff_typ);\r
-       cio_write(cio, atk->Natk, 1);\r
-       for (s = 0; s < atk->Natk; s++){\r
-               if (atk->filt_cat == J3D_ATK_ARB) \r
-                       cio_write(cio, atk->Oatk[s], 1);\r
-               if (atk->wt_typ == J3D_ATK_REV){\r
-                       cio_write(cio, atk->Eatk[s], 1);\r
-                       cio_write(cio, atk->Batk[s], 1);\r
-               }\r
-               cio_write(cio, atk->LCatk[s], 1);\r
-               for (k = 0; k < atk->LCatk[s]; k++)\r
-                       cio_write(cio,(unsigned int) (atk->Aatk[s][k] * 8192.0), 1 << atk->coeff_typ);\r
-       }\r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);         /* Latk */\r
-       cio_seek(cio, lenp + len);\r
-}\r
-static void j3d_read_atk(opj_j3d_t *j3d){\r
-       int len, i, Satk, k;\r
-       \r
-       opj_cio_t *cio = j3d->cio;\r
-       opj_volume_t *volume = j3d->volume;\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_atk_t *atk = cp->tcps->tccps->atk; \r
-       \r
-       len = cio_read(cio, 2);                 /* Latk */\r
-       Satk = cio_read(cio, 2); \r
-       atk->index = Satk & 0x00ff;\r
-       atk->coeff_typ = Satk >> 8 & 0x0007;\r
-       atk->filt_cat = Satk >> 11 & 0x0001;\r
-       atk->wt_typ = Satk >> 12  & 0x0001;\r
-       atk->minit = Satk >> 13 & 0x0001;\r
-       atk->exten = Satk >> 14 & 0x0001;\r
-    if (atk->wt_typ == J3D_ATK_IRR) \r
-               atk->Katk = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);\r
-       atk->Natk = cio_read(cio, 1);\r
-       for (i = 0; i < atk->Natk; i++) {\r
-               if (atk->filt_cat == J3D_ATK_ARB) \r
-                       atk->Oatk[i] = cio_read(cio, 1);\r
-               if (atk->wt_typ == J3D_ATK_REV){\r
-                       atk->Eatk[i] = cio_read(cio, 1);\r
-                       atk->Batk[i] = cio_read(cio, 1);\r
-               }\r
-               atk->LCatk[i] = cio_read(cio, 1);\r
-               for (k = 0; k < atk->LCatk[i]; k++)\r
-                       atk->Aatk[i][k] = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);\r
-       }\r
-}\r
-static void j3d_write_cbd(opj_j3d_t *j3d){\r
-}\r
-static void j3d_read_cbd(opj_j3d_t *j3d){\r
-}\r
-static void j3d_write_mct(opj_j3d_t *j3d){\r
-}\r
-static void j3d_read_mct(opj_j3d_t *j3d){\r
-}\r
-static void j3d_write_mcc(opj_j3d_t *j3d){\r
-}\r
-static void j3d_read_mcc(opj_j3d_t *j3d){\r
-}\r
-static void j3d_write_mco(opj_j3d_t *j3d){\r
-}\r
-static void j3d_read_mco(opj_j3d_t *j3d){\r
-}\r
-static void j3d_write_nlt(opj_j3d_t *j3d){\r
-}\r
-static void j3d_read_nlt(opj_j3d_t *j3d){\r
-}\r
-/* ----------------------------------------------------------------------- \r
-15444-1 codestream syntax\r
-------------------------------------------------------------------------*/\r
-static void j3d_write_soc(opj_j3d_t *j3d) {\r
-       opj_cio_t *cio = j3d->cio;\r
-       cio_write(cio, J3D_MS_SOC, 2);\r
-}\r
-\r
-static void j3d_read_soc(opj_j3d_t *j3d) {\r
-       j3d->state = J3D_STATE_MHSIZ;\r
-}\r
-\r
-static void j3d_write_siz(opj_j3d_t *j3d) {\r
-       int i;\r
-       int lenp, len;\r
-       int Rsiz;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       opj_volume_t *volume = j3d->volume;\r
-       opj_cp_t *cp = j3d->cp;\r
-       \r
-       cio_write(cio, J3D_MS_SIZ, 2);  /* SIZ */\r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);\r
-       /*cio_write(cio, 0, 2);*/                       /* Rsiz (capabilities of 15444-1 only) */\r
-       Rsiz = J3D_RSIZ_DCO | J3D_RSIZ_ATK; /** | J3D_RSIZ_MCT | J3D_RSIZ_NONLT (not implemented yet)*/\r
-       cio_write(cio, Rsiz, 2); /* capabilities of WDv5.2*/\r
-       cio_write(cio, volume->x1, 4);  /* Xsiz */\r
-       cio_write(cio, volume->y1, 4);  /* Ysiz */\r
-       cio_write(cio, volume->x0, 4);  /* X0siz */\r
-       cio_write(cio, volume->y0, 4);  /* Y0siz */\r
-       cio_write(cio, cp->tdx, 4);             /* XTsiz */\r
-       cio_write(cio, cp->tdy, 4);             /* YTsiz */\r
-       cio_write(cio, cp->tx0, 4);             /* XT0siz */\r
-       cio_write(cio, cp->ty0, 4);             /* YT0siz */\r
-       cio_write(cio, volume->numcomps, 2);    /* Csiz */\r
-       for (i = 0; i < volume->numcomps; i++) {\r
-               cio_write(cio, volume->comps[i].prec - 1 + (volume->comps[i].sgnd << 7), 1);    /* Ssiz_i */\r
-               cio_write(cio, volume->comps[i].dx, 1); /* XRsiz_i */\r
-               cio_write(cio, volume->comps[i].dy, 1); /* YRsiz_i */\r
-       }\r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);         /* Lsiz */\r
-       cio_seek(cio, lenp + len);\r
-}\r
-\r
-static void j3d_read_siz(opj_j3d_t *j3d) {\r
-       int len, i;\r
-       \r
-       opj_cio_t *cio = j3d->cio;\r
-       opj_volume_t *volume = j3d->volume;\r
-       opj_cp_t *cp = j3d->cp;\r
-       \r
-       len = cio_read(cio, 2);                 /* Lsiz */\r
-       cp->rsiz = cio_read(cio, 2);    /* Rsiz (capabilities) */\r
-       volume->x1 = cio_read(cio, 4);  /* Xsiz */\r
-       volume->y1 = cio_read(cio, 4);  /* Ysiz */\r
-       volume->x0 = cio_read(cio, 4);  /* X0siz */\r
-       volume->y0 = cio_read(cio, 4);  /* Y0siz */\r
-       cp->tdx = cio_read(cio, 4);             /* XTsiz */\r
-       cp->tdy = cio_read(cio, 4);             /* YTsiz */\r
-       cp->tx0 = cio_read(cio, 4);             /* XT0siz */\r
-       cp->ty0 = cio_read(cio, 4);             /* YT0siz */\r
-       \r
-       volume->numcomps = cio_read(cio, 2);    /* Csiz */\r
-       volume->comps = (opj_volume_comp_t *) opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t));\r
-       for (i = 0; i < volume->numcomps; i++) {\r
-               int tmp, j;\r
-               tmp = cio_read(cio, 1);         /* Ssiz_i */\r
-               volume->comps[i].prec = (tmp & 0x7f) + 1;\r
-               volume->comps[i].sgnd = tmp >> 7;\r
-               volume->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */\r
-               volume->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */\r
-               for (j = 0; j < 3; j++) {\r
-                       volume->comps[i].resno_decoded[j] = 0;          /* number of resolution decoded */\r
-                       volume->comps[i].factor[j] = 0;         /* reducing factor per component */\r
-               }\r
-       }\r
-\r
-       if (j3d->cinfo->codec_format == CODEC_J2K){\r
-               volume->z1 = 1;\r
-               volume->z0 = 0;\r
-               volume->numslices = 1;\r
-               cp->tdz = 1;\r
-               cp->tz0 = 0;\r
-               for (i = 0; i < volume->numcomps; i++) \r
-                       volume->comps[i].dz = 1;\r
-\r
-               /*Initialization of volume*/\r
-               cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);\r
-               cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);\r
-               cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);\r
-               cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));\r
-               cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));\r
-               cp->tileno_size = 0;\r
-               \r
-               for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {\r
-                       cp->tcps[i].POC = 0;\r
-                       cp->tcps[i].numpocs = 0;\r
-                       cp->tcps[i].first = 1;\r
-               }\r
-               \r
-               /* Initialization for PPM marker (Packets header)*/\r
-               cp->ppm = 0;\r
-               cp->ppm_data = NULL;\r
-               cp->ppm_data_first = NULL;\r
-               cp->ppm_previous = 0;\r
-               cp->ppm_store = 0;\r
-               \r
-               j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);\r
-               for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {\r
-                       cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);\r
-               }\r
-               j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));\r
-               j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));\r
-               j3d->state = J3D_STATE_MH;\r
-       }\r
-}\r
-\r
-\r
-\r
-static void j3d_write_com(opj_j3d_t *j3d) {\r
-       unsigned int i;\r
-       int lenp, len;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-\r
-       cio_write(cio, J3D_MS_COM, 2);\r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);\r
-       cio_write(cio, 1, 2);\r
-       /*opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);*/\r
-       if (j3d->cp->comment != NULL) {\r
-               char *comment = j3d->cp->comment;\r
-               for (i = 0; i < strlen(comment); i++) {\r
-            cio_write(cio, comment[i], 1);\r
-               }\r
-       }\r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);\r
-       cio_seek(cio, lenp + len);\r
-}\r
-\r
-static void j3d_read_com(opj_j3d_t *j3d) {\r
-       int len;\r
-       opj_cio_t *cio = j3d->cio;\r
-\r
-       len = cio_read(cio, 2);\r
-       cio_read(cio, 2); // read registration\r
-       \r
-       /*opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);*/\r
-\r
-       cio_skip(cio, len - 4);  /*posible comments*/\r
-}\r
-\r
-static void j3d_write_cox(opj_j3d_t *j3d, int compno) {\r
-       int i;\r
-  int shift = 2;\r
-\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];\r
-       opj_tccp_t *tccp = &tcp->tccps[compno];\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       cio_write(cio, tccp->numresolution[0] - 1, 1);  /* SPcox (D) No of decomposition levels in x-axis*/\r
-       if (j3d->cinfo->codec_format == CODEC_J3D) {\r
-               cio_write(cio, tccp->numresolution[1] - 1, 1);  /* SPcox (E) No of decomposition levels in y-axis*/\r
-               cio_write(cio, tccp->numresolution[2] - 1, 1);  /* SPcox (F) No of decomposition levels in z-axis*/\r
-       }\r
-       if (j3d->cinfo->codec_format == CODEC_J3D) {\r
-    /* Table A.7 */\r
-    shift = 0;\r
-  }\r
-       /* (cblkw - 2) + (cblkh - 2) + (cblkl - 2) <= 18*/\r
-       cio_write(cio, tccp->cblk[0] - shift, 1);                               /* SPcox (G) Cblk width entre 10 y 2 (8 y 0)*/\r
-       cio_write(cio, tccp->cblk[1] - shift, 1);                               /* SPcox (H) Cblk height*/\r
-       if (j3d->cinfo->codec_format == CODEC_J3D) {\r
-               cio_write(cio, tccp->cblk[2] - shift, 1);                       /* SPcox (I) Cblk depth*/\r
-       }\r
-       cio_write(cio, tccp->cblksty, 1);                               /* SPcox (J) Cblk style*/\r
-       cio_write(cio, tccp->dwtid[0], 1);                              /* SPcox (K) WT in x-axis 15444-2 Table A10*/\r
-       if (j3d->cinfo->codec_format == CODEC_J3D) {\r
-               cio_write(cio, tccp->dwtid[1], 1);                              /* SPcox (L) WT in y-axis 15444-2 Table A10*/\r
-               cio_write(cio, tccp->dwtid[2], 1);                              /* SPcox (M) WT in z-axis 15444-2 Table A10*/\r
-       }\r
-       \r
-       if (tccp->csty & J3D_CCP_CSTY_PRT) {\r
-               for (i = 0; i < tccp->numresolution[0]; i++) {\r
-                       if (i < tccp->numresolution[2])\r
-                cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4) + (tccp->prctsiz[2][i] << 8), 2);      /* SPcox (N_i) Table A9*/\r
-                       else\r
-                               if (j3d->cinfo->codec_format == CODEC_J3D) \r
-                    cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 2);       /* SPcox (N_i) Table A9*/\r
-                               else\r
-                    cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 1);       /* SPcox (N_i) Table A9*/               }\r
-       }\r
-}\r
-\r
-static void j3d_read_cox(opj_j3d_t *j3d, int compno) {\r
-       int i;\r
-  int shift = 2;\r
-\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;\r
-       opj_tccp_t *tccp = &tcp->tccps[compno];\r
-       opj_cio_t *cio = j3d->cio;\r
-\r
-       tccp->numresolution[0] = cio_read(cio, 1) + 1;  /* SPcox (D) No of decomposition levels in x-axis*/\r
-       if (j3d->cinfo->codec_format == CODEC_J3D) {\r
-               tccp->numresolution[1] = cio_read(cio, 1) + 1;  /* SPcox (E) No of decomposition levels in y-axis*/\r
-               tccp->numresolution[2] = cio_read(cio, 1) + 1;  /* SPcox (F) No of decomposition levels in z-axis*/\r
-       }else if (j3d->cinfo->codec_format == CODEC_J2K) {\r
-               tccp->numresolution[1] = tccp->numresolution[0];        \r
-               tccp->numresolution[2] = 1;                                                     \r
-       }\r
-       /* check the reduce value */\r
-       cp->reduce[0] = int_min((tccp->numresolution[0])-1, cp->reduce[0]);\r
-       cp->reduce[1] = int_min((tccp->numresolution[1])-1, cp->reduce[1]);\r
-       cp->reduce[2] = int_min((tccp->numresolution[2])-1, cp->reduce[2]);\r
-       \r
-  if (j3d->cinfo->codec_format == CODEC_J3D) {\r
-    /* Table A.7 */\r
-    shift = 0;\r
-  }\r
-       tccp->cblk[0] = cio_read(cio, 1) + shift;       /* SPcox (G) */\r
-       tccp->cblk[1] = cio_read(cio, 1) + shift;       /* SPcox (H) */\r
-       if (j3d->cinfo->codec_format == CODEC_J3D)\r
-               tccp->cblk[2] = cio_read(cio, 1) + shift;       /* SPcox (I) */\r
-       else\r
-               tccp->cblk[2] = tccp->cblk[0];\r
-\r
-       tccp->cblksty = cio_read(cio, 1);       /* SPcox (J) */\r
-       tccp->dwtid[0] = cio_read(cio, 1);      /* SPcox (K) */\r
-       if (j3d->cinfo->codec_format == CODEC_J3D) {\r
-               tccp->dwtid[1] = cio_read(cio, 1);      /* SPcox (L) */\r
-               tccp->dwtid[2] = cio_read(cio, 1);      /* SPcox (M) */\r
-       }else{\r
-               tccp->dwtid[1] = tccp->dwtid[0];        /* SPcox (L) */\r
-               tccp->dwtid[2] = tccp->dwtid[0];        /* SPcox (M) */\r
-       }\r
-       tccp->reversible = (tccp->dwtid[0]>=1 && tccp->dwtid[1]>=1 && tccp->dwtid[2]>=1); /*TODO: only valid for irreversible 9x7 WTs*/\r
-       if (tccp->csty & J3D_CP_CSTY_PRT) {\r
-               for (i = 0; i < tccp->numresolution[0]; i++) {\r
-                       int tmp = cio_read(cio, 2);     /* SPcox (N_i) */\r
-                       tccp->prctsiz[0][i] = tmp & 0xf;\r
-                       tccp->prctsiz[1][i] = tmp >> 4;\r
-                       tccp->prctsiz[2][i] = tmp >> 8;\r
-               }\r
-       }\r
-}\r
-\r
-static void j3d_write_cod(opj_j3d_t *j3d) {\r
-       opj_cp_t *cp = NULL;\r
-       opj_tcp_t *tcp = NULL;\r
-       int lenp, len;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       cio_write(cio, J3D_MS_COD, 2);  /* COD */\r
-       \r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);\r
-       \r
-       cp = j3d->cp;\r
-       tcp = &cp->tcps[j3d->curtileno];\r
-\r
-       /* Scod : Table A-4*/\r
-       cio_write(cio, tcp->csty, 1);           /* Scod : Coding style parameters */\r
-       /* SGcod : Table A-5*/\r
-       cio_write(cio, tcp->prg, 1);            /* SGcod (A) : Progression order */\r
-       cio_write(cio, tcp->numlayers, 2);      /* SGcod (B) : No of layers */\r
-       cio_write(cio, tcp->mct, 1);            /* SGcod (C) : Multiple component transformation usage */\r
-       /* SPcod : Table A-6*/\r
-       j3d_write_cox(j3d, 0);                          \r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);         /* Lcod */\r
-       cio_seek(cio, lenp + len);\r
-}\r
-\r
-static void j3d_read_cod(opj_j3d_t *j3d) {\r
-       int len, i, pos;\r
-       \r
-       opj_cio_t *cio = j3d->cio;\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;\r
-       opj_volume_t *volume = j3d->volume;\r
-\r
-       /* Lcod */\r
-       len = cio_read(cio, 2);                         \r
-       /* Scod : Table A-4*/\r
-       tcp->csty = cio_read(cio, 1);           \r
-       /* SGcod : Table A-5*/\r
-       tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);\r
-       tcp->numlayers = cio_read(cio, 2);      \r
-       tcp->mct = cio_read(cio, 1);            \r
-       \r
-       pos = cio_tell(cio);\r
-       for (i = 0; i < volume->numcomps; i++) {\r
-               tcp->tccps[i].csty = tcp->csty & J3D_CP_CSTY_PRT;\r
-               cio_seek(cio, pos);\r
-               j3d_read_cox(j3d, i);\r
-       }\r
-}\r
-\r
-static void j3d_write_coc(opj_j3d_t *j3d, int compno) {\r
-       int lenp, len;\r
-\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];\r
-       opj_volume_t *volume = j3d->volume;\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       cio_write(cio, J3D_MS_COC, 2);  /* COC */\r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);\r
-       cio_write(cio, compno, volume->numcomps <= 256 ? 1 : 2);        /* Ccoc */\r
-       cio_write(cio, tcp->tccps[compno].csty, 1);                                     /* Scoc */\r
-       \r
-       j3d_write_cox(j3d, compno);\r
-       \r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);                 /* Lcoc */\r
-       cio_seek(cio, lenp + len);\r
-}\r
-\r
-static void j3d_read_coc(opj_j3d_t *j3d) {\r
-       int len, compno;\r
-\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;\r
-       opj_volume_t *volume = j3d->volume;\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       len = cio_read(cio, 2);         /* Lcoc */\r
-       compno = cio_read(cio, volume->numcomps <= 256 ? 1 : 2);        /* Ccoc */\r
-       tcp->tccps[compno].csty = cio_read(cio, 1);     /* Scoc */\r
-       j3d_read_cox(j3d, compno);\r
-}\r
-\r
-static void j3d_write_qcx(opj_j3d_t *j3d, int compno) {\r
-       int bandno, numbands;\r
-       int expn, mant;\r
-       \r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];\r
-       opj_tccp_t *tccp = &tcp->tccps[compno];\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);        /* Sqcx : Table A28 de 15444-1*/\r
-       \r
-       if (j3d->cinfo->codec_format == CODEC_J2K)\r
-        numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolution[0] * 3 - 2; \r
-       else if (j3d->cinfo->codec_format == CODEC_J3D) {\r
-               int diff = tccp->numresolution[0] - tccp->numresolution[2];\r
-        numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : (tccp->numresolution[0] * 7 - 6) - 4 *diff; /* SIQNT vs. SEQNT */\r
-       }\r
-       \r
-       for (bandno = 0; bandno < numbands; bandno++) {\r
-               expn = tccp->stepsizes[bandno].expn;\r
-               mant = tccp->stepsizes[bandno].mant;\r
-               \r
-               if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {\r
-                       cio_write(cio, expn << 3, 1);   /* SPqcx_i */\r
-               } else {\r
-                       cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */\r
-               }\r
-       }\r
-}\r
-\r
-static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len) {\r
-       int tmp;\r
-       int bandno, numbands;\r
-\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;\r
-       opj_tccp_t *tccp = &tcp->tccps[compno];\r
-       opj_cio_t *cio = j3d->cio;\r
-\r
-       tmp = cio_read(cio, 1);         /* Sqcx */\r
-       tccp->qntsty = tmp & 0x1f;\r
-       tccp->numgbits = tmp >> 5;\r
-\r
-       /*Numbands = 1                          si SIQNT\r
-                            len - 1            si NOQNT\r
-                                (len - 1) / 2  si SEQNT */\r
-       numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : ((tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);\r
-\r
-       for (bandno = 0; bandno < numbands; bandno++) {\r
-               int expn, mant;\r
-               if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {\r
-                       expn = cio_read(cio, 1) >> 3;   /* SPqcx_i */\r
-                       mant = 0;\r
-               } else {\r
-                       tmp = cio_read(cio, 2);                 /* SPqcx_i */\r
-                       expn = tmp >> 11;\r
-                       mant = tmp & 0x7ff;\r
-               }\r
-               tccp->stepsizes[bandno].expn = expn;\r
-               tccp->stepsizes[bandno].mant = mant;\r
-       }\r
-       \r
-       /* Add Antonin : if scalar_derived -> compute other stepsizes */\r
-       if (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) {\r
-               for (bandno = 1; bandno < J3D_MAXBANDS; bandno++) {\r
-                       int numbands = (cp->transform_format==TRF_2D_DWT) ? 3 : 7;\r
-                       tccp->stepsizes[bandno].expn = tccp->stepsizes[0].expn - ((bandno - 1) / numbands);\r
-                       tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;\r
-               }\r
-       }\r
-       /* ddA */\r
-}\r
-\r
-static void j3d_write_qcd(opj_j3d_t *j3d) {\r
-       int lenp, len;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       cio_write(cio, J3D_MS_QCD, 2);  /* QCD */\r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);\r
-       j3d_write_qcx(j3d, 0);                  /* Sqcd*/\r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);                 /* Lqcd */\r
-       cio_seek(cio, lenp + len);\r
-}\r
-\r
-static void j3d_read_qcd(opj_j3d_t *j3d) {\r
-       int len, i, pos;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       opj_volume_t *volume = j3d->volume;\r
-       \r
-       len = cio_read(cio, 2);         /* Lqcd */\r
-       pos = cio_tell(cio);\r
-       for (i = 0; i < volume->numcomps; i++) {\r
-               cio_seek(cio, pos);\r
-               j3d_read_qcx(j3d, i, len - 2);\r
-       }\r
-}\r
-\r
-static void j3d_write_qcc(opj_j3d_t *j3d, int compno) {\r
-       int lenp, len;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       cio_write(cio, J3D_MS_QCC, 2);  /* QCC */\r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);\r
-       cio_write(cio, compno, j3d->volume->numcomps <= 256 ? 1 : 2);   /* Cqcc */\r
-       j3d_write_qcx(j3d, compno);\r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);                 /* Lqcc */\r
-       cio_seek(cio, lenp + len);\r
-}\r
-\r
-static void j3d_read_qcc(opj_j3d_t *j3d) {\r
-       int len, compno;\r
-       int numcomp = j3d->volume->numcomps;\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       len = cio_read(cio, 2); /* Lqcc */\r
-       compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */\r
-       j3d_read_qcx(j3d, compno, len - 2 - (numcomp <= 256 ? 1 : 2));\r
-}\r
-\r
-static void j3d_write_poc(opj_j3d_t *j3d) {\r
-       int len, numpchgs, i;\r
-\r
-       int numcomps = j3d->volume->numcomps;\r
-       \r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];\r
-       opj_tccp_t *tccp = &tcp->tccps[0];\r
-       opj_cio_t *cio = j3d->cio;\r
-\r
-       numpchgs = tcp->numpocs;\r
-       cio_write(cio, J3D_MS_POC, 2);  /* POC  */\r
-       len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;\r
-       cio_write(cio, len, 2);         /* Lpoc */\r
-       for (i = 0; i < numpchgs; i++) {\r
-               opj_poc_t *poc = &tcp->pocs[i];\r
-               cio_write(cio, poc->resno0, 1); /* RSpoc_i */\r
-               cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));        /* CSpoc_i */\r
-               cio_write(cio, poc->layno1, 2); /* LYEpoc_i */\r
-               poc->layno1 = int_min(poc->layno1, tcp->numlayers);\r
-               cio_write(cio, poc->resno1, 1); /* REpoc_i */\r
-               poc->resno1 = int_min(poc->resno1, tccp->numresolution[0]);\r
-               cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));        /* CEpoc_i */\r
-               poc->compno1 = int_min(poc->compno1, numcomps);\r
-               cio_write(cio, poc->prg, 1);    /* Ppoc_i */\r
-       }\r
-}\r
-\r
-static void j3d_read_poc(opj_j3d_t *j3d) {\r
-       int len, numpchgs, i, old_poc;\r
-\r
-       int numcomps = j3d->volume->numcomps;\r
-       \r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;\r
-       opj_tccp_t *tccp = &tcp->tccps[0];\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       old_poc = tcp->POC ? tcp->numpocs + 1 : 0;\r
-       tcp->POC = 1;\r
-       len = cio_read(cio, 2);         /* Lpoc */\r
-       numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));\r
-       \r
-       for (i = old_poc; i < numpchgs + old_poc; i++) {\r
-               opj_poc_t *poc;\r
-               poc = &tcp->pocs[i];\r
-               poc->resno0 = cio_read(cio, 1); /* RSpoc_i */\r
-               poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */\r
-               poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */\r
-               poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolution[0]); /* REpoc_i */\r
-               poc->compno1 = int_min(\r
-                       cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */\r
-               poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */\r
-       }\r
-       \r
-       tcp->numpocs = numpchgs + old_poc - 1;\r
-}\r
-\r
-static void j3d_read_crg(opj_j3d_t *j3d) {\r
-       int len, i, Xcrg_i, Ycrg_i, Zcrg_i;\r
-       \r
-       opj_cio_t *cio = j3d->cio;\r
-       int numcomps = j3d->volume->numcomps;\r
-       \r
-       len = cio_read(cio, 2);                 /* Lcrg */\r
-       for (i = 0; i < numcomps; i++) {\r
-               Xcrg_i = cio_read(cio, 2);      /* Xcrg_i */\r
-               Ycrg_i = cio_read(cio, 2);      /* Ycrg_i */\r
-               Zcrg_i = cio_read(cio, 2);      /* Zcrg_i */\r
-       }\r
-}\r
-\r
-static void j3d_read_tlm(opj_j3d_t *j3d) {\r
-       int len, Ztlm, Stlm, ST, SP, tile_tlm, i;\r
-       long int Ttlm_i, Ptlm_i;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       len = cio_read(cio, 2);         /* Ltlm */\r
-       Ztlm = cio_read(cio, 1);        /* Ztlm */\r
-       Stlm = cio_read(cio, 1);        /* Stlm */\r
-       ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);\r
-       SP = (Stlm >> 6) & 0x01;\r
-       tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);\r
-       for (i = 0; i < tile_tlm; i++) {\r
-               Ttlm_i = cio_read(cio, ST);     /* Ttlm_i */\r
-               Ptlm_i = cio_read(cio, SP ? 4 : 2);     /* Ptlm_i */\r
-       }\r
-}\r
-\r
-static void j3d_read_plm(opj_j3d_t *j3d) {\r
-       int len, i, Zplm, Nplm, add, packet_len = 0;\r
-       \r
-       opj_cio_t *cio = j3d->cio;\r
-\r
-       len = cio_read(cio, 2);         /* Lplm */\r
-       Zplm = cio_read(cio, 1);        /* Zplm */\r
-       len -= 3;\r
-       while (len > 0) {\r
-               Nplm = cio_read(cio, 4);                /* Nplm */\r
-               len -= 4;\r
-               for (i = Nplm; i > 0; i--) {\r
-                       add = cio_read(cio, 1);\r
-                       len--;\r
-                       packet_len = (packet_len << 7) + add;   /* Iplm_ij */\r
-                       if ((add & 0x80) == 0) {\r
-                               /* New packet */\r
-                               packet_len = 0;\r
-                       }\r
-                       if (len <= 0)\r
-                               break;\r
-               }\r
-       }\r
-}\r
-\r
-static void j3d_read_plt(opj_j3d_t *j3d) {\r
-       int len, i, Zplt, packet_len = 0, add;\r
-       \r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       len = cio_read(cio, 2);         /* Lplt */\r
-       Zplt = cio_read(cio, 1);        /* Zplt */\r
-       for (i = len - 3; i > 0; i--) {\r
-               add = cio_read(cio, 1);\r
-               packet_len = (packet_len << 7) + add;   /* Iplt_i */\r
-               if ((add & 0x80) == 0) {\r
-                       /* New packet */\r
-                       packet_len = 0;\r
-               }\r
-       }\r
-}\r
-\r
-static void j3d_read_ppm(opj_j3d_t *j3d) {\r
-       int len, Z_ppm, i, j;\r
-       int N_ppm;\r
-\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       len = cio_read(cio, 2);\r
-       cp->ppm = 1;\r
-       \r
-       Z_ppm = cio_read(cio, 1);       /* Z_ppm */\r
-       len -= 3;\r
-       while (len > 0) {\r
-               if (cp->ppm_previous == 0) {\r
-                       N_ppm = cio_read(cio, 4);       /* N_ppm */\r
-                       len -= 4;\r
-               } else {\r
-                       N_ppm = cp->ppm_previous;\r
-               }\r
-               j = cp->ppm_store;\r
-               if (Z_ppm == 0) {       /* First PPM marker */\r
-                       cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));\r
-                       cp->ppm_data_first = cp->ppm_data;\r
-                       cp->ppm_len = N_ppm;\r
-               } else {                        /* NON-first PPM marker */\r
-                       cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +     cp->ppm_store) * sizeof(unsigned char));\r
-                       cp->ppm_data_first = cp->ppm_data;\r
-                       cp->ppm_len = N_ppm + cp->ppm_store;\r
-               }\r
-               for (i = N_ppm; i > 0; i--) {   /* Read packet header */\r
-                       cp->ppm_data[j] = cio_read(cio, 1);\r
-                       j++;\r
-                       len--;\r
-                       if (len == 0)\r
-                               break;                  /* Case of non-finished packet header in present marker but finished in next one */\r
-               }\r
-               cp->ppm_previous = i - 1;\r
-               cp->ppm_store = j;\r
-       }\r
-}\r
-\r
-static void j3d_read_ppt(opj_j3d_t *j3d) {\r
-       int len, Z_ppt, i, j = 0;\r
-\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = cp->tcps + j3d->curtileno;\r
-       opj_cio_t *cio = j3d->cio;\r
-\r
-       len = cio_read(cio, 2);\r
-       Z_ppt = cio_read(cio, 1);\r
-       tcp->ppt = 1;\r
-       if (Z_ppt == 0) {               /* First PPT marker */\r
-               tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));\r
-               tcp->ppt_data_first = tcp->ppt_data;\r
-               tcp->ppt_store = 0;\r
-               tcp->ppt_len = len - 3;\r
-       } else {                        /* NON-first PPT marker */\r
-               tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));\r
-               tcp->ppt_data_first = tcp->ppt_data;\r
-               tcp->ppt_len = len - 3 + tcp->ppt_store;\r
-       }\r
-       j = tcp->ppt_store;\r
-       for (i = len - 3; i > 0; i--) {\r
-               tcp->ppt_data[j] = cio_read(cio, 1);\r
-               j++;\r
-       }\r
-       tcp->ppt_store = j;\r
-}\r
-\r
-static void j3d_write_sot(opj_j3d_t *j3d) {\r
-       int lenp, len;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-\r
-       j3d->sot_start = cio_tell(cio);\r
-       cio_write(cio, J3D_MS_SOT, 2);          /* SOT */\r
-       lenp = cio_tell(cio);\r
-       cio_skip(cio, 2);                                       /* Lsot (further) */\r
-       cio_write(cio, j3d->curtileno, 2);      /* Isot */\r
-       cio_skip(cio, 4);                                       /* Psot (further in j3d_write_sod) */\r
-       cio_write(cio, 0, 1);                           /* TPsot */\r
-       cio_write(cio, 1, 1);                           /* TNsot (no of tile-parts of this tile in this codestream)*/\r
-       len = cio_tell(cio) - lenp;\r
-       cio_seek(cio, lenp);\r
-       cio_write(cio, len, 2);                         /* Lsot */\r
-       cio_seek(cio, lenp + len);\r
-}\r
-\r
-static void j3d_read_sot(opj_j3d_t *j3d) {\r
-       int len, tileno, totlen, partno, numparts, i;\r
-       opj_tcp_t *tcp = NULL;\r
-       char status = 0;\r
-\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       len = cio_read(cio, 2);\r
-       tileno = cio_read(cio, 2);\r
-       \r
-       if (cp->tileno_size == 0) {\r
-               cp->tileno[cp->tileno_size] = tileno;\r
-               cp->tileno_size++;\r
-       } else {\r
-               i = 0;\r
-               while (i < cp->tileno_size && status == 0) {\r
-                       status = cp->tileno[i] == tileno ? 1 : 0;\r
-                       i++;\r
-               }\r
-               if (status == 0) {\r
-                       cp->tileno[cp->tileno_size] = tileno;\r
-                       cp->tileno_size++;\r
-               }\r
-       }\r
-       \r
-       totlen = cio_read(cio, 4);\r
-       if (!totlen)\r
-               totlen = cio_numbytesleft(cio) + 8;\r
-       \r
-       partno = cio_read(cio, 1);\r
-       numparts = cio_read(cio, 1);\r
-       \r
-       j3d->curtileno = tileno;\r
-       j3d->eot = cio_getbp(cio) - 12 + totlen;\r
-       j3d->state = J3D_STATE_TPH;\r
-       tcp = &cp->tcps[j3d->curtileno];\r
-       \r
-       if (tcp->first == 1) {\r
-               \r
-               /* Initialization PPT */\r
-               opj_tccp_t *tmp = tcp->tccps;\r
-               memcpy(tcp, j3d->default_tcp, sizeof(opj_tcp_t));\r
-               tcp->ppt = 0;\r
-               tcp->ppt_data = NULL;\r
-               tcp->ppt_data_first = NULL;\r
-               tcp->tccps = tmp;\r
-\r
-               for (i = 0; i < j3d->volume->numcomps; i++) {\r
-                       tcp->tccps[i] = j3d->default_tcp->tccps[i];\r
-               }\r
-               cp->tcps[j3d->curtileno].first = 0;\r
-       }\r
-}\r
-\r
-static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder) {\r
-       int l, layno;\r
-       int totlen;\r
-       opj_tcp_t *tcp = NULL;\r
-       opj_volume_info_t *volume_info = NULL;\r
-       \r
-       opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_cio_t *cio = j3d->cio;\r
-       \r
-       cio_write(cio, J3D_MS_SOD, 2);\r
-       if (j3d->curtileno == 0) {\r
-               j3d->sod_start = cio_tell(cio) + j3d->pos_correction;\r
-       }\r
-       \r
-       /* INDEX >> */\r
-       volume_info = j3d->volume_info;\r
-       if (volume_info && volume_info->index_on) {\r
-               volume_info->tile[j3d->curtileno].end_header = cio_tell(cio) + j3d->pos_correction - 1;\r
-       }\r
-       /* << INDEX */\r
-       \r
-       tcp = &cp->tcps[j3d->curtileno];\r
-       for (layno = 0; layno < tcp->numlayers; layno++) {\r
-               tcp->rates[layno] -= tcp->rates[layno] ? (j3d->sod_start / (cp->th * cp->tw * cp->tl)) : 0;\r
-       }\r
-       \r
-       if(volume_info) {\r
-               volume_info->num = 0;\r
-       }\r
-\r
-       l = tcd_encode_tile(tcd, j3d->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, volume_info);\r
-       \r
-       /* Writing Psot in SOT marker */\r
-       totlen = cio_tell(cio) + l - j3d->sot_start;\r
-       cio_seek(cio, j3d->sot_start + 6);\r
-       cio_write(cio, totlen, 4);\r
-       cio_seek(cio, j3d->sot_start + totlen);\r
-}\r
-\r
-static void j3d_read_sod(opj_j3d_t *j3d) {\r
-       int len, truncate = 0, i;\r
-       unsigned char *data = NULL, *data_ptr = NULL;\r
-\r
-       opj_cio_t *cio = j3d->cio;\r
-       int curtileno = j3d->curtileno;\r
-       \r
-       len = int_min(j3d->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);\r
-       \r
-       if (len == cio_numbytesleft(cio) + 1) {\r
-               truncate = 1;           /* Case of a truncate codestream */\r
-       }\r
-       \r
-       data = (unsigned char *) opj_malloc((j3d->tile_len[curtileno] + len) * sizeof(unsigned char));\r
-\r
-       for (i = 0; i < j3d->tile_len[curtileno]; i++) {\r
-               data[i] = j3d->tile_data[curtileno][i];\r
-       }\r
-\r
-       data_ptr = data + j3d->tile_len[curtileno];\r
-       for (i = 0; i < len; i++) {\r
-               data_ptr[i] = cio_read(cio, 1);\r
-       }\r
-       \r
-       j3d->tile_len[curtileno] += len;\r
-       opj_free(j3d->tile_data[curtileno]);\r
-       j3d->tile_data[curtileno] = data;\r
-       \r
-       if (!truncate) {\r
-               j3d->state = J3D_STATE_TPHSOT;\r
-       } else {\r
-               j3d->state = J3D_STATE_NEOC;    /* RAJOUTE !! */\r
-       }\r
-}\r
-\r
-static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno) {\r
-       \r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = &cp->tcps[tileno];\r
-       opj_cio_t *cio = j3d->cio;\r
-       int numcomps = j3d->volume->numcomps;\r
-       \r
-       cio_write(cio, J3D_MS_RGN, 2);                                          /* RGN  */\r
-       cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */\r
-       cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */\r
-       cio_write(cio, 0, 1);                                                           /* Srgn */\r
-       cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */\r
-}\r
-\r
-static void j3d_read_rgn(opj_j3d_t *j3d) {\r
-       int len, compno, roisty;\r
-\r
-       opj_cp_t *cp = j3d->cp;\r
-       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;\r
-       opj_cio_t *cio = j3d->cio;\r
-       int numcomps = j3d->volume->numcomps;\r
-\r
-       len = cio_read(cio, 2);                                                                         /* Lrgn */\r
-       compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */\r
-       roisty = cio_read(cio, 1);                                                                      /* Srgn */\r
-       tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */\r
-}\r
-\r
-static void j3d_write_eoc(opj_j3d_t *j3d) {\r
-       opj_cio_t *cio = j3d->cio;\r
-       /* opj_event_msg(j3d->cinfo, "%.8x: EOC\n", cio_tell(cio) + j3d->pos_correction); */\r
-       cio_write(cio, J3D_MS_EOC, 2);\r
-}\r
-\r
-static void j3d_read_eoc(opj_j3d_t *j3d) {\r
-       int i, tileno;\r
-\r
-#ifndef NO_PACKETS_DECODING  \r
-       opj_tcd_t *tcd = tcd_create(j3d->cinfo);\r
-    tcd_malloc_decode(tcd, j3d->volume, j3d->cp);\r
-       /*j3d_dump_volume(stdout, tcd->volume);\r
-       j3d_dump_cp(stdout, tcd->volume, tcd->cp);*/\r
-       for (i = 0; i < j3d->cp->tileno_size; i++) {\r
-               tileno = j3d->cp->tileno[i];\r
-               /*opj_event_msg(j3d->cinfo, EVT_INFO, "tcd_decode_tile \n");*/\r
-               tcd_decode_tile(tcd, j3d->tile_data[tileno], j3d->tile_len[tileno], tileno);\r
-               opj_free(j3d->tile_data[tileno]);\r
-               j3d->tile_data[tileno] = NULL;\r
-       }\r
-       tcd_free_decode(tcd);\r
-       tcd_destroy(tcd);\r
-#else \r
-       for (i = 0; i < j3d->cp->tileno_size; i++) {\r
-               tileno = j3d->cp->tileno[i];\r
-               opj_free(j3d->tile_data[tileno]);\r
-               j3d->tile_data[tileno] = NULL;\r
-       }\r
-#endif\r
-       \r
-       j3d->state = J3D_STATE_MT;\r
-}\r
-\r
-static void j3d_read_unk(opj_j3d_t *j3d) {\r
-       opj_event_msg(j3d->cinfo, EVT_WARNING, "Unknown marker\n");\r
-}\r
-\r
-static opj_atk_t atk_info_wt[] = {\r
-       {0, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1.230174104, 4, {0}, {0}, {0}, {1,1,1,1}, {-1.586134342059924, -0.052980118572961, 0.882911075530934, 0.443506852043971}},/* WT 9-7 IRR*/\r
-       {1, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {1,2}, {1,2}, {1,1}, {-1.0,1.0}},/* WT 5-3 REV*/\r
-       {2, 0, J3D_ATK_ARB, J3D_ATK_REV, 0, J3D_ATK_CON, 0, 2, {0,0}, {0,1}, {0,1}, {1,1}, {{-1.0},{1.0}}}, /* WT 2-2 REV*/\r
-       {3, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-1}, {0,1,2}, {0,1,2}, {1,1,3}, {{-1.0},{1.0},{1.0,0.0,-1.0}}}, /* WT 2-6 REV*/\r
-       {4, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-2}, {0,1,6}, {0,1,32}, {1,1,5}, {{-1},{1},{-3.0,22.0,0.0,-22.0,3.0}}}, /* WT 2-10 REV*/\r
-       {5, 1, J3D_ATK_ARB, J3D_ATK_IRR, 1, J3D_ATK_WS, 1, 7, {0}, {0}, {0}, {1,1,2,1,2,1,3},{{-1},{1.58613434206},{-0.460348209828, 0.460348209828},{0.25},{0.374213867768,-0.374213867768},{-1.33613434206},{0.29306717103,0,-0.29306717103}}}, /* WT 6-10 IRR*/\r
-       {6, 1, J3D_ATK_ARB, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 11, {0}, {0}, {0}, {1,1,2,1,2,1,2,1,2,1,5},{{-1},{0,99715069105},{-1.00573127827, 1.00573127827},{-0.27040357631},{2.20509972343, -2.20509972343},{0.08059995736},\r
-               {-1.62682532350, 1.62682532350},{0.52040357631},{0.60404664250, -0.60404664250},{-0.82775064841},{-0.06615812964, 0.29402137720, 0, -0.29402137720, 0.06615812964}}}, /* WT 10-18 IRR*/\r
-       {7, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 2, {0}, {0}, {0}, {1,1}, {-0.5, 0.25}},       /* WT 5-3 IRR*/\r
-       {8, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {4,4}, {8,8}, {2,2}, {{-9,1},{5,-1}}}         /* WT 13-7 REV*/\r
-};\r
-\r
-typedef struct opj_dec_mstabent {\r
-       /** marker value */\r
-       int id;\r
-       /** value of the state when the marker can appear */\r
-       int states;\r
-       /** action linked to the marker */\r
-       void (*handler) (opj_j3d_t *j3d);\r
-} opj_dec_mstabent_t;\r
-\r
-opj_dec_mstabent_t j3d_dec_mstab[] = {\r
-  {J3D_MS_SOC, J3D_STATE_MHSOC, j3d_read_soc},\r
-  {J3D_MS_SOT, J3D_STATE_MH | J3D_STATE_TPHSOT, j3d_read_sot},\r
-  {J3D_MS_SOD, J3D_STATE_TPH, j3d_read_sod},\r
-  {J3D_MS_EOC, J3D_STATE_TPHSOT, j3d_read_eoc},\r
-  {J3D_MS_CAP, J3D_STATE_MHSIZ, j3d_read_cap},\r
-  {J3D_MS_SIZ, J3D_STATE_MHSIZ, j3d_read_siz},\r
-  {J3D_MS_NSI, J3D_STATE_MHSIZ, j3d_read_nsi},\r
-  {J3D_MS_COD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cod},\r
-  {J3D_MS_COC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_coc},\r
-  {J3D_MS_RGN, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_rgn},\r
-  {J3D_MS_QCD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcd},\r
-  {J3D_MS_QCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcc},\r
-  {J3D_MS_POC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_poc},\r
-  {J3D_MS_TLM, J3D_STATE_MH, j3d_read_tlm},\r
-  {J3D_MS_PLM, J3D_STATE_MH, j3d_read_plm},\r
-  {J3D_MS_PLT, J3D_STATE_TPH, j3d_read_plt},\r
-  {J3D_MS_PPM, J3D_STATE_MH, j3d_read_ppm},\r
-  {J3D_MS_PPT, J3D_STATE_TPH, j3d_read_ppt},\r
-  {J3D_MS_SOP, 0, 0},\r
-  {J3D_MS_CRG, J3D_STATE_MH, j3d_read_crg},\r
-  {J3D_MS_COM, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_com},\r
-  {J3D_MS_DCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_dco},\r
-  {J3D_MS_ATK, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_atk},\r
-  {0, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_unk}\r
-  /*, -->must define the j3d_read functions\r
-  {J3D_MS_CBD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cbd},\r
-  {J3D_MS_MCT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mct},\r
-  {J3D_MS_MCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mcc},\r
-  {J3D_MS_MCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mco},\r
-  {J3D_MS_NLT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_nlt},\r
-  {J3D_MS_VMS, J3D_STATE_MH, j3d_read_vms},\r
-  {J3D_MS_DFS, J3D_STATE_MH, j3d_read_dfs},\r
-  {J3D_MS_ADS, J3D_STATE_MH, j3d_read_ads},\r
-  {J3D_MS_QPD, J3D_STATE_MH, j3d_read_qpd},\r
-  {J3D_MS_QPC, J3D_STATE_TPH, j3d_read_qpc}*/\r
-};\r
-\r
-/**\r
-Read the lookup table containing all the marker, status and action\r
-@param id Marker value\r
-*/\r
-static opj_dec_mstabent_t *j3d_dec_mstab_lookup(int id) {\r
-       opj_dec_mstabent_t *e;\r
-       for (e = j3d_dec_mstab; e->id != 0; e++) {\r
-               if (e->id == id) {\r
-                       break;\r
-               }\r
-       }\r
-       return e;\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-/* J3D / JPT decoder interface                                             */\r
-/* ----------------------------------------------------------------------- */\r
-\r
-opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo) {\r
-       opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));\r
-       if(j3d) {\r
-               j3d->cinfo = cinfo;\r
-               j3d->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));\r
-               if(!j3d->default_tcp) {\r
-                       opj_free(j3d);\r
-                       return NULL;\r
-               }\r
-       }\r
-       return j3d;\r
-}\r
-\r
-void j3d_destroy_decompress(opj_j3d_t *j3d) {\r
-       int i = 0;\r
-\r
-       if(j3d->tile_len != NULL) {\r
-               opj_free(j3d->tile_len);\r
-       }\r
-       if(j3d->tile_data != NULL) {\r
-               opj_free(j3d->tile_data);\r
-       }\r
-       if(j3d->default_tcp != NULL) {\r
-               opj_tcp_t *default_tcp = j3d->default_tcp;\r
-               if(default_tcp->ppt_data_first != NULL) {\r
-                       opj_free(default_tcp->ppt_data_first);\r
-               }\r
-               if(j3d->default_tcp->tccps != NULL) {\r
-                       opj_free(j3d->default_tcp->tccps);\r
-               }\r
-               opj_free(j3d->default_tcp);\r
-       }\r
-       if(j3d->cp != NULL) {\r
-               opj_cp_t *cp = j3d->cp;\r
-               if(cp->tcps != NULL) {\r
-                       for(i = 0; i < cp->tw * cp->th * cp->tl; i++) {\r
-                               if(cp->tcps[i].ppt_data_first != NULL) {\r
-                                       opj_free(cp->tcps[i].ppt_data_first);\r
-                               }\r
-                               if(cp->tcps[i].tccps != NULL) {\r
-                                       opj_free(cp->tcps[i].tccps);\r
-                               }\r
-                       }\r
-                       opj_free(cp->tcps);\r
-               }\r
-               if(cp->ppm_data_first != NULL) {\r
-                       opj_free(cp->ppm_data_first);\r
-               }\r
-               if(cp->tileno != NULL) {\r
-                       opj_free(cp->tileno);  \r
-               }\r
-               if(cp->comment != NULL) {\r
-                       opj_free(cp->comment);\r
-               }\r
-\r
-               opj_free(cp);\r
-       }\r
-\r
-       opj_free(j3d);\r
-}\r
-\r
-void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters) {\r
-       if(j3d && parameters) {\r
-               /* create and initialize the coding parameters structure */\r
-               opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));\r
-               cp->reduce[0] = parameters->cp_reduce[0];\r
-               cp->reduce[1] = parameters->cp_reduce[1];\r
-               cp->reduce[2] = parameters->cp_reduce[2];\r
-               cp->layer = parameters->cp_layer;\r
-               cp->bigendian = parameters->bigendian;\r
-\r
-    /* MM: Settings of the following two member variables would take\r
-      place during j3d_read_com. FIXME */\r
-               cp->encoding_format = ENCOD_3EB;\r
-               cp->transform_format = TRF_2D_DWT;\r
-\r
-               /* keep a link to cp so that we can destroy it later in j3d_destroy_decompress */\r
-               j3d->cp = cp;\r
-       }\r
-}\r
-\r
-opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio) {\r
-       opj_volume_t *volume = NULL;\r
-\r
-       opj_common_ptr cinfo = j3d->cinfo;\r
-\r
-       j3d->cio = cio;\r
-\r
-       /* create an empty volume */\r
-       volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));\r
-       j3d->volume = volume;\r
-\r
-       j3d->state = J3D_STATE_MHSOC;\r
-       \r
-       for (;;) {\r
-               opj_dec_mstabent_t *e;\r
-               int id = cio_read(cio, 2);\r
-               if (id >> 8 != 0xff) {\r
-                       opj_volume_destroy(volume);\r
-                       opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);\r
-                       return 0;\r
-               }\r
-               e = j3d_dec_mstab_lookup(id);\r
-               /*opj_event_msg(cinfo, EVT_INFO, "MARKER %x PREVSTATE %d E->STATE %d\n",e->id,j3d->state,e->states);*/\r
-               if (!(j3d->state & e->states)) {\r
-                       opj_volume_destroy(volume);\r
-                       opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);\r
-                       return 0;\r
-               }\r
-               if (e->handler) {\r
-                       (*e->handler)(j3d);\r
-               }\r
-               /*opj_event_msg(cinfo, EVT_INFO, "POSTSTATE %d\n",j3d->state);*/\r
-               if (j3d->state == J3D_STATE_MT) {\r
-                       break;\r
-               }\r
-               if (j3d->state == J3D_STATE_NEOC) {\r
-                       break;\r
-               }\r
-       }\r
-       if (j3d->state == J3D_STATE_NEOC) {\r
-               j3d_read_eoc(j3d);\r
-       }\r
-\r
-       if (j3d->state != J3D_STATE_MT) {\r
-               opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");\r
-       }\r
-       \r
-       return volume;\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-/* J3D encoder interface                                                       */\r
-/* ----------------------------------------------------------------------- */\r
-\r
-opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo) {\r
-       opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));\r
-       if(j3d) {\r
-               j3d->cinfo = cinfo;\r
-       }\r
-       return j3d;\r
-}\r
-\r
-void j3d_destroy_compress(opj_j3d_t *j3d) {\r
-       int tileno;\r
-\r
-       if(!j3d) return;\r
-\r
-       if(j3d->volume_info != NULL) {\r
-               opj_volume_info_t *volume_info = j3d->volume_info;\r
-               if (volume_info->index_on && j3d->cp) {\r
-                       opj_cp_t *cp = j3d->cp;\r
-                       for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {\r
-                               opj_tile_info_t *tile_info = &volume_info->tile[tileno];\r
-                               opj_free(tile_info->thresh);\r
-                               opj_free(tile_info->packet);\r
-                       }\r
-                       opj_free(volume_info->tile);\r
-               }\r
-               opj_free(volume_info);\r
-       }\r
-       if(j3d->cp != NULL) {\r
-               opj_cp_t *cp = j3d->cp;\r
-\r
-               if(cp->comment) {\r
-                       opj_free(cp->comment);\r
-               }\r
-               if(cp->matrice) {\r
-                       opj_free(cp->matrice);\r
-               }\r
-               for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {\r
-                       opj_free(cp->tcps[tileno].tccps);\r
-               }\r
-               opj_free(cp->tcps);\r
-               opj_free(cp);\r
-       }\r
-\r
-       opj_free(j3d);\r
-}\r
-\r
-void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume) {\r
-       int i, j, tileno, numpocs_tile;\r
-       opj_cp_t *cp = NULL;\r
-\r
-       if(!j3d || !parameters || ! volume) {\r
-               return;\r
-       }\r
-\r
-       /* create and initialize the coding parameters structure */\r
-       cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));\r
-\r
-       /* keep a link to cp so that we can destroy it later in j3d_destroy_compress */\r
-       j3d->cp = cp;\r
-\r
-       /* set default values for cp */\r
-       cp->tw = 1;\r
-       cp->th = 1;\r
-       cp->tl = 1;\r
-\r
-       /* copy user encoding parameters */\r
-       cp->disto_alloc = parameters->cp_disto_alloc;\r
-       cp->fixed_alloc = parameters->cp_fixed_alloc;\r
-       cp->fixed_quality = parameters->cp_fixed_quality;\r
-\r
-       /* transform and coding method */\r
-       cp->transform_format = parameters->transform_format;\r
-       cp->encoding_format = parameters->encoding_format;\r
-\r
-       /* mod fixed_quality */\r
-       if(parameters->cp_matrice) {\r
-               size_t array_size = parameters->tcp_numlayers * 3 * parameters->numresolution[0] * sizeof(int);\r
-               cp->matrice = (int *) opj_malloc(array_size);\r
-               memcpy(cp->matrice, parameters->cp_matrice, array_size);\r
-       } \r
-\r
-       /* creation of an index file ? */\r
-       cp->index_on = parameters->index_on;\r
-       if(cp->index_on) {\r
-               j3d->volume_info = (opj_volume_info_t*)opj_malloc(sizeof(opj_volume_info_t));\r
-       }\r
-       \r
-       /* tiles */\r
-       cp->tdx = parameters->cp_tdx;\r
-       cp->tdy = parameters->cp_tdy;\r
-       cp->tdz = parameters->cp_tdz;\r
-       /* tile offset */\r
-       cp->tx0 = parameters->cp_tx0;\r
-       cp->ty0 = parameters->cp_ty0;\r
-       cp->tz0 = parameters->cp_tz0;\r
-       /* comment string */\r
-       if(parameters->cp_comment) {\r
-               cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);\r
-               if(cp->comment) {\r
-                       strcpy(cp->comment, parameters->cp_comment);\r
-               }\r
-       }\r
-\r
-       /*calculate other encoding parameters*/\r
-       if (parameters->tile_size_on) {\r
-               cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);\r
-               cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);\r
-               cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);\r
-       } else {\r
-               cp->tdx = volume->x1 - cp->tx0;\r
-               cp->tdy = volume->y1 - cp->ty0;\r
-               cp->tdz = volume->z1 - cp->tz0;\r
-       }\r
-\r
-       /* initialize the multiple tiles */\r
-       /* ---------------------------- */\r
-       cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));\r
-\r
-       for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {\r
-               opj_tcp_t *tcp = &cp->tcps[tileno];\r
-               tcp->numlayers = parameters->tcp_numlayers;\r
-               for (j = 0; j < tcp->numlayers; j++) {\r
-                       if (cp->fixed_quality) {        /* add fixed_quality */\r
-                               tcp->distoratio[j] = parameters->tcp_distoratio[j];\r
-                       } else {\r
-                               tcp->rates[j] = parameters->tcp_rates[j];\r
-                       }\r
-               }\r
-               tcp->csty = parameters->csty;\r
-               tcp->prg = parameters->prog_order;\r
-               tcp->mct = volume->numcomps == 3 ? 1 : 0;\r
-\r
-               numpocs_tile = 0;\r
-               tcp->POC = 0;\r
-               if (parameters->numpocs) {\r
-                       /* initialisation of POC */\r
-                       tcp->POC = 1;\r
-                       for (i = 0; i < parameters->numpocs; i++) {\r
-                               if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {\r
-                                       opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];\r
-                                       tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;\r
-                                       tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;\r
-                                       tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;\r
-                                       tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;\r
-                                       tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;\r
-                                       tcp_poc->prg            = parameters->POC[numpocs_tile].prg;\r
-                                       tcp_poc->tile           = parameters->POC[numpocs_tile].tile;\r
-                                       numpocs_tile++;\r
-                               }\r
-                       }\r
-               }\r
-               tcp->numpocs = numpocs_tile;\r
-\r
-               tcp->tccps = (opj_tccp_t *) opj_malloc(volume->numcomps * sizeof(opj_tccp_t));\r
-               \r
-               for (i = 0; i < volume->numcomps; i++) {\r
-                       opj_tccp_t *tccp = &tcp->tccps[i];\r
-                       tccp->csty = parameters->csty & J3D_CCP_CSTY_PRT;       /* 0 => standard precint || 1 => custom-defined precinct  */\r
-                       tccp->numresolution[0] = parameters->numresolution[0];\r
-                       tccp->numresolution[1] = parameters->numresolution[1];\r
-                       tccp->numresolution[2] = parameters->numresolution[2];\r
-                                               assert (parameters->cblock_init[0] <= T1_MAXCBLKW);\r
-                                               assert (parameters->cblock_init[0] >= T1_MINCBLKW);\r
-                                               assert (parameters->cblock_init[1] <= T1_MAXCBLKH);\r
-                                               assert (parameters->cblock_init[1] >= T1_MINCBLKH);\r
-                                               assert (parameters->cblock_init[2] <= T1_MAXCBLKD);\r
-                                               assert (parameters->cblock_init[2] >= T1_MINCBLKD);\r
-                       tccp->cblk[0] = int_floorlog2(parameters->cblock_init[0]); \r
-                       tccp->cblk[1] = int_floorlog2(parameters->cblock_init[1]); \r
-                       tccp->cblk[2] = int_floorlog2(parameters->cblock_init[2]); \r
-                                               assert (tccp->cblk[0]+tccp->cblk[1]+tccp->cblk[1] <= T1_MAXWHD);\r
-                       tccp->cblksty = parameters->mode; /*Codeblock style --> Table A.19 (default 0)*/\r
-\r
-                       /*ATK / transform */\r
-                       tccp->reversible = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3  */\r
-                       for (j = 0; j < 3; j++) {\r
-                                       tccp->dwtid[j] = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3  */\r
-                       }\r
-                                               \r
-                       /* Quantification: SEQNT (Scalar Expounded, value for each subband) / NOQNT (no quant)*/\r
-                       tccp->qntsty = parameters->irreversible ? J3D_CCP_QNTSTY_SEQNT : J3D_CCP_QNTSTY_NOQNT;\r
-                       tccp->numgbits = 2;\r
-                       if (i == parameters->roi_compno) {\r
-                               tccp->roishift = parameters->roi_shift;\r
-                       } else {\r
-                               tccp->roishift = 0;\r
-                       }\r
-                       /* Custom defined precints */\r
-                       if (parameters->csty & J3D_CCP_CSTY_PRT) {\r
-                               int k;\r
-                               for (k = 0; k < 3; k++) {\r
-                                       int p = 0;\r
-                                       for (j = tccp->numresolution[k] - 1; j >= 0; j--) {\r
-                                               if (p < parameters->res_spec) {/* p < number of precinct size specifications */\r
-                                                       if (parameters->prct_init[k][p] < 1) {\r
-                                                               tccp->prctsiz[k][j] = 1;\r
-                                                       } else {\r
-                                                               tccp->prctsiz[k][j] = int_floorlog2(parameters->prct_init[k][p]);\r
-                                                       }\r
-                                               } else {\r
-                                                       int res_spec = parameters->res_spec;\r
-                                                       int size_prct = parameters->prct_init[k][res_spec - 1] >> (p - (res_spec - 1));\r
-                                                       if (size_prct < 1) {\r
-                                                               tccp->prctsiz[k][j] = 1;\r
-                                                       } else {\r
-                                                               tccp->prctsiz[k][j] = int_floorlog2(size_prct);\r
-                                                       }\r
-                                               }\r
-                                       }\r
-                                       p++;\r
-                               }\r
-                       } else {\r
-                               int k;\r
-                               for (k = 0; k < 3; k++) {\r
-                    for (j = 0; j < tccp->numresolution[k]; j++) {\r
-                        tccp->prctsiz[k][j] = 15;\r
-                                       }\r
-                               }\r
-                       }\r
-                       /*Calcular stepsize for each subband (if NOQNT -->stepsize = 1.0)*/\r
-                       dwt_calc_explicit_stepsizes(tccp, volume->comps[i].prec);\r
-               }\r
-       }\r
-}\r
-\r
-/**\r
-Create an index file\r
-@param j3d\r
-@param cio\r
-@param volume_info\r
-@param index Index filename\r
-@return Returns 1 if successful, returns 0 otherwise\r
-*/\r
-static int j3d_create_index(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_info_t *volume_info, char *index) {\r
-       \r
-       int tileno, compno, layno, resno, precno, pack_nb, x, y, z;\r
-       FILE *stream = NULL;\r
-       double total_disto = 0;\r
-\r
-       volume_info->codestream_size = cio_tell(cio) + j3d->pos_correction;     /* Correction 14/4/03 suite rmq de Patrick */\r
-\r
-       stream = fopen(index, "w");\r
-       if (!stream) {\r
-               opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);\r
-               return 0;\r
-       }\r
-       \r
-       fprintf(stream, "w %d\t h %d\t l %d\n", volume_info->volume_w, volume_info->volume_h, volume_info->volume_l);\r
-       fprintf(stream, "TRASNFORM\t%d\n", volume_info->transform_format);\r
-       fprintf(stream, "ENTROPY CODING\t%d\n", volume_info->encoding_format);\r
-       fprintf(stream, "PROG\t%d\n", volume_info->prog);\r
-       fprintf(stream, "TILE\tx %d y %d z %d\n", volume_info->tile_x, volume_info->tile_y, volume_info->tile_z);\r
-       fprintf(stream, "NOTILE\tx %d y %d z %d\n", volume_info->tw, volume_info->th, volume_info->tl);\r
-       fprintf(stream, "COMPONENTS\t%d\n", volume_info->comp);\r
-       fprintf(stream, "LAYER\t%d\n", volume_info->layer);\r
-       fprintf(stream, "RESOLUTIONS\tx %d y %d z %d\n", volume_info->decomposition[0], volume_info->decomposition[1], volume_info->decomposition[2]);\r
-       \r
-       fprintf(stream, "Precint sizes for each resolution:\n");\r
-       for (resno = volume_info->decomposition[0]; resno >= 0; resno--) {\r
-               fprintf(stream, "Resno %d \t [%d,%d,%d] \n", resno,\r
-                       (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[2][resno]));   /* based on tile 0 */\r
-       }\r
-       fprintf(stream, "HEADER_END\t%d\n", volume_info->main_head_end);\r
-       fprintf(stream, "CODESTREAM\t%d\n", volume_info->codestream_size);\r
-       fprintf(stream, "Num_tile Start_pos End_header End_pos Distotile Nbpix Ratio\n");\r
-       for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {\r
-               fprintf(stream, "%4d\t%9d\t%9d\t%9d\t%9e\t%9d\t%9e\n",\r
-                       volume_info->tile[tileno].num_tile,\r
-                       volume_info->tile[tileno].start_pos,\r
-                       volume_info->tile[tileno].end_header,\r
-                       volume_info->tile[tileno].end_pos,\r
-                       volume_info->tile[tileno].distotile, volume_info->tile[tileno].nbpix,\r
-                       volume_info->tile[tileno].distotile / volume_info->tile[tileno].nbpix);\r
-       }\r
-       \r
-       for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {\r
-               int start_pos, end_pos;\r
-               double disto = 0;\r
-               pack_nb = 0;\r
-               if (volume_info->prog == LRCP) {        /* LRCP */\r
-                       fprintf(stream, "pack_nb tileno layno resno compno precno start_pos  end_pos   disto\n");\r
-                       for (layno = 0; layno < volume_info->layer; layno++) {\r
-                               for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {\r
-                                       for (compno = 0; compno < volume_info->comp; compno++) {\r
-                                               int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];\r
-                                               for (precno = 0; precno < prec_max; precno++) {\r
-                                                       start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;\r
-                                                       end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;\r
-                                                       disto = volume_info->tile[tileno].packet[pack_nb].disto;\r
-                                                       fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);\r
-                                                       total_disto += disto;\r
-                                                       pack_nb++;\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               } /* LRCP */\r
-               else if (volume_info->prog == RLCP) {   /* RLCP */\r
-                       /*\r
-                       fprintf(stream, "pack_nb tileno resno layno compno precno start_pos  end_pos   disto");\r
-                       */\r
-                       for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {\r
-                               for (layno = 0; layno < volume_info->layer; layno++) {\r
-                                       for (compno = 0; compno < volume_info->comp; compno++) {\r
-                                               int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno]* volume_info->tile[tileno].prctno[2][resno];\r
-                                               for (precno = 0; precno < prec_max; precno++) {\r
-                                                       start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;\r
-                                                       end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;\r
-                                                       disto = volume_info->tile[tileno].packet[pack_nb].disto;\r
-                                                       fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",\r
-                                                               pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);\r
-                                                       total_disto += disto;\r
-                                                       pack_nb++;\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               } /* RLCP */\r
-               else if (volume_info->prog == RPCL) {   /* RPCL */\r
-                       /*\r
-                       fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos  end_pos   disto\n"); \r
-                       */\r
-                       for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {\r
-                               /* I suppose components have same XRsiz, YRsiz */\r
-                               /*int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;*/\r
-                               /*int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;*/\r
-                               int x0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_x;\r
-                               int y0 = volume_info->tile_Oy + (int)floor( (float)tileno/(float)volume_info->th ) * volume_info->tile_y;\r
-                               int z0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tl ) * volume_info->tile_z;\r
-                               int x1 = x0 + volume_info->tile_x;\r
-                               int y1 = y0 + volume_info->tile_y;\r
-                               int z1 = z0 + volume_info->tile_z;\r
-                               for(z = z0; z < z1; z++) {\r
-                                       for(y = y0; y < y1; y++) {\r
-                                               for(x = x0; x < x1; x++) {\r
-                                                       for (compno = 0; compno < volume_info->comp; compno++) {\r
-                                                               int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];\r
-                                                               for (precno = 0; precno < prec_max; precno++) {\r
-                                                                       int pcnx = volume_info->tile[tileno].prctno[0][resno];\r
-                                                                       int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );\r
-                                                                       int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );\r
-                                                                       int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );\r
-                                                                       int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;\r
-                                                                       int precno_y = (int) floor( (float)precno/(float)pcnx );\r
-                                                                       if (precno_y*pcy == y ) {\r
-                                                                               if (precno_x*pcx == x ) {\r
-                                                                                       for (layno = 0; layno < volume_info->layer; layno++) {\r
-                                                                                               start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;\r
-                                                                                               end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;\r
-                                                                                               disto = volume_info->tile[tileno].packet[pack_nb].disto;\r
-                                                                                               fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",\r
-                                                                                                       pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto); \r
-                                                                                               total_disto += disto;\r
-                                                                                               pack_nb++; \r
-                                                                                       }\r
-                                                                               }\r
-                                                                       }\r
-                                                               } /* precno */\r
-                                                       } /* compno */\r
-                                               } /* x = x0..x1 */\r
-                                       } /* y = y0..y1 */\r
-                               } /* z = z0..z1 */\r
-                       } /* resno */\r
-               } /* RPCL */\r
-               else if (volume_info->prog == PCRL) {   /* PCRL */\r
-                       /* I suppose components have same XRsiz, YRsiz */\r
-                       int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;\r
-                       int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;\r
-                       int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;\r
-                       int x1 = x0 + volume_info->tile_x;\r
-                       int y1 = y0 + volume_info->tile_y;\r
-                       int z1 = z0 + volume_info->tile_z;\r
-                       /*\r
-                       fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos  end_pos   disto\n"); \r
-                       */\r
-                       for(z = z0; z < z1; z++) {\r
-                               for(y = y0; y < y1; y++) {\r
-                                       for(x = x0; x < x1; x++) {\r
-                                               for (compno = 0; compno < volume_info->comp; compno++) {\r
-                                                       for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {\r
-                                                               int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno];\r
-                                                               for (precno = 0; precno < prec_max; precno++) {\r
-                                                               int pcnx = volume_info->tile[tileno].prctno[0][resno];\r
-                                                               int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );\r
-                                                               int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );\r
-                                                               int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );\r
-                                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;\r
-                                                               int precno_y = (int) floor( (float)precno/(float)pcnx );\r
-                                                               int precno_z = (int) floor( (float)precno/(float)pcnx );\r
-                                                               if (precno_z*pcz == z ) {\r
-                                                                       if (precno_y*pcy == y ) {\r
-                                                                               if (precno_x*pcx == x ) {\r
-                                                                                       for (layno = 0; layno < volume_info->layer; layno++) {\r
-                                                                                               start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;\r
-                                                                                               end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;\r
-                                                                                               disto = volume_info->tile[tileno].packet[pack_nb].disto;\r
-                                                                                               fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",\r
-                                                                                                       pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto); \r
-                                                                                               total_disto += disto;\r
-                                                                                               pack_nb++; \r
-                                                                                       }\r
-                                                                               }\r
-                                                                       }\r
-                                                               }\r
-                                                       } /* precno */\r
-                                               } /* resno */\r
-                                       } /* compno */\r
-                               } /* x = x0..x1 */\r
-                       } /* y = y0..y1 */\r
-                       }\r
-               } /* PCRL */\r
-               else {  /* CPRL */\r
-                       /*\r
-                       fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos  end_pos   disto\n"); \r
-                       */\r
-                       for (compno = 0; compno < volume_info->comp; compno++) {\r
-                               /* I suppose components have same XRsiz, YRsiz */\r
-                               int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;\r
-                               int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;\r
-                               int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;\r
-                               int x1 = x0 + volume_info->tile_x;\r
-                               int y1 = y0 + volume_info->tile_y;\r
-                               int z1 = z0 + volume_info->tile_z;\r
-                               for(z = z0; z < z1; z++) {\r
-                                       for(y = y0; y < y1; y++) {\r
-                                               for(x = x0; x < x1; x++) {\r
-                                                       for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {\r
-                                                               int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];\r
-                                                               for (precno = 0; precno < prec_max; precno++) {\r
-                                                                       int pcnx = volume_info->tile[tileno].prctno[0][resno];\r
-                                                                       int pcny = volume_info->tile[tileno].prctno[1][resno];\r
-                                                                       int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );\r
-                                                                       int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );\r
-                                                                       int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );\r
-                                                                       int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;\r
-                                                                       int precno_y = (int) floor( (float)precno/(float)pcnx );\r
-                                                                       int precno_z = 0; /*???*/\r
-                                                                       if (precno_z*pcz == z ) {\r
-                                                                               if (precno_y*pcy == y ) {\r
-                                                                                       if (precno_x*pcx == x ) {\r
-                                                                                               for (layno = 0; layno < volume_info->layer; layno++) {\r
-                                                                                                       start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;\r
-                                                                                                       end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;\r
-                                                                                                       disto = volume_info->tile[tileno].packet[pack_nb].disto;\r
-                                                                                                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",\r
-                                                                                                               pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto); \r
-                                                                                                       total_disto += disto;\r
-                                                                                                       pack_nb++; \r
-                                                                                               }\r
-                                                                                       }\r
-                                                                               }\r
-                                                                       }\r
-                                                               } /* precno */\r
-                                                       } /* resno */\r
-                                               } /* x = x0..x1 */\r
-                                       } /* y = y0..y1 */\r
-                               } /* z = z0..z1 */\r
-                       } /* comno */\r
-               } /* CPRL */   \r
-       } /* tileno */\r
-       \r
-       fprintf(stream, "SE_MAX\t%8e\n", volume_info->D_max);   /* SE max */\r
-       fprintf(stream, "SE_TOTAL\t%.8e\n", total_disto);                       /* SE totale */\r
-       \r
-\r
-       fclose(stream);\r
-\r
-       return 1;\r
-}\r
-\r
-bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index) {\r
-       int tileno, compno;\r
-       opj_volume_info_t *volume_info = NULL;\r
-       opj_cp_t *cp = NULL;\r
-       opj_tcd_t *tcd = NULL;  /* TCD component */\r
-\r
-       j3d->cio = cio; \r
-       j3d->volume = volume;\r
-       cp = j3d->cp;\r
-\r
-       /*j3d_dump_volume(stdout, volume);\r
-       j3d_dump_cp(stdout, volume, cp);*/\r
-\r
-       /* INDEX >> */\r
-       volume_info = j3d->volume_info;\r
-       if (volume_info && cp->index_on) {\r
-               volume_info->index_on = cp->index_on;\r
-               volume_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tile_info_t));\r
-               volume_info->volume_w = volume->x1 - volume->x0;\r
-               volume_info->volume_h = volume->y1 - volume->y0;\r
-               volume_info->volume_l = volume->z1 - volume->z0;\r
-               volume_info->prog = (&cp->tcps[0])->prg;\r
-               volume_info->tw = cp->tw;\r
-               volume_info->th = cp->th;\r
-               volume_info->tl = cp->tl;\r
-               volume_info->tile_x = cp->tdx;  /* new version parser */\r
-               volume_info->tile_y = cp->tdy;  /* new version parser */\r
-               volume_info->tile_z = cp->tdz;  /* new version parser */\r
-               volume_info->tile_Ox = cp->tx0; /* new version parser */\r
-               volume_info->tile_Oy = cp->ty0; /* new version parser */\r
-               volume_info->tile_Oz = cp->tz0; /* new version parser */\r
-               volume_info->transform_format = cp->transform_format;\r
-               volume_info->encoding_format = cp->encoding_format;\r
-               volume_info->comp = volume->numcomps;\r
-               volume_info->layer = (&cp->tcps[0])->numlayers;\r
-               volume_info->decomposition[0] = (&cp->tcps[0])->tccps->numresolution[0] - 1;\r
-               volume_info->decomposition[1] = (&cp->tcps[0])->tccps->numresolution[1] - 1;\r
-               volume_info->decomposition[2] = (&cp->tcps[0])->tccps->numresolution[2] - 1;\r
-               volume_info->D_max = 0;         /* ADD Marcela */\r
-       }\r
-       /* << INDEX */\r
-\r
-       j3d_write_soc(j3d);\r
-       j3d_write_siz(j3d);\r
-       if (j3d->cinfo->codec_format == CODEC_J3D) {\r
-               j3d_write_cap(j3d);\r
-               j3d_write_nsi(j3d);\r
-       }\r
-\r
-       /*if (j3d->cp->transform_format != TRF_2D_DWT || j3d->cp->encoding_format != ENCOD_2EB)*/\r
-               j3d_write_com(j3d);\r
-\r
-       j3d_write_cod(j3d);\r
-       j3d_write_qcd(j3d);\r
-       for (compno = 0; compno < volume->numcomps; compno++) {\r
-               opj_tcp_t *tcp = &cp->tcps[0];\r
-               if (tcp->tccps[compno].roishift)\r
-                       j3d_write_rgn(j3d, compno, 0);                  \r
-       }\r
-       /*Optional 15444-2 markers*/\r
-       if (j3d->cp->tcps->tccps[0].atk != NULL)\r
-        j3d_write_atk(j3d);\r
-       if (j3d->volume->comps[0].dcoffset != 0)\r
-        j3d_write_dco(j3d);\r
-\r
-       /* INDEX >> */\r
-       if(volume_info && volume_info->index_on) {\r
-               volume_info->main_head_end = cio_tell(cio) - 1;\r
-       }\r
-       /* << INDEX */\r
-\r
-       /* create the tile encoder */\r
-       tcd = tcd_create(j3d->cinfo);\r
-\r
-       /* encode each tile */\r
-       for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {\r
-               opj_event_msg(j3d->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th * cp->tl);\r
-               \r
-               j3d->curtileno = tileno;\r
-\r
-               /* initialisation before tile encoding  */\r
-               if (tileno == 0) {      \r
-                       tcd_malloc_encode(tcd, volume, cp, j3d->curtileno);\r
-               } else {\r
-                       tcd_init_encode(tcd, volume, cp, j3d->curtileno);\r
-               }\r
-               \r
-               /* INDEX >> */\r
-               if(volume_info && volume_info->index_on) {\r
-                       volume_info->tile[j3d->curtileno].num_tile = j3d->curtileno;\r
-                       volume_info->tile[j3d->curtileno].start_pos = cio_tell(cio) + j3d->pos_correction;\r
-               }\r
-               /* << INDEX */\r
-               \r
-               j3d_write_sot(j3d);\r
-       \r
-               for (compno = 1; compno < volume->numcomps; compno++) {\r
-                       j3d_write_coc(j3d, compno);\r
-                       j3d_write_qcc(j3d, compno);\r
-               }\r
-\r
-               if (cp->tcps[tileno].numpocs) {\r
-                       j3d_write_poc(j3d);\r
-               }\r
-               j3d_write_sod(j3d, tcd); /*--> tcd_encode_tile*/\r
-\r
-               /* INDEX >> */\r
-               if(volume_info && volume_info->index_on) {\r
-                       volume_info->tile[j3d->curtileno].end_pos = cio_tell(cio) + j3d->pos_correction - 1;\r
-               }\r
-               /* << INDEX */          \r
-       }\r
-       \r
-       /* destroy the tile encoder */\r
-       tcd_free_encode(tcd);\r
-       tcd_destroy(tcd);\r
-\r
-       j3d_write_eoc(j3d);\r
-       \r
-       /* Creation of the index file */\r
-       if(volume_info && volume_info->index_on) {\r
-               if(!j3d_create_index(j3d, cio, volume_info, index)) {\r
-                       opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to create index file %s\n", index);\r
-                       return false;\r
-               }\r
-       }\r
-         \r
-       return true;\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup J3D J3D - JPEG-2000 PART 10 codestream reader/writer */
+/*@{*/
+
+/** @name Funciones locales */
+/*@{*/
+
+/**
+Write the SOC marker (Start Of Codestream)
+@param j3d J3D handle
+*/
+static void j3d_write_soc(opj_j3d_t *j3d);
+/**
+Read the SOC marker (Start of Codestream)
+@param j3d J3D handle
+*/
+static void j3d_read_soc(opj_j3d_t *j3d);
+/**
+Write the SIZ marker (2D volume and tile size)
+@param j3d J3D handle
+*/
+static void j3d_write_siz(opj_j3d_t *j3d);
+/**
+Read the SIZ marker (2D volume and tile size)
+@param j3d J3D handle
+*/
+static void j3d_read_siz(opj_j3d_t *j3d);
+/**
+Write the NSI marker (3rd volume and tile size)
+@param j3d J3D handle
+*/
+static void j3d_write_nsi(opj_j3d_t *j3d);
+/**
+Read the NSI marker (3rd volume and tile size)
+@param j3d J3D handle
+*/
+static void j3d_read_nsi(opj_j3d_t *j3d);
+/**
+Write the COM marker (comment)
+@param j3d J3D handle
+*/
+static void j3d_write_com(opj_j3d_t *j3d);
+/**
+Read the COM marker (comment)
+@param j3d J3D handle
+*/
+static void j3d_read_com(opj_j3d_t *j3d);
+/**
+Write the value concerning the specified component in the marker COD and COC
+@param j3d J3D handle
+@param compno Number of the component concerned by the information written
+*/
+static void j3d_write_cox(opj_j3d_t *j3d, int compno);
+/**
+Read the value concerning the specified component in the marker COD and COC
+@param j3d J3D handle
+@param compno Number of the component concerned by the information read
+*/
+static void j3d_read_cox(opj_j3d_t *j3d, int compno);
+/**
+Write the COD marker (coding style default)
+@param j3d J3D handle
+*/
+static void j3d_write_cod(opj_j3d_t *j3d);
+/**
+Read the COD marker (coding style default)
+@param j3d J3D handle
+*/
+static void j3d_read_cod(opj_j3d_t *j3d);
+/**
+Write the COC marker (coding style component)
+@param j3d J3D handle
+@param compno Number of the component concerned by the information written
+*/
+static void j3d_write_coc(opj_j3d_t *j3d, int compno);
+/**
+Read the COC marker (coding style component)
+@param j3d J3D handle
+*/
+static void j3d_read_coc(opj_j3d_t *j3d);
+/**
+Write the value concerning the specified component in the marker QCD and QCC
+@param j3d J3D handle
+@param compno Number of the component concerned by the information written
+*/
+static void j3d_write_qcx(opj_j3d_t *j3d, int compno);
+/**
+Read the value concerning the specified component in the marker QCD and QCC
+@param j3d J3D handle
+@param compno Number of the component concern by the information read
+@param len Length of the information in the QCX part of the marker QCD/QCC
+*/
+static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len);
+/**
+Write the QCD marker (quantization default)
+@param j3d J3D handle
+*/
+static void j3d_write_qcd(opj_j3d_t *j3d);
+/**
+Read the QCD marker (quantization default)
+@param j3d J3D handle
+*/
+static void j3d_read_qcd(opj_j3d_t *j3d);
+/**
+Write the QCC marker (quantization component)
+@param j3d J3D handle
+@param compno Number of the component concerned by the information written
+*/
+static void j3d_write_qcc(opj_j3d_t *j3d, int compno);
+/**
+Read the QCC marker (quantization component)
+@param j3d J3D handle
+*/
+static void j3d_read_qcc(opj_j3d_t *j3d);
+/**
+Write the POC marker (progression order change)
+@param j3d J3D handle
+*/
+static void j3d_write_poc(opj_j3d_t *j3d);
+/**
+Read the POC marker (progression order change)
+@param j3d J3D handle
+*/
+static void j3d_read_poc(opj_j3d_t *j3d);
+/**
+Read the CRG marker (component registration)
+@param j3d J3D handle
+*/
+static void j3d_read_crg(opj_j3d_t *j3d);
+/**
+Read the TLM marker (tile-part lengths)
+@param j3d J3D handle
+*/
+static void j3d_read_tlm(opj_j3d_t *j3d);
+/**
+Read the PLM marker (packet length, main header)
+@param j3d J3D handle
+*/
+static void j3d_read_plm(opj_j3d_t *j3d);
+/**
+Read the PLT marker (packet length, tile-part header)
+@param j3d J3D handle
+*/
+static void j3d_read_plt(opj_j3d_t *j3d);
+/**
+Read the PPM marker (packet packet headers, main header)
+@param j3d J3D handle
+*/
+static void j3d_read_ppm(opj_j3d_t *j3d);
+/**
+Read the PPT marker (packet packet headers, tile-part header)
+@param j3d J3D handle
+*/
+static void j3d_read_ppt(opj_j3d_t *j3d);
+/**
+Write the SOT marker (start of tile-part)
+@param j3d J3D handle
+*/
+static void j3d_write_sot(opj_j3d_t *j3d);
+/**
+Read the SOT marker (start of tile-part)
+@param j3d J3D handle
+*/
+static void j3d_read_sot(opj_j3d_t *j3d);
+/**
+Write the SOD marker (start of data)
+@param j3d J3D handle
+@param tile_coder Pointer to a TCD handle
+*/
+static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder);
+/**
+Read the SOD marker (start of data)
+@param j3d J3D handle
+*/
+static void j3d_read_sod(opj_j3d_t *j3d);
+/**
+Write the RGN marker (region-of-interest)
+@param j3d J3D handle
+@param compno Number of the component concerned by the information written
+@param tileno Number of the tile concerned by the information written
+*/
+static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno);
+/**
+Read the RGN marker (region-of-interest)
+@param j3d J3D handle
+*/
+static void j3d_read_rgn(opj_j3d_t *j3d);
+/**
+Write the EOC marker (end of codestream)
+@param j3d J3D handle
+*/
+static void j3d_write_eoc(opj_j3d_t *j3d);
+/**
+Read the EOC marker (end of codestream)
+@param j3d J3D handle
+*/
+static void j3d_read_eoc(opj_j3d_t *j3d);
+/**
+Read an unknown marker
+@param j3d J3D handle
+*/
+static void j3d_read_unk(opj_j3d_t *j3d);
+/**
+Write the CAP marker (extended capabilities)
+@param j3d J3D handle
+*/
+static void j3d_write_cap(opj_j3d_t *j3d);
+/**
+Read the CAP marker (extended capabilities)
+@param j3d J3D handle
+*/
+static void j3d_read_cap(opj_j3d_t *j3d);
+/**
+Write the DCO marker (Variable DC offset)
+@param j3d J3D handle
+*/
+static void j3d_write_dco(opj_j3d_t *j3d);
+/**
+Read the DCO marker (Variable DC offset)
+@param j3d J3D handle
+*/
+static void j3d_read_dco(opj_j3d_t *j3d);
+/**
+Write the ATK marker (arbitrary transformation kernel)
+@param j3d J3D handle
+*/
+static void j3d_write_atk(opj_j3d_t *j3d);
+/**
+Read the ATK marker (arbitrary transformation kernel)
+@param j3d J3D handle
+*/
+static void j3d_read_atk(opj_j3d_t *j3d);
+/**
+Write the CBD marker (component bit depth definition)
+@param j3d J3D handle
+*/
+static void j3d_write_cbd(opj_j3d_t *j3d);
+/**
+Read the CBD marker (component bit depth definition)
+@param j3d J3D handle
+*/
+static void j3d_read_cbd(opj_j3d_t *j3d);
+/**
+Write the MCT marker (multiple component transfomation definition)
+@param j3d J3D handle
+*/
+static void j3d_write_mct(opj_j3d_t *j3d);
+/**
+Read the MCT marker (multiple component transfomation definition)
+@param j3d J3D handle
+*/
+static void j3d_read_mct(opj_j3d_t *j3d);
+/**
+Write the MCC marker (multiple component transfomation collection)
+@param j3d J3D handle
+*/
+static void j3d_write_mcc(opj_j3d_t *j3d);
+/**
+Read the MCC marker (multiple component transfomation collection)
+@param j3d J3D handle
+*/
+static void j3d_read_mcc(opj_j3d_t *j3d);
+/**
+Write the MCO marker (multiple component transfomation ordering)
+@param j3d J3D handle
+*/
+static void j3d_write_mco(opj_j3d_t *j3d);
+/**
+Read the MCO marker (multiple component transfomation ordering)
+@param j3d J3D handle
+*/
+static void j3d_read_mco(opj_j3d_t *j3d);
+/**
+Write the NLT marker (non-linearity point transformation)
+@param j3d J3D handle
+*/
+static void j3d_write_nlt(opj_j3d_t *j3d);
+/**
+Read the NLT marker (non-linearity point transformation)
+@param j3d J3D handle
+*/
+static void j3d_read_nlt(opj_j3d_t *j3d);
+/*@}*/
+
+/* ----------------------------------------------------------------------- */
+
+static void j3d_dump_volume(FILE *fd, opj_volume_t * vol) {
+       int compno;
+       fprintf(fd, "volume {\n");
+       fprintf(fd, "  x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", vol->x0, vol->y0, vol->z0,vol->x1, vol->y1,  vol->z1);
+       fprintf(fd, "  numcomps=%d\n", vol->numcomps);
+       for (compno = 0; compno < vol->numcomps; compno++) {
+               opj_volume_comp_t *comp = &vol->comps[compno];
+               fprintf(fd, "  comp %d {\n", compno);
+               fprintf(fd, "    dx=%d, dy=%d, dz=%d\n", comp->dx, comp->dy, comp->dz);
+               fprintf(fd, "    prec=%d\n", comp->prec);
+               fprintf(fd, "    sgnd=%d\n", comp->sgnd);
+               fprintf(fd, "  }\n");
+       }
+       fprintf(fd, "}\n");
+}
+
+static void j3d_dump_cp(FILE *fd, opj_volume_t * vol, opj_cp_t * cp) {
+       int tileno, compno, layno, bandno, resno, numbands;
+       fprintf(fd, "coding parameters {\n");
+       fprintf(fd, "  tx0=%d, ty0=%d, tz0=%d\n", cp->tx0, cp->ty0, cp->tz0);
+       fprintf(fd, "  tdx=%d, tdy=%d, tdz=%d\n", cp->tdx, cp->tdy, cp->tdz);
+       fprintf(fd, "  tw=%d, th=%d, tl=%d\n", cp->tw, cp->th, cp->tl);
+       fprintf(fd, "  transform format: %d\n", cp->transform_format);
+       fprintf(fd, "  encoding format: %d\n", cp->encoding_format);
+       for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
+               opj_tcp_t *tcp = &cp->tcps[tileno];
+               fprintf(fd, "  tile %d {\n", tileno);
+               fprintf(fd, "    csty=%x\n", tcp->csty);
+               fprintf(fd, "    prg=%d\n", tcp->prg);
+               fprintf(fd, "    numlayers=%d\n", tcp->numlayers);
+               fprintf(fd, "    mct=%d\n", tcp->mct);
+               fprintf(fd, "    rates=");
+               for (layno = 0; layno < tcp->numlayers; layno++) {
+                       fprintf(fd, "%f ", tcp->rates[layno]);
+               }
+               fprintf(fd, "\n");
+               fprintf(fd, "    first=%d\n", tcp->first);
+               for (compno = 0; compno < vol->numcomps; compno++) {
+                       opj_tccp_t *tccp = &tcp->tccps[compno];
+                       fprintf(fd, "    comp %d {\n", compno);
+                       fprintf(fd, "      csty=%x\n", tccp->csty);
+                       fprintf(fd, "      numresx=%d, numresy=%d, numresz=%d\n", tccp->numresolution[0], tccp->numresolution[1], tccp->numresolution[2]);
+                       fprintf(fd, "      cblkw=%d, cblkh=%d, cblkl=%d\n", tccp->cblk[0], tccp->cblk[1], tccp->cblk[2]);
+                       fprintf(fd, "      cblksty=%x\n", tccp->cblksty);
+                       fprintf(fd, "      qntsty=%d\n", tccp->qntsty);
+                       fprintf(fd, "      numgbits=%d\n", tccp->numgbits);
+                       fprintf(fd, "      roishift=%d\n", tccp->roishift);
+                       fprintf(fd, "      reversible=%d\n", tccp->reversible);
+                       fprintf(fd, "      dwtidx=%d dwtidy=%d dwtidz=%d\n", tccp->dwtid[0], tccp->dwtid[1], tccp->dwtid[2]);
+                       if (tccp->atk != NULL) {
+                fprintf(fd, "      atk.index=%d\n", tccp->atk->index);
+                               fprintf(fd, "      atk.coeff_typ=%d\n", tccp->atk->coeff_typ);
+                               fprintf(fd, "      atk.filt_cat=%d\n", tccp->atk->filt_cat);
+                               fprintf(fd, "      atk.exten=%d\n", tccp->atk->exten);
+                               fprintf(fd, "      atk.minit=%d\n", tccp->atk->minit);
+                               fprintf(fd, "      atk.wt_typ=%d\n", tccp->atk->wt_typ);
+                       }
+                       fprintf(fd, "      stepsizes of bands=");
+            numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 :
+                       ( (cp->transform_format == TRF_2D_DWT) ? (tccp->numresolution[0] * 3 - 2) :
+                               (tccp->numresolution[0] * 7 - 6) - 4 *(tccp->numresolution[0] - tccp->numresolution[2]) );
+                       for (bandno = 0; bandno < numbands; bandno++) {
+                               fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,tccp->stepsizes[bandno].expn);
+                       }
+                       fprintf(fd, "\n");
+                       
+                       if (tccp->csty & J3D_CCP_CSTY_PRT) {
+                               fprintf(fd, "      prcw=");
+                               for (resno = 0; resno < tccp->numresolution[0]; resno++) {
+                                       fprintf(fd, "%d ", tccp->prctsiz[0][resno]);
+                               }
+                               fprintf(fd, "\n");
+                               fprintf(fd, "      prch=");
+                               for (resno = 0; resno < tccp->numresolution[0]; resno++) {
+                                       fprintf(fd, "%d ", tccp->prctsiz[1][resno]);
+                               }
+                               fprintf(fd, "\n");
+                               fprintf(fd, "      prcl=");
+                               for (resno = 0; resno < tccp->numresolution[0]; resno++) {
+                                       fprintf(fd, "%d ", tccp->prctsiz[2][resno]);
+                               }
+                               fprintf(fd, "\n");
+                       }
+                       fprintf(fd, "    }\n");
+               }
+               fprintf(fd, "  }\n");
+       }
+       fprintf(fd, "}\n");
+}
+
+/* ----------------------------------------------------------------------- 
+Extended capabilities
+------------------------------------------------------------------------*/
+
+static void j3d_write_cap(opj_j3d_t *j3d){
+       int len,lenp;
+
+       opj_cio_t *cio = j3d->cio;
+       cio_write(cio, J3D_MS_CAP, 2);  /* CAP */
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);
+       cio_write(cio,J3D_CAP_10, 4); 
+  if( J3D_CAP_10 )
+    {
+    cio_write(cio, 0x0, 2); 
+    }
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);         /* Lsiz */
+       cio_seek(cio, lenp + len);
+
+}
+static void j3d_read_cap(opj_j3d_t *j3d){
+       int len, Cap;
+       opj_cio_t *cio = j3d->cio;
+       /*cio_read(cio, 2);      CAP */
+       len = cio_read(cio, 2);
+       Cap = cio_read(cio, 4);
+  if(Cap) {
+    cio_read(cio, 2);
+  }
+  assert( len == 2 + 4 + 2 );
+}
+static void j3d_write_nsi(opj_j3d_t *j3d) {
+       int i;
+       int lenp, len;
+  int ndim = 3;
+
+       opj_cio_t *cio = j3d->cio;
+       opj_volume_t *volume = j3d->volume;
+       opj_cp_t *cp = j3d->cp;
+       
+       cio_write(cio, J3D_MS_NSI, 2);  /* NSI */
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);
+       cio_write(cio, ndim, 1);        /* Ndim */
+       cio_write(cio, volume->z1, 4);  /* Zsiz */
+       cio_write(cio, volume->z0, 4);  /* Z0siz */
+       cio_write(cio, cp->tdz, 4);             /* ZTsiz */
+       cio_write(cio, cp->tz0, 4);             /* ZT0siz */
+       for (i = 0; i < volume->numcomps; i++) {
+               cio_write(cio, volume->comps[i].dz, 1); /* ZRsiz_i */
+       }
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);         /* Lsiz */
+       cio_seek(cio, lenp + len);
+}
+
+static void j3d_read_nsi(opj_j3d_t *j3d) {
+  int ndim;
+       int len, i;
+       
+       opj_cio_t *cio = j3d->cio;
+       opj_volume_t *volume = j3d->volume;
+       opj_cp_t *cp = j3d->cp;
+       
+       len = cio_read(cio, 2);                 /* Lnsi */
+       ndim = cio_read(cio, 1);                        /* Ndim */
+  assert( ndim == 3 );
+       volume->z1 = cio_read(cio, 4);  /* Zsiz */
+       volume->z0 = cio_read(cio, 4);  /* Z0siz */
+       cp->tdz = cio_read(cio, 4);             /* ZTsiz */
+       cp->tz0 = cio_read(cio, 4);             /* ZT0siz */
+       for (i = 0; i < volume->numcomps; i++) {
+               volume->comps[i].dz = cio_read(cio, 1); /* ZRsiz_i */
+       }
+       
+       /*Initialization of volume*/
+       cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
+       cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
+       cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
+       cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
+       cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
+       cp->tileno_size = 0;
+       
+       for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
+               cp->tcps[i].POC = 0;
+               cp->tcps[i].numpocs = 0;
+               cp->tcps[i].first = 1;
+       }
+       
+       /* Initialization for PPM marker (Packets header)*/
+       cp->ppm = 0;
+       cp->ppm_data = NULL;
+       cp->ppm_data_first = NULL;
+       cp->ppm_previous = 0;
+       cp->ppm_store = 0;
+       
+       j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
+       for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
+               cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
+       }
+       j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));
+       j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
+       j3d->state = J3D_STATE_MH;
+       
+}
+static void j3d_write_dco(opj_j3d_t *j3d){
+       int lenp, len, i;
+       int dcotype;    
+
+       opj_cio_t *cio = j3d->cio;
+       opj_volume_t *volume = j3d->volume;
+       opj_cp_t *cp = j3d->cp;
+       
+       dcotype = 1; /* Offsets are 16bit signed integers Table A21 15444-2 */
+       cio_write(cio, J3D_MS_DCO, 2);  /* DCO */
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);
+       cio_write(cio, dcotype, 1);     
+       if (dcotype == 0) {
+               for (i = 0; i < volume->numcomps; i++) 
+                       cio_write(cio, volume->comps[i].dcoffset, 1);   /* SPdco_i */
+       } else if (dcotype == 1) {
+               for (i = 0; i < volume->numcomps; i++){ 
+                       cio_write(cio, volume->comps[i].dcoffset, 1);   /* SPdco_i */
+                       opj_event_msg(j3d->cinfo, EVT_INFO, "dcotype %d DCO %d \n",dcotype,volume->comps[i].dcoffset);
+               }
+       }
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);         /* Ldco */
+       cio_seek(cio, lenp + len);
+
+}
+static void j3d_read_dco(opj_j3d_t *j3d){
+       int len, i;
+       int dcotype;
+
+       opj_cio_t *cio = j3d->cio;
+       opj_volume_t *volume = j3d->volume;
+       opj_cp_t *cp = j3d->cp;
+       
+       len = cio_read(cio, 2);                 /* Lsiz */
+       dcotype = cio_read(cio, 1); /*offset 8bit unsigned / 16bit signed integers*/
+       if (dcotype == 0) {
+               for (i = 0; i < volume->numcomps; i++) {
+                       volume->comps[i].dcoffset = cio_read(cio, 1);
+                       if (volume->comps[i].dcoffset > 128) 
+                               volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;
+               }
+       } else if (dcotype == 1) {
+               for (i = 0; i < volume->numcomps; i++) {
+                       volume->comps[i].dcoffset = cio_read(cio, 1);
+                       if (volume->comps[i].dcoffset > 128) 
+                               volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;
+               }
+       }
+       
+}
+static void j3d_write_atk(opj_j3d_t *j3d){
+       int lenp, len, s, k;
+
+       opj_cio_t *cio = j3d->cio;
+       opj_volume_t *volume = j3d->volume;
+       opj_atk_t *atk = j3d->cp->tcps->tccps->atk;
+       
+       cio_write(cio, J3D_MS_ATK, 2);  /* ATK */
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);                               
+       cio_write(cio, atk->index + (atk->coeff_typ << 8) + (atk->filt_cat << 11) 
+               + (atk->wt_typ << 12) + (atk->minit << 13) + (atk->exten << 14), 2);                    /* Satk */
+    if (atk->wt_typ == J3D_ATK_IRR) 
+               cio_write(cio,(unsigned int) (atk->Katk * 8192.0), 1 << atk->coeff_typ);
+       cio_write(cio, atk->Natk, 1);
+       for (s = 0; s < atk->Natk; s++){
+               if (atk->filt_cat == J3D_ATK_ARB) 
+                       cio_write(cio, atk->Oatk[s], 1);
+               if (atk->wt_typ == J3D_ATK_REV){
+                       cio_write(cio, atk->Eatk[s], 1);
+                       cio_write(cio, atk->Batk[s], 1);
+               }
+               cio_write(cio, atk->LCatk[s], 1);
+               for (k = 0; k < atk->LCatk[s]; k++)
+                       cio_write(cio,(unsigned int) (atk->Aatk[s][k] * 8192.0), 1 << atk->coeff_typ);
+       }
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);         /* Latk */
+       cio_seek(cio, lenp + len);
+}
+static void j3d_read_atk(opj_j3d_t *j3d){
+       int len, i, Satk, k;
+       
+       opj_cio_t *cio = j3d->cio;
+       opj_volume_t *volume = j3d->volume;
+       opj_cp_t *cp = j3d->cp;
+       opj_atk_t *atk = cp->tcps->tccps->atk; 
+       
+       len = cio_read(cio, 2);                 /* Latk */
+       Satk = cio_read(cio, 2); 
+       atk->index = Satk & 0x00ff;
+       atk->coeff_typ = Satk >> 8 & 0x0007;
+       atk->filt_cat = Satk >> 11 & 0x0001;
+       atk->wt_typ = Satk >> 12  & 0x0001;
+       atk->minit = Satk >> 13 & 0x0001;
+       atk->exten = Satk >> 14 & 0x0001;
+    if (atk->wt_typ == J3D_ATK_IRR) 
+               atk->Katk = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);
+       atk->Natk = cio_read(cio, 1);
+       for (i = 0; i < atk->Natk; i++) {
+               if (atk->filt_cat == J3D_ATK_ARB) 
+                       atk->Oatk[i] = cio_read(cio, 1);
+               if (atk->wt_typ == J3D_ATK_REV){
+                       atk->Eatk[i] = cio_read(cio, 1);
+                       atk->Batk[i] = cio_read(cio, 1);
+               }
+               atk->LCatk[i] = cio_read(cio, 1);
+               for (k = 0; k < atk->LCatk[i]; k++)
+                       atk->Aatk[i][k] = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);
+       }
+}
+static void j3d_write_cbd(opj_j3d_t *j3d){
+}
+static void j3d_read_cbd(opj_j3d_t *j3d){
+}
+static void j3d_write_mct(opj_j3d_t *j3d){
+}
+static void j3d_read_mct(opj_j3d_t *j3d){
+}
+static void j3d_write_mcc(opj_j3d_t *j3d){
+}
+static void j3d_read_mcc(opj_j3d_t *j3d){
+}
+static void j3d_write_mco(opj_j3d_t *j3d){
+}
+static void j3d_read_mco(opj_j3d_t *j3d){
+}
+static void j3d_write_nlt(opj_j3d_t *j3d){
+}
+static void j3d_read_nlt(opj_j3d_t *j3d){
+}
+/* ----------------------------------------------------------------------- 
+15444-1 codestream syntax
+------------------------------------------------------------------------*/
+static void j3d_write_soc(opj_j3d_t *j3d) {
+       opj_cio_t *cio = j3d->cio;
+       cio_write(cio, J3D_MS_SOC, 2);
+}
+
+static void j3d_read_soc(opj_j3d_t *j3d) {
+       j3d->state = J3D_STATE_MHSIZ;
+}
+
+static void j3d_write_siz(opj_j3d_t *j3d) {
+       int i;
+       int lenp, len;
+       int Rsiz;
+
+       opj_cio_t *cio = j3d->cio;
+       opj_volume_t *volume = j3d->volume;
+       opj_cp_t *cp = j3d->cp;
+       
+       cio_write(cio, J3D_MS_SIZ, 2);  /* SIZ */
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);
+       /*cio_write(cio, 0, 2);*/                       /* Rsiz (capabilities of 15444-1 only) */
+       Rsiz = J3D_RSIZ_DCO | J3D_RSIZ_ATK; /** | J3D_RSIZ_MCT | J3D_RSIZ_NONLT (not implemented yet)*/
+       cio_write(cio, Rsiz, 2); /* capabilities of WDv5.2*/
+       cio_write(cio, volume->x1, 4);  /* Xsiz */
+       cio_write(cio, volume->y1, 4);  /* Ysiz */
+       cio_write(cio, volume->x0, 4);  /* X0siz */
+       cio_write(cio, volume->y0, 4);  /* Y0siz */
+       cio_write(cio, cp->tdx, 4);             /* XTsiz */
+       cio_write(cio, cp->tdy, 4);             /* YTsiz */
+       cio_write(cio, cp->tx0, 4);             /* XT0siz */
+       cio_write(cio, cp->ty0, 4);             /* YT0siz */
+       cio_write(cio, volume->numcomps, 2);    /* Csiz */
+       for (i = 0; i < volume->numcomps; i++) {
+               cio_write(cio, volume->comps[i].prec - 1 + (volume->comps[i].sgnd << 7), 1);    /* Ssiz_i */
+               cio_write(cio, volume->comps[i].dx, 1); /* XRsiz_i */
+               cio_write(cio, volume->comps[i].dy, 1); /* YRsiz_i */
+       }
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);         /* Lsiz */
+       cio_seek(cio, lenp + len);
+}
+
+static void j3d_read_siz(opj_j3d_t *j3d) {
+       int len, i;
+       
+       opj_cio_t *cio = j3d->cio;
+       opj_volume_t *volume = j3d->volume;
+       opj_cp_t *cp = j3d->cp;
+       
+       len = cio_read(cio, 2);                 /* Lsiz */
+       cp->rsiz = cio_read(cio, 2);    /* Rsiz (capabilities) */
+       volume->x1 = cio_read(cio, 4);  /* Xsiz */
+       volume->y1 = cio_read(cio, 4);  /* Ysiz */
+       volume->x0 = cio_read(cio, 4);  /* X0siz */
+       volume->y0 = cio_read(cio, 4);  /* Y0siz */
+       cp->tdx = cio_read(cio, 4);             /* XTsiz */
+       cp->tdy = cio_read(cio, 4);             /* YTsiz */
+       cp->tx0 = cio_read(cio, 4);             /* XT0siz */
+       cp->ty0 = cio_read(cio, 4);             /* YT0siz */
+       
+       volume->numcomps = cio_read(cio, 2);    /* Csiz */
+       volume->comps = (opj_volume_comp_t *) opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t));
+       for (i = 0; i < volume->numcomps; i++) {
+               int tmp, j;
+               tmp = cio_read(cio, 1);         /* Ssiz_i */
+               volume->comps[i].prec = (tmp & 0x7f) + 1;
+               volume->comps[i].sgnd = tmp >> 7;
+               volume->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
+               volume->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
+               for (j = 0; j < 3; j++) {
+                       volume->comps[i].resno_decoded[j] = 0;          /* number of resolution decoded */
+                       volume->comps[i].factor[j] = 0;         /* reducing factor per component */
+               }
+       }
+
+       if (j3d->cinfo->codec_format == CODEC_J2K){
+               volume->z1 = 1;
+               volume->z0 = 0;
+               volume->numslices = 1;
+               cp->tdz = 1;
+               cp->tz0 = 0;
+               for (i = 0; i < volume->numcomps; i++) 
+                       volume->comps[i].dz = 1;
+
+               /*Initialization of volume*/
+               cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
+               cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
+               cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
+               cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
+               cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
+               cp->tileno_size = 0;
+               
+               for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
+                       cp->tcps[i].POC = 0;
+                       cp->tcps[i].numpocs = 0;
+                       cp->tcps[i].first = 1;
+               }
+               
+               /* Initialization for PPM marker (Packets header)*/
+               cp->ppm = 0;
+               cp->ppm_data = NULL;
+               cp->ppm_data_first = NULL;
+               cp->ppm_previous = 0;
+               cp->ppm_store = 0;
+               
+               j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
+               for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
+                       cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
+               }
+               j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));
+               j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
+               j3d->state = J3D_STATE_MH;
+       }
+}
+
+
+
+static void j3d_write_com(opj_j3d_t *j3d) {
+       unsigned int i;
+       int lenp, len;
+
+       opj_cio_t *cio = j3d->cio;
+
+       cio_write(cio, J3D_MS_COM, 2);
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);
+       cio_write(cio, 1, 2);
+       /*opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);*/
+       if (j3d->cp->comment != NULL) {
+               char *comment = j3d->cp->comment;
+               for (i = 0; i < strlen(comment); i++) {
+            cio_write(cio, comment[i], 1);
+               }
+       }
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);
+       cio_seek(cio, lenp + len);
+}
+
+static void j3d_read_com(opj_j3d_t *j3d) {
+       int len;
+       opj_cio_t *cio = j3d->cio;
+
+       len = cio_read(cio, 2);
+       cio_read(cio, 2); // read registration
+       
+       /*opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);*/
+
+       cio_skip(cio, len - 4);  /*posible comments*/
+}
+
+static void j3d_write_cox(opj_j3d_t *j3d, int compno) {
+       int i;
+  int shift = 2;
+
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
+       opj_tccp_t *tccp = &tcp->tccps[compno];
+       opj_cio_t *cio = j3d->cio;
+       
+       cio_write(cio, tccp->numresolution[0] - 1, 1);  /* SPcox (D) No of decomposition levels in x-axis*/
+       if (j3d->cinfo->codec_format == CODEC_J3D) {
+               cio_write(cio, tccp->numresolution[1] - 1, 1);  /* SPcox (E) No of decomposition levels in y-axis*/
+               cio_write(cio, tccp->numresolution[2] - 1, 1);  /* SPcox (F) No of decomposition levels in z-axis*/
+       }
+       if (j3d->cinfo->codec_format == CODEC_J3D) {
+    /* Table A.7 */
+    shift = 0;
+  }
+       /* (cblkw - 2) + (cblkh - 2) + (cblkl - 2) <= 18*/
+       cio_write(cio, tccp->cblk[0] - shift, 1);                               /* SPcox (G) Cblk width entre 10 y 2 (8 y 0)*/
+       cio_write(cio, tccp->cblk[1] - shift, 1);                               /* SPcox (H) Cblk height*/
+       if (j3d->cinfo->codec_format == CODEC_J3D) {
+               cio_write(cio, tccp->cblk[2] - shift, 1);                       /* SPcox (I) Cblk depth*/
+       }
+       cio_write(cio, tccp->cblksty, 1);                               /* SPcox (J) Cblk style*/
+       cio_write(cio, tccp->dwtid[0], 1);                              /* SPcox (K) WT in x-axis 15444-2 Table A10*/
+       if (j3d->cinfo->codec_format == CODEC_J3D) {
+               cio_write(cio, tccp->dwtid[1], 1);                              /* SPcox (L) WT in y-axis 15444-2 Table A10*/
+               cio_write(cio, tccp->dwtid[2], 1);                              /* SPcox (M) WT in z-axis 15444-2 Table A10*/
+       }
+       
+       if (tccp->csty & J3D_CCP_CSTY_PRT) {
+               for (i = 0; i < tccp->numresolution[0]; i++) {
+                       if (i < tccp->numresolution[2])
+                cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4) + (tccp->prctsiz[2][i] << 8), 2);      /* SPcox (N_i) Table A9*/
+                       else
+                               if (j3d->cinfo->codec_format == CODEC_J3D) 
+                    cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 2);       /* SPcox (N_i) Table A9*/
+                               else
+                    cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 1);       /* SPcox (N_i) Table A9*/               }
+       }
+}
+
+static void j3d_read_cox(opj_j3d_t *j3d, int compno) {
+       int i;
+  int shift = 2;
+
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
+       opj_tccp_t *tccp = &tcp->tccps[compno];
+       opj_cio_t *cio = j3d->cio;
+
+       tccp->numresolution[0] = cio_read(cio, 1) + 1;  /* SPcox (D) No of decomposition levels in x-axis*/
+       if (j3d->cinfo->codec_format == CODEC_J3D) {
+               tccp->numresolution[1] = cio_read(cio, 1) + 1;  /* SPcox (E) No of decomposition levels in y-axis*/
+               tccp->numresolution[2] = cio_read(cio, 1) + 1;  /* SPcox (F) No of decomposition levels in z-axis*/
+       }else if (j3d->cinfo->codec_format == CODEC_J2K) {
+               tccp->numresolution[1] = tccp->numresolution[0];        
+               tccp->numresolution[2] = 1;                                                     
+       }
+       /* check the reduce value */
+       cp->reduce[0] = int_min((tccp->numresolution[0])-1, cp->reduce[0]);
+       cp->reduce[1] = int_min((tccp->numresolution[1])-1, cp->reduce[1]);
+       cp->reduce[2] = int_min((tccp->numresolution[2])-1, cp->reduce[2]);
+       
+  if (j3d->cinfo->codec_format == CODEC_J3D) {
+    /* Table A.7 */
+    shift = 0;
+  }
+       tccp->cblk[0] = cio_read(cio, 1) + shift;       /* SPcox (G) */
+       tccp->cblk[1] = cio_read(cio, 1) + shift;       /* SPcox (H) */
+       if (j3d->cinfo->codec_format == CODEC_J3D)
+               tccp->cblk[2] = cio_read(cio, 1) + shift;       /* SPcox (I) */
+       else
+               tccp->cblk[2] = tccp->cblk[0];
+
+       tccp->cblksty = cio_read(cio, 1);       /* SPcox (J) */
+       tccp->dwtid[0] = cio_read(cio, 1);      /* SPcox (K) */
+       if (j3d->cinfo->codec_format == CODEC_J3D) {
+               tccp->dwtid[1] = cio_read(cio, 1);      /* SPcox (L) */
+               tccp->dwtid[2] = cio_read(cio, 1);      /* SPcox (M) */
+       }else{
+               tccp->dwtid[1] = tccp->dwtid[0];        /* SPcox (L) */
+               tccp->dwtid[2] = tccp->dwtid[0];        /* SPcox (M) */
+       }
+       tccp->reversible = (tccp->dwtid[0]>=1 && tccp->dwtid[1]>=1 && tccp->dwtid[2]>=1); /*TODO: only valid for irreversible 9x7 WTs*/
+       if (tccp->csty & J3D_CP_CSTY_PRT) {
+               for (i = 0; i < tccp->numresolution[0]; i++) {
+                       int tmp = cio_read(cio, 2);     /* SPcox (N_i) */
+                       tccp->prctsiz[0][i] = tmp & 0xf;
+                       tccp->prctsiz[1][i] = tmp >> 4;
+                       tccp->prctsiz[2][i] = tmp >> 8;
+               }
+       }
+}
+
+static void j3d_write_cod(opj_j3d_t *j3d) {
+       opj_cp_t *cp = NULL;
+       opj_tcp_t *tcp = NULL;
+       int lenp, len;
+
+       opj_cio_t *cio = j3d->cio;
+       
+       cio_write(cio, J3D_MS_COD, 2);  /* COD */
+       
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);
+       
+       cp = j3d->cp;
+       tcp = &cp->tcps[j3d->curtileno];
+
+       /* Scod : Table A-4*/
+       cio_write(cio, tcp->csty, 1);           /* Scod : Coding style parameters */
+       /* SGcod : Table A-5*/
+       cio_write(cio, tcp->prg, 1);            /* SGcod (A) : Progression order */
+       cio_write(cio, tcp->numlayers, 2);      /* SGcod (B) : No of layers */
+       cio_write(cio, tcp->mct, 1);            /* SGcod (C) : Multiple component transformation usage */
+       /* SPcod : Table A-6*/
+       j3d_write_cox(j3d, 0);                          
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);         /* Lcod */
+       cio_seek(cio, lenp + len);
+}
+
+static void j3d_read_cod(opj_j3d_t *j3d) {
+       int len, i, pos;
+       
+       opj_cio_t *cio = j3d->cio;
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
+       opj_volume_t *volume = j3d->volume;
+
+       /* Lcod */
+       len = cio_read(cio, 2);                         
+       /* Scod : Table A-4*/
+       tcp->csty = cio_read(cio, 1);           
+       /* SGcod : Table A-5*/
+       tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);
+       tcp->numlayers = cio_read(cio, 2);      
+       tcp->mct = cio_read(cio, 1);            
+       
+       pos = cio_tell(cio);
+       for (i = 0; i < volume->numcomps; i++) {
+               tcp->tccps[i].csty = tcp->csty & J3D_CP_CSTY_PRT;
+               cio_seek(cio, pos);
+               j3d_read_cox(j3d, i);
+       }
+}
+
+static void j3d_write_coc(opj_j3d_t *j3d, int compno) {
+       int lenp, len;
+
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
+       opj_volume_t *volume = j3d->volume;
+       opj_cio_t *cio = j3d->cio;
+       
+       cio_write(cio, J3D_MS_COC, 2);  /* COC */
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);
+       cio_write(cio, compno, volume->numcomps <= 256 ? 1 : 2);        /* Ccoc */
+       cio_write(cio, tcp->tccps[compno].csty, 1);                                     /* Scoc */
+       
+       j3d_write_cox(j3d, compno);
+       
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);                 /* Lcoc */
+       cio_seek(cio, lenp + len);
+}
+
+static void j3d_read_coc(opj_j3d_t *j3d) {
+       int len, compno;
+
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
+       opj_volume_t *volume = j3d->volume;
+       opj_cio_t *cio = j3d->cio;
+       
+       len = cio_read(cio, 2);         /* Lcoc */
+       compno = cio_read(cio, volume->numcomps <= 256 ? 1 : 2);        /* Ccoc */
+       tcp->tccps[compno].csty = cio_read(cio, 1);     /* Scoc */
+       j3d_read_cox(j3d, compno);
+}
+
+static void j3d_write_qcx(opj_j3d_t *j3d, int compno) {
+       int bandno, numbands;
+       int expn, mant;
+       
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
+       opj_tccp_t *tccp = &tcp->tccps[compno];
+       opj_cio_t *cio = j3d->cio;
+       
+       cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);        /* Sqcx : Table A28 de 15444-1*/
+       
+       if (j3d->cinfo->codec_format == CODEC_J2K)
+        numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolution[0] * 3 - 2; 
+       else if (j3d->cinfo->codec_format == CODEC_J3D) {
+               int diff = tccp->numresolution[0] - tccp->numresolution[2];
+        numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : (tccp->numresolution[0] * 7 - 6) - 4 *diff; /* SIQNT vs. SEQNT */
+       }
+       
+       for (bandno = 0; bandno < numbands; bandno++) {
+               expn = tccp->stepsizes[bandno].expn;
+               mant = tccp->stepsizes[bandno].mant;
+               
+               if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
+                       cio_write(cio, expn << 3, 1);   /* SPqcx_i */
+               } else {
+                       cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
+               }
+       }
+}
+
+static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len) {
+       int tmp;
+       int bandno, numbands;
+
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
+       opj_tccp_t *tccp = &tcp->tccps[compno];
+       opj_cio_t *cio = j3d->cio;
+
+       tmp = cio_read(cio, 1);         /* Sqcx */
+       tccp->qntsty = tmp & 0x1f;
+       tccp->numgbits = tmp >> 5;
+
+       /*Numbands = 1                          si SIQNT
+                            len - 1            si NOQNT
+                                (len - 1) / 2  si SEQNT */
+       numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : ((tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
+
+       for (bandno = 0; bandno < numbands; bandno++) {
+               int expn, mant;
+               if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
+                       expn = cio_read(cio, 1) >> 3;   /* SPqcx_i */
+                       mant = 0;
+               } else {
+                       tmp = cio_read(cio, 2);                 /* SPqcx_i */
+                       expn = tmp >> 11;
+                       mant = tmp & 0x7ff;
+               }
+               tccp->stepsizes[bandno].expn = expn;
+               tccp->stepsizes[bandno].mant = mant;
+       }
+       
+       /* Add Antonin : if scalar_derived -> compute other stepsizes */
+       if (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) {
+               for (bandno = 1; bandno < J3D_MAXBANDS; bandno++) {
+                       int numbands = (cp->transform_format==TRF_2D_DWT) ? 3 : 7;
+                       tccp->stepsizes[bandno].expn = tccp->stepsizes[0].expn - ((bandno - 1) / numbands);
+                       tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
+               }
+       }
+       /* ddA */
+}
+
+static void j3d_write_qcd(opj_j3d_t *j3d) {
+       int lenp, len;
+
+       opj_cio_t *cio = j3d->cio;
+       
+       cio_write(cio, J3D_MS_QCD, 2);  /* QCD */
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);
+       j3d_write_qcx(j3d, 0);                  /* Sqcd*/
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);                 /* Lqcd */
+       cio_seek(cio, lenp + len);
+}
+
+static void j3d_read_qcd(opj_j3d_t *j3d) {
+       int len, i, pos;
+
+       opj_cio_t *cio = j3d->cio;
+       opj_volume_t *volume = j3d->volume;
+       
+       len = cio_read(cio, 2);         /* Lqcd */
+       pos = cio_tell(cio);
+       for (i = 0; i < volume->numcomps; i++) {
+               cio_seek(cio, pos);
+               j3d_read_qcx(j3d, i, len - 2);
+       }
+}
+
+static void j3d_write_qcc(opj_j3d_t *j3d, int compno) {
+       int lenp, len;
+
+       opj_cio_t *cio = j3d->cio;
+       
+       cio_write(cio, J3D_MS_QCC, 2);  /* QCC */
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);
+       cio_write(cio, compno, j3d->volume->numcomps <= 256 ? 1 : 2);   /* Cqcc */
+       j3d_write_qcx(j3d, compno);
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);                 /* Lqcc */
+       cio_seek(cio, lenp + len);
+}
+
+static void j3d_read_qcc(opj_j3d_t *j3d) {
+       int len, compno;
+       int numcomp = j3d->volume->numcomps;
+       opj_cio_t *cio = j3d->cio;
+       
+       len = cio_read(cio, 2); /* Lqcc */
+       compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
+       j3d_read_qcx(j3d, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
+}
+
+static void j3d_write_poc(opj_j3d_t *j3d) {
+       int len, numpchgs, i;
+
+       int numcomps = j3d->volume->numcomps;
+       
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
+       opj_tccp_t *tccp = &tcp->tccps[0];
+       opj_cio_t *cio = j3d->cio;
+
+       numpchgs = tcp->numpocs;
+       cio_write(cio, J3D_MS_POC, 2);  /* POC  */
+       len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
+       cio_write(cio, len, 2);         /* Lpoc */
+       for (i = 0; i < numpchgs; i++) {
+               opj_poc_t *poc = &tcp->pocs[i];
+               cio_write(cio, poc->resno0, 1); /* RSpoc_i */
+               cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));        /* CSpoc_i */
+               cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
+               poc->layno1 = int_min(poc->layno1, tcp->numlayers);
+               cio_write(cio, poc->resno1, 1); /* REpoc_i */
+               poc->resno1 = int_min(poc->resno1, tccp->numresolution[0]);
+               cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));        /* CEpoc_i */
+               poc->compno1 = int_min(poc->compno1, numcomps);
+               cio_write(cio, poc->prg, 1);    /* Ppoc_i */
+       }
+}
+
+static void j3d_read_poc(opj_j3d_t *j3d) {
+       int len, numpchgs, i, old_poc;
+
+       int numcomps = j3d->volume->numcomps;
+       
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
+       opj_tccp_t *tccp = &tcp->tccps[0];
+       opj_cio_t *cio = j3d->cio;
+       
+       old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
+       tcp->POC = 1;
+       len = cio_read(cio, 2);         /* Lpoc */
+       numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
+       
+       for (i = old_poc; i < numpchgs + old_poc; i++) {
+               opj_poc_t *poc;
+               poc = &tcp->pocs[i];
+               poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
+               poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
+               poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
+               poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolution[0]); /* REpoc_i */
+               poc->compno1 = int_min(
+                       cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */
+               poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */
+       }
+       
+       tcp->numpocs = numpchgs + old_poc - 1;
+}
+
+static void j3d_read_crg(opj_j3d_t *j3d) {
+       int len, i, Xcrg_i, Ycrg_i, Zcrg_i;
+       
+       opj_cio_t *cio = j3d->cio;
+       int numcomps = j3d->volume->numcomps;
+       
+       len = cio_read(cio, 2);                 /* Lcrg */
+       for (i = 0; i < numcomps; i++) {
+               Xcrg_i = cio_read(cio, 2);      /* Xcrg_i */
+               Ycrg_i = cio_read(cio, 2);      /* Ycrg_i */
+               Zcrg_i = cio_read(cio, 2);      /* Zcrg_i */
+       }
+}
+
+static void j3d_read_tlm(opj_j3d_t *j3d) {
+       int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
+       long int Ttlm_i, Ptlm_i;
+
+       opj_cio_t *cio = j3d->cio;
+       
+       len = cio_read(cio, 2);         /* Ltlm */
+       Ztlm = cio_read(cio, 1);        /* Ztlm */
+       Stlm = cio_read(cio, 1);        /* Stlm */
+       ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
+       SP = (Stlm >> 6) & 0x01;
+       tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
+       for (i = 0; i < tile_tlm; i++) {
+               Ttlm_i = cio_read(cio, ST);     /* Ttlm_i */
+               Ptlm_i = cio_read(cio, SP ? 4 : 2);     /* Ptlm_i */
+       }
+}
+
+static void j3d_read_plm(opj_j3d_t *j3d) {
+       int len, i, Zplm, Nplm, add, packet_len = 0;
+       
+       opj_cio_t *cio = j3d->cio;
+
+       len = cio_read(cio, 2);         /* Lplm */
+       Zplm = cio_read(cio, 1);        /* Zplm */
+       len -= 3;
+       while (len > 0) {
+               Nplm = cio_read(cio, 4);                /* Nplm */
+               len -= 4;
+               for (i = Nplm; i > 0; i--) {
+                       add = cio_read(cio, 1);
+                       len--;
+                       packet_len = (packet_len << 7) + add;   /* Iplm_ij */
+                       if ((add & 0x80) == 0) {
+                               /* New packet */
+                               packet_len = 0;
+                       }
+                       if (len <= 0)
+                               break;
+               }
+       }
+}
+
+static void j3d_read_plt(opj_j3d_t *j3d) {
+       int len, i, Zplt, packet_len = 0, add;
+       
+       opj_cio_t *cio = j3d->cio;
+       
+       len = cio_read(cio, 2);         /* Lplt */
+       Zplt = cio_read(cio, 1);        /* Zplt */
+       for (i = len - 3; i > 0; i--) {
+               add = cio_read(cio, 1);
+               packet_len = (packet_len << 7) + add;   /* Iplt_i */
+               if ((add & 0x80) == 0) {
+                       /* New packet */
+                       packet_len = 0;
+               }
+       }
+}
+
+static void j3d_read_ppm(opj_j3d_t *j3d) {
+       int len, Z_ppm, i, j;
+       int N_ppm;
+
+       opj_cp_t *cp = j3d->cp;
+       opj_cio_t *cio = j3d->cio;
+       
+       len = cio_read(cio, 2);
+       cp->ppm = 1;
+       
+       Z_ppm = cio_read(cio, 1);       /* Z_ppm */
+       len -= 3;
+       while (len > 0) {
+               if (cp->ppm_previous == 0) {
+                       N_ppm = cio_read(cio, 4);       /* N_ppm */
+                       len -= 4;
+               } else {
+                       N_ppm = cp->ppm_previous;
+               }
+               j = cp->ppm_store;
+               if (Z_ppm == 0) {       /* First PPM marker */
+                       cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
+                       cp->ppm_data_first = cp->ppm_data;
+                       cp->ppm_len = N_ppm;
+               } else {                        /* NON-first PPM marker */
+                       cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +     cp->ppm_store) * sizeof(unsigned char));
+                       cp->ppm_data_first = cp->ppm_data;
+                       cp->ppm_len = N_ppm + cp->ppm_store;
+               }
+               for (i = N_ppm; i > 0; i--) {   /* Read packet header */
+                       cp->ppm_data[j] = cio_read(cio, 1);
+                       j++;
+                       len--;
+                       if (len == 0)
+                               break;                  /* Case of non-finished packet header in present marker but finished in next one */
+               }
+               cp->ppm_previous = i - 1;
+               cp->ppm_store = j;
+       }
+}
+
+static void j3d_read_ppt(opj_j3d_t *j3d) {
+       int len, Z_ppt, i, j = 0;
+
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = cp->tcps + j3d->curtileno;
+       opj_cio_t *cio = j3d->cio;
+
+       len = cio_read(cio, 2);
+       Z_ppt = cio_read(cio, 1);
+       tcp->ppt = 1;
+       if (Z_ppt == 0) {               /* First PPT marker */
+               tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
+               tcp->ppt_data_first = tcp->ppt_data;
+               tcp->ppt_store = 0;
+               tcp->ppt_len = len - 3;
+       } else {                        /* NON-first PPT marker */
+               tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
+               tcp->ppt_data_first = tcp->ppt_data;
+               tcp->ppt_len = len - 3 + tcp->ppt_store;
+       }
+       j = tcp->ppt_store;
+       for (i = len - 3; i > 0; i--) {
+               tcp->ppt_data[j] = cio_read(cio, 1);
+               j++;
+       }
+       tcp->ppt_store = j;
+}
+
+static void j3d_write_sot(opj_j3d_t *j3d) {
+       int lenp, len;
+
+       opj_cio_t *cio = j3d->cio;
+
+       j3d->sot_start = cio_tell(cio);
+       cio_write(cio, J3D_MS_SOT, 2);          /* SOT */
+       lenp = cio_tell(cio);
+       cio_skip(cio, 2);                                       /* Lsot (further) */
+       cio_write(cio, j3d->curtileno, 2);      /* Isot */
+       cio_skip(cio, 4);                                       /* Psot (further in j3d_write_sod) */
+       cio_write(cio, 0, 1);                           /* TPsot */
+       cio_write(cio, 1, 1);                           /* TNsot (no of tile-parts of this tile in this codestream)*/
+       len = cio_tell(cio) - lenp;
+       cio_seek(cio, lenp);
+       cio_write(cio, len, 2);                         /* Lsot */
+       cio_seek(cio, lenp + len);
+}
+
+static void j3d_read_sot(opj_j3d_t *j3d) {
+       int len, tileno, totlen, partno, numparts, i;
+       opj_tcp_t *tcp = NULL;
+       char status = 0;
+
+       opj_cp_t *cp = j3d->cp;
+       opj_cio_t *cio = j3d->cio;
+       
+       len = cio_read(cio, 2);
+       tileno = cio_read(cio, 2);
+       
+       if (cp->tileno_size == 0) {
+               cp->tileno[cp->tileno_size] = tileno;
+               cp->tileno_size++;
+       } else {
+               i = 0;
+               while (i < cp->tileno_size && status == 0) {
+                       status = cp->tileno[i] == tileno ? 1 : 0;
+                       i++;
+               }
+               if (status == 0) {
+                       cp->tileno[cp->tileno_size] = tileno;
+                       cp->tileno_size++;
+               }
+       }
+       
+       totlen = cio_read(cio, 4);
+       if (!totlen)
+               totlen = cio_numbytesleft(cio) + 8;
+       
+       partno = cio_read(cio, 1);
+       numparts = cio_read(cio, 1);
+       
+       j3d->curtileno = tileno;
+       j3d->eot = cio_getbp(cio) - 12 + totlen;
+       j3d->state = J3D_STATE_TPH;
+       tcp = &cp->tcps[j3d->curtileno];
+       
+       if (tcp->first == 1) {
+               
+               /* Initialization PPT */
+               opj_tccp_t *tmp = tcp->tccps;
+               memcpy(tcp, j3d->default_tcp, sizeof(opj_tcp_t));
+               tcp->ppt = 0;
+               tcp->ppt_data = NULL;
+               tcp->ppt_data_first = NULL;
+               tcp->tccps = tmp;
+
+               for (i = 0; i < j3d->volume->numcomps; i++) {
+                       tcp->tccps[i] = j3d->default_tcp->tccps[i];
+               }
+               cp->tcps[j3d->curtileno].first = 0;
+       }
+}
+
+static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder) {
+       int l, layno;
+       int totlen;
+       opj_tcp_t *tcp = NULL;
+       opj_volume_info_t *volume_info = NULL;
+       
+       opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
+       opj_cp_t *cp = j3d->cp;
+       opj_cio_t *cio = j3d->cio;
+       
+       cio_write(cio, J3D_MS_SOD, 2);
+       if (j3d->curtileno == 0) {
+               j3d->sod_start = cio_tell(cio) + j3d->pos_correction;
+       }
+       
+       /* INDEX >> */
+       volume_info = j3d->volume_info;
+       if (volume_info && volume_info->index_on) {
+               volume_info->tile[j3d->curtileno].end_header = cio_tell(cio) + j3d->pos_correction - 1;
+       }
+       /* << INDEX */
+       
+       tcp = &cp->tcps[j3d->curtileno];
+       for (layno = 0; layno < tcp->numlayers; layno++) {
+               tcp->rates[layno] -= tcp->rates[layno] ? (j3d->sod_start / (cp->th * cp->tw * cp->tl)) : 0;
+       }
+       
+       if(volume_info) {
+               volume_info->num = 0;
+       }
+
+       l = tcd_encode_tile(tcd, j3d->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, volume_info);
+       
+       /* Writing Psot in SOT marker */
+       totlen = cio_tell(cio) + l - j3d->sot_start;
+       cio_seek(cio, j3d->sot_start + 6);
+       cio_write(cio, totlen, 4);
+       cio_seek(cio, j3d->sot_start + totlen);
+}
+
+static void j3d_read_sod(opj_j3d_t *j3d) {
+       int len, truncate = 0, i;
+       unsigned char *data = NULL, *data_ptr = NULL;
+
+       opj_cio_t *cio = j3d->cio;
+       int curtileno = j3d->curtileno;
+       
+       len = int_min(j3d->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
+       
+       if (len == cio_numbytesleft(cio) + 1) {
+               truncate = 1;           /* Case of a truncate codestream */
+       }
+       
+       data = (unsigned char *) opj_malloc((j3d->tile_len[curtileno] + len) * sizeof(unsigned char));
+
+       for (i = 0; i < j3d->tile_len[curtileno]; i++) {
+               data[i] = j3d->tile_data[curtileno][i];
+       }
+
+       data_ptr = data + j3d->tile_len[curtileno];
+       for (i = 0; i < len; i++) {
+               data_ptr[i] = cio_read(cio, 1);
+       }
+       
+       j3d->tile_len[curtileno] += len;
+       opj_free(j3d->tile_data[curtileno]);
+       j3d->tile_data[curtileno] = data;
+       
+       if (!truncate) {
+               j3d->state = J3D_STATE_TPHSOT;
+       } else {
+               j3d->state = J3D_STATE_NEOC;    /* RAJOUTE !! */
+       }
+}
+
+static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno) {
+       
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = &cp->tcps[tileno];
+       opj_cio_t *cio = j3d->cio;
+       int numcomps = j3d->volume->numcomps;
+       
+       cio_write(cio, J3D_MS_RGN, 2);                                          /* RGN  */
+       cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */
+       cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */
+       cio_write(cio, 0, 1);                                                           /* Srgn */
+       cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */
+}
+
+static void j3d_read_rgn(opj_j3d_t *j3d) {
+       int len, compno, roisty;
+
+       opj_cp_t *cp = j3d->cp;
+       opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
+       opj_cio_t *cio = j3d->cio;
+       int numcomps = j3d->volume->numcomps;
+
+       len = cio_read(cio, 2);                                                                         /* Lrgn */
+       compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */
+       roisty = cio_read(cio, 1);                                                                      /* Srgn */
+       tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */
+}
+
+static void j3d_write_eoc(opj_j3d_t *j3d) {
+       opj_cio_t *cio = j3d->cio;
+       /* opj_event_msg(j3d->cinfo, "%.8x: EOC\n", cio_tell(cio) + j3d->pos_correction); */
+       cio_write(cio, J3D_MS_EOC, 2);
+}
+
+static void j3d_read_eoc(opj_j3d_t *j3d) {
+       int i, tileno;
+
+#ifndef NO_PACKETS_DECODING  
+       opj_tcd_t *tcd = tcd_create(j3d->cinfo);
+    tcd_malloc_decode(tcd, j3d->volume, j3d->cp);
+       /*j3d_dump_volume(stdout, tcd->volume);
+       j3d_dump_cp(stdout, tcd->volume, tcd->cp);*/
+       for (i = 0; i < j3d->cp->tileno_size; i++) {
+               tileno = j3d->cp->tileno[i];
+               /*opj_event_msg(j3d->cinfo, EVT_INFO, "tcd_decode_tile \n");*/
+               tcd_decode_tile(tcd, j3d->tile_data[tileno], j3d->tile_len[tileno], tileno);
+               opj_free(j3d->tile_data[tileno]);
+               j3d->tile_data[tileno] = NULL;
+       }
+       tcd_free_decode(tcd);
+       tcd_destroy(tcd);
+#else 
+       for (i = 0; i < j3d->cp->tileno_size; i++) {
+               tileno = j3d->cp->tileno[i];
+               opj_free(j3d->tile_data[tileno]);
+               j3d->tile_data[tileno] = NULL;
+       }
+#endif
+       
+       j3d->state = J3D_STATE_MT;
+}
+
+static void j3d_read_unk(opj_j3d_t *j3d) {
+       opj_event_msg(j3d->cinfo, EVT_WARNING, "Unknown marker\n");
+}
+
+static opj_atk_t atk_info_wt[] = {
+       {0, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1.230174104, 4, {0}, {0}, {0}, {1,1,1,1}, {-1.586134342059924, -0.052980118572961, 0.882911075530934, 0.443506852043971}},/* WT 9-7 IRR*/
+       {1, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {1,2}, {1,2}, {1,1}, {-1.0,1.0}},/* WT 5-3 REV*/
+       {2, 0, J3D_ATK_ARB, J3D_ATK_REV, 0, J3D_ATK_CON, 0, 2, {0,0}, {0,1}, {0,1}, {1,1}, {{-1.0},{1.0}}}, /* WT 2-2 REV*/
+       {3, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-1}, {0,1,2}, {0,1,2}, {1,1,3}, {{-1.0},{1.0},{1.0,0.0,-1.0}}}, /* WT 2-6 REV*/
+       {4, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-2}, {0,1,6}, {0,1,32}, {1,1,5}, {{-1},{1},{-3.0,22.0,0.0,-22.0,3.0}}}, /* WT 2-10 REV*/
+       {5, 1, J3D_ATK_ARB, J3D_ATK_IRR, 1, J3D_ATK_WS, 1, 7, {0}, {0}, {0}, {1,1,2,1,2,1,3},{{-1},{1.58613434206},{-0.460348209828, 0.460348209828},{0.25},{0.374213867768,-0.374213867768},{-1.33613434206},{0.29306717103,0,-0.29306717103}}}, /* WT 6-10 IRR*/
+       {6, 1, J3D_ATK_ARB, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 11, {0}, {0}, {0}, {1,1,2,1,2,1,2,1,2,1,5},{{-1},{0,99715069105},{-1.00573127827, 1.00573127827},{-0.27040357631},{2.20509972343, -2.20509972343},{0.08059995736},
+               {-1.62682532350, 1.62682532350},{0.52040357631},{0.60404664250, -0.60404664250},{-0.82775064841},{-0.06615812964, 0.29402137720, 0, -0.29402137720, 0.06615812964}}}, /* WT 10-18 IRR*/
+       {7, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 2, {0}, {0}, {0}, {1,1}, {-0.5, 0.25}},       /* WT 5-3 IRR*/
+       {8, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {4,4}, {8,8}, {2,2}, {{-9,1},{5,-1}}}         /* WT 13-7 REV*/
+};
+
+typedef struct opj_dec_mstabent {
+       /** marker value */
+       int id;
+       /** value of the state when the marker can appear */
+       int states;
+       /** action linked to the marker */
+       void (*handler) (opj_j3d_t *j3d);
+} opj_dec_mstabent_t;
+
+opj_dec_mstabent_t j3d_dec_mstab[] = {
+  {J3D_MS_SOC, J3D_STATE_MHSOC, j3d_read_soc},
+  {J3D_MS_SOT, J3D_STATE_MH | J3D_STATE_TPHSOT, j3d_read_sot},
+  {J3D_MS_SOD, J3D_STATE_TPH, j3d_read_sod},
+  {J3D_MS_EOC, J3D_STATE_TPHSOT, j3d_read_eoc},
+  {J3D_MS_CAP, J3D_STATE_MHSIZ, j3d_read_cap},
+  {J3D_MS_SIZ, J3D_STATE_MHSIZ, j3d_read_siz},
+  {J3D_MS_NSI, J3D_STATE_MHSIZ, j3d_read_nsi},
+  {J3D_MS_COD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cod},
+  {J3D_MS_COC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_coc},
+  {J3D_MS_RGN, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_rgn},
+  {J3D_MS_QCD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcd},
+  {J3D_MS_QCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcc},
+  {J3D_MS_POC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_poc},
+  {J3D_MS_TLM, J3D_STATE_MH, j3d_read_tlm},
+  {J3D_MS_PLM, J3D_STATE_MH, j3d_read_plm},
+  {J3D_MS_PLT, J3D_STATE_TPH, j3d_read_plt},
+  {J3D_MS_PPM, J3D_STATE_MH, j3d_read_ppm},
+  {J3D_MS_PPT, J3D_STATE_TPH, j3d_read_ppt},
+  {J3D_MS_SOP, 0, 0},
+  {J3D_MS_CRG, J3D_STATE_MH, j3d_read_crg},
+  {J3D_MS_COM, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_com},
+  {J3D_MS_DCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_dco},
+  {J3D_MS_ATK, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_atk},
+  {0, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_unk}
+  /*, -->must define the j3d_read functions
+  {J3D_MS_CBD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cbd},
+  {J3D_MS_MCT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mct},
+  {J3D_MS_MCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mcc},
+  {J3D_MS_MCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mco},
+  {J3D_MS_NLT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_nlt},
+  {J3D_MS_VMS, J3D_STATE_MH, j3d_read_vms},
+  {J3D_MS_DFS, J3D_STATE_MH, j3d_read_dfs},
+  {J3D_MS_ADS, J3D_STATE_MH, j3d_read_ads},
+  {J3D_MS_QPD, J3D_STATE_MH, j3d_read_qpd},
+  {J3D_MS_QPC, J3D_STATE_TPH, j3d_read_qpc}*/
+};
+
+/**
+Read the lookup table containing all the marker, status and action
+@param id Marker value
+*/
+static opj_dec_mstabent_t *j3d_dec_mstab_lookup(int id) {
+       opj_dec_mstabent_t *e;
+       for (e = j3d_dec_mstab; e->id != 0; e++) {
+               if (e->id == id) {
+                       break;
+               }
+       }
+       return e;
+}
+
+/* ----------------------------------------------------------------------- */
+/* J3D / JPT decoder interface                                             */
+/* ----------------------------------------------------------------------- */
+
+opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo) {
+       opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
+       if(j3d) {
+               j3d->cinfo = cinfo;
+               j3d->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
+               if(!j3d->default_tcp) {
+                       opj_free(j3d);
+                       return NULL;
+               }
+       }
+       return j3d;
+}
+
+void j3d_destroy_decompress(opj_j3d_t *j3d) {
+       int i = 0;
+
+       if(j3d->tile_len != NULL) {
+               opj_free(j3d->tile_len);
+       }
+       if(j3d->tile_data != NULL) {
+               opj_free(j3d->tile_data);
+       }
+       if(j3d->default_tcp != NULL) {
+               opj_tcp_t *default_tcp = j3d->default_tcp;
+               if(default_tcp->ppt_data_first != NULL) {
+                       opj_free(default_tcp->ppt_data_first);
+               }
+               if(j3d->default_tcp->tccps != NULL) {
+                       opj_free(j3d->default_tcp->tccps);
+               }
+               opj_free(j3d->default_tcp);
+       }
+       if(j3d->cp != NULL) {
+               opj_cp_t *cp = j3d->cp;
+               if(cp->tcps != NULL) {
+                       for(i = 0; i < cp->tw * cp->th * cp->tl; i++) {
+                               if(cp->tcps[i].ppt_data_first != NULL) {
+                                       opj_free(cp->tcps[i].ppt_data_first);
+                               }
+                               if(cp->tcps[i].tccps != NULL) {
+                                       opj_free(cp->tcps[i].tccps);
+                               }
+                       }
+                       opj_free(cp->tcps);
+               }
+               if(cp->ppm_data_first != NULL) {
+                       opj_free(cp->ppm_data_first);
+               }
+               if(cp->tileno != NULL) {
+                       opj_free(cp->tileno);  
+               }
+               if(cp->comment != NULL) {
+                       opj_free(cp->comment);
+               }
+
+               opj_free(cp);
+       }
+
+       opj_free(j3d);
+}
+
+void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters) {
+       if(j3d && parameters) {
+               /* create and initialize the coding parameters structure */
+               opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
+               cp->reduce[0] = parameters->cp_reduce[0];
+               cp->reduce[1] = parameters->cp_reduce[1];
+               cp->reduce[2] = parameters->cp_reduce[2];
+               cp->layer = parameters->cp_layer;
+               cp->bigendian = parameters->bigendian;
+
+    /* MM: Settings of the following two member variables would take
+      place during j3d_read_com. FIXME */
+               cp->encoding_format = ENCOD_3EB;
+               cp->transform_format = TRF_2D_DWT;
+
+               /* keep a link to cp so that we can destroy it later in j3d_destroy_decompress */
+               j3d->cp = cp;
+       }
+}
+
+opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio) {
+       opj_volume_t *volume = NULL;
+
+       opj_common_ptr cinfo = j3d->cinfo;
+
+       j3d->cio = cio;
+
+       /* create an empty volume */
+       volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));
+       j3d->volume = volume;
+
+       j3d->state = J3D_STATE_MHSOC;
+       
+       for (;;) {
+               opj_dec_mstabent_t *e;
+               int id = cio_read(cio, 2);
+               if (id >> 8 != 0xff) {
+                       opj_volume_destroy(volume);
+                       opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
+                       return 0;
+               }
+               e = j3d_dec_mstab_lookup(id);
+               /*opj_event_msg(cinfo, EVT_INFO, "MARKER %x PREVSTATE %d E->STATE %d\n",e->id,j3d->state,e->states);*/
+               if (!(j3d->state & e->states)) {
+                       opj_volume_destroy(volume);
+                       opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
+                       return 0;
+               }
+               if (e->handler) {
+                       (*e->handler)(j3d);
+               }
+               /*opj_event_msg(cinfo, EVT_INFO, "POSTSTATE %d\n",j3d->state);*/
+               if (j3d->state == J3D_STATE_MT) {
+                       break;
+               }
+               if (j3d->state == J3D_STATE_NEOC) {
+                       break;
+               }
+       }
+       if (j3d->state == J3D_STATE_NEOC) {
+               j3d_read_eoc(j3d);
+       }
+
+       if (j3d->state != J3D_STATE_MT) {
+               opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
+       }
+       
+       return volume;
+}
+
+/* ----------------------------------------------------------------------- */
+/* J3D encoder interface                                                       */
+/* ----------------------------------------------------------------------- */
+
+opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo) {
+       opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
+       if(j3d) {
+               j3d->cinfo = cinfo;
+       }
+       return j3d;
+}
+
+void j3d_destroy_compress(opj_j3d_t *j3d) {
+       int tileno;
+
+       if(!j3d) return;
+
+       if(j3d->volume_info != NULL) {
+               opj_volume_info_t *volume_info = j3d->volume_info;
+               if (volume_info->index_on && j3d->cp) {
+                       opj_cp_t *cp = j3d->cp;
+                       for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
+                               opj_tile_info_t *tile_info = &volume_info->tile[tileno];
+                               opj_free(tile_info->thresh);
+                               opj_free(tile_info->packet);
+                       }
+                       opj_free(volume_info->tile);
+               }
+               opj_free(volume_info);
+       }
+       if(j3d->cp != NULL) {
+               opj_cp_t *cp = j3d->cp;
+
+               if(cp->comment) {
+                       opj_free(cp->comment);
+               }
+               if(cp->matrice) {
+                       opj_free(cp->matrice);
+               }
+               for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
+                       opj_free(cp->tcps[tileno].tccps);
+               }
+               opj_free(cp->tcps);
+               opj_free(cp);
+       }
+
+       opj_free(j3d);
+}
+
+void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume) {
+       int i, j, tileno, numpocs_tile;
+       opj_cp_t *cp = NULL;
+
+       if(!j3d || !parameters || ! volume) {
+               return;
+       }
+
+       /* create and initialize the coding parameters structure */
+       cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
+
+       /* keep a link to cp so that we can destroy it later in j3d_destroy_compress */
+       j3d->cp = cp;
+
+       /* set default values for cp */
+       cp->tw = 1;
+       cp->th = 1;
+       cp->tl = 1;
+
+       /* copy user encoding parameters */
+       cp->disto_alloc = parameters->cp_disto_alloc;
+       cp->fixed_alloc = parameters->cp_fixed_alloc;
+       cp->fixed_quality = parameters->cp_fixed_quality;
+
+       /* transform and coding method */
+       cp->transform_format = parameters->transform_format;
+       cp->encoding_format = parameters->encoding_format;
+
+       /* mod fixed_quality */
+       if(parameters->cp_matrice) {
+               size_t array_size = parameters->tcp_numlayers * 3 * parameters->numresolution[0] * sizeof(int);
+               cp->matrice = (int *) opj_malloc(array_size);
+               memcpy(cp->matrice, parameters->cp_matrice, array_size);
+       } 
+
+       /* creation of an index file ? */
+       cp->index_on = parameters->index_on;
+       if(cp->index_on) {
+               j3d->volume_info = (opj_volume_info_t*)opj_malloc(sizeof(opj_volume_info_t));
+       }
+       
+       /* tiles */
+       cp->tdx = parameters->cp_tdx;
+       cp->tdy = parameters->cp_tdy;
+       cp->tdz = parameters->cp_tdz;
+       /* tile offset */
+       cp->tx0 = parameters->cp_tx0;
+       cp->ty0 = parameters->cp_ty0;
+       cp->tz0 = parameters->cp_tz0;
+       /* comment string */
+       if(parameters->cp_comment) {
+               cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
+               if(cp->comment) {
+                       strcpy(cp->comment, parameters->cp_comment);
+               }
+       }
+
+       /*calculate other encoding parameters*/
+       if (parameters->tile_size_on) {
+               cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
+               cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
+               cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
+       } else {
+               cp->tdx = volume->x1 - cp->tx0;
+               cp->tdy = volume->y1 - cp->ty0;
+               cp->tdz = volume->z1 - cp->tz0;
+       }
+
+       /* initialize the multiple tiles */
+       /* ---------------------------- */
+       cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
+
+       for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
+               opj_tcp_t *tcp = &cp->tcps[tileno];
+               tcp->numlayers = parameters->tcp_numlayers;
+               for (j = 0; j < tcp->numlayers; j++) {
+                       if (cp->fixed_quality) {        /* add fixed_quality */
+                               tcp->distoratio[j] = parameters->tcp_distoratio[j];
+                       } else {
+                               tcp->rates[j] = parameters->tcp_rates[j];
+                       }
+               }
+               tcp->csty = parameters->csty;
+               tcp->prg = parameters->prog_order;
+               tcp->mct = volume->numcomps == 3 ? 1 : 0;
+
+               numpocs_tile = 0;
+               tcp->POC = 0;
+               if (parameters->numpocs) {
+                       /* initialisation of POC */
+                       tcp->POC = 1;
+                       for (i = 0; i < parameters->numpocs; i++) {
+                               if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
+                                       opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
+                                       tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
+                                       tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
+                                       tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
+                                       tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
+                                       tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
+                                       tcp_poc->prg            = parameters->POC[numpocs_tile].prg;
+                                       tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
+                                       numpocs_tile++;
+                               }
+                       }
+               }
+               tcp->numpocs = numpocs_tile;
+
+               tcp->tccps = (opj_tccp_t *) opj_malloc(volume->numcomps * sizeof(opj_tccp_t));
+               
+               for (i = 0; i < volume->numcomps; i++) {
+                       opj_tccp_t *tccp = &tcp->tccps[i];
+                       tccp->csty = parameters->csty & J3D_CCP_CSTY_PRT;       /* 0 => standard precint || 1 => custom-defined precinct  */
+                       tccp->numresolution[0] = parameters->numresolution[0];
+                       tccp->numresolution[1] = parameters->numresolution[1];
+                       tccp->numresolution[2] = parameters->numresolution[2];
+                                               assert (parameters->cblock_init[0] <= T1_MAXCBLKW);
+                                               assert (parameters->cblock_init[0] >= T1_MINCBLKW);
+                                               assert (parameters->cblock_init[1] <= T1_MAXCBLKH);
+                                               assert (parameters->cblock_init[1] >= T1_MINCBLKH);
+                                               assert (parameters->cblock_init[2] <= T1_MAXCBLKD);
+                                               assert (parameters->cblock_init[2] >= T1_MINCBLKD);
+                       tccp->cblk[0] = int_floorlog2(parameters->cblock_init[0]); 
+                       tccp->cblk[1] = int_floorlog2(parameters->cblock_init[1]); 
+                       tccp->cblk[2] = int_floorlog2(parameters->cblock_init[2]); 
+                                               assert (tccp->cblk[0]+tccp->cblk[1]+tccp->cblk[1] <= T1_MAXWHD);
+                       tccp->cblksty = parameters->mode; /*Codeblock style --> Table A.19 (default 0)*/
+
+                       /*ATK / transform */
+                       tccp->reversible = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3  */
+                       for (j = 0; j < 3; j++) {
+                                       tccp->dwtid[j] = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3  */
+                       }
+                                               
+                       /* Quantification: SEQNT (Scalar Expounded, value for each subband) / NOQNT (no quant)*/
+                       tccp->qntsty = parameters->irreversible ? J3D_CCP_QNTSTY_SEQNT : J3D_CCP_QNTSTY_NOQNT;
+                       tccp->numgbits = 2;
+                       if (i == parameters->roi_compno) {
+                               tccp->roishift = parameters->roi_shift;
+                       } else {
+                               tccp->roishift = 0;
+                       }
+                       /* Custom defined precints */
+                       if (parameters->csty & J3D_CCP_CSTY_PRT) {
+                               int k;
+                               for (k = 0; k < 3; k++) {
+                                       int p = 0;
+                                       for (j = tccp->numresolution[k] - 1; j >= 0; j--) {
+                                               if (p < parameters->res_spec) {/* p < number of precinct size specifications */
+                                                       if (parameters->prct_init[k][p] < 1) {
+                                                               tccp->prctsiz[k][j] = 1;
+                                                       } else {
+                                                               tccp->prctsiz[k][j] = int_floorlog2(parameters->prct_init[k][p]);
+                                                       }
+                                               } else {
+                                                       int res_spec = parameters->res_spec;
+                                                       int size_prct = parameters->prct_init[k][res_spec - 1] >> (p - (res_spec - 1));
+                                                       if (size_prct < 1) {
+                                                               tccp->prctsiz[k][j] = 1;
+                                                       } else {
+                                                               tccp->prctsiz[k][j] = int_floorlog2(size_prct);
+                                                       }
+                                               }
+                                       }
+                                       p++;
+                               }
+                       } else {
+                               int k;
+                               for (k = 0; k < 3; k++) {
+                    for (j = 0; j < tccp->numresolution[k]; j++) {
+                        tccp->prctsiz[k][j] = 15;
+                                       }
+                               }
+                       }
+                       /*Calcular stepsize for each subband (if NOQNT -->stepsize = 1.0)*/
+                       dwt_calc_explicit_stepsizes(tccp, volume->comps[i].prec);
+               }
+       }
+}
+
+/**
+Create an index file
+@param j3d
+@param cio
+@param volume_info
+@param index Index filename
+@return Returns 1 if successful, returns 0 otherwise
+*/
+static int j3d_create_index(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_info_t *volume_info, char *index) {
+       
+       int tileno, compno, layno, resno, precno, pack_nb, x, y, z;
+       FILE *stream = NULL;
+       double total_disto = 0;
+
+       volume_info->codestream_size = cio_tell(cio) + j3d->pos_correction;     /* Correction 14/4/03 suite rmq de Patrick */
+
+       stream = fopen(index, "w");
+       if (!stream) {
+               opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
+               return 0;
+       }
+       
+       fprintf(stream, "w %d\t h %d\t l %d\n", volume_info->volume_w, volume_info->volume_h, volume_info->volume_l);
+       fprintf(stream, "TRASNFORM\t%d\n", volume_info->transform_format);
+       fprintf(stream, "ENTROPY CODING\t%d\n", volume_info->encoding_format);
+       fprintf(stream, "PROG\t%d\n", volume_info->prog);
+       fprintf(stream, "TILE\tx %d y %d z %d\n", volume_info->tile_x, volume_info->tile_y, volume_info->tile_z);
+       fprintf(stream, "NOTILE\tx %d y %d z %d\n", volume_info->tw, volume_info->th, volume_info->tl);
+       fprintf(stream, "COMPONENTS\t%d\n", volume_info->comp);
+       fprintf(stream, "LAYER\t%d\n", volume_info->layer);
+       fprintf(stream, "RESOLUTIONS\tx %d y %d z %d\n", volume_info->decomposition[0], volume_info->decomposition[1], volume_info->decomposition[2]);
+       
+       fprintf(stream, "Precint sizes for each resolution:\n");
+       for (resno = volume_info->decomposition[0]; resno >= 0; resno--) {
+               fprintf(stream, "Resno %d \t [%d,%d,%d] \n", resno,
+                       (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[2][resno]));   /* based on tile 0 */
+       }
+       fprintf(stream, "HEADER_END\t%d\n", volume_info->main_head_end);
+       fprintf(stream, "CODESTREAM\t%d\n", volume_info->codestream_size);
+       fprintf(stream, "Num_tile Start_pos End_header End_pos Distotile Nbpix Ratio\n");
+       for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
+               fprintf(stream, "%4d\t%9d\t%9d\t%9d\t%9e\t%9d\t%9e\n",
+                       volume_info->tile[tileno].num_tile,
+                       volume_info->tile[tileno].start_pos,
+                       volume_info->tile[tileno].end_header,
+                       volume_info->tile[tileno].end_pos,
+                       volume_info->tile[tileno].distotile, volume_info->tile[tileno].nbpix,
+                       volume_info->tile[tileno].distotile / volume_info->tile[tileno].nbpix);
+       }
+       
+       for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
+               int start_pos, end_pos;
+               double disto = 0;
+               pack_nb = 0;
+               if (volume_info->prog == LRCP) {        /* LRCP */
+                       fprintf(stream, "pack_nb tileno layno resno compno precno start_pos  end_pos   disto\n");
+                       for (layno = 0; layno < volume_info->layer; layno++) {
+                               for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
+                                       for (compno = 0; compno < volume_info->comp; compno++) {
+                                               int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
+                                               for (precno = 0; precno < prec_max; precno++) {
+                                                       start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
+                                                       end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
+                                                       disto = volume_info->tile[tileno].packet[pack_nb].disto;
+                                                       fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
+                                                       total_disto += disto;
+                                                       pack_nb++;
+                                               }
+                                       }
+                               }
+                       }
+               } /* LRCP */
+               else if (volume_info->prog == RLCP) {   /* RLCP */
+                       /*
+                       fprintf(stream, "pack_nb tileno resno layno compno precno start_pos  end_pos   disto");
+                       */
+                       for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
+                               for (layno = 0; layno < volume_info->layer; layno++) {
+                                       for (compno = 0; compno < volume_info->comp; compno++) {
+                                               int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno]* volume_info->tile[tileno].prctno[2][resno];
+                                               for (precno = 0; precno < prec_max; precno++) {
+                                                       start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
+                                                       end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
+                                                       disto = volume_info->tile[tileno].packet[pack_nb].disto;
+                                                       fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
+                                                               pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
+                                                       total_disto += disto;
+                                                       pack_nb++;
+                                               }
+                                       }
+                               }
+                       }
+               } /* RLCP */
+               else if (volume_info->prog == RPCL) {   /* RPCL */
+                       /*
+                       fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos  end_pos   disto\n"); 
+                       */
+                       for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
+                               /* I suppose components have same XRsiz, YRsiz */
+                               /*int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;*/
+                               /*int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;*/
+                               int x0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_x;
+                               int y0 = volume_info->tile_Oy + (int)floor( (float)tileno/(float)volume_info->th ) * volume_info->tile_y;
+                               int z0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tl ) * volume_info->tile_z;
+                               int x1 = x0 + volume_info->tile_x;
+                               int y1 = y0 + volume_info->tile_y;
+                               int z1 = z0 + volume_info->tile_z;
+                               for(z = z0; z < z1; z++) {
+                                       for(y = y0; y < y1; y++) {
+                                               for(x = x0; x < x1; x++) {
+                                                       for (compno = 0; compno < volume_info->comp; compno++) {
+                                                               int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
+                                                               for (precno = 0; precno < prec_max; precno++) {
+                                                                       int pcnx = volume_info->tile[tileno].prctno[0][resno];
+                                                                       int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
+                                                                       int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
+                                                                       int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
+                                                                       int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
+                                                                       int precno_y = (int) floor( (float)precno/(float)pcnx );
+                                                                       if (precno_y*pcy == y ) {
+                                                                               if (precno_x*pcx == x ) {
+                                                                                       for (layno = 0; layno < volume_info->layer; layno++) {
+                                                                                               start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
+                                                                                               end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
+                                                                                               disto = volume_info->tile[tileno].packet[pack_nb].disto;
+                                                                                               fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
+                                                                                                       pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto); 
+                                                                                               total_disto += disto;
+                                                                                               pack_nb++; 
+                                                                                       }
+                                                                               }
+                                                                       }
+                                                               } /* precno */
+                                                       } /* compno */
+                                               } /* x = x0..x1 */
+                                       } /* y = y0..y1 */
+                               } /* z = z0..z1 */
+                       } /* resno */
+               } /* RPCL */
+               else if (volume_info->prog == PCRL) {   /* PCRL */
+                       /* I suppose components have same XRsiz, YRsiz */
+                       int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
+                       int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
+                       int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
+                       int x1 = x0 + volume_info->tile_x;
+                       int y1 = y0 + volume_info->tile_y;
+                       int z1 = z0 + volume_info->tile_z;
+                       /*
+                       fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos  end_pos   disto\n"); 
+                       */
+                       for(z = z0; z < z1; z++) {
+                               for(y = y0; y < y1; y++) {
+                                       for(x = x0; x < x1; x++) {
+                                               for (compno = 0; compno < volume_info->comp; compno++) {
+                                                       for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
+                                                               int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno];
+                                                               for (precno = 0; precno < prec_max; precno++) {
+                                                               int pcnx = volume_info->tile[tileno].prctno[0][resno];
+                                                               int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
+                                                               int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
+                                                               int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
+                                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
+                                                               int precno_y = (int) floor( (float)precno/(float)pcnx );
+                                                               int precno_z = (int) floor( (float)precno/(float)pcnx );
+                                                               if (precno_z*pcz == z ) {
+                                                                       if (precno_y*pcy == y ) {
+                                                                               if (precno_x*pcx == x ) {
+                                                                                       for (layno = 0; layno < volume_info->layer; layno++) {
+                                                                                               start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
+                                                                                               end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
+                                                                                               disto = volume_info->tile[tileno].packet[pack_nb].disto;
+                                                                                               fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
+                                                                                                       pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto); 
+                                                                                               total_disto += disto;
+                                                                                               pack_nb++; 
+                                                                                       }
+                                                                               }
+                                                                       }
+                                                               }
+                                                       } /* precno */
+                                               } /* resno */
+                                       } /* compno */
+                               } /* x = x0..x1 */
+                       } /* y = y0..y1 */
+                       }
+               } /* PCRL */
+               else {  /* CPRL */
+                       /*
+                       fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos  end_pos   disto\n"); 
+                       */
+                       for (compno = 0; compno < volume_info->comp; compno++) {
+                               /* I suppose components have same XRsiz, YRsiz */
+                               int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
+                               int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
+                               int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
+                               int x1 = x0 + volume_info->tile_x;
+                               int y1 = y0 + volume_info->tile_y;
+                               int z1 = z0 + volume_info->tile_z;
+                               for(z = z0; z < z1; z++) {
+                                       for(y = y0; y < y1; y++) {
+                                               for(x = x0; x < x1; x++) {
+                                                       for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
+                                                               int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
+                                                               for (precno = 0; precno < prec_max; precno++) {
+                                                                       int pcnx = volume_info->tile[tileno].prctno[0][resno];
+                                                                       int pcny = volume_info->tile[tileno].prctno[1][resno];
+                                                                       int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
+                                                                       int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
+                                                                       int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
+                                                                       int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
+                                                                       int precno_y = (int) floor( (float)precno/(float)pcnx );
+                                                                       int precno_z = 0; /*???*/
+                                                                       if (precno_z*pcz == z ) {
+                                                                               if (precno_y*pcy == y ) {
+                                                                                       if (precno_x*pcx == x ) {
+                                                                                               for (layno = 0; layno < volume_info->layer; layno++) {
+                                                                                                       start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
+                                                                                                       end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
+                                                                                                       disto = volume_info->tile[tileno].packet[pack_nb].disto;
+                                                                                                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
+                                                                                                               pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto); 
+                                                                                                       total_disto += disto;
+                                                                                                       pack_nb++; 
+                                                                                               }
+                                                                                       }
+                                                                               }
+                                                                       }
+                                                               } /* precno */
+                                                       } /* resno */
+                                               } /* x = x0..x1 */
+                                       } /* y = y0..y1 */
+                               } /* z = z0..z1 */
+                       } /* comno */
+               } /* CPRL */   
+       } /* tileno */
+       
+       fprintf(stream, "SE_MAX\t%8e\n", volume_info->D_max);   /* SE max */
+       fprintf(stream, "SE_TOTAL\t%.8e\n", total_disto);                       /* SE totale */
+       
+
+       fclose(stream);
+
+       return 1;
+}
+
+bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index) {
+       int tileno, compno;
+       opj_volume_info_t *volume_info = NULL;
+       opj_cp_t *cp = NULL;
+       opj_tcd_t *tcd = NULL;  /* TCD component */
+
+       j3d->cio = cio; 
+       j3d->volume = volume;
+       cp = j3d->cp;
+
+       /*j3d_dump_volume(stdout, volume);
+       j3d_dump_cp(stdout, volume, cp);*/
+
+       /* INDEX >> */
+       volume_info = j3d->volume_info;
+       if (volume_info && cp->index_on) {
+               volume_info->index_on = cp->index_on;
+               volume_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tile_info_t));
+               volume_info->volume_w = volume->x1 - volume->x0;
+               volume_info->volume_h = volume->y1 - volume->y0;
+               volume_info->volume_l = volume->z1 - volume->z0;
+               volume_info->prog = (&cp->tcps[0])->prg;
+               volume_info->tw = cp->tw;
+               volume_info->th = cp->th;
+               volume_info->tl = cp->tl;
+               volume_info->tile_x = cp->tdx;  /* new version parser */
+               volume_info->tile_y = cp->tdy;  /* new version parser */
+               volume_info->tile_z = cp->tdz;  /* new version parser */
+               volume_info->tile_Ox = cp->tx0; /* new version parser */
+               volume_info->tile_Oy = cp->ty0; /* new version parser */
+               volume_info->tile_Oz = cp->tz0; /* new version parser */
+               volume_info->transform_format = cp->transform_format;
+               volume_info->encoding_format = cp->encoding_format;
+               volume_info->comp = volume->numcomps;
+               volume_info->layer = (&cp->tcps[0])->numlayers;
+               volume_info->decomposition[0] = (&cp->tcps[0])->tccps->numresolution[0] - 1;
+               volume_info->decomposition[1] = (&cp->tcps[0])->tccps->numresolution[1] - 1;
+               volume_info->decomposition[2] = (&cp->tcps[0])->tccps->numresolution[2] - 1;
+               volume_info->D_max = 0;         /* ADD Marcela */
+       }
+       /* << INDEX */
+
+       j3d_write_soc(j3d);
+       j3d_write_siz(j3d);
+       if (j3d->cinfo->codec_format == CODEC_J3D) {
+               j3d_write_cap(j3d);
+               j3d_write_nsi(j3d);
+       }
+
+       /*if (j3d->cp->transform_format != TRF_2D_DWT || j3d->cp->encoding_format != ENCOD_2EB)*/
+               j3d_write_com(j3d);
+
+       j3d_write_cod(j3d);
+       j3d_write_qcd(j3d);
+       for (compno = 0; compno < volume->numcomps; compno++) {
+               opj_tcp_t *tcp = &cp->tcps[0];
+               if (tcp->tccps[compno].roishift)
+                       j3d_write_rgn(j3d, compno, 0);                  
+       }
+       /*Optional 15444-2 markers*/
+       if (j3d->cp->tcps->tccps[0].atk != NULL)
+        j3d_write_atk(j3d);
+       if (j3d->volume->comps[0].dcoffset != 0)
+        j3d_write_dco(j3d);
+
+       /* INDEX >> */
+       if(volume_info && volume_info->index_on) {
+               volume_info->main_head_end = cio_tell(cio) - 1;
+       }
+       /* << INDEX */
+
+       /* create the tile encoder */
+       tcd = tcd_create(j3d->cinfo);
+
+       /* encode each tile */
+       for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
+               opj_event_msg(j3d->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th * cp->tl);
+               
+               j3d->curtileno = tileno;
+
+               /* initialisation before tile encoding  */
+               if (tileno == 0) {      
+                       tcd_malloc_encode(tcd, volume, cp, j3d->curtileno);
+               } else {
+                       tcd_init_encode(tcd, volume, cp, j3d->curtileno);
+               }
+               
+               /* INDEX >> */
+               if(volume_info && volume_info->index_on) {
+                       volume_info->tile[j3d->curtileno].num_tile = j3d->curtileno;
+                       volume_info->tile[j3d->curtileno].start_pos = cio_tell(cio) + j3d->pos_correction;
+               }
+               /* << INDEX */
+               
+               j3d_write_sot(j3d);
+       
+               for (compno = 1; compno < volume->numcomps; compno++) {
+                       j3d_write_coc(j3d, compno);
+                       j3d_write_qcc(j3d, compno);
+               }
+
+               if (cp->tcps[tileno].numpocs) {
+                       j3d_write_poc(j3d);
+               }
+               j3d_write_sod(j3d, tcd); /*--> tcd_encode_tile*/
+
+               /* INDEX >> */
+               if(volume_info && volume_info->index_on) {
+                       volume_info->tile[j3d->curtileno].end_pos = cio_tell(cio) + j3d->pos_correction - 1;
+               }
+               /* << INDEX */          
+       }
+       
+       /* destroy the tile encoder */
+       tcd_free_encode(tcd);
+       tcd_destroy(tcd);
+
+       j3d_write_eoc(j3d);
+       
+       /* Creation of the index file */
+       if(volume_info && volume_info->index_on) {
+               if(!j3d_create_index(j3d, cio, volume_info, index)) {
+                       opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
+                       return false;
+               }
+       }
+         
+       return true;
+}
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
index edd27a1a57ca9f76fbb4fbd885006a7232f43cdf..d54e21dcd18eee096958eff6448e3994c77338d0 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-#ifndef __J3D_H\r
-#define __J3D_H\r
-/**\r
-@file jp3d.h\r
-@brief The JPEG-2000 Codestream Reader/Writer (J3D)\r
-\r
-The functions in J3D.C have for goal to read/write the several parts of the codestream: markers and data.\r
-*/\r
-\r
-/** @defgroup J3D J3D - JPEG-2000 PART 10 codestream reader/writer */\r
-/*@{*/\r
-\r
-#define J3D_CP_CSTY_PRT 0x01\r
-#define J3D_CP_CSTY_SOP 0x02\r
-#define J3D_CP_CSTY_EPH 0x04\r
-#define J3D_CCP_CSTY_PRT 0x01\r
-/** Table A-8 */\r
-#define J3D_CCP_CBLKSTY_LAZY 0x01       /* Selective arithmetic coding bypass */\r
-#define J3D_CCP_CBLKSTY_RESET 0x02   /* Reset context probabilities on coding pass boundaries */\r
-#define J3D_CCP_CBLKSTY_TERMALL 0x04 /* Termination on each coding pass */\r
-#define J3D_CCP_CBLKSTY_VSC 0x08        /* Vertically causal context, add also hook for switching off and on 3D context models */      \r
-#define J3D_CCP_CBLKSTY_PTERM 0x10      /* Predictable termination */\r
-#define J3D_CCP_CBLKSTY_SEGSYM 0x20     /* Segmentation symbols are used */\r
-#define J3D_CCP_CBLKSTY_3DCTXT 0x40  /* 3D context models (3D-EBCOT) vs 2D context models */\r
-\r
-#define J3D_CCP_QNTSTY_NOQNT 0 /* Quantization style : no quantization */\r
-#define J3D_CCP_QNTSTY_SIQNT 1 /* Quantization style : scalar derived (values signalled only in LLL subband) */\r
-#define J3D_CCP_QNTSTY_SEQNT 2 /* Quantization style : scalar expounded (values signalled for each subband) */\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-#define J3D_MS_SOC 0xff4f      /**< SOC marker value */\r
-#define J3D_MS_SOT 0xff90      /**< SOT marker value */\r
-#define J3D_MS_SOD 0xff93      /**< SOD marker value */\r
-#define J3D_MS_EOC 0xffd9      /**< EOC marker value */\r
-#define J3D_MS_CAP 0xff50      /**< CAP marker value */\r
-#define J3D_MS_SIZ 0xff51      /**< SIZ marker value */\r
-#define J3D_MS_NSI 0xff54      /**< NSI marker value */\r
-#define J3D_MS_COD 0xff52      /**< COD marker value */\r
-#define J3D_MS_COC 0xff53      /**< COC marker value */\r
-#define J3D_MS_RGN 0xff5e      /**< RGN marker value */\r
-#define J3D_MS_QCD 0xff5c      /**< QCD marker value */\r
-#define J3D_MS_QCC 0xff5d      /**< QCC marker value */\r
-#define J3D_MS_POC 0xff5f      /**< POC marker value */\r
-#define J3D_MS_TLM 0xff55      /**< TLM marker value */\r
-#define J3D_MS_PLM 0xff57      /**< PLM marker value */\r
-#define J3D_MS_PLT 0xff58      /**< PLT marker value */\r
-#define J3D_MS_PPM 0xff60      /**< PPM marker value */\r
-#define J3D_MS_PPT 0xff61      /**< PPT marker value */\r
-#define J3D_MS_SOP 0xff91      /**< SOP marker value */\r
-#define J3D_MS_EPH 0xff92      /**< EPH marker value */\r
-#define J3D_MS_CRG 0xff63      /**< CRG marker value */\r
-#define J3D_MS_COM 0xff64      /**< COM marker value */\r
-/*15444-2*/\r
-#define J3D_MS_DCO 0xff70      /**< DCO marker value */\r
-#define J3D_MS_VMS 0xff71   /**< VMS marker value */\r
-#define J3D_MS_DFS 0xff72      /**< DFS marker value */\r
-#define J3D_MS_ADS 0xff73      /**< ADS marker value */\r
-#define J3D_MS_ATK 0xff79      /**< ATK marker value */\r
-#define J3D_MS_CBD 0xff78      /**< CBD marker value */\r
-#define J3D_MS_MCT 0xff74      /**< MCT marker value */\r
-#define J3D_MS_MCC 0xff75      /**< MCC marker value */\r
-#define J3D_MS_MCO 0xff77      /**< MCO marker value */\r
-#define J3D_MS_NLT 0xff76      /**< NLT marker value */\r
-#define J3D_MS_QPD 0xff5a      /**< QPD marker value */\r
-#define J3D_MS_QPC 0xff5b      /**< QPC marker value */\r
-\r
-/* ----------------------------------------------------------------------- */\r
-/* Capability RSIZ parameter, extended */\r
-#define J3D_RSIZ_BASIC 0x0000\r
-\r
-#define J3D_RSIZ_DCO   0x8001 /* Required */\r
-#define J3D_RSIZ_VSQNT 0x8002\r
-#define J3D_RSIZ_TCQNT 0x8004\r
-#define J3D_RSIZ_VMASK 0x8008\r
-#define J3D_RSIZ_SSOVL 0x8010\r
-#define J3D_RSIZ_ADECS 0x8020\r
-#define J3D_RSIZ_ATK   0x8040 /*Required*/\r
-#define J3D_RSIZ_SSYMK 0x8080\r
-#define J3D_RSIZ_MCT   0x8100 /*Not compatible with DCO*/\r
-#define J3D_RSIZ_NLT   0x8200 /*Required*/\r
-#define J3D_RSIZ_ASHAP 0x8400\r
-#define J3D_RSIZ_PRQNT 0x8800\r
-\r
-#define J3D_CAP_10             0x00400000\r
-/* Arbitrary transformation kernel, 15444-2 */\r
-#define J3D_ATK_IRR 0\r
-#define J3D_ATK_REV 1\r
-#define J3D_ATK_ARB 0\r
-#define J3D_ATK_WS 1\r
-#define J3D_ATK_CON 0\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/**\r
-Values that specify the status of the decoding process when decoding the main header. \r
-These values may be combined with a | operator. \r
-*/\r
-typedef enum J3D_STATUS {\r
-       /**< a SOC marker is expected */\r
-       J3D_STATE_MHSOC  = 0x0001, \r
-       /**< a SIZ marker is expected */\r
-       J3D_STATE_MHSIZ  = 0x0002, \r
-       /**< the decoding process is in the main header */\r
-       J3D_STATE_MH     = 0x0004, \r
-       /**< the decoding process is in a tile part header and expects a SOT marker */\r
-       J3D_STATE_TPHSOT = 0x0008, \r
-       /**< the decoding process is in a tile part header */\r
-       J3D_STATE_TPH    = 0x0010, \r
-       /**< the EOC marker has just been read */\r
-       J3D_STATE_MT     = 0x0020, \r
-       /**< the decoding process must not expect a EOC marker because the codestream is truncated */\r
-       J3D_STATE_NEOC   = 0x0040  \r
-} J3D_STATUS;\r
-\r
-\r
-\r
-/**\r
-Arbitrary transformation kernel\r
-*/\r
-typedef struct opj_atk {\r
-/** index of wavelet kernel */\r
-       int index;\r
-/** Numerical type of scaling factor and lifting step parameters */\r
-       int coeff_typ;          \r
-/** Wavelet filter category */\r
-       int filt_cat;           \r
-/** Wavelet transformation type (REV/IRR) */\r
-       int wt_typ;                     \r
-/** Initial odd/even subsequence */\r
-       int minit;                      \r
-/** Boundary extension method (constant CON / whole-sample symmetric WS) */\r
-       int exten;                      \r
-/** Scaling factor. Only for wt_typ=IRR */\r
-       double Katk;                    \r
-/** Number of lifting steps */\r
-       int Natk;                       \r
-/** Offset for lifting step s. Only for filt_cat=ARB */\r
-       int Oatk[256];          \r
-/** Base 2 scaling exponent for lifting step s. Only for wt_typ=REV */\r
-       int Eatk[256];          \r
-/** Additive residue for lifting step s. Only for wt_typ=REV */\r
-       int Batk[256];          \r
-/** Number of lifting coefficients signaled for lifting step s  */\r
-       int LCatk[256]; \r
-/** Lifting coefficient k for lifting step s */\r
-       double Aatk[256][256];  \r
-} opj_atk_t;\r
-\r
-\r
-/**\r
-Quantization stepsize\r
-*/\r
-typedef struct opj_stepsize {\r
-/** exponent */\r
-       int expn;       \r
-/** mantissa */\r
-       int mant;       \r
-} opj_stepsize_t;\r
-\r
-/**\r
-Tile-component coding parameters\r
-*/\r
-typedef struct opj_tccp {\r
-       /** coding style */\r
-       int csty;                                                       \r
-       /** number of resolutions of x, y and z-axis */\r
-       int numresolution[3];   \r
-       /** code-blocks width height & depth*/\r
-       int cblk[3];                    \r
-       /** code-block coding style */\r
-       int cblksty;                    \r
-       /** 0: no ATK (only 9-7 or 5-3) 1: ATK defined WT*/\r
-       int atk_wt[3];                          \r
-       /** Arbitrary transformation kernel (15444-2)*/\r
-       opj_atk_t *atk;                 \r
-       /** DWT identifier for x, y and z-axis (0:WT9-7 1:WT5-3 >1:WT-atk->index) */\r
-       int dwtid[3];\r
-       /** reversible/irreversible wavelet transfomation (0:irrev 1:reversible)*/      \r
-       int reversible;                 \r
-       /** quantisation style */\r
-       int qntsty;                             \r
-       /** stepsizes used for quantization */\r
-       opj_stepsize_t stepsizes[J3D_MAXBANDS]; \r
-       /** number of guard bits. Table A28 de 15444-1*/\r
-       int numgbits;                   \r
-       /** Region Of Interest shift */\r
-       int roishift;                   \r
-       /** precinct width heigth & depth*/\r
-       int prctsiz[3][J3D_MAXRLVLS];           \r
-} opj_tccp_t;\r
-\r
-/**\r
-Tile coding parameters : coding/decoding parameters common to all tiles \r
-(information like COD, COC in main header)\r
-*/\r
-typedef struct opj_tcp {\r
-/** 1 : first part-tile of a tile */\r
-       int first;                              \r
-       /** coding style */\r
-       int csty;                               \r
-       /** progression order */\r
-       OPJ_PROG_ORDER prg;             \r
-       /** number of layers */\r
-       int numlayers;                  \r
-       /** multi-component transform identifier */\r
-       int mct;                                \r
-       /** rates of layers */\r
-       float rates[100];                       \r
-       /** number of progression order changes */\r
-       int numpocs;                    \r
-       /** indicates if a POC marker has been used O:NO, 1:YES */\r
-       int POC;                                \r
-       /** progression order changes */\r
-       opj_poc_t pocs[J3D_MAXRLVLS - 1];\r
-       /** add fixed_quality */\r
-       float distoratio[100];\r
-       /** tile-component coding parameters */\r
-       opj_tccp_t *tccps;              \r
-/** packet header store there for futur use in t2_decode_packet */\r
-       unsigned char *ppt_data;                \r
-       /** pointer remaining on the first byte of the first header if ppt is used */\r
-       unsigned char *ppt_data_first;  \r
-       /** If ppt == 1 --> there was a PPT marker for the present tile */\r
-       int ppt;                \r
-       /** used in case of multiple marker PPT (number of info already stored) */\r
-       int ppt_store;  \r
-       int ppt_len;    \r
-} opj_tcp_t;\r
-\r
-/**\r
-Coding parameters\r
-*/\r
-typedef struct opj_cp {\r
-/** transform format 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */\r
-       OPJ_TRANSFORM transform_format;         \r
-       /** entropy coding format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI*/\r
-       OPJ_ENTROPY_CODING encoding_format;     \r
-       /** allocation by rate/distortion */\r
-       int disto_alloc;        \r
-       /** allocation by fixed layer */\r
-       int fixed_alloc;        \r
-       /** add fixed_quality */\r
-       int fixed_quality;      \r
-       /** Rsiz: capabilities */\r
-       int rsiz;                       \r
-       /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, volume is decoded to the full resolution */\r
-       int reduce[3];          \r
-       /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */\r
-       int layer;                      \r
-       /** 0 = no index || 1 = index */\r
-       int index_on;           \r
-       /** Big-Endian/Little-endian order */\r
-       int bigendian;\r
-       /** XTOsiz */\r
-       int tx0;        \r
-       /** YTOsiz */\r
-       int ty0;                \r
-       /** ZTOsiz */\r
-       int tz0;        \r
-       /** XTsiz */\r
-       int tdx;        \r
-       /** YTsiz */\r
-       int tdy;        \r
-       /** ZTsiz */\r
-       int tdz;        \r
-       /** comment for coding */\r
-       char *comment;          \r
-       /** number of tiles in width, heigth and depth */\r
-       int tw;         \r
-       int th;\r
-       int tl;\r
-       /** ID number of the tiles present in the codestream */\r
-       int *tileno;    \r
-       /** size of the vector tileno */\r
-       int tileno_size;\r
-       /** tile coding parameters */\r
-       opj_tcp_t *tcps;\r
-       /** fixed layer */\r
-       int *matrice;           \r
-\r
-       /** packet header store there for futur use in t2_decode_packet */\r
-       unsigned char *ppm_data;                \r
-       /** pointer remaining on the first byte of the first header if ppm is used */\r
-       unsigned char *ppm_data_first;  \r
-       /** if ppm == 1 --> there was a PPM marker for the present tile */\r
-       int ppm;                        \r
-       /** use in case of multiple marker PPM (number of info already store) */\r
-       int ppm_store;          \r
-       /** use in case of multiple marker PPM (case on non-finished previous info) */\r
-       int ppm_previous;       \r
-       int ppm_len;            \r
-} opj_cp_t;\r
-\r
-/**\r
-Information concerning a packet inside tile\r
-*/\r
-typedef struct opj_packet_info {\r
-       /** start position */\r
-       int start_pos;  \r
-       /** end position */\r
-       int end_pos;    \r
-       /** distorsion introduced */\r
-       double disto;   \r
-} opj_packet_info_t;\r
-\r
-/**\r
-Index structure : information regarding tiles inside volume\r
-*/\r
-typedef struct opj_tile_info {\r
-       /** value of thresh for each layer by tile cfr. Marcela   */\r
-       double *thresh;         \r
-       /** number of tile */\r
-       int num_tile;           \r
-       /** start position */\r
-       int start_pos;          \r
-       /** end position of the header */\r
-       int end_header;         \r
-       /** end position */\r
-       int end_pos;            \r
-       /** precinct number for each resolution level (width, heigth and depth) */\r
-       int prctno[3][J3D_MAXRLVLS];    \r
-       /** precinct size (in power of 2), in X for each resolution level */\r
-       int prctsiz[3][J3D_MAXRLVLS];   \r
-       /** information concerning packets inside tile */\r
-       opj_packet_info_t *packet;              \r
-       \r
-       /** add fixed_quality */\r
-       int nbpix;                      \r
-       /** add fixed_quality */\r
-       double distotile;       \r
-} opj_tile_info_t;\r
-\r
-/**\r
-Index structure\r
-*/\r
-typedef struct opj_volume_info {\r
-       \r
-       /** transform format 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */\r
-       OPJ_TRANSFORM transform_format;         \r
-       /** output file format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI*/\r
-       OPJ_ENTROPY_CODING encoding_format;     /** 0 = no index || 1 = index */\r
-       int index_on;   \r
-       /** 0 = wt 9-7 || 1 = wt 5-3 || >=2 wt atk defined */\r
-       int dwtid[3];   \r
-       /** maximum distortion reduction on the whole volume (add for Marcela) */\r
-       double D_max;   \r
-       /** packet number */\r
-       int num;                \r
-       /** writing the packet in the index with t2_encode_packets */\r
-       int index_write;        \r
-       /** volume width, height and depth */\r
-       int volume_w;   \r
-       int volume_h;\r
-       int volume_l;\r
-       /** progression order */\r
-       OPJ_PROG_ORDER prog;    \r
-       /** tile size in x, y and z */\r
-       int tile_x;             \r
-       int tile_y;\r
-       int tile_z;\r
-       /** tile origin in x, y and z */\r
-       int tile_Ox;    \r
-       int tile_Oy;\r
-       int tile_Oz;\r
-       /** number of tiles in X, Y and Z */\r
-       int tw;                 \r
-       int th;\r
-       int tl;\r
-       /** component numbers */\r
-       int comp;                               \r
-       /** number of layer */\r
-       int layer;                              \r
-       /** number of decomposition in X, Y and Z*/\r
-       int decomposition[3];   \r
-       /** DC offset (15444-2) */\r
-       int dcoffset;   \r
-       /** main header position */\r
-       int main_head_end;              \r
-       /** codestream's size */\r
-       int codestream_size;    \r
-       /** information regarding tiles inside volume */\r
-       opj_tile_info_t *tile;  \r
-} opj_volume_info_t;\r
-\r
-/**\r
-JPEG-2000 codestream reader/writer\r
-*/\r
-typedef struct opj_j3d {\r
-       /** codec context */\r
-       opj_common_ptr cinfo;   \r
-       /** locate in which part of the codestream the decoder is (main header, tile header, end) */\r
-       int state;                              \r
-       /** number of the tile curently concern by coding/decoding */\r
-       int curtileno;                  \r
-       /** locate the position of the end of the tile in the codestream, used to detect a truncated codestream (in j3d_read_sod)       */\r
-       unsigned char *eot;     \r
-       /**     locate the start position of the SOT marker of the current coded tile:  */\r
-       int sot_start;          \r
-       /*  after encoding the tile, a jump (in j3d_write_sod) is done to the SOT marker to store the value of its length. */\r
-       int sod_start;          \r
-       /**     as the J3D-file is written in several parts during encoding, it enables to make the right correction in position return by cio_tell     */\r
-       int pos_correction;     \r
-       /** array used to store the data of each tile */\r
-       unsigned char **tile_data;      \r
-       /** array used to store the length of each tile */\r
-       int *tile_len;                          \r
-\r
-       /** decompression only : store decoding parameters common to all tiles */\r
-       opj_tcp_t *default_tcp;         \r
-       /** pointer to the encoded / decoded volume */\r
-       opj_volume_t *volume;           \r
-       /** pointer to the coding parameters */\r
-       opj_cp_t *cp;                           \r
-       /** helper used to write the index file */\r
-       opj_volume_info_t *volume_info; \r
-       /** pointer to the byte i/o stream */\r
-    opj_cio_t *cio;                                            \r
-} opj_j3d_t;\r
-\r
-/** @name Funciones generales */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Creates a J3D decompression structure\r
-@param cinfo Codec context info\r
-@return Returns a handle to a J3D decompressor if successful, returns NULL otherwise\r
-*/\r
-opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo);\r
-/**\r
-Destroy a J3D decompressor handle\r
-@param j3d J3D decompressor handle to destroy\r
-*/\r
-void j3d_destroy_decompress(opj_j3d_t *j3d);\r
-/**\r
-Setup the decoder decoding parameters using user parameters.\r
-Decoding parameters are returned in j3d->cp. \r
-@param j3d J3D decompressor handle\r
-@param parameters decompression parameters\r
-*/\r
-void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters);\r
-/**\r
-Decode an volume from a JPEG-2000 codestream\r
-@param j3d J3D decompressor handle\r
-@param cio Input buffer stream\r
-@return Returns a decoded volume if successful, returns NULL otherwise\r
-*/\r
-opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio);\r
-/**\r
-Decode an volume form a JPT-stream (JPEG 2000, JPIP)\r
-@param j3d J3D decompressor handle\r
-@param cio Input buffer stream\r
-@return Returns a decoded volume if successful, returns NULL otherwise\r
-*/\r
-opj_volume_t* j3d_decode_jpt_stream(opj_j3d_t *j3d, opj_cio_t *cio);\r
-/**\r
-Creates a J3D compression structure\r
-@param cinfo Codec context info\r
-@return Returns a handle to a J3D compressor if successful, returns NULL otherwise\r
-*/\r
-opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo);\r
-/**\r
-Destroy a J3D compressor handle\r
-@param j3d J3D compressor handle to destroy\r
-*/\r
-void j3d_destroy_compress(opj_j3d_t *j3d);\r
-/**\r
-Setup the encoder parameters using the current volume and using user parameters. \r
-Coding parameters are returned in j3d->cp. \r
-@param j3d J3D compressor handle\r
-@param parameters compression parameters\r
-@param volume input filled volume\r
-*/\r
-void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume);\r
-/**\r
-Encode an volume into a JPEG-2000 codestream\r
-@param j3d J3D compressor handle\r
-@param cio Output buffer stream\r
-@param volume Volume to encode\r
-@param index Name of the index file if required, NULL otherwise\r
-@return Returns true if successful, returns false otherwise\r
-*/\r
-bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index);\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __J3D_H */\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __J3D_H
+#define __J3D_H
+/**
+@file jp3d.h
+@brief The JPEG-2000 Codestream Reader/Writer (J3D)
+
+The functions in J3D.C have for goal to read/write the several parts of the codestream: markers and data.
+*/
+
+/** @defgroup J3D J3D - JPEG-2000 PART 10 codestream reader/writer */
+/*@{*/
+
+#define J3D_CP_CSTY_PRT 0x01
+#define J3D_CP_CSTY_SOP 0x02
+#define J3D_CP_CSTY_EPH 0x04
+#define J3D_CCP_CSTY_PRT 0x01
+/** Table A-8 */
+#define J3D_CCP_CBLKSTY_LAZY 0x01       /* Selective arithmetic coding bypass */
+#define J3D_CCP_CBLKSTY_RESET 0x02   /* Reset context probabilities on coding pass boundaries */
+#define J3D_CCP_CBLKSTY_TERMALL 0x04 /* Termination on each coding pass */
+#define J3D_CCP_CBLKSTY_VSC 0x08        /* Vertically causal context, add also hook for switching off and on 3D context models */      
+#define J3D_CCP_CBLKSTY_PTERM 0x10      /* Predictable termination */
+#define J3D_CCP_CBLKSTY_SEGSYM 0x20     /* Segmentation symbols are used */
+#define J3D_CCP_CBLKSTY_3DCTXT 0x40  /* 3D context models (3D-EBCOT) vs 2D context models */
+
+#define J3D_CCP_QNTSTY_NOQNT 0 /* Quantization style : no quantization */
+#define J3D_CCP_QNTSTY_SIQNT 1 /* Quantization style : scalar derived (values signalled only in LLL subband) */
+#define J3D_CCP_QNTSTY_SEQNT 2 /* Quantization style : scalar expounded (values signalled for each subband) */
+
+/* ----------------------------------------------------------------------- */
+
+#define J3D_MS_SOC 0xff4f      /**< SOC marker value */
+#define J3D_MS_SOT 0xff90      /**< SOT marker value */
+#define J3D_MS_SOD 0xff93      /**< SOD marker value */
+#define J3D_MS_EOC 0xffd9      /**< EOC marker value */
+#define J3D_MS_CAP 0xff50      /**< CAP marker value */
+#define J3D_MS_SIZ 0xff51      /**< SIZ marker value */
+#define J3D_MS_NSI 0xff54      /**< NSI marker value */
+#define J3D_MS_COD 0xff52      /**< COD marker value */
+#define J3D_MS_COC 0xff53      /**< COC marker value */
+#define J3D_MS_RGN 0xff5e      /**< RGN marker value */
+#define J3D_MS_QCD 0xff5c      /**< QCD marker value */
+#define J3D_MS_QCC 0xff5d      /**< QCC marker value */
+#define J3D_MS_POC 0xff5f      /**< POC marker value */
+#define J3D_MS_TLM 0xff55      /**< TLM marker value */
+#define J3D_MS_PLM 0xff57      /**< PLM marker value */
+#define J3D_MS_PLT 0xff58      /**< PLT marker value */
+#define J3D_MS_PPM 0xff60      /**< PPM marker value */
+#define J3D_MS_PPT 0xff61      /**< PPT marker value */
+#define J3D_MS_SOP 0xff91      /**< SOP marker value */
+#define J3D_MS_EPH 0xff92      /**< EPH marker value */
+#define J3D_MS_CRG 0xff63      /**< CRG marker value */
+#define J3D_MS_COM 0xff64      /**< COM marker value */
+/*15444-2*/
+#define J3D_MS_DCO 0xff70      /**< DCO marker value */
+#define J3D_MS_VMS 0xff71   /**< VMS marker value */
+#define J3D_MS_DFS 0xff72      /**< DFS marker value */
+#define J3D_MS_ADS 0xff73      /**< ADS marker value */
+#define J3D_MS_ATK 0xff79      /**< ATK marker value */
+#define J3D_MS_CBD 0xff78      /**< CBD marker value */
+#define J3D_MS_MCT 0xff74      /**< MCT marker value */
+#define J3D_MS_MCC 0xff75      /**< MCC marker value */
+#define J3D_MS_MCO 0xff77      /**< MCO marker value */
+#define J3D_MS_NLT 0xff76      /**< NLT marker value */
+#define J3D_MS_QPD 0xff5a      /**< QPD marker value */
+#define J3D_MS_QPC 0xff5b      /**< QPC marker value */
+
+/* ----------------------------------------------------------------------- */
+/* Capability RSIZ parameter, extended */
+#define J3D_RSIZ_BASIC 0x0000
+
+#define J3D_RSIZ_DCO   0x8001 /* Required */
+#define J3D_RSIZ_VSQNT 0x8002
+#define J3D_RSIZ_TCQNT 0x8004
+#define J3D_RSIZ_VMASK 0x8008
+#define J3D_RSIZ_SSOVL 0x8010
+#define J3D_RSIZ_ADECS 0x8020
+#define J3D_RSIZ_ATK   0x8040 /*Required*/
+#define J3D_RSIZ_SSYMK 0x8080
+#define J3D_RSIZ_MCT   0x8100 /*Not compatible with DCO*/
+#define J3D_RSIZ_NLT   0x8200 /*Required*/
+#define J3D_RSIZ_ASHAP 0x8400
+#define J3D_RSIZ_PRQNT 0x8800
+
+#define J3D_CAP_10             0x00400000
+/* Arbitrary transformation kernel, 15444-2 */
+#define J3D_ATK_IRR 0
+#define J3D_ATK_REV 1
+#define J3D_ATK_ARB 0
+#define J3D_ATK_WS 1
+#define J3D_ATK_CON 0
+/* ----------------------------------------------------------------------- */
+
+/**
+Values that specify the status of the decoding process when decoding the main header. 
+These values may be combined with a | operator. 
+*/
+typedef enum J3D_STATUS {
+       /**< a SOC marker is expected */
+       J3D_STATE_MHSOC  = 0x0001, 
+       /**< a SIZ marker is expected */
+       J3D_STATE_MHSIZ  = 0x0002, 
+       /**< the decoding process is in the main header */
+       J3D_STATE_MH     = 0x0004, 
+       /**< the decoding process is in a tile part header and expects a SOT marker */
+       J3D_STATE_TPHSOT = 0x0008, 
+       /**< the decoding process is in a tile part header */
+       J3D_STATE_TPH    = 0x0010, 
+       /**< the EOC marker has just been read */
+       J3D_STATE_MT     = 0x0020, 
+       /**< the decoding process must not expect a EOC marker because the codestream is truncated */
+       J3D_STATE_NEOC   = 0x0040  
+} J3D_STATUS;
+
+
+
+/**
+Arbitrary transformation kernel
+*/
+typedef struct opj_atk {
+/** index of wavelet kernel */
+       int index;
+/** Numerical type of scaling factor and lifting step parameters */
+       int coeff_typ;          
+/** Wavelet filter category */
+       int filt_cat;           
+/** Wavelet transformation type (REV/IRR) */
+       int wt_typ;                     
+/** Initial odd/even subsequence */
+       int minit;                      
+/** Boundary extension method (constant CON / whole-sample symmetric WS) */
+       int exten;                      
+/** Scaling factor. Only for wt_typ=IRR */
+       double Katk;                    
+/** Number of lifting steps */
+       int Natk;                       
+/** Offset for lifting step s. Only for filt_cat=ARB */
+       int Oatk[256];          
+/** Base 2 scaling exponent for lifting step s. Only for wt_typ=REV */
+       int Eatk[256];          
+/** Additive residue for lifting step s. Only for wt_typ=REV */
+       int Batk[256];          
+/** Number of lifting coefficients signaled for lifting step s  */
+       int LCatk[256]; 
+/** Lifting coefficient k for lifting step s */
+       double Aatk[256][256];  
+} opj_atk_t;
+
+
+/**
+Quantization stepsize
+*/
+typedef struct opj_stepsize {
+/** exponent */
+       int expn;       
+/** mantissa */
+       int mant;       
+} opj_stepsize_t;
+
+/**
+Tile-component coding parameters
+*/
+typedef struct opj_tccp {
+       /** coding style */
+       int csty;                                                       
+       /** number of resolutions of x, y and z-axis */
+       int numresolution[3];   
+       /** code-blocks width height & depth*/
+       int cblk[3];                    
+       /** code-block coding style */
+       int cblksty;                    
+       /** 0: no ATK (only 9-7 or 5-3) 1: ATK defined WT*/
+       int atk_wt[3];                          
+       /** Arbitrary transformation kernel (15444-2)*/
+       opj_atk_t *atk;                 
+       /** DWT identifier for x, y and z-axis (0:WT9-7 1:WT5-3 >1:WT-atk->index) */
+       int dwtid[3];
+       /** reversible/irreversible wavelet transfomation (0:irrev 1:reversible)*/      
+       int reversible;                 
+       /** quantisation style */
+       int qntsty;                             
+       /** stepsizes used for quantization */
+       opj_stepsize_t stepsizes[J3D_MAXBANDS]; 
+       /** number of guard bits. Table A28 de 15444-1*/
+       int numgbits;                   
+       /** Region Of Interest shift */
+       int roishift;                   
+       /** precinct width heigth & depth*/
+       int prctsiz[3][J3D_MAXRLVLS];           
+} opj_tccp_t;
+
+/**
+Tile coding parameters : coding/decoding parameters common to all tiles 
+(information like COD, COC in main header)
+*/
+typedef struct opj_tcp {
+/** 1 : first part-tile of a tile */
+       int first;                              
+       /** coding style */
+       int csty;                               
+       /** progression order */
+       OPJ_PROG_ORDER prg;             
+       /** number of layers */
+       int numlayers;                  
+       /** multi-component transform identifier */
+       int mct;                                
+       /** rates of layers */
+       float rates[100];                       
+       /** number of progression order changes */
+       int numpocs;                    
+       /** indicates if a POC marker has been used O:NO, 1:YES */
+       int POC;                                
+       /** progression order changes */
+       opj_poc_t pocs[J3D_MAXRLVLS - 1];
+       /** add fixed_quality */
+       float distoratio[100];
+       /** tile-component coding parameters */
+       opj_tccp_t *tccps;              
+/** packet header store there for futur use in t2_decode_packet */
+       unsigned char *ppt_data;                
+       /** pointer remaining on the first byte of the first header if ppt is used */
+       unsigned char *ppt_data_first;  
+       /** If ppt == 1 --> there was a PPT marker for the present tile */
+       int ppt;                
+       /** used in case of multiple marker PPT (number of info already stored) */
+       int ppt_store;  
+       int ppt_len;    
+} opj_tcp_t;
+
+/**
+Coding parameters
+*/
+typedef struct opj_cp {
+/** transform format 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */
+       OPJ_TRANSFORM transform_format;         
+       /** entropy coding format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI*/
+       OPJ_ENTROPY_CODING encoding_format;     
+       /** allocation by rate/distortion */
+       int disto_alloc;        
+       /** allocation by fixed layer */
+       int fixed_alloc;        
+       /** add fixed_quality */
+       int fixed_quality;      
+       /** Rsiz: capabilities */
+       int rsiz;                       
+       /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, volume is decoded to the full resolution */
+       int reduce[3];          
+       /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
+       int layer;                      
+       /** 0 = no index || 1 = index */
+       int index_on;           
+       /** Big-Endian/Little-endian order */
+       int bigendian;
+       /** XTOsiz */
+       int tx0;        
+       /** YTOsiz */
+       int ty0;                
+       /** ZTOsiz */
+       int tz0;        
+       /** XTsiz */
+       int tdx;        
+       /** YTsiz */
+       int tdy;        
+       /** ZTsiz */
+       int tdz;        
+       /** comment for coding */
+       char *comment;          
+       /** number of tiles in width, heigth and depth */
+       int tw;         
+       int th;
+       int tl;
+       /** ID number of the tiles present in the codestream */
+       int *tileno;    
+       /** size of the vector tileno */
+       int tileno_size;
+       /** tile coding parameters */
+       opj_tcp_t *tcps;
+       /** fixed layer */
+       int *matrice;           
+
+       /** packet header store there for futur use in t2_decode_packet */
+       unsigned char *ppm_data;                
+       /** pointer remaining on the first byte of the first header if ppm is used */
+       unsigned char *ppm_data_first;  
+       /** if ppm == 1 --> there was a PPM marker for the present tile */
+       int ppm;                        
+       /** use in case of multiple marker PPM (number of info already store) */
+       int ppm_store;          
+       /** use in case of multiple marker PPM (case on non-finished previous info) */
+       int ppm_previous;       
+       int ppm_len;            
+} opj_cp_t;
+
+/**
+Information concerning a packet inside tile
+*/
+typedef struct opj_packet_info {
+       /** start position */
+       int start_pos;  
+       /** end position */
+       int end_pos;    
+       /** distorsion introduced */
+       double disto;   
+} opj_packet_info_t;
+
+/**
+Index structure : information regarding tiles inside volume
+*/
+typedef struct opj_tile_info {
+       /** value of thresh for each layer by tile cfr. Marcela   */
+       double *thresh;         
+       /** number of tile */
+       int num_tile;           
+       /** start position */
+       int start_pos;          
+       /** end position of the header */
+       int end_header;         
+       /** end position */
+       int end_pos;            
+       /** precinct number for each resolution level (width, heigth and depth) */
+       int prctno[3][J3D_MAXRLVLS];    
+       /** precinct size (in power of 2), in X for each resolution level */
+       int prctsiz[3][J3D_MAXRLVLS];   
+       /** information concerning packets inside tile */
+       opj_packet_info_t *packet;              
+       
+       /** add fixed_quality */
+       int nbpix;                      
+       /** add fixed_quality */
+       double distotile;       
+} opj_tile_info_t;
+
+/**
+Index structure
+*/
+typedef struct opj_volume_info {
+       
+       /** transform format 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */
+       OPJ_TRANSFORM transform_format;         
+       /** output file format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI*/
+       OPJ_ENTROPY_CODING encoding_format;     /** 0 = no index || 1 = index */
+       int index_on;   
+       /** 0 = wt 9-7 || 1 = wt 5-3 || >=2 wt atk defined */
+       int dwtid[3];   
+       /** maximum distortion reduction on the whole volume (add for Marcela) */
+       double D_max;   
+       /** packet number */
+       int num;                
+       /** writing the packet in the index with t2_encode_packets */
+       int index_write;        
+       /** volume width, height and depth */
+       int volume_w;   
+       int volume_h;
+       int volume_l;
+       /** progression order */
+       OPJ_PROG_ORDER prog;    
+       /** tile size in x, y and z */
+       int tile_x;             
+       int tile_y;
+       int tile_z;
+       /** tile origin in x, y and z */
+       int tile_Ox;    
+       int tile_Oy;
+       int tile_Oz;
+       /** number of tiles in X, Y and Z */
+       int tw;                 
+       int th;
+       int tl;
+       /** component numbers */
+       int comp;                               
+       /** number of layer */
+       int layer;                              
+       /** number of decomposition in X, Y and Z*/
+       int decomposition[3];   
+       /** DC offset (15444-2) */
+       int dcoffset;   
+       /** main header position */
+       int main_head_end;              
+       /** codestream's size */
+       int codestream_size;    
+       /** information regarding tiles inside volume */
+       opj_tile_info_t *tile;  
+} opj_volume_info_t;
+
+/**
+JPEG-2000 codestream reader/writer
+*/
+typedef struct opj_j3d {
+       /** codec context */
+       opj_common_ptr cinfo;   
+       /** locate in which part of the codestream the decoder is (main header, tile header, end) */
+       int state;                              
+       /** number of the tile curently concern by coding/decoding */
+       int curtileno;                  
+       /** locate the position of the end of the tile in the codestream, used to detect a truncated codestream (in j3d_read_sod)       */
+       unsigned char *eot;     
+       /**     locate the start position of the SOT marker of the current coded tile:  */
+       int sot_start;          
+       /*  after encoding the tile, a jump (in j3d_write_sod) is done to the SOT marker to store the value of its length. */
+       int sod_start;          
+       /**     as the J3D-file is written in several parts during encoding, it enables to make the right correction in position return by cio_tell     */
+       int pos_correction;     
+       /** array used to store the data of each tile */
+       unsigned char **tile_data;      
+       /** array used to store the length of each tile */
+       int *tile_len;                          
+
+       /** decompression only : store decoding parameters common to all tiles */
+       opj_tcp_t *default_tcp;         
+       /** pointer to the encoded / decoded volume */
+       opj_volume_t *volume;           
+       /** pointer to the coding parameters */
+       opj_cp_t *cp;                           
+       /** helper used to write the index file */
+       opj_volume_info_t *volume_info; 
+       /** pointer to the byte i/o stream */
+    opj_cio_t *cio;                                            
+} opj_j3d_t;
+
+/** @name Funciones generales */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Creates a J3D decompression structure
+@param cinfo Codec context info
+@return Returns a handle to a J3D decompressor if successful, returns NULL otherwise
+*/
+opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo);
+/**
+Destroy a J3D decompressor handle
+@param j3d J3D decompressor handle to destroy
+*/
+void j3d_destroy_decompress(opj_j3d_t *j3d);
+/**
+Setup the decoder decoding parameters using user parameters.
+Decoding parameters are returned in j3d->cp. 
+@param j3d J3D decompressor handle
+@param parameters decompression parameters
+*/
+void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters);
+/**
+Decode an volume from a JPEG-2000 codestream
+@param j3d J3D decompressor handle
+@param cio Input buffer stream
+@return Returns a decoded volume if successful, returns NULL otherwise
+*/
+opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio);
+/**
+Decode an volume form a JPT-stream (JPEG 2000, JPIP)
+@param j3d J3D decompressor handle
+@param cio Input buffer stream
+@return Returns a decoded volume if successful, returns NULL otherwise
+*/
+opj_volume_t* j3d_decode_jpt_stream(opj_j3d_t *j3d, opj_cio_t *cio);
+/**
+Creates a J3D compression structure
+@param cinfo Codec context info
+@return Returns a handle to a J3D compressor if successful, returns NULL otherwise
+*/
+opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo);
+/**
+Destroy a J3D compressor handle
+@param j3d J3D compressor handle to destroy
+*/
+void j3d_destroy_compress(opj_j3d_t *j3d);
+/**
+Setup the encoder parameters using the current volume and using user parameters. 
+Coding parameters are returned in j3d->cp. 
+@param j3d J3D compressor handle
+@param parameters compression parameters
+@param volume input filled volume
+*/
+void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume);
+/**
+Encode an volume into a JPEG-2000 codestream
+@param j3d J3D compressor handle
+@param cio Output buffer stream
+@param volume Volume to encode
+@param index Name of the index file if required, NULL otherwise
+@return Returns true if successful, returns false otherwise
+*/
+bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __J3D_H */
index 4445e8594a95fc78bc8bc27dc31967d058de8cb4..d63a9b0d94271aaa801f55af1a2a1a492db6f55f 100644 (file)
@@ -1,76 +1,76 @@
-/*\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifdef _WIN32\r
-#include <windows.h>\r
-#else\r
-#include <sys/time.h>\r
-#include <sys/resource.h>\r
-#include <sys/times.h>\r
-#endif /* _WIN32 */\r
-#include "opj_includes.h"\r
-\r
-double opj_clock() {\r
-#ifdef _WIN32\r
-       /* WIN32: use QueryPerformance (very accurate) */\r
-    LARGE_INTEGER freq , t ;\r
-    /* freq is the clock speed of the CPU */\r
-    QueryPerformanceFrequency(&freq) ;\r
-       /* cout << "freq = " << ((double) freq.QuadPart) << endl; */\r
-    /* t is the high resolution performance counter (see MSDN) */\r
-    QueryPerformanceCounter ( & t ) ;\r
-    return ( t.QuadPart /(double) freq.QuadPart ) ;\r
-#else\r
-       /* Unix or Linux: use resource usage */\r
-    struct rusage t;\r
-    double procTime;\r
-    /* (1) Get the rusage data structure at this moment (man getrusage) */\r
-    getrusage(0,&t);\r
-    /* (2) What is the elapsed time ? - CPU time = User time + System time */\r
-       /* (2a) Get the seconds */\r
-    procTime = t.ru_utime.tv_sec + t.ru_stime.tv_sec;\r
-    /* (2b) More precisely! Get the microseconds part ! */\r
-    return ( procTime + (t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ;\r
-#endif /* _WIN32 */\r
-}\r
-\r
-void* opj_malloc( size_t size ) {\r
-       void *memblock = malloc(size);\r
-       if(memblock) {\r
-               memset(memblock, 0, size);\r
-       }\r
-       return memblock;\r
-}\r
-\r
-void* opj_realloc( void *memblock, size_t size ) {\r
-       return realloc(memblock, size);\r
-}\r
-\r
-void opj_free( void *memblock ) {\r
-       free(memblock);\r
-}\r
-\r
-\r
+/*
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/times.h>
+#endif /* _WIN32 */
+#include "opj_includes.h"
+
+double opj_clock() {
+#ifdef _WIN32
+       /* WIN32: use QueryPerformance (very accurate) */
+    LARGE_INTEGER freq , t ;
+    /* freq is the clock speed of the CPU */
+    QueryPerformanceFrequency(&freq) ;
+       /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
+    /* t is the high resolution performance counter (see MSDN) */
+    QueryPerformanceCounter ( & t ) ;
+    return ( t.QuadPart /(double) freq.QuadPart ) ;
+#else
+       /* Unix or Linux: use resource usage */
+    struct rusage t;
+    double procTime;
+    /* (1) Get the rusage data structure at this moment (man getrusage) */
+    getrusage(0,&t);
+    /* (2) What is the elapsed time ? - CPU time = User time + System time */
+       /* (2a) Get the seconds */
+    procTime = t.ru_utime.tv_sec + t.ru_stime.tv_sec;
+    /* (2b) More precisely! Get the microseconds part ! */
+    return ( procTime + (t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ;
+#endif /* _WIN32 */
+}
+
+void* opj_malloc( size_t size ) {
+       void *memblock = malloc(size);
+       if(memblock) {
+               memset(memblock, 0, size);
+       }
+       return memblock;
+}
+
+void* opj_realloc( void *memblock, size_t size ) {
+       return realloc(memblock, size);
+}
+
+void opj_free( void *memblock ) {
+       free(memblock);
+}
+
+
index dae43275bdf88a102da3cc4ad6e652fd3347af94..1b910ae31d5f9e03d12851dde01c3263aa64070c 100644 (file)
@@ -1,75 +1,75 @@
-/*\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-#ifndef __J3D_LIB_H\r
-#define __J3D_LIB_H\r
-/**\r
-@file jp3d_lib.h\r
-@brief Internal functions\r
-\r
-The functions in JP3D_LIB.C are internal utilities mainly used for memory management.\r
-*/\r
-\r
-/** @defgroup MISC MISC - Miscellaneous internal functions */\r
-/*@{*/\r
-\r
-/** @name Funciones generales */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/**\r
-Difference in successive opj_clock() calls tells you the elapsed time\r
-@return Returns time in seconds\r
-*/\r
-double opj_clock(void);\r
-\r
-/**\r
-Allocate a memory block with elements initialized to 0\r
-@param size Bytes to allocate\r
-@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available\r
-*/\r
-void* opj_malloc( size_t size );\r
-\r
-/**\r
-Reallocate memory blocks.\r
-@param memblock Pointer to previously allocated memory block\r
-@param size New size in bytes\r
-@return Returns a void pointer to the reallocated (and possibly moved) memory block\r
-*/\r
-void* opj_realloc( void *memblock, size_t size );\r
-\r
-/**\r
-Deallocates or frees a memory block.\r
-@param memblock Previously allocated memory block to be freed\r
-*/\r
-void opj_free( void *memblock );\r
-\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __J3D_LIB_H */\r
-\r
+/*
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __J3D_LIB_H
+#define __J3D_LIB_H
+/**
+@file jp3d_lib.h
+@brief Internal functions
+
+The functions in JP3D_LIB.C are internal utilities mainly used for memory management.
+*/
+
+/** @defgroup MISC MISC - Miscellaneous internal functions */
+/*@{*/
+
+/** @name Funciones generales */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+Difference in successive opj_clock() calls tells you the elapsed time
+@return Returns time in seconds
+*/
+double opj_clock(void);
+
+/**
+Allocate a memory block with elements initialized to 0
+@param size Bytes to allocate
+@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
+*/
+void* opj_malloc( size_t size );
+
+/**
+Reallocate memory blocks.
+@param memblock Pointer to previously allocated memory block
+@param size New size in bytes
+@return Returns a void pointer to the reallocated (and possibly moved) memory block
+*/
+void* opj_realloc( void *memblock, size_t size );
+
+/**
+Deallocates or frees a memory block.
+@param memblock Previously allocated memory block to be freed
+*/
+void opj_free( void *memblock );
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __J3D_LIB_H */
+
index e915e39092469473349bc496e0de651799def4c8..ccccdbc4a9001e89ed1a69ac76d82c66c5cc0bd2 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/* <summary> */\r
-/* This table contains the norms of the basis function of the reversible MCT. */\r
-/* </summary> */\r
-static const double mct_norms[3] = { 1.732, .8292, .8292 };\r
-\r
-/* <summary> */\r
-/* This table contains the norms of the basis function of the irreversible MCT. */\r
-/* </summary> */\r
-static const double mct_norms_real[3] = { 1.732, 1.805, 1.573 };\r
-\r
-/* <summary> */\r
-/* Foward reversible MCT. */\r
-/* </summary> */\r
-void mct_encode(int *c0, int *c1, int *c2, int n) {\r
-       int i;\r
-       for (i = 0; i < n; i++) {\r
-               int r, g, b, y, u, v;\r
-               r = c0[i];\r
-               g = c1[i];\r
-               b = c2[i];\r
-               y = (r + (g << 1) + b) >> 2;\r
-               u = b - g;\r
-               v = r - g;\r
-               c0[i] = y;\r
-               c1[i] = u;\r
-               c2[i] = v;\r
-       }\r
-}\r
-\r
-/* <summary> */\r
-/* Inverse reversible MCT. */\r
-/* </summary> */\r
-void mct_decode(int *c0, int *c1, int *c2, int n) {\r
-       int i;\r
-       for (i = 0; i < n; i++) {\r
-               int y, u, v, r, g, b;\r
-               y = c0[i];\r
-               u = c1[i];\r
-               v = c2[i];\r
-               g = y - ((u + v) >> 2);\r
-               r = v + g;\r
-               b = u + g;\r
-               c0[i] = r;\r
-               c1[i] = g;\r
-               c2[i] = b;\r
-       }\r
-}\r
-\r
-/* <summary> */\r
-/* Get norm of basis function of reversible MCT. */\r
-/* </summary> */\r
-double mct_getnorm(int compno) {\r
-       return mct_norms[compno];\r
-}\r
-\r
-/* <summary> */\r
-/* Foward irreversible MCT. */\r
-/* </summary> */\r
-void mct_encode_real(int *c0, int *c1, int *c2, int n) {\r
-       int i;\r
-       for (i = 0; i < n; i++) {\r
-               int r, g, b, y, u, v;\r
-               r = c0[i];\r
-               g = c1[i];\r
-               b = c2[i];\r
-               y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934);\r
-               u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096);\r
-               v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666);\r
-               c0[i] = y;\r
-               c1[i] = u;\r
-               c2[i] = v;\r
-       }\r
-}\r
-\r
-/* <summary> */\r
-/* Inverse irreversible MCT. */\r
-/* </summary> */\r
-void mct_decode_real(int *c0, int *c1, int *c2, int n) {\r
-       int i;\r
-       for (i = 0; i < n; i++) {\r
-               int y, u, v, r, g, b;\r
-               y = c0[i];\r
-               u = c1[i];\r
-               v = c2[i];\r
-               r = y + fix_mul(v, 11485);\r
-               g = y - fix_mul(u, 2819) - fix_mul(v, 5850);\r
-               b = y + fix_mul(u, 14516);\r
-               c0[i] = r;\r
-               c1[i] = g;\r
-               c2[i] = b;\r
-       }\r
-}\r
-\r
-/* <summary> */\r
-/* Get norm of basis function of irreversible MCT. */\r
-/* </summary> */\r
-double mct_getnorm_real(int compno) {\r
-       return mct_norms_real[compno];\r
-}\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/* <summary> */
+/* This table contains the norms of the basis function of the reversible MCT. */
+/* </summary> */
+static const double mct_norms[3] = { 1.732, .8292, .8292 };
+
+/* <summary> */
+/* This table contains the norms of the basis function of the irreversible MCT. */
+/* </summary> */
+static const double mct_norms_real[3] = { 1.732, 1.805, 1.573 };
+
+/* <summary> */
+/* Foward reversible MCT. */
+/* </summary> */
+void mct_encode(int *c0, int *c1, int *c2, int n) {
+       int i;
+       for (i = 0; i < n; i++) {
+               int r, g, b, y, u, v;
+               r = c0[i];
+               g = c1[i];
+               b = c2[i];
+               y = (r + (g << 1) + b) >> 2;
+               u = b - g;
+               v = r - g;
+               c0[i] = y;
+               c1[i] = u;
+               c2[i] = v;
+       }
+}
+
+/* <summary> */
+/* Inverse reversible MCT. */
+/* </summary> */
+void mct_decode(int *c0, int *c1, int *c2, int n) {
+       int i;
+       for (i = 0; i < n; i++) {
+               int y, u, v, r, g, b;
+               y = c0[i];
+               u = c1[i];
+               v = c2[i];
+               g = y - ((u + v) >> 2);
+               r = v + g;
+               b = u + g;
+               c0[i] = r;
+               c1[i] = g;
+               c2[i] = b;
+       }
+}
+
+/* <summary> */
+/* Get norm of basis function of reversible MCT. */
+/* </summary> */
+double mct_getnorm(int compno) {
+       return mct_norms[compno];
+}
+
+/* <summary> */
+/* Foward irreversible MCT. */
+/* </summary> */
+void mct_encode_real(int *c0, int *c1, int *c2, int n) {
+       int i;
+       for (i = 0; i < n; i++) {
+               int r, g, b, y, u, v;
+               r = c0[i];
+               g = c1[i];
+               b = c2[i];
+               y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934);
+               u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096);
+               v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666);
+               c0[i] = y;
+               c1[i] = u;
+               c2[i] = v;
+       }
+}
+
+/* <summary> */
+/* Inverse irreversible MCT. */
+/* </summary> */
+void mct_decode_real(int *c0, int *c1, int *c2, int n) {
+       int i;
+       for (i = 0; i < n; i++) {
+               int y, u, v, r, g, b;
+               y = c0[i];
+               u = c1[i];
+               v = c2[i];
+               r = y + fix_mul(v, 11485);
+               g = y - fix_mul(u, 2819) - fix_mul(v, 5850);
+               b = y + fix_mul(u, 14516);
+               c0[i] = r;
+               c1[i] = g;
+               c2[i] = b;
+       }
+}
+
+/* <summary> */
+/* Get norm of basis function of irreversible MCT. */
+/* </summary> */
+double mct_getnorm_real(int compno) {
+       return mct_norms_real[compno];
+}
index 0b8edb7bf46a22a524183cefb64ae2ec3de94a53..e843aec9b9d346e23d4267c260c271d71d594c38 100644 (file)
@@ -1,97 +1,97 @@
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __MCT_H\r
-#define __MCT_H\r
-/**\r
-@file mct.h\r
-@brief Implementation of a multi-component transforms (MCT)\r
-\r
-The functions in MCT.C have for goal to realize reversible and irreversible multicomponent\r
-transform. The functions in MCT.C are used by some function in TCD.C.\r
-*/\r
-\r
-/** @defgroup MCT MCT - Implementation of a multi-component transform */\r
-/*@{*/\r
-\r
-/** @name Funciones generales */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Apply a reversible multi-component transform to an image\r
-@param c0 Samples for red component\r
-@param c1 Samples for green component\r
-@param c2 Samples blue component\r
-@param n Number of samples for each component\r
-*/\r
-void mct_encode(int *c0, int *c1, int *c2, int n);\r
-/**\r
-Apply a reversible multi-component inverse transform to an image\r
-@param c0 Samples for luminance component\r
-@param c1 Samples for red chrominance component\r
-@param c2 Samples for blue chrominance component\r
-@param n Number of samples for each component\r
-*/\r
-void mct_decode(int *c0, int *c1, int *c2, int n);\r
-/**\r
-Get norm of the basis function used for the reversible multi-component transform\r
-@param compno Number of the component (0->Y, 1->U, 2->V)\r
-@return \r
-*/\r
-double mct_getnorm(int compno);\r
-\r
-/**\r
-Apply an irreversible multi-component transform to an image\r
-@param c0 Samples for red component\r
-@param c1 Samples for green component\r
-@param c2 Samples blue component\r
-@param n Number of samples for each component\r
-*/\r
-void mct_encode_real(int *c0, int *c1, int *c2, int n);\r
-/**\r
-Apply an irreversible multi-component inverse transform to an image\r
-@param c0 Samples for luminance component\r
-@param c1 Samples for red chrominance component\r
-@param c2 Samples for blue chrominance component\r
-@param n Number of samples for each component\r
-*/\r
-void mct_decode_real(int *c0, int *c1, int *c2, int n);\r
-/**\r
-Get norm of the basis function used for the irreversible multi-component transform\r
-@param compno Number of the component (0->Y, 1->U, 2->V)\r
-@return \r
-*/\r
-double mct_getnorm_real(int compno);\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __MCT_H */\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MCT_H
+#define __MCT_H
+/**
+@file mct.h
+@brief Implementation of a multi-component transforms (MCT)
+
+The functions in MCT.C have for goal to realize reversible and irreversible multicomponent
+transform. The functions in MCT.C are used by some function in TCD.C.
+*/
+
+/** @defgroup MCT MCT - Implementation of a multi-component transform */
+/*@{*/
+
+/** @name Funciones generales */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Apply a reversible multi-component transform to an image
+@param c0 Samples for red component
+@param c1 Samples for green component
+@param c2 Samples blue component
+@param n Number of samples for each component
+*/
+void mct_encode(int *c0, int *c1, int *c2, int n);
+/**
+Apply a reversible multi-component inverse transform to an image
+@param c0 Samples for luminance component
+@param c1 Samples for red chrominance component
+@param c2 Samples for blue chrominance component
+@param n Number of samples for each component
+*/
+void mct_decode(int *c0, int *c1, int *c2, int n);
+/**
+Get norm of the basis function used for the reversible multi-component transform
+@param compno Number of the component (0->Y, 1->U, 2->V)
+@return 
+*/
+double mct_getnorm(int compno);
+
+/**
+Apply an irreversible multi-component transform to an image
+@param c0 Samples for red component
+@param c1 Samples for green component
+@param c2 Samples blue component
+@param n Number of samples for each component
+*/
+void mct_encode_real(int *c0, int *c1, int *c2, int n);
+/**
+Apply an irreversible multi-component inverse transform to an image
+@param c0 Samples for luminance component
+@param c1 Samples for red chrominance component
+@param c2 Samples for blue chrominance component
+@param n Number of samples for each component
+*/
+void mct_decode_real(int *c0, int *c1, int *c2, int n);
+/**
+Get norm of the basis function used for the irreversible multi-component transform
+@param compno Number of the component (0->Y, 1->U, 2->V)
+@return 
+*/
+double mct_getnorm_real(int compno);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __MCT_H */
index 12ac90d693fe11fa2768acd11ea546b82b02b1cb..874b7340331ee72b95dd597503bd68638c67c440 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/** @defgroup MQC MQC - Implementation of an MQ-Coder */\r
-/*@{*/\r
-\r
-/** @name Local static functions */\r
-/*@{*/\r
-\r
-/**\r
-Output a byte, doing bit-stuffing if necessary.\r
-After a 0xff byte, the next byte must be smaller than 0x90.\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_byteout(opj_mqc_t *mqc);\r
-/**\r
-Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_renorme(opj_mqc_t *mqc);\r
-/**\r
-Encode the most probable symbol\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_codemps(opj_mqc_t *mqc);\r
-/**\r
-Encode the most least symbol\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_codelps(opj_mqc_t *mqc);\r
-/**\r
-Fill mqc->c with 1's for flushing\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_setbits(opj_mqc_t *mqc);\r
-/**\r
-Exchange MPS with LPS\r
-@param mqc MQC handle\r
-@return \r
-*/\r
-static int mqc_mpsexchange(opj_mqc_t *mqc);\r
-/**\r
-Exchange LPS with MPS\r
-@param mqc MQC handle\r
-@return \r
-*/\r
-static int mqc_lpsexchange(opj_mqc_t *mqc);\r
-/**\r
-Input a byte\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_bytein(opj_mqc_t *mqc);\r
-/**\r
-Renormalize mqc->a and mqc->c while decoding\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_renormd(opj_mqc_t *mqc);\r
-\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-/* <summary> */\r
-/* This array defines all the possible states for a context. */\r
-/* </summary> */\r
-static opj_mqc_state_t mqc_states[47 * 2] = {\r
-       {0x5601, 0, &mqc_states[2], &mqc_states[3]},\r
-       {0x5601, 1, &mqc_states[3], &mqc_states[2]},\r
-       {0x3401, 0, &mqc_states[4], &mqc_states[12]},\r
-       {0x3401, 1, &mqc_states[5], &mqc_states[13]},\r
-       {0x1801, 0, &mqc_states[6], &mqc_states[18]},\r
-       {0x1801, 1, &mqc_states[7], &mqc_states[19]},\r
-       {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},\r
-       {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},\r
-       {0x0521, 0, &mqc_states[10], &mqc_states[58]},\r
-       {0x0521, 1, &mqc_states[11], &mqc_states[59]},\r
-       {0x0221, 0, &mqc_states[76], &mqc_states[66]},\r
-       {0x0221, 1, &mqc_states[77], &mqc_states[67]},\r
-       {0x5601, 0, &mqc_states[14], &mqc_states[13]},\r
-       {0x5601, 1, &mqc_states[15], &mqc_states[12]},\r
-       {0x5401, 0, &mqc_states[16], &mqc_states[28]},\r
-       {0x5401, 1, &mqc_states[17], &mqc_states[29]},\r
-       {0x4801, 0, &mqc_states[18], &mqc_states[28]},\r
-       {0x4801, 1, &mqc_states[19], &mqc_states[29]},\r
-       {0x3801, 0, &mqc_states[20], &mqc_states[28]},\r
-       {0x3801, 1, &mqc_states[21], &mqc_states[29]},\r
-       {0x3001, 0, &mqc_states[22], &mqc_states[34]},\r
-       {0x3001, 1, &mqc_states[23], &mqc_states[35]},\r
-       {0x2401, 0, &mqc_states[24], &mqc_states[36]},\r
-       {0x2401, 1, &mqc_states[25], &mqc_states[37]},\r
-       {0x1c01, 0, &mqc_states[26], &mqc_states[40]},\r
-       {0x1c01, 1, &mqc_states[27], &mqc_states[41]},\r
-       {0x1601, 0, &mqc_states[58], &mqc_states[42]},\r
-       {0x1601, 1, &mqc_states[59], &mqc_states[43]},\r
-       {0x5601, 0, &mqc_states[30], &mqc_states[29]},\r
-       {0x5601, 1, &mqc_states[31], &mqc_states[28]},\r
-       {0x5401, 0, &mqc_states[32], &mqc_states[28]},\r
-       {0x5401, 1, &mqc_states[33], &mqc_states[29]},\r
-       {0x5101, 0, &mqc_states[34], &mqc_states[30]},\r
-       {0x5101, 1, &mqc_states[35], &mqc_states[31]},\r
-       {0x4801, 0, &mqc_states[36], &mqc_states[32]},\r
-       {0x4801, 1, &mqc_states[37], &mqc_states[33]},\r
-       {0x3801, 0, &mqc_states[38], &mqc_states[34]},\r
-       {0x3801, 1, &mqc_states[39], &mqc_states[35]},\r
-       {0x3401, 0, &mqc_states[40], &mqc_states[36]},\r
-       {0x3401, 1, &mqc_states[41], &mqc_states[37]},\r
-       {0x3001, 0, &mqc_states[42], &mqc_states[38]},\r
-       {0x3001, 1, &mqc_states[43], &mqc_states[39]},\r
-       {0x2801, 0, &mqc_states[44], &mqc_states[38]},\r
-       {0x2801, 1, &mqc_states[45], &mqc_states[39]},\r
-       {0x2401, 0, &mqc_states[46], &mqc_states[40]},\r
-       {0x2401, 1, &mqc_states[47], &mqc_states[41]},\r
-       {0x2201, 0, &mqc_states[48], &mqc_states[42]},\r
-       {0x2201, 1, &mqc_states[49], &mqc_states[43]},\r
-       {0x1c01, 0, &mqc_states[50], &mqc_states[44]},\r
-       {0x1c01, 1, &mqc_states[51], &mqc_states[45]},\r
-       {0x1801, 0, &mqc_states[52], &mqc_states[46]},\r
-       {0x1801, 1, &mqc_states[53], &mqc_states[47]},\r
-       {0x1601, 0, &mqc_states[54], &mqc_states[48]},\r
-       {0x1601, 1, &mqc_states[55], &mqc_states[49]},\r
-       {0x1401, 0, &mqc_states[56], &mqc_states[50]},\r
-       {0x1401, 1, &mqc_states[57], &mqc_states[51]},\r
-       {0x1201, 0, &mqc_states[58], &mqc_states[52]},\r
-       {0x1201, 1, &mqc_states[59], &mqc_states[53]},\r
-       {0x1101, 0, &mqc_states[60], &mqc_states[54]},\r
-       {0x1101, 1, &mqc_states[61], &mqc_states[55]},\r
-       {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},\r
-       {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},\r
-       {0x09c1, 0, &mqc_states[64], &mqc_states[58]},\r
-       {0x09c1, 1, &mqc_states[65], &mqc_states[59]},\r
-       {0x08a1, 0, &mqc_states[66], &mqc_states[60]},\r
-       {0x08a1, 1, &mqc_states[67], &mqc_states[61]},\r
-       {0x0521, 0, &mqc_states[68], &mqc_states[62]},\r
-       {0x0521, 1, &mqc_states[69], &mqc_states[63]},\r
-       {0x0441, 0, &mqc_states[70], &mqc_states[64]},\r
-       {0x0441, 1, &mqc_states[71], &mqc_states[65]},\r
-       {0x02a1, 0, &mqc_states[72], &mqc_states[66]},\r
-       {0x02a1, 1, &mqc_states[73], &mqc_states[67]},\r
-       {0x0221, 0, &mqc_states[74], &mqc_states[68]},\r
-       {0x0221, 1, &mqc_states[75], &mqc_states[69]},\r
-       {0x0141, 0, &mqc_states[76], &mqc_states[70]},\r
-       {0x0141, 1, &mqc_states[77], &mqc_states[71]},\r
-       {0x0111, 0, &mqc_states[78], &mqc_states[72]},\r
-       {0x0111, 1, &mqc_states[79], &mqc_states[73]},\r
-       {0x0085, 0, &mqc_states[80], &mqc_states[74]},\r
-       {0x0085, 1, &mqc_states[81], &mqc_states[75]},\r
-       {0x0049, 0, &mqc_states[82], &mqc_states[76]},\r
-       {0x0049, 1, &mqc_states[83], &mqc_states[77]},\r
-       {0x0025, 0, &mqc_states[84], &mqc_states[78]},\r
-       {0x0025, 1, &mqc_states[85], &mqc_states[79]},\r
-       {0x0015, 0, &mqc_states[86], &mqc_states[80]},\r
-       {0x0015, 1, &mqc_states[87], &mqc_states[81]},\r
-       {0x0009, 0, &mqc_states[88], &mqc_states[82]},\r
-       {0x0009, 1, &mqc_states[89], &mqc_states[83]},\r
-       {0x0005, 0, &mqc_states[90], &mqc_states[84]},\r
-       {0x0005, 1, &mqc_states[91], &mqc_states[85]},\r
-       {0x0001, 0, &mqc_states[90], &mqc_states[86]},\r
-       {0x0001, 1, &mqc_states[91], &mqc_states[87]},\r
-       {0x5601, 0, &mqc_states[92], &mqc_states[92]},\r
-       {0x5601, 1, &mqc_states[93], &mqc_states[93]},\r
-};\r
-\r
-/* \r
-==========================================================\r
-   local functions\r
-==========================================================\r
-*/\r
-\r
-static void mqc_byteout(opj_mqc_t *mqc) {\r
-       if (*mqc->bp == 0xff) {\r
-               mqc->bp++;\r
-               *mqc->bp = mqc->c >> 20;\r
-               mqc->c &= 0xfffff;\r
-               mqc->ct = 7;\r
-       } else {\r
-               if ((mqc->c & 0x8000000) == 0) {        /* ((mqc->c&0x8000000)==0) CHANGE */\r
-                       mqc->bp++;\r
-                       *mqc->bp = mqc->c >> 19;\r
-                       mqc->c &= 0x7ffff;\r
-                       mqc->ct = 8;\r
-               } else {\r
-                       (*mqc->bp)++;\r
-                       if (*mqc->bp == 0xff) {\r
-                               mqc->c &= 0x7ffffff;\r
-                               mqc->bp++;\r
-                               *mqc->bp = mqc->c >> 20;\r
-                               mqc->c &= 0xfffff;\r
-                               mqc->ct = 7;\r
-                       } else {\r
-                               mqc->bp++;\r
-                               *mqc->bp = mqc->c >> 19;\r
-                               mqc->c &= 0x7ffff;\r
-                               mqc->ct = 8;\r
-                       }\r
-               }\r
-       }\r
-}\r
-\r
-static void mqc_renorme(opj_mqc_t *mqc) {\r
-       do {\r
-               mqc->a <<= 1;\r
-               mqc->c <<= 1;\r
-               mqc->ct--;\r
-               if (mqc->ct == 0) {\r
-                       mqc_byteout(mqc);\r
-               }\r
-       } while ((mqc->a & 0x8000) == 0);\r
-}\r
-\r
-static void mqc_codemps(opj_mqc_t *mqc) {\r
-       mqc->a -= (*mqc->curctx)->qeval;\r
-       if ((mqc->a & 0x8000) == 0) {\r
-               if (mqc->a < (*mqc->curctx)->qeval) {\r
-                       mqc->a = (*mqc->curctx)->qeval;\r
-               } else {\r
-                       mqc->c += (*mqc->curctx)->qeval;\r
-               }\r
-               *mqc->curctx = (*mqc->curctx)->nmps;\r
-               mqc_renorme(mqc);\r
-       } else {\r
-               mqc->c += (*mqc->curctx)->qeval;\r
-       }\r
-}\r
-\r
-static void mqc_codelps(opj_mqc_t *mqc) {\r
-       mqc->a -= (*mqc->curctx)->qeval;\r
-       if (mqc->a < (*mqc->curctx)->qeval) {\r
-               mqc->c += (*mqc->curctx)->qeval;\r
-       } else {\r
-               mqc->a = (*mqc->curctx)->qeval;\r
-       }\r
-       *mqc->curctx = (*mqc->curctx)->nlps;\r
-       mqc_renorme(mqc);\r
-}\r
-\r
-static void mqc_setbits(opj_mqc_t *mqc) {\r
-       unsigned int tempc = mqc->c + mqc->a;\r
-       mqc->c |= 0xffff;\r
-       if (mqc->c >= tempc) {\r
-               mqc->c -= 0x8000;\r
-       }\r
-}\r
-\r
-static int mqc_mpsexchange(opj_mqc_t *mqc) {\r
-       int d;\r
-       if (mqc->a < (*mqc->curctx)->qeval) {\r
-               d = 1 - (*mqc->curctx)->mps;\r
-               *mqc->curctx = (*mqc->curctx)->nlps;\r
-       } else {\r
-               d = (*mqc->curctx)->mps;\r
-               *mqc->curctx = (*mqc->curctx)->nmps;\r
-       }\r
-       \r
-       return d;\r
-}\r
-\r
-static int mqc_lpsexchange(opj_mqc_t *mqc) {\r
-       int d;\r
-       if (mqc->a < (*mqc->curctx)->qeval) {\r
-               mqc->a = (*mqc->curctx)->qeval;\r
-               d = (*mqc->curctx)->mps;\r
-               *mqc->curctx = (*mqc->curctx)->nmps;\r
-       } else {\r
-               mqc->a = (*mqc->curctx)->qeval;\r
-               d = 1 - (*mqc->curctx)->mps;\r
-               *mqc->curctx = (*mqc->curctx)->nlps;\r
-       }\r
-       \r
-       return d;\r
-}\r
-\r
-static void mqc_bytein(opj_mqc_t *mqc) {\r
-       if (mqc->bp != mqc->end) {\r
-               unsigned int c;\r
-               if (mqc->bp + 1 != mqc->end) {\r
-                       c = *(mqc->bp + 1);\r
-               } else {\r
-                       c = 0xff;\r
-               }\r
-               if (*mqc->bp == 0xff) {\r
-                       if (c > 0x8f) {\r
-                               mqc->c += 0xff00;\r
-                               mqc->ct = 8;\r
-                       } else {\r
-                               mqc->bp++;\r
-                               mqc->c += c << 9;\r
-                               mqc->ct = 7;\r
-                       }\r
-               } else {\r
-                       mqc->bp++;\r
-                       mqc->c += c << 8;\r
-                       mqc->ct = 8;\r
-               }\r
-       } else {\r
-               mqc->c += 0xff00;\r
-               mqc->ct = 8;\r
-       }\r
-}\r
-\r
-static void mqc_renormd(opj_mqc_t *mqc) {\r
-       do {\r
-               if (mqc->ct == 0) {\r
-                       mqc_bytein(mqc);\r
-               }\r
-               mqc->a <<= 1;\r
-               mqc->c <<= 1;\r
-               mqc->ct--;\r
-       } while (mqc->a < 0x8000);\r
-}\r
-\r
-/* \r
-==========================================================\r
-   MQ-Coder interface\r
-==========================================================\r
-*/\r
-\r
-opj_mqc_t* mqc_create() {\r
-       opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));\r
-       return mqc;\r
-}\r
-\r
-void mqc_destroy(opj_mqc_t *mqc) {\r
-       if(mqc) {\r
-               opj_free(mqc);\r
-       }\r
-}\r
-\r
-int mqc_numbytes(opj_mqc_t *mqc) {\r
-       return mqc->bp - mqc->start;\r
-}\r
-\r
-void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) {\r
-       mqc_setcurctx(mqc, 0);\r
-       mqc->a = 0x8000;\r
-       mqc->c = 0;\r
-       mqc->bp = bp - 1;\r
-       mqc->ct = 12;\r
-       if (*mqc->bp == 0xff) {\r
-               mqc->ct = 13;\r
-       }\r
-       mqc->start = bp;\r
-}\r
-\r
-void mqc_setcurctx(opj_mqc_t *mqc, int ctxno) {\r
-       mqc->curctx = &mqc->ctxs[ctxno];\r
-}\r
-\r
-void mqc_encode(opj_mqc_t *mqc, int d) {\r
-       if ((*mqc->curctx)->mps == d) {\r
-               mqc_codemps(mqc);\r
-       } else {\r
-               mqc_codelps(mqc);\r
-       }\r
-}\r
-\r
-void mqc_flush(opj_mqc_t *mqc) {\r
-       mqc_setbits(mqc);\r
-       mqc->c <<= mqc->ct;\r
-       mqc_byteout(mqc);\r
-       mqc->c <<= mqc->ct;\r
-       mqc_byteout(mqc);\r
-       \r
-       if (*mqc->bp != 0xff) {\r
-               mqc->bp++;\r
-       }\r
-}\r
-\r
-void mqc_bypass_init_enc(opj_mqc_t *mqc) {\r
-       mqc->c = 0;\r
-       mqc->ct = 8;\r
-       /*if (*mqc->bp == 0xff) {\r
-       mqc->ct = 7;\r
-     } */\r
-}\r
-\r
-void mqc_bypass_enc(opj_mqc_t *mqc, int d) {\r
-       mqc->ct--;\r
-       mqc->c = mqc->c + (d << mqc->ct);\r
-       if (mqc->ct == 0) {\r
-               mqc->bp++;\r
-               *mqc->bp = mqc->c;\r
-               mqc->ct = 8;\r
-               if (*mqc->bp == 0xff) {\r
-                       mqc->ct = 7;\r
-               }\r
-               mqc->c = 0;\r
-       }\r
-}\r
-\r
-int mqc_bypass_flush_enc(opj_mqc_t *mqc) {\r
-       unsigned char bit_padding;\r
-       \r
-       bit_padding = 0;\r
-       \r
-       if (mqc->ct != 0) {\r
-               while (mqc->ct > 0) {\r
-                       mqc->ct--;\r
-                       mqc->c += bit_padding << mqc->ct;\r
-                       bit_padding = (bit_padding + 1) & 0x01;\r
-               }\r
-               mqc->bp++;\r
-               *mqc->bp = mqc->c;\r
-               mqc->ct = 8;\r
-               mqc->c = 0;\r
-       }\r
-       \r
-       return 1;\r
-}\r
-\r
-void mqc_reset_enc(opj_mqc_t *mqc) {\r
-       mqc_resetstates(mqc);\r
-       mqc_setstate(mqc, 18, 0, 46);\r
-       mqc_setstate(mqc, 0, 0, 3);\r
-       mqc_setstate(mqc, 1, 0, 4);\r
-}\r
-\r
-void mqc_reset_enc_3(opj_mqc_t *mqc) {\r
-       mqc_resetstates(mqc);\r
-       mqc_setstate(mqc, T1_3D_CTXNO_UNI, 0, 46);\r
-       mqc_setstate(mqc, T1_3D_CTXNO_AGG, 0, 3);\r
-       mqc_setstate(mqc, T1_3D_CTXNO_ZC, 0, 4);\r
-}\r
-\r
-int mqc_restart_enc(opj_mqc_t *mqc) {\r
-       int correction = 1;\r
-       \r
-       /* <flush part> */\r
-       int n = 27 - 15 - mqc->ct;\r
-       mqc->c <<= mqc->ct;\r
-       while (n > 0) {\r
-               mqc_byteout(mqc);\r
-               n -= mqc->ct;\r
-               mqc->c <<= mqc->ct;\r
-       }\r
-       mqc_byteout(mqc);\r
-       \r
-       return correction;\r
-}\r
-\r
-void mqc_restart_init_enc(opj_mqc_t *mqc) {\r
-       /* <Re-init part> */\r
-       mqc_setcurctx(mqc, 0);\r
-       mqc->a = 0x8000;\r
-       mqc->c = 0;\r
-       mqc->ct = 12;\r
-       mqc->bp--;\r
-       if (*mqc->bp == 0xff) {\r
-               mqc->ct = 13;\r
-       }\r
-}\r
-\r
-void mqc_erterm_enc(opj_mqc_t *mqc) {\r
-       int k = 11 - mqc->ct + 1;\r
-       \r
-       while (k > 0) {\r
-               mqc->c <<= mqc->ct;\r
-               mqc->ct = 0;\r
-               mqc_byteout(mqc);\r
-               k -= mqc->ct;\r
-       }\r
-       \r
-       if (*mqc->bp != 0xff) {\r
-               mqc_byteout(mqc);\r
-       }\r
-}\r
-\r
-void mqc_segmark_enc(opj_mqc_t *mqc) {\r
-       int i;\r
-       mqc_setcurctx(mqc, 18);\r
-       \r
-       for (i = 1; i < 5; i++) {\r
-               mqc_encode(mqc, i % 2);\r
-       }\r
-}\r
-\r
-void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) {\r
-       mqc_setcurctx(mqc, 0);\r
-       mqc->start = bp;\r
-       mqc->end = bp + len;\r
-       mqc->bp = bp;\r
-       if (len==0) mqc->c = 0xff << 16;\r
-       else mqc->c = *mqc->bp << 16;\r
-       mqc_bytein(mqc);\r
-       mqc->c <<= 7;\r
-       mqc->ct -= 7;\r
-       mqc->a = 0x8000;\r
-}\r
-\r
-int mqc_decode(opj_mqc_t *mqc) {\r
-       int d;\r
-       mqc->a -= (*mqc->curctx)->qeval;\r
-       if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {\r
-               d = mqc_lpsexchange(mqc);\r
-               mqc_renormd(mqc);\r
-       } else {\r
-               mqc->c -= (*mqc->curctx)->qeval << 16;\r
-               if ((mqc->a & 0x8000) == 0) {\r
-                       d = mqc_mpsexchange(mqc);\r
-                       mqc_renormd(mqc);\r
-               } else {\r
-                       d = (*mqc->curctx)->mps;\r
-               }\r
-       }\r
-\r
-       return d;\r
-}\r
-\r
-void mqc_resetstates(opj_mqc_t *mqc) {\r
-       int i;\r
-       for (i = 0; i < MQC_NUMCTXS; i++) {\r
-               mqc->ctxs[i] = mqc_states;\r
-       }\r
-}\r
-\r
-void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) {\r
-       mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];\r
-}\r
-\r
-\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup MQC MQC - Implementation of an MQ-Coder */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+/**
+Output a byte, doing bit-stuffing if necessary.
+After a 0xff byte, the next byte must be smaller than 0x90.
+@param mqc MQC handle
+*/
+static void mqc_byteout(opj_mqc_t *mqc);
+/**
+Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
+@param mqc MQC handle
+*/
+static void mqc_renorme(opj_mqc_t *mqc);
+/**
+Encode the most probable symbol
+@param mqc MQC handle
+*/
+static void mqc_codemps(opj_mqc_t *mqc);
+/**
+Encode the most least symbol
+@param mqc MQC handle
+*/
+static void mqc_codelps(opj_mqc_t *mqc);
+/**
+Fill mqc->c with 1's for flushing
+@param mqc MQC handle
+*/
+static void mqc_setbits(opj_mqc_t *mqc);
+/**
+Exchange MPS with LPS
+@param mqc MQC handle
+@return 
+*/
+static int mqc_mpsexchange(opj_mqc_t *mqc);
+/**
+Exchange LPS with MPS
+@param mqc MQC handle
+@return 
+*/
+static int mqc_lpsexchange(opj_mqc_t *mqc);
+/**
+Input a byte
+@param mqc MQC handle
+*/
+static void mqc_bytein(opj_mqc_t *mqc);
+/**
+Renormalize mqc->a and mqc->c while decoding
+@param mqc MQC handle
+*/
+static void mqc_renormd(opj_mqc_t *mqc);
+
+/*@}*/
+
+/*@}*/
+
+/* <summary> */
+/* This array defines all the possible states for a context. */
+/* </summary> */
+static opj_mqc_state_t mqc_states[47 * 2] = {
+       {0x5601, 0, &mqc_states[2], &mqc_states[3]},
+       {0x5601, 1, &mqc_states[3], &mqc_states[2]},
+       {0x3401, 0, &mqc_states[4], &mqc_states[12]},
+       {0x3401, 1, &mqc_states[5], &mqc_states[13]},
+       {0x1801, 0, &mqc_states[6], &mqc_states[18]},
+       {0x1801, 1, &mqc_states[7], &mqc_states[19]},
+       {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
+       {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
+       {0x0521, 0, &mqc_states[10], &mqc_states[58]},
+       {0x0521, 1, &mqc_states[11], &mqc_states[59]},
+       {0x0221, 0, &mqc_states[76], &mqc_states[66]},
+       {0x0221, 1, &mqc_states[77], &mqc_states[67]},
+       {0x5601, 0, &mqc_states[14], &mqc_states[13]},
+       {0x5601, 1, &mqc_states[15], &mqc_states[12]},
+       {0x5401, 0, &mqc_states[16], &mqc_states[28]},
+       {0x5401, 1, &mqc_states[17], &mqc_states[29]},
+       {0x4801, 0, &mqc_states[18], &mqc_states[28]},
+       {0x4801, 1, &mqc_states[19], &mqc_states[29]},
+       {0x3801, 0, &mqc_states[20], &mqc_states[28]},
+       {0x3801, 1, &mqc_states[21], &mqc_states[29]},
+       {0x3001, 0, &mqc_states[22], &mqc_states[34]},
+       {0x3001, 1, &mqc_states[23], &mqc_states[35]},
+       {0x2401, 0, &mqc_states[24], &mqc_states[36]},
+       {0x2401, 1, &mqc_states[25], &mqc_states[37]},
+       {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
+       {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
+       {0x1601, 0, &mqc_states[58], &mqc_states[42]},
+       {0x1601, 1, &mqc_states[59], &mqc_states[43]},
+       {0x5601, 0, &mqc_states[30], &mqc_states[29]},
+       {0x5601, 1, &mqc_states[31], &mqc_states[28]},
+       {0x5401, 0, &mqc_states[32], &mqc_states[28]},
+       {0x5401, 1, &mqc_states[33], &mqc_states[29]},
+       {0x5101, 0, &mqc_states[34], &mqc_states[30]},
+       {0x5101, 1, &mqc_states[35], &mqc_states[31]},
+       {0x4801, 0, &mqc_states[36], &mqc_states[32]},
+       {0x4801, 1, &mqc_states[37], &mqc_states[33]},
+       {0x3801, 0, &mqc_states[38], &mqc_states[34]},
+       {0x3801, 1, &mqc_states[39], &mqc_states[35]},
+       {0x3401, 0, &mqc_states[40], &mqc_states[36]},
+       {0x3401, 1, &mqc_states[41], &mqc_states[37]},
+       {0x3001, 0, &mqc_states[42], &mqc_states[38]},
+       {0x3001, 1, &mqc_states[43], &mqc_states[39]},
+       {0x2801, 0, &mqc_states[44], &mqc_states[38]},
+       {0x2801, 1, &mqc_states[45], &mqc_states[39]},
+       {0x2401, 0, &mqc_states[46], &mqc_states[40]},
+       {0x2401, 1, &mqc_states[47], &mqc_states[41]},
+       {0x2201, 0, &mqc_states[48], &mqc_states[42]},
+       {0x2201, 1, &mqc_states[49], &mqc_states[43]},
+       {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
+       {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
+       {0x1801, 0, &mqc_states[52], &mqc_states[46]},
+       {0x1801, 1, &mqc_states[53], &mqc_states[47]},
+       {0x1601, 0, &mqc_states[54], &mqc_states[48]},
+       {0x1601, 1, &mqc_states[55], &mqc_states[49]},
+       {0x1401, 0, &mqc_states[56], &mqc_states[50]},
+       {0x1401, 1, &mqc_states[57], &mqc_states[51]},
+       {0x1201, 0, &mqc_states[58], &mqc_states[52]},
+       {0x1201, 1, &mqc_states[59], &mqc_states[53]},
+       {0x1101, 0, &mqc_states[60], &mqc_states[54]},
+       {0x1101, 1, &mqc_states[61], &mqc_states[55]},
+       {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
+       {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
+       {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
+       {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
+       {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
+       {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
+       {0x0521, 0, &mqc_states[68], &mqc_states[62]},
+       {0x0521, 1, &mqc_states[69], &mqc_states[63]},
+       {0x0441, 0, &mqc_states[70], &mqc_states[64]},
+       {0x0441, 1, &mqc_states[71], &mqc_states[65]},
+       {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
+       {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
+       {0x0221, 0, &mqc_states[74], &mqc_states[68]},
+       {0x0221, 1, &mqc_states[75], &mqc_states[69]},
+       {0x0141, 0, &mqc_states[76], &mqc_states[70]},
+       {0x0141, 1, &mqc_states[77], &mqc_states[71]},
+       {0x0111, 0, &mqc_states[78], &mqc_states[72]},
+       {0x0111, 1, &mqc_states[79], &mqc_states[73]},
+       {0x0085, 0, &mqc_states[80], &mqc_states[74]},
+       {0x0085, 1, &mqc_states[81], &mqc_states[75]},
+       {0x0049, 0, &mqc_states[82], &mqc_states[76]},
+       {0x0049, 1, &mqc_states[83], &mqc_states[77]},
+       {0x0025, 0, &mqc_states[84], &mqc_states[78]},
+       {0x0025, 1, &mqc_states[85], &mqc_states[79]},
+       {0x0015, 0, &mqc_states[86], &mqc_states[80]},
+       {0x0015, 1, &mqc_states[87], &mqc_states[81]},
+       {0x0009, 0, &mqc_states[88], &mqc_states[82]},
+       {0x0009, 1, &mqc_states[89], &mqc_states[83]},
+       {0x0005, 0, &mqc_states[90], &mqc_states[84]},
+       {0x0005, 1, &mqc_states[91], &mqc_states[85]},
+       {0x0001, 0, &mqc_states[90], &mqc_states[86]},
+       {0x0001, 1, &mqc_states[91], &mqc_states[87]},
+       {0x5601, 0, &mqc_states[92], &mqc_states[92]},
+       {0x5601, 1, &mqc_states[93], &mqc_states[93]},
+};
+
+/* 
+==========================================================
+   local functions
+==========================================================
+*/
+
+static void mqc_byteout(opj_mqc_t *mqc) {
+       if (*mqc->bp == 0xff) {
+               mqc->bp++;
+               *mqc->bp = mqc->c >> 20;
+               mqc->c &= 0xfffff;
+               mqc->ct = 7;
+       } else {
+               if ((mqc->c & 0x8000000) == 0) {        /* ((mqc->c&0x8000000)==0) CHANGE */
+                       mqc->bp++;
+                       *mqc->bp = mqc->c >> 19;
+                       mqc->c &= 0x7ffff;
+                       mqc->ct = 8;
+               } else {
+                       (*mqc->bp)++;
+                       if (*mqc->bp == 0xff) {
+                               mqc->c &= 0x7ffffff;
+                               mqc->bp++;
+                               *mqc->bp = mqc->c >> 20;
+                               mqc->c &= 0xfffff;
+                               mqc->ct = 7;
+                       } else {
+                               mqc->bp++;
+                               *mqc->bp = mqc->c >> 19;
+                               mqc->c &= 0x7ffff;
+                               mqc->ct = 8;
+                       }
+               }
+       }
+}
+
+static void mqc_renorme(opj_mqc_t *mqc) {
+       do {
+               mqc->a <<= 1;
+               mqc->c <<= 1;
+               mqc->ct--;
+               if (mqc->ct == 0) {
+                       mqc_byteout(mqc);
+               }
+       } while ((mqc->a & 0x8000) == 0);
+}
+
+static void mqc_codemps(opj_mqc_t *mqc) {
+       mqc->a -= (*mqc->curctx)->qeval;
+       if ((mqc->a & 0x8000) == 0) {
+               if (mqc->a < (*mqc->curctx)->qeval) {
+                       mqc->a = (*mqc->curctx)->qeval;
+               } else {
+                       mqc->c += (*mqc->curctx)->qeval;
+               }
+               *mqc->curctx = (*mqc->curctx)->nmps;
+               mqc_renorme(mqc);
+       } else {
+               mqc->c += (*mqc->curctx)->qeval;
+       }
+}
+
+static void mqc_codelps(opj_mqc_t *mqc) {
+       mqc->a -= (*mqc->curctx)->qeval;
+       if (mqc->a < (*mqc->curctx)->qeval) {
+               mqc->c += (*mqc->curctx)->qeval;
+       } else {
+               mqc->a = (*mqc->curctx)->qeval;
+       }
+       *mqc->curctx = (*mqc->curctx)->nlps;
+       mqc_renorme(mqc);
+}
+
+static void mqc_setbits(opj_mqc_t *mqc) {
+       unsigned int tempc = mqc->c + mqc->a;
+       mqc->c |= 0xffff;
+       if (mqc->c >= tempc) {
+               mqc->c -= 0x8000;
+       }
+}
+
+static int mqc_mpsexchange(opj_mqc_t *mqc) {
+       int d;
+       if (mqc->a < (*mqc->curctx)->qeval) {
+               d = 1 - (*mqc->curctx)->mps;
+               *mqc->curctx = (*mqc->curctx)->nlps;
+       } else {
+               d = (*mqc->curctx)->mps;
+               *mqc->curctx = (*mqc->curctx)->nmps;
+       }
+       
+       return d;
+}
+
+static int mqc_lpsexchange(opj_mqc_t *mqc) {
+       int d;
+       if (mqc->a < (*mqc->curctx)->qeval) {
+               mqc->a = (*mqc->curctx)->qeval;
+               d = (*mqc->curctx)->mps;
+               *mqc->curctx = (*mqc->curctx)->nmps;
+       } else {
+               mqc->a = (*mqc->curctx)->qeval;
+               d = 1 - (*mqc->curctx)->mps;
+               *mqc->curctx = (*mqc->curctx)->nlps;
+       }
+       
+       return d;
+}
+
+static void mqc_bytein(opj_mqc_t *mqc) {
+       if (mqc->bp != mqc->end) {
+               unsigned int c;
+               if (mqc->bp + 1 != mqc->end) {
+                       c = *(mqc->bp + 1);
+               } else {
+                       c = 0xff;
+               }
+               if (*mqc->bp == 0xff) {
+                       if (c > 0x8f) {
+                               mqc->c += 0xff00;
+                               mqc->ct = 8;
+                       } else {
+                               mqc->bp++;
+                               mqc->c += c << 9;
+                               mqc->ct = 7;
+                       }
+               } else {
+                       mqc->bp++;
+                       mqc->c += c << 8;
+                       mqc->ct = 8;
+               }
+       } else {
+               mqc->c += 0xff00;
+               mqc->ct = 8;
+       }
+}
+
+static void mqc_renormd(opj_mqc_t *mqc) {
+       do {
+               if (mqc->ct == 0) {
+                       mqc_bytein(mqc);
+               }
+               mqc->a <<= 1;
+               mqc->c <<= 1;
+               mqc->ct--;
+       } while (mqc->a < 0x8000);
+}
+
+/* 
+==========================================================
+   MQ-Coder interface
+==========================================================
+*/
+
+opj_mqc_t* mqc_create() {
+       opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
+       return mqc;
+}
+
+void mqc_destroy(opj_mqc_t *mqc) {
+       if(mqc) {
+               opj_free(mqc);
+       }
+}
+
+int mqc_numbytes(opj_mqc_t *mqc) {
+       return mqc->bp - mqc->start;
+}
+
+void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) {
+       mqc_setcurctx(mqc, 0);
+       mqc->a = 0x8000;
+       mqc->c = 0;
+       mqc->bp = bp - 1;
+       mqc->ct = 12;
+       if (*mqc->bp == 0xff) {
+               mqc->ct = 13;
+       }
+       mqc->start = bp;
+}
+
+void mqc_setcurctx(opj_mqc_t *mqc, int ctxno) {
+       mqc->curctx = &mqc->ctxs[ctxno];
+}
+
+void mqc_encode(opj_mqc_t *mqc, int d) {
+       if ((*mqc->curctx)->mps == d) {
+               mqc_codemps(mqc);
+       } else {
+               mqc_codelps(mqc);
+       }
+}
+
+void mqc_flush(opj_mqc_t *mqc) {
+       mqc_setbits(mqc);
+       mqc->c <<= mqc->ct;
+       mqc_byteout(mqc);
+       mqc->c <<= mqc->ct;
+       mqc_byteout(mqc);
+       
+       if (*mqc->bp != 0xff) {
+               mqc->bp++;
+       }
+}
+
+void mqc_bypass_init_enc(opj_mqc_t *mqc) {
+       mqc->c = 0;
+       mqc->ct = 8;
+       /*if (*mqc->bp == 0xff) {
+       mqc->ct = 7;
+     } */
+}
+
+void mqc_bypass_enc(opj_mqc_t *mqc, int d) {
+       mqc->ct--;
+       mqc->c = mqc->c + (d << mqc->ct);
+       if (mqc->ct == 0) {
+               mqc->bp++;
+               *mqc->bp = mqc->c;
+               mqc->ct = 8;
+               if (*mqc->bp == 0xff) {
+                       mqc->ct = 7;
+               }
+               mqc->c = 0;
+       }
+}
+
+int mqc_bypass_flush_enc(opj_mqc_t *mqc) {
+       unsigned char bit_padding;
+       
+       bit_padding = 0;
+       
+       if (mqc->ct != 0) {
+               while (mqc->ct > 0) {
+                       mqc->ct--;
+                       mqc->c += bit_padding << mqc->ct;
+                       bit_padding = (bit_padding + 1) & 0x01;
+               }
+               mqc->bp++;
+               *mqc->bp = mqc->c;
+               mqc->ct = 8;
+               mqc->c = 0;
+       }
+       
+       return 1;
+}
+
+void mqc_reset_enc(opj_mqc_t *mqc) {
+       mqc_resetstates(mqc);
+       mqc_setstate(mqc, 18, 0, 46);
+       mqc_setstate(mqc, 0, 0, 3);
+       mqc_setstate(mqc, 1, 0, 4);
+}
+
+void mqc_reset_enc_3(opj_mqc_t *mqc) {
+       mqc_resetstates(mqc);
+       mqc_setstate(mqc, T1_3D_CTXNO_UNI, 0, 46);
+       mqc_setstate(mqc, T1_3D_CTXNO_AGG, 0, 3);
+       mqc_setstate(mqc, T1_3D_CTXNO_ZC, 0, 4);
+}
+
+int mqc_restart_enc(opj_mqc_t *mqc) {
+       int correction = 1;
+       
+       /* <flush part> */
+       int n = 27 - 15 - mqc->ct;
+       mqc->c <<= mqc->ct;
+       while (n > 0) {
+               mqc_byteout(mqc);
+               n -= mqc->ct;
+               mqc->c <<= mqc->ct;
+       }
+       mqc_byteout(mqc);
+       
+       return correction;
+}
+
+void mqc_restart_init_enc(opj_mqc_t *mqc) {
+       /* <Re-init part> */
+       mqc_setcurctx(mqc, 0);
+       mqc->a = 0x8000;
+       mqc->c = 0;
+       mqc->ct = 12;
+       mqc->bp--;
+       if (*mqc->bp == 0xff) {
+               mqc->ct = 13;
+       }
+}
+
+void mqc_erterm_enc(opj_mqc_t *mqc) {
+       int k = 11 - mqc->ct + 1;
+       
+       while (k > 0) {
+               mqc->c <<= mqc->ct;
+               mqc->ct = 0;
+               mqc_byteout(mqc);
+               k -= mqc->ct;
+       }
+       
+       if (*mqc->bp != 0xff) {
+               mqc_byteout(mqc);
+       }
+}
+
+void mqc_segmark_enc(opj_mqc_t *mqc) {
+       int i;
+       mqc_setcurctx(mqc, 18);
+       
+       for (i = 1; i < 5; i++) {
+               mqc_encode(mqc, i % 2);
+       }
+}
+
+void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) {
+       mqc_setcurctx(mqc, 0);
+       mqc->start = bp;
+       mqc->end = bp + len;
+       mqc->bp = bp;
+       if (len==0) mqc->c = 0xff << 16;
+       else mqc->c = *mqc->bp << 16;
+       mqc_bytein(mqc);
+       mqc->c <<= 7;
+       mqc->ct -= 7;
+       mqc->a = 0x8000;
+}
+
+int mqc_decode(opj_mqc_t *mqc) {
+       int d;
+       mqc->a -= (*mqc->curctx)->qeval;
+       if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
+               d = mqc_lpsexchange(mqc);
+               mqc_renormd(mqc);
+       } else {
+               mqc->c -= (*mqc->curctx)->qeval << 16;
+               if ((mqc->a & 0x8000) == 0) {
+                       d = mqc_mpsexchange(mqc);
+                       mqc_renormd(mqc);
+               } else {
+                       d = (*mqc->curctx)->mps;
+               }
+       }
+
+       return d;
+}
+
+void mqc_resetstates(opj_mqc_t *mqc) {
+       int i;
+       for (i = 0; i < MQC_NUMCTXS; i++) {
+               mqc->ctxs[i] = mqc_states;
+       }
+}
+
+void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) {
+       mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
+}
+
+
index 023bf41578f310b15e15aa02bd268fc342afcdae..57bfc9f86063dc4828cef06961bcdb3f4263db67 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __MQC_H\r
-#define __MQC_H\r
-/**\r
-@file mqc.h\r
-@brief Implementation of an MQ-Coder (MQC)\r
-\r
-The functions in MQC.C have for goal to realize the MQ-coder operations. The functions\r
-in MQC.C are used by some function in T1.C.\r
-*/\r
-\r
-/** @defgroup MQC MQC - Implementation of an MQ-Coder */\r
-/*@{*/\r
-\r
-/**\r
-This struct defines the state of a context.\r
-*/\r
-typedef struct opj_mqc_state {\r
-       /** the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */\r
-       unsigned int qeval;\r
-       /** the Most Probable Symbol (0 or 1) */\r
-       int mps;\r
-       /** next state if the next encoded symbol is the MPS */\r
-       struct opj_mqc_state *nmps;\r
-       /** next state if the next encoded symbol is the LPS */\r
-       struct opj_mqc_state *nlps;\r
-} opj_mqc_state_t;\r
-\r
-#define MQC_NUMCTXS 32\r
-\r
-/**\r
-MQ coder\r
-*/\r
-typedef struct opj_mqc {\r
-       unsigned int c;\r
-       unsigned int a;\r
-       unsigned int ct;\r
-       unsigned char *bp;\r
-       unsigned char *start;\r
-       unsigned char *end;\r
-       opj_mqc_state_t *ctxs[MQC_NUMCTXS];\r
-       opj_mqc_state_t **curctx;\r
-} opj_mqc_t;\r
-\r
-/** @name Exported functions */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Create a new MQC handle \r
-@return Returns a new MQC handle if successful, returns NULL otherwise\r
-*/\r
-opj_mqc_t* mqc_create(void);\r
-/**\r
-Destroy a previously created MQC handle\r
-@param mqc MQC handle to destroy\r
-*/\r
-void mqc_destroy(opj_mqc_t *mqc);\r
-/**\r
-Return the number of bytes written/read since initialisation\r
-@param mqc MQC handle\r
-@return Returns the number of bytes already encoded\r
-*/\r
-int mqc_numbytes(opj_mqc_t *mqc);\r
-/**\r
-Reset the states of all the context of the coder/decoder \r
-(each context is set to a state where 0 and 1 are more or less equiprobable)\r
-@param mqc MQC handle\r
-*/\r
-void mqc_resetstates(opj_mqc_t *mqc);\r
-/**\r
-Set the state of a particular context\r
-@param mqc MQC handle\r
-@param ctxno Number that identifies the context\r
-@param msb The MSB of the new state of the context\r
-@param prob Number that identifies the probability of the symbols for the new state of the context\r
-*/\r
-void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob);\r
-/**\r
-Initialize the encoder\r
-@param mqc MQC handle\r
-@param bp Pointer to the start of the buffer where the bytes will be written\r
-*/\r
-void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp);\r
-/**\r
-Set the current context used for coding/decoding\r
-@param mqc MQC handle\r
-@param ctxno Number that identifies the context\r
-*/\r
-void mqc_setcurctx(opj_mqc_t *mqc, int ctxno);\r
-/**\r
-Encode a symbol using the MQ-coder\r
-@param mqc MQC handle\r
-@param d The symbol to be encoded (0 or 1)\r
-*/\r
-void mqc_encode(opj_mqc_t *mqc, int d);\r
-/**\r
-Flush the encoder, so that all remaining data is written\r
-@param mqc MQC handle\r
-*/\r
-void mqc_flush(opj_mqc_t *mqc);\r
-/**\r
-BYPASS mode switch, initialization operation. \r
-JPEG 2000 p 505. \r
-<h2>Not fully implemented and tested !!</h2>\r
-@param mqc MQC handle\r
-*/\r
-void mqc_bypass_init_enc(opj_mqc_t *mqc);\r
-/**\r
-BYPASS mode switch, coding operation. \r
-JPEG 2000 p 505. \r
-<h2>Not fully implemented and tested !!</h2>\r
-@param mqc MQC handle\r
-@param d The symbol to be encoded (0 or 1)\r
-*/\r
-void mqc_bypass_enc(opj_mqc_t *mqc, int d);\r
-/**\r
-BYPASS mode switch, flush operation\r
-<h2>Not fully implemented and tested !!</h2>\r
-@param mqc MQC handle\r
-@return Returns 1 (always)\r
-*/\r
-int mqc_bypass_flush_enc(opj_mqc_t *mqc);\r
-/**\r
-RESET mode switch\r
-@param mqc MQC handle\r
-*/\r
-void mqc_reset_enc(opj_mqc_t *mqc);\r
-/**\r
-RESET mode switch\r
-@param mqc MQC handle\r
-*/\r
-void mqc_reset_enc_3(opj_mqc_t *mqc);\r
-/**\r
-RESTART mode switch (TERMALL)\r
-@param mqc MQC handle\r
-@return Returns 1 (always)\r
-*/\r
-int mqc_restart_enc(opj_mqc_t *mqc);\r
-/**\r
-RESTART mode switch (TERMALL) reinitialisation\r
-@param mqc MQC handle\r
-*/\r
-void mqc_restart_init_enc(opj_mqc_t *mqc);\r
-/**\r
-ERTERM mode switch (PTERM)\r
-@param mqc MQC handle\r
-*/\r
-void mqc_erterm_enc(opj_mqc_t *mqc);\r
-/**\r
-SEGMARK mode switch (SEGSYM)\r
-@param mqc MQC handle\r
-*/\r
-void mqc_segmark_enc(opj_mqc_t *mqc);\r
-/**\r
-Initialize the decoder\r
-@param mqc MQC handle\r
-@param bp Pointer to the start of the buffer from which the bytes will be read\r
-@param len Length of the input buffer\r
-*/\r
-void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len);\r
-/**\r
-Decode a symbol\r
-@param mqc MQC handle\r
-@return Returns the decoded symbol (0 or 1)\r
-*/\r
-int mqc_decode(opj_mqc_t *mqc);\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __MQC_H */\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MQC_H
+#define __MQC_H
+/**
+@file mqc.h
+@brief Implementation of an MQ-Coder (MQC)
+
+The functions in MQC.C have for goal to realize the MQ-coder operations. The functions
+in MQC.C are used by some function in T1.C.
+*/
+
+/** @defgroup MQC MQC - Implementation of an MQ-Coder */
+/*@{*/
+
+/**
+This struct defines the state of a context.
+*/
+typedef struct opj_mqc_state {
+       /** the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */
+       unsigned int qeval;
+       /** the Most Probable Symbol (0 or 1) */
+       int mps;
+       /** next state if the next encoded symbol is the MPS */
+       struct opj_mqc_state *nmps;
+       /** next state if the next encoded symbol is the LPS */
+       struct opj_mqc_state *nlps;
+} opj_mqc_state_t;
+
+#define MQC_NUMCTXS 32
+
+/**
+MQ coder
+*/
+typedef struct opj_mqc {
+       unsigned int c;
+       unsigned int a;
+       unsigned int ct;
+       unsigned char *bp;
+       unsigned char *start;
+       unsigned char *end;
+       opj_mqc_state_t *ctxs[MQC_NUMCTXS];
+       opj_mqc_state_t **curctx;
+} opj_mqc_t;
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a new MQC handle 
+@return Returns a new MQC handle if successful, returns NULL otherwise
+*/
+opj_mqc_t* mqc_create(void);
+/**
+Destroy a previously created MQC handle
+@param mqc MQC handle to destroy
+*/
+void mqc_destroy(opj_mqc_t *mqc);
+/**
+Return the number of bytes written/read since initialisation
+@param mqc MQC handle
+@return Returns the number of bytes already encoded
+*/
+int mqc_numbytes(opj_mqc_t *mqc);
+/**
+Reset the states of all the context of the coder/decoder 
+(each context is set to a state where 0 and 1 are more or less equiprobable)
+@param mqc MQC handle
+*/
+void mqc_resetstates(opj_mqc_t *mqc);
+/**
+Set the state of a particular context
+@param mqc MQC handle
+@param ctxno Number that identifies the context
+@param msb The MSB of the new state of the context
+@param prob Number that identifies the probability of the symbols for the new state of the context
+*/
+void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob);
+/**
+Initialize the encoder
+@param mqc MQC handle
+@param bp Pointer to the start of the buffer where the bytes will be written
+*/
+void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp);
+/**
+Set the current context used for coding/decoding
+@param mqc MQC handle
+@param ctxno Number that identifies the context
+*/
+void mqc_setcurctx(opj_mqc_t *mqc, int ctxno);
+/**
+Encode a symbol using the MQ-coder
+@param mqc MQC handle
+@param d The symbol to be encoded (0 or 1)
+*/
+void mqc_encode(opj_mqc_t *mqc, int d);
+/**
+Flush the encoder, so that all remaining data is written
+@param mqc MQC handle
+*/
+void mqc_flush(opj_mqc_t *mqc);
+/**
+BYPASS mode switch, initialization operation. 
+JPEG 2000 p 505. 
+<h2>Not fully implemented and tested !!</h2>
+@param mqc MQC handle
+*/
+void mqc_bypass_init_enc(opj_mqc_t *mqc);
+/**
+BYPASS mode switch, coding operation. 
+JPEG 2000 p 505. 
+<h2>Not fully implemented and tested !!</h2>
+@param mqc MQC handle
+@param d The symbol to be encoded (0 or 1)
+*/
+void mqc_bypass_enc(opj_mqc_t *mqc, int d);
+/**
+BYPASS mode switch, flush operation
+<h2>Not fully implemented and tested !!</h2>
+@param mqc MQC handle
+@return Returns 1 (always)
+*/
+int mqc_bypass_flush_enc(opj_mqc_t *mqc);
+/**
+RESET mode switch
+@param mqc MQC handle
+*/
+void mqc_reset_enc(opj_mqc_t *mqc);
+/**
+RESET mode switch
+@param mqc MQC handle
+*/
+void mqc_reset_enc_3(opj_mqc_t *mqc);
+/**
+RESTART mode switch (TERMALL)
+@param mqc MQC handle
+@return Returns 1 (always)
+*/
+int mqc_restart_enc(opj_mqc_t *mqc);
+/**
+RESTART mode switch (TERMALL) reinitialisation
+@param mqc MQC handle
+*/
+void mqc_restart_init_enc(opj_mqc_t *mqc);
+/**
+ERTERM mode switch (PTERM)
+@param mqc MQC handle
+*/
+void mqc_erterm_enc(opj_mqc_t *mqc);
+/**
+SEGMARK mode switch (SEGSYM)
+@param mqc MQC handle
+*/
+void mqc_segmark_enc(opj_mqc_t *mqc);
+/**
+Initialize the decoder
+@param mqc MQC handle
+@param bp Pointer to the start of the buffer from which the bytes will be read
+@param len Length of the input buffer
+*/
+void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len);
+/**
+Decode a symbol
+@param mqc MQC handle
+@return Returns the decoded symbol (0 or 1)
+*/
+int mqc_decode(opj_mqc_t *mqc);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __MQC_H */
index c1f3c44ffe15d89e8975044188ecc9fbfe6317c1..7adfbea0d67c51125f91b42fe06dd8d6f9baa8f4 100644 (file)
-/*\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifdef _WIN32\r
-#include <windows.h>\r
-#endif /* _WIN32 */\r
-\r
-#include "opj_includes.h"\r
-#include "openjp3d.h"\r
-#define JP3D_VERSION "1.3.0"\r
-/* ---------------------------------------------------------------------- */\r
-#ifdef _WIN32\r
-#ifndef OPJ_STATIC\r
-BOOL APIENTRY\r
-DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {\r
-       switch (ul_reason_for_call) {\r
-               case DLL_PROCESS_ATTACH :\r
-                       break;\r
-               case DLL_PROCESS_DETACH :\r
-                       break;\r
-               case DLL_THREAD_ATTACH :\r
-               case DLL_THREAD_DETACH :\r
-                       break;\r
-    }\r
-\r
-    return TRUE;\r
-}\r
-#endif /* OPJ_STATIC */\r
-#endif /* _WIN32 */\r
-\r
-/* ---------------------------------------------------------------------- */\r
-\r
-const char* OPJ_CALLCONV opj_version() {\r
-    return JP3D_VERSION;\r
-}\r
-opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {\r
-       opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_malloc(sizeof(opj_dinfo_t));\r
-       if(!dinfo) return NULL;\r
-       dinfo->is_decompressor = true;\r
-       switch(format) {\r
-               case CODEC_J3D:\r
-               case CODEC_J2K:\r
-                       /* get a J3D decoder handle */\r
-                       dinfo->j3d_handle = (void*)j3d_create_decompress((opj_common_ptr)dinfo);\r
-                       if(!dinfo->j3d_handle) {\r
-                               opj_free(dinfo);\r
-                               return NULL;\r
-                       }\r
-                       break;\r
-               default:\r
-                       opj_free(dinfo);\r
-                       return NULL;\r
-       }\r
-\r
-       dinfo->codec_format = format;\r
-\r
-       return dinfo;\r
-}\r
-\r
-void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {\r
-       if(dinfo) {\r
-               /* destroy the codec */\r
-               if(dinfo->codec_format != CODEC_UNKNOWN) {\r
-                       j3d_destroy_decompress((opj_j3d_t*)dinfo->j3d_handle);\r
-               }\r
-               /* destroy the decompressor */\r
-               opj_free(dinfo);\r
-       }\r
-}\r
-\r
-void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {\r
-       if(parameters) {\r
-               memset(parameters, 0, sizeof(opj_dparameters_t));\r
-               /* default decoding parameters */\r
-               parameters->cp_layer = 0;\r
-               parameters->cp_reduce[0] = 0;\r
-               parameters->cp_reduce[1] = 0;\r
-               parameters->cp_reduce[2] = 0;\r
-               parameters->bigendian = 0;\r
-\r
-               parameters->decod_format = -1;\r
-               parameters->cod_format = -1;\r
-       }\r
-}\r
-\r
-void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {\r
-       if(dinfo && parameters) {\r
-               if (dinfo->codec_format != CODEC_UNKNOWN) {\r
-                       j3d_setup_decoder((opj_j3d_t*)dinfo->j3d_handle, parameters);\r
-               }\r
-       }\r
-}\r
-\r
-opj_volume_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {\r
-       if(dinfo && cio) {\r
-               if (dinfo->codec_format != CODEC_UNKNOWN) {\r
-                       return j3d_decode((opj_j3d_t*)dinfo->j3d_handle, cio);\r
-               }\r
-       }\r
-\r
-       return NULL;\r
-}\r
-\r
-opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {\r
-       opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_malloc(sizeof(opj_cinfo_t));\r
-       if(!cinfo) return NULL;\r
-       cinfo->is_decompressor = false;\r
-       switch(format) {\r
-               case CODEC_J3D:\r
-               case CODEC_J2K:\r
-                       /* get a J3D coder handle */\r
-                       cinfo->j3d_handle = (void*)j3d_create_compress((opj_common_ptr)cinfo);\r
-                       if(!cinfo->j3d_handle) {\r
-                               opj_free(cinfo);\r
-                               return NULL;\r
-                       }\r
-                       break;\r
-               default:\r
-                       opj_free(cinfo);\r
-                       return NULL;\r
-       }\r
-\r
-       cinfo->codec_format = format;\r
-\r
-       return cinfo;\r
-}\r
-\r
-void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {\r
-       if(cinfo) {\r
-               /* destroy the codec */\r
-               if (cinfo->codec_format != CODEC_UNKNOWN) {\r
-                               j3d_destroy_compress((opj_j3d_t*)cinfo->j3d_handle);\r
-               }\r
-               /* destroy the decompressor */\r
-               opj_free(cinfo);\r
-       }\r
-}\r
-\r
-void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {\r
-       if(parameters) {\r
-               memset(parameters, 0, sizeof(opj_cparameters_t));\r
-               /* default coding parameters */\r
-               parameters->numresolution[0] = 3;\r
-               parameters->numresolution[1] = 3;\r
-               parameters->numresolution[2] = 1;\r
-               parameters->cblock_init[0] = 64;\r
-               parameters->cblock_init[1] = 64;\r
-               parameters->cblock_init[2] = 64;\r
-               parameters->prog_order = LRCP;\r
-               parameters->roi_compno = -1;            /* no ROI */\r
-               parameters->atk_wt[0] = 1;                              /* 5-3 WT */\r
-               parameters->atk_wt[1] = 1;                              /* 5-3 WT */\r
-               parameters->atk_wt[2] = 1;                              /* 5-3 WT */\r
-               parameters->irreversible = 0;\r
-               parameters->subsampling_dx = 1;\r
-               parameters->subsampling_dy = 1;\r
-               parameters->subsampling_dz = 1;\r
-\r
-               parameters->decod_format = -1;\r
-               parameters->cod_format = -1;\r
-               parameters->encoding_format = ENCOD_2EB;\r
-               parameters->transform_format = TRF_2D_DWT;\r
-       }\r
-}\r
-\r
-void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_volume_t *volume) {\r
-       if(cinfo && parameters && volume) {\r
-               if (cinfo->codec_format != CODEC_UNKNOWN) {\r
-                       j3d_setup_encoder((opj_j3d_t*)cinfo->j3d_handle, parameters, volume);\r
-               }\r
-       }\r
-}\r
-\r
-bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_volume_t *volume, char *index) {\r
-       if(cinfo && cio && volume) {\r
-               if (cinfo->codec_format != CODEC_UNKNOWN) {\r
-                       return j3d_encode((opj_j3d_t*)cinfo->j3d_handle, cio, volume, index);\r
-               }\r
-       }\r
-\r
-       return false;\r
-}\r
-\r
-\r
+/*
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef _WIN32
+#include <windows.h>
+#endif /* _WIN32 */
+
+#include "opj_includes.h"
+#include "openjp3d.h"
+#define JP3D_VERSION "1.3.0"
+/* ---------------------------------------------------------------------- */
+#ifdef _WIN32
+#ifndef OPJ_STATIC
+BOOL APIENTRY
+DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
+       switch (ul_reason_for_call) {
+               case DLL_PROCESS_ATTACH :
+                       break;
+               case DLL_PROCESS_DETACH :
+                       break;
+               case DLL_THREAD_ATTACH :
+               case DLL_THREAD_DETACH :
+                       break;
+    }
+
+    return TRUE;
+}
+#endif /* OPJ_STATIC */
+#endif /* _WIN32 */
+
+/* ---------------------------------------------------------------------- */
+
+const char* OPJ_CALLCONV opj_version() {
+    return JP3D_VERSION;
+}
+opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
+       opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_malloc(sizeof(opj_dinfo_t));
+       if(!dinfo) return NULL;
+       dinfo->is_decompressor = true;
+       switch(format) {
+               case CODEC_J3D:
+               case CODEC_J2K:
+                       /* get a J3D decoder handle */
+                       dinfo->j3d_handle = (void*)j3d_create_decompress((opj_common_ptr)dinfo);
+                       if(!dinfo->j3d_handle) {
+                               opj_free(dinfo);
+                               return NULL;
+                       }
+                       break;
+               default:
+                       opj_free(dinfo);
+                       return NULL;
+       }
+
+       dinfo->codec_format = format;
+
+       return dinfo;
+}
+
+void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
+       if(dinfo) {
+               /* destroy the codec */
+               if(dinfo->codec_format != CODEC_UNKNOWN) {
+                       j3d_destroy_decompress((opj_j3d_t*)dinfo->j3d_handle);
+               }
+               /* destroy the decompressor */
+               opj_free(dinfo);
+       }
+}
+
+void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
+       if(parameters) {
+               memset(parameters, 0, sizeof(opj_dparameters_t));
+               /* default decoding parameters */
+               parameters->cp_layer = 0;
+               parameters->cp_reduce[0] = 0;
+               parameters->cp_reduce[1] = 0;
+               parameters->cp_reduce[2] = 0;
+               parameters->bigendian = 0;
+
+               parameters->decod_format = -1;
+               parameters->cod_format = -1;
+       }
+}
+
+void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
+       if(dinfo && parameters) {
+               if (dinfo->codec_format != CODEC_UNKNOWN) {
+                       j3d_setup_decoder((opj_j3d_t*)dinfo->j3d_handle, parameters);
+               }
+       }
+}
+
+opj_volume_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
+       if(dinfo && cio) {
+               if (dinfo->codec_format != CODEC_UNKNOWN) {
+                       return j3d_decode((opj_j3d_t*)dinfo->j3d_handle, cio);
+               }
+       }
+
+       return NULL;
+}
+
+opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
+       opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_malloc(sizeof(opj_cinfo_t));
+       if(!cinfo) return NULL;
+       cinfo->is_decompressor = false;
+       switch(format) {
+               case CODEC_J3D:
+               case CODEC_J2K:
+                       /* get a J3D coder handle */
+                       cinfo->j3d_handle = (void*)j3d_create_compress((opj_common_ptr)cinfo);
+                       if(!cinfo->j3d_handle) {
+                               opj_free(cinfo);
+                               return NULL;
+                       }
+                       break;
+               default:
+                       opj_free(cinfo);
+                       return NULL;
+       }
+
+       cinfo->codec_format = format;
+
+       return cinfo;
+}
+
+void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
+       if(cinfo) {
+               /* destroy the codec */
+               if (cinfo->codec_format != CODEC_UNKNOWN) {
+                               j3d_destroy_compress((opj_j3d_t*)cinfo->j3d_handle);
+               }
+               /* destroy the decompressor */
+               opj_free(cinfo);
+       }
+}
+
+void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
+       if(parameters) {
+               memset(parameters, 0, sizeof(opj_cparameters_t));
+               /* default coding parameters */
+               parameters->numresolution[0] = 3;
+               parameters->numresolution[1] = 3;
+               parameters->numresolution[2] = 1;
+               parameters->cblock_init[0] = 64;
+               parameters->cblock_init[1] = 64;
+               parameters->cblock_init[2] = 64;
+               parameters->prog_order = LRCP;
+               parameters->roi_compno = -1;            /* no ROI */
+               parameters->atk_wt[0] = 1;                              /* 5-3 WT */
+               parameters->atk_wt[1] = 1;                              /* 5-3 WT */
+               parameters->atk_wt[2] = 1;                              /* 5-3 WT */
+               parameters->irreversible = 0;
+               parameters->subsampling_dx = 1;
+               parameters->subsampling_dy = 1;
+               parameters->subsampling_dz = 1;
+
+               parameters->decod_format = -1;
+               parameters->cod_format = -1;
+               parameters->encoding_format = ENCOD_2EB;
+               parameters->transform_format = TRF_2D_DWT;
+       }
+}
+
+void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_volume_t *volume) {
+       if(cinfo && parameters && volume) {
+               if (cinfo->codec_format != CODEC_UNKNOWN) {
+                       j3d_setup_encoder((opj_j3d_t*)cinfo->j3d_handle, parameters, volume);
+               }
+       }
+}
+
+bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_volume_t *volume, char *index) {
+       if(cinfo && cio && volume) {
+               if (cinfo->codec_format != CODEC_UNKNOWN) {
+                       return j3d_encode((opj_j3d_t*)cinfo->j3d_handle, cio, volume, index);
+               }
+       }
+
+       return false;
+}
+
+
index 44fe1f70a14ee75b92bacf605985d96c27506620..3657f92bb2b36531f1164aa54cb582a9be82060e 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-#ifndef OPENJPEG_H\r
-#define OPENJPEG_H\r
-\r
-/* \r
-==========================================================\r
-   Compiler directives\r
-==========================================================\r
-*/\r
-\r
-#if defined(OPJ_STATIC) || !defined(_WIN32)\r
-/* http://gcc.gnu.org/wiki/Visibility */\r
-#if __GNUC__ >= 4\r
-#define OPJ_API    __attribute__ ((visibility ("default")))\r
-#define OPJ_LOCAL  __attribute__ ((visibility ("hidden")))\r
-#else\r
-#define OPJ_API\r
-#define OPJ_LOCAL\r
-#endif\r
-#define OPJ_CALLCONV\r
-#else\r
-#define OPJ_CALLCONV __stdcall\r
-\r
-/*\r
-The following ifdef block is the standard way of creating macros which make exporting \r
-from a DLL simpler. All files within this DLL are compiled with the OPJ_EXPORTS\r
-symbol defined on the command line. this symbol should not be defined on any project\r
-that uses this DLL. This way any other project whose source files include this file see \r
-OPJ_API functions as being imported from a DLL, wheras this DLL sees symbols\r
-defined with this macro as being exported.\r
-*/\r
-#if defined(OPJ_EXPORTS) || defined(DLL_EXPORT)\r
-#define OPJ_API __declspec(dllexport)\r
-#else\r
-#define OPJ_API __declspec(dllimport)\r
-#endif /* OPJ_EXPORTS */\r
-#endif /* !OPJ_STATIC || !WIN32 */\r
-\r
-#ifndef __cplusplus\r
-#if defined(HAVE_STDBOOL_H)\r
-/*\r
-The C language implementation does correctly provide the standard header\r
-file "stdbool.h".\r
- */\r
-#include <stdbool.h>\r
-#else\r
-/*\r
-The C language implementation does not provide the standard header file\r
-"stdbool.h" as required by ISO/IEC 9899:1999.  Try to compensate for this\r
-braindamage below.\r
-*/\r
-#if !defined(bool)\r
-#define        bool    int\r
-#endif\r
-#if !defined(true)\r
-#define true   1\r
-#endif\r
-#if !defined(false)\r
-#define        false   0\r
-#endif\r
-#endif\r
-#endif /* __cplusplus */\r
-\r
-/* \r
-==========================================================\r
-   Useful constant definitions\r
-==========================================================\r
-*/\r
-#ifndef MAX_SLICES\r
-#define MAX_SLICES 300 /**< Maximum allowed size for slices */\r
-#endif /* MAX_PATH */\r
-\r
-#ifndef MAX_PATH\r
-#define MAX_PATH 260   /**< Maximum allowed size for filenames */\r
-#endif /* MAX_PATH */\r
-\r
-#define J3D_MAXRLVLS 32                                        /**< Number of maximum resolution level authorized */\r
-#define J3D_MAXBANDS (7*J3D_MAXRLVLS + 1)      /**< Number of maximum sub-band linked to number of resolution level */\r
-\r
-#define TINY 1.0E-20\r
-/* \r
-==========================================================\r
-   enum definitions\r
-==========================================================\r
-*/\r
-\r
-#define J2K_CFMT 0\r
-#define J3D_CFMT 1\r
-#define LSE_CFMT 2\r
-\r
-#define BIN_DFMT 0\r
-#define PGX_DFMT 1\r
-#define IMG_DFMT 2\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/** Progression order */\r
-typedef enum PROG_ORDER {\r
-/**< place-holder */   \r
-       PROG_UNKNOWN = -1,      \r
-/**< layer-resolution-component-precinct order */      \r
-       LRCP = 0,               \r
-/**< resolution-layer-component-precinct order */      \r
-       RLCP = 1,               \r
-/**< resolution-precinct-component-layer order */      \r
-       RPCL = 2,               \r
-/**< precinct-component-resolution-layer order */      \r
-       PCRL = 3,               \r
-/**< component-precinct-resolution-layer order */      \r
-       CPRL = 4                \r
-} OPJ_PROG_ORDER;\r
-\r
-/**\r
-Supported volume color spaces\r
-*/\r
-typedef enum COLOR_SPACE {\r
-/**< place-holder */   \r
-       CLRSPC_UNKNOWN = -1,    \r
-/**< sRGB */   \r
-       CLRSPC_SRGB = 1,                \r
-/**< grayscale */      \r
-       CLRSPC_GRAY = 2,                \r
-/**< YUV */    \r
-       CLRSPC_SYCC = 3                 \r
-} OPJ_COLOR_SPACE;\r
-\r
-/**\r
-Supported codec\r
-*/\r
-typedef enum CODEC_FORMAT {\r
-       /**< place-holder */\r
-       CODEC_UNKNOWN = -1,\r
-/**< JPEG-2000 codestream : read/write */\r
-       CODEC_J2K = 0,          \r
-/**< JPEG-2000 Part 10 file format : read/write */\r
-       CODEC_J3D = 1           \r
-} OPJ_CODEC_FORMAT;\r
-\r
-/**\r
-Supported entropy coding algorithms\r
-*/\r
-typedef enum ENTROPY_CODING {\r
-/**< place-holder */\r
-       ENCOD_UNKNOWN = -1,\r
-/**< 2D EBCOT encoding */\r
-       ENCOD_2EB = 0,          \r
-/**< 3D EBCOT encoding */\r
-       ENCOD_3EB = 1,          \r
-/**< Golomb-Rice coding with 2D context */\r
-       ENCOD_2GR = 2,          \r
-/**< Golomb-Rice coding with 3D context  */\r
-       ENCOD_3GR = 3           \r
-} OPJ_ENTROPY_CODING;\r
-\r
-/**\r
-Supported transforms\r
-*/\r
-typedef enum TRANSFORM {\r
-/**< place-holder */\r
-       TRF_UNKNOWN = -1,       \r
-/**< 2D DWT, no transform in axial dim */\r
-       TRF_2D_DWT = 0,         \r
-/**< 3D DWT */\r
-       TRF_3D_DWT = 1,         \r
-/**< 3D prediction*/\r
-       TRF_3D_RLS = 2,         \r
-       TRF_3D_LSE = 3\r
-} OPJ_TRANSFORM;\r
-/* \r
-==========================================================\r
-   event manager typedef definitions\r
-==========================================================\r
-*/\r
-\r
-/**\r
-Callback function prototype for events\r
-@param msg Event message\r
-@param client_data \r
-*/\r
-typedef void (*opj_msg_callback) (const char *msg, void *client_data);\r
-\r
-/**\r
-Message handler object\r
-used for \r
-<ul>\r
-<li>Error messages\r
-<li>Warning messages\r
-<li>Debugging messages\r
-</ul>\r
-*/\r
-typedef struct opj_event_mgr {\r
-       /** Error message callback if available, NULL otherwise */\r
-       opj_msg_callback error_handler;\r
-       /** Warning message callback if available, NULL otherwise */\r
-       opj_msg_callback warning_handler;\r
-       /** Debug message callback if available, NULL otherwise */\r
-       opj_msg_callback info_handler;\r
-} opj_event_mgr_t;\r
-\r
-\r
-/* \r
-==========================================================\r
-   codec typedef definitions\r
-==========================================================\r
-*/\r
-\r
-/**\r
-Progression order changes\r
-*/\r
-typedef struct opj_poc {\r
-  int resno0, compno0;\r
-  int layno1, resno1, compno1;\r
-  OPJ_PROG_ORDER prg;\r
-  int tile;\r
-  char progorder[4];\r
-} opj_poc_t;\r
-\r
-\r
-/**\r
-Compression parameters\r
-*/\r
-typedef struct opj_cparameters {\r
-/** size of tile: tile_size_on = false (not in argument) or = true (in argument) */\r
-       bool tile_size_on;      \r
-/** XTOsiz */\r
-       int cp_tx0;\r
-/** YTOsiz */\r
-       int cp_ty0;     \r
-/** ZTOsiz */\r
-       int cp_tz0;             \r
-\r
-/** XTsiz */\r
-       int cp_tdx;     \r
-/** YTsiz */\r
-       int cp_tdy;     \r
-/** ZTsiz */\r
-       int cp_tdz;             \r
-                                                       \r
-/** allocation by rate/distortion */\r
-       int cp_disto_alloc;             \r
-/** allocation by fixed layer */               \r
-       int cp_fixed_alloc;             \r
-/** add fixed_quality */       \r
-       int cp_fixed_quality;   \r
-/** fixed layer */\r
-       int *cp_matrice;\r
-/** number of layers */\r
-       int tcp_numlayers;\r
-/** rates for successive layers */\r
-       float tcp_rates[100];\r
-/** psnr's for successive layers */\r
-       float tcp_distoratio[100];      \r
-/** comment for coding */\r
-       char *cp_comment;\r
-/** csty : coding style */\r
-       int csty;                                       \r
-/** DC offset (DCO) */\r
-       int dcoffset;\r
-/** progression order (default LRCP) */\r
-       OPJ_PROG_ORDER prog_order;      \r
-/** progression order changes */\r
-       opj_poc_t POC[J3D_MAXRLVLS-1];  \r
-/** number of progression order changes (POC), default to 0 */\r
-       int numpocs;                            \r
-\r
-/** number of resolutions */\r
-       int numresolution[3];           \r
-/** initial code block width, height and depth, default to 64 */\r
-       int cblock_init[3];                     \r
-/** mode switch (1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL) 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)) */\r
-       int mode;                                       \r
-\r
-/** 1 : use the irreversible DWT 9-7, 0 : use lossless compression (default) */\r
-       int irreversible;                       \r
-/** WT from ATK, default to 0 (false), no of atk used */\r
-       int atk_wt[3];\r
-/** region of interest: affected component in [0..3], -1 means no ROI */\r
-       int roi_compno;                         \r
-/** region of interest: upshift value */\r
-       int roi_shift;                          \r
-\r
-/* number of precinct size specifications */\r
-       int res_spec;                           \r
-/** initial precinct width */\r
-       int prct_init[3][J3D_MAXRLVLS]; \r
-\r
-/** transform format 0: 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */\r
-       OPJ_TRANSFORM transform_format; \r
-/** output file format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI */\r
-       OPJ_ENTROPY_CODING encoding_format;     \r
-\r
-       /**@name command line encoder parameters (not used inside the library) */\r
-       /*@{*/\r
-       char infile[MAX_PATH];          /** input file name */\r
-       char outfile[MAX_PATH];         /** output file name */\r
-       char imgfile[MAX_PATH];         /** IMG file name for BIN volumes*/\r
-       int index_on;                           /** creation of an index file, default to 0 (false) */\r
-       char index[MAX_PATH];           /** index file name */\r
-       \r
-       int volume_offset_x0;           /** subvolume encoding: origin volume offset in x, y and z direction */\r
-       int volume_offset_y0;\r
-       int volume_offset_z0;\r
-       \r
-       int subsampling_dx;                     /** subsampling value for dx */\r
-       int subsampling_dy;\r
-       int subsampling_dz;\r
-       \r
-       int decod_format;                       /** input file format 0: BIN, 1: PGX */\r
-       int cod_format;                         /** output file format 0: JP3D */\r
-       /*@}*/\r
-} opj_cparameters_t;\r
-\r
-/**\r
-Decompression parameters\r
-*/\r
-typedef struct opj_dparameters {\r
-/** Set the number of highest resolution levels to be discarded. if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, volume is decoded to the full resolution */\r
-       int cp_reduce[3];               \r
-/** Set the maximum number of quality layers to decode. if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded       */\r
-       int cp_layer;   \r
-       int bigendian;\r
-       \r
-       /**@name command line encoder parameters (not used inside the library) */\r
-       /*@{*/\r
-/** input file name */\r
-       char infile[MAX_PATH];  \r
-/** output file name */\r
-       char outfile[MAX_PATH]; \r
-/** IMG file name for BIN volumes*/\r
-       char imgfile[MAX_PATH];\r
-/** Original file name for PSNR measures*/\r
-       char original[MAX_PATH];\r
-/** input file format 0: J2K, 1: JP3D */\r
-       int decod_format;               \r
-/** input file format 0: BIN, 1: PGM */\r
-       int cod_format;\r
-/** original file format 0: BIN, 1: PGM */\r
-       int orig_format;        \r
-       /*@}*/\r
-} opj_dparameters_t;\r
-\r
-/** Common fields between JPEG-2000 compression and decompression master structs. */\r
-#define opj_common_fields \\r
-       opj_event_mgr_t *event_mgr;     /**< pointer to the event manager */\\r
-       void * client_data;                     /**< Available for use by application */\\r
-       bool is_decompressor;           /**< So common code can tell which is which */\\r
-       OPJ_CODEC_FORMAT codec_format;          /**< selected codec */\\r
-       OPJ_ENTROPY_CODING encoding_format;     /**< selected entropy coding */\\r
-       OPJ_TRANSFORM transform_format;         /**< selected transform */\\r
-       void *j3d_handle                        /**< pointer to the J3D codec */\r
-       \r
-/* Routines that are to be used by both halves of the library are declared\r
- * to receive a pointer to this structure.  There are no actual instances of\r
- * opj_common_struct_t, only of opj_cinfo_t and opj_dinfo_t.\r
- */\r
-typedef struct opj_common_struct {\r
-  opj_common_fields;           /* Fields common to both master struct types */\r
-  /* Additional fields follow in an actual opj_cinfo_t or\r
-   * opj_dinfo_t.  All three structs must agree on these\r
-   * initial fields!  (This would be a lot cleaner in C++.)\r
-   */\r
-} opj_common_struct_t;\r
-\r
-typedef opj_common_struct_t * opj_common_ptr;\r
-\r
-/**\r
-Compression context info\r
-*/\r
-typedef struct opj_cinfo {\r
-       /** Fields shared with opj_dinfo_t */\r
-       opj_common_fields;      \r
-       /* other specific fields go here */\r
-} opj_cinfo_t;\r
-\r
-/**\r
-Decompression context info\r
-*/\r
-typedef struct opj_dinfo {\r
-       /** Fields shared with opj_cinfo_t */\r
-       opj_common_fields;      \r
-       /* other specific fields go here */\r
-} opj_dinfo_t;\r
-\r
-/* \r
-==========================================================\r
-   I/O stream typedef definitions\r
-==========================================================\r
-*/\r
-\r
-/*\r
- * Stream open flags.\r
- */\r
-/** The stream was opened for reading. */\r
-#define OPJ_STREAM_READ        0x0001\r
-/** The stream was opened for writing. */\r
-#define OPJ_STREAM_WRITE 0x0002\r
-\r
-/**\r
-Byte input-output stream (CIO)\r
-*/\r
-typedef struct opj_cio {\r
-/** codec context */\r
-       opj_common_ptr cinfo;   \r
-/** open mode (read/write) either OPJ_STREAM_READ or OPJ_STREAM_WRITE */\r
-       int openmode;                   \r
-/** pointer to the start of the buffer */\r
-       unsigned char *buffer;  \r
-/** buffer size in bytes */\r
-       int length;                             \r
-/** pointer to the start of the stream */\r
-       unsigned char *start;           \r
-/** pointer to the end of the stream */\r
-       unsigned char *end;                     \r
-/** pointer to the current position */\r
-       unsigned char *bp;                      \r
-} opj_cio_t;\r
-\r
-/* \r
-==========================================================\r
-   volume typedef definitions\r
-==========================================================\r
-*/\r
-\r
-/**\r
-Defines a single volume component\r
-*/\r
-typedef struct opj_volume_comp {\r
-/** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */\r
-       int dx; \r
-/** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */\r
-       int dy; \r
-/** ZRsiz: vertical separation of a sample of ith component with respect to the reference grid */\r
-       int dz;\r
-/** data width */\r
-       int w;  \r
-       /** data height */\r
-       int h;  \r
-       /** data length : no of slices */\r
-       int l;  \r
-       /** x component offset compared to the whole volume */\r
-       int x0; \r
-       /** y component offset compared to the whole volume */\r
-       int y0; \r
-       /** z component offset compared to the whole volume */\r
-       int z0; \r
-       /** precision */\r
-       int prec;       \r
-       /** volume depth in bits */\r
-       int bpp;        \r
-       /** DC offset (15444-2) */\r
-       int dcoffset;\r
-       /** signed (1) / unsigned (0) */\r
-       int sgnd;               \r
-       /** BE byte order (1) / LE byte order  (0) */\r
-       int bigendian;\r
-       /** number of decoded resolution */\r
-       int resno_decoded[3];   \r
-       /** number of division by 2 of the out volume compared to the original size of volume */\r
-       int factor[3];  \r
-       /** volume component data */\r
-       int *data;                              \r
-} opj_volume_comp_t;\r
-\r
-/** \r
-Defines volume data and characteristics\r
-*/\r
-typedef struct opj_volume {\r
-/** XOsiz: horizontal offset from the origin of the reference grid to the left side of the volume area */\r
-       int x0; \r
-/** YOsiz: vertical offset from the origin of the reference grid to the top side of the volume area */\r
-       int y0; \r
-/** ZOsiz: vertical offset from the origin of the reference grid to the top side of the volume area */\r
-       int z0; \r
-/** Xsiz: width of the reference grid */\r
-       int x1; \r
-/** Ysiz: height of the reference grid */\r
-       int y1; \r
-/** Zsiz: length of the reference grid */\r
-       int z1; \r
-/** number of components in the volume */\r
-       int numcomps;   \r
-/** number of slices in the volume */\r
-       int numslices;  \r
-/** color space: sRGB, Greyscale or YUV */\r
-       OPJ_COLOR_SPACE color_space;\r
-/** volume components */\r
-       opj_volume_comp_t *comps;       \r
-} opj_volume_t;\r
-\r
-/**\r
-Component parameters structure used by the opj_volume_create function\r
-*/\r
-typedef struct opj_volume_comptparm {\r
-       /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */\r
-       int dx; \r
-       /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */\r
-       int dy; \r
-       /** ZRsiz: axial separation of a sample of ith component with respect to the reference grid */\r
-       int dz; \r
-       /** data width */\r
-       int w;  \r
-       /** data height */\r
-       int h;  \r
-       /** data length */\r
-       int l;  \r
-       /** x component offset compared to the whole volume */\r
-       int x0; \r
-       /** y component offset compared to the whole volume */\r
-       int y0;\r
-       /** z component offset compared to the whole volume */\r
-       int z0; \r
-       /** precision */\r
-       int prec;\r
-       /** volume depth in bits */\r
-       int bpp;        \r
-       /** signed (1) / unsigned (0) */\r
-       int sgnd;\r
-       /** DC offset*/\r
-       int dcoffset;\r
-       /** BE byte order (1) / LE byte order  (0) */\r
-       int bigendian;\r
-} opj_volume_cmptparm_t;\r
-\r
-#ifdef __cplusplus\r
-extern "C" {\r
-#endif\r
-\r
-\r
-/* \r
-==========================================================\r
-   openjpeg version\r
-==========================================================\r
-*/\r
-\r
-OPJ_API const char * OPJ_CALLCONV opj_version(void);\r
-\r
-/* \r
-==========================================================\r
-   volume functions definitions\r
-==========================================================\r
-*/\r
-\r
-/**\r
-Create an volume\r
-@param numcmpts number of components\r
-@param cmptparms components parameters\r
-@param clrspc volume color space\r
-@return returns a new volume structure if successful, returns NULL otherwise\r
-*/\r
-OPJ_API opj_volume_t* OPJ_CALLCONV opj_volume_create(int numcmpts, opj_volume_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc);\r
-\r
-/**\r
-Deallocate any resources associated with an volume\r
-@param volume volume to be destroyed\r
-*/\r
-OPJ_API void OPJ_CALLCONV opj_volume_destroy(opj_volume_t *volume);\r
-\r
-/* \r
-==========================================================\r
-   stream functions definitions\r
-==========================================================\r
-*/\r
-\r
-/**\r
-Open and allocate a memory stream for read / write. \r
-On reading, the user must provide a buffer containing encoded data. The buffer will be \r
-wrapped by the returned CIO handle. \r
-On writing, buffer parameters must be set to 0: a buffer will be allocated by the library \r
-to contain encoded data. \r
-@param cinfo Codec context info\r
-@param buffer Reading: buffer address. Writing: NULL\r
-@param length Reading: buffer length. Writing: 0\r
-@return Returns a CIO handle if successful, returns NULL otherwise\r
-*/\r
-OPJ_API opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length);\r
-\r
-/**\r
-Close and free a CIO handle\r
-@param cio CIO handle to free\r
-*/\r
-OPJ_API void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio);\r
-\r
-/**\r
-Get position in byte stream\r
-@param cio CIO handle\r
-@return Returns the position in bytes\r
-*/\r
-OPJ_API int OPJ_CALLCONV cio_tell(opj_cio_t *cio);\r
-/**\r
-Set position in byte stream\r
-@param cio CIO handle\r
-@param pos Position, in number of bytes, from the beginning of the stream\r
-*/\r
-OPJ_API void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos);\r
-\r
-/* \r
-==========================================================\r
-   event manager functions definitions\r
-==========================================================\r
-*/\r
-\r
-OPJ_API opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context);\r
-\r
-/* \r
-==========================================================\r
-   codec functions definitions\r
-==========================================================\r
-*/\r
-/**\r
-Creates a J3D decompression structure\r
-@param format Decoder to select\r
-@return Returns a handle to a decompressor if successful, returns NULL otherwise\r
-*/\r
-OPJ_API opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format);\r
-/**\r
-Destroy a decompressor handle\r
-@param dinfo decompressor handle to destroy\r
-*/\r
-OPJ_API void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo);\r
-/**\r
-Set decoding parameters to default values\r
-@param parameters Decompression parameters\r
-*/\r
-OPJ_API void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters);\r
-/**\r
-Setup the decoder decoding parameters using user parameters.\r
-Decoding parameters are returned in j3d->cp. \r
-@param dinfo decompressor handle\r
-@param parameters decompression parameters\r
-*/\r
-OPJ_API void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters);\r
-/**\r
-Decode an volume from a JPEG-2000 codestream\r
-@param dinfo decompressor handle\r
-@param cio Input buffer stream\r
-@return Returns a decoded volume if successful, returns NULL otherwise\r
-*/\r
-OPJ_API opj_volume_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio);\r
-/**\r
-Creates a J3D/JP2 compression structure\r
-@param format Coder to select\r
-@return Returns a handle to a compressor if successful, returns NULL otherwise\r
-*/\r
-OPJ_API opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format);\r
-/**\r
-Destroy a compressor handle\r
-@param cinfo compressor handle to destroy\r
-*/\r
-OPJ_API void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo);\r
-/**\r
-Set encoding parameters to default values, that means : \r
-<ul>\r
-<li>Lossless\r
-<li>1 tile\r
-<li>Size of precinct : 2^15 x 2^15 (means 1 precinct)\r
-<li>Size of code-block : 64 x 64\r
-<li>Number of resolutions: 6\r
-<li>No SOP marker in the codestream\r
-<li>No EPH marker in the codestream\r
-<li>No sub-sampling in x or y direction\r
-<li>No mode switch activated\r
-<li>Progression order: LRCP\r
-<li>No index file\r
-<li>No ROI upshifted\r
-<li>No offset of the origin of the volume\r
-<li>No offset of the origin of the tiles\r
-<li>Reversible DWT 5-3\r
-</ul>\r
-@param parameters Compression parameters\r
-*/\r
-OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters);\r
-/**\r
-Setup the encoder parameters using the current volume and using user parameters. \r
-@param cinfo compressor handle\r
-@param parameters compression parameters\r
-@param volume input filled volume\r
-*/\r
-OPJ_API void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_volume_t *volume);\r
-/**\r
-Encode an volume into a JPEG-2000 codestream\r
-@param cinfo compressor handle\r
-@param cio Output buffer stream\r
-@param volume Volume to encode\r
-@param index Name of the index file if required, NULL otherwise\r
-@return Returns true if successful, returns false otherwise\r
-*/\r
-OPJ_API bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_volume_t *volume, char *index);\r
-\r
-#ifdef __cplusplus\r
-}\r
-#endif\r
-\r
-#endif /* OPENJPEG_H */\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef OPENJPEG_H
+#define OPENJPEG_H
+
+/* 
+==========================================================
+   Compiler directives
+==========================================================
+*/
+
+#if defined(OPJ_STATIC) || !defined(_WIN32)
+/* http://gcc.gnu.org/wiki/Visibility */
+#if __GNUC__ >= 4
+#define OPJ_API    __attribute__ ((visibility ("default")))
+#define OPJ_LOCAL  __attribute__ ((visibility ("hidden")))
+#else
+#define OPJ_API
+#define OPJ_LOCAL
+#endif
+#define OPJ_CALLCONV
+#else
+#define OPJ_CALLCONV __stdcall
+
+/*
+The following ifdef block is the standard way of creating macros which make exporting 
+from a DLL simpler. All files within this DLL are compiled with the OPJ_EXPORTS
+symbol defined on the command line. this symbol should not be defined on any project
+that uses this DLL. This way any other project whose source files include this file see 
+OPJ_API functions as being imported from a DLL, wheras this DLL sees symbols
+defined with this macro as being exported.
+*/
+#if defined(OPJ_EXPORTS) || defined(DLL_EXPORT)
+#define OPJ_API __declspec(dllexport)
+#else
+#define OPJ_API __declspec(dllimport)
+#endif /* OPJ_EXPORTS */
+#endif /* !OPJ_STATIC || !WIN32 */
+
+#ifndef __cplusplus
+#if defined(HAVE_STDBOOL_H)
+/*
+The C language implementation does correctly provide the standard header
+file "stdbool.h".
+ */
+#include <stdbool.h>
+#else
+/*
+The C language implementation does not provide the standard header file
+"stdbool.h" as required by ISO/IEC 9899:1999.  Try to compensate for this
+braindamage below.
+*/
+#if !defined(bool)
+#define        bool    int
+#endif
+#if !defined(true)
+#define true   1
+#endif
+#if !defined(false)
+#define        false   0
+#endif
+#endif
+#endif /* __cplusplus */
+
+/* 
+==========================================================
+   Useful constant definitions
+==========================================================
+*/
+#ifndef MAX_SLICES
+#define MAX_SLICES 300 /**< Maximum allowed size for slices */
+#endif /* MAX_PATH */
+
+#ifndef MAX_PATH
+#define MAX_PATH 260   /**< Maximum allowed size for filenames */
+#endif /* MAX_PATH */
+
+#define J3D_MAXRLVLS 32                                        /**< Number of maximum resolution level authorized */
+#define J3D_MAXBANDS (7*J3D_MAXRLVLS + 1)      /**< Number of maximum sub-band linked to number of resolution level */
+
+#define TINY 1.0E-20
+/* 
+==========================================================
+   enum definitions
+==========================================================
+*/
+
+#define J2K_CFMT 0
+#define J3D_CFMT 1
+#define LSE_CFMT 2
+
+#define BIN_DFMT 0
+#define PGX_DFMT 1
+#define IMG_DFMT 2
+/* ----------------------------------------------------------------------- */
+
+/** Progression order */
+typedef enum PROG_ORDER {
+/**< place-holder */   
+       PROG_UNKNOWN = -1,      
+/**< layer-resolution-component-precinct order */      
+       LRCP = 0,               
+/**< resolution-layer-component-precinct order */      
+       RLCP = 1,               
+/**< resolution-precinct-component-layer order */      
+       RPCL = 2,               
+/**< precinct-component-resolution-layer order */      
+       PCRL = 3,               
+/**< component-precinct-resolution-layer order */      
+       CPRL = 4                
+} OPJ_PROG_ORDER;
+
+/**
+Supported volume color spaces
+*/
+typedef enum COLOR_SPACE {
+/**< place-holder */   
+       CLRSPC_UNKNOWN = -1,    
+/**< sRGB */   
+       CLRSPC_SRGB = 1,                
+/**< grayscale */      
+       CLRSPC_GRAY = 2,                
+/**< YUV */    
+       CLRSPC_SYCC = 3                 
+} OPJ_COLOR_SPACE;
+
+/**
+Supported codec
+*/
+typedef enum CODEC_FORMAT {
+       /**< place-holder */
+       CODEC_UNKNOWN = -1,
+/**< JPEG-2000 codestream : read/write */
+       CODEC_J2K = 0,          
+/**< JPEG-2000 Part 10 file format : read/write */
+       CODEC_J3D = 1           
+} OPJ_CODEC_FORMAT;
+
+/**
+Supported entropy coding algorithms
+*/
+typedef enum ENTROPY_CODING {
+/**< place-holder */
+       ENCOD_UNKNOWN = -1,
+/**< 2D EBCOT encoding */
+       ENCOD_2EB = 0,          
+/**< 3D EBCOT encoding */
+       ENCOD_3EB = 1,          
+/**< Golomb-Rice coding with 2D context */
+       ENCOD_2GR = 2,          
+/**< Golomb-Rice coding with 3D context  */
+       ENCOD_3GR = 3           
+} OPJ_ENTROPY_CODING;
+
+/**
+Supported transforms
+*/
+typedef enum TRANSFORM {
+/**< place-holder */
+       TRF_UNKNOWN = -1,       
+/**< 2D DWT, no transform in axial dim */
+       TRF_2D_DWT = 0,         
+/**< 3D DWT */
+       TRF_3D_DWT = 1,         
+/**< 3D prediction*/
+       TRF_3D_RLS = 2,         
+       TRF_3D_LSE = 3
+} OPJ_TRANSFORM;
+/* 
+==========================================================
+   event manager typedef definitions
+==========================================================
+*/
+
+/**
+Callback function prototype for events
+@param msg Event message
+@param client_data 
+*/
+typedef void (*opj_msg_callback) (const char *msg, void *client_data);
+
+/**
+Message handler object
+used for 
+<ul>
+<li>Error messages
+<li>Warning messages
+<li>Debugging messages
+</ul>
+*/
+typedef struct opj_event_mgr {
+       /** Error message callback if available, NULL otherwise */
+       opj_msg_callback error_handler;
+       /** Warning message callback if available, NULL otherwise */
+       opj_msg_callback warning_handler;
+       /** Debug message callback if available, NULL otherwise */
+       opj_msg_callback info_handler;
+} opj_event_mgr_t;
+
+
+/* 
+==========================================================
+   codec typedef definitions
+==========================================================
+*/
+
+/**
+Progression order changes
+*/
+typedef struct opj_poc {
+  int resno0, compno0;
+  int layno1, resno1, compno1;
+  OPJ_PROG_ORDER prg;
+  int tile;
+  char progorder[4];
+} opj_poc_t;
+
+
+/**
+Compression parameters
+*/
+typedef struct opj_cparameters {
+/** size of tile: tile_size_on = false (not in argument) or = true (in argument) */
+       bool tile_size_on;      
+/** XTOsiz */
+       int cp_tx0;
+/** YTOsiz */
+       int cp_ty0;     
+/** ZTOsiz */
+       int cp_tz0;             
+
+/** XTsiz */
+       int cp_tdx;     
+/** YTsiz */
+       int cp_tdy;     
+/** ZTsiz */
+       int cp_tdz;             
+                                                       
+/** allocation by rate/distortion */
+       int cp_disto_alloc;             
+/** allocation by fixed layer */               
+       int cp_fixed_alloc;             
+/** add fixed_quality */       
+       int cp_fixed_quality;   
+/** fixed layer */
+       int *cp_matrice;
+/** number of layers */
+       int tcp_numlayers;
+/** rates for successive layers */
+       float tcp_rates[100];
+/** psnr's for successive layers */
+       float tcp_distoratio[100];      
+/** comment for coding */
+       char *cp_comment;
+/** csty : coding style */
+       int csty;                                       
+/** DC offset (DCO) */
+       int dcoffset;
+/** progression order (default LRCP) */
+       OPJ_PROG_ORDER prog_order;      
+/** progression order changes */
+       opj_poc_t POC[J3D_MAXRLVLS-1];  
+/** number of progression order changes (POC), default to 0 */
+       int numpocs;                            
+
+/** number of resolutions */
+       int numresolution[3];           
+/** initial code block width, height and depth, default to 64 */
+       int cblock_init[3];                     
+/** mode switch (1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL) 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)) */
+       int mode;                                       
+
+/** 1 : use the irreversible DWT 9-7, 0 : use lossless compression (default) */
+       int irreversible;                       
+/** WT from ATK, default to 0 (false), no of atk used */
+       int atk_wt[3];
+/** region of interest: affected component in [0..3], -1 means no ROI */
+       int roi_compno;                         
+/** region of interest: upshift value */
+       int roi_shift;                          
+
+/* number of precinct size specifications */
+       int res_spec;                           
+/** initial precinct width */
+       int prct_init[3][J3D_MAXRLVLS]; 
+
+/** transform format 0: 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */
+       OPJ_TRANSFORM transform_format; 
+/** output file format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI */
+       OPJ_ENTROPY_CODING encoding_format;     
+
+       /**@name command line encoder parameters (not used inside the library) */
+       /*@{*/
+       char infile[MAX_PATH];          /** input file name */
+       char outfile[MAX_PATH];         /** output file name */
+       char imgfile[MAX_PATH];         /** IMG file name for BIN volumes*/
+       int index_on;                           /** creation of an index file, default to 0 (false) */
+       char index[MAX_PATH];           /** index file name */
+       
+       int volume_offset_x0;           /** subvolume encoding: origin volume offset in x, y and z direction */
+       int volume_offset_y0;
+       int volume_offset_z0;
+       
+       int subsampling_dx;                     /** subsampling value for dx */
+       int subsampling_dy;
+       int subsampling_dz;
+       
+       int decod_format;                       /** input file format 0: BIN, 1: PGX */
+       int cod_format;                         /** output file format 0: JP3D */
+       /*@}*/
+} opj_cparameters_t;
+
+/**
+Decompression parameters
+*/
+typedef struct opj_dparameters {
+/** Set the number of highest resolution levels to be discarded. if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, volume is decoded to the full resolution */
+       int cp_reduce[3];               
+/** Set the maximum number of quality layers to decode. if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded       */
+       int cp_layer;   
+       int bigendian;
+       
+       /**@name command line encoder parameters (not used inside the library) */
+       /*@{*/
+/** input file name */
+       char infile[MAX_PATH];  
+/** output file name */
+       char outfile[MAX_PATH]; 
+/** IMG file name for BIN volumes*/
+       char imgfile[MAX_PATH];
+/** Original file name for PSNR measures*/
+       char original[MAX_PATH];
+/** input file format 0: J2K, 1: JP3D */
+       int decod_format;               
+/** input file format 0: BIN, 1: PGM */
+       int cod_format;
+/** original file format 0: BIN, 1: PGM */
+       int orig_format;        
+       /*@}*/
+} opj_dparameters_t;
+
+/** Common fields between JPEG-2000 compression and decompression master structs. */
+#define opj_common_fields \
+       opj_event_mgr_t *event_mgr;     /**< pointer to the event manager */\
+       void * client_data;                     /**< Available for use by application */\
+       bool is_decompressor;           /**< So common code can tell which is which */\
+       OPJ_CODEC_FORMAT codec_format;          /**< selected codec */\
+       OPJ_ENTROPY_CODING encoding_format;     /**< selected entropy coding */\
+       OPJ_TRANSFORM transform_format;         /**< selected transform */\
+       void *j3d_handle                        /**< pointer to the J3D codec */
+       
+/* Routines that are to be used by both halves of the library are declared
+ * to receive a pointer to this structure.  There are no actual instances of
+ * opj_common_struct_t, only of opj_cinfo_t and opj_dinfo_t.
+ */
+typedef struct opj_common_struct {
+  opj_common_fields;           /* Fields common to both master struct types */
+  /* Additional fields follow in an actual opj_cinfo_t or
+   * opj_dinfo_t.  All three structs must agree on these
+   * initial fields!  (This would be a lot cleaner in C++.)
+   */
+} opj_common_struct_t;
+
+typedef opj_common_struct_t * opj_common_ptr;
+
+/**
+Compression context info
+*/
+typedef struct opj_cinfo {
+       /** Fields shared with opj_dinfo_t */
+       opj_common_fields;      
+       /* other specific fields go here */
+} opj_cinfo_t;
+
+/**
+Decompression context info
+*/
+typedef struct opj_dinfo {
+       /** Fields shared with opj_cinfo_t */
+       opj_common_fields;      
+       /* other specific fields go here */
+} opj_dinfo_t;
+
+/* 
+==========================================================
+   I/O stream typedef definitions
+==========================================================
+*/
+
+/*
+ * Stream open flags.
+ */
+/** The stream was opened for reading. */
+#define OPJ_STREAM_READ        0x0001
+/** The stream was opened for writing. */
+#define OPJ_STREAM_WRITE 0x0002
+
+/**
+Byte input-output stream (CIO)
+*/
+typedef struct opj_cio {
+/** codec context */
+       opj_common_ptr cinfo;   
+/** open mode (read/write) either OPJ_STREAM_READ or OPJ_STREAM_WRITE */
+       int openmode;                   
+/** pointer to the start of the buffer */
+       unsigned char *buffer;  
+/** buffer size in bytes */
+       int length;                             
+/** pointer to the start of the stream */
+       unsigned char *start;           
+/** pointer to the end of the stream */
+       unsigned char *end;                     
+/** pointer to the current position */
+       unsigned char *bp;                      
+} opj_cio_t;
+
+/* 
+==========================================================
+   volume typedef definitions
+==========================================================
+*/
+
+/**
+Defines a single volume component
+*/
+typedef struct opj_volume_comp {
+/** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
+       int dx; 
+/** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
+       int dy; 
+/** ZRsiz: vertical separation of a sample of ith component with respect to the reference grid */
+       int dz;
+/** data width */
+       int w;  
+       /** data height */
+       int h;  
+       /** data length : no of slices */
+       int l;  
+       /** x component offset compared to the whole volume */
+       int x0; 
+       /** y component offset compared to the whole volume */
+       int y0; 
+       /** z component offset compared to the whole volume */
+       int z0; 
+       /** precision */
+       int prec;       
+       /** volume depth in bits */
+       int bpp;        
+       /** DC offset (15444-2) */
+       int dcoffset;
+       /** signed (1) / unsigned (0) */
+       int sgnd;               
+       /** BE byte order (1) / LE byte order  (0) */
+       int bigendian;
+       /** number of decoded resolution */
+       int resno_decoded[3];   
+       /** number of division by 2 of the out volume compared to the original size of volume */
+       int factor[3];  
+       /** volume component data */
+       int *data;                              
+} opj_volume_comp_t;
+
+/** 
+Defines volume data and characteristics
+*/
+typedef struct opj_volume {
+/** XOsiz: horizontal offset from the origin of the reference grid to the left side of the volume area */
+       int x0; 
+/** YOsiz: vertical offset from the origin of the reference grid to the top side of the volume area */
+       int y0; 
+/** ZOsiz: vertical offset from the origin of the reference grid to the top side of the volume area */
+       int z0; 
+/** Xsiz: width of the reference grid */
+       int x1; 
+/** Ysiz: height of the reference grid */
+       int y1; 
+/** Zsiz: length of the reference grid */
+       int z1; 
+/** number of components in the volume */
+       int numcomps;   
+/** number of slices in the volume */
+       int numslices;  
+/** color space: sRGB, Greyscale or YUV */
+       OPJ_COLOR_SPACE color_space;
+/** volume components */
+       opj_volume_comp_t *comps;       
+} opj_volume_t;
+
+/**
+Component parameters structure used by the opj_volume_create function
+*/
+typedef struct opj_volume_comptparm {
+       /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
+       int dx; 
+       /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
+       int dy; 
+       /** ZRsiz: axial separation of a sample of ith component with respect to the reference grid */
+       int dz; 
+       /** data width */
+       int w;  
+       /** data height */
+       int h;  
+       /** data length */
+       int l;  
+       /** x component offset compared to the whole volume */
+       int x0; 
+       /** y component offset compared to the whole volume */
+       int y0;
+       /** z component offset compared to the whole volume */
+       int z0; 
+       /** precision */
+       int prec;
+       /** volume depth in bits */
+       int bpp;        
+       /** signed (1) / unsigned (0) */
+       int sgnd;
+       /** DC offset*/
+       int dcoffset;
+       /** BE byte order (1) / LE byte order  (0) */
+       int bigendian;
+} opj_volume_cmptparm_t;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* 
+==========================================================
+   openjpeg version
+==========================================================
+*/
+
+OPJ_API const char * OPJ_CALLCONV opj_version(void);
+
+/* 
+==========================================================
+   volume functions definitions
+==========================================================
+*/
+
+/**
+Create an volume
+@param numcmpts number of components
+@param cmptparms components parameters
+@param clrspc volume color space
+@return returns a new volume structure if successful, returns NULL otherwise
+*/
+OPJ_API opj_volume_t* OPJ_CALLCONV opj_volume_create(int numcmpts, opj_volume_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc);
+
+/**
+Deallocate any resources associated with an volume
+@param volume volume to be destroyed
+*/
+OPJ_API void OPJ_CALLCONV opj_volume_destroy(opj_volume_t *volume);
+
+/* 
+==========================================================
+   stream functions definitions
+==========================================================
+*/
+
+/**
+Open and allocate a memory stream for read / write. 
+On reading, the user must provide a buffer containing encoded data. The buffer will be 
+wrapped by the returned CIO handle. 
+On writing, buffer parameters must be set to 0: a buffer will be allocated by the library 
+to contain encoded data. 
+@param cinfo Codec context info
+@param buffer Reading: buffer address. Writing: NULL
+@param length Reading: buffer length. Writing: 0
+@return Returns a CIO handle if successful, returns NULL otherwise
+*/
+OPJ_API opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length);
+
+/**
+Close and free a CIO handle
+@param cio CIO handle to free
+*/
+OPJ_API void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio);
+
+/**
+Get position in byte stream
+@param cio CIO handle
+@return Returns the position in bytes
+*/
+OPJ_API int OPJ_CALLCONV cio_tell(opj_cio_t *cio);
+/**
+Set position in byte stream
+@param cio CIO handle
+@param pos Position, in number of bytes, from the beginning of the stream
+*/
+OPJ_API void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos);
+
+/* 
+==========================================================
+   event manager functions definitions
+==========================================================
+*/
+
+OPJ_API opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context);
+
+/* 
+==========================================================
+   codec functions definitions
+==========================================================
+*/
+/**
+Creates a J3D decompression structure
+@param format Decoder to select
+@return Returns a handle to a decompressor if successful, returns NULL otherwise
+*/
+OPJ_API opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format);
+/**
+Destroy a decompressor handle
+@param dinfo decompressor handle to destroy
+*/
+OPJ_API void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo);
+/**
+Set decoding parameters to default values
+@param parameters Decompression parameters
+*/
+OPJ_API void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters);
+/**
+Setup the decoder decoding parameters using user parameters.
+Decoding parameters are returned in j3d->cp. 
+@param dinfo decompressor handle
+@param parameters decompression parameters
+*/
+OPJ_API void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters);
+/**
+Decode an volume from a JPEG-2000 codestream
+@param dinfo decompressor handle
+@param cio Input buffer stream
+@return Returns a decoded volume if successful, returns NULL otherwise
+*/
+OPJ_API opj_volume_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio);
+/**
+Creates a J3D/JP2 compression structure
+@param format Coder to select
+@return Returns a handle to a compressor if successful, returns NULL otherwise
+*/
+OPJ_API opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format);
+/**
+Destroy a compressor handle
+@param cinfo compressor handle to destroy
+*/
+OPJ_API void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo);
+/**
+Set encoding parameters to default values, that means : 
+<ul>
+<li>Lossless
+<li>1 tile
+<li>Size of precinct : 2^15 x 2^15 (means 1 precinct)
+<li>Size of code-block : 64 x 64
+<li>Number of resolutions: 6
+<li>No SOP marker in the codestream
+<li>No EPH marker in the codestream
+<li>No sub-sampling in x or y direction
+<li>No mode switch activated
+<li>Progression order: LRCP
+<li>No index file
+<li>No ROI upshifted
+<li>No offset of the origin of the volume
+<li>No offset of the origin of the tiles
+<li>Reversible DWT 5-3
+</ul>
+@param parameters Compression parameters
+*/
+OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters);
+/**
+Setup the encoder parameters using the current volume and using user parameters. 
+@param cinfo compressor handle
+@param parameters compression parameters
+@param volume input filled volume
+*/
+OPJ_API void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_volume_t *volume);
+/**
+Encode an volume into a JPEG-2000 codestream
+@param cinfo compressor handle
+@param cio Output buffer stream
+@param volume Volume to encode
+@param index Name of the index file if required, NULL otherwise
+@return Returns true if successful, returns false otherwise
+*/
+OPJ_API bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_volume_t *volume, char *index);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* OPENJPEG_H */
index 960c1d845fed17ac56a28cbbdef4189af2476aa0..185d26209bb3829610bca4219790452dc5acf6b6 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * Copyright (c) 2006, M�nica D�ez, LPI-UVA, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/** @defgroup PI PI - Implementation of a packet iterator */\r
-/*@{*/\r
-\r
-/** @name Funciones locales */\r
-/*@{*/\r
-\r
-/**\r
-Get next packet in layer-resolution-component-precinct order.\r
-@param pi packet iterator to modify\r
-@return returns false if pi pointed to the last packet or else returns true \r
-*/\r
-static bool pi_next_lrcp(opj_pi_iterator_t * pi);\r
-/**\r
-Get next packet in resolution-layer-component-precinct order.\r
-@param pi packet iterator to modify\r
-@return returns false if pi pointed to the last packet or else returns true \r
-*/\r
-static bool pi_next_rlcp(opj_pi_iterator_t * pi);\r
-/**\r
-Get next packet in resolution-precinct-component-layer order.\r
-@param pi packet iterator to modify\r
-@return returns false if pi pointed to the last packet or else returns true \r
-*/\r
-static bool pi_next_rpcl(opj_pi_iterator_t * pi);\r
-/**\r
-Get next packet in precinct-component-resolution-layer order.\r
-@param pi packet iterator to modify\r
-@return returns false if pi pointed to the last packet or else returns true \r
-*/\r
-static bool pi_next_pcrl(opj_pi_iterator_t * pi);\r
-/**\r
-Get next packet in component-precinct-resolution-layer order.\r
-@param pi packet iterator to modify\r
-@return returns false if pi pointed to the last packet or else returns true \r
-*/\r
-static bool pi_next_cprl(opj_pi_iterator_t * pi);\r
-\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-/* \r
-==========================================================\r
-   local functions\r
-==========================================================\r
-*/\r
-\r
-static bool pi_next_lrcp(opj_pi_iterator_t * pi) {\r
-       opj_pi_comp_t *comp = NULL;\r
-       opj_pi_resolution_t *res = NULL;\r
-       long index = 0;\r
-\r
-       if (!pi->first) {\r
-               comp = &pi->comps[pi->compno];\r
-               res = &comp->resolutions[pi->resno];\r
-               goto LABEL_SKIP;\r
-       } else {\r
-               pi->first = 0;\r
-       }\r
-\r
-       for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {\r
-               for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {\r
-                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {\r
-                               comp = &pi->comps[pi->compno];\r
-                               if (pi->resno >= comp->numresolution[0]) {\r
-                                       continue;\r
-                               }\r
-                               res = &comp->resolutions[pi->resno];\r
-                               /*for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1]); pi->precno++) {*/\r
-                               for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); pi->precno++) {\r
-                                       index = pi->layno * pi->step_l \r
-                                               + pi->resno * pi->step_r \r
-                                               + pi->compno * pi->step_c \r
-                                               + pi->precno * pi->step_p;\r
-                                       if (!pi->include[index]) {\r
-                                               pi->include[index] = 1;\r
-                                               return true;\r
-                                       }\r
-LABEL_SKIP:;\r
-\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-       \r
-       return false;\r
-}\r
-\r
-static bool pi_next_rlcp(opj_pi_iterator_t * pi) {\r
-       opj_pi_comp_t *comp = NULL;\r
-       opj_pi_resolution_t *res = NULL;\r
-       long index = 0;\r
-\r
-       if (!pi->first) {\r
-               comp = &pi->comps[pi->compno];\r
-               res = &comp->resolutions[pi->resno];\r
-               goto LABEL_SKIP;\r
-       } else {\r
-               pi->first = 0;\r
-       }\r
-\r
-       for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {\r
-               for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {\r
-                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {\r
-                               comp = &pi->comps[pi->compno];\r
-                               if (pi->resno >= comp->numresolution[0]) {\r
-                                       continue;\r
-                               }\r
-                               res = &comp->resolutions[pi->resno];\r
-                               /*for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1]); pi->precno++) {*/\r
-                               for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); pi->precno++) {\r
-                                       index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;\r
-                                       if (!pi->include[index]) {\r
-                                               pi->include[index] = 1;\r
-                                               return true;\r
-                                       }\r
-LABEL_SKIP:;\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-       \r
-       return false;\r
-}\r
-\r
-static bool pi_next_rpcl(opj_pi_iterator_t * pi) {\r
-       opj_pi_comp_t *comp = NULL;\r
-       opj_pi_resolution_t *res = NULL;\r
-       long index = 0;\r
-\r
-       if (!pi->first) {\r
-               goto LABEL_SKIP;\r
-       } else {\r
-               int compno, resno;\r
-               pi->first = 0;\r
-               pi->dx = 0;\r
-               pi->dy = 0;\r
-               for (compno = 0; compno < pi->numcomps; compno++) {\r
-                       comp = &pi->comps[compno];\r
-                       for (resno = 0; resno < comp->numresolution[0]; resno++) {\r
-                               int dx, dy,dz;\r
-                               res = &comp->resolutions[resno];\r
-                               dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno));\r
-                               dy = comp->dy * (1 << (res->pdy + comp->numresolution[1] - 1 - resno));\r
-                               dz = comp->dz * (1 << (res->pdz + comp->numresolution[2] - 1 - resno));\r
-                               pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);\r
-                               pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);\r
-                               pi->dz = !pi->dz ? dz : int_min(pi->dz, dz);\r
-                       }\r
-               }\r
-       }\r
-\r
-       for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {\r
-               for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) {\r
-                       for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) {\r
-                               for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) {\r
-                                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {\r
-                                               int levelnox, levelnoy, levelnoz;\r
-                                               int trx0, try0, trz0;\r
-                                               int trx1, try1, trz1;\r
-                                               int rpx, rpy, rpz;\r
-                                               int prci, prcj, prck;\r
-                                               comp = &pi->comps[pi->compno];\r
-                                               if (pi->resno >= comp->numresolution[0]) {\r
-                                                       continue;\r
-                                               }\r
-                                               res = &comp->resolutions[pi->resno];\r
-                                               levelnox = comp->numresolution[0] - 1 - pi->resno;\r
-                                               levelnoy = comp->numresolution[1] - 1 - pi->resno;\r
-                                               levelnoz = comp->numresolution[2] - 1 - pi->resno;\r
-                                               trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox);\r
-                                               try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy);\r
-                                               trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz);\r
-                                               trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox);\r
-                                               try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy);\r
-                                               trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz);\r
-                                               rpx = res->pdx + levelnox;\r
-                                               rpy = res->pdy + levelnoy;\r
-                                               rpz = res->pdz + levelnoz;\r
-                                               if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) {\r
-                                                       continue;\r
-                                               }\r
-                                               if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) {\r
-                                                       continue;\r
-                                               }\r
-                                               if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) {\r
-                                                       continue;\r
-                                               }\r
-                                               if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue;\r
-                                               \r
-                                               if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue;\r
-                                               \r
-                                               prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx) \r
-                                                       - int_floordivpow2(trx0, res->pdx);\r
-                                               prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy) \r
-                                                       - int_floordivpow2(try0, res->pdy);\r
-                                               prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz) \r
-                                                       - int_floordivpow2(trz0, res->pdz);\r
-                                               pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1];\r
-                                               for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {\r
-                                                       index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;\r
-                                                       if (!pi->include[index]) {\r
-                                                               pi->include[index] = 1;\r
-                                                               return true;\r
-                                                       }\r
-       LABEL_SKIP:;\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-       \r
-       return false;\r
-}\r
-\r
-static bool pi_next_pcrl(opj_pi_iterator_t * pi) {\r
-       opj_pi_comp_t *comp = NULL;\r
-       opj_pi_resolution_t *res = NULL;\r
-       long index = 0;\r
-\r
-       if (!pi->first) {\r
-               comp = &pi->comps[pi->compno];\r
-               goto LABEL_SKIP;\r
-       } else {\r
-               int compno, resno;\r
-               pi->first = 0;\r
-               pi->dx = 0;\r
-               pi->dy = 0;\r
-               pi->dz = 0;\r
-               for (compno = 0; compno < pi->numcomps; compno++) {\r
-                       comp = &pi->comps[compno];\r
-                       for (resno = 0; resno < comp->numresolution[0]; resno++) {\r
-                               int dx, dy, dz;\r
-                               res = &comp->resolutions[resno];\r
-                               dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno));\r
-                               dy = comp->dy * (1 << (res->pdy + comp->numresolution[1] - 1 - resno));\r
-                               dz = comp->dz * (1 << (res->pdy + comp->numresolution[2] - 1 - resno));\r
-                               pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);\r
-                               pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);\r
-                               pi->dz = !pi->dz ? dz : int_min(pi->dz, dz);\r
-                       }\r
-               }\r
-       }\r
-\r
-for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) {\r
-       for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) {\r
-               for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) {\r
-                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {\r
-                               comp = &pi->comps[pi->compno];\r
-                               for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolution[0]); pi->resno++) {\r
-                                               int levelnox, levelnoy, levelnoz;\r
-                                               int trx0, try0, trz0;\r
-                                               int trx1, try1, trz1;\r
-                                               int rpx, rpy, rpz;\r
-                                               int prci, prcj, prck;\r
-                                               comp = &pi->comps[pi->compno];\r
-                                               if (pi->resno >= comp->numresolution[0]) {\r
-                                                       continue;\r
-                                               }\r
-                                               res = &comp->resolutions[pi->resno];\r
-                                               levelnox = comp->numresolution[0] - 1 - pi->resno;\r
-                                               levelnoy = comp->numresolution[1] - 1 - pi->resno;\r
-                                               levelnoz = comp->numresolution[2] - 1 - pi->resno;\r
-                                               trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox);\r
-                                               try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy);\r
-                                               trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz);\r
-                                               trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox);\r
-                                               try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy);\r
-                                               trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz);\r
-                                               rpx = res->pdx + levelnox;\r
-                                               rpy = res->pdy + levelnoy;\r
-                                               rpz = res->pdz + levelnoz;\r
-                                               if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) {\r
-                                                       continue;\r
-                                               }\r
-                                               if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) {\r
-                                                       continue;\r
-                                               }\r
-                                               if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) {\r
-                                                       continue;\r
-                                               }\r
-                                               if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue;\r
-                                               \r
-                                               if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue;\r
-                                               \r
-                                               prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx) \r
-                                                       - int_floordivpow2(trx0, res->pdx);\r
-                                               prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy) \r
-                                                       - int_floordivpow2(try0, res->pdy);\r
-                                               prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz) \r
-                                                       - int_floordivpow2(trz0, res->pdz);\r
-                                               pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1];\r
-                                               for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {\r
-                                                       index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;\r
-                                                       if (!pi->include[index]) {\r
-                                                               pi->include[index] = 1;\r
-                                                               return true;\r
-                                                       }       \r
-LABEL_SKIP:;\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-}\r
-       \r
-       return false;\r
-}\r
-\r
-static bool pi_next_cprl(opj_pi_iterator_t * pi) {\r
-       opj_pi_comp_t *comp = NULL;\r
-       opj_pi_resolution_t *res = NULL;\r
-       long index = 0;\r
-\r
-       if (!pi->first) {\r
-               comp = &pi->comps[pi->compno];\r
-               goto LABEL_SKIP;\r
-       } else {\r
-               pi->first = 0;\r
-       }\r
-\r
-       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {\r
-               int resno;\r
-               comp = &pi->comps[pi->compno];\r
-               pi->dx = 0;\r
-               pi->dy = 0;\r
-               for (resno = 0; resno < comp->numresolution[0]; resno++) {\r
-                       int dx, dy;\r
-                       res = &comp->resolutions[resno];\r
-                       dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno));\r
-                       dy = comp->dy * (1 << (res->pdy + comp->numresolution[0] - 1 - resno));\r
-                       pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);\r
-                       pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);\r
-               }\r
-       for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) {\r
-               for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) {\r
-                       for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) {\r
-                               for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolution[0]); pi->resno++) {\r
-                                               int levelnox, levelnoy, levelnoz;\r
-                                               int trx0, try0, trz0;\r
-                                               int trx1, try1, trz1;\r
-                                               int rpx, rpy, rpz;\r
-                                               int prci, prcj, prck;\r
-                                               comp = &pi->comps[pi->compno];\r
-                                               if (pi->resno >= comp->numresolution[0]) {\r
-                                                       continue;\r
-                                               }\r
-                                               res = &comp->resolutions[pi->resno];\r
-                                               levelnox = comp->numresolution[0] - 1 - pi->resno;\r
-                                               levelnoy = comp->numresolution[1] - 1 - pi->resno;\r
-                                               levelnoz = comp->numresolution[2] - 1 - pi->resno;\r
-                                               trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox);\r
-                                               try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy);\r
-                                               trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz);\r
-                                               trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox);\r
-                                               try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy);\r
-                                               trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz);\r
-                                               rpx = res->pdx + levelnox;\r
-                                               rpy = res->pdy + levelnoy;\r
-                                               rpz = res->pdz + levelnoz;\r
-                                               if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) {\r
-                                                       continue;\r
-                                               }\r
-                                               if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) {\r
-                                                       continue;\r
-                                               }\r
-                                               if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) {\r
-                                                       continue;\r
-                                               }\r
-                                               if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue;\r
-                                               \r
-                                               if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue;\r
-                                               \r
-                                               prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx) \r
-                                                       - int_floordivpow2(trx0, res->pdx);\r
-                                               prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy) \r
-                                                       - int_floordivpow2(try0, res->pdy);\r
-                                               prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz) \r
-                                                       - int_floordivpow2(trz0, res->pdz);\r
-                                               pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1];\r
-                                               for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {\r
-                                                       index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;\r
-                                                       if (!pi->include[index]) {\r
-                                                               pi->include[index] = 1;\r
-                                                               return true;\r
-                                                       }\r
-       LABEL_SKIP:;\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-       \r
-       return false;\r
-}\r
-\r
-/* \r
-==========================================================\r
-   Packet iterator interface\r
-==========================================================\r
-*/\r
-\r
-opj_pi_iterator_t *pi_create(opj_volume_t *volume, opj_cp_t *cp, int tileno) {\r
-       int p, q, r;\r
-       int compno, resno, pino;\r
-       opj_pi_iterator_t *pi = NULL;\r
-       opj_tcp_t *tcp = NULL;\r
-       opj_tccp_t *tccp = NULL;\r
-       size_t array_size;\r
-       \r
-       tcp = &cp->tcps[tileno];\r
-\r
-       array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);\r
-       pi = (opj_pi_iterator_t *) opj_malloc(array_size);\r
-       if(!pi) {\r
-               fprintf(stdout,"[ERROR] Malloc of opj_pi_iterator failed \n");\r
-               return NULL;\r
-       }\r
-       \r
-       for (pino = 0; pino < tcp->numpocs + 1; pino++) {       /* change */\r
-               int maxres = 0;\r
-               int maxprec = 0;\r
-               p = tileno % cp->tw;\r
-               q = tileno / cp->tw;\r
-               r = tileno / (cp->tw * cp->th);\r
-\r
-               pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);\r
-               pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);\r
-               pi[pino].tz0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);\r
-               pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);\r
-               pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);\r
-               pi[pino].tz1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);\r
-               pi[pino].numcomps = volume->numcomps;\r
-\r
-               array_size = volume->numcomps * sizeof(opj_pi_comp_t);\r
-               pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);\r
-               if(!pi[pino].comps) {\r
-                       fprintf(stdout,"[ERROR] Malloc of opj_pi_comp failed \n");\r
-                       pi_destroy(pi, cp, tileno);\r
-                       return NULL;\r
-               }\r
-               memset(pi[pino].comps, 0, array_size);\r
-               \r
-               for (compno = 0; compno < pi->numcomps; compno++) {\r
-                       int tcx0, tcx1, tcy0, tcy1, tcz0, tcz1;\r
-                       int i;\r
-                       opj_pi_comp_t *comp = &pi[pino].comps[compno];\r
-                       tccp = &tcp->tccps[compno];\r
-                       \r
-                       comp->dx = volume->comps[compno].dx;\r
-                       comp->dy = volume->comps[compno].dy;\r
-                       comp->dz = volume->comps[compno].dz;\r
-                       for (i = 0; i < 3; i++) {\r
-                               comp->numresolution[i] = tccp->numresolution[i];\r
-                               if (comp->numresolution[i] > maxres) {\r
-                                       maxres = comp->numresolution[i];\r
-                               }\r
-                       }\r
-                       array_size = comp->numresolution[0] * sizeof(opj_pi_resolution_t);\r
-                       comp->resolutions =     (opj_pi_resolution_t *) opj_malloc(array_size);\r
-                       if(!comp->resolutions) {\r
-                               fprintf(stdout,"[ERROR] Malloc of opj_pi_resolution failed \n");\r
-                               pi_destroy(pi, cp, tileno);\r
-                               return NULL;\r
-                       }\r
-\r
-                       tcx0 = int_ceildiv(pi->tx0, comp->dx);\r
-                       tcy0 = int_ceildiv(pi->ty0, comp->dy);\r
-                       tcz0 = int_ceildiv(pi->tz0, comp->dz);\r
-                       tcx1 = int_ceildiv(pi->tx1, comp->dx);\r
-                       tcy1 = int_ceildiv(pi->ty1, comp->dy);\r
-                       tcz1 = int_ceildiv(pi->tz1, comp->dz);\r
-                       \r
-                       for (resno = 0; resno < comp->numresolution[0]; resno++) {\r
-                               int levelnox, levelnoy, levelnoz, diff;\r
-                               int rx0, ry0, rz0, rx1, ry1, rz1;\r
-                               int px0, py0, pz0, px1, py1, pz1;\r
-                               opj_pi_resolution_t *res = &comp->resolutions[resno];\r
-                               if (tccp->csty & J3D_CCP_CSTY_PRT) {\r
-                                       res->pdx = tccp->prctsiz[0][resno];\r
-                                       res->pdy = tccp->prctsiz[1][resno];\r
-                                       res->pdz = tccp->prctsiz[2][resno];\r
-                               } else {\r
-                                       res->pdx = 15;\r
-                                       res->pdy = 15;\r
-                                       res->pdz = 15;\r
-                               }\r
-                               levelnox = comp->numresolution[0] - 1 - resno;\r
-                               levelnoy = comp->numresolution[1] - 1 - resno;\r
-                levelnoz = comp->numresolution[2] - 1 - resno;\r
-                               if (levelnoz < 0) levelnoz = 0; \r
-                               diff = comp->numresolution[0] - comp->numresolution[2];\r
-\r
-                               rx0 = int_ceildivpow2(tcx0, levelnox);\r
-                               ry0 = int_ceildivpow2(tcy0, levelnoy);\r
-                               rz0 = int_ceildivpow2(tcz0, levelnoz);\r
-                               rx1 = int_ceildivpow2(tcx1, levelnox);\r
-                               ry1 = int_ceildivpow2(tcy1, levelnoy);\r
-                               rz1 = int_ceildivpow2(tcz1, levelnoz);\r
-                               px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;\r
-                               py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;\r
-                               pz0 = int_floordivpow2(rz0, res->pdz) << res->pdz;\r
-                               px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;\r
-                               py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;\r
-                               pz1 = int_ceildivpow2(rz1, res->pdz) << res->pdz;\r
-                               res->prctno[0] = (rx0==rx1)? 0 : ((px1 - px0) >> res->pdx);\r
-                               res->prctno[1] = (ry0==ry1)? 0 : ((py1 - py0) >> res->pdy);\r
-                               res->prctno[2] = (rz0==rz1)? 0 : ((pz1 - pz0) >> res->pdz);\r
-\r
-                               if (res->prctno[0]*res->prctno[1]*res->prctno[2] > maxprec) {\r
-                                       maxprec = res->prctno[0]*res->prctno[1]*res->prctno[2];\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               tccp = &tcp->tccps[0];\r
-               pi[pino].step_p = 1;\r
-               pi[pino].step_c = maxprec * pi[pino].step_p;\r
-               pi[pino].step_r = volume->numcomps * pi[pino].step_c;\r
-               pi[pino].step_l = maxres * pi[pino].step_r;\r
-               \r
-               if (pino == 0) {\r
-                       array_size = volume->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int);\r
-                       pi[pino].include = (short int *) opj_malloc(array_size);\r
-                       if(!pi[pino].include) {\r
-                               fprintf(stdout,"[ERROR] Malloc of pi[pino].include failed \n");\r
-                               pi_destroy(pi, cp, tileno);\r
-                               return NULL;\r
-                       }\r
-               }\r
-               else {\r
-                       pi[pino].include = pi[pino - 1].include;\r
-               }\r
-               \r
-               if (tcp->POC == 0) {\r
-                       pi[pino].first = 1;\r
-                       pi[pino].poc.resno0 = 0;\r
-                       pi[pino].poc.compno0 = 0;\r
-                       pi[pino].poc.layno1 = tcp->numlayers;\r
-                       pi[pino].poc.resno1 = maxres;\r
-                       pi[pino].poc.compno1 = volume->numcomps;\r
-                       pi[pino].poc.prg = tcp->prg;\r
-               } else {\r
-                       pi[pino].first = 1;\r
-                       pi[pino].poc.resno0 = tcp->pocs[pino].resno0;\r
-                       pi[pino].poc.compno0 = tcp->pocs[pino].compno0;\r
-                       pi[pino].poc.layno1 = tcp->pocs[pino].layno1;\r
-                       pi[pino].poc.resno1 = tcp->pocs[pino].resno1;\r
-                       pi[pino].poc.compno1 = tcp->pocs[pino].compno1;\r
-                       pi[pino].poc.prg = tcp->pocs[pino].prg;\r
-               }\r
-       }\r
-       \r
-       return pi;\r
-}\r
-\r
-void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {\r
-       int compno, pino;\r
-       opj_tcp_t *tcp = &cp->tcps[tileno];\r
-       if(pi) {\r
-               for (pino = 0; pino < tcp->numpocs + 1; pino++) {       \r
-                       if(pi[pino].comps) {\r
-                               for (compno = 0; compno < pi->numcomps; compno++) {\r
-                                       opj_pi_comp_t *comp = &pi[pino].comps[compno];\r
-                                       if(comp->resolutions) {\r
-                                               opj_free(comp->resolutions);\r
-                                       }\r
-                               }\r
-                               opj_free(pi[pino].comps);\r
-                       }\r
-               }\r
-               if(pi->include) {\r
-                       opj_free(pi->include);\r
-               }\r
-               opj_free(pi);\r
-       }\r
-}\r
-\r
-bool pi_next(opj_pi_iterator_t * pi) {\r
-       switch (pi->poc.prg) {\r
-               case LRCP:\r
-                       return pi_next_lrcp(pi);\r
-               case RLCP:\r
-                       return pi_next_rlcp(pi);\r
-               case RPCL:\r
-                       return pi_next_rpcl(pi);\r
-               case PCRL:\r
-                       return pi_next_pcrl(pi);\r
-               case CPRL:\r
-                       return pi_next_cprl(pi);\r
-       }\r
-       \r
-       return false;\r
-}\r
-\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2006, M�nica D�ez, LPI-UVA, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup PI PI - Implementation of a packet iterator */
+/*@{*/
+
+/** @name Funciones locales */
+/*@{*/
+
+/**
+Get next packet in layer-resolution-component-precinct order.
+@param pi packet iterator to modify
+@return returns false if pi pointed to the last packet or else returns true 
+*/
+static bool pi_next_lrcp(opj_pi_iterator_t * pi);
+/**
+Get next packet in resolution-layer-component-precinct order.
+@param pi packet iterator to modify
+@return returns false if pi pointed to the last packet or else returns true 
+*/
+static bool pi_next_rlcp(opj_pi_iterator_t * pi);
+/**
+Get next packet in resolution-precinct-component-layer order.
+@param pi packet iterator to modify
+@return returns false if pi pointed to the last packet or else returns true 
+*/
+static bool pi_next_rpcl(opj_pi_iterator_t * pi);
+/**
+Get next packet in precinct-component-resolution-layer order.
+@param pi packet iterator to modify
+@return returns false if pi pointed to the last packet or else returns true 
+*/
+static bool pi_next_pcrl(opj_pi_iterator_t * pi);
+/**
+Get next packet in component-precinct-resolution-layer order.
+@param pi packet iterator to modify
+@return returns false if pi pointed to the last packet or else returns true 
+*/
+static bool pi_next_cprl(opj_pi_iterator_t * pi);
+
+/*@}*/
+
+/*@}*/
+
+/* 
+==========================================================
+   local functions
+==========================================================
+*/
+
+static bool pi_next_lrcp(opj_pi_iterator_t * pi) {
+       opj_pi_comp_t *comp = NULL;
+       opj_pi_resolution_t *res = NULL;
+       long index = 0;
+
+       if (!pi->first) {
+               comp = &pi->comps[pi->compno];
+               res = &comp->resolutions[pi->resno];
+               goto LABEL_SKIP;
+       } else {
+               pi->first = 0;
+       }
+
+       for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
+               for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
+                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+                               comp = &pi->comps[pi->compno];
+                               if (pi->resno >= comp->numresolution[0]) {
+                                       continue;
+                               }
+                               res = &comp->resolutions[pi->resno];
+                               /*for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1]); pi->precno++) {*/
+                               for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); pi->precno++) {
+                                       index = pi->layno * pi->step_l 
+                                               + pi->resno * pi->step_r 
+                                               + pi->compno * pi->step_c 
+                                               + pi->precno * pi->step_p;
+                                       if (!pi->include[index]) {
+                                               pi->include[index] = 1;
+                                               return true;
+                                       }
+LABEL_SKIP:;
+
+                               }
+                       }
+               }
+       }
+       
+       return false;
+}
+
+static bool pi_next_rlcp(opj_pi_iterator_t * pi) {
+       opj_pi_comp_t *comp = NULL;
+       opj_pi_resolution_t *res = NULL;
+       long index = 0;
+
+       if (!pi->first) {
+               comp = &pi->comps[pi->compno];
+               res = &comp->resolutions[pi->resno];
+               goto LABEL_SKIP;
+       } else {
+               pi->first = 0;
+       }
+
+       for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
+               for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
+                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+                               comp = &pi->comps[pi->compno];
+                               if (pi->resno >= comp->numresolution[0]) {
+                                       continue;
+                               }
+                               res = &comp->resolutions[pi->resno];
+                               /*for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1]); pi->precno++) {*/
+                               for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); pi->precno++) {
+                                       index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
+                                       if (!pi->include[index]) {
+                                               pi->include[index] = 1;
+                                               return true;
+                                       }
+LABEL_SKIP:;
+                               }
+                       }
+               }
+       }
+       
+       return false;
+}
+
+static bool pi_next_rpcl(opj_pi_iterator_t * pi) {
+       opj_pi_comp_t *comp = NULL;
+       opj_pi_resolution_t *res = NULL;
+       long index = 0;
+
+       if (!pi->first) {
+               goto LABEL_SKIP;
+       } else {
+               int compno, resno;
+               pi->first = 0;
+               pi->dx = 0;
+               pi->dy = 0;
+               for (compno = 0; compno < pi->numcomps; compno++) {
+                       comp = &pi->comps[compno];
+                       for (resno = 0; resno < comp->numresolution[0]; resno++) {
+                               int dx, dy,dz;
+                               res = &comp->resolutions[resno];
+                               dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno));
+                               dy = comp->dy * (1 << (res->pdy + comp->numresolution[1] - 1 - resno));
+                               dz = comp->dz * (1 << (res->pdz + comp->numresolution[2] - 1 - resno));
+                               pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
+                               pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
+                               pi->dz = !pi->dz ? dz : int_min(pi->dz, dz);
+                       }
+               }
+       }
+
+       for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
+               for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) {
+                       for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
+                               for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
+                                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+                                               int levelnox, levelnoy, levelnoz;
+                                               int trx0, try0, trz0;
+                                               int trx1, try1, trz1;
+                                               int rpx, rpy, rpz;
+                                               int prci, prcj, prck;
+                                               comp = &pi->comps[pi->compno];
+                                               if (pi->resno >= comp->numresolution[0]) {
+                                                       continue;
+                                               }
+                                               res = &comp->resolutions[pi->resno];
+                                               levelnox = comp->numresolution[0] - 1 - pi->resno;
+                                               levelnoy = comp->numresolution[1] - 1 - pi->resno;
+                                               levelnoz = comp->numresolution[2] - 1 - pi->resno;
+                                               trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox);
+                                               try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy);
+                                               trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz);
+                                               trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox);
+                                               try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy);
+                                               trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz);
+                                               rpx = res->pdx + levelnox;
+                                               rpy = res->pdy + levelnoy;
+                                               rpz = res->pdz + levelnoz;
+                                               if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) {
+                                                       continue;
+                                               }
+                                               if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) {
+                                                       continue;
+                                               }
+                                               if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) {
+                                                       continue;
+                                               }
+                                               if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue;
+                                               
+                                               if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue;
+                                               
+                                               prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx) 
+                                                       - int_floordivpow2(trx0, res->pdx);
+                                               prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy) 
+                                                       - int_floordivpow2(try0, res->pdy);
+                                               prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz) 
+                                                       - int_floordivpow2(trz0, res->pdz);
+                                               pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1];
+                                               for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
+                                                       index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
+                                                       if (!pi->include[index]) {
+                                                               pi->include[index] = 1;
+                                                               return true;
+                                                       }
+       LABEL_SKIP:;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+       
+       return false;
+}
+
+static bool pi_next_pcrl(opj_pi_iterator_t * pi) {
+       opj_pi_comp_t *comp = NULL;
+       opj_pi_resolution_t *res = NULL;
+       long index = 0;
+
+       if (!pi->first) {
+               comp = &pi->comps[pi->compno];
+               goto LABEL_SKIP;
+       } else {
+               int compno, resno;
+               pi->first = 0;
+               pi->dx = 0;
+               pi->dy = 0;
+               pi->dz = 0;
+               for (compno = 0; compno < pi->numcomps; compno++) {
+                       comp = &pi->comps[compno];
+                       for (resno = 0; resno < comp->numresolution[0]; resno++) {
+                               int dx, dy, dz;
+                               res = &comp->resolutions[resno];
+                               dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno));
+                               dy = comp->dy * (1 << (res->pdy + comp->numresolution[1] - 1 - resno));
+                               dz = comp->dz * (1 << (res->pdy + comp->numresolution[2] - 1 - resno));
+                               pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
+                               pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
+                               pi->dz = !pi->dz ? dz : int_min(pi->dz, dz);
+                       }
+               }
+       }
+
+for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) {
+       for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
+               for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
+                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+                               comp = &pi->comps[pi->compno];
+                               for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolution[0]); pi->resno++) {
+                                               int levelnox, levelnoy, levelnoz;
+                                               int trx0, try0, trz0;
+                                               int trx1, try1, trz1;
+                                               int rpx, rpy, rpz;
+                                               int prci, prcj, prck;
+                                               comp = &pi->comps[pi->compno];
+                                               if (pi->resno >= comp->numresolution[0]) {
+                                                       continue;
+                                               }
+                                               res = &comp->resolutions[pi->resno];
+                                               levelnox = comp->numresolution[0] - 1 - pi->resno;
+                                               levelnoy = comp->numresolution[1] - 1 - pi->resno;
+                                               levelnoz = comp->numresolution[2] - 1 - pi->resno;
+                                               trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox);
+                                               try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy);
+                                               trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz);
+                                               trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox);
+                                               try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy);
+                                               trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz);
+                                               rpx = res->pdx + levelnox;
+                                               rpy = res->pdy + levelnoy;
+                                               rpz = res->pdz + levelnoz;
+                                               if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) {
+                                                       continue;
+                                               }
+                                               if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) {
+                                                       continue;
+                                               }
+                                               if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) {
+                                                       continue;
+                                               }
+                                               if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue;
+                                               
+                                               if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue;
+                                               
+                                               prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx) 
+                                                       - int_floordivpow2(trx0, res->pdx);
+                                               prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy) 
+                                                       - int_floordivpow2(try0, res->pdy);
+                                               prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz) 
+                                                       - int_floordivpow2(trz0, res->pdz);
+                                               pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1];
+                                               for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
+                                                       index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
+                                                       if (!pi->include[index]) {
+                                                               pi->include[index] = 1;
+                                                               return true;
+                                                       }       
+LABEL_SKIP:;
+                                       }
+                               }
+                       }
+               }
+       }
+}
+       
+       return false;
+}
+
+static bool pi_next_cprl(opj_pi_iterator_t * pi) {
+       opj_pi_comp_t *comp = NULL;
+       opj_pi_resolution_t *res = NULL;
+       long index = 0;
+
+       if (!pi->first) {
+               comp = &pi->comps[pi->compno];
+               goto LABEL_SKIP;
+       } else {
+               pi->first = 0;
+       }
+
+       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+               int resno;
+               comp = &pi->comps[pi->compno];
+               pi->dx = 0;
+               pi->dy = 0;
+               for (resno = 0; resno < comp->numresolution[0]; resno++) {
+                       int dx, dy;
+                       res = &comp->resolutions[resno];
+                       dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno));
+                       dy = comp->dy * (1 << (res->pdy + comp->numresolution[0] - 1 - resno));
+                       pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
+                       pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
+               }
+       for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) {
+               for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
+                       for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
+                               for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolution[0]); pi->resno++) {
+                                               int levelnox, levelnoy, levelnoz;
+                                               int trx0, try0, trz0;
+                                               int trx1, try1, trz1;
+                                               int rpx, rpy, rpz;
+                                               int prci, prcj, prck;
+                                               comp = &pi->comps[pi->compno];
+                                               if (pi->resno >= comp->numresolution[0]) {
+                                                       continue;
+                                               }
+                                               res = &comp->resolutions[pi->resno];
+                                               levelnox = comp->numresolution[0] - 1 - pi->resno;
+                                               levelnoy = comp->numresolution[1] - 1 - pi->resno;
+                                               levelnoz = comp->numresolution[2] - 1 - pi->resno;
+                                               trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox);
+                                               try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy);
+                                               trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz);
+                                               trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox);
+                                               try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy);
+                                               trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz);
+                                               rpx = res->pdx + levelnox;
+                                               rpy = res->pdy + levelnoy;
+                                               rpz = res->pdz + levelnoz;
+                                               if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) {
+                                                       continue;
+                                               }
+                                               if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) {
+                                                       continue;
+                                               }
+                                               if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) {
+                                                       continue;
+                                               }
+                                               if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue;
+                                               
+                                               if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue;
+                                               
+                                               prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx) 
+                                                       - int_floordivpow2(trx0, res->pdx);
+                                               prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy) 
+                                                       - int_floordivpow2(try0, res->pdy);
+                                               prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz) 
+                                                       - int_floordivpow2(trz0, res->pdz);
+                                               pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1];
+                                               for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
+                                                       index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
+                                                       if (!pi->include[index]) {
+                                                               pi->include[index] = 1;
+                                                               return true;
+                                                       }
+       LABEL_SKIP:;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+       
+       return false;
+}
+
+/* 
+==========================================================
+   Packet iterator interface
+==========================================================
+*/
+
+opj_pi_iterator_t *pi_create(opj_volume_t *volume, opj_cp_t *cp, int tileno) {
+       int p, q, r;
+       int compno, resno, pino;
+       opj_pi_iterator_t *pi = NULL;
+       opj_tcp_t *tcp = NULL;
+       opj_tccp_t *tccp = NULL;
+       size_t array_size;
+       
+       tcp = &cp->tcps[tileno];
+
+       array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);
+       pi = (opj_pi_iterator_t *) opj_malloc(array_size);
+       if(!pi) {
+               fprintf(stdout,"[ERROR] Malloc of opj_pi_iterator failed \n");
+               return NULL;
+       }
+       
+       for (pino = 0; pino < tcp->numpocs + 1; pino++) {       /* change */
+               int maxres = 0;
+               int maxprec = 0;
+               p = tileno % cp->tw;
+               q = tileno / cp->tw;
+               r = tileno / (cp->tw * cp->th);
+
+               pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
+               pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
+               pi[pino].tz0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
+               pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
+               pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
+               pi[pino].tz1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
+               pi[pino].numcomps = volume->numcomps;
+
+               array_size = volume->numcomps * sizeof(opj_pi_comp_t);
+               pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);
+               if(!pi[pino].comps) {
+                       fprintf(stdout,"[ERROR] Malloc of opj_pi_comp failed \n");
+                       pi_destroy(pi, cp, tileno);
+                       return NULL;
+               }
+               memset(pi[pino].comps, 0, array_size);
+               
+               for (compno = 0; compno < pi->numcomps; compno++) {
+                       int tcx0, tcx1, tcy0, tcy1, tcz0, tcz1;
+                       int i;
+                       opj_pi_comp_t *comp = &pi[pino].comps[compno];
+                       tccp = &tcp->tccps[compno];
+                       
+                       comp->dx = volume->comps[compno].dx;
+                       comp->dy = volume->comps[compno].dy;
+                       comp->dz = volume->comps[compno].dz;
+                       for (i = 0; i < 3; i++) {
+                               comp->numresolution[i] = tccp->numresolution[i];
+                               if (comp->numresolution[i] > maxres) {
+                                       maxres = comp->numresolution[i];
+                               }
+                       }
+                       array_size = comp->numresolution[0] * sizeof(opj_pi_resolution_t);
+                       comp->resolutions =     (opj_pi_resolution_t *) opj_malloc(array_size);
+                       if(!comp->resolutions) {
+                               fprintf(stdout,"[ERROR] Malloc of opj_pi_resolution failed \n");
+                               pi_destroy(pi, cp, tileno);
+                               return NULL;
+                       }
+
+                       tcx0 = int_ceildiv(pi->tx0, comp->dx);
+                       tcy0 = int_ceildiv(pi->ty0, comp->dy);
+                       tcz0 = int_ceildiv(pi->tz0, comp->dz);
+                       tcx1 = int_ceildiv(pi->tx1, comp->dx);
+                       tcy1 = int_ceildiv(pi->ty1, comp->dy);
+                       tcz1 = int_ceildiv(pi->tz1, comp->dz);
+                       
+                       for (resno = 0; resno < comp->numresolution[0]; resno++) {
+                               int levelnox, levelnoy, levelnoz, diff;
+                               int rx0, ry0, rz0, rx1, ry1, rz1;
+                               int px0, py0, pz0, px1, py1, pz1;
+                               opj_pi_resolution_t *res = &comp->resolutions[resno];
+                               if (tccp->csty & J3D_CCP_CSTY_PRT) {
+                                       res->pdx = tccp->prctsiz[0][resno];
+                                       res->pdy = tccp->prctsiz[1][resno];
+                                       res->pdz = tccp->prctsiz[2][resno];
+                               } else {
+                                       res->pdx = 15;
+                                       res->pdy = 15;
+                                       res->pdz = 15;
+                               }
+                               levelnox = comp->numresolution[0] - 1 - resno;
+                               levelnoy = comp->numresolution[1] - 1 - resno;
+                levelnoz = comp->numresolution[2] - 1 - resno;
+                               if (levelnoz < 0) levelnoz = 0; 
+                               diff = comp->numresolution[0] - comp->numresolution[2];
+
+                               rx0 = int_ceildivpow2(tcx0, levelnox);
+                               ry0 = int_ceildivpow2(tcy0, levelnoy);
+                               rz0 = int_ceildivpow2(tcz0, levelnoz);
+                               rx1 = int_ceildivpow2(tcx1, levelnox);
+                               ry1 = int_ceildivpow2(tcy1, levelnoy);
+                               rz1 = int_ceildivpow2(tcz1, levelnoz);
+                               px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
+                               py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
+                               pz0 = int_floordivpow2(rz0, res->pdz) << res->pdz;
+                               px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
+                               py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
+                               pz1 = int_ceildivpow2(rz1, res->pdz) << res->pdz;
+                               res->prctno[0] = (rx0==rx1)? 0 : ((px1 - px0) >> res->pdx);
+                               res->prctno[1] = (ry0==ry1)? 0 : ((py1 - py0) >> res->pdy);
+                               res->prctno[2] = (rz0==rz1)? 0 : ((pz1 - pz0) >> res->pdz);
+
+                               if (res->prctno[0]*res->prctno[1]*res->prctno[2] > maxprec) {
+                                       maxprec = res->prctno[0]*res->prctno[1]*res->prctno[2];
+                               }
+                       }
+               }
+               
+               tccp = &tcp->tccps[0];
+               pi[pino].step_p = 1;
+               pi[pino].step_c = maxprec * pi[pino].step_p;
+               pi[pino].step_r = volume->numcomps * pi[pino].step_c;
+               pi[pino].step_l = maxres * pi[pino].step_r;
+               
+               if (pino == 0) {
+                       array_size = volume->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int);
+                       pi[pino].include = (short int *) opj_malloc(array_size);
+                       if(!pi[pino].include) {
+                               fprintf(stdout,"[ERROR] Malloc of pi[pino].include failed \n");
+                               pi_destroy(pi, cp, tileno);
+                               return NULL;
+                       }
+               }
+               else {
+                       pi[pino].include = pi[pino - 1].include;
+               }
+               
+               if (tcp->POC == 0) {
+                       pi[pino].first = 1;
+                       pi[pino].poc.resno0 = 0;
+                       pi[pino].poc.compno0 = 0;
+                       pi[pino].poc.layno1 = tcp->numlayers;
+                       pi[pino].poc.resno1 = maxres;
+                       pi[pino].poc.compno1 = volume->numcomps;
+                       pi[pino].poc.prg = tcp->prg;
+               } else {
+                       pi[pino].first = 1;
+                       pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
+                       pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
+                       pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
+                       pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
+                       pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
+                       pi[pino].poc.prg = tcp->pocs[pino].prg;
+               }
+       }
+       
+       return pi;
+}
+
+void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
+       int compno, pino;
+       opj_tcp_t *tcp = &cp->tcps[tileno];
+       if(pi) {
+               for (pino = 0; pino < tcp->numpocs + 1; pino++) {       
+                       if(pi[pino].comps) {
+                               for (compno = 0; compno < pi->numcomps; compno++) {
+                                       opj_pi_comp_t *comp = &pi[pino].comps[compno];
+                                       if(comp->resolutions) {
+                                               opj_free(comp->resolutions);
+                                       }
+                               }
+                               opj_free(pi[pino].comps);
+                       }
+               }
+               if(pi->include) {
+                       opj_free(pi->include);
+               }
+               opj_free(pi);
+       }
+}
+
+bool pi_next(opj_pi_iterator_t * pi) {
+       switch (pi->poc.prg) {
+               case LRCP:
+                       return pi_next_lrcp(pi);
+               case RLCP:
+                       return pi_next_rlcp(pi);
+               case RPCL:
+                       return pi_next_rpcl(pi);
+               case PCRL:
+                       return pi_next_pcrl(pi);
+               case CPRL:
+                       return pi_next_cprl(pi);
+       }
+       
+       return false;
+}
+
index 1edcc5fce5c7c1f7dbab76756a61044bbfbb9fb8..4274f4e6cad6e3e4be3a2ceadd76191d45fbffa8 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __PI_H\r
-#define __PI_H\r
-/**\r
-@file pi.h\r
-@brief Implementation of a packet iterator (PI)\r
-\r
-The functions in PI.C have for goal to realize a packet iterator that permits to get the next\r
-packet following the progression order and change of it. The functions in PI.C are used\r
-by some function in T2.C.\r
-*/\r
-\r
-/** @defgroup PI PI - Implementation of a packet iterator */\r
-/*@{*/\r
-\r
-/**\r
-Packet iterator : resolution level information \r
-*/\r
-typedef struct opj_pi_resolution {\r
-/** Size of precints in horizontal axis */\r
-       int pdx;\r
-/** Size of precints in vertical axis */\r
-       int pdy;\r
-/** Size of precints in axial axis */\r
-       int pdz;\r
-/** Number of precints in each axis */\r
-       int prctno[3];                          \r
-} opj_pi_resolution_t;\r
-\r
-/**\r
-Packet iterator : component information \r
-*/\r
-typedef struct opj_pi_comp {\r
-/** Size in horizontal axis */\r
-       int dx;\r
-/** Size in vertical axis */\r
-       int dy;\r
-/** Size in axial axis */\r
-       int dz;\r
-/** Number of resolution levels */\r
-       int numresolution[3];                   \r
-/** Packet iterator : resolution level information */\r
-       opj_pi_resolution_t *resolutions;\r
-} opj_pi_comp_t;\r
-\r
-/** \r
-Packet iterator \r
-*/\r
-typedef struct opj_pi_iterator {\r
-/** precise if the packet has been already used (usefull for progression order change) */\r
-       short int *include;             \r
-/** layer step used to localize the packet in the include vector */\r
-       int step_l;             \r
-/** resolution step used to localize the packet in the include vector */\r
-       int step_r;     \r
-/** component step used to localize the packet in the include vector */\r
-       int step_c;                             \r
-/** precinct step used to localize the packet in the include vector */\r
-       int step_p;                             \r
-/** component that identify the packet */\r
-       int compno;                             \r
-/** resolution that identify the packet */\r
-       int resno;                              \r
-/** precinct that identify the packet */\r
-       int precno;                             \r
-/** layer that identify the packet */\r
-       int layno;                              \r
-/** 0 if the first packet */\r
-       int first;                              \r
-/** progression order change information */\r
-       opj_poc_t poc;                  \r
-/**    Packet iterator : component information */\r
-opj_pi_comp_t *comps;\r
-       \r
-       int numcomps;\r
-       int tx0, ty0, tz0;\r
-       int tx1, ty1, tz1;\r
-       int x, y, z;\r
-       int dx, dy, dz;\r
-} opj_pi_iterator_t;\r
-\r
-/** @name Funciones generales */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Create a packet iterator\r
-@param volume Raw volume for which the packets will be listed\r
-@param cp Coding parameters\r
-@param tileno Number that identifies the tile for which to list the packets\r
-@return Returns a packet iterator that points to the first packet of the tile\r
-@see pi_destroy\r
-*/\r
-opj_pi_iterator_t *pi_create(opj_volume_t * volume, opj_cp_t * cp, int tileno);\r
-\r
-/**\r
-Destroy a packet iterator\r
-@param pi Previously created packet iterator\r
-@param cp Coding parameters\r
-@param tileno Number that identifies the tile for which the packets were listed\r
-@see pi_create\r
-*/\r
-void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno);\r
-\r
-/**\r
-Modify the packet iterator to point to the next packet\r
-@param pi Packet iterator to modify\r
-@return Returns false if pi pointed to the last packet or else returns true \r
-*/\r
-bool pi_next(opj_pi_iterator_t * pi);\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __PI_H */\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __PI_H
+#define __PI_H
+/**
+@file pi.h
+@brief Implementation of a packet iterator (PI)
+
+The functions in PI.C have for goal to realize a packet iterator that permits to get the next
+packet following the progression order and change of it. The functions in PI.C are used
+by some function in T2.C.
+*/
+
+/** @defgroup PI PI - Implementation of a packet iterator */
+/*@{*/
+
+/**
+Packet iterator : resolution level information 
+*/
+typedef struct opj_pi_resolution {
+/** Size of precints in horizontal axis */
+       int pdx;
+/** Size of precints in vertical axis */
+       int pdy;
+/** Size of precints in axial axis */
+       int pdz;
+/** Number of precints in each axis */
+       int prctno[3];                          
+} opj_pi_resolution_t;
+
+/**
+Packet iterator : component information 
+*/
+typedef struct opj_pi_comp {
+/** Size in horizontal axis */
+       int dx;
+/** Size in vertical axis */
+       int dy;
+/** Size in axial axis */
+       int dz;
+/** Number of resolution levels */
+       int numresolution[3];                   
+/** Packet iterator : resolution level information */
+       opj_pi_resolution_t *resolutions;
+} opj_pi_comp_t;
+
+/** 
+Packet iterator 
+*/
+typedef struct opj_pi_iterator {
+/** precise if the packet has been already used (usefull for progression order change) */
+       short int *include;             
+/** layer step used to localize the packet in the include vector */
+       int step_l;             
+/** resolution step used to localize the packet in the include vector */
+       int step_r;     
+/** component step used to localize the packet in the include vector */
+       int step_c;                             
+/** precinct step used to localize the packet in the include vector */
+       int step_p;                             
+/** component that identify the packet */
+       int compno;                             
+/** resolution that identify the packet */
+       int resno;                              
+/** precinct that identify the packet */
+       int precno;                             
+/** layer that identify the packet */
+       int layno;                              
+/** 0 if the first packet */
+       int first;                              
+/** progression order change information */
+       opj_poc_t poc;                  
+/**    Packet iterator : component information */
+opj_pi_comp_t *comps;
+       
+       int numcomps;
+       int tx0, ty0, tz0;
+       int tx1, ty1, tz1;
+       int x, y, z;
+       int dx, dy, dz;
+} opj_pi_iterator_t;
+
+/** @name Funciones generales */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a packet iterator
+@param volume Raw volume for which the packets will be listed
+@param cp Coding parameters
+@param tileno Number that identifies the tile for which to list the packets
+@return Returns a packet iterator that points to the first packet of the tile
+@see pi_destroy
+*/
+opj_pi_iterator_t *pi_create(opj_volume_t * volume, opj_cp_t * cp, int tileno);
+
+/**
+Destroy a packet iterator
+@param pi Previously created packet iterator
+@param cp Coding parameters
+@param tileno Number that identifies the tile for which the packets were listed
+@see pi_create
+*/
+void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno);
+
+/**
+Modify the packet iterator to point to the next packet
+@param pi Packet iterator to modify
+@return Returns false if pi pointed to the last packet or else returns true 
+*/
+bool pi_next(opj_pi_iterator_t * pi);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __PI_H */
index b6a58106d2b7eb9c0550e7f46c8464da25443ad2..1457a66b8924b342329e16a6cde96a9c8d1a478c 100644 (file)
@@ -1,86 +1,86 @@
-/*\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/* \r
-==========================================================\r
-   local functions\r
-==========================================================\r
-*/\r
-\r
-\r
-/* \r
-==========================================================\r
-   RAW encoding interface\r
-==========================================================\r
-*/\r
-\r
-opj_raw_t* raw_create() {\r
-       opj_raw_t *raw = (opj_raw_t*)opj_malloc(sizeof(opj_raw_t));\r
-       return raw;\r
-}\r
-\r
-void raw_destroy(opj_raw_t *raw) {\r
-       if(raw) {\r
-               opj_free(raw);\r
-       }\r
-}\r
-\r
-int raw_numbytes(opj_raw_t *raw) {\r
-       return raw->bp - raw->start;\r
-}\r
-\r
-void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len) {\r
-       raw->start = bp;\r
-       raw->lenmax = len;\r
-       raw->len = 0;\r
-       raw->c = 0;\r
-       raw->ct = 0;\r
-}\r
-\r
-int raw_decode(opj_raw_t *raw) {\r
-       int d;\r
-       if (raw->ct == 0) {\r
-               raw->ct = 8;\r
-               if (raw->len == raw->lenmax) {\r
-                       raw->c = 0xff;\r
-               } else {\r
-                       if (raw->c == 0xff) {\r
-                               raw->ct = 7;\r
-                       }\r
-                       raw->c = *(raw->start + raw->len);\r
-                       raw->len++;\r
-               }\r
-       }\r
-       raw->ct--;\r
-       d = (raw->c >> raw->ct) & 0x01;\r
-       \r
-       return d;\r
-}\r
-\r
+/*
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/* 
+==========================================================
+   local functions
+==========================================================
+*/
+
+
+/* 
+==========================================================
+   RAW encoding interface
+==========================================================
+*/
+
+opj_raw_t* raw_create() {
+       opj_raw_t *raw = (opj_raw_t*)opj_malloc(sizeof(opj_raw_t));
+       return raw;
+}
+
+void raw_destroy(opj_raw_t *raw) {
+       if(raw) {
+               opj_free(raw);
+       }
+}
+
+int raw_numbytes(opj_raw_t *raw) {
+       return raw->bp - raw->start;
+}
+
+void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len) {
+       raw->start = bp;
+       raw->lenmax = len;
+       raw->len = 0;
+       raw->c = 0;
+       raw->ct = 0;
+}
+
+int raw_decode(opj_raw_t *raw) {
+       int d;
+       if (raw->ct == 0) {
+               raw->ct = 8;
+               if (raw->len == raw->lenmax) {
+                       raw->c = 0xff;
+               } else {
+                       if (raw->c == 0xff) {
+                               raw->ct = 7;
+                       }
+                       raw->c = *(raw->start + raw->len);
+                       raw->len++;
+               }
+       }
+       raw->ct--;
+       d = (raw->c >> raw->ct) & 0x01;
+       
+       return d;
+}
+
index 55e889da3c22f95f2653a00dc08a7e26b1cb06ad..c452804db7cc5f2d624b375f67087dcc7c214302 100644 (file)
@@ -1,99 +1,99 @@
-/*\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __RAW_H\r
-#define __RAW_H\r
-/**\r
-@file raw.h\r
-@brief Implementation of operations for raw encoding (RAW)\r
-\r
-The functions in RAW.C have for goal to realize the operation of raw encoding linked\r
-with the corresponding mode switch.\r
-*/\r
-\r
-/** @defgroup RAW RAW - Implementation of operations for raw encoding */\r
-/*@{*/\r
-\r
-/**\r
-RAW encoding operations\r
-*/\r
-typedef struct opj_raw {\r
-/** Temporary buffer where bits are coded or decoded */\r
-       unsigned char c;                \r
-/** Number of bits already read or free to write */\r
-       unsigned int ct;                \r
-/** Maximum length to decode */\r
-       unsigned int lenmax;    \r
-/** Length decoded */\r
-       unsigned int len;               \r
-/** Pointer to the current position in the buffer */\r
-       unsigned char *bp;              \r
-/** Pointer to the start of the buffer */\r
-       unsigned char *start;   \r
-/** Pointer to the end of the buffer */\r
-       unsigned char *end;             \r
-} opj_raw_t;\r
-\r
-/** @name Funciones generales */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Create a new RAW handle \r
-@return Returns a new RAW handle if successful, returns NULL otherwise\r
-*/\r
-opj_raw_t* raw_create(void);\r
-/**\r
-Destroy a previously created RAW handle\r
-@param raw RAW handle to destroy\r
-*/\r
-void raw_destroy(opj_raw_t *raw);\r
-/**\r
-Return the number of bytes written/read since initialisation\r
-@param raw RAW handle to destroy\r
-@return Returns the number of bytes already encoded\r
-*/\r
-int raw_numbytes(opj_raw_t *raw);\r
-/**\r
-Initialize the decoder\r
-@param raw RAW handle\r
-@param bp Pointer to the start of the buffer from which the bytes will be read\r
-@param len Length of the input buffer\r
-*/\r
-void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len);\r
-/**\r
-Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN\r
-@param raw RAW handle\r
-@return Returns the decoded symbol (0 or 1)\r
-*/\r
-int raw_decode(opj_raw_t *raw);\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __RAW_H */\r
+/*
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __RAW_H
+#define __RAW_H
+/**
+@file raw.h
+@brief Implementation of operations for raw encoding (RAW)
+
+The functions in RAW.C have for goal to realize the operation of raw encoding linked
+with the corresponding mode switch.
+*/
+
+/** @defgroup RAW RAW - Implementation of operations for raw encoding */
+/*@{*/
+
+/**
+RAW encoding operations
+*/
+typedef struct opj_raw {
+/** Temporary buffer where bits are coded or decoded */
+       unsigned char c;                
+/** Number of bits already read or free to write */
+       unsigned int ct;                
+/** Maximum length to decode */
+       unsigned int lenmax;    
+/** Length decoded */
+       unsigned int len;               
+/** Pointer to the current position in the buffer */
+       unsigned char *bp;              
+/** Pointer to the start of the buffer */
+       unsigned char *start;   
+/** Pointer to the end of the buffer */
+       unsigned char *end;             
+} opj_raw_t;
+
+/** @name Funciones generales */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a new RAW handle 
+@return Returns a new RAW handle if successful, returns NULL otherwise
+*/
+opj_raw_t* raw_create(void);
+/**
+Destroy a previously created RAW handle
+@param raw RAW handle to destroy
+*/
+void raw_destroy(opj_raw_t *raw);
+/**
+Return the number of bytes written/read since initialisation
+@param raw RAW handle to destroy
+@return Returns the number of bytes already encoded
+*/
+int raw_numbytes(opj_raw_t *raw);
+/**
+Initialize the decoder
+@param raw RAW handle
+@param bp Pointer to the start of the buffer from which the bytes will be read
+@param len Length of the input buffer
+*/
+void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len);
+/**
+Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN
+@param raw RAW handle
+@return Returns the decoded symbol (0 or 1)
+*/
+int raw_decode(opj_raw_t *raw);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __RAW_H */
index 6a3898643af5e3f9bd120c253226171d4a6b1ab7..8ba8ee5e92188cb8959cb9122de43a8cd81e92ce 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/** @defgroup T1 T1 - Implementation of the tier-1 coding */\r
-/*@{*/\r
-\r
-/** @name Local static functions */\r
-/*@{*/\r
-\r
-static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient);\r
-static int t1_getctxno_sc(opj_t1_t *t1, int f);\r
-static int t1_getctxno_mag(opj_t1_t *t1, int f);\r
-static int t1_getspb(opj_t1_t *t1, int f);\r
-static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos);\r
-static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos);\r
-static void t1_updateflags(int *fp, int s);\r
-/**\r
-Encode significant pass\r
-*/\r
-static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc);\r
-/**\r
-Decode significant pass\r
-*/\r
-static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc);\r
-/**\r
-Encode significant pass\r
-*/\r
-static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty);\r
-/**\r
-Decode significant pass\r
-*/\r
-static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty);\r
-/**\r
-Encode refinement pass\r
-*/\r
-static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);\r
-/**\r
-Decode refinement pass\r
-*/\r
-static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc);\r
-/**\r
-Encode refinement pass\r
-*/\r
-static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty);\r
-/**\r
-Decode refinement pass\r
-*/\r
-static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, char type, int cblksty);\r
-/**\r
-Encode clean-up pass\r
-*/\r
-static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc);\r
-/**\r
-Decode clean-up pass\r
-*/\r
-static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc);\r
-/**\r
-Encode clean-up pass\r
-*/\r
-static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty);\r
-/**\r
-Decode clean-up pass\r
-*/\r
-static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int cblksty);\r
-/**\r
-Encode 1 code-block\r
-@param t1 T1 handle\r
-@param cblk Code-block coding parameters\r
-@param orient\r
-@param compno Component number\r
-@param level\r
-@param dwtid\r
-@param stepsize\r
-@param cblksty Code-block style\r
-@param numcomps\r
-@param tile\r
-*/\r
-static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile);\r
-/**\r
-Decode 1 code-block\r
-@param t1 T1 handle\r
-@param cblk Code-block coding parameters\r
-@param orient\r
-@param roishift Region of interest shifting value\r
-@param cblksty Code-block style\r
-*/\r
-static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);\r
-\r
-static int t1_init_ctxno_zc(int f, int orient);\r
-static int t1_init_ctxno_sc(int f);\r
-static int t1_init_ctxno_mag(int f);\r
-static int t1_init_spb(int f);\r
-/**\r
-Initialize the look-up tables of the Tier-1 coder/decoder\r
-@param t1 T1 handle\r
-*/\r
-static void t1_init_luts(opj_t1_t *t1);\r
-\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient) {\r
-       return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];\r
-}\r
-\r
-static int t1_getctxno_sc(opj_t1_t *t1, int f) {\r
-       return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];\r
-}\r
-\r
-static int t1_getctxno_mag(opj_t1_t *t1, int f) {\r
-       return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];\r
-}\r
-\r
-static int t1_getspb(opj_t1_t *t1, int f) {\r
-       return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];\r
-}\r
-\r
-static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos) {\r
-       if (bitpos > T1_NMSEDEC_FRACBITS) {\r
-               return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];\r
-       }\r
-       \r
-       return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];\r
-}\r
-\r
-static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos) {\r
-       if (bitpos > T1_NMSEDEC_FRACBITS) {\r
-               return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];\r
-       }\r
-\r
-    return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];\r
-}\r
-\r
-static void t1_updateflags(int *fp, int s) {\r
-       int *np = fp - (T1_MAXCBLKW + 2);\r
-       int *sp = fp + (T1_MAXCBLKW + 2);\r
-       np[-1] |= T1_SIG_SE;\r
-       np[1] |= T1_SIG_SW;\r
-       sp[-1] |= T1_SIG_NE;\r
-       sp[1] |= T1_SIG_NW;\r
-       *np |= T1_SIG_S;\r
-       *sp |= T1_SIG_N;\r
-       fp[-1] |= T1_SIG_E;\r
-       fp[1] |= T1_SIG_W;\r
-       if (s) {\r
-               *np |= T1_SGN_S;\r
-               *sp |= T1_SGN_N;\r
-               fp[-1] |= T1_SGN_E;\r
-               fp[1] |= T1_SGN_W;\r
-       }\r
-}\r
-\r
-static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) {\r
-       int v, flag;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);\r
-       if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {\r
-               v = int_abs(*dp) & one ? 1 : 0;\r
-               if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */\r
-                       mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));   /* ESSAI */\r
-                       mqc_bypass_enc(mqc, v);\r
-               } else {\r
-                       mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));\r
-                       mqc_encode(mqc, v);\r
-               }\r
-               if (v) {\r
-                       v = *dp < 0 ? 1 : 0;\r
-                       *nmsedec +=     t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);\r
-                       if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */\r
-                               mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));   /* ESSAI */\r
-                               mqc_bypass_enc(mqc, v);\r
-                       } else {\r
-                               mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));\r
-                               mqc_encode(mqc, v ^ t1_getspb(t1, flag));\r
-                       }\r
-                       t1_updateflags(fp, v);\r
-                       *fp |= T1_SIG;\r
-               }\r
-               *fp |= T1_VISIT;\r
-       }\r
-}\r
-\r
-static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {\r
-       int v, flag;\r
-       \r
-       opj_raw_t *raw = t1->raw;       /* RAW component */\r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);\r
-       if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {\r
-               if (type == T1_TYPE_RAW) {\r
-                       if (raw_decode(raw)) {\r
-                               v = raw_decode(raw);    /* ESSAI */\r
-                               *dp = v ? -oneplushalf : oneplushalf;\r
-                               t1_updateflags(fp, v);\r
-                               *fp |= T1_SIG;\r
-                       }\r
-               } else {\r
-                       mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));\r
-                       if (mqc_decode(mqc)) {\r
-                               mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));\r
-                               v = mqc_decode(mqc) ^ t1_getspb(t1, flag);\r
-                               *dp = v ? -oneplushalf : oneplushalf;\r
-                               t1_updateflags(fp, v);\r
-                               *fp |= T1_SIG;\r
-                       }\r
-               }\r
-               *fp |= T1_VISIT;\r
-       }\r
-}                              /* VSC and  BYPASS by Antonin */\r
-\r
-static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty) {\r
-       int i, j, k, m, one, vsc;\r
-       *nmsedec = 0;\r
-       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);\r
-       for (m = 0; m < l; m++) {\r
-       for (k = 0; k < h; k += 4) {\r
-               for (i = 0; i < w; i++) {\r
-                       for (j = k; j < k + 4 && j < h; j++) {\r
-                               vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                               t1_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, type, vsc);\r
-                       }\r
-               }\r
-       }\r
-       }\r
-}\r
-\r
-static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty) {\r
-       int i, j, k, m, one, half, oneplushalf, vsc;\r
-       one = 1 << bpno;\r
-       half = one >> 1;\r
-       oneplushalf = one | half;\r
-       for (m = 0; m < l; m++) {\r
-       for (k = 0; k < h; k += 4) {\r
-               for (i = 0; i < w; i++) {\r
-                       for (j = k; j < k + 4 && j < h; j++) {\r
-                               vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                               t1_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, type, vsc);\r
-                       }\r
-               }\r
-       }\r
-       }\r
-}                              /* VSC and  BYPASS by Antonin */\r
-\r
-static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {\r
-       int v, flag;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);\r
-       if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {\r
-               *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);\r
-               v = int_abs(*dp) & one ? 1 : 0;\r
-               if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */\r
-                       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));  /* ESSAI */\r
-                       mqc_bypass_enc(mqc, v);\r
-               } else {\r
-                       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));\r
-                       mqc_encode(mqc, v);\r
-               }\r
-               *fp |= T1_REFINE;\r
-       }\r
-}\r
-\r
-static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {\r
-       int v, t, flag;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       opj_raw_t *raw = t1->raw;       /* RAW component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);\r
-       if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {\r
-               if (type == T1_TYPE_RAW) {\r
-                       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));  /* ESSAI */\r
-                       v = raw_decode(raw);\r
-               } else {\r
-                       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));\r
-                       v = mqc_decode(mqc);\r
-               }\r
-               t = v ? poshalf : neghalf;\r
-               *dp += *dp < 0 ? -t : t;\r
-               *fp |= T1_REFINE;\r
-       }\r
-}                              /* VSC and  BYPASS by Antonin  */\r
-\r
-static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty) {\r
-       int i, j, k, m, one, vsc;\r
-       *nmsedec = 0;\r
-       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);\r
-       for (m = 0; m < l; m++) {\r
-               for (k = 0; k < h; k += 4) {\r
-               for (i = 0; i < w; i++) {\r
-                       for (j = k; j < k + 4 && j < h; j++) {\r
-                               vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                               t1_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type, vsc);\r
-                       }\r
-               }\r
-       }\r
-       }\r
-}\r
-\r
-static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, char type, int cblksty) {\r
-       int i, j, k, m, one, poshalf, neghalf;\r
-       int vsc;\r
-       one = 1 << bpno;\r
-       poshalf = one >> 1;\r
-       neghalf = bpno > 0 ? -poshalf : -1;\r
-       for (m = 0; m < l; m++) {\r
-               for (k = 0; k < h; k += 4) {\r
-               for (i = 0; i < w; i++) {\r
-                       for (j = k; j < k + 4 && j < h; j++) {\r
-                               vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                               t1_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type, vsc);\r
-                       }\r
-               }\r
-       }\r
-       }\r
-}                              /* VSC and  BYPASS by Antonin */\r
-\r
-static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) {\r
-       int v, flag;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);\r
-       if (partial) {\r
-               goto LABEL_PARTIAL;\r
-       }\r
-       if (!(*fp & (T1_SIG | T1_VISIT))) {\r
-               mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));\r
-               v = int_abs(*dp) & one ? 1 : 0;\r
-               mqc_encode(mqc, v);\r
-               if (v) {\r
-LABEL_PARTIAL:\r
-                       *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);\r
-                       mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));\r
-                       v = *dp < 0 ? 1 : 0;\r
-                       mqc_encode(mqc, v ^ t1_getspb(t1, flag));\r
-                       t1_updateflags(fp, v);\r
-                       *fp |= T1_SIG;\r
-               }\r
-       }\r
-       *fp &= ~T1_VISIT;\r
-}\r
-\r
-static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {\r
-       int v, flag;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);\r
-       if (partial) {\r
-               goto LABEL_PARTIAL;\r
-       }\r
-       if (!(flag & (T1_SIG | T1_VISIT))) {\r
-               mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));\r
-               if (mqc_decode(mqc)) {\r
-LABEL_PARTIAL:\r
-                       mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));\r
-                       v = mqc_decode(mqc) ^ t1_getspb(t1, flag);\r
-                       *dp = v ? -oneplushalf : oneplushalf;\r
-                       t1_updateflags(fp, v);\r
-                       *fp |= T1_SIG;\r
-               }\r
-       }\r
-       *fp &= ~T1_VISIT;\r
-}                              /* VSC and  BYPASS by Antonin */\r
-\r
-static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty) {\r
-       int i, j, k, m, one, agg, runlen, vsc;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       *nmsedec = 0;\r
-       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);\r
-       for (m = 0; m < l; m++) {\r
-               for (k = 0; k < h; k += 4) {\r
-                       for (i = 0; i < w; i++) {\r
-                               if (k + 3 < h) {\r
-                                       if (cblksty & J3D_CCP_CBLKSTY_VSC) {\r
-                                               agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                                       || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                                       || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                                       || (t1->flags[1 + m][1 + k + 3][1 + i] \r
-                                                       & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));\r
-                                       } else {\r
-                                               agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                                       || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                                       || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                                       || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));\r
-                                       }\r
-                               } else {\r
-                                       agg = 0;\r
-                               }\r
-                               if (agg) {\r
-                                       for (runlen = 0; runlen < 4; runlen++) {\r
-                                               if (int_abs(t1->data[m][k + runlen][i]) & one)\r
-                                                       break;\r
-                                       }\r
-                                       mqc_setcurctx(mqc, T1_CTXNO_AGG);\r
-                                       mqc_encode(mqc, runlen != 4);\r
-                                       if (runlen == 4) {\r
-                                               continue;\r
-                                       }\r
-                                       mqc_setcurctx(mqc, T1_CTXNO_UNI);\r
-                                       mqc_encode(mqc, runlen >> 1);\r
-                                       mqc_encode(mqc, runlen & 1);\r
-                               } else {\r
-                                       runlen = 0;\r
-                               }\r
-                               for (j = k + runlen; j < k + 4 && j < h; j++) {\r
-                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                                       t1_enc_clnpass_step(t1, &(t1->flags[1 + m][1 + j][1 + i]), &(t1->data[m][j][i]), orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);\r
-                               }\r
-                       }\r
-       }\r
-       }\r
-}\r
-\r
-static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int cblksty) {\r
-       int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc;\r
-       int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       one = 1 << bpno;\r
-       half = one >> 1;\r
-       oneplushalf = one | half;\r
-       for (m = 0; m < l; m++) {\r
-               for (k = 0; k < h; k += 4) {\r
-               for (i = 0; i < w; i++) {\r
-                       if (k + 3 < h) {\r
-                               if (cblksty & J3D_CCP_CBLKSTY_VSC) {\r
-                                       agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                               || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                               || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                               || (t1->flags[1 + m][1 + k + 3][1 + i] \r
-                                               & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));\r
-                               } else {\r
-                                       agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                               || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                               || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)\r
-                                               || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));\r
-                               }\r
-                       } else {\r
-                               agg = 0;\r
-                       }\r
-                       if (agg) {\r
-                               mqc_setcurctx(mqc, T1_CTXNO_AGG);\r
-                               if (!mqc_decode(mqc)) {\r
-                                       continue;\r
-                               }\r
-                               mqc_setcurctx(mqc, T1_CTXNO_UNI);\r
-                               runlen = mqc_decode(mqc);\r
-                               runlen = (runlen << 1) | mqc_decode(mqc);\r
-                       } else {\r
-                               runlen = 0;\r
-                       }\r
-                       for (j = k + runlen; j < k + 4 && j < h; j++) {\r
-                               vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                               t1_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);\r
-                       }\r
-               }\r
-       }\r
-       }\r
-       if (segsym) {\r
-               int v = 0;\r
-               mqc_setcurctx(mqc, T1_CTXNO_UNI);\r
-               v = mqc_decode(mqc);\r
-               v = (v << 1) | mqc_decode(mqc);\r
-               v = (v << 1) | mqc_decode(mqc);\r
-               v = (v << 1) | mqc_decode(mqc);\r
-               /*\r
-               if (v!=0xa) {\r
-                       opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);\r
-               } \r
-               */\r
-       }\r
-}                              /* VSC and  BYPASS by Antonin */\r
-\r
-\r
-static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {\r
-       int i, j, k;\r
-       int w, h, l;\r
-       int passno;\r
-       int bpno, passtype;\r
-       int max;\r
-       int nmsedec = 0;\r
-       double cumwmsedec = 0;\r
-       char type = T1_TYPE_MQ;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       w = cblk->x1 - cblk->x0;\r
-       h = cblk->y1 - cblk->y0;\r
-       l = cblk->z1 - cblk->z0;\r
-\r
-       max = 0;\r
-       for (k = 0; k < l; k++) {\r
-               for (j = 0; j < h; j++) {\r
-                       for (i = 0; i < w; i++) {\r
-                               max = int_max(max, int_abs(t1->data[k][j][i]));\r
-                       }\r
-               }\r
-       }\r
-       for (k = 0; k <= l; k++) {\r
-               for (j = 0; j <= h; j++) {\r
-                       for (i = 0; i <= w; i++) {\r
-                               t1->flags[k][j][i] = 0; \r
-                       }\r
-               }\r
-       }\r
-\r
-       cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;\r
-       \r
-       bpno = cblk->numbps - 1;\r
-       passtype = 2;\r
-       \r
-       mqc_reset_enc(mqc);\r
-       mqc_init_enc(mqc, cblk->data);\r
-       \r
-       for (passno = 0; bpno >= 0; passno++) {\r
-               opj_tcd_pass_t *pass = &cblk->passes[passno];\r
-               int correction = 3;\r
-               double tmpwmsedec;\r
-               type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;\r
-               /*fprintf(stdout,"passno %d passtype %d w %d h %d l %d bpno %d orient %d type %d cblksty %d\n",passno,passtype,w,h,l,bpno,orient,type,cblksty);*/\r
-\r
-               switch (passtype) {\r
-                       case 0:\r
-                               t1_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty);\r
-                               break;\r
-                       case 1:\r
-                               t1_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty);\r
-                               break;\r
-                       case 2:\r
-                               /*fprintf(stdout,"w %d h %d l %d bpno %d orient %d \n",w,h,l,bpno,orient);*/\r
-                               t1_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty);\r
-                               /* code switch SEGMARK (i.e. SEGSYM) */\r
-                               if (cblksty & J3D_CCP_CBLKSTY_SEGSYM)\r
-                                       mqc_segmark_enc(mqc);\r
-                               break;\r
-               }\r
-               \r
-               /* fixed_quality */\r
-               tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize, numcomps, dwtid);\r
-               cumwmsedec += tmpwmsedec;\r
-               tile->distotile += tmpwmsedec;\r
-               \r
-               /* Code switch "RESTART" (i.e. TERMALL) */\r
-               if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {\r
-                       if (type == T1_TYPE_RAW) {\r
-                               mqc_flush(mqc);\r
-                               correction = 1;\r
-                               /* correction = mqc_bypass_flush_enc(); */\r
-                       } else {                        /* correction = mqc_restart_enc(); */\r
-                               mqc_flush(mqc);\r
-                               correction = 1;\r
-                       }\r
-                       pass->term = 1;\r
-               } else {\r
-                       if (((bpno < (cblk->numbps - 4) && (passtype > 0)) \r
-                               || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) {\r
-                               if (type == T1_TYPE_RAW) {\r
-                                       mqc_flush(mqc);\r
-                                       correction = 1;\r
-                                       /* correction = mqc_bypass_flush_enc(); */\r
-                               } else {                /* correction = mqc_restart_enc(); */\r
-                                       mqc_flush(mqc);\r
-                                       correction = 1;\r
-                               }\r
-                               pass->term = 1;\r
-                       } else {\r
-                               pass->term = 0;\r
-                       }\r
-               }\r
-               \r
-               if (++passtype == 3) {\r
-                       passtype = 0;\r
-                       bpno--;\r
-               }\r
-               \r
-               if (pass->term && bpno > 0) {\r
-                       type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;\r
-                       if (type == T1_TYPE_RAW)\r
-                               mqc_bypass_init_enc(mqc);\r
-                       else\r
-                               mqc_restart_init_enc(mqc);\r
-               }\r
-               \r
-               pass->distortiondec = cumwmsedec;\r
-               pass->rate = mqc_numbytes(mqc) + correction;    /* FIXME */\r
-               pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);\r
-               \r
-               /* Code-switch "RESET" */\r
-               if (cblksty & J3D_CCP_CBLKSTY_RESET)\r
-                       mqc_reset_enc(mqc);\r
-       }\r
-       \r
-       /* Code switch "ERTERM" (i.e. PTERM) */\r
-       if (cblksty & J3D_CCP_CBLKSTY_PTERM)\r
-               mqc_erterm_enc(mqc);\r
-       else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY))\r
-               mqc_flush(mqc);\r
-       \r
-       cblk->totalpasses = passno;\r
-}\r
-\r
-static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {\r
-       int i, j, k, w, h, l;\r
-       int bpno, passtype;\r
-       int segno, passno;\r
-       char type = T1_TYPE_MQ; /* BYPASS mode */\r
-       \r
-       opj_raw_t *raw = t1->raw;       /* RAW component */\r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       w = cblk->x1 - cblk->x0;\r
-       h = cblk->y1 - cblk->y0;\r
-       l = cblk->z1 - cblk->z0;\r
-\r
-    for (k = 0; k < l; k++) {\r
-               for (j = 0; j < h; j++) {\r
-                       for (i = 0; i < w; i++) {\r
-                               t1->data[k][j][i] = 0;\r
-                       }\r
-               }\r
-       }\r
-       \r
-       for (k = 0; k <= l; k++) {\r
-               for (j = 0; j <= h; j++) {\r
-                       for (i = 0; i <= w; i++) {\r
-                               t1->flags[k][j][i] = 0;\r
-                       }\r
-               }\r
-       }\r
-\r
-       bpno = roishift + cblk->numbps - 1;\r
-       passtype = 2;\r
-       \r
-       mqc_reset_enc(mqc);\r
-       \r
-       for (segno = 0; segno < cblk->numsegs; segno++) {\r
-               opj_tcd_seg_t *seg = &cblk->segs[segno];\r
-               \r
-               /* BYPASS mode */\r
-               type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;\r
-               if (type == T1_TYPE_RAW) {\r
-                       raw_init_dec(raw, seg->data, seg->len);\r
-               } else {\r
-                       mqc_init_dec(mqc, seg->data, seg->len);\r
-               }\r
-\r
-               for (passno = 0; passno < seg->numpasses; passno++) {\r
-                       switch (passtype) {\r
-                               case 0:\r
-                                       t1_dec_sigpass(t1, w, h, l, bpno+1, orient, type, cblksty);\r
-                                       break;\r
-                               case 1:\r
-                                       t1_dec_refpass(t1, w, h, l, bpno+1, type, cblksty);\r
-                                       break;\r
-                               case 2:\r
-                                       t1_dec_clnpass(t1, w, h, l, bpno+1, orient, cblksty);\r
-                                       break;\r
-                       }\r
-                       \r
-                       if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {\r
-                               mqc_reset_enc(mqc);\r
-                       }\r
-                       if (++passtype == 3) {\r
-                               passtype = 0;\r
-                               bpno--;\r
-                       }\r
-               }\r
-       }\r
-}\r
-\r
-static int t1_init_ctxno_zc(int f, int orient) {\r
-       int h, v, d, n, t, hv;\r
-       n = 0;\r
-       h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);\r
-       v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);\r
-       d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);\r
-       \r
-       switch (orient) {\r
-               case 2:\r
-                       t = h;\r
-                       h = v;\r
-                       v = t;\r
-               case 0:\r
-               case 1:\r
-                       if (!h) {\r
-                               if (!v) {\r
-                                       if (!d)\r
-                                               n = 0;\r
-                                       else if (d == 1)\r
-                                               n = 1;\r
-                                       else\r
-                                               n = 2;\r
-                               } else if (v == 1) {\r
-                                       n = 3;\r
-                               } else {\r
-                                       n = 4;\r
-                               }\r
-                       } else if (h == 1) {\r
-                               if (!v) {\r
-                                       if (!d)\r
-                                               n = 5;\r
-                                       else\r
-                                               n = 6;\r
-                               } else {\r
-                                       n = 7;\r
-                               }\r
-                       } else\r
-                               n = 8;\r
-                       break;\r
-               case 3:\r
-                       hv = h + v;\r
-                       if (!d) {\r
-                               if (!hv) {\r
-                                       n = 0;\r
-                               } else if (hv == 1) {\r
-                                       n = 1;\r
-                               } else {\r
-                                       n = 2;\r
-                               }\r
-                       } else if (d == 1) {\r
-                               if (!hv) {\r
-                                       n = 3;\r
-                               } else if (hv == 1) {\r
-                                       n = 4;\r
-                               } else {\r
-                                       n = 5;\r
-                               }\r
-                       } else if (d == 2) {\r
-                               if (!hv) {\r
-                                       n = 6;\r
-                               } else {\r
-                                       n = 7;\r
-                               }\r
-                       } else {\r
-                               n = 8;\r
-                       }\r
-                       break;\r
-       }\r
-       \r
-       return (T1_CTXNO_ZC + n);\r
-}\r
-\r
-static int t1_init_ctxno_sc(int f) {\r
-       int hc, vc, n;\r
-       n = 0;\r
-\r
-       hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==\r
-               T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),\r
-              1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==\r
-                  (T1_SIG_E | T1_SGN_E)) +\r
-                  ((f & (T1_SIG_W | T1_SGN_W)) ==\r
-                  (T1_SIG_W | T1_SGN_W)), 1);\r
-       \r
-       vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==\r
-               T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),\r
-              1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==\r
-                  (T1_SIG_N | T1_SGN_N)) +\r
-                  ((f & (T1_SIG_S | T1_SGN_S)) ==\r
-                  (T1_SIG_S | T1_SGN_S)), 1);\r
-       \r
-       if (hc < 0) {\r
-               hc = -hc;\r
-               vc = -vc;\r
-       }\r
-       if (!hc) {\r
-               if (vc == -1)\r
-                       n = 1;\r
-               else if (!vc)\r
-                       n = 0;\r
-               else\r
-                       n = 1;\r
-       } else if (hc == 1) {\r
-               if (vc == -1)\r
-                       n = 2;\r
-               else if (!vc)\r
-                       n = 3;\r
-               else\r
-                       n = 4;\r
-       }\r
-       \r
-       return (T1_CTXNO_SC + n);\r
-}\r
-\r
-static int t1_init_ctxno_mag(int f) {\r
-       int n;\r
-       if (!(f & T1_REFINE))\r
-               n = (f & (T1_SIG_OTH)) ? 1 : 0;\r
-       else\r
-               n = 2;\r
-       \r
-       return (T1_CTXNO_MAG + n);\r
-}\r
-\r
-static int t1_init_spb(int f) {\r
-       int hc, vc, n;\r
-       \r
-       hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==\r
-               T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),\r
-              1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==\r
-                  (T1_SIG_E | T1_SGN_E)) +\r
-                  ((f & (T1_SIG_W | T1_SGN_W)) ==\r
-                  (T1_SIG_W | T1_SGN_W)), 1);\r
-       \r
-       vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==\r
-               T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),\r
-              1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==\r
-                  (T1_SIG_N | T1_SGN_N)) +\r
-                  ((f & (T1_SIG_S | T1_SGN_S)) ==\r
-                  (T1_SIG_S | T1_SGN_S)), 1);\r
-       \r
-       if (!hc && !vc)\r
-               n = 0;\r
-       else\r
-               n = (!(hc > 0 || (!hc && vc > 0)));\r
-       \r
-       return n;\r
-}\r
-\r
-static void t1_init_luts(opj_t1_t *t1) {\r
-       int i, j;\r
-       double u, v, t;\r
-       for (j = 0; j < 4; j++) {\r
-               for (i = 0; i < 256; ++i) {\r
-                       t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);\r
-               }\r
-       }\r
-       for (i = 0; i < 256; i++) {\r
-               t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);\r
-       }\r
-       for (j = 0; j < 2; j++) {\r
-               for (i = 0; i < 2048; ++i) {\r
-                       t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);\r
-               }\r
-       }\r
-       for (i = 0; i < 256; ++i) {\r
-               t1->lut_spb[i] = t1_init_spb(i << 4);\r
-       }\r
-       /* FIXME FIXME FIXME */\r
-       /* fprintf(stdout,"nmsedec luts:\n"); */\r
-       for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {\r
-               t = i / pow(2, T1_NMSEDEC_FRACBITS);\r
-               u = t;\r
-               v = t - 1.5;\r
-               t1->lut_nmsedec_sig[i] = \r
-                       int_max(0, \r
-                       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
-               t1->lut_nmsedec_sig0[i] =\r
-                       int_max(0,\r
-                       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
-               u = t - 1.0;\r
-               if (i & (1 << (T1_NMSEDEC_BITS - 1))) {\r
-                       v = t - 1.5;\r
-               } else {\r
-                       v = t - 0.5;\r
-               }\r
-               t1->lut_nmsedec_ref[i] =\r
-                       int_max(0,\r
-                       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
-               t1->lut_nmsedec_ref0[i] =\r
-                       int_max(0,\r
-                       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
-       }\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-opj_t1_t* t1_create(opj_common_ptr cinfo) {\r
-       opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));\r
-       if(t1) {\r
-               t1->cinfo = cinfo;\r
-               /* create MQC and RAW handles */\r
-               t1->mqc = mqc_create();\r
-               t1->raw = raw_create();\r
-               /* initialize the look-up tables of the Tier-1 coder/decoder */\r
-               t1_init_luts(t1);\r
-       }\r
-       return t1;\r
-}\r
-\r
-void t1_destroy(opj_t1_t *t1) {\r
-       if(t1) {\r
-               /* destroy MQC and RAW handles */\r
-               mqc_destroy(t1->mqc);\r
-               raw_destroy(t1->raw);\r
-               /*opj_free(t1->data);*/\r
-               /*opj_free(t1->flags);*/\r
-               opj_free(t1);\r
-       }\r
-}\r
-\r
-void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {\r
-       int compno, resno, bandno, precno, cblkno;\r
-       int x, y, z, i, j, k, orient;\r
-       int n=0;\r
-       int level[3];\r
-       FILE *fid = NULL;\r
-/*     char filename[10];*/\r
-       tile->distotile = 0;            /* fixed_quality */\r
-       \r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-\r
-               for (resno = 0; resno < tilec->numresolution[0]; resno++) {\r
-                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-                       \r
-                       /* Weighted first order entropy\r
-                       sprintf(filename,"res%d.txt",resno);\r
-                       if ((fid = fopen(filename,"w")) == 0){\r
-                               fprintf(stdout,"Error while opening %s\n", filename);\r
-                               exit(1);\r
-                       }\r
-                       */\r
-                       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                               opj_tcd_band_t *band = &res->bands[bandno];\r
-                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {\r
-                                       opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-\r
-                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-\r
-                                               /*fprintf(stdout,"Precno %d Cblkno %d \n",precno,cblkno);*/\r
-                                               if (band->bandno == 0) {\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 1) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 2) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 3) {         \r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 4) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 5) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 6) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 7) {         \r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               }\r
-\r
-                                               if (tcp->tccps[compno].reversible == 1) {\r
-                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {\r
-                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
-                                    for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
-                                        t1->data[k][j][i] = \r
-                                                                               tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS;\r
-/*fprintf(fid," %d",t1->data[k][j][i]);*/\r
-                                                                       }\r
-                                                               }\r
-                                                       }\r
-                                               } else if (tcp->tccps[compno].reversible == 0) {\r
-                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {\r
-                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
-                                    for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
-                                        t1->data[k][j][i] = fix_mul(\r
-                                                                               tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)],\r
-                                                                               8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);\r
-                                                                       }\r
-                                                               }\r
-                                                       }\r
-                                               }\r
-\r
-                                               orient = band->bandno;  /* FIXME */\r
-                                               if (orient == 2) {\r
-                                                       orient = 1;\r
-                                               } else if (orient == 1) {\r
-                                                       orient = 2;\r
-                                               }\r
-                                               for (i = 0; i < 3; i++) \r
-                                                       level[i] = tilec->numresolution[i] - 1 - resno;\r
-                                               /*fprintf(stdout,"t1_encode_cblk(t1, cblk, %d, %d, %d %d %d, %d, %f, %d, %d, tile);\n", orient, compno, level[0], level[1], level[2], tcp->tccps[compno].reversible, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps);*/\r
-                                               t1_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);\r
-                                                       \r
-                                       } /* cblkno */\r
-                               } /* precno */\r
-/*fprintf(fid,"\n");*/\r
-                       } /* bandno */\r
-/*fclose(fid);*/\r
-               } /* resno  */\r
-       } /* compno  */\r
-}\r
-\r
-void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {\r
-       int compno, resno, bandno, precno, cblkno;\r
-       \r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-\r
-               for (resno = 0; resno < tilec->numresolution[0]; resno++) {\r
-                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-\r
-                       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                               opj_tcd_band_t *band = &res->bands[bandno];\r
-\r
-                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {\r
-                                       opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-\r
-                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                                               int x, y, k, i, j, z, orient;\r
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-\r
-                                               orient = band->bandno;  /* FIXME */\r
-                                               if (orient == 2) {\r
-                                                       orient = 1;\r
-                                               } else if (orient == 1) {\r
-                                                       orient = 2;\r
-                                               }\r
-\r
-                                               t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);\r
-\r
-                                               if (band->bandno == 0) {\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 1) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 2) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 3) {         \r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 4) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 5) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 6) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 7) {         \r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               }\r
-                                               \r
-                                               if (tcp->tccps[compno].roishift) {\r
-                                                       int thresh, val, mag;\r
-                                                       thresh = 1 << tcp->tccps[compno].roishift;\r
-                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {\r
-                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
-                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
-                                                                               val = t1->data[k][j][i];\r
-                                                                               mag = int_abs(val);\r
-                                                                               if (mag >= thresh) {\r
-                                                                                       mag >>= tcp->tccps[compno].roishift;\r
-                                                                                       t1->data[k][j][i] = val < 0 ? -mag : mag;\r
-                                                                               }\r
-                                                                       }\r
-                                                               }\r
-                                                       }\r
-                                               }\r
-                                               \r
-                                               if (tcp->tccps[compno].reversible == 1) {\r
-                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {\r
-                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
-                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
-                                                                               int tmp = t1->data[k][j][i];\r
-                                                                               tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp/2;\r
-                                                                       }\r
-                                                               }\r
-                                                       }\r
-                                               } else {                /* if (tcp->tccps[compno].reversible == 0) */\r
-                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {\r
-                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
-                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
-                                                                               double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0);\r
-                                                                               if (t1->data[k][j][i] >> 1 == 0) {\r
-                                                                                       tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0;\r
-                                                                               } else {\r
-                                                                                       int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);\r
-                                                                                       tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp<0)?-tmp2:tmp2);\r
-                                                                               }\r
-                                                                       }\r
-                                                               }\r
-                                                       }\r
-                                               }\r
-                                       } /* cblkno */\r
-                               } /* precno */\r
-                       } /* bandno */\r
-               } /* resno */\r
-       } /* compno */\r
-}\r
-\r
-\r
-/** mod fixed_quality */\r
-double t1_getwmsedec(int nmsedec, int compno, int level[3], int orient, int bpno, double stepsize, int numcomps, int dwtid[3]) {\r
-       double w1, w2, wmsedec;\r
-       \r
-       if (dwtid[0] == 1 || dwtid[1] == 1 || dwtid[2] == 1) {\r
-               w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;\r
-       } else {                        \r
-               w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;\r
-       }\r
-       w2 = dwt_getnorm(orient, level, dwtid);\r
-\r
-       /*fprintf(stdout,"nmsedec %d level %d %d %d orient %d bpno %d stepsize %f \n",nmsedec ,level[0],level[1],level[2],orient,bpno,stepsize);*/\r
-       wmsedec = w1 * w2 * stepsize * (1 << bpno);\r
-       wmsedec *= wmsedec * nmsedec / 8192.0;\r
-       \r
-       return wmsedec;\r
-}\r
-/** mod fixed_quality */\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup T1 T1 - Implementation of the tier-1 coding */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient);
+static int t1_getctxno_sc(opj_t1_t *t1, int f);
+static int t1_getctxno_mag(opj_t1_t *t1, int f);
+static int t1_getspb(opj_t1_t *t1, int f);
+static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos);
+static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos);
+static void t1_updateflags(int *fp, int s);
+/**
+Encode significant pass
+*/
+static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc);
+/**
+Decode significant pass
+*/
+static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc);
+/**
+Encode significant pass
+*/
+static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty);
+/**
+Decode significant pass
+*/
+static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty);
+/**
+Encode refinement pass
+*/
+static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
+/**
+Decode refinement pass
+*/
+static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc);
+/**
+Encode refinement pass
+*/
+static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty);
+/**
+Decode refinement pass
+*/
+static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, char type, int cblksty);
+/**
+Encode clean-up pass
+*/
+static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc);
+/**
+Decode clean-up pass
+*/
+static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc);
+/**
+Encode clean-up pass
+*/
+static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty);
+/**
+Decode clean-up pass
+*/
+static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int cblksty);
+/**
+Encode 1 code-block
+@param t1 T1 handle
+@param cblk Code-block coding parameters
+@param orient
+@param compno Component number
+@param level
+@param dwtid
+@param stepsize
+@param cblksty Code-block style
+@param numcomps
+@param tile
+*/
+static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile);
+/**
+Decode 1 code-block
+@param t1 T1 handle
+@param cblk Code-block coding parameters
+@param orient
+@param roishift Region of interest shifting value
+@param cblksty Code-block style
+*/
+static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
+
+static int t1_init_ctxno_zc(int f, int orient);
+static int t1_init_ctxno_sc(int f);
+static int t1_init_ctxno_mag(int f);
+static int t1_init_spb(int f);
+/**
+Initialize the look-up tables of the Tier-1 coder/decoder
+@param t1 T1 handle
+*/
+static void t1_init_luts(opj_t1_t *t1);
+
+/*@}*/
+
+/*@}*/
+
+/* ----------------------------------------------------------------------- */
+
+static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient) {
+       return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
+}
+
+static int t1_getctxno_sc(opj_t1_t *t1, int f) {
+       return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
+}
+
+static int t1_getctxno_mag(opj_t1_t *t1, int f) {
+       return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
+}
+
+static int t1_getspb(opj_t1_t *t1, int f) {
+       return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
+}
+
+static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos) {
+       if (bitpos > T1_NMSEDEC_FRACBITS) {
+               return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
+       }
+       
+       return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
+}
+
+static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos) {
+       if (bitpos > T1_NMSEDEC_FRACBITS) {
+               return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
+       }
+
+    return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
+}
+
+static void t1_updateflags(int *fp, int s) {
+       int *np = fp - (T1_MAXCBLKW + 2);
+       int *sp = fp + (T1_MAXCBLKW + 2);
+       np[-1] |= T1_SIG_SE;
+       np[1] |= T1_SIG_SW;
+       sp[-1] |= T1_SIG_NE;
+       sp[1] |= T1_SIG_NW;
+       *np |= T1_SIG_S;
+       *sp |= T1_SIG_N;
+       fp[-1] |= T1_SIG_E;
+       fp[1] |= T1_SIG_W;
+       if (s) {
+               *np |= T1_SGN_S;
+               *sp |= T1_SGN_N;
+               fp[-1] |= T1_SGN_E;
+               fp[1] |= T1_SGN_W;
+       }
+}
+
+static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) {
+       int v, flag;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
+       if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
+               v = int_abs(*dp) & one ? 1 : 0;
+               if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
+                       mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));   /* ESSAI */
+                       mqc_bypass_enc(mqc, v);
+               } else {
+                       mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
+                       mqc_encode(mqc, v);
+               }
+               if (v) {
+                       v = *dp < 0 ? 1 : 0;
+                       *nmsedec +=     t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
+                       if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
+                               mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));   /* ESSAI */
+                               mqc_bypass_enc(mqc, v);
+                       } else {
+                               mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
+                               mqc_encode(mqc, v ^ t1_getspb(t1, flag));
+                       }
+                       t1_updateflags(fp, v);
+                       *fp |= T1_SIG;
+               }
+               *fp |= T1_VISIT;
+       }
+}
+
+static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {
+       int v, flag;
+       
+       opj_raw_t *raw = t1->raw;       /* RAW component */
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
+       if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
+               if (type == T1_TYPE_RAW) {
+                       if (raw_decode(raw)) {
+                               v = raw_decode(raw);    /* ESSAI */
+                               *dp = v ? -oneplushalf : oneplushalf;
+                               t1_updateflags(fp, v);
+                               *fp |= T1_SIG;
+                       }
+               } else {
+                       mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
+                       if (mqc_decode(mqc)) {
+                               mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
+                               v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
+                               *dp = v ? -oneplushalf : oneplushalf;
+                               t1_updateflags(fp, v);
+                               *fp |= T1_SIG;
+                       }
+               }
+               *fp |= T1_VISIT;
+       }
+}                              /* VSC and  BYPASS by Antonin */
+
+static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty) {
+       int i, j, k, m, one, vsc;
+       *nmsedec = 0;
+       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
+       for (m = 0; m < l; m++) {
+       for (k = 0; k < h; k += 4) {
+               for (i = 0; i < w; i++) {
+                       for (j = k; j < k + 4 && j < h; j++) {
+                               vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                               t1_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, type, vsc);
+                       }
+               }
+       }
+       }
+}
+
+static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty) {
+       int i, j, k, m, one, half, oneplushalf, vsc;
+       one = 1 << bpno;
+       half = one >> 1;
+       oneplushalf = one | half;
+       for (m = 0; m < l; m++) {
+       for (k = 0; k < h; k += 4) {
+               for (i = 0; i < w; i++) {
+                       for (j = k; j < k + 4 && j < h; j++) {
+                               vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                               t1_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, type, vsc);
+                       }
+               }
+       }
+       }
+}                              /* VSC and  BYPASS by Antonin */
+
+static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
+       int v, flag;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
+       if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
+               *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
+               v = int_abs(*dp) & one ? 1 : 0;
+               if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
+                       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));  /* ESSAI */
+                       mqc_bypass_enc(mqc, v);
+               } else {
+                       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
+                       mqc_encode(mqc, v);
+               }
+               *fp |= T1_REFINE;
+       }
+}
+
+static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {
+       int v, t, flag;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       opj_raw_t *raw = t1->raw;       /* RAW component */
+       
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
+       if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
+               if (type == T1_TYPE_RAW) {
+                       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));  /* ESSAI */
+                       v = raw_decode(raw);
+               } else {
+                       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
+                       v = mqc_decode(mqc);
+               }
+               t = v ? poshalf : neghalf;
+               *dp += *dp < 0 ? -t : t;
+               *fp |= T1_REFINE;
+       }
+}                              /* VSC and  BYPASS by Antonin  */
+
+static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty) {
+       int i, j, k, m, one, vsc;
+       *nmsedec = 0;
+       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
+       for (m = 0; m < l; m++) {
+               for (k = 0; k < h; k += 4) {
+               for (i = 0; i < w; i++) {
+                       for (j = k; j < k + 4 && j < h; j++) {
+                               vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                               t1_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type, vsc);
+                       }
+               }
+       }
+       }
+}
+
+static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, char type, int cblksty) {
+       int i, j, k, m, one, poshalf, neghalf;
+       int vsc;
+       one = 1 << bpno;
+       poshalf = one >> 1;
+       neghalf = bpno > 0 ? -poshalf : -1;
+       for (m = 0; m < l; m++) {
+               for (k = 0; k < h; k += 4) {
+               for (i = 0; i < w; i++) {
+                       for (j = k; j < k + 4 && j < h; j++) {
+                               vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                               t1_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type, vsc);
+                       }
+               }
+       }
+       }
+}                              /* VSC and  BYPASS by Antonin */
+
+static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) {
+       int v, flag;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
+       if (partial) {
+               goto LABEL_PARTIAL;
+       }
+       if (!(*fp & (T1_SIG | T1_VISIT))) {
+               mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
+               v = int_abs(*dp) & one ? 1 : 0;
+               mqc_encode(mqc, v);
+               if (v) {
+LABEL_PARTIAL:
+                       *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
+                       mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
+                       v = *dp < 0 ? 1 : 0;
+                       mqc_encode(mqc, v ^ t1_getspb(t1, flag));
+                       t1_updateflags(fp, v);
+                       *fp |= T1_SIG;
+               }
+       }
+       *fp &= ~T1_VISIT;
+}
+
+static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {
+       int v, flag;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
+       if (partial) {
+               goto LABEL_PARTIAL;
+       }
+       if (!(flag & (T1_SIG | T1_VISIT))) {
+               mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
+               if (mqc_decode(mqc)) {
+LABEL_PARTIAL:
+                       mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
+                       v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
+                       *dp = v ? -oneplushalf : oneplushalf;
+                       t1_updateflags(fp, v);
+                       *fp |= T1_SIG;
+               }
+       }
+       *fp &= ~T1_VISIT;
+}                              /* VSC and  BYPASS by Antonin */
+
+static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty) {
+       int i, j, k, m, one, agg, runlen, vsc;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       *nmsedec = 0;
+       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
+       for (m = 0; m < l; m++) {
+               for (k = 0; k < h; k += 4) {
+                       for (i = 0; i < w; i++) {
+                               if (k + 3 < h) {
+                                       if (cblksty & J3D_CCP_CBLKSTY_VSC) {
+                                               agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                                       || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                                       || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                                       || (t1->flags[1 + m][1 + k + 3][1 + i] 
+                                                       & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
+                                       } else {
+                                               agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                                       || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                                       || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                                       || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
+                                       }
+                               } else {
+                                       agg = 0;
+                               }
+                               if (agg) {
+                                       for (runlen = 0; runlen < 4; runlen++) {
+                                               if (int_abs(t1->data[m][k + runlen][i]) & one)
+                                                       break;
+                                       }
+                                       mqc_setcurctx(mqc, T1_CTXNO_AGG);
+                                       mqc_encode(mqc, runlen != 4);
+                                       if (runlen == 4) {
+                                               continue;
+                                       }
+                                       mqc_setcurctx(mqc, T1_CTXNO_UNI);
+                                       mqc_encode(mqc, runlen >> 1);
+                                       mqc_encode(mqc, runlen & 1);
+                               } else {
+                                       runlen = 0;
+                               }
+                               for (j = k + runlen; j < k + 4 && j < h; j++) {
+                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                                       t1_enc_clnpass_step(t1, &(t1->flags[1 + m][1 + j][1 + i]), &(t1->data[m][j][i]), orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
+                               }
+                       }
+       }
+       }
+}
+
+static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int cblksty) {
+       int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc;
+       int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       one = 1 << bpno;
+       half = one >> 1;
+       oneplushalf = one | half;
+       for (m = 0; m < l; m++) {
+               for (k = 0; k < h; k += 4) {
+               for (i = 0; i < w; i++) {
+                       if (k + 3 < h) {
+                               if (cblksty & J3D_CCP_CBLKSTY_VSC) {
+                                       agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                               || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                               || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                               || (t1->flags[1 + m][1 + k + 3][1 + i] 
+                                               & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
+                               } else {
+                                       agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                               || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                               || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                               || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
+                               }
+                       } else {
+                               agg = 0;
+                       }
+                       if (agg) {
+                               mqc_setcurctx(mqc, T1_CTXNO_AGG);
+                               if (!mqc_decode(mqc)) {
+                                       continue;
+                               }
+                               mqc_setcurctx(mqc, T1_CTXNO_UNI);
+                               runlen = mqc_decode(mqc);
+                               runlen = (runlen << 1) | mqc_decode(mqc);
+                       } else {
+                               runlen = 0;
+                       }
+                       for (j = k + runlen; j < k + 4 && j < h; j++) {
+                               vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                               t1_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
+                       }
+               }
+       }
+       }
+       if (segsym) {
+               int v = 0;
+               mqc_setcurctx(mqc, T1_CTXNO_UNI);
+               v = mqc_decode(mqc);
+               v = (v << 1) | mqc_decode(mqc);
+               v = (v << 1) | mqc_decode(mqc);
+               v = (v << 1) | mqc_decode(mqc);
+               /*
+               if (v!=0xa) {
+                       opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
+               } 
+               */
+       }
+}                              /* VSC and  BYPASS by Antonin */
+
+
+static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {
+       int i, j, k;
+       int w, h, l;
+       int passno;
+       int bpno, passtype;
+       int max;
+       int nmsedec = 0;
+       double cumwmsedec = 0;
+       char type = T1_TYPE_MQ;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       w = cblk->x1 - cblk->x0;
+       h = cblk->y1 - cblk->y0;
+       l = cblk->z1 - cblk->z0;
+
+       max = 0;
+       for (k = 0; k < l; k++) {
+               for (j = 0; j < h; j++) {
+                       for (i = 0; i < w; i++) {
+                               max = int_max(max, int_abs(t1->data[k][j][i]));
+                       }
+               }
+       }
+       for (k = 0; k <= l; k++) {
+               for (j = 0; j <= h; j++) {
+                       for (i = 0; i <= w; i++) {
+                               t1->flags[k][j][i] = 0; 
+                       }
+               }
+       }
+
+       cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
+       
+       bpno = cblk->numbps - 1;
+       passtype = 2;
+       
+       mqc_reset_enc(mqc);
+       mqc_init_enc(mqc, cblk->data);
+       
+       for (passno = 0; bpno >= 0; passno++) {
+               opj_tcd_pass_t *pass = &cblk->passes[passno];
+               int correction = 3;
+               double tmpwmsedec;
+               type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
+               /*fprintf(stdout,"passno %d passtype %d w %d h %d l %d bpno %d orient %d type %d cblksty %d\n",passno,passtype,w,h,l,bpno,orient,type,cblksty);*/
+
+               switch (passtype) {
+                       case 0:
+                               t1_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty);
+                               break;
+                       case 1:
+                               t1_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty);
+                               break;
+                       case 2:
+                               /*fprintf(stdout,"w %d h %d l %d bpno %d orient %d \n",w,h,l,bpno,orient);*/
+                               t1_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty);
+                               /* code switch SEGMARK (i.e. SEGSYM) */
+                               if (cblksty & J3D_CCP_CBLKSTY_SEGSYM)
+                                       mqc_segmark_enc(mqc);
+                               break;
+               }
+               
+               /* fixed_quality */
+               tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize, numcomps, dwtid);
+               cumwmsedec += tmpwmsedec;
+               tile->distotile += tmpwmsedec;
+               
+               /* Code switch "RESTART" (i.e. TERMALL) */
+               if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
+                       if (type == T1_TYPE_RAW) {
+                               mqc_flush(mqc);
+                               correction = 1;
+                               /* correction = mqc_bypass_flush_enc(); */
+                       } else {                        /* correction = mqc_restart_enc(); */
+                               mqc_flush(mqc);
+                               correction = 1;
+                       }
+                       pass->term = 1;
+               } else {
+                       if (((bpno < (cblk->numbps - 4) && (passtype > 0)) 
+                               || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) {
+                               if (type == T1_TYPE_RAW) {
+                                       mqc_flush(mqc);
+                                       correction = 1;
+                                       /* correction = mqc_bypass_flush_enc(); */
+                               } else {                /* correction = mqc_restart_enc(); */
+                                       mqc_flush(mqc);
+                                       correction = 1;
+                               }
+                               pass->term = 1;
+                       } else {
+                               pass->term = 0;
+                       }
+               }
+               
+               if (++passtype == 3) {
+                       passtype = 0;
+                       bpno--;
+               }
+               
+               if (pass->term && bpno > 0) {
+                       type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
+                       if (type == T1_TYPE_RAW)
+                               mqc_bypass_init_enc(mqc);
+                       else
+                               mqc_restart_init_enc(mqc);
+               }
+               
+               pass->distortiondec = cumwmsedec;
+               pass->rate = mqc_numbytes(mqc) + correction;    /* FIXME */
+               pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
+               
+               /* Code-switch "RESET" */
+               if (cblksty & J3D_CCP_CBLKSTY_RESET)
+                       mqc_reset_enc(mqc);
+       }
+       
+       /* Code switch "ERTERM" (i.e. PTERM) */
+       if (cblksty & J3D_CCP_CBLKSTY_PTERM)
+               mqc_erterm_enc(mqc);
+       else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY))
+               mqc_flush(mqc);
+       
+       cblk->totalpasses = passno;
+}
+
+static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
+       int i, j, k, w, h, l;
+       int bpno, passtype;
+       int segno, passno;
+       char type = T1_TYPE_MQ; /* BYPASS mode */
+       
+       opj_raw_t *raw = t1->raw;       /* RAW component */
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       w = cblk->x1 - cblk->x0;
+       h = cblk->y1 - cblk->y0;
+       l = cblk->z1 - cblk->z0;
+
+    for (k = 0; k < l; k++) {
+               for (j = 0; j < h; j++) {
+                       for (i = 0; i < w; i++) {
+                               t1->data[k][j][i] = 0;
+                       }
+               }
+       }
+       
+       for (k = 0; k <= l; k++) {
+               for (j = 0; j <= h; j++) {
+                       for (i = 0; i <= w; i++) {
+                               t1->flags[k][j][i] = 0;
+                       }
+               }
+       }
+
+       bpno = roishift + cblk->numbps - 1;
+       passtype = 2;
+       
+       mqc_reset_enc(mqc);
+       
+       for (segno = 0; segno < cblk->numsegs; segno++) {
+               opj_tcd_seg_t *seg = &cblk->segs[segno];
+               
+               /* BYPASS mode */
+               type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
+               if (type == T1_TYPE_RAW) {
+                       raw_init_dec(raw, seg->data, seg->len);
+               } else {
+                       mqc_init_dec(mqc, seg->data, seg->len);
+               }
+
+               for (passno = 0; passno < seg->numpasses; passno++) {
+                       switch (passtype) {
+                               case 0:
+                                       t1_dec_sigpass(t1, w, h, l, bpno+1, orient, type, cblksty);
+                                       break;
+                               case 1:
+                                       t1_dec_refpass(t1, w, h, l, bpno+1, type, cblksty);
+                                       break;
+                               case 2:
+                                       t1_dec_clnpass(t1, w, h, l, bpno+1, orient, cblksty);
+                                       break;
+                       }
+                       
+                       if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
+                               mqc_reset_enc(mqc);
+                       }
+                       if (++passtype == 3) {
+                               passtype = 0;
+                               bpno--;
+                       }
+               }
+       }
+}
+
+static int t1_init_ctxno_zc(int f, int orient) {
+       int h, v, d, n, t, hv;
+       n = 0;
+       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);
+       
+       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;
+       }
+       
+       return (T1_CTXNO_ZC + n);
+}
+
+static int t1_init_ctxno_sc(int f) {
+       int hc, vc, n;
+       n = 0;
+
+       hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
+               T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
+              1) - 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 = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
+               T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
+              1) - 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;
+       }
+       
+       return (T1_CTXNO_SC + n);
+}
+
+static int t1_init_ctxno_mag(int f) {
+       int n;
+       if (!(f & T1_REFINE))
+               n = (f & (T1_SIG_OTH)) ? 1 : 0;
+       else
+               n = 2;
+       
+       return (T1_CTXNO_MAG + n);
+}
+
+static int t1_init_spb(int f) {
+       int hc, vc, n;
+       
+       hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
+               T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
+              1) - 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 = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
+               T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
+              1) - 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)));
+       
+       return n;
+}
+
+static void t1_init_luts(opj_t1_t *t1) {
+       int i, j;
+       double u, v, t;
+       for (j = 0; j < 4; j++) {
+               for (i = 0; i < 256; ++i) {
+                       t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
+               }
+       }
+       for (i = 0; i < 256; i++) {
+               t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
+       }
+       for (j = 0; j < 2; j++) {
+               for (i = 0; i < 2048; ++i) {
+                       t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
+               }
+       }
+       for (i = 0; i < 256; ++i) {
+               t1->lut_spb[i] = t1_init_spb(i << 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;
+               t1->lut_nmsedec_sig[i] = 
+                       int_max(0, 
+                       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+               t1->lut_nmsedec_sig0[i] =
+                       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;
+               }
+               t1->lut_nmsedec_ref[i] =
+                       int_max(0,
+                       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+               t1->lut_nmsedec_ref0[i] =
+                       int_max(0,
+                       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+       }
+}
+
+/* ----------------------------------------------------------------------- */
+
+opj_t1_t* t1_create(opj_common_ptr cinfo) {
+       opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
+       if(t1) {
+               t1->cinfo = cinfo;
+               /* create MQC and RAW handles */
+               t1->mqc = mqc_create();
+               t1->raw = raw_create();
+               /* initialize the look-up tables of the Tier-1 coder/decoder */
+               t1_init_luts(t1);
+       }
+       return t1;
+}
+
+void t1_destroy(opj_t1_t *t1) {
+       if(t1) {
+               /* destroy MQC and RAW handles */
+               mqc_destroy(t1->mqc);
+               raw_destroy(t1->raw);
+               /*opj_free(t1->data);*/
+               /*opj_free(t1->flags);*/
+               opj_free(t1);
+       }
+}
+
+void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
+       int compno, resno, bandno, precno, cblkno;
+       int x, y, z, i, j, k, orient;
+       int n=0;
+       int level[3];
+       FILE *fid = NULL;
+/*     char filename[10];*/
+       tile->distotile = 0;            /* fixed_quality */
+       
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+
+               for (resno = 0; resno < tilec->numresolution[0]; resno++) {
+                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+                       
+                       /* Weighted first order entropy
+                       sprintf(filename,"res%d.txt",resno);
+                       if ((fid = fopen(filename,"w")) == 0){
+                               fprintf(stdout,"Error while opening %s\n", filename);
+                               exit(1);
+                       }
+                       */
+                       for (bandno = 0; bandno < res->numbands; bandno++) {
+                               opj_tcd_band_t *band = &res->bands[bandno];
+                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
+                                       opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+
+                                               /*fprintf(stdout,"Precno %d Cblkno %d \n",precno,cblkno);*/
+                                               if (band->bandno == 0) {
+                                                       x = cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 1) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 2) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 3) {         
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 4) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 5) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 6) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 7) {         
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               }
+
+                                               if (tcp->tccps[compno].reversible == 1) {
+                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {
+                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                                    for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                                        t1->data[k][j][i] = 
+                                                                               tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS;
+/*fprintf(fid," %d",t1->data[k][j][i]);*/
+                                                                       }
+                                                               }
+                                                       }
+                                               } else if (tcp->tccps[compno].reversible == 0) {
+                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {
+                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                                    for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                                        t1->data[k][j][i] = fix_mul(
+                                                                               tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)],
+                                                                               8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+
+                                               orient = band->bandno;  /* FIXME */
+                                               if (orient == 2) {
+                                                       orient = 1;
+                                               } else if (orient == 1) {
+                                                       orient = 2;
+                                               }
+                                               for (i = 0; i < 3; i++) 
+                                                       level[i] = tilec->numresolution[i] - 1 - resno;
+                                               /*fprintf(stdout,"t1_encode_cblk(t1, cblk, %d, %d, %d %d %d, %d, %f, %d, %d, tile);\n", orient, compno, level[0], level[1], level[2], tcp->tccps[compno].reversible, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps);*/
+                                               t1_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
+                                                       
+                                       } /* cblkno */
+                               } /* precno */
+/*fprintf(fid,"\n");*/
+                       } /* bandno */
+/*fclose(fid);*/
+               } /* resno  */
+       } /* compno  */
+}
+
+void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
+       int compno, resno, bandno, precno, cblkno;
+       
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+
+               for (resno = 0; resno < tilec->numresolution[0]; resno++) {
+                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
+                       for (bandno = 0; bandno < res->numbands; bandno++) {
+                               opj_tcd_band_t *band = &res->bands[bandno];
+
+                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
+                                       opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                                               int x, y, k, i, j, z, orient;
+                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+
+                                               orient = band->bandno;  /* FIXME */
+                                               if (orient == 2) {
+                                                       orient = 1;
+                                               } else if (orient == 1) {
+                                                       orient = 2;
+                                               }
+
+                                               t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
+
+                                               if (band->bandno == 0) {
+                                                       x = cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 1) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 2) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 3) {         
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 4) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 5) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 6) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 7) {         
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               }
+                                               
+                                               if (tcp->tccps[compno].roishift) {
+                                                       int thresh, val, mag;
+                                                       thresh = 1 << tcp->tccps[compno].roishift;
+                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {
+                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                                                                               val = t1->data[k][j][i];
+                                                                               mag = int_abs(val);
+                                                                               if (mag >= thresh) {
+                                                                                       mag >>= tcp->tccps[compno].roishift;
+                                                                                       t1->data[k][j][i] = val < 0 ? -mag : mag;
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                               
+                                               if (tcp->tccps[compno].reversible == 1) {
+                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {
+                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                                                                               int tmp = t1->data[k][j][i];
+                                                                               tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp/2;
+                                                                       }
+                                                               }
+                                                       }
+                                               } else {                /* if (tcp->tccps[compno].reversible == 0) */
+                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {
+                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                                                                               double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0);
+                                                                               if (t1->data[k][j][i] >> 1 == 0) {
+                                                                                       tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0;
+                                                                               } else {
+                                                                                       int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
+                                                                                       tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp<0)?-tmp2:tmp2);
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       } /* cblkno */
+                               } /* precno */
+                       } /* bandno */
+               } /* resno */
+       } /* compno */
+}
+
+
+/** mod fixed_quality */
+double t1_getwmsedec(int nmsedec, int compno, int level[3], int orient, int bpno, double stepsize, int numcomps, int dwtid[3]) {
+       double w1, w2, wmsedec;
+       
+       if (dwtid[0] == 1 || dwtid[1] == 1 || dwtid[2] == 1) {
+               w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
+       } else {                        
+               w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
+       }
+       w2 = dwt_getnorm(orient, level, dwtid);
+
+       /*fprintf(stdout,"nmsedec %d level %d %d %d orient %d bpno %d stepsize %f \n",nmsedec ,level[0],level[1],level[2],orient,bpno,stepsize);*/
+       wmsedec = w1 * w2 * stepsize * (1 << bpno);
+       wmsedec *= wmsedec * nmsedec / 8192.0;
+       
+       return wmsedec;
+}
+/** mod fixed_quality */
index 1863484c41c22cc9ddb79f018dd37d5b0e6eef24..138f04c74d76ce64b1760630396ee8672e09eb07 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-#ifndef __T1_H\r
-#define __T1_H\r
-/**\r
-@file t1.h\r
-@brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1)\r
-\r
-The functions in T1.C have for goal to realize the tier-1 coding operation. The functions\r
-in T1.C are used by some function in TCD.C.\r
-*/\r
-\r
-/** @defgroup T1 T1 - Implementation of the tier-1 coding */\r
-/*@{*/\r
-\r
-/* ----------------------------------------------------------------------- */\r
-#define T1_NMSEDEC_BITS 7\r
-\r
-#define T1_MAXCBLKW 256        /*< Maximum size of code-block (width) */\r
-#define T1_MAXCBLKH 256        /*< Maximum size of code-block (heigth) */\r
-#define T1_MAXCBLKD 256        /*< Maximum size of code-block (depth) */\r
-#define T1_MINCBLKW 4          /*< Minimum size of code-block (width) */\r
-#define T1_MINCBLKH 4          /*< Minimum size of code-block (heigth) */\r
-#define T1_MINCBLKD 4          /*< Minimum size of code-block (depth) */\r
-#define T1_MAXWHD 18   \r
-#define T1_CBLKW 256\r
-#define T1_CBLKH 256\r
-#define T1_CBLKD 256\r
-\r
-#define T1_SIG_NE 0x0001       /*< Context orientation : North-East direction */\r
-#define T1_SIG_SE 0x0002       /*< Context orientation : South-East direction */\r
-#define T1_SIG_SW 0x0004       /*< Context orientation : South-West direction */\r
-#define T1_SIG_NW 0x0008       /*< Context orientation : North-West direction */\r
-#define T1_SIG_N 0x0010                /*< Context orientation : North direction */\r
-#define T1_SIG_E 0x0020                /*< Context orientation : East direction */\r
-#define T1_SIG_S 0x0040                /*< Context orientation : South direction */\r
-#define T1_SIG_W 0x0080                /*< Context orientation : West direction */\r
-#define T1_SIG_OTH (T1_SIG_N|T1_SIG_NE|T1_SIG_E|T1_SIG_SE|T1_SIG_S|T1_SIG_SW|T1_SIG_W|T1_SIG_NW)\r
-#define T1_SIG_PRIM (T1_SIG_N|T1_SIG_E|T1_SIG_S|T1_SIG_W)\r
-\r
-#define T1_SGN_N 0x0100\r
-#define T1_SGN_E 0x0200\r
-#define T1_SGN_S 0x0400\r
-#define T1_SGN_W 0x0800\r
-#define T1_SGN (T1_SGN_N|T1_SGN_E|T1_SGN_S|T1_SGN_W)\r
-\r
-#define T1_SIG 0x1000\r
-#define T1_REFINE 0x2000\r
-#define T1_VISIT 0x4000\r
-\r
-#define T1_NUMCTXS_AGG 1\r
-#define T1_NUMCTXS_ZC 9\r
-#define T1_NUMCTXS_MAG 3\r
-#define T1_NUMCTXS_SC 5\r
-#define T1_NUMCTXS_UNI 1\r
-\r
-#define T1_CTXNO_AGG 0\r
-#define T1_CTXNO_ZC (T1_CTXNO_AGG+T1_NUMCTXS_AGG)\r
-#define T1_CTXNO_MAG (T1_CTXNO_ZC+T1_NUMCTXS_ZC)\r
-#define T1_CTXNO_SC (T1_CTXNO_MAG+T1_NUMCTXS_MAG)\r
-#define T1_CTXNO_UNI (T1_CTXNO_SC+T1_NUMCTXS_SC)\r
-#define T1_NUMCTXS (T1_CTXNO_UNI+T1_NUMCTXS_UNI)\r
-\r
-#define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1)\r
-\r
-#define T1_TYPE_MQ 0   /*< Normal coding using entropy coder */\r
-#define T1_TYPE_RAW 1  /*< No encoding the information is store under raw format in codestream (mode switch RAW)*/\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/**\r
-Tier-1 coding (coding of code-block coefficients)\r
-*/\r
-typedef struct opj_t1 {\r
-       /** codec context */\r
-       opj_common_ptr cinfo;\r
-\r
-       /** MQC component */\r
-       opj_mqc_t *mqc;\r
-       /** RAW component */\r
-       opj_raw_t *raw;\r
-       /** LUTs for context-based coding */\r
-       int lut_ctxno_zc[1024];\r
-       int lut_ctxno_sc[256];\r
-       int lut_ctxno_mag[4096];\r
-       int lut_spb[256];\r
-       /** LUTs for decoding normalised MSE */\r
-       int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];\r
-       int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];\r
-       int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];\r
-       int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];\r
-       /** Codeblock data */\r
-       int data[T1_CBLKD][T1_CBLKH][T1_CBLKW];/*int ***data;*/\r
-       /** Context information for each voxel in codeblock */\r
-       int flags[T1_CBLKD + 2][T1_CBLKH + 2][T1_CBLKH + 2];/*int ***flags;*/\r
-} opj_t1_t;\r
-\r
-/** @name Exported functions */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Create a new T1 handle \r
-and initialize the look-up tables of the Tier-1 coder/decoder\r
-@return Returns a new T1 handle if successful, returns NULL otherwise\r
-@see t1_init_luts\r
-*/\r
-opj_t1_t* t1_create(opj_common_ptr cinfo);\r
-/**\r
-Destroy a previously created T1 handle\r
-@param t1 T1 handle to destroy\r
-*/\r
-void t1_destroy(opj_t1_t *t1);\r
-/**\r
-Encode the code-blocks of a tile\r
-@param t1 T1 handle\r
-@param tile The tile to encode\r
-@param tcp Tile coding parameters\r
-*/\r
-void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp);\r
-/**\r
-Decode the code-blocks of a tile\r
-@param t1 T1 handle\r
-@param tile The tile to decode\r
-@param tcp Tile coding parameters\r
-*/\r
-void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp);\r
-/**\r
-Get weigths of MSE decoding\r
-@param nmsedec The normalized MSE reduction\r
-@param compno \r
-@param level \r
-@param orient\r
-@param bpno\r
-@param stepsize\r
-@param numcomps\r
-@param dwtid\r
-returns MSE associated to decoding pass\r
-*/\r
-double t1_getwmsedec(int nmsedec, int compno, int level[3], int orient, int bpno, double stepsize, int numcomps, int dwtid[3]);\r
-\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __T1_H */\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __T1_H
+#define __T1_H
+/**
+@file t1.h
+@brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1)
+
+The functions in T1.C have for goal to realize the tier-1 coding operation. The functions
+in T1.C are used by some function in TCD.C.
+*/
+
+/** @defgroup T1 T1 - Implementation of the tier-1 coding */
+/*@{*/
+
+/* ----------------------------------------------------------------------- */
+#define T1_NMSEDEC_BITS 7
+
+#define T1_MAXCBLKW 256        /*< Maximum size of code-block (width) */
+#define T1_MAXCBLKH 256        /*< Maximum size of code-block (heigth) */
+#define T1_MAXCBLKD 256        /*< Maximum size of code-block (depth) */
+#define T1_MINCBLKW 4          /*< Minimum size of code-block (width) */
+#define T1_MINCBLKH 4          /*< Minimum size of code-block (heigth) */
+#define T1_MINCBLKD 4          /*< Minimum size of code-block (depth) */
+#define T1_MAXWHD 18   
+#define T1_CBLKW 256
+#define T1_CBLKH 256
+#define T1_CBLKD 256
+
+#define T1_SIG_NE 0x0001       /*< Context orientation : North-East direction */
+#define T1_SIG_SE 0x0002       /*< Context orientation : South-East direction */
+#define T1_SIG_SW 0x0004       /*< Context orientation : South-West direction */
+#define T1_SIG_NW 0x0008       /*< Context orientation : North-West direction */
+#define T1_SIG_N 0x0010                /*< Context orientation : North direction */
+#define T1_SIG_E 0x0020                /*< Context orientation : East direction */
+#define T1_SIG_S 0x0040                /*< Context orientation : South direction */
+#define T1_SIG_W 0x0080                /*< Context orientation : West direction */
+#define T1_SIG_OTH (T1_SIG_N|T1_SIG_NE|T1_SIG_E|T1_SIG_SE|T1_SIG_S|T1_SIG_SW|T1_SIG_W|T1_SIG_NW)
+#define T1_SIG_PRIM (T1_SIG_N|T1_SIG_E|T1_SIG_S|T1_SIG_W)
+
+#define T1_SGN_N 0x0100
+#define T1_SGN_E 0x0200
+#define T1_SGN_S 0x0400
+#define T1_SGN_W 0x0800
+#define T1_SGN (T1_SGN_N|T1_SGN_E|T1_SGN_S|T1_SGN_W)
+
+#define T1_SIG 0x1000
+#define T1_REFINE 0x2000
+#define T1_VISIT 0x4000
+
+#define T1_NUMCTXS_AGG 1
+#define T1_NUMCTXS_ZC 9
+#define T1_NUMCTXS_MAG 3
+#define T1_NUMCTXS_SC 5
+#define T1_NUMCTXS_UNI 1
+
+#define T1_CTXNO_AGG 0
+#define T1_CTXNO_ZC (T1_CTXNO_AGG+T1_NUMCTXS_AGG)
+#define T1_CTXNO_MAG (T1_CTXNO_ZC+T1_NUMCTXS_ZC)
+#define T1_CTXNO_SC (T1_CTXNO_MAG+T1_NUMCTXS_MAG)
+#define T1_CTXNO_UNI (T1_CTXNO_SC+T1_NUMCTXS_SC)
+#define T1_NUMCTXS (T1_CTXNO_UNI+T1_NUMCTXS_UNI)
+
+#define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1)
+
+#define T1_TYPE_MQ 0   /*< Normal coding using entropy coder */
+#define T1_TYPE_RAW 1  /*< No encoding the information is store under raw format in codestream (mode switch RAW)*/
+
+/* ----------------------------------------------------------------------- */
+
+/**
+Tier-1 coding (coding of code-block coefficients)
+*/
+typedef struct opj_t1 {
+       /** codec context */
+       opj_common_ptr cinfo;
+
+       /** MQC component */
+       opj_mqc_t *mqc;
+       /** RAW component */
+       opj_raw_t *raw;
+       /** LUTs for context-based coding */
+       int lut_ctxno_zc[1024];
+       int lut_ctxno_sc[256];
+       int lut_ctxno_mag[4096];
+       int lut_spb[256];
+       /** LUTs for decoding normalised MSE */
+       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];
+       /** Codeblock data */
+       int data[T1_CBLKD][T1_CBLKH][T1_CBLKW];/*int ***data;*/
+       /** Context information for each voxel in codeblock */
+       int flags[T1_CBLKD + 2][T1_CBLKH + 2][T1_CBLKH + 2];/*int ***flags;*/
+} opj_t1_t;
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a new T1 handle 
+and initialize the look-up tables of the Tier-1 coder/decoder
+@return Returns a new T1 handle if successful, returns NULL otherwise
+@see t1_init_luts
+*/
+opj_t1_t* t1_create(opj_common_ptr cinfo);
+/**
+Destroy a previously created T1 handle
+@param t1 T1 handle to destroy
+*/
+void t1_destroy(opj_t1_t *t1);
+/**
+Encode the code-blocks of a tile
+@param t1 T1 handle
+@param tile The tile to encode
+@param tcp Tile coding parameters
+*/
+void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp);
+/**
+Decode the code-blocks of a tile
+@param t1 T1 handle
+@param tile The tile to decode
+@param tcp Tile coding parameters
+*/
+void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp);
+/**
+Get weigths of MSE decoding
+@param nmsedec The normalized MSE reduction
+@param compno 
+@param level 
+@param orient
+@param bpno
+@param stepsize
+@param numcomps
+@param dwtid
+returns MSE associated to decoding pass
+*/
+double t1_getwmsedec(int nmsedec, int compno, int level[3], int orient, int bpno, double stepsize, int numcomps, int dwtid[3]);
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __T1_H */
index 2403824260f0ae63747ff7f2c450cd86cb665271..63332d21766ac52170eb8f65dbb8f3d7dff17817 100644 (file)
-/*\r
- * Copyrigth (c) 2006, M�nica D�ez, LPI-UVA, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/** @defgroup T1_3D T1_3D - Implementation of the tier-1 coding */\r
-/*@{*/\r
-\r
-/** @name Local static functions */\r
-/*@{*/\r
-\r
-static int t1_3d_getctxno_zc(unsigned int f, int orient);\r
-static int t1_3d_getctxno_sc(unsigned int f);\r
-static int t1_3d_getctxno_mag(unsigned int f, int fsvr);\r
-static int t1_3d_getspb(unsigned int f);\r
-static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos);\r
-static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos);\r
-static void t1_3d_updateflags(unsigned int *fp, int s);\r
-/**\r
-Encode significant pass\r
-*/\r
-static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc);\r
-/**\r
-Decode significant pass\r
-*/\r
-static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, char type, int vsc);\r
-/**\r
-Encode significant pass\r
-*/\r
-static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty);\r
-/**\r
-Decode significant pass\r
-*/\r
-static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty);\r
-/**\r
-Encode refinement pass\r
-*/\r
-static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);\r
-/**\r
-Decode refinement pass\r
-*/\r
-static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int poshalf, int neghalf, char type, int vsc);\r
-/**\r
-Encode refinement pass\r
-*/\r
-static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty);\r
-/**\r
-Decode refinement pass\r
-*/\r
-static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, char type, int cblksty);\r
-/**\r
-Encode clean-up pass\r
-*/\r
-static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc);\r
-/**\r
-Decode clean-up pass\r
-*/\r
-static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, int partial, int vsc);\r
-/**\r
-Encode clean-up pass\r
-*/\r
-static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty);\r
-/**\r
-Decode clean-up pass\r
-*/\r
-static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int cblksty);\r
-/**\r
-Encode 1 code-block\r
-@param t1 T1 handle\r
-@param cblk Code-block coding parameters\r
-@param orient\r
-@param compno Component number\r
-@param level[3]\r
-@param dwtid[3]\r
-@param stepsize\r
-@param cblksty Code-block style\r
-@param numcomps\r
-@param tile\r
-*/\r
-static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno,  int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile);\r
-/**\r
-Decode 1 code-block\r
-@param t1 T1 handle\r
-@param cblk Code-block coding parameters\r
-@param orient\r
-@param roishift Region of interest shifting value\r
-@param cblksty Code-block style\r
-*/\r
-static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);\r
-static int t1_3d_init_ctxno_zc(unsigned int f, int orient);\r
-static int t1_3d_init_ctxno_sc(unsigned int f);\r
-static int t1_3d_init_ctxno_mag(unsigned int f, int f2);\r
-static int t1_3d_init_spb(unsigned int f);\r
-/**\r
-Initialize the look-up tables of the Tier-1 coder/decoder\r
-@param t1 T1 handle\r
-*/\r
-static void t1_3d_init_luts(opj_t1_3d_t *t1);\r
-\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-static int t1_3d_getctxno_zc(unsigned int f, int orient) {\r
-       return t1_3d_init_ctxno_zc((f & T1_3D_SIG_OTH), orient);\r
-}\r
-\r
-static int t1_3d_getctxno_sc(unsigned int f) {\r
-       return t1_3d_init_ctxno_sc((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN));\r
-       /*return t1->lut_ctxno_sc[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4];*/\r
-}\r
-\r
-static int t1_3d_getctxno_mag(unsigned int f, int fsvr) {\r
-       return t1_3d_init_ctxno_mag((f & T1_3D_SIG_OTH), fsvr);\r
-}\r
-\r
-static int t1_3d_getspb(unsigned int f) {\r
-       return t1_3d_init_spb((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN));\r
-       /*return t1->lut_spb[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4];*/\r
-}\r
-\r
-static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos) {\r
-       if (bitpos > T1_NMSEDEC_FRACBITS) {\r
-               return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];\r
-       }\r
-       \r
-       return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];\r
-}\r
-\r
-static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos) {\r
-       if (bitpos > T1_NMSEDEC_FRACBITS) {\r
-               return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];\r
-       }\r
-\r
-    return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];\r
-}\r
-\r
-static void t1_3d_updateflags(unsigned int *fp, int s) {\r
-       unsigned int *np = fp - (T1_MAXCBLKW + 2);\r
-       unsigned int *sp = fp + (T1_MAXCBLKW + 2);\r
-\r
-       unsigned int *bwp = fp + ((T1_MAXCBLKW + 2)*(T1_MAXCBLKH +2));\r
-       unsigned int *bnp = bwp - (T1_MAXCBLKW + 2);\r
-       unsigned int *bsp = bwp + (T1_MAXCBLKW + 2);\r
-       \r
-       unsigned int *fwp = fp - ((T1_MAXCBLKW + 2)*(T1_MAXCBLKH +2));\r
-       unsigned int *fnp = fwp - (T1_MAXCBLKW + 2);\r
-       unsigned int *fsp = fwp + (T1_MAXCBLKW + 2);\r
-\r
-       np[-1] |= T1_3D_SIG_SE;\r
-       np[1] |= T1_3D_SIG_SW;\r
-       sp[-1] |= T1_3D_SIG_NE;\r
-       sp[1] |= T1_3D_SIG_NW;\r
-       *np |= T1_3D_SIG_S;\r
-       *sp |= T1_3D_SIG_N;\r
-       fp[-1] |= T1_3D_SIG_E;\r
-       fp[1] |= T1_3D_SIG_W;\r
-\r
-       *fwp |= T1_3D_SIG_FC;\r
-       *bwp |= T1_3D_SIG_BC;\r
-\r
-       fnp[-1] |= T1_3D_SIG_FSE;\r
-       fnp[1] |= T1_3D_SIG_FSW;\r
-       fsp[-1] |= T1_3D_SIG_FNE;\r
-       fsp[1] |= T1_3D_SIG_FNW;\r
-       *fnp |= T1_3D_SIG_FS;\r
-       *fsp |= T1_3D_SIG_FN;\r
-       fwp[-1] |= T1_3D_SIG_FE;\r
-       fwp[1] |= T1_3D_SIG_FW;\r
-\r
-       bnp[-1] |= T1_3D_SIG_BSE;\r
-       bnp[1] |= T1_3D_SIG_BSW;\r
-       bsp[-1] |= T1_3D_SIG_BNE;\r
-       bsp[1] |= T1_3D_SIG_BNW;\r
-       *bnp |= T1_3D_SIG_BS;\r
-       *bsp |= T1_3D_SIG_BN;\r
-       bwp[-1] |= T1_3D_SIG_BE;\r
-       bwp[1] |= T1_3D_SIG_BW;\r
-\r
-       if (s) {\r
-               *np |= (T1_3D_SGN_S << 16);\r
-               *sp |= (T1_3D_SGN_N << 16);\r
-               fp[-1] |= (T1_3D_SGN_E << 16);\r
-               fp[1] |= (T1_3D_SGN_W << 16);\r
-               *fwp |= (T1_3D_SGN_F << 16);\r
-               *bwp |= (T1_3D_SGN_B << 16);\r
-       }\r
-}\r
-\r
-static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) {\r
-       int v, flagsvr;\r
-       unsigned int flag;\r
-\r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);\r
-       flagsvr = (*fsvr);\r
-       if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {\r
-               v = int_abs(*dp) & one ? 1 : 0;\r
-               if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */\r
-                       mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));    /* ESSAI */\r
-                       mqc_bypass_enc(mqc, v);\r
-               } else {\r
-                       mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));\r
-                       mqc_encode(mqc, v);\r
-               }\r
-               if (v) {\r
-                       v = *dp < 0 ? 1 : 0;\r
-                       *nmsedec +=     t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);\r
-                       if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */\r
-                               mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));    /* ESSAI */\r
-                               mqc_bypass_enc(mqc, v);\r
-                       } else {\r
-                               mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));\r
-                               mqc_encode(mqc, v ^ t1_3d_getspb(flag));\r
-                       }\r
-                       t1_3d_updateflags(fp, v);\r
-                       *fsvr |= T1_3D_SIG;\r
-               }\r
-               *fsvr |= T1_3D_VISIT;\r
-       }\r
-}\r
-\r
-static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, char type, int vsc) {\r
-       int v, flagsvr;\r
-       unsigned int flag;\r
-       \r
-       opj_raw_t *raw = t1->raw;       /* RAW component */\r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);\r
-       flagsvr = (*fsvr);\r
-       if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {\r
-               if (type == T1_TYPE_RAW) {\r
-                       if (raw_decode(raw)) {\r
-                               v = raw_decode(raw);    /* ESSAI */\r
-                               *dp = v ? -oneplushalf : oneplushalf;\r
-                               t1_3d_updateflags(fp, v);\r
-                               *fsvr |= T1_3D_SIG;\r
-                       }\r
-               } else {\r
-                       mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));\r
-                       if (mqc_decode(mqc)) {\r
-                               mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));\r
-                               v = mqc_decode(mqc) ^ t1_3d_getspb(flag);\r
-                               *dp = v ? -oneplushalf : oneplushalf;\r
-                               t1_3d_updateflags(fp, v);\r
-                               *fsvr |= T1_3D_SIG;\r
-                       }\r
-               }\r
-               *fsvr |= T1_3D_VISIT;\r
-       }\r
-}                              /* VSC and  BYPASS by Antonin */\r
-\r
-static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty) {\r
-       int i, j, k, m, one, vsc;\r
-       *nmsedec = 0;\r
-       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);\r
-       for (m = 0; m < l; m++) {\r
-               for (k = 0; k < h; k += 4) {\r
-                       for (i = 0; i < w; i++) {\r
-                               for (j = k; j < k + 4 && j < h; j++) {\r
-                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                                       t1_3d_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, type, vsc);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-}\r
-\r
-static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty) {\r
-       int i, j, k, m, one, half, oneplushalf, vsc;\r
-       one = 1 << bpno;\r
-       half = one >> 1;\r
-       oneplushalf = one | half;\r
-       for (m = 0; m < l; m++) {\r
-               for (k = 0; k < h; k += 4) {\r
-                       for (i = 0; i < w; i++) {\r
-                               for (j = k; j < k + 4 && j < h; j++) {\r
-                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                                       t1_3d_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, type, vsc);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-}                              /* VSC and  BYPASS by Antonin */\r
-\r
-static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {\r
-       int v, flagsvr;\r
-       unsigned int flag;\r
-\r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);\r
-       flagsvr = (*fsvr);\r
-       if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) {\r
-               *nmsedec += t1_3d_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);\r
-               v = int_abs(*dp) & one ? 1 : 0;\r
-               if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */\r
-                       mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));  /* ESSAI */\r
-                       mqc_bypass_enc(mqc, v);\r
-               } else {\r
-                       mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));\r
-                       mqc_encode(mqc, v);\r
-               }\r
-               *fsvr |= T1_3D_REFINE;\r
-       }\r
-}\r
-\r
-static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int poshalf, int neghalf, char type, int vsc) {\r
-       int v, t, flagsvr;\r
-       unsigned int flag;\r
-\r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       opj_raw_t *raw = t1->raw;       /* RAW component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);\r
-       flagsvr = (*fsvr);\r
-       if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) {\r
-               if (type == T1_TYPE_RAW) {\r
-                       mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));  /* ESSAI */\r
-                       v = raw_decode(raw);\r
-               } else {\r
-                       mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));\r
-                       v = mqc_decode(mqc);\r
-               }\r
-               t = v ? poshalf : neghalf;\r
-               *dp += *dp < 0 ? -t : t;\r
-               *fsvr |= T1_3D_REFINE;\r
-       }\r
-}                              /* VSC and  BYPASS by Antonin  */\r
-\r
-static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty) {\r
-       int i, j, k, m, one, vsc;\r
-       *nmsedec = 0;\r
-       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);\r
-       for (m = 0; m < l; m++){\r
-               for (k = 0; k < h; k += 4) {\r
-                       for (i = 0; i < w; i++) {\r
-                               for (j = k; j < k + 4 && j < h; j++) {\r
-                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                                       t1_3d_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type, vsc);\r
-                               }\r
-                       }\r
-               }\r
-       }       \r
-}\r
-\r
-static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, char type, int cblksty) {\r
-       int i, j, k, m, one, poshalf, neghalf;\r
-       int vsc;\r
-       one = 1 << bpno;\r
-       poshalf = one >> 1;\r
-       neghalf = bpno > 0 ? -poshalf : -1;\r
-       for (m = 0; m < l; m++) {\r
-               for (k = 0; k < h; k += 4) {\r
-                       for (i = 0; i < w; i++) {\r
-                               for (j = k; j < k + 4 && j < h; j++) {\r
-                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                                       t1_3d_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type, vsc);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-}                              /* VSC and  BYPASS by Antonin */\r
-\r
-static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) {\r
-       int v, flagsvr;\r
-       unsigned int flag;\r
-\r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);\r
-       flagsvr = (*fsvr);\r
-       if (partial) {\r
-               goto LABEL_PARTIAL;\r
-       }\r
-       if (!(*fsvr & (T1_3D_SIG | T1_3D_VISIT))) {\r
-               mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));\r
-               v = int_abs(*dp) & one ? 1 : 0;\r
-               mqc_encode(mqc, v);\r
-               if (v) {\r
-LABEL_PARTIAL:\r
-                       *nmsedec += t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);\r
-                       mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));\r
-                       v = *dp < 0 ? 1 : 0;\r
-                       mqc_encode(mqc, v ^ t1_3d_getspb(flag));\r
-                       t1_3d_updateflags(fp, v);\r
-                       *fsvr |= T1_3D_SIG;\r
-               }\r
-       }\r
-       *fsvr &= ~T1_3D_VISIT;\r
-}\r
-\r
-static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, int partial, int vsc) {\r
-       int v, flagsvr;\r
-       unsigned int flag;\r
-\r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);\r
-       flagsvr = (*fsvr);\r
-       if (partial) {\r
-               goto LABEL_PARTIAL;\r
-       }\r
-       if (!(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {\r
-               mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));\r
-               if (mqc_decode(mqc)) {\r
-LABEL_PARTIAL:\r
-                       mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));\r
-                       v = mqc_decode(mqc) ^ t1_3d_getspb(flag);\r
-                       *dp = v ? -oneplushalf : oneplushalf;\r
-                       t1_3d_updateflags(fp, v);\r
-                       *fsvr |= T1_3D_SIG;\r
-               }\r
-       }\r
-       *fsvr &= ~T1_3D_VISIT;\r
-}                              /* VSC and  BYPASS by Antonin */\r
-\r
-static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty) {\r
-       int i, j, k, m, one, agg, runlen, vsc;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       *nmsedec = 0;\r
-       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);\r
-       for (m = 0; m < l; m++) {\r
-               for (k = 0; k < h; k += 4) {\r
-                       for (i = 0; i < w; i++) {\r
-                               if (k + 3 < h) {\r
-                                       if (cblksty & J3D_CCP_CBLKSTY_VSC) {\r
-                                               agg = !( ((t1->flagSVR[1 + m][1 + k][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))\r
-                                                       ||   ((t1->flagSVR[1 + m][1 + k + 1][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))\r
-                                                       ||   ((t1->flagSVR[1 + m][1 + k + 2][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))\r
-                                                       ||   ((t1->flagSVR[1 + m][1 + k + 3][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & ((t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH)))\r
-                                                       );\r
-                                       } else {\r
-                                               agg = !(\r
-                                                       ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))\r
-                                                       || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))\r
-                                                       || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))\r
-                                                       || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH))\r
-                                                       );\r
-                                       }\r
-                               } else {\r
-                                       agg = 0;\r
-                               }\r
-                               if (agg) {\r
-                                       for (runlen = 0; runlen < 4; runlen++) {\r
-                                               if (int_abs(t1->data[m][k + runlen][i]) & one)\r
-                                                       break;\r
-                                       }\r
-                                       mqc_setcurctx(mqc, T1_CTXNO_AGG);\r
-                                       mqc_encode(mqc, runlen != 4);\r
-                                       if (runlen == 4) {\r
-                                               continue;\r
-                                       }\r
-                                       mqc_setcurctx(mqc, T1_CTXNO_UNI);\r
-                                       mqc_encode(mqc, runlen >> 1);\r
-                                       mqc_encode(mqc, runlen & 1);\r
-                               } else {\r
-                                       runlen = 0;\r
-                               }\r
-                               for (j = k + runlen; j < k + 4 && j < h; j++) {\r
-                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                                       t1_3d_enc_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-}\r
-\r
-static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int cblksty) {\r
-       int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc;\r
-       int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       one = 1 << bpno;\r
-       half = one >> 1;\r
-       oneplushalf = one | half;\r
-       for (m = 0; m < l; m++) {\r
-               for (k = 0; k < h; k += 4) {\r
-                       for (i = 0; i < w; i++) {\r
-                               if (k + 3 < h) {\r
-                                       if (cblksty & J3D_CCP_CBLKSTY_VSC) {\r
-                                               agg = !(\r
-                                                       ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))\r
-                                                       || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))\r
-                                                       || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))\r
-                                                       || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | ((t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH)))\r
-                                                       );\r
-                                       } else {\r
-                                               agg = !(\r
-                                                       ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))\r
-                                                       || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))\r
-                                                       || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))\r
-                                                       || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH))\r
-                                                       );\r
-                                       }\r
-                               } else {\r
-                                       agg = 0;\r
-                               }\r
-                               if (agg) {\r
-                                       mqc_setcurctx(mqc, T1_CTXNO_AGG);\r
-                                       if (!mqc_decode(mqc)) {\r
-                                               continue;\r
-                                       }\r
-                                       mqc_setcurctx(mqc, T1_CTXNO_UNI);\r
-                                       runlen = mqc_decode(mqc);\r
-                                       runlen = (runlen << 1) | mqc_decode(mqc);\r
-                               } else {\r
-                                       runlen = 0;\r
-                               }\r
-                               for (j = k + runlen; j < k + 4 && j < h; j++) {\r
-                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;\r
-                                       t1_3d_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-       if (segsym) {\r
-               int v = 0;\r
-               mqc_setcurctx(mqc, T1_CTXNO_UNI);\r
-               v = mqc_decode(mqc);\r
-               v = (v << 1) | mqc_decode(mqc);\r
-               v = (v << 1) | mqc_decode(mqc);\r
-               v = (v << 1) | mqc_decode(mqc);\r
-               /*\r
-               if (v!=0xa) {\r
-                       opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);\r
-               } \r
-               */\r
-       }\r
-}                              /* VSC and  BYPASS by Antonin */\r
-\r
-\r
-static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {\r
-       int i, j, k;\r
-       int w, h, l;\r
-       int passno;\r
-       int bpno, passtype;\r
-       int max;\r
-       int nmsedec = 0;\r
-       double cumwmsedec = 0;\r
-       char type = T1_TYPE_MQ;\r
-       \r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       w = cblk->x1 - cblk->x0;\r
-       h = cblk->y1 - cblk->y0;\r
-       l = cblk->z1 - cblk->z0;\r
-\r
-       max = 0;\r
-       for (k = 0; k < l; k++) {\r
-               for (j = 0; j < h; j++) {\r
-                       for (i = 0; i < w; i++) {\r
-                               max = int_max(max, int_abs(t1->data[k][j][i]));\r
-                       }\r
-               }\r
-       }\r
-       for (k = 0; k <= l; k++) {\r
-               for (j = 0; j <= h; j++) {\r
-                       for (i = 0; i <= w; i++) {\r
-                               t1->flags[k][j][i] = 0;\r
-                               t1->flagSVR[k][j][i] = 0;\r
-                       }\r
-               }\r
-       }\r
-       \r
-       cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;\r
-       \r
-       bpno = cblk->numbps - 1;\r
-       passtype = 2;\r
-       \r
-       mqc_reset_enc(mqc);\r
-       mqc_init_enc(mqc, cblk->data);\r
-       \r
-       for (passno = 0; bpno >= 0; passno++) {\r
-               opj_tcd_pass_t *pass = &cblk->passes[passno];\r
-               int correction = 3;\r
-               double tmpwmsedec;\r
-               type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;\r
-               \r
-               switch (passtype) {\r
-                       case 0:\r
-                               t1_3d_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty);\r
-                               break;\r
-                       case 1:\r
-                               t1_3d_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty);\r
-                               break;\r
-                       case 2:\r
-                               t1_3d_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty);\r
-                               /* code switch SEGMARK (i.e. SEGSYM) */\r
-                               if (cblksty & J3D_CCP_CBLKSTY_SEGSYM)\r
-                                       mqc_segmark_enc(mqc);\r
-                               break;\r
-               }\r
-               \r
-               /* fixed_quality */\r
-               tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize, numcomps, dwtid);\r
-               cumwmsedec += tmpwmsedec;\r
-               tile->distotile += tmpwmsedec;\r
-               \r
-               /* Code switch "RESTART" (i.e. TERMALL) */\r
-               if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {\r
-                       if (type == T1_TYPE_RAW) {\r
-                               mqc_flush(mqc);\r
-                               correction = 1;\r
-                               /* correction = mqc_bypass_flush_enc(); */\r
-                       } else {                        /* correction = mqc_restart_enc(); */\r
-                               mqc_flush(mqc);\r
-                               correction = 1;\r
-                       }\r
-                       pass->term = 1;\r
-               } else {\r
-                       if (((bpno < (cblk->numbps - 4) && (passtype > 0)) \r
-                               || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) {\r
-                               if (type == T1_TYPE_RAW) {\r
-                                       mqc_flush(mqc);\r
-                                       correction = 1;\r
-                               } else {        \r
-                                       mqc_flush(mqc);\r
-                                       correction = 1;\r
-                               }\r
-                               pass->term = 1;\r
-                       } else {\r
-                               pass->term = 0;\r
-                       }\r
-               }\r
-               \r
-               if (++passtype == 3) {\r
-                       passtype = 0;\r
-                       bpno--;\r
-               }\r
-               \r
-               if (pass->term && bpno > 0) {\r
-                       type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;\r
-                       if (type == T1_TYPE_RAW)\r
-                               mqc_bypass_init_enc(mqc);\r
-                       else\r
-                               mqc_restart_init_enc(mqc);\r
-               }\r
-               \r
-               pass->distortiondec = cumwmsedec;\r
-               pass->rate = mqc_numbytes(mqc) + correction;    /* FIXME */\r
-               pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);\r
-\r
-               /* Code-switch "RESET" */\r
-               if (cblksty & J3D_CCP_CBLKSTY_RESET)\r
-                       mqc_reset_enc(mqc);\r
-       }\r
-       \r
-       /* Code switch "ERTERM" (i.e. PTERM) */\r
-       if (cblksty & J3D_CCP_CBLKSTY_PTERM)\r
-               mqc_erterm_enc(mqc);\r
-       else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY))\r
-               mqc_flush(mqc);\r
-       \r
-       cblk->totalpasses = passno;\r
-}\r
-\r
-static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {\r
-       int i, j, k;\r
-       int w, h, l;\r
-       int bpno, passtype;\r
-       int segno, passno;\r
-       char type = T1_TYPE_MQ; /* BYPASS mode */\r
-       opj_raw_t *raw = t1->raw;       /* RAW component */\r
-       opj_mqc_t *mqc = t1->mqc;       /* MQC component */\r
-       \r
-       w = cblk->x1 - cblk->x0;\r
-       h = cblk->y1 - cblk->y0;\r
-       l = cblk->z1 - cblk->z0;\r
-\r
-    for (k = 0; k < l; k++) {\r
-               for (j = 0; j < h; j++) {\r
-                       for (i = 0; i < w; i++) {\r
-                               t1->data[k][j][i] = 0;\r
-                       }\r
-               }\r
-       }\r
-       \r
-       for (k = 0; k <= l; k++) {\r
-               for (j = 0; j <= h; j++) {\r
-                       for (i = 0; i <= w; i++) {\r
-                               t1->flags[k][j][i] = 0;\r
-                               t1->flagSVR[k][j][i] = 0;\r
-                       }\r
-               }\r
-       }\r
-\r
-       \r
-       bpno = roishift + cblk->numbps - 1;\r
-       passtype = 2;\r
-       \r
-       mqc_reset_enc(mqc);\r
-       \r
-       for (segno = 0; segno < cblk->numsegs; segno++) {\r
-               opj_tcd_seg_t *seg = &cblk->segs[segno];\r
-               \r
-               /* BYPASS mode */\r
-               type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;\r
-               if (type == T1_TYPE_RAW) {\r
-                       raw_init_dec(raw, seg->data, seg->len);\r
-               } else {\r
-                       mqc_init_dec(mqc, seg->data, seg->len);\r
-               }\r
-               \r
-               for (passno = 0; passno < seg->numpasses; passno++) {\r
-                       switch (passtype) {\r
-                               case 0:\r
-                                       t1_3d_dec_sigpass(t1, w, h, l, bpno+1, orient, type, cblksty);\r
-                                       break;\r
-                               case 1:\r
-                                       t1_3d_dec_refpass(t1, w, h, l, bpno+1, type, cblksty);\r
-                                       break;\r
-                               case 2:\r
-                                       t1_3d_dec_clnpass(t1, w, h, l, bpno+1, orient, cblksty);\r
-                                       break;\r
-                       }\r
-                       \r
-                       if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {\r
-                               mqc_reset_enc(mqc);\r
-                       }\r
-                       if (++passtype == 3) {\r
-                               passtype = 0;\r
-                               bpno--;\r
-                       }\r
-\r
-               }\r
-       }\r
-}\r
-\r
-static int t1_3d_init_ctxno_zc(unsigned int f, int orient) {\r
-       unsigned int h, v, c;\r
-       unsigned int d2xy, d2xz, d2yz, d3;\r
-       int n;\r
-       unsigned int hvc, hc, d2, d2xy2yz, d2xy2xz;\r
-       n = 0;\r
-       h = ((f & T1_3D_SIG_W) != 0) + ((f & T1_3D_SIG_E) != 0);\r
-       v = ((f & T1_3D_SIG_N) != 0) + ((f & T1_3D_SIG_S) != 0);\r
-       c = ((f & T1_3D_SIG_FC) != 0) + ((f & T1_3D_SIG_BC) != 0);\r
-       d2xy = ((f & T1_3D_SIG_NW) != 0) + ((f & T1_3D_SIG_NE) != 0) + ((f & T1_3D_SIG_SE) != 0) + ((f & T1_3D_SIG_SW) != 0);\r
-       d2xz = ((f & T1_3D_SIG_FW) != 0) + ((f & T1_3D_SIG_BW) != 0) + ((f & T1_3D_SIG_FE) != 0) + ((f & T1_3D_SIG_BE) != 0);\r
-       d2yz = ((f & T1_3D_SIG_FN) != 0) + ((f & T1_3D_SIG_FS) != 0) + ((f & T1_3D_SIG_BN) != 0) + ((f & T1_3D_SIG_BS) != 0);\r
-    d3 = ((f & T1_3D_SIG_FNW) != 0) + ((f & T1_3D_SIG_FNE) != 0) + ((f & T1_3D_SIG_FSE) != 0) + ((f & T1_3D_SIG_FSW) != 0) \r
-               + ((f & T1_3D_SIG_BNW) != 0) + ((f & T1_3D_SIG_BNE) != 0) + ((f & T1_3D_SIG_BSE) != 0) + ((f & T1_3D_SIG_BSW) != 0);\r
-       \r
-       switch (orient) {\r
-               case 0: /*LLL*/\r
-               case 7: /*HHH*/\r
-                       hvc = h + v + c;\r
-                       d2 = d2xy + d2xz + d2yz;\r
-                       if (!hvc) {\r
-                               if (!d2) {\r
-                    n = (!d3) ? 0 : 1;\r
-                               } else if (d2 == 1) {\r
-                                       n = (!d3) ? 2 : 3;\r
-                               } else {\r
-                                       n = (!d3) ? 4 : 5;\r
-                               }\r
-                       } else if (hvc == 1) {\r
-                               if (!d2) {\r
-                    n = (!d3) ? 6 : 7;\r
-                               } else if (d2 == 1) {\r
-                                       n = (!d3) ? 8 : 9;\r
-                               } else {\r
-                                       n = 10;\r
-                               }\r
-                       } else if (hvc == 2) {\r
-                               if (!d2) {\r
-                    n = (!d3) ? 11 : 12;\r
-                               } else {\r
-                                       n = 13;\r
-                               }\r
-                       } else if (hvc == 3) {\r
-                               n = 14;\r
-                       } else {\r
-                               n = 15;\r
-                       }\r
-                       break;\r
-               /*LHL, HLL, LLH*/\r
-               case 1:\r
-               case 2:\r
-               case 4:\r
-                       hc = h + c;\r
-                       d2xy2yz = d2xy + d2yz;\r
-            if (!hc) {\r
-                               if (!v) {\r
-                                       if (!d2xy) {\r
-                                               n = (!d2xy2yz) ? ((!d3) ? 0 : 1) : ((!d3) ? 2 : 3);     \r
-                                       } else if (d2xy == 1) {\r
-                                               n = (!d2xy2yz) ? ((!d3) ? 4 : 5) : 6;   \r
-                                       } else { /*>=2*/\r
-                        n = 7;\r
-                                       }\r
-                               } else {\r
-                                       n = (v == 1) ? 8 : 9; /* =1 or =2*/\r
-                               } \r
-                       } else if (hc == 1) {\r
-                               n = (!v) ? ( (!d2xy) ? ( (!d2xy2yz) ? ( (!d3) ? 10 : 11) : (12) ) : (13) ) : (14);\r
-                       } else { /*if (hc >= 2)*/\r
-                               n = 15;\r
-                       }\r
-                       break;\r
-               /*HLH, HHL, LHH*/\r
-               case 3:\r
-               case 5:\r
-               case 6:\r
-                       hc = h + c;\r
-                       d2xy2xz = d2xy + d2xz;\r
-                       if (!v) {\r
-                               if (!d2xz) {\r
-                                       if (!hc && !d2xy2xz) {\r
-                                               n = (!d3) ? 0 : 1;\r
-                                       } else if (hc == 1) {\r
-                                               n = (!d2xy2xz) ?  2 : 3;\r
-                                       } else { /*if >= 2*/\r
-                                               n = 4;\r
-                                       }\r
-                               } else if ( d2xz>=1 && !hc ) {\r
-                                       n = 5;\r
-                               } else if ( hc>=1 ) {\r
-                                       n = (d2xz==1) ? 6 : 7;\r
-                               } \r
-                       } else if (v == 1) {\r
-                               if (!d2xz) {\r
-                                       n = (!hc) ? 8 : 9;\r
-                               } else if (d2xz == 1) {\r
-                                       n = (!hc) ? 10 : 11;\r
-                               } else if (d2xz == 2) {\r
-                                       n = (!hc) ? 12 : 13;\r
-                               } else { /* if (d2xz >= 3) {*/\r
-                                       n = 14;\r
-                               }\r
-                       } else if (v == 2) {\r
-                               n = 15;\r
-                       } \r
-                       break;\r
-       }\r
-       \r
-       return (T1_3D_CTXNO_ZC + n);\r
-}\r
-\r
-static int t1_3d_init_ctxno_sc(unsigned int f) {\r
-       int hc, vc, cc;\r
-       int n = 0;\r
-\r
-       hc = int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E ) \r
-                                       + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W ) , 1) \r
-               - int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E) ) \r
-                                       + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W) ) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1);\r
-       \r
-       vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N) \r
-                                       + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1) \r
-               - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N)) \r
-                                       + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1);\r
-       \r
-       cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC) \r
-                                       + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1) \r
-               - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F)) \r
-                                       + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1);\r
-       if (hc < 0) {\r
-               hc = -hc;\r
-               vc = -vc;\r
-               cc = -cc;\r
-       }\r
-\r
-       if (!hc) {\r
-               if (!vc) \r
-                       n = (!cc) ? 0 : 1;\r
-               else if (vc == -1)\r
-                       n = (!cc) ? 1 : ( (cc>0) ? 2 : 4);\r
-               else if (vc == 1)\r
-                       n = (!cc) ? 1 : ( (cc<0) ? 2 : 4);\r
-       } else if (hc == 1) {\r
-               if (!vc)\r
-                       n = (!cc) ? 1 : ( (cc<0) ? 2 : 4);\r
-               else if (vc == 1)\r
-                       n = (!cc) ? 4 : ( (cc>0) ? 5 : 3);\r
-               else if (vc == -1)\r
-                       n = (!cc) ? 2 : 3;\r
-       } else if (hc == -1) {\r
-               if (!vc)\r
-                       n = (!cc) ? 1 : ( (cc>0) ? 2 : 4);\r
-               else if (vc == 1)\r
-                       n = (!cc) ? 2 : 3;\r
-               else if (vc == -1)\r
-                       n = (!cc) ? 4 : ( (cc<0) ? 5 : 3);\r
-       }\r
-       \r
-       return (T1_3D_CTXNO_SC + n);\r
-}\r
-\r
-static int t1_3d_init_ctxno_mag(unsigned int f, int f2) {\r
-       int n;\r
-       if (!(f2 & T1_3D_REFINE))       /*First refinement for this coefficient (no previous refinement)*/\r
-               n = (f & (T1_3D_SIG_PRIM)) ? 1 : 0;\r
-       else\r
-               n = 2;\r
-       \r
-       return (T1_3D_CTXNO_MAG + n);\r
-}\r
-\r
-static int t1_3d_init_spb(unsigned int f) {\r
-       int hc, vc, cc;\r
-       int n = 0;\r
-       \r
-       hc = int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E ) \r
-                                       + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W ) , 1) \r
-               - int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E) ) \r
-                                       + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W) ) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1);\r
-       \r
-       vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N) \r
-                                       + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1) \r
-               - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N)) \r
-                                       + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1);\r
-       \r
-       cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC) \r
-                                       + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1) \r
-               - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F)) \r
-                                       + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1);\r
-       \r
-       n = ((hc + vc + cc) < 0); \r
-       \r
-       return n;\r
-}\r
-\r
-static void t1_3d_init_luts(opj_t1_3d_t *t1) {\r
-       int i;\r
-       double u, v, t;\r
-       /*for (j = 0; j < 4; j++) {\r
-               for (i = 0; i < 256; ++i) {\r
-                       t1->lut_ctxno_zc[(j << 8) | i] = t1_3d_init_ctxno_zc(i, j);\r
-               }\r
-       }\r
-       for (i = 0; i < 4096; i++) {\r
-               t1->lut_ctxno_sc[i] = t1_3d_init_ctxno_sc(i << 4);\r
-       }\r
-       for (j = 0; j < 2; j++) {\r
-               for (i = 0; i < 2048; ++i) {\r
-                       t1->lut_ctxno_mag[(j << 11) + i] = t1_3d_init_ctxno_mag((j ? T1_3D_REFINE : 0) | i);\r
-               }\r
-       }\r
-       for (i = 0; i < 4096; ++i) {\r
-               t1->lut_spb[i] = t1_3d_init_spb(i << 4);\r
-       }*/\r
-       /* FIXME FIXME FIXME */\r
-       for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {\r
-               t = i / pow(2, T1_NMSEDEC_FRACBITS);\r
-               u = t;\r
-               v = t - 1.5;\r
-               t1->lut_nmsedec_sig[i] = \r
-                       int_max(0, \r
-                       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
-               t1->lut_nmsedec_sig0[i] =\r
-                       int_max(0,\r
-                       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
-               u = t - 1.0;\r
-               if (i & (1 << (T1_NMSEDEC_BITS - 1))) {\r
-                       v = t - 1.5;\r
-               } else {\r
-                       v = t - 0.5;\r
-               }\r
-               t1->lut_nmsedec_ref[i] =\r
-                       int_max(0,\r
-                       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
-               t1->lut_nmsedec_ref0[i] =\r
-                       int_max(0,\r
-                       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));\r
-       }\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-opj_t1_3d_t* t1_3d_create(opj_common_ptr cinfo) {\r
-       opj_t1_3d_t *t1 = (opj_t1_3d_t*)opj_malloc(sizeof(opj_t1_3d_t));\r
-       if(t1) {\r
-               t1->cinfo = cinfo;\r
-               /* create MQC and RAW handles */\r
-               t1->mqc = mqc_create();\r
-               t1->raw = raw_create();\r
-               /* initialize the look-up tables of the Tier-1 coder/decoder */\r
-               t1_3d_init_luts(t1);\r
-       }\r
-       return t1;\r
-}\r
-\r
-void t1_3d_destroy(opj_t1_3d_t *t1) {\r
-       if(t1) {\r
-               /* destroy MQC and RAW handles */\r
-               mqc_destroy(t1->mqc);\r
-               raw_destroy(t1->raw);\r
-               opj_free(t1);\r
-       }\r
-}\r
-\r
-void t1_3d_encode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {\r
-       int compno, resno, bandno, precno, cblkno;\r
-       int x, y, z, i, j, k, orient;\r
-       int level[3];\r
-       tile->distotile = 0;            /* fixed_quality */\r
-\r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-\r
-               for (resno = 0; resno < tilec->numresolution[0]; resno++) {\r
-                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-\r
-                       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                               opj_tcd_band_t *band = &res->bands[bandno];\r
-\r
-                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {\r
-                                       opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-\r
-                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-\r
-                                               if (band->bandno == 0) {\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 1) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 2) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 3) {         \r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 4) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 5) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 6) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 7) {         \r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               }\r
-\r
-                                               if (tcp->tccps[compno].reversible == 1) {\r
-                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {\r
-                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
-                                    for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
-                                        t1->data[k][j][i] = \r
-                                                                               tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS;\r
-                                                                       }\r
-                                                               }\r
-                                                       }\r
-                                               } else if (tcp->tccps[compno].reversible == 0) {\r
-                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {\r
-                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
-                                    for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
-                                        t1->data[k][j][i] = fix_mul(\r
-                                                                               tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)],\r
-                                                                               8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);\r
-                                                                       }\r
-                                                               }\r
-                                                       }\r
-                                               }\r
-                                               orient = band->bandno;  /* FIXME */\r
-                                               for (i = 0; i < 3; i++) \r
-                                                       level[i] = tilec->numresolution[i] - 1 - resno;\r
-\r
-                                               t1_3d_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);\r
-                                                       \r
-                                       } /* cblkno */\r
-                               } /* precno */\r
-                       } /* bandno */\r
-               } /* resno  */\r
-       } /* compno  */\r
-}\r
-\r
-void t1_3d_decode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {\r
-       int compno, resno, bandno, precno, cblkno;\r
-       \r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-\r
-               for (resno = 0; resno < tilec->numresolution[0]; resno++) {\r
-                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-\r
-                       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                               opj_tcd_band_t *band = &res->bands[bandno];\r
-\r
-                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {\r
-                                       opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-\r
-                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                                               int x, y, z, i, j, k, orient;\r
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-\r
-                                               orient = band->bandno;  /* FIXME */\r
-\r
-                                               /*fprintf(stdout,"[INFO] t1_3d_decode_cblk(t1, cblk, orient(%d), tcp->tccps[compno].roishift (%d), tcp->tccps[compno].cblksty (%d));\n",orient,tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);*/\r
-                                               t1_3d_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);\r
-\r
-                                               if (band->bandno == 0) {\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 1) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 2) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 3) {         \r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 4) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 5) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 6) {\r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               } else if (band->bandno == 7) {         \r
-                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];\r
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;\r
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;\r
-                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;\r
-                                               }\r
-                                       \r
-                                               if (tcp->tccps[compno].roishift) {\r
-                                                       int thresh, val, mag;\r
-                                                       thresh = 1 << tcp->tccps[compno].roishift;\r
-                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {\r
-                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
-                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
-                                                                               val = t1->data[k][j][i];\r
-                                                                               mag = int_abs(val);\r
-                                                                               if (mag >= thresh) {\r
-                                                                                       mag >>= tcp->tccps[compno].roishift;\r
-                                                                                       t1->data[k][j][i] = val < 0 ? -mag : mag;\r
-                                                                               }\r
-                                                                       }\r
-                                                               }\r
-                                                       }\r
-                                               }\r
-                                               \r
-                                               if (tcp->tccps[compno].reversible == 1) {\r
-                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {\r
-                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
-                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
-                                                                               int tmp = t1->data[k][j][i];\r
-                                                                               tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp/2;\r
-                                                                       }\r
-                                                               }\r
-                                                       }\r
-                                               } else {                /* if (tcp->tccps[compno].reversible == 0) */\r
-                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {\r
-                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {\r
-                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {\r
-                                                                               double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0);\r
-                                                                               if (t1->data[k][j][i] >> 1 == 0) {\r
-                                                                                       tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0;\r
-                                                                               } else {\r
-                                                                                       int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);\r
-                                                                                       tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp<0)?-tmp2:tmp2);\r
-                                                                               }\r
-                                                                       }\r
-                                                               }\r
-                                                       }\r
-                                               }\r
-                                       } /* cblkno */\r
-                               } /* precno */\r
-                       } /* bandno */\r
-               } /* resno */\r
-       } /* compno */\r
-}\r
+/*
+ * Copyrigth (c) 2006, M�nica D�ez, LPI-UVA, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup T1_3D T1_3D - Implementation of the tier-1 coding */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+static int t1_3d_getctxno_zc(unsigned int f, int orient);
+static int t1_3d_getctxno_sc(unsigned int f);
+static int t1_3d_getctxno_mag(unsigned int f, int fsvr);
+static int t1_3d_getspb(unsigned int f);
+static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos);
+static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos);
+static void t1_3d_updateflags(unsigned int *fp, int s);
+/**
+Encode significant pass
+*/
+static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc);
+/**
+Decode significant pass
+*/
+static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, char type, int vsc);
+/**
+Encode significant pass
+*/
+static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty);
+/**
+Decode significant pass
+*/
+static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty);
+/**
+Encode refinement pass
+*/
+static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
+/**
+Decode refinement pass
+*/
+static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int poshalf, int neghalf, char type, int vsc);
+/**
+Encode refinement pass
+*/
+static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty);
+/**
+Decode refinement pass
+*/
+static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, char type, int cblksty);
+/**
+Encode clean-up pass
+*/
+static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc);
+/**
+Decode clean-up pass
+*/
+static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, int partial, int vsc);
+/**
+Encode clean-up pass
+*/
+static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty);
+/**
+Decode clean-up pass
+*/
+static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int cblksty);
+/**
+Encode 1 code-block
+@param t1 T1 handle
+@param cblk Code-block coding parameters
+@param orient
+@param compno Component number
+@param level[3]
+@param dwtid[3]
+@param stepsize
+@param cblksty Code-block style
+@param numcomps
+@param tile
+*/
+static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno,  int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile);
+/**
+Decode 1 code-block
+@param t1 T1 handle
+@param cblk Code-block coding parameters
+@param orient
+@param roishift Region of interest shifting value
+@param cblksty Code-block style
+*/
+static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
+static int t1_3d_init_ctxno_zc(unsigned int f, int orient);
+static int t1_3d_init_ctxno_sc(unsigned int f);
+static int t1_3d_init_ctxno_mag(unsigned int f, int f2);
+static int t1_3d_init_spb(unsigned int f);
+/**
+Initialize the look-up tables of the Tier-1 coder/decoder
+@param t1 T1 handle
+*/
+static void t1_3d_init_luts(opj_t1_3d_t *t1);
+
+/*@}*/
+
+/*@}*/
+
+/* ----------------------------------------------------------------------- */
+
+static int t1_3d_getctxno_zc(unsigned int f, int orient) {
+       return t1_3d_init_ctxno_zc((f & T1_3D_SIG_OTH), orient);
+}
+
+static int t1_3d_getctxno_sc(unsigned int f) {
+       return t1_3d_init_ctxno_sc((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN));
+       /*return t1->lut_ctxno_sc[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4];*/
+}
+
+static int t1_3d_getctxno_mag(unsigned int f, int fsvr) {
+       return t1_3d_init_ctxno_mag((f & T1_3D_SIG_OTH), fsvr);
+}
+
+static int t1_3d_getspb(unsigned int f) {
+       return t1_3d_init_spb((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN));
+       /*return t1->lut_spb[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4];*/
+}
+
+static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos) {
+       if (bitpos > T1_NMSEDEC_FRACBITS) {
+               return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
+       }
+       
+       return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
+}
+
+static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos) {
+       if (bitpos > T1_NMSEDEC_FRACBITS) {
+               return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
+       }
+
+    return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
+}
+
+static void t1_3d_updateflags(unsigned int *fp, int s) {
+       unsigned int *np = fp - (T1_MAXCBLKW + 2);
+       unsigned int *sp = fp + (T1_MAXCBLKW + 2);
+
+       unsigned int *bwp = fp + ((T1_MAXCBLKW + 2)*(T1_MAXCBLKH +2));
+       unsigned int *bnp = bwp - (T1_MAXCBLKW + 2);
+       unsigned int *bsp = bwp + (T1_MAXCBLKW + 2);
+       
+       unsigned int *fwp = fp - ((T1_MAXCBLKW + 2)*(T1_MAXCBLKH +2));
+       unsigned int *fnp = fwp - (T1_MAXCBLKW + 2);
+       unsigned int *fsp = fwp + (T1_MAXCBLKW + 2);
+
+       np[-1] |= T1_3D_SIG_SE;
+       np[1] |= T1_3D_SIG_SW;
+       sp[-1] |= T1_3D_SIG_NE;
+       sp[1] |= T1_3D_SIG_NW;
+       *np |= T1_3D_SIG_S;
+       *sp |= T1_3D_SIG_N;
+       fp[-1] |= T1_3D_SIG_E;
+       fp[1] |= T1_3D_SIG_W;
+
+       *fwp |= T1_3D_SIG_FC;
+       *bwp |= T1_3D_SIG_BC;
+
+       fnp[-1] |= T1_3D_SIG_FSE;
+       fnp[1] |= T1_3D_SIG_FSW;
+       fsp[-1] |= T1_3D_SIG_FNE;
+       fsp[1] |= T1_3D_SIG_FNW;
+       *fnp |= T1_3D_SIG_FS;
+       *fsp |= T1_3D_SIG_FN;
+       fwp[-1] |= T1_3D_SIG_FE;
+       fwp[1] |= T1_3D_SIG_FW;
+
+       bnp[-1] |= T1_3D_SIG_BSE;
+       bnp[1] |= T1_3D_SIG_BSW;
+       bsp[-1] |= T1_3D_SIG_BNE;
+       bsp[1] |= T1_3D_SIG_BNW;
+       *bnp |= T1_3D_SIG_BS;
+       *bsp |= T1_3D_SIG_BN;
+       bwp[-1] |= T1_3D_SIG_BE;
+       bwp[1] |= T1_3D_SIG_BW;
+
+       if (s) {
+               *np |= (T1_3D_SGN_S << 16);
+               *sp |= (T1_3D_SGN_N << 16);
+               fp[-1] |= (T1_3D_SGN_E << 16);
+               fp[1] |= (T1_3D_SGN_W << 16);
+               *fwp |= (T1_3D_SGN_F << 16);
+               *bwp |= (T1_3D_SGN_B << 16);
+       }
+}
+
+static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) {
+       int v, flagsvr;
+       unsigned int flag;
+
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
+       flagsvr = (*fsvr);
+       if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
+               v = int_abs(*dp) & one ? 1 : 0;
+               if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
+                       mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));    /* ESSAI */
+                       mqc_bypass_enc(mqc, v);
+               } else {
+                       mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
+                       mqc_encode(mqc, v);
+               }
+               if (v) {
+                       v = *dp < 0 ? 1 : 0;
+                       *nmsedec +=     t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
+                       if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
+                               mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));    /* ESSAI */
+                               mqc_bypass_enc(mqc, v);
+                       } else {
+                               mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
+                               mqc_encode(mqc, v ^ t1_3d_getspb(flag));
+                       }
+                       t1_3d_updateflags(fp, v);
+                       *fsvr |= T1_3D_SIG;
+               }
+               *fsvr |= T1_3D_VISIT;
+       }
+}
+
+static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, char type, int vsc) {
+       int v, flagsvr;
+       unsigned int flag;
+       
+       opj_raw_t *raw = t1->raw;       /* RAW component */
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
+       flagsvr = (*fsvr);
+       if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
+               if (type == T1_TYPE_RAW) {
+                       if (raw_decode(raw)) {
+                               v = raw_decode(raw);    /* ESSAI */
+                               *dp = v ? -oneplushalf : oneplushalf;
+                               t1_3d_updateflags(fp, v);
+                               *fsvr |= T1_3D_SIG;
+                       }
+               } else {
+                       mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
+                       if (mqc_decode(mqc)) {
+                               mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
+                               v = mqc_decode(mqc) ^ t1_3d_getspb(flag);
+                               *dp = v ? -oneplushalf : oneplushalf;
+                               t1_3d_updateflags(fp, v);
+                               *fsvr |= T1_3D_SIG;
+                       }
+               }
+               *fsvr |= T1_3D_VISIT;
+       }
+}                              /* VSC and  BYPASS by Antonin */
+
+static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty) {
+       int i, j, k, m, one, vsc;
+       *nmsedec = 0;
+       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
+       for (m = 0; m < l; m++) {
+               for (k = 0; k < h; k += 4) {
+                       for (i = 0; i < w; i++) {
+                               for (j = k; j < k + 4 && j < h; j++) {
+                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                                       t1_3d_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, type, vsc);
+                               }
+                       }
+               }
+       }
+}
+
+static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty) {
+       int i, j, k, m, one, half, oneplushalf, vsc;
+       one = 1 << bpno;
+       half = one >> 1;
+       oneplushalf = one | half;
+       for (m = 0; m < l; m++) {
+               for (k = 0; k < h; k += 4) {
+                       for (i = 0; i < w; i++) {
+                               for (j = k; j < k + 4 && j < h; j++) {
+                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                                       t1_3d_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, type, vsc);
+                               }
+                       }
+               }
+       }
+}                              /* VSC and  BYPASS by Antonin */
+
+static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
+       int v, flagsvr;
+       unsigned int flag;
+
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
+       flagsvr = (*fsvr);
+       if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) {
+               *nmsedec += t1_3d_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
+               v = int_abs(*dp) & one ? 1 : 0;
+               if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
+                       mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));  /* ESSAI */
+                       mqc_bypass_enc(mqc, v);
+               } else {
+                       mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));
+                       mqc_encode(mqc, v);
+               }
+               *fsvr |= T1_3D_REFINE;
+       }
+}
+
+static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int poshalf, int neghalf, char type, int vsc) {
+       int v, t, flagsvr;
+       unsigned int flag;
+
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       opj_raw_t *raw = t1->raw;       /* RAW component */
+       
+       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
+       flagsvr = (*fsvr);
+       if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) {
+               if (type == T1_TYPE_RAW) {
+                       mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));  /* ESSAI */
+                       v = raw_decode(raw);
+               } else {
+                       mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));
+                       v = mqc_decode(mqc);
+               }
+               t = v ? poshalf : neghalf;
+               *dp += *dp < 0 ? -t : t;
+               *fsvr |= T1_3D_REFINE;
+       }
+}                              /* VSC and  BYPASS by Antonin  */
+
+static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty) {
+       int i, j, k, m, one, vsc;
+       *nmsedec = 0;
+       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
+       for (m = 0; m < l; m++){
+               for (k = 0; k < h; k += 4) {
+                       for (i = 0; i < w; i++) {
+                               for (j = k; j < k + 4 && j < h; j++) {
+                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                                       t1_3d_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type, vsc);
+                               }
+                       }
+               }
+       }       
+}
+
+static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, char type, int cblksty) {
+       int i, j, k, m, one, poshalf, neghalf;
+       int vsc;
+       one = 1 << bpno;
+       poshalf = one >> 1;
+       neghalf = bpno > 0 ? -poshalf : -1;
+       for (m = 0; m < l; m++) {
+               for (k = 0; k < h; k += 4) {
+                       for (i = 0; i < w; i++) {
+                               for (j = k; j < k + 4 && j < h; j++) {
+                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                                       t1_3d_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type, vsc);
+                               }
+                       }
+               }
+       }
+}                              /* VSC and  BYPASS by Antonin */
+
+static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) {
+       int v, flagsvr;
+       unsigned int flag;
+
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
+       flagsvr = (*fsvr);
+       if (partial) {
+               goto LABEL_PARTIAL;
+       }
+       if (!(*fsvr & (T1_3D_SIG | T1_3D_VISIT))) {
+               mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
+               v = int_abs(*dp) & one ? 1 : 0;
+               mqc_encode(mqc, v);
+               if (v) {
+LABEL_PARTIAL:
+                       *nmsedec += t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
+                       mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
+                       v = *dp < 0 ? 1 : 0;
+                       mqc_encode(mqc, v ^ t1_3d_getspb(flag));
+                       t1_3d_updateflags(fp, v);
+                       *fsvr |= T1_3D_SIG;
+               }
+       }
+       *fsvr &= ~T1_3D_VISIT;
+}
+
+static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, int partial, int vsc) {
+       int v, flagsvr;
+       unsigned int flag;
+
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
+       flagsvr = (*fsvr);
+       if (partial) {
+               goto LABEL_PARTIAL;
+       }
+       if (!(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
+               mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
+               if (mqc_decode(mqc)) {
+LABEL_PARTIAL:
+                       mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
+                       v = mqc_decode(mqc) ^ t1_3d_getspb(flag);
+                       *dp = v ? -oneplushalf : oneplushalf;
+                       t1_3d_updateflags(fp, v);
+                       *fsvr |= T1_3D_SIG;
+               }
+       }
+       *fsvr &= ~T1_3D_VISIT;
+}                              /* VSC and  BYPASS by Antonin */
+
+static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty) {
+       int i, j, k, m, one, agg, runlen, vsc;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       *nmsedec = 0;
+       one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
+       for (m = 0; m < l; m++) {
+               for (k = 0; k < h; k += 4) {
+                       for (i = 0; i < w; i++) {
+                               if (k + 3 < h) {
+                                       if (cblksty & J3D_CCP_CBLKSTY_VSC) {
+                                               agg = !( ((t1->flagSVR[1 + m][1 + k][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
+                                                       ||   ((t1->flagSVR[1 + m][1 + k + 1][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
+                                                       ||   ((t1->flagSVR[1 + m][1 + k + 2][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
+                                                       ||   ((t1->flagSVR[1 + m][1 + k + 3][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & ((t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH)))
+                                                       );
+                                       } else {
+                                               agg = !(
+                                                       ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
+                                                       || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
+                                                       || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
+                                                       || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH))
+                                                       );
+                                       }
+                               } else {
+                                       agg = 0;
+                               }
+                               if (agg) {
+                                       for (runlen = 0; runlen < 4; runlen++) {
+                                               if (int_abs(t1->data[m][k + runlen][i]) & one)
+                                                       break;
+                                       }
+                                       mqc_setcurctx(mqc, T1_CTXNO_AGG);
+                                       mqc_encode(mqc, runlen != 4);
+                                       if (runlen == 4) {
+                                               continue;
+                                       }
+                                       mqc_setcurctx(mqc, T1_CTXNO_UNI);
+                                       mqc_encode(mqc, runlen >> 1);
+                                       mqc_encode(mqc, runlen & 1);
+                               } else {
+                                       runlen = 0;
+                               }
+                               for (j = k + runlen; j < k + 4 && j < h; j++) {
+                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                                       t1_3d_enc_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
+                               }
+                       }
+               }
+       }
+}
+
+static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int cblksty) {
+       int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc;
+       int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       one = 1 << bpno;
+       half = one >> 1;
+       oneplushalf = one | half;
+       for (m = 0; m < l; m++) {
+               for (k = 0; k < h; k += 4) {
+                       for (i = 0; i < w; i++) {
+                               if (k + 3 < h) {
+                                       if (cblksty & J3D_CCP_CBLKSTY_VSC) {
+                                               agg = !(
+                                                       ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
+                                                       || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
+                                                       || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
+                                                       || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | ((t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH)))
+                                                       );
+                                       } else {
+                                               agg = !(
+                                                       ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
+                                                       || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
+                                                       || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
+                                                       || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH))
+                                                       );
+                                       }
+                               } else {
+                                       agg = 0;
+                               }
+                               if (agg) {
+                                       mqc_setcurctx(mqc, T1_CTXNO_AGG);
+                                       if (!mqc_decode(mqc)) {
+                                               continue;
+                                       }
+                                       mqc_setcurctx(mqc, T1_CTXNO_UNI);
+                                       runlen = mqc_decode(mqc);
+                                       runlen = (runlen << 1) | mqc_decode(mqc);
+                               } else {
+                                       runlen = 0;
+                               }
+                               for (j = k + runlen; j < k + 4 && j < h; j++) {
+                                       vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                                       t1_3d_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
+                               }
+                       }
+               }
+       }
+       if (segsym) {
+               int v = 0;
+               mqc_setcurctx(mqc, T1_CTXNO_UNI);
+               v = mqc_decode(mqc);
+               v = (v << 1) | mqc_decode(mqc);
+               v = (v << 1) | mqc_decode(mqc);
+               v = (v << 1) | mqc_decode(mqc);
+               /*
+               if (v!=0xa) {
+                       opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
+               } 
+               */
+       }
+}                              /* VSC and  BYPASS by Antonin */
+
+
+static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {
+       int i, j, k;
+       int w, h, l;
+       int passno;
+       int bpno, passtype;
+       int max;
+       int nmsedec = 0;
+       double cumwmsedec = 0;
+       char type = T1_TYPE_MQ;
+       
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       w = cblk->x1 - cblk->x0;
+       h = cblk->y1 - cblk->y0;
+       l = cblk->z1 - cblk->z0;
+
+       max = 0;
+       for (k = 0; k < l; k++) {
+               for (j = 0; j < h; j++) {
+                       for (i = 0; i < w; i++) {
+                               max = int_max(max, int_abs(t1->data[k][j][i]));
+                       }
+               }
+       }
+       for (k = 0; k <= l; k++) {
+               for (j = 0; j <= h; j++) {
+                       for (i = 0; i <= w; i++) {
+                               t1->flags[k][j][i] = 0;
+                               t1->flagSVR[k][j][i] = 0;
+                       }
+               }
+       }
+       
+       cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
+       
+       bpno = cblk->numbps - 1;
+       passtype = 2;
+       
+       mqc_reset_enc(mqc);
+       mqc_init_enc(mqc, cblk->data);
+       
+       for (passno = 0; bpno >= 0; passno++) {
+               opj_tcd_pass_t *pass = &cblk->passes[passno];
+               int correction = 3;
+               double tmpwmsedec;
+               type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
+               
+               switch (passtype) {
+                       case 0:
+                               t1_3d_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty);
+                               break;
+                       case 1:
+                               t1_3d_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty);
+                               break;
+                       case 2:
+                               t1_3d_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty);
+                               /* code switch SEGMARK (i.e. SEGSYM) */
+                               if (cblksty & J3D_CCP_CBLKSTY_SEGSYM)
+                                       mqc_segmark_enc(mqc);
+                               break;
+               }
+               
+               /* fixed_quality */
+               tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize, numcomps, dwtid);
+               cumwmsedec += tmpwmsedec;
+               tile->distotile += tmpwmsedec;
+               
+               /* Code switch "RESTART" (i.e. TERMALL) */
+               if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
+                       if (type == T1_TYPE_RAW) {
+                               mqc_flush(mqc);
+                               correction = 1;
+                               /* correction = mqc_bypass_flush_enc(); */
+                       } else {                        /* correction = mqc_restart_enc(); */
+                               mqc_flush(mqc);
+                               correction = 1;
+                       }
+                       pass->term = 1;
+               } else {
+                       if (((bpno < (cblk->numbps - 4) && (passtype > 0)) 
+                               || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) {
+                               if (type == T1_TYPE_RAW) {
+                                       mqc_flush(mqc);
+                                       correction = 1;
+                               } else {        
+                                       mqc_flush(mqc);
+                                       correction = 1;
+                               }
+                               pass->term = 1;
+                       } else {
+                               pass->term = 0;
+                       }
+               }
+               
+               if (++passtype == 3) {
+                       passtype = 0;
+                       bpno--;
+               }
+               
+               if (pass->term && bpno > 0) {
+                       type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
+                       if (type == T1_TYPE_RAW)
+                               mqc_bypass_init_enc(mqc);
+                       else
+                               mqc_restart_init_enc(mqc);
+               }
+               
+               pass->distortiondec = cumwmsedec;
+               pass->rate = mqc_numbytes(mqc) + correction;    /* FIXME */
+               pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
+
+               /* Code-switch "RESET" */
+               if (cblksty & J3D_CCP_CBLKSTY_RESET)
+                       mqc_reset_enc(mqc);
+       }
+       
+       /* Code switch "ERTERM" (i.e. PTERM) */
+       if (cblksty & J3D_CCP_CBLKSTY_PTERM)
+               mqc_erterm_enc(mqc);
+       else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY))
+               mqc_flush(mqc);
+       
+       cblk->totalpasses = passno;
+}
+
+static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
+       int i, j, k;
+       int w, h, l;
+       int bpno, passtype;
+       int segno, passno;
+       char type = T1_TYPE_MQ; /* BYPASS mode */
+       opj_raw_t *raw = t1->raw;       /* RAW component */
+       opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+       w = cblk->x1 - cblk->x0;
+       h = cblk->y1 - cblk->y0;
+       l = cblk->z1 - cblk->z0;
+
+    for (k = 0; k < l; k++) {
+               for (j = 0; j < h; j++) {
+                       for (i = 0; i < w; i++) {
+                               t1->data[k][j][i] = 0;
+                       }
+               }
+       }
+       
+       for (k = 0; k <= l; k++) {
+               for (j = 0; j <= h; j++) {
+                       for (i = 0; i <= w; i++) {
+                               t1->flags[k][j][i] = 0;
+                               t1->flagSVR[k][j][i] = 0;
+                       }
+               }
+       }
+
+       
+       bpno = roishift + cblk->numbps - 1;
+       passtype = 2;
+       
+       mqc_reset_enc(mqc);
+       
+       for (segno = 0; segno < cblk->numsegs; segno++) {
+               opj_tcd_seg_t *seg = &cblk->segs[segno];
+               
+               /* BYPASS mode */
+               type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
+               if (type == T1_TYPE_RAW) {
+                       raw_init_dec(raw, seg->data, seg->len);
+               } else {
+                       mqc_init_dec(mqc, seg->data, seg->len);
+               }
+               
+               for (passno = 0; passno < seg->numpasses; passno++) {
+                       switch (passtype) {
+                               case 0:
+                                       t1_3d_dec_sigpass(t1, w, h, l, bpno+1, orient, type, cblksty);
+                                       break;
+                               case 1:
+                                       t1_3d_dec_refpass(t1, w, h, l, bpno+1, type, cblksty);
+                                       break;
+                               case 2:
+                                       t1_3d_dec_clnpass(t1, w, h, l, bpno+1, orient, cblksty);
+                                       break;
+                       }
+                       
+                       if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
+                               mqc_reset_enc(mqc);
+                       }
+                       if (++passtype == 3) {
+                               passtype = 0;
+                               bpno--;
+                       }
+
+               }
+       }
+}
+
+static int t1_3d_init_ctxno_zc(unsigned int f, int orient) {
+       unsigned int h, v, c;
+       unsigned int d2xy, d2xz, d2yz, d3;
+       int n;
+       unsigned int hvc, hc, d2, d2xy2yz, d2xy2xz;
+       n = 0;
+       h = ((f & T1_3D_SIG_W) != 0) + ((f & T1_3D_SIG_E) != 0);
+       v = ((f & T1_3D_SIG_N) != 0) + ((f & T1_3D_SIG_S) != 0);
+       c = ((f & T1_3D_SIG_FC) != 0) + ((f & T1_3D_SIG_BC) != 0);
+       d2xy = ((f & T1_3D_SIG_NW) != 0) + ((f & T1_3D_SIG_NE) != 0) + ((f & T1_3D_SIG_SE) != 0) + ((f & T1_3D_SIG_SW) != 0);
+       d2xz = ((f & T1_3D_SIG_FW) != 0) + ((f & T1_3D_SIG_BW) != 0) + ((f & T1_3D_SIG_FE) != 0) + ((f & T1_3D_SIG_BE) != 0);
+       d2yz = ((f & T1_3D_SIG_FN) != 0) + ((f & T1_3D_SIG_FS) != 0) + ((f & T1_3D_SIG_BN) != 0) + ((f & T1_3D_SIG_BS) != 0);
+    d3 = ((f & T1_3D_SIG_FNW) != 0) + ((f & T1_3D_SIG_FNE) != 0) + ((f & T1_3D_SIG_FSE) != 0) + ((f & T1_3D_SIG_FSW) != 0) 
+               + ((f & T1_3D_SIG_BNW) != 0) + ((f & T1_3D_SIG_BNE) != 0) + ((f & T1_3D_SIG_BSE) != 0) + ((f & T1_3D_SIG_BSW) != 0);
+       
+       switch (orient) {
+               case 0: /*LLL*/
+               case 7: /*HHH*/
+                       hvc = h + v + c;
+                       d2 = d2xy + d2xz + d2yz;
+                       if (!hvc) {
+                               if (!d2) {
+                    n = (!d3) ? 0 : 1;
+                               } else if (d2 == 1) {
+                                       n = (!d3) ? 2 : 3;
+                               } else {
+                                       n = (!d3) ? 4 : 5;
+                               }
+                       } else if (hvc == 1) {
+                               if (!d2) {
+                    n = (!d3) ? 6 : 7;
+                               } else if (d2 == 1) {
+                                       n = (!d3) ? 8 : 9;
+                               } else {
+                                       n = 10;
+                               }
+                       } else if (hvc == 2) {
+                               if (!d2) {
+                    n = (!d3) ? 11 : 12;
+                               } else {
+                                       n = 13;
+                               }
+                       } else if (hvc == 3) {
+                               n = 14;
+                       } else {
+                               n = 15;
+                       }
+                       break;
+               /*LHL, HLL, LLH*/
+               case 1:
+               case 2:
+               case 4:
+                       hc = h + c;
+                       d2xy2yz = d2xy + d2yz;
+            if (!hc) {
+                               if (!v) {
+                                       if (!d2xy) {
+                                               n = (!d2xy2yz) ? ((!d3) ? 0 : 1) : ((!d3) ? 2 : 3);     
+                                       } else if (d2xy == 1) {
+                                               n = (!d2xy2yz) ? ((!d3) ? 4 : 5) : 6;   
+                                       } else { /*>=2*/
+                        n = 7;
+                                       }
+                               } else {
+                                       n = (v == 1) ? 8 : 9; /* =1 or =2*/
+                               } 
+                       } else if (hc == 1) {
+                               n = (!v) ? ( (!d2xy) ? ( (!d2xy2yz) ? ( (!d3) ? 10 : 11) : (12) ) : (13) ) : (14);
+                       } else { /*if (hc >= 2)*/
+                               n = 15;
+                       }
+                       break;
+               /*HLH, HHL, LHH*/
+               case 3:
+               case 5:
+               case 6:
+                       hc = h + c;
+                       d2xy2xz = d2xy + d2xz;
+                       if (!v) {
+                               if (!d2xz) {
+                                       if (!hc && !d2xy2xz) {
+                                               n = (!d3) ? 0 : 1;
+                                       } else if (hc == 1) {
+                                               n = (!d2xy2xz) ?  2 : 3;
+                                       } else { /*if >= 2*/
+                                               n = 4;
+                                       }
+                               } else if ( d2xz>=1 && !hc ) {
+                                       n = 5;
+                               } else if ( hc>=1 ) {
+                                       n = (d2xz==1) ? 6 : 7;
+                               } 
+                       } else if (v == 1) {
+                               if (!d2xz) {
+                                       n = (!hc) ? 8 : 9;
+                               } else if (d2xz == 1) {
+                                       n = (!hc) ? 10 : 11;
+                               } else if (d2xz == 2) {
+                                       n = (!hc) ? 12 : 13;
+                               } else { /* if (d2xz >= 3) {*/
+                                       n = 14;
+                               }
+                       } else if (v == 2) {
+                               n = 15;
+                       } 
+                       break;
+       }
+       
+       return (T1_3D_CTXNO_ZC + n);
+}
+
+static int t1_3d_init_ctxno_sc(unsigned int f) {
+       int hc, vc, cc;
+       int n = 0;
+
+       hc = int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E ) 
+                                       + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W ) , 1) 
+               - int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E) ) 
+                                       + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W) ) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1);
+       
+       vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N) 
+                                       + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1) 
+               - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N)) 
+                                       + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1);
+       
+       cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC) 
+                                       + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1) 
+               - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F)) 
+                                       + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1);
+       if (hc < 0) {
+               hc = -hc;
+               vc = -vc;
+               cc = -cc;
+       }
+
+       if (!hc) {
+               if (!vc) 
+                       n = (!cc) ? 0 : 1;
+               else if (vc == -1)
+                       n = (!cc) ? 1 : ( (cc>0) ? 2 : 4);
+               else if (vc == 1)
+                       n = (!cc) ? 1 : ( (cc<0) ? 2 : 4);
+       } else if (hc == 1) {
+               if (!vc)
+                       n = (!cc) ? 1 : ( (cc<0) ? 2 : 4);
+               else if (vc == 1)
+                       n = (!cc) ? 4 : ( (cc>0) ? 5 : 3);
+               else if (vc == -1)
+                       n = (!cc) ? 2 : 3;
+       } else if (hc == -1) {
+               if (!vc)
+                       n = (!cc) ? 1 : ( (cc>0) ? 2 : 4);
+               else if (vc == 1)
+                       n = (!cc) ? 2 : 3;
+               else if (vc == -1)
+                       n = (!cc) ? 4 : ( (cc<0) ? 5 : 3);
+       }
+       
+       return (T1_3D_CTXNO_SC + n);
+}
+
+static int t1_3d_init_ctxno_mag(unsigned int f, int f2) {
+       int n;
+       if (!(f2 & T1_3D_REFINE))       /*First refinement for this coefficient (no previous refinement)*/
+               n = (f & (T1_3D_SIG_PRIM)) ? 1 : 0;
+       else
+               n = 2;
+       
+       return (T1_3D_CTXNO_MAG + n);
+}
+
+static int t1_3d_init_spb(unsigned int f) {
+       int hc, vc, cc;
+       int n = 0;
+       
+       hc = int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E ) 
+                                       + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W ) , 1) 
+               - int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E) ) 
+                                       + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W) ) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1);
+       
+       vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N) 
+                                       + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1) 
+               - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N)) 
+                                       + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1);
+       
+       cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC) 
+                                       + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1) 
+               - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F)) 
+                                       + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1);
+       
+       n = ((hc + vc + cc) < 0); 
+       
+       return n;
+}
+
+static void t1_3d_init_luts(opj_t1_3d_t *t1) {
+       int i;
+       double u, v, t;
+       /*for (j = 0; j < 4; j++) {
+               for (i = 0; i < 256; ++i) {
+                       t1->lut_ctxno_zc[(j << 8) | i] = t1_3d_init_ctxno_zc(i, j);
+               }
+       }
+       for (i = 0; i < 4096; i++) {
+               t1->lut_ctxno_sc[i] = t1_3d_init_ctxno_sc(i << 4);
+       }
+       for (j = 0; j < 2; j++) {
+               for (i = 0; i < 2048; ++i) {
+                       t1->lut_ctxno_mag[(j << 11) + i] = t1_3d_init_ctxno_mag((j ? T1_3D_REFINE : 0) | i);
+               }
+       }
+       for (i = 0; i < 4096; ++i) {
+               t1->lut_spb[i] = t1_3d_init_spb(i << 4);
+       }*/
+       /* FIXME FIXME FIXME */
+       for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
+               t = i / pow(2, T1_NMSEDEC_FRACBITS);
+               u = t;
+               v = t - 1.5;
+               t1->lut_nmsedec_sig[i] = 
+                       int_max(0, 
+                       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+               t1->lut_nmsedec_sig0[i] =
+                       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;
+               }
+               t1->lut_nmsedec_ref[i] =
+                       int_max(0,
+                       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+               t1->lut_nmsedec_ref0[i] =
+                       int_max(0,
+                       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+       }
+}
+
+/* ----------------------------------------------------------------------- */
+
+opj_t1_3d_t* t1_3d_create(opj_common_ptr cinfo) {
+       opj_t1_3d_t *t1 = (opj_t1_3d_t*)opj_malloc(sizeof(opj_t1_3d_t));
+       if(t1) {
+               t1->cinfo = cinfo;
+               /* create MQC and RAW handles */
+               t1->mqc = mqc_create();
+               t1->raw = raw_create();
+               /* initialize the look-up tables of the Tier-1 coder/decoder */
+               t1_3d_init_luts(t1);
+       }
+       return t1;
+}
+
+void t1_3d_destroy(opj_t1_3d_t *t1) {
+       if(t1) {
+               /* destroy MQC and RAW handles */
+               mqc_destroy(t1->mqc);
+               raw_destroy(t1->raw);
+               opj_free(t1);
+       }
+}
+
+void t1_3d_encode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
+       int compno, resno, bandno, precno, cblkno;
+       int x, y, z, i, j, k, orient;
+       int level[3];
+       tile->distotile = 0;            /* fixed_quality */
+
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+
+               for (resno = 0; resno < tilec->numresolution[0]; resno++) {
+                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
+                       for (bandno = 0; bandno < res->numbands; bandno++) {
+                               opj_tcd_band_t *band = &res->bands[bandno];
+
+                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
+                                       opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+
+                                               if (band->bandno == 0) {
+                                                       x = cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 1) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 2) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 3) {         
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 4) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 5) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 6) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 7) {         
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               }
+
+                                               if (tcp->tccps[compno].reversible == 1) {
+                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {
+                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                                    for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                                        t1->data[k][j][i] = 
+                                                                               tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS;
+                                                                       }
+                                                               }
+                                                       }
+                                               } else if (tcp->tccps[compno].reversible == 0) {
+                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {
+                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                                    for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                                        t1->data[k][j][i] = fix_mul(
+                                                                               tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)],
+                                                                               8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                               orient = band->bandno;  /* FIXME */
+                                               for (i = 0; i < 3; i++) 
+                                                       level[i] = tilec->numresolution[i] - 1 - resno;
+
+                                               t1_3d_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
+                                                       
+                                       } /* cblkno */
+                               } /* precno */
+                       } /* bandno */
+               } /* resno  */
+       } /* compno  */
+}
+
+void t1_3d_decode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
+       int compno, resno, bandno, precno, cblkno;
+       
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+
+               for (resno = 0; resno < tilec->numresolution[0]; resno++) {
+                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
+                       for (bandno = 0; bandno < res->numbands; bandno++) {
+                               opj_tcd_band_t *band = &res->bands[bandno];
+
+                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
+                                       opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                                               int x, y, z, i, j, k, orient;
+                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+
+                                               orient = band->bandno;  /* FIXME */
+
+                                               /*fprintf(stdout,"[INFO] t1_3d_decode_cblk(t1, cblk, orient(%d), tcp->tccps[compno].roishift (%d), tcp->tccps[compno].cblksty (%d));\n",orient,tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);*/
+                                               t1_3d_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
+
+                                               if (band->bandno == 0) {
+                                                       x = cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 1) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 2) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 3) {         
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = cblk->z0 - band->z0;
+                                               } else if (band->bandno == 4) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 5) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 6) {
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               } else if (band->bandno == 7) {         
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                                                       z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
+                                               }
+                                       
+                                               if (tcp->tccps[compno].roishift) {
+                                                       int thresh, val, mag;
+                                                       thresh = 1 << tcp->tccps[compno].roishift;
+                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {
+                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                                                                               val = t1->data[k][j][i];
+                                                                               mag = int_abs(val);
+                                                                               if (mag >= thresh) {
+                                                                                       mag >>= tcp->tccps[compno].roishift;
+                                                                                       t1->data[k][j][i] = val < 0 ? -mag : mag;
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                               
+                                               if (tcp->tccps[compno].reversible == 1) {
+                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {
+                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                                                                               int tmp = t1->data[k][j][i];
+                                                                               tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp/2;
+                                                                       }
+                                                               }
+                                                       }
+                                               } else {                /* if (tcp->tccps[compno].reversible == 0) */
+                                                       for (k = 0; k < cblk->z1 - cblk->z0; k++) {
+                                                               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                                                                       for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                                                                               double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0);
+                                                                               if (t1->data[k][j][i] >> 1 == 0) {
+                                                                                       tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0;
+                                                                               } else {
+                                                                                       int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
+                                                                                       tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp<0)?-tmp2:tmp2);
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       } /* cblkno */
+                               } /* precno */
+                       } /* bandno */
+               } /* resno */
+       } /* compno */
+}
index 6410bd5af60de449eb51e2a0a88b245490bcb847..2974781ff19e4ef0f109dd4cb18042da137bb16f 100644 (file)
-/*\r
- * Copyrigth (c) 2006, M�nica D�ez, LPI-UVA, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-#ifndef __T1_3D_H\r
-#define __T1_3D_H\r
-/**\r
-@file t1_3d.h\r
-@brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1)\r
-\r
-The functions in T1_3D.C have for goal to realize the tier-1 coding operation of 3D-EBCOT.\r
-The functions in T1_3D.C are used by some function in TCD.C.\r
-*/\r
-\r
-/** @defgroup T1_3D T1_3D - Implementation of the tier-1 coding */\r
-/*@{*/\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/* Neighbourhood of 3D EBCOT (Significance context)*/\r
-#define T1_3D_SIG_NE  0x00000001       /*< Context orientation : North-East direction */\r
-#define T1_3D_SIG_SE  0x00000002       /*< Context orientation : South-East direction */\r
-#define T1_3D_SIG_SW  0x00000004       /*< Context orientation : South-West direction */\r
-#define T1_3D_SIG_NW  0x00000008       /* Context orientation : North-West direction */\r
-#define T1_3D_SIG_N   0x00000010       /*< Context orientation : North direction */\r
-#define T1_3D_SIG_E   0x00000020       /*< Context orientation : East direction */\r
-#define T1_3D_SIG_S   0x00000040       /*< Context orientation : South direction */\r
-#define T1_3D_SIG_W   0x00000080       /*< Context orientation : West direction */\r
-#define T1_3D_SIG_FC  0x00000100       /*< Context orientation : Forward Central direction */          \r
-#define T1_3D_SIG_BC  0x00000200       /*< Context orientation : Backward Central direction */ \r
-#define T1_3D_SIG_FNE 0x00000400       /*< Context orientation : Forward North-East direction */       \r
-#define T1_3D_SIG_FSE 0x00000800       /*< Context orientation : Forward South-East direction */       \r
-#define T1_3D_SIG_FSW 0x00001000       /*< Context orientation : Forward South-West direction */       \r
-#define T1_3D_SIG_FNW 0x00002000       /*< Context orientation : Forward North-West direction */       \r
-#define T1_3D_SIG_FN  0x00004000       /*< Context orientation : Forward North direction */            \r
-#define T1_3D_SIG_FE  0x00008000       /*< Context orientation : Forward East direction */             \r
-#define T1_3D_SIG_FS  0x00010000       /*< Context orientation : Forward South direction */            \r
-#define T1_3D_SIG_FW  0x00020000       /*< Context orientation : Forward West direction */             \r
-#define T1_3D_SIG_BNE 0x00040000       /*< Context orientation : Backward North-East direction */      \r
-#define T1_3D_SIG_BSE 0x00080000       /*< Context orientation : Backward South-East direction */      \r
-#define T1_3D_SIG_BSW 0x00100000       /*< Context orientation : Backward South-West direction */      \r
-#define T1_3D_SIG_BNW 0x00200000       /*< Context orientation : Backward North-West direction */      \r
-#define T1_3D_SIG_BN  0x00400000       /*< Context orientation : Backward North direction */           \r
-#define T1_3D_SIG_BE  0x00800000       /*< Context orientation : Backward East direction */            \r
-#define T1_3D_SIG_BS  0x01000000       /*< Context orientation : Backward South direction */           \r
-#define T1_3D_SIG_BW  0x02000000       /*< Context orientation : Backward West direction */            \r
-#define T1_3D_SIG_COTH (T1_3D_SIG_N|T1_3D_SIG_NE|T1_3D_SIG_E|T1_3D_SIG_SE|T1_3D_SIG_S|T1_3D_SIG_SW|T1_3D_SIG_W|T1_3D_SIG_NW)\r
-#define T1_3D_SIG_BOTH (T1_3D_SIG_BN|T1_3D_SIG_BNE|T1_3D_SIG_BE|T1_3D_SIG_BSE|T1_3D_SIG_BS|T1_3D_SIG_BSW|T1_3D_SIG_BW|T1_3D_SIG_BNW|T1_3D_SIG_BC)\r
-#define T1_3D_SIG_FOTH  (T1_3D_SIG_FN|T1_3D_SIG_FNE|T1_3D_SIG_FE|T1_3D_SIG_FSE|T1_3D_SIG_FS|T1_3D_SIG_FSW|T1_3D_SIG_FW|T1_3D_SIG_FNW|T1_3D_SIG_FC)\r
-#define T1_3D_SIG_OTH  (T1_3D_SIG_FOTH|T1_3D_SIG_BOTH|T1_3D_SIG_COTH)\r
-#define T1_3D_SIG_PRIM (T1_3D_SIG_N|T1_3D_SIG_E|T1_3D_SIG_S|T1_3D_SIG_W|T1_3D_SIG_FC|T1_3D_SIG_BC)\r
-\r
-#define T1_3D_SGN_N            0x0400          \r
-#define T1_3D_SGN_E            0x0800          \r
-#define T1_3D_SGN_S            0x1000          \r
-#define T1_3D_SGN_W            0x2000          \r
-#define T1_3D_SGN_F            0x4000  \r
-#define T1_3D_SGN_B            0x8000\r
-#define T1_3D_SGN              (T1_3D_SGN_N|T1_3D_SGN_E|T1_3D_SGN_S|T1_3D_SGN_W|T1_3D_SGN_F|T1_3D_SGN_B)\r
-\r
-#define T1_3D_SIG              0x0001  /*Significance state*/\r
-#define T1_3D_REFINE   0x0002  /*Delayed significance*/\r
-#define T1_3D_VISIT            0x0004  /*First-pass membership*/\r
-\r
-#define T1_3D_NUMCTXS_AGG      1\r
-#define T1_3D_NUMCTXS_ZC       16\r
-#define T1_3D_NUMCTXS_MAG      3\r
-#define T1_3D_NUMCTXS_SC       6\r
-#define T1_3D_NUMCTXS_UNI      1\r
-\r
-#define T1_3D_CTXNO_AGG 0\r
-#define T1_3D_CTXNO_ZC (T1_3D_CTXNO_AGG+T1_3D_NUMCTXS_AGG) /*1*/\r
-#define T1_3D_CTXNO_MAG (T1_3D_CTXNO_ZC+T1_3D_NUMCTXS_ZC)      /*17*/\r
-#define T1_3D_CTXNO_SC (T1_3D_CTXNO_MAG+T1_3D_NUMCTXS_MAG)     /*20*/\r
-#define T1_3D_CTXNO_UNI (T1_3D_CTXNO_SC+T1_3D_NUMCTXS_SC)      /*26*/\r
-#define T1_3D_NUMCTXS  (T1_3D_CTXNO_UNI+T1_3D_NUMCTXS_UNI) /*27*/\r
-\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/**\r
-Tier-1 coding (coding of code-block coefficients)\r
-*/\r
-typedef struct opj_t1_3d {\r
-       /** Codec context */\r
-       opj_common_ptr cinfo;\r
-       /** MQC component */\r
-       opj_mqc_t *mqc;\r
-       /** RAW component */\r
-       opj_raw_t *raw;\r
-       /** LUTs for decoding normalised MSE */\r
-       int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];\r
-       int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];\r
-       int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];\r
-       int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];\r
-       /** Codeblock data */\r
-       int data[T1_CBLKD][T1_CBLKH][T1_CBLKW];\r
-       /** Context information for each voxel in codeblock */\r
-       unsigned int flags[T1_CBLKD + 2][T1_CBLKH + 2][T1_CBLKH + 2];\r
-       /** Voxel information (significance/visited/refined) */\r
-       int flagSVR[T1_CBLKD + 2][T1_CBLKH + 2][T1_CBLKH + 2];\r
-} opj_t1_3d_t;\r
-\r
-/** @name Exported functions */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Create a new T1_3D handle \r
-and initialize the look-up tables of the Tier-1 coder/decoder\r
-@return Returns a new T1 handle if successful, returns NULL otherwise\r
-@see t1_init_luts\r
-*/\r
-opj_t1_3d_t* t1_3d_create(opj_common_ptr cinfo);\r
-/**\r
-Destroy a previously created T1_3D handle\r
-@param t1 T1_3D handle to destroy\r
-*/\r
-void t1_3d_destroy(opj_t1_3d_t *t1);\r
-/**\r
-Encode the code-blocks of a tile\r
-@param t1 T1_3D handle\r
-@param tile The tile to encode\r
-@param tcp Tile coding parameters\r
-*/\r
-void t1_3d_encode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp);\r
-/**\r
-Decode the code-blocks of a tile\r
-@param t1 T1_3D handle\r
-@param tile The tile to decode\r
-@param tcp Tile coding parameters\r
-*/\r
-void t1_3d_decode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp);\r
-/**\r
-Get weigths of MSE decoding\r
-@param nmsedec The normalized MSE reduction\r
-@param compno \r
-@param level \r
-@param orient\r
-@param bpno\r
-@param reversible\r
-@param stepsize\r
-@param numcomps\r
-@param dwtid\r
-returns MSE associated to decoding pass\r
-double t1_3d_getwmsedec(int nmsedec, int compno, int levelxy, int levelz, int orient, int bpno, int reversible, double stepsize, int numcomps, int dwtid);\r
-*/\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __T1_H */\r
+/*
+ * Copyrigth (c) 2006, M�nica D�ez, LPI-UVA, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __T1_3D_H
+#define __T1_3D_H
+/**
+@file t1_3d.h
+@brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1)
+
+The functions in T1_3D.C have for goal to realize the tier-1 coding operation of 3D-EBCOT.
+The functions in T1_3D.C are used by some function in TCD.C.
+*/
+
+/** @defgroup T1_3D T1_3D - Implementation of the tier-1 coding */
+/*@{*/
+
+/* ----------------------------------------------------------------------- */
+
+/* Neighbourhood of 3D EBCOT (Significance context)*/
+#define T1_3D_SIG_NE  0x00000001       /*< Context orientation : North-East direction */
+#define T1_3D_SIG_SE  0x00000002       /*< Context orientation : South-East direction */
+#define T1_3D_SIG_SW  0x00000004       /*< Context orientation : South-West direction */
+#define T1_3D_SIG_NW  0x00000008       /* Context orientation : North-West direction */
+#define T1_3D_SIG_N   0x00000010       /*< Context orientation : North direction */
+#define T1_3D_SIG_E   0x00000020       /*< Context orientation : East direction */
+#define T1_3D_SIG_S   0x00000040       /*< Context orientation : South direction */
+#define T1_3D_SIG_W   0x00000080       /*< Context orientation : West direction */
+#define T1_3D_SIG_FC  0x00000100       /*< Context orientation : Forward Central direction */          
+#define T1_3D_SIG_BC  0x00000200       /*< Context orientation : Backward Central direction */ 
+#define T1_3D_SIG_FNE 0x00000400       /*< Context orientation : Forward North-East direction */       
+#define T1_3D_SIG_FSE 0x00000800       /*< Context orientation : Forward South-East direction */       
+#define T1_3D_SIG_FSW 0x00001000       /*< Context orientation : Forward South-West direction */       
+#define T1_3D_SIG_FNW 0x00002000       /*< Context orientation : Forward North-West direction */       
+#define T1_3D_SIG_FN  0x00004000       /*< Context orientation : Forward North direction */            
+#define T1_3D_SIG_FE  0x00008000       /*< Context orientation : Forward East direction */             
+#define T1_3D_SIG_FS  0x00010000       /*< Context orientation : Forward South direction */            
+#define T1_3D_SIG_FW  0x00020000       /*< Context orientation : Forward West direction */             
+#define T1_3D_SIG_BNE 0x00040000       /*< Context orientation : Backward North-East direction */      
+#define T1_3D_SIG_BSE 0x00080000       /*< Context orientation : Backward South-East direction */      
+#define T1_3D_SIG_BSW 0x00100000       /*< Context orientation : Backward South-West direction */      
+#define T1_3D_SIG_BNW 0x00200000       /*< Context orientation : Backward North-West direction */      
+#define T1_3D_SIG_BN  0x00400000       /*< Context orientation : Backward North direction */           
+#define T1_3D_SIG_BE  0x00800000       /*< Context orientation : Backward East direction */            
+#define T1_3D_SIG_BS  0x01000000       /*< Context orientation : Backward South direction */           
+#define T1_3D_SIG_BW  0x02000000       /*< Context orientation : Backward West direction */            
+#define T1_3D_SIG_COTH (T1_3D_SIG_N|T1_3D_SIG_NE|T1_3D_SIG_E|T1_3D_SIG_SE|T1_3D_SIG_S|T1_3D_SIG_SW|T1_3D_SIG_W|T1_3D_SIG_NW)
+#define T1_3D_SIG_BOTH (T1_3D_SIG_BN|T1_3D_SIG_BNE|T1_3D_SIG_BE|T1_3D_SIG_BSE|T1_3D_SIG_BS|T1_3D_SIG_BSW|T1_3D_SIG_BW|T1_3D_SIG_BNW|T1_3D_SIG_BC)
+#define T1_3D_SIG_FOTH  (T1_3D_SIG_FN|T1_3D_SIG_FNE|T1_3D_SIG_FE|T1_3D_SIG_FSE|T1_3D_SIG_FS|T1_3D_SIG_FSW|T1_3D_SIG_FW|T1_3D_SIG_FNW|T1_3D_SIG_FC)
+#define T1_3D_SIG_OTH  (T1_3D_SIG_FOTH|T1_3D_SIG_BOTH|T1_3D_SIG_COTH)
+#define T1_3D_SIG_PRIM (T1_3D_SIG_N|T1_3D_SIG_E|T1_3D_SIG_S|T1_3D_SIG_W|T1_3D_SIG_FC|T1_3D_SIG_BC)
+
+#define T1_3D_SGN_N            0x0400          
+#define T1_3D_SGN_E            0x0800          
+#define T1_3D_SGN_S            0x1000          
+#define T1_3D_SGN_W            0x2000          
+#define T1_3D_SGN_F            0x4000  
+#define T1_3D_SGN_B            0x8000
+#define T1_3D_SGN              (T1_3D_SGN_N|T1_3D_SGN_E|T1_3D_SGN_S|T1_3D_SGN_W|T1_3D_SGN_F|T1_3D_SGN_B)
+
+#define T1_3D_SIG              0x0001  /*Significance state*/
+#define T1_3D_REFINE   0x0002  /*Delayed significance*/
+#define T1_3D_VISIT            0x0004  /*First-pass membership*/
+
+#define T1_3D_NUMCTXS_AGG      1
+#define T1_3D_NUMCTXS_ZC       16
+#define T1_3D_NUMCTXS_MAG      3
+#define T1_3D_NUMCTXS_SC       6
+#define T1_3D_NUMCTXS_UNI      1
+
+#define T1_3D_CTXNO_AGG 0
+#define T1_3D_CTXNO_ZC (T1_3D_CTXNO_AGG+T1_3D_NUMCTXS_AGG) /*1*/
+#define T1_3D_CTXNO_MAG (T1_3D_CTXNO_ZC+T1_3D_NUMCTXS_ZC)      /*17*/
+#define T1_3D_CTXNO_SC (T1_3D_CTXNO_MAG+T1_3D_NUMCTXS_MAG)     /*20*/
+#define T1_3D_CTXNO_UNI (T1_3D_CTXNO_SC+T1_3D_NUMCTXS_SC)      /*26*/
+#define T1_3D_NUMCTXS  (T1_3D_CTXNO_UNI+T1_3D_NUMCTXS_UNI) /*27*/
+
+
+/* ----------------------------------------------------------------------- */
+
+/**
+Tier-1 coding (coding of code-block coefficients)
+*/
+typedef struct opj_t1_3d {
+       /** Codec context */
+       opj_common_ptr cinfo;
+       /** MQC component */
+       opj_mqc_t *mqc;
+       /** RAW component */
+       opj_raw_t *raw;
+       /** LUTs for decoding normalised MSE */
+       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];
+       /** Codeblock data */
+       int data[T1_CBLKD][T1_CBLKH][T1_CBLKW];
+       /** Context information for each voxel in codeblock */
+       unsigned int flags[T1_CBLKD + 2][T1_CBLKH + 2][T1_CBLKH + 2];
+       /** Voxel information (significance/visited/refined) */
+       int flagSVR[T1_CBLKD + 2][T1_CBLKH + 2][T1_CBLKH + 2];
+} opj_t1_3d_t;
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a new T1_3D handle 
+and initialize the look-up tables of the Tier-1 coder/decoder
+@return Returns a new T1 handle if successful, returns NULL otherwise
+@see t1_init_luts
+*/
+opj_t1_3d_t* t1_3d_create(opj_common_ptr cinfo);
+/**
+Destroy a previously created T1_3D handle
+@param t1 T1_3D handle to destroy
+*/
+void t1_3d_destroy(opj_t1_3d_t *t1);
+/**
+Encode the code-blocks of a tile
+@param t1 T1_3D handle
+@param tile The tile to encode
+@param tcp Tile coding parameters
+*/
+void t1_3d_encode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp);
+/**
+Decode the code-blocks of a tile
+@param t1 T1_3D handle
+@param tile The tile to decode
+@param tcp Tile coding parameters
+*/
+void t1_3d_decode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp);
+/**
+Get weigths of MSE decoding
+@param nmsedec The normalized MSE reduction
+@param compno 
+@param level 
+@param orient
+@param bpno
+@param reversible
+@param stepsize
+@param numcomps
+@param dwtid
+returns MSE associated to decoding pass
+double t1_3d_getwmsedec(int nmsedec, int compno, int levelxy, int levelz, int orient, int bpno, int reversible, double stepsize, int numcomps, int dwtid);
+*/
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __T1_H */
index e902d60dbf16c19d4ec2cf25c01ca070c2c713ae..7fd551e56a334f5df116dd133c0f80174f3efc28 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/** @defgroup T2 T2 - Implementation of a tier-2 coding */\r
-/*@{*/\r
-\r
-/** @name Local static functions */\r
-/*@{*/\r
-\r
-static void t2_putcommacode(opj_bio_t *bio, int n);\r
-static int t2_getcommacode(opj_bio_t *bio);\r
-/**\r
-Variable length code for signalling delta Zil (truncation point)\r
-@param bio Bit Input/Output component\r
-@param n delta Zil\r
-*/\r
-static void t2_putnumpasses(opj_bio_t *bio, int n);\r
-static int t2_getnumpasses(opj_bio_t *bio);\r
-/**\r
-Encode a packet of a tile to a destination buffer\r
-@param tile Tile for which to write the packets\r
-@param tcp Tile coding parameters\r
-@param pi Packet identity\r
-@param dest Destination buffer\r
-@param len Length of the destination buffer\r
-@param volume_info Structure to create an index file\r
-@param tileno Number of the tile encoded\r
-@param cp Coding parameters\r
-@return Number of bytes encoded from the packet\r
-*/\r
-static int t2_encode_packet(opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_volume_info_t *volume_info, int tileno, opj_cp_t *cp);\r
-/**\r
-Initialize the segment decoder\r
-@param seg Segment instance\r
-@param cblksty Codeblock style\r
-@param first Is first segment\r
-*/\r
-static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);\r
-/**\r
-Decode a packet of a tile from a source buffer\r
-@param t2 T2 handle\r
-@param src Source buffer\r
-@param len Length of the source buffer\r
-@param tile Tile for which to write the packets\r
-@param tcp Tile coding parameters\r
-@param pi Packet identity\r
-@return Number of bytes decoded from the packet\r
-*/\r
-int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi);\r
-\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/* #define RESTART 0x04 */\r
-static void t2_putcommacode(opj_bio_t *bio, int n) {\r
-       while (--n >= 0) {\r
-               bio_write(bio, 1, 1);\r
-       }\r
-       bio_write(bio, 0, 1);\r
-}\r
-\r
-static int t2_getcommacode(opj_bio_t *bio) {\r
-       int n;\r
-       for (n = 0; bio_read(bio, 1); n++) {\r
-               ;\r
-       }\r
-       return n;\r
-}\r
-\r
-static void t2_putnumpasses(opj_bio_t *bio, int n) {\r
-       if (n == 1) {\r
-               bio_write(bio, 0, 1);\r
-       } else if (n == 2) {\r
-               bio_write(bio, 2, 2);\r
-       } else if (n <= 5) {\r
-               bio_write(bio, 0xc | (n - 3), 4);\r
-       } else if (n <= 36) {\r
-               bio_write(bio, 0x1e0 | (n - 6), 9);\r
-       } else if (n <= 164) {\r
-               bio_write(bio, 0xff80 | (n - 37), 16);\r
-       }\r
-}\r
-\r
-static int t2_getnumpasses(opj_bio_t *bio) {\r
-       int n;\r
-       if (!bio_read(bio, 1))\r
-               return 1;\r
-       if (!bio_read(bio, 1))\r
-               return 2;\r
-       if ((n = bio_read(bio, 2)) != 3)\r
-               return (3 + n);\r
-       if ((n = bio_read(bio, 5)) != 31)\r
-               return (6 + n);\r
-       return (37 + bio_read(bio, 7));\r
-}\r
-\r
-static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_volume_info_t * volume_info, int tileno, opj_cp_t *cp) {\r
-       int bandno, cblkno;\r
-       unsigned char *sop = 0, *eph = 0;\r
-       unsigned char *c = dest;\r
-\r
-       int compno = pi->compno;        /* component value */\r
-       int resno  = pi->resno;         /* resolution level value */\r
-       int precno = pi->precno;        /* precinct value */\r
-       int layno  = pi->layno;         /* quality layer value */\r
-\r
-       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-       opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-       \r
-       opj_bio_t *bio = NULL;  /* BIO component */\r
-\r
-       /* <SOP 0xff91> */\r
-       if ((tcp->csty & J3D_CP_CSTY_SOP)) {\r
-               sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));\r
-               sop[0] = 255;\r
-               sop[1] = 145;\r
-               sop[2] = 0;\r
-               sop[3] = 4;\r
-               sop[4] = (volume_info) ? (volume_info->num % 65536) / 256 : (0 % 65536) / 256 ;\r
-               sop[5] = (volume_info) ? (volume_info->num % 65536) % 256 : (0 % 65536) % 256 ;\r
-               memcpy(c, sop, 6);\r
-               opj_free(sop);\r
-               c += 6;\r
-       } \r
-       /* </SOP> */\r
-       \r
-       if (!layno) {\r
-               for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                       opj_tcd_band_t *band = &res->bands[bandno];\r
-                       opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-                       tgt_reset(prc->incltree);\r
-                       tgt_reset(prc->imsbtree);\r
-                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                               cblk->numpasses = 0;\r
-                tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);\r
-                       }\r
-               }\r
-       }\r
-               \r
-       bio = bio_create();\r
-       bio_init_enc(bio, c, len);\r
-       bio_write(bio, 1, 1);           /* Empty header bit */\r
-       \r
-       /* Writing Packet header */\r
-       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-               opj_tcd_band_t *band = &res->bands[bandno];\r
-               opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                       opj_tcd_layer_t *layer = &cblk->layers[layno];\r
-                       if (!cblk->numpasses && layer->numpasses) {\r
-                tgt_setvalue(prc->incltree, cblkno, layno);\r
-                       }\r
-               }\r
-\r
-               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                       opj_tcd_layer_t *layer = &cblk->layers[layno];\r
-                       int increment = 0;\r
-                       int nump = 0;\r
-                       int len = 0, passno;\r
-                       /* cblk inclusion bits */\r
-                       if (!cblk->numpasses) {\r
-                               tgt_encode(bio, prc->incltree, cblkno, layno + 1);\r
-                       } else {\r
-                               bio_write(bio, layer->numpasses != 0, 1);\r
-                       }\r
-                       /* if cblk not included, go to the next cblk  */\r
-                       if (!layer->numpasses) {\r
-                               continue;\r
-                       }\r
-                       /* if first instance of cblk --> zero bit-planes information */\r
-                       if (!cblk->numpasses) {\r
-                               cblk->numlenbits = 3;\r
-                               tgt_encode(bio, prc->imsbtree, cblkno, 999);\r
-                       }\r
-                       /* number of coding passes included */\r
-                       t2_putnumpasses(bio, layer->numpasses);\r
-                       \r
-                       /* computation of the increase of the length indicator and insertion in the header     */\r
-                       for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {\r
-                               opj_tcd_pass_t *pass = &cblk->passes[passno];\r
-                               nump++;\r
-                               len += pass->len;\r
-                               if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {\r
-                                       increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));\r
-                                       len = 0;\r
-                                       nump = 0;\r
-                               }\r
-                       }\r
-                       t2_putcommacode(bio, increment);\r
-\r
-                       /* computation of the new Length indicator */\r
-                       cblk->numlenbits += increment;\r
-\r
-                       /* insertion of the codeword segment length */\r
-                       for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {\r
-                               opj_tcd_pass_t *pass = &cblk->passes[passno];\r
-                               nump++;\r
-                               len += pass->len;\r
-                               if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {\r
-                                       bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));\r
-                                       len = 0;\r
-                                       nump = 0;\r
-                               }\r
-                       }\r
-\r
-               }\r
-       }\r
-       \r
-       \r
-       if (bio_flush(bio)) {\r
-               return -999;            /* modified to eliminate longjmp !! */\r
-       }\r
-       \r
-       c += bio_numbytes(bio);\r
-\r
-       bio_destroy(bio);\r
-       \r
-       /* <EPH 0xff92> */\r
-       if (tcp->csty & J3D_CP_CSTY_EPH) {\r
-               eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));\r
-               eph[0] = 255;\r
-               eph[1] = 146;\r
-               memcpy(c, eph, 2);\r
-               opj_free(eph);\r
-               c += 2;\r
-       }\r
-       /* </EPH> */\r
-       \r
-       /* Writing the packet body */\r
-       \r
-       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-               opj_tcd_band_t *band = &res->bands[bandno];\r
-               opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                       opj_tcd_layer_t *layer = &cblk->layers[layno];\r
-                       if (!layer->numpasses) {\r
-                               continue;\r
-                       }\r
-                       if (c + layer->len > dest + len) {\r
-                               return -999;\r
-                       }\r
-                       \r
-                       memcpy(c, layer->data, layer->len);\r
-                       cblk->numpasses += layer->numpasses;\r
-                       c += layer->len;\r
-                       /* ADD for index Cfr. Marcela --> delta disto by packet */\r
-                       if(volume_info && volume_info->index_write && volume_info->index_on) {\r
-                               opj_tile_info_t *info_TL = &volume_info->tile[tileno];\r
-                               opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];\r
-                               info_PK->disto += layer->disto;\r
-                               if (volume_info->D_max < info_PK->disto) {\r
-                                       volume_info->D_max = info_PK->disto;\r
-                               }\r
-                       }\r
-                       /* </ADD> */\r
-               }\r
-       }\r
-       \r
-       return (c - dest);\r
-}\r
-\r
-static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {\r
-       seg->numpasses = 0;\r
-       seg->len = 0;\r
-       if (cblksty & J3D_CCP_CBLKSTY_TERMALL) {\r
-               seg->maxpasses = 1;\r
-       }\r
-       else if (cblksty & J3D_CCP_CBLKSTY_LAZY) {\r
-               if (first) {\r
-                       seg->maxpasses = 10;\r
-               } else {\r
-                       seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;\r
-               }\r
-       } else {\r
-               seg->maxpasses = 109;\r
-       }\r
-}\r
-\r
-int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi) {\r
-       int bandno, cblkno;\r
-       unsigned char *c = src;\r
-\r
-       opj_cp_t *cp = t2->cp;\r
-\r
-       int compno = pi->compno;        /* component value */\r
-       int resno  = pi->resno;         /* resolution level value */\r
-       int precno = pi->precno;        /* precinct value */\r
-       int layno  = pi->layno;         /* quality layer value */\r
-\r
-       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-       opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-       \r
-       unsigned char *hd = NULL;\r
-       int present;\r
-       \r
-       opj_bio_t *bio = NULL;  /* BIO component */\r
-       \r
-       if (layno == 0) {\r
-               for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                       opj_tcd_band_t *band = &res->bands[bandno];\r
-                       opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-                       \r
-                       if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;\r
-\r
-                       tgt_reset(prc->incltree);\r
-                       tgt_reset(prc->imsbtree);\r
-                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                               cblk->numsegs = 0;\r
-                       }\r
-               }\r
-       }\r
-       \r
-       /* SOP markers */\r
-       \r
-       if (tcp->csty & J3D_CP_CSTY_SOP) {\r
-               if ((*c) != 0xff || (*(c + 1) != 0x91)) {\r
-                       opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");\r
-               } else {\r
-                       c += 6;\r
-               }\r
-               \r
-               /** TODO : check the Nsop value */\r
-       }\r
-       \r
-       /* \r
-       When the marker PPT/PPM is used the packet header are store in PPT/PPM marker\r
-       This part deal with this caracteristic\r
-       step 1: Read packet header in the saved structure\r
-       step 2: Return to codestream for decoding \r
-       */\r
-\r
-       bio = bio_create();\r
-       \r
-       if (cp->ppm == 1) {             /* PPM */\r
-               hd = cp->ppm_data;\r
-               bio_init_dec(bio, hd, cp->ppm_len);\r
-       } else if (tcp->ppt == 1) {     /* PPT */\r
-               hd = tcp->ppt_data;\r
-               bio_init_dec(bio, hd, tcp->ppt_len);\r
-       } else {                        /* Normal Case */\r
-               hd = c;\r
-               bio_init_dec(bio, hd, src+len-hd);\r
-       }\r
-       \r
-       present = bio_read(bio, 1);\r
-       \r
-       if (!present) {\r
-               bio_inalign(bio);\r
-               hd += bio_numbytes(bio);\r
-               bio_destroy(bio);\r
-               \r
-               /* EPH markers */\r
-               \r
-               if (tcp->csty & J3D_CP_CSTY_EPH) {\r
-                       if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {\r
-                               printf("Error : expected EPH marker\n");\r
-                       } else {\r
-                               hd += 2;\r
-                       }\r
-               }\r
-               \r
-               if (cp->ppm == 1) {             /* PPM case */\r
-                       cp->ppm_len += cp->ppm_data-hd;\r
-                       cp->ppm_data = hd;\r
-                       return (c - src);\r
-               }\r
-               if (tcp->ppt == 1) {    /* PPT case */\r
-                       tcp->ppt_len+=tcp->ppt_data-hd;\r
-                       tcp->ppt_data = hd;\r
-                       return (c - src);\r
-               }\r
-               \r
-               return (hd - src);\r
-       }\r
-       \r
-       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-               opj_tcd_band_t *band = &res->bands[bandno];\r
-               opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-               \r
-               if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;\r
-               \r
-               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                       int included, increment, n;\r
-                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                       opj_tcd_seg_t *seg = NULL;\r
-                       /* if cblk not yet included before --> inclusion tagtree */\r
-                       if (!cblk->numsegs) {\r
-                included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);\r
-                               /* else one bit */\r
-                       } else {\r
-                               included = bio_read(bio, 1);\r
-                       }\r
-                       /* if cblk not included */\r
-                       if (!included) {\r
-                               cblk->numnewpasses = 0;\r
-                               continue;\r
-                       }\r
-                       /* if cblk not yet included --> zero-bitplane tagtree */\r
-                       if (!cblk->numsegs) {\r
-                               int i, numimsbs;\r
-                               for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++);\r
-                               numimsbs = i - 1;\r
-                               cblk->numbps = band->numbps - numimsbs;\r
-                               cblk->numlenbits = 3;\r
-                       }\r
-                       /* number of coding passes */\r
-                       cblk->numnewpasses = t2_getnumpasses(bio);\r
-                       increment = t2_getcommacode(bio);\r
-                       /* length indicator increment */\r
-                       cblk->numlenbits += increment;\r
-                       if (!cblk->numsegs) {\r
-                               seg = &cblk->segs[0];\r
-                               t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);\r
-                       } else {\r
-                               seg = &cblk->segs[cblk->numsegs - 1];\r
-                               if (seg->numpasses == seg->maxpasses) {\r
-                                       t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);\r
-                               }\r
-                       }\r
-                       n = cblk->numnewpasses;\r
-                       \r
-                       do {\r
-                               seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);\r
-                               seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));\r
-                               n -= seg->numnewpasses;\r
-                               if (n > 0) {\r
-                                       t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);\r
-                               }\r
-                       } while (n > 0);\r
-               }\r
-       }\r
-       \r
-       if (bio_inalign(bio)) {\r
-               bio_destroy(bio);\r
-               return -999;\r
-       }\r
-       \r
-       hd += bio_numbytes(bio);\r
-       bio_destroy(bio);\r
-       \r
-       /* EPH markers */\r
-       if (tcp->csty & J3D_CP_CSTY_EPH) {\r
-               if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {\r
-                       opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");\r
-               } else {\r
-                       hd += 2;\r
-               }\r
-       }\r
-       \r
-       if (cp->ppm==1) {\r
-               cp->ppm_len+=cp->ppm_data-hd;\r
-               cp->ppm_data = hd;\r
-       } else if (tcp->ppt == 1) {\r
-               tcp->ppt_len+=tcp->ppt_data-hd;\r
-               tcp->ppt_data = hd;\r
-       } else {\r
-               c=hd;\r
-       }\r
-       \r
-       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-               opj_tcd_band_t *band = &res->bands[bandno];\r
-               opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-               \r
-               if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;             \r
-\r
-               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                       opj_tcd_seg_t *seg = NULL;\r
-                       if (!cblk->numnewpasses)\r
-                               continue;\r
-                       if (!cblk->numsegs) {\r
-                               seg = &cblk->segs[0];\r
-                               cblk->numsegs++;\r
-                               cblk->len = 0;\r
-                       } else {\r
-                               seg = &cblk->segs[cblk->numsegs - 1];\r
-                               if (seg->numpasses == seg->maxpasses) {\r
-                                       seg++;\r
-                                       cblk->numsegs++;\r
-                               }\r
-                       }\r
-                       \r
-                       do {\r
-                               if (c + seg->newlen > src + len) {\r
-                                       return -999;\r
-                               }\r
-                               \r
-                               memcpy(cblk->data + cblk->len, c, seg->newlen);\r
-                               if (seg->numpasses == 0) {\r
-                                       seg->data = cblk->data + cblk->len;\r
-                               }\r
-                               c += seg->newlen;\r
-                               cblk->len += seg->newlen;\r
-                               seg->len += seg->newlen;\r
-                               seg->numpasses += seg->numnewpasses;\r
-                               cblk->numnewpasses -= seg->numnewpasses;\r
-                               if (cblk->numnewpasses > 0) {\r
-                                       seg++;\r
-                                       cblk->numsegs++;\r
-                               }\r
-                       } while (cblk->numnewpasses > 0);\r
-               }\r
-       }\r
-       \r
-       return (c - src);\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_volume_info_t *volume_info) {\r
-       unsigned char *c = dest;\r
-       int e = 0;\r
-       opj_pi_iterator_t *pi = NULL;\r
-       int pino;\r
-\r
-       opj_volume_t *volume = t2->volume;\r
-       opj_cp_t *cp = t2->cp;\r
-       \r
-       /* create a packet iterator */\r
-       pi = pi_create(volume, cp, tileno);\r
-       if(!pi) {\r
-               fprintf(stdout,"[ERROR] Failed to create a pi structure\n");\r
-               return -999;\r
-       }\r
-       \r
-       if(volume_info) {\r
-               volume_info->num = 0;\r
-       }\r
-       \r
-       for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {\r
-               while (pi_next(&pi[pino])) {\r
-                       if (pi[pino].layno < maxlayers) {\r
-                               e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, volume_info, tileno, cp);\r
-                               /*opj_event_msg(t2->cinfo, EVT_INFO, "  t2_encode_packet: %d bytes coded\n",e);*/\r
-                               if (e == -999) {\r
-                                       break;\r
-                               } else {\r
-                                       c += e;\r
-                               }\r
-                               \r
-                               /* INDEX >> */\r
-                               if(volume_info && volume_info->index_on) {\r
-                                       if(volume_info->index_write) {\r
-                                               opj_tile_info_t *info_TL = &volume_info->tile[tileno];\r
-                                               opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];\r
-                                               if (!volume_info->num) {\r
-                                                       info_PK->start_pos = info_TL->end_header + 1;\r
-                                               } else {\r
-                                                       info_PK->start_pos = info_TL->packet[volume_info->num - 1].end_pos + 1;\r
-                                               }\r
-                                               info_PK->end_pos = info_PK->start_pos + e - 1;\r
-                                       }\r
-\r
-                                       volume_info->num++;\r
-                               }\r
-                               /* << INDEX */\r
-                       }\r
-               }\r
-       }\r
-\r
-       /* don't forget to release pi */\r
-       pi_destroy(pi, cp, tileno);\r
-       \r
-       if (e == -999) {\r
-               return e;\r
-       }\r
-\r
-    return (c - dest);\r
-}\r
-\r
-int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {\r
-       unsigned char *c = src;\r
-       opj_pi_iterator_t *pi;\r
-       int pino, e = 0;\r
-       int n = 0,i;\r
-\r
-       opj_volume_t *volume = t2->volume;\r
-       opj_cp_t *cp = t2->cp;\r
-       \r
-       /* create a packet iterator */\r
-       pi = pi_create(volume, cp, tileno);\r
-       if(!pi) {\r
-               /* TODO: throw an error */\r
-               return -999;\r
-       }\r
-       \r
-       for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {\r
-               while (pi_next(&pi[pino])) {\r
-                       if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {\r
-                               e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);\r
-                       } else {\r
-                               e = 0;\r
-                       }\r
-                       \r
-                       /* progression in resolution */\r
-                       for (i = 0; i < 3; i++){\r
-                volume->comps[pi[pino].compno].resno_decoded[i] = (e > 0) ? int_max(pi[pino].resno, volume->comps[pi[pino].compno].resno_decoded[i]) : volume->comps[pi[pino].compno].resno_decoded[i];\r
-                       }\r
-                       n++;\r
-                       \r
-                       if (e == -999) {                /* ADD */\r
-                               break;\r
-                       } else {\r
-                               opj_event_msg(t2->cinfo, EVT_INFO, "  t2_decode_packet: %d bytes decoded\n",e);\r
-                               c += e;\r
-                       }\r
-               }\r
-       }\r
-\r
-       /* don't forget to release pi */\r
-       pi_destroy(pi, cp, tileno);\r
-       \r
-       if (e == -999) {\r
-               return e;\r
-       }\r
-       \r
-    return (c - src);\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp) {\r
-       /* create the tcd structure */\r
-       opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));\r
-       if(!t2) return NULL;\r
-       t2->cinfo = cinfo;\r
-       t2->volume = volume;\r
-       t2->cp = cp;\r
-\r
-       return t2;\r
-}\r
-\r
-void t2_destroy(opj_t2_t *t2) {\r
-       if(t2) {\r
-               opj_free(t2);\r
-       }\r
-}\r
-\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup T2 T2 - Implementation of a tier-2 coding */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+static void t2_putcommacode(opj_bio_t *bio, int n);
+static int t2_getcommacode(opj_bio_t *bio);
+/**
+Variable length code for signalling delta Zil (truncation point)
+@param bio Bit Input/Output component
+@param n delta Zil
+*/
+static void t2_putnumpasses(opj_bio_t *bio, int n);
+static int t2_getnumpasses(opj_bio_t *bio);
+/**
+Encode a packet of a tile to a destination buffer
+@param tile Tile for which to write the packets
+@param tcp Tile coding parameters
+@param pi Packet identity
+@param dest Destination buffer
+@param len Length of the destination buffer
+@param volume_info Structure to create an index file
+@param tileno Number of the tile encoded
+@param cp Coding parameters
+@return Number of bytes encoded from the packet
+*/
+static int t2_encode_packet(opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_volume_info_t *volume_info, int tileno, opj_cp_t *cp);
+/**
+Initialize the segment decoder
+@param seg Segment instance
+@param cblksty Codeblock style
+@param first Is first segment
+*/
+static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);
+/**
+Decode a packet of a tile from a source buffer
+@param t2 T2 handle
+@param src Source buffer
+@param len Length of the source buffer
+@param tile Tile for which to write the packets
+@param tcp Tile coding parameters
+@param pi Packet identity
+@return Number of bytes decoded from the packet
+*/
+int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi);
+
+/*@}*/
+
+/*@}*/
+
+/* ----------------------------------------------------------------------- */
+
+/* #define RESTART 0x04 */
+static void t2_putcommacode(opj_bio_t *bio, int n) {
+       while (--n >= 0) {
+               bio_write(bio, 1, 1);
+       }
+       bio_write(bio, 0, 1);
+}
+
+static int t2_getcommacode(opj_bio_t *bio) {
+       int n;
+       for (n = 0; bio_read(bio, 1); n++) {
+               ;
+       }
+       return n;
+}
+
+static void t2_putnumpasses(opj_bio_t *bio, int n) {
+       if (n == 1) {
+               bio_write(bio, 0, 1);
+       } else if (n == 2) {
+               bio_write(bio, 2, 2);
+       } else if (n <= 5) {
+               bio_write(bio, 0xc | (n - 3), 4);
+       } else if (n <= 36) {
+               bio_write(bio, 0x1e0 | (n - 6), 9);
+       } else if (n <= 164) {
+               bio_write(bio, 0xff80 | (n - 37), 16);
+       }
+}
+
+static int t2_getnumpasses(opj_bio_t *bio) {
+       int n;
+       if (!bio_read(bio, 1))
+               return 1;
+       if (!bio_read(bio, 1))
+               return 2;
+       if ((n = bio_read(bio, 2)) != 3)
+               return (3 + n);
+       if ((n = bio_read(bio, 5)) != 31)
+               return (6 + n);
+       return (37 + bio_read(bio, 7));
+}
+
+static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_volume_info_t * volume_info, int tileno, opj_cp_t *cp) {
+       int bandno, cblkno;
+       unsigned char *sop = 0, *eph = 0;
+       unsigned char *c = dest;
+
+       int compno = pi->compno;        /* component value */
+       int resno  = pi->resno;         /* resolution level value */
+       int precno = pi->precno;        /* precinct value */
+       int layno  = pi->layno;         /* quality layer value */
+
+       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+       
+       opj_bio_t *bio = NULL;  /* BIO component */
+
+       /* <SOP 0xff91> */
+       if ((tcp->csty & J3D_CP_CSTY_SOP)) {
+               sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
+               sop[0] = 255;
+               sop[1] = 145;
+               sop[2] = 0;
+               sop[3] = 4;
+               sop[4] = (volume_info) ? (volume_info->num % 65536) / 256 : (0 % 65536) / 256 ;
+               sop[5] = (volume_info) ? (volume_info->num % 65536) % 256 : (0 % 65536) % 256 ;
+               memcpy(c, sop, 6);
+               opj_free(sop);
+               c += 6;
+       } 
+       /* </SOP> */
+       
+       if (!layno) {
+               for (bandno = 0; bandno < res->numbands; bandno++) {
+                       opj_tcd_band_t *band = &res->bands[bandno];
+                       opj_tcd_precinct_t *prc = &band->precincts[precno];
+                       tgt_reset(prc->incltree);
+                       tgt_reset(prc->imsbtree);
+                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                               cblk->numpasses = 0;
+                tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
+                       }
+               }
+       }
+               
+       bio = bio_create();
+       bio_init_enc(bio, c, len);
+       bio_write(bio, 1, 1);           /* Empty header bit */
+       
+       /* Writing Packet header */
+       for (bandno = 0; bandno < res->numbands; bandno++) {
+               opj_tcd_band_t *band = &res->bands[bandno];
+               opj_tcd_precinct_t *prc = &band->precincts[precno];
+               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                       opj_tcd_layer_t *layer = &cblk->layers[layno];
+                       if (!cblk->numpasses && layer->numpasses) {
+                tgt_setvalue(prc->incltree, cblkno, layno);
+                       }
+               }
+
+               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                       opj_tcd_layer_t *layer = &cblk->layers[layno];
+                       int increment = 0;
+                       int nump = 0;
+                       int len = 0, passno;
+                       /* cblk inclusion bits */
+                       if (!cblk->numpasses) {
+                               tgt_encode(bio, prc->incltree, cblkno, layno + 1);
+                       } else {
+                               bio_write(bio, layer->numpasses != 0, 1);
+                       }
+                       /* if cblk not included, go to the next cblk  */
+                       if (!layer->numpasses) {
+                               continue;
+                       }
+                       /* if first instance of cblk --> zero bit-planes information */
+                       if (!cblk->numpasses) {
+                               cblk->numlenbits = 3;
+                               tgt_encode(bio, prc->imsbtree, cblkno, 999);
+                       }
+                       /* number of coding passes included */
+                       t2_putnumpasses(bio, layer->numpasses);
+                       
+                       /* computation of the increase of the length indicator and insertion in the header     */
+                       for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
+                               opj_tcd_pass_t *pass = &cblk->passes[passno];
+                               nump++;
+                               len += pass->len;
+                               if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
+                                       increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
+                                       len = 0;
+                                       nump = 0;
+                               }
+                       }
+                       t2_putcommacode(bio, increment);
+
+                       /* computation of the new Length indicator */
+                       cblk->numlenbits += increment;
+
+                       /* insertion of the codeword segment length */
+                       for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
+                               opj_tcd_pass_t *pass = &cblk->passes[passno];
+                               nump++;
+                               len += pass->len;
+                               if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
+                                       bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
+                                       len = 0;
+                                       nump = 0;
+                               }
+                       }
+
+               }
+       }
+       
+       
+       if (bio_flush(bio)) {
+               return -999;            /* modified to eliminate longjmp !! */
+       }
+       
+       c += bio_numbytes(bio);
+
+       bio_destroy(bio);
+       
+       /* <EPH 0xff92> */
+       if (tcp->csty & J3D_CP_CSTY_EPH) {
+               eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
+               eph[0] = 255;
+               eph[1] = 146;
+               memcpy(c, eph, 2);
+               opj_free(eph);
+               c += 2;
+       }
+       /* </EPH> */
+       
+       /* Writing the packet body */
+       
+       for (bandno = 0; bandno < res->numbands; bandno++) {
+               opj_tcd_band_t *band = &res->bands[bandno];
+               opj_tcd_precinct_t *prc = &band->precincts[precno];
+               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                       opj_tcd_layer_t *layer = &cblk->layers[layno];
+                       if (!layer->numpasses) {
+                               continue;
+                       }
+                       if (c + layer->len > dest + len) {
+                               return -999;
+                       }
+                       
+                       memcpy(c, layer->data, layer->len);
+                       cblk->numpasses += layer->numpasses;
+                       c += layer->len;
+                       /* ADD for index Cfr. Marcela --> delta disto by packet */
+                       if(volume_info && volume_info->index_write && volume_info->index_on) {
+                               opj_tile_info_t *info_TL = &volume_info->tile[tileno];
+                               opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
+                               info_PK->disto += layer->disto;
+                               if (volume_info->D_max < info_PK->disto) {
+                                       volume_info->D_max = info_PK->disto;
+                               }
+                       }
+                       /* </ADD> */
+               }
+       }
+       
+       return (c - dest);
+}
+
+static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
+       seg->numpasses = 0;
+       seg->len = 0;
+       if (cblksty & J3D_CCP_CBLKSTY_TERMALL) {
+               seg->maxpasses = 1;
+       }
+       else if (cblksty & J3D_CCP_CBLKSTY_LAZY) {
+               if (first) {
+                       seg->maxpasses = 10;
+               } else {
+                       seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
+               }
+       } else {
+               seg->maxpasses = 109;
+       }
+}
+
+int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi) {
+       int bandno, cblkno;
+       unsigned char *c = src;
+
+       opj_cp_t *cp = t2->cp;
+
+       int compno = pi->compno;        /* component value */
+       int resno  = pi->resno;         /* resolution level value */
+       int precno = pi->precno;        /* precinct value */
+       int layno  = pi->layno;         /* quality layer value */
+
+       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+       
+       unsigned char *hd = NULL;
+       int present;
+       
+       opj_bio_t *bio = NULL;  /* BIO component */
+       
+       if (layno == 0) {
+               for (bandno = 0; bandno < res->numbands; bandno++) {
+                       opj_tcd_band_t *band = &res->bands[bandno];
+                       opj_tcd_precinct_t *prc = &band->precincts[precno];
+                       
+                       if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
+
+                       tgt_reset(prc->incltree);
+                       tgt_reset(prc->imsbtree);
+                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                               cblk->numsegs = 0;
+                       }
+               }
+       }
+       
+       /* SOP markers */
+       
+       if (tcp->csty & J3D_CP_CSTY_SOP) {
+               if ((*c) != 0xff || (*(c + 1) != 0x91)) {
+                       opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
+               } else {
+                       c += 6;
+               }
+               
+               /** TODO : check the Nsop value */
+       }
+       
+       /* 
+       When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
+       This part deal with this caracteristic
+       step 1: Read packet header in the saved structure
+       step 2: Return to codestream for decoding 
+       */
+
+       bio = bio_create();
+       
+       if (cp->ppm == 1) {             /* PPM */
+               hd = cp->ppm_data;
+               bio_init_dec(bio, hd, cp->ppm_len);
+       } else if (tcp->ppt == 1) {     /* PPT */
+               hd = tcp->ppt_data;
+               bio_init_dec(bio, hd, tcp->ppt_len);
+       } else {                        /* Normal Case */
+               hd = c;
+               bio_init_dec(bio, hd, src+len-hd);
+       }
+       
+       present = bio_read(bio, 1);
+       
+       if (!present) {
+               bio_inalign(bio);
+               hd += bio_numbytes(bio);
+               bio_destroy(bio);
+               
+               /* EPH markers */
+               
+               if (tcp->csty & J3D_CP_CSTY_EPH) {
+                       if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
+                               printf("Error : expected EPH marker\n");
+                       } else {
+                               hd += 2;
+                       }
+               }
+               
+               if (cp->ppm == 1) {             /* PPM case */
+                       cp->ppm_len += cp->ppm_data-hd;
+                       cp->ppm_data = hd;
+                       return (c - src);
+               }
+               if (tcp->ppt == 1) {    /* PPT case */
+                       tcp->ppt_len+=tcp->ppt_data-hd;
+                       tcp->ppt_data = hd;
+                       return (c - src);
+               }
+               
+               return (hd - src);
+       }
+       
+       for (bandno = 0; bandno < res->numbands; bandno++) {
+               opj_tcd_band_t *band = &res->bands[bandno];
+               opj_tcd_precinct_t *prc = &band->precincts[precno];
+               
+               if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
+               
+               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                       int included, increment, n;
+                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                       opj_tcd_seg_t *seg = NULL;
+                       /* if cblk not yet included before --> inclusion tagtree */
+                       if (!cblk->numsegs) {
+                included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
+                               /* else one bit */
+                       } else {
+                               included = bio_read(bio, 1);
+                       }
+                       /* if cblk not included */
+                       if (!included) {
+                               cblk->numnewpasses = 0;
+                               continue;
+                       }
+                       /* if cblk not yet included --> zero-bitplane tagtree */
+                       if (!cblk->numsegs) {
+                               int i, numimsbs;
+                               for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++);
+                               numimsbs = i - 1;
+                               cblk->numbps = band->numbps - numimsbs;
+                               cblk->numlenbits = 3;
+                       }
+                       /* number of coding passes */
+                       cblk->numnewpasses = t2_getnumpasses(bio);
+                       increment = t2_getcommacode(bio);
+                       /* length indicator increment */
+                       cblk->numlenbits += increment;
+                       if (!cblk->numsegs) {
+                               seg = &cblk->segs[0];
+                               t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
+                       } else {
+                               seg = &cblk->segs[cblk->numsegs - 1];
+                               if (seg->numpasses == seg->maxpasses) {
+                                       t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
+                               }
+                       }
+                       n = cblk->numnewpasses;
+                       
+                       do {
+                               seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
+                               seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
+                               n -= seg->numnewpasses;
+                               if (n > 0) {
+                                       t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
+                               }
+                       } while (n > 0);
+               }
+       }
+       
+       if (bio_inalign(bio)) {
+               bio_destroy(bio);
+               return -999;
+       }
+       
+       hd += bio_numbytes(bio);
+       bio_destroy(bio);
+       
+       /* EPH markers */
+       if (tcp->csty & J3D_CP_CSTY_EPH) {
+               if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
+                       opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
+               } else {
+                       hd += 2;
+               }
+       }
+       
+       if (cp->ppm==1) {
+               cp->ppm_len+=cp->ppm_data-hd;
+               cp->ppm_data = hd;
+       } else if (tcp->ppt == 1) {
+               tcp->ppt_len+=tcp->ppt_data-hd;
+               tcp->ppt_data = hd;
+       } else {
+               c=hd;
+       }
+       
+       for (bandno = 0; bandno < res->numbands; bandno++) {
+               opj_tcd_band_t *band = &res->bands[bandno];
+               opj_tcd_precinct_t *prc = &band->precincts[precno];
+               
+               if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;             
+
+               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                       opj_tcd_seg_t *seg = NULL;
+                       if (!cblk->numnewpasses)
+                               continue;
+                       if (!cblk->numsegs) {
+                               seg = &cblk->segs[0];
+                               cblk->numsegs++;
+                               cblk->len = 0;
+                       } else {
+                               seg = &cblk->segs[cblk->numsegs - 1];
+                               if (seg->numpasses == seg->maxpasses) {
+                                       seg++;
+                                       cblk->numsegs++;
+                               }
+                       }
+                       
+                       do {
+                               if (c + seg->newlen > src + len) {
+                                       return -999;
+                               }
+                               
+                               memcpy(cblk->data + cblk->len, c, seg->newlen);
+                               if (seg->numpasses == 0) {
+                                       seg->data = cblk->data + cblk->len;
+                               }
+                               c += seg->newlen;
+                               cblk->len += seg->newlen;
+                               seg->len += seg->newlen;
+                               seg->numpasses += seg->numnewpasses;
+                               cblk->numnewpasses -= seg->numnewpasses;
+                               if (cblk->numnewpasses > 0) {
+                                       seg++;
+                                       cblk->numsegs++;
+                               }
+                       } while (cblk->numnewpasses > 0);
+               }
+       }
+       
+       return (c - src);
+}
+
+/* ----------------------------------------------------------------------- */
+
+int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_volume_info_t *volume_info) {
+       unsigned char *c = dest;
+       int e = 0;
+       opj_pi_iterator_t *pi = NULL;
+       int pino;
+
+       opj_volume_t *volume = t2->volume;
+       opj_cp_t *cp = t2->cp;
+       
+       /* create a packet iterator */
+       pi = pi_create(volume, cp, tileno);
+       if(!pi) {
+               fprintf(stdout,"[ERROR] Failed to create a pi structure\n");
+               return -999;
+       }
+       
+       if(volume_info) {
+               volume_info->num = 0;
+       }
+       
+       for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
+               while (pi_next(&pi[pino])) {
+                       if (pi[pino].layno < maxlayers) {
+                               e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, volume_info, tileno, cp);
+                               /*opj_event_msg(t2->cinfo, EVT_INFO, "  t2_encode_packet: %d bytes coded\n",e);*/
+                               if (e == -999) {
+                                       break;
+                               } else {
+                                       c += e;
+                               }
+                               
+                               /* INDEX >> */
+                               if(volume_info && volume_info->index_on) {
+                                       if(volume_info->index_write) {
+                                               opj_tile_info_t *info_TL = &volume_info->tile[tileno];
+                                               opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
+                                               if (!volume_info->num) {
+                                                       info_PK->start_pos = info_TL->end_header + 1;
+                                               } else {
+                                                       info_PK->start_pos = info_TL->packet[volume_info->num - 1].end_pos + 1;
+                                               }
+                                               info_PK->end_pos = info_PK->start_pos + e - 1;
+                                       }
+
+                                       volume_info->num++;
+                               }
+                               /* << INDEX */
+                       }
+               }
+       }
+
+       /* don't forget to release pi */
+       pi_destroy(pi, cp, tileno);
+       
+       if (e == -999) {
+               return e;
+       }
+
+    return (c - dest);
+}
+
+int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
+       unsigned char *c = src;
+       opj_pi_iterator_t *pi;
+       int pino, e = 0;
+       int n = 0,i;
+
+       opj_volume_t *volume = t2->volume;
+       opj_cp_t *cp = t2->cp;
+       
+       /* create a packet iterator */
+       pi = pi_create(volume, cp, tileno);
+       if(!pi) {
+               /* TODO: throw an error */
+               return -999;
+       }
+       
+       for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
+               while (pi_next(&pi[pino])) {
+                       if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
+                               e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
+                       } else {
+                               e = 0;
+                       }
+                       
+                       /* progression in resolution */
+                       for (i = 0; i < 3; i++){
+                volume->comps[pi[pino].compno].resno_decoded[i] = (e > 0) ? int_max(pi[pino].resno, volume->comps[pi[pino].compno].resno_decoded[i]) : volume->comps[pi[pino].compno].resno_decoded[i];
+                       }
+                       n++;
+                       
+                       if (e == -999) {                /* ADD */
+                               break;
+                       } else {
+                               opj_event_msg(t2->cinfo, EVT_INFO, "  t2_decode_packet: %d bytes decoded\n",e);
+                               c += e;
+                       }
+               }
+       }
+
+       /* don't forget to release pi */
+       pi_destroy(pi, cp, tileno);
+       
+       if (e == -999) {
+               return e;
+       }
+       
+    return (c - src);
+}
+
+/* ----------------------------------------------------------------------- */
+
+opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp) {
+       /* create the tcd structure */
+       opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
+       if(!t2) return NULL;
+       t2->cinfo = cinfo;
+       t2->volume = volume;
+       t2->cp = cp;
+
+       return t2;
+}
+
+void t2_destroy(opj_t2_t *t2) {
+       if(t2) {
+               opj_free(t2);
+       }
+}
+
index b937290b50c1a5cef218702076721586cb817636..9aaecb14dc05b51d6762cc0a5c3c4a812b29a0ed 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-#ifndef __T2_H\r
-#define __T2_H\r
-/**\r
-@file t2.h\r
-@brief Implementation of a tier-2 coding (packetization of code-block data) (T2)\r
-\r
-*/\r
-\r
-/** @defgroup T2 T2 - Implementation of a tier-2 coding */\r
-/*@{*/\r
-\r
-/**\r
-Tier-2 coding\r
-*/\r
-typedef struct opj_t2 {\r
-/** Codec context */\r
-       opj_common_ptr cinfo;   \r
-/** Encoding: pointer to the src volume. Decoding: pointer to the dst volume. */\r
-       opj_volume_t *volume;   \r
-/** Pointer to the volume coding parameters */\r
-       opj_cp_t *cp;                   \r
-} opj_t2_t;\r
-\r
-/** @name Funciones generales */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/**\r
-Encode the packets of a tile to a destination buffer\r
-@param t2 T2 handle\r
-@param tileno number of the tile encoded\r
-@param tile the tile for which to write the packets\r
-@param maxlayers maximum number of layers\r
-@param dest the destination buffer\r
-@param len the length of the destination buffer\r
-@param volume_info structure to create an index file\r
-@return Number of bytes written from packets\r
-*/\r
-int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_volume_info_t *volume_info);\r
-\r
-/**\r
-Decode the packets of a tile from a source buffer\r
-@param t2 T2 handle\r
-@param src the source buffer\r
-@param len length of the source buffer\r
-@param tileno number that identifies the tile for which to decode the packets\r
-@param tile tile for which to decode the packets\r
-@return Number of bytes read from packets\r
- */\r
-int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile);\r
-\r
-/**\r
-Create a T2 handle\r
-@param cinfo Codec context info\r
-@param volume Source or destination volume\r
-@param cp Volume coding parameters\r
-@return Returns a new T2 handle if successful, returns NULL otherwise\r
-*/\r
-opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp);\r
-/**\r
-Destroy a T2 handle\r
-@param t2 T2 handle to destroy\r
-*/\r
-void t2_destroy(opj_t2_t *t2);\r
-\r
-/* ----------------------------------------------------------------------- */\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-#endif /* __T2_H */\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __T2_H
+#define __T2_H
+/**
+@file t2.h
+@brief Implementation of a tier-2 coding (packetization of code-block data) (T2)
+
+*/
+
+/** @defgroup T2 T2 - Implementation of a tier-2 coding */
+/*@{*/
+
+/**
+Tier-2 coding
+*/
+typedef struct opj_t2 {
+/** Codec context */
+       opj_common_ptr cinfo;   
+/** Encoding: pointer to the src volume. Decoding: pointer to the dst volume. */
+       opj_volume_t *volume;   
+/** Pointer to the volume coding parameters */
+       opj_cp_t *cp;                   
+} opj_t2_t;
+
+/** @name Funciones generales */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+Encode the packets of a tile to a destination buffer
+@param t2 T2 handle
+@param tileno number of the tile encoded
+@param tile the tile for which to write the packets
+@param maxlayers maximum number of layers
+@param dest the destination buffer
+@param len the length of the destination buffer
+@param volume_info structure to create an index file
+@return Number of bytes written from packets
+*/
+int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_volume_info_t *volume_info);
+
+/**
+Decode the packets of a tile from a source buffer
+@param t2 T2 handle
+@param src the source buffer
+@param len length of the source buffer
+@param tileno number that identifies the tile for which to decode the packets
+@param tile tile for which to decode the packets
+@return Number of bytes read from packets
+ */
+int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile);
+
+/**
+Create a T2 handle
+@param cinfo Codec context info
+@param volume Source or destination volume
+@param cp Volume coding parameters
+@return Returns a new T2 handle if successful, returns NULL otherwise
+*/
+opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp);
+/**
+Destroy a T2 handle
+@param t2 T2 handle to destroy
+*/
+void t2_destroy(opj_t2_t *t2);
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __T2_H */
index 84c1ed1e1084f0599f2012385263c341a879e982..e13a4af63ed659415d531224f6c9f4c1323ff711 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * Copyright (c) 2006, M�nica D�ez, LPI-UVA, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_volume_t * vol) {\r
-       int tileno, compno, resno, bandno, precno, cblkno;\r
-\r
-       fprintf(fd, "volume {\n");\r
-       fprintf(fd, "  tw=%d, th=%d, tl=%d, x0=%d x1=%d y0=%d y1=%d z0=%d z1=%d\n", \r
-               vol->tw, vol->th, vol->tl, tcd->volume->x0, tcd->volume->x1, tcd->volume->y0, tcd->volume->y1, tcd->volume->z0, tcd->volume->z1);\r
-\r
-       for (tileno = 0; tileno < vol->th * vol->tw * vol->tl; tileno++) {\r
-               opj_tcd_tile_t *tile = &tcd->tcd_volume->tiles[tileno];\r
-               fprintf(fd, "  tile {\n");\r
-               fprintf(fd, "    x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numcomps=%d\n",\r
-                       tile->x0, tile->y0, tile->z0, tile->x1, tile->y1, tile->z1, tile->numcomps);\r
-               for (compno = 0; compno < tile->numcomps; compno++) {\r
-                       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-                       fprintf(fd, "    tilecomp %d {\n",compno);\r
-                       fprintf(fd,     "     x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numresx=%d, numresy=%d, numresz=%d\n",\r
-                               tilec->x0, tilec->y0, tilec->z0, tilec->x1, tilec->y1, tilec->z1, tilec->numresolution[0], tilec->numresolution[1], tilec->numresolution[2]);\r
-                       for (resno = 0; resno < tilec->numresolution[0]; resno++) {\r
-                               opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-                               fprintf(fd, "     res %d{\n",resno);\r
-                               fprintf(fd,"      x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, pw=%d, ph=%d, pl=%d, numbands=%d\n",\r
-                                       res->x0, res->y0, res->z0, res->x1, res->y1, res->z1, res->prctno[0], res->prctno[1], res->prctno[2], res->numbands);\r
-                               for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                                       opj_tcd_band_t *band = &res->bands[bandno];\r
-                                       fprintf(fd, "       band %d{\n", bandno);\r
-                                       fprintf(fd,     "                x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, stepsize=%f, numbps=%d\n",\r
-                                               band->x0, band->y0, band->z0, band->x1, band->y1, band->z1, band->stepsize, band->numbps);\r
-                                       for (precno = 0; precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); precno++) {\r
-                                               opj_tcd_precinct_t *prec = &band->precincts[precno];\r
-                                               fprintf(fd, "             prec %d{\n",precno);\r
-                                               fprintf(fd,     "                  x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, cw=%d, ch=%d, cl=%d,\n",\r
-                                                       prec->x0, prec->y0, prec->z0, prec->x1, prec->y1, prec->z1, prec->cblkno[0], prec->cblkno[1], prec->cblkno[2]);\r
-                                               for (cblkno = 0; cblkno < (prec->cblkno[0] * prec->cblkno[1] * prec->cblkno[2]); cblkno++) {\r
-                                                       opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];\r
-                                                       fprintf(fd, "               cblk %d{\n",cblkno);\r
-                                                       fprintf(fd,     "                    x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", cblk->x0, cblk->y0, cblk->z0, cblk->x1, cblk->y1, cblk->z1);\r
-                                                       fprintf(fd, "            }\n");\r
-                                               }\r
-                                               fprintf(fd, "          }\n");\r
-                                       }\r
-                                       fprintf(fd, "        }\n");\r
-                               }\r
-                               fprintf(fd, "      }\n");\r
-                       }\r
-                       fprintf(fd, "    }\n");\r
-               }\r
-               fprintf(fd, "  }\n");\r
-       }\r
-       fprintf(fd, "}\n");\r
-}\r
-\r
-static void tilec_dump(FILE *fd, opj_tcd_tilecomp_t *tilec) {\r
-\r
-       int i=0,k;\r
-       int datalen;\r
-       int *a;\r
-\r
-       fprintf(fd, "    tilecomp{\n");\r
-       fprintf(fd,     "     x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numresx=%d, numresy=%d, numresz=%d\n",\r
-               tilec->x0, tilec->y0, tilec->z0, tilec->x1, tilec->y1, tilec->z1, tilec->numresolution[0], tilec->numresolution[1], tilec->numresolution[2]);\r
-       fprintf(fd, "     data {\n");\r
-       datalen = (tilec->z1 - tilec->z0) * (tilec->y1 - tilec->y0) * (tilec->x1 - tilec->x0);\r
-       a = tilec->data;\r
-       for (k = 0; k < datalen; k++) {\r
-               if (!(k % tilec->x1)){\r
-                       fprintf(fd, "\n");\r
-               }\r
-               if (!(k % (tilec->y1 * tilec->x1))){\r
-                       fprintf(fd, "Slice %d\n",i++);\r
-               }\r
-               fprintf(fd," %d",a[k]);\r
-               \r
-               \r
-       }                       \r
-       fprintf(fd, "     }\n");\r
-       /*i=0;\r
-       fprintf(fd, "Slice %d\n");\r
-       if (tilec->prediction->prederr) {\r
-               fprintf(fd, "     prederror {\n");\r
-               a = tilec->prediction->prederr;\r
-               for (k = 0; k < datalen; k++) {\r
-                       fprintf(fd," %d",*(a++));\r
-                       if (!(k % (tilec->y1 - tilec->y0) * (tilec->x1 - tilec->x0))){\r
-                               fprintf(fd, "\n");fprintf(fd, "Slice %d\n",i++);\r
-                       }\r
-                       if (!(k % (tilec->x1 - tilec->x0))){\r
-                               fprintf(fd, "\n");\r
-                       }\r
-               }\r
-       }\r
-       fprintf(fd, "     }\n");*/\r
-       fprintf(fd, "}\n");\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/**\r
-Create a new TCD handle\r
-*/\r
-opj_tcd_t* tcd_create(opj_common_ptr cinfo) {\r
-       /* create the tcd structure */\r
-       opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));\r
-       if(!tcd) return NULL;\r
-       tcd->cinfo = cinfo;\r
-       tcd->tcd_volume = (opj_tcd_volume_t*)opj_malloc(sizeof(opj_tcd_volume_t));\r
-       if(!tcd->tcd_volume) {\r
-               opj_free(tcd);\r
-               return NULL;\r
-       }\r
-\r
-       return tcd;\r
-}\r
-\r
-/**\r
-Destroy a previously created TCD handle\r
-*/\r
-void tcd_destroy(opj_tcd_t *tcd) {\r
-       if(tcd) {\r
-               opj_free(tcd->tcd_volume);\r
-               opj_free(tcd);\r
-       }\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-void tcd_malloc_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno) {\r
-       int compno, resno, bandno, precno, cblkno, i, j;/*, k;*/\r
-\r
-       opj_tcd_tile_t          *tile = NULL;           /* pointer to tcd->tile */\r
-       opj_tcd_tilecomp_t      *tilec = NULL;          /* pointer to tcd->tilec */\r
-       opj_tcd_resolution_t    *res = NULL;            /* pointer to tcd->res */\r
-       opj_tcd_band_t          *band = NULL;           /* pointer to tcd->band */\r
-       opj_tcd_precinct_t      *prc = NULL;            /* pointer to tcd->prc */\r
-       opj_tcd_cblk_t          *cblk = NULL;           /* pointer to tcd->cblk */\r
-       opj_tcp_t               *tcp = &cp->tcps[curtileno];\r
-       int p,q,r;\r
-\r
-       tcd->volume = volume;\r
-       tcd->cp = cp;\r
-       tcd->tcd_volume->tw = cp->tw;\r
-       tcd->tcd_volume->th = cp->th;\r
-       tcd->tcd_volume->tl = cp->tl;\r
-       tcd->tcd_volume->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));\r
-       tcd->tile = tcd->tcd_volume->tiles;\r
-       tile = tcd->tile;\r
-       \r
-\r
-       /* p61 ISO/IEC IS15444-1 : 2002 */\r
-       /* curtileno --> raster scanned index of tiles */\r
-       /* p,q,r --> matricial index of tiles */\r
-       p = curtileno % cp->tw; \r
-       q = curtileno / cp->tw; \r
-       r = curtileno / (cp->tw * cp->th); /* extension to 3-D */\r
-\r
-       /* 4 borders of the tile rescale on the volume if necessary (B.3)*/\r
-       tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);\r
-       tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);\r
-       tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);\r
-       tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);\r
-       tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);\r
-       tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);\r
-       tile->numcomps = volume->numcomps;\r
-\r
-       /* Modification of the RATE >> */\r
-       for (j = 0; j < tcp->numlayers; j++) {\r
-               if (tcp->rates[j] <= 1) {\r
-                       tcp->rates[j] = 0;\r
-               } else {\r
-                       float num = (float) (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec);\r
-                       float den = (float) (8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz);\r
-                       den = tcp->rates[j] * den;\r
-                       tcp->rates[j] = (num + den - 1) / den;\r
-               }\r
-               /*tcp->rates[j] = tcp->rates[j] ? int_ceildiv(\r
-                       tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec,\r
-            (tcp->rates[j] * 8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz)) : 0;*/\r
-               if (tcp->rates[j]) {\r
-                       if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {\r
-                               tcp->rates[j] = tcp->rates[j - 1] + 20;\r
-                       } else if (!j && tcp->rates[j] < 30){\r
-                               tcp->rates[j] = 30;\r
-                       }\r
-               }\r
-       }\r
-       /* << Modification of the RATE */\r
-\r
-       tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(volume->numcomps * sizeof(opj_tcd_tilecomp_t));\r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               opj_tccp_t *tccp = &tcp->tccps[compno];\r
-               int res_max;\r
-               int prevnumbands = 0;\r
-\r
-               /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */\r
-               tcd->tilec = &tile->comps[compno];\r
-               tilec = tcd->tilec;\r
-\r
-               /* border of each tile component (global) (B.3) */\r
-               tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);\r
-               tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);\r
-               tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);\r
-               tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);\r
-               tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);\r
-               tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);\r
-\r
-               tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));\r
-               \r
-               res_max = 0;\r
-               for (i = 0;i < 3; i++){\r
-                       tilec->numresolution[i] = tccp->numresolution[i];\r
-                       /*Greater of 3 resolutions contains all information*/\r
-                       res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;\r
-               }\r
-               \r
-\r
-               tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));\r
-               for (resno = 0; resno < res_max; resno++) {\r
-                       \r
-                       int pdx, pdy, pdz;\r
-                       int tlprcxstart, tlprcystart, tlprczstart;\r
-                       int brprcxend, brprcyend, brprczend;\r
-                       int tlcbgxstart, tlcbgystart, tlcbgzstart;\r
-                       int brcbgxend, brcbgyend, brcbgzend;\r
-                       int cbgwidthexpn, cbgheightexpn, cbglengthexpn;\r
-                       int cblkwidthexpn, cblkheightexpn, cblklengthexpn;\r
-\r
-                       int diff = tccp->numresolution[0] - tccp->numresolution[2]; \r
-                       int levelnox = tilec->numresolution[0] - 1 - resno; \r
-                       int levelnoy = tilec->numresolution[1] - 1 - resno;\r
-                       int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));\r
-                               if (levelnoz < 0) levelnoz = 0;\r
-\r
-                       /* opj_tcd_resolution_t *res=&tilec->resolutions[resno]; */\r
-                       tcd->res = &tilec->resolutions[resno];\r
-                       res = tcd->res;\r
-                       \r
-                       /* border for each resolution level (global) (B.14)*/\r
-                       res->x0 = int_ceildivpow2(tilec->x0, levelnox);\r
-                       res->y0 = int_ceildivpow2(tilec->y0, levelnoy);\r
-                       res->z0 = int_ceildivpow2(tilec->z0, levelnoz);\r
-                       res->x1 = int_ceildivpow2(tilec->x1, levelnox);\r
-                       res->y1 = int_ceildivpow2(tilec->y1, levelnoy);\r
-                       res->z1 = int_ceildivpow2(tilec->z1, levelnoz);\r
-                       /*if (res->z1 < 0)fprintf(stdout,"Res: %d       %d/%d --> %d\n",resno,tilec->z1, levelnoz, int_ceildivpow2(tilec->z1, levelnoz));*/\r
-\r
-                       res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */\r
-\r
-                       /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */\r
-                       if (tccp->csty & J3D_CCP_CSTY_PRT) {\r
-                               pdx = tccp->prctsiz[0][resno];\r
-                               pdy = tccp->prctsiz[1][resno];\r
-                               pdz = tccp->prctsiz[2][resno];\r
-                       } else {\r
-                               pdx = 15;\r
-                               pdy = 15;\r
-                               pdz = 15;\r
-                       }\r
-                       \r
-                       /* p. 66, B.16, ISO/IEC IS15444-1 : 2002  */\r
-                       tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;\r
-                       tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;\r
-                       tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;\r
-                       brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;\r
-                       brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;\r
-                       brprczend = int_ceildivpow2(res->z1, pdz) << pdz;\r
-                       \r
-                       res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;\r
-                       res->prctno[1] = (brprcyend - tlprcystart) >> pdy;\r
-                       res->prctno[2] = (brprczend - tlprczstart) >> pdz;\r
-                               if (res->prctno[2] == 0) res->prctno[2] = 1;\r
-                               \r
-                       /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002  */\r
-                       if (resno == 0) {\r
-                               tlcbgxstart = tlprcxstart;\r
-                               tlcbgystart = tlprcystart;\r
-                               tlcbgzstart = tlprczstart;\r
-                               brcbgxend = brprcxend;\r
-                               brcbgyend = brprcyend;\r
-                               brcbgzend = brprczend;\r
-                               cbgwidthexpn = pdx;\r
-                               cbgheightexpn = pdy;\r
-                               cbglengthexpn = pdz;\r
-                       } else {\r
-                               tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);\r
-                               tlcbgystart = int_ceildivpow2(tlprcystart, 1);\r
-                               tlcbgzstart = int_ceildivpow2(tlprczstart, 1);\r
-                               brcbgxend = int_ceildivpow2(brprcxend, 1);\r
-                               brcbgyend = int_ceildivpow2(brprcyend, 1);\r
-                               brcbgzend = int_ceildivpow2(brprczend, 1);\r
-                               cbgwidthexpn = pdx - 1;\r
-                               cbgheightexpn = pdy - 1;\r
-                               cbglengthexpn = pdz - 1;\r
-                       }\r
-                       \r
-                       cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); /*6*/\r
-                       cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); /*6*/\r
-                       cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); /*6*/\r
-                       \r
-                       res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));\r
-                       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                               int x0b, y0b, z0b, i;\r
-                               int gain, numbps;\r
-                               opj_stepsize_t *ss = NULL;\r
-\r
-                               tcd->band = &res->bands[bandno];\r
-                               band = tcd->band;\r
-\r
-                               band->bandno = (resno == 0) ? 0 : bandno + 1;\r
-                               /* Bandno:      0 - LLL         2 - LHL \r
-                                                       1 - HLL         3 - HHL\r
-                                                       4 - LLH         6 - LHH\r
-                                                       5 - HLH         7 - HHH         */\r
-                               x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0; \r
-                               y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;\r
-                               z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; \r
-                               \r
-                               /* p. 65, B.15, ISO/IEC IS15444-1 : 2002  */\r
-                               if (band->bandno == 0) {\r
-                                       /* band border (global) */\r
-                                       band->x0 = int_ceildivpow2(tilec->x0, levelnox);\r
-                                       band->y0 = int_ceildivpow2(tilec->y0, levelnoy);\r
-                                       band->z0 = int_ceildivpow2(tilec->z0, levelnoz);\r
-                                       band->x1 = int_ceildivpow2(tilec->x1, levelnox);\r
-                                       band->y1 = int_ceildivpow2(tilec->y1, levelnoy);\r
-                                       band->z1 = int_ceildivpow2(tilec->z1, levelnoz);\r
-                               } else {\r
-                                       band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);\r
-                                       band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);\r
-                                       band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);\r
-                                       band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);\r
-                                       band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);\r
-                                       band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);\r
-                               }\r
-                               \r
-                               ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];\r
-                               if (bandno == (res->numbands - 1)) \r
-                                       prevnumbands += (resno == 0) ? 0 : res->numbands;\r
-                               gain = dwt_getgain(band->bandno,tccp->reversible);                                      \r
-                               numbps = volume->comps[compno].prec + gain;\r
-                               band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));\r
-                               band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */\r
-                               \r
-                               band->precincts = (opj_tcd_precinct_t *) opj_malloc((res->prctno[0] * res->prctno[1] * res->prctno[2]) * sizeof(opj_tcd_precinct_t));\r
-                               \r
-                               for (i = 0; i < (res->prctno[0] * res->prctno[1] * res->prctno[2]); i++) {\r
-                                       band->precincts[i].imsbtree = NULL;\r
-                                       band->precincts[i].incltree = NULL;\r
-                               }\r
-\r
-                               for (precno = 0; precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); precno++) {\r
-                                       int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;\r
-                                       int cbgxstart, cbgystart, cbgzstart, cbgxend, cbgyend, cbgzend;\r
-\r
-                                       cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);\r
-                                       cbgystart = tlcbgystart + ((precno % (res->prctno[0] * res->prctno[1])) / res->prctno[0]) * (1 << cbgheightexpn);\r
-                                       cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);\r
-                                       cbgxend = cbgxstart + (1 << cbgwidthexpn);\r
-                                       cbgyend = cbgystart + (1 << cbgheightexpn);\r
-                                       cbgzend = cbgzstart + (1 << cbglengthexpn);\r
-                                       \r
-                                       tcd->prc = &band->precincts[precno];\r
-                                       prc = tcd->prc;\r
-\r
-                                       /* precinct size (global) */\r
-                                       prc->x0 = int_max(cbgxstart, band->x0);\r
-                                       prc->y0 = int_max(cbgystart, band->y0);\r
-                                       prc->z0 = int_max(cbgzstart, band->z0);\r
-                                       prc->x1 = int_min(cbgxend, band->x1);\r
-                                       prc->y1 = int_min(cbgyend, band->y1);\r
-                                       prc->z1 = int_min(cbgzend, band->z1);\r
-                                       \r
-                                       tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;\r
-                                       tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;\r
-                                       tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;\r
-                                       brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;\r
-                                       brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;\r
-                                       brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;\r
-                                       prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;\r
-                                       prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;\r
-                                       prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;\r
-                                       prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];\r
-\r
-                                       prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));\r
-                                       prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);\r
-                                       prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);\r
-                                       /*tgt_tree_dump(stdout,prc->incltree);*/\r
-                                       for (cblkno = 0; cblkno < (prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]); cblkno++) {\r
-                                               int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);\r
-                                               int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);\r
-                                               int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);\r
-                                               int cblkxend = cblkxstart + (1 << cblkwidthexpn);\r
-                                               int cblkyend = cblkystart + (1 << cblkheightexpn);\r
-                                               int cblkzend = cblkzstart + (1 << cblklengthexpn);\r
-                                               int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));\r
-                                               \r
-                                               tcd->cblk = &prc->cblks[cblkno];\r
-                                               cblk = tcd->cblk;\r
-\r
-                                               /* code-block size (global) */\r
-                                               cblk->x0 = int_max(cblkxstart, prc->x0);\r
-                                               cblk->y0 = int_max(cblkystart, prc->y0);\r
-                                               cblk->z0 = int_max(cblkzstart, prc->z0);\r
-                                               cblk->x1 = int_min(cblkxend, prc->x1);\r
-                                               cblk->y1 = int_min(cblkyend, prc->y1);\r
-                                               cblk->z1 = int_min(cblkzend, prc->z1);\r
-                                       }                                       \r
-                               }\r
-                       }\r
-               }\r
-       }\r
-       /*tcd_dump(stdout, tcd, tcd->tcd_volume);*/\r
-\r
-}\r
-void tcd_init_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno) {\r
-       int compno, resno, bandno, precno, cblkno;\r
-       int j, p, q, r;\r
-\r
-       opj_tcd_tile_t          *tile = NULL;           /* pointer to tcd->tile */\r
-       opj_tcd_tilecomp_t      *tilec = NULL;          /* pointer to tcd->tilec */\r
-       opj_tcd_resolution_t    *res = NULL;    /* pointer to tcd->res */\r
-       opj_tcd_band_t          *band = NULL;           /* pointer to tcd->band */\r
-       opj_tcd_precinct_t      *prc = NULL;            /* pointer to tcd->prc */\r
-       opj_tcd_cblk_t          *cblk = NULL;           /* pointer to tcd->cblk */\r
-       opj_tcp_t *tcp = &cp->tcps[curtileno];\r
-\r
-       tcd->tile = tcd->tcd_volume->tiles;\r
-       tile = tcd->tile;\r
-\r
-       /* p61 ISO/IEC IS15444-1 : 2002 */\r
-       /* curtileno --> raster scanned index of tiles */\r
-       /* p,q,r --> matricial index of tiles */\r
-       p = curtileno % cp->tw; \r
-       q = curtileno / cp->tw; \r
-       r = curtileno / (cp->tw * cp->th); /* extension to 3-D */\r
-       \r
-       /* 4 borders of the tile rescale on the volume if necessary (B.3)*/\r
-       tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);\r
-       tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);\r
-       tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);\r
-       tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);\r
-       tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);\r
-       tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);\r
-       tile->numcomps = volume->numcomps;\r
-\r
-       /* Modification of the RATE >> */\r
-       for (j = 0; j < tcp->numlayers; j++) {\r
-               if (tcp->rates[j] <= 1) {\r
-                       tcp->rates[j] = 0;\r
-               } else {\r
-                       float num = (float) (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec);\r
-                       float den = (float) (8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz);\r
-                       den = tcp->rates[j] * den;\r
-                       tcp->rates[j] = (num + den - 1) / den;\r
-               }\r
-               /*tcp->rates[j] = tcp->rates[j] ? int_ceildiv(\r
-                       tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec,\r
-            (tcp->rates[j] * 8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz)) : 0;*/\r
-               if (tcp->rates[j]) {\r
-                       if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {\r
-                               tcp->rates[j] = tcp->rates[j - 1] + 20;\r
-                       } else if (!j && tcp->rates[j] < 30){\r
-                               tcp->rates[j] = 30;\r
-                       }\r
-               }\r
-       }\r
-       /* << Modification of the RATE */\r
-\r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               opj_tccp_t *tccp = &tcp->tccps[compno];\r
-               int res_max, i;\r
-               int prevnumbands = 0;\r
-\r
-               /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */\r
-               tcd->tilec = &tile->comps[compno];\r
-               tilec = tcd->tilec;\r
-\r
-               /* border of each tile component (global) (B.3) */\r
-               tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);\r
-               tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);\r
-               tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);\r
-               tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);\r
-               tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);\r
-               tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);\r
-\r
-               tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));\r
-               \r
-               res_max = 0;\r
-               for (i = 0;i < 3; i++){\r
-                       tilec->numresolution[i] = tccp->numresolution[i];\r
-                       /*Greater of 3 resolutions contains all information*/\r
-                       res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;\r
-               }\r
-\r
-               tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));\r
-               for (resno = 0; resno < res_max; resno++) {\r
-                       int pdx, pdy, pdz;\r
-                       int tlprcxstart, tlprcystart, tlprczstart, brprcxend, brprcyend, brprczend;\r
-                       int tlcbgxstart, tlcbgystart, tlcbgzstart, brcbgxend, brcbgyend, brcbgzend;\r
-                       int cbgwidthexpn, cbgheightexpn, cbglengthexpn;\r
-                       int cblkwidthexpn, cblkheightexpn, cblklengthexpn;\r
-                       \r
-                       int levelnox = tilec->numresolution[0] - 1 - resno; \r
-                       int levelnoy = tilec->numresolution[1] - 1 - resno;\r
-                       int diff = tccp->numresolution[0] - tccp->numresolution[2]; \r
-                       int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));\r
-                               if (levelnoz < 0) levelnoz = 0;\r
-\r
-                       tcd->res = &tilec->resolutions[resno];\r
-                       res = tcd->res;\r
-                       \r
-                       /* border for each resolution level (global) (B.14)*/\r
-                       res->x0 = int_ceildivpow2(tilec->x0, levelnox);\r
-                       res->y0 = int_ceildivpow2(tilec->y0, levelnoy);\r
-                       res->z0 = int_ceildivpow2(tilec->z0, levelnoz);\r
-                       res->x1 = int_ceildivpow2(tilec->x1, levelnox);\r
-                       res->y1 = int_ceildivpow2(tilec->y1, levelnoy);\r
-                       res->z1 = int_ceildivpow2(tilec->z1, levelnoz);\r
-\r
-                       /* res->numbands = resno == 0 ? 1 : 3; *//* --> 2D */\r
-\r
-                       res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */\r
-\r
-                       /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */                      \r
-                       if (tccp->csty & J3D_CCP_CSTY_PRT) {\r
-                               pdx = tccp->prctsiz[0][resno];\r
-                               pdy = tccp->prctsiz[1][resno];\r
-                               pdz = tccp->prctsiz[2][resno];\r
-                       } else {\r
-                               pdx = 15;\r
-                               pdy = 15;\r
-                               pdz = 15;\r
-                       }\r
-                       /* p. 66, B.16, ISO/IEC IS15444-1 : 2002  */\r
-                       tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;\r
-                       tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;\r
-                       tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;\r
-                       brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;\r
-                       brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;\r
-                       brprczend = int_ceildivpow2(res->z1, pdz) << pdz;\r
-                       \r
-                       res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;\r
-                       res->prctno[1] = (brprcyend - tlprcystart) >> pdy;\r
-                       res->prctno[2] = (brprczend - tlprczstart) >> pdz;\r
-                       if (res->prctno[2] == 0) res->prctno[2] = 1;\r
-\r
-                       /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002  */\r
-                       if (resno == 0) {\r
-                               tlcbgxstart = tlprcxstart;\r
-                               tlcbgystart = tlprcystart;\r
-                               tlcbgzstart = tlprczstart;\r
-                               brcbgxend = brprcxend;\r
-                               brcbgyend = brprcyend;\r
-                               brcbgzend = brprczend;\r
-                               cbgwidthexpn = pdx;\r
-                               cbgheightexpn = pdy;\r
-                               cbglengthexpn = pdz;\r
-                       } else {\r
-                               tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);\r
-                               tlcbgystart = int_ceildivpow2(tlprcystart, 1);\r
-                               tlcbgzstart = int_ceildivpow2(tlprczstart, 1);\r
-                               brcbgxend = int_ceildivpow2(brprcxend, 1);\r
-                               brcbgyend = int_ceildivpow2(brprcyend, 1);\r
-                               brcbgzend = int_ceildivpow2(brprczend, 1);\r
-                               cbgwidthexpn = pdx - 1;\r
-                               cbgheightexpn = pdy - 1;\r
-                               cbglengthexpn = pdz - 1;\r
-                       }\r
-                       \r
-                       cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn);\r
-                       cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn);\r
-                       cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn);\r
-                       \r
-                       res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));\r
-                       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                               int x0b, y0b, z0b;\r
-                               int gain, numbps;\r
-                               opj_stepsize_t *ss = NULL;\r
-\r
-                               tcd->band = &res->bands[bandno];\r
-                               band = tcd->band;\r
-\r
-                               band->bandno = resno == 0 ? 0 : bandno + 1;\r
-                               /* Bandno:      0 - LLL         2 - LHL \r
-                                                       1 - HLL         3 - HHL\r
-                                                       4 - LLH         6 - LHH\r
-                                                       5 - HLH         7 - HHH         */\r
-                               x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0; \r
-                               y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;\r
-                               z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; \r
-                               \r
-                               /* p. 65, B.15, ISO/IEC IS15444-1 : 2002  */\r
-                               if (band->bandno == 0) {\r
-                                       /* band border (global) */\r
-                                       band->x0 = int_ceildivpow2(tilec->x0, levelnox);\r
-                                       band->y0 = int_ceildivpow2(tilec->y0, levelnoy);\r
-                                       band->z0 = int_ceildivpow2(tilec->z0, levelnoz);\r
-                                       band->x1 = int_ceildivpow2(tilec->x1, levelnox);\r
-                                       band->y1 = int_ceildivpow2(tilec->y1, levelnoy);\r
-                                       band->z1 = int_ceildivpow2(tilec->z1, levelnoz);\r
-                               } else {\r
-                                       band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);\r
-                                       band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);\r
-                                       band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);\r
-                                       band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);\r
-                                       band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);\r
-                                       band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);\r
-                               }\r
-                                                               \r
-                               ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];\r
-                               if (bandno == (res->numbands - 1)) \r
-                                       prevnumbands += (resno == 0) ? 0 : res->numbands;\r
-                               gain = dwt_getgain(band->bandno,tccp->reversible);                                      \r
-                               numbps = volume->comps[compno].prec + gain;\r
-                               \r
-                               band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));\r
-                               band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */\r
-                               \r
-                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {\r
-                                       int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;\r
-\r
-                                       int cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);\r
-                                       int cbgystart = tlcbgystart + ((precno / (res->prctno[0] * res->prctno[1])) / res->prctno[0]) * (1 << cbgheightexpn);\r
-                                       int cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);\r
-                                       int cbgxend = cbgxstart + (1 << cbgwidthexpn);\r
-                                       int cbgyend = cbgystart + (1 << cbgheightexpn);\r
-                                       int cbgzend = cbgzstart + (1 << cbglengthexpn);\r
-\r
-                                       /* opj_tcd_precinct_t *prc=&band->precincts[precno]; */\r
-                                       tcd->prc = &band->precincts[precno];\r
-                                       prc = tcd->prc;\r
-\r
-                                       /* precinct size (global) */\r
-                                       prc->x0 = int_max(cbgxstart, band->x0);\r
-                                       prc->y0 = int_max(cbgystart, band->y0);\r
-                                       prc->z0 = int_max(cbgzstart, band->z0);\r
-                                       prc->x1 = int_min(cbgxend, band->x1);\r
-                                       prc->y1 = int_min(cbgyend, band->y1);\r
-                                       prc->z1 = int_min(cbgzend, band->z1);\r
-\r
-                                       tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;\r
-                                       tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;\r
-                                       tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;\r
-                                       brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;\r
-                                       brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;\r
-                                       brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;\r
-                                       prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;\r
-                                       prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;\r
-                                       prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;\r
-                                       prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];\r
-\r
-                                       opj_free(prc->cblks);\r
-                                       prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));\r
-                                       prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);\r
-                                       prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);\r
-\r
-                                       for (cblkno = 0; cblkno < (prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]); cblkno++) {\r
-                                                       int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);\r
-                                                       int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);\r
-                                                       int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);\r
-                                                       int cblkxend = cblkxstart + (1 << cblkwidthexpn);\r
-                                                       int cblkyend = cblkystart + (1 << cblkheightexpn);\r
-                                                       int cblkzend = cblkzstart + (1 << cblklengthexpn);\r
-                                                       int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));\r
-\r
-                                                       tcd->cblk = &prc->cblks[cblkno];\r
-                                                       cblk = tcd->cblk;\r
-\r
-                                                       /* code-block size (global) */\r
-                                                       cblk->x0 = int_max(cblkxstart, prc->x0);\r
-                                                       cblk->y0 = int_max(cblkystart, prc->y0);\r
-                                                       cblk->z0 = int_max(cblkzstart, prc->z0);\r
-                                                       cblk->x1 = int_min(cblkxend, prc->x1);\r
-                                                       cblk->y1 = int_min(cblkyend, prc->y1);\r
-                                                       cblk->z1 = int_min(cblkzend, prc->z1);\r
-                                       }\r
-                               } /* precno */\r
-                       } /* bandno */\r
-               } /* resno */\r
-       } /* compno */\r
-       /*tcd_dump(stdout, tcd, tcd->tcd_volume);*/\r
-}\r
-\r
-\r
-void tcd_free_encode(opj_tcd_t *tcd) {\r
-       int tileno, compno, resno, bandno, precno;\r
-\r
-       opj_tcd_tile_t *tile = NULL;            /* pointer to tcd->tile         */\r
-/*     opj_tcd_slice_t *slice = NULL; */               /* pointer to tcd->slice */\r
-       opj_tcd_tilecomp_t *tilec = NULL;       /* pointer to tcd->tilec        */\r
-       opj_tcd_resolution_t *res = NULL;       /* pointer to tcd->res          */\r
-       opj_tcd_band_t *band = NULL;            /* pointer to tcd->band         */\r
-       opj_tcd_precinct_t *prc = NULL;         /* pointer to tcd->prc          */\r
-\r
-       for (tileno = 0; tileno < 1; tileno++) {\r
-               tcd->tile = tcd->tcd_volume->tiles;\r
-               tile = tcd->tile;\r
-\r
-               for (compno = 0; compno < tile->numcomps; compno++) {\r
-                       tcd->tilec = &tile->comps[compno];\r
-                       tilec = tcd->tilec;\r
-\r
-                       for (resno = 0; resno < tilec->numresolution[0]; resno++) {\r
-                               tcd->res = &tilec->resolutions[resno];\r
-                               res = tcd->res;\r
-\r
-                               for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                                       tcd->band = &res->bands[bandno];\r
-                                       band = tcd->band;\r
-\r
-                                       for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {\r
-                                               tcd->prc = &band->precincts[precno];\r
-                                               prc = tcd->prc;\r
-\r
-                                               if (prc->incltree != NULL) {\r
-                            tgt_destroy(prc->incltree);\r
-                            prc->incltree = NULL;\r
-                                               }\r
-                                               if (prc->imsbtree != NULL) {\r
-                            tgt_destroy(prc->imsbtree);\r
-                            prc->imsbtree = NULL;\r
-                                               }\r
-                                               opj_free(prc->cblks);\r
-                                               prc->cblks = NULL;\r
-                                       } /* for (precno */\r
-                                       opj_free(band->precincts);\r
-                                       band->precincts = NULL;\r
-                               } /* for (bandno */\r
-                       } /* for (resno */\r
-                       opj_free(tilec->resolutions);\r
-                       tilec->resolutions = NULL;\r
-               } /* for (compno */\r
-               opj_free(tile->comps);\r
-               tile->comps = NULL;\r
-       } /* for (tileno */\r
-       opj_free(tcd->tcd_volume->tiles);\r
-       tcd->tcd_volume->tiles = NULL;\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-void tcd_malloc_decode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp) {\r
-       int tileno, compno, resno, bandno, precno, cblkno, res_max,\r
-               i, j, p, q, r;\r
-       unsigned int x0 = 0, y0 = 0, z0 = 0, \r
-               x1 = 0, y1 = 0, z1 = 0, \r
-               w, h, l;\r
-\r
-       tcd->volume = volume;\r
-       tcd->cp = cp;\r
-       tcd->tcd_volume->tw = cp->tw;\r
-       tcd->tcd_volume->th = cp->th;\r
-       tcd->tcd_volume->tl = cp->tl;\r
-       tcd->tcd_volume->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcd_tile_t));\r
-       \r
-       for (i = 0; i < cp->tileno_size; i++) {\r
-               opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]);\r
-               opj_tcd_tile_t *tile = &(tcd->tcd_volume->tiles[cp->tileno[i]]);\r
-       \r
-               /* p61 ISO/IEC IS15444-1 : 2002 */\r
-               /* curtileno --> raster scanned index of tiles */\r
-               /* p,q,r --> matricial index of tiles */\r
-               tileno = cp->tileno[i];\r
-               p = tileno % cp->tw;    \r
-               q = tileno / cp->tw;    \r
-               r = tileno / (cp->tw * cp->th); /* extension to 3-D */\r
-\r
-               /* 4 borders of the tile rescale on the volume if necessary (B.3)*/\r
-               tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);\r
-               tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);\r
-               tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);\r
-               tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);\r
-               tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);\r
-               tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);\r
-               tile->numcomps = volume->numcomps;              \r
-               \r
-               tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(volume->numcomps * sizeof(opj_tcd_tilecomp_t));\r
-               for (compno = 0; compno < tile->numcomps; compno++) {\r
-                       opj_tccp_t *tccp = &tcp->tccps[compno];\r
-                       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-                       int prevnumbands = 0;\r
-\r
-                       /* border of each tile component (global) */\r
-                       tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);\r
-                       tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);\r
-                       tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);\r
-                       tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);\r
-                       tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);\r
-                       tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);\r
-                       \r
-                       tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));\r
-\r
-                       res_max = 0;\r
-                       for (i = 0;i < 3; i++){\r
-                               tilec->numresolution[i] = tccp->numresolution[i];\r
-                               /*Greater of 3 resolutions contains all information*/\r
-                               res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;\r
-                       }\r
-\r
-                       tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));\r
-\r
-                       for (resno = 0; resno < res_max; resno++) {\r
-                               opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-                               int pdx, pdy, pdz;\r
-                               int tlprcxstart, tlprcystart, tlprczstart, brprcxend, brprcyend, brprczend;\r
-                               int tlcbgxstart, tlcbgystart, tlcbgzstart, brcbgxend, brcbgyend, brcbgzend;\r
-                               int cbgwidthexpn, cbgheightexpn, cbglengthexpn;\r
-                               int cblkwidthexpn, cblkheightexpn, cblklengthexpn;\r
-                               int levelnox = tilec->numresolution[0] - 1 - resno; \r
-                               int levelnoy = tilec->numresolution[1] - 1 - resno;\r
-                               int diff = tccp->numresolution[0] - tccp->numresolution[2]; \r
-                               int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));\r
-                                       if (levelnoz < 0) levelnoz = 0;\r
-\r
-                               /* border for each resolution level (global) */\r
-                               res->x0 = int_ceildivpow2(tilec->x0, levelnox);\r
-                               res->y0 = int_ceildivpow2(tilec->y0, levelnoy);\r
-                               res->z0 = int_ceildivpow2(tilec->z0, levelnoz);\r
-                               res->x1 = int_ceildivpow2(tilec->x1, levelnox);\r
-                               res->y1 = int_ceildivpow2(tilec->y1, levelnoy);\r
-                               res->z1 = int_ceildivpow2(tilec->z1, levelnoz);\r
-                               res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */\r
-                               \r
-                               /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */\r
-                               if (tccp->csty & J3D_CCP_CSTY_PRT) {\r
-                                       pdx = tccp->prctsiz[0][resno];\r
-                                       pdy = tccp->prctsiz[1][resno];\r
-                                       pdz = tccp->prctsiz[2][resno];\r
-                               } else {\r
-                                       pdx = 15;\r
-                                       pdy = 15;\r
-                                       pdz = 15;\r
-                               }\r
-                               \r
-                               /* p. 66, B.16, ISO/IEC IS15444-1 : 2002  */\r
-                               tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;\r
-                               tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;\r
-                               tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;\r
-                               brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;\r
-                               brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;\r
-                               brprczend = int_ceildivpow2(res->z1, pdz) << pdz;\r
-                               \r
-                               res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;\r
-                               res->prctno[1] = (brprcyend - tlprcystart) >> pdy;\r
-                               res->prctno[2] = (brprczend - tlprczstart) >> pdz;\r
-                               \r
-                               /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002  */\r
-                               if (resno == 0) {\r
-                                       tlcbgxstart = tlprcxstart;/*0*/\r
-                                       tlcbgystart = tlprcystart;\r
-                                       tlcbgzstart = tlprczstart;\r
-                                       brcbgxend = brprcxend;/*1*/\r
-                                       brcbgyend = brprcyend;\r
-                                       brcbgzend = brprczend;\r
-                                       cbgwidthexpn = pdx; /*15*/\r
-                                       cbgheightexpn = pdy;\r
-                                       cbglengthexpn = pdz;\r
-                               } else {\r
-                                       tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);\r
-                                       tlcbgystart = int_ceildivpow2(tlprcystart, 1);\r
-                                       tlcbgzstart = int_ceildivpow2(tlprczstart, 1);\r
-                                       brcbgxend = int_ceildivpow2(brprcxend, 1);\r
-                                       brcbgyend = int_ceildivpow2(brprcyend, 1);\r
-                                       brcbgzend = int_ceildivpow2(brprczend, 1);\r
-                                       cbgwidthexpn = pdx - 1;\r
-                                       cbgheightexpn = pdy - 1;\r
-                                       cbglengthexpn = pdz - 1;\r
-                               }\r
-                               \r
-                               cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); /*6*/\r
-                               cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); /*6*/\r
-                               cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); /*6*/\r
-\r
-                               res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));\r
-                               for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                                       int x0b, y0b, z0b;\r
-                                       int gain, numbps;\r
-                                       opj_stepsize_t *ss = NULL;\r
-\r
-                                       opj_tcd_band_t *band = &res->bands[bandno];\r
-                                       band->bandno = resno == 0 ? 0 : bandno + 1;\r
-                                       /* Bandno:      0 - LLL         2 - LHL \r
-                                                               1 - HLL         3 - HHL\r
-                                                               4 - LLH         6 - LHH\r
-                                                               5 - HLH         7 - HHH         */\r
-                                       x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0; \r
-                                       y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;\r
-                                       z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; \r
-                                       \r
-                                       /* p. 65, B.15, ISO/IEC IS15444-1 : 2002  */\r
-                                       if (band->bandno == 0) {\r
-                                               /* band border (global) */\r
-                                               band->x0 = int_ceildivpow2(tilec->x0, levelnox);\r
-                                               band->y0 = int_ceildivpow2(tilec->y0, levelnoy);\r
-                                               band->z0 = int_ceildivpow2(tilec->z0, levelnoz);\r
-                                               band->x1 = int_ceildivpow2(tilec->x1, levelnox);\r
-                                               band->y1 = int_ceildivpow2(tilec->y1, levelnoy);\r
-                                               band->z1 = int_ceildivpow2(tilec->z1, levelnoz);\r
-                                       } else {\r
-                                               band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);\r
-                                               band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);\r
-                                               band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);\r
-                                               band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);\r
-                                               band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);\r
-                                               band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);\r
-                                       }       \r
-\r
-                                       ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];\r
-                                       if (bandno == (res->numbands - 1)) \r
-                                               prevnumbands += (resno == 0) ? 0 : res->numbands;\r
-                                       gain = dwt_getgain(band->bandno,tccp->reversible);                                      \r
-                                       numbps = volume->comps[compno].prec + gain;\r
-\r
-                                       band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));\r
-                                       band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */\r
-                                       \r
-                                       band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->prctno[0] * res->prctno[1] * res->prctno[2] * sizeof(opj_tcd_precinct_t));\r
-                                       \r
-                                       for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {\r
-                                               int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;\r
-\r
-                                               int cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);\r
-                                               int cbgystart = tlcbgystart + (precno / res->prctno[0]) * (1 << cbgheightexpn);\r
-                                               int cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);\r
-                                               int cbgxend = cbgxstart + (1 << cbgwidthexpn);\r
-                                               int cbgyend = cbgystart + (1 << cbgheightexpn);\r
-                                               int cbgzend = cbgzstart + (1 << cbglengthexpn);\r
-\r
-                                               opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-                                               /* precinct size (global) */\r
-                                               prc->x0 = int_max(cbgxstart, band->x0);\r
-                                               prc->y0 = int_max(cbgystart, band->y0);\r
-                                               prc->z0 = int_max(cbgzstart, band->z0);\r
-                                               prc->x1 = int_min(cbgxend, band->x1);\r
-                                               prc->y1 = int_min(cbgyend, band->y1);\r
-                                               prc->z1 = int_min(cbgzend, band->z1);\r
-\r
-                                               tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;\r
-                                               tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;\r
-                                               tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;\r
-                                               brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;\r
-                                               brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;\r
-                                               brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;\r
-                                               prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;\r
-                                               prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;\r
-                                               prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;\r
-                                               prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];\r
-\r
-                                               prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));\r
-                                               prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);\r
-                                               prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);\r
-                                               \r
-                                               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                                                       int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);\r
-                                                       int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);\r
-                                                       int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);\r
-                                                       int cblkxend = cblkxstart + (1 << cblkwidthexpn);\r
-                                                       int cblkyend = cblkystart + (1 << cblkheightexpn);\r
-                                                       int cblkzend = cblkzstart + (1 << cblklengthexpn);\r
-                                                       int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));\r
-                                                       /* code-block size (global) */\r
-                                                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                                                       \r
-                                                       /* code-block size (global) */\r
-                                                       cblk->x0 = int_max(cblkxstart, prc->x0);\r
-                                                       cblk->y0 = int_max(cblkystart, prc->y0);\r
-                                                       cblk->z0 = int_max(cblkzstart, prc->z0);\r
-                                                       cblk->x1 = int_min(cblkxend, prc->x1);\r
-                                                       cblk->y1 = int_min(cblkyend, prc->y1);\r
-                                                       cblk->z1 = int_min(cblkzend, prc->z1);\r
-                                               }\r
-                                       } /* precno */\r
-                               } /* bandno */\r
-                       } /* resno */\r
-               } /* compno */\r
-       } /* i = 0..cp->tileno_size */\r
-\r
-       /*tcd_dump(stdout, tcd, tcd->tcd_volume);*/\r
-\r
-       /* \r
-       Allocate place to store the decoded data = final volume\r
-       Place limited by the tile really present in the codestream \r
-       */\r
-       \r
-       for (i = 0; i < volume->numcomps; i++) {\r
-               for (j = 0; j < cp->tileno_size; j++) {\r
-                       tileno = cp->tileno[j];\r
-                       x0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].x0 : int_min(x0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].x0);\r
-                       y0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].y0 : int_min(y0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].y0);\r
-                       z0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].z0 : int_min(z0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].z0);\r
-                       x1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].x1 : int_max(x1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].x1);\r
-                       y1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].y1 : int_max(y1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].y1);\r
-                       z1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].z1 : int_max(z1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].z1);\r
-               }\r
-               \r
-               w = x1 - x0;\r
-               h = y1 - y0;\r
-               l = z1 - z0;\r
-               \r
-               volume->comps[i].data = (int *) opj_malloc(w * h * l * sizeof(int));\r
-               volume->comps[i].w = w;\r
-               volume->comps[i].h = h;\r
-               volume->comps[i].l = l;\r
-               volume->comps[i].x0 = x0;\r
-               volume->comps[i].y0 = y0;\r
-               volume->comps[i].z0 = z0;\r
-               volume->comps[i].bigendian = cp->bigendian;\r
-       }\r
-}\r
-\r
-void tcd_free_decode(opj_tcd_t *tcd) {\r
-       int tileno,compno,resno,bandno,precno;\r
-\r
-       opj_tcd_volume_t *tcd_volume = tcd->tcd_volume;\r
-       \r
-       for (tileno = 0; tileno < tcd_volume->tw * tcd_volume->th * tcd_volume->tl; tileno++) {\r
-               opj_tcd_tile_t *tile = &tcd_volume->tiles[tileno];\r
-               for (compno = 0; compno < tile->numcomps; compno++) {\r
-                       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-                       for (resno = 0; resno < tilec->numresolution[0]; resno++) {\r
-                               opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-                               for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                                       opj_tcd_band_t *band = &res->bands[bandno];\r
-                                       for (precno = 0; precno < res->prctno[1] * res->prctno[0] * res->prctno[2]; precno++) {\r
-                                               opj_tcd_precinct_t *prec = &band->precincts[precno];\r
-                                               if (prec->cblks != NULL) opj_free(prec->cblks);\r
-                                               if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);\r
-                        if (prec->incltree != NULL) tgt_destroy(prec->incltree);\r
-                                               /*for (treeno = 0; treeno < prec->numtrees; treeno++){\r
-                            if (prec->imsbtree[treeno] != NULL) tgt_destroy(prec->imsbtree[treeno]);\r
-                            if (prec->incltree[treeno] != NULL) tgt_destroy(prec->incltree[treeno]);\r
-                                               }*/\r
-                                       }\r
-                                       if (band->precincts != NULL) opj_free(band->precincts);\r
-                               }\r
-                       }\r
-                       if (tilec->resolutions != NULL) opj_free(tilec->resolutions);\r
-               }\r
-               if (tile->comps != NULL) opj_free(tile->comps);\r
-       }\r
-\r
-       if (tcd_volume->tiles != NULL) opj_free(tcd_volume->tiles);\r
-}\r
-\r
-\r
-\r
-/* ----------------------------------------------------------------------- */\r
-void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {\r
-       int compno, resno, bandno, precno, cblkno;\r
-       int value;                      /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolution[0]][3]; */\r
-       int matrice[10][10][3];\r
-       int i, j, k;\r
-\r
-       opj_cp_t *cp = tcd->cp;\r
-       opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;\r
-       opj_tcp_t *tcd_tcp = tcd->tcp;\r
-\r
-       /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolution[0]*3*sizeof(int)); */\r
-       \r
-       for (compno = 0; compno < tcd_tile->numcomps; compno++) {\r
-               opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];\r
-               for (i = 0; i < tcd_tcp->numlayers; i++) {\r
-                       for (j = 0; j < tilec->numresolution[0]; j++) {\r
-                               for (k = 0; k < 3; k++) {\r
-                                       matrice[i][j][k] =\r
-                                               (int) (cp->matrice[i * tilec->numresolution[0] * 3 + j * 3 + k] \r
-                                               * (float) (tcd->volume->comps[compno].prec / 16.0));\r
-                               }\r
-                       }\r
-               }\r
-        \r
-               for (resno = 0; resno < tilec->numresolution[0]; resno++) {\r
-                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-                       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                               opj_tcd_band_t *band = &res->bands[bandno];\r
-                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {\r
-                                       opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                                               opj_tcd_layer_t *layer = &cblk->layers[layno];\r
-                                               int n;\r
-                                               int imsb = tcd->volume->comps[compno].prec - cblk->numbps;      /* number of bit-plan equal to zero */\r
-                                               /* Correction of the matrix of coefficient to include the IMSB information */\r
-                                               if (layno == 0) {\r
-                                                       value = matrice[layno][resno][bandno];\r
-                                                       if (imsb >= value) {\r
-                                                               value = 0;\r
-                                                       } else {\r
-                                                               value -= imsb;\r
-                                                       }\r
-                                               } else {\r
-                                                       value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];\r
-                                                       if (imsb >= matrice[layno - 1][resno][bandno]) {\r
-                                                               value -= (imsb - matrice[layno - 1][resno][bandno]);\r
-                                                               if (value < 0) {\r
-                                                                       value = 0;\r
-                                                               }\r
-                                                       }\r
-                                               }\r
-                                               \r
-                                               if (layno == 0) {\r
-                                                       cblk->numpassesinlayers = 0;\r
-                                               }\r
-                                               \r
-                                               n = cblk->numpassesinlayers;\r
-                                               if (cblk->numpassesinlayers == 0) {\r
-                                                       if (value != 0) {\r
-                                                               n = 3 * value - 2 + cblk->numpassesinlayers;\r
-                                                       } else {\r
-                                                               n = cblk->numpassesinlayers;\r
-                                                       }\r
-                                               } else {\r
-                                                       n = 3 * value + cblk->numpassesinlayers;\r
-                                               }\r
-                                               \r
-                                               layer->numpasses = n - cblk->numpassesinlayers;\r
-                                               \r
-                                               if (!layer->numpasses)\r
-                                                       continue;\r
-                                               \r
-                                               if (cblk->numpassesinlayers == 0) {\r
-                                                       layer->len = cblk->passes[n - 1].rate;\r
-                                                       layer->data = cblk->data;\r
-                                               } else {\r
-                                                       layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;\r
-                                                       layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;\r
-                                               }\r
-                                               if (final)\r
-                                                       cblk->numpassesinlayers = n;\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-}\r
-\r
-void tcd_rateallocate_fixed(opj_tcd_t *tcd) {\r
-       int layno;\r
-       for (layno = 0; layno < tcd->tcp->numlayers; layno++) {\r
-               tcd_makelayer_fixed(tcd, layno, 1);\r
-       }\r
-}\r
-\r
-void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {\r
-       int compno, resno, bandno, precno, cblkno, passno;\r
-       \r
-       opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;\r
-\r
-       tcd_tile->distolayer[layno] = 0;        /* fixed_quality */\r
-       \r
-       for (compno = 0; compno < tcd_tile->numcomps; compno++) {\r
-               opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];\r
-               for (resno = 0; resno < tilec->numresolution[0]; resno++) {\r
-                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-                       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                               opj_tcd_band_t *band = &res->bands[bandno];\r
-                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {\r
-                                       opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                                               opj_tcd_layer_t *layer = &cblk->layers[layno];\r
-                                               \r
-                                               int n;\r
-                                               if (layno == 0) {\r
-                                                       cblk->numpassesinlayers = 0;\r
-                                               }\r
-                                               n = cblk->numpassesinlayers;\r
-                                               for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {\r
-                                                       int dr;\r
-                                                       double dd;\r
-                                                       opj_tcd_pass_t *pass = &cblk->passes[passno];\r
-                                                       if (n == 0) {\r
-                                                               dr = pass->rate;\r
-                                                               dd = pass->distortiondec;\r
-                                                       } else {\r
-                                                               dr = pass->rate - cblk->passes[n - 1].rate;\r
-                                                               dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;\r
-                                                       }\r
-                                                       if (!dr) {\r
-                                                               if (dd)\r
-                                                                       n = passno + 1;\r
-                                                               continue;\r
-                                                       }\r
-                                                       if (dd / dr >= thresh){\r
-                                                               n = passno + 1;\r
-                                                       }\r
-                                               }\r
-                                               layer->numpasses = n - cblk->numpassesinlayers;\r
-                                               \r
-                                               if (!layer->numpasses) {\r
-                                                       layer->disto = 0;\r
-                                                       continue;\r
-                                               }\r
-                                               if (cblk->numpassesinlayers == 0) {\r
-                                                       layer->len = cblk->passes[n - 1].rate;\r
-                                                       layer->data = cblk->data;\r
-                                                       layer->disto = cblk->passes[n - 1].distortiondec;\r
-                                               } else {\r
-                                                       layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;\r
-                                                       layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;\r
-                                                       layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;\r
-                                               }\r
-                                               \r
-                                               tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */\r
-                                               \r
-                                               if (final)\r
-                                                       cblk->numpassesinlayers = n;\r
-\r
-                                       /*      fprintf(stdout,"MakeLayer : %d %f %d %d \n",layer->len, layer->disto, layer->numpasses, n);*/\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-}\r
-\r
-bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_volume_info_t * volume_info) {\r
-       int compno, resno, bandno, precno, cblkno, passno, layno;\r
-       double min, max;\r
-       double cumdisto[100];   /* fixed_quality */\r
-       const double K = 1;             /* 1.1; // fixed_quality */\r
-       double maxSE = 0;\r
-\r
-       opj_cp_t *cp = tcd->cp;\r
-       opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;\r
-       opj_tcp_t *tcd_tcp = tcd->tcp;\r
-\r
-       min = DBL_MAX;\r
-       max = 0;\r
-       \r
-       tcd_tile->nbpix = 0;            /* fixed_quality */\r
-       \r
-       for (compno = 0; compno < tcd_tile->numcomps; compno++) {\r
-               opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];\r
-               tilec->nbpix = 0;\r
-               for (resno = 0; resno < tilec->numresolution[0]; resno++) {\r
-                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];\r
-                       for (bandno = 0; bandno < res->numbands; bandno++) {\r
-                               opj_tcd_band_t *band = &res->bands[bandno];\r
-                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {\r
-                                       opj_tcd_precinct_t *prc = &band->precincts[precno];\r
-                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {\r
-                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];\r
-                                               for (passno = 0; passno < cblk->totalpasses; passno++) {\r
-                                                       opj_tcd_pass_t *pass = &cblk->passes[passno];\r
-                                                       int dr;\r
-                                                       double dd, rdslope;\r
-                                                       if (passno == 0) {\r
-                                                               dr = pass->rate;\r
-                                                               dd = pass->distortiondec;\r
-                                                       } else {\r
-                                                               dr = pass->rate - cblk->passes[passno - 1].rate;\r
-                                                               dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;\r
-                                                       }\r
-                                                       if (dr == 0) {\r
-                                                               continue;\r
-                                                       }\r
-                                                       rdslope = dd / dr;\r
-                                                       if (rdslope < min) {\r
-                                                               min = rdslope;\r
-                                                       }\r
-                                                       if (rdslope > max) {\r
-                                                               max = rdslope;\r
-                                                       }\r
-\r
-                                               } /* passno */\r
-                                               \r
-                                               /* fixed_quality */\r
-                                               tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0) * (cblk->z1 - cblk->z0));\r
-                        tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0) * (cblk->z1 - cblk->z0));\r
-                                       } /* cbklno */ \r
-                               } /* precno */\r
-                       } /* bandno */\r
-               } /* resno */\r
-               \r
-               maxSE += (((double)(1 << tcd->volume->comps[compno].prec) - 1.0) \r
-                       * ((double)(1 << tcd->volume->comps[compno].prec) -1.0)) \r
-                       * ((double)(tilec->nbpix));\r
-       } /* compno */\r
-       \r
-       /* add antonin index */\r
-       if(volume_info && volume_info->index_on) {\r
-               opj_tile_info_t *info_TL = &volume_info->tile[tcd->tcd_tileno];\r
-               info_TL->nbpix = tcd_tile->nbpix;\r
-               info_TL->distotile = tcd_tile->distotile;\r
-               info_TL->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));\r
-       }\r
-       /* dda */\r
-       \r
-       for (layno = 0; layno < tcd_tcp->numlayers; layno++) {\r
-               double lo = min;\r
-               double hi = max;\r
-               int success = 0;\r
-               int maxlen = tcd_tcp->rates[layno] ? int_min(((int) tcd_tcp->rates[layno]), len) : len;\r
-               double goodthresh;\r
-               double distotarget;             /* fixed_quality */\r
-               int i = 0;\r
-               \r
-        /* fixed_quality */\r
-               distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));\r
-        \r
-               if ((tcd_tcp->rates[layno]) || (cp->disto_alloc==0)) {\r
-                       opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->volume, cp);\r
-                       int oldl = 0, oldoldl = 0;\r
-                       for (i = 0; i < 128; i++) {\r
-                               double thresh = (lo + hi) / 2;\r
-                               int l = 0;\r
-                               double distoachieved = 0;       /* fixed_quality -q */\r
-                       \r
-                               tcd_makelayer(tcd, layno, thresh, 0);\r
-               \r
-                               if (cp->fixed_quality) {        /* fixed_quality -q */\r
-                                       distoachieved = (layno == 0) ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];\r
-                                       if (distoachieved < distotarget) {\r
-                                               hi = thresh; \r
-                                               continue;\r
-                                       }\r
-                                       lo = thresh;\r
-                               } else {                /* disto_alloc -r, fixed_alloc -f */\r
-                                       l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, volume_info);\r
-                                       /*fprintf(stdout, "layno %d i %d len=%d max=%d \n",layno,i,l,maxlen);*/\r
-                                       if (l == -999) {\r
-                                               lo = thresh; \r
-                                               continue;\r
-                                       } else if (l == oldl && oldl == oldoldl && tcd_tile->distolayer[layno] > 0.0 && i>32)\r
-                                               break;\r
-                                       hi = thresh;\r
-                                       oldoldl = oldl;\r
-                                       oldl = l;\r
-                               }\r
-                               success = 1;\r
-                               goodthresh = thresh;\r
-                       } \r
-                       t2_destroy(t2);\r
-               } else {\r
-                       success = 1;\r
-                       goodthresh = min;\r
-               }\r
-               if (!success) {\r
-                       return false;\r
-               }\r
-               \r
-               if(volume_info && volume_info->index_on) {      /* Threshold for Marcela Index */\r
-                       volume_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;\r
-               }\r
-               tcd_makelayer(tcd, layno, goodthresh, 1);\r
-               \r
-               /* fixed_quality */\r
-               cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];   \r
-       }\r
-\r
-       return true;\r
-}\r
-\r
-/* ----------------------------------------------------------------------- */\r
-int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_volume_info_t * volume_info) {\r
-       int compno;\r
-       int l, i, npck = 0;\r
-       double encoding_time;\r
-       \r
-       opj_tcd_tile_t  *tile = NULL;\r
-       opj_tcp_t               *tcd_tcp = NULL;\r
-       opj_cp_t                *cp = NULL;\r
-\r
-       opj_tcp_t               *tcp = &tcd->cp->tcps[0];\r
-       opj_tccp_t              *tccp = &tcp->tccps[0];\r
-       opj_volume_t    *volume = tcd->volume;\r
-       opj_t2_t                *t2 = NULL;             /* T2 component */\r
-\r
-       tcd->tcd_tileno = tileno;                       /* current encoded/decoded tile */\r
-       \r
-       tcd->tcd_tile = tcd->tcd_volume->tiles; /* tile information */\r
-       tile = tcd->tcd_tile;\r
-       \r
-       tcd->tcp = &tcd->cp->tcps[tileno];      /* coding/decoding params of tileno */  \r
-       tcd_tcp = tcd->tcp;\r
-       \r
-       cp = tcd->cp;           /* coding parameters */\r
-\r
-       /* INDEX >> */\r
-       if(volume_info && volume_info->index_on) {\r
-               opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0];        /* based on component 0 */\r
-               for (i = 0; i < tilec_idx->numresolution[0]; i++) {\r
-                       opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];\r
-\r
-                       volume_info->tile[tileno].prctno[0][i] = res_idx->prctno[0];\r
-                       volume_info->tile[tileno].prctno[1][i] = res_idx->prctno[1];\r
-                       volume_info->tile[tileno].prctno[2][i] = res_idx->prctno[2];\r
-\r
-                       npck += res_idx->prctno[0] * res_idx->prctno[1] * res_idx->prctno[2];\r
-\r
-                       volume_info->tile[tileno].prctsiz[0][i] = tccp->prctsiz[0][i];\r
-                       volume_info->tile[tileno].prctsiz[1][i] = tccp->prctsiz[1][i];\r
-                       volume_info->tile[tileno].prctsiz[2][i] = tccp->prctsiz[2][i];\r
-               }\r
-               volume_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(volume_info->comp * volume_info->layer * npck * sizeof(opj_packet_info_t));\r
-       }\r
-       /* << INDEX */\r
-       \r
-       /*---------------TILE-------------------*/\r
-       encoding_time = opj_clock();    /* time needed to encode a tile */\r
-       \r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               int x, y, z;\r
-               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-               \r
-               int adjust;\r
-               int offset_x = int_ceildiv(volume->x0, volume->comps[compno].dx); /*ceil(x0 / subsampling_dx)*/\r
-               int offset_y = int_ceildiv(volume->y0, volume->comps[compno].dy);\r
-               int offset_z = int_ceildiv(volume->z0, volume->comps[compno].dz);\r
-               \r
-               int tw = tilec->x1 - tilec->x0;\r
-               int w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);\r
-               int th = tilec->y1 - tilec->y0;\r
-               int h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);\r
-               int tl = tilec->z1 - tilec->z0;\r
-               int l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);\r
-\r
-               \r
-               \r
-               /* extract tile data from volume.comps[0].data to tile.comps[0].data */\r
-               /*fprintf(stdout,"[INFO] Extract tile data\n");*/\r
-               if (tcd->cp->transform_format == TRF_3D_RLS || tcd->cp->transform_format == TRF_3D_LSE) {\r
-                       adjust = 0;\r
-               } else {\r
-            adjust = volume->comps[compno].sgnd ? 0 : 1 << (volume->comps[compno].prec - 1); /*sign=='+' --> 2^(prec-1)*/\r
-                       if (volume->comps[compno].dcoffset != 0){\r
-                               adjust += volume->comps[compno].dcoffset;\r
-                               fprintf(stdout,"[INFO] DC Offset applied: DCO = %d -> adjust = %d\n",volume->comps[compno].dcoffset,adjust);\r
-                       }\r
-               }               \r
-\r
-               if (tcd_tcp->tccps[compno].reversible == 1) { /*IF perfect reconstruction (DWT.5-3)*/\r
-                       for (z = tilec->z0; z < tilec->z1; z++) {\r
-                               for (y = tilec->y0; y < tilec->y1; y++) {\r
-                                       /* start of the src tile scanline */\r
-                                       int *data = &volume->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w + (z - offset_z) * w * h];\r
-                                       /* start of the dst tile scanline */\r
-                                       int *tile_data = &tilec->data[(y - tilec->y0) * tw + (z - tilec->z0) * tw * th];\r
-                                       for (x = tilec->x0; x < tilec->x1; x++) {\r
-                                               *tile_data++ = *data++ - adjust;\r
-                                       }\r
-                               }\r
-                       }\r
-               } else if (tcd_tcp->tccps[compno].reversible == 0) { /*IF not (DWT.9-7)*/\r
-                       for (z = tilec->z0; z < tilec->z1; z++) {\r
-                               for (y = tilec->y0; y < tilec->y1; y++) {\r
-                                       /* start of the src tile scanline */\r
-                                       int *data = &volume->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w + (z - offset_z) * w * h];\r
-                                       /* start of the dst tile scanline */\r
-                                       int *tile_data = &tilec->data[(y - tilec->y0) * tw + (z - tilec->z0) * tw * th];\r
-                                       for (x = tilec->x0; x < tilec->x1; x++) {\r
-                                               *tile_data++ = (*data++ - adjust) << 13;\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-       \r
-       }\r
-\r
-       /*----------------MCT-------------------*/\r
-       if (tcd_tcp->mct) {\r
-               int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) * (tile->comps[0].z1 - tile->comps[0].z0);\r
-               fprintf(stdout,"[INFO] Tcd_encode_tile: mct\n");\r
-               if (tcd_tcp->tccps[0].reversible == 0) {\r
-                       mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);\r
-               } else {\r
-                       mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);\r
-               }\r
-       }\r
-       /*----------------TRANSFORM---------------------------------*/\r
-       fprintf(stdout,"[INFO] Tcd_encode_tile: Transform\n");\r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-               dwt_encode(tilec, tcd_tcp->tccps[compno].dwtid);\r
-       } \r
-\r
-       /*-------------------ENTROPY CODING-----------------------------*/\r
-       fprintf(stdout,"[INFO] Tcd_encode_tile: Entropy coding\n");\r
-       if ((cp->encoding_format == ENCOD_2EB)||(cp->encoding_format == ENCOD_3EB))\r
-       {\r
-               if (cp->encoding_format == ENCOD_2EB) {\r
-                       opj_t1_t *t1 = NULL;\r
-                       t1 = t1_create(tcd->cinfo);\r
-                       t1_encode_cblks(t1, tile, tcd_tcp);\r
-                       t1_destroy(t1); \r
-               } else if (cp->encoding_format == ENCOD_3EB) {\r
-                       opj_t1_3d_t *t1 = NULL;         \r
-                       t1 = t1_3d_create(tcd->cinfo);\r
-                       t1_3d_encode_cblks(t1, tile, tcd_tcp);\r
-                       t1_3d_destroy(t1);      \r
-               }\r
-               /*-----------RATE-ALLOCATE------------------*/\r
-               /* INDEX */\r
-               if(volume_info) {\r
-                       volume_info->index_write = 0;\r
-               }\r
-               if (cp->disto_alloc || cp->fixed_quality) {     \r
-                       fprintf(stdout,"[INFO] Tcd_encode_tile: Rate-allocate\n");\r
-                       tcd_rateallocate(tcd, dest, len, volume_info);                  /* Normal Rate/distortion allocation */\r
-               } else {/* fixed_alloc */\r
-           fprintf(stdout,"[INFO] Tcd_encode_tile: Rate-allocate fixed\n");\r
-            tcd_rateallocate_fixed(tcd);                                                       /* Fixed layer allocation */\r
-               }\r
-\r
-               /*--------------TIER2------------------*/\r
-               /* INDEX */\r
-               if(volume_info) {\r
-                       volume_info->index_write = 1;\r
-               }\r
-               fprintf(stdout,"[INFO] Tcd_encode_tile: Tier - 2\n");\r
-        t2 = t2_create(tcd->cinfo, volume, cp);\r
-               l = t2_encode_packets(t2, tileno, tile, tcd_tcp->numlayers, dest, len, volume_info);\r
-        t2_destroy(t2);\r
-       } else if ((cp->encoding_format == ENCOD_2GR)||(cp->encoding_format == ENCOD_3GR)) {\r
-               /*if(volume_info) {\r
-                       volume_info->index_write = 1;\r
-               }\r
-               gr = golomb_create(tcd->cinfo, volume, cp);\r
-               l = golomb_encode(gr, tileno, tile, dest, len, volume_info);\r
-               golomb_destroy(gr);*/\r
-       }\r
-\r
-       \r
-       /*---------------CLEAN-------------------*/\r
-       fprintf(stdout,"[INFO] Tcd_encode_tile: %d bytes coded\n",l);\r
-       encoding_time = opj_clock() - encoding_time;\r
-       opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", encoding_time);\r
-       \r
-       /* cleaning memory */\r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               tcd->tilec = &tile->comps[compno];\r
-               opj_free(tcd->tilec->data);\r
-       }\r
-       \r
-       if (l == -999){\r
-               fprintf(stdout,"[ERROR] Unable to perform T2 tier. Return -999.\n");\r
-               return 0;\r
-       }\r
-\r
-       return l;\r
-}\r
-\r
-\r
-bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) {\r
-       int l, i;\r
-       int compno, eof = 0;\r
-       double tile_time, t1_time, dwt_time;\r
-\r
-       opj_tcd_tile_t *tile = NULL;\r
-       opj_t2_t *t2 = NULL;            /* T2 component */\r
-       \r
-       tcd->tcd_tileno = tileno;\r
-       tcd->tcd_tile = &(tcd->tcd_volume->tiles[tileno]);\r
-       tcd->tcp = &(tcd->cp->tcps[tileno]);\r
-       tile = tcd->tcd_tile;\r
-       \r
-       tile_time = opj_clock();        /* time needed to decode a tile */\r
-       opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d / %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th * tcd->cp->tl);\r
-\r
-       if ((tcd->cp->encoding_format == ENCOD_2EB) || (tcd->cp->encoding_format == ENCOD_3EB)) {\r
-               /*--------------TIER2------------------*/\r
-               t2 = t2_create(tcd->cinfo, tcd->volume, tcd->cp);\r
-               l = t2_decode_packets(t2, src, len, tileno, tile);\r
-               t2_destroy(t2);\r
-               opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: %d bytes decoded\n",l);\r
-               \r
-               if (l == -999) {\r
-                       eof = 1;\r
-                       opj_event_msg(tcd->cinfo, EVT_ERROR, "Tcd_decode_tile: incomplete bistream\n");\r
-               }\r
-       \r
-               /*------------------TIER1-----------------*/\r
-               opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: Entropy decoding %d \n",tcd->cp->encoding_format);\r
-               t1_time = opj_clock();  /* time needed to decode a tile */\r
-               if (tcd->cp->encoding_format == ENCOD_2EB) {\r
-                       opj_t1_t *t1 = NULL;            /* T1 component */\r
-                       t1 = t1_create(tcd->cinfo);\r
-                       t1_decode_cblks(t1, tile, tcd->tcp);\r
-                       t1_destroy(t1);\r
-               }else if (tcd->cp->encoding_format == ENCOD_3EB) {\r
-                       opj_t1_3d_t *t1 = NULL;         /* T1 component */\r
-                       t1 = t1_3d_create(tcd->cinfo);\r
-                       t1_3d_decode_cblks(t1, tile, tcd->tcp);\r
-                       t1_3d_destroy(t1);\r
-               }\r
-\r
-               t1_time = opj_clock() - t1_time;\r
-               #ifdef VERBOSE\r
-                               opj_event_msg(tcd->cinfo, EVT_INFO, "- tier-1 took %f s\n", t1_time);\r
-               #endif\r
-       } else if ((tcd->cp->encoding_format == ENCOD_2GR)||(tcd->cp->encoding_format == ENCOD_3GR)) {\r
-               opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: Entropy decoding -- Does nothing :-D\n");\r
-               /*\r
-               gr = golomb_create(tcd->cinfo, tcd->volume, tcd->cp);\r
-               l = golomb_decode(gr, tileno, tile, src, len);\r
-               golomb_destroy(gr);\r
-               if (l == -999) {\r
-                       eof = 1;\r
-                       opj_event_msg(tcd->cinfo, EVT_ERROR, "Tcd_decode_tile: incomplete bistream\n");\r
-               }\r
-               */\r
-       } \r
-\r
-       /*----------------DWT---------------------*/\r
-       fprintf(stdout,"[INFO] Tcd_decode_tile: Inverse DWT\n");\r
-       dwt_time = opj_clock(); /* time needed to decode a tile */\r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-               int stops[3], dwtid[3];\r
-       \r
-               for (i = 0; i < 3; i++) {\r
-                       if (tcd->cp->reduce[i] != 0) \r
-                               tcd->volume->comps[compno].resno_decoded[i] = tile->comps[compno].numresolution[i] - tcd->cp->reduce[i] - 1;\r
-                       stops[i] = tilec->numresolution[i] - 1 - tcd->volume->comps[compno].resno_decoded[i];\r
-                       if (stops[i] < 0) stops[i]=0;\r
-                       dwtid[i] = tcd->cp->tcps->tccps[compno].dwtid[i];\r
-               }\r
-               \r
-               dwt_decode(tilec, stops, dwtid);\r
-\r
-               for (i = 0; i < 3; i++) {\r
-                       if (tile->comps[compno].numresolution[i] > 0) {\r
-                               tcd->volume->comps[compno].factor[i] = tile->comps[compno].numresolution[i] - (tcd->volume->comps[compno].resno_decoded[i] + 1);\r
-                               if ( (tcd->volume->comps[compno].factor[i]) < 0 )\r
-                                       tcd->volume->comps[compno].factor[i] = 0;\r
-                       }\r
-               }\r
-       }\r
-       dwt_time = opj_clock() - dwt_time;\r
-       #ifdef VERBOSE\r
-                       opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);\r
-       #endif\r
-\r
-       /*----------------MCT-------------------*/\r
-       \r
-       if (tcd->tcp->mct) {\r
-               if (tcd->tcp->tccps[0].reversible == 1) {\r
-                       mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, \r
-                               (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) * (tile->comps[0].z1 - tile->comps[0].z0));\r
-               } else {\r
-                       mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, \r
-                               (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0)* (tile->comps[0].z1 - tile->comps[0].z0));\r
-               }\r
-       }\r
-       \r
-       /*---------------TILE-------------------*/\r
-       \r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];\r
-               opj_tcd_resolution_t *res =     &tilec->resolutions[tcd->volume->comps[compno].resno_decoded[0]];\r
-               int adjust;\r
-               int minval = tcd->volume->comps[compno].sgnd ? -(1 << (tcd->volume->comps[compno].prec - 1)) : 0;\r
-               int maxval = tcd->volume->comps[compno].sgnd ? (1 << (tcd->volume->comps[compno].prec - 1)) - 1 : (1 << tcd->volume->comps[compno].prec) - 1;\r
-               \r
-               int tw = tilec->x1 - tilec->x0;\r
-               int w = tcd->volume->comps[compno].w;\r
-               int th = tilec->y1 - tilec->y0;\r
-               int h = tcd->volume->comps[compno].h;\r
-\r
-               int i, j, k;\r
-               int offset_x = int_ceildivpow2(tcd->volume->comps[compno].x0, tcd->volume->comps[compno].factor[0]);\r
-               int offset_y = int_ceildivpow2(tcd->volume->comps[compno].y0, tcd->volume->comps[compno].factor[1]);\r
-               int offset_z = int_ceildivpow2(tcd->volume->comps[compno].z0, tcd->volume->comps[compno].factor[2]);\r
-               \r
-               if (tcd->cp->transform_format == TRF_3D_RLS || tcd->cp->transform_format == TRF_3D_LSE) {\r
-                       adjust = 0;\r
-               } else {\r
-            adjust = tcd->volume->comps[compno].sgnd ? 0 : 1 << (tcd->volume->comps[compno].prec - 1); /*sign=='+' --> 2^(prec-1)*/\r
-                       if (tcd->volume->comps[compno].dcoffset != 0){\r
-                               adjust += tcd->volume->comps[compno].dcoffset;\r
-                               fprintf(stdout,"[INFO] DC Offset applied: DCO = %d -> adjust = %d\n",tcd->volume->comps[compno].dcoffset,adjust);\r
-                       }\r
-               }\r
-\r
-               for (k = res->z0; k < res->z1; k++) {\r
-                       for (j = res->y0; j < res->y1; j++) {\r
-                               for (i = res->x0; i < res->x1; i++) {\r
-                                       int v;\r
-                                       float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw + (k - res->z0) * tw * th]) / 8192.0);\r
-\r
-                                       if (tcd->tcp->tccps[compno].reversible == 1) {\r
-                                               v = tilec->data[i - res->x0 + (j - res->y0) * tw + (k - res->z0) * tw * th];\r
-                                       } else {\r
-                                               int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);\r
-                                               v = ((tmp < 0) ? -tmp2:tmp2);\r
-                                       }\r
-                                       v += adjust;\r
-                                       \r
-                                       tcd->volume->comps[compno].data[(i - offset_x) + (j - offset_y) * w + (k - offset_z) * w * h] = int_clamp(v, minval, maxval);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-       \r
-       tile_time = opj_clock() - tile_time;    /* time needed to decode a tile */\r
-       opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);\r
-               \r
-       for (compno = 0; compno < tile->numcomps; compno++) {\r
-               opj_free(tcd->tcd_volume->tiles[tileno].comps[compno].data);\r
-               tcd->tcd_volume->tiles[tileno].comps[compno].data = NULL;\r
-       }\r
-       \r
-       if (eof) {\r
-               return false;\r
-       }\r
-       \r
-       return true;\r
-}\r
-\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2006, M�nica D�ez, LPI-UVA, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_volume_t * vol) {
+       int tileno, compno, resno, bandno, precno, cblkno;
+
+       fprintf(fd, "volume {\n");
+       fprintf(fd, "  tw=%d, th=%d, tl=%d, x0=%d x1=%d y0=%d y1=%d z0=%d z1=%d\n", 
+               vol->tw, vol->th, vol->tl, tcd->volume->x0, tcd->volume->x1, tcd->volume->y0, tcd->volume->y1, tcd->volume->z0, tcd->volume->z1);
+
+       for (tileno = 0; tileno < vol->th * vol->tw * vol->tl; tileno++) {
+               opj_tcd_tile_t *tile = &tcd->tcd_volume->tiles[tileno];
+               fprintf(fd, "  tile {\n");
+               fprintf(fd, "    x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numcomps=%d\n",
+                       tile->x0, tile->y0, tile->z0, tile->x1, tile->y1, tile->z1, tile->numcomps);
+               for (compno = 0; compno < tile->numcomps; compno++) {
+                       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+                       fprintf(fd, "    tilecomp %d {\n",compno);
+                       fprintf(fd,     "     x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numresx=%d, numresy=%d, numresz=%d\n",
+                               tilec->x0, tilec->y0, tilec->z0, tilec->x1, tilec->y1, tilec->z1, tilec->numresolution[0], tilec->numresolution[1], tilec->numresolution[2]);
+                       for (resno = 0; resno < tilec->numresolution[0]; resno++) {
+                               opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+                               fprintf(fd, "     res %d{\n",resno);
+                               fprintf(fd,"      x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, pw=%d, ph=%d, pl=%d, numbands=%d\n",
+                                       res->x0, res->y0, res->z0, res->x1, res->y1, res->z1, res->prctno[0], res->prctno[1], res->prctno[2], res->numbands);
+                               for (bandno = 0; bandno < res->numbands; bandno++) {
+                                       opj_tcd_band_t *band = &res->bands[bandno];
+                                       fprintf(fd, "       band %d{\n", bandno);
+                                       fprintf(fd,     "                x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, stepsize=%f, numbps=%d\n",
+                                               band->x0, band->y0, band->z0, band->x1, band->y1, band->z1, band->stepsize, band->numbps);
+                                       for (precno = 0; precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); precno++) {
+                                               opj_tcd_precinct_t *prec = &band->precincts[precno];
+                                               fprintf(fd, "             prec %d{\n",precno);
+                                               fprintf(fd,     "                  x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, cw=%d, ch=%d, cl=%d,\n",
+                                                       prec->x0, prec->y0, prec->z0, prec->x1, prec->y1, prec->z1, prec->cblkno[0], prec->cblkno[1], prec->cblkno[2]);
+                                               for (cblkno = 0; cblkno < (prec->cblkno[0] * prec->cblkno[1] * prec->cblkno[2]); cblkno++) {
+                                                       opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
+                                                       fprintf(fd, "               cblk %d{\n",cblkno);
+                                                       fprintf(fd,     "                    x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", cblk->x0, cblk->y0, cblk->z0, cblk->x1, cblk->y1, cblk->z1);
+                                                       fprintf(fd, "            }\n");
+                                               }
+                                               fprintf(fd, "          }\n");
+                                       }
+                                       fprintf(fd, "        }\n");
+                               }
+                               fprintf(fd, "      }\n");
+                       }
+                       fprintf(fd, "    }\n");
+               }
+               fprintf(fd, "  }\n");
+       }
+       fprintf(fd, "}\n");
+}
+
+static void tilec_dump(FILE *fd, opj_tcd_tilecomp_t *tilec) {
+
+       int i=0,k;
+       int datalen;
+       int *a;
+
+       fprintf(fd, "    tilecomp{\n");
+       fprintf(fd,     "     x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numresx=%d, numresy=%d, numresz=%d\n",
+               tilec->x0, tilec->y0, tilec->z0, tilec->x1, tilec->y1, tilec->z1, tilec->numresolution[0], tilec->numresolution[1], tilec->numresolution[2]);
+       fprintf(fd, "     data {\n");
+       datalen = (tilec->z1 - tilec->z0) * (tilec->y1 - tilec->y0) * (tilec->x1 - tilec->x0);
+       a = tilec->data;
+       for (k = 0; k < datalen; k++) {
+               if (!(k % tilec->x1)){
+                       fprintf(fd, "\n");
+               }
+               if (!(k % (tilec->y1 * tilec->x1))){
+                       fprintf(fd, "Slice %d\n",i++);
+               }
+               fprintf(fd," %d",a[k]);
+               
+               
+       }                       
+       fprintf(fd, "     }\n");
+       /*i=0;
+       fprintf(fd, "Slice %d\n");
+       if (tilec->prediction->prederr) {
+               fprintf(fd, "     prederror {\n");
+               a = tilec->prediction->prederr;
+               for (k = 0; k < datalen; k++) {
+                       fprintf(fd," %d",*(a++));
+                       if (!(k % (tilec->y1 - tilec->y0) * (tilec->x1 - tilec->x0))){
+                               fprintf(fd, "\n");fprintf(fd, "Slice %d\n",i++);
+                       }
+                       if (!(k % (tilec->x1 - tilec->x0))){
+                               fprintf(fd, "\n");
+                       }
+               }
+       }
+       fprintf(fd, "     }\n");*/
+       fprintf(fd, "}\n");
+}
+
+/* ----------------------------------------------------------------------- */
+
+/**
+Create a new TCD handle
+*/
+opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
+       /* create the tcd structure */
+       opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
+       if(!tcd) return NULL;
+       tcd->cinfo = cinfo;
+       tcd->tcd_volume = (opj_tcd_volume_t*)opj_malloc(sizeof(opj_tcd_volume_t));
+       if(!tcd->tcd_volume) {
+               opj_free(tcd);
+               return NULL;
+       }
+
+       return tcd;
+}
+
+/**
+Destroy a previously created TCD handle
+*/
+void tcd_destroy(opj_tcd_t *tcd) {
+       if(tcd) {
+               opj_free(tcd->tcd_volume);
+               opj_free(tcd);
+       }
+}
+
+/* ----------------------------------------------------------------------- */
+void tcd_malloc_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno) {
+       int compno, resno, bandno, precno, cblkno, i, j;/*, k;*/
+
+       opj_tcd_tile_t          *tile = NULL;           /* pointer to tcd->tile */
+       opj_tcd_tilecomp_t      *tilec = NULL;          /* pointer to tcd->tilec */
+       opj_tcd_resolution_t    *res = NULL;            /* pointer to tcd->res */
+       opj_tcd_band_t          *band = NULL;           /* pointer to tcd->band */
+       opj_tcd_precinct_t      *prc = NULL;            /* pointer to tcd->prc */
+       opj_tcd_cblk_t          *cblk = NULL;           /* pointer to tcd->cblk */
+       opj_tcp_t               *tcp = &cp->tcps[curtileno];
+       int p,q,r;
+
+       tcd->volume = volume;
+       tcd->cp = cp;
+       tcd->tcd_volume->tw = cp->tw;
+       tcd->tcd_volume->th = cp->th;
+       tcd->tcd_volume->tl = cp->tl;
+       tcd->tcd_volume->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
+       tcd->tile = tcd->tcd_volume->tiles;
+       tile = tcd->tile;
+       
+
+       /* p61 ISO/IEC IS15444-1 : 2002 */
+       /* curtileno --> raster scanned index of tiles */
+       /* p,q,r --> matricial index of tiles */
+       p = curtileno % cp->tw; 
+       q = curtileno / cp->tw; 
+       r = curtileno / (cp->tw * cp->th); /* extension to 3-D */
+
+       /* 4 borders of the tile rescale on the volume if necessary (B.3)*/
+       tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
+       tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
+       tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
+       tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
+       tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
+       tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
+       tile->numcomps = volume->numcomps;
+
+       /* Modification of the RATE >> */
+       for (j = 0; j < tcp->numlayers; j++) {
+               if (tcp->rates[j] <= 1) {
+                       tcp->rates[j] = 0;
+               } else {
+                       float num = (float) (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec);
+                       float den = (float) (8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz);
+                       den = tcp->rates[j] * den;
+                       tcp->rates[j] = (num + den - 1) / den;
+               }
+               /*tcp->rates[j] = tcp->rates[j] ? int_ceildiv(
+                       tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec,
+            (tcp->rates[j] * 8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz)) : 0;*/
+               if (tcp->rates[j]) {
+                       if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
+                               tcp->rates[j] = tcp->rates[j - 1] + 20;
+                       } else if (!j && tcp->rates[j] < 30){
+                               tcp->rates[j] = 30;
+                       }
+               }
+       }
+       /* << Modification of the RATE */
+
+       tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(volume->numcomps * sizeof(opj_tcd_tilecomp_t));
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               opj_tccp_t *tccp = &tcp->tccps[compno];
+               int res_max;
+               int prevnumbands = 0;
+
+               /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */
+               tcd->tilec = &tile->comps[compno];
+               tilec = tcd->tilec;
+
+               /* border of each tile component (global) (B.3) */
+               tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);
+               tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);
+               tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);
+               tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);
+               tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);
+               tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);
+
+               tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));
+               
+               res_max = 0;
+               for (i = 0;i < 3; i++){
+                       tilec->numresolution[i] = tccp->numresolution[i];
+                       /*Greater of 3 resolutions contains all information*/
+                       res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;
+               }
+               
+
+               tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));
+               for (resno = 0; resno < res_max; resno++) {
+                       
+                       int pdx, pdy, pdz;
+                       int tlprcxstart, tlprcystart, tlprczstart;
+                       int brprcxend, brprcyend, brprczend;
+                       int tlcbgxstart, tlcbgystart, tlcbgzstart;
+                       int brcbgxend, brcbgyend, brcbgzend;
+                       int cbgwidthexpn, cbgheightexpn, cbglengthexpn;
+                       int cblkwidthexpn, cblkheightexpn, cblklengthexpn;
+
+                       int diff = tccp->numresolution[0] - tccp->numresolution[2]; 
+                       int levelnox = tilec->numresolution[0] - 1 - resno; 
+                       int levelnoy = tilec->numresolution[1] - 1 - resno;
+                       int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));
+                               if (levelnoz < 0) levelnoz = 0;
+
+                       /* opj_tcd_resolution_t *res=&tilec->resolutions[resno]; */
+                       tcd->res = &tilec->resolutions[resno];
+                       res = tcd->res;
+                       
+                       /* border for each resolution level (global) (B.14)*/
+                       res->x0 = int_ceildivpow2(tilec->x0, levelnox);
+                       res->y0 = int_ceildivpow2(tilec->y0, levelnoy);
+                       res->z0 = int_ceildivpow2(tilec->z0, levelnoz);
+                       res->x1 = int_ceildivpow2(tilec->x1, levelnox);
+                       res->y1 = int_ceildivpow2(tilec->y1, levelnoy);
+                       res->z1 = int_ceildivpow2(tilec->z1, levelnoz);
+                       /*if (res->z1 < 0)fprintf(stdout,"Res: %d       %d/%d --> %d\n",resno,tilec->z1, levelnoz, int_ceildivpow2(tilec->z1, levelnoz));*/
+
+                       res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */
+
+                       /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */
+                       if (tccp->csty & J3D_CCP_CSTY_PRT) {
+                               pdx = tccp->prctsiz[0][resno];
+                               pdy = tccp->prctsiz[1][resno];
+                               pdz = tccp->prctsiz[2][resno];
+                       } else {
+                               pdx = 15;
+                               pdy = 15;
+                               pdz = 15;
+                       }
+                       
+                       /* p. 66, B.16, ISO/IEC IS15444-1 : 2002  */
+                       tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
+                       tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
+                       tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;
+                       brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
+                       brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
+                       brprczend = int_ceildivpow2(res->z1, pdz) << pdz;
+                       
+                       res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;
+                       res->prctno[1] = (brprcyend - tlprcystart) >> pdy;
+                       res->prctno[2] = (brprczend - tlprczstart) >> pdz;
+                               if (res->prctno[2] == 0) res->prctno[2] = 1;
+                               
+                       /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002  */
+                       if (resno == 0) {
+                               tlcbgxstart = tlprcxstart;
+                               tlcbgystart = tlprcystart;
+                               tlcbgzstart = tlprczstart;
+                               brcbgxend = brprcxend;
+                               brcbgyend = brprcyend;
+                               brcbgzend = brprczend;
+                               cbgwidthexpn = pdx;
+                               cbgheightexpn = pdy;
+                               cbglengthexpn = pdz;
+                       } else {
+                               tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
+                               tlcbgystart = int_ceildivpow2(tlprcystart, 1);
+                               tlcbgzstart = int_ceildivpow2(tlprczstart, 1);
+                               brcbgxend = int_ceildivpow2(brprcxend, 1);
+                               brcbgyend = int_ceildivpow2(brprcyend, 1);
+                               brcbgzend = int_ceildivpow2(brprczend, 1);
+                               cbgwidthexpn = pdx - 1;
+                               cbgheightexpn = pdy - 1;
+                               cbglengthexpn = pdz - 1;
+                       }
+                       
+                       cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); /*6*/
+                       cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); /*6*/
+                       cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); /*6*/
+                       
+                       res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));
+                       for (bandno = 0; bandno < res->numbands; bandno++) {
+                               int x0b, y0b, z0b, i;
+                               int gain, numbps;
+                               opj_stepsize_t *ss = NULL;
+
+                               tcd->band = &res->bands[bandno];
+                               band = tcd->band;
+
+                               band->bandno = (resno == 0) ? 0 : bandno + 1;
+                               /* Bandno:      0 - LLL         2 - LHL 
+                                                       1 - HLL         3 - HHL
+                                                       4 - LLH         6 - LHH
+                                                       5 - HLH         7 - HHH         */
+                               x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0; 
+                               y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
+                               z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; 
+                               
+                               /* p. 65, B.15, ISO/IEC IS15444-1 : 2002  */
+                               if (band->bandno == 0) {
+                                       /* band border (global) */
+                                       band->x0 = int_ceildivpow2(tilec->x0, levelnox);
+                                       band->y0 = int_ceildivpow2(tilec->y0, levelnoy);
+                                       band->z0 = int_ceildivpow2(tilec->z0, levelnoz);
+                                       band->x1 = int_ceildivpow2(tilec->x1, levelnox);
+                                       band->y1 = int_ceildivpow2(tilec->y1, levelnoy);
+                                       band->z1 = int_ceildivpow2(tilec->z1, levelnoz);
+                               } else {
+                                       band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);
+                                       band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);
+                                       band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
+                                       band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);
+                                       band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);
+                                       band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
+                               }
+                               
+                               ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];
+                               if (bandno == (res->numbands - 1)) 
+                                       prevnumbands += (resno == 0) ? 0 : res->numbands;
+                               gain = dwt_getgain(band->bandno,tccp->reversible);                                      
+                               numbps = volume->comps[compno].prec + gain;
+                               band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
+                               band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
+                               
+                               band->precincts = (opj_tcd_precinct_t *) opj_malloc((res->prctno[0] * res->prctno[1] * res->prctno[2]) * sizeof(opj_tcd_precinct_t));
+                               
+                               for (i = 0; i < (res->prctno[0] * res->prctno[1] * res->prctno[2]); i++) {
+                                       band->precincts[i].imsbtree = NULL;
+                                       band->precincts[i].incltree = NULL;
+                               }
+
+                               for (precno = 0; precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); precno++) {
+                                       int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;
+                                       int cbgxstart, cbgystart, cbgzstart, cbgxend, cbgyend, cbgzend;
+
+                                       cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);
+                                       cbgystart = tlcbgystart + ((precno % (res->prctno[0] * res->prctno[1])) / res->prctno[0]) * (1 << cbgheightexpn);
+                                       cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);
+                                       cbgxend = cbgxstart + (1 << cbgwidthexpn);
+                                       cbgyend = cbgystart + (1 << cbgheightexpn);
+                                       cbgzend = cbgzstart + (1 << cbglengthexpn);
+                                       
+                                       tcd->prc = &band->precincts[precno];
+                                       prc = tcd->prc;
+
+                                       /* precinct size (global) */
+                                       prc->x0 = int_max(cbgxstart, band->x0);
+                                       prc->y0 = int_max(cbgystart, band->y0);
+                                       prc->z0 = int_max(cbgzstart, band->z0);
+                                       prc->x1 = int_min(cbgxend, band->x1);
+                                       prc->y1 = int_min(cbgyend, band->y1);
+                                       prc->z1 = int_min(cbgzend, band->z1);
+                                       
+                                       tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
+                                       tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
+                                       tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;
+                                       brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
+                                       brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
+                                       brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;
+                                       prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
+                                       prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;
+                                       prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;
+                                       prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];
+
+                                       prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));
+                                       prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
+                                       prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
+                                       /*tgt_tree_dump(stdout,prc->incltree);*/
+                                       for (cblkno = 0; cblkno < (prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]); cblkno++) {
+                                               int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);
+                                               int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);
+                                               int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);
+                                               int cblkxend = cblkxstart + (1 << cblkwidthexpn);
+                                               int cblkyend = cblkystart + (1 << cblkheightexpn);
+                                               int cblkzend = cblkzstart + (1 << cblklengthexpn);
+                                               int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));
+                                               
+                                               tcd->cblk = &prc->cblks[cblkno];
+                                               cblk = tcd->cblk;
+
+                                               /* code-block size (global) */
+                                               cblk->x0 = int_max(cblkxstart, prc->x0);
+                                               cblk->y0 = int_max(cblkystart, prc->y0);
+                                               cblk->z0 = int_max(cblkzstart, prc->z0);
+                                               cblk->x1 = int_min(cblkxend, prc->x1);
+                                               cblk->y1 = int_min(cblkyend, prc->y1);
+                                               cblk->z1 = int_min(cblkzend, prc->z1);
+                                       }                                       
+                               }
+                       }
+               }
+       }
+       /*tcd_dump(stdout, tcd, tcd->tcd_volume);*/
+
+}
+void tcd_init_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno) {
+       int compno, resno, bandno, precno, cblkno;
+       int j, p, q, r;
+
+       opj_tcd_tile_t          *tile = NULL;           /* pointer to tcd->tile */
+       opj_tcd_tilecomp_t      *tilec = NULL;          /* pointer to tcd->tilec */
+       opj_tcd_resolution_t    *res = NULL;    /* pointer to tcd->res */
+       opj_tcd_band_t          *band = NULL;           /* pointer to tcd->band */
+       opj_tcd_precinct_t      *prc = NULL;            /* pointer to tcd->prc */
+       opj_tcd_cblk_t          *cblk = NULL;           /* pointer to tcd->cblk */
+       opj_tcp_t *tcp = &cp->tcps[curtileno];
+
+       tcd->tile = tcd->tcd_volume->tiles;
+       tile = tcd->tile;
+
+       /* p61 ISO/IEC IS15444-1 : 2002 */
+       /* curtileno --> raster scanned index of tiles */
+       /* p,q,r --> matricial index of tiles */
+       p = curtileno % cp->tw; 
+       q = curtileno / cp->tw; 
+       r = curtileno / (cp->tw * cp->th); /* extension to 3-D */
+       
+       /* 4 borders of the tile rescale on the volume if necessary (B.3)*/
+       tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
+       tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
+       tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
+       tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
+       tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
+       tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
+       tile->numcomps = volume->numcomps;
+
+       /* Modification of the RATE >> */
+       for (j = 0; j < tcp->numlayers; j++) {
+               if (tcp->rates[j] <= 1) {
+                       tcp->rates[j] = 0;
+               } else {
+                       float num = (float) (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec);
+                       float den = (float) (8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz);
+                       den = tcp->rates[j] * den;
+                       tcp->rates[j] = (num + den - 1) / den;
+               }
+               /*tcp->rates[j] = tcp->rates[j] ? int_ceildiv(
+                       tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec,
+            (tcp->rates[j] * 8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz)) : 0;*/
+               if (tcp->rates[j]) {
+                       if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
+                               tcp->rates[j] = tcp->rates[j - 1] + 20;
+                       } else if (!j && tcp->rates[j] < 30){
+                               tcp->rates[j] = 30;
+                       }
+               }
+       }
+       /* << Modification of the RATE */
+
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               opj_tccp_t *tccp = &tcp->tccps[compno];
+               int res_max, i;
+               int prevnumbands = 0;
+
+               /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */
+               tcd->tilec = &tile->comps[compno];
+               tilec = tcd->tilec;
+
+               /* border of each tile component (global) (B.3) */
+               tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);
+               tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);
+               tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);
+               tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);
+               tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);
+               tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);
+
+               tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));
+               
+               res_max = 0;
+               for (i = 0;i < 3; i++){
+                       tilec->numresolution[i] = tccp->numresolution[i];
+                       /*Greater of 3 resolutions contains all information*/
+                       res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;
+               }
+
+               tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));
+               for (resno = 0; resno < res_max; resno++) {
+                       int pdx, pdy, pdz;
+                       int tlprcxstart, tlprcystart, tlprczstart, brprcxend, brprcyend, brprczend;
+                       int tlcbgxstart, tlcbgystart, tlcbgzstart, brcbgxend, brcbgyend, brcbgzend;
+                       int cbgwidthexpn, cbgheightexpn, cbglengthexpn;
+                       int cblkwidthexpn, cblkheightexpn, cblklengthexpn;
+                       
+                       int levelnox = tilec->numresolution[0] - 1 - resno; 
+                       int levelnoy = tilec->numresolution[1] - 1 - resno;
+                       int diff = tccp->numresolution[0] - tccp->numresolution[2]; 
+                       int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));
+                               if (levelnoz < 0) levelnoz = 0;
+
+                       tcd->res = &tilec->resolutions[resno];
+                       res = tcd->res;
+                       
+                       /* border for each resolution level (global) (B.14)*/
+                       res->x0 = int_ceildivpow2(tilec->x0, levelnox);
+                       res->y0 = int_ceildivpow2(tilec->y0, levelnoy);
+                       res->z0 = int_ceildivpow2(tilec->z0, levelnoz);
+                       res->x1 = int_ceildivpow2(tilec->x1, levelnox);
+                       res->y1 = int_ceildivpow2(tilec->y1, levelnoy);
+                       res->z1 = int_ceildivpow2(tilec->z1, levelnoz);
+
+                       /* res->numbands = resno == 0 ? 1 : 3; *//* --> 2D */
+
+                       res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */
+
+                       /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */                      
+                       if (tccp->csty & J3D_CCP_CSTY_PRT) {
+                               pdx = tccp->prctsiz[0][resno];
+                               pdy = tccp->prctsiz[1][resno];
+                               pdz = tccp->prctsiz[2][resno];
+                       } else {
+                               pdx = 15;
+                               pdy = 15;
+                               pdz = 15;
+                       }
+                       /* p. 66, B.16, ISO/IEC IS15444-1 : 2002  */
+                       tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
+                       tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
+                       tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;
+                       brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
+                       brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
+                       brprczend = int_ceildivpow2(res->z1, pdz) << pdz;
+                       
+                       res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;
+                       res->prctno[1] = (brprcyend - tlprcystart) >> pdy;
+                       res->prctno[2] = (brprczend - tlprczstart) >> pdz;
+                       if (res->prctno[2] == 0) res->prctno[2] = 1;
+
+                       /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002  */
+                       if (resno == 0) {
+                               tlcbgxstart = tlprcxstart;
+                               tlcbgystart = tlprcystart;
+                               tlcbgzstart = tlprczstart;
+                               brcbgxend = brprcxend;
+                               brcbgyend = brprcyend;
+                               brcbgzend = brprczend;
+                               cbgwidthexpn = pdx;
+                               cbgheightexpn = pdy;
+                               cbglengthexpn = pdz;
+                       } else {
+                               tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
+                               tlcbgystart = int_ceildivpow2(tlprcystart, 1);
+                               tlcbgzstart = int_ceildivpow2(tlprczstart, 1);
+                               brcbgxend = int_ceildivpow2(brprcxend, 1);
+                               brcbgyend = int_ceildivpow2(brprcyend, 1);
+                               brcbgzend = int_ceildivpow2(brprczend, 1);
+                               cbgwidthexpn = pdx - 1;
+                               cbgheightexpn = pdy - 1;
+                               cbglengthexpn = pdz - 1;
+                       }
+                       
+                       cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn);
+                       cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn);
+                       cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn);
+                       
+                       res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));
+                       for (bandno = 0; bandno < res->numbands; bandno++) {
+                               int x0b, y0b, z0b;
+                               int gain, numbps;
+                               opj_stepsize_t *ss = NULL;
+
+                               tcd->band = &res->bands[bandno];
+                               band = tcd->band;
+
+                               band->bandno = resno == 0 ? 0 : bandno + 1;
+                               /* Bandno:      0 - LLL         2 - LHL 
+                                                       1 - HLL         3 - HHL
+                                                       4 - LLH         6 - LHH
+                                                       5 - HLH         7 - HHH         */
+                               x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0; 
+                               y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
+                               z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; 
+                               
+                               /* p. 65, B.15, ISO/IEC IS15444-1 : 2002  */
+                               if (band->bandno == 0) {
+                                       /* band border (global) */
+                                       band->x0 = int_ceildivpow2(tilec->x0, levelnox);
+                                       band->y0 = int_ceildivpow2(tilec->y0, levelnoy);
+                                       band->z0 = int_ceildivpow2(tilec->z0, levelnoz);
+                                       band->x1 = int_ceildivpow2(tilec->x1, levelnox);
+                                       band->y1 = int_ceildivpow2(tilec->y1, levelnoy);
+                                       band->z1 = int_ceildivpow2(tilec->z1, levelnoz);
+                               } else {
+                                       band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);
+                                       band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);
+                                       band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
+                                       band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);
+                                       band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);
+                                       band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
+                               }
+                                                               
+                               ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];
+                               if (bandno == (res->numbands - 1)) 
+                                       prevnumbands += (resno == 0) ? 0 : res->numbands;
+                               gain = dwt_getgain(band->bandno,tccp->reversible);                                      
+                               numbps = volume->comps[compno].prec + gain;
+                               
+                               band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
+                               band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
+                               
+                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
+                                       int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;
+
+                                       int cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);
+                                       int cbgystart = tlcbgystart + ((precno / (res->prctno[0] * res->prctno[1])) / res->prctno[0]) * (1 << cbgheightexpn);
+                                       int cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);
+                                       int cbgxend = cbgxstart + (1 << cbgwidthexpn);
+                                       int cbgyend = cbgystart + (1 << cbgheightexpn);
+                                       int cbgzend = cbgzstart + (1 << cbglengthexpn);
+
+                                       /* opj_tcd_precinct_t *prc=&band->precincts[precno]; */
+                                       tcd->prc = &band->precincts[precno];
+                                       prc = tcd->prc;
+
+                                       /* precinct size (global) */
+                                       prc->x0 = int_max(cbgxstart, band->x0);
+                                       prc->y0 = int_max(cbgystart, band->y0);
+                                       prc->z0 = int_max(cbgzstart, band->z0);
+                                       prc->x1 = int_min(cbgxend, band->x1);
+                                       prc->y1 = int_min(cbgyend, band->y1);
+                                       prc->z1 = int_min(cbgzend, band->z1);
+
+                                       tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
+                                       tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
+                                       tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;
+                                       brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
+                                       brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
+                                       brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;
+                                       prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
+                                       prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;
+                                       prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;
+                                       prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];
+
+                                       opj_free(prc->cblks);
+                                       prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));
+                                       prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
+                                       prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
+
+                                       for (cblkno = 0; cblkno < (prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]); cblkno++) {
+                                                       int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);
+                                                       int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);
+                                                       int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);
+                                                       int cblkxend = cblkxstart + (1 << cblkwidthexpn);
+                                                       int cblkyend = cblkystart + (1 << cblkheightexpn);
+                                                       int cblkzend = cblkzstart + (1 << cblklengthexpn);
+                                                       int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));
+
+                                                       tcd->cblk = &prc->cblks[cblkno];
+                                                       cblk = tcd->cblk;
+
+                                                       /* code-block size (global) */
+                                                       cblk->x0 = int_max(cblkxstart, prc->x0);
+                                                       cblk->y0 = int_max(cblkystart, prc->y0);
+                                                       cblk->z0 = int_max(cblkzstart, prc->z0);
+                                                       cblk->x1 = int_min(cblkxend, prc->x1);
+                                                       cblk->y1 = int_min(cblkyend, prc->y1);
+                                                       cblk->z1 = int_min(cblkzend, prc->z1);
+                                       }
+                               } /* precno */
+                       } /* bandno */
+               } /* resno */
+       } /* compno */
+       /*tcd_dump(stdout, tcd, tcd->tcd_volume);*/
+}
+
+
+void tcd_free_encode(opj_tcd_t *tcd) {
+       int tileno, compno, resno, bandno, precno;
+
+       opj_tcd_tile_t *tile = NULL;            /* pointer to tcd->tile         */
+/*     opj_tcd_slice_t *slice = NULL; */               /* pointer to tcd->slice */
+       opj_tcd_tilecomp_t *tilec = NULL;       /* pointer to tcd->tilec        */
+       opj_tcd_resolution_t *res = NULL;       /* pointer to tcd->res          */
+       opj_tcd_band_t *band = NULL;            /* pointer to tcd->band         */
+       opj_tcd_precinct_t *prc = NULL;         /* pointer to tcd->prc          */
+
+       for (tileno = 0; tileno < 1; tileno++) {
+               tcd->tile = tcd->tcd_volume->tiles;
+               tile = tcd->tile;
+
+               for (compno = 0; compno < tile->numcomps; compno++) {
+                       tcd->tilec = &tile->comps[compno];
+                       tilec = tcd->tilec;
+
+                       for (resno = 0; resno < tilec->numresolution[0]; resno++) {
+                               tcd->res = &tilec->resolutions[resno];
+                               res = tcd->res;
+
+                               for (bandno = 0; bandno < res->numbands; bandno++) {
+                                       tcd->band = &res->bands[bandno];
+                                       band = tcd->band;
+
+                                       for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
+                                               tcd->prc = &band->precincts[precno];
+                                               prc = tcd->prc;
+
+                                               if (prc->incltree != NULL) {
+                            tgt_destroy(prc->incltree);
+                            prc->incltree = NULL;
+                                               }
+                                               if (prc->imsbtree != NULL) {
+                            tgt_destroy(prc->imsbtree);
+                            prc->imsbtree = NULL;
+                                               }
+                                               opj_free(prc->cblks);
+                                               prc->cblks = NULL;
+                                       } /* for (precno */
+                                       opj_free(band->precincts);
+                                       band->precincts = NULL;
+                               } /* for (bandno */
+                       } /* for (resno */
+                       opj_free(tilec->resolutions);
+                       tilec->resolutions = NULL;
+               } /* for (compno */
+               opj_free(tile->comps);
+               tile->comps = NULL;
+       } /* for (tileno */
+       opj_free(tcd->tcd_volume->tiles);
+       tcd->tcd_volume->tiles = NULL;
+}
+
+/* ----------------------------------------------------------------------- */
+void tcd_malloc_decode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp) {
+       int tileno, compno, resno, bandno, precno, cblkno, res_max,
+               i, j, p, q, r;
+       unsigned int x0 = 0, y0 = 0, z0 = 0, 
+               x1 = 0, y1 = 0, z1 = 0, 
+               w, h, l;
+
+       tcd->volume = volume;
+       tcd->cp = cp;
+       tcd->tcd_volume->tw = cp->tw;
+       tcd->tcd_volume->th = cp->th;
+       tcd->tcd_volume->tl = cp->tl;
+       tcd->tcd_volume->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcd_tile_t));
+       
+       for (i = 0; i < cp->tileno_size; i++) {
+               opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]);
+               opj_tcd_tile_t *tile = &(tcd->tcd_volume->tiles[cp->tileno[i]]);
+       
+               /* p61 ISO/IEC IS15444-1 : 2002 */
+               /* curtileno --> raster scanned index of tiles */
+               /* p,q,r --> matricial index of tiles */
+               tileno = cp->tileno[i];
+               p = tileno % cp->tw;    
+               q = tileno / cp->tw;    
+               r = tileno / (cp->tw * cp->th); /* extension to 3-D */
+
+               /* 4 borders of the tile rescale on the volume if necessary (B.3)*/
+               tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
+               tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
+               tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
+               tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
+               tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
+               tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
+               tile->numcomps = volume->numcomps;              
+               
+               tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(volume->numcomps * sizeof(opj_tcd_tilecomp_t));
+               for (compno = 0; compno < tile->numcomps; compno++) {
+                       opj_tccp_t *tccp = &tcp->tccps[compno];
+                       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+                       int prevnumbands = 0;
+
+                       /* border of each tile component (global) */
+                       tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);
+                       tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);
+                       tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);
+                       tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);
+                       tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);
+                       tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);
+                       
+                       tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));
+
+                       res_max = 0;
+                       for (i = 0;i < 3; i++){
+                               tilec->numresolution[i] = tccp->numresolution[i];
+                               /*Greater of 3 resolutions contains all information*/
+                               res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;
+                       }
+
+                       tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));
+
+                       for (resno = 0; resno < res_max; resno++) {
+                               opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+                               int pdx, pdy, pdz;
+                               int tlprcxstart, tlprcystart, tlprczstart, brprcxend, brprcyend, brprczend;
+                               int tlcbgxstart, tlcbgystart, tlcbgzstart, brcbgxend, brcbgyend, brcbgzend;
+                               int cbgwidthexpn, cbgheightexpn, cbglengthexpn;
+                               int cblkwidthexpn, cblkheightexpn, cblklengthexpn;
+                               int levelnox = tilec->numresolution[0] - 1 - resno; 
+                               int levelnoy = tilec->numresolution[1] - 1 - resno;
+                               int diff = tccp->numresolution[0] - tccp->numresolution[2]; 
+                               int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));
+                                       if (levelnoz < 0) levelnoz = 0;
+
+                               /* border for each resolution level (global) */
+                               res->x0 = int_ceildivpow2(tilec->x0, levelnox);
+                               res->y0 = int_ceildivpow2(tilec->y0, levelnoy);
+                               res->z0 = int_ceildivpow2(tilec->z0, levelnoz);
+                               res->x1 = int_ceildivpow2(tilec->x1, levelnox);
+                               res->y1 = int_ceildivpow2(tilec->y1, levelnoy);
+                               res->z1 = int_ceildivpow2(tilec->z1, levelnoz);
+                               res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */
+                               
+                               /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */
+                               if (tccp->csty & J3D_CCP_CSTY_PRT) {
+                                       pdx = tccp->prctsiz[0][resno];
+                                       pdy = tccp->prctsiz[1][resno];
+                                       pdz = tccp->prctsiz[2][resno];
+                               } else {
+                                       pdx = 15;
+                                       pdy = 15;
+                                       pdz = 15;
+                               }
+                               
+                               /* p. 66, B.16, ISO/IEC IS15444-1 : 2002  */
+                               tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
+                               tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
+                               tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;
+                               brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
+                               brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
+                               brprczend = int_ceildivpow2(res->z1, pdz) << pdz;
+                               
+                               res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;
+                               res->prctno[1] = (brprcyend - tlprcystart) >> pdy;
+                               res->prctno[2] = (brprczend - tlprczstart) >> pdz;
+                               
+                               /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002  */
+                               if (resno == 0) {
+                                       tlcbgxstart = tlprcxstart;/*0*/
+                                       tlcbgystart = tlprcystart;
+                                       tlcbgzstart = tlprczstart;
+                                       brcbgxend = brprcxend;/*1*/
+                                       brcbgyend = brprcyend;
+                                       brcbgzend = brprczend;
+                                       cbgwidthexpn = pdx; /*15*/
+                                       cbgheightexpn = pdy;
+                                       cbglengthexpn = pdz;
+                               } else {
+                                       tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
+                                       tlcbgystart = int_ceildivpow2(tlprcystart, 1);
+                                       tlcbgzstart = int_ceildivpow2(tlprczstart, 1);
+                                       brcbgxend = int_ceildivpow2(brprcxend, 1);
+                                       brcbgyend = int_ceildivpow2(brprcyend, 1);
+                                       brcbgzend = int_ceildivpow2(brprczend, 1);
+                                       cbgwidthexpn = pdx - 1;
+                                       cbgheightexpn = pdy - 1;
+                                       cbglengthexpn = pdz - 1;
+                               }
+                               
+                               cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); /*6*/
+                               cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); /*6*/
+                               cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); /*6*/
+
+                               res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));
+                               for (bandno = 0; bandno < res->numbands; bandno++) {
+                                       int x0b, y0b, z0b;
+                                       int gain, numbps;
+                                       opj_stepsize_t *ss = NULL;
+
+                                       opj_tcd_band_t *band = &res->bands[bandno];
+                                       band->bandno = resno == 0 ? 0 : bandno + 1;
+                                       /* Bandno:      0 - LLL         2 - LHL 
+                                                               1 - HLL         3 - HHL
+                                                               4 - LLH         6 - LHH
+                                                               5 - HLH         7 - HHH         */
+                                       x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0; 
+                                       y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
+                                       z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; 
+                                       
+                                       /* p. 65, B.15, ISO/IEC IS15444-1 : 2002  */
+                                       if (band->bandno == 0) {
+                                               /* band border (global) */
+                                               band->x0 = int_ceildivpow2(tilec->x0, levelnox);
+                                               band->y0 = int_ceildivpow2(tilec->y0, levelnoy);
+                                               band->z0 = int_ceildivpow2(tilec->z0, levelnoz);
+                                               band->x1 = int_ceildivpow2(tilec->x1, levelnox);
+                                               band->y1 = int_ceildivpow2(tilec->y1, levelnoy);
+                                               band->z1 = int_ceildivpow2(tilec->z1, levelnoz);
+                                       } else {
+                                               band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);
+                                               band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);
+                                               band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
+                                               band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);
+                                               band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);
+                                               band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
+                                       }       
+
+                                       ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];
+                                       if (bandno == (res->numbands - 1)) 
+                                               prevnumbands += (resno == 0) ? 0 : res->numbands;
+                                       gain = dwt_getgain(band->bandno,tccp->reversible);                                      
+                                       numbps = volume->comps[compno].prec + gain;
+
+                                       band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
+                                       band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
+                                       
+                                       band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->prctno[0] * res->prctno[1] * res->prctno[2] * sizeof(opj_tcd_precinct_t));
+                                       
+                                       for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
+                                               int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;
+
+                                               int cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);
+                                               int cbgystart = tlcbgystart + (precno / res->prctno[0]) * (1 << cbgheightexpn);
+                                               int cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);
+                                               int cbgxend = cbgxstart + (1 << cbgwidthexpn);
+                                               int cbgyend = cbgystart + (1 << cbgheightexpn);
+                                               int cbgzend = cbgzstart + (1 << cbglengthexpn);
+
+                                               opj_tcd_precinct_t *prc = &band->precincts[precno];
+                                               /* precinct size (global) */
+                                               prc->x0 = int_max(cbgxstart, band->x0);
+                                               prc->y0 = int_max(cbgystart, band->y0);
+                                               prc->z0 = int_max(cbgzstart, band->z0);
+                                               prc->x1 = int_min(cbgxend, band->x1);
+                                               prc->y1 = int_min(cbgyend, band->y1);
+                                               prc->z1 = int_min(cbgzend, band->z1);
+
+                                               tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
+                                               tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
+                                               tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;
+                                               brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
+                                               brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
+                                               brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;
+                                               prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
+                                               prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;
+                                               prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;
+                                               prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];
+
+                                               prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));
+                                               prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
+                                               prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
+                                               
+                                               for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                                                       int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);
+                                                       int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);
+                                                       int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);
+                                                       int cblkxend = cblkxstart + (1 << cblkwidthexpn);
+                                                       int cblkyend = cblkystart + (1 << cblkheightexpn);
+                                                       int cblkzend = cblkzstart + (1 << cblklengthexpn);
+                                                       int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));
+                                                       /* code-block size (global) */
+                                                       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                                                       
+                                                       /* code-block size (global) */
+                                                       cblk->x0 = int_max(cblkxstart, prc->x0);
+                                                       cblk->y0 = int_max(cblkystart, prc->y0);
+                                                       cblk->z0 = int_max(cblkzstart, prc->z0);
+                                                       cblk->x1 = int_min(cblkxend, prc->x1);
+                                                       cblk->y1 = int_min(cblkyend, prc->y1);
+                                                       cblk->z1 = int_min(cblkzend, prc->z1);
+                                               }
+                                       } /* precno */
+                               } /* bandno */
+                       } /* resno */
+               } /* compno */
+       } /* i = 0..cp->tileno_size */
+
+       /*tcd_dump(stdout, tcd, tcd->tcd_volume);*/
+
+       /* 
+       Allocate place to store the decoded data = final volume
+       Place limited by the tile really present in the codestream 
+       */
+       
+       for (i = 0; i < volume->numcomps; i++) {
+               for (j = 0; j < cp->tileno_size; j++) {
+                       tileno = cp->tileno[j];
+                       x0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].x0 : int_min(x0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].x0);
+                       y0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].y0 : int_min(y0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].y0);
+                       z0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].z0 : int_min(z0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].z0);
+                       x1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].x1 : int_max(x1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].x1);
+                       y1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].y1 : int_max(y1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].y1);
+                       z1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].z1 : int_max(z1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].z1);
+               }
+               
+               w = x1 - x0;
+               h = y1 - y0;
+               l = z1 - z0;
+               
+               volume->comps[i].data = (int *) opj_malloc(w * h * l * sizeof(int));
+               volume->comps[i].w = w;
+               volume->comps[i].h = h;
+               volume->comps[i].l = l;
+               volume->comps[i].x0 = x0;
+               volume->comps[i].y0 = y0;
+               volume->comps[i].z0 = z0;
+               volume->comps[i].bigendian = cp->bigendian;
+       }
+}
+
+void tcd_free_decode(opj_tcd_t *tcd) {
+       int tileno,compno,resno,bandno,precno;
+
+       opj_tcd_volume_t *tcd_volume = tcd->tcd_volume;
+       
+       for (tileno = 0; tileno < tcd_volume->tw * tcd_volume->th * tcd_volume->tl; tileno++) {
+               opj_tcd_tile_t *tile = &tcd_volume->tiles[tileno];
+               for (compno = 0; compno < tile->numcomps; compno++) {
+                       opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+                       for (resno = 0; resno < tilec->numresolution[0]; resno++) {
+                               opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+                               for (bandno = 0; bandno < res->numbands; bandno++) {
+                                       opj_tcd_band_t *band = &res->bands[bandno];
+                                       for (precno = 0; precno < res->prctno[1] * res->prctno[0] * res->prctno[2]; precno++) {
+                                               opj_tcd_precinct_t *prec = &band->precincts[precno];
+                                               if (prec->cblks != NULL) opj_free(prec->cblks);
+                                               if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
+                        if (prec->incltree != NULL) tgt_destroy(prec->incltree);
+                                               /*for (treeno = 0; treeno < prec->numtrees; treeno++){
+                            if (prec->imsbtree[treeno] != NULL) tgt_destroy(prec->imsbtree[treeno]);
+                            if (prec->incltree[treeno] != NULL) tgt_destroy(prec->incltree[treeno]);
+                                               }*/
+                                       }
+                                       if (band->precincts != NULL) opj_free(band->precincts);
+                               }
+                       }
+                       if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
+               }
+               if (tile->comps != NULL) opj_free(tile->comps);
+       }
+
+       if (tcd_volume->tiles != NULL) opj_free(tcd_volume->tiles);
+}
+
+
+
+/* ----------------------------------------------------------------------- */
+void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
+       int compno, resno, bandno, precno, cblkno;
+       int value;                      /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolution[0]][3]; */
+       int matrice[10][10][3];
+       int i, j, k;
+
+       opj_cp_t *cp = tcd->cp;
+       opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
+       opj_tcp_t *tcd_tcp = tcd->tcp;
+
+       /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolution[0]*3*sizeof(int)); */
+       
+       for (compno = 0; compno < tcd_tile->numcomps; compno++) {
+               opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
+               for (i = 0; i < tcd_tcp->numlayers; i++) {
+                       for (j = 0; j < tilec->numresolution[0]; j++) {
+                               for (k = 0; k < 3; k++) {
+                                       matrice[i][j][k] =
+                                               (int) (cp->matrice[i * tilec->numresolution[0] * 3 + j * 3 + k] 
+                                               * (float) (tcd->volume->comps[compno].prec / 16.0));
+                               }
+                       }
+               }
+        
+               for (resno = 0; resno < tilec->numresolution[0]; resno++) {
+                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+                       for (bandno = 0; bandno < res->numbands; bandno++) {
+                               opj_tcd_band_t *band = &res->bands[bandno];
+                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
+                                       opj_tcd_precinct_t *prc = &band->precincts[precno];
+                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                                               opj_tcd_layer_t *layer = &cblk->layers[layno];
+                                               int n;
+                                               int imsb = tcd->volume->comps[compno].prec - cblk->numbps;      /* number of bit-plan equal to zero */
+                                               /* Correction of the matrix of coefficient to include the IMSB information */
+                                               if (layno == 0) {
+                                                       value = matrice[layno][resno][bandno];
+                                                       if (imsb >= value) {
+                                                               value = 0;
+                                                       } else {
+                                                               value -= imsb;
+                                                       }
+                                               } else {
+                                                       value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
+                                                       if (imsb >= matrice[layno - 1][resno][bandno]) {
+                                                               value -= (imsb - matrice[layno - 1][resno][bandno]);
+                                                               if (value < 0) {
+                                                                       value = 0;
+                                                               }
+                                                       }
+                                               }
+                                               
+                                               if (layno == 0) {
+                                                       cblk->numpassesinlayers = 0;
+                                               }
+                                               
+                                               n = cblk->numpassesinlayers;
+                                               if (cblk->numpassesinlayers == 0) {
+                                                       if (value != 0) {
+                                                               n = 3 * value - 2 + cblk->numpassesinlayers;
+                                                       } else {
+                                                               n = cblk->numpassesinlayers;
+                                                       }
+                                               } else {
+                                                       n = 3 * value + cblk->numpassesinlayers;
+                                               }
+                                               
+                                               layer->numpasses = n - cblk->numpassesinlayers;
+                                               
+                                               if (!layer->numpasses)
+                                                       continue;
+                                               
+                                               if (cblk->numpassesinlayers == 0) {
+                                                       layer->len = cblk->passes[n - 1].rate;
+                                                       layer->data = cblk->data;
+                                               } else {
+                                                       layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
+                                                       layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
+                                               }
+                                               if (final)
+                                                       cblk->numpassesinlayers = n;
+                                       }
+                               }
+                       }
+               }
+       }
+}
+
+void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
+       int layno;
+       for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
+               tcd_makelayer_fixed(tcd, layno, 1);
+       }
+}
+
+void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
+       int compno, resno, bandno, precno, cblkno, passno;
+       
+       opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
+
+       tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
+       
+       for (compno = 0; compno < tcd_tile->numcomps; compno++) {
+               opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
+               for (resno = 0; resno < tilec->numresolution[0]; resno++) {
+                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+                       for (bandno = 0; bandno < res->numbands; bandno++) {
+                               opj_tcd_band_t *band = &res->bands[bandno];
+                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
+                                       opj_tcd_precinct_t *prc = &band->precincts[precno];
+                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                                               opj_tcd_layer_t *layer = &cblk->layers[layno];
+                                               
+                                               int n;
+                                               if (layno == 0) {
+                                                       cblk->numpassesinlayers = 0;
+                                               }
+                                               n = cblk->numpassesinlayers;
+                                               for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
+                                                       int dr;
+                                                       double dd;
+                                                       opj_tcd_pass_t *pass = &cblk->passes[passno];
+                                                       if (n == 0) {
+                                                               dr = pass->rate;
+                                                               dd = pass->distortiondec;
+                                                       } else {
+                                                               dr = pass->rate - cblk->passes[n - 1].rate;
+                                                               dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
+                                                       }
+                                                       if (!dr) {
+                                                               if (dd)
+                                                                       n = passno + 1;
+                                                               continue;
+                                                       }
+                                                       if (dd / dr >= thresh){
+                                                               n = passno + 1;
+                                                       }
+                                               }
+                                               layer->numpasses = n - cblk->numpassesinlayers;
+                                               
+                                               if (!layer->numpasses) {
+                                                       layer->disto = 0;
+                                                       continue;
+                                               }
+                                               if (cblk->numpassesinlayers == 0) {
+                                                       layer->len = cblk->passes[n - 1].rate;
+                                                       layer->data = cblk->data;
+                                                       layer->disto = cblk->passes[n - 1].distortiondec;
+                                               } else {
+                                                       layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
+                                                       layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
+                                                       layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
+                                               }
+                                               
+                                               tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
+                                               
+                                               if (final)
+                                                       cblk->numpassesinlayers = n;
+
+                                       /*      fprintf(stdout,"MakeLayer : %d %f %d %d \n",layer->len, layer->disto, layer->numpasses, n);*/
+                                       }
+                               }
+                       }
+               }
+       }
+}
+
+bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_volume_info_t * volume_info) {
+       int compno, resno, bandno, precno, cblkno, passno, layno;
+       double min, max;
+       double cumdisto[100];   /* fixed_quality */
+       const double K = 1;             /* 1.1; // fixed_quality */
+       double maxSE = 0;
+
+       opj_cp_t *cp = tcd->cp;
+       opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
+       opj_tcp_t *tcd_tcp = tcd->tcp;
+
+       min = DBL_MAX;
+       max = 0;
+       
+       tcd_tile->nbpix = 0;            /* fixed_quality */
+       
+       for (compno = 0; compno < tcd_tile->numcomps; compno++) {
+               opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
+               tilec->nbpix = 0;
+               for (resno = 0; resno < tilec->numresolution[0]; resno++) {
+                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+                       for (bandno = 0; bandno < res->numbands; bandno++) {
+                               opj_tcd_band_t *band = &res->bands[bandno];
+                               for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
+                                       opj_tcd_precinct_t *prc = &band->precincts[precno];
+                                       for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
+                                               opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                                               for (passno = 0; passno < cblk->totalpasses; passno++) {
+                                                       opj_tcd_pass_t *pass = &cblk->passes[passno];
+                                                       int dr;
+                                                       double dd, rdslope;
+                                                       if (passno == 0) {
+                                                               dr = pass->rate;
+                                                               dd = pass->distortiondec;
+                                                       } else {
+                                                               dr = pass->rate - cblk->passes[passno - 1].rate;
+                                                               dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
+                                                       }
+                                                       if (dr == 0) {
+                                                               continue;
+                                                       }
+                                                       rdslope = dd / dr;
+                                                       if (rdslope < min) {
+                                                               min = rdslope;
+                                                       }
+                                                       if (rdslope > max) {
+                                                               max = rdslope;
+                                                       }
+
+                                               } /* passno */
+                                               
+                                               /* fixed_quality */
+                                               tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0) * (cblk->z1 - cblk->z0));
+                        tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0) * (cblk->z1 - cblk->z0));
+                                       } /* cbklno */ 
+                               } /* precno */
+                       } /* bandno */
+               } /* resno */
+               
+               maxSE += (((double)(1 << tcd->volume->comps[compno].prec) - 1.0) 
+                       * ((double)(1 << tcd->volume->comps[compno].prec) -1.0)) 
+                       * ((double)(tilec->nbpix));
+       } /* compno */
+       
+       /* add antonin index */
+       if(volume_info && volume_info->index_on) {
+               opj_tile_info_t *info_TL = &volume_info->tile[tcd->tcd_tileno];
+               info_TL->nbpix = tcd_tile->nbpix;
+               info_TL->distotile = tcd_tile->distotile;
+               info_TL->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
+       }
+       /* dda */
+       
+       for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
+               double lo = min;
+               double hi = max;
+               int success = 0;
+               int maxlen = tcd_tcp->rates[layno] ? int_min(((int) tcd_tcp->rates[layno]), len) : len;
+               double goodthresh;
+               double distotarget;             /* fixed_quality */
+               int i = 0;
+               
+        /* fixed_quality */
+               distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
+        
+               if ((tcd_tcp->rates[layno]) || (cp->disto_alloc==0)) {
+                       opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->volume, cp);
+                       int oldl = 0, oldoldl = 0;
+                       for (i = 0; i < 128; i++) {
+                               double thresh = (lo + hi) / 2;
+                               int l = 0;
+                               double distoachieved = 0;       /* fixed_quality -q */
+                       
+                               tcd_makelayer(tcd, layno, thresh, 0);
+               
+                               if (cp->fixed_quality) {        /* fixed_quality -q */
+                                       distoachieved = (layno == 0) ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
+                                       if (distoachieved < distotarget) {
+                                               hi = thresh; 
+                                               continue;
+                                       }
+                                       lo = thresh;
+                               } else {                /* disto_alloc -r, fixed_alloc -f */
+                                       l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, volume_info);
+                                       /*fprintf(stdout, "layno %d i %d len=%d max=%d \n",layno,i,l,maxlen);*/
+                                       if (l == -999) {
+                                               lo = thresh; 
+                                               continue;
+                                       } else if (l == oldl && oldl == oldoldl && tcd_tile->distolayer[layno] > 0.0 && i>32)
+                                               break;
+                                       hi = thresh;
+                                       oldoldl = oldl;
+                                       oldl = l;
+                               }
+                               success = 1;
+                               goodthresh = thresh;
+                       } 
+                       t2_destroy(t2);
+               } else {
+                       success = 1;
+                       goodthresh = min;
+               }
+               if (!success) {
+                       return false;
+               }
+               
+               if(volume_info && volume_info->index_on) {      /* Threshold for Marcela Index */
+                       volume_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
+               }
+               tcd_makelayer(tcd, layno, goodthresh, 1);
+               
+               /* fixed_quality */
+               cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];   
+       }
+
+       return true;
+}
+
+/* ----------------------------------------------------------------------- */
+int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_volume_info_t * volume_info) {
+       int compno;
+       int l, i, npck = 0;
+       double encoding_time;
+       
+       opj_tcd_tile_t  *tile = NULL;
+       opj_tcp_t               *tcd_tcp = NULL;
+       opj_cp_t                *cp = NULL;
+
+       opj_tcp_t               *tcp = &tcd->cp->tcps[0];
+       opj_tccp_t              *tccp = &tcp->tccps[0];
+       opj_volume_t    *volume = tcd->volume;
+       opj_t2_t                *t2 = NULL;             /* T2 component */
+
+       tcd->tcd_tileno = tileno;                       /* current encoded/decoded tile */
+       
+       tcd->tcd_tile = tcd->tcd_volume->tiles; /* tile information */
+       tile = tcd->tcd_tile;
+       
+       tcd->tcp = &tcd->cp->tcps[tileno];      /* coding/decoding params of tileno */  
+       tcd_tcp = tcd->tcp;
+       
+       cp = tcd->cp;           /* coding parameters */
+
+       /* INDEX >> */
+       if(volume_info && volume_info->index_on) {
+               opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0];        /* based on component 0 */
+               for (i = 0; i < tilec_idx->numresolution[0]; i++) {
+                       opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
+
+                       volume_info->tile[tileno].prctno[0][i] = res_idx->prctno[0];
+                       volume_info->tile[tileno].prctno[1][i] = res_idx->prctno[1];
+                       volume_info->tile[tileno].prctno[2][i] = res_idx->prctno[2];
+
+                       npck += res_idx->prctno[0] * res_idx->prctno[1] * res_idx->prctno[2];
+
+                       volume_info->tile[tileno].prctsiz[0][i] = tccp->prctsiz[0][i];
+                       volume_info->tile[tileno].prctsiz[1][i] = tccp->prctsiz[1][i];
+                       volume_info->tile[tileno].prctsiz[2][i] = tccp->prctsiz[2][i];
+               }
+               volume_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(volume_info->comp * volume_info->layer * npck * sizeof(opj_packet_info_t));
+       }
+       /* << INDEX */
+       
+       /*---------------TILE-------------------*/
+       encoding_time = opj_clock();    /* time needed to encode a tile */
+       
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               int x, y, z;
+               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+               
+               int adjust;
+               int offset_x = int_ceildiv(volume->x0, volume->comps[compno].dx); /*ceil(x0 / subsampling_dx)*/
+               int offset_y = int_ceildiv(volume->y0, volume->comps[compno].dy);
+               int offset_z = int_ceildiv(volume->z0, volume->comps[compno].dz);
+               
+               int tw = tilec->x1 - tilec->x0;
+               int w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);
+               int th = tilec->y1 - tilec->y0;
+               int h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);
+               int tl = tilec->z1 - tilec->z0;
+               int l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);
+
+               
+               
+               /* extract tile data from volume.comps[0].data to tile.comps[0].data */
+               /*fprintf(stdout,"[INFO] Extract tile data\n");*/
+               if (tcd->cp->transform_format == TRF_3D_RLS || tcd->cp->transform_format == TRF_3D_LSE) {
+                       adjust = 0;
+               } else {
+            adjust = volume->comps[compno].sgnd ? 0 : 1 << (volume->comps[compno].prec - 1); /*sign=='+' --> 2^(prec-1)*/
+                       if (volume->comps[compno].dcoffset != 0){
+                               adjust += volume->comps[compno].dcoffset;
+                               fprintf(stdout,"[INFO] DC Offset applied: DCO = %d -> adjust = %d\n",volume->comps[compno].dcoffset,adjust);
+                       }
+               }               
+
+               if (tcd_tcp->tccps[compno].reversible == 1) { /*IF perfect reconstruction (DWT.5-3)*/
+                       for (z = tilec->z0; z < tilec->z1; z++) {
+                               for (y = tilec->y0; y < tilec->y1; y++) {
+                                       /* start of the src tile scanline */
+                                       int *data = &volume->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w + (z - offset_z) * w * h];
+                                       /* start of the dst tile scanline */
+                                       int *tile_data = &tilec->data[(y - tilec->y0) * tw + (z - tilec->z0) * tw * th];
+                                       for (x = tilec->x0; x < tilec->x1; x++) {
+                                               *tile_data++ = *data++ - adjust;
+                                       }
+                               }
+                       }
+               } else if (tcd_tcp->tccps[compno].reversible == 0) { /*IF not (DWT.9-7)*/
+                       for (z = tilec->z0; z < tilec->z1; z++) {
+                               for (y = tilec->y0; y < tilec->y1; y++) {
+                                       /* start of the src tile scanline */
+                                       int *data = &volume->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w + (z - offset_z) * w * h];
+                                       /* start of the dst tile scanline */
+                                       int *tile_data = &tilec->data[(y - tilec->y0) * tw + (z - tilec->z0) * tw * th];
+                                       for (x = tilec->x0; x < tilec->x1; x++) {
+                                               *tile_data++ = (*data++ - adjust) << 13;
+                                       }
+                               }
+                       }
+               }
+       
+       }
+
+       /*----------------MCT-------------------*/
+       if (tcd_tcp->mct) {
+               int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) * (tile->comps[0].z1 - tile->comps[0].z0);
+               fprintf(stdout,"[INFO] Tcd_encode_tile: mct\n");
+               if (tcd_tcp->tccps[0].reversible == 0) {
+                       mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
+               } else {
+                       mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
+               }
+       }
+       /*----------------TRANSFORM---------------------------------*/
+       fprintf(stdout,"[INFO] Tcd_encode_tile: Transform\n");
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+               dwt_encode(tilec, tcd_tcp->tccps[compno].dwtid);
+       } 
+
+       /*-------------------ENTROPY CODING-----------------------------*/
+       fprintf(stdout,"[INFO] Tcd_encode_tile: Entropy coding\n");
+       if ((cp->encoding_format == ENCOD_2EB)||(cp->encoding_format == ENCOD_3EB))
+       {
+               if (cp->encoding_format == ENCOD_2EB) {
+                       opj_t1_t *t1 = NULL;
+                       t1 = t1_create(tcd->cinfo);
+                       t1_encode_cblks(t1, tile, tcd_tcp);
+                       t1_destroy(t1); 
+               } else if (cp->encoding_format == ENCOD_3EB) {
+                       opj_t1_3d_t *t1 = NULL;         
+                       t1 = t1_3d_create(tcd->cinfo);
+                       t1_3d_encode_cblks(t1, tile, tcd_tcp);
+                       t1_3d_destroy(t1);      
+               }
+               /*-----------RATE-ALLOCATE------------------*/
+               /* INDEX */
+               if(volume_info) {
+                       volume_info->index_write = 0;
+               }
+               if (cp->disto_alloc || cp->fixed_quality) {     
+                       fprintf(stdout,"[INFO] Tcd_encode_tile: Rate-allocate\n");
+                       tcd_rateallocate(tcd, dest, len, volume_info);                  /* Normal Rate/distortion allocation */
+               } else {/* fixed_alloc */
+           fprintf(stdout,"[INFO] Tcd_encode_tile: Rate-allocate fixed\n");
+            tcd_rateallocate_fixed(tcd);                                                       /* Fixed layer allocation */
+               }
+
+               /*--------------TIER2------------------*/
+               /* INDEX */
+               if(volume_info) {
+                       volume_info->index_write = 1;
+               }
+               fprintf(stdout,"[INFO] Tcd_encode_tile: Tier - 2\n");
+        t2 = t2_create(tcd->cinfo, volume, cp);
+               l = t2_encode_packets(t2, tileno, tile, tcd_tcp->numlayers, dest, len, volume_info);
+        t2_destroy(t2);
+       } else if ((cp->encoding_format == ENCOD_2GR)||(cp->encoding_format == ENCOD_3GR)) {
+               /*if(volume_info) {
+                       volume_info->index_write = 1;
+               }
+               gr = golomb_create(tcd->cinfo, volume, cp);
+               l = golomb_encode(gr, tileno, tile, dest, len, volume_info);
+               golomb_destroy(gr);*/
+       }
+
+       
+       /*---------------CLEAN-------------------*/
+       fprintf(stdout,"[INFO] Tcd_encode_tile: %d bytes coded\n",l);
+       encoding_time = opj_clock() - encoding_time;
+       opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", encoding_time);
+       
+       /* cleaning memory */
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               tcd->tilec = &tile->comps[compno];
+               opj_free(tcd->tilec->data);
+       }
+       
+       if (l == -999){
+               fprintf(stdout,"[ERROR] Unable to perform T2 tier. Return -999.\n");
+               return 0;
+       }
+
+       return l;
+}
+
+
+bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) {
+       int l, i;
+       int compno, eof = 0;
+       double tile_time, t1_time, dwt_time;
+
+       opj_tcd_tile_t *tile = NULL;
+       opj_t2_t *t2 = NULL;            /* T2 component */
+       
+       tcd->tcd_tileno = tileno;
+       tcd->tcd_tile = &(tcd->tcd_volume->tiles[tileno]);
+       tcd->tcp = &(tcd->cp->tcps[tileno]);
+       tile = tcd->tcd_tile;
+       
+       tile_time = opj_clock();        /* time needed to decode a tile */
+       opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d / %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th * tcd->cp->tl);
+
+       if ((tcd->cp->encoding_format == ENCOD_2EB) || (tcd->cp->encoding_format == ENCOD_3EB)) {
+               /*--------------TIER2------------------*/
+               t2 = t2_create(tcd->cinfo, tcd->volume, tcd->cp);
+               l = t2_decode_packets(t2, src, len, tileno, tile);
+               t2_destroy(t2);
+               opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: %d bytes decoded\n",l);
+               
+               if (l == -999) {
+                       eof = 1;
+                       opj_event_msg(tcd->cinfo, EVT_ERROR, "Tcd_decode_tile: incomplete bistream\n");
+               }
+       
+               /*------------------TIER1-----------------*/
+               opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: Entropy decoding %d \n",tcd->cp->encoding_format);
+               t1_time = opj_clock();  /* time needed to decode a tile */
+               if (tcd->cp->encoding_format == ENCOD_2EB) {
+                       opj_t1_t *t1 = NULL;            /* T1 component */
+                       t1 = t1_create(tcd->cinfo);
+                       t1_decode_cblks(t1, tile, tcd->tcp);
+                       t1_destroy(t1);
+               }else if (tcd->cp->encoding_format == ENCOD_3EB) {
+                       opj_t1_3d_t *t1 = NULL;         /* T1 component */
+                       t1 = t1_3d_create(tcd->cinfo);
+                       t1_3d_decode_cblks(t1, tile, tcd->tcp);
+                       t1_3d_destroy(t1);
+               }
+
+               t1_time = opj_clock() - t1_time;
+               #ifdef VERBOSE
+                               opj_event_msg(tcd->cinfo, EVT_INFO, "- tier-1 took %f s\n", t1_time);
+               #endif
+       } else if ((tcd->cp->encoding_format == ENCOD_2GR)||(tcd->cp->encoding_format == ENCOD_3GR)) {
+               opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: Entropy decoding -- Does nothing :-D\n");
+               /*
+               gr = golomb_create(tcd->cinfo, tcd->volume, tcd->cp);
+               l = golomb_decode(gr, tileno, tile, src, len);
+               golomb_destroy(gr);
+               if (l == -999) {
+                       eof = 1;
+                       opj_event_msg(tcd->cinfo, EVT_ERROR, "Tcd_decode_tile: incomplete bistream\n");
+               }
+               */
+       } 
+
+       /*----------------DWT---------------------*/
+       fprintf(stdout,"[INFO] Tcd_decode_tile: Inverse DWT\n");
+       dwt_time = opj_clock(); /* time needed to decode a tile */
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+               int stops[3], dwtid[3];
+       
+               for (i = 0; i < 3; i++) {
+                       if (tcd->cp->reduce[i] != 0) 
+                               tcd->volume->comps[compno].resno_decoded[i] = tile->comps[compno].numresolution[i] - tcd->cp->reduce[i] - 1;
+                       stops[i] = tilec->numresolution[i] - 1 - tcd->volume->comps[compno].resno_decoded[i];
+                       if (stops[i] < 0) stops[i]=0;
+                       dwtid[i] = tcd->cp->tcps->tccps[compno].dwtid[i];
+               }
+               
+               dwt_decode(tilec, stops, dwtid);
+
+               for (i = 0; i < 3; i++) {
+                       if (tile->comps[compno].numresolution[i] > 0) {
+                               tcd->volume->comps[compno].factor[i] = tile->comps[compno].numresolution[i] - (tcd->volume->comps[compno].resno_decoded[i] + 1);
+                               if ( (tcd->volume->comps[compno].factor[i]) < 0 )
+                                       tcd->volume->comps[compno].factor[i] = 0;
+                       }
+               }
+       }
+       dwt_time = opj_clock() - dwt_time;
+       #ifdef VERBOSE
+                       opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
+       #endif
+
+       /*----------------MCT-------------------*/
+       
+       if (tcd->tcp->mct) {
+               if (tcd->tcp->tccps[0].reversible == 1) {
+                       mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, 
+                               (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) * (tile->comps[0].z1 - tile->comps[0].z0));
+               } else {
+                       mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, 
+                               (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0)* (tile->comps[0].z1 - tile->comps[0].z0));
+               }
+       }
+       
+       /*---------------TILE-------------------*/
+       
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+               opj_tcd_resolution_t *res =     &tilec->resolutions[tcd->volume->comps[compno].resno_decoded[0]];
+               int adjust;
+               int minval = tcd->volume->comps[compno].sgnd ? -(1 << (tcd->volume->comps[compno].prec - 1)) : 0;
+               int maxval = tcd->volume->comps[compno].sgnd ? (1 << (tcd->volume->comps[compno].prec - 1)) - 1 : (1 << tcd->volume->comps[compno].prec) - 1;
+               
+               int tw = tilec->x1 - tilec->x0;
+               int w = tcd->volume->comps[compno].w;
+               int th = tilec->y1 - tilec->y0;
+               int h = tcd->volume->comps[compno].h;
+
+               int i, j, k;
+               int offset_x = int_ceildivpow2(tcd->volume->comps[compno].x0, tcd->volume->comps[compno].factor[0]);
+               int offset_y = int_ceildivpow2(tcd->volume->comps[compno].y0, tcd->volume->comps[compno].factor[1]);
+               int offset_z = int_ceildivpow2(tcd->volume->comps[compno].z0, tcd->volume->comps[compno].factor[2]);
+               
+               if (tcd->cp->transform_format == TRF_3D_RLS || tcd->cp->transform_format == TRF_3D_LSE) {
+                       adjust = 0;
+               } else {
+            adjust = tcd->volume->comps[compno].sgnd ? 0 : 1 << (tcd->volume->comps[compno].prec - 1); /*sign=='+' --> 2^(prec-1)*/
+                       if (tcd->volume->comps[compno].dcoffset != 0){
+                               adjust += tcd->volume->comps[compno].dcoffset;
+                               fprintf(stdout,"[INFO] DC Offset applied: DCO = %d -> adjust = %d\n",tcd->volume->comps[compno].dcoffset,adjust);
+                       }
+               }
+
+               for (k = res->z0; k < res->z1; k++) {
+                       for (j = res->y0; j < res->y1; j++) {
+                               for (i = res->x0; i < res->x1; i++) {
+                                       int v;
+                                       float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw + (k - res->z0) * tw * th]) / 8192.0);
+
+                                       if (tcd->tcp->tccps[compno].reversible == 1) {
+                                               v = tilec->data[i - res->x0 + (j - res->y0) * tw + (k - res->z0) * tw * th];
+                                       } else {
+                                               int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
+                                               v = ((tmp < 0) ? -tmp2:tmp2);
+                                       }
+                                       v += adjust;
+                                       
+                                       tcd->volume->comps[compno].data[(i - offset_x) + (j - offset_y) * w + (k - offset_z) * w * h] = int_clamp(v, minval, maxval);
+                               }
+                       }
+               }
+       }
+       
+       tile_time = opj_clock() - tile_time;    /* time needed to decode a tile */
+       opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
+               
+       for (compno = 0; compno < tile->numcomps; compno++) {
+               opj_free(tcd->tcd_volume->tiles[tileno].comps[compno].data);
+               tcd->tcd_volume->tiles[tileno].comps[compno].data = NULL;
+       }
+       
+       if (eof) {
+               return false;
+       }
+       
+       return true;
+}
+
index c86cfe6dc5f21ca73934506baa055fa33a380561..8792995e4e85081a6a812bdb58d351effe4ae164 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/* \r
-==========================================================\r
-   Tag-tree coder interface\r
-==========================================================\r
-*/\r
-void tgt_tree_dump (FILE *fd, opj_tgt_tree_t * tree){\r
-       int nodesno;\r
-\r
-       fprintf(fd, "TGT_TREE {\n");\r
-       fprintf(fd, "  numnodes: %d \n", tree->numnodes);       \r
-       fprintf(fd, "  numleafsh: %d, numleafsv: %d, numleafsz: %d,\n", tree->numleafsh, tree->numleafsv, tree->numleafsz);\r
-\r
-       for (nodesno = 0; nodesno < tree->numnodes; nodesno++) {\r
-               fprintf(fd, "tgt_node %d {\n", nodesno);\r
-               fprintf(fd, "  value: %d \n", tree->nodes[nodesno].value);\r
-               fprintf(fd, "  low: %d \n", tree->nodes[nodesno].low);\r
-               fprintf(fd, "  known: %d \n", tree->nodes[nodesno].known);\r
-               if (tree->nodes[nodesno].parent) {\r
-                       fprintf(fd, "  parent.value: %d \n", tree->nodes[nodesno].parent->value);\r
-                       fprintf(fd, "  parent.low: %d \n", tree->nodes[nodesno].parent->low);\r
-                       fprintf(fd, "  parent.known: %d \n", tree->nodes[nodesno].parent->known);\r
-               }\r
-               fprintf(fd, "}\n");\r
-\r
-       }\r
-       fprintf(fd, "}\n");\r
-\r
-}\r
-\r
-\r
-opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv, int numleafsz) {\r
-       \r
-       int nplh[32];\r
-       int nplv[32];\r
-       int nplz[32];\r
-       opj_tgt_node_t *node = NULL;\r
-       opj_tgt_node_t *parentnode = NULL;\r
-       opj_tgt_node_t *parentnode0 = NULL;\r
-       opj_tgt_node_t *parentnode1 = NULL;\r
-       opj_tgt_tree_t *tree = NULL;\r
-       int i, j, k, p, p0;\r
-       int numlvls;\r
-       int n, z = 0;\r
-\r
-       tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t));\r
-       if(!tree) \r
-               return NULL;\r
-       tree->numleafsh = numleafsh;\r
-       tree->numleafsv = numleafsv;\r
-       tree->numleafsz = numleafsz;\r
-\r
-       numlvls = 0;\r
-       nplh[0] = numleafsh;\r
-       nplv[0] = numleafsv;\r
-       nplz[0] = numleafsz;\r
-       tree->numnodes = 0;\r
-       do {\r
-               n = nplh[numlvls] * nplv[numlvls] * nplz[numlvls]; \r
-               nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;\r
-               nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;\r
-               nplz[numlvls + 1] = (nplz[numlvls] + 1) / 2;\r
-               tree->numnodes += n;\r
-               ++numlvls;\r
-       } while (n > 1);\r
-\r
-       if (tree->numnodes == 0) {\r
-               opj_free(tree);\r
-               return NULL;\r
-       }\r
-\r
-       tree->nodes = (opj_tgt_node_t *) opj_malloc(tree->numnodes * sizeof(opj_tgt_node_t));\r
-       if(!tree->nodes) {\r
-               opj_free(tree);\r
-               return NULL;\r
-       }\r
-\r
-       node = tree->nodes;\r
-       parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv * tree->numleafsz];\r
-       parentnode0 = parentnode;\r
-       parentnode1 = parentnode;\r
-       /*fprintf(stdout,"\nH %d V %d Z %d numlvls %d nodes %d\n",tree->numleafsh,tree->numleafsv,tree->numleafsz,numlvls,tree->numnodes);*/\r
-       for (i = 0; i < numlvls - 1; ++i) {\r
-               for (z = 0; z < nplz[i]; ++z) {\r
-                       for (j = 0; j < nplv[i]; ++j) {\r
-                               k = nplh[i];\r
-                               while(--k >= 0) {\r
-                                       node->parent = parentnode;              /*fprintf(stdout,"node[%d].parent = node[%d]\n",n,p);*/\r
-                                       ++node;\r
-                                       if(--k >= 0) {\r
-                                               node->parent = parentnode;      /*fprintf(stdout,"node[%d].parent = node[%d]\n",n,p);*/\r
-                                               ++node;\r
-                                       }\r
-                                       ++parentnode;\r
-                               }\r
-                               if((j & 1) || j == nplv[i] - 1) {\r
-                                       parentnode0 = parentnode;\r
-                               } else {\r
-                                       parentnode = parentnode0;\r
-                               }\r
-                       }\r
-                       if ((z & 1) || z == nplz[i] - 1) {\r
-                               parentnode1 = parentnode;\r
-                       } else {\r
-                               parentnode0 = parentnode1;\r
-                               parentnode = parentnode1;\r
-                       }\r
-               }\r
-       }\r
-       node->parent = 0;\r
-\r
-       \r
-       tgt_reset(tree);\r
-\r
-       return tree;\r
-}\r
-\r
-void tgt_destroy(opj_tgt_tree_t *tree) {\r
-       opj_free(tree->nodes);\r
-       opj_free(tree);\r
-}\r
-\r
-void tgt_reset(opj_tgt_tree_t *tree) {\r
-       int i;\r
-\r
-       if (NULL == tree)\r
-               return;\r
-       \r
-       for (i = 0; i < tree->numnodes; i++) {\r
-               tree->nodes[i].value = 999;\r
-               tree->nodes[i].low = 0;\r
-               tree->nodes[i].known = 0;\r
-       }\r
-}\r
-\r
-void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) {\r
-       opj_tgt_node_t *node;\r
-       node = &tree->nodes[leafno];\r
-       while (node && node->value > value) {\r
-               node->value = value;\r
-               node = node->parent;\r
-       }\r
-}\r
-\r
-void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {\r
-       opj_tgt_node_t *stk[31];\r
-       opj_tgt_node_t **stkptr;\r
-       opj_tgt_node_t *node;\r
-       int low;\r
-\r
-       stkptr = stk;\r
-       node = &tree->nodes[leafno];\r
-       while (node->parent) {\r
-               *stkptr++ = node;\r
-               node = node->parent;\r
-       }\r
-       \r
-       low = 0;\r
-       for (;;) {\r
-               if (low > node->low) {\r
-                       node->low = low;\r
-               } else {\r
-                       low = node->low;\r
-               }\r
-               \r
-               while (low < threshold) {\r
-                       if (low >= node->value) {\r
-                               if (!node->known) {\r
-                                       bio_write(bio, 1, 1);\r
-                                       node->known = 1;\r
-                               }\r
-                               break;\r
-                       }\r
-                       bio_write(bio, 0, 1);\r
-                       ++low;\r
-               }\r
-               \r
-               node->low = low;\r
-               if (stkptr == stk)\r
-                       break;\r
-               node = *--stkptr;\r
-       }\r
-}\r
-\r
-int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {\r
-       opj_tgt_node_t *stk[31];\r
-       opj_tgt_node_t **stkptr;\r
-       opj_tgt_node_t *node;\r
-       int low;\r
-\r
-       stkptr = stk;\r
-       node = &tree->nodes[leafno];\r
-       while (node->parent) {\r
-               *stkptr++ = node;\r
-               node = node->parent;\r
-       }\r
-       \r
-       low = 0;\r
-       for (;;) {\r
-               if (low > node->low) {\r
-                       node->low = low;\r
-               } else {\r
-                       low = node->low;\r
-               }\r
-               while (low < threshold && low < node->value) {\r
-                       if (bio_read(bio, 1)) {\r
-                               node->value = low;\r
-                       } else {\r
-                               ++low;\r
-                       }\r
-               }\r
-               node->low = low;\r
-               if (stkptr == stk) {\r
-                       break;\r
-               }\r
-               node = *--stkptr;\r
-       }\r
-       \r
-       return (node->value < threshold) ? 1 : 0;\r
-}\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/* 
+==========================================================
+   Tag-tree coder interface
+==========================================================
+*/
+void tgt_tree_dump (FILE *fd, opj_tgt_tree_t * tree){
+       int nodesno;
+
+       fprintf(fd, "TGT_TREE {\n");
+       fprintf(fd, "  numnodes: %d \n", tree->numnodes);       
+       fprintf(fd, "  numleafsh: %d, numleafsv: %d, numleafsz: %d,\n", tree->numleafsh, tree->numleafsv, tree->numleafsz);
+
+       for (nodesno = 0; nodesno < tree->numnodes; nodesno++) {
+               fprintf(fd, "tgt_node %d {\n", nodesno);
+               fprintf(fd, "  value: %d \n", tree->nodes[nodesno].value);
+               fprintf(fd, "  low: %d \n", tree->nodes[nodesno].low);
+               fprintf(fd, "  known: %d \n", tree->nodes[nodesno].known);
+               if (tree->nodes[nodesno].parent) {
+                       fprintf(fd, "  parent.value: %d \n", tree->nodes[nodesno].parent->value);
+                       fprintf(fd, "  parent.low: %d \n", tree->nodes[nodesno].parent->low);
+                       fprintf(fd, "  parent.known: %d \n", tree->nodes[nodesno].parent->known);
+               }
+               fprintf(fd, "}\n");
+
+       }
+       fprintf(fd, "}\n");
+
+}
+
+
+opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv, int numleafsz) {
+       
+       int nplh[32];
+       int nplv[32];
+       int nplz[32];
+       opj_tgt_node_t *node = NULL;
+       opj_tgt_node_t *parentnode = NULL;
+       opj_tgt_node_t *parentnode0 = NULL;
+       opj_tgt_node_t *parentnode1 = NULL;
+       opj_tgt_tree_t *tree = NULL;
+       int i, j, k, p, p0;
+       int numlvls;
+       int n, z = 0;
+
+       tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t));
+       if(!tree) 
+               return NULL;
+       tree->numleafsh = numleafsh;
+       tree->numleafsv = numleafsv;
+       tree->numleafsz = numleafsz;
+
+       numlvls = 0;
+       nplh[0] = numleafsh;
+       nplv[0] = numleafsv;
+       nplz[0] = numleafsz;
+       tree->numnodes = 0;
+       do {
+               n = nplh[numlvls] * nplv[numlvls] * nplz[numlvls]; 
+               nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
+               nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
+               nplz[numlvls + 1] = (nplz[numlvls] + 1) / 2;
+               tree->numnodes += n;
+               ++numlvls;
+       } while (n > 1);
+
+       if (tree->numnodes == 0) {
+               opj_free(tree);
+               return NULL;
+       }
+
+       tree->nodes = (opj_tgt_node_t *) opj_malloc(tree->numnodes * sizeof(opj_tgt_node_t));
+       if(!tree->nodes) {
+               opj_free(tree);
+               return NULL;
+       }
+
+       node = tree->nodes;
+       parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv * tree->numleafsz];
+       parentnode0 = parentnode;
+       parentnode1 = parentnode;
+       /*fprintf(stdout,"\nH %d V %d Z %d numlvls %d nodes %d\n",tree->numleafsh,tree->numleafsv,tree->numleafsz,numlvls,tree->numnodes);*/
+       for (i = 0; i < numlvls - 1; ++i) {
+               for (z = 0; z < nplz[i]; ++z) {
+                       for (j = 0; j < nplv[i]; ++j) {
+                               k = nplh[i];
+                               while(--k >= 0) {
+                                       node->parent = parentnode;              /*fprintf(stdout,"node[%d].parent = node[%d]\n",n,p);*/
+                                       ++node;
+                                       if(--k >= 0) {
+                                               node->parent = parentnode;      /*fprintf(stdout,"node[%d].parent = node[%d]\n",n,p);*/
+                                               ++node;
+                                       }
+                                       ++parentnode;
+                               }
+                               if((j & 1) || j == nplv[i] - 1) {
+                                       parentnode0 = parentnode;
+                               } else {
+                                       parentnode = parentnode0;
+                               }
+                       }
+                       if ((z & 1) || z == nplz[i] - 1) {
+                               parentnode1 = parentnode;
+                       } else {
+                               parentnode0 = parentnode1;
+                               parentnode = parentnode1;
+                       }
+               }
+       }
+       node->parent = 0;
+
+       
+       tgt_reset(tree);
+
+       return tree;
+}
+
+void tgt_destroy(opj_tgt_tree_t *tree) {
+       opj_free(tree->nodes);
+       opj_free(tree);
+}
+
+void tgt_reset(opj_tgt_tree_t *tree) {
+       int i;
+
+       if (NULL == tree)
+               return;
+       
+       for (i = 0; i < tree->numnodes; i++) {
+               tree->nodes[i].value = 999;
+               tree->nodes[i].low = 0;
+               tree->nodes[i].known = 0;
+       }
+}
+
+void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) {
+       opj_tgt_node_t *node;
+       node = &tree->nodes[leafno];
+       while (node && node->value > value) {
+               node->value = value;
+               node = node->parent;
+       }
+}
+
+void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
+       opj_tgt_node_t *stk[31];
+       opj_tgt_node_t **stkptr;
+       opj_tgt_node_t *node;
+       int low;
+
+       stkptr = stk;
+       node = &tree->nodes[leafno];
+       while (node->parent) {
+               *stkptr++ = node;
+               node = node->parent;
+       }
+       
+       low = 0;
+       for (;;) {
+               if (low > node->low) {
+                       node->low = low;
+               } else {
+                       low = node->low;
+               }
+               
+               while (low < threshold) {
+                       if (low >= node->value) {
+                               if (!node->known) {
+                                       bio_write(bio, 1, 1);
+                                       node->known = 1;
+                               }
+                               break;
+                       }
+                       bio_write(bio, 0, 1);
+                       ++low;
+               }
+               
+               node->low = low;
+               if (stkptr == stk)
+                       break;
+               node = *--stkptr;
+       }
+}
+
+int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
+       opj_tgt_node_t *stk[31];
+       opj_tgt_node_t **stkptr;
+       opj_tgt_node_t *node;
+       int low;
+
+       stkptr = stk;
+       node = &tree->nodes[leafno];
+       while (node->parent) {
+               *stkptr++ = node;
+               node = node->parent;
+       }
+       
+       low = 0;
+       for (;;) {
+               if (low > node->low) {
+                       node->low = low;
+               } else {
+                       low = node->low;
+               }
+               while (low < threshold && low < node->value) {
+                       if (bio_read(bio, 1)) {
+                               node->value = low;
+                       } else {
+                               ++low;
+                       }
+               }
+               node->low = low;
+               if (stkptr == stk) {
+                       break;
+               }
+               node = *--stkptr;
+       }
+       
+       return (node->value < threshold) ? 1 : 0;
+}
index 2b54baf959ee9ba12eed502c1d07d55a50f055a0..4565ad221fbd62ed0eccd62f4007630c605f7be3 100644 (file)
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#ifndef __TGT_H\r
-#define __TGT_H\r
-/**\r
-@file tgt.h\r
-@brief Implementation of a tag-tree coder (TGT)\r
-\r
-The functions in TGT.C have for goal to realize a tag-tree coder. The functions in TGT.C\r
-are used by some function in T2.C.\r
-*/\r
-\r
-/** @defgroup TGT TGT - Implementation of a tag-tree coder */\r
-/*@{*/\r
-\r
-/**\r
-Tag node\r
-*/\r
-typedef struct opj_tgt_node {\r
-/** Node parent reference */\r
-  struct opj_tgt_node *parent;\r
-/**  */\r
-  int value;\r
-/**  */\r
-  int low;\r
-/**  */\r
-  int known;\r
-} opj_tgt_node_t;\r
-\r
-/**\r
-Tag tree\r
-*/\r
-typedef struct opj_tgt_tree {\r
-/** Number of leaves from horizontal axis */\r
-  int numleafsh;\r
-/** Number of leaves from vertical axis */\r
-  int numleafsv;\r
-/** Number of leaves from axial axis */\r
-  int numleafsz;\r
-/** Number of nodes */\r
-  int numnodes;\r
-/** Reference to each node instance */\r
-  opj_tgt_node_t *nodes;\r
-} opj_tgt_tree_t;\r
-\r
-/** @name Funciones generales */\r
-/*@{*/\r
-/* ----------------------------------------------------------------------- */\r
-/**\r
-Create a tag-tree\r
-@param numleafsh Width of the array of leafs of the tree\r
-@param numleafsv Height of the array of leafs of the tree\r
-@param numleafsz Depth of the array of leafs of the tree\r
-@return Returns a new tag-tree if successful, returns NULL otherwise\r
-*/\r
-opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv, int numleafsz);\r
-/**\r
-Destroy a tag-tree, liberating memory\r
-@param tree Tag-tree to destroy\r
-*/\r
-void tgt_destroy(opj_tgt_tree_t *tree);\r
-/**\r
-Reset a tag-tree (set all leaves to 0)\r
-@param tree Tag-tree to reset\r
-*/\r
-void tgt_reset(opj_tgt_tree_t *tree);\r
-/**\r
-Set the value of a leaf of a tag-tree\r
-@param tree Tag-tree to modify\r
-@param leafno Number that identifies the leaf to modify\r
-@param value New value of the leaf\r
-*/\r
-void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value);\r
-/**\r
-Encode the value of a leaf of the tag-tree up to a given threshold\r
-@param bio Pointer to a BIO handle\r
-@param tree Tag-tree to modify\r
-@param leafno Number that identifies the leaf to encode\r
-@param threshold Threshold to use when encoding value of the leaf\r
-*/\r
-void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold);\r
-/**\r
-Decode the value of a leaf of the tag-tree up to a given threshold\r
-@param bio Pointer to a BIO handle\r
-@param tree Tag-tree to decode\r
-@param leafno Number that identifies the leaf to decode\r
-@param threshold Threshold to use when decoding value of the leaf\r
-@return Returns 1 if the node's value < threshold, returns 0 otherwise\r
-*/\r
-int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold);\r
-\r
-/*@}*/\r
-/* ----------------------------------------------------------------------- */\r
-void tgt_tree_dump (FILE *fd, opj_tgt_tree_t * tree);\r
-/*@}*/\r
-\r
-#endif /* __TGT_H */\r
+/*
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __TGT_H
+#define __TGT_H
+/**
+@file tgt.h
+@brief Implementation of a tag-tree coder (TGT)
+
+The functions in TGT.C have for goal to realize a tag-tree coder. The functions in TGT.C
+are used by some function in T2.C.
+*/
+
+/** @defgroup TGT TGT - Implementation of a tag-tree coder */
+/*@{*/
+
+/**
+Tag node
+*/
+typedef struct opj_tgt_node {
+/** Node parent reference */
+  struct opj_tgt_node *parent;
+/**  */
+  int value;
+/**  */
+  int low;
+/**  */
+  int known;
+} opj_tgt_node_t;
+
+/**
+Tag tree
+*/
+typedef struct opj_tgt_tree {
+/** Number of leaves from horizontal axis */
+  int numleafsh;
+/** Number of leaves from vertical axis */
+  int numleafsv;
+/** Number of leaves from axial axis */
+  int numleafsz;
+/** Number of nodes */
+  int numnodes;
+/** Reference to each node instance */
+  opj_tgt_node_t *nodes;
+} opj_tgt_tree_t;
+
+/** @name Funciones generales */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a tag-tree
+@param numleafsh Width of the array of leafs of the tree
+@param numleafsv Height of the array of leafs of the tree
+@param numleafsz Depth of the array of leafs of the tree
+@return Returns a new tag-tree if successful, returns NULL otherwise
+*/
+opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv, int numleafsz);
+/**
+Destroy a tag-tree, liberating memory
+@param tree Tag-tree to destroy
+*/
+void tgt_destroy(opj_tgt_tree_t *tree);
+/**
+Reset a tag-tree (set all leaves to 0)
+@param tree Tag-tree to reset
+*/
+void tgt_reset(opj_tgt_tree_t *tree);
+/**
+Set the value of a leaf of a tag-tree
+@param tree Tag-tree to modify
+@param leafno Number that identifies the leaf to modify
+@param value New value of the leaf
+*/
+void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value);
+/**
+Encode the value of a leaf of the tag-tree up to a given threshold
+@param bio Pointer to a BIO handle
+@param tree Tag-tree to modify
+@param leafno Number that identifies the leaf to encode
+@param threshold Threshold to use when encoding value of the leaf
+*/
+void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold);
+/**
+Decode the value of a leaf of the tag-tree up to a given threshold
+@param bio Pointer to a BIO handle
+@param tree Tag-tree to decode
+@param leafno Number that identifies the leaf to decode
+@param threshold Threshold to use when decoding value of the leaf
+@return Returns 1 if the node's value < threshold, returns 0 otherwise
+*/
+int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold);
+
+/*@}*/
+/* ----------------------------------------------------------------------- */
+void tgt_tree_dump (FILE *fd, opj_tgt_tree_t * tree);
+/*@}*/
+
+#endif /* __TGT_H */
index 065a7a342836a5963a121e5e5cb86e104af9425a..669b613b1ad6812f892410e1f73acc49421561fb 100644 (file)
@@ -1,91 +1,91 @@
-/*\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-#include "volume.h"\r
-#include "openjp3d.h"\r
-\r
-opj_volume_t* OPJ_CALLCONV opj_volume_create(int numcmpts, opj_volume_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {\r
-       int compno;\r
-       opj_volume_t *volume = NULL;\r
-\r
-       volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));\r
-       if(volume) {\r
-               volume->color_space = clrspc;\r
-               volume->numcomps = numcmpts;\r
-               /* allocate memory for the per-component information */\r
-               volume->comps = (opj_volume_comp_t*)opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t));\r
-               if(!volume->comps) {\r
-                       opj_volume_destroy(volume);\r
-                       return NULL;\r
-               }\r
-               /* create the individual volume components */\r
-               for(compno = 0; compno < numcmpts; compno++) {\r
-                       opj_volume_comp_t *comp = &volume->comps[compno];\r
-                       comp->dx = cmptparms[compno].dx;\r
-                       comp->dy = cmptparms[compno].dy;\r
-                       comp->dz = cmptparms[compno].dz;\r
-                       comp->w = cmptparms[compno].w;\r
-                       comp->h = cmptparms[compno].h;\r
-                       comp->l = cmptparms[compno].l;\r
-                       comp->x0 = cmptparms[compno].x0;\r
-                       comp->y0 = cmptparms[compno].y0;\r
-                       comp->z0 = cmptparms[compno].z0;\r
-                       comp->prec = cmptparms[compno].prec;\r
-                       comp->bpp = cmptparms[compno].bpp;\r
-                       comp->sgnd = cmptparms[compno].sgnd;\r
-                       comp->bigendian = cmptparms[compno].bigendian;\r
-                       comp->dcoffset = cmptparms[compno].dcoffset;\r
-                       comp->data = (int*)opj_malloc(comp->w * comp->h * comp->l * sizeof(int));\r
-                       if(!comp->data) {\r
-                               fprintf(stdout,"Unable to malloc comp->data (%d x %d x %d x bytes)",comp->w,comp->h,comp->l);\r
-                               opj_volume_destroy(volume);\r
-                               return NULL;\r
-                       }\r
-                       /*fprintf(stdout,"%d %d %d %d %d %d %d %d %d", comp->w,comp->h, comp->l, comp->dx, comp->dy, comp->dz, comp->prec, comp->bpp, comp->sgnd);*/\r
-               }\r
-       }\r
-\r
-       return volume;\r
-}\r
-\r
-void OPJ_CALLCONV opj_volume_destroy(opj_volume_t *volume) {\r
-       int i;\r
-       if(volume) {\r
-               if(volume->comps) {\r
-                       /* volume components */\r
-                       for(i = 0; i < volume->numcomps; i++) {\r
-                               opj_volume_comp_t *volume_comp = &volume->comps[i];\r
-                               if(volume_comp->data) {\r
-                                       opj_free(volume_comp->data);\r
-                               }\r
-                       }\r
-                       opj_free(volume->comps);\r
-               }\r
-               opj_free(volume);\r
-       }\r
-}\r
-\r
+/*
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+#include "volume.h"
+#include "openjp3d.h"
+
+opj_volume_t* OPJ_CALLCONV opj_volume_create(int numcmpts, opj_volume_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
+       int compno;
+       opj_volume_t *volume = NULL;
+
+       volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));
+       if(volume) {
+               volume->color_space = clrspc;
+               volume->numcomps = numcmpts;
+               /* allocate memory for the per-component information */
+               volume->comps = (opj_volume_comp_t*)opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t));
+               if(!volume->comps) {
+                       opj_volume_destroy(volume);
+                       return NULL;
+               }
+               /* create the individual volume components */
+               for(compno = 0; compno < numcmpts; compno++) {
+                       opj_volume_comp_t *comp = &volume->comps[compno];
+                       comp->dx = cmptparms[compno].dx;
+                       comp->dy = cmptparms[compno].dy;
+                       comp->dz = cmptparms[compno].dz;
+                       comp->w = cmptparms[compno].w;
+                       comp->h = cmptparms[compno].h;
+                       comp->l = cmptparms[compno].l;
+                       comp->x0 = cmptparms[compno].x0;
+                       comp->y0 = cmptparms[compno].y0;
+                       comp->z0 = cmptparms[compno].z0;
+                       comp->prec = cmptparms[compno].prec;
+                       comp->bpp = cmptparms[compno].bpp;
+                       comp->sgnd = cmptparms[compno].sgnd;
+                       comp->bigendian = cmptparms[compno].bigendian;
+                       comp->dcoffset = cmptparms[compno].dcoffset;
+                       comp->data = (int*)opj_malloc(comp->w * comp->h * comp->l * sizeof(int));
+                       if(!comp->data) {
+                               fprintf(stdout,"Unable to malloc comp->data (%d x %d x %d x bytes)",comp->w,comp->h,comp->l);
+                               opj_volume_destroy(volume);
+                               return NULL;
+                       }
+                       /*fprintf(stdout,"%d %d %d %d %d %d %d %d %d", comp->w,comp->h, comp->l, comp->dx, comp->dy, comp->dz, comp->prec, comp->bpp, comp->sgnd);*/
+               }
+       }
+
+       return volume;
+}
+
+void OPJ_CALLCONV opj_volume_destroy(opj_volume_t *volume) {
+       int i;
+       if(volume) {
+               if(volume->comps) {
+                       /* volume components */
+                       for(i = 0; i < volume->numcomps; i++) {
+                               opj_volume_comp_t *volume_comp = &volume->comps[i];
+                               if(volume_comp->data) {
+                                       opj_free(volume_comp->data);
+                               }
+                       }
+                       opj_free(volume->comps);
+               }
+               opj_free(volume);
+       }
+}
+
index 02f28d2a6c2ece79ba62e985c7b28def16212831..a0cad3709a40364b7ef358a143a173c5e594e31a 100644 (file)
@@ -1,43 +1,43 @@
-/*\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-#ifndef __VOLUME_H\r
-#define __VOLUME_H\r
-/**\r
-@file volume.h\r
-@brief Implementation of operations on volumes (VOLUME)\r
-\r
-The functions in VOLUME.C have for goal to realize operations on volumes.\r
-*/\r
-\r
-/** @defgroup VOLUME VOLUME - Implementation of operations on volumes */\r
-/*@{*/\r
-\r
-\r
-/*@}*/\r
-\r
-#endif /* __VOLUME_H */\r
-\r
+/*
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __VOLUME_H
+#define __VOLUME_H
+/**
+@file volume.h
+@brief Implementation of operations on volumes (VOLUME)
+
+The functions in VOLUME.C have for goal to realize operations on volumes.
+*/
+
+/** @defgroup VOLUME VOLUME - Implementation of operations on volumes */
+/*@{*/
+
+
+/*@}*/
+
+#endif /* __VOLUME_H */
+