1 /*
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
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <math.h>
36 
37 #include "opj_config.h"
38 #include "openjpeg.h"
39 #include "color.h"
40 
41 #ifdef HAVE_LIBLCMS2
42 #include <lcms2.h>
43 #endif
44 #ifdef HAVE_LIBLCMS1
45 #include <lcms.h>
46 #endif
47 
48 /*--------------------------------------------------------
49 Matrix f�r sYCC, Amendment 1 to IEC 61966-2-1
50 
51 Y :   0.299   0.587    0.114   :R
52 Cb:  -0.1687 -0.3312   0.5     :G
53 Cr:   0.5    -0.4187  -0.0812  :B
54 
55 Inverse:
56 
57 R: 1        -3.68213e-05    1.40199      :Y
58 G: 1.00003  -0.344125      -0.714128     :Cb - 2^(prec - 1)
59 B: 0.999823  1.77204       -8.04142e-06  :Cr - 2^(prec - 1)
60 
61 -----------------------------------------------------------*/
sycc_to_rgb(int offset,int upb,int y,int cb,int cr,int * out_r,int * out_g,int * out_b)62 static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
63 	int *out_r, int *out_g, int *out_b)
64 {
65 	int r, g, b;
66 
67 	cb -= offset; cr -= offset;
68 	r = y + (int)(1.402 * (float)cr);
69 	if(r < 0) r = 0; else if(r > upb) r = upb; *out_r = r;
70 
71 	g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
72 	if(g < 0) g = 0; else if(g > upb) g = upb; *out_g = g;
73 
74 	b = y + (int)(1.772 * (float)cb);
75 	if(b < 0) b = 0; else if(b > upb) b = upb; *out_b = b;
76 }
77 
sycc444_to_rgb(opj_image_t * img)78 static void sycc444_to_rgb(opj_image_t *img)
79 {
80 	int *d0, *d1, *d2, *r, *g, *b;
81 	const int *y, *cb, *cr;
82 	int maxw, maxh, max, i, offset, upb;
83 
84 	i = img->comps[0].prec;
85 	offset = 1<<(i - 1); upb = (1<<i)-1;
86 
87 	maxw = img->comps[0].w; maxh = img->comps[0].h;
88 	max = maxw * maxh;
89 
90 	y = img->comps[0].data;
91 	cb = img->comps[1].data;
92 	cr = img->comps[2].data;
93 
94 	d0 = r = (int*)malloc(sizeof(int) * max);
95 	d1 = g = (int*)malloc(sizeof(int) * max);
96 	d2 = b = (int*)malloc(sizeof(int) * max);
97 
98 	for(i = 0; i < max; ++i)
99    {
100 	sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
101 
102 	++y; ++cb; ++cr; ++r; ++g; ++b;
103    }
104 	free(img->comps[0].data); img->comps[0].data = d0;
105 	free(img->comps[1].data); img->comps[1].data = d1;
106 	free(img->comps[2].data); img->comps[2].data = d2;
107 
108 }/* sycc444_to_rgb() */
109 
sycc422_to_rgb(opj_image_t * img)110 static void sycc422_to_rgb(opj_image_t *img)
111 {
112 	int *d0, *d1, *d2, *r, *g, *b;
113 	const int *y, *cb, *cr;
114 	int maxw, maxh, max, offset, upb;
115 	int i, j;
116 
117 	i = img->comps[0].prec;
118 	offset = 1<<(i - 1); upb = (1<<i)-1;
119 
120 	maxw = img->comps[0].w; maxh = img->comps[0].h;
121 	max = maxw * maxh;
122 
123 	y = img->comps[0].data;
124 	cb = img->comps[1].data;
125 	cr = img->comps[2].data;
126 
127 	d0 = r = (int*)malloc(sizeof(int) * max);
128 	d1 = g = (int*)malloc(sizeof(int) * max);
129 	d2 = b = (int*)malloc(sizeof(int) * max);
130 
131 	for(i=0; i < maxh; ++i)
132    {
133 	for(j=0; j < maxw; j += 2)
134   {
135 	sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
136 
137 	++y; ++r; ++g; ++b;
138 
139 	sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
140 
141 	++y; ++r; ++g; ++b; ++cb; ++cr;
142   }
143    }
144 	free(img->comps[0].data); img->comps[0].data = d0;
145 	free(img->comps[1].data); img->comps[1].data = d1;
146 	free(img->comps[2].data); img->comps[2].data = d2;
147 
148 	img->comps[1].w = maxw; img->comps[1].h = maxh;
149 	img->comps[2].w = maxw; img->comps[2].h = maxh;
150 	img->comps[1].dx = img->comps[0].dx;
151 	img->comps[2].dx = img->comps[0].dx;
152 	img->comps[1].dy = img->comps[0].dy;
153 	img->comps[2].dy = img->comps[0].dy;
154 
155 }/* sycc422_to_rgb() */
156 
sycc420_to_rgb(opj_image_t * img)157 static void sycc420_to_rgb(opj_image_t *img)
158 {
159 	int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
160 	const int *y, *cb, *cr, *ny;
161 	int maxw, maxh, max, offset, upb;
162 	int i, j;
163 
164 	i = img->comps[0].prec;
165 	offset = 1<<(i - 1); upb = (1<<i)-1;
166 
167 	maxw = img->comps[0].w; maxh = img->comps[0].h;
168 	max = maxw * maxh;
169 
170 	y = img->comps[0].data;
171 	cb = img->comps[1].data;
172 	cr = img->comps[2].data;
173 
174 	d0 = r = (int*)malloc(sizeof(int) * max);
175 	d1 = g = (int*)malloc(sizeof(int) * max);
176 	d2 = b = (int*)malloc(sizeof(int) * max);
177 
178 	for(i=0; i < maxh; i += 2)
179    {
180 	ny = y + maxw;
181 	nr = r + maxw; ng = g + maxw; nb = b + maxw;
182 
183 	for(j=0; j < maxw;  j += 2)
184   {
185 	sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
186 
187 	++y; ++r; ++g; ++b;
188 
189 	sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
190 
191 	++y; ++r; ++g; ++b;
192 
193 	sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
194 
195 	++ny; ++nr; ++ng; ++nb;
196 
197 	sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
198 
199 	++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
200   }
201 	y += maxw; r += maxw; g += maxw; b += maxw;
202    }
203 	free(img->comps[0].data); img->comps[0].data = d0;
204 	free(img->comps[1].data); img->comps[1].data = d1;
205 	free(img->comps[2].data); img->comps[2].data = d2;
206 
207 	img->comps[1].w = maxw; img->comps[1].h = maxh;
208 	img->comps[2].w = maxw; img->comps[2].h = maxh;
209 	img->comps[1].dx = img->comps[0].dx;
210 	img->comps[2].dx = img->comps[0].dx;
211 	img->comps[1].dy = img->comps[0].dy;
212 	img->comps[2].dy = img->comps[0].dy;
213 
214 }/* sycc420_to_rgb() */
215 
color_sycc_to_rgb(opj_image_t * img)216 void color_sycc_to_rgb(opj_image_t *img)
217 {
218 	if(img->numcomps < 3)
219    {
220 	img->color_space = CLRSPC_GRAY;
221 	return;
222    }
223 
224 	if((img->comps[0].dx == 1)
225 	&& (img->comps[1].dx == 2)
226 	&& (img->comps[2].dx == 2)
227 	&& (img->comps[0].dy == 1)
228 	&& (img->comps[1].dy == 2)
229 	&& (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */
230   {
231 	sycc420_to_rgb(img);
232   }
233 	else
234 	if((img->comps[0].dx == 1)
235 	&& (img->comps[1].dx == 2)
236 	&& (img->comps[2].dx == 2)
237 	&& (img->comps[0].dy == 1)
238 	&& (img->comps[1].dy == 1)
239 	&& (img->comps[2].dy == 1))/* horizontal sub-sample only */
240   {
241 	sycc422_to_rgb(img);
242   }
243 	else
244 	if((img->comps[0].dx == 1)
245 	&& (img->comps[1].dx == 1)
246 	&& (img->comps[2].dx == 1)
247 	&& (img->comps[0].dy == 1)
248 	&& (img->comps[1].dy == 1)
249 	&& (img->comps[2].dy == 1))/* no sub-sample */
250   {
251 	sycc444_to_rgb(img);
252   }
253 	else
254   {
255 	fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n",
256 	 __FILE__,__LINE__);
257 	return;
258   }
259 	img->color_space = CLRSPC_SRGB;
260 
261 }/* color_sycc_to_rgb() */
262 
263 #if defined(HAVE_LIBLCMS2) || defined(HAVE_LIBLCMS1)
264 #ifdef HAVE_LIBLCMS1
265 /* Bob Friesenhahn proposed:*/
266 #define cmsSigXYZData   icSigXYZData
267 #define cmsSigLabData   icSigLabData
268 #define cmsSigCmykData  icSigCmykData
269 #define cmsSigYCbCrData icSigYCbCrData
270 #define cmsSigLuvData   icSigLuvData
271 #define cmsSigGrayData  icSigGrayData
272 #define cmsSigRgbData   icSigRgbData
273 #define cmsUInt32Number DWORD
274 
275 #define cmsColorSpaceSignature icColorSpaceSignature
276 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
277 
278 #endif /* HAVE_LIBLCMS1 */
279 
color_apply_icc_profile(opj_image_t * image)280 void color_apply_icc_profile(opj_image_t *image)
281 {
282 	cmsHPROFILE in_prof, out_prof;
283 	cmsHTRANSFORM transform;
284 	cmsColorSpaceSignature in_space, out_space;
285 	cmsUInt32Number intent, in_type, out_type, nr_samples;
286 	int *r, *g, *b;
287 	int prec, i, max, max_w, max_h;
288 	OPJ_COLOR_SPACE oldspace;
289 
290 	in_prof =
291 	 cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
292 
293 	if(in_prof == NULL) return;
294 
295 	in_space = cmsGetPCS(in_prof);
296   (void)in_space;
297 	out_space = cmsGetColorSpace(in_prof);
298 	intent = cmsGetHeaderRenderingIntent(in_prof);
299 
300 
301 	max_w = image->comps[0].w; max_h = image->comps[0].h;
302 	prec = image->comps[0].prec;
303   (void)prec;
304 	oldspace = image->color_space;
305 
306 	if(out_space == cmsSigRgbData) /* enumCS 16 */
307    {
308 	in_type = TYPE_RGB_16;
309 	out_type = TYPE_RGB_16;
310 	out_prof = cmsCreate_sRGBProfile();
311 	image->color_space = CLRSPC_SRGB;
312    }
313 	else
314 	if(out_space == cmsSigGrayData) /* enumCS 17 */
315    {
316 	in_type = TYPE_GRAY_8;
317 	out_type = TYPE_RGB_8;
318 	out_prof = cmsCreate_sRGBProfile();
319 	image->color_space = CLRSPC_SRGB;
320    }
321 	else
322 	if(out_space == cmsSigYCbCrData) /* enumCS 18 */
323    {
324 	in_type = TYPE_YCbCr_16;
325 	out_type = TYPE_RGB_16;
326 	out_prof = cmsCreate_sRGBProfile();
327 	image->color_space = CLRSPC_SRGB;
328    }
329 	else
330    {
331 #ifdef DEBUG_PROFILE
332 fprintf(stderr,"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
333 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
334 __FILE__,__LINE__,out_space,
335 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
336 (out_space>>8) & 0xff, out_space & 0xff);
337 #endif
338 	return;
339    }
340 
341 #ifdef DEBUG_PROFILE
342 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
343 "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
344 max_w,max_h, (void*)in_prof,(void*)out_prof);
345 
346 fprintf(stderr,"\trender_intent (%u)\n\t"
347 "color_space: in(%#x)(%c%c%c%c)   out:(%#x)(%c%c%c%c)\n\t"
348 "       type: in(%u)              out:(%u)\n",
349 intent,
350 in_space,
351 (in_space>>24) & 0xff,(in_space>>16) & 0xff,
352 (in_space>>8) & 0xff, in_space & 0xff,
353 
354 out_space,
355 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
356 (out_space>>8) & 0xff, out_space & 0xff,
357 
358 in_type,out_type
359  );
360 #endif /* DEBUG_PROFILE */
361 
362 	transform = cmsCreateTransform(in_prof, in_type,
363 	 out_prof, out_type, intent, 0);
364 
365 #ifdef HAVE_LIBLCMS2
366 /* Possible for: LCMS_VERSION >= 2000 :*/
367 	cmsCloseProfile(in_prof);
368 	cmsCloseProfile(out_prof);
369 #endif
370 
371 	if(transform == NULL)
372    {
373 #ifdef DEBUG_PROFILE
374 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
375 "ICC Profile ignored.\n",__FILE__,__LINE__);
376 #endif
377 	image->color_space = oldspace;
378 #ifdef HAVE_LIBLCMS1
379 	cmsCloseProfile(in_prof);
380 	cmsCloseProfile(out_prof);
381 #endif
382 	return;
383    }
384 
385 	if(image->numcomps > 2)/* RGB, RGBA */
386    {
387 	unsigned short *inbuf, *outbuf, *in, *out;
388 	max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned short);
389 	in = inbuf = (unsigned short*)malloc(nr_samples);
390 	out = outbuf = (unsigned short*)malloc(nr_samples);
391 
392 	r = image->comps[0].data;
393 	g = image->comps[1].data;
394 	b = image->comps[2].data;
395 
396 	for(i = 0; i < max; ++i)
397   {
398 	*in++ = (unsigned short)*r++;
399 	*in++ = (unsigned short)*g++;
400 	*in++ = (unsigned short)*b++;
401   }
402 
403 	cmsDoTransform(transform, inbuf, outbuf, max);
404 
405 	r = image->comps[0].data;
406 	g = image->comps[1].data;
407 	b = image->comps[2].data;
408 
409 	for(i = 0; i < max; ++i)
410   {
411 	*r++ = (int)*out++;
412 	*g++ = (int)*out++;
413 	*b++ = (int)*out++;
414   }
415 	free(inbuf); free(outbuf);
416    }
417 	else /* GRAY, GRAYA */
418    {
419 	unsigned char *in, *inbuf, *out, *outbuf;
420 
421 	max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned char);
422 	in = inbuf = (unsigned char*)malloc(nr_samples);
423 	out = outbuf = (unsigned char*)malloc(nr_samples);
424 
425 	image->comps = (opj_image_comp_t*)
426 	 realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
427 
428 	if(image->numcomps == 2)
429 	 image->comps[3] = image->comps[1];
430 
431 	image->comps[1] = image->comps[0];
432 	image->comps[2] = image->comps[0];
433 
434 	image->comps[1].data = (int*)calloc(max, sizeof(int));
435 	image->comps[2].data = (int*)calloc(max, sizeof(int));
436 
437 	image->numcomps += 2;
438 
439 	r = image->comps[0].data;
440 
441 	for(i = 0; i < max; ++i)
442   {
443 	*in++ = (unsigned char)*r++;
444   }
445 	cmsDoTransform(transform, inbuf, outbuf, max);
446 
447 	r = image->comps[0].data;
448 	g = image->comps[1].data;
449 	b = image->comps[2].data;
450 
451 	for(i = 0; i < max; ++i)
452   {
453 	*r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
454   }
455 	free(inbuf); free(outbuf);
456 
457    }/* if(image->numcomps */
458 
459 	cmsDeleteTransform(transform);
460 
461 #ifdef HAVE_LIBLCMS1
462 	cmsCloseProfile(in_prof);
463 	cmsCloseProfile(out_prof);
464 #endif
465 }/* color_apply_icc_profile() */
466 
467 #endif /* HAVE_LIBLCMS2 || HAVE_LIBLCMS1 */
468 
469