2 * Copyright (c) 2005, Herve Drolon, FreeImage Team
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
31 #include "opj_config.h"
32 #include "opj_includes.h"
36 * Decompression handler.
38 typedef struct opj_decompression
40 /** Main header reading function handler*/
41 opj_bool (*opj_read_header) ( struct opj_stream_private * cio,
43 opj_image_t **p_image,
44 struct opj_event_mgr * p_manager);
45 /** Decoding function */
46 opj_bool (*opj_decode) ( void * p_codec,
47 struct opj_stream_private *p_cio,
49 struct opj_event_mgr * p_manager);
51 opj_bool (*opj_read_tile_header)( void * p_codec,
52 OPJ_UINT32 * p_tile_index,
53 OPJ_UINT32* p_data_size,
54 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
55 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
56 OPJ_UINT32 * p_nb_comps,
57 opj_bool * p_should_go_on,
58 struct opj_stream_private *p_cio,
59 struct opj_event_mgr * p_manager);
61 opj_bool (*opj_decode_tile_data)( void * p_codec,
62 OPJ_UINT32 p_tile_index,
64 OPJ_UINT32 p_data_size,
65 struct opj_stream_private *p_cio,
66 struct opj_event_mgr * p_manager);
67 /** Reading function used after codestream if necessary */
68 opj_bool (* opj_end_decompress) ( void *p_codec,
69 struct opj_stream_private *cio,
70 struct opj_event_mgr * p_manager);
71 /** Codec destroy function handler*/
72 void (*opj_destroy) (void * p_codec);
73 /** Setup decoder function handler */
74 void (*opj_setup_decoder) (void * p_codec, opj_dparameters_t * p_param);
75 /** Set decode area function handler */
76 opj_bool (*opj_set_decode_area) ( void * p_codec,
78 OPJ_INT32 p_start_x, OPJ_INT32 p_end_x,
79 OPJ_INT32 p_start_y, OPJ_INT32 p_end_y,
80 struct opj_event_mgr * p_manager);
82 /** Get tile function */
83 opj_bool (*opj_get_decoded_tile) ( void *p_codec,
84 opj_stream_private_t *p_cio,
86 struct opj_event_mgr * p_manager,
87 OPJ_UINT32 tile_index);
89 /** Set the decoded resolution factor */
90 opj_bool (*opj_set_decoded_resolution_factor) (void * p_codec, OPJ_UINT32 res_factor, struct opj_event_mgr * p_manager);
95 * Compression handler. FIXME DOC
97 typedef struct opj_compression
99 opj_bool (* opj_start_compress) (void *p_codec,struct opj_stream_private *cio,struct opj_image * p_image, struct opj_event_mgr * p_manager);
100 opj_bool (* opj_encode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
101 opj_bool (* opj_write_tile) (void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private * p_cio,struct opj_event_mgr * p_manager);
102 opj_bool (* opj_end_compress) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
103 void (* opj_destroy) (void * p_codec);
104 void (*opj_setup_encoder) (void * p_codec,opj_cparameters_t * p_param,struct opj_image * p_image, struct opj_event_mgr * p_manager);
109 * Main codec handler used for compression or decompression.
111 typedef struct opj_codec_private
115 opj_decompression_t m_decompression;
116 opj_compression_t m_compression;
121 opj_event_mgr_t* m_event_mgr;
122 /** Flag to indicate if the codec is used to decode or encode*/
123 opj_bool is_decompressor;
124 void (*opj_dump_codec) (void * p_codec, OPJ_INT32 info_flag, FILE* output_stream);
125 opj_codestream_info_v2_t* (*opj_get_codec_info)(void* p_codec);
126 opj_codestream_index_t* (*opj_get_codec_index)(void* p_codec);
132 /* ---------------------------------------------------------------------- */
135 OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
137 OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
138 return l_nb_read ? l_nb_read : -1;
141 OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
143 OPJ_OFF_T file_length = 0;
145 OPJ_FSEEK(p_file, 0, SEEK_END);
146 file_length = (OPJ_UINT64)OPJ_FTELL(p_file);
147 OPJ_FSEEK(p_file, 0, SEEK_SET);
152 OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
154 return fwrite(p_buffer,1,p_nb_bytes,p_file);
157 OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
159 if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
166 opj_bool opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
168 if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
175 /* ---------------------------------------------------------------------- */
179 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
181 OPJ_ARG_NOT_USED(lpReserved);
182 OPJ_ARG_NOT_USED(hModule);
184 switch (ul_reason_for_call) {
185 case DLL_PROCESS_ATTACH :
187 case DLL_PROCESS_DETACH :
189 case DLL_THREAD_ATTACH :
190 case DLL_THREAD_DETACH :
196 #endif /* OPJ_STATIC */
199 /* ---------------------------------------------------------------------- */
202 const char* OPJ_CALLCONV opj_version(void) {
203 return PACKAGE_VERSION;
208 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
209 opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
210 if(!dinfo) return NULL;
211 dinfo->is_decompressor = OPJ_TRUE;
215 /* get a J2K decoder handle */
216 dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
217 if(!dinfo->j2k_handle) {
223 /* get a JP2 decoder handle */
224 dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
225 if(!dinfo->jp2_handle) {
236 dinfo->codec_format = format;
241 opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format)
243 opj_codec_private_t *l_info = 00;
245 l_info = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
249 memset(l_info, 0, sizeof(opj_codec_private_t));
251 l_info->is_decompressor = 1;
255 l_info->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
257 l_info->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
259 l_info->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
261 l_info->m_codec_data.m_decompression.opj_decode =
262 (opj_bool (*) ( void *,
263 struct opj_stream_private *,
264 opj_image_t*, struct opj_event_mgr * )) j2k_decode_v2;
266 l_info->m_codec_data.m_decompression.opj_end_decompress =
267 (opj_bool (*) ( void *,
268 struct opj_stream_private *,
269 struct opj_event_mgr *)) j2k_end_decompress;
271 l_info->m_codec_data.m_decompression.opj_read_header =
272 (opj_bool (*) ( struct opj_stream_private *,
275 struct opj_event_mgr * )) j2k_read_header;
277 l_info->m_codec_data.m_decompression.opj_destroy =
278 (void (*) (void *))j2k_destroy;
280 l_info->m_codec_data.m_decompression.opj_setup_decoder =
281 (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
283 l_info->m_codec_data.m_decompression.opj_read_tile_header =
284 (opj_bool (*) ( void *,
287 OPJ_INT32*, OPJ_INT32*,
288 OPJ_INT32*, OPJ_INT32*,
291 struct opj_stream_private *,
292 struct opj_event_mgr * )) j2k_read_tile_header;
294 l_info->m_codec_data.m_decompression.opj_decode_tile_data =
295 (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
297 l_info->m_codec_data.m_decompression.opj_set_decode_area =
298 (opj_bool (*) (void *, opj_image_t*, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
300 l_info->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) ( void *p_codec,
301 opj_stream_private_t *p_cio,
302 opj_image_t *p_image,
303 struct opj_event_mgr * p_manager,
304 OPJ_UINT32 tile_index)) j2k_get_tile;
306 l_info->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
307 OPJ_UINT32 res_factor,
308 struct opj_event_mgr * p_manager)) j2k_set_decoded_resolution_factor;
310 l_info->m_codec = j2k_create_decompress_v2();
312 if (! l_info->m_codec) {
320 /* get a JP2 decoder handle */
321 l_info->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
323 l_info->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
325 l_info->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
327 l_info->m_codec_data.m_decompression.opj_decode =
328 (opj_bool (*) ( void *,
329 struct opj_stream_private *,
331 struct opj_event_mgr * )) jp2_decode_v2;
333 l_info->m_codec_data.m_decompression.opj_end_decompress = (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
335 l_info->m_codec_data.m_decompression.opj_read_header = (opj_bool (*) (
336 struct opj_stream_private *,
339 struct opj_event_mgr * )) jp2_read_header;
341 l_info->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) (
351 struct opj_stream_private *,
352 struct opj_event_mgr * )) jp2_read_tile_header;
354 l_info->m_codec_data.m_decompression.opj_decode_tile_data = (opj_bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *, struct opj_event_mgr * )) jp2_decode_tile;
356 l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
358 l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder_v2;
360 l_info->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,opj_image_t*, OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) jp2_set_decode_area;
362 l_info->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) ( void *p_codec,
363 opj_stream_private_t *p_cio,
364 opj_image_t *p_image,
365 struct opj_event_mgr * p_manager,
366 OPJ_UINT32 tile_index)) jp2_get_tile;
368 l_info->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
369 OPJ_UINT32 res_factor,
370 opj_event_mgr_t * p_manager)) jp2_set_decoded_resolution_factor;
372 l_info->m_codec = jp2_create(OPJ_TRUE);
374 if (! l_info->m_codec) {
387 return (opj_codec_t*) l_info;
391 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
393 /* destroy the codec */
394 switch(dinfo->codec_format) {
397 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
400 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
406 /* destroy the decompressor */
411 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
413 memset(parameters, 0, sizeof(opj_dparameters_t));
414 /* default decoding parameters */
415 parameters->cp_layer = 0;
416 parameters->cp_reduce = 0;
417 parameters->cp_limit_decoding = NO_LIMITATION;
419 parameters->decod_format = -1;
420 parameters->cod_format = -1;
423 parameters->jpwl_correct = OPJ_FALSE;
424 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
425 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
426 #endif /* USE_JPWL */
432 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
433 if(dinfo && parameters) {
434 switch(dinfo->codec_format) {
437 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
440 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
449 opj_bool OPJ_CALLCONV opj_setup_decoder_v2(opj_codec_t *p_info, opj_dparameters_t *parameters, opj_event_mgr_t* event_mgr)
451 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
453 if ( !p_info || !parameters || !event_mgr ){
454 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
458 if ( !event_mgr->error_handler || !event_mgr->warning_handler || !event_mgr->error_handler){
459 fprintf(stderr, "[ERROR] Event handler provided to the setup_decoder function is not valid.\n");
463 if (! l_info->is_decompressor) {
464 opj_event_msg_v2(event_mgr, EVT_ERROR, "Codec provided to the setup_decoder function is not a decompressor handler.\n");
468 l_info->m_codec_data.m_decompression.opj_setup_decoder(l_info->m_codec, parameters);
470 l_info->m_event_mgr = event_mgr;
476 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
477 return opj_decode_with_info(dinfo, cio, NULL);
481 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
483 switch(dinfo->codec_format) {
485 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
487 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
489 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
498 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
499 opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
500 if(!cinfo) return NULL;
501 cinfo->is_decompressor = OPJ_FALSE;
504 /* get a J2K coder handle */
505 cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
506 if(!cinfo->j2k_handle) {
512 /* get a JP2 coder handle */
513 cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
514 if(!cinfo->jp2_handle) {
526 cinfo->codec_format = format;
531 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
533 /* destroy the codec */
534 switch(cinfo->codec_format) {
536 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
539 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
546 /* destroy the decompressor */
551 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
553 memset(parameters, 0, sizeof(opj_cparameters_t));
554 /* default coding parameters */
555 parameters->cp_cinema = OFF;
556 parameters->max_comp_size = 0;
557 parameters->numresolution = 6;
558 parameters->cp_rsiz = STD_RSIZ;
559 parameters->cblockw_init = 64;
560 parameters->cblockh_init = 64;
561 parameters->prog_order = LRCP;
562 parameters->roi_compno = -1; /* no ROI */
563 parameters->subsampling_dx = 1;
564 parameters->subsampling_dy = 1;
565 parameters->tp_on = 0;
566 parameters->decod_format = -1;
567 parameters->cod_format = -1;
568 parameters->tcp_rates[0] = 0;
569 parameters->tcp_numlayers = 0;
570 parameters->cp_disto_alloc = 0;
571 parameters->cp_fixed_alloc = 0;
572 parameters->cp_fixed_quality = 0;
573 parameters->jpip_on = OPJ_FALSE;
576 parameters->jpwl_epc_on = OPJ_FALSE;
577 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
580 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
581 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
582 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
587 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
588 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
589 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
590 parameters->jpwl_pprot[i] = 0; /* absent */
593 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
594 parameters->jpwl_sens_addr = 0; /* 0 means auto */
595 parameters->jpwl_sens_range = 0; /* 0 means packet */
596 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
599 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
600 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
601 parameters->jpwl_sens_TPH[i] = -1; /* absent */
604 #endif /* USE_JPWL */
611 * Sets the stream to be a file stream. The FILE must have been open previously.
612 * @param p_stream the stream to modify
613 * @param p_file handler to an already open file.
615 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
617 return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
620 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file, OPJ_SIZE_T p_size, opj_bool p_is_read_stream)
622 opj_stream_t* l_stream = 00;
628 l_stream = opj_stream_create(p_size,p_is_read_stream);
633 opj_stream_set_user_data(l_stream, p_file);
634 opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
635 opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
636 opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
637 opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
638 opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
643 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
644 if(cinfo && parameters && image) {
645 switch(cinfo->codec_format) {
647 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
650 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
660 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
662 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
663 "To extract the index, use the opj_encode_with_info() function.\n"
664 "No index will be generated during this encoding\n");
665 return opj_encode_with_info(cinfo, cio, image, NULL);
668 opj_bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
669 if(cinfo && cio && image) {
670 switch(cinfo->codec_format) {
672 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
674 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);
684 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
687 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
688 opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
689 opj_free(tile_info->thresh);
690 opj_free(tile_info->packet);
691 opj_free(tile_info->tp);
692 opj_free(tile_info->marker);
694 opj_free(cstr_info->tile);
695 opj_free(cstr_info->marker);
696 opj_free(cstr_info->numdecompos);
701 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_cio,
702 opj_codec_t *p_codec,
703 opj_image_t **p_image )
705 if (p_codec && p_cio) {
706 opj_codec_private_t* l_info = (opj_codec_private_t*) p_codec;
707 opj_stream_private_t* l_cio = (opj_stream_private_t*) p_cio;
709 if(! l_info->is_decompressor) {
710 opj_event_msg_v2(l_info->m_event_mgr, EVT_ERROR, "Codec provided to the read_header function is not a decompressor handler.\n");
714 return l_info->m_codec_data.m_decompression.opj_read_header(
718 l_info->m_event_mgr);
721 fprintf(stderr, "[ERROR] Input parameters of the read_header function are incorrect.\n");
726 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info)
729 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
731 if (l_info->is_decompressor) {
732 l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec);
735 l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec);
738 l_info->m_codec = 00;
744 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
746 * @param p_codec the jpeg2000 codec.
747 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
748 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
749 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
750 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
752 * @return true if the area could be set.
754 opj_bool OPJ_CALLCONV opj_set_decode_area( opj_codec_t *p_codec,
755 opj_image_t* p_image,
756 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
757 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
761 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
762 if (! l_info->is_decompressor) {
766 return l_info->m_codec_data.m_decompression.opj_set_decode_area( l_info->m_codec,
768 p_start_x, p_start_y,
770 l_info->m_event_mgr);
777 * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
778 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
780 * @param p_codec the jpeg2000 codec.
781 * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success.
782 * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
783 * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
784 * as depicted in opj_write_tile.
785 * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image).
786 * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image).
787 * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image).
788 * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image).
789 * @param p_nb_comps pointer to a value that will hold the number of components in the tile.
790 * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the
791 * codestream is over at the time of the call, the value will be set to false. The user should then stop
793 * @param p_stream the stream to decode.
794 * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true.
795 * returning false may be the result of a shortage of memory or an internal error.
797 opj_bool OPJ_CALLCONV opj_read_tile_header(
798 opj_codec_t *p_codec,
799 opj_stream_t * p_stream,
800 OPJ_UINT32 * p_tile_index,
801 OPJ_UINT32 * p_data_size,
802 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
803 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
804 OPJ_UINT32 * p_nb_comps,
805 opj_bool * p_should_go_on)
807 if (p_codec && p_stream && p_data_size && p_tile_index) {
808 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
809 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
811 if (! l_info->is_decompressor) {
815 return l_info->m_codec_data.m_decompression.opj_read_tile_header(
819 p_tile_x0, p_tile_y0,
820 p_tile_x1, p_tile_y1,
824 l_info->m_event_mgr);
830 * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
831 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
833 * @param p_codec the jpeg2000 codec.
834 * @param p_tile_index the index of the tile being decoded, this should be the value set by opj_read_tile_header.
835 * @param p_data pointer to a memory block that will hold the decoded data.
836 * @param p_data_size size of p_data. p_data_size should be bigger or equal to the value set by opj_read_tile_header.
837 * @param p_stream the stream to decode.
839 * @return true if the data could be decoded.
841 opj_bool OPJ_CALLCONV opj_decode_tile_data(
842 opj_codec_t *p_codec,
843 OPJ_UINT32 p_tile_index,
845 OPJ_UINT32 p_data_size,
846 opj_stream_t *p_stream
849 if (p_codec && p_data && p_stream) {
850 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
851 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
853 if (! l_info->is_decompressor) {
857 return l_info->m_codec_data.m_decompression.opj_decode_tile_data( l_info->m_codec,
862 l_info->m_event_mgr);
871 void OPJ_CALLCONV opj_dump_codec( opj_codec_t *p_codec,
876 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
878 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
882 fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
890 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
893 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
895 return l_codec->opj_get_codec_info(l_codec->m_codec);
905 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t **cstr_info) {
908 if ((*cstr_info)->m_default_tile_info.tccp_info){
909 opj_free((*cstr_info)->m_default_tile_info.tccp_info);
912 if ((*cstr_info)->tile_info){
913 /* FIXME not used for the moment*/
916 opj_free((*cstr_info));
925 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
928 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
930 return l_codec->opj_get_codec_index(l_codec->m_codec);
940 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
944 j2k_destroy_cstr_index(*p_cstr_index);
945 (*p_cstr_index) = NULL;
950 opj_bool OPJ_CALLCONV opj_decode_v2(opj_codec_t *p_info,
952 opj_image_t* p_image)
955 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
956 opj_stream_private_t * l_cio = (opj_stream_private_t *) cio;
958 if (! l_info->is_decompressor) {
962 return l_info->m_codec_data.m_decompression.opj_decode( l_info->m_codec,
965 l_info->m_event_mgr);
975 opj_bool OPJ_CALLCONV opj_end_decompress (opj_codec_t *p_codec,opj_stream_t *p_cio)
977 if (p_codec && p_cio) {
978 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
979 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
981 if (! l_info->is_decompressor) {
984 return l_info->m_codec_data.m_decompression.opj_end_decompress( l_info->m_codec,
986 l_info->m_event_mgr);
996 opj_bool OPJ_CALLCONV opj_get_decoded_tile( opj_codec_t *p_codec,
998 opj_image_t *p_image,
999 OPJ_UINT32 tile_index)
1001 if (p_codec && p_cio) {
1002 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1003 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_cio;
1005 if (! l_codec->is_decompressor) {
1008 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile( l_codec->m_codec,
1011 l_codec->m_event_mgr,
1022 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, OPJ_UINT32 res_factor)
1024 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1027 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
1032 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, res_factor, l_codec->m_event_mgr);