jp2_cod.c 26.6 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
 *   British Columbia.
 * Copyright (c) 2001-2002 Michael David Adams.
 * All rights reserved.
 */

8 9 10 11 12
/*
 * Modified by Andrey Kiselev <dron@remotesensing.org> to handle UUID
 * box properly.
 */

13
/* __START_OF_JASPER_LICENSE__
14
 *
15
 * JasPer License Version 2.0
16
 *
17 18 19
 * Copyright (c) 2001-2006 Michael David Adams
 * Copyright (c) 1999-2000 Image Power, Inc.
 * Copyright (c) 1999-2000 The University of British Columbia
20
 *
21
 * All rights reserved.
22
 *
23 24 25 26 27 28 29
 * Permission is hereby granted, free of charge, to any person (the
 * "User") obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do so, subject to the
 * following conditions:
30
 *
31 32 33
 * 1.  The above copyright notices and this permission notice (which
 * includes the disclaimer below) shall be included in all copies or
 * substantial portions of the Software.
34
 *
35 36 37
 * 2.  The name of a copyright holder shall not be used to endorse or
 * promote products derived from the Software without specific prior
 * written permission.
38
 *
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
 * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
 * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
 * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
 * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
65
 *
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
 * __END_OF_JASPER_LICENSE__
 */

/*
 * JP2 Library
 *
 * $Id: jp2_cod.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
 */

/******************************************************************************\
* Includes.
\******************************************************************************/

#include <assert.h>
#include <stdlib.h>

#include "jasper/jas_stream.h"
#include "jasper/jas_malloc.h"
#include "jasper/jas_debug.h"

#include "jp2_cod.h"

/******************************************************************************\
* Function prototypes.
\******************************************************************************/

#define	ONES(n)	((1 << (n)) - 1)

jp2_boxinfo_t *jp2_boxinfolookup(int type);

static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val);
static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val);
static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val);
static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val);
static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val);
static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val);
static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val);
static int jp2_putuint64(jas_stream_t *out, uint_fast64_t val);

static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val);

jp2_box_t *jp2_box_get(jas_stream_t *in);
void jp2_box_dump(jp2_box_t *box, FILE *out);

static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in);
static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out);
static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in);
static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out);
static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in);
static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out);
static void jp2_bpcc_destroy(jp2_box_t *box);
static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in);
static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out);
static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in);
static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out);
static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out);
static void jp2_colr_destroy(jp2_box_t *box);
static void jp2_cdef_destroy(jp2_box_t *box);
static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in);
static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out);
static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out);
static void jp2_cmap_destroy(jp2_box_t *box);
static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in);
static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out);
static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out);
static void jp2_pclr_destroy(jp2_box_t *box);
static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in);
static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out);
static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out);
135 136 137
static void jp2_uuid_destroy(jp2_box_t *box);
static int jp2_uuid_getdata(jp2_box_t *box, jas_stream_t *in);
static int jp2_uuid_putdata(jp2_box_t *box, jas_stream_t *out);
138 139 140 141 142 143

/******************************************************************************\
* Local data.
\******************************************************************************/

