Patch from Callum Lewick. Memset patch. See ChangeLog for more details. Thanks Callum !
[openjpeg.git] / jpwl / jpwl.c
index 0246100c802ed21abd02b0071cfd821b4abd9596..894e7d3fd2c7f11f345c4f9acf372d95af134d9e 100644 (file)
  * POSSIBILITY OF SUCH DAMAGE.\r
  */\r
 \r
-#ifdef USE_JPWL\r
-\r
 #include "../libopenjpeg/opj_includes.h"\r
 \r
+#ifdef USE_JPWL\r
+\r
 /** @defgroup JPWL JPWL - JPEG-2000 Part11 (JPWL) codestream manager */\r
 /*@{*/\r
 \r
 /** @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
@@ -107,13 +107,64 @@ void jpwl_esd_write(jpwl_esd_ms_t *esdmark, unsigned char *buf);
 \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
+                       free(jwmarker[mm].epbmark);\r
+                       break;\r
+\r
+               case J2K_MS_EPC:\r
+                       free(jwmarker[mm].epcmark);\r
+                       break;\r
+\r
+               case J2K_MS_ESD:\r
+                       free(jwmarker[mm].esdmark);\r
+                       break;\r
+\r
+               case J2K_MS_RED:\r
+                       free(jwmarker[mm].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
+\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
-       int ciopos = cio_tell(cio);\r
+       int ciopos = cio_tell(cio), soc_pos = j2k->cstr_info->main_head_start;\r
        unsigned char *socp = NULL;\r
 \r
-       int tileno, tilespec, hprot, sens, pprot, packspec, lastileno, packno;\r
+       int tileno, acc_tpno, tpno, tilespec, hprot, sens, pprot, packspec, lastileno, packno;\r
 \r
        jpwl_epb_ms_t *epb_mark;\r
        jpwl_epc_ms_t *epc_mark;\r
@@ -121,9 +172,9 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
 \r
        /* find SOC + SIZ length */\r
        /* I assume SIZ is always the first marker after SOC */\r
-       cio_seek(cio, 4);\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
-       cio_seek(cio, 0);\r
+       cio_seek(cio, soc_pos + 0);\r
        socp = cio_getbp(cio); /* pointer to SOC */\r
 \r
        /* \r
@@ -142,7 +193,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                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].pos = socsiz_len; /* after SIZ */\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
                        jwmarker[jwmarker_num].len_ready = true; /* ready */\r
@@ -179,7 +230,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        (unsigned char) j2k->cp->sens_MH, /* sensitivity method */\r
                        j2k->cp->sens_size, /* sensitivity size */\r
                        -1, /* this ESD is in main header */\r
-                       0 /*j2k->image_info->num*/, /* number of packets in codestream */\r
+                       0 /*j2k->cstr_info->num*/, /* number of packets in codestream */\r
                        NULL /*sensval*/ /* pointer to sensitivity data of packets */\r
                        )) {\r
                        \r
@@ -187,7 +238,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        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].pos = socsiz_len; /* we choose to place it after SIZ */\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
                                jwmarker[jwmarker_num].len_ready = true; /* not ready, yet */\r
@@ -212,89 +263,107 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
        /* \r
         ESD MSs for Tile Part Headers \r
        */\r
-       /* cycle through TPHs */\r
+       /* cycle through tiles */\r
        sens = -1; /* default spec: no ESD */\r
        tilespec = 0; /* first tile spec */\r
