51 #ifdef OPJ_HAVE_LIBLCMS2 54 #ifdef OPJ_HAVE_LIBLCMS1 59 #define OPJ_CLRSPC_GRAY CLRSPC_GRAY 60 #define OPJ_CLRSPC_SRGB CLRSPC_SRGB 77 static void sycc_to_rgb(
int offset,
int upb,
int y,
int cb,
int cr,
78 int *out_r,
int *out_g,
int *out_b)
82 cb -= offset; cr -= offset;
83 r =
y + (int)(1.402 * (
float)cr);
84 if(r < 0) r = 0;
else if(r > upb) r = upb; *out_r = r;
86 g =
y - (int)(0.344 * (
float)cb + 0.714 * (float)cr);
87 if(g < 0) g = 0;
else if(g > upb) g = upb; *out_g = g;
89 b =
y + (int)(1.772 * (
float)cb);
90 if(b < 0) b = 0;
else if(b > upb) b = upb; *out_b = b;
93 static void sycc444_to_rgb(opj_image_t *img)
95 int *d0, *d1, *d2, *r, *g, *b;
96 const int *
y, *cb, *cr;
97 int maxw, maxh,
max, i, offset, upb;
99 i = (int)img->comps[0].prec;
100 offset = 1<<(i - 1); upb = (1<<i)-1;
102 maxw = (int)img->comps[0].w; maxh = (
int)img->comps[0].h;
105 y = img->comps[0].data;
106 cb = img->comps[1].data;
107 cr = img->comps[2].data;
109 d0 = r = (
int*)malloc(
sizeof(
int) * (size_t)
max);
110 d1 = g = (
int*)malloc(
sizeof(
int) * (size_t)
max);
111 d2 = b = (
int*)malloc(
sizeof(
int) * (size_t)
max);
113 for(i = 0; i <
max; ++i)
115 sycc_to_rgb(offset, upb, *
y, *cb, *cr, r, g, b);
117 ++
y; ++cb; ++cr; ++r; ++g; ++b;
119 free(img->comps[0].data); img->comps[0].data = d0;
120 free(img->comps[1].data); img->comps[1].data = d1;
121 free(img->comps[2].data); img->comps[2].data = d2;
125 static void sycc422_to_rgb(opj_image_t *img)
127 int *d0, *d1, *d2, *r, *g, *b;
128 const int *
y, *cb, *cr;
129 int maxw, maxh,
max, offset, upb;
132 i = (int)img->comps[0].prec;
133 offset = 1<<(i - 1); upb = (1<<i)-1;
135 maxw = (int)img->comps[0].w; maxh = (
int)img->comps[0].h;
138 y = img->comps[0].data;
139 cb = img->comps[1].data;
140 cr = img->comps[2].data;
142 d0 = r = (
int*)malloc(
sizeof(
int) * (size_t)
max);
143 d1 = g = (
int*)malloc(
sizeof(
int) * (size_t)
max);
144 d2 = b = (
int*)malloc(
sizeof(
int) * (size_t)
max);
146 for(i=0; i < maxh; ++i)
148 for(j=0; j < maxw; j += 2)
150 sycc_to_rgb(offset, upb, *
y, *cb, *cr, r, g, b);
154 sycc_to_rgb(offset, upb, *
y, *cb, *cr, r, g, b);
156 ++
y; ++r; ++g; ++b; ++cb; ++cr;
159 free(img->comps[0].data); img->comps[0].data = d0;
160 free(img->comps[1].data); img->comps[1].data = d1;
161 free(img->comps[2].data); img->comps[2].data = d2;
163 #if defined(USE_JPWL) || defined(USE_MJ2) 164 img->comps[1].w = maxw; img->comps[1].h = maxh;
165 img->comps[2].w = maxw; img->comps[2].h = maxh;
167 img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
168 img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
170 img->comps[1].dx = img->comps[0].dx;
171 img->comps[2].dx = img->comps[0].dx;
172 img->comps[1].dy = img->comps[0].dy;
173 img->comps[2].dy = img->comps[0].dy;
177 static void sycc420_to_rgb(opj_image_t *img)
179 int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
180 const int *
y, *cb, *cr, *ny;
181 int maxw, maxh,
max, offset, upb;
184 i = (int)img->comps[0].prec;
185 offset = 1<<(i - 1); upb = (1<<i)-1;
187 maxw = (int)img->comps[0].w; maxh = (
int)img->comps[0].h;
190 y = img->comps[0].data;
191 cb = img->comps[1].data;
192 cr = img->comps[2].data;
194 d0 = r = (
int*)malloc(
sizeof(
int) * (size_t)
max);
195 d1 = g = (
int*)malloc(
sizeof(
int) * (size_t)
max);
196 d2 = b = (
int*)malloc(
sizeof(
int) * (size_t)
max);
198 for(i=0; i < maxh; i += 2)
201 nr = r + maxw; ng = g + maxw; nb = b + maxw;
203 for(j=0; j < maxw; j += 2)
205 sycc_to_rgb(offset, upb, *
y, *cb, *cr, r, g, b);
209 sycc_to_rgb(offset, upb, *
y, *cb, *cr, r, g, b);
213 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
215 ++ny; ++nr; ++ng; ++nb;
217 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
219 ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
221 y += maxw; r += maxw; g += maxw; b += maxw;
223 free(img->comps[0].data); img->comps[0].data = d0;
224 free(img->comps[1].data); img->comps[1].data = d1;
225 free(img->comps[2].data); img->comps[2].data = d2;
227 #if defined(USE_JPWL) || defined(USE_MJ2) 228 img->comps[1].w = maxw; img->comps[1].h = maxh;
229 img->comps[2].w = maxw; img->comps[2].h = maxh;
231 img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
232 img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
234 img->comps[1].dx = img->comps[0].dx;
235 img->comps[2].dx = img->comps[0].dx;
236 img->comps[1].dy = img->comps[0].dy;
237 img->comps[2].dy = img->comps[0].dy;
243 if(img->numcomps < 3)
245 img->color_space = OPJ_CLRSPC_GRAY;
249 if((img->comps[0].dx == 1)
250 && (img->comps[1].dx == 2)
251 && (img->comps[2].dx == 2)
252 && (img->comps[0].dy == 1)
253 && (img->comps[1].dy == 2)
254 && (img->comps[2].dy == 2))
259 if((img->comps[0].dx == 1)
260 && (img->comps[1].dx == 2)
261 && (img->comps[2].dx == 2)
262 && (img->comps[0].dy == 1)
263 && (img->comps[1].dy == 1)
264 && (img->comps[2].dy == 1))
269 if((img->comps[0].dx == 1)
270 && (img->comps[1].dx == 1)
271 && (img->comps[2].dx == 1)
272 && (img->comps[0].dy == 1)
273 && (img->comps[1].dy == 1)
274 && (img->comps[2].dy == 1))
280 fprintf(stderr,
"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n",
284 img->color_space = OPJ_CLRSPC_SRGB;
288 #if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1) 289 #ifdef OPJ_HAVE_LIBLCMS1 291 #define cmsSigXYZData icSigXYZData 292 #define cmsSigLabData icSigLabData 293 #define cmsSigCmykData icSigCmykData 294 #define cmsSigYCbCrData icSigYCbCrData 295 #define cmsSigLuvData icSigLuvData 296 #define cmsSigGrayData icSigGrayData 297 #define cmsSigRgbData icSigRgbData 298 #define cmsUInt32Number DWORD 300 #define cmsColorSpaceSignature icColorSpaceSignature 301 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent 308 cmsHPROFILE in_prof, out_prof;
309 cmsHTRANSFORM transform;
310 cmsColorSpaceSignature in_space, out_space;
311 cmsUInt32Number intent, in_type, out_type, nr_samples;
313 int prec, i,
max, max_w, max_h;
314 OPJ_COLOR_SPACE oldspace;
317 cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
319 FILE *icm = fopen(
"debug.icm",
"wb");
320 fwrite( image->icc_profile_buf,1, image->icc_profile_len,icm);
324 if(in_prof == NULL)
return;
326 in_space = cmsGetPCS(in_prof);
327 out_space = cmsGetColorSpace(in_prof);
328 intent = cmsGetHeaderRenderingIntent(in_prof);
331 max_w = (int)image->comps[0].w;
332 max_h = (
int)image->comps[0].h;
333 prec = (int)image->comps[0].prec;
334 oldspace = image->color_space;
336 if(out_space == cmsSigRgbData)
340 in_type = TYPE_RGB_8;
341 out_type = TYPE_RGB_8;
345 in_type = TYPE_RGB_16;
346 out_type = TYPE_RGB_16;
348 out_prof = cmsCreate_sRGBProfile();
349 image->color_space = OPJ_CLRSPC_SRGB;
351 else if(out_space == cmsSigGrayData)
353 in_type = TYPE_GRAY_8;
354 out_type = TYPE_RGB_8;
355 out_prof = cmsCreate_sRGBProfile();
356 image->color_space = OPJ_CLRSPC_SRGB;
358 else if(out_space == cmsSigYCbCrData)
360 in_type = TYPE_YCbCr_16;
361 out_type = TYPE_RGB_16;
362 out_prof = cmsCreate_sRGBProfile();
363 image->color_space = OPJ_CLRSPC_SRGB;
368 fprintf(stderr,
"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown " 369 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
370 __FILE__,__LINE__,out_space,
371 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
372 (out_space>>8) & 0xff, out_space & 0xff);
378 fprintf(stderr,
"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)" 379 "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
380 max_w,max_h, (
void*)in_prof,(
void*)out_prof);
382 fprintf(stderr,
"\trender_intent (%u)\n\t" 383 "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t" 384 " type: in(%u) out:(%u)\n",
387 (in_space>>24) & 0xff,(in_space>>16) & 0xff,
388 (in_space>>8) & 0xff, in_space & 0xff,
391 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
392 (out_space>>8) & 0xff, out_space & 0xff,
401 transform = cmsCreateTransform(in_prof, in_type,
402 out_prof, out_type, intent, 0);
404 #ifdef OPJ_HAVE_LIBLCMS2 406 cmsCloseProfile(in_prof);
407 cmsCloseProfile(out_prof);
410 if(transform == NULL)
413 fprintf(stderr,
"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. " 414 "ICC Profile ignored.\n",__FILE__,__LINE__);
416 image->color_space = oldspace;
417 #ifdef OPJ_HAVE_LIBLCMS1 418 cmsCloseProfile(in_prof);
419 cmsCloseProfile(out_prof);
424 if(image->numcomps > 2)
428 unsigned char *inbuf, *outbuf, *in, *out;
430 nr_samples = (cmsUInt32Number)
max * 3 * (cmsUInt32Number)
sizeof(
unsigned char);
431 in = inbuf = (
unsigned char*)malloc(nr_samples);
432 out = outbuf = (
unsigned char*)malloc(nr_samples);
434 r = image->comps[0].data;
435 g = image->comps[1].data;
436 b = image->comps[2].data;
438 for(i = 0; i <
max; ++i)
440 *in++ = (
unsigned char)*r++;
441 *in++ = (
unsigned char)*g++;
442 *in++ = (
unsigned char)*b++;
445 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)
max);
447 r = image->comps[0].data;
448 g = image->comps[1].data;
449 b = image->comps[2].data;
451 for(i = 0; i <
max; ++i)
457 free(inbuf); free(outbuf);
461 unsigned short *inbuf, *outbuf, *in, *out;
463 nr_samples = (cmsUInt32Number)
max * 3 * (cmsUInt32Number)
sizeof(
unsigned short);
464 in = inbuf = (
unsigned short*)malloc(nr_samples);
465 out = outbuf = (
unsigned short*)malloc(nr_samples);
467 r = image->comps[0].data;
468 g = image->comps[1].data;
469 b = image->comps[2].data;
471 for(i = 0; i <
max; ++i)
473 *in++ = (
unsigned short)*r++;
474 *in++ = (
unsigned short)*g++;
475 *in++ = (
unsigned short)*b++;
478 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)
max);
480 r = image->comps[0].data;
481 g = image->comps[1].data;
482 b = image->comps[2].data;
484 for(i = 0; i <
max; ++i)
490 free(inbuf); free(outbuf);
495 unsigned char *in, *inbuf, *out, *outbuf;
497 nr_samples = (cmsUInt32Number)
max * 3 *
sizeof(
unsigned char);
498 in = inbuf = (
unsigned char*)malloc(nr_samples);
499 out = outbuf = (
unsigned char*)malloc(nr_samples);
501 image->comps = (opj_image_comp_t*)
502 realloc(image->comps, (image->numcomps+2)*
sizeof(opj_image_comp_t));
504 if(image->numcomps == 2)
505 image->comps[3] = image->comps[1];
507 image->comps[1] = image->comps[0];
508 image->comps[2] = image->comps[0];
510 image->comps[1].data = (
int*)calloc((
size_t)
max,
sizeof(int));
511 image->comps[2].data = (
int*)calloc((
size_t)
max,
sizeof(int));
513 image->numcomps += 2;
515 r = image->comps[0].data;
517 for(i = 0; i <
max; ++i)
519 *in++ = (
unsigned char)*r++;
521 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)
max);
523 r = image->comps[0].data;
524 g = image->comps[1].data;
525 b = image->comps[2].data;
527 for(i = 0; i <
max; ++i)
529 *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
531 free(inbuf); free(outbuf);
535 cmsDeleteTransform(transform);
537 #ifdef OPJ_HAVE_LIBLCMS1 538 cmsCloseProfile(in_prof);
539 cmsCloseProfile(out_prof);
void color_sycc_to_rgb(opj_image *img)
void color_apply_icc_profile(opj_image *image)