Fix formatting
[openjpeg.git] / src / bin / common / color.c
1 /*
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.
6  *
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.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
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.
24  *
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.
36  */
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <math.h>
42 #include <assert.h>
43
44 #include "opj_apps_config.h"
45 #include "openjpeg.h"
46 #include "color.h"
47
48 #ifdef OPJ_HAVE_LIBLCMS2
49 #include <lcms2.h>
50 #endif
51 #ifdef OPJ_HAVE_LIBLCMS1
52 #include <lcms.h>
53 #endif
54
55 #ifdef OPJ_USE_LEGACY
56 #define OPJ_CLRSPC_GRAY CLRSPC_GRAY
57 #define OPJ_CLRSPC_SRGB CLRSPC_SRGB
58 #endif
59
60 /*--------------------------------------------------------
61 Matrix for sYCC, Amendment 1 to IEC 61966-2-1
62
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
66
67 Inverse:
68
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)
72
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)
76 {
77         int r, g, b;
78
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;
82
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;
85
86         b = y + (int)(1.772 * (float)cb);
87         if(b < 0) b = 0; else if(b > upb) b = upb; *out_b = b;
88 }
89
90 static void sycc444_to_rgb(opj_image_t *img)
91 {
92         int *d0, *d1, *d2, *r, *g, *b;
93         const int *y, *cb, *cr;
94         unsigned int maxw, maxh, max, i;
95         int offset, upb;
96
97         upb = (int)img->comps[0].prec;
98         offset = 1<<(upb - 1); upb = (1<<upb)-1;
99
100         maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
101         max = maxw * maxh;
102
103         y = img->comps[0].data;
104         cb = img->comps[1].data;
105         cr = img->comps[2].data;
106
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);
110
111         for(i = 0U; i < max; ++i)
112         {
113                 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
114                 ++y; ++cb; ++cr; ++r; ++g; ++b;
115         }
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;
119
120 }/* sycc444_to_rgb() */
121
122 static void sycc422_to_rgb(opj_image_t *img)
123 {       
124         int *d0, *d1, *d2, *r, *g, *b;
125         const int *y, *cb, *cr;
126         unsigned int maxw, maxh, max;
127         int offset, upb;
128         unsigned int i, j;
129
130         upb = (int)img->comps[0].prec;
131         offset = 1<<(upb - 1); upb = (1<<upb)-1;
132
133         maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
134         max = maxw * maxh;
135
136         y = img->comps[0].data;
137         cb = img->comps[1].data;
138         cr = img->comps[2].data;
139
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);
143
144         for(i=0U; i < maxh; ++i)
145         {
146                 for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
147                 {
148                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
149                         ++y; ++r; ++g; ++b;
150                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
151                         ++y; ++r; ++g; ++b; ++cb; ++cr;
152                 }
153                 if (j < maxw) {
154                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
155                         ++y; ++r; ++g; ++b; ++cb; ++cr;
156                 }
157         }
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;
161
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;
165 #else
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;
168 #endif
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;
173
174 }/* sycc422_to_rgb() */
175
176 static void sycc420_to_rgb(opj_image_t *img)
177 {
178         int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
179         const int *y, *cb, *cr, *ny;
180         unsigned int maxw, maxh, max;
181         int offset, upb;
182         unsigned int i, j;
183
184         upb = (int)img->comps[0].prec;
185         offset = 1<<(upb - 1); upb = (1<<upb)-1;
186
187         maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
188         max = maxw * maxh;
189
190         y = img->comps[0].data;
191         cb = img->comps[1].data;
192         cr = img->comps[2].data;
193
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);
197
198         for(i=0U; i < (maxh & ~(unsigned int)1U); i += 2U)
199         {
200                 ny = y + maxw;
201                 nr = r + maxw; ng = g + maxw; nb = b + maxw;
202
203                 for(j=0; j < (maxw & ~(unsigned int)1U); j += 2U)
204                 {
205                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
206                         ++y; ++r; ++g; ++b;
207                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
208                         ++y; ++r; ++g; ++b;
209
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;
214                 }
215                 if(j < maxw)
216                 {
217                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
218                         ++y; ++r; ++g; ++b;
219
220                         sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
221                         ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
222                 }
223                 y += maxw; r += maxw; g += maxw; b += maxw;
224         }
225         if(i < maxh)
226         {
227                 for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
228                 {
229                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
230
231                         ++y; ++r; ++g; ++b;
232
233                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
234
235                         ++y; ++r; ++g; ++b; ++cb; ++cr;
236                 }
237                 if(j < maxw)
238                 {
239                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
240                 }
241         }
242
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;
246
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;
250 #else
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;
253 #endif
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;
258
259 }/* sycc420_to_rgb() */
260
261 void color_sycc_to_rgb(opj_image_t *img)
262 {
263         if(img->numcomps < 3)
264         {
265                 img->color_space = OPJ_CLRSPC_GRAY;
266                 return;
267         }
268
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 */
275   {
276                 sycc420_to_rgb(img);
277   }
278         else
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 */
285   {
286                 sycc422_to_rgb(img);
287   }
288         else
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 */
295   {
296                 sycc444_to_rgb(img);
297   }
298         else
299   {
300                 fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,__LINE__);
301                 return;
302   }
303         img->color_space = OPJ_CLRSPC_SRGB;
304
305 }/* color_sycc_to_rgb() */
306
307 #if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
308
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
319
320 #define cmsColorSpaceSignature icColorSpaceSignature
321 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
322
323 #endif /* OPJ_HAVE_LIBLCMS1 */
324
325 /*#define DEBUG_PROFILE*/
326 void color_apply_icc_profile(opj_image_t *image)
327 {
328         cmsHPROFILE in_prof, out_prof;
329         cmsHTRANSFORM transform;
330         cmsColorSpaceSignature in_space, out_space;
331         cmsUInt32Number intent, in_type, out_type, nr_samples;
332         int *r, *g, *b;
333         int prec, i, max, max_w, max_h;
334         OPJ_COLOR_SPACE oldspace;
335
336         in_prof = 
337          cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
338 #ifdef DEBUG_PROFILE
339   FILE *icm = fopen("debug.icm","wb");
340   fwrite( image->icc_profile_buf,1, image->icc_profile_len,icm);
341   fclose(icm);
342 #endif
343
344         if(in_prof == NULL) return;
345
346         in_space = cmsGetPCS(in_prof);
347         out_space = cmsGetColorSpace(in_prof);
348         intent = cmsGetHeaderRenderingIntent(in_prof);
349
350         
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;
355
356         if(out_space == cmsSigRgbData) /* enumCS 16 */
357    {
358         if( prec <= 8 )
359   {
360         in_type = TYPE_RGB_8;
361         out_type = TYPE_RGB_8;
362   }
363         else
364   {
365         in_type = TYPE_RGB_16;
366         out_type = TYPE_RGB_16;
367   }
368         out_prof = cmsCreate_sRGBProfile();
369         image->color_space = OPJ_CLRSPC_SRGB;
370    }
371         else
372         if(out_space == cmsSigGrayData) /* enumCS 17 */
373    {
374         in_type = TYPE_GRAY_8;
375         out_type = TYPE_RGB_8;
376         out_prof = cmsCreate_sRGBProfile();
377         image->color_space = OPJ_CLRSPC_SRGB;
378    }
379         else
380         if(out_space == cmsSigYCbCrData) /* enumCS 18 */
381    {
382         in_type = TYPE_YCbCr_16;
383         out_type = TYPE_RGB_16;
384         out_prof = cmsCreate_sRGBProfile();
385         image->color_space = OPJ_CLRSPC_SRGB;
386    }
387         else
388    {
389 #ifdef DEBUG_PROFILE
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);
395 #endif
396         return;
397    }
398
399 #ifdef DEBUG_PROFILE
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);
403
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",
407         intent,
408         in_space,
409         (in_space>>24) & 0xff,(in_space>>16) & 0xff,
410         (in_space>>8) & 0xff, in_space & 0xff,
411
412         out_space,
413         (out_space>>24) & 0xff,(out_space>>16) & 0xff,
414         (out_space>>8) & 0xff, out_space & 0xff,
415
416         in_type,out_type
417  );
418 #else
419   (void)prec;
420   (void)in_space;
421 #endif /* DEBUG_PROFILE */
422
423         transform = cmsCreateTransform(in_prof, in_type,
424          out_prof, out_type, intent, 0);
425
426 #ifdef OPJ_HAVE_LIBLCMS2
427 /* Possible for: LCMS_VERSION >= 2000 :*/
428         cmsCloseProfile(in_prof);
429         cmsCloseProfile(out_prof);
430 #endif
431
432         if(transform == NULL)
433    {
434 #ifdef DEBUG_PROFILE
435 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
436 "ICC Profile ignored.\n",__FILE__,__LINE__);
437 #endif
438         image->color_space = oldspace;
439 #ifdef OPJ_HAVE_LIBLCMS1
440         cmsCloseProfile(in_prof);
441         cmsCloseProfile(out_prof);
442 #endif
443         return;
444    }
445
446         if(image->numcomps > 2)/* RGB, RGBA */
447    {
448         if( prec <= 8 )
449   {
450         unsigned char *inbuf, *outbuf, *in, *out;
451         max = max_w * max_h;
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);
455
456         r = image->comps[0].data;
457         g = image->comps[1].data;
458         b = image->comps[2].data;
459
460         for(i = 0; i < max; ++i)
461  {
462         *in++ = (unsigned char)*r++;
463         *in++ = (unsigned char)*g++;
464         *in++ = (unsigned char)*b++;
465  }
466
467         cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
468
469         r = image->comps[0].data;
470         g = image->comps[1].data;
471         b = image->comps[2].data;
472
473         for(i = 0; i < max; ++i)
474  {
475         *r++ = (int)*out++;
476         *g++ = (int)*out++;
477         *b++ = (int)*out++;
478  }
479         free(inbuf); free(outbuf);
480   }
481         else
482   {
483         unsigned short *inbuf, *outbuf, *in, *out;
484         max = max_w * max_h;
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);
488
489         r = image->comps[0].data;
490         g = image->comps[1].data;
491         b = image->comps[2].data;
492
493         for(i = 0; i < max; ++i)
494  {
495         *in++ = (unsigned short)*r++;
496         *in++ = (unsigned short)*g++;
497         *in++ = (unsigned short)*b++;
498  }
499
500         cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
501
502         r = image->comps[0].data;
503         g = image->comps[1].data;
504         b = image->comps[2].data;
505
506         for(i = 0; i < max; ++i)
507  {
508         *r++ = (int)*out++;
509         *g++ = (int)*out++;
510         *b++ = (int)*out++;
511  }
512         free(inbuf); free(outbuf);
513   }
514    }
515         else /* GRAY, GRAYA */
516    {
517         unsigned char *in, *inbuf, *out, *outbuf;
518         max = max_w * max_h;
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);
522
523         image->comps = (opj_image_comp_t*)
524          realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
525
526         if(image->numcomps == 2)
527          image->comps[3] = image->comps[1];
528
529         image->comps[1] = image->comps[0];
530         image->comps[2] = image->comps[0];
531
532         image->comps[1].data = (int*)calloc((size_t)max, sizeof(int));
533         image->comps[2].data = (int*)calloc((size_t)max, sizeof(int));
534
535         image->numcomps += 2;
536
537         r = image->comps[0].data;
538
539         for(i = 0; i < max; ++i)
540   {
541         *in++ = (unsigned char)*r++;
542   }
543         cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
544
545         r = image->comps[0].data;
546         g = image->comps[1].data;
547         b = image->comps[2].data;
548
549         for(i = 0; i < max; ++i)
550   {
551         *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
552   }
553         free(inbuf); free(outbuf);
554
555    }/* if(image->numcomps */
556
557         cmsDeleteTransform(transform);
558
559 #ifdef OPJ_HAVE_LIBLCMS1
560         cmsCloseProfile(in_prof);
561         cmsCloseProfile(out_prof);
562 #endif
563 }/* color_apply_icc_profile() */
564
565 void color_apply_conversion(opj_image_t *image)
566 {
567         int *row;
568         int enumcs, numcomps;
569         
570         image->color_space = OPJ_CLRSPC_SRGB;
571         
572         numcomps = image->numcomps;
573         
574         if(numcomps != 3)
575         {
576                 fprintf(stderr,"%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
577                                                 __FILE__,__LINE__,numcomps);
578                 return;
579         }
580         
581         row = (int*)image->icc_profile_buf;
582         enumcs = row[0];
583         
584         if(enumcs == 14)// CIELab
585         {
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;
591                 unsigned int i, max;
592                 cmsHPROFILE in, out;
593                 cmsHTRANSFORM transform;
594                 cmsUInt16Number RGB[3];
595                 cmsCIELab Lab;
596                 
597                 in = cmsCreateLab4Profile(NULL);
598                 out = cmsCreate_sRGBProfile();
599                 
600                 transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16, INTENT_PERCEPTUAL, 0);
601                 
602 #ifdef HAVE_LIBLCMS2
603                 cmsCloseProfile(in);
604                 cmsCloseProfile(out);
605 #endif
606                 if(transform == NULL)
607                 {
608 #ifdef HAVE_LIBLCMS1
609                         cmsCloseProfile(in);
610                         cmsCloseProfile(out);
611 #endif
612                         return;
613                 }
614                 prec0 = (double)image->comps[0].prec;
615                 prec1 = (double)image->comps[1].prec;
616                 prec2 = (double)image->comps[2].prec;
617                 
618                 default_type = row[1];
619                 
620                 if(default_type == 0x44454600)// DEF : default
621                 {
622                         rl = 100; ra = 170; rb = 200;
623                         ol = 0;
624                         oa = pow(2, prec1 - 1);
625                         ob = pow(2, prec2 - 2) +  pow(2, prec2 - 3);
626                 }
627                 else
628                 {
629                         rl = row[2]; ra = row[4]; rb = row[6];
630                         ol = row[3]; oa = row[5]; ob = row[7];
631                 }
632                 
633                 L = src0 = image->comps[0].data;
634                 a = src1 = image->comps[1].data;
635                 b = src2 = image->comps[2].data;
636                 
637                 max = image->comps[0].w * image->comps[0].h;
638                 
639                 red = dst0 = (int*)malloc(max * sizeof(int));
640                 green = dst1 = (int*)malloc(max * sizeof(int));
641                 blue = dst2 = (int*)malloc(max * sizeof(int));
642                 
643                 minL = -(rl * ol)/(pow(2, prec0)-1);
644                 maxL = minL + rl;
645                 
646                 mina = -(ra * oa)/(pow(2, prec1)-1);
647                 maxa = mina + ra;
648                 
649                 minb = -(rb * ob)/(pow(2, prec2)-1);
650                 maxb = minb + rb;
651                 
652                 for(i = 0; i < max; ++i)
653                 {
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;
657                 
658                         cmsDoTransform(transform, &Lab, RGB, 1);
659                 
660                         *red++ = RGB[0];
661                         *green++ = RGB[1];
662                         *blue++ = RGB[2];
663                 }
664                 cmsDeleteTransform(transform);
665 #ifdef HAVE_LIBLCMS1
666                 cmsCloseProfile(in);
667                 cmsCloseProfile(out);
668 #endif
669                 free(src0); image->comps[0].data = dst0;
670                 free(src1); image->comps[1].data = dst1;
671                 free(src2); image->comps[2].data = dst2;
672                 
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;
677                 
678                 return;
679         }
680         
681         fprintf(stderr,"%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,__LINE__, enumcs);
682 }// color_apply_conversion()
683
684 #endif // HAVE_LIBLCMS2 || HAVE_LIBLCMS1
685
686 void color_cmyk_to_rgb(opj_image_t *image)
687 {
688         float C, M, Y, K;
689         float sC, sM, sY, sK;
690         unsigned int w, h, max, i;
691
692         w = image->comps[0].w;
693         h = image->comps[0].h;
694
695         if(image->numcomps < 4) return;
696
697         max = w * h;
698         
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);
703
704         for(i = 0; i < max; ++i)
705         {
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;
711                 
712                 /* Invert all CMYK values */
713                 C = 1.0F - C;
714                 M = 1.0F - M;
715                 Y = 1.0F - Y;
716                 K = 1.0F - K;
717
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 */
722         }
723
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;
730         
731         for (i = 3; i < image->numcomps; ++i) {
732                 memcpy(&(image->comps[i]), &(image->comps[i+1]), sizeof(image->comps[i]));
733         }
734
735 }// color_cmyk_to_rgb()
736
737 //
738 // This code has been adopted from sjpx_openjpeg.c of ghostscript
739 //
740 void color_esycc_to_rgb(opj_image_t *image)
741 {
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));
746         
747         if(image->numcomps < 3) return;
748         
749         w = image->comps[0].w;
750         h = image->comps[0].h;
751         
752         sign1 = image->comps[1].sgnd;
753         sign2 = image->comps[2].sgnd;
754         
755         max = w * h;
756         
757         for(i = 0; i < max; ++i)
758         {
759                 
760                 y = image->comps[0].data[i]; cb = image->comps[1].data[i]; cr = image->comps[2].data[i];
761                 
762                 if( !sign1) cb -= flip_value;
763                 if( !sign2) cr -= flip_value;
764                 
765                 val = (int)
766                 ((float)y - (float)0.0000368 * (float)cb
767                  + (float)1.40199 * (float)cr + (float)0.5);
768                 
769                 if(val > max_value) val = max_value; else if(val < 0) val = 0;
770                 image->comps[0].data[i] = val;
771                 
772                 val = (int)
773                 ((float)1.0003 * (float)y - (float)0.344125 * (float)cb
774                  - (float)0.7141128 * (float)cr + (float)0.5);
775                 
776                 if(val > max_value) val = max_value; else if(val < 0) val = 0;
777                 image->comps[1].data[i] = val;
778                 
779                 val = (int)
780                 ((float)0.999823 * (float)y + (float)1.77204 * (float)cb
781                  - (float)0.000008 *(float)cr + (float)0.5);
782                 
783                 if(val > max_value) val = max_value; else if(val < 0) val = 0;
784                 image->comps[2].data[i] = val;
785         }
786         image->color_space = OPJ_CLRSPC_SRGB;
787
788 }// color_esycc_to_rgb()