jp2_boxinfo_t jp2_boxinfos[] = {
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    {JP2_BOX_JP, "JP", 0,
      {0, 0, jp2_jp_getdata, jp2_jp_putdata, 0}},
    {JP2_BOX_FTYP, "FTYP", 0,
      {0, 0, jp2_ftyp_getdata, jp2_ftyp_putdata, 0}},
    {JP2_BOX_JP2H, "JP2H", JP2_BOX_SUPER,
      {0, 0, 0, 0, 0}},
    {JP2_BOX_IHDR, "IHDR", 0,
      {0, 0, jp2_ihdr_getdata, jp2_ihdr_putdata, 0}},
    {JP2_BOX_BPCC, "BPCC", 0,
      {0, jp2_bpcc_destroy, jp2_bpcc_getdata, jp2_bpcc_putdata, 0}},
    {JP2_BOX_COLR, "COLR", 0,
      {0, jp2_colr_destroy, jp2_colr_getdata, jp2_colr_putdata, jp2_colr_dumpdata}},
    {JP2_BOX_PCLR, "PCLR", 0,
      {0, jp2_pclr_destroy, jp2_pclr_getdata, jp2_pclr_putdata, jp2_pclr_dumpdata}},
    {JP2_BOX_CMAP, "CMAP", 0,
      {0, jp2_cmap_destroy, jp2_cmap_getdata, jp2_cmap_putdata, jp2_cmap_dumpdata}},
    {JP2_BOX_CDEF, "CDEF", 0,
      {0, jp2_cdef_destroy, jp2_cdef_getdata, jp2_cdef_putdata, jp2_cdef_dumpdata}},
    {JP2_BOX_RES, "RES", JP2_BOX_SUPER,
      {0, 0, 0, 0, 0}},
    {JP2_BOX_RESC, "RESC", 0,
      {0, 0, 0, 0, 0}},
    {JP2_BOX_RESD, "RESD", 0,
      {0, 0, 0, 0, 0}},
    {JP2_BOX_JP2C, "JP2C", JP2_BOX_NODATA,
      {0, 0, 0, 0, 0}},
    {JP2_BOX_JP2I, "JP2I", 0,
      {0, 0, 0, 0, 0}},
    {JP2_BOX_XML, "XML", 0,
      {0, 0, 0, 0, 0}},
    {JP2_BOX_UUID, "UUID", 0,
      {0, jp2_uuid_destroy, jp2_uuid_getdata, jp2_uuid_putdata, 0}},
    {JP2_BOX_UINF, "UINF", JP2_BOX_SUPER,
      {0, 0, 0, 0, 0}},
    {JP2_BOX_ULST, "ULST", 0,
      {0, 0, 0, 0, 0}},
    {JP2_BOX_URL, "URL", 0,
      {0, 0, 0, 0, 0}},
    {0, 0, 0, {0, 0, 0, 0, 0}},
183 184 185
};

jp2_boxinfo_t jp2_boxinfo_unk = {
186
    0, "Unknown", 0, {0, 0, 0, 0, 0}
187 188 189 190 191 192 193 194
};

/******************************************************************************\
* Box constructor.
\******************************************************************************/

jp2_box_t *jp2_box_create(int type)
{
195 196
    jp2_box_t *box;
    jp2_boxinfo_t *boxinfo;
197

198 199 200 201 202 203 204 205 206 207 208 209
    if (!(box = jas_malloc(sizeof(jp2_box_t)))) {
        return 0;
    }
    memset(box, 0, sizeof(jp2_box_t));
    box->type = type;
    box->len = 0;
    if (!(boxinfo = jp2_boxinfolookup(type))) {
        return 0;
    }
    box->info = boxinfo;
    box->ops = &boxinfo->ops;
    return box;
210 211 212 213 214 215 216 217
}

/******************************************************************************\
* Box destructor.
\******************************************************************************/

void jp2_box_destroy(jp2_box_t *box)
{
218 219 220 221
    if (box->ops->destroy) {
        (*box->ops->destroy)(box);
    }
    jas_free(box);
222 223 224 225
}

static void jp2_bpcc_destroy(jp2_box_t *box)
{
226 227 228 229 230
    jp2_bpcc_t *bpcc = &box->data.bpcc;
    if (bpcc->bpcs) {
        jas_free(bpcc->bpcs);
        bpcc->bpcs = 0;
    }
231 232 233 234
}

static void jp2_cdef_destroy(jp2_box_t *box)
{
235 236 237 238 239
    jp2_cdef_t *cdef = &box->data.cdef;
    if (cdef->ents) {
        jas_free(cdef->ents);
        cdef->ents = 0;
    }
240 241 242 243 244 245 246 247
}

/******************************************************************************\
* Box input.
\******************************************************************************/

