ENH: add Java j2kviewer but do not compile it using cmake since cmake has too poor...
[openjpeg.git] / libopenjpeg / j2k.c
index 7c04256ae0837267eae956ebd8afb611439ae220..9a6d7458482a0998fbd019a4a2f9346552ee538a 100644 (file)
@@ -421,6 +421,16 @@ int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *
                        pi_destroy(pi, cp, tileno);
                }
                j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
+/* UniPG>> */
+               /* INDEX >> */
+               if (j2k->cstr_info && j2k->cstr_info->index_on) {
+                       j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
+                       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));
+               }
+               /* << INDEX */
+/* <<UniPG */
        }
        return totnum_tp;
 }
@@ -1031,8 +1041,8 @@ static void j2k_read_poc(opj_j2k_t *j2k) {
                poc = &tcp->pocs[i];
                poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
                poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
-               poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
-               poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolutions);   /* REpoc_i */
+               poc->layno1 = cio_read(cio, 2);    /* LYEpoc_i */
+               poc->resno1 = cio_read(cio, 1);    /* REpoc_i */
                poc->compno1 = int_min(
                        cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */
                poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */
@@ -1340,7 +1350,7 @@ static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
        int l, layno;
        int totlen;
        opj_tcp_t *tcp = NULL;
-       opj_image_info_t *image_info = NULL;
+       opj_codestream_info_t *cstr_info = NULL;
        
        opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
        opj_cp_t *cp = j2k->cp;
@@ -1348,17 +1358,21 @@ static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
 
        tcd->tp_num = j2k->tp_num ;
        tcd->cur_tp_num = j2k->cur_tp_num;
-       tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
        
        cio_write(cio, J2K_MS_SOD, 2);
        if (j2k->curtileno == 0) {
                j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
        }
-       
+
        /* INDEX >> */
-       image_info = j2k->image_info;
-       if (image_info && image_info->index_on) {
-               image_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
+       cstr_info = j2k->cstr_info;
+       if (cstr_info && cstr_info->index_on) {
+               if (!j2k->cur_tp_num ){
+                       cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
+               }else{
+                       if(cstr_info->tile[j2k->curtileno].packet[cstr_info->num - 1].end_pos < cio_tell(cio))
+                               cstr_info->tile[j2k->curtileno].packet[cstr_info->num].start_pos = cio_tell(cio);
+               }
        }
        /* << INDEX */
        
@@ -1366,11 +1380,11 @@ static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
        for (layno = 0; layno < tcp->numlayers; layno++) {
                tcp->rates[layno] -= tcp->rates[layno] ? (j2k->sod_start / (cp->th * cp->tw)) : 0;
        }
-       if(image_info) {
-               image_info->num = 0;
+       if(cstr_info && (j2k->cur_tp_num == 0)) {
+               cstr_info->num = 0;
        }
        
-       l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, image_info);
+       l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
        
        /* Writing Psot in SOT marker */
        totlen = cio_tell(cio) + l - j2k->sot_start;
@@ -1474,24 +1488,29 @@ static void j2k_write_eoc(opj_j2k_t *j2k) {
 static void j2k_read_eoc(opj_j2k_t *j2k) {
        int i, tileno;
 
-#ifndef NO_PACKETS_DECODING  
-       opj_tcd_t *tcd = tcd_create(j2k->cinfo);
-       tcd_malloc_decode(tcd, j2k->image, j2k->cp);
-       for (i = 0; i < j2k->cp->tileno_size; i++) {
-               tileno = j2k->cp->tileno[i];
-               tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno);
-               opj_free(j2k->tile_data[tileno]);
-               j2k->tile_data[tileno] = NULL;
+       /* if packets should be decoded */
+       if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
+               opj_tcd_t *tcd = tcd_create(j2k->cinfo);
+               tcd_malloc_decode(tcd, j2k->image, j2k->cp);
+               for (i = 0; i < j2k->cp->tileno_size; i++) {
+                       tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i);
+                       tileno = j2k->cp->tileno[i];
+                       tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno);
+                       opj_free(j2k->tile_data[tileno]);
+                       j2k->tile_data[tileno] = NULL;
+                       tcd_free_decode_tile(tcd, i);
+               }
+               tcd_free_decode(tcd);
+               tcd_destroy(tcd);
        }
-       tcd_free_decode(tcd);
-       tcd_destroy(tcd);
-#else 
-       for (i = 0; i < j2k->cp->tileno_size; i++) {
-               tileno = j2k->cp->tileno[i];
-               opj_free(j2k->tile_data[tileno]);
-               j2k->tile_data[tileno] = NULL;
+       /* if packets should not be decoded  */
+       else {
+               for (i = 0; i < j2k->cp->tileno_size; i++) {
+                       tileno = j2k->cp->tileno[i];
+                       opj_free(j2k->tile_data[tileno]);
+                       j2k->tile_data[tileno] = NULL;
+               }
        }
-#endif
        
        j2k->state = J2K_STATE_MT;
 }
@@ -1532,6 +1551,10 @@ opj_dec_mstabent_t j2k_dec_mstab[] = {
   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
 #endif /* USE_JPWL */
+#ifdef USE_JPSEC
+  {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
+  {J2K_MS_INSEC, 0, j2k_read_insec},
+#endif /* USE_JPSEC */
 
   {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
 };
@@ -1807,6 +1830,7 @@ opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio) {
 
        /* create an empty image */
        image = opj_image_create0();
+       j2k->image = image;
 
        j2k->state = J2K_STATE_MHSOC;
        
@@ -1891,18 +1915,24 @@ void j2k_destroy_compress(opj_j2k_t *j2k) {
 
        if(!j2k) return;
 
-       if(j2k->image_info != NULL) {
-               opj_image_info_t *image_info = j2k->image_info;
-               if (image_info->index_on && j2k->cp) {
+       if(j2k->cstr_info != NULL) {
+               opj_codestream_info_t *cstr_info = j2k->cstr_info;
+               if (cstr_info->index_on && j2k->cp) {
                        opj_cp_t *cp = j2k->cp;
                        for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
-                               opj_tile_info_t *tile_info = &image_info->tile[tileno];
+                               opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
                                opj_free(tile_info->thresh);
                                opj_free(tile_info->packet);
+/* UniPG>> */
+                /* INDEX >> */
+                opj_free(tile_info->tp_start_pos);
+                opj_free(tile_info->tp_end_header);
+                opj_free(tile_info->tp_end_pos);
+                /* << INDEX */
+/* <<UniPG */
                        }
-                       opj_free(image_info->tile);
+                       opj_free(cstr_info->tile);
                }
-               opj_free(image_info);
        }
        if(j2k->cp != NULL) {
                opj_cp_t *cp = j2k->cp;
@@ -1960,9 +1990,6 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_
 
        /* creation of an index file ? */
        cp->index_on = parameters->index_on;
-       if(cp->index_on) {
-               j2k->image_info = (opj_image_info_t*)opj_malloc(sizeof(opj_image_info_t));
-       }
 
        /* tiles */
        cp->tdx = parameters->cp_tdx;
@@ -2083,7 +2110,7 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_
                }
                tcp->csty = parameters->csty;
                tcp->prg = parameters->prog_order;
-               tcp->mct = image->numcomps == 3 ? 1 : 0;
+               tcp->mct = parameters->tcp_mct; 
 
                numpocs_tile = 0;
                tcp->POC = 0;
@@ -2188,244 +2215,8 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_
        }
 }
 
-/**
-Create an index file
-@param j2k
-@param cio
-@param image_info
-@param index Index filename
-@return Returns 1 if successful, returns 0 otherwise
-*/
-static int j2k_create_index(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_info_t *image_info, char *index) {
-       int tileno, compno, layno, resno, precno, pack_nb, x, y;
-       FILE *stream = NULL;
-       double total_disto = 0;
-
-       image_info->codestream_size = cio_tell(cio) + j2k->pos_correction;      /* Correction 14/4/03 suite rmq de Patrick */
-
-
-#ifdef USE_JPWL
-       /* if JPWL is enabled and the name coincides with our own set
-          then discard the creation of the file: this was just done to
-          enable indexing, we do not want an index file
-       */
-       if (j2k->cp->epc_on && !strcmp(index, JPWL_PRIVATEINDEX_NAME))
-               return 1;
-#endif /* USE_JPWL */
-
-
-       stream = fopen(index, "w");
-       if (!stream) {
-               opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
-               return 0;
-       }
-       
-       fprintf(stream, "%d %d\n", image_info->image_w, image_info->image_h);
-       fprintf(stream, "%d\n", image_info->prog);
-       fprintf(stream, "%d %d\n", image_info->tile_x, image_info->tile_y);
-       fprintf(stream, "%d %d\n", image_info->tw, image_info->th);
-       fprintf(stream, "%d\n", image_info->comp);
-       fprintf(stream, "%d\n", image_info->layer);
-       fprintf(stream, "%d\n", image_info->decomposition);
-       
-       for (resno = image_info->decomposition; resno >= 0; resno--) {
-               fprintf(stream, "[%d,%d] ", 
-                       (1 << image_info->tile[0].pdx[resno]), (1 << image_info->tile[0].pdx[resno]));  /* based on tile 0 */
-       }
-       fprintf(stream, "\n");
-       fprintf(stream, "%d\n", image_info->main_head_end);
-       fprintf(stream, "%d\n", image_info->codestream_size);
-       
-       for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
-               fprintf(stream, "%4d %9d %9d %9d %9e %9d %9e\n",
-                       image_info->tile[tileno].num_tile,
-                       image_info->tile[tileno].start_pos,
-                       image_info->tile[tileno].end_header,
-                       image_info->tile[tileno].end_pos,
-                       image_info->tile[tileno].distotile, image_info->tile[tileno].nbpix,
-                       image_info->tile[tileno].distotile / image_info->tile[tileno].nbpix);
-       }
-       
-       for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
-               int start_pos, end_pos;
-               double disto = 0;
-               pack_nb = 0;
-               
-               /*
-               fprintf(stream, "pkno tileno layerno resno compno precno start_pos   end_pos       deltaSE        \n");
-               */
-               
-               if (image_info->prog == LRCP) { /* LRCP */
-                       /*
-                       fprintf(stream, "pack_nb tileno layno resno compno precno start_pos  end_pos   disto");
-                       */
-                       for (layno = 0; layno < image_info->layer; layno++) {
-                               for (resno = 0; resno < image_info->decomposition + 1; resno++) {
-                                       for (compno = 0; compno < image_info->comp; compno++) {
-                                               int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
-                                               for (precno = 0; precno < prec_max; precno++) {
-                                                       start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
-                                                       end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
-                                                       disto = image_info->tile[tileno].packet[pack_nb].disto;
-                                                       fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
-                                                               pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
-                                                       total_disto += disto;
-                                                       pack_nb++;
-                                               }
-                                       }
-                               }
-                       }
-               } /* LRCP */
-               else if (image_info->prog == RLCP) {    /* RLCP */
-                       /*
-                       fprintf(stream, "pack_nb tileno resno layno compno precno start_pos  end_pos   disto");
-                       */
-                       for (resno = 0; resno < image_info->decomposition + 1; resno++) {
-                               for (layno = 0; layno < image_info->layer; layno++) {
-                                       for (compno = 0; compno < image_info->comp; compno++) {
-                                               int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
-                                               for (precno = 0; precno < prec_max; precno++) {
-                                                       start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
-                                                       end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
-                                                       disto = image_info->tile[tileno].packet[pack_nb].disto;
-                                                       fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
-                                                               pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
-                                                       total_disto += disto;
-                                                       pack_nb++;
-                                               }
-                                       }
-                               }
-                       }
-               } /* RLCP */
-               else if (image_info->prog == RPCL) {    /* RPCL */
-                       /*
-                       fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos  end_pos   disto\n"); 
-                       */
-                       for (resno = 0; resno < image_info->decomposition + 1; resno++) {
-                               /* I suppose components have same XRsiz, YRsiz */
-                               int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
-                               int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
-                               int x1 = x0 + image_info->tile_x;
-                               int y1 = y0 + image_info->tile_y;
-                               for(y = y0; y < y1; y++) {
-                                       for(x = x0; x < x1; x++) {
-                                               for (compno = 0; compno < image_info->comp; compno++) {
-                                                       int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
-                                                       for (precno = 0; precno < prec_max; precno++) {
-                                                               int pcnx = image_info->tile[tileno].pw[resno];
-                                                               int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
-                                                               int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
-                                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
-                                                               int precno_y = (int) floor( (float)precno/(float)pcnx );
-                                                               if (precno_y*pcy == y ) {
-                                                                       if (precno_x*pcx == x ) {
-                                                                               for (layno = 0; layno < image_info->layer; layno++) {
-                                                                                       start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
-                                                                                       end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
-                                                                                       disto = image_info->tile[tileno].packet[pack_nb].disto;
-                                                                                       fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
-                                                                                               pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto); 
-                                                                                       total_disto += disto;
-                                                                                       pack_nb++; 
-                                                                               }
-                                                                       }
-                                                               }
-                                                       } /* precno */
-                                               } /* compno */
-                                       } /* x = x0..x1 */
-                               } /* y = y0..y1 */
-                       } /* resno */
-               } /* RPCL */
-               else if (image_info->prog == PCRL) {    /* PCRL */
-                       /* I suppose components have same XRsiz, YRsiz */
-                       int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
-                       int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
-                       int x1 = x0 + image_info->tile_x;
-                       int y1 = y0 + image_info->tile_y;
-                       /*
-                       fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos  end_pos   disto\n"); 
-                       */
-                       for(y = y0; y < y1; y++) {
-                               for(x = x0; x < x1; x++) {
-                                       for (compno = 0; compno < image_info->comp; compno++) {
-                                               for (resno = 0; resno < image_info->decomposition + 1; resno++) {
-                                                       int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
-                                                       for (precno = 0; precno < prec_max; precno++) {
-                                                               int pcnx = image_info->tile[tileno].pw[resno];
-                                                               int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
-                                                               int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
-                                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
-                                                               int precno_y = (int) floor( (float)precno/(float)pcnx );
-                                                               if (precno_y*pcy == y ) {
-                                                                       if (precno_x*pcx == x ) {
-                                                                               for (layno = 0; layno < image_info->layer; layno++) {
-                                                                                       start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
-                                                                                       end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
-                                                                                       disto = image_info->tile[tileno].packet[pack_nb].disto;
-                                                                                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
-                                                                                               pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto); 
-                                                                                       total_disto += disto;
-                                                                                       pack_nb++; 
-                                                                               }
-                                                                       }
-                                                               }
-                                                       } /* precno */
-                                               } /* resno */
-                                       } /* compno */
-                               } /* x = x0..x1 */
-                       } /* y = y0..y1 */
-               } /* PCRL */
-               else {  /* CPRL */
-                       /*
-                       fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos  end_pos   disto\n"); 
-                       */
-                       for (compno = 0; compno < image_info->comp; compno++) {
-                               /* I suppose components have same XRsiz, YRsiz */
-                               int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
-                               int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
-                               int x1 = x0 + image_info->tile_x;
-                               int y1 = y0 + image_info->tile_y;
-                               for(y = y0; y < y1; y++) {
-                                       for(x = x0; x < x1; x++) {
-                                               for (resno = 0; resno < image_info->decomposition + 1; resno++) {
-                                                       int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
-                                                       for (precno = 0; precno < prec_max; precno++) {
-                                                               int pcnx = image_info->tile[tileno].pw[resno];
-                                                               int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
-                                                               int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
-                                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
-                                                               int precno_y = (int) floor( (float)precno/(float)pcnx );
-                                                               if (precno_y*pcy == y ) {
-                                                                       if (precno_x*pcx == x ) {
-                                                                               for (layno = 0; layno < image_info->layer; layno++) {
-                                                                                       start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
-                                                                                       end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
-                                                                                       disto = image_info->tile[tileno].packet[pack_nb].disto;
-                                                                                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
-                                                                                               pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto); 
-                                                                                       total_disto += disto;
-                                                                                       pack_nb++; 
-                                                                               }
-                                                                       }
-                                                               }
-                                                       } /* precno */
-                                               } /* resno */
-                                       } /* x = x0..x1 */
-                               } /* y = y0..y1 */
-                       } /* comno */
-               } /* CPRL */   
-       } /* tileno */
-       
-       fprintf(stream, "%8e\n", image_info->D_max); /* SE max */
-       fprintf(stream, "%.8e\n", total_disto); /* SE totale */
-       fclose(stream);
-
-       return 1;
-}
-
-bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index) {
+bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
        int tileno, compno;
-       opj_image_info_t *image_info = NULL;
        opj_cp_t *cp = NULL;
 
        opj_tcd_t *tcd = NULL;  /* TCD component */
@@ -2438,23 +2229,29 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index)
        /* j2k_dump_cp(stdout, image, cp); */
 
        /* INDEX >> */
