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 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 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, char *index);
75 static 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 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 @return Returns true if successful, returns false otherwise
90 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio);
96 /* ----------------------------------------------------------------------- */
98 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
99 box->init_pos = cio_tell(cio);
100 box->length = cio_read(cio, 4);
101 box->type = cio_read(cio, 4);
102 if (box->length == 1) {
103 if (cio_read(cio, 4) != 0) {
104 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
107 box->length = cio_read(cio, 4);
108 if (box->length == 0)
109 box->length = cio_numbytesleft(cio) + 12;
111 else if (box->length == 0) {
112 box->length = cio_numbytesleft(cio) + 8;
119 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
123 box.init_pos = cio_tell(cio);
125 cio_write(cio, JP2_URL, 4); /* DBTL */
126 cio_write(cio, 0, 1); /* VERS */
127 cio_write(cio, 0, 3); /* FLAG */
130 for (i = 0; i < strlen(Idx_file); i++) {
131 cio_write(cio, Idx_file[i], 1);
135 box.length = cio_tell(cio) - box.init_pos;
136 cio_seek(cio, box.init_pos);
137 cio_write(cio, box.length, 4); /* L */
138 cio_seek(cio, box.init_pos + box.length);
142 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
145 opj_common_ptr cinfo = jp2->cinfo;
147 jp2_read_boxhdr(cinfo, cio, &box);
148 if (JP2_IHDR != box.type) {
149 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
153 jp2->h = cio_read(cio, 4); /* HEIGHT */
154 jp2->w = cio_read(cio, 4); /* WIDTH */
155 jp2->numcomps = cio_read(cio, 2); /* NC */
156 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
158 jp2->bpc = cio_read(cio, 1); /* BPC */
160 jp2->C = cio_read(cio, 1); /* C */
161 jp2->UnkC = cio_read(cio, 1); /* UnkC */
162 jp2->IPR = cio_read(cio, 1); /* IPR */
164 if (cio_tell(cio) - box.init_pos != box.length) {
165 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
172 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
175 box.init_pos = cio_tell(cio);
177 cio_write(cio, JP2_IHDR, 4); /* IHDR */
179 cio_write(cio, jp2->h, 4); /* HEIGHT */
180 cio_write(cio, jp2->w, 4); /* WIDTH */
181 cio_write(cio, jp2->numcomps, 2); /* NC */
183 cio_write(cio, jp2->bpc, 1); /* BPC */
185 cio_write(cio, jp2->C, 1); /* C : Always 7 */
186 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
187 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
189 box.length = cio_tell(cio) - box.init_pos;
190 cio_seek(cio, box.init_pos);
191 cio_write(cio, box.length, 4); /* L */
192 cio_seek(cio, box.init_pos + box.length);
195 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
199 box.init_pos = cio_tell(cio);
201 cio_write(cio, JP2_BPCC, 4); /* BPCC */
203 for (i = 0; i < jp2->numcomps; i++) {
204 cio_write(cio, jp2->comps[i].bpcc, 1);
207 box.length = cio_tell(cio) - box.init_pos;
208 cio_seek(cio, box.init_pos);
209 cio_write(cio, box.length, 4); /* L */
210 cio_seek(cio, box.init_pos + box.length);
214 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
218 opj_common_ptr cinfo = jp2->cinfo;
220 jp2_read_boxhdr(cinfo, cio, &box);
221 if (JP2_BPCC != box.type) {
222 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
226 for (i = 0; i < jp2->numcomps; i++) {
227 jp2->comps[i].bpcc = cio_read(cio, 1);
230 if (cio_tell(cio) - box.init_pos != box.length) {
231 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
238 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
241 box.init_pos = cio_tell(cio);
243 cio_write(cio, JP2_COLR, 4); /* COLR */
245 cio_write(cio, jp2->meth, 1); /* METH */
246 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
247 cio_write(cio, jp2->approx, 1); /* APPROX */
249 if (jp2->meth == 1) {
250 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
252 cio_write(cio, 0, 1); /* PROFILE (??) */
255 box.length = cio_tell(cio) - box.init_pos;
256 cio_seek(cio, box.init_pos);
257 cio_write(cio, box.length, 4); /* L */
258 cio_seek(cio, box.init_pos + box.length);
261 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
265 opj_common_ptr cinfo = jp2->cinfo;
267 jp2_read_boxhdr(cinfo, cio, &box);
269 if (JP2_COLR != box.type) {
270 cio_skip(cio, box.length - 8);
271 jp2_read_boxhdr(cinfo, cio, &box);
273 } while(JP2_COLR != box.type);
275 jp2->meth = cio_read(cio, 1); /* METH */
276 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
277 jp2->approx = cio_read(cio, 1); /* APPROX */
279 if (jp2->meth == 1) {
280 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
283 skip_len = box.init_pos + box.length - cio_tell(cio);
285 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n");
288 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
291 if (cio_tell(cio) - box.init_pos != box.length) {
292 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
298 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
301 box.init_pos = cio_tell(cio);
303 cio_write(cio, JP2_JP2H, 4); /* JP2H */
305 jp2_write_ihdr(jp2, cio);
307 if (jp2->bpc == 255) {
308 jp2_write_bpcc(jp2, cio);
310 jp2_write_colr(jp2, cio);
312 box.length = cio_tell(cio) - box.init_pos;
313 cio_seek(cio, box.init_pos);
314 cio_write(cio, box.length, 4); /* L */
315 cio_seek(cio, box.init_pos + box.length);
318 bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
322 opj_common_ptr cinfo = jp2->cinfo;
324 jp2_read_boxhdr(cinfo, cio, &box);
326 if (JP2_JP2H != box.type) {
327 if (box.type == JP2_JP2C) {
328 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
331 cio_skip(cio, box.length - 8);
332 jp2_read_boxhdr(cinfo, cio, &box);
334 } while(JP2_JP2H != box.type);
336 if (!jp2_read_ihdr(jp2, cio))
339 if (jp2->bpc == 255) {
340 if (!jp2_read_bpcc(jp2, cio))
343 if (!jp2_read_colr(jp2, cio))
346 skip_len = box.init_pos + box.length - cio_tell(cio);
348 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n");
351 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
356 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
360 box.init_pos = cio_tell(cio);
362 cio_write(cio, JP2_FTYP, 4); /* FTYP */
364 cio_write(cio, jp2->brand, 4); /* BR */
365 cio_write(cio, jp2->minversion, 4); /* MinV */
367 for (i = 0; i < jp2->numcl; i++) {
368 cio_write(cio, jp2->cl[i], 4); /* CL */
371 box.length = cio_tell(cio) - box.init_pos;
372 cio_seek(cio, box.init_pos);
373 cio_write(cio, box.length, 4); /* L */
374 cio_seek(cio, box.init_pos + box.length);
377 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
381 opj_common_ptr cinfo = jp2->cinfo;
383 jp2_read_boxhdr(cinfo, cio, &box);
385 if (JP2_FTYP != box.type) {
386 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
390 jp2->brand = cio_read(cio, 4); /* BR */
391 jp2->minversion = cio_read(cio, 4); /* MinV */
392 jp2->numcl = (box.length - 16) / 4;
393 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
395 for (i = 0; i < (int)jp2->numcl; i++) {
396 jp2->cl[i] = cio_read(cio, 4); /* CLi */
399 if (cio_tell(cio) - box.init_pos != box.length) {
400 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
407 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
408 unsigned int j2k_codestream_offset, j2k_codestream_length;
411 opj_j2k_t *j2k = jp2->j2k;
413 box.init_pos = cio_tell(cio);
415 cio_write(cio, JP2_JP2C, 4); /* JP2C */
418 j2k_codestream_offset = cio_tell(cio);
419 if(!j2k_encode(j2k, cio, image, index)) {
420 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
423 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
425 jp2->j2k_codestream_offset = j2k_codestream_offset;
426 jp2->j2k_codestream_length = j2k_codestream_length;
428 box.length = 8 + jp2->j2k_codestream_length;
429 cio_seek(cio, box.init_pos);
430 cio_write(cio, box.length, 4); /* L */
431 cio_seek(cio, box.init_pos + box.length);
436 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
439 opj_common_ptr cinfo = jp2->cinfo;
441 jp2_read_boxhdr(cinfo, cio, &box);
443 if(JP2_JP2C != box.type) {
444 cio_skip(cio, box.length - 8);
445 jp2_read_boxhdr(cinfo, cio, &box);
447 } while(JP2_JP2C != box.type);
449 *j2k_codestream_offset = cio_tell(cio);
450 *j2k_codestream_length = box.length - 8;
455 static void jp2_write_jp(opj_cio_t *cio) {
458 box.init_pos = cio_tell(cio);
460 cio_write(cio, JP2_JP, 4); /* JP2 signature */
461 cio_write(cio, 0x0d0a870a, 4);
463 box.length = cio_tell(cio) - box.init_pos;
464 cio_seek(cio, box.init_pos);
465 cio_write(cio, box.length, 4); /* L */
466 cio_seek(cio, box.init_pos + box.length);
469 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
472 opj_common_ptr cinfo = jp2->cinfo;
474 jp2_read_boxhdr(cinfo, cio, &box);
475 if (JP2_JP != box.type) {
476 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
479 if (0x0d0a870a != cio_read(cio, 4)) {
480 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
483 if (cio_tell(cio) - box.init_pos != box.length) {
484 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
492 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) {
493 if (!jp2_read_jp(jp2, cio))
495 if (!jp2_read_ftyp(jp2, cio))
497 if (!jp2_read_jp2h(jp2, cio))
499 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
505 /* ----------------------------------------------------------------------- */
506 /* JP2 decoder interface */
507 /* ----------------------------------------------------------------------- */
509 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
510 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
513 /* create the J2K codec */
514 jp2->j2k = j2k_create_decompress(cinfo);
515 if(jp2->j2k == NULL) {
516 jp2_destroy_decompress(jp2);
523 void jp2_destroy_decompress(opj_jp2_t *jp2) {
525 /* destroy the J2K codec */
526 j2k_destroy_decompress(jp2->j2k);
529 opj_free(jp2->comps);
538 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
539 /* setup the J2K codec */
540 j2k_setup_decoder(jp2->j2k, parameters);
541 /* further JP2 initializations go here */
544 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) {
545 opj_common_ptr cinfo;
546 opj_image_t *image = NULL;
555 if(!jp2_read_struct(jp2, cio)) {
556 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
561 image = j2k_decode(jp2->j2k, cio);
563 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
569 /* ----------------------------------------------------------------------- */
570 /* JP2 encoder interface */
571 /* ----------------------------------------------------------------------- */
573 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
574 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
577 /* create the J2K codec */
578 jp2->j2k = j2k_create_compress(cinfo);
579 if(jp2->j2k == NULL) {
580 jp2_destroy_compress(jp2);
587 void jp2_destroy_compress(opj_jp2_t *jp2) {
589 /* destroy the J2K codec */
590 j2k_destroy_compress(jp2->j2k);
593 opj_free(jp2->comps);
602 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
606 if(!jp2 || !parameters || !image)
609 /* setup the J2K codec */
610 /* ------------------- */
612 /* Check if number of components respects standard */
613 if (image->numcomps < 1 || image->numcomps > 16384) {
614 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
618 j2k_setup_encoder(jp2->j2k, parameters, image);
620 /* setup the JP2 codec */
621 /* ------------------- */
625 jp2->brand = JP2_JP2; /* BR */
626 jp2->minversion = 0; /* MinV */
628 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
629 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
631 /* Image Header box */
633 jp2->numcomps = image->numcomps; /* NC */
634 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
635 jp2->h = image->y1 - image->y0; /* HEIGHT */
636 jp2->w = image->x1 - image->x0; /* WIDTH */
638 depth_0 = image->comps[0].prec - 1;
639 sign = image->comps[0].sgnd;
640 jp2->bpc = depth_0 + (sign << 7);
641 for (i = 1; i < image->numcomps; i++) {
642 int depth = image->comps[i].prec - 1;
643 sign = image->comps[i].sgnd;
644 if (depth_0 != depth)
647 jp2->C = 7; /* C : Always 7 */
648 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
649 jp2->IPR = 0; /* IPR, no intellectual property */
651 /* BitsPerComponent box */
653 for (i = 0; i < image->numcomps; i++) {
654 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
657 /* Colour Specification box */
659 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
660 jp2->meth = 1; /* METH: Enumerated colourspace */
662 jp2->meth = 2; /* METH: Restricted ICC profile */
664 if (jp2->meth == 1) {
665 if (image->color_space == 1)
666 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
667 else if (image->color_space == 2)
668 jp2->enumcs = 17; /* greyscale */
669 else if (image->color_space == 3)
670 jp2->enumcs = 18; /* YUV */
672 jp2->enumcs = 0; /* PROFILE (??) */
674 jp2->precedence = 0; /* PRECEDENCE */
675 jp2->approx = 0; /* APPROX */
679 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
683 /* JPEG 2000 Signature box */
686 jp2_write_ftyp(jp2, cio);
688 jp2_write_jp2h(jp2, cio);
692 if(!jp2_write_jp2c(jp2, cio, image, index)) {
693 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");