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*)malloc(sizeof(int) * max);
126 d1 = g = (int*)malloc(sizeof(int) * max);
127 d2 = b = (int*)malloc(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 free(img->comps[0].data);
143 img->comps[0].data = d0;
144 free(img->comps[1].data);
145 img->comps[1].data = d1;
146 free(img->comps[2].data);
147 img->comps[2].data = d2;
148 img->color_space = OPJ_CLRSPC_SRGB;
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*)malloc(sizeof(int) * max);
178 d1 = g = (int*)malloc(sizeof(int) * max);
179 d2 = b = (int*)malloc(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 free(img->comps[0].data);
226 img->comps[0].data = d0;
227 free(img->comps[1].data);
228 img->comps[1].data = d1;
229 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;
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*)malloc(sizeof(int) * max);
266 d1 = g = (int*)malloc(sizeof(int) * max);
267 d2 = b = (int*)malloc(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);
361 for (j = 0U; j < (maxw & ~(size_t)1U); j += 2U) {
362 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
369 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
379 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
383 free(img->comps[0].data);
384 img->comps[0].data = d0;
385 free(img->comps[1].data);
386 img->comps[1].data = d1;
387 free(img->comps[2].data);
388 img->comps[2].data = d2;
390 img->comps[1].w = img->comps[2].w = img->comps[0].w;
391 img->comps[1].h = img->comps[2].h = img->comps[0].h;
392 img->comps[1].dx = img->comps[2].dx = img->comps[0].dx;
393 img->comps[1].dy = img->comps[2].dy = img->comps[0].dy;
394 img->color_space = OPJ_CLRSPC_SRGB;
401 }/* sycc420_to_rgb() */
403 void color_sycc_to_rgb(opj_image_t *img)
405 if (img->numcomps < 3) {
406 img->color_space = OPJ_CLRSPC_GRAY;
410 if ((img->comps[0].dx == 1)
411 && (img->comps[1].dx == 2)
412 && (img->comps[2].dx == 2)
413 && (img->comps[0].dy == 1)
414 && (img->comps[1].dy == 2)
415 && (img->comps[2].dy == 2)) { /* horizontal and vertical sub-sample */
417 } else if ((img->comps[0].dx == 1)
418 && (img->comps[1].dx == 2)
419 && (img->comps[2].dx == 2)
420 && (img->comps[0].dy == 1)
421 && (img->comps[1].dy == 1)
422 && (img->comps[2].dy == 1)) { /* horizontal sub-sample only */
424 } else if ((img->comps[0].dx == 1)
425 && (img->comps[1].dx == 1)
426 && (img->comps[2].dx == 1)
427 && (img->comps[0].dy == 1)
428 && (img->comps[1].dy == 1)
429 && (img->comps[2].dy == 1)) { /* no sub-sample */
432 fprintf(stderr, "%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
436 }/* color_sycc_to_rgb() */
438 #if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
440 #ifdef OPJ_HAVE_LIBLCMS1
441 /* Bob Friesenhahn proposed:*/
442 #define cmsSigXYZData icSigXYZData
443 #define cmsSigLabData icSigLabData
444 #define cmsSigCmykData icSigCmykData
445 #define cmsSigYCbCrData icSigYCbCrData
446 #define cmsSigLuvData icSigLuvData
447 #define cmsSigGrayData icSigGrayData
448 #define cmsSigRgbData icSigRgbData
449 #define cmsUInt32Number DWORD
451 #define cmsColorSpaceSignature icColorSpaceSignature
452 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
454 #endif /* OPJ_HAVE_LIBLCMS1 */
456 /*#define DEBUG_PROFILE*/
457 void color_apply_icc_profile(opj_image_t *image)
459 cmsHPROFILE in_prof, out_prof;
460 cmsHTRANSFORM transform;
461 cmsColorSpaceSignature in_space, out_space;
462 cmsUInt32Number intent, in_type, out_type;
464 size_t nr_samples, i, max, max_w, max_h;
466 OPJ_COLOR_SPACE new_space;
468 in_prof = cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
470 FILE *icm = fopen("debug.icm", "wb");
471 fwrite(image->icc_profile_buf, 1, image->icc_profile_len, icm);
475 if (in_prof == NULL) {
479 in_space = cmsGetPCS(in_prof);
480 out_space = cmsGetColorSpace(in_prof);
481 intent = cmsGetHeaderRenderingIntent(in_prof);
484 max_w = image->comps[0].w;
485 max_h = image->comps[0].h;
486 prec = (int)image->comps[0].prec;
488 if (out_space == cmsSigRgbData) { /* enumCS 16 */
490 in_type = TYPE_RGB_8;
491 out_type = TYPE_RGB_8;
493 in_type = TYPE_RGB_16;
494 out_type = TYPE_RGB_16;
496 out_prof = cmsCreate_sRGBProfile();
497 new_space = OPJ_CLRSPC_SRGB;
498 } else if (out_space == cmsSigGrayData) { /* enumCS 17 */
499 in_type = TYPE_GRAY_8;
500 out_type = TYPE_RGB_8;
501 out_prof = cmsCreate_sRGBProfile();
502 new_space = OPJ_CLRSPC_SRGB;
503 } else if (out_space == cmsSigYCbCrData) { /* enumCS 18 */
504 in_type = TYPE_YCbCr_16;
505 out_type = TYPE_RGB_16;
506 out_prof = cmsCreate_sRGBProfile();
507 new_space = OPJ_CLRSPC_SRGB;
510 fprintf(stderr, "%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
511 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
512 __FILE__, __LINE__, out_space,
513 (out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
514 (out_space >> 8) & 0xff, out_space & 0xff);
516 cmsCloseProfile(in_prof);
520 if (out_prof == NULL) {
521 cmsCloseProfile(in_prof);
527 "%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
528 "\n\tprofile: in(%p) out(%p)\n", __FILE__, __LINE__, image->numcomps, prec,
529 max_w, max_h, (void*)in_prof, (void*)out_prof);
531 fprintf(stderr, "\trender_intent (%u)\n\t"
532 "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
533 " type: in(%u) out:(%u)\n",
536 (in_space >> 24) & 0xff, (in_space >> 16) & 0xff,
537 (in_space >> 8) & 0xff, in_space & 0xff,
540 (out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
541 (out_space >> 8) & 0xff, out_space & 0xff,
548 #endif /* DEBUG_PROFILE */
550 transform = cmsCreateTransform(in_prof, in_type, out_prof, out_type, intent, 0);
552 #ifdef OPJ_HAVE_LIBLCMS2
553 /* Possible for: LCMS_VERSION >= 2000 :*/
554 cmsCloseProfile(in_prof);
555 cmsCloseProfile(out_prof);
558 if (transform == NULL) {
560 fprintf(stderr, "%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
561 "ICC Profile ignored.\n", __FILE__, __LINE__);
564 #ifdef OPJ_HAVE_LIBLCMS1
565 cmsCloseProfile(in_prof);
566 cmsCloseProfile(out_prof);
571 if (image->numcomps > 2) { /* RGB, RGBA */
573 unsigned char *inbuf, *outbuf, *in, *out;
576 nr_samples = (size_t)(max * 3U * sizeof(unsigned char));
577 in = inbuf = (unsigned char*)malloc(nr_samples);
578 out = outbuf = (unsigned char*)malloc(nr_samples);
580 if (inbuf == NULL || outbuf == NULL) {
584 r = image->comps[0].data;
585 g = image->comps[1].data;
586 b = image->comps[2].data;
588 for (i = 0U; i < max; ++i) {
589 *in++ = (unsigned char) * r++;
590 *in++ = (unsigned char) * g++;
591 *in++ = (unsigned char) * b++;
594 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
596 r = image->comps[0].data;
597 g = image->comps[1].data;
598 b = image->comps[2].data;
600 for (i = 0U; i < max; ++i) {
601 *r++ = (int) * out++;
602 *g++ = (int) * out++;
603 *b++ = (int) * out++;
610 } else { /* prec > 8 */
611 unsigned short *inbuf, *outbuf, *in, *out;
614 nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
615 in = inbuf = (unsigned short*)malloc(nr_samples);
616 out = outbuf = (unsigned short*)malloc(nr_samples);
618 if (inbuf == NULL || outbuf == NULL) {
622 r = image->comps[0].data;
623 g = image->comps[1].data;
624 b = image->comps[2].data;
626 for (i = 0U ; i < max; ++i) {
627 *in++ = (unsigned short) * r++;
628 *in++ = (unsigned short) * g++;
629 *in++ = (unsigned short) * b++;
632 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
634 r = image->comps[0].data;
635 g = image->comps[1].data;
636 b = image->comps[2].data;
638 for (i = 0; i < max; ++i) {
639 *r++ = (int) * out++;
640 *g++ = (int) * out++;
641 *b++ = (int) * out++;
649 } else { /* image->numcomps <= 2 : GRAY, GRAYA */
651 unsigned char *in, *inbuf, *out, *outbuf;
652 opj_image_comp_t *new_comps;
655 nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
656 in = inbuf = (unsigned char*)malloc(nr_samples);
657 out = outbuf = (unsigned char*)malloc(nr_samples);
658 g = (int*)calloc((size_t)max, sizeof(int));
659 b = (int*)calloc((size_t)max, sizeof(int));
661 if (inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) {
665 new_comps = (opj_image_comp_t*)realloc(image->comps,
666 (image->numcomps + 2) * sizeof(opj_image_comp_t));
668 if (new_comps == NULL) {
672 image->comps = new_comps;
674 if (image->numcomps == 2) {
675 image->comps[3] = image->comps[1];
678 image->comps[1] = image->comps[0];
679 image->comps[2] = image->comps[0];
681 image->comps[1].data = g;
682 image->comps[2].data = b;
684 image->numcomps += 2;
686 r = image->comps[0].data;
688 for (i = 0U; i < max; ++i) {
689 *in++ = (unsigned char) * r++;
691 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
693 r = image->comps[0].data;
694 g = image->comps[1].data;
695 b = image->comps[2].data;
697 for (i = 0U; i < max; ++i) {
698 *r++ = (int) * out++;
699 *g++ = (int) * out++;
700 *b++ = (int) * out++;
710 } else { /* prec > 8 */
711 unsigned short *in, *inbuf, *out, *outbuf;
712 opj_image_comp_t *new_comps;
715 nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
716 in = inbuf = (unsigned short*)malloc(nr_samples);
717 out = outbuf = (unsigned short*)malloc(nr_samples);
718 g = (int*)calloc((size_t)max, sizeof(int));
719 b = (int*)calloc((size_t)max, sizeof(int));
721 if (inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) {
725 new_comps = (opj_image_comp_t*)realloc(image->comps,
726 (image->numcomps + 2) * sizeof(opj_image_comp_t));
728 if (new_comps == NULL) {
732 image->comps = new_comps;
734 if (image->numcomps == 2) {
735 image->comps[3] = image->comps[1];
738 image->comps[1] = image->comps[0];
739 image->comps[2] = image->comps[0];
741 image->comps[1].data = g;
742 image->comps[2].data = b;
744 image->numcomps += 2;
746 r = image->comps[0].data;
748 for (i = 0U; i < max; ++i) {
749 *in++ = (unsigned short) * r++;
751 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
753 r = image->comps[0].data;
754 g = image->comps[1].data;
755 b = image->comps[2].data;
757 for (i = 0; i < max; ++i) {
758 *r++ = (int) * out++;
759 *g++ = (int) * out++;
760 *b++ = (int) * out++;
771 }/* if(image->numcomps > 2) */
773 cmsDeleteTransform(transform);
775 #ifdef OPJ_HAVE_LIBLCMS1
776 cmsCloseProfile(in_prof);
777 cmsCloseProfile(out_prof);
780 image->color_space = new_space;
782 }/* color_apply_icc_profile() */
784 void color_cielab_to_rgb(opj_image_t *image)
787 int enumcs, numcomps;
788 OPJ_COLOR_SPACE new_space;
790 numcomps = (int)image->numcomps;
793 fprintf(stderr, "%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
794 __FILE__, __LINE__, numcomps);
798 row = (int*)image->icc_profile_buf;
801 if (enumcs == 14) { /* CIELab */
802 int *L, *a, *b, *red, *green, *blue;
803 int *src0, *src1, *src2, *dst0, *dst1, *dst2;
804 double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
805 double minL, maxL, mina, maxa, minb, maxb;
806 unsigned int default_type;
809 cmsHTRANSFORM transform;
810 cmsUInt16Number RGB[3];
813 in = cmsCreateLab4Profile(NULL);
817 out = cmsCreate_sRGBProfile();
822 transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16,
823 INTENT_PERCEPTUAL, 0);
825 #ifdef OPJ_HAVE_LIBLCMS2
827 cmsCloseProfile(out);
829 if (transform == NULL) {
830 #ifdef OPJ_HAVE_LIBLCMS1
832 cmsCloseProfile(out);
836 new_space = OPJ_CLRSPC_SRGB;
838 prec0 = (double)image->comps[0].prec;
839 prec1 = (double)image->comps[1].prec;
840 prec2 = (double)image->comps[2].prec;
842 default_type = (unsigned int)row[1];
844 if (default_type == 0x44454600) { /* DEF : default */
849 oa = pow(2, prec1 - 1);
850 ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
860 L = src0 = image->comps[0].data;
861 a = src1 = image->comps[1].data;
862 b = src2 = image->comps[2].data;
864 max = image->comps[0].w * image->comps[0].h;
866 red = dst0 = (int*)malloc(max * sizeof(int));
867 green = dst1 = (int*)malloc(max * sizeof(int));
868 blue = dst2 = (int*)malloc(max * sizeof(int));
870 if (red == NULL || green == NULL || blue == NULL) {
874 minL = -(rl * ol) / (pow(2, prec0) - 1);
877 mina = -(ra * oa) / (pow(2, prec1) - 1);
880 minb = -(rb * ob) / (pow(2, prec2) - 1);
883 for (i = 0; i < max; ++i) {
884 Lab.L = minL + (double)(*L) * (maxL - minL) / (pow(2, prec0) - 1);
886 Lab.a = mina + (double)(*a) * (maxa - mina) / (pow(2, prec1) - 1);
888 Lab.b = minb + (double)(*b) * (maxb - minb) / (pow(2, prec2) - 1);
891 cmsDoTransform(transform, &Lab, RGB, 1);
897 cmsDeleteTransform(transform);
898 #ifdef OPJ_HAVE_LIBLCMS1
900 cmsCloseProfile(out);
903 image->comps[0].data = dst0;
905 image->comps[1].data = dst1;
907 image->comps[2].data = dst2;
909 image->color_space = new_space;
910 image->comps[0].prec = 16;
911 image->comps[1].prec = 16;
912 image->comps[2].prec = 16;
917 cmsDeleteTransform(transform);
918 #ifdef OPJ_HAVE_LIBLCMS1
920 cmsCloseProfile(out);
934 fprintf(stderr, "%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,
936 }/* color_cielab_to_rgb() */
938 #endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */
940 void color_cmyk_to_rgb(opj_image_t *image)
943 float sC, sM, sY, sK;
944 unsigned int w, h, max, i;
946 w = image->comps[0].w;
947 h = image->comps[0].h;
950 (image->numcomps < 4)
951 || (image->comps[0].dx != image->comps[1].dx) ||
952 (image->comps[0].dx != image->comps[2].dx) ||
953 (image->comps[0].dx != image->comps[3].dx)
954 || (image->comps[0].dy != image->comps[1].dy) ||
955 (image->comps[0].dy != image->comps[2].dy) ||
956 (image->comps[0].dy != image->comps[3].dy)
958 fprintf(stderr, "%s:%d:color_cmyk_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
965 sC = 1.0F / (float)((1 << image->comps[0].prec) - 1);
966 sM = 1.0F / (float)((1 << image->comps[1].prec) - 1);
967 sY = 1.0F / (float)((1 << image->comps[2].prec) - 1);
968 sK = 1.0F / (float)((1 << image->comps[3].prec) - 1);
970 for (i = 0; i < max; ++i) {
971 /* CMYK values from 0 to 1 */
972 C = (float)(image->comps[0].data[i]) * sC;
973 M = (float)(image->comps[1].data[i]) * sM;
974 Y = (float)(image->comps[2].data[i]) * sY;
975 K = (float)(image->comps[3].data[i]) * sK;
977 /* Invert all CMYK values */
983 /* CMYK -> RGB : RGB results from 0 to 255 */
984 image->comps[0].data[i] = (int)(255.0F * C * K); /* R */
985 image->comps[1].data[i] = (int)(255.0F * M * K); /* G */
986 image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */
989 free(image->comps[3].data);
990 image->comps[3].data = NULL;
991 image->comps[0].prec = 8;
992 image->comps[1].prec = 8;
993 image->comps[2].prec = 8;
994 image->numcomps -= 1;
995 image->color_space = OPJ_CLRSPC_SRGB;
997 for (i = 3; i < image->numcomps; ++i) {
998 memcpy(&(image->comps[i]), &(image->comps[i + 1]), sizeof(image->comps[i]));
1001 }/* color_cmyk_to_rgb() */
1004 * This code has been adopted from sjpx_openjpeg.c of ghostscript
1006 void color_esycc_to_rgb(opj_image_t *image)
1008 int y, cb, cr, sign1, sign2, val;
1009 unsigned int w, h, max, i;
1010 int flip_value = (1 << (image->comps[0].prec - 1));
1011 int max_value = (1 << image->comps[0].prec) - 1;
1014 (image->numcomps < 3)
1015 || (image->comps[0].dx != image->comps[1].dx) ||
1016 (image->comps[0].dx != image->comps[2].dx)
1017 || (image->comps[0].dy != image->comps[1].dy) ||
1018 (image->comps[0].dy != image->comps[2].dy)
1020 fprintf(stderr, "%s:%d:color_esycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
1025 w = image->comps[0].w;
1026 h = image->comps[0].h;
1028 sign1 = (int)image->comps[1].sgnd;
1029 sign2 = (int)image->comps[2].sgnd;
1033 for (i = 0; i < max; ++i) {
1035 y = image->comps[0].data[i];
1036 cb = image->comps[1].data[i];
1037 cr = image->comps[2].data[i];
1047 ((float)y - (float)0.0000368 * (float)cb
1048 + (float)1.40199 * (float)cr + (float)0.5);
1050 if (val > max_value) {
1052 } else if (val < 0) {
1055 image->comps[0].data[i] = val;
1058 ((float)1.0003 * (float)y - (float)0.344125 * (float)cb
1059 - (float)0.7141128 * (float)cr + (float)0.5);
1061 if (val > max_value) {
1063 } else if (val < 0) {
1066 image->comps[1].data[i] = val;
1069 ((float)0.999823 * (float)y + (float)1.77204 * (float)cb
1070 - (float)0.000008 * (float)cr + (float)0.5);
1072 if (val > max_value) {
1074 } else if (val < 0) {
1077 image->comps[2].data[i] = val;
1079 image->color_space = OPJ_CLRSPC_SRGB;
1081 }/* color_esycc_to_rgb() */