-       for (tileno = 0; tileno < j2k->image_info->tw * j2k->image_info->th; tileno++) {\r
-\r
-               int sot_len, Psot, Psotp, mm;\r
-               unsigned long sot_pos, post_sod_pos;\r
+       acc_tpno = 0;\r
+       for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) {\r
 \r
-               unsigned long int left_THmarks_len;\r
-\r
-               sot_pos = j2k->image_info->tile[tileno].start_pos;\r
-               cio_seek(cio, sot_pos + 2); \r
-               sot_len = cio_read(cio, 2); /* SOT Len */\r
-               cio_skip(cio, 2);\r
-               Psotp = cio_tell(cio);\r
-               Psot = cio_read(cio, 4); /* tile length */\r
-\r
-               post_sod_pos = j2k->image_info->tile[tileno].end_header + 1;\r
-               left_THmarks_len = post_sod_pos - sot_pos;\r
+               opj_event_msg(j2k->cinfo, EVT_INFO,\r
+                       "Tile %d has %d tile part(s)\n",\r
+                       tileno, j2k->cstr_info->tile[tileno].num_tps\r
+                       );\r
 \r
-               /* add all the lengths of the markers which are len-ready and stay within SOT and SOD */\r
-               for (mm = 0; mm < jwmarker_num; mm++) {\r
-                       if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) {\r
-                               if (jwmarker[mm].len_ready)\r
-                                       left_THmarks_len += jwmarker[mm].len + 2;\r
-                               else {\r
-                                       opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n",\r
-                                               jwmarker[mm].id, jwmarker[mm].dpos);                            \r
-                                       exit(1);\r
+               /* for every tile part in the tile */\r
+               for (tpno = 0; tpno < j2k->cstr_info->tile[tileno].num_tps; tpno++, acc_tpno++) {\r
+       \r
+                       int sot_len, Psot, Psotp, mm;\r
+                       unsigned long sot_pos, post_sod_pos;\r
+\r
+                       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[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
+                       Psotp = cio_tell(cio);\r
+                       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[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
+                       for (mm = 0; mm < jwmarker_num; mm++) {\r
+                               if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) {\r
+                                       if (jwmarker[mm].len_ready)\r
+                                               left_THmarks_len += jwmarker[mm].len + 2;\r
+                                       else {\r
+                                               opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n",\r
+                                                       jwmarker[mm].id, jwmarker[mm].dpos);                            \r
+                                               exit(1);\r
+                                       }\r
                                }\r
                        }\r
-               }\r
 \r
-               if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == tileno))\r
-                       /* we got a specification from this tile onwards */\r
-                       sens = j2k->cp->sens_TPH[tilespec++];\r
-       \r
-               /* must this TPH have an ESD MS? */\r
-               if (j2k->cp->esd_on && (sens >= 0)) {\r
-\r
-                       /* Create the ESD */\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
-                               (unsigned char) j2k->cp->sens_addr, /* sensitivity addressing size */\r
-                               (unsigned char) sens, /* sensitivity method */\r
-                               j2k->cp->sens_size, /* sensitivity value size */\r
-                               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
-                               /* 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].pos = j2k->image_info->tile[tileno].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
-                                       jwmarker[jwmarker_num].pos_ready = true; /* ready */\r
-                                       jwmarker[jwmarker_num].parms_ready = true; /* not ready */\r
-                                       jwmarker[jwmarker_num].data_ready = false; /* ready */\r
-                                       jwmarker_num++;\r
-                               }\r
+                       /******* if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == tileno)) */\r
+                       if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == acc_tpno))\r
+                               /* we got a specification from this tile onwards */\r
+                               sens = j2k->cp->sens_TPH[tilespec++];\r
+               \r
+                       /* must this TPH have an ESD MS? */\r
+                       if (j2k->cp->esd_on && (sens >= 0)) {\r
+\r
+                               /* Create the ESD */\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
+                                       (unsigned char) j2k->cp->sens_addr, /* sensitivity addressing size */\r
+                                       (unsigned char) sens, /* sensitivity method */\r
+                                       j2k->cp->sens_size, /* sensitivity value size */\r
+                                       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
+                                       /* 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].pos = j2k->cstr_info->tile[tileno].start_pos + 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
+                                               jwmarker[jwmarker_num].pos_ready = true; /* ready */\r
+                                               jwmarker[jwmarker_num].parms_ready = true; /* not ready */\r
+                                               jwmarker[jwmarker_num].data_ready = false; /* ready */\r
+                                               jwmarker_num++;\r
+                                       }\r
 \r
-                               /* update Psot of the tile  */\r
-                               cio_seek(cio, Psotp);\r
-                               cio_write(cio, Psot + esd_mark->Lesd + 2, 4);\r
+                                       /* update Psot of the tile  */\r
+                                       cio_seek(cio, Psotp);\r
+                                       cio_write(cio, Psot + esd_mark->Lesd + 2, 4);\r
 \r
