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 /* ---------------------------------------------------------------------- */
152 /* Functions to set info_handler */
154 opj_bool OPJ_CALLCONV opj_set_info_handler( opj_codec_t * p_codec,
155 opj_msg_callback p_callback,
158 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
163 l_codec->m_event_mgr.info_handler = p_callback;
164 l_codec->m_event_mgr.m_info_data = p_user_data;
169 opj_bool OPJ_CALLCONV opj_set_warning_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.warning_handler = p_callback;
179 l_codec->m_event_mgr.m_warning_data = p_user_data;
184 opj_bool OPJ_CALLCONV opj_set_error_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.error_handler = p_callback;
194 l_codec->m_event_mgr.m_error_data = p_user_data;
199 /* ---------------------------------------------------------------------- */
202 OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
204 OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
205 return l_nb_read ? l_nb_read : -1;
208 OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
210 OPJ_OFF_T file_length = 0;
212 OPJ_FSEEK(p_file, 0, SEEK_END);
213 file_length = (OPJ_UINT64)OPJ_FTELL(p_file);
214 OPJ_FSEEK(p_file, 0, SEEK_SET);
219 OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
221 return fwrite(p_buffer,1,p_nb_bytes,p_file);
224 OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
226 if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
233 opj_bool opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
235 if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
242 /* ---------------------------------------------------------------------- */
246 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
248 OPJ_ARG_NOT_USED(lpReserved);
249 OPJ_ARG_NOT_USED(hModule);
251 switch (ul_reason_for_call) {
252 case DLL_PROCESS_ATTACH :
254 case DLL_PROCESS_DETACH :
256 case DLL_THREAD_ATTACH :
257 case DLL_THREAD_DETACH :
263 #endif /* OPJ_STATIC */
266 /* ---------------------------------------------------------------------- */
269 const char* OPJ_CALLCONV opj_version(void) {
270 return PACKAGE_VERSION;
273 /* ---------------------------------------------------------------------- */
274 /* DECOMPRESSION FUNCTIONS*/
276 opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
278 opj_codec_private_t *l_codec = 00;
280 l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
284 memset(l_codec, 0, sizeof(opj_codec_private_t));
286 l_codec->is_decompressor = 1;
290 l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
292 l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
294 l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
296 l_codec->m_codec_data.m_decompression.opj_decode =
297 (opj_bool (*) ( void *,
298 struct opj_stream_private *,
299 opj_image_t*, struct opj_event_mgr * )) j2k_decode_v2;
301 l_codec->m_codec_data.m_decompression.opj_end_decompress =
302 (opj_bool (*) ( void *,
303 struct opj_stream_private *,
304 struct opj_event_mgr *)) j2k_end_decompress;
306 l_codec->m_codec_data.m_decompression.opj_read_header =
307 (opj_bool (*) ( struct opj_stream_private *,
310 struct opj_event_mgr * )) j2k_read_header;
312 l_codec->m_codec_data.m_decompression.opj_destroy =
313 (void (*) (void *))j2k_destroy;
315 l_codec->m_codec_data.m_decompression.opj_setup_decoder =
316 (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
318 l_codec->m_codec_data.m_decompression.opj_read_tile_header =
319 (opj_bool (*) ( void *,
322 OPJ_INT32*, OPJ_INT32*,
323 OPJ_INT32*, OPJ_INT32*,
326 struct opj_stream_private *,
327 struct opj_event_mgr * )) j2k_read_tile_header;
329 l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
330 (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
332 l_codec->m_codec_data.m_decompression.opj_set_decode_area =
333 (opj_bool (*) (void *, opj_image_t*, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
335 l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
336 opj_stream_private_t *p_cio,
337 opj_image_t *p_image,
338 struct opj_event_mgr * p_manager,
339 OPJ_UINT32 tile_index)) j2k_get_tile;
341 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
342 OPJ_UINT32 res_factor,
343 struct opj_event_mgr * p_manager)) j2k_set_decoded_resolution_factor;
345 l_codec->m_codec = opj_j2k_create_decompress();
347 if (! l_codec->m_codec) {
355 /* get a JP2 decoder handle */
356 l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
358 l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
360 l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
362 l_codec->m_codec_data.m_decompression.opj_decode =
363 (opj_bool (*) ( void *,
364 struct opj_stream_private *,
366 struct opj_event_mgr * )) opj_jp2_decode;
368 l_codec->m_codec_data.m_decompression.opj_end_decompress = (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
370 l_codec->m_codec_data.m_decompression.opj_read_header = (opj_bool (*) (
371 struct opj_stream_private *,
374 struct opj_event_mgr * )) jp2_read_header;
376 l_codec->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) (
386 struct opj_stream_private *,
387 struct opj_event_mgr * )) jp2_read_tile_header;
389 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;
391 l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
393 l_codec->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) opj_jp2_setup_decoder;
395 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;
397 l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
398 opj_stream_private_t *p_cio,
399 opj_image_t *p_image,
400 struct opj_event_mgr * p_manager,
401 OPJ_UINT32 tile_index)) jp2_get_tile;
403 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
404 OPJ_UINT32 res_factor,
405 opj_event_mgr_t * p_manager)) jp2_set_decoded_resolution_factor;
407 l_codec->m_codec = jp2_create(OPJ_TRUE);
409 if (! l_codec->m_codec) {
422 opj_set_default_event_handler(&(l_codec->m_event_mgr));
423 return (opj_codec_t*) l_codec;
426 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
428 memset(parameters, 0, sizeof(opj_dparameters_t));
429 /* default decoding parameters */
430 parameters->cp_layer = 0;
431 parameters->cp_reduce = 0;
432 parameters->cp_limit_decoding = NO_LIMITATION;
434 parameters->decod_format = -1;
435 parameters->cod_format = -1;
436 parameters->flags = 0;
439 parameters->jpwl_correct = OPJ_FALSE;
440 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
441 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
442 #endif /* USE_JPWL */
447 opj_bool OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
448 opj_dparameters_t *parameters
451 if (p_codec && parameters) {
452 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
454 if (! l_codec->is_decompressor) {
455 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");
459 l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
466 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
467 opj_codec_t *p_codec,
468 opj_image_t **p_image )
470 if (p_codec && p_stream) {
471 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
472 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
474 if(! l_codec->is_decompressor) {
475 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");
479 return l_codec->m_codec_data.m_decompression.opj_read_header( l_stream,
482 &(l_codec->m_event_mgr) );
492 opj_bool OPJ_CALLCONV opj_decode( opj_codec_t *p_codec,
493 opj_stream_t *p_stream,
494 opj_image_t* p_image)
496 if (p_codec && p_stream) {
497 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
498 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
500 if (! l_codec->is_decompressor) {
504 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
507 &(l_codec->m_event_mgr) );
515 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
517 * @param p_codec the jpeg2000 codec.
518 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
519 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
520 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
521 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
523 * @return true if the area could be set.
525 opj_bool OPJ_CALLCONV opj_set_decode_area( opj_codec_t *p_codec,
526 opj_image_t* p_image,
527 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
528 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
532 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
534 if (! l_codec->is_decompressor) {
538 return l_codec->m_codec_data.m_decompression.opj_set_decode_area( l_codec->m_codec,
540 p_start_x, p_start_y,
542 &(l_codec->m_event_mgr) );
548 * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
549 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
551 * @param p_codec the jpeg2000 codec.
552 * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success.
553 * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
554 * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
555 * as depicted in opj_write_tile.
556 * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image).
557 * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image).
558 * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image).
559 * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image).
560 * @param p_nb_comps pointer to a value that will hold the number of components in the tile.
561 * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the
562 * codestream is over at the time of the call, the value will be set to false. The user should then stop
564 * @param p_stream the stream to decode.
565 * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true.
566 * returning false may be the result of a shortage of memory or an internal error.
568 opj_bool OPJ_CALLCONV opj_read_tile_header( opj_codec_t *p_codec,
569 opj_stream_t * p_stream,
570 OPJ_UINT32 * p_tile_index,
571 OPJ_UINT32 * p_data_size,
572 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
573 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
574 OPJ_UINT32 * p_nb_comps,
575 opj_bool * p_should_go_on)
577 if (p_codec && p_stream && p_data_size && p_tile_index) {
578 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
579 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
581 if (! l_codec->is_decompressor) {
585 return l_codec->m_codec_data.m_decompression.opj_read_tile_header( l_codec->m_codec,
588 p_tile_x0, p_tile_y0,
589 p_tile_x1, p_tile_y1,
593 &(l_codec->m_event_mgr));
599 * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
600 * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
602 * @param p_codec the jpeg2000 codec.
603 * @param p_tile_index the index of the tile being decoded, this should be the value set by opj_read_tile_header.
604 * @param p_data pointer to a memory block that will hold the decoded data.
605 * @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.
606 * @param p_stream the stream to decode.
608 * @return true if the data could be decoded.
610 opj_bool OPJ_CALLCONV opj_decode_tile_data( opj_codec_t *p_codec,
611 OPJ_UINT32 p_tile_index,
613 OPJ_UINT32 p_data_size,
614 opj_stream_t *p_stream
617 if (p_codec && p_data && p_stream) {
618 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
619 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
621 if (! l_codec->is_decompressor) {
625 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data( l_codec->m_codec,
630 &(l_codec->m_event_mgr) );
639 opj_bool OPJ_CALLCONV opj_get_decoded_tile( opj_codec_t *p_codec,
640 opj_stream_t *p_stream,
641 opj_image_t *p_image,
642 OPJ_UINT32 tile_index)
644 if (p_codec && p_stream) {
645 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
646 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
648 if (! l_codec->is_decompressor) {
652 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile( l_codec->m_codec,
655 &(l_codec->m_event_mgr),
666 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
667 OPJ_UINT32 res_factor )
669 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
672 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
676 l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec,
678 &(l_codec->m_event_mgr) );
682 /* ---------------------------------------------------------------------- */
683 /* COMPRESSION FUNCTIONS*/
685 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
687 opj_codec_private_t *l_codec = 00;
689 l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
693 memset(l_codec, 0, sizeof(opj_codec_private_t));
695 l_codec->is_decompressor = 0;
699 l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
700 struct opj_stream_private *,
701 struct opj_event_mgr * )) j2k_encode_v2;
703 l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) ( void *,
704 struct opj_stream_private *,
705 struct opj_event_mgr *)) j2k_end_compress;
707 l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
708 struct opj_stream_private *,
710 struct opj_event_mgr *)) j2k_start_compress;
712 l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
716 struct opj_stream_private *,
717 struct opj_event_mgr *) ) j2k_write_tile;
719 l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy;
721 l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
724 struct opj_event_mgr * )) j2k_setup_encoder_v2;
726 l_codec->m_codec = j2k_create_compress_v2();
727 if (! l_codec->m_codec) {
735 /* get a JP2 decoder handle */
736 l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
737 struct opj_stream_private *,
738 struct opj_event_mgr * )) opj_jp2_encode_v2;
740 l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) ( void *,
741 struct opj_stream_private *,
742 struct opj_event_mgr *)) jp2_end_compress;
744 l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
745 struct opj_stream_private *,
747 struct opj_event_mgr *)) jp2_start_compress;
749 l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
753 struct opj_stream_private *,
754 struct opj_event_mgr *)) jp2_write_tile;
756 l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy;
758 l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
761 struct opj_event_mgr * )) jp2_setup_encoder;
763 l_codec->m_codec = jp2_create(OPJ_FALSE);
764 if (! l_codec->m_codec) {
778 opj_set_default_event_handler(&(l_codec->m_event_mgr));
779 return (opj_codec_t*) l_codec;
782 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
784 memset(parameters, 0, sizeof(opj_cparameters_t));
785 /* default coding parameters */
786 parameters->cp_cinema = OFF;
787 parameters->max_comp_size = 0;
788 parameters->numresolution = 6;
789 parameters->cp_rsiz = STD_RSIZ;
790 parameters->cblockw_init = 64;
791 parameters->cblockh_init = 64;
792 parameters->prog_order = LRCP;
793 parameters->roi_compno = -1; /* no ROI */
794 parameters->subsampling_dx = 1;
795 parameters->subsampling_dy = 1;
796 parameters->tp_on = 0;
797 parameters->decod_format = -1;
798 parameters->cod_format = -1;
799 parameters->tcp_rates[0] = 0;
800 parameters->tcp_numlayers = 0;
801 parameters->cp_disto_alloc = 0;
802 parameters->cp_fixed_alloc = 0;
803 parameters->cp_fixed_quality = 0;
804 parameters->jpip_on = OPJ_FALSE;
807 parameters->jpwl_epc_on = OPJ_FALSE;
808 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
811 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
812 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
813 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
818 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
819 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
820 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
821 parameters->jpwl_pprot[i] = 0; /* absent */
824 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
825 parameters->jpwl_sens_addr = 0; /* 0 means auto */
826 parameters->jpwl_sens_range = 0; /* 0 means packet */
827 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
830 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
831 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
832 parameters->jpwl_sens_TPH[i] = -1; /* absent */
835 #endif /* USE_JPWL */
840 opj_bool OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
841 opj_cparameters_t *parameters,
842 opj_image_t *p_image)
844 if (p_codec && parameters && p_image) {
845 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
847 if (! l_codec->is_decompressor) {
848 l_codec->m_codec_data.m_compression.opj_setup_encoder( l_codec->m_codec,
851 &(l_codec->m_event_mgr) );
859 opj_bool OPJ_CALLCONV opj_start_compress ( opj_codec_t *p_codec,
860 opj_image_t * p_image,
861 opj_stream_t *p_stream)
863 if (p_codec && p_stream) {
864 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
865 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
867 if (! l_codec->is_decompressor) {
868 return l_codec->m_codec_data.m_compression.opj_start_compress( l_codec->m_codec,
871 &(l_codec->m_event_mgr));
878 opj_bool OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
880 if (p_info && p_stream) {
881 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
882 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
884 if (! l_codec->is_decompressor) {
885 l_codec->m_codec_data.m_compression.opj_encode( l_codec->m_codec,
887 &(l_codec->m_event_mgr));
896 opj_bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
897 opj_stream_t *p_stream)
899 if (p_codec && p_stream) {
900 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
901 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
903 if (! l_codec->is_decompressor) {
904 return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
906 &(l_codec->m_event_mgr));
917 opj_bool OPJ_CALLCONV opj_end_decompress ( opj_codec_t *p_codec,
918 opj_stream_t *p_stream)
920 if (p_codec && p_stream) {
921 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
922 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
924 if (! l_codec->is_decompressor) {
928 return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
930 &(l_codec->m_event_mgr) );
937 opj_bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
939 OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32);
940 OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32);
941 OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
943 /* add MCT capability */
944 int rsiz = (int)parameters->cp_rsiz | (int)MCT;
945 parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
946 parameters->irreversible = 1;
948 /* use array based MCT */
949 parameters->tcp_mct = 2;
950 parameters->mct_data = opj_malloc(l_mct_total_size);
951 if (! parameters->mct_data) {
955 memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
956 memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size,p_dc_shift,l_dc_shift_size);
962 * Writes a tile with the given data.
964 * @param p_compressor the jpeg2000 codec.
965 * @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.
966 * @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.
967 * @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,
968 * depending on the precision of the given component.
969 * @param p_stream the stream to write data to.
971 opj_bool OPJ_CALLCONV opj_write_tile ( opj_codec_t *p_codec,
972 OPJ_UINT32 p_tile_index,
974 OPJ_UINT32 p_data_size,
975 opj_stream_t *p_stream )
977 if (p_codec && p_stream && p_data) {
978 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
979 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
981 if (l_codec->is_decompressor) {
985 return l_codec->m_codec_data.m_compression.opj_write_tile( l_codec->m_codec,
990 &(l_codec->m_event_mgr) );
996 /* ---------------------------------------------------------------------- */
998 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
1001 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1003 if (l_codec->is_decompressor) {
1004 l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
1007 l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
1010 l_codec->m_codec = 00;
1015 /* ---------------------------------------------------------------------- */
1021 void OPJ_CALLCONV opj_dump_codec( opj_codec_t *p_codec,
1022 OPJ_INT32 info_flag,
1023 FILE* output_stream)
1026 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1028 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
1032 fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
1040 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
1043 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1045 return l_codec->opj_get_codec_info(l_codec->m_codec);
1055 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) {
1058 if ((*cstr_info)->m_default_tile_info.tccp_info){
1059 opj_free((*cstr_info)->m_default_tile_info.tccp_info);
1062 if ((*cstr_info)->tile_info){
1063 /* FIXME not used for the moment*/
1066 opj_free((*cstr_info));
1067 (*cstr_info) = NULL;
1075 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
1078 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1080 return l_codec->opj_get_codec_index(l_codec->m_codec);
1090 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
1093 j2k_destroy_cstr_index(*p_cstr_index);
1094 (*p_cstr_index) = NULL;
1098 /* ---------------------------------------------------------------------- */
1101 * Sets the stream to be a file stream. The FILE must have been open previously.
1102 * @param p_stream the stream to modify
1103 * @param p_file handler to an already open file.
1105 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
1107 return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
1110 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream ( FILE * p_file,
1112 opj_bool p_is_read_stream)
1114 opj_stream_t* l_stream = 00;
1120 l_stream = opj_stream_create(p_size,p_is_read_stream);
1125 opj_stream_set_user_data(l_stream, p_file);
1126 opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
1127 opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
1128 opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
1129 opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
1130 opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);