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"
32 /* ----------------------------------------------------------------------- */
34 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
35 box->init_pos = cio_tell(cio);
36 box->length = cio_read(cio, 4);
37 box->type = cio_read(cio, 4);
38 if (box->length == 1) {
39 if (cio_read(cio, 4) != 0) {
40 opg_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
43 box->length = cio_read(cio, 4);
45 box->length = cio_numbytesleft(cio) + 12;
47 else if (box->length == 0) {
48 box->length = cio_numbytesleft(cio) + 8;
54 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
58 box.init_pos = cio_tell(cio);
60 cio_write(cio, JP2_URL, 4); /* DBTL */
61 cio_write(cio, 0, 1); /* VERS */
62 cio_write(cio, 0, 3); /* FLAG */
65 for (i = 0; i < strlen(Idx_file); i++) {
66 cio_write(cio, Idx_file[i], 1);
70 box.length = cio_tell(cio) - box.init_pos;
71 cio_seek(cio, box.init_pos);
72 cio_write(cio, box.length, 4); /* L */
73 cio_seek(cio, box.init_pos + box.length);
76 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
79 opj_common_ptr cinfo = jp2->cinfo;
81 jp2_read_boxhdr(cinfo, cio, &box);
82 if (JP2_IHDR != box.type) {
83 opg_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
87 jp2->h = cio_read(cio, 4); /* HEIGHT */
88 jp2->w = cio_read(cio, 4); /* WIDTH */
89 jp2->numcomps = cio_read(cio, 2); /* NC */
91 jp2->bpc = cio_read(cio, 1); /* BPC */
93 jp2->C = cio_read(cio, 1); /* C */
94 jp2->UnkC = cio_read(cio, 1); /* UnkC */
95 jp2->IPR = cio_read(cio, 1); /* IPR */
97 if (cio_tell(cio) - box.init_pos != box.length) {
98 opg_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
105 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
108 box.init_pos = cio_tell(cio);
110 cio_write(cio, JP2_IHDR, 4); /* IHDR */
112 cio_write(cio, jp2->h, 4); /* HEIGHT */
113 cio_write(cio, jp2->w, 4); /* WIDTH */
114 cio_write(cio, jp2->numcomps, 2); /* NC */
116 cio_write(cio, jp2->bpc, 1); /* BPC */
118 cio_write(cio, jp2->C, 1); /* C : Always 7 */
119 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
120 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
122 box.length = cio_tell(cio) - box.init_pos;
123 cio_seek(cio, box.init_pos);
124 cio_write(cio, box.length, 4); /* L */
125 cio_seek(cio, box.init_pos + box.length);
128 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
132 box.init_pos = cio_tell(cio);
134 cio_write(cio, JP2_BPCC, 4); /* BPCC */
136 for (i = 0; i < jp2->numcomps; i++) {
137 cio_write(cio, jp2->comps[i].bpcc, 1);
140 box.length = cio_tell(cio) - box.init_pos;
141 cio_seek(cio, box.init_pos);
142 cio_write(cio, box.length, 4); /* L */
143 cio_seek(cio, box.init_pos + box.length);
147 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
151 opj_common_ptr cinfo = jp2->cinfo;
153 jp2_read_boxhdr(cinfo, cio, &box);
154 if (JP2_BPCC != box.type) {
155 opg_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
159 for (i = 0; i < jp2->numcomps; i++) {
160 jp2->comps[i].bpcc = cio_read(cio, 1);
163 if (cio_tell(cio) - box.init_pos != box.length) {
164 opg_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
171 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
174 box.init_pos = cio_tell(cio);
176 cio_write(cio, JP2_COLR, 4); /* COLR */
178 cio_write(cio, jp2->meth, 1); /* METH */
179 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
180 cio_write(cio, jp2->approx, 1); /* APPROX */
182 if (jp2->meth == 1) {
183 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
185 cio_write(cio, 0, 1); /* PROFILE (??) */
188 box.length = cio_tell(cio) - box.init_pos;
189 cio_seek(cio, box.init_pos);
190 cio_write(cio, box.length, 4); /* L */
191 cio_seek(cio, box.init_pos + box.length);
194 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
198 opj_common_ptr cinfo = jp2->cinfo;
200 jp2_read_boxhdr(cinfo, cio, &box);
202 if (JP2_COLR != box.type) {
203 cio_skip(cio, box.length - 8);
204 jp2_read_boxhdr(cinfo, cio, &box);
206 } while(JP2_COLR != box.type);
208 jp2->meth = cio_read(cio, 1); /* METH */
209 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
210 jp2->approx = cio_read(cio, 1); /* APPROX */
212 if (jp2->meth == 1) {
213 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
216 skip_len = box.init_pos + box.length - cio_tell(cio);
218 opg_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n");
221 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
224 if (cio_tell(cio) - box.init_pos != box.length) {
225 opg_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
231 static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
234 box.init_pos = cio_tell(cio);
236 cio_write(cio, JP2_JP2H, 4); /* JP2H */
238 jp2_write_ihdr(jp2, cio);
240 if (jp2->bpc == 255) {
241 jp2_write_bpcc(jp2, cio);
243 jp2_write_colr(jp2, cio);
245 box.length = cio_tell(cio) - box.init_pos;
246 cio_seek(cio, box.init_pos);
247 cio_write(cio, box.length, 4); /* L */
248 cio_seek(cio, box.init_pos + box.length);
251 static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
255 opj_common_ptr cinfo = jp2->cinfo;
257 jp2_read_boxhdr(cinfo, cio, &box);
259 if (JP2_JP2H != box.type) {
260 if (box.type == JP2_JP2C) {
261 opg_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
264 cio_skip(cio, box.length - 8);
265 jp2_read_boxhdr(cinfo, cio, &box);
267 } while(JP2_JP2H != box.type);
269 if (!jp2_read_ihdr(jp2, cio))
272 if (jp2->bpc == 255) {
273 if (!jp2_read_bpcc(jp2, cio))
276 if (!jp2_read_colr(jp2, cio))
279 skip_len = box.init_pos + box.length - cio_tell(cio);
281 opg_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n");
284 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
289 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
293 box.init_pos = cio_tell(cio);
295 cio_write(cio, JP2_FTYP, 4); /* FTYP */
297 cio_write(cio, jp2->brand, 4); /* BR */
298 cio_write(cio, jp2->minversion, 4); /* MinV */
300 for (i = 0; i < jp2->numcl; i++) {
301 cio_write(cio, jp2->cl[i], 4); /* CL */
304 box.length = cio_tell(cio) - box.init_pos;
305 cio_seek(cio, box.init_pos);
306 cio_write(cio, box.length, 4); /* L */
307 cio_seek(cio, box.init_pos + box.length);
310 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
314 opj_common_ptr cinfo = jp2->cinfo;
316 jp2_read_boxhdr(cinfo, cio, &box);
318 if (JP2_FTYP != box.type) {
319 opg_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
323 jp2->brand = cio_read(cio, 4); /* BR */
324 jp2->minversion = cio_read(cio, 4); /* MinV */
325 jp2->numcl = (box.length - 16) / 4;
326 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
328 for (i = 0; i < (int)jp2->numcl; i++) {
329 jp2->cl[i] = cio_read(cio, 4); /* CLi */
332 if (cio_tell(cio) - box.init_pos != box.length) {
333 opg_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
340 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, char *index) {
341 unsigned int j2k_codestream_offset, j2k_codestream_length;
344 opj_j2k_t *j2k = jp2->j2k;
345 opj_image_t *image = jp2->image;
347 box.init_pos = cio_tell(cio);
349 cio_write(cio, JP2_JP2C, 4); /* JP2C */
352 j2k_codestream_offset = cio_tell(cio);
353 if(!j2k_encode(j2k, cio, image, index)) {
354 opg_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
357 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
359 jp2->j2k_codestream_offset = j2k_codestream_offset;
360 jp2->j2k_codestream_length = j2k_codestream_length;
362 box.length = 8 + jp2->j2k_codestream_length;
363 cio_seek(cio, box.init_pos);
364 cio_write(cio, box.length, 4); /* L */
365 cio_seek(cio, box.init_pos + box.length);
370 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
373 opj_common_ptr cinfo = jp2->cinfo;
375 jp2_read_boxhdr(cinfo, cio, &box);
377 if(JP2_JP2C != box.type) {
378 cio_skip(cio, box.length - 8);
379 jp2_read_boxhdr(cinfo, cio, &box);
381 } while(JP2_JP2C != box.type);
383 *j2k_codestream_offset = cio_tell(cio);
384 *j2k_codestream_length = box.length - 8;
389 static void jp2_write_jp(opj_cio_t *cio) {
392 box.init_pos = cio_tell(cio);
394 cio_write(cio, JP2_JP, 4); /* JP2 signature */
395 cio_write(cio, 0x0d0a870a, 4);
397 box.length = cio_tell(cio) - box.init_pos;
398 cio_seek(cio, box.init_pos);
399 cio_write(cio, box.length, 4); /* L */
400 cio_seek(cio, box.init_pos + box.length);
403 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
406 opj_common_ptr cinfo = jp2->cinfo;
408 jp2_read_boxhdr(cinfo, cio, &box);
409 if (JP2_JP != box.type) {
410 opg_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
413 if (0x0d0a870a != cio_read(cio, 4)) {
414 opg_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
417 if (cio_tell(cio) - box.init_pos != box.length) {
418 opg_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
426 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) {
427 if (!jp2_read_jp(jp2, cio))
429 if (!jp2_read_ftyp(jp2, cio))
431 if (!jp2_read_jp2h(jp2, cio))
433 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
439 /* ----------------------------------------------------------------------- */
440 /* JP2 decoder interface */
441 /* ----------------------------------------------------------------------- */
443 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
444 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
447 /* create the J2K codec */
448 jp2->j2k = j2k_create_decompress(cinfo);
449 if(jp2->j2k == NULL) {
450 jp2_destroy_decompress(jp2);
457 void jp2_destroy_decompress(opj_jp2_t *jp2) {
459 /* destroy the J2K codec */
460 j2k_destroy_decompress(jp2->j2k);
463 opj_free(jp2->comps);
472 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
473 /* setup the J2K codec */
474 j2k_setup_decoder(jp2->j2k, parameters);
475 /* further JP2 initializations go here */
478 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) {
479 opj_common_ptr cinfo;
480 opj_image_t *image = NULL;
489 if(!jp2_read_struct(jp2, cio)) {
490 opg_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
495 image = j2k_decode(jp2->j2k, cio);
497 opg_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
503 /* ----------------------------------------------------------------------- */
504 /* JP2 encoder interface */
505 /* ----------------------------------------------------------------------- */
507 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
508 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
511 /* create the J2K codec */
512 jp2->j2k = j2k_create_compress(cinfo);
513 if(jp2->j2k == NULL) {
514 jp2_destroy_compress(jp2);
521 void jp2_destroy_compress(opj_jp2_t *jp2) {
523 /* destroy the J2K codec */
524 j2k_destroy_compress(jp2->j2k);
527 opj_free(jp2->comps);
536 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
540 if(!jp2 || !parameters || !image)
543 /* setup the J2K codec */
544 /* ------------------- */
546 j2k_setup_encoder(jp2->j2k, parameters, image);
548 /* setup the JP2 codec */
549 /* ------------------- */
553 jp2->brand = JP2_JP2; /* BR */
554 jp2->minversion = 0; /* MinV */
556 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
557 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
559 /* Image Header box */
563 jp2->numcomps = image->numcomps; /* NC */
564 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
565 jp2->h = image->y1 - image->y0; /* HEIGHT */
566 jp2->w = image->x1 - image->x0; /* WIDTH */
568 depth_0 = image->comps[0].prec - 1;
569 sign = image->comps[0].sgnd;
570 jp2->bpc = depth_0 + (sign << 7);
571 for (i = 1; i < image->numcomps; i++) {
572 int depth = image->comps[i].prec - 1;
573 sign = image->comps[i].sgnd;
574 if (depth_0 != depth)
577 jp2->C = 7; /* C : Always 7 */
578 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
579 jp2->IPR = 0; /* IPR, no intellectual property */
581 /* BitsPerComponent box */
583 for (i = 0; i < image->numcomps; i++) {
584 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
587 /* Colour Specification box */
589 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
590 jp2->meth = 1; /* METH: Enumerated colourspace */
592 jp2->meth = 2; /* METH: Restricted ICC profile */
594 if (jp2->meth == 1) {
595 if (image->color_space == 1)
596 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
597 else if (image->color_space == 2)
598 jp2->enumcs = 17; /* greyscale */
599 else if (image->color_space == 3)
600 jp2->enumcs = 18; /* YUV */
602 jp2->enumcs = 0; /* PROFILE (??) */
604 jp2->precedence = 0; /* PRECEDENCE */
605 jp2->approx = 0; /* APPROX */
609 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
613 /* JPEG 2000 Signature box */
616 jp2_write_ftyp(jp2, cio);
618 jp2_write_jp2h(jp2, cio);
622 if(!jp2_write_jp2c(jp2, cio, index)) {
623 opg_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");