2
* Copyright (c) 1999-2000 Image Power, Inc. and the University of
4
* Copyright (c) 2001-2002 Michael David Adams.
9
* Modified by Andrey Kiselev <dron@remotesensing.org> to handle UUID
13
/* __START_OF_JASPER_LICENSE__
15
* JasPer License Version 2.0
17
* Copyright (c) 2001-2006 Michael David Adams
18
* Copyright (c) 1999-2000 Image Power, Inc.
19
* Copyright (c) 1999-2000 The University of British Columbia
21
* All rights reserved.
23
* Permission is hereby granted, free of charge, to any person (the
24
* "User") obtaining a copy of this software and associated documentation
25
* files (the "Software"), to deal in the Software without restriction,
26
* including without limitation the rights to use, copy, modify, merge,
27
* publish, distribute, and/or sell copies of the Software, and to permit
28
* persons to whom the Software is furnished to do so, subject to the
29
* following conditions:
31
* 1. The above copyright notices and this permission notice (which
32
* includes the disclaimer below) shall be included in all copies or
33
* substantial portions of the Software.
35
* 2. The name of a copyright holder shall not be used to endorse or
36
* promote products derived from the Software without specific prior
39
* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
40
* LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
41
* THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
42
* "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
43
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
44
* PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
45
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
46
* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
47
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
48
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
49
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
50
* PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
51
* THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
52
* EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
53
* BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
54
* PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
55
* GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
56
* ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
57
* IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
58
* SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
59
* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
60
* SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
61
* THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
62
* PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
63
* RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
64
* EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
66
* __END_OF_JASPER_LICENSE__
72
* $Id: jp2_cod.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
75
/******************************************************************************\
77
\******************************************************************************/
82
#include "jasper/jas_stream.h"
83
#include "jasper/jas_malloc.h"
84
#include "jasper/jas_debug.h"
88
/******************************************************************************\
89
* Function prototypes.
90
\******************************************************************************/
92
#define ONES(n) ((1 << (n)) - 1)
94
jp2_boxinfo_t *jp2_boxinfolookup(int type);
96
static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val);
97
static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val);
98
static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val);
99
static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val);
100
static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val);
101
static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val);
102
static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val);
103
static int jp2_putuint64(jas_stream_t *out, uint_fast64_t val);
105
static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val);
107
jp2_box_t *jp2_box_get(jas_stream_t *in);
108
void jp2_box_dump(jp2_box_t *box, FILE *out);
110
static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in);
111
static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out);
112
static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in);
113
static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out);
114
static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in);
115
static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out);
116
static void jp2_bpcc_destroy(jp2_box_t *box);
117
static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in);
118
static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out);
119
static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in);
120
static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out);
121
static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out);
122
static void jp2_colr_destroy(jp2_box_t *box);
123
static void jp2_cdef_destroy(jp2_box_t *box);
124
static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in);
125
static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out);
126
static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out);
127
static void jp2_cmap_destroy(jp2_box_t *box);
128
static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in);
129
static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out);
130
static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out);
131
static void jp2_pclr_destroy(jp2_box_t *box);
132
static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in);
133
static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out);
134
static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out);
135
static void jp2_uuid_destroy(jp2_box_t *box);
136
static int jp2_uuid_getdata(jp2_box_t *box, jas_stream_t *in);
137
static int jp2_uuid_putdata(jp2_box_t *box, jas_stream_t *out);
139
/******************************************************************************\
141
\******************************************************************************/
143
jp2_boxinfo_t jp2_boxinfos[] = {
144
{JP2_BOX_JP, "JP", 0,
145
{0, 0, jp2_jp_getdata, jp2_jp_putdata, 0}},
146
{JP2_BOX_FTYP, "FTYP", 0,
147
{0, 0, jp2_ftyp_getdata, jp2_ftyp_putdata, 0}},
148
{JP2_BOX_JP2H, "JP2H", JP2_BOX_SUPER,
150
{JP2_BOX_IHDR, "IHDR", 0,
151
{0, 0, jp2_ihdr_getdata, jp2_ihdr_putdata, 0}},
152
{JP2_BOX_BPCC, "BPCC", 0,
153
{0, jp2_bpcc_destroy, jp2_bpcc_getdata, jp2_bpcc_putdata, 0}},
154
{JP2_BOX_COLR, "COLR", 0,
155
{0, jp2_colr_destroy, jp2_colr_getdata, jp2_colr_putdata, jp2_colr_dumpdata}},
156
{JP2_BOX_PCLR, "PCLR", 0,
157
{0, jp2_pclr_destroy, jp2_pclr_getdata, jp2_pclr_putdata, jp2_pclr_dumpdata}},
158
{JP2_BOX_CMAP, "CMAP", 0,
159
{0, jp2_cmap_destroy, jp2_cmap_getdata, jp2_cmap_putdata, jp2_cmap_dumpdata}},
160
{JP2_BOX_CDEF, "CDEF", 0,
161
{0, jp2_cdef_destroy, jp2_cdef_getdata, jp2_cdef_putdata, jp2_cdef_dumpdata}},
162
{JP2_BOX_RES, "RES", JP2_BOX_SUPER,
164
{JP2_BOX_RESC, "RESC", 0,
166
{JP2_BOX_RESD, "RESD", 0,
168
{JP2_BOX_JP2C, "JP2C", JP2_BOX_NODATA,
170
{JP2_BOX_JP2I, "JP2I", 0,
172
{JP2_BOX_XML, "XML", 0,
174
{JP2_BOX_UUID, "UUID", 0,
175
{0, jp2_uuid_destroy, jp2_uuid_getdata, jp2_uuid_putdata, 0}},
176
{JP2_BOX_UINF, "UINF", JP2_BOX_SUPER,
178
{JP2_BOX_ULST, "ULST", 0,
180
{JP2_BOX_URL, "URL", 0,
182
{0, 0, 0, {0, 0, 0, 0, 0}},
185
jp2_boxinfo_t jp2_boxinfo_unk = {
186
0, "Unknown", 0, {0, 0, 0, 0, 0}
189
/******************************************************************************\
191
\******************************************************************************/
193
jp2_box_t *jp2_box_create(int type)
196
jp2_boxinfo_t *boxinfo;
198
if (!(box = jas_malloc(sizeof(jp2_box_t)))) {
201
memset(box, 0, sizeof(jp2_box_t));
204
if (!(boxinfo = jp2_boxinfolookup(type))) {
208
box->ops = &boxinfo->ops;
212
/******************************************************************************\
214
\******************************************************************************/
216
void jp2_box_destroy(jp2_box_t *box)
218
if (box->ops->destroy) {
219
(*box->ops->destroy)(box);
224
static void jp2_bpcc_destroy(jp2_box_t *box)
226
jp2_bpcc_t *bpcc = &box->data.bpcc;
228
jas_free(bpcc->bpcs);
233
static void jp2_cdef_destroy(jp2_box_t *box)
235
jp2_cdef_t *cdef = &box->data.cdef;
237
jas_free(cdef->ents);
242
/******************************************************************************\
244
\******************************************************************************/
246
jp2_box_t *jp2_box_get(jas_stream_t *in)
249
jp2_boxinfo_t *boxinfo;
250
jas_stream_t *tmpstream;
252
uint_fast64_t extlen;
258
if (!(box = jas_malloc(sizeof(jp2_box_t)))) {
261
box->ops = &jp2_boxinfo_unk.ops;
262
if (jp2_getuint32(in, &len) || jp2_getuint32(in, &box->type)) {
265
boxinfo = jp2_boxinfolookup(box->type);
267
box->ops = &boxinfo->ops;
270
if (jp2_getuint64(in, &extlen)) {
273
if (extlen > 0xffffffffUL) {
274
jas_eprintf("warning: cannot handle large 64-bit box length\n");
275
extlen = 0xffffffffUL;
278
box->datalen = extlen - JP2_BOX_HDRLEN(true);
280
box->datalen = box->len - JP2_BOX_HDRLEN(false);
282
if (box->len != 0 && box->len < 8) {
286
dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
289
if (!(tmpstream = jas_stream_memopen(0, 0))) {
292
if (jas_stream_copy(tmpstream, in, box->datalen)) {
293
jas_eprintf("cannot copy box data\n");
296
jas_stream_rewind(tmpstream);
298
if (box->ops->getdata) {
299
if ((*box->ops->getdata)(box, tmpstream)) {
300
jas_eprintf("cannot parse box data\n");
304
jas_stream_close(tmpstream);
307
if (jas_getdbglevel() >= 1) {
308
jp2_box_dump(box, stderr);
316
jp2_box_destroy(box);
319
jas_stream_close(tmpstream);
324
void jp2_box_dump(jp2_box_t *box, FILE *out)
326
jp2_boxinfo_t *boxinfo;
327
boxinfo = jp2_boxinfolookup(box->type);
330
fprintf(out, "JP2 box: ");
331
fprintf(out, "type=%c%s%c (0x%08x); length=%d\n", '"', boxinfo->name,
332
'"', (unsigned)box->type, (int)box->len);
333
if (box->ops->dumpdata) {
334
(*box->ops->dumpdata)(box, out);
338
static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in)
340
jp2_jp_t *jp = &box->data.jp;
341
if (jp2_getuint32(in, &jp->magic)) {
347
static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in)
349
jp2_ftyp_t *ftyp = &box->data.ftyp;
351
if (jp2_getuint32(in, &ftyp->majver) || jp2_getuint32(in, &ftyp->minver)) {
354
ftyp->numcompatcodes = (box->datalen - 8) / 4;
355
if (ftyp->numcompatcodes > JP2_FTYP_MAXCOMPATCODES) {
358
for (i = 0; i < ftyp->numcompatcodes; ++i) {
359
if (jp2_getuint32(in, &ftyp->compatcodes[i])) {
366
static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in)
368
jp2_ihdr_t *ihdr = &box->data.ihdr;
369
if (jp2_getuint32(in, &ihdr->height) || jp2_getuint32(in, &ihdr->width) ||
370
jp2_getuint16(in, &ihdr->numcmpts) || jp2_getuint8(in, &ihdr->bpc) ||
371
jp2_getuint8(in, &ihdr->comptype) || jp2_getuint8(in, &ihdr->csunk) ||
372
jp2_getuint8(in, &ihdr->ipr)) {
378
static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in)
380
jp2_bpcc_t *bpcc = &box->data.bpcc;
382
bpcc->numcmpts = box->datalen;
383
if (!(bpcc->bpcs = jas_alloc2(bpcc->numcmpts, sizeof(uint_fast8_t)))) {
386
for (i = 0; i < bpcc->numcmpts; ++i) {
387
if (jp2_getuint8(in, &bpcc->bpcs[i])) {
394
static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out)
396
jp2_colr_t *colr = &box->data.colr;
397
fprintf(out, "method=%d; pri=%d; approx=%d\n", (int)colr->method, (int)colr->pri, (int)colr->approx);
398
switch (colr->method) {
400
fprintf(out, "csid=%d\n", (int)colr->csid);
403
jas_memdump(out, colr->iccp, colr->iccplen);
408
static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in)
410
jp2_colr_t *colr = &box->data.colr;
415
if (jp2_getuint8(in, &colr->method) || jp2_getuint8(in, &colr->pri) ||
416
jp2_getuint8(in, &colr->approx)) {
419
switch (colr->method) {
421
if (jp2_getuint32(in, &colr->csid)) {
426
colr->iccplen = box->datalen - 3;
427
if (!(colr->iccp = jas_alloc2(colr->iccplen, sizeof(uint_fast8_t)))) {
430
if (jas_stream_read(in, colr->iccp, colr->iccplen) != colr->iccplen) {
438
static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out)
440
jp2_cdef_t *cdef = &box->data.cdef;
442
for (i = 0; i < cdef->numchans; ++i) {
443
fprintf(out, "channo=%d; type=%d; assoc=%d\n",
444
(int)cdef->ents[i].channo, (int)cdef->ents[i].type, (int)cdef->ents[i].assoc);
448
static void jp2_colr_destroy(jp2_box_t *box)
450
jp2_colr_t *colr = &box->data.colr;
452
jas_free(colr->iccp);
456
static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in)
458
jp2_cdef_t *cdef = &box->data.cdef;
459
jp2_cdefchan_t *chan;
461
if (jp2_getuint16(in, &cdef->numchans)) {
464
if (!(cdef->ents = jas_alloc2(cdef->numchans, sizeof(jp2_cdefchan_t)))) {
467
for (channo = 0; channo < cdef->numchans; ++channo) {
468
chan = &cdef->ents[channo];
469
if (jp2_getuint16(in, &chan->channo) || jp2_getuint16(in, &chan->type) ||
470
jp2_getuint16(in, &chan->assoc)) {
477
/******************************************************************************\
479
\******************************************************************************/
481
int jp2_box_put(jp2_box_t *box, jas_stream_t *out)
483
jas_stream_t *tmpstream;
489
dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
492
tmpstream = jas_stream_memopen(0, 0);
493
if (box->ops->putdata) {
494
if ((*box->ops->putdata)(box, tmpstream)) {
498
box->len = jas_stream_tell(tmpstream) + JP2_BOX_HDRLEN(false);
499
jas_stream_rewind(tmpstream);
501
//extlen = (box->len >= (((uint_fast64_t)1) << 32)) != 0;
502
if (jp2_putuint32(out, /*extlen ? 1 :*/ box->len)) {
505
if (jp2_putuint32(out, box->type)) {
509
if (jp2_putuint64(out, box->len)) {
515
if (jas_stream_copy(out, tmpstream, box->len - JP2_BOX_HDRLEN(false))) {
518
jas_stream_close(tmpstream);
527
jas_stream_close(tmpstream);
532
static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out)
534
jp2_jp_t *jp = &box->data.jp;
535
if (jp2_putuint32(out, jp->magic)) {
541
static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out)
543
jp2_ftyp_t *ftyp = &box->data.ftyp;
545
if (jp2_putuint32(out, ftyp->majver) || jp2_putuint32(out, ftyp->minver)) {
548
for (i = 0; i < ftyp->numcompatcodes; ++i) {
549
if (jp2_putuint32(out, ftyp->compatcodes[i])) {
556
static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out)
558
jp2_ihdr_t *ihdr = &box->data.ihdr;
559
if (jp2_putuint32(out, ihdr->height) || jp2_putuint32(out, ihdr->width) ||
560
jp2_putuint16(out, ihdr->numcmpts) || jp2_putuint8(out, ihdr->bpc) ||
561
jp2_putuint8(out, ihdr->comptype) || jp2_putuint8(out, ihdr->csunk) ||
562
jp2_putuint8(out, ihdr->ipr)) {
568
static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out)
570
jp2_bpcc_t *bpcc = &box->data.bpcc;
572
for (i = 0; i < bpcc->numcmpts; ++i) {
573
if (jp2_putuint8(out, bpcc->bpcs[i])) {
580
static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out)
582
jp2_colr_t *colr = &box->data.colr;
583
if (jp2_putuint8(out, colr->method) || jp2_putuint8(out, colr->pri) ||
584
jp2_putuint8(out, colr->approx)) {
587
switch (colr->method) {
589
if (jp2_putuint32(out, colr->csid)) {
594
if (jas_stream_write(out, colr->iccp,
595
JAS_CAST(int, colr->iccplen)) != JAS_CAST(int, colr->iccplen))
602
static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out)
604
jp2_cdef_t *cdef = &box->data.cdef;
608
if (jp2_putuint16(out, cdef->numchans)) {
612
for (i = 0; i < cdef->numchans; ++i) {
613
ent = &cdef->ents[i];
614
if (jp2_putuint16(out, ent->channo) ||
615
jp2_putuint16(out, ent->type) ||
616
jp2_putuint16(out, ent->assoc)) {
623
/******************************************************************************\
624
* Input operations for primitive types.
625
\******************************************************************************/
627
static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val)
630
if ((c = jas_stream_getc(in)) == EOF) {
639
static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val)
643
if ((c = jas_stream_getc(in)) == EOF) {
647
if ((c = jas_stream_getc(in)) == EOF) {
657
static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val)
661
if ((c = jas_stream_getc(in)) == EOF) {
665
if ((c = jas_stream_getc(in)) == EOF) {
669
if ((c = jas_stream_getc(in)) == EOF) {
673
if ((c = jas_stream_getc(in)) == EOF) {
683
static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val)
685
uint_fast64_t tmpval;
690
for (i = 0; i < 8; ++i) {
692
if ((c = jas_stream_getc(in)) == EOF) {
695
tmpval |= (c & 0xff);
702
/******************************************************************************\
703
* Output operations for primitive types.
704
\******************************************************************************/
706
static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val)
708
if (jas_stream_putc(out, val & 0xff) == EOF) {
714
static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val)
716
if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
717
jas_stream_putc(out, val & 0xff) == EOF) {
723
static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val)
725
if (jas_stream_putc(out, (val >> 24) & 0xff) == EOF ||
726
jas_stream_putc(out, (val >> 16) & 0xff) == EOF ||
727
jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
728
jas_stream_putc(out, val & 0xff) == EOF) {
734
static int jp2_putuint64(jas_stream_t *out, uint_fast64_t val)
736
if (jp2_putuint32(out, (val >> 32) & 0xffffffffUL) ||
737
jp2_putuint32(out, val & 0xffffffffUL)) {
743
/******************************************************************************\
744
* Miscellaneous code.
745
\******************************************************************************/
747
jp2_boxinfo_t *jp2_boxinfolookup(int type)
749
jp2_boxinfo_t *boxinfo;
750
for (boxinfo = jp2_boxinfos; boxinfo->name; ++boxinfo) {
751
if (boxinfo->type == type) {
755
return &jp2_boxinfo_unk;
762
static void jp2_cmap_destroy(jp2_box_t *box)
764
jp2_cmap_t *cmap = &box->data.cmap;
766
jas_free(cmap->ents);
770
static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in)
772
jp2_cmap_t *cmap = &box->data.cmap;
776
cmap->numchans = (box->datalen) / 4;
777
if (!(cmap->ents = jas_alloc2(cmap->numchans, sizeof(jp2_cmapent_t)))) {
780
for (i = 0; i < cmap->numchans; ++i) {
781
ent = &cmap->ents[i];
782
if (jp2_getuint16(in, &ent->cmptno) ||
783
jp2_getuint8(in, &ent->map) ||
784
jp2_getuint8(in, &ent->pcol)) {
792
static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out)
794
/* Eliminate compiler warning about unused variables. */
801
static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out)
803
jp2_cmap_t *cmap = &box->data.cmap;
806
fprintf(out, "numchans = %d\n", (int) cmap->numchans);
807
for (i = 0; i < cmap->numchans; ++i) {
808
ent = &cmap->ents[i];
809
fprintf(out, "cmptno=%d; map=%d; pcol=%d\n",
810
(int) ent->cmptno, (int) ent->map, (int) ent->pcol);
814
static void jp2_pclr_destroy(jp2_box_t *box)
816
jp2_pclr_t *pclr = &box->data.pclr;
818
jas_free(pclr->lutdata);
824
static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in)
826
jp2_pclr_t *pclr = &box->data.pclr;
834
if (jp2_getuint16(in, &pclr->numlutents) ||
835
jp2_getuint8(in, &pclr->numchans)) {
838
lutsize = pclr->numlutents * pclr->numchans;
839
if (!(pclr->lutdata = jas_alloc2(lutsize, sizeof(int_fast32_t)))) {
842
if (!(pclr->bpc = jas_alloc2(pclr->numchans, sizeof(uint_fast8_t)))) {
845
for (i = 0; i < pclr->numchans; ++i) {
846
if (jp2_getuint8(in, &pclr->bpc[i])) {
850
for (i = 0; i < pclr->numlutents; ++i) {
851
for (j = 0; j < pclr->numchans; ++j) {
852
if (jp2_getint(in, (pclr->bpc[j] & 0x80) != 0,
853
(pclr->bpc[j] & 0x7f) + 1, &x)) {
856
pclr->lutdata[i * pclr->numchans + j] = x;
862
static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out)
865
jp2_pclr_t *pclr = &box->data.pclr;
867
/* Eliminate warning about unused variable. */
873
static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out)
875
jp2_pclr_t *pclr = &box->data.pclr;
878
fprintf(out, "numents=%d; numchans=%d\n", (int) pclr->numlutents,
879
(int) pclr->numchans);
880
for (i = 0; i < pclr->numlutents; ++i) {
881
for (j = 0; j < pclr->numchans; ++j) {
882
fprintf(out, "LUT[%d][%d]=%d\n", i, j, (int)pclr->lutdata[i * pclr->numchans + j]);
887
static void jp2_uuid_destroy(jp2_box_t *box)
889
jp2_uuid_t *uuid = &box->data.uuid;
892
jas_free(uuid->data);
897
static int jp2_uuid_getdata(jp2_box_t *box, jas_stream_t *in)
899
jp2_uuid_t *uuid = &box->data.uuid;
902
for (i = 0; i < 16; i++)
904
if (jp2_getuint8(in, &uuid->uuid[i]))
908
uuid->datalen = box->datalen - 16;
909
uuid->data = jas_malloc(uuid->datalen * sizeof(uint_fast8_t));
910
for (i = 0; i < uuid->datalen; i++)
912
if (jp2_getuint8(in, &uuid->data[i]))
918
static int jp2_uuid_putdata(jp2_box_t *box, jas_stream_t *out)
920
jp2_uuid_t *uuid = &box->data.uuid;
923
for (i = 0; i < 16; i++)
925
if (jp2_putuint8(out, uuid->uuid[i]))
929
for (i = 0; i < uuid->datalen; i++)
931
if (jp2_putuint8(out, uuid->data[i]))
937
static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val)
947
for (i = 0; i < m; ++i) {
948
if ((c = jas_stream_getc(in)) == EOF) {
956
sb = v & (1 << (8 * m - 1));
957
*val = ((~v) + 1) & ONES(8 * m);
968
jp2_cdefchan_t *jp2_cdef_lookup(jp2_cdef_t *cdef, int channo)
971
jp2_cdefchan_t *cdefent;
972
for (i = 0; i < cdef->numchans; ++i) {
973
cdefent = &cdef->ents[i];
974
if (cdefent->channo == JAS_CAST(unsigned int, channo)) {