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,
100 struct opj_stream_private *cio,
101 struct opj_image * p_image,
102 struct opj_event_mgr * p_manager);
104 opj_bool (* opj_encode) ( void * p_codec,
105 struct opj_stream_private *p_cio,
106 struct opj_event_mgr * p_manager);
108 opj_bool (* opj_write_tile) ( void * p_codec,
109 OPJ_UINT32 p_tile_index,
111 OPJ_UINT32 p_data_size,
112 struct opj_stream_private * p_cio,
113 struct opj_event_mgr * p_manager);
115 opj_bool (* opj_end_compress) ( void * p_codec,
116 struct opj_stream_private *p_cio,
117 struct opj_event_mgr * p_manager);
119 void (* opj_destroy) (void * p_codec);
121 void (*opj_setup_encoder) ( void * p_codec,
122 opj_cparameters_t * p_param,
123 struct opj_image * p_image,
124 struct opj_event_mgr * p_manager);
129 * Main codec handler used for compression or decompression.
131 typedef struct opj_codec_private
135 opj_decompression_t m_decompression;
136 opj_compression_t m_compression;
141 opj_event_mgr_t m_event_mgr;
142 /** Flag to indicate if the codec is used to decode or encode*/
143 opj_bool is_decompressor;
144 void (*opj_dump_codec) (void * p_codec, OPJ_INT32 info_flag, FILE* output_stream);
145 opj_codestream_info_v2_t* (*opj_get_codec_info)(void* p_codec);
146 opj_codestream_index_t* (*opj_get_codec_index)(void* p_codec);
150 /* ---------------------------------------------------------------------- */
152 * Default callback function.
155 void opj_default_callback (const char *msg, void *client_data)
159 void set_default_event_handler(opj_event_mgr_t * p_manager)
161 p_manager->m_error_data = 00;
162 p_manager->m_warning_data = 00;
163 p_manager->m_info_data = 00;
164 p_manager->error_handler = opj_default_callback;
165 p_manager->info_handler = opj_default_callback;
166 p_manager->warning_handler = opj_default_callback;
169 opj_bool OPJ_CALLCONV opj_set_info_handler( opj_codec_t * p_codec,
170 opj_msg_callback p_callback,
173 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
178 l_codec->m_event_mgr.info_handler = p_callback;
179 l_codec->m_event_mgr.m_info_data = p_user_data;
184 opj_bool OPJ_CALLCONV opj_set_warning_handler( opj_codec_t * p_codec,
185 opj_msg_callback p_callback,
188 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
193 l_codec->m_event_mgr.warning_handler = p_callback;
194 l_codec->m_event_mgr.m_warning_data = p_user_data;
199 opj_bool OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec,
200 opj_msg_callback p_callback,
203 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
208 l_codec->m_event_mgr.error_handler = p_callback;
209 l_codec->m_event_mgr.m_error_data = p_user_data;
214 /* ---------------------------------------------------------------------- */
217 OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
219 OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
220 return l_nb_read ? l_nb_read : -1;
223 OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
225 OPJ_OFF_T file_length = 0;
227 OPJ_FSEEK(p_file, 0, SEEK_END);
228 file_length = (OPJ_UINT64)OPJ_FTELL(p_file);
229 OPJ_FSEEK(p_file, 0, SEEK_SET);
234 OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
236 return fwrite(p_buffer,1,p_nb_bytes,p_file);
239 OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
241 if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
248 opj_bool opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
250 if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
257 /* ---------------------------------------------------------------------- */
261 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
263 OPJ_ARG_NOT_USED(lpReserved);
264 OPJ_ARG_NOT_USED(hModule);
266 switch (ul_reason_for_call) {
267 case DLL_PROCESS_ATTACH :
269 case DLL_PROCESS_DETACH :
271 case DLL_THREAD_ATTACH :
272 case DLL_THREAD_DETACH :
278 #endif /* OPJ_STATIC */
281 /* ---------------------------------------------------------------------- */
284 const char* OPJ_CALLCONV opj_version(void) {
285 return PACKAGE_VERSION;
290 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
291 opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
292 if(!dinfo) return NULL;
293 dinfo->is_decompressor = OPJ_TRUE;
297 /* get a J2K decoder handle */
298 dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
299 if(!dinfo->j2k_handle) {
305 /* get a JP2 decoder handle */
306 dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
307 if(!dinfo->jp2_handle) {
318 dinfo->codec_format = format;
323 opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format)
325 opj_codec_private_t *l_codec = 00;
327 l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
331 memset(l_codec, 0, sizeof(opj_codec_private_t));
333 l_codec->is_decompressor = 1;
337 l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
339 l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
341 l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
343 l_codec->m_codec_data.m_decompression.opj_decode =
344 (opj_bool (*) ( void *,
345 struct opj_stream_private *,
346 opj_image_t*, struct opj_event_mgr * )) j2k_decode_v2;
348 l_codec->m_codec_data.m_decompression.opj_end_decompress =
349 (opj_bool (*) ( void *,
350 struct opj_stream_private *,
351 struct opj_event_mgr *)) j2k_end_decompress;
353 l_codec->m_codec_data.m_decompression.opj_read_header =
354 (opj_bool (*) ( struct opj_stream_private *,
357 struct opj_event_mgr * )) j2k_read_header;
359 l_codec->m_codec_data.m_decompression.opj_destroy =
360 (void (*) (void *))j2k_destroy;
362 l_codec->m_codec_data.m_decompression.opj_setup_decoder =
363 (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
365 l_codec->m_codec_data.m_decompression.opj_read_tile_header =
366 (opj_bool (*) ( void *,
369 OPJ_INT32*, OPJ_INT32*,
370 OPJ_INT32*, OPJ_INT32*,
373 struct opj_stream_private *,
374 struct opj_event_mgr * )) j2k_read_tile_header;
376 l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
377 (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
379 l_codec->m_codec_data.m_decompression.opj_set_decode_area =
380 (opj_bool (*) (void *, opj_image_t*, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
382 l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
383 opj_stream_private_t *p_cio,
384 opj_image_t *p_image,
385 struct opj_event_mgr * p_manager,
386 OPJ_UINT32 tile_index)) j2k_get_tile;
388 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
389 OPJ_UINT32 res_factor,
390 struct opj_event_mgr * p_manager)) j2k_set_decoded_resolution_factor;
392 l_codec->m_codec = j2k_create_decompress_v2();
394 if (! l_codec->m_codec) {
402 /* get a JP2 decoder handle */
403 l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
405 l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
407 l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
409 l_codec->m_codec_data.m_decompression.opj_decode =
410 (opj_bool (*) ( void *,
411 struct opj_stream_private *,
413 struct opj_event_mgr * )) jp2_decode_v2;
415 l_codec->m_codec_data.m_decompression.opj_end_decompress = (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
417 l_codec->m_codec_data.m_decompression.opj_read_header = (opj_bool (*) (
418 struct opj_stream_private *,
421 struct opj_event_mgr * )) jp2_read_header;
423 l_codec->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) (
433 struct opj_stream_private *,
434 struct opj_event_mgr * )) jp2_read_tile_header;
436 l_codec->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;
438 l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
440 l_codec->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder_v2;
442 l_codec->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;
444 l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
445 opj_stream_private_t *p_cio,
446 opj_image_t *p_image,
447 struct opj_event_mgr * p_manager,
448 OPJ_UINT32 tile_index)) jp2_get_tile;
450 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
451 OPJ_UINT32 res_factor,
452 opj_event_mgr_t * p_manager)) jp2_set_decoded_resolution_factor;
454 l_codec->m_codec = jp2_create(OPJ_TRUE);
456 if (! l_codec->m_codec) {
469 set_default_event_handler(&(l_codec->m_event_mgr));
470 return (opj_codec_t*) l_codec;
474 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
476 /* destroy the codec */
477 switch(dinfo->codec_format) {
480 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
483 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
489 /* destroy the decompressor */
494 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
496 memset(parameters, 0, sizeof(opj_dparameters_t));
497 /* default decoding parameters */
498 parameters->cp_layer = 0;
499 parameters->cp_reduce = 0;
500 parameters->cp_limit_decoding = NO_LIMITATION;
502 parameters->decod_format = -1;
503 parameters->cod_format = -1;
504 parameters->flags = 0;
507 parameters->jpwl_correct = OPJ_FALSE;
508 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
509 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
510 #endif /* USE_JPWL */
516 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
517 if(dinfo && parameters) {
518 switch(dinfo->codec_format) {
521 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
524 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
533 opj_bool OPJ_CALLCONV opj_setup_decoder_v2( opj_codec_t *p_codec,
534 opj_dparameters_t *parameters
537 if (p_codec && parameters) {
538 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
540 if (! l_codec->is_decompressor) {
541 opj_event_msg_v2(&(l_codec->m_event_mgr), EVT_ERROR, "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
545 l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
553 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
554 return opj_decode_with_info(dinfo, cio, NULL);
558 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
560 switch(dinfo->codec_format) {
562 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
564 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
566 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
576 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
577 opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
578 if(!cinfo) return NULL;
579 cinfo->is_decompressor = OPJ_FALSE;
582 /* get a J2K coder handle */
583 cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
584 if(!cinfo->j2k_handle) {
590 /* get a JP2 coder handle */
591 cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
592 if(!cinfo->jp2_handle) {
604 cinfo->codec_format = format;
610 opj_codec_t* OPJ_CALLCONV opj_create_compress_v2(OPJ_CODEC_FORMAT p_format)
612 opj_codec_private_t *l_codec = 00;
614 l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
618 memset(l_codec, 0, sizeof(opj_codec_private_t));
620 l_codec->is_decompressor = 0;
624 l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
625 struct opj_stream_private *,
626 struct opj_event_mgr * )) j2k_encode_v2;
628 l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) ( void *,
629 struct opj_stream_private *,
630 struct opj_event_mgr *)) j2k_end_compress;
632 l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
633 struct opj_stream_private *,
635 struct opj_event_mgr *)) j2k_start_compress;
637 l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
641 struct opj_stream_private *,
642 struct opj_event_mgr *) ) j2k_write_tile;
644 l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy;
646 l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
649 struct opj_event_mgr * )) j2k_setup_encoder_v2;
651 l_codec->m_codec = j2k_create_compress_v2();
652 if (! l_codec->m_codec) {
660 /* get a JP2 decoder handle */
661 l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
662 struct opj_stream_private *,
663 struct opj_event_mgr * )) opj_jp2_encode;
665 l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) ( void *,
666 struct opj_stream_private *,
667 struct opj_event_mgr *)) jp2_end_compress;
669 l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
670 struct opj_stream_private *,
672 struct opj_event_mgr *)) jp2_start_compress;
674 l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
678 struct opj_stream_private *,
679 struct opj_event_mgr *)) jp2_write_tile;
681 l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy;
683 l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
686 struct opj_event_mgr * )) jp2_setup_encoder;
688 l_codec->m_codec = jp2_create(OPJ_FALSE);
689 if (! l_codec->m_codec) {
703 set_default_event_handler(&(l_codec->m_event_mgr));
704 return (opj_codec_t*) l_codec;
708 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
710 /* destroy the codec */
711 switch(cinfo->codec_format) {
713 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
716 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
723 /* destroy the decompressor */
728 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
730 memset(parameters, 0, sizeof(opj_cparameters_t));
731 /* default coding parameters */
732 parameters->cp_cinema = OFF;
733 parameters->max_comp_size = 0;
734 parameters->numresolution = 6;
735 parameters->cp_rsiz = STD_RSIZ;
736 parameters->cblockw_init = 64;
737 parameters->cblockh_init = 64;
738 parameters->prog_order = LRCP;
739 parameters->roi_compno = -1; /* no ROI */
740 parameters->subsampling_dx = 1;
741 parameters->subsampling_dy = 1;
742 parameters->tp_on = 0;
743 parameters->decod_format = -1;
744 parameters->cod_format = -1;
745 parameters->tcp_rates[0] = 0;
746 parameters->tcp_numlayers = 0;
747 parameters->cp_disto_alloc = 0;
748 parameters->cp_fixed_alloc = 0;
749 parameters->cp_fixed_quality = 0;
750 parameters->jpip_on = OPJ_FALSE;
753 parameters->jpwl_epc_on = OPJ_FALSE;
754 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
757 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
758 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
759 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
764 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
765 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
766 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
767 parameters->jpwl_pprot[i] = 0; /* absent */
770 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
771 parameters->jpwl_sens_addr = 0; /* 0 means auto */
772 parameters->jpwl_sens_range = 0; /* 0 means packet */
773 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
776 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
777 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
778 parameters->jpwl_sens_TPH[i] = -1; /* absent */
781 #endif /* USE_JPWL */
788 * Sets the stream to be a file stream. The FILE must have been open previously.
789 * @param p_stream the stream to modify
790 * @param p_file handler to an already open file.
792 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
794 return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
797 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream ( FILE * p_file,
799 opj_bool p_is_read_stream)
801 opj_stream_t* l_stream = 00;
807 l_stream = opj_stream_create(p_size,p_is_read_stream);
812 opj_stream_set_user_data(l_stream, p_file);
813 opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
814 opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
815 opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
816 opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
817 opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
822 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
823 if(cinfo && parameters && image) {
824 switch(cinfo->codec_format) {
826 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
829 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
839 opj_bool OPJ_CALLCONV opj_setup_encoder_v2( opj_codec_t *p_codec,
840 opj_cparameters_t *parameters,
841 opj_image_t *p_image)
843 if (p_codec && parameters && p_image) {
844 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
846 if (! l_codec->is_decompressor) {
847 l_codec->m_codec_data.m_compression.opj_setup_encoder( l_codec->m_codec,
850 &(l_codec->m_event_mgr) );
858 opj_bool OPJ_CALLCONV opj_start_compress ( opj_codec_t *p_codec,
859 opj_image_t * p_image,
860 opj_stream_t *p_stream)
862 if (p_codec && p_stream) {
863 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
864 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
866 if (! l_codec->is_decompressor) {
867 return l_codec->m_codec_data.m_compression.opj_start_compress( l_codec->m_codec,
870 &(l_codec->m_event_mgr));
877 opj_bool OPJ_CALLCONV opj_encode_v2(opj_codec_t *p_info, opj_stream_t *p_stream)
879 if (p_info && p_stream) {
880 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
881 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
883 if (! l_codec->is_decompressor) {
884 l_codec->m_codec_data.m_compression.opj_encode( l_codec->m_codec,
886 &(l_codec->m_event_mgr));
895 opj_bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
896 opj_stream_t *p_stream)
898 if (p_codec && p_stream) {
899 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
900 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
902 if (! l_codec->is_decompressor) {
903 return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
905 &(l_codec->m_event_mgr));
913 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
915 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
916 "To extract the index, use the opj_encode_with_info() function.\n"
917 "No index will be generated during this encoding\n");
918 return opj_encode_with_info(cinfo, cio, image, NULL);
922 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) {
923 if(cinfo && cio && image) {
924 switch(cinfo->codec_format) {
926 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
928 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);
939 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
942 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
943 opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
944 opj_free(tile_info->thresh);
945 opj_free(tile_info->packet);
946 opj_free(tile_info->tp);
947 opj_free(tile_info->marker);
949 opj_free(cstr_info->tile);
950 opj_free(cstr_info->marker);
951 opj_free(cstr_info->numdecompos);
956 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
957 opj_codec_t *p_codec,
958 opj_image_t **p_image )
960 if (p_codec && p_stream) {
961 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
962 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
964 if(! l_codec->is_decompressor) {
965 opj_event_msg_v2(&(l_codec->m_event_mgr), EVT_ERROR, "Codec provided to the opj_read_header function is not a decompressor handler.\n");
969 return l_codec->m_codec_data.m_decompression.opj_read_header( l_stream,
972 &(l_codec->m_event_mgr) );
979 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
982 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
984 if (l_codec->is_decompressor) {
985 l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
988 l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
991 l_codec->m_codec = 00;
997 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
999 * @param p_codec the jpeg2000 codec.
1000 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
1001 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
1002 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
1003 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
1005 * @return true if the area could be set.
1007 opj_bool OPJ_CALLCONV opj_set_decode_area( opj_codec_t *p_codec,
1008 opj_image_t* p_image,
1009 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
1010 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
1014 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1016 if (! l_codec->is_decompressor) {
1020 return l_codec->m_codec_data.m_decompression.opj_set_decode_area( l_codec->m_codec,
1022 p_start_x, p_start_y,
1024 &(l_codec->m_event_mgr) );
1030 * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
1031 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
1033 * @param p_codec the jpeg2000 codec.
1034 * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success.
1035 * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
1036 * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
1037 * as depicted in opj_write_tile.
1038 * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image).
1039 * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image).
1040 * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image).
1041 * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image).
1042 * @param p_nb_comps pointer to a value that will hold the number of components in the tile.
1043 * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the
1044 * codestream is over at the time of the call, the value will be set to false. The user should then stop
1046 * @param p_stream the stream to decode.
1047 * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true.
1048 * returning false may be the result of a shortage of memory or an internal error.
1050 opj_bool OPJ_CALLCONV opj_read_tile_header( opj_codec_t *p_codec,
1051 opj_stream_t * p_stream,
1052 OPJ_UINT32 * p_tile_index,
1053 OPJ_UINT32 * p_data_size,
1054 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
1055 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
1056 OPJ_UINT32 * p_nb_comps,
1057 opj_bool * p_should_go_on)
1059 if (p_codec && p_stream && p_data_size && p_tile_index) {
1060 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1061 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1063 if (! l_codec->is_decompressor) {
1067 return l_codec->m_codec_data.m_decompression.opj_read_tile_header( l_codec->m_codec,
1070 p_tile_x0, p_tile_y0,
1071 p_tile_x1, p_tile_y1,
1075 &(l_codec->m_event_mgr));
1081 * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
1082 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
1084 * @param p_codec the jpeg2000 codec.
1085 * @param p_tile_index the index of the tile being decoded, this should be the value set by opj_read_tile_header.
1086 * @param p_data pointer to a memory block that will hold the decoded data.
1087 * @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.
1088 * @param p_stream the stream to decode.
1090 * @return true if the data could be decoded.
1092 opj_bool OPJ_CALLCONV opj_decode_tile_data( opj_codec_t *p_codec,
1093 OPJ_UINT32 p_tile_index,
1095 OPJ_UINT32 p_data_size,
1096 opj_stream_t *p_stream
1099 if (p_codec && p_data && p_stream) {
1100 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1101 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1103 if (! l_codec->is_decompressor) {
1107 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data( l_codec->m_codec,
1112 &(l_codec->m_event_mgr) );
1121 void OPJ_CALLCONV opj_dump_codec( opj_codec_t *p_codec,
1122 OPJ_INT32 info_flag,
1123 FILE* output_stream)
1126 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1128 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
1132 fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
1140 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
1143 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1145 return l_codec->opj_get_codec_info(l_codec->m_codec);
1155 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t **cstr_info) {
1158 if ((*cstr_info)->m_default_tile_info.tccp_info){
1159 opj_free((*cstr_info)->m_default_tile_info.tccp_info);
1162 if ((*cstr_info)->tile_info){
1163 /* FIXME not used for the moment*/
1166 opj_free((*cstr_info));
1167 (*cstr_info) = NULL;
1175 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
1178 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1180 return l_codec->opj_get_codec_index(l_codec->m_codec);
1190 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
1193 j2k_destroy_cstr_index(*p_cstr_index);
1194 (*p_cstr_index) = NULL;
1202 opj_bool OPJ_CALLCONV opj_decode_v2(opj_codec_t *p_codec,
1203 opj_stream_t *p_stream,
1204 opj_image_t* p_image)
1206 if (p_codec && p_stream) {
1207 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1208 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1210 if (! l_codec->is_decompressor) {
1214 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
1217 &(l_codec->m_event_mgr) );
1227 opj_bool OPJ_CALLCONV opj_end_decompress ( opj_codec_t *p_codec,
1228 opj_stream_t *p_stream)
1230 if (p_codec && p_stream) {
1231 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1232 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1234 if (! l_codec->is_decompressor) {
1238 return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
1240 &(l_codec->m_event_mgr) );
1250 opj_bool OPJ_CALLCONV opj_get_decoded_tile( opj_codec_t *p_codec,
1251 opj_stream_t *p_stream,
1252 opj_image_t *p_image,
1253 OPJ_UINT32 tile_index)
1255 if (p_codec && p_stream) {
1256 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1257 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1259 if (! l_codec->is_decompressor) {
1263 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile( l_codec->m_codec,
1266 &(l_codec->m_event_mgr),
1277 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
1278 OPJ_UINT32 res_factor )
1280 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1283 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
1287 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec,
1289 &(l_codec->m_event_mgr) );
1294 opj_bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
1296 OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32);
1297 OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32);
1298 OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
1300 /* add MCT capability */
1301 int rsiz = (int)parameters->cp_rsiz | (int)MCT;
1302 parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
1303 parameters->irreversible = 1;
1305 /* use array based MCT */
1306 parameters->tcp_mct = 2;
1307 parameters->mct_data = opj_malloc(l_mct_total_size);
1308 if (! parameters->mct_data) {
1312 memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
1313 memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size,p_dc_shift,l_dc_shift_size);
1319 * Writes a tile with the given data.
1321 * @param p_compressor the jpeg2000 codec.
1322 * @param p_tile_index the index of the tile to write. At the moment, the tiles must be written from 0 to n-1 in sequence.
1323 * @param p_data pointer to the data to write. Data is arranged in sequence, data_comp0, then data_comp1, then ... NO INTERLEAVING should be set.
1324 * @param p_data_size this value os used to make sure the data being written is correct. The size must be equal to the sum for each component of tile_width * tile_height * component_size. component_size can be 1,2 or 4 bytes,
1325 * depending on the precision of the given component.
1326 * @param p_stream the stream to write data to.
1328 opj_bool OPJ_CALLCONV opj_write_tile ( opj_codec_t *p_codec,
1329 OPJ_UINT32 p_tile_index,
1331 OPJ_UINT32 p_data_size,
1332 opj_stream_t *p_stream )
1334 if (p_codec && p_stream && p_data) {
1335 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1336 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1338 if (l_codec->is_decompressor) {
1342 return l_codec->m_codec_data.m_compression.opj_write_tile( l_codec->m_codec,
1347 &(l_codec->m_event_mgr) );