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_apps_config.h"
41 #ifdef OPJ_HAVE_LIBLCMS2
44 #ifdef OPJ_HAVE_LIBLCMS1
49 #define OPJ_CLRSPC_GRAY CLRSPC_GRAY
50 #define OPJ_CLRSPC_SRGB CLRSPC_SRGB
53 /*--------------------------------------------------------
54 Matrix for sYCC, Amendment 1 to IEC 61966-2-1
56 Y : 0.299 0.587 0.114 :R
57 Cb: -0.1687 -0.3312 0.5 :G
58 Cr: 0.5 -0.4187 -0.0812 :B
62 R: 1 -3.68213e-05 1.40199 :Y
63 G: 1.00003 -0.344125 -0.714128 :Cb - 2^(prec - 1)
64 B: 0.999823 1.77204 -8.04142e-06 :Cr - 2^(prec - 1)
66 -----------------------------------------------------------*/
67 static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
68 int *out_r, int *out_g, int *out_b)
72 cb -= offset; cr -= offset;
73 r = y + (int)(1.402 * (float)cr);
74 if(r < 0) r = 0; else if(r > upb) r = upb; *out_r = r;
76 g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
77 if(g < 0) g = 0; else if(g > upb) g = upb; *out_g = g;
79 b = y + (int)(1.772 * (float)cb);
80 if(b < 0) b = 0; else if(b > upb) b = upb; *out_b = b;
83 static void sycc444_to_rgb(opj_image_t *img)
85 int *d0, *d1, *d2, *r, *g, *b;
86 const int *y, *cb, *cr;
87 int maxw, maxh, max, i, offset, upb;
89 i = img->comps[0].prec;
90 offset = 1<<(i - 1); upb = (1<<i)-1;
92 maxw = img->comps[0].w; maxh = img->comps[0].h;
95 y = img->comps[0].data;
96 cb = img->comps[1].data;
97 cr = img->comps[2].data;
99 d0 = r = (int*)malloc(sizeof(int) * max);
100 d1 = g = (int*)malloc(sizeof(int) * max);
101 d2 = b = (int*)malloc(sizeof(int) * max);
103 for(i = 0; i < max; ++i)
105 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
107 ++y; ++cb; ++cr; ++r; ++g; ++b;
109 free(img->comps[0].data); img->comps[0].data = d0;
110 free(img->comps[1].data); img->comps[1].data = d1;
111 free(img->comps[2].data); img->comps[2].data = d2;
113 }/* sycc444_to_rgb() */
115 static void sycc422_to_rgb(opj_image_t *img)
117 int *d0, *d1, *d2, *r, *g, *b;
118 const int *y, *cb, *cr;
119 int maxw, maxh, max, offset, upb;
122 i = img->comps[0].prec;
123 offset = 1<<(i - 1); upb = (1<<i)-1;
125 maxw = img->comps[0].w; maxh = img->comps[0].h;
128 y = img->comps[0].data;
129 cb = img->comps[1].data;
130 cr = img->comps[2].data;
132 d0 = r = (int*)malloc(sizeof(int) * max);
133 d1 = g = (int*)malloc(sizeof(int) * max);
134 d2 = b = (int*)malloc(sizeof(int) * max);
136 for(i=0; i < maxh; ++i)
138 for(j=0; j < maxw; j += 2)
140 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
144 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
146 ++y; ++r; ++g; ++b; ++cb; ++cr;
149 free(img->comps[0].data); img->comps[0].data = d0;
150 free(img->comps[1].data); img->comps[1].data = d1;
151 free(img->comps[2].data); img->comps[2].data = d2;
153 img->comps[1].w = maxw; img->comps[1].h = maxh;
154 img->comps[2].w = maxw; img->comps[2].h = maxh;
155 img->comps[1].dx = img->comps[0].dx;
156 img->comps[2].dx = img->comps[0].dx;
157 img->comps[1].dy = img->comps[0].dy;
158 img->comps[2].dy = img->comps[0].dy;
160 }/* sycc422_to_rgb() */
162 static void sycc420_to_rgb(opj_image_t *img)
164 int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
165 const int *y, *cb, *cr, *ny;
166 int maxw, maxh, max, offset, upb;
169 i = img->comps[0].prec;
170 offset = 1<<(i - 1); upb = (1<<i)-1;
172 maxw = img->comps[0].w; maxh = img->comps[0].h;
175 y = img->comps[0].data;
176 cb = img->comps[1].data;
177 cr = img->comps[2].data;
179 d0 = r = (int*)malloc(sizeof(int) * max);
180 d1 = g = (int*)malloc(sizeof(int) * max);
181 d2 = b = (int*)malloc(sizeof(int) * max);
183 for(i=0; i < maxh; i += 2)
186 nr = r + maxw; ng = g + maxw; nb = b + maxw;
188 for(j=0; j < maxw; j += 2)
190 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
194 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
198 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
200 ++ny; ++nr; ++ng; ++nb;
202 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
204 ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
206 y += maxw; r += maxw; g += maxw; b += maxw;
208 free(img->comps[0].data); img->comps[0].data = d0;
209 free(img->comps[1].data); img->comps[1].data = d1;
210 free(img->comps[2].data); img->comps[2].data = d2;
212 img->comps[1].w = maxw; img->comps[1].h = maxh;
213 img->comps[2].w = maxw; img->comps[2].h = maxh;
214 img->comps[1].dx = img->comps[0].dx;
215 img->comps[2].dx = img->comps[0].dx;
216 img->comps[1].dy = img->comps[0].dy;
217 img->comps[2].dy = img->comps[0].dy;
219 }/* sycc420_to_rgb() */
221 void color_sycc_to_rgb(opj_image_t *img)
223 if(img->numcomps < 3)
225 img->color_space = OPJ_CLRSPC_GRAY;
229 if((img->comps[0].dx == 1)
230 && (img->comps[1].dx == 2)
231 && (img->comps[2].dx == 2)
232 && (img->comps[0].dy == 1)
233 && (img->comps[1].dy == 2)
234 && (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */
239 if((img->comps[0].dx == 1)
240 && (img->comps[1].dx == 2)
241 && (img->comps[2].dx == 2)
242 && (img->comps[0].dy == 1)
243 && (img->comps[1].dy == 1)
244 && (img->comps[2].dy == 1))/* horizontal sub-sample only */
249 if((img->comps[0].dx == 1)
250 && (img->comps[1].dx == 1)
251 && (img->comps[2].dx == 1)
252 && (img->comps[0].dy == 1)
253 && (img->comps[1].dy == 1)
254 && (img->comps[2].dy == 1))/* no sub-sample */
260 fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n",
264 img->color_space = OPJ_CLRSPC_SRGB;
266 }/* color_sycc_to_rgb() */
268 #if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
269 #ifdef OPJ_HAVE_LIBLCMS1
270 /* Bob Friesenhahn proposed:*/
271 #define cmsSigXYZData icSigXYZData
272 #define cmsSigLabData icSigLabData
273 #define cmsSigCmykData icSigCmykData
274 #define cmsSigYCbCrData icSigYCbCrData
275 #define cmsSigLuvData icSigLuvData
276 #define cmsSigGrayData icSigGrayData
277 #define cmsSigRgbData icSigRgbData
278 #define cmsUInt32Number DWORD
280 #define cmsColorSpaceSignature icColorSpaceSignature
281 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
283 #endif /* OPJ_HAVE_LIBLCMS1 */
285 void color_apply_icc_profile(opj_image_t *image)
287 cmsHPROFILE in_prof, out_prof;
288 cmsHTRANSFORM transform;
289 cmsColorSpaceSignature in_space, out_space;
290 cmsUInt32Number intent, in_type, out_type, nr_samples;
292 int prec, i, max, max_w, max_h;
293 OPJ_COLOR_SPACE oldspace;
296 cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
298 if(in_prof == NULL) return;
300 in_space = cmsGetPCS(in_prof);
301 out_space = cmsGetColorSpace(in_prof);
302 intent = cmsGetHeaderRenderingIntent(in_prof);
305 max_w = image->comps[0].w; max_h = image->comps[0].h;
306 prec = image->comps[0].prec;
307 oldspace = image->color_space;
309 if(out_space == cmsSigRgbData) /* enumCS 16 */
311 in_type = TYPE_RGB_16;
312 out_type = TYPE_RGB_16;
313 out_prof = cmsCreate_sRGBProfile();
314 image->color_space = OPJ_CLRSPC_SRGB;
317 if(out_space == cmsSigGrayData) /* enumCS 17 */
319 in_type = TYPE_GRAY_8;
320 out_type = TYPE_RGB_8;
321 out_prof = cmsCreate_sRGBProfile();
322 image->color_space = OPJ_CLRSPC_SRGB;
325 if(out_space == cmsSigYCbCrData) /* enumCS 18 */
327 in_type = TYPE_YCbCr_16;
328 out_type = TYPE_RGB_16;
329 out_prof = cmsCreate_sRGBProfile();
330 image->color_space = OPJ_CLRSPC_SRGB;
335 fprintf(stderr,"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
336 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
337 __FILE__,__LINE__,out_space,
338 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
339 (out_space>>8) & 0xff, out_space & 0xff);
345 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
346 "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
347 max_w,max_h, (void*)in_prof,(void*)out_prof);
349 fprintf(stderr,"\trender_intent (%u)\n\t"
350 "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
351 " type: in(%u) out:(%u)\n",
354 (in_space>>24) & 0xff,(in_space>>16) & 0xff,
355 (in_space>>8) & 0xff, in_space & 0xff,
358 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
359 (out_space>>8) & 0xff, out_space & 0xff,
363 #endif /* DEBUG_PROFILE */
365 transform = cmsCreateTransform(in_prof, in_type,
366 out_prof, out_type, intent, 0);
368 #ifdef OPJ_HAVE_LIBLCMS2
369 /* Possible for: LCMS_VERSION >= 2000 :*/
370 cmsCloseProfile(in_prof);
371 cmsCloseProfile(out_prof);
374 if(transform == NULL)
377 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
378 "ICC Profile ignored.\n",__FILE__,__LINE__);
380 image->color_space = oldspace;
381 #ifdef OPJ_HAVE_LIBLCMS1
382 cmsCloseProfile(in_prof);
383 cmsCloseProfile(out_prof);
388 if(image->numcomps > 2)/* RGB, RGBA */
390 unsigned short *inbuf, *outbuf, *in, *out;
391 max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned short);
392 in = inbuf = (unsigned short*)malloc(nr_samples);
393 out = outbuf = (unsigned short*)malloc(nr_samples);
395 r = image->comps[0].data;
396 g = image->comps[1].data;
397 b = image->comps[2].data;
399 for(i = 0; i < max; ++i)
401 *in++ = (unsigned short)*r++;
402 *in++ = (unsigned short)*g++;
403 *in++ = (unsigned short)*b++;
406 cmsDoTransform(transform, inbuf, outbuf, max);
408 r = image->comps[0].data;
409 g = image->comps[1].data;
410 b = image->comps[2].data;
412 for(i = 0; i < max; ++i)
418 free(inbuf); free(outbuf);
420 else /* GRAY, GRAYA */
422 unsigned char *in, *inbuf, *out, *outbuf;
424 max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned char);
425 in = inbuf = (unsigned char*)malloc(nr_samples);
426 out = outbuf = (unsigned char*)malloc(nr_samples);
428 image->comps = (opj_image_comp_t*)
429 realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
431 if(image->numcomps == 2)
432 image->comps[3] = image->comps[1];
434 image->comps[1] = image->comps[0];
435 image->comps[2] = image->comps[0];
437 image->comps[1].data = (int*)calloc(max, sizeof(int));
438 image->comps[2].data = (int*)calloc(max, sizeof(int));
440 image->numcomps += 2;
442 r = image->comps[0].data;
444 for(i = 0; i < max; ++i)
446 *in++ = (unsigned char)*r++;
448 cmsDoTransform(transform, inbuf, outbuf, max);
450 r = image->comps[0].data;
451 g = image->comps[1].data;
452 b = image->comps[2].data;
454 for(i = 0; i < max; ++i)
456 *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
458 free(inbuf); free(outbuf);
460 }/* if(image->numcomps */
462 cmsDeleteTransform(transform);
464 #ifdef OPJ_HAVE_LIBLCMS1
465 cmsCloseProfile(in_prof);
466 cmsCloseProfile(out_prof);
468 }/* color_apply_icc_profile() */
470 #endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */