48a1f8d016d0badec1802583b347cfb4fb59a771
[openjpeg.git] / libopenjpeg / jp2.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <opj_config.h>
32 #ifdef HAVE_LIBLCMS2
33 #include <math.h>
34 #include <lcms2.h>
35 #endif
36 #ifdef HAVE_LIBLCMS1
37 #include <math.h>
38 #include <lcms.h>
39 #endif
40 #include "opj_includes.h"
41 #include "opj_convert.h"
42
43 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
44 /*@{*/
45
46 /** @name Local static functions */
47 /*@{*/
48
49 /**
50 Read box headers
51 @param cinfo Codec context info
52 @param cio Input stream
53 @param box
54 @return Returns true if successful, returns false otherwise
55 */
56 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
57 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
58 /**
59 Read the IHDR box - Image Header box
60 @param jp2 JP2 handle
61 @param cio Input buffer stream
62 @return Returns true if successful, returns false otherwise
63 */
64 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
65 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
66 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
67 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
68 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
69 /**
70 Write the FTYP box - File type box
71 @param jp2 JP2 handle
72 @param cio Output buffer stream
73 */
74 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
75 /**
76 Read the FTYP box - File type box
77 @param jp2 JP2 handle
78 @param cio Input buffer stream
79 @return Returns true if successful, returns false otherwise
80 */
81 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
82 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
83 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
84 static void jp2_write_jp(opj_cio_t *cio);
85 /**
86 Read the JP box - JPEG 2000 signature
87 @param jp2 JP2 handle
88 @param cio Input buffer stream
89 @return Returns true if successful, returns false otherwise
90 */
91 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
92 /**
93 Decode the structure of a JP2 file
94 @param jp2 JP2 handle
95 @param cio Input buffer stream
96 @param ext Collector for profile, cdef and pclr data
97 @return Returns true if successful, returns false otherwise
98 */
99 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
100         struct extension *ext);
101 /**
102 Apply collected palette data
103 @param ext Collector for profile, cdef and pclr data
104 @param image 
105 */
106 static void jp2_apply_pclr(struct extension *ext, opj_image_t *image);
107 /**
108 Collect palette data
109 @param jp2 JP2 handle
110 @param cio Input buffer stream
111 @param box
112 @param ext Collector for profile, cdef and pclr data
113 @return Returns true if successful, returns false otherwise
114 */
115 static bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
116     opj_jp2_box_t *box, struct extension *ext);
117 /**
118 Collect component mapping data
119 @param jp2 JP2 handle
120 @param cio Input buffer stream
121 @param box
122 @param ext Collector for profile, cdef and pclr data
123 @return Returns true if successful, returns false otherwise
124 */
125 static bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
126     opj_jp2_box_t *box, struct extension *ext);
127 /**
128 Collect colour specification data
129 @param jp2 JP2 handle
130 @param cio Input buffer stream
131 @param box
132 @param ext Collector for profile, cdef and pclr data
133 @return Returns true if successful, returns false otherwise
134 */
135 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
136     opj_jp2_box_t *box, struct extension *ext);
137 /**
138 Apply ICC profile if Color Management System available
139 @param ext Collector for profile, cdef and pclr data
140 @param image
141 */
142 static void jp2_apply_profile(struct extension *ext, opj_image_t *image);
143 /*@}*/
144
145 /*@}*/
146
147 /* ----------------------------------------------------------------------- */
148
149 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
150         box->init_pos = cio_tell(cio);
151         box->length = cio_read(cio, 4);
152         box->type = cio_read(cio, 4);
153         if (box->length == 1) {
154                 if (cio_read(cio, 4) != 0) {
155                         opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
156                         return false;
157                 }
158                 box->length = cio_read(cio, 4);
159                 if (box->length == 0) 
160                         box->length = cio_numbytesleft(cio) + 12;
161         }
162         else if (box->length == 0) {
163                 box->length = cio_numbytesleft(cio) + 8;
164         }
165         
166         return true;
167 }
168
169 #if 0
170 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
171         unsigned int i;
172         opj_jp2_box_t box;
173
174         box.init_pos = cio_tell(cio);
175         cio_skip(cio, 4);
176         cio_write(cio, JP2_URL, 4);     /* DBTL */
177         cio_write(cio, 0, 1);           /* VERS */
178         cio_write(cio, 0, 3);           /* FLAG */
179
180         if(Idx_file) {
181                 for (i = 0; i < strlen(Idx_file); i++) {
182                         cio_write(cio, Idx_file[i], 1);
183                 }
184         }
185
186         box.length = cio_tell(cio) - box.init_pos;
187         cio_seek(cio, box.init_pos);
188         cio_write(cio, box.length, 4);  /* L */
189         cio_seek(cio, box.init_pos + box.length);
190 }
191 #endif
192
193 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
194         opj_jp2_box_t box;
195
196         opj_common_ptr cinfo = jp2->cinfo;
197
198         jp2_read_boxhdr(cinfo, cio, &box);
199         if (JP2_IHDR != box.type) {
200                 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
201                 return false;
202         }
203
204         jp2->h = cio_read(cio, 4);                      /* HEIGHT */
205         jp2->w = cio_read(cio, 4);                      /* WIDTH */
206         jp2->numcomps = cio_read(cio, 2);       /* NC */
207         jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
208
209         jp2->bpc = cio_read(cio, 1);            /* BPC */
210
211         jp2->C = cio_read(cio, 1);                      /* C */
212         jp2->UnkC = cio_read(cio, 1);           /* UnkC */
213         jp2->IPR = cio_read(cio, 1);            /* IPR */
214
215         if (cio_tell(cio) - box.init_pos != box.length) {
216                 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
217                 return false;
218         }
219
220         return true;
221 }
222
223 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
224         opj_jp2_box_t box;
225
226         box.init_pos = cio_tell(cio);
227         cio_skip(cio, 4);
228         cio_write(cio, JP2_IHDR, 4);            /* IHDR */
229
230         cio_write(cio, jp2->h, 4);                      /* HEIGHT */
231         cio_write(cio, jp2->w, 4);                      /* WIDTH */
232         cio_write(cio, jp2->numcomps, 2);       /* NC */
233
234         cio_write(cio, jp2->bpc, 1);            /* BPC */
235
236         cio_write(cio, jp2->C, 1);                      /* C : Always 7 */
237         cio_write(cio, jp2->UnkC, 1);           /* UnkC, colorspace unknown */
238         cio_write(cio, jp2->IPR, 1);            /* IPR, no intellectual property */
239
240         box.length = cio_tell(cio) - box.init_pos;
241         cio_seek(cio, box.init_pos);
242         cio_write(cio, box.length, 4);  /* L */
243         cio_seek(cio, box.init_pos + box.length);
244 }
245
246 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
247         unsigned int i;
248         opj_jp2_box_t box;
249
250         box.init_pos = cio_tell(cio);
251         cio_skip(cio, 4);
252         cio_write(cio, JP2_BPCC, 4);    /* BPCC */
253
254         for (i = 0; i < jp2->numcomps; i++) {
255                 cio_write(cio, jp2->comps[i].bpcc, 1);
256         }
257
258         box.length = cio_tell(cio) - box.init_pos;
259         cio_seek(cio, box.init_pos);
260         cio_write(cio, box.length, 4);  /* L */
261         cio_seek(cio, box.init_pos + box.length);
262 }
263
264
265 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
266         unsigned int i;
267         opj_jp2_box_t box;
268
269         opj_common_ptr cinfo = jp2->cinfo;
270
271         jp2_read_boxhdr(cinfo, cio, &box);
272         if (JP2_BPCC != box.type) {
273                 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
274                 return false;
275         }
276
277         for (i = 0; i < jp2->numcomps; i++) {
278                 jp2->comps[i].bpcc = cio_read(cio, 1);
279         }
280
281         if (cio_tell(cio) - box.init_pos != box.length) {
282                 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
283                 return false;
284         }
285
286         return true;
287 }
288
289 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
290         opj_jp2_box_t box;
291
292         box.init_pos = cio_tell(cio);
293         cio_skip(cio, 4);
294         cio_write(cio, JP2_COLR, 4);            /* COLR */
295
296         cio_write(cio, jp2->meth, 1);           /* METH */
297         cio_write(cio, jp2->precedence, 1);     /* PRECEDENCE */
298         cio_write(cio, jp2->approx, 1);         /* APPROX */
299
300         if (jp2->meth == 1) {
301                 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
302         } else {
303                 cio_write(cio, 0, 1);                   /* PROFILE (??) */
304         }
305
306         box.length = cio_tell(cio) - box.init_pos;
307         cio_seek(cio, box.init_pos);
308         cio_write(cio, box.length, 4);  /* L */
309         cio_seek(cio, box.init_pos + box.length);
310 }
311
312 static void jp2_free_pclr(struct extension *ext)
313 {
314     opj_free(ext->jp2_pclr->channel_sign);
315     opj_free(ext->jp2_pclr->channel_size);
316     opj_free(ext->jp2_pclr->entries);
317
318         if(ext->jp2_pclr->cmap) opj_free(ext->jp2_pclr->cmap);
319
320     opj_free(ext->jp2_pclr); ext->jp2_pclr = NULL;
321 }
322
323 static void free_ext_data(struct extension *ext)
324 {
325         if(ext->jp2_pclr)
326    {
327         jp2_free_pclr(ext);
328    }
329         if(ext->jp2_cdef) 
330    {
331         if(ext->jp2_cdef->info) opj_free(ext->jp2_cdef->info);
332         opj_free(ext->jp2_cdef);
333    }
334         if(ext->jp2_profile_buf) opj_free(ext->jp2_profile_buf);
335 }
336
337 static void jp2_apply_pclr(struct extension *ext, opj_image_t *image)
338 {
339         opj_image_comp_t *old_comps, *new_comps;
340         unsigned char *channel_size, *channel_sign;
341         unsigned int *entries;
342         opj_jp2_cmap_comp_t *cmap;
343         int *src, *dst;
344         unsigned int j, max;
345         unsigned short i, nr_channels, cmp, pcol;
346         int k, top_k;
347
348         channel_size = ext->jp2_pclr->channel_size;
349         channel_sign = ext->jp2_pclr->channel_sign;
350         entries = ext->jp2_pclr->entries;
351         cmap = ext->jp2_pclr->cmap;
352         nr_channels = ext->jp2_pclr->nr_channels;
353
354         old_comps = image->comps;
355         new_comps = (opj_image_comp_t*)
356          opj_malloc(nr_channels * sizeof(opj_image_comp_t));
357
358         for(i = 0; i < nr_channels; ++i)
359    {
360         pcol = cmap[i].pcol; cmp = cmap[i].cmp;
361
362         new_comps[pcol] = old_comps[cmp];
363
364         if(cmap[i].mtyp == 0) /* Direct use */
365   {
366         old_comps[cmp].data = NULL; continue;
367   }
368 /* Palette mapping: */
369         new_comps[pcol].data = (int*)
370          opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
371         new_comps[pcol].prec = channel_size[i];
372         new_comps[pcol].sgnd = channel_sign[i];
373    }
374         top_k = ext->jp2_pclr->nr_entries - 1;
375
376         for(i = 0; i < nr_channels; ++i)
377    {
378 /* Direct use: */
379         if(cmap[i].mtyp == 0) continue;
380
381 /* Palette mapping: */
382         cmp = cmap[i].cmp; pcol = cmap[i].pcol;
383         src = old_comps[cmp].data; 
384         dst = new_comps[pcol].data;
385         max = new_comps[pcol].w * new_comps[pcol].h;
386
387         for(j = 0; j < max; ++j)
388   {
389 /* The index */
390         if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
391 /* The colour */
392         dst[j] = entries[k * nr_channels + pcol];
393   }
394    }
395         max = image->numcomps;
396         for(i = 0; i < max; ++i)
397    {
398         if(old_comps[i].data) opj_free(old_comps[i].data);
399    }
400         opj_free(old_comps);
401         image->comps = new_comps;
402         image->numcomps = nr_channels;
403
404         jp2_free_pclr(ext);
405
406 }/* apply_pclr() */
407
408 static bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
409         opj_jp2_box_t *box, struct extension *ext)
410 {
411         opj_jp2_pclr_t *jp2_pclr;
412         unsigned char *channel_size, *channel_sign;
413         unsigned int *entries;
414         unsigned short nr_entries, nr_channels;
415         unsigned short i, j;
416         unsigned char uc;
417
418 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
419  * a JP2 Header box' :
420 */
421         if(ext->jp2_pclr) return false;
422
423         nr_entries = cio_read(cio, 2); /* NE */
424         nr_channels = cio_read(cio, 1);/* NPC */
425
426         entries = (unsigned int*)
427          opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
428         channel_size = (unsigned char*)opj_malloc(nr_channels);
429         channel_sign = (unsigned char*)opj_malloc(nr_channels);
430
431         jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
432         jp2_pclr->channel_sign = channel_sign;
433         jp2_pclr->channel_size = channel_size;
434         jp2_pclr->entries = entries;
435         jp2_pclr->nr_entries = nr_entries;
436         jp2_pclr->nr_channels = nr_channels;
437         jp2_pclr->cmap = NULL;
438
439         ext->jp2_pclr = jp2_pclr;
440
441         for(i = 0; i < nr_channels; ++i)
442    {
443         uc = cio_read(cio, 1); /* Bi */
444         channel_size[i] = (uc & 0x7f) + 1;
445         channel_sign[i] = (uc & 0x80)?1:0;
446    }
447
448         for(j = 0; j < nr_entries; ++j)
449    {
450         for(i = 0; i < nr_channels; ++i)
451   {
452 /* Cji */
453         *entries++ = cio_read(cio, channel_size[i]>>3);
454   }
455    }
456
457         return true;
458 }/* jp2_read_pclr() */
459
460 static bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
461         opj_jp2_box_t *box, struct extension *ext)
462 {
463         opj_jp2_cmap_comp_t *cmap;
464         unsigned short i, nr_channels;
465
466 /* Need nr_channels: */
467         if(ext->jp2_pclr == NULL) return false;
468
469 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
470  * inside a JP2 Header box' :
471 */
472         if(ext->jp2_pclr->cmap) return false;
473
474         nr_channels = ext->jp2_pclr->nr_channels;
475         cmap = (opj_jp2_cmap_comp_t*)
476          opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
477
478         for(i = 0; i < nr_channels; ++i)
479    {
480         cmap[i].cmp = cio_read(cio, 2);
481         cmap[i].mtyp = cio_read(cio, 1);
482         cmap[i].pcol = cio_read(cio, 1);
483
484    }
485         ext->jp2_pclr->cmap = cmap;
486
487         return true;
488 }/* jp2_read_cmap() */
489
490 static void jp2_apply_cdef(opj_image_t *image, struct extension *ext)
491 {
492         opj_jp2_cdef_info_t *info;
493         int color_space;
494         unsigned short i, n, cn, typ, asoc, acn;
495
496         color_space = image->color_space;
497         info = ext->jp2_cdef->info;
498         n = ext->jp2_cdef->n;
499
500         for(i = 0; i < n; ++i)
501    {
502 /* WATCH: acn = asoc - 1 ! */
503         if((asoc = info[i].asoc) == 0) continue;
504
505         cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
506
507         if(cn != acn)
508   {
509         opj_image_comp_t saved;
510
511         memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
512         memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
513         memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
514
515         info[i].asoc = cn + 1;
516         info[acn].asoc = info[acn].cn + 1;
517   }
518    }
519         if(ext->jp2_cdef->info) opj_free(ext->jp2_cdef->info);
520
521         opj_free(ext->jp2_cdef); ext->jp2_cdef = NULL;
522
523 }/* jp2_apply_cdef() */
524
525 static bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
526         opj_jp2_box_t *box, struct extension *ext)
527 {
528         opj_jp2_cdef_info_t *info;
529         unsigned short i, n;
530
531 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
532  * inside a JP2 Header box.' 
533 */
534         if(ext->jp2_cdef) return false;
535
536         if((n = cio_read(cio, 2)) == 0) return false; /* szukw000: FIXME */
537
538         info = (opj_jp2_cdef_info_t*)
539          opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
540
541         ext->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
542         ext->jp2_cdef->info = info;
543         ext->jp2_cdef->n = n;
544
545         for(i = 0; i < n; ++i)
546    {
547         info[i].cn = cio_read(cio, 2);
548         info[i].typ = cio_read(cio, 2);
549         info[i].asoc = cio_read(cio, 2);
550
551    }
552         return true;
553 }/* jp2_read_cdef() */
554
555 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
556         opj_jp2_box_t *box, struct extension *ext) 
557 {
558         int skip_len;
559     opj_common_ptr cinfo;
560
561 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
562  * Specification boxes after the first.' 
563 */
564         if(ext->jp2_has_colr) return false;
565
566         cinfo = jp2->cinfo;
567
568         jp2->meth = cio_read(cio, 1);           /* METH */
569         jp2->precedence = cio_read(cio, 1);     /* PRECEDENCE */
570         jp2->approx = cio_read(cio, 1);         /* APPROX */
571
572         if (jp2->meth == 1) 
573    {
574         jp2->enumcs = cio_read(cio, 4); /* EnumCS */
575    } 
576         else 
577    {
578 /* skip PROFILE */
579         skip_len = box->init_pos + box->length - cio_tell(cio);
580         if (skip_len < 0) 
581   {
582         opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
583         return false;
584   }
585         if(skip_len > 0)
586   {
587         unsigned char *start;
588
589         start = cio_getbp(cio);
590         ext->jp2_profile_buf = (unsigned char*)opj_malloc(skip_len);
591         ext->jp2_profile_len = skip_len;
592
593         cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
594
595         memcpy(ext->jp2_profile_buf, start, skip_len);
596   }
597    }
598
599         if (cio_tell(cio) - box->init_pos != box->length) 
600    {
601         opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
602         return false;
603    }
604         ext->jp2_has_colr = 1;
605
606         return true;
607 }/* jp2_read_colr() */
608
609 #if defined(HAVE_LIBLCMS2) || defined(HAVE_LIBLCMS1)
610 #ifdef HAVE_LIBLCMS1
611 /* Bob Friesenhahn proposed:*/
612 #define cmsSigXYZData   icSigXYZData
613 #define cmsSigLabData   icSigLabData
614 #define cmsSigCmykData  icSigCmykData
615 #define cmsSigYCbCrData icSigYCbCrData
616 #define cmsSigLuvData   icSigLuvData
617 #define cmsSigGrayData  icSigGrayData
618 #define cmsSigRgbData   icSigRgbData
619 #define cmsUInt32Number DWORD
620
621 #define cmsColorSpaceSignature icColorSpaceSignature
622 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
623
624 #endif /* HAVE_LIBLCMS1 */
625
626 static void jp2_apply_profile(struct extension *ext, opj_image_t *image)
627 {
628         cmsHPROFILE in_prof, out_prof;
629         cmsHTRANSFORM transform;
630         cmsColorSpaceSignature in_space, out_space;
631         cmsUInt32Number intent, in_type, out_type, nr_samples;
632         int *r, *g, *b;
633         int prec, i, max, max_w, max_h;
634         OPJ_COLOR_SPACE oldspace;
635
636         in_prof = 
637          cmsOpenProfileFromMem(ext->jp2_profile_buf, ext->jp2_profile_len);
638         in_space = cmsGetPCS(in_prof);
639         out_space = cmsGetColorSpace(in_prof);
640         intent = cmsGetHeaderRenderingIntent(in_prof);
641
642         
643         max_w = image->comps[0].w; max_h = image->comps[0].h;
644         prec = image->comps[0].prec;
645         oldspace = image->color_space;
646
647         if(out_space == cmsSigRgbData) /* enumCS 16 */
648    {
649         in_type = TYPE_RGB_16;
650         out_type = TYPE_RGB_16;
651         out_prof = cmsCreate_sRGBProfile();
652         image->color_space = CLRSPC_SRGB;
653    }
654         else
655         if(out_space == cmsSigGrayData) /* enumCS 17 */
656    {
657         in_type = TYPE_GRAY_8;
658         out_type = TYPE_RGB_8;
659         out_prof = cmsCreate_sRGBProfile();
660         image->color_space = CLRSPC_SRGB;
661    }
662         else
663         if(out_space == cmsSigYCbCrData) /* enumCS 18 */
664    {
665         in_type = TYPE_YCbCr_16;
666         out_type = TYPE_RGB_16;
667         out_prof = cmsCreate_sRGBProfile();
668         image->color_space = CLRSPC_SRGB;
669    }
670         else
671    {
672 #ifdef DEBUG_PROFILE
673 fprintf(stderr,"%s:%d: jp2_apply_profile\n\tICC Profile has unknown "
674 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
675 __FILE__,__LINE__,out_space,
676 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
677 (out_space>>8) & 0xff, out_space & 0xff);
678 #endif
679         return;
680    }
681
682 #ifdef DEBUG_PROFILE
683 fprintf(stderr,"%s:%d:jp2_apply_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
684 "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
685 max_w,max_h, (void*)in_prof,(void*)out_prof);
686
687 fprintf(stderr,"\trender_intent (%u)\n\t"
688 "color_space: in(%#x)(%c%c%c%c)   out:(%#x)(%c%c%c%c)\n\t"
689 "       type: in(%u)              out:(%u)\n",
690 intent,
691 in_space,
692 (in_space>>24) & 0xff,(in_space>>16) & 0xff,
693 (in_space>>8) & 0xff, in_space & 0xff,
694
695 out_space,
696 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
697 (out_space>>8) & 0xff, out_space & 0xff,
698
699 in_type,out_type
700  );
701 #endif /* DEBUG_PROFILE */
702
703         transform = cmsCreateTransform(in_prof, in_type,
704          out_prof, out_type, intent, 0);
705
706 #ifdef HAVE_LIBLCMS2
707 /* Possible for: LCMS_VERSION >= 2000 :*/
708         cmsCloseProfile(in_prof);
709         cmsCloseProfile(out_prof);
710 #endif
711
712         if(transform == NULL)
713    {
714 #ifdef DEBUG_PROFILE
715 fprintf(stderr,"%s:%d:jp2_apply_profile\n\tcmsCreateTransform failed. "
716 "ICC Profile ignored.\n",__FILE__,__LINE__);
717 #endif
718         image->color_space = oldspace;
719 #ifdef HAVE_LIBLCMS1
720         cmsCloseProfile(in_prof);
721         cmsCloseProfile(out_prof);
722 #endif
723         return;
724    }
725
726         if(image->numcomps > 2)/* RGB, RGBA */
727    {
728         unsigned short *inbuf, *outbuf, *in, *out;
729         max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned short);
730         in = inbuf = (unsigned short*)opj_malloc(nr_samples);
731         out = outbuf = (unsigned short*)opj_malloc(nr_samples);
732
733         r = image->comps[0].data;
734         g = image->comps[1].data;
735         b = image->comps[2].data;
736
737         for(i = 0; i < max; ++i)
738   {
739         *in++ = (unsigned short)*r++;
740         *in++ = (unsigned short)*g++;
741         *in++ = (unsigned short)*b++;
742   }
743
744         cmsDoTransform(transform, inbuf, outbuf, max);
745
746         r = image->comps[0].data;
747         g = image->comps[1].data;
748         b = image->comps[2].data;
749
750         for(i = 0; i < max; ++i)
751   {
752         *r++ = (int)*out++;
753         *g++ = (int)*out++;
754         *b++ = (int)*out++;
755   }
756         opj_free(inbuf); opj_free(outbuf);
757    }
758         else /* GRAY, GRAYA */
759    {
760         unsigned char *in, *inbuf, *out, *outbuf;
761
762         max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned char);
763         in = inbuf = (unsigned char*)opj_malloc(nr_samples);
764         out = outbuf = (unsigned char*)opj_malloc(nr_samples);
765
766         image->comps = (opj_image_comp_t*)
767          opj_realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
768
769         if(image->numcomps == 2)
770          image->comps[3] = image->comps[1];
771
772         image->comps[1] = image->comps[0];
773         image->comps[2] = image->comps[0];
774
775         image->comps[1].data = (int*)opj_calloc(max, sizeof(int));
776         image->comps[2].data = (int*)opj_calloc(max, sizeof(int));
777
778         image->numcomps += 2;
779
780         r = image->comps[0].data;
781
782         for(i = 0; i < max; ++i)
783   {
784         *in++ = (unsigned char)*r++;
785   }
786         cmsDoTransform(transform, inbuf, outbuf, max);
787
788         r = image->comps[0].data;
789         g = image->comps[1].data;
790         b = image->comps[2].data;
791
792         for(i = 0; i < max; ++i)
793   {
794         *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
795   }
796         opj_free(inbuf); opj_free(outbuf);
797
798    }/* if(image->numcomps */
799
800         cmsDeleteTransform(transform);
801
802 #ifdef HAVE_LIBLCMS1
803         cmsCloseProfile(in_prof);
804         cmsCloseProfile(out_prof);
805 #endif
806 }/* jp2_apply_profile() */
807
808 #endif /* HAVE_LIBLCMS2 || HAVE_LIBLCMS1 */
809
810 bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, struct extension *ext) 
811 {
812         opj_jp2_box_t box;
813         unsigned int jp2h_end;
814
815         opj_common_ptr cinfo = jp2->cinfo;
816
817         jp2_read_boxhdr(cinfo, cio, &box);
818         do 
819    {
820         if (JP2_JP2H != box.type) 
821   {
822         if (box.type == JP2_JP2C) 
823  {
824         opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
825         return false;
826  }
827         cio_skip(cio, box.length - 8);
828
829         if(cio->bp >= cio->end) return false;
830
831         jp2_read_boxhdr(cinfo, cio, &box);
832   }
833    } while(JP2_JP2H != box.type);
834
835         if (!jp2_read_ihdr(jp2, cio))
836                 return false;
837         jp2h_end = box.init_pos + box.length;
838
839         if (jp2->bpc == 255) 
840    {
841         if (!jp2_read_bpcc(jp2, cio))
842                 return false;
843    }
844         jp2_read_boxhdr(cinfo, cio, &box);
845
846         while(cio_tell(cio) < jp2h_end)
847    {
848         if(box.type == JP2_COLR)
849   {
850         if( !jp2_read_colr(jp2, cio, &box, ext))
851  {
852     cio_seek(cio, box.init_pos + 8);
853     cio_skip(cio, box.length - 8);
854  }
855     jp2_read_boxhdr(cinfo, cio, &box);
856     continue;
857   }
858     if(box.type == JP2_CDEF)
859   {
860     if( !jp2_read_cdef(jp2, cio, &box, ext))
861  {
862     cio_seek(cio, box.init_pos + 8);
863     cio_skip(cio, box.length - 8);
864  }
865     jp2_read_boxhdr(cinfo, cio, &box);
866     continue;
867   }
868     if(box.type == JP2_PCLR)
869   {
870     if( !jp2_read_pclr(jp2, cio, &box, ext))
871  {
872     cio_seek(cio, box.init_pos + 8);
873     cio_skip(cio, box.length - 8);
874  }
875     jp2_read_boxhdr(cinfo, cio, &box);
876     continue;
877   }
878     if(box.type == JP2_CMAP)
879   {
880     if( !jp2_read_cmap(jp2, cio, &box, ext))
881  {
882     cio_seek(cio, box.init_pos + 8);
883     cio_skip(cio, box.length - 8);
884  }
885     jp2_read_boxhdr(cinfo, cio, &box);
886     continue;
887   }
888         cio_seek(cio, box.init_pos + 8);
889         cio_skip(cio, box.length - 8);
890         jp2_read_boxhdr(cinfo, cio, &box);
891
892    }/* while(cio_tell(cio) < box_end) */
893
894         cio_seek(cio, jp2h_end);
895
896 /* Part 1, I.5.3.3 : 'must contain at least one' */
897         return (ext->jp2_has_colr == 1);
898
899 }/* jp2_read_jp2h() */
900
901 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio, 
902         opj_codestream_info_t *cstr_info) 
903 {
904         opj_common_ptr cinfo;
905         opj_image_t *image = NULL;
906         struct extension ext;
907
908         if(!jp2 || !cio) 
909    {
910         return NULL;
911    }
912         memset(&ext, 0, sizeof(struct extension));
913         cinfo = jp2->cinfo;
914
915 /* JP2 decoding */
916         if(!jp2_read_struct(jp2, cio, &ext)) 
917    {
918         free_ext_data(&ext);
919         opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
920         return NULL;
921    }
922
923 /* J2K decoding */
924         image = j2k_decode(jp2->j2k, cio, cstr_info);
925
926         if(!image) 
927    {
928         free_ext_data(&ext);
929         opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
930         return NULL;
931    }
932
933 /* Set Image Color Space */
934         if (jp2->enumcs == 16)
935                 image->color_space = CLRSPC_SRGB;
936         else if (jp2->enumcs == 17)
937                 image->color_space = CLRSPC_GRAY;
938         else if (jp2->enumcs == 18)
939                 image->color_space = CLRSPC_SYCC;
940         else
941                 image->color_space = CLRSPC_UNKNOWN;
942
943         if(ext.jp2_cdef)
944    {
945         jp2_apply_cdef(image, &ext);
946    }
947         if(ext.jp2_pclr)
948    {
949 /* Part 1, I.5.3.4: Either both or none : */
950         if( !ext.jp2_pclr->cmap) 
951          jp2_free_pclr(&ext);
952         else
953          jp2_apply_pclr(&ext, image);
954    }
955         if(ext.jp2_profile_buf)
956    {
957 #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) 
958         jp2_apply_profile(&ext, image);
959 #endif
960         opj_free(ext.jp2_profile_buf);
961    }
962         else
963         if(image->color_space == CLRSPC_SYCC)
964    {
965         opj_convert_sycc_to_rgb(image);
966    }
967         return image;
968
969 }/* jp2_decode() */
970
971
972 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
973         opj_jp2_box_t box;
974
975         box.init_pos = cio_tell(cio);
976         cio_skip(cio, 4);
977         cio_write(cio, JP2_JP2H, 4);    /* JP2H */
978
979         jp2_write_ihdr(jp2, cio);
980
981         if (jp2->bpc == 255) {
982                 jp2_write_bpcc(jp2, cio);
983         }
984         jp2_write_colr(jp2, cio);
985
986         box.length = cio_tell(cio) - box.init_pos;
987         cio_seek(cio, box.init_pos);
988         cio_write(cio, box.length, 4);  /* L */
989         cio_seek(cio, box.init_pos + box.length);
990 }
991
992 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
993         unsigned int i;
994         opj_jp2_box_t box;
995
996         box.init_pos = cio_tell(cio);
997         cio_skip(cio, 4);
998         cio_write(cio, JP2_FTYP, 4);            /* FTYP */
999
1000         cio_write(cio, jp2->brand, 4);          /* BR */
1001         cio_write(cio, jp2->minversion, 4);     /* MinV */
1002
1003         for (i = 0; i < jp2->numcl; i++) {
1004                 cio_write(cio, jp2->cl[i], 4);  /* CL */
1005         }
1006
1007         box.length = cio_tell(cio) - box.init_pos;
1008         cio_seek(cio, box.init_pos);
1009         cio_write(cio, box.length, 4);  /* L */
1010         cio_seek(cio, box.init_pos + box.length);
1011 }
1012
1013 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1014         int i;
1015         opj_jp2_box_t box;
1016
1017         opj_common_ptr cinfo = jp2->cinfo;
1018
1019         jp2_read_boxhdr(cinfo, cio, &box);
1020
1021         if (JP2_FTYP != box.type) {
1022                 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
1023                 return false;
1024         }
1025
1026         jp2->brand = cio_read(cio, 4);          /* BR */
1027         jp2->minversion = cio_read(cio, 4);     /* MinV */
1028         jp2->numcl = (box.length - 16) / 4;
1029         jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
1030
1031         for (i = 0; i < (int)jp2->numcl; i++) {
1032                 jp2->cl[i] = cio_read(cio, 4);  /* CLi */
1033         }
1034
1035         if (cio_tell(cio) - box.init_pos != box.length) {
1036                 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
1037                 return false;
1038         }
1039
1040         return true;
1041 }
1042
1043 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1044         unsigned int j2k_codestream_offset, j2k_codestream_length;
1045         opj_jp2_box_t box;
1046
1047         opj_j2k_t *j2k = jp2->j2k;
1048
1049         box.init_pos = cio_tell(cio);
1050         cio_skip(cio, 4);
1051         cio_write(cio, JP2_JP2C, 4);    /* JP2C */
1052
1053         /* J2K encoding */
1054         j2k_codestream_offset = cio_tell(cio);
1055         if(!j2k_encode(j2k, cio, image, cstr_info)) {
1056                 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
1057                 return 0;
1058         }
1059         j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
1060
1061         jp2->j2k_codestream_offset = j2k_codestream_offset;
1062         jp2->j2k_codestream_length = j2k_codestream_length;
1063
1064         box.length = 8 + jp2->j2k_codestream_length;
1065         cio_seek(cio, box.init_pos);
1066         cio_write(cio, box.length, 4);  /* L */
1067         cio_seek(cio, box.init_pos + box.length);
1068
1069         return box.length;
1070 }
1071
1072 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
1073         opj_jp2_box_t box;
1074
1075         opj_common_ptr cinfo = jp2->cinfo;
1076
1077         jp2_read_boxhdr(cinfo, cio, &box);
1078         do {
1079                 if(JP2_JP2C != box.type) {
1080                         cio_skip(cio, box.length - 8);
1081                         jp2_read_boxhdr(cinfo, cio, &box);
1082                 }
1083         } while(JP2_JP2C != box.type);
1084
1085         *j2k_codestream_offset = cio_tell(cio);
1086         *j2k_codestream_length = box.length - 8;
1087
1088         return true;
1089 }
1090
1091 static void jp2_write_jp(opj_cio_t *cio) {
1092         opj_jp2_box_t box;
1093
1094         box.init_pos = cio_tell(cio);
1095         cio_skip(cio, 4);
1096         cio_write(cio, JP2_JP, 4);              /* JP2 signature */
1097         cio_write(cio, 0x0d0a870a, 4);
1098
1099         box.length = cio_tell(cio) - box.init_pos;
1100         cio_seek(cio, box.init_pos);
1101         cio_write(cio, box.length, 4);  /* L */
1102         cio_seek(cio, box.init_pos + box.length);
1103 }
1104
1105 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
1106         opj_jp2_box_t box;
1107
1108         opj_common_ptr cinfo = jp2->cinfo;
1109
1110         jp2_read_boxhdr(cinfo, cio, &box);
1111         if (JP2_JP != box.type) {
1112                 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
1113                 return false;
1114         }
1115         if (0x0d0a870a != cio_read(cio, 4)) {
1116                 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
1117                 return false;
1118         }
1119         if (cio_tell(cio) - box.init_pos != box.length) {
1120                 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
1121                 return false;
1122         }
1123
1124         return true;
1125 }
1126
1127
1128 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
1129         struct extension *ext) {
1130         if (!jp2_read_jp(jp2, cio))
1131                 return false;
1132         if (!jp2_read_ftyp(jp2, cio))
1133                 return false;
1134         if (!jp2_read_jp2h(jp2, cio, ext))
1135                 return false;
1136         if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
1137                 return false;
1138         
1139         return true;
1140 }
1141
1142 /* ----------------------------------------------------------------------- */
1143 /* JP2 decoder interface                                             */
1144 /* ----------------------------------------------------------------------- */
1145
1146 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
1147         opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
1148         if(jp2) {
1149                 jp2->cinfo = cinfo;
1150                 /* create the J2K codec */
1151                 jp2->j2k = j2k_create_decompress(cinfo);
1152                 if(jp2->j2k == NULL) {
1153                         jp2_destroy_decompress(jp2);
1154                         return NULL;
1155                 }
1156         }
1157         return jp2;
1158 }
1159
1160 void jp2_destroy_decompress(opj_jp2_t *jp2) {
1161         if(jp2) {
1162                 /* destroy the J2K codec */
1163                 j2k_destroy_decompress(jp2->j2k);
1164
1165                 if(jp2->comps) {
1166                         opj_free(jp2->comps);
1167                 }
1168                 if(jp2->cl) {
1169                         opj_free(jp2->cl);
1170                 }
1171                 opj_free(jp2);
1172         }
1173 }
1174
1175 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
1176         /* setup the J2K codec */
1177         j2k_setup_decoder(jp2->j2k, parameters);
1178         /* further JP2 initializations go here */
1179 }
1180
1181 /* ----------------------------------------------------------------------- */
1182 /* JP2 encoder interface                                             */
1183 /* ----------------------------------------------------------------------- */
1184
1185 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
1186         opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1187         if(jp2) {
1188                 jp2->cinfo = cinfo;
1189                 /* create the J2K codec */
1190                 jp2->j2k = j2k_create_compress(cinfo);
1191                 if(jp2->j2k == NULL) {
1192                         jp2_destroy_compress(jp2);
1193                         return NULL;
1194                 }
1195         }
1196         return jp2;
1197 }
1198
1199 void jp2_destroy_compress(opj_jp2_t *jp2) {
1200         if(jp2) {
1201                 /* destroy the J2K codec */
1202                 j2k_destroy_compress(jp2->j2k);
1203
1204                 if(jp2->comps) {
1205                         opj_free(jp2->comps);
1206                 }
1207                 if(jp2->cl) {
1208                         opj_free(jp2->cl);
1209                 }
1210                 opj_free(jp2);
1211         }
1212 }
1213
1214 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1215         int i;
1216         int depth_0, sign;
1217
1218         if(!jp2 || !parameters || !image)
1219                 return;
1220
1221         /* setup the J2K codec */
1222         /* ------------------- */
1223
1224         /* Check if number of components respects standard */
1225         if (image->numcomps < 1 || image->numcomps > 16384) {
1226                 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1227                 return;
1228         }
1229
1230         j2k_setup_encoder(jp2->j2k, parameters, image);
1231
1232         /* setup the JP2 codec */
1233         /* ------------------- */
1234         
1235         /* Profile box */
1236
1237         jp2->brand = JP2_JP2;   /* BR */
1238         jp2->minversion = 0;    /* MinV */
1239         jp2->numcl = 1;
1240         jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1241         jp2->cl[0] = JP2_JP2;   /* CL0 : JP2 */
1242
1243         /* Image Header box */
1244
1245         jp2->numcomps = image->numcomps;        /* NC */
1246         jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1247         jp2->h = image->y1 - image->y0;         /* HEIGHT */
1248         jp2->w = image->x1 - image->x0;         /* WIDTH */
1249         /* BPC */
1250         depth_0 = image->comps[0].prec - 1;
1251         sign = image->comps[0].sgnd;
1252         jp2->bpc = depth_0 + (sign << 7);
1253         for (i = 1; i < image->numcomps; i++) {
1254                 int depth = image->comps[i].prec - 1;
1255                 sign = image->comps[i].sgnd;
1256                 if (depth_0 != depth)
1257                         jp2->bpc = 255;
1258         }
1259         jp2->C = 7;                     /* C : Always 7 */
1260         jp2->UnkC = 0;          /* UnkC, colorspace specified in colr box */
1261         jp2->IPR = 0;           /* IPR, no intellectual property */
1262         
1263         /* BitsPerComponent box */
1264
1265         for (i = 0; i < image->numcomps; i++) {
1266                 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1267         }
1268
1269         /* Colour Specification box */
1270
1271         if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
1272                 jp2->meth = 1;  /* METH: Enumerated colourspace */
1273         } else {
1274                 jp2->meth = 2;  /* METH: Restricted ICC profile */
1275         }
1276         if (jp2->meth == 1) {
1277                 if (image->color_space == 1)
1278                         jp2->enumcs = 16;       /* sRGB as defined by IEC 61966�2�1 */
1279                 else if (image->color_space == 2)
1280                         jp2->enumcs = 17;       /* greyscale */
1281                 else if (image->color_space == 3)
1282                         jp2->enumcs = 18;       /* YUV */
1283         } else {
1284                 jp2->enumcs = 0;                /* PROFILE (??) */
1285         }
1286         jp2->precedence = 0;    /* PRECEDENCE */
1287         jp2->approx = 0;                /* APPROX */
1288
1289 }
1290
1291 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1292
1293         /* JP2 encoding */
1294
1295         /* JPEG 2000 Signature box */
1296         jp2_write_jp(cio);
1297         /* File Type box */
1298         jp2_write_ftyp(jp2, cio);
1299         /* JP2 Header box */
1300         jp2_write_jp2h(jp2, cio);
1301
1302         /* J2K encoding */
1303
1304         if(!jp2_write_jp2c(jp2, cio, image, cstr_info)) {
1305                 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
1306                 return false;
1307         }
1308
1309         return true;
1310 }
1311
1312