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"
41 #include "opj_convert.h"
43 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
46 /** @name Local static functions */
51 @param cinfo Codec context info
52 @param cio Input stream
54 @return Returns true if successful, returns false otherwise
56 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
57 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
59 Read the IHDR box - Image Header box
61 @param cio Input buffer stream
62 @return Returns true if successful, returns false otherwise
64 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
65 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
66 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
67 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
68 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
70 Write the FTYP box - File type box
72 @param cio Output buffer stream
74 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
76 Read the FTYP box - File type box
78 @param cio Input buffer stream
79 @return Returns true if successful, returns false otherwise
81 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
82 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
83 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
84 static void jp2_write_jp(opj_cio_t *cio);
86 Read the JP box - JPEG 2000 signature
88 @param cio Input buffer stream
89 @return Returns true if successful, returns false otherwise
91 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
93 Decode the structure of a JP2 file
95 @param cio Input buffer stream
96 @param ext Collector for profile, cdef and pclr data
97 @return Returns true if successful, returns false otherwise
99 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
100 struct extension *ext);
102 Apply collected palette data
103 @param ext Collector for profile, cdef and pclr data
106 static void jp2_apply_pclr(struct extension *ext, opj_image_t *image);
109 @param jp2 JP2 handle
110 @param cio Input buffer stream
112 @param ext Collector for profile, cdef and pclr data
113 @return Returns true if successful, returns false otherwise
115 static bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
116 opj_jp2_box_t *box, struct extension *ext);
118 Collect component mapping data
119 @param jp2 JP2 handle
120 @param cio Input buffer stream
122 @param ext Collector for profile, cdef and pclr data
123 @return Returns true if successful, returns false otherwise
125 static bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
126 opj_jp2_box_t *box, struct extension *ext);
128 Collect colour specification data
129 @param jp2 JP2 handle
130 @param cio Input buffer stream
132 @param ext Collector for profile, cdef and pclr data
133 @return Returns true if successful, returns false otherwise
135 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
136 opj_jp2_box_t *box, struct extension *ext);
138 Apply ICC profile if Color Management System available
139 @param ext Collector for profile, cdef and pclr data
142 static void jp2_apply_profile(struct extension *ext, opj_image_t *image);
147 /* ----------------------------------------------------------------------- */
149 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
150 box->init_pos = cio_tell(cio);
151 box->length = cio_read(cio, 4);
152 box->type = cio_read(cio, 4);
153 if (box->length == 1) {
154 if (cio_read(cio, 4) != 0) {
155 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
158 box->length = cio_read(cio, 4);
159 if (box->length == 0)
160 box->length = cio_numbytesleft(cio) + 12;
162 else if (box->length == 0) {
163 box->length = cio_numbytesleft(cio) + 8;
170 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
174 box.init_pos = cio_tell(cio);
176 cio_write(cio, JP2_URL, 4); /* DBTL */
177 cio_write(cio, 0, 1); /* VERS */
178 cio_write(cio, 0, 3); /* FLAG */
181 for (i = 0; i < strlen(Idx_file); i++) {
182 cio_write(cio, Idx_file[i], 1);
186 box.length = cio_tell(cio) - box.init_pos;
187 cio_seek(cio, box.init_pos);
188 cio_write(cio, box.length, 4); /* L */
189 cio_seek(cio, box.init_pos + box.length);
193 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
196 opj_common_ptr cinfo = jp2->cinfo;
198 jp2_read_boxhdr(cinfo, cio, &box);
199 if (JP2_IHDR != box.type) {
200 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
204 jp2->h = cio_read(cio, 4); /* HEIGHT */
205 jp2->w = cio_read(cio, 4); /* WIDTH */
206 jp2->numcomps = cio_read(cio, 2); /* NC */
207 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
209 jp2->bpc = cio_read(cio, 1); /* BPC */
211 jp2->C = cio_read(cio, 1); /* C */
212 jp2->UnkC = cio_read(cio, 1); /* UnkC */
213 jp2->IPR = cio_read(cio, 1); /* IPR */
215 if (cio_tell(cio) - box.init_pos != box.length) {
216 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
223 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
226 box.init_pos = cio_tell(cio);
228 cio_write(cio, JP2_IHDR, 4); /* IHDR */
230 cio_write(cio, jp2->h, 4); /* HEIGHT */
231 cio_write(cio, jp2->w, 4); /* WIDTH */
232 cio_write(cio, jp2->numcomps, 2); /* NC */
234 cio_write(cio, jp2->bpc, 1); /* BPC */
236 cio_write(cio, jp2->C, 1); /* C : Always 7 */
237 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
238 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
240 box.length = cio_tell(cio) - box.init_pos;
241 cio_seek(cio, box.init_pos);
242 cio_write(cio, box.length, 4); /* L */
243 cio_seek(cio, box.init_pos + box.length);
246 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
250 box.init_pos = cio_tell(cio);
252 cio_write(cio, JP2_BPCC, 4); /* BPCC */
254 for (i = 0; i < jp2->numcomps; i++) {
255 cio_write(cio, jp2->comps[i].bpcc, 1);
258 box.length = cio_tell(cio) - box.init_pos;
259 cio_seek(cio, box.init_pos);
260 cio_write(cio, box.length, 4); /* L */
261 cio_seek(cio, box.init_pos + box.length);
265 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
269 opj_common_ptr cinfo = jp2->cinfo;
271 jp2_read_boxhdr(cinfo, cio, &box);
272 if (JP2_BPCC != box.type) {
273 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
277 for (i = 0; i < jp2->numcomps; i++) {
278 jp2->comps[i].bpcc = cio_read(cio, 1);
281 if (cio_tell(cio) - box.init_pos != box.length) {
282 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
289 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
292 box.init_pos = cio_tell(cio);
294 cio_write(cio, JP2_COLR, 4); /* COLR */
296 cio_write(cio, jp2->meth, 1); /* METH */
297 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
298 cio_write(cio, jp2->approx, 1); /* APPROX */
300 if (jp2->meth == 1) {
301 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
303 cio_write(cio, 0, 1); /* PROFILE (??) */
306 box.length = cio_tell(cio) - box.init_pos;
307 cio_seek(cio, box.init_pos);
308 cio_write(cio, box.length, 4); /* L */
309 cio_seek(cio, box.init_pos + box.length);
312 static void jp2_free_pclr(struct extension *ext)
314 opj_free(ext->jp2_pclr->channel_sign);
315 opj_free(ext->jp2_pclr->channel_size);
316 opj_free(ext->jp2_pclr->entries);
318 if(ext->jp2_pclr->cmap) opj_free(ext->jp2_pclr->cmap);
320 opj_free(ext->jp2_pclr); ext->jp2_pclr = NULL;
323 static void free_ext_data(struct extension *ext)
331 if(ext->jp2_cdef->info) opj_free(ext->jp2_cdef->info);
332 opj_free(ext->jp2_cdef);
334 if(ext->jp2_profile_buf) opj_free(ext->jp2_profile_buf);
337 static void jp2_apply_pclr(struct extension *ext, opj_image_t *image)
339 opj_image_comp_t *old_comps, *new_comps;
340 unsigned char *channel_size, *channel_sign;
341 unsigned int *entries;
342 opj_jp2_cmap_comp_t *cmap;
345 unsigned short i, nr_channels, cmp, pcol;
348 channel_size = ext->jp2_pclr->channel_size;
349 channel_sign = ext->jp2_pclr->channel_sign;
350 entries = ext->jp2_pclr->entries;
351 cmap = ext->jp2_pclr->cmap;
352 nr_channels = ext->jp2_pclr->nr_channels;
354 old_comps = image->comps;
355 new_comps = (opj_image_comp_t*)
356 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
358 for(i = 0; i < nr_channels; ++i)
360 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
362 new_comps[pcol] = old_comps[cmp];
364 if(cmap[i].mtyp == 0) /* Direct use */
366 old_comps[cmp].data = NULL; continue;
368 /* Palette mapping: */
369 new_comps[pcol].data = (int*)
370 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
371 new_comps[pcol].prec = channel_size[i];
372 new_comps[pcol].sgnd = channel_sign[i];
374 top_k = ext->jp2_pclr->nr_entries - 1;
376 for(i = 0; i < nr_channels; ++i)
379 if(cmap[i].mtyp == 0) continue;
381 /* Palette mapping: */
382 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
383 src = old_comps[cmp].data;
384 dst = new_comps[pcol].data;
385 max = new_comps[pcol].w * new_comps[pcol].h;
387 for(j = 0; j < max; ++j)
390 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
392 dst[j] = entries[k * nr_channels + pcol];
395 max = image->numcomps;
396 for(i = 0; i < max; ++i)
398 if(old_comps[i].data) opj_free(old_comps[i].data);
401 image->comps = new_comps;
402 image->numcomps = nr_channels;
408 static bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
409 opj_jp2_box_t *box, struct extension *ext)
411 opj_jp2_pclr_t *jp2_pclr;
412 unsigned char *channel_size, *channel_sign;
413 unsigned int *entries;
414 unsigned short nr_entries, nr_channels;
418 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
419 * a JP2 Header box' :
421 if(ext->jp2_pclr) return false;
423 nr_entries = cio_read(cio, 2); /* NE */
424 nr_channels = cio_read(cio, 1);/* NPC */
426 entries = (unsigned int*)
427 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
428 channel_size = (unsigned char*)opj_malloc(nr_channels);
429 channel_sign = (unsigned char*)opj_malloc(nr_channels);
431 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
432 jp2_pclr->channel_sign = channel_sign;
433 jp2_pclr->channel_size = channel_size;
434 jp2_pclr->entries = entries;
435 jp2_pclr->nr_entries = nr_entries;
436 jp2_pclr->nr_channels = nr_channels;
437 jp2_pclr->cmap = NULL;
439 ext->jp2_pclr = jp2_pclr;
441 for(i = 0; i < nr_channels; ++i)
443 uc = cio_read(cio, 1); /* Bi */
444 channel_size[i] = (uc & 0x7f) + 1;
445 channel_sign[i] = (uc & 0x80)?1:0;
448 for(j = 0; j < nr_entries; ++j)
450 for(i = 0; i < nr_channels; ++i)
453 *entries++ = cio_read(cio, channel_size[i]>>3);
458 }/* jp2_read_pclr() */
460 static bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
461 opj_jp2_box_t *box, struct extension *ext)
463 opj_jp2_cmap_comp_t *cmap;
464 unsigned short i, nr_channels;
466 /* Need nr_channels: */
467 if(ext->jp2_pclr == NULL) return false;
469 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
470 * inside a JP2 Header box' :
472 if(ext->jp2_pclr->cmap) return false;
474 nr_channels = ext->jp2_pclr->nr_channels;
475 cmap = (opj_jp2_cmap_comp_t*)
476 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
478 for(i = 0; i < nr_channels; ++i)
480 cmap[i].cmp = cio_read(cio, 2);
481 cmap[i].mtyp = cio_read(cio, 1);
482 cmap[i].pcol = cio_read(cio, 1);
485 ext->jp2_pclr->cmap = cmap;
488 }/* jp2_read_cmap() */
490 static void jp2_apply_cdef(opj_image_t *image, struct extension *ext)
492 opj_jp2_cdef_info_t *info;
494 unsigned short i, n, cn, typ, asoc, acn;
496 color_space = image->color_space;
497 info = ext->jp2_cdef->info;
498 n = ext->jp2_cdef->n;
500 for(i = 0; i < n; ++i)
502 /* WATCH: acn = asoc - 1 ! */
503 if((asoc = info[i].asoc) == 0) continue;
505 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
509 opj_image_comp_t saved;
511 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
512 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
513 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
515 info[i].asoc = cn + 1;
516 info[acn].asoc = info[acn].cn + 1;
519 if(ext->jp2_cdef->info) opj_free(ext->jp2_cdef->info);
521 opj_free(ext->jp2_cdef); ext->jp2_cdef = NULL;
523 }/* jp2_apply_cdef() */
525 static bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
526 opj_jp2_box_t *box, struct extension *ext)
528 opj_jp2_cdef_info_t *info;
531 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
532 * inside a JP2 Header box.'
534 if(ext->jp2_cdef) return false;
536 if((n = cio_read(cio, 2)) == 0) return false; /* szukw000: FIXME */
538 info = (opj_jp2_cdef_info_t*)
539 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
541 ext->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
542 ext->jp2_cdef->info = info;
543 ext->jp2_cdef->n = n;
545 for(i = 0; i < n; ++i)
547 info[i].cn = cio_read(cio, 2);
548 info[i].typ = cio_read(cio, 2);
549 info[i].asoc = cio_read(cio, 2);
553 }/* jp2_read_cdef() */
555 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
556 opj_jp2_box_t *box, struct extension *ext)
559 opj_common_ptr cinfo;
561 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
562 * Specification boxes after the first.'
564 if(ext->jp2_has_colr) return false;
568 jp2->meth = cio_read(cio, 1); /* METH */
569 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
570 jp2->approx = cio_read(cio, 1); /* APPROX */
574 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
579 skip_len = box->init_pos + box->length - cio_tell(cio);
582 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
587 unsigned char *start;
589 start = cio_getbp(cio);
590 ext->jp2_profile_buf = (unsigned char*)opj_malloc(skip_len);
591 ext->jp2_profile_len = skip_len;
593 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
595 memcpy(ext->jp2_profile_buf, start, skip_len);
599 if (cio_tell(cio) - box->init_pos != box->length)
601 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
604 ext->jp2_has_colr = 1;
607 }/* jp2_read_colr() */
609 #if defined(HAVE_LIBLCMS2) || defined(HAVE_LIBLCMS1)
611 /* Bob Friesenhahn proposed:*/
612 #define cmsSigXYZData icSigXYZData
613 #define cmsSigLabData icSigLabData
614 #define cmsSigCmykData icSigCmykData
615 #define cmsSigYCbCrData icSigYCbCrData
616 #define cmsSigLuvData icSigLuvData
617 #define cmsSigGrayData icSigGrayData
618 #define cmsSigRgbData icSigRgbData
619 #define cmsUInt32Number DWORD
621 #define cmsColorSpaceSignature icColorSpaceSignature
622 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
624 #endif /* HAVE_LIBLCMS1 */
626 static void jp2_apply_profile(struct extension *ext, opj_image_t *image)
628 cmsHPROFILE in_prof, out_prof;
629 cmsHTRANSFORM transform;
630 cmsColorSpaceSignature in_space, out_space;
631 cmsUInt32Number intent, in_type, out_type, nr_samples;
633 int prec, i, max, max_w, max_h;
634 OPJ_COLOR_SPACE oldspace;
637 cmsOpenProfileFromMem(ext->jp2_profile_buf, ext->jp2_profile_len);
638 in_space = cmsGetPCS(in_prof);
639 out_space = cmsGetColorSpace(in_prof);
640 intent = cmsGetHeaderRenderingIntent(in_prof);
643 max_w = image->comps[0].w; max_h = image->comps[0].h;
644 prec = image->comps[0].prec;
645 oldspace = image->color_space;
647 if(out_space == cmsSigRgbData) /* enumCS 16 */
649 in_type = TYPE_RGB_16;
650 out_type = TYPE_RGB_16;
651 out_prof = cmsCreate_sRGBProfile();
652 image->color_space = CLRSPC_SRGB;
655 if(out_space == cmsSigGrayData) /* enumCS 17 */
657 in_type = TYPE_GRAY_8;
658 out_type = TYPE_RGB_8;
659 out_prof = cmsCreate_sRGBProfile();
660 image->color_space = CLRSPC_SRGB;
663 if(out_space == cmsSigYCbCrData) /* enumCS 18 */
665 in_type = TYPE_YCbCr_16;
666 out_type = TYPE_RGB_16;
667 out_prof = cmsCreate_sRGBProfile();
668 image->color_space = CLRSPC_SRGB;
673 fprintf(stderr,"%s:%d: jp2_apply_profile\n\tICC Profile has unknown "
674 "output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
675 __FILE__,__LINE__,out_space,
676 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
677 (out_space>>8) & 0xff, out_space & 0xff);
683 fprintf(stderr,"%s:%d:jp2_apply_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
684 "\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
685 max_w,max_h, (void*)in_prof,(void*)out_prof);
687 fprintf(stderr,"\trender_intent (%u)\n\t"
688 "color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
689 " type: in(%u) out:(%u)\n",
692 (in_space>>24) & 0xff,(in_space>>16) & 0xff,
693 (in_space>>8) & 0xff, in_space & 0xff,
696 (out_space>>24) & 0xff,(out_space>>16) & 0xff,
697 (out_space>>8) & 0xff, out_space & 0xff,
701 #endif /* DEBUG_PROFILE */
703 transform = cmsCreateTransform(in_prof, in_type,
704 out_prof, out_type, intent, 0);
707 /* Possible for: LCMS_VERSION >= 2000 :*/
708 cmsCloseProfile(in_prof);
709 cmsCloseProfile(out_prof);
712 if(transform == NULL)
715 fprintf(stderr,"%s:%d:jp2_apply_profile\n\tcmsCreateTransform failed. "
716 "ICC Profile ignored.\n",__FILE__,__LINE__);
718 image->color_space = oldspace;
720 cmsCloseProfile(in_prof);
721 cmsCloseProfile(out_prof);
726 if(image->numcomps > 2)/* RGB, RGBA */
728 unsigned short *inbuf, *outbuf, *in, *out;
729 max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned short);
730 in = inbuf = (unsigned short*)opj_malloc(nr_samples);
731 out = outbuf = (unsigned short*)opj_malloc(nr_samples);
733 r = image->comps[0].data;
734 g = image->comps[1].data;
735 b = image->comps[2].data;
737 for(i = 0; i < max; ++i)
739 *in++ = (unsigned short)*r++;
740 *in++ = (unsigned short)*g++;
741 *in++ = (unsigned short)*b++;
744 cmsDoTransform(transform, inbuf, outbuf, max);
746 r = image->comps[0].data;
747 g = image->comps[1].data;
748 b = image->comps[2].data;
750 for(i = 0; i < max; ++i)
756 opj_free(inbuf); opj_free(outbuf);
758 else /* GRAY, GRAYA */
760 unsigned char *in, *inbuf, *out, *outbuf;
762 max = max_w * max_h; nr_samples = max * 3 * sizeof(unsigned char);
763 in = inbuf = (unsigned char*)opj_malloc(nr_samples);
764 out = outbuf = (unsigned char*)opj_malloc(nr_samples);
766 image->comps = (opj_image_comp_t*)
767 opj_realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
769 if(image->numcomps == 2)
770 image->comps[3] = image->comps[1];
772 image->comps[1] = image->comps[0];
773 image->comps[2] = image->comps[0];
775 image->comps[1].data = (int*)opj_calloc(max, sizeof(int));
776 image->comps[2].data = (int*)opj_calloc(max, sizeof(int));
778 image->numcomps += 2;
780 r = image->comps[0].data;
782 for(i = 0; i < max; ++i)
784 *in++ = (unsigned char)*r++;
786 cmsDoTransform(transform, inbuf, outbuf, max);
788 r = image->comps[0].data;
789 g = image->comps[1].data;
790 b = image->comps[2].data;
792 for(i = 0; i < max; ++i)
794 *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
796 opj_free(inbuf); opj_free(outbuf);
798 }/* if(image->numcomps */
800 cmsDeleteTransform(transform);
803 cmsCloseProfile(in_prof);
804 cmsCloseProfile(out_prof);
806 }/* jp2_apply_profile() */
808 #endif /* HAVE_LIBLCMS2 || HAVE_LIBLCMS1 */
810 bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, struct extension *ext)
813 unsigned int jp2h_end;
815 opj_common_ptr cinfo = jp2->cinfo;
817 jp2_read_boxhdr(cinfo, cio, &box);
820 if (JP2_JP2H != box.type)
822 if (box.type == JP2_JP2C)
824 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
827 cio_skip(cio, box.length - 8);
829 if(cio->bp >= cio->end) return false;
831 jp2_read_boxhdr(cinfo, cio, &box);
833 } while(JP2_JP2H != box.type);
835 if (!jp2_read_ihdr(jp2, cio))
837 jp2h_end = box.init_pos + box.length;
841 if (!jp2_read_bpcc(jp2, cio))
844 jp2_read_boxhdr(cinfo, cio, &box);
846 while(cio_tell(cio) < jp2h_end)
848 if(box.type == JP2_COLR)
850 if( !jp2_read_colr(jp2, cio, &box, ext))
852 cio_seek(cio, box.init_pos + 8);
853 cio_skip(cio, box.length - 8);
855 jp2_read_boxhdr(cinfo, cio, &box);
858 if(box.type == JP2_CDEF)
860 if( !jp2_read_cdef(jp2, cio, &box, ext))
862 cio_seek(cio, box.init_pos + 8);
863 cio_skip(cio, box.length - 8);
865 jp2_read_boxhdr(cinfo, cio, &box);
868 if(box.type == JP2_PCLR)
870 if( !jp2_read_pclr(jp2, cio, &box, ext))
872 cio_seek(cio, box.init_pos + 8);
873 cio_skip(cio, box.length - 8);
875 jp2_read_boxhdr(cinfo, cio, &box);
878 if(box.type == JP2_CMAP)
880 if( !jp2_read_cmap(jp2, cio, &box, ext))
882 cio_seek(cio, box.init_pos + 8);
883 cio_skip(cio, box.length - 8);
885 jp2_read_boxhdr(cinfo, cio, &box);
888 cio_seek(cio, box.init_pos + 8);
889 cio_skip(cio, box.length - 8);
890 jp2_read_boxhdr(cinfo, cio, &box);
892 }/* while(cio_tell(cio) < box_end) */
894 cio_seek(cio, jp2h_end);
896 /* Part 1, I.5.3.3 : 'must contain at least one' */
897 return (ext->jp2_has_colr == 1);
899 }/* jp2_read_jp2h() */
901 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
902 opj_codestream_info_t *cstr_info)
904 opj_common_ptr cinfo;
905 opj_image_t *image = NULL;
906 struct extension ext;
912 memset(&ext, 0, sizeof(struct extension));
916 if(!jp2_read_struct(jp2, cio, &ext))
919 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
924 image = j2k_decode(jp2->j2k, cio, cstr_info);
929 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
933 /* Set Image Color Space */
934 if (jp2->enumcs == 16)
935 image->color_space = CLRSPC_SRGB;
936 else if (jp2->enumcs == 17)
937 image->color_space = CLRSPC_GRAY;
938 else if (jp2->enumcs == 18)
939 image->color_space = CLRSPC_SYCC;
941 image->color_space = CLRSPC_UNKNOWN;
945 jp2_apply_cdef(image, &ext);
949 /* Part 1, I.5.3.4: Either both or none : */
950 if( !ext.jp2_pclr->cmap)
953 jp2_apply_pclr(&ext, image);
955 if(ext.jp2_profile_buf)
957 #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2)
958 jp2_apply_profile(&ext, image);
960 opj_free(ext.jp2_profile_buf);
963 if(image->color_space == CLRSPC_SYCC)
965 opj_convert_sycc_to_rgb(image);
972 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
975 box.init_pos = cio_tell(cio);
977 cio_write(cio, JP2_JP2H, 4); /* JP2H */
979 jp2_write_ihdr(jp2, cio);
981 if (jp2->bpc == 255) {
982 jp2_write_bpcc(jp2, cio);
984 jp2_write_colr(jp2, cio);
986 box.length = cio_tell(cio) - box.init_pos;
987 cio_seek(cio, box.init_pos);
988 cio_write(cio, box.length, 4); /* L */
989 cio_seek(cio, box.init_pos + box.length);
992 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
996 box.init_pos = cio_tell(cio);
998 cio_write(cio, JP2_FTYP, 4); /* FTYP */
1000 cio_write(cio, jp2->brand, 4); /* BR */
1001 cio_write(cio, jp2->minversion, 4); /* MinV */
1003 for (i = 0; i < jp2->numcl; i++) {
1004 cio_write(cio, jp2->cl[i], 4); /* CL */
1007 box.length = cio_tell(cio) - box.init_pos;
1008 cio_seek(cio, box.init_pos);
1009 cio_write(cio, box.length, 4); /* L */
1010 cio_seek(cio, box.init_pos + box.length);
1013 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1017 opj_common_ptr cinfo = jp2->cinfo;
1019 jp2_read_boxhdr(cinfo, cio, &box);
1021 if (JP2_FTYP != box.type) {
1022 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
1026 jp2->brand = cio_read(cio, 4); /* BR */
1027 jp2->minversion = cio_read(cio, 4); /* MinV */
1028 jp2->numcl = (box.length - 16) / 4;
1029 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
1031 for (i = 0; i < (int)jp2->numcl; i++) {
1032 jp2->cl[i] = cio_read(cio, 4); /* CLi */
1035 if (cio_tell(cio) - box.init_pos != box.length) {
1036 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
1043 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1044 unsigned int j2k_codestream_offset, j2k_codestream_length;
1047 opj_j2k_t *j2k = jp2->j2k;
1049 box.init_pos = cio_tell(cio);
1051 cio_write(cio, JP2_JP2C, 4); /* JP2C */
1054 j2k_codestream_offset = cio_tell(cio);
1055 if(!j2k_encode(j2k, cio, image, cstr_info)) {
1056 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
1059 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
1061 jp2->j2k_codestream_offset = j2k_codestream_offset;
1062 jp2->j2k_codestream_length = j2k_codestream_length;
1064 box.length = 8 + jp2->j2k_codestream_length;
1065 cio_seek(cio, box.init_pos);
1066 cio_write(cio, box.length, 4); /* L */
1067 cio_seek(cio, box.init_pos + box.length);
1072 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
1075 opj_common_ptr cinfo = jp2->cinfo;
1077 jp2_read_boxhdr(cinfo, cio, &box);
1079 if(JP2_JP2C != box.type) {
1080 cio_skip(cio, box.length - 8);
1081 jp2_read_boxhdr(cinfo, cio, &box);
1083 } while(JP2_JP2C != box.type);
1085 *j2k_codestream_offset = cio_tell(cio);
1086 *j2k_codestream_length = box.length - 8;
1091 static void jp2_write_jp(opj_cio_t *cio) {
1094 box.init_pos = cio_tell(cio);
1096 cio_write(cio, JP2_JP, 4); /* JP2 signature */
1097 cio_write(cio, 0x0d0a870a, 4);
1099 box.length = cio_tell(cio) - box.init_pos;
1100 cio_seek(cio, box.init_pos);
1101 cio_write(cio, box.length, 4); /* L */
1102 cio_seek(cio, box.init_pos + box.length);
1105 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
1108 opj_common_ptr cinfo = jp2->cinfo;
1110 jp2_read_boxhdr(cinfo, cio, &box);
1111 if (JP2_JP != box.type) {
1112 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
1115 if (0x0d0a870a != cio_read(cio, 4)) {
1116 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
1119 if (cio_tell(cio) - box.init_pos != box.length) {
1120 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
1128 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
1129 struct extension *ext) {
1130 if (!jp2_read_jp(jp2, cio))
1132 if (!jp2_read_ftyp(jp2, cio))
1134 if (!jp2_read_jp2h(jp2, cio, ext))
1136 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
1142 /* ----------------------------------------------------------------------- */
1143 /* JP2 decoder interface */
1144 /* ----------------------------------------------------------------------- */
1146 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
1147 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
1150 /* create the J2K codec */
1151 jp2->j2k = j2k_create_decompress(cinfo);
1152 if(jp2->j2k == NULL) {
1153 jp2_destroy_decompress(jp2);
1160 void jp2_destroy_decompress(opj_jp2_t *jp2) {
1162 /* destroy the J2K codec */
1163 j2k_destroy_decompress(jp2->j2k);
1166 opj_free(jp2->comps);
1175 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
1176 /* setup the J2K codec */
1177 j2k_setup_decoder(jp2->j2k, parameters);
1178 /* further JP2 initializations go here */
1181 /* ----------------------------------------------------------------------- */
1182 /* JP2 encoder interface */
1183 /* ----------------------------------------------------------------------- */
1185 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
1186 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1189 /* create the J2K codec */
1190 jp2->j2k = j2k_create_compress(cinfo);
1191 if(jp2->j2k == NULL) {
1192 jp2_destroy_compress(jp2);
1199 void jp2_destroy_compress(opj_jp2_t *jp2) {
1201 /* destroy the J2K codec */
1202 j2k_destroy_compress(jp2->j2k);
1205 opj_free(jp2->comps);
1214 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1218 if(!jp2 || !parameters || !image)
1221 /* setup the J2K codec */
1222 /* ------------------- */
1224 /* Check if number of components respects standard */
1225 if (image->numcomps < 1 || image->numcomps > 16384) {
1226 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1230 j2k_setup_encoder(jp2->j2k, parameters, image);
1232 /* setup the JP2 codec */
1233 /* ------------------- */
1237 jp2->brand = JP2_JP2; /* BR */
1238 jp2->minversion = 0; /* MinV */
1240 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1241 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1243 /* Image Header box */
1245 jp2->numcomps = image->numcomps; /* NC */
1246 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1247 jp2->h = image->y1 - image->y0; /* HEIGHT */
1248 jp2->w = image->x1 - image->x0; /* WIDTH */
1250 depth_0 = image->comps[0].prec - 1;
1251 sign = image->comps[0].sgnd;
1252 jp2->bpc = depth_0 + (sign << 7);
1253 for (i = 1; i < image->numcomps; i++) {
1254 int depth = image->comps[i].prec - 1;
1255 sign = image->comps[i].sgnd;
1256 if (depth_0 != depth)
1259 jp2->C = 7; /* C : Always 7 */
1260 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1261 jp2->IPR = 0; /* IPR, no intellectual property */
1263 /* BitsPerComponent box */
1265 for (i = 0; i < image->numcomps; i++) {
1266 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1269 /* Colour Specification box */
1271 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
1272 jp2->meth = 1; /* METH: Enumerated colourspace */
1274 jp2->meth = 2; /* METH: Restricted ICC profile */
1276 if (jp2->meth == 1) {
1277 if (image->color_space == 1)
1278 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
1279 else if (image->color_space == 2)
1280 jp2->enumcs = 17; /* greyscale */
1281 else if (image->color_space == 3)
1282 jp2->enumcs = 18; /* YUV */
1284 jp2->enumcs = 0; /* PROFILE (??) */
1286 jp2->precedence = 0; /* PRECEDENCE */
1287 jp2->approx = 0; /* APPROX */
1291 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1295 /* JPEG 2000 Signature box */
1298 jp2_write_ftyp(jp2, cio);
1299 /* JP2 Header box */
1300 jp2_write_jp2h(jp2, cio);
1304 if(!jp2_write_jp2c(jp2, cio, image, cstr_info)) {
1305 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");