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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 */
287 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
289 box.length = cio_tell(cio) - box.init_pos;
290 cio_seek(cio, box.init_pos);
291 cio_write(cio, box.length, 4); /* L */
292 cio_seek(cio, box.init_pos + box.length);
295 static void jp2_free_pclr(opj_jp2_color_t *color)
297 opj_free(color->jp2_pclr->channel_sign);
298 opj_free(color->jp2_pclr->channel_size);
299 opj_free(color->jp2_pclr->entries);
301 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
303 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
306 static void free_color_data(opj_jp2_color_t *color)
310 jp2_free_pclr(color);
314 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
315 opj_free(color->jp2_cdef);
317 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
320 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image)
322 opj_image_comp_t *old_comps, *new_comps;
323 unsigned char *channel_size, *channel_sign;
324 unsigned int *entries;
325 opj_jp2_cmap_comp_t *cmap;
328 unsigned short i, nr_channels, cmp, pcol;
331 channel_size = color->jp2_pclr->channel_size;
332 channel_sign = color->jp2_pclr->channel_sign;
333 entries = color->jp2_pclr->entries;
334 cmap = color->jp2_pclr->cmap;
335 nr_channels = color->jp2_pclr->nr_channels;
337 old_comps = image->comps;
338 new_comps = (opj_image_comp_t*)
339 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
341 for(i = 0; i < nr_channels; ++i)
343 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
345 new_comps[pcol] = old_comps[cmp];
347 if(cmap[i].mtyp == 0) /* Direct use */
349 old_comps[cmp].data = NULL; continue;
351 /* Palette mapping: */
352 new_comps[pcol].data = (int*)
353 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
354 new_comps[pcol].prec = channel_size[i];
355 new_comps[pcol].sgnd = channel_sign[i];
357 top_k = color->jp2_pclr->nr_entries - 1;
359 for(i = 0; i < nr_channels; ++i)
362 if(cmap[i].mtyp == 0) continue;
364 /* Palette mapping: */
365 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
366 src = old_comps[cmp].data;
367 dst = new_comps[pcol].data;
368 max = new_comps[pcol].w * new_comps[pcol].h;
370 for(j = 0; j < max; ++j)
373 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
375 dst[j] = entries[k * nr_channels + pcol];
378 max = image->numcomps;
379 for(i = 0; i < max; ++i)
381 if(old_comps[i].data) opj_free(old_comps[i].data);
384 image->comps = new_comps;
385 image->numcomps = nr_channels;
387 jp2_free_pclr(color);
391 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
392 opj_jp2_box_t *box, opj_jp2_color_t *color)
394 opj_jp2_pclr_t *jp2_pclr;
395 unsigned char *channel_size, *channel_sign;
396 unsigned int *entries;
397 unsigned short nr_entries, nr_channels;
401 OPJ_ARG_NOT_USED(box);
402 OPJ_ARG_NOT_USED(jp2);
404 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
405 * a JP2 Header box' :
407 if(color->jp2_pclr) return OPJ_FALSE;
409 nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
410 nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
412 entries = (unsigned int*)
413 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
414 channel_size = (unsigned char*)opj_malloc(nr_channels);
415 channel_sign = (unsigned char*)opj_malloc(nr_channels);
417 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
418 jp2_pclr->channel_sign = channel_sign;
419 jp2_pclr->channel_size = channel_size;
420 jp2_pclr->entries = entries;
421 jp2_pclr->nr_entries = nr_entries;
422 jp2_pclr->nr_channels = nr_channels;
423 jp2_pclr->cmap = NULL;
425 color->jp2_pclr = jp2_pclr;
427 for(i = 0; i < nr_channels; ++i)
429 uc = cio_read(cio, 1); /* Bi */
430 channel_size[i] = (uc & 0x7f) + 1;
431 channel_sign[i] = (uc & 0x80)?1:0;
434 for(j = 0; j < nr_entries; ++j)
436 for(i = 0; i < nr_channels; ++i)
439 *entries++ = cio_read(cio, channel_size[i]>>3);
444 }/* jp2_read_pclr() */
446 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
447 opj_jp2_box_t *box, opj_jp2_color_t *color)
449 opj_jp2_cmap_comp_t *cmap;
450 unsigned short i, nr_channels;
452 OPJ_ARG_NOT_USED(box);
453 OPJ_ARG_NOT_USED(jp2);
455 /* Need nr_channels: */
456 if(color->jp2_pclr == NULL) return OPJ_FALSE;
458 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
459 * inside a JP2 Header box' :
461 if(color->jp2_pclr->cmap) return OPJ_FALSE;
463 nr_channels = color->jp2_pclr->nr_channels;
464 cmap = (opj_jp2_cmap_comp_t*)
465 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
467 for(i = 0; i < nr_channels; ++i)
469 cmap[i].cmp = (unsigned short)cio_read(cio, 2);
470 cmap[i].mtyp = cio_read(cio, 1);
471 cmap[i].pcol = cio_read(cio, 1);
474 color->jp2_pclr->cmap = cmap;
477 }/* jp2_read_cmap() */
479 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
481 opj_jp2_cdef_info_t *info;
483 unsigned short i, n, cn, typ, asoc, acn;
485 color_space = image->color_space;
486 info = color->jp2_cdef->info;
487 n = color->jp2_cdef->n;
489 for(i = 0; i < n; ++i)
491 /* WATCH: acn = asoc - 1 ! */
492 if((asoc = info[i].asoc) == 0) continue;
494 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
498 opj_image_comp_t saved;
500 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
501 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
502 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
504 info[i].asoc = cn + 1;
505 info[acn].asoc = info[acn].cn + 1;
508 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
510 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
512 }/* jp2_apply_cdef() */
514 static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
515 opj_jp2_box_t *box, opj_jp2_color_t *color)
517 opj_jp2_cdef_info_t *info;
520 OPJ_ARG_NOT_USED(box);
521 OPJ_ARG_NOT_USED(jp2);
523 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
524 * inside a JP2 Header box.'
526 if(color->jp2_cdef) return OPJ_FALSE;
528 if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
530 info = (opj_jp2_cdef_info_t*)
531 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
533 color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
534 color->jp2_cdef->info = info;
535 color->jp2_cdef->n = n;
537 for(i = 0; i < n; ++i)
539 info[i].cn = (unsigned short)cio_read(cio, 2);
540 info[i].typ = (unsigned short)cio_read(cio, 2);
541 info[i].asoc = (unsigned short)cio_read(cio, 2);
545 }/* jp2_read_cdef() */
547 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
548 opj_jp2_box_t *box, opj_jp2_color_t *color)
551 opj_common_ptr cinfo;
553 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
554 * Specification boxes after the first.'
556 if(color->jp2_has_colr) return OPJ_FALSE;
560 jp2->meth = cio_read(cio, 1); /* METH */
561 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
562 jp2->approx = cio_read(cio, 1); /* APPROX */
566 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
572 skip_len = box->init_pos + box->length - cio_tell(cio);
575 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
580 unsigned char *start;
582 start = cio_getbp(cio);
583 color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
584 color->icc_profile_len = skip_len;
586 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
588 memcpy(color->icc_profile_buf, start, skip_len);
592 if (cio_tell(cio) - box->init_pos != box->length)
594 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
597 color->jp2_has_colr = 1;
600 }/* jp2_read_colr() */
602 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
607 opj_common_ptr cinfo = jp2->cinfo;
609 jp2_read_boxhdr(cinfo, cio, &box);
612 if (JP2_JP2H != box.type)
614 if (box.type == JP2_JP2C)
616 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
619 cio_skip(cio, box.length - 8);
621 if(cio->bp >= cio->end) return OPJ_FALSE;
623 jp2_read_boxhdr(cinfo, cio, &box);
625 } while(JP2_JP2H != box.type);
627 if (!jp2_read_ihdr(jp2, cio))
629 jp2h_end = box.init_pos + box.length;
633 if (!jp2_read_bpcc(jp2, cio))
636 jp2_read_boxhdr(cinfo, cio, &box);
638 while(cio_tell(cio) < jp2h_end)
640 if(box.type == JP2_COLR)
642 if( !jp2_read_colr(jp2, cio, &box, color))
644 cio_seek(cio, box.init_pos + 8);
645 cio_skip(cio, box.length - 8);
647 jp2_read_boxhdr(cinfo, cio, &box);
650 if(box.type == JP2_CDEF)
652 if( !jp2_read_cdef(jp2, cio, &box, color))
654 cio_seek(cio, box.init_pos + 8);
655 cio_skip(cio, box.length - 8);
657 jp2_read_boxhdr(cinfo, cio, &box);
660 if(box.type == JP2_PCLR)
662 if( !jp2_read_pclr(jp2, cio, &box, color))
664 cio_seek(cio, box.init_pos + 8);
665 cio_skip(cio, box.length - 8);
667 jp2_read_boxhdr(cinfo, cio, &box);
670 if(box.type == JP2_CMAP)
672 if( !jp2_read_cmap(jp2, cio, &box, color))
674 cio_seek(cio, box.init_pos + 8);
675 cio_skip(cio, box.length - 8);
677 jp2_read_boxhdr(cinfo, cio, &box);
680 cio_seek(cio, box.init_pos + 8);
681 cio_skip(cio, box.length - 8);
682 jp2_read_boxhdr(cinfo, cio, &box);
684 }/* while(cio_tell(cio) < box_end) */
686 cio_seek(cio, jp2h_end);
688 /* Part 1, I.5.3.3 : 'must contain at least one' */
689 return (color->jp2_has_colr == 1);
691 }/* jp2_read_jp2h() */
693 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
694 opj_codestream_info_t *cstr_info)
696 opj_common_ptr cinfo;
697 opj_image_t *image = NULL;
698 opj_jp2_color_t color;
704 memset(&color, 0, sizeof(opj_jp2_color_t));
708 if(!jp2_read_struct(jp2, cio, &color))
710 free_color_data(&color);
711 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
716 image = j2k_decode(jp2->j2k, cio, cstr_info);
720 free_color_data(&color);
721 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
725 /* Set Image Color Space */
726 if (jp2->enumcs == 16)
727 image->color_space = CLRSPC_SRGB;
728 else if (jp2->enumcs == 17)
729 image->color_space = CLRSPC_GRAY;
730 else if (jp2->enumcs == 18)
731 image->color_space = CLRSPC_SYCC;
733 image->color_space = CLRSPC_UNKNOWN;
737 jp2_apply_cdef(image, &color);
741 /* Part 1, I.5.3.4: Either both or none : */
742 if( !color.jp2_pclr->cmap)
743 jp2_free_pclr(&color);
745 jp2_apply_pclr(&color, image);
747 if(color.icc_profile_buf)
749 image->icc_profile_buf = color.icc_profile_buf;
750 color.icc_profile_buf = NULL;
751 image->icc_profile_len = color.icc_profile_len;
755 }/* opj_jp2_decode() */
758 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
761 box.init_pos = cio_tell(cio);
763 cio_write(cio, JP2_JP2H, 4); /* JP2H */
765 jp2_write_ihdr(jp2, cio);
767 if (jp2->bpc == 255) {
768 jp2_write_bpcc(jp2, cio);
770 jp2_write_colr(jp2, cio);
772 box.length = cio_tell(cio) - box.init_pos;
773 cio_seek(cio, box.init_pos);
774 cio_write(cio, box.length, 4); /* L */
775 cio_seek(cio, box.init_pos + box.length);
778 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
782 box.init_pos = cio_tell(cio);
784 cio_write(cio, JP2_FTYP, 4); /* FTYP */
786 cio_write(cio, jp2->brand, 4); /* BR */
787 cio_write(cio, jp2->minversion, 4); /* MinV */
789 for (i = 0; i < jp2->numcl; i++) {
790 cio_write(cio, jp2->cl[i], 4); /* CL */
793 box.length = cio_tell(cio) - box.init_pos;
794 cio_seek(cio, box.init_pos);
795 cio_write(cio, box.length, 4); /* L */
796 cio_seek(cio, box.init_pos + box.length);
799 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
803 opj_common_ptr cinfo = jp2->cinfo;
805 jp2_read_boxhdr(cinfo, cio, &box);
807 if (JP2_FTYP != box.type) {
808 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
812 jp2->brand = cio_read(cio, 4); /* BR */
813 jp2->minversion = cio_read(cio, 4); /* MinV */
814 jp2->numcl = (box.length - 16) / 4;
815 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
817 for (i = 0; i < (int)jp2->numcl; i++) {
818 jp2->cl[i] = cio_read(cio, 4); /* CLi */
821 if (cio_tell(cio) - box.init_pos != box.length) {
822 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
829 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
830 unsigned int j2k_codestream_offset, j2k_codestream_length;
833 opj_j2k_t *j2k = jp2->j2k;
835 box.init_pos = cio_tell(cio);
837 cio_write(cio, JP2_JP2C, 4); /* JP2C */
840 j2k_codestream_offset = cio_tell(cio);
841 if(!j2k_encode(j2k, cio, image, cstr_info)) {
842 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
845 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
847 jp2->j2k_codestream_offset = j2k_codestream_offset;
848 jp2->j2k_codestream_length = j2k_codestream_length;
850 box.length = 8 + jp2->j2k_codestream_length;
851 cio_seek(cio, box.init_pos);
852 cio_write(cio, box.length, 4); /* L */
853 cio_seek(cio, box.init_pos + box.length);
858 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
861 opj_common_ptr cinfo = jp2->cinfo;
863 jp2_read_boxhdr(cinfo, cio, &box);
865 if(JP2_JP2C != box.type) {
866 cio_skip(cio, box.length - 8);
867 jp2_read_boxhdr(cinfo, cio, &box);
869 } while(JP2_JP2C != box.type);
871 *j2k_codestream_offset = cio_tell(cio);
872 *j2k_codestream_length = box.length - 8;
877 static void jp2_write_jp(opj_cio_t *cio) {
880 box.init_pos = cio_tell(cio);
882 cio_write(cio, JP2_JP, 4); /* JP2 signature */
883 cio_write(cio, 0x0d0a870a, 4);
885 box.length = cio_tell(cio) - box.init_pos;
886 cio_seek(cio, box.init_pos);
887 cio_write(cio, box.length, 4); /* L */
888 cio_seek(cio, box.init_pos + box.length);
891 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
894 opj_common_ptr cinfo = jp2->cinfo;
896 jp2_read_boxhdr(cinfo, cio, &box);
897 if (JP2_JP != box.type) {
898 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
901 if (0x0d0a870a != cio_read(cio, 4)) {
902 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
905 if (cio_tell(cio) - box.init_pos != box.length) {
906 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
914 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
915 opj_jp2_color_t *color) {
916 if (!jp2_read_jp(jp2, cio))
918 if (!jp2_read_ftyp(jp2, cio))
920 if (!jp2_read_jp2h(jp2, cio, color))
922 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
928 /* ----------------------------------------------------------------------- */
929 /* JP2 decoder interface */
930 /* ----------------------------------------------------------------------- */
932 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
933 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
936 /* create the J2K codec */
937 jp2->j2k = j2k_create_decompress(cinfo);
938 if(jp2->j2k == NULL) {
939 jp2_destroy_decompress(jp2);
946 void jp2_destroy_decompress(opj_jp2_t *jp2) {
948 /* destroy the J2K codec */
949 j2k_destroy_decompress(jp2->j2k);
952 opj_free(jp2->comps);
961 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
962 /* setup the J2K codec */
963 j2k_setup_decoder(jp2->j2k, parameters);
964 /* further JP2 initializations go here */
967 /* ----------------------------------------------------------------------- */
968 /* JP2 encoder interface */
969 /* ----------------------------------------------------------------------- */
971 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
972 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
975 /* create the J2K codec */
976 jp2->j2k = j2k_create_compress(cinfo);
977 if(jp2->j2k == NULL) {
978 jp2_destroy_compress(jp2);
985 void jp2_destroy_compress(opj_jp2_t *jp2) {
987 /* destroy the J2K codec */
988 j2k_destroy_compress(jp2->j2k);
991 opj_free(jp2->comps);
1000 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1004 if(!jp2 || !parameters || !image)
1007 /* setup the J2K codec */
1008 /* ------------------- */
1010 /* Check if number of components respects standard */
1011 if (image->numcomps < 1 || image->numcomps > 16384) {
1012 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1016 j2k_setup_encoder(jp2->j2k, parameters, image);
1018 /* setup the JP2 codec */
1019 /* ------------------- */
1023 jp2->brand = JP2_JP2; /* BR */
1024 jp2->minversion = 0; /* MinV */
1026 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1027 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1029 /* Image Header box */
1031 jp2->numcomps = image->numcomps; /* NC */
1032 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1033 jp2->h = image->y1 - image->y0; /* HEIGHT */
1034 jp2->w = image->x1 - image->x0; /* WIDTH */
1036 depth_0 = image->comps[0].prec - 1;
1037 sign = image->comps[0].sgnd;
1038 jp2->bpc = depth_0 + (sign << 7);
1039 for (i = 1; i < image->numcomps; i++) {
1040 int depth = image->comps[i].prec - 1;
1041 sign = image->comps[i].sgnd;
1042 if (depth_0 != depth)
1045 jp2->C = 7; /* C : Always 7 */
1046 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1047 jp2->IPR = 0; /* IPR, no intellectual property */
1049 /* BitsPerComponent box */
1051 for (i = 0; i < image->numcomps; i++) {
1052 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1055 if (image->color_space == 1)
1056 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
1057 else if (image->color_space == 2)
1058 jp2->enumcs = 17; /* greyscale */
1059 else if (image->color_space == 3)
1060 jp2->enumcs = 18; /* YUV */
1061 jp2->precedence = 0; /* PRECEDENCE */
1062 jp2->approx = 0; /* APPROX */
1066 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1070 /* JPEG 2000 Signature box */
1073 jp2_write_ftyp(jp2, cio);
1074 /* JP2 Header box */
1075 jp2_write_jp2h(jp2, cio);
1079 if(!jp2_write_jp2c(jp2, cio, image, cstr_info)) {
1080 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");