-       image_info = j2k->image_info;
-       if (image_info && cp->index_on) {
-               image_info->index_on = cp->index_on;
-               image_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
-               image_info->image_w = image->x1 - image->x0;
-               image_info->image_h = image->y1 - image->y0;
-               image_info->prog = (&cp->tcps[0])->prg;
-               image_info->tw = cp->tw;
-               image_info->th = cp->th;
-               image_info->tile_x = cp->tdx;   /* new version parser */
-               image_info->tile_y = cp->tdy;   /* new version parser */
-               image_info->tile_Ox = cp->tx0;  /* new version parser */
-               image_info->tile_Oy = cp->ty0;  /* new version parser */
-               image_info->comp = image->numcomps;
-               image_info->layer = (&cp->tcps[0])->numlayers;
-               image_info->decomposition = (&cp->tcps[0])->tccps->numresolutions - 1;
-               image_info->D_max = 0;          /* ADD Marcela */
+       j2k->cstr_info = cstr_info;
+       if (cstr_info && cp->index_on) {
+               cstr_info->index_on = cp->index_on;
+               cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
+               cstr_info->image_w = image->x1 - image->x0;
+               cstr_info->image_h = image->y1 - image->y0;
+               cstr_info->prog = (&cp->tcps[0])->prg;
+               cstr_info->tw = cp->tw;
+               cstr_info->th = cp->th;
+               cstr_info->tile_x = cp->tdx;    /* new version parser */
+               cstr_info->tile_y = cp->tdy;    /* new version parser */
+               cstr_info->tile_Ox = cp->tx0;   /* new version parser */
+               cstr_info->tile_Oy = cp->ty0;   /* new version parser */
+               cstr_info->comp = image->numcomps;
+               cstr_info->layer = (&cp->tcps[0])->numlayers;
+               cstr_info->decomposition = (&cp->tcps[0])->tccps->numresolutions - 1;
+               cstr_info->D_max = 0;           /* ADD Marcela */
+/* UniPG>> */
+               cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
+/* <<UniPG */
+       }
+       else if (cstr_info) {
+               cstr_info->index_on = 0;
        }
        /* << INDEX */
        
@@ -2478,27 +2275,30 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index)
        if (cp->comment != NULL) {
                j2k_write_com(j2k);
        }
