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(
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 PPT marker\n");
+ 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;
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 ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
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 PPT marker\n");
+ 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;
return OPJ_TRUE;
}
-void opj_j2k_setup_encoder( opj_j2k_t *p_j2k,
+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_cp_t *cp = 00;
if(!p_j2k || !parameters || ! image) {
- return;
+ return OPJ_FALSE;
}
/* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
cp->tw = 1;
cp->th = 1;
- /* Create comment for codestream */
- if(parameters->cp_comment == NULL) {
- const char comment[] = "Created by OpenJPEG version ";
- const size_t clen = strlen(comment);
- const char *version = opj_version();
- /* UniPG>> */
-#ifdef USE_JPWL
- parameters->cp_comment = (char*)opj_malloc(clen+strlen(version)+11);
- sprintf(parameters->cp_comment,"%s%s with JPWL", comment, version);
-#else
- parameters->cp_comment = (char*)opj_malloc(clen+strlen(version)+1);
- sprintf(parameters->cp_comment,"%s%s", comment, version);
-#endif
- /* <<UniPG */
- }
-
/* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */
if (parameters->rsiz == OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */
OPJ_BOOL deprecated_used = OPJ_FALSE;
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);
+ 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");
+ return OPJ_FALSE;
+ }
memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
}
/* comment string */
if(parameters->cp_comment) {
- cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
- if(cp->comment) {
- strcpy(cp->comment, parameters->cp_comment);
- }
- }
+ cp->comment = (char*)opj_malloc(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");
+ return OPJ_FALSE;
+ }
+ strcpy(cp->comment, parameters->cp_comment);
+ } else {
+ /* Create default comment for codestream */
+ const char comment[] = "Created by OpenJPEG version ";
+ const size_t clen = strlen(comment);
+ const char *version = opj_version();
+
+ /* UniPG>> */
+#ifdef USE_JPWL
+ cp->comment = (char*)opj_malloc(clen+strlen(version)+11);
+ if(!cp->comment) {
+ opj_event_msg(p_manager, 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);
+ if(!cp->comment) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate comment string\n");
+ return OPJ_FALSE;
+ }
+ sprintf(cp->comment,"%s%s", comment, version);
+#endif
+ /* <<UniPG */
+ }
/*
calculate other encoding parameters
/* initialize the mutiple tiles */
/* ---------------------------- */
cp->tcps = (opj_tcp_t*) opj_calloc(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");
+ 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);
}
tcp->tccps = (opj_tccp_t*) opj_calloc(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");
+ 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_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");
+ return OPJ_FALSE;
+ }
+
tcp->mct = 2;
tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
+ if (! tcp->m_mct_coding_matrix) {
+ opj_free(lTmpBuf);
+ lTmpBuf = NULL;
+ opj_event_msg(p_manager, 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);
- assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
+ if (! tcp->m_mct_decoding_matrix) {
+ opj_free(lTmpBuf);
+ lTmpBuf = NULL;
+ opj_event_msg(p_manager, 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);
+ lTmpBuf = NULL;
+ opj_event_msg(p_manager, 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));
-
+ if (! tcp->mct_norms) {
+ opj_free(lTmpBuf);
+ lTmpBuf = NULL;
+ opj_event_msg(p_manager, 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);
tccp->m_dc_level_shift = l_dc_shift[i];
}
- opj_j2k_setup_mct_encoding(tcp,image);
+ if (opj_j2k_setup_mct_encoding(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");
+ return OPJ_FALSE;
+ }
}
else {
if(tcp->mct==1 && image->numcomps == 3) { // RGB->YCC MCT is enabled
opj_free(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)
/* codestream index creation */
l_j2k->cstr_index = opj_j2k_create_cstr_index();
-
- /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
if (!l_j2k->cstr_index){
opj_j2k_destroy(l_j2k);
- return NULL;
+ return 00;
}
- l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
-*/
-
/* validation list creation */
l_j2k->m_validation_list = opj_procedure_list_create();
if (! l_j2k->m_validation_list) {
assert(p_manager != 00);
p_j2k->m_private_image = opj_image_create0();
+ if (! p_j2k->m_private_image) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header." );
+ return OPJ_FALSE;
+ }
opj_copy_image_header(p_image, p_j2k->m_private_image);
/* TODO_MSD: Find a better way */
old_comps = image->comps;
new_comps = (opj_image_comp_t*)
opj_malloc(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;
+ }
for(i = 0; i < nr_channels; ++i) {
pcol = cmap[i].pcol; cmp = cmap[i].cmp;
/* Palette mapping: */
new_comps[i].data = (OPJ_INT32*)
opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
+ if (!new_comps[i].data) {
+ opj_free(new_comps);
+ new_comps = NULL;
+ /* FIXME no error code for opj_jp2_apply_pclr */
+ /* FIXME event manager error callback */
+ return;
+ }
new_comps[i].prec = channel_size[i];
new_comps[i].sgnd = channel_sign[i];
}
/* JP2 encoder interface */
/* ----------------------------------------------------------------------- */
-void opj_jp2_setup_encoder( opj_jp2_t *jp2,
+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_UINT32 sign;
if(!jp2 || !parameters || !image)
- return;
+ return OPJ_FALSE;
/* setup the J2K codec */
/* ------------------- */
/* 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");
- return;
+ return OPJ_FALSE;
}
- opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager );
+ if (opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager ) == OPJ_FALSE) {
+ return OPJ_FALSE;
+ }
/* setup the JP2 codec */
/* ------------------- */
jp2->minversion = 0; /* MinV */
jp2->numcl = 1;
jp2->cl = (OPJ_UINT32*) opj_malloc(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");
- return;
- }
+ if (!jp2->cl){
+ jp2->cl = NULL;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
+ return OPJ_FALSE;
+ }
jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
/* Image Header box */
jp2->numcomps = image->numcomps; /* NC */
jp2->comps = (opj_jp2_comps_t*) opj_malloc(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");
- return;
- }
+ if (!jp2->comps) {
+ jp2->comps = NULL;
+ opj_event_msg(p_manager, 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;
+ }
jp2->h = image->y1 - image->y0; /* HEIGHT */
jp2->w = image->x1 - image->x0; /* WIDTH */
jp2->approx = 0; /* APPROX */
jp2->jpip_on = parameters->jpip_on;
+
+ return OPJ_TRUE;
}
OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,