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"
35 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
38 /** @name Local static functions */
43 @param cinfo Codec context info
44 @param cio Input stream
46 @return Returns true if successful, returns false otherwise
48 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
49 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
51 Read the IHDR box - Image Header box
53 @param cio Input buffer stream
54 @return Returns true if successful, returns false otherwise
56 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
57 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
58 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
59 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
60 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
62 Write the FTYP box - File type box
64 @param cio Output buffer stream
66 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
68 Read the FTYP box - File type box
70 @param cio Input buffer stream
71 @return Returns true if successful, returns false otherwise
73 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
74 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
75 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
76 static void jp2_write_jp(opj_cio_t *cio);
78 Read the JP box - JPEG 2000 signature
80 @param cio Input buffer stream
81 @return Returns true if successful, returns false otherwise
83 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
85 Decode the structure of a JP2 file
87 @param cio Input buffer stream
88 @param color Collector for profile, cdef and pclr data
89 @return Returns true if successful, returns false otherwise
91 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
92 opj_jp2_color_t *color);
94 Apply collected palette data
95 @param color Collector for profile, cdef and pclr data
98 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image, opj_common_ptr cinfo);
101 @param jp2 JP2 handle
102 @param cio Input buffer stream
104 @param color Collector for profile, cdef and pclr data
105 @return Returns true if successful, returns false otherwise
107 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
108 opj_jp2_box_t *box, opj_jp2_color_t *color);
110 Collect component mapping data
111 @param jp2 JP2 handle
112 @param cio Input buffer stream
114 @param color Collector for profile, cdef and pclr data
115 @return Returns true if successful, returns false otherwise
117 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
118 opj_jp2_box_t *box, opj_jp2_color_t *color);
120 Collect colour specification data
121 @param jp2 JP2 handle
122 @param cio Input buffer stream
124 @param color Collector for profile, cdef and pclr data
125 @return Returns true if successful, returns false otherwise
127 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
128 opj_jp2_box_t *box, opj_jp2_color_t *color);
130 Write file Index (superbox)
131 @param[in] offset_jp2c offset of jp2c box
132 @param[in] length_jp2c length of jp2c box
133 @param[in] offset_idx offset of cidx box
134 @param[in] length_idx length of cidx box
135 @param[in] cio file output handle
136 @return length of fidx box
138 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
140 Write index Finder box
141 @param[in] offset offset of fidx box
142 @param[in] length length of fidx box
143 @param[in] cio file output handle
145 static void write_iptr( int offset, int length, opj_cio_t *cio);
148 @param[in] offset_jp2c offset of jp2c box
149 @param[in] length_jp2c length of jp2c box
150 @param[in] offset_idx offset of cidx box
151 @param[in] length_idx length of cidx box
152 @param[in] cio file output handle
154 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
159 /* ----------------------------------------------------------------------- */
161 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
162 box->init_pos = cio_tell(cio);
163 box->length = cio_read(cio, 4);
164 box->type = cio_read(cio, 4);
165 if (box->length == 1) {
166 if (cio_read(cio, 4) != 0) {
167 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
170 box->length = cio_read(cio, 4);
171 if (box->length == 0)
172 box->length = cio_numbytesleft(cio) + 12;
174 else if (box->length == 0) {
175 box->length = cio_numbytesleft(cio) + 8;
176 } else if (box->length < 0) {
177 opj_event_msg(cinfo, EVT_ERROR, "Integer overflow in box->length\n");
178 return OPJ_FALSE; /* TODO: actually check jp2_read_boxhdr's return value */
185 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
189 box.init_pos = cio_tell(cio);
191 cio_write(cio, JP2_URL, 4); /* DBTL */
192 cio_write(cio, 0, 1); /* VERS */
193 cio_write(cio, 0, 3); /* FLAG */
196 for (i = 0; i < strlen(Idx_file); i++) {
197 cio_write(cio, Idx_file[i], 1);
201 box.length = cio_tell(cio) - box.init_pos;
202 cio_seek(cio, box.init_pos);
203 cio_write(cio, box.length, 4); /* L */
204 cio_seek(cio, box.init_pos + box.length);
208 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
211 opj_common_ptr cinfo = jp2->cinfo;
213 if(jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
214 opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
217 if (JP2_IHDR != box.type) {
218 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
222 jp2->h = cio_read(cio, 4); /* HEIGHT */
223 jp2->w = cio_read(cio, 4); /* WIDTH */
224 jp2->numcomps = cio_read(cio, 2); /* NC */
225 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
227 jp2->bpc = cio_read(cio, 1); /* BPC */
229 jp2->C = cio_read(cio, 1); /* C */
230 jp2->UnkC = cio_read(cio, 1); /* UnkC */
231 jp2->IPR = cio_read(cio, 1); /* IPR */
233 if (cio_tell(cio) - box.init_pos != box.length) {
234 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
241 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
244 box.init_pos = cio_tell(cio);
246 cio_write(cio, JP2_IHDR, 4); /* IHDR */
248 cio_write(cio, jp2->h, 4); /* HEIGHT */
249 cio_write(cio, jp2->w, 4); /* WIDTH */
250 cio_write(cio, jp2->numcomps, 2); /* NC */
252 cio_write(cio, jp2->bpc, 1); /* BPC */
254 cio_write(cio, jp2->C, 1); /* C : Always 7 */
255 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
256 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
258 box.length = cio_tell(cio) - box.init_pos;
259 cio_seek(cio, box.init_pos);
260 cio_write(cio, box.length, 4); /* L */
261 cio_seek(cio, box.init_pos + box.length);
264 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
268 box.init_pos = cio_tell(cio);
270 cio_write(cio, JP2_BPCC, 4); /* BPCC */
272 for (i = 0; i < jp2->numcomps; i++) {
273 cio_write(cio, jp2->comps[i].bpcc, 1);
276 box.length = cio_tell(cio) - box.init_pos;
277 cio_seek(cio, box.init_pos);
278 cio_write(cio, box.length, 4); /* L */
279 cio_seek(cio, box.init_pos + box.length);
283 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
287 opj_common_ptr cinfo = jp2->cinfo;
289 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
290 opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
293 if (JP2_BPCC != box.type) {
294 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
298 for (i = 0; i < jp2->numcomps; i++) {
299 jp2->comps[i].bpcc = cio_read(cio, 1);
302 if (cio_tell(cio) - box.init_pos != box.length) {
303 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
310 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
313 box.init_pos = cio_tell(cio);
315 cio_write(cio, JP2_COLR, 4); /* COLR */
317 cio_write(cio, jp2->meth, 1); /* METH */
318 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
319 cio_write(cio, jp2->approx, 1); /* APPROX */
324 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
326 box.length = cio_tell(cio) - box.init_pos;
327 cio_seek(cio, box.init_pos);
328 cio_write(cio, box.length, 4); /* L */
329 cio_seek(cio, box.init_pos + box.length);
332 static void jp2_free_pclr(opj_jp2_color_t *color)
334 opj_free(color->jp2_pclr->channel_sign);
335 opj_free(color->jp2_pclr->channel_size);
336 opj_free(color->jp2_pclr->entries);
338 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
340 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
343 static void free_color_data(opj_jp2_color_t *color)
347 jp2_free_pclr(color);
351 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
352 opj_free(color->jp2_cdef);
354 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
357 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image, opj_common_ptr cinfo)
359 opj_image_comp_t *old_comps, *new_comps;
360 unsigned char *channel_size, *channel_sign;
361 unsigned int *entries;
362 opj_jp2_cmap_comp_t *cmap;
365 unsigned short i, nr_channels, cmp, pcol;
368 channel_size = color->jp2_pclr->channel_size;
369 channel_sign = color->jp2_pclr->channel_sign;
370 entries = color->jp2_pclr->entries;
371 cmap = color->jp2_pclr->cmap;
372 nr_channels = color->jp2_pclr->nr_channels;
374 old_comps = image->comps;
375 new_comps = (opj_image_comp_t*)
376 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
378 for(i = 0; i < nr_channels; ++i)
380 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
382 if( pcol < nr_channels )
383 new_comps[pcol] = old_comps[cmp];
386 opj_event_msg(cinfo, EVT_ERROR, "Error with pcol value %d (max: %d). skipping\n", pcol, nr_channels);
390 if(cmap[i].mtyp == 0) /* Direct use */
392 old_comps[cmp].data = NULL; continue;
394 /* Palette mapping: */
395 new_comps[pcol].data = (int*)
396 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
397 new_comps[pcol].prec = channel_size[i];
398 new_comps[pcol].sgnd = channel_sign[i];
400 top_k = color->jp2_pclr->nr_entries - 1;
402 for(i = 0; i < nr_channels; ++i)
405 if(cmap[i].mtyp == 0) continue;
407 /* Palette mapping: */
408 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
409 src = old_comps[cmp].data;
410 dst = new_comps[pcol].data;
411 max = new_comps[pcol].w * new_comps[pcol].h;
413 for(j = 0; j < max; ++j)
416 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
418 dst[j] = entries[k * nr_channels + pcol];
421 max = image->numcomps;
422 for(i = 0; i < max; ++i)
424 if(old_comps[i].data) opj_free(old_comps[i].data);
427 image->comps = new_comps;
428 image->numcomps = nr_channels;
430 jp2_free_pclr(color);
434 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
435 opj_jp2_box_t *box, opj_jp2_color_t *color)
437 opj_jp2_pclr_t *jp2_pclr;
438 unsigned char *channel_size, *channel_sign;
439 unsigned int *entries;
440 unsigned short nr_entries, nr_channels;
444 OPJ_ARG_NOT_USED(box);
445 OPJ_ARG_NOT_USED(jp2);
447 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
448 * a JP2 Header box' :
450 if(color->jp2_pclr) return OPJ_FALSE;
452 nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
453 nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
455 entries = (unsigned int*)
456 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
457 channel_size = (unsigned char*)opj_malloc(nr_channels);
458 channel_sign = (unsigned char*)opj_malloc(nr_channels);
460 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
461 jp2_pclr->channel_sign = channel_sign;
462 jp2_pclr->channel_size = channel_size;
463 jp2_pclr->entries = entries;
464 jp2_pclr->nr_entries = nr_entries;
465 jp2_pclr->nr_channels = nr_channels;
466 jp2_pclr->cmap = NULL;
468 color->jp2_pclr = jp2_pclr;
470 for(i = 0; i < nr_channels; ++i)
472 uc = cio_read(cio, 1); /* Bi */
473 channel_size[i] = (uc & 0x7f) + 1;
474 channel_sign[i] = (uc & 0x80)?1:0;
477 for(j = 0; j < nr_entries; ++j)
479 for(i = 0; i < nr_channels; ++i)
482 *entries++ = cio_read(cio, (channel_size[i]+7)>>3);
487 }/* jp2_read_pclr() */
489 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
490 opj_jp2_box_t *box, opj_jp2_color_t *color)
492 opj_jp2_cmap_comp_t *cmap;
493 unsigned short i, nr_channels;
495 OPJ_ARG_NOT_USED(box);
496 OPJ_ARG_NOT_USED(jp2);
498 /* Need nr_channels: */
499 if(color->jp2_pclr == NULL) return OPJ_FALSE;
501 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
502 * inside a JP2 Header box' :
504 if(color->jp2_pclr->cmap) return OPJ_FALSE;
506 nr_channels = color->jp2_pclr->nr_channels;
507 cmap = (opj_jp2_cmap_comp_t*)
508 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
510 for(i = 0; i < nr_channels; ++i)
512 cmap[i].cmp = (unsigned short)cio_read(cio, 2);
513 cmap[i].mtyp = cio_read(cio, 1);
514 cmap[i].pcol = cio_read(cio, 1);
517 color->jp2_pclr->cmap = cmap;
520 }/* jp2_read_cmap() */
522 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
524 opj_jp2_cdef_info_t *info;
525 unsigned short i, n, cn, typ, asoc, acn;
527 info = color->jp2_cdef->info;
528 n = color->jp2_cdef->n;
530 for(i = 0; i < n; ++i)
532 /* WATCH: acn = asoc - 1 ! */
533 if((asoc = info[i].asoc) == 0) continue;
535 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
540 opj_image_comp_t saved;
542 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
543 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
544 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
546 info[i].asoc = cn + 1;
547 info[acn].asoc = info[acn].cn + 1;
550 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
552 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
554 }/* jp2_apply_cdef() */
556 static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
557 opj_jp2_box_t *box, opj_jp2_color_t *color)
559 opj_jp2_cdef_info_t *info;
562 OPJ_ARG_NOT_USED(box);
563 OPJ_ARG_NOT_USED(jp2);
565 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
566 * inside a JP2 Header box.'
568 if(color->jp2_cdef) return OPJ_FALSE;
570 if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
572 info = (opj_jp2_cdef_info_t*)
573 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
575 color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
576 color->jp2_cdef->info = info;
577 color->jp2_cdef->n = n;
579 for(i = 0; i < n; ++i)
581 info[i].cn = (unsigned short)cio_read(cio, 2);
582 info[i].typ = (unsigned short)cio_read(cio, 2);
583 info[i].asoc = (unsigned short)cio_read(cio, 2);
587 }/* jp2_read_cdef() */
589 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
590 opj_jp2_box_t *box, opj_jp2_color_t *color)
593 opj_common_ptr cinfo;
595 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
596 * Specification boxes after the first.'
598 if(color->jp2_has_colr) return OPJ_FALSE;
602 jp2->meth = cio_read(cio, 1); /* METH */
603 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
604 jp2->approx = cio_read(cio, 1); /* APPROX */
608 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
614 skip_len = box->init_pos + box->length - cio_tell(cio);
617 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
622 unsigned char *start;
624 start = cio_getbp(cio);
625 color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
626 color->icc_profile_len = skip_len;
628 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
630 memcpy(color->icc_profile_buf, start, skip_len);
634 if (cio_tell(cio) - box->init_pos != box->length)
636 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
639 color->jp2_has_colr = 1;
642 }/* jp2_read_colr() */
644 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
649 opj_common_ptr cinfo = jp2->cinfo;
651 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
653 if (JP2_JP2H != box.type)
655 if (box.type == JP2_JP2C)
657 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
660 if (box.length <= 8) return OPJ_FALSE;
661 cio_skip(cio, box.length - 8);
663 if(cio->bp >= cio->end) return OPJ_FALSE;
665 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
667 } while(JP2_JP2H != box.type);
669 if (!jp2_read_ihdr(jp2, cio))
671 jp2h_end = box.init_pos + box.length;
675 if (!jp2_read_bpcc(jp2, cio))
678 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
680 while(cio_tell(cio) < jp2h_end)
682 if(box.type == JP2_COLR)
684 if( !jp2_read_colr(jp2, cio, &box, color))
686 if (box.length <= 8) return OPJ_FALSE;
687 cio_seek(cio, box.init_pos + 8);
688 cio_skip(cio, box.length - 8);
690 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
693 if(box.type == JP2_CDEF && !jp2->ignore_pclr_cmap_cdef)
695 if( !jp2_read_cdef(jp2, cio, &box, color))
697 if (box.length <= 8) return OPJ_FALSE;
698 cio_seek(cio, box.init_pos + 8);
699 cio_skip(cio, box.length - 8);
701 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
704 if(box.type == JP2_PCLR && !jp2->ignore_pclr_cmap_cdef)
706 if( !jp2_read_pclr(jp2, cio, &box, color))
708 if (box.length <= 8) return OPJ_FALSE;
709 cio_seek(cio, box.init_pos + 8);
710 cio_skip(cio, box.length - 8);
712 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
715 if(box.type == JP2_CMAP && !jp2->ignore_pclr_cmap_cdef)
717 if( !jp2_read_cmap(jp2, cio, &box, color))
719 if (box.length <= 8) return OPJ_FALSE;
720 cio_seek(cio, box.init_pos + 8);
721 cio_skip(cio, box.length - 8);
723 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
726 if (box.length <= 8) return OPJ_FALSE;
727 cio_seek(cio, box.init_pos + 8);
728 cio_skip(cio, box.length - 8);
729 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
731 }/* while(cio_tell(cio) < box_end) */
733 cio_seek(cio, jp2h_end);
735 /* Part 1, I.5.3.3 : 'must contain at least one' */
736 return (color->jp2_has_colr == 1);
738 }/* jp2_read_jp2h() */
740 static opj_bool opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, opj_common_ptr cinfo)
744 /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
745 if (color->jp2_cdef) {
746 opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
747 int n = color->jp2_cdef->n;
749 for (i = 0; i < n; i++) {
750 if (info[i].cn >= image->numcomps) {
751 opj_event_msg(cinfo, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].cn, image->numcomps);
754 if (info[i].asoc > 0 && (info[i].asoc - 1) >= image->numcomps) {
755 opj_event_msg(cinfo, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].asoc - 1, image->numcomps);
761 /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
762 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
763 if (color->jp2_pclr && color->jp2_pclr->cmap) {
764 int nr_channels = color->jp2_pclr->nr_channels;
765 opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
766 opj_bool *pcol_usage, is_sane = OPJ_TRUE;
768 /* verify that all original components match an existing one */
769 for (i = 0; i < nr_channels; i++) {
770 if (cmap[i].cmp >= image->numcomps) {
771 opj_event_msg(cinfo, EVT_ERROR, "Invalid component index %d (>= %d).\n", cmap[i].cmp, image->numcomps);
776 pcol_usage = opj_calloc(nr_channels, sizeof(opj_bool));
778 opj_event_msg(cinfo, EVT_ERROR, "Unexpected OOM.\n");
781 /* verify that no component is targeted more than once */
782 for (i = 0; i < nr_channels; i++) {
783 int pcol = cmap[i].pcol;
784 assert(cmap[i].mtyp == 0 || cmap[i].mtyp == 1);
785 if (pcol >= nr_channels) {
786 opj_event_msg(cinfo, EVT_ERROR, "Invalid component/palette index for direct mapping %d.\n", pcol);
789 else if (pcol_usage[pcol] && cmap[i].mtyp == 1) {
790 opj_event_msg(cinfo, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
793 else if (cmap[i].mtyp == 0 && cmap[i].pcol != 0) {
794 /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
795 * the value of this field shall be 0. */
796 opj_event_msg(cinfo, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i, pcol);
800 pcol_usage[pcol] = OPJ_TRUE;
802 /* verify that all components are targeted at least once */
803 for (i = 0; i < nr_channels; i++) {
804 if (!pcol_usage[i] && cmap[i].mtyp != 0) {
805 opj_event_msg(cinfo, EVT_ERROR, "Component %d doesn't have a mapping.\n", i);
809 opj_free(pcol_usage);
818 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
819 opj_codestream_info_t *cstr_info)
821 opj_common_ptr cinfo;
822 opj_image_t *image = NULL;
823 opj_jp2_color_t color;
829 memset(&color, 0, sizeof(opj_jp2_color_t));
833 if(!jp2_read_struct(jp2, cio, &color))
835 free_color_data(&color);
836 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
841 image = j2k_decode(jp2->j2k, cio, cstr_info);
845 free_color_data(&color);
846 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
850 if (!jp2->ignore_pclr_cmap_cdef){
851 if (!opj_jp2_check_color(image, &color, cinfo)) {
852 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode PCRL box\n");
856 /* Set Image Color Space */
857 if (jp2->enumcs == 16)
858 image->color_space = CLRSPC_SRGB;
859 else if (jp2->enumcs == 17)
860 image->color_space = CLRSPC_GRAY;
861 else if (jp2->enumcs == 18)
862 image->color_space = CLRSPC_SYCC;
864 image->color_space = CLRSPC_UNKNOWN;
868 jp2_apply_cdef(image, &color);
872 /* Part 1, I.5.3.4: Either both or none : */
873 if( !color.jp2_pclr->cmap)
874 jp2_free_pclr(&color);
876 jp2_apply_pclr(&color, image, cinfo);
878 if(color.icc_profile_buf)
880 image->icc_profile_buf = color.icc_profile_buf;
881 color.icc_profile_buf = NULL;
882 image->icc_profile_len = color.icc_profile_len;
888 }/* opj_jp2_decode() */
891 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
894 box.init_pos = cio_tell(cio);
896 cio_write(cio, JP2_JP2H, 4); /* JP2H */
898 jp2_write_ihdr(jp2, cio);
900 if (jp2->bpc == 255) {
901 jp2_write_bpcc(jp2, cio);
903 jp2_write_colr(jp2, cio);
905 box.length = cio_tell(cio) - box.init_pos;
906 cio_seek(cio, box.init_pos);
907 cio_write(cio, box.length, 4); /* L */
908 cio_seek(cio, box.init_pos + box.length);
911 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
915 box.init_pos = cio_tell(cio);
917 cio_write(cio, JP2_FTYP, 4); /* FTYP */
919 cio_write(cio, jp2->brand, 4); /* BR */
920 cio_write(cio, jp2->minversion, 4); /* MinV */
922 for (i = 0; i < jp2->numcl; i++) {
923 cio_write(cio, jp2->cl[i], 4); /* CL */
926 box.length = cio_tell(cio) - box.init_pos;
927 cio_seek(cio, box.init_pos);
928 cio_write(cio, box.length, 4); /* L */
929 cio_seek(cio, box.init_pos + box.length);
932 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
936 opj_common_ptr cinfo = jp2->cinfo;
938 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
939 opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
942 if (JP2_FTYP != box.type) {
943 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
947 jp2->brand = cio_read(cio, 4); /* BR */
948 jp2->minversion = cio_read(cio, 4); /* MinV */
949 jp2->numcl = (box.length - 16) / 4;
951 /* edf_c2_1673169.jp2 */
952 if (cio_numbytesleft(cio) < ((int)jp2->numcl * 4)) {
953 opj_event_msg(cinfo, EVT_ERROR, "Not enough bytes in FTYP Box "
954 "(expected %d, but only %d left)\n",
955 ((int)jp2->numcl * 4), cio_numbytesleft(cio));
958 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
960 for (i = 0; i < (int)jp2->numcl; i++) {
961 jp2->cl[i] = cio_read(cio, 4); /* CLi */
964 if (cio_tell(cio) - box.init_pos != box.length) {
965 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
972 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
973 unsigned int j2k_codestream_offset, j2k_codestream_length;
976 opj_j2k_t *j2k = jp2->j2k;
978 box.init_pos = cio_tell(cio);
980 cio_write(cio, JP2_JP2C, 4); /* JP2C */
983 j2k_codestream_offset = cio_tell(cio);
984 if(!j2k_encode(j2k, cio, image, cstr_info)) {
985 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
988 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
990 jp2->j2k_codestream_offset = j2k_codestream_offset;
991 jp2->j2k_codestream_length = j2k_codestream_length;
993 box.length = 8 + jp2->j2k_codestream_length;
994 cio_seek(cio, box.init_pos);
995 cio_write(cio, box.length, 4); /* L */
996 cio_seek(cio, box.init_pos + box.length);
1001 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
1004 opj_common_ptr cinfo = jp2->cinfo;
1006 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
1007 opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
1011 if(JP2_JP2C != box.type) {
1012 if (box.length <= 8) return OPJ_FALSE;
1013 cio_skip(cio, box.length - 8);
1014 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
1016 } while(JP2_JP2C != box.type);
1018 *j2k_codestream_offset = cio_tell(cio);
1019 if (box.length <= 8) return OPJ_FALSE;
1020 *j2k_codestream_length = box.length - 8;
1025 static void jp2_write_jp(opj_cio_t *cio) {
1028 box.init_pos = cio_tell(cio);
1030 cio_write(cio, JP2_JP, 4); /* JP2 signature */
1031 cio_write(cio, 0x0d0a870a, 4);
1033 box.length = cio_tell(cio) - box.init_pos;
1034 cio_seek(cio, box.init_pos);
1035 cio_write(cio, box.length, 4); /* L */
1036 cio_seek(cio, box.init_pos + box.length);
1039 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
1042 opj_common_ptr cinfo = jp2->cinfo;
1044 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
1045 opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
1048 if (JP2_JP != box.type) {
1049 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
1052 if (0x0d0a870a != cio_read(cio, 4)) {
1053 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
1056 if (cio_tell(cio) - box.init_pos != box.length) {
1057 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
1065 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
1066 opj_jp2_color_t *color) {
1067 if (!jp2_read_jp(jp2, cio))
1069 if (!jp2_read_ftyp(jp2, cio))
1071 if (!jp2_read_jp2h(jp2, cio, color))
1073 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
1080 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1084 lenp = cio_tell( cio);
1085 cio_skip( cio, 4); /* L [at the end] */
1086 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
1088 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
1090 len = cio_tell( cio)-lenp;
1091 cio_seek( cio, lenp);
1092 cio_write( cio, len, 4); /* L */
1093 cio_seek( cio, lenp+len);
1098 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1102 lenp = cio_tell( cio);
1103 cio_skip( cio, 4); /* L [at the end] */
1104 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
1106 cio_write( cio, offset_jp2c, 8); /* OOFF */
1107 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
1108 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
1110 cio_write( cio, 1,1); /* NI */
1112 cio_write( cio, offset_idx, 8); /* IOFF */
1113 cio_write( cio, length_idx, 4); /* IBH part 1 */
1114 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
1116 len = cio_tell( cio)-lenp;
1117 cio_seek( cio, lenp);
1118 cio_write( cio, len, 4); /* L */
1119 cio_seek( cio, lenp+len);
1122 static void write_iptr( int offset, int length, opj_cio_t *cio)
1126 lenp = cio_tell( cio);
1127 cio_skip( cio, 4); /* L [at the end] */
1128 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
1130 cio_write( cio, offset, 8);
1131 cio_write( cio, length, 8);
1133 len = cio_tell( cio)-lenp;
1134 cio_seek( cio, lenp);
1135 cio_write( cio, len, 4); /* L */
1136 cio_seek( cio, lenp+len);
1140 /* ----------------------------------------------------------------------- */
1141 /* JP2 decoder interface */
1142 /* ----------------------------------------------------------------------- */
1144 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
1145 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
1148 /* create the J2K codec */
1149 jp2->j2k = j2k_create_decompress(cinfo);
1150 if(jp2->j2k == NULL) {
1151 jp2_destroy_decompress(jp2);
1158 void jp2_destroy_decompress(opj_jp2_t *jp2) {
1160 /* destroy the J2K codec */
1161 j2k_destroy_decompress(jp2->j2k);
1164 opj_free(jp2->comps);
1173 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
1174 /* setup the J2K codec */
1175 j2k_setup_decoder(jp2->j2k, parameters);
1176 /* further JP2 initializations go here */
1177 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1180 /* ----------------------------------------------------------------------- */
1181 /* JP2 encoder interface */
1182 /* ----------------------------------------------------------------------- */
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));
1187 memset(jp2, 0, sizeof(opj_jp2_t));
1189 /* create the J2K codec */
1190 jp2->j2k = j2k_create_compress(cinfo);
1191 if(jp2->j2k == NULL) {
1192 jp2_destroy_compress(jp2);
1199 void jp2_destroy_compress(opj_jp2_t *jp2) {
1201 /* destroy the J2K codec */
1202 j2k_destroy_compress(jp2->j2k);
1205 opj_free(jp2->comps);
1214 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1218 if(!jp2 || !parameters || !image)
1221 /* setup the J2K codec */
1222 /* ------------------- */
1224 /* Check if number of components respects standard */
1225 if (image->numcomps < 1 || image->numcomps > 16384) {
1226 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1230 j2k_setup_encoder(jp2->j2k, parameters, image);
1232 /* setup the JP2 codec */
1233 /* ------------------- */
1237 jp2->brand = JP2_JP2; /* BR */
1238 jp2->minversion = 0; /* MinV */
1240 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1241 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1243 /* Image Header box */
1245 jp2->numcomps = image->numcomps; /* NC */
1246 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1247 jp2->h = image->y1 - image->y0; /* HEIGHT */
1248 jp2->w = image->x1 - image->x0; /* WIDTH */
1250 depth_0 = image->comps[0].prec - 1;
1251 sign = image->comps[0].sgnd;
1252 jp2->bpc = depth_0 + (sign << 7);
1253 for (i = 1; i < image->numcomps; i++) {
1254 int depth = image->comps[i].prec - 1;
1255 sign = image->comps[i].sgnd;
1256 if (depth_0 != depth)
1259 jp2->C = 7; /* C : Always 7 */
1260 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1261 jp2->IPR = 0; /* IPR, no intellectual property */
1263 /* BitsPerComponent box */
1265 for (i = 0; i < image->numcomps; i++) {
1266 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1269 if (image->color_space == 1)
1270 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
1271 else if (image->color_space == 2)
1272 jp2->enumcs = 17; /* greyscale */
1273 else if (image->color_space == 3)
1274 jp2->enumcs = 18; /* YUV */
1275 jp2->precedence = 0; /* PRECEDENCE */
1276 jp2->approx = 0; /* APPROX */
1278 jp2->jpip_on = parameters->jpip_on;
1281 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1283 int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
1284 pos_jp2c = pos_iptr = -1; /* remove a warning */
1288 /* JPEG 2000 Signature box */
1291 jp2_write_ftyp(jp2, cio);
1292 /* JP2 Header box */
1293 jp2_write_jp2h(jp2, cio);
1296 pos_iptr = cio_tell( cio);
1297 cio_skip( cio, 24); /* IPTR further ! */
1299 pos_jp2c = cio_tell( cio);
1303 if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
1304 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
1309 pos_cidx = cio_tell( cio);
1311 len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
1313 pos_fidx = cio_tell( cio);
1314 len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
1316 end_pos = cio_tell( cio);
1318 cio_seek( cio, pos_iptr);
1319 write_iptr( pos_fidx, len_fidx, cio);
1321 cio_seek( cio, end_pos);