Reactivate ctest
[openjpeg.git] / jpwl / jpwl.c
index f97331d4be7f7d62f37b66dbe66f28298fcda69f..07ca796adfd54a1103b1bb4afecdeec5bd7750d4 100644 (file)
 /** @name Local static variables */\r
 /*@{*/\r
 \r
-/** position of markers to insert */\r
-static jpwl_marker_t jwmarker[JPWL_MAX_NO_MARKERS]; \r
-/** number of prepared markers */\r
+/** number of JPWL prepared markers */\r
 static int jwmarker_num;\r
+/** properties of JPWL markers to insert */\r
+static jpwl_marker_t jwmarker[JPWL_MAX_NO_MARKERS]; \r
 \r
 /*@}*/\r
 \r
@@ -88,25 +88,97 @@ their relevant wishlist position
 int jpwl_markcomp(const void *arg1, const void *arg2);\r
 \r
 /** write an EPB MS to a buffer\r
+@param j2k J2K compressor handle\r
 @param epbmark pointer to the EPB MS\r
 @param buf pointer to the memory buffer\r
 */\r
-void jpwl_epb_write(jpwl_epb_ms_t *epbmark, unsigned char *buf);\r
+void jpwl_epb_write(opj_j2k_t *j2k, jpwl_epb_ms_t *epbmark, unsigned char *buf);\r
 \r
 /** write an EPC MS to a buffer\r
+@param j2k J2K compressor handle\r
 @param epcmark pointer to the EPC MS\r
 @param buf pointer to the memory buffer\r
 */\r
-void jpwl_epc_write(jpwl_epc_ms_t *epcmark, unsigned char *buf);\r
+void jpwl_epc_write(opj_j2k_t *j2k, jpwl_epc_ms_t *epcmark, unsigned char *buf);\r
 \r
 /** write an ESD MS to a buffer\r
+@param j2k J2K compressor handle\r
 @param esdmark pointer to the ESD MS\r
 @param buf pointer to the memory buffer\r
 */\r
-void jpwl_esd_write(jpwl_esd_ms_t *esdmark, unsigned char *buf);\r
+void jpwl_esd_write(opj_j2k_t *j2k, jpwl_esd_ms_t *esdmark, unsigned char *buf);\r
 \r
 /*-----------------------------------------------------------------*/\r
 \r
