Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / libjasper / jp2_cod.c
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 License Version 2.0
11  * 
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
15  * 
16  * All rights reserved.
17  * 
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:
25  * 
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.
29  * 
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
32  * written permission.
33  * 
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.
60  * 
61  * __END_OF_JASPER_LICENSE__
62  */
63
64 /*
65  * JP2 Library
66  *
67  * $Id: jp2_cod.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
68  */
69
70 /******************************************************************************\
71 * Includes.
72 \******************************************************************************/
73
74 #include <assert.h>
75 #include <stdlib.h>
76
77 #include "jasper/jas_stream.h"
78 #include "jasper/jas_malloc.h"
79 #include "jasper/jas_debug.h"
80
81 #include "jp2_cod.h"
82
83 /******************************************************************************\
84 * Function prototypes.
85 \******************************************************************************/
86
87 #define ONES(n) ((1 << (n)) - 1)
88
89 jp2_boxinfo_t *jp2_boxinfolookup(int type);
90
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);
99
100 static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val);
101
102 jp2_box_t *jp2_box_get(jas_stream_t *in);
103 void jp2_box_dump(jp2_box_t *box, FILE *out);
104
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);
130
131 /******************************************************************************\
132 * Local data.
133 \******************************************************************************/
134
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,
141           {0, 0, 0, 0, 0}},
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,
155           {0, 0, 0, 0, 0}},
156         {JP2_BOX_RESC, "RESC", 0,
157           {0, 0, 0, 0, 0}},
158         {JP2_BOX_RESD, "RESD", 0,
159           {0, 0, 0, 0, 0}},
160         {JP2_BOX_JP2C, "JP2C", JP2_BOX_NODATA,
161           {0, 0, 0, 0, 0}},
162         {JP2_BOX_JP2I, "JP2I", 0,
163           {0, 0, 0, 0, 0}},
164         {JP2_BOX_XML, "XML", 0,
165           {0, 0, 0, 0, 0}},
166         {JP2_BOX_UUID, "UUID", 0,
167           {0, 0, 0, 0, 0}},
168         {JP2_BOX_UINF, "UINF", JP2_BOX_SUPER,
169           {0, 0, 0, 0, 0}},
170         {JP2_BOX_ULST, "ULST", 0,
171           {0, 0, 0, 0, 0}},
172         {JP2_BOX_URL, "URL", 0,
173           {0, 0, 0, 0, 0}},
174         {0, 0, 0, {0, 0, 0, 0, 0}},
175 };
176
177 jp2_boxinfo_t jp2_boxinfo_unk = {
178         0, "Unknown", 0, {0, 0, 0, 0, 0}
179 };
180
181 /******************************************************************************\
182 * Box constructor.
183 \******************************************************************************/
184
185 jp2_box_t *jp2_box_create(int type)
186 {
187         jp2_box_t *box;
188         jp2_boxinfo_t *boxinfo;
189
190         if (!(box = jas_malloc(sizeof(jp2_box_t)))) {
191                 return 0;
192         }
193         memset(box, 0, sizeof(jp2_box_t));
194         box->type = type;
195         box->len = 0;
196         if (!(boxinfo = jp2_boxinfolookup(type))) {
197                 return 0;
198         }
199         box->info = boxinfo;
200         box->ops = &boxinfo->ops;
201         return box;
202 }
203
204 /******************************************************************************\
205 * Box destructor.
206 \******************************************************************************/
207
208 void jp2_box_destroy(jp2_box_t *box)
209 {
210         if (box->ops->destroy) {
211                 (*box->ops->destroy)(box);
212         }
213         jas_free(box);
214 }
215
216 static void jp2_bpcc_destroy(jp2_box_t *box)
217 {
218         jp2_bpcc_t *bpcc = &box->data.bpcc;
219         if (bpcc->bpcs) {
220                 jas_free(bpcc->bpcs);
221                 bpcc->bpcs = 0;
222         }
223 }
224
225 static void jp2_cdef_destroy(jp2_box_t *box)
226 {
227         jp2_cdef_t *cdef = &box->data.cdef;
228         if (cdef->ents) {
229                 jas_free(cdef->ents);
230                 cdef->ents = 0;
231         }
232 }
233
234 /******************************************************************************\
235 * Box input.
236 \******************************************************************************/
237
238 jp2_box_t *jp2_box_get(jas_stream_t *in)
239 {
240         jp2_box_t *box;
241         jp2_boxinfo_t *boxinfo;
242         jas_stream_t *tmpstream;
243         uint_fast32_t len;
244         uint_fast64_t extlen;
245         bool dataflag;
246
247         box = 0;
248         tmpstream = 0;
249
250         if (!(box = jas_malloc(sizeof(jp2_box_t)))) {
251                 goto error;
252         }
253         box->ops = &jp2_boxinfo_unk.ops;
254         if (jp2_getuint32(in, &len) || jp2_getuint32(in, &box->type)) {
255                 goto error;
256         }
257         boxinfo = jp2_boxinfolookup(box->type);
258         box->info = boxinfo;
259         box->ops = &boxinfo->ops;
260         box->len = len;
261         if (box->len == 1) {
262                 if (jp2_getuint64(in, &extlen)) {
263                         goto error;
264                 }
265                 if (extlen > 0xffffffffUL) {
266                         jas_eprintf("warning: cannot handle large 64-bit box length\n");
267                         extlen = 0xffffffffUL;
268                 }
269                 box->len = extlen;
270                 box->datalen = extlen - JP2_BOX_HDRLEN(true);
271         } else {
272                 box->datalen = box->len - JP2_BOX_HDRLEN(false);
273         }
274         if (box->len != 0 && box->len < 8) {
275                 goto error;
276         }
277
278         dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
279
280         if (dataflag) {
281                 if (!(tmpstream = jas_stream_memopen(0, 0))) {
282                         goto error;
283                 }
284                 if (jas_stream_copy(tmpstream, in, box->datalen)) {
285                         jas_eprintf("cannot copy box data\n");
286                         goto error;
287                 }
288                 jas_stream_rewind(tmpstream);
289
290                 if (box->ops->getdata) {
291                         if ((*box->ops->getdata)(box, tmpstream)) {
292                                 jas_eprintf("cannot parse box data\n");
293                                 goto error;
294                         }
295                 }
296                 jas_stream_close(tmpstream);
297         }
298
299         if (jas_getdbglevel() >= 1) {
300                 jp2_box_dump(box, stderr);
301         }
302
303         return box;
304         abort();
305
306 error:
307         if (box) {
308                 jp2_box_destroy(box);
309         }
310         if (tmpstream) {
311                 jas_stream_close(tmpstream);
312         }
313         return 0;
314 }
315
316 void jp2_box_dump(jp2_box_t *box, FILE *out)
317 {
318         jp2_boxinfo_t *boxinfo;
319         boxinfo = jp2_boxinfolookup(box->type);
320         assert(boxinfo);
321
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);
327         }
328 }
329
330 static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in)
331 {
332         jp2_jp_t *jp = &box->data.jp;
333         if (jp2_getuint32(in, &jp->magic)) {
334                 return -1;
335         }
336         return 0;
337 }
338
339 static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in)
340 {
341         jp2_ftyp_t *ftyp = &box->data.ftyp;
342         unsigned int i;
343         if (jp2_getuint32(in, &ftyp->majver) || jp2_getuint32(in, &ftyp->minver)) {
344                 return -1;
345         }
346         ftyp->numcompatcodes = (box->datalen - 8) / 4;
347         if (ftyp->numcompatcodes > JP2_FTYP_MAXCOMPATCODES) {
348                 return -1;
349         }
350         for (i = 0; i < ftyp->numcompatcodes; ++i) {
351                 if (jp2_getuint32(in, &ftyp->compatcodes[i])) {
352                         return -1;
353                 }
354         }
355         return 0;
356 }
357
358 static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in)
359 {
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)) {
365                 return -1;
366         }
367         return 0;
368 }
369
370 static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in)
371 {
372         jp2_bpcc_t *bpcc = &box->data.bpcc;
373         unsigned int i;
374         bpcc->numcmpts = box->datalen;
375         if (!(bpcc->bpcs = jas_malloc(bpcc->numcmpts * sizeof(uint_fast8_t)))) {
376                 return -1;
377         }
378         for (i = 0; i < bpcc->numcmpts; ++i) {
379                 if (jp2_getuint8(in, &bpcc->bpcs[i])) {
380                         return -1;
381                 }
382         }
383         return 0;
384 }
385
386 static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out)
387 {
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) {
391         case JP2_COLR_ENUM:
392                 fprintf(out, "csid=%d\n", (int)colr->csid);
393                 break;
394         case JP2_COLR_ICC:
395                 jas_memdump(out, colr->iccp, colr->iccplen);
396                 break;
397         }
398 }
399
400 static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in)
401 {
402         jp2_colr_t *colr = &box->data.colr;
403         colr->csid = 0;
404         colr->iccp = 0;
405         colr->iccplen = 0;
406
407         if (jp2_getuint8(in, &colr->method) || jp2_getuint8(in, &colr->pri) ||
408           jp2_getuint8(in, &colr->approx)) {
409                 return -1;
410         }
411         switch (colr->method) {
412         case JP2_COLR_ENUM:
413                 if (jp2_getuint32(in, &colr->csid)) {
414                         return -1;
415                 }
416                 break;
417         case JP2_COLR_ICC:
418                 colr->iccplen = box->datalen - 3;
419                 if (!(colr->iccp = jas_malloc(colr->iccplen * sizeof(uint_fast8_t)))) {
420                         return -1;
421                 }
422                 if (jas_stream_read(in, colr->iccp, colr->iccplen) != colr->iccplen) {
423                         return -1;
424                 }
425                 break;
426         }
427         return 0;
428 }
429
430 static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out)
431 {
432         jp2_cdef_t *cdef = &box->data.cdef;
433         unsigned int i;
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);
437         }
438 }
439
440 static void jp2_colr_destroy(jp2_box_t *box)
441 {
442         jp2_colr_t *colr = &box->data.colr;
443         if (colr->iccp) {
444                 jas_free(colr->iccp);
445         }
446 }
447
448 static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in)
449 {
450         jp2_cdef_t *cdef = &box->data.cdef;
451         jp2_cdefchan_t *chan;
452         unsigned int channo;
453         if (jp2_getuint16(in, &cdef->numchans)) {
454                 return -1;
455         }
456         if (!(cdef->ents = jas_malloc(cdef->numchans * sizeof(jp2_cdefchan_t)))) {
457                 return -1;
458         }
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)) {
463                         return -1;
464                 }
465         }
466         return 0;
467 }
468
469 /******************************************************************************\
470 * Box output.
471 \******************************************************************************/
472
473 int jp2_box_put(jp2_box_t *box, jas_stream_t *out)
474 {
475         jas_stream_t *tmpstream;
476         bool extlen;
477         bool dataflag;
478
479         tmpstream = 0;
480
481         dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
482
483         if (dataflag) {
484                 tmpstream = jas_stream_memopen(0, 0);
485                 if (box->ops->putdata) {
486                         if ((*box->ops->putdata)(box, tmpstream)) {
487                                 goto error;
488                         }
489                 }
490                 box->len = jas_stream_tell(tmpstream) + JP2_BOX_HDRLEN(false);
491                 jas_stream_rewind(tmpstream);
492         }
493         extlen = (box->len >= (((uint_fast64_t)1) << 32)) != 0;
494         if (jp2_putuint32(out, extlen ? 1 : box->len)) {
495                 goto error;
496         }
497         if (jp2_putuint32(out, box->type)) {
498                 goto error;
499         }
500         if (extlen) {
501                 if (jp2_putuint64(out, box->len)) {
502                         goto error;
503                 }
504         }
505
506         if (dataflag) {
507                 if (jas_stream_copy(out, tmpstream, box->len - JP2_BOX_HDRLEN(false))) {
508                         goto error;
509                 }
510                 jas_stream_close(tmpstream);
511         }
512
513         return 0;
514         abort();
515
516 error:
517
518         if (tmpstream) {
519                 jas_stream_close(tmpstream);
520         }
521         return -1;
522 }
523
524 static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out)
525 {
526         jp2_jp_t *jp = &box->data.jp;
527         if (jp2_putuint32(out, jp->magic)) {
528                 return -1;
529         }
530         return 0;
531 }
532
533 static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out)
534 {
535         jp2_ftyp_t *ftyp = &box->data.ftyp;
536         unsigned int i;
537         if (jp2_putuint32(out, ftyp->majver) || jp2_putuint32(out, ftyp->minver)) {
538                 return -1;
539         }
540         for (i = 0; i < ftyp->numcompatcodes; ++i) {
541                 if (jp2_putuint32(out, ftyp->compatcodes[i])) {
542                         return -1;
543                 }
544         }
545         return 0;
546 }
547
548 static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out)
549 {
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)) {
555                 return -1;
556         }
557         return 0;
558 }
559
560 static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out)
561 {
562         jp2_bpcc_t *bpcc = &box->data.bpcc;
563         unsigned int i;
564         for (i = 0; i < bpcc->numcmpts; ++i) {
565                 if (jp2_putuint8(out, bpcc->bpcs[i])) {
566                         return -1;
567                 }
568         }
569         return 0;
570 }
571
572 static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out)
573 {
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)) {
577                 return -1;
578         }
579         switch (colr->method) {
580         case JP2_COLR_ENUM:
581                 if (jp2_putuint32(out, colr->csid)) {
582                         return -1;
583                 }
584                 break;
585         case JP2_COLR_ICC:
586                 if (jas_stream_write(out, colr->iccp,
587                   JAS_CAST(int, colr->iccplen)) != JAS_CAST(int, colr->iccplen))
588                         return -1;
589                 break;
590         }
591         return 0;
592 }
593
594 static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out)
595 {
596         jp2_cdef_t *cdef = &box->data.cdef;
597         unsigned int i;
598         jp2_cdefchan_t *ent;
599
600         if (jp2_putuint16(out, cdef->numchans)) {
601                 return -1;
602         }
603
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)) {
609                         return -1;
610                 }
611         }
612         return 0;
613 }
614
615 /******************************************************************************\
616 * Input operations for primitive types.
617 \******************************************************************************/
618
619 static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val)
620 {
621         int c;
622         if ((c = jas_stream_getc(in)) == EOF) {
623                 return -1;
624         }
625         if (val) {
626                 *val = c;
627         }
628         return 0;
629 }
630
631 static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val)
632 {
633         uint_fast16_t v;
634         int c;
635         if ((c = jas_stream_getc(in)) == EOF) {
636                 return -1;
637         }
638         v = c;
639         if ((c = jas_stream_getc(in)) == EOF) {
640                 return -1;
641         }
642         v = (v << 8) | c;
643         if (val) {
644                 *val = v;
645         }
646         return 0;
647 }
648
649 static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val)
650 {
651         uint_fast32_t v;
652         int c;
653         if ((c = jas_stream_getc(in)) == EOF) {
654                 return -1;
655         }
656         v = c;
657         if ((c = jas_stream_getc(in)) == EOF) {
658                 return -1;
659         }
660         v = (v << 8) | c;
661         if ((c = jas_stream_getc(in)) == EOF) {
662                 return -1;
663         }
664         v = (v << 8) | c;
665         if ((c = jas_stream_getc(in)) == EOF) {
666                 return -1;
667         }
668         v = (v << 8) | c;
669         if (val) {
670                 *val = v;
671         }
672         return 0;
673 }
674
675 static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val)
676 {
677         uint_fast64_t tmpval;
678         int i;
679         int c;
680
681         tmpval = 0;
682         for (i = 0; i < 8; ++i) {
683                 tmpval <<= 8;
684                 if ((c = jas_stream_getc(in)) == EOF) {
685                         return -1;
686                 }
687                 tmpval |= (c & 0xff);
688         }
689         *val = tmpval;
690
691         return 0;
692 }
693
694 /******************************************************************************\
695 * Output operations for primitive types.
696 \******************************************************************************/
697
698 static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val)
699 {
700         if (jas_stream_putc(out, val & 0xff) == EOF) {
701                 return -1;
702         }
703         return 0;
704 }
705
706 static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val)
707 {
708         if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
709           jas_stream_putc(out, val & 0xff) == EOF) {
710                 return -1;
711         }
712         return 0;
713 }
714
715 static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val)
716 {
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) {
721                 return -1;
722         }
723         return 0;
724 }
725
726 static int jp2_putuint64(jas_stream_t *out, uint_fast64_t val)
727 {
728         if (jp2_putuint32(out, (val >> 32) & 0xffffffffUL) ||
729           jp2_putuint32(out, val & 0xffffffffUL)) {
730                 return -1;
731         }
732         return 0;
733 }
734
735 /******************************************************************************\
736 * Miscellaneous code.
737 \******************************************************************************/
738
739 jp2_boxinfo_t *jp2_boxinfolookup(int type)
740 {
741         jp2_boxinfo_t *boxinfo;
742         for (boxinfo = jp2_boxinfos; boxinfo->name; ++boxinfo) {
743                 if (boxinfo->type == type) {
744                         return boxinfo;
745                 }
746         }
747         return &jp2_boxinfo_unk;
748 }
749
750
751
752
753
754 static void jp2_cmap_destroy(jp2_box_t *box)
755 {
756         jp2_cmap_t *cmap = &box->data.cmap;
757         if (cmap->ents) {
758                 jas_free(cmap->ents);
759         }
760 }
761
762 static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in)
763 {
764         jp2_cmap_t *cmap = &box->data.cmap;
765         jp2_cmapent_t *ent;
766         unsigned int i;
767
768         cmap->numchans = (box->datalen) / 4;
769         if (!(cmap->ents = jas_malloc(cmap->numchans * sizeof(jp2_cmapent_t)))) {
770                 return -1;
771         }
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)) {
777                         return -1;
778                 }
779         }
780         
781         return 0;
782 }
783
784 static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out)
785 {
786         /* Eliminate compiler warning about unused variables. */
787         box = 0;
788         out = 0;
789
790         return -1;
791 }
792
793 static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out)
794 {
795         jp2_cmap_t *cmap = &box->data.cmap;
796         unsigned int i;
797         jp2_cmapent_t *ent;
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);
803         }
804 }
805
806 static void jp2_pclr_destroy(jp2_box_t *box)
807 {
808         jp2_pclr_t *pclr = &box->data.pclr;
809         if (pclr->lutdata) {
810                 jas_free(pclr->lutdata);
811         }
812         if (pclr->bpc)
813                 jas_free(pclr->bpc);
814 }
815
816 static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in)
817 {
818         jp2_pclr_t *pclr = &box->data.pclr;
819         int lutsize;
820         unsigned int i;
821         unsigned int j;
822         int_fast32_t x;
823
824         pclr->lutdata = 0;
825
826         if (jp2_getuint16(in, &pclr->numlutents) ||
827           jp2_getuint8(in, &pclr->numchans)) {
828                 return -1;
829         }
830         lutsize = pclr->numlutents * pclr->numchans;
831         if (!(pclr->lutdata = jas_malloc(lutsize * sizeof(int_fast32_t)))) {
832                 return -1;
833         }
834         if (!(pclr->bpc = jas_malloc(pclr->numchans * sizeof(uint_fast8_t)))) {
835                 return -1;
836         }
837         for (i = 0; i < pclr->numchans; ++i) {
838                 if (jp2_getuint8(in, &pclr->bpc[i])) {
839                         return -1;
840                 }
841         }
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)) {
846                                 return -1;
847                         }
848                         pclr->lutdata[i * pclr->numchans + j] = x;
849                 }
850         }
851         return 0;
852 }
853
854 static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out)
855 {
856 #if 0
857         jp2_pclr_t *pclr = &box->data.pclr;
858 #endif
859 /* Eliminate warning about unused variable. */
860 box = 0;
861 out = 0;
862         return -1;
863 }
864
865 static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out)
866 {
867         jp2_pclr_t *pclr = &box->data.pclr;
868         unsigned int i;
869         int j;
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]);
875                 }
876         }
877 }
878
879 static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val)
880 {
881         int c;
882         int i;
883         uint_fast32_t v;
884         int m;
885
886         m = (n + 7) / 8;
887
888         v = 0;
889         for (i = 0; i < m; ++i) {
890                 if ((c = jas_stream_getc(in)) == EOF) {
891                         return -1;
892                 }
893                 v = (v << 8) | c;
894         }
895         v &= ONES(n);
896         if (s) {
897                 int sb;
898                 sb = v & (1 << (8 * m - 1));
899                 *val = ((~v) + 1) & ONES(8 * m);
900                 if (sb) {
901                         *val = -*val;
902                 }
903         } else {
904                 *val = v;
905         }
906
907         return 0;
908 }
909
910 jp2_cdefchan_t *jp2_cdef_lookup(jp2_cdef_t *cdef, int channo)
911 {
912         unsigned int i;
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)) {
917                         return cdefent;
918                 }
919         }
920         return 0;
921 }