jp2_box_t *jp2_box_get(jas_stream_t *in)
{
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    jp2_box_t *box;
    jp2_boxinfo_t *boxinfo;
    jas_stream_t *tmpstream;
    uint_fast32_t len;
    uint_fast64_t extlen;
    bool dataflag;

    box = 0;
    tmpstream = 0;

    if (!(box = jas_malloc(sizeof(jp2_box_t)))) {
        goto error;
    }
    box->ops = &jp2_boxinfo_unk.ops;
    if (jp2_getuint32(in, &len) || jp2_getuint32(in, &box->type)) {
        goto error;
    }
    boxinfo = jp2_boxinfolookup(box->type);
    box->info = boxinfo;
    box->ops = &boxinfo->ops;
    box->len = len;
    if (box->len == 1) {
        if (jp2_getuint64(in, &extlen)) {
            goto error;
        }
        if (extlen > 0xffffffffUL) {
            jas_eprintf("warning: cannot handle large 64-bit box length\n");
            extlen = 0xffffffffUL;
        }
        box->len = extlen;
        box->datalen = extlen - JP2_BOX_HDRLEN(true);
    } else {
        box->datalen = box->len - JP2_BOX_HDRLEN(false);
    }
    if (box->len != 0 && box->len < 8) {
        goto error;
    }

    dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));

    if (dataflag) {
        if (!(tmpstream = jas_stream_memopen(0, 0))) {
            goto error;
        }
        if (jas_stream_copy(tmpstream, in, box->datalen)) {
            jas_eprintf("cannot copy box data\n");
            goto error;
        }
        jas_stream_rewind(tmpstream);

        if (box->ops->getdata) {
            if ((*box->ops->getdata)(box, tmpstream)) {
                jas_eprintf("cannot parse box data\n");
                goto error;
            }
        }
        jas_stream_close(tmpstream);
    }

    if (jas_getdbglevel() >= 1) {
        jp2_box_dump(box, stderr);
    }

    return box;
    abort();
313 314

error:
315 316 317 318 319 320 321
    if (box) {
        jp2_box_destroy(box);
    }
    if (tmpstream) {
        jas_stream_close(tmpstream);
    }
    return 0;
322 323 324 325
}

void jp2_box_dump(jp2_box_t *box, FILE *out)
{
326 327 328
    jp2_boxinfo_t *boxinfo;
    boxinfo = jp2_boxinfolookup(box->type);
    assert(boxinfo);
329

330 331 332 333 334 335
    fprintf(out, "JP2 box: ");
    fprintf(out, "type=%c%s%c (0x%08x); length=%d\n", '"', boxinfo->name,
      '"', (unsigned)box->type, (int)box->len);
    if (box->ops->dumpdata) {
        (*box->ops->dumpdata)(box, out);
    }
336 337 338 339
}

static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in)
{
340 341 342 343 344
    jp2_jp_t *jp = &box->data.jp;
    if (jp2_getuint32(in, &jp->magic)) {
        return -1;
    }
    return 0;
345 346 347 348
}

static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in)
{
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    jp2_ftyp_t *ftyp = &box->data.ftyp;
    unsigned int i;
    if (jp2_getuint32(in, &ftyp->majver) || jp2_getuint32(in, &ftyp->minver)) {
        return -1;
    }
    ftyp->numcompatcodes = (box->datalen - 8) / 4;
    if (ftyp->numcompatcodes > JP2_FTYP_MAXCOMPATCODES) {
        return -1;
    }
    for (i = 0; i < ftyp->numcompatcodes; ++i) {
        if (jp2_getuint32(in, &ftyp->compatcodes[i])) {
            return -1;
        }
    }
    return 0;
364 365 366 367
}

static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in)
{
368 369 370 371 372 373 374 375
    jp2_ihdr_t *ihdr = &box->data.ihdr;
    if (jp2_getuint32(in, &ihdr->height) || jp2_getuint32(in, &ihdr->width) ||
      jp2_getuint16(in, &ihdr->numcmpts) || jp2_getuint8(in, &ihdr->bpc) ||
      jp2_getuint8(in, &ihdr->comptype) || jp2_getuint8(in, &ihdr->csunk) ||
      jp2_getuint8(in, &ihdr->ipr)) {
        return -1;
    }
    return 0;
376 377 378 379
}

static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in)
{
380 381 382 383 384 385 386 387 388 389 390 391
    jp2_bpcc_t *bpcc = &box->data.bpcc;
    unsigned int i;
    bpcc->numcmpts = box->datalen;
    if (!(bpcc->bpcs = jas_alloc2(bpcc->numcmpts, sizeof(uint_fast8_t)))) {
        return -1;
    }
    for (i = 0; i < bpcc->numcmpts; ++i) {
        if (jp2_getuint8(in, &bpcc->bpcs[i])) {
            return -1;
        }
    }
    return 0;
392 393 394 395
}

