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)
81 r = y + (int)(1.402 * (float)cr);
89 g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
97 b = y + (int)(1.772 * (float)cb);
100 } else if (b > upb) {
106 static void sycc444_to_rgb(opj_image_t *img)
108 int *d0, *d1, *d2, *r, *g, *b;
109 const int *y, *cb, *cr;
110 size_t maxw, maxh, max, i;
113 upb = (int)img->comps[0].prec;
114 offset = 1 << (upb - 1);
115 upb = (1 << upb) - 1;
117 maxw = (size_t)img->comps[0].w;
118 maxh = (size_t)img->comps[0].h;
121 y = img->comps[0].data;
122 cb = img->comps[1].data;
123 cr = img->comps[2].data;
125 d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
126 d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
127 d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);
129 if (r == NULL || g == NULL || b == NULL) {
133 for (i = 0U; i < max; ++i) {
134 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
142 opj_image_data_free(img->comps[0].data);
143 img->comps[0].data = d0;
144 opj_image_data_free(img->comps[1].data);
145 img->comps[1].data = d1;
146 opj_image_data_free(img->comps[2].data);
147 img->comps[2].data = d2;
148 img->color_space = OPJ_CLRSPC_SRGB;
152 opj_image_data_free(r);
153 opj_image_data_free(g);
154 opj_image_data_free(b);
155 }/* sycc444_to_rgb() */
157 static void sycc422_to_rgb(opj_image_t *img)
159 int *d0, *d1, *d2, *r, *g, *b;
160 const int *y, *cb, *cr;
161 size_t maxw, maxh, max, offx, loopmaxw;
165 upb = (int)img->comps[0].prec;
166 offset = 1 << (upb - 1);
167 upb = (1 << upb) - 1;
169 maxw = (size_t)img->comps[0].w;
170 maxh = (size_t)img->comps[0].h;
173 y = img->comps[0].data;
174 cb = img->comps[1].data;
175 cr = img->comps[2].data;
177 d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
178 d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
179 d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);
181 if (r == NULL || g == NULL || b == NULL) {
185 /* if img->x0 is odd, then first column shall use Cb/Cr = 0 */
187 loopmaxw = maxw - offx;
189 for (i = 0U; i < maxh; ++i) {
193 sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
200 for (j = 0U; j < (loopmaxw & ~(size_t)1U); j += 2U) {
201 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
206 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
215 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
225 opj_image_data_free(img->comps[0].data);
226 img->comps[0].data = d0;
227 opj_image_data_free(img->comps[1].data);
228 img->comps[1].data = d1;
229 opj_image_data_free(img->comps[2].data);
230 img->comps[2].data = d2;
232 img->comps[1].w = img->comps[2].w = img->comps[0].w;
233 img->comps[1].h = img->comps[2].h = img->comps[0].h;
234 img->comps[1].dx = img->comps[2].dx = img->comps[0].dx;
235 img->comps[1].dy = img->comps[2].dy = img->comps[0].dy;
236 img->color_space = OPJ_CLRSPC_SRGB;
240 opj_image_data_free(r);
241 opj_image_data_free(g);
242 opj_image_data_free(b);
243 }/* sycc422_to_rgb() */
245 static void sycc420_to_rgb(opj_image_t *img)
247 int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
248 const int *y, *cb, *cr, *ny;
249 size_t maxw, maxh, max, offx, loopmaxw, offy, loopmaxh;
253 upb = (int)img->comps[0].prec;
254 offset = 1 << (upb - 1);
255 upb = (1 << upb) - 1;
257 maxw = (size_t)img->comps[0].w;
258 maxh = (size_t)img->comps[0].h;
261 y = img->comps[0].data;
262 cb = img->comps[1].data;
263 cr = img->comps[2].data;
265 d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
266 d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
267 d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);
269 if (r == NULL || g == NULL || b == NULL) {
273 /* if img->x0 is odd, then first column shall use Cb/Cr = 0 */
275 loopmaxw = maxw - offx;
276 /* if img->y0 is odd, then first line shall use Cb/Cr = 0 */
278 loopmaxh = maxh - offy;
283 for (j = 0; j < maxw; ++j) {
284 sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
292 for (i = 0U; i < (loopmaxh & ~(size_t)1U); i += 2U) {
301 sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
306 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
313 for (j = 0; j < (loopmaxw & ~(size_t)1U); j += 2U) {
314 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
319 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
325 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
330 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
339 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
345 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
362 sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
369 for (j = 0U; j < (loopmaxw & ~(size_t)1U); j += 2U) {
370 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
377 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
387 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
391 opj_image_data_free(img->comps[0].data);
392 img->comps[0].data = d0;
393 opj_image_data_free(img->comps[1].data);
394 img->comps[1].data = d1;
395 opj_image_data_free(img->comps[2].data);
396 img->comps[2].data = d2;
398 img->comps[1].w = img->comps[2].w = img->comps[0].w;
399 img->comps[1].h = img->comps[2].h = img->comps[0].h;
400 img->comps[1].dx = img->comps[2].dx = img->comps[0].dx;
401 img->comps[1].dy = img->comps[2].dy = img->comps[0].dy;
402 img->color_space = OPJ_CLRSPC_SRGB;
406 opj_image_data_free(r);
407 opj_image_data_free(g);
408 opj_image_data_free(b);
409 }/* sycc420_to_rgb() */
411 void color_sycc_to_rgb(opj_image_t *img)
413 if (img->numcomps < 3) {
414 img->color_space = OPJ_CLRSPC_GRAY;
418 if ((img->comps[0].dx == 1)
419 && (img->comps[1].dx == 2)
420 && (img->comps[2].dx == 2)
421 && (img->comps[0].dy == 1)
422 && (img->comps[1].dy == 2)
423 && (img->comps[2].dy == 2)) { /* horizontal and vertical sub-sample */
425 } else if ((img->comps[0].dx == 1)
426 && (img->comps[1].dx == 2)
427 && (img->comps[2].dx == 2)
428 && (img->comps[0].dy == 1)
429 && (img->comps[1].dy == 1)
430 && (img->comps[2].dy == 1)) { /* horizontal sub-sample only */
432 } else if ((img->comps[0].dx == 1)
433 && (img->comps[1].dx == 1)
434 && (img->comps[2].dx == 1)
435 && (img->comps[0].dy == 1)
436 && (img->comps[1].dy == 1)
437 && (img->comps[2].dy == 1)) { /* no sub-sample */
440 fprintf(stderr, "%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
444 }/* color_sycc_to_rgb() */
446 #if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
448 #ifdef OPJ_HAVE_LIBLCMS1
449 /* Bob Friesenhahn proposed:*/
450 #define cmsSigXYZData icSigXYZData
451 #define cmsSigLabData icSigLabData
452 #define cmsSigCmykData icSigCmykData
453 #define cmsSigYCbCrData icSigYCbCrData
454 #define cmsSigLuvData icSigLuvData
455 #define cmsSigGrayData icSigGrayData
456 #define cmsSigRgbData icSigRgbData
457 #define cmsUInt32Number DWORD
459 #define cmsColorSpaceSignature icColorSpaceSignature
460 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
462 #endif /* OPJ_HAVE_LIBLCMS1 */
464 /*#define DEBUG_PROFILE*/
465 void color_apply_icc_profile(opj_image_t *image)
467 cmsHPROFILE in_prof, out_prof;
468 cmsHTRANSFORM transform;
469 cmsColorSpaceSignature in_space, out_space;
470 cmsUInt32Number intent, in_type, out_type;
472 size_t nr_samples, i, max, max_w, max_h;
474 OPJ_COLOR_SPACE new_space;
476 in_prof = cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
478 FILE *icm = fopen("debug.icm", "wb");
479 fwrite(image->icc_profile_buf, 1, image->icc_profile_len, icm);
483 if (in_prof == NULL) {
487 in_space = cmsGetPCS(in_prof);
488 out_space = cmsGetColorSpace(in_prof);
489 intent = cmsGetHeaderRenderingIntent(in_prof);
492 max_w = image->comps[0].w;
493 max_h = image->comps[0].h;
494 prec = (int)image->comps[0].prec;
496 if (out_space == cmsSigRgbData) { /* enumCS 16 */
497 unsigned int i, nr_comp = image->numcomps;
499 if (nr_comp < 3) { /* GRAY or GRAYA, not RGB or RGBA */
500 cmsCloseProfile(in_prof);
506 for (i = 1; i < nr_comp; ++i) { /* AFL test */
507 if (image->comps[0].dx != image->comps[i].dx) {
511 if (image->comps[0].dy != image->comps[i].dy) {
515 if (image->comps[0].prec != image->comps[i].prec) {
519 if (image->comps[0].sgnd != image->comps[i].sgnd) {
525 cmsCloseProfile(in_prof);
530 in_type = TYPE_RGB_8;
531 out_type = TYPE_RGB_8;
533 in_type = TYPE_RGB_16;
534 out_type = TYPE_RGB_16;
536 out_prof = cmsCreate_sRGBProfile();
537 new_space = OPJ_CLRSPC_SRGB;
538 } else if (out_space == cmsSigGrayData) { /* enumCS 17 */
539 in_type = TYPE_GRAY_8;
540 out_type = TYPE_RGB_8;
541 out_prof = cmsCreate_sRGBProfile();
542 new_space = OPJ_CLRSPC_SRGB;
543 } else if (out_space == cmsSigYCbCrData) { /* enumCS 18 */
544 if (image->numcomps < 3) {
545 cmsCloseProfile(in_prof);
548 in_type = TYPE_YCbCr_16;
549 out_type = TYPE_RGB_16;
550 out_prof = cmsCreate_sRGBProfile();
551 new_space = OPJ_CLRSPC_SRGB;
554 fprintf(stderr, "%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
555 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
556 __FILE__, __LINE__, out_space,
557 (out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
558 (out_space >> 8) & 0xff, out_space & 0xff);
560 cmsCloseProfile(in_prof);
564 if (out_prof == NULL) {
565 cmsCloseProfile(in_prof);
571 "%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
572 "\n\tprofile: in(%p) out(%p)\n", __FILE__, __LINE__, image->numcomps, prec,
573 max_w, max_h, (void*)in_prof, (void*)out_prof);
575 fprintf(stderr, "\trender_intent (%u)\n\t"
576 "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
577 " type: in(%u) out:(%u)\n",
580 (in_space >> 24) & 0xff, (in_space >> 16) & 0xff,
581 (in_space >> 8) & 0xff, in_space & 0xff,
584 (out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
585 (out_space >> 8) & 0xff, out_space & 0xff,
592 #endif /* DEBUG_PROFILE */
594 transform = cmsCreateTransform(in_prof, in_type, out_prof, out_type, intent, 0);
596 #ifdef OPJ_HAVE_LIBLCMS2
597 /* Possible for: LCMS_VERSION >= 2000 :*/
598 cmsCloseProfile(in_prof);
599 cmsCloseProfile(out_prof);
602 if (transform == NULL) {
604 fprintf(stderr, "%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
605 "ICC Profile ignored.\n", __FILE__, __LINE__);
608 #ifdef OPJ_HAVE_LIBLCMS1
609 cmsCloseProfile(in_prof);
610 cmsCloseProfile(out_prof);
615 if (image->numcomps > 2) { /* RGB, RGBA */
616 if ((image->comps[0].w == image->comps[1].w &&
617 image->comps[0].w == image->comps[2].w) &&
618 (image->comps[0].h == image->comps[1].h &&
619 image->comps[0].h == image->comps[2].h)) {
621 unsigned char *inbuf, *outbuf, *in, *out;
624 nr_samples = (size_t)(max * 3U * sizeof(unsigned char));
625 in = inbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
626 out = outbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
628 if (inbuf == NULL || outbuf == NULL) {
632 r = image->comps[0].data;
633 g = image->comps[1].data;
634 b = image->comps[2].data;
636 for (i = 0U; i < max; ++i) {
637 *in++ = (unsigned char) * r++;
638 *in++ = (unsigned char) * g++;
639 *in++ = (unsigned char) * b++;
642 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
644 r = image->comps[0].data;
645 g = image->comps[1].data;
646 b = image->comps[2].data;
648 for (i = 0U; i < max; ++i) {
649 *r++ = (int) * out++;
650 *g++ = (int) * out++;
651 *b++ = (int) * out++;
656 opj_image_data_free(inbuf);
657 opj_image_data_free(outbuf);
658 } else { /* prec > 8 */
659 unsigned short *inbuf, *outbuf, *in, *out;
662 nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
663 in = inbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
664 out = outbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
666 if (inbuf == NULL || outbuf == NULL) {
670 r = image->comps[0].data;
671 g = image->comps[1].data;
672 b = image->comps[2].data;
674 for (i = 0U ; i < max; ++i) {
675 *in++ = (unsigned short) * r++;
676 *in++ = (unsigned short) * g++;
677 *in++ = (unsigned short) * b++;
680 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
682 r = image->comps[0].data;
683 g = image->comps[1].data;
684 b = image->comps[2].data;
686 for (i = 0; i < max; ++i) {
687 *r++ = (int) * out++;
688 *g++ = (int) * out++;
689 *b++ = (int) * out++;
694 opj_image_data_free(inbuf);
695 opj_image_data_free(outbuf);
699 "[ERROR] Image components should have the same width and height\n");
700 cmsDeleteTransform(transform);
703 } else { /* image->numcomps <= 2 : GRAY, GRAYA */
705 unsigned char *in, *inbuf, *out, *outbuf;
706 opj_image_comp_t *new_comps;
709 nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
710 in = inbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
711 out = outbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
712 g = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
713 b = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
715 if (inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) {
719 new_comps = (opj_image_comp_t*)realloc(image->comps,
720 (image->numcomps + 2) * sizeof(opj_image_comp_t));
722 if (new_comps == NULL) {
726 image->comps = new_comps;
728 if (image->numcomps == 2) {
729 image->comps[3] = image->comps[1];
732 image->comps[1] = image->comps[0];
733 image->comps[2] = image->comps[0];
735 image->comps[1].data = g;
736 image->comps[2].data = b;
738 image->numcomps += 2;
740 r = image->comps[0].data;
742 for (i = 0U; i < max; ++i) {
743 *in++ = (unsigned char) * r++;
745 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
747 r = image->comps[0].data;
748 g = image->comps[1].data;
749 b = image->comps[2].data;
751 for (i = 0U; i < max; ++i) {
752 *r++ = (int) * out++;
753 *g++ = (int) * out++;
754 *b++ = (int) * out++;
760 opj_image_data_free(inbuf);
761 opj_image_data_free(outbuf);
762 opj_image_data_free(g);
763 opj_image_data_free(b);
764 } else { /* prec > 8 */
765 unsigned short *in, *inbuf, *out, *outbuf;
766 opj_image_comp_t *new_comps;
769 nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
770 in = inbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
771 out = outbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
772 g = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
773 b = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
775 if (inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) {
779 new_comps = (opj_image_comp_t*)realloc(image->comps,
780 (image->numcomps + 2) * sizeof(opj_image_comp_t));
782 if (new_comps == NULL) {
786 image->comps = new_comps;
788 if (image->numcomps == 2) {
789 image->comps[3] = image->comps[1];
792 image->comps[1] = image->comps[0];
793 image->comps[2] = image->comps[0];
795 image->comps[1].data = g;
796 image->comps[2].data = b;
798 image->numcomps += 2;
800 r = image->comps[0].data;
802 for (i = 0U; i < max; ++i) {
803 *in++ = (unsigned short) * r++;
805 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
807 r = image->comps[0].data;
808 g = image->comps[1].data;
809 b = image->comps[2].data;
811 for (i = 0; i < max; ++i) {
812 *r++ = (int) * out++;
813 *g++ = (int) * out++;
814 *b++ = (int) * out++;
820 opj_image_data_free(inbuf);
821 opj_image_data_free(outbuf);
822 opj_image_data_free(g);
823 opj_image_data_free(b);
825 }/* if(image->numcomps > 2) */
827 cmsDeleteTransform(transform);
829 #ifdef OPJ_HAVE_LIBLCMS1
830 cmsCloseProfile(in_prof);
831 cmsCloseProfile(out_prof);
834 image->color_space = new_space;
836 }/* color_apply_icc_profile() */
838 static int are_comps_same_dimensions(opj_image_t * image)
841 for (i = 1; i < image->numcomps; i++) {
842 if (image->comps[0].dx != image->comps[i].dx ||
843 image->comps[0].dy != image->comps[i].dy) {
850 void color_cielab_to_rgb(opj_image_t *image)
853 int enumcs, numcomps;
854 OPJ_COLOR_SPACE new_space;
856 numcomps = (int)image->numcomps;
859 fprintf(stderr, "%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
860 __FILE__, __LINE__, numcomps);
863 if (!are_comps_same_dimensions(image)) {
865 "%s:%d:\n\tcomponents are not all of the same dimension. Quitting.\n",
870 row = (int*)image->icc_profile_buf;
873 if (enumcs == 14) { /* CIELab */
874 int *L, *a, *b, *red, *green, *blue;
875 int *src0, *src1, *src2, *dst0, *dst1, *dst2;
876 double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
877 double minL, maxL, mina, maxa, minb, maxb;
878 unsigned int default_type;
881 cmsHTRANSFORM transform;
882 cmsUInt16Number RGB[3];
885 in = cmsCreateLab4Profile(NULL);
889 out = cmsCreate_sRGBProfile();
894 transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16,
895 INTENT_PERCEPTUAL, 0);
897 #ifdef OPJ_HAVE_LIBLCMS2
899 cmsCloseProfile(out);
901 if (transform == NULL) {
902 #ifdef OPJ_HAVE_LIBLCMS1
904 cmsCloseProfile(out);
908 new_space = OPJ_CLRSPC_SRGB;
910 prec0 = (double)image->comps[0].prec;
911 prec1 = (double)image->comps[1].prec;
912 prec2 = (double)image->comps[2].prec;
914 default_type = (unsigned int)row[1];
916 if (default_type == 0x44454600) { /* DEF : default */
921 oa = pow(2, prec1 - 1);
922 ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
932 L = src0 = image->comps[0].data;
933 a = src1 = image->comps[1].data;
934 b = src2 = image->comps[2].data;
936 max = image->comps[0].w * image->comps[0].h;
938 red = dst0 = (int*)opj_image_data_alloc(max * sizeof(int));
939 green = dst1 = (int*)opj_image_data_alloc(max * sizeof(int));
940 blue = dst2 = (int*)opj_image_data_alloc(max * sizeof(int));
942 if (red == NULL || green == NULL || blue == NULL) {
946 minL = -(rl * ol) / (pow(2, prec0) - 1);
949 mina = -(ra * oa) / (pow(2, prec1) - 1);
952 minb = -(rb * ob) / (pow(2, prec2) - 1);
955 for (i = 0; i < max; ++i) {
956 Lab.L = minL + (double)(*L) * (maxL - minL) / (pow(2, prec0) - 1);
958 Lab.a = mina + (double)(*a) * (maxa - mina) / (pow(2, prec1) - 1);
960 Lab.b = minb + (double)(*b) * (maxb - minb) / (pow(2, prec2) - 1);
963 cmsDoTransform(transform, &Lab, RGB, 1);
969 cmsDeleteTransform(transform);
970 #ifdef OPJ_HAVE_LIBLCMS1
972 cmsCloseProfile(out);
974 opj_image_data_free(src0);
975 image->comps[0].data = dst0;
976 opj_image_data_free(src1);
977 image->comps[1].data = dst1;
978 opj_image_data_free(src2);
979 image->comps[2].data = dst2;
981 image->color_space = new_space;
982 image->comps[0].prec = 16;
983 image->comps[1].prec = 16;
984 image->comps[2].prec = 16;
989 cmsDeleteTransform(transform);
990 #ifdef OPJ_HAVE_LIBLCMS1
992 cmsCloseProfile(out);
995 opj_image_data_free(red);
998 opj_image_data_free(green);
1001 opj_image_data_free(blue);
1006 fprintf(stderr, "%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,
1008 }/* color_cielab_to_rgb() */
1010 #endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */
1012 void color_cmyk_to_rgb(opj_image_t *image)
1015 float sC, sM, sY, sK;
1016 unsigned int w, h, max, i;
1018 w = image->comps[0].w;
1019 h = image->comps[0].h;
1022 (image->numcomps < 4)
1023 || (image->comps[0].dx != image->comps[1].dx) ||
1024 (image->comps[0].dx != image->comps[2].dx) ||
1025 (image->comps[0].dx != image->comps[3].dx)
1026 || (image->comps[0].dy != image->comps[1].dy) ||
1027 (image->comps[0].dy != image->comps[2].dy) ||
1028 (image->comps[0].dy != image->comps[3].dy)
1030 fprintf(stderr, "%s:%d:color_cmyk_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
1037 sC = 1.0F / (float)((1 << image->comps[0].prec) - 1);
1038 sM = 1.0F / (float)((1 << image->comps[1].prec) - 1);
1039 sY = 1.0F / (float)((1 << image->comps[2].prec) - 1);
1040 sK = 1.0F / (float)((1 << image->comps[3].prec) - 1);
1042 for (i = 0; i < max; ++i) {
1043 /* CMYK values from 0 to 1 */
1044 C = (float)(image->comps[0].data[i]) * sC;
1045 M = (float)(image->comps[1].data[i]) * sM;
1046 Y = (float)(image->comps[2].data[i]) * sY;
1047 K = (float)(image->comps[3].data[i]) * sK;
1049 /* Invert all CMYK values */
1055 /* CMYK -> RGB : RGB results from 0 to 255 */
1056 image->comps[0].data[i] = (int)(255.0F * C * K); /* R */
1057 image->comps[1].data[i] = (int)(255.0F * M * K); /* G */
1058 image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */
1061 opj_image_data_free(image->comps[3].data);
1062 image->comps[3].data = NULL;
1063 image->comps[0].prec = 8;
1064 image->comps[1].prec = 8;
1065 image->comps[2].prec = 8;
1066 image->numcomps -= 1;
1067 image->color_space = OPJ_CLRSPC_SRGB;
1069 for (i = 3; i < image->numcomps; ++i) {
1070 memcpy(&(image->comps[i]), &(image->comps[i + 1]), sizeof(image->comps[i]));
1073 }/* color_cmyk_to_rgb() */
1076 * This code has been adopted from sjpx_openjpeg.c of ghostscript
1078 void color_esycc_to_rgb(opj_image_t *image)
1080 int y, cb, cr, sign1, sign2, val;
1081 unsigned int w, h, max, i;
1082 int flip_value = (1 << (image->comps[0].prec - 1));
1083 int max_value = (1 << image->comps[0].prec) - 1;
1086 (image->numcomps < 3)
1087 || (image->comps[0].dx != image->comps[1].dx) ||
1088 (image->comps[0].dx != image->comps[2].dx)
1089 || (image->comps[0].dy != image->comps[1].dy) ||
1090 (image->comps[0].dy != image->comps[2].dy)
1092 fprintf(stderr, "%s:%d:color_esycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
1097 w = image->comps[0].w;
1098 h = image->comps[0].h;
1100 sign1 = (int)image->comps[1].sgnd;
1101 sign2 = (int)image->comps[2].sgnd;
1105 for (i = 0; i < max; ++i) {
1107 y = image->comps[0].data[i];
1108 cb = image->comps[1].data[i];
1109 cr = image->comps[2].data[i];
1119 ((float)y - (float)0.0000368 * (float)cb
1120 + (float)1.40199 * (float)cr + (float)0.5);
1122 if (val > max_value) {
1124 } else if (val < 0) {
1127 image->comps[0].data[i] = val;
1130 ((float)1.0003 * (float)y - (float)0.344125 * (float)cb
1131 - (float)0.7141128 * (float)cr + (float)0.5);
1133 if (val > max_value) {
1135 } else if (val < 0) {
1138 image->comps[1].data[i] = val;
1141 ((float)0.999823 * (float)y + (float)1.77204 * (float)cb
1142 - (float)0.000008 * (float)cr + (float)0.5);
1144 if (val > max_value) {
1146 } else if (val < 0) {
1149 image->comps[2].data[i] = val;
1151 image->color_space = OPJ_CLRSPC_SRGB;
1153 }/* color_esycc_to_rgb() */