cppcheck fix for openjp2 (#740)
[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         if(r == NULL || g == NULL || b == NULL) goto fails;
112
113         for(i = 0U; i < max; ++i)
114         {
115                 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
116                 ++y; ++cb; ++cr; ++r; ++g; ++b;
117         }
118         free(img->comps[0].data); img->comps[0].data = d0;
119         free(img->comps[1].data); img->comps[1].data = d1;
120         free(img->comps[2].data); img->comps[2].data = d2;
121         return;
122
123 fails:
124         if(r) free(r);
125         if(g) free(g);
126         if(b) free(b);
127
128 }/* sycc444_to_rgb() */
129
130 static void sycc422_to_rgb(opj_image_t *img)
131 {       
132         int *d0, *d1, *d2, *r, *g, *b;
133         const int *y, *cb, *cr;
134         unsigned int maxw, maxh, max;
135         int offset, upb;
136         unsigned int i, j;
137
138         upb = (int)img->comps[0].prec;
139         offset = 1<<(upb - 1); upb = (1<<upb)-1;
140
141         maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
142         max = maxw * maxh;
143
144         y = img->comps[0].data;
145         cb = img->comps[1].data;
146         cr = img->comps[2].data;
147
148         d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
149         d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
150         d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
151
152         if(r == NULL || g == NULL || b == NULL) goto fails;
153
154         for(i=0U; i < maxh; ++i)
155         {
156                 for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
157                 {
158                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
159                         ++y; ++r; ++g; ++b;
160                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
161                         ++y; ++r; ++g; ++b; ++cb; ++cr;
162                 }
163                 if (j < maxw) {
164                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
165                         ++y; ++r; ++g; ++b; ++cb; ++cr;
166                 }
167         }
168         free(img->comps[0].data); img->comps[0].data = d0;
169         free(img->comps[1].data); img->comps[1].data = d1;
170         free(img->comps[2].data); img->comps[2].data = d2;
171
172 #if defined(USE_JPWL) || defined(USE_MJ2)
173         img->comps[1].w = maxw; img->comps[1].h = maxh;
174         img->comps[2].w = maxw; img->comps[2].h = maxh;
175 #else
176         img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
177         img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
178 #endif
179         img->comps[1].dx = img->comps[0].dx;
180         img->comps[2].dx = img->comps[0].dx;
181         img->comps[1].dy = img->comps[0].dy;
182         img->comps[2].dy = img->comps[0].dy;
183         return;
184
185 fails:
186         if(r) free(r);
187         if(g) free(g);
188         if(b) free(b);
189
190 }/* sycc422_to_rgb() */
191
192 static void sycc420_to_rgb(opj_image_t *img)
193 {
194         int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
195         const int *y, *cb, *cr, *ny;
196         unsigned int maxw, maxh, max;
197         int offset, upb;
198         unsigned int i, j;
199
200         upb = (int)img->comps[0].prec;
201         offset = 1<<(upb - 1); upb = (1<<upb)-1;
202
203         maxw = (unsigned int)img->comps[0].w; maxh = (unsigned int)img->comps[0].h;
204         max = maxw * maxh;
205
206         y = img->comps[0].data;
207         cb = img->comps[1].data;
208         cr = img->comps[2].data;
209
210         d0 = r = (int*)malloc(sizeof(int) * (size_t)max);
211         d1 = g = (int*)malloc(sizeof(int) * (size_t)max);
212         d2 = b = (int*)malloc(sizeof(int) * (size_t)max);
213
214         if(r == NULL || g == NULL || b == NULL) goto fails;
215
216         for(i=0U; i < (maxh & ~(unsigned int)1U); i += 2U)
217         {
218                 ny = y + maxw;
219                 nr = r + maxw; ng = g + maxw; nb = b + maxw;
220
221                 for(j=0; j < (maxw & ~(unsigned int)1U); j += 2U)
222                 {
223                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
224                         ++y; ++r; ++g; ++b;
225                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
226                         ++y; ++r; ++g; ++b;
227
228                         sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
229                         ++ny; ++nr; ++ng; ++nb;
230                         sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
231                         ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
232                 }
233                 if(j < maxw)
234                 {
235                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
236                         ++y; ++r; ++g; ++b;
237
238                         sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
239                         ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
240                 }
241                 y += maxw; r += maxw; g += maxw; b += maxw;
242         }
243         if(i < maxh)
244         {
245                 for(j=0U; j < (maxw & ~(unsigned int)1U); j += 2U)
246                 {
247                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
248
249                         ++y; ++r; ++g; ++b;
250
251                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
252
253                         ++y; ++r; ++g; ++b; ++cb; ++cr;
254                 }
255                 if(j < maxw)
256                 {
257                         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
258                 }
259         }
260
261         free(img->comps[0].data); img->comps[0].data = d0;
262         free(img->comps[1].data); img->comps[1].data = d1;
263         free(img->comps[2].data); img->comps[2].data = d2;
264
265 #if defined(USE_JPWL) || defined(USE_MJ2)
266         img->comps[1].w = maxw; img->comps[1].h = maxh;
267         img->comps[2].w = maxw; img->comps[2].h = maxh;
268 #else
269         img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
270         img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
271 #endif
272         img->comps[1].dx = img->comps[0].dx;
273         img->comps[2].dx = img->comps[0].dx;
274         img->comps[1].dy = img->comps[0].dy;
275         img->comps[2].dy = img->comps[0].dy;
276         return;
277
278 fails:
279         if(r) free(r);
280         if(g) free(g);
281         if(b) free(b);
282
283 }/* sycc420_to_rgb() */
284
285 void color_sycc_to_rgb(opj_image_t *img)
286 {
287         if(img->numcomps < 3)
288         {
289                 img->color_space = OPJ_CLRSPC_GRAY;
290                 return;
291         }
292
293         if((img->comps[0].dx == 1)
294         && (img->comps[1].dx == 2)
295         && (img->comps[2].dx == 2)
296         && (img->comps[0].dy == 1)
297         && (img->comps[1].dy == 2)
298         && (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */
299   {
300                 sycc420_to_rgb(img);
301   }
302         else
303         if((img->comps[0].dx == 1)
304         && (img->comps[1].dx == 2)
305         && (img->comps[2].dx == 2)
306         && (img->comps[0].dy == 1)
307         && (img->comps[1].dy == 1)
308         && (img->comps[2].dy == 1))/* horizontal sub-sample only */
309   {
310                 sycc422_to_rgb(img);
311   }
312         else
313         if((img->comps[0].dx == 1)
314         && (img->comps[1].dx == 1)
315         && (img->comps[2].dx == 1)
316         && (img->comps[0].dy == 1)
317         && (img->comps[1].dy == 1)
318         && (img->comps[2].dy == 1))/* no sub-sample */
319   {
320                 sycc444_to_rgb(img);
321   }
322         else
323   {
324                 fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,__LINE__);
325                 return;
326   }
327         img->color_space = OPJ_CLRSPC_SRGB;
328
329 }/* color_sycc_to_rgb() */
330
331 #if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
332
333 #ifdef OPJ_HAVE_LIBLCMS1
334 /* Bob Friesenhahn proposed:*/
335 #define cmsSigXYZData   icSigXYZData
336 #define cmsSigLabData   icSigLabData
337 #define cmsSigCmykData  icSigCmykData
338 #define cmsSigYCbCrData icSigYCbCrData
339 #define cmsSigLuvData   icSigLuvData
340 #define cmsSigGrayData  icSigGrayData
341 #define cmsSigRgbData   icSigRgbData
342 #define cmsUInt32Number DWORD
343
344 #define cmsColorSpaceSignature icColorSpaceSignature
345 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
346
347 #endif /* OPJ_HAVE_LIBLCMS1 */
348
349 /*#define DEBUG_PROFILE*/
350 void color_apply_icc_profile(opj_image_t *image)
351 {
352         cmsHPROFILE in_prof, out_prof;
353         cmsHTRANSFORM transform;
354         cmsColorSpaceSignature in_space, out_space;
355         cmsUInt32Number intent, in_type, out_type;
356         int *r, *g, *b;
357         size_t nr_samples;
358         int prec, i, max, max_w, max_h, ok = 0;
359         OPJ_COLOR_SPACE new_space;
360
361         in_prof = 
362          cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
363 #ifdef DEBUG_PROFILE
364   FILE *icm = fopen("debug.icm","wb");
365   fwrite( image->icc_profile_buf,1, image->icc_profile_len,icm);
366   fclose(icm);
367 #endif
368
369         if(in_prof == NULL) return;
370
371         in_space = cmsGetPCS(in_prof);
372         out_space = cmsGetColorSpace(in_prof);
373         intent = cmsGetHeaderRenderingIntent(in_prof);
374
375         
376         max_w = (int)image->comps[0].w;
377   max_h = (int)image->comps[0].h;
378         prec = (int)image->comps[0].prec;
379
380         if(out_space == cmsSigRgbData) /* enumCS 16 */
381    {
382         if( prec <= 8 )
383   {
384         in_type = TYPE_RGB_8;
385         out_type = TYPE_RGB_8;
386   }
387         else
388   {
389         in_type = TYPE_RGB_16;
390         out_type = TYPE_RGB_16;
391   }
392         out_prof = cmsCreate_sRGBProfile();
393         new_space = OPJ_CLRSPC_SRGB;
394    }
395         else
396         if(out_space == cmsSigGrayData) /* enumCS 17 */
397    {
398         in_type = TYPE_GRAY_8;
399         out_type = TYPE_RGB_8;
400         out_prof = cmsCreate_sRGBProfile();
401         new_space = OPJ_CLRSPC_SRGB;
402    }
403         else
404         if(out_space == cmsSigYCbCrData) /* enumCS 18 */
405    {
406         in_type = TYPE_YCbCr_16;
407         out_type = TYPE_RGB_16;
408         out_prof = cmsCreate_sRGBProfile();
409         new_space = OPJ_CLRSPC_SRGB;
410    }
411         else
412    {
413 #ifdef DEBUG_PROFILE
414 fprintf(stderr,"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
415 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
416 __FILE__,__LINE__,out_space,
417         (out_space>>24) & 0xff,(out_space>>16) & 0xff,
418         (out_space>>8) & 0xff, out_space & 0xff);
419 #endif
420         cmsCloseProfile(in_prof);
421
422         return;
423    }
424         if(out_prof == NULL)
425    {
426         cmsCloseProfile(in_prof);
427
428         return;
429    }
430
431 #ifdef DEBUG_PROFILE
432 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
433 "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
434         max_w,max_h, (void*)in_prof,(void*)out_prof);
435
436 fprintf(stderr,"\trender_intent (%u)\n\t"
437 "color_space: in(%#x)(%c%c%c%c)   out:(%#x)(%c%c%c%c)\n\t"
438 "       type: in(%u)              out:(%u)\n",
439         intent,
440         in_space,
441         (in_space>>24) & 0xff,(in_space>>16) & 0xff,
442         (in_space>>8) & 0xff, in_space & 0xff,
443
444         out_space,
445         (out_space>>24) & 0xff,(out_space>>16) & 0xff,
446         (out_space>>8) & 0xff, out_space & 0xff,
447
448         in_type,out_type
449  );
450 #else
451   (void)prec;
452   (void)in_space;
453 #endif /* DEBUG_PROFILE */
454
455         transform = cmsCreateTransform(in_prof, in_type,
456          out_prof, out_type, intent, 0);
457
458 #ifdef OPJ_HAVE_LIBLCMS2
459 /* Possible for: LCMS_VERSION >= 2000 :*/
460         cmsCloseProfile(in_prof);
461         cmsCloseProfile(out_prof);
462 #endif
463
464         if(transform == NULL)
465    {
466 #ifdef DEBUG_PROFILE
467 fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
468 "ICC Profile ignored.\n",__FILE__,__LINE__);
469 #endif
470
471 #ifdef OPJ_HAVE_LIBLCMS1
472         cmsCloseProfile(in_prof);
473         cmsCloseProfile(out_prof);
474 #endif
475         return;
476    }
477
478         if(image->numcomps > 2)/* RGB, RGBA */
479    {
480         if( prec <= 8 )
481   {
482         unsigned char *inbuf, *outbuf, *in, *out;
483
484         max = max_w * max_h;
485         nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
486         in = inbuf = (unsigned char*)malloc(nr_samples);
487         out = outbuf = (unsigned char*)malloc(nr_samples);
488
489         if(inbuf == NULL || outbuf == NULL) goto fails0;
490
491         r = image->comps[0].data;
492         g = image->comps[1].data;
493         b = image->comps[2].data;
494
495         for(i = 0; i < max; ++i)
496  {
497         *in++ = (unsigned char)*r++;
498         *in++ = (unsigned char)*g++;
499         *in++ = (unsigned char)*b++;
500  }
501
502         cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
503
504         r = image->comps[0].data;
505         g = image->comps[1].data;
506         b = image->comps[2].data;
507
508         for(i = 0; i < max; ++i)
509  {
510         *r++ = (int)*out++;
511         *g++ = (int)*out++;
512         *b++ = (int)*out++;
513  }
514         ok = 1;
515
516 fails0:
517         if(inbuf) free(inbuf);
518         if(outbuf) free(outbuf);
519   }
520         else /* prec > 8 */
521   {
522         unsigned short *inbuf, *outbuf, *in, *out;
523
524         max = max_w * max_h;
525         nr_samples = (size_t)(max * 3 * sizeof(unsigned short));
526         in = inbuf = (unsigned short*)malloc(nr_samples);
527         out = outbuf = (unsigned short*)malloc(nr_samples);
528
529         if(inbuf == NULL || outbuf == NULL) goto fails1;
530
531         r = image->comps[0].data;
532         g = image->comps[1].data;
533         b = image->comps[2].data;
534
535         for(i = 0; i < max; ++i)
536  {
537         *in++ = (unsigned short)*r++;
538         *in++ = (unsigned short)*g++;
539         *in++ = (unsigned short)*b++;
540  }
541
542         cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
543
544         r = image->comps[0].data;
545         g = image->comps[1].data;
546         b = image->comps[2].data;
547
548         for(i = 0; i < max; ++i)
549  {
550         *r++ = (int)*out++;
551         *g++ = (int)*out++;
552         *b++ = (int)*out++;
553  }
554         ok = 1;
555
556 fails1:
557         if(inbuf) free(inbuf); 
558         if(outbuf) free(outbuf);
559   }
560    }
561         else /* image->numcomps <= 2 : GRAY, GRAYA */
562    {
563         if(prec <= 8)
564   {
565         unsigned char *in, *inbuf, *out, *outbuf;
566         opj_image_comp_t *new_comps;
567
568         max = max_w * max_h;
569         nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
570         in = inbuf = (unsigned char*)malloc(nr_samples);
571         out = outbuf = (unsigned char*)malloc(nr_samples);
572         g = (int*)calloc((size_t)max, sizeof(int));
573         b = (int*)calloc((size_t)max, sizeof(int));
574
575         if(inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) goto fails2;
576
577         new_comps = (opj_image_comp_t*)
578          realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
579
580         if(new_comps == NULL) goto fails2;
581
582         image->comps = new_comps;
583
584         if(image->numcomps == 2)
585          image->comps[3] = image->comps[1];
586
587         image->comps[1] = image->comps[0];
588         image->comps[2] = image->comps[0];
589
590         image->comps[1].data = g;
591         image->comps[2].data = b;
592
593         image->numcomps += 2;
594
595         r = image->comps[0].data;
596
597         for(i = 0; i < max; ++i)
598  {
599         *in++ = (unsigned char)*r++;
600  }
601         cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
602
603         r = image->comps[0].data;
604         g = image->comps[1].data;
605         b = image->comps[2].data;
606
607         for(i = 0; i < max; ++i)
608  {
609         *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
610  }
611         r = g = b = NULL;
612         ok = 1;
613
614 fails2:
615         if(inbuf) free(inbuf);
616         if(outbuf) free(outbuf);
617         if(g) free(g);
618         if(b) free(b);
619   }
620         else /* prec > 8 */
621   {
622         unsigned short *in, *inbuf, *out, *outbuf;
623         opj_image_comp_t *new_comps;
624
625         max = max_w * max_h;
626         nr_samples = (size_t)(max * 3 * sizeof(unsigned short));
627         in = inbuf = (unsigned short*)malloc(nr_samples);
628         out = outbuf = (unsigned short*)malloc(nr_samples);
629         g = (int*)calloc((size_t)max, sizeof(int));
630         b = (int*)calloc((size_t)max, sizeof(int));
631
632         if(inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) goto fails3;
633
634         new_comps = (opj_image_comp_t*)
635          realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
636
637         if(new_comps == NULL) goto fails3;
638
639         image->comps = new_comps;
640
641         if(image->numcomps == 2)
642          image->comps[3] = image->comps[1];
643
644         image->comps[1] = image->comps[0];
645         image->comps[2] = image->comps[0];
646
647         image->comps[1].data = g;
648         image->comps[2].data = b;
649
650         image->numcomps += 2;
651
652         r = image->comps[0].data;
653
654         for(i = 0; i < max; ++i)
655  {
656         *in++ = (unsigned short)*r++;
657  }
658         cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
659
660         r = image->comps[0].data;
661         g = image->comps[1].data;
662         b = image->comps[2].data;
663
664         for(i = 0; i < max; ++i)
665  {
666         *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
667  }
668         r = g = b = NULL;
669         ok = 1;
670
671 fails3:
672         if(inbuf) free(inbuf);
673         if(outbuf) free(outbuf);
674         if(g) free(g);
675         if(b) free(b);
676   }
677    }/* if(image->numcomps > 2) */
678
679         cmsDeleteTransform(transform);
680
681 #ifdef OPJ_HAVE_LIBLCMS1
682         cmsCloseProfile(in_prof);
683         cmsCloseProfile(out_prof);
684 #endif
685         if(ok)
686    {
687         image->color_space = new_space;
688    }
689 }/* color_apply_icc_profile() */
690
691 void color_cielab_to_rgb(opj_image_t *image)
692 {
693         int *row;
694         int enumcs, numcomps;
695         OPJ_COLOR_SPACE new_space;
696
697         numcomps = (int)image->numcomps;
698         
699         if(numcomps != 3)
700         {
701                 fprintf(stderr,"%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
702                                                 __FILE__,__LINE__,numcomps);
703                 return;
704         }
705         
706         row = (int*)image->icc_profile_buf;
707         enumcs = row[0];
708         
709         if(enumcs == 14) /* CIELab */
710         {
711                 int *L, *a, *b, *red, *green, *blue;
712                 int *src0, *src1, *src2, *dst0, *dst1, *dst2;
713                 double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
714                 double minL, maxL, mina, maxa, minb, maxb;
715                 unsigned int default_type;
716                 unsigned int i, max;
717                 cmsHPROFILE in, out;
718                 cmsHTRANSFORM transform;
719                 cmsUInt16Number RGB[3];
720                 cmsCIELab Lab;
721                 
722                 in = cmsCreateLab4Profile(NULL);
723                 if(in == NULL){
724                         return;
725                 }
726                 out = cmsCreate_sRGBProfile();
727                 if(out == NULL){
728                         cmsCloseProfile(in);
729                         return;
730                 }
731                 transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16, INTENT_PERCEPTUAL, 0);
732                 
733 #ifdef OPJ_HAVE_LIBLCMS2
734                 cmsCloseProfile(in);
735                 cmsCloseProfile(out);
736 #endif
737                 if(transform == NULL)
738                 {
739 #ifdef OPJ_HAVE_LIBLCMS1
740                         cmsCloseProfile(in);
741                         cmsCloseProfile(out);
742 #endif
743                         return;
744                 }
745                 new_space = OPJ_CLRSPC_SRGB;
746
747                 prec0 = (double)image->comps[0].prec;
748                 prec1 = (double)image->comps[1].prec;
749                 prec2 = (double)image->comps[2].prec;
750                 
751                 default_type = (unsigned int)row[1];
752                 
753                 if(default_type == 0x44454600)/* DEF : default */
754                 {
755                         rl = 100; ra = 170; rb = 200;
756                         ol = 0;
757                         oa = pow(2, prec1 - 1);
758                         ob = pow(2, prec2 - 2) +  pow(2, prec2 - 3);
759                 }
760                 else
761                 {
762                         rl = row[2]; ra = row[4]; rb = row[6];
763                         ol = row[3]; oa = row[5]; ob = row[7];
764                 }
765                 
766                 L = src0 = image->comps[0].data;
767                 a = src1 = image->comps[1].data;
768                 b = src2 = image->comps[2].data;
769                 
770                 max = image->comps[0].w * image->comps[0].h;
771                 
772                 red = dst0 = (int*)malloc(max * sizeof(int));
773                 green = dst1 = (int*)malloc(max * sizeof(int));
774                 blue = dst2 = (int*)malloc(max * sizeof(int));
775
776                 if(red == NULL || green == NULL || blue == NULL) goto fails;
777
778                 minL = -(rl * ol)/(pow(2, prec0)-1);
779                 maxL = minL + rl;
780                 
781                 mina = -(ra * oa)/(pow(2, prec1)-1);
782                 maxa = mina + ra;
783                 
784                 minb = -(rb * ob)/(pow(2, prec2)-1);
785                 maxb = minb + rb;
786                 
787                 for(i = 0; i < max; ++i)
788                 {
789                         Lab.L = minL + (double)(*L) * (maxL - minL)/(pow(2, prec0)-1); ++L;
790                         Lab.a = mina + (double)(*a) * (maxa - mina)/(pow(2, prec1)-1); ++a;
791                         Lab.b = minb + (double)(*b) * (maxb - minb)/(pow(2, prec2)-1); ++b;
792                 
793                         cmsDoTransform(transform, &Lab, RGB, 1);
794                 
795                         *red++ = RGB[0];
796                         *green++ = RGB[1];
797                         *blue++ = RGB[2];
798                 }
799                 cmsDeleteTransform(transform);
800 #ifdef OPJ_HAVE_LIBLCMS1
801                 cmsCloseProfile(in);
802                 cmsCloseProfile(out);
803 #endif
804                 free(src0); image->comps[0].data = dst0;
805                 free(src1); image->comps[1].data = dst1;
806                 free(src2); image->comps[2].data = dst2;
807                 
808                 image->color_space = new_space;
809                 image->comps[0].prec = 16;
810                 image->comps[1].prec = 16;
811                 image->comps[2].prec = 16;
812                 
813                 return;
814
815 fails:
816                 cmsDeleteTransform(transform);
817 #ifdef OPJ_HAVE_LIBLCMS1
818                 cmsCloseProfile(in);
819                 cmsCloseProfile(out);
820 #endif
821                 if(red) free(red);
822                 if(green) free(green);
823                 if(blue) free(blue);
824                 return;
825         }
826         
827         fprintf(stderr,"%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,__LINE__, enumcs);
828 }/* color_cielab_to_rgb() */
829
830 #endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */
831
832 void color_cmyk_to_rgb(opj_image_t *image)
833 {
834         float C, M, Y, K;
835         float sC, sM, sY, sK;
836         unsigned int w, h, max, i;
837
838         w = image->comps[0].w;
839         h = image->comps[0].h;
840
841         if(image->numcomps < 4) return;
842
843         max = w * h;
844         
845         sC = 1.0F / (float)((1 << image->comps[0].prec) - 1);
846         sM = 1.0F / (float)((1 << image->comps[1].prec) - 1);
847         sY = 1.0F / (float)((1 << image->comps[2].prec) - 1);
848         sK = 1.0F / (float)((1 << image->comps[3].prec) - 1);
849
850         for(i = 0; i < max; ++i)
851         {
852                 /* CMYK values from 0 to 1 */
853                 C = (float)(image->comps[0].data[i]) * sC;
854                 M = (float)(image->comps[1].data[i]) * sM;
855                 Y = (float)(image->comps[2].data[i]) * sY;
856                 K = (float)(image->comps[3].data[i]) * sK;
857                 
858                 /* Invert all CMYK values */
859                 C = 1.0F - C;
860                 M = 1.0F - M;
861                 Y = 1.0F - Y;
862                 K = 1.0F - K;
863
864                 /* CMYK -> RGB : RGB results from 0 to 255 */
865                 image->comps[0].data[i] = (int)(255.0F * C * K); /* R */
866                 image->comps[1].data[i] = (int)(255.0F * M * K); /* G */
867                 image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */
868         }
869
870         free(image->comps[3].data); image->comps[3].data = NULL;
871         image->comps[0].prec = 8;
872         image->comps[1].prec = 8;
873         image->comps[2].prec = 8;
874         image->numcomps -= 1;
875         image->color_space = OPJ_CLRSPC_SRGB;
876         
877         for (i = 3; i < image->numcomps; ++i) {
878                 memcpy(&(image->comps[i]), &(image->comps[i+1]), sizeof(image->comps[i]));
879         }
880
881 }/* color_cmyk_to_rgb() */
882
883 /*
884  * This code has been adopted from sjpx_openjpeg.c of ghostscript
885  */
886 void color_esycc_to_rgb(opj_image_t *image)
887 {
888         int y, cb, cr, sign1, sign2, val;
889         unsigned int w, h, max, i;
890         int flip_value = (1 << (image->comps[0].prec-1));
891         int max_value = (1 << image->comps[0].prec) - 1;
892         
893         if(image->numcomps < 3) return;
894         
895         w = image->comps[0].w;
896         h = image->comps[0].h;
897         
898         sign1 = (int)image->comps[1].sgnd;
899         sign2 = (int)image->comps[2].sgnd;
900         
901         max = w * h;
902         
903         for(i = 0; i < max; ++i)
904         {
905                 
906                 y = image->comps[0].data[i]; cb = image->comps[1].data[i]; cr = image->comps[2].data[i];
907                 
908                 if( !sign1) cb -= flip_value;
909                 if( !sign2) cr -= flip_value;
910                 
911                 val = (int)
912                 ((float)y - (float)0.0000368 * (float)cb
913                  + (float)1.40199 * (float)cr + (float)0.5);
914                 
915                 if(val > max_value) val = max_value; else if(val < 0) val = 0;
916                 image->comps[0].data[i] = val;
917                 
918                 val = (int)
919                 ((float)1.0003 * (float)y - (float)0.344125 * (float)cb
920                  - (float)0.7141128 * (float)cr + (float)0.5);
921                 
922                 if(val > max_value) val = max_value; else if(val < 0) val = 0;
923                 image->comps[1].data[i] = val;
924                 
925                 val = (int)
926                 ((float)0.999823 * (float)y + (float)1.77204 * (float)cb
927                  - (float)0.000008 *(float)cr + (float)0.5);
928                 
929                 if(val > max_value) val = max_value; else if(val < 0) val = 0;
930                 image->comps[2].data[i] = val;
931         }
932         image->color_space = OPJ_CLRSPC_SRGB;
933
934 }/* color_esycc_to_rgb() */