static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out)
{
396 397 398 399 400 401 402 403 404 405
    jp2_colr_t *colr = &box->data.colr;
    fprintf(out, "method=%d; pri=%d; approx=%d\n", (int)colr->method, (int)colr->pri, (int)colr->approx);
    switch (colr->method) {
    case JP2_COLR_ENUM:
        fprintf(out, "csid=%d\n", (int)colr->csid);
        break;
    case JP2_COLR_ICC:
        jas_memdump(out, colr->iccp, colr->iccplen);
        break;
    }
406 407 408 409
}

static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in)
{
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    jp2_colr_t *colr = &box->data.colr;
    colr->csid = 0;
    colr->iccp = 0;
    colr->iccplen = 0;

    if (jp2_getuint8(in, &colr->method) || jp2_getuint8(in, &colr->pri) ||
      jp2_getuint8(in, &colr->approx)) {
        return -1;
    }
    switch (colr->method) {
    case JP2_COLR_ENUM:
        if (jp2_getuint32(in, &colr->csid)) {
            return -1;
        }
        break;
    case JP2_COLR_ICC:
        colr->iccplen = box->datalen - 3;
        if (!(colr->iccp = jas_alloc2(colr->iccplen, sizeof(uint_fast8_t)))) {
            return -1;
        }
        if (jas_stream_read(in, colr->iccp, colr->iccplen) != colr->iccplen) {
            return -1;
        }
        break;
    }
    return 0;
436 437 438 439
}

static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out)
{
440 441 442 443 444 445
    jp2_cdef_t *cdef = &box->data.cdef;
    unsigned int i;
    for (i = 0; i < cdef->numchans; ++i) {
        fprintf(out, "channo=%d; type=%d; assoc=%d\n",
          (int)cdef->ents[i].channo, (int)cdef->ents[i].type, (int)cdef->ents[i].assoc);
    }
446 447 448 449
}

static void jp2_colr_destroy(jp2_box_t *box)
{
450 451 452 453
    jp2_colr_t *colr = &box->data.colr;
    if (colr->iccp) {
        jas_free(colr->iccp);
    }
454 455 456 457
}

static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in)
{
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    jp2_cdef_t *cdef = &box->data.cdef;
    jp2_cdefchan_t *chan;
    unsigned int channo;
    if (jp2_getuint16(in, &cdef->numchans)) {
        return -1;
    }
    if (!(cdef->ents = jas_alloc2(cdef->numchans, sizeof(jp2_cdefchan_t)))) {
        return -1;
    }
    for (channo = 0; channo < cdef->numchans; ++channo) {
        chan = &cdef->ents[channo];
        if (jp2_getuint16(in, &chan->channo) || jp2_getuint16(in, &chan->type) ||
          jp2_getuint16(in, &chan->assoc)) {
            return -1;
        }
    }
    return 0;
475 476 477 478 479 480 481 482
}

/******************************************************************************\
* Box output.
\******************************************************************************/

int jp2_box_put(jp2_box_t *box, jas_stream_t *out)
{
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    jas_stream_t *tmpstream;
    bool extlen;
    bool dataflag;

    tmpstream = 0;

    dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));

    if (dataflag) {
        tmpstream = jas_stream_memopen(0, 0);
        if (box->ops->putdata) {
            if ((*box->ops->putdata)(box, tmpstream)) {
                goto error;
            }
        }
        box->len = jas_stream_tell(tmpstream) + JP2_BOX_HDRLEN(false);
        jas_stream_rewind(tmpstream);
    }
    //extlen = (box->len >= (((uint_fast64_t)1) << 32)) != 0;
    if (jp2_putuint32(out, /*extlen ? 1 :*/ box->len)) {
        goto error;
    }
    if (jp2_putuint32(out, box->type)) {
        goto error;
    }
    /*if (extlen) {
        if (jp2_putuint64(out, box->len)) {
            goto error;
        }
    }*/

    if (dataflag) {
        if (jas_stream_copy(out, tmpstream, box->len - JP2_BOX_HDRLEN(false))) {
            goto error;
        }
        jas_stream_close(tmpstream);
    }

    return 0;
    abort();
