Fix warnings about signed/unsigned casts in pi.c
[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;
80     cr -= offset;
81     r = y + (int)(1.402 * (float)cr);
82     if (r < 0) {
83         r = 0;
84     } else if (r > upb) {
85         r = upb;
86     }
87     *out_r = r;
88
89     g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
90     if (g < 0) {
91         g = 0;
92     } else if (g > upb) {
93         g = upb;
94     }
95     *out_g = g;
96
97     b = y + (int)(1.772 * (float)cb);
98     if (b < 0) {
99         b = 0;
100     } else if (b > upb) {
101         b = upb;
102     }
103     *out_b = b;
104 }
105
106 static void sycc444_to_rgb(opj_image_t *img)
107 {
108     int *d0, *d1, *d2, *r, *g, *b;
109     const int *y, *cb, *cr;
110     size_t maxw, maxh, max, i;
111     int offset, upb;
112
113     upb = (int)img->comps[0].prec;
114     offset = 1 << (upb - 1);
115     upb = (1 << upb) - 1;
116
117     maxw = (size_t)img->comps[0].w;
118     maxh = (size_t)img->comps[0].h;
119     max = maxw * maxh;
120
121     y = img->comps[0].data;
122     cb = img->comps[1].data;
123     cr = img->comps[2].data;
124
125     d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
126     d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
127     d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);
128
129     if (r == NULL || g == NULL || b == NULL) {
130         goto fails;
131     }
132
133     for (i = 0U; i < max; ++i) {
134         sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
135         ++y;
136         ++cb;
137         ++cr;
138         ++r;
139         ++g;
140         ++b;
141     }
142     opj_image_data_free(img->comps[0].data);
143     img->comps[0].data = d0;
144     opj_image_data_free(img->comps[1].data);
145     img->comps[1].data = d1;
146     opj_image_data_free(img->comps[2].data);
147     img->comps[2].data = d2;
148     img->color_space = OPJ_CLRSPC_SRGB;
149     return;
150
151 fails:
152     opj_image_data_free(r);
153     opj_image_data_free(g);
154     opj_image_data_free(b);
155 }/* sycc444_to_rgb() */
156
157 static void sycc422_to_rgb(opj_image_t *img)
158 {
159     int *d0, *d1, *d2, *r, *g, *b;
160     const int *y, *cb, *cr;
161     size_t maxw, maxh, max, offx, loopmaxw;
162     int offset, upb;
163     size_t i;
164
165     upb = (int)img->comps[0].prec;
166     offset = 1 << (upb - 1);
167     upb = (1 << upb) - 1;
168
169     maxw = (size_t)img->comps[0].w;
170     maxh = (size_t)img->comps[0].h;
171     max = maxw * maxh;
172
173     y = img->comps[0].data;
174     cb = img->comps[1].data;
175     cr = img->comps[2].data;
176
177     d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
178     d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
179     d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);
180
181     if (r == NULL || g == NULL || b == NULL) {
182         goto fails;
183     }
184
185     /* if img->x0 is odd, then first column shall use Cb/Cr = 0 */
186     offx = img->x0 & 1U;
187     loopmaxw = maxw - offx;
188
189     for (i = 0U; i < maxh; ++i) {
190         size_t j;
191
192         if (offx > 0U) {
193             sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
194             ++y;
195             ++r;
196             ++g;
197             ++b;
198         }
199
200         for (j = 0U; j < (loopmaxw & ~(size_t)1U); j += 2U) {
201             sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
202             ++y;
203             ++r;
204             ++g;
205             ++b;
206             sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
207             ++y;
208             ++r;
209             ++g;
210             ++b;
211             ++cb;
212             ++cr;
213         }
214         if (j < loopmaxw) {
215             sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
216             ++y;
217             ++r;
218             ++g;
219             ++b;
220             ++cb;
221             ++cr;
222         }
223     }
224
225     opj_image_data_free(img->comps[0].data);
226     img->comps[0].data = d0;
227     opj_image_data_free(img->comps[1].data);
228     img->comps[1].data = d1;
229     opj_image_data_free(img->comps[2].data);
230     img->comps[2].data = d2;
231
232     img->comps[1].w = img->comps[2].w = img->comps[0].w;
233     img->comps[1].h = img->comps[2].h = img->comps[0].h;
234     img->comps[1].dx = img->comps[2].dx = img->comps[0].dx;
235     img->comps[1].dy = img->comps[2].dy = img->comps[0].dy;
236     img->color_space = OPJ_CLRSPC_SRGB;
237     return;
238
239 fails:
240     opj_image_data_free(r);
241     opj_image_data_free(g);
242     opj_image_data_free(b);
243 }/* sycc422_to_rgb() */
244
245 static void sycc420_to_rgb(opj_image_t *img)
246 {
247     int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
248     const int *y, *cb, *cr, *ny;
249     size_t maxw, maxh, max, offx, loopmaxw, offy, loopmaxh;
250     int offset, upb;
251     size_t i;
252
253     upb = (int)img->comps[0].prec;
254     offset = 1 << (upb - 1);
255     upb = (1 << upb) - 1;
256
257     maxw = (size_t)img->comps[0].w;
258     maxh = (size_t)img->comps[0].h;
259     max = maxw * maxh;
260
261     y = img->comps[0].data;
262     cb = img->comps[1].data;
263     cr = img->comps[2].data;
264
265     d0 = r = (int*)opj_image_data_alloc(sizeof(int) * max);
266     d1 = g = (int*)opj_image_data_alloc(sizeof(int) * max);
267     d2 = b = (int*)opj_image_data_alloc(sizeof(int) * max);
268
269     if (r == NULL || g == NULL || b == NULL) {
270         goto fails;
271     }
272
273     /* if img->x0 is odd, then first column shall use Cb/Cr = 0 */
274     offx = img->x0 & 1U;
275     loopmaxw = maxw - offx;
276     /* if img->y0 is odd, then first line shall use Cb/Cr = 0 */
277     offy = img->y0 & 1U;
278     loopmaxh = maxh - offy;
279
280     if (offy > 0U) {
281         size_t j;
282
283         for (j = 0; j < maxw; ++j) {
284             sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
285             ++y;
286             ++r;
287             ++g;
288             ++b;
289         }
290     }
291
292     for (i = 0U; i < (loopmaxh & ~(size_t)1U); i += 2U) {
293         size_t j;
294
295         ny = y + maxw;
296         nr = r + maxw;
297         ng = g + maxw;
298         nb = b + maxw;
299
300         if (offx > 0U) {
301             sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
302             ++y;
303             ++r;
304             ++g;
305             ++b;
306             sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
307             ++ny;
308             ++nr;
309             ++ng;
310             ++nb;
311         }
312
313         for (j = 0; j < (loopmaxw & ~(size_t)1U); j += 2U) {
314             sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
315             ++y;
316             ++r;
317             ++g;
318             ++b;
319             sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
320             ++y;
321             ++r;
322             ++g;
323             ++b;
324
325             sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
326             ++ny;
327             ++nr;
328             ++ng;
329             ++nb;
330             sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
331             ++ny;
332             ++nr;
333             ++ng;
334             ++nb;
335             ++cb;
336             ++cr;
337         }
338         if (j < loopmaxw) {
339             sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
340             ++y;
341             ++r;
342             ++g;
343             ++b;
344
345             sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
346             ++ny;
347             ++nr;
348             ++ng;
349             ++nb;
350             ++cb;
351             ++cr;
352         }
353         y += maxw;
354         r += maxw;
355         g += maxw;
356         b += maxw;
357     }
358     if (i < loopmaxh) {
359         size_t j;
360
361         for (j = 0U; j < (maxw & ~(size_t)1U); j += 2U) {
362             sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
363
364             ++y;
365             ++r;
366             ++g;
367             ++b;
368
369             sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
370
371             ++y;
372             ++r;
373             ++g;
374             ++b;
375             ++cb;
376             ++cr;
377         }
378         if (j < maxw) {
379             sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
380         }
381     }
382
383     opj_image_data_free(img->comps[0].data);
384     img->comps[0].data = d0;
385     opj_image_data_free(img->comps[1].data);
386     img->comps[1].data = d1;
387     opj_image_data_free(img->comps[2].data);
388     img->comps[2].data = d2;
389
390     img->comps[1].w = img->comps[2].w = img->comps[0].w;
391     img->comps[1].h = img->comps[2].h = img->comps[0].h;
392     img->comps[1].dx = img->comps[2].dx = img->comps[0].dx;
393     img->comps[1].dy = img->comps[2].dy = img->comps[0].dy;
394     img->color_space = OPJ_CLRSPC_SRGB;
395     return;
396
397 fails:
398     opj_image_data_free(r);
399     opj_image_data_free(g);
400     opj_image_data_free(b);
401 }/* sycc420_to_rgb() */
402
403 void color_sycc_to_rgb(opj_image_t *img)
404 {
405     if (img->numcomps < 3) {
406         img->color_space = OPJ_CLRSPC_GRAY;
407         return;
408     }
409
410     if ((img->comps[0].dx == 1)
411             && (img->comps[1].dx == 2)
412             && (img->comps[2].dx == 2)
413             && (img->comps[0].dy == 1)
414             && (img->comps[1].dy == 2)
415             && (img->comps[2].dy == 2)) { /* horizontal and vertical sub-sample */
416         sycc420_to_rgb(img);
417     } else if ((img->comps[0].dx == 1)
418                && (img->comps[1].dx == 2)
419                && (img->comps[2].dx == 2)
420                && (img->comps[0].dy == 1)
421                && (img->comps[1].dy == 1)
422                && (img->comps[2].dy == 1)) { /* horizontal sub-sample only */
423         sycc422_to_rgb(img);
424     } else if ((img->comps[0].dx == 1)
425                && (img->comps[1].dx == 1)
426                && (img->comps[2].dx == 1)
427                && (img->comps[0].dy == 1)
428                && (img->comps[1].dy == 1)
429                && (img->comps[2].dy == 1)) { /* no sub-sample */
430         sycc444_to_rgb(img);
431     } else {
432         fprintf(stderr, "%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
433                 __LINE__);
434         return;
435     }
436 }/* color_sycc_to_rgb() */
437
438 #if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
439
440 #ifdef OPJ_HAVE_LIBLCMS1
441 /* Bob Friesenhahn proposed:*/
442 #define cmsSigXYZData   icSigXYZData
443 #define cmsSigLabData   icSigLabData
444 #define cmsSigCmykData  icSigCmykData
445 #define cmsSigYCbCrData icSigYCbCrData
446 #define cmsSigLuvData   icSigLuvData
447 #define cmsSigGrayData  icSigGrayData
448 #define cmsSigRgbData   icSigRgbData
449 #define cmsUInt32Number DWORD
450
451 #define cmsColorSpaceSignature icColorSpaceSignature
452 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
453
454 #endif /* OPJ_HAVE_LIBLCMS1 */
455
456 /*#define DEBUG_PROFILE*/
457 void color_apply_icc_profile(opj_image_t *image)
458 {
459     cmsHPROFILE in_prof, out_prof;
460     cmsHTRANSFORM transform;
461     cmsColorSpaceSignature in_space, out_space;
462     cmsUInt32Number intent, in_type, out_type;
463     int *r, *g, *b;
464     size_t nr_samples, i, max, max_w, max_h;
465     int prec, ok = 0;
466     OPJ_COLOR_SPACE new_space;
467
468     in_prof = cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
469 #ifdef DEBUG_PROFILE
470     FILE *icm = fopen("debug.icm", "wb");
471     fwrite(image->icc_profile_buf, 1, image->icc_profile_len, icm);
472     fclose(icm);
473 #endif
474
475     if (in_prof == NULL) {
476         return;
477     }
478
479     in_space = cmsGetPCS(in_prof);
480     out_space = cmsGetColorSpace(in_prof);
481     intent = cmsGetHeaderRenderingIntent(in_prof);
482
483
484     max_w = image->comps[0].w;
485     max_h = image->comps[0].h;
486     prec = (int)image->comps[0].prec;
487
488     if (out_space == cmsSigRgbData) { /* enumCS 16 */
489         unsigned int i, nr_comp = image->numcomps;
490
491         if (nr_comp > 4) {
492             nr_comp = 4;
493         }
494         for (i = 1; i < nr_comp; ++i) { /* AFL test */
495             if (image->comps[0].dx != image->comps[i].dx) {
496                 break;
497             }
498
499             if (image->comps[0].dy != image->comps[i].dy) {
500                 break;
501             }
502
503             if (image->comps[0].prec != image->comps[i].prec) {
504                 break;
505             }
506
507             if (image->comps[0].sgnd != image->comps[i].sgnd) {
508                 break;
509             }
510
511         }
512         if (i != nr_comp) {
513             cmsCloseProfile(in_prof);
514             return;
515         }
516
517         if (prec <= 8) {
518             in_type = TYPE_RGB_8;
519             out_type = TYPE_RGB_8;
520         } else {
521             in_type = TYPE_RGB_16;
522             out_type = TYPE_RGB_16;
523         }
524         out_prof = cmsCreate_sRGBProfile();
525         new_space = OPJ_CLRSPC_SRGB;
526     } else if (out_space == cmsSigGrayData) { /* enumCS 17 */
527         in_type = TYPE_GRAY_8;
528         out_type = TYPE_RGB_8;
529         out_prof = cmsCreate_sRGBProfile();
530         new_space = OPJ_CLRSPC_SRGB;
531     } else if (out_space == cmsSigYCbCrData) { /* enumCS 18 */
532         in_type = TYPE_YCbCr_16;
533         out_type = TYPE_RGB_16;
534         out_prof = cmsCreate_sRGBProfile();
535         new_space = OPJ_CLRSPC_SRGB;
536     } else {
537 #ifdef DEBUG_PROFILE
538         fprintf(stderr, "%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
539                 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
540                 __FILE__, __LINE__, out_space,
541                 (out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
542                 (out_space >> 8) & 0xff, out_space & 0xff);
543 #endif
544         cmsCloseProfile(in_prof);
545
546         return;
547     }
548     if (out_prof == NULL) {
549         cmsCloseProfile(in_prof);
550         return;
551     }
552
553 #ifdef DEBUG_PROFILE
554     fprintf(stderr,
555             "%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
556             "\n\tprofile: in(%p) out(%p)\n", __FILE__, __LINE__, image->numcomps, prec,
557             max_w, max_h, (void*)in_prof, (void*)out_prof);
558
559     fprintf(stderr, "\trender_intent (%u)\n\t"
560             "color_space: in(%#x)(%c%c%c%c)   out:(%#x)(%c%c%c%c)\n\t"
561             "       type: in(%u)              out:(%u)\n",
562             intent,
563             in_space,
564             (in_space >> 24) & 0xff, (in_space >> 16) & 0xff,
565             (in_space >> 8) & 0xff, in_space & 0xff,
566
567             out_space,
568             (out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
569             (out_space >> 8) & 0xff, out_space & 0xff,
570
571             in_type, out_type
572            );
573 #else
574     (void)prec;
575     (void)in_space;
576 #endif /* DEBUG_PROFILE */
577
578     transform = cmsCreateTransform(in_prof, in_type, out_prof, out_type, intent, 0);
579
580 #ifdef OPJ_HAVE_LIBLCMS2
581     /* Possible for: LCMS_VERSION >= 2000 :*/
582     cmsCloseProfile(in_prof);
583     cmsCloseProfile(out_prof);
584 #endif
585
586     if (transform == NULL) {
587 #ifdef DEBUG_PROFILE
588         fprintf(stderr, "%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
589                 "ICC Profile ignored.\n", __FILE__, __LINE__);
590 #endif
591
592 #ifdef OPJ_HAVE_LIBLCMS1
593         cmsCloseProfile(in_prof);
594         cmsCloseProfile(out_prof);
595 #endif
596         return;
597     }
598
599     if (image->numcomps > 2) { /* RGB, RGBA */
600         if ((image->comps[0].w == image->comps[1].w &&
601                 image->comps[0].w == image->comps[2].w) &&
602                 (image->comps[0].h == image->comps[1].h &&
603                  image->comps[0].h == image->comps[2].h)) {
604             if (prec <= 8) {
605                 unsigned char *inbuf, *outbuf, *in, *out;
606
607                 max = max_w * max_h;
608                 nr_samples = (size_t)(max * 3U * sizeof(unsigned char));
609                 in = inbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
610                 out = outbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
611
612                 if (inbuf == NULL || outbuf == NULL) {
613                     goto fails0;
614                 }
615
616                 r = image->comps[0].data;
617                 g = image->comps[1].data;
618                 b = image->comps[2].data;
619
620                 for (i = 0U; i < max; ++i) {
621                     *in++ = (unsigned char) * r++;
622                     *in++ = (unsigned char) * g++;
623                     *in++ = (unsigned char) * b++;
624                 }
625
626                 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
627
628                 r = image->comps[0].data;
629                 g = image->comps[1].data;
630                 b = image->comps[2].data;
631
632                 for (i = 0U; i < max; ++i) {
633                     *r++ = (int) * out++;
634                     *g++ = (int) * out++;
635                     *b++ = (int) * out++;
636                 }
637                 ok = 1;
638
639 fails0:
640                 opj_image_data_free(inbuf);
641                 opj_image_data_free(outbuf);
642             } else { /* prec > 8 */
643                 unsigned short *inbuf, *outbuf, *in, *out;
644
645                 max = max_w * max_h;
646                 nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
647                 in = inbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
648                 out = outbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
649
650                 if (inbuf == NULL || outbuf == NULL) {
651                     goto fails1;
652                 }
653
654                 r = image->comps[0].data;
655                 g = image->comps[1].data;
656                 b = image->comps[2].data;
657
658                 for (i = 0U  ; i < max; ++i) {
659                     *in++ = (unsigned short) * r++;
660                     *in++ = (unsigned short) * g++;
661                     *in++ = (unsigned short) * b++;
662                 }
663
664                 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
665
666                 r = image->comps[0].data;
667                 g = image->comps[1].data;
668                 b = image->comps[2].data;
669
670                 for (i = 0; i < max; ++i) {
671                     *r++ = (int) * out++;
672                     *g++ = (int) * out++;
673                     *b++ = (int) * out++;
674                 }
675                 ok = 1;
676
677 fails1:
678                 opj_image_data_free(inbuf);
679                 opj_image_data_free(outbuf);
680             }
681         } else {
682             fprintf(stderr,
683                     "[ERROR] Image components should have the same width and height\n");
684             cmsDeleteTransform(transform);
685             return;
686         }
687     } else { /* image->numcomps <= 2 : GRAY, GRAYA */
688         if (prec <= 8) {
689             unsigned char *in, *inbuf, *out, *outbuf;
690             opj_image_comp_t *new_comps;
691
692             max = max_w * max_h;
693             nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
694             in = inbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
695             out = outbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
696             g = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
697             b = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
698
699             if (inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) {
700                 goto fails2;
701             }
702
703             new_comps = (opj_image_comp_t*)realloc(image->comps,
704                                                    (image->numcomps + 2) * sizeof(opj_image_comp_t));
705
706             if (new_comps == NULL) {
707                 goto fails2;
708             }
709
710             image->comps = new_comps;
711
712             if (image->numcomps == 2) {
713                 image->comps[3] = image->comps[1];
714             }
715
716             image->comps[1] = image->comps[0];
717             image->comps[2] = image->comps[0];
718
719             image->comps[1].data = g;
720             image->comps[2].data = b;
721
722             image->numcomps += 2;
723
724             r = image->comps[0].data;
725
726             for (i = 0U; i < max; ++i) {
727                 *in++ = (unsigned char) * r++;
728             }
729             cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
730
731             r = image->comps[0].data;
732             g = image->comps[1].data;
733             b = image->comps[2].data;
734
735             for (i = 0U; i < max; ++i) {
736                 *r++ = (int) * out++;
737                 *g++ = (int) * out++;
738                 *b++ = (int) * out++;
739             }
740             r = g = b = NULL;
741             ok = 1;
742
743 fails2:
744             opj_image_data_free(inbuf);
745             opj_image_data_free(outbuf);
746             opj_image_data_free(g);
747             opj_image_data_free(b);
748         } else { /* prec > 8 */
749             unsigned short *in, *inbuf, *out, *outbuf;
750             opj_image_comp_t *new_comps;
751
752             max = max_w * max_h;
753             nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
754             in = inbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
755             out = outbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
756             g = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
757             b = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
758
759             if (inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) {
760                 goto fails3;
761             }
762
763             new_comps = (opj_image_comp_t*)realloc(image->comps,
764                                                    (image->numcomps + 2) * sizeof(opj_image_comp_t));
765
766             if (new_comps == NULL) {
767                 goto fails3;
768             }
769
770             image->comps = new_comps;
771
772             if (image->numcomps == 2) {
773                 image->comps[3] = image->comps[1];
774             }
775
776             image->comps[1] = image->comps[0];
777             image->comps[2] = image->comps[0];
778
779             image->comps[1].data = g;
780             image->comps[2].data = b;
781
782             image->numcomps += 2;
783
784             r = image->comps[0].data;
785
786             for (i = 0U; i < max; ++i) {
787                 *in++ = (unsigned short) * r++;
788             }
789             cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
790
791             r = image->comps[0].data;
792             g = image->comps[1].data;
793             b = image->comps[2].data;
794
795             for (i = 0; i < max; ++i) {
796                 *r++ = (int) * out++;
797                 *g++ = (int) * out++;
798                 *b++ = (int) * out++;
799             }
800             r = g = b = NULL;
801             ok = 1;
802
803 fails3:
804             opj_image_data_free(inbuf);
805             opj_image_data_free(outbuf);
806             opj_image_data_free(g);
807             opj_image_data_free(b);
808         }
809     }/* if(image->numcomps > 2) */
810
811     cmsDeleteTransform(transform);
812
813 #ifdef OPJ_HAVE_LIBLCMS1
814     cmsCloseProfile(in_prof);
815     cmsCloseProfile(out_prof);
816 #endif
817     if (ok) {
818         image->color_space = new_space;
819     }
820 }/* color_apply_icc_profile() */
821
822 static int are_comps_same_dimensions(opj_image_t * image)
823 {
824     unsigned int i;
825     for (i = 1; i < image->numcomps; i++) {
826         if (image->comps[0].dx != image->comps[i].dx ||
827                 image->comps[0].dy != image->comps[i].dy) {
828             return OPJ_FALSE;
829         }
830     }
831     return OPJ_TRUE;
832 }
833
834 void color_cielab_to_rgb(opj_image_t *image)
835 {
836     int *row;
837     int enumcs, numcomps;
838     OPJ_COLOR_SPACE new_space;
839
840     numcomps = (int)image->numcomps;
841
842     if (numcomps != 3) {
843         fprintf(stderr, "%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
844                 __FILE__, __LINE__, numcomps);
845         return;
846     }
847     if (!are_comps_same_dimensions(image)) {
848         fprintf(stderr,
849                 "%s:%d:\n\tcomponents are not all of the same dimension. Quitting.\n",
850                 __FILE__, __LINE__);
851         return;
852     }
853
854     row = (int*)image->icc_profile_buf;
855     enumcs = row[0];
856
857     if (enumcs == 14) { /* CIELab */
858         int *L, *a, *b, *red, *green, *blue;
859         int *src0, *src1, *src2, *dst0, *dst1, *dst2;
860         double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
861         double minL, maxL, mina, maxa, minb, maxb;
862         unsigned int default_type;
863         unsigned int i, max;
864         cmsHPROFILE in, out;
865         cmsHTRANSFORM transform;
866         cmsUInt16Number RGB[3];
867         cmsCIELab Lab;
868
869         in = cmsCreateLab4Profile(NULL);
870         if (in == NULL) {
871             return;
872         }
873         out = cmsCreate_sRGBProfile();
874         if (out == NULL) {
875             cmsCloseProfile(in);
876             return;
877         }
878         transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16,
879                                        INTENT_PERCEPTUAL, 0);
880
881 #ifdef OPJ_HAVE_LIBLCMS2
882         cmsCloseProfile(in);
883         cmsCloseProfile(out);
884 #endif
885         if (transform == NULL) {
886 #ifdef OPJ_HAVE_LIBLCMS1
887             cmsCloseProfile(in);
888             cmsCloseProfile(out);
889 #endif
890             return;
891         }
892         new_space = OPJ_CLRSPC_SRGB;
893
894         prec0 = (double)image->comps[0].prec;
895         prec1 = (double)image->comps[1].prec;
896         prec2 = (double)image->comps[2].prec;
897
898         default_type = (unsigned int)row[1];
899
900         if (default_type == 0x44454600) { /* DEF : default */
901             rl = 100;
902             ra = 170;
903             rb = 200;
904             ol = 0;
905             oa = pow(2, prec1 - 1);
906             ob = pow(2, prec2 - 2) +  pow(2, prec2 - 3);
907         } else {
908             rl = row[2];
909             ra = row[4];
910             rb = row[6];
911             ol = row[3];
912             oa = row[5];
913             ob = row[7];
914         }
915
916         L = src0 = image->comps[0].data;
917         a = src1 = image->comps[1].data;
918         b = src2 = image->comps[2].data;
919
920         max = image->comps[0].w * image->comps[0].h;
921
922         red = dst0 = (int*)opj_image_data_alloc(max * sizeof(int));
923         green = dst1 = (int*)opj_image_data_alloc(max * sizeof(int));
924         blue = dst2 = (int*)opj_image_data_alloc(max * sizeof(int));
925
926         if (red == NULL || green == NULL || blue == NULL) {
927             goto fails;
928         }
929
930         minL = -(rl * ol) / (pow(2, prec0) - 1);
931         maxL = minL + rl;
932
933         mina = -(ra * oa) / (pow(2, prec1) - 1);
934         maxa = mina + ra;
935
936         minb = -(rb * ob) / (pow(2, prec2) - 1);
937         maxb = minb + rb;
938
939         for (i = 0; i < max; ++i) {
940             Lab.L = minL + (double)(*L) * (maxL - minL) / (pow(2, prec0) - 1);
941             ++L;
942             Lab.a = mina + (double)(*a) * (maxa - mina) / (pow(2, prec1) - 1);
943             ++a;
944             Lab.b = minb + (double)(*b) * (maxb - minb) / (pow(2, prec2) - 1);
945             ++b;
946
947             cmsDoTransform(transform, &Lab, RGB, 1);
948
949             *red++ = RGB[0];
950             *green++ = RGB[1];
951             *blue++ = RGB[2];
952         }
953         cmsDeleteTransform(transform);
954 #ifdef OPJ_HAVE_LIBLCMS1
955         cmsCloseProfile(in);
956         cmsCloseProfile(out);
957 #endif
958         opj_image_data_free(src0);
959         image->comps[0].data = dst0;
960         opj_image_data_free(src1);
961         image->comps[1].data = dst1;
962         opj_image_data_free(src2);
963         image->comps[2].data = dst2;
964
965         image->color_space = new_space;
966         image->comps[0].prec = 16;
967         image->comps[1].prec = 16;
968         image->comps[2].prec = 16;
969
970         return;
971
972 fails:
973         cmsDeleteTransform(transform);
974 #ifdef OPJ_HAVE_LIBLCMS1
975         cmsCloseProfile(in);
976         cmsCloseProfile(out);
977 #endif
978         if (red) {
979             opj_image_data_free(red);
980         }
981         if (green) {
982             opj_image_data_free(green);
983         }
984         if (blue) {
985             opj_image_data_free(blue);
986         }
987         return;
988     }
989
990     fprintf(stderr, "%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,
991             __LINE__, enumcs);
992 }/* color_cielab_to_rgb() */
993
994 #endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */
995
996 void color_cmyk_to_rgb(opj_image_t *image)
997 {
998     float C, M, Y, K;
999     float sC, sM, sY, sK;
1000     unsigned int w, h, max, i;
1001
1002     w = image->comps[0].w;
1003     h = image->comps[0].h;
1004
1005     if (
1006         (image->numcomps < 4)
1007         || (image->comps[0].dx != image->comps[1].dx) ||
1008         (image->comps[0].dx != image->comps[2].dx) ||
1009         (image->comps[0].dx != image->comps[3].dx)
1010         || (image->comps[0].dy != image->comps[1].dy) ||
1011         (image->comps[0].dy != image->comps[2].dy) ||
1012         (image->comps[0].dy != image->comps[3].dy)
1013     ) {
1014         fprintf(stderr, "%s:%d:color_cmyk_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
1015                 __LINE__);
1016         return;
1017     }
1018
1019     max = w * h;
1020
1021     sC = 1.0F / (float)((1 << image->comps[0].prec) - 1);
1022     sM = 1.0F / (float)((1 << image->comps[1].prec) - 1);
1023     sY = 1.0F / (float)((1 << image->comps[2].prec) - 1);
1024     sK = 1.0F / (float)((1 << image->comps[3].prec) - 1);
1025
1026     for (i = 0; i < max; ++i) {
1027         /* CMYK values from 0 to 1 */
1028         C = (float)(image->comps[0].data[i]) * sC;
1029         M = (float)(image->comps[1].data[i]) * sM;
1030         Y = (float)(image->comps[2].data[i]) * sY;
1031         K = (float)(image->comps[3].data[i]) * sK;
1032
1033         /* Invert all CMYK values */
1034         C = 1.0F - C;
1035         M = 1.0F - M;
1036         Y = 1.0F - Y;
1037         K = 1.0F - K;
1038
1039         /* CMYK -> RGB : RGB results from 0 to 255 */
1040         image->comps[0].data[i] = (int)(255.0F * C * K); /* R */
1041         image->comps[1].data[i] = (int)(255.0F * M * K); /* G */
1042         image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */
1043     }
1044
1045     opj_image_data_free(image->comps[3].data);
1046     image->comps[3].data = NULL;
1047     image->comps[0].prec = 8;
1048     image->comps[1].prec = 8;
1049     image->comps[2].prec = 8;
1050     image->numcomps -= 1;
1051     image->color_space = OPJ_CLRSPC_SRGB;
1052
1053     for (i = 3; i < image->numcomps; ++i) {
1054         memcpy(&(image->comps[i]), &(image->comps[i + 1]), sizeof(image->comps[i]));
1055     }
1056
1057 }/* color_cmyk_to_rgb() */
1058
1059 /*
1060  * This code has been adopted from sjpx_openjpeg.c of ghostscript
1061  */
1062 void color_esycc_to_rgb(opj_image_t *image)
1063 {
1064     int y, cb, cr, sign1, sign2, val;
1065     unsigned int w, h, max, i;
1066     int flip_value = (1 << (image->comps[0].prec - 1));
1067     int max_value = (1 << image->comps[0].prec) - 1;
1068
1069     if (
1070         (image->numcomps < 3)
1071         || (image->comps[0].dx != image->comps[1].dx) ||
1072         (image->comps[0].dx != image->comps[2].dx)
1073         || (image->comps[0].dy != image->comps[1].dy) ||
1074         (image->comps[0].dy != image->comps[2].dy)
1075     ) {
1076         fprintf(stderr, "%s:%d:color_esycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
1077                 __LINE__);
1078         return;
1079     }
1080
1081     w = image->comps[0].w;
1082     h = image->comps[0].h;
1083
1084     sign1 = (int)image->comps[1].sgnd;
1085     sign2 = (int)image->comps[2].sgnd;
1086
1087     max = w * h;
1088
1089     for (i = 0; i < max; ++i) {
1090
1091         y = image->comps[0].data[i];
1092         cb = image->comps[1].data[i];
1093         cr = image->comps[2].data[i];
1094
1095         if (!sign1) {
1096             cb -= flip_value;
1097         }
1098         if (!sign2) {
1099             cr -= flip_value;
1100         }
1101
1102         val = (int)
1103               ((float)y - (float)0.0000368 * (float)cb
1104                + (float)1.40199 * (float)cr + (float)0.5);
1105
1106         if (val > max_value) {
1107             val = max_value;
1108         } else if (val < 0) {
1109             val = 0;
1110         }
1111         image->comps[0].data[i] = val;
1112
1113         val = (int)
1114               ((float)1.0003 * (float)y - (float)0.344125 * (float)cb
1115                - (float)0.7141128 * (float)cr + (float)0.5);
1116
1117         if (val > max_value) {
1118             val = max_value;
1119         } else if (val < 0) {
1120             val = 0;
1121         }
1122         image->comps[1].data[i] = val;
1123
1124         val = (int)
1125               ((float)0.999823 * (float)y + (float)1.77204 * (float)cb
1126                - (float)0.000008 * (float)cr + (float)0.5);
1127
1128         if (val > max_value) {
1129             val = max_value;
1130         } else if (val < 0) {
1131             val = 0;
1132         }
1133         image->comps[2].data[i] = val;
1134     }
1135     image->color_space = OPJ_CLRSPC_SRGB;
1136
1137 }/* color_esycc_to_rgb() */