2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
37 #include "opj_config.h"
49 #define OPJ_CLRSPC_GRAY CLRSPC_GRAY
53 #define OPJ_CLRSPC_SRGB CLRSPC_SRGB
56 /*--------------------------------------------------------
57 Matrix for sYCC, Amendment 1 to IEC 61966-2-1
59 Y : 0.299 0.587 0.114 :R
60 Cb: -0.1687 -0.3312 0.5 :G
61 Cr: 0.5 -0.4187 -0.0812 :B
65 R: 1 -3.68213e-05 1.40199 :Y
66 G: 1.00003 -0.344125 -0.714128 :Cb - 2^(prec - 1)
67 B: 0.999823 1.77204 -8.04142e-06 :Cr - 2^(prec - 1)
69 -----------------------------------------------------------*/
70 static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
71 int *out_r, int *out_g, int *out_b)
75 cb -= offset; cr -= offset;
76 r = y + (int)(1.402 * (float)cr);
77 if(r < 0) r = 0; else if(r > upb) r = upb; *out_r = r;
79 g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
80 if(g < 0) g = 0; else if(g > upb) g = upb; *out_g = g;
82 b = y + (int)(1.772 * (float)cb);
83 if(b < 0) b = 0; else if(b > upb) b = upb; *out_b = b;
86 static void sycc444_to_rgb(opj_image_t *img)
88 int *d0, *d1, *d2, *r, *g, *b;
89 const int *y, *cb, *cr;
90 int maxw, maxh, max, i, offset, upb;
92 i = img->comps[0].prec;
93 offset = 1<<(i - 1); upb = (1<<i)-1;
95 maxw = img->comps[0].w; maxh = img->comps[0].h;
98 y = img->comps[0].data;
99 cb = img->comps[1].data;
100 cr = img->comps[2].data;
102 d0 = r = (int*)malloc(sizeof(int) * max);
103 d1 = g = (int*)malloc(sizeof(int) * max);
104 d2 = b = (int*)malloc(sizeof(int) * max);
106 for(i = 0; i < max; ++i)
108 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
110 ++y; ++cb; ++cr; ++r; ++g; ++b;
112 free(img->comps[0].data); img->comps[0].data = d0;
113 free(img->comps[1].data); img->comps[1].data = d1;
114 free(img->comps[2].data); img->comps[2].data = d2;
116 }/* sycc444_to_rgb() */
118 static void sycc422_to_rgb(opj_image_t *img)
120 int *d0, *d1, *d2, *r, *g, *b;
121 const int *y, *cb, *cr;
122 int maxw, maxh, max, offset, upb;
125 i = img->comps[0].prec;
126 offset = 1<<(i - 1); upb = (1<<i)-1;
128 maxw = img->comps[0].w; maxh = img->comps[0].h;
131 y = img->comps[0].data;
132 cb = img->comps[1].data;
133 cr = img->comps[2].data;
135 d0 = r = (int*)malloc(sizeof(int) * max);
136 d1 = g = (int*)malloc(sizeof(int) * max);
137 d2 = b = (int*)malloc(sizeof(int) * max);
139 for(i=0; i < maxh; ++i)
141 for(j=0; j < maxw; j += 2)
143 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
147 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
149 ++y; ++r; ++g; ++b; ++cb; ++cr;
152 free(img->comps[0].data); img->comps[0].data = d0;
153 free(img->comps[1].data); img->comps[1].data = d1;
154 free(img->comps[2].data); img->comps[2].data = d2;
156 img->comps[1].w = maxw; img->comps[1].h = maxh;
157 img->comps[2].w = maxw; img->comps[2].h = maxh;
158 img->comps[1].dx = img->comps[0].dx;
159 img->comps[2].dx = img->comps[0].dx;
160 img->comps[1].dy = img->comps[0].dy;
161 img->comps[2].dy = img->comps[0].dy;
163 }/* sycc422_to_rgb() */
165 static void sycc420_to_rgb(opj_image_t *img)
167 int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
168 const int *y, *cb, *cr, *ny;
169 int maxw, maxh, max, offset, upb;
172 i = img->comps[0].prec;
173 offset = 1<<(i - 1); upb = (1<<i)-1;
175 maxw = img->comps[0].w; maxh = img->comps[0].h;
178 y = img->comps[0].data;
179 cb = img->comps[1].data;
180 cr = img->comps[2].data;
182 d0 = r = (int*)malloc(sizeof(int) * max);
183 d1 = g = (int*)malloc(sizeof(int) * max);
184 d2 = b = (int*)malloc(sizeof(int) * max);
186 for(i=0; i < maxh; i += 2)
189 nr = r + maxw; ng = g + maxw; nb = b + maxw;
191 for(j=0; j < maxw; j += 2)
193 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
197 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
201 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
203 ++ny; ++nr; ++ng; ++nb;
205 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
207 ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
209 y += maxw; r += maxw; g += maxw; b += maxw;
211 free(img->comps[0].data); img->comps[0].data = d0;
212 free(img->comps[1].data); img->comps[1].data = d1;
213 free(img->comps[2].data); img->comps[2].data = d2;
215 img->comps[1].w = maxw; img->comps[1].h = maxh;
216 img->comps[2].w = maxw; img->comps[2].h = maxh;
217 img->comps[1].dx = img->comps[0].dx;
218 img->comps[2].dx = img->comps[0].dx;
219 img->comps[1].dy = img->comps[0].dy;
220 img->comps[2].dy = img->comps[0].dy;
222 }/* sycc420_to_rgb() */
224 void color_sycc_to_rgb(opj_image_t *img)
226 if(img->numcomps < 3)
228 img->color_space = OPJ_CLRSPC_GRAY;
232 if((img->comps[0].dx == 1)
233 && (img->comps[1].dx == 2)
234 && (img->comps[2].dx == 2)
235 && (img->comps[0].dy == 1)
236 && (img->comps[1].dy == 2)
237 && (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */
242 if((img->comps[0].dx == 1)
243 && (img->comps[1].dx == 2)
244 && (img->comps[2].dx == 2)
245 && (img->comps[0].dy == 1)
246 && (img->comps[1].dy == 1)
247 && (img->comps[2].dy == 1))/* horizontal sub-sample only */
252 if((img->comps[0].dx == 1)
253 && (img->comps[1].dx == 1)
254 && (img->comps[2].dx == 1)
255 && (img->comps[0].dy == 1)
256 && (img->comps[1].dy == 1)
257 && (img->comps[2].dy == 1))/* no sub-sample */
263 fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n",
267 img->color_space = OPJ_CLRSPC_SRGB;
269 }/* color_sycc_to_rgb() */
271 #if defined(HAVE_LIBLCMS2) || defined(HAVE_LIBLCMS1)
273 /* Bob Friesenhahn proposed:*/
274 #define cmsSigXYZData icSigXYZData
275 #define cmsSigLabData icSigLabData
276 #define cmsSigCmykData icSigCmykData
277 #define cmsSigYCbCrData icSigYCbCrData
278 #define cmsSigLuvData icSigLuvData
279 #define cmsSigGrayData icSigGrayData
280 #define cmsSigRgbData icSigRgbData
281 #define cmsUInt32Number DWORD
283 #define cmsColorSpaceSignature icColorSpaceSignature
284 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
286 #endif /* HAVE_LIBLCMS1 */
288 void color_apply_icc_profile(opj_image_t *image)
290 cmsHPROFILE in_prof, out_prof;
291 cmsHTRANSFORM transform;
292 cmsColorSpaceSignature in_space, out_space;
293 cmsUInt32Number intent, in_type, out_type, nr_samples;
295 int prec, i, max, max_w, max_h;
296 OPJ_COLOR_SPACE oldspace;
299 cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
301 if(in_prof == NULL) return;
303 in_space = cmsGetPCS(in_prof);
304 out_space = cmsGetColorSpace(in_prof);
305 intent = cmsGetHeaderRenderingIntent(in_prof);
308 max_w = image->comps[0].w; max_h = image->comps[0].h;
309 prec = image->comps[0].prec;
310 oldspace = image->color_space;
312 if(out_space == cmsSigRgbData) /* enumCS 16 */
314 in_type = TYPE_RGB_16;
315 out_type = TYPE_RGB_16;
316 out_prof = cmsCreate_sRGBProfile();
317 image->color_space = OPJ_CLRSPC_SRGB;
320 if(out_space == cmsSigGrayData) /* enumCS 17 */
322 in_type = TYPE_GRAY_8;
323 out_type = TYPE_RGB_8;
324 out_prof = cmsCreate_sRGBProfile();
325 image->color_space = OPJ_CLRSPC_SRGB;
328 if(out_space == cmsSigYCbCrData) /* enumCS 18 */
330 in_type = TYPE_YCbCr_16;
331 out_type = TYPE_RGB_16;
332 out_prof = cmsCreate_sRGBProfile();
333 image->color_space = OPJ_CLRSPC_SRGB;
338 fprintf(stderr,"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
339 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
340 __FILE__,__LINE__,out_space,
341 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
342 (out_space>>8) & 0xff, out_space & 0xff);
348 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
349 "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
350 max_w,max_h, (void*)in_prof,(void*)out_prof);
352 fprintf(stderr,"\trender_intent (%u)\n\t"
353 "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
354 " type: in(%u) out:(%u)\n",
357 (in_space>>24) & 0xff,(in_space>>16) & 0xff,
358 (in_space>>8) & 0xff, in_space & 0xff,
361 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
362 (out_space>>8) & 0xff, out_space & 0xff,
366 #endif /* DEBUG_PROFILE */
368 transform = cmsCreateTransform(in_prof, in_type,
369 out_prof, out_type, intent, 0);
372 /* Possible for: LCMS_VERSION >= 2000 :*/
373 cmsCloseProfile(in_prof);
374 cmsCloseProfile(out_prof);
377 if(transform == NULL)
380 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
381 "ICC Profile ignored.\n",__FILE__,__LINE__);
383 image->color_space = oldspace;
385 cmsCloseProfile(in_prof);
386 cmsCloseProfile(out_prof);
391 if(image->numcomps > 2)/* RGB, RGBA */
393 unsigned short *inbuf, *outbuf, *in, *out;
394 max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned short);
395 in = inbuf = (unsigned short*)malloc(nr_samples);
396 out = outbuf = (unsigned short*)malloc(nr_samples);
398 r = image->comps[0].data;
399 g = image->comps[1].data;
400 b = image->comps[2].data;
402 for(i = 0; i < max; ++i)
404 *in++ = (unsigned short)*r++;
405 *in++ = (unsigned short)*g++;
406 *in++ = (unsigned short)*b++;
409 cmsDoTransform(transform, inbuf, outbuf, max);
411 r = image->comps[0].data;
412 g = image->comps[1].data;
413 b = image->comps[2].data;
415 for(i = 0; i < max; ++i)
421 free(inbuf); free(outbuf);
423 else /* GRAY, GRAYA */
425 unsigned char *in, *inbuf, *out, *outbuf;
427 max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned char);
428 in = inbuf = (unsigned char*)malloc(nr_samples);
429 out = outbuf = (unsigned char*)malloc(nr_samples);
431 image->comps = (opj_image_comp_t*)
432 realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
434 if(image->numcomps == 2)
435 image->comps[3] = image->comps[1];
437 image->comps[1] = image->comps[0];
438 image->comps[2] = image->comps[0];
440 image->comps[1].data = (int*)calloc(max, sizeof(int));
441 image->comps[2].data = (int*)calloc(max, sizeof(int));
443 image->numcomps += 2;
445 r = image->comps[0].data;
447 for(i = 0; i < max; ++i)
449 *in++ = (unsigned char)*r++;
451 cmsDoTransform(transform, inbuf, outbuf, max);
453 r = image->comps[0].data;
454 g = image->comps[1].data;
455 b = image->comps[2].data;
457 for(i = 0; i < max; ++i)
459 *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
461 free(inbuf); free(outbuf);
463 }/* if(image->numcomps */
465 cmsDeleteTransform(transform);
468 cmsCloseProfile(in_prof);
469 cmsCloseProfile(out_prof);
471 }/* color_apply_icc_profile() */
473 #endif /* HAVE_LIBLCMS2 || HAVE_LIBLCMS1 */