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_config.h"
40 #include "opj_includes.h"
42 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
45 /** @name Local static functions */
50 @param cinfo Codec context info
51 @param cio Input stream
53 @return Returns true if successful, returns false otherwise
55 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
56 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
58 Read the IHDR box - Image Header box
60 @param cio Input buffer stream
61 @return Returns true if successful, returns false otherwise
63 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
64 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
65 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
66 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
67 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
69 Write the FTYP box - File type box
71 @param cio Output buffer stream
73 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
75 Read the FTYP box - File type box
77 @param cio Input buffer stream
78 @return Returns true if successful, returns false otherwise
80 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
81 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
82 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
83 static void jp2_write_jp(opj_cio_t *cio);
85 Read the JP box - JPEG 2000 signature
87 @param cio Input buffer stream
88 @return Returns true if successful, returns false otherwise
90 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
92 Decode the structure of a JP2 file
94 @param cio Input buffer stream
95 @param ext Collector for profile, cdef and pclr data
96 @return Returns true if successful, returns false otherwise
98 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
99 struct extension *ext);
101 Apply collected palette data
102 @param ext Collector for profile, cdef and pclr data
105 static void jp2_apply_pclr(struct extension *ext, opj_image_t *image);
108 @param jp2 JP2 handle
109 @param cio Input buffer stream
111 @param ext Collector for profile, cdef and pclr data
112 @return Returns true if successful, returns false otherwise
114 static bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
115 opj_jp2_box_t *box, struct extension *ext);
117 Collect component mapping data
118 @param jp2 JP2 handle
119 @param cio Input buffer stream
121 @param ext Collector for profile, cdef and pclr data
122 @return Returns true if successful, returns false otherwise
124 static bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
125 opj_jp2_box_t *box, struct extension *ext);
127 Collect colour specification data
128 @param jp2 JP2 handle
129 @param cio Input buffer stream
131 @param ext Collector for profile, cdef and pclr data
132 @return Returns true if successful, returns false otherwise
134 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
135 opj_jp2_box_t *box, struct extension *ext);
137 Apply ICC profile if Color Management System available
138 @param ext Collector for profile, cdef and pclr data
141 static void jp2_apply_profile(struct extension *ext, opj_image_t *image);
146 /* ----------------------------------------------------------------------- */
148 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
149 box->init_pos = cio_tell(cio);
150 box->length = cio_read(cio, 4);
151 box->type = cio_read(cio, 4);
152 if (box->length == 1) {
153 if (cio_read(cio, 4) != 0) {
154 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
157 box->length = cio_read(cio, 4);
158 if (box->length == 0)
159 box->length = cio_numbytesleft(cio) + 12;
161 else if (box->length == 0) {
162 box->length = cio_numbytesleft(cio) + 8;
169 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
173 box.init_pos = cio_tell(cio);
175 cio_write(cio, JP2_URL, 4); /* DBTL */
176 cio_write(cio, 0, 1); /* VERS */
177 cio_write(cio, 0, 3); /* FLAG */
180 for (i = 0; i < strlen(Idx_file); i++) {
181 cio_write(cio, Idx_file[i], 1);
185 box.length = cio_tell(cio) - box.init_pos;
186 cio_seek(cio, box.init_pos);
187 cio_write(cio, box.length, 4); /* L */
188 cio_seek(cio, box.init_pos + box.length);
192 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
195 opj_common_ptr cinfo = jp2->cinfo;
197 jp2_read_boxhdr(cinfo, cio, &box);
198 if (JP2_IHDR != box.type) {
199 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
203 jp2->h = cio_read(cio, 4); /* HEIGHT */
204 jp2->w = cio_read(cio, 4); /* WIDTH */
205 jp2->numcomps = cio_read(cio, 2); /* NC */
206 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
208 jp2->bpc = cio_read(cio, 1); /* BPC */
210 jp2->C = cio_read(cio, 1); /* C */
211 jp2->UnkC = cio_read(cio, 1); /* UnkC */
212 jp2->IPR = cio_read(cio, 1); /* IPR */
214 if (cio_tell(cio) - box.init_pos != box.length) {
215 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
222 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
225 box.init_pos = cio_tell(cio);
227 cio_write(cio, JP2_IHDR, 4); /* IHDR */
229 cio_write(cio, jp2->h, 4); /* HEIGHT */
230 cio_write(cio, jp2->w, 4); /* WIDTH */
231 cio_write(cio, jp2->numcomps, 2); /* NC */
233 cio_write(cio, jp2->bpc, 1); /* BPC */
235 cio_write(cio, jp2->C, 1); /* C : Always 7 */
236 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
237 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
239 box.length = cio_tell(cio) - box.init_pos;
240 cio_seek(cio, box.init_pos);
241 cio_write(cio, box.length, 4); /* L */
242 cio_seek(cio, box.init_pos + box.length);
245 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
249 box.init_pos = cio_tell(cio);
251 cio_write(cio, JP2_BPCC, 4); /* BPCC */
253 for (i = 0; i < jp2->numcomps; i++) {
254 cio_write(cio, jp2->comps[i].bpcc, 1);
257 box.length = cio_tell(cio) - box.init_pos;
258 cio_seek(cio, box.init_pos);
259 cio_write(cio, box.length, 4); /* L */
260 cio_seek(cio, box.init_pos + box.length);
264 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
268 opj_common_ptr cinfo = jp2->cinfo;
270 jp2_read_boxhdr(cinfo, cio, &box);
271 if (JP2_BPCC != box.type) {
272 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
276 for (i = 0; i < jp2->numcomps; i++) {
277 jp2->comps[i].bpcc = cio_read(cio, 1);
280 if (cio_tell(cio) - box.init_pos != box.length) {
281 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
288 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
291 box.init_pos = cio_tell(cio);
293 cio_write(cio, JP2_COLR, 4); /* COLR */
295 cio_write(cio, jp2->meth, 1); /* METH */
296 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
297 cio_write(cio, jp2->approx, 1); /* APPROX */
299 if (jp2->meth == 1) {
300 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
302 cio_write(cio, 0, 1); /* PROFILE (??) */
305 box.length = cio_tell(cio) - box.init_pos;
306 cio_seek(cio, box.init_pos);
307 cio_write(cio, box.length, 4); /* L */
308 cio_seek(cio, box.init_pos + box.length);
311 static void jp2_free_pclr(struct extension *ext)
313 opj_free(ext->jp2_pclr->channel_sign);
314 opj_free(ext->jp2_pclr->channel_size);
315 opj_free(ext->jp2_pclr->entries);
317 if(ext->jp2_pclr->cmap) opj_free(ext->jp2_pclr->cmap);
319 opj_free(ext->jp2_pclr); ext->jp2_pclr = NULL;
322 static void free_ext_data(struct extension *ext)
330 if(ext->jp2_cdef->info) opj_free(ext->jp2_cdef->info);
331 opj_free(ext->jp2_cdef);
333 if(ext->jp2_profile_buf) opj_free(ext->jp2_profile_buf);
336 static void jp2_apply_pclr(struct extension *ext, opj_image_t *image)
338 opj_image_comp_t *old_comps, *new_comps;
339 unsigned char *channel_size, *channel_sign;
340 unsigned int *entries;
341 opj_jp2_cmap_comp_t *cmap;
344 unsigned short i, nr_channels, cmp, pcol;
347 channel_size = ext->jp2_pclr->channel_size;
348 channel_sign = ext->jp2_pclr->channel_sign;
349 entries = ext->jp2_pclr->entries;
350 cmap = ext->jp2_pclr->cmap;
351 nr_channels = ext->jp2_pclr->nr_channels;
353 old_comps = image->comps;
354 new_comps = (opj_image_comp_t*)
355 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
357 for(i = 0; i < nr_channels; ++i)
359 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
361 new_comps[pcol] = old_comps[cmp];
363 if(cmap[i].mtyp == 0) /* Direct use */
365 old_comps[cmp].data = NULL; continue;
367 /* Palette mapping: */
368 new_comps[pcol].data = (int*)
369 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
370 new_comps[pcol].prec = channel_size[i];
371 new_comps[pcol].sgnd = channel_sign[i];
373 top_k = ext->jp2_pclr->nr_entries - 1;
375 for(i = 0; i < nr_channels; ++i)
378 if(cmap[i].mtyp == 0) continue;
380 /* Palette mapping: */
381 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
382 src = old_comps[cmp].data;
383 dst = new_comps[pcol].data;
384 max = new_comps[pcol].w * new_comps[pcol].h;
386 for(j = 0; j < max; ++j)
389 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
391 dst[j] = entries[k * nr_channels + pcol];
394 max = image->numcomps;
395 for(i = 0; i < max; ++i)
397 if(old_comps[i].data) opj_free(old_comps[i].data);
400 image->comps = new_comps;
401 image->numcomps = nr_channels;
407 static bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
408 opj_jp2_box_t *box, struct extension *ext)
410 opj_jp2_pclr_t *jp2_pclr;
411 unsigned char *channel_size, *channel_sign;
412 unsigned int *entries;
413 unsigned short nr_entries, nr_channels;
417 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
418 * a JP2 Header box' :
420 if(ext->jp2_pclr) return false;
422 nr_entries = cio_read(cio, 2); /* NE */
423 nr_channels = cio_read(cio, 1);/* NPC */
425 entries = (unsigned int*)
426 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
427 channel_size = (unsigned char*)opj_malloc(nr_channels);
428 channel_sign = (unsigned char*)opj_malloc(nr_channels);
430 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
431 jp2_pclr->channel_sign = channel_sign;
432 jp2_pclr->channel_size = channel_size;
433 jp2_pclr->entries = entries;
434 jp2_pclr->nr_entries = nr_entries;
435 jp2_pclr->nr_channels = nr_channels;
436 jp2_pclr->cmap = NULL;
438 ext->jp2_pclr = jp2_pclr;
440 for(i = 0; i < nr_channels; ++i)
442 uc = cio_read(cio, 1); /* Bi */
443 channel_size[i] = (uc & 0x7f) + 1;
444 channel_sign[i] = (uc & 0x80)?1:0;
447 for(j = 0; j < nr_entries; ++j)
449 for(i = 0; i < nr_channels; ++i)
452 *entries++ = cio_read(cio, channel_size[i]>>3);
457 }/* jp2_read_pclr() */
459 static bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
460 opj_jp2_box_t *box, struct extension *ext)
462 opj_jp2_cmap_comp_t *cmap;
463 unsigned short i, nr_channels;
465 /* Need nr_channels: */
466 if(ext->jp2_pclr == NULL) return false;
468 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
469 * inside a JP2 Header box' :
471 if(ext->jp2_pclr->cmap) return false;
473 nr_channels = ext->jp2_pclr->nr_channels;
474 cmap = (opj_jp2_cmap_comp_t*)
475 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
477 for(i = 0; i < nr_channels; ++i)
479 cmap[i].cmp = cio_read(cio, 2);
480 cmap[i].mtyp = cio_read(cio, 1);
481 cmap[i].pcol = cio_read(cio, 1);
484 ext->jp2_pclr->cmap = cmap;
487 }/* jp2_read_cmap() */
489 static void jp2_apply_cdef(opj_image_t *image, struct extension *ext)
491 opj_jp2_cdef_info_t *info;
493 unsigned short i, n, cn, typ, asoc, acn;
495 color_space = image->color_space;
496 info = ext->jp2_cdef->info;
497 n = ext->jp2_cdef->n;
499 for(i = 0; i < n; ++i)
501 /* WATCH: acn = asoc - 1 ! */
502 if((asoc = info[i].asoc) == 0) continue;
504 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
508 opj_image_comp_t saved;
510 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
511 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
512 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
514 info[i].asoc = cn + 1;
515 info[acn].asoc = info[acn].cn + 1;
518 if(ext->jp2_cdef->info) opj_free(ext->jp2_cdef->info);
520 opj_free(ext->jp2_cdef); ext->jp2_cdef = NULL;
522 }/* jp2_apply_cdef() */
524 static bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
525 opj_jp2_box_t *box, struct extension *ext)
527 opj_jp2_cdef_info_t *info;
530 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
531 * inside a JP2 Header box.'
533 if(ext->jp2_cdef) return false;
535 if((n = cio_read(cio, 2)) == 0) return false; /* szukw000: FIXME */
537 info = (opj_jp2_cdef_info_t*)
538 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
540 ext->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
541 ext->jp2_cdef->info = info;
542 ext->jp2_cdef->n = n;
544 for(i = 0; i < n; ++i)
546 info[i].cn = cio_read(cio, 2);
547 info[i].typ = cio_read(cio, 2);
548 info[i].asoc = cio_read(cio, 2);
552 }/* jp2_read_cdef() */
554 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
555 opj_jp2_box_t *box, struct extension *ext)
558 opj_common_ptr cinfo;
560 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
561 * Specification boxes after the first.'
563 if(ext->jp2_has_colr) return false;
567 jp2->meth = cio_read(cio, 1); /* METH */
568 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
569 jp2->approx = cio_read(cio, 1); /* APPROX */
573 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
578 skip_len = box->init_pos + box->length - cio_tell(cio);
581 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
586 unsigned char *start;
588 start = cio_getbp(cio);
589 ext->jp2_profile_buf = (unsigned char*)opj_malloc(skip_len);
590 ext->jp2_profile_len = skip_len;
592 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
594 memcpy(ext->jp2_profile_buf, start, skip_len);
598 if (cio_tell(cio) - box->init_pos != box->length)
600 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
603 ext->jp2_has_colr = 1;
606 }/* jp2_read_colr() */
608 #if defined(HAVE_LIBLCMS2) || defined(HAVE_LIBLCMS1)
610 /* Bob Friesenhahn proposed:*/
611 #define cmsSigXYZData icSigXYZData
612 #define cmsSigLabData icSigLabData
613 #define cmsSigCmykData icSigCmykData
614 #define cmsSigYCbCrData icSigYCbCrData
615 #define cmsSigLuvData icSigLuvData
616 #define cmsSigGrayData icSigGrayData
617 #define cmsSigRgbData icSigRgbData
618 #define cmsUInt32Number DWORD
620 #define cmsColorSpaceSignature icColorSpaceSignature
621 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
623 #endif /* HAVE_LIBLCMS1 */
625 static void jp2_apply_profile(struct extension *ext, opj_image_t *image)
627 cmsHPROFILE in_prof, out_prof;
628 cmsHTRANSFORM transform;
629 cmsColorSpaceSignature in_space, out_space;
630 cmsUInt32Number intent, in_type, out_type, nr_samples;
632 int prec, i, max, max_w, max_h;
633 OPJ_COLOR_SPACE oldspace;
636 cmsOpenProfileFromMem(ext->jp2_profile_buf, ext->jp2_profile_len);
637 in_space = cmsGetPCS(in_prof);
638 out_space = cmsGetColorSpace(in_prof);
639 intent = cmsGetHeaderRenderingIntent(in_prof);
642 max_w = image->comps[0].w; max_h = image->comps[0].h;
643 prec = image->comps[0].prec;
644 oldspace = image->color_space;
646 if(out_space == cmsSigRgbData) /* enumCS 16 */
648 in_type = TYPE_RGB_16;
649 out_type = TYPE_RGB_16;
650 out_prof = cmsCreate_sRGBProfile();
651 image->color_space = CLRSPC_SRGB;
654 if(out_space == cmsSigGrayData) /* enumCS 17 */
656 in_type = TYPE_GRAY_8;
657 out_type = TYPE_RGB_8;
658 out_prof = cmsCreate_sRGBProfile();
659 image->color_space = CLRSPC_SRGB;
662 if(out_space == cmsSigYCbCrData) /* enumCS 18 */
664 in_type = TYPE_YCbCr_16;
665 out_type = TYPE_RGB_16;
666 out_prof = cmsCreate_sRGBProfile();
667 image->color_space = CLRSPC_SRGB;
672 fprintf(stderr,"%s:%d: jp2_apply_profile\n\tICC Profile has unknown "
673 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
674 __FILE__,__LINE__,out_space,
675 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
676 (out_space>>8) & 0xff, out_space & 0xff);
682 fprintf(stderr,"%s:%d:jp2_apply_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
683 "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
684 max_w,max_h, (void*)in_prof,(void*)out_prof);
686 fprintf(stderr,"\trender_intent (%u)\n\t"
687 "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
688 " type: in(%u) out:(%u)\n",
691 (in_space>>24) & 0xff,(in_space>>16) & 0xff,
692 (in_space>>8) & 0xff, in_space & 0xff,
695 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
696 (out_space>>8) & 0xff, out_space & 0xff,
700 #endif /* DEBUG_PROFILE */
702 transform = cmsCreateTransform(in_prof, in_type,
703 out_prof, out_type, intent, 0);
706 /* Possible for: LCMS_VERSION >= 2000 :*/
707 cmsCloseProfile(in_prof);
708 cmsCloseProfile(out_prof);
711 if(transform == NULL)
714 fprintf(stderr,"%s:%d:jp2_apply_profile\n\tcmsCreateTransform failed. "
715 "ICC Profile ignored.\n",__FILE__,__LINE__);
717 image->color_space = oldspace;
719 cmsCloseProfile(in_prof);
720 cmsCloseProfile(out_prof);
725 if(image->numcomps > 2)/* RGB, RGBA */
727 unsigned short *inbuf, *outbuf, *in, *out;
728 max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned short);
729 in = inbuf = (unsigned short*)opj_malloc(nr_samples);
730 out = outbuf = (unsigned short*)opj_malloc(nr_samples);
732 r = image->comps[0].data;
733 g = image->comps[1].data;
734 b = image->comps[2].data;
736 for(i = 0; i < max; ++i)
738 *in++ = (unsigned short)*r++;
739 *in++ = (unsigned short)*g++;
740 *in++ = (unsigned short)*b++;
743 cmsDoTransform(transform, inbuf, outbuf, max);
745 r = image->comps[0].data;
746 g = image->comps[1].data;
747 b = image->comps[2].data;
749 for(i = 0; i < max; ++i)
755 opj_free(inbuf); opj_free(outbuf);
757 else /* GRAY, GRAYA */
759 unsigned char *in, *inbuf, *out, *outbuf;
761 max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned char);
762 in = inbuf = (unsigned char*)opj_malloc(nr_samples);
763 out = outbuf = (unsigned char*)opj_malloc(nr_samples);
765 image->comps = (opj_image_comp_t*)
766 opj_realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
768 if(image->numcomps == 2)
769 image->comps[3] = image->comps[1];
771 image->comps[1] = image->comps[0];
772 image->comps[2] = image->comps[0];
774 image->comps[1].data = (int*)opj_calloc(max, sizeof(int));
775 image->comps[2].data = (int*)opj_calloc(max, sizeof(int));
777 image->numcomps += 2;
779 r = image->comps[0].data;
781 for(i = 0; i < max; ++i)
783 *in++ = (unsigned char)*r++;
785 cmsDoTransform(transform, inbuf, outbuf, max);
787 r = image->comps[0].data;
788 g = image->comps[1].data;
789 b = image->comps[2].data;
791 for(i = 0; i < max; ++i)
793 *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
795 opj_free(inbuf); opj_free(outbuf);
797 }/* if(image->numcomps */
799 cmsDeleteTransform(transform);
802 cmsCloseProfile(in_prof);
803 cmsCloseProfile(out_prof);
805 }/* jp2_apply_profile() */
807 #endif /* HAVE_LIBLCMS2 || HAVE_LIBLCMS1 */
809 bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, struct extension *ext)
812 unsigned int jp2h_end;
814 opj_common_ptr cinfo = jp2->cinfo;
816 jp2_read_boxhdr(cinfo, cio, &box);
819 if (JP2_JP2H != box.type)
821 if (box.type == JP2_JP2C)
823 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
826 cio_skip(cio, box.length - 8);
828 if(cio->bp >= cio->end) return false;
830 jp2_read_boxhdr(cinfo, cio, &box);
832 } while(JP2_JP2H != box.type);
834 if (!jp2_read_ihdr(jp2, cio))
836 jp2h_end = box.init_pos + box.length;
840 if (!jp2_read_bpcc(jp2, cio))
843 jp2_read_boxhdr(cinfo, cio, &box);
845 while(cio_tell(cio) < jp2h_end)
847 if(box.type == JP2_COLR)
849 if( !jp2_read_colr(jp2, cio, &box, ext))
851 cio_seek(cio, box.init_pos + 8);
852 cio_skip(cio, box.length - 8);
854 jp2_read_boxhdr(cinfo, cio, &box);
857 if(box.type == JP2_CDEF)
859 if( !jp2_read_cdef(jp2, cio, &box, ext))
861 cio_seek(cio, box.init_pos + 8);
862 cio_skip(cio, box.length - 8);
864 jp2_read_boxhdr(cinfo, cio, &box);
867 if(box.type == JP2_PCLR)
869 if( !jp2_read_pclr(jp2, cio, &box, ext))
871 cio_seek(cio, box.init_pos + 8);
872 cio_skip(cio, box.length - 8);
874 jp2_read_boxhdr(cinfo, cio, &box);
877 if(box.type == JP2_CMAP)
879 if( !jp2_read_cmap(jp2, cio, &box, ext))
881 cio_seek(cio, box.init_pos + 8);
882 cio_skip(cio, box.length - 8);
884 jp2_read_boxhdr(cinfo, cio, &box);
887 cio_seek(cio, box.init_pos + 8);
888 cio_skip(cio, box.length - 8);
889 jp2_read_boxhdr(cinfo, cio, &box);
891 }/* while(cio_tell(cio) < box_end) */
893 cio_seek(cio, jp2h_end);
895 /* Part 1, I.5.3.3 : 'must contain at least one' */
896 return (ext->jp2_has_colr == 1);
898 }/* jp2_read_jp2h() */
900 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
901 opj_codestream_info_t *cstr_info)
903 opj_common_ptr cinfo;
904 opj_image_t *image = NULL;
905 struct extension ext;
911 memset(&ext, 0, sizeof(struct extension));
915 if(!jp2_read_struct(jp2, cio, &ext))
918 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
923 image = j2k_decode(jp2->j2k, cio, cstr_info);
928 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
932 /* Set Image Color Space */
933 if (jp2->enumcs == 16)
934 image->color_space = CLRSPC_SRGB;
935 else if (jp2->enumcs == 17)
936 image->color_space = CLRSPC_GRAY;
937 else if (jp2->enumcs == 18)
938 image->color_space = CLRSPC_SYCC;
940 image->color_space = CLRSPC_UNKNOWN;
944 jp2_apply_cdef(image, &ext);
948 /* Part 1, I.5.3.4: Either both or none : */
949 if( !ext.jp2_pclr->cmap)
952 jp2_apply_pclr(&ext, image);
954 if(ext.jp2_profile_buf)
956 #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2)
957 jp2_apply_profile(&ext, image);
959 opj_free(ext.jp2_profile_buf);
962 if(image->color_space == CLRSPC_SYCC)
964 opj_convert_sycc_to_rgb(image);
971 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
974 box.init_pos = cio_tell(cio);
976 cio_write(cio, JP2_JP2H, 4); /* JP2H */
978 jp2_write_ihdr(jp2, cio);
980 if (jp2->bpc == 255) {
981 jp2_write_bpcc(jp2, cio);
983 jp2_write_colr(jp2, cio);
985 box.length = cio_tell(cio) - box.init_pos;
986 cio_seek(cio, box.init_pos);
987 cio_write(cio, box.length, 4); /* L */
988 cio_seek(cio, box.init_pos + box.length);
991 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
995 box.init_pos = cio_tell(cio);
997 cio_write(cio, JP2_FTYP, 4); /* FTYP */
999 cio_write(cio, jp2->brand, 4); /* BR */
1000 cio_write(cio, jp2->minversion, 4); /* MinV */
1002 for (i = 0; i < jp2->numcl; i++) {
1003 cio_write(cio, jp2->cl[i], 4); /* CL */
1006 box.length = cio_tell(cio) - box.init_pos;
1007 cio_seek(cio, box.init_pos);
1008 cio_write(cio, box.length, 4); /* L */
1009 cio_seek(cio, box.init_pos + box.length);
1012 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1016 opj_common_ptr cinfo = jp2->cinfo;
1018 jp2_read_boxhdr(cinfo, cio, &box);
1020 if (JP2_FTYP != box.type) {
1021 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
1025 jp2->brand = cio_read(cio, 4); /* BR */
1026 jp2->minversion = cio_read(cio, 4); /* MinV */
1027 jp2->numcl = (box.length - 16) / 4;
1028 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
1030 for (i = 0; i < (int)jp2->numcl; i++) {
1031 jp2->cl[i] = cio_read(cio, 4); /* CLi */
1034 if (cio_tell(cio) - box.init_pos != box.length) {
1035 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
1042 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1043 unsigned int j2k_codestream_offset, j2k_codestream_length;
1046 opj_j2k_t *j2k = jp2->j2k;
1048 box.init_pos = cio_tell(cio);
1050 cio_write(cio, JP2_JP2C, 4); /* JP2C */
1053 j2k_codestream_offset = cio_tell(cio);
1054 if(!j2k_encode(j2k, cio, image, cstr_info)) {
1055 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
1058 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
1060 jp2->j2k_codestream_offset = j2k_codestream_offset;
1061 jp2->j2k_codestream_length = j2k_codestream_length;
1063 box.length = 8 + jp2->j2k_codestream_length;
1064 cio_seek(cio, box.init_pos);
1065 cio_write(cio, box.length, 4); /* L */
1066 cio_seek(cio, box.init_pos + box.length);
1071 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
1074 opj_common_ptr cinfo = jp2->cinfo;
1076 jp2_read_boxhdr(cinfo, cio, &box);
1078 if(JP2_JP2C != box.type) {
1079 cio_skip(cio, box.length - 8);
1080 jp2_read_boxhdr(cinfo, cio, &box);
1082 } while(JP2_JP2C != box.type);
1084 *j2k_codestream_offset = cio_tell(cio);
1085 *j2k_codestream_length = box.length - 8;
1090 static void jp2_write_jp(opj_cio_t *cio) {
1093 box.init_pos = cio_tell(cio);
1095 cio_write(cio, JP2_JP, 4); /* JP2 signature */
1096 cio_write(cio, 0x0d0a870a, 4);
1098 box.length = cio_tell(cio) - box.init_pos;
1099 cio_seek(cio, box.init_pos);
1100 cio_write(cio, box.length, 4); /* L */
1101 cio_seek(cio, box.init_pos + box.length);
1104 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
1107 opj_common_ptr cinfo = jp2->cinfo;
1109 jp2_read_boxhdr(cinfo, cio, &box);
1110 if (JP2_JP != box.type) {
1111 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
1114 if (0x0d0a870a != cio_read(cio, 4)) {
1115 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
1118 if (cio_tell(cio) - box.init_pos != box.length) {
1119 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
1127 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
1128 struct extension *ext) {
1129 if (!jp2_read_jp(jp2, cio))
1131 if (!jp2_read_ftyp(jp2, cio))
1133 if (!jp2_read_jp2h(jp2, cio, ext))
1135 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
1141 /* ----------------------------------------------------------------------- */
1142 /* JP2 decoder interface */
1143 /* ----------------------------------------------------------------------- */
1145 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
1146 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
1149 /* create the J2K codec */
1150 jp2->j2k = j2k_create_decompress(cinfo);
1151 if(jp2->j2k == NULL) {
1152 jp2_destroy_decompress(jp2);
1159 void jp2_destroy_decompress(opj_jp2_t *jp2) {
1161 /* destroy the J2K codec */
1162 j2k_destroy_decompress(jp2->j2k);
1165 opj_free(jp2->comps);
1174 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
1175 /* setup the J2K codec */
1176 j2k_setup_decoder(jp2->j2k, parameters);
1177 /* further JP2 initializations go here */
1180 /* ----------------------------------------------------------------------- */
1181 /* JP2 encoder interface */
1182 /* ----------------------------------------------------------------------- */
1184 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
1185 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1188 /* create the J2K codec */
1189 jp2->j2k = j2k_create_compress(cinfo);
1190 if(jp2->j2k == NULL) {
1191 jp2_destroy_compress(jp2);
1198 void jp2_destroy_compress(opj_jp2_t *jp2) {
1200 /* destroy the J2K codec */
1201 j2k_destroy_compress(jp2->j2k);
1204 opj_free(jp2->comps);
1213 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1217 if(!jp2 || !parameters || !image)
1220 /* setup the J2K codec */
1221 /* ------------------- */
1223 /* Check if number of components respects standard */
1224 if (image->numcomps < 1 || image->numcomps > 16384) {
1225 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1229 j2k_setup_encoder(jp2->j2k, parameters, image);
1231 /* setup the JP2 codec */
1232 /* ------------------- */
1236 jp2->brand = JP2_JP2; /* BR */
1237 jp2->minversion = 0; /* MinV */
1239 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1240 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1242 /* Image Header box */
1244 jp2->numcomps = image->numcomps; /* NC */
1245 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1246 jp2->h = image->y1 - image->y0; /* HEIGHT */
1247 jp2->w = image->x1 - image->x0; /* WIDTH */
1249 depth_0 = image->comps[0].prec - 1;
1250 sign = image->comps[0].sgnd;
1251 jp2->bpc = depth_0 + (sign << 7);
1252 for (i = 1; i < image->numcomps; i++) {
1253 int depth = image->comps[i].prec - 1;
1254 sign = image->comps[i].sgnd;
1255 if (depth_0 != depth)
1258 jp2->C = 7; /* C : Always 7 */
1259 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1260 jp2->IPR = 0; /* IPR, no intellectual property */
1262 /* BitsPerComponent box */
1264 for (i = 0; i < image->numcomps; i++) {
1265 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1268 /* Colour Specification box */
1270 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
1271 jp2->meth = 1; /* METH: Enumerated colourspace */
1273 jp2->meth = 2; /* METH: Restricted ICC profile */
1275 if (jp2->meth == 1) {
1276 if (image->color_space == 1)
1277 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
1278 else if (image->color_space == 2)
1279 jp2->enumcs = 17; /* greyscale */
1280 else if (image->color_space == 3)
1281 jp2->enumcs = 18; /* YUV */
1283 jp2->enumcs = 0; /* PROFILE (??) */
1285 jp2->precedence = 0; /* PRECEDENCE */
1286 jp2->approx = 0; /* APPROX */
1290 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1294 /* JPEG 2000 Signature box */
1297 jp2_write_ftyp(jp2, cio);
1298 /* JP2 Header box */
1299 jp2_write_jp2h(jp2, cio);
1303 if(!jp2_write_jp2c(jp2, cio, image, cstr_info)) {
1304 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");