From 4e81ea2a8aca513fda983d5e943a111e27938f9c Mon Sep 17 00:00:00 2001 From: Luc Hermitte Date: Wed, 22 Aug 2012 18:45:31 +0000 Subject: [PATCH] [trunk] realloc is misused and may leak memory (Issue#168) --- applications/mj2/wrap_j2k_in_mj2.c | 11 +- libopenjpeg/function_list.c | 124 +- libopenjpeg/j2k.c | 15471 ++++++++++++++------------- libopenjpeg/jp2.c | 9 +- libopenjpeg/jpwl/jpwl.c | 2255 ++-- libopenjpeg/mqc.c | 13 +- libopenjpeg/mqc.h | 2 +- libopenjpeg/t1.c | 36 +- libopenjpeg/t1.h | 2 +- libopenjpeg/t2.c | 3368 +++--- libopenjpeg/tcd.c | 3620 +++---- libopenjpeg/tgt.c | 716 +- tests/test_tile_decoder.c | 603 +- 13 files changed, 13246 insertions(+), 12984 deletions(-) diff --git a/applications/mj2/wrap_j2k_in_mj2.c b/applications/mj2/wrap_j2k_in_mj2.c index 65a9ed22..7ce88677 100644 --- a/applications/mj2/wrap_j2k_in_mj2.c +++ b/applications/mj2/wrap_j2k_in_mj2.c @@ -475,10 +475,17 @@ int main(int argc, char *argv[]) { // Ending loop fclose(j2kfile); snum++; - movie->tk[0].sample = (mj2_sample_t*) + mj2_sample_t * new_sample = (mj2_sample_t*) realloc(movie->tk[0].sample, (snum+1) * sizeof(mj2_sample_t)); - movie->tk[0].chunk = (mj2_chunk_t*) + mj2_chunk_t * new_chunk = (mj2_chunk_t*) realloc(movie->tk[0].chunk, (snum+1) * sizeof(mj2_chunk_t)); + if (new_sample && new_chunk) { + movie->tk[0].sample = new_sample; + movie->tk[0].chunk = new_chunk; + } else { + fprintf(stderr, "Failed to allocate enough memory to read %s\n", j2kfilename); + return 1; + } free(frame_codestream); } diff --git a/libopenjpeg/function_list.c b/libopenjpeg/function_list.c index 0be913c4..bda09710 100644 --- a/libopenjpeg/function_list.c +++ b/libopenjpeg/function_list.c @@ -34,30 +34,30 @@ /** * Creates a validation list. * - * @return the newly created validation list. + * @return the newly created validation list. */ opj_procedure_list_t * opj_procedure_list_create() { - /* memory allocation */ - opj_procedure_list_t * l_validation = (opj_procedure_list_t *) opj_malloc(sizeof(opj_procedure_list_t)); - if - (! l_validation) - { - return 00; - } - /* initialization */ - memset(l_validation,0,sizeof(opj_procedure_list_t)); - l_validation->m_nb_max_procedures = OPJ_VALIDATION_SIZE; - l_validation->m_procedures = (opj_procedure*)opj_malloc( - OPJ_VALIDATION_SIZE * sizeof(opj_procedure)); - if - (! l_validation->m_procedures) - { - opj_free(l_validation); - return 00; - } - memset(l_validation->m_procedures,0,OPJ_VALIDATION_SIZE * sizeof(opj_procedure)); - return l_validation; + /* memory allocation */ + opj_procedure_list_t * l_validation = (opj_procedure_list_t *) opj_malloc(sizeof(opj_procedure_list_t)); + if + (! l_validation) + { + return 00; + } + /* initialization */ + memset(l_validation,0,sizeof(opj_procedure_list_t)); + l_validation->m_nb_max_procedures = OPJ_VALIDATION_SIZE; + l_validation->m_procedures = (opj_procedure*)opj_malloc( + OPJ_VALIDATION_SIZE * sizeof(opj_procedure)); + if + (! l_validation->m_procedures) + { + opj_free(l_validation); + return 00; + } + memset(l_validation->m_procedures,0,OPJ_VALIDATION_SIZE * sizeof(opj_procedure)); + return l_validation; } @@ -69,64 +69,66 @@ opj_procedure_list_t * opj_procedure_list_create() */ void opj_procedure_list_destroy(opj_procedure_list_t * p_list) { - if - (! p_list) - { - return; - } - /* initialization */ - if - (p_list->m_procedures) - { - opj_free(p_list->m_procedures); - } - opj_free(p_list); + if + (! p_list) + { + return; + } + /* initialization */ + if + (p_list->m_procedures) + { + opj_free(p_list->m_procedures); + } + opj_free(p_list); } /** * Adds a new validation procedure. * - * @param p_validation_list the list of procedure to modify. - * @param p_procedure the procedure to add. + * @param p_validation_list the list of procedure to modify. + * @param p_procedure the procedure to add. */ opj_bool opj_procedure_list_add_procedure (opj_procedure_list_t * p_validation_list, opj_procedure p_procedure) { - if - (p_validation_list->m_nb_max_procedures == p_validation_list->m_nb_procedures) - { - opj_procedure * new_procedures; + if + (p_validation_list->m_nb_max_procedures == p_validation_list->m_nb_procedures) + { + opj_procedure * new_procedures; - p_validation_list->m_nb_max_procedures += OPJ_VALIDATION_SIZE; + p_validation_list->m_nb_max_procedures += OPJ_VALIDATION_SIZE; new_procedures = (opj_procedure*)opj_realloc( - p_validation_list->m_procedures,p_validation_list->m_nb_max_procedures * sizeof(opj_procedure)); - if - (! new_procedures) - { - opj_free(p_validation_list->m_procedures); - p_validation_list->m_nb_max_procedures = 0; - p_validation_list->m_nb_procedures = 0; - return OPJ_FALSE; - } + p_validation_list->m_procedures,p_validation_list->m_nb_max_procedures * sizeof(opj_procedure)); + if + (! new_procedures) + { + opj_free(p_validation_list->m_procedures); + p_validation_list->m_nb_max_procedures = 0; + p_validation_list->m_nb_procedures = 0; + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add a new validation procedure\n"); */ + fprintf(stderr, "Not enough memory to add a new validation procedure\n"); + return OPJ_FALSE; + } else { - p_validation_list->m_procedures = new_procedures; + p_validation_list->m_procedures = new_procedures; } - } - p_validation_list->m_procedures[p_validation_list->m_nb_procedures] = p_procedure; - ++p_validation_list->m_nb_procedures; - return OPJ_TRUE; + } + p_validation_list->m_procedures[p_validation_list->m_nb_procedures] = p_procedure; + ++p_validation_list->m_nb_procedures; + return OPJ_TRUE; } /** * Gets the number of validation procedures. * - * @param p_validation_list the list of procedure to modify. + * @param p_validation_list the list of procedure to modify. * * @return the number of validation procedures. */ OPJ_UINT32 opj_procedure_list_get_nb_procedures (opj_procedure_list_t * p_validation_list) { - return p_validation_list->m_nb_procedures; + return p_validation_list->m_nb_procedures; } /** @@ -134,22 +136,22 @@ OPJ_UINT32 opj_procedure_list_get_nb_procedures (opj_procedure_list_t * p_valida * iterator class to iterate through all the procedures inside the validation list. * the caller does not take ownership of the pointer. * - * @param p_validation_list the list of procedure to get the first procedure from. + * @param p_validation_list the list of procedure to get the first procedure from. * - * @return a pointer to the first procedure. + * @return a pointer to the first procedure. */ opj_procedure* opj_procedure_list_get_first_procedure (opj_procedure_list_t * p_validation_list) { - return p_validation_list->m_procedures; + return p_validation_list->m_procedures; } /** * Clears the list of validation procedures. * - * @param p_validation_list the list of procedure to clear. + * @param p_validation_list the list of procedure to clear. * */ void opj_procedure_list_clear (opj_procedure_list_t * p_validation_list) { - p_validation_list->m_nb_procedures = 0; + p_validation_list->m_nb_procedures = 0; } diff --git a/libopenjpeg/j2k.c b/libopenjpeg/j2k.c index eb77110e..115cd7f8 100644 --- a/libopenjpeg/j2k.c +++ b/libopenjpeg/j2k.c @@ -56,9 +56,9 @@ static opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k, /** * The default encoding validation procedure without any extension. * - * @param p_j2k the jpeg2000 codec to validate. - * @param p_stream the input stream to validate. - * @param p_manager the user event manager. + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. * * @return true if the parameters are correct. */ @@ -69,9 +69,9 @@ static opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k, /** * The default decoding validation procedure without any extension. * - * @param p_j2k the jpeg2000 codec to validate. - * @param p_stream the input stream to validate. - * @param p_manager the user event manager. + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. * * @return true if the parameters are correct. */ @@ -100,9 +100,9 @@ static void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k); /** * The mct encoding validation procedure. * - * @param p_j2k the jpeg2000 codec to validate. - * @param p_stream the input stream to validate. - * @param p_manager the user event manager. + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. * * @return true if the parameters are correct. */ @@ -126,23 +126,23 @@ static opj_bool opj_j2k_build_encoder ( opj_j2k_v2_t * p_j2k, /** * Creates a tile-coder decoder. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Excutes the given procedures on the given codec. * - * @param p_procedure_list the list of procedures to execute - * @param p_j2k the jpeg2000 codec to execute the procedures on. - * @param p_stream the stream to execute the procedures on. - * @param p_manager the user manager. + * @param p_procedure_list the list of procedures to execute + * @param p_j2k the jpeg2000 codec to execute the procedures on. + * @param p_stream the stream to execute the procedures on. + * @param p_manager the user manager. * - * @return true if all the procedures were successfully executed. + * @return true if all the procedures were successfully executed. */ static opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k, opj_procedure_list_t * p_procedure_list, @@ -152,19 +152,19 @@ static opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k, /** * Updates the rates of the tcp. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Copies the decoding tile parameters onto all the tile parameters. * Creates also the tile decoder. */ -static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k, +static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager ); @@ -178,30 +178,30 @@ static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k, /** * Reads the lookup table containing all the marker, status and action, and returns the handler associated * with the marker value. - * @param p_id Marker value to look up + * @param p_id Marker value to look up * - * @return the handler associated with the id. + * @return the handler associated with the id. */ static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler (OPJ_UINT32 p_id); /** * Destroys a tile coding parameter structure. * - * @param p_tcp the tile coding parameter to destroy. + * @param p_tcp the tile coding parameter to destroy. */ static void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp); /** * Destroys the data inside a tile coding parameter structure. * - * @param p_tcp the tile coding parameter which contain data to destroy. + * @param p_tcp the tile coding parameter which contain data to destroy. */ static void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp); /** * Destroys a coding parameter structure. * - * @param p_cp the coding parameter to destroy. + * @param p_cp the coding parameter to destroy. */ static void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp); @@ -209,38 +209,38 @@ static void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp); /** * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. * - * @param p_comp_no the component number to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_no the component number to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. * */ -static opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_tile_no, - OPJ_UINT32 p_comp_no, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_header_size, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + opj_event_mgr_t * p_manager ); /** * Gets the size taken by writing a SPCod or SPCoc for the given tile and component. * - * @param p_tile_no the tile index. - * @param p_comp_no the component being outputted. - * @param p_j2k the J2K codec. + * @param p_tile_no the tile index. + * @param p_comp_no the component being outputted. + * @param p_j2k the J2K codec. * - * @return the number of bytes taken by the SPCod element. + * @return the number of bytes taken by the SPCod element. */ static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_tile_no, - OPJ_UINT32 p_comp_no ); + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no ); /** * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. - * @param p_header_data the data contained in the COM box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the COM marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the COM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k, OPJ_UINT32 compno, @@ -251,33 +251,33 @@ static opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k, /** * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. * - * @param p_tile_no the tile index. - * @param p_comp_no the component being outputted. - * @param p_j2k the J2K codec. + * @param p_tile_no the tile index. + * @param p_comp_no the component being outputted. + * @param p_j2k the J2K codec. * - * @return the number of bytes taken by the SPCod element. + * @return the number of bytes taken by the SPCod element. */ -static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_tile_no, - OPJ_UINT32 p_comp_no ); +static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no ); /** * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. * - * @param p_tile_no the tile to output. - * @param p_comp_no the component number to output. - * @param p_data the data buffer. - * @param p_header_size pointer to the size of the data buffer, it is changed by the function. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_tile_no the tile to output. + * @param p_comp_no the component number to output. + * @param p_data the data buffer. + * @param p_header_size pointer to the size of the data buffer, it is changed by the function. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. * */ static opj_bool opj_j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_tile_no, - OPJ_UINT32 p_comp_no, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_header_size, - opj_event_mgr_t * p_manager); + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + opj_event_mgr_t * p_manager); /** * Updates the Tile Length Marker. @@ -287,12 +287,12 @@ static void opj_j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_si /** * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. * - * @param p_tile_no the tile to output. - * @param p_comp_no the component number to output. - * @param p_data the data buffer. - * @param p_header_size pointer to the size of the data buffer, it is changed by the function. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_tile_no the tile to output. + * @param p_comp_no the component number to output. + * @param p_data the data buffer. + * @param p_header_size pointer to the size of the data buffer, it is changed by the function. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. * */ static opj_bool opj_j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k, @@ -304,39 +304,39 @@ static opj_bool opj_j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k, /** * Copies the tile component parameters of all the component from the first tile component. * - * @param p_j2k the J2k codec. + * @param p_j2k the J2k codec. */ static void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k ); /** * Copies the tile quantization parameters of all the component from the first tile component. * - * @param p_j2k the J2k codec. + * @param p_j2k the J2k codec. */ static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k ); /** * Reads the tiles. */ -static opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k, +static opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager); static opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k, - OPJ_UINT32 p_tile_index, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_tile_index, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); static opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image); static void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data); static opj_bool opj_j2k_post_write_tile (opj_j2k_v2_t * p_j2k, - OPJ_BYTE * p_data, - OPJ_UINT32 p_data_size, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Sets up the procedures to do on writing header. @@ -344,28 +344,28 @@ static opj_bool opj_j2k_post_write_tile (opj_j2k_v2_t * p_j2k, */ static void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k); -static opj_bool opj_j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 p_total_data_size, - opj_stream_private_t *p_stream, - struct opj_event_mgr * p_manager ); +static opj_bool opj_j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager ); -static opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 p_total_data_size, - opj_stream_private_t *p_stream, - struct opj_event_mgr * p_manager ); +static opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager ); /** * Gets the offset of the header. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_get_end_header( opj_j2k_v2_t *p_j2k, +static opj_bool opj_j2k_get_end_header( opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager ); @@ -380,20 +380,20 @@ static opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k); /** * Writes the SOC marker (Start Of Codestream) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Reads a SOC marker (Start of Codestream) - * @param p_header_data the data contained in the SOC box. - * @param jp2 the jpeg2000 file codec. - * @param p_header_size the size of the data contained in the SOC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the SOC box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the SOC marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, @@ -402,20 +402,20 @@ static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k, /** * Writes the SIZ marker (image and tile size) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Reads a SIZ marker (image and tile size) - * @param p_header_data the data contained in the SIZ box. - * @param jp2 the jpeg2000 file codec. - * @param p_header_size the size of the data contained in the SIZ marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the SIZ box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the SIZ marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -425,20 +425,20 @@ static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k, /** * Writes the COM marker (comment) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Reads a COM marker (comments) - * @param p_header_data the data contained in the COM box. - * @param jp2 the jpeg2000 file codec. - * @param p_header_size the size of the data contained in the COM marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the COM box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -447,20 +447,20 @@ static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k, /** * Writes the COD marker (Coding style default) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Reads a COD marker (Coding Styke defaults) - * @param p_header_data the data contained in the COD box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the COD marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the COD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COD marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -470,43 +470,43 @@ static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k, /** * Writes the COC marker (Coding style component) * - * @param p_comp_number the index of the component to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_number the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_comp_no, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_comp_no, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Writes the COC marker (Coding style component) * - * @param p_comp_no the index of the component to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ static void opj_j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_comp_no, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager ); /** * Gets the maximum size taken by a coc. * - * @param p_j2k the jpeg2000 codec to use. + * @param p_j2k the jpeg2000 codec to use. */ static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k); /** * Reads a COC marker (Coding Style Component) - * @param p_header_data the data contained in the COC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the COC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the COC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COC marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -516,21 +516,21 @@ static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k, /** * Writes the QCD marker (quantization default) * - * @param p_comp_number the index of the component to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_number the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Reads a QCD marker (Quantization defaults) - * @param p_header_data the data contained in the QCD box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the QCD marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the QCD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCD marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -539,29 +539,29 @@ static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k, /** * Writes the QCC marker (quantization component) * - * @param p_comp_no the index of the component to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_comp_no, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k, + OPJ_UINT32 p_comp_no, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Writes the QCC marker (quantization component) * - * @param p_comp_no the index of the component to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ static void opj_j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_comp_no, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager ); /** * Gets the maximum size taken by a qcc. @@ -570,10 +570,10 @@ static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k); /** * Reads a QCC marker (Quantization component) - * @param p_header_data the data contained in the QCC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the QCC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the QCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCC marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -582,24 +582,24 @@ static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k, /** * Writes the POC marker (Progression Order Change) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Writes the POC marker (Progression Order Change) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ static void opj_j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - opj_event_mgr_t * p_manager ); + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager ); /** * Gets the maximum size taken by the writting of a POC. */ @@ -608,10 +608,10 @@ static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k); /** * Reads a POC marker (Progression Order Change) * - * @param p_header_data the data contained in the POC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the POC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -626,7 +626,7 @@ static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k); /** * Gets the maximum size taken by the headers of the SOT. * - * @param p_j2k the jpeg2000 codec to use. + * @param p_j2k the jpeg2000 codec to use. */ static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k); @@ -634,10 +634,10 @@ static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k); /** * Reads a CRG marker (Component registration) * - * @param p_header_data the data contained in the TLM box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the TLM marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -646,10 +646,10 @@ static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k, /** * Reads a TLM marker (Tile Length Marker) * - * @param p_header_data the data contained in the TLM box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the TLM marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -659,21 +659,21 @@ static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k, /** * Writes the updated tlm. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k, +static opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager ); /** * Reads a PLM marker (Packet length, main header marker) * - * @param p_header_data the data contained in the TLM box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the TLM marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -682,10 +682,10 @@ static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k, /** * Reads a PLT marker (Packet length, tile-part header) * - * @param p_header_data the data contained in the PLT box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the PLT marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the PLT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PLT marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -696,34 +696,34 @@ static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k, /** * Reads a PPM marker (Packed packet headers, main header) * - * @param p_header_data the data contained in the POC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the POC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. */ #if 0 static opj_bool j2k_read_ppm_v2 ( - opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - struct opj_event_mgr * p_manager - ); + opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); #endif static opj_bool j2k_read_ppm_v3 ( - opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager ); /** * Reads a PPT marker (Packed packet headers, tile-part header) * - * @param p_header_data the data contained in the PPT box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the PPT marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the PPT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PPT marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -732,34 +732,34 @@ static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k, /** * Writes the TLM marker (Tile Length Marker) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Writes the SOT marker (Start of tile-part) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - const opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + const opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Reads a PPT marker (Packed packet headers, tile-part header) * - * @param p_header_data the data contained in the PPT box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the PPT marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the PPT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PPT marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -768,25 +768,25 @@ static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k, /** * Writes the SOD marker (Start of data) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k, - opj_tcd_v2_t * p_tile_coder, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 p_total_data_size, - const opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k, + opj_tcd_v2_t * p_tile_coder, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + const opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Reads a SOD marker (Start Of Data) * - * @param p_header_data the data contained in the SOD box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the SOD marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the SOD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the SOD marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, @@ -797,35 +797,35 @@ static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k, */ void opj_j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size ) { - opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */ - ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current; + opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */ + ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current; - opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */ - p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4; + opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */ + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4; } /** * Writes the RGN marker (Region Of Interest) * - * @param p_tile_no the tile to output - * @param p_comp_no the component to output - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_tile_no the tile to output + * @param p_comp_no the component to output + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_rgn( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_tile_no, - OPJ_UINT32 p_comp_no, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_rgn( opj_j2k_v2_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Reads a RGN marker (Region Of Interest) * - * @param p_header_data the data contained in the POC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the POC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -835,45 +835,45 @@ static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k, /** * Writes the EOC marker (End of Codestream) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_eoc( opj_j2k_v2_t *p_j2k, +static opj_bool opj_j2k_write_eoc( opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager ); /** * Reads a EOC marker (End Of Codestream) * - * @param p_header_data the data contained in the SOD box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the SOD marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the SOD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the SOD marker. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_read_eoc ( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_read_eoc ( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Writes the CBD-MCT-MCC-MCO markers (Multi components transform) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k, +static opj_bool opj_j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager ); /** * Inits the Info * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_init_info( opj_j2k_v2_t *p_j2k, +static opj_bool opj_j2k_init_info( opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager ); @@ -884,7 +884,7 @@ Add main header marker information @param pos byte offset of marker segment @param len length of marker segment */ -static void opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ; +static opj_bool opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ; /** Add tile header marker information @param tileno tile index number @@ -893,56 +893,56 @@ Add tile header marker information @param pos byte offset of marker segment @param len length of marker segment */ -static void opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len); +static opj_bool opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len); /** * Reads an unknown marker * - * @param p_stream the stream object to read from. - * @param p_j2k the jpeg2000 codec. - * @param p_manager the user event manager. + * @param p_stream the stream object to read from. + * @param p_j2k the jpeg2000 codec. + * @param p_manager the user event manager. * - * @return true if the marker could be deduced. + * @return true if the marker could be deduced. */ -static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - OPJ_UINT32 *output_marker, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + OPJ_UINT32 *output_marker, + opj_event_mgr_t * p_manager ); /** * Writes the MCT marker (Multiple Component Transform) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_mct_record( opj_j2k_v2_t *p_j2k, - opj_mct_data_t * p_mct_record, +static opj_bool opj_j2k_write_mct_record( opj_j2k_v2_t *p_j2k, + opj_mct_data_t * p_mct_record, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager ); /** * Reads a MCT marker (Multiple Component Transform) * - * @param p_header_data the data contained in the MCT box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the MCT marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the MCT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCT marker. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager ); /** * Writes the MCC marker (Multiple Component Collection) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k, - opj_simple_mcc_decorrelation_data_t * p_mcc_record, +static opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k, + opj_simple_mcc_decorrelation_data_t * p_mcc_record, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager ); @@ -950,39 +950,39 @@ static opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k, /** * Reads a MCC marker (Multiple Component Collection) * - * @param p_header_data the data contained in the MCC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the MCC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the MCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCC marker. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager ); /** * Writes the MCO marker (Multiple component transformation ordering) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_mco( opj_j2k_v2_t *p_j2k, +static opj_bool opj_j2k_write_mco( opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager ); /** * Reads a MCO marker (Multiple Component Transform Ordering) * - * @param p_header_data the data contained in the MCO box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the MCO marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the MCO box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCO marker. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager ); static opj_bool opj_j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index); @@ -1005,97 +1005,97 @@ static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_d /** * Ends the encoding, i.e. frees memory. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_end_encoding( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_end_encoding( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Writes the CBD marker (Component bit depth definition) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_cbd( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_cbd( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Reads a CBD marker (Component bit depth definition) - * @param p_header_data the data contained in the CBD box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the CBD marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the CBD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the CBD marker. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager); +static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager); /** * Writes the image components. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_image_components( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_image_components( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Writes regions of interests. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_regions( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_regions( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Writes EPC ???? * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_write_epc( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static opj_bool opj_j2k_write_epc( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ); /** * Checks the progression order changes values. Tells of the poc given as input are valid. * A nice message is outputted at errors. * - * @param p_pocs the progression order changes. - * @param p_nb_pocs the number of progression order changes. - * @param p_nb_resolutions the number of resolutions. - * @param numcomps the number of components - * @param numlayers the number of layers. + * @param p_pocs the progression order changes. + * @param p_nb_pocs the number of progression order changes. + * @param p_nb_resolutions the number of resolutions. + * @param numcomps the number of components + * @param numlayers the number of layers. * - * @return true if the pocs are valid. + * @return true if the pocs are valid. */ -static opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs, - OPJ_UINT32 p_nb_pocs, - OPJ_UINT32 p_nb_resolutions, - OPJ_UINT32 numcomps, - OPJ_UINT32 numlayers, - opj_event_mgr_t * p_manager); +static opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs, + OPJ_UINT32 p_nb_pocs, + OPJ_UINT32 p_nb_resolutions, + OPJ_UINT32 numcomps, + OPJ_UINT32 numlayers, + opj_event_mgr_t * p_manager); /** * Gets the number of tile parts used for the given change of progression (if any) and the given tile. * - * @param cp the coding parameters. - * @param pino the offset of the given poc (i.e. its position in the coding parameter). - * @param tileno the given tile. + * @param cp the coding parameters. + * @param pino the offset of the given poc (i.e. its position in the coding parameter). + * @param tileno the given tile. * - * @return the number of tile parts. + * @return the number of tile parts. */ static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno); @@ -1103,19 +1103,19 @@ static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT * Calculates the total number of tile parts needed by the encoder to * encode such an image. If not enough memory is available, then the function return false. * - * @param p_nb_tiles pointer that will hold the number of tile parts. - * @param cp the coding parameters for the image. - * @param image the image to encode. - * @param p_j2k the p_j2k encoder. - * @param p_manager the user event manager. + * @param p_nb_tiles pointer that will hold the number of tile parts. + * @param cp the coding parameters for the image. + * @param image the image to encode. + * @param p_j2k the p_j2k encoder. + * @param p_manager the user event manager. * * @return true if the function was successful, false else. */ static opj_bool opj_j2k_calculate_tp( opj_j2k_v2_t *p_j2k, - opj_cp_v2_t *cp, - OPJ_UINT32 * p_nb_tiles, - opj_image_t *image, - opj_event_mgr_t * p_manager); + opj_cp_v2_t *cp, + OPJ_UINT32 * p_nb_tiles, + opj_image_t *image, + opj_event_mgr_t * p_manager); static void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream); @@ -1133,17 +1133,17 @@ static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_v2_t * p_tcp); /* ----------------------------------------------------------------------- */ typedef struct j2k_prog_order{ - OPJ_PROG_ORDER enum_prog; - char str_prog[5]; + OPJ_PROG_ORDER enum_prog; + char str_prog[5]; }j2k_prog_order_t; j2k_prog_order_t j2k_prog_order_list[] = { - {CPRL, "CPRL"}, - {LRCP, "LRCP"}, - {PCRL, "PCRL"}, - {RLCP, "RLCP"}, - {RPCL, "RPCL"}, - {(OPJ_PROG_ORDER)-1, ""} + {CPRL, "CPRL"}, + {LRCP, "LRCP"}, + {PCRL, "PCRL"}, + {RLCP, "RLCP"}, + {RPCL, "RPCL"}, + {(OPJ_PROG_ORDER)-1, ""} }; @@ -1153,46 +1153,46 @@ j2k_prog_order_t j2k_prog_order_list[] = { */ const OPJ_UINT32 MCT_ELEMENT_SIZE [] = { - 2, - 4, - 4, - 8 + 2, + 4, + 4, + 8 }; typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); const opj_j2k_mct_function j2k_mct_read_functions_to_float [] = { - opj_j2k_read_int16_to_float, - opj_j2k_read_int32_to_float, - opj_j2k_read_float32_to_float, - opj_j2k_read_float64_to_float + opj_j2k_read_int16_to_float, + opj_j2k_read_int32_to_float, + opj_j2k_read_float32_to_float, + opj_j2k_read_float64_to_float }; const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] = { - opj_j2k_read_int16_to_int32, - opj_j2k_read_int32_to_int32, - opj_j2k_read_float32_to_int32, - opj_j2k_read_float64_to_int32 + opj_j2k_read_int16_to_int32, + opj_j2k_read_int32_to_int32, + opj_j2k_read_float32_to_int32, + opj_j2k_read_float64_to_int32 }; const opj_j2k_mct_function j2k_mct_write_functions_from_float [] = { - opj_j2k_write_float_to_int16, - opj_j2k_write_float_to_int32, - opj_j2k_write_float_to_float, - opj_j2k_write_float_to_float64 + opj_j2k_write_float_to_int16, + opj_j2k_write_float_to_int32, + opj_j2k_write_float_to_float, + opj_j2k_write_float_to_float64 }; typedef struct opj_dec_memory_marker_handler { - /** marker value */ - OPJ_UINT32 id; - /** value of the state when the marker can appear */ - OPJ_UINT32 states; - /** action linked to the marker */ - opj_bool (*handler) ( opj_j2k_v2_t *p_j2k, + /** marker value */ + OPJ_UINT32 id; + /** value of the state when the marker can appear */ + OPJ_UINT32 states; + /** action linked to the marker */ + opj_bool (*handler) ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, OPJ_UINT32 p_header_size, opj_event_mgr_t * p_manager ); @@ -1238,326 +1238,326 @@ const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] = void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) { - OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; - OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; - OPJ_UINT32 i; - OPJ_UINT32 l_temp; + OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; + OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; + OPJ_UINT32 i; + OPJ_UINT32 l_temp; - for (i=0;ienum_prog != -1; po++ ){ - if(po->enum_prog == prg_order){ - return po->str_prog; - } - } - return po->str_prog; + j2k_prog_order_t *po; + for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){ + if(po->enum_prog == prg_order){ + return po->str_prog; + } + } + return po->str_prog; } /** * Checks the progression order changes values. Tells if the poc given as input are valid. * - * @param p_pocs the progression order changes. - * @param p_nb_pocs the number of progression order changes. - * @param p_nb_resolutions the number of resolutions. - * @param numcomps the number of components - * @param numlayers the number of layers. - * @param p_manager the user event manager. + * @param p_pocs the progression order changes. + * @param p_nb_pocs the number of progression order changes. + * @param p_nb_resolutions the number of resolutions. + * @param numcomps the number of components + * @param numlayers the number of layers. + * @param p_manager the user event manager. * - * @return true if the pocs are valid. + * @return true if the pocs are valid. */ -opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs, - OPJ_UINT32 p_nb_pocs, - OPJ_UINT32 p_nb_resolutions, - OPJ_UINT32 p_num_comps, - OPJ_UINT32 p_num_layers, - opj_event_mgr_t * p_manager) -{ - OPJ_UINT32* packet_array; - OPJ_UINT32 index , resno, compno, layno; - OPJ_UINT32 i; - OPJ_UINT32 step_c = 1; - OPJ_UINT32 step_r = p_num_comps * step_c; - OPJ_UINT32 step_l = p_nb_resolutions * step_r; - opj_bool loss = OPJ_FALSE; - OPJ_UINT32 layno0 = 0; - - packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32)); - if (packet_array == 00) { - opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n"); - return OPJ_FALSE; - } - memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32)); - - if (p_nb_pocs == 0) { - return OPJ_TRUE; - } - - index = step_r * p_pocs->resno0; - // take each resolution for each poc - for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) - { - OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; - - // take each comp of each resolution for each poc - for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { - OPJ_UINT32 comp_index = res_index + layno0 * step_l; - - // and finally take each layer of each res of ... - for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { - //index = step_r * resno + step_c * compno + step_l * layno; - packet_array[comp_index] = 1; - comp_index += step_l; - } - - res_index += step_c; - } - - index += step_r; - } - ++p_pocs; - - // iterate through all the pocs - for (i = 1; i < p_nb_pocs ; ++i) { - OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ; - - layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0; - index = step_r * p_pocs->resno0; - - // take each resolution for each poc - for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) { - OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; - - // take each comp of each resolution for each poc - for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { - OPJ_UINT32 comp_index = res_index + layno0 * step_l; - - // and finally take each layer of each res of ... - for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { - //index = step_r * resno + step_c * compno + step_l * layno; - packet_array[comp_index] = 1; - comp_index += step_l; - } - - res_index += step_c; - } - - index += step_r; - } - - ++p_pocs; - } - - index = 0; - for (layno = 0; layno < p_num_layers ; ++layno) { - for (resno = 0; resno < p_nb_resolutions; ++resno) { - for (compno = 0; compno < p_num_comps; ++compno) { - loss |= (packet_array[index]!=1); - //index = step_r * resno + step_c * compno + step_l * layno; - index += step_c; - } - } - } - - if (loss) { - opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n"); - } - - opj_free(packet_array); - - return !loss; +opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs, + OPJ_UINT32 p_nb_pocs, + OPJ_UINT32 p_nb_resolutions, + OPJ_UINT32 p_num_comps, + OPJ_UINT32 p_num_layers, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32* packet_array; + OPJ_UINT32 index , resno, compno, layno; + OPJ_UINT32 i; + OPJ_UINT32 step_c = 1; + OPJ_UINT32 step_r = p_num_comps * step_c; + OPJ_UINT32 step_l = p_nb_resolutions * step_r; + opj_bool loss = OPJ_FALSE; + OPJ_UINT32 layno0 = 0; + + packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32)); + if (packet_array == 00) { + opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n"); + return OPJ_FALSE; + } + memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32)); + + if (p_nb_pocs == 0) { + return OPJ_TRUE; + } + + index = step_r * p_pocs->resno0; + // take each resolution for each poc + for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) + { + OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; + + // take each comp of each resolution for each poc + for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { + OPJ_UINT32 comp_index = res_index + layno0 * step_l; + + // and finally take each layer of each res of ... + for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { + //index = step_r * resno + step_c * compno + step_l * layno; + packet_array[comp_index] = 1; + comp_index += step_l; + } + + res_index += step_c; + } + + index += step_r; + } + ++p_pocs; + + // iterate through all the pocs + for (i = 1; i < p_nb_pocs ; ++i) { + OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ; + + layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0; + index = step_r * p_pocs->resno0; + + // take each resolution for each poc + for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) { + OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; + + // take each comp of each resolution for each poc + for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) { + OPJ_UINT32 comp_index = res_index + layno0 * step_l; + + // and finally take each layer of each res of ... + for (layno = layno0; layno < p_pocs->layno1 ; ++layno) { + //index = step_r * resno + step_c * compno + step_l * layno; + packet_array[comp_index] = 1; + comp_index += step_l; + } + + res_index += step_c; + } + + index += step_r; + } + + ++p_pocs; + } + + index = 0; + for (layno = 0; layno < p_num_layers ; ++layno) { + for (resno = 0; resno < p_nb_resolutions; ++resno) { + for (compno = 0; compno < p_num_comps; ++compno) { + loss |= (packet_array[index]!=1); + //index = step_r * resno + step_c * compno + step_l * layno; + index += step_c; + } + } + } + + if (loss) { + opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n"); + } + + opj_free(packet_array); + + return !loss; } /* ----------------------------------------------------------------------- */ @@ -1565,356 +1565,359 @@ opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs, /** * Gets the number of tile parts used for the given change of progression (if any) and the given tile. * - * @param cp the coding parameters. - * @param pino the offset of the given poc (i.e. its position in the coding parameter). - * @param tileno the given tile. + * @param cp the coding parameters. + * @param pino the offset of the given poc (i.e. its position in the coding parameter). + * @param tileno the given tile. * - * @return the number of tile parts. + * @return the number of tile parts. */ OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno) { - const OPJ_CHAR *prog = 00; - OPJ_UINT32 i; - OPJ_UINT32 tpnum = 1; - opj_tcp_v2_t *tcp = 00; - opj_poc_t * l_current_poc = 00; - - /* preconditions */ - assert(tileno < (cp->tw * cp->th)); - assert(pino < (cp->tcps[tileno].numpocs + 1)); - - /* get the given tile coding parameter */ - tcp = &cp->tcps[tileno]; - assert(tcp != 00); - - l_current_poc = &(tcp->pocs[pino]); - assert(l_current_poc != 0); - - /* get the progression order as a character string */ - prog = opj_j2k_convert_progression_order(tcp->prg); - assert(strlen(prog) > 0); - - if (cp->m_specific_param.m_enc.m_tp_on == 1) { - for (i=0;i<4;++i) { - switch (prog[i]) - { - /* component wise */ - case 'C': - tpnum *= l_current_poc->compE; - break; - /* resolution wise */ - case 'R': - tpnum *= l_current_poc->resE; - break; - /* precinct wise */ - case 'P': - tpnum *= l_current_poc->prcE; - break; - /* layer wise */ - case 'L': - tpnum *= l_current_poc->layE; - break; - } - /* whould we split here ? */ - if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) { - cp->m_specific_param.m_enc.m_tp_pos=i; - break; - } - } - } - else { - tpnum=1; - } - - return tpnum; + const OPJ_CHAR *prog = 00; + OPJ_UINT32 i; + OPJ_UINT32 tpnum = 1; + opj_tcp_v2_t *tcp = 00; + opj_poc_t * l_current_poc = 00; + + /* preconditions */ + assert(tileno < (cp->tw * cp->th)); + assert(pino < (cp->tcps[tileno].numpocs + 1)); + + /* get the given tile coding parameter */ + tcp = &cp->tcps[tileno]; + assert(tcp != 00); + + l_current_poc = &(tcp->pocs[pino]); + assert(l_current_poc != 0); + + /* get the progression order as a character string */ + prog = opj_j2k_convert_progression_order(tcp->prg); + assert(strlen(prog) > 0); + + if (cp->m_specific_param.m_enc.m_tp_on == 1) { + for (i=0;i<4;++i) { + switch (prog[i]) + { + /* component wise */ + case 'C': + tpnum *= l_current_poc->compE; + break; + /* resolution wise */ + case 'R': + tpnum *= l_current_poc->resE; + break; + /* precinct wise */ + case 'P': + tpnum *= l_current_poc->prcE; + break; + /* layer wise */ + case 'L': + tpnum *= l_current_poc->layE; + break; + } + /* whould we split here ? */ + if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) { + cp->m_specific_param.m_enc.m_tp_pos=i; + break; + } + } + } + else { + tpnum=1; + } + + return tpnum; } /** * Calculates the total number of tile parts needed by the encoder to * encode such an image. If not enough memory is available, then the function return false. * - * @param p_nb_tiles pointer that will hold the number of tile parts. - * @param cp the coding parameters for the image. - * @param image the image to encode. - * @param p_j2k the p_j2k encoder. - * @param p_manager the user event manager. + * @param p_nb_tiles pointer that will hold the number of tile parts. + * @param cp the coding parameters for the image. + * @param image the image to encode. + * @param p_j2k the p_j2k encoder. + * @param p_manager the user event manager. * * @return true if the function was successful, false else. */ opj_bool opj_j2k_calculate_tp( opj_j2k_v2_t *p_j2k, - opj_cp_v2_t *cp, - OPJ_UINT32 * p_nb_tiles, - opj_image_t *image, - opj_event_mgr_t * p_manager + opj_cp_v2_t *cp, + OPJ_UINT32 * p_nb_tiles, + opj_image_t *image, + opj_event_mgr_t * p_manager ) { - OPJ_UINT32 pino,tileno; - OPJ_UINT32 l_nb_tiles; - opj_tcp_v2_t *tcp; + OPJ_UINT32 pino,tileno; + OPJ_UINT32 l_nb_tiles; + opj_tcp_v2_t *tcp; - /* preconditions */ - assert(p_nb_tiles != 00); - assert(cp != 00); - assert(image != 00); - assert(p_j2k != 00); - assert(p_manager != 00); + /* preconditions */ + assert(p_nb_tiles != 00); + assert(cp != 00); + assert(image != 00); + assert(p_j2k != 00); + assert(p_manager != 00); - l_nb_tiles = cp->tw * cp->th; - * p_nb_tiles = 0; - tcp = cp->tcps; + l_nb_tiles = cp->tw * cp->th; + * p_nb_tiles = 0; + tcp = cp->tcps; - /* INDEX >> */ - /* TODO mergeV2: check this part which use cstr_info */ - /*if (p_j2k->cstr_info) { - opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile; + /* INDEX >> */ + /* TODO mergeV2: check this part which use cstr_info */ + /*if (p_j2k->cstr_info) { + opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile; - for (tileno = 0; tileno < l_nb_tiles; ++tileno) { - OPJ_UINT32 cur_totnum_tp = 0; + for (tileno = 0; tileno < l_nb_tiles; ++tileno) { + OPJ_UINT32 cur_totnum_tp = 0; - pi_update_encoding_parameters(image,cp,tileno); + pi_update_encoding_parameters(image,cp,tileno); - for (pino = 0; pino <= tcp->numpocs; ++pino) - { - OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno); + for (pino = 0; pino <= tcp->numpocs; ++pino) + { + OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno); - *p_nb_tiles = *p_nb_tiles + tp_num; + *p_nb_tiles = *p_nb_tiles + tp_num; - cur_totnum_tp += tp_num; - } + cur_totnum_tp += tp_num; + } - tcp->m_nb_tile_parts = cur_totnum_tp; + tcp->m_nb_tile_parts = cur_totnum_tp; - l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t)); - if (l_info_tile_ptr->tp == 00) { - return OPJ_FALSE; - } + l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t)); + if (l_info_tile_ptr->tp == 00) { + return OPJ_FALSE; + } - memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t)); + memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t)); - l_info_tile_ptr->num_tps = cur_totnum_tp; + l_info_tile_ptr->num_tps = cur_totnum_tp; - ++l_info_tile_ptr; - ++tcp; - } - } - else */{ - for (tileno = 0; tileno < l_nb_tiles; ++tileno) { - OPJ_UINT32 cur_totnum_tp = 0; + ++l_info_tile_ptr; + ++tcp; + } + } + else */{ + for (tileno = 0; tileno < l_nb_tiles; ++tileno) { + OPJ_UINT32 cur_totnum_tp = 0; - pi_update_encoding_parameters(image,cp,tileno); + pi_update_encoding_parameters(image,cp,tileno); - for (pino = 0; pino <= tcp->numpocs; ++pino) { - OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno); + for (pino = 0; pino <= tcp->numpocs; ++pino) { + OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno); - *p_nb_tiles = *p_nb_tiles + tp_num; + *p_nb_tiles = *p_nb_tiles + tp_num; - cur_totnum_tp += tp_num; - } - tcp->m_nb_tile_parts = cur_totnum_tp; + cur_totnum_tp += tp_num; + } + tcp->m_nb_tile_parts = cur_totnum_tp; - ++tcp; - } - } + ++tcp; + } + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the SOC marker (Start Of Codestream) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - /* 2 bytes will be written */ - OPJ_BYTE * l_start_stream = 00; + /* 2 bytes will be written */ + OPJ_BYTE * l_start_stream = 00; - /* preconditions */ - assert(p_stream != 00); - assert(p_j2k != 00); - assert(p_manager != 00); + /* preconditions */ + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); - l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - /* write SOC identifier */ - opj_write_bytes(l_start_stream,J2K_MS_SOC,2); + /* write SOC identifier */ + opj_write_bytes(l_start_stream,J2K_MS_SOC,2); - if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) { + return OPJ_FALSE; + } /* UniPG>> */ #ifdef USE_JPWL - /* update markers struct */ + /* update markers struct */ /* - j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2); + j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2); */ assert( 0 && "TODO" ); #endif /* USE_JPWL */ /* <m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ; + opj_read_bytes(l_data,&l_marker,2); + if (l_marker != J2K_MS_SOC) { + return OPJ_FALSE; + } - /* FIXME move it in a index structure included in p_j2k*/ - p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2; + /* Next marker should be a SIZ marker in the main header */ + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ; - opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start); + /* FIXME move it in a index structure included in p_j2k*/ + p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2; - /* Add the marker to the codestream index*/ - opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2); + opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start); - return OPJ_TRUE; + /* Add the marker to the codestream index*/ + if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); + return OPJ_FALSE; + } + return OPJ_TRUE; } /** * Writes the SIZ marker (image and tile size) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - OPJ_UINT32 i; - OPJ_UINT32 l_size_len; - OPJ_BYTE * l_current_ptr; - opj_image_t * l_image = 00; - opj_cp_v2_t *cp = 00; - opj_image_comp_t * l_img_comp = 00; - - /* preconditions */ - assert(p_stream != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - l_image = p_j2k->m_private_image; - cp = &(p_j2k->m_cp); - l_size_len = 40 + 3 * l_image->numcomps; - l_img_comp = l_image->comps; - - if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_size_len); - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len; - } +opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_size_len; + OPJ_BYTE * l_current_ptr; + opj_image_t * l_image = 00; + opj_cp_v2_t *cp = 00; + opj_image_comp_t * l_img_comp = 00; + + /* preconditions */ + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_private_image; + cp = &(p_j2k->m_cp); + l_size_len = 40 + 3 * l_image->numcomps; + l_img_comp = l_image->comps; + + if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len; + } - l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - /* write SOC identifier */ - opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */ - l_current_ptr+=2; + /* write SOC identifier */ + opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */ + l_current_ptr+=2; - opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */ - l_current_ptr+=2; + opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */ + l_current_ptr+=2; - opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */ - l_current_ptr+=2; + opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */ + l_current_ptr+=2; - opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */ - l_current_ptr+=4; + opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */ + l_current_ptr+=4; - opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */ - l_current_ptr+=4; + opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */ + l_current_ptr+=4; - opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */ - l_current_ptr+=4; + opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */ + l_current_ptr+=4; - opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */ - l_current_ptr+=4; + opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */ + l_current_ptr+=4; - opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */ - l_current_ptr+=4; + opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */ + l_current_ptr+=4; - opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */ - l_current_ptr+=4; + opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */ + l_current_ptr+=4; - opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */ - l_current_ptr+=4; + opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */ + l_current_ptr+=4; - opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */ - l_current_ptr+=4; + opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */ + l_current_ptr+=4; - opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */ - l_current_ptr+=2; + opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */ + l_current_ptr+=2; - for (i = 0; i < l_image->numcomps; ++i) { - /* TODO here with MCT ? */ - opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */ - ++l_current_ptr; + for (i = 0; i < l_image->numcomps; ++i) { + /* TODO here with MCT ? */ + opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */ + ++l_current_ptr; - opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */ - ++l_current_ptr; + opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */ + ++l_current_ptr; - opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */ - ++l_current_ptr; + opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */ + ++l_current_ptr; - ++l_img_comp; - } + ++l_img_comp; + } - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,p_manager) != l_size_len) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,p_manager) != l_size_len) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a SIZ marker (image and tile size) - * @param p_header_data the data contained in the SIZ box. - * @param jp2 the jpeg2000 file codec. - * @param p_header_size the size of the data contained in the SIZ marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the SIZ box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the SIZ marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -1922,371 +1925,372 @@ static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_size, i; - OPJ_UINT32 l_nb_comp; - OPJ_UINT32 l_nb_comp_remain; - OPJ_UINT32 l_remaining_size; - OPJ_UINT32 l_nb_tiles; - OPJ_UINT32 l_tmp; - opj_image_t *l_image = 00; - opj_cp_v2_t *l_cp = 00; - opj_image_comp_t * l_img_comp = 00; - opj_tcp_v2_t * l_current_tile_param = 00; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_header_data != 00); - - l_image = p_j2k->m_private_image; - l_cp = &(p_j2k->m_cp); - - /* minimum size == 39 - 3 (= minimum component parameter) */ - if (p_header_size < 36) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); - return OPJ_FALSE; - } - - l_remaining_size = p_header_size - 36; - l_nb_comp = l_remaining_size / 3; - l_nb_comp_remain = l_remaining_size % 3; - if (l_nb_comp_remain != 0){ - opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); - return OPJ_FALSE; - } - - l_size = p_header_size + 2; /* Lsiz */ - - opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */ - p_header_data+=2; - l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp; - opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */ - p_header_data+=4; - opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */ - p_header_data+=4; - opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */ - p_header_data+=4; - opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */ - p_header_data+=4; - opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */ - p_header_data+=4; - opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */ - p_header_data+=4; - opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */ - p_header_data+=4; - opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */ - p_header_data+=4; - opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */ - p_header_data+=2; - if (l_tmp < 16385) - l_image->numcomps = (OPJ_UINT16) l_tmp; - else { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp); - return OPJ_FALSE; - } - - if (l_image->numcomps != l_nb_comp) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", l_image->numcomps, l_nb_comp); - return OPJ_FALSE; - } + OPJ_UINT32 l_size, i; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_comp_remain; + OPJ_UINT32 l_remaining_size; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 l_tmp; + opj_image_t *l_image = 00; + opj_cp_v2_t *l_cp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_tcp_v2_t * l_current_tile_param = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); + + l_image = p_j2k->m_private_image; + l_cp = &(p_j2k->m_cp); + + /* minimum size == 39 - 3 (= minimum component parameter) */ + if (p_header_size < 36) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); + return OPJ_FALSE; + } + + l_remaining_size = p_header_size - 36; + l_nb_comp = l_remaining_size / 3; + l_nb_comp_remain = l_remaining_size % 3; + if (l_nb_comp_remain != 0){ + opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); + return OPJ_FALSE; + } + + l_size = p_header_size + 2; /* Lsiz */ + + opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */ + p_header_data+=2; + l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */ + p_header_data+=4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */ + p_header_data+=4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */ + p_header_data+=4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */ + p_header_data+=4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */ + p_header_data+=4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */ + p_header_data+=4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */ + p_header_data+=4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */ + p_header_data+=4; + opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */ + p_header_data+=2; + if (l_tmp < 16385) + l_image->numcomps = (OPJ_UINT16) l_tmp; + else { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp); + return OPJ_FALSE; + } + + if (l_image->numcomps != l_nb_comp) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", l_image->numcomps, l_nb_comp); + return OPJ_FALSE; + } #ifdef USE_JPWL - if (l_cp->correct) { - /* if JPWL is on, we check whether TX errors have damaged - too much the SIZ parameters */ - if (!(l_image->x1 * l_image->y1)) { - opj_event_msg_v2(p_manager, EVT_ERROR, - "JPWL: bad image size (%d x %d)\n", - l_image->x1, l_image->y1); - if (!JPWL_ASSUME || JPWL_ASSUME) { - opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - } - - /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ? - if (l_image->numcomps != ((len - 38) / 3)) { - opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, - "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n", - l_image->numcomps, ((len - 38) / 3)); - if (!JPWL_ASSUME) { - opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - */ /* we try to correct */ - /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"); - if (l_image->numcomps < ((len - 38) / 3)) { - len = 38 + 3 * l_image->numcomps; - opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", - len); - } else { - l_image->numcomps = ((len - 38) / 3); - opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n", - l_image->numcomps); - } - } - */ - - /* update components number in the jpwl_exp_comps filed */ - l_cp->exp_comps = l_image->numcomps; - } + if (l_cp->correct) { + /* if JPWL is on, we check whether TX errors have damaged + too much the SIZ parameters */ + if (!(l_image->x1 * l_image->y1)) { + opj_event_msg_v2(p_manager, EVT_ERROR, + "JPWL: bad image size (%d x %d)\n", + l_image->x1, l_image->y1); + if (!JPWL_ASSUME || JPWL_ASSUME) { + opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + } + + /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ? + if (l_image->numcomps != ((len - 38) / 3)) { + opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n", + l_image->numcomps, ((len - 38) / 3)); + if (!JPWL_ASSUME) { + opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + */ /* we try to correct */ + /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"); + if (l_image->numcomps < ((len - 38) / 3)) { + len = 38 + 3 * l_image->numcomps; + opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", + len); + } else { + l_image->numcomps = ((len - 38) / 3); + opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n", + l_image->numcomps); + } + } + */ + + /* update components number in the jpwl_exp_comps filed */ + l_cp->exp_comps = l_image->numcomps; + } #endif /* USE_JPWL */ - /* Allocate the resulting image components */ - l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t)); - if (l_image->comps == 00){ - l_image->numcomps = 0; - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } - - memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t)); - l_img_comp = l_image->comps; - - /* Read the component information */ - for (i = 0; i < l_image->numcomps; ++i){ - OPJ_UINT32 tmp; - opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */ - ++p_header_data; - l_img_comp->prec = (tmp & 0x7f) + 1; - l_img_comp->sgnd = tmp >> 7; - opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */ - ++p_header_data; - l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */ - opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */ - ++p_header_data; - l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */ + /* Allocate the resulting image components */ + l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t)); + if (l_image->comps == 00){ + l_image->numcomps = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + + memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t)); + l_img_comp = l_image->comps; + + /* Read the component information */ + for (i = 0; i < l_image->numcomps; ++i){ + OPJ_UINT32 tmp; + opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */ + ++p_header_data; + l_img_comp->prec = (tmp & 0x7f) + 1; + l_img_comp->sgnd = tmp >> 7; + opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */ + ++p_header_data; + l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */ + opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */ + ++p_header_data; + l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */ #ifdef USE_JPWL - if (l_cp->correct) { - /* if JPWL is on, we check whether TX errors have damaged - too much the SIZ parameters, again */ - if (!(l_image->comps[i].dx * l_image->comps[i].dy)) { - opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, - "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n", - i, i, l_image->comps[i].dx, l_image->comps[i].dy); - if (!JPWL_ASSUME) { - opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - /* we try to correct */ - opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"); - if (!l_image->comps[i].dx) { - l_image->comps[i].dx = 1; - opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n", - i, l_image->comps[i].dx); - } - if (!l_image->comps[i].dy) { - l_image->comps[i].dy = 1; - opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n", - i, l_image->comps[i].dy); - } - } - } + if (l_cp->correct) { + /* if JPWL is on, we check whether TX errors have damaged + too much the SIZ parameters, again */ + if (!(l_image->comps[i].dx * l_image->comps[i].dy)) { + opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n", + i, i, l_image->comps[i].dx, l_image->comps[i].dy); + if (!JPWL_ASSUME) { + opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"); + if (!l_image->comps[i].dx) { + l_image->comps[i].dx = 1; + opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n", + i, l_image->comps[i].dx); + } + if (!l_image->comps[i].dy) { + l_image->comps[i].dy = 1; + opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n", + i, l_image->comps[i].dy); + } + } + } #endif /* USE_JPWL */ - l_img_comp->resno_decoded = 0; /* number of resolution decoded */ - l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */ - ++l_img_comp; - } - - /* Compute the number of tiles */ - l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx); - l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy); - l_nb_tiles = l_cp->tw * l_cp->th; - - /* Define the tiles which will be decoded */ - if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) { - p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx; - p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy; - p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), l_cp->tdx); - p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), l_cp->tdy); - } - else { - p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; - p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; - p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; - p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; - } + l_img_comp->resno_decoded = 0; /* number of resolution decoded */ + l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */ + ++l_img_comp; + } + + /* Compute the number of tiles */ + l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx); + l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy); + l_nb_tiles = l_cp->tw * l_cp->th; + + /* Define the tiles which will be decoded */ + if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) { + p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx; + p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy; + p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), l_cp->tdx); + p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), l_cp->tdy); + } + else { + p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; + p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; + p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; + p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; + } #ifdef USE_JPWL - if (l_cp->correct) { - /* if JPWL is on, we check whether TX errors have damaged - too much the SIZ parameters */ - if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) { - opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, - "JPWL: bad number of tiles (%d x %d)\n", - l_cp->tw, l_cp->th); - if (!JPWL_ASSUME) { - opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - /* we try to correct */ - opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"); - if (l_cp->tw < 1) { - l_cp->tw= 1; - opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n", - l_cp->tw); - } - if (l_cp->tw > l_cp->max_tiles) { - l_cp->tw= 1; - opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n" - "- setting %d tiles in x => HYPOTHESIS!!!\n", - l_cp->max_tiles, l_cp->tw); - } - if (l_cp->th < 1) { - l_cp->th= 1; - opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n", - l_cp->th); - } - if (l_cp->th > l_cp->max_tiles) { - l_cp->th= 1; - opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n", - "- setting %d tiles in y => HYPOTHESIS!!!\n", - l_cp->max_tiles, l_cp->th); - } - } - } + if (l_cp->correct) { + /* if JPWL is on, we check whether TX errors have damaged + too much the SIZ parameters */ + if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) { + opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: bad number of tiles (%d x %d)\n", + l_cp->tw, l_cp->th); + if (!JPWL_ASSUME) { + opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"); + if (l_cp->tw < 1) { + l_cp->tw= 1; + opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n", + l_cp->tw); + } + if (l_cp->tw > l_cp->max_tiles) { + l_cp->tw= 1; + opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n" + "- setting %d tiles in x => HYPOTHESIS!!!\n", + l_cp->max_tiles, l_cp->tw); + } + if (l_cp->th < 1) { + l_cp->th= 1; + opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n", + l_cp->th); + } + if (l_cp->th > l_cp->max_tiles) { + l_cp->th= 1; + opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n", + "- setting %d tiles in y => HYPOTHESIS!!!\n", + l_cp->max_tiles, l_cp->th); + } + } + } #endif /* USE_JPWL */ - /* memory allocations */ - l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t)); - if (l_cp->tcps == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } - memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t)); + /* memory allocations */ + l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t)); + if (l_cp->tcps == 00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t)); #ifdef USE_JPWL - if (l_cp->correct) { - if (!l_cp->tcps) { - opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, - "JPWL: could not alloc tcps field of cp\n"); - if (!JPWL_ASSUME || JPWL_ASSUME) { - opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - } - } + if (l_cp->correct) { + if (!l_cp->tcps) { + opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: could not alloc tcps field of cp\n"); + if (!JPWL_ASSUME || JPWL_ASSUME) { + opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + } + } #endif /* USE_JPWL */ - p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps = - (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t)); - if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } - memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t)); - - p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = - (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t)); - - if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } - memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t)); - p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS; - - p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = - (opj_simple_mcc_decorrelation_data_t*) - opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t)); - - if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } - memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t)); - p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS; - - /* set up default dc level shift */ - for (i=0;inumcomps;++i) { - if (! l_image->comps[i].sgnd) { - p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1); - } - } - - l_current_tile_param = l_cp->tcps; - for (i = 0; i < l_nb_tiles; ++i) { - l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t)); - if (l_current_tile_param->tccps == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } - memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t)); - - ++l_current_tile_param; - } - - p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */ - opj_image_comp_header_update(l_image,l_cp); - - return OPJ_TRUE; + p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps = + (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t)); + if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t)); + + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = + (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t)); + + if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t)); + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS; + + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = + (opj_simple_mcc_decorrelation_data_t*) + opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t)); + + if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t)); + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS; + + /* set up default dc level shift */ + for (i=0;inumcomps;++i) { + if (! l_image->comps[i].sgnd) { + p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1); + } + } + + l_current_tile_param = l_cp->tcps; + for (i = 0; i < l_nb_tiles; ++i) { + l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t)); + if (l_current_tile_param->tccps == 00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t)); + + ++l_current_tile_param; + } + + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */ + opj_image_comp_header_update(l_image,l_cp); + + return OPJ_TRUE; } /** * Writes the COM marker (comment) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager +opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_comment_size; - OPJ_UINT32 l_total_com_size; - const OPJ_CHAR *l_comment; - OPJ_BYTE * l_current_ptr = 00; - - // preconditions - assert(p_j2k != 00); - assert(p_stream != 00); - assert(p_manager != 00); - - l_comment = p_j2k->m_cp.comment; - l_comment_size = strlen(l_comment); - l_total_com_size = l_comment_size + 6; - - if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_total_com_size); - - if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size; - } - - l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - - opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */ - l_current_ptr+=2; - - opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */ - l_current_ptr+=2; - - opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */ - l_current_ptr+=2; - - memcpy( l_current_ptr,l_comment,l_comment_size); - - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) { - return OPJ_FALSE; - } - - return OPJ_TRUE; + OPJ_UINT32 l_comment_size; + OPJ_UINT32 l_total_com_size; + const OPJ_CHAR *l_comment; + OPJ_BYTE * l_current_ptr = 00; + + // preconditions + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + l_comment = p_j2k->m_cp.comment; + l_comment_size = strlen(l_comment); + l_total_com_size = l_comment_size + 6; + + if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write the COM marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size; + } + + l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */ + l_current_ptr+=2; + + opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */ + l_current_ptr+=2; + + opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */ + l_current_ptr+=2; + + memcpy( l_current_ptr,l_comment,l_comment_size); + + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; } /** * Reads a COM marker (comments) - * @param p_header_data the data contained in the COM box. - * @param jp2 the jpeg2000 file codec. - * @param p_header_size the size of the data contained in the COM marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the COM box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -2294,100 +2298,100 @@ static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_header_data != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); (void)p_header_size; - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the COD marker (Coding style default) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - OPJ_UINT32 l_code_size,l_remaining_size; - OPJ_BYTE * l_current_data = 00; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_cp = &(p_j2k->m_cp); - l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; - l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0); - l_remaining_size = l_code_size; - - if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_code_size); - - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size; - } +opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + OPJ_UINT32 l_code_size,l_remaining_size; + OPJ_BYTE * l_current_data = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0); + l_remaining_size = l_code_size; + + if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size; + } - l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */ - l_current_data += 2; + opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */ - l_current_data += 2; + opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */ - ++l_current_data; + opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */ + ++l_current_data; - opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */ - ++l_current_data; + opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */ + ++l_current_data; - opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */ - l_current_data+=2; + opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */ + l_current_data+=2; - opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */ - ++l_current_data; + opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */ + ++l_current_data; - l_remaining_size -= 9; + l_remaining_size -= 9; - if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n"); - return OPJ_FALSE; - } + if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n"); + return OPJ_FALSE; + } - if (l_remaining_size != 0) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n"); - return OPJ_FALSE; - } + if (l_remaining_size != 0) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n"); + return OPJ_FALSE; + } - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_code_size,p_manager) != l_code_size) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_code_size,p_manager) != l_code_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a COD marker (Coding Styke defaults) - * @param p_header_data the data contained in the COD box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the COD marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the COD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COD marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -2395,217 +2399,223 @@ static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - /* loop */ - OPJ_UINT32 i; - OPJ_UINT32 l_tmp; - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - opj_image_t *l_image = 00; - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - l_image = p_j2k->m_private_image; - l_cp = &(p_j2k->m_cp); - - /* If we are in the first tile-part header of the current tile */ - l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? - &l_cp->tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; - - /* Make sure room is sufficient */ - if (p_header_size < 5) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n"); - return OPJ_FALSE; - } - - opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */ - ++p_header_data; - opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */ - ++p_header_data; - l_tcp->prg = (OPJ_PROG_ORDER) l_tmp; - opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */ - p_header_data+=2; - - /* If user didn't set a number layer to decode take the max specify in the codestream. */ - if (l_cp->m_specific_param.m_dec.m_layer) { - l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer; - } - else { - l_tcp->num_layers_to_decode = l_tcp->numlayers; - } - - opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */ - ++p_header_data; - - p_header_size -= 5; - for (i = 0; i < l_image->numcomps; ++i) { - l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT; - } - - if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n"); - return OPJ_FALSE; - } - - if (p_header_size != 0) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n"); - return OPJ_FALSE; - } - - /* Apply the coding style to other components of the current tile or the m_default_tcp*/ - opj_j2k_copy_tile_component_parameters(p_j2k); - - /* Index */ + /* loop */ + OPJ_UINT32 i; + OPJ_UINT32 l_tmp; + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + opj_image_t *l_image = 00; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_private_image; + l_cp = &(p_j2k->m_cp); + + /* If we are in the first tile-part header of the current tile */ + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + /* Make sure room is sufficient */ + if (p_header_size < 5) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */ + ++p_header_data; + opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */ + ++p_header_data; + l_tcp->prg = (OPJ_PROG_ORDER) l_tmp; + opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */ + p_header_data+=2; + + /* If user didn't set a number layer to decode take the max specify in the codestream. */ + if (l_cp->m_specific_param.m_dec.m_layer) { + l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer; + } + else { + l_tcp->num_layers_to_decode = l_tcp->numlayers; + } + + opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */ + ++p_header_data; + + p_header_size -= 5; + for (i = 0; i < l_image->numcomps; ++i) { + l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT; + } + + if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n"); + return OPJ_FALSE; + } + + if (p_header_size != 0) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n"); + return OPJ_FALSE; + } + + /* Apply the coding style to other components of the current tile or the m_default_tcp*/ + opj_j2k_copy_tile_component_parameters(p_j2k); + + /* Index */ #ifdef WIP_REMOVE_MSD - if (p_j2k->cstr_info) { - /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/ - p_j2k->cstr_info->prog = l_tcp->prg; - p_j2k->cstr_info->numlayers = l_tcp->numlayers; - p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32)); - for (i = 0; i < l_image->numcomps; ++i) { - p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1; - } - } + if (p_j2k->cstr_info) { + /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/ + p_j2k->cstr_info->prog = l_tcp->prg; + p_j2k->cstr_info->numlayers = l_tcp->numlayers; + p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32)); + for (i = 0; i < l_image->numcomps; ++i) { + p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1; + } + } #endif - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the COC marker (Coding style component) * - * @param p_comp_no the index of the component to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_comp_no, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - OPJ_UINT32 l_coc_size,l_remaining_size; - OPJ_UINT32 l_comp_room; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2; - - l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); - - if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_coc_size); - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size; - } + OPJ_UINT32 p_comp_no, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + OPJ_UINT32 l_coc_size,l_remaining_size; + OPJ_UINT32 l_comp_room; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2; + + l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); + + if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = (OPJ_BYTE*)opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_coc_size); + + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size; + } - opj_j2k_write_coc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager); + opj_j2k_write_coc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager); - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_coc_size,p_manager) != l_coc_size) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_coc_size,p_manager) != l_coc_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the COC marker (Coding style component) * - * @param p_comp_no the index of the component to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ void opj_j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_comp_no, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager ) { - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - OPJ_UINT32 l_coc_size,l_remaining_size; - OPJ_BYTE * l_current_data = 00; - opj_image_t *l_image = 00; - OPJ_UINT32 l_comp_room; + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + OPJ_UINT32 l_coc_size,l_remaining_size; + OPJ_BYTE * l_current_data = 00; + opj_image_t *l_image = 00; + OPJ_UINT32 l_comp_room; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); - l_cp = &(p_j2k->m_cp); - l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; - l_image = p_j2k->m_private_image; - l_comp_room = (l_image->numcomps <= 256) ? 1 : 2; + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_image = p_j2k->m_private_image; + l_comp_room = (l_image->numcomps <= 256) ? 1 : 2; - l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); - l_remaining_size = l_coc_size; + l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); + l_remaining_size = l_coc_size; - l_current_data = p_data; + l_current_data = p_data; - opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */ - l_current_data += 2; + opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */ - l_current_data += 2; + opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */ + l_current_data += 2; - opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */ - l_current_data+=l_comp_room; + opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */ + l_current_data+=l_comp_room; - opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */ - ++l_current_data; + opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */ + ++l_current_data; - l_remaining_size -= (5 + l_comp_room); - opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager); - * p_data_written = l_coc_size; + l_remaining_size -= (5 + l_comp_room); + opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager); + * p_data_written = l_coc_size; } /** * Gets the maximum size taken by a coc. * - * @param p_j2k the jpeg2000 codec to use. + * @param p_j2k the jpeg2000 codec to use. */ OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k) { - OPJ_UINT32 i,j; - OPJ_UINT32 l_nb_comp; - OPJ_UINT32 l_nb_tiles; - OPJ_UINT32 l_max = 0; + OPJ_UINT32 i,j; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 l_max = 0; - /* preconditions */ + /* preconditions */ - l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; - l_nb_comp = p_j2k->m_private_image->numcomps; + l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; + l_nb_comp = p_j2k->m_private_image->numcomps; - for (i=0;im_cp); - l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/ - &l_cp->tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; - l_image = p_j2k->m_private_image; - - l_comp_room = l_image->numcomps <= 256 ? 1 : 2; - - /* make sure room is sufficient*/ - if (p_header_size < l_comp_room + 1) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n"); - return OPJ_FALSE; - } - p_header_size -= l_comp_room + 1; + opj_cp_v2_t *l_cp = NULL; + opj_tcp_v2_t *l_tcp = NULL; + opj_image_t *l_image = NULL; + OPJ_UINT32 l_comp_room; + OPJ_UINT32 l_comp_no; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/ + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_image = p_j2k->m_private_image; + + l_comp_room = l_image->numcomps <= 256 ? 1 : 2; + + /* make sure room is sufficient*/ + if (p_header_size < l_comp_room + 1) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n"); + return OPJ_FALSE; + } + p_header_size -= l_comp_room + 1; - opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */ - p_header_data += l_comp_room; - if (l_comp_no >= l_image->numcomps) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n"); - return OPJ_FALSE; - } + opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */ + p_header_data += l_comp_room; + if (l_comp_no >= l_image->numcomps) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n"); + return OPJ_FALSE; + } - opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */ - ++p_header_data ; + opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */ + ++p_header_data ; - if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n"); - return OPJ_FALSE; - } + if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n"); + return OPJ_FALSE; + } - if (p_header_size != 0) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n"); - return OPJ_FALSE; - } - return OPJ_TRUE; + if (p_header_size != 0) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n"); + return OPJ_FALSE; + } + return OPJ_TRUE; } /** * Writes the QCD marker (quantization default) * - * @param p_comp_number the index of the component to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_number the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager +opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - OPJ_UINT32 l_qcd_size,l_remaining_size; - OPJ_BYTE * l_current_data = 00; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_cp = &(p_j2k->m_cp); - l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; - l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0); - l_remaining_size = l_qcd_size; - - if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_qcd_size); - - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size; - } + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + OPJ_UINT32 l_qcd_size,l_remaining_size; + OPJ_BYTE * l_current_data = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0); + l_remaining_size = l_qcd_size; + + if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size; + } - l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */ - l_current_data += 2; + opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */ - l_current_data += 2; + opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */ + l_current_data += 2; - l_remaining_size -= 4; + l_remaining_size -= 4; - if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n"); - return OPJ_FALSE; - } + if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n"); + return OPJ_FALSE; + } - if (l_remaining_size != 0) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n"); - return OPJ_FALSE; - } + if (l_remaining_size != 0) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n"); + return OPJ_FALSE; + } - if (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcd_size,p_manager) != l_qcd_size) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcd_size,p_manager) != l_qcd_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a QCD marker (Quantization defaults) - * @param p_header_data the data contained in the QCD box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the QCD marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the QCD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCD marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -2742,127 +2752,128 @@ static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); - if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n"); - return OPJ_FALSE; - } + if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n"); + return OPJ_FALSE; + } - if (p_header_size != 0) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n"); - return OPJ_FALSE; - } + if (p_header_size != 0) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n"); + return OPJ_FALSE; + } - /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */ - opj_j2k_copy_tile_quantization_parameters(p_j2k); + /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */ + opj_j2k_copy_tile_quantization_parameters(p_j2k); - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the QCC marker (quantization component) * - * @param p_comp_no the index of the component to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_comp_no, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager +opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k, + OPJ_UINT32 p_comp_no, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_qcc_size,l_remaining_size; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); - l_remaining_size = l_qcc_size; - - if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_qcc_size); - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size; - } + OPJ_UINT32 l_qcc_size,l_remaining_size; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); + l_remaining_size = l_qcc_size; + + if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size; + } - opj_j2k_write_qcc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager); + opj_j2k_write_qcc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager); - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcc_size,p_manager) != l_qcc_size) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcc_size,p_manager) != l_qcc_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the QCC marker (quantization component) * - * @param p_comp_no the index of the component to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ void opj_j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_comp_no, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_qcc_size,l_remaining_size; - OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_qcc_size,l_remaining_size; + OPJ_BYTE * l_current_data = 00; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); - l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); - l_remaining_size = l_qcc_size; + l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); + l_remaining_size = l_qcc_size; - l_current_data = p_data; + l_current_data = p_data; - opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */ - l_current_data += 2; + opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */ + l_current_data += 2; - if (p_j2k->m_private_image->numcomps <= 256) { - --l_qcc_size; + if (p_j2k->m_private_image->numcomps <= 256) { + --l_qcc_size; - opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */ - l_current_data += 2; + opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */ + l_current_data += 2; - opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */ - ++l_current_data; + opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */ + ++l_current_data; - /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */ - l_remaining_size -= 6; - } - else { - opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */ - l_current_data += 2; + /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */ + l_remaining_size -= 6; + } + else { + opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */ + l_current_data += 2; - opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */ - l_current_data+=2; + opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */ + l_current_data+=2; - l_remaining_size -= 6; - } + l_remaining_size -= 6; + } - opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager); + opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager); - *p_data_written = l_qcc_size; + *p_data_written = l_qcc_size; } /** @@ -2870,15 +2881,15 @@ void opj_j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k, */ OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k) { - return opj_j2k_get_max_coc_size(p_j2k); + return opj_j2k_get_max_coc_size(p_j2k); } /** * Reads a QCC marker (Quantization component) - * @param p_header_data the data contained in the QCC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the QCC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the QCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCC marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -2886,214 +2897,214 @@ static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_num_comp,l_comp_no; - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - l_num_comp = p_j2k->m_private_image->numcomps; - - if (l_num_comp <= 256) { - if (p_header_size < 1) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n"); - return OPJ_FALSE; - } - opj_read_bytes(p_header_data,&l_comp_no,1); - ++p_header_data; - --p_header_size; - } - else { - if (p_header_size < 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n"); - return OPJ_FALSE; - } - opj_read_bytes(p_header_data,&l_comp_no,2); - p_header_data+=2; - p_header_size-=2; - } + OPJ_UINT32 l_num_comp,l_comp_no; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_num_comp = p_j2k->m_private_image->numcomps; + + if (l_num_comp <= 256) { + if (p_header_size < 1) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return OPJ_FALSE; + } + opj_read_bytes(p_header_data,&l_comp_no,1); + ++p_header_data; + --p_header_size; + } + else { + if (p_header_size < 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return OPJ_FALSE; + } + opj_read_bytes(p_header_data,&l_comp_no,2); + p_header_data+=2; + p_header_size-=2; + } #ifdef USE_JPWL - if (p_j2k->m_cp.correct) { - - static OPJ_UINT32 backup_compno = 0; - - /* compno is negative or larger than the number of components!!! */ - if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) { - opj_event_msg_v2(p_manager, EVT_ERROR, - "JPWL: bad component number in QCC (%d out of a maximum of %d)\n", - l_comp_no, l_num_comp); - if (!JPWL_ASSUME) { - opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - /* we try to correct */ - l_comp_no = backup_compno % l_num_comp; - opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n" - "- setting component number to %d\n", - l_comp_no); - } - - /* keep your private count of tiles */ - backup_compno++; - }; + if (p_j2k->m_cp.correct) { + + static OPJ_UINT32 backup_compno = 0; + + /* compno is negative or larger than the number of components!!! */ + if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) { + opj_event_msg_v2(p_manager, EVT_ERROR, + "JPWL: bad component number in QCC (%d out of a maximum of %d)\n", + l_comp_no, l_num_comp); + if (!JPWL_ASSUME) { + opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + l_comp_no = backup_compno % l_num_comp; + opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n" + "- setting component number to %d\n", + l_comp_no); + } + + /* keep your private count of tiles */ + backup_compno++; + }; #endif /* USE_JPWL */ - if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n"); - return OPJ_FALSE; - } + if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return OPJ_FALSE; + } - if (p_header_size != 0) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n"); - return OPJ_FALSE; - } + if (p_header_size != 0) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the POC marker (Progression Order Change) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager +opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_nb_comp; - OPJ_UINT32 l_nb_poc; - OPJ_UINT32 l_poc_size; - OPJ_UINT32 l_written_size = 0; - opj_tcp_v2_t *l_tcp = 00; - opj_tccp_t *l_tccp = 00; - OPJ_UINT32 l_poc_room; - - // preconditions - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; - l_tccp = &l_tcp->tccps[0]; - l_nb_comp = p_j2k->m_private_image->numcomps; - l_nb_poc = 1 + l_tcp->numpocs; - - if (l_nb_comp <= 256) { - l_poc_room = 1; - } - else { - l_poc_room = 2; - } - l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; - - if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_poc_size); - - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size; - } - - opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager); - - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) { - return OPJ_FALSE; - } - - return OPJ_TRUE; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_poc; + OPJ_UINT32 l_poc_size; + OPJ_UINT32 l_written_size = 0; + opj_tcp_v2_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + OPJ_UINT32 l_poc_room; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; + l_tccp = &l_tcp->tccps[0]; + l_nb_comp = p_j2k->m_private_image->numcomps; + l_nb_poc = 1 + l_tcp->numpocs; + + if (l_nb_comp <= 256) { + l_poc_room = 1; + } + else { + l_poc_room = 2; + } + l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; + + if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size; + } + + opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager); + + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; } /** * Writes the POC marker (Progression Order Change) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ void opj_j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - opj_event_mgr_t * p_manager + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + opj_event_mgr_t * p_manager ) { - OPJ_UINT32 i; - OPJ_BYTE * l_current_data = 00; - OPJ_UINT32 l_nb_comp; - OPJ_UINT32 l_nb_poc; - OPJ_UINT32 l_poc_size; - opj_image_t *l_image = 00; - opj_tcp_v2_t *l_tcp = 00; - opj_tccp_t *l_tccp = 00; - opj_poc_t *l_current_poc = 00; - OPJ_UINT32 l_poc_room; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - - l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; - l_tccp = &l_tcp->tccps[0]; - l_image = p_j2k->m_private_image; - l_nb_comp = l_image->numcomps; - l_nb_poc = 1 + l_tcp->numpocs; - - if (l_nb_comp <= 256) { - l_poc_room = 1; - } - else { - l_poc_room = 2; - } + OPJ_UINT32 i; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_poc; + OPJ_UINT32 l_poc_size; + opj_image_t *l_image = 00; + opj_tcp_v2_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + opj_poc_t *l_current_poc = 00; + OPJ_UINT32 l_poc_room; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + + l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; + l_tccp = &l_tcp->tccps[0]; + l_image = p_j2k->m_private_image; + l_nb_comp = l_image->numcomps; + l_nb_poc = 1 + l_tcp->numpocs; + + if (l_nb_comp <= 256) { + l_poc_room = 1; + } + else { + l_poc_room = 2; + } - l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; + l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; - l_current_data = p_data; + l_current_data = p_data; - opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */ - l_current_data += 2; + opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */ - l_current_data += 2; + opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */ + l_current_data += 2; - l_current_poc = l_tcp->pocs; - for (i = 0; i < l_nb_poc; ++i) { - opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */ - ++l_current_data; + l_current_poc = l_tcp->pocs; + for (i = 0; i < l_nb_poc; ++i) { + opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */ + ++l_current_data; - opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */ - l_current_data+=l_poc_room; + opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */ + l_current_data+=l_poc_room; - opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */ - l_current_data+=2; + opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */ + l_current_data+=2; - opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */ - ++l_current_data; + opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */ + ++l_current_data; - opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */ - l_current_data+=l_poc_room; + opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */ + l_current_data+=l_poc_room; - opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */ - ++l_current_data; + opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */ + ++l_current_data; - /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/ - l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers); - l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions); - l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp); + /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/ + l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers); + l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions); + l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp); - ++l_current_poc; - } + ++l_current_poc; + } - *p_data_written = l_poc_size; + *p_data_written = l_poc_size; } /** @@ -3101,22 +3112,22 @@ void opj_j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k, */ OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k) { - opj_tcp_v2_t * l_tcp = 00; - OPJ_UINT32 l_nb_tiles = 0; - OPJ_UINT32 l_max_poc = 0; - OPJ_UINT32 i; + opj_tcp_v2_t * l_tcp = 00; + OPJ_UINT32 l_nb_tiles = 0; + OPJ_UINT32 l_max_poc = 0; + OPJ_UINT32 i; - l_tcp = p_j2k->m_cp.tcps; - l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + l_tcp = p_j2k->m_cp.tcps; + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; - for (i=0;inumpocs); - ++l_tcp; - } + for (i=0;inumpocs); + ++l_tcp; + } - ++l_max_poc; + ++l_max_poc; - return 4 + 9 * l_max_poc; + return 4 + 9 * l_max_poc; } /** @@ -3124,61 +3135,61 @@ OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k) */ OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k) { - OPJ_UINT32 i; - OPJ_UINT32 l_nb_tiles; - OPJ_UINT32 l_max = 0; - opj_tcp_v2_t * l_tcp = 00; + OPJ_UINT32 i; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 l_max = 0; + opj_tcp_v2_t * l_tcp = 00; - l_tcp = p_j2k->m_cp.tcps; - l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; + l_tcp = p_j2k->m_cp.tcps; + l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; - for (i=0;im_nb_tile_parts); + for (i=0;im_nb_tile_parts); - ++l_tcp; - } + ++l_tcp; + } - return 12 * l_max; + return 12 * l_max; } /** * Gets the maximum size taken by the headers of the SOT. * - * @param p_j2k the jpeg2000 codec to use. + * @param p_j2k the jpeg2000 codec to use. */ OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k) { - OPJ_UINT32 l_nb_bytes = 0; - OPJ_UINT32 l_nb_comps; - OPJ_UINT32 l_coc_bytes,l_qcc_bytes; + OPJ_UINT32 l_nb_bytes = 0; + OPJ_UINT32 l_nb_comps; + OPJ_UINT32 l_coc_bytes,l_qcc_bytes; - l_nb_comps = p_j2k->m_private_image->numcomps - 1; - l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k); + l_nb_comps = p_j2k->m_private_image->numcomps - 1; + l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k); - if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) { - l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k); - l_nb_bytes += l_nb_comps * l_coc_bytes; + if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) { + l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k); + l_nb_bytes += l_nb_comps * l_coc_bytes; - l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k); - l_nb_bytes += l_nb_comps * l_qcc_bytes; - } + l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k); + l_nb_bytes += l_nb_comps * l_qcc_bytes; + } - l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k); + l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k); - /*** DEVELOPER CORNER, Add room for your headers ***/ + /*** DEVELOPER CORNER, Add room for your headers ***/ - return l_nb_bytes; + return l_nb_bytes; } /** * Reads a POC marker (Progression Order Change) * - * @param p_header_data the data contained in the POC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the POC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -3186,80 +3197,80 @@ static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - OPJ_UINT32 i, l_nb_comp, l_tmp; - opj_image_t * l_image = 00; - OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining; - OPJ_UINT32 l_chunk_size, l_comp_room; - - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - opj_poc_t *l_current_poc = 00; - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - l_image = p_j2k->m_private_image; - l_nb_comp = l_image->numcomps; - if (l_nb_comp <= 256) { - l_comp_room = 1; - } - else { - l_comp_room = 2; - } - l_chunk_size = 5 + 2 * l_comp_room; - l_current_poc_nb = p_header_size / l_chunk_size; - l_current_poc_remaining = p_header_size % l_chunk_size; - - if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n"); - return OPJ_FALSE; - } - - l_cp = &(p_j2k->m_cp); - l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? - &l_cp->tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; - l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0; - l_current_poc_nb += l_old_poc_nb; - - assert(l_current_poc_nb < 32); - - /* now poc is in use.*/ - l_tcp->POC = 1; - - l_current_poc = &l_tcp->pocs[l_old_poc_nb]; - for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) { - opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */ - ++p_header_data; - opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */ - p_header_data+=l_comp_room; - opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */ - p_header_data+=2; - opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */ - ++p_header_data; - opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */ - p_header_data+=l_comp_room; - opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */ - ++p_header_data; - l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp; - /* make sure comp is in acceptable bounds */ - l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp); - ++l_current_poc; - } - - l_tcp->numpocs = l_current_poc_nb - 1; - return OPJ_TRUE; + OPJ_UINT32 i, l_nb_comp, l_tmp; + opj_image_t * l_image = 00; + OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining; + OPJ_UINT32 l_chunk_size, l_comp_room; + + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + opj_poc_t *l_current_poc = 00; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_private_image; + l_nb_comp = l_image->numcomps; + if (l_nb_comp <= 256) { + l_comp_room = 1; + } + else { + l_comp_room = 2; + } + l_chunk_size = 5 + 2 * l_comp_room; + l_current_poc_nb = p_header_size / l_chunk_size; + l_current_poc_remaining = p_header_size % l_chunk_size; + + if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n"); + return OPJ_FALSE; + } + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0; + l_current_poc_nb += l_old_poc_nb; + + assert(l_current_poc_nb < 32); + + /* now poc is in use.*/ + l_tcp->POC = 1; + + l_current_poc = &l_tcp->pocs[l_old_poc_nb]; + for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) { + opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */ + ++p_header_data; + opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */ + p_header_data+=l_comp_room; + opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */ + p_header_data+=2; + opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */ + ++p_header_data; + opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */ + p_header_data+=l_comp_room; + opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */ + ++p_header_data; + l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp; + /* make sure comp is in acceptable bounds */ + l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp); + ++l_current_poc; + } + + l_tcp->numpocs = l_current_poc_nb - 1; + return OPJ_TRUE; } /** * Reads a CRG marker (Component registration) * - * @param p_header_data the data contained in the TLM box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the TLM marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -3267,39 +3278,39 @@ static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_nb_comp; - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - l_nb_comp = p_j2k->m_private_image->numcomps; - - if (p_header_size != l_nb_comp *4) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n"); - return OPJ_FALSE; - } - /* Do not care of this at the moment since only local variables are set here */ - /* - for - (i = 0; i < l_nb_comp; ++i) - { - opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i - p_header_data+=2; - opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i - p_header_data+=2; - } - */ - return OPJ_TRUE; + OPJ_UINT32 l_nb_comp; + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_nb_comp = p_j2k->m_private_image->numcomps; + + if (p_header_size != l_nb_comp *4) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n"); + return OPJ_FALSE; + } + /* Do not care of this at the moment since only local variables are set here */ + /* + for + (i = 0; i < l_nb_comp; ++i) + { + opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i + p_header_data+=2; + opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i + p_header_data+=2; + } + */ + return OPJ_TRUE; } /** * Reads a TLM marker (Tile Length Marker) * - * @param p_header_data the data contained in the TLM box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the TLM marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -3307,57 +3318,57 @@ static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size; - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); + OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size; + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); - if (p_header_size < 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n"); - return OPJ_FALSE; - } - p_header_size -= 2; + if (p_header_size < 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n"); + return OPJ_FALSE; + } + p_header_size -= 2; - opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */ - ++p_header_data; - opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */ - ++p_header_data; + opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */ + ++p_header_data; + opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */ + ++p_header_data; - l_ST = ((l_Stlm >> 4) & 0x3); - l_SP = (l_Stlm >> 6) & 0x1; + l_ST = ((l_Stlm >> 4) & 0x3); + l_SP = (l_Stlm >> 6) & 0x1; - l_Ptlm_size = (l_SP + 1) * 2; - l_quotient = l_Ptlm_size + l_ST; + l_Ptlm_size = (l_SP + 1) * 2; + l_quotient = l_Ptlm_size + l_ST; - l_tot_num_tp = p_header_size / l_quotient; - l_tot_num_tp_remaining = p_header_size % l_quotient; + l_tot_num_tp = p_header_size / l_quotient; + l_tot_num_tp_remaining = p_header_size % l_quotient; - if (l_tot_num_tp_remaining != 0) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n"); - return OPJ_FALSE; - } - /* FIXME Do not care of this at the moment since only local variables are set here */ - /* - for - (i = 0; i < l_tot_num_tp; ++i) - { - opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i - p_header_data += l_ST; - opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i - p_header_data += l_Ptlm_size; - }*/ - return OPJ_TRUE; + if (l_tot_num_tp_remaining != 0) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n"); + return OPJ_FALSE; + } + /* FIXME Do not care of this at the moment since only local variables are set here */ + /* + for + (i = 0; i < l_tot_num_tp; ++i) + { + opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i + p_header_data += l_ST; + opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i + p_header_data += l_Ptlm_size; + }*/ + return OPJ_TRUE; } /** * Reads a PLM marker (Packet length, main header marker) * - * @param p_header_data the data contained in the TLM box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the TLM marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -3365,69 +3376,69 @@ static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - if (p_header_size < 1) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n"); - return OPJ_FALSE; - } - /* Do not care of this at the moment since only local variables are set here */ - /* - opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm - ++p_header_data; - --p_header_size; - - while - (p_header_size > 0) - { - opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm - ++p_header_data; - p_header_size -= (1+l_Nplm); - if - (p_header_size < 0) - { - opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); - return false; - } - for - (i = 0; i < l_Nplm; ++i) - { - opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij - ++p_header_data; - // take only the last seven bytes - l_packet_len |= (l_tmp & 0x7f); - if - (l_tmp & 0x80) - { - l_packet_len <<= 7; - } - else - { + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if (p_header_size < 1) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return OPJ_FALSE; + } + /* Do not care of this at the moment since only local variables are set here */ + /* + opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm + ++p_header_data; + --p_header_size; + + while + (p_header_size > 0) + { + opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm + ++p_header_data; + p_header_size -= (1+l_Nplm); + if + (p_header_size < 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return false; + } + for + (i = 0; i < l_Nplm; ++i) + { + opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij + ++p_header_data; + // take only the last seven bytes + l_packet_len |= (l_tmp & 0x7f); + if + (l_tmp & 0x80) + { + l_packet_len <<= 7; + } + else + { // store packet length and proceed to next packet - l_packet_len = 0; - } - } - if - (l_packet_len != 0) - { - opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); - return false; - } - } - */ - return OPJ_TRUE; + l_packet_len = 0; + } + } + if + (l_packet_len != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return false; + } + } + */ + return OPJ_TRUE; } /** * Reads a PLT marker (Packet length, tile-part header) * - * @param p_header_data the data contained in the PLT box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the PLT marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the PLT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PLT marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -3435,149 +3446,153 @@ static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i; - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - if (p_header_size < 1) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n"); - return OPJ_FALSE; - } - - opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */ - ++p_header_data; - --p_header_size; - - for (i = 0; i < p_header_size; ++i) { - opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */ - ++p_header_data; - /* take only the last seven bytes */ - l_packet_len |= (l_tmp & 0x7f); - if (l_tmp & 0x80) { - l_packet_len <<= 7; - } - else { + OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if (p_header_size < 1) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */ + ++p_header_data; + --p_header_size; + + for (i = 0; i < p_header_size; ++i) { + opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */ + ++p_header_data; + /* take only the last seven bytes */ + l_packet_len |= (l_tmp & 0x7f); + if (l_tmp & 0x80) { + l_packet_len <<= 7; + } + else { /* store packet length and proceed to next packet */ - l_packet_len = 0; - } - } + l_packet_len = 0; + } + } - if (l_packet_len != 0) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n"); - return OPJ_FALSE; - } + if (l_packet_len != 0) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n"); + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a PPM marker (Packed packet headers, main header) * - * @param p_header_data the data contained in the POC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the POC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. */ #if 0 opj_bool j2k_read_ppm_v2 ( - opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - struct opj_event_mgr * p_manager - ) -{ - - opj_cp_v2_t *l_cp = 00; - OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm; - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - if (p_header_size < 1) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n"); - return OPJ_FALSE; - } - - l_cp = &(p_j2k->m_cp); - l_cp->ppm = 1; - - opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */ - ++p_header_data; - --p_header_size; - - /* First PPM marker */ - if (l_Z_ppm == 0) { - if (p_header_size < 4) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n"); - return OPJ_FALSE; - } - - opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ - p_header_data+=4; - p_header_size-=4; - - /* First PPM marker: Initialization */ - l_cp->ppm_len = l_N_ppm; - l_cp->ppm_data_size = 0; - - l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len); - if (l_cp->ppm_buffer == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); - return OPJ_FALSE; - } - memset(l_cp->ppm_buffer,0,l_cp->ppm_len); - - l_cp->ppm_data = l_cp->ppm_buffer; - } - - while (1) { - if (l_cp->ppm_data_size == l_cp->ppm_len) { - if (p_header_size >= 4) { - /* read a N_ppm */ - opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ - p_header_data+=4; - p_header_size-=4; - l_cp->ppm_len += l_N_ppm ; - - l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len); - if (l_cp->ppm_buffer == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); - return OPJ_FALSE; - } - memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm); - - l_cp->ppm_data = l_cp->ppm_buffer; - } - else { - return OPJ_FALSE; - } - } - - l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size; - - if (l_remaining_data <= p_header_size) { - /* we must store less information than available in the packet */ - memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data); - l_cp->ppm_data_size = l_cp->ppm_len; - p_header_size -= l_remaining_data; - p_header_data += l_remaining_data; - } - else { - memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size); - l_cp->ppm_data_size += p_header_size; - p_header_data += p_header_size; - p_header_size = 0; - break; - } - } - - return OPJ_TRUE; + opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + + opj_cp_v2_t *l_cp = 00; + OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if (p_header_size < 1) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n"); + return OPJ_FALSE; + } + + l_cp = &(p_j2k->m_cp); + l_cp->ppm = 1; + + opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */ + ++p_header_data; + --p_header_size; + + /* First PPM marker */ + if (l_Z_ppm == 0) { + if (p_header_size < 4) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ + p_header_data+=4; + p_header_size-=4; + + /* First PPM marker: Initialization */ + l_cp->ppm_len = l_N_ppm; + l_cp->ppm_data_size = 0; + + l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len); + if (l_cp->ppm_buffer == 00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); + return OPJ_FALSE; + } + memset(l_cp->ppm_buffer,0,l_cp->ppm_len); + + l_cp->ppm_data = l_cp->ppm_buffer; + } + + while (1) { + if (l_cp->ppm_data_size == l_cp->ppm_len) { + if (p_header_size >= 4) { + /* read a N_ppm */ + opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ + p_header_data+=4; + p_header_size-=4; + l_cp->ppm_len += l_N_ppm ; + + OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len); + if (! new_ppm_buffer) { + opj_free(l_cp->ppm_buffer); + l_cp->ppm_buffer = NULL; + l_cp->ppm_len = 0; + l_cp->ppm_data = NULL; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); + return OPJ_FALSE; + } + l_cp->ppm_buffer = new_ppm_buffer; + memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm); + l_cp->ppm_data = l_cp->ppm_buffer; + } + else { + return OPJ_FALSE; + } + } + + l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size; + + if (l_remaining_data <= p_header_size) { + /* we must store less information than available in the packet */ + memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data); + l_cp->ppm_data_size = l_cp->ppm_len; + p_header_size -= l_remaining_data; + p_header_data += l_remaining_data; + } + else { + memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size); + l_cp->ppm_data_size += p_header_size; + p_header_data += p_header_size; + p_header_size = 0; + break; + } + } + + return OPJ_TRUE; } #endif @@ -3586,196 +3601,224 @@ opj_bool j2k_read_ppm_v2 ( /** * Reads a PPM marker (Packed packet headers, main header) * - * @param p_header_data the data contained in the POC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the POC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. */ opj_bool j2k_read_ppm_v3 ( - opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - struct opj_event_mgr * p_manager - ) -{ - opj_cp_v2_t *l_cp = 00; - OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm; - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - /* Minimum size of PPM marker is equal to the size of Zppm element */ - if (p_header_size < 1) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n"); - return OPJ_FALSE; - } - - l_cp = &(p_j2k->m_cp); - l_cp->ppm = 1; - - opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */ - ++p_header_data; - --p_header_size; - - /* First PPM marker */ - if (l_Z_ppm == 0) { - /* We need now at least the Nppm^0 element */ - if (p_header_size < 4) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n"); - return OPJ_FALSE; - } - - opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */ - p_header_data+=4; - p_header_size-=4; - - /* First PPM marker: Initialization */ - l_cp->ppm_len = l_N_ppm; - l_cp->ppm_data_read = 0; - - l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len); - if (l_cp->ppm_data == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); - return OPJ_FALSE; - } - memset(l_cp->ppm_data,0,l_cp->ppm_len); - - l_cp->ppm_data_current = l_cp->ppm_data; - - /*l_cp->ppm_data = l_cp->ppm_buffer;*/ - } - else { - if (p_header_size < 4) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n"); - return OPJ_TRUE; - } - else { - /* Uncompleted Ippm series in the previous PPM marker?*/ - if (l_cp->ppm_data_read < l_cp->ppm_len) { - /* Get the place where add the remaining Ippm series*/ - l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]); - l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read; - } - else { - opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */ - p_header_data+=4; - p_header_size-=4; - - /* Increase the size of ppm_data to add the new Ippm series*/ - l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm); - - /* Keep the position of the place where concatenate the new series*/ - l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]); - l_cp->ppm_len += l_N_ppm; - } - } - } - - l_remaining_data = p_header_size; - - while (l_remaining_data >= l_N_ppm) { - /* read a complete Ippm series*/ - memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm); - p_header_size -= l_N_ppm; - p_header_data += l_N_ppm; - - l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/ - - if (p_header_size) - { - opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */ - p_header_data+=4; - p_header_size-=4; - } - else { - l_remaining_data = p_header_size; - break; - } - - l_remaining_data = p_header_size; - - /* Next Ippm series is a complete series ?*/ - if (l_remaining_data > l_N_ppm) { - /* Increase the size of ppm_data to add the new Ippm series*/ - l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm); - - /* Keep the position of the place where concatenate the new series */ - l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]); - l_cp->ppm_len += l_N_ppm; - } - - } - - /* Need to read an incomplete Ippm series*/ - if (l_remaining_data) { - l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm); - - /* Keep the position of the place where concatenate the new series*/ - l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]); - l_cp->ppm_len += l_N_ppm; - - /* Read incomplete Ippm series*/ - memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data); - p_header_size -= l_remaining_data; - p_header_data += l_remaining_data; - - l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/ - } + opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + opj_cp_v2_t *l_cp = 00; + OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + /* Minimum size of PPM marker is equal to the size of Zppm element */ + if (p_header_size < 1) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n"); + return OPJ_FALSE; + } + + l_cp = &(p_j2k->m_cp); + l_cp->ppm = 1; + + opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */ + ++p_header_data; + --p_header_size; + + /* First PPM marker */ + if (l_Z_ppm == 0) { + /* We need now at least the Nppm^0 element */ + if (p_header_size < 4) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */ + p_header_data+=4; + p_header_size-=4; + + /* First PPM marker: Initialization */ + l_cp->ppm_len = l_N_ppm; + l_cp->ppm_data_read = 0; + + l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len); + if (l_cp->ppm_data == 00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); + return OPJ_FALSE; + } + memset(l_cp->ppm_data,0,l_cp->ppm_len); + + l_cp->ppm_data_current = l_cp->ppm_data; + + /*l_cp->ppm_data = l_cp->ppm_buffer;*/ + } + else { + if (p_header_size < 4) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n"); + return OPJ_TRUE; + } + else { + /* Uncompleted Ippm series in the previous PPM marker?*/ + if (l_cp->ppm_data_read < l_cp->ppm_len) { + /* Get the place where add the remaining Ippm series*/ + l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]); + l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read; + } + else { + opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */ + p_header_data+=4; + p_header_size-=4; + + /* Increase the size of ppm_data to add the new Ippm series*/ + OPJ_BYTE *new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm); + if (! new_ppm_data) { + opj_free(l_cp->ppm_data); + l_cp->ppm_data = NULL; + l_cp->ppm_len = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new Ippm series\n"); + return OPJ_FALSE; + } + l_cp->ppm_data = new_ppm_data; + + /* Keep the position of the place where concatenate the new series*/ + l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]); + l_cp->ppm_len += l_N_ppm; + } + } + } + + l_remaining_data = p_header_size; + + while (l_remaining_data >= l_N_ppm) { + /* read a complete Ippm series*/ + memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm); + p_header_size -= l_N_ppm; + p_header_data += l_N_ppm; + + l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/ + + if (p_header_size) + { + opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */ + p_header_data+=4; + p_header_size-=4; + } + else { + l_remaining_data = p_header_size; + break; + } + + l_remaining_data = p_header_size; + + /* Next Ippm series is a complete series ?*/ + if (l_remaining_data > l_N_ppm) { + /* Increase the size of ppm_data to add the new Ippm series*/ + OPJ_BYTE *new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm); + if (! new_ppm_data) { + opj_free(l_cp->ppm_data); + l_cp->ppm_data = NULL; + l_cp->ppm_len = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (complete) Ippm series\n"); + return OPJ_FALSE; + } + l_cp->ppm_data = new_ppm_data; + + /* Keep the position of the place where concatenate the new series */ + l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]); + l_cp->ppm_len += l_N_ppm; + } + + } + + /* Need to read an incomplete Ippm series*/ + if (l_remaining_data) { + OPJ_BYTE *new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm); + if (! new_ppm_data) { + opj_free(l_cp->ppm_data); + l_cp->ppm_data = NULL; + l_cp->ppm_len = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (incomplete) Ippm series\n"); + return OPJ_FALSE; + } + l_cp->ppm_data = new_ppm_data; + + /* Keep the position of the place where concatenate the new series*/ + l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]); + l_cp->ppm_len += l_N_ppm; + + /* Read incomplete Ippm series*/ + memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data); + p_header_size -= l_remaining_data; + p_header_data += l_remaining_data; + + l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/ + } #ifdef CLEAN_MSD - if (l_cp->ppm_data_size == l_cp->ppm_len) { - if (p_header_size >= 4) { - /* read a N_ppm*/ - opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ - p_header_data+=4; - p_header_size-=4; - l_cp->ppm_len += l_N_ppm ; - - l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len); - if (l_cp->ppm_buffer == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); - return OPJ_FALSE; - } - memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm); - - l_cp->ppm_data = l_cp->ppm_buffer; - } - else { - return OPJ_FALSE; - } - } - - l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size; - - if (l_remaining_data <= p_header_size) { - /* we must store less information than available in the packet */ - memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data); - l_cp->ppm_data_size = l_cp->ppm_len; - p_header_size -= l_remaining_data; - p_header_data += l_remaining_data; - } - else { - memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size); - l_cp->ppm_data_size += p_header_size; - p_header_data += p_header_size; - p_header_size = 0; - break; - } - } + if (l_cp->ppm_data_size == l_cp->ppm_len) { + if (p_header_size >= 4) { + /* read a N_ppm*/ + opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ + p_header_data+=4; + p_header_size-=4; + l_cp->ppm_len += l_N_ppm ; + + OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len); + if (! new_ppm_buffer) { + opj_free(l_cp->ppm_buffer); + l_cp->ppm_buffer = NULL; + l_cp->ppm_len = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n"); + return OPJ_FALSE; + } + l_cp->ppm_buffer = new_ppm_buffer; + memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm); + + l_cp->ppm_data = l_cp->ppm_buffer; + } + else { + return OPJ_FALSE; + } + } + + l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size; + + if (l_remaining_data <= p_header_size) { + /* we must store less information than available in the packet */ + memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data); + l_cp->ppm_data_size = l_cp->ppm_len; + p_header_size -= l_remaining_data; + p_header_data += l_remaining_data; + } + else { + memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size); + l_cp->ppm_data_size += p_header_size; + p_header_data += p_header_size; + p_header_size = 0; + break; + } + } #endif - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a PPT marker (Packed packet headers, tile-part header) * - * @param p_header_data the data contained in the PPT box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the PPT marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the PPT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PPT marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -3783,180 +3826,184 @@ static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - OPJ_UINT32 l_Z_ppt; - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + OPJ_UINT32 l_Z_ppt; - /* We need to have the Z_ppt element at minimum */ - if (p_header_size < 1) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n"); - return OPJ_FALSE; - } + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); - l_cp = &(p_j2k->m_cp); - if (l_cp->ppm){ - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n"); - return OPJ_FALSE; - } - - l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]); - l_tcp->ppt = 1; + /* We need to have the Z_ppt element at minimum */ + if (p_header_size < 1) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n"); + return OPJ_FALSE; + } - opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */ - ++p_header_data; - --p_header_size; + l_cp = &(p_j2k->m_cp); + if (l_cp->ppm){ + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n"); + return OPJ_FALSE; + } - /* Allocate buffer to read the packet header */ - if (l_Z_ppt == 0) { - /* First PPT marker */ - l_tcp->ppt_data_size = 0; - l_tcp->ppt_len = p_header_size; + l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]); + l_tcp->ppt = 1; - l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) ); - if (l_tcp->ppt_buffer == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n"); - return OPJ_FALSE; - } - l_tcp->ppt_data = l_tcp->ppt_buffer; + opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */ + ++p_header_data; + --p_header_size; - /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */ - } - else { - l_tcp->ppt_len += p_header_size; + /* Allocate buffer to read the packet header */ + if (l_Z_ppt == 0) { + /* First PPT marker */ + l_tcp->ppt_data_size = 0; + l_tcp->ppt_len = p_header_size; - l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len); - if (l_tcp->ppt_buffer == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n"); - return OPJ_FALSE; - } - l_tcp->ppt_data = l_tcp->ppt_buffer; + l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) ); + if (l_tcp->ppt_buffer == 00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + return OPJ_FALSE; + } + l_tcp->ppt_data = l_tcp->ppt_buffer; - memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size); - } + /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */ + } + else { + l_tcp->ppt_len += p_header_size; + + OPJ_BYTE *new_ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer, l_tcp->ppt_len); + if (! new_ppt_buffer) { + opj_free(l_tcp->ppt_buffer); + l_tcp->ppt_buffer = NULL; + l_tcp->ppt_len = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + return OPJ_FALSE; + } + l_tcp->ppt_buffer = new_ppt_buffer; + l_tcp->ppt_data = l_tcp->ppt_buffer; + + memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size); + } - /* Read packet header from buffer */ - memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size); + /* Read packet header from buffer */ + memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size); - l_tcp->ppt_data_size += p_header_size; + l_tcp->ppt_data_size += p_header_size; - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the TLM marker (Tile Length Marker) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager +opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - OPJ_BYTE * l_current_data = 00; - OPJ_UINT32 l_tlm_size; - - // preconditions - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts); - - if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_tlm_size); - - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size; - } - - l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - - /* change the way data is written to avoid seeking if possible */ - // TODO - p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream); - - opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */ - l_current_data += 2; - - opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */ - l_current_data += 2; - - opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/ - ++l_current_data; - - opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */ - ++l_current_data; - - /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */ - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) { - return OPJ_FALSE; - } - - return OPJ_TRUE; -} + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_tlm_size; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts); + + if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size; + } -/** + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + /* change the way data is written to avoid seeking if possible */ + // TODO + p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream); + + opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */ + l_current_data += 2; + + opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */ + l_current_data += 2; + + opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/ + ++l_current_data; + + opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */ + ++l_current_data; + + /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */ + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) { + return OPJ_FALSE; + } + + return OPJ_TRUE; +} + +/** * Writes the SOT marker (Start of tile-part) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - const opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager +opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + const opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); - opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */ - p_data += 2; + opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */ + p_data += 2; - opj_write_bytes(p_data,10,2); /* Lsot */ - p_data += 2; + opj_write_bytes(p_data,10,2); /* Lsot */ + p_data += 2; - opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */ - p_data += 2; + opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */ + p_data += 2; - /* Psot */ - p_data += 4; + /* Psot */ + p_data += 4; - opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */ - ++p_data; + opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */ + ++p_data; - opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */ - ++p_data; + opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */ + ++p_data; - /* UniPG>> */ + /* UniPG>> */ #ifdef USE_JPWL - /* update markers struct */ + /* update markers struct */ /* - j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2); + j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2); */ assert( 0 && "TODO" ); #endif /* USE_JPWL */ - * p_data_written = 12; + * p_data_written = 12; - return OPJ_TRUE; + return OPJ_TRUE; } @@ -3965,327 +4012,341 @@ opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k, /** * Reads a PPT marker (Packed packet headers, tile-part header) * - * @param p_header_data the data contained in the PPT box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the PPT marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the PPT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PPT marker. + * @param p_manager the user event manager. */ opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, OPJ_UINT32 p_header_size, opj_event_mgr_t * p_manager ) { - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - OPJ_UINT32 l_tot_len, l_num_parts = 0; - OPJ_UINT32 l_current_part; - OPJ_UINT32 l_tile_x,l_tile_y; - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - /* Size of this marker is fixed = 12 (we have already read marker and its size)*/ - if (p_header_size != 8) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n"); - return OPJ_FALSE; - } + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + OPJ_UINT32 l_tot_len, l_num_parts = 0; + OPJ_UINT32 l_current_part; + OPJ_UINT32 l_tile_x,l_tile_y; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + /* Size of this marker is fixed = 12 (we have already read marker and its size)*/ + if (p_header_size != 8) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n"); + return OPJ_FALSE; + } - l_cp = &(p_j2k->m_cp); - opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */ - p_header_data+=2; + l_cp = &(p_j2k->m_cp); + opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */ + p_header_data+=2; - l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; - l_tile_x = p_j2k->m_current_tile_number % l_cp->tw; - l_tile_y = p_j2k->m_current_tile_number / l_cp->tw; + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_tile_x = p_j2k->m_current_tile_number % l_cp->tw; + l_tile_y = p_j2k->m_current_tile_number / l_cp->tw; #ifdef USE_JPWL - if (l_cp->correct) { - - OPJ_UINT32 tileno = p_j2k->m_current_tile_number; - static OPJ_UINT32 backup_tileno = 0; - - /* tileno is negative or larger than the number of tiles!!! */ - if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) { - opj_event_msg_v2(p_manager, EVT_ERROR, - "JPWL: bad tile number (%d out of a maximum of %d)\n", - tileno, (l_cp->tw * l_cp->th)); - if (!JPWL_ASSUME) { - opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - /* we try to correct */ - tileno = backup_tileno; - opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n" - "- setting tile number to %d\n", - tileno); - } - - /* keep your private count of tiles */ - backup_tileno++; - }; + if (l_cp->correct) { + + OPJ_UINT32 tileno = p_j2k->m_current_tile_number; + static OPJ_UINT32 backup_tileno = 0; + + /* tileno is negative or larger than the number of tiles!!! */ + if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) { + opj_event_msg_v2(p_manager, EVT_ERROR, + "JPWL: bad tile number (%d out of a maximum of %d)\n", + tileno, (l_cp->tw * l_cp->th)); + if (!JPWL_ASSUME) { + opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + tileno = backup_tileno; + opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n" + "- setting tile number to %d\n", + tileno); + } + + /* keep your private count of tiles */ + backup_tileno++; + }; #endif /* USE_JPWL */ - /* look for the tile in the list of already processed tile (in parts). */ - /* Optimization possible here with a more complex data structure and with the removing of tiles */ - /* since the time taken by this function can only grow at the time */ + /* look for the tile in the list of already processed tile (in parts). */ + /* Optimization possible here with a more complex data structure and with the removing of tiles */ + /* since the time taken by this function can only grow at the time */ - opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */ - p_header_data+=4; + opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */ + p_header_data+=4; - /* PSot should be equal to zero or >=14 or <= 2^32-1 */ - if ((l_tot_len !=0 ) && (l_tot_len < 14) ) - { - opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len); - return OPJ_FALSE; - } + /* PSot should be equal to zero or >=14 or <= 2^32-1 */ + if ((l_tot_len !=0 ) && (l_tot_len < 14) ) + { + opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len); + return OPJ_FALSE; + } #ifdef USE_JPWL - if (l_cp->correct) { - - /* totlen is negative or larger than the bytes left!!! */ - if (/*(l_tot_len < 0) ||*/ (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */ - opj_event_msg_v2(p_manager, EVT_ERROR, - "JPWL: bad tile byte size (%d bytes against %d bytes left)\n", - l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */ - if (!JPWL_ASSUME) { - opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - /* we try to correct */ - l_tot_len = 0; - opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n" - "- setting Psot to %d => assuming it is the last tile\n", - l_tot_len); - } - }; + if (l_cp->correct) { + + /* totlen is negative or larger than the bytes left!!! */ + if (/*(l_tot_len < 0) ||*/ (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */ + opj_event_msg_v2(p_manager, EVT_ERROR, + "JPWL: bad tile byte size (%d bytes against %d bytes left)\n", + l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */ + if (!JPWL_ASSUME) { + opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + l_tot_len = 0; + opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n" + "- setting Psot to %d => assuming it is the last tile\n", + l_tot_len); + } + }; #endif /* USE_JPWL */ - /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/ - if (!l_tot_len) { - opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, " - "we assuming it is the last tile-part of the codestream.\n"); - p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; - } - - opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */ - ++p_header_data; - - opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */ - ++p_header_data; - - if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */ - /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of - * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */ - if (l_tcp->m_nb_tile_parts) { - if (l_current_part >= l_tcp->m_nb_tile_parts){ - opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current " - "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts ); - p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; - return OPJ_FALSE; - } - } - l_tcp->m_nb_tile_parts = l_num_parts; - } - - /* If know the number of tile part header we will check if we didn't read the last*/ - if (l_tcp->m_nb_tile_parts) { - if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) { - p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/ - } - } - - if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){ - /* Keep the size of data to skip after this marker */ - p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */ - } - else { - /* FIXME: need to be computed from the number of bytes remaining in the codestream */ - p_j2k->m_specific_param.m_decoder.m_sot_length = 0; - } - - p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH; - - /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/ - if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) { - p_j2k->m_specific_param.m_decoder.m_skip_data = - (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x) - || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x) - || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y) - || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y); - } - else { - assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 ); - p_j2k->m_specific_param.m_decoder.m_skip_data = - (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec); - } - - /* Index */ - if (p_j2k->cstr_index) - { - assert(p_j2k->cstr_index->tile_index != 00); - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part; - - if (l_num_parts != 0){ - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts; - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts; - - - if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = - (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t)); - else - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = - (opj_tp_index_t*)opj_realloc( - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, - l_num_parts* sizeof(opj_tp_index_t)); - } - else{ - /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ { - - if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10; - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = - (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps, - sizeof(opj_tp_index_t)); - } - - if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){ - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10; - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = - (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps - * sizeof(opj_tp_index_t)); - } - } - - } - - } - - - /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */ - /* if (p_j2k->cstr_info) { - if (l_tcp->first) { - if (tileno == 0) { - p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13; - } - - p_j2k->cstr_info->tile[tileno].tileno = tileno; - p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12; - p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1; - p_j2k->cstr_info->tile[tileno].num_tps = numparts; - - if (numparts) { - p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t)); - } - else { - p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10) - } - } - else { - p_j2k->cstr_info->tile[tileno].end_pos += totlen; - } - - p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12; - p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = - p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1; - }*/ - return OPJ_TRUE; -} + /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/ + if (!l_tot_len) { + opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, " + "we assuming it is the last tile-part of the codestream.\n"); + p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; + } + + opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */ + ++p_header_data; + + opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */ + ++p_header_data; + + if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */ + /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of + * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */ + if (l_tcp->m_nb_tile_parts) { + if (l_current_part >= l_tcp->m_nb_tile_parts){ + opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current " + "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts ); + p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1; + return OPJ_FALSE; + } + } + l_tcp->m_nb_tile_parts = l_num_parts; + } + + /* If know the number of tile part header we will check if we didn't read the last*/ + if (l_tcp->m_nb_tile_parts) { + if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) { + p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/ + } + } + + if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){ + /* Keep the size of data to skip after this marker */ + p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */ + } + else { + /* FIXME: need to be computed from the number of bytes remaining in the codestream */ + p_j2k->m_specific_param.m_decoder.m_sot_length = 0; + } + + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH; + + /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/ + if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) { + p_j2k->m_specific_param.m_decoder.m_skip_data = + (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x) + || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x) + || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y) + || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y); + } + else { + assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 ); + p_j2k->m_specific_param.m_decoder.m_skip_data = + (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec); + } + + /* Index */ + if (p_j2k->cstr_index) + { + assert(p_j2k->cstr_index->tile_index != 00); + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part; + + if (l_num_parts != 0){ + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts; + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts; + + + if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = + (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t)); + } + else { + opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc( + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, l_num_parts* sizeof(opj_tp_index_t)); + if (! new_tp_index) { + opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + return OPJ_FALSE; + } + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index; + } + } + else{ + /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ { + + if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10; + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = + (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps, + sizeof(opj_tp_index_t)); + } + + if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){ + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10; + opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc( + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * sizeof(opj_tp_index_t)); + if (! new_tp_index) { + opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index); + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL; + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + return OPJ_FALSE; + } + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index; + } + } + + } + + } + + + /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */ + /* if (p_j2k->cstr_info) { + if (l_tcp->first) { + if (tileno == 0) { + p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13; + } + + p_j2k->cstr_info->tile[tileno].tileno = tileno; + p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12; + p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1; + p_j2k->cstr_info->tile[tileno].num_tps = numparts; + + if (numparts) { + p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t)); + } + else { + p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10) + } + } + else { + p_j2k->cstr_info->tile[tileno].end_pos += totlen; + } + + p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12; + p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = + p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1; + }*/ + return OPJ_TRUE; + } /** * Writes the SOD marker (Start of data) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k, - opj_tcd_v2_t * p_tile_coder, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 p_total_data_size, - const opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager +opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k, + opj_tcd_v2_t * p_tile_coder, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + const opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - opj_tcp_v2_t *l_tcp = 00; - opj_codestream_info_t *l_cstr_info = 00; - opj_cp_v2_t *l_cp = 00; - - OPJ_UINT32 l_size_tile; - OPJ_UINT32 l_remaining_data; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */ - p_data += 2; - - /* make room for the EOF marker */ - l_remaining_data = p_total_data_size - 4; - - l_cp = &(p_j2k->m_cp); - l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; - - - /* update tile coder */ - p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ; - p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; - - l_size_tile = l_cp->th * l_cp->tw; - - /* INDEX >> */ - /* TODO mergeV2: check this part which use cstr_info */ - /*l_cstr_info = p_j2k->cstr_info; - if (l_cstr_info) { - if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) { - //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1; - l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; - } - else {*/ - /* - TODO - if - (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream)) - { - cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream); - }*/ - /*}*/ - /* UniPG>> */ + opj_tcp_v2_t *l_tcp = 00; + opj_codestream_info_t *l_cstr_info = 00; + opj_cp_v2_t *l_cp = 00; + + OPJ_UINT32 l_size_tile; + OPJ_UINT32 l_remaining_data; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */ + p_data += 2; + + /* make room for the EOF marker */ + l_remaining_data = p_total_data_size - 4; + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + + + /* update tile coder */ + p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ; + p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; + + l_size_tile = l_cp->th * l_cp->tw; + + /* INDEX >> */ + /* TODO mergeV2: check this part which use cstr_info */ + /*l_cstr_info = p_j2k->cstr_info; + if (l_cstr_info) { + if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) { + //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1; + l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; + } + else {*/ + /* + TODO + if + (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream)) + { + cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream); + }*/ + /*}*/ + /* UniPG>> */ #ifdef USE_JPWL - /* update markers struct */ - /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2); + /* update markers struct */ + /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2); */ assert( 0 && "TODO" ); #endif /* USE_JPWL */ - /* <m_specific_param.m_encoder.m_current_tile_part_number == 0) { - p_tile_coder->tcd_image->tiles->packno = 0; - if (l_cstr_info) { - l_cstr_info->packno = 0; - } - } + /* <m_specific_param.m_encoder.m_current_tile_part_number == 0) { + p_tile_coder->tcd_image->tiles->packno = 0; + if (l_cstr_info) { + l_cstr_info->packno = 0; + } + } - *p_data_written = 0; + *p_data_written = 0; - if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n"); - return OPJ_FALSE; - } + if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n"); + return OPJ_FALSE; + } - *p_data_written += 2; + *p_data_written += 2; - return OPJ_TRUE; + return OPJ_TRUE; } @@ -4294,208 +4355,223 @@ opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k, /** * Reads a SOD marker (Start Of Data) * - * @param p_header_data the data contained in the SOD box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the SOD marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the SOD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the SOD marker. + * @param p_manager the user event manager. */ opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager + opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_current_read_size; - opj_codestream_index_t * l_cstr_index = 00; - OPJ_BYTE ** l_current_data = 00; - opj_tcp_v2_t * l_tcp = 00; - OPJ_UINT32 * l_tile_len = 00; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); - - if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) { - /* opj_stream_get_number_byte_left returns OPJ_OFF_T - // but we are in the last tile part, - // so its result will fit on OPJ_UINT32 unless we find - // a file with a single tile part of more than 4 GB...*/ - p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2); - } - else - p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; + OPJ_UINT32 l_current_read_size; + opj_codestream_index_t * l_cstr_index = 00; + OPJ_BYTE ** l_current_data = 00; + opj_tcp_v2_t * l_tcp = 00; + OPJ_UINT32 * l_tile_len = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); + + if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) { + /* opj_stream_get_number_byte_left returns OPJ_OFF_T + // but we are in the last tile part, + // so its result will fit on OPJ_UINT32 unless we find + // a file with a single tile part of more than 4 GB...*/ + p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2); + } + else + p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; - l_current_data = &(l_tcp->m_data); - l_tile_len = &l_tcp->m_data_size; + l_current_data = &(l_tcp->m_data); + l_tile_len = &l_tcp->m_data_size; - if (! *l_current_data) { - *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length); - } - else { - *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length); - } + if (! *l_current_data) { + /* LH: oddly enough, in this path, l_tile_len!=0. + * TODO: If this was consistant, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...). + */ + *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length); + } + else { + OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length); + if (! l_new_current_data) { + opj_free(*l_current_data); + // nothing more is done as l_current_data will be set to null, and just + // afterward we enter in the error path + // and the actual tile_len is updated (committed) at the end of the + // function. + } + *l_current_data = l_new_current_data; + } - if (*l_current_data == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n"); - return OPJ_FALSE; - } + if (*l_current_data == 00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to decode tile\n"); + return OPJ_FALSE; + } - /* Index */ - l_cstr_index = p_j2k->cstr_index; - if (l_cstr_index) { - OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2; + /* Index */ + l_cstr_index = p_j2k->cstr_index; + if (l_cstr_index) { + OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2; - OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno; - l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header = - l_current_pos; - l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos = - l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2; + OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno; + l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header = + l_current_pos; + l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos = + l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2; - opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, - l_cstr_index, - J2K_MS_SOD, - l_current_pos, - p_j2k->m_specific_param.m_decoder.m_sot_length + 2); + if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, + l_cstr_index, + J2K_MS_SOD, + l_current_pos, + p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); + return OPJ_FALSE; + } - /*l_cstr_index->packno = 0;*/ - } + /*l_cstr_index->packno = 0;*/ + } - l_current_read_size = opj_stream_read_data( p_stream, - *l_current_data + *l_tile_len, - p_j2k->m_specific_param.m_decoder.m_sot_length, - p_manager); + l_current_read_size = opj_stream_read_data( + p_stream, + *l_current_data + *l_tile_len, + p_j2k->m_specific_param.m_decoder.m_sot_length, + p_manager); - if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) { - p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; - } - else { - p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; - } + if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) { + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; + } + else { + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; + } - *l_tile_len += l_current_read_size; + *l_tile_len += l_current_read_size; - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the RGN marker (Region Of Interest) * - * @param p_tile_no the tile to output - * @param p_comp_no the component to output - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_tile_no the tile to output + * @param p_comp_no the component to output + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ opj_bool opj_j2k_write_rgn(opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_tile_no, - OPJ_UINT32 p_comp_no, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - OPJ_BYTE * l_current_data = 00; - OPJ_UINT32 l_nb_comp; - OPJ_UINT32 l_rgn_size; - opj_image_t *l_image = 00; - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - opj_tccp_t *l_tccp = 00; - OPJ_UINT32 l_comp_room; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_rgn_size; + opj_image_t *l_image = 00; + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + OPJ_UINT32 l_comp_room; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); - l_cp = &(p_j2k->m_cp); - l_tcp = &l_cp->tcps[p_tile_no]; - l_tccp = &l_tcp->tccps[p_comp_no]; + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; - l_nb_comp = l_image->numcomps; + l_nb_comp = l_image->numcomps; - if (l_nb_comp <= 256) { - l_comp_room = 1; - } - else { - l_comp_room = 2; - } + if (l_nb_comp <= 256) { + l_comp_room = 1; + } + else { + l_comp_room = 2; + } - l_rgn_size = 6 + l_comp_room; + l_rgn_size = 6 + l_comp_room; - l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */ - l_current_data += 2; + opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */ - l_current_data += 2; + opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */ + l_current_data += 2; - opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */ - l_current_data+=l_comp_room; + opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */ + l_current_data+=l_comp_room; - opj_write_bytes(l_current_data, 0,1); /* Srgn */ - ++l_current_data; + opj_write_bytes(l_current_data, 0,1); /* Srgn */ + ++l_current_data; - opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */ - ++l_current_data; + opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */ + ++l_current_data; - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the EOC marker (End of Codestream) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_eoc( opj_j2k_v2_t *p_j2k, +opj_bool opj_j2k_write_eoc( opj_j2k_v2_t *p_j2k, opj_stream_private_t *p_stream, opj_event_mgr_t * p_manager ) { - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */ - + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */ + /* UniPG>> */ #ifdef USE_JPWL - /* update markers struct */ - /* - j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2); + /* update markers struct */ + /* + j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2); */ #endif /* USE_JPWL */ - if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) { - return OPJ_FALSE; - } + if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) { + return OPJ_FALSE; + } - if ( ! opj_stream_flush(p_stream,p_manager) ) { - return OPJ_FALSE; - } + if ( ! opj_stream_flush(p_stream,p_manager) ) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a RGN marker (Region Of Interest) * - * @param p_header_data the data contained in the POC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the POC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. */ static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k, OPJ_BYTE * p_header_data, @@ -4503,66 +4579,66 @@ static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_nb_comp; - opj_image_t * l_image = 00; + OPJ_UINT32 l_nb_comp; + opj_image_t * l_image = 00; - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty; + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty; - /* preconditions*/ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); + /* preconditions*/ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); - l_image = p_j2k->m_private_image; - l_nb_comp = l_image->numcomps; + l_image = p_j2k->m_private_image; + l_nb_comp = l_image->numcomps; - if (l_nb_comp <= 256) { - l_comp_room = 1; } - else { - l_comp_room = 2; } + if (l_nb_comp <= 256) { + l_comp_room = 1; } + else { + l_comp_room = 2; } - if (p_header_size != 2 + l_comp_room) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n"); - return OPJ_FALSE; - } + if (p_header_size != 2 + l_comp_room) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n"); + return OPJ_FALSE; + } - l_cp = &(p_j2k->m_cp); - l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? - &l_cp->tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; - opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */ - p_header_data+=l_comp_room; - opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */ - ++p_header_data; + opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */ + p_header_data+=l_comp_room; + opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */ + ++p_header_data; #ifdef USE_JPWL - if (l_cp->correct) { - /* totlen is negative or larger than the bytes left!!! */ - if (l_comp_room >= l_nb_comp) { - opj_event_msg_v2(p_manager, EVT_ERROR, - "JPWL: bad component number in RGN (%d when there are only %d)\n", - l_comp_room, l_nb_comp); - if (!JPWL_ASSUME || JPWL_ASSUME) { - opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - } - }; + if (l_cp->correct) { + /* totlen is negative or larger than the bytes left!!! */ + if (l_comp_room >= l_nb_comp) { + opj_event_msg_v2(p_manager, EVT_ERROR, + "JPWL: bad component number in RGN (%d when there are only %d)\n", + l_comp_room, l_nb_comp); + if (!JPWL_ASSUME || JPWL_ASSUME) { + opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + } + }; #endif /* USE_JPWL */ - opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */ - ++p_header_data; + opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */ + ++p_header_data; - return OPJ_TRUE; + return OPJ_TRUE; } OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_v2_t * p_tcp) { - return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14); + return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14); } OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_v2_t * p_tcp) @@ -4574,417 +4650,417 @@ OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_v2_t * p_tcp) /** * Updates the rates of the tcp. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - opj_cp_v2_t * l_cp = 00; - opj_image_t * l_image = 00; - opj_tcp_v2_t * l_tcp = 00; - opj_image_comp_t * l_img_comp = 00; - - OPJ_UINT32 i,j,k; - OPJ_INT32 l_x0,l_y0,l_x1,l_y1; - OPJ_FLOAT32 * l_rates = 0; - OPJ_FLOAT32 l_sot_remove; - OPJ_UINT32 l_bits_empty, l_size_pixel; - OPJ_UINT32 l_tile_size = 0; - OPJ_UINT32 l_last_res; - OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - - l_cp = &(p_j2k->m_cp); - l_image = p_j2k->m_private_image; - l_tcp = l_cp->tcps; - - l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy; - l_size_pixel = l_image->numcomps * l_image->comps->prec; - l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw); - - if (l_cp->m_specific_param.m_enc.m_tp_on) { - l_tp_stride_func = opj_j2k_get_tp_stride; - } - else { - l_tp_stride_func = opj_j2k_get_default_stride; - } - - for (i=0;ith;++i) { - for (j=0;jtw;++j) { - OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers; - - /* 4 borders of the tile rescale on the image if necessary */ - l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0); - l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0); - l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1); - l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1); - - l_rates = l_tcp->rates; - - /* Modification of the RATE >> */ - if (*l_rates) { - *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0))) - / - ((*l_rates) * l_bits_empty) - ) - - - l_offset; - } - - ++l_rates; - - for (k = 1; k < l_tcp->numlayers; ++k) { - if (*l_rates) { - *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0))) - / - ((*l_rates) * l_bits_empty) - ) - - - l_offset; - } - - ++l_rates; - } - - ++l_tcp; +opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + opj_cp_v2_t * l_cp = 00; + opj_image_t * l_image = 00; + opj_tcp_v2_t * l_tcp = 00; + opj_image_comp_t * l_img_comp = 00; + + OPJ_UINT32 i,j,k; + OPJ_INT32 l_x0,l_y0,l_x1,l_y1; + OPJ_FLOAT32 * l_rates = 0; + OPJ_FLOAT32 l_sot_remove; + OPJ_UINT32 l_bits_empty, l_size_pixel; + OPJ_UINT32 l_tile_size = 0; + OPJ_UINT32 l_last_res; + OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + + l_cp = &(p_j2k->m_cp); + l_image = p_j2k->m_private_image; + l_tcp = l_cp->tcps; + + l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy; + l_size_pixel = l_image->numcomps * l_image->comps->prec; + l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw); + + if (l_cp->m_specific_param.m_enc.m_tp_on) { + l_tp_stride_func = opj_j2k_get_tp_stride; + } + else { + l_tp_stride_func = opj_j2k_get_default_stride; + } - } - } + for (i=0;ith;++i) { + for (j=0;jtw;++j) { + OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers; + + /* 4 borders of the tile rescale on the image if necessary */ + l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0); + l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0); + l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1); + l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1); + + l_rates = l_tcp->rates; + + /* Modification of the RATE >> */ + if (*l_rates) { + *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0))) + / + ((*l_rates) * l_bits_empty) + ) + - + l_offset; + } + + ++l_rates; + + for (k = 1; k < l_tcp->numlayers; ++k) { + if (*l_rates) { + *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0))) + / + ((*l_rates) * l_bits_empty) + ) + - + l_offset; + } + + ++l_rates; + } + + ++l_tcp; + + } + } - l_tcp = l_cp->tcps; + l_tcp = l_cp->tcps; - for (i=0;ith;++i) { - for (j=0;jtw;++j) { - l_rates = l_tcp->rates; + for (i=0;ith;++i) { + for (j=0;jtw;++j) { + l_rates = l_tcp->rates; - if (*l_rates) { - *l_rates -= l_sot_remove; + if (*l_rates) { + *l_rates -= l_sot_remove; - if (*l_rates < 30) { - *l_rates = 30; - } - } + if (*l_rates < 30) { + *l_rates = 30; + } + } - ++l_rates; + ++l_rates; - l_last_res = l_tcp->numlayers - 1; + l_last_res = l_tcp->numlayers - 1; - for (k = 1; k < l_last_res; ++k) { + for (k = 1; k < l_last_res; ++k) { - if (*l_rates) { - *l_rates -= l_sot_remove; + if (*l_rates) { + *l_rates -= l_sot_remove; - if (*l_rates < *(l_rates - 1) + 10) { - *l_rates = (*(l_rates - 1)) + 20; - } - } + if (*l_rates < *(l_rates - 1) + 10) { + *l_rates = (*(l_rates - 1)) + 20; + } + } - ++l_rates; - } + ++l_rates; + } - if (*l_rates) { - *l_rates -= (l_sot_remove + 2.f); + if (*l_rates) { + *l_rates -= (l_sot_remove + 2.f); - if (*l_rates < *(l_rates - 1) + 10) { - *l_rates = (*(l_rates - 1)) + 20; - } - } + if (*l_rates < *(l_rates - 1) + 10) { + *l_rates = (*(l_rates - 1)) + 20; + } + } - ++l_tcp; - } - } + ++l_tcp; + } + } - l_img_comp = l_image->comps; - l_tile_size = 0; + l_img_comp = l_image->comps; + l_tile_size = 0; - for (i=0;inumcomps;++i) { - l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx) - * - uint_ceildiv(l_cp->tdy,l_img_comp->dy) - * - l_img_comp->prec - ); + for (i=0;inumcomps;++i) { + l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx) + * + uint_ceildiv(l_cp->tdy,l_img_comp->dy) + * + l_img_comp->prec + ); - ++l_img_comp; - } + ++l_img_comp; + } - l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */ + l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */ - l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k); + l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k); - p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size; - p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = - (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size); - if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) { - return OPJ_FALSE; - } + p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size; + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = + (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size); + if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) { + return OPJ_FALSE; + } - if (l_cp->m_specific_param.m_enc.m_cinema) { - p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = - (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts); - if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { - return OPJ_FALSE; - } + if (l_cp->m_specific_param.m_enc.m_cinema) { + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = + (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts); + if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { + return OPJ_FALSE; + } - p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = - p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer; - } + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a EOC marker (End Of Codestream) * - * @param p_header_data the data contained in the SOD box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the SOD marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the SOD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the SOD marker. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_read_eoc ( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - OPJ_UINT32 i; - opj_tcd_v2_t * l_tcd = 00; - OPJ_UINT32 l_nb_tiles; - opj_tcp_v2_t * l_tcp = 00; - opj_bool l_success; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; - l_tcp = p_j2k->m_cp.tcps; - - l_tcd = opj_tcd_create(OPJ_TRUE); - if (l_tcd == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); - return OPJ_FALSE; - } - - for (i = 0; i < l_nb_tiles; ++i) { - if (l_tcp->m_data) { - if (! opj_tcd_init_decode_tile(l_tcd, i)) { - opj_tcd_destroy(l_tcd); - opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); - return OPJ_FALSE; - } - - l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index); - /* cleanup */ - - if (! l_success) { - p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; - break; - } - } +opj_bool opj_j2k_read_eoc ( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + OPJ_UINT32 i; + opj_tcd_v2_t * l_tcd = 00; + OPJ_UINT32 l_nb_tiles; + opj_tcp_v2_t * l_tcp = 00; + opj_bool l_success; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + l_tcp = p_j2k->m_cp.tcps; + + l_tcd = opj_tcd_create(OPJ_TRUE); + if (l_tcd == 00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return OPJ_FALSE; + } - opj_j2k_tcp_destroy(l_tcp); - ++l_tcp; - } + for (i = 0; i < l_nb_tiles; ++i) { + if (l_tcp->m_data) { + if (! opj_tcd_init_decode_tile(l_tcd, i)) { + opj_tcd_destroy(l_tcd); + opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return OPJ_FALSE; + } + + l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index); + /* cleanup */ + + if (! l_success) { + p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; + break; + } + } + + opj_j2k_tcp_destroy(l_tcp); + ++l_tcp; + } - opj_tcd_destroy(l_tcd); - return OPJ_TRUE; + opj_tcd_destroy(l_tcd); + return OPJ_TRUE; } /** * Gets the offset of the header. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ opj_bool opj_j2k_get_end_header(opj_j2k_v2_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager ) { - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); - p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream); + p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream); - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the MCT marker (Multiple Component Transform) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) -{ - OPJ_UINT32 i; - opj_simple_mcc_decorrelation_data_t * l_mcc_record; - opj_mct_data_t * l_mct_record; - opj_tcp_v2_t * l_tcp; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_stream != 00); - assert(p_manager != 00); - - if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) { - return OPJ_FALSE; - } +opj_bool opj_j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager ) +{ + OPJ_UINT32 i; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + opj_mct_data_t * l_mct_record; + opj_tcp_v2_t * l_tcp; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) { + return OPJ_FALSE; + } - l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); - l_mct_record = l_tcp->m_mct_records; + l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); + l_mct_record = l_tcp->m_mct_records; - for (i=0;im_nb_mct_records;++i) { + for (i=0;im_nb_mct_records;++i) { - if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) { - return OPJ_FALSE; - } + if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) { + return OPJ_FALSE; + } - ++l_mct_record; - } + ++l_mct_record; + } - l_mcc_record = l_tcp->m_mcc_records; + l_mcc_record = l_tcp->m_mcc_records; - for (i=0;im_nb_mcc_records;++i) { + for (i=0;im_nb_mcc_records;++i) { - if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) { - return OPJ_FALSE; - } + if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) { + return OPJ_FALSE; + } - ++l_mcc_record; - } + ++l_mcc_record; + } - if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) { - return OPJ_FALSE; - } + if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the image components. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ opj_bool opj_j2k_write_image_components(opj_j2k_v2_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager ) { - OPJ_UINT32 compno; + OPJ_UINT32 compno; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); - for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) - { - if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) { - return OPJ_FALSE; - } + for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) + { + if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) { + return OPJ_FALSE; + } - if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) { - return OPJ_FALSE; - } - } + if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) { + return OPJ_FALSE; + } + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes regions of interests. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_regions( opj_j2k_v2_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) +opj_bool opj_j2k_write_regions( opj_j2k_v2_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager ) { - OPJ_UINT32 compno; - const opj_tccp_t *l_tccp = 00; + OPJ_UINT32 compno; + const opj_tccp_t *l_tccp = 00; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); - l_tccp = p_j2k->m_cp.tcps->tccps; + l_tccp = p_j2k->m_cp.tcps->tccps; - for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { - if (l_tccp->roishift) { + for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) { + if (l_tccp->roishift) { - if (! opj_j2k_write_rgn(p_j2k,0,compno,p_stream,p_manager)) { - return OPJ_FALSE; - } - } + if (! opj_j2k_write_rgn(p_j2k,0,compno,p_stream,p_manager)) { + return OPJ_FALSE; + } + } - ++l_tccp; - } + ++l_tccp; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes EPC ???? * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_epc( opj_j2k_v2_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) -{ - opj_codestream_index_t * l_cstr_index = 00; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_cstr_index = p_j2k->cstr_index; - if (l_cstr_index) { - l_cstr_index->codestream_size = opj_stream_tell(p_stream); - /* UniPG>> */ - /* The following adjustment is done to adjust the codestream size */ - /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */ - /* the first bunch of bytes is not in the codestream */ - l_cstr_index->codestream_size -= l_cstr_index->main_head_start; - /* <cstr_index; + if (l_cstr_index) { + l_cstr_index->codestream_size = opj_stream_tell(p_stream); + /* UniPG>> */ + /* The following adjustment is done to adjust the codestream size */ + /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */ + /* the first bunch of bytes is not in the codestream */ + l_cstr_index->codestream_size -= l_cstr_index->main_head_start; + /* <epc_on) { + if(cp->epc_on) { - /* encode according to JPWL */ - jpwl_encode(p_j2k, p_stream, image); + /* encode according to JPWL */ + jpwl_encode(p_j2k, p_stream, image); - } + } #endif assert( 0 && "TODO" ); #endif /* USE_JPWL */ - return OPJ_TRUE; + return OPJ_TRUE; } @@ -4994,904 +5070,920 @@ opj_bool opj_j2k_write_epc( opj_j2k_v2_t *p_j2k, /** * Reads an unknown marker * - * @param p_stream the stream object to read from. - * @param p_j2k the jpeg2000 codec. - * @param p_manager the user event manager. + * @param p_stream the stream object to read from. + * @param p_j2k the jpeg2000 codec. + * @param p_manager the user event manager. * - * @return true if the marker could be deduced. + * @return true if the marker could be deduced. */ -opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - OPJ_UINT32 *output_marker, - opj_event_mgr_t * p_manager - ) -{ - OPJ_UINT32 l_unknown_marker; - const opj_dec_memory_marker_handler_t * l_marker_handler; - OPJ_UINT32 l_size_unk = 2; - - /* preconditions*/ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n"); - - while(1) { - /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ - if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - - /* read 2 bytes as the new marker ID*/ - opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2); - - if (!(l_unknown_marker < 0xff00)) { - - /* Get the marker handler from the marker ID*/ - l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker); - - if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); - return OPJ_FALSE; - } - else { - if (l_marker_handler->id != J2K_MS_UNK) { - /* Add the marker to the codestream index*/ - if (l_marker_handler->id != J2K_MS_SOT) - opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK, - (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk, - l_size_unk); - break; /* next marker is known and well located */ - } - else - l_size_unk += 2; - } - } - } - - *output_marker = l_marker_handler->id ; - - return OPJ_TRUE; +opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + OPJ_UINT32 *output_marker, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_unknown_marker; + const opj_dec_memory_marker_handler_t * l_marker_handler; + OPJ_UINT32 l_size_unk = 2; + + /* preconditions*/ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n"); + + while(1) { + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* read 2 bytes as the new marker ID*/ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2); + + if (!(l_unknown_marker < 0xff00)) { + + /* Get the marker handler from the marker ID*/ + l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker); + + if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); + return OPJ_FALSE; + } + else { + if (l_marker_handler->id != J2K_MS_UNK) { + /* Add the marker to the codestream index*/ + if (l_marker_handler->id != J2K_MS_SOT) + { + opj_bool res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK, + (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk, + l_size_unk); + if (res == OPJ_FALSE) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); + return OPJ_FALSE; + } + } + break; /* next marker is known and well located */ + } + else + l_size_unk += 2; + } + } + } + + *output_marker = l_marker_handler->id ; + + return OPJ_TRUE; } /** * Writes the MCT marker (Multiple Component Transform) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_mct_record( opj_j2k_v2_t *p_j2k, - opj_mct_data_t * p_mct_record, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) -{ - OPJ_UINT32 l_mct_size; - OPJ_BYTE * l_current_data = 00; - OPJ_UINT32 l_tmp; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_mct_size = 10 + p_mct_record->m_data_size; - - if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_mct_size); - - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size; - } +opj_bool opj_j2k_write_mct_record( opj_j2k_v2_t *p_j2k, + opj_mct_data_t * p_mct_record, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager ) +{ + OPJ_UINT32 l_mct_size; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_tmp; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_mct_size = 10 + p_mct_record->m_data_size; + + if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size; + } - l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */ - l_current_data += 2; + opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */ - l_current_data += 2; + opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */ + l_current_data += 2; - opj_write_bytes(l_current_data,0,2); /* Zmct */ - l_current_data += 2; + opj_write_bytes(l_current_data,0,2); /* Zmct */ + l_current_data += 2; - /* only one marker atm */ - l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10); + /* only one marker atm */ + l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10); - opj_write_bytes(l_current_data,l_tmp,2); - l_current_data += 2; + opj_write_bytes(l_current_data,l_tmp,2); + l_current_data += 2; - opj_write_bytes(l_current_data,0,2); /* Ymct */ - l_current_data+=2; + opj_write_bytes(l_current_data,0,2); /* Ymct */ + l_current_data+=2; - memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size); + memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size); - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a MCT marker (Multiple Component Transform) * - * @param p_header_data the data contained in the MCT box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the MCT marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the MCT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCT marker. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager +static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager ) { - OPJ_UINT32 i; - opj_tcp_v2_t *l_tcp = 00; - OPJ_UINT32 l_tmp; - OPJ_UINT32 l_indix; - opj_mct_data_t * l_mct_data; - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - - l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? - &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; - - if (p_header_size < 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n"); - return OPJ_FALSE; - } - - /* first marker */ - opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */ - p_header_data += 2; - if (l_tmp != 0) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n"); - return OPJ_TRUE; - } - - if(p_header_size <= 6) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n"); - return OPJ_FALSE; - } - - /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/ - opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */ - p_header_data += 2; - - l_indix = l_tmp & 0xff; - l_mct_data = l_tcp->m_mct_records; - - for (i=0;im_nb_mct_records;++i) { - if (l_mct_data->m_index == l_indix) { - break; - } - ++l_mct_data; - } - - /* NOT FOUND */ - if (i == l_tcp->m_nb_mct_records) { - if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) { - l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS; - - l_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(l_tcp->m_mct_records,l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); - if(! l_tcp->m_mct_records) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n"); - return OPJ_FALSE; - } - - l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; - memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); - } - - l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; - } - - if (l_mct_data->m_data) { - opj_free(l_mct_data->m_data); - l_mct_data->m_data = 00; - } - - l_mct_data->m_index = l_indix; - l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3); - l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3); - - opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */ - p_header_data+=2; - if (l_tmp != 0) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n"); - return OPJ_TRUE; - } - - p_header_size -= 6; - - l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size); - if (! l_mct_data->m_data) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n"); - return OPJ_FALSE; - } - memcpy(l_mct_data->m_data,p_header_data,p_header_size); - - l_mct_data->m_data_size = p_header_size; - ++l_tcp->m_nb_mct_records; - - return OPJ_TRUE; + OPJ_UINT32 i; + opj_tcp_v2_t *l_tcp = 00; + OPJ_UINT32 l_tmp; + OPJ_UINT32 l_indix; + opj_mct_data_t * l_mct_data; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? + &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + if (p_header_size < 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + return OPJ_FALSE; + } + + /* first marker */ + opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */ + p_header_data += 2; + if (l_tmp != 0) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n"); + return OPJ_TRUE; + } + + if(p_header_size <= 6) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + return OPJ_FALSE; + } + + /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/ + opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */ + p_header_data += 2; + + l_indix = l_tmp & 0xff; + l_mct_data = l_tcp->m_mct_records; + + for (i=0;im_nb_mct_records;++i) { + if (l_mct_data->m_index == l_indix) { + break; + } + ++l_mct_data; + } + + /* NOT FOUND */ + if (i == l_tcp->m_nb_mct_records) { + if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) { + l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS; + + opj_mct_data_t *new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records, l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); + if (! new_mct_records) { + opj_free(l_tcp->m_mct_records); + l_tcp->m_mct_records = NULL; + l_tcp->m_nb_max_mct_records = 0; + l_tcp->m_nb_mct_records = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n"); + return OPJ_FALSE; + } + l_tcp->m_mct_records = new_mct_records; + l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; + memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); + } + + l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; + } + + if (l_mct_data->m_data) { + opj_free(l_mct_data->m_data); + l_mct_data->m_data = 00; + } + + l_mct_data->m_index = l_indix; + l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3); + l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3); + + opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */ + p_header_data+=2; + if (l_tmp != 0) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n"); + return OPJ_TRUE; + } + + p_header_size -= 6; + + l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size); + if (! l_mct_data->m_data) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + return OPJ_FALSE; + } + memcpy(l_mct_data->m_data,p_header_data,p_header_size); + + l_mct_data->m_data_size = p_header_size; + ++l_tcp->m_nb_mct_records; + + return OPJ_TRUE; } /** * Writes the MCC marker (Multiple Component Collection) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k, - struct opj_simple_mcc_decorrelation_data * p_mcc_record, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) -{ - OPJ_UINT32 i; - OPJ_UINT32 l_mcc_size; - OPJ_BYTE * l_current_data = 00; - OPJ_UINT32 l_nb_bytes_for_comp; - OPJ_UINT32 l_mask; - OPJ_UINT32 l_tmcc; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - if (p_mcc_record->m_nb_comps > 255 ) { +opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k, + struct opj_simple_mcc_decorrelation_data * p_mcc_record, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager ) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_mcc_size; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_nb_bytes_for_comp; + OPJ_UINT32 l_mask; + OPJ_UINT32 l_tmcc; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + if (p_mcc_record->m_nb_comps > 255 ) { l_nb_bytes_for_comp = 2; - l_mask = 0x8000; - } - else { - l_nb_bytes_for_comp = 1; - l_mask = 0; - } - - l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19; - if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) - { - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_mcc_size); - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } + l_mask = 0x8000; + } + else { + l_nb_bytes_for_comp = 1; + l_mask = 0; + } - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size; - } + l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19; + if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size; + } - l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */ - l_current_data += 2; + opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */ - l_current_data += 2; + opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */ + l_current_data += 2; - /* first marker */ - opj_write_bytes(l_current_data,0,2); /* Zmcc */ - l_current_data += 2; + /* first marker */ + opj_write_bytes(l_current_data,0,2); /* Zmcc */ + l_current_data += 2; - opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */ - ++l_current_data; + opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */ + ++l_current_data; - /* only one marker atm */ - opj_write_bytes(l_current_data,0,2); /* Ymcc */ - l_current_data+=2; + /* only one marker atm */ + opj_write_bytes(l_current_data,0,2); /* Ymcc */ + l_current_data+=2; - opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */ - l_current_data+=2; + opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */ + l_current_data+=2; - opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */ - ++l_current_data; + opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */ + ++l_current_data; - opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2); /* Nmcci number of input components involved and size for each component offset = 8 bits */ - l_current_data+=2; + opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2); /* Nmcci number of input components involved and size for each component offset = 8 bits */ + l_current_data+=2; - for (i=0;im_nb_comps;++i) { - opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/ - l_current_data+=l_nb_bytes_for_comp; - } + for (i=0;im_nb_comps;++i) { + opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/ + l_current_data+=l_nb_bytes_for_comp; + } - opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2); /* Mmcci number of output components involved and size for each component offset = 8 bits */ - l_current_data+=2; + opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2); /* Mmcci number of output components involved and size for each component offset = 8 bits */ + l_current_data+=2; - for (i=0;im_nb_comps;++i) - { - opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/ - l_current_data+=l_nb_bytes_for_comp; - } + for (i=0;im_nb_comps;++i) + { + opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/ + l_current_data+=l_nb_bytes_for_comp; + } - l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16; + l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16; - if (p_mcc_record->m_decorrelation_array) { - l_tmcc |= p_mcc_record->m_decorrelation_array->m_index; - } + if (p_mcc_record->m_decorrelation_array) { + l_tmcc |= p_mcc_record->m_decorrelation_array->m_index; + } - if (p_mcc_record->m_offset_array) { - l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8); - } + if (p_mcc_record->m_offset_array) { + l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8); + } - opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */ - l_current_data+=3; + opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */ + l_current_data+=3; - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a MCC marker (Multiple Component Collection) * - * @param p_header_data the data contained in the MCC box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the MCC marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the MCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCC marker. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ) -{ - OPJ_UINT32 i,j; - OPJ_UINT32 l_tmp; - OPJ_UINT32 l_indix; - opj_tcp_v2_t * l_tcp; - opj_simple_mcc_decorrelation_data_t * l_mcc_record; - opj_mct_data_t * l_mct_data; - OPJ_UINT32 l_nb_collections; - OPJ_UINT32 l_nb_comps; - OPJ_UINT32 l_nb_bytes_by_comp; - - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? - &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; - - if (p_header_size < 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); - return OPJ_FALSE; - } - - /* first marker */ - opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */ - p_header_data += 2; - if (l_tmp != 0) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n"); - return OPJ_TRUE; - } - - if (p_header_size < 7) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); - return OPJ_FALSE; - } - - opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */ - ++p_header_data; - - l_mcc_record = l_tcp->m_mcc_records; - - for(i=0;im_nb_mcc_records;++i) { - if (l_mcc_record->m_index == l_indix) { - break; - } - ++l_mcc_record; - } - - /** NOT FOUND */ - if (i == l_tcp->m_nb_mcc_records) { - if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) { - l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS; - - l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) - opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t)); - if (! l_tcp->m_mcc_records) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); - return OPJ_FALSE; - } - l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; - memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t)); - } - l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; - } - l_mcc_record->m_index = l_indix; - - /* only one marker atm */ - opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */ - p_header_data+=2; - if (l_tmp != 0) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n"); - return OPJ_TRUE; - } - - opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */ - p_header_data+=2; - - if (l_nb_collections > 1) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n"); - return OPJ_TRUE; - } - - p_header_size -= 7; - - for (i=0;i array based decorrelation */ - ++p_header_data; - - if (l_tmp != 1) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n"); - return OPJ_TRUE; - } - - opj_read_bytes(p_header_data,&l_nb_comps,2); - - p_header_data+=2; - p_header_size-=3; - - l_nb_bytes_by_comp = 1 + (l_nb_comps>>15); - l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff; - - if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); - return OPJ_FALSE; - } - - p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2); - - for (j=0;jm_nb_comps;++j) { - opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/ - p_header_data+=l_nb_bytes_by_comp; - - if (l_tmp != j) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); - return OPJ_TRUE; - } - } - - opj_read_bytes(p_header_data,&l_nb_comps,2); - p_header_data+=2; - - l_nb_bytes_by_comp = 1 + (l_nb_comps>>15); - l_nb_comps &= 0x7fff; - - if (l_nb_comps != l_mcc_record->m_nb_comps) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n"); - return OPJ_TRUE; - } - - if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); - return OPJ_FALSE; - } - - p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3); - - for (j=0;jm_nb_comps;++j) { - opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/ - p_header_data+=l_nb_bytes_by_comp; - - if (l_tmp != j) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); - return OPJ_TRUE; - } - } - - opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/ - p_header_data += 3; - - l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1); - l_mcc_record->m_decorrelation_array = 00; - l_mcc_record->m_offset_array = 00; - - l_indix = l_tmp & 0xff; - if (l_indix != 0) { - l_mct_data = l_tcp->m_mct_records; - for (j=0;jm_nb_mct_records;++j) { - if (l_mct_data->m_index == l_indix) { - l_mcc_record->m_decorrelation_array = l_mct_data; - break; - } - ++l_mct_data; - } - - if (l_mcc_record->m_decorrelation_array == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); - return OPJ_FALSE; - } - } +opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager ) +{ + OPJ_UINT32 i,j; + OPJ_UINT32 l_tmp; + OPJ_UINT32 l_indix; + opj_tcp_v2_t * l_tcp; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + opj_mct_data_t * l_mct_data; + OPJ_UINT32 l_nb_collections; + OPJ_UINT32 l_nb_comps; + OPJ_UINT32 l_nb_bytes_by_comp; + + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? + &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + if (p_header_size < 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + + /* first marker */ + opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */ + p_header_data += 2; + if (l_tmp != 0) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n"); + return OPJ_TRUE; + } + + if (p_header_size < 7) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */ + ++p_header_data; + + l_mcc_record = l_tcp->m_mcc_records; + + for(i=0;im_nb_mcc_records;++i) { + if (l_mcc_record->m_index == l_indix) { + break; + } + ++l_mcc_record; + } + + /** NOT FOUND */ + if (i == l_tcp->m_nb_mcc_records) { + if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) { + l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS; + + opj_simple_mcc_decorrelation_data_t *new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( + l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t)); + if (! new_mcc_records) { + opj_free(l_tcp->m_mcc_records); + l_tcp->m_mcc_records = NULL; + l_tcp->m_nb_max_mcc_records = 0; + l_tcp->m_nb_mcc_records = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n"); + return OPJ_FALSE; + } + l_tcp->m_mcc_records = new_mcc_records; + l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; + memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t)); + } + l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; + } + l_mcc_record->m_index = l_indix; + + /* only one marker atm */ + opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */ + p_header_data+=2; + if (l_tmp != 0) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n"); + return OPJ_TRUE; + } + + opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */ + p_header_data+=2; + + if (l_nb_collections > 1) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n"); + return OPJ_TRUE; + } - l_indix = (l_tmp >> 8) & 0xff; - if (l_indix != 0) { - l_mct_data = l_tcp->m_mct_records; - for (j=0;jm_nb_mct_records;++j) { - if (l_mct_data->m_index == l_indix) { - l_mcc_record->m_offset_array = l_mct_data; - break; - } - ++l_mct_data; - } - - if (l_mcc_record->m_offset_array == 00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); - return OPJ_FALSE; - } - } - } + p_header_size -= 7; + + for (i=0;i array based decorrelation */ + ++p_header_data; + + if (l_tmp != 1) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n"); + return OPJ_TRUE; + } + + opj_read_bytes(p_header_data,&l_nb_comps,2); + + p_header_data+=2; + p_header_size-=3; + + l_nb_bytes_by_comp = 1 + (l_nb_comps>>15); + l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff; + + if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + + p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2); + + for (j=0;jm_nb_comps;++j) { + opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/ + p_header_data+=l_nb_bytes_by_comp; + + if (l_tmp != j) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); + return OPJ_TRUE; + } + } + + opj_read_bytes(p_header_data,&l_nb_comps,2); + p_header_data+=2; + + l_nb_bytes_by_comp = 1 + (l_nb_comps>>15); + l_nb_comps &= 0x7fff; + + if (l_nb_comps != l_mcc_record->m_nb_comps) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n"); + return OPJ_TRUE; + } + + if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + + p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3); + + for (j=0;jm_nb_comps;++j) { + opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/ + p_header_data+=l_nb_bytes_by_comp; + + if (l_tmp != j) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); + return OPJ_TRUE; + } + } + + opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/ + p_header_data += 3; + + l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1); + l_mcc_record->m_decorrelation_array = 00; + l_mcc_record->m_offset_array = 00; + + l_indix = l_tmp & 0xff; + if (l_indix != 0) { + l_mct_data = l_tcp->m_mct_records; + for (j=0;jm_nb_mct_records;++j) { + if (l_mct_data->m_index == l_indix) { + l_mcc_record->m_decorrelation_array = l_mct_data; + break; + } + ++l_mct_data; + } + + if (l_mcc_record->m_decorrelation_array == 00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + } + + l_indix = (l_tmp >> 8) & 0xff; + if (l_indix != 0) { + l_mct_data = l_tcp->m_mct_records; + for (j=0;jm_nb_mct_records;++j) { + if (l_mct_data->m_index == l_indix) { + l_mcc_record->m_offset_array = l_mct_data; + break; + } + ++l_mct_data; + } + + if (l_mcc_record->m_offset_array == 00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } + } + } - if (p_header_size != 0) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); - return OPJ_FALSE; - } + if (p_header_size != 0) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return OPJ_FALSE; + } - ++l_tcp->m_nb_mcc_records; + ++l_tcp->m_nb_mcc_records; - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the MCO marker (Multiple component transformation ordering) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_write_mco( opj_j2k_v2_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager - ) +opj_bool opj_j2k_write_mco( opj_j2k_v2_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) { - OPJ_BYTE * l_current_data = 00; - OPJ_UINT32 l_mco_size; - opj_tcp_v2_t * l_tcp = 00; - opj_simple_mcc_decorrelation_data_t * l_mcc_record; - OPJ_UINT32 i; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); - l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - - l_mco_size = 5 + l_tcp->m_nb_mcc_records; - if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_mco_size); - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) - { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size; - } + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_mco_size; + opj_tcp_v2_t * l_tcp = 00; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + OPJ_UINT32 i; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + l_mco_size = 5 + l_tcp->m_nb_mcc_records; + if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size; + } - opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */ - l_current_data += 2; + opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */ - l_current_data += 2; + opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/ - ++l_current_data; + opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/ + ++l_current_data; - l_mcc_record = l_tcp->m_mcc_records; - for (i=0;im_nb_mcc_records;++i) { - opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/ - ++l_current_data; + l_mcc_record = l_tcp->m_mcc_records; + for (i=0;im_nb_mcc_records;++i) { + opj_write_bytes(l_current_data,l_mcc_record->m_index,1);/* Imco -> use the mcc indicated by 1*/ + ++l_current_data; - ++l_mcc_record; - } + ++l_mcc_record; + } - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a MCO marker (Multiple Component Transform Ordering) * - * @param p_header_data the data contained in the MCO box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the MCO marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the MCO box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCO marker. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager +static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_tmp, i; - OPJ_UINT32 l_nb_stages; - opj_tcp_v2_t * l_tcp; - opj_tccp_t * l_tccp; - opj_image_t * l_image; - opj_image_comp_t * l_img_comp; - - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - l_image = p_j2k->m_private_image; - l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? - &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; - - if (p_header_size < 1) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n"); - return OPJ_FALSE; - } + OPJ_UINT32 l_tmp, i; + OPJ_UINT32 l_nb_stages; + opj_tcp_v2_t * l_tcp; + opj_tccp_t * l_tccp; + opj_image_t * l_image; + opj_image_comp_t * l_img_comp; + + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_private_image; + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? + &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + if (p_header_size < 1) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n"); + return OPJ_FALSE; + } - opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/ - ++p_header_data; + opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/ + ++p_header_data; - if (l_nb_stages > 1) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n"); - return OPJ_TRUE; - } + if (l_nb_stages > 1) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n"); + return OPJ_TRUE; + } - if (p_header_size != l_nb_stages + 1) { - opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n"); - return OPJ_FALSE; - } + if (p_header_size != l_nb_stages + 1) { + opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n"); + return OPJ_FALSE; + } - l_tccp = l_tcp->tccps; - l_img_comp = l_image->comps; + l_tccp = l_tcp->tccps; + l_img_comp = l_image->comps; - for (i=0;inumcomps;++i) { - l_tccp->m_dc_level_shift = 0; - ++l_tccp; - } + for (i=0;inumcomps;++i) { + l_tccp->m_dc_level_shift = 0; + ++l_tccp; + } - if (l_tcp->m_mct_decoding_matrix) { - opj_free(l_tcp->m_mct_decoding_matrix); - l_tcp->m_mct_decoding_matrix = 00; - } + if (l_tcp->m_mct_decoding_matrix) { + opj_free(l_tcp->m_mct_decoding_matrix); + l_tcp->m_mct_decoding_matrix = 00; + } - for (i=0;im_private_image,l_tmp)) { - return OPJ_FALSE; - } - } + if (! opj_j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) { + return OPJ_FALSE; + } + } - return OPJ_TRUE; + return OPJ_TRUE; } opj_bool opj_j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index) { - OPJ_UINT32 i; - opj_simple_mcc_decorrelation_data_t * l_mcc_record; - opj_mct_data_t * l_deco_array, * l_offset_array; - OPJ_UINT32 l_data_size,l_mct_size, l_offset_size; - OPJ_UINT32 l_nb_elem; - OPJ_UINT32 * l_offset_data, * l_current_offset_data; - opj_tccp_t * l_tccp; + OPJ_UINT32 i; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + opj_mct_data_t * l_deco_array, * l_offset_array; + OPJ_UINT32 l_data_size,l_mct_size, l_offset_size; + OPJ_UINT32 l_nb_elem; + OPJ_UINT32 * l_offset_data, * l_current_offset_data; + opj_tccp_t * l_tccp; - /* preconditions */ - assert(p_tcp != 00); + /* preconditions */ + assert(p_tcp != 00); - l_mcc_record = p_tcp->m_mcc_records; + l_mcc_record = p_tcp->m_mcc_records; - for (i=0;im_nb_mcc_records;++i) { - if (l_mcc_record->m_index == p_index) { - break; - } - } + for (i=0;im_nb_mcc_records;++i) { + if (l_mcc_record->m_index == p_index) { + break; + } + } - if (i==p_tcp->m_nb_mcc_records) { - /** element discarded **/ - return OPJ_TRUE; - } + if (i==p_tcp->m_nb_mcc_records) { + /** element discarded **/ + return OPJ_TRUE; + } - if (l_mcc_record->m_nb_comps != p_image->numcomps) { - /** do not support number of comps != image */ - return OPJ_TRUE; - } + if (l_mcc_record->m_nb_comps != p_image->numcomps) { + /** do not support number of comps != image */ + return OPJ_TRUE; + } - l_deco_array = l_mcc_record->m_decorrelation_array; + l_deco_array = l_mcc_record->m_decorrelation_array; - if (l_deco_array) { - l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps; - if (l_deco_array->m_data_size != l_data_size) { - return OPJ_FALSE; - } + if (l_deco_array) { + l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps; + if (l_deco_array->m_data_size != l_data_size) { + return OPJ_FALSE; + } - l_nb_elem = p_image->numcomps * p_image->numcomps; - l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32); - p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); + l_nb_elem = p_image->numcomps * p_image->numcomps; + l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32); + p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); - if (! p_tcp->m_mct_decoding_matrix ) { - return OPJ_FALSE; - } + if (! p_tcp->m_mct_decoding_matrix ) { + return OPJ_FALSE; + } - j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem); - } + j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem); + } - l_offset_array = l_mcc_record->m_offset_array; + l_offset_array = l_mcc_record->m_offset_array; - if (l_offset_array) { - l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps; - if (l_offset_array->m_data_size != l_data_size) { - return OPJ_FALSE; - } + if (l_offset_array) { + l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps; + if (l_offset_array->m_data_size != l_data_size) { + return OPJ_FALSE; + } - l_nb_elem = p_image->numcomps; - l_offset_size = l_nb_elem * sizeof(OPJ_UINT32); - l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size); + l_nb_elem = p_image->numcomps; + l_offset_size = l_nb_elem * sizeof(OPJ_UINT32); + l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size); - if (! l_offset_data ) { - return OPJ_FALSE; - } + if (! l_offset_data ) { + return OPJ_FALSE; + } - j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem); + j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem); - l_tccp = p_tcp->tccps; - l_current_offset_data = l_offset_data; + l_tccp = p_tcp->tccps; + l_current_offset_data = l_offset_data; - for (i=0;inumcomps;++i) { - l_tccp->m_dc_level_shift = *(l_current_offset_data++); - ++l_tccp; - } + for (i=0;inumcomps;++i) { + l_tccp->m_dc_level_shift = *(l_current_offset_data++); + ++l_tccp; + } - opj_free(l_offset_data); - } + opj_free(l_offset_data); + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes the CBD marker (Component bit depth definition) * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ opj_bool opj_j2k_write_cbd( opj_j2k_v2_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) -{ - OPJ_UINT32 i; - OPJ_UINT32 l_cbd_size; - OPJ_BYTE * l_current_data = 00; - opj_image_t *l_image = 00; - opj_image_comp_t * l_comp = 00; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - l_image = p_j2k->m_private_image; - l_cbd_size = 6 + p_j2k->m_private_image->numcomps; - - if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { - p_j2k->m_specific_param.m_encoder.m_header_tile_data - = (OPJ_BYTE*)opj_realloc( - p_j2k->m_specific_param.m_encoder.m_header_tile_data, - l_cbd_size); - - if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - return OPJ_FALSE; - } - - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size; - } + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager ) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_cbd_size; + OPJ_BYTE * l_current_data = 00; + opj_image_t *l_image = 00; + opj_image_comp_t * l_comp = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_image = p_j2k->m_private_image; + l_cbd_size = 6 + p_j2k->m_private_image->numcomps; + + if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) { + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size); + if (! new_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size; + } - l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */ - l_current_data += 2; + opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */ - l_current_data += 2; + opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */ + l_current_data += 2; - opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */ - l_current_data+=2; + opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */ + l_current_data+=2; - l_comp = l_image->comps; + l_comp = l_image->comps; - for (i=0;inumcomps;++i) { - opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */ - ++l_current_data; + for (i=0;inumcomps;++i) { + opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */ + ++l_current_data; - ++l_comp; - } + ++l_comp; + } - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a CBD marker (Component bit depth definition) - * @param p_header_data the data contained in the CBD box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the CBD marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the CBD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the CBD marker. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager +static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + opj_event_mgr_t * p_manager ) { - OPJ_UINT32 l_nb_comp,l_num_comp; - OPJ_UINT32 l_comp_def; - OPJ_UINT32 i; - opj_image_comp_t * l_comp = 00; + OPJ_UINT32 l_nb_comp,l_num_comp; + OPJ_UINT32 l_comp_def; + OPJ_UINT32 i; + opj_image_comp_t * l_comp = 00; - /* preconditions */ - assert(p_header_data != 00); - assert(p_j2k != 00); - assert(p_manager != 00); + /* preconditions */ + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); - l_num_comp = p_j2k->m_private_image->numcomps; + l_num_comp = p_j2k->m_private_image->numcomps; - if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); - return OPJ_FALSE; - } + if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); + return OPJ_FALSE; + } - opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */ - p_header_data+=2; + opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */ + p_header_data+=2; - if (l_nb_comp != l_num_comp) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); - return OPJ_FALSE; - } + if (l_nb_comp != l_num_comp) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); + return OPJ_FALSE; + } - l_comp = p_j2k->m_private_image->comps; - for (i=0;im_private_image->comps; + for (i=0;isgnd = (l_comp_def>>7) & 1; - l_comp->prec = (l_comp_def&0x7f) + 1; - ++l_comp; - } + l_comp->prec = (l_comp_def&0x7f) + 1; + ++l_comp; + } - return OPJ_TRUE; + return OPJ_TRUE; } @@ -5901,16 +5993,16 @@ static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k, void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters) { - if(j2k && parameters) { - j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer; - j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce; + if(j2k && parameters) { + j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer; + j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce; #ifdef USE_JPWL - j2k->m_cp.correct = parameters->jpwl_correct; - j2k->m_cp.exp_comps = parameters->jpwl_exp_comps; - j2k->m_cp.max_tiles = parameters->jpwl_max_tiles; + j2k->m_cp.correct = parameters->jpwl_correct; + j2k->m_cp.exp_comps = parameters->jpwl_exp_comps; + j2k->m_cp.max_tiles = parameters->jpwl_max_tiles; #endif /* USE_JPWL */ - } + } } @@ -5920,403 +6012,422 @@ void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters) opj_j2k_v2_t* opj_j2k_create_compress(void) { - opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t)); - if (!l_j2k) { - return NULL; - } - - memset(l_j2k,0,sizeof(opj_j2k_v2_t)); - - l_j2k->m_is_decoder = 0; - l_j2k->m_cp.m_is_decoder = 0; - - l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE); - if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) { - opj_j2k_destroy(l_j2k); - return NULL; - } - - l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE; - - /* validation list creation*/ - l_j2k->m_validation_list = opj_procedure_list_create(); - if (! l_j2k->m_validation_list) { - opj_j2k_destroy(l_j2k); - return NULL; - } - - /* execution list creation*/ - l_j2k->m_procedure_list = opj_procedure_list_create(); - if (! l_j2k->m_procedure_list) { - opj_j2k_destroy(l_j2k); - return NULL; - } - - return l_j2k; -} - - -void opj_j2k_setup_encoder( opj_j2k_v2_t *p_j2k, - opj_cparameters_t *parameters, - opj_image_t *image, - opj_event_mgr_t * p_manager) -{ - OPJ_UINT32 i, j, tileno, numpocs_tile; - opj_cp_v2_t *cp = 00; - opj_bool l_res; - - if(!p_j2k || !parameters || ! image) { - return; - } - - /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */ - cp = &(p_j2k->m_cp); - - /* set default values for cp */ - cp->tw = 1; - cp->th = 1; - - /* - copy user encoding parameters - */ - cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema; - cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size; - cp->rsiz = parameters->cp_rsiz; - cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc; - cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc; - cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality; - - /* mod fixed_quality */ - if (parameters->cp_matrice) { - size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32); - cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size); - memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size); - } - - /* tiles */ - cp->tdx = parameters->cp_tdx; - cp->tdy = parameters->cp_tdy; - - /* tile offset */ - cp->tx0 = parameters->cp_tx0; - cp->ty0 = parameters->cp_ty0; - - /* comment string */ - if(parameters->cp_comment) { - cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1); - if(cp->comment) { - strcpy(cp->comment, parameters->cp_comment); - } - } - - /* - calculate other encoding parameters - */ - - if (parameters->tile_size_on) { - cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx); - cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy); - } else { - cp->tdx = image->x1 - cp->tx0; - cp->tdy = image->y1 - cp->ty0; - } - - if (parameters->tp_on) { - cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag; - cp->m_specific_param.m_enc.m_tp_on = 1; - } + opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t)); + if (!l_j2k) { + return NULL; + } + + memset(l_j2k,0,sizeof(opj_j2k_v2_t)); + + l_j2k->m_is_decoder = 0; + l_j2k->m_cp.m_is_decoder = 0; + + l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE); + if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) { + opj_j2k_destroy(l_j2k); + return NULL; + } + + l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE; + + /* validation list creation*/ + l_j2k->m_validation_list = opj_procedure_list_create(); + if (! l_j2k->m_validation_list) { + opj_j2k_destroy(l_j2k); + return NULL; + } + + /* execution list creation*/ + l_j2k->m_procedure_list = opj_procedure_list_create(); + if (! l_j2k->m_procedure_list) { + opj_j2k_destroy(l_j2k); + return NULL; + } + + return l_j2k; +} + + +void opj_j2k_setup_encoder( opj_j2k_v2_t *p_j2k, + opj_cparameters_t *parameters, + opj_image_t *image, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i, j, tileno, numpocs_tile; + opj_cp_v2_t *cp = 00; + opj_bool l_res; + + if(!p_j2k || !parameters || ! image) { + return; + } + + /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */ + cp = &(p_j2k->m_cp); + + /* set default values for cp */ + cp->tw = 1; + cp->th = 1; + + /* + copy user encoding parameters + */ + cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema; + cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size; + cp->rsiz = parameters->cp_rsiz; + cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc; + cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc; + cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality; + + /* mod fixed_quality */ + if (parameters->cp_matrice) { + size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32); + cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size); + memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size); + } + + /* tiles */ + cp->tdx = parameters->cp_tdx; + cp->tdy = parameters->cp_tdy; + + /* tile offset */ + cp->tx0 = parameters->cp_tx0; + cp->ty0 = parameters->cp_ty0; + + /* comment string */ + if(parameters->cp_comment) { + cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1); + if(cp->comment) { + strcpy(cp->comment, parameters->cp_comment); + } + } + + /* + calculate other encoding parameters + */ + + if (parameters->tile_size_on) { + cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx); + cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy); + } else { + cp->tdx = image->x1 - cp->tx0; + cp->tdy = image->y1 - cp->ty0; + } + + if (parameters->tp_on) { + cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag; + cp->m_specific_param.m_enc.m_tp_on = 1; + } #ifdef USE_JPWL - /* - calculate JPWL encoding parameters - */ - - if (parameters->jpwl_epc_on) { - OPJ_INT32 i; - - /* set JPWL on */ - cp->epc_on = OPJ_TRUE; - cp->info_on = OPJ_FALSE; /* no informative technique */ - - /* set EPB on */ - if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) { - cp->epb_on = OPJ_TRUE; - - cp->hprot_MH = parameters->jpwl_hprot_MH; - for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { - cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i]; - cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i]; - } - /* if tile specs are not specified, copy MH specs */ - if (cp->hprot_TPH[0] == -1) { - cp->hprot_TPH_tileno[0] = 0; - cp->hprot_TPH[0] = parameters->jpwl_hprot_MH; - } - for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { - cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i]; - cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i]; - cp->pprot[i] = parameters->jpwl_pprot[i]; - } - } - - /* set ESD writing */ - if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) { - cp->esd_on = OPJ_TRUE; - - cp->sens_size = parameters->jpwl_sens_size; - cp->sens_addr = parameters->jpwl_sens_addr; - cp->sens_range = parameters->jpwl_sens_range; - - cp->sens_MH = parameters->jpwl_sens_MH; - for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { - cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i]; - cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i]; - } - } - - /* always set RED writing to false: we are at the encoder */ - cp->red_on = OPJ_FALSE; - - } else { - cp->epc_on = OPJ_FALSE; - } + /* + calculate JPWL encoding parameters + */ + + if (parameters->jpwl_epc_on) { + OPJ_INT32 i; + + /* set JPWL on */ + cp->epc_on = OPJ_TRUE; + cp->info_on = OPJ_FALSE; /* no informative technique */ + + /* set EPB on */ + if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) { + cp->epb_on = OPJ_TRUE; + + cp->hprot_MH = parameters->jpwl_hprot_MH; + for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { + cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i]; + cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i]; + } + /* if tile specs are not specified, copy MH specs */ + if (cp->hprot_TPH[0] == -1) { + cp->hprot_TPH_tileno[0] = 0; + cp->hprot_TPH[0] = parameters->jpwl_hprot_MH; + } + for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { + cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i]; + cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i]; + cp->pprot[i] = parameters->jpwl_pprot[i]; + } + } + + /* set ESD writing */ + if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) { + cp->esd_on = OPJ_TRUE; + + cp->sens_size = parameters->jpwl_sens_size; + cp->sens_addr = parameters->jpwl_sens_addr; + cp->sens_range = parameters->jpwl_sens_range; + + cp->sens_MH = parameters->jpwl_sens_MH; + for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { + cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i]; + cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i]; + } + } + + /* always set RED writing to false: we are at the encoder */ + cp->red_on = OPJ_FALSE; + + } else { + cp->epc_on = OPJ_FALSE; + } #endif /* USE_JPWL */ - /* initialize the mutiple tiles */ - /* ---------------------------- */ - cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t)); - if (parameters->numpocs) { - /* initialisation of POC */ - l_res = opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager); - // TODO - } - - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - opj_tcp_v2_t *tcp = &cp->tcps[tileno]; - tcp->numlayers = parameters->tcp_numlayers; - - for (j = 0; j < tcp->numlayers; j++) { - if(cp->m_specific_param.m_enc.m_cinema){ - if (cp->m_specific_param.m_enc.m_fixed_quality) { - tcp->distoratio[j] = parameters->tcp_distoratio[j]; - } - tcp->rates[j] = parameters->tcp_rates[j]; - }else{ - if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */ - tcp->distoratio[j] = parameters->tcp_distoratio[j]; - } else { - tcp->rates[j] = parameters->tcp_rates[j]; - } - } - } - - tcp->csty = parameters->csty; - tcp->prg = parameters->prog_order; - tcp->mct = parameters->tcp_mct; - - numpocs_tile = 0; - tcp->POC = 0; - - if (parameters->numpocs) { - /* initialisation of POC */ - tcp->POC = 1; - // TODO - for (i = 0; i < (unsigned int) parameters->numpocs; i++) { - if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) { - opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; - - tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; - tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0; - tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1; - tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1; - tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1; - tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1; - tcp_poc->tile = parameters->POC[numpocs_tile].tile; - - numpocs_tile++; - } - } - - tcp->numpocs = numpocs_tile -1 ; - }else{ - tcp->numpocs = 0; - } - - tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); - - if (parameters->mct_data) { - - opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n"); - return; - - /* TODO MSD : merge v2 add invert.c or used a external lib ? - OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32); - OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize); - OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize); - - tcp->mct = 2; - tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); - memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize); - memcpy(lTmpBuf,parameters->mct_data,lMctSize); - - tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); - assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps)); - - tcp->mct_norms = (OPJ_FLOAT64*) - opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64)); - - opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix); - opj_free(lTmpBuf); - - for (i = 0; i < image->numcomps; i++) { - opj_tccp_t *tccp = &tcp->tccps[i]; - tccp->m_dc_level_shift = l_dc_shift[i]; - } - - opj_j2k_setup_mct_encoding(tcp,image); - */ - } - else { - for (i = 0; i < image->numcomps; i++) { - opj_tccp_t *tccp = &tcp->tccps[i]; - opj_image_comp_t * l_comp = &(image->comps[i]); - - if (! l_comp->sgnd) { - tccp->m_dc_level_shift = 1 << (l_comp->prec - 1); - } - } - } - - for (i = 0; i < image->numcomps; i++) { - opj_tccp_t *tccp = &tcp->tccps[i]; - - tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */ - tccp->numresolutions = parameters->numresolution; - tccp->cblkw = int_floorlog2(parameters->cblockw_init); - tccp->cblkh = int_floorlog2(parameters->cblockh_init); - tccp->cblksty = parameters->mode; - tccp->qmfbid = parameters->irreversible ? 0 : 1; - tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT; - tccp->numgbits = 2; - - if (i == parameters->roi_compno) { - tccp->roishift = parameters->roi_shift; - } else { - tccp->roishift = 0; - } - - if(parameters->cp_cinema) { - //Precinct size for lowest frequency subband=128 - tccp->prcw[0] = 7; - tccp->prch[0] = 7; - //Precinct size at all other resolutions = 256 - for (j = 1; j < tccp->numresolutions; j++) { - tccp->prcw[j] = 8; - tccp->prch[j] = 8; - } - }else{ - if (parameters->csty & J2K_CCP_CSTY_PRT) { - OPJ_INT32 p = 0, it_res; - for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) { - if (p < parameters->res_spec) { - - if (parameters->prcw_init[p] < 1) { - tccp->prcw[it_res] = 1; - } else { - tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]); - } - - if (parameters->prch_init[p] < 1) { - tccp->prch[it_res] = 1; - }else { - tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]); - } - - } else { - int res_spec = parameters->res_spec; - int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1)); - int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1)); - - if (size_prcw < 1) { - tccp->prcw[it_res] = 1; - } else { - tccp->prcw[it_res] = int_floorlog2(size_prcw); - } - - if (size_prch < 1) { - tccp->prch[it_res] = 1; - } else { - tccp->prch[it_res] = int_floorlog2(size_prch); - } - } - p++; - /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */ - } //end for - } else { - for (j = 0; j < tccp->numresolutions; j++) { - tccp->prcw[j] = 15; - tccp->prch[j] = 15; - } - } - } - - opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec); - } - } - - if (parameters->mct_data) { - opj_free(parameters->mct_data); - parameters->mct_data = 00; - } -} - - - -static void opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) -{ - assert(cstr_index != 00); - - /* expand the list? */ - if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) { - cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F); - cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t)); - } - - /* add the marker */ - cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type; - cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos; - cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len; - cstr_index->marknum++; - -} - -static void opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) -{ - assert(cstr_index != 00); - assert(cstr_index->tile_index != 00); - - /* expand the list? */ - if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) { - cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F); - cstr_index->tile_index[tileno].marker = - (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker, - cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t)); - } - - /* add the marker */ - cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type; - cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos; - cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len; - cstr_index->tile_index[tileno].marknum++; - - if (type == J2K_MS_SOT) { - OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno; - - if (cstr_index->tile_index[tileno].tp_index) - cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos; + /* initialize the mutiple tiles */ + /* ---------------------------- */ + cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t)); + if (parameters->numpocs) { + /* initialisation of POC */ + l_res = opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager); + // TODO + } + + for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { + opj_tcp_v2_t *tcp = &cp->tcps[tileno]; + tcp->numlayers = parameters->tcp_numlayers; + + for (j = 0; j < tcp->numlayers; j++) { + if(cp->m_specific_param.m_enc.m_cinema){ + if (cp->m_specific_param.m_enc.m_fixed_quality) { + tcp->distoratio[j] = parameters->tcp_distoratio[j]; + } + tcp->rates[j] = parameters->tcp_rates[j]; + }else{ + if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */ + tcp->distoratio[j] = parameters->tcp_distoratio[j]; + } else { + tcp->rates[j] = parameters->tcp_rates[j]; + } + } + } + + tcp->csty = parameters->csty; + tcp->prg = parameters->prog_order; + tcp->mct = parameters->tcp_mct; + + numpocs_tile = 0; + tcp->POC = 0; + + if (parameters->numpocs) { + /* initialisation of POC */ + tcp->POC = 1; + // TODO + for (i = 0; i < (unsigned int) parameters->numpocs; i++) { + if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) { + opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; + + tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; + tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0; + tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1; + tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1; + tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1; + tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1; + tcp_poc->tile = parameters->POC[numpocs_tile].tile; + + numpocs_tile++; + } + } + + tcp->numpocs = numpocs_tile -1 ; + }else{ + tcp->numpocs = 0; + } + + tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); + + if (parameters->mct_data) { + + opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n"); + return; + + /* TODO MSD : merge v2 add invert.c or used a external lib ? + OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32); + OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize); + OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize); + + tcp->mct = 2; + tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); + memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize); + memcpy(lTmpBuf,parameters->mct_data,lMctSize); + + tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); + assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps)); + + tcp->mct_norms = (OPJ_FLOAT64*) + opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64)); + + opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix); + opj_free(lTmpBuf); + + for (i = 0; i < image->numcomps; i++) { + opj_tccp_t *tccp = &tcp->tccps[i]; + tccp->m_dc_level_shift = l_dc_shift[i]; + } + + opj_j2k_setup_mct_encoding(tcp,image); + */ + } + else { + for (i = 0; i < image->numcomps; i++) { + opj_tccp_t *tccp = &tcp->tccps[i]; + opj_image_comp_t * l_comp = &(image->comps[i]); + + if (! l_comp->sgnd) { + tccp->m_dc_level_shift = 1 << (l_comp->prec - 1); + } + } + } + + for (i = 0; i < image->numcomps; i++) { + opj_tccp_t *tccp = &tcp->tccps[i]; + + tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */ + tccp->numresolutions = parameters->numresolution; + tccp->cblkw = int_floorlog2(parameters->cblockw_init); + tccp->cblkh = int_floorlog2(parameters->cblockh_init); + tccp->cblksty = parameters->mode; + tccp->qmfbid = parameters->irreversible ? 0 : 1; + tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT; + tccp->numgbits = 2; + + if (i == parameters->roi_compno) { + tccp->roishift = parameters->roi_shift; + } else { + tccp->roishift = 0; + } + + if(parameters->cp_cinema) { + //Precinct size for lowest frequency subband=128 + tccp->prcw[0] = 7; + tccp->prch[0] = 7; + //Precinct size at all other resolutions = 256 + for (j = 1; j < tccp->numresolutions; j++) { + tccp->prcw[j] = 8; + tccp->prch[j] = 8; + } + }else{ + if (parameters->csty & J2K_CCP_CSTY_PRT) { + OPJ_INT32 p = 0, it_res; + for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) { + if (p < parameters->res_spec) { + + if (parameters->prcw_init[p] < 1) { + tccp->prcw[it_res] = 1; + } else { + tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]); + } + + if (parameters->prch_init[p] < 1) { + tccp->prch[it_res] = 1; + }else { + tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]); + } + + } else { + int res_spec = parameters->res_spec; + int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1)); + int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1)); + + if (size_prcw < 1) { + tccp->prcw[it_res] = 1; + } else { + tccp->prcw[it_res] = int_floorlog2(size_prcw); + } + + if (size_prch < 1) { + tccp->prch[it_res] = 1; + } else { + tccp->prch[it_res] = int_floorlog2(size_prch); + } + } + p++; + /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */ + } //end for + } else { + for (j = 0; j < tccp->numresolutions; j++) { + tccp->prcw[j] = 15; + tccp->prch[j] = 15; + } + } + } + + opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec); + } + } + + if (parameters->mct_data) { + opj_free(parameters->mct_data); + parameters->mct_data = 00; + } +} + + + +static opj_bool opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) +{ + assert(cstr_index != 00); + + /* expand the list? */ + if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) { + cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F); + opj_marker_info_t *new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t)); + if (! new_marker) { + opj_free(cstr_index->marker); + cstr_index->marker = NULL; + cstr_index->maxmarknum = 0; + cstr_index->marknum = 0; + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */ + return OPJ_FALSE; + } + cstr_index->marker = new_marker; + } + + /* add the marker */ + cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type; + cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos; + cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len; + cstr_index->marknum++; + return OPJ_TRUE; +} + +static opj_bool opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) +{ + assert(cstr_index != 00); + assert(cstr_index->tile_index != 00); + + /* expand the list? */ + if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) { + cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F); + opj_marker_info_t *new_marker = (opj_marker_info_t *) opj_realloc( + cstr_index->tile_index[tileno].marker, + cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t)); + if (! new_marker) { + opj_free(cstr_index->tile_index[tileno].marker); + cstr_index->tile_index[tileno].marker = NULL; + cstr_index->tile_index[tileno].maxmarknum = 0; + cstr_index->tile_index[tileno].marknum = 0; + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */ + return OPJ_FALSE; + } + cstr_index->tile_index[tileno].marker = new_marker; + } + + /* add the marker */ + cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type; + cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos; + cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len; + cstr_index->tile_index[tileno].marknum++; + + if (type == J2K_MS_SOT) { + OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno; - } + if (cstr_index->tile_index[tileno].tp_index) + cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos; + + } + return OPJ_TRUE; } @@ -6352,55 +6463,55 @@ opj_bool opj_j2k_end_decompress(opj_j2k_v2_t *p_j2k, * @return true if the box is valid. */ opj_bool opj_j2k_read_header( opj_stream_private_t *p_stream, - opj_j2k_v2_t* p_j2k, - opj_image_t** p_image, - opj_event_mgr_t* p_manager ) -{ - /* preconditions */ - assert(p_j2k != 00); - assert(p_stream != 00); - assert(p_manager != 00); - - /* create an empty image header */ - p_j2k->m_private_image = opj_image_create0(); - if (! p_j2k->m_private_image) { - return OPJ_FALSE; - } - - /* customization of the validation */ - opj_j2k_setup_decoding_validation(p_j2k); - - /* validation of the parameters codec */ - if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) { - opj_image_destroy(p_j2k->m_private_image); - p_j2k->m_private_image = NULL; - return OPJ_FALSE; - } - - /* customization of the encoding */ - opj_j2k_setup_header_reading(p_j2k); - - /* read header */ - if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { - opj_image_destroy(p_j2k->m_private_image); - p_j2k->m_private_image = NULL; - return OPJ_FALSE; - } - - *p_image = opj_image_create0(); - if (! (*p_image)) { - return OPJ_FALSE; - } - - /* Copy codestream image information to the output image */ - opj_copy_image_header(p_j2k->m_private_image, *p_image); + opj_j2k_v2_t* p_j2k, + opj_image_t** p_image, + opj_event_mgr_t* p_manager ) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + /* create an empty image header */ + p_j2k->m_private_image = opj_image_create0(); + if (! p_j2k->m_private_image) { + return OPJ_FALSE; + } + + /* customization of the validation */ + opj_j2k_setup_decoding_validation(p_j2k); + + /* validation of the parameters codec */ + if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } + + /* customization of the encoding */ + opj_j2k_setup_header_reading(p_j2k); + + /* read header */ + if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } + + *p_image = opj_image_create0(); + if (! (*p_image)) { + return OPJ_FALSE; + } + + /* Copy codestream image information to the output image */ + opj_copy_image_header(p_j2k->m_private_image, *p_image); /*Allocate and initialize some elements of codestrem index*/ - if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){ - return OPJ_FALSE; - } + if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){ + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** @@ -6408,13 +6519,13 @@ opj_bool opj_j2k_read_header( opj_stream_private_t *p_stream, */ void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k) { - /* preconditions*/ - assert(p_j2k != 00); + /* preconditions*/ + assert(p_j2k != 00); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure); - /* DEVELOPER CORNER, add your custom procedures */ - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd); + /* DEVELOPER CORNER, add your custom procedures */ + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd); } @@ -6424,12 +6535,12 @@ void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k) */ void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k) { - /* preconditions*/ - assert(p_j2k != 00); + /* preconditions*/ + assert(p_j2k != 00); - opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder); - opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation); - /* DEVELOPER CORNER, add your custom validation procedure */ + opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder); + opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation); + /* DEVELOPER CORNER, add your custom validation procedure */ } @@ -6437,259 +6548,275 @@ void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k) /** * The mct encoding validation procedure. * - * @param p_j2k the jpeg2000 codec to validate. - * @param p_stream the input stream to validate. - * @param p_manager the user event manager. + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. * * @return true if the parameters are correct. */ -opj_bool opj_j2k_mct_validation ( opj_j2k_v2_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - opj_bool l_is_valid = OPJ_TRUE; - OPJ_UINT32 i,j; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_stream != 00); - assert(p_manager != 00); - - if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) { - OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; - opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps; - - for (i=0;imct == 2) { - opj_tccp_t * l_tccp = l_tcp->tccps; - l_is_valid &= (l_tcp->m_mct_coding_matrix != 00); - - for (j=0;jm_private_image->numcomps;++j) { - l_is_valid &= ! (l_tccp->qmfbid & 1); - ++l_tccp; - } - } - ++l_tcp; - } - } +opj_bool opj_j2k_mct_validation ( opj_j2k_v2_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + opj_bool l_is_valid = OPJ_TRUE; + OPJ_UINT32 i,j; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) { + OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps; + + for (i=0;imct == 2) { + opj_tccp_t * l_tccp = l_tcp->tccps; + l_is_valid &= (l_tcp->m_mct_coding_matrix != 00); + + for (j=0;jm_private_image->numcomps;++j) { + l_is_valid &= ! (l_tccp->qmfbid & 1); + ++l_tccp; + } + } + ++l_tcp; + } + } - return l_is_valid; + return l_is_valid; } opj_bool opj_j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image) { - OPJ_UINT32 i; - OPJ_UINT32 l_indix = 1; - opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00; - opj_simple_mcc_decorrelation_data_t * l_mcc_data; - OPJ_UINT32 l_mct_size,l_nb_elem; - OPJ_FLOAT32 * l_data, * l_current_data; - opj_tccp_t * l_tccp; - - // preconditions - assert(p_tcp != 00); - - if (p_tcp->mct != 2) { - return OPJ_TRUE; - } - - if (p_tcp->m_mct_decoding_matrix) { - if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { - p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS; - - p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); - if (! p_tcp->m_mct_records) { - return OPJ_FALSE; - } - l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; - - memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); - } - l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; - - if (l_mct_deco_data->m_data) { - opj_free(l_mct_deco_data->m_data); - l_mct_deco_data->m_data = 00; - } - - l_mct_deco_data->m_index = l_indix++; - l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION; - l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT; - l_nb_elem = p_image->numcomps * p_image->numcomps; - l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type]; - l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size ); - - if (! l_mct_deco_data->m_data) { - return OPJ_FALSE; - } - - j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem); - - l_mct_deco_data->m_data_size = l_mct_size; - ++p_tcp->m_nb_mct_records; - } + OPJ_UINT32 i; + OPJ_UINT32 l_indix = 1; + opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00; + opj_simple_mcc_decorrelation_data_t * l_mcc_data; + OPJ_UINT32 l_mct_size,l_nb_elem; + OPJ_FLOAT32 * l_data, * l_current_data; + opj_tccp_t * l_tccp; - if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { - p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS; - p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); + // preconditions + assert(p_tcp != 00); - if (! p_tcp->m_mct_records) { - return OPJ_FALSE; - } - - l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; - memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); + if (p_tcp->mct != 2) { + return OPJ_TRUE; + } - if (l_mct_deco_data) { - l_mct_deco_data = l_mct_offset_data - 1; - } - } + if (p_tcp->m_mct_decoding_matrix) { + if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { + p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS; + + opj_mct_data_t *new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); + if (! new_mct_records) { + opj_free(p_tcp->m_mct_records); + p_tcp->m_mct_records = NULL; + p_tcp->m_nb_max_mct_records = 0; + p_tcp->m_nb_mct_records = 0; + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ + return OPJ_FALSE; + } + p_tcp->m_mct_records = new_mct_records; + l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + + memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); + } + l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + + if (l_mct_deco_data->m_data) { + opj_free(l_mct_deco_data->m_data); + l_mct_deco_data->m_data = 00; + } + + l_mct_deco_data->m_index = l_indix++; + l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION; + l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT; + l_nb_elem = p_image->numcomps * p_image->numcomps; + l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type]; + l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size ); + + if (! l_mct_deco_data->m_data) { + return OPJ_FALSE; + } + + j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem); + + l_mct_deco_data->m_data_size = l_mct_size; + ++p_tcp->m_nb_mct_records; + } - l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { + p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS; + opj_mct_data_t *new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); + if (! new_mct_records) { + opj_free(p_tcp->m_mct_records); + p_tcp->m_mct_records = NULL; + p_tcp->m_nb_max_mct_records = 0; + p_tcp->m_nb_mct_records = 0; + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ + return OPJ_FALSE; + } + p_tcp->m_mct_records = new_mct_records; + l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + + memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); + + if (l_mct_deco_data) { + l_mct_deco_data = l_mct_offset_data - 1; + } + } - if (l_mct_offset_data->m_data) { - opj_free(l_mct_offset_data->m_data); - l_mct_offset_data->m_data = 00; - } + l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; - l_mct_offset_data->m_index = l_indix++; - l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET; - l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT; - l_nb_elem = p_image->numcomps; - l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type]; - l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size ); + if (l_mct_offset_data->m_data) { + opj_free(l_mct_offset_data->m_data); + l_mct_offset_data->m_data = 00; + } - if (! l_mct_offset_data->m_data) { - return OPJ_FALSE; - } + l_mct_offset_data->m_index = l_indix++; + l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET; + l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT; + l_nb_elem = p_image->numcomps; + l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type]; + l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size ); - l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32)); - if (! l_data) { - opj_free(l_mct_offset_data->m_data); - l_mct_offset_data->m_data = 00; - return OPJ_FALSE; - } + if (! l_mct_offset_data->m_data) { + return OPJ_FALSE; + } - l_tccp = p_tcp->tccps; - l_current_data = l_data; + l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32)); + if (! l_data) { + opj_free(l_mct_offset_data->m_data); + l_mct_offset_data->m_data = 00; + return OPJ_FALSE; + } - for (i=0;im_dc_level_shift); - ++l_tccp; - } + l_tccp = p_tcp->tccps; + l_current_data = l_data; - j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem); + for (i=0;im_dc_level_shift); + ++l_tccp; + } - opj_free(l_data); + j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem); - l_mct_offset_data->m_data_size = l_mct_size; + opj_free(l_data); - ++p_tcp->m_nb_mct_records; + l_mct_offset_data->m_data_size = l_mct_size; - if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) { - p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS; - p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) - opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t)); + ++p_tcp->m_nb_mct_records; - if (! p_tcp->m_mcc_records) { - return OPJ_FALSE; - } - l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; - memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t)); + if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) { + p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS; + opj_simple_mcc_decorrelation_data_t *new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( + p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t)); + if (! new_mcc_records) { + opj_free(p_tcp->m_mcc_records); + p_tcp->m_mcc_records = NULL; + p_tcp->m_nb_max_mcc_records = 0; + p_tcp->m_nb_mcc_records = 0; + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ + return OPJ_FALSE; + } + p_tcp->m_mcc_records = new_mcc_records; + l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; + memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t)); - } + } - l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; - l_mcc_data->m_decorrelation_array = l_mct_deco_data; - l_mcc_data->m_is_irreversible = 1; - l_mcc_data->m_nb_comps = p_image->numcomps; - l_mcc_data->m_index = l_indix++; - l_mcc_data->m_offset_array = l_mct_offset_data; - ++p_tcp->m_nb_mcc_records; + l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; + l_mcc_data->m_decorrelation_array = l_mct_deco_data; + l_mcc_data->m_is_irreversible = 1; + l_mcc_data->m_nb_comps = p_image->numcomps; + l_mcc_data->m_index = l_indix++; + l_mcc_data->m_offset_array = l_mct_offset_data; + ++p_tcp->m_nb_mcc_records; - return OPJ_TRUE; + return OPJ_TRUE; } /** * Builds the cp decoder parameters to use to decode tile. */ opj_bool opj_j2k_build_decoder (opj_j2k_v2_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - /* add here initialization of cp - copy paste of setup_decoder */ + /* add here initialization of cp + copy paste of setup_decoder */ (void)p_j2k; (void)p_stream; (void)p_manager; - return OPJ_TRUE; + return OPJ_TRUE; } /** * Builds the cp encoder parameters to use to encode tile. */ opj_bool opj_j2k_build_encoder (opj_j2k_v2_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - /* add here initialization of cp - copy paste of setup_encoder */ + /* add here initialization of cp + copy paste of setup_encoder */ (void)p_j2k; (void)p_stream; (void)p_manager; - return OPJ_TRUE; + return OPJ_TRUE; } /** * The default encoding validation procedure without any extension. * - * @param p_j2k the jpeg2000 codec to validate. - * @param p_stream the input stream to validate. - * @param p_manager the user event manager. + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. * * @return true if the parameters are correct. */ -opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - opj_bool l_is_valid = OPJ_TRUE; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_stream != 00); - assert(p_manager != 00); - - /* STATE checking */ - /* make sure the state is at 0 */ - l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE); - - /* POINTER validation */ - /* make sure a p_j2k codec is present */ - l_is_valid &= (p_j2k->m_procedure_list != 00); - /* make sure a validation list is present */ - l_is_valid &= (p_j2k->m_validation_list != 00); - - if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); - return OPJ_FALSE; - } +opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + opj_bool l_is_valid = OPJ_TRUE; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + /* STATE checking */ + /* make sure the state is at 0 */ + l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE); + + /* POINTER validation */ + /* make sure a p_j2k codec is present */ + l_is_valid &= (p_j2k->m_procedure_list != 00); + /* make sure a validation list is present */ + l_is_valid &= (p_j2k->m_validation_list != 00); + + if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); + return OPJ_FALSE; + } - if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); - return OPJ_FALSE; - } + if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); + return OPJ_FALSE; + } - /* PARAMETER VALIDATION */ - return l_is_valid; + /* PARAMETER VALIDATION */ + return l_is_valid; } /** * The default decoding validation procedure without any extension. * - * @param p_j2k the jpeg2000 codec to validate. - * @param p_stream the input stream to validate. - * @param p_manager the user event manager. + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. * * @return true if the parameters are correct. */ @@ -6698,446 +6825,454 @@ opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t *p_j2k, opj_event_mgr_t * p_manager ) { - opj_bool l_is_valid = OPJ_TRUE; + opj_bool l_is_valid = OPJ_TRUE; - /* preconditions*/ - assert(p_j2k != 00); - assert(p_stream != 00); - assert(p_manager != 00); + /* preconditions*/ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); - /* STATE checking */ - /* make sure the state is at 0 */ + /* STATE checking */ + /* make sure the state is at 0 */ #ifdef TODO_MSD - l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE); + l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE); #endif - l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000); - - /* POINTER validation */ - /* make sure a p_j2k codec is present */ - /* make sure a procedure list is present */ - l_is_valid &= (p_j2k->m_procedure_list != 00); - /* make sure a validation list is present */ - l_is_valid &= (p_j2k->m_validation_list != 00); - - /* PARAMETER VALIDATION */ - return l_is_valid; -} - -opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager) -{ - OPJ_UINT32 l_current_marker; - OPJ_UINT32 l_marker_size; - const opj_dec_memory_marker_handler_t * l_marker_handler = 00; - - /* preconditions */ - assert(p_stream != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - /* We enter in the main header */ - p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; - - /* Try to read the SOC marker, the codestream must begin with SOC marker */ - if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n"); - return OPJ_FALSE; - } - - /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ - if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - - /* Read 2 bytes as the new marker ID */ - opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); - - /* Try to read until the SOT is detected */ - while (l_current_marker != J2K_MS_SOT) { - - /* Check if the current marker ID is valid */ - if (l_current_marker < 0xff00) { - opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker); - return OPJ_FALSE; - } - - /* Get the marker handler from the marker ID */ - l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); - - /* Manage case where marker is unknown */ - if (l_marker_handler->id == J2K_MS_UNK) { - if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){ - opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n"); - return OPJ_FALSE; - } - - if (l_current_marker == J2K_MS_SOT) - break; /* SOT marker is detected main header is completely read */ - else /* Get the marker handler from the marker ID */ - l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); - } - - /* Check if the marker is known and if it is the right place to find it */ - if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); - return OPJ_FALSE; - } - - /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ - if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - - /* read 2 bytes as the marker size */ - opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2); - l_marker_size -= 2; /* Subtract the size of the marker ID already read */ - - /* Check if the marker size is compatible with the header data size */ - if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { - p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*) - opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size); - if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) { - return OPJ_FALSE; - } - p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; - } - - /* Try to read the rest of the marker segment from stream and copy them into the buffer */ - if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - - /* Read the marker segment with the correct marker handler */ - if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n"); - return OPJ_FALSE; - } - - /* Add the marker to the codestream index*/ - opj_j2k_add_mhmarker(p_j2k->cstr_index, - l_marker_handler->id, - (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, - l_marker_size + 4 ); - - /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ - if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - - /* read 2 bytes as the new marker ID */ - opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); - } - - opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n"); - - /* Position of the last element if the main header */ - p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2; - - /* Next step: read a tile-part header */ - p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; - - return OPJ_TRUE; + l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000); + + /* POINTER validation */ + /* make sure a p_j2k codec is present */ + /* make sure a procedure list is present */ + l_is_valid &= (p_j2k->m_procedure_list != 00); + /* make sure a validation list is present */ + l_is_valid &= (p_j2k->m_validation_list != 00); + + /* PARAMETER VALIDATION */ + return l_is_valid; } -/** - * Excutes the given procedures on the given codec. - * - * @param p_procedure_list the list of procedures to execute - * @param p_j2k the jpeg2000 codec to execute the procedures on. - * @param p_stream the stream to execute the procedures on. - * @param p_manager the user manager. - * - * @return true if all the procedures were successfully executed. - */ -opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k, - opj_procedure_list_t * p_procedure_list, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) { - opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00; - opj_bool l_result = OPJ_TRUE; - OPJ_UINT32 l_nb_proc, i; + OPJ_UINT32 l_current_marker; + OPJ_UINT32 l_marker_size; + const opj_dec_memory_marker_handler_t * l_marker_handler = 00; - /* preconditions*/ - assert(p_procedure_list != 00); - assert(p_j2k != 00); - assert(p_stream != 00); - assert(p_manager != 00); + /* preconditions */ + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + /* We enter in the main header */ + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; - l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); - l_procedure = (opj_bool (**) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); + /* Try to read the SOC marker, the codestream must begin with SOC marker */ + if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n"); + return OPJ_FALSE; + } + + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read 2 bytes as the new marker ID */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); + + /* Try to read until the SOT is detected */ + while (l_current_marker != J2K_MS_SOT) { + + /* Check if the current marker ID is valid */ + if (l_current_marker < 0xff00) { + opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker); + return OPJ_FALSE; + } + + /* Get the marker handler from the marker ID */ + l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); + + /* Manage case where marker is unknown */ + if (l_marker_handler->id == J2K_MS_UNK) { + if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){ + opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n"); + return OPJ_FALSE; + } + + if (l_current_marker == J2K_MS_SOT) + break; /* SOT marker is detected main header is completely read */ + else /* Get the marker handler from the marker ID */ + l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); + } + + /* Check if the marker is known and if it is the right place to find it */ + if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); + return OPJ_FALSE; + } + + /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* read 2 bytes as the marker size */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2); + l_marker_size -= 2; /* Subtract the size of the marker ID already read */ + + /* Check if the marker size is compatible with the header data size */ + if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { + OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); + if (! new_header_data) { + opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); + p_j2k->m_specific_param.m_decoder.m_header_data = NULL; + p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read header\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; + p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; + } + + /* Try to read the rest of the marker segment from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read the marker segment with the correct marker handler */ + if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n"); + return OPJ_FALSE; + } + + /* Add the marker to the codestream index*/ + if (OPJ_FALSE == opj_j2k_add_mhmarker( + p_j2k->cstr_index, + l_marker_handler->id, + (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, + l_marker_size + 4 )) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); + return OPJ_FALSE; + } + + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* read 2 bytes as the new marker ID */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); + } + + opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n"); - for (i=0;icstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2; - /* and clear the procedure list at the end.*/ - opj_procedure_list_clear(p_procedure_list); - return l_result; + /* Next step: read a tile-part header */ + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; + + return OPJ_TRUE; +} + +/** + * Excutes the given procedures on the given codec. + * + * @param p_procedure_list the list of procedures to execute + * @param p_j2k the jpeg2000 codec to execute the procedures on. + * @param p_stream the stream to execute the procedures on. + * @param p_manager the user manager. + * + * @return true if all the procedures were successfully executed. + */ +opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k, + opj_procedure_list_t * p_procedure_list, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00; + opj_bool l_result = OPJ_TRUE; + OPJ_UINT32 l_nb_proc, i; + + /* preconditions*/ + assert(p_procedure_list != 00); + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + + l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); + l_procedure = (opj_bool (**) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); + + for (i=0;im_private_image; - l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; - l_tcp = p_j2k->m_cp.tcps; - l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t); - l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp; - l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32); - - /* For each tile */ - for (i=0; itccps; - /*Copy default coding parameters into the current tile coding parameters*/ - memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t)); - /* Initialize some values of the current tile coding parameters*/ - l_tcp->ppt = 0; - l_tcp->ppt_data = 00; - /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/ - l_tcp->tccps = l_current_tccp; - - /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/ - if (l_default_tcp->m_mct_decoding_matrix) { - l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); - if (! l_tcp->m_mct_decoding_matrix ) { - return OPJ_FALSE; - } - memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size); - } - - /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/ - l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t); - l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size); - if (! l_tcp->m_mct_records) { - return OPJ_FALSE; - } - memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size); - - /* Copy the mct record data from dflt_tile_cp to the current tile*/ - l_src_mct_rec = l_default_tcp->m_mct_records; - l_dest_mct_rec = l_tcp->m_mct_records; - - for (j=0;jm_nb_mct_records;++j) { - - if (l_src_mct_rec->m_data) { - - l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size); - if(! l_dest_mct_rec->m_data) { - return OPJ_FALSE; - } - memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size); - } - - ++l_src_mct_rec; - ++l_dest_mct_rec; - } - - /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/ - l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t); - l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size); - if (! l_tcp->m_mcc_records) { - return OPJ_FALSE; - } - memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size); - - /* Copy the mcc record data from dflt_tile_cp to the current tile*/ - l_src_mcc_rec = l_default_tcp->m_mcc_records; - l_dest_mcc_rec = l_tcp->m_mcc_records; - - for (j=0;jm_nb_max_mcc_records;++j) { - - if (l_src_mcc_rec->m_decorrelation_array) { - l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records; - l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset; - } - - if (l_src_mcc_rec->m_offset_array) { - l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records; - l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset; - } - - ++l_src_mcc_rec; - ++l_dest_mcc_rec; - } - - /* Copy all the dflt_tile_compo_cp to the current tile cp */ - memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size); - - /* Move to next tile cp*/ - ++l_tcp; - } - - /* Create the current tile decoder*/ - p_j2k->m_tcd = (opj_tcd_v2_t*)opj_tcd_create(OPJ_TRUE); /* FIXME why a cast ? */ - if (! p_j2k->m_tcd ) { - return OPJ_FALSE; - } - - if ( !opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) { - opj_tcd_destroy(p_j2k->m_tcd); - p_j2k->m_tcd = 00; - opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); - return OPJ_FALSE; - } - - return OPJ_TRUE; + opj_tcp_v2_t * l_tcp = 00; + opj_tcp_v2_t * l_default_tcp = 00; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 i,j; + opj_tccp_t *l_current_tccp = 00; + OPJ_UINT32 l_tccp_size; + OPJ_UINT32 l_mct_size; + opj_image_t * l_image; + OPJ_UINT32 l_mcc_records_size,l_mct_records_size; + opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec; + opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec; + OPJ_UINT32 l_offset; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_private_image; + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + l_tcp = p_j2k->m_cp.tcps; + l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t); + l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32); + + /* For each tile */ + for (i=0; itccps; + /*Copy default coding parameters into the current tile coding parameters*/ + memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t)); + /* Initialize some values of the current tile coding parameters*/ + l_tcp->ppt = 0; + l_tcp->ppt_data = 00; + /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/ + l_tcp->tccps = l_current_tccp; + + /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/ + if (l_default_tcp->m_mct_decoding_matrix) { + l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); + if (! l_tcp->m_mct_decoding_matrix ) { + return OPJ_FALSE; + } + memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size); + } + + /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/ + l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t); + l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size); + if (! l_tcp->m_mct_records) { + return OPJ_FALSE; + } + memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size); + + /* Copy the mct record data from dflt_tile_cp to the current tile*/ + l_src_mct_rec = l_default_tcp->m_mct_records; + l_dest_mct_rec = l_tcp->m_mct_records; + + for (j=0;jm_nb_mct_records;++j) { + + if (l_src_mct_rec->m_data) { + + l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size); + if(! l_dest_mct_rec->m_data) { + return OPJ_FALSE; + } + memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size); + } + + ++l_src_mct_rec; + ++l_dest_mct_rec; + } + + /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/ + l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t); + l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size); + if (! l_tcp->m_mcc_records) { + return OPJ_FALSE; + } + memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size); + + /* Copy the mcc record data from dflt_tile_cp to the current tile*/ + l_src_mcc_rec = l_default_tcp->m_mcc_records; + l_dest_mcc_rec = l_tcp->m_mcc_records; + + for (j=0;jm_nb_max_mcc_records;++j) { + + if (l_src_mcc_rec->m_decorrelation_array) { + l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records; + l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset; + } + + if (l_src_mcc_rec->m_offset_array) { + l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records; + l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset; + } + + ++l_src_mcc_rec; + ++l_dest_mcc_rec; + } + + /* Copy all the dflt_tile_compo_cp to the current tile cp */ + memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size); + + /* Move to next tile cp*/ + ++l_tcp; + } + + /* Create the current tile decoder*/ + p_j2k->m_tcd = (opj_tcd_v2_t*)opj_tcd_create(OPJ_TRUE); /* FIXME why a cast ? */ + if (! p_j2k->m_tcd ) { + return OPJ_FALSE; + } + + if ( !opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) { + opj_tcd_destroy(p_j2k->m_tcd); + p_j2k->m_tcd = 00; + opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return OPJ_FALSE; + } + + return OPJ_TRUE; } /** * Reads the lookup table containing all the marker, status and action, and returns the handler associated * with the marker value. - * @param p_id Marker value to look up + * @param p_id Marker value to look up * - * @return the handler associated with the id. + * @return the handler associated with the id. */ const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id) { - const opj_dec_memory_marker_handler_t *e; - for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) { - if (e->id == p_id) { - break; /* we find a handler corresponding to the marker ID*/ - } - } - return e; + const opj_dec_memory_marker_handler_t *e; + for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) { + if (e->id == p_id) { + break; /* we find a handler corresponding to the marker ID*/ + } + } + return e; } /** * Destroys a jpeg2000 codec. * - * @param p_j2k the jpeg20000 structure to destroy. + * @param p_j2k the jpeg20000 structure to destroy. */ void opj_j2k_destroy (opj_j2k_v2_t *p_j2k) { - if (p_j2k == 00) { - return; - } - - if (p_j2k->m_is_decoder) { - - if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) { - opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp); - opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp); - p_j2k->m_specific_param.m_decoder.m_default_tcp = 00; - } - - if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) { - opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); - p_j2k->m_specific_param.m_decoder.m_header_data = 00; - p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; - } - } - else { + if (p_j2k == 00) { + return; + } - if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { - opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); - p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00; - } + if (p_j2k->m_is_decoder) { - if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { - opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); - p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00; - p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00; - } + if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) { + opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp); + opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp); + p_j2k->m_specific_param.m_decoder.m_default_tcp = 00; + } - if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); - p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00; - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; - } - } + if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) { + opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); + p_j2k->m_specific_param.m_decoder.m_header_data = 00; + p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; + } + } + else { + + if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00; + } + + if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { + opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00; + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00; + } + + if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + } + } - opj_tcd_destroy(p_j2k->m_tcd); + opj_tcd_destroy(p_j2k->m_tcd); - opj_j2k_cp_destroy(&(p_j2k->m_cp)); - memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t)); + opj_j2k_cp_destroy(&(p_j2k->m_cp)); + memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t)); - opj_procedure_list_destroy(p_j2k->m_procedure_list); - p_j2k->m_procedure_list = 00; + opj_procedure_list_destroy(p_j2k->m_procedure_list); + p_j2k->m_procedure_list = 00; - opj_procedure_list_destroy(p_j2k->m_validation_list); - p_j2k->m_procedure_list = 00; + opj_procedure_list_destroy(p_j2k->m_validation_list); + p_j2k->m_procedure_list = 00; - j2k_destroy_cstr_index(p_j2k->cstr_index); - p_j2k->cstr_index = NULL; + j2k_destroy_cstr_index(p_j2k->cstr_index); + p_j2k->cstr_index = NULL; - opj_image_destroy(p_j2k->m_private_image); - p_j2k->m_private_image = NULL; + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; - opj_image_destroy(p_j2k->m_output_image); - p_j2k->m_output_image = NULL; + opj_image_destroy(p_j2k->m_output_image); + p_j2k->m_output_image = NULL; - opj_free(p_j2k); + opj_free(p_j2k); } void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind) { - if (p_cstr_ind) { + if (p_cstr_ind) { - if (p_cstr_ind->marker) { - opj_free(p_cstr_ind->marker); - p_cstr_ind->marker = NULL; - } + if (p_cstr_ind->marker) { + opj_free(p_cstr_ind->marker); + p_cstr_ind->marker = NULL; + } - if (p_cstr_ind->tile_index) { - OPJ_UINT32 it_tile = 0; + if (p_cstr_ind->tile_index) { + OPJ_UINT32 it_tile = 0; - for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) { + for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) { - if(p_cstr_ind->tile_index[it_tile].packet_index) { - opj_free(p_cstr_ind->tile_index[it_tile].packet_index); - p_cstr_ind->tile_index[it_tile].packet_index = NULL; - } + if(p_cstr_ind->tile_index[it_tile].packet_index) { + opj_free(p_cstr_ind->tile_index[it_tile].packet_index); + p_cstr_ind->tile_index[it_tile].packet_index = NULL; + } - if(p_cstr_ind->tile_index[it_tile].tp_index){ - opj_free(p_cstr_ind->tile_index[it_tile].tp_index); - p_cstr_ind->tile_index[it_tile].tp_index = NULL; - } + if(p_cstr_ind->tile_index[it_tile].tp_index){ + opj_free(p_cstr_ind->tile_index[it_tile].tp_index); + p_cstr_ind->tile_index[it_tile].tp_index = NULL; + } - if(p_cstr_ind->tile_index[it_tile].marker){ - opj_free(p_cstr_ind->tile_index[it_tile].marker); - p_cstr_ind->tile_index[it_tile].marker = NULL; + if(p_cstr_ind->tile_index[it_tile].marker){ + opj_free(p_cstr_ind->tile_index[it_tile].marker); + p_cstr_ind->tile_index[it_tile].marker = NULL; - } - } + } + } - opj_free( p_cstr_ind->tile_index); - p_cstr_ind->tile_index = NULL; - } + opj_free( p_cstr_ind->tile_index); + p_cstr_ind->tile_index = NULL; + } - opj_free(p_cstr_ind); - } + opj_free(p_cstr_ind); + } } @@ -7145,847 +7280,854 @@ void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind) /** * Destroys a tile coding parameter structure. * - * @param p_tcp the tile coding parameter to destroy. + * @param p_tcp the tile coding parameter to destroy. */ void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp) { - if (p_tcp == 00) { - return; - } + if (p_tcp == 00) { + return; + } - if (p_tcp->ppt_buffer != 00) { - opj_free(p_tcp->ppt_buffer); - p_tcp->ppt_buffer = 00; - } + if (p_tcp->ppt_buffer != 00) { + opj_free(p_tcp->ppt_buffer); + p_tcp->ppt_buffer = 00; + } - if (p_tcp->tccps != 00) { - opj_free(p_tcp->tccps); - p_tcp->tccps = 00; - } + if (p_tcp->tccps != 00) { + opj_free(p_tcp->tccps); + p_tcp->tccps = 00; + } - if (p_tcp->m_mct_coding_matrix != 00) { - opj_free(p_tcp->m_mct_coding_matrix); - p_tcp->m_mct_coding_matrix = 00; - } + if (p_tcp->m_mct_coding_matrix != 00) { + opj_free(p_tcp->m_mct_coding_matrix); + p_tcp->m_mct_coding_matrix = 00; + } - if (p_tcp->m_mct_decoding_matrix != 00) { - opj_free(p_tcp->m_mct_decoding_matrix); - p_tcp->m_mct_decoding_matrix = 00; - } + if (p_tcp->m_mct_decoding_matrix != 00) { + opj_free(p_tcp->m_mct_decoding_matrix); + p_tcp->m_mct_decoding_matrix = 00; + } - if (p_tcp->m_mcc_records) { - opj_free(p_tcp->m_mcc_records); - p_tcp->m_mcc_records = 00; - p_tcp->m_nb_max_mcc_records = 0; - p_tcp->m_nb_mcc_records = 0; - } + if (p_tcp->m_mcc_records) { + opj_free(p_tcp->m_mcc_records); + p_tcp->m_mcc_records = 00; + p_tcp->m_nb_max_mcc_records = 0; + p_tcp->m_nb_mcc_records = 0; + } - if (p_tcp->m_mct_records) { - opj_mct_data_t * l_mct_data = p_tcp->m_mct_records; - OPJ_UINT32 i; + if (p_tcp->m_mct_records) { + opj_mct_data_t * l_mct_data = p_tcp->m_mct_records; + OPJ_UINT32 i; - for (i=0;im_nb_mct_records;++i) { - if (l_mct_data->m_data) { - opj_free(l_mct_data->m_data); - l_mct_data->m_data = 00; - } + for (i=0;im_nb_mct_records;++i) { + if (l_mct_data->m_data) { + opj_free(l_mct_data->m_data); + l_mct_data->m_data = 00; + } - ++l_mct_data; - } + ++l_mct_data; + } - opj_free(p_tcp->m_mct_records); - p_tcp->m_mct_records = 00; - } + opj_free(p_tcp->m_mct_records); + p_tcp->m_mct_records = 00; + } - if (p_tcp->mct_norms != 00) { - opj_free(p_tcp->mct_norms); - p_tcp->mct_norms = 00; - } + if (p_tcp->mct_norms != 00) { + opj_free(p_tcp->mct_norms); + p_tcp->mct_norms = 00; + } - opj_j2k_tcp_data_destroy(p_tcp); + opj_j2k_tcp_data_destroy(p_tcp); } /** * Destroys the data inside a tile coding parameter structure. * - * @param p_tcp the tile coding parameter which contain data to destroy. + * @param p_tcp the tile coding parameter which contain data to destroy. */ void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp) { - if (p_tcp->m_data) { - opj_free(p_tcp->m_data); - p_tcp->m_data = NULL; - p_tcp->m_data_size = 0; - } + if (p_tcp->m_data) { + opj_free(p_tcp->m_data); + p_tcp->m_data = NULL; + p_tcp->m_data_size = 0; + } } /** * Destroys a coding parameter structure. * - * @param p_cp the coding parameter to destroy. + * @param p_cp the coding parameter to destroy. */ void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp) { - OPJ_UINT32 l_nb_tiles; - opj_tcp_v2_t * l_current_tile = 00; - OPJ_UINT32 i; - - if - (p_cp == 00) - { - return; - } - if - (p_cp->tcps != 00) - { - l_current_tile = p_cp->tcps; - l_nb_tiles = p_cp->th * p_cp->tw; - - for - (i = 0; i < l_nb_tiles; ++i) - { - opj_j2k_tcp_destroy(l_current_tile); - ++l_current_tile; - } - opj_free(p_cp->tcps); - p_cp->tcps = 00; - } - if - (p_cp->ppm_buffer != 00) - { - opj_free(p_cp->ppm_buffer); - p_cp->ppm_buffer = 00; - } - if - (p_cp->comment != 00) - { - opj_free(p_cp->comment); - p_cp->comment = 00; - } - if - (! p_cp->m_is_decoder) - { - if - (p_cp->m_specific_param.m_enc.m_matrice) - { - opj_free(p_cp->m_specific_param.m_enc.m_matrice); - p_cp->m_specific_param.m_enc.m_matrice = 00; - } - } + OPJ_UINT32 l_nb_tiles; + opj_tcp_v2_t * l_current_tile = 00; + OPJ_UINT32 i; + + if + (p_cp == 00) + { + return; + } + if + (p_cp->tcps != 00) + { + l_current_tile = p_cp->tcps; + l_nb_tiles = p_cp->th * p_cp->tw; + + for + (i = 0; i < l_nb_tiles; ++i) + { + opj_j2k_tcp_destroy(l_current_tile); + ++l_current_tile; + } + opj_free(p_cp->tcps); + p_cp->tcps = 00; + } + if + (p_cp->ppm_buffer != 00) + { + opj_free(p_cp->ppm_buffer); + p_cp->ppm_buffer = 00; + } + if + (p_cp->comment != 00) + { + opj_free(p_cp->comment); + p_cp->comment = 00; + } + if + (! p_cp->m_is_decoder) + { + if + (p_cp->m_specific_param.m_enc.m_matrice) + { + opj_free(p_cp->m_specific_param.m_enc.m_matrice); + p_cp->m_specific_param.m_enc.m_matrice = 00; + } + } } /** * Reads a tile header. - * @param p_j2k the jpeg2000 codec. - * @param p_stream the stream to write data to. - * @param p_manager the user event manager. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_read_tile_header( opj_j2k_v2_t * p_j2k, - 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_go_on, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - OPJ_UINT32 l_current_marker = J2K_MS_SOT; - OPJ_UINT32 l_marker_size; - const opj_dec_memory_marker_handler_t * l_marker_handler = 00; - opj_tcp_v2_t * l_tcp = NULL; - OPJ_UINT32 l_nb_tiles; - - /* preconditions */ - assert(p_stream != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - /* Reach the End Of Codestream ?*/ - if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){ - l_current_marker = J2K_MS_EOC; - } - /* We need to encounter a SOT marker (a new tile-part header) */ - else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){ - return OPJ_FALSE; - } - - /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */ - while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) { - - /* Try to read until the Start Of Data is detected */ - while (l_current_marker != J2K_MS_SOD) { - - /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ - if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - - /* Read 2 bytes from the buffer as the marker size */ - opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2); - - /* Why this condition? FIXME */ - if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){ - p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2); - } - l_marker_size -= 2; /* Subtract the size of the marker ID already read */ - - /* Get the marker handler from the marker ID */ - l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); - - /* Check if the marker is known and if it is the right place to find it */ - if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); - return OPJ_FALSE; - } +opj_bool opj_j2k_read_tile_header( opj_j2k_v2_t * p_j2k, + 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_go_on, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + OPJ_UINT32 l_current_marker = J2K_MS_SOT; + OPJ_UINT32 l_marker_size; + const opj_dec_memory_marker_handler_t * l_marker_handler = 00; + opj_tcp_v2_t * l_tcp = NULL; + OPJ_UINT32 l_nb_tiles; + + /* preconditions */ + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + /* Reach the End Of Codestream ?*/ + if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){ + l_current_marker = J2K_MS_EOC; + } + /* We need to encounter a SOT marker (a new tile-part header) */ + else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){ + return OPJ_FALSE; + } + + /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */ + while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) { + + /* Try to read until the Start Of Data is detected */ + while (l_current_marker != J2K_MS_SOD) { + + /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read 2 bytes from the buffer as the marker size */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2); + + /* Why this condition? FIXME */ + if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){ + p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2); + } + l_marker_size -= 2; /* Subtract the size of the marker ID already read */ + + /* Get the marker handler from the marker ID */ + l_marker_handler = opj_j2k_get_marker_handler(l_current_marker); + + /* Check if the marker is known and if it is the right place to find it */ + if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); + return OPJ_FALSE; + } /* FIXME manage case of unknown marker as in the main header ? */ - /* Check if the marker size is compatible with the header data size */ - if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { - p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*) - opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size); - if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) { - return OPJ_FALSE; - } - p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; - } - - /* Try to read the rest of the marker segment from stream and copy them into the buffer */ - if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - - /* Read the marker segment with the correct marker handler */ - if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker); - return OPJ_FALSE; - } - - /* Add the marker to the codestream index*/ - opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, - p_j2k->cstr_index, - l_marker_handler->id, - (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, - l_marker_size + 4 ); - - /* Keep the position of the last SOT marker read */ - if ( l_marker_handler->id == J2K_MS_SOT ) { - OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ; - if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) - { - p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos; - } - } - - - if (p_j2k->m_specific_param.m_decoder.m_skip_data) { - /* Skip the rest of the tile part header*/ - if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */ - } - else { - /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ - if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - /* Read 2 bytes from the buffer as the new marker ID */ - opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); - } - } - - /* If we didn't skip data before, we need to read the SOD marker*/ - if (! p_j2k->m_specific_param.m_decoder.m_skip_data) { - /* Try to read the SOD marker and skip data ? FIXME */ - if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) { - return OPJ_FALSE; - } - - - - if (! p_j2k->m_specific_param.m_decoder.m_can_decode){ - /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ - if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - - /* Read 2 bytes from buffer as the new marker ID */ - opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); - } - } - else { - /* Indicate we will try to read a new tile-part header*/ - p_j2k->m_specific_param.m_decoder.m_skip_data = 0; - p_j2k->m_specific_param.m_decoder.m_can_decode = 0; - p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; - - /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ - if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - - /* Read 2 bytes from buffer as the new marker ID */ - opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); - } - } - - /* Current marker is the EOC marker ?*/ - if (l_current_marker == J2K_MS_EOC) { - if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){ - p_j2k->m_current_tile_number = 0; - p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; - } - } - - /* FIXME DOC ???*/ - if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) { - l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; - l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; - - while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) { - ++p_j2k->m_current_tile_number; - ++l_tcp; - } - - if (p_j2k->m_current_tile_number == l_nb_tiles) { - *p_go_on = OPJ_FALSE; - return OPJ_TRUE; - } - } - - /*FIXME ???*/ - if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); - return OPJ_FALSE; - } - - opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n", - p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1); - - *p_tile_index = p_j2k->m_current_tile_number; - *p_go_on = OPJ_TRUE; - *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd); - *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0; - *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0; - *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1; - *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1; - *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps; - - p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/ - - return OPJ_TRUE; -} - - -opj_bool opj_j2k_decode_tile ( opj_j2k_v2_t * p_j2k, - OPJ_UINT32 p_tile_index, - OPJ_BYTE * p_data, - OPJ_UINT32 p_data_size, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - OPJ_UINT32 l_current_marker; - OPJ_BYTE l_data [2]; - opj_tcp_v2_t * l_tcp; - - /* preconditions */ - assert(p_stream != 00); - assert(p_j2k != 00); - assert(p_manager != 00); - - if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/) - || (p_tile_index != p_j2k->m_current_tile_number) ) { - return OPJ_FALSE; - } - - l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]); - if (! l_tcp->m_data) { - opj_j2k_tcp_destroy(l_tcp); - return OPJ_FALSE; - } - - if (! opj_tcd_decode_tile( p_j2k->m_tcd, - l_tcp->m_data, - l_tcp->m_data_size, - p_tile_index, - p_j2k->cstr_index) ) { - opj_j2k_tcp_destroy(l_tcp); - p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/ - return OPJ_FALSE; - } - - if (! opj_tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) { - return OPJ_FALSE; - } - - /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access) - * we destroy just the data which will be re-read in read_tile_header*/ - /*opj_j2k_tcp_destroy(l_tcp); - p_j2k->m_tcd->tcp = 0;*/ - opj_j2k_tcp_data_destroy(l_tcp); - - p_j2k->m_specific_param.m_decoder.m_can_decode = 0; - p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/ - - if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/ - if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); - return OPJ_FALSE; - } - - opj_read_bytes(l_data,&l_current_marker,2); - - if (l_current_marker == J2K_MS_EOC) { - p_j2k->m_current_tile_number = 0; - p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/ - } - else if (l_current_marker != J2K_MS_SOT) - { - opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n"); - return OPJ_FALSE; - } - } - - return OPJ_TRUE; + /* Check if the marker size is compatible with the header data size */ + if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) { + OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); + if (! new_header_data) { + opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); + p_j2k->m_specific_param.m_decoder.m_header_data = NULL; + p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read header\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data; + p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; + } + + /* Try to read the rest of the marker segment from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read the marker segment with the correct marker handler */ + if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker); + return OPJ_FALSE; + } + + /* Add the marker to the codestream index*/ + if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number, + p_j2k->cstr_index, + l_marker_handler->id, + (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4, + l_marker_size + 4 )) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); + return OPJ_FALSE; + } + + /* Keep the position of the last SOT marker read */ + if ( l_marker_handler->id == J2K_MS_SOT ) { + OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ; + if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) + { + p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos; + } + } + + + if (p_j2k->m_specific_param.m_decoder.m_skip_data) { + /* Skip the rest of the tile part header*/ + if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */ + } + else { + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/ + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + /* Read 2 bytes from the buffer as the new marker ID */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); + } + } + + /* If we didn't skip data before, we need to read the SOD marker*/ + if (! p_j2k->m_specific_param.m_decoder.m_skip_data) { + /* Try to read the SOD marker and skip data ? FIXME */ + if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) { + return OPJ_FALSE; + } + + + + if (! p_j2k->m_specific_param.m_decoder.m_can_decode){ + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read 2 bytes from buffer as the new marker ID */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); + } + } + else { + /* Indicate we will try to read a new tile-part header*/ + p_j2k->m_specific_param.m_decoder.m_skip_data = 0; + p_j2k->m_specific_param.m_decoder.m_can_decode = 0; + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; + + /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */ + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + /* Read 2 bytes from buffer as the new marker ID */ + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); + } + } + + /* Current marker is the EOC marker ?*/ + if (l_current_marker == J2K_MS_EOC) { + if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){ + p_j2k->m_current_tile_number = 0; + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; + } + } + + /* FIXME DOC ???*/ + if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) { + l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + + while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) { + ++p_j2k->m_current_tile_number; + ++l_tcp; + } + + if (p_j2k->m_current_tile_number == l_nb_tiles) { + *p_go_on = OPJ_FALSE; + return OPJ_TRUE; + } + } + + /*FIXME ???*/ + if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return OPJ_FALSE; + } + + opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n", + p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1); + + *p_tile_index = p_j2k->m_current_tile_number; + *p_go_on = OPJ_TRUE; + *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd); + *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0; + *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0; + *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1; + *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1; + *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps; + + p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/ + + return OPJ_TRUE; +} + + +opj_bool opj_j2k_decode_tile ( opj_j2k_v2_t * p_j2k, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + OPJ_UINT32 l_current_marker; + OPJ_BYTE l_data [2]; + opj_tcp_v2_t * l_tcp; + + /* preconditions */ + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/) + || (p_tile_index != p_j2k->m_current_tile_number) ) { + return OPJ_FALSE; + } + + l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]); + if (! l_tcp->m_data) { + opj_j2k_tcp_destroy(l_tcp); + return OPJ_FALSE; + } + + if (! opj_tcd_decode_tile( p_j2k->m_tcd, + l_tcp->m_data, + l_tcp->m_data_size, + p_tile_index, + p_j2k->cstr_index) ) { + opj_j2k_tcp_destroy(l_tcp); + p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/ + return OPJ_FALSE; + } + + if (! opj_tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) { + return OPJ_FALSE; + } + + /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access) + * we destroy just the data which will be re-read in read_tile_header*/ + /*opj_j2k_tcp_destroy(l_tcp); + p_j2k->m_tcd->tcp = 0;*/ + opj_j2k_tcp_data_destroy(l_tcp); + + p_j2k->m_specific_param.m_decoder.m_can_decode = 0; + p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/ + + if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/ + if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n"); + return OPJ_FALSE; + } + + opj_read_bytes(l_data,&l_current_marker,2); + + if (l_current_marker == J2K_MS_EOC) { + p_j2k->m_current_tile_number = 0; + p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/ + } + else if (l_current_marker != J2K_MS_SOT) + { + opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n"); + return OPJ_FALSE; + } + } + + return OPJ_TRUE; } opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image) { - OPJ_UINT32 i,j,k = 0; - OPJ_UINT32 l_width_src,l_height_src; - OPJ_UINT32 l_width_dest,l_height_dest; - OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src; - OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ; - OPJ_UINT32 l_start_x_dest , l_start_y_dest; - OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest; - OPJ_INT32 l_start_offset_dest, l_line_offset_dest; - - opj_image_comp_t * l_img_comp_src = 00; - opj_image_comp_t * l_img_comp_dest = 00; - - opj_tcd_tilecomp_v2_t * l_tilec = 00; - opj_image_t * l_image_src = 00; - OPJ_UINT32 l_size_comp, l_remaining; - OPJ_INT32 * l_dest_ptr; - opj_tcd_resolution_v2_t* l_res= 00; - - l_tilec = p_tcd->tcd_image->tiles->comps; - l_image_src = p_tcd->image; - l_img_comp_src = l_image_src->comps; - - l_img_comp_dest = p_output_image->comps; - - for (i=0; inumcomps; i++) { - - /* Allocate output component buffer if necessary */ - if (!l_img_comp_dest->data) { - - l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32)); - if (! l_img_comp_dest->data) { - return OPJ_FALSE; - } - } - - /* Copy info from decoded comp image to output image */ - l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded; - - /*-----*/ - /* Compute the precision of the output buffer */ - l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/ - l_remaining = l_img_comp_src->prec & 7; /* (%8) */ - l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded; - - if (l_remaining) { - ++l_size_comp; - } - - if (l_size_comp == 3) { - l_size_comp = 4; - } - /*-----*/ - - /* Current tile component size*/ - /*if (i == 0) { - fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n", - l_res->x0, l_res->x1, l_res->y0, l_res->y1); - }*/ - - l_width_src = (l_res->x1 - l_res->x0); - l_height_src = (l_res->y1 - l_res->y0); - - /* Border of the current output component*/ - l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor); - l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor); - l_x1_dest = l_x0_dest + l_img_comp_dest->w; - l_y1_dest = l_y0_dest + l_img_comp_dest->h; - - /*if (i == 0) { - fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n", - l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor ); - }*/ - - /*-----*/ - /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src) - * of the input buffer (decoded tile component) which will be move - * in the output buffer. Compute the area of the output buffer (l_start_x_dest, - * l_start_y_dest, l_width_dest, l_height_dest) which will be modified - * by this input area. - * */ - assert( l_res->x0 >= 0); - assert( l_res->x1 >= 0); - if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) { - l_start_x_dest = l_res->x0 - l_x0_dest; - l_offset_x0_src = 0; - - if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) { - l_width_dest = l_width_src; - l_offset_x1_src = 0; - } - else { - l_width_dest = l_x1_dest - l_res->x0 ; - l_offset_x1_src = l_width_src - l_width_dest; - } - } - else { - l_start_x_dest = 0 ; - l_offset_x0_src = l_x0_dest - l_res->x0; - - if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) { - l_width_dest = l_width_src - l_offset_x0_src; - l_offset_x1_src = 0; - } - else { - l_width_dest = l_img_comp_dest->w ; - l_offset_x1_src = l_res->x1 - l_x1_dest; - } - } - - if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) { - l_start_y_dest = l_res->y0 - l_y0_dest; - l_offset_y0_src = 0; - - if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) { - l_height_dest = l_height_src; - l_offset_y1_src = 0; - } - else { - l_height_dest = l_y1_dest - l_res->y0 ; - l_offset_y1_src = l_height_src - l_height_dest; - } - } - else { - l_start_y_dest = 0 ; - l_offset_y0_src = l_y0_dest - l_res->y0; - - if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) { - l_height_dest = l_height_src - l_offset_y0_src; - l_offset_y1_src = 0; - } - else { - l_height_dest = l_img_comp_dest->h ; - l_offset_y1_src = l_res->y1 - l_y1_dest; - } - } - - if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){ - return OPJ_FALSE; - } - /*-----*/ - - /* Compute the input buffer offset */ - l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src; - l_line_offset_src = l_offset_x1_src + l_offset_x0_src; - l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src; - - /* Compute the output buffer offset */ - l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w; - l_line_offset_dest = l_img_comp_dest->w - l_width_dest; - - /* Move the output buffer to the first place where we will write*/ - l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest; - - /*if (i == 0) { - fprintf(stdout, "COMPO[%d]:\n",i); - fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n" - "\t tile offset:%d, %d, %d, %d\n" - "\t buffer offset: %d; %d, %d\n", - l_res->x0, l_res->y0, l_width_src, l_height_src, - l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src, - l_start_offset_src, l_line_offset_src, l_end_offset_src); - - fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n" - "\t start offset: %d, line offset= %d\n", - l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest); - }*/ - - - switch (l_size_comp) { - case 1: - { - OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data; - l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/ - - if (l_img_comp_src->sgnd) { - for (j = 0 ; j < l_height_dest ; ++j) { - for ( k = 0 ; k < l_width_dest ; ++k) { - *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */ - } - - l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */ - l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */ - } - } - else { - for ( j = 0 ; j < l_height_dest ; ++j ) { - for ( k = 0 ; k < l_width_dest ; ++k) { - *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff); - } - - l_dest_ptr+= l_line_offset_dest; - l_src_ptr += l_line_offset_src; - } - } - - l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */ - p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */ - } - break; - case 2: - { - OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data; - l_src_ptr += l_start_offset_src; - - if (l_img_comp_src->sgnd) { - for (j=0;jtcd_image->tiles->comps; + l_image_src = p_tcd->image; + l_img_comp_src = l_image_src->comps; + + l_img_comp_dest = p_output_image->comps; + + for (i=0; inumcomps; i++) { + + /* Allocate output component buffer if necessary */ + if (!l_img_comp_dest->data) { + + l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32)); + if (! l_img_comp_dest->data) { + return OPJ_FALSE; + } + } + + /* Copy info from decoded comp image to output image */ + l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded; + + /*-----*/ + /* Compute the precision of the output buffer */ + l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp_src->prec & 7; /* (%8) */ + l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded; + + if (l_remaining) { + ++l_size_comp; + } + + if (l_size_comp == 3) { + l_size_comp = 4; + } + /*-----*/ + + /* Current tile component size*/ + /*if (i == 0) { + fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n", + l_res->x0, l_res->x1, l_res->y0, l_res->y1); + }*/ + + l_width_src = (l_res->x1 - l_res->x0); + l_height_src = (l_res->y1 - l_res->y0); + + /* Border of the current output component*/ + l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor); + l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor); + l_x1_dest = l_x0_dest + l_img_comp_dest->w; + l_y1_dest = l_y0_dest + l_img_comp_dest->h; + + /*if (i == 0) { + fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n", + l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor ); + }*/ + + /*-----*/ + /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src) + * of the input buffer (decoded tile component) which will be move + * in the output buffer. Compute the area of the output buffer (l_start_x_dest, + * l_start_y_dest, l_width_dest, l_height_dest) which will be modified + * by this input area. + * */ + assert( l_res->x0 >= 0); + assert( l_res->x1 >= 0); + if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) { + l_start_x_dest = l_res->x0 - l_x0_dest; + l_offset_x0_src = 0; + + if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) { + l_width_dest = l_width_src; + l_offset_x1_src = 0; + } + else { + l_width_dest = l_x1_dest - l_res->x0 ; + l_offset_x1_src = l_width_src - l_width_dest; + } + } + else { + l_start_x_dest = 0 ; + l_offset_x0_src = l_x0_dest - l_res->x0; + + if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) { + l_width_dest = l_width_src - l_offset_x0_src; + l_offset_x1_src = 0; + } + else { + l_width_dest = l_img_comp_dest->w ; + l_offset_x1_src = l_res->x1 - l_x1_dest; + } + } + + if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) { + l_start_y_dest = l_res->y0 - l_y0_dest; + l_offset_y0_src = 0; + + if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) { + l_height_dest = l_height_src; + l_offset_y1_src = 0; + } + else { + l_height_dest = l_y1_dest - l_res->y0 ; + l_offset_y1_src = l_height_src - l_height_dest; + } + } + else { + l_start_y_dest = 0 ; + l_offset_y0_src = l_y0_dest - l_res->y0; + + if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) { + l_height_dest = l_height_src - l_offset_y0_src; + l_offset_y1_src = 0; + } + else { + l_height_dest = l_img_comp_dest->h ; + l_offset_y1_src = l_res->y1 - l_y1_dest; + } + } + + if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){ + return OPJ_FALSE; + } + /*-----*/ + + /* Compute the input buffer offset */ + l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src; + l_line_offset_src = l_offset_x1_src + l_offset_x0_src; + l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src; + + /* Compute the output buffer offset */ + l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w; + l_line_offset_dest = l_img_comp_dest->w - l_width_dest; + + /* Move the output buffer to the first place where we will write*/ + l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest; + + /*if (i == 0) { + fprintf(stdout, "COMPO[%d]:\n",i); + fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n" + "\t tile offset:%d, %d, %d, %d\n" + "\t buffer offset: %d; %d, %d\n", + l_res->x0, l_res->y0, l_width_src, l_height_src, + l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src, + l_start_offset_src, l_line_offset_src, l_end_offset_src); + + fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n" + "\t start offset: %d, line offset= %d\n", + l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest); + }*/ + + + switch (l_size_comp) { + case 1: + { + OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data; + l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/ + + if (l_img_comp_src->sgnd) { + for (j = 0 ; j < l_height_dest ; ++j) { + for ( k = 0 ; k < l_width_dest ; ++k) { + *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */ + } + + l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */ + l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */ + } + } + else { + for ( j = 0 ; j < l_height_dest ; ++j ) { + for ( k = 0 ; k < l_width_dest ; ++k) { + *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff); + } + + l_dest_ptr+= l_line_offset_dest; + l_src_ptr += l_line_offset_src; + } + } + + l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */ + p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */ + } + break; + case 2: + { + OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data; + l_src_ptr += l_start_offset_src; + + if (l_img_comp_src->sgnd) { + for (j=0;jm_cp); - opj_image_t * l_image = p_j2k->m_private_image; - - OPJ_UINT32 it_comp; - OPJ_INT32 l_comp_x1, l_comp_y1; - opj_image_comp_t* l_img_comp = NULL; - - /* Check if we are read the main header */ - if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/ - opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream"); - return OPJ_FALSE; - } - - if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){ - opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n"); - - p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; - p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; - p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; - p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; - - return OPJ_TRUE; - } - - /* ----- */ - /* Check if the positions provided by the user are correct */ - - /* Left */ - assert(p_start_x >= 0 ); - assert(p_start_y >= 0 ); - - if ((OPJ_UINT32)p_start_x > l_image->x1 ) { - opj_event_msg_v2(p_manager, EVT_ERROR, - "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n", - p_start_x, l_image->x1); - return OPJ_FALSE; - } - else if ((OPJ_UINT32)p_start_x < l_image->x0){ - opj_event_msg_v2(p_manager, EVT_WARNING, - "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n", - p_start_x, l_image->x0); - p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; - p_image->x0 = l_image->x0; - } - else { - p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx; - p_image->x0 = p_start_x; - } - - /* Up */ - if ((OPJ_UINT32)p_start_y > l_image->y1){ - opj_event_msg_v2(p_manager, EVT_ERROR, - "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n", - p_start_y, l_image->y1); - return OPJ_FALSE; - } - else if ((OPJ_UINT32)p_start_y < l_image->y0){ - opj_event_msg_v2(p_manager, EVT_WARNING, - "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n", - p_start_y, l_image->y0); - p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; - p_image->y0 = l_image->y0; - } - else { - p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy; - p_image->y0 = p_start_y; - } - - /* Right */ - assert((OPJ_UINT32)p_end_x > 0); - assert((OPJ_UINT32)p_end_y > 0); - if ((OPJ_UINT32)p_end_x < l_image->x0) { - opj_event_msg_v2(p_manager, EVT_ERROR, - "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n", - p_end_x, l_image->x0); - return OPJ_FALSE; - } - else if ((OPJ_UINT32)p_end_x > l_image->x1) { - opj_event_msg_v2(p_manager, EVT_WARNING, - "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n", - p_end_x, l_image->x1); - p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; - p_image->x1 = l_image->x1; - } - else { - p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx); - p_image->x1 = p_end_x; - } - - /* Bottom */ - if ((OPJ_UINT32)p_end_y < l_image->y0) { - opj_event_msg_v2(p_manager, EVT_ERROR, - "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n", - p_end_y, l_image->y0); - return OPJ_FALSE; - } - if ((OPJ_UINT32)p_end_y > l_image->y1){ - opj_event_msg_v2(p_manager, EVT_WARNING, - "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n", - p_end_y, l_image->y1); - p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; - p_image->y1 = l_image->y1; - } - else{ - p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy); - p_image->y1 = p_end_y; - } - /* ----- */ - - p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1; - - l_img_comp = p_image->comps; - for (it_comp=0; it_comp < p_image->numcomps; ++it_comp) - { - OPJ_INT32 l_h,l_w; - - l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx); - l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy); - l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx); - l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy); - - l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor); - if (l_w < 0){ - opj_event_msg_v2(p_manager, EVT_ERROR, - "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n", - it_comp, l_w); - return OPJ_FALSE; - } - l_img_comp->w = l_w; - - l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor); - if (l_h < 0){ - opj_event_msg_v2(p_manager, EVT_ERROR, - "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n", - it_comp, l_h); - return OPJ_FALSE; - } - l_img_comp->h = l_h; - - l_img_comp++; - } - - opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n", - p_image->x0, p_image->y0, p_image->x1, p_image->y1); - - - return OPJ_TRUE; +opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k, + opj_image_t* p_image, + OPJ_INT32 p_start_x, OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, OPJ_INT32 p_end_y, + opj_event_mgr_t * p_manager ) +{ + opj_cp_v2_t * l_cp = &(p_j2k->m_cp); + opj_image_t * l_image = p_j2k->m_private_image; + + OPJ_UINT32 it_comp; + OPJ_INT32 l_comp_x1, l_comp_y1; + opj_image_comp_t* l_img_comp = NULL; + + /* Check if we are read the main header */ + if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/ + opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream"); + return OPJ_FALSE; + } + + if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){ + opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n"); + + p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; + p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; + p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; + p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; + + return OPJ_TRUE; + } + + /* ----- */ + /* Check if the positions provided by the user are correct */ + + /* Left */ + assert(p_start_x >= 0 ); + assert(p_start_y >= 0 ); + + if ((OPJ_UINT32)p_start_x > l_image->x1 ) { + opj_event_msg_v2(p_manager, EVT_ERROR, + "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n", + p_start_x, l_image->x1); + return OPJ_FALSE; + } + else if ((OPJ_UINT32)p_start_x < l_image->x0){ + opj_event_msg_v2(p_manager, EVT_WARNING, + "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n", + p_start_x, l_image->x0); + p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; + p_image->x0 = l_image->x0; + } + else { + p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx; + p_image->x0 = p_start_x; + } + + /* Up */ + if ((OPJ_UINT32)p_start_y > l_image->y1){ + opj_event_msg_v2(p_manager, EVT_ERROR, + "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n", + p_start_y, l_image->y1); + return OPJ_FALSE; + } + else if ((OPJ_UINT32)p_start_y < l_image->y0){ + opj_event_msg_v2(p_manager, EVT_WARNING, + "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n", + p_start_y, l_image->y0); + p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; + p_image->y0 = l_image->y0; + } + else { + p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy; + p_image->y0 = p_start_y; + } + + /* Right */ + assert((OPJ_UINT32)p_end_x > 0); + assert((OPJ_UINT32)p_end_y > 0); + if ((OPJ_UINT32)p_end_x < l_image->x0) { + opj_event_msg_v2(p_manager, EVT_ERROR, + "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n", + p_end_x, l_image->x0); + return OPJ_FALSE; + } + else if ((OPJ_UINT32)p_end_x > l_image->x1) { + opj_event_msg_v2(p_manager, EVT_WARNING, + "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n", + p_end_x, l_image->x1); + p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; + p_image->x1 = l_image->x1; + } + else { + p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx); + p_image->x1 = p_end_x; + } + + /* Bottom */ + if ((OPJ_UINT32)p_end_y < l_image->y0) { + opj_event_msg_v2(p_manager, EVT_ERROR, + "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n", + p_end_y, l_image->y0); + return OPJ_FALSE; + } + if ((OPJ_UINT32)p_end_y > l_image->y1){ + opj_event_msg_v2(p_manager, EVT_WARNING, + "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n", + p_end_y, l_image->y1); + p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; + p_image->y1 = l_image->y1; + } + else{ + p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy); + p_image->y1 = p_end_y; + } + /* ----- */ + + p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1; + + l_img_comp = p_image->comps; + for (it_comp=0; it_comp < p_image->numcomps; ++it_comp) + { + OPJ_INT32 l_h,l_w; + + l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx); + l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy); + l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx); + l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy); + + l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) + - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor); + if (l_w < 0){ + opj_event_msg_v2(p_manager, EVT_ERROR, + "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n", + it_comp, l_w); + return OPJ_FALSE; + } + l_img_comp->w = l_w; + + l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) + - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor); + if (l_h < 0){ + opj_event_msg_v2(p_manager, EVT_ERROR, + "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n", + it_comp, l_h); + return OPJ_FALSE; + } + l_img_comp->h = l_h; + + l_img_comp++; + } + + opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n", + p_image->x0, p_image->y0, p_image->x1, p_image->y1); + + + return OPJ_TRUE; } @@ -7999,1164 +8141,1166 @@ opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k, */ opj_j2k_v2_t* opj_j2k_create_decompress(void) { - opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t)); - if (!l_j2k) { - return 00; - } - memset(l_j2k,0,sizeof(opj_j2k_v2_t)); + opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t)); + if (!l_j2k) { + return 00; + } + memset(l_j2k,0,sizeof(opj_j2k_v2_t)); - l_j2k->m_is_decoder = 1; - l_j2k->m_cp.m_is_decoder = 1; + l_j2k->m_is_decoder = 1; + l_j2k->m_cp.m_is_decoder = 1; - l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t)); - if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) { - opj_j2k_destroy(l_j2k); - return 00; - } - memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t)); + l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t)); + if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) { + opj_j2k_destroy(l_j2k); + return 00; + } + memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t)); - l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE); - if (! l_j2k->m_specific_param.m_decoder.m_header_data) { - opj_j2k_destroy(l_j2k); - return 00; - } + l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE); + if (! l_j2k->m_specific_param.m_decoder.m_header_data) { + opj_j2k_destroy(l_j2k); + return 00; + } - l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE; + l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE; - l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ; + l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ; - l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ; + l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ; - /* codestream index creation */ - l_j2k->cstr_index = opj_j2k_create_cstr_index(); + /* codestream index creation */ + l_j2k->cstr_index = opj_j2k_create_cstr_index(); - /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t)); - if (!l_j2k->cstr_index){ - opj_j2k_destroy(l_j2k); - return NULL; - } + /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t)); + if (!l_j2k->cstr_index){ + opj_j2k_destroy(l_j2k); + return NULL; + } - l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t)); + l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t)); */ - /* validation list creation */ - l_j2k->m_validation_list = opj_procedure_list_create(); - if (! l_j2k->m_validation_list) { - opj_j2k_destroy(l_j2k); - return 00; - } + /* validation list creation */ + l_j2k->m_validation_list = opj_procedure_list_create(); + if (! l_j2k->m_validation_list) { + opj_j2k_destroy(l_j2k); + return 00; + } - /* execution list creation */ - l_j2k->m_procedure_list = opj_procedure_list_create(); - if (! l_j2k->m_procedure_list) { - opj_j2k_destroy(l_j2k); - return 00; - } + /* execution list creation */ + l_j2k->m_procedure_list = opj_procedure_list_create(); + if (! l_j2k->m_procedure_list) { + opj_j2k_destroy(l_j2k); + return 00; + } - return l_j2k; + return l_j2k; } opj_codestream_index_t* opj_j2k_create_cstr_index(void) { - opj_codestream_index_t* cstr_index = (opj_codestream_index_t*) - opj_calloc(1,sizeof(opj_codestream_index_t)); - if (!cstr_index) - return NULL; + opj_codestream_index_t* cstr_index = (opj_codestream_index_t*) + opj_calloc(1,sizeof(opj_codestream_index_t)); + if (!cstr_index) + return NULL; - cstr_index->maxmarknum = 100; - cstr_index->marknum = 0; - cstr_index->marker = (opj_marker_info_t*) - opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t)); - if (!cstr_index-> marker) - return NULL; + cstr_index->maxmarknum = 100; + cstr_index->marknum = 0; + cstr_index->marker = (opj_marker_info_t*) + opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t)); + if (!cstr_index-> marker) + return NULL; - cstr_index->tile_index = NULL; + cstr_index->tile_index = NULL; - return cstr_index; + return cstr_index; } /** * Gets the size taken by writing a SPCod or SPCoc for the given tile and component. * - * @param p_tile_no the tile index. - * @param p_comp_no the component being outputted. - * @param p_j2k the J2K codec. + * @param p_tile_no the tile index. + * @param p_comp_no the component being outputted. + * @param p_j2k the J2K codec. * - * @return the number of bytes taken by the SPCod element. + * @return the number of bytes taken by the SPCod element. */ -OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_tile_no, - OPJ_UINT32 p_comp_no ) +OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no ) { - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - opj_tccp_t *l_tccp = 00; + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; - /* preconditions */ - assert(p_j2k != 00); + /* preconditions */ + assert(p_j2k != 00); - l_cp = &(p_j2k->m_cp); - l_tcp = &l_cp->tcps[p_tile_no]; - l_tccp = &l_tcp->tccps[p_comp_no]; + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; - /* preconditions again */ - assert(p_tile_no < (l_cp->tw * l_cp->th)); - assert(p_comp_no < p_j2k->m_private_image->numcomps); + /* preconditions again */ + assert(p_tile_no < (l_cp->tw * l_cp->th)); + assert(p_comp_no < p_j2k->m_private_image->numcomps); - if (l_tccp->csty & J2K_CCP_CSTY_PRT) { - return 5 + l_tccp->numresolutions; - } - else { - return 5; - } + if (l_tccp->csty & J2K_CCP_CSTY_PRT) { + return 5 + l_tccp->numresolutions; + } + else { + return 5; + } } /** * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. * - * @param p_comp_no the component number to output. - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_comp_no the component number to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. * */ -opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_tile_no, - OPJ_UINT32 p_comp_no, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_header_size, - struct opj_event_mgr * p_manager ) -{ - OPJ_UINT32 i; - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - opj_tccp_t *l_tccp = 00; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_header_size != 00); - assert(p_manager != 00); - assert(p_data != 00); - - l_cp = &(p_j2k->m_cp); - l_tcp = &l_cp->tcps[p_tile_no]; - l_tccp = &l_tcp->tccps[p_comp_no]; - - /* preconditions again */ - assert(p_tile_no < (l_cp->tw * l_cp->th)); - assert(p_comp_no <(p_j2k->m_private_image->numcomps)); - - if (*p_header_size < 5) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); - return OPJ_FALSE; - } +opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager ) +{ + OPJ_UINT32 i; + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_header_size != 00); + assert(p_manager != 00); + assert(p_data != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + /* preconditions again */ + assert(p_tile_no < (l_cp->tw * l_cp->th)); + assert(p_comp_no <(p_j2k->m_private_image->numcomps)); + + if (*p_header_size < 5) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); + return OPJ_FALSE; + } - opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */ - ++p_data; + opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */ + ++p_data; - opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */ - ++p_data; + opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */ + ++p_data; - opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */ - ++p_data; + opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */ + ++p_data; - opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */ - ++p_data; + opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */ + ++p_data; - opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */ - ++p_data; + opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */ + ++p_data; - *p_header_size = *p_header_size - 5; + *p_header_size = *p_header_size - 5; - if (l_tccp->csty & J2K_CCP_CSTY_PRT) { + if (l_tccp->csty & J2K_CCP_CSTY_PRT) { - if (*p_header_size < l_tccp->numresolutions) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n"); - return OPJ_FALSE; - } + if (*p_header_size < l_tccp->numresolutions) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n"); + return OPJ_FALSE; + } - for (i = 0; i < l_tccp->numresolutions; ++i) { - opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */ - ++p_data; - } + for (i = 0; i < l_tccp->numresolutions; ++i) { + opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */ + ++p_data; + } - *p_header_size = *p_header_size - l_tccp->numresolutions; - } + *p_header_size = *p_header_size - l_tccp->numresolutions; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. - * @param p_header_data the data contained in the COM box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the COM marker. - * @param p_manager the user event manager. + * @param p_header_data the data contained in the COM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. */ opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 compno, - OPJ_BYTE * p_header_data, - OPJ_UINT32 * p_header_size, - opj_event_mgr_t * p_manager) -{ - OPJ_UINT32 i, l_tmp; - opj_cp_v2_t *l_cp = NULL; - opj_tcp_v2_t *l_tcp = NULL; - opj_tccp_t *l_tccp = NULL; - OPJ_BYTE * l_current_ptr = NULL; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_header_data != 00); - - l_cp = &(p_j2k->m_cp); - l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? - &l_cp->tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; - - /* precondition again */ - assert(compno < p_j2k->m_private_image->numcomps); - - l_tccp = &l_tcp->tccps[compno]; - l_current_ptr = p_header_data; - - /* make sure room is sufficient */ - if (*p_header_size < 5) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); - return OPJ_FALSE; - } - - opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */ - ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */ - ++l_current_ptr; - - /* If user wants to remove more resolutions than the codestream contains, return error */ - if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number " - "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno); - p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/ - return OPJ_FALSE; - } - - opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */ - ++l_current_ptr; - l_tccp->cblkw += 2; - - opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */ - ++l_current_ptr; - l_tccp->cblkh += 2; - - opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */ - ++l_current_ptr; - - opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */ - ++l_current_ptr; - - *p_header_size = *p_header_size - 5; - - /* use custom precinct size ? */ - if (l_tccp->csty & J2K_CCP_CSTY_PRT) { - if (*p_header_size < l_tccp->numresolutions) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); - return OPJ_FALSE; - } - - for (i = 0; i < l_tccp->numresolutions; ++i) { - opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */ - ++l_current_ptr; - l_tccp->prcw[i] = l_tmp & 0xf; - l_tccp->prch[i] = l_tmp >> 4; - } - - *p_header_size = *p_header_size - l_tccp->numresolutions; - } - else { - /* set default size for the precinct width and height */ - for (i = 0; i < l_tccp->numresolutions; ++i) { - l_tccp->prcw[i] = 15; - l_tccp->prch[i] = 15; - } - } + OPJ_UINT32 compno, + OPJ_BYTE * p_header_data, + OPJ_UINT32 * p_header_size, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 i, l_tmp; + opj_cp_v2_t *l_cp = NULL; + opj_tcp_v2_t *l_tcp = NULL; + opj_tccp_t *l_tccp = NULL; + OPJ_BYTE * l_current_ptr = NULL; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + /* precondition again */ + assert(compno < p_j2k->m_private_image->numcomps); + + l_tccp = &l_tcp->tccps[compno]; + l_current_ptr = p_header_data; + + /* make sure room is sufficient */ + if (*p_header_size < 5) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); + return OPJ_FALSE; + } + + opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */ + ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */ + ++l_current_ptr; + + /* If user wants to remove more resolutions than the codestream contains, return error */ + if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number " + "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno); + p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/ + return OPJ_FALSE; + } + + opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */ + ++l_current_ptr; + l_tccp->cblkw += 2; + + opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */ + ++l_current_ptr; + l_tccp->cblkh += 2; + + opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */ + ++l_current_ptr; + + opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */ + ++l_current_ptr; + + *p_header_size = *p_header_size - 5; + + /* use custom precinct size ? */ + if (l_tccp->csty & J2K_CCP_CSTY_PRT) { + if (*p_header_size < l_tccp->numresolutions) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); + return OPJ_FALSE; + } + + for (i = 0; i < l_tccp->numresolutions; ++i) { + opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */ + ++l_current_ptr; + l_tccp->prcw[i] = l_tmp & 0xf; + l_tccp->prch[i] = l_tmp >> 4; + } + + *p_header_size = *p_header_size - l_tccp->numresolutions; + } + else { + /* set default size for the precinct width and height */ + for (i = 0; i < l_tccp->numresolutions; ++i) { + l_tccp->prcw[i] = 15; + l_tccp->prch[i] = 15; + } + } #ifdef WIP_REMOVE_MSD - /* INDEX >> */ - if (p_j2k->cstr_info && compno == 0) { - OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32); + /* INDEX >> */ + if (p_j2k->cstr_info && compno == 0) { + OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32); - p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh; - p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw; - p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions; - p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty; - p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid; + p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh; + p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw; + p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions; + p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty; + p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid; - memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size); - memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size); - } - /* << INDEX */ + memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size); + memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size); + } + /* << INDEX */ #endif - return OPJ_TRUE; + return OPJ_TRUE; } /** * Copies the tile component parameters of all the component from the first tile component. * - * @param p_j2k the J2k codec. + * @param p_j2k the J2k codec. */ void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k ) { - /* loop */ - OPJ_UINT32 i; - opj_cp_v2_t *l_cp = NULL; - opj_tcp_v2_t *l_tcp = NULL; - opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL; - OPJ_UINT32 l_prc_size; - - /* preconditions */ - assert(p_j2k != 00); - - l_cp = &(p_j2k->m_cp); - l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/ - &l_cp->tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; - - l_ref_tccp = &l_tcp->tccps[0]; - l_copied_tccp = l_ref_tccp + 1; - l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32); - - for (i=1; im_private_image->numcomps; ++i) { - l_copied_tccp->numresolutions = l_ref_tccp->numresolutions; - l_copied_tccp->cblkw = l_ref_tccp->cblkw; - l_copied_tccp->cblkh = l_ref_tccp->cblkh; - l_copied_tccp->cblksty = l_ref_tccp->cblksty; - l_copied_tccp->qmfbid = l_ref_tccp->qmfbid; - memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size); - memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size); - ++l_copied_tccp; - } + /* loop */ + OPJ_UINT32 i; + opj_cp_v2_t *l_cp = NULL; + opj_tcp_v2_t *l_tcp = NULL; + opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL; + OPJ_UINT32 l_prc_size; + + /* preconditions */ + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/ + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + l_ref_tccp = &l_tcp->tccps[0]; + l_copied_tccp = l_ref_tccp + 1; + l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32); + + for (i=1; im_private_image->numcomps; ++i) { + l_copied_tccp->numresolutions = l_ref_tccp->numresolutions; + l_copied_tccp->cblkw = l_ref_tccp->cblkw; + l_copied_tccp->cblkh = l_ref_tccp->cblkh; + l_copied_tccp->cblksty = l_ref_tccp->cblksty; + l_copied_tccp->qmfbid = l_ref_tccp->qmfbid; + memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size); + memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size); + ++l_copied_tccp; + } } /** * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. * - * @param p_tile_no the tile index. - * @param p_comp_no the component being outputted. - * @param p_j2k the J2K codec. + * @param p_tile_no the tile index. + * @param p_comp_no the component being outputted. + * @param p_j2k the J2K codec. * - * @return the number of bytes taken by the SPCod element. + * @return the number of bytes taken by the SPCod element. */ -OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_tile_no, - OPJ_UINT32 p_comp_no ) +OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no ) { - OPJ_UINT32 l_num_bands; + OPJ_UINT32 l_num_bands; - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - opj_tccp_t *l_tccp = 00; + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; - /* preconditions */ - assert(p_j2k != 00); + /* preconditions */ + assert(p_j2k != 00); - l_cp = &(p_j2k->m_cp); - l_tcp = &l_cp->tcps[p_tile_no]; - l_tccp = &l_tcp->tccps[p_comp_no]; + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; - /* preconditions again */ - assert(p_tile_no < l_cp->tw * l_cp->th); - assert(p_comp_no < p_j2k->m_private_image->numcomps); + /* preconditions again */ + assert(p_tile_no < l_cp->tw * l_cp->th); + assert(p_comp_no < p_j2k->m_private_image->numcomps); - l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2); + l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2); - if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { - return 1 + l_num_bands; - } - else { - return 1 + 2*l_num_bands; - } + if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { + return 1 + l_num_bands; + } + else { + return 1 + 2*l_num_bands; + } } /** * Writes a SQcd or SQcc element, i.e. the quantization values of a band. * - * @param p_tile_no the tile to output. - * @param p_comp_no the component number to output. - * @param p_data the data buffer. - * @param p_header_size pointer to the size of the data buffer, it is changed by the function. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_tile_no the tile to output. + * @param p_comp_no the component number to output. + * @param p_data the data buffer. + * @param p_header_size pointer to the size of the data buffer, it is changed by the function. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. * */ -opj_bool opj_j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_tile_no, - OPJ_UINT32 p_comp_no, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_header_size, - struct opj_event_mgr * p_manager ) -{ - OPJ_UINT32 l_header_size; - OPJ_UINT32 l_band_no, l_num_bands; - OPJ_UINT32 l_expn,l_mant; - - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - opj_tccp_t *l_tccp = 00; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_header_size != 00); - assert(p_manager != 00); - assert(p_data != 00); - - l_cp = &(p_j2k->m_cp); - l_tcp = &l_cp->tcps[p_tile_no]; - l_tccp = &l_tcp->tccps[p_comp_no]; - - /* preconditions again */ - assert(p_tile_no < l_cp->tw * l_cp->th); - assert(p_comp_no m_private_image->numcomps); - - l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2); - - if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { - l_header_size = 1 + l_num_bands; - - if (*p_header_size < l_header_size) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); - return OPJ_FALSE; - } - - opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */ - ++p_data; - - for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { - l_expn = l_tccp->stepsizes[l_band_no].expn; - opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */ - ++p_data; - } - } - else { - l_header_size = 1 + 2*l_num_bands; +opj_bool opj_j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager ) +{ + OPJ_UINT32 l_header_size; + OPJ_UINT32 l_band_no, l_num_bands; + OPJ_UINT32 l_expn,l_mant; + + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_header_size != 00); + assert(p_manager != 00); + assert(p_data != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + /* preconditions again */ + assert(p_tile_no < l_cp->tw * l_cp->th); + assert(p_comp_no m_private_image->numcomps); + + l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2); + + if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { + l_header_size = 1 + l_num_bands; + + if (*p_header_size < l_header_size) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); + return OPJ_FALSE; + } + + opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */ + ++p_data; + + for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { + l_expn = l_tccp->stepsizes[l_band_no].expn; + opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */ + ++p_data; + } + } + else { + l_header_size = 1 + 2*l_num_bands; - if (*p_header_size < l_header_size) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); - return OPJ_FALSE; - } + if (*p_header_size < l_header_size) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); + return OPJ_FALSE; + } - opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */ - ++p_data; + opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */ + ++p_data; - for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { - l_expn = l_tccp->stepsizes[l_band_no].expn; - l_mant = l_tccp->stepsizes[l_band_no].mant; + for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) { + l_expn = l_tccp->stepsizes[l_band_no].expn; + l_mant = l_tccp->stepsizes[l_band_no].mant; - opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */ - p_data += 2; - } - } + opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */ + p_data += 2; + } + } - *p_header_size = *p_header_size - l_header_size; + *p_header_size = *p_header_size - l_header_size; - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a SQcd or SQcc element, i.e. the quantization values of a band. * - * @param p_comp_no the component being targeted. - * @param p_header_data the data contained in the COM box. - * @param p_j2k the jpeg2000 codec. - * @param p_header_size the size of the data contained in the COM marker. - * @param p_manager the user event manager. + * @param p_comp_no the component being targeted. + * @param p_header_data the data contained in the COM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. */ opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_v2_t *p_j2k, - OPJ_UINT32 p_comp_no, - OPJ_BYTE* p_header_data, - OPJ_UINT32 * p_header_size, - opj_event_mgr_t * p_manager - ) -{ - /* loop*/ - OPJ_UINT32 l_band_no; - opj_cp_v2_t *l_cp = 00; - opj_tcp_v2_t *l_tcp = 00; - opj_tccp_t *l_tccp = 00; - OPJ_BYTE * l_current_ptr = 00; - OPJ_UINT32 l_tmp, l_num_band; - - /* preconditions*/ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_header_data != 00); - - l_cp = &(p_j2k->m_cp); - /* come from tile part header or main header ?*/ - l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/ - &l_cp->tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; - - /* precondition again*/ - assert(p_comp_no < p_j2k->m_private_image->numcomps); - - l_tccp = &l_tcp->tccps[p_comp_no]; - l_current_ptr = p_header_data; - - if (*p_header_size < 1) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n"); - return OPJ_FALSE; - } - *p_header_size -= 1; - - opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */ - ++l_current_ptr; - - l_tccp->qntsty = l_tmp & 0x1f; - l_tccp->numgbits = l_tmp >> 5; - if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { + OPJ_UINT32 p_comp_no, + OPJ_BYTE* p_header_data, + OPJ_UINT32 * p_header_size, + opj_event_mgr_t * p_manager + ) +{ + /* loop*/ + OPJ_UINT32 l_band_no; + opj_cp_v2_t *l_cp = 00; + opj_tcp_v2_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + OPJ_BYTE * l_current_ptr = 00; + OPJ_UINT32 l_tmp, l_num_band; + + /* preconditions*/ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); + + l_cp = &(p_j2k->m_cp); + /* come from tile part header or main header ?*/ + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/ + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + /* precondition again*/ + assert(p_comp_no < p_j2k->m_private_image->numcomps); + + l_tccp = &l_tcp->tccps[p_comp_no]; + l_current_ptr = p_header_data; + + if (*p_header_size < 1) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n"); + return OPJ_FALSE; + } + *p_header_size -= 1; + + opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */ + ++l_current_ptr; + + l_tccp->qntsty = l_tmp & 0x1f; + l_tccp->numgbits = l_tmp >> 5; + if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { l_num_band = 1; - } - else { - l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? - (*p_header_size) : - (*p_header_size) / 2; - - if( l_num_band > J2K_MAXBANDS ) { - opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, " - "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to " - "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS); - /*return OPJ_FALSE;*/ - } - } + } + else { + l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? + (*p_header_size) : + (*p_header_size) / 2; + + if( l_num_band > J2K_MAXBANDS ) { + opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, " + "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to " + "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS); + /*return OPJ_FALSE;*/ + } + } #ifdef USE_JPWL - if (l_cp->correct) { - - /* if JPWL is on, we check whether there are too many subbands */ - if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) { - opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, - "JPWL: bad number of subbands in Sqcx (%d)\n", - l_num_band); - if (!JPWL_ASSUME) { - opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - /* we try to correct */ - l_num_band = 1; - opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n" - "- setting number of bands to %d => HYPOTHESIS!!!\n", - l_num_band); - }; - - }; + if (l_cp->correct) { + + /* if JPWL is on, we check whether there are too many subbands */ + if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) { + opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: bad number of subbands in Sqcx (%d)\n", + l_num_band); + if (!JPWL_ASSUME) { + opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + l_num_band = 1; + opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n" + "- setting number of bands to %d => HYPOTHESIS!!!\n", + l_num_band); + }; + + }; #endif /* USE_JPWL */ - if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { - for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { - opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */ - ++l_current_ptr; - if (l_band_no < J2K_MAXBANDS){ - l_tccp->stepsizes[l_band_no].expn = l_tmp>>3; - l_tccp->stepsizes[l_band_no].mant = 0; - } - } - *p_header_size = *p_header_size - l_num_band; - } - else { - for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { - opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */ - l_current_ptr+=2; - if (l_band_no < J2K_MAXBANDS){ - l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11; - l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff; - } - } - *p_header_size = *p_header_size - 2*l_num_band; - } - - /* Add Antonin : if scalar_derived -> compute other stepsizes */ - if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { - for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) { - l_tccp->stepsizes[l_band_no].expn = - ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ? - (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0; - l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant; - } - } - - return OPJ_TRUE; + if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { + for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { + opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */ + ++l_current_ptr; + if (l_band_no < J2K_MAXBANDS){ + l_tccp->stepsizes[l_band_no].expn = l_tmp>>3; + l_tccp->stepsizes[l_band_no].mant = 0; + } + } + *p_header_size = *p_header_size - l_num_band; + } + else { + for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) { + opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */ + l_current_ptr+=2; + if (l_band_no < J2K_MAXBANDS){ + l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11; + l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff; + } + } + *p_header_size = *p_header_size - 2*l_num_band; + } + + /* Add Antonin : if scalar_derived -> compute other stepsizes */ + if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { + for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) { + l_tccp->stepsizes[l_band_no].expn = + ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ? + (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0; + l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant; + } + } + + return OPJ_TRUE; } /** * Copies the tile component parameters of all the component from the first tile component. * - * @param p_j2k the J2k codec. + * @param p_j2k the J2k codec. */ void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k ) { - OPJ_UINT32 i; - opj_cp_v2_t *l_cp = NULL; - opj_tcp_v2_t *l_tcp = NULL; - opj_tccp_t *l_ref_tccp = NULL; - opj_tccp_t *l_copied_tccp = NULL; - OPJ_UINT32 l_size; - - /* preconditions */ - assert(p_j2k != 00); - - l_cp = &(p_j2k->m_cp); - l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? - &l_cp->tcps[p_j2k->m_current_tile_number] : - p_j2k->m_specific_param.m_decoder.m_default_tcp; - - l_ref_tccp = &l_tcp->tccps[0]; - l_copied_tccp = l_ref_tccp + 1; - l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t); - - for (i=1;im_private_image->numcomps;++i) { - l_copied_tccp->qntsty = l_ref_tccp->qntsty; - l_copied_tccp->numgbits = l_ref_tccp->numgbits; - memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size); - ++l_copied_tccp; - } + OPJ_UINT32 i; + opj_cp_v2_t *l_cp = NULL; + opj_tcp_v2_t *l_tcp = NULL; + opj_tccp_t *l_ref_tccp = NULL; + opj_tccp_t *l_copied_tccp = NULL; + OPJ_UINT32 l_size; + + /* preconditions */ + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ? + &l_cp->tcps[p_j2k->m_current_tile_number] : + p_j2k->m_specific_param.m_decoder.m_default_tcp; + + l_ref_tccp = &l_tcp->tccps[0]; + l_copied_tccp = l_ref_tccp + 1; + l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t); + + for (i=1;im_private_image->numcomps;++i) { + l_copied_tccp->qntsty = l_ref_tccp->qntsty; + l_copied_tccp->numgbits = l_ref_tccp->numgbits; + memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size); + ++l_copied_tccp; + } } /** * Dump some elements from the J2K decompression structure . * - *@param p_j2k the jpeg2000 codec. - *@param flag flag to describe what elments are dump. - *@param out_stream output stream where dump the elements. + *@param p_j2k the jpeg2000 codec. + *@param flag flag to describe what elments are dump. + *@param out_stream output stream where dump the elements. * */ void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream) { - /* Check if the flag is compatible with j2k file*/ - if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){ - fprintf(out_stream, "Wrong flag\n"); - return; - } + /* Check if the flag is compatible with j2k file*/ + if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){ + fprintf(out_stream, "Wrong flag\n"); + return; + } - /* Dump the image_header */ - if (flag & OPJ_IMG_INFO){ - if (p_j2k->m_private_image) - j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream); - } + /* Dump the image_header */ + if (flag & OPJ_IMG_INFO){ + if (p_j2k->m_private_image) + j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream); + } - /* Dump the codestream info from main header */ - if (flag & OPJ_J2K_MH_INFO){ - opj_j2k_dump_MH_info(p_j2k, out_stream); - } + /* Dump the codestream info from main header */ + if (flag & OPJ_J2K_MH_INFO){ + opj_j2k_dump_MH_info(p_j2k, out_stream); + } - /* Dump the codestream info of the current tile */ - if (flag & OPJ_J2K_TH_INFO){ + /* Dump the codestream info of the current tile */ + if (flag & OPJ_J2K_TH_INFO){ - } + } - /* Dump the codestream index from main header */ - if (flag & OPJ_J2K_MH_IND){ - opj_j2k_dump_MH_index(p_j2k, out_stream); - } + /* Dump the codestream index from main header */ + if (flag & OPJ_J2K_MH_IND){ + opj_j2k_dump_MH_index(p_j2k, out_stream); + } - /* Dump the codestream index of the current tile */ - if (flag & OPJ_J2K_TH_IND){ + /* Dump the codestream index of the current tile */ + if (flag & OPJ_J2K_TH_IND){ - } + } } /** * Dump index elements of the codestream extract from the main header. * - *@param p_j2k the jpeg2000 codec. - *@param out_stream output stream where dump the elements. + *@param p_j2k the jpeg2000 codec. + *@param out_stream output stream where dump the elements. * */ void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream) { - opj_codestream_index_t* cstr_index = p_j2k->cstr_index; - OPJ_UINT32 it_marker, it_tile, it_tile_part; + opj_codestream_index_t* cstr_index = p_j2k->cstr_index; + OPJ_UINT32 it_marker, it_tile, it_tile_part; - fprintf(out_stream, "Codestream index from main header: {\n"); + fprintf(out_stream, "Codestream index from main header: {\n"); - fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n" - "\t Main header end position=%" PRIi64 "\n", - cstr_index->main_head_start, cstr_index->main_head_end); + fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n" + "\t Main header end position=%" PRIi64 "\n", + cstr_index->main_head_start, cstr_index->main_head_end); - fprintf(out_stream, "\t Marker list: {\n"); + fprintf(out_stream, "\t Marker list: {\n"); - if (cstr_index->marker){ - for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){ - fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", - cstr_index->marker[it_marker].type, - cstr_index->marker[it_marker].pos, - cstr_index->marker[it_marker].len ); - } - } + if (cstr_index->marker){ + for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){ + fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", + cstr_index->marker[it_marker].type, + cstr_index->marker[it_marker].pos, + cstr_index->marker[it_marker].len ); + } + } - fprintf(out_stream, "\t }\n"); + fprintf(out_stream, "\t }\n"); - if (cstr_index->tile_index){ + if (cstr_index->tile_index){ /* Simple test to avoid to write empty information*/ OPJ_UINT32 l_acc_nb_of_tile_part = 0; for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){ - l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps; + l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps; } if (l_acc_nb_of_tile_part) { fprintf(out_stream, "\t Tile index: {\n"); - for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){ - OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps; - - fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part); - - if (cstr_index->tile_index[it_tile].tp_index){ - for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){ - fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n", - it_tile_part, - cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos, - cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header, - cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos); - } - } - - if (cstr_index->tile_index[it_tile].marker){ - for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){ - fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", - cstr_index->tile_index[it_tile].marker[it_marker].type, - cstr_index->tile_index[it_tile].marker[it_marker].pos, - cstr_index->tile_index[it_tile].marker[it_marker].len ); - } - } - } - fprintf(out_stream,"\t }\n"); + for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){ + OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps; + + fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part); + + if (cstr_index->tile_index[it_tile].tp_index){ + for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){ + fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n", + it_tile_part, + cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos, + cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header, + cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos); + } + } + + if (cstr_index->tile_index[it_tile].marker){ + for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){ + fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n", + cstr_index->tile_index[it_tile].marker[it_marker].type, + cstr_index->tile_index[it_tile].marker[it_marker].pos, + cstr_index->tile_index[it_tile].marker[it_marker].len ); + } + } + } + fprintf(out_stream,"\t }\n"); + } } - } - fprintf(out_stream,"}\n"); + fprintf(out_stream,"}\n"); } /** * Dump info elements of the codestream extract from the main header. * - *@param p_j2k the jpeg2000 codec. - *@param out_stream output stream where dump the elements. + *@param p_j2k the jpeg2000 codec. + *@param out_stream output stream where dump the elements. * */ void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream) { - opj_tcp_v2_t * l_default_tile=NULL; - - fprintf(out_stream, "Codestream info from main header: {\n"); + opj_tcp_v2_t * l_default_tile=NULL; - fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0); - fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy); - fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th); + fprintf(out_stream, "Codestream info from main header: {\n"); - l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp; - if (l_default_tile) - { - OPJ_INT32 compno; - OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps; + fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0); + fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy); + fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th); - fprintf(out_stream, "\t default tile {\n"); - fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty); - fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg); - fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers); - fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct); - - for (compno = 0; compno < numcomps; compno++) { - opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); - OPJ_UINT32 resno; + l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp; + if (l_default_tile) + { + OPJ_INT32 compno; + OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps; + + fprintf(out_stream, "\t default tile {\n"); + fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty); + fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg); + fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers); + fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct); + + for (compno = 0; compno < numcomps; compno++) { + opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); + OPJ_UINT32 resno; OPJ_INT32 bandno, numbands; - /* coding style*/ - fprintf(out_stream, "\t\t comp %d {\n", compno); - fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty); - fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions); - fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw); - fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh); - fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty); - fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid); - - fprintf(out_stream, "\t\t\t preccintsize (w,h)="); - for (resno = 0; resno < l_tccp->numresolutions; resno++) { - fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]); - } - fprintf(out_stream, "\n"); + /* coding style*/ + fprintf(out_stream, "\t\t comp %d {\n", compno); + fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty); + fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions); + fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw); + fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh); + fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty); + fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid); + + fprintf(out_stream, "\t\t\t preccintsize (w,h)="); + for (resno = 0; resno < l_tccp->numresolutions; resno++) { + fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]); + } + fprintf(out_stream, "\n"); + + /* quantization style*/ + fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty); + fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits); + fprintf(out_stream, "\t\t\t stepsizes (m,e)="); + numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2; + for (bandno = 0; bandno < numbands; bandno++) { + fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant, + l_tccp->stepsizes[bandno].expn); + } + fprintf(out_stream, "\n"); + + /* RGN value*/ + fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift); + + fprintf(out_stream, "\t\t }\n"); + } /*end of component of default tile*/ + fprintf(out_stream, "\t }\n"); /*end of default tile*/ - /* quantization style*/ - fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty); - fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits); - fprintf(out_stream, "\t\t\t stepsizes (m,e)="); - numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2; - for (bandno = 0; bandno < numbands; bandno++) { - fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant, - l_tccp->stepsizes[bandno].expn); - } - fprintf(out_stream, "\n"); - - /* RGN value*/ - fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift); - - fprintf(out_stream, "\t\t }\n"); - } /*end of component of default tile*/ - fprintf(out_stream, "\t }\n"); /*end of default tile*/ - - } + } - fprintf(out_stream, "}\n"); + fprintf(out_stream, "}\n"); } /** * Dump an image header structure. * - *@param img_header the image header to dump. - *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function - *@param out_stream output stream where dump the elements. + *@param img_header the image header to dump. + *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function + *@param out_stream output stream where dump the elements. */ void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream) { - char tab[2]; - - if (dev_dump_flag){ - fprintf(stdout, "[DEV] Dump an image_header struct {\n"); - tab[0] = '\0'; - } - else { - fprintf(out_stream, "Image info {\n"); - tab[0] = '\t';tab[1] = '\0'; - } + char tab[2]; - fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0); - fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1); - fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps); + if (dev_dump_flag){ + fprintf(stdout, "[DEV] Dump an image_header struct {\n"); + tab[0] = '\0'; + } + else { + fprintf(out_stream, "Image info {\n"); + tab[0] = '\t';tab[1] = '\0'; + } - if (img_header->comps){ - OPJ_UINT32 compno; - for (compno = 0; compno < img_header->numcomps; compno++) { - fprintf(out_stream, "%s\t component %d {\n", tab, compno); - j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream); - fprintf(out_stream,"%s}\n",tab); - } - } + fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0); + fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1); + fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps); + + if (img_header->comps){ + OPJ_UINT32 compno; + for (compno = 0; compno < img_header->numcomps; compno++) { + fprintf(out_stream, "%s\t component %d {\n", tab, compno); + j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream); + fprintf(out_stream,"%s}\n",tab); + } + } - fprintf(out_stream, "}\n"); + fprintf(out_stream, "}\n"); } /** * Dump a component image header structure. * - *@param comp_header the component image header to dump. - *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function - *@param out_stream output stream where dump the elements. + *@param comp_header the component image header to dump. + *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function + *@param out_stream output stream where dump the elements. */ void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream) { - char tab[3]; + char tab[3]; - if (dev_dump_flag){ - fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n"); - tab[0] = '\0'; - } else { - tab[0] = '\t';tab[1] = '\t';tab[2] = '\0'; - } + if (dev_dump_flag){ + fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n"); + tab[0] = '\0'; + } else { + tab[0] = '\t';tab[1] = '\t';tab[2] = '\0'; + } - fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy); - fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec); - fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd); + fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy); + fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec); + fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd); - if (dev_dump_flag) - fprintf(out_stream, "}\n"); + if (dev_dump_flag) + fprintf(out_stream, "}\n"); } /** * Get the codestream info from a JPEG2000 codec. * - *@param p_j2k the component image header to dump. + *@param p_j2k the component image header to dump. * - *@return the codestream information extract from the jpg2000 codec + *@return the codestream information extract from the jpg2000 codec */ opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k) { - OPJ_UINT16 compno; - OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps; - opj_tcp_v2_t *l_default_tile; - opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t)); - - cstr_info->nbcomps = p_j2k->m_private_image->numcomps; - - cstr_info->tx0 = p_j2k->m_cp.tx0; - cstr_info->ty0 = p_j2k->m_cp.ty0; - cstr_info->tdx = p_j2k->m_cp.tdx; - cstr_info->tdy = p_j2k->m_cp.tdy; - cstr_info->tw = p_j2k->m_cp.tw; - cstr_info->th = p_j2k->m_cp.th; - - cstr_info->tile_info = NULL; /* Not fill from the main header*/ - - l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp; - - cstr_info->m_default_tile_info.csty = l_default_tile->csty; - cstr_info->m_default_tile_info.prg = l_default_tile->prg; - cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers; - cstr_info->m_default_tile_info.mct = l_default_tile->mct; - - cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t)); - - for (compno = 0; compno < numcomps; compno++) { - opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); - opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]); - OPJ_INT32 bandno, numbands; - - /* coding style*/ - l_tccp_info->csty = l_tccp->csty; - l_tccp_info->numresolutions = l_tccp->numresolutions; - l_tccp_info->cblkw = l_tccp->cblkw; - l_tccp_info->cblkh = l_tccp->cblkh; - l_tccp_info->cblksty = l_tccp->cblksty; - l_tccp_info->qmfbid = l_tccp->qmfbid; - if (l_tccp->numresolutions < J2K_MAXRLVLS) - { - memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions); - memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions); - } - - /* quantization style*/ - l_tccp_info->qntsty = l_tccp->qntsty; - l_tccp_info->numgbits = l_tccp->numgbits; - - numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2; - if (numbands < J2K_MAXBANDS) { - for (bandno = 0; bandno < numbands; bandno++) { - l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant; - l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn; - } - } - - /* RGN value*/ - l_tccp_info->roishift = l_tccp->roishift; - } + OPJ_UINT16 compno; + OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps; + opj_tcp_v2_t *l_default_tile; + opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t)); + + cstr_info->nbcomps = p_j2k->m_private_image->numcomps; + + cstr_info->tx0 = p_j2k->m_cp.tx0; + cstr_info->ty0 = p_j2k->m_cp.ty0; + cstr_info->tdx = p_j2k->m_cp.tdx; + cstr_info->tdy = p_j2k->m_cp.tdy; + cstr_info->tw = p_j2k->m_cp.tw; + cstr_info->th = p_j2k->m_cp.th; + + cstr_info->tile_info = NULL; /* Not fill from the main header*/ + + l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp; + + cstr_info->m_default_tile_info.csty = l_default_tile->csty; + cstr_info->m_default_tile_info.prg = l_default_tile->prg; + cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers; + cstr_info->m_default_tile_info.mct = l_default_tile->mct; + + cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t)); + + for (compno = 0; compno < numcomps; compno++) { + opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]); + opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]); + OPJ_INT32 bandno, numbands; + + /* coding style*/ + l_tccp_info->csty = l_tccp->csty; + l_tccp_info->numresolutions = l_tccp->numresolutions; + l_tccp_info->cblkw = l_tccp->cblkw; + l_tccp_info->cblkh = l_tccp->cblkh; + l_tccp_info->cblksty = l_tccp->cblksty; + l_tccp_info->qmfbid = l_tccp->qmfbid; + if (l_tccp->numresolutions < J2K_MAXRLVLS) + { + memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions); + memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions); + } + + /* quantization style*/ + l_tccp_info->qntsty = l_tccp->qntsty; + l_tccp_info->numgbits = l_tccp->numgbits; + + numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2; + if (numbands < J2K_MAXBANDS) { + for (bandno = 0; bandno < numbands; bandno++) { + l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant; + l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn; + } + } + + /* RGN value*/ + l_tccp_info->roishift = l_tccp->roishift; + } - return cstr_info; + return cstr_info; } /** * Get the codestream index from a JPEG2000 codec. * - *@param p_j2k the component image header to dump. + *@param p_j2k the component image header to dump. * - *@return the codestream index extract from the jpg2000 codec + *@return the codestream index extract from the jpg2000 codec */ opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k) { - opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*) - opj_calloc(1,sizeof(opj_codestream_index_t)); - if (!l_cstr_index) - return NULL; - - l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start; - l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end; - l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size; - - l_cstr_index->marknum = p_j2k->cstr_index->marknum; - l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t)); - if (!l_cstr_index->marker){ - opj_free( l_cstr_index); - return NULL; - } - - if (p_j2k->cstr_index->marker) - memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) ); - else{ - opj_free(l_cstr_index->marker); - l_cstr_index->marker = NULL; - } - - l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles; - l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) ); - if (!l_cstr_index->tile_index){ - opj_free( l_cstr_index->marker); - opj_free( l_cstr_index); - return NULL; - } - - if (!p_j2k->cstr_index->tile_index){ - opj_free(l_cstr_index->tile_index); - l_cstr_index->tile_index = NULL; - } - else { - OPJ_UINT32 it_tile = 0; - for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){ - - /* Tile Marker*/ - l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum; - - l_cstr_index->tile_index[it_tile].marker = - (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t)); - - if (!l_cstr_index->tile_index[it_tile].marker) { - OPJ_UINT32 it_tile_free; - - for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){ - opj_free(l_cstr_index->tile_index[it_tile_free].marker); - } - - opj_free( l_cstr_index->tile_index); - opj_free( l_cstr_index->marker); - opj_free( l_cstr_index); - return NULL; - } - - if (p_j2k->cstr_index->tile_index[it_tile].marker) - memcpy( l_cstr_index->tile_index[it_tile].marker, - p_j2k->cstr_index->tile_index[it_tile].marker, - l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) ); - else{ - opj_free(l_cstr_index->tile_index[it_tile].marker); - l_cstr_index->tile_index[it_tile].marker = NULL; - } - - /* Tile part index*/ - l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps; - - l_cstr_index->tile_index[it_tile].tp_index = - (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t)); - - if(!l_cstr_index->tile_index[it_tile].tp_index){ - OPJ_UINT32 it_tile_free; - - for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){ - opj_free(l_cstr_index->tile_index[it_tile_free].marker); - opj_free(l_cstr_index->tile_index[it_tile_free].tp_index); - } - - opj_free( l_cstr_index->tile_index); - opj_free( l_cstr_index->marker); - opj_free( l_cstr_index); - return NULL; - } - - if (p_j2k->cstr_index->tile_index[it_tile].tp_index){ - memcpy( l_cstr_index->tile_index[it_tile].tp_index, - p_j2k->cstr_index->tile_index[it_tile].tp_index, - l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) ); - } - else{ - opj_free(l_cstr_index->tile_index[it_tile].tp_index); - l_cstr_index->tile_index[it_tile].tp_index = NULL; - } - - /* Packet index (NOT USED)*/ - l_cstr_index->tile_index[it_tile].nb_packet = 0; - l_cstr_index->tile_index[it_tile].packet_index = NULL; - - } - } - - return l_cstr_index; + opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*) + opj_calloc(1,sizeof(opj_codestream_index_t)); + if (!l_cstr_index) + return NULL; + + l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start; + l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end; + l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size; + + l_cstr_index->marknum = p_j2k->cstr_index->marknum; + l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t)); + if (!l_cstr_index->marker){ + opj_free( l_cstr_index); + return NULL; + } + + if (p_j2k->cstr_index->marker) + memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) ); + else{ + opj_free(l_cstr_index->marker); + l_cstr_index->marker = NULL; + } + + l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles; + l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) ); + if (!l_cstr_index->tile_index){ + opj_free( l_cstr_index->marker); + opj_free( l_cstr_index); + return NULL; + } + + if (!p_j2k->cstr_index->tile_index){ + opj_free(l_cstr_index->tile_index); + l_cstr_index->tile_index = NULL; + } + else { + OPJ_UINT32 it_tile = 0; + for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){ + + /* Tile Marker*/ + l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum; + + l_cstr_index->tile_index[it_tile].marker = + (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t)); + + if (!l_cstr_index->tile_index[it_tile].marker) { + OPJ_UINT32 it_tile_free; + + for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){ + opj_free(l_cstr_index->tile_index[it_tile_free].marker); + } + + opj_free( l_cstr_index->tile_index); + opj_free( l_cstr_index->marker); + opj_free( l_cstr_index); + return NULL; + } + + if (p_j2k->cstr_index->tile_index[it_tile].marker) + memcpy( l_cstr_index->tile_index[it_tile].marker, + p_j2k->cstr_index->tile_index[it_tile].marker, + l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) ); + else{ + opj_free(l_cstr_index->tile_index[it_tile].marker); + l_cstr_index->tile_index[it_tile].marker = NULL; + } + + /* Tile part index*/ + l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps; + + l_cstr_index->tile_index[it_tile].tp_index = + (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t)); + + if(!l_cstr_index->tile_index[it_tile].tp_index){ + OPJ_UINT32 it_tile_free; + + for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){ + opj_free(l_cstr_index->tile_index[it_tile_free].marker); + opj_free(l_cstr_index->tile_index[it_tile_free].tp_index); + } + + opj_free( l_cstr_index->tile_index); + opj_free( l_cstr_index->marker); + opj_free( l_cstr_index); + return NULL; + } + + if (p_j2k->cstr_index->tile_index[it_tile].tp_index){ + memcpy( l_cstr_index->tile_index[it_tile].tp_index, + p_j2k->cstr_index->tile_index[it_tile].tp_index, + l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) ); + } + else{ + opj_free(l_cstr_index->tile_index[it_tile].tp_index); + l_cstr_index->tile_index[it_tile].tp_index = NULL; + } + + /* Packet index (NOT USED)*/ + l_cstr_index->tile_index[it_tile].nb_packet = 0; + l_cstr_index->tile_index[it_tile].packet_index = NULL; + + } + } + + return l_cstr_index; } opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k) { - OPJ_UINT32 it_tile=0; + OPJ_UINT32 it_tile=0; - p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; - p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); - if (!p_j2k->cstr_index->tile_index) - return OPJ_FALSE; + p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th; + p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); + if (!p_j2k->cstr_index->tile_index) + return OPJ_FALSE; - for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){ - p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100; - p_j2k->cstr_index->tile_index[it_tile].marknum = 0; - p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*) - opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t)); - if (!p_j2k->cstr_index->tile_index[it_tile].marker) - return OPJ_FALSE; - } + for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){ + p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100; + p_j2k->cstr_index->tile_index[it_tile].marknum = 0; + p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*) + opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t)); + if (!p_j2k->cstr_index->tile_index[it_tile].marker) + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads the tiles. */ -opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager) -{ - opj_bool l_go_on = OPJ_TRUE; - OPJ_UINT32 l_current_tile_no; - OPJ_UINT32 l_data_size,l_max_data_size; - OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1; - OPJ_UINT32 l_nb_comps; - OPJ_BYTE * l_current_data; - - l_current_data = (OPJ_BYTE*)opj_malloc(1000); - if (! l_current_data) { - return OPJ_FALSE; - } - l_max_data_size = 1000; - - - - while (OPJ_TRUE) { - if (! opj_j2k_read_tile_header( p_j2k, - &l_current_tile_no, - &l_data_size, - &l_tile_x0, &l_tile_y0, - &l_tile_x1, &l_tile_y1, - &l_nb_comps, - &l_go_on, - p_stream, - p_manager)) { - opj_free(l_current_data); - return OPJ_FALSE; - } - - if (! l_go_on) { - break; - } - - if (l_data_size > l_max_data_size) { - l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size); - if (! l_current_data) { - opj_free(l_current_data); - return OPJ_FALSE; - } - - l_max_data_size = l_data_size; - } - - if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) { - opj_free(l_current_data); - return OPJ_FALSE; - } - opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw); - - if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) { - opj_free(l_current_data); - return OPJ_FALSE; - } - opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); +opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + opj_bool l_go_on = OPJ_TRUE; + OPJ_UINT32 l_current_tile_no; + OPJ_UINT32 l_data_size,l_max_data_size; + OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1; + OPJ_UINT32 l_nb_comps; + OPJ_BYTE * l_current_data; + + l_current_data = (OPJ_BYTE*)opj_malloc(1000); + if (! l_current_data) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to decode tiles\n"); + return OPJ_FALSE; + } + l_max_data_size = 1000; + + + + while (OPJ_TRUE) { + if (! opj_j2k_read_tile_header( p_j2k, + &l_current_tile_no, + &l_data_size, + &l_tile_x0, &l_tile_y0, + &l_tile_x1, &l_tile_y1, + &l_nb_comps, + &l_go_on, + p_stream, + p_manager)) { + opj_free(l_current_data); + return OPJ_FALSE; + } + + if (! l_go_on) { + break; + } + + if (l_data_size > l_max_data_size) { + OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size); + if (! l_new_current_data) { + opj_free(l_current_data); + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw); + return OPJ_FALSE; + } + l_current_data = l_new_current_data; + l_max_data_size = l_data_size; + } + + if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) { + opj_free(l_current_data); + return OPJ_FALSE; + } + opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw); + + if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) { + opj_free(l_current_data); + return OPJ_FALSE; + } + opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); - } + } - opj_free(l_current_data); + opj_free(l_current_data); - return OPJ_TRUE; + return OPJ_TRUE; } /** @@ -9164,128 +9308,133 @@ opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k, */ static void opj_j2k_setup_decoding (opj_j2k_v2_t *p_j2k) { - /* preconditions*/ - assert(p_j2k != 00); + /* preconditions*/ + assert(p_j2k != 00); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles); - /* DEVELOPER CORNER, add your custom procedures */ + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles); + /* DEVELOPER CORNER, add your custom procedures */ } /* * Read and decode one tile. */ -static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager) -{ - opj_bool l_go_on = OPJ_TRUE; - OPJ_UINT32 l_current_tile_no; - OPJ_UINT32 l_tile_no_to_dec; - OPJ_UINT32 l_data_size,l_max_data_size; - OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1; - OPJ_UINT32 l_nb_comps; - OPJ_BYTE * l_current_data; - - l_current_data = (OPJ_BYTE*)opj_malloc(1000); - if (! l_current_data) { - return OPJ_FALSE; - } - l_max_data_size = 1000; - - /*Allocate and initialize some elements of codestrem index if not already done*/ - if( !p_j2k->cstr_index->tile_index) - { - if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){ - opj_free(l_current_data); - return OPJ_FALSE; - } - } - /* Move into the codestream to the first SOT used to decode the desired tile */ - l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec; - if (p_j2k->cstr_index->tile_index) - if(p_j2k->cstr_index->tile_index->tp_index) - { - if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) { - /* the index for this tile has not been built, - * so move to the last SOT read */ - if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){ - opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n"); - return OPJ_FALSE; - } - } - else{ - if ( !(opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) ) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n"); - return OPJ_FALSE; - } - } - /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */ - if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) - p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; - } - - while (OPJ_TRUE) { - if (! opj_j2k_read_tile_header( p_j2k, - &l_current_tile_no, - &l_data_size, - &l_tile_x0, &l_tile_y0, - &l_tile_x1, &l_tile_y1, - &l_nb_comps, - &l_go_on, - p_stream, - p_manager)) { - opj_free(l_current_data); - return OPJ_FALSE; - } - - - if (! l_go_on) { - break; - } - - if (l_data_size > l_max_data_size) { - l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size); - if (! l_current_data) { - opj_free(l_current_data); - return OPJ_FALSE; - } - - l_max_data_size = l_data_size; - } - - - - if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) { - opj_free(l_current_data); - return OPJ_FALSE; - } - opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1); - - if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) { - opj_free(l_current_data); - return OPJ_FALSE; - } - opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no); - - if(l_current_tile_no == l_tile_no_to_dec) - { - /* move into the codestream to the the first SOT (FIXME or not move?)*/ - if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n"); - return OPJ_FALSE; - } - break; - } - else { - opj_event_msg_v2(p_manager, EVT_WARNING, "Tile read, decode and updated is not the desired (%d vs %d).\n", l_current_tile_no, l_tile_no_to_dec); - } - - } - - opj_free(l_current_data); - - return OPJ_TRUE; +static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) +{ + opj_bool l_go_on = OPJ_TRUE; + OPJ_UINT32 l_current_tile_no; + OPJ_UINT32 l_tile_no_to_dec; + OPJ_UINT32 l_data_size,l_max_data_size; + OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1; + OPJ_UINT32 l_nb_comps; + OPJ_BYTE * l_current_data; + + l_current_data = (OPJ_BYTE*)opj_malloc(1000); + if (! l_current_data) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to decode one tile\n"); + return OPJ_FALSE; + } + l_max_data_size = 1000; + + /*Allocate and initialize some elements of codestrem index if not already done*/ + if( !p_j2k->cstr_index->tile_index) + { + if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){ + opj_free(l_current_data); + return OPJ_FALSE; + } + } + /* Move into the codestream to the first SOT used to decode the desired tile */ + l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec; + if (p_j2k->cstr_index->tile_index) + if(p_j2k->cstr_index->tile_index->tp_index) + { + if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) { + /* the index for this tile has not been built, + * so move to the last SOT read */ + if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){ + opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n"); + return OPJ_FALSE; + } + } + else{ + if ( !(opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) ) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n"); + return OPJ_FALSE; + } + } + /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */ + if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; + } + + while (OPJ_TRUE) { + if (! opj_j2k_read_tile_header( p_j2k, + &l_current_tile_no, + &l_data_size, + &l_tile_x0, &l_tile_y0, + &l_tile_x1, &l_tile_y1, + &l_nb_comps, + &l_go_on, + p_stream, + p_manager)) { + opj_free(l_current_data); + return OPJ_FALSE; + } + + + if (! l_go_on) { + break; + } + + if (l_data_size > l_max_data_size) { + OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size); + if (! l_new_current_data) { + opj_free(l_current_data); + l_current_data = NULL; + // TODO: LH: why tile numbering policy used in messages differs from + // the one used in opj_j2k_decode_tiles() ? + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1); + return OPJ_FALSE; + } + l_current_data = l_new_current_data; + l_max_data_size = l_data_size; + } + + + + if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) { + opj_free(l_current_data); + return OPJ_FALSE; + } + opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1); + + if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) { + opj_free(l_current_data); + return OPJ_FALSE; + } + opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no); + + if(l_current_tile_no == l_tile_no_to_dec) + { + /* move into the codestream to the the first SOT (FIXME or not move?)*/ + if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n"); + return OPJ_FALSE; + } + break; + } + else { + opj_event_msg_v2(p_manager, EVT_WARNING, "Tile read, decode and updated is not the desired (%d vs %d).\n", l_current_tile_no, l_tile_no_to_dec); + } + + } + + opj_free(l_current_data); + + return OPJ_TRUE; } @@ -9294,11 +9443,11 @@ static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k, */ static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k) { - /* preconditions*/ - assert(p_j2k != 00); + /* preconditions*/ + assert(p_j2k != 00); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile); - /* DEVELOPER CORNER, add your custom procedures */ + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile); + /* DEVELOPER CORNER, add your custom procedures */ } @@ -9307,326 +9456,330 @@ static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k) * Decodes the tiles of the stream. */ opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k, - opj_stream_private_t * p_stream, - opj_image_t * p_image, - opj_event_mgr_t * p_manager) + opj_stream_private_t * p_stream, + opj_image_t * p_image, + opj_event_mgr_t * p_manager) { - OPJ_UINT32 compno; + OPJ_UINT32 compno; - if (!p_image) - return OPJ_FALSE; + if (!p_image) + return OPJ_FALSE; - p_j2k->m_output_image = opj_image_create0(); - if (! (p_j2k->m_output_image)) { - return OPJ_FALSE; - } - opj_copy_image_header(p_image, p_j2k->m_output_image); + p_j2k->m_output_image = opj_image_create0(); + if (! (p_j2k->m_output_image)) { + return OPJ_FALSE; + } + opj_copy_image_header(p_image, p_j2k->m_output_image); - /* customization of the decoding */ - opj_j2k_setup_decoding(p_j2k); + /* customization of the decoding */ + opj_j2k_setup_decoding(p_j2k); - /* Decode the codestream */ - if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { - opj_image_destroy(p_j2k->m_private_image); - p_j2k->m_private_image = NULL; - return OPJ_FALSE; - } + /* Decode the codestream */ + if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } - /* Move data and copy one information from codec to output image*/ - for (compno = 0; compno < p_image->numcomps; compno++) { - p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded; - p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; - p_j2k->m_output_image->comps[compno].data = NULL; - } + /* Move data and copy one information from codec to output image*/ + for (compno = 0; compno < p_image->numcomps; compno++) { + p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded; + p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; + p_j2k->m_output_image->comps[compno].data = NULL; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Get the decoded tile. * - * @param p_j2k the jpeg2000 codestream codec. - * @param p_stream input_stream - * @param p_image output image. . - * @param p_manager the user event manager - * @param tile_index index of the tile we want decode + * @param p_j2k the jpeg2000 codestream codec. + * @param p_stream input_stream + * @param p_image output image. . + * @param p_manager the user event manager + * @param tile_index index of the tile we want decode * - * @return true if succeed. + * @return true if succeed. */ -opj_bool opj_j2k_get_tile( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_image_t* p_image, - opj_event_mgr_t * p_manager, - OPJ_UINT32 tile_index ) -{ - OPJ_UINT32 compno; - OPJ_UINT32 l_tile_x, l_tile_y; - opj_image_comp_t* l_img_comp; - - if (!p_image) { - opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n"); - return OPJ_FALSE; - } - - if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){ - opj_event_msg_v2(p_manager, EVT_ERROR, "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1); - return OPJ_FALSE; - } - - /* Compute the dimension of the desired tile*/ - l_tile_x = tile_index % p_j2k->m_cp.tw; - l_tile_y = tile_index / p_j2k->m_cp.tw; - - p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; - if (p_image->x0 < p_j2k->m_private_image->x0) - p_image->x0 = p_j2k->m_private_image->x0; - p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; - if (p_image->x1 > p_j2k->m_private_image->x1) - p_image->x1 = p_j2k->m_private_image->x1; +opj_bool opj_j2k_get_tile( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_image_t* p_image, + opj_event_mgr_t * p_manager, + OPJ_UINT32 tile_index ) +{ + OPJ_UINT32 compno; + OPJ_UINT32 l_tile_x, l_tile_y; + opj_image_comp_t* l_img_comp; + + if (!p_image) { + opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n"); + return OPJ_FALSE; + } - p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; - if (p_image->y0 < p_j2k->m_private_image->y0) - p_image->y0 = p_j2k->m_private_image->y0; - p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; - if (p_image->y1 > p_j2k->m_private_image->y1) - p_image->y1 = p_j2k->m_private_image->y1; + if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){ + opj_event_msg_v2(p_manager, EVT_ERROR, "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1); + return OPJ_FALSE; + } - l_img_comp = p_image->comps; - for (compno=0; compno < p_image->numcomps; ++compno) - { - OPJ_INT32 l_comp_x1, l_comp_y1; + /* Compute the dimension of the desired tile*/ + l_tile_x = tile_index % p_j2k->m_cp.tw; + l_tile_y = tile_index / p_j2k->m_cp.tw; + + p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; + if (p_image->x0 < p_j2k->m_private_image->x0) + p_image->x0 = p_j2k->m_private_image->x0; + p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0; + if (p_image->x1 > p_j2k->m_private_image->x1) + p_image->x1 = p_j2k->m_private_image->x1; + + p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; + if (p_image->y0 < p_j2k->m_private_image->y0) + p_image->y0 = p_j2k->m_private_image->y0; + p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0; + if (p_image->y1 > p_j2k->m_private_image->y1) + p_image->y1 = p_j2k->m_private_image->y1; + + l_img_comp = p_image->comps; + for (compno=0; compno < p_image->numcomps; ++compno) + { + OPJ_INT32 l_comp_x1, l_comp_y1; - l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor; + l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor; - l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx); - l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy); - l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx); - l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy); + l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx); + l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy); + l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx); + l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy); - l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor); - l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor); + l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor); + l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor); - l_img_comp++; - } + l_img_comp++; + } - /* Destroy the previous output image*/ - if (p_j2k->m_output_image) - opj_image_destroy(p_j2k->m_output_image); + /* Destroy the previous output image*/ + if (p_j2k->m_output_image) + opj_image_destroy(p_j2k->m_output_image); - /* Create the ouput image from the information previously computed*/ - p_j2k->m_output_image = opj_image_create0(); - if (! (p_j2k->m_output_image)) { - return OPJ_FALSE; - } - opj_copy_image_header(p_image, p_j2k->m_output_image); + /* Create the ouput image from the information previously computed*/ + p_j2k->m_output_image = opj_image_create0(); + if (! (p_j2k->m_output_image)) { + return OPJ_FALSE; + } + opj_copy_image_header(p_image, p_j2k->m_output_image); - p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index; + p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index; - /* customization of the decoding */ - opj_j2k_setup_decoding_tile(p_j2k); + /* customization of the decoding */ + opj_j2k_setup_decoding_tile(p_j2k); - /* Decode the codestream */ - if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { - opj_image_destroy(p_j2k->m_private_image); - p_j2k->m_private_image = NULL; - return OPJ_FALSE; - } + /* Decode the codestream */ + if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { + opj_image_destroy(p_j2k->m_private_image); + p_j2k->m_private_image = NULL; + return OPJ_FALSE; + } - /* Move data and copy one information from codec to output image*/ - for (compno = 0; compno < p_image->numcomps; compno++) { - p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded; + /* Move data and copy one information from codec to output image*/ + for (compno = 0; compno < p_image->numcomps; compno++) { + p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded; - if (p_image->comps[compno].data) - opj_free(p_image->comps[compno].data); + if (p_image->comps[compno].data) + opj_free(p_image->comps[compno].data); - p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; + p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; - p_j2k->m_output_image->comps[compno].data = NULL; - } + p_j2k->m_output_image->comps[compno].data = NULL; + } - return OPJ_TRUE; + return OPJ_TRUE; } opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager) { - OPJ_UINT32 it_comp; - - p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor; - - if (p_j2k->m_private_image) { - if (p_j2k->m_private_image->comps) { - if (p_j2k->m_specific_param.m_decoder.m_default_tcp) { - if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) { - for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) { - OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions; - if ( res_factor >= max_res){ - opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n"); - return OPJ_FALSE; - } - p_j2k->m_private_image->comps[it_comp].factor = res_factor; - } - return OPJ_TRUE; - } - } - } - } + OPJ_UINT32 it_comp; + + p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor; + + if (p_j2k->m_private_image) { + if (p_j2k->m_private_image->comps) { + if (p_j2k->m_specific_param.m_decoder.m_default_tcp) { + if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) { + for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) { + OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions; + if ( res_factor >= max_res){ + opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n"); + return OPJ_FALSE; + } + p_j2k->m_private_image->comps[it_comp].factor = res_factor; + } + return OPJ_TRUE; + } + } + } + } - return OPJ_FALSE; + return OPJ_FALSE; } /** * Encodes all the tiles in a row. */ -opj_bool opj_j2k_encode_v2( opj_j2k_v2_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - OPJ_UINT32 i; - OPJ_UINT32 l_nb_tiles; - OPJ_UINT32 l_max_tile_size, l_current_tile_size; - OPJ_BYTE * l_current_data; - - /* preconditions */ - assert(p_j2k != 00); - assert(p_stream != 00); - assert(p_manager != 00); - - l_current_data = (OPJ_BYTE*)opj_malloc(1000); - if (! l_current_data) { - return OPJ_FALSE; - } - l_max_tile_size = 1000; - - l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; - for (i=0;im_tcd); - if (l_current_tile_size > l_max_tile_size) { - l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size); - if (! l_current_data) { - return OPJ_FALSE; - } - l_max_tile_size = l_current_tile_size; - } - - opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data); - - if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) { - return OPJ_FALSE; - } - } - - opj_free(l_current_data); - return OPJ_TRUE; +opj_bool opj_j2k_encode_v2( opj_j2k_v2_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 l_max_tile_size, l_current_tile_size; + OPJ_BYTE * l_current_data; + + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + l_current_data = (OPJ_BYTE*)opj_malloc(1000); + if (! l_current_data) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n"); + return OPJ_FALSE; + } + l_max_tile_size = 1000; + + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + for (i=0;im_tcd); + if (l_current_tile_size > l_max_tile_size) { + OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size); + if (! l_new_current_data) { + opj_free(l_current_data); + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n"); + return OPJ_FALSE; + } + l_current_data = l_new_current_data; + l_max_tile_size = l_current_tile_size; + } + + opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data); + + if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) { + return OPJ_FALSE; + } + } + + opj_free(l_current_data); + return OPJ_TRUE; } /** * Ends the compression procedures and possibility add data to be read after the * codestream. */ -opj_bool opj_j2k_end_compress( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager) +opj_bool opj_j2k_end_compress( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager) { - /* customization of the encoding */ - opj_j2k_setup_end_compress(p_j2k); + /* customization of the encoding */ + opj_j2k_setup_end_compress(p_j2k); - if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) - { - return OPJ_FALSE; - } + if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) + { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Starts a compression scheme, i.e. validates the codec parameters, writes the header. * - * @param p_j2k the jpeg2000 codec. - * @param p_stream the stream object. - * @param p_manager the user event manager. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream object. + * @param p_manager the user event manager. * * @return true if the codec is valid. */ opj_bool opj_j2k_start_compress(opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_image_t * p_image, - opj_event_mgr_t * p_manager) -{ - /* preconditions */ - assert(p_j2k != 00); - assert(p_stream != 00); - assert(p_manager != 00); - - p_j2k->m_private_image = opj_image_create0(); - opj_copy_image_header(p_image, p_j2k->m_private_image); - - // TODO_MSD: Find a better way - if (p_image->comps) { - OPJ_UINT32 it_comp; - for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) { - if (p_image->comps[it_comp].data) { - p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data; - p_image->comps[it_comp].data = NULL; - - } - } - } + opj_stream_private_t *p_stream, + opj_image_t * p_image, + opj_event_mgr_t * p_manager) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + p_j2k->m_private_image = opj_image_create0(); + opj_copy_image_header(p_image, p_j2k->m_private_image); + + // TODO_MSD: Find a better way + if (p_image->comps) { + OPJ_UINT32 it_comp; + for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) { + if (p_image->comps[it_comp].data) { + p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data; + p_image->comps[it_comp].data = NULL; + + } + } + } - /* customization of the validation */ - opj_j2k_setup_encoding_validation (p_j2k); + /* customization of the validation */ + opj_j2k_setup_encoding_validation (p_j2k); - /* validation of the parameters codec */ - if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) { - return OPJ_FALSE; - } + /* validation of the parameters codec */ + if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) { + return OPJ_FALSE; + } - /* customization of the encoding */ - opj_j2k_setup_header_writting(p_j2k); + /* customization of the encoding */ + opj_j2k_setup_header_writting(p_j2k); - /* write header */ - if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { - return OPJ_FALSE; - } + /* write header */ + if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /* * */ -opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k, - OPJ_UINT32 p_tile_index, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k, + OPJ_UINT32 p_tile_index, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { (void)p_stream; - if (p_tile_index != p_j2k->m_current_tile_number) { - opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." ); - return OPJ_FALSE; - } + if (p_tile_index != p_j2k->m_current_tile_number) { + opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." ); + return OPJ_FALSE; + } - opj_event_msg_v2(p_manager, EVT_INFO, "tile number %d / %d\n", p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th); + opj_event_msg_v2(p_manager, EVT_INFO, "tile number %d / %d\n", p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th); - p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0; - p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts; - p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; + p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0; + p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts; + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; - /* initialisation before tile encoding */ - if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) { - return OPJ_FALSE; - } + /* initialisation before tile encoding */ + if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /* @@ -9634,168 +9787,168 @@ opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k, */ void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data) { - OPJ_UINT32 i,j,k = 0; - OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width; - opj_image_comp_t * l_img_comp = 00; - opj_tcd_tilecomp_v2_t * l_tilec = 00; - opj_image_t * l_image = 00; - OPJ_UINT32 l_size_comp, l_remaining; - OPJ_INT32 * l_src_ptr; - l_tilec = p_tcd->tcd_image->tiles->comps; - l_image = p_tcd->image; - l_img_comp = l_image->comps; - - for (i=0;iimage->numcomps;++i) { - l_size_comp = l_img_comp->prec >> 3; /* (/8) */ - l_remaining = l_img_comp->prec & 7; /* (%8) */ - if (l_remaining) { - ++l_size_comp; - } - - if (l_size_comp == 3) { - l_size_comp = 4; - } - - l_width = (l_tilec->x1 - l_tilec->x0); - l_height = (l_tilec->y1 - l_tilec->y0); - l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx); - l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy); - l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx); - l_stride = l_image_width - l_width; - l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width; - - switch (l_size_comp) { - case 1: - { - OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data; - if (l_img_comp->sgnd) { - for (j=0;jsgnd) { - for (j=0;jtcd_image->tiles->comps; + l_image = p_tcd->image; + l_img_comp = l_image->comps; + + for (i=0;iimage->numcomps;++i) { + l_size_comp = l_img_comp->prec >> 3; /* (/8) */ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + if (l_remaining) { + ++l_size_comp; + } + + if (l_size_comp == 3) { + l_size_comp = 4; + } + + l_width = (l_tilec->x1 - l_tilec->x0); + l_height = (l_tilec->y1 - l_tilec->y0); + l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx); + l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy); + l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx); + l_stride = l_image_width - l_width; + l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width; + + switch (l_size_comp) { + case 1: + { + OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data; + if (l_img_comp->sgnd) { + for (j=0;jsgnd) { + for (j=0;jm_specific_param.m_encoder.m_encoded_tile_data); - - l_tcd = p_j2k->m_tcd; - l_cp = &(p_j2k->m_cp); - l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; - - l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size; - l_available_data = l_tile_size; - l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data; - - if (! opj_tcd_copy_tile_data(l_tcd,p_data,p_data_size)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." ); - return OPJ_FALSE; - } +opj_bool opj_j2k_post_write_tile ( opj_j2k_v2_t * p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + opj_tcd_v2_t * l_tcd = 00; + opj_cp_v2_t * l_cp = 00; + opj_tcp_v2_t * l_tcp = 00; + OPJ_UINT32 l_nb_bytes_written; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_tile_size = 0; + OPJ_UINT32 l_available_data; + + /* preconditions */ + assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); + + l_tcd = p_j2k->m_tcd; + l_cp = &(p_j2k->m_cp); + l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; + + l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size; + l_available_data = l_tile_size; + l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data; + + if (! opj_tcd_copy_tile_data(l_tcd,p_data,p_data_size)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." ); + return OPJ_FALSE; + } - l_nb_bytes_written = 0; - if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) { - return OPJ_FALSE; - } - l_current_data += l_nb_bytes_written; - l_available_data -= l_nb_bytes_written; + l_nb_bytes_written = 0; + if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) { + return OPJ_FALSE; + } + l_current_data += l_nb_bytes_written; + l_available_data -= l_nb_bytes_written; - l_nb_bytes_written = 0; - if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) { - return OPJ_FALSE; - } + l_nb_bytes_written = 0; + if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) { + return OPJ_FALSE; + } - l_available_data -= l_nb_bytes_written; - l_nb_bytes_written = l_tile_size - l_available_data; + l_available_data -= l_nb_bytes_written; + l_nb_bytes_written = l_tile_size - l_available_data; - if ( opj_stream_write_data( p_stream, - p_j2k->m_specific_param.m_encoder.m_encoded_tile_data, - l_nb_bytes_written,p_manager) != l_nb_bytes_written) { - return OPJ_FALSE; - } + if ( opj_stream_write_data( p_stream, + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data, + l_nb_bytes_written,p_manager) != l_nb_bytes_written) { + return OPJ_FALSE; + } - ++p_j2k->m_current_tile_number; + ++p_j2k->m_current_tile_number; - return OPJ_TRUE; + return OPJ_TRUE; } @@ -9805,19 +9958,19 @@ opj_bool opj_j2k_post_write_tile ( opj_j2k_v2_t * p_j2k, */ void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k) { - /* preconditions */ - assert(p_j2k != 00); + /* preconditions */ + assert(p_j2k != 00); - /* DEVELOPER CORNER, insert your custom procedures */ - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_eoc ); + /* DEVELOPER CORNER, insert your custom procedures */ + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_eoc ); - if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) { - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm); - } + if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm); + } - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_epc ); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_end_encoding ); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_epc ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_end_encoding ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory); } /** @@ -9826,14 +9979,14 @@ void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k) */ void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k) { - /* preconditions */ - assert(p_j2k != 00); + /* preconditions */ + assert(p_j2k != 00); - opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder); - opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation); + opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder); + opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation); - /* DEVELOPER CORNER, add your custom validation procedure */ - opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation); + /* DEVELOPER CORNER, add your custom validation procedure */ + opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation); } @@ -9843,309 +9996,309 @@ void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k) */ void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k) { - /* preconditions */ - assert(p_j2k != 00); + /* preconditions */ + assert(p_j2k != 00); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_init_info ); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc ); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz ); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod ); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_init_info ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd ); - if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) { - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_image_components ); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm ); + if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_image_components ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm ); - if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) { - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc ); - } - } + if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc ); + } + } - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_regions); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_regions); - if (p_j2k->m_cp.comment != 00) { - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com); - } + if (p_j2k->m_cp.comment != 00) { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com); + } - /* DEVELOPER CORNER, insert your custom procedures */ - if (p_j2k->m_cp.rsiz & MCT) { - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group ); - } - /* End of Developer Corner */ + /* DEVELOPER CORNER, insert your custom procedures */ + if (p_j2k->m_cp.rsiz & MCT) { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group ); + } + /* End of Developer Corner */ - if (p_j2k->cstr_index) { - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header ); - } + if (p_j2k->cstr_index) { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header ); + } - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd); - opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates); } opj_bool opj_j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 p_total_data_size, - opj_stream_private_t *p_stream, - struct opj_event_mgr * p_manager ) -{ - OPJ_UINT32 compno; - OPJ_UINT32 l_nb_bytes_written = 0; - OPJ_UINT32 l_current_nb_bytes_written; - OPJ_BYTE * l_begin_data = 00; - - opj_tcp_v2_t *l_tcp = 00; - opj_tcd_v2_t * l_tcd = 00; - opj_cp_v2_t * l_cp = 00; - - l_tcd = p_j2k->m_tcd; - l_cp = &(p_j2k->m_cp); - l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; - - l_tcd->cur_pino = 0; - - /*Get number of tile parts*/ - p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; - - /* INDEX >> */ - /* << INDEX */ - - l_current_nb_bytes_written = 0; - l_begin_data = p_data; - if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) - { - return OPJ_FALSE; - } - - l_nb_bytes_written += l_current_nb_bytes_written; - p_data += l_current_nb_bytes_written; - p_total_data_size -= l_current_nb_bytes_written; - - if (l_cp->m_specific_param.m_enc.m_cinema == 0) { - for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) { - l_current_nb_bytes_written = 0; - opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager); - l_nb_bytes_written += l_current_nb_bytes_written; - p_data += l_current_nb_bytes_written; - p_total_data_size -= l_current_nb_bytes_written; - - l_current_nb_bytes_written = 0; - opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager); - l_nb_bytes_written += l_current_nb_bytes_written; - p_data += l_current_nb_bytes_written; - p_total_data_size -= l_current_nb_bytes_written; - } - - if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) { - l_current_nb_bytes_written = 0; - opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager); - l_nb_bytes_written += l_current_nb_bytes_written; - p_data += l_current_nb_bytes_written; - p_total_data_size -= l_current_nb_bytes_written; - } - } - - l_current_nb_bytes_written = 0; - if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { - return OPJ_FALSE; - } - - l_nb_bytes_written += l_current_nb_bytes_written; - * p_data_written = l_nb_bytes_written; - - /* Writing Psot in SOT marker */ - opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */ - - if (l_cp->m_specific_param.m_enc.m_cinema){ - opj_j2k_update_tlm(p_j2k,l_nb_bytes_written); - } - - return OPJ_TRUE; -} - -opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k, - OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 p_total_data_size, - opj_stream_private_t *p_stream, - struct opj_event_mgr * p_manager - ) -{ - OPJ_UINT32 tilepartno=0; - OPJ_UINT32 l_nb_bytes_written = 0; - OPJ_UINT32 l_current_nb_bytes_written; - OPJ_UINT32 l_part_tile_size; - OPJ_UINT32 tot_num_tp; - OPJ_UINT32 pino; - - OPJ_BYTE * l_begin_data; - opj_tcp_v2_t *l_tcp = 00; - opj_tcd_v2_t * l_tcd = 00; - opj_cp_v2_t * l_cp = 00; - - - l_tcd = p_j2k->m_tcd; - l_cp = &(p_j2k->m_cp); - l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager ) +{ + OPJ_UINT32 compno; + OPJ_UINT32 l_nb_bytes_written = 0; + OPJ_UINT32 l_current_nb_bytes_written; + OPJ_BYTE * l_begin_data = 00; - /*Get number of tile parts*/ - tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number); - - for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) { - p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; - l_current_nb_bytes_written = 0; - l_part_tile_size = 0; - l_begin_data = p_data; + opj_tcp_v2_t *l_tcp = 00; + opj_tcd_v2_t * l_tcd = 00; + opj_cp_v2_t * l_cp = 00; - if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) { - return OPJ_FALSE; - } + l_tcd = p_j2k->m_tcd; + l_cp = &(p_j2k->m_cp); + l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; - l_nb_bytes_written += l_current_nb_bytes_written; - p_data += l_current_nb_bytes_written; - p_total_data_size -= l_current_nb_bytes_written; - l_part_tile_size += l_nb_bytes_written; - - l_current_nb_bytes_written = 0; - if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { - return OPJ_FALSE; - } - - p_data += l_current_nb_bytes_written; - l_nb_bytes_written += l_current_nb_bytes_written; - p_total_data_size -= l_current_nb_bytes_written; - l_part_tile_size += l_nb_bytes_written; + l_tcd->cur_pino = 0; - /* Writing Psot in SOT marker */ - opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */ - - if (l_cp->m_specific_param.m_enc.m_cinema) { - opj_j2k_update_tlm(p_j2k,l_part_tile_size); - } - - ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; - } + /*Get number of tile parts*/ + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; - for (pino = 1; pino <= l_tcp->numpocs; ++pino) { - l_tcd->cur_pino = pino; + /* INDEX >> */ + /* << INDEX */ - /*Get number of tile parts*/ - tot_num_tp = opj_j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number); - for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) { - p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; - l_current_nb_bytes_written = 0; - l_part_tile_size = 0; - l_begin_data = p_data; + l_current_nb_bytes_written = 0; + l_begin_data = p_data; + if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) + { + return OPJ_FALSE; + } + + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + + if (l_cp->m_specific_param.m_enc.m_cinema == 0) { + for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) { + l_current_nb_bytes_written = 0; + opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager); + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + + l_current_nb_bytes_written = 0; + opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager); + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + } + + if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) { + l_current_nb_bytes_written = 0; + opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager); + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + } + } + + l_current_nb_bytes_written = 0; + if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { + return OPJ_FALSE; + } + + l_nb_bytes_written += l_current_nb_bytes_written; + * p_data_written = l_nb_bytes_written; + + /* Writing Psot in SOT marker */ + opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */ + + if (l_cp->m_specific_param.m_enc.m_cinema){ + opj_j2k_update_tlm(p_j2k,l_nb_bytes_written); + } + + return OPJ_TRUE; +} + +opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 tilepartno=0; + OPJ_UINT32 l_nb_bytes_written = 0; + OPJ_UINT32 l_current_nb_bytes_written; + OPJ_UINT32 l_part_tile_size; + OPJ_UINT32 tot_num_tp; + OPJ_UINT32 pino; + + OPJ_BYTE * l_begin_data; + opj_tcp_v2_t *l_tcp = 00; + opj_tcd_v2_t * l_tcd = 00; + opj_cp_v2_t * l_cp = 00; + + + l_tcd = p_j2k->m_tcd; + l_cp = &(p_j2k->m_cp); + l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; + + /*Get number of tile parts*/ + tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number); + + for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) { + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; + l_current_nb_bytes_written = 0; + l_part_tile_size = 0; + l_begin_data = p_data; + + if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) { + return OPJ_FALSE; + } + + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_nb_bytes_written; + + l_current_nb_bytes_written = 0; + if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { + return OPJ_FALSE; + } - if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) { - return OPJ_FALSE; - } + p_data += l_current_nb_bytes_written; + l_nb_bytes_written += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_nb_bytes_written; - l_nb_bytes_written += l_current_nb_bytes_written; - p_data += l_current_nb_bytes_written; - p_total_data_size -= l_current_nb_bytes_written; - l_part_tile_size += l_current_nb_bytes_written; + /* Writing Psot in SOT marker */ + opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */ - l_current_nb_bytes_written = 0; + if (l_cp->m_specific_param.m_enc.m_cinema) { + opj_j2k_update_tlm(p_j2k,l_part_tile_size); + } - if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { - return OPJ_FALSE; - } + ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; + } + + for (pino = 1; pino <= l_tcp->numpocs; ++pino) { + l_tcd->cur_pino = pino; + + /*Get number of tile parts*/ + tot_num_tp = opj_j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number); + for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) { + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; + l_current_nb_bytes_written = 0; + l_part_tile_size = 0; + l_begin_data = p_data; - l_nb_bytes_written += l_current_nb_bytes_written; - p_data += l_current_nb_bytes_written; - p_total_data_size -= l_current_nb_bytes_written; - l_part_tile_size += l_current_nb_bytes_written; + if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) { + return OPJ_FALSE; + } - /* Writing Psot in SOT marker */ - opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */ + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_current_nb_bytes_written; - if (l_cp->m_specific_param.m_enc.m_cinema) { - opj_j2k_update_tlm(p_j2k,l_part_tile_size); - } + l_current_nb_bytes_written = 0; - ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; - } - } + if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) { + return OPJ_FALSE; + } - *p_data_written = l_nb_bytes_written; + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_current_nb_bytes_written; - return OPJ_TRUE; + /* Writing Psot in SOT marker */ + opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */ + + if (l_cp->m_specific_param.m_enc.m_cinema) { + opj_j2k_update_tlm(p_j2k,l_part_tile_size); + } + + ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; + } + } + + *p_data_written = l_nb_bytes_written; + + return OPJ_TRUE; } /** * Writes the updated tlm. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager ) { - OPJ_UINT32 l_tlm_size; - OPJ_OFF_T l_tlm_position, l_current_position; + OPJ_UINT32 l_tlm_size; + OPJ_OFF_T l_tlm_position, l_current_position; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); - l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts; - l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start; - l_current_position = opj_stream_tell(p_stream); + l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts; + l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start; + l_current_position = opj_stream_tell(p_stream); - if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) { - return OPJ_FALSE; - } + if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) { + return OPJ_FALSE; + } - if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer,l_tlm_size,p_manager) != l_tlm_size) { - return OPJ_FALSE; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer,l_tlm_size,p_manager) != l_tlm_size) { + return OPJ_FALSE; + } - if (! opj_stream_seek(p_stream,l_current_position,p_manager)) { - return OPJ_FALSE; - } + if (! opj_stream_seek(p_stream,l_current_position,p_manager)) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Ends the encoding, i.e. frees memory. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_end_encoding( opj_j2k_v2_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) -{ - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); - - opj_tcd_destroy(p_j2k->m_tcd); - p_j2k->m_tcd = 00; - - if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { - opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); - p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0; - p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0; - } +opj_bool opj_j2k_end_encoding( opj_j2k_v2_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager ) +{ + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + opj_tcd_destroy(p_j2k->m_tcd); + p_j2k->m_tcd = 00; + + if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) { + opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0; + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0; + } - if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { - opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); - p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0; - } + if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0; + } - p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0; + p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0; - return OPJ_TRUE; + return OPJ_TRUE; } /** @@ -10156,140 +10309,140 @@ static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k, opj_event_mgr_t * p_manager ) { - /* preconditions */ - assert(p_j2k != 00); - assert(p_stream != 00); - assert(p_manager != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); - if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { - opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); - p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0; - } + if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0; + } - p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; - return OPJ_TRUE; + return OPJ_TRUE; } /** * Inits the Info * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -opj_bool opj_j2k_init_info( opj_j2k_v2_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) +opj_bool opj_j2k_init_info( opj_j2k_v2_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager ) { - opj_codestream_info_t * l_cstr_info = 00; + opj_codestream_info_t * l_cstr_info = 00; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); (void)l_cstr_info; - /* TODO mergeV2: check this part which use cstr_info */ - /*l_cstr_info = p_j2k->cstr_info; + /* TODO mergeV2: check this part which use cstr_info */ + /*l_cstr_info = p_j2k->cstr_info; - if (l_cstr_info) { - OPJ_UINT32 compno; - l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t)); + if (l_cstr_info) { + OPJ_UINT32 compno; + l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t)); - l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0; - l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0; + l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0; + l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0; - l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg; + l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg; - l_cstr_info->tw = p_j2k->m_cp.tw; - l_cstr_info->th = p_j2k->m_cp.th; + l_cstr_info->tw = p_j2k->m_cp.tw; + l_cstr_info->th = p_j2k->m_cp.th; - l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */ - /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */ - /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */ - /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */ + l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */ + /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */ + /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */ + /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */ - /*l_cstr_info->numcomps = p_j2k->m_image->numcomps; + /*l_cstr_info->numcomps = p_j2k->m_image->numcomps; - l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers; + l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers; - l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32)); + l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32)); - for (compno=0; compno < p_j2k->m_image->numcomps; compno++) { - l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1; - } + for (compno=0; compno < p_j2k->m_image->numcomps; compno++) { + l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1; + } - l_cstr_info->D_max = 0.0; */ /* ADD Marcela */ + l_cstr_info->D_max = 0.0; */ /* ADD Marcela */ - /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */ + /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */ - /*l_cstr_info->maxmarknum = 100; - l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t)); - l_cstr_info->marknum = 0; - }*/ + /*l_cstr_info->maxmarknum = 100; + l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t)); + l_cstr_info->marknum = 0; + }*/ - return opj_j2k_calculate_tp(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager); + return opj_j2k_calculate_tp(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager); } /** * Creates a tile-coder decoder. * - * @param p_stream the stream to write data to. - * @param p_j2k J2K codec. - * @param p_manager the user event manager. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager +static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) { - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); - p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE); + p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE); - if (! p_j2k->m_tcd) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n"); - return OPJ_FALSE; - } + if (! p_j2k->m_tcd) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n"); + return OPJ_FALSE; + } - if (!opj_tcd_init(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) { - opj_tcd_destroy(p_j2k->m_tcd); - p_j2k->m_tcd = 00; - return OPJ_FALSE; - } + if (!opj_tcd_init(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) { + opj_tcd_destroy(p_j2k->m_tcd); + p_j2k->m_tcd = 00; + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Writes a tile. - * @param p_j2k the jpeg2000 codec. - * @param p_stream the stream to write data to. - * @param p_manager the user event manager. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. */ opj_bool opj_j2k_write_tile (opj_j2k_v2_t * p_j2k, - OPJ_UINT32 p_tile_index, - OPJ_BYTE * p_data, - OPJ_UINT32 p_data_size, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) -{ - if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index); - return OPJ_FALSE; - } - else { - if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) { - opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index); - return OPJ_FALSE; - } - } - - return OPJ_TRUE; + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager ) +{ + if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index); + return OPJ_FALSE; + } + else { + if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) { + opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index); + return OPJ_FALSE; + } + } + + return OPJ_TRUE; } diff --git a/libopenjpeg/jp2.c b/libopenjpeg/jp2.c index 22034bc3..59515872 100644 --- a/libopenjpeg/jp2.c +++ b/libopenjpeg/jp2.c @@ -1765,7 +1765,7 @@ static opj_bool opj_jp2_read_header_procedure( opj_jp2_v2_t *jp2, if (box.type == JP2_JP2C) { if (jp2->jp2_state & JP2_STATE_HEADER) { jp2->jp2_state |= JP2_STATE_CODESTREAM; - opj_free(l_current_data); + opj_free(l_current_data); return OPJ_TRUE; } else { @@ -1785,17 +1785,22 @@ static opj_bool opj_jp2_read_header_procedure( opj_jp2_v2_t *jp2, if (l_current_handler != 00) { if (l_current_data_size > l_last_data_size) { - l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size); + unsigned char* new_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size); if (!l_current_data){ opj_free(l_current_data); + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 box\n"); return OPJ_FALSE; } + l_current_data = new_current_data; l_last_data_size = l_current_data_size; } l_nb_bytes_read = opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager); if (l_nb_bytes_read != l_current_data_size) { opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n"); + // TODO: LH: why nothing is freed here (as + // all other returns imply a free, even + // in the nominal case)? return OPJ_FALSE; } diff --git a/libopenjpeg/jpwl/jpwl.c b/libopenjpeg/jpwl/jpwl.c index db8cfa3f..8a5447fb 100644 --- a/libopenjpeg/jpwl/jpwl.c +++ b/libopenjpeg/jpwl/jpwl.c @@ -76,10 +76,10 @@ jpwl_epc_ms_t *jpwl_epc_create(opj_j2k_t *j2k, opj_bool esd_on, opj_bool red_on, @return returns the freshly created ESD */ jpwl_esd_ms_t *jpwl_esd_create(opj_j2k_t *j2k, int comps, - unsigned char addrm, unsigned char ad_size, - unsigned char senst, int se_size, int tileno, - unsigned long int svalnum, void *sensval); - + unsigned char addrm, unsigned char ad_size, + unsigned char senst, int se_size, int tileno, + unsigned long int svalnum, void *sensval); + /** this function is used to compare two JPWL markers based on their relevant wishlist position @param arg1 pointer to first marker @@ -113,1178 +113,1191 @@ void jpwl_esd_write(opj_j2k_t *j2k, jpwl_esd_ms_t *esdmark, unsigned char *buf); void jpwl_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) { - int mm; + int mm; - /* let's reset some settings */ + /* let's reset some settings */ - /* clear the existing markers */ - for (mm = 0; mm < jwmarker_num; mm++) { + /* clear the existing markers */ + for (mm = 0; mm < jwmarker_num; mm++) { - switch (jwmarker[mm].id) { + switch (jwmarker[mm].id) { - case J2K_MS_EPB: - opj_free(jwmarker[mm].m.epbmark); - break; + case J2K_MS_EPB: + opj_free(jwmarker[mm].m.epbmark); + break; - case J2K_MS_EPC: - opj_free(jwmarker[mm].m.epcmark); - break; + case J2K_MS_EPC: + opj_free(jwmarker[mm].m.epcmark); + break; - case J2K_MS_ESD: - opj_free(jwmarker[mm].m.esdmark); - break; + case J2K_MS_ESD: + opj_free(jwmarker[mm].m.esdmark); + break; - case J2K_MS_RED: - opj_free(jwmarker[mm].m.redmark); - break; + case J2K_MS_RED: + opj_free(jwmarker[mm].m.redmark); + break; - default: - break; - } - } + default: + break; + } + } - /* clear the marker structure array */ - memset(jwmarker, 0, sizeof(jpwl_marker_t) * JPWL_MAX_NO_MARKERS); + /* clear the marker structure array */ + memset(jwmarker, 0, sizeof(jpwl_marker_t) * JPWL_MAX_NO_MARKERS); - /* no more markers in the list */ - jwmarker_num = 0; + /* no more markers in the list */ + jwmarker_num = 0; - /* let's begin creating a marker list, according to user wishes */ - jpwl_prepare_marks(j2k, cio, image); + /* let's begin creating a marker list, according to user wishes */ + jpwl_prepare_marks(j2k, cio, image); - /* now we dump the JPWL markers on the codestream */ - jpwl_dump_marks(j2k, cio, image); + /* now we dump the JPWL markers on the codestream */ + jpwl_dump_marks(j2k, cio, image); - /* do not know exactly what is this for, - but it gets called during index creation */ - j2k->pos_correction = 0; + /* do not know exactly what is this for, + but it gets called during index creation */ + j2k->pos_correction = 0; } -void j2k_add_marker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) { - - if (!cstr_info) - return; - - /* expand the list? */ - if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) { - cstr_info->maxmarknum += 100; - cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum * sizeof(opj_marker_info_t)); - } - - /* add the marker */ - cstr_info->marker[cstr_info->marknum].type = type; - cstr_info->marker[cstr_info->marknum].pos = pos; - cstr_info->marker[cstr_info->marknum].len = len; - cstr_info->marknum++; +opj_bool j2k_add_marker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) { + + if (!cstr_info) + return; + + /* expand the list? */ + if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) { + opj_marker_info_t* new_marker; + cstr_info->maxmarknum += 100; + new_marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum * sizeof(opj_marker_info_t)); + if (! new_marker) + { + opj_free(cstr_info->marker); + cstr_info->marker = 0; + cstr_info->marknum = 0; + cstr_info->maxmarknum = 0; + opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add a marker\n"); // TODO: find a better error message + TODO_test_add_marker_result; + return OPJ_FALSE; + } + cstr_info->marker = new_marker; + } + + /* add the marker */ + cstr_info->marker[cstr_info->marknum].type = type; + cstr_info->marker[cstr_info->marknum].pos = pos; + cstr_info->marker[cstr_info->marknum].len = len; + cstr_info->marknum++; + return OPJ_TRUE; } void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) { - unsigned short int socsiz_len = 0; - int ciopos = cio_tell(cio), soc_pos = j2k->cstr_info->main_head_start; - unsigned char *socp = NULL; + unsigned short int socsiz_len = 0; + int ciopos = cio_tell(cio), soc_pos = j2k->cstr_info->main_head_start; + unsigned char *socp = NULL; - int tileno, acc_tpno, tpno, tilespec, hprot, sens, pprot, packspec, lastileno, packno; + int tileno, acc_tpno, tpno, tilespec, hprot, sens, pprot, packspec, lastileno, packno; - jpwl_epb_ms_t *epb_mark; - jpwl_epc_ms_t *epc_mark; - jpwl_esd_ms_t *esd_mark; + jpwl_epb_ms_t *epb_mark; + jpwl_epc_ms_t *epc_mark; + jpwl_esd_ms_t *esd_mark; (void)image; - /* find (SOC + SIZ) length */ - /* I assume SIZ is always the first marker after SOC */ - cio_seek(cio, soc_pos + 4); - socsiz_len = (unsigned short int) cio_read(cio, 2) + 4; /* add the 2 marks length itself */ - cio_seek(cio, soc_pos + 0); - socp = cio_getbp(cio); /* pointer to SOC */ - - /* - EPC MS for Main Header: if we are here it's required - */ - /* create the EPC */ - if ((epc_mark = jpwl_epc_create( - j2k, - j2k->cp->esd_on, /* is ESD present? */ - j2k->cp->red_on, /* is RED present? */ - j2k->cp->epb_on, /* is EPB present? */ - OPJ_FALSE /* are informative techniques present? */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (epc_mark) { - jwmarker[jwmarker_num].id = J2K_MS_EPC; /* its type */ - jwmarker[jwmarker_num].m.epcmark = epc_mark; /* the EPC */ - jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* after SIZ */ - jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.1; /* not so first */ - jwmarker[jwmarker_num].len = epc_mark->Lepc; /* its length */ - jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].parms_ready = OPJ_FALSE; /* not ready */ - jwmarker[jwmarker_num].data_ready = OPJ_TRUE; /* ready */ - jwmarker_num++; - }; - - opj_event_msg(j2k->cinfo, EVT_INFO, - "MH EPC : setting %s%s%s\n", - j2k->cp->esd_on ? "ESD, " : "", - j2k->cp->red_on ? "RED, " : "", - j2k->cp->epb_on ? "EPB, " : "" - ); - - } else { - /* ooops, problems */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH EPC\n"); - }; - - /* - ESD MS for Main Header - */ - /* first of all, must MH have an ESD MS? */ - if (j2k->cp->esd_on && (j2k->cp->sens_MH >= 0)) { - - /* Create the ESD */ - if ((esd_mark = jpwl_esd_create( - j2k, /* this encoder handle */ - -1, /* we are averaging over all components */ - (unsigned char) j2k->cp->sens_range, /* range method */ - (unsigned char) j2k->cp->sens_addr, /* sensitivity addressing */ - (unsigned char) j2k->cp->sens_MH, /* sensitivity method */ - j2k->cp->sens_size, /* sensitivity size */ - -1, /* this ESD is in main header */ - 0 /*j2k->cstr_info->num*/, /* number of packets in codestream */ - NULL /*sensval*/ /* pointer to sensitivity data of packets */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (jwmarker_num < JPWL_MAX_NO_MARKERS) { - jwmarker[jwmarker_num].id = J2K_MS_ESD; /* its type */ - jwmarker[jwmarker_num].m.esdmark = esd_mark; /* the EPB */ - jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* we choose to place it after SIZ */ - jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.2; /* not first at all! */ - jwmarker[jwmarker_num].len = esd_mark->Lesd; /* its length */ - jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* not ready, yet */ - jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* not ready */ - jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ - jwmarker_num++; - } - - opj_event_msg(j2k->cinfo, EVT_INFO, - "MH ESDs: method %d\n", - j2k->cp->sens_MH - ); - - } else { - /* ooops, problems */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH ESD\n"); - }; - - } - - /* - ESD MSs for Tile Part Headers - */ - /* cycle through tiles */ - sens = -1; /* default spec: no ESD */ - tilespec = 0; /* first tile spec */ - acc_tpno = 0; - for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) { - - opj_event_msg(j2k->cinfo, EVT_INFO, - "Tile %d has %d tile part(s)\n", - tileno, j2k->cstr_info->tile[tileno].num_tps - ); - - /* for every tile part in the tile */ - for (tpno = 0; tpno < j2k->cstr_info->tile[tileno].num_tps; tpno++, acc_tpno++) { - - int sot_len, Psot, Psotp, mm; - unsigned long sot_pos, post_sod_pos; - - unsigned long int left_THmarks_len; - - /******* sot_pos = j2k->cstr_info->tile[tileno].start_pos; */ - sot_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos; - cio_seek(cio, sot_pos + 2); - sot_len = cio_read(cio, 2); /* SOT Len */ - cio_skip(cio, 2); - Psotp = cio_tell(cio); - Psot = cio_read(cio, 4); /* tile length */ - - /******* post_sod_pos = j2k->cstr_info->tile[tileno].end_header + 1; */ - post_sod_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_end_header + 1; - left_THmarks_len = post_sod_pos - sot_pos; - - /* add all the lengths of the markers which are len-ready and stay within SOT and SOD */ - for (mm = 0; mm < jwmarker_num; mm++) { - if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) { - if (jwmarker[mm].len_ready) - left_THmarks_len += jwmarker[mm].len + 2; - else { - opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n", - jwmarker[mm].id, jwmarker[mm].dpos); - exit(1); - } - } - } - - /******* if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == tileno)) */ - if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == acc_tpno)) - /* we got a specification from this tile onwards */ - sens = j2k->cp->sens_TPH[tilespec++]; - - /* must this TPH have an ESD MS? */ - if (j2k->cp->esd_on && (sens >= 0)) { - - /* Create the ESD */ - if ((esd_mark = jpwl_esd_create( - j2k, /* this encoder handle */ - -1, /* we are averaging over all components */ - (unsigned char) j2k->cp->sens_range, /* range method */ - (unsigned char) j2k->cp->sens_addr, /* sensitivity addressing size */ - (unsigned char) sens, /* sensitivity method */ - j2k->cp->sens_size, /* sensitivity value size */ - tileno, /* this ESD is in a tile */ - 0, /* number of packets in codestream */ - NULL /* pointer to sensitivity data of packets */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (jwmarker_num < JPWL_MAX_NO_MARKERS) { - jwmarker[jwmarker_num].id = J2K_MS_ESD; /* its type */ - jwmarker[jwmarker_num].m.esdmark = esd_mark; /* the EPB */ - /****** jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].start_pos + sot_len + 2; */ /* after SOT */ - jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2; /* after SOT */ - jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.2; /* not first at all! */ - jwmarker[jwmarker_num].len = esd_mark->Lesd; /* its length */ - jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready, yet */ - jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* not ready */ - jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* ready */ - jwmarker_num++; - } - - /* update Psot of the tile */ - cio_seek(cio, Psotp); - cio_write(cio, Psot + esd_mark->Lesd + 2, 4); - - opj_event_msg(j2k->cinfo, EVT_INFO, - /******* "TPH ESDs: tile %02d, method %d\n", */ - "TPH ESDs: tile %02d, part %02d, method %d\n", - /******* tileno, */ - tileno, tpno, - sens - ); - - } else { - /* ooops, problems */ - /***** opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d\n", tileno); */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d,%d\n", tileno, tpno); - }; - - } - - } - - }; - - /* - EPB MS for Main Header - */ - /* first of all, must MH have an EPB MS? */ - if (j2k->cp->epb_on && (j2k->cp->hprot_MH > 0)) { - - int mm; - - /* position of SOT */ - unsigned int sot_pos = j2k->cstr_info->main_head_end + 1; - - /* how much space is there between end of SIZ and beginning of SOT? */ - int left_MHmarks_len = sot_pos - socsiz_len; - - /* add all the lengths of the markers which are len-ready and stay within SOC and SOT */ - for (mm = 0; mm < jwmarker_num; mm++) { - if ( jwmarker[mm].pos < sot_pos) { /* jwmarker[mm].pos >=0 since ulong */ - if (jwmarker[mm].len_ready) - left_MHmarks_len += jwmarker[mm].len + 2; - else { - opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up MH EPB\n", - jwmarker[mm].id, jwmarker[mm].dpos); - exit(1); - } - } - } - - /* Create the EPB */ - if ((epb_mark = jpwl_epb_create( - j2k, /* this encoder handle */ - OPJ_TRUE, /* is it the latest? */ - OPJ_TRUE, /* is it packed? not for now */ - -1, /* we are in main header */ - 0, /* its index is 0 (first) */ - j2k->cp->hprot_MH, /* protection type parameters of data */ - socsiz_len, /* pre-data: only SOC+SIZ */ - left_MHmarks_len /* post-data: from SOC to SOT, and all JPWL markers within */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (jwmarker_num < JPWL_MAX_NO_MARKERS) { - jwmarker[jwmarker_num].id = J2K_MS_EPB; /* its type */ - jwmarker[jwmarker_num].m.epbmark = epb_mark; /* the EPB */ - jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* after SIZ */ - jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos; /* first first first! */ - jwmarker[jwmarker_num].len = epb_mark->Lepb; /* its length */ - jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ - jwmarker_num++; - } - - opj_event_msg(j2k->cinfo, EVT_INFO, - "MH EPB : prot. %d\n", - j2k->cp->hprot_MH - ); - - } else { - /* ooops, problems */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH EPB\n"); - }; - } - - /* - EPB MSs for Tile Parts - */ - /* cycle through TPHs */ - hprot = j2k->cp->hprot_MH; /* default spec */ - tilespec = 0; /* first tile spec */ - lastileno = 0; - packspec = 0; - pprot = -1; - acc_tpno = 0; - for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) { - - opj_event_msg(j2k->cinfo, EVT_INFO, - "Tile %d has %d tile part(s)\n", - tileno, j2k->cstr_info->tile[tileno].num_tps - ); - - /* for every tile part in the tile */ - for (tpno = 0; tpno < j2k->cstr_info->tile[tileno].num_tps; tpno++, acc_tpno++) { - - int sot_len, Psot, Psotp, mm, epb_index = 0, prot_len = 0; - unsigned long sot_pos, post_sod_pos; - unsigned long int left_THmarks_len/*, epbs_len = 0*/; - int startpack = 0, stoppack = j2k->cstr_info->packno; - int first_tp_pack, last_tp_pack; - jpwl_epb_ms_t *tph_epb = NULL; - - /****** sot_pos = j2k->cstr_info->tile[tileno].start_pos; */ - sot_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos; - cio_seek(cio, sot_pos + 2); - sot_len = cio_read(cio, 2); /* SOT Len */ - cio_skip(cio, 2); - Psotp = cio_tell(cio); - Psot = cio_read(cio, 4); /* tile length */ - - /* a-priori length of the data dwelling between SOT and SOD */ - /****** post_sod_pos = j2k->cstr_info->tile[tileno].end_header + 1; */ - post_sod_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_end_header + 1; - left_THmarks_len = post_sod_pos - (sot_pos + sot_len + 2); - - /* add all the lengths of the JPWL markers which are len-ready and stay within SOT and SOD */ - for (mm = 0; mm < jwmarker_num; mm++) { - if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) { - if (jwmarker[mm].len_ready) - left_THmarks_len += jwmarker[mm].len + 2; - else { - opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n", - jwmarker[mm].id, jwmarker[mm].dpos); - exit(1); - } - } - } - - /****** if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == tileno)) */ - if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == acc_tpno)) - /* we got a specification from this tile part onwards */ - hprot = j2k->cp->hprot_TPH[tilespec++]; - - /* must this TPH have an EPB MS? */ - if (j2k->cp->epb_on && (hprot > 0)) { - - /* Create the EPB */ - if ((epb_mark = jpwl_epb_create( - j2k, /* this encoder handle */ - OPJ_FALSE, /* is it the latest? in TPH, no for now (if huge data size in TPH, we'd need more) */ - OPJ_TRUE, /* is it packed? yes for now */ - tileno, /* we are in TPH */ - epb_index++, /* its index is 0 (first) */ - hprot, /* protection type parameters of following data */ - sot_len + 2, /* pre-data length: only SOT */ - left_THmarks_len /* post-data length: from SOT end to SOD inclusive */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (jwmarker_num < JPWL_MAX_NO_MARKERS) { - jwmarker[jwmarker_num].id = J2K_MS_EPB; /* its type */ - jwmarker[jwmarker_num].m.epbmark = epb_mark; /* the EPB */ - /****** jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].start_pos + sot_len + 2; */ /* after SOT */ - jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2; /* after SOT */ - jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos; /* first first first! */ - jwmarker[jwmarker_num].len = epb_mark->Lepb; /* its length */ - jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ - jwmarker_num++; - } - - /* update Psot of the tile */ - Psot += epb_mark->Lepb + 2; - - opj_event_msg(j2k->cinfo, EVT_INFO, - /***** "TPH EPB : tile %02d, prot. %d\n", */ - "TPH EPB : tile %02d, part %02d, prot. %d\n", - /***** tileno, */ - tileno, tpno, - hprot - ); - - /* save this TPH EPB address */ - tph_epb = epb_mark; - - } else { - /* ooops, problems */ - /****** opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB #%d\n", tileno); */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB in #%d,d\n", tileno, tpno); - }; - - } - - startpack = 0; - /* EPB MSs for UEP packet data protection in Tile Parts */ - /****** for (packno = 0; packno < j2k->cstr_info->num; packno++) { */ - /*first_tp_pack = (tpno > 0) ? (first_tp_pack + j2k->cstr_info->tile[tileno].tp[tpno - 1].tp_numpacks) : 0;*/ - first_tp_pack = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pack; - last_tp_pack = first_tp_pack + j2k->cstr_info->tile[tileno].tp[tpno].tp_numpacks - 1; - for (packno = 0; packno < j2k->cstr_info->tile[tileno].tp[tpno].tp_numpacks; packno++) { - - /******** if ((packspec < JPWL_MAX_NO_PACKSPECS) && - (j2k->cp->pprot_tileno[packspec] == tileno) && (j2k->cp->pprot_packno[packspec] == packno)) { */ - if ((packspec < JPWL_MAX_NO_PACKSPECS) && - (j2k->cp->pprot_tileno[packspec] == acc_tpno) && (j2k->cp->pprot_packno[packspec] == packno)) { - - /* we got a specification from this tile and packet onwards */ - /* print the previous spec */ - if (packno > 0) { - stoppack = packno - 1; - opj_event_msg(j2k->cinfo, EVT_INFO, - /***** "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", */ - "UEP EPBs: tile %02d, part %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", - /***** tileno, */ - tileno, tpno, - startpack, - stoppack, - /***** j2k->cstr_info->tile[tileno].packet[startpack].start_pos, */ - j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos, - /***** j2k->cstr_info->tile[tileno].packet[stoppack].end_pos, */ - j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos, - pprot); - - /***** prot_len = j2k->cstr_info->tile[tileno].packet[stoppack].end_pos + 1 - - j2k->cstr_info->tile[tileno].packet[startpack].start_pos; */ - prot_len = j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos + 1 - - j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos; - - /* - particular case: if this is the last header and the last packet, - then it is better to protect even the EOC marker - */ - /****** if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && - (stoppack == (j2k->cstr_info->num - 1))) */ - if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && - (tpno == (j2k->cstr_info->tile[tileno].num_tps - 1)) && - (stoppack == last_tp_pack)) - /* add the EOC len */ - prot_len += 2; - - /* let's add the EPBs */ - Psot += jpwl_epbs_add( - j2k, /* J2K handle */ - jwmarker, /* pointer to JPWL markers list */ - &jwmarker_num, /* pointer to the number of current markers */ - OPJ_FALSE, /* latest */ - OPJ_TRUE, /* packed */ - OPJ_FALSE, /* inside MH */ - &epb_index, /* pointer to EPB index */ - pprot, /* protection type */ - /****** (double) (j2k->cstr_info->tile[tileno].start_pos + sot_len + 2) + 0.0001, */ /* position */ - (double) (j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2) + 0.0001, /* position */ - tileno, /* number of tile */ - 0, /* length of pre-data */ - prot_len /*4000*/ /* length of post-data */ - ); - } - - startpack = packno; - pprot = j2k->cp->pprot[packspec++]; - } - - /*printf("Tile %02d, pack %02d ==> %d\n", tileno, packno, pprot);*/ - - } - - /* we are at the end: print the remaining spec */ - stoppack = packno - 1; - if (pprot >= 0) { - - opj_event_msg(j2k->cinfo, EVT_INFO, - /**** "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", */ - "UEP EPBs: tile %02d, part %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", - /**** tileno, */ - tileno, tpno, - startpack, - stoppack, - /***** j2k->image_info->tile[tileno].packet[startpack].start_pos, - j2k->image_info->tile[tileno].packet[stoppack].end_pos, */ - j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos, - j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos, - pprot); - - /***** prot_len = j2k->cstr_info->tile[tileno].packet[stoppack].end_pos + 1 - - j2k->cstr_info->tile[tileno].packet[startpack].start_pos; */ - prot_len = j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos + 1 - - j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos; - - /* - particular case: if this is the last header and the last packet, - then it is better to protect even the EOC marker - */ - /***** if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && - (stoppack == (j2k->cstr_info->num - 1))) */ - if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && - (tpno == (j2k->cstr_info->tile[tileno].num_tps - 1)) && - (stoppack == last_tp_pack)) - /* add the EOC len */ - prot_len += 2; - - /* let's add the EPBs */ - Psot += jpwl_epbs_add( - j2k, /* J2K handle */ - jwmarker, /* pointer to JPWL markers list */ - &jwmarker_num, /* pointer to the number of current markers */ - OPJ_TRUE, /* latest */ - OPJ_TRUE, /* packed */ - OPJ_FALSE, /* inside MH */ - &epb_index, /* pointer to EPB index */ - pprot, /* protection type */ - /***** (double) (j2k->cstr_info->tile[tileno].start_pos + sot_len + 2) + 0.0001,*/ /* position */ - (double) (j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2) + 0.0001, /* position */ - tileno, /* number of tile */ - 0, /* length of pre-data */ - prot_len /*4000*/ /* length of post-data */ - ); - } - - /* we can now check if the TPH EPB was really the last one */ - if (tph_epb && (epb_index == 1)) { - /* set the TPH EPB to be the last one in current header */ - tph_epb->Depb |= (unsigned char) ((OPJ_TRUE & 0x0001) << 6); - tph_epb = NULL; - } - - /* write back Psot */ - cio_seek(cio, Psotp); - cio_write(cio, Psot, 4); - - } - - }; - - /* reset the position */ - cio_seek(cio, ciopos); + /* find (SOC + SIZ) length */ + /* I assume SIZ is always the first marker after SOC */ + cio_seek(cio, soc_pos + 4); + socsiz_len = (unsigned short int) cio_read(cio, 2) + 4; /* add the 2 marks length itself */ + cio_seek(cio, soc_pos + 0); + socp = cio_getbp(cio); /* pointer to SOC */ + + /* + EPC MS for Main Header: if we are here it's required + */ + /* create the EPC */ + if ((epc_mark = jpwl_epc_create( + j2k, + j2k->cp->esd_on, /* is ESD present? */ + j2k->cp->red_on, /* is RED present? */ + j2k->cp->epb_on, /* is EPB present? */ + OPJ_FALSE /* are informative techniques present? */ + ))) { + + /* Add this marker to the 'insertanda' list */ + if (epc_mark) { + jwmarker[jwmarker_num].id = J2K_MS_EPC; /* its type */ + jwmarker[jwmarker_num].m.epcmark = epc_mark; /* the EPC */ + jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* after SIZ */ + jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.1; /* not so first */ + jwmarker[jwmarker_num].len = epc_mark->Lepc; /* its length */ + jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].parms_ready = OPJ_FALSE; /* not ready */ + jwmarker[jwmarker_num].data_ready = OPJ_TRUE; /* ready */ + jwmarker_num++; + }; + + opj_event_msg(j2k->cinfo, EVT_INFO, + "MH EPC : setting %s%s%s\n", + j2k->cp->esd_on ? "ESD, " : "", + j2k->cp->red_on ? "RED, " : "", + j2k->cp->epb_on ? "EPB, " : "" + ); + + } else { + /* ooops, problems */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH EPC\n"); + }; + + /* + ESD MS for Main Header + */ + /* first of all, must MH have an ESD MS? */ + if (j2k->cp->esd_on && (j2k->cp->sens_MH >= 0)) { + + /* Create the ESD */ + if ((esd_mark = jpwl_esd_create( + j2k, /* this encoder handle */ + -1, /* we are averaging over all components */ + (unsigned char) j2k->cp->sens_range, /* range method */ + (unsigned char) j2k->cp->sens_addr, /* sensitivity addressing */ + (unsigned char) j2k->cp->sens_MH, /* sensitivity method */ + j2k->cp->sens_size, /* sensitivity size */ + -1, /* this ESD is in main header */ + 0 /*j2k->cstr_info->num*/, /* number of packets in codestream */ + NULL /*sensval*/ /* pointer to sensitivity data of packets */ + ))) { + + /* Add this marker to the 'insertanda' list */ + if (jwmarker_num < JPWL_MAX_NO_MARKERS) { + jwmarker[jwmarker_num].id = J2K_MS_ESD; /* its type */ + jwmarker[jwmarker_num].m.esdmark = esd_mark; /* the EPB */ + jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* we choose to place it after SIZ */ + jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.2; /* not first at all! */ + jwmarker[jwmarker_num].len = esd_mark->Lesd; /* its length */ + jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* not ready, yet */ + jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* not ready */ + jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ + jwmarker_num++; + } + + opj_event_msg(j2k->cinfo, EVT_INFO, + "MH ESDs: method %d\n", + j2k->cp->sens_MH + ); + + } else { + /* ooops, problems */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH ESD\n"); + }; + + } + + /* + ESD MSs for Tile Part Headers + */ + /* cycle through tiles */ + sens = -1; /* default spec: no ESD */ + tilespec = 0; /* first tile spec */ + acc_tpno = 0; + for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) { + + opj_event_msg(j2k->cinfo, EVT_INFO, + "Tile %d has %d tile part(s)\n", + tileno, j2k->cstr_info->tile[tileno].num_tps + ); + + /* for every tile part in the tile */ + for (tpno = 0; tpno < j2k->cstr_info->tile[tileno].num_tps; tpno++, acc_tpno++) { + + int sot_len, Psot, Psotp, mm; + unsigned long sot_pos, post_sod_pos; + + unsigned long int left_THmarks_len; + + /******* sot_pos = j2k->cstr_info->tile[tileno].start_pos; */ + sot_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos; + cio_seek(cio, sot_pos + 2); + sot_len = cio_read(cio, 2); /* SOT Len */ + cio_skip(cio, 2); + Psotp = cio_tell(cio); + Psot = cio_read(cio, 4); /* tile length */ + + /******* post_sod_pos = j2k->cstr_info->tile[tileno].end_header + 1; */ + post_sod_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_end_header + 1; + left_THmarks_len = post_sod_pos - sot_pos; + + /* add all the lengths of the markers which are len-ready and stay within SOT and SOD */ + for (mm = 0; mm < jwmarker_num; mm++) { + if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) { + if (jwmarker[mm].len_ready) + left_THmarks_len += jwmarker[mm].len + 2; + else { + opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n", + jwmarker[mm].id, jwmarker[mm].dpos); + exit(1); + } + } + } + + /******* if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == tileno)) */ + if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == acc_tpno)) + /* we got a specification from this tile onwards */ + sens = j2k->cp->sens_TPH[tilespec++]; + + /* must this TPH have an ESD MS? */ + if (j2k->cp->esd_on && (sens >= 0)) { + + /* Create the ESD */ + if ((esd_mark = jpwl_esd_create( + j2k, /* this encoder handle */ + -1, /* we are averaging over all components */ + (unsigned char) j2k->cp->sens_range, /* range method */ + (unsigned char) j2k->cp->sens_addr, /* sensitivity addressing size */ + (unsigned char) sens, /* sensitivity method */ + j2k->cp->sens_size, /* sensitivity value size */ + tileno, /* this ESD is in a tile */ + 0, /* number of packets in codestream */ + NULL /* pointer to sensitivity data of packets */ + ))) { + + /* Add this marker to the 'insertanda' list */ + if (jwmarker_num < JPWL_MAX_NO_MARKERS) { + jwmarker[jwmarker_num].id = J2K_MS_ESD; /* its type */ + jwmarker[jwmarker_num].m.esdmark = esd_mark; /* the EPB */ + /****** jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].start_pos + sot_len + 2; */ /* after SOT */ + jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2; /* after SOT */ + jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.2; /* not first at all! */ + jwmarker[jwmarker_num].len = esd_mark->Lesd; /* its length */ + jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready, yet */ + jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* not ready */ + jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* ready */ + jwmarker_num++; + } + + /* update Psot of the tile */ + cio_seek(cio, Psotp); + cio_write(cio, Psot + esd_mark->Lesd + 2, 4); + + opj_event_msg(j2k->cinfo, EVT_INFO, + /******* "TPH ESDs: tile %02d, method %d\n", */ + "TPH ESDs: tile %02d, part %02d, method %d\n", + /******* tileno, */ + tileno, tpno, + sens + ); + + } else { + /* ooops, problems */ + /***** opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d\n", tileno); */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d,%d\n", tileno, tpno); + }; + + } + + } + + }; + + /* + EPB MS for Main Header + */ + /* first of all, must MH have an EPB MS? */ + if (j2k->cp->epb_on && (j2k->cp->hprot_MH > 0)) { + + int mm; + + /* position of SOT */ + unsigned int sot_pos = j2k->cstr_info->main_head_end + 1; + + /* how much space is there between end of SIZ and beginning of SOT? */ + int left_MHmarks_len = sot_pos - socsiz_len; + + /* add all the lengths of the markers which are len-ready and stay within SOC and SOT */ + for (mm = 0; mm < jwmarker_num; mm++) { + if ( jwmarker[mm].pos < sot_pos) { /* jwmarker[mm].pos >=0 since ulong */ + if (jwmarker[mm].len_ready) + left_MHmarks_len += jwmarker[mm].len + 2; + else { + opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up MH EPB\n", + jwmarker[mm].id, jwmarker[mm].dpos); + exit(1); + } + } + } + + /* Create the EPB */ + if ((epb_mark = jpwl_epb_create( + j2k, /* this encoder handle */ + OPJ_TRUE, /* is it the latest? */ + OPJ_TRUE, /* is it packed? not for now */ + -1, /* we are in main header */ + 0, /* its index is 0 (first) */ + j2k->cp->hprot_MH, /* protection type parameters of data */ + socsiz_len, /* pre-data: only SOC+SIZ */ + left_MHmarks_len /* post-data: from SOC to SOT, and all JPWL markers within */ + ))) { + + /* Add this marker to the 'insertanda' list */ + if (jwmarker_num < JPWL_MAX_NO_MARKERS) { + jwmarker[jwmarker_num].id = J2K_MS_EPB; /* its type */ + jwmarker[jwmarker_num].m.epbmark = epb_mark; /* the EPB */ + jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* after SIZ */ + jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos; /* first first first! */ + jwmarker[jwmarker_num].len = epb_mark->Lepb; /* its length */ + jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ + jwmarker_num++; + } + + opj_event_msg(j2k->cinfo, EVT_INFO, + "MH EPB : prot. %d\n", + j2k->cp->hprot_MH + ); + + } else { + /* ooops, problems */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH EPB\n"); + }; + } + + /* + EPB MSs for Tile Parts + */ + /* cycle through TPHs */ + hprot = j2k->cp->hprot_MH; /* default spec */ + tilespec = 0; /* first tile spec */ + lastileno = 0; + packspec = 0; + pprot = -1; + acc_tpno = 0; + for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) { + + opj_event_msg(j2k->cinfo, EVT_INFO, + "Tile %d has %d tile part(s)\n", + tileno, j2k->cstr_info->tile[tileno].num_tps + ); + + /* for every tile part in the tile */ + for (tpno = 0; tpno < j2k->cstr_info->tile[tileno].num_tps; tpno++, acc_tpno++) { + + int sot_len, Psot, Psotp, mm, epb_index = 0, prot_len = 0; + unsigned long sot_pos, post_sod_pos; + unsigned long int left_THmarks_len/*, epbs_len = 0*/; + int startpack = 0, stoppack = j2k->cstr_info->packno; + int first_tp_pack, last_tp_pack; + jpwl_epb_ms_t *tph_epb = NULL; + + /****** sot_pos = j2k->cstr_info->tile[tileno].start_pos; */ + sot_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos; + cio_seek(cio, sot_pos + 2); + sot_len = cio_read(cio, 2); /* SOT Len */ + cio_skip(cio, 2); + Psotp = cio_tell(cio); + Psot = cio_read(cio, 4); /* tile length */ + + /* a-priori length of the data dwelling between SOT and SOD */ + /****** post_sod_pos = j2k->cstr_info->tile[tileno].end_header + 1; */ + post_sod_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_end_header + 1; + left_THmarks_len = post_sod_pos - (sot_pos + sot_len + 2); + + /* add all the lengths of the JPWL markers which are len-ready and stay within SOT and SOD */ + for (mm = 0; mm < jwmarker_num; mm++) { + if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) { + if (jwmarker[mm].len_ready) + left_THmarks_len += jwmarker[mm].len + 2; + else { + opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n", + jwmarker[mm].id, jwmarker[mm].dpos); + exit(1); + } + } + } + + /****** if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == tileno)) */ + if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == acc_tpno)) + /* we got a specification from this tile part onwards */ + hprot = j2k->cp->hprot_TPH[tilespec++]; + + /* must this TPH have an EPB MS? */ + if (j2k->cp->epb_on && (hprot > 0)) { + + /* Create the EPB */ + if ((epb_mark = jpwl_epb_create( + j2k, /* this encoder handle */ + OPJ_FALSE, /* is it the latest? in TPH, no for now (if huge data size in TPH, we'd need more) */ + OPJ_TRUE, /* is it packed? yes for now */ + tileno, /* we are in TPH */ + epb_index++, /* its index is 0 (first) */ + hprot, /* protection type parameters of following data */ + sot_len + 2, /* pre-data length: only SOT */ + left_THmarks_len /* post-data length: from SOT end to SOD inclusive */ + ))) { + + /* Add this marker to the 'insertanda' list */ + if (jwmarker_num < JPWL_MAX_NO_MARKERS) { + jwmarker[jwmarker_num].id = J2K_MS_EPB; /* its type */ + jwmarker[jwmarker_num].m.epbmark = epb_mark; /* the EPB */ + /****** jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].start_pos + sot_len + 2; */ /* after SOT */ + jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2; /* after SOT */ + jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos; /* first first first! */ + jwmarker[jwmarker_num].len = epb_mark->Lepb; /* its length */ + jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ + jwmarker_num++; + } + + /* update Psot of the tile */ + Psot += epb_mark->Lepb + 2; + + opj_event_msg(j2k->cinfo, EVT_INFO, + /***** "TPH EPB : tile %02d, prot. %d\n", */ + "TPH EPB : tile %02d, part %02d, prot. %d\n", + /***** tileno, */ + tileno, tpno, + hprot + ); + + /* save this TPH EPB address */ + tph_epb = epb_mark; + + } else { + /* ooops, problems */ + /****** opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB #%d\n", tileno); */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB in #%d,d\n", tileno, tpno); + }; + + } + + startpack = 0; + /* EPB MSs for UEP packet data protection in Tile Parts */ + /****** for (packno = 0; packno < j2k->cstr_info->num; packno++) { */ + /*first_tp_pack = (tpno > 0) ? (first_tp_pack + j2k->cstr_info->tile[tileno].tp[tpno - 1].tp_numpacks) : 0;*/ + first_tp_pack = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pack; + last_tp_pack = first_tp_pack + j2k->cstr_info->tile[tileno].tp[tpno].tp_numpacks - 1; + for (packno = 0; packno < j2k->cstr_info->tile[tileno].tp[tpno].tp_numpacks; packno++) { + + /******** if ((packspec < JPWL_MAX_NO_PACKSPECS) && + (j2k->cp->pprot_tileno[packspec] == tileno) && (j2k->cp->pprot_packno[packspec] == packno)) { */ + if ((packspec < JPWL_MAX_NO_PACKSPECS) && + (j2k->cp->pprot_tileno[packspec] == acc_tpno) && (j2k->cp->pprot_packno[packspec] == packno)) { + + /* we got a specification from this tile and packet onwards */ + /* print the previous spec */ + if (packno > 0) { + stoppack = packno - 1; + opj_event_msg(j2k->cinfo, EVT_INFO, + /***** "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", */ + "UEP EPBs: tile %02d, part %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", + /***** tileno, */ + tileno, tpno, + startpack, + stoppack, + /***** j2k->cstr_info->tile[tileno].packet[startpack].start_pos, */ + j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos, + /***** j2k->cstr_info->tile[tileno].packet[stoppack].end_pos, */ + j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos, + pprot); + + /***** prot_len = j2k->cstr_info->tile[tileno].packet[stoppack].end_pos + 1 - + j2k->cstr_info->tile[tileno].packet[startpack].start_pos; */ + prot_len = j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos + 1 - + j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos; + + /* + particular case: if this is the last header and the last packet, + then it is better to protect even the EOC marker + */ + /****** if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && + (stoppack == (j2k->cstr_info->num - 1))) */ + if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && + (tpno == (j2k->cstr_info->tile[tileno].num_tps - 1)) && + (stoppack == last_tp_pack)) + /* add the EOC len */ + prot_len += 2; + + /* let's add the EPBs */ + Psot += jpwl_epbs_add( + j2k, /* J2K handle */ + jwmarker, /* pointer to JPWL markers list */ + &jwmarker_num, /* pointer to the number of current markers */ + OPJ_FALSE, /* latest */ + OPJ_TRUE, /* packed */ + OPJ_FALSE, /* inside MH */ + &epb_index, /* pointer to EPB index */ + pprot, /* protection type */ + /****** (double) (j2k->cstr_info->tile[tileno].start_pos + sot_len + 2) + 0.0001, */ /* position */ + (double) (j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2) + 0.0001, /* position */ + tileno, /* number of tile */ + 0, /* length of pre-data */ + prot_len /*4000*/ /* length of post-data */ + ); + } + + startpack = packno; + pprot = j2k->cp->pprot[packspec++]; + } + + /*printf("Tile %02d, pack %02d ==> %d\n", tileno, packno, pprot);*/ + + } + + /* we are at the end: print the remaining spec */ + stoppack = packno - 1; + if (pprot >= 0) { + + opj_event_msg(j2k->cinfo, EVT_INFO, + /**** "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", */ + "UEP EPBs: tile %02d, part %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", + /**** tileno, */ + tileno, tpno, + startpack, + stoppack, + /***** j2k->image_info->tile[tileno].packet[startpack].start_pos, + j2k->image_info->tile[tileno].packet[stoppack].end_pos, */ + j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos, + j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos, + pprot); + + /***** prot_len = j2k->cstr_info->tile[tileno].packet[stoppack].end_pos + 1 - + j2k->cstr_info->tile[tileno].packet[startpack].start_pos; */ + prot_len = j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos + 1 - + j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos; + + /* + particular case: if this is the last header and the last packet, + then it is better to protect even the EOC marker + */ + /***** if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && + (stoppack == (j2k->cstr_info->num - 1))) */ + if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && + (tpno == (j2k->cstr_info->tile[tileno].num_tps - 1)) && + (stoppack == last_tp_pack)) + /* add the EOC len */ + prot_len += 2; + + /* let's add the EPBs */ + Psot += jpwl_epbs_add( + j2k, /* J2K handle */ + jwmarker, /* pointer to JPWL markers list */ + &jwmarker_num, /* pointer to the number of current markers */ + OPJ_TRUE, /* latest */ + OPJ_TRUE, /* packed */ + OPJ_FALSE, /* inside MH */ + &epb_index, /* pointer to EPB index */ + pprot, /* protection type */ + /***** (double) (j2k->cstr_info->tile[tileno].start_pos + sot_len + 2) + 0.0001,*/ /* position */ + (double) (j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2) + 0.0001, /* position */ + tileno, /* number of tile */ + 0, /* length of pre-data */ + prot_len /*4000*/ /* length of post-data */ + ); + } + + /* we can now check if the TPH EPB was really the last one */ + if (tph_epb && (epb_index == 1)) { + /* set the TPH EPB to be the last one in current header */ + tph_epb->Depb |= (unsigned char) ((OPJ_TRUE & 0x0001) << 6); + tph_epb = NULL; + } + + /* write back Psot */ + cio_seek(cio, Psotp); + cio_write(cio, Psot, 4); + + } + + }; + + /* reset the position */ + cio_seek(cio, ciopos); } void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) { - int mm; - unsigned long int old_size = j2k->cstr_info->codestream_size; - unsigned long int new_size = old_size; - int /*ciopos = cio_tell(cio),*/ soc_pos = j2k->cstr_info->main_head_start; - unsigned char *jpwl_buf, *orig_buf; - unsigned long int orig_pos; - double epbcoding_time = 0.0, esdcoding_time = 0.0; + int mm; + unsigned long int old_size = j2k->cstr_info->codestream_size; + unsigned long int new_size = old_size; + int /*ciopos = cio_tell(cio),*/ soc_pos = j2k->cstr_info->main_head_start; + unsigned char *jpwl_buf, *orig_buf; + unsigned long int orig_pos; + double epbcoding_time = 0.0, esdcoding_time = 0.0; (void)image; - /* Order JPWL markers according to their wishlist position */ - qsort((void *) jwmarker, (size_t) jwmarker_num, sizeof (jpwl_marker_t), jpwl_markcomp); - - /* compute markers total size */ - for (mm = 0; mm < jwmarker_num; mm++) { - /*printf("%x, %d, %.10f, %d long\n", jwmarker[mm].id, jwmarker[mm].pos, - jwmarker[mm].dpos, jwmarker[mm].len);*/ - new_size += jwmarker[mm].len + 2; - } - - /* allocate a new buffer of proper size */ - if (!(jpwl_buf = (unsigned char *) opj_malloc((size_t) (new_size + soc_pos) * sizeof(unsigned char)))) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for JPWL codestream buffer\n"); - exit(1); - }; - - /* copy the jp2 part, if any */ - orig_buf = jpwl_buf; - memcpy(jpwl_buf, cio->buffer, soc_pos); - jpwl_buf += soc_pos; - - /* cycle through markers */ - orig_pos = soc_pos + 0; /* start from the beginning */ - cio_seek(cio, soc_pos + 0); /* rewind the original */ - for (mm = 0; mm < jwmarker_num; mm++) { - - /* - need to copy a piece of the original codestream - if there is such - */ - memcpy(jpwl_buf, cio_getbp(cio), jwmarker[mm].pos - orig_pos); - jpwl_buf += jwmarker[mm].pos - orig_pos; - orig_pos = jwmarker[mm].pos; - cio_seek(cio, orig_pos); - - /* - then write down the marker - */ - switch (jwmarker[mm].id) { - - case J2K_MS_EPB: - jpwl_epb_write(j2k, jwmarker[mm].m.epbmark, jpwl_buf); - break; - - case J2K_MS_EPC: - jpwl_epc_write(j2k, jwmarker[mm].m.epcmark, jpwl_buf); - break; - - case J2K_MS_ESD: - jpwl_esd_write(j2k, jwmarker[mm].m.esdmark, jpwl_buf); - break; - - case J2K_MS_RED: - memset(jpwl_buf, 0, jwmarker[mm].len + 2); /* placeholder */ - break; - - default: - break; - }; - - /* we update the markers struct */ - if (j2k->cstr_info) - j2k->cstr_info->marker[j2k->cstr_info->marknum - 1].pos = (jpwl_buf - orig_buf); - - /* we set the marker dpos to the new position in the JPWL codestream */ - jwmarker[mm].dpos = (double) (jpwl_buf - orig_buf); - - /* advance JPWL buffer position */ - jpwl_buf += jwmarker[mm].len + 2; - - } - - /* finish remaining original codestream */ - memcpy(jpwl_buf, cio_getbp(cio), old_size - (orig_pos - soc_pos)); - jpwl_buf += old_size - (orig_pos - soc_pos); - cio_seek(cio, soc_pos + old_size); - - /* - update info file based on added markers - */ - if (!jpwl_update_info(j2k, jwmarker, jwmarker_num)) - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not update OPJ cstr_info structure\n"); - - /* now we need to repass some markers and fill their data fields */ - - /* first of all, DL and Pcrc in EPCs */ - for (mm = 0; mm < jwmarker_num; mm++) { - - /* find the EPCs */ - if (jwmarker[mm].id == J2K_MS_EPC) { - - int epc_pos = (int) jwmarker[mm].dpos, pp; - unsigned short int mycrc = 0x0000; - - /* fix and fill the DL field */ - jwmarker[mm].m.epcmark->DL = new_size; - orig_buf[epc_pos + 6] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 24); - orig_buf[epc_pos + 7] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 16); - orig_buf[epc_pos + 8] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 8); - orig_buf[epc_pos + 9] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 0); - - /* compute the CRC field (excluding itself) */ - for (pp = 0; pp < 4; pp++) - jpwl_updateCRC16(&mycrc, orig_buf[epc_pos + pp]); - for (pp = 6; pp < (jwmarker[mm].len + 2); pp++) - jpwl_updateCRC16(&mycrc, orig_buf[epc_pos + pp]); - - /* fix and fill the CRC */ - jwmarker[mm].m.epcmark->Pcrc = mycrc; - orig_buf[epc_pos + 4] = (unsigned char) (jwmarker[mm].m.epcmark->Pcrc >> 8); - orig_buf[epc_pos + 5] = (unsigned char) (jwmarker[mm].m.epcmark->Pcrc >> 0); - - } - } - - /* then, sensitivity data in ESDs */ - esdcoding_time = opj_clock(); - for (mm = 0; mm < jwmarker_num; mm++) { - - /* find the ESDs */ - if (jwmarker[mm].id == J2K_MS_ESD) { - - /* remember that they are now in a new position (dpos) */ - int esd_pos = (int) jwmarker[mm].dpos; - - jpwl_esd_fill(j2k, jwmarker[mm].m.esdmark, &orig_buf[esd_pos]); - - } - - } - esdcoding_time = opj_clock() - esdcoding_time; - if (j2k->cp->esd_on) - opj_event_msg(j2k->cinfo, EVT_INFO, "ESDs sensitivities computed in %f s\n", esdcoding_time); - - /* finally, RS or CRC parity in EPBs */ - epbcoding_time = opj_clock(); - for (mm = 0; mm < jwmarker_num; mm++) { - - /* find the EPBs */ - if (jwmarker[mm].id == J2K_MS_EPB) { - - /* remember that they are now in a new position (dpos) */ - int nn, accum_len; - - /* let's see how many EPBs are following this one, included itself */ - /* for this to work, we suppose that the markers are correctly ordered */ - /* and, overall, that they are in packed mode inside headers */ - accum_len = 0; - for (nn = mm; (nn < jwmarker_num) && (jwmarker[nn].id == J2K_MS_EPB) && - (jwmarker[nn].pos == jwmarker[mm].pos); nn++) - accum_len += jwmarker[nn].m.epbmark->Lepb + 2; - - /* fill the current (first) EPB with post-data starting from the computed position */ - jpwl_epb_fill(j2k, jwmarker[mm].m.epbmark, &orig_buf[(int) jwmarker[mm].dpos], - &orig_buf[(int) jwmarker[mm].dpos + accum_len]); - - /* fill the remaining EPBs in the header with post-data starting from the last position */ - for (nn = mm + 1; (nn < jwmarker_num) && (jwmarker[nn].id == J2K_MS_EPB) && - (jwmarker[nn].pos == jwmarker[mm].pos); nn++) - jpwl_epb_fill(j2k, jwmarker[nn].m.epbmark, &orig_buf[(int) jwmarker[nn].dpos], NULL); - - /* skip all the processed EPBs */ - mm = nn - 1; - } - - } - epbcoding_time = opj_clock() - epbcoding_time; - if (j2k->cp->epb_on) - opj_event_msg(j2k->cinfo, EVT_INFO, "EPBs redundancy computed in %f s\n", epbcoding_time); - - /* free original cio buffer and set it to the JPWL one */ - opj_free(cio->buffer); - cio->cinfo = cio->cinfo; /* no change */ - cio->openmode = cio->openmode; /* no change */ - cio->buffer = orig_buf; - cio->length = new_size + soc_pos; - cio->start = cio->buffer; - cio->end = cio->buffer + cio->length; - cio->bp = cio->buffer; - cio_seek(cio, soc_pos + new_size); + /* Order JPWL markers according to their wishlist position */ + qsort((void *) jwmarker, (size_t) jwmarker_num, sizeof (jpwl_marker_t), jpwl_markcomp); + + /* compute markers total size */ + for (mm = 0; mm < jwmarker_num; mm++) { + /*printf("%x, %d, %.10f, %d long\n", jwmarker[mm].id, jwmarker[mm].pos, + jwmarker[mm].dpos, jwmarker[mm].len);*/ + new_size += jwmarker[mm].len + 2; + } + + /* allocate a new buffer of proper size */ + if (!(jpwl_buf = (unsigned char *) opj_malloc((size_t) (new_size + soc_pos) * sizeof(unsigned char)))) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for JPWL codestream buffer\n"); + exit(1); + }; + + /* copy the jp2 part, if any */ + orig_buf = jpwl_buf; + memcpy(jpwl_buf, cio->buffer, soc_pos); + jpwl_buf += soc_pos; + + /* cycle through markers */ + orig_pos = soc_pos + 0; /* start from the beginning */ + cio_seek(cio, soc_pos + 0); /* rewind the original */ + for (mm = 0; mm < jwmarker_num; mm++) { + + /* + need to copy a piece of the original codestream + if there is such + */ + memcpy(jpwl_buf, cio_getbp(cio), jwmarker[mm].pos - orig_pos); + jpwl_buf += jwmarker[mm].pos - orig_pos; + orig_pos = jwmarker[mm].pos; + cio_seek(cio, orig_pos); + + /* + then write down the marker + */ + switch (jwmarker[mm].id) { + + case J2K_MS_EPB: + jpwl_epb_write(j2k, jwmarker[mm].m.epbmark, jpwl_buf); + break; + + case J2K_MS_EPC: + jpwl_epc_write(j2k, jwmarker[mm].m.epcmark, jpwl_buf); + break; + + case J2K_MS_ESD: + jpwl_esd_write(j2k, jwmarker[mm].m.esdmark, jpwl_buf); + break; + + case J2K_MS_RED: + memset(jpwl_buf, 0, jwmarker[mm].len + 2); /* placeholder */ + break; + + default: + break; + }; + + /* we update the markers struct */ + if (j2k->cstr_info) + j2k->cstr_info->marker[j2k->cstr_info->marknum - 1].pos = (jpwl_buf - orig_buf); + + /* we set the marker dpos to the new position in the JPWL codestream */ + jwmarker[mm].dpos = (double) (jpwl_buf - orig_buf); + + /* advance JPWL buffer position */ + jpwl_buf += jwmarker[mm].len + 2; + + } + + /* finish remaining original codestream */ + memcpy(jpwl_buf, cio_getbp(cio), old_size - (orig_pos - soc_pos)); + jpwl_buf += old_size - (orig_pos - soc_pos); + cio_seek(cio, soc_pos + old_size); + + /* + update info file based on added markers + */ + if (!jpwl_update_info(j2k, jwmarker, jwmarker_num)) + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not update OPJ cstr_info structure\n"); + + /* now we need to repass some markers and fill their data fields */ + + /* first of all, DL and Pcrc in EPCs */ + for (mm = 0; mm < jwmarker_num; mm++) { + + /* find the EPCs */ + if (jwmarker[mm].id == J2K_MS_EPC) { + + int epc_pos = (int) jwmarker[mm].dpos, pp; + unsigned short int mycrc = 0x0000; + + /* fix and fill the DL field */ + jwmarker[mm].m.epcmark->DL = new_size; + orig_buf[epc_pos + 6] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 24); + orig_buf[epc_pos + 7] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 16); + orig_buf[epc_pos + 8] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 8); + orig_buf[epc_pos + 9] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 0); + + /* compute the CRC field (excluding itself) */ + for (pp = 0; pp < 4; pp++) + jpwl_updateCRC16(&mycrc, orig_buf[epc_pos + pp]); + for (pp = 6; pp < (jwmarker[mm].len + 2); pp++) + jpwl_updateCRC16(&mycrc, orig_buf[epc_pos + pp]); + + /* fix and fill the CRC */ + jwmarker[mm].m.epcmark->Pcrc = mycrc; + orig_buf[epc_pos + 4] = (unsigned char) (jwmarker[mm].m.epcmark->Pcrc >> 8); + orig_buf[epc_pos + 5] = (unsigned char) (jwmarker[mm].m.epcmark->Pcrc >> 0); + + } + } + + /* then, sensitivity data in ESDs */ + esdcoding_time = opj_clock(); + for (mm = 0; mm < jwmarker_num; mm++) { + + /* find the ESDs */ + if (jwmarker[mm].id == J2K_MS_ESD) { + + /* remember that they are now in a new position (dpos) */ + int esd_pos = (int) jwmarker[mm].dpos; + + jpwl_esd_fill(j2k, jwmarker[mm].m.esdmark, &orig_buf[esd_pos]); + + } + + } + esdcoding_time = opj_clock() - esdcoding_time; + if (j2k->cp->esd_on) + opj_event_msg(j2k->cinfo, EVT_INFO, "ESDs sensitivities computed in %f s\n", esdcoding_time); + + /* finally, RS or CRC parity in EPBs */ + epbcoding_time = opj_clock(); + for (mm = 0; mm < jwmarker_num; mm++) { + + /* find the EPBs */ + if (jwmarker[mm].id == J2K_MS_EPB) { + + /* remember that they are now in a new position (dpos) */ + int nn, accum_len; + + /* let's see how many EPBs are following this one, included itself */ + /* for this to work, we suppose that the markers are correctly ordered */ + /* and, overall, that they are in packed mode inside headers */ + accum_len = 0; + for (nn = mm; (nn < jwmarker_num) && (jwmarker[nn].id == J2K_MS_EPB) && + (jwmarker[nn].pos == jwmarker[mm].pos); nn++) + accum_len += jwmarker[nn].m.epbmark->Lepb + 2; + + /* fill the current (first) EPB with post-data starting from the computed position */ + jpwl_epb_fill(j2k, jwmarker[mm].m.epbmark, &orig_buf[(int) jwmarker[mm].dpos], + &orig_buf[(int) jwmarker[mm].dpos + accum_len]); + + /* fill the remaining EPBs in the header with post-data starting from the last position */ + for (nn = mm + 1; (nn < jwmarker_num) && (jwmarker[nn].id == J2K_MS_EPB) && + (jwmarker[nn].pos == jwmarker[mm].pos); nn++) + jpwl_epb_fill(j2k, jwmarker[nn].m.epbmark, &orig_buf[(int) jwmarker[nn].dpos], NULL); + + /* skip all the processed EPBs */ + mm = nn - 1; + } + + } + epbcoding_time = opj_clock() - epbcoding_time; + if (j2k->cp->epb_on) + opj_event_msg(j2k->cinfo, EVT_INFO, "EPBs redundancy computed in %f s\n", epbcoding_time); + + /* free original cio buffer and set it to the JPWL one */ + opj_free(cio->buffer); + cio->cinfo = cio->cinfo; /* no change */ + cio->openmode = cio->openmode; /* no change */ + cio->buffer = orig_buf; + cio->length = new_size + soc_pos; + cio->start = cio->buffer; + cio->end = cio->buffer + cio->length; + cio->bp = cio->buffer; + cio_seek(cio, soc_pos + new_size); } void j2k_read_epc(opj_j2k_t *j2k) { - unsigned long int DL, Lepcp, Pcrcp, l; - unsigned short int Lepc, Pcrc = 0x0000; - unsigned char Pepc; - opj_cio_t *cio = j2k->cio; - const char *ans1; - - /* Simply read the EPC parameters */ - Lepcp = cio_tell(cio); - Lepc = cio_read(cio, 2); - Pcrcp = cio_tell(cio); - cio_skip(cio, 2); /* Pcrc */ - DL = cio_read(cio, 4); - Pepc = cio_read(cio, 1); - - /* compute Pcrc */ - cio_seek(cio, Lepcp - 2); - - /* Marker */ - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - - /* Length */ - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - - /* skip Pcrc */ - cio_skip(cio, 2); - - /* read all remaining */ - for (l = 4; l < Lepc; l++) - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - - /* check Pcrc with the result */ - cio_seek(cio, Pcrcp); - ans1 = (Pcrc == (unsigned short int) cio_read(cio, 2)) ? "crc-ok" : "crc-ko"; - - /* now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "EPC(%u,%d): %s, DL=%d%s %s %s\n", - Lepcp - 2, - Lepc, - ans1, - DL, /* data length this EPC is referring to */ - (Pepc & 0x10) ? ", esd" : "", /* ESD is present */ - (Pepc & 0x20) ? ", red" : "", /* RED is present */ - (Pepc & 0x40) ? ", epb" : ""); /* EPB is present */ - - cio_seek(cio, Lepcp + Lepc); + unsigned long int DL, Lepcp, Pcrcp, l; + unsigned short int Lepc, Pcrc = 0x0000; + unsigned char Pepc; + opj_cio_t *cio = j2k->cio; + const char *ans1; + + /* Simply read the EPC parameters */ + Lepcp = cio_tell(cio); + Lepc = cio_read(cio, 2); + Pcrcp = cio_tell(cio); + cio_skip(cio, 2); /* Pcrc */ + DL = cio_read(cio, 4); + Pepc = cio_read(cio, 1); + + /* compute Pcrc */ + cio_seek(cio, Lepcp - 2); + + /* Marker */ + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + + /* Length */ + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + + /* skip Pcrc */ + cio_skip(cio, 2); + + /* read all remaining */ + for (l = 4; l < Lepc; l++) + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + + /* check Pcrc with the result */ + cio_seek(cio, Pcrcp); + ans1 = (Pcrc == (unsigned short int) cio_read(cio, 2)) ? "crc-ok" : "crc-ko"; + + /* now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "EPC(%u,%d): %s, DL=%d%s %s %s\n", + Lepcp - 2, + Lepc, + ans1, + DL, /* data length this EPC is referring to */ + (Pepc & 0x10) ? ", esd" : "", /* ESD is present */ + (Pepc & 0x20) ? ", red" : "", /* RED is present */ + (Pepc & 0x40) ? ", epb" : ""); /* EPB is present */ + + cio_seek(cio, Lepcp + Lepc); } #if 0 void j2k_write_epc(opj_j2k_t *j2k) { - unsigned long int DL, Lepcp, Pcrcp, l; - unsigned short int Lepc, Pcrc; - unsigned char Pepc; + unsigned long int DL, Lepcp, Pcrcp, l; + unsigned short int Lepc, Pcrc; + unsigned char Pepc; - opj_cio_t *cio = j2k->cio; + opj_cio_t *cio = j2k->cio; - cio_write(cio, J2K_MS_EPC, 2); /* EPC */ - Lepcp = cio_tell(cio); - cio_skip(cio, 2); + cio_write(cio, J2K_MS_EPC, 2); /* EPC */ + Lepcp = cio_tell(cio); + cio_skip(cio, 2); - /* CRC-16 word of the EPC */ - Pcrc = 0x0000; /* initialize */ - Pcrcp = cio_tell(cio); - cio_write(cio, Pcrc, 2); /* Pcrc placeholder*/ + /* CRC-16 word of the EPC */ + Pcrc = 0x0000; /* initialize */ + Pcrcp = cio_tell(cio); + cio_write(cio, Pcrc, 2); /* Pcrc placeholder*/ - /* data length of the EPC protection domain */ - DL = 0x00000000; /* we leave this set to 0, as if the information is not available */ - cio_write(cio, DL, 4); /* DL */ + /* data length of the EPC protection domain */ + DL = 0x00000000; /* we leave this set to 0, as if the information is not available */ + cio_write(cio, DL, 4); /* DL */ - /* jpwl capabilities */ - Pepc = 0x00; - cio_write(cio, Pepc, 1); /* Pepc */ + /* jpwl capabilities */ + Pepc = 0x00; + cio_write(cio, Pepc, 1); /* Pepc */ - /* ID section */ - /* no ID's, as of now */ + /* ID section */ + /* no ID's, as of now */ - Lepc = (unsigned short) (cio_tell(cio) - Lepcp); - cio_seek(cio, Lepcp); - cio_write(cio, Lepc, 2); /* Lepc */ + Lepc = (unsigned short) (cio_tell(cio) - Lepcp); + cio_seek(cio, Lepcp); + cio_write(cio, Lepc, 2); /* Lepc */ - /* compute Pcrc */ - cio_seek(cio, Lepcp - 2); + /* compute Pcrc */ + cio_seek(cio, Lepcp - 2); - /* Marker */ - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + /* Marker */ + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - /* Length */ - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + /* Length */ + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - /* skip Pcrc */ - cio_skip(cio, 2); + /* skip Pcrc */ + cio_skip(cio, 2); - /* read all remaining */ - for (l = 4; l < Lepc; l++) - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + /* read all remaining */ + for (l = 4; l < Lepc; l++) + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - /* fill Pcrc with the result */ - cio_seek(cio, Pcrcp); - cio_write(cio, Pcrc, 2); + /* fill Pcrc with the result */ + cio_seek(cio, Pcrcp); + cio_write(cio, Pcrc, 2); - cio_seek(cio, Lepcp + Lepc); + cio_seek(cio, Lepcp + Lepc); - /* marker struct update */ - j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, Lepcp - 2, Lepc + 2); + /* marker struct update */ + j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, Lepcp - 2, Lepc + 2); } #endif void j2k_read_epb(opj_j2k_t *j2k) { - unsigned long int LDPepb, Pepb; - unsigned short int Lepb; - unsigned char Depb; - char str1[25] = ""; - opj_bool status; - static opj_bool first_in_tph = OPJ_TRUE; - int type, pre_len, post_len; - static unsigned char *redund = NULL; - - opj_cio_t *cio = j2k->cio; - - /* B/W = 45, RGB = 51 */ - /* SIZ SIZ_FIELDS SIZ_COMPS FOLLOWING_MARKER */ - int skipnum = 2 + 38 + 3 * j2k->cp->exp_comps + 2; - - if (j2k->cp->correct) { - - /* go back to EPB marker value */ - cio_seek(cio, cio_tell(cio) - 2); - - /* we need to understand where we are */ - if (j2k->state == J2K_STATE_MH) { - /* we are in MH */ - type = 0; /* MH */ - pre_len = skipnum; /* SOC+SIZ */ - post_len = -1; /* auto */ - - } else if ((j2k->state == J2K_STATE_TPH) && first_in_tph) { - /* we are in TPH */ - type = 1; /* TPH */ - pre_len = 12; /* SOC+SIZ */ - first_in_tph = OPJ_FALSE; - post_len = -1; /* auto */ - - } else { - /* we are elsewhere */ - type = 2; /* other */ - pre_len = 0; /* nada */ - post_len = -1; /* auto */ - - } - - /* call EPB corrector */ - /*printf("before %x, ", redund);*/ - status = jpwl_epb_correct(j2k, /* J2K decompressor handle */ - cio->bp, /* pointer to EPB in codestream buffer */ - type, /* EPB type: MH */ - pre_len, /* length of pre-data */ - post_len, /* length of post-data: -1 means auto */ - NULL, /* do everything auto */ - &redund - ); - /*printf("after %x\n", redund);*/ - - /* Read the (possibly corrected) EPB parameters */ - cio_skip(cio, 2); - Lepb = cio_read(cio, 2); - Depb = cio_read(cio, 1); - LDPepb = cio_read(cio, 4); - Pepb = cio_read(cio, 4); - - if (!status) { - - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL correction could not be performed\n"); - - /* advance to EPB endpoint */ - cio_skip(cio, Lepb + 2); - - return; - } - - /* last in current header? */ - if (Depb & 0x40) { - redund = NULL; /* reset the pointer to L4 buffer */ - first_in_tph = OPJ_TRUE; - } - - /* advance to EPB endpoint */ - cio_skip(cio, Lepb - 11); - - } else { - - /* Simply read the EPB parameters */ - Lepb = cio_read(cio, 2); - Depb = cio_read(cio, 1); - LDPepb = cio_read(cio, 4); - Pepb = cio_read(cio, 4); - - /* What does Pepb tells us about the protection method? */ - if (((Pepb & 0xF0000000) >> 28) == 0) - sprintf(str1, "pred"); /* predefined */ - else if (((Pepb & 0xF0000000) >> 28) == 1) - sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */ - else if (((Pepb & 0xF0000000) >> 28) == 2) - sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */ - else if (Pepb == 0xFFFFFFFF) - sprintf(str1, "nometh"); /* RS mode */ - else - sprintf(str1, "unknown"); /* unknown */ - - /* Now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "EPB(%d): (%sl, %sp, %u), %lu, %s\n", - cio_tell(cio) - 13, - (Depb & 0x40) ? "" : "n", /* latest EPB or not? */ - (Depb & 0x80) ? "" : "n", /* packed or unpacked EPB? */ - (Depb & 0x3F), /* EPB index value */ - LDPepb, /*length of the data protected by the EPB */ - str1); /* protection method */ - - cio_skip(cio, Lepb - 11); - } + unsigned long int LDPepb, Pepb; + unsigned short int Lepb; + unsigned char Depb; + char str1[25] = ""; + opj_bool status; + static opj_bool first_in_tph = OPJ_TRUE; + int type, pre_len, post_len; + static unsigned char *redund = NULL; + + opj_cio_t *cio = j2k->cio; + + /* B/W = 45, RGB = 51 */ + /* SIZ SIZ_FIELDS SIZ_COMPS FOLLOWING_MARKER */ + int skipnum = 2 + 38 + 3 * j2k->cp->exp_comps + 2; + + if (j2k->cp->correct) { + + /* go back to EPB marker value */ + cio_seek(cio, cio_tell(cio) - 2); + + /* we need to understand where we are */ + if (j2k->state == J2K_STATE_MH) { + /* we are in MH */ + type = 0; /* MH */ + pre_len = skipnum; /* SOC+SIZ */ + post_len = -1; /* auto */ + + } else if ((j2k->state == J2K_STATE_TPH) && first_in_tph) { + /* we are in TPH */ + type = 1; /* TPH */ + pre_len = 12; /* SOC+SIZ */ + first_in_tph = OPJ_FALSE; + post_len = -1; /* auto */ + + } else { + /* we are elsewhere */ + type = 2; /* other */ + pre_len = 0; /* nada */ + post_len = -1; /* auto */ + + } + + /* call EPB corrector */ + /*printf("before %x, ", redund);*/ + status = jpwl_epb_correct(j2k, /* J2K decompressor handle */ + cio->bp, /* pointer to EPB in codestream buffer */ + type, /* EPB type: MH */ + pre_len, /* length of pre-data */ + post_len, /* length of post-data: -1 means auto */ + NULL, /* do everything auto */ + &redund + ); + /*printf("after %x\n", redund);*/ + + /* Read the (possibly corrected) EPB parameters */ + cio_skip(cio, 2); + Lepb = cio_read(cio, 2); + Depb = cio_read(cio, 1); + LDPepb = cio_read(cio, 4); + Pepb = cio_read(cio, 4); + + if (!status) { + + opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL correction could not be performed\n"); + + /* advance to EPB endpoint */ + cio_skip(cio, Lepb + 2); + + return; + } + + /* last in current header? */ + if (Depb & 0x40) { + redund = NULL; /* reset the pointer to L4 buffer */ + first_in_tph = OPJ_TRUE; + } + + /* advance to EPB endpoint */ + cio_skip(cio, Lepb - 11); + + } else { + + /* Simply read the EPB parameters */ + Lepb = cio_read(cio, 2); + Depb = cio_read(cio, 1); + LDPepb = cio_read(cio, 4); + Pepb = cio_read(cio, 4); + + /* What does Pepb tells us about the protection method? */ + if (((Pepb & 0xF0000000) >> 28) == 0) + sprintf(str1, "pred"); /* predefined */ + else if (((Pepb & 0xF0000000) >> 28) == 1) + sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */ + else if (((Pepb & 0xF0000000) >> 28) == 2) + sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */ + else if (Pepb == 0xFFFFFFFF) + sprintf(str1, "nometh"); /* RS mode */ + else + sprintf(str1, "unknown"); /* unknown */ + + /* Now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "EPB(%d): (%sl, %sp, %u), %lu, %s\n", + cio_tell(cio) - 13, + (Depb & 0x40) ? "" : "n", /* latest EPB or not? */ + (Depb & 0x80) ? "" : "n", /* packed or unpacked EPB? */ + (Depb & 0x3F), /* EPB index value */ + LDPepb, /*length of the data protected by the EPB */ + str1); /* protection method */ + + cio_skip(cio, Lepb - 11); + } } void j2k_write_epb(opj_j2k_t *j2k) { - unsigned long int LDPepb, Pepb, Lepbp; - unsigned short int Lepb; - unsigned char Depb; + unsigned long int LDPepb, Pepb, Lepbp; + unsigned short int Lepb; + unsigned char Depb; - opj_cio_t *cio = j2k->cio; + opj_cio_t *cio = j2k->cio; - cio_write(cio, J2K_MS_EPB, 2); /* EPB */ - Lepbp = cio_tell(cio); - cio_skip(cio, 2); + cio_write(cio, J2K_MS_EPB, 2); /* EPB */ + Lepbp = cio_tell(cio); + cio_skip(cio, 2); - /* EPB style */ - Depb = 0x00; /* test */ - cio_write(cio, Depb, 1); /* Depb */ + /* EPB style */ + Depb = 0x00; /* test */ + cio_write(cio, Depb, 1); /* Depb */ - /* length of the data to be protected by this EPB */ - LDPepb = 0x00000000; /* test */ - cio_write(cio, LDPepb, 4); /* LDPepb */ + /* length of the data to be protected by this EPB */ + LDPepb = 0x00000000; /* test */ + cio_write(cio, LDPepb, 4); /* LDPepb */ - /* next error correction tool */ - Pepb = 0x00000000; /* test */ - cio_write(cio, Pepb, 4); /* Pepb */ + /* next error correction tool */ + Pepb = 0x00000000; /* test */ + cio_write(cio, Pepb, 4); /* Pepb */ - /* EPB data */ - /* no data, as of now */ + /* EPB data */ + /* no data, as of now */ - Lepb = (unsigned short) (cio_tell(cio) - Lepbp); - cio_seek(cio, Lepbp); - cio_write(cio, Lepb, 2); /* Lepb */ + Lepb = (unsigned short) (cio_tell(cio) - Lepbp); + cio_seek(cio, Lepbp); + cio_write(cio, Lepb, 2); /* Lepb */ - cio_seek(cio, Lepbp + Lepb); + cio_seek(cio, Lepbp + Lepb); - /* marker struct update */ - j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, Lepbp - 2, Lepb + 2); + /* marker struct update */ + j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, Lepbp - 2, Lepb + 2); } void j2k_read_esd(opj_j2k_t *j2k) { - unsigned short int Lesd, Cesd; - unsigned char Pesd; - - int cesdsize = (j2k->image->numcomps >= 257) ? 2 : 1; - - char str1[4][4] = {"p", "br", "pr", "res"}; - char str2[8][8] = {"res", "mse", "mse-r", "psnr", "psnr-i", "maxerr", "tse", "res"}; - - opj_cio_t *cio = j2k->cio; - - /* Simply read the ESD parameters */ - Lesd = cio_read(cio, 2); - Cesd = cio_read(cio, cesdsize); - Pesd = cio_read(cio, 1); - - /* Now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "ESD(%d): c%d, %s, %s, %s, %s, %s\n", - cio_tell(cio) - (5 + cesdsize), - Cesd, /* component number for this ESD */ - str1[(Pesd & (unsigned char) 0xC0) >> 6], /* addressing mode */ - str2[(Pesd & (unsigned char) 0x38) >> 3], /* sensitivity type */ - ((Pesd & (unsigned char) 0x04) >> 2) ? "2Bs" : "1Bs", - ((Pesd & (unsigned char) 0x02) >> 1) ? "4Ba" : "2Ba", - (Pesd & (unsigned char) 0x01) ? "avgc" : ""); - - cio_skip(cio, Lesd - (3 + cesdsize)); + unsigned short int Lesd, Cesd; + unsigned char Pesd; + + int cesdsize = (j2k->image->numcomps >= 257) ? 2 : 1; + + char str1[4][4] = {"p", "br", "pr", "res"}; + char str2[8][8] = {"res", "mse", "mse-r", "psnr", "psnr-i", "maxerr", "tse", "res"}; + + opj_cio_t *cio = j2k->cio; + + /* Simply read the ESD parameters */ + Lesd = cio_read(cio, 2); + Cesd = cio_read(cio, cesdsize); + Pesd = cio_read(cio, 1); + + /* Now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "ESD(%d): c%d, %s, %s, %s, %s, %s\n", + cio_tell(cio) - (5 + cesdsize), + Cesd, /* component number for this ESD */ + str1[(Pesd & (unsigned char) 0xC0) >> 6], /* addressing mode */ + str2[(Pesd & (unsigned char) 0x38) >> 3], /* sensitivity type */ + ((Pesd & (unsigned char) 0x04) >> 2) ? "2Bs" : "1Bs", + ((Pesd & (unsigned char) 0x02) >> 1) ? "4Ba" : "2Ba", + (Pesd & (unsigned char) 0x01) ? "avgc" : ""); + + cio_skip(cio, Lesd - (3 + cesdsize)); } void j2k_read_red(opj_j2k_t *j2k) { - unsigned short int Lred; - unsigned char Pred; - char str1[4][4] = {"p", "br", "pr", "res"}; - - opj_cio_t *cio = j2k->cio; - - /* Simply read the RED parameters */ - Lred = cio_read(cio, 2); - Pred = cio_read(cio, 1); - - /* Now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "RED(%d): %s, %dc, %s, %s\n", - cio_tell(cio) - 5, - str1[(Pred & (unsigned char) 0xC0) >> 6], /* addressing mode */ - (Pred & (unsigned char) 0x38) >> 3, /* corruption level */ - ((Pred & (unsigned char) 0x02) >> 1) ? "4Ba" : "2Ba", /* address range */ - (Pred & (unsigned char) 0x01) ? "errs" : "free"); /* error free? */ - - cio_skip(cio, Lred - 3); + unsigned short int Lred; + unsigned char Pred; + char str1[4][4] = {"p", "br", "pr", "res"}; + + opj_cio_t *cio = j2k->cio; + + /* Simply read the RED parameters */ + Lred = cio_read(cio, 2); + Pred = cio_read(cio, 1); + + /* Now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "RED(%d): %s, %dc, %s, %s\n", + cio_tell(cio) - 5, + str1[(Pred & (unsigned char) 0xC0) >> 6], /* addressing mode */ + (Pred & (unsigned char) 0x38) >> 3, /* corruption level */ + ((Pred & (unsigned char) 0x02) >> 1) ? "4Ba" : "2Ba", /* address range */ + (Pred & (unsigned char) 0x01) ? "errs" : "free"); /* error free? */ + + cio_skip(cio, Lred - 3); } opj_bool jpwl_check_tile(opj_j2k_t *j2k, opj_tcd_t *tcd, int tileno) { #ifdef oerhgierhgvhreit4u - /* - we navigate through the tile and find possible invalid parameters: + /* + we navigate through the tile and find possible invalid parameters: this saves a lot of crashes!!!!! - */ - int compno, resno, precno, /*layno,*/ bandno, blockno; - int numprecincts, numblocks; - - /* this is the selected tile */ - opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[tileno]); - - /* will keep the component */ - opj_tcd_tilecomp_t *comp = NULL; - - /* will keep the resolution */ - opj_tcd_resolution_t *res; - - /* will keep the subband */ - opj_tcd_band_t *band; - - /* will keep the precinct */ - opj_tcd_precinct_t *prec; - - /* will keep the codeblock */ - opj_tcd_cblk_t *block; - - /* check all tile components */ - for (compno = 0; compno < tile->numcomps; compno++) { - comp = &(tile->comps[compno]); - - /* check all component resolutions */ - for (resno = 0; resno < comp->numresolutions; resno++) { - res = &(comp->resolutions[resno]); - numprecincts = res->pw * res->ph; - - /* check all the subbands */ - for (bandno = 0; bandno < res->numbands; bandno++) { - band = &(res->bands[bandno]); - - /* check all the precincts */ - for (precno = 0; precno < numprecincts; precno++) { - prec = &(band->precincts[precno]); - numblocks = prec->ch * prec->cw; - - /* check all the codeblocks */ - for (blockno = 0; blockno < numblocks; blockno++) { - block = &(prec->cblks[blockno]); - - /* x-origin is invalid */ - if ((block->x0 < prec->x0) || (block->x0 > prec->x1)) { - opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, - "JPWL: wrong x-cord of block origin %d => x-prec is (%d, %d)\n", - block->x0, prec->x0, prec->x1); - if (!JPWL_ASSUME || JPWL_ASSUME) - return OPJ_FALSE; - }; - } - } - } - } - } + */ + int compno, resno, precno, /*layno,*/ bandno, blockno; + int numprecincts, numblocks; + + /* this is the selected tile */ + opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[tileno]); + + /* will keep the component */ + opj_tcd_tilecomp_t *comp = NULL; + + /* will keep the resolution */ + opj_tcd_resolution_t *res; + + /* will keep the subband */ + opj_tcd_band_t *band; + + /* will keep the precinct */ + opj_tcd_precinct_t *prec; + + /* will keep the codeblock */ + opj_tcd_cblk_t *block; + + /* check all tile components */ + for (compno = 0; compno < tile->numcomps; compno++) { + comp = &(tile->comps[compno]); + + /* check all component resolutions */ + for (resno = 0; resno < comp->numresolutions; resno++) { + res = &(comp->resolutions[resno]); + numprecincts = res->pw * res->ph; + + /* check all the subbands */ + for (bandno = 0; bandno < res->numbands; bandno++) { + band = &(res->bands[bandno]); + + /* check all the precincts */ + for (precno = 0; precno < numprecincts; precno++) { + prec = &(band->precincts[precno]); + numblocks = prec->ch * prec->cw; + + /* check all the codeblocks */ + for (blockno = 0; blockno < numblocks; blockno++) { + block = &(prec->cblks[blockno]); + + /* x-origin is invalid */ + if ((block->x0 < prec->x0) || (block->x0 > prec->x1)) { + opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: wrong x-cord of block origin %d => x-prec is (%d, %d)\n", + block->x0, prec->x0, prec->x1); + if (!JPWL_ASSUME || JPWL_ASSUME) + return OPJ_FALSE; + }; + } + } + } + } + } #else (void)j2k; @@ -1292,7 +1305,7 @@ opj_bool jpwl_check_tile(opj_j2k_t *j2k, opj_tcd_t *tcd, int tileno) { (void)tileno; #endif - return OPJ_TRUE; + return OPJ_TRUE; } /*@}*/ @@ -1310,51 +1323,51 @@ opj_bool jpwl_check_tile(opj_j2k_t *j2k, opj_tcd_t *tcd, int tileno) { /*@{*/ void j2k_read_sec(opj_j2k_t *j2k) { - unsigned short int Lsec; - - opj_cio_t *cio = j2k->cio; + unsigned short int Lsec; + + opj_cio_t *cio = j2k->cio; - /* Simply read the SEC length */ - Lsec = cio_read(cio, 2); + /* Simply read the SEC length */ + Lsec = cio_read(cio, 2); - /* Now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "SEC(%d)\n", - cio_tell(cio) - 2 - ); + /* Now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "SEC(%d)\n", + cio_tell(cio) - 2 + ); - cio_skip(cio, Lsec - 2); + cio_skip(cio, Lsec - 2); } void j2k_write_sec(opj_j2k_t *j2k) { - unsigned short int Lsec = 24; - int i; + unsigned short int Lsec = 24; + int i; - opj_cio_t *cio = j2k->cio; + opj_cio_t *cio = j2k->cio; - cio_write(cio, J2K_MS_SEC, 2); /* SEC */ - cio_write(cio, Lsec, 2); + cio_write(cio, J2K_MS_SEC, 2); /* SEC */ + cio_write(cio, Lsec, 2); - /* write dummy data */ - for (i = 0; i < Lsec - 2; i++) - cio_write(cio, 0, 1); + /* write dummy data */ + for (i = 0; i < Lsec - 2; i++) + cio_write(cio, 0, 1); } void j2k_read_insec(opj_j2k_t *j2k) { - unsigned short int Linsec; - - opj_cio_t *cio = j2k->cio; + unsigned short int Linsec; + + opj_cio_t *cio = j2k->cio; - /* Simply read the INSEC length */ - Linsec = cio_read(cio, 2); + /* Simply read the INSEC length */ + Linsec = cio_read(cio, 2); - /* Now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "INSEC(%d)\n", - cio_tell(cio) - 2 - ); + /* Now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "INSEC(%d)\n", + cio_tell(cio) - 2 + ); - cio_skip(cio, Linsec - 2); + cio_skip(cio, Linsec - 2); } diff --git a/libopenjpeg/mqc.c b/libopenjpeg/mqc.c index 14129fbf..ef3cb7bb 100644 --- a/libopenjpeg/mqc.c +++ b/libopenjpeg/mqc.c @@ -363,7 +363,7 @@ opj_mqc_t* mqc_create(void) { void mqc_destroy(opj_mqc_t *mqc) { if(mqc) { #ifdef MQC_PERF_OPT - if (mqc->buffer) { + if (mqc->buffer) { // TODO: LH: this test is pointless as free() is a no-op on 0 opj_free(mqc->buffer); } #endif @@ -508,7 +508,7 @@ void mqc_segmark_enc(opj_mqc_t *mqc) { } } -void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) { +opj_bool mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) { mqc_setcurctx(mqc, 0); mqc->start = bp; mqc->end = bp + len; @@ -521,7 +521,13 @@ void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) { unsigned int c; unsigned int *ip; unsigned char *end = mqc->end - 1; - mqc->buffer = opj_realloc(mqc->buffer, (len + 1) * sizeof(unsigned int)); + void* new_buffer = opj_realloc(mqc->buffer, (len + 1) * sizeof(unsigned int)); + if (! new_buffer) { + opj_free(mqc->buffer); + mqc->buffer = NULL; + return OPJ_FALSE; + } + mqc->buffer = new_buffer; ip = (unsigned int *) mqc->buffer; while (bp < end) { @@ -557,6 +563,7 @@ void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) { mqc->c <<= 7; mqc->ct -= 7; mqc->a = 0x8000; + return OPJ_TRUE; } int mqc_decode(opj_mqc_t *const mqc) { diff --git a/libopenjpeg/mqc.h b/libopenjpeg/mqc.h index d00cd106..7984aaf8 100644 --- a/libopenjpeg/mqc.h +++ b/libopenjpeg/mqc.h @@ -185,7 +185,7 @@ Initialize the decoder @param bp Pointer to the start of the buffer from which the bytes will be read @param len Length of the input buffer */ -void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len); +opj_bool mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len); /** Decode a symbol @param mqc MQC handle diff --git a/libopenjpeg/t1.c b/libopenjpeg/t1.c index 8be947dc..becc41a2 100644 --- a/libopenjpeg/t1.c +++ b/libopenjpeg/t1.c @@ -330,8 +330,9 @@ Decode 1 code-block @param orient @param roishift Region of interest shifting value @param cblksty Code-block style +@deprecated ? */ -static void t1_decode_cblk( +static opj_bool t1_decode_cblk( opj_t1_t *t1, opj_tcd_cblk_dec_t* cblk, int orient, @@ -346,7 +347,7 @@ Decode 1 code-block @param roishift Region of interest shifting value @param cblksty Code-block style */ -static void t1_decode_cblk_v2( +static opj_bool t1_decode_cblk_v2( opj_t1_t *t1, opj_tcd_cblk_dec_v2_t* cblk, OPJ_UINT32 orient, @@ -1393,7 +1394,7 @@ static void t1_encode_cblk( } } -static void t1_decode_cblk( +static opj_bool t1_decode_cblk( opj_t1_t *t1, opj_tcd_cblk_dec_t* cblk, int orient, @@ -1435,7 +1436,9 @@ static void t1_decode_cblk( if (type == T1_TYPE_RAW) { raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len); } else { - mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len); + if (OPJ_FALSE == mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) { + return OPJ_FALSE; + } } for (passno = 0; passno < seg->numpasses; ++passno) { @@ -1479,6 +1482,7 @@ static void t1_decode_cblk( } } } + return OPJ_TRUE; } /* ----------------------------------------------------------------------- */ @@ -1644,7 +1648,7 @@ void opj_t1_destroy(opj_t1_t *p_t1) opj_free(p_t1); } -void opj_t1_decode_cblks( opj_t1_t* t1, +opj_bool opj_t1_decode_cblks( opj_t1_t* t1, opj_tcd_tilecomp_v2_t* tilec, opj_tccp_t* tccp ) @@ -1669,12 +1673,14 @@ void opj_t1_decode_cblks( opj_t1_t* t1, OPJ_INT32 x, y; OPJ_UINT32 i, j; - t1_decode_cblk_v2( - t1, - cblk, - band->bandno, - tccp->roishift, - tccp->cblksty); + if (OPJ_FALSE == t1_decode_cblk_v2( + t1, + cblk, + band->bandno, + tccp->roishift, + tccp->cblksty)) { + return OPJ_FALSE; + } x = cblk->x0 - band->x0; y = cblk->y0 - band->y0; @@ -1727,10 +1733,11 @@ void opj_t1_decode_cblks( opj_t1_t* t1, } /* precno */ } /* bandno */ } /* resno */ + return OPJ_TRUE; } -static void t1_decode_cblk_v2( +static opj_bool t1_decode_cblk_v2( opj_t1_t *t1, opj_tcd_cblk_dec_v2_t* cblk, OPJ_UINT32 orient, @@ -1773,7 +1780,9 @@ static void t1_decode_cblk_v2( if (type == T1_TYPE_RAW) { raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len); } else { - mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len); + if (OPJ_FALSE == mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) { + return OPJ_FALSE; + } } for (passno = 0; passno < seg->real_num_passes; ++passno) { @@ -1801,6 +1810,7 @@ static void t1_decode_cblk_v2( } } } + return OPJ_TRUE; } opj_bool opj_t1_encode_cblks( opj_t1_t *t1, diff --git a/libopenjpeg/t1.h b/libopenjpeg/t1.h index a2374aae..3963e738 100644 --- a/libopenjpeg/t1.h +++ b/libopenjpeg/t1.h @@ -131,7 +131,7 @@ Decode the code-blocks of a tile @param tilec The tile to decode @param tccp Tile coding parameters */ -void opj_t1_decode_cblks( opj_t1_t* t1, +opj_bool opj_t1_decode_cblks( opj_t1_t* t1, opj_tcd_tilecomp_v2_t* tilec, opj_tccp_t* tccp); diff --git a/libopenjpeg/t2.c b/libopenjpeg/t2.c index a091ef22..37d64b1a 100644 --- a/libopenjpeg/t2.c +++ b/libopenjpeg/t2.c @@ -71,14 +71,14 @@ Encode a packet of a tile to a destination buffer @return */ static opj_bool t2_encode_packet_v2( - OPJ_UINT32 tileno, - opj_tcd_tile_v2_t *tile, - opj_tcp_v2_t *tcp, - opj_pi_iterator_t *pi, - OPJ_BYTE *dest, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 len, - opj_codestream_info_t *cstr_info); + OPJ_UINT32 tileno, + opj_tcd_tile_v2_t *tile, + opj_tcp_v2_t *tcp, + opj_pi_iterator_t *pi, + OPJ_BYTE *dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 len, + opj_codestream_info_t *cstr_info); /** @param cblk @@ -86,7 +86,7 @@ static opj_bool t2_encode_packet_v2( @param cblksty @param first */ -static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first); +static opj_bool t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first); /** Decode a packet of a tile from a source buffer @param t2 T2 handle @@ -99,7 +99,7 @@ Decode a packet of a tile from a source buffer @return */ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, - opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info); + opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info); /** @@ -114,52 +114,52 @@ Decode a packet of a tile from a source buffer @return */ static opj_bool t2_decode_packet_v2( - opj_t2_v2_t* t2, - opj_tcd_tile_v2_t *tile, + opj_t2_v2_t* t2, + opj_tcd_tile_v2_t *tile, opj_tcp_v2_t *tcp, - opj_pi_iterator_t *pi, - OPJ_BYTE *src, - OPJ_UINT32 * data_read, - OPJ_UINT32 max_length, - opj_packet_info_t *pack_info); + opj_pi_iterator_t *pi, + OPJ_BYTE *src, + OPJ_UINT32 * data_read, + OPJ_UINT32 max_length, + opj_packet_info_t *pack_info); static opj_bool t2_skip_packet( - opj_t2_v2_t* p_t2, - opj_tcd_tile_v2_t *p_tile, + opj_t2_v2_t* p_t2, + opj_tcd_tile_v2_t *p_tile, opj_tcp_v2_t *p_tcp, - opj_pi_iterator_t *p_pi, - OPJ_BYTE *p_src, - OPJ_UINT32 * p_data_read, - OPJ_UINT32 p_max_length, - opj_packet_info_t *p_pack_info); + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info); static opj_bool t2_read_packet_header( - opj_t2_v2_t* p_t2, - opj_tcd_tile_v2_t *p_tile, + opj_t2_v2_t* p_t2, + opj_tcd_tile_v2_t *p_tile, opj_tcp_v2_t *p_tcp, - opj_pi_iterator_t *p_pi, - opj_bool * p_is_data_present, - OPJ_BYTE *p_src_data, - OPJ_UINT32 * p_data_read, - OPJ_UINT32 p_max_length, - opj_packet_info_t *p_pack_info); + opj_pi_iterator_t *p_pi, + opj_bool * p_is_data_present, + OPJ_BYTE *p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info); static opj_bool t2_read_packet_data( - opj_t2_v2_t* p_t2, - opj_tcd_tile_v2_t *p_tile, - opj_pi_iterator_t *p_pi, - OPJ_BYTE *p_src_data, - OPJ_UINT32 * p_data_read, - OPJ_UINT32 p_max_length, - opj_packet_info_t *pack_info); + opj_t2_v2_t* p_t2, + opj_tcd_tile_v2_t *p_tile, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *pack_info); static opj_bool t2_skip_packet_data( - opj_t2_v2_t* p_t2, - opj_tcd_tile_v2_t *p_tile, - opj_pi_iterator_t *p_pi, - OPJ_UINT32 * p_data_read, - OPJ_UINT32 p_max_length, - opj_packet_info_t *pack_info); + opj_t2_v2_t* p_t2, + opj_tcd_tile_v2_t *p_tile, + opj_pi_iterator_t *p_pi, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *pack_info); /** @param cblk @@ -167,10 +167,10 @@ static opj_bool t2_skip_packet_data( @param cblksty @param first */ -static opj_bool t2_init_seg_v2( opj_tcd_cblk_dec_v2_t* cblk, - OPJ_UINT32 index, - OPJ_UINT32 cblksty, - OPJ_UINT32 first); +static opj_bool t2_init_seg_v2( opj_tcd_cblk_dec_v2_t* cblk, + OPJ_UINT32 index, + OPJ_UINT32 cblksty, + OPJ_UINT32 first); /*@}*/ @@ -181,1776 +181,1810 @@ static opj_bool t2_init_seg_v2( opj_tcd_cblk_dec_v2_t* cblk, /* #define RESTART 0x04 */ static void t2_putcommacode(opj_bio_t *bio, int n) { - while (--n >= 0) { - bio_write(bio, 1, 1); - } - bio_write(bio, 0, 1); + while (--n >= 0) { + bio_write(bio, 1, 1); + } + bio_write(bio, 0, 1); } static int t2_getcommacode(opj_bio_t *bio) { - int n; - for (n = 0; bio_read(bio, 1); n++) { - ; - } - return n; + int n; + for (n = 0; bio_read(bio, 1); n++) { + ; + } + return n; } static void t2_putnumpasses(opj_bio_t *bio, int n) { - if (n == 1) { - bio_write(bio, 0, 1); - } else if (n == 2) { - bio_write(bio, 2, 2); - } else if (n <= 5) { - bio_write(bio, 0xc | (n - 3), 4); - } else if (n <= 36) { - bio_write(bio, 0x1e0 | (n - 6), 9); - } else if (n <= 164) { - bio_write(bio, 0xff80 | (n - 37), 16); - } + if (n == 1) { + bio_write(bio, 0, 1); + } else if (n == 2) { + bio_write(bio, 2, 2); + } else if (n <= 5) { + bio_write(bio, 0xc | (n - 3), 4); + } else if (n <= 36) { + bio_write(bio, 0x1e0 | (n - 6), 9); + } else if (n <= 164) { + bio_write(bio, 0xff80 | (n - 37), 16); + } } static int t2_getnumpasses(opj_bio_t *bio) { - int n; - if (!bio_read(bio, 1)) - return 1; - if (!bio_read(bio, 1)) - return 2; - if ((n = bio_read(bio, 2)) != 3) - return (3 + n); - if ((n = bio_read(bio, 5)) != 31) - return (6 + n); - return (37 + bio_read(bio, 7)); + int n; + if (!bio_read(bio, 1)) + return 1; + if (!bio_read(bio, 1)) + return 2; + if ((n = bio_read(bio, 2)) != 3) + return (3 + n); + if ((n = bio_read(bio, 5)) != 31) + return (6 + n); + return (37 + bio_read(bio, 7)); } static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int length, opj_codestream_info_t *cstr_info, int tileno) { - int bandno, cblkno; - unsigned char *c = dest; - - int compno = pi->compno; /* component value */ - int resno = pi->resno; /* resolution level value */ - int precno = pi->precno; /* precinct value */ - int layno = pi->layno; /* quality layer value */ - - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - opj_bio_t *bio = NULL; /* BIO component */ - - /* */ - if (tcp->csty & J2K_CP_CSTY_SOP) { - c[0] = 255; - c[1] = 145; - c[2] = 0; - c[3] = 4; - c[4] = (unsigned char)((tile->packno % 65536) / 256); - c[5] = (unsigned char)((tile->packno % 65536) % 256); - c += 6; - } - /* */ - - if (!layno) { - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - tgt_reset(prc->incltree); - tgt_reset(prc->imsbtree); - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; - cblk->numpasses = 0; - tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps); - } - } - } - - bio = bio_create(); - bio_init_enc(bio, c, length); - bio_write(bio, 1, 1); /* Empty header bit */ - - /* Writing Packet header */ - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; - opj_tcd_layer_t *layer = &cblk->layers[layno]; - if (!cblk->numpasses && layer->numpasses) { - tgt_setvalue(prc->incltree, cblkno, layno); - } - } - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; - opj_tcd_layer_t *layer = &cblk->layers[layno]; - int increment = 0; - int nump = 0; - int len = 0, passno; - /* cblk inclusion bits */ - if (!cblk->numpasses) { - tgt_encode(bio, prc->incltree, cblkno, layno + 1); - } else { - bio_write(bio, layer->numpasses != 0, 1); - } - /* if cblk not included, go to the next cblk */ - if (!layer->numpasses) { - continue; - } - /* if first instance of cblk --> zero bit-planes information */ - if (!cblk->numpasses) { - cblk->numlenbits = 3; - tgt_encode(bio, prc->imsbtree, cblkno, 999); - } - /* number of coding passes included */ - t2_putnumpasses(bio, layer->numpasses); - - /* computation of the increase of the length indicator and insertion in the header */ - for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) { - opj_tcd_pass_t *pass = &cblk->passes[passno]; - nump++; - len += pass->len; - if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { - increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump))); - len = 0; - nump = 0; - } - } - t2_putcommacode(bio, increment); - - /* computation of the new Length indicator */ - cblk->numlenbits += increment; - - /* insertion of the codeword segment length */ - for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) { - opj_tcd_pass_t *pass = &cblk->passes[passno]; - nump++; - len += pass->len; - if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { - bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump)); - len = 0; - nump = 0; - } - } - } - } - - if (bio_flush(bio)) { - bio_destroy(bio); - return -999; /* modified to eliminate longjmp !! */ - } - - c += bio_numbytes(bio); - bio_destroy(bio); - - /* */ - if (tcp->csty & J2K_CP_CSTY_EPH) { - c[0] = 255; - c[1] = 146; - c += 2; - } - /* */ - - /* << INDEX */ - /* End of packet header position. Currently only represents the distance to start of packet - // Will be updated later by incrementing with packet start value */ - if(cstr_info && cstr_info->index_write) { - opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; - info_PK->end_ph_pos = (int)(c - dest); - } - /* INDEX >> */ - - /* Writing the packet body */ - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; - opj_tcd_layer_t *layer = &cblk->layers[layno]; - if (!layer->numpasses) { - continue; - } - if (c + layer->len > dest + length) { - return -999; - } - - memcpy(c, layer->data, layer->len); - cblk->numpasses += layer->numpasses; - c += layer->len; - /* << INDEX */ - if(cstr_info && cstr_info->index_write) { - opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; - info_PK->disto += layer->disto; - if (cstr_info->D_max < info_PK->disto) { - cstr_info->D_max = info_PK->disto; - } - } - /* INDEX >> */ - } - } - - return (c - dest); + int bandno, cblkno; + unsigned char *c = dest; + + int compno = pi->compno; /* component value */ + int resno = pi->resno; /* resolution level value */ + int precno = pi->precno; /* precinct value */ + int layno = pi->layno; /* quality layer value */ + + opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; + opj_tcd_resolution_t *res = &tilec->resolutions[resno]; + + opj_bio_t *bio = NULL; /* BIO component */ + + /* */ + if (tcp->csty & J2K_CP_CSTY_SOP) { + c[0] = 255; + c[1] = 145; + c[2] = 0; + c[3] = 4; + c[4] = (unsigned char)((tile->packno % 65536) / 256); + c[5] = (unsigned char)((tile->packno % 65536) % 256); + c += 6; + } + /* */ + + if (!layno) { + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + opj_tcd_precinct_t *prc = &band->precincts[precno]; + tgt_reset(prc->incltree); + tgt_reset(prc->imsbtree); + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; + cblk->numpasses = 0; + tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps); + } + } + } + + bio = bio_create(); + bio_init_enc(bio, c, length); + bio_write(bio, 1, 1); /* Empty header bit */ + + /* Writing Packet header */ + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + opj_tcd_precinct_t *prc = &band->precincts[precno]; + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; + opj_tcd_layer_t *layer = &cblk->layers[layno]; + if (!cblk->numpasses && layer->numpasses) { + tgt_setvalue(prc->incltree, cblkno, layno); + } + } + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; + opj_tcd_layer_t *layer = &cblk->layers[layno]; + int increment = 0; + int nump = 0; + int len = 0, passno; + /* cblk inclusion bits */ + if (!cblk->numpasses) { + tgt_encode(bio, prc->incltree, cblkno, layno + 1); + } else { + bio_write(bio, layer->numpasses != 0, 1); + } + /* if cblk not included, go to the next cblk */ + if (!layer->numpasses) { + continue; + } + /* if first instance of cblk --> zero bit-planes information */ + if (!cblk->numpasses) { + cblk->numlenbits = 3; + tgt_encode(bio, prc->imsbtree, cblkno, 999); + } + /* number of coding passes included */ + t2_putnumpasses(bio, layer->numpasses); + + /* computation of the increase of the length indicator and insertion in the header */ + for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) { + opj_tcd_pass_t *pass = &cblk->passes[passno]; + nump++; + len += pass->len; + if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { + increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump))); + len = 0; + nump = 0; + } + } + t2_putcommacode(bio, increment); + + /* computation of the new Length indicator */ + cblk->numlenbits += increment; + + /* insertion of the codeword segment length */ + for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) { + opj_tcd_pass_t *pass = &cblk->passes[passno]; + nump++; + len += pass->len; + if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { + bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump)); + len = 0; + nump = 0; + } + } + } + } + + if (bio_flush(bio)) { + bio_destroy(bio); + return -999; /* modified to eliminate longjmp !! */ + } + + c += bio_numbytes(bio); + bio_destroy(bio); + + /* */ + if (tcp->csty & J2K_CP_CSTY_EPH) { + c[0] = 255; + c[1] = 146; + c += 2; + } + /* */ + + /* << INDEX */ + /* End of packet header position. Currently only represents the distance to start of packet + // Will be updated later by incrementing with packet start value */ + if(cstr_info && cstr_info->index_write) { + opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; + info_PK->end_ph_pos = (int)(c - dest); + } + /* INDEX >> */ + + /* Writing the packet body */ + + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + opj_tcd_precinct_t *prc = &band->precincts[precno]; + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; + opj_tcd_layer_t *layer = &cblk->layers[layno]; + if (!layer->numpasses) { + continue; + } + if (c + layer->len > dest + length) { + return -999; + } + + memcpy(c, layer->data, layer->len); + cblk->numpasses += layer->numpasses; + c += layer->len; + /* << INDEX */ + if(cstr_info && cstr_info->index_write) { + opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; + info_PK->disto += layer->disto; + if (cstr_info->D_max < info_PK->disto) { + cstr_info->D_max = info_PK->disto; + } + } + /* INDEX >> */ + } + } + + return (c - dest); } -static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) { - opj_tcd_seg_t* seg; - cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t)); - seg = &cblk->segs[index]; - seg->data = NULL; - seg->dataindex = 0; - seg->numpasses = 0; - seg->len = 0; - if (cblksty & J2K_CCP_CBLKSTY_TERMALL) { - seg->maxpasses = 1; - } - else if (cblksty & J2K_CCP_CBLKSTY_LAZY) { - if (first) { - seg->maxpasses = 10; - } else { - seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1; - } - } else { - seg->maxpasses = 109; - } +static opj_bool t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) { + opj_tcd_seg_t* seg; + opj_tcd_seg_t* new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t)); + if (!new_segs) { + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to init segment #%d\n", index); */ + // TODO: tell cblk has no segment (in order to update the range of valid indices) + cblk->segs = NULL; + return OPJ_FALSE; + } + cblk->segs = new_segs; + seg = &cblk->segs[index]; + seg->data = NULL; + seg->dataindex = 0; + seg->numpasses = 0; + seg->len = 0; + if (cblksty & J2K_CCP_CBLKSTY_TERMALL) { + seg->maxpasses = 1; + } + else if (cblksty & J2K_CCP_CBLKSTY_LAZY) { + if (first) { + seg->maxpasses = 10; + } else { + seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1; + } + } else { + seg->maxpasses = 109; + } + return OPJ_TRUE; } static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, - opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) { - int bandno, cblkno; - unsigned char *c = src; - - opj_cp_t *cp = t2->cp; - - int compno = pi->compno; /* component value */ - int resno = pi->resno; /* resolution level value */ - int precno = pi->precno; /* precinct value */ - int layno = pi->layno; /* quality layer value */ - - opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno]; - - unsigned char *hd = NULL; - int present; - - opj_bio_t *bio = NULL; /* BIO component */ - - if (layno == 0) { - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; - - tgt_reset(prc->incltree); - tgt_reset(prc->imsbtree); - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; - cblk->numsegs = 0; - } - } - } - - /* SOP markers */ - - if (tcp->csty & J2K_CP_CSTY_SOP) { - if ((*c) != 0xff || (*(c + 1) != 0x91)) { - opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n"); - } else { - c += 6; - } - - /** TODO : check the Nsop value */ - } - - /* - When the marker PPT/PPM is used the packet header are store in PPT/PPM marker - This part deal with this caracteristic - step 1: Read packet header in the saved structure - step 2: Return to codestream for decoding - */ - - bio = bio_create(); - - if (cp->ppm == 1) { /* PPM */ - hd = cp->ppm_data; - bio_init_dec(bio, hd, cp->ppm_len); - } else if (tcp->ppt == 1) { /* PPT */ - hd = tcp->ppt_data; - bio_init_dec(bio, hd, tcp->ppt_len); - } else { /* Normal Case */ - hd = c; - bio_init_dec(bio, hd, src+len-hd); - } - - present = bio_read(bio, 1); - - if (!present) { - bio_inalign(bio); - hd += bio_numbytes(bio); - bio_destroy(bio); - - /* EPH markers */ - - if (tcp->csty & J2K_CP_CSTY_EPH) { - if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { - printf("Error : expected EPH marker\n"); - } else { - hd += 2; - } - } - - /* << INDEX */ - /* End of packet header position. Currently only represents the distance to start of packet - // Will be updated later by incrementing with packet start value*/ - if(pack_info) { - pack_info->end_ph_pos = (int)(c - src); - } - /* INDEX >> */ - - if (cp->ppm == 1) { /* PPM case */ - cp->ppm_len += cp->ppm_data-hd; - cp->ppm_data = hd; - return (c - src); - } - if (tcp->ppt == 1) { /* PPT case */ - tcp->ppt_len+=tcp->ppt_data-hd; - tcp->ppt_data = hd; - return (c - src); - } - - return (hd - src); - } - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - int included, increment, n, segno; - opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; - /* if cblk not yet included before --> inclusion tagtree */ - if (!cblk->numsegs) { - included = tgt_decode(bio, prc->incltree, cblkno, layno + 1); - /* else one bit */ - } else { - included = bio_read(bio, 1); - } - /* if cblk not included */ - if (!included) { - cblk->numnewpasses = 0; - continue; - } - /* if cblk not yet included --> zero-bitplane tagtree */ - if (!cblk->numsegs) { - int i, numimsbs; - for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) { - ; - } - numimsbs = i - 1; - cblk->numbps = band->numbps - numimsbs; - cblk->numlenbits = 3; - } - /* number of coding passes */ - cblk->numnewpasses = t2_getnumpasses(bio); - increment = t2_getcommacode(bio); - /* length indicator increment */ - cblk->numlenbits += increment; - segno = 0; - if (!cblk->numsegs) { - t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1); - } else { - segno = cblk->numsegs - 1; - if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) { - ++segno; - t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0); - } - } - n = cblk->numnewpasses; - - do { - cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n); - cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses)); - n -= cblk->segs[segno].numnewpasses; - if (n > 0) { - ++segno; - t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0); - } - } while (n > 0); - } - } - - if (bio_inalign(bio)) { - bio_destroy(bio); - return -999; - } - - hd += bio_numbytes(bio); - bio_destroy(bio); - - /* EPH markers */ - if (tcp->csty & J2K_CP_CSTY_EPH) { - if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { - opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n"); - return -999; - } else { - hd += 2; - } - } - - /* << INDEX */ - /* End of packet header position. Currently only represents the distance to start of packet - // Will be updated later by incrementing with packet start value*/ - if(pack_info) { - pack_info->end_ph_pos = (int)(hd - src); - } - /* INDEX >> */ - - if (cp->ppm==1) { - cp->ppm_len+=cp->ppm_data-hd; - cp->ppm_data = hd; - } else if (tcp->ppt == 1) { - tcp->ppt_len+=tcp->ppt_data-hd; - tcp->ppt_data = hd; - } else { - c=hd; - } - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; - opj_tcd_seg_t *seg = NULL; - if (!cblk->numnewpasses) - continue; - if (!cblk->numsegs) { - seg = &cblk->segs[0]; - cblk->numsegs++; - cblk->len = 0; - } else { - seg = &cblk->segs[cblk->numsegs - 1]; - if (seg->numpasses == seg->maxpasses) { - seg++; - cblk->numsegs++; - } - } - - do { - if (c + seg->newlen > src + len) { - return -999; - } + opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) { + int bandno, cblkno; + unsigned char *c = src; + + opj_cp_t *cp = t2->cp; + + int compno = pi->compno; /* component value */ + int resno = pi->resno; /* resolution level value */ + int precno = pi->precno; /* precinct value */ + int layno = pi->layno; /* quality layer value */ + + opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno]; + + unsigned char *hd = NULL; + int present; + + opj_bio_t *bio = NULL; /* BIO component */ + + if (layno == 0) { + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + opj_tcd_precinct_t *prc = &band->precincts[precno]; + + if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; + + tgt_reset(prc->incltree); + tgt_reset(prc->imsbtree); + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; + cblk->numsegs = 0; + } + } + } + + /* SOP markers */ + + if (tcp->csty & J2K_CP_CSTY_SOP) { + if ((*c) != 0xff || (*(c + 1) != 0x91)) { + opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n"); + } else { + c += 6; + } + + /** TODO : check the Nsop value */ + } + + /* + When the marker PPT/PPM is used the packet header are store in PPT/PPM marker + This part deal with this caracteristic + step 1: Read packet header in the saved structure + step 2: Return to codestream for decoding + */ + + bio = bio_create(); + + if (cp->ppm == 1) { /* PPM */ + hd = cp->ppm_data; + bio_init_dec(bio, hd, cp->ppm_len); + } else if (tcp->ppt == 1) { /* PPT */ + hd = tcp->ppt_data; + bio_init_dec(bio, hd, tcp->ppt_len); + } else { /* Normal Case */ + hd = c; + bio_init_dec(bio, hd, src+len-hd); + } + + present = bio_read(bio, 1); + + if (!present) { + bio_inalign(bio); + hd += bio_numbytes(bio); + bio_destroy(bio); + + /* EPH markers */ + + if (tcp->csty & J2K_CP_CSTY_EPH) { + if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { + printf("Error : expected EPH marker\n"); + } else { + hd += 2; + } + } + + /* << INDEX */ + /* End of packet header position. Currently only represents the distance to start of packet + // Will be updated later by incrementing with packet start value*/ + if(pack_info) { + pack_info->end_ph_pos = (int)(c - src); + } + /* INDEX >> */ + + if (cp->ppm == 1) { /* PPM case */ + cp->ppm_len += cp->ppm_data-hd; + cp->ppm_data = hd; + return (c - src); + } + if (tcp->ppt == 1) { /* PPT case */ + tcp->ppt_len+=tcp->ppt_data-hd; + tcp->ppt_data = hd; + return (c - src); + } + + return (hd - src); + } + + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + opj_tcd_precinct_t *prc = &band->precincts[precno]; + + if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; + + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + int included, increment, n, segno; + opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; + /* if cblk not yet included before --> inclusion tagtree */ + if (!cblk->numsegs) { + included = tgt_decode(bio, prc->incltree, cblkno, layno + 1); + /* else one bit */ + } else { + included = bio_read(bio, 1); + } + /* if cblk not included */ + if (!included) { + cblk->numnewpasses = 0; + continue; + } + /* if cblk not yet included --> zero-bitplane tagtree */ + if (!cblk->numsegs) { + int i, numimsbs; + for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) { + ; + } + numimsbs = i - 1; + cblk->numbps = band->numbps - numimsbs; + cblk->numlenbits = 3; + } + /* number of coding passes */ + cblk->numnewpasses = t2_getnumpasses(bio); + increment = t2_getcommacode(bio); + /* length indicator increment */ + cblk->numlenbits += increment; + segno = 0; + if (!cblk->numsegs) { + if (OPJ_FALSE == t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1)) { + // TODO: LH: shall we destroy bio here ? + opj_event_msg(t2->cinfo, EVT_WARNING, "Not enough memory to init segment #%d\n", segno); + return -999; + } + } else { + segno = cblk->numsegs - 1; + if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) { + ++segno; + if (OPJ_FALSE == t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0)) { + // TODO: LH: shall we destroy bio here ? + opj_event_msg(t2->cinfo, EVT_WARNING, "Not enough memory to init segment #%d\n", segno); + return -999; + } + } + } + n = cblk->numnewpasses; + + do { + cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n); + cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses)); + n -= cblk->segs[segno].numnewpasses; + if (n > 0) { + ++segno; + if (OPJ_FALSE == t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0)) { + // TODO: LH: shall we destroy bio here ? + opj_event_msg(t2->cinfo, EVT_WARNING, "Not enough memory to init segment #%d\n", segno); + return -999; + } + } + } while (n > 0); + } + } + + if (bio_inalign(bio)) { + bio_destroy(bio); + return -999; + } + + hd += bio_numbytes(bio); + bio_destroy(bio); + + /* EPH markers */ + if (tcp->csty & J2K_CP_CSTY_EPH) { + if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { + opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n"); + return -999; + } else { + hd += 2; + } + } + + /* << INDEX */ + /* End of packet header position. Currently only represents the distance to start of packet + // Will be updated later by incrementing with packet start value*/ + if(pack_info) { + pack_info->end_ph_pos = (int)(hd - src); + } + /* INDEX >> */ + + if (cp->ppm==1) { + cp->ppm_len+=cp->ppm_data-hd; + cp->ppm_data = hd; + } else if (tcp->ppt == 1) { + tcp->ppt_len+=tcp->ppt_data-hd; + tcp->ppt_data = hd; + } else { + c=hd; + } + + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + opj_tcd_precinct_t *prc = &band->precincts[precno]; + + if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; + + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; + opj_tcd_seg_t *seg = NULL; + if (!cblk->numnewpasses) + continue; + if (!cblk->numsegs) { + seg = &cblk->segs[0]; + cblk->numsegs++; + cblk->len = 0; + } else { + seg = &cblk->segs[cblk->numsegs - 1]; + if (seg->numpasses == seg->maxpasses) { + seg++; + cblk->numsegs++; + } + } + + do { + if (c + seg->newlen > src + len) { + return -999; + } #ifdef USE_JPWL - /* we need here a j2k handle to verify if making a check to - the validity of cblocks parameters is selected from user (-W) */ - - /* let's check that we are not exceeding */ - if ((cblk->len + seg->newlen) > 8192) { - opj_event_msg(t2->cinfo, EVT_WARNING, - "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", - seg->newlen, cblkno, precno, bandno, resno, compno); - if (!JPWL_ASSUME) { - opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n"); - return -999; - } - seg->newlen = 8192 - cblk->len; - opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen); - break; - }; + /* we need here a j2k handle to verify if making a check to + the validity of cblocks parameters is selected from user (-W) */ + + /* let's check that we are not exceeding */ + if ((cblk->len + seg->newlen) > 8192) { + opj_event_msg(t2->cinfo, EVT_WARNING, + "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", + seg->newlen, cblkno, precno, bandno, resno, compno); + if (!JPWL_ASSUME) { + opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n"); + return -999; + } + seg->newlen = 8192 - cblk->len; + opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen); + break; + }; #endif /* USE_JPWL */ - - cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char)); - memcpy(cblk->data + cblk->len, c, seg->newlen); - if (seg->numpasses == 0) { - seg->data = &cblk->data; - seg->dataindex = cblk->len; - } - c += seg->newlen; - cblk->len += seg->newlen; - seg->len += seg->newlen; - seg->numpasses += seg->numnewpasses; - cblk->numnewpasses -= seg->numnewpasses; - if (cblk->numnewpasses > 0) { - seg++; - cblk->numsegs++; - } - } while (cblk->numnewpasses > 0); - } - } - - return (c - src); + + unsigned char * new_data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char)); + if (! new_data) { + opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: Not enough memory for codeblock data %d (p=%d, b=%d, r=%d, c=%d)\n", + seg->newlen, cblkno, precno, bandno, resno, compno); + cblk->data = 0; + cblk->len = 0; // TODO: LH: other things to reset ? + opj_free(cblk->data); + return -999; + } + cblk->data = new_data; + memcpy(cblk->data + cblk->len, c, seg->newlen); + if (seg->numpasses == 0) { + seg->data = &cblk->data; + seg->dataindex = cblk->len; + } + c += seg->newlen; + cblk->len += seg->newlen; + seg->len += seg->newlen; + seg->numpasses += seg->numnewpasses; + cblk->numnewpasses -= seg->numnewpasses; + if (cblk->numnewpasses > 0) { + seg++; + cblk->numsegs++; + } + } while (cblk->numnewpasses > 0); + } + } + + return (c - src); } /* ----------------------------------------------------------------------- */ int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_codestream_info_t *cstr_info,int tpnum, int tppos,int pino, J2K_T2_MODE t2_mode, int cur_totnum_tp){ - unsigned char *c = dest; - int e = 0; - int compno; - opj_pi_iterator_t *pi = NULL; - int poc; - opj_image_t *image = t2->image; - opj_cp_t *cp = t2->cp; - opj_tcp_t *tcp = &cp->tcps[tileno]; - int pocno = cp->cinema == CINEMA4K_24? 2: 1; - int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1; - - pi = pi_initialise_encode(image, cp, tileno, t2_mode); - if(!pi) { - /* TODO: throw an error */ - return -999; - } - - if(t2_mode == THRESH_CALC ){ /* Calculating threshold */ - for(compno = 0; compno < maxcomp; compno++ ){ - for(poc = 0; poc < pocno ; poc++){ - int comp_len = 0; - int tpnum = compno; - if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) { - opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n"); - pi_destroy(pi, cp, tileno); - return -999; - } - while (pi_next(&pi[poc])) { - if (pi[poc].layno < maxlayers) { - e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno); - comp_len = comp_len + e; - if (e == -999) { - break; - } else { - c += e; - } - } - } - if (e == -999) break; - if (cp->max_comp_size){ - if (comp_len > cp->max_comp_size){ - e = -999; - break; - } - } - } - if (e == -999) break; - } - }else{ /* t2_mode == FINAL_PASS */ - pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp); - while (pi_next(&pi[pino])) { - if (pi[pino].layno < maxlayers) { - e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno); - if (e == -999) { - break; - } else { - c += e; - } - /* INDEX >> */ - if(cstr_info) { - if(cstr_info->index_write) { - opj_tile_info_t *info_TL = &cstr_info->tile[tileno]; - opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno]; - if (!cstr_info->packno) { - info_PK->start_pos = info_TL->end_header + 1; - } else { - info_PK->start_pos = ((cp->tp_on | tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1; - } - info_PK->end_pos = info_PK->start_pos + e - 1; - info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance - // to start of packet is incremented by value of start of packet*/ - } - - cstr_info->packno++; - } - /* << INDEX */ - tile->packno++; - } - } - } - - pi_destroy(pi, cp, tileno); - - if (e == -999) { - return e; - } - + unsigned char *c = dest; + int e = 0; + int compno; + opj_pi_iterator_t *pi = NULL; + int poc; + opj_image_t *image = t2->image; + opj_cp_t *cp = t2->cp; + opj_tcp_t *tcp = &cp->tcps[tileno]; + int pocno = cp->cinema == CINEMA4K_24? 2: 1; + int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1; + + pi = pi_initialise_encode(image, cp, tileno, t2_mode); + if(!pi) { + /* TODO: throw an error */ + return -999; + } + + if(t2_mode == THRESH_CALC ){ /* Calculating threshold */ + for(compno = 0; compno < maxcomp; compno++ ){ + for(poc = 0; poc < pocno ; poc++){ + int comp_len = 0; + int tpnum = compno; + if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) { + opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n"); + pi_destroy(pi, cp, tileno); + return -999; + } + while (pi_next(&pi[poc])) { + if (pi[poc].layno < maxlayers) { + e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno); + comp_len = comp_len + e; + if (e == -999) { + break; + } else { + c += e; + } + } + } + if (e == -999) break; + if (cp->max_comp_size){ + if (comp_len > cp->max_comp_size){ + e = -999; + break; + } + } + } + if (e == -999) break; + } + }else{ /* t2_mode == FINAL_PASS */ + pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp); + while (pi_next(&pi[pino])) { + if (pi[pino].layno < maxlayers) { + e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno); + if (e == -999) { + break; + } else { + c += e; + } + /* INDEX >> */ + if(cstr_info) { + if(cstr_info->index_write) { + opj_tile_info_t *info_TL = &cstr_info->tile[tileno]; + opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno]; + if (!cstr_info->packno) { + info_PK->start_pos = info_TL->end_header + 1; + } else { + info_PK->start_pos = ((cp->tp_on | tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1; + } + info_PK->end_pos = info_PK->start_pos + e - 1; + info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance + // to start of packet is incremented by value of start of packet*/ + } + + cstr_info->packno++; + } + /* << INDEX */ + tile->packno++; + } + } + } + + pi_destroy(pi, cp, tileno); + + if (e == -999) { + return e; + } + return (c - dest); } opj_bool t2_encode_packets_v2( - opj_t2_v2_t* p_t2, - OPJ_UINT32 p_tile_no, - opj_tcd_tile_v2_t *p_tile, - OPJ_UINT32 p_maxlayers, - OPJ_BYTE *p_dest, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 p_max_len, - opj_codestream_info_t *cstr_info, - OPJ_UINT32 p_tp_num, - OPJ_INT32 p_tp_pos, - OPJ_UINT32 p_pino, - J2K_T2_MODE p_t2_mode) + opj_t2_v2_t* p_t2, + OPJ_UINT32 p_tile_no, + opj_tcd_tile_v2_t *p_tile, + OPJ_UINT32 p_maxlayers, + OPJ_BYTE *p_dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_len, + opj_codestream_info_t *cstr_info, + OPJ_UINT32 p_tp_num, + OPJ_INT32 p_tp_pos, + OPJ_UINT32 p_pino, + J2K_T2_MODE p_t2_mode) { - OPJ_BYTE *l_current_data = p_dest; - OPJ_UINT32 l_nb_bytes = 0; - OPJ_UINT32 compno; - OPJ_UINT32 poc; - opj_pi_iterator_t *l_pi = 00; - opj_pi_iterator_t *l_current_pi = 00; - opj_image_t *l_image = p_t2->image; - opj_cp_v2_t *l_cp = p_t2->cp; - opj_tcp_v2_t *l_tcp = &l_cp->tcps[p_tile_no]; - OPJ_UINT32 pocno = l_cp->m_specific_param.m_enc.m_cinema == CINEMA4K_24? 2: 1; - OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1; - OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1; - - l_pi = pi_initialise_encode_v2(l_image, l_cp, p_tile_no, p_t2_mode); - if (!l_pi) { - return OPJ_FALSE; - } - - * p_data_written = 0; - - if (p_t2_mode == THRESH_CALC ){ /* Calculating threshold */ - l_current_pi = l_pi; - - for (compno = 0; compno < l_max_comp; ++compno) { - OPJ_UINT32 l_comp_len = 0; - l_current_pi = l_pi; - - for (poc = 0; poc < pocno ; ++poc) { - OPJ_UINT32 l_tp_num = compno; - - pi_create_encode_v2(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode); - - while (pi_next(l_current_pi)) { - if (l_current_pi->layno < p_maxlayers) { - l_nb_bytes = 0; - - if (! t2_encode_packet_v2(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) { - pi_destroy_v2(l_pi, l_nb_pocs); - return OPJ_FALSE; - } - - l_comp_len += l_nb_bytes; - l_current_data += l_nb_bytes; - p_max_len -= l_nb_bytes; - - * p_data_written += l_nb_bytes; - } - } - - if (l_cp->m_specific_param.m_enc.m_max_comp_size) { - if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) { - pi_destroy_v2(l_pi, l_nb_pocs); - return OPJ_FALSE; - } - } - - ++l_current_pi; - } - } - } - else { /* t2_mode == FINAL_PASS */ - pi_create_encode_v2(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode); - - l_current_pi = &l_pi[p_pino]; - - while (pi_next(l_current_pi)) { - if (l_current_pi->layno < p_maxlayers) { - l_nb_bytes=0; - - if (! t2_encode_packet_v2(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) { - pi_destroy_v2(l_pi, l_nb_pocs); - return OPJ_FALSE; - } - - l_current_data += l_nb_bytes; - p_max_len -= l_nb_bytes; - - * p_data_written += l_nb_bytes; - - /* INDEX >> */ - if(cstr_info) { - if(cstr_info->index_write) { - opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no]; - opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno]; - if (!cstr_info->packno) { - info_PK->start_pos = info_TL->end_header + 1; - } else { - info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1; - } - info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1; - info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance - // to start of packet is incremented by value of start of packet - } - - cstr_info->packno++; - } - /* << INDEX */ - ++p_tile->packno; - } - } - } - - pi_destroy_v2(l_pi, l_nb_pocs); - - return OPJ_TRUE; + OPJ_BYTE *l_current_data = p_dest; + OPJ_UINT32 l_nb_bytes = 0; + OPJ_UINT32 compno; + OPJ_UINT32 poc; + opj_pi_iterator_t *l_pi = 00; + opj_pi_iterator_t *l_current_pi = 00; + opj_image_t *l_image = p_t2->image; + opj_cp_v2_t *l_cp = p_t2->cp; + opj_tcp_v2_t *l_tcp = &l_cp->tcps[p_tile_no]; + OPJ_UINT32 pocno = l_cp->m_specific_param.m_enc.m_cinema == CINEMA4K_24? 2: 1; + OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1; + OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1; + + l_pi = pi_initialise_encode_v2(l_image, l_cp, p_tile_no, p_t2_mode); + if (!l_pi) { + return OPJ_FALSE; + } + + * p_data_written = 0; + + if (p_t2_mode == THRESH_CALC ){ /* Calculating threshold */ + l_current_pi = l_pi; + + for (compno = 0; compno < l_max_comp; ++compno) { + OPJ_UINT32 l_comp_len = 0; + l_current_pi = l_pi; + + for (poc = 0; poc < pocno ; ++poc) { + OPJ_UINT32 l_tp_num = compno; + + pi_create_encode_v2(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode); + + while (pi_next(l_current_pi)) { + if (l_current_pi->layno < p_maxlayers) { + l_nb_bytes = 0; + + if (! t2_encode_packet_v2(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) { + pi_destroy_v2(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + + l_comp_len += l_nb_bytes; + l_current_data += l_nb_bytes; + p_max_len -= l_nb_bytes; + + * p_data_written += l_nb_bytes; + } + } + + if (l_cp->m_specific_param.m_enc.m_max_comp_size) { + if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) { + pi_destroy_v2(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + } + + ++l_current_pi; + } + } + } + else { /* t2_mode == FINAL_PASS */ + pi_create_encode_v2(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode); + + l_current_pi = &l_pi[p_pino]; + + while (pi_next(l_current_pi)) { + if (l_current_pi->layno < p_maxlayers) { + l_nb_bytes=0; + + if (! t2_encode_packet_v2(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) { + pi_destroy_v2(l_pi, l_nb_pocs); + return OPJ_FALSE; + } + + l_current_data += l_nb_bytes; + p_max_len -= l_nb_bytes; + + * p_data_written += l_nb_bytes; + + /* INDEX >> */ + if(cstr_info) { + if(cstr_info->index_write) { + opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no]; + opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno]; + if (!cstr_info->packno) { + info_PK->start_pos = info_TL->end_header + 1; + } else { + info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1; + } + info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1; + info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance + // to start of packet is incremented by value of start of packet + } + + cstr_info->packno++; + } + /* << INDEX */ + ++p_tile->packno; + } + } + } + + pi_destroy_v2(l_pi, l_nb_pocs); + + return OPJ_TRUE; } int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile, opj_codestream_info_t *cstr_info) { - unsigned char *c = src; - opj_pi_iterator_t *pi; - int pino, e = 0; - int n = 0, curtp = 0; - int tp_start_packno; - - opj_image_t *image = t2->image; - opj_cp_t *cp = t2->cp; - - /* create a packet iterator */ - pi = pi_create_decode(image, cp, tileno); - if(!pi) { - /* TODO: throw an error */ - return -999; - } - - tp_start_packno = 0; - - for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) { - while (pi_next(&pi[pino])) { - if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) { - opj_packet_info_t *pack_info; - if (cstr_info) - pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno]; - else - pack_info = NULL; - e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info); - } else { - e = 0; - } - if(e == -999) return -999; - /* progression in resolution */ - image->comps[pi[pino].compno].resno_decoded = - (e > 0) ? - int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) - : image->comps[pi[pino].compno].resno_decoded; - n++; - - /* INDEX >> */ - if(cstr_info) { - opj_tile_info_t *info_TL = &cstr_info->tile[tileno]; - opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno]; - if (!cstr_info->packno) { - info_PK->start_pos = info_TL->end_header + 1; - } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ /* New tile part*/ - info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part*/ + unsigned char *c = src; + opj_pi_iterator_t *pi; + int pino, e = 0; + int n = 0, curtp = 0; + int tp_start_packno; + + opj_image_t *image = t2->image; + opj_cp_t *cp = t2->cp; + + /* create a packet iterator */ + pi = pi_create_decode(image, cp, tileno); + if(!pi) { + /* TODO: throw an error */ + return -999; + } + + tp_start_packno = 0; + + for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) { + while (pi_next(&pi[pino])) { + if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) { + opj_packet_info_t *pack_info; + if (cstr_info) + pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno]; + else + pack_info = NULL; + e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info); + } else { + e = 0; + } + if(e == -999) return -999; + /* progression in resolution */ + image->comps[pi[pino].compno].resno_decoded = + (e > 0) ? + int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) + : image->comps[pi[pino].compno].resno_decoded; + n++; + + /* INDEX >> */ + if(cstr_info) { + opj_tile_info_t *info_TL = &cstr_info->tile[tileno]; + opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno]; + if (!cstr_info->packno) { + info_PK->start_pos = info_TL->end_header + 1; + } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ /* New tile part*/ + info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part*/ info_TL->tp[curtp].tp_start_pack = tp_start_packno; - tp_start_packno = cstr_info->packno; - curtp++; - info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1; - } else { - info_PK->start_pos = (cp->tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1; - } - info_PK->end_pos = info_PK->start_pos + e - 1; - info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance - // to start of packet is incremented by value of start of packet*/ - cstr_info->packno++; - } - /* << INDEX */ - - if (e == -999) { /* ADD */ - break; - } else { - c += e; - } - } - } - /* INDEX >> */ - if(cstr_info) { - cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part*/ + tp_start_packno = cstr_info->packno; + curtp++; + info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1; + } else { + info_PK->start_pos = (cp->tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1; + } + info_PK->end_pos = info_PK->start_pos + e - 1; + info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance + // to start of packet is incremented by value of start of packet*/ + cstr_info->packno++; + } + /* << INDEX */ + + if (e == -999) { /* ADD */ + break; + } else { + c += e; + } + } + } + /* INDEX >> */ + if(cstr_info) { + cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part*/ cstr_info->tile[tileno].tp[curtp].tp_start_pack = tp_start_packno; - } - /* << INDEX */ - - /* don't forget to release pi */ - pi_destroy(pi, cp, tileno); - - if (e == -999) { - return e; - } - - return (c - src); + } + /* << INDEX */ + + /* don't forget to release pi */ + pi_destroy(pi, cp, tileno); + + if (e == -999) { + return e; + } + + return (c - src); } opj_bool t2_decode_packets_v2( - opj_t2_v2_t *p_t2, - OPJ_UINT32 p_tile_no, - struct opj_tcd_tile_v2 *p_tile, - OPJ_BYTE *p_src, - OPJ_UINT32 * p_data_read, - OPJ_UINT32 p_max_len, - opj_codestream_index_t *p_cstr_index) + opj_t2_v2_t *p_t2, + OPJ_UINT32 p_tile_no, + struct opj_tcd_tile_v2 *p_tile, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_len, + opj_codestream_index_t *p_cstr_index) { - OPJ_BYTE *l_current_data = p_src; - opj_pi_iterator_t *l_pi = 00; - OPJ_UINT32 pino; - opj_image_t *l_image = p_t2->image; - opj_cp_v2_t *l_cp = p_t2->cp; - opj_cp_v2_t *cp = p_t2->cp; - opj_tcp_v2_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]); - OPJ_UINT32 l_nb_bytes_read; - OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1; - opj_pi_iterator_t *l_current_pi = 00; - OPJ_UINT32 curtp = 0; - OPJ_UINT32 tp_start_packno; - opj_packet_info_t *l_pack_info = 00; - opj_image_comp_t* l_img_comp = 00; + OPJ_BYTE *l_current_data = p_src; + opj_pi_iterator_t *l_pi = 00; + OPJ_UINT32 pino; + opj_image_t *l_image = p_t2->image; + opj_cp_v2_t *l_cp = p_t2->cp; + opj_cp_v2_t *cp = p_t2->cp; + opj_tcp_v2_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]); + OPJ_UINT32 l_nb_bytes_read; + OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1; + opj_pi_iterator_t *l_current_pi = 00; + OPJ_UINT32 curtp = 0; + OPJ_UINT32 tp_start_packno; + opj_packet_info_t *l_pack_info = 00; + opj_image_comp_t* l_img_comp = 00; #ifdef TODO_MSD - if (p_cstr_index) { - l_pack_info = p_cstr_index->tile_index[p_tile_no].packet; - } + if (p_cstr_index) { + l_pack_info = p_cstr_index->tile_index[p_tile_no].packet; + } #endif - /* create a packet iterator */ - l_pi = pi_create_decode_v2(l_image, l_cp, p_tile_no); - if (!l_pi) { - return OPJ_FALSE; - } + /* create a packet iterator */ + l_pi = pi_create_decode_v2(l_image, l_cp, p_tile_no); + if (!l_pi) { + return OPJ_FALSE; + } - tp_start_packno = 0; - l_current_pi = l_pi; + tp_start_packno = 0; + l_current_pi = l_pi; - for (pino = 0; pino <= l_tcp->numpocs; ++pino) { + for (pino = 0; pino <= l_tcp->numpocs; ++pino) { - /* if the resolution needed is to low, one dim of the tilec could be equal to zero - * and no packets are used to encode this resolution and - * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions - * and no l_img_comp->resno_decoded are computed - */ - opj_bool* first_pass_failed = (opj_bool*)opj_malloc(l_image->numcomps * sizeof(opj_bool)); - memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(opj_bool)); + /* if the resolution needed is to low, one dim of the tilec could be equal to zero + * and no packets are used to encode this resolution and + * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions + * and no l_img_comp->resno_decoded are computed + */ + opj_bool* first_pass_failed = (opj_bool*)opj_malloc(l_image->numcomps * sizeof(opj_bool)); + memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(opj_bool)); - while (pi_next(l_current_pi)) { + while (pi_next(l_current_pi)) { - if (l_tcp->num_layers_to_decode > l_current_pi->layno - && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) { - l_nb_bytes_read = 0; + if (l_tcp->num_layers_to_decode > l_current_pi->layno + && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) { + l_nb_bytes_read = 0; - first_pass_failed[l_current_pi->compno] = OPJ_FALSE; + first_pass_failed[l_current_pi->compno] = OPJ_FALSE; - if (! t2_decode_packet_v2(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) { - pi_destroy_v2(l_pi,l_nb_pocs); - return OPJ_FALSE; - } + if (! t2_decode_packet_v2(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) { + pi_destroy_v2(l_pi,l_nb_pocs); + return OPJ_FALSE; + } - l_img_comp = &(l_image->comps[l_current_pi->compno]); - l_img_comp->resno_decoded = uint_max(l_current_pi->resno, l_img_comp->resno_decoded); - } - else { - l_nb_bytes_read = 0; - if (! t2_skip_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) { - pi_destroy_v2(l_pi,l_nb_pocs); - return OPJ_FALSE; - } - } + l_img_comp = &(l_image->comps[l_current_pi->compno]); + l_img_comp->resno_decoded = uint_max(l_current_pi->resno, l_img_comp->resno_decoded); + } + else { + l_nb_bytes_read = 0; + if (! t2_skip_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) { + pi_destroy_v2(l_pi,l_nb_pocs); + return OPJ_FALSE; + } + } - if (first_pass_failed[l_current_pi->compno]) { - l_img_comp = &(l_image->comps[l_current_pi->compno]); - if (l_img_comp->resno_decoded == 0) - l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1; - } + if (first_pass_failed[l_current_pi->compno]) { + l_img_comp = &(l_image->comps[l_current_pi->compno]); + if (l_img_comp->resno_decoded == 0) + l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1; + } - l_current_data += l_nb_bytes_read; - p_max_len -= l_nb_bytes_read; + l_current_data += l_nb_bytes_read; + p_max_len -= l_nb_bytes_read; - /* INDEX >> */ + /* INDEX >> */ #ifdef TODO_MSD - if(p_cstr_info) { - opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no]; - opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno]; - if (!p_cstr_info->packno) { - info_PK->start_pos = info_TL->end_header + 1; - } else if (info_TL->packet[p_cstr_info->packno-1].end_pos >= (OPJ_INT32)p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos){ /* New tile part */ - info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */ - tp_start_packno = p_cstr_info->packno; - curtp++; - info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1; - } else { - info_PK->start_pos = (cp->m_specific_param.m_enc.m_tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno - 1].end_pos + 1; - } - info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1; - info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance */ - ++p_cstr_info->packno; - } + if(p_cstr_info) { + opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no]; + opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno]; + if (!p_cstr_info->packno) { + info_PK->start_pos = info_TL->end_header + 1; + } else if (info_TL->packet[p_cstr_info->packno-1].end_pos >= (OPJ_INT32)p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos){ /* New tile part */ + info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */ + tp_start_packno = p_cstr_info->packno; + curtp++; + info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1; + } else { + info_PK->start_pos = (cp->m_specific_param.m_enc.m_tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno - 1].end_pos + 1; + } + info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1; + info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance */ + ++p_cstr_info->packno; + } #endif - /* << INDEX */ - } - ++l_current_pi; + /* << INDEX */ + } + ++l_current_pi; - opj_free(first_pass_failed); - } - /* INDEX >> */ + opj_free(first_pass_failed); + } + /* INDEX >> */ #ifdef TODO_MSD - if - (p_cstr_info) { - p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part */ - } + if + (p_cstr_info) { + p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part */ + } #endif - /* << INDEX */ + /* << INDEX */ - /* don't forget to release pi */ - pi_destroy_v2(l_pi,l_nb_pocs); - *p_data_read = l_current_data - p_src; - return OPJ_TRUE; + /* don't forget to release pi */ + pi_destroy_v2(l_pi,l_nb_pocs); + *p_data_read = l_current_data - p_src; + return OPJ_TRUE; } /* ----------------------------------------------------------------------- */ opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) { - /* create the tcd structure */ - opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t)); - if(!t2) return NULL; - t2->cinfo = cinfo; - t2->image = image; - t2->cp = cp; - - return t2; + /* create the tcd structure */ + opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t)); + if(!t2) return NULL; + t2->cinfo = cinfo; + t2->image = image; + t2->cp = cp; + + return t2; } /** * Creates a Tier 2 handle * - * @param p_image Source or destination image - * @param p_cp Image coding parameters. - * @return a new T2 handle if successful, NULL otherwise. + * @param p_image Source or destination image + * @param p_cp Image coding parameters. + * @return a new T2 handle if successful, NULL otherwise. */ -opj_t2_v2_t* t2_create_v2( opj_image_t *p_image, - opj_cp_v2_t *p_cp) +opj_t2_v2_t* t2_create_v2( opj_image_t *p_image, + opj_cp_v2_t *p_cp) { - /* create the tcd structure */ - opj_t2_v2_t *l_t2 = (opj_t2_v2_t*)opj_malloc(sizeof(opj_t2_v2_t)); - if (!l_t2) { - return NULL; - } - memset(l_t2,0,sizeof(opj_t2_v2_t)); + /* create the tcd structure */ + opj_t2_v2_t *l_t2 = (opj_t2_v2_t*)opj_malloc(sizeof(opj_t2_v2_t)); + if (!l_t2) { + return NULL; + } + memset(l_t2,0,sizeof(opj_t2_v2_t)); - l_t2->image = p_image; - l_t2->cp = p_cp; + l_t2->image = p_image; + l_t2->cp = p_cp; - return l_t2; + return l_t2; } void t2_destroy(opj_t2_t *t2) { - if(t2) { - opj_free(t2); - } + if(t2) { + opj_free(t2); + } } void t2_destroy_v2(opj_t2_v2_t *t2) { - if(t2) { - opj_free(t2); - } + if(t2) { + opj_free(t2); + } } static opj_bool t2_decode_packet_v2( - opj_t2_v2_t* p_t2, - opj_tcd_tile_v2_t *p_tile, + opj_t2_v2_t* p_t2, + opj_tcd_tile_v2_t *p_tile, opj_tcp_v2_t *p_tcp, - opj_pi_iterator_t *p_pi, - OPJ_BYTE *p_src, - OPJ_UINT32 * p_data_read, - OPJ_UINT32 p_max_length, - opj_packet_info_t *p_pack_info) + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info) { - opj_bool l_read_data; - OPJ_UINT32 l_nb_bytes_read = 0; - OPJ_UINT32 l_nb_total_bytes_read = 0; + opj_bool l_read_data; + OPJ_UINT32 l_nb_bytes_read = 0; + OPJ_UINT32 l_nb_total_bytes_read = 0; - *p_data_read = 0; + *p_data_read = 0; - if (! t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) { - return OPJ_FALSE; - } + if (! t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) { + return OPJ_FALSE; + } - p_src += l_nb_bytes_read; - l_nb_total_bytes_read += l_nb_bytes_read; - p_max_length -= l_nb_bytes_read; + p_src += l_nb_bytes_read; + l_nb_total_bytes_read += l_nb_bytes_read; + p_max_length -= l_nb_bytes_read; - /* we should read data for the packet */ - if (l_read_data) { - l_nb_bytes_read = 0; + /* we should read data for the packet */ + if (l_read_data) { + l_nb_bytes_read = 0; - if (! t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) { - return OPJ_FALSE; - } + if (! t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) { + return OPJ_FALSE; + } - l_nb_total_bytes_read += l_nb_bytes_read; - } + l_nb_total_bytes_read += l_nb_bytes_read; + } - *p_data_read = l_nb_total_bytes_read; + *p_data_read = l_nb_total_bytes_read; - return OPJ_TRUE; + return OPJ_TRUE; } static opj_bool t2_encode_packet_v2( - OPJ_UINT32 tileno, - opj_tcd_tile_v2_t * tile, - opj_tcp_v2_t * tcp, - opj_pi_iterator_t *pi, - OPJ_BYTE *dest, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 length, - opj_codestream_info_t *cstr_info) + OPJ_UINT32 tileno, + opj_tcd_tile_v2_t * tile, + opj_tcp_v2_t * tcp, + opj_pi_iterator_t *pi, + OPJ_BYTE *dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 length, + opj_codestream_info_t *cstr_info) { - OPJ_UINT32 bandno, cblkno; - OPJ_BYTE *c = dest; - OPJ_UINT32 l_nb_bytes; - OPJ_UINT32 compno = pi->compno; /* component value */ - OPJ_UINT32 resno = pi->resno; /* resolution level value */ - OPJ_UINT32 precno = pi->precno; /* precinct value */ - OPJ_UINT32 layno = pi->layno; /* quality layer value */ - OPJ_UINT32 l_nb_blocks; - opj_tcd_band_v2_t *band = 00; - opj_tcd_cblk_enc_v2_t* cblk = 00; - opj_tcd_pass_v2_t *pass = 00; - - opj_tcd_tilecomp_v2_t *tilec = &tile->comps[compno]; - opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno]; - - opj_bio_t *bio = 00; /* BIO component */ - - /* */ - if (tcp->csty & J2K_CP_CSTY_SOP) { - c[0] = 255; - c[1] = 145; - c[2] = 0; - c[3] = 4; - c[4] = (tile->packno % 65536) / 256; - c[5] = (tile->packno % 65536) % 256; - c += 6; - length -= 6; - } - /* */ - - if (!layno) { - band = res->bands; - - for(bandno = 0; bandno < res->numbands; ++bandno) { - opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; - - tgt_reset(prc->incltree); - tgt_reset(prc->imsbtree); - - l_nb_blocks = prc->cw * prc->ch; - for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { - opj_tcd_cblk_enc_v2_t* cblk = &prc->cblks.enc[cblkno]; - - cblk->numpasses = 0; - tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps); - } - ++band; - } - } - - bio = bio_create(); - bio_init_enc(bio, c, length); - bio_write(bio, 1, 1); /* Empty header bit */ - - /* Writing Packet header */ - band = res->bands; - for (bandno = 0; bandno < res->numbands; ++bandno) { - opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; - - l_nb_blocks = prc->cw * prc->ch; - cblk = prc->cblks.enc; - - for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { - opj_tcd_layer_t *layer = &cblk->layers[layno]; - - if (!cblk->numpasses && layer->numpasses) { - tgt_setvalue(prc->incltree, cblkno, layno); - } - - ++cblk; - } - - cblk = prc->cblks.enc; - for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) { - opj_tcd_layer_t *layer = &cblk->layers[layno]; - OPJ_UINT32 increment = 0; - OPJ_UINT32 nump = 0; - OPJ_UINT32 len = 0, passno; - OPJ_UINT32 l_nb_passes; - - /* cblk inclusion bits */ - if (!cblk->numpasses) { - tgt_encode(bio, prc->incltree, cblkno, layno + 1); - } else { - bio_write(bio, layer->numpasses != 0, 1); - } - - /* if cblk not included, go to the next cblk */ - if (!layer->numpasses) { - ++cblk; - continue; - } - - /* if first instance of cblk --> zero bit-planes information */ - if (!cblk->numpasses) { - cblk->numlenbits = 3; - tgt_encode(bio, prc->imsbtree, cblkno, 999); - } - - /* number of coding passes included */ - t2_putnumpasses(bio, layer->numpasses); - l_nb_passes = cblk->numpasses + layer->numpasses; - pass = cblk->passes + cblk->numpasses; - - /* computation of the increase of the length indicator and insertion in the header */ - for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) { - ++nump; - len += pass->len; - - if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { - increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump))); - len = 0; - nump = 0; - } - - ++pass; - } - t2_putcommacode(bio, increment); - - /* computation of the new Length indicator */ - cblk->numlenbits += increment; - - pass = cblk->passes + cblk->numpasses; - /* insertion of the codeword segment length */ - for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) { - nump++; - len += pass->len; - - if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { - bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump)); - len = 0; - nump = 0; - } - ++pass; - } - - ++cblk; - } - - ++band; - } - - if (bio_flush(bio)) { - bio_destroy(bio); - return OPJ_FALSE; /* modified to eliminate longjmp !! */ - } - - l_nb_bytes = bio_numbytes(bio); - c += l_nb_bytes; - length -= l_nb_bytes; - - bio_destroy(bio); - - /* */ - if (tcp->csty & J2K_CP_CSTY_EPH) { - c[0] = 255; - c[1] = 146; - c += 2; - length -= 2; - } - /* */ - - /* << INDEX */ - // End of packet header position. Currently only represents the distance to start of packet - // Will be updated later by incrementing with packet start value - if(cstr_info && cstr_info->index_write) { - opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; - info_PK->end_ph_pos = (OPJ_INT32)(c - dest); - } - /* INDEX >> */ - - /* Writing the packet body */ - band = res->bands; - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; - - l_nb_blocks = prc->cw * prc->ch; - cblk = prc->cblks.enc; - - for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { - opj_tcd_layer_t *layer = &cblk->layers[layno]; - - if (!layer->numpasses) { - ++cblk; - continue; - } - - if (layer->len > length) { - return OPJ_FALSE; - } - - memcpy(c, layer->data, layer->len); - cblk->numpasses += layer->numpasses; - c += layer->len; - length -= layer->len; - - /* << INDEX */ - if(cstr_info && cstr_info->index_write) { - opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; - info_PK->disto += layer->disto; - if (cstr_info->D_max < info_PK->disto) { - cstr_info->D_max = info_PK->disto; - } - } - - ++cblk; - /* INDEX >> */ - } - ++band; - } - - * p_data_written += (c - dest); - - return OPJ_TRUE; + OPJ_UINT32 bandno, cblkno; + OPJ_BYTE *c = dest; + OPJ_UINT32 l_nb_bytes; + OPJ_UINT32 compno = pi->compno; /* component value */ + OPJ_UINT32 resno = pi->resno; /* resolution level value */ + OPJ_UINT32 precno = pi->precno; /* precinct value */ + OPJ_UINT32 layno = pi->layno; /* quality layer value */ + OPJ_UINT32 l_nb_blocks; + opj_tcd_band_v2_t *band = 00; + opj_tcd_cblk_enc_v2_t* cblk = 00; + opj_tcd_pass_v2_t *pass = 00; + + opj_tcd_tilecomp_v2_t *tilec = &tile->comps[compno]; + opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno]; + + opj_bio_t *bio = 00; /* BIO component */ + + /* */ + if (tcp->csty & J2K_CP_CSTY_SOP) { + c[0] = 255; + c[1] = 145; + c[2] = 0; + c[3] = 4; + c[4] = (tile->packno % 65536) / 256; + c[5] = (tile->packno % 65536) % 256; + c += 6; + length -= 6; + } + /* */ + + if (!layno) { + band = res->bands; + + for(bandno = 0; bandno < res->numbands; ++bandno) { + opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; + + tgt_reset(prc->incltree); + tgt_reset(prc->imsbtree); + + l_nb_blocks = prc->cw * prc->ch; + for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { + opj_tcd_cblk_enc_v2_t* cblk = &prc->cblks.enc[cblkno]; + + cblk->numpasses = 0; + tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps); + } + ++band; + } + } + + bio = bio_create(); + bio_init_enc(bio, c, length); + bio_write(bio, 1, 1); /* Empty header bit */ + + /* Writing Packet header */ + band = res->bands; + for (bandno = 0; bandno < res->numbands; ++bandno) { + opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; + + l_nb_blocks = prc->cw * prc->ch; + cblk = prc->cblks.enc; + + for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { + opj_tcd_layer_t *layer = &cblk->layers[layno]; + + if (!cblk->numpasses && layer->numpasses) { + tgt_setvalue(prc->incltree, cblkno, layno); + } + + ++cblk; + } + + cblk = prc->cblks.enc; + for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) { + opj_tcd_layer_t *layer = &cblk->layers[layno]; + OPJ_UINT32 increment = 0; + OPJ_UINT32 nump = 0; + OPJ_UINT32 len = 0, passno; + OPJ_UINT32 l_nb_passes; + + /* cblk inclusion bits */ + if (!cblk->numpasses) { + tgt_encode(bio, prc->incltree, cblkno, layno + 1); + } else { + bio_write(bio, layer->numpasses != 0, 1); + } + + /* if cblk not included, go to the next cblk */ + if (!layer->numpasses) { + ++cblk; + continue; + } + + /* if first instance of cblk --> zero bit-planes information */ + if (!cblk->numpasses) { + cblk->numlenbits = 3; + tgt_encode(bio, prc->imsbtree, cblkno, 999); + } + + /* number of coding passes included */ + t2_putnumpasses(bio, layer->numpasses); + l_nb_passes = cblk->numpasses + layer->numpasses; + pass = cblk->passes + cblk->numpasses; + + /* computation of the increase of the length indicator and insertion in the header */ + for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) { + ++nump; + len += pass->len; + + if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { + increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump))); + len = 0; + nump = 0; + } + + ++pass; + } + t2_putcommacode(bio, increment); + + /* computation of the new Length indicator */ + cblk->numlenbits += increment; + + pass = cblk->passes + cblk->numpasses; + /* insertion of the codeword segment length */ + for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) { + nump++; + len += pass->len; + + if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { + bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump)); + len = 0; + nump = 0; + } + ++pass; + } + + ++cblk; + } + + ++band; + } + + if (bio_flush(bio)) { + bio_destroy(bio); + return OPJ_FALSE; /* modified to eliminate longjmp !! */ + } + + l_nb_bytes = bio_numbytes(bio); + c += l_nb_bytes; + length -= l_nb_bytes; + + bio_destroy(bio); + + /* */ + if (tcp->csty & J2K_CP_CSTY_EPH) { + c[0] = 255; + c[1] = 146; + c += 2; + length -= 2; + } + /* */ + + /* << INDEX */ + // End of packet header position. Currently only represents the distance to start of packet + // Will be updated later by incrementing with packet start value + if(cstr_info && cstr_info->index_write) { + opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; + info_PK->end_ph_pos = (OPJ_INT32)(c - dest); + } + /* INDEX >> */ + + /* Writing the packet body */ + band = res->bands; + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; + + l_nb_blocks = prc->cw * prc->ch; + cblk = prc->cblks.enc; + + for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) { + opj_tcd_layer_t *layer = &cblk->layers[layno]; + + if (!layer->numpasses) { + ++cblk; + continue; + } + + if (layer->len > length) { + return OPJ_FALSE; + } + + memcpy(c, layer->data, layer->len); + cblk->numpasses += layer->numpasses; + c += layer->len; + length -= layer->len; + + /* << INDEX */ + if(cstr_info && cstr_info->index_write) { + opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; + info_PK->disto += layer->disto; + if (cstr_info->D_max < info_PK->disto) { + cstr_info->D_max = info_PK->disto; + } + } + + ++cblk; + /* INDEX >> */ + } + ++band; + } + + * p_data_written += (c - dest); + + return OPJ_TRUE; } static opj_bool t2_skip_packet( - opj_t2_v2_t* p_t2, - opj_tcd_tile_v2_t *p_tile, + opj_t2_v2_t* p_t2, + opj_tcd_tile_v2_t *p_tile, opj_tcp_v2_t *p_tcp, - opj_pi_iterator_t *p_pi, - OPJ_BYTE *p_src, - OPJ_UINT32 * p_data_read, - OPJ_UINT32 p_max_length, - opj_packet_info_t *p_pack_info) + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info) { - opj_bool l_read_data; - OPJ_UINT32 l_nb_bytes_read = 0; - OPJ_UINT32 l_nb_total_bytes_read = 0; + opj_bool l_read_data; + OPJ_UINT32 l_nb_bytes_read = 0; + OPJ_UINT32 l_nb_total_bytes_read = 0; - *p_data_read = 0; + *p_data_read = 0; - if (! t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) { - return OPJ_FALSE; - } + if (! t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) { + return OPJ_FALSE; + } - p_src += l_nb_bytes_read; - l_nb_total_bytes_read += l_nb_bytes_read; - p_max_length -= l_nb_bytes_read; + p_src += l_nb_bytes_read; + l_nb_total_bytes_read += l_nb_bytes_read; + p_max_length -= l_nb_bytes_read; - /* we should read data for the packet */ - if (l_read_data) { - l_nb_bytes_read = 0; + /* we should read data for the packet */ + if (l_read_data) { + l_nb_bytes_read = 0; - if (! t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) { - return OPJ_FALSE; - } + if (! t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) { + return OPJ_FALSE; + } - l_nb_total_bytes_read += l_nb_bytes_read; - } - *p_data_read = l_nb_total_bytes_read; + l_nb_total_bytes_read += l_nb_bytes_read; + } + *p_data_read = l_nb_total_bytes_read; - return OPJ_TRUE; + return OPJ_TRUE; } static opj_bool t2_read_packet_header( - opj_t2_v2_t* p_t2, - opj_tcd_tile_v2_t *p_tile, + opj_t2_v2_t* p_t2, + opj_tcd_tile_v2_t *p_tile, opj_tcp_v2_t *p_tcp, - opj_pi_iterator_t *p_pi, - opj_bool * p_is_data_present, - OPJ_BYTE *p_src_data, - OPJ_UINT32 * p_data_read, - OPJ_UINT32 p_max_length, - opj_packet_info_t *p_pack_info) + opj_pi_iterator_t *p_pi, + opj_bool * p_is_data_present, + OPJ_BYTE *p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info) { - /* loop */ - OPJ_UINT32 bandno, cblkno; - OPJ_UINT32 l_nb_code_blocks; - OPJ_UINT32 l_remaining_length; - OPJ_UINT32 l_header_length; - OPJ_UINT32 * l_modified_length_ptr = 00; - OPJ_BYTE *l_current_data = p_src_data; - opj_cp_v2_t *l_cp = p_t2->cp; - opj_bio_t *l_bio = 00; /* BIO component */ - opj_tcd_band_v2_t *l_band = 00; - opj_tcd_cblk_dec_v2_t* l_cblk = 00; - opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; - - OPJ_BYTE *l_header_data = 00; - OPJ_BYTE **l_header_data_start = 00; - - OPJ_UINT32 l_present; - - if (p_pi->layno == 0) { - l_band = l_res->bands; - - /* reset tagtrees */ - for (bandno = 0; bandno < l_res->numbands; ++bandno) { - opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno]; - - if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) { - tgt_reset(l_prc->incltree); - tgt_reset(l_prc->imsbtree); - l_cblk = l_prc->cblks.dec; - - l_nb_code_blocks = l_prc->cw * l_prc->ch; - for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { - l_cblk->numsegs = 0; - l_cblk->real_num_segs = 0; - ++l_cblk; - } - } - - ++l_band; - } - } - - /* SOP markers */ - - if (p_tcp->csty & J2K_CP_CSTY_SOP) { - if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) { - /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); */ - } else { - l_current_data += 6; - } - - /** TODO : check the Nsop value */ - } - - /* - When the marker PPT/PPM is used the packet header are store in PPT/PPM marker - This part deal with this caracteristic - step 1: Read packet header in the saved structure - step 2: Return to codestream for decoding - */ - - l_bio = bio_create(); - if (! l_bio) { - return OPJ_FALSE; - } - - if (l_cp->ppm == 1) { /* PPM */ - l_header_data_start = &l_cp->ppm_data; - l_header_data = *l_header_data_start; - l_modified_length_ptr = &(l_cp->ppm_len); - - } - else if (p_tcp->ppt == 1) { /* PPT */ - l_header_data_start = &(p_tcp->ppt_data); - l_header_data = *l_header_data_start; - l_modified_length_ptr = &(p_tcp->ppt_len); - } - else { /* Normal Case */ - l_header_data_start = &(l_current_data); - l_header_data = *l_header_data_start; - l_remaining_length = p_src_data+p_max_length-l_header_data; - l_modified_length_ptr = &(l_remaining_length); - } - - bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr); - - l_present = bio_read(l_bio, 1); - if (!l_present) { - bio_inalign(l_bio); - l_header_data += bio_numbytes(l_bio); - bio_destroy(l_bio); - - /* EPH markers */ - if (p_tcp->csty & J2K_CP_CSTY_EPH) { - if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { - printf("Error : expected EPH marker\n"); - } else { - l_header_data += 2; - } - } - - l_header_length = (l_header_data - *l_header_data_start); - *l_modified_length_ptr -= l_header_length; - *l_header_data_start += l_header_length; - - /* << INDEX */ - /* End of packet header position. Currently only represents the distance to start of packet - Will be updated later by incrementing with packet start value */ - if (p_pack_info) { - p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); - } - /* INDEX >> */ - - * p_is_data_present = OPJ_FALSE; - *p_data_read = l_current_data - p_src_data; - return OPJ_TRUE; - } - - l_band = l_res->bands; - for (bandno = 0; bandno < l_res->numbands; ++bandno) { - opj_tcd_precinct_v2_t *l_prc = &(l_band->precincts[p_pi->precno]); - - if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) { - ++l_band; - continue; - } - - l_nb_code_blocks = l_prc->cw * l_prc->ch; - l_cblk = l_prc->cblks.dec; - for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) { - OPJ_UINT32 l_included,l_increment, l_segno; - OPJ_INT32 n; - - /* if cblk not yet included before --> inclusion tagtree */ - if (!l_cblk->numsegs) { - l_included = tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1); - /* else one bit */ - } - else { - l_included = bio_read(l_bio, 1); - } - - /* if cblk not included */ - if (!l_included) { - l_cblk->numnewpasses = 0; - ++l_cblk; - continue; - } - - /* if cblk not yet included --> zero-bitplane tagtree */ - if (!l_cblk->numsegs) { - OPJ_UINT32 i = 0; - - while (!tgt_decode(l_bio, l_prc->imsbtree, cblkno, i)) { - ++i; - } - - l_cblk->numbps = l_band->numbps + 1 - i; - l_cblk->numlenbits = 3; - } - - /* number of coding passes */ - l_cblk->numnewpasses = t2_getnumpasses(l_bio); - l_increment = t2_getcommacode(l_bio); - - /* length indicator increment */ - l_cblk->numlenbits += l_increment; - l_segno = 0; - - if (!l_cblk->numsegs) { - if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) { - bio_destroy(l_bio); - return OPJ_FALSE; - } - } - else { - l_segno = l_cblk->numsegs - 1; - if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) { - ++l_segno; - if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { - bio_destroy(l_bio); - return OPJ_FALSE; - } - } - } - n = l_cblk->numnewpasses; - - do { - l_cblk->segs[l_segno].numnewpasses = int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n); - l_cblk->segs[l_segno].newlen = bio_read(l_bio, l_cblk->numlenbits + uint_floorlog2(l_cblk->segs[l_segno].numnewpasses)); - - n -= l_cblk->segs[l_segno].numnewpasses; - if (n > 0) { - ++l_segno; - - if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { - bio_destroy(l_bio); - return OPJ_FALSE; - } - } - } while (n > 0); - - ++l_cblk; - } - - ++l_band; - } - - if (bio_inalign(l_bio)) { - bio_destroy(l_bio); - return OPJ_FALSE; - } - - l_header_data += bio_numbytes(l_bio); - bio_destroy(l_bio); - - /* EPH markers */ - if (p_tcp->csty & J2K_CP_CSTY_EPH) { - if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { - /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */ - } else { - l_header_data += 2; - } - } - - l_header_length = (l_header_data - *l_header_data_start); - *l_modified_length_ptr -= l_header_length; - *l_header_data_start += l_header_length; - - /* << INDEX */ - /* End of packet header position. Currently only represents the distance to start of packet - Will be updated later by incrementing with packet start value */ - if (p_pack_info) { - p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); - } - /* INDEX >> */ - - *p_is_data_present = OPJ_TRUE; - *p_data_read = l_current_data - p_src_data; - - return OPJ_TRUE; + /* loop */ + OPJ_UINT32 bandno, cblkno; + OPJ_UINT32 l_nb_code_blocks; + OPJ_UINT32 l_remaining_length; + OPJ_UINT32 l_header_length; + OPJ_UINT32 * l_modified_length_ptr = 00; + OPJ_BYTE *l_current_data = p_src_data; + opj_cp_v2_t *l_cp = p_t2->cp; + opj_bio_t *l_bio = 00; /* BIO component */ + opj_tcd_band_v2_t *l_band = 00; + opj_tcd_cblk_dec_v2_t* l_cblk = 00; + opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; + + OPJ_BYTE *l_header_data = 00; + OPJ_BYTE **l_header_data_start = 00; + + OPJ_UINT32 l_present; + + if (p_pi->layno == 0) { + l_band = l_res->bands; + + /* reset tagtrees */ + for (bandno = 0; bandno < l_res->numbands; ++bandno) { + opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno]; + + if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) { + tgt_reset(l_prc->incltree); + tgt_reset(l_prc->imsbtree); + l_cblk = l_prc->cblks.dec; + + l_nb_code_blocks = l_prc->cw * l_prc->ch; + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + l_cblk->numsegs = 0; + l_cblk->real_num_segs = 0; + ++l_cblk; + } + } + + ++l_band; + } + } + + /* SOP markers */ + + if (p_tcp->csty & J2K_CP_CSTY_SOP) { + if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) { + /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); */ + } else { + l_current_data += 6; + } + + /** TODO : check the Nsop value */ + } + + /* + When the marker PPT/PPM is used the packet header are store in PPT/PPM marker + This part deal with this caracteristic + step 1: Read packet header in the saved structure + step 2: Return to codestream for decoding + */ + + l_bio = bio_create(); + if (! l_bio) { + return OPJ_FALSE; + } + + if (l_cp->ppm == 1) { /* PPM */ + l_header_data_start = &l_cp->ppm_data; + l_header_data = *l_header_data_start; + l_modified_length_ptr = &(l_cp->ppm_len); + + } + else if (p_tcp->ppt == 1) { /* PPT */ + l_header_data_start = &(p_tcp->ppt_data); + l_header_data = *l_header_data_start; + l_modified_length_ptr = &(p_tcp->ppt_len); + } + else { /* Normal Case */ + l_header_data_start = &(l_current_data); + l_header_data = *l_header_data_start; + l_remaining_length = p_src_data+p_max_length-l_header_data; + l_modified_length_ptr = &(l_remaining_length); + } + + bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr); + + l_present = bio_read(l_bio, 1); + if (!l_present) { + bio_inalign(l_bio); + l_header_data += bio_numbytes(l_bio); + bio_destroy(l_bio); + + /* EPH markers */ + if (p_tcp->csty & J2K_CP_CSTY_EPH) { + if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { + printf("Error : expected EPH marker\n"); + } else { + l_header_data += 2; + } + } + + l_header_length = (l_header_data - *l_header_data_start); + *l_modified_length_ptr -= l_header_length; + *l_header_data_start += l_header_length; + + /* << INDEX */ + /* End of packet header position. Currently only represents the distance to start of packet + Will be updated later by incrementing with packet start value */ + if (p_pack_info) { + p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); + } + /* INDEX >> */ + + * p_is_data_present = OPJ_FALSE; + *p_data_read = l_current_data - p_src_data; + return OPJ_TRUE; + } + + l_band = l_res->bands; + for (bandno = 0; bandno < l_res->numbands; ++bandno) { + opj_tcd_precinct_v2_t *l_prc = &(l_band->precincts[p_pi->precno]); + + if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) { + ++l_band; + continue; + } + + l_nb_code_blocks = l_prc->cw * l_prc->ch; + l_cblk = l_prc->cblks.dec; + for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) { + OPJ_UINT32 l_included,l_increment, l_segno; + OPJ_INT32 n; + + /* if cblk not yet included before --> inclusion tagtree */ + if (!l_cblk->numsegs) { + l_included = tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1); + /* else one bit */ + } + else { + l_included = bio_read(l_bio, 1); + } + + /* if cblk not included */ + if (!l_included) { + l_cblk->numnewpasses = 0; + ++l_cblk; + continue; + } + + /* if cblk not yet included --> zero-bitplane tagtree */ + if (!l_cblk->numsegs) { + OPJ_UINT32 i = 0; + + while (!tgt_decode(l_bio, l_prc->imsbtree, cblkno, i)) { + ++i; + } + + l_cblk->numbps = l_band->numbps + 1 - i; + l_cblk->numlenbits = 3; + } + + /* number of coding passes */ + l_cblk->numnewpasses = t2_getnumpasses(l_bio); + l_increment = t2_getcommacode(l_bio); + + /* length indicator increment */ + l_cblk->numlenbits += l_increment; + l_segno = 0; + + if (!l_cblk->numsegs) { + if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) { + bio_destroy(l_bio); + return OPJ_FALSE; + } + } + else { + l_segno = l_cblk->numsegs - 1; + if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) { + ++l_segno; + if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { + bio_destroy(l_bio); + return OPJ_FALSE; + } + } + } + n = l_cblk->numnewpasses; + + do { + l_cblk->segs[l_segno].numnewpasses = int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n); + l_cblk->segs[l_segno].newlen = bio_read(l_bio, l_cblk->numlenbits + uint_floorlog2(l_cblk->segs[l_segno].numnewpasses)); + + n -= l_cblk->segs[l_segno].numnewpasses; + if (n > 0) { + ++l_segno; + + if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { + bio_destroy(l_bio); + return OPJ_FALSE; + } + } + } while (n > 0); + + ++l_cblk; + } + + ++l_band; + } + + if (bio_inalign(l_bio)) { + bio_destroy(l_bio); + return OPJ_FALSE; + } + + l_header_data += bio_numbytes(l_bio); + bio_destroy(l_bio); + + /* EPH markers */ + if (p_tcp->csty & J2K_CP_CSTY_EPH) { + if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { + /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */ + } else { + l_header_data += 2; + } + } + + l_header_length = (l_header_data - *l_header_data_start); + *l_modified_length_ptr -= l_header_length; + *l_header_data_start += l_header_length; + + /* << INDEX */ + /* End of packet header position. Currently only represents the distance to start of packet + Will be updated later by incrementing with packet start value */ + if (p_pack_info) { + p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); + } + /* INDEX >> */ + + *p_is_data_present = OPJ_TRUE; + *p_data_read = l_current_data - p_src_data; + + return OPJ_TRUE; } static opj_bool t2_read_packet_data( - opj_t2_v2_t* p_t2, - opj_tcd_tile_v2_t *p_tile, - opj_pi_iterator_t *p_pi, - OPJ_BYTE *p_src_data, - OPJ_UINT32 * p_data_read, - OPJ_UINT32 p_max_length, - opj_packet_info_t *pack_info) + opj_t2_v2_t* p_t2, + opj_tcd_tile_v2_t *p_tile, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *pack_info) { - OPJ_UINT32 bandno, cblkno; - OPJ_UINT32 l_nb_code_blocks; - OPJ_BYTE *l_current_data = p_src_data; - opj_tcd_band_v2_t *l_band = 00; - opj_tcd_cblk_dec_v2_t* l_cblk = 00; - opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; - - l_band = l_res->bands; - for (bandno = 0; bandno < l_res->numbands; ++bandno) { - opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno]; - - if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) { - ++l_band; - continue; - } - - l_nb_code_blocks = l_prc->cw * l_prc->ch; - l_cblk = l_prc->cblks.dec; - - for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { - opj_tcd_seg_t *l_seg = 00; - - if (!l_cblk->numnewpasses) { - /* nothing to do */ - ++l_cblk; - continue; - } - - if (!l_cblk->numsegs) { - l_seg = l_cblk->segs; - ++l_cblk->numsegs; - l_cblk->len = 0; - } - else { - l_seg = &l_cblk->segs[l_cblk->numsegs - 1]; - - if (l_seg->numpasses == l_seg->maxpasses) { - ++l_seg; - ++l_cblk->numsegs; - } - } - - do { - if (l_current_data + l_seg->newlen > p_src_data + p_max_length) { - return OPJ_FALSE; - } + OPJ_UINT32 bandno, cblkno; + OPJ_UINT32 l_nb_code_blocks; + OPJ_BYTE *l_current_data = p_src_data; + opj_tcd_band_v2_t *l_band = 00; + opj_tcd_cblk_dec_v2_t* l_cblk = 00; + opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; + + l_band = l_res->bands; + for (bandno = 0; bandno < l_res->numbands; ++bandno) { + opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno]; + + if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) { + ++l_band; + continue; + } + + l_nb_code_blocks = l_prc->cw * l_prc->ch; + l_cblk = l_prc->cblks.dec; + + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + opj_tcd_seg_t *l_seg = 00; + + if (!l_cblk->numnewpasses) { + /* nothing to do */ + ++l_cblk; + continue; + } + + if (!l_cblk->numsegs) { + l_seg = l_cblk->segs; + ++l_cblk->numsegs; + l_cblk->len = 0; + } + else { + l_seg = &l_cblk->segs[l_cblk->numsegs - 1]; + + if (l_seg->numpasses == l_seg->maxpasses) { + ++l_seg; + ++l_cblk->numsegs; + } + } + + do { + if (l_current_data + l_seg->newlen > p_src_data + p_max_length) { + return OPJ_FALSE; + } #ifdef USE_JPWL - /* we need here a j2k handle to verify if making a check to - the validity of cblocks parameters is selected from user (-W) */ - - /* let's check that we are not exceeding */ - if ((l_cblk->len + l_seg->newlen) > 8192) { - opj_event_msg(p_t2->cinfo, EVT_WARNING, - "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", - l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno); - if (!JPWL_ASSUME) { - opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - l_seg->newlen = 8192 - l_cblk->len; - opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen); - break; - }; + /* we need here a j2k handle to verify if making a check to + the validity of cblocks parameters is selected from user (-W) */ + + /* let's check that we are not exceeding */ + if ((l_cblk->len + l_seg->newlen) > 8192) { + opj_event_msg(p_t2->cinfo, EVT_WARNING, + "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", + l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno); + if (!JPWL_ASSUME) { + opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + l_seg->newlen = 8192 - l_cblk->len; + opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen); + break; + }; #endif /* USE_JPWL */ - memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen); + memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen); - if (l_seg->numpasses == 0) { - l_seg->data = &l_cblk->data; - l_seg->dataindex = l_cblk->len; - } + if (l_seg->numpasses == 0) { + l_seg->data = &l_cblk->data; + l_seg->dataindex = l_cblk->len; + } - l_current_data += l_seg->newlen; - l_seg->numpasses += l_seg->numnewpasses; - l_cblk->numnewpasses -= l_seg->numnewpasses; + l_current_data += l_seg->newlen; + l_seg->numpasses += l_seg->numnewpasses; + l_cblk->numnewpasses -= l_seg->numnewpasses; - l_seg->real_num_passes = l_seg->numpasses; - l_cblk->len += l_seg->newlen; - l_seg->len += l_seg->newlen; + l_seg->real_num_passes = l_seg->numpasses; + l_cblk->len += l_seg->newlen; + l_seg->len += l_seg->newlen; - if (l_cblk->numnewpasses > 0) { - ++l_seg; - ++l_cblk->numsegs; - } - } while (l_cblk->numnewpasses > 0); + if (l_cblk->numnewpasses > 0) { + ++l_seg; + ++l_cblk->numsegs; + } + } while (l_cblk->numnewpasses > 0); - l_cblk->real_num_segs = l_cblk->numsegs; - ++l_cblk; - } + l_cblk->real_num_segs = l_cblk->numsegs; + ++l_cblk; + } - ++l_band; - } + ++l_band; + } - *(p_data_read) = l_current_data - p_src_data; + *(p_data_read) = l_current_data - p_src_data; - return OPJ_TRUE; + return OPJ_TRUE; } static opj_bool t2_skip_packet_data( - opj_t2_v2_t* p_t2, - opj_tcd_tile_v2_t *p_tile, - opj_pi_iterator_t *p_pi, - OPJ_UINT32 * p_data_read, - OPJ_UINT32 p_max_length, - opj_packet_info_t *pack_info) + opj_t2_v2_t* p_t2, + opj_tcd_tile_v2_t *p_tile, + opj_pi_iterator_t *p_pi, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *pack_info) { - OPJ_UINT32 bandno, cblkno; - OPJ_UINT32 l_nb_code_blocks; - opj_tcd_band_v2_t *l_band = 00; - opj_tcd_cblk_dec_v2_t* l_cblk = 00; - opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; - - *p_data_read = 0; - l_band = l_res->bands; - - for (bandno = 0; bandno < l_res->numbands; ++bandno) { - opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno]; - - if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) { - ++l_band; - continue; - } - - l_nb_code_blocks = l_prc->cw * l_prc->ch; - l_cblk = l_prc->cblks.dec; - - for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { - opj_tcd_seg_t *l_seg = 00; - - if (!l_cblk->numnewpasses) { - /* nothing to do */ - ++l_cblk; - continue; - } - - if (!l_cblk->numsegs) { - l_seg = l_cblk->segs; - ++l_cblk->numsegs; - l_cblk->len = 0; - } - else { - l_seg = &l_cblk->segs[l_cblk->numsegs - 1]; - - if (l_seg->numpasses == l_seg->maxpasses) { - ++l_seg; - ++l_cblk->numsegs; - } - } - - do { - if (* p_data_read + l_seg->newlen > p_max_length) { - return OPJ_FALSE; - } + OPJ_UINT32 bandno, cblkno; + OPJ_UINT32 l_nb_code_blocks; + opj_tcd_band_v2_t *l_band = 00; + opj_tcd_cblk_dec_v2_t* l_cblk = 00; + opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; + + *p_data_read = 0; + l_band = l_res->bands; + + for (bandno = 0; bandno < l_res->numbands; ++bandno) { + opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno]; + + if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) { + ++l_band; + continue; + } + + l_nb_code_blocks = l_prc->cw * l_prc->ch; + l_cblk = l_prc->cblks.dec; + + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + opj_tcd_seg_t *l_seg = 00; + + if (!l_cblk->numnewpasses) { + /* nothing to do */ + ++l_cblk; + continue; + } + + if (!l_cblk->numsegs) { + l_seg = l_cblk->segs; + ++l_cblk->numsegs; + l_cblk->len = 0; + } + else { + l_seg = &l_cblk->segs[l_cblk->numsegs - 1]; + + if (l_seg->numpasses == l_seg->maxpasses) { + ++l_seg; + ++l_cblk->numsegs; + } + } + + do { + if (* p_data_read + l_seg->newlen > p_max_length) { + return OPJ_FALSE; + } #ifdef USE_JPWL - /* we need here a j2k handle to verify if making a check to - the validity of cblocks parameters is selected from user (-W) */ - - /* let's check that we are not exceeding */ - if ((l_cblk->len + l_seg->newlen) > 8192) { - opj_event_msg(p_t2->cinfo, EVT_WARNING, - "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", - l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno); - if (!JPWL_ASSUME) { - opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n"); - return -999; - } - l_seg->newlen = 8192 - l_cblk->len; - opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen); - break; - }; + /* we need here a j2k handle to verify if making a check to + the validity of cblocks parameters is selected from user (-W) */ + + /* let's check that we are not exceeding */ + if ((l_cblk->len + l_seg->newlen) > 8192) { + opj_event_msg(p_t2->cinfo, EVT_WARNING, + "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", + l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno); + if (!JPWL_ASSUME) { + opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n"); + return -999; + } + l_seg->newlen = 8192 - l_cblk->len; + opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen); + break; + }; #endif /* USE_JPWL */ - *(p_data_read) += l_seg->newlen; + *(p_data_read) += l_seg->newlen; - l_seg->numpasses += l_seg->numnewpasses; - l_cblk->numnewpasses -= l_seg->numnewpasses; - if (l_cblk->numnewpasses > 0) - { - ++l_seg; - ++l_cblk->numsegs; - } - } while (l_cblk->numnewpasses > 0); + l_seg->numpasses += l_seg->numnewpasses; + l_cblk->numnewpasses -= l_seg->numnewpasses; + if (l_cblk->numnewpasses > 0) + { + ++l_seg; + ++l_cblk->numsegs; + } + } while (l_cblk->numnewpasses > 0); - ++l_cblk; - } + ++l_cblk; + } - ++l_band; - } + ++l_band; + } - return OPJ_TRUE; + return OPJ_TRUE; } static opj_bool t2_init_seg_v2(opj_tcd_cblk_dec_v2_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first) { - opj_tcd_seg_t* seg = 00; - OPJ_UINT32 l_nb_segs = index + 1; - - if (l_nb_segs > cblk->m_current_max_segs) { - cblk->m_current_max_segs += J2K_DEFAULT_NB_SEGS; - - cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t)); - if(! cblk->segs) { - return OPJ_FALSE; - } - } - - seg = &cblk->segs[index]; - memset(seg,0,sizeof(opj_tcd_seg_t)); - - if (cblksty & J2K_CCP_CBLKSTY_TERMALL) { - seg->maxpasses = 1; - } - else if (cblksty & J2K_CCP_CBLKSTY_LAZY) { - if (first) { - seg->maxpasses = 10; - } else { - seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1; - } - } else { - seg->maxpasses = 109; - } - - return OPJ_TRUE; + opj_tcd_seg_t* seg = 00; + OPJ_UINT32 l_nb_segs = index + 1; + + if (l_nb_segs > cblk->m_current_max_segs) { + cblk->m_current_max_segs += J2K_DEFAULT_NB_SEGS; + + opj_tcd_seg_t* new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t)); + if(! new_segs) { + opj_free(cblk->segs); + cblk->segs = NULL; + cblk->m_current_max_segs = 0; + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */ + return OPJ_FALSE; + } + cblk->segs = new_segs; + } + + seg = &cblk->segs[index]; + memset(seg,0,sizeof(opj_tcd_seg_t)); + + if (cblksty & J2K_CCP_CBLKSTY_TERMALL) { + seg->maxpasses = 1; + } + else if (cblksty & J2K_CCP_CBLKSTY_LAZY) { + if (first) { + seg->maxpasses = 10; + } else { + seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1; + } + } else { + seg->maxpasses = 109; + } + + return OPJ_TRUE; } diff --git a/libopenjpeg/tcd.c b/libopenjpeg/tcd.c index 4dcd3e65..b0da9ca5 100644 --- a/libopenjpeg/tcd.c +++ b/libopenjpeg/tcd.c @@ -36,62 +36,62 @@ void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) { - int tileno, compno, resno, bandno, precno;/*, cblkno;*/ - - fprintf(fd, "image {\n"); - fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", - img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1); - - for (tileno = 0; tileno < img->th * img->tw; tileno++) { - opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno]; - fprintf(fd, " tile {\n"); - fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", - tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - fprintf(fd, " tilec {\n"); - fprintf(fd, - " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", - tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions); - for (resno = 0; resno < tilec->numresolutions; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - fprintf(fd, "\n res {\n"); - fprintf(fd, - " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", - res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands); - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - fprintf(fd, " band {\n"); - fprintf(fd, - " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n", - band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); - for (precno = 0; precno < res->pw * res->ph; precno++) { - opj_tcd_precinct_t *prec = &band->precincts[precno]; - fprintf(fd, " prec {\n"); - fprintf(fd, - " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", - prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch); - /* - for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) { - opj_tcd_cblk_t *cblk = &prec->cblks[cblkno]; - fprintf(fd, " cblk {\n"); - fprintf(fd, - " x0=%d, y0=%d, x1=%d, y1=%d\n", - cblk->x0, cblk->y0, cblk->x1, cblk->y1); - fprintf(fd, " }\n"); - } - */ - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, "}\n"); + int tileno, compno, resno, bandno, precno;/*, cblkno;*/ + + fprintf(fd, "image {\n"); + fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", + img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1); + + for (tileno = 0; tileno < img->th * img->tw; tileno++) { + opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno]; + fprintf(fd, " tile {\n"); + fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", + tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); + for (compno = 0; compno < tile->numcomps; compno++) { + opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; + fprintf(fd, " tilec {\n"); + fprintf(fd, + " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", + tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions); + for (resno = 0; resno < tilec->numresolutions; resno++) { + opj_tcd_resolution_t *res = &tilec->resolutions[resno]; + fprintf(fd, "\n res {\n"); + fprintf(fd, + " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", + res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands); + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_t *band = &res->bands[bandno]; + fprintf(fd, " band {\n"); + fprintf(fd, + " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n", + band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); + for (precno = 0; precno < res->pw * res->ph; precno++) { + opj_tcd_precinct_t *prec = &band->precincts[precno]; + fprintf(fd, " prec {\n"); + fprintf(fd, + " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", + prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch); + /* + for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) { + opj_tcd_cblk_t *cblk = &prec->cblks[cblkno]; + fprintf(fd, " cblk {\n"); + fprintf(fd, + " x0=%d, y0=%d, x1=%d, y1=%d\n", + cblk->x0, cblk->y0, cblk->x1, cblk->y1); + fprintf(fd, " }\n"); + } + */ + fprintf(fd, " }\n"); + } + fprintf(fd, " }\n"); + } + fprintf(fd, " }\n"); + } + fprintf(fd, " }\n"); + } + fprintf(fd, " }\n"); + } + fprintf(fd, "}\n"); } /** * Allocates memory for a decoding code block. @@ -144,16 +144,16 @@ static opj_bool opj_tcd_dwt_encode ( opj_tcd_v2_t *p_tcd ); static opj_bool opj_tcd_t1_encode ( opj_tcd_v2_t *p_tcd ); -static opj_bool opj_tcd_t2_encode ( opj_tcd_v2_t *p_tcd, - OPJ_BYTE * p_dest_data, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 p_max_dest_size, - opj_codestream_info_t *p_cstr_info ); +static opj_bool opj_tcd_t2_encode ( opj_tcd_v2_t *p_tcd, + OPJ_BYTE * p_dest_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_dest_size, + opj_codestream_info_t *p_cstr_info ); -static opj_bool opj_tcd_rate_allocate_encode( opj_tcd_v2_t *p_tcd, - OPJ_BYTE * p_dest_data, - OPJ_UINT32 p_max_dest_size, - opj_codestream_info_t *p_cstr_info ); +static opj_bool opj_tcd_rate_allocate_encode( opj_tcd_v2_t *p_tcd, + OPJ_BYTE * p_dest_data, + OPJ_UINT32 p_max_dest_size, + opj_codestream_info_t *p_cstr_info ); /* ----------------------------------------------------------------------- */ @@ -162,813 +162,836 @@ Create a new TCD handle */ opj_tcd_v2_t* opj_tcd_create(opj_bool p_is_decoder) { - opj_tcd_v2_t *l_tcd = 00; + opj_tcd_v2_t *l_tcd = 00; - /* create the tcd structure */ - l_tcd = (opj_tcd_v2_t*) opj_malloc(sizeof(opj_tcd_v2_t)); - if (!l_tcd) { - return 00; - } - memset(l_tcd,0,sizeof(opj_tcd_v2_t)); + /* create the tcd structure */ + l_tcd = (opj_tcd_v2_t*) opj_malloc(sizeof(opj_tcd_v2_t)); + if (!l_tcd) { + return 00; + } + memset(l_tcd,0,sizeof(opj_tcd_v2_t)); - l_tcd->m_is_decoder = p_is_decoder ? 1 : 0; + l_tcd->m_is_decoder = p_is_decoder ? 1 : 0; - l_tcd->tcd_image = (opj_tcd_image_v2_t*)opj_malloc(sizeof(opj_tcd_image_v2_t)); - if (!l_tcd->tcd_image) { - opj_free(l_tcd); - return 00; - } - memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_v2_t)); + l_tcd->tcd_image = (opj_tcd_image_v2_t*)opj_malloc(sizeof(opj_tcd_image_v2_t)); + if (!l_tcd->tcd_image) { + opj_free(l_tcd); + return 00; + } + memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_v2_t)); - return l_tcd; + return l_tcd; } /* ----------------------------------------------------------------------- */ void opj_tcd_rateallocate_fixed(opj_tcd_v2_t *tcd) { - OPJ_UINT32 layno; + OPJ_UINT32 layno; - for (layno = 0; layno < tcd->tcp->numlayers; layno++) { - opj_tcd_makelayer_fixed(tcd, layno, 1); - } + for (layno = 0; layno < tcd->tcp->numlayers; layno++) { + opj_tcd_makelayer_fixed(tcd, layno, 1); + } } -void opj_tcd_makelayer( opj_tcd_v2_t *tcd, - OPJ_UINT32 layno, - OPJ_FLOAT64 thresh, - OPJ_UINT32 final) +void opj_tcd_makelayer( opj_tcd_v2_t *tcd, + OPJ_UINT32 layno, + OPJ_FLOAT64 thresh, + OPJ_UINT32 final) { - OPJ_UINT32 compno, resno, bandno, precno, cblkno; - OPJ_UINT32 passno; - - opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles; - - tcd_tile->distolayer[layno] = 0; /* fixed_quality */ - - for (compno = 0; compno < tcd_tile->numcomps; compno++) { - opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno]; - - for (resno = 0; resno < tilec->numresolutions; resno++) { - opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno]; - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_v2_t *band = &res->bands[bandno]; - - for (precno = 0; precno < res->pw * res->ph; precno++) { - opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno]; - opj_tcd_layer_t *layer = &cblk->layers[layno]; - OPJ_UINT32 n; - - if (layno == 0) { - cblk->numpassesinlayers = 0; - } - - n = cblk->numpassesinlayers; - - for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) { - OPJ_INT32 dr; - OPJ_FLOAT64 dd; - opj_tcd_pass_v2_t *pass = &cblk->passes[passno]; - - if (n == 0) { - dr = pass->rate; - dd = pass->distortiondec; - } else { - dr = pass->rate - cblk->passes[n - 1].rate; - dd = pass->distortiondec - cblk->passes[n - 1].distortiondec; - } - - if (!dr) { - if (dd != 0) - n = passno + 1; - continue; - } - if (dd / dr >= thresh) - n = passno + 1; - } - - layer->numpasses = n - cblk->numpassesinlayers; - - if (!layer->numpasses) { - layer->disto = 0; - continue; - } - - if (cblk->numpassesinlayers == 0) { - layer->len = cblk->passes[n - 1].rate; - layer->data = cblk->data; - layer->disto = cblk->passes[n - 1].distortiondec; - } else { - layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec; - } - - tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */ - - if (final) - cblk->numpassesinlayers = n; - } - } - } - } - } + OPJ_UINT32 compno, resno, bandno, precno, cblkno; + OPJ_UINT32 passno; + + opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles; + + tcd_tile->distolayer[layno] = 0; /* fixed_quality */ + + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno]; + + for (resno = 0; resno < tilec->numresolutions; resno++) { + opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno]; + + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_v2_t *band = &res->bands[bandno]; + + for (precno = 0; precno < res->pw * res->ph; precno++) { + opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; + + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno]; + opj_tcd_layer_t *layer = &cblk->layers[layno]; + OPJ_UINT32 n; + + if (layno == 0) { + cblk->numpassesinlayers = 0; + } + + n = cblk->numpassesinlayers; + + for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) { + OPJ_INT32 dr; + OPJ_FLOAT64 dd; + opj_tcd_pass_v2_t *pass = &cblk->passes[passno]; + + if (n == 0) { + dr = pass->rate; + dd = pass->distortiondec; + } else { + dr = pass->rate - cblk->passes[n - 1].rate; + dd = pass->distortiondec - cblk->passes[n - 1].distortiondec; + } + + if (!dr) { + if (dd != 0) + n = passno + 1; + continue; + } + if (dd / dr >= thresh) + n = passno + 1; + } + + layer->numpasses = n - cblk->numpassesinlayers; + + if (!layer->numpasses) { + layer->disto = 0; + continue; + } + + if (cblk->numpassesinlayers == 0) { + layer->len = cblk->passes[n - 1].rate; + layer->data = cblk->data; + layer->disto = cblk->passes[n - 1].distortiondec; + } else { + layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate; + layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; + layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec; + } + + tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */ + + if (final) + cblk->numpassesinlayers = n; + } + } + } + } + } } void opj_tcd_makelayer_fixed(opj_tcd_v2_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) { - OPJ_UINT32 compno, resno, bandno, precno, cblkno; - OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */ - OPJ_INT32 matrice[10][10][3]; - OPJ_UINT32 i, j, k; - - opj_cp_v2_t *cp = tcd->cp; - opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles; - opj_tcp_v2_t *tcd_tcp = tcd->tcp; - - for (compno = 0; compno < tcd_tile->numcomps; compno++) { - opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno]; - - for (i = 0; i < tcd_tcp->numlayers; i++) { - for (j = 0; j < tilec->numresolutions; j++) { - for (k = 0; k < 3; k++) { - matrice[i][j][k] = - (OPJ_INT32) (cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k] - * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0)); - } - } - } - - for (resno = 0; resno < tilec->numresolutions; resno++) { - opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno]; - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_v2_t *band = &res->bands[bandno]; - - for (precno = 0; precno < res->pw * res->ph; precno++) { - opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno]; - opj_tcd_layer_t *layer = &cblk->layers[layno]; - OPJ_UINT32 n; - OPJ_INT32 imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */ - - /* Correction of the matrix of coefficient to include the IMSB information */ - if (layno == 0) { - value = matrice[layno][resno][bandno]; - if (imsb >= value) { - value = 0; - } else { - value -= imsb; - } - } else { - value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno]; - if (imsb >= matrice[layno - 1][resno][bandno]) { - value -= (imsb - matrice[layno - 1][resno][bandno]); - if (value < 0) { - value = 0; - } - } - } - - if (layno == 0) { - cblk->numpassesinlayers = 0; - } - - n = cblk->numpassesinlayers; - if (cblk->numpassesinlayers == 0) { - if (value != 0) { - n = 3 * value - 2 + cblk->numpassesinlayers; - } else { - n = cblk->numpassesinlayers; - } - } else { - n = 3 * value + cblk->numpassesinlayers; - } - - layer->numpasses = n - cblk->numpassesinlayers; - - if (!layer->numpasses) - continue; - - if (cblk->numpassesinlayers == 0) { - layer->len = cblk->passes[n - 1].rate; - layer->data = cblk->data; - } else { - layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; - } - - if (final) - cblk->numpassesinlayers = n; - } - } - } - } - } + OPJ_UINT32 compno, resno, bandno, precno, cblkno; + OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */ + OPJ_INT32 matrice[10][10][3]; + OPJ_UINT32 i, j, k; + + opj_cp_v2_t *cp = tcd->cp; + opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles; + opj_tcp_v2_t *tcd_tcp = tcd->tcp; + + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno]; + + for (i = 0; i < tcd_tcp->numlayers; i++) { + for (j = 0; j < tilec->numresolutions; j++) { + for (k = 0; k < 3; k++) { + matrice[i][j][k] = + (OPJ_INT32) (cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k] + * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0)); + } + } + } + + for (resno = 0; resno < tilec->numresolutions; resno++) { + opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno]; + + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_v2_t *band = &res->bands[bandno]; + + for (precno = 0; precno < res->pw * res->ph; precno++) { + opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; + + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno]; + opj_tcd_layer_t *layer = &cblk->layers[layno]; + OPJ_UINT32 n; + OPJ_INT32 imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */ + + /* Correction of the matrix of coefficient to include the IMSB information */ + if (layno == 0) { + value = matrice[layno][resno][bandno]; + if (imsb >= value) { + value = 0; + } else { + value -= imsb; + } + } else { + value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno]; + if (imsb >= matrice[layno - 1][resno][bandno]) { + value -= (imsb - matrice[layno - 1][resno][bandno]); + if (value < 0) { + value = 0; + } + } + } + + if (layno == 0) { + cblk->numpassesinlayers = 0; + } + + n = cblk->numpassesinlayers; + if (cblk->numpassesinlayers == 0) { + if (value != 0) { + n = 3 * value - 2 + cblk->numpassesinlayers; + } else { + n = cblk->numpassesinlayers; + } + } else { + n = 3 * value + cblk->numpassesinlayers; + } + + layer->numpasses = n - cblk->numpassesinlayers; + + if (!layer->numpasses) + continue; + + if (cblk->numpassesinlayers == 0) { + layer->len = cblk->passes[n - 1].rate; + layer->data = cblk->data; + } else { + layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate; + layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; + } + + if (final) + cblk->numpassesinlayers = n; + } + } + } + } + } } -opj_bool opj_tcd_rateallocate( opj_tcd_v2_t *tcd, - OPJ_BYTE *dest, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 len, - opj_codestream_info_t *cstr_info) +opj_bool opj_tcd_rateallocate( opj_tcd_v2_t *tcd, + OPJ_BYTE *dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 len, + opj_codestream_info_t *cstr_info) { - OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno; - OPJ_UINT32 passno; - OPJ_FLOAT64 min, max; - OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */ - const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */ - OPJ_FLOAT64 maxSE = 0; - - opj_cp_v2_t *cp = tcd->cp; - opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles; - opj_tcp_v2_t *tcd_tcp = tcd->tcp; - - min = DBL_MAX; - max = 0; - - tcd_tile->numpix = 0; /* fixed_quality */ - - for (compno = 0; compno < tcd_tile->numcomps; compno++) { - opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno]; - tilec->numpix = 0; - - for (resno = 0; resno < tilec->numresolutions; resno++) { - opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno]; - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_v2_t *band = &res->bands[bandno]; - - for (precno = 0; precno < res->pw * res->ph; precno++) { - opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno]; - - for (passno = 0; passno < cblk->totalpasses; passno++) { - opj_tcd_pass_v2_t *pass = &cblk->passes[passno]; - OPJ_INT32 dr; - OPJ_FLOAT64 dd, rdslope; - - if (passno == 0) { - dr = pass->rate; - dd = pass->distortiondec; - } else { - dr = pass->rate - cblk->passes[passno - 1].rate; - dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec; - } - - if (dr == 0) { - continue; - } - - rdslope = dd / dr; - if (rdslope < min) { - min = rdslope; - } - - if (rdslope > max) { - max = rdslope; - } - } /* passno */ - - /* fixed_quality */ - tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); - tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); - } /* cbklno */ - } /* precno */ - } /* bandno */ - } /* resno */ - - maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0) - * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0)) - * ((OPJ_FLOAT64)(tilec->numpix)); - } /* compno */ - - /* index file */ - if(cstr_info) { - opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno]; - tile_info->numpix = tcd_tile->numpix; - tile_info->distotile = tcd_tile->distotile; - tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64)); - } - - for (layno = 0; layno < tcd_tcp->numlayers; layno++) { - OPJ_FLOAT64 lo = min; - OPJ_FLOAT64 hi = max; - opj_bool success = OPJ_FALSE; - OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len; - OPJ_FLOAT64 goodthresh = 0; - OPJ_FLOAT64 stable_thresh = 0; - OPJ_UINT32 i; - OPJ_FLOAT64 distotarget; /* fixed_quality */ - - /* fixed_quality */ - distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10)); - - /* Don't try to find an optimal threshold but rather take everything not included yet, if - -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0) - -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0) - ==> possible to have some lossy layers and the last layer for sure lossless */ - if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) { - opj_t2_v2_t*t2 = t2_create_v2(tcd->image, cp); - OPJ_FLOAT64 thresh = 0; - - if (t2 == 00) { - return OPJ_FALSE; - } - - for (i = 0; i < 128; ++i) { - OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */ - - thresh = (lo + hi) / 2; - - opj_tcd_makelayer(tcd, layno, thresh, 0); - - if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */ - if(cp->m_specific_param.m_enc.m_cinema){ - if (! t2_encode_packets_v2(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) { - - lo = thresh; - continue; - } - else { - distoachieved = layno == 0 ? - tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; - - if (distoachieved < distotarget) { - hi=thresh; - stable_thresh = thresh; - continue; - }else{ - lo=thresh; - } - } - }else{ - distoachieved = (layno == 0) ? - tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); - - if (distoachieved < distotarget) { - hi = thresh; - stable_thresh = thresh; - continue; - } - lo = thresh; - } - } else { - if (! t2_encode_packets_v2(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) - { - /* TODO: what to do with l ??? seek / tell ??? */ - /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */ - lo = thresh; - continue; - } - - hi = thresh; - stable_thresh = thresh; - } - } - - success = OPJ_TRUE; - goodthresh = stable_thresh == 0? thresh : stable_thresh; - - t2_destroy_v2(t2); - } else { - success = OPJ_TRUE; - goodthresh = min; - } - - if (!success) { - return OPJ_FALSE; - } - - if(cstr_info) { /* Threshold for Marcela Index */ - cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh; - } - - opj_tcd_makelayer(tcd, layno, goodthresh, 1); - - /* fixed_quality */ - cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); - } - - return OPJ_TRUE; + OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno; + OPJ_UINT32 passno; + OPJ_FLOAT64 min, max; + OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */ + const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */ + OPJ_FLOAT64 maxSE = 0; + + opj_cp_v2_t *cp = tcd->cp; + opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles; + opj_tcp_v2_t *tcd_tcp = tcd->tcp; + + min = DBL_MAX; + max = 0; + + tcd_tile->numpix = 0; /* fixed_quality */ + + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno]; + tilec->numpix = 0; + + for (resno = 0; resno < tilec->numresolutions; resno++) { + opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno]; + + for (bandno = 0; bandno < res->numbands; bandno++) { + opj_tcd_band_v2_t *band = &res->bands[bandno]; + + for (precno = 0; precno < res->pw * res->ph; precno++) { + opj_tcd_precinct_v2_t *prc = &band->precincts[precno]; + + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno]; + + for (passno = 0; passno < cblk->totalpasses; passno++) { + opj_tcd_pass_v2_t *pass = &cblk->passes[passno]; + OPJ_INT32 dr; + OPJ_FLOAT64 dd, rdslope; + + if (passno == 0) { + dr = pass->rate; + dd = pass->distortiondec; + } else { + dr = pass->rate - cblk->passes[passno - 1].rate; + dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec; + } + + if (dr == 0) { + continue; + } + + rdslope = dd / dr; + if (rdslope < min) { + min = rdslope; + } + + if (rdslope > max) { + max = rdslope; + } + } /* passno */ + + /* fixed_quality */ + tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); + tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); + } /* cbklno */ + } /* precno */ + } /* bandno */ + } /* resno */ + + maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0) + * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0)) + * ((OPJ_FLOAT64)(tilec->numpix)); + } /* compno */ + + /* index file */ + if(cstr_info) { + opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno]; + tile_info->numpix = tcd_tile->numpix; + tile_info->distotile = tcd_tile->distotile; + tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64)); + } + + for (layno = 0; layno < tcd_tcp->numlayers; layno++) { + OPJ_FLOAT64 lo = min; + OPJ_FLOAT64 hi = max; + opj_bool success = OPJ_FALSE; + OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len; + OPJ_FLOAT64 goodthresh = 0; + OPJ_FLOAT64 stable_thresh = 0; + OPJ_UINT32 i; + OPJ_FLOAT64 distotarget; /* fixed_quality */ + + /* fixed_quality */ + distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10)); + + /* Don't try to find an optimal threshold but rather take everything not included yet, if + -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0) + -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0) + ==> possible to have some lossy layers and the last layer for sure lossless */ + if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) { + opj_t2_v2_t*t2 = t2_create_v2(tcd->image, cp); + OPJ_FLOAT64 thresh = 0; + + if (t2 == 00) { + return OPJ_FALSE; + } + + for (i = 0; i < 128; ++i) { + OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */ + + thresh = (lo + hi) / 2; + + opj_tcd_makelayer(tcd, layno, thresh, 0); + + if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */ + if(cp->m_specific_param.m_enc.m_cinema){ + if (! t2_encode_packets_v2(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) { + + lo = thresh; + continue; + } + else { + distoachieved = layno == 0 ? + tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; + + if (distoachieved < distotarget) { + hi=thresh; + stable_thresh = thresh; + continue; + }else{ + lo=thresh; + } + } + }else{ + distoachieved = (layno == 0) ? + tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); + + if (distoachieved < distotarget) { + hi = thresh; + stable_thresh = thresh; + continue; + } + lo = thresh; + } + } else { + if (! t2_encode_packets_v2(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) + { + /* TODO: what to do with l ??? seek / tell ??? */ + /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */ + lo = thresh; + continue; + } + + hi = thresh; + stable_thresh = thresh; + } + } + + success = OPJ_TRUE; + goodthresh = stable_thresh == 0? thresh : stable_thresh; + + t2_destroy_v2(t2); + } else { + success = OPJ_TRUE; + goodthresh = min; + } + + if (!success) { + return OPJ_FALSE; + } + + if(cstr_info) { /* Threshold for Marcela Index */ + cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh; + } + + opj_tcd_makelayer(tcd, layno, goodthresh, 1); + + /* fixed_quality */ + cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); + } + + return OPJ_TRUE; } opj_bool opj_tcd_init( opj_tcd_v2_t *p_tcd, - opj_image_t * p_image, - opj_cp_v2_t * p_cp ) + opj_image_t * p_image, + opj_cp_v2_t * p_cp ) { - OPJ_UINT32 l_tile_comp_size; + OPJ_UINT32 l_tile_comp_size; - p_tcd->image = p_image; - p_tcd->cp = p_cp; + p_tcd->image = p_image; + p_tcd->cp = p_cp; - p_tcd->tcd_image->tiles = (opj_tcd_tile_v2_t *) opj_malloc(sizeof(opj_tcd_tile_v2_t)); - if (! p_tcd->tcd_image->tiles) { - return OPJ_FALSE; - } - memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_v2_t)); + p_tcd->tcd_image->tiles = (opj_tcd_tile_v2_t *) opj_malloc(sizeof(opj_tcd_tile_v2_t)); + if (! p_tcd->tcd_image->tiles) { + return OPJ_FALSE; + } + memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_v2_t)); - l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_v2_t); - p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_v2_t *) opj_malloc(l_tile_comp_size); - if (! p_tcd->tcd_image->tiles->comps ) { - return OPJ_FALSE; - } - memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size); + l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_v2_t); + p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_v2_t *) opj_malloc(l_tile_comp_size); + if (! p_tcd->tcd_image->tiles->comps ) { + return OPJ_FALSE; + } + memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size); - p_tcd->tcd_image->tiles->numcomps = p_image->numcomps; - p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos; + p_tcd->tcd_image->tiles->numcomps = p_image->numcomps; + p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos; - return OPJ_TRUE; + return OPJ_TRUE; } /** Destroy a previously created TCD handle */ void opj_tcd_destroy(opj_tcd_v2_t *tcd) { - if (tcd) { - opj_tcd_free_tile(tcd); - - if (tcd->tcd_image) { - opj_free(tcd->tcd_image); - tcd->tcd_image = 00; - } - opj_free(tcd); - } + if (tcd) { + opj_tcd_free_tile(tcd); + + if (tcd->tcd_image) { + opj_free(tcd->tcd_image); + tcd->tcd_image = 00; + } + opj_free(tcd); + } } /* ----------------------------------------------------------------------- */ /** * Initialize the tile coder and may reuse some meory. - * @param p_tcd TCD handle. - * @param p_image raw image. - * @param p_cp coding parameters. - * @param p_tile_no current tile index to encode. + * @param p_tcd TCD handle. + * @param p_image raw image. + * @param p_cp coding parameters. + * @param p_tile_no current tile index to encode. * * @return true if the encoding values could be set (false otherwise). */ -#define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \ -opj_bool FUNCTION ( opj_tcd_v2_t *p_tcd, \ - OPJ_UINT32 p_tile_no \ - ) \ -{ \ - OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \ - OPJ_UINT32 compno, resno, bandno, precno, cblkno; \ - opj_tcp_v2_t * l_tcp = 00; \ - opj_cp_v2_t * l_cp = 00; \ - opj_tcd_tile_v2_t * l_tile = 00; \ - opj_tccp_t *l_tccp = 00; \ - opj_tcd_tilecomp_v2_t *l_tilec = 00; \ - opj_image_comp_t * l_image_comp = 00; \ - opj_tcd_resolution_v2_t *l_res = 00; \ - opj_tcd_band_v2_t *l_band = 00; \ - opj_stepsize_t * l_step_size = 00; \ - opj_tcd_precinct_v2_t *l_current_precinct = 00; \ - TYPE* l_code_block = 00; \ - opj_image_t *l_image = 00; \ - OPJ_UINT32 p,q; \ - OPJ_UINT32 l_level_no; \ - OPJ_UINT32 l_pdx, l_pdy; \ - OPJ_UINT32 l_gain; \ - OPJ_INT32 l_x0b, l_y0b; \ - /* extent of precincts , top left, bottom right**/ \ - OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \ - /* number of precinct for a resolution */ \ - OPJ_UINT32 l_nb_precincts; \ - /* room needed to store l_nb_precinct precinct for a resolution */ \ - OPJ_UINT32 l_nb_precinct_size; \ - /* number of code blocks for a precinct*/ \ - OPJ_UINT32 l_nb_code_blocks; \ - /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \ - OPJ_UINT32 l_nb_code_blocks_size; \ - /* size of data for a tile */ \ - OPJ_UINT32 l_data_size; \ - \ - l_cp = p_tcd->cp; \ - l_tcp = &(l_cp->tcps[p_tile_no]); \ - l_tile = p_tcd->tcd_image->tiles; \ - l_tccp = l_tcp->tccps; \ - l_tilec = l_tile->comps; \ - l_image = p_tcd->image; \ - l_image_comp = p_tcd->image->comps; \ - \ - p = p_tile_no % l_cp->tw; /* tile coordinates */ \ - q = p_tile_no / l_cp->tw; \ - /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \ - \ - /* 4 borders of the tile rescale on the image if necessary */ \ - l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0); \ - l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0); \ - l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1); \ - l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1); \ - /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/\ - \ - /*tile->numcomps = image->numcomps; */ \ - for(compno = 0; compno < l_tile->numcomps; ++compno) { \ - /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \ - \ - /* border of each l_tile component (global) */ \ - l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx); \ - l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy); \ - l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx); \ - l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy); \ - /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/\ - \ - l_data_size = (l_tilec->x1 - l_tilec->x0) \ - * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 ); \ - l_tilec->numresolutions = l_tccp->numresolutions; \ - if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \ - l_tilec->minimum_num_resolutions = 1; \ - } \ - else { \ - l_tilec->minimum_num_resolutions = l_tccp->numresolutions \ - - l_cp->m_specific_param.m_dec.m_reduce; \ - } \ - \ - if (l_tilec->data == 00) { \ - l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \ - if (! l_tilec->data ) { \ - return OPJ_FALSE; \ - } \ - /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \ - \ - l_tilec->data_size = l_data_size; \ - } \ - else if (l_data_size > l_tilec->data_size) { \ - l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \ - if (! l_tilec->data) { \ - return OPJ_FALSE; \ - } \ - /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \ - l_tilec->data_size = l_data_size; \ - } \ - \ - l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_v2_t); \ - \ - if (l_tilec->resolutions == 00) { \ - l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_malloc(l_data_size); \ - if (! l_tilec->resolutions ) { \ - return OPJ_FALSE; \ - } \ - /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_v2_t): %d\n",l_data_size);*/ \ - l_tilec->resolutions_size = l_data_size; \ - memset(l_tilec->resolutions,0,l_data_size); \ - } \ - else if (l_data_size > l_tilec->resolutions_size) { \ - l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_realloc(l_tilec->resolutions, l_data_size); \ - if (! l_tilec->resolutions) { \ - return OPJ_FALSE; \ - } \ - /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \ - memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \ - l_tilec->resolutions_size = l_data_size; \ - } \ - \ - l_level_no = l_tilec->numresolutions - 1; \ - l_res = l_tilec->resolutions; \ - l_step_size = l_tccp->stepsizes; \ - if (l_tccp->qmfbid == 0) { \ - l_gain_ptr = &opj_dwt_getgain_real; \ - } \ - else { \ - l_gain_ptr = &opj_dwt_getgain; \ - } \ - /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \ - \ - for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \ - /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \ - OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; \ - OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \ - OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \ - \ - /* border for each resolution level (global) */ \ - l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \ - l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \ - l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \ - l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \ - /*fprintf(stderr, "\t\t\tres_x0= %d, res_y0 =%d, res_x1=%d, res_y1=%d\n", l_res->x0, l_res->y0, l_res->x1, l_res->y1);*/ \ - /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \ - l_pdx = l_tccp->prcw[resno]; \ - l_pdy = l_tccp->prch[resno]; \ - /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \ - /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \ - l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx; \ - l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy; \ - l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx; \ - l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy; \ - /*fprintf(stderr, "\t\t\tprc_x_start=%d, prc_y_start=%d, br_prc_x_end=%d, br_prc_y_end=%d \n", l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end ,l_br_prc_y_end );*/ \ - \ - l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \ - l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \ - /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \ - \ - l_nb_precincts = l_res->pw * l_res->ph; \ - l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_v2_t); \ - if (resno == 0) { \ - tlcbgxstart = l_tl_prc_x_start; \ - tlcbgystart = l_tl_prc_y_start; \ - brcbgxend = l_br_prc_x_end; \ - brcbgyend = l_br_prc_y_end; \ - cbgwidthexpn = l_pdx; \ - cbgheightexpn = l_pdy; \ - l_res->numbands = 1; \ - } \ - else { \ - tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1); \ - tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1); \ - brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1); \ - brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1); \ - cbgwidthexpn = l_pdx - 1; \ - cbgheightexpn = l_pdy - 1; \ - l_res->numbands = 3; \ - } \ - \ - cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn); \ - cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn); \ - l_band = l_res->bands; \ - \ - for (bandno = 0; bandno < l_res->numbands; ++bandno) { \ - OPJ_INT32 numbps;\ - /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \ - \ - if (resno == 0) { \ - l_band->bandno = 0 ; \ - l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \ - l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \ - l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \ - l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \ - } \ - else { \ - l_band->bandno = bandno + 1; \ - /* x0b = 1 if bandno = 1 or 3 */ \ - l_x0b = l_band->bandno&1; \ - /* y0b = 1 if bandno = 2 or 3 */ \ - l_y0b = (l_band->bandno)>>1; \ - /* l_band border (global) */ \ - l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1); \ - l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1); \ - l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1); \ - l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1); \ - } \ - \ - /** avoid an if with storing function pointer */ \ - l_gain = (*l_gain_ptr) (l_band->bandno); \ - numbps = l_image_comp->prec + l_gain; \ - l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;\ - l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1; /* WHY -1 ? */\ - \ - if (! l_band->precincts) { \ - l_band->precincts = (opj_tcd_precinct_v2_t *) opj_malloc( /*3 * */ l_nb_precinct_size);\ - if (! l_band->precincts) { \ - return OPJ_FALSE; \ - } \ - /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_v2_t): %d\n",l_nb_precinct_size); */ \ - memset(l_band->precincts,0,l_nb_precinct_size); \ - l_band->precincts_data_size = l_nb_precinct_size; \ - } \ - else if (l_band->precincts_data_size < l_nb_precinct_size) { \ - \ - l_band->precincts = (opj_tcd_precinct_v2_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\ - if (! l_band->precincts) { \ - return OPJ_FALSE; \ - } \ - /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_v2_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/\ - memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);\ - l_band->precincts_data_size = l_nb_precinct_size; \ - } \ - \ - l_current_precinct = l_band->precincts; \ - for (precno = 0; precno < l_nb_precincts; ++precno) { \ - OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \ - OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \ - OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn); \ - OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \ - OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \ - /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/\ - /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/\ - \ - /* precinct size (global) */ \ - /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \ - \ - l_current_precinct->x0 = int_max(cbgxstart, l_band->x0); \ - l_current_precinct->y0 = int_max(cbgystart, l_band->y0); \ - l_current_precinct->x1 = int_min(cbgxend, l_band->x1); \ - l_current_precinct->y1 = int_min(cbgyend, l_band->y1); \ - /*fprintf(stderr, "\t prc_x0=%d; prc_y0=%d, prc_x1=%d; prc_y1=%d\n",l_current_precinct->x0, l_current_precinct->y0 ,l_current_precinct->x1, l_current_precinct->y1);*/ \ - \ - tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn; \ - /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \ - tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn; \ - /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \ - brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn; \ - /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \ - brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \ - /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \ - l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; \ - l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \ - \ - l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \ - /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \ - l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE); \ - \ - if (! l_current_precinct->cblks.ELEMENT) { \ - l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\ - if (! l_current_precinct->cblks.ELEMENT ) { \ - return OPJ_FALSE; \ - } \ - /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): %d\n",l_nb_code_blocks_size);*/ \ - \ - memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\ - \ - l_current_precinct->block_size = l_nb_code_blocks_size; \ - } \ - else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \ - l_current_precinct->cblks.ELEMENT = (TYPE*) \ - opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \ - if (! l_current_precinct->cblks.ELEMENT ) { \ - return OPJ_FALSE; \ - } \ - /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size); */ \ - \ - memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \ - ,0 \ - ,l_nb_code_blocks_size - l_current_precinct->block_size); \ - \ - l_current_precinct->block_size = l_nb_code_blocks_size; \ - } \ - \ - if (! l_current_precinct->incltree) { \ - l_current_precinct->incltree = tgt_create_v2(l_current_precinct->cw, \ - l_current_precinct->ch); \ - } \ - else{ \ - l_current_precinct->incltree = tgt_init(l_current_precinct->incltree, \ - l_current_precinct->cw, \ - l_current_precinct->ch); \ - } \ - \ - if (! l_current_precinct->incltree) { \ - fprintf(stderr, "WARNING: No incltree created.\n");\ - /*return OPJ_FALSE;*/ \ - } \ - \ - if (! l_current_precinct->imsbtree) { \ - l_current_precinct->imsbtree = tgt_create_v2( \ - l_current_precinct->cw, \ - l_current_precinct->ch);\ - } \ - else { \ - l_current_precinct->imsbtree = tgt_init( \ - l_current_precinct->imsbtree, \ - l_current_precinct->cw, \ - l_current_precinct->ch); \ - } \ - \ - if (! l_current_precinct->imsbtree) { \ - fprintf(stderr, "WARNING: No imsbtree created.\n");\ - /*return OPJ_FALSE;*/ \ - } \ - \ - l_code_block = l_current_precinct->cblks.ELEMENT; \ - \ - for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \ - OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \ - OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \ - OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \ - OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);\ - \ - /* code-block size (global) */ \ - l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0); \ - l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0); \ - l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1); \ - l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1); \ - \ - if (! FUNCTION_ELEMENT(l_code_block)) { \ - return OPJ_FALSE; \ - } \ - ++l_code_block; \ - } \ - ++l_current_precinct; \ - } /* precno */ \ - ++l_band; \ - ++l_step_size; \ - } /* bandno */ \ - ++l_res; \ - --l_level_no; \ - } /* resno */ \ - ++l_tccp; \ - ++l_tilec; \ - ++l_image_comp; \ - } /* compno */ \ - return OPJ_TRUE; \ -} \ +#define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \ + opj_bool FUNCTION ( opj_tcd_v2_t *p_tcd, \ + OPJ_UINT32 p_tile_no \ + ) \ +{ \ + OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \ + OPJ_UINT32 compno, resno, bandno, precno, cblkno; \ + opj_tcp_v2_t * l_tcp = 00; \ + opj_cp_v2_t * l_cp = 00; \ + opj_tcd_tile_v2_t * l_tile = 00; \ + opj_tccp_t *l_tccp = 00; \ + opj_tcd_tilecomp_v2_t *l_tilec = 00; \ + opj_image_comp_t * l_image_comp = 00; \ + opj_tcd_resolution_v2_t *l_res = 00; \ + opj_tcd_band_v2_t *l_band = 00; \ + opj_stepsize_t * l_step_size = 00; \ + opj_tcd_precinct_v2_t *l_current_precinct = 00; \ + TYPE* l_code_block = 00; \ + opj_image_t *l_image = 00; \ + OPJ_UINT32 p,q; \ + OPJ_UINT32 l_level_no; \ + OPJ_UINT32 l_pdx, l_pdy; \ + OPJ_UINT32 l_gain; \ + OPJ_INT32 l_x0b, l_y0b; \ + /* extent of precincts , top left, bottom right**/ \ + OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \ + /* number of precinct for a resolution */ \ + OPJ_UINT32 l_nb_precincts; \ + /* room needed to store l_nb_precinct precinct for a resolution */ \ + OPJ_UINT32 l_nb_precinct_size; \ + /* number of code blocks for a precinct*/ \ + OPJ_UINT32 l_nb_code_blocks; \ + /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \ + OPJ_UINT32 l_nb_code_blocks_size; \ + /* size of data for a tile */ \ + OPJ_UINT32 l_data_size; \ + \ + l_cp = p_tcd->cp; \ + l_tcp = &(l_cp->tcps[p_tile_no]); \ + l_tile = p_tcd->tcd_image->tiles; \ + l_tccp = l_tcp->tccps; \ + l_tilec = l_tile->comps; \ + l_image = p_tcd->image; \ + l_image_comp = p_tcd->image->comps; \ + \ + p = p_tile_no % l_cp->tw; /* tile coordinates */ \ + q = p_tile_no / l_cp->tw; \ + /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \ + \ + /* 4 borders of the tile rescale on the image if necessary */ \ + l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0); \ + l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0); \ + l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1); \ + l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1); \ + /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/ \ + \ + /*tile->numcomps = image->numcomps; */ \ + for(compno = 0; compno < l_tile->numcomps; ++compno) { \ + /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \ + \ + /* border of each l_tile component (global) */ \ + l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx); \ + l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy); \ + l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx); \ + l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy); \ + /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/ \ + \ + l_data_size = (l_tilec->x1 - l_tilec->x0) \ + * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 ); \ + l_tilec->numresolutions = l_tccp->numresolutions; \ + if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \ + l_tilec->minimum_num_resolutions = 1; \ + } \ + else { \ + l_tilec->minimum_num_resolutions = l_tccp->numresolutions \ + - l_cp->m_specific_param.m_dec.m_reduce; \ + } \ + \ + if (l_tilec->data == 00) { \ + l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \ + if (! l_tilec->data ) { \ + return OPJ_FALSE; \ + } \ + /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \ + \ + l_tilec->data_size = l_data_size; \ + } \ + else if (l_data_size > l_tilec->data_size) { \ + OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \ + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle tile data\n"); */ \ + fprintf(stderr, "Not enough memory to handle tile data\n"); \ + if (! new_data) { \ + opj_free(l_tilec->data); \ + l_tilec->data = NULL; \ + l_tilec->data_size = 0; \ + return OPJ_FALSE; \ + } \ + l_tilec->data = new_data; \ + /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \ + l_tilec->data_size = l_data_size; \ + } \ + \ + l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_v2_t); \ + \ + if (l_tilec->resolutions == 00) { \ + l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_malloc(l_data_size); \ + if (! l_tilec->resolutions ) { \ + return OPJ_FALSE; \ + } \ + /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_v2_t): %d\n",l_data_size);*/ \ + l_tilec->resolutions_size = l_data_size; \ + memset(l_tilec->resolutions,0,l_data_size); \ + } \ + else if (l_data_size > l_tilec->resolutions_size) { \ + opj_tcd_resolution_v2_t* new_resolutions = (opj_tcd_resolution_v2_t *) opj_realloc(l_tilec->resolutions, l_data_size); \ + if (! new_resolutions) { \ + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to tile resolutions\n"); */ \ + fprintf(stderr, "Not enough memory to tile resolutions\n"); \ + opj_free(l_tilec->resolutions); \ + l_tilec->resolutions = NULL; \ + l_tilec->resolutions_size = 0; \ + return OPJ_FALSE; \ + } \ + l_tilec->resolutions = new_resolutions; \ + /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \ + memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \ + l_tilec->resolutions_size = l_data_size; \ + } \ + \ + l_level_no = l_tilec->numresolutions - 1; \ + l_res = l_tilec->resolutions; \ + l_step_size = l_tccp->stepsizes; \ + if (l_tccp->qmfbid == 0) { \ + l_gain_ptr = &opj_dwt_getgain_real; \ + } \ + else { \ + l_gain_ptr = &opj_dwt_getgain; \ + } \ + /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \ + \ + for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \ + /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \ + OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; \ + OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \ + OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \ + \ + /* border for each resolution level (global) */ \ + l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \ + l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \ + l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \ + l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \ + /*fprintf(stderr, "\t\t\tres_x0= %d, res_y0 =%d, res_x1=%d, res_y1=%d\n", l_res->x0, l_res->y0, l_res->x1, l_res->y1);*/ \ + /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \ + l_pdx = l_tccp->prcw[resno]; \ + l_pdy = l_tccp->prch[resno]; \ + /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \ + /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \ + l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx; \ + l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy; \ + l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx; \ + l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy; \ + /*fprintf(stderr, "\t\t\tprc_x_start=%d, prc_y_start=%d, br_prc_x_end=%d, br_prc_y_end=%d \n", l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end ,l_br_prc_y_end );*/ \ + \ + l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \ + l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \ + /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \ + \ + l_nb_precincts = l_res->pw * l_res->ph; \ + l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_v2_t); \ + if (resno == 0) { \ + tlcbgxstart = l_tl_prc_x_start; \ + tlcbgystart = l_tl_prc_y_start; \ + brcbgxend = l_br_prc_x_end; \ + brcbgyend = l_br_prc_y_end; \ + cbgwidthexpn = l_pdx; \ + cbgheightexpn = l_pdy; \ + l_res->numbands = 1; \ + } \ + else { \ + tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1); \ + tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1); \ + brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1); \ + brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1); \ + cbgwidthexpn = l_pdx - 1; \ + cbgheightexpn = l_pdy - 1; \ + l_res->numbands = 3; \ + } \ + \ + cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn); \ + cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn); \ + l_band = l_res->bands; \ + \ + for (bandno = 0; bandno < l_res->numbands; ++bandno) { \ + OPJ_INT32 numbps; \ + /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \ + \ + if (resno == 0) { \ + l_band->bandno = 0 ; \ + l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \ + l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \ + l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \ + l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \ + } \ + else { \ + l_band->bandno = bandno + 1; \ + /* x0b = 1 if bandno = 1 or 3 */ \ + l_x0b = l_band->bandno&1; \ + /* y0b = 1 if bandno = 2 or 3 */ \ + l_y0b = (l_band->bandno)>>1; \ + /* l_band border (global) */ \ + l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1); \ + l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1); \ + l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1); \ + l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1); \ + } \ + \ + /** avoid an if with storing function pointer */ \ + l_gain = (*l_gain_ptr) (l_band->bandno); \ + numbps = l_image_comp->prec + l_gain; \ + l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION; \ + l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1; /* WHY -1 ? */ \ + \ + if (! l_band->precincts) { \ + l_band->precincts = (opj_tcd_precinct_v2_t *) opj_malloc( /*3 * */ l_nb_precinct_size); \ + if (! l_band->precincts) { \ + return OPJ_FALSE; \ + } \ + /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_v2_t): %d\n",l_nb_precinct_size); */ \ + memset(l_band->precincts,0,l_nb_precinct_size); \ + l_band->precincts_data_size = l_nb_precinct_size; \ + } \ + else if (l_band->precincts_data_size < l_nb_precinct_size) { \ + \ + opj_tcd_precinct_v2_t * new_precincts = (opj_tcd_precinct_v2_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size); \ + if (! new_precincts) { \ + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n"); */ \ + fprintf(stderr, "Not enough memory to handle band precints\n"); \ + opj_free(l_band->precincts); \ + l_band->precincts = NULL; \ + l_band->precincts_data_size = 0; \ + return OPJ_FALSE; \ + } \ + l_band->precincts = new_precincts; \ + /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_v2_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/ \ + memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size); \ + l_band->precincts_data_size = l_nb_precinct_size; \ + } \ + \ + l_current_precinct = l_band->precincts; \ + for (precno = 0; precno < l_nb_precincts; ++precno) { \ + OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \ + OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \ + OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn); \ + OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \ + OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \ + /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/ \ + /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/ \ + \ + /* precinct size (global) */ \ + /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \ + \ + l_current_precinct->x0 = int_max(cbgxstart, l_band->x0); \ + l_current_precinct->y0 = int_max(cbgystart, l_band->y0); \ + l_current_precinct->x1 = int_min(cbgxend, l_band->x1); \ + l_current_precinct->y1 = int_min(cbgyend, l_band->y1); \ + /*fprintf(stderr, "\t prc_x0=%d; prc_y0=%d, prc_x1=%d; prc_y1=%d\n",l_current_precinct->x0, l_current_precinct->y0 ,l_current_precinct->x1, l_current_precinct->y1);*/ \ + \ + tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn; \ + /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \ + tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn; \ + /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \ + brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn; \ + /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \ + brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \ + /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \ + l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; \ + l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \ + \ + l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \ + /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \ + l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE); \ + \ + if (! l_current_precinct->cblks.ELEMENT) { \ + l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size); \ + if (! l_current_precinct->cblks.ELEMENT ) { \ + return OPJ_FALSE; \ + } \ + /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): %d\n",l_nb_code_blocks_size);*/ \ + \ + memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size); \ + \ + l_current_precinct->block_size = l_nb_code_blocks_size; \ + } \ + else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \ + TYPE *new_ELEMENT = (TYPE*) opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \ + if (! new_ELEMENT) { \ + opj_free(l_current_precinct->cblks.ELEMENT); \ + l_current_precinct->cblks.ELEMENT = NULL; \ + l_current_precinct->block_size = 0; \ + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n"); */ \ + fprintf(stderr, "Not enough memory for current precinct codeblock element\n"); \ + return OPJ_FALSE; \ + } \ + l_current_precinct->cblks.ELEMENT = new_ELEMENT; \ + /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size); */\ + \ + memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \ + ,0 \ + ,l_nb_code_blocks_size - l_current_precinct->block_size); \ + \ + l_current_precinct->block_size = l_nb_code_blocks_size; \ + } \ + \ + if (! l_current_precinct->incltree) { \ + l_current_precinct->incltree = tgt_create_v2(l_current_precinct->cw, \ + l_current_precinct->ch); \ + } \ + else{ \ + l_current_precinct->incltree = tgt_init(l_current_precinct->incltree, \ + l_current_precinct->cw, \ + l_current_precinct->ch); \ + } \ + \ + if (! l_current_precinct->incltree) { \ + fprintf(stderr, "WARNING: No incltree created.\n"); \ + /*return OPJ_FALSE;*/ \ + } \ + \ + if (! l_current_precinct->imsbtree) { \ + l_current_precinct->imsbtree = tgt_create_v2( \ + l_current_precinct->cw, \ + l_current_precinct->ch); \ + } \ + else { \ + l_current_precinct->imsbtree = tgt_init( \ + l_current_precinct->imsbtree, \ + l_current_precinct->cw, \ + l_current_precinct->ch); \ + } \ + \ + if (! l_current_precinct->imsbtree) { \ + fprintf(stderr, "WARNING: No imsbtree created.\n"); \ + /*return OPJ_FALSE;*/ \ + } \ + \ + l_code_block = l_current_precinct->cblks.ELEMENT; \ + \ + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \ + OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \ + OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \ + OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \ + OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn); \ + \ + /* code-block size (global) */ \ + l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0); \ + l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0); \ + l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1); \ + l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1); \ + \ + if (! FUNCTION_ELEMENT(l_code_block)) { \ + return OPJ_FALSE; \ + } \ + ++l_code_block; \ + } \ + ++l_current_precinct; \ + } /* precno */ \ + ++l_band; \ + ++l_step_size; \ + } /* bandno */ \ + ++l_res; \ + --l_level_no; \ + } /* resno */ \ + ++l_tccp; \ + ++l_tilec; \ + ++l_image_comp; \ + } /* compno */ \ + return OPJ_TRUE; \ +} \ MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_v2_t, 1.f, enc, opj_tcd_code_block_enc_allocate) @@ -981,32 +1004,32 @@ MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_v2_t, 0.5f, dec, o */ opj_bool opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block) { - if (! p_code_block->data) { + if (! p_code_block->data) { - p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1); - if(! p_code_block->data) { - return OPJ_FALSE; - } + p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1); + if(! p_code_block->data) { + return OPJ_FALSE; + } - p_code_block->data[0] = 0; - p_code_block->data+=1; + p_code_block->data[0] = 0; + p_code_block->data+=1; - /* no memset since data */ - p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t)); - if (! p_code_block->layers) { - return OPJ_FALSE; - } + /* no memset since data */ + p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t)); + if (! p_code_block->layers) { + return OPJ_FALSE; + } - p_code_block->passes = (opj_tcd_pass_v2_t*) opj_malloc(100 * sizeof(opj_tcd_pass_v2_t)); - if (! p_code_block->passes) { - return OPJ_FALSE; - } - } + p_code_block->passes = (opj_tcd_pass_v2_t*) opj_malloc(100 * sizeof(opj_tcd_pass_v2_t)); + if (! p_code_block->passes) { + return OPJ_FALSE; + } + } - memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t)); - memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_v2_t)); + memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t)); + memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_v2_t)); - return OPJ_TRUE; + return OPJ_TRUE; } /** @@ -1014,148 +1037,148 @@ opj_bool opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block) */ opj_bool opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block) { - OPJ_UINT32 l_seg_size; - - if (! p_code_block->data) { - - p_code_block->data = (OPJ_BYTE*) opj_malloc(8192); - if (! p_code_block->data) { - return OPJ_FALSE; - } - /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/ - - l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t); - p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size); - if (! p_code_block->segs) { - return OPJ_FALSE; - } - memset(p_code_block->segs,0,l_seg_size); - /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/ - - p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS; - /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/ - } - /* TODO */ - /*p_code_block->numsegs = 0; */ - - return OPJ_TRUE; + OPJ_UINT32 l_seg_size; + + if (! p_code_block->data) { + + p_code_block->data = (OPJ_BYTE*) opj_malloc(8192); + if (! p_code_block->data) { + return OPJ_FALSE; + } + /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/ + + l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t); + p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size); + if (! p_code_block->segs) { + return OPJ_FALSE; + } + memset(p_code_block->segs,0,l_seg_size); + /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/ + + p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS; + /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/ + } + /* TODO */ + /*p_code_block->numsegs = 0; */ + + return OPJ_TRUE; } OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_v2_t *p_tcd ) { - OPJ_UINT32 i; - OPJ_UINT32 l_data_size = 0; - opj_image_comp_t * l_img_comp = 00; - opj_tcd_tilecomp_v2_t * l_tile_comp = 00; - opj_tcd_resolution_v2_t * l_res = 00; - OPJ_UINT32 l_size_comp, l_remaining; - - l_tile_comp = p_tcd->tcd_image->tiles->comps; - l_img_comp = p_tcd->image->comps; - - for (i=0;iimage->numcomps;++i) { - l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ - l_remaining = l_img_comp->prec & 7; /* (%8) */ - - if(l_remaining) { - ++l_size_comp; - } - - if (l_size_comp == 3) { - l_size_comp = 4; - } - - l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1; - l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0); - ++l_img_comp; - ++l_tile_comp; - } - - return l_data_size; + OPJ_UINT32 i; + OPJ_UINT32 l_data_size = 0; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tilecomp_v2_t * l_tile_comp = 00; + opj_tcd_resolution_v2_t * l_res = 00; + OPJ_UINT32 l_size_comp, l_remaining; + + l_tile_comp = p_tcd->tcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + + for (i=0;iimage->numcomps;++i) { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + + if(l_remaining) { + ++l_size_comp; + } + + if (l_size_comp == 3) { + l_size_comp = 4; + } + + l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1; + l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0); + ++l_img_comp; + ++l_tile_comp; + } + + return l_data_size; } opj_bool opj_tcd_encode_tile( opj_tcd_v2_t *p_tcd, - OPJ_UINT32 p_tile_no, - OPJ_BYTE *p_dest, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 p_max_length, - opj_codestream_info_t *p_cstr_info) + OPJ_UINT32 p_tile_no, + OPJ_BYTE *p_dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_length, + opj_codestream_info_t *p_cstr_info) { - if (p_tcd->cur_tp_num == 0) { - - p_tcd->tcd_tileno = p_tile_no; - p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no]; - - /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ - if(p_cstr_info) { - OPJ_UINT32 l_num_packs = 0; - OPJ_UINT32 i; - opj_tcd_tilecomp_v2_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */ - opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */ - - for (i = 0; i < l_tilec_idx->numresolutions; i++) { - opj_tcd_resolution_v2_t *l_res_idx = &l_tilec_idx->resolutions[i]; - - p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw; - p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph; - - l_num_packs += l_res_idx->pw * l_res_idx->ph; - p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i]; - p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i]; - } - p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc(p_cstr_info->numcomps * p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t)); - } - /* << INDEX */ - - /* FIXME _ProfStart(PGROUP_DC_SHIFT); */ - /*---------------TILE-------------------*/ - if (! opj_tcd_dc_level_shift_encode(p_tcd)) { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_DC_SHIFT); */ - - /* FIXME _ProfStart(PGROUP_MCT); */ - if (! opj_tcd_mct_encode(p_tcd)) { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_MCT); */ - - /* FIXME _ProfStart(PGROUP_DWT); */ - if (! opj_tcd_dwt_encode(p_tcd)) { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_DWT); */ - - /* FIXME _ProfStart(PGROUP_T1); */ - if (! opj_tcd_t1_encode(p_tcd)) { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_T1); */ - - /* FIXME _ProfStart(PGROUP_RATE); */ - if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_RATE); */ - - } - /*--------------TIER2------------------*/ - - /* INDEX */ - if (p_cstr_info) { - p_cstr_info->index_write = 1; - } - /* FIXME _ProfStart(PGROUP_T2); */ - - if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_T2); */ - - /*---------------CLEAN-------------------*/ - - return OPJ_TRUE; + if (p_tcd->cur_tp_num == 0) { + + p_tcd->tcd_tileno = p_tile_no; + p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no]; + + /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ + if(p_cstr_info) { + OPJ_UINT32 l_num_packs = 0; + OPJ_UINT32 i; + opj_tcd_tilecomp_v2_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */ + opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */ + + for (i = 0; i < l_tilec_idx->numresolutions; i++) { + opj_tcd_resolution_v2_t *l_res_idx = &l_tilec_idx->resolutions[i]; + + p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw; + p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph; + + l_num_packs += l_res_idx->pw * l_res_idx->ph; + p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i]; + p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i]; + } + p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc(p_cstr_info->numcomps * p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t)); + } + /* << INDEX */ + + /* FIXME _ProfStart(PGROUP_DC_SHIFT); */ + /*---------------TILE-------------------*/ + if (! opj_tcd_dc_level_shift_encode(p_tcd)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_DC_SHIFT); */ + + /* FIXME _ProfStart(PGROUP_MCT); */ + if (! opj_tcd_mct_encode(p_tcd)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_MCT); */ + + /* FIXME _ProfStart(PGROUP_DWT); */ + if (! opj_tcd_dwt_encode(p_tcd)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_DWT); */ + + /* FIXME _ProfStart(PGROUP_T1); */ + if (! opj_tcd_t1_encode(p_tcd)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_T1); */ + + /* FIXME _ProfStart(PGROUP_RATE); */ + if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_RATE); */ + + } + /*--------------TIER2------------------*/ + + /* INDEX */ + if (p_cstr_info) { + p_cstr_info->index_write = 1; + } + /* FIXME _ProfStart(PGROUP_T2); */ + + if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_T2); */ + + /*---------------CLEAN-------------------*/ + + return OPJ_TRUE; } opj_bool opj_tcd_decode_tile( opj_tcd_v2_t *p_tcd, @@ -1165,83 +1188,83 @@ opj_bool opj_tcd_decode_tile( opj_tcd_v2_t *p_tcd, opj_codestream_index_t *p_cstr_index ) { - OPJ_UINT32 l_data_read; - p_tcd->tcd_tileno = p_tile_no; - p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]); + OPJ_UINT32 l_data_read; + p_tcd->tcd_tileno = p_tile_no; + p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]); #ifdef TODO_MSD /* FIXME */ - /* INDEX >> */ - if(p_cstr_info) { - OPJ_UINT32 resno, compno, numprec = 0; - for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) { - opj_tcp_v2_t *tcp = &p_tcd->cp->tcps[0]; - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_tcd_tilecomp_v2_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno]; - for (resno = 0; resno < tilec_idx->numresolutions; resno++) { - opj_tcd_resolution_v2_t *res_idx = &tilec_idx->resolutions[resno]; - p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw; - p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph; - numprec += res_idx->pw * res_idx->ph; - p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno]; - p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno]; - } - } - p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t)); - p_cstr_info->packno = 0; - } - /* << INDEX */ + /* INDEX >> */ + if(p_cstr_info) { + OPJ_UINT32 resno, compno, numprec = 0; + for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) { + opj_tcp_v2_t *tcp = &p_tcd->cp->tcps[0]; + opj_tccp_t *tccp = &tcp->tccps[compno]; + opj_tcd_tilecomp_v2_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno]; + for (resno = 0; resno < tilec_idx->numresolutions; resno++) { + opj_tcd_resolution_v2_t *res_idx = &tilec_idx->resolutions[resno]; + p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw; + p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph; + numprec += res_idx->pw * res_idx->ph; + p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno]; + p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno]; + } + } + p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t)); + p_cstr_info->packno = 0; + } + /* << INDEX */ #endif - /*--------------TIER2------------------*/ - /* FIXME _ProfStart(PGROUP_T2); */ - l_data_read = 0; - if - (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index)) - { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_T2); */ - - /*------------------TIER1-----------------*/ - - /* FIXME _ProfStart(PGROUP_T1); */ - if - (! opj_tcd_t1_decode(p_tcd)) - { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_T1); */ - - /*----------------DWT---------------------*/ - - /* FIXME _ProfStart(PGROUP_DWT); */ - if - (! opj_tcd_dwt_decode(p_tcd)) - { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_DWT); */ - - /*----------------MCT-------------------*/ - /* FIXME _ProfStart(PGROUP_MCT); */ - if - (! opj_tcd_mct_decode(p_tcd)) - { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_MCT); */ - - /* FIXME _ProfStart(PGROUP_DC_SHIFT); */ - if - (! opj_tcd_dc_level_shift_decode(p_tcd)) - { - return OPJ_FALSE; - } - /* FIXME _ProfStop(PGROUP_DC_SHIFT); */ - - - /*---------------TILE-------------------*/ - return OPJ_TRUE; + /*--------------TIER2------------------*/ + /* FIXME _ProfStart(PGROUP_T2); */ + l_data_read = 0; + if + (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index)) + { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_T2); */ + + /*------------------TIER1-----------------*/ + + /* FIXME _ProfStart(PGROUP_T1); */ + if + (! opj_tcd_t1_decode(p_tcd)) + { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_T1); */ + + /*----------------DWT---------------------*/ + + /* FIXME _ProfStart(PGROUP_DWT); */ + if + (! opj_tcd_dwt_decode(p_tcd)) + { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_DWT); */ + + /*----------------MCT-------------------*/ + /* FIXME _ProfStart(PGROUP_MCT); */ + if + (! opj_tcd_mct_decode(p_tcd)) + { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_MCT); */ + + /* FIXME _ProfStart(PGROUP_DC_SHIFT); */ + if + (! opj_tcd_dc_level_shift_decode(p_tcd)) + { + return OPJ_FALSE; + } + /* FIXME _ProfStop(PGROUP_DC_SHIFT); */ + + + /*---------------TILE-------------------*/ + return OPJ_TRUE; } opj_bool opj_tcd_update_tile_data ( opj_tcd_v2_t *p_tcd, @@ -1249,111 +1272,111 @@ opj_bool opj_tcd_update_tile_data ( opj_tcd_v2_t *p_tcd, OPJ_UINT32 p_dest_length ) { - OPJ_UINT32 i,j,k,l_data_size = 0; - opj_image_comp_t * l_img_comp = 00; - opj_tcd_tilecomp_v2_t * l_tilec = 00; - opj_tcd_resolution_v2_t * l_res; - OPJ_UINT32 l_size_comp, l_remaining; - OPJ_UINT32 l_stride, l_width,l_height; - - l_data_size = opj_tcd_get_decoded_tile_size(p_tcd); - if (l_data_size > p_dest_length) { - return OPJ_FALSE; - } - - l_tilec = p_tcd->tcd_image->tiles->comps; - l_img_comp = p_tcd->image->comps; - - for (i=0;iimage->numcomps;++i) { - l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ - l_remaining = l_img_comp->prec & 7; /* (%8) */ - l_res = l_tilec->resolutions + l_img_comp->resno_decoded; - l_width = (l_res->x1 - l_res->x0); - l_height = (l_res->y1 - l_res->y0); - l_stride = (l_tilec->x1 - l_tilec->x0) - l_width; - - if (l_remaining) { - ++l_size_comp; - } - - if (l_size_comp == 3) { - l_size_comp = 4; - } - - switch (l_size_comp) - { - case 1: - { - OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest; - const OPJ_INT32 * l_src_ptr = l_tilec->data; - - if (l_img_comp->sgnd) { - for (j=0;jdata; - OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest; - - if (l_img_comp->sgnd) { - for (j=0;jdata; - - for (j=0;j p_dest_length) { + return OPJ_FALSE; + } + + l_tilec = p_tcd->tcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + + for (i=0;iimage->numcomps;++i) { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + l_res = l_tilec->resolutions + l_img_comp->resno_decoded; + l_width = (l_res->x1 - l_res->x0); + l_height = (l_res->y1 - l_res->y0); + l_stride = (l_tilec->x1 - l_tilec->x0) - l_width; + + if (l_remaining) { + ++l_size_comp; + } + + if (l_size_comp == 3) { + l_size_comp = 4; + } + + switch (l_size_comp) + { + case 1: + { + OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest; + const OPJ_INT32 * l_src_ptr = l_tilec->data; + + if (l_img_comp->sgnd) { + for (j=0;jdata; + OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest; + + if (l_img_comp->sgnd) { + for (j=0;jdata; + + for (j=0;jtcd_image) { - return; - } - - if (p_tcd->m_is_decoder) { - l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate; - } - else { - l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate; - } - - l_tile = p_tcd->tcd_image->tiles; - if (! l_tile) { - return; - } - - l_tile_comp = l_tile->comps; - - for (compno = 0; compno < l_tile->numcomps; ++compno) { - l_res = l_tile_comp->resolutions; - if (l_res) { - - l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_v2_t); - for (resno = 0; resno < l_nb_resolutions; ++resno) { - l_band = l_res->bands; - for (bandno = 0; bandno < 3; ++bandno) { - l_precinct = l_band->precincts; - if (l_precinct) { - - l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_v2_t); - for (precno = 0; precno < l_nb_precincts; ++precno) { - tgt_destroy(l_precinct->incltree); - l_precinct->incltree = 00; - tgt_destroy(l_precinct->imsbtree); - l_precinct->imsbtree = 00; - (*l_tcd_code_block_deallocate) (l_precinct); - ++l_precinct; - } - - opj_free(l_band->precincts); - l_band->precincts = 00; - } - ++l_band; - } /* for (resno */ - ++l_res; - } - - opj_free(l_tile_comp->resolutions); - l_tile_comp->resolutions = 00; - } - - if (l_tile_comp->data) { - opj_free(l_tile_comp->data); - l_tile_comp->data = 00; - } - ++l_tile_comp; - } - - opj_free(l_tile->comps); - l_tile->comps = 00; - opj_free(p_tcd->tcd_image->tiles); - p_tcd->tcd_image->tiles = 00; + OPJ_UINT32 compno, resno, bandno, precno; + opj_tcd_tile_v2_t *l_tile = 00; + opj_tcd_tilecomp_v2_t *l_tile_comp = 00; + opj_tcd_resolution_v2_t *l_res = 00; + opj_tcd_band_v2_t *l_band = 00; + opj_tcd_precinct_v2_t *l_precinct = 00; + OPJ_UINT32 l_nb_resolutions, l_nb_precincts; + void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_v2_t *) = 00; + + if (! p_tcd) { + return; + } + + if (! p_tcd->tcd_image) { + return; + } + + if (p_tcd->m_is_decoder) { + l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate; + } + else { + l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate; + } + + l_tile = p_tcd->tcd_image->tiles; + if (! l_tile) { + return; + } + + l_tile_comp = l_tile->comps; + + for (compno = 0; compno < l_tile->numcomps; ++compno) { + l_res = l_tile_comp->resolutions; + if (l_res) { + + l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_v2_t); + for (resno = 0; resno < l_nb_resolutions; ++resno) { + l_band = l_res->bands; + for (bandno = 0; bandno < 3; ++bandno) { + l_precinct = l_band->precincts; + if (l_precinct) { + + l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_v2_t); + for (precno = 0; precno < l_nb_precincts; ++precno) { + tgt_destroy(l_precinct->incltree); + l_precinct->incltree = 00; + tgt_destroy(l_precinct->imsbtree); + l_precinct->imsbtree = 00; + (*l_tcd_code_block_deallocate) (l_precinct); + ++l_precinct; + } + + opj_free(l_band->precincts); + l_band->precincts = 00; + } + ++l_band; + } /* for (resno */ + ++l_res; + } + + opj_free(l_tile_comp->resolutions); + l_tile_comp->resolutions = 00; + } + + if (l_tile_comp->data) { + opj_free(l_tile_comp->data); + l_tile_comp->data = 00; + } + ++l_tile_comp; + } + + opj_free(l_tile->comps); + l_tile->comps = 00; + opj_free(p_tcd->tcd_image->tiles); + p_tcd->tcd_image->tiles = 00; } @@ -1446,232 +1469,235 @@ opj_bool opj_tcd_t2_decode (opj_tcd_v2_t *p_tcd, opj_codestream_index_t *p_cstr_index ) { - opj_t2_v2_t * l_t2; - - l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp); - if (l_t2 == 00) { - return OPJ_FALSE; - } - - if (! t2_decode_packets_v2( - l_t2, - p_tcd->tcd_tileno, - p_tcd->tcd_image->tiles, - p_src_data, - p_data_read, - p_max_src_size, - p_cstr_index)) { - t2_destroy_v2(l_t2); - return OPJ_FALSE; - } - - t2_destroy_v2(l_t2); - - /*---------------CLEAN-------------------*/ - return OPJ_TRUE; + opj_t2_v2_t * l_t2; + + l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp); + if (l_t2 == 00) { + return OPJ_FALSE; + } + + if (! t2_decode_packets_v2( + l_t2, + p_tcd->tcd_tileno, + p_tcd->tcd_image->tiles, + p_src_data, + p_data_read, + p_max_src_size, + p_cstr_index)) { + t2_destroy_v2(l_t2); + return OPJ_FALSE; + } + + t2_destroy_v2(l_t2); + + /*---------------CLEAN-------------------*/ + return OPJ_TRUE; } opj_bool opj_tcd_t1_decode ( opj_tcd_v2_t *p_tcd ) { - OPJ_UINT32 compno; - opj_t1_t * l_t1; - opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles; - opj_tcd_tilecomp_v2_t* l_tile_comp = l_tile->comps; - opj_tccp_t * l_tccp = p_tcd->tcp->tccps; + OPJ_UINT32 compno; + opj_t1_t * l_t1; + opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_v2_t* l_tile_comp = l_tile->comps; + opj_tccp_t * l_tccp = p_tcd->tcp->tccps; + + + l_t1 = opj_t1_create(); + if (l_t1 == 00) { + return OPJ_FALSE; + } + + for (compno = 0; compno < l_tile->numcomps; ++compno) { + /* The +3 is headroom required by the vectorized DWT */ + if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) { + opj_t1_destroy(l_t1); + return OPJ_FALSE; + } + ++l_tile_comp; + ++l_tccp; + } + opj_t1_destroy(l_t1); - l_t1 = opj_t1_create(); - if (l_t1 == 00) { - return OPJ_FALSE; - } - - for (compno = 0; compno < l_tile->numcomps; ++compno) { - /* The +3 is headroom required by the vectorized DWT */ - opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp); - ++l_tile_comp; - ++l_tccp; - } - - opj_t1_destroy(l_t1); - - return OPJ_TRUE; + return OPJ_TRUE; } opj_bool opj_tcd_dwt_decode ( opj_tcd_v2_t *p_tcd ) { - OPJ_UINT32 compno; - opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles; - opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps; - opj_tccp_t * l_tccp = p_tcd->tcp->tccps; - opj_image_comp_t * l_img_comp = p_tcd->image->comps; - - for (compno = 0; compno < l_tile->numcomps; compno++) { - /* - if (tcd->cp->reduce != 0) { - tcd->image->comps[compno].resno_decoded = - tile->comps[compno].numresolutions - tcd->cp->reduce - 1; - if (tcd->image->comps[compno].resno_decoded < 0) - { - return false; - } - } - numres2decode = tcd->image->comps[compno].resno_decoded + 1; - if(numres2decode > 0){ - */ - - if (l_tccp->qmfbid == 1) { - if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) { - return OPJ_FALSE; - } - } - else { - if (! dwt_decode_real_v2(l_tile_comp, l_img_comp->resno_decoded+1)) { - return OPJ_FALSE; - } - } - - ++l_tile_comp; - ++l_img_comp; - ++l_tccp; - } - - return OPJ_TRUE; + OPJ_UINT32 compno; + opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps; + opj_tccp_t * l_tccp = p_tcd->tcp->tccps; + opj_image_comp_t * l_img_comp = p_tcd->image->comps; + + for (compno = 0; compno < l_tile->numcomps; compno++) { + /* + if (tcd->cp->reduce != 0) { + tcd->image->comps[compno].resno_decoded = + tile->comps[compno].numresolutions - tcd->cp->reduce - 1; + if (tcd->image->comps[compno].resno_decoded < 0) + { + return false; + } + } + numres2decode = tcd->image->comps[compno].resno_decoded + 1; + if(numres2decode > 0){ + */ + + if (l_tccp->qmfbid == 1) { + if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) { + return OPJ_FALSE; + } + } + else { + if (! dwt_decode_real_v2(l_tile_comp, l_img_comp->resno_decoded+1)) { + return OPJ_FALSE; + } + } + + ++l_tile_comp; + ++l_img_comp; + ++l_tccp; + } + + return OPJ_TRUE; } opj_bool opj_tcd_mct_decode ( opj_tcd_v2_t *p_tcd ) { - opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles; - opj_tcp_v2_t * l_tcp = p_tcd->tcp; - opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps; - OPJ_UINT32 l_samples,i; - - if (! l_tcp->mct) { - return OPJ_TRUE; - } - - l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0); - - if (l_tile->numcomps >= 3 ){ - if (l_tcp->mct == 2) { - OPJ_BYTE ** l_data; - - if (! l_tcp->m_mct_decoding_matrix) { - return OPJ_TRUE; - } - - l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*)); - if (! l_data) { - return OPJ_FALSE; - } - - for (i=0;inumcomps;++i) { - l_data[i] = (OPJ_BYTE*) l_tile_comp->data; - ++l_tile_comp; - } - - if (! mct_decode_custom(/* MCT data */ - (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix, - /* size of components */ - l_samples, - /* components */ - l_data, - /* nb of components (i.e. size of pData) */ - l_tile->numcomps, - /* tells if the data is signed */ - p_tcd->image->comps->sgnd)) { - opj_free(l_data); - return OPJ_FALSE; - } - - opj_free(l_data); - } - else { - if (l_tcp->tccps->qmfbid == 1) { - mct_decode( l_tile->comps[0].data, - l_tile->comps[1].data, - l_tile->comps[2].data, - l_samples); - } - else { - mct_decode_real( (float*)l_tile->comps[0].data, - (float*)l_tile->comps[1].data, - (float*)l_tile->comps[2].data, - l_samples); - } - } - } - else { - /* FIXME need to use opj_event_msg_v2 function */ - fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps); - } - - return OPJ_TRUE; + opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcp_v2_t * l_tcp = p_tcd->tcp; + opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps; + OPJ_UINT32 l_samples,i; + + if (! l_tcp->mct) { + return OPJ_TRUE; + } + + l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0); + + if (l_tile->numcomps >= 3 ){ + if (l_tcp->mct == 2) { + OPJ_BYTE ** l_data; + + if (! l_tcp->m_mct_decoding_matrix) { + return OPJ_TRUE; + } + + l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*)); + if (! l_data) { + return OPJ_FALSE; + } + + for (i=0;inumcomps;++i) { + l_data[i] = (OPJ_BYTE*) l_tile_comp->data; + ++l_tile_comp; + } + + if (! mct_decode_custom(/* MCT data */ + (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix, + /* size of components */ + l_samples, + /* components */ + l_data, + /* nb of components (i.e. size of pData) */ + l_tile->numcomps, + /* tells if the data is signed */ + p_tcd->image->comps->sgnd)) { + opj_free(l_data); + return OPJ_FALSE; + } + + opj_free(l_data); + } + else { + if (l_tcp->tccps->qmfbid == 1) { + mct_decode( l_tile->comps[0].data, + l_tile->comps[1].data, + l_tile->comps[2].data, + l_samples); + } + else { + mct_decode_real( (float*)l_tile->comps[0].data, + (float*)l_tile->comps[1].data, + (float*)l_tile->comps[2].data, + l_samples); + } + } + } + else { + /* FIXME need to use opj_event_msg_v2 function */ + fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps); + } + + return OPJ_TRUE; } opj_bool opj_tcd_dc_level_shift_decode ( opj_tcd_v2_t *p_tcd ) { - OPJ_UINT32 compno; - opj_tcd_tilecomp_v2_t * l_tile_comp = 00; - opj_tccp_t * l_tccp = 00; - opj_image_comp_t * l_img_comp = 00; - opj_tcd_resolution_v2_t* l_res = 00; - opj_tcp_v2_t * l_tcp = 00; - opj_tcd_tile_v2_t * l_tile; - OPJ_UINT32 l_width,l_height,i,j; - OPJ_INT32 * l_current_ptr; - OPJ_INT32 l_min, l_max; - OPJ_UINT32 l_stride; - - l_tile = p_tcd->tcd_image->tiles; - l_tile_comp = l_tile->comps; - l_tcp = p_tcd->tcp; - l_tccp = p_tcd->tcp->tccps; - l_img_comp = p_tcd->image->comps; - - for (compno = 0; compno < l_tile->numcomps; compno++) { - l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded; - l_width = (l_res->x1 - l_res->x0); - l_height = (l_res->y1 - l_res->y0); - l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width; - - if (l_img_comp->sgnd) { - l_min = -(1 << (l_img_comp->prec - 1)); - l_max = (1 << (l_img_comp->prec - 1)) - 1; - } - else { + OPJ_UINT32 compno; + opj_tcd_tilecomp_v2_t * l_tile_comp = 00; + opj_tccp_t * l_tccp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_resolution_v2_t* l_res = 00; + opj_tcp_v2_t * l_tcp = 00; + opj_tcd_tile_v2_t * l_tile; + OPJ_UINT32 l_width,l_height,i,j; + OPJ_INT32 * l_current_ptr; + OPJ_INT32 l_min, l_max; + OPJ_UINT32 l_stride; + + l_tile = p_tcd->tcd_image->tiles; + l_tile_comp = l_tile->comps; + l_tcp = p_tcd->tcp; + l_tccp = p_tcd->tcp->tccps; + l_img_comp = p_tcd->image->comps; + + for (compno = 0; compno < l_tile->numcomps; compno++) { + l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded; + l_width = (l_res->x1 - l_res->x0); + l_height = (l_res->y1 - l_res->y0); + l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width; + + if (l_img_comp->sgnd) { + l_min = -(1 << (l_img_comp->prec - 1)); + l_max = (1 << (l_img_comp->prec - 1)) - 1; + } + else { l_min = 0; - l_max = (1 << l_img_comp->prec) - 1; - } - - l_current_ptr = l_tile_comp->data; - - if (l_tccp->qmfbid == 1) { - for (j=0;jm_dc_level_shift, l_min, l_max); - ++l_current_ptr; - } - l_current_ptr += l_stride; - } - } - else { - for (j=0;jm_dc_level_shift, l_min, l_max); ; - ++l_current_ptr; - } - l_current_ptr += l_stride; - } - } - - ++l_img_comp; - ++l_tccp; - ++l_tile_comp; - } - - return OPJ_TRUE; + l_max = (1 << l_img_comp->prec) - 1; + } + + l_current_ptr = l_tile_comp->data; + + if (l_tccp->qmfbid == 1) { + for (j=0;jm_dc_level_shift, l_min, l_max); + ++l_current_ptr; + } + l_current_ptr += l_stride; + } + } + else { + for (j=0;jm_dc_level_shift, l_min, l_max); ; + ++l_current_ptr; + } + l_current_ptr += l_stride; + } + } + + ++l_img_comp; + ++l_tccp; + ++l_tile_comp; + } + + return OPJ_TRUE; } @@ -1681,412 +1707,412 @@ opj_bool opj_tcd_dc_level_shift_decode ( opj_tcd_v2_t *p_tcd ) */ void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct) { - OPJ_UINT32 cblkno , l_nb_code_blocks; + OPJ_UINT32 cblkno , l_nb_code_blocks; - opj_tcd_cblk_dec_v2_t * l_code_block = p_precinct->cblks.dec; - if (l_code_block) { - /*fprintf(stderr,"deallocate codeblock:{\n");*/ - /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/ - /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ", - l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/ + opj_tcd_cblk_dec_v2_t * l_code_block = p_precinct->cblks.dec; + if (l_code_block) { + /*fprintf(stderr,"deallocate codeblock:{\n");*/ + /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/ + /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ", + l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/ - l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_v2_t); - /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/ + l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_v2_t); + /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/ - for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { - if (l_code_block->data) { - opj_free(l_code_block->data); - l_code_block->data = 00; - } + if (l_code_block->data) { + opj_free(l_code_block->data); + l_code_block->data = 00; + } - if (l_code_block->segs) { - opj_free(l_code_block->segs ); - l_code_block->segs = 00; - } + if (l_code_block->segs) { + opj_free(l_code_block->segs ); + l_code_block->segs = 00; + } - ++l_code_block; - } + ++l_code_block; + } - opj_free(p_precinct->cblks.dec); - p_precinct->cblks.dec = 00; - } + opj_free(p_precinct->cblks.dec); + p_precinct->cblks.dec = 00; + } } /** * Deallocates the encoding data of the given precinct. */ void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_v2_t * p_precinct) -{ - OPJ_UINT32 cblkno , l_nb_code_blocks; - - opj_tcd_cblk_enc_v2_t * l_code_block = p_precinct->cblks.enc; - if (l_code_block) { - l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t); - - for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { - if (l_code_block->data) { - opj_free(l_code_block->data-1); - l_code_block->data = 00; - } - - if (l_code_block->layers) { - opj_free(l_code_block->layers ); - l_code_block->layers = 00; - } - - if (l_code_block->passes) { - opj_free(l_code_block->passes ); - l_code_block->passes = 00; - } - ++l_code_block; - } - - opj_free(p_precinct->cblks.enc); - - p_precinct->cblks.enc = 00; - } +{ + OPJ_UINT32 cblkno , l_nb_code_blocks; + + opj_tcd_cblk_enc_v2_t * l_code_block = p_precinct->cblks.enc; + if (l_code_block) { + l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t); + + for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { + if (l_code_block->data) { + opj_free(l_code_block->data-1); + l_code_block->data = 00; + } + + if (l_code_block->layers) { + opj_free(l_code_block->layers ); + l_code_block->layers = 00; + } + + if (l_code_block->passes) { + opj_free(l_code_block->passes ); + l_code_block->passes = 00; + } + ++l_code_block; + } + + opj_free(p_precinct->cblks.enc); + + p_precinct->cblks.enc = 00; + } } OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_v2_t *p_tcd ) { - OPJ_UINT32 i,l_data_size = 0; - opj_image_comp_t * l_img_comp = 00; - opj_tcd_tilecomp_v2_t * l_tilec = 00; - OPJ_UINT32 l_size_comp, l_remaining; - - l_tilec = p_tcd->tcd_image->tiles->comps; - l_img_comp = p_tcd->image->comps; - for (i=0;iimage->numcomps;++i) { - l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ - l_remaining = l_img_comp->prec & 7; /* (%8) */ - - if (l_remaining) { - ++l_size_comp; - } - - if (l_size_comp == 3) { - l_size_comp = 4; - } - - l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0); - ++l_img_comp; - ++l_tilec; - } - - return l_data_size; + OPJ_UINT32 i,l_data_size = 0; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tilecomp_v2_t * l_tilec = 00; + OPJ_UINT32 l_size_comp, l_remaining; + + l_tilec = p_tcd->tcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + for (i=0;iimage->numcomps;++i) { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + + if (l_remaining) { + ++l_size_comp; + } + + if (l_size_comp == 3) { + l_size_comp = 4; + } + + l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0); + ++l_img_comp; + ++l_tilec; + } + + return l_data_size; } - + opj_bool opj_tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd ) { - OPJ_UINT32 compno; - opj_tcd_tilecomp_v2_t * l_tile_comp = 00; - opj_tccp_t * l_tccp = 00; - opj_image_comp_t * l_img_comp = 00; - opj_tcp_v2_t * l_tcp = 00; - opj_tcd_tile_v2_t * l_tile; - OPJ_UINT32 l_nb_elem,i; - OPJ_INT32 * l_current_ptr; - - l_tile = p_tcd->tcd_image->tiles; - l_tile_comp = l_tile->comps; - l_tcp = p_tcd->tcp; - l_tccp = p_tcd->tcp->tccps; - l_img_comp = p_tcd->image->comps; - - for (compno = 0; compno < l_tile->numcomps; compno++) { - l_current_ptr = l_tile_comp->data; - l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0); - - if (l_tccp->qmfbid == 1) { - for (i = 0; i < l_nb_elem; ++i) { - *l_current_ptr -= l_tccp->m_dc_level_shift ; - ++l_current_ptr; - } - } - else { - for (i = 0; i < l_nb_elem; ++i) { - *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ; - ++l_current_ptr; - } - } - - ++l_img_comp; - ++l_tccp; - ++l_tile_comp; - } - - return OPJ_TRUE; + OPJ_UINT32 compno; + opj_tcd_tilecomp_v2_t * l_tile_comp = 00; + opj_tccp_t * l_tccp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_tcp_v2_t * l_tcp = 00; + opj_tcd_tile_v2_t * l_tile; + OPJ_UINT32 l_nb_elem,i; + OPJ_INT32 * l_current_ptr; + + l_tile = p_tcd->tcd_image->tiles; + l_tile_comp = l_tile->comps; + l_tcp = p_tcd->tcp; + l_tccp = p_tcd->tcp->tccps; + l_img_comp = p_tcd->image->comps; + + for (compno = 0; compno < l_tile->numcomps; compno++) { + l_current_ptr = l_tile_comp->data; + l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0); + + if (l_tccp->qmfbid == 1) { + for (i = 0; i < l_nb_elem; ++i) { + *l_current_ptr -= l_tccp->m_dc_level_shift ; + ++l_current_ptr; + } + } + else { + for (i = 0; i < l_nb_elem; ++i) { + *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ; + ++l_current_ptr; + } + } + + ++l_img_comp; + ++l_tccp; + ++l_tile_comp; + } + + return OPJ_TRUE; } opj_bool opj_tcd_mct_encode ( opj_tcd_v2_t *p_tcd ) { - opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles; - opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps; - OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0); - OPJ_UINT32 i; - OPJ_BYTE ** l_data = 00; - opj_tcp_v2_t * l_tcp = p_tcd->tcp; - - if(!p_tcd->tcp->mct) { - return OPJ_TRUE; - } - - if (p_tcd->tcp->mct == 2) { - if (! p_tcd->tcp->m_mct_coding_matrix) { - return OPJ_TRUE; - } + opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps; + OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0); + OPJ_UINT32 i; + OPJ_BYTE ** l_data = 00; + opj_tcp_v2_t * l_tcp = p_tcd->tcp; + + if(!p_tcd->tcp->mct) { + return OPJ_TRUE; + } + + if (p_tcd->tcp->mct == 2) { + if (! p_tcd->tcp->m_mct_coding_matrix) { + return OPJ_TRUE; + } l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*)); - if (! l_data) { - return OPJ_FALSE; - } - - for (i=0;inumcomps;++i) { - l_data[i] = (OPJ_BYTE*) l_tile_comp->data; - ++l_tile_comp; - } - - if (! mct_encode_custom(/* MCT data */ - (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix, - /* size of components */ - samples, - /* components */ - l_data, - /* nb of components (i.e. size of pData) */ - l_tile->numcomps, - /* tells if the data is signed */ - p_tcd->image->comps->sgnd) ) - { + if (! l_data) { + return OPJ_FALSE; + } + + for (i=0;inumcomps;++i) { + l_data[i] = (OPJ_BYTE*) l_tile_comp->data; + ++l_tile_comp; + } + + if (! mct_encode_custom(/* MCT data */ + (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix, + /* size of components */ + samples, + /* components */ + l_data, + /* nb of components (i.e. size of pData) */ + l_tile->numcomps, + /* tells if the data is signed */ + p_tcd->image->comps->sgnd) ) + { opj_free(l_data); - return OPJ_FALSE; - } - - opj_free(l_data); - } - else if (l_tcp->tccps->qmfbid == 0) { - mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples); - } - else { - mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples); - } - - return OPJ_TRUE; + return OPJ_FALSE; + } + + opj_free(l_data); + } + else if (l_tcp->tccps->qmfbid == 0) { + mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples); + } + else { + mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples); + } + + return OPJ_TRUE; } opj_bool opj_tcd_dwt_encode ( opj_tcd_v2_t *p_tcd ) { - opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles; - opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps; - opj_tccp_t * l_tccp = p_tcd->tcp->tccps; - OPJ_UINT32 compno; - - for (compno = 0; compno < l_tile->numcomps; ++compno) { - if (l_tccp->qmfbid == 1) { - if (! opj_dwt_encode(l_tile_comp)) { - return OPJ_FALSE; - } - } - else if (l_tccp->qmfbid == 0) { - if (! opj_dwt_encode_real(l_tile_comp)) { - return OPJ_FALSE; - } - } - - ++l_tile_comp; - ++l_tccp; - } - - return OPJ_TRUE; + opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps; + opj_tccp_t * l_tccp = p_tcd->tcp->tccps; + OPJ_UINT32 compno; + + for (compno = 0; compno < l_tile->numcomps; ++compno) { + if (l_tccp->qmfbid == 1) { + if (! opj_dwt_encode(l_tile_comp)) { + return OPJ_FALSE; + } + } + else if (l_tccp->qmfbid == 0) { + if (! opj_dwt_encode_real(l_tile_comp)) { + return OPJ_FALSE; + } + } + + ++l_tile_comp; + ++l_tccp; + } + + return OPJ_TRUE; } opj_bool opj_tcd_t1_encode ( opj_tcd_v2_t *p_tcd ) { - opj_t1_t * l_t1; - const OPJ_FLOAT64 * l_mct_norms; - opj_tcp_v2_t * l_tcp = p_tcd->tcp; - - l_t1 = opj_t1_create(); - if (l_t1 == 00) { - return OPJ_FALSE; - } - - if (l_tcp->mct == 1) { - /* irreversible encoding */ - if (l_tcp->tccps->qmfbid == 0) { - l_mct_norms = get_mct_norms_real(); - } - else { - l_mct_norms = get_mct_norms(); - } - } - else { - l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms); - } - - if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) { + opj_t1_t * l_t1; + const OPJ_FLOAT64 * l_mct_norms; + opj_tcp_v2_t * l_tcp = p_tcd->tcp; + + l_t1 = opj_t1_create(); + if (l_t1 == 00) { + return OPJ_FALSE; + } + + if (l_tcp->mct == 1) { + /* irreversible encoding */ + if (l_tcp->tccps->qmfbid == 0) { + l_mct_norms = get_mct_norms_real(); + } + else { + l_mct_norms = get_mct_norms(); + } + } + else { + l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms); + } + + if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) { opj_t1_destroy(l_t1); - return OPJ_FALSE; - } + return OPJ_FALSE; + } - opj_t1_destroy(l_t1); + opj_t1_destroy(l_t1); - return OPJ_TRUE; + return OPJ_TRUE; } opj_bool opj_tcd_t2_encode (opj_tcd_v2_t *p_tcd, - OPJ_BYTE * p_dest_data, - OPJ_UINT32 * p_data_written, - OPJ_UINT32 p_max_dest_size, - opj_codestream_info_t *p_cstr_info ) + OPJ_BYTE * p_dest_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_dest_size, + opj_codestream_info_t *p_cstr_info ) { - opj_t2_v2_t * l_t2; - - l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp); - if (l_t2 == 00) { - return OPJ_FALSE; - } - - if (! t2_encode_packets_v2( - l_t2, - p_tcd->tcd_tileno, - p_tcd->tcd_image->tiles, - p_tcd->tcp->numlayers, - p_dest_data, - p_data_written, - p_max_dest_size, - p_cstr_info, - p_tcd->tp_num, - p_tcd->tp_pos, - p_tcd->cur_pino, - FINAL_PASS)) - { - t2_destroy_v2(l_t2); - return OPJ_FALSE; - } - - t2_destroy_v2(l_t2); - - /*---------------CLEAN-------------------*/ - return OPJ_TRUE; + opj_t2_v2_t * l_t2; + + l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp); + if (l_t2 == 00) { + return OPJ_FALSE; + } + + if (! t2_encode_packets_v2( + l_t2, + p_tcd->tcd_tileno, + p_tcd->tcd_image->tiles, + p_tcd->tcp->numlayers, + p_dest_data, + p_data_written, + p_max_dest_size, + p_cstr_info, + p_tcd->tp_num, + p_tcd->tp_pos, + p_tcd->cur_pino, + FINAL_PASS)) + { + t2_destroy_v2(l_t2); + return OPJ_FALSE; + } + + t2_destroy_v2(l_t2); + + /*---------------CLEAN-------------------*/ + return OPJ_TRUE; } -opj_bool opj_tcd_rate_allocate_encode( opj_tcd_v2_t *p_tcd, - OPJ_BYTE * p_dest_data, - OPJ_UINT32 p_max_dest_size, - opj_codestream_info_t *p_cstr_info ) +opj_bool opj_tcd_rate_allocate_encode( opj_tcd_v2_t *p_tcd, + OPJ_BYTE * p_dest_data, + OPJ_UINT32 p_max_dest_size, + opj_codestream_info_t *p_cstr_info ) { - opj_cp_v2_t * l_cp = p_tcd->cp; - OPJ_UINT32 l_nb_written = 0; - - if (p_cstr_info) { - p_cstr_info->index_write = 0; - } - - if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) { - /* fixed_quality */ - /* Normal Rate/distortion allocation */ - if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) { - return OPJ_FALSE; - } - } - else { - /* Fixed layer allocation */ - opj_tcd_rateallocate_fixed(p_tcd); - } - - return OPJ_TRUE; + opj_cp_v2_t * l_cp = p_tcd->cp; + OPJ_UINT32 l_nb_written = 0; + + if (p_cstr_info) { + p_cstr_info->index_write = 0; + } + + if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) { + /* fixed_quality */ + /* Normal Rate/distortion allocation */ + if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) { + return OPJ_FALSE; + } + } + else { + /* Fixed layer allocation */ + opj_tcd_rateallocate_fixed(p_tcd); + } + + return OPJ_TRUE; } -opj_bool opj_tcd_copy_tile_data ( opj_tcd_v2_t *p_tcd, - OPJ_BYTE * p_src, - OPJ_UINT32 p_src_length ) +opj_bool opj_tcd_copy_tile_data ( opj_tcd_v2_t *p_tcd, + OPJ_BYTE * p_src, + OPJ_UINT32 p_src_length ) { - OPJ_UINT32 i,j,l_data_size = 0; - opj_image_comp_t * l_img_comp = 00; - opj_tcd_tilecomp_v2_t * l_tilec = 00; - OPJ_UINT32 l_size_comp, l_remaining; - OPJ_UINT32 l_nb_elem; - - l_data_size = opj_tcd_get_encoded_tile_size(p_tcd); - if (l_data_size != p_src_length) { - return OPJ_FALSE; - } - - l_tilec = p_tcd->tcd_image->tiles->comps; - l_img_comp = p_tcd->image->comps; - for (i=0;iimage->numcomps;++i) { - l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ - l_remaining = l_img_comp->prec & 7; /* (%8) */ - l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0); - - if (l_remaining) { - ++l_size_comp; - } - - if (l_size_comp == 3) { - l_size_comp = 4; - } - - switch (l_size_comp) { - case 1: - { - OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src; - OPJ_INT32 * l_dest_ptr = l_tilec->data; - - if (l_img_comp->sgnd) { - for (j=0;jdata; - OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src; - - if (l_img_comp->sgnd) { - for (j=0;jdata; - - for (j=0;jtcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + for (i=0;iimage->numcomps;++i) { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0); + + if (l_remaining) { + ++l_size_comp; + } + + if (l_size_comp == 3) { + l_size_comp = 4; + } + + switch (l_size_comp) { + case 1: + { + OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src; + OPJ_INT32 * l_dest_ptr = l_tilec->data; + + if (l_img_comp->sgnd) { + for (j=0;jdata; + OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src; + + if (l_img_comp->sgnd) { + for (j=0;jdata; + + for (j=0;jnumleafsh = numleafsh; - tree->numleafsv = numleafsv; - - numlvls = 0; - nplh[0] = numleafsh; - nplv[0] = numleafsv; - tree->numnodes = 0; - do { - n = nplh[numlvls] * nplv[numlvls]; - nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2; - nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2; - tree->numnodes += n; - ++numlvls; - } while (n > 1); - - /* ADD */ - if (tree->numnodes == 0) { - opj_free(tree); - return NULL; - } - - tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t)); - if(!tree->nodes) { - opj_free(tree); - return NULL; - } - - node = tree->nodes; - parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv]; - parentnode0 = parentnode; - - for (i = 0; i < numlvls - 1; ++i) { - for (j = 0; j < nplv[i]; ++j) { - k = nplh[i]; - while (--k >= 0) { - node->parent = parentnode; - ++node; - if (--k >= 0) { - node->parent = parentnode; - ++node; - } - ++parentnode; - } - if ((j & 1) || j == nplv[i] - 1) { - parentnode0 = parentnode; - } else { - parentnode = parentnode0; - parentnode0 += nplh[i]; - } - } - } - node->parent = 0; - - tgt_reset(tree); - - return tree; + int nplh[32]; + int nplv[32]; + opj_tgt_node_t *node = NULL; + opj_tgt_node_t *parentnode = NULL; + opj_tgt_node_t *parentnode0 = NULL; + opj_tgt_tree_t *tree = NULL; + int i, j, k; + int numlvls; + int n; + + tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t)); + if(!tree) return NULL; + tree->numleafsh = numleafsh; + tree->numleafsv = numleafsv; + + numlvls = 0; + nplh[0] = numleafsh; + nplv[0] = numleafsv; + tree->numnodes = 0; + do { + n = nplh[numlvls] * nplv[numlvls]; + nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2; + nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2; + tree->numnodes += n; + ++numlvls; + } while (n > 1); + + /* ADD */ + if (tree->numnodes == 0) { + opj_free(tree); + return NULL; + } + + tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t)); + if(!tree->nodes) { + opj_free(tree); + return NULL; + } + + node = tree->nodes; + parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv]; + parentnode0 = parentnode; + + for (i = 0; i < numlvls - 1; ++i) { + for (j = 0; j < nplv[i]; ++j) { + k = nplh[i]; + while (--k >= 0) { + node->parent = parentnode; + ++node; + if (--k >= 0) { + node->parent = parentnode; + ++node; + } + ++parentnode; + } + if ((j & 1) || j == nplv[i] - 1) { + parentnode0 = parentnode; + } else { + parentnode = parentnode0; + parentnode0 += nplh[i]; + } + } + } + node->parent = 0; + + tgt_reset(tree); + + return tree; } opj_tgt_tree_t *tgt_create_v2(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv) { - OPJ_INT32 nplh[32]; - OPJ_INT32 nplv[32]; - opj_tgt_node_t *node = 00; - opj_tgt_node_t *l_parent_node = 00; - opj_tgt_node_t *l_parent_node0 = 00; - opj_tgt_tree_t *tree = 00; - OPJ_UINT32 i; - OPJ_INT32 j,k; - OPJ_UINT32 numlvls; - OPJ_UINT32 n; - - tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t)); - if(!tree) { - fprintf(stderr, "ERROR in tgt_create_v2 while allocating tree\n"); - return 00; - } - memset(tree,0,sizeof(opj_tgt_tree_t)); - - tree->numleafsh = numleafsh; - tree->numleafsv = numleafsv; - - numlvls = 0; - nplh[0] = numleafsh; - nplv[0] = numleafsv; - tree->numnodes = 0; - do { - n = nplh[numlvls] * nplv[numlvls]; - nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2; - nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2; - tree->numnodes += n; - ++numlvls; - } while (n > 1); - - /* ADD */ - if (tree->numnodes == 0) { - opj_free(tree); - fprintf(stderr, "WARNING in tgt_create_v2 tree->numnodes == 0, no tree created.\n"); - return 00; - } - - tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t)); - if(!tree->nodes) { - fprintf(stderr, "ERROR in tgt_create_v2 while allocating node of the tree\n"); - opj_free(tree); - return 00; - } - memset(tree->nodes,0,tree->numnodes * sizeof(opj_tgt_node_t)); - tree->nodes_size = tree->numnodes * sizeof(opj_tgt_node_t); - - node = tree->nodes; - l_parent_node = &tree->nodes[tree->numleafsh * tree->numleafsv]; - l_parent_node0 = l_parent_node; - - for (i = 0; i < numlvls - 1; ++i) { - for (j = 0; j < nplv[i]; ++j) { - k = nplh[i]; - while (--k >= 0) { - node->parent = l_parent_node; - ++node; - if (--k >= 0) { - node->parent = l_parent_node; - ++node; - } - ++l_parent_node; - } - if ((j & 1) || j == nplv[i] - 1) { - l_parent_node0 = l_parent_node; - } else { - l_parent_node = l_parent_node0; - l_parent_node0 += nplh[i]; - } - } - } - node->parent = 0; - tgt_reset(tree); - return tree; + OPJ_INT32 nplh[32]; + OPJ_INT32 nplv[32]; + opj_tgt_node_t *node = 00; + opj_tgt_node_t *l_parent_node = 00; + opj_tgt_node_t *l_parent_node0 = 00; + opj_tgt_tree_t *tree = 00; + OPJ_UINT32 i; + OPJ_INT32 j,k; + OPJ_UINT32 numlvls; + OPJ_UINT32 n; + + tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t)); + if(!tree) { + fprintf(stderr, "ERROR in tgt_create_v2 while allocating tree\n"); + return 00; + } + memset(tree,0,sizeof(opj_tgt_tree_t)); + + tree->numleafsh = numleafsh; + tree->numleafsv = numleafsv; + + numlvls = 0; + nplh[0] = numleafsh; + nplv[0] = numleafsv; + tree->numnodes = 0; + do { + n = nplh[numlvls] * nplv[numlvls]; + nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2; + nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2; + tree->numnodes += n; + ++numlvls; + } while (n > 1); + + /* ADD */ + if (tree->numnodes == 0) { + opj_free(tree); + fprintf(stderr, "WARNING in tgt_create_v2 tree->numnodes == 0, no tree created.\n"); + return 00; + } + + tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t)); + if(!tree->nodes) { + fprintf(stderr, "ERROR in tgt_create_v2 while allocating node of the tree\n"); + opj_free(tree); + return 00; + } + memset(tree->nodes,0,tree->numnodes * sizeof(opj_tgt_node_t)); + tree->nodes_size = tree->numnodes * sizeof(opj_tgt_node_t); + + node = tree->nodes; + l_parent_node = &tree->nodes[tree->numleafsh * tree->numleafsv]; + l_parent_node0 = l_parent_node; + + for (i = 0; i < numlvls - 1; ++i) { + for (j = 0; j < nplv[i]; ++j) { + k = nplh[i]; + while (--k >= 0) { + node->parent = l_parent_node; + ++node; + if (--k >= 0) { + node->parent = l_parent_node; + ++node; + } + ++l_parent_node; + } + if ((j & 1) || j == nplv[i] - 1) { + l_parent_node0 = l_parent_node; + } else { + l_parent_node = l_parent_node0; + l_parent_node0 += nplh[i]; + } + } + } + node->parent = 0; + tgt_reset(tree); + return tree; } /** * Reinitialises a tag-tree from an exixting one. (V2 framevork) * - * @param p_tree the tree to reinitialize. - * @param p_num_leafs_h the width of the array of leafs of the tree - * @param p_num_leafs_v the height of the array of leafs of the tree - * @return a new tag-tree if successful, NULL otherwise + * @param p_tree the tree to reinitialize. + * @param p_num_leafs_h the width of the array of leafs of the tree + * @param p_num_leafs_v the height of the array of leafs of the tree + * @return a new tag-tree if successful, NULL otherwise */ opj_tgt_tree_t *tgt_init(opj_tgt_tree_t * p_tree,OPJ_UINT32 p_num_leafs_h, OPJ_UINT32 p_num_leafs_v) { - OPJ_INT32 l_nplh[32]; - OPJ_INT32 l_nplv[32]; - opj_tgt_node_t *l_node = 00; - opj_tgt_node_t *l_parent_node = 00; - opj_tgt_node_t *l_parent_node0 = 00; - OPJ_UINT32 i; - OPJ_INT32 j,k; - OPJ_UINT32 l_num_levels; - OPJ_UINT32 n; - OPJ_UINT32 l_node_size; - - if - (! p_tree) - { - return 00; - } - if - ((p_tree->numleafsh != p_num_leafs_h) || (p_tree->numleafsv != p_num_leafs_v)) - { - p_tree->numleafsh = p_num_leafs_h; - p_tree->numleafsv = p_num_leafs_v; - - l_num_levels = 0; - l_nplh[0] = p_num_leafs_h; - l_nplv[0] = p_num_leafs_v; - p_tree->numnodes = 0; - do - { - n = l_nplh[l_num_levels] * l_nplv[l_num_levels]; - l_nplh[l_num_levels + 1] = (l_nplh[l_num_levels] + 1) / 2; - l_nplv[l_num_levels + 1] = (l_nplv[l_num_levels] + 1) / 2; - p_tree->numnodes += n; - ++l_num_levels; - } - while (n > 1); - - /* ADD */ - if - (p_tree->numnodes == 0) - { - tgt_destroy(p_tree); - return 00; - } - l_node_size = p_tree->numnodes * sizeof(opj_tgt_node_t); - if - (l_node_size > p_tree->nodes_size) - { - p_tree->nodes = (opj_tgt_node_t*) opj_realloc(p_tree->nodes, l_node_size); - if - (! p_tree->nodes) - { - tgt_destroy(p_tree); - return 00; - } - memset(((char *) p_tree->nodes) + p_tree->nodes_size, 0 , l_node_size - p_tree->nodes_size); - p_tree->nodes_size = l_node_size; - } - l_node = p_tree->nodes; - l_parent_node = &p_tree->nodes[p_tree->numleafsh * p_tree->numleafsv]; - l_parent_node0 = l_parent_node; - - for - (i = 0; i < l_num_levels - 1; ++i) - { - for - (j = 0; j < l_nplv[i]; ++j) - { - k = l_nplh[i]; - while - (--k >= 0) - { - l_node->parent = l_parent_node; - ++l_node; - if (--k >= 0) - { - l_node->parent = l_parent_node; - ++l_node; - } - ++l_parent_node; - } - if ((j & 1) || j == l_nplv[i] - 1) - { - l_parent_node0 = l_parent_node; - } - else - { - l_parent_node = l_parent_node0; - l_parent_node0 += l_nplh[i]; - } - } - } - l_node->parent = 0; - } - tgt_reset(p_tree); - - return p_tree; + OPJ_INT32 l_nplh[32]; + OPJ_INT32 l_nplv[32]; + opj_tgt_node_t *l_node = 00; + opj_tgt_node_t *l_parent_node = 00; + opj_tgt_node_t *l_parent_node0 = 00; + OPJ_UINT32 i; + OPJ_INT32 j,k; + OPJ_UINT32 l_num_levels; + OPJ_UINT32 n; + OPJ_UINT32 l_node_size; + + if + (! p_tree) + { + return 00; + } + if + ((p_tree->numleafsh != p_num_leafs_h) || (p_tree->numleafsv != p_num_leafs_v)) + { + p_tree->numleafsh = p_num_leafs_h; + p_tree->numleafsv = p_num_leafs_v; + + l_num_levels = 0; + l_nplh[0] = p_num_leafs_h; + l_nplv[0] = p_num_leafs_v; + p_tree->numnodes = 0; + do + { + n = l_nplh[l_num_levels] * l_nplv[l_num_levels]; + l_nplh[l_num_levels + 1] = (l_nplh[l_num_levels] + 1) / 2; + l_nplv[l_num_levels + 1] = (l_nplv[l_num_levels] + 1) / 2; + p_tree->numnodes += n; + ++l_num_levels; + } + while (n > 1); + + /* ADD */ + if + (p_tree->numnodes == 0) + { + tgt_destroy(p_tree); + return 00; + } + l_node_size = p_tree->numnodes * sizeof(opj_tgt_node_t); + if + (l_node_size > p_tree->nodes_size) + { + opj_tgt_node_t* new_nodes = (opj_tgt_node_t*) opj_realloc(p_tree->nodes, l_node_size); + if + (! p_tree->nodes) + { + fprintf(stderr, "Not enough memory to reinitialize the tag tree\n"); + tgt_destroy(p_tree); + return 00; + } + p_tree->nodes = new_nodes; + memset(((char *) p_tree->nodes) + p_tree->nodes_size, 0 , l_node_size - p_tree->nodes_size); + p_tree->nodes_size = l_node_size; + } + l_node = p_tree->nodes; + l_parent_node = &p_tree->nodes[p_tree->numleafsh * p_tree->numleafsv]; + l_parent_node0 = l_parent_node; + + for + (i = 0; i < l_num_levels - 1; ++i) + { + for + (j = 0; j < l_nplv[i]; ++j) + { + k = l_nplh[i]; + while + (--k >= 0) + { + l_node->parent = l_parent_node; + ++l_node; + if (--k >= 0) + { + l_node->parent = l_parent_node; + ++l_node; + } + ++l_parent_node; + } + if ((j & 1) || j == l_nplv[i] - 1) + { + l_parent_node0 = l_parent_node; + } + else + { + l_parent_node = l_parent_node0; + l_parent_node0 += l_nplh[i]; + } + } + } + l_node->parent = 0; + } + tgt_reset(p_tree); + + return p_tree; } /*void tgt_destroy(opj_tgt_tree_t *tree) { - opj_free(tree->nodes); - opj_free(tree); + opj_free(tree->nodes); + opj_free(tree); }*/ void tgt_destroy(opj_tgt_tree_t *p_tree) { - if (! p_tree) { - return; - } - - if (p_tree->nodes) { - opj_free(p_tree->nodes); - p_tree->nodes = 00; - } - opj_free(p_tree); + if (! p_tree) { + return; + } + + if (p_tree->nodes) { + opj_free(p_tree->nodes); + p_tree->nodes = 00; + } + opj_free(p_tree); } /*void tgt_reset(opj_tgt_tree_t *tree) { - int i; - - if (NULL == tree) - return; - - for (i = 0; i < tree->numnodes; i++) { - tree->nodes[i].value = 999; - tree->nodes[i].low = 0; - tree->nodes[i].known = 0; - } + int i; + + if (NULL == tree) + return; + + for (i = 0; i < tree->numnodes; i++) { + tree->nodes[i].value = 999; + tree->nodes[i].low = 0; + tree->nodes[i].known = 0; + } }*/ void tgt_reset(opj_tgt_tree_t *p_tree) { - OPJ_UINT32 i; - opj_tgt_node_t * l_current_node = 00;; - - if (! p_tree) { - return; - } - - l_current_node = p_tree->nodes; - for (i = 0; i < p_tree->numnodes; ++i) - { - l_current_node->value = 999; - l_current_node->low = 0; - l_current_node->known = 0; - ++l_current_node; - } + OPJ_UINT32 i; + opj_tgt_node_t * l_current_node = 00;; + + if (! p_tree) { + return; + } + + l_current_node = p_tree->nodes; + for (i = 0; i < p_tree->numnodes; ++i) + { + l_current_node->value = 999; + l_current_node->low = 0; + l_current_node->known = 0; + ++l_current_node; + } } void tgt_setvalue(opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 value) { - opj_tgt_node_t *node; - node = &tree->nodes[leafno]; - while (node && node->value > value) { - node->value = value; - node = node->parent; - } + opj_tgt_node_t *node; + node = &tree->nodes[leafno]; + while (node && node->value > value) { + node->value = value; + node = node->parent; + } } void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 threshold) { - opj_tgt_node_t *stk[31]; - opj_tgt_node_t **stkptr; - opj_tgt_node_t *node; - OPJ_INT32 low; - - stkptr = stk; - node = &tree->nodes[leafno]; - while (node->parent) { - *stkptr++ = node; - node = node->parent; - } - - low = 0; - for (;;) { - if (low > node->low) { - node->low = low; - } else { - low = node->low; - } - - while (low < threshold) { - if (low >= node->value) { - if (!node->known) { - bio_write(bio, 1, 1); - node->known = 1; - } - break; - } - bio_write(bio, 0, 1); - ++low; - } - - node->low = low; - if (stkptr == stk) - break; - node = *--stkptr; - } + opj_tgt_node_t *stk[31]; + opj_tgt_node_t **stkptr; + opj_tgt_node_t *node; + OPJ_INT32 low; + + stkptr = stk; + node = &tree->nodes[leafno]; + while (node->parent) { + *stkptr++ = node; + node = node->parent; + } + + low = 0; + for (;;) { + if (low > node->low) { + node->low = low; + } else { + low = node->low; + } + + while (low < threshold) { + if (low >= node->value) { + if (!node->known) { + bio_write(bio, 1, 1); + node->known = 1; + } + break; + } + bio_write(bio, 0, 1); + ++low; + } + + node->low = low; + if (stkptr == stk) + break; + node = *--stkptr; + } } OPJ_UINT32 tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 threshold) { - opj_tgt_node_t *stk[31]; - opj_tgt_node_t **stkptr; - opj_tgt_node_t *node; - OPJ_INT32 low; - - stkptr = stk; - node = &tree->nodes[leafno]; - while (node->parent) { - *stkptr++ = node; - node = node->parent; - } - - low = 0; - for (;;) { - if (low > node->low) { - node->low = low; - } else { - low = node->low; - } - while (low < threshold && low < node->value) { - if (bio_read(bio, 1)) { - node->value = low; - } else { - ++low; - } - } - node->low = low; - if (stkptr == stk) { - break; - } - node = *--stkptr; - } - - return (node->value < threshold) ? 1 : 0; + opj_tgt_node_t *stk[31]; + opj_tgt_node_t **stkptr; + opj_tgt_node_t *node; + OPJ_INT32 low; + + stkptr = stk; + node = &tree->nodes[leafno]; + while (node->parent) { + *stkptr++ = node; + node = node->parent; + } + + low = 0; + for (;;) { + if (low > node->low) { + node->low = low; + } else { + low = node->low; + } + while (low < threshold && low < node->value) { + if (bio_read(bio, 1)) { + node->value = low; + } else { + ++low; + } + } + node->low = low; + if (stkptr == stk) { + break; + } + node = *--stkptr; + } + + return (node->value < threshold) ? 1 : 0; } diff --git a/tests/test_tile_decoder.c b/tests/test_tile_decoder.c index 84293586..f0f4c952 100644 --- a/tests/test_tile_decoder.c +++ b/tests/test_tile_decoder.c @@ -42,346 +42,349 @@ #include "opj_config.h" #include -#ifdef _WIN32 -#include -#define strcasecmp _stricmp -#define strncasecmp _strnicmp -#else -#include +#ifdef _WIN32 +#include +#define strcasecmp _stricmp +#define strncasecmp _strnicmp +#else +#include #endif /* _WIN32 */ #include "openjpeg.h" #include "format_defs.h" -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ /* Declarations */ -int get_file_format(const char *filename); +int get_file_format(const char *filename); static int infile_format(const char *fname); -/* -------------------------------------------------------------------------- */ -int get_file_format(const char *filename) { - unsigned int i; - static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" }; - static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT }; - char * ext = strrchr(filename, '.'); - if (ext == NULL) - return -1; - ext++; - if(ext) { - for(i = 0; i < sizeof(format)/sizeof(*format); i++) { - if(strcasecmp(ext, extension[i]) == 0) { - return format[i]; - } - } - } - - return -1; +/* -------------------------------------------------------------------------- */ +int get_file_format(const char *filename) { + unsigned int i; + static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" }; + static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT }; + char * ext = strrchr(filename, '.'); + if (ext == NULL) + return -1; + ext++; + if(ext) { + for(i = 0; i < sizeof(format)/sizeof(*format); i++) { + if(strcasecmp(ext, extension[i]) == 0) { + return format[i]; + } + } + } + + return -1; } -/* -------------------------------------------------------------------------- */ -#define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a" -#define JP2_MAGIC "\x0d\x0a\x87\x0a" -/* position 45: "\xff\x52" */ -#define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51" - -static int infile_format(const char *fname) -{ - FILE *reader; - const char *s, *magic_s; - int ext_format, magic_format; - unsigned char buf[12]; - unsigned int l_nb_read; - - reader = fopen(fname, "rb"); - - if (reader == NULL) - return -1; - - memset(buf, 0, 12); - l_nb_read = fread(buf, 1, 12, reader); - fclose(reader); - if (l_nb_read != 12) - return -1; - - ext_format = get_file_format(fname); - - if (ext_format == JPT_CFMT) - return JPT_CFMT; - - if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) { - magic_format = JP2_CFMT; - magic_s = ".jp2"; - } - else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) { - magic_format = J2K_CFMT; - magic_s = ".j2k or .jpc or .j2c"; - } - else - return -1; - - if (magic_format == ext_format) - return ext_format; - - s = fname + strlen(fname) - 4; - - fputs("\n===========================================\n", stderr); - fprintf(stderr, "The extension of this file is incorrect.\n" - "FOUND %s. SHOULD BE %s\n", s, magic_s); - fputs("===========================================\n", stderr); - - return magic_format; +/* -------------------------------------------------------------------------- */ +#define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a" +#define JP2_MAGIC "\x0d\x0a\x87\x0a" +/* position 45: "\xff\x52" */ +#define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51" + +static int infile_format(const char *fname) +{ + FILE *reader; + const char *s, *magic_s; + int ext_format, magic_format; + unsigned char buf[12]; + unsigned int l_nb_read; + + reader = fopen(fname, "rb"); + + if (reader == NULL) + return -1; + + memset(buf, 0, 12); + l_nb_read = fread(buf, 1, 12, reader); + fclose(reader); + if (l_nb_read != 12) + return -1; + + ext_format = get_file_format(fname); + + if (ext_format == JPT_CFMT) + return JPT_CFMT; + + if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) { + magic_format = JP2_CFMT; + magic_s = ".jp2"; + } + else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) { + magic_format = J2K_CFMT; + magic_s = ".j2k or .jpc or .j2c"; + } + else + return -1; + + if (magic_format == ext_format) + return ext_format; + + s = fname + strlen(fname) - 4; + + fputs("\n===========================================\n", stderr); + fprintf(stderr, "The extension of this file is incorrect.\n" + "FOUND %s. SHOULD BE %s\n", s, magic_s); + fputs("===========================================\n", stderr); + + return magic_format; } /* -------------------------------------------------------------------------- */ /** -sample error callback expecting a FILE* client object -*/ + sample error callback expecting a FILE* client object + */ void error_callback_file(const char *msg, void *client_data) { - FILE *stream = (FILE*)client_data; - fprintf(stream, "[ERROR] %s", msg); + FILE *stream = (FILE*)client_data; + fprintf(stream, "[ERROR] %s", msg); } /** -sample warning callback expecting a FILE* client object -*/ + sample warning callback expecting a FILE* client object + */ void warning_callback_file(const char *msg, void *client_data) { - FILE *stream = (FILE*)client_data; - fprintf(stream, "[WARNING] %s", msg); + FILE *stream = (FILE*)client_data; + fprintf(stream, "[WARNING] %s", msg); } /** -sample error debug callback expecting no client object -*/ + sample error debug callback expecting no client object + */ void error_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[ERROR] %s", msg); + (void)client_data; + fprintf(stdout, "[ERROR] %s", msg); } /** -sample warning debug callback expecting no client object -*/ + sample warning debug callback expecting no client object + */ void warning_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[WARNING] %s", msg); + (void)client_data; + fprintf(stdout, "[WARNING] %s", msg); } /** -sample debug callback expecting no client object -*/ + sample debug callback expecting no client object + */ void info_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[INFO] %s", msg); + (void)client_data; + fprintf(stdout, "[INFO] %s", msg); } /* -------------------------------------------------------------------------- */ int main (int argc, char *argv[]) { - opj_dparameters_t l_param; - opj_codec_t * l_codec; - opj_image_t * l_image; - FILE * l_file; - opj_stream_t * l_stream; - OPJ_UINT32 l_data_size; - OPJ_UINT32 l_max_data_size = 1000; - OPJ_UINT32 l_tile_index; - OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000); - opj_bool l_go_on = OPJ_TRUE; - OPJ_INT32 l_tile_x0=0, l_tile_y0=0 ; - OPJ_UINT32 l_tile_width=0, l_tile_height=0, l_nb_tiles_x=0, l_nb_tiles_y=0, l_nb_comps=0 ; - OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1; - - int da_x0=0; - int da_y0=0; - int da_x1=1000; - int da_y1=1000; - char input_file[64]; - - /* should be test_tile_decoder 0 0 1000 1000 tte1.j2k */ - if( argc == 6 ) - { - da_x0=atoi(argv[1]); - da_y0=atoi(argv[2]); - da_x1=atoi(argv[3]); - da_y1=atoi(argv[4]); - strcpy(input_file,argv[5]); - - } - else - { - da_x0=0; - da_y0=0; - da_x1=1000; - da_y1=1000; - strcpy(input_file,"test.j2k"); - } - - if (! l_data) { - return EXIT_FAILURE; - } - - l_file = fopen(input_file,"rb"); - if (! l_file) - { - fprintf(stdout, "ERROR while opening input file\n"); - free(l_data); - return EXIT_FAILURE; - } - - l_stream = opj_stream_create_default_file_stream(l_file,OPJ_TRUE); - if (!l_stream){ - fclose(l_file); - free(l_data); - fprintf(stderr, "ERROR -> failed to create the stream from the file\n"); - return EXIT_FAILURE; - } - - /* Set the default decoding parameters */ - opj_set_default_decoder_parameters(&l_param); - - /* */ - l_param.decod_format = infile_format(input_file); - - /** you may here add custom decoding parameters */ - /* do not use layer decoding limitations */ - l_param.cp_layer = 0; - - /* do not use resolutions reductions */ - l_param.cp_reduce = 0; - - /* to decode only a part of the image data */ - //opj_restrict_decoding(&l_param,0,0,1000,1000); - - - switch(l_param.decod_format) { - case J2K_CFMT: /* JPEG-2000 codestream */ - { - /* Get a decoder handle */ - l_codec = opj_create_decompress(CODEC_J2K); - break; - } - case JP2_CFMT: /* JPEG 2000 compressed image data */ - { - /* Get a decoder handle */ - l_codec = opj_create_decompress(CODEC_JP2); - break; - } - default: - { - fprintf(stderr, "ERROR -> Not a valid JPEG2000 file!\n"); - fclose(l_file); - free(l_data); - opj_stream_destroy(l_stream); - return EXIT_FAILURE; - } - } - - /* catch events using our callbacks and give a local context */ - opj_set_info_handler(l_codec, info_callback,00); - opj_set_warning_handler(l_codec, warning_callback,00); - opj_set_error_handler(l_codec, error_callback,00); - - /* Setup the decoder decoding parameters using user parameters */ - if (! opj_setup_decoder(l_codec, &l_param)) - { - fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n"); - fclose(l_file); - free(l_data); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - return EXIT_FAILURE; - } - - /* Read the main header of the codestream and if necessary the JP2 boxes*/ - if (! opj_read_header(l_stream, l_codec, &l_image)) - { - fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n"); - fclose(l_file); - free(l_data); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - return EXIT_FAILURE; - } + opj_dparameters_t l_param; + opj_codec_t * l_codec; + opj_image_t * l_image; + FILE * l_file; + opj_stream_t * l_stream; + OPJ_UINT32 l_data_size; + OPJ_UINT32 l_max_data_size = 1000; + OPJ_UINT32 l_tile_index; + OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000); + opj_bool l_go_on = OPJ_TRUE; + OPJ_INT32 l_tile_x0=0, l_tile_y0=0 ; + OPJ_UINT32 l_tile_width=0, l_tile_height=0, l_nb_tiles_x=0, l_nb_tiles_y=0, l_nb_comps=0 ; + OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1; + + int da_x0=0; + int da_y0=0; + int da_x1=1000; + int da_y1=1000; + char input_file[64]; + + /* should be test_tile_decoder 0 0 1000 1000 tte1.j2k */ + if( argc == 6 ) + { + da_x0=atoi(argv[1]); + da_y0=atoi(argv[2]); + da_x1=atoi(argv[3]); + da_y1=atoi(argv[4]); + strcpy(input_file,argv[5]); + + } + else + { + da_x0=0; + da_y0=0; + da_x1=1000; + da_y1=1000; + strcpy(input_file,"test.j2k"); + } + + if (! l_data) { + return EXIT_FAILURE; + } + + l_file = fopen(input_file,"rb"); + if (! l_file) + { + fprintf(stdout, "ERROR while opening input file\n"); + free(l_data); + return EXIT_FAILURE; + } - if (!opj_set_decode_area(l_codec, l_image, da_x0, da_y0,da_x1, da_y1)){ - fprintf(stderr, "ERROR -> j2k_to_image: failed to set the decoded area\n"); - fclose(l_file); - free(l_data); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - opj_image_destroy(l_image); - return EXIT_FAILURE; - } - - - while (l_go_on) - { - if (! opj_read_tile_header( l_codec, - l_stream, - &l_tile_index, - &l_data_size, - &l_current_tile_x0, - &l_current_tile_y0, - &l_current_tile_x1, - &l_current_tile_y1, - &l_nb_comps, - &l_go_on)) + l_stream = opj_stream_create_default_file_stream(l_file,OPJ_TRUE); + if (!l_stream){ + fclose(l_file); + free(l_data); + fprintf(stderr, "ERROR -> failed to create the stream from the file\n"); + return EXIT_FAILURE; + } + + /* Set the default decoding parameters */ + opj_set_default_decoder_parameters(&l_param); + + /* */ + l_param.decod_format = infile_format(input_file); + + /** you may here add custom decoding parameters */ + /* do not use layer decoding limitations */ + l_param.cp_layer = 0; + + /* do not use resolutions reductions */ + l_param.cp_reduce = 0; + + /* to decode only a part of the image data */ + //opj_restrict_decoding(&l_param,0,0,1000,1000); + + + switch(l_param.decod_format) { + case J2K_CFMT: /* JPEG-2000 codestream */ + { + /* Get a decoder handle */ + l_codec = opj_create_decompress(CODEC_J2K); + break; + } + case JP2_CFMT: /* JPEG 2000 compressed image data */ + { + /* Get a decoder handle */ + l_codec = opj_create_decompress(CODEC_JP2); + break; + } + default: + { + fprintf(stderr, "ERROR -> Not a valid JPEG2000 file!\n"); + fclose(l_file); + free(l_data); + opj_stream_destroy(l_stream); + return EXIT_FAILURE; + } + } + + /* catch events using our callbacks and give a local context */ + opj_set_info_handler(l_codec, info_callback,00); + opj_set_warning_handler(l_codec, warning_callback,00); + opj_set_error_handler(l_codec, error_callback,00); + + /* Setup the decoder decoding parameters using user parameters */ + if (! opj_setup_decoder(l_codec, &l_param)) { - fclose(l_file); - free(l_data); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - opj_image_destroy(l_image); - return EXIT_FAILURE; - } - - if (l_go_on) + fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n"); + fclose(l_file); + free(l_data); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + return EXIT_FAILURE; + } + + /* Read the main header of the codestream and if necessary the JP2 boxes*/ + if (! opj_read_header(l_stream, l_codec, &l_image)) + { + fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n"); + fclose(l_file); + free(l_data); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + return EXIT_FAILURE; + } + + if (!opj_set_decode_area(l_codec, l_image, da_x0, da_y0,da_x1, da_y1)){ + fprintf(stderr, "ERROR -> j2k_to_image: failed to set the decoded area\n"); + fclose(l_file); + free(l_data); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return EXIT_FAILURE; + } + + + while (l_go_on) { - if (l_data_size > l_max_data_size) - { - l_data = (OPJ_BYTE *) realloc(l_data,l_data_size); - if (! l_data) + if (! opj_read_tile_header( l_codec, + l_stream, + &l_tile_index, + &l_data_size, + &l_current_tile_x0, + &l_current_tile_y0, + &l_current_tile_x1, + &l_current_tile_y1, + &l_nb_comps, + &l_go_on)) + { + fclose(l_file); + free(l_data); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return EXIT_FAILURE; + } + + if (l_go_on) { - fclose(l_file); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - opj_image_destroy(l_image); - return EXIT_FAILURE; - } - l_max_data_size = l_data_size; - } - - if (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream)) - { + if (l_data_size > l_max_data_size) + { + OPJ_BYTE *l_new_data = (OPJ_BYTE *) realloc(l_data, l_data_size); + if (! l_new_data) + { + fclose(l_file); + free(l_new_data); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return EXIT_FAILURE; + } + l_data = l_new_data; + l_max_data_size = l_data_size; + } + + if (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream)) + { + fclose(l_file); + free(l_data); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return EXIT_FAILURE; + } + /** now should inspect image to know the reduction factor and then how to behave with data */ + } + } + + if (! opj_end_decompress(l_codec,l_stream)) + { fclose(l_file); - free(l_data); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - opj_image_destroy(l_image); - return EXIT_FAILURE; - } - /** now should inspect image to know the reduction factor and then how to behave with data */ - } - } - - if (! opj_end_decompress(l_codec,l_stream)) - { + free(l_data); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return EXIT_FAILURE; + } + + /* Free memory */ fclose(l_file); - free(l_data); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - opj_image_destroy(l_image); - return EXIT_FAILURE; - } - - /* Free memory */ - fclose(l_file); - free(l_data); - opj_stream_destroy(l_stream); - opj_destroy_codec(l_codec); - opj_image_destroy(l_image); - - // Print profiling - //PROFPRINT(); - - return EXIT_SUCCESS; + free(l_data); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + + // Print profiling + //PROFPRINT(); + + return EXIT_SUCCESS; } + -- 2.40.0