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.
31 #include "opj_includes.h"
33 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
36 /** @name Local static functions */
41 @param cinfo Codec context info
42 @param cio Input stream
44 @return Returns true if successful, returns false otherwise
46 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
47 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
49 Read the IHDR box - Image Header box
51 @param cio Input buffer stream
52 @return Returns true if successful, returns false otherwise
54 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
55 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
56 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
57 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
58 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
60 Write the FTYP box - File type box
62 @param cio Output buffer stream
64 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
66 Read the FTYP box - File type box
68 @param cio Input buffer stream
69 @return Returns true if successful, returns false otherwise
71 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
72 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
73 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
74 static void jp2_write_jp(opj_cio_t *cio);
76 Read the JP box - JPEG 2000 signature
78 @param cio Input buffer stream
79 @return Returns true if successful, returns false otherwise
81 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
83 Decode the structure of a JP2 file
85 @param cio Input buffer stream
86 @param color Collector for profile, cdef and pclr data
87 @return Returns true if successful, returns false otherwise
89 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
90 opj_jp2_color_t *color);
92 Apply collected palette data
93 @param color Collector for profile, cdef and pclr data
96 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image);
100 @param cio Input buffer stream
102 @param color Collector for profile, cdef and pclr data
103 @return Returns true if successful, returns false otherwise
105 static bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
106 opj_jp2_box_t *box, opj_jp2_color_t *color);
108 Collect component mapping data
109 @param jp2 JP2 handle
110 @param cio Input buffer stream
112 @param color Collector for profile, cdef and pclr data
113 @return Returns true if successful, returns false otherwise
115 static bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
116 opj_jp2_box_t *box, opj_jp2_color_t *color);
118 Collect colour specification data
119 @param jp2 JP2 handle
120 @param cio Input buffer stream
122 @param color Collector for profile, cdef and pclr data
123 @return Returns true if successful, returns false otherwise
125 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
126 opj_jp2_box_t *box, opj_jp2_color_t *color);
131 /* ----------------------------------------------------------------------- */
133 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
134 box->init_pos = cio_tell(cio);
135 box->length = cio_read(cio, 4);
136 box->type = cio_read(cio, 4);
137 if (box->length == 1) {
138 if (cio_read(cio, 4) != 0) {
139 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
142 box->length = cio_read(cio, 4);
143 if (box->length == 0)
144 box->length = cio_numbytesleft(cio) + 12;
146 else if (box->length == 0) {
147 box->length = cio_numbytesleft(cio) + 8;
154 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
158 box.init_pos = cio_tell(cio);
160 cio_write(cio, JP2_URL, 4); /* DBTL */
161 cio_write(cio, 0, 1); /* VERS */
162 cio_write(cio, 0, 3); /* FLAG */
165 for (i = 0; i < strlen(Idx_file); i++) {
166 cio_write(cio, Idx_file[i], 1);
170 box.length = cio_tell(cio) - box.init_pos;
171 cio_seek(cio, box.init_pos);
172 cio_write(cio, box.length, 4); /* L */
173 cio_seek(cio, box.init_pos + box.length);
177 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
180 opj_common_ptr cinfo = jp2->cinfo;
182 jp2_read_boxhdr(cinfo, cio, &box);
183 if (JP2_IHDR != box.type) {
184 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
188 jp2->h = cio_read(cio, 4); /* HEIGHT */
189 jp2->w = cio_read(cio, 4); /* WIDTH */
190 jp2->numcomps = cio_read(cio, 2); /* NC */
191 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
193 jp2->bpc = cio_read(cio, 1); /* BPC */
195 jp2->C = cio_read(cio, 1); /* C */
196 jp2->UnkC = cio_read(cio, 1); /* UnkC */
197 jp2->IPR = cio_read(cio, 1); /* IPR */
199 if (cio_tell(cio) - box.init_pos != box.length) {
200 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
207 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
210 box.init_pos = cio_tell(cio);
212 cio_write(cio, JP2_IHDR, 4); /* IHDR */
214 cio_write(cio, jp2->h, 4); /* HEIGHT */
215 cio_write(cio, jp2->w, 4); /* WIDTH */
216 cio_write(cio, jp2->numcomps, 2); /* NC */
218 cio_write(cio, jp2->bpc, 1); /* BPC */
220 cio_write(cio, jp2->C, 1); /* C : Always 7 */
221 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
222 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
224 box.length = cio_tell(cio) - box.init_pos;
225 cio_seek(cio, box.init_pos);
226 cio_write(cio, box.length, 4); /* L */
227 cio_seek(cio, box.init_pos + box.length);
230 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
234 box.init_pos = cio_tell(cio);
236 cio_write(cio, JP2_BPCC, 4); /* BPCC */
238 for (i = 0; i < jp2->numcomps; i++) {
239 cio_write(cio, jp2->comps[i].bpcc, 1);
242 box.length = cio_tell(cio) - box.init_pos;
243 cio_seek(cio, box.init_pos);
244 cio_write(cio, box.length, 4); /* L */
245 cio_seek(cio, box.init_pos + box.length);
249 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
253 opj_common_ptr cinfo = jp2->cinfo;
255 jp2_read_boxhdr(cinfo, cio, &box);
256 if (JP2_BPCC != box.type) {
257 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
261 for (i = 0; i < jp2->numcomps; i++) {
262 jp2->comps[i].bpcc = cio_read(cio, 1);
265 if (cio_tell(cio) - box.init_pos != box.length) {
266 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
273 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
276 box.init_pos = cio_tell(cio);
278 cio_write(cio, JP2_COLR, 4); /* COLR */
280 cio_write(cio, jp2->meth, 1); /* METH */
281 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
282 cio_write(cio, jp2->approx, 1); /* APPROX */
284 if (jp2->meth == 1) {
285 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
287 cio_write(cio, 0, 1); /* PROFILE (??) */
290 box.length = cio_tell(cio) - box.init_pos;
291 cio_seek(cio, box.init_pos);
292 cio_write(cio, box.length, 4); /* L */
293 cio_seek(cio, box.init_pos + box.length);
296 static void jp2_free_pclr(opj_jp2_color_t *color)
298 opj_free(color->jp2_pclr->channel_sign);
299 opj_free(color->jp2_pclr->channel_size);
300 opj_free(color->jp2_pclr->entries);
302 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
304 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
307 static void free_color_data(opj_jp2_color_t *color)
311 jp2_free_pclr(color);
315 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
316 opj_free(color->jp2_cdef);
318 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
321 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image)
323 opj_image_comp_t *old_comps, *new_comps;
324 unsigned char *channel_size, *channel_sign;
325 unsigned int *entries;
326 opj_jp2_cmap_comp_t *cmap;
329 unsigned short i, nr_channels, cmp, pcol;
332 channel_size = color->jp2_pclr->channel_size;
333 channel_sign = color->jp2_pclr->channel_sign;
334 entries = color->jp2_pclr->entries;
335 cmap = color->jp2_pclr->cmap;
336 nr_channels = color->jp2_pclr->nr_channels;
338 old_comps = image->comps;
339 new_comps = (opj_image_comp_t*)
340 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
342 for(i = 0; i < nr_channels; ++i)
344 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
346 new_comps[pcol] = old_comps[cmp];
348 if(cmap[i].mtyp == 0) /* Direct use */
350 old_comps[cmp].data = NULL; continue;
352 /* Palette mapping: */
353 new_comps[pcol].data = (int*)
354 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
355 new_comps[pcol].prec = channel_size[i];
356 new_comps[pcol].sgnd = channel_sign[i];
358 top_k = color->jp2_pclr->nr_entries - 1;
360 for(i = 0; i < nr_channels; ++i)
363 if(cmap[i].mtyp == 0) continue;
365 /* Palette mapping: */
366 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
367 src = old_comps[cmp].data;
368 dst = new_comps[pcol].data;
369 max = new_comps[pcol].w * new_comps[pcol].h;
371 for(j = 0; j < max; ++j)
374 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
376 dst[j] = entries[k * nr_channels + pcol];
379 max = image->numcomps;
380 for(i = 0; i < max; ++i)
382 if(old_comps[i].data) opj_free(old_comps[i].data);
385 image->comps = new_comps;
386 image->numcomps = nr_channels;
388 jp2_free_pclr(color);
392 static bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
393 opj_jp2_box_t *box, opj_jp2_color_t *color)
395 opj_jp2_pclr_t *jp2_pclr;
396 unsigned char *channel_size, *channel_sign;
397 unsigned int *entries;
398 unsigned short nr_entries, nr_channels;
402 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
403 * a JP2 Header box' :
405 if(color->jp2_pclr) return false;
407 nr_entries = cio_read(cio, 2); /* NE */
408 nr_channels = cio_read(cio, 1);/* NPC */
410 entries = (unsigned int*)
411 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
412 channel_size = (unsigned char*)opj_malloc(nr_channels);
413 channel_sign = (unsigned char*)opj_malloc(nr_channels);
415 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
416 jp2_pclr->channel_sign = channel_sign;
417 jp2_pclr->channel_size = channel_size;
418 jp2_pclr->entries = entries;
419 jp2_pclr->nr_entries = nr_entries;
420 jp2_pclr->nr_channels = nr_channels;
421 jp2_pclr->cmap = NULL;
423 color->jp2_pclr = jp2_pclr;
425 for(i = 0; i < nr_channels; ++i)
427 uc = cio_read(cio, 1); /* Bi */
428 channel_size[i] = (uc & 0x7f) + 1;
429 channel_sign[i] = (uc & 0x80)?1:0;
432 for(j = 0; j < nr_entries; ++j)
434 for(i = 0; i < nr_channels; ++i)
437 *entries++ = cio_read(cio, channel_size[i]>>3);
442 }/* jp2_read_pclr() */
444 static bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
445 opj_jp2_box_t *box, opj_jp2_color_t *color)
447 opj_jp2_cmap_comp_t *cmap;
448 unsigned short i, nr_channels;
450 /* Need nr_channels: */
451 if(color->jp2_pclr == NULL) return false;
453 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
454 * inside a JP2 Header box' :
456 if(color->jp2_pclr->cmap) return false;
458 nr_channels = color->jp2_pclr->nr_channels;
459 cmap = (opj_jp2_cmap_comp_t*)
460 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
462 for(i = 0; i < nr_channels; ++i)
464 cmap[i].cmp = cio_read(cio, 2);
465 cmap[i].mtyp = cio_read(cio, 1);
466 cmap[i].pcol = cio_read(cio, 1);
469 color->jp2_pclr->cmap = cmap;
472 }/* jp2_read_cmap() */
474 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
476 opj_jp2_cdef_info_t *info;
478 unsigned short i, n, cn, typ, asoc, acn;
480 color_space = image->color_space;
481 info = color->jp2_cdef->info;
482 n = color->jp2_cdef->n;
484 for(i = 0; i < n; ++i)
486 /* WATCH: acn = asoc - 1 ! */
487 if((asoc = info[i].asoc) == 0) continue;
489 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
493 opj_image_comp_t saved;
495 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
496 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
497 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
499 info[i].asoc = cn + 1;
500 info[acn].asoc = info[acn].cn + 1;
503 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
505 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
507 }/* jp2_apply_cdef() */
509 static bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
510 opj_jp2_box_t *box, opj_jp2_color_t *color)
512 opj_jp2_cdef_info_t *info;
515 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
516 * inside a JP2 Header box.'
518 if(color->jp2_cdef) return false;
520 if((n = cio_read(cio, 2)) == 0) return false; /* szukw000: FIXME */
522 info = (opj_jp2_cdef_info_t*)
523 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
525 color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
526 color->jp2_cdef->info = info;
527 color->jp2_cdef->n = n;
529 for(i = 0; i < n; ++i)
531 info[i].cn = cio_read(cio, 2);
532 info[i].typ = cio_read(cio, 2);
533 info[i].asoc = cio_read(cio, 2);
537 }/* jp2_read_cdef() */
539 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
540 opj_jp2_box_t *box, opj_jp2_color_t *color)
543 opj_common_ptr cinfo;
545 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
546 * Specification boxes after the first.'
548 if(color->jp2_has_colr) return false;
552 jp2->meth = cio_read(cio, 1); /* METH */
553 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
554 jp2->approx = cio_read(cio, 1); /* APPROX */
558 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
563 skip_len = box->init_pos + box->length - cio_tell(cio);
566 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
571 unsigned char *start;
573 start = cio_getbp(cio);
574 color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
575 color->icc_profile_len = skip_len;
577 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
579 memcpy(color->icc_profile_buf, start, skip_len);
583 if (cio_tell(cio) - box->init_pos != box->length)
585 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
588 color->jp2_has_colr = 1;
591 }/* jp2_read_colr() */
593 bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
596 unsigned int jp2h_end;
598 opj_common_ptr cinfo = jp2->cinfo;
600 jp2_read_boxhdr(cinfo, cio, &box);
603 if (JP2_JP2H != box.type)
605 if (box.type == JP2_JP2C)
607 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
610 cio_skip(cio, box.length - 8);
612 if(cio->bp >= cio->end) return false;
614 jp2_read_boxhdr(cinfo, cio, &box);
616 } while(JP2_JP2H != box.type);
618 if (!jp2_read_ihdr(jp2, cio))
620 jp2h_end = box.init_pos + box.length;
624 if (!jp2_read_bpcc(jp2, cio))
627 jp2_read_boxhdr(cinfo, cio, &box);
629 while(cio_tell(cio) < jp2h_end)
631 if(box.type == JP2_COLR)
633 if( !jp2_read_colr(jp2, cio, &box, color))
635 cio_seek(cio, box.init_pos + 8);
636 cio_skip(cio, box.length - 8);
638 jp2_read_boxhdr(cinfo, cio, &box);
641 if(box.type == JP2_CDEF)
643 if( !jp2_read_cdef(jp2, cio, &box, color))
645 cio_seek(cio, box.init_pos + 8);
646 cio_skip(cio, box.length - 8);
648 jp2_read_boxhdr(cinfo, cio, &box);
651 if(box.type == JP2_PCLR)
653 if( !jp2_read_pclr(jp2, cio, &box, color))
655 cio_seek(cio, box.init_pos + 8);
656 cio_skip(cio, box.length - 8);
658 jp2_read_boxhdr(cinfo, cio, &box);
661 if(box.type == JP2_CMAP)
663 if( !jp2_read_cmap(jp2, cio, &box, color))
665 cio_seek(cio, box.init_pos + 8);
666 cio_skip(cio, box.length - 8);
668 jp2_read_boxhdr(cinfo, cio, &box);
671 cio_seek(cio, box.init_pos + 8);
672 cio_skip(cio, box.length - 8);
673 jp2_read_boxhdr(cinfo, cio, &box);
675 }/* while(cio_tell(cio) < box_end) */
677 cio_seek(cio, jp2h_end);
679 /* Part 1, I.5.3.3 : 'must contain at least one' */
680 return (color->jp2_has_colr == 1);
682 }/* jp2_read_jp2h() */
684 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
685 opj_codestream_info_t *cstr_info)
687 opj_common_ptr cinfo;
688 opj_image_t *image = NULL;
689 opj_jp2_color_t color;
695 memset(&color, 0, sizeof(opj_jp2_color_t));
699 if(!jp2_read_struct(jp2, cio, &color))
701 free_color_data(&color);
702 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
707 image = j2k_decode(jp2->j2k, cio, cstr_info);
711 free_color_data(&color);
712 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
716 /* Set Image Color Space */
717 if (jp2->enumcs == 16)
718 image->color_space = CLRSPC_SRGB;
719 else if (jp2->enumcs == 17)
720 image->color_space = CLRSPC_GRAY;
721 else if (jp2->enumcs == 18)
722 image->color_space = CLRSPC_SYCC;
724 image->color_space = CLRSPC_UNKNOWN;
728 jp2_apply_cdef(image, &color);
732 /* Part 1, I.5.3.4: Either both or none : */
733 if( !color.jp2_pclr->cmap)
734 jp2_free_pclr(&color);
736 jp2_apply_pclr(&color, image);
738 if(color.icc_profile_buf)
740 image->icc_profile_buf = color.icc_profile_buf;
741 color.icc_profile_buf = NULL;
742 image->icc_profile_len = color.icc_profile_len;
749 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
752 box.init_pos = cio_tell(cio);
754 cio_write(cio, JP2_JP2H, 4); /* JP2H */
756 jp2_write_ihdr(jp2, cio);
758 if (jp2->bpc == 255) {
759 jp2_write_bpcc(jp2, cio);
761 jp2_write_colr(jp2, cio);
763 box.length = cio_tell(cio) - box.init_pos;
764 cio_seek(cio, box.init_pos);
765 cio_write(cio, box.length, 4); /* L */
766 cio_seek(cio, box.init_pos + box.length);
769 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
773 box.init_pos = cio_tell(cio);
775 cio_write(cio, JP2_FTYP, 4); /* FTYP */
777 cio_write(cio, jp2->brand, 4); /* BR */
778 cio_write(cio, jp2->minversion, 4); /* MinV */
780 for (i = 0; i < jp2->numcl; i++) {
781 cio_write(cio, jp2->cl[i], 4); /* CL */
784 box.length = cio_tell(cio) - box.init_pos;
785 cio_seek(cio, box.init_pos);
786 cio_write(cio, box.length, 4); /* L */
787 cio_seek(cio, box.init_pos + box.length);
790 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
794 opj_common_ptr cinfo = jp2->cinfo;
796 jp2_read_boxhdr(cinfo, cio, &box);
798 if (JP2_FTYP != box.type) {
799 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
803 jp2->brand = cio_read(cio, 4); /* BR */
804 jp2->minversion = cio_read(cio, 4); /* MinV */
805 jp2->numcl = (box.length - 16) / 4;
806 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
808 for (i = 0; i < (int)jp2->numcl; i++) {
809 jp2->cl[i] = cio_read(cio, 4); /* CLi */
812 if (cio_tell(cio) - box.init_pos != box.length) {
813 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
820 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
821 unsigned int j2k_codestream_offset, j2k_codestream_length;
824 opj_j2k_t *j2k = jp2->j2k;
826 box.init_pos = cio_tell(cio);
828 cio_write(cio, JP2_JP2C, 4); /* JP2C */
831 j2k_codestream_offset = cio_tell(cio);
832 if(!j2k_encode(j2k, cio, image, cstr_info)) {
833 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
836 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
838 jp2->j2k_codestream_offset = j2k_codestream_offset;
839 jp2->j2k_codestream_length = j2k_codestream_length;
841 box.length = 8 + jp2->j2k_codestream_length;
842 cio_seek(cio, box.init_pos);
843 cio_write(cio, box.length, 4); /* L */
844 cio_seek(cio, box.init_pos + box.length);
849 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
852 opj_common_ptr cinfo = jp2->cinfo;
854 jp2_read_boxhdr(cinfo, cio, &box);
856 if(JP2_JP2C != box.type) {
857 cio_skip(cio, box.length - 8);
858 jp2_read_boxhdr(cinfo, cio, &box);
860 } while(JP2_JP2C != box.type);
862 *j2k_codestream_offset = cio_tell(cio);
863 *j2k_codestream_length = box.length - 8;
868 static void jp2_write_jp(opj_cio_t *cio) {
871 box.init_pos = cio_tell(cio);
873 cio_write(cio, JP2_JP, 4); /* JP2 signature */
874 cio_write(cio, 0x0d0a870a, 4);
876 box.length = cio_tell(cio) - box.init_pos;
877 cio_seek(cio, box.init_pos);
878 cio_write(cio, box.length, 4); /* L */
879 cio_seek(cio, box.init_pos + box.length);
882 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
885 opj_common_ptr cinfo = jp2->cinfo;
887 jp2_read_boxhdr(cinfo, cio, &box);
888 if (JP2_JP != box.type) {
889 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
892 if (0x0d0a870a != cio_read(cio, 4)) {
893 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
896 if (cio_tell(cio) - box.init_pos != box.length) {
897 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
905 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
906 opj_jp2_color_t *color) {
907 if (!jp2_read_jp(jp2, cio))
909 if (!jp2_read_ftyp(jp2, cio))
911 if (!jp2_read_jp2h(jp2, cio, color))
913 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
919 /* ----------------------------------------------------------------------- */
920 /* JP2 decoder interface */
921 /* ----------------------------------------------------------------------- */
923 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
924 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
927 /* create the J2K codec */
928 jp2->j2k = j2k_create_decompress(cinfo);
929 if(jp2->j2k == NULL) {
930 jp2_destroy_decompress(jp2);
937 void jp2_destroy_decompress(opj_jp2_t *jp2) {
939 /* destroy the J2K codec */
940 j2k_destroy_decompress(jp2->j2k);
943 opj_free(jp2->comps);
952 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
953 /* setup the J2K codec */
954 j2k_setup_decoder(jp2->j2k, parameters);
955 /* further JP2 initializations go here */
958 /* ----------------------------------------------------------------------- */
959 /* JP2 encoder interface */
960 /* ----------------------------------------------------------------------- */
962 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
963 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
966 /* create the J2K codec */
967 jp2->j2k = j2k_create_compress(cinfo);
968 if(jp2->j2k == NULL) {
969 jp2_destroy_compress(jp2);
976 void jp2_destroy_compress(opj_jp2_t *jp2) {
978 /* destroy the J2K codec */
979 j2k_destroy_compress(jp2->j2k);
982 opj_free(jp2->comps);
991 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
995 if(!jp2 || !parameters || !image)
998 /* setup the J2K codec */
999 /* ------------------- */
1001 /* Check if number of components respects standard */
1002 if (image->numcomps < 1 || image->numcomps > 16384) {
1003 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1007 j2k_setup_encoder(jp2->j2k, parameters, image);
1009 /* setup the JP2 codec */
1010 /* ------------------- */
1014 jp2->brand = JP2_JP2; /* BR */
1015 jp2->minversion = 0; /* MinV */
1017 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1018 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1020 /* Image Header box */
1022 jp2->numcomps = image->numcomps; /* NC */
1023 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1024 jp2->h = image->y1 - image->y0; /* HEIGHT */
1025 jp2->w = image->x1 - image->x0; /* WIDTH */
1027 depth_0 = image->comps[0].prec - 1;
1028 sign = image->comps[0].sgnd;
1029 jp2->bpc = depth_0 + (sign << 7);
1030 for (i = 1; i < image->numcomps; i++) {
1031 int depth = image->comps[i].prec - 1;
1032 sign = image->comps[i].sgnd;
1033 if (depth_0 != depth)
1036 jp2->C = 7; /* C : Always 7 */
1037 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1038 jp2->IPR = 0; /* IPR, no intellectual property */
1040 /* BitsPerComponent box */
1042 for (i = 0; i < image->numcomps; i++) {
1043 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1046 /* Colour Specification box */
1048 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
1049 jp2->meth = 1; /* METH: Enumerated colourspace */
1051 jp2->meth = 2; /* METH: Restricted ICC profile */
1053 if (jp2->meth == 1) {
1054 if (image->color_space == 1)
1055 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
1056 else if (image->color_space == 2)
1057 jp2->enumcs = 17; /* greyscale */
1058 else if (image->color_space == 3)
1059 jp2->enumcs = 18; /* YUV */
1061 jp2->enumcs = 0; /* PROFILE (??) */
1063 jp2->precedence = 0; /* PRECEDENCE */
1064 jp2->approx = 0; /* APPROX */
1068 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1072 /* JPEG 2000 Signature box */
1075 jp2_write_ftyp(jp2, cio);
1076 /* JP2 Header box */
1077 jp2_write_jp2h(jp2, cio);
1081 if(!jp2_write_jp2c(jp2, cio, image, cstr_info)) {
1082 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");