-                               opj_event_msg(j2k->cinfo, EVT_INFO,\r
-                                       "TPH ESDs: tile %02d, method %d\n",\r
-                                       tileno,\r
-                                       sens\r
-                                       );\r
+                                       opj_event_msg(j2k->cinfo, EVT_INFO,\r
+                                               /******* "TPH ESDs: tile %02d, method %d\n", */\r
+                                               "TPH ESDs: tile %02d, part %02d, method %d\n",\r
+                                               /******* tileno, */\r
+                                               tileno, tpno,\r
+                                               sens\r
+                                               );\r
 \r
-                       } else {\r
-                               /* ooops, problems */\r
-                               opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d\n", tileno);                         \r
-                       };\r
+                               } else {\r
+                                       /* ooops, problems */\r
+                                       /***** opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d\n", tileno); */\r
+                                       opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d,%d\n", tileno, tpno);\r
+                               };\r
 \r
-               }                               \r
+                       }\r
+                       \r
+               }\r
        \r
        };\r
 \r
@@ -307,7 +376,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                int mm;\r
 \r
                /* position of SOT */\r
-               unsigned int sot_pos = j2k->image_info->main_head_end + 1;\r
+               unsigned int sot_pos = j2k->cstr_info->main_head_end + 1;\r
 \r
                /* how much space is there between end of SIZ and beginning of SOT? */\r
                int left_MHmarks_len = sot_pos - socsiz_len;\r
@@ -341,7 +410,7 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                        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].pos = socsiz_len; /* after SIZ */\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
                                jwmarker[jwmarker_num].len_ready = true; /* ready */\r
@@ -371,200 +440,244 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
        lastileno = 0;\r
        packspec = 0;\r
        pprot = -1;\r
-       for (tileno = 0; tileno < j2k->image_info->tw * j2k->image_info->th; tileno++) {\r
-\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->image_info->num;\r
-               jpwl_epb_ms_t *tph_epb = NULL;\r
-\r
-               sot_pos = j2k->image_info->tile[tileno].start_pos;\r
-               cio_seek(cio, sot_pos + 2); \r
-               sot_len = cio_read(cio, 2); /* SOT Len */\r
-               cio_skip(cio, 2);\r
-               Psotp = cio_tell(cio);\r
-               Psot = cio_read(cio, 4); /* tile length */\r
+       acc_tpno = 0;\r
+       for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) {\r
 \r
-               /* a-priori length of the data dwelling between SOT and SOD */\r
-               post_sod_pos = j2k->image_info->tile[tileno].end_header + 1;\r
-               left_THmarks_len = post_sod_pos - (sot_pos + sot_len + 2);\r
+               opj_event_msg(j2k->cinfo, EVT_INFO,\r
+                       "Tile %d has %d tile part(s)\n",\r
+                       tileno, j2k->cstr_info->tile[tileno].num_tps\r
+                       );\r
 \r
-               /* add all the lengths of the JPWL markers which are len-ready and stay within SOT and SOD */\r
-               for (mm = 0; mm < jwmarker_num; mm++) {\r
-                       if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) {\r
-                               if (jwmarker[mm].len_ready)\r
-                                       left_THmarks_len += jwmarker[mm].len + 2;\r
-                               else {\r
-                                       opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n",\r
-                                               jwmarker[mm].id, jwmarker[mm].dpos);                            \r
-                                       exit(1);\r
+               /* for every tile part in the tile */\r
+               for (tpno = 0; tpno < j2k->cstr_info->tile[tileno].num_tps; tpno++, acc_tpno++) { \r
+               \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->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[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
+                       Psotp = cio_tell(cio);\r
+                       Psot = cio_read(cio, 4); /* tile length */\r
+\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[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
+                       for (mm = 0; mm < jwmarker_num; mm++) {\r
+                               if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) {\r
+                                       if (jwmarker[mm].len_ready)\r
+                                               left_THmarks_len += jwmarker[mm].len + 2;\r
+                                       else {\r
+                                               opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n",\r
+                                                       jwmarker[mm].id, jwmarker[mm].dpos);                            \r
+                                               exit(1);\r
+                                       }\r
                                }\r
                        }\r
-               }\r
-\r
-               if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == tileno))\r
-                       /* we got a specification from this tile onwards */\r
-                       hprot = j2k->cp->hprot_TPH[tilespec++];\r
-       \r
-               /* must this TPH have an EPB MS? */\r
-               if (j2k->cp->epb_on && (hprot > 0)) {\r
-\r
-                       /* Create the EPB */\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
-                               tileno, /* we are in TPH */\r
-                               epb_index++, /* its index is 0 (first) */\r
-                               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
-                               /* 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].pos = j2k->image_info->tile[tileno].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
-                                       jwmarker[jwmarker_num].pos_ready = true; /* ready */\r
-                                       jwmarker[jwmarker_num].parms_ready = true; /* ready */\r
-                                       jwmarker[jwmarker_num].data_ready = false; /* not ready */\r
-                                       jwmarker_num++;\r
-                               }\r
 \r
