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;
421 parameters->flags = 0;
424 parameters->jpwl_correct = OPJ_FALSE;
425 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
426 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
427 #endif /* USE_JPWL */
433 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
434 if(dinfo && parameters) {
435 switch(dinfo->codec_format) {
438 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
441 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
450 opj_bool OPJ_CALLCONV opj_setup_decoder_v2(opj_codec_t *p_info, opj_dparameters_t *parameters, opj_event_mgr_t* event_mgr)
452 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
454 if ( !p_info || !parameters || !event_mgr ){
455 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
459 if ( !event_mgr->error_handler || !event_mgr->warning_handler || !event_mgr->error_handler){
460 fprintf(stderr, "[ERROR] Event handler provided to the setup_decoder function is not valid.\n");
464 if (! l_info->is_decompressor) {
465 opj_event_msg_v2(event_mgr, EVT_ERROR, "Codec provided to the setup_decoder function is not a decompressor handler.\n");
469 l_info->m_codec_data.m_decompression.opj_setup_decoder(l_info->m_codec, parameters);
471 l_info->m_event_mgr = event_mgr;
477 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
478 return opj_decode_with_info(dinfo, cio, NULL);
482 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
484 switch(dinfo->codec_format) {
486 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
488 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
490 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
499 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
500 opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
501 if(!cinfo) return NULL;
502 cinfo->is_decompressor = OPJ_FALSE;
505 /* get a J2K coder handle */
506 cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
507 if(!cinfo->j2k_handle) {
513 /* get a JP2 coder handle */
514 cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
515 if(!cinfo->jp2_handle) {
527 cinfo->codec_format = format;
532 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
534 /* destroy the codec */
535 switch(cinfo->codec_format) {
537 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
540 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
547 /* destroy the decompressor */
552 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
554 memset(parameters, 0, sizeof(opj_cparameters_t));
555 /* default coding parameters */
556 parameters->cp_cinema = OFF;
557 parameters->max_comp_size = 0;
558 parameters->numresolution = 6;
559 parameters->cp_rsiz = STD_RSIZ;
560 parameters->cblockw_init = 64;
561 parameters->cblockh_init = 64;
562 parameters->prog_order = LRCP;
563 parameters->roi_compno = -1; /* no ROI */
564 parameters->subsampling_dx = 1;
565 parameters->subsampling_dy = 1;
566 parameters->tp_on = 0;
567 parameters->decod_format = -1;
568 parameters->cod_format = -1;
569 parameters->tcp_rates[0] = 0;
570 parameters->tcp_numlayers = 0;
571 parameters->cp_disto_alloc = 0;
572 parameters->cp_fixed_alloc = 0;
573 parameters->cp_fixed_quality = 0;
574 parameters->jpip_on = OPJ_FALSE;
577 parameters->jpwl_epc_on = OPJ_FALSE;
578 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
581 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
582 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
583 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
588 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
589 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
590 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
591 parameters->jpwl_pprot[i] = 0; /* absent */
594 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
595 parameters->jpwl_sens_addr = 0; /* 0 means auto */
596 parameters->jpwl_sens_range = 0; /* 0 means packet */
597 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
600 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
601 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
602 parameters->jpwl_sens_TPH[i] = -1; /* absent */
605 #endif /* USE_JPWL */
612 * Sets the stream to be a file stream. The FILE must have been open previously.
613 * @param p_stream the stream to modify
614 * @param p_file handler to an already open file.
616 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
618 return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
621 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file, OPJ_SIZE_T p_size, opj_bool p_is_read_stream)
623 opj_stream_t* l_stream = 00;
629 l_stream = opj_stream_create(p_size,p_is_read_stream);
634 opj_stream_set_user_data(l_stream, p_file);
635 opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
636 opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
637 opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
638 opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
639 opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
644 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
645 if(cinfo && parameters && image) {
646 switch(cinfo->codec_format) {
648 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
651 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
661 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
663 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
664 "To extract the index, use the opj_encode_with_info() function.\n"
665 "No index will be generated during this encoding\n");
666 return opj_encode_with_info(cinfo, cio, image, NULL);
669 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) {
670 if(cinfo && cio && image) {
671 switch(cinfo->codec_format) {
673 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
675 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);
685 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
688 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
689 opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
690 opj_free(tile_info->thresh);
691 opj_free(tile_info->packet);
692 opj_free(tile_info->tp);
693 opj_free(tile_info->marker);
695 opj_free(cstr_info->tile);
696 opj_free(cstr_info->marker);
697 opj_free(cstr_info->numdecompos);
702 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_cio,
703 opj_codec_t *p_codec,
704 opj_image_t **p_image )
706 if (p_codec && p_cio) {
707 opj_codec_private_t* l_info = (opj_codec_private_t*) p_codec;
708 opj_stream_private_t* l_cio = (opj_stream_private_t*) p_cio;
710 if(! l_info->is_decompressor) {
711 opj_event_msg_v2(l_info->m_event_mgr, EVT_ERROR, "Codec provided to the read_header function is not a decompressor handler.\n");
715 return l_info->m_codec_data.m_decompression.opj_read_header(
719 l_info->m_event_mgr);
722 fprintf(stderr, "[ERROR] Input parameters of the read_header function are incorrect.\n");
727 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info)
730 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
732 if (l_info->is_decompressor) {
733 l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec);
736 l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec);
739 l_info->m_codec = 00;
745 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
747 * @param p_codec the jpeg2000 codec.
748 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
749 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
750 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
751 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
753 * @return true if the area could be set.
755 opj_bool OPJ_CALLCONV opj_set_decode_area( opj_codec_t *p_codec,
756 opj_image_t* p_image,
757 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
758 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
762 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
763 if (! l_info->is_decompressor) {
767 return l_info->m_codec_data.m_decompression.opj_set_decode_area( l_info->m_codec,
769 p_start_x, p_start_y,
771 l_info->m_event_mgr);
778 * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
779 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
781 * @param p_codec the jpeg2000 codec.
782 * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success.
783 * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
784 * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
785 * as depicted in opj_write_tile.
786 * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image).
787 * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image).
788 * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image).
789 * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image).
790 * @param p_nb_comps pointer to a value that will hold the number of components in the tile.
791 * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the
792 * codestream is over at the time of the call, the value will be set to false. The user should then stop
794 * @param p_stream the stream to decode.
795 * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true.
796 * returning false may be the result of a shortage of memory or an internal error.
798 opj_bool OPJ_CALLCONV opj_read_tile_header(
799 opj_codec_t *p_codec,
800 opj_stream_t * p_stream,
801 OPJ_UINT32 * p_tile_index,
802 OPJ_UINT32 * p_data_size,
803 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
804 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
805 OPJ_UINT32 * p_nb_comps,
806 opj_bool * p_should_go_on)
808 if (p_codec && p_stream && p_data_size && p_tile_index) {
809 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
810 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
812 if (! l_info->is_decompressor) {
816 return l_info->m_codec_data.m_decompression.opj_read_tile_header(
820 p_tile_x0, p_tile_y0,
821 p_tile_x1, p_tile_y1,
825 l_info->m_event_mgr);
831 * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
832 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
834 * @param p_codec the jpeg2000 codec.
835 * @param p_tile_index the index of the tile being decoded, this should be the value set by opj_read_tile_header.
836 * @param p_data pointer to a memory block that will hold the decoded data.
837 * @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.
838 * @param p_stream the stream to decode.
840 * @return true if the data could be decoded.
842 opj_bool OPJ_CALLCONV opj_decode_tile_data(
843 opj_codec_t *p_codec,
844 OPJ_UINT32 p_tile_index,
846 OPJ_UINT32 p_data_size,
847 opj_stream_t *p_stream
850 if (p_codec && p_data && p_stream) {
851 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
852 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
854 if (! l_info->is_decompressor) {
858 return l_info->m_codec_data.m_decompression.opj_decode_tile_data( l_info->m_codec,
863 l_info->m_event_mgr);
872 void OPJ_CALLCONV opj_dump_codec( opj_codec_t *p_codec,
877 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
879 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
883 fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
891 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
894 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
896 return l_codec->opj_get_codec_info(l_codec->m_codec);
906 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t **cstr_info) {
909 if ((*cstr_info)->m_default_tile_info.tccp_info){
910 opj_free((*cstr_info)->m_default_tile_info.tccp_info);
913 if ((*cstr_info)->tile_info){
914 /* FIXME not used for the moment*/
917 opj_free((*cstr_info));
926 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
929 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
931 return l_codec->opj_get_codec_index(l_codec->m_codec);
941 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
945 j2k_destroy_cstr_index(*p_cstr_index);
946 (*p_cstr_index) = NULL;
951 opj_bool OPJ_CALLCONV opj_decode_v2(opj_codec_t *p_info,
953 opj_image_t* p_image)
956 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
957 opj_stream_private_t * l_cio = (opj_stream_private_t *) cio;
959 if (! l_info->is_decompressor) {
963 return l_info->m_codec_data.m_decompression.opj_decode( l_info->m_codec,
966 l_info->m_event_mgr);
976 opj_bool OPJ_CALLCONV opj_end_decompress (opj_codec_t *p_codec,opj_stream_t *p_cio)
978 if (p_codec && p_cio) {
979 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
980 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
982 if (! l_info->is_decompressor) {
985 return l_info->m_codec_data.m_decompression.opj_end_decompress( l_info->m_codec,
987 l_info->m_event_mgr);
997 opj_bool OPJ_CALLCONV opj_get_decoded_tile( opj_codec_t *p_codec,
999 opj_image_t *p_image,
1000 OPJ_UINT32 tile_index)
1002 if (p_codec && p_cio) {
1003 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1004 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_cio;
1006 if (! l_codec->is_decompressor) {
1009 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile( l_codec->m_codec,
1012 l_codec->m_event_mgr,
1023 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, OPJ_UINT32 res_factor)
1025 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1028 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
1033 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, res_factor, l_codec->m_event_mgr);