2 * Copyright (c) 2004, Yannick Verschueren
3 * Copyright (c) 2005, Herv� Drolon, FreeImage Team
4 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include "opj_includes.h"
31 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
34 /** @name Local static functions */
39 @param cinfo Codec context info
40 @param cio Input stream
42 @return Returns true if successful, returns false otherwise
44 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
45 static void jp2_write_url(opj_cio_t *cio, char *Idx_file);
47 Read the IHDR box - Image Header box
49 @param cio Input buffer stream
50 @return Returns true if successful, returns false otherwise
52 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
53 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
54 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
55 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
56 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
57 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio);
59 Write the JP2H box - JP2 Header box
61 @param cio Output buffer stream
63 static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
65 Read the JP2H box - JP2 Header box
67 @param cio Input buffer stream
68 @return Returns true if successful, returns false otherwise
70 static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
72 Write the FTYP box - File type box
74 @param cio Output buffer stream
76 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
78 Read the FTYP box - File type box
80 @param cio Input buffer stream
81 @return Returns true if successful, returns false otherwise
83 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
84 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, char *index);
85 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
86 static void jp2_write_jp(opj_cio_t *cio);
88 Read the JP box - JPEG 2000 signature
90 @param cio Input buffer stream
91 @return Returns true if successful, returns false otherwise
93 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
95 Decode the structure of a JP2 file
97 @param cio Input buffer stream
98 @return Returns true if successful, returns false otherwise
100 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio);
106 /* ----------------------------------------------------------------------- */
108 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
109 box->init_pos = cio_tell(cio);
110 box->length = cio_read(cio, 4);
111 box->type = cio_read(cio, 4);
112 if (box->length == 1) {
113 if (cio_read(cio, 4) != 0) {
114 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
117 box->length = cio_read(cio, 4);
118 if (box->length == 0)
119 box->length = cio_numbytesleft(cio) + 12;
121 else if (box->length == 0) {
122 box->length = cio_numbytesleft(cio) + 8;
128 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
132 box.init_pos = cio_tell(cio);
134 cio_write(cio, JP2_URL, 4); /* DBTL */
135 cio_write(cio, 0, 1); /* VERS */
136 cio_write(cio, 0, 3); /* FLAG */
139 for (i = 0; i < strlen(Idx_file); i++) {
140 cio_write(cio, Idx_file[i], 1);
144 box.length = cio_tell(cio) - box.init_pos;
145 cio_seek(cio, box.init_pos);
146 cio_write(cio, box.length, 4); /* L */
147 cio_seek(cio, box.init_pos + box.length);
150 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
153 opj_common_ptr cinfo = jp2->cinfo;
155 jp2_read_boxhdr(cinfo, cio, &box);
156 if (JP2_IHDR != box.type) {
157 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
161 jp2->h = cio_read(cio, 4); /* HEIGHT */
162 jp2->w = cio_read(cio, 4); /* WIDTH */
163 jp2->numcomps = cio_read(cio, 2); /* NC */
165 jp2->bpc = cio_read(cio, 1); /* BPC */
167 jp2->C = cio_read(cio, 1); /* C */
168 jp2->UnkC = cio_read(cio, 1); /* UnkC */
169 jp2->IPR = cio_read(cio, 1); /* IPR */
171 if (cio_tell(cio) - box.init_pos != box.length) {
172 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
179 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
182 box.init_pos = cio_tell(cio);
184 cio_write(cio, JP2_IHDR, 4); /* IHDR */
186 cio_write(cio, jp2->h, 4); /* HEIGHT */
187 cio_write(cio, jp2->w, 4); /* WIDTH */
188 cio_write(cio, jp2->numcomps, 2); /* NC */
190 cio_write(cio, jp2->bpc, 1); /* BPC */
192 cio_write(cio, jp2->C, 1); /* C : Always 7 */
193 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
194 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
196 box.length = cio_tell(cio) - box.init_pos;
197 cio_seek(cio, box.init_pos);
198 cio_write(cio, box.length, 4); /* L */
199 cio_seek(cio, box.init_pos + box.length);
202 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
206 box.init_pos = cio_tell(cio);
208 cio_write(cio, JP2_BPCC, 4); /* BPCC */
210 for (i = 0; i < jp2->numcomps; i++) {
211 cio_write(cio, jp2->comps[i].bpcc, 1);
214 box.length = cio_tell(cio) - box.init_pos;
215 cio_seek(cio, box.init_pos);
216 cio_write(cio, box.length, 4); /* L */
217 cio_seek(cio, box.init_pos + box.length);
221 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
225 opj_common_ptr cinfo = jp2->cinfo;
227 jp2_read_boxhdr(cinfo, cio, &box);
228 if (JP2_BPCC != box.type) {
229 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
233 for (i = 0; i < jp2->numcomps; i++) {
234 jp2->comps[i].bpcc = cio_read(cio, 1);
237 if (cio_tell(cio) - box.init_pos != box.length) {
238 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
245 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
248 box.init_pos = cio_tell(cio);
250 cio_write(cio, JP2_COLR, 4); /* COLR */
252 cio_write(cio, jp2->meth, 1); /* METH */
253 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
254 cio_write(cio, jp2->approx, 1); /* APPROX */
256 if (jp2->meth == 1) {
257 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
259 cio_write(cio, 0, 1); /* PROFILE (??) */
262 box.length = cio_tell(cio) - box.init_pos;
263 cio_seek(cio, box.init_pos);
264 cio_write(cio, box.length, 4); /* L */
265 cio_seek(cio, box.init_pos + box.length);
268 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
272 opj_common_ptr cinfo = jp2->cinfo;
274 jp2_read_boxhdr(cinfo, cio, &box);
276 if (JP2_COLR != box.type) {
277 cio_skip(cio, box.length - 8);
278 jp2_read_boxhdr(cinfo, cio, &box);
280 } while(JP2_COLR != box.type);
282 jp2->meth = cio_read(cio, 1); /* METH */
283 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
284 jp2->approx = cio_read(cio, 1); /* APPROX */
286 if (jp2->meth == 1) {
287 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
290 skip_len = box.init_pos + box.length - cio_tell(cio);
292 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n");
295 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
298 if (cio_tell(cio) - box.init_pos != box.length) {
299 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
305 static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
308 box.init_pos = cio_tell(cio);
310 cio_write(cio, JP2_JP2H, 4); /* JP2H */
312 jp2_write_ihdr(jp2, cio);
314 if (jp2->bpc == 255) {
315 jp2_write_bpcc(jp2, cio);
317 jp2_write_colr(jp2, cio);
319 box.length = cio_tell(cio) - box.init_pos;
320 cio_seek(cio, box.init_pos);
321 cio_write(cio, box.length, 4); /* L */
322 cio_seek(cio, box.init_pos + box.length);
325 static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
329 opj_common_ptr cinfo = jp2->cinfo;
331 jp2_read_boxhdr(cinfo, cio, &box);
333 if (JP2_JP2H != box.type) {
334 if (box.type == JP2_JP2C) {
335 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
338 cio_skip(cio, box.length - 8);
339 jp2_read_boxhdr(cinfo, cio, &box);
341 } while(JP2_JP2H != box.type);
343 if (!jp2_read_ihdr(jp2, cio))
346 if (jp2->bpc == 255) {
347 if (!jp2_read_bpcc(jp2, cio))
350 if (!jp2_read_colr(jp2, cio))
353 skip_len = box.init_pos + box.length - cio_tell(cio);
355 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n");
358 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
363 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
367 box.init_pos = cio_tell(cio);
369 cio_write(cio, JP2_FTYP, 4); /* FTYP */
371 cio_write(cio, jp2->brand, 4); /* BR */
372 cio_write(cio, jp2->minversion, 4); /* MinV */
374 for (i = 0; i < jp2->numcl; i++) {
375 cio_write(cio, jp2->cl[i], 4); /* CL */
378 box.length = cio_tell(cio) - box.init_pos;
379 cio_seek(cio, box.init_pos);
380 cio_write(cio, box.length, 4); /* L */
381 cio_seek(cio, box.init_pos + box.length);
384 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
388 opj_common_ptr cinfo = jp2->cinfo;
390 jp2_read_boxhdr(cinfo, cio, &box);
392 if (JP2_FTYP != box.type) {
393 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
397 jp2->brand = cio_read(cio, 4); /* BR */
398 jp2->minversion = cio_read(cio, 4); /* MinV */
399 jp2->numcl = (box.length - 16) / 4;
400 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
402 for (i = 0; i < (int)jp2->numcl; i++) {
403 jp2->cl[i] = cio_read(cio, 4); /* CLi */
406 if (cio_tell(cio) - box.init_pos != box.length) {
407 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
414 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, char *index) {
415 unsigned int j2k_codestream_offset, j2k_codestream_length;
418 opj_j2k_t *j2k = jp2->j2k;
419 opj_image_t *image = jp2->image;
421 box.init_pos = cio_tell(cio);
423 cio_write(cio, JP2_JP2C, 4); /* JP2C */
426 j2k_codestream_offset = cio_tell(cio);
427 if(!j2k_encode(j2k, cio, image, index)) {
428 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
431 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
433 jp2->j2k_codestream_offset = j2k_codestream_offset;
434 jp2->j2k_codestream_length = j2k_codestream_length;
436 box.length = 8 + jp2->j2k_codestream_length;
437 cio_seek(cio, box.init_pos);
438 cio_write(cio, box.length, 4); /* L */
439 cio_seek(cio, box.init_pos + box.length);
444 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
447 opj_common_ptr cinfo = jp2->cinfo;
449 jp2_read_boxhdr(cinfo, cio, &box);
451 if(JP2_JP2C != box.type) {
452 cio_skip(cio, box.length - 8);
453 jp2_read_boxhdr(cinfo, cio, &box);
455 } while(JP2_JP2C != box.type);
457 *j2k_codestream_offset = cio_tell(cio);
458 *j2k_codestream_length = box.length - 8;
463 static void jp2_write_jp(opj_cio_t *cio) {
466 box.init_pos = cio_tell(cio);
468 cio_write(cio, JP2_JP, 4); /* JP2 signature */
469 cio_write(cio, 0x0d0a870a, 4);
471 box.length = cio_tell(cio) - box.init_pos;
472 cio_seek(cio, box.init_pos);
473 cio_write(cio, box.length, 4); /* L */
474 cio_seek(cio, box.init_pos + box.length);
477 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
480 opj_common_ptr cinfo = jp2->cinfo;
482 jp2_read_boxhdr(cinfo, cio, &box);
483 if (JP2_JP != box.type) {
484 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
487 if (0x0d0a870a != cio_read(cio, 4)) {
488 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
491 if (cio_tell(cio) - box.init_pos != box.length) {
492 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
500 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) {
501 if (!jp2_read_jp(jp2, cio))
503 if (!jp2_read_ftyp(jp2, cio))
505 if (!jp2_read_jp2h(jp2, cio))
507 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
513 /* ----------------------------------------------------------------------- */
514 /* JP2 decoder interface */
515 /* ----------------------------------------------------------------------- */
517 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
518 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
521 /* create the J2K codec */
522 jp2->j2k = j2k_create_decompress(cinfo);
523 if(jp2->j2k == NULL) {
524 jp2_destroy_decompress(jp2);
531 void jp2_destroy_decompress(opj_jp2_t *jp2) {
533 /* destroy the J2K codec */
534 j2k_destroy_decompress(jp2->j2k);
537 opj_free(jp2->comps);
546 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
547 /* setup the J2K codec */
548 j2k_setup_decoder(jp2->j2k, parameters);
549 /* further JP2 initializations go here */
552 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) {
553 opj_common_ptr cinfo;
554 opj_image_t *image = NULL;
563 if(!jp2_read_struct(jp2, cio)) {
564 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
569 image = j2k_decode(jp2->j2k, cio);
571 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
577 /* ----------------------------------------------------------------------- */
578 /* JP2 encoder interface */
579 /* ----------------------------------------------------------------------- */
581 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
582 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
585 /* create the J2K codec */
586 jp2->j2k = j2k_create_compress(cinfo);
587 if(jp2->j2k == NULL) {
588 jp2_destroy_compress(jp2);
595 void jp2_destroy_compress(opj_jp2_t *jp2) {
597 /* destroy the J2K codec */
598 j2k_destroy_compress(jp2->j2k);
601 opj_free(jp2->comps);
610 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
614 if(!jp2 || !parameters || !image)
617 /* setup the J2K codec */
618 /* ------------------- */
620 j2k_setup_encoder(jp2->j2k, parameters, image);
622 /* setup the JP2 codec */
623 /* ------------------- */
627 jp2->brand = JP2_JP2; /* BR */
628 jp2->minversion = 0; /* MinV */
630 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
631 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
633 /* Image Header box */
637 jp2->numcomps = image->numcomps; /* NC */
638 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
639 jp2->h = image->y1 - image->y0; /* HEIGHT */
640 jp2->w = image->x1 - image->x0; /* WIDTH */
642 depth_0 = image->comps[0].prec - 1;
643 sign = image->comps[0].sgnd;
644 jp2->bpc = depth_0 + (sign << 7);
645 for (i = 1; i < image->numcomps; i++) {
646 int depth = image->comps[i].prec - 1;
647 sign = image->comps[i].sgnd;
648 if (depth_0 != depth)
651 jp2->C = 7; /* C : Always 7 */
652 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
653 jp2->IPR = 0; /* IPR, no intellectual property */
655 /* BitsPerComponent box */
657 for (i = 0; i < image->numcomps; i++) {
658 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
661 /* Colour Specification box */
663 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
664 jp2->meth = 1; /* METH: Enumerated colourspace */
666 jp2->meth = 2; /* METH: Restricted ICC profile */
668 if (jp2->meth == 1) {
669 if (image->color_space == 1)
670 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
671 else if (image->color_space == 2)
672 jp2->enumcs = 17; /* greyscale */
673 else if (image->color_space == 3)
674 jp2->enumcs = 18; /* YUV */
676 jp2->enumcs = 0; /* PROFILE (??) */
678 jp2->precedence = 0; /* PRECEDENCE */
679 jp2->approx = 0; /* APPROX */
683 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
687 /* JPEG 2000 Signature box */
690 jp2_write_ftyp(jp2, cio);
692 jp2_write_jp2h(jp2, cio);
696 if(!jp2_write_jp2c(jp2, cio, index)) {
697 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");