2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2002 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
12 * Copyright (c) 2001-2006 Michael David Adams
13 * Copyright (c) 1999-2000 Image Power, Inc.
14 * Copyright (c) 1999-2000 The University of British Columbia
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_cod.c,v 1.2 2008/05/26 09:40:52 vp153 Exp $
70 /******************************************************************************\
72 \******************************************************************************/
77 #include "jasper/jas_stream.h"
78 #include "jasper/jas_malloc.h"
79 #include "jasper/jas_debug.h"
83 /******************************************************************************\
84 * Function prototypes.
85 \******************************************************************************/
87 #define ONES(n) ((1 << (n)) - 1)
89 jp2_boxinfo_t *jp2_boxinfolookup(int type);
91 static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val);
92 static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val);
93 static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val);
94 static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val);
95 static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val);
96 static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val);
97 static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val);
98 static int jp2_putuint64(jas_stream_t *out, uint_fast64_t val);
100 static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val);
102 jp2_box_t *jp2_box_get(jas_stream_t *in);
103 void jp2_box_dump(jp2_box_t *box, FILE *out);
105 static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in);
106 static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out);
107 static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in);
108 static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out);
109 static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in);
110 static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out);
111 static void jp2_bpcc_destroy(jp2_box_t *box);
112 static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in);
113 static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out);
114 static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in);
115 static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out);
116 static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out);
117 static void jp2_colr_destroy(jp2_box_t *box);
118 static void jp2_cdef_destroy(jp2_box_t *box);
119 static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in);
120 static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out);
121 static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out);
122 static void jp2_cmap_destroy(jp2_box_t *box);
123 static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in);
124 static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out);
125 static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out);
126 static void jp2_pclr_destroy(jp2_box_t *box);
127 static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in);
128 static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out);
129 static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out);
131 /******************************************************************************\
133 \******************************************************************************/
135 jp2_boxinfo_t jp2_boxinfos[] = {
136 {JP2_BOX_JP, "JP", 0,
137 {0, 0, jp2_jp_getdata, jp2_jp_putdata, 0}},
138 {JP2_BOX_FTYP, "FTYP", 0,
139 {0, 0, jp2_ftyp_getdata, jp2_ftyp_putdata, 0}},
140 {JP2_BOX_JP2H, "JP2H", JP2_BOX_SUPER,
142 {JP2_BOX_IHDR, "IHDR", 0,
143 {0, 0, jp2_ihdr_getdata, jp2_ihdr_putdata, 0}},
144 {JP2_BOX_BPCC, "BPCC", 0,
145 {0, jp2_bpcc_destroy, jp2_bpcc_getdata, jp2_bpcc_putdata, 0}},
146 {JP2_BOX_COLR, "COLR", 0,
147 {0, jp2_colr_destroy, jp2_colr_getdata, jp2_colr_putdata, jp2_colr_dumpdata}},
148 {JP2_BOX_PCLR, "PCLR", 0,
149 {0, jp2_pclr_destroy, jp2_pclr_getdata, jp2_pclr_putdata, jp2_pclr_dumpdata}},
150 {JP2_BOX_CMAP, "CMAP", 0,
151 {0, jp2_cmap_destroy, jp2_cmap_getdata, jp2_cmap_putdata, jp2_cmap_dumpdata}},
152 {JP2_BOX_CDEF, "CDEF", 0,
153 {0, jp2_cdef_destroy, jp2_cdef_getdata, jp2_cdef_putdata, jp2_cdef_dumpdata}},
154 {JP2_BOX_RES, "RES", JP2_BOX_SUPER,
156 {JP2_BOX_RESC, "RESC", 0,
158 {JP2_BOX_RESD, "RESD", 0,
160 {JP2_BOX_JP2C, "JP2C", JP2_BOX_NODATA,
162 {JP2_BOX_JP2I, "JP2I", 0,
164 {JP2_BOX_XML, "XML", 0,
166 {JP2_BOX_UUID, "UUID", 0,
168 {JP2_BOX_UINF, "UINF", JP2_BOX_SUPER,
170 {JP2_BOX_ULST, "ULST", 0,
172 {JP2_BOX_URL, "URL", 0,
174 {0, 0, 0, {0, 0, 0, 0, 0}},
177 jp2_boxinfo_t jp2_boxinfo_unk = {
178 0, "Unknown", 0, {0, 0, 0, 0, 0}
181 /******************************************************************************\
183 \******************************************************************************/
185 jp2_box_t *jp2_box_create(int type)
188 jp2_boxinfo_t *boxinfo;
190 if (!(box = jas_malloc(sizeof(jp2_box_t)))) {
193 memset(box, 0, sizeof(jp2_box_t));
196 if (!(boxinfo = jp2_boxinfolookup(type))) {
200 box->ops = &boxinfo->ops;
204 /******************************************************************************\
206 \******************************************************************************/
208 void jp2_box_destroy(jp2_box_t *box)
210 if (box->ops->destroy) {
211 (*box->ops->destroy)(box);
216 static void jp2_bpcc_destroy(jp2_box_t *box)
218 jp2_bpcc_t *bpcc = &box->data.bpcc;
220 jas_free(bpcc->bpcs);
225 static void jp2_cdef_destroy(jp2_box_t *box)
227 jp2_cdef_t *cdef = &box->data.cdef;
229 jas_free(cdef->ents);
234 /******************************************************************************\
236 \******************************************************************************/
238 jp2_box_t *jp2_box_get(jas_stream_t *in)
241 jp2_boxinfo_t *boxinfo;
242 jas_stream_t *tmpstream;
244 uint_fast64_t extlen;
250 if (!(box = jas_malloc(sizeof(jp2_box_t)))) {
253 box->ops = &jp2_boxinfo_unk.ops;
254 if (jp2_getuint32(in, &len) || jp2_getuint32(in, &box->type)) {
257 boxinfo = jp2_boxinfolookup(box->type);
259 box->ops = &boxinfo->ops;
262 if (jp2_getuint64(in, &extlen)) {
265 if (extlen > 0xffffffffUL) {
266 jas_eprintf("warning: cannot handle large 64-bit box length\n");
267 extlen = 0xffffffffUL;
270 box->datalen = extlen - JP2_BOX_HDRLEN(true);
272 box->datalen = box->len - JP2_BOX_HDRLEN(false);
274 if (box->len != 0 && box->len < 8) {
278 dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
281 if (!(tmpstream = jas_stream_memopen(0, 0))) {
284 if (jas_stream_copy(tmpstream, in, box->datalen)) {
285 jas_eprintf("cannot copy box data\n");
288 jas_stream_rewind(tmpstream);
290 if (box->ops->getdata) {
291 if ((*box->ops->getdata)(box, tmpstream)) {
292 jas_eprintf("cannot parse box data\n");
296 jas_stream_close(tmpstream);
299 if (jas_getdbglevel() >= 1) {
300 jp2_box_dump(box, stderr);
308 jp2_box_destroy(box);
311 jas_stream_close(tmpstream);
316 void jp2_box_dump(jp2_box_t *box, FILE *out)
318 jp2_boxinfo_t *boxinfo;
319 boxinfo = jp2_boxinfolookup(box->type);
322 fprintf(out, "JP2 box: ");
323 fprintf(out, "type=%c%s%c (0x%08x); length=%d\n", '"', boxinfo->name,
324 '"', box->type, box->len);
325 if (box->ops->dumpdata) {
326 (*box->ops->dumpdata)(box, out);
330 static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in)
332 jp2_jp_t *jp = &box->data.jp;
333 if (jp2_getuint32(in, &jp->magic)) {
339 static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in)
341 jp2_ftyp_t *ftyp = &box->data.ftyp;
343 if (jp2_getuint32(in, &ftyp->majver) || jp2_getuint32(in, &ftyp->minver)) {
346 ftyp->numcompatcodes = (box->datalen - 8) / 4;
347 if (ftyp->numcompatcodes > JP2_FTYP_MAXCOMPATCODES) {
350 for (i = 0; i < ftyp->numcompatcodes; ++i) {
351 if (jp2_getuint32(in, &ftyp->compatcodes[i])) {
358 static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in)
360 jp2_ihdr_t *ihdr = &box->data.ihdr;
361 if (jp2_getuint32(in, &ihdr->height) || jp2_getuint32(in, &ihdr->width) ||
362 jp2_getuint16(in, &ihdr->numcmpts) || jp2_getuint8(in, &ihdr->bpc) ||
363 jp2_getuint8(in, &ihdr->comptype) || jp2_getuint8(in, &ihdr->csunk) ||
364 jp2_getuint8(in, &ihdr->ipr)) {
370 static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in)
372 jp2_bpcc_t *bpcc = &box->data.bpcc;
374 bpcc->numcmpts = box->datalen;
375 if (!(bpcc->bpcs = jas_malloc(bpcc->numcmpts * sizeof(uint_fast8_t)))) {
378 for (i = 0; i < bpcc->numcmpts; ++i) {
379 if (jp2_getuint8(in, &bpcc->bpcs[i])) {
386 static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out)
388 jp2_colr_t *colr = &box->data.colr;
389 fprintf(out, "method=%d; pri=%d; approx=%d\n", (int)colr->method, (int)colr->pri, (int)colr->approx);
390 switch (colr->method) {
392 fprintf(out, "csid=%d\n", (int)colr->csid);
395 jas_memdump(out, colr->iccp, colr->iccplen);
400 static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in)
402 jp2_colr_t *colr = &box->data.colr;
407 if (jp2_getuint8(in, &colr->method) || jp2_getuint8(in, &colr->pri) ||
408 jp2_getuint8(in, &colr->approx)) {
411 switch (colr->method) {
413 if (jp2_getuint32(in, &colr->csid)) {
418 colr->iccplen = box->datalen - 3;
419 if (!(colr->iccp = jas_malloc(colr->iccplen * sizeof(uint_fast8_t)))) {
422 if (jas_stream_read(in, colr->iccp, colr->iccplen) != colr->iccplen) {
430 static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out)
432 jp2_cdef_t *cdef = &box->data.cdef;
434 for (i = 0; i < cdef->numchans; ++i) {
435 fprintf(out, "channo=%d; type=%d; assoc=%d\n",
436 cdef->ents[i].channo, cdef->ents[i].type, cdef->ents[i].assoc);
440 static void jp2_colr_destroy(jp2_box_t *box)
442 jp2_colr_t *colr = &box->data.colr;
444 jas_free(colr->iccp);
448 static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in)
450 jp2_cdef_t *cdef = &box->data.cdef;
451 jp2_cdefchan_t *chan;
453 if (jp2_getuint16(in, &cdef->numchans)) {
456 if (!(cdef->ents = jas_malloc(cdef->numchans * sizeof(jp2_cdefchan_t)))) {
459 for (channo = 0; channo < cdef->numchans; ++channo) {
460 chan = &cdef->ents[channo];
461 if (jp2_getuint16(in, &chan->channo) || jp2_getuint16(in, &chan->type) ||
462 jp2_getuint16(in, &chan->assoc)) {
469 /******************************************************************************\
471 \******************************************************************************/
473 int jp2_box_put(jp2_box_t *box, jas_stream_t *out)
475 jas_stream_t *tmpstream;
481 dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
484 tmpstream = jas_stream_memopen(0, 0);
485 if (box->ops->putdata) {
486 if ((*box->ops->putdata)(box, tmpstream)) {
490 box->len = jas_stream_tell(tmpstream) + JP2_BOX_HDRLEN(false);
491 jas_stream_rewind(tmpstream);
493 extlen = (box->len >= (((uint_fast64_t)1) << 32)) != 0;
494 if (jp2_putuint32(out, extlen ? 1 : box->len)) {
497 if (jp2_putuint32(out, box->type)) {
501 if (jp2_putuint64(out, box->len)) {
507 if (jas_stream_copy(out, tmpstream, box->len - JP2_BOX_HDRLEN(false))) {
510 jas_stream_close(tmpstream);
519 jas_stream_close(tmpstream);
524 static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out)
526 jp2_jp_t *jp = &box->data.jp;
527 if (jp2_putuint32(out, jp->magic)) {
533 static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out)
535 jp2_ftyp_t *ftyp = &box->data.ftyp;
537 if (jp2_putuint32(out, ftyp->majver) || jp2_putuint32(out, ftyp->minver)) {
540 for (i = 0; i < ftyp->numcompatcodes; ++i) {
541 if (jp2_putuint32(out, ftyp->compatcodes[i])) {
548 static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out)
550 jp2_ihdr_t *ihdr = &box->data.ihdr;
551 if (jp2_putuint32(out, ihdr->height) || jp2_putuint32(out, ihdr->width) ||
552 jp2_putuint16(out, ihdr->numcmpts) || jp2_putuint8(out, ihdr->bpc) ||
553 jp2_putuint8(out, ihdr->comptype) || jp2_putuint8(out, ihdr->csunk) ||
554 jp2_putuint8(out, ihdr->ipr)) {
560 static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out)
562 jp2_bpcc_t *bpcc = &box->data.bpcc;
564 for (i = 0; i < bpcc->numcmpts; ++i) {
565 if (jp2_putuint8(out, bpcc->bpcs[i])) {
572 static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out)
574 jp2_colr_t *colr = &box->data.colr;
575 if (jp2_putuint8(out, colr->method) || jp2_putuint8(out, colr->pri) ||
576 jp2_putuint8(out, colr->approx)) {
579 switch (colr->method) {
581 if (jp2_putuint32(out, colr->csid)) {
586 if (jas_stream_write(out, colr->iccp,
587 JAS_CAST(int, colr->iccplen)) != JAS_CAST(int, colr->iccplen))
594 static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out)
596 jp2_cdef_t *cdef = &box->data.cdef;
600 if (jp2_putuint16(out, cdef->numchans)) {
604 for (i = 0; i < cdef->numchans; ++i) {
605 ent = &cdef->ents[i];
606 if (jp2_putuint16(out, ent->channo) ||
607 jp2_putuint16(out, ent->type) ||
608 jp2_putuint16(out, ent->assoc)) {
615 /******************************************************************************\
616 * Input operations for primitive types.
617 \******************************************************************************/
619 static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val)
622 if ((c = jas_stream_getc(in)) == EOF) {
631 static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val)
635 if ((c = jas_stream_getc(in)) == EOF) {
639 if ((c = jas_stream_getc(in)) == EOF) {
649 static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val)
653 if ((c = jas_stream_getc(in)) == EOF) {
657 if ((c = jas_stream_getc(in)) == EOF) {
661 if ((c = jas_stream_getc(in)) == EOF) {
665 if ((c = jas_stream_getc(in)) == EOF) {
675 static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val)
677 uint_fast64_t tmpval;
682 for (i = 0; i < 8; ++i) {
684 if ((c = jas_stream_getc(in)) == EOF) {
687 tmpval |= (c & 0xff);
694 /******************************************************************************\
695 * Output operations for primitive types.
696 \******************************************************************************/
698 static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val)
700 if (jas_stream_putc(out, val & 0xff) == EOF) {
706 static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val)
708 if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
709 jas_stream_putc(out, val & 0xff) == EOF) {
715 static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val)
717 if (jas_stream_putc(out, (val >> 24) & 0xff) == EOF ||
718 jas_stream_putc(out, (val >> 16) & 0xff) == EOF ||
719 jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
720 jas_stream_putc(out, val & 0xff) == EOF) {
726 static int jp2_putuint64(jas_stream_t *out, uint_fast64_t val)
728 if (jp2_putuint32(out, (val >> 32) & 0xffffffffUL) ||
729 jp2_putuint32(out, val & 0xffffffffUL)) {
735 /******************************************************************************\
736 * Miscellaneous code.
737 \******************************************************************************/
739 jp2_boxinfo_t *jp2_boxinfolookup(int type)
741 jp2_boxinfo_t *boxinfo;
742 for (boxinfo = jp2_boxinfos; boxinfo->name; ++boxinfo) {
743 if (boxinfo->type == type) {
747 return &jp2_boxinfo_unk;
754 static void jp2_cmap_destroy(jp2_box_t *box)
756 jp2_cmap_t *cmap = &box->data.cmap;
758 jas_free(cmap->ents);
762 static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in)
764 jp2_cmap_t *cmap = &box->data.cmap;
768 cmap->numchans = (box->datalen) / 4;
769 if (!(cmap->ents = jas_malloc(cmap->numchans * sizeof(jp2_cmapent_t)))) {
772 for (i = 0; i < cmap->numchans; ++i) {
773 ent = &cmap->ents[i];
774 if (jp2_getuint16(in, &ent->cmptno) ||
775 jp2_getuint8(in, &ent->map) ||
776 jp2_getuint8(in, &ent->pcol)) {
784 static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out)
786 /* Eliminate compiler warning about unused variables. */
793 static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out)
795 jp2_cmap_t *cmap = &box->data.cmap;
798 fprintf(out, "numchans = %d\n", (int) cmap->numchans);
799 for (i = 0; i < cmap->numchans; ++i) {
800 ent = &cmap->ents[i];
801 fprintf(out, "cmptno=%d; map=%d; pcol=%d\n",
802 (int) ent->cmptno, (int) ent->map, (int) ent->pcol);
806 static void jp2_pclr_destroy(jp2_box_t *box)
808 jp2_pclr_t *pclr = &box->data.pclr;
810 jas_free(pclr->lutdata);
816 static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in)
818 jp2_pclr_t *pclr = &box->data.pclr;
826 if (jp2_getuint16(in, &pclr->numlutents) ||
827 jp2_getuint8(in, &pclr->numchans)) {
830 lutsize = pclr->numlutents * pclr->numchans;
831 if (!(pclr->lutdata = jas_malloc(lutsize * sizeof(int_fast32_t)))) {
834 if (!(pclr->bpc = jas_malloc(pclr->numchans * sizeof(uint_fast8_t)))) {
837 for (i = 0; i < pclr->numchans; ++i) {
838 if (jp2_getuint8(in, &pclr->bpc[i])) {
842 for (i = 0; i < pclr->numlutents; ++i) {
843 for (j = 0; j < pclr->numchans; ++j) {
844 if (jp2_getint(in, (pclr->bpc[j] & 0x80) != 0,
845 (pclr->bpc[j] & 0x7f) + 1, &x)) {
848 pclr->lutdata[i * pclr->numchans + j] = x;
854 static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out)
857 jp2_pclr_t *pclr = &box->data.pclr;
859 /* Eliminate warning about unused variable. */
865 static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out)
867 jp2_pclr_t *pclr = &box->data.pclr;
870 fprintf(out, "numents=%d; numchans=%d\n", (int) pclr->numlutents,
871 (int) pclr->numchans);
872 for (i = 0; i < pclr->numlutents; ++i) {
873 for (j = 0; j < pclr->numchans; ++j) {
874 fprintf(out, "LUT[%d][%d]=%d\n", i, j, pclr->lutdata[i * pclr->numchans + j]);
879 static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val)
889 for (i = 0; i < m; ++i) {
890 if ((c = jas_stream_getc(in)) == EOF) {
898 sb = v & (1 << (8 * m - 1));
899 *val = ((~v) + 1) & ONES(8 * m);
910 jp2_cdefchan_t *jp2_cdef_lookup(jp2_cdef_t *cdef, int channo)
913 jp2_cdefchan_t *cdefent;
914 for (i = 0; i < cdef->numchans; ++i) {
915 cdefent = &cdef->ents[i];
916 if (cdefent->channo == JAS_CAST(unsigned int, channo)) {