opj_image_t *oimage = NULL;\r
opj_image_cmptparm_t *cmptparm; \r
opj_cio_t *cio = NULL;\r
+ opj_codestream_info_t cstr_info;\r
int codestream_length;\r
bool bSuccess;\r
int i;\r
opj_set_default_encoder_parameters(¶meters);\r
\r
/* load parameters */\r
+ parameters.cp_cinema = OFF;\r
\r
/* subsampling */\r
if (sscanf(m_subsampling.c_str(), wxT("%d,%d"), &(parameters.subsampling_dx), &(parameters.subsampling_dy)) != 2) {\r
cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);\r
\r
/* encode the image */\r
- bSuccess = opj_encode(cinfo, cio, oimage, parameters.index);\r
+ bSuccess = opj_encode(cinfo, cio, oimage, &cstr_info);\r
if (!bSuccess) {\r
\r
opj_cio_close(cio);\r
/* close and free the byte stream */\r
opj_cio_close(cio);\r
\r
+ /* Write the index to disk */\r
+ if (parameters.index_on) {\r
+ bSuccess = write_index_file(&cstr_info, parameters.index);\r
+ if (bSuccess) {\r
+ wxLogError(wxT("Failed to output index file"));\r
+ }\r
+ }\r
+\r
/* free remaining compression structures */\r
opj_destroy_compress(cinfo);\r
\r
return hdr[0] == 0xFF && hdr[1] == 0x4F;\r
}\r
\r
+/**\r
+Create an index and write it to a file\r
+@param cstr_info Codestream information \r
+@param index Index filename\r
+@return Returns 0 if successful, returns 1 otherwise\r
+*/\r
+bool wxJ2KHandler::write_index_file(opj_codestream_info_t *cstr_info, char *index) {\r
+ int tileno, compno, layno, resno, precno, pack_nb, x, y;\r
+ FILE *stream = NULL;\r
+ double total_disto = 0;\r
+\r
+ if (!cstr_info) \r
+ return false;\r
+\r
+ stream = fopen(index, "w");\r
+ if (!stream) {\r
+ wxLogError(wxT("failed to open index file [%s] for writing"), index);\r
+ return false;\r
+ }\r
+ \r
+ fprintf(stream, "%d %d\n", cstr_info->image_w, cstr_info->image_h);\r
+ fprintf(stream, "%d\n", cstr_info->prog);\r
+ fprintf(stream, "%d %d\n", cstr_info->tile_x, cstr_info->tile_y);\r
+ fprintf(stream, "%d %d\n", cstr_info->tw, cstr_info->th);\r
+ fprintf(stream, "%d\n", cstr_info->comp);\r
+ fprintf(stream, "%d\n", cstr_info->layer);\r
+ fprintf(stream, "%d\n", cstr_info->decomposition);\r
+ \r
+ for (resno = cstr_info->decomposition; resno >= 0; resno--) {\r
+ fprintf(stream, "[%d,%d] ", \r
+ (1 << cstr_info->tile[0].pdx[resno]), (1 << cstr_info->tile[0].pdx[resno])); /* based on tile 0 */\r
+ }\r
+ fprintf(stream, "\n");\r
+ fprintf(stream, "%d\n", cstr_info->main_head_end);\r
+ fprintf(stream, "%d\n", cstr_info->codestream_size);\r
+ \r
+ fprintf(stream, "\nINFO ON TILES\n");\r
+ fprintf(stream, "tileno start_pos end_hd end_tile disto nbpix disto/nbpix\n");\r
+ for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {\r
+ fprintf(stream, "%4d %9d %9d %9d %9e %9d %9e\n",\r
+ cstr_info->tile[tileno].num_tile,\r
+ cstr_info->tile[tileno].start_pos,\r
+ cstr_info->tile[tileno].end_header,\r
+ cstr_info->tile[tileno].end_pos,\r
+ cstr_info->tile[tileno].distotile, cstr_info->tile[tileno].nbpix,\r
+ cstr_info->tile[tileno].distotile / cstr_info->tile[tileno].nbpix);\r
+ }\r
+ \r
+ for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {\r
+ int start_pos, end_ph_pos, end_pos;\r
+ double disto = 0;\r
+ pack_nb = 0;\r
+\r
+ fprintf(stream, "\nTILE %d DETAILS\n", tileno); \r
+ if (cstr_info->prog == LRCP) { /* LRCP */\r
+\r
+ fprintf(stream, "LRCP\npack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n");\r
+\r
+ for (layno = 0; layno < cstr_info->layer; layno++) {\r
+ for (resno = 0; resno < cstr_info->decomposition + 1; resno++) {\r
+ for (compno = 0; compno < cstr_info->comp; compno++) {\r
+ int prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
+ for (precno = 0; precno < prec_max; precno++) {\r
+ start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;\r
+ end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;\r
+ end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;\r
+ disto = cstr_info->tile[tileno].packet[pack_nb].disto;\r
+ fprintf(stream, "%4d %6d %7d %5d %6d %6d %6d %6d %7d %8e\n",\r
+ pack_nb, tileno, layno, resno, compno, precno, start_pos, end_ph_pos, end_pos, disto);\r
+ total_disto += disto;\r
+ pack_nb++;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ } /* LRCP */\r
+ else if (cstr_info->prog == RLCP) { /* RLCP */\r
+\r
+ fprintf(stream, "RLCP\npack_nb tileno resno layno compno precno start_pos end_ph_pos end_pos disto\n");\r
+\r
+ for (resno = 0; resno < cstr_info->decomposition + 1; resno++) {\r
+ for (layno = 0; layno < cstr_info->layer; layno++) {\r
+ for (compno = 0; compno < cstr_info->comp; compno++) {\r
+ int prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
+ for (precno = 0; precno < prec_max; precno++) {\r
+ start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;\r
+ end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;\r
+ end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;\r
+ disto = cstr_info->tile[tileno].packet[pack_nb].disto;\r
+ fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %7d %8e\n",\r
+ pack_nb, tileno, resno, layno, compno, precno, start_pos, end_ph_pos, end_pos, disto);\r
+ total_disto += disto;\r
+ pack_nb++;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ } /* RLCP */\r
+ else if (cstr_info->prog == RPCL) { /* RPCL */\r
+\r
+ fprintf(stream, "RPCL\npack_nb tileno resno precno compno layno start_pos end_ph_pos end_pos disto\n"); \r
+\r
+ for (resno = 0; resno < cstr_info->decomposition + 1; resno++) {\r
+ /* I suppose components have same XRsiz, YRsiz */\r
+ int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;\r
+ int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;\r
+ int x1 = x0 + cstr_info->tile_x;\r
+ int y1 = y0 + cstr_info->tile_y;\r
+ for (compno = 0; compno < cstr_info->comp; compno++) {\r
+ int prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
+ for (precno = 0; precno < prec_max; precno++) {\r
+ int pcnx = cstr_info->tile[tileno].pw[resno];\r
+ int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->decomposition - resno );\r
+ int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->decomposition - resno );\r
+ int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;\r
+ int precno_y = (int) floor( (float)precno/(float)pcnx );\r
+ for(y = y0; y < y1; y++) { \r
+ if (precno_y*pcy == y ) {\r
+ for (x = x0; x < x1; x++) { \r
+ if (precno_x*pcx == x ) {\r
+ for (layno = 0; layno < cstr_info->layer; layno++) {\r
+ start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;\r
+ end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;\r
+ end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;\r
+ disto = cstr_info->tile[tileno].packet[pack_nb].disto;\r
+ fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %7d %8e\n",\r
+ pack_nb, tileno, resno, precno, compno, layno, start_pos, end_ph_pos, end_pos, disto); \r
+ total_disto += disto;\r
+ pack_nb++; \r
+ }\r
+ }\r
+ }/* x = x0..x1 */\r
+ } \r
+ } /* y = y0..y1 */\r
+ } /* precno */\r
+ } /* compno */\r
+ } /* resno */\r
+ } /* RPCL */\r
+ else if (cstr_info->prog == PCRL) { /* PCRL */\r
+ /* I suppose components have same XRsiz, YRsiz */\r
+ int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;\r
+ int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;\r
+ int x1 = x0 + cstr_info->tile_x;\r
+ int y1 = y0 + cstr_info->tile_y;\r
+\r
+ fprintf(stream, "PCRL\npack_nb tileno precno compno resno layno start_pos end_ph_pos end_pos disto\n"); \r
+\r
+ for (compno = 0; compno < cstr_info->comp; compno++) {\r
+ for (resno = 0; resno < cstr_info->decomposition + 1; resno++) {\r
+ int prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
+ for (precno = 0; precno < prec_max; precno++) {\r
+ int pcnx = cstr_info->tile[tileno].pw[resno];\r
+ int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->decomposition - resno );\r
+ int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->decomposition - resno );\r
+ int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;\r
+ int precno_y = (int) floor( (float)precno/(float)pcnx );\r
+ for(y = y0; y < y1; y++) { \r
+ if (precno_y*pcy == y ) {\r
+ for (x = x0; x < x1; x++) { \r
+ if (precno_x*pcx == x ) {\r
+ for (layno = 0; layno < cstr_info->layer; layno++) {\r
+ start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;\r
+ end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;\r
+ end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;\r
+ disto = cstr_info->tile[tileno].packet[pack_nb].disto;\r
+ fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %7d %8e\n",\r
+ pack_nb, tileno, precno, compno, resno, layno, start_pos, end_ph_pos, end_pos, disto); \r
+ total_disto += disto;\r
+ pack_nb++; \r
+ }\r
+ }\r
+ }/* x = x0..x1 */\r
+ } \r
+ } /* y = y0..y1 */\r
+ } /* precno */\r
+ } /* resno */\r
+ } /* compno */\r
+ } /* PCRL */\r
+ else { /* CPRL */\r
+\r
+ fprintf(stream, "CPRL\npack_nb tileno compno precno resno layno start_pos end_ph_pos end_pos disto\n"); \r
+\r
+ for (compno = 0; compno < cstr_info->comp; compno++) {\r
+ /* I suppose components have same XRsiz, YRsiz */\r
+ int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;\r
+ int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;\r
+ int x1 = x0 + cstr_info->tile_x;\r
+ int y1 = y0 + cstr_info->tile_y;\r
+ \r
+ for (resno = 0; resno < cstr_info->decomposition + 1; resno++) {\r
+ int prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];\r
+ for (precno = 0; precno < prec_max; precno++) {\r
+ int pcnx = cstr_info->tile[tileno].pw[resno];\r
+ int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->decomposition - resno );\r
+ int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->decomposition - resno );\r
+ int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;\r
+ int precno_y = (int) floor( (float)precno/(float)pcnx );\r
+ for(y = y0; y < y1; y++) {\r
+ if (precno_y*pcy == y ) {\r
+ for (x = x0; x < x1; x++) {\r
+ if (precno_x*pcx == x ) {\r
+ for (layno = 0; layno < cstr_info->layer; layno++) {\r
+ start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;\r
+ end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;\r
+ end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;\r
+ disto = cstr_info->tile[tileno].packet[pack_nb].disto;\r
+ fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %7d %8e\n",\r
+ pack_nb, tileno, compno, precno, resno, layno, start_pos, end_ph_pos, end_pos, disto); \r
+ total_disto += disto;\r
+ pack_nb++; \r
+ }\r
+ }\r
+ }/* x = x0..x1 */\r
+ }\r
+ } /* y = y0..y1 */\r
+ } /* precno */\r
+ } /* resno */\r
+ } /* compno */\r
+ } /* CPRL */ \r
+ } /* tileno */\r
+ \r
+ fprintf(stream, "%8e\n", cstr_info->D_max); /* SE max */\r
+ fprintf(stream, "%.8e\n", total_disto); /* SE totale */\r
+ fclose(stream);\r
+\r
+ wxLogError(wxT("Generated index file %s"), index);\r
+\r
+ return true;\r
+}\r
+\r
#endif // wxUSE_STREAMS\r
\r
#endif // wxUSE_LIBOPENJPEG\r