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
@ -78,13 +103,13 @@ static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
cb -= offset; cr -= offset; cb -= offset; cr -= offset;
r = y + (int)(1.402 * (float)cr); r = y + (int)(1.402 * (float)cr);
if(r < 0) r = 0; else if(r > upb) r = upb; *out_r = r; if (r < 0) r = 0; else if (r > upb) r = upb; *out_r = r;
g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr); g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
if(g < 0) g = 0; else if(g > upb) g = upb; *out_g = g; if (g < 0) g = 0; else if (g > upb) g = upb; *out_g = g;
b = y + (int)(1.772 * (float)cb); b = y + (int)(1.772 * (float)cb);
if(b < 0) b = 0; else if(b > upb) b = upb; *out_b = b; if (b < 0) b = 0; else if (b > upb) b = upb; *out_b = b;
} }
static void sycc444_to_rgb(opj_image_t *img) static void sycc444_to_rgb(opj_image_t *img)
@ -93,22 +118,31 @@ 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;
for(i = 0U; i < max; ++i) 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)
{ {
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
++y; ++cb; ++cr; ++r; ++g; ++b; ++y; ++cb; ++cr; ++r; ++g; ++b;
@ -116,7 +150,7 @@ static void sycc444_to_rgb(opj_image_t *img)
opj_image_all_components_data_free(img); opj_image_all_components_data_free(img);
img->comps[0].data = d0; img->comps[0].data = d0;
img->comps[1].data = d1; img->comps[1].data = d1;
img->comps[2].data = d2; img->comps[2].data = d2;
}/* sycc444_to_rgb() */ }/* sycc444_to_rgb() */
@ -124,27 +158,35 @@ 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;
for(i=0U; i < maxh; ++i) 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(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U) for (j = 0U; j < (maxw & ~(unsigned int)1U); j += 2U)
{ {
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
++y; ++r; ++g; ++b; ++y; ++r; ++g; ++b;
@ -157,9 +199,9 @@ static void sycc422_to_rgb(opj_image_t *img)
} }
} }
opj_image_all_components_data_free(img); opj_image_all_components_data_free(img);
img->comps[0].data = d0; img->comps[0].data = d0;
img->comps[1].data = d1; img->comps[1].data = d1;
img->comps[2].data = d2; img->comps[2].data = d2;
#if defined(USE_JPWL) || defined(USE_MJ2) #if defined(USE_JPWL) || defined(USE_MJ2)
img->comps[1].w = maxw; img->comps[1].h = maxh; img->comps[1].w = maxw; img->comps[1].h = maxh;
@ -179,30 +221,38 @@ 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;
for(i=0U; i < (maxh & ~(unsigned int)1U); i += 2U) 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)
{ {
ny = y + maxw; ny = y + maxw;
nr = r + maxw; ng = g + maxw; nb = b + maxw; nr = r + maxw; ng = g + maxw; nb = b + maxw;
for(j=0; j < (maxw & ~(unsigned int)1U); j += 2U) for (j = 0; j < (maxw & ~(unsigned int)1U); j += 2U)
{ {
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
++y; ++r; ++g; ++b; ++y; ++r; ++g; ++b;
@ -214,7 +264,7 @@ static void sycc420_to_rgb(opj_image_t *img)
sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb); sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
++ny; ++nr; ++ng; ++nb; ++cb; ++cr; ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
} }
if(j < maxw) if (j < maxw)
{ {
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
++y; ++r; ++g; ++b; ++y; ++r; ++g; ++b;
@ -224,9 +274,9 @@ static void sycc420_to_rgb(opj_image_t *img)
} }
y += maxw; r += maxw; g += maxw; b += maxw; y += maxw; r += maxw; g += maxw; b += maxw;
} }
if(i < maxh) if (i < maxh)
{ {
for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U) for (j = 0U; j < (maxw & ~(unsigned int)1U); j += 2U)
{ {
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
@ -236,7 +286,7 @@ static void sycc420_to_rgb(opj_image_t *img)
++y; ++r; ++g; ++b; ++cb; ++cr; ++y; ++r; ++g; ++b; ++cb; ++cr;
} }
if(j < maxw) if (j < maxw)
{ {
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
} }
@ -244,7 +294,7 @@ static void sycc420_to_rgb(opj_image_t *img)
opj_image_all_components_data_free(img); opj_image_all_components_data_free(img);
img->comps[0].data = d0; img->comps[0].data = d0;
img->comps[1].data = d1; img->comps[1].data = d1;
img->comps[2].data = d2; img->comps[2].data = d2;
#if defined(USE_JPWL) || defined(USE_MJ2) #if defined(USE_JPWL) || defined(USE_MJ2)
img->comps[1].w = maxw; img->comps[1].h = maxh; img->comps[1].w = maxw; img->comps[1].h = maxh;
@ -262,46 +312,46 @@ static void sycc420_to_rgb(opj_image_t *img)
void color_sycc_to_rgb(opj_image_t *img) void color_sycc_to_rgb(opj_image_t *img)
{ {
if(img->numcomps < 3) if (img->numcomps < 3)
{ {
img->color_space = OPJ_CLRSPC_GRAY; img->color_space = OPJ_CLRSPC_GRAY;
return; return;
} }
if((img->comps[0].dx == 1) if ((img->comps[0].dx == 1)
&& (img->comps[1].dx == 2) && (img->comps[1].dx == 2)
&& (img->comps[2].dx == 2) && (img->comps[2].dx == 2)
&& (img->comps[0].dy == 1) && (img->comps[0].dy == 1)
&& (img->comps[1].dy == 2) && (img->comps[1].dy == 2)
&& (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */ && (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */
{ {
sycc420_to_rgb(img); sycc420_to_rgb(img);
} }
else else
if((img->comps[0].dx == 1) if ((img->comps[0].dx == 1)
&& (img->comps[1].dx == 2) && (img->comps[1].dx == 2)
&& (img->comps[2].dx == 2) && (img->comps[2].dx == 2)
&& (img->comps[0].dy == 1) && (img->comps[0].dy == 1)
&& (img->comps[1].dy == 1) && (img->comps[1].dy == 1)
&& (img->comps[2].dy == 1))/* horizontal sub-sample only */ && (img->comps[2].dy == 1))/* horizontal sub-sample only */
{ {
sycc422_to_rgb(img); sycc422_to_rgb(img);
} }
else else
if((img->comps[0].dx == 1) if ((img->comps[0].dx == 1)
&& (img->comps[1].dx == 1) && (img->comps[1].dx == 1)
&& (img->comps[2].dx == 1) && (img->comps[2].dx == 1)
&& (img->comps[0].dy == 1) && (img->comps[0].dy == 1)
&& (img->comps[1].dy == 1) && (img->comps[1].dy == 1)
&& (img->comps[2].dy == 1))/* no sub-sample */ && (img->comps[2].dy == 1))/* no sub-sample */
{ {
sycc444_to_rgb(img); sycc444_to_rgb(img);
} }
else else
{ {
fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,__LINE__); fprintf(stderr, "%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__, __LINE__);
return; return;
} }
img->color_space = OPJ_CLRSPC_SRGB; img->color_space = OPJ_CLRSPC_SRGB;
}/* color_sycc_to_rgb() */ }/* color_sycc_to_rgb() */
@ -334,16 +384,17 @@ 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);
#ifdef DEBUG_PROFILE #ifdef DEBUG_PROFILE
FILE *icm = fopen("debug.icm","wb"); FILE *icm = fopen("debug.icm", "wb");
fwrite( image->icc_profile_buf,1, image->icc_profile_len,icm); fwrite(image->icc_profile_buf, 1, image->icc_profile_len, icm);
fclose(icm); fclose(icm);
#endif #endif
if(in_prof == NULL) return; if (in_prof == NULL) return;
in_space = cmsGetPCS(in_prof); in_space = cmsGetPCS(in_prof);
out_space = cmsGetColorSpace(in_prof); out_space = cmsGetColorSpace(in_prof);
@ -351,210 +402,216 @@ void color_apply_icc_profile(opj_image_t *image)
max_w = (int)image->comps[0].w; max_w = (int)image->comps[0].w;
max_h = (int)image->comps[0].h; max_h = (int)image->comps[0].h;
prec = (int)image->comps[0].prec; prec = (int)image->comps[0].prec;
oldspace = image->color_space; oldspace = image->color_space;
if(out_space == cmsSigRgbData) /* enumCS 16 */ if (out_space == cmsSigRgbData) /* enumCS 16 */
{ {
if( prec <= 8 ) if (prec <= 8)
{ {
in_type = TYPE_RGB_8; in_type = TYPE_RGB_8;
out_type = TYPE_RGB_8; out_type = TYPE_RGB_8;
} }
else
{
in_type = TYPE_RGB_16;
out_type = TYPE_RGB_16;
}
out_prof = cmsCreate_sRGBProfile();
image->color_space = OPJ_CLRSPC_SRGB;
}
else else
{ if (out_space == cmsSigGrayData) /* enumCS 17 */
in_type = TYPE_RGB_16; {
out_type = TYPE_RGB_16; in_type = TYPE_GRAY_8;
} out_type = TYPE_RGB_8;
out_prof = cmsCreate_sRGBProfile(); out_prof = cmsCreate_sRGBProfile();
image->color_space = OPJ_CLRSPC_SRGB; image->color_space = OPJ_CLRSPC_SRGB;
} }
else else
if(out_space == cmsSigGrayData) /* enumCS 17 */ if (out_space == cmsSigYCbCrData) /* enumCS 18 */
{ {
in_type = TYPE_GRAY_8; in_type = TYPE_YCbCr_16;
out_type = TYPE_RGB_8; out_type = TYPE_RGB_16;
out_prof = cmsCreate_sRGBProfile(); out_prof = cmsCreate_sRGBProfile();
image->color_space = OPJ_CLRSPC_SRGB; image->color_space = OPJ_CLRSPC_SRGB;
} }
else else
if(out_space == cmsSigYCbCrData) /* enumCS 18 */ {
{
in_type = TYPE_YCbCr_16;
out_type = TYPE_RGB_16;
out_prof = cmsCreate_sRGBProfile();
image->color_space = OPJ_CLRSPC_SRGB;
}
else
{
#ifdef DEBUG_PROFILE #ifdef DEBUG_PROFILE
fprintf(stderr,"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown " fprintf(stderr, "%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
"output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n", "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
__FILE__,__LINE__,out_space, __FILE__, __LINE__, out_space,
(out_space>>24) & 0xff,(out_space>>16) & 0xff, (out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
(out_space>>8) & 0xff, out_space & 0xff); (out_space >> 8) & 0xff, out_space & 0xff);
#endif #endif
return; return;
} }
#ifdef DEBUG_PROFILE #ifdef DEBUG_PROFILE
fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)" fprintf(stderr, "%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
"\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec, "\n\tprofile: in(%p) out(%p)\n", __FILE__, __LINE__, image->numcomps, prec,
max_w,max_h, (void*)in_prof,(void*)out_prof); max_w, max_h, (void*)in_prof, (void*)out_prof);
fprintf(stderr,"\trender_intent (%u)\n\t" fprintf(stderr, "\trender_intent (%u)\n\t"
"color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t" "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
" type: in(%u) out:(%u)\n", " type: in(%u) out:(%u)\n",
intent, intent,
in_space, in_space,
(in_space>>24) & 0xff,(in_space>>16) & 0xff, (in_space >> 24) & 0xff, (in_space >> 16) & 0xff,
(in_space>>8) & 0xff, in_space & 0xff, (in_space >> 8) & 0xff, in_space & 0xff,
out_space, out_space,
(out_space>>24) & 0xff,(out_space>>16) & 0xff, (out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
(out_space>>8) & 0xff, out_space & 0xff, (out_space >> 8) & 0xff, out_space & 0xff,
in_type,out_type in_type, out_type
); );
#else #else
(void)prec; (void)prec;
(void)in_space; (void)in_space;
#endif /* DEBUG_PROFILE */ #endif /* DEBUG_PROFILE */
transform = cmsCreateTransform(in_prof, in_type, transform = cmsCreateTransform(in_prof, in_type,
out_prof, out_type, intent, 0); out_prof, out_type, intent, 0);
#ifdef OPJ_HAVE_LIBLCMS2 #ifdef OPJ_HAVE_LIBLCMS2
/* Possible for: LCMS_VERSION >= 2000 :*/ /* Possible for: LCMS_VERSION >= 2000 :*/
cmsCloseProfile(in_prof); cmsCloseProfile(in_prof);
cmsCloseProfile(out_prof); cmsCloseProfile(out_prof);
#endif #endif
if(transform == NULL) if (transform == NULL)
{ {
#ifdef DEBUG_PROFILE #ifdef DEBUG_PROFILE
fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. " fprintf(stderr, "%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
"ICC Profile ignored.\n",__FILE__,__LINE__); "ICC Profile ignored.\n", __FILE__, __LINE__);
#endif #endif
image->color_space = oldspace; image->color_space = oldspace;
#ifdef OPJ_HAVE_LIBLCMS1 #ifdef OPJ_HAVE_LIBLCMS1
cmsCloseProfile(in_prof); cmsCloseProfile(in_prof);
cmsCloseProfile(out_prof); cmsCloseProfile(out_prof);
#endif #endif
return; return;
} }
if(image->numcomps > 2)/* RGB, RGBA */ if (image->numcomps > 2)/* RGB, RGBA */
{ {
if( prec <= 8 ) if (prec <= 8)
{ {
unsigned char *inbuf, *outbuf, *in, *out; unsigned char *inbuf, *outbuf, *in, *out;
max = max_w * max_h; max = max_w * max_h;
nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char); nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
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);
r = image->comps[0].data; r = image->comps[0].data;
g = image->comps[1].data; g = image->comps[1].data;
b = image->comps[2].data; b = image->comps[2].data;
for(i = 0; i < max; ++i) for (i = 0; i < max; ++i)
{ {
*in++ = (unsigned char)*r++; *in++ = (unsigned char)*r++;
*in++ = (unsigned char)*g++; *in++ = (unsigned char)*g++;
*in++ = (unsigned char)*b++; *in++ = (unsigned char)*b++;
} }
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max); cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
r = image->comps[0].data; r = image->comps[0].data;
g = image->comps[1].data; g = image->comps[1].data;
b = image->comps[2].data; b = image->comps[2].data;
for(i = 0; i < max; ++i) for (i = 0; i < max; ++i)
{ {
*r++ = (int)*out++; *r++ = (int)*out++;
*g++ = (int)*out++; *g++ = (int)*out++;
*b++ = (int)*out++; *b++ = (int)*out++;
} }
free(inbuf); free(outbuf); free(inbuf); free(outbuf);
} }
else else
{ {
unsigned short *inbuf, *outbuf, *in, *out; unsigned short *inbuf, *outbuf, *in, *out;
max = max_w * max_h; max = max_w * max_h;
nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short); nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short);
in = inbuf = (unsigned short*)malloc(nr_samples); in = inbuf = (unsigned short*)malloc(nr_samples);
out = outbuf = (unsigned short*)malloc(nr_samples); out = outbuf = (unsigned short*)malloc(nr_samples);
r = image->comps[0].data; r = image->comps[0].data;
g = image->comps[1].data; g = image->comps[1].data;
b = image->comps[2].data; b = image->comps[2].data;
for(i = 0; i < max; ++i) for (i = 0; i < max; ++i)
{ {
*in++ = (unsigned short)*r++; *in++ = (unsigned short)*r++;
*in++ = (unsigned short)*g++; *in++ = (unsigned short)*g++;
*in++ = (unsigned short)*b++; *in++ = (unsigned short)*b++;
} }
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max); cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
r = image->comps[0].data; r = image->comps[0].data;
g = image->comps[1].data; g = image->comps[1].data;
b = image->comps[2].data; b = image->comps[2].data;
for(i = 0; i < max; ++i) for (i = 0; i < max; ++i)
{ {
*r++ = (int)*out++; *r++ = (int)*out++;
*g++ = (int)*out++; *g++ = (int)*out++;
*b++ = (int)*out++; *b++ = (int)*out++;
} }
free(inbuf); free(outbuf); free(inbuf); free(outbuf);
} }
} }
else /* GRAY, GRAYA */ else /* GRAY, GRAYA */
{ {
unsigned char *in, *inbuf, *out, *outbuf; unsigned char *in, *inbuf, *out, *outbuf;
max = max_w * max_h; max = max_w * max_h;
nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char); nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char);
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];
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;
image->numcomps += 2; new_image->comps[0].data= NULL;
new_image->comps[1].data = NULL;
r = image->comps[0].data; opj_image_destroy(new_image);
new_image = NULL;
for(i = 0; i < max; ++i) image->numcomps += 2;
{
*in++ = (unsigned char)*r++;
}
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
r = image->comps[0].data; r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
for(i = 0; i < max; ++i) for (i = 0; i < max; ++i)
{ {
*r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++; *in++ = (unsigned char)*r++;
} }
free(inbuf); free(outbuf); cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
}/* if(image->numcomps */ r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
for (i = 0; i < max; ++i)
{
*r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
}
free(inbuf); free(outbuf);
}/* if(image->numcomps */
cmsDeleteTransform(transform); cmsDeleteTransform(transform);
@ -573,17 +630,17 @@ void color_cielab_to_rgb(opj_image_t *image)
numcomps = (int)image->numcomps; numcomps = (int)image->numcomps;
if(numcomps != 3) if (numcomps != 3)
{ {
fprintf(stderr,"%s:%d:\n\tnumcomps %d not handled. Quitting.\n", fprintf(stderr, "%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
__FILE__,__LINE__,numcomps); __FILE__, __LINE__, numcomps);
return; return;
} }
row = (int*)image->icc_profile_buf; row = (int*)image->icc_profile_buf;
enumcs = row[0]; enumcs = row[0];
if(enumcs == 14) /* CIELab */ if (enumcs == 14) /* CIELab */
{ {
int *L, *a, *b, *red, *green, *blue; int *L, *a, *b, *red, *green, *blue;
int *src0, *src1, *src2, *dst0, *dst1, *dst2; int *src0, *src1, *src2, *dst0, *dst1, *dst2;
@ -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();
@ -605,7 +663,7 @@ void color_cielab_to_rgb(opj_image_t *image)
cmsCloseProfile(in); cmsCloseProfile(in);
cmsCloseProfile(out); cmsCloseProfile(out);
#endif #endif
if(transform == NULL) if (transform == NULL)
{ {
#ifdef OPJ_HAVE_LIBLCMS1 #ifdef OPJ_HAVE_LIBLCMS1
cmsCloseProfile(in); cmsCloseProfile(in);
@ -619,12 +677,12 @@ void color_cielab_to_rgb(opj_image_t *image)
default_type = (unsigned int)row[1]; default_type = (unsigned int)row[1];
if(default_type == 0x44454600)/* DEF : default */ if (default_type == 0x44454600)/* DEF : default */
{ {
rl = 100; ra = 170; rb = 200; rl = 100; ra = 170; rb = 200;
ol = 0; ol = 0;
oa = pow(2, prec1 - 1); oa = pow(2, prec1 - 1);
ob = pow(2, prec2 - 2) + pow(2, prec2 - 3); ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
} }
else else
{ {
@ -638,24 +696,31 @@ 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;
minL = -(rl * ol)/(pow(2, prec0)-1); 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);
maxL = minL + rl; maxL = minL + rl;
mina = -(ra * oa)/(pow(2, prec1)-1); mina = -(ra * oa) / (pow(2, prec1) - 1);
maxa = mina + ra; maxa = mina + ra;
minb = -(rb * ob)/(pow(2, prec2)-1); minb = -(rb * ob) / (pow(2, prec2) - 1);
maxb = minb + rb; maxb = minb + rb;
for(i = 0; i < max; ++i) for (i = 0; i < max; ++i)
{ {
Lab.L = minL + (double)(*L) * (maxL - minL)/(pow(2, prec0)-1); ++L; Lab.L = minL + (double)(*L) * (maxL - minL) / (pow(2, prec0) - 1); ++L;
Lab.a = mina + (double)(*a) * (maxa - mina)/(pow(2, prec1)-1); ++a; Lab.a = mina + (double)(*a) * (maxa - mina) / (pow(2, prec1) - 1); ++a;
Lab.b = minb + (double)(*b) * (maxb - minb)/(pow(2, prec2)-1); ++b; Lab.b = minb + (double)(*b) * (maxb - minb) / (pow(2, prec2) - 1); ++b;
cmsDoTransform(transform, &Lab, RGB, 1); cmsDoTransform(transform, &Lab, RGB, 1);
@ -681,7 +746,7 @@ void color_cielab_to_rgb(opj_image_t *image)
return; return;
} }
fprintf(stderr,"%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,__LINE__, enumcs); fprintf(stderr, "%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__, __LINE__, enumcs);
}/* color_apply_conversion() */ }/* color_apply_conversion() */
#endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */ #endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */
@ -695,7 +760,7 @@ void color_cmyk_to_rgb(opj_image_t *image)
w = image->comps[0].w; w = image->comps[0].w;
h = image->comps[0].h; h = image->comps[0].h;
if(image->numcomps < 4) return; if (image->numcomps < 4) return;
max = w * h; max = w * h;
@ -704,7 +769,7 @@ void color_cmyk_to_rgb(opj_image_t *image)
sY = 1.0F / (float)((1 << image->comps[2].prec) - 1); sY = 1.0F / (float)((1 << image->comps[2].prec) - 1);
sK = 1.0F / (float)((1 << image->comps[3].prec) - 1); sK = 1.0F / (float)((1 << image->comps[3].prec) - 1);
for(i = 0; i < max; ++i) for (i = 0; i < max; ++i)
{ {
/* CMYK values from 0 to 1 */ /* CMYK values from 0 to 1 */
C = (float)(image->comps[0].data[i]) * sC; C = (float)(image->comps[0].data[i]) * sC;
@ -724,7 +789,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 */
} }
opj_image_single_component_data_free(image->comps+3); opj_image_single_component_data_free(image->comps + 3);
image->comps[0].prec = 8; image->comps[0].prec = 8;
image->comps[1].prec = 8; image->comps[1].prec = 8;
image->comps[2].prec = 8; image->comps[2].prec = 8;
@ -732,7 +797,7 @@ void color_cmyk_to_rgb(opj_image_t *image)
image->color_space = OPJ_CLRSPC_SRGB; image->color_space = OPJ_CLRSPC_SRGB;
for (i = 3; i < image->numcomps; ++i) { for (i = 3; i < image->numcomps; ++i) {
memcpy(&(image->comps[i]), &(image->comps[i+1]), sizeof(image->comps[i])); memcpy(&(image->comps[i]), &(image->comps[i + 1]), sizeof(image->comps[i]));
} }
}/* color_cmyk_to_rgb() */ }/* color_cmyk_to_rgb() */
@ -744,10 +809,10 @@ void color_esycc_to_rgb(opj_image_t *image)
{ {
int y, cb, cr, sign1, sign2, val; int y, cb, cr, sign1, sign2, val;
unsigned int w, h, max, i; unsigned int w, h, max, i;
int flip_value = (1 << (image->comps[0].prec-1)); int flip_value = (1 << (image->comps[0].prec - 1));
int max_value = (1 << image->comps[0].prec) - 1; int max_value = (1 << image->comps[0].prec) - 1;
if(image->numcomps < 3) return; if (image->numcomps < 3) return;
w = image->comps[0].w; w = image->comps[0].w;
h = image->comps[0].h; h = image->comps[0].h;
@ -757,33 +822,33 @@ void color_esycc_to_rgb(opj_image_t *image)
max = w * h; max = w * h;
for(i = 0; i < max; ++i) for (i = 0; i < max; ++i)
{ {
y = image->comps[0].data[i]; cb = image->comps[1].data[i]; cr = image->comps[2].data[i]; y = image->comps[0].data[i]; cb = image->comps[1].data[i]; cr = image->comps[2].data[i];
if( !sign1) cb -= flip_value; if (!sign1) cb -= flip_value;
if( !sign2) cr -= flip_value; if (!sign2) cr -= flip_value;
val = (int) val = (int)
((float)y - (float)0.0000368 * (float)cb ((float)y - (float)0.0000368 * (float)cb
+ (float)1.40199 * (float)cr + (float)0.5); + (float)1.40199 * (float)cr + (float)0.5);
if(val > max_value) val = max_value; else if(val < 0) val = 0; if (val > max_value) val = max_value; else if (val < 0) val = 0;
image->comps[0].data[i] = val; image->comps[0].data[i] = val;
val = (int) val = (int)
((float)1.0003 * (float)y - (float)0.344125 * (float)cb ((float)1.0003 * (float)y - (float)0.344125 * (float)cb
- (float)0.7141128 * (float)cr + (float)0.5); - (float)0.7141128 * (float)cr + (float)0.5);
if(val > max_value) val = max_value; else if(val < 0) val = 0; if (val > max_value) val = max_value; else if (val < 0) val = 0;
image->comps[1].data[i] = val; image->comps[1].data[i] = val;
val = (int) val = (int)
((float)0.999823 * (float)y + (float)1.77204 * (float)cb ((float)0.999823 * (float)y + (float)1.77204 * (float)cb
- (float)0.000008 *(float)cr + (float)0.5); - (float)0.000008 *(float)cr + (float)0.5);
if(val > max_value) val = max_value; else if(val < 0) val = 0; if (val > max_value) val = max_value; else if (val < 0) val = 0;
image->comps[2].data[i] = val; image->comps[2].data[i] = val;
} }
image->color_space = OPJ_CLRSPC_SRGB; image->color_space = OPJ_CLRSPC_SRGB;

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;