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

@ -1,6 +1,6 @@
/*
* 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
* 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.
*
@ -8,7 +8,7 @@
* Copyright (c) 2002-2014, Professor Benoit Macq
* Copyright (c) 2001-2003, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2003-2007, Francois-Olivier Devaux
* Copyright (c) 2003-2007, Francois-Olivier Devaux
* Copyright (c) 2003-2014, Antonin Descampe
* Copyright (c) 2005, Herve Drolon, FreeImage Team
* All rights reserved.
@ -57,6 +57,31 @@
#define OPJ_CLRSPC_SRGB CLRSPC_SRGB
#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
@ -78,13 +103,13 @@ static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
cb -= offset; cr -= offset;
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);
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);
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)
@ -93,22 +118,31 @@ static void sycc444_to_rgb(opj_image_t *img)
const int *y, *cb, *cr;
unsigned int maxw, maxh, max, i;
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;
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;
y = img->comps[0].data;
cb = img->comps[1].data;
cr = img->comps[2].data;
d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
d0 = r = new_image->comps[0].data;
d1 = g = new_image->comps[1].data;
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);
++y; ++cb; ++cr; ++r; ++g; ++b;
@ -116,35 +150,43 @@ static void sycc444_to_rgb(opj_image_t *img)
opj_image_all_components_data_free(img);
img->comps[0].data = d0;
img->comps[1].data = d1;
img->comps[2].data = d2;
img->comps[2].data = d2;
}/* sycc444_to_rgb() */
static void sycc422_to_rgb(opj_image_t *img)
{
{
int *d0, *d1, *d2, *r, *g, *b;
const int *y, *cb, *cr;
unsigned int maxw, maxh, max;
unsigned int maxw, maxh;
int offset, upb;
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;
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;
max = maxw * maxh;
maxw = (unsigned int)img->comps[0].w;
maxh = (unsigned int)img->comps[0].h;
y = img->comps[0].data;
cb = img->comps[1].data;
cr = img->comps[2].data;
d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
d0 = r = new_image->comps[0].data;
d1 = g = new_image->comps[1].data;
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);
++y; ++r; ++g; ++b;
@ -157,9 +199,9 @@ static void sycc422_to_rgb(opj_image_t *img)
}
}
opj_image_all_components_data_free(img);
img->comps[0].data = d0;
img->comps[1].data = d1;
img->comps[2].data = d2;
img->comps[0].data = d0;
img->comps[1].data = d1;
img->comps[2].data = d2;
#if defined(USE_JPWL) || defined(USE_MJ2)
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;
const int *y, *cb, *cr, *ny;
unsigned int maxw, maxh, max;
unsigned int maxw, maxh;
int offset, upb;
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;
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;
max = maxw * maxh;
maxw = (unsigned int)img->comps[0].w;
maxh = (unsigned int)img->comps[0].h;
y = img->comps[0].data;
cb = img->comps[1].data;
cr = img->comps[2].data;
d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
d0 = r = new_image->comps[0].data;
d1 = g = new_image->comps[1].data;
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;
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);
++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);
++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
}
if(j < maxw)
if (j < maxw)
{
sycc_to_rgb(offset, upb, *y, *cb, *cr, 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;
}
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);
@ -236,7 +286,7 @@ static void sycc420_to_rgb(opj_image_t *img)
++y; ++r; ++g; ++b; ++cb; ++cr;
}
if(j < maxw)
if (j < maxw)
{
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);
img->comps[0].data = d0;
img->comps[1].data = d1;
img->comps[2].data = d2;
img->comps[2].data = d2;
#if defined(USE_JPWL) || defined(USE_MJ2)
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)
{
if(img->numcomps < 3)
if (img->numcomps < 3)
{
img->color_space = OPJ_CLRSPC_GRAY;
return;
}
if((img->comps[0].dx == 1)
&& (img->comps[1].dx == 2)
&& (img->comps[2].dx == 2)
&& (img->comps[0].dy == 1)
&& (img->comps[1].dy == 2)
&& (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */
{
if ((img->comps[0].dx == 1)
&& (img->comps[1].dx == 2)
&& (img->comps[2].dx == 2)
&& (img->comps[0].dy == 1)
&& (img->comps[1].dy == 2)
&& (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */
{
sycc420_to_rgb(img);
}
}
else
if((img->comps[0].dx == 1)
&& (img->comps[1].dx == 2)
&& (img->comps[2].dx == 2)
&& (img->comps[0].dy == 1)
&& (img->comps[1].dy == 1)
&& (img->comps[2].dy == 1))/* horizontal sub-sample only */
{
sycc422_to_rgb(img);
}
else
if((img->comps[0].dx == 1)
&& (img->comps[1].dx == 1)
&& (img->comps[2].dx == 1)
&& (img->comps[0].dy == 1)
&& (img->comps[1].dy == 1)
&& (img->comps[2].dy == 1))/* no sub-sample */
{
sycc444_to_rgb(img);
}
else
{
fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,__LINE__);
return;
}
if ((img->comps[0].dx == 1)
&& (img->comps[1].dx == 2)
&& (img->comps[2].dx == 2)
&& (img->comps[0].dy == 1)
&& (img->comps[1].dy == 1)
&& (img->comps[2].dy == 1))/* horizontal sub-sample only */
{
sycc422_to_rgb(img);
}
else
if ((img->comps[0].dx == 1)
&& (img->comps[1].dx == 1)
&& (img->comps[2].dx == 1)
&& (img->comps[0].dy == 1)
&& (img->comps[1].dy == 1)
&& (img->comps[2].dy == 1))/* no sub-sample */
{
sycc444_to_rgb(img);
}
else
{
fprintf(stderr, "%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__, __LINE__);
return;
}
img->color_space = OPJ_CLRSPC_SRGB;
}/* color_sycc_to_rgb() */
@ -334,227 +384,234 @@ void color_apply_icc_profile(opj_image_t *image)
int *r, *g, *b;
int prec, i, max, max_w, max_h;
OPJ_COLOR_SPACE oldspace;
opj_image_t* new_image = NULL;
in_prof =
cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
in_prof =
cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
#ifdef DEBUG_PROFILE
FILE *icm = fopen("debug.icm","wb");
fwrite( image->icc_profile_buf,1, image->icc_profile_len,icm);
fclose(icm);
FILE *icm = fopen("debug.icm", "wb");
fwrite(image->icc_profile_buf, 1, image->icc_profile_len, icm);
fclose(icm);
#endif
if(in_prof == NULL) return;
if (in_prof == NULL) return;
in_space = cmsGetPCS(in_prof);
out_space = cmsGetColorSpace(in_prof);
intent = cmsGetHeaderRenderingIntent(in_prof);
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;
oldspace = image->color_space;
if(out_space == cmsSigRgbData) /* enumCS 16 */
{
if( prec <= 8 )
{
in_type = TYPE_RGB_8;
out_type = TYPE_RGB_8;
}
if (out_space == cmsSigRgbData) /* enumCS 16 */
{
if (prec <= 8)
{
in_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
{
in_type = TYPE_RGB_16;
out_type = TYPE_RGB_16;
}
out_prof = cmsCreate_sRGBProfile();
image->color_space = OPJ_CLRSPC_SRGB;
}
else
if(out_space == cmsSigGrayData) /* enumCS 17 */
{
in_type = TYPE_GRAY_8;
out_type = TYPE_RGB_8;
out_prof = cmsCreate_sRGBProfile();
image->color_space = OPJ_CLRSPC_SRGB;
}
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
{
if (out_space == cmsSigGrayData) /* enumCS 17 */
{
in_type = TYPE_GRAY_8;
out_type = TYPE_RGB_8;
out_prof = cmsCreate_sRGBProfile();
image->color_space = OPJ_CLRSPC_SRGB;
}
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
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",
__FILE__,__LINE__,out_space,
(out_space>>24) & 0xff,(out_space>>16) & 0xff,
(out_space>>8) & 0xff, out_space & 0xff);
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",
__FILE__, __LINE__, out_space,
(out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
(out_space >> 8) & 0xff, out_space & 0xff);
#endif
return;
}
return;
}
#ifdef DEBUG_PROFILE
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,
max_w,max_h, (void*)in_prof,(void*)out_prof);
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,
max_w, max_h, (void*)in_prof, (void*)out_prof);
fprintf(stderr,"\trender_intent (%u)\n\t"
"color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
" type: in(%u) out:(%u)\n",
intent,
in_space,
(in_space>>24) & 0xff,(in_space>>16) & 0xff,
(in_space>>8) & 0xff, in_space & 0xff,
fprintf(stderr, "\trender_intent (%u)\n\t"
"color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
" type: in(%u) out:(%u)\n",
intent,
in_space,
(in_space >> 24) & 0xff, (in_space >> 16) & 0xff,
(in_space >> 8) & 0xff, in_space & 0xff,
out_space,
(out_space>>24) & 0xff,(out_space>>16) & 0xff,
(out_space>>8) & 0xff, out_space & 0xff,
out_space,
(out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
(out_space >> 8) & 0xff, out_space & 0xff,
in_type,out_type
);
in_type, out_type
);
#else
(void)prec;
(void)in_space;
(void)prec;
(void)in_space;
#endif /* DEBUG_PROFILE */
transform = cmsCreateTransform(in_prof, in_type,
out_prof, out_type, intent, 0);
out_prof, out_type, intent, 0);
#ifdef OPJ_HAVE_LIBLCMS2
/* Possible for: LCMS_VERSION >= 2000 :*/
/* Possible for: LCMS_VERSION >= 2000 :*/
cmsCloseProfile(in_prof);
cmsCloseProfile(out_prof);
#endif
if(transform == NULL)
{
if (transform == NULL)
{
#ifdef DEBUG_PROFILE
fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
"ICC Profile ignored.\n",__FILE__,__LINE__);
fprintf(stderr, "%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
"ICC Profile ignored.\n", __FILE__, __LINE__);
#endif
image->color_space = oldspace;
image->color_space = oldspace;
#ifdef OPJ_HAVE_LIBLCMS1
cmsCloseProfile(in_prof);
cmsCloseProfile(out_prof);
cmsCloseProfile(in_prof);
cmsCloseProfile(out_prof);
#endif
return;
}
return;
}
if(image->numcomps > 2)/* RGB, RGBA */
{
if( prec <= 8 )
{
unsigned char *inbuf, *outbuf, *in, *out;
max = max_w * max_h;
nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
in = inbuf = (unsigned char*)malloc(nr_samples);
out = outbuf = (unsigned char*)malloc(nr_samples);
if (image->numcomps > 2)/* RGB, RGBA */
{
if (prec <= 8)
{
unsigned char *inbuf, *outbuf, *in, *out;
max = max_w * max_h;
nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
in = inbuf = (unsigned char*)malloc(nr_samples);
out = outbuf = (unsigned char*)malloc(nr_samples);
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
for(i = 0; i < max; ++i)
{
*in++ = (unsigned char)*r++;
*in++ = (unsigned char)*g++;
*in++ = (unsigned char)*b++;
}
for (i = 0; i < max; ++i)
{
*in++ = (unsigned char)*r++;
*in++ = (unsigned char)*g++;
*in++ = (unsigned char)*b++;
}
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
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);
}
else
{
unsigned short *inbuf, *outbuf, *in, *out;
max = max_w * max_h;
nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short);
in = inbuf = (unsigned short*)malloc(nr_samples);
out = outbuf = (unsigned short*)malloc(nr_samples);
for (i = 0; i < max; ++i)
{
*r++ = (int)*out++;
*g++ = (int)*out++;
*b++ = (int)*out++;
}
free(inbuf); free(outbuf);
}
else
{
unsigned short *inbuf, *outbuf, *in, *out;
max = max_w * max_h;
nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short);
in = inbuf = (unsigned short*)malloc(nr_samples);
out = outbuf = (unsigned short*)malloc(nr_samples);
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
for(i = 0; i < max; ++i)
{
*in++ = (unsigned short)*r++;
*in++ = (unsigned short)*g++;
*in++ = (unsigned short)*b++;
}
for (i = 0; i < max; ++i)
{
*in++ = (unsigned short)*r++;
*in++ = (unsigned short)*g++;
*in++ = (unsigned short)*b++;
}
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
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);
}
}
for (i = 0; i < max; ++i)
{
*r++ = (int)*out++;
*g++ = (int)*out++;
*b++ = (int)*out++;
}
free(inbuf); free(outbuf);
}
}
else /* GRAY, GRAYA */
{
unsigned char *in, *inbuf, *out, *outbuf;
max = max_w * max_h;
nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char);
in = inbuf = (unsigned char*)malloc(nr_samples);
out = outbuf = (unsigned char*)malloc(nr_samples);
{
unsigned char *in, *inbuf, *out, *outbuf;
max = max_w * max_h;
nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char);
in = inbuf = (unsigned char*)malloc(nr_samples);
out = outbuf = (unsigned char*)malloc(nr_samples);
image->comps = (opj_image_comp_t*)
realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
new_image = image_create(2, image->comps[0].w, image->comps[0].h, image->comps[0].prec);
image->comps = (opj_image_comp_t*)realloc(image->comps, (image->numcomps + 2)*sizeof(opj_image_comp_t));
if(image->numcomps == 2)
image->comps[3] = image->comps[1];
if (image->numcomps == 2)
image->comps[3] = image->comps[1];
image->comps[1] = image->comps[0];
image->comps[2] = image->comps[0];
image->comps[1] = image->comps[0];
image->comps[2] = image->comps[0];
image->comps[1].data = (int*)calloc((size_t)max, sizeof(int));
image->comps[2].data = (int*)calloc((size_t)max, sizeof(int));
image->comps[1].data = new_image->comps[0].data;
image->comps[2].data = new_image->comps[1].data;
image->numcomps += 2;
new_image->comps[0].data= NULL;
new_image->comps[1].data = NULL;
opj_image_destroy(new_image);
new_image = NULL;
r = image->comps[0].data;
image->numcomps += 2;
for(i = 0; i < max; ++i)
{
*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)
{
*in++ = (unsigned char)*r++;
}
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
for(i = 0; i < max; ++i)
{
*r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
}
free(inbuf); free(outbuf);
r = image->comps[0].data;
g = image->comps[1].data;
b = image->comps[2].data;
}/* if(image->numcomps */
for (i = 0; i < max; ++i)
{
*r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
}
free(inbuf); free(outbuf);
}/* if(image->numcomps */
cmsDeleteTransform(transform);
@ -568,22 +625,22 @@ void color_cielab_to_rgb(opj_image_t *image)
{
int *row;
int enumcs, numcomps;
image->color_space = OPJ_CLRSPC_SRGB;
numcomps = (int)image->numcomps;
if(numcomps != 3)
if (numcomps != 3)
{
fprintf(stderr,"%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
__FILE__,__LINE__,numcomps);
fprintf(stderr, "%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
__FILE__, __LINE__, numcomps);
return;
}
row = (int*)image->icc_profile_buf;
enumcs = row[0];
if(enumcs == 14) /* CIELab */
if (enumcs == 14) /* CIELab */
{
int *L, *a, *b, *red, *green, *blue;
int *src0, *src1, *src2, *dst0, *dst1, *dst2;
@ -595,17 +652,18 @@ void color_cielab_to_rgb(opj_image_t *image)
cmsHTRANSFORM transform;
cmsUInt16Number RGB[3];
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);
out = cmsCreate_sRGBProfile();
transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16, INTENT_PERCEPTUAL, 0);
#ifdef OPJ_HAVE_LIBLCMS2
cmsCloseProfile(in);
cmsCloseProfile(out);
#endif
if(transform == NULL)
if (transform == NULL)
{
#ifdef OPJ_HAVE_LIBLCMS1
cmsCloseProfile(in);
@ -616,49 +674,56 @@ void color_cielab_to_rgb(opj_image_t *image)
prec0 = (double)image->comps[0].prec;
prec1 = (double)image->comps[1].prec;
prec2 = (double)image->comps[2].prec;
default_type = (unsigned int)row[1];
if(default_type == 0x44454600)/* DEF : default */
if (default_type == 0x44454600)/* DEF : default */
{
rl = 100; ra = 170; rb = 200;
ol = 0;
oa = pow(2, prec1 - 1);
ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
}
else
{
rl = row[2]; ra = row[4]; rb = row[6];
ol = row[3]; oa = row[5]; ob = row[7];
}
L = src0 = image->comps[0].data;
a = src1 = image->comps[1].data;
b = src2 = image->comps[2].data;
max = image->comps[0].w * image->comps[0].h;
red = dst0 = (int*)malloc(max * sizeof(int));
green = dst1 = (int*)malloc(max * sizeof(int));
blue = dst2 = (int*)malloc(max * sizeof(int));
minL = -(rl * ol)/(pow(2, prec0)-1);
red = dst0 = new_image->comps[0].data;
green = dst1 = new_image->comps[1].data;
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);
maxL = minL + rl;
mina = -(ra * oa)/(pow(2, prec1)-1);
mina = -(ra * oa) / (pow(2, prec1) - 1);
maxa = mina + ra;
minb = -(rb * ob)/(pow(2, prec2)-1);
minb = -(rb * ob) / (pow(2, prec2) - 1);
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.a = mina + (double)(*a) * (maxa - mina)/(pow(2, prec1)-1); ++a;
Lab.b = minb + (double)(*b) * (maxb - minb)/(pow(2, prec2)-1); ++b;
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.b = minb + (double)(*b) * (maxb - minb) / (pow(2, prec2) - 1); ++b;
cmsDoTransform(transform, &Lab, RGB, 1);
*red++ = RGB[0];
*green++ = RGB[1];
*blue++ = RGB[2];
@ -672,16 +737,16 @@ void color_cielab_to_rgb(opj_image_t *image)
image->comps[0].data = dst0;
image->comps[1].data = dst1;
image->comps[2].data = dst2;
image->color_space = OPJ_CLRSPC_SRGB;
image->comps[0].prec = 16;
image->comps[1].prec = 16;
image->comps[2].prec = 16;
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() */
#endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */
@ -695,23 +760,23 @@ void color_cmyk_to_rgb(opj_image_t *image)
w = image->comps[0].w;
h = image->comps[0].h;
if(image->numcomps < 4) return;
if (image->numcomps < 4) return;
max = w * h;
sC = 1.0F / (float)((1 << image->comps[0].prec) - 1);
sM = 1.0F / (float)((1 << image->comps[1].prec) - 1);
sY = 1.0F / (float)((1 << image->comps[2].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 */
C = (float)(image->comps[0].data[i]) * sC;
M = (float)(image->comps[1].data[i]) * sM;
Y = (float)(image->comps[2].data[i]) * sY;
K = (float)(image->comps[3].data[i]) * sK;
/* Invert all CMYK values */
C = 1.0F - C;
M = 1.0F - M;
@ -724,15 +789,15 @@ void color_cmyk_to_rgb(opj_image_t *image)
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[1].prec = 8;
image->comps[2].prec = 8;
image->numcomps -= 1;
image->color_space = OPJ_CLRSPC_SRGB;
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() */
@ -744,46 +809,46 @@ void color_esycc_to_rgb(opj_image_t *image)
{
int y, cb, cr, sign1, sign2, val;
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;
if(image->numcomps < 3) return;
if (image->numcomps < 3) return;
w = image->comps[0].w;
h = image->comps[0].h;
sign1 = (int)image->comps[1].sgnd;
sign2 = (int)image->comps[2].sgnd;
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];
if( !sign1) cb -= flip_value;
if( !sign2) cr -= flip_value;
if (!sign1) cb -= flip_value;
if (!sign2) cr -= flip_value;
val = (int)
((float)y - (float)0.0000368 * (float)cb
+ (float)1.40199 * (float)cr + (float)0.5);
if(val > max_value) val = max_value; else if(val < 0) val = 0;
((float)y - (float)0.0000368 * (float)cb
+ (float)1.40199 * (float)cr + (float)0.5);
if (val > max_value) val = max_value; else if (val < 0) val = 0;
image->comps[0].data[i] = val;
val = (int)
((float)1.0003 * (float)y - (float)0.344125 * (float)cb
- (float)0.7141128 * (float)cr + (float)0.5);
if(val > max_value) val = max_value; else if(val < 0) val = 0;
((float)1.0003 * (float)y - (float)0.344125 * (float)cb
- (float)0.7141128 * (float)cr + (float)0.5);
if (val > max_value) val = max_value; else if (val < 0) val = 0;
image->comps[1].data[i] = val;
val = (int)
((float)0.999823 * (float)y + (float)1.77204 * (float)cb
- (float)0.000008 *(float)cr + (float)0.5);
if(val > max_value) val = max_value; else if(val < 0) val = 0;
((float)0.999823 * (float)y + (float)1.77204 * (float)cb
- (float)0.000008 *(float)cr + (float)0.5);
if (val > max_value) val = max_value; else if (val < 0) val = 0;
image->comps[2].data[i] = val;
}
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->bpp = cmptparms[compno].bpp;
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) {
/* TODO replace with event manager, breaks API */
/* 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;
if (dest_comp->data) {
if (dest_comp->aligned_data)
opj_aligned_free(dest_comp->data);
else
opj_free(dest_comp->data);
opj_aligned_free(dest_comp->data);
}
dest_comp->data = src_comp->data;
dest_comp->aligned_data = src_comp->aligned_data;
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_image_comp_t* comp = p_j2k->m_output_image->comps + compno;
comp->data = tilec->data;
comp->aligned_data = OPJ_TRUE;
tilec->data = NULL;
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 */
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) {
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;
}
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: */
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) {
opj_free(new_comps);
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)
return;
if (comp->data) {
if (comp->aligned_data)
opj_aligned_free(comp->data);
else
opj_free(comp->data);
opj_aligned_free(comp->data);
comp->data = NULL;
}
comp->aligned_data = OPJ_FALSE;
}

View File

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