a6fef14535950e28eabd048ff4f4ab6b645de2df
[openjpeg.git] / src / bin / common / color.c
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 #ifdef CLRSPC_GRAY
49 #define OPJ_CLRSPC_GRAY CLRSPC_GRAY
50 #endif
51
52 #ifdef CLRSPC_SRGB
53 #define OPJ_CLRSPC_SRGB CLRSPC_SRGB
54 #endif
55
56 /*--------------------------------------------------------
57 Matrix for sYCC, Amendment 1 to IEC 61966-2-1
58
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
62
63 Inverse:
64
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)
68
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)
72 {
73         int r, g, b;
74
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;
78
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;
81
82         b = y + (int)(1.772 * (float)cb);
83         if(b < 0) b = 0; else if(b > upb) b = upb; *out_b = b;
84 }
85
86 static void sycc444_to_rgb(opj_image_t *img)
87 {
88         int *d0, *d1, *d2, *r, *g, *b;
89         const int *y, *cb, *cr;
90         int maxw, maxh, max, i, offset, upb;
91
92         i = img->comps[0].prec;
93         offset = 1<<(i - 1); upb = (1<<i)-1;
94
95         maxw = img->comps[0].w; maxh = img->comps[0].h;
96         max = maxw * maxh;
97
98         y = img->comps[0].data;
99         cb = img->comps[1].data;
100         cr = img->comps[2].data;
101
102         d0 = r = (int*)malloc(sizeof(int) * max);
103         d1 = g = (int*)malloc(sizeof(int) * max);
104         d2 = b = (int*)malloc(sizeof(int) * max);
105
106         for(i = 0; i < max; ++i)
107    {
108         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);        
109
110         ++y; ++cb; ++cr; ++r; ++g; ++b;
111    }    
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;
115
116 }/* sycc444_to_rgb() */
117
118 static void sycc422_to_rgb(opj_image_t *img)
119 {       
120         int *d0, *d1, *d2, *r, *g, *b;
121         const int *y, *cb, *cr;
122         int maxw, maxh, max, offset, upb;
123         int i, j;
124
125         i = img->comps[0].prec;
126         offset = 1<<(i - 1); upb = (1<<i)-1;
127
128         maxw = img->comps[0].w; maxh = img->comps[0].h;
129         max = maxw * maxh;
130
131         y = img->comps[0].data;
132         cb = img->comps[1].data;
133         cr = img->comps[2].data;
134
135         d0 = r = (int*)malloc(sizeof(int) * max);
136         d1 = g = (int*)malloc(sizeof(int) * max);
137         d2 = b = (int*)malloc(sizeof(int) * max);
138
139         for(i=0; i < maxh; ++i)
140    {
141         for(j=0; j < maxw; j += 2)
142   {
143         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
144
145         ++y; ++r; ++g; ++b;
146
147         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
148
149         ++y; ++r; ++g; ++b; ++cb; ++cr;
150   }
151    }
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;
155
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;
162
163 }/* sycc422_to_rgb() */
164
165 static void sycc420_to_rgb(opj_image_t *img)
166 {
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;
170         int i, j;
171
172         i = img->comps[0].prec;
173         offset = 1<<(i - 1); upb = (1<<i)-1;
174
175         maxw = img->comps[0].w; maxh = img->comps[0].h;
176         max = maxw * maxh;
177
178         y = img->comps[0].data;
179         cb = img->comps[1].data;
180         cr = img->comps[2].data;
181
182         d0 = r = (int*)malloc(sizeof(int) * max);
183         d1 = g = (int*)malloc(sizeof(int) * max);
184         d2 = b = (int*)malloc(sizeof(int) * max);
185
186         for(i=0; i < maxh; i += 2)
187    {
188         ny = y + maxw;
189         nr = r + maxw; ng = g + maxw; nb = b + maxw;
190
191         for(j=0; j < maxw;  j += 2)
192   {
193         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
194
195         ++y; ++r; ++g; ++b;
196
197         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
198
199         ++y; ++r; ++g; ++b;
200
201         sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
202
203         ++ny; ++nr; ++ng; ++nb;
204
205         sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
206
207         ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
208   }
209         y += maxw; r += maxw; g += maxw; b += maxw;
210    }
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;
214
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;
221
222 }/* sycc420_to_rgb() */
223
224 void color_sycc_to_rgb(opj_image_t *img)
225 {
226         if(img->numcomps < 3) 
227    {
228         img->color_space = OPJ_CLRSPC_GRAY;
229         return;
230    }
231
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 */
238   {
239         sycc420_to_rgb(img);
240   }
241         else
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 */
248   {
249         sycc422_to_rgb(img);
250   }
251         else
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 */
258   {
259         sycc444_to_rgb(img);
260   }
261         else
262   {
263         fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n",
264          __FILE__,__LINE__);
265         return;
266   }
267         img->color_space = OPJ_CLRSPC_SRGB;
268
269 }/* color_sycc_to_rgb() */
270
271 #if defined(HAVE_LIBLCMS2) || defined(HAVE_LIBLCMS1)
272 #ifdef 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
282
283 #define cmsColorSpaceSignature icColorSpaceSignature
284 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
285
286 #endif /* HAVE_LIBLCMS1 */
287
288 void color_apply_icc_profile(opj_image_t *image)
289 {
290         cmsHPROFILE in_prof, out_prof;
291         cmsHTRANSFORM transform;
292         cmsColorSpaceSignature in_space, out_space;
293         cmsUInt32Number intent, in_type, out_type, nr_samples;
294         int *r, *g, *b;
295         int prec, i, max, max_w, max_h;
296         OPJ_COLOR_SPACE oldspace;
297
298         in_prof = 
299          cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
300
301         if(in_prof == NULL) return;
302
303         in_space = cmsGetPCS(in_prof);
304         out_space = cmsGetColorSpace(in_prof);
305         intent = cmsGetHeaderRenderingIntent(in_prof);
306
307         
308         max_w = image->comps[0].w; max_h = image->comps[0].h;
309         prec = image->comps[0].prec;
310         oldspace = image->color_space;
311
312         if(out_space == cmsSigRgbData) /* enumCS 16 */
313    {
314         in_type = TYPE_RGB_16;
315         out_type = TYPE_RGB_16;
316         out_prof = cmsCreate_sRGBProfile();
317         image->color_space = OPJ_CLRSPC_SRGB;
318    }
319         else
320         if(out_space == cmsSigGrayData) /* enumCS 17 */
321    {
322         in_type = TYPE_GRAY_8;
323         out_type = TYPE_RGB_8;
324         out_prof = cmsCreate_sRGBProfile();
325         image->color_space = OPJ_CLRSPC_SRGB;
326    }
327         else
328         if(out_space == cmsSigYCbCrData) /* enumCS 18 */
329    {
330         in_type = TYPE_YCbCr_16;
331         out_type = TYPE_RGB_16;
332         out_prof = cmsCreate_sRGBProfile();
333         image->color_space = OPJ_CLRSPC_SRGB;
334    }
335         else
336    {
337 #ifdef DEBUG_PROFILE
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);
343 #endif
344         return;
345    }
346
347 #ifdef DEBUG_PROFILE
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);
351
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",
355 intent,
356 in_space,
357 (in_space>>24) & 0xff,(in_space>>16) & 0xff,
358 (in_space>>8) & 0xff, in_space & 0xff,
359
360 out_space,
361 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
362 (out_space>>8) & 0xff, out_space & 0xff,
363
364 in_type,out_type
365  );
366 #endif /* DEBUG_PROFILE */
367
368         transform = cmsCreateTransform(in_prof, in_type,
369          out_prof, out_type, intent, 0);
370
371 #ifdef HAVE_LIBLCMS2
372 /* Possible for: LCMS_VERSION >= 2000 :*/
373         cmsCloseProfile(in_prof);
374         cmsCloseProfile(out_prof);
375 #endif
376
377         if(transform == NULL)
378    {
379 #ifdef DEBUG_PROFILE
380 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
381 "ICC Profile ignored.\n",__FILE__,__LINE__);
382 #endif
383         image->color_space = oldspace;
384 #ifdef HAVE_LIBLCMS1
385         cmsCloseProfile(in_prof);
386         cmsCloseProfile(out_prof);
387 #endif
388         return;
389    }
390
391         if(image->numcomps > 2)/* RGB, RGBA */
392    {
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);
397
398         r = image->comps[0].data;
399         g = image->comps[1].data;
400         b = image->comps[2].data;
401
402         for(i = 0; i < max; ++i)
403   {
404         *in++ = (unsigned short)*r++;
405         *in++ = (unsigned short)*g++;
406         *in++ = (unsigned short)*b++;
407   }
408
409         cmsDoTransform(transform, inbuf, outbuf, max);
410
411         r = image->comps[0].data;
412         g = image->comps[1].data;
413         b = image->comps[2].data;
414
415         for(i = 0; i < max; ++i)
416   {
417         *r++ = (int)*out++;
418         *g++ = (int)*out++;
419         *b++ = (int)*out++;
420   }
421         free(inbuf); free(outbuf);
422    }
423         else /* GRAY, GRAYA */
424    {
425         unsigned char *in, *inbuf, *out, *outbuf;
426
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);
430
431         image->comps = (opj_image_comp_t*)
432          realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
433
434         if(image->numcomps == 2)
435          image->comps[3] = image->comps[1];
436
437         image->comps[1] = image->comps[0];
438         image->comps[2] = image->comps[0];
439
440         image->comps[1].data = (int*)calloc(max, sizeof(int));
441         image->comps[2].data = (int*)calloc(max, sizeof(int));
442
443         image->numcomps += 2;
444
445         r = image->comps[0].data;
446
447         for(i = 0; i < max; ++i)
448   {
449         *in++ = (unsigned char)*r++;
450   }
451         cmsDoTransform(transform, inbuf, outbuf, max);
452
453         r = image->comps[0].data;
454         g = image->comps[1].data;
455         b = image->comps[2].data;
456
457         for(i = 0; i < max; ++i)
458   {
459         *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
460   }
461         free(inbuf); free(outbuf);
462
463    }/* if(image->numcomps */
464
465         cmsDeleteTransform(transform);
466
467 #ifdef HAVE_LIBLCMS1
468         cmsCloseProfile(in_prof);
469         cmsCloseProfile(out_prof);
470 #endif
471 }/* color_apply_icc_profile() */
472
473 #endif /* HAVE_LIBLCMS2 || HAVE_LIBLCMS1 */
474