1/*
2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3 *   British Columbia.
4 * Copyright (c) 2001-2002 Michael David Adams.
5 * All rights reserved.
6 */
7
8/* __START_OF_JASPER_LICENSE__
9 *
10 * JasPer Software License
11 *
12 * IMAGE POWER JPEG-2000 PUBLIC LICENSE
13 * ************************************
14 *
15 * GRANT:
16 *
17 * Permission is hereby granted, free of charge, to any person (the "User")
18 * obtaining a copy of this software and associated documentation, to deal
19 * in the JasPer Software without restriction, including without limitation
20 * the right to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the JasPer Software (in source and binary forms),
22 * and to permit persons to whom the JasPer Software is furnished to do so,
23 * provided further that the License Conditions below are met.
24 *
25 * License Conditions
26 * ******************
27 *
28 * A.  Redistributions of source code must retain the above copyright notice,
29 * and this list of conditions, and the following disclaimer.
30 *
31 * B.  Redistributions in binary form must reproduce the above copyright
32 * notice, and this list of conditions, and the following disclaimer in
33 * the documentation and/or other materials provided with the distribution.
34 *
35 * C.  Neither the name of Image Power, Inc. nor any other contributor
36 * (including, but not limited to, the University of British Columbia and
37 * Michael David Adams) may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
39 *
40 * D.  User agrees that it shall not commence any action against Image Power,
41 * Inc., the University of British Columbia, Michael David Adams, or any
42 * other contributors (collectively "Licensors") for infringement of any
43 * intellectual property rights ("IPR") held by the User in respect of any
44 * technology that User owns or has a right to license or sublicense and
45 * which is an element required in order to claim compliance with ISO/IEC
46 * 15444-1 (i.e., JPEG-2000 Part 1).  "IPR" means all intellectual property
47 * rights worldwide arising under statutory or common law, and whether
48 * or not perfected, including, without limitation, all (i) patents and
49 * patent applications owned or licensable by User; (ii) rights associated
50 * with works of authorship including copyrights, copyright applications,
51 * copyright registrations, mask work rights, mask work applications,
52 * mask work registrations; (iii) rights relating to the protection of
53 * trade secrets and confidential information; (iv) any right analogous
54 * to those set forth in subsections (i), (ii), or (iii) and any other
55 * proprietary rights relating to intangible property (other than trademark,
56 * trade dress, or service mark rights); and (v) divisions, continuations,
57 * renewals, reissues and extensions of the foregoing (as and to the extent
58 * applicable) now existing, hereafter filed, issued or acquired.
59 *
60 * E.  If User commences an infringement action against any Licensor(s) then
61 * such Licensor(s) shall have the right to terminate User's license and
62 * all sublicenses that have been granted hereunder by User to other parties.
63 *
64 * F.  This software is for use only in hardware or software products that
65 * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1).  No license
66 * or right to this Software is granted for products that do not comply
67 * with ISO/IEC 15444-1.  The JPEG-2000 Part 1 standard can be purchased
68 * from the ISO.
69 *
70 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
71 * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
72 * THIS DISCLAIMER.  THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
73 * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
74 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
75 * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
76 * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING.  THOSE INTENDING
77 * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
78 * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
79 * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
80 * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
81 * IS WITH THE USER.  SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
82 * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
83 * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
84 * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
85 * REPAIR OR CORRECTION.  UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
86 * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
87 * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
88 * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
89 * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
90 * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
91 * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
92 * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
93 * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
94 * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
95 * OF SUCH DAMAGES.  THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
96 * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
97 * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
98 * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
99 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
100 * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
101 * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
102 * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
103 * ("HIGH RISK ACTIVITIES").  LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
104 * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.  USER WILL NOT
105 * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
106 * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
107 * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
108 * NOTICE SPECIFIED IN THIS SECTION.
109 *
110 * __END_OF_JASPER_LICENSE__
111 */
112
113/*
114 * JP2 Library
115 *
116 * $Id: jp2_dec.c 14449 2005-10-20 12:15:56Z stippi $
117 */
118
119/******************************************************************************\
120* Includes.
121\******************************************************************************/
122
123#include "jasper/jas_image.h"
124#include "jasper/jas_stream.h"
125#include "jasper/jas_math.h"
126#include "jasper/jas_debug.h"
127#include "jasper/jas_malloc.h"
128#include "jasper/jas_version.h"
129
130#include "jp2_cod.h"
131#include "jp2_dec.h"
132
133#define	JP2_VALIDATELEN	(JAS_MIN(JP2_JP_LEN + 16, JAS_STREAM_MAXPUTBACK))
134
135static jp2_dec_t *jp2_dec_create(void);
136static void jp2_dec_destroy(jp2_dec_t *dec);
137
138/******************************************************************************\
139* Functions.
140\******************************************************************************/
141
142jas_image_t *jp2_decode(jas_stream_t *in, char *optstr)
143{
144	jp2_box_t *box;
145	int found;
146	jas_image_t *image;
147	jp2_dec_t *dec;
148	JPR_BOOL samedtype;
149	int dtype;
150	int i;
151	jp2_cmap_t *cmapd;
152	jp2_pclr_t *pclrd;
153	jp2_cdef_t *cdefd;
154	int channo;
155	int newcmptno;
156	int cmptno;
157	int_fast32_t *lutents;
158	jp2_cdefchan_t *cdefent;
159	jp2_cmapent_t *cmapent;
160	jpr_uchar_t *iccp;
161	int cs;
162
163	dec = 0;
164	box = 0;
165	image = 0;
166
167	if (!(dec = jp2_dec_create())) {
168		goto error;
169	}
170
171	/* Get the first box.  This should be a JP box. */
172	if (!(box = jp2_box_get(in))) {
173		jas_eprintf("error: cannot get box\n");
174		goto error;
175	}
176	if (box->type != JP2_BOX_JP) {
177		jas_eprintf("error: expecting signature box\n");
178		goto error;
179	}
180	if (box->data.jp.magic != JP2_JP_MAGIC) {
181		jas_eprintf("incorrect magic number\n");
182		goto error;
183	}
184	jp2_box_destroy(box);
185	box = 0;
186
187	/* Get the second box.  This should be a FTYP box. */
188	if (!(box = jp2_box_get(in))) {
189		goto error;
190	}
191	if (box->type != JP2_BOX_FTYP) {
192		jas_eprintf("expecting file type box\n");
193		goto error;
194	}
195	jp2_box_destroy(box);
196	box = 0;
197
198	/* Get more boxes... */
199	found = 0;
200	while ((box = jp2_box_get(in))) {
201		if (jas_getdbglevel() >= 1) {
202			fprintf(stderr, "box type %s\n", box->info->name);
203		}
204		switch (box->type) {
205		case JP2_BOX_JP2C:
206			found = 1;
207			break;
208		case JP2_BOX_IHDR:
209			if (!dec->ihdr) {
210				dec->ihdr = box;
211				box = 0;
212			}
213			break;
214		case JP2_BOX_BPCC:
215			if (!dec->bpcc) {
216				dec->bpcc = box;
217				box = 0;
218			}
219			break;
220		case JP2_BOX_CDEF:
221			if (!dec->cdef) {
222				dec->cdef = box;
223				box = 0;
224			}
225			break;
226		case JP2_BOX_PCLR:
227			if (!dec->pclr) {
228				dec->pclr = box;
229				box = 0;
230			}
231			break;
232		case JP2_BOX_CMAP:
233			if (!dec->cmap) {
234				dec->cmap = box;
235				box = 0;
236			}
237			break;
238		case JP2_BOX_COLR:
239			if (!dec->colr) {
240				dec->colr = box;
241				box = 0;
242			}
243			break;
244		}
245		if (box) {
246			jp2_box_destroy(box);
247			box = 0;
248		}
249		if (found) {
250			break;
251		}
252	}
253
254	if (!found) {
255		jas_eprintf("error: no code stream found\n");
256		goto error;
257	}
258
259	if (!(dec->image = jpc_decode(in, optstr))) {
260		jas_eprintf("error: cannot decode code stream\n");
261		goto error;
262	}
263
264	/* An IHDR box must be present. */
265	if (!dec->ihdr) {
266		jas_eprintf("error: missing IHDR box\n");
267		goto error;
268	}
269
270	/* Does the number of components indicated in the IHDR box match
271	  the value specified in the code stream? */
272	if (dec->ihdr->data.ihdr.numcmpts != jas_image_numcmpts(dec->image)) {
273		jas_eprintf("warning: number of components mismatch\n");
274	}
275
276	/* At least one component must be present. */
277	if (!jas_image_numcmpts(dec->image)) {
278		jas_eprintf("error: no components\n");
279		goto error;
280	}
281
282	/* Determine if all components have the same data type. */
283	samedtype = JPR_TRUE;
284	dtype = jas_image_cmptdtype(dec->image, 0);
285	for (i = 1; i < jas_image_numcmpts(dec->image); ++i) {
286		if (jas_image_cmptdtype(dec->image, i) != dtype) {
287			samedtype = JPR_FALSE;
288			break;
289		}
290	}
291
292	/* Is the component data type indicated in the IHDR box consistent
293	  with the data in the code stream? */
294	if ((samedtype && dec->ihdr->data.ihdr.bpc != JP2_DTYPETOBPC(dtype)) ||
295	  (!samedtype && dec->ihdr->data.ihdr.bpc != JP2_IHDR_BPCNULL)) {
296		jas_eprintf("warning: component data type mismatch\n");
297	}
298
299	/* Is the compression type supported? */
300	if (dec->ihdr->data.ihdr.comptype != JP2_IHDR_COMPTYPE) {
301		jas_eprintf("error: unsupported compression type\n");
302		goto error;
303	}
304
305	if (dec->bpcc) {
306		/* Is the number of components indicated in the BPCC box
307		  consistent with the code stream data? */
308		if (dec->bpcc->data.bpcc.numcmpts != jas_image_numcmpts(
309		  dec->image)) {
310			jas_eprintf("warning: number of components mismatch\n");
311		}
312		/* Is the component data type information indicated in the BPCC
313		  box consistent with the code stream data? */
314		if (!samedtype) {
315			for (i = 0; i < jas_image_numcmpts(dec->image); ++i) {
316				if (jas_image_cmptdtype(dec->image, i) != JP2_BPCTODTYPE(dec->bpcc->data.bpcc.bpcs[i])) {
317					jas_eprintf("warning: component data type mismatch\n");
318				}
319			}
320		} else {
321			jas_eprintf("warning: superfluous BPCC box\n");
322		}
323	}
324
325	/* A COLR box must be present. */
326	if (!dec->colr) {
327		jas_eprintf("error: no COLR box\n");
328		goto error;
329	}
330
331	switch (dec->colr->data.colr.method) {
332	case JP2_COLR_ENUM:
333		jas_image_setcolorspace(dec->image, jp2_getcs(&dec->colr->data.colr));
334		break;
335	case JP2_COLR_ICC:
336		if (dec->colr->data.colr.iccplen < 128) {
337			abort();
338		}
339		iccp = dec->colr->data.colr.iccp;
340		cs = (iccp[16] << 24) | (iccp[17] << 16) | (iccp[18] << 8) |
341		  iccp[19];
342		jas_eprintf("ICC Profile CS %08x\n", cs);
343		jas_image_setcolorspace(dec->image, fromiccpcs(cs));
344		break;
345	}
346
347	/* If a CMAP box is present, a PCLR box must also be present. */
348	if (dec->cmap && !dec->pclr) {
349		jas_eprintf("warning: missing PCLR box or superfluous CMAP box\n");
350		jp2_box_destroy(dec->cmap);
351		dec->cmap = 0;
352	}
353
354	/* If a CMAP box is not present, a PCLR box must not be present. */
355	if (!dec->cmap && dec->pclr) {
356		jas_eprintf("warning: missing CMAP box or superfluous PCLR box\n");
357		jp2_box_destroy(dec->pclr);
358		dec->pclr = 0;
359	}
360
361	/* Determine the number of channels (which is essentially the number
362	  of components after any palette mappings have been applied). */
363	dec->numchans = dec->cmap ? dec->cmap->data.cmap.numchans : jas_image_numcmpts(dec->image);
364
365	/* Perform a basic sanity check on the CMAP box if present. */
366	if (dec->cmap) {
367		for (i = 0; i < dec->numchans; ++i) {
368			/* Is the component number reasonable? */
369			if (dec->cmap->data.cmap.ents[i].cmptno >= jas_image_numcmpts(dec->image)) {
370				jas_eprintf("error: invalid component number in CMAP box\n");
371				goto error;
372			}
373			/* Is the LUT index reasonable? */
374			if (dec->cmap->data.cmap.ents[i].pcol >= dec->pclr->data.pclr.numchans) {
375				jas_eprintf("error: invalid CMAP LUT index\n");
376				goto error;
377			}
378		}
379	}
380
381	/* Allocate space for the channel-number to component-number LUT. */
382	if (!(dec->chantocmptlut = jas_malloc(dec->numchans * sizeof(uint_fast16_t)))) {
383		jas_eprintf("error: no memory\n");
384		goto error;
385	}
386
387	if (!dec->cmap) {
388		for (i = 0; i < dec->numchans; ++i) {
389			dec->chantocmptlut[i] = i;
390		}
391	} else {
392		cmapd = &dec->cmap->data.cmap;
393		pclrd = &dec->pclr->data.pclr;
394		cdefd = &dec->cdef->data.cdef;
395		for (channo = 0; channo < cmapd->numchans; ++channo) {
396			cmapent = &cmapd->ents[channo];
397			if (cmapent->map == JP2_CMAP_DIRECT) {
398				dec->chantocmptlut[channo] = channo;
399			} else if (cmapent->map == JP2_CMAP_PALETTE) {
400				lutents = jas_malloc(pclrd->numlutents * sizeof(int_fast32_t));
401				for (i = 0; i < pclrd->numlutents; ++i) {
402					lutents[i] = pclrd->lutdata[cmapent->pcol + i * pclrd->numchans];
403				}
404				newcmptno = jas_image_numcmpts(dec->image);
405				jas_image_depalettize(dec->image, cmapent->cmptno, pclrd->numlutents, lutents, JP2_BPCTODTYPE(pclrd->bpc[cmapent->pcol]), newcmptno);
406				dec->chantocmptlut[channo] = newcmptno;
407				jas_free(lutents);
408#if 0
409				if (dec->cdef) {
410					cdefent = jp2_cdef_lookup(cdefd, channo);
411					if (!cdefent) {
412						abort();
413					}
414				jas_image_setcmpttype(dec->image, newcmptno, jp2_getct(jas_image_colorspace(dec->image), cdefent->type, cdefent->assoc));
415				} else {
416				jas_image_setcmpttype(dec->image, newcmptno, jp2_getct(jas_image_colorspace(dec->image), 0, channo + 1));
417				}
418#endif
419			}
420		}
421	}
422
423	/* Mark all components as being of unknown type. */
424
425	for (i = 0; i < jas_image_numcmpts(dec->image); ++i) {
426		jas_image_setcmpttype(dec->image, i, JAS_IMAGE_CT_UNKNOWN);
427	}
428
429	/* Determine the type of each component. */
430	if (dec->cdef) {
431		for (i = 0; i < dec->numchans; ++i) {
432			jas_image_setcmpttype(dec->image,
433			  dec->chantocmptlut[dec->cdef->data.cdef.ents[i].channo],
434			  jp2_getct(jas_image_colorspace(dec->image),
435			  dec->cdef->data.cdef.ents[i].type, dec->cdef->data.cdef.ents[i].assoc));
436		}
437	} else {
438		for (i = 0; i < dec->numchans; ++i) {
439			jas_image_setcmpttype(dec->image, dec->chantocmptlut[i],
440			  jp2_getct(jas_image_colorspace(dec->image), 0, i + 1));
441		}
442	}
443
444	/* Delete any components that are not of interest. */
445	for (i = jas_image_numcmpts(dec->image) - 1; i >= 0; --i) {
446		if (jas_image_cmpttype(dec->image, i) == JAS_IMAGE_CT_UNKNOWN) {
447			jas_image_delcmpt(dec->image, i);
448		}
449	}
450
451	/* Ensure that some components survived. */
452	if (!jas_image_numcmpts(dec->image)) {
453		jas_eprintf("error: no components\n");
454		goto error;
455	}
456fprintf(stderr, "no of components is %d\n", jas_image_numcmpts(dec->image));
457
458	/* Prevent the image from being destroyed later. */
459	image = dec->image;
460	dec->image = 0;
461
462	jp2_dec_destroy(dec);
463
464	return image;
465
466error:
467	if (box) {
468		jp2_box_destroy(box);
469	}
470	if (dec) {
471		jp2_dec_destroy(dec);
472	}
473	return 0;
474}
475
476int jp2_validate(jas_stream_t *in)
477{
478	jas_stream_t *tmpstream;
479	char buf[JP2_VALIDATELEN];
480	int i;
481	int n;
482	jp2_box_t *box;
483
484	assert(JAS_STREAM_MAXPUTBACK >= JP2_VALIDATELEN);
485
486	/* Read the validation data (i.e., the data used for detecting
487	  the format). */
488	if ((n = jas_stream_read(in, buf, JP2_VALIDATELEN)) < 0) {
489		return -1;
490	}
491
492	/* Put the validation data back onto the stream, so that the
493	  stream position will not be changed. */
494	for (i = n - 1; i >= 0; --i) {
495		if (jas_stream_ungetc(in, buf[i]) == EOF) {
496			return -1;
497		}
498	}
499
500	/* Did we read enough data? */
501	if (n < JP2_VALIDATELEN) {
502		return -1;
503	}
504
505	/* Is the box type correct? */
506	if (((buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]) !=
507	  JP2_BOX_JP)
508	{
509		return -1;
510	}
511
512	return 0;
513}
514
515static jp2_dec_t *jp2_dec_create(void)
516{
517	jp2_dec_t *dec;
518
519	if (!(dec = jas_malloc(sizeof(jp2_dec_t)))) {
520		return 0;
521	}
522	dec->ihdr = 0;
523	dec->bpcc = 0;
524	dec->cdef = 0;
525	dec->pclr = 0;
526	dec->image = 0;
527	dec->chantocmptlut = 0;
528	dec->cmap = 0;
529	dec->colr = 0;
530	return dec;
531}
532
533static void jp2_dec_destroy(jp2_dec_t *dec)
534{
535	if (dec->ihdr) {
536		jp2_box_destroy(dec->ihdr);
537	}
538	if (dec->bpcc) {
539		jp2_box_destroy(dec->bpcc);
540	}
541	if (dec->cdef) {
542		jp2_box_destroy(dec->cdef);
543	}
544	if (dec->pclr) {
545		jp2_box_destroy(dec->pclr);
546	}
547	if (dec->image) {
548		jas_image_destroy(dec->image);
549	}
550	if (dec->cmap) {
551		jp2_box_destroy(dec->cmap);
552	}
553	if (dec->colr) {
554		jp2_box_destroy(dec->colr);
555	}
556	if (dec->chantocmptlut) {
557		jas_free(dec->chantocmptlut);
558	}
559	jas_free(dec);
560}
561
562
563
564
565
566
567int jp2_getct(int colorspace, int type, int assoc)
568{
569	if (type == 1 && assoc == 0) {
570		return JAS_IMAGE_CT_OPACITY;
571	}
572	if (type == 0 && assoc >= 1 && assoc <= 65534) {
573		switch (colorspace) {
574		case JAS_IMAGE_CS_RGB:
575			switch (assoc) {
576			case JP2_CDEF_RGB_R:
577				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R);
578				break;
579			case JP2_CDEF_RGB_G:
580				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G);
581				break;
582			case JP2_CDEF_RGB_B:
583				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B);
584				break;
585			}
586			break;
587		case JAS_IMAGE_CS_YCBCR:
588			switch (assoc) {
589			case JP2_CDEF_YCBCR_Y:
590				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_Y);
591				break;
592			case JP2_CDEF_YCBCR_CB:
593				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CB);
594				break;
595			case JP2_CDEF_YCBCR_CR:
596				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CR);
597				break;
598			}
599			break;
600		case JAS_IMAGE_CS_GRAY:
601			switch (assoc) {
602			case JP2_CDEF_GRAY_Y:
603				return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y);
604				break;
605			}
606			break;
607#if 0
608		case JAS_IMAGE_CS_ICC:
609#endif
610		default:
611			return JAS_IMAGE_CT_COLOR(assoc - 1);
612			break;
613		}
614	}
615	return JAS_IMAGE_CT_UNKNOWN;
616}
617
618int jp2_getcs(jp2_colr_t *colr)
619{
620	if (colr->method == JP2_COLR_ENUM) {
621		switch (colr->csid) {
622		case JP2_COLR_SRGB:
623			return JAS_IMAGE_CS_RGB;
624			break;
625		case JP2_COLR_SYCC:
626			return JAS_IMAGE_CS_YCBCR;
627			break;
628		case JP2_COLR_SGRAY:
629			return JAS_IMAGE_CS_GRAY;
630			break;
631		}
632	}
633	return JAS_IMAGE_CS_UNKNOWN;
634}
635
636int fromiccpcs(int cs)
637{
638	switch (cs) {
639	case ICC_CS_RGB:
640		return JAS_IMAGE_CS_RGB;
641		break;
642	case ICC_CS_YCBCR:
643		return JAS_IMAGE_CS_YCBCR;
644		break;
645	case ICC_CS_GRAY:
646		return JAS_IMAGE_CS_GRAY;
647		break;
648	}
649	return JAS_IMAGE_CS_UNKNOWN;
650}
651