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 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
483 opj_codec_t *p_codec,
484 opj_image_t **p_image )
486 if (p_codec && p_stream) {
487 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
488 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
490 if(! l_codec->is_decompressor) {
491 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");
495 return l_codec->m_codec_data.m_decompression.opj_read_header( l_stream,
498 &(l_codec->m_event_mgr) );
508 opj_bool OPJ_CALLCONV opj_decode( opj_codec_t *p_codec,
509 opj_stream_t *p_stream,
510 opj_image_t* p_image)
512 if (p_codec && p_stream) {
513 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
514 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
516 if (! l_codec->is_decompressor) {
520 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
523 &(l_codec->m_event_mgr) );
531 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
533 * @param p_codec the jpeg2000 codec.
534 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
535 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
536 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
537 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
539 * @return true if the area could be set.
541 opj_bool OPJ_CALLCONV opj_set_decode_area( opj_codec_t *p_codec,
542 opj_image_t* p_image,
543 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
544 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
548 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
550 if (! l_codec->is_decompressor) {
554 return l_codec->m_codec_data.m_decompression.opj_set_decode_area( l_codec->m_codec,
556 p_start_x, p_start_y,
558 &(l_codec->m_event_mgr) );
564 * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
565 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
567 * @param p_codec the jpeg2000 codec.
568 * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success.
569 * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
570 * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
571 * as depicted in opj_write_tile.
572 * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image).
573 * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image).
574 * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image).
575 * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image).
576 * @param p_nb_comps pointer to a value that will hold the number of components in the tile.
577 * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the
578 * codestream is over at the time of the call, the value will be set to false. The user should then stop
580 * @param p_stream the stream to decode.
581 * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true.
582 * returning false may be the result of a shortage of memory or an internal error.
584 opj_bool OPJ_CALLCONV opj_read_tile_header( opj_codec_t *p_codec,
585 opj_stream_t * p_stream,
586 OPJ_UINT32 * p_tile_index,
587 OPJ_UINT32 * p_data_size,
588 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
589 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
590 OPJ_UINT32 * p_nb_comps,
591 opj_bool * p_should_go_on)
593 if (p_codec && p_stream && p_data_size && p_tile_index) {
594 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
595 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
597 if (! l_codec->is_decompressor) {
601 return l_codec->m_codec_data.m_decompression.opj_read_tile_header( l_codec->m_codec,
604 p_tile_x0, p_tile_y0,
605 p_tile_x1, p_tile_y1,
609 &(l_codec->m_event_mgr));
615 * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
616 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
618 * @param p_codec the jpeg2000 codec.
619 * @param p_tile_index the index of the tile being decoded, this should be the value set by opj_read_tile_header.
620 * @param p_data pointer to a memory block that will hold the decoded data.
621 * @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.
622 * @param p_stream the stream to decode.
624 * @return true if the data could be decoded.
626 opj_bool OPJ_CALLCONV opj_decode_tile_data( opj_codec_t *p_codec,
627 OPJ_UINT32 p_tile_index,
629 OPJ_UINT32 p_data_size,
630 opj_stream_t *p_stream
633 if (p_codec && p_data && p_stream) {
634 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
635 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
637 if (! l_codec->is_decompressor) {
641 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data( l_codec->m_codec,
646 &(l_codec->m_event_mgr) );
655 opj_bool OPJ_CALLCONV opj_get_decoded_tile( opj_codec_t *p_codec,
656 opj_stream_t *p_stream,
657 opj_image_t *p_image,
658 OPJ_UINT32 tile_index)
660 if (p_codec && p_stream) {
661 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
662 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
664 if (! l_codec->is_decompressor) {
668 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile( l_codec->m_codec,
671 &(l_codec->m_event_mgr),
682 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
683 OPJ_UINT32 res_factor )
685 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
688 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
692 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec,
694 &(l_codec->m_event_mgr) );
698 /* ---------------------------------------------------------------------- */
699 /* COMPRESSION FUNCTIONS*/
701 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
703 opj_codec_private_t *l_codec = 00;
705 l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
709 memset(l_codec, 0, sizeof(opj_codec_private_t));
711 l_codec->is_decompressor = 0;
715 l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
716 struct opj_stream_private *,
717 struct opj_event_mgr * )) j2k_encode_v2;
719 l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) ( void *,
720 struct opj_stream_private *,
721 struct opj_event_mgr *)) j2k_end_compress;
723 l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
724 struct opj_stream_private *,
726 struct opj_event_mgr *)) j2k_start_compress;
728 l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
732 struct opj_stream_private *,
733 struct opj_event_mgr *) ) j2k_write_tile;
735 l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy;
737 l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
740 struct opj_event_mgr * )) j2k_setup_encoder_v2;
742 l_codec->m_codec = j2k_create_compress_v2();
743 if (! l_codec->m_codec) {
751 /* get a JP2 decoder handle */
752 l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
753 struct opj_stream_private *,
754 struct opj_event_mgr * )) opj_jp2_encode_v2;
756 l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) ( void *,
757 struct opj_stream_private *,
758 struct opj_event_mgr *)) jp2_end_compress;
760 l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
761 struct opj_stream_private *,
763 struct opj_event_mgr *)) jp2_start_compress;
765 l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
769 struct opj_stream_private *,
770 struct opj_event_mgr *)) jp2_write_tile;
772 l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy;
774 l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
777 struct opj_event_mgr * )) jp2_setup_encoder;
779 l_codec->m_codec = jp2_create(OPJ_FALSE);
780 if (! l_codec->m_codec) {
794 set_default_event_handler(&(l_codec->m_event_mgr));
795 return (opj_codec_t*) l_codec;
798 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
800 memset(parameters, 0, sizeof(opj_cparameters_t));
801 /* default coding parameters */
802 parameters->cp_cinema = OFF;
803 parameters->max_comp_size = 0;
804 parameters->numresolution = 6;
805 parameters->cp_rsiz = STD_RSIZ;
806 parameters->cblockw_init = 64;
807 parameters->cblockh_init = 64;
808 parameters->prog_order = LRCP;
809 parameters->roi_compno = -1; /* no ROI */
810 parameters->subsampling_dx = 1;
811 parameters->subsampling_dy = 1;
812 parameters->tp_on = 0;
813 parameters->decod_format = -1;
814 parameters->cod_format = -1;
815 parameters->tcp_rates[0] = 0;
816 parameters->tcp_numlayers = 0;
817 parameters->cp_disto_alloc = 0;
818 parameters->cp_fixed_alloc = 0;
819 parameters->cp_fixed_quality = 0;
820 parameters->jpip_on = OPJ_FALSE;
823 parameters->jpwl_epc_on = OPJ_FALSE;
824 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
827 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
828 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
829 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
834 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
835 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
836 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
837 parameters->jpwl_pprot[i] = 0; /* absent */
840 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
841 parameters->jpwl_sens_addr = 0; /* 0 means auto */
842 parameters->jpwl_sens_range = 0; /* 0 means packet */
843 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
846 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
847 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
848 parameters->jpwl_sens_TPH[i] = -1; /* absent */
851 #endif /* USE_JPWL */
856 opj_bool OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
857 opj_cparameters_t *parameters,
858 opj_image_t *p_image)
860 if (p_codec && parameters && p_image) {
861 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
863 if (! l_codec->is_decompressor) {
864 l_codec->m_codec_data.m_compression.opj_setup_encoder( l_codec->m_codec,
867 &(l_codec->m_event_mgr) );
875 opj_bool OPJ_CALLCONV opj_start_compress ( opj_codec_t *p_codec,
876 opj_image_t * p_image,
877 opj_stream_t *p_stream)
879 if (p_codec && p_stream) {
880 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
881 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
883 if (! l_codec->is_decompressor) {
884 return l_codec->m_codec_data.m_compression.opj_start_compress( l_codec->m_codec,
887 &(l_codec->m_event_mgr));
894 opj_bool OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
896 if (p_info && p_stream) {
897 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
898 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
900 if (! l_codec->is_decompressor) {
901 l_codec->m_codec_data.m_compression.opj_encode( l_codec->m_codec,
903 &(l_codec->m_event_mgr));
912 opj_bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
913 opj_stream_t *p_stream)
915 if (p_codec && p_stream) {
916 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
917 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
919 if (! l_codec->is_decompressor) {
920 return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
922 &(l_codec->m_event_mgr));
933 opj_bool OPJ_CALLCONV opj_end_decompress ( opj_codec_t *p_codec,
934 opj_stream_t *p_stream)
936 if (p_codec && p_stream) {
937 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
938 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
940 if (! l_codec->is_decompressor) {
944 return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
946 &(l_codec->m_event_mgr) );
953 opj_bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
955 OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32);
956 OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32);
957 OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
959 /* add MCT capability */
960 int rsiz = (int)parameters->cp_rsiz | (int)MCT;
961 parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
962 parameters->irreversible = 1;
964 /* use array based MCT */
965 parameters->tcp_mct = 2;
966 parameters->mct_data = opj_malloc(l_mct_total_size);
967 if (! parameters->mct_data) {
971 memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
972 memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size,p_dc_shift,l_dc_shift_size);
978 * Writes a tile with the given data.
980 * @param p_compressor the jpeg2000 codec.
981 * @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.
982 * @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.
983 * @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,
984 * depending on the precision of the given component.
985 * @param p_stream the stream to write data to.
987 opj_bool OPJ_CALLCONV opj_write_tile ( opj_codec_t *p_codec,
988 OPJ_UINT32 p_tile_index,
990 OPJ_UINT32 p_data_size,
991 opj_stream_t *p_stream )
993 if (p_codec && p_stream && p_data) {
994 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
995 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
997 if (l_codec->is_decompressor) {
1001 return l_codec->m_codec_data.m_compression.opj_write_tile( l_codec->m_codec,
1006 &(l_codec->m_event_mgr) );
1014 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
1017 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
1018 opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
1019 opj_free(tile_info->thresh);
1020 opj_free(tile_info->packet);
1021 opj_free(tile_info->tp);
1022 opj_free(tile_info->marker);
1024 opj_free(cstr_info->tile);
1025 opj_free(cstr_info->marker);
1026 opj_free(cstr_info->numdecompos);
1034 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
1037 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1039 if (l_codec->is_decompressor) {
1040 l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
1043 l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
1046 l_codec->m_codec = 00;
1057 void OPJ_CALLCONV opj_dump_codec( opj_codec_t *p_codec,
1058 OPJ_INT32 info_flag,
1059 FILE* output_stream)
1062 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1064 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
1068 fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
1076 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
1079 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1081 return l_codec->opj_get_codec_info(l_codec->m_codec);
1091 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t **cstr_info) {
1094 if ((*cstr_info)->m_default_tile_info.tccp_info){
1095 opj_free((*cstr_info)->m_default_tile_info.tccp_info);
1098 if ((*cstr_info)->tile_info){
1099 /* FIXME not used for the moment*/
1102 opj_free((*cstr_info));
1103 (*cstr_info) = NULL;
1111 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
1114 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1116 return l_codec->opj_get_codec_index(l_codec->m_codec);
1126 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
1129 j2k_destroy_cstr_index(*p_cstr_index);
1130 (*p_cstr_index) = NULL;
1138 /* ---------------------------------------------------------------------- */
1141 * Sets the stream to be a file stream. The FILE must have been open previously.
1142 * @param p_stream the stream to modify
1143 * @param p_file handler to an already open file.
1145 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
1147 return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
1150 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream ( FILE * p_file,
1152 opj_bool p_is_read_stream)
1154 opj_stream_t* l_stream = 00;
1160 l_stream = opj_stream_create(p_size,p_is_read_stream);
1165 opj_stream_set_user_data(l_stream, p_file);
1166 opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
1167 opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
1168 opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
1169 opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
1170 opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);