One more field in the codestream_info struct for recording the number of packets...
authorGiuseppe Baruffa <gbaruffa@users.noreply.github.com>
Thu, 6 Sep 2007 15:59:39 +0000 (15:59 +0000)
committerGiuseppe Baruffa <gbaruffa@users.noreply.github.com>
Thu, 6 Sep 2007 15:59:39 +0000 (15:59 +0000)
ChangeLog
codec/image_to_j2k.c
jpwl/jpwl.c
jpwl/jpwl_lib.c
libopenjpeg/j2k.c
libopenjpeg/openjpeg.h

index 16b8029dfbedbc1784fd6065bc874d916cf0bf88..eb500934c4278c0c22676c91efb81fd52bef5845 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,9 +12,11 @@ September 6, 2007
 * [Mathieu Malaterre] OpenJPEG.rc update copyright year
 * [Mathieu Malaterre] CMake: add Java j2kviewer but do not compile it using cmake since cmake has too poor support for Java. Should create a custom command running ant instead.
 * [Mathieu Malaterre] CMake: Add doxygen output
++ [GB] One more field in the codestream_info struct for recording the number of packets per tile part; JPWL now distributes the EPBs in all the tile part headers
 
 September 4, 2007
 + [GB] Added some fields in the codestream_info structure: they are used to record the position of single tile parts. Changed also the write_index function in the codec, to reflect the presence of this new information.
+
 September 3, 2007
 + [GB] Added the knowledge of JPSEC SEC and INSEC markers (you have to compile the JPWL project). Management of these markers is limited to skipping them without crashing: no real security function at this stage. Deprecated USE_JPSEC will be removed next
 
