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;
129 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
133 box.init_pos = cio_tell(cio);
135 cio_write(cio, JP2_URL, 4); /* DBTL */
136 cio_write(cio, 0, 1); /* VERS */
137 cio_write(cio, 0, 3); /* FLAG */
140 for (i = 0; i < strlen(Idx_file); i++) {
141 cio_write(cio, Idx_file[i], 1);
145 box.length = cio_tell(cio) - box.init_pos;
146 cio_seek(cio, box.init_pos);
147 cio_write(cio, box.length, 4); /* L */
148 cio_seek(cio, box.init_pos + box.length);
152 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
155 opj_common_ptr cinfo = jp2->cinfo;
157 jp2_read_boxhdr(cinfo, cio, &box);
158 if (JP2_IHDR != box.type) {
159 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
163 jp2->h = cio_read(cio, 4); /* HEIGHT */
164 jp2->w = cio_read(cio, 4); /* WIDTH */
165 jp2->numcomps = cio_read(cio, 2); /* NC */
166 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
168 jp2->bpc = cio_read(cio, 1); /* BPC */
170 jp2->C = cio_read(cio, 1); /* C */
171 jp2->UnkC = cio_read(cio, 1); /* UnkC */
172 jp2->IPR = cio_read(cio, 1); /* IPR */
174 if (cio_tell(cio) - box.init_pos != box.length) {
175 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
182 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
185 box.init_pos = cio_tell(cio);
187 cio_write(cio, JP2_IHDR, 4); /* IHDR */
189 cio_write(cio, jp2->h, 4); /* HEIGHT */
190 cio_write(cio, jp2->w, 4); /* WIDTH */
191 cio_write(cio, jp2->numcomps, 2); /* NC */
193 cio_write(cio, jp2->bpc, 1); /* BPC */
195 cio_write(cio, jp2->C, 1); /* C : Always 7 */
196 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
197 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
199 box.length = cio_tell(cio) - box.init_pos;
200 cio_seek(cio, box.init_pos);
201 cio_write(cio, box.length, 4); /* L */
202 cio_seek(cio, box.init_pos + box.length);
205 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
209 box.init_pos = cio_tell(cio);
211 cio_write(cio, JP2_BPCC, 4); /* BPCC */
213 for (i = 0; i < jp2->numcomps; i++) {
214 cio_write(cio, jp2->comps[i].bpcc, 1);
217 box.length = cio_tell(cio) - box.init_pos;
218 cio_seek(cio, box.init_pos);
219 cio_write(cio, box.length, 4); /* L */
220 cio_seek(cio, box.init_pos + box.length);
224 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
228 opj_common_ptr cinfo = jp2->cinfo;
230 jp2_read_boxhdr(cinfo, cio, &box);
231 if (JP2_BPCC != box.type) {
232 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
236 for (i = 0; i < jp2->numcomps; i++) {
237 jp2->comps[i].bpcc = cio_read(cio, 1);
240 if (cio_tell(cio) - box.init_pos != box.length) {
241 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
248 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
251 box.init_pos = cio_tell(cio);
253 cio_write(cio, JP2_COLR, 4); /* COLR */
255 cio_write(cio, jp2->meth, 1); /* METH */
256 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
257 cio_write(cio, jp2->approx, 1); /* APPROX */
259 if (jp2->meth == 1) {
260 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
262 cio_write(cio, 0, 1); /* PROFILE (??) */
265 box.length = cio_tell(cio) - box.init_pos;
266 cio_seek(cio, box.init_pos);
267 cio_write(cio, box.length, 4); /* L */
268 cio_seek(cio, box.init_pos + box.length);
271 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
275 opj_common_ptr cinfo = jp2->cinfo;
277 jp2_read_boxhdr(cinfo, cio, &box);
279 if (JP2_COLR != box.type) {
280 cio_skip(cio, box.length - 8);
281 jp2_read_boxhdr(cinfo, cio, &box);
283 } while(JP2_COLR != box.type);
285 jp2->meth = cio_read(cio, 1); /* METH */
286 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
287 jp2->approx = cio_read(cio, 1); /* APPROX */
289 if (jp2->meth == 1) {
290 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
293 skip_len = box.init_pos + box.length - cio_tell(cio);
295 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n");
298 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
301 if (cio_tell(cio) - box.init_pos != box.length) {
302 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
308 static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
311 box.init_pos = cio_tell(cio);
313 cio_write(cio, JP2_JP2H, 4); /* JP2H */
315 jp2_write_ihdr(jp2, cio);
317 if (jp2->bpc == 255) {
318 jp2_write_bpcc(jp2, cio);
320 jp2_write_colr(jp2, cio);
322 box.length = cio_tell(cio) - box.init_pos;
323 cio_seek(cio, box.init_pos);
324 cio_write(cio, box.length, 4); /* L */
325 cio_seek(cio, box.init_pos + box.length);
328 static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
332 opj_common_ptr cinfo = jp2->cinfo;
334 jp2_read_boxhdr(cinfo, cio, &box);
336 if (JP2_JP2H != box.type) {
337 if (box.type == JP2_JP2C) {
338 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
341 cio_skip(cio, box.length - 8);
342 jp2_read_boxhdr(cinfo, cio, &box);
344 } while(JP2_JP2H != box.type);
346 if (!jp2_read_ihdr(jp2, cio))
349 if (jp2->bpc == 255) {
350 if (!jp2_read_bpcc(jp2, cio))
353 if (!jp2_read_colr(jp2, cio))
356 skip_len = box.init_pos + box.length - cio_tell(cio);
358 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n");
361 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
366 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
370 box.init_pos = cio_tell(cio);
372 cio_write(cio, JP2_FTYP, 4); /* FTYP */
374 cio_write(cio, jp2->brand, 4); /* BR */
375 cio_write(cio, jp2->minversion, 4); /* MinV */
377 for (i = 0; i < jp2->numcl; i++) {
378 cio_write(cio, jp2->cl[i], 4); /* CL */
381 box.length = cio_tell(cio) - box.init_pos;
382 cio_seek(cio, box.init_pos);
383 cio_write(cio, box.length, 4); /* L */
384 cio_seek(cio, box.init_pos + box.length);
387 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
391 opj_common_ptr cinfo = jp2->cinfo;
393 jp2_read_boxhdr(cinfo, cio, &box);
395 if (JP2_FTYP != box.type) {
396 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
400 jp2->brand = cio_read(cio, 4); /* BR */
401 jp2->minversion = cio_read(cio, 4); /* MinV */
402 jp2->numcl = (box.length - 16) / 4;
403 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
405 for (i = 0; i < (int)jp2->numcl; i++) {
406 jp2->cl[i] = cio_read(cio, 4); /* CLi */
409 if (cio_tell(cio) - box.init_pos != box.length) {
410 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
417 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, char *index) {
418 unsigned int j2k_codestream_offset, j2k_codestream_length;
421 opj_j2k_t *j2k = jp2->j2k;
422 opj_image_t *image = jp2->image;
424 box.init_pos = cio_tell(cio);
426 cio_write(cio, JP2_JP2C, 4); /* JP2C */
429 j2k_codestream_offset = cio_tell(cio);
430 if(!j2k_encode(j2k, cio, image, index)) {
431 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
434 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
436 jp2->j2k_codestream_offset = j2k_codestream_offset;
437 jp2->j2k_codestream_length = j2k_codestream_length;
439 box.length = 8 + jp2->j2k_codestream_length;
440 cio_seek(cio, box.init_pos);
441 cio_write(cio, box.length, 4); /* L */
442 cio_seek(cio, box.init_pos + box.length);
447 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
450 opj_common_ptr cinfo = jp2->cinfo;
452 jp2_read_boxhdr(cinfo, cio, &box);
454 if(JP2_JP2C != box.type) {
455 cio_skip(cio, box.length - 8);
456 jp2_read_boxhdr(cinfo, cio, &box);
458 } while(JP2_JP2C != box.type);
460 *j2k_codestream_offset = cio_tell(cio);
461 *j2k_codestream_length = box.length - 8;
466 static void jp2_write_jp(opj_cio_t *cio) {
469 box.init_pos = cio_tell(cio);
471 cio_write(cio, JP2_JP, 4); /* JP2 signature */
472 cio_write(cio, 0x0d0a870a, 4);
474 box.length = cio_tell(cio) - box.init_pos;
475 cio_seek(cio, box.init_pos);
476 cio_write(cio, box.length, 4); /* L */
477 cio_seek(cio, box.init_pos + box.length);
480 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
483 opj_common_ptr cinfo = jp2->cinfo;
485 jp2_read_boxhdr(cinfo, cio, &box);
486 if (JP2_JP != box.type) {
487 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
490 if (0x0d0a870a != cio_read(cio, 4)) {
491 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
494 if (cio_tell(cio) - box.init_pos != box.length) {
495 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
503 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) {
504 if (!jp2_read_jp(jp2, cio))
506 if (!jp2_read_ftyp(jp2, cio))
508 if (!jp2_read_jp2h(jp2, cio))
510 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
516 /* ----------------------------------------------------------------------- */
517 /* JP2 decoder interface */
518 /* ----------------------------------------------------------------------- */
520 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
521 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
524 /* create the J2K codec */
525 jp2->j2k = j2k_create_decompress(cinfo);
526 if(jp2->j2k == NULL) {
527 jp2_destroy_decompress(jp2);
534 void jp2_destroy_decompress(opj_jp2_t *jp2) {
536 /* destroy the J2K codec */
537 j2k_destroy_decompress(jp2->j2k);
540 opj_free(jp2->comps);
549 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
550 /* setup the J2K codec */
551 j2k_setup_decoder(jp2->j2k, parameters);
552 /* further JP2 initializations go here */
555 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) {
556 opj_common_ptr cinfo;
557 opj_image_t *image = NULL;
566 if(!jp2_read_struct(jp2, cio)) {
567 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
572 image = j2k_decode(jp2->j2k, cio);
574 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
580 /* ----------------------------------------------------------------------- */
581 /* JP2 encoder interface */
582 /* ----------------------------------------------------------------------- */
584 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
585 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
588 /* create the J2K codec */
589 jp2->j2k = j2k_create_compress(cinfo);
590 if(jp2->j2k == NULL) {
591 jp2_destroy_compress(jp2);
598 void jp2_destroy_compress(opj_jp2_t *jp2) {
600 /* destroy the J2K codec */
601 j2k_destroy_compress(jp2->j2k);
604 opj_free(jp2->comps);
613 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
617 if(!jp2 || !parameters || !image)
620 /* setup the J2K codec */
621 /* ------------------- */
623 j2k_setup_encoder(jp2->j2k, parameters, image);
625 /* setup the JP2 codec */
626 /* ------------------- */
630 jp2->brand = JP2_JP2; /* BR */
631 jp2->minversion = 0; /* MinV */
633 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
634 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
636 /* Image Header box */
640 jp2->numcomps = image->numcomps; /* NC */
641 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
642 jp2->h = image->y1 - image->y0; /* HEIGHT */
643 jp2->w = image->x1 - image->x0; /* WIDTH */
645 depth_0 = image->comps[0].prec - 1;
646 sign = image->comps[0].sgnd;
647 jp2->bpc = depth_0 + (sign << 7);
648 for (i = 1; i < image->numcomps; i++) {
649 int depth = image->comps[i].prec - 1;
650 sign = image->comps[i].sgnd;
651 if (depth_0 != depth)
654 jp2->C = 7; /* C : Always 7 */
655 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
656 jp2->IPR = 0; /* IPR, no intellectual property */
658 /* BitsPerComponent box */
660 for (i = 0; i < image->numcomps; i++) {
661 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
664 /* Colour Specification box */
666 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
667 jp2->meth = 1; /* METH: Enumerated colourspace */
669 jp2->meth = 2; /* METH: Restricted ICC profile */
671 if (jp2->meth == 1) {
672 if (image->color_space == 1)
673 jp2->enumcs = 16; /* sRGB as defined by IEC 61966
\962
\961 */
674 else if (image->color_space == 2)
675 jp2->enumcs = 17; /* greyscale */
676 else if (image->color_space == 3)
677 jp2->enumcs = 18; /* YUV */
679 jp2->enumcs = 0; /* PROFILE (??) */
681 jp2->precedence = 0; /* PRECEDENCE */
682 jp2->approx = 0; /* APPROX */
686 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
691 /* JPEG 2000 Signature box */
694 jp2_write_ftyp(jp2, cio);
696 jp2_write_jp2h(jp2, cio);
700 if(!jp2_write_jp2c(jp2, cio, index)) {
701 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");