523 524 525

error:

526 527 528 529
    if (tmpstream) {
        jas_stream_close(tmpstream);
    }
    return -1;
530 531 532 533
}

static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out)
{
534 535 536 537 538
    jp2_jp_t *jp = &box->data.jp;
    if (jp2_putuint32(out, jp->magic)) {
        return -1;
    }
    return 0;
539 540 541 542
}

static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out)
{
543 544 545 546 547 548 549 550 551 552 553
    jp2_ftyp_t *ftyp = &box->data.ftyp;
    unsigned int i;
    if (jp2_putuint32(out, ftyp->majver) || jp2_putuint32(out, ftyp->minver)) {
        return -1;
    }
    for (i = 0; i < ftyp->numcompatcodes; ++i) {
        if (jp2_putuint32(out, ftyp->compatcodes[i])) {
            return -1;
        }
    }
    return 0;
554 555 556 557
}

static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out)
{
558 559 560 561 562 563 564 565
    jp2_ihdr_t *ihdr = &box->data.ihdr;
    if (jp2_putuint32(out, ihdr->height) || jp2_putuint32(out, ihdr->width) ||
      jp2_putuint16(out, ihdr->numcmpts) || jp2_putuint8(out, ihdr->bpc) ||
      jp2_putuint8(out, ihdr->comptype) || jp2_putuint8(out, ihdr->csunk) ||
      jp2_putuint8(out, ihdr->ipr)) {
        return -1;
    }
    return 0;
566 567 568 569
}

static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out)
{
570 571 572 573 574 575 576 577
    jp2_bpcc_t *bpcc = &box->data.bpcc;
    unsigned int i;
    for (i = 0; i < bpcc->numcmpts; ++i) {
        if (jp2_putuint8(out, bpcc->bpcs[i])) {
            return -1;
        }
    }
    return 0;
578 579 580 581
}

static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out)
{
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    jp2_colr_t *colr = &box->data.colr;
    if (jp2_putuint8(out, colr->method) || jp2_putuint8(out, colr->pri) ||
      jp2_putuint8(out, colr->approx)) {
        return -1;
    }
    switch (colr->method) {
    case JP2_COLR_ENUM:
        if (jp2_putuint32(out, colr->csid)) {
            return -1;
        }
        break;
    case JP2_COLR_ICC:
        if (jas_stream_write(out, colr->iccp,
          JAS_CAST(int, colr->iccplen)) != JAS_CAST(int, colr->iccplen))
            return -1;
        break;
    }
    return 0;
600 601 602 603
}

static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out)
{
604 605 606
    jp2_cdef_t *cdef = &box->data.cdef;
    unsigned int i;
    jp2_cdefchan_t *ent;
607

608 609 610
    if (jp2_putuint16(out, cdef->numchans)) {
        return -1;
    }
611

612 613 614 615 616 617 618 619 620
    for (i = 0; i < cdef->numchans; ++i) {
        ent = &cdef->ents[i];
        if (jp2_putuint16(out, ent->channo) ||
          jp2_putuint16(out, ent->type) ||
          jp2_putuint16(out, ent->assoc)) {
            return -1;
        }
    }
    return 0;
621 622 623 624 625 626 627 628
}

/******************************************************************************\
* Input operations for primitive types.
\******************************************************************************/

static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val)
{
629 630 631 632 633 634 635 636
    int c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    if (val) {
        *val = c;
    }
    return 0;
637 638 639 640
}

static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val)
{
641 642 643 644 645 646 647 648 649 650 651 652 653 654
    uint_fast16_t v;
    int c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = (v << 8) | c;
    if (val) {
        *val = v;
    }
    return 0;
655 656 657 658
}

static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val)
{
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
    uint_fast32_t v;
    int c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = (v << 8) | c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = (v << 8) | c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = (v << 8) | c;
    if (val) {
        *val = v;
    }
    return 0;
681 682 683 684
}

static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val)
{
685 686 687
    uint_fast64_t tmpval;
    int i;
    int c;
688

689 690 691 692 693 694 695 696 697
    tmpval = 0;
    for (i = 0; i < 8; ++i) {
        tmpval <<= 8;
        if ((c = jas_stream_getc(in)) == EOF) {
            return -1;
        }
        tmpval |= (c & 0xff);
    }
    *val = tmpval;
698

699
    return 0;
700 701 702 703 704 705 706 707
}

