color_apply_icc_profile: add checks on the number of components (#1236)
[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 < 3) { /* GRAY or GRAYA, not RGB or RGBA */
492             cmsCloseProfile(in_prof);
493             return;
494         }
495         if (nr_comp > 4) {
496             nr_comp = 4;
497         }
498         for (i = 1; i < nr_comp; ++i) { /* AFL test */
499             if (image->comps[0].dx != image->comps[i].dx) {
500                 break;
501             }
502
503             if (image->comps[0].dy != image->comps[i].dy) {
504                 break;
505             }
506
507             if (image->comps[0].prec != image->comps[i].prec) {
508                 break;
509             }
510
511             if (image->comps[0].sgnd != image->comps[i].sgnd) {
512                 break;
513             }
514
515         }
516         if (i != nr_comp) {
517             cmsCloseProfile(in_prof);
518             return;
519         }
520
521         if (prec <= 8) {
522             in_type = TYPE_RGB_8;
523             out_type = TYPE_RGB_8;
524         } else {
525             in_type = TYPE_RGB_16;
526             out_type = TYPE_RGB_16;
527         }
528         out_prof = cmsCreate_sRGBProfile();
529         new_space = OPJ_CLRSPC_SRGB;
530     } else if (out_space == cmsSigGrayData) { /* enumCS 17 */
531         in_type = TYPE_GRAY_8;
532         out_type = TYPE_RGB_8;
533         out_prof = cmsCreate_sRGBProfile();
534         new_space = OPJ_CLRSPC_SRGB;
535     } else if (out_space == cmsSigYCbCrData) { /* enumCS 18 */
536         if (image->numcomps < 3) {
537             cmsCloseProfile(in_prof);
538             return;
539         }
540         in_type = TYPE_YCbCr_16;
541         out_type = TYPE_RGB_16;
542         out_prof = cmsCreate_sRGBProfile();
543         new_space = OPJ_CLRSPC_SRGB;
544     } else {
545 #ifdef DEBUG_PROFILE
546         fprintf(stderr, "%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
547                 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
548                 __FILE__, __LINE__, out_space,
549                 (out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
550                 (out_space >> 8) & 0xff, out_space & 0xff);
551 #endif
552         cmsCloseProfile(in_prof);
553
554         return;
555     }
556     if (out_prof == NULL) {
557         cmsCloseProfile(in_prof);
558         return;
559     }
560
561 #ifdef DEBUG_PROFILE
562     fprintf(stderr,
563             "%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
564             "\n\tprofile: in(%p) out(%p)\n", __FILE__, __LINE__, image->numcomps, prec,
565             max_w, max_h, (void*)in_prof, (void*)out_prof);
566
567     fprintf(stderr, "\trender_intent (%u)\n\t"
568             "color_space: in(%#x)(%c%c%c%c)   out:(%#x)(%c%c%c%c)\n\t"
569             "       type: in(%u)              out:(%u)\n",
570             intent,
571             in_space,
572             (in_space >> 24) & 0xff, (in_space >> 16) & 0xff,
573             (in_space >> 8) & 0xff, in_space & 0xff,
574
575             out_space,
576             (out_space >> 24) & 0xff, (out_space >> 16) & 0xff,
577             (out_space >> 8) & 0xff, out_space & 0xff,
578
579             in_type, out_type
580            );
581 #else
582     (void)prec;
583     (void)in_space;
584 #endif /* DEBUG_PROFILE */
585
586     transform = cmsCreateTransform(in_prof, in_type, out_prof, out_type, intent, 0);
587
588 #ifdef OPJ_HAVE_LIBLCMS2
589     /* Possible for: LCMS_VERSION >= 2000 :*/
590     cmsCloseProfile(in_prof);
591     cmsCloseProfile(out_prof);
592 #endif
593
594     if (transform == NULL) {
595 #ifdef DEBUG_PROFILE
596         fprintf(stderr, "%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
597                 "ICC Profile ignored.\n", __FILE__, __LINE__);
598 #endif
599
600 #ifdef OPJ_HAVE_LIBLCMS1
601         cmsCloseProfile(in_prof);
602         cmsCloseProfile(out_prof);
603 #endif
604         return;
605     }
606
607     if (image->numcomps > 2) { /* RGB, RGBA */
608         if ((image->comps[0].w == image->comps[1].w &&
609                 image->comps[0].w == image->comps[2].w) &&
610                 (image->comps[0].h == image->comps[1].h &&
611                  image->comps[0].h == image->comps[2].h)) {
612             if (prec <= 8) {
613                 unsigned char *inbuf, *outbuf, *in, *out;
614
615                 max = max_w * max_h;
616                 nr_samples = (size_t)(max * 3U * sizeof(unsigned char));
617                 in = inbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
618                 out = outbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
619
620                 if (inbuf == NULL || outbuf == NULL) {
621                     goto fails0;
622                 }
623
624                 r = image->comps[0].data;
625                 g = image->comps[1].data;
626                 b = image->comps[2].data;
627
628                 for (i = 0U; i < max; ++i) {
629                     *in++ = (unsigned char) * r++;
630                     *in++ = (unsigned char) * g++;
631                     *in++ = (unsigned char) * b++;
632                 }
633
634                 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
635
636                 r = image->comps[0].data;
637                 g = image->comps[1].data;
638                 b = image->comps[2].data;
639
640                 for (i = 0U; i < max; ++i) {
641                     *r++ = (int) * out++;
642                     *g++ = (int) * out++;
643                     *b++ = (int) * out++;
644                 }
645                 ok = 1;
646
647 fails0:
648                 opj_image_data_free(inbuf);
649                 opj_image_data_free(outbuf);
650             } else { /* prec > 8 */
651                 unsigned short *inbuf, *outbuf, *in, *out;
652
653                 max = max_w * max_h;
654                 nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
655                 in = inbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
656                 out = outbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
657
658                 if (inbuf == NULL || outbuf == NULL) {
659                     goto fails1;
660                 }
661
662                 r = image->comps[0].data;
663                 g = image->comps[1].data;
664                 b = image->comps[2].data;
665
666                 for (i = 0U  ; i < max; ++i) {
667                     *in++ = (unsigned short) * r++;
668                     *in++ = (unsigned short) * g++;
669                     *in++ = (unsigned short) * b++;
670                 }
671
672                 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
673
674                 r = image->comps[0].data;
675                 g = image->comps[1].data;
676                 b = image->comps[2].data;
677
678                 for (i = 0; i < max; ++i) {
679                     *r++ = (int) * out++;
680                     *g++ = (int) * out++;
681                     *b++ = (int) * out++;
682                 }
683                 ok = 1;
684
685 fails1:
686                 opj_image_data_free(inbuf);
687                 opj_image_data_free(outbuf);
688             }
689         } else {
690             fprintf(stderr,
691                     "[ERROR] Image components should have the same width and height\n");
692             cmsDeleteTransform(transform);
693             return;
694         }
695     } else { /* image->numcomps <= 2 : GRAY, GRAYA */
696         if (prec <= 8) {
697             unsigned char *in, *inbuf, *out, *outbuf;
698             opj_image_comp_t *new_comps;
699
700             max = max_w * max_h;
701             nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
702             in = inbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
703             out = outbuf = (unsigned char*)opj_image_data_alloc(nr_samples);
704             g = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
705             b = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
706
707             if (inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) {
708                 goto fails2;
709             }
710
711             new_comps = (opj_image_comp_t*)realloc(image->comps,
712                                                    (image->numcomps + 2) * sizeof(opj_image_comp_t));
713
714             if (new_comps == NULL) {
715                 goto fails2;
716             }
717
718             image->comps = new_comps;
719
720             if (image->numcomps == 2) {
721                 image->comps[3] = image->comps[1];
722             }
723
724             image->comps[1] = image->comps[0];
725             image->comps[2] = image->comps[0];
726
727             image->comps[1].data = g;
728             image->comps[2].data = b;
729
730             image->numcomps += 2;
731
732             r = image->comps[0].data;
733
734             for (i = 0U; i < max; ++i) {
735                 *in++ = (unsigned char) * r++;
736             }
737             cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
738
739             r = image->comps[0].data;
740             g = image->comps[1].data;
741             b = image->comps[2].data;
742
743             for (i = 0U; i < max; ++i) {
744                 *r++ = (int) * out++;
745                 *g++ = (int) * out++;
746                 *b++ = (int) * out++;
747             }
748             r = g = b = NULL;
749             ok = 1;
750
751 fails2:
752             opj_image_data_free(inbuf);
753             opj_image_data_free(outbuf);
754             opj_image_data_free(g);
755             opj_image_data_free(b);
756         } else { /* prec > 8 */
757             unsigned short *in, *inbuf, *out, *outbuf;
758             opj_image_comp_t *new_comps;
759
760             max = max_w * max_h;
761             nr_samples = (size_t)(max * 3U * sizeof(unsigned short));
762             in = inbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
763             out = outbuf = (unsigned short*)opj_image_data_alloc(nr_samples);
764             g = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
765             b = (int*)opj_image_data_alloc((size_t)max * sizeof(int));
766
767             if (inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) {
768                 goto fails3;
769             }
770
771             new_comps = (opj_image_comp_t*)realloc(image->comps,
772                                                    (image->numcomps + 2) * sizeof(opj_image_comp_t));
773
774             if (new_comps == NULL) {
775                 goto fails3;
776             }
777
778             image->comps = new_comps;
779
780             if (image->numcomps == 2) {
781                 image->comps[3] = image->comps[1];
782             }
783
784             image->comps[1] = image->comps[0];
785             image->comps[2] = image->comps[0];
786
787             image->comps[1].data = g;
788             image->comps[2].data = b;
789
790             image->numcomps += 2;
791
792             r = image->comps[0].data;
793
794             for (i = 0U; i < max; ++i) {
795                 *in++ = (unsigned short) * r++;
796             }
797             cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
798
799             r = image->comps[0].data;
800             g = image->comps[1].data;
801             b = image->comps[2].data;
802
803             for (i = 0; i < max; ++i) {
804                 *r++ = (int) * out++;
805                 *g++ = (int) * out++;
806                 *b++ = (int) * out++;
807             }
808             r = g = b = NULL;
809             ok = 1;
810
811 fails3:
812             opj_image_data_free(inbuf);
813             opj_image_data_free(outbuf);
814             opj_image_data_free(g);
815             opj_image_data_free(b);
816         }
817     }/* if(image->numcomps > 2) */
818
819     cmsDeleteTransform(transform);
820
821 #ifdef OPJ_HAVE_LIBLCMS1
822     cmsCloseProfile(in_prof);
823     cmsCloseProfile(out_prof);
824 #endif
825     if (ok) {
826         image->color_space = new_space;
827     }
828 }/* color_apply_icc_profile() */
829
830 static int are_comps_same_dimensions(opj_image_t * image)
831 {
832     unsigned int i;
833     for (i = 1; i < image->numcomps; i++) {
834         if (image->comps[0].dx != image->comps[i].dx ||
835                 image->comps[0].dy != image->comps[i].dy) {
836             return OPJ_FALSE;
837         }
838     }
839     return OPJ_TRUE;
840 }
841
842 void color_cielab_to_rgb(opj_image_t *image)
843 {
844     int *row;
845     int enumcs, numcomps;
846     OPJ_COLOR_SPACE new_space;
847
848     numcomps = (int)image->numcomps;
849
850     if (numcomps != 3) {
851         fprintf(stderr, "%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
852                 __FILE__, __LINE__, numcomps);
853         return;
854     }
855     if (!are_comps_same_dimensions(image)) {
856         fprintf(stderr,
857                 "%s:%d:\n\tcomponents are not all of the same dimension. Quitting.\n",
858                 __FILE__, __LINE__);
859         return;
860     }
861
862     row = (int*)image->icc_profile_buf;
863     enumcs = row[0];
864
865     if (enumcs == 14) { /* CIELab */
866         int *L, *a, *b, *red, *green, *blue;
867         int *src0, *src1, *src2, *dst0, *dst1, *dst2;
868         double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
869         double minL, maxL, mina, maxa, minb, maxb;
870         unsigned int default_type;
871         unsigned int i, max;
872         cmsHPROFILE in, out;
873         cmsHTRANSFORM transform;
874         cmsUInt16Number RGB[3];
875         cmsCIELab Lab;
876
877         in = cmsCreateLab4Profile(NULL);
878         if (in == NULL) {
879             return;
880         }
881         out = cmsCreate_sRGBProfile();
882         if (out == NULL) {
883             cmsCloseProfile(in);
884             return;
885         }
886         transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16,
887                                        INTENT_PERCEPTUAL, 0);
888
889 #ifdef OPJ_HAVE_LIBLCMS2
890         cmsCloseProfile(in);
891         cmsCloseProfile(out);
892 #endif
893         if (transform == NULL) {
894 #ifdef OPJ_HAVE_LIBLCMS1
895             cmsCloseProfile(in);
896             cmsCloseProfile(out);
897 #endif
898             return;
899         }
900         new_space = OPJ_CLRSPC_SRGB;
901
902         prec0 = (double)image->comps[0].prec;
903         prec1 = (double)image->comps[1].prec;
904         prec2 = (double)image->comps[2].prec;
905
906         default_type = (unsigned int)row[1];
907
908         if (default_type == 0x44454600) { /* DEF : default */
909             rl = 100;
910             ra = 170;
911             rb = 200;
912             ol = 0;
913             oa = pow(2, prec1 - 1);
914             ob = pow(2, prec2 - 2) +  pow(2, prec2 - 3);
915         } else {
916             rl = row[2];
917             ra = row[4];
918             rb = row[6];
919             ol = row[3];
920             oa = row[5];
921             ob = row[7];
922         }
923
924         L = src0 = image->comps[0].data;
925         a = src1 = image->comps[1].data;
926         b = src2 = image->comps[2].data;
927
928         max = image->comps[0].w * image->comps[0].h;
929
930         red = dst0 = (int*)opj_image_data_alloc(max * sizeof(int));
931         green = dst1 = (int*)opj_image_data_alloc(max * sizeof(int));
932         blue = dst2 = (int*)opj_image_data_alloc(max * sizeof(int));
933
934         if (red == NULL || green == NULL || blue == NULL) {
935             goto fails;
936         }
937
938         minL = -(rl * ol) / (pow(2, prec0) - 1);
939         maxL = minL + rl;
940
941         mina = -(ra * oa) / (pow(2, prec1) - 1);
942         maxa = mina + ra;
943
944         minb = -(rb * ob) / (pow(2, prec2) - 1);
945         maxb = minb + rb;
946
947         for (i = 0; i < max; ++i) {
948             Lab.L = minL + (double)(*L) * (maxL - minL) / (pow(2, prec0) - 1);
949             ++L;
950             Lab.a = mina + (double)(*a) * (maxa - mina) / (pow(2, prec1) - 1);
951             ++a;
952             Lab.b = minb + (double)(*b) * (maxb - minb) / (pow(2, prec2) - 1);
953             ++b;
954
955             cmsDoTransform(transform, &Lab, RGB, 1);
956
957             *red++ = RGB[0];
958             *green++ = RGB[1];
959             *blue++ = RGB[2];
960         }
961         cmsDeleteTransform(transform);
962 #ifdef OPJ_HAVE_LIBLCMS1
963         cmsCloseProfile(in);
964         cmsCloseProfile(out);
965 #endif
966         opj_image_data_free(src0);
967         image->comps[0].data = dst0;
968         opj_image_data_free(src1);
969         image->comps[1].data = dst1;
970         opj_image_data_free(src2);
971         image->comps[2].data = dst2;
972
973         image->color_space = new_space;
974         image->comps[0].prec = 16;
975         image->comps[1].prec = 16;
976         image->comps[2].prec = 16;
977
978         return;
979
980 fails:
981         cmsDeleteTransform(transform);
982 #ifdef OPJ_HAVE_LIBLCMS1
983         cmsCloseProfile(in);
984         cmsCloseProfile(out);
985 #endif
986         if (red) {
987             opj_image_data_free(red);
988         }
989         if (green) {
990             opj_image_data_free(green);
991         }
992         if (blue) {
993             opj_image_data_free(blue);
994         }
995         return;
996     }
997
998     fprintf(stderr, "%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,
999             __LINE__, enumcs);
1000 }/* color_cielab_to_rgb() */
1001
1002 #endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */
1003
1004 void color_cmyk_to_rgb(opj_image_t *image)
1005 {
1006     float C, M, Y, K;
1007     float sC, sM, sY, sK;
1008     unsigned int w, h, max, i;
1009
1010     w = image->comps[0].w;
1011     h = image->comps[0].h;
1012
1013     if (
1014         (image->numcomps < 4)
1015         || (image->comps[0].dx != image->comps[1].dx) ||
1016         (image->comps[0].dx != image->comps[2].dx) ||
1017         (image->comps[0].dx != image->comps[3].dx)
1018         || (image->comps[0].dy != image->comps[1].dy) ||
1019         (image->comps[0].dy != image->comps[2].dy) ||
1020         (image->comps[0].dy != image->comps[3].dy)
1021     ) {
1022         fprintf(stderr, "%s:%d:color_cmyk_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
1023                 __LINE__);
1024         return;
1025     }
1026
1027     max = w * h;
1028
1029     sC = 1.0F / (float)((1 << image->comps[0].prec) - 1);
1030     sM = 1.0F / (float)((1 << image->comps[1].prec) - 1);
1031     sY = 1.0F / (float)((1 << image->comps[2].prec) - 1);
1032     sK = 1.0F / (float)((1 << image->comps[3].prec) - 1);
1033
1034     for (i = 0; i < max; ++i) {
1035         /* CMYK values from 0 to 1 */
1036         C = (float)(image->comps[0].data[i]) * sC;
1037         M = (float)(image->comps[1].data[i]) * sM;
1038         Y = (float)(image->comps[2].data[i]) * sY;
1039         K = (float)(image->comps[3].data[i]) * sK;
1040
1041         /* Invert all CMYK values */
1042         C = 1.0F - C;
1043         M = 1.0F - M;
1044         Y = 1.0F - Y;
1045         K = 1.0F - K;
1046
1047         /* CMYK -> RGB : RGB results from 0 to 255 */
1048         image->comps[0].data[i] = (int)(255.0F * C * K); /* R */
1049         image->comps[1].data[i] = (int)(255.0F * M * K); /* G */
1050         image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */
1051     }
1052
1053     opj_image_data_free(image->comps[3].data);
1054     image->comps[3].data = NULL;
1055     image->comps[0].prec = 8;
1056     image->comps[1].prec = 8;
1057     image->comps[2].prec = 8;
1058     image->numcomps -= 1;
1059     image->color_space = OPJ_CLRSPC_SRGB;
1060
1061     for (i = 3; i < image->numcomps; ++i) {
1062         memcpy(&(image->comps[i]), &(image->comps[i + 1]), sizeof(image->comps[i]));
1063     }
1064
1065 }/* color_cmyk_to_rgb() */
1066
1067 /*
1068  * This code has been adopted from sjpx_openjpeg.c of ghostscript
1069  */
1070 void color_esycc_to_rgb(opj_image_t *image)
1071 {
1072     int y, cb, cr, sign1, sign2, val;
1073     unsigned int w, h, max, i;
1074     int flip_value = (1 << (image->comps[0].prec - 1));
1075     int max_value = (1 << image->comps[0].prec) - 1;
1076
1077     if (
1078         (image->numcomps < 3)
1079         || (image->comps[0].dx != image->comps[1].dx) ||
1080         (image->comps[0].dx != image->comps[2].dx)
1081         || (image->comps[0].dy != image->comps[1].dy) ||
1082         (image->comps[0].dy != image->comps[2].dy)
1083     ) {
1084         fprintf(stderr, "%s:%d:color_esycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,
1085                 __LINE__);
1086         return;
1087     }
1088
1089     w = image->comps[0].w;
1090     h = image->comps[0].h;
1091
1092     sign1 = (int)image->comps[1].sgnd;
1093     sign2 = (int)image->comps[2].sgnd;
1094
1095     max = w * h;
1096
1097     for (i = 0; i < max; ++i) {
1098
1099         y = image->comps[0].data[i];
1100         cb = image->comps[1].data[i];
1101         cr = image->comps[2].data[i];
1102
1103         if (!sign1) {
1104             cb -= flip_value;
1105         }
1106         if (!sign2) {
1107             cr -= flip_value;
1108         }
1109
1110         val = (int)
1111               ((float)y - (float)0.0000368 * (float)cb
1112                + (float)1.40199 * (float)cr + (float)0.5);
1113
1114         if (val > max_value) {
1115             val = max_value;
1116         } else if (val < 0) {
1117             val = 0;
1118         }
1119         image->comps[0].data[i] = val;
1120
1121         val = (int)
1122               ((float)1.0003 * (float)y - (float)0.344125 * (float)cb
1123                - (float)0.7141128 * (float)cr + (float)0.5);
1124
1125         if (val > max_value) {
1126             val = max_value;
1127         } else if (val < 0) {
1128             val = 0;
1129         }
1130         image->comps[1].data[i] = val;
1131
1132         val = (int)
1133               ((float)0.999823 * (float)y + (float)1.77204 * (float)cb
1134                - (float)0.000008 * (float)cr + (float)0.5);
1135
1136         if (val > max_value) {
1137             val = max_value;
1138         } else if (val < 0) {
1139             val = 0;
1140         }
1141         image->comps[2].data[i] = val;
1142     }
1143     image->color_space = OPJ_CLRSPC_SRGB;
1144
1145 }/* color_esycc_to_rgb() */