Add opj_image_data_alloc() / opj_image_data_free()

As bin/common/color.c used to directly call malloc()/free(), we need
to export functions dedicated to allocating/freeing image component data.
This commit is contained in:
Even Rouault 2017-07-06 01:47:40 +02:00
parent 61fb5dd7f8
commit f58aab9d6a
6 changed files with 98 additions and 66 deletions

View File

@ -122,9 +122,9 @@ static void sycc444_to_rgb(opj_image_t *img)
cb = img->comps[1].data; cb = img->comps[1].data;
cr = img->comps[2].data; cr = img->comps[2].data;
d0 = r = (int*)malloc(sizeof(int) * max); d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
d1 = g = (int*)malloc(sizeof(int) * max); d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
d2 = b = (int*)malloc(sizeof(int) * max); d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);
if (r == NULL || g == NULL || b == NULL) { if (r == NULL || g == NULL || b == NULL) {
goto fails; goto fails;
@ -139,19 +139,19 @@ static void sycc444_to_rgb(opj_image_t *img)
++g; ++g;
++b; ++b;
} }
free(img->comps[0].data); opj_image_data_free(img->comps[0].data);
img->comps[0].data = d0; img->comps[0].data = d0;
free(img->comps[1].data); opj_image_data_free(img->comps[1].data);
img->comps[1].data = d1; img->comps[1].data = d1;
free(img->comps[2].data); opj_image_data_free(img->comps[2].data);
img->comps[2].data = d2; img->comps[2].data = d2;
img->color_space = OPJ_CLRSPC_SRGB; img->color_space = OPJ_CLRSPC_SRGB;
return; return;
fails: fails:
free(r); opj_image_data_free(r);
free(g); opj_image_data_free(g);
free(b); opj_image_data_free(b);
}/* sycc444_to_rgb() */ }/* sycc444_to_rgb() */
static void sycc422_to_rgb(opj_image_t *img) static void sycc422_to_rgb(opj_image_t *img)
@ -174,9 +174,9 @@ static void sycc422_to_rgb(opj_image_t *img)
cb = img->comps[1].data; cb = img->comps[1].data;
cr = img->comps[2].data; cr = img->comps[2].data;
d0 = r = (int*)malloc(sizeof(int) * max); d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
d1 = g = (int*)malloc(sizeof(int) * max); d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
d2 = b = (int*)malloc(sizeof(int) * max); d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);
if (r == NULL || g == NULL || b == NULL) { if (r == NULL || g == NULL || b == NULL) {
goto fails; goto fails;
@ -222,11 +222,11 @@ static void sycc422_to_rgb(opj_image_t *img)
} }
} }
free(img->comps[0].data); opj_image_data_free(img->comps[0].data);
img->comps[0].data = d0; img->comps[0].data = d0;
free(img->comps[1].data); opj_image_data_free(img->comps[1].data);
img->comps[1].data = d1; img->comps[1].data = d1;
free(img->comps[2].data); opj_image_data_free(img->comps[2].data);
img->comps[2].data = d2; img->comps[2].data = d2;
img->comps[1].w = img->comps[2].w = img->comps[0].w; img->comps[1].w = img->comps[2].w = img->comps[0].w;
@ -237,9 +237,9 @@ static void sycc422_to_rgb(opj_image_t *img)
return; return;
fails: fails:
free(r); opj_image_data_free(r);
free(g); opj_image_data_free(g);
free(b); opj_image_data_free(b);
}/* sycc422_to_rgb() */ }/* sycc422_to_rgb() */
static void sycc420_to_rgb(opj_image_t *img) static void sycc420_to_rgb(opj_image_t *img)
@ -262,9 +262,9 @@ static void sycc420_to_rgb(opj_image_t *img)
cb = img->comps[1].data; cb = img->comps[1].data;
cr = img->comps[2].data; cr = img->comps[2].data;
d0 = r = (int*)malloc(sizeof(int) * max); d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
d1 = g = (int*)malloc(sizeof(int) * max); d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
d2 = b = (int*)malloc(sizeof(int) * max); d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);
if (r == NULL || g == NULL || b == NULL) { if (r == NULL || g == NULL || b == NULL) {
goto fails; goto fails;
@ -380,11 +380,11 @@ static void sycc420_to_rgb(opj_image_t *img)
} }
} }
free(img->comps[0].data); opj_image_data_free(img->comps[0].data);
img->comps[0].data = d0; img->comps[0].data = d0;
free(img->comps[1].data); opj_image_data_free(img->comps[1].data);
img->comps[1].data = d1; img->comps[1].data = d1;
free(img->comps[2].data); opj_image_data_free(img->comps[2].data);
img->comps[2].data = d2; img->comps[2].data = d2;
img->comps[1].w = img->comps[2].w = img->comps[0].w; img->comps[1].w = img->comps[2].w = img->comps[0].w;
@ -395,9 +395,9 @@ static void sycc420_to_rgb(opj_image_t *img)
return; return;
fails: fails:
free(r); opj_image_data_free(r);
free(g); opj_image_data_free(g);
free(b); opj_image_data_free(b);
}/* sycc420_to_rgb() */ }/* sycc420_to_rgb() */
void color_sycc_to_rgb(opj_image_t *img) void color_sycc_to_rgb(opj_image_t *img)
@ -574,8 +574,8 @@ void color_apply_icc_profile(opj_image_t *image)
max = max_w * max_h; max = max_w * max_h;
nr_samples = (size_t)(max * 3U * sizeof(unsigned char)); nr_samples = (size_t)(max * 3U * sizeof(unsigned char));
in = inbuf = (unsigned char*)malloc(nr_samples); in = inbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
out = outbuf = (unsigned char*)malloc(nr_samples); out = outbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
if (inbuf == NULL || outbuf == NULL) { if (inbuf == NULL || outbuf == NULL) {
goto fails0; goto fails0;
@ -605,15 +605,15 @@ void color_apply_icc_profile(opj_image_t *image)
ok = 1; ok = 1;
fails0: fails0:
free(inbuf); opj_image_data_free(inbuf);
free(outbuf); opj_image_data_free(outbuf);
} else { /* prec > 8 */ } else { /* prec > 8 */
unsigned short *inbuf, *outbuf, *in, *out; unsigned short *inbuf, *outbuf, *in, *out;
max = max_w * max_h; max = max_w * max_h;
nr_samples = (size_t)(max * 3U * sizeof(unsigned short)); nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
in = inbuf = (unsigned short*)malloc(nr_samples); in = inbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
out = outbuf = (unsigned short*)malloc(nr_samples); out = outbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
if (inbuf == NULL || outbuf == NULL) { if (inbuf == NULL || outbuf == NULL) {
goto fails1; goto fails1;
@ -643,8 +643,8 @@ fails0:
ok = 1; ok = 1;
fails1: fails1:
free(inbuf); opj_image_data_free(inbuf);
free(outbuf); opj_image_data_free(outbuf);
} }
} else { /* image->numcomps <= 2 : GRAY, GRAYA */ } else { /* image->numcomps <= 2 : GRAY, GRAYA */
if (prec <= 8) { if (prec <= 8) {
@ -653,8 +653,8 @@ fails1:
max = max_w * max_h; max = max_w * max_h;
nr_samples = (size_t)(max * 3 * sizeof(unsigned char)); nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
in = inbuf = (unsigned char*)malloc(nr_samples); in = inbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
out = outbuf = (unsigned char*)malloc(nr_samples); out = outbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
g = (int*)calloc((size_t)max, sizeof(int)); g = (int*)calloc((size_t)max, sizeof(int));
b = (int*)calloc((size_t)max, sizeof(int)); b = (int*)calloc((size_t)max, sizeof(int));
@ -703,18 +703,18 @@ fails1:
ok = 1; ok = 1;
fails2: fails2:
free(inbuf); opj_image_data_free(inbuf);
free(outbuf); opj_image_data_free(outbuf);
free(g); opj_image_data_free(g);
free(b); opj_image_data_free(b);
} else { /* prec > 8 */ } else { /* prec > 8 */
unsigned short *in, *inbuf, *out, *outbuf; unsigned short *in, *inbuf, *out, *outbuf;
opj_image_comp_t *new_comps; opj_image_comp_t *new_comps;
max = max_w * max_h; max = max_w * max_h;
nr_samples = (size_t)(max * 3U * sizeof(unsigned short)); nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
in = inbuf = (unsigned short*)malloc(nr_samples); in = inbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
out = outbuf = (unsigned short*)malloc(nr_samples); out = outbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
g = (int*)calloc((size_t)max, sizeof(int)); g = (int*)calloc((size_t)max, sizeof(int));
b = (int*)calloc((size_t)max, sizeof(int)); b = (int*)calloc((size_t)max, sizeof(int));
@ -763,10 +763,10 @@ fails2:
ok = 1; ok = 1;
fails3: fails3:
free(inbuf); opj_image_data_free(inbuf);
free(outbuf); opj_image_data_free(outbuf);
free(g); opj_image_data_free(g);
free(b); opj_image_data_free(b);
} }
}/* if(image->numcomps > 2) */ }/* if(image->numcomps > 2) */
@ -881,9 +881,9 @@ void color_cielab_to_rgb(opj_image_t *image)
max = image->comps[0].w * image->comps[0].h; max = image->comps[0].w * image->comps[0].h;
red = dst0 = (int*)malloc(max * sizeof(int)); red = dst0 = (int*)opj_image_data_alloc(max * sizeof(int));
green = dst1 = (int*)malloc(max * sizeof(int)); green = dst1 = (int*)opj_image_data_alloc(max * sizeof(int));
blue = dst2 = (int*)malloc(max * sizeof(int)); blue = dst2 = (int*)opj_image_data_alloc(max * sizeof(int));
if (red == NULL || green == NULL || blue == NULL) { if (red == NULL || green == NULL || blue == NULL) {
goto fails; goto fails;
@ -917,11 +917,11 @@ void color_cielab_to_rgb(opj_image_t *image)
cmsCloseProfile(in); cmsCloseProfile(in);
cmsCloseProfile(out); cmsCloseProfile(out);
#endif #endif
free(src0); opj_image_data_free(src0);
image->comps[0].data = dst0; image->comps[0].data = dst0;
free(src1); opj_image_data_free(src1);
image->comps[1].data = dst1; image->comps[1].data = dst1;
free(src2); opj_image_data_free(src2);
image->comps[2].data = dst2; image->comps[2].data = dst2;
image->color_space = new_space; image->color_space = new_space;
@ -938,13 +938,13 @@ fails:
cmsCloseProfile(out); cmsCloseProfile(out);
#endif #endif
if (red) { if (red) {
free(red); opj_image_data_free(red);
} }
if (green) { if (green) {
free(green); opj_image_data_free(green);
} }
if (blue) { if (blue) {
free(blue); opj_image_data_free(blue);
} }
return; return;
} }
@ -1004,7 +1004,7 @@ void color_cmyk_to_rgb(opj_image_t *image)
image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */ image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */
} }
free(image->comps[3].data); opj_image_data_free(image->comps[3].data);
image->comps[3].data = NULL; image->comps[3].data = NULL;
image->comps[0].prec = 8; image->comps[0].prec = 8;
image->comps[1].prec = 8; image->comps[1].prec = 8;