-       /* INDEX >> */
-       if(image_info && image_info->index_on) {
-               image_info->main_head_end = cio_tell(cio) - 1;
-       }
-       /* << INDEX */
-
+       
        j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
-       /*       TLM Marker*/
+       /* TLM Marker*/
        if(cp->cinema){
                j2k_write_tlm(j2k);
                if (cp->cinema == CINEMA4K_24) {
                        j2k_write_poc(j2k);
                }
        }
+
+       /* uncomment only for testing JPSEC marker writing */
+       /* j2k_write_sec(j2k); */
+
+       /* INDEX >> */
+       if(cstr_info && cstr_info->index_on) {
+               cstr_info->main_head_end = cio_tell(cio) - 1;
+       }
+       /* << INDEX */
        /**** Main Header ENDS here ***/
        
        /* create the tile encoder */
        tcd = tcd_create(j2k->cinfo);
 
        /* encode each tile */
-
        for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
                int pino;
                int tilepartno=0;
@@ -2508,7 +2308,7 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index)
                
                j2k->curtileno = tileno;
                j2k->cur_tp_num = 0;
-
+               tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
                /* initialisation before tile encoding  */
                if (tileno == 0) {
                        tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
@@ -2517,9 +2317,9 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index)
                }
                
                /* INDEX >> */
