Defines three new functions
[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 = 
601          cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16, 
602           INTENT_PERCEPTUAL, 0);
603
604 #ifdef HAVE_LIBLCMS2
605         cmsCloseProfile(in);
606         cmsCloseProfile(out);
607 #endif
608         if(transform == NULL)
609   {
610 #ifdef HAVE_LIBLCMS1
611         cmsCloseProfile(in);
612         cmsCloseProfile(out);
613 #endif
614         return;
615   }
616         prec0 = (double)image->comps[0].prec;
617         prec1 = (double)image->comps[1].prec;
618         prec2 = (double)image->comps[2].prec;
619
620         default_type = row[1];
621
622         if(default_type == 0x44454600)// DEF : default
623   {
624         rl = 100; ra = 170; rb = 200;
625         ol = 0;   
626         oa = pow(2, prec1 - 1);
627         ob = pow(2, prec2 - 2) +  pow(2, prec2 - 3);
628   }
629         else
630   {
631         rl = row[2]; ra = row[4]; rb = row[6];
632         ol = row[3]; oa = row[5]; ob = row[7];
633   }
634         L = src0 = image->comps[0].data;
635         a = src1 = image->comps[1].data;
636         b = src2 = image->comps[2].data;
637
638         max = image->comps[0].w * image->comps[0].h;
639
640         red = dst0 = (int*)malloc(max * sizeof(int));
641         green = dst1 = (int*)malloc(max * sizeof(int));
642         blue = dst2 = (int*)malloc(max * sizeof(int));
643
644         minL = -(rl * ol)/(pow(2, prec0)-1);
645         maxL = minL + rl;
646
647         mina = -(ra * oa)/(pow(2, prec1)-1);
648         maxa = mina + ra;
649
650         minb = -(rb * ob)/(pow(2, prec2)-1);
651         maxb = minb + rb;
652
653         for(i = 0; i < max; ++i)
654   {
655         Lab.L = minL + (double)(*L) * (maxL - minL)/(pow(2, prec0)-1); ++L;
656         Lab.a = mina + (double)(*a) * (maxa - mina)/(pow(2, prec1)-1); ++a;
657         Lab.b = minb + (double)(*b) * (maxb - minb)/(pow(2, prec2)-1); ++b;
658
659         cmsDoTransform(transform, &Lab, RGB, 1);
660
661         *red++ = RGB[0];
662         *green++ = RGB[1];
663         *blue++ = RGB[2];
664   }
665         cmsDeleteTransform(transform);
666 #ifdef HAVE_LIBLCMS1
667         cmsCloseProfile(in);
668         cmsCloseProfile(out);
669 #endif
670         free(src0); image->comps[0].data = dst0;
671         free(src1); image->comps[1].data = dst1;
672         free(src2); image->comps[2].data = dst2;
673
674         image->color_space = OPJ_CLRSPC_SRGB;
675         image->comps[0].prec = 16;
676         image->comps[1].prec = 16;
677         image->comps[2].prec = 16;
678
679         return;
680    }
681
682         fprintf(stderr,"%s:%d:\n\tenumCS %d not handled. Ignoring.\n",
683          __FILE__,__LINE__, enumcs);
684
685 }// color_apply_conversion()
686
687 #endif // HAVE_LIBLCMS2 || HAVE_LIBLCMS1
688
689 void color_cmyk_to_rgb(opj_image_t *image)
690 {
691         int *R, *G, *B, *dst0, *dst1, *dst2;
692         int *sc, *sm, *sy, *sk, *src0, *src1, *src2, *src3;
693         float C, M, Y, K;
694         unsigned int w, h, max, prec, len, i;
695
696         w = image->comps[0].w;
697         h = image->comps[0].h;
698         prec = image->comps[0].prec;
699
700         if(prec != 8) return;
701         if(image->numcomps != 4) return;
702
703         max = w * h;
704         len = max * sizeof(int);
705
706         R = dst0 = (int*)malloc(len);
707         G = dst1 = (int*)malloc(len);
708         B = dst2 = (int*)malloc(len);
709
710         sc = src0 = image->comps[0].data;
711         sm = src1 = image->comps[1].data;
712         sy = src2 = image->comps[2].data;
713         sk = src3 = image->comps[3].data;
714
715         for(i = 0; i < max; ++i)
716    {
717 // CMYK and CMY values from 0 to 1 
718 //
719         C = (float)(*sc++)/(float)255.; 
720         M = (float)(*sm++)/(float)255; 
721         Y = (float)(*sy++)/(float)255; 
722         K = (float)(*sk++)/(float)255;
723
724 // CMYK -> CMY 
725 //
726         C = ( C * ( (float)1. - K ) + K );
727         M = ( M * ( (float)1. - K ) + K );
728         Y = ( Y * ( (float)1. - K ) + K );
729
730 // CMY -> RGB : RGB results from 0 to 255 
731 //
732         *R++ = (int)(unsigned char)(( (float)1. - C ) * (float)255.);
733         *G++ = (int)(unsigned char)(( (float)1. - M ) * (float)255.);
734         *B++ = (int)(unsigned char)(( (float)1. - Y ) * (float)255.);
735    }
736
737         free(src0); image->comps[0].data = dst0;
738         free(src1); image->comps[1].data = dst1;
739         free(src2); image->comps[2].data = dst2;
740         free(src3); image->comps[3].data = NULL;
741
742         image->numcomps = 3;
743         image->color_space = OPJ_CLRSPC_SRGB;
744
745 }// color_cmyk_to_rgb()
746
747 //
748 // This code has been adopted from sjpx_openjpeg.c of ghostscript
749 //
750 void color_esycc_to_rgb(opj_image_t *image)
751 {
752     int *s0, *s1, *s2, *src0, *src1, *src2;
753     int *r, *g, *b, *dst0, *dst1, *dst2;
754     int y, cb, cr, sign1, sign2, val;
755     unsigned int w, h, max, i;
756     int flip_value = (1 << (image->comps[0].prec-1));
757         int max_value = (~(-1 << image->comps[0].prec));
758
759     if(image->numcomps != 3) return;
760
761     w = image->comps[0].w;
762     h = image->comps[0].h;
763
764     s0 = src0 = image->comps[0].data;
765     s1 = src1 = image->comps[1].data;
766     s2 = src2 = image->comps[2].data;
767
768     sign1 = image->comps[1].sgnd;
769     sign2 = image->comps[2].sgnd;
770
771     max = w * h;
772
773         r = dst0 = (int*)malloc(max * sizeof(int));
774     g = dst1 = (int*)malloc(max * sizeof(int));
775     b = dst2 = (int*)malloc(max * sizeof(int));
776
777     for(i = 0; i < max; ++i)
778    {
779
780     y = *s0++; cb = *s1++; cr = *s2++;
781
782     if( !sign1) cb -= flip_value;
783     if( !sign2) cr -= flip_value;
784
785     val = (int)
786         ((float)y - (float)0.0000368 * (float)cb 
787                 + (float)1.40199 * (float)cr + (float)0.5);
788
789         if(val > max_value) val = max_value; else if(val < 0) val = 0;
790     *r++ = val;
791
792     val = (int)
793         ((float)1.0003 * (float)y - (float)0.344125 * (float)cb 
794                 - (float)0.7141128 * (float)cr + (float)0.5);
795
796         if(val > max_value) val = max_value; else if(val < 0) val = 0;
797     *g++ = val;
798
799     val = (int)
800         ((float)0.999823 * (float)y + (float)1.77204 * (float)cb 
801                 - (float)0.000008 *(float)cr + (float)0.5);
802
803         if(val > max_value) val = max_value; else if(val < 0) val = 0;
804     *b++ = val;
805    }
806
807         free(src0); image->comps[0].data = dst0;
808         free(src1); image->comps[1].data = dst1;
809         free(src2); image->comps[2].data = dst2;
810
811         image->numcomps = 3;
812         image->color_space = OPJ_CLRSPC_SRGB;
813
814 }// color_esycc_to_rgb()