-                               /* update Psot of the tile  */\r
-                               Psot += epb_mark->Lepb + 2;\r
+                       /****** if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == tileno)) */\r
+                       if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == acc_tpno))\r
+                               /* we got a specification from this tile part onwards */\r
+                               hprot = j2k->cp->hprot_TPH[tilespec++];\r
+               \r
+                       /* must this TPH have an EPB MS? */\r
+                       if (j2k->cp->epb_on && (hprot > 0)) {\r
+\r
+                               /* Create the EPB */\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
+                                       tileno, /* we are in TPH */\r
+                                       epb_index++, /* its index is 0 (first) */\r
+                                       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
+                                       /* 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].pos = j2k->cstr_info->tile[tileno].start_pos + 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
+                                               jwmarker[jwmarker_num].pos_ready = true; /* ready */\r
+                                               jwmarker[jwmarker_num].parms_ready = true; /* ready */\r
+                                               jwmarker[jwmarker_num].data_ready = false; /* not ready */\r
+                                               jwmarker_num++;\r
+                                       }\r
 \r
-                               opj_event_msg(j2k->cinfo, EVT_INFO,\r
-                                       "TPH EPB : tile %02d, prot. %d\n",\r
-                                       tileno,\r
-                                       hprot\r
-                                       );\r
+                                       /* update Psot of the tile  */\r
+                                       Psot += epb_mark->Lepb + 2;\r
 \r
-                               /* save this TPH EPB address */\r
-                               tph_epb = epb_mark;\r
+                                       opj_event_msg(j2k->cinfo, EVT_INFO,\r
+                                               /***** "TPH EPB : tile %02d, prot. %d\n", */\r
+                                               "TPH EPB : tile %02d, part %02d, prot. %d\n",\r
+                                               /***** tileno, */\r
+                                               tileno, tpno,\r
+                                               hprot\r
+                                               );\r
 \r
-                       } else {\r
-                               /* ooops, problems */\r
-                               opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB #%d\n", tileno);                         \r
-                       };\r
+                                       /* save this TPH EPB address */\r
+                                       tph_epb = epb_mark;\r
 \r
-               }                               \r
-       \r
-               startpack = 0;\r
-               /* EPB MSs for UEP packet data protection in Tile Parts */\r
-               for (packno = 0; packno < j2k->image_info->num; packno++) {\r
+                               } else {\r
+                                       /* ooops, problems */\r
+                                       /****** opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB #%d\n", tileno); */\r
+                                       opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB in #%d,d\n", tileno, tpno);\r
+                               };\r
 \r
