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