-               if(image_info && image_info->index_on) {
-                       image_info->tile[j2k->curtileno].num_tile = j2k->curtileno;
-                       image_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
+               if(cstr_info && cstr_info->index_on) {
+                       cstr_info->tile[j2k->curtileno].num_tile = j2k->curtileno;
+                       cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
                }
                /* << INDEX */
 
@@ -2533,6 +2333,13 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index)
 
                        for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
                                j2k->tp_num = tilepartno;
+/* UniPG>> */
+                               /* INDEX >> */
+                               if(cstr_info && cstr_info->index_on)
+                                       cstr_info->tile[j2k->curtileno].tp_start_pos[j2k->cur_tp_num] =
+                                               cio_tell(cio) + j2k->pos_correction;
+                               /* << INDEX */
+/* <<UniPG */                          
                                j2k_write_sot(j2k);
 
                                if(j2k->cur_tp_num == 0 && cp->cinema == 0){
@@ -2545,14 +2352,28 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index)
                                        }
                                }
 
+/* UniPG>> */
+                               /* INDEX >> */
+                               if(cstr_info && cstr_info->index_on)
+                                       cstr_info->tile[j2k->curtileno].tp_end_header[j2k->cur_tp_num] =
+                                               cio_tell(cio) + j2k->pos_correction + 1;
+                               /* << INDEX */
+/* <<UniPG */
                                j2k_write_sod(j2k, tcd);