/******************************************************************************\
* Output operations for primitive types.
\******************************************************************************/

static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val)
{
708 709 710 711
    if (jas_stream_putc(out, val & 0xff) == EOF) {
        return -1;
    }
    return 0;
712 713 714 715
}

static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val)
{
716 717 718 719 720
    if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
      jas_stream_putc(out, val & 0xff) == EOF) {
        return -1;
    }
    return 0;
721 722 723 724
}

static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val)
{
725 726 727 728 729 730 731
    if (jas_stream_putc(out, (val >> 24) & 0xff) == EOF ||
      jas_stream_putc(out, (val >> 16) & 0xff) == EOF ||
      jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
      jas_stream_putc(out, val & 0xff) == EOF) {
        return -1;
    }
    return 0;
732 733 734 735
}

static int jp2_putuint64(jas_stream_t *out, uint_fast64_t val)
{
736 737 738 739 740
    if (jp2_putuint32(out, (val >> 32) & 0xffffffffUL) ||
      jp2_putuint32(out, val & 0xffffffffUL)) {
        return -1;
    }
    return 0;
741 742 743 744 745 746 747 748
}

/******************************************************************************\
* Miscellaneous code.
\******************************************************************************/

jp2_boxinfo_t *jp2_boxinfolookup(int type)
{
749 750 751 752 753 754 755
    jp2_boxinfo_t *boxinfo;
    for (boxinfo = jp2_boxinfos; boxinfo->name; ++boxinfo) {
        if (boxinfo->type == type) {
            return boxinfo;
        }
    }
    return &jp2_boxinfo_unk;
756 757 758 759 760 761 762 763
}





static void jp2_cmap_destroy(jp2_box_t *box)
{
764 765 766 767
    jp2_cmap_t *cmap = &box->data.cmap;
    if (cmap->ents) {
        jas_free(cmap->ents);
    }
768 769 770 771
}

static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in)
{
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
    jp2_cmap_t *cmap = &box->data.cmap;
    jp2_cmapent_t *ent;
    unsigned int i;

    cmap->numchans = (box->datalen) / 4;
    if (!(cmap->ents = jas_alloc2(cmap->numchans, sizeof(jp2_cmapent_t)))) {
        return -1;
    }
    for (i = 0; i < cmap->numchans; ++i) {
        ent = &cmap->ents[i];
        if (jp2_getuint16(in, &ent->cmptno) ||
          jp2_getuint8(in, &ent->map) ||
          jp2_getuint8(in, &ent->pcol)) {
            return -1;
        }
    }

    return 0;
790 791 792 793
}

static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out)
{
794 795 796
    /* Eliminate compiler warning about unused variables. */
    box = 0;
    out = 0;
797

798
    return -1;
799 800 801 802
}

static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out)
{
803 804 805 806 807 808 809 810 811
    jp2_cmap_t *cmap = &box->data.cmap;
    unsigned int i;
    jp2_cmapent_t *ent;
    fprintf(out, "numchans = %d\n", (int) cmap->numchans);
    for (i = 0; i < cmap->numchans; ++i) {
        ent = &cmap->ents[i];
        fprintf(out, "cmptno=%d; map=%d; pcol=%d\n",
          (int) ent->cmptno, (int) ent->map, (int) ent->pcol);
    }
812 813 814 815
}

static void jp2_pclr_destroy(jp2_box_t *box)
{
816 817 818 819 820 821
    jp2_pclr_t *pclr = &box->data.pclr;
    if (pclr->lutdata) {
        jas_free(pclr->lutdata);
    }
    if (pclr->bpc)
        jas_free(pclr->bpc);
822 823 824 825
}