index e522a505b65f6a707056a0b4101d9c0f62e13ed9..c921e2154b1b85a82e28f44114eb6fae755be4da 100644 (file)
@@ -209,31 +209,31 @@ void encode_help_display() {
 #ifdef USE_JPWL
        fprintf(stdout,"-W           : adoption of JPWL (Part 11) capabilities (-W params)\n");
        fprintf(stdout,"               The parameters can be written and repeated in any order:\n");
-       fprintf(stdout,"               [h<tile><=type>,s<tile><=method>,a=<addr>,z=<size>,g=<range>,...\n");
-       fprintf(stdout,"                ...,p<tile:pack><=type>]\n");
+       fprintf(stdout,"               [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n");
+       fprintf(stdout,"                ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n");
        fprintf(stdout,"\n");
        fprintf(stdout,"                 h selects the header error protection (EPB): 'type' can be\n");
        fprintf(stdout,"                   [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
-       fprintf(stdout,"                   if 'tile' is absent, it applies to main and tile headers\n");
-       fprintf(stdout,"                   if 'tile' is present, it applies from that tile\n");
-       fprintf(stdout,"                     onwards, up to the next h<tile> spec, or to the last tile\n");
+       fprintf(stdout,"                   if 'tilepart' is absent, it is for main and tile headers\n");
+       fprintf(stdout,"                   if 'tilepart' is present, it applies from that tile\n");
+       fprintf(stdout,"                     onwards, up to the next h<> spec, or to the last tilepart\n");
        fprintf(stdout,"                     in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS);
        fprintf(stdout,"\n");
        fprintf(stdout,"                 p selects the packet error protection (EEP/UEP with EPBs)\n");
        fprintf(stdout,"                  to be applied to raw data: 'type' can be\n");
        fprintf(stdout,"                   [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
-       fprintf(stdout,"                   if 'tile:pack' is absent, it starts from tile 0, packet 0\n");
-       fprintf(stdout,"                   if 'tile:pack' is present, it applies from that tile\n");
+       fprintf(stdout,"                   if 'tilepart:pack' is absent, it is from tile 0, packet 0\n");
+       fprintf(stdout,"                   if 'tilepart:pack' is present, it applies from that tile\n");
        fprintf(stdout,"                     and that packet onwards, up to the next packet spec\n");
-       fprintf(stdout,"                     or to the last packet in the last tile in the codestream\n");
+       fprintf(stdout,"                     or to the last packet in the last tilepart in the stream\n");
        fprintf(stdout,"                     (max. %d specs)\n", JPWL_MAX_NO_PACKSPECS);
        fprintf(stdout,"\n");
        fprintf(stdout,"                 s enables sensitivity data insertion (ESD): 'method' can be\n");
        fprintf(stdout,"                   [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n");
        fprintf(stdout,"                    4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n");
-       fprintf(stdout,"                   if 'tile' is absent, it applies to main header only\n");
-       fprintf(stdout,"                   if 'tile' is present, it applies from that tile\n");
-       fprintf(stdout,"                     onwards, up to the next s<tile> spec, or to the last tile\n");
+       fprintf(stdout,"                   if 'tilepart' is absent, it is for main header only\n");
+       fprintf(stdout,"                   if 'tilepart' is present, it applies from that tile\n");
+       fprintf(stdout,"                     onwards, up to the next s<> spec, or to the last tilepart\n");
        fprintf(stdout,"                     in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS);
        fprintf(stdout,"\n");
        fprintf(stdout,"                 g determines the addressing mode: <range> can be\n");
@@ -246,15 +246,16 @@ void encode_help_display() {
        fprintf(stdout,"                   1/2 bytes, for the transformed pseudo-floating point value\n");
        fprintf(stdout,"\n");
        fprintf(stdout,"                 ex.:\n");
-       fprintf(stdout," h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,s0=6,s3=-1,a=0,g=1,z=1\n");
+       fprintf(stdout,"                   h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n");
+       fprintf(stdout,"                     s0=6,s3=-1,a=0,g=1,z=1\n");
        fprintf(stdout,"                 means\n");
        fprintf(stdout,"                   predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n");
        fprintf(stdout,"                   CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n");
        fprintf(stdout,"                   UEP rs(78,32) for packets 0 to 23 of tile 0,\n");
-       fprintf(stdout,"                   UEP rs(56,32) for packets 24 to the last of tile 0,\n");
-       fprintf(stdout,"                   UEP rs default for packets of tile 1,\n");
-       fprintf(stdout,"                   no UEP for packets 0 to 19 of tile 3,\n");
-       fprintf(stdout,"                   UEP CRC-32 for packets 20 of tile 3 to last tile,\n");
+       fprintf(stdout,"                   UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n");
+       fprintf(stdout,"                   UEP rs default for packets of tilepart 1,\n");
+       fprintf(stdout,"                   no UEP for packets 0 to 19 of tilepart 3,\n");
+       fprintf(stdout,"                   UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n");
        fprintf(stdout,"                   relative sensitivity ESD for MH,\n");
        fprintf(stdout,"                   TSE ESD from TPH 0 to TPH 2, byte range with automatic\n");
        fprintf(stdout,"                   size of addresses and 1 byte for each sensitivity value\n");
@@ -561,6 +562,11 @@ int write_index_file(opj_codestream_info_t *cstr_info, char *index) {
        double total_disto = 0;
 /* UniPG>> */
        int tilepartno;
+
+#ifdef USE_JPWL
+       if (!strcmp(index, JPWL_PRIVATEINDEX_NAME))
+               return 0;
+#endif /* USE_JPWL */
 /* <<UniPG */
 
        if (!cstr_info)         
@@ -621,10 +627,11 @@ int write_index_file(opj_codestream_info_t *cstr_info, char *index) {
 
                fprintf(stream, "\nTILE %d DETAILS\n", tileno); 
 /* UniPG>> */
-               fprintf(stream, "part_nb tileno start_pos end_tph_pos   end_pos\n");
+               fprintf(stream, "part_nb tileno   pack_nb start_pos end_tph_pos   end_pos\n");
                for (tilepartno = 0; tilepartno < cstr_info->tile[tileno].num_tps; tilepartno++)
-                       fprintf(stream, "%4d %9d %9d %11d %9d\n",
+                       fprintf(stream, "%4d %9d %9d %9d %11d %9d\n",
                                tilepartno, tileno,
+                               cstr_info->tile[tileno].tp_num[tilepartno],
                                cstr_info->tile[tileno].tp_start_pos[tilepartno],
                                cstr_info->tile[tileno].tp_end_header[tilepartno],
                                cstr_info->tile[tileno].tp_end_pos[tilepartno]
@@ -1326,11 +1333,11 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                        /* Tile part header, specified */
                                                        if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
                                                                ((hprot >= 37) && (hprot <= 128)))) {
-                                                               fprintf(stderr, "ERROR -> invalid tile header protection method h = %d\n", hprot);
+                                                               fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n", hprot);
                                                                return 1;
                                                        }
                                                        if (tile < 0) {
-                                                               fprintf(stderr, "ERROR -> invalid tile number on protection method t = %d\n", tile);
+                                                               fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile);
                                                                return 1;
                                                        }
                                                        if (tilespec < JPWL_MAX_NO_TILESPECS) {
@@ -1341,7 +1348,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                } else if(sscanf(token, "h%d", &tile) == 1) {
                                                        /* Tile part header, unspecified */
                                                        if (tile < 0) {
-                                                               fprintf(stderr, "ERROR -> invalid tile number on protection method t = %d\n", tile);
+                                                               fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile);
                                                                return 1;
                                                        }
                                                        if (tilespec < JPWL_MAX_NO_TILESPECS) {
@@ -1387,7 +1394,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                                return 1;
                                                        }
                                                        if (tile < 0) {
-                                                               fprintf(stderr, "ERROR -> invalid tile number on protection method p = %d\n", tile);
+                                                               fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
                                                                return 1;
                                                        }
                                                        if (packspec < JPWL_MAX_NO_PACKSPECS) {
@@ -1404,7 +1411,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                                return 1;
                                                        }
                                                        if (tile < 0) {
-                                                               fprintf(stderr, "ERROR -> invalid tile number on protection method p = %d\n", tile);
+                                                               fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
                                                                return 1;
                                                        }
                                                        if (pack < 0) {
@@ -1425,7 +1432,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                                return 1;
                                                        }
                                                        if (tile < 0) {
-                                                               fprintf(stderr, "ERROR -> invalid tile number on protection method p = %d\n", tile);
+                                                               fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
                                                                return 1;
                                                        }
                                                        if (pack < 0) {
@@ -1441,7 +1448,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                } else if (sscanf(token, "p%d", &tile) == 1) {
                                                        /* default from a tile on */
                                                        if (tile < 0) {
-                                                               fprintf(stderr, "ERROR -> invalid tile number on protection method p = %d\n", tile);
+                                                               fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile);
                                                                return 1;
                                                        }
                                                        if (packspec < JPWL_MAX_NO_PACKSPECS) {
@@ -1482,11 +1489,11 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                } else if(sscanf(token, "s%d=%d", &tile, &sens) == 2) {
                                                        /* Tile part header, specified */
                                                        if ((sens < -1) || (sens > 7)) {
-                                                               fprintf(stderr, "ERROR -> invalid tile header sensitivity method s = %d\n", sens);
+                                                               fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n", sens);
                                                                return 1;
                                                        }
                                                        if (tile < 0) {
-                                                               fprintf(stderr, "ERROR -> invalid tile number on sensitivity method t = %d\n", tile);
+                                                               fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
                                                                return 1;
                                                        }
                                                        if (tilespec < JPWL_MAX_NO_TILESPECS) {
@@ -1497,7 +1504,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                } else if(sscanf(token, "s%d", &tile) == 1) {
                                                        /* Tile part header, unspecified */
                                                        if (tile < 0) {
-                                                               fprintf(stderr, "ERROR -> invalid tile number on sensitivity method t = %d\n", tile);
+                                                               fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
                                                                return 1;
                                                        }
                                                        if (tilespec < JPWL_MAX_NO_TILESPECS) {
index f687c529278d9917b24c87c7fc4b3d14e53ecbfe..f97331d4be7f7d62f37b66dbe66f28298fcda69f 100644 (file)
@@ -110,10 +110,10 @@ void jpwl_esd_write(jpwl_esd_ms_t *esdmark, unsigned char *buf);
 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 +121,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 +142,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
@@ -187,7 +187,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 +212,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
+       acc_tpno = 0;\r
        for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) {\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
-               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
-               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_start_pos[tpno];\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_end_header[tpno] + 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->cstr_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 = soc_pos + j2k->cstr_info->tile[tileno].tp_start_pos[tpno] + 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
@@ -341,7 +359,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 +389,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
+       acc_tpno = 0;\r
        for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_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->cstr_info->num;\r
-               jpwl_epb_ms_t *tph_epb = NULL;\r
-\r
-               sot_pos = j2k->cstr_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
-               /* a-priori length of the data dwelling between SOT and SOD */\r
-               post_sod_pos = j2k->cstr_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->num;\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
+                       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_end_header[tpno] + 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->cstr_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 = soc_pos + j2k->cstr_info->tile[tileno].tp_start_pos[tpno] + 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->cstr_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_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
+\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_start_pos[tpno] + 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->cstr_info->tile[tileno].packet[startpack].start_pos,\r
-                                               j2k->cstr_info->tile[tileno].packet[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
-\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
-                                               /* 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
-                                               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_start_pos[tpno] + 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->cstr_info->tile[tileno].packet[startpack].start_pos,\r
-                               j2k->cstr_info->tile[tileno].packet[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
-\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
-                               /* 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
-                                               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
@@ -577,7 +639,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);\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
@@ -597,11 +659,15 @@ void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) {
                opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for JPWL temp 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
 \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,9 +713,9 @@ 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), soc_pos + old_size - orig_pos);\r
+       jpwl_buf += soc_pos + old_size - orig_pos;\r
+       cio_seek(cio, soc_pos + old_size);\r
        \r
        /*\r
        update info file based on added markers\r
@@ -748,12 +814,12 @@ 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->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
index 14d5c48df866e79f2cab1d2bfe800d8b12768052..7ebe236f8811b7b5629ce5dbc3c8d11711971c37 100644 (file)
@@ -1658,7 +1658,7 @@ bool jpwl_update_info(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int jwmarker_num)
        unsigned long int addlen;
 
        opj_codestream_info_t *info = j2k->cstr_info;
-       int tileno, packno, numtiles = info->th * info->tw, numpacks = info->num;
+       int tileno, tpno, packno, numtiles = info->th * info->tw, numpacks = info->num;
 
        if (!j2k || !jwmarker ) {
                opj_event_msg(j2k->cinfo, EVT_ERROR, "J2K handle or JPWL markers list badly allocated\n");
@@ -1703,6 +1703,32 @@ bool jpwl_update_info(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int jwmarker_num)
                                addlen += jwmarker[mm].len + 2;*/
                info->tile[tileno].end_pos += addlen;
 
+               /* navigate through all the tile parts */
+               for (tpno = 0; tpno < info->tile[tileno].num_tps; tpno++) {
+
+                       /* start_pos: increment with markers before SOT */
+                       addlen = 0;
+                       for (mm = 0; mm < jwmarker_num; mm++)
+                               if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp_start_pos[tpno])
+                                       addlen += jwmarker[mm].len + 2;
+                       info->tile[tileno].tp_start_pos[tpno] += addlen;
+
+                       /* end_header: increment with markers before of it */
+                       addlen = 0;
+                       for (mm = 0; mm < jwmarker_num; mm++)
+                               if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp_end_header[tpno])
+                                       addlen += jwmarker[mm].len + 2;
+                       info->tile[tileno].tp_end_header[tpno] += addlen;
+
+                       /* end_pos: increment with markers before the end of this tile part */
+                       addlen = 0;
+                       for (mm = 0; mm < jwmarker_num; mm++)
+                               if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp_end_pos[tpno])
+                                       addlen += jwmarker[mm].len + 2;
+                       info->tile[tileno].tp_end_pos[tpno] += addlen;
+
+               }
+
                /* navigate through all the packets in this tile */
                for (packno = 0; packno < numpacks; packno++) {
                        
index 9a6d7458482a0998fbd019a4a2f9346552ee538a..8580098ab7cb0ad838e7d4ac48107c2ed8f0488f 100644 (file)
@@ -428,6 +428,7 @@ int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *
                        j2k->cstr_info->tile[tileno].tp_start_pos = (int *) opj_malloc(cur_totnum_tp * sizeof(int));
                        j2k->cstr_info->tile[tileno].tp_end_header = (int *) opj_malloc(cur_totnum_tp * sizeof(int));
                        j2k->cstr_info->tile[tileno].tp_end_pos = (int *) opj_malloc(cur_totnum_tp      * sizeof(int));
+                       j2k->cstr_info->tile[tileno].tp_num = (int *) opj_malloc(cur_totnum_tp  * sizeof(int));
                }
                /* << INDEX */
 /* <<UniPG */
@@ -1928,6 +1929,7 @@ void j2k_destroy_compress(opj_j2k_t *j2k) {
                 opj_free(tile_info->tp_start_pos);
                 opj_free(tile_info->tp_end_header);
                 opj_free(tile_info->tp_end_pos);
+                opj_free(tile_info->tp_num);
                 /* << INDEX */
 /* <<UniPG */
                        }
@@ -2221,6 +2223,10 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestre
 
        opj_tcd_t *tcd = NULL;  /* TCD component */
 
+/* UniPG>> */
+       int acc_pack_num = 0;
+/* <<UniPG */
+
        j2k->cio = cio; 
        j2k->image = image;
 
@@ -2362,9 +2368,13 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestre
                                j2k_write_sod(j2k, tcd);
 /* UniPG>> */
                                /* INDEX >> */
-                               if(cstr_info && cstr_info->index_on)
+                               if(cstr_info && cstr_info->index_on) {
                                        cstr_info->tile[j2k->curtileno].tp_end_pos[j2k->cur_tp_num] =
                                                cio_tell(cio) + j2k->pos_correction - 1;
+                                       cstr_info->tile[j2k->curtileno].tp_num[j2k->cur_tp_num] =
+                                               cstr_info->num - acc_pack_num;
+                                       acc_pack_num = cstr_info->num;
+                               }
                                /* << INDEX */
 /* <<UniPG */
                                j2k->cur_tp_num ++;
@@ -2409,7 +2419,7 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestre
 /* UniPG>> */
                /* The following adjustment is done to adjust the codestream size */
                /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
-               /* the first unch of bytes is not in the codestream               */
+               /* the first bunch of bytes is not in the codestream              */
                cstr_info->codestream_size -= cstr_info->main_head_start;
 /* <<UniPG */
        }
@@ -2419,7 +2429,7 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestre
        preparation of JPWL marker segments: can be finalized only when the whole
        codestream is known
        */
-       if(cstr_info && cstr_info->index_on && cp->epc_on) {
+       if(cp->epc_on) {
 
                /* let's begin creating a marker list, according to user wishes */
                jpwl_prepare_marks(j2k, cio, image);
@@ -2430,6 +2440,7 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestre
                /* do not know exactly what is this for,
                but it gets called during index creation */
                j2k->pos_correction = 0;
+
        }
 #endif /* USE_JPWL */
 
index 2460d92579c304cac6d8212e9d866b876e0b9935..bf92199ad8fc7e0c2daa2a84d88dff4202827a3b 100644 (file)
@@ -628,7 +628,7 @@ typedef struct opj_tile_info {
        int nbpix;
        /** add fixed_quality */
        double distotile;
-       /* UniPG>> */
+/* UniPG>> */
     /** number of tile parts */
     int num_tps;
     /** start position of tile part */
@@ -637,7 +637,9 @@ typedef struct opj_tile_info {
     int *tp_end_header;
     /** end position of tile part */
     int *tp_end_pos;
-       /* << UniPG */
+       /** number of packets of tile part */
+       int *tp_num;
+/* << UniPG */
 } opj_tile_info_t;
 
 /**