X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=libopenjpeg%2Fopenjpeg.c;h=f69be8e126ac433b4bbaca394d3ef46ba3d2bf8e;hb=1a5c59326ab4836618f88caefa8b66760f624793;hp=da3fe0d288ba131e96efb8dc000868b8a187d2ab;hpb=b138aaae08d81965d0451465426d057aed3cbbfc;p=openjpeg.git diff --git a/libopenjpeg/openjpeg.c b/libopenjpeg/openjpeg.c index da3fe0d2..f69be8e1 100644 --- a/libopenjpeg/openjpeg.c +++ b/libopenjpeg/openjpeg.c @@ -33,37 +33,44 @@ typedef struct opj_decompression { - opj_bool (* opj_read_header) ( - void *p_codec, - opj_image_t **, - OPJ_INT32 * p_tile_x0, - OPJ_INT32 * p_tile_y0, - OPJ_UINT32 * p_tile_width, - OPJ_UINT32 * p_tile_height, - OPJ_UINT32 * p_nb_tiles_x, - OPJ_UINT32 * p_nb_tiles_y, - struct opj_stream_private *cio, - struct opj_event_mgr * p_manager); - opj_image_t* (* opj_decode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager); - opj_bool (*opj_read_tile_header)( - void * p_codec, - OPJ_UINT32 * p_tile_index, - OPJ_UINT32* p_data_size, - OPJ_INT32 * p_tile_x0, - OPJ_INT32 * p_tile_y0, - OPJ_INT32 * p_tile_x1, - OPJ_INT32 * p_tile_y1, - OPJ_UINT32 * p_nb_comps, - opj_bool * p_should_go_on, - struct opj_stream_private *p_cio, - struct opj_event_mgr * p_manager); - opj_bool (*opj_decode_tile_data)(void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private *p_cio,struct opj_event_mgr * p_manager); - opj_bool (* opj_end_decompress) (void *p_codec,struct opj_stream_private *cio,struct opj_event_mgr * p_manager); + opj_bool (* opj_read_header) ( struct opj_stream_private * cio, + void * p_codec, + opj_file_info_t * file_info, + struct opj_event_mgr * p_manager); + + opj_image_t* (* opj_decode) ( void * p_codec, + struct opj_stream_private *p_cio, + struct opj_event_mgr * p_manager); + + opj_bool (*opj_read_tile_header)( void * p_codec, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32* p_data_size, + OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + opj_bool * p_should_go_on, + struct opj_stream_private *p_cio, + struct opj_event_mgr * p_manager); + + opj_bool (*opj_decode_tile_data)( void * p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + struct opj_stream_private *p_cio, + struct opj_event_mgr * p_manager); + + opj_bool (* opj_end_decompress) ( void *p_codec, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager); + void (* opj_destroy) (void * p_codec); - void (*opj_setup_decoder) (void * p_codec,opj_dparameters_t * p_param); - opj_bool (*opj_set_decode_area) (void * p_codec,OPJ_INT32 p_start_x,OPJ_INT32 p_end_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_y,struct opj_event_mgr * p_manager); + void (*opj_setup_decoder) (void * p_codec, opj_dparameters_t * p_param); + opj_bool (*opj_set_decode_area) ( void * p_codec, + OPJ_INT32 p_start_x, OPJ_INT32 p_end_x, + OPJ_INT32 p_start_y, OPJ_INT32 p_end_y, + struct opj_event_mgr * p_manager); }opj_decompression_t; typedef struct opj_compression @@ -79,10 +86,10 @@ typedef struct opj_compression typedef struct opj_codec_private { - union - { /* code-blocks informations */ - opj_decompression_t m_decompression; - opj_compression_t m_compression; + /* code-blocks informations */ + union { + opj_decompression_t m_decompression; + opj_compression_t m_compression; } m_codec_data; void * m_codec; opj_event_mgr_t* m_event_mgr; @@ -123,21 +130,19 @@ OPJ_UINT32 opj_write_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p OPJ_SIZE_T opj_skip_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data) { - if - (fseek(p_user_data,p_nb_bytes,SEEK_CUR)) - { + if (fseek(p_user_data,p_nb_bytes,SEEK_CUR)) { return -1; } + return p_nb_bytes; } opj_bool opj_seek_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data) { - if - (fseek(p_user_data,p_nb_bytes,SEEK_SET)) - { + if (fseek(p_user_data,p_nb_bytes,SEEK_SET)) { return EXIT_FAILURE; } + return EXIT_SUCCESS; } @@ -217,68 +222,63 @@ opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format) l_info->is_decompressor = 1; - switch - (p_format) - { + switch (p_format) { case CODEC_J2K: - l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))j2k_decode; - l_info->m_codec_data.m_decompression.opj_end_decompress = (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *))j2k_end_decompress; - l_info->m_codec_data.m_decompression.opj_read_header = (opj_bool (*) ( - void *, - opj_image_t **, - OPJ_INT32 * , - OPJ_INT32 * , - OPJ_UINT32 * , - OPJ_UINT32 * , - OPJ_UINT32 * , - OPJ_UINT32 * , - struct opj_stream_private *, - struct opj_event_mgr * )) j2k_read_header; + l_info->m_codec_data.m_decompression.opj_decode = + (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))j2k_decode; // TODO MSD + + l_info->m_codec_data.m_decompression.opj_end_decompress = + (opj_bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr *))j2k_end_decompress; + + l_info->m_codec_data.m_decompression.opj_read_header = + (opj_bool (*) ( struct opj_stream_private *, + void *, + opj_file_info_t *, + struct opj_event_mgr * )) j2k_read_header; + l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))j2k_destroy; - l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) j2k_setup_decoder; - l_info->m_codec_data.m_decompression.opj_read_tile_header = (opj_bool (*) ( - void *, - OPJ_UINT32*, - OPJ_UINT32*, - OPJ_INT32 * , - OPJ_INT32 * , - OPJ_INT32 * , - OPJ_INT32 * , - OPJ_UINT32 * , - opj_bool *, - struct opj_stream_private *, - struct opj_event_mgr * )) j2k_read_tile_header; - l_info->m_codec_data.m_decompression.opj_decode_tile_data = (opj_bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *, struct opj_event_mgr * )) j2k_decode_tile; - l_info->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) j2k_set_decode_area; + + l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2; + + l_info->m_codec_data.m_decompression.opj_read_tile_header = + (opj_bool (*) ( void *, + OPJ_UINT32*, + OPJ_UINT32*, + OPJ_INT32*, OPJ_INT32*, + OPJ_INT32*, OPJ_INT32*, + OPJ_UINT32*, + opj_bool*, + struct opj_stream_private *, + struct opj_event_mgr * )) j2k_read_tile_header; + + l_info->m_codec_data.m_decompression.opj_decode_tile_data = + (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile; + + l_info->m_codec_data.m_decompression.opj_set_decode_area = + (opj_bool (*) (void *, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area; + l_info->m_codec = j2k_create_decompress_v2(); - if - (! l_info->m_codec) - { + + if (! l_info->m_codec) { opj_free(l_info); - return 00; + return NULL; } + break; case CODEC_JP2: /* get a JP2 decoder handle */ -#ifdef TODO_MSD - l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))opj_jp2_decode; + l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))opj_jp2_decode; // TODO MSD + l_info->m_codec_data.m_decompression.opj_end_decompress = (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress; + l_info->m_codec_data.m_decompression.opj_read_header = (opj_bool (*) ( - void *, - opj_image_t **, - - OPJ_INT32 * , - OPJ_INT32 * , - OPJ_UINT32 * , - OPJ_UINT32 * , - OPJ_UINT32 * , - OPJ_UINT32 * , - struct opj_stream_private *, - struct opj_event_mgr * )) jp2_read_header; - - l_info->m_codec_data.m_decompression.opj_read_tile_header = ( - opj_bool (*) ( + struct opj_stream_private *, + void *, + opj_file_info_t *, + struct opj_event_mgr * )) jp2_read_header; + + l_info->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) ( void *, OPJ_UINT32*, OPJ_UINT32*, @@ -294,18 +294,18 @@ opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format) l_info->m_codec_data.m_decompression.opj_decode_tile_data = (opj_bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *, struct opj_event_mgr * )) opj_jp2_decode_tile; l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy; - l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder; - l_info->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) jp2_set_decode_area; + l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder_v2; + + l_info->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) jp2_set_decode_area; l_info->m_codec = jp2_create(OPJ_TRUE); - if - (! l_info->m_codec) - { + + if (! l_info->m_codec) { opj_free(l_info); return 00; } -#endif + break; case CODEC_UNKNOWN: case CODEC_JPT: @@ -494,7 +494,7 @@ void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *paramete parameters->cod_format = -1; parameters->tcp_rates[0] = 0; parameters->tcp_numlayers = 0; - parameters->cp_disto_alloc = 0; + parameters->cp_disto_alloc = 0; parameters->cp_fixed_alloc = 0; parameters->cp_fixed_quality = 0; parameters->jpip_on = OPJ_FALSE; @@ -547,22 +547,22 @@ opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file, OPJ_UINT32 p_size, opj_bool p_is_read_stream) { opj_stream_t* l_stream = 00; - if - (! p_file) - { - return 00; + + if (! p_file) { + return NULL; } + l_stream = opj_stream_create(p_size,p_is_read_stream); - if - (! l_stream) - { - return 00; + if (! l_stream) { + return NULL; } - opj_stream_set_user_data(l_stream,p_file); - opj_stream_set_read_function(l_stream,(opj_stream_read_fn) opj_read_from_file); + + opj_stream_set_user_data(l_stream, p_file); + opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file); opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file); opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file); opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file); + return l_stream; } @@ -624,6 +624,8 @@ void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) { } + +#ifdef OLD_WAY_MS opj_bool OPJ_CALLCONV opj_read_header ( opj_codec_t *p_codec, opj_image_t ** p_image, @@ -635,16 +637,14 @@ opj_bool OPJ_CALLCONV opj_read_header ( OPJ_UINT32 * p_nb_tiles_y, opj_stream_t *p_cio) { - if - (p_codec && p_cio) - { + if (p_codec && p_cio) { opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec; opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio; - if - (! l_info->is_decompressor) - { + + if(! l_info->is_decompressor) { return OPJ_FALSE; } + return l_info->m_codec_data.m_decompression.opj_read_header( l_info->m_codec, p_image, @@ -655,12 +655,39 @@ opj_bool OPJ_CALLCONV opj_read_header ( p_nb_tiles_x, p_nb_tiles_y, l_cio, - //&(l_info->m_event_mgr)); - l_info->m_event_mgr); + l_info->m_event_mgr); //&(l_info->m_event_mgr)); + } + return OPJ_FALSE; +} +#endif + +opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_cio, + opj_codec_t *p_codec, + opj_file_info_t* p_file_info, + OPJ_INT32 file_info_flag) + +{ + /* Initialize the output structure */ + opj_initialise_file_info(p_file_info, file_info_flag, CODEC_J2K); + + if (p_codec && p_cio) { + opj_codec_private_t* l_info = (opj_codec_private_t*) p_codec; + opj_stream_private_t* l_cio = (opj_stream_private_t*) p_cio; + + if(! l_info->is_decompressor) { + return OPJ_FALSE; + } + + return l_info->m_codec_data.m_decompression.opj_read_header( + l_cio, + l_info->m_codec, + p_file_info, + l_info->m_event_mgr); } return OPJ_FALSE; } + void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info) { if @@ -680,3 +707,90 @@ void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info) opj_free(l_info); } } + +/** + * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading. + * + * @param p_codec the jpeg2000 codec. + * @param p_start_x the left position of the rectangle to decode (in image coordinates). + * @param p_end_x the right position of the rectangle to decode (in image coordinates). + * @param p_start_y the up position of the rectangle to decode (in image coordinates). + * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). + * + * @return true if the area could be set. + */ +opj_bool OPJ_CALLCONV opj_set_decode_area( opj_codec_t *p_codec, + OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, OPJ_INT32 p_end_y + ) +{ + if (p_codec) { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec; + if (! l_info->is_decompressor) { + return OPJ_FALSE; + } + + return l_info->m_codec_data.m_decompression.opj_set_decode_area( + l_info->m_codec, + p_start_x, + p_start_y, + p_end_x, + p_end_y, + l_info->m_event_mgr); + + } + return OPJ_FALSE; +} + +/** + * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded. + * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile. + * + * @param p_codec the jpeg2000 codec. + * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success. + * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case + * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same + * as depicted in opj_write_tile. + * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image). + * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image). + * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image). + * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image). + * @param p_nb_comps pointer to a value that will hold the number of components in the tile. + * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the + * codestream is over at the time of the call, the value will be set to false. The user should then stop + * the decoding. + * @param p_stream the stream to decode. + * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true. + * returning false may be the result of a shortage of memory or an internal error. + */ +opj_bool OPJ_CALLCONV opj_read_tile_header( + opj_codec_t *p_codec, + opj_stream_t * p_stream, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + opj_bool * p_should_go_on) +{ + if (p_codec && p_stream && p_data_size && p_tile_index) { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream; + + if (! l_info->is_decompressor) { + return OPJ_FALSE; + } + + return l_info->m_codec_data.m_decompression.opj_read_tile_header( + l_info->m_codec, + p_tile_index, + p_data_size, + p_tile_x0, p_tile_y0, + p_tile_x1, p_tile_y1, + p_nb_comps, + p_should_go_on, + l_cio, + l_info->m_event_mgr); + } + return OPJ_FALSE; +}