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) 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_cod.c,v 1.1 2005/05/22 18:33:03 malaterre 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)) {
267 if (box->len != 0 && box->len < 8) {
271 dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
274 if (!(tmpstream = jas_stream_memopen(0, 0))) {
277 if (jas_stream_copy(tmpstream, in, box->len - JP2_BOX_HDRLEN)) {
280 jas_stream_rewind(tmpstream);
282 if (box->ops->getdata) {
283 if ((*box->ops->getdata)(box, tmpstream)) {
287 jas_stream_close(tmpstream);
291 jp2_box_dump(box, stderr);
299 jp2_box_destroy(box);
302 jas_stream_close(tmpstream);
307 void jp2_box_dump(jp2_box_t *box, FILE *out)
309 jp2_boxinfo_t *boxinfo;
310 boxinfo = jp2_boxinfolookup(box->type);
313 fprintf(out, "JP2 box: ");
314 fprintf(out, "type=%c%s%c (0x%08x); length=%d\n", '"', boxinfo->name,
315 '"', box->type, box->len);
316 if (box->ops->dumpdata) {
317 (*box->ops->dumpdata)(box, out);
321 static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in)
323 jp2_jp_t *jp = &box->data.jp;
324 if (jp2_getuint32(in, &jp->magic)) {
330 static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in)
332 jp2_ftyp_t *ftyp = &box->data.ftyp;
334 if (jp2_getuint32(in, &ftyp->majver) || jp2_getuint32(in, &ftyp->minver)) {
337 ftyp->numcompatcodes = ((box->len - JP2_BOX_HDRLEN) - 8) / 4;
338 if (ftyp->numcompatcodes > JP2_FTYP_MAXCOMPATCODES) {
341 for (i = 0; i < ftyp->numcompatcodes; ++i) {
342 if (jp2_getuint32(in, &ftyp->compatcodes[i])) {
349 static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in)
351 jp2_ihdr_t *ihdr = &box->data.ihdr;
352 if (jp2_getuint32(in, &ihdr->height) || jp2_getuint32(in, &ihdr->width) ||
353 jp2_getuint16(in, &ihdr->numcmpts) || jp2_getuint8(in, &ihdr->bpc) ||
354 jp2_getuint8(in, &ihdr->comptype) || jp2_getuint8(in, &ihdr->csunk) ||
355 jp2_getuint8(in, &ihdr->ipr)) {
361 static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in)
363 jp2_bpcc_t *bpcc = &box->data.bpcc;
365 bpcc->numcmpts = box->len - JP2_BOX_HDRLEN;
366 if (!(bpcc->bpcs = jas_malloc(bpcc->numcmpts * sizeof(uint_fast8_t)))) {
369 for (i = 0; i < bpcc->numcmpts; ++i) {
370 if (jp2_getuint8(in, &bpcc->bpcs[i])) {
377 static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out)
379 jp2_colr_t *colr = &box->data.colr;
380 fprintf(out, "method=%d; pri=%d; approx=%d\n", (int)colr->method, (int)colr->pri, (int)colr->approx);
381 switch (colr->method) {
383 fprintf(out, "csid=%d\n", (int)colr->csid);
386 jas_memdump(out, colr->iccp, colr->iccplen);
391 static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in)
393 jp2_colr_t *colr = &box->data.colr;
398 if (jp2_getuint8(in, &colr->method) || jp2_getuint8(in, &colr->pri) ||
399 jp2_getuint8(in, &colr->approx)) {
402 switch (colr->method) {
404 if (jp2_getuint32(in, &colr->csid)) {
409 colr->iccplen = box->len - JP2_BOX_HDRLEN - 3;
410 if (!(colr->iccp = jas_malloc(colr->iccplen * sizeof(uint_fast8_t)))) {
413 if (jas_stream_read(in, colr->iccp, colr->iccplen) != colr->iccplen) {
421 static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out)
423 jp2_cdef_t *cdef = &box->data.cdef;
425 for (i = 0; i < cdef->numchans; ++i) {
426 fprintf(out, "channo=%d; type=%d; assoc=%d\n",
427 cdef->ents[i].channo, cdef->ents[i].type, cdef->ents[i].assoc);
431 static void jp2_colr_destroy(jp2_box_t *box)
433 jp2_colr_t *colr = &box->data.colr;
435 jas_free(colr->iccp);
439 static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in)
441 jp2_cdef_t *cdef = &box->data.cdef;
442 jp2_cdefchan_t *chan;
444 if (jp2_getuint16(in, &cdef->numchans)) {
447 if (!(cdef->ents = jas_malloc(cdef->numchans * sizeof(jp2_cdefchan_t)))) {
450 for (channo = 0; channo < cdef->numchans; ++channo) {
451 chan = &cdef->ents[channo];
452 if (jp2_getuint16(in, &chan->channo) || jp2_getuint16(in, &chan->type) ||
453 jp2_getuint16(in, &chan->assoc)) {
460 /******************************************************************************\
462 \******************************************************************************/
464 int jp2_box_put(jp2_box_t *box, jas_stream_t *out)
466 jas_stream_t *tmpstream;
472 dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
475 tmpstream = jas_stream_memopen(0, 0);
476 if (box->ops->putdata) {
477 if ((*box->ops->putdata)(box, tmpstream)) {
481 box->len = jas_stream_tell(tmpstream) + JP2_BOX_HDRLEN;
482 jas_stream_rewind(tmpstream);
484 extlen = (box->len >= (((uint_fast64_t)1) << 32)) != 0;
485 if (jp2_putuint32(out, extlen ? 1 : box->len)) {
488 if (jp2_putuint32(out, box->type)) {
492 if (jp2_putuint64(out, box->len)) {
498 if (jas_stream_copy(out, tmpstream, box->len - JP2_BOX_HDRLEN)) {
501 jas_stream_close(tmpstream);
510 jas_stream_close(tmpstream);
515 static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out)
517 jp2_jp_t *jp = &box->data.jp;
518 if (jp2_putuint32(out, jp->magic)) {
524 static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out)
526 jp2_ftyp_t *ftyp = &box->data.ftyp;
528 if (jp2_putuint32(out, ftyp->majver) || jp2_putuint32(out, ftyp->minver)) {
531 for (i = 0; i < ftyp->numcompatcodes; ++i) {
532 if (jp2_putuint32(out, ftyp->compatcodes[i])) {
539 static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out)
541 jp2_ihdr_t *ihdr = &box->data.ihdr;
542 if (jp2_putuint32(out, ihdr->height) || jp2_putuint32(out, ihdr->width) ||
543 jp2_putuint16(out, ihdr->numcmpts) || jp2_putuint8(out, ihdr->bpc) ||
544 jp2_putuint8(out, ihdr->comptype) || jp2_putuint8(out, ihdr->csunk) ||
545 jp2_putuint8(out, ihdr->ipr)) {
551 static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out)
553 jp2_bpcc_t *bpcc = &box->data.bpcc;
555 for (i = 0; i < bpcc->numcmpts; ++i) {
556 if (jp2_putuint8(out, bpcc->bpcs[i])) {
563 static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out)
565 jp2_colr_t *colr = &box->data.colr;
566 if (jp2_putuint8(out, colr->method) || jp2_putuint8(out, colr->pri) ||
567 jp2_putuint8(out, colr->approx)) {
570 switch (colr->method) {
572 if (jp2_putuint32(out, colr->csid)) {
577 if (jas_stream_write(out, colr->iccp,
578 JAS_CAST(int, colr->iccplen)) != JAS_CAST(int, colr->iccplen))
585 static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out)
587 jp2_cdef_t *cdef = &box->data.cdef;
591 if (jp2_putuint16(out, cdef->numchans)) {
595 for (i = 0; i < cdef->numchans; ++i) {
596 ent = &cdef->ents[i];
597 if (jp2_putuint16(out, ent->channo) ||
598 jp2_putuint16(out, ent->type) ||
599 jp2_putuint16(out, ent->assoc)) {
606 /******************************************************************************\
607 * Input operations for primitive types.
608 \******************************************************************************/
610 static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val)
613 if ((c = jas_stream_getc(in)) == EOF) {
622 static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val)
626 if ((c = jas_stream_getc(in)) == EOF) {
630 if ((c = jas_stream_getc(in)) == EOF) {
640 static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val)
644 if ((c = jas_stream_getc(in)) == EOF) {
648 if ((c = jas_stream_getc(in)) == EOF) {
652 if ((c = jas_stream_getc(in)) == EOF) {
656 if ((c = jas_stream_getc(in)) == EOF) {
666 static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val)
673 /******************************************************************************\
674 * Output operations for primitive types.
675 \******************************************************************************/
677 static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val)
679 if (jas_stream_putc(out, val & 0xff) == EOF) {
685 static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val)
687 if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
688 jas_stream_putc(out, val & 0xff) == EOF) {
694 static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val)
696 if (jas_stream_putc(out, (val >> 24) & 0xff) == EOF ||
697 jas_stream_putc(out, (val >> 16) & 0xff) == EOF ||
698 jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
699 jas_stream_putc(out, val & 0xff) == EOF) {
705 static int jp2_putuint64(jas_stream_t *out, uint_fast64_t val)
707 if (jp2_putuint32(out, (val >> 32) & 0xffffffffUL) ||
708 jp2_putuint32(out, val & 0xffffffffUL)) {
714 /******************************************************************************\
715 * Miscellaneous code.
716 \******************************************************************************/
718 jp2_boxinfo_t *jp2_boxinfolookup(int type)
720 jp2_boxinfo_t *boxinfo;
721 for (boxinfo = jp2_boxinfos; boxinfo->name; ++boxinfo) {
722 if (boxinfo->type == type) {
726 return &jp2_boxinfo_unk;
733 static void jp2_cmap_destroy(jp2_box_t *box)
735 jp2_cmap_t *cmap = &box->data.cmap;
737 jas_free(cmap->ents);
741 static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in)
743 jp2_cmap_t *cmap = &box->data.cmap;
747 cmap->numchans = (box->len - JP2_BOX_HDRLEN) / 4;
748 if (!(cmap->ents = jas_malloc(cmap->numchans * sizeof(jp2_cmapent_t)))) {
751 for (i = 0; i < cmap->numchans; ++i) {
752 ent = &cmap->ents[i];
753 if (jp2_getuint16(in, &ent->cmptno) ||
754 jp2_getuint8(in, &ent->map) ||
755 jp2_getuint8(in, &ent->pcol)) {
763 static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out)
765 /* Eliminate compiler warning about unused variables. */
772 static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out)
774 jp2_cmap_t *cmap = &box->data.cmap;
777 fprintf(out, "numchans = %d\n", (int) cmap->numchans);
778 for (i = 0; i < cmap->numchans; ++i) {
779 ent = &cmap->ents[i];
780 fprintf(out, "cmptno=%d; map=%d; pcol=%d\n",
781 (int) ent->cmptno, (int) ent->map, (int) ent->pcol);
785 static void jp2_pclr_destroy(jp2_box_t *box)
787 jp2_pclr_t *pclr = &box->data.pclr;
789 jas_free(pclr->lutdata);
795 static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in)
797 jp2_pclr_t *pclr = &box->data.pclr;
805 if (jp2_getuint16(in, &pclr->numlutents) ||
806 jp2_getuint8(in, &pclr->numchans)) {
809 lutsize = pclr->numlutents * pclr->numchans;
810 if (!(pclr->lutdata = jas_malloc(lutsize * sizeof(int_fast32_t)))) {
813 if (!(pclr->bpc = jas_malloc(pclr->numchans * sizeof(uint_fast8_t)))) {
816 for (i = 0; i < pclr->numchans; ++i) {
817 if (jp2_getuint8(in, &pclr->bpc[i])) {
821 for (i = 0; i < pclr->numlutents; ++i) {
822 for (j = 0; j < pclr->numchans; ++j) {
823 if (jp2_getint(in, (pclr->bpc[j] & 0x80) != 0,
824 (pclr->bpc[j] & 0x7f) + 1, &x)) {
827 pclr->lutdata[i * pclr->numchans + j] = x;
833 static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out)
836 jp2_pclr_t *pclr = &box->data.pclr;
838 /* Eliminate warning about unused variable. */
844 static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out)
846 jp2_pclr_t *pclr = &box->data.pclr;
849 fprintf(out, "numents=%d; numchans=%d\n", (int) pclr->numlutents,
850 (int) pclr->numchans);
851 for (i = 0; i < pclr->numlutents; ++i) {
852 for (j = 0; j < pclr->numchans; ++j) {
853 fprintf(out, "LUT[%d][%d]=%d\n", i, j, pclr->lutdata[i * pclr->numchans + j]);
858 static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val)
868 for (i = 0; i < m; ++i) {
869 if ((c = jas_stream_getc(in)) == EOF) {
877 sb = v & (1 << (8 * m - 1));
878 *val = ((~v) + 1) & ONES(8 * m);
889 jp2_cdefchan_t *jp2_cdef_lookup(jp2_cdef_t *cdef, int channo)
892 jp2_cdefchan_t *cdefent;
893 for (i = 0; i < cdef->numchans; ++i) {
894 cdefent = &cdef->ents[i];
895 if (cdefent->channo == JAS_CAST(unsigned int, channo)) {