+
+void j2k_destroy_compress(opj_j2k_t *j2k) {
+ int tileno;
+
+ if(!j2k) return;
+
+ if(j2k->image_info != NULL) {
+ opj_image_info_t *image_info = j2k->image_info;
+ if (image_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_free(tile_info->thresh);
+ opj_free(tile_info->packet);
+ }
+ opj_free(image_info->tile);
+ }
+ opj_free(image_info);
+ }
+ if(j2k->cp != NULL) {
+ opj_cp_t *cp = j2k->cp;
+
+ if(cp->comment) {
+ opj_free(cp->comment);
+ }
+ if(cp->matrice) {
+ opj_free(cp->matrice);
+ }
+ for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
+ opj_free(cp->tcps[tileno].tccps);
+ }
+ opj_free(cp->tcps);
+ opj_free(cp);
+ }
+
+ opj_free(j2k);
+}
+
+void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
+ int i, j, tileno, numpocs_tile;
+ opj_cp_t *cp = NULL;
+
+ if(!j2k || !parameters || ! image) {
+ return;
+ }
+
+ /* create and initialize the coding parameters structure */
+ cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
+
+ /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
+ j2k->cp = cp;
+
+ /* set default values for cp */
+ cp->tw = 1;
+ cp->th = 1;
+
+ /*
+ copy user encoding parameters
+ */
+
+ cp->disto_alloc = parameters->cp_disto_alloc;
+ cp->fixed_alloc = parameters->cp_fixed_alloc;
+ cp->fixed_quality = parameters->cp_fixed_quality;
+
+ /* mod fixed_quality */
+ if(parameters->cp_matrice) {
+ size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
+ cp->matrice = (int *) opj_malloc(array_size);
+ memcpy(cp->matrice, parameters->cp_matrice, array_size);
+ }
+
+ /* creation of an index file ? */
+ cp->index_on = parameters->index_on;
+ if(cp->index_on) {
+ j2k->image_info = (opj_image_info_t*)opj_malloc(sizeof(opj_image_info_t));
+ }
+
+ /* tiles */
+ cp->tdx = parameters->cp_tdx;
+ cp->tdy = parameters->cp_tdy;
+
+ /* tile offset */
+ cp->tx0 = parameters->cp_tx0;
+ cp->ty0 = parameters->cp_ty0;
+
+ /* comment string */
+ if(parameters->cp_comment) {
+ cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
+ if(cp->comment) {
+ strcpy(cp->comment, parameters->cp_comment);
+ }
+ }
+
+ /*
+ calculate other encoding parameters
+ */
+
+ if (parameters->tile_size_on) {
+ cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
+ cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
+ } else {
+ cp->tdx = image->x1 - cp->tx0;
+ cp->tdy = image->y1 - cp->ty0;
+ }
+
+/* UniPG>> */
+#ifdef USE_JPWL
+ /*
+ calculate JPWL encoding parameters
+ */
+
+ if (parameters->jpwl_epc_on) {
+ int i;
+
+ /* set JPWL on */
+ cp->epc_on = true;
+ cp->info_on = false; /* no informative technique */
+
+ /* set EPB on */
+ if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
+ cp->epb_on = true;
+
+ cp->hprot_MH = parameters->jpwl_hprot_MH;
+ for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
+ cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
+ cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
+ }
+ /* if tile specs are not specified, copy MH specs */
+ if (cp->hprot_TPH[0] == -1) {
+ cp->hprot_TPH_tileno[0] = 0;
+ cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
+ }
+ for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
+ cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
+ cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
+ cp->pprot[i] = parameters->jpwl_pprot[i];
+ }
+ }
+
+ /* set ESD writing */
+ if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
+ cp->esd_on = true;
+
+ cp->sens_size = parameters->jpwl_sens_size;
+ cp->sens_addr = parameters->jpwl_sens_addr;
+ cp->sens_range = parameters->jpwl_sens_range;
+
+ cp->sens_MH = parameters->jpwl_sens_MH;
+ for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
+ cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
+ cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
+ }
+ }
+
+ /* always set RED writing to false: we are at the encoder */
+ cp->red_on = false;
+
+ } else {
+ cp->epc_on = false;
+ }
+#endif /* USE_JPWL */
+/* <<UniPG */
+
+ /* initialize the mutiple tiles */
+ /* ---------------------------- */
+ cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
+
+ for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
+ opj_tcp_t *tcp = &cp->tcps[tileno];
+ tcp->numlayers = parameters->tcp_numlayers;
+ for (j = 0; j < tcp->numlayers; j++) {
+ if (cp->fixed_quality) { /* add fixed_quality */
+ tcp->distoratio[j] = parameters->tcp_distoratio[j];
+ } else {
+ tcp->rates[j] = parameters->tcp_rates[j];
+ }
+ }
+ tcp->csty = parameters->csty;
+ tcp->prg = parameters->prog_order;
+ tcp->mct = image->numcomps == 3 ? 1 : 0;
+
+ numpocs_tile = 0;
+ tcp->POC = 0;
+ if (parameters->numpocs) {
+ /* initialisation of POC */
+ tcp->POC = 1;
+ for (i = 0; i < parameters->numpocs; i++) {
+ if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
+ opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
+ tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
+ tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
+ tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
+ tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
+ tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
+ tcp_poc->prg = parameters->POC[numpocs_tile].prg;
+ tcp_poc->tile = parameters->POC[numpocs_tile].tile;
+ numpocs_tile++;
+ }
+ }
+ }
+ tcp->numpocs = numpocs_tile;
+
+ tcp->tccps = (opj_tccp_t *) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
+
+ for (i = 0; i < image->numcomps; i++) {
+ opj_tccp_t *tccp = &tcp->tccps[i];
+ tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
+ tccp->numresolutions = parameters->numresolution;
+ tccp->cblkw = int_floorlog2(parameters->cblockw_init);
+ tccp->cblkh = int_floorlog2(parameters->cblockh_init);
+ tccp->cblksty = parameters->mode;
+ tccp->qmfbid = parameters->irreversible ? 0 : 1;
+ tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
+ tccp->numgbits = 2;
+ if (i == parameters->roi_compno) {
+ tccp->roishift = parameters->roi_shift;
+ } else {
+ tccp->roishift = 0;
+ }
+ if (parameters->csty & J2K_CCP_CSTY_PRT) {
+ int p = 0;
+ for (j = tccp->numresolutions - 1; j >= 0; j--) {
+ if (p < parameters->res_spec) {
+ if (parameters->prcw_init[p] < 1) {
+ tccp->prcw[j] = 1;
+ } else {
+ tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
+ }
+ if (parameters->prch_init[p] < 1) {
+ tccp->prch[j] = 1;
+ } else {
+ tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
+ }
+ } else {
+ int res_spec = parameters->res_spec;
+ int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
+ int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
+ if (size_prcw < 1) {
+ tccp->prcw[j] = 1;
+ } else {
+ tccp->prcw[j] = int_floorlog2(size_prcw);
+ }
+ if (size_prch < 1) {
+ tccp->prch[j] = 1;
+ } else {
+ tccp->prch[j] = int_floorlog2(size_prch);
+ }
+ }
+ p++;
+ /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
+ }
+ } else {
+ for (j = 0; j < tccp->numresolutions; j++) {
+ tccp->prcw[j] = 15;
+ tccp->prch[j] = 15;
+ }
+ }
+
+ dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
+ }
+ }
+}
+
+/**
+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 */
+
+/* UniPG>> */
+#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 */
+/* <<UniPG */
+
+ 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) {
+ int tileno, compno;
+ opj_image_info_t *image_info = NULL;
+ opj_cp_t *cp = NULL;
+
+ opj_tcd_t *tcd = NULL; /* TCD component */
+
+ j2k->cio = cio;
+ j2k->image = image;
+
+ cp = j2k->cp;
+
+ /* 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 */
+ }
+ /* << INDEX */
+
+ j2k_write_soc(j2k);
+ j2k_write_siz(j2k);
+/* UniPG>> */
+#ifdef USE_JPWL
+ /** THIS CODE IS NOT USED */
+ //if(image_info && image_info->index_on && cp->epc_on)
+ // j2k_write_epc(j2k);
+#endif /* USE_JPWL */
+/* <<UniPG */
+ j2k_write_cod(j2k);
+ j2k_write_qcd(j2k);
+ for (compno = 0; compno < image->numcomps; compno++) {
+ opj_tcp_t *tcp = &cp->tcps[0];
+ if (tcp->tccps[compno].roishift)
+ j2k_write_rgn(j2k, compno, 0);
+ }
+ 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 */
+
+ /* create the tile encoder */
+ tcd = tcd_create(j2k->cinfo);
+
+ /* encode each tile */
+
+ for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
+ opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
+
+ j2k->curtileno = tileno;
+
+ /* initialisation before tile encoding */
+ if (tileno == 0) {
+ tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
+ } else {
+ tcd_init_encode(tcd, image, cp, j2k->curtileno);
+ }
+
+ /* 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;
+ }
+ /* << INDEX */
+
+ j2k_write_sot(j2k);
+
+ for (compno = 1; compno < image->numcomps; compno++) {
+ j2k_write_coc(j2k, compno);
+ j2k_write_qcc(j2k, compno);
+ }
+ if (cp->tcps[tileno].numpocs) {
+ j2k_write_poc(j2k);
+ }
+
+ j2k_write_sod(j2k, tcd);
+
+ /* INDEX >> */
+ if(image_info && image_info->index_on) {
+ image_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
+ }
+ /* << INDEX */
+
+
+ /*
+ if (tile->PPT) { // BAD PPT !!!
+ FILE *PPT_file;
+ int i;
+ PPT_file=fopen("PPT","rb");
+ fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
+ for (i=0;i<tile->len_ppt;i++) {
+ unsigned char elmt;
+ fread(&elmt, 1, 1, PPT_file);
+ fwrite(&elmt,1,1,f);
+ }
+ fclose(PPT_file);
+ unlink("PPT");
+ }
+ */
+
+ }
+
+ /* destroy the tile encoder */
+ tcd_free_encode(tcd);
+ tcd_destroy(tcd);
+
+ 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;
+ }
+ }
+
+/* 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) {
+
+ /* let's begin creating a marker list, according to user wishes */
+ jpwl_prepare_marks(j2k, cio, image);
+
+ /* now we dump the JPWL markers on the codestream */
+ jpwl_dump_marks(j2k, cio, image);
+
+ /* 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 */
+/* <<UniPG */
+
+ return true;
+}
+