-                       if ((packspec < JPWL_MAX_NO_PACKSPECS) &&\r
-                               (j2k->cp->pprot_tileno[packspec] == tileno) && (j2k->cp->pprot_packno[packspec] == packno)) {\r
+                       }                               \r
+               \r
+                       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[tpno - 1].tp_numpacks) : 0;\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
+                               if ((packspec < JPWL_MAX_NO_PACKSPECS) &&\r
+                                       (j2k->cp->pprot_tileno[packspec] == acc_tpno) && (j2k->cp->pprot_packno[packspec] == packno)) {\r
+\r
+                                       /* we got a specification from this tile and packet onwards */\r
+                                       /* print the previous spec */\r
+                                       if (packno > 0) {\r
+                                               stoppack = packno - 1;                          \r
+                                               opj_event_msg(j2k->cinfo, EVT_INFO,\r
+                                                       /***** "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", */\r
+                                                       "UEP EPBs: tile %02d, part %02d, packs. %02d-%02d (B %d-%d), prot. %d\n",\r
+                                                       /***** tileno, */\r
+                                                       tileno, tpno,\r
+                                                       startpack,\r
+                                                       stoppack,\r
+                                                       /***** j2k->cstr_info->tile[tileno].packet[startpack].start_pos, */\r
+                                                       j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos,\r
+                                                       /***** j2k->cstr_info->tile[tileno].packet[stoppack].end_pos, */\r
+                                                       j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos,\r
+                                                       pprot);\r
+\r
+                                               /***** prot_len = j2k->cstr_info->tile[tileno].packet[stoppack].end_pos + 1 -\r
+                                                       j2k->cstr_info->tile[tileno].packet[startpack].start_pos; */\r
+                                               prot_len = j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos + 1 -\r
+                                                       j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos;\r
+\r
+                                               /*\r
+                                                 particular case: if this is the last header and the last packet,\r
+                                                 then it is better to protect even the EOC marker\r
+                                               */\r
+                                               /****** if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) &&\r
+                                                       (stoppack == (j2k->cstr_info->num - 1))) */\r
+                                               if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) &&\r
+                                                       (tpno == (j2k->cstr_info->tile[tileno].num_tps - 1)) &&\r
+                                                       (stoppack == last_tp_pack))\r
+                                                       /* add the EOC len */\r
+                                                       prot_len += 2;\r
+\r
+                                               /* let's add the EPBs */\r
+                                               Psot += jpwl_epbs_add(\r
+                                                       j2k, /* J2K handle */\r
+                                                       jwmarker, /* pointer to JPWL markers list */\r
+                                                       &jwmarker_num, /* pointer to the number of current markers */\r
+                                                       false, /* latest */\r
+                                                       true, /* packed */\r
+                                                       false, /* inside MH */\r
+                                                       &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[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
+                                                       );\r
+                                       }\r
 \r
-                               /* we got a specification from this tile and packet onwards */\r
-                               /* print the previous spec */\r
-                               if (packno > 0) {\r
-                                       stoppack = packno - 1;                          \r
-                                       opj_event_msg(j2k->cinfo, EVT_INFO,\r
-                                               "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n",\r
-                                               tileno,\r
-                                               startpack,\r
-                                               stoppack,\r
-                                               j2k->image_info->tile[tileno].packet[startpack].start_pos,\r
-                                               j2k->image_info->tile[tileno].packet[stoppack].end_pos,\r
-                                               pprot);\r
-\r
-                                       prot_len = j2k->image_info->tile[tileno].packet[stoppack].end_pos + 1 -\r
-                                               j2k->image_info->tile[tileno].packet[startpack].start_pos;\r
-\r
-                                       /*\r
-                                         particular case: if this is the last header and the last packet,\r
-                                         then it is better to protect even the EOC marker\r
-                                       */\r
-                                       if ((tileno == ((j2k->image_info->tw * j2k->image_info->th) - 1)) &&\r
-                                               (stoppack == (j2k->image_info->num - 1)))\r
-                                               /* add the EOC len */\r
-                                               prot_len += 2;\r
-\r
-                                       /* let's add the EPBs */\r
-                                       Psot += jpwl_epbs_add(\r
-                                               j2k, /* J2K handle */\r
-                                               jwmarker, /* pointer to JPWL markers list */\r
-                                               &jwmarker_num, /* pointer to the number of current markers */\r
-                                               false, /* latest */\r
-                                               true, /* packed */\r
-                                               false, /* inside MH */\r
-                                               &epb_index, /* pointer to EPB index */\r
-                                               pprot, /* protection type */\r
-                                               (double) (j2k->image_info->tile[tileno].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
-                                               );\r
+                                       startpack = packno;\r
+                                       pprot = j2k->cp->pprot[packspec++];\r
                                }\r
 \r
-                               startpack = packno;\r
-                               pprot = j2k->cp->pprot[packspec++];\r
+                               //printf("Tile %02d, pack %02d ==> %d\n", tileno, packno, pprot);\r
+               \r
                        }\r
 \r