View File

@ -74,7 +74,7 @@ opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts,
opj_image_destroy(image); opj_image_destroy(image);
return NULL; return NULL;
} }
comp->data = (OPJ_INT32*) opj_aligned_malloc( comp->data = (OPJ_INT32*) opj_image_data_alloc(
(size_t)comp->w * comp->h * sizeof(OPJ_INT32)); (size_t)comp->w * comp->h * sizeof(OPJ_INT32));
if (!comp->data) { if (!comp->data) {
/* TODO replace with event manager, breaks API */ /* TODO replace with event manager, breaks API */
@ -99,7 +99,7 @@ void OPJ_CALLCONV opj_image_destroy(opj_image_t *image)
for (compno = 0; compno < image->numcomps; compno++) { for (compno = 0; compno < image->numcomps; compno++) {
opj_image_comp_t *image_comp = &(image->comps[compno]); opj_image_comp_t *image_comp = &(image->comps[compno]);
if (image_comp->data) { if (image_comp->data) {
opj_aligned_free(image_comp->data); opj_image_data_free(image_comp->data);
} }
} }
opj_free(image->comps); opj_free(image->comps);

View File

@ -8803,7 +8803,7 @@ static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data,
/* would overflow */ /* would overflow */
return OPJ_FALSE; return OPJ_FALSE;
} }
l_img_comp_dest->data = (OPJ_INT32*) opj_aligned_malloc(l_width * l_height * l_img_comp_dest->data = (OPJ_INT32*) opj_image_data_alloc(l_width * l_height *
sizeof(OPJ_INT32)); sizeof(OPJ_INT32));
if (! l_img_comp_dest->data) { if (! l_img_comp_dest->data) {
return OPJ_FALSE; return OPJ_FALSE;
@ -10419,7 +10419,7 @@ static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k,
/* Transfer TCD data to output image data */ /* Transfer TCD data to output image data */
for (i = 0; i < p_j2k->m_output_image->numcomps; i++) { for (i = 0; i < p_j2k->m_output_image->numcomps; i++) {
opj_aligned_free(p_j2k->m_output_image->comps[i].data); opj_image_data_free(p_j2k->m_output_image->comps[i].data);
p_j2k->m_output_image->comps[i].data = p_j2k->m_output_image->comps[i].data =
p_j2k->m_tcd->tcd_image->tiles->comps[i].data; p_j2k->m_tcd->tcd_image->tiles->comps[i].data;
p_j2k->m_output_image->comps[i].resno_decoded = p_j2k->m_output_image->comps[i].resno_decoded =
@ -10824,7 +10824,7 @@ OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k,
p_j2k->m_output_image->comps[compno].resno_decoded; p_j2k->m_output_image->comps[compno].resno_decoded;
if (p_image->comps[compno].data) { if (p_image->comps[compno].data) {
opj_aligned_free(p_image->comps[compno].data); opj_image_data_free(p_image->comps[compno].data);
} }
p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;

View File

@ -1007,3 +1007,14 @@ opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream(
return l_stream; return l_stream;
} }
void* OPJ_CALLCONV opj_image_data_alloc(OPJ_SIZE_T size)
{
return opj_aligned_malloc(size);
}
void OPJ_CALLCONV opj_image_data_free(void* ptr)
{
opj_aligned_free(ptr);
}

View File

@ -1093,6 +1093,27 @@ 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_API opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts,
opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc); opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc);
/**
* Allocator for opj_image_t->comps[].data
* To be paired with opj_image_data_free.
*
* @param size number of bytes to allocate
*
* @return a new pointer if successful, NULL otherwise.
* @since 2.2.0
*/
OPJ_API void* OPJ_CALLCONV opj_image_data_alloc(OPJ_SIZE_T size);
/**
* Destructor for opj_image_t->comps[].data
* To be paired with opj_image_data_alloc.
*
* @param ptr Pointer to free
*
* @since 2.2.0
*/
OPJ_API void OPJ_CALLCONV opj_image_data_free(void* ptr);
/* /*
========================================================== ==========================================================
stream functions definitions stream functions definitions

View File

@ -674,7 +674,7 @@ OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
if ((l_tilec->data == 00) || if ((l_tilec->data == 00) ||
((l_tilec->data_size_needed > l_tilec->data_size) && ((l_tilec->data_size_needed > l_tilec->data_size) &&
(l_tilec->ownsData == OPJ_FALSE))) { (l_tilec->ownsData == OPJ_FALSE))) {
l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed); l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
if (! l_tilec->data) { if (! l_tilec->data) {
return OPJ_FALSE; return OPJ_FALSE;
} }
@ -683,8 +683,8 @@ OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
l_tilec->ownsData = OPJ_TRUE; l_tilec->ownsData = OPJ_TRUE;
} else if (l_tilec->data_size_needed > l_tilec->data_size) { } else if (l_tilec->data_size_needed > l_tilec->data_size) {
/* We don't need to keep old data */ /* We don't need to keep old data */
opj_aligned_free(l_tilec->data); opj_image_data_free(l_tilec->data);
l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed); l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
if (! l_tilec->data) { if (! l_tilec->data) {
l_tilec->data_size = 0; l_tilec->data_size = 0;
l_tilec->data_size_needed = 0; l_tilec->data_size_needed = 0;
@ -1645,7 +1645,7 @@ static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
} }
if (l_tile_comp->ownsData && l_tile_comp->data) { if (l_tile_comp->ownsData && l_tile_comp->data) {
opj_aligned_free(l_tile_comp->data); opj_image_data_free(l_tile_comp->data);
l_tile_comp->data = 00; l_tile_comp->data = 00;
l_tile_comp->ownsData = 0; l_tile_comp->ownsData = 0;
l_tile_comp->data_size = 0; l_tile_comp->data_size = 0;