Update color_apply_icc_profile

This commit is contained in:
mayeut 2015-10-18 16:24:51 +02:00
parent 7ad3e2a870
commit 66f65919cf
1 changed files with 196 additions and 148 deletions

View File

@ -286,178 +286,226 @@ 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;
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
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); in_space = cmsGetPCS(in_prof);
(void)in_space;
out_space = cmsGetColorSpace(in_prof); out_space = cmsGetColorSpace(in_prof);
intent = cmsGetHeaderRenderingIntent(in_prof); intent = cmsGetHeaderRenderingIntent(in_prof);
max_w = image->comps[0].w; max_h = image->comps[0].h;
prec = image->comps[0].prec; max_w = (int)image->comps[0].w;
(void)prec; max_h = (int)image->comps[0].h;
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 */
{ {
in_type = TYPE_RGB_16; if( prec <= 8 )
out_type = TYPE_RGB_16; {
out_prof = cmsCreate_sRGBProfile(); in_type = TYPE_RGB_8;
image->color_space = CLRSPC_SRGB; out_type = TYPE_RGB_8;
} }
else
{
in_type = TYPE_RGB_16;
out_type = TYPE_RGB_16;
}
out_prof = cmsCreate_sRGBProfile();
image->color_space = 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 = 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 = CLRSPC_SRGB;
}
else else
if(out_space == cmsSigGrayData) /* enumCS 17 */ {
{
in_type = TYPE_GRAY_8;
out_type = TYPE_RGB_8;
out_prof = cmsCreate_sRGBProfile();
image->color_space = 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 = 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
(void)prec;
(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 HAVE_LIBLCMS2 #ifdef 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 HAVE_LIBLCMS1 #ifdef 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 */
{ {
unsigned short *inbuf, *outbuf, *in, *out; if( prec <= 8 )
max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned short); {
in = inbuf = (unsigned short*)malloc(nr_samples); unsigned char *inbuf, *outbuf, *in, *out;
out = outbuf = (unsigned short*)malloc(nr_samples); max = max_w * max_h;
nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
r = image->comps[0].data; in = inbuf = (unsigned char*)malloc(nr_samples);
g = image->comps[1].data; out = outbuf = (unsigned char*)malloc(nr_samples);
b = image->comps[2].data;
r = image->comps[0].data;
for(i = 0; i < max; ++i) g = image->comps[1].data;
{ b = image->comps[2].data;
*in++ = (unsigned short)*r++;
*in++ = (unsigned short)*g++; for(i = 0; i < max; ++i)
*in++ = (unsigned short)*b++; {
} *in++ = (unsigned char)*r++;
*in++ = (unsigned char)*g++;
cmsDoTransform(transform, inbuf, outbuf, max); *in++ = (unsigned char)*b++;
}
r = image->comps[0].data;
g = image->comps[1].data; cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
b = image->comps[2].data;
r = image->comps[0].data;
for(i = 0; i < max; ++i) g = image->comps[1].data;
{ b = image->comps[2].data;
*r++ = (int)*out++;
*g++ = (int)*out++; for(i = 0; i < max; ++i)
*b++ = (int)*out++; {
} *r++ = (int)*out++;
free(inbuf); free(outbuf); *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;
for(i = 0; i < max; ++i)
{
*in++ = (unsigned short)*r++;
*in++ = (unsigned short)*g++;
*in++ = (unsigned short)*b++;
}
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
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 /* 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 = 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*) image->comps = (opj_image_comp_t*)realloc(image->comps, (image->numcomps+2)*sizeof(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 = (int*)calloc(max, sizeof(int)); image->comps[2].data = (int*)calloc((size_t)max, sizeof(int));
image->comps[2].data = (int*)calloc(max, sizeof(int));
image->numcomps += 2;
image->numcomps += 2;
r = image->comps[0].data;
r = image->comps[0].data;
for(i = 0; i < max; ++i)
for(i = 0; i < max; ++i) {
{ *in++ = (unsigned char)*r++;
*in++ = (unsigned char)*r++; }
} cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
cmsDoTransform(transform, inbuf, outbuf, 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++; *g++ = (int)*out++; *b++ = (int)*out++;
*r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++; }
} free(inbuf); free(outbuf);
free(inbuf); free(outbuf);
}/* if(image->numcomps */
}/* if(image->numcomps */
cmsDeleteTransform(transform); cmsDeleteTransform(transform);
#ifdef HAVE_LIBLCMS1 #ifdef HAVE_LIBLCMS1
cmsCloseProfile(in_prof); cmsCloseProfile(in_prof);
cmsCloseProfile(out_prof); cmsCloseProfile(out_prof);