to avoid changing ABI, image data is now always aligned. This means that

the user should NOT free image data on their own; they must call
opj_image_destroy.
This commit is contained in:
Aaron Boxer 2016-01-08 08:54:38 -05:00
parent 64dc4796d8
commit 588e09726e
6 changed files with 381 additions and 329 deletions

View File

@ -57,6 +57,31 @@
#define OPJ_CLRSPC_SRGB CLRSPC_SRGB #define OPJ_CLRSPC_SRGB CLRSPC_SRGB
#endif #endif
static opj_image_t* image_create(OPJ_UINT32 numcmpts, OPJ_UINT32 w, OPJ_UINT32 h, OPJ_UINT32 prec) {
opj_image_cmptparm_t* cmptparms = (opj_image_cmptparm_t*)calloc(numcmpts, sizeof(opj_image_cmptparm_t));
opj_image_t* img = NULL;
OPJ_UINT32 compno=0;
for (compno = 0; compno < numcmpts; ++compno) {
memset(cmptparms + compno, 0, sizeof(opj_image_cmptparm_t));
cmptparms[compno].dx = 1;
cmptparms[compno].dy = 1;
cmptparms[compno].w = w;
cmptparms[compno].h = h;
cmptparms[compno].x0 = 0;
cmptparms[compno].y0 = 0;
cmptparms[compno].prec = prec;
cmptparms[compno].bpp = prec;
cmptparms[compno].sgnd = 0;
}
img = opj_image_create(numcmpts, (opj_image_cmptparm_t *)cmptparms, OPJ_CLRSPC_SRGB);
free(cmptparms);
return img;
}
/*-------------------------------------------------------- /*--------------------------------------------------------
Matrix for sYCC, Amendment 1 to IEC 61966-2-1 Matrix for sYCC, Amendment 1 to IEC 61966-2-1
@ -93,20 +118,29 @@ static void sycc444_to_rgb(opj_image_t *img)
const int *y, *cb, *cr; const int *y, *cb, *cr;
unsigned int maxw, maxh, max, i; unsigned int maxw, maxh, max, i;
int offset, upb; int offset, upb;
opj_image_t* new_image = image_create(3, img->comps[0].w, img->comps[0].h, img->comps[0].prec);
upb = (int)img->comps[0].prec; upb = (int)img->comps[0].prec;
offset = 1 << (upb - 1); upb = (1 << upb) - 1; offset = 1 << (upb - 1); upb = (1 << upb) - 1;
maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h; maxw = (unsigned int)img->comps[0].w;
maxh = (unsigned int)img->comps[0].h;
max = maxw * maxh; max = maxw * maxh;
y = img->comps[0].data; y = img->comps[0].data;
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) * (size_t)max); d0 = r = new_image->comps[0].data;
d1 = g = (int*)malloc(sizeof(int) * (size_t)max); d1 = g = new_image->comps[1].data;
d2 = b = (int*)malloc(sizeof(int) * (size_t)max); d2 = b = new_image->comps[2].data;
new_image->comps[0].data = NULL;
new_image->comps[1].data = NULL;
new_image->comps[2].data = NULL;
opj_image_destroy(new_image);
new_image = NULL;
for (i = 0U; i < max; ++i) for (i = 0U; i < max; ++i)
{ {
@ -124,23 +158,31 @@ static void sycc422_to_rgb(opj_image_t *img)
{ {
int *d0, *d1, *d2, *r, *g, *b; int *d0, *d1, *d2, *r, *g, *b;
const int *y, *cb, *cr; const int *y, *cb, *cr;
unsigned int maxw, maxh, max; unsigned int maxw, maxh;
int offset, upb; int offset, upb;
unsigned int i, j; unsigned int i, j;
opj_image_t* new_image = image_create(3, img->comps[0].w, img->comps[0].h, img->comps[0].prec);
upb = (int)img->comps[0].prec; upb = (int)img->comps[0].prec;
offset = 1 << (upb - 1); upb = (1 << upb) - 1; offset = 1 << (upb - 1); upb = (1 << upb) - 1;
maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h; maxw = (unsigned int)img->comps[0].w;
max = maxw * maxh; maxh = (unsigned int)img->comps[0].h;
y = img->comps[0].data; y = img->comps[0].data;
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) * (size_t)max); d0 = r = new_image->comps[0].data;
d1 = g = (int*)malloc(sizeof(int) * (size_t)max); d1 = g = new_image->comps[1].data;
d2 = b = (int*)malloc(sizeof(int) * (size_t)max); d2 = b = new_image->comps[2].data;
new_image->comps[0].data = NULL;
new_image->comps[1].data = NULL;
new_image->comps[2].data = NULL;
opj_image_destroy(new_image);
new_image = NULL;
for (i = 0U; i < maxh; ++i) for (i = 0U; i < maxh; ++i)
{ {
@ -179,23 +221,31 @@ static void sycc420_to_rgb(opj_image_t *img)
{ {
int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb; int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
const int *y, *cb, *cr, *ny; const int *y, *cb, *cr, *ny;
unsigned int maxw, maxh, max; unsigned int maxw, maxh;
int offset, upb; int offset, upb;
unsigned int i, j; unsigned int i, j;
opj_image_t* new_image = image_create(3, img->comps[0].w, img->comps[0].h, img->comps[0].prec);
upb = (int)img->comps[0].prec; upb = (int)img->comps[0].prec;
offset = 1 << (upb - 1); upb = (1 << upb) - 1; offset = 1 << (upb - 1); upb = (1 << upb) - 1;
maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h; maxw = (unsigned int)img->comps[0].w;
max = maxw * maxh; maxh = (unsigned int)img->comps[0].h;
y = img->comps[0].data; y = img->comps[0].data;
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) * (size_t)max); d0 = r = new_image->comps[0].data;
d1 = g = (int*)malloc(sizeof(int) * (size_t)max); d1 = g = new_image->comps[1].data;
d2 = b = (int*)malloc(sizeof(int) * (size_t)max); d2 = b = new_image->comps[2].data;
new_image->comps[0].data = NULL;
new_image->comps[1].data = NULL;
new_image->comps[2].data = NULL;
opj_image_destroy(new_image);
new_image = NULL;
for (i = 0U; i < (maxh & ~(unsigned int)1U); i += 2U) for (i = 0U; i < (maxh & ~(unsigned int)1U); i += 2U)
{ {
@ -334,6 +384,7 @@ void color_apply_icc_profile(opj_image_t *image)
int *r, *g, *b; int *r, *g, *b;
int prec, i, max, max_w, max_h; int prec, i, max, max_w, max_h;
OPJ_COLOR_SPACE oldspace; OPJ_COLOR_SPACE oldspace;
opj_image_t* new_image = NULL;
in_prof = in_prof =
cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len); cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
@ -522,8 +573,8 @@ fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
in = inbuf = (unsigned char*)malloc(nr_samples); in = inbuf = (unsigned char*)malloc(nr_samples);
out = outbuf = (unsigned char*)malloc(nr_samples); out = outbuf = (unsigned char*)malloc(nr_samples);
image->comps = (opj_image_comp_t*) new_image = image_create(2, image->comps[0].w, image->comps[0].h, image->comps[0].prec);
realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t)); image->comps = (opj_image_comp_t*)realloc(image->comps, (image->numcomps + 2)*sizeof(opj_image_comp_t));
if (image->numcomps == 2) if (image->numcomps == 2)
image->comps[3] = image->comps[1]; image->comps[3] = image->comps[1];
@ -531,8 +582,14 @@ fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
image->comps[1] = image->comps[0]; image->comps[1] = image->comps[0];
image->comps[2] = image->comps[0]; image->comps[2] = image->comps[0];
image->comps[1].data = (int*)calloc((size_t)max, sizeof(int)); image->comps[1].data = new_image->comps[0].data;
image->comps[2].data = (int*)calloc((size_t)max, sizeof(int)); image->comps[2].data = new_image->comps[1].data;
new_image->comps[0].data= NULL;
new_image->comps[1].data = NULL;
opj_image_destroy(new_image);
new_image = NULL;
image->numcomps += 2; image->numcomps += 2;
@ -595,6 +652,7 @@ void color_cielab_to_rgb(opj_image_t *image)
cmsHTRANSFORM transform; cmsHTRANSFORM transform;
cmsUInt16Number RGB[3]; cmsUInt16Number RGB[3];
cmsCIELab Lab; cmsCIELab Lab;
opj_image_t* new_image = image_create(3, image->comps[0].w, image->comps[0].h, image->comps[0].prec);
in = cmsCreateLab4Profile(NULL); in = cmsCreateLab4Profile(NULL);
out = cmsCreate_sRGBProfile(); out = cmsCreate_sRGBProfile();
@ -638,9 +696,16 @@ 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 = new_image->comps[0].data;
green = dst1 = (int*)malloc(max * sizeof(int)); green = dst1 = new_image->comps[1].data;
blue = dst2 = (int*)malloc(max * sizeof(int)); blue = dst2 = new_image->comps[2].data;
new_image->comps[0].data=NULL;
new_image->comps[1].data=NULL;
new_image->comps[2].data=NULL;
opj_image_destroy(new_image);
new_image = NULL;
minL = -(rl * ol) / (pow(2, prec0) - 1); minL = -(rl * ol) / (pow(2, prec0) - 1);
maxL = minL + rl; maxL = minL + rl;

View File

@ -64,7 +64,7 @@ opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptpa
comp->prec = cmptparms[compno].prec; comp->prec = cmptparms[compno].prec;
comp->bpp = cmptparms[compno].bpp; comp->bpp = cmptparms[compno].bpp;
comp->sgnd = cmptparms[compno].sgnd; comp->sgnd = cmptparms[compno].sgnd;
comp->data = (OPJ_INT32*) opj_calloc(comp->w * comp->h, sizeof(OPJ_INT32)); comp->data = (OPJ_INT32*) opj_aligned_malloc(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 */
/* fprintf(stderr,"Unable to allocate memory for image.\n"); */ /* fprintf(stderr,"Unable to allocate memory for image.\n"); */

