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;
176 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;
526 unsigned short i, n, cn, typ, asoc, acn;
528 color_space = image->color_space;
529 info = color->jp2_cdef->info;
530 n = color->jp2_cdef->n;
532 for(i = 0; i < n; ++i)
534 /* WATCH: acn = asoc - 1 ! */
535 if((asoc = info[i].asoc) == 0) continue;
537 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
541 opj_image_comp_t saved;
543 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
544 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
545 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
547 info[i].asoc = cn + 1;
548 info[acn].asoc = info[acn].cn + 1;
551 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
553 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
555 }/* jp2_apply_cdef() */
557 static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
558 opj_jp2_box_t *box, opj_jp2_color_t *color)
560 opj_jp2_cdef_info_t *info;
563 OPJ_ARG_NOT_USED(box);
564 OPJ_ARG_NOT_USED(jp2);
566 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
567 * inside a JP2 Header box.'
569 if(color->jp2_cdef) return OPJ_FALSE;
571 if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
573 info = (opj_jp2_cdef_info_t*)
574 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
576 color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
577 color->jp2_cdef->info = info;
578 color->jp2_cdef->n = n;
580 for(i = 0; i < n; ++i)
582 info[i].cn = (unsigned short)cio_read(cio, 2);
583 info[i].typ = (unsigned short)cio_read(cio, 2);
584 info[i].asoc = (unsigned short)cio_read(cio, 2);
588 }/* jp2_read_cdef() */
590 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
591 opj_jp2_box_t *box, opj_jp2_color_t *color)
594 opj_common_ptr cinfo;
596 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
597 * Specification boxes after the first.'
599 if(color->jp2_has_colr) return OPJ_FALSE;
603 jp2->meth = cio_read(cio, 1); /* METH */
604 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
605 jp2->approx = cio_read(cio, 1); /* APPROX */
609 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
615 skip_len = box->init_pos + box->length - cio_tell(cio);
618 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
623 unsigned char *start;
625 start = cio_getbp(cio);
626 color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
627 color->icc_profile_len = skip_len;
629 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
631 memcpy(color->icc_profile_buf, start, skip_len);
635 if (cio_tell(cio) - box->init_pos != box->length)
637 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
640 color->jp2_has_colr = 1;
643 }/* jp2_read_colr() */
645 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
650 opj_common_ptr cinfo = jp2->cinfo;
652 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
654 if (JP2_JP2H != box.type)
656 if (box.type == JP2_JP2C)
658 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
661 if (box.length <= 8) return OPJ_FALSE;
662 cio_skip(cio, box.length - 8);
664 if(cio->bp >= cio->end) return OPJ_FALSE;
666 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
668 } while(JP2_JP2H != box.type);
670 if (!jp2_read_ihdr(jp2, cio))
672 jp2h_end = box.init_pos + box.length;
676 if (!jp2_read_bpcc(jp2, cio))
679 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
681 while(cio_tell(cio) < jp2h_end)
683 if(box.type == JP2_COLR)
685 if( !jp2_read_colr(jp2, cio, &box, color))
687 if (box.length <= 8) return OPJ_FALSE;
688 cio_seek(cio, box.init_pos + 8);
689 cio_skip(cio, box.length - 8);
691 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
694 if(box.type == JP2_CDEF && !jp2->ignore_pclr_cmap_cdef)
696 if( !jp2_read_cdef(jp2, cio, &box, color))
698 if (box.length <= 8) return OPJ_FALSE;
699 cio_seek(cio, box.init_pos + 8);
700 cio_skip(cio, box.length - 8);
702 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
705 if(box.type == JP2_PCLR && !jp2->ignore_pclr_cmap_cdef)
707 if( !jp2_read_pclr(jp2, cio, &box, color))
709 if (box.length <= 8) return OPJ_FALSE;
710 cio_seek(cio, box.init_pos + 8);
711 cio_skip(cio, box.length - 8);
713 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
716 if(box.type == JP2_CMAP && !jp2->ignore_pclr_cmap_cdef)
718 if( !jp2_read_cmap(jp2, cio, &box, color))
720 if (box.length <= 8) return OPJ_FALSE;
721 cio_seek(cio, box.init_pos + 8);
722 cio_skip(cio, box.length - 8);
724 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
727 if (box.length <= 8) return OPJ_FALSE;
728 cio_seek(cio, box.init_pos + 8);
729 cio_skip(cio, box.length - 8);
730 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
732 }/* while(cio_tell(cio) < box_end) */
734 cio_seek(cio, jp2h_end);
736 /* Part 1, I.5.3.3 : 'must contain at least one' */
737 return (color->jp2_has_colr == 1);
739 }/* jp2_read_jp2h() */
741 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
742 opj_codestream_info_t *cstr_info)
744 opj_common_ptr cinfo;
745 opj_image_t *image = NULL;
746 opj_jp2_color_t color;
752 memset(&color, 0, sizeof(opj_jp2_color_t));
756 if(!jp2_read_struct(jp2, cio, &color))
758 free_color_data(&color);
759 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
764 image = j2k_decode(jp2->j2k, cio, cstr_info);
768 free_color_data(&color);
769 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
773 if (!jp2->ignore_pclr_cmap_cdef){
775 /* Set Image Color Space */
776 if (jp2->enumcs == 16)
777 image->color_space = CLRSPC_SRGB;
778 else if (jp2->enumcs == 17)
779 image->color_space = CLRSPC_GRAY;
780 else if (jp2->enumcs == 18)
781 image->color_space = CLRSPC_SYCC;
783 image->color_space = CLRSPC_UNKNOWN;
787 jp2_apply_cdef(image, &color);
791 /* Part 1, I.5.3.4: Either both or none : */
792 if( !color.jp2_pclr->cmap)
793 jp2_free_pclr(&color);
795 jp2_apply_pclr(&color, image, cinfo);
797 if(color.icc_profile_buf)
799 image->icc_profile_buf = color.icc_profile_buf;
800 color.icc_profile_buf = NULL;
801 image->icc_profile_len = color.icc_profile_len;
807 }/* opj_jp2_decode() */
810 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
813 box.init_pos = cio_tell(cio);
815 cio_write(cio, JP2_JP2H, 4); /* JP2H */
817 jp2_write_ihdr(jp2, cio);
819 if (jp2->bpc == 255) {
820 jp2_write_bpcc(jp2, cio);
822 jp2_write_colr(jp2, cio);
824 box.length = cio_tell(cio) - box.init_pos;
825 cio_seek(cio, box.init_pos);
826 cio_write(cio, box.length, 4); /* L */
827 cio_seek(cio, box.init_pos + box.length);
830 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
834 box.init_pos = cio_tell(cio);
836 cio_write(cio, JP2_FTYP, 4); /* FTYP */
838 cio_write(cio, jp2->brand, 4); /* BR */
839 cio_write(cio, jp2->minversion, 4); /* MinV */
841 for (i = 0; i < jp2->numcl; i++) {
842 cio_write(cio, jp2->cl[i], 4); /* CL */
845 box.length = cio_tell(cio) - box.init_pos;
846 cio_seek(cio, box.init_pos);
847 cio_write(cio, box.length, 4); /* L */
848 cio_seek(cio, box.init_pos + box.length);
851 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
855 opj_common_ptr cinfo = jp2->cinfo;
857 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
858 opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
861 if (JP2_FTYP != box.type) {
862 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
866 jp2->brand = cio_read(cio, 4); /* BR */
867 jp2->minversion = cio_read(cio, 4); /* MinV */
868 jp2->numcl = (box.length - 16) / 4;
869 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
871 for (i = 0; i < (int)jp2->numcl; i++) {
872 jp2->cl[i] = cio_read(cio, 4); /* CLi */
875 if (cio_tell(cio) - box.init_pos != box.length) {
876 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
883 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
884 unsigned int j2k_codestream_offset, j2k_codestream_length;
887 opj_j2k_t *j2k = jp2->j2k;
889 box.init_pos = cio_tell(cio);
891 cio_write(cio, JP2_JP2C, 4); /* JP2C */
894 j2k_codestream_offset = cio_tell(cio);
895 if(!j2k_encode(j2k, cio, image, cstr_info)) {
896 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
899 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
901 jp2->j2k_codestream_offset = j2k_codestream_offset;
902 jp2->j2k_codestream_length = j2k_codestream_length;
904 box.length = 8 + jp2->j2k_codestream_length;
905 cio_seek(cio, box.init_pos);
906 cio_write(cio, box.length, 4); /* L */
907 cio_seek(cio, box.init_pos + box.length);
912 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
915 opj_common_ptr cinfo = jp2->cinfo;
917 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
918 opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
922 if(JP2_JP2C != box.type) {
923 if (box.length <= 8) return OPJ_FALSE;
924 cio_skip(cio, box.length - 8);
925 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
927 } while(JP2_JP2C != box.type);
929 *j2k_codestream_offset = cio_tell(cio);
930 if (box.length <= 8) return OPJ_FALSE;
931 *j2k_codestream_length = box.length - 8;
936 static void jp2_write_jp(opj_cio_t *cio) {
939 box.init_pos = cio_tell(cio);
941 cio_write(cio, JP2_JP, 4); /* JP2 signature */
942 cio_write(cio, 0x0d0a870a, 4);
944 box.length = cio_tell(cio) - box.init_pos;
945 cio_seek(cio, box.init_pos);
946 cio_write(cio, box.length, 4); /* L */
947 cio_seek(cio, box.init_pos + box.length);
950 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
953 opj_common_ptr cinfo = jp2->cinfo;
955 if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
956 opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
959 if (JP2_JP != box.type) {
960 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
963 if (0x0d0a870a != cio_read(cio, 4)) {
964 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
967 if (cio_tell(cio) - box.init_pos != box.length) {
968 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
976 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
977 opj_jp2_color_t *color) {
978 if (!jp2_read_jp(jp2, cio))
980 if (!jp2_read_ftyp(jp2, cio))
982 if (!jp2_read_jp2h(jp2, cio, color))
984 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
991 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
995 lenp = cio_tell( cio);
996 cio_skip( cio, 4); /* L [at the end] */
997 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
999 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
1001 len = cio_tell( cio)-lenp;
1002 cio_seek( cio, lenp);
1003 cio_write( cio, len, 4); /* L */
1004 cio_seek( cio, lenp+len);
1009 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1013 lenp = cio_tell( cio);
1014 cio_skip( cio, 4); /* L [at the end] */
1015 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
1017 cio_write( cio, offset_jp2c, 8); /* OOFF */
1018 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
1019 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
1021 cio_write( cio, 1,1); /* NI */
1023 cio_write( cio, offset_idx, 8); /* IOFF */
1024 cio_write( cio, length_idx, 4); /* IBH part 1 */
1025 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
1027 len = cio_tell( cio)-lenp;
1028 cio_seek( cio, lenp);
1029 cio_write( cio, len, 4); /* L */
1030 cio_seek( cio, lenp+len);
1033 static void write_iptr( int offset, int length, opj_cio_t *cio)
1037 lenp = cio_tell( cio);
1038 cio_skip( cio, 4); /* L [at the end] */
1039 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
1041 cio_write( cio, offset, 8);
1042 cio_write( cio, length, 8);
1044 len = cio_tell( cio)-lenp;
1045 cio_seek( cio, lenp);
1046 cio_write( cio, len, 4); /* L */
1047 cio_seek( cio, lenp+len);
1051 /* ----------------------------------------------------------------------- */
1052 /* JP2 decoder interface */
1053 /* ----------------------------------------------------------------------- */
1055 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
1056 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
1059 /* create the J2K codec */
1060 jp2->j2k = j2k_create_decompress(cinfo);
1061 if(jp2->j2k == NULL) {
1062 jp2_destroy_decompress(jp2);
1069 void jp2_destroy_decompress(opj_jp2_t *jp2) {
1071 /* destroy the J2K codec */
1072 j2k_destroy_decompress(jp2->j2k);
1075 opj_free(jp2->comps);
1084 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
1085 /* setup the J2K codec */
1086 j2k_setup_decoder(jp2->j2k, parameters);
1087 /* further JP2 initializations go here */
1088 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1091 /* ----------------------------------------------------------------------- */
1092 /* JP2 encoder interface */
1093 /* ----------------------------------------------------------------------- */
1095 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
1096 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1098 memset(jp2, 0, sizeof(opj_jp2_t));
1100 /* create the J2K codec */
1101 jp2->j2k = j2k_create_compress(cinfo);
1102 if(jp2->j2k == NULL) {
1103 jp2_destroy_compress(jp2);
1110 void jp2_destroy_compress(opj_jp2_t *jp2) {
1112 /* destroy the J2K codec */
1113 j2k_destroy_compress(jp2->j2k);
1116 opj_free(jp2->comps);
1125 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1129 if(!jp2 || !parameters || !image)
1132 /* setup the J2K codec */
1133 /* ------------------- */
1135 /* Check if number of components respects standard */
1136 if (image->numcomps < 1 || image->numcomps > 16384) {
1137 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1141 j2k_setup_encoder(jp2->j2k, parameters, image);
1143 /* setup the JP2 codec */
1144 /* ------------------- */
1148 jp2->brand = JP2_JP2; /* BR */
1149 jp2->minversion = 0; /* MinV */
1151 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1152 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1154 /* Image Header box */
1156 jp2->numcomps = image->numcomps; /* NC */
1157 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1158 jp2->h = image->y1 - image->y0; /* HEIGHT */
1159 jp2->w = image->x1 - image->x0; /* WIDTH */
1161 depth_0 = image->comps[0].prec - 1;
1162 sign = image->comps[0].sgnd;
1163 jp2->bpc = depth_0 + (sign << 7);
1164 for (i = 1; i < image->numcomps; i++) {
1165 int depth = image->comps[i].prec - 1;
1166 sign = image->comps[i].sgnd;
1167 if (depth_0 != depth)
1170 jp2->C = 7; /* C : Always 7 */
1171 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1172 jp2->IPR = 0; /* IPR, no intellectual property */
1174 /* BitsPerComponent box */
1176 for (i = 0; i < image->numcomps; i++) {
1177 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1180 if (image->color_space == 1)
1181 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
1182 else if (image->color_space == 2)
1183 jp2->enumcs = 17; /* greyscale */
1184 else if (image->color_space == 3)
1185 jp2->enumcs = 18; /* YUV */
1186 jp2->precedence = 0; /* PRECEDENCE */
1187 jp2->approx = 0; /* APPROX */
1189 jp2->jpip_on = parameters->jpip_on;
1192 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1194 int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
1195 pos_jp2c = pos_iptr = -1; /* remove a warning */
1199 /* JPEG 2000 Signature box */
1202 jp2_write_ftyp(jp2, cio);
1203 /* JP2 Header box */
1204 jp2_write_jp2h(jp2, cio);
1207 pos_iptr = cio_tell( cio);
1208 cio_skip( cio, 24); /* IPTR further ! */
1210 pos_jp2c = cio_tell( cio);
1214 if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
1215 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
1220 pos_cidx = cio_tell( cio);
1222 len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
1224 pos_fidx = cio_tell( cio);
1225 len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
1227 end_pos = cio_tell( cio);
1229 cio_seek( cio, pos_iptr);
1230 write_iptr( pos_fidx, len_fidx, cio);
1232 cio_seek( cio, end_pos);