backport rev[824-834] and rev[837-859] into the openjpeg-1.5 branch
[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_includes.h"
32
33 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
34 /*@{*/
35
36 /** @name Local static functions */
37 /*@{*/
38
39 /**
40 Read box headers
41 @param cinfo Codec context info
42 @param cio Input stream
43 @param box
44 @return Returns true if successful, returns false otherwise
45 */
46 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
47 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
48 /**
49 Read the IHDR box - Image Header box
50 @param jp2 JP2 handle
51 @param cio Input buffer stream
52 @return Returns true if successful, returns false otherwise
53 */
54 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
55 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
56 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
57 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
58 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
59 /**
60 Write the FTYP box - File type box
61 @param jp2 JP2 handle
62 @param cio Output buffer stream
63 */
64 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
65 /**
66 Read the FTYP box - File type box
67 @param jp2 JP2 handle
68 @param cio Input buffer stream
69 @return Returns true if successful, returns false otherwise
70 */
71 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
72 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
73 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
74 static void jp2_write_jp(opj_cio_t *cio);
75 /**
76 Read the JP box - JPEG 2000 signature
77 @param jp2 JP2 handle
78 @param cio Input buffer stream
79 @return Returns true if successful, returns false otherwise
80 */
81 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
82 /**
83 Decode the structure of a JP2 file
84 @param jp2 JP2 handle
85 @param cio Input buffer stream
86 @param color Collector for profile, cdef and pclr data
87 @return Returns true if successful, returns false otherwise
88 */
89 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
90         opj_jp2_color_t *color);
91 /**
92 Apply collected palette data
93 @param color Collector for profile, cdef and pclr data
94 @param image 
95 */
96 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image);
97 /**
98 Collect palette data
99 @param jp2 JP2 handle
100 @param cio Input buffer stream
101 @param box
102 @param color Collector for profile, cdef and pclr data
103 @return Returns true if successful, returns false otherwise
104 */
105 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
106     opj_jp2_box_t *box, opj_jp2_color_t *color);
107 /**
108 Collect component mapping data
109 @param jp2 JP2 handle
110 @param cio Input buffer stream
111 @param box
112 @param color Collector for profile, cdef and pclr data
113 @return Returns true if successful, returns false otherwise
114 */
115 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
116     opj_jp2_box_t *box, opj_jp2_color_t *color);
117 /**
118 Collect colour specification data
119 @param jp2 JP2 handle
120 @param cio Input buffer stream
121 @param box
122 @param color Collector for profile, cdef and pclr data
123 @return Returns true if successful, returns false otherwise
124 */
125 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
126     opj_jp2_box_t *box, opj_jp2_color_t *color);
127 /*@}*/
128
129 /*@}*/
130
131 /* ----------------------------------------------------------------------- */
132
133 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
134         box->init_pos = cio_tell(cio);
135         box->length = cio_read(cio, 4);
136         box->type = cio_read(cio, 4);
137         if (box->length == 1) {
138                 if (cio_read(cio, 4) != 0) {
139                         opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
140                         return OPJ_FALSE;
141                 }
142                 box->length = cio_read(cio, 4);
143                 if (box->length == 0) 
144                         box->length = cio_numbytesleft(cio) + 12;
145         }
146         else if (box->length == 0) {
147                 box->length = cio_numbytesleft(cio) + 8;
148         }
149         
150         return OPJ_TRUE;
151 }
152
153 #if 0
154 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
155         unsigned int i;
156         opj_jp2_box_t box;
157
158         box.init_pos = cio_tell(cio);
159         cio_skip(cio, 4);
160         cio_write(cio, JP2_URL, 4);     /* DBTL */
161         cio_write(cio, 0, 1);           /* VERS */
162         cio_write(cio, 0, 3);           /* FLAG */
163
164         if(Idx_file) {
165                 for (i = 0; i < strlen(Idx_file); i++) {
166                         cio_write(cio, Idx_file[i], 1);
167                 }
168         }
169
170         box.length = cio_tell(cio) - box.init_pos;
171         cio_seek(cio, box.init_pos);
172         cio_write(cio, box.length, 4);  /* L */
173         cio_seek(cio, box.init_pos + box.length);
174 }
175 #endif
176
177 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
178         opj_jp2_box_t box;
179
180         opj_common_ptr cinfo = jp2->cinfo;
181
182         jp2_read_boxhdr(cinfo, cio, &box);
183         if (JP2_IHDR != box.type) {
184                 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
185                 return OPJ_FALSE;
186         }
187
188         jp2->h = cio_read(cio, 4);                      /* HEIGHT */
189         jp2->w = cio_read(cio, 4);                      /* WIDTH */
190         jp2->numcomps = cio_read(cio, 2);       /* NC */
191         jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
192
193         jp2->bpc = cio_read(cio, 1);            /* BPC */
194
195         jp2->C = cio_read(cio, 1);                      /* C */
196         jp2->UnkC = cio_read(cio, 1);           /* UnkC */
197         jp2->IPR = cio_read(cio, 1);            /* IPR */
198
199         if (cio_tell(cio) - box.init_pos != box.length) {
200                 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
201                 return OPJ_FALSE;
202         }
203
204         return OPJ_TRUE;
205 }
206
207 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
208         opj_jp2_box_t box;
209
210         box.init_pos = cio_tell(cio);
211         cio_skip(cio, 4);
212         cio_write(cio, JP2_IHDR, 4);            /* IHDR */
213
214         cio_write(cio, jp2->h, 4);                      /* HEIGHT */
215         cio_write(cio, jp2->w, 4);                      /* WIDTH */
216         cio_write(cio, jp2->numcomps, 2);       /* NC */
217
218         cio_write(cio, jp2->bpc, 1);            /* BPC */
219
220         cio_write(cio, jp2->C, 1);                      /* C : Always 7 */
221         cio_write(cio, jp2->UnkC, 1);           /* UnkC, colorspace unknown */
222         cio_write(cio, jp2->IPR, 1);            /* IPR, no intellectual property */
223
224         box.length = cio_tell(cio) - box.init_pos;
225         cio_seek(cio, box.init_pos);
226         cio_write(cio, box.length, 4);  /* L */
227         cio_seek(cio, box.init_pos + box.length);
228 }
229
230 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
231         unsigned int i;
232         opj_jp2_box_t box;
233
234         box.init_pos = cio_tell(cio);
235         cio_skip(cio, 4);
236         cio_write(cio, JP2_BPCC, 4);    /* BPCC */
237
238         for (i = 0; i < jp2->numcomps; i++) {
239                 cio_write(cio, jp2->comps[i].bpcc, 1);
240         }
241
242         box.length = cio_tell(cio) - box.init_pos;
243         cio_seek(cio, box.init_pos);
244         cio_write(cio, box.length, 4);  /* L */
245         cio_seek(cio, box.init_pos + box.length);
246 }
247
248
249 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
250         unsigned int i;
251         opj_jp2_box_t box;
252
253         opj_common_ptr cinfo = jp2->cinfo;
254
255         jp2_read_boxhdr(cinfo, cio, &box);
256         if (JP2_BPCC != box.type) {
257                 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
258                 return OPJ_FALSE;
259         }
260
261         for (i = 0; i < jp2->numcomps; i++) {
262                 jp2->comps[i].bpcc = cio_read(cio, 1);
263         }
264
265         if (cio_tell(cio) - box.init_pos != box.length) {
266                 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
267                 return OPJ_FALSE;
268         }
269
270         return OPJ_TRUE;
271 }
272
273 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
274         opj_jp2_box_t box;
275
276         box.init_pos = cio_tell(cio);
277         cio_skip(cio, 4);
278         cio_write(cio, JP2_COLR, 4);            /* COLR */
279
280         cio_write(cio, jp2->meth, 1);           /* METH */
281         cio_write(cio, jp2->precedence, 1);     /* PRECEDENCE */
282         cio_write(cio, jp2->approx, 1);         /* APPROX */
283
284         if(jp2->meth == 2)
285          jp2->enumcs = 0;
286
287         cio_write(cio, jp2->enumcs, 4); /* EnumCS */
288
289         box.length = cio_tell(cio) - box.init_pos;
290         cio_seek(cio, box.init_pos);
291         cio_write(cio, box.length, 4);  /* L */
292         cio_seek(cio, box.init_pos + box.length);
293 }
294
295 static void jp2_free_pclr(opj_jp2_color_t *color)
296 {
297     opj_free(color->jp2_pclr->channel_sign);
298     opj_free(color->jp2_pclr->channel_size);
299     opj_free(color->jp2_pclr->entries);
300
301         if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
302
303     opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
304 }
305
306 static void free_color_data(opj_jp2_color_t *color)
307 {
308         if(color->jp2_pclr)
309    {
310         jp2_free_pclr(color);
311    }
312         if(color->jp2_cdef) 
313    {
314         if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
315         opj_free(color->jp2_cdef);
316    }
317         if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
318 }
319
320 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image)
321 {
322         opj_image_comp_t *old_comps, *new_comps;
323         unsigned char *channel_size, *channel_sign;
324         unsigned int *entries;
325         opj_jp2_cmap_comp_t *cmap;
326         int *src, *dst;
327         unsigned int j, max;
328         unsigned short i, nr_channels, cmp, pcol;
329         int k, top_k;
330
331         channel_size = color->jp2_pclr->channel_size;
332         channel_sign = color->jp2_pclr->channel_sign;
333         entries = color->jp2_pclr->entries;
334         cmap = color->jp2_pclr->cmap;
335         nr_channels = color->jp2_pclr->nr_channels;
336
337         old_comps = image->comps;
338         new_comps = (opj_image_comp_t*)
339          opj_malloc(nr_channels * sizeof(opj_image_comp_t));
340
341         for(i = 0; i < nr_channels; ++i)
342    {
343         pcol = cmap[i].pcol; cmp = cmap[i].cmp;
344
345         new_comps[pcol] = old_comps[cmp];
346
347         if(cmap[i].mtyp == 0) /* Direct use */
348   {
349         old_comps[cmp].data = NULL; continue;
350   }
351 /* Palette mapping: */
352         new_comps[pcol].data = (int*)
353          opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
354         new_comps[pcol].prec = channel_size[i];
355         new_comps[pcol].sgnd = channel_sign[i];
356    }
357         top_k = color->jp2_pclr->nr_entries - 1;
358
359         for(i = 0; i < nr_channels; ++i)
360    {
361 /* Direct use: */
362         if(cmap[i].mtyp == 0) continue;
363
364 /* Palette mapping: */
365         cmp = cmap[i].cmp; pcol = cmap[i].pcol;
366         src = old_comps[cmp].data; 
367         dst = new_comps[pcol].data;
368         max = new_comps[pcol].w * new_comps[pcol].h;
369
370         for(j = 0; j < max; ++j)
371   {
372 /* The index */
373         if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
374 /* The colour */
375         dst[j] = entries[k * nr_channels + pcol];
376   }
377    }
378         max = image->numcomps;
379         for(i = 0; i < max; ++i)
380    {
381         if(old_comps[i].data) opj_free(old_comps[i].data);
382    }
383         opj_free(old_comps);
384         image->comps = new_comps;
385         image->numcomps = nr_channels;
386
387         jp2_free_pclr(color);
388
389 }/* apply_pclr() */
390
391 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
392         opj_jp2_box_t *box, opj_jp2_color_t *color)
393 {
394         opj_jp2_pclr_t *jp2_pclr;
395         unsigned char *channel_size, *channel_sign;
396         unsigned int *entries;
397         unsigned short nr_entries, nr_channels;
398         unsigned short i, j;
399         unsigned char uc;
400
401         OPJ_ARG_NOT_USED(box);
402         OPJ_ARG_NOT_USED(jp2);
403
404 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
405  * a JP2 Header box' :
406 */
407         if(color->jp2_pclr) return OPJ_FALSE;
408
409         nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
410         nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
411
412         entries = (unsigned int*)
413          opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
414         channel_size = (unsigned char*)opj_malloc(nr_channels);
415         channel_sign = (unsigned char*)opj_malloc(nr_channels);
416
417         jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
418         jp2_pclr->channel_sign = channel_sign;
419         jp2_pclr->channel_size = channel_size;
420         jp2_pclr->entries = entries;
421         jp2_pclr->nr_entries = nr_entries;
422         jp2_pclr->nr_channels = nr_channels;
423         jp2_pclr->cmap = NULL;
424
425         color->jp2_pclr = jp2_pclr;
426
427         for(i = 0; i < nr_channels; ++i)
428    {
429         uc = cio_read(cio, 1); /* Bi */
430         channel_size[i] = (uc & 0x7f) + 1;
431         channel_sign[i] = (uc & 0x80)?1:0;
432    }
433
434         for(j = 0; j < nr_entries; ++j)
435    {
436         for(i = 0; i < nr_channels; ++i)
437   {
438 /* Cji */
439         *entries++ = cio_read(cio, channel_size[i]>>3);
440   }
441    }
442
443         return OPJ_TRUE;
444 }/* jp2_read_pclr() */
445
446 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
447         opj_jp2_box_t *box, opj_jp2_color_t *color)
448 {
449         opj_jp2_cmap_comp_t *cmap;
450         unsigned short i, nr_channels;
451
452         OPJ_ARG_NOT_USED(box);
453         OPJ_ARG_NOT_USED(jp2);
454
455 /* Need nr_channels: */
456         if(color->jp2_pclr == NULL) return OPJ_FALSE;
457
458 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
459  * inside a JP2 Header box' :
460 */
461         if(color->jp2_pclr->cmap) return OPJ_FALSE;
462
463         nr_channels = color->jp2_pclr->nr_channels;
464         cmap = (opj_jp2_cmap_comp_t*)
465          opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
466
467         for(i = 0; i < nr_channels; ++i)
468    {
469         cmap[i].cmp = (unsigned short)cio_read(cio, 2);
470         cmap[i].mtyp = cio_read(cio, 1);
471         cmap[i].pcol = cio_read(cio, 1);
472
473    }
474         color->jp2_pclr->cmap = cmap;
475
476         return OPJ_TRUE;
477 }/* jp2_read_cmap() */
478
479 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
480 {
481         opj_jp2_cdef_info_t *info;
482         int color_space;
483         unsigned short i, n, cn, typ, asoc, acn;
484
485         color_space = image->color_space;
486         info = color->jp2_cdef->info;
487         n = color->jp2_cdef->n;
488
489         for(i = 0; i < n; ++i)
490    {
491 /* WATCH: acn = asoc - 1 ! */
492         if((asoc = info[i].asoc) == 0) continue;
493
494         cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
495
496         if(cn != acn)
497   {
498         opj_image_comp_t saved;
499
500         memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
501         memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
502         memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
503
504         info[i].asoc = cn + 1;
505         info[acn].asoc = info[acn].cn + 1;
506   }
507    }
508         if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
509
510         opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
511
512 }/* jp2_apply_cdef() */
513
514 static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
515         opj_jp2_box_t *box, opj_jp2_color_t *color)
516 {
517         opj_jp2_cdef_info_t *info;
518         unsigned short i, n;
519
520         OPJ_ARG_NOT_USED(box);
521         OPJ_ARG_NOT_USED(jp2);
522
523 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
524  * inside a JP2 Header box.' 
525 */
526         if(color->jp2_cdef) return OPJ_FALSE;
527
528         if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
529
530         info = (opj_jp2_cdef_info_t*)
531          opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
532
533         color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
534         color->jp2_cdef->info = info;
535         color->jp2_cdef->n = n;
536
537         for(i = 0; i < n; ++i)
538    {
539         info[i].cn = (unsigned short)cio_read(cio, 2);
540         info[i].typ = (unsigned short)cio_read(cio, 2);
541         info[i].asoc = (unsigned short)cio_read(cio, 2);
542
543    }
544         return OPJ_TRUE;
545 }/* jp2_read_cdef() */
546
547 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
548         opj_jp2_box_t *box, opj_jp2_color_t *color) 
549 {
550         int skip_len;
551     opj_common_ptr cinfo;
552
553 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
554  * Specification boxes after the first.' 
555 */
556         if(color->jp2_has_colr) return OPJ_FALSE;
557
558         cinfo = jp2->cinfo;
559
560         jp2->meth = cio_read(cio, 1);           /* METH */
561         jp2->precedence = cio_read(cio, 1);     /* PRECEDENCE */
562         jp2->approx = cio_read(cio, 1);         /* APPROX */
563
564         if (jp2->meth == 1) 
565    {
566         jp2->enumcs = cio_read(cio, 4); /* EnumCS */
567    } 
568         else
569         if (jp2->meth == 2) 
570    {
571 /* skip PROFILE */
572         skip_len = box->init_pos + box->length - cio_tell(cio);
573         if (skip_len < 0) 
574   {
575         opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
576         return OPJ_FALSE;
577   }
578         if(skip_len > 0)
579   {
580         unsigned char *start;
581
582         start = cio_getbp(cio);
583         color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
584         color->icc_profile_len = skip_len;
585
586         cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
587
588         memcpy(color->icc_profile_buf, start, skip_len);
589   }
590    }
591
592         if (cio_tell(cio) - box->init_pos != box->length) 
593    {
594         opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
595         return OPJ_FALSE;
596    }
597         color->jp2_has_colr = 1;
598
599         return OPJ_TRUE;
600 }/* jp2_read_colr() */
601
602 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color) 
603 {
604         opj_jp2_box_t box;
605         int jp2h_end;
606
607         opj_common_ptr cinfo = jp2->cinfo;
608
609         jp2_read_boxhdr(cinfo, cio, &box);
610         do 
611    {
612         if (JP2_JP2H != box.type) 
613   {
614         if (box.type == JP2_JP2C) 
615  {
616         opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
617         return OPJ_FALSE;
618  }
619         cio_skip(cio, box.length - 8);
620
621         if(cio->bp >= cio->end) return OPJ_FALSE;
622
623         jp2_read_boxhdr(cinfo, cio, &box);
624   }
625    } while(JP2_JP2H != box.type);
626
627         if (!jp2_read_ihdr(jp2, cio))
628                 return OPJ_FALSE;
629         jp2h_end = box.init_pos + box.length;
630
631         if (jp2->bpc == 255) 
632    {
633         if (!jp2_read_bpcc(jp2, cio))
634                 return OPJ_FALSE;
635    }
636         jp2_read_boxhdr(cinfo, cio, &box);
637
638         while(cio_tell(cio) < jp2h_end)
639    {
640         if(box.type == JP2_COLR)
641   {
642         if( !jp2_read_colr(jp2, cio, &box, color))
643  {
644     cio_seek(cio, box.init_pos + 8);
645     cio_skip(cio, box.length - 8);
646  }
647     jp2_read_boxhdr(cinfo, cio, &box);
648     continue;
649   }
650     if(box.type == JP2_CDEF)
651   {
652     if( !jp2_read_cdef(jp2, cio, &box, color))
653  {
654     cio_seek(cio, box.init_pos + 8);
655     cio_skip(cio, box.length - 8);
656  }
657     jp2_read_boxhdr(cinfo, cio, &box);
658     continue;
659   }
660     if(box.type == JP2_PCLR)
661   {
662     if( !jp2_read_pclr(jp2, cio, &box, color))
663  {
664     cio_seek(cio, box.init_pos + 8);
665     cio_skip(cio, box.length - 8);
666  }
667     jp2_read_boxhdr(cinfo, cio, &box);
668     continue;
669   }
670     if(box.type == JP2_CMAP)
671   {
672     if( !jp2_read_cmap(jp2, cio, &box, color))
673  {
674     cio_seek(cio, box.init_pos + 8);
675     cio_skip(cio, box.length - 8);
676  }
677     jp2_read_boxhdr(cinfo, cio, &box);
678     continue;
679   }
680         cio_seek(cio, box.init_pos + 8);
681         cio_skip(cio, box.length - 8);
682         jp2_read_boxhdr(cinfo, cio, &box);
683
684    }/* while(cio_tell(cio) < box_end) */
685
686         cio_seek(cio, jp2h_end);
687
688 /* Part 1, I.5.3.3 : 'must contain at least one' */
689         return (color->jp2_has_colr == 1);
690
691 }/* jp2_read_jp2h() */
692
693 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio, 
694         opj_codestream_info_t *cstr_info) 
695 {
696         opj_common_ptr cinfo;
697         opj_image_t *image = NULL;
698         opj_jp2_color_t color;
699
700         if(!jp2 || !cio) 
701    {
702         return NULL;
703    }
704         memset(&color, 0, sizeof(opj_jp2_color_t));
705         cinfo = jp2->cinfo;
706
707 /* JP2 decoding */
708         if(!jp2_read_struct(jp2, cio, &color)) 
709    {
710         free_color_data(&color);
711         opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
712         return NULL;
713    }
714
715 /* J2K decoding */
716         image = j2k_decode(jp2->j2k, cio, cstr_info);
717
718         if(!image) 
719    {
720         free_color_data(&color);
721         opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
722         return NULL;
723    }
724
725 /* Set Image Color Space */
726         if (jp2->enumcs == 16)
727                 image->color_space = CLRSPC_SRGB;
728         else if (jp2->enumcs == 17)
729                 image->color_space = CLRSPC_GRAY;
730         else if (jp2->enumcs == 18)
731                 image->color_space = CLRSPC_SYCC;
732         else
733                 image->color_space = CLRSPC_UNKNOWN;
734
735         if(color.jp2_cdef)
736    {
737         jp2_apply_cdef(image, &color);
738    }
739         if(color.jp2_pclr)
740    {
741 /* Part 1, I.5.3.4: Either both or none : */
742         if( !color.jp2_pclr->cmap) 
743          jp2_free_pclr(&color);
744         else
745          jp2_apply_pclr(&color, image);
746    }
747         if(color.icc_profile_buf)
748    {
749         image->icc_profile_buf = color.icc_profile_buf;
750         color.icc_profile_buf = NULL;
751         image->icc_profile_len = color.icc_profile_len;
752    }
753         return image;
754
755 }/* opj_jp2_decode() */
756
757
758 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
759         opj_jp2_box_t box;
760
761         box.init_pos = cio_tell(cio);
762         cio_skip(cio, 4);
763         cio_write(cio, JP2_JP2H, 4);    /* JP2H */
764
765         jp2_write_ihdr(jp2, cio);
766
767         if (jp2->bpc == 255) {
768                 jp2_write_bpcc(jp2, cio);
769         }
770         jp2_write_colr(jp2, cio);
771
772         box.length = cio_tell(cio) - box.init_pos;
773         cio_seek(cio, box.init_pos);
774         cio_write(cio, box.length, 4);  /* L */
775         cio_seek(cio, box.init_pos + box.length);
776 }
777
778 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
779         unsigned int i;
780         opj_jp2_box_t box;
781
782         box.init_pos = cio_tell(cio);
783         cio_skip(cio, 4);
784         cio_write(cio, JP2_FTYP, 4);            /* FTYP */
785
786         cio_write(cio, jp2->brand, 4);          /* BR */
787         cio_write(cio, jp2->minversion, 4);     /* MinV */
788
789         for (i = 0; i < jp2->numcl; i++) {
790                 cio_write(cio, jp2->cl[i], 4);  /* CL */
791         }
792
793         box.length = cio_tell(cio) - box.init_pos;
794         cio_seek(cio, box.init_pos);
795         cio_write(cio, box.length, 4);  /* L */
796         cio_seek(cio, box.init_pos + box.length);
797 }
798
799 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
800         int i;
801         opj_jp2_box_t box;
802
803         opj_common_ptr cinfo = jp2->cinfo;
804
805         jp2_read_boxhdr(cinfo, cio, &box);
806
807         if (JP2_FTYP != box.type) {
808                 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
809                 return OPJ_FALSE;
810         }
811
812         jp2->brand = cio_read(cio, 4);          /* BR */
813         jp2->minversion = cio_read(cio, 4);     /* MinV */
814         jp2->numcl = (box.length - 16) / 4;
815         jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
816
817         for (i = 0; i < (int)jp2->numcl; i++) {
818                 jp2->cl[i] = cio_read(cio, 4);  /* CLi */
819         }
820
821         if (cio_tell(cio) - box.init_pos != box.length) {
822                 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
823                 return OPJ_FALSE;
824         }
825
826         return OPJ_TRUE;
827 }
828
829 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
830         unsigned int j2k_codestream_offset, j2k_codestream_length;
831         opj_jp2_box_t box;
832
833         opj_j2k_t *j2k = jp2->j2k;
834
835         box.init_pos = cio_tell(cio);
836         cio_skip(cio, 4);
837         cio_write(cio, JP2_JP2C, 4);    /* JP2C */
838
839         /* J2K encoding */
840         j2k_codestream_offset = cio_tell(cio);
841         if(!j2k_encode(j2k, cio, image, cstr_info)) {
842                 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
843                 return 0;
844         }
845         j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
846
847         jp2->j2k_codestream_offset = j2k_codestream_offset;
848         jp2->j2k_codestream_length = j2k_codestream_length;
849
850         box.length = 8 + jp2->j2k_codestream_length;
851         cio_seek(cio, box.init_pos);
852         cio_write(cio, box.length, 4);  /* L */
853         cio_seek(cio, box.init_pos + box.length);
854
855         return box.length;
856 }
857
858 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
859         opj_jp2_box_t box;
860
861         opj_common_ptr cinfo = jp2->cinfo;
862
863         jp2_read_boxhdr(cinfo, cio, &box);
864         do {
865                 if(JP2_JP2C != box.type) {
866                         cio_skip(cio, box.length - 8);
867                         jp2_read_boxhdr(cinfo, cio, &box);
868                 }
869         } while(JP2_JP2C != box.type);
870
871         *j2k_codestream_offset = cio_tell(cio);
872         *j2k_codestream_length = box.length - 8;
873
874         return OPJ_TRUE;
875 }
876
877 static void jp2_write_jp(opj_cio_t *cio) {
878         opj_jp2_box_t box;
879
880         box.init_pos = cio_tell(cio);
881         cio_skip(cio, 4);
882         cio_write(cio, JP2_JP, 4);              /* JP2 signature */
883         cio_write(cio, 0x0d0a870a, 4);
884
885         box.length = cio_tell(cio) - box.init_pos;
886         cio_seek(cio, box.init_pos);
887         cio_write(cio, box.length, 4);  /* L */
888         cio_seek(cio, box.init_pos + box.length);
889 }
890
891 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
892         opj_jp2_box_t box;
893
894         opj_common_ptr cinfo = jp2->cinfo;
895
896         jp2_read_boxhdr(cinfo, cio, &box);
897         if (JP2_JP != box.type) {
898                 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
899                 return OPJ_FALSE;
900         }
901         if (0x0d0a870a != cio_read(cio, 4)) {
902                 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
903                 return OPJ_FALSE;
904         }
905         if (cio_tell(cio) - box.init_pos != box.length) {
906                 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
907                 return OPJ_FALSE;
908         }
909
910         return OPJ_TRUE;
911 }
912
913
914 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
915         opj_jp2_color_t *color) {
916         if (!jp2_read_jp(jp2, cio))
917                 return OPJ_FALSE;
918         if (!jp2_read_ftyp(jp2, cio))
919                 return OPJ_FALSE;
920         if (!jp2_read_jp2h(jp2, cio, color))
921                 return OPJ_FALSE;
922         if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
923                 return OPJ_FALSE;
924         
925         return OPJ_TRUE;
926 }
927
928 /* ----------------------------------------------------------------------- */
929 /* JP2 decoder interface                                             */
930 /* ----------------------------------------------------------------------- */
931
932 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
933         opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
934         if(jp2) {
935                 jp2->cinfo = cinfo;
936                 /* create the J2K codec */
937                 jp2->j2k = j2k_create_decompress(cinfo);
938                 if(jp2->j2k == NULL) {
939                         jp2_destroy_decompress(jp2);
940                         return NULL;
941                 }
942         }
943         return jp2;
944 }
945
946 void jp2_destroy_decompress(opj_jp2_t *jp2) {
947         if(jp2) {
948                 /* destroy the J2K codec */
949                 j2k_destroy_decompress(jp2->j2k);
950
951                 if(jp2->comps) {
952                         opj_free(jp2->comps);
953                 }
954                 if(jp2->cl) {
955                         opj_free(jp2->cl);
956                 }
957                 opj_free(jp2);
958         }
959 }
960
961 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
962         /* setup the J2K codec */
963         j2k_setup_decoder(jp2->j2k, parameters);
964         /* further JP2 initializations go here */
965 }
966
967 /* ----------------------------------------------------------------------- */
968 /* JP2 encoder interface                                             */
969 /* ----------------------------------------------------------------------- */
970
971 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
972         opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
973         if(jp2) {
974                 jp2->cinfo = cinfo;
975                 /* create the J2K codec */
976                 jp2->j2k = j2k_create_compress(cinfo);
977                 if(jp2->j2k == NULL) {
978                         jp2_destroy_compress(jp2);
979                         return NULL;
980                 }
981         }
982         return jp2;
983 }
984
985 void jp2_destroy_compress(opj_jp2_t *jp2) {
986         if(jp2) {
987                 /* destroy the J2K codec */
988                 j2k_destroy_compress(jp2->j2k);
989
990                 if(jp2->comps) {
991                         opj_free(jp2->comps);
992                 }
993                 if(jp2->cl) {
994                         opj_free(jp2->cl);
995                 }
996                 opj_free(jp2);
997         }
998 }
999
1000 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1001         int i;
1002         int depth_0, sign;
1003
1004         if(!jp2 || !parameters || !image)
1005                 return;
1006
1007         /* setup the J2K codec */
1008         /* ------------------- */
1009
1010         /* Check if number of components respects standard */
1011         if (image->numcomps < 1 || image->numcomps > 16384) {
1012                 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1013                 return;
1014         }
1015
1016         j2k_setup_encoder(jp2->j2k, parameters, image);
1017
1018         /* setup the JP2 codec */
1019         /* ------------------- */
1020         
1021         /* Profile box */
1022
1023         jp2->brand = JP2_JP2;   /* BR */
1024         jp2->minversion = 0;    /* MinV */
1025         jp2->numcl = 1;
1026         jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1027         jp2->cl[0] = JP2_JP2;   /* CL0 : JP2 */
1028
1029         /* Image Header box */
1030
1031         jp2->numcomps = image->numcomps;        /* NC */
1032         jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1033         jp2->h = image->y1 - image->y0;         /* HEIGHT */
1034         jp2->w = image->x1 - image->x0;         /* WIDTH */
1035         /* BPC */
1036         depth_0 = image->comps[0].prec - 1;
1037         sign = image->comps[0].sgnd;
1038         jp2->bpc = depth_0 + (sign << 7);
1039         for (i = 1; i < image->numcomps; i++) {
1040                 int depth = image->comps[i].prec - 1;
1041                 sign = image->comps[i].sgnd;
1042                 if (depth_0 != depth)
1043                         jp2->bpc = 255;
1044         }
1045         jp2->C = 7;                     /* C : Always 7 */
1046         jp2->UnkC = 0;          /* UnkC, colorspace specified in colr box */
1047         jp2->IPR = 0;           /* IPR, no intellectual property */
1048         
1049         /* BitsPerComponent box */
1050
1051         for (i = 0; i < image->numcomps; i++) {
1052                 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1053         }
1054         jp2->meth = 1;
1055         if (image->color_space == 1)
1056                 jp2->enumcs = 16;       /* sRGB as defined by IEC 61966-2.1 */
1057         else if (image->color_space == 2)
1058                 jp2->enumcs = 17;       /* greyscale */
1059         else if (image->color_space == 3)
1060                 jp2->enumcs = 18;       /* YUV */
1061         jp2->precedence = 0;    /* PRECEDENCE */
1062         jp2->approx = 0;                /* APPROX */
1063
1064 }
1065
1066 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1067
1068         /* JP2 encoding */
1069
1070         /* JPEG 2000 Signature box */
1071         jp2_write_jp(cio);
1072         /* File Type box */
1073         jp2_write_ftyp(jp2, cio);
1074         /* JP2 Header box */
1075         jp2_write_jp2h(jp2, cio);
1076
1077         /* J2K encoding */
1078
1079         if(!jp2_write_jp2c(jp2, cio, image, cstr_info)) {
1080                 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
1081                 return OPJ_FALSE;
1082         }
1083
1084         return OPJ_TRUE;
1085 }
1086
1087