2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2003 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
12 * Copyright (c) 1999-2000 Image Power, Inc.
13 * Copyright (c) 1999-2000 The University of British Columbia
14 * Copyright (c) 2001-2003 Michael David Adams
16 * All rights reserved.
18 * Permission is hereby granted, free of charge, to any person (the
19 * "User") obtaining a copy of this software and associated documentation
20 * files (the "Software"), to deal in the Software without restriction,
21 * including without limitation the rights to use, copy, modify, merge,
22 * publish, distribute, and/or sell copies of the Software, and to permit
23 * persons to whom the Software is furnished to do so, subject to the
24 * following conditions:
26 * 1. The above copyright notices and this permission notice (which
27 * includes the disclaimer below) shall be included in all copies or
28 * substantial portions of the Software.
30 * 2. The name of a copyright holder shall not be used to endorse or
31 * promote products derived from the Software without specific prior
34 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35 * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36 * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49 * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58 * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
61 * __END_OF_JASPER_LICENSE__
67 * $Id: jp2_dec.c,v 1.2 2005/06/09 22:09:40 malaterre Exp $
70 /******************************************************************************\
72 \******************************************************************************/
74 #include "jasper/jas_image.h"
75 #include "jasper/jas_stream.h"
76 #include "jasper/jas_math.h"
77 #include "jasper/jas_debug.h"
78 #include "jasper/jas_malloc.h"
79 #include "jasper/jas_version.h"
84 #define JP2_VALIDATELEN (JAS_MIN(JP2_JP_LEN + 16, JAS_STREAM_MAXPUTBACK))
86 static jp2_dec_t *jp2_dec_create(void);
87 static void jp2_dec_destroy(jp2_dec_t *dec);
88 static int jp2_getcs(jp2_colr_t *colr);
89 static int fromiccpcs(int cs);
90 static int jp2_getct(int colorspace, int type, int assoc);
92 /******************************************************************************\
94 \******************************************************************************/
96 jas_image_t *jp2_decode(jas_stream_t *in, char *optstr)
110 int_fast32_t *lutents;
112 jp2_cdefchan_t *cdefent;
115 jp2_cmapent_t *cmapent;
117 jas_iccprof_t *iccprof;
123 if (!(dec = jp2_dec_create())) {
127 /* Get the first box. This should be a JP box. */
128 if (!(box = jp2_box_get(in))) {
129 jas_eprintf("error: cannot get box\n");
132 if (box->type != JP2_BOX_JP) {
133 jas_eprintf("error: expecting signature box\n");
136 if (box->data.jp.magic != JP2_JP_MAGIC) {
137 jas_eprintf("incorrect magic number\n");
140 jp2_box_destroy(box);
143 /* Get the second box. This should be a FTYP box. */
144 if (!(box = jp2_box_get(in))) {
147 if (box->type != JP2_BOX_FTYP) {
148 jas_eprintf("expecting file type box\n");
151 jp2_box_destroy(box);
154 /* Get more boxes... */
156 while ((box = jp2_box_get(in))) {
157 if (jas_getdbglevel() >= 1) {
158 fprintf(stderr, "box type %s\n", box->info->name);
202 jp2_box_destroy(box);
211 jas_eprintf("error: no code stream found\n");
215 if (!(dec->image = jpc_decode(in, optstr))) {
216 jas_eprintf("error: cannot decode code stream\n");
220 /* An IHDR box must be present. */
222 jas_eprintf("error: missing IHDR box\n");
226 /* Does the number of components indicated in the IHDR box match
227 the value specified in the code stream? */
228 if (dec->ihdr->data.ihdr.numcmpts != JAS_CAST(uint, jas_image_numcmpts(dec->image))) {
229 jas_eprintf("warning: number of components mismatch\n");
232 /* At least one component must be present. */
233 if (!jas_image_numcmpts(dec->image)) {
234 jas_eprintf("error: no components\n");
238 /* Determine if all components have the same data type. */
240 dtype = jas_image_cmptdtype(dec->image, 0);
241 for (i = 1; i < JAS_CAST(uint, jas_image_numcmpts(dec->image)); ++i) {
242 if (jas_image_cmptdtype(dec->image, i) != dtype) {
248 /* Is the component data type indicated in the IHDR box consistent
249 with the data in the code stream? */
250 if ((samedtype && dec->ihdr->data.ihdr.bpc != JP2_DTYPETOBPC(dtype)) ||
251 (!samedtype && dec->ihdr->data.ihdr.bpc != JP2_IHDR_BPCNULL)) {
252 jas_eprintf("warning: component data type mismatch\n");
255 /* Is the compression type supported? */
256 if (dec->ihdr->data.ihdr.comptype != JP2_IHDR_COMPTYPE) {
257 jas_eprintf("error: unsupported compression type\n");
262 /* Is the number of components indicated in the BPCC box
263 consistent with the code stream data? */
264 if (dec->bpcc->data.bpcc.numcmpts != JAS_CAST(uint, jas_image_numcmpts(
266 jas_eprintf("warning: number of components mismatch\n");
268 /* Is the component data type information indicated in the BPCC
269 box consistent with the code stream data? */
271 for (i = 0; i < JAS_CAST(uint, jas_image_numcmpts(dec->image)); ++i) {
272 if (jas_image_cmptdtype(dec->image, i) != JP2_BPCTODTYPE(dec->bpcc->data.bpcc.bpcs[i])) {
273 jas_eprintf("warning: component data type mismatch\n");
277 jas_eprintf("warning: superfluous BPCC box\n");
281 /* A COLR box must be present. */
283 jas_eprintf("error: no COLR box\n");
287 switch (dec->colr->data.colr.method) {
289 jas_image_setclrspc(dec->image, jp2_getcs(&dec->colr->data.colr));
292 iccprof = jas_iccprof_createfrombuf(dec->colr->data.colr.iccp,
293 dec->colr->data.colr.iccplen);
295 jas_iccprof_gethdr(iccprof, &icchdr);
296 jas_eprintf("ICC Profile CS %08x\n", icchdr.colorspc);
297 jas_image_setclrspc(dec->image, fromiccpcs(icchdr.colorspc));
298 dec->image->cmprof_ = jas_cmprof_createfromiccprof(iccprof);
299 assert(dec->image->cmprof_);
300 jas_iccprof_destroy(iccprof);
304 /* If a CMAP box is present, a PCLR box must also be present. */
305 if (dec->cmap && !dec->pclr) {
306 jas_eprintf("warning: missing PCLR box or superfluous CMAP box\n");
307 jp2_box_destroy(dec->cmap);
311 /* If a CMAP box is not present, a PCLR box must not be present. */
312 if (!dec->cmap && dec->pclr) {
313 jas_eprintf("warning: missing CMAP box or superfluous PCLR box\n");
314 jp2_box_destroy(dec->pclr);
318 /* Determine the number of channels (which is essentially the number
319 of components after any palette mappings have been applied). */
320 dec->numchans = dec->cmap ? dec->cmap->data.cmap.numchans : JAS_CAST(uint, jas_image_numcmpts(dec->image));
322 /* Perform a basic sanity check on the CMAP box if present. */
324 for (i = 0; i < dec->numchans; ++i) {
325 /* Is the component number reasonable? */
326 if (dec->cmap->data.cmap.ents[i].cmptno >= JAS_CAST(uint, jas_image_numcmpts(dec->image))) {
327 jas_eprintf("error: invalid component number in CMAP box\n");
330 /* Is the LUT index reasonable? */
331 if (dec->cmap->data.cmap.ents[i].pcol >= dec->pclr->data.pclr.numchans) {
332 jas_eprintf("error: invalid CMAP LUT index\n");
338 /* Allocate space for the channel-number to component-number LUT. */
339 if (!(dec->chantocmptlut = jas_malloc(dec->numchans * sizeof(uint_fast16_t)))) {
340 jas_eprintf("error: no memory\n");
345 for (i = 0; i < dec->numchans; ++i) {
346 dec->chantocmptlut[i] = i;
349 cmapd = &dec->cmap->data.cmap;
350 pclrd = &dec->pclr->data.pclr;
351 cdefd = &dec->cdef->data.cdef;
352 for (channo = 0; channo < cmapd->numchans; ++channo) {
353 cmapent = &cmapd->ents[channo];
354 if (cmapent->map == JP2_CMAP_DIRECT) {
355 dec->chantocmptlut[channo] = channo;
356 } else if (cmapent->map == JP2_CMAP_PALETTE) {
357 lutents = jas_malloc(pclrd->numlutents * sizeof(int_fast32_t));
358 for (i = 0; i < pclrd->numlutents; ++i) {
359 lutents[i] = pclrd->lutdata[cmapent->pcol + i * pclrd->numchans];
361 newcmptno = jas_image_numcmpts(dec->image);
362 jas_image_depalettize(dec->image, cmapent->cmptno, pclrd->numlutents, lutents, JP2_BPCTODTYPE(pclrd->bpc[cmapent->pcol]), newcmptno);
363 dec->chantocmptlut[channo] = newcmptno;
367 cdefent = jp2_cdef_lookup(cdefd, channo);
371 jas_image_setcmpttype(dec->image, newcmptno, jp2_getct(jas_image_clrspc(dec->image), cdefent->type, cdefent->assoc));
373 jas_image_setcmpttype(dec->image, newcmptno, jp2_getct(jas_image_clrspc(dec->image), 0, channo + 1));
382 /* Mark all components as being of unknown type. */
384 for (i = 0; i < JAS_CAST(uint, jas_image_numcmpts(dec->image)); ++i) {
385 jas_image_setcmpttype(dec->image, i, JAS_IMAGE_CT_UNKNOWN);
388 /* Determine the type of each component. */
390 for (i = 0; i < dec->numchans; ++i) {
391 jas_image_setcmpttype(dec->image,
392 dec->chantocmptlut[dec->cdef->data.cdef.ents[i].channo],
393 jp2_getct(jas_image_clrspc(dec->image),
394 dec->cdef->data.cdef.ents[i].type, dec->cdef->data.cdef.ents[i].assoc));
397 for (i = 0; i < dec->numchans; ++i) {
398 jas_image_setcmpttype(dec->image, dec->chantocmptlut[i],
399 jp2_getct(jas_image_clrspc(dec->image), 0, i + 1));
403 /* Delete any components that are not of interest. */
404 for (i = jas_image_numcmpts(dec->image); i > 0; --i) {
405 if (jas_image_cmpttype(dec->image, i - 1) == JAS_IMAGE_CT_UNKNOWN) {
406 jas_image_delcmpt(dec->image, i - 1);
410 /* Ensure that some components survived. */
411 if (!jas_image_numcmpts(dec->image)) {
412 jas_eprintf("error: no components\n");
416 fprintf(stderr, "no of components is %d\n", jas_image_numcmpts(dec->image));
419 /* Prevent the image from being destroyed later. */
423 jp2_dec_destroy(dec);
429 jp2_box_destroy(box);
432 jp2_dec_destroy(dec);
437 int jp2_validate(jas_stream_t *in)
439 char buf[JP2_VALIDATELEN];
443 jas_stream_t *tmpstream;
447 assert(JAS_STREAM_MAXPUTBACK >= JP2_VALIDATELEN);
449 /* Read the validation data (i.e., the data used for detecting
451 if ((n = jas_stream_read(in, buf, JP2_VALIDATELEN)) < 0) {
455 /* Put the validation data back onto the stream, so that the
456 stream position will not be changed. */
457 for (i = n - 1; i >= 0; --i) {
458 if (jas_stream_ungetc(in, buf[i]) == EOF) {
463 /* Did we read enough data? */
464 if (n < JP2_VALIDATELEN) {
468 /* Is the box type correct? */
469 if (((buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]) !=
478 static jp2_dec_t *jp2_dec_create(void)
482 if (!(dec = jas_malloc(sizeof(jp2_dec_t)))) {
490 dec->chantocmptlut = 0;
496 static void jp2_dec_destroy(jp2_dec_t *dec)
499 jp2_box_destroy(dec->ihdr);
502 jp2_box_destroy(dec->bpcc);
505 jp2_box_destroy(dec->cdef);
508 jp2_box_destroy(dec->pclr);
511 jas_image_destroy(dec->image);
514 jp2_box_destroy(dec->cmap);
517 jp2_box_destroy(dec->colr);
519 if (dec->chantocmptlut) {
520 jas_free(dec->chantocmptlut);
525 static int jp2_getct(int colorspace, int type, int assoc)
527 if (type == 1 && assoc == 0) {
528 return JAS_IMAGE_CT_OPACITY;
530 if (type == 0 && assoc >= 1 && assoc <= 65534) {
531 switch (colorspace) {
532 case JAS_CLRSPC_FAM_RGB:
535 return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R);
538 return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G);
541 return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B);
545 case JAS_CLRSPC_FAM_YCBCR:
547 case JP2_CDEF_YCBCR_Y:
548 return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_Y);
550 case JP2_CDEF_YCBCR_CB:
551 return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_CB);
553 case JP2_CDEF_YCBCR_CR:
554 return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_CR);
558 case JAS_CLRSPC_FAM_GRAY:
560 case JP2_CDEF_GRAY_Y:
561 return JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
566 return JAS_IMAGE_CT_COLOR(assoc - 1);
570 return JAS_IMAGE_CT_UNKNOWN;
573 static int jp2_getcs(jp2_colr_t *colr)
575 if (colr->method == JP2_COLR_ENUM) {
576 switch (colr->csid) {
578 return JAS_CLRSPC_SRGB;
581 return JAS_CLRSPC_SYCBCR;
584 return JAS_CLRSPC_SGRAY;
588 return JAS_CLRSPC_UNKNOWN;
591 static int fromiccpcs(int cs)
595 return JAS_CLRSPC_GENRGB;
598 return JAS_CLRSPC_GENYCBCR;
601 return JAS_CLRSPC_GENGRAY;
604 return JAS_CLRSPC_UNKNOWN;