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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
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 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 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 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);
60 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio);
62 Write the JP2H box - JP2 Header box
64 @param cio Output buffer stream
66 static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
68 Read the JP2H box - JP2 Header box
70 @param cio Input buffer stream
71 @return Returns true if successful, returns false otherwise
73 static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
75 Write the FTYP box - File type box
77 @param cio Output buffer stream
79 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
81 Read the FTYP box - File type box
83 @param cio Input buffer stream
84 @return Returns true if successful, returns false otherwise
86 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
87 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index);
88 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
89 static void jp2_write_jp(opj_cio_t *cio);
91 Read the JP box - JPEG 2000 signature
93 @param cio Input buffer stream
94 @return Returns true if successful, returns false otherwise
96 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
98 Decode the structure of a JP2 file
100 @param cio Input buffer stream
101 @return Returns true if successful, returns false otherwise
103 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio);
109 /* ----------------------------------------------------------------------- */
111 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
112 box->init_pos = cio_tell(cio);
113 box->length = cio_read(cio, 4);
114 box->type = cio_read(cio, 4);
115 if (box->length == 1) {
116 if (cio_read(cio, 4) != 0) {
117 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
120 box->length = cio_read(cio, 4);
121 if (box->length == 0)
122 box->length = cio_numbytesleft(cio) + 12;
124 else if (box->length == 0) {
125 box->length = cio_numbytesleft(cio) + 8;
132 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
136 box.init_pos = cio_tell(cio);
138 cio_write(cio, JP2_URL, 4); /* DBTL */
139 cio_write(cio, 0, 1); /* VERS */
140 cio_write(cio, 0, 3); /* FLAG */
143 for (i = 0; i < strlen(Idx_file); i++) {
144 cio_write(cio, Idx_file[i], 1);
148 box.length = cio_tell(cio) - box.init_pos;
149 cio_seek(cio, box.init_pos);
150 cio_write(cio, box.length, 4); /* L */
151 cio_seek(cio, box.init_pos + box.length);
155 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
158 opj_common_ptr cinfo = jp2->cinfo;
160 jp2_read_boxhdr(cinfo, cio, &box);
161 if (JP2_IHDR != box.type) {
162 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
166 jp2->h = cio_read(cio, 4); /* HEIGHT */
167 jp2->w = cio_read(cio, 4); /* WIDTH */
168 jp2->numcomps = cio_read(cio, 2); /* NC */
169 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
171 jp2->bpc = cio_read(cio, 1); /* BPC */
173 jp2->C = cio_read(cio, 1); /* C */
174 jp2->UnkC = cio_read(cio, 1); /* UnkC */
175 jp2->IPR = cio_read(cio, 1); /* IPR */
177 if (cio_tell(cio) - box.init_pos != box.length) {
178 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
185 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
188 box.init_pos = cio_tell(cio);
190 cio_write(cio, JP2_IHDR, 4); /* IHDR */
192 cio_write(cio, jp2->h, 4); /* HEIGHT */
193 cio_write(cio, jp2->w, 4); /* WIDTH */
194 cio_write(cio, jp2->numcomps, 2); /* NC */
196 cio_write(cio, jp2->bpc, 1); /* BPC */
198 cio_write(cio, jp2->C, 1); /* C : Always 7 */
199 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
200 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
202 box.length = cio_tell(cio) - box.init_pos;
203 cio_seek(cio, box.init_pos);
204 cio_write(cio, box.length, 4); /* L */
205 cio_seek(cio, box.init_pos + box.length);
208 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
212 box.init_pos = cio_tell(cio);
214 cio_write(cio, JP2_BPCC, 4); /* BPCC */
216 for (i = 0; i < jp2->numcomps; i++) {
217 cio_write(cio, jp2->comps[i].bpcc, 1);
220 box.length = cio_tell(cio) - box.init_pos;
221 cio_seek(cio, box.init_pos);
222 cio_write(cio, box.length, 4); /* L */
223 cio_seek(cio, box.init_pos + box.length);
227 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
231 opj_common_ptr cinfo = jp2->cinfo;
233 jp2_read_boxhdr(cinfo, cio, &box);
234 if (JP2_BPCC != box.type) {
235 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
239 for (i = 0; i < jp2->numcomps; i++) {
240 jp2->comps[i].bpcc = cio_read(cio, 1);
243 if (cio_tell(cio) - box.init_pos != box.length) {
244 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
251 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
254 box.init_pos = cio_tell(cio);
256 cio_write(cio, JP2_COLR, 4); /* COLR */
258 cio_write(cio, jp2->meth, 1); /* METH */
259 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
260 cio_write(cio, jp2->approx, 1); /* APPROX */
262 if (jp2->meth == 1) {
263 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
265 cio_write(cio, 0, 1); /* PROFILE (??) */
268 box.length = cio_tell(cio) - box.init_pos;
269 cio_seek(cio, box.init_pos);
270 cio_write(cio, box.length, 4); /* L */
271 cio_seek(cio, box.init_pos + box.length);
274 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
278 opj_common_ptr cinfo = jp2->cinfo;
280 jp2_read_boxhdr(cinfo, cio, &box);
282 if (JP2_COLR != box.type) {
283 cio_skip(cio, box.length - 8);
284 jp2_read_boxhdr(cinfo, cio, &box);
286 } while(JP2_COLR != box.type);
288 jp2->meth = cio_read(cio, 1); /* METH */
289 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
290 jp2->approx = cio_read(cio, 1); /* APPROX */
292 if (jp2->meth == 1) {
293 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
296 skip_len = box.init_pos + box.length - cio_tell(cio);
298 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n");
301 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
304 if (cio_tell(cio) - box.init_pos != box.length) {
305 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
311 static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
314 box.init_pos = cio_tell(cio);
316 cio_write(cio, JP2_JP2H, 4); /* JP2H */
318 jp2_write_ihdr(jp2, cio);
320 if (jp2->bpc == 255) {
321 jp2_write_bpcc(jp2, cio);
323 jp2_write_colr(jp2, cio);
325 box.length = cio_tell(cio) - box.init_pos;
326 cio_seek(cio, box.init_pos);
327 cio_write(cio, box.length, 4); /* L */
328 cio_seek(cio, box.init_pos + box.length);
331 static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
335 opj_common_ptr cinfo = jp2->cinfo;
337 jp2_read_boxhdr(cinfo, cio, &box);
339 if (JP2_JP2H != box.type) {
340 if (box.type == JP2_JP2C) {
341 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
344 cio_skip(cio, box.length - 8);
345 jp2_read_boxhdr(cinfo, cio, &box);
347 } while(JP2_JP2H != box.type);
349 if (!jp2_read_ihdr(jp2, cio))
352 if (jp2->bpc == 255) {
353 if (!jp2_read_bpcc(jp2, cio))
356 if (!jp2_read_colr(jp2, cio))
359 skip_len = box.init_pos + box.length - cio_tell(cio);
361 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n");
364 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
369 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
373 box.init_pos = cio_tell(cio);
375 cio_write(cio, JP2_FTYP, 4); /* FTYP */
377 cio_write(cio, jp2->brand, 4); /* BR */
378 cio_write(cio, jp2->minversion, 4); /* MinV */
380 for (i = 0; i < jp2->numcl; i++) {
381 cio_write(cio, jp2->cl[i], 4); /* CL */
384 box.length = cio_tell(cio) - box.init_pos;
385 cio_seek(cio, box.init_pos);
386 cio_write(cio, box.length, 4); /* L */
387 cio_seek(cio, box.init_pos + box.length);
390 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
394 opj_common_ptr cinfo = jp2->cinfo;
396 jp2_read_boxhdr(cinfo, cio, &box);
398 if (JP2_FTYP != box.type) {
399 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
403 jp2->brand = cio_read(cio, 4); /* BR */
404 jp2->minversion = cio_read(cio, 4); /* MinV */
405 jp2->numcl = (box.length - 16) / 4;
406 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
408 for (i = 0; i < (int)jp2->numcl; i++) {
409 jp2->cl[i] = cio_read(cio, 4); /* CLi */
412 if (cio_tell(cio) - box.init_pos != box.length) {
413 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
420 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
421 unsigned int j2k_codestream_offset, j2k_codestream_length;
424 opj_j2k_t *j2k = jp2->j2k;
426 box.init_pos = cio_tell(cio);
428 cio_write(cio, JP2_JP2C, 4); /* JP2C */
431 j2k_codestream_offset = cio_tell(cio);
432 if(!j2k_encode(j2k, cio, image, index)) {
433 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
436 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
438 jp2->j2k_codestream_offset = j2k_codestream_offset;
439 jp2->j2k_codestream_length = j2k_codestream_length;
441 box.length = 8 + jp2->j2k_codestream_length;
442 cio_seek(cio, box.init_pos);
443 cio_write(cio, box.length, 4); /* L */
444 cio_seek(cio, box.init_pos + box.length);
449 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
452 opj_common_ptr cinfo = jp2->cinfo;
454 jp2_read_boxhdr(cinfo, cio, &box);
456 if(JP2_JP2C != box.type) {
457 cio_skip(cio, box.length - 8);
458 jp2_read_boxhdr(cinfo, cio, &box);
460 } while(JP2_JP2C != box.type);
462 *j2k_codestream_offset = cio_tell(cio);
463 *j2k_codestream_length = box.length - 8;
468 static void jp2_write_jp(opj_cio_t *cio) {
471 box.init_pos = cio_tell(cio);
473 cio_write(cio, JP2_JP, 4); /* JP2 signature */
474 cio_write(cio, 0x0d0a870a, 4);
476 box.length = cio_tell(cio) - box.init_pos;
477 cio_seek(cio, box.init_pos);
478 cio_write(cio, box.length, 4); /* L */
479 cio_seek(cio, box.init_pos + box.length);
482 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
485 opj_common_ptr cinfo = jp2->cinfo;
487 jp2_read_boxhdr(cinfo, cio, &box);
488 if (JP2_JP != box.type) {
489 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
492 if (0x0d0a870a != cio_read(cio, 4)) {
493 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
496 if (cio_tell(cio) - box.init_pos != box.length) {
497 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
505 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) {
506 if (!jp2_read_jp(jp2, cio))
508 if (!jp2_read_ftyp(jp2, cio))
510 if (!jp2_read_jp2h(jp2, cio))
512 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
518 /* ----------------------------------------------------------------------- */
519 /* JP2 decoder interface */
520 /* ----------------------------------------------------------------------- */
522 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
523 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
526 /* create the J2K codec */
527 jp2->j2k = j2k_create_decompress(cinfo);
528 if(jp2->j2k == NULL) {
529 jp2_destroy_decompress(jp2);
536 void jp2_destroy_decompress(opj_jp2_t *jp2) {
538 /* destroy the J2K codec */
539 j2k_destroy_decompress(jp2->j2k);
542 opj_free(jp2->comps);
551 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
552 /* setup the J2K codec */
553 j2k_setup_decoder(jp2->j2k, parameters);
554 /* further JP2 initializations go here */
557 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) {
558 opj_common_ptr cinfo;
559 opj_image_t *image = NULL;
568 if(!jp2_read_struct(jp2, cio)) {
569 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
574 image = j2k_decode(jp2->j2k, cio);
576 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
582 /* ----------------------------------------------------------------------- */
583 /* JP2 encoder interface */
584 /* ----------------------------------------------------------------------- */
586 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
587 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
590 /* create the J2K codec */
591 jp2->j2k = j2k_create_compress(cinfo);
592 if(jp2->j2k == NULL) {
593 jp2_destroy_compress(jp2);
600 void jp2_destroy_compress(opj_jp2_t *jp2) {
602 /* destroy the J2K codec */
603 j2k_destroy_compress(jp2->j2k);
606 opj_free(jp2->comps);
615 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
619 if(!jp2 || !parameters || !image)
622 /* setup the J2K codec */
623 /* ------------------- */
625 /* Check if number of components respects standard */
626 if (image->numcomps < 1 || image->numcomps > 16384) {
627 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
631 j2k_setup_encoder(jp2->j2k, parameters, image);
633 /* setup the JP2 codec */
634 /* ------------------- */
638 jp2->brand = JP2_JP2; /* BR */
639 jp2->minversion = 0; /* MinV */
641 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
642 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
644 /* Image Header box */
646 jp2->numcomps = image->numcomps; /* NC */
647 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
648 jp2->h = image->y1 - image->y0; /* HEIGHT */
649 jp2->w = image->x1 - image->x0; /* WIDTH */
651 depth_0 = image->comps[0].prec - 1;
652 sign = image->comps[0].sgnd;
653 jp2->bpc = depth_0 + (sign << 7);
654 for (i = 1; i < image->numcomps; i++) {
655 int depth = image->comps[i].prec - 1;
656 sign = image->comps[i].sgnd;
657 if (depth_0 != depth)
660 jp2->C = 7; /* C : Always 7 */
661 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
662 jp2->IPR = 0; /* IPR, no intellectual property */
664 /* BitsPerComponent box */
666 for (i = 0; i < image->numcomps; i++) {
667 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
670 /* Colour Specification box */
672 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
673 jp2->meth = 1; /* METH: Enumerated colourspace */
675 jp2->meth = 2; /* METH: Restricted ICC profile */
677 if (jp2->meth == 1) {
678 if (image->color_space == 1)
679 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
680 else if (image->color_space == 2)
681 jp2->enumcs = 17; /* greyscale */
682 else if (image->color_space == 3)
683 jp2->enumcs = 18; /* YUV */
685 jp2->enumcs = 0; /* PROFILE (??) */
687 jp2->precedence = 0; /* PRECEDENCE */
688 jp2->approx = 0; /* APPROX */
692 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
696 /* JPEG 2000 Signature box */
699 jp2_write_ftyp(jp2, cio);
701 jp2_write_jp2h(jp2, cio);
705 if(!jp2_write_jp2c(jp2, cio, image, index)) {
706 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");