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 for(i = 0U; i < max; ++i)
113 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
114 ++y; ++cb; ++cr; ++r; ++g; ++b;
116 free(img->comps[0].data); img->comps[0].data = d0;
117 free(img->comps[1].data); img->comps[1].data = d1;
118 free(img->comps[2].data); img->comps[2].data = d2;
120 }/* sycc444_to_rgb() */
122 static void sycc422_to_rgb(opj_image_t *img)
124 int *d0, *d1, *d2, *r, *g, *b;
125 const int *y, *cb, *cr;
126 unsigned int maxw, maxh, max;
130 upb = (int)img->comps[0].prec;
131 offset = 1<<(upb - 1); upb = (1<<upb)-1;
133 maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
136 y = img->comps[0].data;
137 cb = img->comps[1].data;
138 cr = img->comps[2].data;
140 d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
141 d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
142 d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
144 for(i=0U; i < maxh; ++i)
146 for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
148 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
150 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
151 ++y; ++r; ++g; ++b; ++cb; ++cr;
154 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
155 ++y; ++r; ++g; ++b; ++cb; ++cr;
158 free(img->comps[0].data); img->comps[0].data = d0;
159 free(img->comps[1].data); img->comps[1].data = d1;
160 free(img->comps[2].data); img->comps[2].data = d2;
162 #if defined(USE_JPWL) || defined(USE_MJ2)
163 img->comps[1].w = maxw; img->comps[1].h = maxh;
164 img->comps[2].w = maxw; img->comps[2].h = maxh;
166 img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
167 img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
169 img->comps[1].dx = img->comps[0].dx;
170 img->comps[2].dx = img->comps[0].dx;
171 img->comps[1].dy = img->comps[0].dy;
172 img->comps[2].dy = img->comps[0].dy;
174 }/* sycc422_to_rgb() */
176 static void sycc420_to_rgb(opj_image_t *img)
178 int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
179 const int *y, *cb, *cr, *ny;
180 unsigned int maxw, maxh, max;
184 upb = (int)img->comps[0].prec;
185 offset = 1<<(upb - 1); upb = (1<<upb)-1;
187 maxw = (unsigned int)img->comps[0].w; maxh = (unsigned 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=0U; i < (maxh & ~(unsigned int)1U); i += 2U)
201 nr = r + maxw; ng = g + maxw; nb = b + maxw;
203 for(j=0; j < (maxw & ~(unsigned int)1U); j += 2U)
205 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
207 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
210 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
211 ++ny; ++nr; ++ng; ++nb;
212 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
213 ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
217 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
220 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
221 ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
223 y += maxw; r += maxw; g += maxw; b += maxw;
227 for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
229 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
233 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
235 ++y; ++r; ++g; ++b; ++cb; ++cr;
239 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
243 free(img->comps[0].data); img->comps[0].data = d0;
244 free(img->comps[1].data); img->comps[1].data = d1;
245 free(img->comps[2].data); img->comps[2].data = d2;
247 #if defined(USE_JPWL) || defined(USE_MJ2)
248 img->comps[1].w = maxw; img->comps[1].h = maxh;
249 img->comps[2].w = maxw; img->comps[2].h = maxh;
251 img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
252 img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
254 img->comps[1].dx = img->comps[0].dx;
255 img->comps[2].dx = img->comps[0].dx;
256 img->comps[1].dy = img->comps[0].dy;
257 img->comps[2].dy = img->comps[0].dy;
259 }/* sycc420_to_rgb() */
261 void color_sycc_to_rgb(opj_image_t *img)
263 if(img->numcomps < 3)
265 img->color_space = OPJ_CLRSPC_GRAY;
269 if((img->comps[0].dx == 1)
270 && (img->comps[1].dx == 2)
271 && (img->comps[2].dx == 2)
272 && (img->comps[0].dy == 1)
273 && (img->comps[1].dy == 2)
274 && (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */
279 if((img->comps[0].dx == 1)
280 && (img->comps[1].dx == 2)
281 && (img->comps[2].dx == 2)
282 && (img->comps[0].dy == 1)
283 && (img->comps[1].dy == 1)
284 && (img->comps[2].dy == 1))/* horizontal sub-sample only */
289 if((img->comps[0].dx == 1)
290 && (img->comps[1].dx == 1)
291 && (img->comps[2].dx == 1)
292 && (img->comps[0].dy == 1)
293 && (img->comps[1].dy == 1)
294 && (img->comps[2].dy == 1))/* no sub-sample */
300 fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,__LINE__);
303 img->color_space = OPJ_CLRSPC_SRGB;
305 }/* color_sycc_to_rgb() */
307 #if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
309 #ifdef OPJ_HAVE_LIBLCMS1
310 /* Bob Friesenhahn proposed:*/
311 #define cmsSigXYZData icSigXYZData
312 #define cmsSigLabData icSigLabData
313 #define cmsSigCmykData icSigCmykData
314 #define cmsSigYCbCrData icSigYCbCrData
315 #define cmsSigLuvData icSigLuvData
316 #define cmsSigGrayData icSigGrayData
317 #define cmsSigRgbData icSigRgbData
318 #define cmsUInt32Number DWORD
320 #define cmsColorSpaceSignature icColorSpaceSignature
321 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
323 #endif /* OPJ_HAVE_LIBLCMS1 */
325 /*#define DEBUG_PROFILE*/
326 void color_apply_icc_profile(opj_image_t *image)
328 cmsHPROFILE in_prof, out_prof;
329 cmsHTRANSFORM transform;
330 cmsColorSpaceSignature in_space, out_space;
331 cmsUInt32Number intent, in_type, out_type, nr_samples;
333 int prec, i, max, max_w, max_h;
334 OPJ_COLOR_SPACE oldspace;
337 cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
339 FILE *icm = fopen("debug.icm","wb");
340 fwrite( image->icc_profile_buf,1, image->icc_profile_len,icm);
344 if(in_prof == NULL) return;
346 in_space = cmsGetPCS(in_prof);
347 out_space = cmsGetColorSpace(in_prof);
348 intent = cmsGetHeaderRenderingIntent(in_prof);
351 max_w = (int)image->comps[0].w;
352 max_h = (int)image->comps[0].h;
353 prec = (int)image->comps[0].prec;
354 oldspace = image->color_space;
356 if(out_space == cmsSigRgbData) /* enumCS 16 */
360 in_type = TYPE_RGB_8;
361 out_type = TYPE_RGB_8;
365 in_type = TYPE_RGB_16;
366 out_type = TYPE_RGB_16;
368 out_prof = cmsCreate_sRGBProfile();
369 image->color_space = OPJ_CLRSPC_SRGB;
372 if(out_space == cmsSigGrayData) /* enumCS 17 */
374 in_type = TYPE_GRAY_8;
375 out_type = TYPE_RGB_8;
376 out_prof = cmsCreate_sRGBProfile();
377 image->color_space = OPJ_CLRSPC_SRGB;
380 if(out_space == cmsSigYCbCrData) /* enumCS 18 */
382 in_type = TYPE_YCbCr_16;
383 out_type = TYPE_RGB_16;
384 out_prof = cmsCreate_sRGBProfile();
385 image->color_space = OPJ_CLRSPC_SRGB;
390 fprintf(stderr,"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
391 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
392 __FILE__,__LINE__,out_space,
393 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
394 (out_space>>8) & 0xff, out_space & 0xff);
400 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
401 "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
402 max_w,max_h, (void*)in_prof,(void*)out_prof);
404 fprintf(stderr,"\trender_intent (%u)\n\t"
405 "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
406 " type: in(%u) out:(%u)\n",
409 (in_space>>24) & 0xff,(in_space>>16) & 0xff,
410 (in_space>>8) & 0xff, in_space & 0xff,
413 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
414 (out_space>>8) & 0xff, out_space & 0xff,
421 #endif /* DEBUG_PROFILE */
423 transform = cmsCreateTransform(in_prof, in_type,
424 out_prof, out_type, intent, 0);
426 #ifdef OPJ_HAVE_LIBLCMS2
427 /* Possible for: LCMS_VERSION >= 2000 :*/
428 cmsCloseProfile(in_prof);
429 cmsCloseProfile(out_prof);
432 if(transform == NULL)
435 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
436 "ICC Profile ignored.\n",__FILE__,__LINE__);
438 image->color_space = oldspace;
439 #ifdef OPJ_HAVE_LIBLCMS1
440 cmsCloseProfile(in_prof);
441 cmsCloseProfile(out_prof);
446 if(image->numcomps > 2)/* RGB, RGBA */
450 unsigned char *inbuf, *outbuf, *in, *out;
452 nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
453 in = inbuf = (unsigned char*)malloc(nr_samples);
454 out = outbuf = (unsigned char*)malloc(nr_samples);
456 r = image->comps[0].data;
457 g = image->comps[1].data;
458 b = image->comps[2].data;
460 for(i = 0; i < max; ++i)
462 *in++ = (unsigned char)*r++;
463 *in++ = (unsigned char)*g++;
464 *in++ = (unsigned char)*b++;
467 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
469 r = image->comps[0].data;
470 g = image->comps[1].data;
471 b = image->comps[2].data;
473 for(i = 0; i < max; ++i)
479 free(inbuf); free(outbuf);
483 unsigned short *inbuf, *outbuf, *in, *out;
485 nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short);
486 in = inbuf = (unsigned short*)malloc(nr_samples);
487 out = outbuf = (unsigned short*)malloc(nr_samples);
489 r = image->comps[0].data;
490 g = image->comps[1].data;
491 b = image->comps[2].data;
493 for(i = 0; i < max; ++i)
495 *in++ = (unsigned short)*r++;
496 *in++ = (unsigned short)*g++;
497 *in++ = (unsigned short)*b++;
500 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
502 r = image->comps[0].data;
503 g = image->comps[1].data;
504 b = image->comps[2].data;
506 for(i = 0; i < max; ++i)
512 free(inbuf); free(outbuf);
515 else /* GRAY, GRAYA */
517 unsigned char *in, *inbuf, *out, *outbuf;
519 nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char);
520 in = inbuf = (unsigned char*)malloc(nr_samples);
521 out = outbuf = (unsigned char*)malloc(nr_samples);
523 image->comps = (opj_image_comp_t*)
524 realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
526 if(image->numcomps == 2)
527 image->comps[3] = image->comps[1];
529 image->comps[1] = image->comps[0];
530 image->comps[2] = image->comps[0];
532 image->comps[1].data = (int*)calloc((size_t)max, sizeof(int));
533 image->comps[2].data = (int*)calloc((size_t)max, sizeof(int));
535 image->numcomps += 2;
537 r = image->comps[0].data;
539 for(i = 0; i < max; ++i)
541 *in++ = (unsigned char)*r++;
543 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
545 r = image->comps[0].data;
546 g = image->comps[1].data;
547 b = image->comps[2].data;
549 for(i = 0; i < max; ++i)
551 *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
553 free(inbuf); free(outbuf);
555 }/* if(image->numcomps */
557 cmsDeleteTransform(transform);
559 #ifdef OPJ_HAVE_LIBLCMS1
560 cmsCloseProfile(in_prof);
561 cmsCloseProfile(out_prof);
563 }/* color_apply_icc_profile() */
565 void color_apply_conversion(opj_image_t *image)
568 int enumcs, numcomps;
570 image->color_space = OPJ_CLRSPC_SRGB;
572 numcomps = image->numcomps;
576 fprintf(stderr,"%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
577 __FILE__,__LINE__,numcomps);
581 row = (int*)image->icc_profile_buf;
584 if(enumcs == 14)// CIELab
586 int *L, *a, *b, *red, *green, *blue;
587 int *src0, *src1, *src2, *dst0, *dst1, *dst2;
588 double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
589 double minL, maxL, mina, maxa, minb, maxb;
590 unsigned int default_type;
593 cmsHTRANSFORM transform;
594 cmsUInt16Number RGB[3];
597 in = cmsCreateLab4Profile(NULL);
598 out = cmsCreate_sRGBProfile();
600 transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16, INTENT_PERCEPTUAL, 0);
604 cmsCloseProfile(out);
606 if(transform == NULL)
610 cmsCloseProfile(out);
614 prec0 = (double)image->comps[0].prec;
615 prec1 = (double)image->comps[1].prec;
616 prec2 = (double)image->comps[2].prec;
618 default_type = row[1];
620 if(default_type == 0x44454600)// DEF : default
622 rl = 100; ra = 170; rb = 200;
624 oa = pow(2, prec1 - 1);
625 ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
629 rl = row[2]; ra = row[4]; rb = row[6];
630 ol = row[3]; oa = row[5]; ob = row[7];
633 L = src0 = image->comps[0].data;
634 a = src1 = image->comps[1].data;
635 b = src2 = image->comps[2].data;
637 max = image->comps[0].w * image->comps[0].h;
639 red = dst0 = (int*)malloc(max * sizeof(int));
640 green = dst1 = (int*)malloc(max * sizeof(int));
641 blue = dst2 = (int*)malloc(max * sizeof(int));
643 minL = -(rl * ol)/(pow(2, prec0)-1);
646 mina = -(ra * oa)/(pow(2, prec1)-1);
649 minb = -(rb * ob)/(pow(2, prec2)-1);
652 for(i = 0; i < max; ++i)
654 Lab.L = minL + (double)(*L) * (maxL - minL)/(pow(2, prec0)-1); ++L;
655 Lab.a = mina + (double)(*a) * (maxa - mina)/(pow(2, prec1)-1); ++a;
656 Lab.b = minb + (double)(*b) * (maxb - minb)/(pow(2, prec2)-1); ++b;
658 cmsDoTransform(transform, &Lab, RGB, 1);
664 cmsDeleteTransform(transform);
667 cmsCloseProfile(out);
669 free(src0); image->comps[0].data = dst0;
670 free(src1); image->comps[1].data = dst1;
671 free(src2); image->comps[2].data = dst2;
673 image->color_space = OPJ_CLRSPC_SRGB;
674 image->comps[0].prec = 16;
675 image->comps[1].prec = 16;
676 image->comps[2].prec = 16;
681 fprintf(stderr,"%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,__LINE__, enumcs);
682 }// color_apply_conversion()
684 #endif // HAVE_LIBLCMS2 || HAVE_LIBLCMS1
686 void color_cmyk_to_rgb(opj_image_t *image)
689 float sC, sM, sY, sK;
690 unsigned int w, h, max, i;
692 w = image->comps[0].w;
693 h = image->comps[0].h;
695 if(image->numcomps < 4) return;
699 sC = 1.0F / (float)((1 << image->comps[0].prec) - 1);
700 sM = 1.0F / (float)((1 << image->comps[1].prec) - 1);
701 sY = 1.0F / (float)((1 << image->comps[2].prec) - 1);
702 sK = 1.0F / (float)((1 << image->comps[3].prec) - 1);
704 for(i = 0; i < max; ++i)
706 /* CMYK values from 0 to 1 */
707 C = (float)(image->comps[0].data[i]) * sC;
708 M = (float)(image->comps[1].data[i]) * sM;
709 Y = (float)(image->comps[2].data[i]) * sY;
710 K = (float)(image->comps[3].data[i]) * sK;
712 /* Invert all CMYK values */
718 /* CMYK -> RGB : RGB results from 0 to 255 */
719 image->comps[0].data[i] = (int)(255.0F * C * K); /* R */
720 image->comps[1].data[i] = (int)(255.0F * M * K); /* G */
721 image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */
724 free(image->comps[3].data); image->comps[3].data = NULL;
725 image->comps[0].prec = 8;
726 image->comps[1].prec = 8;
727 image->comps[2].prec = 8;
728 image->numcomps -= 1;
729 image->color_space = OPJ_CLRSPC_SRGB;
731 for (i = 3; i < image->numcomps; ++i) {
732 memcpy(&(image->comps[i]), &(image->comps[i+1]), sizeof(image->comps[i]));
735 }// color_cmyk_to_rgb()
738 // This code has been adopted from sjpx_openjpeg.c of ghostscript
740 void color_esycc_to_rgb(opj_image_t *image)
742 int y, cb, cr, sign1, sign2, val;
743 unsigned int w, h, max, i;
744 int flip_value = (1 << (image->comps[0].prec-1));
745 int max_value = (~(-1 << image->comps[0].prec));
747 if(image->numcomps < 3) return;
749 w = image->comps[0].w;
750 h = image->comps[0].h;
752 sign1 = image->comps[1].sgnd;
753 sign2 = image->comps[2].sgnd;
757 for(i = 0; i < max; ++i)
760 y = image->comps[0].data[i]; cb = image->comps[1].data[i]; cr = image->comps[2].data[i];
762 if( !sign1) cb -= flip_value;
763 if( !sign2) cr -= flip_value;
766 ((float)y - (float)0.0000368 * (float)cb
767 + (float)1.40199 * (float)cr + (float)0.5);
769 if(val > max_value) val = max_value; else if(val < 0) val = 0;
770 image->comps[0].data[i] = val;
773 ((float)1.0003 * (float)y - (float)0.344125 * (float)cb
774 - (float)0.7141128 * (float)cr + (float)0.5);
776 if(val > max_value) val = max_value; else if(val < 0) val = 0;
777 image->comps[1].data[i] = val;
780 ((float)0.999823 * (float)y + (float)1.77204 * (float)cb
781 - (float)0.000008 *(float)cr + (float)0.5);
783 if(val > max_value) val = max_value; else if(val < 0) val = 0;
784 image->comps[2].data[i] = val;
786 image->color_space = OPJ_CLRSPC_SRGB;
788 }// color_esycc_to_rgb()