+void jpwl_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {\r
+\r
+       int mm;\r
+\r
+       /* let's reset some settings */\r
+\r
+       /* clear the existing markers */\r
+       for (mm = 0; mm < jwmarker_num; mm++) {\r
+\r
+               switch (jwmarker[mm].id) {\r
+\r
+               case J2K_MS_EPB:\r
+                       opj_free(jwmarker[mm].m.epbmark);\r
+                       break;\r
+\r
+               case J2K_MS_EPC:\r
+                       opj_free(jwmarker[mm].m.epcmark);\r
+                       break;\r
+\r
+               case J2K_MS_ESD:\r
+                       opj_free(jwmarker[mm].m.esdmark);\r
+                       break;\r
+\r
+               case J2K_MS_RED:\r
+                       opj_free(jwmarker[mm].m.redmark);\r
+                       break;\r
+\r
+               default:\r
+                       break;\r
+               }\r
+       }\r
+\r
+       /* clear the marker structure array */\r
+       memset(jwmarker, 0, sizeof(jpwl_marker_t) * JPWL_MAX_NO_MARKERS);\r
+\r
+       /* no more markers in the list */\r
+       jwmarker_num = 0;\r
+\r
+       /* let's begin creating a marker list, according to user wishes */\r
+       jpwl_prepare_marks(j2k, cio, image);\r
+\r
+       /* now we dump the JPWL markers on the codestream */\r
+       jpwl_dump_marks(j2k, cio, image);\r
+\r
+       /* do not know exactly what is this for,\r
+       but it gets called during index creation */\r
+       j2k->pos_correction = 0;\r
+\r
+}\r
+\r
+void j2k_add_marker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {\r
+\r
+       if (!cstr_info)\r
+               return;\r
+\r
+       /* expand the list? */\r
+       if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {\r
+               cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);\r
+               cstr_info->marker = opj_realloc(cstr_info->marker, cstr_info->maxmarknum);\r
+       }\r
+\r
+       /* add the marker */\r
+       cstr_info->marker[cstr_info->marknum].type = type;\r
+       cstr_info->marker[cstr_info->marknum].pos = pos;\r
+       cstr_info->marker[cstr_info->marknum].len = len;\r
+       cstr_info->marknum++;\r
+\r
+}\r
+\r
 void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {\r
 \r
        unsigned short int socsiz_len = 0;\r
@@ -119,7 +191,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
        jpwl_epc_ms_t *epc_mark;\r
        jpwl_esd_ms_t *esd_mark;\r
 \r
-       /* find SOC + SIZ length */\r
+       /* find (SOC + SIZ) length */\r
        /* I assume SIZ is always the first marker after SOC */\r
        cio_seek(cio, soc_pos + 4);\r
        socsiz_len = (unsigned short int) cio_read(cio, 2) + 4; /* add the 2 marks length itself */\r
@@ -130,18 +202,18 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
         EPC MS for Main Header: if we are here it's required\r
        */\r
        /* create the EPC */\r
-       if (epc_mark = jpwl_epc_create(\r
+       if ((epc_mark = jpwl_epc_create(\r
                        j2k,\r
                        j2k->cp->esd_on, /* is ESD present? */\r
                        j2k->cp->red_on, /* is RED present? */\r
                        j2k->cp->epb_on, /* is EPB present? */\r
                        false /* are informative techniques present? */\r
-               )) {\r
+               ))) {\r
 \r
                /* Add this marker to the 'insertanda' list */\r
                if (epc_mark) {\r
                        jwmarker[jwmarker_num].id = J2K_MS_EPC; /* its type */\r
-                       jwmarker[jwmarker_num].epcmark = epc_mark; /* the EPC */\r
+                       jwmarker[jwmarker_num].m.epcmark = epc_mark; /* the EPC */\r
                        jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* after SIZ */\r
                        jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.1; /* not so first */\r
                        jwmarker[jwmarker_num].len = epc_mark->Lepc; /* its length */\r
@@ -171,7 +243,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
        if (j2k->cp->esd_on && (j2k->cp->sens_MH >= 0)) {\r
 \r
                /* Create the ESD */\r
-               if (esd_mark = jpwl_esd_create(\r
+               if ((esd_mark = jpwl_esd_create(\r
                        j2k, /* this encoder handle */\r
                        -1, /* we are averaging over all components */\r
                        (unsigned char) j2k->cp->sens_range, /* range method */\r
@@ -181,12 +253,12 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        -1, /* this ESD is in main header */\r
                        0 /*j2k->cstr_info->num*/, /* number of packets in codestream */\r
                        NULL /*sensval*/ /* pointer to sensitivity data of packets */\r
-                       )) {\r
+                       ))) {\r
                        \r
                        /* Add this marker to the 'insertanda' list */\r
                        if (jwmarker_num < JPWL_MAX_NO_MARKERS) {\r
                                jwmarker[jwmarker_num].id = J2K_MS_ESD; /* its type */\r
-                               jwmarker[jwmarker_num].esdmark = esd_mark; /* the EPB */\r
+                               jwmarker[jwmarker_num].m.esdmark = esd_mark; /* the EPB */\r
                                jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* we choose to place it after SIZ */\r
                                jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.2; /* not first at all! */\r
                                jwmarker[jwmarker_num].len = esd_mark->Lesd; /* its length */\r
@@ -232,7 +304,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        unsigned long int left_THmarks_len;\r
 \r
                        /******* sot_pos = j2k->cstr_info->tile[tileno].start_pos; */\r
-                       sot_pos = j2k->cstr_info->tile[tileno].tp_start_pos[tpno];\r
+                       sot_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos;\r
                        cio_seek(cio, sot_pos + 2); \r
                        sot_len = cio_read(cio, 2); /* SOT Len */\r
                        cio_skip(cio, 2);\r
@@ -240,7 +312,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        Psot = cio_read(cio, 4); /* tile length */\r
 \r
                        /******* post_sod_pos = j2k->cstr_info->tile[tileno].end_header + 1; */\r
-                       post_sod_pos = j2k->cstr_info->tile[tileno].tp_end_header[tpno] + 1;\r
+                       post_sod_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_end_header + 1;\r
                        left_THmarks_len = post_sod_pos - sot_pos;\r
 \r
                        /* add all the lengths of the markers which are len-ready and stay within SOT and SOD */\r
@@ -265,7 +337,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        if (j2k->cp->esd_on && (sens >= 0)) {\r
 \r
                                /* Create the ESD */\r
-                               if (esd_mark = jpwl_esd_create(\r
+                               if ((esd_mark = jpwl_esd_create(\r
                                        j2k, /* this encoder handle */\r
                                        -1, /* we are averaging over all components */\r
                                        (unsigned char) j2k->cp->sens_range, /* range method */\r
@@ -275,14 +347,14 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                                        tileno, /* this ESD is in a tile */\r
                                        0, /* number of packets in codestream */\r
                                        NULL /* pointer to sensitivity data of packets */\r
-                                       )) {\r
+                                       ))) {\r
                                        \r
                                        /* Add this marker to the 'insertanda' list */\r
                                        if (jwmarker_num < JPWL_MAX_NO_MARKERS) {\r
                                                jwmarker[jwmarker_num].id = J2K_MS_ESD; /* its type */\r
-                                               jwmarker[jwmarker_num].esdmark = esd_mark; /* the EPB */\r
+                                               jwmarker[jwmarker_num].m.esdmark = esd_mark; /* the EPB */\r
                                                /****** jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].start_pos + sot_len + 2; */ /* after SOT */\r
-                                               jwmarker[jwmarker_num].pos = soc_pos + j2k->cstr_info->tile[tileno].tp_start_pos[tpno] + sot_len + 2; /* after SOT */\r
+                                               jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2; /* after SOT */\r
                                                jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.2; /* not first at all! */\r
                                                jwmarker[jwmarker_num].len = esd_mark->Lesd; /* its length */\r
                                                jwmarker[jwmarker_num].len_ready = true; /* ready, yet */\r
@@ -344,7 +416,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                }\r
 \r
                /* Create the EPB */\r
-               if (epb_mark = jpwl_epb_create(\r
+               if ((epb_mark = jpwl_epb_create(\r
                        j2k, /* this encoder handle */\r
                        true, /* is it the latest? */\r
                        true, /* is it packed? not for now */\r
@@ -353,12 +425,12 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        j2k->cp->hprot_MH, /* protection type parameters of data */\r
                        socsiz_len, /* pre-data: only SOC+SIZ */\r
                        left_MHmarks_len /* post-data: from SOC to SOT, and all JPWL markers within */\r
-                       )) {\r
+                       ))) {\r
                        \r
                        /* Add this marker to the 'insertanda' list */\r
                        if (jwmarker_num < JPWL_MAX_NO_MARKERS) {\r
                                jwmarker[jwmarker_num].id = J2K_MS_EPB; /* its type */\r
-                               jwmarker[jwmarker_num].epbmark = epb_mark; /* the EPB */\r
+                               jwmarker[jwmarker_num].m.epbmark = epb_mark; /* the EPB */\r
                                jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* after SIZ */\r
                                jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos; /* first first first! */\r
                                jwmarker[jwmarker_num].len = epb_mark->Lepb; /* its length */\r
@@ -402,13 +474,13 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                \r
                        int sot_len, Psot, Psotp, mm, epb_index = 0, prot_len = 0;\r
                        unsigned long sot_pos, post_sod_pos;\r
-                       unsigned long int left_THmarks_len, epbs_len = 0;\r
-                       int startpack = 0, stoppack = j2k->cstr_info->num;\r
+                       unsigned long int left_THmarks_len/*, epbs_len = 0*/;\r
+                       int startpack = 0, stoppack = j2k->cstr_info->packno;\r
                        int first_tp_pack, last_tp_pack;\r
                        jpwl_epb_ms_t *tph_epb = NULL;\r
 \r
                        /****** sot_pos = j2k->cstr_info->tile[tileno].start_pos; */\r
-                       sot_pos = j2k->cstr_info->tile[tileno].tp_start_pos[tpno];\r
+                       sot_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos;\r
                        cio_seek(cio, sot_pos + 2); \r
                        sot_len = cio_read(cio, 2); /* SOT Len */\r
                        cio_skip(cio, 2);\r
@@ -417,7 +489,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
 \r
                        /* a-priori length of the data dwelling between SOT and SOD */\r
                        /****** post_sod_pos = j2k->cstr_info->tile[tileno].end_header + 1; */\r
-                       post_sod_pos = j2k->cstr_info->tile[tileno].tp_end_header[tpno] + 1;\r
+                       post_sod_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_end_header + 1;\r
                        left_THmarks_len = post_sod_pos - (sot_pos + sot_len + 2);\r
 \r
                        /* add all the lengths of the JPWL markers which are len-ready and stay within SOT and SOD */\r
@@ -442,7 +514,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        if (j2k->cp->epb_on && (hprot > 0)) {\r
 \r
                                /* Create the EPB */\r
-                               if (epb_mark = jpwl_epb_create(\r
+                               if ((epb_mark = jpwl_epb_create(\r
                                        j2k, /* this encoder handle */\r
                                        false, /* is it the latest? in TPH, no for now (if huge data size in TPH, we'd need more) */\r
                                        true, /* is it packed? yes for now */\r
@@ -451,14 +523,14 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                                        hprot, /* protection type parameters of following data */\r
                                        sot_len + 2, /* pre-data length: only SOT */\r
                                        left_THmarks_len /* post-data length: from SOT end to SOD inclusive */\r
-                                       )) {\r
+                                       ))) {\r
                                        \r
                                        /* Add this marker to the 'insertanda' list */\r
                                        if (jwmarker_num < JPWL_MAX_NO_MARKERS) {\r
                                                jwmarker[jwmarker_num].id = J2K_MS_EPB; /* its type */\r
-                                               jwmarker[jwmarker_num].epbmark = epb_mark; /* the EPB */\r
+                                               jwmarker[jwmarker_num].m.epbmark = epb_mark; /* the EPB */\r
                                                /****** jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].start_pos + sot_len + 2; */ /* after SOT */\r
-                                               jwmarker[jwmarker_num].pos = soc_pos + j2k->cstr_info->tile[tileno].tp_start_pos[tpno] + sot_len + 2; /* after SOT */\r
+                                               jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2; /* after SOT */\r
                                                jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos; /* first first first! */\r
                                                jwmarker[jwmarker_num].len = epb_mark->Lepb; /* its length */\r
                                                jwmarker[jwmarker_num].len_ready = true; /* ready */\r
@@ -493,9 +565,10 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        startpack = 0;\r
                        /* EPB MSs for UEP packet data protection in Tile Parts */\r
                        /****** for (packno = 0; packno < j2k->cstr_info->num; packno++) { */\r
-                       first_tp_pack = (tpno > 0) ? (first_tp_pack + j2k->cstr_info->tile[tileno].tp_num[tpno - 1]) : 0;\r
-                       last_tp_pack = first_tp_pack + j2k->cstr_info->tile[tileno].tp_num[tpno] - 1;\r
-                       for (packno = 0; packno < j2k->cstr_info->tile[tileno].tp_num[tpno]; packno++) {\r
+                       /*first_tp_pack = (tpno > 0) ? (first_tp_pack + j2k->cstr_info->tile[tileno].tp[tpno - 1].tp_numpacks) : 0;*/\r
+                       first_tp_pack = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pack;\r
+                       last_tp_pack = first_tp_pack + j2k->cstr_info->tile[tileno].tp[tpno].tp_numpacks - 1;\r
+                       for (packno = 0; packno < j2k->cstr_info->tile[tileno].tp[tpno].tp_numpacks; packno++) {\r
 \r
                                /******** if ((packspec < JPWL_MAX_NO_PACKSPECS) &&\r
                                        (j2k->cp->pprot_tileno[packspec] == tileno) && (j2k->cp->pprot_packno[packspec] == packno)) { */\r
@@ -547,7 +620,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                                                        &epb_index, /* pointer to EPB index */\r
                                                        pprot, /* protection type */\r
                                                        /****** (double) (j2k->cstr_info->tile[tileno].start_pos + sot_len + 2) + 0.0001, */ /* position */\r
-                                                       (double) (j2k->cstr_info->tile[tileno].tp_start_pos[tpno] + sot_len + 2) + 0.0001, /* position */\r
+                                                       (double) (j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2) + 0.0001, /* position */\r
                                                        tileno, /* number of tile */\r
                                                        0, /* length of pre-data */\r
                                                        prot_len /*4000*/ /* length of post-data */\r
@@ -607,7 +680,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                                                        &epb_index, /* pointer to EPB index */\r
                                                        pprot, /* protection type */\r
                                                        /***** (double) (j2k->cstr_info->tile[tileno].start_pos + sot_len + 2) + 0.0001,*/ /* position */\r
-                                                       (double) (j2k->cstr_info->tile[tileno].tp_start_pos[tpno] + sot_len + 2) + 0.0001, /* position */\r
+                                                       (double) (j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2) + 0.0001, /* position */\r
                                                        tileno, /* number of tile */\r
                                                        0, /* length of pre-data */\r
                                                        prot_len /*4000*/ /* length of post-data */\r
@@ -639,7 +712,7 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
        int mm;\r
        unsigned long int old_size = j2k->cstr_info->codestream_size;\r
        unsigned long int new_size = old_size;\r
-       int ciopos = cio_tell(cio), soc_pos = j2k->cstr_info->main_head_start;\r
+       int /*ciopos = cio_tell(cio),*/ soc_pos = j2k->cstr_info->main_head_start;\r
        unsigned char *jpwl_buf, *orig_buf;\r
        unsigned long int orig_pos;\r
        double epbcoding_time = 0.0, esdcoding_time = 0.0;\r
@@ -647,23 +720,23 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
        /* Order JPWL markers according to their wishlist position */\r
        qsort((void *) jwmarker, (size_t) jwmarker_num, sizeof (jpwl_marker_t), jpwl_markcomp);\r
 \r
-       /* compute markers total size */\r
+       /* compute markers total size */ \r
        for (mm = 0; mm < jwmarker_num; mm++) {\r
                /*printf("%x, %d, %.10f, %d long\n", jwmarker[mm].id, jwmarker[mm].pos,\r
                        jwmarker[mm].dpos, jwmarker[mm].len);*/\r
                new_size += jwmarker[mm].len + 2;\r
        }\r
 \r
-       /* allocate a temporary buffer of proper size */\r
-       if (!(jpwl_buf = (unsigned char *) opj_malloc((size_t) new_size * sizeof (unsigned char)))) {\r
-               opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for JPWL temp codestream buffer\n");\r
+       /* allocate a new buffer of proper size */\r
+       if (!(jpwl_buf = (unsigned char *) opj_malloc((size_t) (new_size + soc_pos) * sizeof(unsigned char)))) {\r
+               opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for JPWL codestream buffer\n");\r
                exit(1);\r
        };\r
 \r
        /* copy the jp2 part, if any */\r
-       memcpy(jpwl_buf, cio->buffer, soc_pos);\r
-\r
        orig_buf = jpwl_buf;\r
+       memcpy(jpwl_buf, cio->buffer, soc_pos);\r
+       jpwl_buf += soc_pos;\r
 \r
        /* cycle through markers */\r
        orig_pos = soc_pos + 0; /* start from the beginning */\r
@@ -685,15 +758,15 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                switch (jwmarker[mm].id) {\r
 \r
                case J2K_MS_EPB:\r
-                       jpwl_epb_write(jwmarker[mm].epbmark, jpwl_buf);\r
+                       jpwl_epb_write(j2k, jwmarker[mm].m.epbmark, jpwl_buf);\r
                        break;\r
 \r
                case J2K_MS_EPC:\r
-                       jpwl_epc_write(jwmarker[mm].epcmark, jpwl_buf);\r
+                       jpwl_epc_write(j2k, jwmarker[mm].m.epcmark, jpwl_buf);\r
                        break;\r
 \r
                case J2K_MS_ESD:\r
-                       jpwl_esd_write(jwmarker[mm].esdmark, jpwl_buf);\r
+                       jpwl_esd_write(j2k, jwmarker[mm].m.esdmark, jpwl_buf);\r
                        break;\r
 \r
                case J2K_MS_RED:\r
@@ -704,6 +777,10 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        break;\r
                };\r
 \r
+               /* we update the markers struct */\r
+               if (j2k->cstr_info)\r
+                       j2k->cstr_info->marker[j2k->cstr_info->marknum - 1].pos = (jpwl_buf - orig_buf);\r
+               \r
                /* we set the marker dpos to the new position in the JPWL codestream */\r
                jwmarker[mm].dpos = (double) (jpwl_buf - orig_buf);\r
 \r
@@ -713,8 +790,8 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
        }\r
 \r
        /* finish remaining original codestream */\r
-       memcpy(jpwl_buf, cio_getbp(cio), soc_pos + old_size - orig_pos);\r
-       jpwl_buf += soc_pos + old_size - orig_pos;\r
+       memcpy(jpwl_buf, cio_getbp(cio), old_size - (orig_pos - soc_pos));\r
+       jpwl_buf += old_size - (orig_pos - soc_pos);\r
        cio_seek(cio, soc_pos + old_size);\r
        \r
        /*\r
@@ -735,11 +812,11 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        unsigned short int mycrc = 0x0000;\r
 \r
                        /* fix and fill the DL field */\r
-                       jwmarker[mm].epcmark->DL = new_size;\r
-                       orig_buf[epc_pos + 6] = (unsigned char) (jwmarker[mm].epcmark->DL >> 24);\r
-                       orig_buf[epc_pos + 7] = (unsigned char) (jwmarker[mm].epcmark->DL >> 16);\r
-                       orig_buf[epc_pos + 8] = (unsigned char) (jwmarker[mm].epcmark->DL >> 8);\r
-                       orig_buf[epc_pos + 9] = (unsigned char) (jwmarker[mm].epcmark->DL >> 0);\r
+                       jwmarker[mm].m.epcmark->DL = new_size;\r
+                       orig_buf[epc_pos + 6] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 24);\r
+                       orig_buf[epc_pos + 7] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 16);\r
+                       orig_buf[epc_pos + 8] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 8);\r
+                       orig_buf[epc_pos + 9] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 0);\r
 \r
                        /* compute the CRC field (excluding itself) */\r
                        for (pp = 0; pp < 4; pp++)\r
@@ -748,9 +825,9 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                                jpwl_updateCRC16(&mycrc, orig_buf[epc_pos + pp]);\r
 \r
                        /* fix and fill the CRC */\r
-                       jwmarker[mm].epcmark->Pcrc = mycrc;\r
-                       orig_buf[epc_pos + 4] = (unsigned char) (jwmarker[mm].epcmark->Pcrc >> 8);\r
-                       orig_buf[epc_pos + 5] = (unsigned char) (jwmarker[mm].epcmark->Pcrc >> 0);\r
+                       jwmarker[mm].m.epcmark->Pcrc = mycrc;\r
+                       orig_buf[epc_pos + 4] = (unsigned char) (jwmarker[mm].m.epcmark->Pcrc >> 8);\r
+                       orig_buf[epc_pos + 5] = (unsigned char) (jwmarker[mm].m.epcmark->Pcrc >> 0);\r
 \r
                }\r
        }\r
@@ -765,7 +842,7 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        /* remember that they are now in a new position (dpos) */\r
                        int esd_pos = (int) jwmarker[mm].dpos;\r
 \r
-                       jpwl_esd_fill(j2k, jwmarker[mm].esdmark, &orig_buf[esd_pos]);\r
+                       jpwl_esd_fill(j2k, jwmarker[mm].m.esdmark, &orig_buf[esd_pos]);\r
                \r
                }\r
 \r
@@ -790,16 +867,16 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        accum_len = 0;\r
                        for (nn = mm; (nn < jwmarker_num) && (jwmarker[nn].id == J2K_MS_EPB) &&\r
                                (jwmarker[nn].pos == jwmarker[mm].pos); nn++)\r
-                               accum_len += jwmarker[nn].epbmark->Lepb + 2;\r
+                               accum_len += jwmarker[nn].m.epbmark->Lepb + 2;\r
 \r
                        /* fill the current (first) EPB with post-data starting from the computed position */\r
-                       jpwl_epb_fill(j2k, jwmarker[mm].epbmark, &orig_buf[(int) jwmarker[mm].dpos],\r
+                       jpwl_epb_fill(j2k, jwmarker[mm].m.epbmark, &orig_buf[(int) jwmarker[mm].dpos],\r
                                &orig_buf[(int) jwmarker[mm].dpos + accum_len]);\r
                \r
                        /* fill the remaining EPBs in the header with post-data starting from the last position */\r
                        for (nn = mm + 1; (nn < jwmarker_num) && (jwmarker[nn].id == J2K_MS_EPB) &&\r
                                (jwmarker[nn].pos == jwmarker[mm].pos); nn++)\r
-                               jpwl_epb_fill(j2k, jwmarker[nn].epbmark, &orig_buf[(int) jwmarker[nn].dpos], NULL);\r
+                               jpwl_epb_fill(j2k, jwmarker[nn].m.epbmark, &orig_buf[(int) jwmarker[nn].dpos], NULL);\r
 \r
                        /* skip all the processed EPBs */\r
                        mm = nn - 1;\r
@@ -812,13 +889,13 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
 \r
        /* free original cio buffer and set it to the JPWL one */\r
        opj_free(cio->buffer);\r
-       /*cio->cinfo;*/ /* no change */\r
-       /*cio->openmode;*/ /* no change */\r
-       cio->buffer = jpwl_buf - new_size - soc_pos;\r
+       cio->cinfo = cio->cinfo; /* no change */\r
+       cio->openmode = cio->openmode; /* no change */\r
+       cio->buffer = orig_buf;\r
        cio->length = new_size + soc_pos;\r
-       cio->start = jpwl_buf - new_size - soc_pos;\r
-       cio->end = jpwl_buf - 1;\r
-       cio->bp = jpwl_buf - new_size - soc_pos;\r
+       cio->start = cio->buffer;\r
+       cio->end = cio->buffer + cio->length;\r
+       cio->bp = cio->buffer;\r
        cio_seek(cio, soc_pos + new_size);\r
 \r
 }\r
@@ -930,6 +1007,10 @@ void j2k_write_epc(opj_j2k_t *j2k) {
                cio_write(cio, Pcrc, 2);\r
 \r
        cio_seek(cio, Lepcp + Lepc);\r
+\r
+       /* marker struct update */\r
+       j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, Lepcp - 2, Lepc + 2);\r
+\r
 }\r
 \r
 void j2k_read_epb(opj_j2k_t *j2k) {\r
@@ -1025,9 +1106,9 @@ void j2k_read_epb(opj_j2k_t *j2k) {
                if (((Pepb & 0xF0000000) >> 28) == 0)\r
                        sprintf(str1, "pred"); /* predefined */\r
                else if (((Pepb & 0xF0000000) >> 28) == 1)\r
-                       sprintf(str1, "crc-%d", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */\r
+                       sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */\r
                else if (((Pepb & 0xF0000000) >> 28) == 2)\r
-                       sprintf(str1, "rs(%d,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */\r
+                       sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */\r
                else if (Pepb == 0xFFFFFFFF)\r
                        sprintf(str1, "nometh"); /* RS mode */\r
                else\r
@@ -1078,6 +1159,9 @@ void j2k_write_epb(opj_j2k_t *j2k) {
        cio_write(cio, Lepb, 2);                /* Lepb */\r
 \r
        cio_seek(cio, Lepbp + Lepb);\r
+\r
+       /* marker struct update */\r
+       j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, Lepbp - 2, Lepb + 2);\r
 }\r
 \r
 void j2k_read_esd(opj_j2k_t *j2k) {\r
@@ -1135,6 +1219,7 @@ void j2k_read_red(opj_j2k_t *j2k) {
 \r
 bool jpwl_check_tile(opj_j2k_t *j2k, opj_tcd_t *tcd, int tileno) {\r
 \r
+#ifdef oerhgierhgvhreit4u\r
        /*\r
           we navigate through the tile and find possible invalid parameters:\r
        this saves a lot of crashes!!!!!\r
@@ -1196,6 +1281,8 @@ bool jpwl_check_tile(opj_j2k_t *j2k, opj_tcd_t *tcd, int tileno) {
                }\r
        }\r
 \r
+#endif\r
+\r
        return true;\r
 }\r
 \r