View File

@ -63,15 +63,10 @@ static void opj_j2k_transfer_image_data(opj_image_t* src, opj_image_t* dest) {
dest_comp->resno_decoded = src_comp->resno_decoded; dest_comp->resno_decoded = src_comp->resno_decoded;
if (dest_comp->data) { if (dest_comp->data) {
if (dest_comp->aligned_data)
opj_aligned_free(dest_comp->data); opj_aligned_free(dest_comp->data);
else
opj_free(dest_comp->data);
} }
dest_comp->data = src_comp->data; dest_comp->data = src_comp->data;
dest_comp->aligned_data = src_comp->aligned_data;
src_comp->data = NULL; src_comp->data = NULL;
src_comp->aligned_data = OPJ_FALSE;
} }
} }
@ -8157,7 +8152,6 @@ OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k,
opj_tcd_tilecomp_t* tilec = p_j2k->m_tcd->tcd_image->tiles->comps + compno; opj_tcd_tilecomp_t* tilec = p_j2k->m_tcd->tcd_image->tiles->comps + compno;
opj_image_comp_t* comp = p_j2k->m_output_image->comps + compno; opj_image_comp_t* comp = p_j2k->m_output_image->comps + compno;
comp->data = tilec->data; comp->data = tilec->data;
comp->aligned_data = OPJ_TRUE;
tilec->data = NULL; tilec->data = NULL;
comp->resno_decoded = p_j2k->m_tcd->image->comps[compno].resno_decoded; comp->resno_decoded = p_j2k->m_tcd->image->comps[compno].resno_decoded;
@ -8260,7 +8254,7 @@ static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data,
/* Allocate output component buffer if necessary */ /* Allocate output component buffer if necessary */
if (!l_img_comp_dest->data) { 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_aligned_malloc((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) { if (! l_img_comp_dest->data) {
return OPJ_FALSE; return OPJ_FALSE;
} }
@ -9791,7 +9785,6 @@ static OPJ_BOOL opj_j2k_needs_copy_tile_data(opj_j2k_t *p_j2k) {
} }
} }
return copy_tile_data; return copy_tile_data;
} }
static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,

