diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index ee473ce3..1345fa7b 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -88,6 +88,94 @@ typedef struct img_folder{ char set_out_format; }img_fol_t; +/* -------------------------------------------------------------------------- */ +/* Custom allocation functions */ +//#define OPJ_USE_MEMORY_MANAGER +#ifdef OPJ_USE_MEMORY_MANAGER +static void* opj_custom_malloc(OPJ_SIZE_T size, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + + if ((size + 32U) < size) { + return NULL; + } + size += 32U; + + return (void*)(((OPJ_UINT8*)malloc(size)) + 32); +} +static void* opj_custom_calloc(OPJ_SIZE_T num, OPJ_SIZE_T size, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + + if ((size + 32U) < size) { + return NULL; + } + size += 32U; + + return (void*)(((OPJ_UINT8*)calloc(num, size)) + 32); +} +static void* opj_custom_realloc(void* ptr, OPJ_SIZE_T size, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + + if ((size + 32U) < size) { + return NULL; + } + size += 32U; + + if (ptr != NULL) { + ptr = ((OPJ_UINT8*)ptr) - 32; + } + + return (void*)(((OPJ_UINT8*)realloc(ptr, size)) + 32); +} +static void opj_custom_free(void* ptr, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + if (ptr != NULL) { + free(((OPJ_UINT8*)ptr) - 32); + } +} +static void* opj_custom_aligned_malloc(OPJ_SIZE_T size, OPJ_SIZE_T alignment, void *client_data) +{ + void** l_result = NULL; + void** l_original = NULL; + + OPJ_ARG_NOT_USED(client_data); + + if ((alignment == 0) || (alignment & (alignment - 1))) { + return NULL; + } + + if ((size + alignment) < size) { + return NULL; + } + size += alignment; + if ((size + 32U * sizeof(void*)) < size) { + return NULL; + } + size += 32U * sizeof(void*); + + l_original = (void**)malloc(size); + + l_result = l_original + 30U; + + if (alignment > sizeof(void*)) { + l_result = (void**)(((OPJ_SIZE_T)l_result + (alignment - 1)) & -alignment ); + } + l_result[-1] = l_original; + + return (void*)l_result; +} +static void opj_custom_aligned_free(void* ptr, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + if (ptr != NULL) { + free(((void**)ptr)[-1]); + } +} +#endif + static void encode_help_display(void) { fprintf(stdout,"\nThis is the opj_compress utility from the OpenJPEG project.\n" "It compresses various image formats with the JPEG 2000 algorithm.\n" @@ -1569,7 +1657,9 @@ OPJ_FLOAT64 opj_clock(void) { */ /* -------------------------------------------------------------------------- */ int main(int argc, char **argv) { - +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_t l_manager = NULL; +#endif opj_cparameters_t parameters; /* compression parameters */ opj_stream_t *l_stream = 00; @@ -1634,6 +1724,13 @@ int main(int argc, char **argv) { }else{ num_images=1; } +#ifdef OPJ_USE_MEMORY_MANAGER + l_manager = opj_manager_create(NULL, opj_custom_malloc, opj_custom_calloc, opj_custom_realloc, opj_custom_free, opj_custom_aligned_malloc, opj_custom_aligned_free); + if (!l_manager){ + fprintf(stderr, "ERROR -> failed to create memory manager\n"); + return EXIT_FAILURE; + } +#endif /*Encoding image one by one*/ for(imageno=0;imagenonumcomps < 3)){ fprintf(stderr, "RGB->YCC conversion cannot be used:\n"); fprintf(stderr, "Input image has less than 3 components\n"); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif return 1; } if ((parameters.tcp_mct == 2) && (!parameters.mct_data)){ fprintf(stderr, "Custom MCT has been set but no array-based MCT\n"); fprintf(stderr, "has been provided. Aborting.\n"); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif return 1; } } @@ -1771,13 +1901,21 @@ int main(int argc, char **argv) { case J2K_CFMT: /* JPEG-2000 codestream */ { /* Get a decoder handle */ +#ifdef OPJ_USE_MEMORY_MANAGER + l_codec = opj_manager_create_compress(l_manager, OPJ_CODEC_J2K); +#else l_codec = opj_create_compress(OPJ_CODEC_J2K); +#endif break; } case JP2_CFMT: /* JPEG 2000 compressed image data */ { /* Get a decoder handle */ +#ifdef OPJ_USE_MEMORY_MANAGER + l_codec = opj_manager_create_compress(l_manager, OPJ_CODEC_JP2); +#else l_codec = opj_create_compress(OPJ_CODEC_JP2); +#endif break; } default: @@ -1787,9 +1925,15 @@ int main(int argc, char **argv) { } /* catch events using our callbacks and give a local context */ +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_set_info_handler(l_manager, info_callback,00); + opj_manager_set_warning_handler(l_manager, warning_callback,00); + opj_manager_set_error_handler(l_manager, error_callback,00); +#else 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); +#endif if( bUseTiles ) { parameters.cp_tx0 = 0; @@ -1806,8 +1950,15 @@ int main(int argc, char **argv) { } /* open a byte stream for writing and allocate memory for all tiles */ +#ifdef OPJ_USE_MEMORY_MANAGER + l_stream = opj_manager_stream_create_default_file_stream(l_manager, parameters.outfile,OPJ_FALSE); +#else l_stream = opj_stream_create_default_file_stream(parameters.outfile,OPJ_FALSE); +#endif if (! l_stream){ +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif return 1; } @@ -1827,6 +1978,9 @@ int main(int argc, char **argv) { opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif return 1; } } @@ -1847,8 +2001,11 @@ int main(int argc, char **argv) { opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif fprintf(stderr, "failed to encode image\n"); - remove(parameters.outfile); + remove(parameters.outfile); return 1; } @@ -1869,7 +2026,10 @@ int main(int argc, char **argv) { if(parameters.cp_comment) free(parameters.cp_comment); if(parameters.cp_matrice) free(parameters.cp_matrice); if(raw_cp.rawComps) free(raw_cp.rawComps); - +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif + t = opj_clock() - t; if (num_compressed_files) { fprintf(stdout, "encode time: %d ms \n", (int)((t * 1000.0)/(OPJ_FLOAT64)num_compressed_files)); diff --git a/src/bin/jp2/opj_decompress.c b/src/bin/jp2/opj_decompress.c index ac17b69d..8cc63b5f 100644 --- a/src/bin/jp2/opj_decompress.c +++ b/src/bin/jp2/opj_decompress.c @@ -162,6 +162,94 @@ int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsi static opj_image_t* convert_gray_to_rgb(opj_image_t* original); +/* -------------------------------------------------------------------------- */ +/* Custom allocation functions */ +//#define OPJ_USE_MEMORY_MANAGER +#ifdef OPJ_USE_MEMORY_MANAGER +static void* opj_custom_malloc(OPJ_SIZE_T size, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + + if ((size + 32U) < size) { + return NULL; + } + size += 32U; + + return (void*)(((OPJ_UINT8*)malloc(size)) + 32); +} +static void* opj_custom_calloc(OPJ_SIZE_T num, OPJ_SIZE_T size, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + + if ((size + 32U) < size) { + return NULL; + } + size += 32U; + + return (void*)(((OPJ_UINT8*)calloc(num, size)) + 32); +} +static void* opj_custom_realloc(void* ptr, OPJ_SIZE_T size, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + + if ((size + 32U) < size) { + return NULL; + } + size += 32U; + + if (ptr != NULL) { + ptr = ((OPJ_UINT8*)ptr) - 32; + } + + return (void*)(((OPJ_UINT8*)realloc(ptr, size)) + 32); +} +static void opj_custom_free(void* ptr, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + if (ptr != NULL) { + free(((OPJ_UINT8*)ptr) - 32); + } +} +static void* opj_custom_aligned_malloc(OPJ_SIZE_T size, OPJ_SIZE_T alignment, void *client_data) +{ + void** l_result = NULL; + void** l_original = NULL; + + OPJ_ARG_NOT_USED(client_data); + + if ((alignment == 0) || (alignment & (alignment - 1))) { + return NULL; + } + + if ((size + alignment) < size) { + return NULL; + } + size += alignment; + if ((size + 32U * sizeof(void*)) < size) { + return NULL; + } + size += 32U * sizeof(void*); + + l_original = (void**)malloc(size); + + l_result = l_original + 30U; + + if (alignment > sizeof(void*)) { + l_result = (void**)(((OPJ_SIZE_T)l_result + (alignment - 1)) & -alignment ); + } + l_result[-1] = l_original; + + return (void*)l_result; +} +static void opj_custom_aligned_free(void* ptr, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + if (ptr != NULL) { + free(((void**)ptr)[-1]); + } +} +#endif + /* -------------------------------------------------------------------------- */ static void decode_help_display(void) { fprintf(stdout,"\nThis is the opj_decompress utility from the OpenJPEG project.\n" @@ -822,36 +910,6 @@ int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *para return 0; } -/* -------------------------------------------------------------------------- */ -/** - * Parse decoding area input values - * separator = "," - */ -/* -------------------------------------------------------------------------- */ -int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1) -{ - int it = 0; - int values[4]; - char delims[] = ","; - char *result = NULL; - result = strtok( inArg, delims ); - - while( (result != NULL) && (it < 4 ) ) { - values[it] = atoi(result); - result = strtok( NULL, delims ); - it++; - } - - if (it != 4) { - return EXIT_FAILURE; - } - else{ - *DA_x0 = (OPJ_UINT32)values[0]; *DA_y0 = (OPJ_UINT32)values[1]; - *DA_x1 = (OPJ_UINT32)values[2]; *DA_y1 = (OPJ_UINT32)values[3]; - return EXIT_SUCCESS; - } -} - OPJ_FLOAT64 opj_clock(void) { #ifdef _WIN32 /* _WIN32: use QueryPerformance (very accurate) */ @@ -876,69 +934,19 @@ OPJ_FLOAT64 opj_clock(void) { #endif } -/* -------------------------------------------------------------------------- */ - -/** -sample error callback expecting a FILE* client object -*/ -static void error_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[ERROR] %s", msg); -} -/** -sample warning callback expecting a FILE* client object -*/ -static void warning_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[WARNING] %s", msg); -} -/** -sample debug callback expecting no client object -*/ -static void info_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[INFO] %s", msg); -} - -static void set_default_parameters(opj_decompress_parameters* parameters) -{ - if (parameters) { - memset(parameters, 0, sizeof(opj_decompress_parameters)); - - /* default decoding parameters (command line specific) */ - parameters->decod_format = -1; - parameters->cod_format = -1; - - /* default decoding parameters (core) */ - opj_set_default_decoder_parameters(&(parameters->core)); - } -} - -static void destroy_parameters(opj_decompress_parameters* parameters) -{ - if (parameters) { - if (parameters->precision) { - free(parameters->precision); - parameters->precision = NULL; - } - } -} - -/* -------------------------------------------------------------------------- */ - static opj_image_t* convert_gray_to_rgb(opj_image_t* original) { OPJ_UINT32 compno; opj_image_t* l_new_image = NULL; opj_image_cmptparm_t* l_new_components = NULL; - + l_new_components = (opj_image_cmptparm_t*)malloc((original->numcomps + 2U) * sizeof(opj_image_cmptparm_t)); if (l_new_components == NULL) { fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n"); opj_image_destroy(original); return NULL; } - + l_new_components[0].bpp = l_new_components[1].bpp = l_new_components[2].bpp = original->comps[0].bpp; l_new_components[0].dx = l_new_components[1].dx = l_new_components[2].dx = original->comps[0].dx; l_new_components[0].dy = l_new_components[1].dy = l_new_components[2].dy = original->comps[0].dy; @@ -948,7 +956,7 @@ static opj_image_t* convert_gray_to_rgb(opj_image_t* original) l_new_components[0].sgnd = l_new_components[1].sgnd = l_new_components[2].sgnd = original->comps[0].sgnd; l_new_components[0].x0 = l_new_components[1].x0 = l_new_components[2].x0 = original->comps[0].x0; l_new_components[0].y0 = l_new_components[1].y0 = l_new_components[2].y0 = original->comps[0].y0; - + for(compno = 1U; compno < original->numcomps; ++compno) { l_new_components[compno+2U].bpp = original->comps[compno].bpp; l_new_components[compno+2U].dx = original->comps[compno].dx; @@ -960,7 +968,7 @@ static opj_image_t* convert_gray_to_rgb(opj_image_t* original) l_new_components[compno+2U].x0 = original->comps[compno].x0; l_new_components[compno+2U].y0 = original->comps[compno].y0; } - + l_new_image = opj_image_create(original->numcomps + 2U, l_new_components, OPJ_CLRSPC_SRGB); free(l_new_components); if (l_new_image == NULL) { @@ -968,20 +976,20 @@ static opj_image_t* convert_gray_to_rgb(opj_image_t* original) opj_image_destroy(original); return NULL; } - + l_new_image->x0 = original->x0; l_new_image->x1 = original->x1; l_new_image->y0 = original->y0; l_new_image->y1 = original->y1; - + l_new_image->comps[0].factor = l_new_image->comps[1].factor = l_new_image->comps[2].factor = original->comps[0].factor; l_new_image->comps[0].alpha = l_new_image->comps[1].alpha = l_new_image->comps[2].alpha = original->comps[0].alpha; l_new_image->comps[0].resno_decoded = l_new_image->comps[1].resno_decoded = l_new_image->comps[2].resno_decoded = original->comps[0].resno_decoded; - + memcpy(l_new_image->comps[0].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32)); memcpy(l_new_image->comps[1].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32)); memcpy(l_new_image->comps[2].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32)); - + for(compno = 1U; compno < original->numcomps; ++compno) { l_new_image->comps[compno+2U].factor = original->comps[compno].factor; l_new_image->comps[compno+2U].alpha = original->comps[compno].alpha; @@ -1022,11 +1030,11 @@ static opj_image_t* upsample_image_components(opj_image_t* original) opj_image_destroy(original); return NULL; } - + for (compno = 0U; compno < original->numcomps; ++compno) { opj_image_cmptparm_t* l_new_cmp = &(l_new_components[compno]); opj_image_comp_t* l_org_cmp = &(original->comps[compno]); - + l_new_cmp->bpp = l_org_cmp->bpp; l_new_cmp->prec = l_org_cmp->prec; l_new_cmp->sgnd = l_org_cmp->sgnd; @@ -1035,17 +1043,17 @@ static opj_image_t* upsample_image_components(opj_image_t* original) l_new_cmp->dx = 1; l_new_cmp->dy = 1; l_new_cmp->w = l_org_cmp->w; /* should be original->x1 - original->x0 for dx==1 */ - l_new_cmp->h = l_org_cmp->h; /* should be original->y1 - original->y0 for dy==0 */ - + l_new_cmp->h = l_org_cmp->h; /* should be original->y1 - original->y0 for dy==1 */ + if (l_org_cmp->dx > 1U) { l_new_cmp->w = original->x1 - original->x0; } - + if (l_org_cmp->dy > 1U) { l_new_cmp->h = original->y1 - original->y0; } } - + l_new_image = opj_image_create(original->numcomps, l_new_components, original->color_space); free(l_new_components); if (l_new_image == NULL) { @@ -1053,26 +1061,26 @@ static opj_image_t* upsample_image_components(opj_image_t* original) opj_image_destroy(original); return NULL; } - + l_new_image->x0 = original->x0; l_new_image->x1 = original->x1; l_new_image->y0 = original->y0; l_new_image->y1 = original->y1; - + for (compno = 0U; compno < original->numcomps; ++compno) { opj_image_comp_t* l_new_cmp = &(l_new_image->comps[compno]); opj_image_comp_t* l_org_cmp = &(original->comps[compno]); - + l_new_cmp->factor = l_org_cmp->factor; l_new_cmp->alpha = l_org_cmp->alpha; l_new_cmp->resno_decoded = l_org_cmp->resno_decoded; - + if ((l_org_cmp->dx > 1U) || (l_org_cmp->dy > 1U)) { const OPJ_INT32* l_src = l_org_cmp->data; OPJ_INT32* l_dst = l_new_cmp->data; OPJ_UINT32 y; OPJ_UINT32 xoff, yoff; - + /* need to take into account dx & dy */ xoff = l_org_cmp->dx * l_org_cmp->x0 - original->x0; yoff = l_org_cmp->dy * l_org_cmp->y0 - original->y0; @@ -1082,17 +1090,17 @@ static opj_image_t* upsample_image_components(opj_image_t* original) opj_image_destroy(l_new_image); return NULL; } - + for (y = 0U; y < yoff; ++y) { memset(l_dst, 0U, l_new_cmp->w * sizeof(OPJ_INT32)); l_dst += l_new_cmp->w; } - + if(l_new_cmp->h > (l_org_cmp->dy - 1U)) { /* check substraction overflow for really small images */ for (; y < l_new_cmp->h - (l_org_cmp->dy - 1U); y += l_org_cmp->dy) { OPJ_UINT32 x, dy; OPJ_UINT32 xorg; - + xorg = 0U; for (x = 0U; x < xoff; ++x) { l_dst[x] = 0; @@ -1109,7 +1117,7 @@ static opj_image_t* upsample_image_components(opj_image_t* original) l_dst[x] = l_src[xorg]; } l_dst += l_new_cmp->w; - + for (dy = 1U; dy < l_org_cmp->dy; ++dy) { memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32)); l_dst += l_new_cmp->w; @@ -1120,7 +1128,7 @@ static opj_image_t* upsample_image_components(opj_image_t* original) if (y < l_new_cmp->h) { OPJ_UINT32 x; OPJ_UINT32 xorg; - + xorg = 0U; for (x = 0U; x < xoff; ++x) { l_dst[x] = 0; @@ -1152,6 +1160,84 @@ static opj_image_t* upsample_image_components(opj_image_t* original) return l_new_image; } +/* -------------------------------------------------------------------------- */ +/** + * Parse decoding area input values + * separator = "," + */ +/* -------------------------------------------------------------------------- */ +int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1) +{ + int it = 0; + int values[4]; + char delims[] = ","; + char *result = NULL; + result = strtok( inArg, delims ); + + while( (result != NULL) && (it < 4 ) ) { + values[it] = atoi(result); + result = strtok( NULL, delims ); + it++; + } + + if (it != 4) { + return EXIT_FAILURE; + } + else{ + *DA_x0 = (OPJ_UINT32)values[0]; *DA_y0 = (OPJ_UINT32)values[1]; + *DA_x1 = (OPJ_UINT32)values[2]; *DA_y1 = (OPJ_UINT32)values[3]; + return EXIT_SUCCESS; + } +} + +/* -------------------------------------------------------------------------- */ + +/** +sample error callback expecting a FILE* client object +*/ +static void error_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[ERROR] %s", msg); +} +/** +sample warning callback expecting a FILE* client object +*/ +static void warning_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[WARNING] %s", msg); +} +/** +sample debug callback expecting no client object +*/ +static void info_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[INFO] %s", msg); +} + +static void set_default_parameters(opj_decompress_parameters* parameters) +{ + if (parameters) { + memset(parameters, 0, sizeof(opj_decompress_parameters)); + + /* default decoding parameters (command line specific) */ + parameters->decod_format = -1; + parameters->cod_format = -1; + + /* default decoding parameters (core) */ + opj_set_default_decoder_parameters(&(parameters->core)); + } +} + +static void destroy_parameters(opj_decompress_parameters* parameters) +{ + if (parameters) { + if (parameters->precision) { + free(parameters->precision); + parameters->precision = NULL; + } + } +} + /* -------------------------------------------------------------------------- */ /** * OPJ_DECOMPRESS MAIN @@ -1159,6 +1245,9 @@ static opj_image_t* upsample_image_components(opj_image_t* original) /* -------------------------------------------------------------------------- */ int main(int argc, char **argv) { +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_t l_manager = NULL; +#endif opj_decompress_parameters parameters; /* decompression parameters */ opj_image_t* image = NULL; opj_stream_t *l_stream = NULL; /* Stream */ @@ -1220,6 +1309,15 @@ int main(int argc, char **argv) num_images=1; } +#ifdef OPJ_USE_MEMORY_MANAGER + l_manager = opj_manager_create(NULL, opj_custom_malloc, opj_custom_calloc, opj_custom_realloc, opj_custom_free, opj_custom_aligned_malloc, opj_custom_aligned_free); + if (!l_manager){ + fprintf(stderr, "ERROR -> failed to create memory manager\n"); + destroy_parameters(¶meters); + return EXIT_FAILURE; + } +#endif + /*Decoding image one by one*/ for(imageno = 0; imageno < num_images ; imageno++) { @@ -1235,10 +1333,16 @@ int main(int argc, char **argv) /* read the input file and put it in memory */ /* ---------------------------------------- */ - +#ifdef OPJ_USE_MEMORY_MANAGER + l_stream = opj_manager_stream_create_default_file_stream(l_manager, parameters.infile,1); +#else l_stream = opj_stream_create_default_file_stream(parameters.infile,1); +#endif if (!l_stream){ fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif destroy_parameters(¶meters); return EXIT_FAILURE; } @@ -1250,19 +1354,31 @@ int main(int argc, char **argv) case J2K_CFMT: /* JPEG-2000 codestream */ { /* Get a decoder handle */ +#ifdef OPJ_USE_MEMORY_MANAGER + l_codec = opj_manager_create_decompress(l_manager, OPJ_CODEC_J2K); +#else l_codec = opj_create_decompress(OPJ_CODEC_J2K); +#endif break; } case JP2_CFMT: /* JPEG 2000 compressed image data */ { /* Get a decoder handle */ +#ifdef OPJ_USE_MEMORY_MANAGER + l_codec = opj_manager_create_decompress(l_manager, OPJ_CODEC_JP2); +#else l_codec = opj_create_decompress(OPJ_CODEC_JP2); +#endif break; } case JPT_CFMT: /* JPEG 2000, JPIP */ { /* Get a decoder handle */ +#ifdef OPJ_USE_MEMORY_MANAGER + l_codec = opj_manager_create_decompress(l_manager, OPJ_CODEC_JPT); +#else l_codec = opj_create_decompress(OPJ_CODEC_JPT); +#endif break; } default: @@ -1272,10 +1388,16 @@ int main(int argc, char **argv) continue; } - /* catch events using our callbacks and give a local context */ + /* catch events using our callbacks and give a local context */ +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_set_info_handler(l_manager, info_callback,00); + opj_manager_set_warning_handler(l_manager, warning_callback,00); + opj_manager_set_error_handler(l_manager, error_callback,00); +#else 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); +#endif t = opj_clock(); @@ -1285,6 +1407,9 @@ int main(int argc, char **argv) destroy_parameters(¶meters); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif return EXIT_FAILURE; } @@ -1295,7 +1420,13 @@ int main(int argc, char **argv) destroy_parameters(¶meters); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); + +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_image_destroy(l_manager, image); + opj_manager_destroy(l_manager); +#else opj_image_destroy(image); +#endif return EXIT_FAILURE; } @@ -1307,7 +1438,12 @@ int main(int argc, char **argv) destroy_parameters(¶meters); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_image_destroy(l_manager, image); + opj_manager_destroy(l_manager); +#else opj_image_destroy(image); +#endif return EXIT_FAILURE; } @@ -1317,7 +1453,12 @@ int main(int argc, char **argv) destroy_parameters(¶meters); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_image_destroy(l_manager, image); + opj_manager_destroy(l_manager); +#else opj_image_destroy(image); +#endif return EXIT_FAILURE; } } @@ -1337,7 +1478,12 @@ int main(int argc, char **argv) destroy_parameters(¶meters); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_image_destroy(l_manager, image); + opj_manager_destroy(l_manager); +#else opj_image_destroy(image); +#endif return EXIT_FAILURE; } fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index); @@ -1373,7 +1519,11 @@ int main(int argc, char **argv) else color_apply_conversion(image); #endif +#ifdef OPJ_USE_MEMORY_MANAGER + opj_custom_free(image->icc_profile_buf, NULL); +#else free(image->icc_profile_buf); +#endif image->icc_profile_buf = NULL; image->icc_profile_len = 0; } @@ -1419,6 +1569,9 @@ int main(int argc, char **argv) fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n"); destroy_parameters(¶meters); opj_destroy_codec(l_codec); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif return EXIT_FAILURE; } } @@ -1443,6 +1596,9 @@ int main(int argc, char **argv) fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n"); destroy_parameters(¶meters); opj_destroy_codec(l_codec); +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif return EXIT_FAILURE; } } @@ -1545,13 +1701,25 @@ int main(int argc, char **argv) /* free image data structure */ +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_image_destroy(l_manager, image); +#else opj_image_destroy(image); +#endif /* destroy the codestream index */ +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy_cstr_index(l_manager, &cstr_index); +#else opj_destroy_cstr_index(&cstr_index); +#endif + if(failed) remove(parameters.outfile); } +#ifdef OPJ_USE_MEMORY_MANAGER + opj_manager_destroy(l_manager); +#endif destroy_parameters(¶meters); if (numDecompressedImages) { fprintf(stdout, "decode time: %d ms\n", (int)( (tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages)); diff --git a/src/lib/openjp2/CMakeLists.txt b/src/lib/openjp2/CMakeLists.txt index 3129bf53..be885e85 100644 --- a/src/lib/openjp2/CMakeLists.txt +++ b/src/lib/openjp2/CMakeLists.txt @@ -33,6 +33,7 @@ set(OPENJPEG_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg.h ${CMAKE_CURRENT_SOURCE_DIR}/opj_clock.c ${CMAKE_CURRENT_SOURCE_DIR}/opj_clock.h + ${CMAKE_CURRENT_SOURCE_DIR}/opj_manager.c ${CMAKE_CURRENT_SOURCE_DIR}/pi.c ${CMAKE_CURRENT_SOURCE_DIR}/pi.h ${CMAKE_CURRENT_SOURCE_DIR}/raw.c diff --git a/src/lib/openjp2/bio.c b/src/lib/openjp2/bio.c index e4edb372..ef714825 100644 --- a/src/lib/openjp2/bio.c +++ b/src/lib/openjp2/bio.c @@ -120,14 +120,14 @@ static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio) { ========================================================== */ -opj_bio_t* opj_bio_create(void) { - opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t)); +opj_bio_t* opj_bio_create(opj_manager_t manager) { + opj_bio_t *bio = (opj_bio_t*)opj_manager_malloc(manager, sizeof(opj_bio_t)); return bio; } -void opj_bio_destroy(opj_bio_t *bio) { +void opj_bio_destroy(opj_manager_t manager, opj_bio_t *bio) { if(bio) { - opj_free(bio); + opj_manager_free(manager, bio); } } diff --git a/src/lib/openjp2/bio.h b/src/lib/openjp2/bio.h index fba24284..822d334a 100644 --- a/src/lib/openjp2/bio.h +++ b/src/lib/openjp2/bio.h @@ -73,12 +73,12 @@ typedef struct opj_bio { Create a new BIO handle @return Returns a new BIO handle if successful, returns NULL otherwise */ -opj_bio_t* opj_bio_create(void); +opj_bio_t* opj_bio_create(opj_manager_t manager); /** Destroy a previously created BIO handle @param bio BIO handle to destroy */ -void opj_bio_destroy(opj_bio_t *bio); +void opj_bio_destroy(opj_manager_t manager, opj_bio_t *bio); /** Number of bytes written. @param bio BIO handle diff --git a/src/lib/openjp2/cio.c b/src/lib/openjp2/cio.c index b2f6405f..ed14145c 100644 --- a/src/lib/openjp2/cio.c +++ b/src/lib/openjp2/cio.c @@ -150,17 +150,32 @@ void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value) opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size,OPJ_BOOL l_is_input) { - opj_stream_private_t * l_stream = 00; - l_stream = (opj_stream_private_t*) opj_calloc(1,sizeof(opj_stream_private_t)); - if (! l_stream) { - return 00; + opj_manager_t l_manager = opj_manager_get_global_manager(); + + return opj_manager_stream_create(l_manager, p_buffer_size, l_is_input); +} + +OPJ_API opj_stream_t* OPJ_CALLCONV opj_manager_stream_create(opj_manager_t manager, OPJ_SIZE_T p_buffer_size, OPJ_BOOL l_is_input) +{ + opj_stream_private_t * l_stream = NULL; + + if (manager == NULL) { + return NULL; } + l_stream = (opj_stream_private_t*) opj_manager_calloc(manager, 1, sizeof(opj_stream_private_t)); + if (! l_stream) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for stream."); + return NULL; + } + l_stream->m_manager = manager; + l_stream->m_buffer_size = p_buffer_size; - l_stream->m_stored_data = (OPJ_BYTE *) opj_malloc(p_buffer_size); - if (! l_stream->m_stored_data) { - opj_free(l_stream); - return 00; + l_stream->m_stored_data = (OPJ_BYTE *) opj_manager_malloc(manager, p_buffer_size); + if (l_stream->m_stored_data == NULL) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for stream."); + opj_manager_free(manager, l_stream); + return NULL; } l_stream->m_current_data = l_stream->m_stored_data; @@ -186,20 +201,28 @@ opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size,OPJ_BOOL l opj_stream_t* OPJ_CALLCONV opj_stream_default_create(OPJ_BOOL l_is_input) { - return opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,l_is_input); + opj_manager_t l_manager = opj_manager_get_global_manager(); + + return opj_manager_stream_default_create(l_manager, l_is_input); +} +OPJ_API opj_stream_t* OPJ_CALLCONV opj_manager_stream_default_create(opj_manager_t manager, OPJ_BOOL l_is_input) +{ + return opj_manager_stream_create(manager, OPJ_J2K_STREAM_CHUNK_SIZE,l_is_input); } void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream) { opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; - + if (l_stream) { + opj_manager_t l_manager = l_stream->m_manager; + if (l_stream->m_free_user_data_fn) { l_stream->m_free_user_data_fn(l_stream->m_user_data); } - opj_free(l_stream->m_stored_data); + opj_manager_free(l_manager, l_stream->m_stored_data); l_stream->m_stored_data = 00; - opj_free(l_stream); + opj_manager_free(l_manager, l_stream); } } diff --git a/src/lib/openjp2/cio.h b/src/lib/openjp2/cio.h index 6dfa5bb8..424fef43 100644 --- a/src/lib/openjp2/cio.h +++ b/src/lib/openjp2/cio.h @@ -165,6 +165,10 @@ typedef struct opj_stream_private */ opj_stream_flag m_status; + /** + * OpenJpeg Manager (memory/event) + */ + opj_manager_t m_manager; } opj_stream_private_t; diff --git a/src/lib/openjp2/dwt.c b/src/lib/openjp2/dwt.c index 4ad99ed9..6dd59c9f 100644 --- a/src/lib/openjp2/dwt.c +++ b/src/lib/openjp2/dwt.c @@ -124,9 +124,9 @@ static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_st /** Inverse wavelet transform in 2-D. */ -static OPJ_BOOL opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn); +static OPJ_BOOL opj_dwt_decode_tile(opj_manager_t manager, opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn); -static OPJ_BOOL opj_dwt_encode_procedure( opj_tcd_tilecomp_t * tilec, +static OPJ_BOOL opj_dwt_encode_procedure( opj_manager_t manager, opj_tcd_tilecomp_t * tilec, void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) ); static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i); @@ -385,7 +385,7 @@ static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_st /* */ /* Forward 5-3 wavelet transform in 2-D. */ /* */ -static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) ) +static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_manager_t manager, opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) ) { OPJ_INT32 i, j, k; OPJ_INT32 *a = 00; @@ -408,7 +408,7 @@ static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void l_last_res = l_cur_res - 1; l_data_size = opj_dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * (OPJ_UINT32)sizeof(OPJ_INT32); - bj = (OPJ_INT32*)opj_malloc((size_t)l_data_size); + bj = (OPJ_INT32*)opj_manager_malloc(manager, (size_t)l_data_size); if (! bj) { return OPJ_FALSE; } @@ -457,22 +457,22 @@ static INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void --l_last_res; } - opj_free(bj); + opj_manager_free(manager, bj); return OPJ_TRUE; } /* Forward 5-3 wavelet transform in 2-D. */ /* */ -OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec) +OPJ_BOOL opj_dwt_encode(opj_manager_t manager, opj_tcd_tilecomp_t * tilec) { - return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1); + return opj_dwt_encode_procedure(manager, tilec,opj_dwt_encode_1); } /* */ /* Inverse 5-3 wavelet transform in 2-D. */ /* */ -OPJ_BOOL opj_dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) { - return opj_dwt_decode_tile(tilec, numres, &opj_dwt_decode_1); +OPJ_BOOL opj_dwt_decode(opj_manager_t manager, opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) { + return opj_dwt_decode_tile(manager, tilec, numres, &opj_dwt_decode_1); } @@ -497,9 +497,9 @@ OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) { /* */ /* Forward 9-7 wavelet transform in 2-D. */ /* */ -OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec) +OPJ_BOOL opj_dwt_encode_real(opj_manager_t manager, opj_tcd_tilecomp_t * tilec) { - return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1_real); + return opj_dwt_encode_procedure(manager, tilec,opj_dwt_encode_1_real); } /* */ @@ -557,7 +557,7 @@ static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_U /* */ /* Inverse wavelet transform in 2-D. */ /* */ -static OPJ_BOOL opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) { +static OPJ_BOOL opj_dwt_decode_tile(opj_manager_t manager, opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) { opj_dwt_t h; opj_dwt_t v; @@ -569,7 +569,7 @@ static OPJ_BOOL opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0); h.mem = (OPJ_INT32*) - opj_aligned_malloc(opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32)); + opj_manager_aligned_malloc(manager, opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32), 16); if (! h.mem){ /* FIXME event manager error callback */ return OPJ_FALSE; @@ -609,7 +609,7 @@ static OPJ_BOOL opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres } } } - opj_aligned_free(h.mem); + opj_manager_aligned_free(manager, h.mem); return OPJ_TRUE; } @@ -830,7 +830,7 @@ void opj_v4dwt_decode(opj_v4dwt_t* restrict dwt) /* */ /* Inverse 9-7 wavelet transform in 2-D. */ /* */ -OPJ_BOOL opj_dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numres) +OPJ_BOOL opj_dwt_decode_real(opj_manager_t manager, opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numres) { opj_v4dwt_t h; opj_v4dwt_t v; @@ -842,7 +842,7 @@ OPJ_BOOL opj_dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numr OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0); - h.wavelet = (opj_v4_t*) opj_aligned_malloc((opj_dwt_max_resolution(res, numres)+5) * sizeof(opj_v4_t)); + h.wavelet = (opj_v4_t*) opj_manager_aligned_malloc(manager, (opj_dwt_max_resolution(res, numres)+5) * sizeof(opj_v4_t), 16); if (!h.wavelet) { /* FIXME event manager error callback */ return OPJ_FALSE; @@ -925,6 +925,6 @@ OPJ_BOOL opj_dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numr } } - opj_aligned_free(h.wavelet); + opj_manager_aligned_free(manager, h.wavelet); return OPJ_TRUE; } diff --git a/src/lib/openjp2/dwt.h b/src/lib/openjp2/dwt.h index f8b57bc0..0d7d3db0 100644 --- a/src/lib/openjp2/dwt.h +++ b/src/lib/openjp2/dwt.h @@ -58,7 +58,7 @@ Forward 5-3 wavelet tranform in 2-D. Apply a reversible DWT transform to a component of an image. @param tilec Tile component information (current tile) */ -OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec); +OPJ_BOOL opj_dwt_encode(opj_manager_t manager, opj_tcd_tilecomp_t * tilec); /** Inverse 5-3 wavelet tranform in 2-D. @@ -66,7 +66,7 @@ Apply a reversible inverse DWT transform to a component of an image. @param tilec Tile component information (current tile) @param numres Number of resolution levels to decode */ -OPJ_BOOL opj_dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres); +OPJ_BOOL opj_dwt_decode(opj_manager_t manager, opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres); /** Get the gain of a subband for the reversible 5-3 DWT. @@ -86,14 +86,14 @@ Forward 9-7 wavelet transform in 2-D. Apply an irreversible DWT transform to a component of an image. @param tilec Tile component information (current tile) */ -OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec); +OPJ_BOOL opj_dwt_encode_real(opj_manager_t manager, opj_tcd_tilecomp_t * tilec); /** Inverse 9-7 wavelet transform in 2-D. Apply an irreversible inverse DWT transform to a component of an image. @param tilec Tile component information (current tile) @param numres Number of resolution levels to decode */ -OPJ_BOOL opj_dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numres); +OPJ_BOOL opj_dwt_decode_real(opj_manager_t manager, opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numres); /** Get the gain of a subband for the irreversible 9-7 DWT. diff --git a/src/lib/openjp2/function_list.c b/src/lib/openjp2/function_list.c index a7ea11d5..0432ac26 100644 --- a/src/lib/openjp2/function_list.c +++ b/src/lib/openjp2/function_list.c @@ -36,82 +36,95 @@ */ #define OPJ_VALIDATION_SIZE 10 -opj_procedure_list_t * opj_procedure_list_create() +opj_procedure_list_t * opj_procedure_list_create(opj_manager_t manager) { - /* memory allocation */ - opj_procedure_list_t * l_validation = (opj_procedure_list_t *) opj_calloc(1,sizeof(opj_procedure_list_t)); - if (! l_validation) - { - return 00; - } - /* initialization */ - l_validation->m_nb_max_procedures = OPJ_VALIDATION_SIZE; - l_validation->m_procedures = (opj_procedure*)opj_calloc(OPJ_VALIDATION_SIZE, sizeof(opj_procedure)); - if (! l_validation->m_procedures) - { - opj_free(l_validation); - return 00; - } - return l_validation; + opj_procedure_list_t * l_validation = NULL; + + /* preconditions */ + assert(manager != NULL); + + /* memory allocation */ + l_validation = (opj_procedure_list_t *) opj_manager_calloc(manager, 1, sizeof(opj_procedure_list_t)); + if (l_validation == NULL) + { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for procedure list."); + return NULL; + } + /* initialization */ + l_validation->m_manager = manager; + l_validation->m_nb_max_procedures = OPJ_VALIDATION_SIZE; + l_validation->m_procedures = (opj_procedure*)opj_manager_calloc(manager, OPJ_VALIDATION_SIZE, sizeof(opj_procedure)); + if (l_validation->m_procedures == NULL) + { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for procedure list."); + opj_manager_free(manager, l_validation); + return NULL; + } + return l_validation; } 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); + opj_manager_t l_manager = NULL; + if (p_list == NULL) + { + return; + } + + /* preconditions */ + assert(p_list->m_manager != NULL); + + /* initialization */ + l_manager = p_list->m_manager; + if (p_list->m_procedures) + { + opj_manager_free(l_manager, p_list->m_procedures); + } + opj_manager_free(l_manager, p_list); } OPJ_BOOL opj_procedure_list_add_procedure (opj_procedure_list_t * p_validation_list, opj_procedure p_procedure, opj_event_mgr_t* p_manager ) { + /* preconditions */ + assert(p_validation_list != NULL); + assert(p_validation_list->m_manager != NULL); - assert(p_manager != NULL); - - 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; - 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; - opj_event_msg(p_manager, EVT_ERROR, "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[p_validation_list->m_nb_procedures] = p_procedure; - ++p_validation_list->m_nb_procedures; + p_validation_list->m_nb_max_procedures += OPJ_VALIDATION_SIZE; + new_procedures = (opj_procedure*)opj_manager_realloc(p_validation_list->m_manager, + p_validation_list->m_procedures, + p_validation_list->m_nb_max_procedures * sizeof(opj_procedure)); + if (new_procedures == NULL) + { + opj_manager_free(p_validation_list->m_manager, p_validation_list->m_procedures); + p_validation_list->m_nb_max_procedures = 0; + p_validation_list->m_nb_procedures = 0; + opj_event_msg(&(p_validation_list->m_manager->event_mgr), EVT_ERROR, "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[p_validation_list->m_nb_procedures] = p_procedure; + ++p_validation_list->m_nb_procedures; - return OPJ_TRUE; + return OPJ_TRUE; } 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; } 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; } 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/src/lib/openjp2/function_list.h b/src/lib/openjp2/function_list.h index ab091b7b..af978861 100644 --- a/src/lib/openjp2/function_list.h +++ b/src/lib/openjp2/function_list.h @@ -56,6 +56,11 @@ typedef void (*opj_procedure)(void); */ typedef struct opj_procedure_list { + /** + * The memory manager. + */ + opj_manager_t m_manager; + /** * The number of validation procedures. */ @@ -76,9 +81,11 @@ typedef struct opj_procedure_list /** * Creates a validation list. * + * @param manager OpenJpeg memory/event manager + * * @return the newly created validation list. */ -opj_procedure_list_t * opj_procedure_list_create(void); +opj_procedure_list_t * opj_procedure_list_create(opj_manager_t manager); /** * Destroys a validation list. diff --git a/src/lib/openjp2/image.c b/src/lib/openjp2/image.c index 23462f05..7725c8bd 100644 --- a/src/lib/openjp2/image.c +++ b/src/lib/openjp2/image.c @@ -31,53 +31,86 @@ #include "opj_includes.h" -opj_image_t* opj_image_create0(void) { - opj_image_t *image = (opj_image_t*)opj_calloc(1, sizeof(opj_image_t)); +opj_image_t* opj_image_create0(opj_manager_t manager) { + opj_image_t *image = NULL; + + /* preconditions */ + assert(manager != NULL); + + image = (opj_image_t*)opj_manager_calloc(manager, 1, sizeof(opj_image_t)); + if (image == NULL) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for image."); + return NULL; + } return image; } -opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) { +opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) +{ + opj_manager_t l_manager = opj_manager_get_global_manager(); + + return opj_manager_image_create(l_manager, numcmpts, cmptparms, clrspc); +} + +OPJ_API opj_image_t* OPJ_CALLCONV opj_manager_image_create(opj_manager_t manager, OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) +{ OPJ_UINT32 compno; opj_image_t *image = NULL; - image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t)); - if(image) { - image->color_space = clrspc; - image->numcomps = numcmpts; - /* allocate memory for the per-component information */ - image->comps = (opj_image_comp_t*)opj_calloc(1,image->numcomps * sizeof(opj_image_comp_t)); - if(!image->comps) { - /* TODO replace with event manager, breaks API */ - /* fprintf(stderr,"Unable to allocate memory for image.\n"); */ - opj_image_destroy(image); + if (manager == NULL) { + return NULL; + } + + image = (opj_image_t*) opj_manager_calloc(manager, 1, sizeof(opj_image_t)); + if (image == NULL) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for image."); + return NULL; + } + image->color_space = clrspc; + image->numcomps = numcmpts; + /* allocate memory for the per-component information */ + image->comps = (opj_image_comp_t*)opj_manager_calloc(manager, 1,image->numcomps * sizeof(opj_image_comp_t)); + if(!image->comps) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for image."); + opj_manager_image_destroy(manager, image); + return NULL; + } + /* create the individual image components */ + for(compno = 0; compno < numcmpts; compno++) { + opj_image_comp_t *comp = &image->comps[compno]; + comp->dx = cmptparms[compno].dx; + comp->dy = cmptparms[compno].dy; + comp->w = cmptparms[compno].w; + comp->h = cmptparms[compno].h; + comp->x0 = cmptparms[compno].x0; + comp->y0 = cmptparms[compno].y0; + comp->prec = cmptparms[compno].prec; + comp->bpp = cmptparms[compno].bpp; + comp->sgnd = cmptparms[compno].sgnd; + comp->data = (OPJ_INT32*) opj_manager_calloc(manager, comp->w * comp->h, sizeof(OPJ_INT32)); + if(!comp->data) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for image."); + opj_manager_image_destroy(manager, image); return NULL; } - /* create the individual image components */ - for(compno = 0; compno < numcmpts; compno++) { - opj_image_comp_t *comp = &image->comps[compno]; - comp->dx = cmptparms[compno].dx; - comp->dy = cmptparms[compno].dy; - comp->w = cmptparms[compno].w; - comp->h = cmptparms[compno].h; - comp->x0 = cmptparms[compno].x0; - comp->y0 = cmptparms[compno].y0; - comp->prec = cmptparms[compno].prec; - comp->bpp = cmptparms[compno].bpp; - comp->sgnd = cmptparms[compno].sgnd; - comp->data = (OPJ_INT32*) opj_calloc(comp->w * comp->h, sizeof(OPJ_INT32)); - if(!comp->data) { - /* TODO replace with event manager, breaks API */ - /* fprintf(stderr,"Unable to allocate memory for image.\n"); */ - opj_image_destroy(image); - return NULL; - } - } } return image; } -void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) { +void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) +{ + opj_manager_t l_manager = opj_manager_get_global_manager(); + + opj_manager_image_destroy(l_manager, image); +} + +OPJ_API void OPJ_CALLCONV opj_manager_image_destroy(opj_manager_t manager, opj_image_t *image) +{ + if (manager == NULL) { + return; + } + if(image) { if(image->comps) { OPJ_UINT32 compno; @@ -86,17 +119,16 @@ void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) { for(compno = 0; compno < image->numcomps; compno++) { opj_image_comp_t *image_comp = &(image->comps[compno]); if(image_comp->data) { - opj_free(image_comp->data); + opj_manager_free(manager, image_comp->data); } } - opj_free(image->comps); + opj_manager_free(manager, image->comps); } if(image->icc_profile_buf) { - opj_free(image->icc_profile_buf); + opj_manager_free(manager, image->icc_profile_buf); } - - opj_free(image); + opj_manager_free(manager, image); } } @@ -141,15 +173,17 @@ void opj_image_comp_header_update(opj_image_t * p_image_header, const struct opj * Copy only header of image and its component header (no data are copied) * if dest image have data, they will be freed * - * @param p_image_src the src image - * @param p_image_dest the dest image - * - */ -void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_dest) + * @param manager OpenJpeg memory/event manager + * @param p_image_src the src image + * @param p_image_dest the dest image + * @return OPJ_TRUE on success, OPJ_FALSE on failure + * */ +OPJ_BOOL opj_copy_image_header(opj_manager_t manager, const opj_image_t* p_image_src, opj_image_t* p_image_dest) { OPJ_UINT32 compno; /* preconditions */ + assert(manager != NULL); assert(p_image_src != 00); assert(p_image_dest != 00); @@ -162,26 +196,25 @@ void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_ for(compno = 0; compno < p_image_dest->numcomps; compno++) { opj_image_comp_t *image_comp = &(p_image_dest->comps[compno]); if(image_comp->data) { - opj_free(image_comp->data); + opj_manager_free(manager, image_comp->data); } } - opj_free(p_image_dest->comps); + opj_manager_free(manager, p_image_dest->comps); p_image_dest->comps = NULL; } p_image_dest->numcomps = p_image_src->numcomps; - p_image_dest->comps = (opj_image_comp_t*) opj_malloc(p_image_dest->numcomps * sizeof(opj_image_comp_t)); - if (!p_image_dest->comps){ + p_image_dest->comps = (opj_image_comp_t*) opj_manager_malloc(manager, p_image_dest->numcomps * sizeof(opj_image_comp_t)); + if (p_image_dest->comps == NULL) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for image."); p_image_dest->comps = NULL; p_image_dest->numcomps = 0; - return; + return OPJ_FALSE; } for (compno=0; compno < p_image_dest->numcomps; compno++){ - memcpy( &(p_image_dest->comps[compno]), - &(p_image_src->comps[compno]), - sizeof(opj_image_comp_t)); + memcpy( &(p_image_dest->comps[compno]), &(p_image_src->comps[compno]), sizeof(opj_image_comp_t)); p_image_dest->comps[compno].data = NULL; } @@ -189,53 +222,65 @@ void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_ p_image_dest->icc_profile_len = p_image_src->icc_profile_len; if (p_image_dest->icc_profile_len) { - p_image_dest->icc_profile_buf = (OPJ_BYTE*)opj_malloc(p_image_dest->icc_profile_len); - if (!p_image_dest->icc_profile_buf){ + p_image_dest->icc_profile_buf = (OPJ_BYTE*)opj_manager_malloc(manager, p_image_dest->icc_profile_len); + if (p_image_dest->icc_profile_buf == NULL) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for image."); p_image_dest->icc_profile_buf = NULL; p_image_dest->icc_profile_len = 0; - return; + return OPJ_FALSE; } - memcpy( p_image_dest->icc_profile_buf, - p_image_src->icc_profile_buf, - p_image_src->icc_profile_len); - } - else - p_image_dest->icc_profile_buf = NULL; + memcpy( p_image_dest->icc_profile_buf, p_image_src->icc_profile_buf, p_image_src->icc_profile_len); + } else { + p_image_dest->icc_profile_buf = NULL; + } - return; + return OPJ_TRUE; } -opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) { +opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) +{ + opj_manager_t l_manager = opj_manager_get_global_manager(); + + return opj_manager_image_tile_create(l_manager, numcmpts, cmptparms, clrspc); +} +OPJ_API opj_image_t* OPJ_CALLCONV opj_manager_image_tile_create(opj_manager_t manager, OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) +{ OPJ_UINT32 compno; opj_image_t *image = 00; - image = (opj_image_t*) opj_calloc(1,sizeof(opj_image_t)); - if (image) - { + if (manager == NULL) { + return NULL; + } + + image = (opj_image_t*) opj_manager_calloc(manager, 1, sizeof(opj_image_t)); + if (image == NULL) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for image."); + return NULL; + } - image->color_space = clrspc; - image->numcomps = numcmpts; + image->color_space = clrspc; + image->numcomps = numcmpts; - /* allocate memory for the per-component information */ - image->comps = (opj_image_comp_t*)opj_calloc(image->numcomps, sizeof(opj_image_comp_t)); - if (!image->comps) { - opj_image_destroy(image); - return 00; - } + /* allocate memory for the per-component information */ + image->comps = (opj_image_comp_t*)opj_manager_calloc(manager, image->numcomps, sizeof(opj_image_comp_t)); + if (!image->comps) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate memory for image."); + opj_manager_image_destroy(manager, image); + return NULL; + } - /* create the individual image components */ - for(compno = 0; compno < numcmpts; compno++) { - opj_image_comp_t *comp = &image->comps[compno]; - comp->dx = cmptparms[compno].dx; - comp->dy = cmptparms[compno].dy; - comp->w = cmptparms[compno].w; - comp->h = cmptparms[compno].h; - comp->x0 = cmptparms[compno].x0; - comp->y0 = cmptparms[compno].y0; - comp->prec = cmptparms[compno].prec; - comp->sgnd = cmptparms[compno].sgnd; - comp->data = 0; - } + /* create the individual image components */ + for(compno = 0; compno < numcmpts; compno++) { + opj_image_comp_t *comp = &image->comps[compno]; + comp->dx = cmptparms[compno].dx; + comp->dy = cmptparms[compno].dy; + comp->w = cmptparms[compno].w; + comp->h = cmptparms[compno].h; + comp->x0 = cmptparms[compno].x0; + comp->y0 = cmptparms[compno].y0; + comp->prec = cmptparms[compno].prec; + comp->sgnd = cmptparms[compno].sgnd; + comp->data = NULL; } return image; diff --git a/src/lib/openjp2/image.h b/src/lib/openjp2/image.h index e0e2772d..c08d7851 100644 --- a/src/lib/openjp2/image.h +++ b/src/lib/openjp2/image.h @@ -46,9 +46,11 @@ struct opj_cp; /** * Create an empty image * + * @param manager OpenJpeg memory/event manager + * * @return returns an empty image if successful, returns NULL otherwise */ -opj_image_t* opj_image_create0(void); +opj_image_t* opj_image_create0(opj_manager_t manager); @@ -60,7 +62,7 @@ opj_image_t* opj_image_create0(void); */ void opj_image_comp_header_update(opj_image_t * p_image, const struct opj_cp* p_cp); -void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_dest); +OPJ_BOOL opj_copy_image_header(opj_manager_t manager, const opj_image_t* p_image_src, opj_image_t* p_image_dest); /*@}*/ diff --git a/src/lib/openjp2/invert.c b/src/lib/openjp2/invert.c index dd4998a9..a4e9d1e9 100644 --- a/src/lib/openjp2/invert.c +++ b/src/lib/openjp2/invert.c @@ -67,7 +67,8 @@ static void opj_lupInvert ( OPJ_FLOAT32 * pSrcMatrix, /** * Matrix inversion. */ -OPJ_BOOL opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix, +OPJ_BOOL opj_matrix_inversion_f(opj_manager_t manager, + OPJ_FLOAT32 * pSrcMatrix, OPJ_FLOAT32 * pDestMatrix, OPJ_UINT32 nb_compo) { @@ -78,8 +79,12 @@ OPJ_BOOL opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix, OPJ_UINT32 * lPermutations = 00; OPJ_FLOAT32 * l_double_data = 00; - l_data = (OPJ_BYTE *) opj_malloc(l_total_size); - if (l_data == 0) { + if (manager == NULL) { + return OPJ_FALSE; + } + + l_data = (OPJ_BYTE *) opj_manager_malloc(manager, l_total_size); + if (l_data == NULL) { return OPJ_FALSE; } lPermutations = (OPJ_UINT32 *) l_data; @@ -87,14 +92,14 @@ OPJ_BOOL opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix, memset(lPermutations,0,l_permutation_size); if(! opj_lupDecompose(pSrcMatrix,lPermutations,l_double_data,nb_compo)) { - opj_free(l_data); + opj_manager_free(manager, l_data); return OPJ_FALSE; } - opj_lupInvert(pSrcMatrix,pDestMatrix,nb_compo,lPermutations,l_double_data,l_double_data + nb_compo,l_double_data + 2*nb_compo); - opj_free(l_data); + opj_lupInvert(pSrcMatrix,pDestMatrix,nb_compo,lPermutations,l_double_data,l_double_data + nb_compo,l_double_data + 2*nb_compo); + opj_manager_free(manager, l_data); - return OPJ_TRUE; + return OPJ_TRUE; } diff --git a/src/lib/openjp2/invert.h b/src/lib/openjp2/invert.h index 2fae8e54..a7bae35a 100644 --- a/src/lib/openjp2/invert.h +++ b/src/lib/openjp2/invert.h @@ -48,12 +48,14 @@ The function in INVERT.H compute a matrix inversion with a LUP method * Calculates a n x n double matrix inversion with a LUP method. Data is aligned, rows after rows (or columns after columns). * The function does not take ownership of any memory block, data must be fred by the user. * - * @param pSrcMatrix the matrix to invert. - * @param pDestMatrix data to store the inverted matrix. - * @param n size of the matrix + * @param manager Memory manager. + * @param pSrcMatrix the matrix to invert. + * @param pDestMatrix data to store the inverted matrix. + * @param n size of the matrix * @return OPJ_TRUE if the inversion is successful, OPJ_FALSE if the matrix is singular. */ -OPJ_BOOL opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix, +OPJ_BOOL opj_matrix_inversion_f(opj_manager_t manager, + OPJ_FLOAT32 * pSrcMatrix, OPJ_FLOAT32 * pDestMatrix, OPJ_UINT32 nb_compo); /* ----------------------------------------------------------------------- */ diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index d487d89d..3f53c4ab 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -56,35 +56,29 @@ static OPJ_BOOL opj_j2k_setup_header_reading (opj_j2k_t *p_j2k, opj_event_mgr_t /** * The read header procedure. */ -static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager); +static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** * 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. * * @return true if the parameters are correct. */ static OPJ_BOOL opj_j2k_encoding_validation ( opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * 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. * * @return true if the parameters are correct. */ static OPJ_BOOL opj_j2k_decoding_validation ( opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters @@ -96,7 +90,7 @@ static OPJ_BOOL opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k, opj_event_m * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters * are valid. Developpers wanting to extend the library can add their own validation procedures. */ -static OPJ_BOOL opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager); +static OPJ_BOOL opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k); /** * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters @@ -107,80 +101,63 @@ static OPJ_BOOL opj_j2k_setup_end_compress (opj_j2k_t *p_j2k, opj_event_mgr_t * /** * 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. * * @return true if the parameters are correct. */ -static OPJ_BOOL opj_j2k_mct_validation (opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_mct_validation (opj_j2k_t * p_j2k, opj_stream_private_t *p_stream); /** * Builds the tcd decoder to use to decode tile. */ -static OPJ_BOOL opj_j2k_build_decoder ( opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_build_decoder (opj_j2k_t * p_j2k, opj_stream_private_t *p_stream); /** * Builds the tcd encoder to use to encode tile. */ -static OPJ_BOOL opj_j2k_build_encoder ( opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_build_encoder (opj_j2k_t * p_j2k, opj_stream_private_t *p_stream); /** * 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. */ -static OPJ_BOOL opj_j2k_create_tcd( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** * 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_procedure_list the list of procedures to execute * @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. */ static OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k, opj_procedure_list_t * p_procedure_list, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager); + opj_stream_private_t *p_stream); /** * 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_j2k J2K codec. + * @param p_stream the stream to write data to. */ -static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** * 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_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * Destroys the memory associated with the decoding of headers. */ static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * Reads the lookup table containing all the marker, status and action, and returns the handler associated @@ -196,21 +173,23 @@ static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler ( * * @param p_tcp the tile coding parameter to destroy. */ -static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp); +static void opj_j2k_tcp_destroy (opj_manager_t manager, opj_tcp_t *p_tcp); /** * Destroys the data inside a tile coding parameter structure. * * @param p_tcp the tile coding parameter which contain data to destroy. */ -static void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp); +static void opj_j2k_tcp_data_destroy (opj_manager_t manager, opj_tcp_t *p_tcp); /** * Destroys a coding parameter structure. * * @param p_cp the coding parameter to destroy. */ -static void opj_j2k_cp_destroy (opj_cp_t *p_cp); +static void opj_j2k_cp_destroy (opj_manager_t manager, opj_cp_t *p_cp); + +static OPJ_BOOL opj_j2k_setup_mct_encoding(opj_manager_t manager, opj_tcp_t * p_tcp, opj_image_t * p_image); /** * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. @@ -220,7 +199,6 @@ static void opj_j2k_cp_destroy (opj_cp_t *p_cp); * @param p_comp_no the component number to output. * @param p_data FIXME DOC * @param p_header_size FIXME DOC - * @param p_manager the user event manager. * * @return FIXME DOC */ @@ -228,8 +206,7 @@ static OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_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_header_size ); /** * Gets the size taken by writing a SPCod or SPCoc for the given tile and component. @@ -250,13 +227,11 @@ static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_t *p_j2k, * @param compno FIXME DOC * @param p_header_data the data contained in the COM box. * @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_t *p_j2k, OPJ_UINT32 compno, OPJ_BYTE * p_header_data, - OPJ_UINT32 * p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 * p_header_size ); /** * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. @@ -279,15 +254,13 @@ static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_t *p_j2k, * @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_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_header_size); /** * Updates the Tile Length Marker. @@ -301,14 +274,12 @@ static void opj_j2k_update_tlm ( opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size) * @param compno the component number to output. * @param p_header_data the data buffer. * @param p_header_size pointer to the size of the data buffer, it is changed by the function. - * @param p_manager the user event manager. * */ static OPJ_BOOL opj_j2k_read_SQcd_SQcc( opj_j2k_t *p_j2k, OPJ_UINT32 compno, OPJ_BYTE * p_header_data, - OPJ_UINT32 * p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 * p_header_size ); /** * Copies the tile component parameters of all the component from the first tile component. @@ -327,16 +298,13 @@ static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k ); /** * Reads the tiles. */ -static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager); +static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); static OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_index, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); -static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image); +static OPJ_BOOL opj_j2k_update_image_data(opj_manager_t manager, opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image); static void opj_get_tile_dimensions(opj_image_t * l_image, opj_tcd_tilecomp_t * l_tilec, @@ -352,9 +320,7 @@ static void opj_get_tile_dimensions(opj_image_t * l_image, static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data); -static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k, opj_stream_private_t *p_stream); /** * Sets up the procedures to do on writing header. @@ -366,26 +332,21 @@ static OPJ_BOOL opj_j2k_write_first_tile_part( opj_j2k_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_stream_private_t *p_stream ); static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_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_stream_private_t *p_stream ); /** * 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. */ -static OPJ_BOOL opj_j2k_get_end_header( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k); @@ -400,89 +361,73 @@ static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k); * * @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_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * Reads a SOC marker (Start of Codestream) * @param p_j2k the jpeg2000 file codec. * @param p_stream XXX needs data - * @param p_manager the user event manager. */ static OPJ_BOOL opj_j2k_read_soc( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * Writes the SIZ marker (image and tile size) * * @param p_j2k J2K codec. * @param p_stream the stream to write data to. - * @param p_manager the user event manager. */ static OPJ_BOOL opj_j2k_write_siz( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * Reads a SIZ marker (image and tile size) * @param p_j2k the jpeg2000 file codec. * @param p_header_data the data contained in the SIZ box. * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager); + OPJ_UINT32 p_header_size); /** * 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. */ static OPJ_BOOL opj_j2k_write_com( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * Reads a COM marker (comments) * @param p_j2k the jpeg2000 file codec. * @param p_header_data the data contained in the COM box. * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); /** * 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. */ static OPJ_BOOL opj_j2k_write_cod( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * 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. */ static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager); + OPJ_UINT32 p_header_size); #if 0 /** @@ -491,12 +436,10 @@ static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k, * @param p_j2k J2K codec. * @param p_comp_no the index of the component to output. * @param p_stream the stream to write data to. - * @param p_manager the user event manager. */ static OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k, OPJ_UINT32 p_comp_no, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); #endif #if 0 @@ -507,13 +450,11 @@ static OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k, * @param p_comp_no the index of the component to output. * @param p_data FIXME DOC * @param p_data_written FIXME DOC - * @param p_manager the user event manager. */ static void opj_j2k_write_coc_in_memory(opj_j2k_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_data_written ); #endif /** @@ -528,35 +469,29 @@ static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k); * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); /** * Writes the QCD marker (quantization default) * * @param p_j2k J2K codec. * @param p_stream the stream to write data to. - * @param p_manager the user event manager. */ static OPJ_BOOL opj_j2k_write_qcd( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * 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. */ static OPJ_BOOL opj_j2k_read_qcd ( opj_j2k_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); #if 0 /** * Writes the QCC marker (quantization component) @@ -564,12 +499,10 @@ static OPJ_BOOL opj_j2k_read_qcd ( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_UINT32 p_comp_no, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); #endif #if 0 @@ -580,13 +513,11 @@ static OPJ_BOOL opj_j2k_write_qcc( opj_j2k_t *p_j2k, * @param p_comp_no the index of the component to output. * @param p_data FIXME DOC * @param p_data_written the stream to write data to. - * @param p_manager the user event manager. */ static void opj_j2k_write_qcc_in_memory(opj_j2k_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_data_written ); #endif /** @@ -599,34 +530,28 @@ static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k); * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager); + OPJ_UINT32 p_header_size); /** * 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. */ static OPJ_BOOL opj_j2k_write_poc( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * Writes the POC marker (Progression Order Change) * * @param p_j2k J2K codec. * @param p_data FIXME DOC * @param p_data_written the stream to write data to. - * @param p_manager the user event manager. */ static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k, OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 * p_data_written ); /** * Gets the maximum size taken by the writing of a POC. */ @@ -638,12 +563,10 @@ static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k); * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); /** * Gets the maximum size taken by the toc headers of all the tile parts of any given tile. @@ -663,35 +586,28 @@ static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k); * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); /** * 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. */ static OPJ_BOOL opj_j2k_read_tlm ( opj_j2k_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager); + OPJ_UINT32 p_header_size); /** * 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. */ -static OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** * Reads a PLM marker (Packet length, main header marker) @@ -699,24 +615,20 @@ static OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager); + OPJ_UINT32 p_header_size); /** * 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. */ static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); /** * Reads a PPM marker (Packed headers, main header) @@ -724,22 +636,19 @@ static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k, * @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_ppm ( - opj_j2k_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_ppm ( opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size ); /** * Merges all PPM markers read (Packed headers, main header) * + * @param p_j2k the jpeg2000 codec. * @param p_cp main coding parameters. - * @param p_manager the user event manager. */ -static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_merge_ppm (opj_j2k_t *p_j2k, opj_cp_t *p_cp); /** * Reads a PPT marker (Packed packet headers, tile-part header) @@ -747,33 +656,27 @@ static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size ); /** * Merges all PPT markers read (Packed headers, tile-part header) * + * @param p_j2k the jpeg2000 codec. * @param p_tcp the tile. - * @param p_manager the user event manager. */ -static OPJ_BOOL opj_j2k_merge_ppt ( opj_tcp_t *p_tcp, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_merge_ppt (opj_j2k_t *p_j2k, opj_tcp_t *p_tcp); /** * 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_j2k J2K codec. + * @param p_stream the stream to write data to. */ -static OPJ_BOOL opj_j2k_write_tlm( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream ); /** * Writes the SOT marker (Start of tile-part) @@ -782,13 +685,11 @@ static OPJ_BOOL opj_j2k_write_tlm( opj_j2k_t *p_j2k, * @param p_data FIXME DOC * @param p_data_written FIXME DOC * @param p_stream the stream to write data to. - * @param p_manager the user event manager. */ static OPJ_BOOL opj_j2k_write_sot( opj_j2k_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 ); + const opj_stream_private_t *p_stream ); /** * Reads values from a SOT marker (Start of tile-part) @@ -801,27 +702,24 @@ static OPJ_BOOL opj_j2k_write_sot( opj_j2k_t *p_j2k, * @param p_tot_len Psot. * @param p_current_part TPsot. * @param p_num_parts TNsot. - * @param p_manager the user event manager. */ -static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - OPJ_UINT32* p_tile_no, - OPJ_UINT32* p_tot_len, - OPJ_UINT32* p_current_part, - OPJ_UINT32* p_num_parts, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_get_sot_values( opj_event_mgr_t * p_manager, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + OPJ_UINT32* p_tile_no, + OPJ_UINT32* p_tot_len, + OPJ_UINT32* p_current_part, + OPJ_UINT32* p_num_parts ); /** * Reads a SOT marker (Start of tile-part) * * @param p_header_data the data contained in the SOT marker. * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size ); /** * Writes the SOD marker (Start of data) * @@ -831,26 +729,22 @@ static OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k, * @param p_data_written FIXME DOC * @param p_total_data_size FIXME DOC * @param p_stream the stream to write data to. - * @param p_manager the user event manager. */ static OPJ_BOOL opj_j2k_write_sod( opj_j2k_t *p_j2k, opj_tcd_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 ); + const opj_stream_private_t *p_stream ); /** * Reads a SOD marker (Start Of Data) * * @param p_j2k the jpeg2000 codec. * @param p_stream FIXME DOC - * @param p_manager the user event manager. */ static OPJ_BOOL opj_j2k_read_sod( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); static void opj_j2k_update_tlm (opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size ) { @@ -869,14 +763,12 @@ static void opj_j2k_update_tlm (opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size ) * @param nb_comps the number of components * @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_t *p_j2k, OPJ_UINT32 p_tile_no, OPJ_UINT32 p_comp_no, OPJ_UINT32 nb_comps, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * Reads a RGN marker (Region Of Interest) @@ -884,23 +776,18 @@ static OPJ_BOOL opj_j2k_write_rgn( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); /** * 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. */ -static OPJ_BOOL opj_j2k_write_eoc( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); #if 0 /** @@ -908,34 +795,25 @@ static OPJ_BOOL opj_j2k_write_eoc( opj_j2k_t *p_j2k, * * @param p_j2k the jpeg2000 codec. * @param p_stream FIXME DOC - * @param p_manager the user event manager. */ -static OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); #endif /** * 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. */ -static OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** * 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. */ -static OPJ_BOOL opj_j2k_init_info( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** Add main header marker information @@ -944,7 +822,7 @@ Add main header marker information @param pos byte offset of marker segment @param len length of marker segment */ -static OPJ_BOOL 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_manager_t manager, 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 @@ -953,7 +831,7 @@ Add tile header marker information @param pos byte offset of marker segment @param len length of marker segment */ -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); +static OPJ_BOOL opj_j2k_add_tlmarker(opj_manager_t manager, OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len); /** * Reads an unknown marker @@ -961,14 +839,12 @@ static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t * * @param p_j2k the jpeg2000 codec. * @param p_stream the stream object to read from. * @param output_marker FIXME DOC - * @param p_manager the user event manager. * * @return true if the marker could be deduced. */ static OPJ_BOOL opj_j2k_read_unk( opj_j2k_t *p_j2k, opj_stream_private_t *p_stream, - OPJ_UINT32 *output_marker, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 *output_marker ); /** * Writes the MCT marker (Multiple Component Transform) @@ -976,12 +852,10 @@ static OPJ_BOOL opj_j2k_read_unk( opj_j2k_t *p_j2k, * @param p_j2k J2K codec. * @param p_mct_record FIXME DOC * @param p_stream the stream to write data to. - * @param p_manager the user event manager. */ static OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k, opj_mct_data_t * p_mct_record, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * Reads a MCT marker (Multiple Component Transform) @@ -989,12 +863,10 @@ static OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); /** * Writes the MCC marker (Multiple Component Collection) @@ -1002,51 +874,42 @@ static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k, * @param p_j2k J2K codec. * @param p_mcc_record FIXME DOC * @param p_stream the stream to write data to. - * @param p_manager the user event manager. */ static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k, opj_simple_mcc_decorrelation_data_t * p_mcc_record, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); /** * 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_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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); /** * 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_j2k J2K codec. + * @param p_stream the stream to write data to. */ -static OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** * 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_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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); -static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index); +static OPJ_BOOL opj_j2k_add_mct(opj_manager_t manager, opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index); static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); @@ -1066,36 +929,28 @@ 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. */ -static OPJ_BOOL opj_j2k_end_encoding( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** * 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_j2k J2K codec. + * @param p_stream the stream to write data to. */ -static OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** * 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. */ static OPJ_BOOL opj_j2k_read_cbd ( opj_j2k_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager); + OPJ_UINT32 p_header_size); #if 0 /** @@ -1103,11 +958,9 @@ static OPJ_BOOL opj_j2k_read_cbd ( opj_j2k_t *p_j2k, * * @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_all_coc( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); #endif #if 0 @@ -1116,34 +969,26 @@ static OPJ_BOOL opj_j2k_write_all_coc( opj_j2k_t *p_j2k, * * @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_all_qcc( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *p_stream ); #endif /** * 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. */ -static OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** * 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. */ -static OPJ_BOOL opj_j2k_write_epc( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ); +static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream); /** * Checks the progression order changes values. Tells of the poc given as input are valid. @@ -1154,7 +999,7 @@ static OPJ_BOOL opj_j2k_write_epc( opj_j2k_t *p_j2k, * @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 manager User memory/event manager * * @return true if the pocs are valid. */ @@ -1163,7 +1008,7 @@ static OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs, OPJ_UINT32 p_nb_resolutions, OPJ_UINT32 numcomps, OPJ_UINT32 numlayers, - opj_event_mgr_t * p_manager); + opj_manager_t manager); /** * Gets the number of tile parts used for the given change of progression (if any) and the given tile. @@ -1184,21 +1029,19 @@ static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 * @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_t *p_j2k, opj_cp_t *cp, OPJ_UINT32 * p_nb_tiles, - opj_image_t *image, - opj_event_mgr_t * p_manager); + opj_image_t *image); static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream); static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream); -static opj_codestream_index_t* opj_j2k_create_cstr_index(void); +static opj_codestream_index_t* opj_j2k_create_cstr_index(opj_manager_t manager); static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp); @@ -1216,7 +1059,7 @@ static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, * @param p_stream the stream to read data from. * @param tile_no tile number we're looking for. * @param p_correction_needed output value. if true, non conformant codestream needs TNsot correction. - * @param p_manager the user event manager. + * @param p_manager the user event manager. * * @return true if the function was successful, false else. */ @@ -1287,8 +1130,7 @@ typedef struct opj_dec_memory_marker_handler /** action linked to the marker */ OPJ_BOOL (*handler) ( opj_j2k_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size); } opj_dec_memory_marker_handler_t; @@ -1536,7 +1378,7 @@ static OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs, OPJ_UINT32 p_nb_resolutions, OPJ_UINT32 p_num_comps, OPJ_UINT32 p_num_layers, - opj_event_mgr_t * p_manager) + opj_manager_t p_manager) { OPJ_UINT32* packet_array; OPJ_UINT32 index , resno, compno, layno; @@ -1547,14 +1389,14 @@ static OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs, OPJ_BOOL loss = OPJ_FALSE; OPJ_UINT32 layno0 = 0; - packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32)); + packet_array = (OPJ_UINT32*) opj_manager_calloc(p_manager, step_l * p_num_layers, sizeof(OPJ_UINT32)); if (packet_array == 00) { - opj_event_msg(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n"); + opj_event_msg(&(p_manager->event_mgr), EVT_ERROR, "Not enough memory for checking the poc values.\n"); return OPJ_FALSE; } if (p_nb_pocs == 0) { - opj_free(packet_array); + opj_manager_free(p_manager, packet_array); return OPJ_TRUE; } @@ -1625,10 +1467,10 @@ static OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs, } if (loss) { - opj_event_msg(p_manager , EVT_ERROR, "Missing packets possible loss of data\n"); + opj_event_msg(&(p_manager->event_mgr) , EVT_ERROR, "Missing packets possible loss of data\n"); } - opj_free(packet_array); + opj_manager_free(p_manager, packet_array); return !loss; } @@ -1696,8 +1538,7 @@ static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 t static OPJ_BOOL opj_j2k_calculate_tp( opj_j2k_t *p_j2k, opj_cp_t *cp, OPJ_UINT32 * p_nb_tiles, - opj_image_t *image, - opj_event_mgr_t * p_manager + opj_image_t *image ) { OPJ_UINT32 pino,tileno; @@ -1709,7 +1550,6 @@ static OPJ_BOOL opj_j2k_calculate_tp( opj_j2k_t *p_j2k, 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; @@ -1772,8 +1612,7 @@ static OPJ_BOOL opj_j2k_calculate_tp( opj_j2k_t *p_j2k, } static OPJ_BOOL opj_j2k_write_soc( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *p_stream ) { /* 2 bytes will be written */ OPJ_BYTE * l_start_stream = 00; @@ -1781,14 +1620,13 @@ static OPJ_BOOL opj_j2k_write_soc( opj_j2k_t *p_j2k, /* 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; /* 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) { + if (opj_stream_write_data(p_stream,l_start_stream,2,&(p_j2k->m_manager->event_mgr)) != 2) { return OPJ_FALSE; } @@ -1809,11 +1647,9 @@ static OPJ_BOOL opj_j2k_write_soc( opj_j2k_t *p_j2k, * Reads a SOC marker (Start of Codestream) * @param p_j2k the jpeg2000 file codec. * @param p_stream FIXME DOC - * @param p_manager the user event manager. */ static OPJ_BOOL opj_j2k_read_soc( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager + opj_stream_private_t *p_stream ) { OPJ_BYTE l_data [2]; @@ -1821,10 +1657,9 @@ static OPJ_BOOL opj_j2k_read_soc( opj_j2k_t *p_j2k, /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); assert(p_stream != 00); - if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) { + if (opj_stream_read_data(p_stream,l_data,2,&(p_j2k->m_manager->event_mgr)) != 2) { return OPJ_FALSE; } @@ -1839,50 +1674,47 @@ static OPJ_BOOL opj_j2k_read_soc( opj_j2k_t *p_j2k, /* FIXME move it in a index structure included in p_j2k*/ p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2; - opj_event_msg(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start); /* 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(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); + if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->m_manager, p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to add mh marker\n"); return OPJ_FALSE; } return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_siz( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { - OPJ_UINT32 i; - OPJ_UINT32 l_size_len; - OPJ_BYTE * l_current_ptr; - opj_image_t * l_image = 00; - opj_cp_t *cp = 00; - opj_image_comp_t * l_img_comp = 00; + OPJ_UINT32 i; + OPJ_UINT32 l_size_len; + OPJ_BYTE * l_current_ptr; + opj_image_t * l_image = 00; + opj_cp_t *cp = 00; + opj_image_comp_t * l_img_comp = 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_j2k->m_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; + 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(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; - } + 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_manager_realloc(p_j2k->m_manager, p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len); + if (! new_header_tile_data) { + opj_manager_free(p_j2k->m_manager, 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(&(p_j2k->m_manager->event_mgr), 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; @@ -1937,7 +1769,7 @@ static OPJ_BOOL opj_j2k_write_siz( opj_j2k_t *p_j2k, ++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) { + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,&(p_j2k->m_manager->event_mgr)) != l_size_len) { return OPJ_FALSE; } @@ -1949,398 +1781,376 @@ static OPJ_BOOL opj_j2k_write_siz( opj_j2k_t *p_j2k, * @param p_j2k the jpeg2000 file codec. * @param p_header_data the data contained in the SIZ box. * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { - OPJ_UINT32 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, l_tx1, l_ty1; - opj_image_t *l_image = 00; - opj_cp_t *l_cp = 00; - opj_image_comp_t * l_img_comp = 00; - opj_tcp_t * l_current_tile_param = 00; + OPJ_UINT32 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, l_tx1, l_ty1; + opj_image_t *l_image = 00; + opj_cp_t *l_cp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_tcp_t * l_current_tile_param = 00; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_header_data != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); + assert(p_header_data != 00); - l_image = p_j2k->m_private_image; - l_cp = &(p_j2k->m_cp); + 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(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(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); - return OPJ_FALSE; - } - - opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */ - p_header_data+=2; - l_cp->rsiz = (OPJ_UINT16) 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(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(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; - } - - /* testcase 4035.pdf.SIGSEGV.d8b.3375 */ - /* testcase issue427-null-image-size.jp2 */ - if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) { - opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative or zero image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0); - return OPJ_FALSE; - } - /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */ - if (!(l_cp->tdx * l_cp->tdy)) { - opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, l_cp->tdy); - return OPJ_FALSE; - } - - /* testcase 1610.pdf.SIGSEGV.59c.681 */ - if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) { - opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)\n", l_image->x1, l_image->y1); - return OPJ_FALSE; - } - - /* testcase issue427-illegal-tile-offset.jp2 */ - l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */ - l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */ - if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) || (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0) ) { - opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: illegal tile offset\n"); - 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(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(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(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(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - */ /* we try to correct */ - /* opj_event_msg(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(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", - len); - } else { - l_image->numcomps = ((len - 38) / 3); - opj_event_msg(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(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } - - 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_UINT32)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_UINT32)tmp; /* should be between 1 and 255 */ - if( l_img_comp->dx < 1 || l_img_comp->dx > 255 || - l_img_comp->dy < 1 || l_img_comp->dy > 255 ) { - opj_event_msg(p_manager, EVT_ERROR, - "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)", - i, l_img_comp->dx, l_img_comp->dy); - 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, again */ - if (!(l_image->comps[i].dx * l_image->comps[i].dy)) { - opj_event_msg(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(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - /* we try to correct */ - opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"); - if (!l_image->comps[i].dx) { - l_image->comps[i].dx = 1; - opj_event_msg(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(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 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx); - l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy); - - /* Check that the number of tiles is valid */ - if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) { - opj_event_msg( p_manager, EVT_ERROR, - "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n", - l_cp->tw, l_cp->th); + /* minimum size == 39 - 3 (= minimum component parameter) */ + if (p_header_size < 36) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error with SIZ marker size\n"); return OPJ_FALSE; - } - 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 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), (OPJ_INT32)l_cp->tdx); - p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), (OPJ_INT32)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_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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error with SIZ marker size\n"); + return OPJ_FALSE; + } + + opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */ + p_header_data+=2; + l_cp->rsiz = (OPJ_UINT16) 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(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), 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; + } + + /* testcase 4035.pdf.SIGSEGV.d8b.3375 */ + /* testcase issue427-null-image-size.jp2 */ + if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error with SIZ marker: negative or zero image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0); + return OPJ_FALSE; + } + + /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */ + if (!(l_cp->tdx * l_cp->tdy)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, l_cp->tdy); + return OPJ_FALSE; + } + + /* testcase 1610.pdf.SIGSEGV.59c.681 */ + if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)\n", l_image->x1, l_image->y1); + return OPJ_FALSE; + } + + /* testcase issue427-illegal-tile-offset.jp2 */ + l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */ + l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */ + if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) || (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0) ) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error with SIZ marker: illegal tile offset\n"); + 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_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) { - opj_event_msg(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(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - /* we try to correct */ - opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"); - if (l_cp->tw < 1) { - l_cp->tw= 1; - opj_event_msg(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(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(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(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_image->x1 * l_image->y1)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "JPWL: bad image size (%d x %d)\n", l_image->x1, l_image->y1); + if (!JPWL_ASSUME || JPWL_ASSUME) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } +*/ /* we try to correct */ +/* opj_event_msg(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(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", len); + } else { + l_image->numcomps = ((len - 38) / 3); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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 */ - /* memory allocations */ - l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t)); - if (l_cp->tcps == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } + /* Allocate the resulting image components */ + l_image->comps = (opj_image_comp_t*) opj_manager_calloc(p_j2k->m_manager, l_image->numcomps, sizeof(opj_image_comp_t)); + if (l_image->comps == 00){ + l_image->numcomps = 0; + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + + 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_UINT32)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_UINT32)tmp; /* should be between 1 and 255 */ + if( l_img_comp->dx < 1 || l_img_comp->dx > 255 || + l_img_comp->dy < 1 || l_img_comp->dy > 255 ) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, + "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)", + i, l_img_comp->dx, l_img_comp->dy); + return OPJ_FALSE; + } #ifdef USE_JPWL - if (l_cp->correct) { - if (!l_cp->tcps) { - opj_event_msg(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(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - } - } + 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(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"); + if (!l_image->comps[i].dx) { + l_image->comps[i].dx = 1; + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), 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 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx); + l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy); + + /* Check that the number of tiles is valid */ + if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) { + opj_event_msg( &(p_j2k->m_manager->event_mgr), EVT_ERROR, + "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n", + l_cp->tw, l_cp->th); + return OPJ_FALSE; + } + 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 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), (OPJ_INT32)l_cp->tdx); + p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), (OPJ_INT32)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(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "- trying to adjust them\n"); + if (l_cp->tw < 1) { + l_cp->tw= 1; + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), 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 */ - 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(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } + /* memory allocations */ + l_cp->tcps = (opj_tcp_t*) opj_manager_calloc(p_j2k->m_manager, l_nb_tiles, sizeof(opj_tcp_t)); + if (l_cp->tcps == 00) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } - p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = - (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS ,sizeof(opj_mct_data_t)); +#ifdef USE_JPWL + if (l_cp->correct) { + if (!l_cp->tcps) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, "JPWL: could not alloc tcps field of cp\n"); + if (!JPWL_ASSUME || JPWL_ASSUME) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + } + } +#endif /* USE_JPWL */ - if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } - p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = OPJ_J2K_MCT_DEFAULT_NB_RECORDS; + p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps = (opj_tccp_t*) opj_manager_calloc(p_j2k->m_manager, l_image->numcomps, sizeof(opj_tccp_t)); + if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } - p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = - (opj_simple_mcc_decorrelation_data_t*) - opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS, sizeof(opj_simple_mcc_decorrelation_data_t)); + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = (opj_mct_data_t*)opj_manager_calloc(p_j2k->m_manager, OPJ_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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = OPJ_J2K_MCT_DEFAULT_NB_RECORDS; - if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } - p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = OPJ_J2K_MCC_DEFAULT_NB_RECORDS; + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)opj_manager_calloc(p_j2k->m_manager, OPJ_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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = OPJ_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); - } - } + /* 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_calloc(l_image->numcomps, sizeof(opj_tccp_t)); - if (l_current_tile_param->tccps == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); - return OPJ_FALSE; - } + l_current_tile_param = l_cp->tcps; + for (i = 0; i < l_nb_tiles; ++i) { + l_current_tile_param->tccps = (opj_tccp_t*) opj_manager_calloc(p_j2k->m_manager, l_image->numcomps, sizeof(opj_tccp_t)); + if (l_current_tile_param->tccps == 00) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return OPJ_FALSE; + } - ++l_current_tile_param; - } + ++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); + 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; + return OPJ_TRUE; } static OPJ_BOOL opj_j2k_write_com( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager + opj_stream_private_t *p_stream ) { - OPJ_UINT32 l_comment_size; - OPJ_UINT32 l_total_com_size; - const OPJ_CHAR *l_comment; - OPJ_BYTE * l_current_ptr = 00; + 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); + /* preconditions */ + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); + assert(p_stream != 00); - l_comment = p_j2k->m_cp.comment; - l_comment_size = (OPJ_UINT32)strlen(l_comment); - l_total_com_size = l_comment_size + 6; + l_comment = p_j2k->m_cp.comment; + l_comment_size = (OPJ_UINT32)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(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; - } + 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_manager_realloc(p_j2k->m_manager, p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size); + if (! new_header_tile_data) { + opj_manager_free(p_j2k->m_manager, 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(&(p_j2k->m_manager->event_mgr), 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; + 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,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,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; + 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); + 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; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,&(p_j2k->m_manager->event_mgr)) != l_total_com_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** @@ -2348,92 +2158,89 @@ static OPJ_BOOL opj_j2k_write_com( opj_j2k_t *p_j2k, * @param p_j2k the jpeg2000 file codec. * @param p_header_data the data contained in the COM box. * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_header_data != 00); - (void)p_header_size; + /* unused args */ + (void)p_header_size; - return OPJ_TRUE; + /* preconditions */ + assert(p_j2k != 00); + assert(p_header_data != 00); + + return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_cod( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { - opj_cp_t *l_cp = 00; - opj_tcp_t *l_tcp = 00; - OPJ_UINT32 l_code_size,l_remaining_size; - OPJ_BYTE * l_current_data = 00; + opj_cp_t *l_cp = 00; + opj_tcp_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); + /* preconditions */ + assert(p_j2k != 00); + assert(p_j2k->m_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; + 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(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; - } + 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_manager_realloc(p_j2k->m_manager, p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size); + if (! new_header_tile_data) { + opj_manager_free(p_j2k->m_manager, 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(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "Error writing 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)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error writing COD marker\n"); + return OPJ_FALSE; + } - if (l_remaining_size != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n"); - return OPJ_FALSE; - } + if (l_remaining_size != 0) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error writing 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_j2k->m_manager->event_mgr)) != l_code_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** @@ -2441,12 +2248,10 @@ static OPJ_BOOL opj_j2k_write_cod( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { /* loop */ @@ -2459,7 +2264,6 @@ static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k, /* 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); @@ -2471,14 +2275,14 @@ static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k, /* Only one COD per tile */ if (l_tcp->cod) { - opj_event_msg(p_manager, EVT_ERROR, "COD marker already read. No more than one COD marker per tile.\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "COD marker already read. No more than one COD marker per tile.\n"); return OPJ_FALSE; } l_tcp->cod = 1; /* Make sure room is sufficient */ if (p_header_size < 5) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading COD marker\n"); return OPJ_FALSE; } @@ -2486,7 +2290,7 @@ static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k, ++p_header_data; /* Make sure we know how to decode this */ if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP | J2K_CP_CSTY_EPH)) != 0U) { - opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Unknown Scod value in COD marker\n"); return OPJ_FALSE; } opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */ @@ -2494,14 +2298,14 @@ static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k, l_tcp->prg = (OPJ_PROG_ORDER) l_tmp; /* Make sure progression order is valid */ if (l_tcp->prg > OPJ_CPRL ) { - opj_event_msg(p_manager, EVT_ERROR, "Unknown progression order in COD marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Unknown progression order in COD marker\n"); l_tcp->prg = OPJ_PROG_UNKNOWN; } opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */ p_header_data+=2; if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid number of layers in COD marker : %d not in range [1-65535]\n", l_tcp->numlayers); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Invalid number of layers in COD marker : %d not in range [1-65535]\n", l_tcp->numlayers); return OPJ_FALSE; } @@ -2521,13 +2325,13 @@ static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k, 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(p_manager, EVT_ERROR, "Error reading COD marker\n"); + if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading COD marker\n"); return OPJ_FALSE; } if (p_header_size != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading COD marker\n"); return OPJ_FALSE; } @@ -2670,12 +2474,10 @@ static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k) * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { opj_cp_t *l_cp = NULL; @@ -2687,7 +2489,6 @@ static OPJ_BOOL opj_j2k_read_coc ( opj_j2k_t *p_j2k, /* 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*/ @@ -2699,7 +2500,7 @@ static OPJ_BOOL opj_j2k_read_coc ( opj_j2k_t *p_j2k, /* make sure room is sufficient*/ if (p_header_size < l_comp_room + 1) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading COC marker\n"); return OPJ_FALSE; } p_header_size -= l_comp_room + 1; @@ -2707,79 +2508,76 @@ static OPJ_BOOL opj_j2k_read_coc ( opj_j2k_t *p_j2k, 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(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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 ; - if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); + if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading COC marker\n"); return OPJ_FALSE; } if (p_header_size != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading COC marker\n"); return OPJ_FALSE; } return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_qcd( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager - ) +static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { - OPJ_UINT32 l_qcd_size,l_remaining_size; - OPJ_BYTE * l_current_data = 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); + /* preconditions */ + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); + assert(p_stream != 00); - 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; + 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(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; - } + 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_manager_realloc(p_j2k->m_manager, p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size); + if (! new_header_tile_data) { + opj_manager_free(p_j2k->m_manager, 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(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "Error writing 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)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error writing QCD marker\n"); + return OPJ_FALSE; + } - if (l_remaining_size != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n"); - return OPJ_FALSE; - } + if (l_remaining_size != 0) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error writing 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_j2k->m_manager->event_mgr)) != l_qcd_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** @@ -2787,26 +2585,23 @@ static OPJ_BOOL opj_j2k_write_qcd( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { /* 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(p_manager, EVT_ERROR, "Error reading QCD marker\n"); + if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading QCD marker\n"); return OPJ_FALSE; } if (p_header_size != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading QCD marker\n"); return OPJ_FALSE; } @@ -2918,12 +2713,10 @@ static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k) * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { OPJ_UINT32 l_num_comp,l_comp_no; @@ -2931,13 +2724,12 @@ static OPJ_BOOL opj_j2k_read_qcc( opj_j2k_t *p_j2k, /* 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(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading QCC marker\n"); return OPJ_FALSE; } opj_read_bytes(p_header_data,&l_comp_no,1); @@ -2946,7 +2738,7 @@ static OPJ_BOOL opj_j2k_read_qcc( opj_j2k_t *p_j2k, } else { if (p_header_size < 2) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading QCC marker\n"); return OPJ_FALSE; } opj_read_bytes(p_header_data,&l_comp_no,2); @@ -2961,16 +2753,16 @@ static OPJ_BOOL opj_j2k_read_qcc( opj_j2k_t *p_j2k, /* compno is negative or larger than the number of components!!! */ if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) { - opj_event_msg(p_manager, EVT_ERROR, + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "JPWL: giving up\n"); return OPJ_FALSE; } /* we try to correct */ l_comp_no = backup_compno % l_num_comp; - opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "- trying to adjust this\n" "- setting component number to %d\n", l_comp_no); } @@ -2981,80 +2773,76 @@ static OPJ_BOOL opj_j2k_read_qcc( opj_j2k_t *p_j2k, #endif /* USE_JPWL */ if (l_comp_no >= p_j2k->m_private_image->numcomps) { - opj_event_msg(p_manager, EVT_ERROR, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Invalid component number: %d, regarding the number of components %d\n", l_comp_no, p_j2k->m_private_image->numcomps); 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(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading QCC marker\n"); return OPJ_FALSE; } if (p_header_size != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading QCC marker\n"); return OPJ_FALSE; } return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_poc( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager - ) +static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { - OPJ_UINT32 l_nb_comp; - OPJ_UINT32 l_nb_poc; - OPJ_UINT32 l_poc_size; - OPJ_UINT32 l_written_size = 0; - opj_tcp_t *l_tcp = 00; - OPJ_UINT32 l_poc_room; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_poc; + OPJ_UINT32 l_poc_size; + OPJ_UINT32 l_written_size = 0; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_poc_room; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); + assert(p_stream != 00); - l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; - l_nb_comp = p_j2k->m_private_image->numcomps; - l_nb_poc = 1 + l_tcp->numpocs; + l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; + 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_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(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; - } + 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_manager_realloc(p_j2k->m_manager, p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size); + if (! new_header_tile_data) { + opj_manager_free(p_j2k->m_manager, 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(&(p_j2k->m_manager->event_mgr), 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); + opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size); - 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; - } + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,&(p_j2k->m_manager->event_mgr)) != l_poc_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } static void opj_j2k_write_poc_in_memory( opj_j2k_t *p_j2k, OPJ_BYTE * p_data, - OPJ_UINT32 * p_data_written, - opj_event_mgr_t * p_manager + OPJ_UINT32 * p_data_written ) { OPJ_UINT32 i; @@ -3070,7 +2858,6 @@ static void opj_j2k_write_poc_in_memory( opj_j2k_t *p_j2k, /* 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]; @@ -3195,12 +2982,10 @@ static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k) * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { OPJ_UINT32 i, l_nb_comp, l_tmp; @@ -3215,7 +3000,6 @@ static OPJ_BOOL opj_j2k_read_poc ( opj_j2k_t *p_j2k, /* 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; @@ -3230,7 +3014,7 @@ static OPJ_BOOL opj_j2k_read_poc ( opj_j2k_t *p_j2k, l_current_poc_remaining = p_header_size % l_chunk_size; if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading POC marker\n"); return OPJ_FALSE; } @@ -3243,7 +3027,7 @@ static OPJ_BOOL opj_j2k_read_poc ( opj_j2k_t *p_j2k, if(l_current_poc_nb >= 32) { - opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb); return OPJ_FALSE; } assert(l_current_poc_nb < 32); @@ -3283,24 +3067,22 @@ static OPJ_BOOL opj_j2k_read_poc ( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { 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(p_manager, EVT_ERROR, "Error reading CRG marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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 */ @@ -3323,22 +3105,20 @@ static OPJ_BOOL opj_j2k_read_crg ( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, 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(p_manager, EVT_ERROR, "Error reading TLM marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading TLM marker\n"); return OPJ_FALSE; } p_header_size -= 2; @@ -3357,7 +3137,7 @@ static OPJ_BOOL opj_j2k_read_tlm ( opj_j2k_t *p_j2k, l_tot_num_tp_remaining = p_header_size % l_quotient; if (l_tot_num_tp_remaining != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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 */ @@ -3379,21 +3159,18 @@ static OPJ_BOOL opj_j2k_read_tlm ( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { /* preconditions */ assert(p_header_data != 00); assert(p_j2k != 00); - assert(p_manager != 00); if (p_header_size < 1) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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 */ @@ -3449,12 +3226,10 @@ static OPJ_BOOL opj_j2k_read_plm ( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i; @@ -3462,10 +3237,9 @@ static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k, /* preconditions */ assert(p_header_data != 00); assert(p_j2k != 00); - assert(p_manager != 00); if (p_header_size < 1) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading PLT marker\n"); return OPJ_FALSE; } @@ -3488,7 +3262,7 @@ static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k, } if (l_packet_len != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading PLT marker\n"); return OPJ_FALSE; } @@ -3501,14 +3275,11 @@ static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k, * @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_ppm ( - opj_j2k_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_ppm ( opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size ) { opj_cp_t *l_cp = 00; OPJ_UINT32 l_Z_ppm; @@ -3516,11 +3287,11 @@ static OPJ_BOOL opj_j2k_read_ppm ( /* preconditions */ assert(p_header_data != 00); assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_manager != 00); /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */ if (p_header_size < 2) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading PPM marker\n"); return OPJ_FALSE; } @@ -3536,19 +3307,19 @@ static OPJ_BOOL opj_j2k_read_ppm ( OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ assert(l_cp->ppm_markers_count == 0U); - l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); + l_cp->ppm_markers = (opj_ppx *) opj_manager_calloc(p_j2k->m_manager, l_newCount, sizeof(opj_ppx)); if (l_cp->ppm_markers == NULL) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read PPM marker\n"); return OPJ_FALSE; } l_cp->ppm_markers_count = l_newCount; } else if (l_cp->ppm_markers_count <= l_Z_ppm) { OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */ opj_ppx *new_ppm_markers; - new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers, l_newCount * sizeof(opj_ppx)); + new_ppm_markers = (opj_ppx *) opj_manager_realloc(p_j2k->m_manager, l_cp->ppm_markers, l_newCount * sizeof(opj_ppx)); if (new_ppm_markers == NULL) { /* clean up to be done on l_cp destruction */ - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read PPM marker\n"); return OPJ_FALSE; } l_cp->ppm_markers = new_ppm_markers; @@ -3558,14 +3329,14 @@ static OPJ_BOOL opj_j2k_read_ppm ( if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) { /* clean up to be done on l_cp destruction */ - opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n", l_Z_ppm); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Zppm %u already read\n", l_Z_ppm); return OPJ_FALSE; } - l_cp->ppm_markers[l_Z_ppm].m_data = opj_malloc(p_header_size); + l_cp->ppm_markers[l_Z_ppm].m_data = opj_manager_malloc(p_j2k->m_manager, p_header_size); if (l_cp->ppm_markers[l_Z_ppm].m_data == NULL) { /* clean up to be done on l_cp destruction */ - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read PPM marker\n"); return OPJ_FALSE; } l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size; @@ -3577,16 +3348,17 @@ static OPJ_BOOL opj_j2k_read_ppm ( /** * Merges all PPM markers read (Packed headers, main header) * + * @param p_j2k the jpeg2000 codec. * @param p_cp main coding parameters. - * @param p_manager the user event manager. */ -static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_j2k_merge_ppm (opj_j2k_t *p_j2k, opj_cp_t *p_cp) { OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining; /* preconditions */ assert(p_cp != 00); - assert(p_manager != 00); + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); assert(p_cp->ppm_buffer == NULL); if (p_cp->ppm == 0U) { @@ -3616,7 +3388,7 @@ static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_manager /* read Nppm */ if (l_data_size < 4U) { /* clean up to be done on l_cp destruction */ - opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough bytes to read Nppm\n"); return OPJ_FALSE; } opj_read_bytes(l_data, &l_N_ppm, 4); @@ -3638,13 +3410,13 @@ static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_manager if (l_N_ppm_remaining != 0U) { /* clean up to be done on l_cp destruction */ - opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Corrupted PPM markers\n"); return OPJ_FALSE; } - p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size); + p_cp->ppm_buffer = (OPJ_BYTE *) opj_manager_malloc(p_j2k->m_manager, l_ppm_data_size); if (p_cp->ppm_buffer == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read PPM marker\n"); return OPJ_FALSE; } p_cp->ppm_len = l_ppm_data_size; @@ -3675,7 +3447,7 @@ static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_manager /* read Nppm */ if (l_data_size < 4U) { /* clean up to be done on l_cp destruction */ - opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough bytes to read Nppm\n"); return OPJ_FALSE; } opj_read_bytes(l_data, &l_N_ppm, 4); @@ -3695,7 +3467,7 @@ static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_manager } } while (l_data_size > 0U); } - opj_free(p_cp->ppm_markers[i].m_data); + opj_manager_free(p_j2k->m_manager, p_cp->ppm_markers[i].m_data); p_cp->ppm_markers[i].m_data = NULL; p_cp->ppm_markers[i].m_data_size = 0U; } @@ -3705,7 +3477,7 @@ static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_manager p_cp->ppm_data_size = p_cp->ppm_len; p_cp->ppm_markers_count = 0U; - opj_free(p_cp->ppm_markers); + opj_manager_free(p_j2k->m_manager, p_cp->ppm_markers); p_cp->ppm_markers = NULL; return OPJ_TRUE; @@ -3717,12 +3489,10 @@ static OPJ_BOOL opj_j2k_merge_ppm ( opj_cp_t *p_cp, opj_event_mgr_t * p_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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { opj_cp_t *l_cp = 00; @@ -3732,17 +3502,17 @@ static OPJ_BOOL opj_j2k_read_ppt ( opj_j2k_t *p_j2k, /* preconditions */ assert(p_header_data != 00); assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_manager != 00); /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */ if (p_header_size < 2) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading PPT marker\n"); return OPJ_FALSE; } l_cp = &(p_j2k->m_cp); if (l_cp->ppm){ - opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n"); return OPJ_FALSE; } @@ -3758,19 +3528,19 @@ static OPJ_BOOL opj_j2k_read_ppt ( opj_j2k_t *p_j2k, OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ assert(l_tcp->ppt_markers_count == 0U); - l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx)); + l_tcp->ppt_markers = (opj_ppx *) opj_manager_calloc(p_j2k->m_manager, l_newCount, sizeof(opj_ppx)); if (l_tcp->ppt_markers == NULL) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read PPT marker\n"); return OPJ_FALSE; } l_tcp->ppt_markers_count = l_newCount; } else if (l_tcp->ppt_markers_count <= l_Z_ppt) { OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */ opj_ppx *new_ppt_markers; - new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers, l_newCount * sizeof(opj_ppx)); + new_ppt_markers = (opj_ppx *) opj_manager_realloc(p_j2k->m_manager, l_tcp->ppt_markers, l_newCount * sizeof(opj_ppx)); if (new_ppt_markers == NULL) { /* clean up to be done on l_tcp destruction */ - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read PPT marker\n"); return OPJ_FALSE; } l_tcp->ppt_markers = new_ppt_markers; @@ -3780,14 +3550,14 @@ static OPJ_BOOL opj_j2k_read_ppt ( opj_j2k_t *p_j2k, if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) { /* clean up to be done on l_tcp destruction */ - opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n", l_Z_ppt); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Zppt %u already read\n", l_Z_ppt); return OPJ_FALSE; } - l_tcp->ppt_markers[l_Z_ppt].m_data = opj_malloc(p_header_size); + l_tcp->ppt_markers[l_Z_ppt].m_data = opj_manager_malloc(p_j2k->m_manager, p_header_size); if (l_tcp->ppt_markers[l_Z_ppt].m_data == NULL) { /* clean up to be done on l_tcp destruction */ - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read PPT marker\n"); return OPJ_FALSE; } l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size; @@ -3798,16 +3568,17 @@ static OPJ_BOOL opj_j2k_read_ppt ( opj_j2k_t *p_j2k, /** * Merges all PPT markers read (Packed packet headers, tile-part header) * + * @param p_j2k the jpeg2000 codec. * @param p_tcp the tile. - * @param p_manager the user event manager. */ -static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager) +static OPJ_BOOL opj_j2k_merge_ppt(opj_j2k_t *p_j2k, opj_tcp_t *p_tcp) { OPJ_UINT32 i, l_ppt_data_size; /* preconditions */ assert(p_tcp != 00); - assert(p_manager != 00); assert(p_tcp->ppt_buffer == NULL); + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); if (p_tcp->ppt == 0U) { return OPJ_TRUE; @@ -3818,9 +3589,9 @@ static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager) l_ppt_data_size += p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ } - p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size); + p_tcp->ppt_buffer = (OPJ_BYTE *) opj_manager_malloc(p_j2k->m_manager, l_ppt_data_size); if (p_tcp->ppt_buffer == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read PPT marker\n"); return OPJ_FALSE; } p_tcp->ppt_len = l_ppt_data_size; @@ -3830,14 +3601,14 @@ static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager) memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data, p_tcp->ppt_markers[i].m_data_size); l_ppt_data_size += p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */ - opj_free(p_tcp->ppt_markers[i].m_data); + opj_manager_free(p_j2k->m_manager, p_tcp->ppt_markers[i].m_data); p_tcp->ppt_markers[i].m_data = NULL; p_tcp->ppt_markers[i].m_data_size = 0U; } } p_tcp->ppt_markers_count = 0U; - opj_free(p_tcp->ppt_markers); + opj_manager_free(p_j2k->m_manager, p_tcp->ppt_markers); p_tcp->ppt_markers = NULL; p_tcp->ppt_data = p_tcp->ppt_buffer; @@ -3845,70 +3616,65 @@ static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager) return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_tlm( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager - ) +static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { - OPJ_BYTE * l_current_data = 00; - OPJ_UINT32 l_tlm_size; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_tlm_size; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); + assert(p_stream != 00); - l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts); + 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(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; - } + 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_manager_realloc(p_j2k->m_manager, p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size); + if (! new_header_tile_data) { + opj_manager_free(p_j2k->m_manager, 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(&(p_j2k->m_manager->event_mgr), 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; + 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); + /* 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,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,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,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; + 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; - } + /* 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_j2k->m_manager->event_mgr)) != l_tlm_size) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } static OPJ_BOOL opj_j2k_write_sot( opj_j2k_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 + const opj_stream_private_t *p_stream ) { /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); assert(p_stream != 00); opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */ @@ -3943,17 +3709,17 @@ static OPJ_BOOL opj_j2k_write_sot( opj_j2k_t *p_j2k, return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - OPJ_UINT32* p_tile_no, - OPJ_UINT32* p_tot_len, - OPJ_UINT32* p_current_part, - OPJ_UINT32* p_num_parts, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_j2k_get_sot_values( opj_event_mgr_t * p_manager, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + OPJ_UINT32* p_tile_no, + OPJ_UINT32* p_tot_len, + OPJ_UINT32* p_current_part, + OPJ_UINT32* p_num_parts ) { /* preconditions */ - assert(p_header_data != 00); assert(p_manager != 00); + assert(p_header_data != 00); /* Size of this marker is fixed = 12 (we have already read marker and its size)*/ if (p_header_size != 8) { @@ -3974,462 +3740,456 @@ static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data, static OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ) + OPJ_UINT32 p_header_size ) { - opj_cp_t *l_cp = 00; - opj_tcp_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; + opj_cp_t *l_cp = 00; + opj_tcp_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 */ + /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); - if (! opj_j2k_get_sot_values(p_header_data, p_header_size, &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts, p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); - return OPJ_FALSE; - } + if (!opj_j2k_get_sot_values(&(p_j2k->m_manager->event_mgr), p_header_data, p_header_size, &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading SOT marker\n"); + return OPJ_FALSE; + } - l_cp = &(p_j2k->m_cp); + l_cp = &(p_j2k->m_cp); - /* testcase 2.pdf.SIGFPE.706.1112 */ - if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", p_j2k->m_current_tile_number); - return OPJ_FALSE; - } + /* testcase 2.pdf.SIGFPE.706.1112 */ + if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Invalid tile number %d\n", p_j2k->m_current_tile_number); + return OPJ_FALSE; + } - 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) { + if (l_cp->correct) { - OPJ_UINT32 tileno = p_j2k->m_current_tile_number; - static OPJ_UINT32 backup_tileno = 0; + 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 > (l_cp->tw * l_cp->th)) { - opj_event_msg(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(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - /* we try to correct */ - tileno = backup_tileno; - opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n" - "- setting tile number to %d\n", - tileno); - } + /* tileno is negative or larger than the number of tiles!!! */ + if (tileno > (l_cp->tw * l_cp->th)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr)); + return OPJ_FALSE; + } + /* we try to correct */ + tileno = backup_tileno; + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "- trying to adjust this\n" + "- setting tile number to %d\n", + tileno); + } - /* keep your private count of tiles */ - backup_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 */ - /* PSot should be equal to zero or >=14 or <= 2^32-1 */ - if ((l_tot_len !=0 ) && (l_tot_len < 14) ) + /* PSot should be equal to zero or >=14 or <= 2^32-1 */ + if ((l_tot_len !=0 ) && (l_tot_len < 14) ) + { + if (l_tot_len == 12 ) /* MSD: Special case for the PHR data which are read by kakadu*/ { - if (l_tot_len == 12 ) /* MSD: Special case for the PHR data which are read by kakadu*/ - { - opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", l_tot_len); - } - else - { - opj_event_msg(p_manager, EVT_ERROR, "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len); - return OPJ_FALSE; - } + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", l_tot_len); } + else + { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Psot value is not correct regards to the JPEG2000 norm: %d.\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(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(p_manager, EVT_ERROR, "JPWL: giving up\n"); - return OPJ_FALSE; - } - /* we try to correct */ - l_tot_len = 0; - opj_event_msg(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(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + /* we try to correct */ + l_tot_len = 0; + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(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; - } + /* 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(&(p_j2k->m_manager->event_mgr), 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; + } - if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */ - l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction; - /* 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(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; - } - } - if( l_current_part >= l_num_parts ) { - /* testcase 451.pdf.SIGSEGV.ce9.3723 */ - opj_event_msg(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current " - "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_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 (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */ + l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction; + /* 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(&(p_j2k->m_manager->event_mgr), 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; + } + } + if( l_current_part >= l_num_parts ) { + /* testcase 451.pdf.SIGSEGV.ce9.3723 */ + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current " + "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_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 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; - } + 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; + 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); - } + /* 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; + /* 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 (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)); - if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n"); - return OPJ_FALSE; - } - } - 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(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\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].tp_index = + (opj_tp_index_t*)opj_manager_calloc(p_j2k->m_manager, l_num_parts, sizeof(opj_tp_index_t)); + if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n"); + return OPJ_FALSE; + } + } + else { + opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_manager_realloc(p_j2k->m_manager, + 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_manager_free(p_j2k->m_manager, 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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\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 (!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 = 0; - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n"); - return OPJ_FALSE; - } - } + 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_manager_calloc(p_j2k->m_manager, p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps, + sizeof(opj_tp_index_t)); + 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 = 0; + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n"); + return OPJ_FALSE; + } + } - if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){ - opj_tp_index_t *new_tp_index; - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_current_part + 1; - 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(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n"); - return OPJ_FALSE; - } - p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index; - } - } + if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){ + opj_tp_index_t *new_tp_index; + p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_current_part + 1; + new_tp_index = (opj_tp_index_t *) opj_manager_realloc( + p_j2k->m_manager, + 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_manager_free(p_j2k->m_manager, 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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\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; - } + /* 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; + 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; - } + 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; - } + 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; +} static OPJ_BOOL opj_j2k_write_sod( opj_j2k_t *p_j2k, opj_tcd_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 + const opj_stream_private_t *p_stream ) { - opj_codestream_info_t *l_cstr_info = 00; - OPJ_UINT32 l_remaining_data; + opj_codestream_info_t *l_cstr_info = 00; + OPJ_UINT32 l_remaining_data; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); + assert(p_stream != 00); - opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */ - p_data += 2; + 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; + /* make room for the EOF marker */ + l_remaining_data = p_total_data_size - 4; - /* 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; + /* 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; - /* 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>> */ + /* 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 */ - /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2); + /* update markers struct */ + /*OPJ_BOOL res = 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; - } - } + if (p_j2k->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(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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Cannot encode tile\n"); + return OPJ_FALSE; + } - *p_data_written += 2; + *p_data_written += 2; - return OPJ_TRUE; + return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_read_sod (opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager - ) +static OPJ_BOOL opj_j2k_read_sod (opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { - OPJ_SIZE_T l_current_read_size; - opj_codestream_index_t * l_cstr_index = 00; - OPJ_BYTE ** l_current_data = 00; - opj_tcp_t * l_tcp = 00; - OPJ_UINT32 * l_tile_len = 00; - OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE; + OPJ_SIZE_T l_current_read_size; + opj_codestream_index_t * l_cstr_index = 00; + OPJ_BYTE ** l_current_data = 00; + opj_tcp_t * l_tcp = 00; + OPJ_UINT32 * l_tile_len = 00; + OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE; - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); + assert(p_stream != 00); - l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); + 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); + 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 { + /* Check to avoid pass the limit of OPJ_UINT32 */ + if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2 ) + p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; + else { + /* MSD: case commented to support empty SOT marker (PHR data) */ + } + } + + l_current_data = &(l_tcp->m_data); + l_tile_len = &l_tcp->m_data_size; + + /* Patch to support new PHR data */ + if (p_j2k->m_specific_param.m_decoder.m_sot_length) { + /* If we are here, we'll try to read the data after allocation */ + /* Check enough bytes left in stream before allocation */ + if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length > opj_stream_get_number_byte_left(p_stream)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Tile part length size inconsistent with stream length\n"); + return OPJ_FALSE; + } + 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_manager_malloc(p_j2k->m_manager, p_j2k->m_specific_param.m_decoder.m_sot_length); } else { - /* Check to avoid pass the limit of OPJ_UINT32 */ - if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2 ) - p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; - else { - /* MSD: case commented to support empty SOT marker (PHR data) */ + OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_manager_realloc(p_j2k->m_manager, *l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length); + if (! l_new_current_data) { + opj_manager_free(p_j2k->m_manager, *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; } - l_current_data = &(l_tcp->m_data); - l_tile_len = &l_tcp->m_data_size; + if (*l_current_data == 00) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to decode tile\n"); + return OPJ_FALSE; + } + } + else { + l_sot_length_pb_detected = OPJ_TRUE; + } - /* Patch to support new PHR data */ - if (p_j2k->m_specific_param.m_decoder.m_sot_length) { - /* If we are here, we'll try to read the data after allocation */ - /* Check enough bytes left in stream before allocation */ - if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length > opj_stream_get_number_byte_left(p_stream)) { - opj_event_msg(p_manager, EVT_ERROR, "Tile part length size inconsistent with stream length\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; + + 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; + + if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_manager, 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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to add tl marker\n"); + return OPJ_FALSE; } - 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(p_manager, EVT_ERROR, "Not enough memory to decode tile\n"); - return OPJ_FALSE; - } - } - else { - l_sot_length_pb_detected = OPJ_TRUE; - } - /* Index */ - l_cstr_index = p_j2k->cstr_index; - if (l_cstr_index) { - OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2; + /*l_cstr_index->packno = 0;*/ + } - 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; + /* Patch to support new PHR data */ + if (!l_sot_length_pb_detected) { + 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_j2k->m_manager->event_mgr)); + } + else + { + l_current_read_size = 0; + } - 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(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); - return OPJ_FALSE; - } + 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_cstr_index->packno = 0;*/ - } + *l_tile_len += (OPJ_UINT32)l_current_read_size; - /* Patch to support new PHR data */ - if (!l_sot_length_pb_detected) { - 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); - } - else - { - l_current_read_size = 0; - } - - 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 += (OPJ_UINT32)l_current_read_size; - - return OPJ_TRUE; + return OPJ_TRUE; } static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, OPJ_UINT32 p_tile_no, OPJ_UINT32 p_comp_no, OPJ_UINT32 nb_comps, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager + opj_stream_private_t *p_stream ) { OPJ_BYTE * l_current_data = 00; @@ -4441,7 +4201,6 @@ static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); assert(p_stream != 00); l_cp = &(p_j2k->m_cp); @@ -4474,121 +4233,115 @@ static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k, opj_write_bytes(l_current_data, (OPJ_UINT32)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) { + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,&(p_j2k->m_manager->event_mgr)) != l_rgn_size) { return OPJ_FALSE; } return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_eoc( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager - ) +static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { - /* preconditions */ - assert(p_j2k != 00); - assert(p_manager != 00); - assert(p_stream != 00); + /* preconditions */ + assert(p_j2k != 00); + assert(p_j2k->m_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 */ + 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 */ - /* - OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2); + /* update markers struct */ + /* + OPJ_BOOL res = 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_j2k->m_manager->event_mgr)) != 2) { + return OPJ_FALSE; + } - if ( ! opj_stream_flush(p_stream,p_manager) ) { - return OPJ_FALSE; - } + if ( ! opj_stream_flush(p_stream,&(p_j2k->m_manager->event_mgr)) ) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return OPJ_TRUE; } /** * Reads a RGN marker (Region Of Interest) * + * @param p_j2k the jpeg2000 codec. * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { - OPJ_UINT32 l_nb_comp; - opj_image_t * l_image = 00; + OPJ_UINT32 l_nb_comp; + opj_image_t * l_image = 00; - opj_cp_t *l_cp = 00; - opj_tcp_t *l_tcp = 00; - OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty; + opj_cp_t *l_cp = 00; + opj_tcp_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_j2k->m_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(p_manager, EVT_ERROR, "Error reading RGN marker\n"); - return OPJ_FALSE; - } + if (p_header_size != 2 + l_comp_room) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(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(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(&(p_j2k->m_manager->event_mgr), 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(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "JPWL: giving up\n"); + return OPJ_FALSE; + } + } + }; #endif /* USE_JPWL */ - /* testcase 3635.pdf.asan.77.2930 */ - if (l_comp_no >= l_nb_comp) { - opj_event_msg(p_manager, EVT_ERROR, - "bad component number in RGN (%d when there are only %d)\n", - l_comp_no, l_nb_comp); - return OPJ_FALSE; - } + /* testcase 3635.pdf.asan.77.2930 */ + if (l_comp_no >= l_nb_comp) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, + "bad component number in RGN (%d when there are only %d)\n", + l_comp_no, l_nb_comp); + return OPJ_FALSE; + } - opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */ - ++p_header_data; - - return OPJ_TRUE; + opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */ + ++p_header_data; + return OPJ_TRUE; } static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp) @@ -4602,9 +4355,7 @@ static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp) return 0; } -static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { opj_cp_t * l_cp = 00; opj_image_t * l_image = 00; @@ -4622,7 +4373,7 @@ static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k, /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_manager != 00); assert(p_stream != 00); l_cp = &(p_j2k->m_cp); @@ -4745,14 +4496,14 @@ static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *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); + (OPJ_BYTE *) opj_manager_malloc(p_j2k->m_manager, 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 (OPJ_IS_CINEMA(l_cp->rsiz)) { 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); + (OPJ_BYTE *) opj_manager_malloc(p_j2k->m_manager, 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; } @@ -4785,19 +4536,19 @@ static OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k, l_tcd = opj_tcd_create(OPJ_TRUE); if (l_tcd == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + opj_tcd_destroy(p_j2k->m_manager, l_tcd); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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); + l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index, &(p_j2k->m_manager->event_mgr)); /* cleanup */ if (! l_success) { @@ -4806,22 +4557,19 @@ static OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k, } } - opj_j2k_tcp_destroy(l_tcp); + opj_j2k_tcp_destroy(p_j2k->m_manager, l_tcp); ++l_tcp; } - opj_tcd_destroy(l_tcd); + opj_tcd_destroy(p_j2k->m_manager, l_tcd); return OPJ_TRUE; } #endif -static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) +static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, struct opj_stream_private *p_stream) { /* 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); @@ -4829,9 +4577,7 @@ static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k, return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) +static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k, struct opj_stream_private *p_stream) { OPJ_UINT32 i; opj_simple_mcc_decorrelation_data_t * l_mcc_record; @@ -4841,9 +4587,8 @@ static OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k, /* preconditions */ assert(p_j2k != 00); assert(p_stream != 00); - assert(p_manager != 00); - if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) { + if (! opj_j2k_write_cbd(p_j2k,p_stream)) { return OPJ_FALSE; } @@ -4852,7 +4597,7 @@ static OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k, for (i=0;im_nb_mct_records;++i) { - if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) { + if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream)) { return OPJ_FALSE; } @@ -4863,14 +4608,14 @@ static OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k, for (i=0;im_nb_mcc_records;++i) { - if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) { + if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream)) { return OPJ_FALSE; } ++l_mcc_record; } - if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) { + if (! opj_j2k_write_mco(p_j2k,p_stream)) { return OPJ_FALSE; } @@ -4924,16 +4669,13 @@ static OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k, #endif -static OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) +static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k, struct opj_stream_private *p_stream) { OPJ_UINT32 compno; const opj_tccp_t *l_tccp = 00; /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); assert(p_stream != 00); l_tccp = p_j2k->m_cp.tcps->tccps; @@ -4941,7 +4683,7 @@ static OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k, 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_j2k->m_private_image->numcomps,p_stream,p_manager)) { + if (! opj_j2k_write_rgn(p_j2k,0,compno,p_j2k->m_private_image->numcomps,p_stream)) { return OPJ_FALSE; } } @@ -4952,15 +4694,12 @@ static OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k, return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_epc( opj_j2k_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) +static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k, struct opj_stream_private *p_stream) { 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; @@ -4992,8 +4731,7 @@ static OPJ_BOOL opj_j2k_write_epc( opj_j2k_t *p_j2k, static OPJ_BOOL opj_j2k_read_unk ( opj_j2k_t *p_j2k, opj_stream_private_t *p_stream, - OPJ_UINT32 *output_marker, - opj_event_mgr_t * p_manager + OPJ_UINT32 *output_marker ) { OPJ_UINT32 l_unknown_marker; @@ -5002,15 +4740,15 @@ static OPJ_BOOL opj_j2k_read_unk ( opj_j2k_t *p_j2k, /* preconditions*/ assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_manager != 00); assert(p_stream != 00); - opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Unknown marker\n"); for (;;) { /* 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,&(p_j2k->m_manager->event_mgr)) != 2) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short\n"); return OPJ_FALSE; } @@ -5023,7 +4761,7 @@ static OPJ_BOOL opj_j2k_read_unk ( opj_j2k_t *p_j2k, 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(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Marker is not compliant with its position\n"); return OPJ_FALSE; } else { @@ -5031,11 +4769,11 @@ static OPJ_BOOL opj_j2k_read_unk ( opj_j2k_t *p_j2k, /* 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_BOOL res = opj_j2k_add_mhmarker(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to add mh marker\n"); return OPJ_FALSE; } } @@ -5054,8 +4792,7 @@ static OPJ_BOOL opj_j2k_read_unk ( opj_j2k_t *p_j2k, static OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k, opj_mct_data_t * p_mct_record, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) + struct opj_stream_private *p_stream ) { OPJ_UINT32 l_mct_size; OPJ_BYTE * l_current_data = 00; @@ -5063,18 +4800,17 @@ static OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k, /* 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); + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_manager_realloc(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -5103,7 +4839,7 @@ static OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k, 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) { + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,&(p_j2k->m_manager->event_mgr)) != l_mct_size) { return OPJ_FALSE; } @@ -5116,12 +4852,10 @@ static OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { OPJ_UINT32 i; @@ -5139,7 +4873,7 @@ static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k, p_j2k->m_specific_param.m_decoder.m_default_tcp; if (p_header_size < 2) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCT marker\n"); return OPJ_FALSE; } @@ -5147,12 +4881,12 @@ static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k, opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */ p_header_data += 2; if (l_tmp != 0) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n"); return OPJ_TRUE; } if(p_header_size <= 6) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCT marker\n"); return OPJ_FALSE; } @@ -5176,13 +4910,13 @@ static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k, opj_mct_data_t *new_mct_records; l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; - 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)); + new_mct_records = (opj_mct_data_t *) opj_manager_realloc(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read MCT marker\n"); return OPJ_FALSE; } l_tcp->m_mct_records = new_mct_records; @@ -5195,7 +4929,7 @@ static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k, } if (l_mct_data->m_data) { - opj_free(l_mct_data->m_data); + opj_manager_free(p_j2k->m_manager, l_mct_data->m_data); l_mct_data->m_data = 00; } @@ -5206,15 +4940,15 @@ static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k, opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */ p_header_data+=2; if (l_tmp != 0) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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); + l_mct_data->m_data = (OPJ_BYTE*)opj_manager_malloc(p_j2k->m_manager, p_header_size); if (! l_mct_data->m_data) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCT marker\n"); return OPJ_FALSE; } memcpy(l_mct_data->m_data,p_header_data,p_header_size); @@ -5226,8 +4960,7 @@ static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k, static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k, struct opj_simple_mcc_decorrelation_data * p_mcc_record, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) + struct opj_stream_private *p_stream ) { OPJ_UINT32 i; OPJ_UINT32 l_mcc_size; @@ -5238,7 +4971,6 @@ static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k, /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); assert(p_stream != 00); if (p_mcc_record->m_nb_comps > 255 ) { @@ -5253,12 +4985,12 @@ static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k, 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); + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_manager_realloc(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -5320,7 +5052,7 @@ static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k, 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) { + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,&(p_j2k->m_manager->event_mgr)) != l_mcc_size) { return OPJ_FALSE; } @@ -5329,8 +5061,7 @@ static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k, static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ) + OPJ_UINT32 p_header_size ) { OPJ_UINT32 i,j; OPJ_UINT32 l_tmp; @@ -5345,14 +5076,13 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, /* 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(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCC marker\n"); return OPJ_FALSE; } @@ -5360,12 +5090,12 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */ p_header_data += 2; if (l_tmp != 0) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Cannot take in charge multiple data spanning\n"); return OPJ_TRUE; } if (p_header_size < 7) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCC marker\n"); return OPJ_FALSE; } @@ -5387,14 +5117,14 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, opj_simple_mcc_decorrelation_data_t *new_mcc_records; l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS; - new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( + new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_manager_realloc(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to read MCC marker\n"); return OPJ_FALSE; } l_tcp->m_mcc_records = new_mcc_records; @@ -5409,7 +5139,7 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */ p_header_data+=2; if (l_tmp != 0) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Cannot take in charge multiple data spanning\n"); return OPJ_TRUE; } @@ -5417,7 +5147,7 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, p_header_data+=2; if (l_nb_collections > 1) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Cannot take in charge multiple collections\n"); return OPJ_TRUE; } @@ -5425,7 +5155,7 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, for (i=0;im_manager->event_mgr), EVT_ERROR, "Error reading MCC marker\n"); return OPJ_FALSE; } @@ -5433,7 +5163,7 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, ++p_header_data; if (l_tmp != 1) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n"); return OPJ_TRUE; } @@ -5446,7 +5176,7 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, 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(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCC marker\n"); return OPJ_FALSE; } @@ -5457,7 +5187,7 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, p_header_data+=l_nb_bytes_by_comp; if (l_tmp != j) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); return OPJ_TRUE; } } @@ -5469,12 +5199,12 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, l_nb_comps &= 0x7fff; if (l_nb_comps != l_mcc_record->m_nb_comps) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCC marker\n"); return OPJ_FALSE; } @@ -5485,7 +5215,7 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, p_header_data+=l_nb_bytes_by_comp; if (l_tmp != j) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); return OPJ_TRUE; } } @@ -5509,7 +5239,7 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, } if (l_mcc_record->m_decorrelation_array == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCC marker\n"); return OPJ_FALSE; } } @@ -5526,14 +5256,14 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, } if (l_mcc_record->m_offset_array == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCC marker\n"); return OPJ_FALSE; } } } if (p_header_size != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCC marker\n"); return OPJ_FALSE; } @@ -5542,10 +5272,7 @@ static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k, return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager - ) +static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k, struct opj_stream_private *p_stream) { OPJ_BYTE * l_current_data = 00; OPJ_UINT32 l_mco_size; @@ -5555,7 +5282,7 @@ static OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k, /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_manager != 00); assert(p_stream != 00); l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); @@ -5563,12 +5290,12 @@ static OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k, 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); + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_manager_realloc(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -5593,7 +5320,7 @@ static OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k, ++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) { + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,&(p_j2k->m_manager->event_mgr)) != l_mco_size) { return OPJ_FALSE; } @@ -5606,12 +5333,10 @@ static OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { OPJ_UINT32 l_tmp, i; @@ -5623,7 +5348,6 @@ static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k, /* 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 ? @@ -5631,7 +5355,7 @@ static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k, p_j2k->m_specific_param.m_decoder.m_default_tcp; if (p_header_size < 1) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading MCO marker\n"); return OPJ_FALSE; } @@ -5639,12 +5363,12 @@ static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k, ++p_header_data; if (l_nb_stages > 1) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Cannot take in charge multiple transformation stages.\n"); return OPJ_TRUE; } if (p_header_size != l_nb_stages + 1) { - opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Error reading MCO marker\n"); return OPJ_FALSE; } @@ -5656,7 +5380,7 @@ static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k, } if (l_tcp->m_mct_decoding_matrix) { - opj_free(l_tcp->m_mct_decoding_matrix); + opj_manager_free(p_j2k->m_manager, l_tcp->m_mct_decoding_matrix); l_tcp->m_mct_decoding_matrix = 00; } @@ -5664,7 +5388,7 @@ static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k, opj_read_bytes(p_header_data,&l_tmp,1); ++p_header_data; - if (! opj_j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) { + if (! opj_j2k_add_mct(p_j2k->m_manager, l_tcp,p_j2k->m_private_image,l_tmp)) { return OPJ_FALSE; } } @@ -5672,7 +5396,7 @@ static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k, return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index) +static OPJ_BOOL opj_j2k_add_mct(opj_manager_t manager, opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index) { OPJ_UINT32 i; opj_simple_mcc_decorrelation_data_t * l_mcc_record; @@ -5683,6 +5407,7 @@ static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UI opj_tccp_t * l_tccp; /* preconditions */ + assert(manager != 00); assert(p_tcp != 00); l_mcc_record = p_tcp->m_mcc_records; @@ -5713,7 +5438,7 @@ static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UI l_nb_elem = p_image->numcomps * p_image->numcomps; l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32); - p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size); + p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_manager_malloc(manager, l_mct_size); if (! p_tcp->m_mct_decoding_matrix ) { return OPJ_FALSE; @@ -5732,7 +5457,7 @@ static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UI l_nb_elem = p_image->numcomps; l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32); - l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size); + l_offset_data = (OPJ_UINT32*)opj_manager_malloc(manager, l_offset_size); if (! l_offset_data ) { return OPJ_FALSE; @@ -5748,15 +5473,13 @@ static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UI ++l_tccp; } - opj_free(l_offset_data); + opj_manager_free(manager, l_offset_data); } return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) +static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k, struct opj_stream_private *p_stream) { OPJ_UINT32 i; OPJ_UINT32 l_cbd_size; @@ -5766,19 +5489,19 @@ static OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k, /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_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); + OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_manager_realloc(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -5805,7 +5528,7 @@ static OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k, ++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) { + if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,&(p_j2k->m_manager->event_mgr)) != l_cbd_size) { return OPJ_FALSE; } @@ -5817,12 +5540,10 @@ static OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k, * @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_t *p_j2k, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { OPJ_UINT32 l_nb_comp,l_num_comp; @@ -5833,12 +5554,11 @@ static OPJ_BOOL opj_j2k_read_cbd ( opj_j2k_t *p_j2k, /* preconditions */ assert(p_header_data != 00); assert(p_j2k != 00); - assert(p_manager != 00); l_num_comp = p_j2k->m_private_image->numcomps; if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) { - opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Crror reading CBD marker\n"); return OPJ_FALSE; } @@ -5846,7 +5566,7 @@ static OPJ_BOOL opj_j2k_read_cbd ( opj_j2k_t *p_j2k, p_header_data+=2; if (l_nb_comp != l_num_comp) { - opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Crror reading CBD marker\n"); return OPJ_FALSE; } @@ -5884,34 +5604,38 @@ void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) /* J2K encoder interface */ /* ----------------------------------------------------------------------- */ -opj_j2k_t* opj_j2k_create_compress(void) +opj_j2k_t* opj_j2k_create_compress(opj_manager_t manager) { - opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1,sizeof(opj_j2k_t)); + if (manager == NULL) { + return NULL; + } + opj_j2k_t *l_j2k = (opj_j2k_t*) opj_manager_calloc(manager, 1,sizeof(opj_j2k_t)); if (!l_j2k) { return NULL; } + l_j2k->m_manager = manager; 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(OPJ_J2K_DEFAULT_HEADER_SIZE); + l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_manager_malloc(manager, OPJ_J2K_DEFAULT_HEADER_SIZE); if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) { - opj_j2k_destroy(l_j2k); + opj_j2k_destroy(l_j2k); return NULL; } l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE; /* validation list creation*/ - l_j2k->m_validation_list = opj_procedure_list_create(); + l_j2k->m_validation_list = opj_procedure_list_create(manager); 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(); + l_j2k->m_procedure_list = opj_procedure_list_create(manager); if (! l_j2k->m_procedure_list) { opj_j2k_destroy(l_j2k); return NULL; @@ -6139,8 +5863,7 @@ static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, opj_cparameters_t *parameters, - opj_image_t *image, - opj_event_mgr_t * p_manager) + opj_image_t *image) { OPJ_UINT32 i, j, tileno, numpocs_tile; opj_cp_t *cp = 00; @@ -6150,7 +5873,7 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, } if ((parameters->numresolution <= 0) || (parameters->numresolution > OPJ_J2K_MAXRLVLS)) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid number of resolutions : %d not in range [1,%d]\n", parameters->numresolution, OPJ_J2K_MAXRLVLS); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Invalid number of resolutions : %d not in range [1,%d]\n", parameters->numresolution, OPJ_J2K_MAXRLVLS); return OPJ_FALSE; } @@ -6204,7 +5927,7 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, break; } if (deprecated_used) { - opj_event_msg(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Deprecated fields cp_cinema or cp_rsiz are used\n" "Please consider using only the rsiz field\n" "See openjpeg.h documentation for more details\n"); @@ -6233,7 +5956,7 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, } } if (cap) { - opj_event_msg(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "The desired maximum codestream size has limited\n" "at least one of the desired quality layers\n"); } @@ -6244,36 +5967,36 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, if (OPJ_IS_CINEMA(parameters->rsiz)){ if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K) || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)){ - opj_event_msg(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n"); parameters->rsiz = OPJ_PROFILE_NONE; } else { - opj_j2k_set_cinema_parameters(parameters,image,p_manager); - if (!opj_j2k_is_cinema_compliant(image,parameters->rsiz,p_manager)) { + opj_j2k_set_cinema_parameters(parameters,image,&(p_j2k->m_manager->event_mgr)); + if (!opj_j2k_is_cinema_compliant(image,parameters->rsiz,&(p_j2k->m_manager->event_mgr))) { parameters->rsiz = OPJ_PROFILE_NONE; } } } else if (OPJ_IS_STORAGE(parameters->rsiz)) { - opj_event_msg(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "JPEG 2000 Long Term Storage profile not yet supported\n"); parameters->rsiz = OPJ_PROFILE_NONE; } else if (OPJ_IS_BROADCAST(parameters->rsiz)) { - opj_event_msg(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "JPEG 2000 Broadcast profiles not yet supported\n"); parameters->rsiz = OPJ_PROFILE_NONE; } else if (OPJ_IS_IMF(parameters->rsiz)) { - opj_event_msg(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "JPEG 2000 IMF profiles not yet supported\n"); parameters->rsiz = OPJ_PROFILE_NONE; } else if (OPJ_IS_PART2(parameters->rsiz)) { if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) { - opj_event_msg(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "JPEG 2000 Part-2 profile defined\n" "but no Part-2 extension enabled.\n" "Profile set to NONE.\n"); parameters->rsiz = OPJ_PROFILE_NONE; } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) { - opj_event_msg(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Unsupported Part-2 extension enabled\n" "Profile set to NONE.\n"); parameters->rsiz = OPJ_PROFILE_NONE; @@ -6292,9 +6015,9 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, /* mod fixed_quality */ if (parameters->cp_fixed_alloc && parameters->cp_matrice) { size_t array_size = (size_t)parameters->tcp_numlayers * (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32); - cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size); + cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_manager_malloc(p_j2k->m_manager, array_size); if (!cp->m_specific_param.m_enc.m_matrice) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate copy of user encoding parameters matrix \n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to allocate copy of user encoding parameters matrix \n"); return OPJ_FALSE; } memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size); @@ -6310,9 +6033,9 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, /* comment string */ if(parameters->cp_comment) { - cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U); + cp->comment = (char*)opj_manager_malloc(p_j2k->m_manager, strlen(parameters->cp_comment) + 1U); if(!cp->comment) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate copy of comment string\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to allocate copy of comment string\n"); return OPJ_FALSE; } strcpy(cp->comment, parameters->cp_comment); @@ -6324,16 +6047,16 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, /* UniPG>> */ #ifdef USE_JPWL - cp->comment = (char*)opj_malloc(clen+strlen(version)+11); + cp->comment = (char*)opj_manager_malloc(p_j2k->m_manager, clen+strlen(version)+11); if(!cp->comment) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate comment string\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to allocate comment string\n"); return OPJ_FALSE; } sprintf(cp->comment,"%s%s with JPWL", comment, version); #else - cp->comment = (char*)opj_malloc(clen+strlen(version)+1); + cp->comment = (char*)opj_manager_malloc(p_j2k->m_manager, clen+strlen(version)+1); if(!cp->comment) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate comment string\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to allocate comment string\n"); return OPJ_FALSE; } sprintf(cp->comment,"%s%s", comment, version); @@ -6416,14 +6139,14 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, /* initialize the mutiple tiles */ /* ---------------------------- */ - cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t)); + cp->tcps = (opj_tcp_t*) opj_manager_calloc(p_j2k->m_manager, cp->tw * cp->th, sizeof(opj_tcp_t)); if (!cp->tcps) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate tile coding parameters\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to allocate tile coding parameters\n"); return OPJ_FALSE; } if (parameters->numpocs) { /* initialisation of POC */ - opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, (OPJ_UINT32)parameters->numresolution, image->numcomps, (OPJ_UINT32)parameters->tcp_numlayers, p_manager); + opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, (OPJ_UINT32)parameters->numresolution, image->numcomps, (OPJ_UINT32)parameters->tcp_numlayers, p_j2k->m_manager); /* TODO MSD use the return value*/ } @@ -6477,66 +6200,66 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, tcp->numpocs = 0; } - tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); + tcp->tccps = (opj_tccp_t*) opj_manager_calloc(p_j2k->m_manager, image->numcomps, sizeof(opj_tccp_t)); if (!tcp->tccps) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate tile component coding parameters\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to allocate tile component coding parameters\n"); return OPJ_FALSE; } if (parameters->mct_data) { OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32); - OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize); + OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_manager_malloc(p_j2k->m_manager, lMctSize); OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize); if (!lTmpBuf) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate temp buffer\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to allocate temp buffer\n"); return OPJ_FALSE; } tcp->mct = 2; - tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize); + tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_manager_malloc(p_j2k->m_manager, lMctSize); if (! tcp->m_mct_coding_matrix) { - opj_free(lTmpBuf); + opj_manager_free(p_j2k->m_manager, lTmpBuf); lTmpBuf = NULL; - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT coding matrix \n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to allocate encoder MCT coding matrix \n"); return OPJ_FALSE; } 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); + tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_manager_malloc(p_j2k->m_manager, lMctSize); if (! tcp->m_mct_decoding_matrix) { - opj_free(lTmpBuf); + opj_manager_free(p_j2k->m_manager, lTmpBuf); lTmpBuf = NULL; - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT decoding matrix \n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to allocate encoder MCT decoding matrix \n"); return OPJ_FALSE; } - if(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps) == OPJ_FALSE) { - opj_free(lTmpBuf); + if(opj_matrix_inversion_f(p_j2k->m_manager, lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps) == OPJ_FALSE) { + opj_manager_free(p_j2k->m_manager, lTmpBuf); lTmpBuf = NULL; - opj_event_msg(p_manager, EVT_ERROR, "Failed to inverse encoder MCT decoding matrix \n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Failed to inverse encoder MCT decoding matrix \n"); return OPJ_FALSE; } tcp->mct_norms = (OPJ_FLOAT64*) - opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64)); + opj_manager_malloc(p_j2k->m_manager, image->numcomps * sizeof(OPJ_FLOAT64)); if (! tcp->mct_norms) { - opj_free(lTmpBuf); + opj_manager_free(p_j2k->m_manager, lTmpBuf); lTmpBuf = NULL; - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT norms \n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to allocate encoder MCT norms \n"); return OPJ_FALSE; } opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix); - opj_free(lTmpBuf); + opj_manager_free(p_j2k->m_manager, lTmpBuf); for (i = 0; i < image->numcomps; i++) { opj_tccp_t *tccp = &tcp->tccps[i]; tccp->m_dc_level_shift = l_dc_shift[i]; } - if (opj_j2k_setup_mct_encoding(tcp,image) == OPJ_FALSE) { + if (opj_j2k_setup_mct_encoding(p_j2k->m_manager, tcp,image) == OPJ_FALSE) { /* free will be handled by opj_j2k_destroy */ - opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Failed to setup j2k mct encoding\n"); return OPJ_FALSE; } } @@ -6546,7 +6269,7 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, (image->comps[0].dx != image->comps[2].dx) || (image->comps[0].dy != image->comps[1].dy) || (image->comps[0].dy != image->comps[2].dy)) { - opj_event_msg(p_manager, EVT_WARNING, "Cannot perform MCT on components with different sizes. Disabling MCT.\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Cannot perform MCT on components with different sizes. Disabling MCT.\n"); tcp->mct = 0; } } @@ -6632,14 +6355,15 @@ OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, } } + /* FIXME Shall we really do this ??? */ if (parameters->mct_data) { - opj_free(parameters->mct_data); + opj_manager_free(p_j2k->m_manager, parameters->mct_data); parameters->mct_data = 00; } return OPJ_TRUE; } -static OPJ_BOOL 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_manager_t manager, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) { assert(cstr_index != 00); @@ -6647,9 +6371,9 @@ static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UIN if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) { opj_marker_info_t *new_marker; cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->maxmarknum); - new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t)); + new_marker = (opj_marker_info_t *) opj_manager_realloc(manager, cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t)); if (! new_marker) { - opj_free(cstr_index->marker); + opj_manager_free(manager, cstr_index->marker); cstr_index->marker = NULL; cstr_index->maxmarknum = 0; cstr_index->marknum = 0; @@ -6667,7 +6391,7 @@ static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UIN 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) +static OPJ_BOOL opj_j2k_add_tlmarker(opj_manager_t manager, 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); @@ -6676,11 +6400,11 @@ static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t * if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) { opj_marker_info_t *new_marker; cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum); - new_marker = (opj_marker_info_t *) opj_realloc( + new_marker = (opj_marker_info_t *) opj_manager_realloc(manager, 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); + opj_manager_free(manager, 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; @@ -6713,67 +6437,66 @@ static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t * */ OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager + opj_stream_private_t *p_stream ) { (void)p_j2k; (void)p_stream; - (void)p_manager; return OPJ_TRUE; } OPJ_BOOL opj_j2k_read_header( opj_stream_private_t *p_stream, opj_j2k_t* p_j2k, - opj_image_t** p_image, - opj_event_mgr_t* p_manager ) + opj_image_t** p_image) { /* preconditions */ assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); assert(p_stream != 00); - assert(p_manager != 00); /* create an empty image header */ - p_j2k->m_private_image = opj_image_create0(); + p_j2k->m_private_image = opj_image_create0(p_j2k->m_manager); if (! p_j2k->m_private_image) { return OPJ_FALSE; } /* customization of the validation */ - if (! opj_j2k_setup_decoding_validation(p_j2k, p_manager)) { - opj_image_destroy(p_j2k->m_private_image); + if (! opj_j2k_setup_decoding_validation(p_j2k)) { + opj_manager_image_destroy(p_j2k->m_manager, p_j2k->m_private_image); p_j2k->m_private_image = NULL; return OPJ_FALSE; } /* 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); + if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream)) { + opj_manager_image_destroy(p_j2k->m_manager, p_j2k->m_private_image); p_j2k->m_private_image = NULL; return OPJ_FALSE; } /* customization of the encoding */ - if (! opj_j2k_setup_header_reading(p_j2k, p_manager)) { - opj_image_destroy(p_j2k->m_private_image); + if (! opj_j2k_setup_header_reading(p_j2k, &(p_j2k->m_manager->event_mgr))) { + opj_manager_image_destroy(p_j2k->m_manager, p_j2k->m_private_image); p_j2k->m_private_image = NULL; return OPJ_FALSE; } /* 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); + if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream)) { + opj_manager_image_destroy(p_j2k->m_manager, p_j2k->m_private_image); p_j2k->m_private_image = NULL; return OPJ_FALSE; } - *p_image = opj_image_create0(); + *p_image = opj_image_create0(p_j2k->m_manager); 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); + if (! opj_copy_image_header(p_j2k->m_manager, p_j2k->m_private_image, *p_image)) { + return OPJ_FALSE; + } /*Allocate and initialize some elements of codestrem index*/ if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){ @@ -6801,16 +6524,16 @@ static OPJ_BOOL opj_j2k_setup_header_reading (opj_j2k_t *p_j2k, opj_event_mgr_t return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager) +static OPJ_BOOL opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k) { /* preconditions*/ assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_manager != 00); - if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,(opj_procedure)opj_j2k_build_decoder, p_manager)) { + if (!opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder, &(p_j2k->m_manager->event_mgr))) { return OPJ_FALSE; } - if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,(opj_procedure)opj_j2k_decoding_validation, p_manager)) { + if (!opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation, &(p_j2k->m_manager->event_mgr))) { return OPJ_FALSE; } @@ -6818,9 +6541,7 @@ static OPJ_BOOL opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k, opj_event_m return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_mct_validation ( opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_j2k_mct_validation (opj_j2k_t * p_j2k, opj_stream_private_t *p_stream) { OPJ_BOOL l_is_valid = OPJ_TRUE; OPJ_UINT32 i,j; @@ -6828,7 +6549,6 @@ static OPJ_BOOL opj_j2k_mct_validation ( opj_j2k_t * p_j2k, /* 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; @@ -6851,7 +6571,7 @@ static OPJ_BOOL opj_j2k_mct_validation ( opj_j2k_t * p_j2k, return l_is_valid; } -OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) +static OPJ_BOOL opj_j2k_setup_mct_encoding(opj_manager_t manager, opj_tcp_t * p_tcp, opj_image_t * p_image) { OPJ_UINT32 i; OPJ_UINT32 l_indix = 1; @@ -6873,13 +6593,13 @@ OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) opj_mct_data_t *new_mct_records; p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; - 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)); + new_mct_records = (opj_mct_data_t *) opj_manager_realloc(manager, 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); + opj_manager_free(manager, 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(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Not enough memory to setup mct encoding\n"); return OPJ_FALSE; } p_tcp->m_mct_records = new_mct_records; @@ -6890,7 +6610,7 @@ OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) 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); + opj_manager_free(manager, l_mct_deco_data->m_data); l_mct_deco_data->m_data = 00; } @@ -6899,7 +6619,7 @@ OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) 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 ); + l_mct_deco_data->m_data = (OPJ_BYTE*)opj_manager_malloc(manager, l_mct_size ); if (! l_mct_deco_data->m_data) { return OPJ_FALSE; @@ -6914,13 +6634,13 @@ OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) { opj_mct_data_t *new_mct_records; p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; - 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)); + new_mct_records = (opj_mct_data_t *) opj_manager_realloc(manager, 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); + opj_manager_free(manager, 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(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Not enough memory to setup mct encoding\n"); return OPJ_FALSE; } p_tcp->m_mct_records = new_mct_records; @@ -6936,7 +6656,7 @@ OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; if (l_mct_offset_data->m_data) { - opj_free(l_mct_offset_data->m_data); + opj_manager_free(manager, l_mct_offset_data->m_data); l_mct_offset_data->m_data = 00; } @@ -6945,15 +6665,15 @@ OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) 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_mct_offset_data->m_data = (OPJ_BYTE*)opj_manager_malloc(manager, l_mct_size ); if (! l_mct_offset_data->m_data) { return OPJ_FALSE; } - l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32)); + l_data = (OPJ_FLOAT32*)opj_manager_malloc(manager, l_nb_elem * sizeof(OPJ_FLOAT32)); if (! l_data) { - opj_free(l_mct_offset_data->m_data); + opj_manager_free(manager, l_mct_offset_data->m_data); l_mct_offset_data->m_data = 00; return OPJ_FALSE; } @@ -6968,7 +6688,7 @@ OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem); - opj_free(l_data); + opj_manager_free(manager, l_data); l_mct_offset_data->m_data_size = l_mct_size; @@ -6977,20 +6697,19 @@ OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) { opj_simple_mcc_decorrelation_data_t *new_mcc_records; p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS; - new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc( + new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_manager_realloc(manager, 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); + opj_manager_free(manager, 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(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */ + opj_event_msg(&(manager->event_mgr), 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; @@ -7004,40 +6723,32 @@ OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image) return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_build_decoder (opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_j2k_build_decoder (opj_j2k_t * p_j2k, opj_stream_private_t *p_stream) { - /* add here initialization of cp - copy paste of setup_decoder */ - (void)p_j2k; - (void)p_stream; - (void)p_manager; - return OPJ_TRUE; + /* add here initialization of cp + copy paste of setup_decoder */ + (void)p_j2k; + (void)p_stream; + return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_build_encoder (opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_j2k_build_encoder (opj_j2k_t * p_j2k, opj_stream_private_t *p_stream) { - /* add here initialization of cp - copy paste of setup_encoder */ - (void)p_j2k; - (void)p_stream; - (void)p_manager; - return OPJ_TRUE; + /* add here initialization of cp + copy paste of setup_encoder */ + (void)p_j2k; + (void)p_stream; + return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_encoding_validation ( opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_j2k_encoding_validation (opj_j2k_t * p_j2k, opj_stream_private_t *p_stream) { OPJ_BOOL l_is_valid = OPJ_TRUE; /* preconditions */ assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); assert(p_stream != 00); - assert(p_manager != 00); /* STATE checking */ /* make sure the state is at 0 */ @@ -7053,17 +6764,17 @@ static OPJ_BOOL opj_j2k_encoding_validation ( opj_j2k_t * p_j2k, /* 33 (32) would always fail the check below (if a cast to 64bits was done) */ /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */ if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) || (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) { - opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); return OPJ_FALSE; } if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) { - opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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 - 1U))) { - opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); return OPJ_FALSE; } @@ -7071,17 +6782,13 @@ static OPJ_BOOL opj_j2k_encoding_validation ( opj_j2k_t * p_j2k, return l_is_valid; } -static OPJ_BOOL opj_j2k_decoding_validation ( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager - ) +static OPJ_BOOL opj_j2k_decoding_validation (opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { 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 */ @@ -7101,9 +6808,7 @@ static OPJ_BOOL opj_j2k_decoding_validation ( opj_j2k_t *p_j2k, return l_is_valid; } -static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager) +static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { OPJ_UINT32 l_current_marker; OPJ_UINT32 l_marker_size; @@ -7115,20 +6820,20 @@ static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, /* preconditions */ assert(p_stream != 00); assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_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(p_manager, EVT_ERROR, "Expected a SOC marker \n"); + if (! opj_j2k_read_soc(p_j2k,p_stream)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,&(p_j2k->m_manager->event_mgr)) != 2) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short\n"); return OPJ_FALSE; } @@ -7140,7 +6845,7 @@ static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, /* Check if the current marker ID is valid */ if (l_current_marker < 0xff00) { - opj_event_msg(p_manager, EVT_ERROR, "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker); return OPJ_FALSE; } @@ -7149,8 +6854,8 @@ static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, /* 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(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n"); + if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker)){ + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Unknow marker have been detected and generated error.\n"); return OPJ_FALSE; } @@ -7175,13 +6880,13 @@ static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, /* 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(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,&(p_j2k->m_manager->event_mgr)) != 2) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short\n"); return OPJ_FALSE; } @@ -7191,12 +6896,12 @@ static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, /* 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); + OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_manager_realloc(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to read header\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -7204,30 +6909,29 @@ static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, } /* 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,&(p_j2k->m_manager->event_mgr)) != l_marker_size) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n"); + if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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, + if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,&(p_j2k->m_manager->event_mgr)) != 2) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short\n"); return OPJ_FALSE; } @@ -7236,24 +6940,24 @@ static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, } if (l_has_siz == 0) { - opj_event_msg(p_manager, EVT_ERROR, "required SIZ marker not found in main header\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "required SIZ marker not found in main header\n"); return OPJ_FALSE; } if (l_has_cod == 0) { - opj_event_msg(p_manager, EVT_ERROR, "required COD marker not found in main header\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "required COD marker not found in main header\n"); return OPJ_FALSE; } if (l_has_qcd == 0) { - opj_event_msg(p_manager, EVT_ERROR, "required QCD marker not found in main header\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "required QCD marker not found in main header\n"); return OPJ_FALSE; } - if (! opj_j2k_merge_ppm(&(p_j2k->m_cp), p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPM data\n"); + if (! opj_j2k_merge_ppm(p_j2k, &(p_j2k->m_cp))) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Failed to merge PPM data\n"); return OPJ_FALSE; } - opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -7266,10 +6970,9 @@ static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k, static OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k, opj_procedure_list_t * p_procedure_list, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *p_stream ) { - OPJ_BOOL (** l_procedure) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00; + OPJ_BOOL (** l_procedure) (opj_j2k_t * ,opj_stream_private_t *) = 00; OPJ_BOOL l_result = OPJ_TRUE; OPJ_UINT32 l_nb_proc, i; @@ -7277,13 +6980,12 @@ static OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k, 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_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); + l_procedure = (OPJ_BOOL (**) (opj_j2k_t * ,opj_stream_private_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; @@ -7344,7 +7044,7 @@ static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2 /* 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); + l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_manager_malloc(p_j2k->m_manager, l_mct_size); if (! l_tcp->m_mct_decoding_matrix ) { return OPJ_FALSE; } @@ -7353,7 +7053,7 @@ static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2 /* 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 * (OPJ_UINT32)sizeof(opj_mct_data_t); - l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size); + l_tcp->m_mct_records = (opj_mct_data_t*)opj_manager_malloc(p_j2k->m_manager, l_mct_records_size); if (! l_tcp->m_mct_records) { return OPJ_FALSE; } @@ -7367,7 +7067,7 @@ static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2 if (l_src_mct_rec->m_data) { - l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size); + l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_manager_malloc(p_j2k->m_manager, l_src_mct_rec->m_data_size); if(! l_dest_mct_rec->m_data) { return OPJ_FALSE; } @@ -7382,7 +7082,7 @@ static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2 /* 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 * (OPJ_UINT32)sizeof(opj_simple_mcc_decorrelation_data_t); - l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size); + l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_manager_malloc(p_j2k->m_manager, l_mcc_records_size); if (! l_tcp->m_mcc_records) { return OPJ_FALSE; } @@ -7417,15 +7117,15 @@ static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2 } /* Create the current tile decoder*/ - p_j2k->m_tcd = (opj_tcd_t*)opj_tcd_create(OPJ_TRUE); /* FIXME why a cast ? */ + p_j2k->m_tcd = (opj_tcd_t*)opj_tcd_create(p_j2k->m_manager, 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); + opj_tcd_destroy(p_j2k->m_manager, p_j2k->m_tcd); p_j2k->m_tcd = 00; - opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Cannot decode tile, memory error\n"); return OPJ_FALSE; } @@ -7445,20 +7145,22 @@ static const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_U void opj_j2k_destroy (opj_j2k_t *p_j2k) { + opj_manager_t manager = NULL; if (p_j2k == 00) { return; } + manager = p_j2k->m_manager; 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); + opj_j2k_tcp_destroy(manager, p_j2k->m_specific_param.m_decoder.m_default_tcp); + opj_manager_free(manager, 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); + opj_manager_free(manager, 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; } @@ -7466,26 +7168,26 @@ void opj_j2k_destroy (opj_j2k_t *p_j2k) 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); + opj_manager_free(manager, 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); + opj_manager_free(manager, 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); + opj_manager_free(manager, 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(manager, p_j2k->m_tcd); - opj_j2k_cp_destroy(&(p_j2k->m_cp)); + opj_j2k_cp_destroy(manager, &(p_j2k->m_cp)); memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t)); opj_procedure_list_destroy(p_j2k->m_procedure_list); @@ -7494,24 +7196,24 @@ void opj_j2k_destroy (opj_j2k_t *p_j2k) opj_procedure_list_destroy(p_j2k->m_validation_list); p_j2k->m_procedure_list = 00; - j2k_destroy_cstr_index(p_j2k->cstr_index); + j2k_destroy_cstr_index(manager, p_j2k->cstr_index); p_j2k->cstr_index = NULL; - opj_image_destroy(p_j2k->m_private_image); + opj_manager_image_destroy(manager, p_j2k->m_private_image); p_j2k->m_private_image = NULL; - opj_image_destroy(p_j2k->m_output_image); + opj_manager_image_destroy(manager, p_j2k->m_output_image); p_j2k->m_output_image = NULL; - opj_free(p_j2k); + opj_manager_free(manager, p_j2k); } -void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind) +void j2k_destroy_cstr_index (opj_manager_t manager, opj_codestream_index_t *p_cstr_ind) { if (p_cstr_ind) { if (p_cstr_ind->marker) { - opj_free(p_cstr_ind->marker); + opj_manager_free(manager, p_cstr_ind->marker); p_cstr_ind->marker = NULL; } @@ -7521,31 +7223,31 @@ void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind) 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); + opj_manager_free(manager, 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); + opj_manager_free(manager, 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); + opj_manager_free(manager, p_cstr_ind->tile_index[it_tile].marker); p_cstr_ind->tile_index[it_tile].marker = NULL; } } - opj_free( p_cstr_ind->tile_index); + opj_manager_free(manager, p_cstr_ind->tile_index); p_cstr_ind->tile_index = NULL; } - opj_free(p_cstr_ind); + opj_manager_free(manager, p_cstr_ind); } } -static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp) +static void opj_j2k_tcp_destroy (opj_manager_t manager, opj_tcp_t *p_tcp) { if (p_tcp == 00) { return; @@ -7555,36 +7257,36 @@ static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp) OPJ_UINT32 i; for (i = 0U; i < p_tcp->ppt_markers_count; ++i) { if (p_tcp->ppt_markers[i].m_data != NULL) { - opj_free(p_tcp->ppt_markers[i].m_data); + opj_manager_free(manager, p_tcp->ppt_markers[i].m_data); } } p_tcp->ppt_markers_count = 0U; - opj_free(p_tcp->ppt_markers); + opj_manager_free(manager, p_tcp->ppt_markers); p_tcp->ppt_markers = NULL; } if (p_tcp->ppt_buffer != 00) { - opj_free(p_tcp->ppt_buffer); + opj_manager_free(manager, p_tcp->ppt_buffer); p_tcp->ppt_buffer = 00; } if (p_tcp->tccps != 00) { - opj_free(p_tcp->tccps); + opj_manager_free(manager, p_tcp->tccps); p_tcp->tccps = 00; } if (p_tcp->m_mct_coding_matrix != 00) { - opj_free(p_tcp->m_mct_coding_matrix); + opj_manager_free(manager, 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); + opj_manager_free(manager, 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); + opj_manager_free(manager, 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; @@ -7596,36 +7298,36 @@ static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp) for (i=0;im_nb_mct_records;++i) { if (l_mct_data->m_data) { - opj_free(l_mct_data->m_data); + opj_manager_free(manager, l_mct_data->m_data); l_mct_data->m_data = 00; } ++l_mct_data; } - opj_free(p_tcp->m_mct_records); + opj_manager_free(manager, p_tcp->m_mct_records); p_tcp->m_mct_records = 00; } if (p_tcp->mct_norms != 00) { - opj_free(p_tcp->mct_norms); + opj_manager_free(manager, p_tcp->mct_norms); p_tcp->mct_norms = 00; } - opj_j2k_tcp_data_destroy(p_tcp); + opj_j2k_tcp_data_destroy(manager, p_tcp); } -static void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp) +static void opj_j2k_tcp_data_destroy (opj_manager_t manager, opj_tcp_t *p_tcp) { if (p_tcp->m_data) { - opj_free(p_tcp->m_data); + opj_manager_free(manager, p_tcp->m_data); p_tcp->m_data = NULL; p_tcp->m_data_size = 0; } } -static void opj_j2k_cp_destroy (opj_cp_t *p_cp) +static void opj_j2k_cp_destroy (opj_manager_t manager, opj_cp_t *p_cp) { OPJ_UINT32 l_nb_tiles; opj_tcp_t * l_current_tile = 00; @@ -7642,31 +7344,31 @@ static void opj_j2k_cp_destroy (opj_cp_t *p_cp) for (i = 0U; i < l_nb_tiles; ++i) { - opj_j2k_tcp_destroy(l_current_tile); + opj_j2k_tcp_destroy(manager, l_current_tile); ++l_current_tile; } - opj_free(p_cp->tcps); + opj_manager_free(manager, p_cp->tcps); p_cp->tcps = 00; } if (p_cp->ppm_markers != 00) { OPJ_UINT32 i; for (i = 0U; i < p_cp->ppm_markers_count; ++i) { if (p_cp->ppm_markers[i].m_data != NULL) { - opj_free(p_cp->ppm_markers[i].m_data); + opj_manager_free(manager, p_cp->ppm_markers[i].m_data); } } p_cp->ppm_markers_count = 0U; - opj_free(p_cp->ppm_markers); + opj_manager_free(manager, p_cp->ppm_markers); p_cp->ppm_markers = NULL; } - opj_free(p_cp->ppm_buffer); + opj_manager_free(manager, p_cp->ppm_buffer); p_cp->ppm_buffer = 00; p_cp->ppm_data = NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */ - opj_free(p_cp->comment); + opj_manager_free(manager, p_cp->comment); p_cp->comment = 00; if (! p_cp->m_is_decoder) { - opj_free(p_cp->m_specific_param.m_enc.m_matrice); + opj_manager_free(manager, p_cp->m_specific_param.m_enc.m_matrice); p_cp->m_specific_param.m_enc.m_matrice = 00; } } @@ -7730,7 +7432,7 @@ static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t *p_st return OPJ_FALSE; } - if (! opj_j2k_get_sot_values(l_header_data, l_marker_size, &l_tile_no, &l_tot_len, &l_current_part, &l_num_parts, p_manager)) { + if (!opj_j2k_get_sot_values(p_manager, l_header_data, l_marker_size, &l_tile_no, &l_tot_len, &l_current_part, &l_num_parts)) { return OPJ_FALSE; } @@ -7776,8 +7478,7 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, 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_stream_private_t *p_stream ) { OPJ_UINT32 l_current_marker = J2K_MS_SOT; OPJ_UINT32 l_marker_size; @@ -7787,7 +7488,6 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, /* 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){ @@ -7811,8 +7511,8 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, } /* 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,&(p_j2k->m_manager->event_mgr)) != 2) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short\n"); return OPJ_FALSE; } @@ -7821,7 +7521,7 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, /* Check marker size (does not include marker ID but includes marker size) */ if (l_marker_size < 2) { - opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Inconsistent marker size\n"); return OPJ_FALSE; } @@ -7842,7 +7542,7 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, /* 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(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Marker is not compliant with its position\n"); return OPJ_FALSE; } /* FIXME manage case of unknown marker as in the main header ? */ @@ -7853,15 +7553,15 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, /* If we are here, this means we consider this marker as known & we will read it */ /* Check enough bytes left in stream before allocation */ if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) { - opj_event_msg(p_manager, EVT_ERROR, "Marker size inconsistent with stream length\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Marker size inconsistent with stream length\n"); return OPJ_FALSE; } - new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size); + new_header_data = (OPJ_BYTE *) opj_manager_realloc(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to read header\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -7869,29 +7569,29 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, } /* 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,&(p_j2k->m_manager->event_mgr)) != l_marker_size) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short\n"); return OPJ_FALSE; } if (!l_marker_handler->handler) { /* See issue #175 */ - opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not sure how that happened.\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(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker); + if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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, + if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_manager, 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(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to add tl marker\n"); return OPJ_FALSE; } @@ -7906,16 +7606,16 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,&(p_j2k->m_manager->event_mgr)) != p_j2k->m_specific_param.m_decoder.m_sot_length) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,&(p_j2k->m_manager->event_mgr)) != 2) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short\n"); return OPJ_FALSE; } /* Read 2 bytes from the buffer as the new marker ID */ @@ -7929,7 +7629,7 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, /* 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)) { + if (! opj_j2k_read_sod(p_j2k, p_stream)) { return OPJ_FALSE; } if (p_j2k->m_specific_param.m_decoder.m_can_decode && !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) { @@ -7937,8 +7637,8 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, OPJ_BOOL l_correction_needed; p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; - if(!opj_j2k_need_nb_tile_parts_correction(p_stream, p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "opj_j2k_apply_nb_tile_parts_correction error\n"); + if (!opj_j2k_need_nb_tile_parts_correction(p_stream, p_j2k->m_current_tile_number, &l_correction_needed, &(p_j2k->m_manager->event_mgr))) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "opj_j2k_apply_nb_tile_parts_correction error\n"); return OPJ_FALSE; } if (l_correction_needed) { @@ -7953,13 +7653,13 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts+=1; } } - opj_event_msg(p_manager, EVT_WARNING, "Non conformant codestream TPsot==TNsot.\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Non conformant codestream TPsot==TNsot.\n"); } } 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,&(p_j2k->m_manager->event_mgr)) != 2) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short\n"); return OPJ_FALSE; } @@ -7974,8 +7674,8 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,&(p_j2k->m_manager->event_mgr)) != 2) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short\n"); return OPJ_FALSE; } @@ -8008,17 +7708,17 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, } } - if (! opj_j2k_merge_ppt(p_j2k->m_cp.tcps + p_j2k->m_current_tile_number, p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPT data\n"); + if (! opj_j2k_merge_ppt(p_j2k, p_j2k->m_cp.tcps + p_j2k->m_current_tile_number)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Failed to merge PPT data\n"); return OPJ_FALSE; } /*FIXME ???*/ - if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + if (!opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, &(p_j2k->m_manager->event_mgr))) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Cannot decode tile, memory error\n"); return OPJ_FALSE; } - opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n", + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_INFO, "Header of tile %d / %d has been read.\n", p_j2k->m_current_tile_number+1, (p_j2k->m_cp.th * p_j2k->m_cp.tw)); *p_tile_index = p_j2k->m_current_tile_number; @@ -8039,8 +7739,7 @@ OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_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_stream_private_t *p_stream ) { OPJ_UINT32 l_current_marker; OPJ_BYTE l_data [2]; @@ -8049,7 +7748,7 @@ OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k, /* preconditions */ assert(p_stream != 00); assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_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) ) { @@ -8058,7 +7757,7 @@ OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k, l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]); if (! l_tcp->m_data) { - opj_j2k_tcp_destroy(l_tcp); + opj_j2k_tcp_destroy(p_j2k->m_manager, l_tcp); return OPJ_FALSE; } @@ -8066,10 +7765,10 @@ OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k, l_tcp->m_data, l_tcp->m_data_size, p_tile_index, - p_j2k->cstr_index, p_manager) ) { - opj_j2k_tcp_destroy(l_tcp); + p_j2k->cstr_index, &(p_j2k->m_manager->event_mgr))) { + opj_j2k_tcp_destroy(p_j2k->m_manager, l_tcp); p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/ - opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Failed to decode.\n"); return OPJ_FALSE; } @@ -8081,7 +7780,7 @@ OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k, * 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); + opj_j2k_tcp_data_destroy(p_j2k->m_manager, l_tcp); p_j2k->m_specific_param.m_decoder.m_can_decode = 0; p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080u));/* FIXME J2K_DEC_STATE_DATA);*/ @@ -8092,8 +7791,8 @@ OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k, } 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(p_manager, EVT_ERROR, "Stream too short\n"); + if (opj_stream_read_data(p_stream,l_data,2,&(p_j2k->m_manager->event_mgr)) != 2) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short\n"); return OPJ_FALSE; } @@ -8107,10 +7806,10 @@ OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k, { if(opj_stream_get_number_byte_left(p_stream) == 0) { p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; - opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "Stream does not end with EOC\n"); return OPJ_TRUE; } - opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Stream too short, expected SOT\n"); return OPJ_FALSE; } } @@ -8118,7 +7817,7 @@ OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k, return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image) +static OPJ_BOOL opj_j2k_update_image_data(opj_manager_t manager, opj_tcd_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; @@ -8149,7 +7848,7 @@ static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, /* Allocate output component buffer if necessary */ if (!l_img_comp_dest->data) { - l_img_comp_dest->data = (OPJ_INT32*) opj_calloc((OPJ_SIZE_T)l_img_comp_dest->w * (OPJ_SIZE_T)l_img_comp_dest->h, sizeof(OPJ_INT32)); + l_img_comp_dest->data = (OPJ_INT32*) opj_manager_calloc(manager, (OPJ_SIZE_T)l_img_comp_dest->w * (OPJ_SIZE_T)l_img_comp_dest->h, sizeof(OPJ_INT32)); if (! l_img_comp_dest->data) { return OPJ_FALSE; } @@ -8383,8 +8082,7 @@ static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_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_INT32 p_end_x, OPJ_INT32 p_end_y ) { opj_cp_t * l_cp = &(p_j2k->m_cp); opj_image_t * l_image = p_j2k->m_private_image; @@ -8395,12 +8093,12 @@ OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k, /* 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(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -8418,13 +8116,13 @@ OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k, assert(p_start_y >= 0 ); if ((OPJ_UINT32)p_start_x > l_image->x1 ) { - opj_event_msg(p_manager, EVT_ERROR, + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -8437,13 +8135,13 @@ OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k, /* Up */ if ((OPJ_UINT32)p_start_y > l_image->y1){ - opj_event_msg(p_manager, EVT_ERROR, + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -8458,13 +8156,13 @@ OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k, 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(p_manager, EVT_ERROR, + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -8477,13 +8175,13 @@ OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k, /* Bottom */ if ((OPJ_UINT32)p_end_y < l_image->y0) { - opj_event_msg(p_manager, EVT_ERROR, + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_WARNING, + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -8510,7 +8208,7 @@ OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k, l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor); if (l_w < 0){ - opj_event_msg(p_manager, EVT_ERROR, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n", it_comp, l_w); return OPJ_FALSE; @@ -8520,7 +8218,7 @@ OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k, l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor); if (l_h < 0){ - opj_event_msg(p_manager, EVT_ERROR, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n", it_comp, l_h); return OPJ_FALSE; @@ -8530,15 +8228,15 @@ OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k, l_img_comp++; } - opj_event_msg( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n", + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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_j2k_t* opj_j2k_create_decompress(void) +opj_j2k_t* opj_j2k_create_decompress(opj_manager_t manager) { - opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1,sizeof(opj_j2k_t)); + opj_j2k_t *l_j2k = (opj_j2k_t*) opj_manager_calloc(manager,1,sizeof(opj_j2k_t)); if (!l_j2k) { return 00; } @@ -8550,13 +8248,13 @@ opj_j2k_t* opj_j2k_create_decompress(void) l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1; #endif - l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1,sizeof(opj_tcp_t)); + l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_manager_calloc(manager,1,sizeof(opj_tcp_t)); if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) { opj_j2k_destroy(l_j2k); return 00; } - l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1,OPJ_J2K_DEFAULT_HEADER_SIZE); + l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_manager_calloc(manager,1,OPJ_J2K_DEFAULT_HEADER_SIZE); if (! l_j2k->m_specific_param.m_decoder.m_header_data) { opj_j2k_destroy(l_j2k); return 00; @@ -8569,21 +8267,21 @@ opj_j2k_t* opj_j2k_create_decompress(void) 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(); + l_j2k->cstr_index = opj_j2k_create_cstr_index(manager); if (!l_j2k->cstr_index){ opj_j2k_destroy(l_j2k); return 00; } /* validation list creation */ - l_j2k->m_validation_list = opj_procedure_list_create(); + l_j2k->m_validation_list = opj_procedure_list_create(manager); 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(); + l_j2k->m_procedure_list = opj_procedure_list_create(manager); if (! l_j2k->m_procedure_list) { opj_j2k_destroy(l_j2k); return 00; @@ -8592,17 +8290,17 @@ opj_j2k_t* opj_j2k_create_decompress(void) return l_j2k; } -static opj_codestream_index_t* opj_j2k_create_cstr_index(void) +static opj_codestream_index_t* opj_j2k_create_cstr_index(opj_manager_t manager) { opj_codestream_index_t* cstr_index = (opj_codestream_index_t*) - opj_calloc(1,sizeof(opj_codestream_index_t)); + opj_manager_calloc(manager,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)); + opj_manager_calloc(manager,cstr_index->maxmarknum, sizeof(opj_marker_info_t)); if (!cstr_index-> marker) return NULL; @@ -8642,8 +8340,7 @@ static OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_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 * p_header_size ) { OPJ_UINT32 i; opj_cp_t *l_cp = 00; @@ -8653,7 +8350,6 @@ static OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_t *p_j2k, /* preconditions */ assert(p_j2k != 00); assert(p_header_size != 00); - assert(p_manager != 00); assert(p_data != 00); l_cp = &(p_j2k->m_cp); @@ -8665,7 +8361,7 @@ static OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_t *p_j2k, assert(p_comp_no <(p_j2k->m_private_image->numcomps)); if (*p_header_size < 5) { - opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error writing SPCod SPCoc element\n"); return OPJ_FALSE; } @@ -8689,7 +8385,7 @@ static OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_t *p_j2k, if (l_tccp->csty & J2K_CCP_CSTY_PRT) { if (*p_header_size < l_tccp->numresolutions) { - opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error writing SPCod SPCoc element\n"); return OPJ_FALSE; } @@ -8707,8 +8403,7 @@ static OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_t *p_j2k, static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k, OPJ_UINT32 compno, OPJ_BYTE * p_header_data, - OPJ_UINT32 * p_header_size, - opj_event_mgr_t * p_manager) + OPJ_UINT32 * p_header_size) { OPJ_UINT32 i, l_tmp; opj_cp_t *l_cp = NULL; @@ -8718,7 +8413,6 @@ static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k, /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); assert(p_header_data != 00); l_cp = &(p_j2k->m_cp); @@ -8734,14 +8428,14 @@ static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k, /* make sure room is sufficient */ if (*p_header_size < 5) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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 */ if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) { - opj_event_msg(p_manager, EVT_ERROR, + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n", l_tccp->numresolutions, OPJ_J2K_MAXRLVLS); return OPJ_FALSE; @@ -8750,7 +8444,7 @@ static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k, /* 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(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number " + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -8765,7 +8459,7 @@ static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k, l_tccp->cblkh += 2; if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) || ((l_tccp->cblkw + l_tccp->cblkh) > 12)) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n"); return OPJ_FALSE; } @@ -8781,7 +8475,7 @@ static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k, /* use custom precinct size ? */ if (l_tccp->csty & J2K_CCP_CSTY_PRT) { if (*p_header_size < l_tccp->numresolutions) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading SPCod SPCoc element\n"); return OPJ_FALSE; } @@ -8790,7 +8484,7 @@ static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k, ++l_current_ptr; /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */ if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Invalid precinct size\n"); return OPJ_FALSE; } l_tccp->prcw[i] = l_tmp & 0xf; @@ -8895,8 +8589,7 @@ static OPJ_BOOL opj_j2k_write_SQcd_SQcc( opj_j2k_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 * p_header_size ) { OPJ_UINT32 l_header_size; OPJ_UINT32 l_band_no, l_num_bands; @@ -8909,7 +8602,6 @@ static OPJ_BOOL opj_j2k_write_SQcd_SQcc( opj_j2k_t *p_j2k, /* preconditions */ assert(p_j2k != 00); assert(p_header_size != 00); - assert(p_manager != 00); assert(p_data != 00); l_cp = &(p_j2k->m_cp); @@ -8926,7 +8618,7 @@ static OPJ_BOOL opj_j2k_write_SQcd_SQcc( opj_j2k_t *p_j2k, l_header_size = 1 + l_num_bands; if (*p_header_size < l_header_size) { - opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error writing SQcd SQcc element\n"); return OPJ_FALSE; } @@ -8943,7 +8635,7 @@ static OPJ_BOOL opj_j2k_write_SQcd_SQcc( opj_j2k_t *p_j2k, l_header_size = 1 + 2*l_num_bands; if (*p_header_size < l_header_size) { - opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error writing SQcd SQcc element\n"); return OPJ_FALSE; } @@ -8967,8 +8659,7 @@ static OPJ_BOOL opj_j2k_write_SQcd_SQcc( opj_j2k_t *p_j2k, static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, OPJ_UINT32 p_comp_no, OPJ_BYTE* p_header_data, - OPJ_UINT32 * p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 * p_header_size ) { /* loop*/ @@ -8981,7 +8672,6 @@ static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, /* preconditions*/ assert(p_j2k != 00); - assert(p_manager != 00); assert(p_header_data != 00); l_cp = &(p_j2k->m_cp); @@ -8997,7 +8687,7 @@ static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, l_current_ptr = p_header_data; if (*p_header_size < 1) { - opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error reading SQcd or SQcc element\n"); return OPJ_FALSE; } *p_header_size -= 1; @@ -9016,7 +8706,7 @@ static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, (*p_header_size) / 2; if( l_num_band > OPJ_J2K_MAXBANDS ) { - opj_event_msg(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, " + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, " "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to " "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS, OPJ_J2K_MAXBANDS); /*return OPJ_FALSE;*/ @@ -9028,16 +8718,16 @@ static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k, /* if JPWL is on, we check whether there are too many subbands */ if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) { - opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + opj_event_msg(&(p_j2k->m_manager->event_mgr), JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, "JPWL: bad number of subbands in Sqcx (%d)\n", l_num_band); if (!JPWL_ASSUME) { - opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "JPWL: giving up\n"); return OPJ_FALSE; } /* we try to correct */ l_num_band = 1; - opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n" + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_WARNING, "- trying to adjust them\n" "- setting number of bands to %d => HYPOTHESIS!!!\n", l_num_band); }; @@ -9342,7 +9032,7 @@ opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k) OPJ_UINT32 compno; OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps; opj_tcp_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)); + opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_manager_calloc(p_j2k->m_manager, 1,sizeof(opj_codestream_info_v2_t)); if (!cstr_info) return NULL; @@ -9364,10 +9054,10 @@ opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k) 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)); + cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_manager_calloc(p_j2k->m_manager, cstr_info->nbcomps, sizeof(opj_tccp_info_t)); if (!cstr_info->m_default_tile_info.tccp_info) { - opj_destroy_cstr_info(&cstr_info); + opj_manager_destroy_cstr_info(p_j2k->m_manager, &cstr_info); return NULL; } @@ -9411,7 +9101,7 @@ opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k) opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) { opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*) - opj_calloc(1,sizeof(opj_codestream_index_t)); + opj_manager_calloc(p_j2k->m_manager, 1,sizeof(opj_codestream_index_t)); if (!l_cstr_index) return NULL; @@ -9420,29 +9110,29 @@ opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) 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)); + l_cstr_index->marker = (opj_marker_info_t*)opj_manager_malloc(p_j2k->m_manager, l_cstr_index->marknum*sizeof(opj_marker_info_t)); if (!l_cstr_index->marker){ - opj_free( l_cstr_index); + opj_manager_free(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, 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) ); + l_cstr_index->tile_index = (opj_tile_index_t*)opj_manager_calloc(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, l_cstr_index->marker); + opj_manager_free(p_j2k->m_manager, l_cstr_index); return NULL; } if (!p_j2k->cstr_index->tile_index){ - opj_free(l_cstr_index->tile_index); + opj_manager_free(p_j2k->m_manager, l_cstr_index->tile_index); l_cstr_index->tile_index = NULL; } else { @@ -9453,18 +9143,18 @@ opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) 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)); + (opj_marker_info_t*)opj_manager_malloc(p_j2k->m_manager, 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_manager_free(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, l_cstr_index->tile_index); + opj_manager_free(p_j2k->m_manager, l_cstr_index->marker); + opj_manager_free(p_j2k->m_manager, l_cstr_index); return NULL; } @@ -9473,7 +9163,7 @@ opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) 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); + opj_manager_free(p_j2k->m_manager, l_cstr_index->tile_index[it_tile].marker); l_cstr_index->tile_index[it_tile].marker = NULL; } @@ -9481,19 +9171,19 @@ opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) 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)); + (opj_tp_index_t*)opj_manager_malloc(p_j2k->m_manager, 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_manager_free(p_j2k->m_manager, l_cstr_index->tile_index[it_tile_free].marker); + opj_manager_free(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, l_cstr_index->tile_index); + opj_manager_free(p_j2k->m_manager, l_cstr_index->marker); + opj_manager_free(p_j2k->m_manager, l_cstr_index); return NULL; } @@ -9503,7 +9193,7 @@ opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k) 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); + opj_manager_free(p_j2k->m_manager, l_cstr_index->tile_index[it_tile].tp_index); l_cstr_index->tile_index[it_tile].tp_index = NULL; } @@ -9522,7 +9212,7 @@ static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k) 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)); + p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_manager_calloc(p_j2k->m_manager, p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t)); if (!p_j2k->cstr_index->tile_index) return OPJ_FALSE; @@ -9530,7 +9220,7 @@ static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k) 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)); + opj_manager_calloc(p_j2k->m_manager, 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; } @@ -9538,9 +9228,7 @@ static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k) return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager) +static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { OPJ_BOOL l_go_on = OPJ_TRUE; OPJ_UINT32 l_current_tile_no; @@ -9550,9 +9238,9 @@ static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, OPJ_BYTE * l_current_data; OPJ_UINT32 nr_tiles = 0; - l_current_data = (OPJ_BYTE*)opj_malloc(1000); + l_current_data = (OPJ_BYTE*)opj_manager_malloc(p_j2k->m_manager, 1000); if (! l_current_data) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tiles\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to decode tiles\n"); return OPJ_FALSE; } l_max_data_size = 1000; @@ -9565,9 +9253,8 @@ static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, &l_tile_x1, &l_tile_y1, &l_nb_comps, &l_go_on, - p_stream, - p_manager)) { - opj_free(l_current_data); + p_stream)) { + opj_manager_free(p_j2k->m_manager, l_current_data); return OPJ_FALSE; } @@ -9576,28 +9263,28 @@ static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, } if (l_data_size > l_max_data_size) { - OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size); + OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_manager_realloc(p_j2k->m_manager, l_current_data, l_data_size); if (! l_new_current_data) { - opj_free(l_current_data); - opj_event_msg(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); + opj_manager_free(p_j2k->m_manager, l_current_data); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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); - opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw); + if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream)) { + opj_manager_free(p_j2k->m_manager, l_current_data); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Failed to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw); return OPJ_FALSE; } - opj_event_msg(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); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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); + if (!opj_j2k_update_image_data(p_j2k->m_manager, p_j2k->m_tcd, l_current_data, p_j2k->m_output_image)) { + opj_manager_free(p_j2k->m_manager, l_current_data); return OPJ_FALSE; } - opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); if(opj_stream_get_number_byte_left(p_stream) == 0 && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) @@ -9606,7 +9293,7 @@ static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, break; } - opj_free(l_current_data); + opj_manager_free(p_j2k->m_manager, l_current_data); return OPJ_TRUE; } @@ -9614,13 +9301,13 @@ static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, /** * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures. */ -static OPJ_BOOL opj_j2k_setup_decoding (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager) +static OPJ_BOOL opj_j2k_setup_decoding (opj_j2k_t *p_j2k) { /* preconditions*/ assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_manager != 00); - if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles, p_manager)) { + if (!opj_procedure_list_add_procedure(p_j2k->m_procedure_list, (opj_procedure)opj_j2k_decode_tiles, &(p_j2k->m_manager->event_mgr))) { return OPJ_FALSE; } /* DEVELOPER CORNER, add your custom procedures */ @@ -9632,8 +9319,7 @@ static OPJ_BOOL opj_j2k_setup_decoding (opj_j2k_t *p_j2k, opj_event_mgr_t * p_ma * Read and decode one tile. */ static OPJ_BOOL opj_j2k_decode_one_tile ( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager) + opj_stream_private_t *p_stream) { OPJ_BOOL l_go_on = OPJ_TRUE; OPJ_UINT32 l_current_tile_no; @@ -9643,9 +9329,9 @@ static OPJ_BOOL opj_j2k_decode_one_tile ( opj_j2k_t *p_j2k, OPJ_UINT32 l_nb_comps; OPJ_BYTE * l_current_data; - l_current_data = (OPJ_BYTE*)opj_malloc(1000); + l_current_data = (OPJ_BYTE*)opj_manager_malloc(p_j2k->m_manager, 1000); if (! l_current_data) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode one tile\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to decode one tile\n"); return OPJ_FALSE; } l_max_data_size = 1000; @@ -9654,7 +9340,7 @@ static OPJ_BOOL opj_j2k_decode_one_tile ( opj_j2k_t *p_j2k, if( !p_j2k->cstr_index->tile_index) { if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){ - opj_free(l_current_data); + opj_manager_free(p_j2k->m_manager, l_current_data); return OPJ_FALSE; } } @@ -9666,16 +9352,16 @@ static OPJ_BOOL opj_j2k_decode_one_tile ( opj_j2k_t *p_j2k, 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(p_manager, EVT_ERROR, "Problem with seek function\n"); - opj_free(l_current_data); + if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, &(p_j2k->m_manager->event_mgr))) ){ + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Problem with seek function\n"); + opj_manager_free(p_j2k->m_manager, l_current_data); 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(p_manager, EVT_ERROR, "Problem with seek function\n"); - opj_free(l_current_data); + 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_j2k->m_manager->event_mgr))) ) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Problem with seek function\n"); + opj_manager_free(p_j2k->m_manager, l_current_data); return OPJ_FALSE; } } @@ -9692,9 +9378,8 @@ static OPJ_BOOL opj_j2k_decode_one_tile ( opj_j2k_t *p_j2k, &l_tile_x1, &l_tile_y1, &l_nb_comps, &l_go_on, - p_stream, - p_manager)) { - opj_free(l_current_data); + p_stream)) { + opj_manager_free(p_j2k->m_manager, l_current_data); return OPJ_FALSE; } @@ -9703,47 +9388,47 @@ static OPJ_BOOL opj_j2k_decode_one_tile ( opj_j2k_t *p_j2k, } if (l_data_size > l_max_data_size) { - OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size); + OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_manager_realloc(p_j2k->m_manager, l_current_data, l_data_size); if (! l_new_current_data) { - opj_free(l_current_data); + opj_manager_free(p_j2k->m_manager, 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(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); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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); + if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream)) { + opj_manager_free(p_j2k->m_manager, l_current_data); return OPJ_FALSE; } - opj_event_msg(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); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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); + if (!opj_j2k_update_image_data(p_j2k->m_manager, p_j2k->m_tcd, l_current_data, p_j2k->m_output_image)) { + opj_manager_free(p_j2k->m_manager, l_current_data); return OPJ_FALSE; } - opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(p_manager, EVT_ERROR, "Problem with seek function\n"); + if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, &(p_j2k->m_manager->event_mgr)) ) ) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Problem with seek function\n"); return OPJ_FALSE; } break; } else { - opj_event_msg(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_event_msg(&(p_j2k->m_manager->event_mgr), 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); + opj_manager_free(p_j2k->m_manager, l_current_data); return OPJ_TRUE; } @@ -9751,13 +9436,13 @@ static OPJ_BOOL opj_j2k_decode_one_tile ( opj_j2k_t *p_j2k, /** * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures. */ -static OPJ_BOOL opj_j2k_setup_decoding_tile (opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager) +static OPJ_BOOL opj_j2k_setup_decoding_tile (opj_j2k_t *p_j2k) { /* preconditions*/ assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_manager != 00); - if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile, p_manager)) { + if (!opj_procedure_list_add_procedure(p_j2k->m_procedure_list, (opj_procedure)opj_j2k_decode_one_tile, &(p_j2k->m_manager->event_mgr))) { return OPJ_FALSE; } /* DEVELOPER CORNER, add your custom procedures */ @@ -9767,26 +9452,25 @@ static OPJ_BOOL opj_j2k_setup_decoding_tile (opj_j2k_t *p_j2k, opj_event_mgr_t * OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k, opj_stream_private_t * p_stream, - opj_image_t * p_image, - opj_event_mgr_t * p_manager) + opj_image_t * p_image) { OPJ_UINT32 compno; - if (!p_image) + if (!p_image || !p_j2k || !p_j2k->m_manager) return OPJ_FALSE; - p_j2k->m_output_image = opj_image_create0(); + p_j2k->m_output_image = opj_image_create0(p_j2k->m_manager); if (! (p_j2k->m_output_image)) { return OPJ_FALSE; } - opj_copy_image_header(p_image, p_j2k->m_output_image); + opj_copy_image_header(p_j2k->m_manager, p_image, p_j2k->m_output_image); /* customization of the decoding */ - opj_j2k_setup_decoding(p_j2k, p_manager); + 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); + if (!opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream)) { + opj_manager_image_destroy(p_j2k->m_manager, p_j2k->m_private_image); p_j2k->m_private_image = NULL; return OPJ_FALSE; } @@ -9811,7 +9495,6 @@ OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k, OPJ_BOOL opj_j2k_get_tile( opj_j2k_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; @@ -9819,12 +9502,12 @@ OPJ_BOOL opj_j2k_get_tile( opj_j2k_t *p_j2k, opj_image_comp_t* l_img_comp; if (!p_image) { - opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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(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); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; } @@ -9866,23 +9549,25 @@ OPJ_BOOL opj_j2k_get_tile( opj_j2k_t *p_j2k, /* Destroy the previous output image*/ if (p_j2k->m_output_image) - opj_image_destroy(p_j2k->m_output_image); + opj_manager_image_destroy(p_j2k->m_manager, p_j2k->m_output_image); /* Create the ouput image from the information previously computed*/ - p_j2k->m_output_image = opj_image_create0(); + p_j2k->m_output_image = opj_image_create0(p_j2k->m_manager); if (! (p_j2k->m_output_image)) { return OPJ_FALSE; } - opj_copy_image_header(p_image, p_j2k->m_output_image); + if (! opj_copy_image_header(p_j2k->m_manager, p_image, p_j2k->m_output_image)) { + return OPJ_FALSE; + } p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index; /* customization of the decoding */ - opj_j2k_setup_decoding_tile(p_j2k, p_manager); + 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); + if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream)) { + opj_manager_image_destroy(p_j2k->m_manager, p_j2k->m_private_image); p_j2k->m_private_image = NULL; return OPJ_FALSE; } @@ -9892,7 +9577,7 @@ OPJ_BOOL opj_j2k_get_tile( opj_j2k_t *p_j2k, 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); + opj_manager_free(p_j2k->m_manager, p_image->comps[compno].data); p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; @@ -9903,8 +9588,7 @@ OPJ_BOOL opj_j2k_get_tile( opj_j2k_t *p_j2k, } OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, - OPJ_UINT32 res_factor, - opj_event_mgr_t * p_manager) + OPJ_UINT32 res_factor) { OPJ_UINT32 it_comp; @@ -9917,7 +9601,7 @@ OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, 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(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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; @@ -9931,9 +9615,7 @@ OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, return OPJ_FALSE; } -OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, opj_stream_private_t *p_stream) { OPJ_UINT32 i, j; OPJ_UINT32 l_nb_tiles; @@ -9943,16 +9625,16 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, /* preconditions */ assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); assert(p_stream != 00); - assert(p_manager != 00); p_tcd = p_j2k->m_tcd; l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; for (i=0;im_manager, l_current_data); } return OPJ_FALSE; } @@ -9966,10 +9648,10 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, l_tilec->data = l_img_comp->data; l_tilec->ownsData = OPJ_FALSE; } else { - if(! opj_alloc_tile_component_data(l_tilec)) { - opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data." ); + if(! opj_alloc_tile_component_data(p_j2k->m_manager, l_tilec)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error allocating tile component data." ); if (l_current_data) { - opj_free(l_current_data); + opj_manager_free(p_j2k->m_manager, l_current_data); } return OPJ_FALSE; } @@ -9978,12 +9660,12 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd); if (l_nb_tiles > 1) { 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); + OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_manager_realloc(p_j2k->m_manager, l_current_data, l_current_tile_size); if (! l_new_current_data) { if (l_current_data) { - opj_free(l_current_data); + opj_manager_free(p_j2k->m_manager, l_current_data); } - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Not enough memory to encode all tiles\n"); return OPJ_FALSE; } l_current_data = l_new_current_data; @@ -9997,32 +9679,30 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, /* now copy this data into the tile component */ if (! opj_tcd_copy_tile_data(p_j2k->m_tcd,l_current_data,l_current_tile_size)) { - opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." ); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Size mismatch between tile data and sent data." ); return OPJ_FALSE; } } - if (! opj_j2k_post_write_tile (p_j2k,p_stream,p_manager)) { + if (! opj_j2k_post_write_tile (p_j2k,p_stream)) { return OPJ_FALSE; } } if (l_current_data) { - opj_free(l_current_data); + opj_manager_free(p_j2k->m_manager, l_current_data); } return OPJ_TRUE; } -OPJ_BOOL opj_j2k_end_compress( opj_j2k_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager) +OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream) { /* customization of the encoding */ - if (! opj_j2k_setup_end_compress(p_j2k, p_manager)) { + if (! opj_j2k_setup_end_compress(p_j2k, &(p_j2k->m_manager->event_mgr))) { return OPJ_FALSE; } - if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) + if (!opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream)) { return OPJ_FALSE; } @@ -10032,20 +9712,20 @@ OPJ_BOOL opj_j2k_end_compress( opj_j2k_t *p_j2k, OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream, - opj_image_t * p_image, - opj_event_mgr_t * p_manager) + opj_image_t * p_image) { /* preconditions */ assert(p_j2k != 00); assert(p_stream != 00); - assert(p_manager != 00); - p_j2k->m_private_image = opj_image_create0(); + p_j2k->m_private_image = opj_image_create0(p_j2k->m_manager); if (! p_j2k->m_private_image) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header." ); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Failed to allocate image header." ); return OPJ_FALSE; } - opj_copy_image_header(p_image, p_j2k->m_private_image); + if (! opj_copy_image_header(p_j2k->m_manager, p_image, p_j2k->m_private_image)) { + return OPJ_FALSE; + } /* TODO_MSD: Find a better way */ if (p_image->comps) { @@ -10060,22 +9740,22 @@ OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, } /* customization of the validation */ - if (! opj_j2k_setup_encoding_validation (p_j2k, p_manager)) { + if (! opj_j2k_setup_encoding_validation (p_j2k, &(p_j2k->m_manager->event_mgr))) { return OPJ_FALSE; } /* validation of the parameters codec */ - if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) { + if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream)) { return OPJ_FALSE; } /* customization of the encoding */ - if (! opj_j2k_setup_header_writing(p_j2k, p_manager)) { + if (! opj_j2k_setup_header_writing(p_j2k, &(p_j2k->m_manager->event_mgr))) { return OPJ_FALSE; } /* write header */ - if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) { + if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream)) { return OPJ_FALSE; } @@ -10084,23 +9764,22 @@ OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, static OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_index, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *p_stream ) { (void)p_stream; if (p_tile_index != p_j2k->m_current_tile_number) { - opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match." ); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "The given tile index does not match." ); return OPJ_FALSE; } - opj_event_msg(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(&(p_j2k->m_manager->event_mgr), 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; /* initialisation before tile encoding */ - if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, p_manager)) { + if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, &(p_j2k->m_manager->event_mgr))) { return OPJ_FALSE; } @@ -10231,9 +9910,7 @@ static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data) } } -static OPJ_BOOL opj_j2k_post_write_tile ( opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k, opj_stream_private_t *p_stream) { OPJ_UINT32 l_nb_bytes_written; OPJ_BYTE * l_current_data = 00; @@ -10241,6 +9918,8 @@ static OPJ_BOOL opj_j2k_post_write_tile ( opj_j2k_t * p_j2k, OPJ_UINT32 l_available_data; /* preconditions */ + assert(p_j2k != 00); + assert(p_j2k->m_manager != 00); assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size; @@ -10248,14 +9927,14 @@ static OPJ_BOOL opj_j2k_post_write_tile ( opj_j2k_t * p_j2k, l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data; 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)) { + if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream)) { 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)) { + if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream)) { return OPJ_FALSE; } @@ -10264,7 +9943,7 @@ static OPJ_BOOL opj_j2k_post_write_tile ( opj_j2k_t * p_j2k, 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) { + l_nb_bytes_written,&(p_j2k->m_manager->event_mgr)) != l_nb_bytes_written) { return OPJ_FALSE; } @@ -10403,8 +10082,7 @@ static OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_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_stream_private_t *p_stream ) { OPJ_UINT32 l_nb_bytes_written = 0; OPJ_UINT32 l_current_nb_bytes_written; @@ -10426,7 +10104,7 @@ static OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k, 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)) + if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream)) { return OPJ_FALSE; } @@ -10439,13 +10117,13 @@ static OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k, #if 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); + opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,&(p_j2k->m_manager->event_mgr)); 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); + opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,&(p_j2k->m_manager->event_mgr)); 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; @@ -10454,7 +10132,7 @@ static OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k, 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); + opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_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; @@ -10462,7 +10140,7 @@ static OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k, } 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)) { + if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream)) { return OPJ_FALSE; } @@ -10483,8 +10161,7 @@ static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_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_stream_private_t *p_stream ) { OPJ_UINT32 tilepartno=0; @@ -10514,7 +10191,7 @@ static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k, 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)) { + if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream)) { return OPJ_FALSE; } @@ -10524,7 +10201,7 @@ static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k, l_part_tile_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)) { + if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream)) { return OPJ_FALSE; } @@ -10554,7 +10231,7 @@ static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k, 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)) { + if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream)) { return OPJ_FALSE; } @@ -10565,7 +10242,7 @@ static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k, 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)) { + if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream)) { return OPJ_FALSE; } @@ -10590,57 +10267,53 @@ static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k, return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) +static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k, struct opj_stream_private *p_stream) { 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_j2k->m_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); - if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) { + if (!opj_stream_seek(p_stream, l_tlm_position, &(p_j2k->m_manager->event_mgr))) { 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) { + if (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer, l_tlm_size, &(p_j2k->m_manager->event_mgr)) != l_tlm_size) { return OPJ_FALSE; } - if (! opj_stream_seek(p_stream,l_current_position,p_manager)) { + if (!opj_stream_seek(p_stream, l_current_position, &(p_j2k->m_manager->event_mgr))) { return OPJ_FALSE; } return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_end_encoding( opj_j2k_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) +static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k, struct opj_stream_private *p_stream) { /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); + assert(p_j2k->m_manager != 00); assert(p_stream != 00); - opj_tcd_destroy(p_j2k->m_tcd); + opj_tcd_destroy(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, 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); + opj_manager_free(p_j2k->m_manager, p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0; } @@ -10653,17 +10326,15 @@ static OPJ_BOOL opj_j2k_end_encoding( opj_j2k_t *p_j2k, * Destroys the memory associated with the decoding of headers. */ static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager + opj_stream_private_t *p_stream ) { /* 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); + opj_manager_free(p_j2k->m_manager, p_j2k->m_specific_param.m_encoder.m_header_tile_data); p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0; } @@ -10672,15 +10343,12 @@ static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k, return OPJ_TRUE; } -static OPJ_BOOL opj_j2k_init_info( opj_j2k_t *p_j2k, - struct opj_stream_private *p_stream, - struct opj_event_mgr * p_manager ) +static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k, struct opj_stream_private *p_stream) { opj_codestream_info_t * l_cstr_info = 00; /* preconditions */ assert(p_j2k != 00); - assert(p_manager != 00); assert(p_stream != 00); (void)l_cstr_info; @@ -10723,7 +10391,7 @@ static OPJ_BOOL opj_j2k_init_info( opj_j2k_t *p_j2k, 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); } /** @@ -10731,27 +10399,24 @@ static OPJ_BOOL opj_j2k_init_info( opj_j2k_t *p_j2k, * * @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_t *p_j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager + opj_stream_private_t *p_stream ) { /* 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(p_j2k->m_manager, OPJ_FALSE); if (! p_j2k->m_tcd) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n"); + opj_event_msg(&(p_j2k->m_manager->event_mgr), 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); + opj_tcd_destroy(p_j2k->m_manager, p_j2k->m_tcd); p_j2k->m_tcd = 00; return OPJ_FALSE; } @@ -10763,11 +10428,10 @@ OPJ_BOOL opj_j2k_write_tile (opj_j2k_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_stream_private_t *p_stream ) { - if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index); + if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index); return OPJ_FALSE; } else { @@ -10776,19 +10440,19 @@ OPJ_BOOL opj_j2k_write_tile (opj_j2k_t * p_j2k, for (j=0;jm_tcd->image->numcomps;++j) { opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j; - if(! opj_alloc_tile_component_data(l_tilec)) { - opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data." ); + if(! opj_alloc_tile_component_data(p_j2k->m_manager, l_tilec)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error allocating tile component data." ); return OPJ_FALSE; } } /* now copy data into the the tile component */ if (! opj_tcd_copy_tile_data(p_j2k->m_tcd,p_data,p_data_size)) { - opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." ); + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Size mismatch between tile data and sent data." ); return OPJ_FALSE; } - if (! opj_j2k_post_write_tile(p_j2k,p_stream,p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index); + if (! opj_j2k_post_write_tile(p_j2k,p_stream)) { + opj_event_msg(&(p_j2k->m_manager->event_mgr), EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index); return OPJ_FALSE; } } diff --git a/src/lib/openjp2/j2k.h b/src/lib/openjp2/j2k.h index 358e0739..dfe38251 100644 --- a/src/lib/openjp2/j2k.h +++ b/src/lib/openjp2/j2k.h @@ -555,6 +555,9 @@ JPEG-2000 codestream reader/writer */ typedef struct opj_j2k { + /* Memory manager */ + opj_manager_t m_manager; + /* J2K codestream is decoded*/ OPJ_BOOL m_is_decoder; @@ -612,13 +615,12 @@ void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters); * * @return Returns a handle to a J2K compressor if successful, returns NULL otherwise */ -opj_j2k_t* opj_j2k_create_compress(void); +opj_j2k_t* opj_j2k_create_compress(opj_manager_t manager); OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k, opj_cparameters_t *parameters, - opj_image_t *image, - opj_event_mgr_t * p_manager); + opj_image_t *image); /** Converts an enum type progression order to string type @@ -635,8 +637,7 @@ char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order); * codestream. */ OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *j2k, - opj_stream_private_t *p_stream, - opj_event_mgr_t * p_manager); + opj_stream_private_t *p_stream); /** * Reads a jpeg2000 codestream header structure. @@ -644,14 +645,12 @@ OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *j2k, * @param p_stream the stream to read data from. * @param p_j2k the jpeg2000 codec. * @param p_image FIXME DOC - * @param p_manager the user event manager. * * @return true if the box is valid. */ OPJ_BOOL opj_j2k_read_header( opj_stream_private_t *p_stream, opj_j2k_t* p_j2k, - opj_image_t** p_image, - opj_event_mgr_t* p_manager ); + opj_image_t** p_image ); /** @@ -666,7 +665,7 @@ void opj_j2k_destroy (opj_j2k_t *p_j2k); * * @param p_cstr_ind the codestream index parameter to destroy. */ -void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind); +void j2k_destroy_cstr_index (opj_manager_t manager, opj_codestream_index_t *p_cstr_ind); /** * Decode tile data. @@ -681,8 +680,7 @@ OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_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_stream_private_t *p_stream ); /** * Reads a tile header. @@ -707,8 +705,7 @@ OPJ_BOOL opj_j2k_read_tile_header ( opj_j2k_t * p_j2k, 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_stream_private_t *p_stream ); /** @@ -720,22 +717,20 @@ OPJ_BOOL opj_j2k_read_tile_header ( opj_j2k_t * p_j2k, * @param p_start_y the up position of the rectangle to decode (in image coordinates). * @param p_end_x the right position of the rectangle to decode (in image coordinates). * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). - * @param p_manager the user event manager * * @return true if the area could be set. */ OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_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_INT32 p_end_x, OPJ_INT32 p_end_y ); /** * Creates a J2K decompression structure. * * @return a handle to a J2K decompressor if successful, NULL otherwise. */ -opj_j2k_t* opj_j2k_create_decompress(void); +opj_j2k_t* opj_j2k_create_decompress(opj_manager_t manager); /** @@ -791,24 +786,20 @@ opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k); * @param j2k J2K decompressor handle * @param p_stream FIXME DOC * @param p_image FIXME DOC - * @param p_manager FIXME DOC * @return FIXME DOC */ OPJ_BOOL opj_j2k_decode(opj_j2k_t *j2k, opj_stream_private_t *p_stream, - opj_image_t *p_image, - opj_event_mgr_t *p_manager); + opj_image_t *p_image); OPJ_BOOL opj_j2k_get_tile( opj_j2k_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_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, - OPJ_UINT32 res_factor, - opj_event_mgr_t * p_manager); + OPJ_UINT32 res_factor); /** @@ -818,21 +809,18 @@ OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k, * @param p_data FIXME DOC * @param p_data_size FIXME DOC * @param p_stream the stream to write data to. - * @param p_manager the user event manager. */ OPJ_BOOL opj_j2k_write_tile ( opj_j2k_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_stream_private_t *p_stream ); /** * Encodes an image into a JPEG-2000 codestream */ -OPJ_BOOL opj_j2k_encode( opj_j2k_t * p_j2k, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ); +OPJ_BOOL opj_j2k_encode( opj_j2k_t * p_j2k, + opj_stream_private_t *cio ); /** * Starts a compression scheme, i.e. validates the codec parameters, writes the header. @@ -840,24 +828,20 @@ OPJ_BOOL opj_j2k_encode( opj_j2k_t * p_j2k, * @param p_j2k the jpeg2000 codec. * @param p_stream the stream object. * @param p_image FIXME DOC - * @param p_manager the user event manager. * * @return true if the codec is valid. */ OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k, opj_stream_private_t *p_stream, - opj_image_t * p_image, - opj_event_mgr_t * p_manager); + opj_image_t * p_image); /** * Ends the compression procedures and possibiliy add data to be read after the * codestream. */ -OPJ_BOOL opj_j2k_end_compress( opj_j2k_t *p_j2k, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager); +OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k, opj_stream_private_t *cio); -OPJ_BOOL opj_j2k_setup_mct_encoding (opj_tcp_t * p_tcp, opj_image_t * p_image); +static OPJ_BOOL opj_j2k_setup_mct_encoding (opj_manager_t manager, opj_tcp_t * p_tcp, opj_image_t * p_image); #endif /* __J2K_H */ diff --git a/src/lib/openjp2/jp2.c b/src/lib/openjp2/jp2.c index af692d59..5a99e5a9 100644 --- a/src/lib/openjp2/jp2.c +++ b/src/lib/openjp2/jp2.c @@ -55,14 +55,12 @@ * @param p_image_header_data pointer to actual data (already read from file) * @param jp2 the jpeg2000 file codec. * @param p_image_header_size the size of the image header - * @param p_manager the user event manager. * * @return true if the image header is valid, false else. */ static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2, OPJ_BYTE *p_image_header_data, - OPJ_UINT32 p_image_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_image_header_size ); /** * Writes the Image Header box - Image Header box. @@ -92,21 +90,18 @@ static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2, * @param p_bpc_header_data pointer to actual data (already read from file) * @param jp2 the jpeg2000 file codec. * @param p_bpc_header_size the size of the bpc header - * @param p_manager the user event manager. * * @return true if the bpc header is valid, fale else. */ static OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2, OPJ_BYTE * p_bpc_header_data, - OPJ_UINT32 p_bpc_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_bpc_header_size ); static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2, OPJ_BYTE * p_cdef_header_data, - OPJ_UINT32 p_cdef_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_cdef_header_size ); -static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color, opj_event_mgr_t *); +static void opj_jp2_apply_cdef(opj_jp2_t *jp2, opj_image_t *image, opj_jp2_color_t *color); /** * Writes the Channel Definition box. @@ -135,13 +130,11 @@ static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2, * * @param cio the stream to write data to. * @param jp2 the jpeg2000 file codec. - * @param p_manager the user event manager. * * @return true if writing was successful. */ static OPJ_BOOL opj_jp2_write_ftyp( opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *cio ); /** * Reads a a FTYP box - File type box @@ -149,18 +142,15 @@ static OPJ_BOOL opj_jp2_write_ftyp( opj_jp2_t *jp2, * @param p_header_data the data contained in the FTYP box. * @param jp2 the jpeg2000 file codec. * @param p_header_size the size of the data contained in the FTYP box. - * @param p_manager the user event manager. * * @return true if the FTYP box is valid. */ static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size ); static OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *cio ); /** * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box). @@ -168,71 +158,59 @@ static OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2, * @param p_header_data the data contained in the file header box. * @param jp2 the jpeg2000 file codec. * @param p_header_size the size of the data contained in the file header box. - * @param p_manager the user event manager. * * @return true if the JP2 Header box was successfully reconized. */ static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2, OPJ_BYTE *p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_header_size ); /** * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box). * * @param jp2 the jpeg2000 file codec. * @param stream the stream to write data to. - * @param p_manager user event manager. * * @return true if writing was successful. */ static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, - opj_stream_private_t *stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *stream ); /** * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done. * * @param cio the stream to write data to. * @param jp2 the jpeg2000 file codec. - * @param p_manager user event manager. * * @return true if writing was successful. */ static OPJ_BOOL opj_jp2_write_jp2c( opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *cio ); #ifdef USE_JPIP /** * Write index Finder box * @param cio the stream to write to. * @param jp2 the jpeg2000 file codec. - * @param p_manager user event manager. */ static OPJ_BOOL opj_jpip_write_iptr( opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *cio ); /** * Write index Finder box * @param cio the stream to write to. * @param jp2 the jpeg2000 file codec. - * @param p_manager user event manager. */ static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *cio ); /** * Write file Index (superbox) * @param cio the stream to write to. * @param jp2 the jpeg2000 file codec. - * @param p_manager user event manager. */ static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *cio ); #endif /* USE_JPIP */ /** @@ -241,36 +219,32 @@ static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2, * @param p_header_data the data contained in the signature box. * @param jp2 the jpeg2000 file codec. * @param p_header_size the size of the data contained in the signature box. - * @param p_manager the user event manager. * * @return true if the file signature box is valid. */ static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager); + OPJ_UINT32 p_header_size); /** * Writes a jpeg2000 file signature box. * * @param cio the stream to write data to. * @param jp2 the jpeg2000 file codec. - * @param p_manager the user event manager. * * @return true if writing was successful. */ static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *cio ); /** Apply collected palette data @param color Collector for profile, cdef and pclr data @param image */ -static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color); +static OPJ_BOOL opj_jp2_apply_pclr(opj_manager_t manager, opj_image_t *image, opj_jp2_color_t *color); -static void opj_jp2_free_pclr(opj_jp2_color_t *color); +static void opj_jp2_free_pclr(opj_manager_t manager, opj_jp2_color_t *color); /** * Collect palette data @@ -278,14 +252,12 @@ static void opj_jp2_free_pclr(opj_jp2_color_t *color); * @param jp2 JP2 handle * @param p_pclr_header_data FIXME DOC * @param p_pclr_header_size FIXME DOC - * @param p_manager * * @return Returns true if successful, returns false otherwise */ static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2, OPJ_BYTE * p_pclr_header_data, - OPJ_UINT32 p_pclr_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_pclr_header_size); /** * Collect component mapping data @@ -293,15 +265,13 @@ static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2, * @param jp2 JP2 handle * @param p_cmap_header_data FIXME DOC * @param p_cmap_header_size FIXME DOC - * @param p_manager FIXME DOC * * @return Returns true if successful, returns false otherwise */ static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2, OPJ_BYTE * p_cmap_header_data, - OPJ_UINT32 p_cmap_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_cmap_header_size); /** * Reads the Color Specification box. @@ -309,14 +279,12 @@ static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2, * @param p_colr_header_data pointer to actual data (already read from file) * @param jp2 the jpeg2000 file codec. * @param p_colr_header_size the size of the color header - * @param p_manager the user event manager. * * @return true if the bpc header is valid, fale else. */ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2, OPJ_BYTE * p_colr_header_data, - OPJ_UINT32 p_colr_header_size, - opj_event_mgr_t * p_manager ); + OPJ_UINT32 p_colr_header_size); /*@}*/ @@ -339,13 +307,11 @@ static OPJ_BOOL opj_jp2_setup_end_header_reading (opj_jp2_t *jp2, opj_event_mgr_ * * @param jp2 the jpeg2000 file header structure. * @param stream the stream to read data from. - * @param p_manager the user event manager. * * @return true if the box is valid. */ static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2, - opj_stream_private_t *stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *stream ); /** * Excutes the given procedures on the given codec. @@ -353,14 +319,12 @@ static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2, * @param p_procedure_list the list of procedures to execute * @param jp2 the jpeg2000 file codec to execute the procedures on. * @param stream the stream to execute the procedures on. - * @param p_manager the user manager. * * @return true if all the procedures were successfully executed. */ static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2, opj_procedure_list_t * p_procedure_list, - opj_stream_private_t *stream, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *stream ); /** * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. @@ -368,7 +332,7 @@ static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2, * @param cio the input stream to read data from. * @param box the box structure to fill. * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2). - * @param p_manager user event manager. + * @param p_manager user event manager. * * @return true if the box is reconized, false otherwise */ @@ -389,8 +353,7 @@ static OPJ_BOOL opj_jp2_setup_encoding_validation (opj_jp2_t *jp2, opj_event_mgr static OPJ_BOOL opj_jp2_setup_header_writing (opj_jp2_t *jp2, opj_event_mgr_t * p_manager); static OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ); + opj_stream_private_t *cio ); /** * Finds the image execution function related to the given box id. @@ -435,7 +398,7 @@ static const opj_jp2_header_handler_t jp2_img_header [] = * @param p_data the character string to read data from. * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2). * @param p_box_max_size the maximum number of bytes in the box. - * @param p_manager FIXME DOC + * @param p_manager FIXME DOC * * @return true if the box is reconized, false otherwise */ @@ -449,7 +412,7 @@ static OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box, * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters * are valid. Developpers wanting to extend the library can add their own validation procedures. */ -static OPJ_BOOL opj_jp2_setup_decoding_validation (opj_jp2_t *jp2, opj_event_mgr_t * p_manager); +static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2, opj_event_mgr_t * p_manager); /** * Sets up the procedures to do on reading header. @@ -461,7 +424,7 @@ static OPJ_BOOL opj_jp2_setup_header_reading (opj_jp2_t *jp2, opj_event_mgr_t * static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box, OPJ_UINT32 * p_number_bytes_read, opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ) + opj_event_mgr_t * p_manager) { /* read header from file */ OPJ_BYTE l_data_header [8]; @@ -540,16 +503,15 @@ static void jp2_write_url(opj_cio_t *cio, char *Idx_file) { static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2, OPJ_BYTE *p_image_header_data, - OPJ_UINT32 p_image_header_size, - opj_event_mgr_t * p_manager ) + OPJ_UINT32 p_image_header_size ) { /* preconditions */ - assert(p_image_header_data != 00); - assert(jp2 != 00); - assert(p_manager != 00); + assert(p_image_header_data != NULL); + assert(jp2 != NULL); + assert(jp2->manager != NULL); if (p_image_header_size != 14) { - opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Bad image header box (bad size)\n"); return OPJ_FALSE; } @@ -561,9 +523,9 @@ static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2, p_image_header_data += 2; /* allocate memory for components */ - jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps, sizeof(opj_jp2_comps_t)); + jp2->comps = (opj_jp2_comps_t*) opj_manager_calloc(jp2->manager, jp2->numcomps, sizeof(opj_jp2_comps_t)); if (jp2->comps == 0) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Not enough memory to handle image header (ihdr)\n"); return OPJ_FALSE; } @@ -575,7 +537,7 @@ static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2, /* Should be equal to 7 cf. chapter about image header box of the norm */ if (jp2->C != 7){ - opj_event_msg(p_manager, EVT_INFO, "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n", jp2->C); + opj_event_msg(&(jp2->manager->event_mgr), EVT_INFO, "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n", jp2->C); } opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */ @@ -593,11 +555,12 @@ static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2, OPJ_BYTE * l_ihdr_data,* l_current_ihdr_ptr; /* preconditions */ - assert(jp2 != 00); - assert(p_nb_bytes_written != 00); + assert(jp2 != NULL); + assert(jp2->manager != NULL); + assert(p_nb_bytes_written != NULL); /* default image header is 22 bytes wide */ - l_ihdr_data = (OPJ_BYTE *) opj_calloc(1,22); + l_ihdr_data = (OPJ_BYTE *) opj_manager_calloc(jp2->manager, 1, 22); if (l_ihdr_data == 00) { return 00; } @@ -647,9 +610,10 @@ static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2, /* preconditions */ assert(jp2 != 00); + assert(jp2->manager != 00); assert(p_nb_bytes_written != 00); - l_bpcc_data = (OPJ_BYTE *) opj_calloc(1,l_bpcc_size); + l_bpcc_data = (OPJ_BYTE *) opj_manager_calloc(jp2->manager, 1, l_bpcc_size); if (l_bpcc_data == 00) { return 00; } @@ -674,8 +638,7 @@ static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2, static OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2, OPJ_BYTE * p_bpc_header_data, - OPJ_UINT32 p_bpc_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_bpc_header_size ) { OPJ_UINT32 i; @@ -683,16 +646,16 @@ static OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2, /* preconditions */ assert(p_bpc_header_data != 00); assert(jp2 != 00); - assert(p_manager != 00); + assert(jp2->manager != 00); if (jp2->bpc != 255 ){ - opj_event_msg(p_manager, EVT_WARNING, "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",jp2->bpc); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",jp2->bpc); } /* and length is relevant */ if (p_bpc_header_size != jp2->numcomps) { - opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Bad BPCC header box (bad size)\n"); return OPJ_FALSE; } @@ -721,7 +684,7 @@ static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2, OPJ_UINT32 * p_nb_bytes_wri l_cdef_size += 6U * jp2->color.jp2_cdef->n; - l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size); + l_cdef_data = (OPJ_BYTE *) opj_manager_malloc(jp2->manager, l_cdef_size); if (l_cdef_data == 00) { return 00; } @@ -764,8 +727,9 @@ static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2, /* preconditions */ assert(jp2 != 00); + assert(jp2->manager != 00); assert(p_nb_bytes_written != 00); - assert(jp2->meth == 1 || jp2->meth == 2); + assert(jp2->meth == 1 || jp2->meth == 2); switch (jp2->meth) { case 1 : @@ -779,7 +743,7 @@ static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2, return 00; } - l_colr_data = (OPJ_BYTE *) opj_calloc(1,l_colr_size); + l_colr_data = (OPJ_BYTE *) opj_manager_calloc(jp2->manager, 1, l_colr_size); if (l_colr_data == 00) { return 00; } @@ -818,18 +782,21 @@ static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2, return l_colr_data; } -static void opj_jp2_free_pclr(opj_jp2_color_t *color) +static void opj_jp2_free_pclr(opj_manager_t manager, opj_jp2_color_t *color) { - opj_free(color->jp2_pclr->channel_sign); - opj_free(color->jp2_pclr->channel_size); - opj_free(color->jp2_pclr->entries); + opj_manager_free(manager, color->jp2_pclr->channel_sign); + opj_manager_free(manager, color->jp2_pclr->channel_size); + opj_manager_free(manager, color->jp2_pclr->entries); - if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap); + if(color->jp2_pclr->cmap) { + opj_manager_free(manager, color->jp2_pclr->cmap); + } - opj_free(color->jp2_pclr); color->jp2_pclr = NULL; + opj_manager_free(manager, color->jp2_pclr); + color->jp2_pclr = NULL; } -static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, opj_event_mgr_t *p_manager) +static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, opj_manager_t p_manager) { OPJ_UINT16 i; @@ -846,13 +813,13 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, for (i = 0; i < n; i++) { if (info[i].cn >= nr_channels) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].cn, nr_channels); + opj_event_msg(&(p_manager->event_mgr), EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].cn, nr_channels); return OPJ_FALSE; } if (info[i].asoc == 65535U) continue; if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].asoc - 1, nr_channels); + opj_event_msg(&(p_manager->event_mgr), EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].asoc - 1, nr_channels); return OPJ_FALSE; } } @@ -867,7 +834,7 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, } } if (i == n) { - opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n"); + opj_event_msg(&(p_manager->event_mgr), EVT_ERROR, "Incomplete channel definitions.\n"); return OPJ_FALSE; } --nr_channels; @@ -884,14 +851,14 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, /* verify that all original components match an existing one */ for (i = 0; i < nr_channels; i++) { if (cmap[i].cmp >= image->numcomps) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", cmap[i].cmp, image->numcomps); + opj_event_msg(&(p_manager->event_mgr), EVT_ERROR, "Invalid component index %d (>= %d).\n", cmap[i].cmp, image->numcomps); is_sane = OPJ_FALSE; } } - pcol_usage = opj_calloc(nr_channels, sizeof(OPJ_BOOL)); + pcol_usage = opj_manager_calloc(p_manager, nr_channels, sizeof(OPJ_BOOL)); if (!pcol_usage) { - opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n"); + opj_event_msg(&(p_manager->event_mgr), EVT_ERROR, "Unexpected OOM.\n"); return OPJ_FALSE; } /* verify that no component is targeted more than once */ @@ -899,17 +866,17 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, OPJ_UINT16 pcol = cmap[i].pcol; assert(cmap[i].mtyp == 0 || cmap[i].mtyp == 1); if (pcol >= nr_channels) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid component/palette index for direct mapping %d.\n", pcol); + opj_event_msg(&(p_manager->event_mgr), EVT_ERROR, "Invalid component/palette index for direct mapping %d.\n", pcol); is_sane = OPJ_FALSE; } else if (pcol_usage[pcol] && cmap[i].mtyp == 1) { - opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol); + opj_event_msg(&(p_manager->event_mgr), EVT_ERROR, "Component %d is mapped twice.\n", pcol); is_sane = OPJ_FALSE; } else if (cmap[i].mtyp == 0 && cmap[i].pcol != 0) { /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then * the value of this field shall be 0. */ - opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i, pcol); + opj_event_msg(&(p_manager->event_mgr), EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i, pcol); is_sane = OPJ_FALSE; } else @@ -918,7 +885,7 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, /* verify that all components are targeted at least once */ for (i = 0; i < nr_channels; i++) { if (!pcol_usage[i] && cmap[i].mtyp != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n", i); + opj_event_msg(&(p_manager->event_mgr), EVT_ERROR, "Component %d doesn't have a mapping.\n", i); is_sane = OPJ_FALSE; } } @@ -927,7 +894,7 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, for (i = 0; i < nr_channels; i++) { if (!pcol_usage[i]) { is_sane = 0U; - opj_event_msg(p_manager, EVT_WARNING, "Component mapping seems wrong. Trying to correct.\n", i); + opj_event_msg(&(p_manager->event_mgr), EVT_WARNING, "Component mapping seems wrong. Trying to correct.\n", i); break; } } @@ -939,7 +906,7 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, } } } - opj_free(pcol_usage); + opj_manager_free(p_manager, pcol_usage); if (!is_sane) { return OPJ_FALSE; } @@ -949,7 +916,7 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, } /* file9.jp2 */ -static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color) +static OPJ_BOOL opj_jp2_apply_pclr(opj_manager_t manager, opj_image_t *image, opj_jp2_color_t *color) { opj_image_comp_t *old_comps, *new_comps; OPJ_BYTE *channel_size, *channel_sign; @@ -967,12 +934,10 @@ static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color) nr_channels = color->jp2_pclr->nr_channels; old_comps = image->comps; - new_comps = (opj_image_comp_t*) - opj_malloc(nr_channels * sizeof(opj_image_comp_t)); + new_comps = (opj_image_comp_t*)opj_manager_malloc(manager, nr_channels * sizeof(opj_image_comp_t)); if (!new_comps) { - /* FIXME no error code for opj_jp2_apply_pclr */ /* FIXME event manager error callback */ - return; + return OPJ_FALSE; } for(i = 0; i < nr_channels; ++i) { pcol = cmap[i].pcol; cmp = cmap[i].cmp; @@ -987,14 +952,12 @@ static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color) } /* Palette mapping: */ - new_comps[i].data = (OPJ_INT32*) - opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32)); + new_comps[i].data = (OPJ_INT32*)opj_manager_malloc(manager, old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32)); if (!new_comps[i].data) { - opj_free(new_comps); + opj_manager_free(manager, new_comps); new_comps = NULL; - /* FIXME no error code for opj_jp2_apply_pclr */ /* FIXME event manager error callback */ - return; + return OPJ_FALSE; } new_comps[i].prec = channel_size[i]; new_comps[i].sgnd = channel_sign[i]; @@ -1034,21 +997,20 @@ static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color) max = image->numcomps; for(i = 0; i < max; ++i) { - if(old_comps[i].data) opj_free(old_comps[i].data); + if(old_comps[i].data) opj_manager_free(manager, old_comps[i].data); } - opj_free(old_comps); + opj_manager_free(manager, old_comps); image->comps = new_comps; image->numcomps = nr_channels; - opj_jp2_free_pclr(color); - + opj_jp2_free_pclr(manager, color); + return OPJ_TRUE; }/* apply_pclr() */ static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2, OPJ_BYTE * p_pclr_header_data, - OPJ_UINT32 p_pclr_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_pclr_header_size ) { opj_jp2_pclr_t *jp2_pclr; @@ -1062,7 +1024,7 @@ static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2, /* preconditions */ assert(p_pclr_header_data != 00); assert(jp2 != 00); - assert(p_manager != 00); + assert(jp2->manager != 00); (void)p_pclr_header_size; if(jp2->color.jp2_pclr) @@ -1075,7 +1037,7 @@ static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2, p_pclr_header_data += 2; nr_entries = (OPJ_UINT16) l_value; if ((nr_entries == 0U) || (nr_entries > 1024U)) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports %d entries\n", (int)nr_entries); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Invalid PCLR box. Reports %d entries\n", (int)nr_entries); return OPJ_FALSE; } @@ -1083,36 +1045,36 @@ static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2, ++p_pclr_header_data; nr_channels = (OPJ_UINT16) l_value; if (nr_channels == 0U) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports 0 palette columns\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Invalid PCLR box. Reports 0 palette columns\n"); return OPJ_FALSE; } if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels) return OPJ_FALSE; - entries = (OPJ_UINT32*) opj_malloc((size_t)nr_channels * nr_entries * sizeof(OPJ_UINT32)); + entries = (OPJ_UINT32*) opj_manager_malloc(jp2->manager, (size_t)nr_channels * nr_entries * sizeof(OPJ_UINT32)); if (!entries) return OPJ_FALSE; - channel_size = (OPJ_BYTE*) opj_malloc(nr_channels); + channel_size = (OPJ_BYTE*) opj_manager_malloc(jp2->manager, nr_channels); if (!channel_size) { - opj_free(entries); + opj_manager_free(jp2->manager, entries); return OPJ_FALSE; } - channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels); + channel_sign = (OPJ_BYTE*) opj_manager_malloc(jp2->manager, nr_channels); if (!channel_sign) { - opj_free(entries); - opj_free(channel_size); + opj_manager_free(jp2->manager, entries); + opj_manager_free(jp2->manager, channel_size); return OPJ_FALSE; } - jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t)); + jp2_pclr = (opj_jp2_pclr_t*)opj_manager_malloc(jp2->manager, sizeof(opj_jp2_pclr_t)); if (!jp2_pclr) { - opj_free(entries); - opj_free(channel_size); - opj_free(channel_sign); + opj_manager_free(jp2->manager, entries); + opj_manager_free(jp2->manager, channel_size); + opj_manager_free(jp2->manager, channel_sign); return OPJ_FALSE; } @@ -1154,8 +1116,7 @@ static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2, static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2, OPJ_BYTE * p_cmap_header_data, - OPJ_UINT32 p_cmap_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_cmap_header_size ) { opj_jp2_cmap_comp_t *cmap; @@ -1165,12 +1126,12 @@ static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2, /* preconditions */ assert(jp2 != 00); assert(p_cmap_header_data != 00); - assert(p_manager != 00); + assert(jp2->manager != 00); (void)p_cmap_header_size; /* Need nr_channels: */ if(jp2->color.jp2_pclr == NULL) { - opj_event_msg(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n"); return OPJ_FALSE; } @@ -1178,17 +1139,17 @@ static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2, * inside a JP2 Header box' : */ if(jp2->color.jp2_pclr->cmap) { - opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Only one CMAP box is allowed.\n"); return OPJ_FALSE; } nr_channels = jp2->color.jp2_pclr->nr_channels; if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) { - opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Insufficient data for CMAP box.\n"); return OPJ_FALSE; } - cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t)); + cmap = (opj_jp2_cmap_comp_t*) opj_manager_malloc(jp2->manager, nr_channels * sizeof(opj_jp2_cmap_comp_t)); if (!cmap) return OPJ_FALSE; @@ -1212,7 +1173,7 @@ static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2, return OPJ_TRUE; } -static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color, opj_event_mgr_t *manager) +static void opj_jp2_apply_cdef(opj_jp2_t *jp2, opj_image_t *image, opj_jp2_color_t *color) { opj_jp2_cdef_info_t *info; OPJ_UINT16 i, n, cn, asoc, acn; @@ -1228,7 +1189,7 @@ static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color, opj_e if( cn >= image->numcomps) { - opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n", cn, image->numcomps); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n", cn, image->numcomps); continue; } if(asoc == 0 || asoc == 65535) @@ -1240,7 +1201,7 @@ static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color, opj_e acn = (OPJ_UINT16)(asoc - 1); if( acn >= image->numcomps ) { - opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n", acn, image->numcomps); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n", acn, image->numcomps); continue; } @@ -1270,16 +1231,15 @@ static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color, opj_e image->comps[cn].alpha = info[i].typ; } - if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info); - - opj_free(color->jp2_cdef); color->jp2_cdef = NULL; + if (color->jp2_cdef->info) opj_manager_free(jp2->manager, color->jp2_cdef->info); + opj_manager_free(jp2->manager, color->jp2_cdef); + color->jp2_cdef = NULL; }/* jp2_apply_cdef() */ static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2, OPJ_BYTE * p_cdef_header_data, - OPJ_UINT32 p_cdef_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_cdef_header_size ) { opj_jp2_cdef_info_t *cdef_info; @@ -1288,16 +1248,17 @@ static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2, /* preconditions */ assert(jp2 != 00); + assert(jp2->manager != 00); assert(p_cdef_header_data != 00); - assert(p_manager != 00); - (void)p_cdef_header_size; + + (void)p_cdef_header_size; /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box * inside a JP2 Header box.'*/ if(jp2->color.jp2_cdef) return OPJ_FALSE; if (p_cdef_header_size < 2) { - opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Insufficient data for CDEF box.\n"); return OPJ_FALSE; } @@ -1305,23 +1266,23 @@ static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2, p_cdef_header_data+= 2; if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */ - opj_event_msg(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n"); return OPJ_FALSE; } if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) { - opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Insufficient data for CDEF box.\n"); return OPJ_FALSE; } - cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t)); + cdef_info = (opj_jp2_cdef_info_t*) opj_manager_malloc(jp2->manager, l_value * sizeof(opj_jp2_cdef_info_t)); if (!cdef_info) return OPJ_FALSE; - jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t)); + jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_manager_malloc(jp2->manager, sizeof(opj_jp2_cdef_t)); if(!jp2->color.jp2_cdef) { - opj_free(cdef_info); + opj_manager_free(jp2->manager, cdef_info); return OPJ_FALSE; } jp2->color.jp2_cdef->info = cdef_info; @@ -1346,19 +1307,18 @@ static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2, static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2, OPJ_BYTE * p_colr_header_data, - OPJ_UINT32 p_colr_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_colr_header_size ) { OPJ_UINT32 l_value; /* preconditions */ assert(jp2 != 00); + assert(jp2->manager != 00); assert(p_colr_header_data != 00); - assert(p_manager != 00); if (p_colr_header_size < 3) { - opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Bad COLR header box (bad size)\n"); return OPJ_FALSE; } @@ -1366,7 +1326,7 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2, * Specification boxes after the first.' */ if(jp2->color.jp2_has_colr) { - opj_event_msg(p_manager, EVT_INFO, "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_INFO, "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n"); p_colr_header_data += p_colr_header_size; return OPJ_TRUE; } @@ -1382,12 +1342,12 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2, if (jp2->meth == 1) { if (p_colr_header_size < 7) { - opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n", p_colr_header_size); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Bad COLR header box (bad size: %d)\n", p_colr_header_size); return OPJ_FALSE; } if ((p_colr_header_size > 7) && (jp2->enumcs != 14)) { /* handled below for CIELab) */ /* testcase Altona_Technical_v20_x4.pdf */ - opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n", p_colr_header_size); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "Bad COLR header box (bad size: %d)\n", p_colr_header_size); } opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */ @@ -1399,7 +1359,7 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2, OPJ_UINT32 *cielab; OPJ_UINT32 rl, ol, ra, oa, rb, ob, il; - cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32)); + cielab = (OPJ_UINT32*)opj_manager_malloc(jp2->manager, 9 * sizeof(OPJ_UINT32)); cielab[0] = 14; /* enumcs */ /* default values */ @@ -1428,7 +1388,7 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2, } else if(p_colr_header_size != 7) { - opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size); } cielab[2] = rl; cielab[4] = ra; cielab[6] = rb; cielab[3] = ol; cielab[5] = oa; cielab[7] = ob; @@ -1445,7 +1405,7 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2, OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3; jp2->color.icc_profile_len = (OPJ_UINT32)icc_len; - jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1,(size_t)icc_len); + jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_manager_calloc(jp2->manager, 1,(size_t)icc_len); if (!jp2->color.icc_profile_buf) { jp2->color.icc_profile_len = 0; @@ -1465,7 +1425,7 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2, { /* ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values: conforming JP2 reader shall ignore the entire Colour Specification box.*/ - opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), " + opj_event_msg(&(jp2->manager->event_mgr), EVT_INFO, "COLR BOX meth value is not a regular value (%d), " "so we will ignore the entire Colour Specification box. \n", jp2->meth); } return OPJ_TRUE; @@ -1473,20 +1433,19 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2, OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2, opj_stream_private_t *p_stream, - opj_image_t* p_image, - opj_event_mgr_t * p_manager) + opj_image_t* p_image) { if (!p_image) return OPJ_FALSE; /* J2K decoding */ - if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n"); + if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image) ) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to decode the codestream in the JP2 file\n"); return OPJ_FALSE; } if (!jp2->ignore_pclr_cmap_cdef){ - if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) { + if (!opj_jp2_check_color(p_image, &(jp2->color), jp2->manager)) { return OPJ_FALSE; } @@ -1507,14 +1466,14 @@ OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2, if(jp2->color.jp2_pclr) { /* Part 1, I.5.3.4: Either both or none : */ if( !jp2->color.jp2_pclr->cmap) - opj_jp2_free_pclr(&(jp2->color)); + opj_jp2_free_pclr(jp2->manager, &(jp2->color)); else - opj_jp2_apply_pclr(p_image, &(jp2->color)); + opj_jp2_apply_pclr(jp2->manager, p_image, &(jp2->color)); } /* Apply the color space if needed */ if(jp2->color.jp2_cdef) { - opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager); + opj_jp2_apply_cdef(jp2, p_image, &(jp2->color)); } if(jp2->color.icc_profile_buf) { @@ -1528,8 +1487,7 @@ OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2, } static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, - opj_stream_private_t *stream, - opj_event_mgr_t * p_manager + opj_stream_private_t *stream ) { opj_jp2_img_header_writer_handler_t l_writers [4]; @@ -1546,7 +1504,6 @@ static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, /* preconditions */ assert(stream != 00); assert(jp2 != 00); - assert(p_manager != 00); memset(l_writers,0,sizeof(l_writers)); @@ -1575,7 +1532,7 @@ static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, for (i=0;im_data = l_current_writer->handler(jp2,&(l_current_writer->m_size)); if (l_current_writer->m_data == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Not enough memory to hold JP2 Header data\n"); l_result = OPJ_FALSE; break; } @@ -1588,7 +1545,7 @@ static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, l_current_writer = l_writers; for (i=0;im_data != 00) { - opj_free(l_current_writer->m_data ); + opj_manager_free(jp2->manager, l_current_writer->m_data ); } ++l_current_writer; } @@ -1600,16 +1557,16 @@ static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, opj_write_bytes(l_jp2h_data,l_jp2h_size,4); /* write super box data on stream */ - if (opj_stream_write_data(stream,l_jp2h_data,8,p_manager) != 8) { - opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n"); + if (opj_stream_write_data(stream,l_jp2h_data,8,&(jp2->manager->event_mgr)) != 8) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Stream error while writing JP2 Header box\n"); l_result = OPJ_FALSE; } if (l_result) { l_current_writer = l_writers; for (i=0;im_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) { - opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n"); + if (opj_stream_write_data(stream,l_current_writer->m_data,l_current_writer->m_size,&(jp2->manager->event_mgr)) != l_current_writer->m_size) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Stream error while writing JP2 Header box\n"); l_result = OPJ_FALSE; break; } @@ -1622,7 +1579,7 @@ static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, /* cleanup */ for (i=0;im_data != 00) { - opj_free(l_current_writer->m_data ); + opj_manager_free(jp2->manager, l_current_writer->m_data ); } ++l_current_writer; } @@ -1631,8 +1588,7 @@ static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, } static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *cio ) { OPJ_UINT32 i; OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl; @@ -1642,12 +1598,11 @@ static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2, /* preconditions */ assert(cio != 00); assert(jp2 != 00); - assert(p_manager != 00); - l_ftyp_data = (OPJ_BYTE *) opj_calloc(1,l_ftyp_size); + l_ftyp_data = (OPJ_BYTE *) opj_manager_calloc(jp2->manager, 1, l_ftyp_size); if (l_ftyp_data == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Not enough memory to handle ftyp data\n"); return OPJ_FALSE; } @@ -1669,20 +1624,19 @@ static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2, opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */ } - l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size); + l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,&(jp2->manager->event_mgr)) == l_ftyp_size); if (! l_result) { - opj_event_msg(p_manager, EVT_ERROR, "Error while writing ftyp data to stream\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Error while writing ftyp data to stream\n"); } - opj_free(l_ftyp_data); + opj_manager_free(jp2->manager, l_ftyp_data); return l_result; } static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *cio ) { OPJ_OFF_T j2k_codestream_exit; OPJ_BYTE l_data_header [8]; @@ -1690,7 +1644,6 @@ static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2, /* preconditions */ assert(jp2 != 00); assert(cio != 00); - assert(p_manager != 00); assert(opj_stream_has_seek(cio)); j2k_codestream_exit = opj_stream_tell(cio); @@ -1699,18 +1652,18 @@ static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2, 4); /* size of codestream */ opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */ - if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,&(jp2->manager->event_mgr))) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } - if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (opj_stream_write_data(cio,l_data_header,8,&(jp2->manager->event_mgr)) != 8) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } - if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (! opj_stream_seek(cio,j2k_codestream_exit,&(jp2->manager->event_mgr))) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } @@ -1718,8 +1671,7 @@ static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2, } static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *cio ) { /* 12 bytes will be read */ OPJ_BYTE l_signature_data [12]; @@ -1727,7 +1679,6 @@ static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2, /* preconditions */ assert(cio != 00); assert(jp2 != 00); - assert(p_manager != 00); /* write box length */ opj_write_bytes(l_signature_data,12,4); @@ -1736,7 +1687,7 @@ static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2, /* writes magic number*/ opj_write_bytes(l_signature_data+8,0x0d0a870a,4); - if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) { + if (opj_stream_write_data(cio,l_signature_data,12,&(jp2->manager->event_mgr)) != 12) { return OPJ_FALSE; } @@ -1763,8 +1714,7 @@ void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2, opj_cparameters_t *parameters, - opj_image_t *image, - opj_event_mgr_t * p_manager) + opj_image_t *image) { OPJ_UINT32 i; OPJ_UINT32 depth_0; @@ -1782,11 +1732,11 @@ OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2, /* Check if number of components respects standard */ if (image->numcomps < 1 || image->numcomps > 16384) { - opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n"); return OPJ_FALSE; } - if (opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager ) == OPJ_FALSE) { + if (opj_j2k_setup_encoder(jp2->j2k, parameters, image) == OPJ_FALSE) { return OPJ_FALSE; } @@ -1798,10 +1748,10 @@ OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2, jp2->brand = JP2_JP2; /* BR */ jp2->minversion = 0; /* MinV */ jp2->numcl = 1; - jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32)); + jp2->cl = (OPJ_UINT32*) opj_manager_malloc(jp2->manager, jp2->numcl * sizeof(OPJ_UINT32)); if (!jp2->cl){ jp2->cl = NULL; - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Not enough memory when setup the JP2 encoder\n"); return OPJ_FALSE; } jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */ @@ -1809,10 +1759,10 @@ OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2, /* Image Header box */ jp2->numcomps = image->numcomps; /* NC */ - jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t)); + jp2->comps = (opj_jp2_comps_t*) opj_manager_malloc(jp2->manager, jp2->numcomps * sizeof(opj_jp2_comps_t)); if (!jp2->comps) { jp2->comps = NULL; - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Not enough memory when setup the JP2 encoder\n"); /* Memory of jp2->cl will be freed by opj_jp2_destroy */ return OPJ_FALSE; } @@ -1877,29 +1827,29 @@ OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2, break; } if (alpha_count == 0U) { - opj_event_msg(p_manager, EVT_WARNING, "Alpha channel specified but unknown enumcs. No cdef box will be created.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "Alpha channel specified but unknown enumcs. No cdef box will be created.\n"); } else if (image->numcomps < (color_channels+1)) { - opj_event_msg(p_manager, EVT_WARNING, "Alpha channel specified but not enough image components for an automatic cdef box creation.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "Alpha channel specified but not enough image components for an automatic cdef box creation.\n"); alpha_count = 0U; } else if ((OPJ_UINT32)alpha_channel < color_channels) { - opj_event_msg(p_manager, EVT_WARNING, "Alpha channel position conflicts with color channel. No cdef box will be created.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "Alpha channel position conflicts with color channel. No cdef box will be created.\n"); alpha_count = 0U; } } else if (alpha_count > 1) { - opj_event_msg(p_manager, EVT_WARNING, "Multiple alpha channels specified. No cdef box will be created.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "Multiple alpha channels specified. No cdef box will be created.\n"); } if (alpha_count == 1U) { /* if here, we know what we can do */ - jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t)); + jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_manager_malloc(jp2->manager, sizeof(opj_jp2_cdef_t)); if(!jp2->color.jp2_cdef) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup the JP2 encoder\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Not enough memory to setup the JP2 encoder\n"); return OPJ_FALSE; } /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */ /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */ - jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(image->numcomps * sizeof(opj_jp2_cdef_info_t)); + jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_manager_malloc(jp2->manager, image->numcomps * sizeof(opj_jp2_cdef_info_t)); if (!jp2->color.jp2_cdef->info) { /* memory will be freed by opj_jp2_destroy */ - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup the JP2 encoder\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Not enough memory to setup the JP2 encoder\n"); return OPJ_FALSE; } jp2->color.jp2_cdef->n = (OPJ_UINT16) image->numcomps; /* cast is valid : image->numcomps [1,16384] */ @@ -1931,56 +1881,51 @@ OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2, } OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2, - opj_stream_private_t *stream, - opj_event_mgr_t * p_manager) + opj_stream_private_t *stream) { - return opj_j2k_encode(jp2->j2k, stream, p_manager); + return opj_j2k_encode(jp2->j2k, stream); } -OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager - ) +OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2, opj_stream_private_t *cio) { /* preconditions */ assert(jp2 != 00); + assert(jp2->manager != 00); assert(cio != 00); - assert(p_manager != 00); /* customization of the end encoding */ - if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) { + if (! opj_jp2_setup_end_header_reading(jp2, &(jp2->manager->event_mgr))) { return OPJ_FALSE; } /* write header */ - if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) { + if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio)) { return OPJ_FALSE; } - return opj_j2k_end_decompress(jp2->j2k, cio, p_manager); + return opj_j2k_end_decompress(jp2->j2k, cio); } OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager + opj_stream_private_t *cio ) { /* preconditions */ assert(jp2 != 00); + assert(jp2->manager != 00); assert(cio != 00); - assert(p_manager != 00); /* customization of the end encoding */ - if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) { + if (! opj_jp2_setup_end_header_writing(jp2, &(jp2->manager->event_mgr))) { return OPJ_FALSE; } - if (! opj_j2k_end_compress(jp2->j2k,cio,p_manager)) { + if (! opj_j2k_end_compress(jp2->j2k,cio)) { return OPJ_FALSE; } /* write header */ - return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager); + return opj_jp2_exec(jp2,jp2->m_procedure_list,cio); } static OPJ_BOOL opj_jp2_setup_end_header_writing (opj_jp2_t *jp2, opj_event_mgr_t * p_manager) @@ -2029,8 +1974,7 @@ static OPJ_BOOL opj_jp2_setup_end_header_reading (opj_jp2_t *jp2, opj_event_mgr_ } static OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager + opj_stream_private_t *cio ) { OPJ_BOOL l_is_valid = OPJ_TRUE; @@ -2039,7 +1983,6 @@ static OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2, /* preconditions */ assert(jp2 != 00); assert(cio != 00); - assert(p_manager != 00); /* JPEG2000 codec validation */ @@ -2083,8 +2026,7 @@ static OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2, } static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2, - opj_stream_private_t *stream, - opj_event_mgr_t * p_manager + opj_stream_private_t *stream ) { opj_jp2_box_t box; @@ -2098,38 +2040,38 @@ static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2, /* preconditions */ assert(stream != 00); assert(jp2 != 00); - assert(p_manager != 00); + assert(jp2->manager != 00); - l_current_data = (OPJ_BYTE*)opj_calloc(1,l_last_data_size); + l_current_data = (OPJ_BYTE*)opj_manager_calloc(jp2->manager, 1, l_last_data_size); if (l_current_data == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n"); return OPJ_FALSE; } - while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) { + while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,&(jp2->manager->event_mgr))) { /* is it the codestream box ? */ if (box.type == JP2_JP2C) { if (jp2->jp2_state & JP2_STATE_HEADER) { jp2->jp2_state |= JP2_STATE_CODESTREAM; - opj_free(l_current_data); + opj_manager_free(jp2->manager, l_current_data); return OPJ_TRUE; } else { - opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n"); - opj_free(l_current_data); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "bad placed jpeg codestream\n"); + opj_manager_free(jp2->manager, l_current_data); return OPJ_FALSE; } } else if (box.length == 0) { - opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n"); - opj_free(l_current_data); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Cannot handle box of undefined sizes\n"); + opj_manager_free(jp2->manager, l_current_data); return OPJ_FALSE; } /* testcase 1851.pdf.SIGSEGV.ce9.948 */ - else if (box.length < l_nb_bytes_read) { - opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length, box.type); - opj_free(l_current_data); + else if (box.length < l_nb_bytes_read) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "invalid box size %d (%x)\n", box.length, box.type); + opj_manager_free(jp2->manager, l_current_data); return OPJ_FALSE; } @@ -2139,16 +2081,16 @@ static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2, if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) { if (l_current_handler == 00) { - opj_event_msg(p_manager, EVT_WARNING, "Found a misplaced '%c%c%c%c' box outside jp2h box\n", (OPJ_BYTE)(box.type>>24), (OPJ_BYTE)(box.type>>16), (OPJ_BYTE)(box.type>>8), (OPJ_BYTE)(box.type>>0)); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "Found a misplaced '%c%c%c%c' box outside jp2h box\n", (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16), (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0)); if (jp2->jp2_state & JP2_STATE_HEADER) { /* read anyway, we already have jp2h */ l_current_handler = l_current_handler_misplaced; } else { - opj_event_msg(p_manager, EVT_WARNING, "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n", (OPJ_BYTE)(box.type>>24), (OPJ_BYTE)(box.type>>16), (OPJ_BYTE)(box.type>>8), (OPJ_BYTE)(box.type>>0)); + opj_event_msg(&(jp2->manager->event_mgr), EVT_WARNING, "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n", (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16), (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0)); jp2->jp2_state |= JP2_STATE_UNKNOWN; - if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) { - opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n"); - opj_free(l_current_data); + if (opj_stream_skip(stream, l_current_data_size, &(jp2->manager->event_mgr)) != l_current_data_size) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n"); + opj_manager_free(jp2->manager, l_current_data); return OPJ_FALSE; } continue; @@ -2156,54 +2098,54 @@ static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2, } if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) { /* do not even try to malloc if we can't read */ - opj_event_msg(p_manager, EVT_ERROR, "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n", box.length, (OPJ_BYTE)(box.type>>24), (OPJ_BYTE)(box.type>>16), (OPJ_BYTE)(box.type>>8), (OPJ_BYTE)(box.type>>0), l_current_data_size, (OPJ_UINT32)opj_stream_get_number_byte_left(stream)); - opj_free(l_current_data); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n", box.length, (OPJ_BYTE)(box.type>>24), (OPJ_BYTE)(box.type>>16), (OPJ_BYTE)(box.type>>8), (OPJ_BYTE)(box.type>>0), l_current_data_size, (OPJ_UINT32)opj_stream_get_number_byte_left(stream)); + opj_manager_free(jp2->manager, l_current_data); return OPJ_FALSE; } if (l_current_data_size > l_last_data_size) { - OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_data_size); + OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_manager_realloc(jp2->manager, l_current_data,l_current_data_size); if (!new_current_data) { - opj_free(l_current_data); - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 box\n"); + opj_manager_free(jp2->manager, l_current_data); + opj_event_msg(&(jp2->manager->event_mgr), 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_UINT32)opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager); + l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream,l_current_data,l_current_data_size,&(jp2->manager->event_mgr)); if (l_nb_bytes_read != l_current_data_size) { - opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n"); - opj_free(l_current_data); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n"); + opj_manager_free(jp2->manager, l_current_data); return OPJ_FALSE; } - if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) { - opj_free(l_current_data); + if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size)) { + opj_manager_free(jp2->manager, l_current_data); return OPJ_FALSE; } } else { - if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) { - opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format: first box must be JPEG 2000 signature box\n"); - opj_free(l_current_data); - return OPJ_FALSE; - } - if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) { - opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format: second box must be file type box\n"); - opj_free(l_current_data); - return OPJ_FALSE; - } + if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Malformed JP2 file format: first box must be JPEG 2000 signature box\n"); + opj_manager_free(jp2->manager, l_current_data); + return OPJ_FALSE; + } + if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Malformed JP2 file format: second box must be file type box\n"); + opj_manager_free(jp2->manager, l_current_data); + return OPJ_FALSE; + } jp2->jp2_state |= JP2_STATE_UNKNOWN; - if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) { - opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n"); - opj_free(l_current_data); + if (opj_stream_skip(stream,l_current_data_size,&(jp2->manager->event_mgr)) != l_current_data_size) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n"); + opj_manager_free(jp2->manager, l_current_data); return OPJ_FALSE; } } } - opj_free(l_current_data); + opj_manager_free(jp2->manager, l_current_data); return OPJ_TRUE; } @@ -2220,12 +2162,11 @@ static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2, */ static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2, opj_procedure_list_t * p_procedure_list, - opj_stream_private_t *stream, - opj_event_mgr_t * p_manager + opj_stream_private_t *stream ) { - OPJ_BOOL (** l_procedure) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00; + OPJ_BOOL (** l_procedure) (opj_jp2_t * jp2, opj_stream_private_t *) = 00; OPJ_BOOL l_result = OPJ_TRUE; OPJ_UINT32 l_nb_proc, i; @@ -2233,13 +2174,12 @@ static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2, assert(p_procedure_list != 00); assert(jp2 != 00); assert(stream != 00); - assert(p_manager != 00); l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); - l_procedure = (OPJ_BOOL (**) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); + l_procedure = (OPJ_BOOL (**) (opj_jp2_t * jp2, opj_stream_private_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); for (i=0;imanager->event_mgr))) { return OPJ_FALSE; } /* validation of the parameters codec */ - if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) { + if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream)) { return OPJ_FALSE; } /* customization of the encoding */ - if (! opj_jp2_setup_header_writing(jp2, p_manager)) { + if (! opj_jp2_setup_header_writing(jp2, &(jp2->manager->event_mgr))) { return OPJ_FALSE; } /* write header */ - if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) { + if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream)) { return OPJ_FALSE; } - return opj_j2k_start_compress(jp2->j2k,stream,p_image,p_manager); + return opj_j2k_start_compress(jp2->j2k,stream,p_image); } static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id) @@ -2326,8 +2264,7 @@ static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_i */ static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { @@ -2336,23 +2273,23 @@ static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2, /* preconditions */ assert(p_header_data != 00); assert(jp2 != 00); - assert(p_manager != 00); + assert(jp2->manager != 00); if (jp2->jp2_state != JP2_STATE_NONE) { - opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "The signature box must be the first box in the file.\n"); return OPJ_FALSE; } /* assure length of data is correct (4 -> magic number) */ if (p_header_size != 4) { - opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Error with JP signature Box size\n"); return OPJ_FALSE; } /* rearrange data */ opj_read_bytes(p_header_data,&l_magic_number,4); if (l_magic_number != 0x0d0a870a ) { - opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Error with JP Signature : bad magic number\n"); return OPJ_FALSE; } @@ -2373,8 +2310,7 @@ static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2, */ static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2, OPJ_BYTE * p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { OPJ_UINT32 i, l_remaining_bytes; @@ -2382,16 +2318,16 @@ static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2, /* preconditions */ assert(p_header_data != 00); assert(jp2 != 00); - assert(p_manager != 00); + assert(jp2->manager != 00); if (jp2->jp2_state != JP2_STATE_SIGNATURE) { - opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "The ftyp box must be the second box in the file.\n"); return OPJ_FALSE; } /* assure length of data is correct */ if (p_header_size < 8) { - opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Error with FTYP signature Box size\n"); return OPJ_FALSE; } @@ -2405,16 +2341,16 @@ static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2, /* the number of remaining bytes should be a multiple of 4 */ if ((l_remaining_bytes & 0x3) != 0) { - opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Error with FTYP signature Box size\n"); return OPJ_FALSE; } /* div by 4 */ jp2->numcl = l_remaining_bytes >> 2; if (jp2->numcl) { - jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32)); + jp2->cl = (OPJ_UINT32 *) opj_manager_calloc(jp2->manager, jp2->numcl, sizeof(OPJ_UINT32)); if (jp2->cl == 00) { - opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Not enough memory with FTYP Box\n"); return OPJ_FALSE; } } @@ -2430,36 +2366,30 @@ static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2, return OPJ_TRUE; } -static OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2, - opj_stream_private_t *stream, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2, opj_stream_private_t *stream) { /* preconditions */ assert(jp2 != 00); assert(stream != 00); - assert(p_manager != 00); jp2->j2k_codestream_offset = opj_stream_tell(stream); - if (opj_stream_skip(stream,8,p_manager) != 8) { + if (opj_stream_skip(stream,8,&(jp2->manager->event_mgr)) != 8) { return OPJ_FALSE; } return OPJ_TRUE; } -static OPJ_BOOL opj_jpip_skip_iptr( opj_jp2_t *jp2, - opj_stream_private_t *stream, - opj_event_mgr_t * p_manager ) +static OPJ_BOOL opj_jpip_skip_iptr(opj_jp2_t *jp2, opj_stream_private_t *stream) { /* preconditions */ assert(jp2 != 00); assert(stream != 00); - assert(p_manager != 00); jp2->jpip_iptr_offset = opj_stream_tell(stream); - if (opj_stream_skip(stream,24,p_manager) != 24) { + if (opj_stream_skip(stream,24,&(jp2->manager->event_mgr)) != 24) { return OPJ_FALSE; } @@ -2472,14 +2402,12 @@ static OPJ_BOOL opj_jpip_skip_iptr( opj_jp2_t *jp2, * @param p_header_data the data contained in the file header box. * @param jp2 the jpeg2000 file codec. * @param p_header_size the size of the data contained in the file header box. - * @param p_manager the user event manager. * * @return true if the JP2 Header box was successfully reconized. */ static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2, OPJ_BYTE *p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager + OPJ_UINT32 p_header_size ) { OPJ_UINT32 l_box_size=0, l_current_data_size = 0; @@ -2490,11 +2418,11 @@ static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2, /* preconditions */ assert(p_header_data != 00); assert(jp2 != 00); - assert(p_manager != 00); + assert(jp2->manager != 00); /* make sure the box is well placed */ if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) { - opj_event_msg(p_manager, EVT_ERROR, "The box must be the first box in the file.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "The box must be the first box in the file.\n"); return OPJ_FALSE; } @@ -2503,13 +2431,13 @@ static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2, /* iterate while remaining data */ while (p_header_size > 0) { - if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n"); + if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, &(jp2->manager->event_mgr))) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Stream error while reading JP2 Header box\n"); return OPJ_FALSE; } if (box.length > p_header_size) { - opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n"); return OPJ_FALSE; } @@ -2518,7 +2446,7 @@ static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2, p_header_data += l_box_size; if (l_current_handler != 00) { - if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) { + if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size)) { return OPJ_FALSE; } } @@ -2535,7 +2463,7 @@ static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2, } if (l_has_ihdr == 0) { - opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: no 'ihdr' box.\n"); + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Stream error while reading JP2 Header box: no 'ihdr' box.\n"); return OPJ_FALSE; } @@ -2616,39 +2544,37 @@ static OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box, OPJ_BOOL opj_jp2_read_header( opj_stream_private_t *p_stream, opj_jp2_t *jp2, - opj_image_t ** p_image, - opj_event_mgr_t * p_manager + opj_image_t ** p_image ) { /* preconditions */ assert(jp2 != 00); + assert(jp2->manager != 00); assert(p_stream != 00); - assert(p_manager != 00); /* customization of the validation */ - if (! opj_jp2_setup_decoding_validation (jp2, p_manager)) { + if (! opj_jp2_setup_decoding_validation (jp2, &(jp2->manager->event_mgr))) { return OPJ_FALSE; } /* customization of the encoding */ - if (! opj_jp2_setup_header_reading(jp2, p_manager)) { + if (! opj_jp2_setup_header_reading(jp2, &(jp2->manager->event_mgr))) { return OPJ_FALSE; } /* validation of the parameters codec */ - if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) { + if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream)) { return OPJ_FALSE; } /* read header */ - if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) { + if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream)) { return OPJ_FALSE; } return opj_j2k_read_header( p_stream, jp2->j2k, - p_image, - p_manager); + p_image); } static OPJ_BOOL opj_jp2_setup_encoding_validation (opj_jp2_t *jp2, opj_event_mgr_t * p_manager) @@ -2729,8 +2655,7 @@ OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2, 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_stream_private_t *p_stream ) { return opj_j2k_read_tile_header(p_jp2->j2k, @@ -2740,84 +2665,83 @@ OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2, p_tile_x1, p_tile_y1, p_nb_comps, p_go_on, - p_stream, - p_manager); + p_stream); } OPJ_BOOL opj_jp2_write_tile ( opj_jp2_t *p_jp2, 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_stream_private_t *p_stream ) { - return opj_j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager); + return opj_j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream); } OPJ_BOOL opj_jp2_decode_tile ( opj_jp2_t * p_jp2, 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_stream_private_t *p_stream ) { - return opj_j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager); + return opj_j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream); } void opj_jp2_destroy(opj_jp2_t *jp2) { if (jp2) { + opj_manager_t l_manager = jp2->manager; + /* destroy the J2K codec */ opj_j2k_destroy(jp2->j2k); jp2->j2k = 00; if (jp2->comps) { - opj_free(jp2->comps); + opj_manager_free(l_manager, jp2->comps); jp2->comps = 00; } if (jp2->cl) { - opj_free(jp2->cl); + opj_manager_free(l_manager, jp2->cl); jp2->cl = 00; } if (jp2->color.icc_profile_buf) { - opj_free(jp2->color.icc_profile_buf); + opj_manager_free(l_manager, jp2->color.icc_profile_buf); jp2->color.icc_profile_buf = 00; } if (jp2->color.jp2_cdef) { if (jp2->color.jp2_cdef->info) { - opj_free(jp2->color.jp2_cdef->info); + opj_manager_free(l_manager, jp2->color.jp2_cdef->info); jp2->color.jp2_cdef->info = NULL; } - opj_free(jp2->color.jp2_cdef); + opj_manager_free(l_manager, jp2->color.jp2_cdef); jp2->color.jp2_cdef = 00; } if (jp2->color.jp2_pclr) { if (jp2->color.jp2_pclr->cmap) { - opj_free(jp2->color.jp2_pclr->cmap); + opj_manager_free(l_manager, jp2->color.jp2_pclr->cmap); jp2->color.jp2_pclr->cmap = NULL; } if (jp2->color.jp2_pclr->channel_sign) { - opj_free(jp2->color.jp2_pclr->channel_sign); + opj_manager_free(l_manager, jp2->color.jp2_pclr->channel_sign); jp2->color.jp2_pclr->channel_sign = NULL; } if (jp2->color.jp2_pclr->channel_size) { - opj_free(jp2->color.jp2_pclr->channel_size); + opj_manager_free(l_manager, jp2->color.jp2_pclr->channel_size); jp2->color.jp2_pclr->channel_size = NULL; } if (jp2->color.jp2_pclr->entries) { - opj_free(jp2->color.jp2_pclr->entries); + opj_manager_free(l_manager, jp2->color.jp2_pclr->entries); jp2->color.jp2_pclr->entries = NULL; } - opj_free(jp2->color.jp2_pclr); + opj_manager_free(l_manager, jp2->color.jp2_pclr); jp2->color.jp2_pclr = 00; } @@ -2831,38 +2755,36 @@ void opj_jp2_destroy(opj_jp2_t *jp2) jp2->m_procedure_list = 00; } - opj_free(jp2); + opj_manager_free(l_manager, jp2); } } OPJ_BOOL opj_jp2_set_decode_area( opj_jp2_t *p_jp2, 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_INT32 p_end_x, OPJ_INT32 p_end_y ) { - return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager); + return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y); } OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2, opj_stream_private_t *p_stream, opj_image_t* p_image, - opj_event_mgr_t * p_manager, OPJ_UINT32 tile_index ) { if (!p_image) return OPJ_FALSE; - opj_event_msg(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n"); + opj_event_msg(&(p_jp2->manager->event_mgr), EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n"); - if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index) ){ - opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n"); + if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, tile_index) ){ + opj_event_msg(&(p_jp2->manager->event_mgr), EVT_ERROR, "Failed to decode the codestream in the JP2 file\n"); return OPJ_FALSE; } - if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) { + if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_jp2->manager)) { return OPJ_FALSE; } @@ -2883,14 +2805,14 @@ OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2, if(p_jp2->color.jp2_pclr) { /* Part 1, I.5.3.4: Either both or none : */ if( !p_jp2->color.jp2_pclr->cmap) - opj_jp2_free_pclr(&(p_jp2->color)); + opj_jp2_free_pclr(p_jp2->manager, &(p_jp2->color)); else - opj_jp2_apply_pclr(p_image, &(p_jp2->color)); + opj_jp2_apply_pclr(p_jp2->manager, p_image, &(p_jp2->color)); } /* Apply the color space if needed */ if(p_jp2->color.jp2_cdef) { - opj_jp2_apply_cdef(p_image, &(p_jp2->color), p_manager); + opj_jp2_apply_cdef(p_jp2, p_image, &(p_jp2->color)); } if(p_jp2->color.icc_profile_buf) { @@ -2906,44 +2828,52 @@ OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2, /* JP2 encoder interface */ /* ----------------------------------------------------------------------- */ -opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder) +opj_jp2_t* opj_jp2_create(opj_manager_t manager, OPJ_BOOL p_is_decoder) { - opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1,sizeof(opj_jp2_t)); - if (jp2) { + opj_jp2_t *jp2 = NULL; - /* create the J2K codec */ - if (! p_is_decoder) { - jp2->j2k = opj_j2k_create_compress(); - } - else { - jp2->j2k = opj_j2k_create_decompress(); - } + /* preconditions */ + assert(manager != NULL); - if (jp2->j2k == 00) { - opj_jp2_destroy(jp2); - return 00; - } + jp2 = (opj_jp2_t*)opj_manager_calloc(manager, 1, sizeof(opj_jp2_t)); + if (jp2 == NULL) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unable to allocate jp2 codec."); + return NULL; + } + jp2->manager = manager; - /* Color structure */ - jp2->color.icc_profile_buf = NULL; - jp2->color.icc_profile_len = 0; - jp2->color.jp2_cdef = NULL; - jp2->color.jp2_pclr = NULL; - jp2->color.jp2_has_colr = 0; + /* create the J2K codec */ + if (! p_is_decoder) { + jp2->j2k = opj_j2k_create_compress(manager); + } + else { + jp2->j2k = opj_j2k_create_decompress(manager); + } - /* validation list creation */ - jp2->m_validation_list = opj_procedure_list_create(); - if (! jp2->m_validation_list) { - opj_jp2_destroy(jp2); - return 00; - } + if (jp2->j2k == NULL) { + opj_jp2_destroy(jp2); + return NULL; + } - /* execution list creation */ - jp2->m_procedure_list = opj_procedure_list_create(); - if (! jp2->m_procedure_list) { - opj_jp2_destroy(jp2); - return 00; - } + /* Color structure */ + jp2->color.icc_profile_buf = NULL; + jp2->color.icc_profile_len = 0; + jp2->color.jp2_cdef = NULL; + jp2->color.jp2_pclr = NULL; + jp2->color.jp2_has_colr = 0; + + /* validation list creation */ + jp2->m_validation_list = opj_procedure_list_create(manager); + if (jp2->m_validation_list == NULL) { + opj_jp2_destroy(jp2); + return NULL; + } + + /* execution list creation */ + jp2->m_procedure_list = opj_procedure_list_create(manager); + if (jp2->m_procedure_list == NULL) { + opj_jp2_destroy(jp2); + return NULL; } return jp2; @@ -2970,18 +2900,16 @@ opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2) } OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2, - OPJ_UINT32 res_factor, - opj_event_mgr_t * p_manager) + OPJ_UINT32 res_factor) { - return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager); + return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor); } /* JPIP specific */ #ifdef USE_JPIP static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *cio ) { OPJ_OFF_T j2k_codestream_exit; OPJ_BYTE l_data_header [24]; @@ -2989,7 +2917,6 @@ static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2, /* preconditions */ assert(jp2 != 00); assert(cio != 00); - assert(p_manager != 00); assert(opj_stream_has_seek(cio)); j2k_codestream_exit = opj_stream_tell(cio); @@ -3003,18 +2930,18 @@ static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2, opj_write_double(l_data_header + 8 + 8, 0); /* length */ #endif - if (! opj_stream_seek(cio,jp2->jpip_iptr_offset,p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (! opj_stream_seek(cio,jp2->jpip_iptr_offset,&(jp2->manager->event_mgr))) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } - if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (opj_stream_write_data(cio,l_data_header,24,&(jp2->manager->event_mgr)) != 24) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } - if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (! opj_stream_seek(cio,j2k_codestream_exit,&(jp2->manager->event_mgr))) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } @@ -3022,8 +2949,7 @@ static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2, } static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *cio ) { OPJ_OFF_T j2k_codestream_exit; OPJ_BYTE l_data_header [24]; @@ -3031,7 +2957,6 @@ static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2, /* preconditions */ assert(jp2 != 00); assert(cio != 00); - assert(p_manager != 00); assert(opj_stream_has_seek(cio)); opj_write_bytes(l_data_header, 24, 4); /* size of iptr */ @@ -3039,14 +2964,14 @@ static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2, opj_write_double(l_data_header + 4 + 4, 0); /* offset */ opj_write_double(l_data_header + 8 + 8, 0); /* length */ - if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (opj_stream_write_data(cio,l_data_header,24,&(jp2->manager->event_mgr)) != 24) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } j2k_codestream_exit = opj_stream_tell(cio); - if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (! opj_stream_seek(cio,j2k_codestream_exit,&(jp2->manager->event_mgr))) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } @@ -3054,8 +2979,7 @@ static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2, } static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager ) + opj_stream_private_t *cio ) { OPJ_OFF_T j2k_codestream_exit; OPJ_BYTE l_data_header [24]; @@ -3063,7 +2987,6 @@ static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2, /* preconditions */ assert(jp2 != 00); assert(cio != 00); - assert(p_manager != 00); assert(opj_stream_has_seek(cio)); j2k_codestream_exit = opj_stream_tell(cio); @@ -3077,19 +3000,19 @@ static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2, opj_write_double(l_data_header + 8 + 8, 0); /* length */ #endif - if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (! opj_stream_seek(cio,j2k_codestream_exit,&(jp2->manager->event_mgr))) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } - if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (opj_stream_write_data(cio,l_data_header,24,&(jp2->manager->event_mgr)) != 24) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } j2k_codestream_exit = opj_stream_tell(cio); - if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + if (! opj_stream_seek(cio,j2k_codestream_exit,&(jp2->manager->event_mgr))) { + opj_event_msg(&(jp2->manager->event_mgr), EVT_ERROR, "Failed to seek in the stream.\n"); return OPJ_FALSE; } diff --git a/src/lib/openjp2/jp2.h b/src/lib/openjp2/jp2.h index 914c47f9..f8add723 100644 --- a/src/lib/openjp2/jp2.h +++ b/src/lib/openjp2/jp2.h @@ -155,6 +155,8 @@ JPEG-2000 file format reader/writer */ typedef struct opj_jp2 { + /** handle to the memory manager */ + opj_manager_t manager; /** handle to the J2K codec */ opj_j2k_t *j2k; /** list of validation procedures */ @@ -186,15 +188,15 @@ typedef struct opj_jp2 as codec writers will need to extand those fields as new part of the standard are implemented. */ - OPJ_OFF_T j2k_codestream_offset; - OPJ_OFF_T jpip_iptr_offset; - OPJ_BOOL jpip_on; + OPJ_OFF_T j2k_codestream_offset; + OPJ_OFF_T jpip_iptr_offset; + OPJ_BOOL jpip_on; OPJ_UINT32 jp2_state; OPJ_UINT32 jp2_img_state; opj_jp2_color_t color; - - OPJ_BOOL ignore_pclr_cmap_cdef; + + OPJ_BOOL ignore_pclr_cmap_cdef; } opj_jp2_t; @@ -214,8 +216,7 @@ typedef struct opj_jp2_header_handler /* action linked to the marker */ OPJ_BOOL (*handler) ( opj_jp2_t *jp2, OPJ_BYTE *p_header_data, - OPJ_UINT32 p_header_size, - opj_event_mgr_t * p_manager); + OPJ_UINT32 p_header_size); } opj_jp2_header_handler_t; @@ -248,14 +249,12 @@ void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters); * @param jp2 JP2 decompressor handle * @param p_stream FIXME DOC * @param p_image FIXME DOC - * @param p_manager FIXME DOC * * @return Returns a decoded image if successful, returns NULL otherwise */ OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2, opj_stream_private_t *p_stream, - opj_image_t* p_image, - opj_event_mgr_t * p_manager); + opj_image_t* p_image); /** * Setup the encoder parameters using the current image and using user parameters. @@ -264,24 +263,20 @@ OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2, * @param jp2 JP2 compressor handle * @param parameters compression parameters * @param image input filled image - * @param p_manager FIXME DOC * @return OPJ_TRUE if successful, OPJ_FALSE otherwise */ OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2, opj_cparameters_t *parameters, - opj_image_t *image, - opj_event_mgr_t * p_manager); + opj_image_t *image); /** Encode an image into a JPEG-2000 file stream @param jp2 JP2 compressor handle @param stream Output buffer stream -@param p_manager event manager @return Returns true if successful, returns false otherwise */ OPJ_BOOL opj_jp2_encode( opj_jp2_t *jp2, - opj_stream_private_t *stream, - opj_event_mgr_t * p_manager); + opj_stream_private_t *stream); /** @@ -290,14 +285,12 @@ OPJ_BOOL opj_jp2_encode( opj_jp2_t *jp2, * @param jp2 the jpeg2000 file codec. * @param stream the stream object. * @param p_image FIXME DOC - * @param p_manager FIXME DOC * * @return true if the codec is valid. */ OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2, opj_stream_private_t *stream, - opj_image_t * p_image, - opj_event_mgr_t * p_manager); + opj_image_t * p_image); /** @@ -305,8 +298,7 @@ OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2, * codestream. */ OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager); + opj_stream_private_t *cio); /* ----------------------------------------------------------------------- */ @@ -315,8 +307,7 @@ OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2, * codestream. */ OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2, - opj_stream_private_t *cio, - opj_event_mgr_t * p_manager); + opj_stream_private_t *cio); /** * Reads a jpeg2000 file header structure. @@ -324,14 +315,12 @@ OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2, * @param p_stream the stream to read data from. * @param jp2 the jpeg2000 file header structure. * @param p_image FIXME DOC - * @param p_manager the user event manager. * * @return true if the box is valid. */ OPJ_BOOL opj_jp2_read_header( opj_stream_private_t *p_stream, opj_jp2_t *jp2, - opj_image_t ** p_image, - opj_event_mgr_t * p_manager ); + opj_image_t ** p_image ); /** * Reads a tile header. @@ -345,7 +334,6 @@ OPJ_BOOL opj_jp2_read_header( opj_stream_private_t *p_stream, * @param p_nb_comps FIXME DOC * @param p_go_on FIXME DOC * @param p_stream the stream to write data to. - * @param p_manager the user event manager. */ OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2, OPJ_UINT32 * p_tile_index, @@ -356,8 +344,7 @@ OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2, 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_stream_private_t *p_stream ); /** * Writes a tile. @@ -367,14 +354,12 @@ OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2, * @param p_data FIXME DOC * @param p_data_size FIXME DOC * @param p_stream the stream to write data to. - * @param p_manager the user event manager. */ OPJ_BOOL opj_jp2_write_tile ( opj_jp2_t *p_jp2, 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_stream_private_t *p_stream ); /** * Decode tile data. @@ -383,7 +368,6 @@ OPJ_BOOL opj_jp2_write_tile ( opj_jp2_t *p_jp2, * @param p_data FIXME DOC * @param p_data_size FIXME DOC * @param p_stream the stream to write data to. - * @param p_manager the user event manager. * * @return FIXME DOC */ @@ -391,15 +375,14 @@ OPJ_BOOL opj_jp2_decode_tile ( opj_jp2_t * p_jp2, 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_stream_private_t *p_stream ); /** * Creates a jpeg2000 file decompressor. * * @return an empty jpeg2000 file codec. */ -opj_jp2_t* opj_jp2_create (OPJ_BOOL p_is_decoder); +opj_jp2_t* opj_jp2_create (opj_manager_t manager, OPJ_BOOL p_is_decoder); /** Destroy a JP2 decompressor handle @@ -417,15 +400,14 @@ void opj_jp2_destroy(opj_jp2_t *jp2); * @param p_start_y the up position of the rectangle to decode (in image coordinates). * @param p_end_x the right position of the rectangle to decode (in image coordinates). * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). - * @param p_manager the user event manager + * * @return true if the area could be set. */ OPJ_BOOL opj_jp2_set_decode_area( opj_jp2_t *p_jp2, 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_INT32 p_end_x, OPJ_INT32 p_end_y ); /** * @@ -433,7 +415,6 @@ OPJ_BOOL opj_jp2_set_decode_area( opj_jp2_t *p_jp2, OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2, opj_stream_private_t *p_stream, opj_image_t* p_image, - opj_event_mgr_t * p_manager, OPJ_UINT32 tile_index ); @@ -441,8 +422,7 @@ OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2, * */ OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2, - OPJ_UINT32 res_factor, - opj_event_mgr_t * p_manager); + OPJ_UINT32 res_factor); /* TODO MSD: clean these 3 functions */ diff --git a/src/lib/openjp2/mct.c b/src/lib/openjp2/mct.c index a0da0996..ceacc5b5 100644 --- a/src/lib/openjp2/mct.c +++ b/src/lib/openjp2/mct.c @@ -117,10 +117,8 @@ void opj_mct_encode( OPJ_INT32* restrict c2, OPJ_UINT32 n) { - OPJ_SIZE_T i; - const OPJ_SIZE_T len = n; - - for(i = 0; i < len; ++i) { + OPJ_UINT32 i; + for(i = 0; i < n; ++i) { OPJ_INT32 r = c0[i]; OPJ_INT32 g = c1[i]; OPJ_INT32 b = c2[i]; @@ -206,10 +204,10 @@ OPJ_FLOAT64 opj_mct_getnorm(OPJ_UINT32 compno) { /* */ #ifdef __SSE4_1__ void opj_mct_encode_real( - OPJ_INT32* restrict c0, - OPJ_INT32* restrict c1, - OPJ_INT32* restrict c2, - OPJ_UINT32 n) + OPJ_INT32* restrict c0, + OPJ_INT32* restrict c1, + OPJ_INT32* restrict c2, + OPJ_UINT32 n) { OPJ_SIZE_T i; const OPJ_SIZE_T len = n; @@ -297,11 +295,11 @@ void opj_mct_encode_real( hi = _mm_slli_epi64(hi, 32-13); u = _mm_sub_epi32(u, _mm_blend_epi16(lo, hi, 0xCC)); _mm_store_si128((__m128i *)&(c1[i]), u); - + /*lo = r; - hi = _mm_shuffle_epi32(r, _MM_SHUFFLE(3, 3, 1, 1)); - lo = _mm_mul_epi32(lo, mulround); - hi = _mm_mul_epi32(hi, mulround);*/ + hi = _mm_shuffle_epi32(r, _MM_SHUFFLE(3, 3, 1, 1)); + lo = _mm_mul_epi32(lo, mulround); + hi = _mm_mul_epi32(hi, mulround);*/ lo = _mm_cvtepi32_epi64(_mm_shuffle_epi32(r, _MM_SHUFFLE(3, 2, 2, 0))); hi = _mm_cvtepi32_epi64(_mm_shuffle_epi32(r, _MM_SHUFFLE(3, 2, 3, 1))); lo = _mm_slli_epi64(lo, 12); @@ -437,6 +435,7 @@ OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno) { OPJ_BOOL opj_mct_encode_custom( + opj_manager_t manager, OPJ_BYTE * pCodingdata, OPJ_UINT32 n, OPJ_BYTE ** pData, @@ -454,9 +453,13 @@ OPJ_BOOL opj_mct_encode_custom( OPJ_UINT32 lMultiplicator = 1 << 13; OPJ_INT32 * lMctPtr; - OPJ_ARG_NOT_USED(isSigned); + OPJ_ARG_NOT_USED(isSigned); - lCurrentData = (OPJ_INT32 *) opj_malloc((pNbComp + lNbMatCoeff) * sizeof(OPJ_INT32)); + if (manager == NULL) { + return OPJ_FALSE; + } + + lCurrentData = (OPJ_INT32 *) opj_manager_malloc(manager, (pNbComp + lNbMatCoeff) * sizeof(OPJ_INT32)); if (! lCurrentData) { return OPJ_FALSE; } @@ -484,12 +487,13 @@ OPJ_BOOL opj_mct_encode_custom( } } - opj_free(lCurrentData); + opj_manager_free(manager, lCurrentData); return OPJ_TRUE; } OPJ_BOOL opj_mct_decode_custom( + opj_manager_t manager, OPJ_BYTE * pDecodingData, OPJ_UINT32 n, OPJ_BYTE ** pData, @@ -507,7 +511,11 @@ OPJ_BOOL opj_mct_decode_custom( OPJ_ARG_NOT_USED(isSigned); - lCurrentData = (OPJ_FLOAT32 *) opj_malloc (2 * pNbComp * sizeof(OPJ_FLOAT32)); + if (manager == NULL) { + return OPJ_FALSE; + } + + lCurrentData = (OPJ_FLOAT32 *) opj_manager_malloc(manager, 2 * pNbComp * sizeof(OPJ_FLOAT32)); if (! lCurrentData) { return OPJ_FALSE; } @@ -526,7 +534,7 @@ OPJ_BOOL opj_mct_decode_custom( *(lData[j]++) = (OPJ_FLOAT32) (lCurrentResult[j]); } } - opj_free(lCurrentData); + opj_manager_free(manager, lCurrentData); return OPJ_TRUE; } diff --git a/src/lib/openjp2/mct.h b/src/lib/openjp2/mct.h index 1c1f4d0c..573d45fe 100644 --- a/src/lib/openjp2/mct.h +++ b/src/lib/openjp2/mct.h @@ -101,6 +101,7 @@ OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno); /** FIXME DOC +@param manager User memory/event manager @param p_coding_data MCT data @param n size of components @param p_data components @@ -109,6 +110,7 @@ FIXME DOC @return OPJ_FALSE if function encounter a problem, OPJ_TRUE otherwise */ OPJ_BOOL opj_mct_encode_custom( + opj_manager_t manager, OPJ_BYTE * p_coding_data, OPJ_UINT32 n, OPJ_BYTE ** p_data, @@ -116,6 +118,7 @@ OPJ_BOOL opj_mct_encode_custom( OPJ_UINT32 is_signed); /** FIXME DOC +@param manager User memory/event manager @param pDecodingData MCT data @param n size of components @param pData components @@ -124,6 +127,7 @@ FIXME DOC @return OPJ_FALSE if function encounter a problem, OPJ_TRUE otherwise */ OPJ_BOOL opj_mct_decode_custom( + opj_manager_t manager, OPJ_BYTE * pDecodingData, OPJ_UINT32 n, OPJ_BYTE ** pData, diff --git a/src/lib/openjp2/mqc.c b/src/lib/openjp2/mqc.c index 7e0f5637..dfb33bf0 100644 --- a/src/lib/openjp2/mqc.c +++ b/src/lib/openjp2/mqc.c @@ -359,8 +359,8 @@ static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc) { ========================================================== */ -opj_mqc_t* opj_mqc_create(void) { - opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t)); +opj_mqc_t* opj_mqc_create(opj_manager_t manager) { + opj_mqc_t *mqc = (opj_mqc_t*)opj_manager_malloc(manager, sizeof(opj_mqc_t)); #ifdef MQC_PERF_OPT if (mqc) { mqc->buffer = NULL; @@ -369,14 +369,14 @@ opj_mqc_t* opj_mqc_create(void) { return mqc; } -void opj_mqc_destroy(opj_mqc_t *mqc) { +void opj_mqc_destroy(opj_manager_t manager, opj_mqc_t *mqc) { if(mqc) { #ifdef MQC_PERF_OPT if (mqc->buffer) { - opj_free(mqc->buffer); + opj_manager_free(manager, mqc->buffer); } #endif - opj_free(mqc); + opj_manager_free(manager, mqc); } } diff --git a/src/lib/openjp2/mqc.h b/src/lib/openjp2/mqc.h index 69a2d460..a4b31a1e 100644 --- a/src/lib/openjp2/mqc.h +++ b/src/lib/openjp2/mqc.h @@ -89,12 +89,12 @@ typedef struct opj_mqc { Create a new MQC handle @return Returns a new MQC handle if successful, returns NULL otherwise */ -opj_mqc_t* opj_mqc_create(void); +opj_mqc_t* opj_mqc_create(opj_manager_t manager); /** Destroy a previously created MQC handle @param mqc MQC handle to destroy */ -void opj_mqc_destroy(opj_mqc_t *mqc); +void opj_mqc_destroy(opj_manager_t manager, opj_mqc_t *mqc); /** Return the number of bytes written/read since initialisation @param mqc MQC handle diff --git a/src/lib/openjp2/openjpeg.c b/src/lib/openjp2/openjpeg.c index 5114cc10..013aa6a3 100644 --- a/src/lib/openjp2/openjpeg.c +++ b/src/lib/openjp2/openjpeg.c @@ -49,11 +49,11 @@ OPJ_BOOL OPJ_CALLCONV opj_set_info_handler( opj_codec_t * p_codec, if(! l_codec){ return OPJ_FALSE; } + if (! l_codec->manager_is_private) { + return OPJ_FALSE; + } - l_codec->m_event_mgr.info_handler = p_callback; - l_codec->m_event_mgr.m_info_data = p_user_data; - - return OPJ_TRUE; + return opj_manager_set_info_handler(l_codec->manager, p_callback, p_user_data); } OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler( opj_codec_t * p_codec, @@ -65,10 +65,11 @@ OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler( opj_codec_t * p_codec, return OPJ_FALSE; } - l_codec->m_event_mgr.warning_handler = p_callback; - l_codec->m_event_mgr.m_warning_data = p_user_data; + if (! l_codec->manager_is_private) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return opj_manager_set_warning_handler(l_codec->manager, p_callback, p_user_data); } OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, @@ -80,10 +81,11 @@ OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, return OPJ_FALSE; } - l_codec->m_event_mgr.error_handler = p_callback; - l_codec->m_event_mgr.m_error_data = p_user_data; + if (! l_codec->manager_is_private) { + return OPJ_FALSE; + } - return OPJ_TRUE; + return opj_manager_set_error_handler(l_codec->manager, p_callback, p_user_data); } /* ---------------------------------------------------------------------- */ @@ -162,14 +164,38 @@ const char* OPJ_CALLCONV opj_version(void) { /* DECOMPRESSION FUNCTIONS*/ opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) +{ + opj_codec_private_t* l_codec = NULL; + opj_manager_t l_manager = opj_manager_create_default(); + + if (l_manager == NULL) { + return NULL; + } + + l_codec = (opj_codec_private_t*)opj_manager_create_decompress(l_manager, p_format); + if (l_codec == NULL) { + opj_manager_destroy(l_manager); + return NULL; + } + l_codec->manager_is_private = OPJ_TRUE; + return (opj_codec_t*)l_codec; +} + +OPJ_API opj_codec_t* OPJ_CALLCONV opj_manager_create_decompress(opj_manager_t manager, OPJ_CODEC_FORMAT p_format) { opj_codec_private_t *l_codec = 00; - l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t)); + if (manager == NULL) { + return NULL; + } + + l_codec = (opj_codec_private_t*) opj_manager_calloc(manager, 1, sizeof(opj_codec_private_t)); if (!l_codec){ + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Not enough memory to allocate codec abstraction."); return 00; } + l_codec->manager = manager; l_codec->is_decompressor = 1; switch (p_format) { @@ -183,18 +209,16 @@ opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) l_codec->m_codec_data.m_decompression.opj_decode = (OPJ_BOOL (*) ( void *, struct opj_stream_private *, - opj_image_t*, struct opj_event_mgr * )) opj_j2k_decode; + opj_image_t*)) opj_j2k_decode; l_codec->m_codec_data.m_decompression.opj_end_decompress = (OPJ_BOOL (*) ( void *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_j2k_end_decompress; + struct opj_stream_private *)) opj_j2k_end_decompress; l_codec->m_codec_data.m_decompression.opj_read_header = (OPJ_BOOL (*) ( struct opj_stream_private *, void *, - opj_image_t **, - struct opj_event_mgr * )) opj_j2k_read_header; + opj_image_t **)) opj_j2k_read_header; l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))opj_j2k_destroy; @@ -210,39 +234,35 @@ opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) OPJ_INT32*, OPJ_INT32*, OPJ_UINT32*, OPJ_BOOL*, - struct opj_stream_private *, - struct opj_event_mgr * )) opj_j2k_read_tile_header; + struct opj_stream_private *)) opj_j2k_read_tile_header; l_codec->m_codec_data.m_decompression.opj_decode_tile_data = (OPJ_BOOL (*) ( void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_j2k_decode_tile; + struct opj_stream_private *)) opj_j2k_decode_tile; l_codec->m_codec_data.m_decompression.opj_set_decode_area = (OPJ_BOOL (*) ( void *, opj_image_t*, - OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, - struct opj_event_mgr *)) opj_j2k_set_decode_area; + OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32)) opj_j2k_set_decode_area; l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (OPJ_BOOL (*) ( void *p_codec, opj_stream_private_t *p_cio, opj_image_t *p_image, - struct opj_event_mgr * p_manager, OPJ_UINT32 tile_index)) opj_j2k_get_tile; l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (OPJ_BOOL (*) ( void * p_codec, - OPJ_UINT32 res_factor, - struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor; + OPJ_UINT32 res_factor)) opj_j2k_set_decoded_resolution_factor; - l_codec->m_codec = opj_j2k_create_decompress(); + l_codec->m_codec = opj_j2k_create_decompress(manager); if (! l_codec->m_codec) { - opj_free(l_codec); + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Not enough memory to allocate j2k codec."); + opj_manager_free(manager,l_codec); return NULL; } @@ -259,19 +279,16 @@ opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) l_codec->m_codec_data.m_decompression.opj_decode = (OPJ_BOOL (*) ( void *, struct opj_stream_private *, - opj_image_t*, - struct opj_event_mgr * )) opj_jp2_decode; + opj_image_t* )) opj_jp2_decode; l_codec->m_codec_data.m_decompression.opj_end_decompress = (OPJ_BOOL (*) ( void *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_jp2_end_decompress; + struct opj_stream_private * )) opj_jp2_end_decompress; l_codec->m_codec_data.m_decompression.opj_read_header = (OPJ_BOOL (*) ( struct opj_stream_private *, void *, - opj_image_t **, - struct opj_event_mgr * )) opj_jp2_read_header; + opj_image_t ** )) opj_jp2_read_header; l_codec->m_codec_data.m_decompression.opj_read_tile_header = (OPJ_BOOL (*) ( void *, @@ -283,14 +300,12 @@ opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) OPJ_INT32 * , OPJ_UINT32 * , OPJ_BOOL *, - struct opj_stream_private *, - struct opj_event_mgr * )) opj_jp2_read_tile_header; + struct opj_stream_private * )) opj_jp2_read_tile_header; l_codec->m_codec_data.m_decompression.opj_decode_tile_data = (OPJ_BOOL (*) ( void *, OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32, - struct opj_stream_private *, - struct opj_event_mgr * )) opj_jp2_decode_tile; + struct opj_stream_private * )) opj_jp2_decode_tile; l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))opj_jp2_destroy; @@ -300,25 +315,23 @@ opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) l_codec->m_codec_data.m_decompression.opj_set_decode_area = (OPJ_BOOL (*) ( void *, opj_image_t*, - OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, - struct opj_event_mgr * )) opj_jp2_set_decode_area; + OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32 )) opj_jp2_set_decode_area; l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (OPJ_BOOL (*) ( void *p_codec, opj_stream_private_t *p_cio, opj_image_t *p_image, - struct opj_event_mgr * p_manager, OPJ_UINT32 tile_index)) opj_jp2_get_tile; l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (OPJ_BOOL (*) ( void * p_codec, - OPJ_UINT32 res_factor, - opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor; + OPJ_UINT32 res_factor)) opj_jp2_set_decoded_resolution_factor; - l_codec->m_codec = opj_jp2_create(OPJ_TRUE); + l_codec->m_codec = opj_jp2_create(manager, OPJ_TRUE); if (! l_codec->m_codec) { - opj_free(l_codec); + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Not enough memory to allocate jp2 codec."); + opj_manager_free(manager, l_codec); return 00; } @@ -326,11 +339,11 @@ opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) case OPJ_CODEC_UNKNOWN: case OPJ_CODEC_JPT: default: - opj_free(l_codec); + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unknown codec."); + opj_manager_free(manager,l_codec); return 00; } - opj_set_default_event_handler(&(l_codec->m_event_mgr)); return (opj_codec_t*) l_codec; } @@ -362,7 +375,7 @@ OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec, opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; if (! l_codec->is_decompressor) { - opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, + opj_event_msg(&(l_codec->manager->event_mgr), EVT_ERROR, "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n"); return OPJ_FALSE; } @@ -383,15 +396,14 @@ OPJ_BOOL OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream, opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; if(! l_codec->is_decompressor) { - opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, + opj_event_msg(&(l_codec->manager->event_mgr), EVT_ERROR, "Codec provided to the opj_read_header function is not a decompressor handler.\n"); return OPJ_FALSE; } return l_codec->m_codec_data.m_decompression.opj_read_header( l_stream, l_codec->m_codec, - p_image, - &(l_codec->m_event_mgr) ); + p_image ); } return OPJ_FALSE; @@ -411,8 +423,7 @@ OPJ_BOOL OPJ_CALLCONV opj_decode( opj_codec_t *p_codec, return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec, l_stream, - p_image, - &(l_codec->m_event_mgr) ); + p_image); } return OPJ_FALSE; @@ -434,8 +445,7 @@ OPJ_BOOL OPJ_CALLCONV opj_set_decode_area( opj_codec_t *p_codec, return l_codec->m_codec_data.m_decompression.opj_set_decode_area( l_codec->m_codec, p_image, p_start_x, p_start_y, - p_end_x, p_end_y, - &(l_codec->m_event_mgr) ); + p_end_x, p_end_y ); } return OPJ_FALSE; } @@ -464,8 +474,7 @@ OPJ_BOOL OPJ_CALLCONV opj_read_tile_header( opj_codec_t *p_codec, p_tile_x1, p_tile_y1, p_nb_comps, p_should_go_on, - l_stream, - &(l_codec->m_event_mgr)); + l_stream); } return OPJ_FALSE; } @@ -489,8 +498,7 @@ OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data( opj_codec_t *p_codec, p_tile_index, p_data, p_data_size, - l_stream, - &(l_codec->m_event_mgr) ); + l_stream ); } return OPJ_FALSE; } @@ -511,7 +519,6 @@ OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile( opj_codec_t *p_codec, return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile( l_codec->m_codec, l_stream, p_image, - &(l_codec->m_event_mgr), tile_index); } @@ -528,57 +535,73 @@ OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, } return l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, - res_factor, - &(l_codec->m_event_mgr) ); + res_factor ); } /* ---------------------------------------------------------------------- */ /* COMPRESSION FUNCTIONS*/ opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format) +{ + opj_codec_private_t* l_codec = NULL; + opj_manager_t l_manager = opj_manager_create_default(); + + if (l_manager == NULL) { + return NULL; + } + + l_codec = (opj_codec_private_t*)opj_manager_create_compress(l_manager, p_format); + if (l_codec == NULL) { + opj_manager_destroy(l_manager); + return NULL; + } + l_codec->manager_is_private = OPJ_TRUE; + return (opj_codec_t*)l_codec; +} + +OPJ_API opj_codec_t* OPJ_CALLCONV opj_manager_create_compress(opj_manager_t manager, OPJ_CODEC_FORMAT p_format) { opj_codec_private_t *l_codec = 00; - l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t)); - if (!l_codec) { + l_codec = (opj_codec_private_t*) opj_manager_calloc(manager, 1, sizeof(opj_codec_private_t)); + if (!l_codec){ + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Not enough memory to allocate codec abstraction."); return 00; } + l_codec->manager = manager; l_codec->is_decompressor = 0; switch(p_format) { case OPJ_CODEC_J2K: l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *, - struct opj_stream_private *, - struct opj_event_mgr * )) opj_j2k_encode; + struct opj_stream_private * )) opj_j2k_encode; l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) ( void *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_j2k_end_compress; + struct opj_stream_private * )) opj_j2k_end_compress; l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *, struct opj_stream_private *, - struct opj_image * , - struct opj_event_mgr *)) opj_j2k_start_compress; + struct opj_image * )) opj_j2k_start_compress; l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, - struct opj_stream_private *, - struct opj_event_mgr *) ) opj_j2k_write_tile; + struct opj_stream_private * ) ) opj_j2k_write_tile; l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_j2k_destroy; l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL (*) ( void *, opj_cparameters_t *, - struct opj_image *, - struct opj_event_mgr * )) opj_j2k_setup_encoder; + struct opj_image * )) opj_j2k_setup_encoder; + + l_codec->m_codec = opj_j2k_create_compress(manager); - l_codec->m_codec = opj_j2k_create_compress(); if (! l_codec->m_codec) { - opj_free(l_codec); - return 00; + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Not enough memory to allocate j2k codec."); + opj_manager_free(manager,l_codec); + return NULL; } break; @@ -586,36 +609,33 @@ opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format) case OPJ_CODEC_JP2: /* get a JP2 decoder handle */ l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *, - struct opj_stream_private *, - struct opj_event_mgr * )) opj_jp2_encode; + struct opj_stream_private * )) opj_jp2_encode; l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) ( void *, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_jp2_end_compress; + struct opj_stream_private * )) opj_jp2_end_compress; l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *, struct opj_stream_private *, - struct opj_image * , - struct opj_event_mgr *)) opj_jp2_start_compress; + struct opj_image * )) opj_jp2_start_compress; l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, - struct opj_stream_private *, - struct opj_event_mgr *)) opj_jp2_write_tile; + struct opj_stream_private * )) opj_jp2_write_tile; l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_jp2_destroy; l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL (*) ( void *, opj_cparameters_t *, - struct opj_image *, - struct opj_event_mgr * )) opj_jp2_setup_encoder; + struct opj_image * )) opj_jp2_setup_encoder; + + l_codec->m_codec = opj_jp2_create(manager, OPJ_FALSE); - l_codec->m_codec = opj_jp2_create(OPJ_FALSE); if (! l_codec->m_codec) { - opj_free(l_codec); - return 00; + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Not enough memory to allocate jp2 codec."); + opj_manager_free(manager,l_codec); + return NULL; } break; @@ -623,11 +643,11 @@ opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format) case OPJ_CODEC_UNKNOWN: case OPJ_CODEC_JPT: default: - opj_free(l_codec); + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Unknown codec."); + opj_manager_free(manager,l_codec); return 00; } - opj_set_default_event_handler(&(l_codec->m_event_mgr)); return (opj_codec_t*) l_codec; } @@ -700,8 +720,7 @@ OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec, if (! l_codec->is_decompressor) { return l_codec->m_codec_data.m_compression.opj_setup_encoder( l_codec->m_codec, parameters, - p_image, - &(l_codec->m_event_mgr) ); + p_image ); } } @@ -719,8 +738,7 @@ OPJ_BOOL OPJ_CALLCONV opj_start_compress ( opj_codec_t *p_codec, if (! l_codec->is_decompressor) { return l_codec->m_codec_data.m_compression.opj_start_compress( l_codec->m_codec, l_stream, - p_image, - &(l_codec->m_event_mgr)); + p_image); } } @@ -735,8 +753,7 @@ OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream) if (! l_codec->is_decompressor) { return l_codec->m_codec_data.m_compression.opj_encode( l_codec->m_codec, - l_stream, - &(l_codec->m_event_mgr)); + l_stream); } } @@ -753,8 +770,7 @@ OPJ_BOOL OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec, if (! l_codec->is_decompressor) { return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec, - l_stream, - &(l_codec->m_event_mgr)); + l_stream); } } return OPJ_FALSE; @@ -773,8 +789,7 @@ OPJ_BOOL OPJ_CALLCONV opj_end_decompress ( opj_codec_t *p_codec, } return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec, - l_stream, - &(l_codec->m_event_mgr) ); + l_stream); } return OPJ_FALSE; @@ -783,27 +798,40 @@ OPJ_BOOL OPJ_CALLCONV opj_end_decompress ( opj_codec_t *p_codec, OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters, OPJ_FLOAT32 * pEncodingMatrix, OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp) +{ + opj_manager_t l_manager = opj_manager_get_global_manager(); + return opj_manager_set_MCT(l_manager, parameters, pEncodingMatrix, p_dc_shift, pNbComp); +} + +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_manager_set_MCT(opj_manager_t manager, + opj_cparameters_t *parameters, + OPJ_FLOAT32 * pEncodingMatrix, + OPJ_INT32 * p_dc_shift, + OPJ_UINT32 pNbComp) { OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32); OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32); OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size; + if (manager == NULL) { + return OPJ_FALSE; + } /* add MCT capability */ - if (OPJ_IS_PART2(parameters->rsiz)) { - parameters->rsiz |= OPJ_EXTENSION_MCT; - } else { - parameters->rsiz = ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT)); - } + if (OPJ_IS_PART2(parameters->rsiz)) { + parameters->rsiz |= OPJ_EXTENSION_MCT; + } else { + parameters->rsiz = ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT)); + } parameters->irreversible = 1; /* use array based MCT */ parameters->tcp_mct = 2; - parameters->mct_data = opj_malloc(l_mct_total_size); + parameters->mct_data = opj_manager_malloc(manager, l_mct_total_size); if (! parameters->mct_data) { return OPJ_FALSE; } - memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size); + memcpy(parameters->mct_data, pEncodingMatrix, l_matrix_size); memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size,p_dc_shift,l_dc_shift_size); return OPJ_TRUE; @@ -827,8 +855,7 @@ OPJ_BOOL OPJ_CALLCONV opj_write_tile ( opj_codec_t *p_codec, p_tile_index, p_data, p_data_size, - l_stream, - &(l_codec->m_event_mgr) ); + l_stream ); } return OPJ_FALSE; @@ -840,6 +867,8 @@ void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec) { if (p_codec) { opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_manager_t l_manager = l_codec->manager; + OPJ_BOOL l_manager_is_private = l_codec->manager_is_private; if (l_codec->is_decompressor) { l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec); @@ -849,7 +878,11 @@ void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec) } l_codec->m_codec = 00; - opj_free(l_codec); + opj_manager_free(l_manager,l_codec); + + if (l_manager_is_private) { + opj_manager_destroy(l_manager); + } } } @@ -882,18 +915,28 @@ opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec) return NULL; } -void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) { +void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) +{ + opj_manager_t l_manager = opj_manager_get_global_manager(); + opj_manager_destroy_cstr_info(l_manager, cstr_info); +} + +OPJ_API void OPJ_CALLCONV opj_manager_destroy_cstr_info(opj_manager_t manager, opj_codestream_info_v2_t **cstr_info) +{ + if (manager == NULL) { + return; + } if (cstr_info) { if ((*cstr_info)->m_default_tile_info.tccp_info){ - opj_free((*cstr_info)->m_default_tile_info.tccp_info); + opj_manager_free(manager, (*cstr_info)->m_default_tile_info.tccp_info); } if ((*cstr_info)->tile_info){ /* FIXME not used for the moment*/ } - opj_free((*cstr_info)); + opj_manager_free(manager, (*cstr_info)); (*cstr_info) = NULL; } } @@ -911,50 +954,86 @@ opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec) void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index) { + opj_manager_t manager = opj_manager_get_global_manager(); + opj_manager_destroy_cstr_index(manager, p_cstr_index); +} +OPJ_API void OPJ_CALLCONV opj_manager_destroy_cstr_index(opj_manager_t manager, opj_codestream_index_t **p_cstr_index) +{ + if (manager == NULL) { + return; + } if (*p_cstr_index){ - j2k_destroy_cstr_index(*p_cstr_index); + j2k_destroy_cstr_index(manager, *p_cstr_index); (*p_cstr_index) = NULL; } } opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (const char *fname, OPJ_BOOL p_is_read_stream) { - return opj_stream_create_file_stream(fname, OPJ_J2K_STREAM_CHUNK_SIZE, p_is_read_stream); -} + opj_manager_t l_manager = opj_manager_get_global_manager(); -opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream ( - const char *fname, - OPJ_SIZE_T p_size, - OPJ_BOOL p_is_read_stream) + return opj_manager_stream_create_default_file_stream(l_manager, fname, p_is_read_stream); +} +OPJ_API opj_stream_t* OPJ_CALLCONV opj_manager_stream_create_default_file_stream (opj_manager_t manager, const char *fname, OPJ_BOOL p_is_read_stream) { - opj_stream_t* l_stream = 00; - FILE *p_file; - const char *mode; - - if (! fname) { - return NULL; - } - - if(p_is_read_stream) mode = "rb"; else mode = "wb"; - - p_file = fopen(fname, mode); - - if (! p_file) { - return NULL; - } - - l_stream = opj_stream_create(p_size,p_is_read_stream); - if (! l_stream) { - fclose(p_file); - return NULL; - } - - opj_stream_set_user_data(l_stream, p_file, (opj_stream_free_user_data_fn) fclose); - opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file)); - opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file); - opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file); - opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file); - opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file); - - return l_stream; + return opj_manager_stream_create_file_stream(manager, fname, OPJ_J2K_STREAM_CHUNK_SIZE, p_is_read_stream); +} + +opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream +( + const char *fname, + OPJ_SIZE_T p_size, + OPJ_BOOL p_is_read_stream +) +{ + opj_manager_t l_manager = opj_manager_get_global_manager(); + + return opj_manager_stream_create_file_stream(l_manager, fname, p_size, p_is_read_stream); +} + +OPJ_API opj_stream_t* OPJ_CALLCONV opj_manager_stream_create_file_stream +( + opj_manager_t manager, + const char *fname, + OPJ_SIZE_T p_buffer_size, + OPJ_BOOL p_is_read_stream +) +{ + opj_stream_t* l_stream = NULL; + FILE *p_file; + const char *mode; + + if (manager == NULL) { + return NULL; + } + + if (! fname) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "NULL filename"); + return NULL; + } + + if(p_is_read_stream) mode = "rb"; else mode = "wb"; + + p_file = fopen(fname, mode); + + if (! p_file) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Can't open file %s.", fname); + return NULL; + } + + l_stream = opj_manager_stream_create(manager, p_buffer_size, p_is_read_stream); + if (! l_stream) { + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Can't create filestream for file %s.", fname); + fclose(p_file); + return NULL; + } + + opj_stream_set_user_data(l_stream, p_file, (opj_stream_free_user_data_fn) fclose); + opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file)); + opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file); + opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file); + opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file); + opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file); + + return l_stream; } diff --git a/src/lib/openjp2/openjpeg.h b/src/lib/openjp2/openjpeg.h index 4a8e75ce..12f3f582 100644 --- a/src/lib/openjp2/openjpeg.h +++ b/src/lib/openjp2/openjpeg.h @@ -298,6 +298,78 @@ typedef enum CODEC_FORMAT { OPJ_CODEC_JPX = 4 /**< JPX file format (JPEG 2000 Part-2) : to be coded */ } OPJ_CODEC_FORMAT; +/* + ========================================================== + manager typedef definitions + ========================================================== + */ + +/** + * OpenJPEG Manager object handle + * + * This object is responsible for memory allocations & events. + * When no manager is used, events can still be retrieved for an opj_codec_t object using opj_set_info_handler, opj_set_warning_handler and opj_set_error_handler. + * The use of a manager & those functions are mutually exclusive. + * + */ +typedef struct opj_manager* opj_manager_t; + +/* + ========================================================== + memory manager typedef definitions + ========================================================== + */ + +/** + * Callback function prototype for malloc + * @param size Size of the memory block, in bytes. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the memory block allocated by the function. + * */ +typedef void* (*opj_malloc_callback) (OPJ_SIZE_T size, void *client_data); + +/** + * Callback function prototype for calloc + * @param num Number of elements to allocate. + * @param size Size of an element, in bytes. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the memory block allocated by the function. + * */ +typedef void* (*opj_calloc_callback) (OPJ_SIZE_T num, OPJ_SIZE_T size, void *client_data); + +/** + * Callback function prototype for realloc + * @param ptr Pointer to a memory block previously allocated with opj_malloc_callback, opj_calloc_callback or opj_realloc_callback. + * @param size New size for the memory block, in bytes. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the reallocated memory block, which may be either the same as ptr or a new location. + * */ +typedef void* (*opj_realloc_callback) (void* ptr, OPJ_SIZE_T size, void *client_data); + +/** + * Callback function prototype for free + * @param ptr Pointer to a memory block previously allocated with opj_malloc_callback, opj_calloc_callback or opj_realloc_callback. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the memory block allocated by the function. + * */ +typedef void (*opj_free_callback) (void* ptr, void *client_data); + +/** + * Callback function prototype for aligned_malloc + * @param size Size of the memory block, in bytes. + * @param alignment The value of alignment shall be a multiple of sizeof( void *), that is also a power of two. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the memory block allocated by the function. + * */ +typedef void* (*opj_aligned_malloc_callback) (OPJ_SIZE_T size, OPJ_SIZE_T alignment, void *client_data); + +/** + * Callback function prototype for aligned_free + * @param ptr Pointer to a memory block previously allocated with opj_aligned_malloc_callback. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the memory block allocated by the function. + * */ +typedef void (*opj_aligned_free_callback) (void* ptr, void *client_data); /* ========================================================== @@ -1054,7 +1126,77 @@ extern "C" { /* Get the version of the openjpeg library*/ OPJ_API const char * OPJ_CALLCONV opj_version(void); -/* +/* +========================================================== + manager typedef definitions +========================================================== +*/ + +/** + * Creates a manager using user provided allocation functions + * + * @param context callback user context that will be passed back each time an allocation callback is called. + * @param malloc_callback malloc callback. + * @param calloc_callback calloc callback. + * @param realloc_callback realloc callback. + * @param free_callback free callback. + * @param aligned_malloc_callback aligned malloc callback. + * @param aligned_free_callback aligned free callback. + * @return returns a new manager object handle if successful, returns NULL otherwise + * */ +OPJ_API opj_manager_t OPJ_CALLCONV opj_manager_create( + void* context, + opj_malloc_callback malloc_callback, + opj_calloc_callback calloc_callback, + opj_realloc_callback realloc_callback, + opj_free_callback free_callback, + opj_aligned_malloc_callback aligned_malloc_callback, + opj_aligned_free_callback aligned_free_callback + ); + +/** + * Creates a manager using default allocation functions + * + * @return returns a new manager object handle if successful, returns NULL otherwise + * */ +OPJ_API opj_manager_t OPJ_CALLCONV opj_manager_create_default(void); + +/** + * Deallocates a manager + * + * @param manager manager to be destroyed + * */ +OPJ_API void OPJ_CALLCONV opj_manager_destroy(opj_manager_t manager); + +/** + * Set the info handler use by openjpeg. + * @param manager the manager previously initialised + * @param p_callback the callback function which will be used + * @param p_user_data client object where will be returned the message + * */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_manager_set_info_handler(opj_manager_t manager, + opj_msg_callback p_callback, + void * p_user_data); +/** + * Set the warning handler use by openjpeg. + * @param manager the manager previously initialised + * @param p_callback the callback function which will be used + * @param p_user_data client object where will be returned the message + * */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_manager_set_warning_handler(opj_manager_t manager, + opj_msg_callback p_callback, + void * p_user_data); +/** + * Set the error handler use by openjpeg. + * @param manager the manager previously initialised + * @param p_callback the callback function which will be used + * @param p_user_data client object where will be returned the message + * */ +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_manager_set_error_handler(opj_manager_t manager, + opj_msg_callback p_callback, + void * p_user_data); + +/* ========================================================== image functions definitions ========================================================== @@ -1070,6 +1212,17 @@ OPJ_API const char * OPJ_CALLCONV opj_version(void); * */ OPJ_API opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc); +/** + * Creates an image using the specified manager + * + * @param manager the manager previously initialised + * @param numcmpts number of components + * @param cmptparms components parameters + * @param clrspc image color space + * @return returns a new image structure if successful, returns NULL otherwise + * */ +OPJ_API opj_image_t* OPJ_CALLCONV opj_manager_image_create(opj_manager_t manager, OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc); + /** * Deallocate any resources associated with an image * @@ -1077,6 +1230,14 @@ OPJ_API opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_imag */ OPJ_API void OPJ_CALLCONV opj_image_destroy(opj_image_t *image); +/** + * Deallocate any resources associated with an image + * + * @param manager the manager previously initialised + * @param image image to be destroyed + * */ +OPJ_API void OPJ_CALLCONV opj_manager_image_destroy(opj_manager_t manager, opj_image_t *image); + /** * Creates an image without allocating memory for the image (used in the new version of the library). * @@ -1088,6 +1249,18 @@ OPJ_API void OPJ_CALLCONV opj_image_destroy(opj_image_t *image); */ OPJ_API opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc); +/** + * Creates an image without allocating memory for the image (used in the new version of the library). + * + * @param manager the manager previously initialised + * @param numcmpts the number of components + * @param cmptparms the components parameters + * @param clrspc the image color space + * + * @return a new image structure if successful, NULL otherwise. + * */ +OPJ_API opj_image_t* OPJ_CALLCONV opj_manager_image_tile_create(opj_manager_t manager, OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc); + /* ========================================================== stream functions definitions @@ -1103,6 +1276,8 @@ OPJ_API opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj */ OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_default_create(OPJ_BOOL p_is_input); +OPJ_API opj_stream_t* OPJ_CALLCONV opj_manager_stream_default_create(opj_manager_t manager, OPJ_BOOL p_is_input); + /** * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream. * @@ -1113,6 +1288,8 @@ OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_default_create(OPJ_BOOL p_is_input */ OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size, OPJ_BOOL p_is_input); +OPJ_API opj_stream_t* OPJ_CALLCONV opj_manager_stream_create(opj_manager_t manager, OPJ_SIZE_T p_buffer_size, OPJ_BOOL p_is_input); + /** * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream. If needed the user must * close its own implementation of the stream. @@ -1171,6 +1348,7 @@ OPJ_API void OPJ_CALLCONV opj_stream_set_user_data_length(opj_stream_t* p_stream * @param p_is_read_stream whether the stream is a read stream (true) or not (false) */ OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (const char *fname, OPJ_BOOL p_is_read_stream); +OPJ_API opj_stream_t* OPJ_CALLCONV opj_manager_stream_create_default_file_stream (opj_manager_t manager, const char *fname, OPJ_BOOL p_is_read_stream); /** Create a stream from a file identified with its filename with a specific buffer size * @param fname the filename of the file to stream @@ -1180,8 +1358,12 @@ OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (const OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (const char *fname, OPJ_SIZE_T p_buffer_size, OPJ_BOOL p_is_read_stream); +OPJ_API opj_stream_t* OPJ_CALLCONV opj_manager_stream_create_file_stream (opj_manager_t manager, + const char *fname, + OPJ_SIZE_T p_buffer_size, + OPJ_BOOL p_is_read_stream); -/* +/* ========================================================== event manager functions definitions ========================================================== @@ -1227,6 +1409,7 @@ OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, * @return Returns a handle to a decompressor if successful, returns NULL otherwise * */ OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format); +OPJ_API opj_codec_t* OPJ_CALLCONV opj_manager_create_decompress(opj_manager_t manager, OPJ_CODEC_FORMAT format); /** * Destroy a decompressor handle @@ -1243,7 +1426,6 @@ OPJ_API void OPJ_CALLCONV opj_destroy_codec(opj_codec_t * p_codec); OPJ_API OPJ_BOOL OPJ_CALLCONV opj_end_decompress ( opj_codec_t *p_codec, opj_stream_t *p_stream); - /** * Set decoding parameters to default values * @param parameters Decompression parameters @@ -1402,6 +1584,7 @@ OPJ_API OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data( opj_codec_t *p_codec, * @return Returns a handle to a compressor if successful, returns NULL otherwise */ OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format); +OPJ_API opj_codec_t* OPJ_CALLCONV opj_manager_create_compress(opj_manager_t manager, OPJ_CODEC_FORMAT format); /** Set encoding parameters to default values, that means : @@ -1463,6 +1646,7 @@ OPJ_API OPJ_BOOL OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec, */ OPJ_API OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_codec, opj_stream_t *p_stream); + /* ========================================================== codec output functions definitions @@ -1475,6 +1659,7 @@ Destroy Codestream information after compression or decompression @param cstr_info Codestream information structure */ OPJ_API void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info); +OPJ_API void OPJ_CALLCONV opj_manager_destroy_cstr_info(opj_manager_t manager, opj_codestream_info_v2_t **cstr_info); /** @@ -1510,6 +1695,7 @@ OPJ_API opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_ OPJ_API opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec); OPJ_API void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index); +OPJ_API void OPJ_CALLCONV opj_manager_destroy_cstr_index(opj_manager_t manager, opj_codestream_index_t **p_cstr_index); /** @@ -1532,7 +1718,6 @@ OPJ_API opj_jp2_metadata_t* OPJ_CALLCONV opj_get_jp2_metadata(opj_codec_t *p_cod */ OPJ_API opj_jp2_index_t* OPJ_CALLCONV opj_get_jp2_index(opj_codec_t *p_codec); - /* ========================================================== MCT functions @@ -1553,6 +1738,11 @@ OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_MCT( opj_cparameters_t *parameters, OPJ_FLOAT32 * pEncodingMatrix, OPJ_INT32 * p_dc_shift, OPJ_UINT32 pNbComp); +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_manager_set_MCT(opj_manager_t manager, + opj_cparameters_t *parameters, + OPJ_FLOAT32 * pEncodingMatrix, + OPJ_INT32 * p_dc_shift, + OPJ_UINT32 pNbComp); diff --git a/src/lib/openjp2/opj_codec.h b/src/lib/openjp2/opj_codec.h index 6bd791fa..9f0a0400 100644 --- a/src/lib/openjp2/opj_codec.h +++ b/src/lib/openjp2/opj_codec.h @@ -52,14 +52,12 @@ typedef struct opj_codec_private /** Main header reading function handler */ OPJ_BOOL (*opj_read_header) ( struct opj_stream_private * cio, void * p_codec, - opj_image_t **p_image, - struct opj_event_mgr * p_manager); + opj_image_t **p_image); /** Decoding function */ OPJ_BOOL (*opj_decode) ( void * p_codec, struct opj_stream_private * p_cio, - opj_image_t * p_image, - struct opj_event_mgr * p_manager); + opj_image_t * p_image); /** FIXME DOC */ OPJ_BOOL (*opj_read_tile_header)( void * p_codec, @@ -71,21 +69,18 @@ typedef struct opj_codec_private OPJ_INT32 * p_tile_y1, OPJ_UINT32 * p_nb_comps, OPJ_BOOL * p_should_go_on, - struct opj_stream_private * p_cio, - struct opj_event_mgr * p_manager); + struct opj_stream_private * p_cio); /** FIXME DOC */ OPJ_BOOL (*opj_decode_tile_data)( void * p_codec, OPJ_UINT32 p_tile_index, OPJ_BYTE * p_data, OPJ_UINT32 p_data_size, - struct opj_stream_private * p_cio, - struct opj_event_mgr * p_manager); + struct opj_stream_private * p_cio); /** Reading function used after codestream if necessary */ OPJ_BOOL (* opj_end_decompress) ( void *p_codec, - struct opj_stream_private * cio, - struct opj_event_mgr * p_manager); + struct opj_stream_private * cio); /** Codec destroy function handler */ void (*opj_destroy) (void * p_codec); @@ -99,20 +94,17 @@ typedef struct opj_codec_private OPJ_INT32 p_start_x, OPJ_INT32 p_end_x, OPJ_INT32 p_start_y, - OPJ_INT32 p_end_y, - struct opj_event_mgr * p_manager); + OPJ_INT32 p_end_y); /** Get tile function */ OPJ_BOOL (*opj_get_decoded_tile) ( void *p_codec, opj_stream_private_t * p_cio, opj_image_t *p_image, - struct opj_event_mgr * p_manager, OPJ_UINT32 tile_index); /** Set the decoded resolution factor */ OPJ_BOOL (*opj_set_decoded_resolution_factor) ( void * p_codec, - OPJ_UINT32 res_factor, - opj_event_mgr_t * p_manager); + OPJ_UINT32 res_factor); } m_decompression; /** @@ -122,36 +114,33 @@ typedef struct opj_codec_private { OPJ_BOOL (* opj_start_compress) ( void *p_codec, struct opj_stream_private * cio, - struct opj_image * p_image, - struct opj_event_mgr * p_manager); + struct opj_image * p_image); OPJ_BOOL (* opj_encode) ( void * p_codec, - struct opj_stream_private *p_cio, - struct opj_event_mgr * p_manager); + struct opj_stream_private *p_cio); OPJ_BOOL (* opj_write_tile) ( void * p_codec, OPJ_UINT32 p_tile_index, OPJ_BYTE * p_data, OPJ_UINT32 p_data_size, - struct opj_stream_private * p_cio, - struct opj_event_mgr * p_manager); + struct opj_stream_private * p_cio); OPJ_BOOL (* opj_end_compress) ( void * p_codec, - struct opj_stream_private * p_cio, - struct opj_event_mgr * p_manager); + struct opj_stream_private * p_cio); void (* opj_destroy) (void * p_codec); OPJ_BOOL (* opj_setup_encoder) ( void * p_codec, opj_cparameters_t * p_param, - struct opj_image * p_image, - struct opj_event_mgr * p_manager); + struct opj_image * p_image); } m_compression; } m_codec_data; /** FIXME DOC*/ void * m_codec; - /** Event handler */ - opj_event_mgr_t m_event_mgr; + /** Manager */ + opj_manager_t manager; + /** Flag to indicate if the manager is private or not */ + OPJ_BOOL manager_is_private; /** Flag to indicate if the codec is used to decode or encode*/ OPJ_BOOL is_decompressor; void (*opj_dump_codec) (void * p_codec, OPJ_INT32 info_flag, FILE* output_stream); diff --git a/src/lib/openjp2/opj_includes.h b/src/lib/openjp2/opj_includes.h index f855b7c6..175af84c 100644 --- a/src/lib/openjp2/opj_includes.h +++ b/src/lib/openjp2/opj_includes.h @@ -174,7 +174,7 @@ static INLINE long opj_lrintf(float f) { #include "opj_inttypes.h" #include "opj_clock.h" -#include "opj_malloc.h" +/* #include "opj_malloc.h" */ #include "event.h" #include "function_list.h" #include "bio.h" @@ -211,6 +211,7 @@ static INLINE long opj_lrintf(float f) { /* V2 */ #include "opj_codec.h" +#include "opj_manager.h" #endif /* OPJ_INCLUDES_H */ diff --git a/src/lib/openjp2/opj_malloc.h b/src/lib/openjp2/opj_malloc.h index 5007b0c9..20c13963 100644 --- a/src/lib/openjp2/opj_malloc.h +++ b/src/lib/openjp2/opj_malloc.h @@ -107,12 +107,12 @@ Allocate memory aligned to a 16 byte boundry #endif #endif -#define opj_aligned_malloc(size) malloc(size) +#define opj_aligned_malloc(size,align) malloc(size) #define opj_aligned_free(m) free(m) #ifdef HAVE_MM_MALLOC #undef opj_aligned_malloc - #define opj_aligned_malloc(size) _mm_malloc(size, 16) + #define opj_aligned_malloc(size,align) _mm_malloc((size), (align)) #undef opj_aligned_free #define opj_aligned_free(m) _mm_free(m) #endif @@ -120,7 +120,7 @@ Allocate memory aligned to a 16 byte boundry #ifdef HAVE_MEMALIGN extern void* memalign(size_t, size_t); #undef opj_aligned_malloc - #define opj_aligned_malloc(size) memalign(16, (size)) + #define opj_aligned_malloc(size, align) memalign((align), (size)) #undef opj_aligned_free #define opj_aligned_free(m) free(m) #endif @@ -129,9 +129,9 @@ Allocate memory aligned to a 16 byte boundry #undef opj_aligned_malloc extern int posix_memalign(void**, size_t, size_t); - static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){ + static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size, size_t align){ void* mem = NULL; - posix_memalign(&mem, 16, size); + posix_memalign(&mem, align, size); return mem; } #undef opj_aligned_free @@ -140,7 +140,7 @@ Allocate memory aligned to a 16 byte boundry #ifdef ALLOC_PERF_OPT #undef opj_aligned_malloc - #define opj_aligned_malloc(size) opj_malloc(size) + #define opj_aligned_malloc(size, align) opj_malloc(size) #undef opj_aligned_free #define opj_aligned_free(m) opj_free(m) #endif diff --git a/src/lib/openjp2/opj_manager.c b/src/lib/openjp2/opj_manager.c new file mode 100644 index 00000000..4e1e4d6d --- /dev/null +++ b/src/lib/openjp2/opj_manager.c @@ -0,0 +1,252 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2014, Matthieu Darbois + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "opj_includes.h" +#include "opj_malloc.h" + +/** + * Default malloc function + * @param size Size of the memory block, in bytes. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the memory block allocated by the function. + * */ +static void* opj_default_malloc(OPJ_SIZE_T size, void *client_data); + +/** + * Default calloc function + * @param num Number of elements to allocate. + * @param size Size of an element, in bytes. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the memory block allocated by the function. + * */ +static void* opj_default_calloc(OPJ_SIZE_T num, OPJ_SIZE_T size, void *client_data); + +/** + * Default realloc function + * @param ptr Pointer to a memory block previously allocated with opj_malloc_callback, opj_calloc_callback or opj_realloc_callback. + * @param size New size for the memory block, in bytes. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the reallocated memory block, which may be either the same as ptr or a new location. + * */ +static void* opj_default_realloc(void* ptr, OPJ_SIZE_T size, void *client_data); + +/** + * Default free function + * @param ptr Pointer to a memory block previously allocated with opj_malloc_callback, opj_calloc_callback or opj_realloc_callback. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the memory block allocated by the function. + * */ +static void opj_default_free(void* ptr, void *client_data); + +/** + * Default aligned_malloc function + * @param size Size of the memory block, in bytes. + * @param alignment The value of alignment shall be a multiple of sizeof( void *), that is also a power of two. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the memory block allocated by the function. + * */ +static void* opj_default_aligned_malloc(OPJ_SIZE_T size, OPJ_SIZE_T alignment, void *client_data); + +/** + * Default aligned_free function + * @param ptr Pointer to a memory block previously allocated with opj_aligned_malloc_callback. + * @param client_data Client object where will be return the event message + * @return returns a pointer to the memory block allocated by the function. + * */ +static void opj_default_aligned_free(void* ptr, void *client_data); + +/** + * Default manager that can be used when no manager is available. + * + * */ +static const struct opj_manager opj_global_manager = +{ + NULL, + opj_default_malloc, + opj_default_calloc, + opj_default_realloc, + opj_default_free, + opj_default_aligned_malloc, + opj_default_aligned_free, + { + NULL, + NULL, + NULL, + NULL, + NULL, + NULL + } +}; + + +OPJ_API opj_manager_t OPJ_CALLCONV opj_manager_create( + void* context, + opj_malloc_callback malloc_callback, + opj_calloc_callback calloc_callback, + opj_realloc_callback realloc_callback, + opj_free_callback free_callback, + opj_aligned_malloc_callback aligned_malloc_callback, + opj_aligned_free_callback aligned_free_callback + ) +{ + opj_manager_t l_manager = NULL; + + /* Check parameters */ + if ( + (malloc_callback == NULL) || + (calloc_callback == NULL) || + (realloc_callback == NULL) || + (free_callback == NULL) || + (aligned_malloc_callback == NULL) || + (aligned_free_callback == NULL) + ) { + return NULL; + } + + l_manager = malloc_callback(sizeof(struct opj_manager), context); + if (l_manager == NULL) { + return NULL; + } + l_manager->context = context; + l_manager->malloc_callback = malloc_callback; + l_manager->calloc_callback = calloc_callback; + l_manager->realloc_callback = realloc_callback; + l_manager->free_callback = free_callback; + l_manager->aligned_malloc_callback = aligned_malloc_callback; + l_manager->aligned_free_callback = aligned_free_callback; + + /* NULL message handler are valid (& more efficient than default handler from opj_set_default_event_handler) */ + memset(&(l_manager->event_mgr), 0, sizeof(opj_event_mgr_t)); + + return l_manager; +} + + +OPJ_API opj_manager_t OPJ_CALLCONV opj_manager_create_default(void) +{ + return opj_manager_create( + NULL, + opj_default_malloc, + opj_default_calloc, + opj_default_realloc, + opj_default_free, + opj_default_aligned_malloc, + opj_default_aligned_free + ); +} + +OPJ_API void OPJ_CALLCONV opj_manager_destroy(opj_manager_t manager) +{ + opj_free_callback l_callback = NULL; + void* l_context = NULL; + if (manager != NULL) { + l_callback = manager->free_callback; + l_context = manager->context; + memset(manager, 0, sizeof(struct opj_manager)); + l_callback(manager, l_context); + } +} + +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_manager_set_info_handler(opj_manager_t manager, + opj_msg_callback p_callback, + void * p_user_data) +{ + if(manager == NULL){ + return OPJ_FALSE; + } + + manager->event_mgr.info_handler = p_callback; + manager->event_mgr.m_info_data = p_user_data; + return OPJ_TRUE; +} + +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_manager_set_warning_handler(opj_manager_t manager, + opj_msg_callback p_callback, + void * p_user_data) +{ + if(manager == NULL){ + return OPJ_FALSE; + } + + manager->event_mgr.warning_handler = p_callback; + manager->event_mgr.m_warning_data = p_user_data; + return OPJ_TRUE; +} + +OPJ_API OPJ_BOOL OPJ_CALLCONV opj_manager_set_error_handler(opj_manager_t manager, + opj_msg_callback p_callback, + void * p_user_data) +{ + if(manager == NULL){ + return OPJ_FALSE; + } + + manager->event_mgr.error_handler = p_callback; + manager->event_mgr.m_error_data = p_user_data; + return OPJ_TRUE; +} + +opj_manager_t opj_manager_get_global_manager(void) +{ + return (opj_manager_t)&opj_global_manager; /* const is dropped */ +} + +static void* opj_default_malloc(OPJ_SIZE_T size, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + return opj_malloc(size); +} +static void* opj_default_calloc(OPJ_SIZE_T num, OPJ_SIZE_T size, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + return opj_calloc(num, size); +} +static void* opj_default_realloc(void* ptr, OPJ_SIZE_T size, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + return opj_realloc(ptr, size); +} +static void opj_default_free(void* ptr, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + opj_free(ptr); +} +static void* opj_default_aligned_malloc(OPJ_SIZE_T size, OPJ_SIZE_T alignment, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + return opj_aligned_malloc(size, alignment); +} +static void opj_default_aligned_free(void* ptr, void *client_data) +{ + OPJ_ARG_NOT_USED(client_data); + opj_aligned_free(ptr); +} + diff --git a/src/lib/openjp2/opj_manager.h b/src/lib/openjp2/opj_manager.h new file mode 100644 index 00000000..b4cfd76a --- /dev/null +++ b/src/lib/openjp2/opj_manager.h @@ -0,0 +1,106 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2014, Matthieu Darbois + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef __OPJ_MANAGER_H +#define __OPJ_MANAGER_H +/** +@file opj_manager.h +*/ + +/** + * OpenJPEG Manager opaque structure + * + * This object is responsible for memory allocations & events. + * When no manager is used, events can still be retrieved for an opj_codec_t object using opj_set_info_handler, opj_set_warning_handler and opj_set_error_handler. + * The use of a manager & those functions are mutually exclusive. + * + */ +struct opj_manager +{ + /** callback user context that will be passed back each time an allocation callback is called. */ + void* context; + + /** malloc callback. */ + opj_malloc_callback malloc_callback; + + /** calloc callback. */ + opj_calloc_callback calloc_callback; + + /** realloc callback. */ + opj_realloc_callback realloc_callback; + + /** free callback. */ + opj_free_callback free_callback; + + /** aligned malloc callback. */ + opj_aligned_malloc_callback aligned_malloc_callback; + + /** aligned free callback. */ + opj_aligned_free_callback aligned_free_callback; + + /** event manager. */ + opj_event_mgr_t event_mgr; +}; + +/** + * Get the global manager. + * + * @return Global manager + */ +opj_manager_t opj_manager_get_global_manager(void); + + +static INLINE void* opj_manager_malloc(opj_manager_t manager, OPJ_SIZE_T size) +{ + return manager->malloc_callback(size, manager->context); +} +static INLINE void* opj_manager_calloc(opj_manager_t manager, OPJ_SIZE_T num, OPJ_SIZE_T size) +{ + return manager->calloc_callback(num, size, manager->context); +} +static INLINE void* opj_manager_realloc(opj_manager_t manager, void* ptr, OPJ_SIZE_T size) +{ + return manager->realloc_callback(ptr, size, manager->context); +} +static INLINE void opj_manager_free(opj_manager_t manager, void* ptr) +{ + return manager->free_callback(ptr, manager->context); +} +static INLINE void* opj_manager_aligned_malloc(opj_manager_t manager, OPJ_SIZE_T size, OPJ_SIZE_T alignment) +{ + return manager->aligned_malloc_callback(size, alignment, manager->context); +} +static INLINE void opj_manager_aligned_free(opj_manager_t manager, void* ptr) +{ + return manager->aligned_free_callback(ptr, manager->context); +} + +#endif /* __OPJ_MANAGER_H */ + diff --git a/src/lib/openjp2/pi.c b/src/lib/openjp2/pi.c index 1697bcbc..68843a27 100644 --- a/src/lib/openjp2/pi.c +++ b/src/lib/openjp2/pi.c @@ -193,7 +193,8 @@ static void opj_get_all_encoding_parameters(const opj_image_t *p_image, * @param p_cp the coding parameters. * @param tileno the index of the tile from which creating the packet iterator. */ -static opj_pi_iterator_t * opj_pi_create( const opj_image_t *p_image, +static opj_pi_iterator_t * opj_pi_create( opj_manager_t manager, + const opj_image_t *p_image, const opj_cp_t *p_cp, OPJ_UINT32 tileno ); /** @@ -789,7 +790,8 @@ static void opj_get_all_encoding_parameters( const opj_image_t *p_image, } } -static opj_pi_iterator_t * opj_pi_create( const opj_image_t *image, +static opj_pi_iterator_t * opj_pi_create( opj_manager_t manager, + const opj_image_t *image, const opj_cp_t *cp, OPJ_UINT32 tileno ) { @@ -816,7 +818,7 @@ static opj_pi_iterator_t * opj_pi_create( const opj_image_t *image, l_poc_bound = tcp->numpocs+1; /* memory allocations*/ - l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t)); + l_pi = (opj_pi_iterator_t*) opj_manager_calloc(manager, (l_poc_bound), sizeof(opj_pi_iterator_t)); if (!l_pi) { return NULL; } @@ -824,9 +826,9 @@ static opj_pi_iterator_t * opj_pi_create( const opj_image_t *image, l_current_pi = l_pi; for (pino = 0; pino < l_poc_bound ; ++pino) { - l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t)); + l_current_pi->comps = (opj_pi_comp_t*) opj_manager_calloc(manager, image->numcomps, sizeof(opj_pi_comp_t)); if (! l_current_pi->comps) { - opj_pi_destroy(l_pi, l_poc_bound); + opj_pi_destroy(manager, l_pi, l_poc_bound); return NULL; } @@ -837,9 +839,9 @@ static opj_pi_iterator_t * opj_pi_create( const opj_image_t *image, tccp = &tcp->tccps[compno]; - comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions, sizeof(opj_pi_resolution_t)); + comp->resolutions = (opj_pi_resolution_t*) opj_manager_calloc(manager, tccp->numresolutions, sizeof(opj_pi_resolution_t)); if (!comp->resolutions) { - opj_pi_destroy(l_pi, l_poc_bound); + opj_pi_destroy(manager, l_pi, l_poc_bound); return 00; } @@ -1151,7 +1153,7 @@ static OPJ_BOOL opj_pi_check_next_level( OPJ_INT32 pos, Packet iterator interface ========================================================== */ -opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image, +opj_pi_iterator_t *opj_pi_create_decode(opj_manager_t manager, opj_image_t *p_image, opj_cp_t *p_cp, OPJ_UINT32 p_tile_no) { @@ -1191,27 +1193,27 @@ opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image, l_bound = l_tcp->numpocs+1; l_data_stride = 4 * OPJ_J2K_MAXRLVLS; - l_tmp_data = (OPJ_UINT32*)opj_malloc( + l_tmp_data = (OPJ_UINT32*)opj_manager_malloc(manager, l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32)); if (! l_tmp_data) { return 00; } - l_tmp_ptr = (OPJ_UINT32**)opj_malloc( + l_tmp_ptr = (OPJ_UINT32**)opj_manager_malloc(manager, p_image->numcomps * sizeof(OPJ_UINT32 *)); if (! l_tmp_ptr) { - opj_free(l_tmp_data); + opj_manager_free(manager, l_tmp_data); return 00; } /* memory allocation for pi */ - l_pi = opj_pi_create(p_image, p_cp, p_tile_no); + l_pi = opj_pi_create(manager, p_image, p_cp, p_tile_no); if (!l_pi) { - opj_free(l_tmp_data); - opj_free(l_tmp_ptr); + opj_manager_free(manager, l_tmp_data); + opj_manager_free(manager, l_tmp_ptr); return 00; } @@ -1236,13 +1238,13 @@ opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image, l_current_pi = l_pi; /* memory allocation for include */ - l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16)); + l_current_pi->include = (OPJ_INT16*) opj_manager_calloc(manager, (l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16)); if (!l_current_pi->include) { - opj_free(l_tmp_data); - opj_free(l_tmp_ptr); - opj_pi_destroy(l_pi, l_bound); + opj_manager_free(manager, l_tmp_data); + opj_manager_free(manager, l_tmp_ptr); + opj_pi_destroy(manager, l_pi, l_bound); return 00; } @@ -1333,9 +1335,9 @@ opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image, l_current_pi->include = (l_current_pi-1)->include; ++l_current_pi; } - opj_free(l_tmp_data); + opj_manager_free(manager, l_tmp_data); l_tmp_data = 00; - opj_free(l_tmp_ptr); + opj_manager_free(manager, l_tmp_ptr); l_tmp_ptr = 00; if (l_tcp->POC) @@ -1351,7 +1353,7 @@ opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image, -opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image, +opj_pi_iterator_t *opj_pi_initialise_encode(opj_manager_t manager, const opj_image_t *p_image, opj_cp_t *p_cp, OPJ_UINT32 p_tile_no, J2K_T2_MODE p_t2_mode ) @@ -1392,24 +1394,24 @@ opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image, l_bound = l_tcp->numpocs+1; l_data_stride = 4 * OPJ_J2K_MAXRLVLS; - l_tmp_data = (OPJ_UINT32*)opj_malloc( + l_tmp_data = (OPJ_UINT32*)opj_manager_malloc(manager, l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32)); if (! l_tmp_data) { return 00; } - l_tmp_ptr = (OPJ_UINT32**)opj_malloc( + l_tmp_ptr = (OPJ_UINT32**)opj_manager_malloc(manager, p_image->numcomps * sizeof(OPJ_UINT32 *)); if (! l_tmp_ptr) { - opj_free(l_tmp_data); + opj_manager_free(manager, l_tmp_data); return 00; } /* memory allocation for pi*/ - l_pi = opj_pi_create(p_image,p_cp,p_tile_no); + l_pi = opj_pi_create(manager, p_image,p_cp,p_tile_no); if (!l_pi) { - opj_free(l_tmp_data); - opj_free(l_tmp_ptr); + opj_manager_free(manager, l_tmp_data); + opj_manager_free(manager, l_tmp_ptr); return 00; } @@ -1434,11 +1436,11 @@ opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image, l_current_pi = l_pi; /* memory allocation for include*/ - l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16)); + l_current_pi->include = (OPJ_INT16*) opj_manager_calloc(manager, l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16)); if (!l_current_pi->include) { - opj_free(l_tmp_data); - opj_free(l_tmp_ptr); - opj_pi_destroy(l_pi, l_bound); + opj_manager_free(manager, l_tmp_data); + opj_manager_free(manager, l_tmp_ptr); + opj_pi_destroy(manager, l_pi, l_bound); return 00; } @@ -1521,9 +1523,9 @@ opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image, ++l_current_pi; } - opj_free(l_tmp_data); + opj_manager_free(manager, l_tmp_data); l_tmp_data = 00; - opj_free(l_tmp_ptr); + opj_manager_free(manager, l_tmp_ptr); l_tmp_ptr = 00; if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) { @@ -1794,14 +1796,14 @@ void opj_pi_create_encode( opj_pi_iterator_t *pi, } } -void opj_pi_destroy(opj_pi_iterator_t *p_pi, +void opj_pi_destroy(opj_manager_t manager, opj_pi_iterator_t *p_pi, OPJ_UINT32 p_nb_elements) { OPJ_UINT32 compno, pino; opj_pi_iterator_t *l_current_pi = p_pi; if (p_pi) { if (p_pi->include) { - opj_free(p_pi->include); + opj_manager_free(manager, p_pi->include); p_pi->include = 00; } for (pino = 0; pino < p_nb_elements; ++pino){ @@ -1809,18 +1811,18 @@ void opj_pi_destroy(opj_pi_iterator_t *p_pi, opj_pi_comp_t *l_current_component = l_current_pi->comps; for (compno = 0; compno < l_current_pi->numcomps; compno++){ if(l_current_component->resolutions) { - opj_free(l_current_component->resolutions); + opj_manager_free(manager, l_current_component->resolutions); l_current_component->resolutions = 00; } ++l_current_component; } - opj_free(l_current_pi->comps); + opj_manager_free(manager, l_current_pi->comps); l_current_pi->comps = 0; } ++l_current_pi; } - opj_free(p_pi); + opj_manager_free(manager, p_pi); } } diff --git a/src/lib/openjp2/pi.h b/src/lib/openjp2/pi.h index f239679f..6b8091b9 100644 --- a/src/lib/openjp2/pi.h +++ b/src/lib/openjp2/pi.h @@ -120,7 +120,7 @@ typedef struct opj_pi_iterator { * * @return a list of packet iterator that points to the first packet of the tile (not true). */ -opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *image, +opj_pi_iterator_t *opj_pi_initialise_encode(opj_manager_t manager, const opj_image_t *image, opj_cp_t *cp, OPJ_UINT32 tileno, J2K_T2_MODE t2_mode); @@ -162,7 +162,7 @@ Create a packet iterator for Decoder @return Returns a packet iterator that points to the first packet of the tile @see opj_pi_destroy */ -opj_pi_iterator_t *opj_pi_create_decode(opj_image_t * image, +opj_pi_iterator_t *opj_pi_create_decode(opj_manager_t manager, opj_image_t * image, opj_cp_t * cp, OPJ_UINT32 tileno); /** @@ -171,7 +171,7 @@ opj_pi_iterator_t *opj_pi_create_decode(opj_image_t * image, * @param p_pi the packet iterator array to destroy. * @param p_nb_elements the number of elements in the array. */ -void opj_pi_destroy(opj_pi_iterator_t *p_pi, +void opj_pi_destroy(opj_manager_t manager, opj_pi_iterator_t *p_pi, OPJ_UINT32 p_nb_elements); /** diff --git a/src/lib/openjp2/raw.c b/src/lib/openjp2/raw.c index 2498761c..f5d5c5ec 100644 --- a/src/lib/openjp2/raw.c +++ b/src/lib/openjp2/raw.c @@ -48,14 +48,14 @@ ========================================================== */ -opj_raw_t* opj_raw_create(void) { - opj_raw_t *raw = (opj_raw_t*)opj_malloc(sizeof(opj_raw_t)); +opj_raw_t* opj_raw_create(opj_manager_t manager) { + opj_raw_t *raw = (opj_raw_t*)opj_manager_malloc(manager, sizeof(opj_raw_t)); return raw; } -void opj_raw_destroy(opj_raw_t *raw) { +void opj_raw_destroy(opj_manager_t manager, opj_raw_t *raw) { if(raw) { - opj_free(raw); + opj_manager_free(manager, raw); } } diff --git a/src/lib/openjp2/raw.h b/src/lib/openjp2/raw.h index 572c6661..cc4ee09a 100644 --- a/src/lib/openjp2/raw.h +++ b/src/lib/openjp2/raw.h @@ -73,12 +73,12 @@ typedef struct opj_raw { Create a new RAW handle @return Returns a new RAW handle if successful, returns NULL otherwise */ -opj_raw_t* opj_raw_create(void); +opj_raw_t* opj_raw_create(opj_manager_t manager); /** Destroy a previously created RAW handle @param raw RAW handle to destroy */ -void opj_raw_destroy(opj_raw_t *raw); +void opj_raw_destroy(opj_manager_t manager, opj_raw_t *raw); /** Return the number of bytes written/read since initialisation @param raw RAW handle to destroy diff --git a/src/lib/openjp2/t1.c b/src/lib/openjp2/t1.c index 108ce78b..36657c68 100644 --- a/src/lib/openjp2/t1.c +++ b/src/lib/openjp2/t1.c @@ -1172,8 +1172,8 @@ static OPJ_BOOL opj_t1_allocate_buffers( /* encoder uses tile buffer, so no need to allocate */ if (!t1->encoder) { if(datasize > t1->datasize){ - opj_aligned_free(t1->data); - t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32)); + opj_manager_aligned_free(t1->manager, t1->data); + t1->data = (OPJ_INT32*) opj_manager_aligned_malloc(t1->manager, datasize * sizeof(OPJ_INT32), 16); if(!t1->data){ /* FIXME event manager error callback */ return OPJ_FALSE; @@ -1186,8 +1186,8 @@ static OPJ_BOOL opj_t1_allocate_buffers( flagssize=t1->flags_stride * (h+2); if(flagssize > t1->flagssize){ - opj_aligned_free(t1->flags); - t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t)); + opj_manager_aligned_free(t1->manager, t1->flags); + t1->flags = (opj_flag_t*) opj_manager_aligned_malloc(t1->manager, flagssize * sizeof(opj_flag_t), 16); if(!t1->flags){ /* FIXME event manager error callback */ return OPJ_FALSE; @@ -1210,23 +1210,24 @@ static OPJ_BOOL opj_t1_allocate_buffers( * and initializes the look-up tables of the Tier-1 coder/decoder * @return a new T1 handle if successful, returns NULL otherwise */ -opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder) +opj_t1_t* opj_t1_create(opj_manager_t manager, OPJ_BOOL isEncoder) { opj_t1_t *l_t1 = 00; - l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t)); + l_t1 = (opj_t1_t*) opj_manager_calloc(manager, 1, sizeof(opj_t1_t)); if (!l_t1) { return 00; } + l_t1->manager = manager; /* create MQC and RAW handles */ - l_t1->mqc = opj_mqc_create(); + l_t1->mqc = opj_mqc_create(manager); if (! l_t1->mqc) { opj_t1_destroy(l_t1); return 00; } - l_t1->raw = opj_raw_create(); + l_t1->raw = opj_raw_create(manager); if (! l_t1->raw) { opj_t1_destroy(l_t1); return 00; @@ -1244,28 +1245,29 @@ opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder) */ void opj_t1_destroy(opj_t1_t *p_t1) { + opj_manager_t manager; if (! p_t1) { return; } - + manager = p_t1->manager; /* destroy MQC and RAW handles */ - opj_mqc_destroy(p_t1->mqc); + opj_mqc_destroy(manager, p_t1->mqc); p_t1->mqc = 00; - opj_raw_destroy(p_t1->raw); + opj_raw_destroy(manager, p_t1->raw); p_t1->raw = 00; /* encoder uses tile buffer, so no need to free */ if (!p_t1->encoder && p_t1->data) { - opj_aligned_free(p_t1->data); + opj_manager_aligned_free(manager, p_t1->data); p_t1->data = 00; } if (p_t1->flags) { - opj_aligned_free(p_t1->flags); + opj_manager_aligned_free(manager, p_t1->flags); p_t1->flags = 00; } - opj_free(p_t1); + opj_manager_free(manager, p_t1); } OPJ_BOOL opj_t1_decode_cblks( opj_t1_t* t1, @@ -1622,7 +1624,7 @@ static void opj_t1_encode_cblk(opj_t1_t *t1, } /* fixed_quality */ - tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ; + tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps); cumwmsedec += tempwmsedec; tile->distotile += tempwmsedec; diff --git a/src/lib/openjp2/t1.h b/src/lib/openjp2/t1.h index 3bc0ad9e..b54d3f13 100644 --- a/src/lib/openjp2/t1.h +++ b/src/lib/openjp2/t1.h @@ -97,7 +97,8 @@ typedef OPJ_INT16 opj_flag_t; Tier-1 coding (coding of code-block coefficients) */ typedef struct opj_t1 { - + /** User memory/event manager */ + opj_manager_t manager; /** MQC component */ opj_mqc_t *mqc; /** RAW component */ @@ -151,7 +152,7 @@ OPJ_BOOL opj_t1_decode_cblks( opj_t1_t* t1, * and initializes the look-up tables of the Tier-1 coder/decoder * @return a new T1 handle if successful, returns NULL otherwise */ -opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder); +opj_t1_t* opj_t1_create(opj_manager_t manager, OPJ_BOOL isEncoder); /** * Destroys a previously created T1 handle diff --git a/src/lib/openjp2/t2.c b/src/lib/openjp2/t2.c index 5af1a69a..124af7ea 100644 --- a/src/lib/openjp2/t2.c +++ b/src/lib/openjp2/t2.c @@ -68,7 +68,8 @@ Encode a packet of a tile to a destination buffer @param cstr_info Codestream information structure @return */ -static OPJ_BOOL opj_t2_encode_packet( OPJ_UINT32 tileno, +static OPJ_BOOL opj_t2_encode_packet( opj_manager_t manager, + OPJ_UINT32 tileno, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, @@ -144,7 +145,8 @@ static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2, @param cblksty @param first */ -static OPJ_BOOL opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk, +static OPJ_BOOL opj_t2_init_seg( opj_manager_t manager, + opj_tcd_cblk_dec_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first); @@ -227,7 +229,7 @@ OPJ_BOOL opj_t2_encode_packets( opj_t2_t* p_t2, 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 = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode); + l_pi = opj_pi_initialise_encode(p_t2->manager, l_image, l_cp, p_tile_no, p_t2_mode); if (!l_pi) { return OPJ_FALSE; } @@ -249,15 +251,15 @@ OPJ_BOOL opj_t2_encode_packets( opj_t2_t* p_t2, if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) { /* TODO ADE : add an error */ - opj_pi_destroy(l_pi, l_nb_pocs); + opj_pi_destroy(p_t2->manager, l_pi, l_nb_pocs); return OPJ_FALSE; } while (opj_pi_next(l_current_pi)) { if (l_current_pi->layno < p_maxlayers) { l_nb_bytes = 0; - if (! opj_t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) { - opj_pi_destroy(l_pi, l_nb_pocs); + if (! opj_t2_encode_packet(p_t2->manager, p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) { + opj_pi_destroy(p_t2->manager, l_pi, l_nb_pocs); return OPJ_FALSE; } @@ -271,7 +273,7 @@ OPJ_BOOL opj_t2_encode_packets( opj_t2_t* p_t2, 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) { - opj_pi_destroy(l_pi, l_nb_pocs); + opj_pi_destroy(p_t2->manager, l_pi, l_nb_pocs); return OPJ_FALSE; } } @@ -286,15 +288,15 @@ OPJ_BOOL opj_t2_encode_packets( opj_t2_t* p_t2, l_current_pi = &l_pi[p_pino]; if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) { /* TODO ADE : add an error */ - opj_pi_destroy(l_pi, l_nb_pocs); + opj_pi_destroy(p_t2->manager, l_pi, l_nb_pocs); return OPJ_FALSE; } while (opj_pi_next(l_current_pi)) { if (l_current_pi->layno < p_maxlayers) { l_nb_bytes=0; - if (! opj_t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) { - opj_pi_destroy(l_pi, l_nb_pocs); + if (! opj_t2_encode_packet(p_t2->manager, p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) { + opj_pi_destroy(p_t2->manager, l_pi, l_nb_pocs); return OPJ_FALSE; } @@ -326,7 +328,7 @@ OPJ_BOOL opj_t2_encode_packets( opj_t2_t* p_t2, } } - opj_pi_destroy(l_pi, l_nb_pocs); + opj_pi_destroy(p_t2->manager, l_pi, l_nb_pocs); return OPJ_TRUE; } @@ -378,7 +380,7 @@ OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2, #endif /* create a packet iterator */ - l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no); + l_pi = opj_pi_create_decode(p_t2->manager, l_image, l_cp, p_tile_no); if (!l_pi) { return OPJ_FALSE; } @@ -397,14 +399,14 @@ OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2, if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) { /* TODO ADE : add an error */ - opj_pi_destroy(l_pi, l_nb_pocs); + opj_pi_destroy(p_t2->manager, l_pi, l_nb_pocs); return OPJ_FALSE; } - - first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL)); + + first_pass_failed = (OPJ_BOOL*)opj_manager_malloc(p_t2->manager, l_image->numcomps * sizeof(OPJ_BOOL)); if (!first_pass_failed) { - opj_pi_destroy(l_pi,l_nb_pocs); + opj_pi_destroy(p_t2->manager, l_pi,l_nb_pocs); return OPJ_FALSE; } memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL)); @@ -420,8 +422,8 @@ OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2, first_pass_failed[l_current_pi->compno] = OPJ_FALSE; if (! opj_t2_decode_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info, p_manager)) { - opj_pi_destroy(l_pi,l_nb_pocs); - opj_free(first_pass_failed); + opj_pi_destroy(p_t2->manager, l_pi,l_nb_pocs); + opj_manager_free(p_t2->manager, first_pass_failed); return OPJ_FALSE; } @@ -431,8 +433,8 @@ OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2, else { l_nb_bytes_read = 0; if (! opj_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, p_manager)) { - opj_pi_destroy(l_pi,l_nb_pocs); - opj_free(first_pass_failed); + opj_pi_destroy(p_t2->manager, l_pi,l_nb_pocs); + opj_manager_free(p_t2->manager, first_pass_failed); return OPJ_FALSE; } } @@ -471,7 +473,7 @@ OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2, } ++l_current_pi; - opj_free(first_pass_failed); + opj_manager_free(p_t2->manager, first_pass_failed); } /* INDEX >> */ #ifdef TODO_MSD @@ -483,7 +485,7 @@ OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2, /* << INDEX */ /* don't forget to release pi */ - opj_pi_destroy(l_pi,l_nb_pocs); + opj_pi_destroy(p_t2->manager, l_pi,l_nb_pocs); *p_data_read = (OPJ_UINT32)(l_current_data - p_src); return OPJ_TRUE; } @@ -497,24 +499,25 @@ OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2, * @param p_cp Image coding parameters. * @return a new T2 handle if successful, NULL otherwise. */ -opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp) +opj_t2_t* opj_t2_create(opj_manager_t manager, opj_image_t *p_image, opj_cp_t *p_cp) { /* create the t2 structure */ - opj_t2_t *l_t2 = (opj_t2_t*)opj_calloc(1,sizeof(opj_t2_t)); + opj_t2_t *l_t2 = (opj_t2_t*)opj_manager_calloc(manager, 1,sizeof(opj_t2_t)); if (!l_t2) { return NULL; } - + l_t2->manager = manager; l_t2->image = p_image; l_t2->cp = p_cp; return l_t2; } -void opj_t2_destroy(opj_t2_t *t2) { - if(t2) { - opj_free(t2); - } +void opj_t2_destroy(opj_t2_t *t2) +{ + if(t2) { + opj_manager_free(t2->manager, t2); + } } static OPJ_BOOL opj_t2_decode_packet( opj_t2_t* p_t2, @@ -557,7 +560,8 @@ static OPJ_BOOL opj_t2_decode_packet( opj_t2_t* p_t2, return OPJ_TRUE; } -static OPJ_BOOL opj_t2_encode_packet( OPJ_UINT32 tileno, +static OPJ_BOOL opj_t2_encode_packet( opj_manager_t manager, + OPJ_UINT32 tileno, opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, @@ -621,7 +625,7 @@ static OPJ_BOOL opj_t2_encode_packet( OPJ_UINT32 tileno, } } - bio = opj_bio_create(); + bio = opj_bio_create(manager); if (!bio) { /* FIXME event manager error callback */ return OPJ_FALSE; @@ -719,7 +723,7 @@ static OPJ_BOOL opj_t2_encode_packet( OPJ_UINT32 tileno, } if (!opj_bio_flush(bio)) { - opj_bio_destroy(bio); + opj_bio_destroy(manager, bio); return OPJ_FALSE; /* modified to eliminate longjmp !! */ } @@ -727,7 +731,7 @@ static OPJ_BOOL opj_t2_encode_packet( OPJ_UINT32 tileno, c += l_nb_bytes; length -= l_nb_bytes; - opj_bio_destroy(bio); + opj_bio_destroy(manager, bio); /* */ if (tcp->csty & J2K_CP_CSTY_EPH) { @@ -908,7 +912,7 @@ static OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2, step 2: Return to codestream for decoding */ - l_bio = opj_bio_create(); + l_bio = opj_bio_create(p_t2->manager); if (! l_bio) { return OPJ_FALSE; } @@ -939,7 +943,7 @@ static OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2, /* TODO MSD: no test to control the output of this function*/ opj_bio_inalign(l_bio); l_header_data += opj_bio_numbytes(l_bio); - opj_bio_destroy(l_bio); + opj_bio_destroy(p_t2->manager, l_bio); /* EPH markers */ if (p_tcp->csty & J2K_CP_CSTY_EPH) { @@ -1022,8 +1026,8 @@ static OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2, l_segno = 0; if (!l_cblk->numsegs) { - if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) { - opj_bio_destroy(l_bio); + if (! opj_t2_init_seg(p_t2->manager, l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) { + opj_bio_destroy(p_t2->manager, l_bio); return OPJ_FALSE; } } @@ -1031,8 +1035,8 @@ static OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2, l_segno = l_cblk->numsegs - 1; if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) { ++l_segno; - if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { - opj_bio_destroy(l_bio); + if (! opj_t2_init_seg(p_t2->manager, l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { + opj_bio_destroy(p_t2->manager, l_bio); return OPJ_FALSE; } } @@ -1048,8 +1052,8 @@ static OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2, if (n > 0) { ++l_segno; - if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { - opj_bio_destroy(l_bio); + if (! opj_t2_init_seg(p_t2->manager, l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { + opj_bio_destroy(p_t2->manager, l_bio); return OPJ_FALSE; } } @@ -1062,12 +1066,12 @@ static OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2, } if (!opj_bio_inalign(l_bio)) { - opj_bio_destroy(l_bio); + opj_bio_destroy(p_t2->manager, l_bio); return OPJ_FALSE; } l_header_data += opj_bio_numbytes(l_bio); - opj_bio_destroy(l_bio); + opj_bio_destroy(p_t2->manager, l_bio); /* EPH markers */ if (p_tcp->csty & J2K_CP_CSTY_EPH) { @@ -1189,9 +1193,9 @@ static OPJ_BOOL opj_t2_read_packet_data( opj_t2_t* p_t2, } /* Check if the cblk->data have allocated enough memory */ if ((l_cblk->data_current_size + l_seg->newlen) > l_cblk->data_max_size) { - OPJ_BYTE* new_cblk_data = (OPJ_BYTE*) opj_realloc(l_cblk->data, l_cblk->data_current_size + l_seg->newlen); + OPJ_BYTE* new_cblk_data = (OPJ_BYTE*) opj_manager_realloc(p_t2->manager, l_cblk->data, l_cblk->data_current_size + l_seg->newlen); if(! new_cblk_data) { - opj_free(l_cblk->data); + opj_manager_free(p_t2->manager, l_cblk->data); l_cblk->data = NULL; l_cblk->data_max_size = 0; /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to realloc code block cata!\n"); */ @@ -1338,7 +1342,8 @@ static OPJ_BOOL opj_t2_skip_packet_data( opj_t2_t* p_t2, } -OPJ_BOOL opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk, +OPJ_BOOL opj_t2_init_seg( opj_manager_t manager, + opj_tcd_cblk_dec_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first) @@ -1350,9 +1355,9 @@ OPJ_BOOL opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk, opj_tcd_seg_t* new_segs; cblk->m_current_max_segs += OPJ_J2K_DEFAULT_NB_SEGS; - new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t)); + new_segs = (opj_tcd_seg_t*) opj_manager_realloc(manager, cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t)); if(! new_segs) { - opj_free(cblk->segs); + opj_manager_free(manager, cblk->segs); cblk->segs = NULL; cblk->m_current_max_segs = 0; /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */ diff --git a/src/lib/openjp2/t2.h b/src/lib/openjp2/t2.h index 3b652eea..2c8bc599 100644 --- a/src/lib/openjp2/t2.h +++ b/src/lib/openjp2/t2.h @@ -51,7 +51,7 @@ Tier-2 coding */ typedef struct opj_t2 { - + opj_manager_t manager; /** Encoding: pointer to the src image. Decoding: pointer to the dst image. */ opj_image_t *image; /** pointer to the image coding parameters */ @@ -118,7 +118,7 @@ OPJ_BOOL opj_t2_decode_packets( opj_t2_t *t2, * @param p_cp Image coding parameters. * @return a new T2 handle if successful, NULL otherwise. */ -opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp); +opj_t2_t* opj_t2_create(opj_manager_t manager, opj_image_t *p_image, opj_cp_t *p_cp); /** Destroy a T2 handle diff --git a/src/lib/openjp2/tcd.c b/src/lib/openjp2/tcd.c index 8f1c9410..13303913 100644 --- a/src/lib/openjp2/tcd.c +++ b/src/lib/openjp2/tcd.c @@ -112,27 +112,27 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, /** * Allocates memory for a decoding code block. */ -static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block); +static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_manager_t manager, opj_tcd_cblk_dec_t * p_code_block); /** * Deallocates the decoding data of the given precinct. */ -static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct); +static void opj_tcd_code_block_dec_deallocate (opj_manager_t manager, opj_tcd_precinct_t * p_precinct); /** * Allocates memory for an encoding code block (but not data). */ -static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block); +static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_manager_t manager, opj_tcd_cblk_enc_t * p_code_block); /** * Allocates data for an encoding code block */ -static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_code_block); +static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_manager_t manager, opj_tcd_cblk_enc_t * p_code_block); /** * Deallocates the encoding data of the given precinct. */ -static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct); +static void opj_tcd_code_block_enc_deallocate (opj_manager_t manager, opj_tcd_precinct_t * p_precinct); /** @@ -182,21 +182,25 @@ static OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd, /** Create a new TCD handle */ -opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder) +opj_tcd_t* opj_tcd_create(opj_manager_t manager, OPJ_BOOL p_is_decoder) { opj_tcd_t *l_tcd = 00; + if (manager == NULL) { + return NULL; + } /* create the tcd structure */ - l_tcd = (opj_tcd_t*) opj_calloc(1,sizeof(opj_tcd_t)); + l_tcd = (opj_tcd_t*) opj_manager_calloc(manager, 1,sizeof(opj_tcd_t)); if (!l_tcd) { return 00; } + l_tcd->manager = manager; l_tcd->m_is_decoder = p_is_decoder ? 1 : 0; - l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1,sizeof(opj_tcd_image_t)); + l_tcd->tcd_image = (opj_tcd_image_t*)opj_manager_calloc(manager, 1,sizeof(opj_tcd_image_t)); if (!l_tcd->tcd_image) { - opj_free(l_tcd); + opj_manager_free(manager, l_tcd); return 00; } @@ -476,7 +480,7 @@ OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd, 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)); + tile_info->thresh = (OPJ_FLOAT64 *) opj_manager_malloc(tcd->manager, tcd_tcp->numlayers * sizeof(OPJ_FLOAT64)); if (!tile_info->thresh) { /* FIXME event manager error callback */ return OPJ_FALSE; @@ -501,7 +505,7 @@ OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd, -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_t*t2 = opj_t2_create(tcd->image, cp); + opj_t2_t*t2 = opj_t2_create(tcd->manager, tcd->image, cp); OPJ_FLOAT64 thresh = 0; if (t2 == 00) { @@ -592,12 +596,12 @@ OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd, p_tcd->image = p_image; p_tcd->cp = p_cp; - p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,sizeof(opj_tcd_tile_t)); + p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_manager_calloc(p_tcd->manager, 1,sizeof(opj_tcd_tile_t)); if (! p_tcd->tcd_image->tiles) { return OPJ_FALSE; } - p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(p_image->numcomps,sizeof(opj_tcd_tilecomp_t)); + p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_manager_calloc(p_tcd->manager, p_image->numcomps,sizeof(opj_tcd_tilecomp_t)); if (! p_tcd->tcd_image->tiles->comps ) { return OPJ_FALSE; } @@ -611,22 +615,22 @@ OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd, /** Destroy a previously created TCD handle */ -void opj_tcd_destroy(opj_tcd_t *tcd) { +void opj_tcd_destroy(opj_manager_t manager, opj_tcd_t *tcd) { if (tcd) { opj_tcd_free_tile(tcd); if (tcd->tcd_image) { - opj_free(tcd->tcd_image); + opj_manager_free(manager, tcd->tcd_image); tcd->tcd_image = 00; } - opj_free(tcd); + opj_manager_free(manager, tcd); } } -OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec) +OPJ_BOOL opj_alloc_tile_component_data(opj_manager_t manager, opj_tcd_tilecomp_t *l_tilec) { if ((l_tilec->data == 00) || ((l_tilec->data_size_needed > l_tilec->data_size) && (l_tilec->ownsData == OPJ_FALSE))) { - l_tilec->data = (OPJ_INT32 *) opj_malloc(l_tilec->data_size_needed); + l_tilec->data = (OPJ_INT32 *) opj_manager_malloc(manager, l_tilec->data_size_needed); if (! l_tilec->data ) { return OPJ_FALSE; } @@ -635,11 +639,11 @@ OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec) l_tilec->ownsData = OPJ_TRUE; } else if (l_tilec->data_size_needed > l_tilec->data_size) { - OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_tilec->data_size_needed); + OPJ_INT32 * new_data = (OPJ_INT32 *) opj_manager_realloc(manager, l_tilec->data, l_tilec->data_size_needed); /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile datan"); */ /* fprintf(stderr, "Not enough memory to handle tile data"); */ if (! new_data) { - opj_free(l_tilec->data); + opj_manager_free(manager, l_tilec->data); l_tilec->data = NULL; l_tilec->data_size = 0; l_tilec->data_size_needed = 0; @@ -735,7 +739,6 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, return OPJ_FALSE; } l_data_size = l_data_size * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0); - if ((((OPJ_UINT32)-1) / (OPJ_UINT32)sizeof(OPJ_UINT32)) < l_data_size) { opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n"); return OPJ_FALSE; @@ -750,7 +753,7 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, } l_tilec->data_size_needed = l_data_size; - if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(l_tilec)) { + if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(p_tcd->manager, l_tilec)) { opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n"); return OPJ_FALSE; } @@ -758,7 +761,7 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(opj_tcd_resolution_t); if (l_tilec->resolutions == 00) { - l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size); + l_tilec->resolutions = (opj_tcd_resolution_t *) opj_manager_malloc(p_tcd->manager, l_data_size); if (! l_tilec->resolutions ) { return OPJ_FALSE; } @@ -767,10 +770,10 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, memset(l_tilec->resolutions,0,l_data_size); } else if (l_data_size > l_tilec->resolutions_size) { - opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size); + opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_manager_realloc(p_tcd->manager, l_tilec->resolutions, l_data_size); if (! new_resolutions) { opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n"); - opj_free(l_tilec->resolutions); + opj_manager_free(p_tcd->manager, l_tilec->resolutions); l_tilec->resolutions = NULL; l_tilec->resolutions_size = 0; return OPJ_FALSE; @@ -875,7 +878,7 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1; /* WHY -1 ? */ if (! l_band->precincts) { - l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size); + l_band->precincts = (opj_tcd_precinct_t *) opj_manager_malloc(p_tcd->manager, /*3 * */ l_nb_precinct_size); if (! l_band->precincts) { return OPJ_FALSE; } @@ -885,10 +888,10 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, } else if (l_band->precincts_data_size < l_nb_precinct_size) { - opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size); + opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_manager_realloc(p_tcd->manager, l_band->precincts,/*3 * */ l_nb_precinct_size); if (! new_precincts) { - opj_event_msg(manager, EVT_ERROR, "Not enough memory to handle band precints\n"); - opj_free(l_band->precincts); + opj_event_msg(&(p_tcd->manager->event_mgr), EVT_ERROR, "Not enough memory to handle band precints\n"); + opj_manager_free(p_tcd->manager, l_band->precincts); l_band->precincts = NULL; l_band->precincts_data_size = 0; return OPJ_FALSE; @@ -934,7 +937,7 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block; if (! l_current_precinct->cblks.blocks) { - l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size); + l_current_precinct->cblks.blocks = opj_manager_malloc(p_tcd->manager, l_nb_code_blocks_size); if (! l_current_precinct->cblks.blocks ) { return OPJ_FALSE; } @@ -945,12 +948,12 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, l_current_precinct->block_size = l_nb_code_blocks_size; } else if (l_nb_code_blocks_size > l_current_precinct->block_size) { - void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks, l_nb_code_blocks_size); + void *new_blocks = opj_manager_realloc(p_tcd->manager, l_current_precinct->cblks.blocks, l_nb_code_blocks_size); if (! new_blocks) { - opj_free(l_current_precinct->cblks.blocks); + opj_manager_free(p_tcd->manager, l_current_precinct->cblks.blocks); l_current_precinct->cblks.blocks = NULL; l_current_precinct->block_size = 0; - opj_event_msg(manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n"); + opj_event_msg(&(p_tcd->manager->event_mgr), EVT_ERROR, "Not enough memory for current precinct codeblock element\n"); return OPJ_FALSE; } l_current_precinct->cblks.blocks = new_blocks; @@ -964,26 +967,26 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, } if (! l_current_precinct->incltree) { - l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw, l_current_precinct->ch, manager); + l_current_precinct->incltree = opj_tgt_create(p_tcd->manager, l_current_precinct->cw, l_current_precinct->ch); } else{ l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree, l_current_precinct->cw, l_current_precinct->ch, manager); } if (! l_current_precinct->incltree) { - opj_event_msg(manager, EVT_WARNING, "No incltree created.\n"); + opj_event_msg(&(p_tcd->manager->event_mgr), EVT_WARNING, "No incltree created.\n"); /*return OPJ_FALSE;*/ } if (! l_current_precinct->imsbtree) { - l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw, l_current_precinct->ch, manager); + l_current_precinct->imsbtree = opj_tgt_create(p_tcd->manager, l_current_precinct->cw, l_current_precinct->ch); } else { l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree, l_current_precinct->cw, l_current_precinct->ch, manager); } if (! l_current_precinct->imsbtree) { - opj_event_msg(manager, EVT_WARNING, "No imsbtree created.\n"); + opj_event_msg(&(p_tcd->manager->event_mgr), EVT_WARNING, "No imsbtree created.\n"); /*return OPJ_FALSE;*/ } @@ -996,7 +999,7 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, if (isEncoder) { opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno; - if (! opj_tcd_code_block_enc_allocate(l_code_block)) { + if (! opj_tcd_code_block_enc_allocate(p_tcd->manager, l_code_block)) { return OPJ_FALSE; } /* code-block size (global) */ @@ -1005,13 +1008,13 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1); l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1); - if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) { + if (! opj_tcd_code_block_enc_allocate_data(p_tcd->manager, l_code_block)) { return OPJ_FALSE; } } else { opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno; - if (! opj_tcd_code_block_dec_allocate(l_code_block)) { + if (! opj_tcd_code_block_dec_allocate(p_tcd->manager, l_code_block)) { return OPJ_FALSE; } /* code-block size (global) */ @@ -1049,17 +1052,17 @@ OPJ_BOOL opj_tcd_init_decode_tile (opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, opj_e /** * Allocates memory for an encoding code block (but not data memory). */ -static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block) +static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_manager_t manager, opj_tcd_cblk_enc_t * p_code_block) { if (! p_code_block->layers) { /* no memset since data */ - p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t)); + p_code_block->layers = (opj_tcd_layer_t*) opj_manager_calloc(manager, 100, sizeof(opj_tcd_layer_t)); if (! p_code_block->layers) { return OPJ_FALSE; } } if (! p_code_block->passes) { - p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t)); + p_code_block->passes = (opj_tcd_pass_t*) opj_manager_calloc(manager, 100, sizeof(opj_tcd_pass_t)); if (! p_code_block->passes) { return OPJ_FALSE; } @@ -1070,23 +1073,21 @@ static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_blo /** * Allocates data memory for an encoding code block. */ -static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_code_block) +static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_manager_t manager, opj_tcd_cblk_enc_t * p_code_block) { OPJ_UINT32 l_data_size; l_data_size = (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) * (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32)); - if (l_data_size > p_code_block->data_size) { if (p_code_block->data) { - opj_free(p_code_block->data - 1); /* again, why -1 */ + opj_manager_free(manager, p_code_block->data - 1); /* again, why -1 */ } - p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size+1); + p_code_block->data = (OPJ_BYTE*)opj_manager_malloc(manager, l_data_size); if(! p_code_block->data) { p_code_block->data_size = 0U; return OPJ_FALSE; } p_code_block->data_size = l_data_size; - p_code_block->data[0] = 0; p_code_block->data+=1; /*why +1 ?*/ } @@ -1096,18 +1097,18 @@ static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_cod /** * Allocates memory for a decoding code block. */ -static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block) +static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_manager_t manager, opj_tcd_cblk_dec_t * p_code_block) { if (! p_code_block->data) { - p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE); + p_code_block->data = (OPJ_BYTE*) opj_manager_malloc(manager, OPJ_J2K_DEFAULT_CBLK_DATA_SIZE); if (! p_code_block->data) { return OPJ_FALSE; } p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE; /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/ - p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,sizeof(opj_tcd_seg_t)); + p_code_block->segs = (opj_tcd_seg_t *) opj_manager_calloc(manager, OPJ_J2K_DEFAULT_NB_SEGS,sizeof(opj_tcd_seg_t)); if (! p_code_block->segs) { return OPJ_FALSE; } @@ -1195,7 +1196,7 @@ OPJ_BOOL opj_tcd_encode_tile( opj_tcd_t *p_tcd, p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i]; p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i]; } - p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t)); + p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_manager_calloc(p_tcd->manager, (size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t)); if (!p_cstr_info->tile[p_tile_no].packet) { /* FIXME event manager error callback */ return OPJ_FALSE; @@ -1463,7 +1464,7 @@ static void opj_tcd_free_tile(opj_tcd_t *p_tcd) opj_tcd_band_t *l_band = 00; opj_tcd_precinct_t *l_precinct = 00; OPJ_UINT32 l_nb_resolutions, l_nb_precincts; - void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00; + void (* l_tcd_code_block_deallocate) (opj_manager_t, opj_tcd_precinct_t *) = 00; if (! p_tcd) { return; @@ -1504,11 +1505,11 @@ static void opj_tcd_free_tile(opj_tcd_t *p_tcd) l_precinct->incltree = 00; opj_tgt_destroy(l_precinct->imsbtree); l_precinct->imsbtree = 00; - (*l_tcd_code_block_deallocate) (l_precinct); + (*l_tcd_code_block_deallocate) (p_tcd->manager, l_precinct); ++l_precinct; } - opj_free(l_band->precincts); + opj_manager_free(p_tcd->manager, l_band->precincts); l_band->precincts = 00; } ++l_band; @@ -1516,12 +1517,12 @@ static void opj_tcd_free_tile(opj_tcd_t *p_tcd) ++l_res; } - opj_free(l_tile_comp->resolutions); + opj_manager_free(p_tcd->manager, l_tile_comp->resolutions); l_tile_comp->resolutions = 00; } if (l_tile_comp->ownsData && l_tile_comp->data) { - opj_free(l_tile_comp->data); + opj_manager_free(p_tcd->manager, l_tile_comp->data); l_tile_comp->data = 00; l_tile_comp->ownsData = 0; l_tile_comp->data_size = 0; @@ -1530,9 +1531,9 @@ static void opj_tcd_free_tile(opj_tcd_t *p_tcd) ++l_tile_comp; } - opj_free(l_tile->comps); + opj_manager_free(p_tcd->manager, l_tile->comps); l_tile->comps = 00; - opj_free(p_tcd->tcd_image->tiles); + opj_manager_free(p_tcd->manager, p_tcd->tcd_image->tiles); p_tcd->tcd_image->tiles = 00; } @@ -1547,7 +1548,7 @@ static OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd, { opj_t2_t * l_t2; - l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp); + l_t2 = opj_t2_create(p_tcd->manager, p_tcd->image, p_tcd->cp); if (l_t2 == 00) { return OPJ_FALSE; } @@ -1580,7 +1581,7 @@ static OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd ) opj_tccp_t * l_tccp = p_tcd->tcp->tccps; - l_t1 = opj_t1_create(OPJ_FALSE); + l_t1 = opj_t1_create(p_tcd->manager, OPJ_FALSE); if (l_t1 == 00) { return OPJ_FALSE; } @@ -1624,12 +1625,12 @@ static OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd ) */ if (l_tccp->qmfbid == 1) { - if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) { + if (! opj_dwt_decode(p_tcd->manager, l_tile_comp, l_img_comp->resno_decoded+1)) { return OPJ_FALSE; } } else { - if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) { + if (! opj_dwt_decode_real(p_tcd->manager, l_tile_comp, l_img_comp->resno_decoded+1)) { return OPJ_FALSE; } } @@ -1669,7 +1670,7 @@ static OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd, opj_event_mgr_t *p_manage return OPJ_TRUE; } - l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*)); + l_data = (OPJ_BYTE **) opj_manager_malloc(p_tcd->manager, l_tile->numcomps*sizeof(OPJ_BYTE*)); if (! l_data) { return OPJ_FALSE; } @@ -1679,7 +1680,7 @@ static OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd, opj_event_mgr_t *p_manage ++l_tile_comp; } - if (! opj_mct_decode_custom(/* MCT data */ + if (! opj_mct_decode_custom(p_tcd->manager, /* MCT data */ (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix, /* size of components */ l_samples, @@ -1689,11 +1690,11 @@ static OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd, opj_event_mgr_t *p_manage l_tile->numcomps, /* tells if the data is signed */ p_tcd->image->comps->sgnd)) { - opj_free(l_data); + opj_manager_free(p_tcd->manager, l_data); return OPJ_FALSE; } - opj_free(l_data); + opj_manager_free(p_tcd->manager, l_data); } else { if (l_tcp->tccps->qmfbid == 1) { @@ -1788,7 +1789,7 @@ static OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd ) /** * Deallocates the encoding data of the given precinct. */ -static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct) +static void opj_tcd_code_block_dec_deallocate (opj_manager_t manager, opj_tcd_precinct_t * p_precinct) { OPJ_UINT32 cblkno , l_nb_code_blocks; @@ -1806,19 +1807,19 @@ static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct) for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { if (l_code_block->data) { - opj_free(l_code_block->data); + opj_manager_free(manager, l_code_block->data); l_code_block->data = 00; } if (l_code_block->segs) { - opj_free(l_code_block->segs ); + opj_manager_free(manager, l_code_block->segs ); l_code_block->segs = 00; } ++l_code_block; } - opj_free(p_precinct->cblks.dec); + opj_manager_free(manager, p_precinct->cblks.dec); p_precinct->cblks.dec = 00; } } @@ -1826,7 +1827,7 @@ static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct) /** * Deallocates the encoding data of the given precinct. */ -static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct) +static void opj_tcd_code_block_enc_deallocate (opj_manager_t manager, opj_tcd_precinct_t * p_precinct) { OPJ_UINT32 cblkno , l_nb_code_blocks; @@ -1836,23 +1837,23 @@ static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct) for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { if (l_code_block->data) { - opj_free(l_code_block->data - 1); + opj_manager_free(manager, l_code_block->data - 1); l_code_block->data = 00; } if (l_code_block->layers) { - opj_free(l_code_block->layers ); + opj_manager_free(manager, l_code_block->layers ); l_code_block->layers = 00; } if (l_code_block->passes) { - opj_free(l_code_block->passes ); + opj_manager_free(manager, l_code_block->passes ); l_code_block->passes = 00; } ++l_code_block; } - opj_free(p_precinct->cblks.enc); + opj_manager_free(manager, p_precinct->cblks.enc); p_precinct->cblks.enc = 00; } @@ -1945,7 +1946,7 @@ static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd ) return OPJ_TRUE; } - l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*)); + l_data = (OPJ_BYTE **) opj_manager_malloc(p_tcd->manager, l_tile->numcomps*sizeof(OPJ_BYTE*)); if (! l_data) { return OPJ_FALSE; } @@ -1955,7 +1956,7 @@ static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd ) ++l_tile_comp; } - if (! opj_mct_encode_custom(/* MCT data */ + if (! opj_mct_encode_custom(p_tcd->manager, /* MCT data */ (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix, /* size of components */ samples, @@ -1966,11 +1967,11 @@ static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd ) /* tells if the data is signed */ p_tcd->image->comps->sgnd) ) { - opj_free(l_data); + opj_manager_free(p_tcd->manager, l_data); return OPJ_FALSE; } - opj_free(l_data); + opj_manager_free(p_tcd->manager, l_data); } else if (l_tcp->tccps->qmfbid == 0) { opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples); @@ -1991,12 +1992,12 @@ static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd ) for (compno = 0; compno < l_tile->numcomps; ++compno) { if (l_tccp->qmfbid == 1) { - if (! opj_dwt_encode(l_tile_comp)) { + if (! opj_dwt_encode(p_tcd->manager, l_tile_comp)) { return OPJ_FALSE; } } else if (l_tccp->qmfbid == 0) { - if (! opj_dwt_encode_real(l_tile_comp)) { + if (! opj_dwt_encode_real(p_tcd->manager, l_tile_comp)) { return OPJ_FALSE; } } @@ -2015,7 +2016,7 @@ static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd ) OPJ_UINT32 l_mct_numcomps = 0U; opj_tcp_t * l_tcp = p_tcd->tcp; - l_t1 = opj_t1_create(OPJ_TRUE); + l_t1 = opj_t1_create(p_tcd->manager, OPJ_TRUE); if (l_t1 == 00) { return OPJ_FALSE; } @@ -2053,7 +2054,7 @@ static OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd, { opj_t2_t * l_t2; - l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp); + l_t2 = opj_t2_create(p_tcd->manager, p_tcd->image, p_tcd->cp); if (l_t2 == 00) { return OPJ_FALSE; } diff --git a/src/lib/openjp2/tcd.h b/src/lib/openjp2/tcd.h index bb9a6205..bdd9aeee 100644 --- a/src/lib/openjp2/tcd.h +++ b/src/lib/openjp2/tcd.h @@ -198,6 +198,8 @@ Tile coder/decoder */ typedef struct opj_tcd { + /** User memory/event manager */ + opj_manager_t manager; /** Position of the tilepart flag in Progression order*/ OPJ_INT32 tp_pos; /** Tile part number*/ @@ -233,16 +235,17 @@ Dump the content of a tcd structure /** Create a new TCD handle +@param manager User memory/event manager @param p_is_decoder FIXME DOC @return Returns a new TCD handle if successful returns NULL otherwise */ -opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder); +opj_tcd_t* opj_tcd_create(opj_manager_t manager, OPJ_BOOL p_is_decoder); /** Destroy a previously created TCD handle @param tcd TCD handle to destroy */ -void opj_tcd_destroy(opj_tcd_t *tcd); +void opj_tcd_destroy(opj_manager_t manager,opj_tcd_t *tcd); /** * Initialize the tile coder and may reuse some memory. @@ -359,7 +362,7 @@ OPJ_BOOL opj_tcd_copy_tile_data (opj_tcd_t *p_tcd, * * */ -OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec); +OPJ_BOOL opj_alloc_tile_component_data(opj_manager_t manager, opj_tcd_tilecomp_t *l_tilec); /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/src/lib/openjp2/tgt.c b/src/lib/openjp2/tgt.c index 5e34aa91..258d0f21 100644 --- a/src/lib/openjp2/tgt.c +++ b/src/lib/openjp2/tgt.c @@ -45,7 +45,7 @@ ========================================================== */ -opj_tgt_tree_t *opj_tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv, opj_event_mgr_t *manager) { +opj_tgt_tree_t *opj_tgt_create(opj_manager_t manager, OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv) { OPJ_INT32 nplh[32]; OPJ_INT32 nplv[32]; opj_tgt_node_t *node = 00; @@ -57,12 +57,13 @@ opj_tgt_tree_t *opj_tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv, opj_e OPJ_UINT32 numlvls; OPJ_UINT32 n; - tree = (opj_tgt_tree_t *) opj_calloc(1,sizeof(opj_tgt_tree_t)); + tree = (opj_tgt_tree_t *) opj_manager_calloc(manager, 1,sizeof(opj_tgt_tree_t)); if(!tree) { - opj_event_msg(manager, EVT_ERROR, "Not enough memory to create Tag-tree\n"); + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Not enough memory to create Tag-tree\n"); return 00; } + tree->manager = manager; tree->numleafsh = numleafsh; tree->numleafsv = numleafsv; @@ -80,15 +81,15 @@ opj_tgt_tree_t *opj_tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv, opj_e /* ADD */ if (tree->numnodes == 0) { - opj_free(tree); - opj_event_msg(manager, EVT_WARNING, "tgt_create tree->numnodes == 0, no tree created.\n"); + opj_manager_free(manager, tree); + opj_event_msg(&(manager->event_mgr), EVT_WARNING, "tgt_create tree->numnodes == 0, no tree created.\n"); return 00; } - tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t)); + tree->nodes = (opj_tgt_node_t*) opj_manager_calloc(manager, tree->numnodes, sizeof(opj_tgt_node_t)); if(!tree->nodes) { - opj_event_msg(manager, EVT_ERROR, "Not enough memory to create Tag-tree nodes\n"); - opj_free(tree); + opj_event_msg(&(manager->event_mgr), EVT_ERROR, "Not enough memory to create Tag-tree nodes\n"); + opj_manager_free(manager, tree); return 00; } tree->nodes_size = tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t); @@ -173,7 +174,7 @@ opj_tgt_tree_t *opj_tgt_init(opj_tgt_tree_t * p_tree,OPJ_UINT32 p_num_leafs_h, O l_node_size = p_tree->numnodes * (OPJ_UINT32)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); + opj_tgt_node_t* new_nodes = (opj_tgt_node_t*) opj_manager_realloc(p_tree->manager, p_tree->nodes, l_node_size); if (! new_nodes) { opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to reinitialize the tag tree\n"); opj_tgt_destroy(p_tree); @@ -219,15 +220,17 @@ opj_tgt_tree_t *opj_tgt_init(opj_tgt_tree_t * p_tree,OPJ_UINT32 p_num_leafs_h, O void opj_tgt_destroy(opj_tgt_tree_t *p_tree) { - if (! p_tree) { - return; - } + opj_manager_t manager = NULL; + if (! p_tree) { + return; + } + manager = p_tree->manager; - if (p_tree->nodes) { - opj_free(p_tree->nodes); - p_tree->nodes = 00; - } - opj_free(p_tree); + if (p_tree->nodes) { + opj_manager_free(manager, p_tree->nodes); + p_tree->nodes = 00; + } + opj_manager_free(manager, p_tree); } void opj_tgt_reset(opj_tgt_tree_t *p_tree) { diff --git a/src/lib/openjp2/tgt.h b/src/lib/openjp2/tgt.h index 10223805..18121b4f 100644 --- a/src/lib/openjp2/tgt.h +++ b/src/lib/openjp2/tgt.h @@ -66,11 +66,12 @@ Tag tree */ typedef struct opj_tgt_tree { - OPJ_UINT32 numleafsh; - OPJ_UINT32 numleafsv; - OPJ_UINT32 numnodes; + opj_manager_t manager; opj_tgt_node_t *nodes; - OPJ_UINT32 nodes_size; /* maximum size taken by nodes */ + OPJ_UINT32 numleafsh; + OPJ_UINT32 numleafsv; + OPJ_UINT32 numnodes; + OPJ_UINT32 nodes_size; /* maximum size taken by nodes */ } opj_tgt_tree_t; @@ -83,7 +84,7 @@ Create a tag-tree @param numleafsv Height of the array of leafs of the tree @return Returns a new tag-tree if successful, returns NULL otherwise */ -opj_tgt_tree_t *opj_tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv, opj_event_mgr_t *manager); +opj_tgt_tree_t *opj_tgt_create(opj_manager_t manager, OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv); /** * Reinitialises a tag-tree from an exixting one.