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