-                       //printf("Tile %02d, pack %02d ==> %d\n", tileno, packno, pprot);\r
-       \r
-               }\r
+                       /* we are at the end: print the remaining spec */\r
+                       stoppack = packno - 1;\r
+                       if (pprot >= 0) {\r
 \r
-               /* we are at the end: print the remaining spec */\r
-               stoppack = packno - 1;\r
-               if (pprot >= 0) {\r
+                               opj_event_msg(j2k->cinfo, EVT_INFO,\r
+                                       /**** "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", */\r
+                                       "UEP EPBs: tile %02d, part %02d, packs. %02d-%02d (B %d-%d), prot. %d\n",\r
+                                       /**** tileno, */\r
+                                       tileno, tpno,\r
+                                       startpack,\r
+                                       stoppack,\r
+                                       /***** j2k->image_info->tile[tileno].packet[startpack].start_pos,\r
+                                       j2k->image_info->tile[tileno].packet[stoppack].end_pos, */\r
+                                       j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos,\r
+                                       j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos,\r
+                                       pprot);\r
+\r
+                               /***** prot_len = j2k->cstr_info->tile[tileno].packet[stoppack].end_pos + 1 -\r
+                                       j2k->cstr_info->tile[tileno].packet[startpack].start_pos; */\r
+                               prot_len = j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos + 1 -\r
+                                       j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos;\r
+\r
+                               /*\r
+                                 particular case: if this is the last header and the last packet,\r
+                                 then it is better to protect even the EOC marker\r
+                               */\r
+                               /***** if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) &&\r
+                                       (stoppack == (j2k->cstr_info->num - 1))) */\r
+                               if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) &&\r
+                                       (tpno == (j2k->cstr_info->tile[tileno].num_tps - 1)) &&\r
+                                       (stoppack == last_tp_pack))\r
+                                       /* add the EOC len */\r
+                                       prot_len += 2;\r
+\r
+                               /* let's add the EPBs */\r
+                               Psot += jpwl_epbs_add(\r
+                                                       j2k, /* J2K handle */\r
+                                                       jwmarker, /* pointer to JPWL markers list */\r
+                                                       &jwmarker_num, /* pointer to the number of current markers */\r
+                                                       true, /* latest */\r
+                                                       true, /* packed */\r
+                                                       false, /* inside MH */\r
+                                                       &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[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
+                                                       );\r
+                       }\r
 \r
-                       opj_event_msg(j2k->cinfo, EVT_INFO,\r
-                               "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n",\r
-                               tileno,\r
-                               startpack,\r
-                               stoppack,\r
-                               j2k->image_info->tile[tileno].packet[startpack].start_pos,\r
-                               j2k->image_info->tile[tileno].packet[stoppack].end_pos,\r
-                               pprot);\r
-\r
-                       prot_len = j2k->image_info->tile[tileno].packet[stoppack].end_pos + 1 -\r
-                               j2k->image_info->tile[tileno].packet[startpack].start_pos;\r
-\r
-                       /*\r
-                         particular case: if this is the last header and the last packet,\r
-                         then it is better to protect even the EOC marker\r
-                       */\r
-                       if ((tileno == ((j2k->image_info->tw * j2k->image_info->th) - 1)) &&\r
-                               (stoppack == (j2k->image_info->num - 1)))\r
-                               /* add the EOC len */\r
-                               prot_len += 2;\r
-\r
-                       /* let's add the EPBs */\r
-                       Psot += jpwl_epbs_add(\r
-                                               j2k, /* J2K handle */\r
-                                               jwmarker, /* pointer to JPWL markers list */\r
-                                               &jwmarker_num, /* pointer to the number of current markers */\r
-                                               true, /* latest */\r
-                                               true, /* packed */\r
-                                               false, /* inside MH */\r
-                                               &epb_index, /* pointer to EPB index */\r
-                                               pprot, /* protection type */\r
-                                               (double) (j2k->image_info->tile[tileno].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
-                                               );\r
-               }\r
+                       /* we can now check if the TPH EPB was really the last one */\r
+                       if (tph_epb && (epb_index == 1)) {\r
+                               /* set the TPH EPB to be the last one in current header */\r
+                               tph_epb->Depb |= (unsigned char) ((true & 0x0001) << 6);\r
+                               tph_epb = NULL;\r
+                       }\r
 \r
-               /* we can now check if the TPH EPB was really the last one */\r
-               if (tph_epb && (epb_index == 1)) {\r
-                       /* set the TPH EPB to be the last one in current header */\r
-                       tph_epb->Depb |= (unsigned char) ((true & 0x0001) << 6);\r
-                       tph_epb = NULL;\r
+                       /* write back Psot */\r
+                       cio_seek(cio, Psotp);\r
+                       cio_write(cio, Psot, 4);\r
+               \r
                }\r
 \r
-               /* write back Psot */\r
-               cio_seek(cio, Psotp);\r
-               cio_write(cio, Psot, 4);\r
-\r
        };\r
 \r
        /* reset the position */\r
