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 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
34 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
37 /** @name Local static functions */
42 @param cinfo Codec context info
43 @param cio Input stream
45 @return Returns true if successful, returns false otherwise
47 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
48 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
50 Read the IHDR box - Image Header box
52 @param cio Input buffer stream
53 @return Returns true if successful, returns false otherwise
55 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
56 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
57 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
58 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
59 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
61 Write the FTYP box - File type box
63 @param cio Output buffer stream
65 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
67 Read the FTYP box - File type box
69 @param cio Input buffer stream
70 @return Returns true if successful, returns false otherwise
72 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
73 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
74 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
75 static void jp2_write_jp(opj_cio_t *cio);
77 Read the JP box - JPEG 2000 signature
79 @param cio Input buffer stream
80 @return Returns true if successful, returns false otherwise
82 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
84 Decode the structure of a JP2 file
86 @param cio Input buffer stream
87 @param color Collector for profile, cdef and pclr data
88 @return Returns true if successful, returns false otherwise
90 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
91 opj_jp2_color_t *color);
93 Apply collected palette data
94 @param color Collector for profile, cdef and pclr data
97 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image, opj_common_ptr cinfo);
100 @param jp2 JP2 handle
101 @param cio Input buffer stream
103 @param color Collector for profile, cdef and pclr data
104 @return Returns true if successful, returns false otherwise
106 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
107 opj_jp2_box_t *box, opj_jp2_color_t *color);
109 Collect component mapping data
110 @param jp2 JP2 handle
111 @param cio Input buffer stream
113 @param color Collector for profile, cdef and pclr data
114 @return Returns true if successful, returns false otherwise
116 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
117 opj_jp2_box_t *box, opj_jp2_color_t *color);
119 Collect colour specification data
120 @param jp2 JP2 handle
121 @param cio Input buffer stream
123 @param color Collector for profile, cdef and pclr data
124 @return Returns true if successful, returns false otherwise
126 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
127 opj_jp2_box_t *box, opj_jp2_color_t *color);
129 Write file Index (superbox)
130 @param[in] offset_jp2c offset of jp2c box
131 @param[in] length_jp2c length of jp2c box
132 @param[in] offset_idx offset of cidx box
133 @param[in] length_idx length of cidx box
134 @param[in] cio file output handle
135 @return length of fidx box
137 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
139 Write index Finder box
140 @param[in] offset offset of fidx box
141 @param[in] length length of fidx box
142 @param[in] cio file output handle
144 static void write_iptr( int offset, int length, opj_cio_t *cio);
147 @param[in] offset_jp2c offset of jp2c box
148 @param[in] length_jp2c length of jp2c box
149 @param[in] offset_idx offset of cidx box
150 @param[in] length_idx length of cidx box
151 @param[in] cio file output handle
153 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
158 /* ----------------------------------------------------------------------- */
160 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
161 box->init_pos = cio_tell(cio);
162 box->length = cio_read(cio, 4);
163 box->type = cio_read(cio, 4);
164 if (box->length == 1) {
165 if (cio_read(cio, 4) != 0) {
166 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
169 box->length = cio_read(cio, 4);
170 if (box->length == 0)
171 box->length = cio_numbytesleft(cio) + 12;
173 else if (box->length == 0) {
174 box->length = cio_numbytesleft(cio) + 8;
181 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
185 box.init_pos = cio_tell(cio);
187 cio_write(cio, JP2_URL, 4); /* DBTL */
188 cio_write(cio, 0, 1); /* VERS */
189 cio_write(cio, 0, 3); /* FLAG */
192 for (i = 0; i < strlen(Idx_file); i++) {
193 cio_write(cio, Idx_file[i], 1);
197 box.length = cio_tell(cio) - box.init_pos;
198 cio_seek(cio, box.init_pos);
199 cio_write(cio, box.length, 4); /* L */
200 cio_seek(cio, box.init_pos + box.length);
204 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
207 opj_common_ptr cinfo = jp2->cinfo;
209 jp2_read_boxhdr(cinfo, cio, &box);
210 if (JP2_IHDR != box.type) {
211 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
215 jp2->h = cio_read(cio, 4); /* HEIGHT */
216 jp2->w = cio_read(cio, 4); /* WIDTH */
217 jp2->numcomps = cio_read(cio, 2); /* NC */
218 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
220 jp2->bpc = cio_read(cio, 1); /* BPC */
222 jp2->C = cio_read(cio, 1); /* C */
223 jp2->UnkC = cio_read(cio, 1); /* UnkC */
224 jp2->IPR = cio_read(cio, 1); /* IPR */
226 if (cio_tell(cio) - box.init_pos != box.length) {
227 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
234 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
237 box.init_pos = cio_tell(cio);
239 cio_write(cio, JP2_IHDR, 4); /* IHDR */
241 cio_write(cio, jp2->h, 4); /* HEIGHT */
242 cio_write(cio, jp2->w, 4); /* WIDTH */
243 cio_write(cio, jp2->numcomps, 2); /* NC */
245 cio_write(cio, jp2->bpc, 1); /* BPC */
247 cio_write(cio, jp2->C, 1); /* C : Always 7 */
248 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
249 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
251 box.length = cio_tell(cio) - box.init_pos;
252 cio_seek(cio, box.init_pos);
253 cio_write(cio, box.length, 4); /* L */
254 cio_seek(cio, box.init_pos + box.length);
257 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
261 box.init_pos = cio_tell(cio);
263 cio_write(cio, JP2_BPCC, 4); /* BPCC */
265 for (i = 0; i < jp2->numcomps; i++) {
266 cio_write(cio, jp2->comps[i].bpcc, 1);
269 box.length = cio_tell(cio) - box.init_pos;
270 cio_seek(cio, box.init_pos);
271 cio_write(cio, box.length, 4); /* L */
272 cio_seek(cio, box.init_pos + box.length);
276 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
280 opj_common_ptr cinfo = jp2->cinfo;
282 jp2_read_boxhdr(cinfo, cio, &box);
283 if (JP2_BPCC != box.type) {
284 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
288 for (i = 0; i < jp2->numcomps; i++) {
289 jp2->comps[i].bpcc = cio_read(cio, 1);
292 if (cio_tell(cio) - box.init_pos != box.length) {
293 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
300 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
303 box.init_pos = cio_tell(cio);
305 cio_write(cio, JP2_COLR, 4); /* COLR */
307 cio_write(cio, jp2->meth, 1); /* METH */
308 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
309 cio_write(cio, jp2->approx, 1); /* APPROX */
314 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
316 box.length = cio_tell(cio) - box.init_pos;
317 cio_seek(cio, box.init_pos);
318 cio_write(cio, box.length, 4); /* L */
319 cio_seek(cio, box.init_pos + box.length);
322 static void jp2_free_pclr(opj_jp2_color_t *color)
324 opj_free(color->jp2_pclr->channel_sign);
325 opj_free(color->jp2_pclr->channel_size);
326 opj_free(color->jp2_pclr->entries);
328 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
330 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
333 static void free_color_data(opj_jp2_color_t *color)
337 jp2_free_pclr(color);
341 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
342 opj_free(color->jp2_cdef);
344 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
347 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image, opj_common_ptr cinfo)
349 opj_image_comp_t *old_comps, *new_comps;
350 unsigned char *channel_size, *channel_sign;
351 unsigned int *entries;
352 opj_jp2_cmap_comp_t *cmap;
355 unsigned short i, nr_channels, cmp, pcol;
358 channel_size = color->jp2_pclr->channel_size;
359 channel_sign = color->jp2_pclr->channel_sign;
360 entries = color->jp2_pclr->entries;
361 cmap = color->jp2_pclr->cmap;
362 nr_channels = color->jp2_pclr->nr_channels;
364 old_comps = image->comps;
365 new_comps = (opj_image_comp_t*)
366 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
368 for(i = 0; i < nr_channels; ++i)
370 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
372 if( pcol < nr_channels )
373 new_comps[pcol] = old_comps[cmp];
376 opj_event_msg(cinfo, EVT_ERROR, "Error with pcol value %d (max: %d). skipping\n", pcol, nr_channels);
380 if(cmap[i].mtyp == 0) /* Direct use */
382 old_comps[cmp].data = NULL; continue;
384 /* Palette mapping: */
385 new_comps[pcol].data = (int*)
386 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
387 new_comps[pcol].prec = channel_size[i];
388 new_comps[pcol].sgnd = channel_sign[i];
390 top_k = color->jp2_pclr->nr_entries - 1;
392 for(i = 0; i < nr_channels; ++i)
395 if(cmap[i].mtyp == 0) continue;
397 /* Palette mapping: */
398 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
399 src = old_comps[cmp].data;
400 dst = new_comps[pcol].data;
401 max = new_comps[pcol].w * new_comps[pcol].h;
403 for(j = 0; j < max; ++j)
406 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
408 dst[j] = entries[k * nr_channels + pcol];
411 max = image->numcomps;
412 for(i = 0; i < max; ++i)
414 if(old_comps[i].data) opj_free(old_comps[i].data);
417 image->comps = new_comps;
418 image->numcomps = nr_channels;
420 jp2_free_pclr(color);
424 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
425 opj_jp2_box_t *box, opj_jp2_color_t *color)
427 opj_jp2_pclr_t *jp2_pclr;
428 unsigned char *channel_size, *channel_sign;
429 unsigned int *entries;
430 unsigned short nr_entries, nr_channels;
434 OPJ_ARG_NOT_USED(box);
435 OPJ_ARG_NOT_USED(jp2);
437 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
438 * a JP2 Header box' :
440 if(color->jp2_pclr) return OPJ_FALSE;
442 nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
443 nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
445 entries = (unsigned int*)
446 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
447 channel_size = (unsigned char*)opj_malloc(nr_channels);
448 channel_sign = (unsigned char*)opj_malloc(nr_channels);
450 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
451 jp2_pclr->channel_sign = channel_sign;
452 jp2_pclr->channel_size = channel_size;
453 jp2_pclr->entries = entries;
454 jp2_pclr->nr_entries = nr_entries;
455 jp2_pclr->nr_channels = nr_channels;
456 jp2_pclr->cmap = NULL;
458 color->jp2_pclr = jp2_pclr;
460 for(i = 0; i < nr_channels; ++i)
462 uc = cio_read(cio, 1); /* Bi */
463 channel_size[i] = (uc & 0x7f) + 1;
464 channel_sign[i] = (uc & 0x80)?1:0;
467 for(j = 0; j < nr_entries; ++j)
469 for(i = 0; i < nr_channels; ++i)
472 *entries++ = cio_read(cio, channel_size[i]>>3);
477 }/* jp2_read_pclr() */
479 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
480 opj_jp2_box_t *box, opj_jp2_color_t *color)
482 opj_jp2_cmap_comp_t *cmap;
483 unsigned short i, nr_channels;
485 OPJ_ARG_NOT_USED(box);
486 OPJ_ARG_NOT_USED(jp2);
488 /* Need nr_channels: */
489 if(color->jp2_pclr == NULL) return OPJ_FALSE;
491 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
492 * inside a JP2 Header box' :
494 if(color->jp2_pclr->cmap) return OPJ_FALSE;
496 nr_channels = color->jp2_pclr->nr_channels;
497 cmap = (opj_jp2_cmap_comp_t*)
498 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
500 for(i = 0; i < nr_channels; ++i)
502 cmap[i].cmp = (unsigned short)cio_read(cio, 2);
503 cmap[i].mtyp = cio_read(cio, 1);
504 cmap[i].pcol = cio_read(cio, 1);
507 color->jp2_pclr->cmap = cmap;
510 }/* jp2_read_cmap() */
512 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
514 opj_jp2_cdef_info_t *info;
516 unsigned short i, n, cn, typ, asoc, acn;
518 color_space = image->color_space;
519 info = color->jp2_cdef->info;
520 n = color->jp2_cdef->n;
522 for(i = 0; i < n; ++i)
524 /* WATCH: acn = asoc - 1 ! */
525 if((asoc = info[i].asoc) == 0) continue;
527 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
531 opj_image_comp_t saved;
533 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
534 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
535 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
537 info[i].asoc = cn + 1;
538 info[acn].asoc = info[acn].cn + 1;
541 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
543 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
545 }/* jp2_apply_cdef() */
547 static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
548 opj_jp2_box_t *box, opj_jp2_color_t *color)
550 opj_jp2_cdef_info_t *info;
553 OPJ_ARG_NOT_USED(box);
554 OPJ_ARG_NOT_USED(jp2);
556 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
557 * inside a JP2 Header box.'
559 if(color->jp2_cdef) return OPJ_FALSE;
561 if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
563 info = (opj_jp2_cdef_info_t*)
564 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
566 color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
567 color->jp2_cdef->info = info;
568 color->jp2_cdef->n = n;
570 for(i = 0; i < n; ++i)
572 info[i].cn = (unsigned short)cio_read(cio, 2);
573 info[i].typ = (unsigned short)cio_read(cio, 2);
574 info[i].asoc = (unsigned short)cio_read(cio, 2);
578 }/* jp2_read_cdef() */
580 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
581 opj_jp2_box_t *box, opj_jp2_color_t *color)
584 opj_common_ptr cinfo;
586 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
587 * Specification boxes after the first.'
589 if(color->jp2_has_colr) return OPJ_FALSE;
593 jp2->meth = cio_read(cio, 1); /* METH */
594 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
595 jp2->approx = cio_read(cio, 1); /* APPROX */
599 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
605 skip_len = box->init_pos + box->length - cio_tell(cio);
608 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
613 unsigned char *start;
615 start = cio_getbp(cio);
616 color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
617 color->icc_profile_len = skip_len;
619 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
621 memcpy(color->icc_profile_buf, start, skip_len);
625 if (cio_tell(cio) - box->init_pos != box->length)
627 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
630 color->jp2_has_colr = 1;
633 }/* jp2_read_colr() */
635 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
640 opj_common_ptr cinfo = jp2->cinfo;
642 jp2_read_boxhdr(cinfo, cio, &box);
645 if (JP2_JP2H != box.type)
647 if (box.type == JP2_JP2C)
649 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
652 cio_skip(cio, box.length - 8);
654 if(cio->bp >= cio->end) return OPJ_FALSE;
656 jp2_read_boxhdr(cinfo, cio, &box);
658 } while(JP2_JP2H != box.type);
660 if (!jp2_read_ihdr(jp2, cio))
662 jp2h_end = box.init_pos + box.length;
666 if (!jp2_read_bpcc(jp2, cio))
669 jp2_read_boxhdr(cinfo, cio, &box);
671 while(cio_tell(cio) < jp2h_end)
673 if(box.type == JP2_COLR)
675 if( !jp2_read_colr(jp2, cio, &box, color))
677 cio_seek(cio, box.init_pos + 8);
678 cio_skip(cio, box.length - 8);
680 jp2_read_boxhdr(cinfo, cio, &box);
683 if(box.type == JP2_CDEF && !jp2->ignore_pclr_cmap_cdef)
685 if( !jp2_read_cdef(jp2, cio, &box, color))
687 cio_seek(cio, box.init_pos + 8);
688 cio_skip(cio, box.length - 8);
690 jp2_read_boxhdr(cinfo, cio, &box);
693 if(box.type == JP2_PCLR && !jp2->ignore_pclr_cmap_cdef)
695 if( !jp2_read_pclr(jp2, cio, &box, color))
697 cio_seek(cio, box.init_pos + 8);
698 cio_skip(cio, box.length - 8);
700 jp2_read_boxhdr(cinfo, cio, &box);
703 if(box.type == JP2_CMAP && !jp2->ignore_pclr_cmap_cdef)
705 if( !jp2_read_cmap(jp2, cio, &box, color))
707 cio_seek(cio, box.init_pos + 8);
708 cio_skip(cio, box.length - 8);
710 jp2_read_boxhdr(cinfo, cio, &box);
713 cio_seek(cio, box.init_pos + 8);
714 cio_skip(cio, box.length - 8);
715 jp2_read_boxhdr(cinfo, cio, &box);
717 }/* while(cio_tell(cio) < box_end) */
719 cio_seek(cio, jp2h_end);
721 /* Part 1, I.5.3.3 : 'must contain at least one' */
722 return (color->jp2_has_colr == 1);
724 }/* jp2_read_jp2h() */
726 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
727 opj_codestream_info_t *cstr_info)
729 opj_common_ptr cinfo;
730 opj_image_t *image = NULL;
731 opj_jp2_color_t color;
737 memset(&color, 0, sizeof(opj_jp2_color_t));
741 if(!jp2_read_struct(jp2, cio, &color))
743 free_color_data(&color);
744 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
749 image = j2k_decode(jp2->j2k, cio, cstr_info);
753 free_color_data(&color);
754 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
758 if (!jp2->ignore_pclr_cmap_cdef){
760 /* Set Image Color Space */
761 if (jp2->enumcs == 16)
762 image->color_space = CLRSPC_SRGB;
763 else if (jp2->enumcs == 17)
764 image->color_space = CLRSPC_GRAY;
765 else if (jp2->enumcs == 18)
766 image->color_space = CLRSPC_SYCC;
768 image->color_space = CLRSPC_UNKNOWN;
772 jp2_apply_cdef(image, &color);
776 /* Part 1, I.5.3.4: Either both or none : */
777 if( !color.jp2_pclr->cmap)
778 jp2_free_pclr(&color);
780 jp2_apply_pclr(&color, image, cinfo);
782 if(color.icc_profile_buf)
784 image->icc_profile_buf = color.icc_profile_buf;
785 color.icc_profile_buf = NULL;
786 image->icc_profile_len = color.icc_profile_len;
792 }/* opj_jp2_decode() */
795 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
798 box.init_pos = cio_tell(cio);
800 cio_write(cio, JP2_JP2H, 4); /* JP2H */
802 jp2_write_ihdr(jp2, cio);
804 if (jp2->bpc == 255) {
805 jp2_write_bpcc(jp2, cio);
807 jp2_write_colr(jp2, cio);
809 box.length = cio_tell(cio) - box.init_pos;
810 cio_seek(cio, box.init_pos);
811 cio_write(cio, box.length, 4); /* L */
812 cio_seek(cio, box.init_pos + box.length);
815 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
819 box.init_pos = cio_tell(cio);
821 cio_write(cio, JP2_FTYP, 4); /* FTYP */
823 cio_write(cio, jp2->brand, 4); /* BR */
824 cio_write(cio, jp2->minversion, 4); /* MinV */
826 for (i = 0; i < jp2->numcl; i++) {
827 cio_write(cio, jp2->cl[i], 4); /* CL */
830 box.length = cio_tell(cio) - box.init_pos;
831 cio_seek(cio, box.init_pos);
832 cio_write(cio, box.length, 4); /* L */
833 cio_seek(cio, box.init_pos + box.length);
836 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
840 opj_common_ptr cinfo = jp2->cinfo;
842 jp2_read_boxhdr(cinfo, cio, &box);
844 if (JP2_FTYP != box.type) {
845 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
849 jp2->brand = cio_read(cio, 4); /* BR */
850 jp2->minversion = cio_read(cio, 4); /* MinV */
851 jp2->numcl = (box.length - 16) / 4;
852 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
854 for (i = 0; i < (int)jp2->numcl; i++) {
855 jp2->cl[i] = cio_read(cio, 4); /* CLi */
858 if (cio_tell(cio) - box.init_pos != box.length) {
859 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
866 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
867 unsigned int j2k_codestream_offset, j2k_codestream_length;
870 opj_j2k_t *j2k = jp2->j2k;
872 box.init_pos = cio_tell(cio);
874 cio_write(cio, JP2_JP2C, 4); /* JP2C */
877 j2k_codestream_offset = cio_tell(cio);
878 if(!j2k_encode(j2k, cio, image, cstr_info)) {
879 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
882 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
884 jp2->j2k_codestream_offset = j2k_codestream_offset;
885 jp2->j2k_codestream_length = j2k_codestream_length;
887 box.length = 8 + jp2->j2k_codestream_length;
888 cio_seek(cio, box.init_pos);
889 cio_write(cio, box.length, 4); /* L */
890 cio_seek(cio, box.init_pos + box.length);
895 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
898 opj_common_ptr cinfo = jp2->cinfo;
900 jp2_read_boxhdr(cinfo, cio, &box);
902 if(JP2_JP2C != box.type) {
903 cio_skip(cio, box.length - 8);
904 jp2_read_boxhdr(cinfo, cio, &box);
906 } while(JP2_JP2C != box.type);
908 *j2k_codestream_offset = cio_tell(cio);
909 *j2k_codestream_length = box.length - 8;
914 static void jp2_write_jp(opj_cio_t *cio) {
917 box.init_pos = cio_tell(cio);
919 cio_write(cio, JP2_JP, 4); /* JP2 signature */
920 cio_write(cio, 0x0d0a870a, 4);
922 box.length = cio_tell(cio) - box.init_pos;
923 cio_seek(cio, box.init_pos);
924 cio_write(cio, box.length, 4); /* L */
925 cio_seek(cio, box.init_pos + box.length);
928 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
931 opj_common_ptr cinfo = jp2->cinfo;
933 jp2_read_boxhdr(cinfo, cio, &box);
934 if (JP2_JP != box.type) {
935 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
938 if (0x0d0a870a != cio_read(cio, 4)) {
939 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
942 if (cio_tell(cio) - box.init_pos != box.length) {
943 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
951 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
952 opj_jp2_color_t *color) {
953 if (!jp2_read_jp(jp2, cio))
955 if (!jp2_read_ftyp(jp2, cio))
957 if (!jp2_read_jp2h(jp2, cio, color))
959 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
966 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
970 lenp = cio_tell( cio);
971 cio_skip( cio, 4); /* L [at the end] */
972 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
974 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
976 len = cio_tell( cio)-lenp;
977 cio_seek( cio, lenp);
978 cio_write( cio, len, 4); /* L */
979 cio_seek( cio, lenp+len);
984 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
988 lenp = cio_tell( cio);
989 cio_skip( cio, 4); /* L [at the end] */
990 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
992 cio_write( cio, offset_jp2c, 8); /* OOFF */
993 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
994 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
996 cio_write( cio, 1,1); /* NI */
998 cio_write( cio, offset_idx, 8); /* IOFF */
999 cio_write( cio, length_idx, 4); /* IBH part 1 */
1000 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
1002 len = cio_tell( cio)-lenp;
1003 cio_seek( cio, lenp);
1004 cio_write( cio, len, 4); /* L */
1005 cio_seek( cio, lenp+len);
1008 static void write_iptr( int offset, int length, opj_cio_t *cio)
1012 lenp = cio_tell( cio);
1013 cio_skip( cio, 4); /* L [at the end] */
1014 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
1016 cio_write( cio, offset, 8);
1017 cio_write( cio, length, 8);
1019 len = cio_tell( cio)-lenp;
1020 cio_seek( cio, lenp);
1021 cio_write( cio, len, 4); /* L */
1022 cio_seek( cio, lenp+len);
1026 /* ----------------------------------------------------------------------- */
1027 /* JP2 decoder interface */
1028 /* ----------------------------------------------------------------------- */
1030 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
1031 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
1034 /* create the J2K codec */
1035 jp2->j2k = j2k_create_decompress(cinfo);
1036 if(jp2->j2k == NULL) {
1037 jp2_destroy_decompress(jp2);
1044 void jp2_destroy_decompress(opj_jp2_t *jp2) {
1046 /* destroy the J2K codec */
1047 j2k_destroy_decompress(jp2->j2k);
1050 opj_free(jp2->comps);
1059 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
1060 /* setup the J2K codec */
1061 j2k_setup_decoder(jp2->j2k, parameters);
1062 /* further JP2 initializations go here */
1063 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1066 /* ----------------------------------------------------------------------- */
1067 /* JP2 encoder interface */
1068 /* ----------------------------------------------------------------------- */
1070 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
1071 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1074 /* create the J2K codec */
1075 jp2->j2k = j2k_create_compress(cinfo);
1076 if(jp2->j2k == NULL) {
1077 jp2_destroy_compress(jp2);
1084 void jp2_destroy_compress(opj_jp2_t *jp2) {
1086 /* destroy the J2K codec */
1087 j2k_destroy_compress(jp2->j2k);
1090 opj_free(jp2->comps);
1099 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1103 if(!jp2 || !parameters || !image)
1106 /* setup the J2K codec */
1107 /* ------------------- */
1109 /* Check if number of components respects standard */
1110 if (image->numcomps < 1 || image->numcomps > 16384) {
1111 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1115 j2k_setup_encoder(jp2->j2k, parameters, image);
1117 /* setup the JP2 codec */
1118 /* ------------------- */
1122 jp2->brand = JP2_JP2; /* BR */
1123 jp2->minversion = 0; /* MinV */
1125 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1126 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1128 /* Image Header box */
1130 jp2->numcomps = image->numcomps; /* NC */
1131 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1132 jp2->h = image->y1 - image->y0; /* HEIGHT */
1133 jp2->w = image->x1 - image->x0; /* WIDTH */
1135 depth_0 = image->comps[0].prec - 1;
1136 sign = image->comps[0].sgnd;
1137 jp2->bpc = depth_0 + (sign << 7);
1138 for (i = 1; i < image->numcomps; i++) {
1139 int depth = image->comps[i].prec - 1;
1140 sign = image->comps[i].sgnd;
1141 if (depth_0 != depth)
1144 jp2->C = 7; /* C : Always 7 */
1145 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1146 jp2->IPR = 0; /* IPR, no intellectual property */
1148 /* BitsPerComponent box */
1150 for (i = 0; i < image->numcomps; i++) {
1151 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1154 if (image->color_space == 1)
1155 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
1156 else if (image->color_space == 2)
1157 jp2->enumcs = 17; /* greyscale */
1158 else if (image->color_space == 3)
1159 jp2->enumcs = 18; /* YUV */
1160 jp2->precedence = 0; /* PRECEDENCE */
1161 jp2->approx = 0; /* APPROX */
1163 jp2->jpip_on = parameters->jpip_on;
1166 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1168 int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
1169 pos_jp2c = pos_iptr = -1; /* remove a warning */
1173 /* JPEG 2000 Signature box */
1176 jp2_write_ftyp(jp2, cio);
1177 /* JP2 Header box */
1178 jp2_write_jp2h(jp2, cio);
1181 pos_iptr = cio_tell( cio);
1182 cio_skip( cio, 24); /* IPTR further ! */
1184 pos_jp2c = cio_tell( cio);
1188 if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
1189 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
1194 pos_cidx = cio_tell( cio);
1196 len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
1198 pos_fidx = cio_tell( cio);
1199 len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
1201 end_pos = cio_tell( cio);
1203 cio_seek( cio, pos_iptr);
1204 write_iptr( pos_fidx, len_fidx, cio);
1206 cio_seek( cio, end_pos);