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
136 opj_decompression_t m_decompression;
137 opj_compression_t m_compression;
142 opj_event_mgr_t m_event_mgr;
143 /** Flag to indicate if the codec is used to decode or encode*/
144 opj_bool is_decompressor;
145 void (*opj_dump_codec) (void * p_codec, OPJ_INT32 info_flag, FILE* output_stream);
146 opj_codestream_info_v2_t* (*opj_get_codec_info)(void* p_codec);
147 opj_codestream_index_t* (*opj_get_codec_index)(void* p_codec);
151 /* ---------------------------------------------------------------------- */
153 * Default callback function.
156 void opj_default_callback (const char *msg, void *client_data)
160 void set_default_event_handler(opj_event_mgr_t * p_manager)
162 p_manager->m_error_data = 00;
163 p_manager->m_warning_data = 00;
164 p_manager->m_info_data = 00;
165 p_manager->error_handler = opj_default_callback;
166 p_manager->info_handler = opj_default_callback;
167 p_manager->warning_handler = opj_default_callback;
170 opj_bool OPJ_CALLCONV opj_set_info_handler( opj_codec_t * p_codec,
171 opj_msg_callback p_callback,
174 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
179 l_codec->m_event_mgr.info_handler = p_callback;
180 l_codec->m_event_mgr.m_info_data = p_user_data;
185 opj_bool OPJ_CALLCONV opj_set_warning_handler( opj_codec_t * p_codec,
186 opj_msg_callback p_callback,
189 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
194 l_codec->m_event_mgr.warning_handler = p_callback;
195 l_codec->m_event_mgr.m_warning_data = p_user_data;
200 opj_bool OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec,
201 opj_msg_callback p_callback,
204 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
209 l_codec->m_event_mgr.error_handler = p_callback;
210 l_codec->m_event_mgr.m_error_data = p_user_data;
215 /* ---------------------------------------------------------------------- */
218 OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
220 OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
221 return l_nb_read ? l_nb_read : -1;
224 OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
226 OPJ_OFF_T file_length = 0;
228 OPJ_FSEEK(p_file, 0, SEEK_END);
229 file_length = (OPJ_UINT64)OPJ_FTELL(p_file);
230 OPJ_FSEEK(p_file, 0, SEEK_SET);
235 OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
237 return fwrite(p_buffer,1,p_nb_bytes,p_file);
240 OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
242 if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
249 opj_bool opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
251 if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
258 /* ---------------------------------------------------------------------- */
262 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
264 OPJ_ARG_NOT_USED(lpReserved);
265 OPJ_ARG_NOT_USED(hModule);
267 switch (ul_reason_for_call) {
268 case DLL_PROCESS_ATTACH :
270 case DLL_PROCESS_DETACH :
272 case DLL_THREAD_ATTACH :
273 case DLL_THREAD_DETACH :
279 #endif /* OPJ_STATIC */
282 /* ---------------------------------------------------------------------- */
285 const char* OPJ_CALLCONV opj_version(void) {
286 return PACKAGE_VERSION;
289 /* ---------------------------------------------------------------------- */
290 /* DECOMPRESSION FUNCTIONS*/
292 opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
294 opj_codec_private_t *l_codec = 00;
296 l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
300 memset(l_codec, 0, sizeof(opj_codec_private_t));
302 l_codec->is_decompressor = 1;
306 l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
308 l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
310 l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
312 l_codec->m_codec_data.m_decompression.opj_decode =
313 (opj_bool (*) ( void *,
314 struct opj_stream_private *,
315 opj_image_t*, struct opj_event_mgr * )) j2k_decode_v2;
317 l_codec->m_codec_data.m_decompression.opj_end_decompress =
318 (opj_bool (*) ( void *,
319 struct opj_stream_private *,
320 struct opj_event_mgr *)) j2k_end_decompress;
322 l_codec->m_codec_data.m_decompression.opj_read_header =
323 (opj_bool (*) ( struct opj_stream_private *,
326 struct opj_event_mgr * )) j2k_read_header;
328 l_codec->m_codec_data.m_decompression.opj_destroy =
329 (void (*) (void *))j2k_destroy;
331 l_codec->m_codec_data.m_decompression.opj_setup_decoder =
332 (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
334 l_codec->m_codec_data.m_decompression.opj_read_tile_header =
335 (opj_bool (*) ( void *,
338 OPJ_INT32*, OPJ_INT32*,
339 OPJ_INT32*, OPJ_INT32*,
342 struct opj_stream_private *,
343 struct opj_event_mgr * )) j2k_read_tile_header;
345 l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
346 (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
348 l_codec->m_codec_data.m_decompression.opj_set_decode_area =
349 (opj_bool (*) (void *, opj_image_t*, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
351 l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
352 opj_stream_private_t *p_cio,
353 opj_image_t *p_image,
354 struct opj_event_mgr * p_manager,
355 OPJ_UINT32 tile_index)) j2k_get_tile;
357 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
358 OPJ_UINT32 res_factor,
359 struct opj_event_mgr * p_manager)) j2k_set_decoded_resolution_factor;
361 l_codec->m_codec = j2k_create_decompress_v2();
363 if (! l_codec->m_codec) {
371 /* get a JP2 decoder handle */
372 l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
374 l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
376 l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
378 l_codec->m_codec_data.m_decompression.opj_decode =
379 (opj_bool (*) ( void *,
380 struct opj_stream_private *,
382 struct opj_event_mgr * )) jp2_decode_v2;
384 l_codec->m_codec_data.m_decompression.opj_end_decompress = (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
386 l_codec->m_codec_data.m_decompression.opj_read_header = (opj_bool (*) (
387 struct opj_stream_private *,
390 struct opj_event_mgr * )) jp2_read_header;
392 l_codec->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) (
402 struct opj_stream_private *,
403 struct opj_event_mgr * )) jp2_read_tile_header;
405 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;
407 l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
409 l_codec->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder_v2;
411 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;
413 l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
414 opj_stream_private_t *p_cio,
415 opj_image_t *p_image,
416 struct opj_event_mgr * p_manager,
417 OPJ_UINT32 tile_index)) jp2_get_tile;
419 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
420 OPJ_UINT32 res_factor,
421 opj_event_mgr_t * p_manager)) jp2_set_decoded_resolution_factor;
423 l_codec->m_codec = jp2_create(OPJ_TRUE);
425 if (! l_codec->m_codec) {
438 set_default_event_handler(&(l_codec->m_event_mgr));
439 return (opj_codec_t*) l_codec;
442 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
444 memset(parameters, 0, sizeof(opj_dparameters_t));
445 /* default decoding parameters */
446 parameters->cp_layer = 0;
447 parameters->cp_reduce = 0;
448 parameters->cp_limit_decoding = NO_LIMITATION;
450 parameters->decod_format = -1;
451 parameters->cod_format = -1;
452 parameters->flags = 0;
455 parameters->jpwl_correct = OPJ_FALSE;
456 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
457 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
458 #endif /* USE_JPWL */
463 opj_bool OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
464 opj_dparameters_t *parameters
467 if (p_codec && parameters) {
468 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
470 if (! l_codec->is_decompressor) {
471 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");
475 l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
482 /* ---------------------------------------------------------------------- */
483 /* COMPRESSION FUNCTIONS*/
485 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
487 opj_codec_private_t *l_codec = 00;
489 l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
493 memset(l_codec, 0, sizeof(opj_codec_private_t));
495 l_codec->is_decompressor = 0;
499 l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
500 struct opj_stream_private *,
501 struct opj_event_mgr * )) j2k_encode_v2;
503 l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) ( void *,
504 struct opj_stream_private *,
505 struct opj_event_mgr *)) j2k_end_compress;
507 l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
508 struct opj_stream_private *,
510 struct opj_event_mgr *)) j2k_start_compress;
512 l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
516 struct opj_stream_private *,
517 struct opj_event_mgr *) ) j2k_write_tile;
519 l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy;
521 l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
524 struct opj_event_mgr * )) j2k_setup_encoder_v2;
526 l_codec->m_codec = j2k_create_compress_v2();
527 if (! l_codec->m_codec) {
535 /* get a JP2 decoder handle */
536 l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
537 struct opj_stream_private *,
538 struct opj_event_mgr * )) opj_jp2_encode_v2;
540 l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) ( void *,
541 struct opj_stream_private *,
542 struct opj_event_mgr *)) jp2_end_compress;
544 l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
545 struct opj_stream_private *,
547 struct opj_event_mgr *)) jp2_start_compress;
549 l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
553 struct opj_stream_private *,
554 struct opj_event_mgr *)) jp2_write_tile;
556 l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy;
558 l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
561 struct opj_event_mgr * )) jp2_setup_encoder;
563 l_codec->m_codec = jp2_create(OPJ_FALSE);
564 if (! l_codec->m_codec) {
578 set_default_event_handler(&(l_codec->m_event_mgr));
579 return (opj_codec_t*) l_codec;
582 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
584 memset(parameters, 0, sizeof(opj_cparameters_t));
585 /* default coding parameters */
586 parameters->cp_cinema = OFF;
587 parameters->max_comp_size = 0;
588 parameters->numresolution = 6;
589 parameters->cp_rsiz = STD_RSIZ;
590 parameters->cblockw_init = 64;
591 parameters->cblockh_init = 64;
592 parameters->prog_order = LRCP;
593 parameters->roi_compno = -1; /* no ROI */
594 parameters->subsampling_dx = 1;
595 parameters->subsampling_dy = 1;
596 parameters->tp_on = 0;
597 parameters->decod_format = -1;
598 parameters->cod_format = -1;
599 parameters->tcp_rates[0] = 0;
600 parameters->tcp_numlayers = 0;
601 parameters->cp_disto_alloc = 0;
602 parameters->cp_fixed_alloc = 0;
603 parameters->cp_fixed_quality = 0;
604 parameters->jpip_on = OPJ_FALSE;
607 parameters->jpwl_epc_on = OPJ_FALSE;
608 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
611 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
612 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
613 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
618 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
619 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
620 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
621 parameters->jpwl_pprot[i] = 0; /* absent */
624 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
625 parameters->jpwl_sens_addr = 0; /* 0 means auto */
626 parameters->jpwl_sens_range = 0; /* 0 means packet */
627 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
630 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
631 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
632 parameters->jpwl_sens_TPH[i] = -1; /* absent */
635 #endif /* USE_JPWL */
640 opj_bool OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
641 opj_cparameters_t *parameters,
642 opj_image_t *p_image)
644 if (p_codec && parameters && p_image) {
645 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
647 if (! l_codec->is_decompressor) {
648 l_codec->m_codec_data.m_compression.opj_setup_encoder( l_codec->m_codec,
651 &(l_codec->m_event_mgr) );
659 opj_bool OPJ_CALLCONV opj_start_compress ( opj_codec_t *p_codec,
660 opj_image_t * p_image,
661 opj_stream_t *p_stream)
663 if (p_codec && p_stream) {
664 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
665 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
667 if (! l_codec->is_decompressor) {
668 return l_codec->m_codec_data.m_compression.opj_start_compress( l_codec->m_codec,
671 &(l_codec->m_event_mgr));
678 opj_bool OPJ_CALLCONV opj_encode_v2(opj_codec_t *p_info, opj_stream_t *p_stream)
680 if (p_info && p_stream) {
681 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
682 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
684 if (! l_codec->is_decompressor) {
685 l_codec->m_codec_data.m_compression.opj_encode( l_codec->m_codec,
687 &(l_codec->m_event_mgr));
696 opj_bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
697 opj_stream_t *p_stream)
699 if (p_codec && p_stream) {
700 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
701 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
703 if (! l_codec->is_decompressor) {
704 return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
706 &(l_codec->m_event_mgr));
716 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
719 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
720 opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
721 opj_free(tile_info->thresh);
722 opj_free(tile_info->packet);
723 opj_free(tile_info->tp);
724 opj_free(tile_info->marker);
726 opj_free(cstr_info->tile);
727 opj_free(cstr_info->marker);
728 opj_free(cstr_info->numdecompos);
733 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
734 opj_codec_t *p_codec,
735 opj_image_t **p_image )
737 if (p_codec && p_stream) {
738 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
739 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
741 if(! l_codec->is_decompressor) {
742 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");
746 return l_codec->m_codec_data.m_decompression.opj_read_header( l_stream,
749 &(l_codec->m_event_mgr) );
756 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
759 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
761 if (l_codec->is_decompressor) {
762 l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
765 l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
768 l_codec->m_codec = 00;
774 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
776 * @param p_codec the jpeg2000 codec.
777 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
778 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
779 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
780 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
782 * @return true if the area could be set.
784 opj_bool OPJ_CALLCONV opj_set_decode_area( opj_codec_t *p_codec,
785 opj_image_t* p_image,
786 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
787 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
791 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
793 if (! l_codec->is_decompressor) {
797 return l_codec->m_codec_data.m_decompression.opj_set_decode_area( l_codec->m_codec,
799 p_start_x, p_start_y,
801 &(l_codec->m_event_mgr) );
807 * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
808 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
810 * @param p_codec the jpeg2000 codec.
811 * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success.
812 * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
813 * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
814 * as depicted in opj_write_tile.
815 * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image).
816 * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image).
817 * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image).
818 * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image).
819 * @param p_nb_comps pointer to a value that will hold the number of components in the tile.
820 * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the
821 * codestream is over at the time of the call, the value will be set to false. The user should then stop
823 * @param p_stream the stream to decode.
824 * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true.
825 * returning false may be the result of a shortage of memory or an internal error.
827 opj_bool OPJ_CALLCONV opj_read_tile_header( opj_codec_t *p_codec,
828 opj_stream_t * p_stream,
829 OPJ_UINT32 * p_tile_index,
830 OPJ_UINT32 * p_data_size,
831 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
832 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
833 OPJ_UINT32 * p_nb_comps,
834 opj_bool * p_should_go_on)
836 if (p_codec && p_stream && p_data_size && p_tile_index) {
837 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
838 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
840 if (! l_codec->is_decompressor) {
844 return l_codec->m_codec_data.m_decompression.opj_read_tile_header( l_codec->m_codec,
847 p_tile_x0, p_tile_y0,
848 p_tile_x1, p_tile_y1,
852 &(l_codec->m_event_mgr));
858 * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
859 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
861 * @param p_codec the jpeg2000 codec.
862 * @param p_tile_index the index of the tile being decoded, this should be the value set by opj_read_tile_header.
863 * @param p_data pointer to a memory block that will hold the decoded data.
864 * @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.
865 * @param p_stream the stream to decode.
867 * @return true if the data could be decoded.
869 opj_bool OPJ_CALLCONV opj_decode_tile_data( opj_codec_t *p_codec,
870 OPJ_UINT32 p_tile_index,
872 OPJ_UINT32 p_data_size,
873 opj_stream_t *p_stream
876 if (p_codec && p_data && p_stream) {
877 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
878 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
880 if (! l_codec->is_decompressor) {
884 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data( l_codec->m_codec,
889 &(l_codec->m_event_mgr) );
898 void OPJ_CALLCONV opj_dump_codec( opj_codec_t *p_codec,
903 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
905 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
909 fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
917 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
920 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
922 return l_codec->opj_get_codec_info(l_codec->m_codec);
932 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t **cstr_info) {
935 if ((*cstr_info)->m_default_tile_info.tccp_info){
936 opj_free((*cstr_info)->m_default_tile_info.tccp_info);
939 if ((*cstr_info)->tile_info){
940 /* FIXME not used for the moment*/
943 opj_free((*cstr_info));
952 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
955 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
957 return l_codec->opj_get_codec_index(l_codec->m_codec);
967 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
970 j2k_destroy_cstr_index(*p_cstr_index);
971 (*p_cstr_index) = NULL;
979 opj_bool OPJ_CALLCONV opj_decode( opj_codec_t *p_codec,
980 opj_stream_t *p_stream,
981 opj_image_t* p_image)
983 if (p_codec && p_stream) {
984 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
985 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
987 if (! l_codec->is_decompressor) {
991 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
994 &(l_codec->m_event_mgr) );
1004 opj_bool OPJ_CALLCONV opj_end_decompress ( opj_codec_t *p_codec,
1005 opj_stream_t *p_stream)
1007 if (p_codec && p_stream) {
1008 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1009 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1011 if (! l_codec->is_decompressor) {
1015 return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
1017 &(l_codec->m_event_mgr) );
1027 opj_bool OPJ_CALLCONV opj_get_decoded_tile( opj_codec_t *p_codec,
1028 opj_stream_t *p_stream,
1029 opj_image_t *p_image,
1030 OPJ_UINT32 tile_index)
1032 if (p_codec && p_stream) {
1033 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1034 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1036 if (! l_codec->is_decompressor) {
1040 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile( l_codec->m_codec,
1043 &(l_codec->m_event_mgr),
1054 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
1055 OPJ_UINT32 res_factor )
1057 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1060 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
1064 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec,
1066 &(l_codec->m_event_mgr) );
1071 opj_bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
1073 OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32);
1074 OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32);
1075 OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
1077 /* add MCT capability */
1078 int rsiz = (int)parameters->cp_rsiz | (int)MCT;
1079 parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
1080 parameters->irreversible = 1;
1082 /* use array based MCT */
1083 parameters->tcp_mct = 2;
1084 parameters->mct_data = opj_malloc(l_mct_total_size);
1085 if (! parameters->mct_data) {
1089 memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
1090 memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size,p_dc_shift,l_dc_shift_size);
1096 * Writes a tile with the given data.
1098 * @param p_compressor the jpeg2000 codec.
1099 * @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.
1100 * @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.
1101 * @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,
1102 * depending on the precision of the given component.
1103 * @param p_stream the stream to write data to.
1105 opj_bool OPJ_CALLCONV opj_write_tile ( opj_codec_t *p_codec,
1106 OPJ_UINT32 p_tile_index,
1108 OPJ_UINT32 p_data_size,
1109 opj_stream_t *p_stream )
1111 if (p_codec && p_stream && p_data) {
1112 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1113 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1115 if (l_codec->is_decompressor) {
1119 return l_codec->m_codec_data.m_compression.opj_write_tile( l_codec->m_codec,
1124 &(l_codec->m_event_mgr) );
1130 /* ---------------------------------------------------------------------- */
1133 * Sets the stream to be a file stream. The FILE must have been open previously.
1134 * @param p_stream the stream to modify
1135 * @param p_file handler to an already open file.
1137 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
1139 return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
1142 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream ( FILE * p_file,
1144 opj_bool p_is_read_stream)
1146 opj_stream_t* l_stream = 00;
1152 l_stream = opj_stream_create(p_size,p_is_read_stream);
1157 opj_stream_set_user_data(l_stream, p_file);
1158 opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
1159 opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
1160 opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
1161 opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
1162 opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);