View File

@ -992,7 +992,7 @@ static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
/* Palette mapping: */ /* Palette mapping: */
new_comps[i].data = (OPJ_INT32*) new_comps[i].data = (OPJ_INT32*)
opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32)); opj_aligned_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
if (!new_comps[i].data) { if (!new_comps[i].data) {
opj_free(new_comps); opj_free(new_comps);
new_comps = NULL; new_comps = NULL;

View File

@ -1373,13 +1373,9 @@ void OPJ_CALLCONV opj_image_single_component_data_free(opj_image_comp_t* comp) {
if (!comp) if (!comp)
return; return;
if (comp->data) { if (comp->data) {
if (comp->aligned_data)
opj_aligned_free(comp->data); opj_aligned_free(comp->data);
else
opj_free(comp->data);
comp->data = NULL; comp->data = NULL;
} }
comp->aligned_data = OPJ_FALSE;
} }

View File

@ -658,8 +658,6 @@ typedef struct opj_image_comp {
OPJ_UINT32 factor; OPJ_UINT32 factor;
/** image component data */ /** image component data */
OPJ_INT32 *data; OPJ_INT32 *data;
/* true if data was allocated with opj_aligned_malloc */
OPJ_BOOL aligned_data;
/** alpha channel */ /** alpha channel */
OPJ_UINT16 alpha; OPJ_UINT16 alpha;
} opj_image_comp_t; } opj_image_comp_t;