2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
44 #include "opj_apps_config.h"
48 #ifdef OPJ_HAVE_LIBLCMS2
51 #ifdef OPJ_HAVE_LIBLCMS1
56 #define OPJ_CLRSPC_GRAY CLRSPC_GRAY
57 #define OPJ_CLRSPC_SRGB CLRSPC_SRGB
60 /*--------------------------------------------------------
61 Matrix for sYCC, Amendment 1 to IEC 61966-2-1
63 Y : 0.299 0.587 0.114 :R
64 Cb: -0.1687 -0.3312 0.5 :G
65 Cr: 0.5 -0.4187 -0.0812 :B
69 R: 1 -3.68213e-05 1.40199 :Y
70 G: 1.00003 -0.344125 -0.714128 :Cb - 2^(prec - 1)
71 B: 0.999823 1.77204 -8.04142e-06 :Cr - 2^(prec - 1)
73 -----------------------------------------------------------*/
74 static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
75 int *out_r, int *out_g, int *out_b)
79 cb -= offset; cr -= offset;
80 r = y + (int)(1.402 * (float)cr);
81 if(r < 0) r = 0; else if(r > upb) r = upb; *out_r = r;
83 g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
84 if(g < 0) g = 0; else if(g > upb) g = upb; *out_g = g;
86 b = y + (int)(1.772 * (float)cb);
87 if(b < 0) b = 0; else if(b > upb) b = upb; *out_b = b;
90 static void sycc444_to_rgb(opj_image_t *img)
92 int *d0, *d1, *d2, *r, *g, *b;
93 const int *y, *cb, *cr;
94 unsigned int maxw, maxh, max, i;
97 upb = (int)img->comps[0].prec;
98 offset = 1<<(upb - 1); upb = (1<<upb)-1;
100 maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
103 y = img->comps[0].data;
104 cb = img->comps[1].data;
105 cr = img->comps[2].data;
107 d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
108 d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
109 d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
111 if(r == NULL || g == NULL || b == NULL) goto fails;
113 for(i = 0U; i < max; ++i)
115 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
116 ++y; ++cb; ++cr; ++r; ++g; ++b;
118 free(img->comps[0].data); img->comps[0].data = d0;
119 free(img->comps[1].data); img->comps[1].data = d1;
120 free(img->comps[2].data); img->comps[2].data = d2;
128 }/* sycc444_to_rgb() */
130 static void sycc422_to_rgb(opj_image_t *img)
132 int *d0, *d1, *d2, *r, *g, *b;
133 const int *y, *cb, *cr;
134 unsigned int maxw, maxh, max;
138 upb = (int)img->comps[0].prec;
139 offset = 1<<(upb - 1); upb = (1<<upb)-1;
141 maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
144 y = img->comps[0].data;
145 cb = img->comps[1].data;
146 cr = img->comps[2].data;
148 d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
149 d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
150 d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
152 if(r == NULL || g == NULL || b == NULL) goto fails;
154 for(i=0U; i < maxh; ++i)
156 for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
158 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
160 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
161 ++y; ++r; ++g; ++b; ++cb; ++cr;
164 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
165 ++y; ++r; ++g; ++b; ++cb; ++cr;
168 free(img->comps[0].data); img->comps[0].data = d0;
169 free(img->comps[1].data); img->comps[1].data = d1;
170 free(img->comps[2].data); img->comps[2].data = d2;
172 #if defined(USE_JPWL) || defined(USE_MJ2)
173 img->comps[1].w = maxw; img->comps[1].h = maxh;
174 img->comps[2].w = maxw; img->comps[2].h = maxh;
176 img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
177 img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
179 img->comps[1].dx = img->comps[0].dx;
180 img->comps[2].dx = img->comps[0].dx;
181 img->comps[1].dy = img->comps[0].dy;
182 img->comps[2].dy = img->comps[0].dy;
190 }/* sycc422_to_rgb() */
192 static void sycc420_to_rgb(opj_image_t *img)
194 int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
195 const int *y, *cb, *cr, *ny;
196 unsigned int maxw, maxh, max;
200 upb = (int)img->comps[0].prec;
201 offset = 1<<(upb - 1); upb = (1<<upb)-1;
203 maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
206 y = img->comps[0].data;
207 cb = img->comps[1].data;
208 cr = img->comps[2].data;
210 d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
211 d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
212 d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
214 if(r == NULL || g == NULL || b == NULL) goto fails;
216 for(i=0U; i < (maxh & ~(unsigned int)1U); i += 2U)
219 nr = r + maxw; ng = g + maxw; nb = b + maxw;
221 for(j=0; j < (maxw & ~(unsigned int)1U); j += 2U)
223 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
225 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
228 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
229 ++ny; ++nr; ++ng; ++nb;
230 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
231 ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
235 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
238 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
239 ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
241 y += maxw; r += maxw; g += maxw; b += maxw;
245 for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
247 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
251 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
253 ++y; ++r; ++g; ++b; ++cb; ++cr;
257 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
261 free(img->comps[0].data); img->comps[0].data = d0;
262 free(img->comps[1].data); img->comps[1].data = d1;
263 free(img->comps[2].data); img->comps[2].data = d2;
265 #if defined(USE_JPWL) || defined(USE_MJ2)
266 img->comps[1].w = maxw; img->comps[1].h = maxh;
267 img->comps[2].w = maxw; img->comps[2].h = maxh;
269 img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
270 img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
272 img->comps[1].dx = img->comps[0].dx;
273 img->comps[2].dx = img->comps[0].dx;
274 img->comps[1].dy = img->comps[0].dy;
275 img->comps[2].dy = img->comps[0].dy;
283 }/* sycc420_to_rgb() */
285 void color_sycc_to_rgb(opj_image_t *img)
287 if(img->numcomps < 3)
289 img->color_space = OPJ_CLRSPC_GRAY;
293 if((img->comps[0].dx == 1)
294 && (img->comps[1].dx == 2)
295 && (img->comps[2].dx == 2)
296 && (img->comps[0].dy == 1)
297 && (img->comps[1].dy == 2)
298 && (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */
303 if((img->comps[0].dx == 1)
304 && (img->comps[1].dx == 2)
305 && (img->comps[2].dx == 2)
306 && (img->comps[0].dy == 1)
307 && (img->comps[1].dy == 1)
308 && (img->comps[2].dy == 1))/* horizontal sub-sample only */
313 if((img->comps[0].dx == 1)
314 && (img->comps[1].dx == 1)
315 && (img->comps[2].dx == 1)
316 && (img->comps[0].dy == 1)
317 && (img->comps[1].dy == 1)
318 && (img->comps[2].dy == 1))/* no sub-sample */
324 fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,__LINE__);
327 img->color_space = OPJ_CLRSPC_SRGB;
329 }/* color_sycc_to_rgb() */
331 #if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
333 #ifdef OPJ_HAVE_LIBLCMS1
334 /* Bob Friesenhahn proposed:*/
335 #define cmsSigXYZData icSigXYZData
336 #define cmsSigLabData icSigLabData
337 #define cmsSigCmykData icSigCmykData
338 #define cmsSigYCbCrData icSigYCbCrData
339 #define cmsSigLuvData icSigLuvData
340 #define cmsSigGrayData icSigGrayData
341 #define cmsSigRgbData icSigRgbData
342 #define cmsUInt32Number DWORD
344 #define cmsColorSpaceSignature icColorSpaceSignature
345 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
347 #endif /* OPJ_HAVE_LIBLCMS1 */
349 /*#define DEBUG_PROFILE*/
350 void color_apply_icc_profile(opj_image_t *image)
352 cmsHPROFILE in_prof, out_prof;
353 cmsHTRANSFORM transform;
354 cmsColorSpaceSignature in_space, out_space;
355 cmsUInt32Number intent, in_type, out_type;
358 int prec, i, max, max_w, max_h, ok = 0;
359 OPJ_COLOR_SPACE new_space;
362 cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
364 FILE *icm = fopen("debug.icm","wb");
365 fwrite( image->icc_profile_buf,1, image->icc_profile_len,icm);
369 if(in_prof == NULL) return;
371 in_space = cmsGetPCS(in_prof);
372 out_space = cmsGetColorSpace(in_prof);
373 intent = cmsGetHeaderRenderingIntent(in_prof);
376 max_w = (int)image->comps[0].w;
377 max_h = (int)image->comps[0].h;
378 prec = (int)image->comps[0].prec;
380 if(out_space == cmsSigRgbData) /* enumCS 16 */
384 in_type = TYPE_RGB_8;
385 out_type = TYPE_RGB_8;
389 in_type = TYPE_RGB_16;
390 out_type = TYPE_RGB_16;
392 out_prof = cmsCreate_sRGBProfile();
393 new_space = OPJ_CLRSPC_SRGB;
396 if(out_space == cmsSigGrayData) /* enumCS 17 */
398 in_type = TYPE_GRAY_8;
399 out_type = TYPE_RGB_8;
400 out_prof = cmsCreate_sRGBProfile();
401 new_space = OPJ_CLRSPC_SRGB;
404 if(out_space == cmsSigYCbCrData) /* enumCS 18 */
406 in_type = TYPE_YCbCr_16;
407 out_type = TYPE_RGB_16;
408 out_prof = cmsCreate_sRGBProfile();
409 new_space = OPJ_CLRSPC_SRGB;
414 fprintf(stderr,"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
415 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
416 __FILE__,__LINE__,out_space,
417 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
418 (out_space>>8) & 0xff, out_space & 0xff);
420 cmsCloseProfile(in_prof);
426 cmsCloseProfile(in_prof);
432 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
433 "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
434 max_w,max_h, (void*)in_prof,(void*)out_prof);
436 fprintf(stderr,"\trender_intent (%u)\n\t"
437 "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
438 " type: in(%u) out:(%u)\n",
441 (in_space>>24) & 0xff,(in_space>>16) & 0xff,
442 (in_space>>8) & 0xff, in_space & 0xff,
445 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
446 (out_space>>8) & 0xff, out_space & 0xff,
453 #endif /* DEBUG_PROFILE */
455 transform = cmsCreateTransform(in_prof, in_type,
456 out_prof, out_type, intent, 0);
458 #ifdef OPJ_HAVE_LIBLCMS2
459 /* Possible for: LCMS_VERSION >= 2000 :*/
460 cmsCloseProfile(in_prof);
461 cmsCloseProfile(out_prof);
464 if(transform == NULL)
467 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
468 "ICC Profile ignored.\n",__FILE__,__LINE__);
471 #ifdef OPJ_HAVE_LIBLCMS1
472 cmsCloseProfile(in_prof);
473 cmsCloseProfile(out_prof);
478 if(image->numcomps > 2)/* RGB, RGBA */
482 unsigned char *inbuf, *outbuf, *in, *out;
485 nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
486 in = inbuf = (unsigned char*)malloc(nr_samples);
487 out = outbuf = (unsigned char*)malloc(nr_samples);
489 if(inbuf == NULL || outbuf == NULL) goto fails0;
491 r = image->comps[0].data;
492 g = image->comps[1].data;
493 b = image->comps[2].data;
495 for(i = 0; i < max; ++i)
497 *in++ = (unsigned char)*r++;
498 *in++ = (unsigned char)*g++;
499 *in++ = (unsigned char)*b++;
502 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
504 r = image->comps[0].data;
505 g = image->comps[1].data;
506 b = image->comps[2].data;
508 for(i = 0; i < max; ++i)
517 if(inbuf) free(inbuf);
518 if(outbuf) free(outbuf);
522 unsigned short *inbuf, *outbuf, *in, *out;
525 nr_samples = (size_t)(max * 3 * sizeof(unsigned short));
526 in = inbuf = (unsigned short*)malloc(nr_samples);
527 out = outbuf = (unsigned short*)malloc(nr_samples);
529 if(inbuf == NULL || outbuf == NULL) goto fails1;
531 r = image->comps[0].data;
532 g = image->comps[1].data;
533 b = image->comps[2].data;
535 for(i = 0; i < max; ++i)
537 *in++ = (unsigned short)*r++;
538 *in++ = (unsigned short)*g++;
539 *in++ = (unsigned short)*b++;
542 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
544 r = image->comps[0].data;
545 g = image->comps[1].data;
546 b = image->comps[2].data;
548 for(i = 0; i < max; ++i)
557 if(inbuf) free(inbuf);
558 if(outbuf) free(outbuf);
561 else /* image->numcomps <= 2 : GRAY, GRAYA */
565 unsigned char *in, *inbuf, *out, *outbuf;
566 opj_image_comp_t *new_comps;
569 nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
570 in = inbuf = (unsigned char*)malloc(nr_samples);
571 out = outbuf = (unsigned char*)malloc(nr_samples);
572 g = (int*)calloc((size_t)max, sizeof(int));
573 b = (int*)calloc((size_t)max, sizeof(int));
575 if(inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) goto fails2;
577 new_comps = (opj_image_comp_t*)
578 realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
580 if(new_comps == NULL) goto fails2;
582 image->comps = new_comps;
584 if(image->numcomps == 2)
585 image->comps[3] = image->comps[1];
587 image->comps[1] = image->comps[0];
588 image->comps[2] = image->comps[0];
590 image->comps[1].data = g;
591 image->comps[2].data = b;
593 image->numcomps += 2;
595 r = image->comps[0].data;
597 for(i = 0; i < max; ++i)
599 *in++ = (unsigned char)*r++;
601 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
603 r = image->comps[0].data;
604 g = image->comps[1].data;
605 b = image->comps[2].data;
607 for(i = 0; i < max; ++i)
609 *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
615 if(inbuf) free(inbuf);
616 if(outbuf) free(outbuf);
622 unsigned short *in, *inbuf, *out, *outbuf;
623 opj_image_comp_t *new_comps;
626 nr_samples = (size_t)(max * 3 * sizeof(unsigned short));
627 in = inbuf = (unsigned short*)malloc(nr_samples);
628 out = outbuf = (unsigned short*)malloc(nr_samples);
629 g = (int*)calloc((size_t)max, sizeof(int));
630 b = (int*)calloc((size_t)max, sizeof(int));
632 if(inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) goto fails3;
634 new_comps = (opj_image_comp_t*)
635 realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
637 if(new_comps == NULL) goto fails3;
639 image->comps = new_comps;
641 if(image->numcomps == 2)
642 image->comps[3] = image->comps[1];
644 image->comps[1] = image->comps[0];
645 image->comps[2] = image->comps[0];
647 image->comps[1].data = g;
648 image->comps[2].data = b;
650 image->numcomps += 2;
652 r = image->comps[0].data;
654 for(i = 0; i < max; ++i)
656 *in++ = (unsigned short)*r++;
658 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
660 r = image->comps[0].data;
661 g = image->comps[1].data;
662 b = image->comps[2].data;
664 for(i = 0; i < max; ++i)
666 *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
672 if(inbuf) free(inbuf);
673 if(outbuf) free(outbuf);
677 }/* if(image->numcomps > 2) */
679 cmsDeleteTransform(transform);
681 #ifdef OPJ_HAVE_LIBLCMS1
682 cmsCloseProfile(in_prof);
683 cmsCloseProfile(out_prof);
687 image->color_space = new_space;
689 }/* color_apply_icc_profile() */
691 void color_cielab_to_rgb(opj_image_t *image)
694 int enumcs, numcomps;
695 OPJ_COLOR_SPACE new_space;
697 numcomps = (int)image->numcomps;
701 fprintf(stderr,"%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
702 __FILE__,__LINE__,numcomps);
706 row = (int*)image->icc_profile_buf;
709 if(enumcs == 14) /* CIELab */
711 int *L, *a, *b, *red, *green, *blue;
712 int *src0, *src1, *src2, *dst0, *dst1, *dst2;
713 double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
714 double minL, maxL, mina, maxa, minb, maxb;
715 unsigned int default_type;
718 cmsHTRANSFORM transform;
719 cmsUInt16Number RGB[3];
722 in = cmsCreateLab4Profile(NULL);
726 out = cmsCreate_sRGBProfile();
731 transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16, INTENT_PERCEPTUAL, 0);
733 #ifdef OPJ_HAVE_LIBLCMS2
735 cmsCloseProfile(out);
737 if(transform == NULL)
739 #ifdef OPJ_HAVE_LIBLCMS1
741 cmsCloseProfile(out);
745 new_space = OPJ_CLRSPC_SRGB;
747 prec0 = (double)image->comps[0].prec;
748 prec1 = (double)image->comps[1].prec;
749 prec2 = (double)image->comps[2].prec;
751 default_type = (unsigned int)row[1];
753 if(default_type == 0x44454600)/* DEF : default */
755 rl = 100; ra = 170; rb = 200;
757 oa = pow(2, prec1 - 1);
758 ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
762 rl = row[2]; ra = row[4]; rb = row[6];
763 ol = row[3]; oa = row[5]; ob = row[7];
766 L = src0 = image->comps[0].data;
767 a = src1 = image->comps[1].data;
768 b = src2 = image->comps[2].data;
770 max = image->comps[0].w * image->comps[0].h;
772 red = dst0 = (int*)malloc(max * sizeof(int));
773 green = dst1 = (int*)malloc(max * sizeof(int));
774 blue = dst2 = (int*)malloc(max * sizeof(int));
776 if(red == NULL || green == NULL || blue == NULL) goto fails;
778 minL = -(rl * ol)/(pow(2, prec0)-1);
781 mina = -(ra * oa)/(pow(2, prec1)-1);
784 minb = -(rb * ob)/(pow(2, prec2)-1);
787 for(i = 0; i < max; ++i)
789 Lab.L = minL + (double)(*L) * (maxL - minL)/(pow(2, prec0)-1); ++L;
790 Lab.a = mina + (double)(*a) * (maxa - mina)/(pow(2, prec1)-1); ++a;
791 Lab.b = minb + (double)(*b) * (maxb - minb)/(pow(2, prec2)-1); ++b;
793 cmsDoTransform(transform, &Lab, RGB, 1);
799 cmsDeleteTransform(transform);
800 #ifdef OPJ_HAVE_LIBLCMS1
802 cmsCloseProfile(out);
804 free(src0); image->comps[0].data = dst0;
805 free(src1); image->comps[1].data = dst1;
806 free(src2); image->comps[2].data = dst2;
808 image->color_space = new_space;
809 image->comps[0].prec = 16;
810 image->comps[1].prec = 16;
811 image->comps[2].prec = 16;
816 cmsDeleteTransform(transform);
817 #ifdef OPJ_HAVE_LIBLCMS1
819 cmsCloseProfile(out);
822 if(green) free(green);
827 fprintf(stderr,"%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,__LINE__, enumcs);
828 }/* color_cielab_to_rgb() */
830 #endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */
832 void color_cmyk_to_rgb(opj_image_t *image)
835 float sC, sM, sY, sK;
836 unsigned int w, h, max, i;
838 w = image->comps[0].w;
839 h = image->comps[0].h;
841 if(image->numcomps < 4) return;
845 sC = 1.0F / (float)((1 << image->comps[0].prec) - 1);
846 sM = 1.0F / (float)((1 << image->comps[1].prec) - 1);
847 sY = 1.0F / (float)((1 << image->comps[2].prec) - 1);
848 sK = 1.0F / (float)((1 << image->comps[3].prec) - 1);
850 for(i = 0; i < max; ++i)
852 /* CMYK values from 0 to 1 */
853 C = (float)(image->comps[0].data[i]) * sC;
854 M = (float)(image->comps[1].data[i]) * sM;
855 Y = (float)(image->comps[2].data[i]) * sY;
856 K = (float)(image->comps[3].data[i]) * sK;
858 /* Invert all CMYK values */
864 /* CMYK -> RGB : RGB results from 0 to 255 */
865 image->comps[0].data[i] = (int)(255.0F * C * K); /* R */
866 image->comps[1].data[i] = (int)(255.0F * M * K); /* G */
867 image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */
870 free(image->comps[3].data); image->comps[3].data = NULL;
871 image->comps[0].prec = 8;
872 image->comps[1].prec = 8;
873 image->comps[2].prec = 8;
874 image->numcomps -= 1;
875 image->color_space = OPJ_CLRSPC_SRGB;
877 for (i = 3; i < image->numcomps; ++i) {
878 memcpy(&(image->comps[i]), &(image->comps[i+1]), sizeof(image->comps[i]));
881 }/* color_cmyk_to_rgb() */
884 * This code has been adopted from sjpx_openjpeg.c of ghostscript
886 void color_esycc_to_rgb(opj_image_t *image)
888 int y, cb, cr, sign1, sign2, val;
889 unsigned int w, h, max, i;
890 int flip_value = (1 << (image->comps[0].prec-1));
891 int max_value = (1 << image->comps[0].prec) - 1;
893 if(image->numcomps < 3) return;
895 w = image->comps[0].w;
896 h = image->comps[0].h;
898 sign1 = (int)image->comps[1].sgnd;
899 sign2 = (int)image->comps[2].sgnd;
903 for(i = 0; i < max; ++i)
906 y = image->comps[0].data[i]; cb = image->comps[1].data[i]; cr = image->comps[2].data[i];
908 if( !sign1) cb -= flip_value;
909 if( !sign2) cr -= flip_value;
912 ((float)y - (float)0.0000368 * (float)cb
913 + (float)1.40199 * (float)cr + (float)0.5);
915 if(val > max_value) val = max_value; else if(val < 0) val = 0;
916 image->comps[0].data[i] = val;
919 ((float)1.0003 * (float)y - (float)0.344125 * (float)cb
920 - (float)0.7141128 * (float)cr + (float)0.5);
922 if(val > max_value) val = max_value; else if(val < 0) val = 0;
923 image->comps[1].data[i] = val;
926 ((float)0.999823 * (float)y + (float)1.77204 * (float)cb
927 - (float)0.000008 *(float)cr + (float)0.5);
929 if(val > max_value) val = max_value; else if(val < 0) val = 0;
930 image->comps[2].data[i] = val;
932 image->color_space = OPJ_CLRSPC_SRGB;
934 }/* color_esycc_to_rgb() */