+/* UniPG>> */
+                               /* INDEX >> */
+                               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;
+                               /* << INDEX */
+/* <<UniPG */
                                j2k->cur_tp_num ++;
                        }
                        
                }
                /* INDEX >> */
-               if(image_info && image_info->index_on) {
-                       image_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
+               if(cstr_info && cstr_info->index_on) {
+                       cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
                }
                /* << INDEX */
                
@@ -2579,23 +2400,26 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index)
        tcd_free_encode(tcd);
        tcd_destroy(tcd);
 
+       free(j2k->cur_totnum_tp);
+
        j2k_write_eoc(j2k);
-       
-       /* Creation of the index file */
-       if(image_info && image_info->index_on) {
-               if(!j2k_create_index(j2k, cio, image_info, index)) {
-                       opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
-                       return false;
-               }
-       }
 
+       if(cstr_info && cstr_info->index_on) {
+               cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
+/* 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               */
+               cstr_info->codestream_size -= cstr_info->main_head_start;
+/* <<UniPG */
+       }
 
 #ifdef USE_JPWL
        /*
        preparation of JPWL marker segments: can be finalized only when the whole
        codestream is known
        */
-       if(image_info && image_info->index_on && cp->epc_on) {
+       if(cstr_info && cstr_info->index_on && cp->epc_on) {
 
                /* let's begin creating a marker list, according to user wishes */
                jpwl_prepare_marks(j2k, cio, image);
@@ -2606,22 +2430,11 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index)
                /* do not know exactly what is this for,
                but it gets called during index creation */
                j2k->pos_correction = 0;
-
-               /* Re-creation of the index file, with updated info */
-               if(image_info && image_info->index_on) {
-                       if(!j2k_create_index(j2k, cio, image_info, index)) {
-                               opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to re-create index file %s\n", index);
-                               return false;
-                       }
-               }
-
-               /* now we finalize the marker contents */
-               /*jpwl_finalize_marks(j2k, cio, image);*/
-
        }
 #endif /* USE_JPWL */
 
-       
        return true;
 }
 
+
+