@@ -575,9 +688,9 @@ void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
 void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {\r
 \r
        int mm;\r
-       unsigned long int old_size = j2k->image_info->codestream_size;\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);\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
@@ -585,23 +698,27 @@ 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
+       /* allocate a new 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
+               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
        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 = 0; /* start from the beginning */\r
-       cio_seek(cio, 0); /* rewind the original */\r
+       orig_pos = soc_pos + 0; /* start from the beginning */\r
+       cio_seek(cio, soc_pos + 0); /* rewind the original */\r
        for (mm = 0; mm < jwmarker_num; mm++) {\r
 \r
                /*\r
@@ -647,15 +764,15 @@ 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), old_size - orig_pos);\r
-       jpwl_buf += old_size - orig_pos;\r
-       cio_seek(cio, old_size);\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
        update info file based on added markers\r
        */\r
        if (!jpwl_update_info(j2k, jwmarker, jwmarker_num))\r
-               opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not update OPJ image_info structure\n");\r
+               opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not update OPJ cstr_info structure\n");\r
 \r
        /* now we need to repass some markers and fill their data fields */\r
        \r
@@ -748,12 +865,13 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
        opj_free(cio->buffer);\r
        /*cio->cinfo;*/ /* no change */\r
        /*cio->openmode;*/ /* no change */\r
-       cio->buffer = jpwl_buf - new_size;\r
-       cio->length = new_size;\r
-       cio->start = jpwl_buf - new_size;\r
+       /*cio->buffer = jpwl_buf - new_size - soc_pos;*/\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;\r
-       cio_seek(cio, new_size);\r
+       cio->bp = jpwl_buf - new_size - soc_pos;\r
+       cio_seek(cio, soc_pos + new_size);\r
 \r
 }\r
 \r
@@ -1135,4 +1253,70 @@ bool jpwl_check_tile(opj_j2k_t *j2k, opj_tcd_t *tcd, int tileno) {
 \r
 /*@}*/\r
 \r
-#endif /* USE_JPWL */
\ No newline at end of file
+#endif /* USE_JPWL */\r
+\r
+\r
+#ifdef USE_JPSEC\r
+\r
+/** @defgroup JPSEC JPSEC - JPEG-2000 Part 8 (JPSEC) codestream manager */\r
+/*@{*/\r
+\r
+\r
+/** @name Local static functions */\r
+/*@{*/\r
+\r
+void j2k_read_sec(opj_j2k_t *j2k) {\r
+       unsigned short int Lsec;\r
+       \r
+       opj_cio_t *cio = j2k->cio;\r
+\r
+       /* Simply read the SEC length */\r
+       Lsec = cio_read(cio, 2);\r
+\r
+       /* Now we write them to screen */\r
+       opj_event_msg(j2k->cinfo, EVT_INFO,\r
+               "SEC(%d)\n",\r
+               cio_tell(cio) - 2\r
+               );\r
+\r
+       cio_skip(cio, Lsec - 2);  \r
+}\r
+\r
+void j2k_write_sec(opj_j2k_t *j2k) {\r
+       unsigned short int Lsec = 24;\r
+       int i;\r
+\r
+       opj_cio_t *cio = j2k->cio;\r
+\r
+       cio_write(cio, J2K_MS_SEC, 2);  /* SEC */\r
+       cio_write(cio, Lsec, 2);\r
+\r
+       /* write dummy data */\r
+       for (i = 0; i < Lsec - 2; i++)\r
+               cio_write(cio, 0, 1);\r
+}\r
+\r
+void j2k_read_insec(opj_j2k_t *j2k) {\r
+       unsigned short int Linsec;\r
+       \r
+       opj_cio_t *cio = j2k->cio;\r
+\r
+       /* Simply read the INSEC length */\r
+       Linsec = cio_read(cio, 2);\r
+\r
+       /* Now we write them to screen */\r
+       opj_event_msg(j2k->cinfo, EVT_INFO,\r
+               "INSEC(%d)\n",\r
+               cio_tell(cio) - 2\r
+               );\r
+\r
+       cio_skip(cio, Linsec - 2);  \r
+}\r
+\r
+\r
+/*@}*/\r
+\r
+/*@}*/\r
+\r
+#endif /* USE_JPSEC */\r
+\r