static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in)
{
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
    jp2_pclr_t *pclr = &box->data.pclr;
    int lutsize;
    unsigned int i;
    unsigned int j;
    int_fast32_t x;

    pclr->lutdata = 0;

    if (jp2_getuint16(in, &pclr->numlutents) ||
      jp2_getuint8(in, &pclr->numchans)) {
        return -1;
    }
    lutsize = pclr->numlutents * pclr->numchans;
    if (!(pclr->lutdata = jas_alloc2(lutsize, sizeof(int_fast32_t)))) {
        return -1;
    }
    if (!(pclr->bpc = jas_alloc2(pclr->numchans, sizeof(uint_fast8_t)))) {
        return -1;
    }
    for (i = 0; i < pclr->numchans; ++i) {
        if (jp2_getuint8(in, &pclr->bpc[i])) {
            return -1;
        }
    }
    for (i = 0; i < pclr->numlutents; ++i) {
        for (j = 0; j < pclr->numchans; ++j) {
            if (jp2_getint(in, (pclr->bpc[j] & 0x80) != 0,
              (pclr->bpc[j] & 0x7f) + 1, &x)) {
                return -1;
            }
            pclr->lutdata[i * pclr->numchans + j] = x;
        }
    }
    return 0;
860 861 862 863 864
}

static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out)
{
#if 0
865
    jp2_pclr_t *pclr = &box->data.pclr;
866 867 868 869
#endif
/* Eliminate warning about unused variable. */
box = 0;
out = 0;
870
    return -1;
871 872 873 874
}

static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out)
{
875 876 877 878 879 880 881 882 883 884
    jp2_pclr_t *pclr = &box->data.pclr;
    unsigned int i;
    int j;
    fprintf(out, "numents=%d; numchans=%d\n", (int) pclr->numlutents,
      (int) pclr->numchans);
    for (i = 0; i < pclr->numlutents; ++i) {
        for (j = 0; j < pclr->numchans; ++j) {
            fprintf(out, "LUT[%d][%d]=%d\n", i, j, (int)pclr->lutdata[i * pclr->numchans + j]);
        }
    }
885 886
}

887 888
static void jp2_uuid_destroy(jp2_box_t *box)
{
889 890 891 892 893 894
    jp2_uuid_t *uuid = &box->data.uuid;
    if (uuid->data)
    {
        jas_free(uuid->data);
        uuid->data = NULL;
    }
895 896 897 898
}

static int jp2_uuid_getdata(jp2_box_t *box, jas_stream_t *in)
{
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
    jp2_uuid_t *uuid = &box->data.uuid;
    int i;

    for (i = 0; i < 16; i++)
    {
        if (jp2_getuint8(in, &uuid->uuid[i]))
        return -1;
    }

    uuid->datalen = box->datalen - 16;
    uuid->data = jas_malloc(uuid->datalen * sizeof(uint_fast8_t));
    for (i = 0; i < uuid->datalen; i++)
    {
        if (jp2_getuint8(in, &uuid->data[i]))
        return -1;
    }
    return 0;
916 917 918 919
}

static int jp2_uuid_putdata(jp2_box_t *box, jas_stream_t *out)
{
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
    jp2_uuid_t *uuid = &box->data.uuid;
    int i;

    for (i = 0; i < 16; i++)
    {
        if (jp2_putuint8(out, uuid->uuid[i]))
        return -1;
    }

    for (i = 0; i < uuid->datalen; i++)
    {
        if (jp2_putuint8(out, uuid->data[i]))
        return -1;
    }
    return 0;
935 936
}

937 938
static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val)
{
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
    int c;
    int i;
    uint_fast32_t v;
    int m;

    m = (n + 7) / 8;

    v = 0;
    for (i = 0; i < m; ++i) {
        if ((c = jas_stream_getc(in)) == EOF) {
            return -1;
        }
        v = (v << 8) | c;
    }
    v &= ONES(n);
    if (s) {
        int sb;
        sb = v & (1 << (8 * m - 1));
        *val = ((~v) + 1) & ONES(8 * m);
        if (sb) {
            *val = -*val;
        }
    } else {
        *val = v;
    }

    return 0;
966 967 968 969
}

jp2_cdefchan_t *jp2_cdef_lookup(jp2_cdef_t *cdef, int channo)
{
970 971 972 973 974 975 976 977 978
    unsigned int i;
    jp2_cdefchan_t *cdefent;
    for (i = 0; i < cdef->numchans; ++i) {
        cdefent = &cdef->ents[i];
        if (cdefent->channo == JAS_CAST(unsigned int, channo)) {
            return cdefent;
        }
    }
    return 0;
979
}