diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2012-02-12 20:20:38 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2012-02-12 20:20:38 +0000 |
commit | 7270a56af11a9bf5ac18baa50dc8fedbd545af7f (patch) | |
tree | f51aa1306d76f2cadc1ab6da8be41c6d9801b0d5 | |
parent | f3ab913ff41c1873b2460abc2f3350e98c1ad181 (diff) | |
download | netpbm-mirror-7270a56af11a9bf5ac18baa50dc8fedbd545af7f.tar.gz netpbm-mirror-7270a56af11a9bf5ac18baa50dc8fedbd545af7f.tar.xz netpbm-mirror-7270a56af11a9bf5ac18baa50dc8fedbd545af7f.zip |
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1642 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r-- | converter/other/jpeg2000/libjasper/base/jas_image.c | 2 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/base/jas_seq.c | 3 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/common.mk | 5 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jp2/jp2_cod.c | 1050 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jpc/jpc_bs.c | 4 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jpc/jpc_cs.c | 2012 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jpc/jpc_dec.c | 3510 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jpc/jpc_enc.c | 4266 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jpc/jpc_mqenc.c | 392 |
9 files changed, 5647 insertions, 5597 deletions
diff --git a/converter/other/jpeg2000/libjasper/base/jas_image.c b/converter/other/jpeg2000/libjasper/base/jas_image.c index e6439fcd..5660ff0c 100644 --- a/converter/other/jpeg2000/libjasper/base/jas_image.c +++ b/converter/other/jpeg2000/libjasper/base/jas_image.c @@ -857,7 +857,7 @@ void jas_image_dump(jas_image_t *image, FILE *out) } for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) { cmpt = image->cmpts_[cmptno]; - fprintf(out, "prec=%d sgnd=%d\n", cmpt->prec_, cmpt->sgnd_); + fprintf(out, "prec=%d sgnd=%d\n", (int)cmpt->prec_, cmpt->sgnd_); if (jas_image_readcmpt(image, cmptno, 0, 0, 1, 1, data)) { abort(); } diff --git a/converter/other/jpeg2000/libjasper/base/jas_seq.c b/converter/other/jpeg2000/libjasper/base/jas_seq.c index b8e3c94b..12dc1595 100644 --- a/converter/other/jpeg2000/libjasper/base/jas_seq.c +++ b/converter/other/jpeg2000/libjasper/base/jas_seq.c @@ -414,7 +414,8 @@ int jas_matrix_output(jas_matrix_t *matrix, FILE *out) int j; jas_seqent_t x; - fprintf(out, "%d %d\n", jas_matrix_numrows(matrix), jas_matrix_numcols(matrix)); + fprintf(out, "%d %d\n", + (int)jas_matrix_numrows(matrix), (int)jas_matrix_numcols(matrix)); for (i = 0; i < jas_matrix_numrows(matrix); ++i) { for (j = 0; j < jas_matrix_numcols(matrix); ++j) { x = jas_matrix_get(matrix, i, j); diff --git a/converter/other/jpeg2000/libjasper/common.mk b/converter/other/jpeg2000/libjasper/common.mk index 687a9f3f..b31b1209 100644 --- a/converter/other/jpeg2000/libjasper/common.mk +++ b/converter/other/jpeg2000/libjasper/common.mk @@ -20,12 +20,13 @@ $(SUBDIRS:%=%/partlist): %/partlist: $(CURDIR)/% include $(SRCDIR)/common.mk -INCLUDES = -I$(JASPERSRCDIR)/include -Iimportinc +INCLUDES := -I$(JASPERSRCDIR)/include $(INCLUDES) DEFS = -DHAVE_LIBM=1 -DSTDC_HEADERS=1 -DHAVE_FCNTL_H=1 -DHAVE_LIMITS_H=1 -DHAVE_UNISTD_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STDDEF_H=1 -DEXCLUDE_BMP_SUPPORT -DEXCLUDE_RAS_SUPPORT -DEXCLUDE_MIF_SUPPORT -DEXCLUDE_JPG_SUPPORT -DEXCLUDE_PGX_SUPPORT -DEXCLUDE_PNM_SUPPORT $(LIB_OBJECTS):%.o:%.c - $(CC) -c $(INCLUDES) $(DEFS) $(CPPFLAGS) $(CFLAGS) $(CADD) $< + $(CC) -c $(INCLUDES) $(DEFS) $(CPPFLAGS) $(CFLAGS) \ + $(CFLAGS_PERSONAL) $(CADD) $< $(LIB_OBJECTS): importinc diff --git a/converter/other/jpeg2000/libjasper/jp2/jp2_cod.c b/converter/other/jpeg2000/libjasper/jp2/jp2_cod.c index f8fc013b..c99c9608 100644 --- a/converter/other/jpeg2000/libjasper/jp2/jp2_cod.c +++ b/converter/other/jpeg2000/libjasper/jp2/jp2_cod.c @@ -135,7 +135,7 @@ * Function prototypes. \******************************************************************************/ -#define ONES(n) ((1 << (n)) - 1) +#define ONES(n) ((1 << (n)) - 1) jp2_boxinfo_t *jp2_boxinfolookup(int type); @@ -183,49 +183,49 @@ static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out); \******************************************************************************/ jp2_boxinfo_t jp2_boxinfos[] = { - {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, 0, 0, 0, 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}}, + {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, 0, 0, 0, 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}}, }; jp2_boxinfo_t jp2_boxinfo_unk = { - 0, "Unknown", 0, {0, 0, 0, 0} + 0, "Unknown", 0, {0, 0, 0, 0} }; /******************************************************************************\ @@ -234,21 +234,21 @@ jp2_boxinfo_t jp2_boxinfo_unk = { jp2_box_t *jp2_box_create(int type) { - jp2_box_t *box; - jp2_boxinfo_t *boxinfo; + jp2_box_t *box; + jp2_boxinfo_t *boxinfo; - 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; + 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; } /******************************************************************************\ @@ -257,28 +257,28 @@ jp2_box_t *jp2_box_create(int type) void jp2_box_destroy(jp2_box_t *box) { - if (box->ops->destroy) { - (*box->ops->destroy)(box); - } - jas_free(box); + if (box->ops->destroy) { + (*box->ops->destroy)(box); + } + jas_free(box); } static void jp2_bpcc_destroy(jp2_box_t *box) { - jp2_bpcc_t *bpcc = &box->data.bpcc; - if (bpcc->bpcs) { - jas_free(bpcc->bpcs); - bpcc->bpcs = 0; - } + jp2_bpcc_t *bpcc = &box->data.bpcc; + if (bpcc->bpcs) { + jas_free(bpcc->bpcs); + bpcc->bpcs = 0; + } } static void jp2_cdef_destroy(jp2_box_t *box) { - jp2_cdef_t *cdef = &box->data.cdef; - if (cdef->ents) { - jas_free(cdef->ents); - cdef->ents = 0; - } + jp2_cdef_t *cdef = &box->data.cdef; + if (cdef->ents) { + jas_free(cdef->ents); + cdef->ents = 0; + } } /******************************************************************************\ @@ -287,223 +287,224 @@ static void jp2_cdef_destroy(jp2_box_t *box) jp2_box_t *jp2_box_get(jas_stream_t *in) { - jp2_box_t *box; - jp2_boxinfo_t *boxinfo; - jas_stream_t *tmpstream; - uint_fast32_t len; + 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; - } - box->len = extlen; - } - 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->len - JP2_BOX_HDRLEN)) { - goto error; - } - jas_stream_rewind(tmpstream); - - if (box->ops->getdata) { - if ((*box->ops->getdata)(box, tmpstream)) { - goto error; - } - } - jas_stream_close(tmpstream); - } - - if (jas_getdbglevel() > 0) { + 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; + } + box->len = extlen; + } + 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->len - JP2_BOX_HDRLEN)) { + goto error; + } + jas_stream_rewind(tmpstream); + + if (box->ops->getdata) { + if ((*box->ops->getdata)(box, tmpstream)) { + goto error; + } + } + jas_stream_close(tmpstream); + } + + if (jas_getdbglevel() > 0) { jp2_box_dump(box, stderr); } - return box; - abort(); + return box; + abort(); error: - if (box) { - jp2_box_destroy(box); - } - if (tmpstream) { - jas_stream_close(tmpstream); - } - return 0; + if (box) { + jp2_box_destroy(box); + } + if (tmpstream) { + jas_stream_close(tmpstream); + } + return 0; } void jp2_box_dump(jp2_box_t *box, FILE *out) { - jp2_boxinfo_t *boxinfo; - boxinfo = jp2_boxinfolookup(box->type); - assert(boxinfo); + jp2_boxinfo_t *boxinfo; + boxinfo = jp2_boxinfolookup(box->type); + assert(boxinfo); - fprintf(out, "JP2 box: "); - fprintf(out, "type=%c%s%c (0x%08x); length=%d\n", '"', boxinfo->name, - '"', box->type, box->len); - if (box->ops->dumpdata) { - (*box->ops->dumpdata)(box, out); - } + fprintf(out, "JP2 box: "); + fprintf(out, "type=%c%s%c (0x%08x); length=%d\n", '"', boxinfo->name, + '"', (unsigned int) box->type, (int)box->len); + if (box->ops->dumpdata) { + (*box->ops->dumpdata)(box, out); + } } static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in) { - jp2_jp_t *jp = &box->data.jp; - if (jp2_getuint32(in, &jp->magic)) { - return -1; - } - return 0; + jp2_jp_t *jp = &box->data.jp; + if (jp2_getuint32(in, &jp->magic)) { + return -1; + } + return 0; } static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in) { - jp2_ftyp_t *ftyp = &box->data.ftyp; - int i; - if (jp2_getuint32(in, &ftyp->majver) || jp2_getuint32(in, &ftyp->minver)) { - return -1; - } - ftyp->numcompatcodes = ((box->len - JP2_BOX_HDRLEN) - 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; + jp2_ftyp_t *ftyp = &box->data.ftyp; + int i; + if (jp2_getuint32(in, &ftyp->majver) || jp2_getuint32(in, &ftyp->minver)) { + return -1; + } + ftyp->numcompatcodes = ((box->len - JP2_BOX_HDRLEN) - 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; } static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in) { - 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; + 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; } static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in) { - jp2_bpcc_t *bpcc = &box->data.bpcc; - int i; - bpcc->numcmpts = box->len - JP2_BOX_HDRLEN; - if (!(bpcc->bpcs = jas_malloc(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; + jp2_bpcc_t *bpcc = &box->data.bpcc; + int i; + bpcc->numcmpts = box->len - JP2_BOX_HDRLEN; + if (!(bpcc->bpcs = jas_malloc(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; } static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out) { - 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; - } + 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; + } } static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in) { - 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->len - JP2_BOX_HDRLEN - 3; - if (!(colr->iccp = jas_malloc(colr->iccplen * sizeof(uint_fast8_t)))) { - return -1; - } - if (jas_stream_read(in, colr->iccp, colr->iccplen) != colr->iccplen) { - return -1; - } - break; - } - return 0; + 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->len - JP2_BOX_HDRLEN - 3; + if (!(colr->iccp = jas_malloc(colr->iccplen * sizeof(uint_fast8_t)))) { + return -1; + } + if (jas_stream_read(in, colr->iccp, colr->iccplen) != colr->iccplen) { + return -1; + } + break; + } + return 0; } static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out) { - jp2_cdef_t *cdef = &box->data.cdef; - int i; - for (i = 0; i < cdef->numchans; ++i) { - fprintf(out, "channo=%d; type=%d; assoc=%d\n", - cdef->ents[i].channo, cdef->ents[i].type, cdef->ents[i].assoc); - } + jp2_cdef_t *cdef = &box->data.cdef; + 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); + } } static void jp2_colr_destroy(jp2_box_t *box) { - jp2_colr_t *colr = &box->data.colr; - if (colr->iccp) { - free(colr->iccp); - } + jp2_colr_t *colr = &box->data.colr; + if (colr->iccp) { + free(colr->iccp); + } } static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in) { - jp2_cdef_t *cdef = &box->data.cdef; - jp2_cdefchan_t *chan; - int channo; - if (jp2_getuint16(in, &cdef->numchans)) { - return -1; - } - if (!(cdef->ents = jas_malloc(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; + jp2_cdef_t *cdef = &box->data.cdef; + jp2_cdefchan_t *chan; + int channo; + if (jp2_getuint16(in, &cdef->numchans)) { + return -1; + } + if (!(cdef->ents = jas_malloc(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; } /******************************************************************************\ @@ -512,23 +513,23 @@ static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in) int jp2_box_put(jp2_box_t *box, jas_stream_t *out) { - jas_stream_t *tmpstream; - bool dataflag; + jas_stream_t *tmpstream; + bool dataflag; - tmpstream = 0; + tmpstream = 0; - dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA)); + 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; - jas_stream_rewind(tmpstream); - } + 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; + jas_stream_rewind(tmpstream); + } /* There was code here in official Jasper to handle 64 bit lengths, but it was based on determining whether box->len fits in 32 bits. But box->len is a 32 bit data type, so it fits in 32 bits by @@ -536,119 +537,119 @@ int jp2_box_put(jp2_box_t *box, jas_stream_t *out) lengths that don't fit in 32 bits, or it passes invalid values of box->len to us. We assume the former because it's easier. */ - if (jp2_putuint32(out, box->len)) { - goto error; - } - if (jp2_putuint32(out, box->type)) { - goto error; - } - - if (dataflag) { - if (jas_stream_copy(out, tmpstream, box->len - JP2_BOX_HDRLEN)) { - goto error; - } - jas_stream_close(tmpstream); - } - - return 0; - abort(); + if (jp2_putuint32(out, box->len)) { + goto error; + } + if (jp2_putuint32(out, box->type)) { + goto error; + } + + if (dataflag) { + if (jas_stream_copy(out, tmpstream, box->len - JP2_BOX_HDRLEN)) { + goto error; + } + jas_stream_close(tmpstream); + } + + return 0; + abort(); error: - if (tmpstream) { - jas_stream_close(tmpstream); - } - return -1; + if (tmpstream) { + jas_stream_close(tmpstream); + } + return -1; } static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out) { - jp2_jp_t *jp = &box->data.jp; - if (jp2_putuint32(out, jp->magic)) { - return -1; - } - return 0; + jp2_jp_t *jp = &box->data.jp; + if (jp2_putuint32(out, jp->magic)) { + return -1; + } + return 0; } static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out) { - jp2_ftyp_t *ftyp = &box->data.ftyp; - 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; + jp2_ftyp_t *ftyp = &box->data.ftyp; + 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; } static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out) { - 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; + 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; } static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out) { - jp2_bpcc_t *bpcc = &box->data.bpcc; - int i; - for (i = 0; i < bpcc->numcmpts; ++i) { - if (jp2_putuint8(out, bpcc->bpcs[i])) { - return -1; - } - } - return 0; + jp2_bpcc_t *bpcc = &box->data.bpcc; + int i; + for (i = 0; i < bpcc->numcmpts; ++i) { + if (jp2_putuint8(out, bpcc->bpcs[i])) { + return -1; + } + } + return 0; } static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out) { - 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: - /* XXX - not implemented */ - abort(); - break; - } - return 0; + 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: + /* XXX - not implemented */ + abort(); + break; + } + return 0; } static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out) { - jp2_cdef_t *cdef = &box->data.cdef; - int i; - jp2_cdefchan_t *ent; + jp2_cdef_t *cdef = &box->data.cdef; + int i; + jp2_cdefchan_t *ent; - if (jp2_putuint16(out, cdef->numchans)) { - return -1; - } + if (jp2_putuint16(out, cdef->numchans)) { + return -1; + } - 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; + 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; } /******************************************************************************\ @@ -657,63 +658,63 @@ static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out) static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val) { - int c; - if ((c = jas_stream_getc(in)) == EOF) { - return -1; - } - if (val) { - *val = c; - } - return 0; + int c; + if ((c = jas_stream_getc(in)) == EOF) { + return -1; + } + if (val) { + *val = c; + } + return 0; } static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val) { - 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; + 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; } static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val) { - 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; + 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; } static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val) { - abort(); + abort(); } /******************************************************************************\ @@ -722,30 +723,30 @@ static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val) static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val) { - if (jas_stream_putc(out, val & 0xff) == EOF) { - return -1; - } - return 0; + if (jas_stream_putc(out, val & 0xff) == EOF) { + return -1; + } + return 0; } static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val) { - if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF || - jas_stream_putc(out, val & 0xff) == EOF) { - return -1; - } - return 0; + if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF || + jas_stream_putc(out, val & 0xff) == EOF) { + return -1; + } + return 0; } static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val) { - 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; + 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; } /******************************************************************************\ @@ -754,13 +755,13 @@ static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val) jp2_boxinfo_t *jp2_boxinfolookup(int type) { - jp2_boxinfo_t *boxinfo; - for (boxinfo = jp2_boxinfos; boxinfo->name; ++boxinfo) { - if (boxinfo->type == type) { - return boxinfo; - } - } - return &jp2_boxinfo_unk; + jp2_boxinfo_t *boxinfo; + for (boxinfo = jp2_boxinfos; boxinfo->name; ++boxinfo) { + if (boxinfo->type == type) { + return boxinfo; + } + } + return &jp2_boxinfo_unk; } @@ -769,161 +770,162 @@ jp2_boxinfo_t *jp2_boxinfolookup(int type) static void jp2_cmap_destroy(jp2_box_t *box) { - jp2_cmap_t *cmap = &box->data.cmap; - if (cmap->ents) { - jas_free(cmap->ents); - } + jp2_cmap_t *cmap = &box->data.cmap; + if (cmap->ents) { + jas_free(cmap->ents); + } } static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in) { - jp2_cmap_t *cmap = &box->data.cmap; - jp2_cmapent_t *ent; - int i; - - cmap->numchans = (box->len - JP2_BOX_HDRLEN) / 4; - if (!(cmap->ents = jas_malloc(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; + jp2_cmap_t *cmap = &box->data.cmap; + jp2_cmapent_t *ent; + int i; + + cmap->numchans = (box->len - JP2_BOX_HDRLEN) / 4; + if (!(cmap->ents = jas_malloc(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; } static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out) { - return -1; + return -1; } static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out) { - jp2_cmap_t *cmap = &box->data.cmap; - int i; - jp2_cmapent_t *ent; - fprintf(stderr, "numchans = %d\n", (int) cmap->numchans); - for (i = 0; i < cmap->numchans; ++i) { - ent = &cmap->ents[i]; - fprintf(stderr, "cmptno=%d; map=%d; pcol=%d\n", - (int) ent->cmptno, (int) ent->map, (int) ent->pcol); - } + jp2_cmap_t *cmap = &box->data.cmap; + int i; + jp2_cmapent_t *ent; + fprintf(stderr, "numchans = %d\n", (int) cmap->numchans); + for (i = 0; i < cmap->numchans; ++i) { + ent = &cmap->ents[i]; + fprintf(stderr, "cmptno=%d; map=%d; pcol=%d\n", + (int) ent->cmptno, (int) ent->map, (int) ent->pcol); + } } static void jp2_pclr_destroy(jp2_box_t *box) { - jp2_pclr_t *pclr = &box->data.pclr; - if (pclr->lutdata) { - jas_free(pclr->lutdata); - } + jp2_pclr_t *pclr = &box->data.pclr; + if (pclr->lutdata) { + jas_free(pclr->lutdata); + } } static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in) { - jp2_pclr_t *pclr = &box->data.pclr; - int lutsize; - int i; - 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_malloc(lutsize * sizeof(int_fast32_t)))) { - return -1; - } - if (!(pclr->bpc = jas_malloc(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; + jp2_pclr_t *pclr = &box->data.pclr; + int lutsize; + int i; + 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_malloc(lutsize * sizeof(int_fast32_t)))) { + return -1; + } + if (!(pclr->bpc = jas_malloc(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; } static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out) { /* This code from official Jasper must be part of unfinished work. It generates an unused variable warning. - jp2_pclr_t *pclr = &box->data.pclr; + jp2_pclr_t *pclr = &box->data.pclr; */ - return -1; + return -1; } static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out) { - jp2_pclr_t *pclr = &box->data.pclr; - 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, pclr->lutdata[i * pclr->numchans + j]); - } - } + jp2_pclr_t *pclr = &box->data.pclr; + 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]); + } + } } static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val) { - 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; + 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; } jp2_cdefchan_t *jp2_cdef_lookup(jp2_cdef_t *cdef, int channo) { - int i; - jp2_cdefchan_t *cdefent; - for (i = 0; i < cdef->numchans; ++i) { - cdefent = &cdef->ents[i]; - if (cdefent->channo == channo) { - return cdefent; - } - } - return 0; + int i; + jp2_cdefchan_t *cdefent; + for (i = 0; i < cdef->numchans; ++i) { + cdefent = &cdef->ents[i]; + if (cdefent->channo == channo) { + return cdefent; + } + } + return 0; } diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_bs.c b/converter/other/jpeg2000/libjasper/jpc/jpc_bs.c index 54f0a819..c66fcd99 100644 --- a/converter/other/jpeg2000/libjasper/jpc/jpc_bs.c +++ b/converter/other/jpeg2000/libjasper/jpc/jpc_bs.c @@ -157,10 +157,6 @@ jpc_bitstream_t *jpc_bitstream_sopen(jas_stream_t *stream, const char *mode) { jpc_bitstream_t *bitstream; - /* Ensure that the open mode is valid. */ - assert(!strcmp(mode, "r") || !strcmp(mode, "w") || !strcmp(mode, "r+") - || !strcmp(mode, "w+")); - if (!(bitstream = jpc_bitstream_alloc())) { return 0; } diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_cs.c b/converter/other/jpeg2000/libjasper/jpc/jpc_cs.c index 63cf8ba7..ffe6aab5 100644 --- a/converter/other/jpeg2000/libjasper/jpc/jpc_cs.c +++ b/converter/other/jpeg2000/libjasper/jpc/jpc_cs.c @@ -135,9 +135,9 @@ /* Marker segment table entry. */ typedef struct { - int id; - const char *name; - jpc_msops_t ops; + int id; + const char *name; + jpc_msops_t ops; } jpc_mstabent_t; /******************************************************************************\ @@ -220,40 +220,40 @@ static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, \******************************************************************************/ static jpc_mstabent_t jpc_mstab[] = { - {JPC_MS_SOC, "SOC", {0, 0, 0, 0}}, - {JPC_MS_SOT, "SOT", {0, jpc_sot_getparms, jpc_sot_putparms, - jpc_sot_dumpparms}}, - {JPC_MS_SOD, "SOD", {0, 0, 0, 0}}, - {JPC_MS_EOC, "EOC", {0, 0, 0, 0}}, - {JPC_MS_SIZ, "SIZ", {jpc_siz_destroyparms, jpc_siz_getparms, - jpc_siz_putparms, jpc_siz_dumpparms}}, - {JPC_MS_COD, "COD", {jpc_cod_destroyparms, jpc_cod_getparms, - jpc_cod_putparms, jpc_cod_dumpparms}}, - {JPC_MS_COC, "COC", {jpc_coc_destroyparms, jpc_coc_getparms, - jpc_coc_putparms, jpc_coc_dumpparms}}, - {JPC_MS_RGN, "RGN", {0, jpc_rgn_getparms, jpc_rgn_putparms, - jpc_rgn_dumpparms}}, - {JPC_MS_QCD, "QCD", {jpc_qcd_destroyparms, jpc_qcd_getparms, - jpc_qcd_putparms, jpc_qcd_dumpparms}}, - {JPC_MS_QCC, "QCC", {jpc_qcc_destroyparms, jpc_qcc_getparms, - jpc_qcc_putparms, jpc_qcc_dumpparms}}, - {JPC_MS_POC, "POC", {jpc_poc_destroyparms, jpc_poc_getparms, - jpc_poc_putparms, jpc_poc_dumpparms}}, - {JPC_MS_TLM, "TLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}}, - {JPC_MS_PLM, "PLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}}, - {JPC_MS_PPM, "PPM", {jpc_ppm_destroyparms, jpc_ppm_getparms, - jpc_ppm_putparms, jpc_ppm_dumpparms}}, - {JPC_MS_PPT, "PPT", {jpc_ppt_destroyparms, jpc_ppt_getparms, - jpc_ppt_putparms, jpc_ppt_dumpparms}}, - {JPC_MS_SOP, "SOP", {0, jpc_sop_getparms, jpc_sop_putparms, - jpc_sop_dumpparms}}, - {JPC_MS_EPH, "EPH", {0, 0, 0, 0}}, - {JPC_MS_CRG, "CRG", {0, jpc_crg_getparms, jpc_crg_putparms, - jpc_crg_dumpparms}}, - {JPC_MS_COM, "COM", {jpc_com_destroyparms, jpc_com_getparms, - jpc_com_putparms, jpc_com_dumpparms}}, - {-1, "UNKNOWN", {jpc_unk_destroyparms, jpc_unk_getparms, - jpc_unk_putparms, jpc_unk_dumpparms}} + {JPC_MS_SOC, "SOC", {0, 0, 0, 0}}, + {JPC_MS_SOT, "SOT", {0, jpc_sot_getparms, jpc_sot_putparms, + jpc_sot_dumpparms}}, + {JPC_MS_SOD, "SOD", {0, 0, 0, 0}}, + {JPC_MS_EOC, "EOC", {0, 0, 0, 0}}, + {JPC_MS_SIZ, "SIZ", {jpc_siz_destroyparms, jpc_siz_getparms, + jpc_siz_putparms, jpc_siz_dumpparms}}, + {JPC_MS_COD, "COD", {jpc_cod_destroyparms, jpc_cod_getparms, + jpc_cod_putparms, jpc_cod_dumpparms}}, + {JPC_MS_COC, "COC", {jpc_coc_destroyparms, jpc_coc_getparms, + jpc_coc_putparms, jpc_coc_dumpparms}}, + {JPC_MS_RGN, "RGN", {0, jpc_rgn_getparms, jpc_rgn_putparms, + jpc_rgn_dumpparms}}, + {JPC_MS_QCD, "QCD", {jpc_qcd_destroyparms, jpc_qcd_getparms, + jpc_qcd_putparms, jpc_qcd_dumpparms}}, + {JPC_MS_QCC, "QCC", {jpc_qcc_destroyparms, jpc_qcc_getparms, + jpc_qcc_putparms, jpc_qcc_dumpparms}}, + {JPC_MS_POC, "POC", {jpc_poc_destroyparms, jpc_poc_getparms, + jpc_poc_putparms, jpc_poc_dumpparms}}, + {JPC_MS_TLM, "TLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}}, + {JPC_MS_PLM, "PLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}}, + {JPC_MS_PPM, "PPM", {jpc_ppm_destroyparms, jpc_ppm_getparms, + jpc_ppm_putparms, jpc_ppm_dumpparms}}, + {JPC_MS_PPT, "PPT", {jpc_ppt_destroyparms, jpc_ppt_getparms, + jpc_ppt_putparms, jpc_ppt_dumpparms}}, + {JPC_MS_SOP, "SOP", {0, jpc_sop_getparms, jpc_sop_putparms, + jpc_sop_dumpparms}}, + {JPC_MS_EPH, "EPH", {0, 0, 0, 0}}, + {JPC_MS_CRG, "CRG", {0, jpc_crg_getparms, jpc_crg_putparms, + jpc_crg_dumpparms}}, + {JPC_MS_COM, "COM", {jpc_com_destroyparms, jpc_com_getparms, + jpc_com_putparms, jpc_com_dumpparms}}, + {-1, "UNKNOWN", {jpc_unk_destroyparms, jpc_unk_getparms, + jpc_unk_putparms, jpc_unk_dumpparms}} }; /******************************************************************************\ @@ -263,160 +263,160 @@ static jpc_mstabent_t jpc_mstab[] = { /* Create a code stream state object. */ jpc_cstate_t *jpc_cstate_create() { - jpc_cstate_t *cstate; - if (!(cstate = jas_malloc(sizeof(jpc_cstate_t)))) { - return 0; - } - cstate->numcomps = 0; - return cstate; + jpc_cstate_t *cstate; + if (!(cstate = jas_malloc(sizeof(jpc_cstate_t)))) { + return 0; + } + cstate->numcomps = 0; + return cstate; } /* Destroy a code stream state object. */ void jpc_cstate_destroy(jpc_cstate_t *cstate) { - jas_free(cstate); + jas_free(cstate); } /* Read a marker segment from a stream. */ jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate) { - jpc_ms_t *ms; - jpc_mstabent_t *mstabent; - jas_stream_t *tmpstream; - - if (!(ms = jpc_ms_create(0))) { - return 0; - } - - /* Get the marker type. */ - if (jpc_getuint16(in, &ms->id) || ms->id < JPC_MS_MIN || - ms->id > JPC_MS_MAX) { - jpc_ms_destroy(ms); - return 0; - } - - mstabent = jpc_mstab_lookup(ms->id); - ms->ops = &mstabent->ops; - - /* Get the marker segment length and parameters if present. */ - /* Note: It is tacitly assumed that a marker segment cannot have - parameters unless it has a length field. That is, there cannot - be a parameters field without a length field and vice versa. */ - if (JPC_MS_HASPARMS(ms->id)) { - /* Get the length of the marker segment. */ - if (jpc_getuint16(in, &ms->len) || ms->len < 3) { - jpc_ms_destroy(ms); - return 0; - } - /* Calculate the length of the marker segment parameters. */ - ms->len -= 2; - /* Create and prepare a temporary memory stream from which to - read the marker segment parameters. */ - /* Note: This approach provides a simple way of ensuring that - we never read beyond the end of the marker segment (even if - the marker segment length is errantly set too small). */ - if (!(tmpstream = jas_stream_memopen(0, 0))) { - jpc_ms_destroy(ms); - return 0; - } - if (jas_stream_copy(tmpstream, in, ms->len) || - jas_stream_seek(tmpstream, 0, SEEK_SET) < 0) { - jas_stream_close(tmpstream); - jpc_ms_destroy(ms); - return 0; - } - /* Get the marker segment parameters. */ - if ((*ms->ops->getparms)(ms, cstate, tmpstream)) { - ms->ops = 0; - jpc_ms_destroy(ms); - jas_stream_close(tmpstream); - return 0; - } - - if (jas_getdbglevel() > 0) { - jpc_ms_dump(ms, stderr); - } - - if (jas_stream_tell(tmpstream) != ms->len) { - fprintf(stderr, - "warning: trailing garbage in marker segment (%ld bytes)\n", - ms->len - jas_stream_tell(tmpstream)); - } - - /* Close the temporary stream. */ - jas_stream_close(tmpstream); - - } else { - /* There are no marker segment parameters. */ - ms->len = 0; - - if (jas_getdbglevel() > 0) { - jpc_ms_dump(ms, stderr); - } - } - - /* Update the code stream state information based on the type of - marker segment read. */ - /* Note: This is a bit of a hack, but I'm not going to define another - type of virtual function for this one special case. */ - if (ms->id == JPC_MS_SIZ) { - cstate->numcomps = ms->parms.siz.numcomps; - } - - return ms; + jpc_ms_t *ms; + jpc_mstabent_t *mstabent; + jas_stream_t *tmpstream; + + if (!(ms = jpc_ms_create(0))) { + return 0; + } + + /* Get the marker type. */ + if (jpc_getuint16(in, &ms->id) || ms->id < JPC_MS_MIN || + ms->id > JPC_MS_MAX) { + jpc_ms_destroy(ms); + return 0; + } + + mstabent = jpc_mstab_lookup(ms->id); + ms->ops = &mstabent->ops; + + /* Get the marker segment length and parameters if present. */ + /* Note: It is tacitly assumed that a marker segment cannot have + parameters unless it has a length field. That is, there cannot + be a parameters field without a length field and vice versa. */ + if (JPC_MS_HASPARMS(ms->id)) { + /* Get the length of the marker segment. */ + if (jpc_getuint16(in, &ms->len) || ms->len < 3) { + jpc_ms_destroy(ms); + return 0; + } + /* Calculate the length of the marker segment parameters. */ + ms->len -= 2; + /* Create and prepare a temporary memory stream from which to + read the marker segment parameters. */ + /* Note: This approach provides a simple way of ensuring that + we never read beyond the end of the marker segment (even if + the marker segment length is errantly set too small). */ + if (!(tmpstream = jas_stream_memopen(0, 0))) { + jpc_ms_destroy(ms); + return 0; + } + if (jas_stream_copy(tmpstream, in, ms->len) || + jas_stream_seek(tmpstream, 0, SEEK_SET) < 0) { + jas_stream_close(tmpstream); + jpc_ms_destroy(ms); + return 0; + } + /* Get the marker segment parameters. */ + if ((*ms->ops->getparms)(ms, cstate, tmpstream)) { + ms->ops = 0; + jpc_ms_destroy(ms); + jas_stream_close(tmpstream); + return 0; + } + + if (jas_getdbglevel() > 0) { + jpc_ms_dump(ms, stderr); + } + + if (jas_stream_tell(tmpstream) != ms->len) { + fprintf(stderr, + "warning: trailing garbage in marker segment (%ld bytes)\n", + ms->len - jas_stream_tell(tmpstream)); + } + + /* Close the temporary stream. */ + jas_stream_close(tmpstream); + + } else { + /* There are no marker segment parameters. */ + ms->len = 0; + + if (jas_getdbglevel() > 0) { + jpc_ms_dump(ms, stderr); + } + } + + /* Update the code stream state information based on the type of + marker segment read. */ + /* Note: This is a bit of a hack, but I'm not going to define another + type of virtual function for this one special case. */ + if (ms->id == JPC_MS_SIZ) { + cstate->numcomps = ms->parms.siz.numcomps; + } + + return ms; } /* Write a marker segment to a stream. */ int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms) { - jas_stream_t *tmpstream; - int len; - - /* Output the marker segment type. */ - if (jpc_putuint16(out, ms->id)) { - return -1; - } - - /* Output the marker segment length and parameters if necessary. */ - if (ms->ops->putparms) { - /* Create a temporary stream in which to buffer the - parameter data. */ - if (!(tmpstream = jas_stream_memopen(0, 0))) { - return -1; - } - if ((*ms->ops->putparms)(ms, cstate, tmpstream)) { - jas_stream_close(tmpstream); - return -1; - } - /* Get the number of bytes of parameter data written. */ - if ((len = jas_stream_tell(tmpstream)) < 0) { - jas_stream_close(tmpstream); - return -1; - } - ms->len = len; - /* Write the marker segment length and parameter data to - the output stream. */ - if (jas_stream_seek(tmpstream, 0, SEEK_SET) < 0 || - jpc_putuint16(out, ms->len + 2) || - jas_stream_copy(out, tmpstream, ms->len) < 0) { - jas_stream_close(tmpstream); - return -1; - } - /* Close the temporary stream. */ - jas_stream_close(tmpstream); - } - - /* This is a bit of a hack, but I'm not going to define another - type of virtual function for this one special case. */ - if (ms->id == JPC_MS_SIZ) { - cstate->numcomps = ms->parms.siz.numcomps; - } - - if (jas_getdbglevel() > 0) { - jpc_ms_dump(ms, stderr); - } - - return 0; + jas_stream_t *tmpstream; + int len; + + /* Output the marker segment type. */ + if (jpc_putuint16(out, ms->id)) { + return -1; + } + + /* Output the marker segment length and parameters if necessary. */ + if (ms->ops->putparms) { + /* Create a temporary stream in which to buffer the + parameter data. */ + if (!(tmpstream = jas_stream_memopen(0, 0))) { + return -1; + } + if ((*ms->ops->putparms)(ms, cstate, tmpstream)) { + jas_stream_close(tmpstream); + return -1; + } + /* Get the number of bytes of parameter data written. */ + if ((len = jas_stream_tell(tmpstream)) < 0) { + jas_stream_close(tmpstream); + return -1; + } + ms->len = len; + /* Write the marker segment length and parameter data to + the output stream. */ + if (jas_stream_seek(tmpstream, 0, SEEK_SET) < 0 || + jpc_putuint16(out, ms->len + 2) || + jas_stream_copy(out, tmpstream, ms->len) < 0) { + jas_stream_close(tmpstream); + return -1; + } + /* Close the temporary stream. */ + jas_stream_close(tmpstream); + } + + /* This is a bit of a hack, but I'm not going to define another + type of virtual function for this one special case. */ + if (ms->id == JPC_MS_SIZ) { + cstate->numcomps = ms->parms.siz.numcomps; + } + + if (jas_getdbglevel() > 0) { + jpc_ms_dump(ms, stderr); + } + + return 0; } /******************************************************************************\ @@ -426,45 +426,46 @@ int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms) /* Create a marker segment of the specified type. */ jpc_ms_t *jpc_ms_create(int type) { - jpc_ms_t *ms; - jpc_mstabent_t *mstabent; + jpc_ms_t *ms; + jpc_mstabent_t *mstabent; - if (!(ms = jas_malloc(sizeof(jpc_ms_t)))) { - return 0; - } - ms->id = type; - ms->len = 0; - mstabent = jpc_mstab_lookup(ms->id); - ms->ops = &mstabent->ops; - memset(&ms->parms, 0, sizeof(jpc_msparms_t)); - return ms; + if (!(ms = jas_malloc(sizeof(jpc_ms_t)))) { + return 0; + } + ms->id = type; + ms->len = 0; + mstabent = jpc_mstab_lookup(ms->id); + ms->ops = &mstabent->ops; + memset(&ms->parms, 0, sizeof(jpc_msparms_t)); + return ms; } /* Destroy a marker segment. */ void jpc_ms_destroy(jpc_ms_t *ms) { - if (ms->ops && ms->ops->destroyparms) { - (*ms->ops->destroyparms)(ms); - } - jas_free(ms); + if (ms->ops && ms->ops->destroyparms) { + (*ms->ops->destroyparms)(ms); + } + jas_free(ms); } /* Dump a marker segment to a stream for debugging. */ void jpc_ms_dump(jpc_ms_t *ms, FILE *out) { - jpc_mstabent_t *mstabent; - mstabent = jpc_mstab_lookup(ms->id); - fprintf(out, "type = 0x%04x (%s);", ms->id, mstabent->name); - if (JPC_MS_HASPARMS(ms->id)) { - fprintf(out, " len = %d;", ms->len + 2); - if (ms->ops->dumpparms) { - (*ms->ops->dumpparms)(ms, out); - } else { - fprintf(out, "\n"); - } - } else { - fprintf(out, "\n"); - } + jpc_mstabent_t *mstabent; + mstabent = jpc_mstab_lookup(ms->id); + fprintf(out, "type = 0x%04x (%s);", + (unsigned int)ms->id, mstabent->name); + if (JPC_MS_HASPARMS(ms->id)) { + fprintf(out, " len = %d;", (int)ms->len + 2); + if (ms->ops->dumpparms) { + (*ms->ops->dumpparms)(ms, out); + } else { + fprintf(out, "\n"); + } + } else { + fprintf(out, "\n"); + } } /******************************************************************************\ @@ -473,37 +474,37 @@ void jpc_ms_dump(jpc_ms_t *ms, FILE *out) static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_sot_t *sot = &ms->parms.sot; - if (jpc_getuint16(in, &sot->tileno) || - jpc_getuint32(in, &sot->len) || - jpc_getuint8(in, &sot->partno) || - jpc_getuint8(in, &sot->numparts)) { - return -1; - } - if (jas_stream_eof(in)) { - return -1; - } - return 0; + jpc_sot_t *sot = &ms->parms.sot; + if (jpc_getuint16(in, &sot->tileno) || + jpc_getuint32(in, &sot->len) || + jpc_getuint8(in, &sot->partno) || + jpc_getuint8(in, &sot->numparts)) { + return -1; + } + if (jas_stream_eof(in)) { + return -1; + } + return 0; } static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_sot_t *sot = &ms->parms.sot; - if (jpc_putuint16(out, sot->tileno) || - jpc_putuint32(out, sot->len) || - jpc_putuint8(out, sot->partno) || - jpc_putuint8(out, sot->numparts)) { - return -1; - } - return 0; + jpc_sot_t *sot = &ms->parms.sot; + if (jpc_putuint16(out, sot->tileno) || + jpc_putuint32(out, sot->len) || + jpc_putuint8(out, sot->partno) || + jpc_putuint8(out, sot->numparts)) { + return -1; + } + return 0; } static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_sot_t *sot = &ms->parms.sot; - fprintf(out, "tileno = %d; len = %d; partno = %d; numparts = %d\n", - sot->tileno, sot->len, sot->partno, sot->numparts); - return 0; + jpc_sot_t *sot = &ms->parms.sot; + fprintf(out, "tileno = %d; len = %d; partno = %d; numparts = %d\n", + (int)sot->tileno, (int)sot->len, sot->partno, sot->numparts); + return 0; } /******************************************************************************\ @@ -512,242 +513,244 @@ static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out) static void jpc_siz_destroyparms(jpc_ms_t *ms) { - jpc_siz_t *siz = &ms->parms.siz; - if (siz->comps) { - jas_free(siz->comps); - } + jpc_siz_t *siz = &ms->parms.siz; + if (siz->comps) { + jas_free(siz->comps); + } } static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_siz_t *siz = &ms->parms.siz; - int i; - uint_fast8_t tmp; - if (jpc_getuint16(in, &siz->caps) || - jpc_getuint32(in, &siz->width) || - jpc_getuint32(in, &siz->height) || - jpc_getuint32(in, &siz->xoff) || - jpc_getuint32(in, &siz->yoff) || - jpc_getuint32(in, &siz->tilewidth) || - jpc_getuint32(in, &siz->tileheight) || - jpc_getuint32(in, &siz->tilexoff) || - jpc_getuint32(in, &siz->tileyoff) || - jpc_getuint16(in, &siz->numcomps)) { - return -1; - } - if (!siz->width || !siz->height || !siz->tilewidth || - !siz->tileheight || !siz->numcomps) { - return -1; - } - if (!(siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t)))) { - return -1; - } - for (i = 0; i < siz->numcomps; ++i) { - if (jpc_getuint8(in, &tmp) || - jpc_getuint8(in, &siz->comps[i].hsamp) || - jpc_getuint8(in, &siz->comps[i].vsamp)) { - jas_free(siz->comps); - return -1; - } - siz->comps[i].sgnd = (tmp >> 7) & 1; - siz->comps[i].prec = (tmp & 0x7f) + 1; - } - if (jas_stream_eof(in)) { - jas_free(siz->comps); - return -1; - } - return 0; + jpc_siz_t *siz = &ms->parms.siz; + int i; + uint_fast8_t tmp; + if (jpc_getuint16(in, &siz->caps) || + jpc_getuint32(in, &siz->width) || + jpc_getuint32(in, &siz->height) || + jpc_getuint32(in, &siz->xoff) || + jpc_getuint32(in, &siz->yoff) || + jpc_getuint32(in, &siz->tilewidth) || + jpc_getuint32(in, &siz->tileheight) || + jpc_getuint32(in, &siz->tilexoff) || + jpc_getuint32(in, &siz->tileyoff) || + jpc_getuint16(in, &siz->numcomps)) { + return -1; + } + if (!siz->width || !siz->height || !siz->tilewidth || + !siz->tileheight || !siz->numcomps) { + return -1; + } + if (!(siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t)))) { + return -1; + } + for (i = 0; i < siz->numcomps; ++i) { + if (jpc_getuint8(in, &tmp) || + jpc_getuint8(in, &siz->comps[i].hsamp) || + jpc_getuint8(in, &siz->comps[i].vsamp)) { + jas_free(siz->comps); + return -1; + } + siz->comps[i].sgnd = (tmp >> 7) & 1; + siz->comps[i].prec = (tmp & 0x7f) + 1; + } + if (jas_stream_eof(in)) { + jas_free(siz->comps); + return -1; + } + return 0; } static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_siz_t *siz = &ms->parms.siz; - int i; - assert(siz->width && siz->height && siz->tilewidth && - siz->tileheight && siz->numcomps); - if (jpc_putuint16(out, siz->caps) || - jpc_putuint32(out, siz->width) || - jpc_putuint32(out, siz->height) || - jpc_putuint32(out, siz->xoff) || - jpc_putuint32(out, siz->yoff) || - jpc_putuint32(out, siz->tilewidth) || - jpc_putuint32(out, siz->tileheight) || - jpc_putuint32(out, siz->tilexoff) || - jpc_putuint32(out, siz->tileyoff) || - jpc_putuint16(out, siz->numcomps)) { - return -1; - } - for (i = 0; i < siz->numcomps; ++i) { - if (jpc_putuint8(out, ((siz->comps[i].sgnd & 1) << 7) | - ((siz->comps[i].prec - 1) & 0x7f)) || - jpc_putuint8(out, siz->comps[i].hsamp) || - jpc_putuint8(out, siz->comps[i].vsamp)) { - return -1; - } - } - return 0; + jpc_siz_t *siz = &ms->parms.siz; + int i; + assert(siz->width && siz->height && siz->tilewidth && + siz->tileheight && siz->numcomps); + if (jpc_putuint16(out, siz->caps) || + jpc_putuint32(out, siz->width) || + jpc_putuint32(out, siz->height) || + jpc_putuint32(out, siz->xoff) || + jpc_putuint32(out, siz->yoff) || + jpc_putuint32(out, siz->tilewidth) || + jpc_putuint32(out, siz->tileheight) || + jpc_putuint32(out, siz->tilexoff) || + jpc_putuint32(out, siz->tileyoff) || + jpc_putuint16(out, siz->numcomps)) { + return -1; + } + for (i = 0; i < siz->numcomps; ++i) { + if (jpc_putuint8(out, ((siz->comps[i].sgnd & 1) << 7) | + ((siz->comps[i].prec - 1) & 0x7f)) || + jpc_putuint8(out, siz->comps[i].hsamp) || + jpc_putuint8(out, siz->comps[i].vsamp)) { + return -1; + } + } + return 0; } static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_siz_t *siz = &ms->parms.siz; - int i; - fprintf(out, "caps = 0x%02x;\n", siz->caps); - fprintf(out, "width = %d; height = %d; xoff = %d; yoff = %d;\n", - siz->width, siz->height, siz->xoff, siz->yoff); - fprintf(out, "tilewidth = %d; tileheight = %d; tilexoff = %d; " - "tileyoff = %d;\n", siz->tilewidth, siz->tileheight, siz->tilexoff, - siz->tileyoff); - for (i = 0; i < siz->numcomps; ++i) { - fprintf(out, "prec[%d] = %d; sgnd[%d] = %d; hsamp[%d] = %d; " - "vsamp[%d] = %d\n", i, siz->comps[i].prec, i, - siz->comps[i].sgnd, i, siz->comps[i].hsamp, i, - siz->comps[i].vsamp); - } - return 0; -} - -/******************************************************************************\ + jpc_siz_t *siz = &ms->parms.siz; + int i; + fprintf(out, "caps = 0x%02x;\n", (unsigned int)siz->caps); + fprintf(out, "width = %d; height = %d; xoff = %d; yoff = %d;\n", + (int)siz->width, (int)siz->height, (int)siz->xoff, (int)siz->yoff); + fprintf(out, "tilewidth = %d; tileheight = %d; tilexoff = %d; " + "tileyoff = %d;\n", + (int)siz->tilewidth, (int)siz->tileheight, + (int)siz->tilexoff, (int)siz->tileyoff); + for (i = 0; i < siz->numcomps; ++i) { + fprintf(out, "prec[%d] = %d; sgnd[%d] = %d; hsamp[%d] = %d; " + "vsamp[%d] = %d\n", i, siz->comps[i].prec, i, + siz->comps[i].sgnd, i, siz->comps[i].hsamp, i, + siz->comps[i].vsamp); + } + return 0; +} + +/*****************************************************************************\ * COD marker segment operations. -\******************************************************************************/ +\*****************************************************************************/ static void jpc_cod_destroyparms(jpc_ms_t *ms) { - jpc_cod_t *cod = &ms->parms.cod; - jpc_cox_destroycompparms(&cod->compparms); + jpc_cod_t *cod = &ms->parms.cod; + jpc_cox_destroycompparms(&cod->compparms); } static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_cod_t *cod = &ms->parms.cod; - if (jpc_getuint8(in, &cod->csty)) { - return -1; - } - if (jpc_getuint8(in, &cod->prg) || - jpc_getuint16(in, &cod->numlyrs) || - jpc_getuint8(in, &cod->mctrans)) { - return -1; - } - if (jpc_cox_getcompparms(ms, cstate, in, - (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) { - return -1; - } - if (jas_stream_eof(in)) { - jpc_cod_destroyparms(ms); - return -1; - } - return 0; + jpc_cod_t *cod = &ms->parms.cod; + if (jpc_getuint8(in, &cod->csty)) { + return -1; + } + if (jpc_getuint8(in, &cod->prg) || + jpc_getuint16(in, &cod->numlyrs) || + jpc_getuint8(in, &cod->mctrans)) { + return -1; + } + if (jpc_cox_getcompparms(ms, cstate, in, + (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) { + return -1; + } + if (jas_stream_eof(in)) { + jpc_cod_destroyparms(ms); + return -1; + } + return 0; } static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_cod_t *cod = &ms->parms.cod; - assert(cod->numlyrs > 0 && cod->compparms.numdlvls <= 32); - assert(cod->compparms.numdlvls == cod->compparms.numrlvls - 1); - if (jpc_putuint8(out, cod->compparms.csty) || - jpc_putuint8(out, cod->prg) || - jpc_putuint16(out, cod->numlyrs) || - jpc_putuint8(out, cod->mctrans)) { - return -1; - } - if (jpc_cox_putcompparms(ms, cstate, out, - (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) { - return -1; - } - return 0; + jpc_cod_t *cod = &ms->parms.cod; + assert(cod->numlyrs > 0 && cod->compparms.numdlvls <= 32); + assert(cod->compparms.numdlvls == cod->compparms.numrlvls - 1); + if (jpc_putuint8(out, cod->compparms.csty) || + jpc_putuint8(out, cod->prg) || + jpc_putuint16(out, cod->numlyrs) || + jpc_putuint8(out, cod->mctrans)) { + return -1; + } + if (jpc_cox_putcompparms(ms, cstate, out, + (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) { + return -1; + } + return 0; } static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_cod_t *cod = &ms->parms.cod; - int i; - fprintf(out, "csty = 0x%02x;\n", cod->compparms.csty); - fprintf(out, "numdlvls = %d; qmfbid = %d; mctrans = %d\n", - cod->compparms.numdlvls, cod->compparms.qmfbid, cod->mctrans); - fprintf(out, "prg = %d; numlyrs = %d;\n", - cod->prg, cod->numlyrs); - fprintf(out, "cblkwidthval = %d; cblkheightval = %d; " - "cblksty = 0x%02x;\n", cod->compparms.cblkwidthval, cod->compparms.cblkheightval, - cod->compparms.cblksty); - if (cod->csty & JPC_COX_PRT) { - for (i = 0; i < cod->compparms.numrlvls; ++i) { - fprintf(stderr, "prcwidth[%d] = %d, prcheight[%d] = %d\n", - i, cod->compparms.rlvls[i].parwidthval, - i, cod->compparms.rlvls[i].parheightval); - } - } - return 0; -} - -/******************************************************************************\ + jpc_cod_t *cod = &ms->parms.cod; + int i; + fprintf(out, "csty = 0x%02x;\n", cod->compparms.csty); + fprintf(out, "numdlvls = %d; qmfbid = %d; mctrans = %d\n", + cod->compparms.numdlvls, cod->compparms.qmfbid, cod->mctrans); + fprintf(out, "prg = %d; numlyrs = %d;\n", + cod->prg, (int)cod->numlyrs); + fprintf(out, "cblkwidthval = %d; cblkheightval = %d; " + "cblksty = 0x%02x;\n", cod->compparms.cblkwidthval, cod->compparms.cblkheightval, + cod->compparms.cblksty); + if (cod->csty & JPC_COX_PRT) { + for (i = 0; i < cod->compparms.numrlvls; ++i) { + fprintf(stderr, "prcwidth[%d] = %d, prcheight[%d] = %d\n", + i, cod->compparms.rlvls[i].parwidthval, + i, cod->compparms.rlvls[i].parheightval); + } + } + return 0; +} + +/*****************************************************************************\ * COC marker segment operations. -\******************************************************************************/ +\*****************************************************************************/ static void jpc_coc_destroyparms(jpc_ms_t *ms) { - jpc_coc_t *coc = &ms->parms.coc; - jpc_cox_destroycompparms(&coc->compparms); + jpc_coc_t *coc = &ms->parms.coc; + jpc_cox_destroycompparms(&coc->compparms); } static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_coc_t *coc = &ms->parms.coc; - uint_fast8_t tmp; - if (cstate->numcomps <= 256) { - if (jpc_getuint8(in, &tmp)) { - return -1; - } - coc->compno = tmp; - } else { - if (jpc_getuint16(in, &coc->compno)) { - return -1; - } - } - if (jpc_getuint8(in, &coc->compparms.csty)) { - return -1; - } - if (jpc_cox_getcompparms(ms, cstate, in, - (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) { - return -1; - } - if (jas_stream_eof(in)) { - return -1; - } - return 0; + jpc_coc_t *coc = &ms->parms.coc; + uint_fast8_t tmp; + if (cstate->numcomps <= 256) { + if (jpc_getuint8(in, &tmp)) { + return -1; + } + coc->compno = tmp; + } else { + if (jpc_getuint16(in, &coc->compno)) { + return -1; + } + } + if (jpc_getuint8(in, &coc->compparms.csty)) { + return -1; + } + if (jpc_cox_getcompparms(ms, cstate, in, + (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) { + return -1; + } + if (jas_stream_eof(in)) { + return -1; + } + return 0; } static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_coc_t *coc = &ms->parms.coc; - assert(coc->compparms.numdlvls <= 32); - if (cstate->numcomps <= 256) { - if (jpc_putuint8(out, coc->compno)) { - return -1; - } - } else { - if (jpc_putuint16(out, coc->compno)) { - return -1; - } - } - if (jpc_putuint8(out, coc->compparms.csty)) { - return -1; - } - if (jpc_cox_putcompparms(ms, cstate, out, - (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) { - return -1; - } - return 0; + jpc_coc_t *coc = &ms->parms.coc; + assert(coc->compparms.numdlvls <= 32); + if (cstate->numcomps <= 256) { + if (jpc_putuint8(out, coc->compno)) { + return -1; + } + } else { + if (jpc_putuint16(out, coc->compno)) { + return -1; + } + } + if (jpc_putuint8(out, coc->compparms.csty)) { + return -1; + } + if (jpc_cox_putcompparms(ms, cstate, out, + (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) { + return -1; + } + return 0; } static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_coc_t *coc = &ms->parms.coc; - fprintf(out, "compno = %d; csty = 0x%02x; numdlvls = %d;\n", - coc->compno, coc->compparms.csty, coc->compparms.numdlvls); - fprintf(out, "cblkwidthval = %d; cblkheightval = %d; " - "cblksty = 0x%02x; qmfbid = %d;\n", coc->compparms.cblkwidthval, - coc->compparms.cblkheightval, coc->compparms.cblksty, coc->compparms.qmfbid); - return 0; + jpc_coc_t *coc = &ms->parms.coc; + fprintf(out, "compno = %d; csty = 0x%02x; numdlvls = %d;\n", + (int)coc->compno, coc->compparms.csty, coc->compparms.numdlvls); + fprintf(out, "cblkwidthval = %d; cblkheightval = %d; " + "cblksty = 0x%02x; qmfbid = %d;\n", coc->compparms.cblkwidthval, + coc->compparms.cblkheightval, coc->compparms.cblksty, + coc->compparms.qmfbid); + return 0; } /******************************************************************************\ * COD/COC marker segment operation helper functions. @@ -760,58 +763,58 @@ static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms) static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms) { - uint_fast8_t tmp; - int i; - if (jpc_getuint8(in, &compparms->numdlvls) || - jpc_getuint8(in, &compparms->cblkwidthval) || - jpc_getuint8(in, &compparms->cblkheightval) || - jpc_getuint8(in, &compparms->cblksty) || - jpc_getuint8(in, &compparms->qmfbid)) { - return -1; - } - compparms->numrlvls = compparms->numdlvls + 1; - if (prtflag) { - for (i = 0; i < compparms->numrlvls; ++i) { - if (jpc_getuint8(in, &tmp)) { - jpc_cox_destroycompparms(compparms); - return -1; - } - compparms->rlvls[i].parwidthval = tmp & 0xf; - compparms->rlvls[i].parheightval = (tmp >> 4) & 0xf; - } + uint_fast8_t tmp; + int i; + if (jpc_getuint8(in, &compparms->numdlvls) || + jpc_getuint8(in, &compparms->cblkwidthval) || + jpc_getuint8(in, &compparms->cblkheightval) || + jpc_getuint8(in, &compparms->cblksty) || + jpc_getuint8(in, &compparms->qmfbid)) { + return -1; + } + compparms->numrlvls = compparms->numdlvls + 1; + if (prtflag) { + for (i = 0; i < compparms->numrlvls; ++i) { + if (jpc_getuint8(in, &tmp)) { + jpc_cox_destroycompparms(compparms); + return -1; + } + compparms->rlvls[i].parwidthval = tmp & 0xf; + compparms->rlvls[i].parheightval = (tmp >> 4) & 0xf; + } /* Sigh. This bit should be in the same field in both COC and COD mrk segs. */ compparms->csty |= JPC_COX_PRT; - } else { - } - if (jas_stream_eof(in)) { - jpc_cox_destroycompparms(compparms); - return -1; - } - return 0; + } else { + } + if (jas_stream_eof(in)) { + jpc_cox_destroycompparms(compparms); + return -1; + } + return 0; } static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms) { - int i; - assert(compparms->numdlvls <= 32); - if (jpc_putuint8(out, compparms->numdlvls) || - jpc_putuint8(out, compparms->cblkwidthval) || - jpc_putuint8(out, compparms->cblkheightval) || - jpc_putuint8(out, compparms->cblksty) || - jpc_putuint8(out, compparms->qmfbid)) { - return -1; - } - if (prtflag) { - for (i = 0; i < compparms->numrlvls; ++i) { - if (jpc_putuint8(out, - ((compparms->rlvls[i].parheightval & 0xf) << 4) | - (compparms->rlvls[i].parwidthval & 0xf))) { - return -1; - } - } - } - return 0; + int i; + assert(compparms->numdlvls <= 32); + if (jpc_putuint8(out, compparms->numdlvls) || + jpc_putuint8(out, compparms->cblkwidthval) || + jpc_putuint8(out, compparms->cblkheightval) || + jpc_putuint8(out, compparms->cblksty) || + jpc_putuint8(out, compparms->qmfbid)) { + return -1; + } + if (prtflag) { + for (i = 0; i < compparms->numrlvls; ++i) { + if (jpc_putuint8(out, + ((compparms->rlvls[i].parheightval & 0xf) << 4) | + (compparms->rlvls[i].parwidthval & 0xf))) { + return -1; + } + } + } + return 0; } /******************************************************************************\ @@ -820,50 +823,50 @@ static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_rgn_t *rgn = &ms->parms.rgn; - uint_fast8_t tmp; - if (cstate->numcomps <= 256) { - if (jpc_getuint8(in, &tmp)) { - return -1; - } - rgn->compno = tmp; - } else { - if (jpc_getuint16(in, &rgn->compno)) { - return -1; - } - } - if (jpc_getuint8(in, &rgn->roisty) || - jpc_getuint8(in, &rgn->roishift)) { - return -1; - } - return 0; + jpc_rgn_t *rgn = &ms->parms.rgn; + uint_fast8_t tmp; + if (cstate->numcomps <= 256) { + if (jpc_getuint8(in, &tmp)) { + return -1; + } + rgn->compno = tmp; + } else { + if (jpc_getuint16(in, &rgn->compno)) { + return -1; + } + } + if (jpc_getuint8(in, &rgn->roisty) || + jpc_getuint8(in, &rgn->roishift)) { + return -1; + } + return 0; } static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_rgn_t *rgn = &ms->parms.rgn; - if (cstate->numcomps <= 256) { - if (jpc_putuint8(out, rgn->compno)) { - return -1; - } - } else { - if (jpc_putuint16(out, rgn->compno)) { - return -1; - } - } - if (jpc_putuint8(out, rgn->roisty) || - jpc_putuint8(out, rgn->roishift)) { - return -1; - } - return 0; + jpc_rgn_t *rgn = &ms->parms.rgn; + if (cstate->numcomps <= 256) { + if (jpc_putuint8(out, rgn->compno)) { + return -1; + } + } else { + if (jpc_putuint16(out, rgn->compno)) { + return -1; + } + } + if (jpc_putuint8(out, rgn->roisty) || + jpc_putuint8(out, rgn->roishift)) { + return -1; + } + return 0; } static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_rgn_t *rgn = &ms->parms.rgn; - fprintf(out, "compno = %d; roisty = %d; roishift = %d\n", - rgn->compno, rgn->roisty, rgn->roishift); - return 0; + jpc_rgn_t *rgn = &ms->parms.rgn; + fprintf(out, "compno = %d; roisty = %d; roishift = %d\n", + (int)rgn->compno, rgn->roisty, rgn->roishift); + return 0; } /******************************************************************************\ @@ -872,34 +875,34 @@ static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out) static void jpc_qcd_destroyparms(jpc_ms_t *ms) { - jpc_qcd_t *qcd = &ms->parms.qcd; - jpc_qcx_destroycompparms(&qcd->compparms); + jpc_qcd_t *qcd = &ms->parms.qcd; + jpc_qcx_destroycompparms(&qcd->compparms); } static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms; - return jpc_qcx_getcompparms(compparms, cstate, in, ms->len); + jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms; + return jpc_qcx_getcompparms(compparms, cstate, in, ms->len); } static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms; - return jpc_qcx_putcompparms(compparms, cstate, out); + jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms; + return jpc_qcx_putcompparms(compparms, cstate, out); } static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_qcd_t *qcd = &ms->parms.qcd; - int i; - fprintf(out, "qntsty = %d; numguard = %d; numstepsizes = %d\n", - (int) qcd->compparms.qntsty, qcd->compparms.numguard, qcd->compparms.numstepsizes); - for (i = 0; i < qcd->compparms.numstepsizes; ++i) { - fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n", - i, (unsigned) JPC_QCX_GETEXPN(qcd->compparms.stepsizes[i]), - i, (unsigned) JPC_QCX_GETMANT(qcd->compparms.stepsizes[i])); - } - return 0; + jpc_qcd_t *qcd = &ms->parms.qcd; + int i; + fprintf(out, "qntsty = %d; numguard = %d; numstepsizes = %d\n", + (int) qcd->compparms.qntsty, qcd->compparms.numguard, qcd->compparms.numstepsizes); + for (i = 0; i < qcd->compparms.numstepsizes; ++i) { + fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n", + i, (unsigned) JPC_QCX_GETEXPN(qcd->compparms.stepsizes[i]), + i, (unsigned) JPC_QCX_GETMANT(qcd->compparms.stepsizes[i])); + } + return 0; } /******************************************************************************\ @@ -908,133 +911,134 @@ static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out) static void jpc_qcc_destroyparms(jpc_ms_t *ms) { - jpc_qcc_t *qcc = &ms->parms.qcc; - jpc_qcx_destroycompparms(&qcc->compparms); + jpc_qcc_t *qcc = &ms->parms.qcc; + jpc_qcx_destroycompparms(&qcc->compparms); } static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_qcc_t *qcc = &ms->parms.qcc; - uint_fast8_t tmp; - int len; - len = ms->len; - if (cstate->numcomps <= 256) { - jpc_getuint8(in, &tmp); - qcc->compno = tmp; - --len; - } else { - jpc_getuint16(in, &qcc->compno); - len -= 2; - } - if (jpc_qcx_getcompparms(&qcc->compparms, cstate, in, len)) { - return -1; - } - if (jas_stream_eof(in)) { - jpc_qcc_destroyparms(ms); - return -1; - } - return 0; + jpc_qcc_t *qcc = &ms->parms.qcc; + uint_fast8_t tmp; + int len; + len = ms->len; + if (cstate->numcomps <= 256) { + jpc_getuint8(in, &tmp); + qcc->compno = tmp; + --len; + } else { + jpc_getuint16(in, &qcc->compno); + len -= 2; + } + if (jpc_qcx_getcompparms(&qcc->compparms, cstate, in, len)) { + return -1; + } + if (jas_stream_eof(in)) { + jpc_qcc_destroyparms(ms); + return -1; + } + return 0; } static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_qcc_t *qcc = &ms->parms.qcc; - if (cstate->numcomps <= 256) { - jpc_putuint8(out, qcc->compno); - } else { - jpc_putuint16(out, qcc->compno); - } - if (jpc_qcx_putcompparms(&qcc->compparms, cstate, out)) { - return -1; - } - return 0; + jpc_qcc_t *qcc = &ms->parms.qcc; + if (cstate->numcomps <= 256) { + jpc_putuint8(out, qcc->compno); + } else { + jpc_putuint16(out, qcc->compno); + } + if (jpc_qcx_putcompparms(&qcc->compparms, cstate, out)) { + return -1; + } + return 0; } static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_qcc_t *qcc = &ms->parms.qcc; - int i; - fprintf(out, "compno = %d; qntsty = %d; numguard = %d; " - "numstepsizes = %d\n", qcc->compno, qcc->compparms.qntsty, qcc->compparms.numguard, - qcc->compparms.numstepsizes); - for (i = 0; i < qcc->compparms.numstepsizes; ++i) { - fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n", - i, (unsigned) JPC_QCX_GETEXPN(qcc->compparms.stepsizes[i]), - i, (unsigned) JPC_QCX_GETMANT(qcc->compparms.stepsizes[i])); - } - return 0; -} - -/******************************************************************************\ + jpc_qcc_t *qcc = &ms->parms.qcc; + int i; + fprintf(out, "compno = %d; qntsty = %d; numguard = %d; " + "numstepsizes = %d\n", + (int)qcc->compno, qcc->compparms.qntsty, qcc->compparms.numguard, + qcc->compparms.numstepsizes); + for (i = 0; i < qcc->compparms.numstepsizes; ++i) { + fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n", + i, (unsigned) JPC_QCX_GETEXPN(qcc->compparms.stepsizes[i]), + i, (unsigned) JPC_QCX_GETMANT(qcc->compparms.stepsizes[i])); + } + return 0; +} + +/*****************************************************************************\ * QCD/QCC marker segment helper functions. -\******************************************************************************/ +\*****************************************************************************/ static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms) { - if (compparms->stepsizes) { - jas_free(compparms->stepsizes); - } + if (compparms->stepsizes) { + jas_free(compparms->stepsizes); + } } static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, jas_stream_t *in, uint_fast16_t len) { - uint_fast8_t tmp; - int n; - int i; - n = 0; - jpc_getuint8(in, &tmp); - ++n; - compparms->qntsty = tmp & 0x1f; - compparms->numguard = (tmp >> 5) & 7; - switch (compparms->qntsty) { - case JPC_QCX_SIQNT: - compparms->numstepsizes = 1; - break; - case JPC_QCX_NOQNT: - compparms->numstepsizes = (len - n); - break; - case JPC_QCX_SEQNT: - /* XXX - this is a hack */ - compparms->numstepsizes = (len - n) / 2; - break; - } + uint_fast8_t tmp; + int n; + int i; + n = 0; + jpc_getuint8(in, &tmp); + ++n; + compparms->qntsty = tmp & 0x1f; + compparms->numguard = (tmp >> 5) & 7; + switch (compparms->qntsty) { + case JPC_QCX_SIQNT: + compparms->numstepsizes = 1; + break; + case JPC_QCX_NOQNT: + compparms->numstepsizes = (len - n); + break; + case JPC_QCX_SEQNT: + /* XXX - this is a hack */ + compparms->numstepsizes = (len - n) / 2; + break; + } if (compparms->numstepsizes > 0) { - compparms->stepsizes = jas_malloc(compparms->numstepsizes * - sizeof(uint_fast32_t)); - assert(compparms->stepsizes); - for (i = 0; i < compparms->numstepsizes; ++i) { - if (compparms->qntsty == JPC_QCX_NOQNT) { - jpc_getuint8(in, &tmp); - compparms->stepsizes[i] = JPC_QCX_EXPN(tmp >> 3); - } else { - jpc_getuint16(in, &compparms->stepsizes[i]); - } - } + compparms->stepsizes = jas_malloc(compparms->numstepsizes * + sizeof(uint_fast32_t)); + assert(compparms->stepsizes); + for (i = 0; i < compparms->numstepsizes; ++i) { + if (compparms->qntsty == JPC_QCX_NOQNT) { + jpc_getuint8(in, &tmp); + compparms->stepsizes[i] = JPC_QCX_EXPN(tmp >> 3); + } else { + jpc_getuint16(in, &compparms->stepsizes[i]); + } + } } else { - compparms->stepsizes = 0; + compparms->stepsizes = 0; } - if (jas_stream_error(in) || jas_stream_eof(in)) { - jpc_qcx_destroycompparms(compparms); - return -1; - } - return 0; + if (jas_stream_error(in) || jas_stream_eof(in)) { + jpc_qcx_destroycompparms(compparms); + return -1; + } + return 0; } static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, jas_stream_t *out) { - int i; - jpc_putuint8(out, ((compparms->numguard & 7) << 5) | compparms->qntsty); - for (i = 0; i < compparms->numstepsizes; ++i) { - if (compparms->qntsty == JPC_QCX_NOQNT) { - jpc_putuint8(out, JPC_QCX_GETEXPN( - compparms->stepsizes[i]) << 3); - } else { - jpc_putuint16(out, compparms->stepsizes[i]); - } - } - return 0; + int i; + jpc_putuint8(out, ((compparms->numguard & 7) << 5) | compparms->qntsty); + for (i = 0; i < compparms->numstepsizes; ++i) { + if (compparms->qntsty == JPC_QCX_NOQNT) { + jpc_putuint8(out, JPC_QCX_GETEXPN( + compparms->stepsizes[i]) << 3); + } else { + jpc_putuint16(out, compparms->stepsizes[i]); + } + } + return 0; } /******************************************************************************\ @@ -1043,27 +1047,27 @@ static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_sop_t *sop = &ms->parms.sop; - if (jpc_getuint16(in, &sop->seqno)) { - return -1; - } - return 0; + jpc_sop_t *sop = &ms->parms.sop; + if (jpc_getuint16(in, &sop->seqno)) { + return -1; + } + return 0; } static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_sop_t *sop = &ms->parms.sop; - if (jpc_putuint16(out, sop->seqno)) { - return -1; - } - return 0; + jpc_sop_t *sop = &ms->parms.sop; + if (jpc_putuint16(out, sop->seqno)) { + return -1; + } + return 0; } static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_sop_t *sop = &ms->parms.sop; - fprintf(out, "seqno = %d;\n", sop->seqno); - return 0; + jpc_sop_t *sop = &ms->parms.sop; + fprintf(out, "seqno = %d;\n", (int)sop->seqno); + return 0; } /******************************************************************************\ @@ -1072,61 +1076,61 @@ static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out) static void jpc_ppm_destroyparms(jpc_ms_t *ms) { - jpc_ppm_t *ppm = &ms->parms.ppm; - if (ppm->data) { - jas_free(ppm->data); - } + jpc_ppm_t *ppm = &ms->parms.ppm; + if (ppm->data) { + jas_free(ppm->data); + } } static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_ppm_t *ppm = &ms->parms.ppm; - - ppm->data = 0; - - if (ms->len < 1) { - goto error; - } - if (jpc_getuint8(in, &ppm->ind)) { - goto error; - } - - ppm->len = ms->len - 1; - if (ppm->len > 0) { - if (!(ppm->data = jas_malloc(ppm->len * sizeof(unsigned char)))) { - goto error; - } - if (jas_stream_read(in, ppm->data, ppm->len) != ppm->len) { - goto error; - } - } else { - ppm->data = 0; - } - return 0; + jpc_ppm_t *ppm = &ms->parms.ppm; + + ppm->data = 0; + + if (ms->len < 1) { + goto error; + } + if (jpc_getuint8(in, &ppm->ind)) { + goto error; + } + + ppm->len = ms->len - 1; + if (ppm->len > 0) { + if (!(ppm->data = jas_malloc(ppm->len * sizeof(unsigned char)))) { + goto error; + } + if (jas_stream_read(in, ppm->data, ppm->len) != ppm->len) { + goto error; + } + } else { + ppm->data = 0; + } + return 0; error: - jpc_ppm_destroyparms(ms); - return -1; + jpc_ppm_destroyparms(ms); + return -1; } static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_ppm_t *ppm = &ms->parms.ppm; - if (jas_stream_write(out, (char *) ppm->data, ppm->len) != ppm->len) { - return -1; - } - return 0; + jpc_ppm_t *ppm = &ms->parms.ppm; + if (jas_stream_write(out, (char *) ppm->data, ppm->len) != ppm->len) { + return -1; + } + return 0; } static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_ppm_t *ppm = &ms->parms.ppm; - fprintf(out, "ind=%d; len = %d;\n", ppm->ind, ppm->len); - if (ppm->len > 0) { - fprintf(out, "data =\n"); - jas_memdump(out, ppm->data, ppm->len); - } - return 0; + jpc_ppm_t *ppm = &ms->parms.ppm; + fprintf(out, "ind=%d; len = %d;\n", ppm->ind, (int)ppm->len); + if (ppm->len > 0) { + fprintf(out, "data =\n"); + jas_memdump(out, ppm->data, ppm->len); + } + return 0; } /******************************************************************************\ @@ -1135,169 +1139,169 @@ static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out) static void jpc_ppt_destroyparms(jpc_ms_t *ms) { - jpc_ppt_t *ppt = &ms->parms.ppt; - if (ppt->data) { - jas_free(ppt->data); - } + jpc_ppt_t *ppt = &ms->parms.ppt; + if (ppt->data) { + jas_free(ppt->data); + } } static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_ppt_t *ppt = &ms->parms.ppt; - ppt->data = 0; - - if (ms->len < 1) { - goto error; - } - if (jpc_getuint8(in, &ppt->ind)) { - goto error; - } - ppt->len = ms->len - 1; - if (ppt->len > 0) { - if (!(ppt->data = jas_malloc(ppt->len * sizeof(unsigned char)))) { - goto error; - } - if (jas_stream_read(in, (char *) ppt->data, ppt->len) != ppt->len) { - goto error; - } - } else { - ppt->data = 0; - } - return 0; + jpc_ppt_t *ppt = &ms->parms.ppt; + ppt->data = 0; + + if (ms->len < 1) { + goto error; + } + if (jpc_getuint8(in, &ppt->ind)) { + goto error; + } + ppt->len = ms->len - 1; + if (ppt->len > 0) { + if (!(ppt->data = jas_malloc(ppt->len * sizeof(unsigned char)))) { + goto error; + } + if (jas_stream_read(in, (char *) ppt->data, ppt->len) != ppt->len) { + goto error; + } + } else { + ppt->data = 0; + } + return 0; error: - jpc_ppt_destroyparms(ms); - return -1; + jpc_ppt_destroyparms(ms); + return -1; } static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_ppt_t *ppt = &ms->parms.ppt; - if (jpc_putuint8(out, ppt->ind)) { - return -1; - } - if (jas_stream_write(out, (char *) ppt->data, ppt->len) != ppt->len) { - return -1; - } - return 0; + jpc_ppt_t *ppt = &ms->parms.ppt; + if (jpc_putuint8(out, ppt->ind)) { + return -1; + } + if (jas_stream_write(out, (char *) ppt->data, ppt->len) != ppt->len) { + return -1; + } + return 0; } static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_ppt_t *ppt = &ms->parms.ppt; - fprintf(out, "ind=%d; len = %d;\n", ppt->ind, ppt->len); - if (ppt->len > 0) { - fprintf(out, "data =\n"); - jas_memdump(out, ppt->data, ppt->len); - } - return 0; + jpc_ppt_t *ppt = &ms->parms.ppt; + fprintf(out, "ind=%d; len = %d;\n", ppt->ind, (int)ppt->len); + if (ppt->len > 0) { + fprintf(out, "data =\n"); + jas_memdump(out, ppt->data, ppt->len); + } + return 0; } -/******************************************************************************\ +/*****************************************************************************\ * POC marker segment operations. -\******************************************************************************/ +\*****************************************************************************/ static void jpc_poc_destroyparms(jpc_ms_t *ms) { - jpc_poc_t *poc = &ms->parms.poc; - if (poc->pchgs) { - jas_free(poc->pchgs); - } + jpc_poc_t *poc = &ms->parms.poc; + if (poc->pchgs) { + jas_free(poc->pchgs); + } } static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_poc_t *poc = &ms->parms.poc; - jpc_pocpchg_t *pchg; - int pchgno; - uint_fast8_t tmp; - poc->numpchgs = (cstate->numcomps > 256) ? (ms->len / 9) : - (ms->len / 7); - if (!(poc->pchgs = jas_malloc(poc->numpchgs * sizeof(jpc_pocpchg_t)))) { - goto error; - } - for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno, - ++pchg) { - if (jpc_getuint8(in, &pchg->rlvlnostart)) { - goto error; - } - if (cstate->numcomps > 256) { - if (jpc_getuint16(in, &pchg->compnostart)) { - goto error; - } - } else { - if (jpc_getuint8(in, &tmp)) { - goto error; - }; - pchg->compnostart = tmp; - } - if (jpc_getuint16(in, &pchg->lyrnoend) || - jpc_getuint8(in, &pchg->rlvlnoend)) { - goto error; - } - if (cstate->numcomps > 256) { - if (jpc_getuint16(in, &pchg->compnoend)) { - goto error; - } - } else { - if (jpc_getuint8(in, &tmp)) { - goto error; - } - pchg->compnoend = tmp; - } - if (jpc_getuint8(in, &pchg->prgord)) { - goto error; - } - if (pchg->rlvlnostart > pchg->rlvlnoend || - pchg->compnostart > pchg->compnoend) { - goto error; - } - } - return 0; + jpc_poc_t *poc = &ms->parms.poc; + jpc_pocpchg_t *pchg; + int pchgno; + uint_fast8_t tmp; + poc->numpchgs = (cstate->numcomps > 256) ? (ms->len / 9) : + (ms->len / 7); + if (!(poc->pchgs = jas_malloc(poc->numpchgs * sizeof(jpc_pocpchg_t)))) { + goto error; + } + for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno, + ++pchg) { + if (jpc_getuint8(in, &pchg->rlvlnostart)) { + goto error; + } + if (cstate->numcomps > 256) { + if (jpc_getuint16(in, &pchg->compnostart)) { + goto error; + } + } else { + if (jpc_getuint8(in, &tmp)) { + goto error; + }; + pchg->compnostart = tmp; + } + if (jpc_getuint16(in, &pchg->lyrnoend) || + jpc_getuint8(in, &pchg->rlvlnoend)) { + goto error; + } + if (cstate->numcomps > 256) { + if (jpc_getuint16(in, &pchg->compnoend)) { + goto error; + } + } else { + if (jpc_getuint8(in, &tmp)) { + goto error; + } + pchg->compnoend = tmp; + } + if (jpc_getuint8(in, &pchg->prgord)) { + goto error; + } + if (pchg->rlvlnostart > pchg->rlvlnoend || + pchg->compnostart > pchg->compnoend) { + goto error; + } + } + return 0; error: - jpc_poc_destroyparms(ms); - return -1; + jpc_poc_destroyparms(ms); + return -1; } static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_poc_t *poc = &ms->parms.poc; - jpc_pocpchg_t *pchg; - int pchgno; - for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno, - ++pchg) { - if (jpc_putuint8(out, pchg->rlvlnostart) || - ((cstate->numcomps > 256) ? - jpc_putuint16(out, pchg->compnostart) : - jpc_putuint8(out, pchg->compnostart)) || - jpc_putuint16(out, pchg->lyrnoend) || - jpc_putuint8(out, pchg->rlvlnoend) || - ((cstate->numcomps > 256) ? - jpc_putuint16(out, pchg->compnoend) : - jpc_putuint8(out, pchg->compnoend)) || - jpc_putuint8(out, pchg->prgord)) { - return -1; - } - } - return 0; + jpc_poc_t *poc = &ms->parms.poc; + jpc_pocpchg_t *pchg; + int pchgno; + for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno, + ++pchg) { + if (jpc_putuint8(out, pchg->rlvlnostart) || + ((cstate->numcomps > 256) ? + jpc_putuint16(out, pchg->compnostart) : + jpc_putuint8(out, pchg->compnostart)) || + jpc_putuint16(out, pchg->lyrnoend) || + jpc_putuint8(out, pchg->rlvlnoend) || + ((cstate->numcomps > 256) ? + jpc_putuint16(out, pchg->compnoend) : + jpc_putuint8(out, pchg->compnoend)) || + jpc_putuint8(out, pchg->prgord)) { + return -1; + } + } + return 0; } static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_poc_t *poc = &ms->parms.poc; - jpc_pocpchg_t *pchg; - int pchgno; - for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; - ++pchgno, ++pchg) { - fprintf(out, "po[%d] = %d; ", pchgno, pchg->prgord); - fprintf(out, "cs[%d] = %d; ce[%d] = %d; ", - pchgno, pchg->compnostart, pchgno, pchg->compnoend); - fprintf(out, "rs[%d] = %d; re[%d] = %d; ", - pchgno, pchg->rlvlnostart, pchgno, pchg->rlvlnoend); - fprintf(out, "le[%d] = %d\n", pchgno, pchg->lyrnoend); - } - return 0; + jpc_poc_t *poc = &ms->parms.poc; + jpc_pocpchg_t *pchg; + int pchgno; + for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; + ++pchgno, ++pchg) { + fprintf(out, "po[%d] = %d; ", pchgno, pchg->prgord); + fprintf(out, "cs[%d] = %d; ce[%d] = %d; ", + pchgno, (int)pchg->compnostart, pchgno, (int)pchg->compnoend); + fprintf(out, "rs[%d] = %d; re[%d] = %d; ", + pchgno, pchg->rlvlnostart, pchgno, pchg->rlvlnoend); + fprintf(out, "le[%d] = %d\n", pchgno, (int)pchg->lyrnoend); + } + return 0; } /******************************************************************************\ @@ -1306,58 +1310,58 @@ static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out) static void jpc_crg_destroyparms(jpc_ms_t *ms) { - jpc_crg_t *crg = &ms->parms.crg; - if (crg->comps) { - jas_free(crg->comps); - } + jpc_crg_t *crg = &ms->parms.crg; + if (crg->comps) { + jas_free(crg->comps); + } } static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_crg_t *crg = &ms->parms.crg; - jpc_crgcomp_t *comp; - uint_fast16_t compno; - crg->numcomps = cstate->numcomps; - if (!(crg->comps = jas_malloc(cstate->numcomps * sizeof(uint_fast16_t)))) { - return -1; - } - for (compno = 0, comp = crg->comps; compno < cstate->numcomps; - ++compno, ++comp) { - if (jpc_getuint16(in, &comp->hoff) || - jpc_getuint16(in, &comp->voff)) { - jpc_crg_destroyparms(ms); - return -1; - } - } - return 0; + jpc_crg_t *crg = &ms->parms.crg; + jpc_crgcomp_t *comp; + uint_fast16_t compno; + crg->numcomps = cstate->numcomps; + if (!(crg->comps = jas_malloc(cstate->numcomps * sizeof(uint_fast16_t)))) { + return -1; + } + for (compno = 0, comp = crg->comps; compno < cstate->numcomps; + ++compno, ++comp) { + if (jpc_getuint16(in, &comp->hoff) || + jpc_getuint16(in, &comp->voff)) { + jpc_crg_destroyparms(ms); + return -1; + } + } + return 0; } static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_crg_t *crg = &ms->parms.crg; - int compno; - jpc_crgcomp_t *comp; - for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno, - ++comp) { - if (jpc_putuint16(out, comp->hoff) || - jpc_putuint16(out, comp->voff)) { - return -1; - } - } - return 0; + jpc_crg_t *crg = &ms->parms.crg; + int compno; + jpc_crgcomp_t *comp; + for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno, + ++comp) { + if (jpc_putuint16(out, comp->hoff) || + jpc_putuint16(out, comp->voff)) { + return -1; + } + } + return 0; } static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_crg_t *crg = &ms->parms.crg; - int compno; - jpc_crgcomp_t *comp; - for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno, - ++comp) { - fprintf(out, "hoff[%d] = %d; voff[%d] = %d\n", compno, - comp->hoff, compno, comp->voff); - } - return 0; + jpc_crg_t *crg = &ms->parms.crg; + int compno; + jpc_crgcomp_t *comp; + for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno, + ++comp) { + fprintf(out, "hoff[%d] = %d; voff[%d] = %d\n", + compno, (int)comp->hoff, compno, (int)comp->voff); + } + return 0; } /******************************************************************************\ @@ -1366,112 +1370,110 @@ static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out) static void jpc_com_destroyparms(jpc_ms_t *ms) { - jpc_com_t *com = &ms->parms.com; - if (com->data) { - jas_free(com->data); - } + jpc_com_t *com = &ms->parms.com; + if (com->data) { + jas_free(com->data); + } } static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_com_t *com = &ms->parms.com; - if (jpc_getuint16(in, &com->regid)) { - return -1; - } - com->len = ms->len - 2; - if (com->len > 0) { - if (!(com->data = jas_malloc(com->len))) { - return -1; - } - if (jas_stream_read(in, com->data, com->len) != com->len) { - return -1; - } - } else { - com->data = 0; - } - return 0; + jpc_com_t *com = &ms->parms.com; + if (jpc_getuint16(in, &com->regid)) { + return -1; + } + com->len = ms->len - 2; + if (com->len > 0) { + if (!(com->data = jas_malloc(com->len))) { + return -1; + } + if (jas_stream_read(in, com->data, com->len) != com->len) { + return -1; + } + } else { + com->data = 0; + } + return 0; } static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - jpc_com_t *com = &ms->parms.com; - if (jpc_putuint16(out, com->regid)) { - return -1; - } - if (jas_stream_write(out, com->data, com->len) != com->len) { - return -1; - } - return 0; + jpc_com_t *com = &ms->parms.com; + if (jpc_putuint16(out, com->regid)) { + return -1; + } + if (jas_stream_write(out, com->data, com->len) != com->len) { + return -1; + } + return 0; } static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out) { - jpc_com_t *com = &ms->parms.com; - int i; - int printable; - fprintf(out, "regid = %d;\n", com->regid); - printable = 1; - for (i = 0; i < com->len; ++i) { - if (!isprint(com->data[i])) { - printable = 0; - break; - } - } - if (printable) { - fprintf(out, "data = "); - fwrite(com->data, sizeof(char), com->len, out); - fprintf(out, "\n"); - } - return 0; -} - -/******************************************************************************\ + jpc_com_t *com = &ms->parms.com; + int i; + int printable; + fprintf(out, "regid = %d;\n", (int)com->regid); + printable = 1; + for (i = 0, printable = 1; i < com->len && printable; ++i) { + if (!isprint(com->data[i])) + printable = 0; + } + if (printable) { + fprintf(out, "data = "); + fwrite(com->data, sizeof(char), com->len, out); + fprintf(out, "\n"); + } + return 0; +} + +/*****************************************************************************\ * Operations for unknown types of marker segments. -\******************************************************************************/ +\*****************************************************************************/ static void jpc_unk_destroyparms(jpc_ms_t *ms) { - jpc_unk_t *unk = &ms->parms.unk; - if (unk->data) { - jas_free(unk->data); - } + jpc_unk_t *unk = &ms->parms.unk; + if (unk->data) { + jas_free(unk->data); + } } static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) { - jpc_unk_t *unk = &ms->parms.unk; + jpc_unk_t *unk = &ms->parms.unk; - if (ms->len > 0) { - if (!(unk->data = jas_malloc(ms->len * sizeof(unsigned char)))) { - return -1; - } - if (jas_stream_read(in, (char *) unk->data, ms->len) != ms->len) { - jas_free(unk->data); - return -1; - } - unk->len = ms->len; - } else { - unk->data = 0; - unk->len = 0; - } - return 0; + if (ms->len > 0) { + if (!(unk->data = jas_malloc(ms->len * sizeof(unsigned char)))) { + return -1; + } + if (jas_stream_read(in, (char *) unk->data, ms->len) != ms->len) { + jas_free(unk->data); + return -1; + } + unk->len = ms->len; + } else { + unk->data = 0; + unk->len = 0; + } + return 0; } static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) { - /* If this function is called, we are trying to write an unsupported - type of marker segment. Return with an error indication. */ - return -1; + /* If this function is called, we are trying to write an unsupported + type of marker segment. Return with an error indication. */ + return -1; } static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out) { - int i; - jpc_unk_t *unk = &ms->parms.unk; - for (i = 0; i < unk->len; ++i) { - fprintf(out, "%02x ", unk->data[i]); - } - return 0; + int i; + jpc_unk_t *unk = &ms->parms.unk; + for (i = 0; i < unk->len; ++i) { + fprintf(out, "%02x ", unk->data[i]); + } + return 0; } /******************************************************************************\ @@ -1480,86 +1482,86 @@ static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out) int jpc_getuint8(jas_stream_t *in, uint_fast8_t *val) { - int c; - if ((c = jas_stream_getc(in)) == EOF) { - return -1; - } - if (val) { - *val = c; - } - return 0; + int c; + if ((c = jas_stream_getc(in)) == EOF) { + return -1; + } + if (val) { + *val = c; + } + return 0; } int jpc_putuint8(jas_stream_t *out, uint_fast8_t val) { - if (jas_stream_putc(out, val & 0xff) == EOF) { - return -1; - } - return 0; + if (jas_stream_putc(out, val & 0xff) == EOF) { + return -1; + } + return 0; } int jpc_getuint16(jas_stream_t *in, uint_fast16_t *val) { - 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; + 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; } int jpc_putuint16(jas_stream_t *out, uint_fast16_t val) { - if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF || - jas_stream_putc(out, val & 0xff) == EOF) { - return -1; - } - return 0; + if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF || + jas_stream_putc(out, val & 0xff) == EOF) { + return -1; + } + return 0; } int jpc_getuint32(jas_stream_t *in, uint_fast32_t *val) { - 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; + 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; } int jpc_putuint32(jas_stream_t *out, uint_fast32_t val) { - 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; + 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; } /******************************************************************************\ @@ -1568,47 +1570,47 @@ int jpc_putuint32(jas_stream_t *out, uint_fast32_t val) static jpc_mstabent_t *jpc_mstab_lookup(int id) { - jpc_mstabent_t *mstabent; - for (mstabent = jpc_mstab;; ++mstabent) { - if (mstabent->id == id || mstabent->id < 0) { - return mstabent; - } - } - assert(0); - return 0; + jpc_mstabent_t *mstabent; + for (mstabent = jpc_mstab;; ++mstabent) { + if (mstabent->id == id || mstabent->id < 0) { + return mstabent; + } + } + assert(0); + return 0; } int jpc_validate(jas_stream_t *in) { - int n; - int i; - unsigned char buf[2]; - - assert(JAS_STREAM_MAXPUTBACK >= 2); - - if ((n = jas_stream_read(in, (char *) buf, 2)) < 0) { - return -1; - } - for (i = n - 1; i >= 0; --i) { - if (jas_stream_ungetc(in, buf[i]) == EOF) { - return -1; - } - } - if (n < 2) { - return -1; - } - if (buf[0] == (JPC_MS_SOC >> 8) && buf[1] == (JPC_MS_SOC & 0xff)) { - return 0; - } - return -1; + int n; + int i; + unsigned char buf[2]; + + assert(JAS_STREAM_MAXPUTBACK >= 2); + + if ((n = jas_stream_read(in, (char *) buf, 2)) < 0) { + return -1; + } + for (i = n - 1; i >= 0; --i) { + if (jas_stream_ungetc(in, buf[i]) == EOF) { + return -1; + } + } + if (n < 2) { + return -1; + } + if (buf[0] == (JPC_MS_SOC >> 8) && buf[1] == (JPC_MS_SOC & 0xff)) { + return 0; + } + return -1; } int jpc_getdata(jas_stream_t *in, jas_stream_t *out, long len) { - return jas_stream_copy(out, in, len); + return jas_stream_copy(out, in, len); } int jpc_putdata(jas_stream_t *out, jas_stream_t *in, long len) { - return jas_stream_copy(out, in, len); + return jas_stream_copy(out, in, len); } diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c b/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c index 42980225..e4c395e2 100644 --- a/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c +++ b/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c @@ -140,30 +140,30 @@ * \******************************************************************************/ -#define JPC_MHSOC 0x0001 +#define JPC_MHSOC 0x0001 /* In the main header, expecting a SOC marker segment. */ -#define JPC_MHSIZ 0x0002 +#define JPC_MHSIZ 0x0002 /* In the main header, expecting a SIZ marker segment. */ -#define JPC_MH 0x0004 +#define JPC_MH 0x0004 /* In the main header, expecting "other" marker segments. */ -#define JPC_TPHSOT 0x0008 +#define JPC_TPHSOT 0x0008 /* In a tile-part header, expecting a SOT marker segment. */ -#define JPC_TPH 0x0010 +#define JPC_TPH 0x0010 /* In a tile-part header, expecting "other" marker segments. */ -#define JPC_MT 0x0020 +#define JPC_MT 0x0020 /* In the main trailer. */ typedef struct { - uint_fast16_t id; - /* The marker segment type. */ + uint_fast16_t id; + /* The marker segment type. */ - int validstates; - /* The states in which this type of marker segment can be - validly encountered. */ + int validstates; + /* The states in which this type of marker segment can be + validly encountered. */ - int (*action)(jpc_dec_t *dec, jpc_ms_t *ms); - /* The action to take upon encountering this type of marker segment. */ + int (*action)(jpc_dec_t *dec, jpc_ms_t *ms); + /* The action to take upon encountering this type of marker segment. */ } jpc_dec_mstabent_t; @@ -172,13 +172,13 @@ typedef struct { \******************************************************************************/ /* COD/COC parameters have been specified. */ -#define JPC_CSET 0x0001 +#define JPC_CSET 0x0001 /* QCD/QCC parameters have been specified. */ -#define JPC_QSET 0x0002 +#define JPC_QSET 0x0002 /* COD/COC parameters set from a COC marker segment. */ -#define JPC_COC 0x0004 +#define JPC_COC 0x0004 /* QCD/QCC parameters set from a QCC marker segment. */ -#define JPC_QCC 0x0008 +#define JPC_QCC 0x0008 /******************************************************************************\ * Local function prototypes. @@ -253,26 +253,26 @@ static int jpc_dec_parseopts(char *optstr, jpc_dec_importopts_t *opts); \******************************************************************************/ jpc_dec_mstabent_t jpc_dec_mstab[] = { - {JPC_MS_SOC, JPC_MHSOC, jpc_dec_process_soc}, - {JPC_MS_SOT, JPC_MH | JPC_TPHSOT, jpc_dec_process_sot}, - {JPC_MS_SOD, JPC_TPH, jpc_dec_process_sod}, - {JPC_MS_EOC, JPC_TPHSOT, jpc_dec_process_eoc}, - {JPC_MS_SIZ, JPC_MHSIZ, jpc_dec_process_siz}, - {JPC_MS_COD, JPC_MH | JPC_TPH, jpc_dec_process_cod}, - {JPC_MS_COC, JPC_MH | JPC_TPH, jpc_dec_process_coc}, - {JPC_MS_RGN, JPC_MH | JPC_TPH, jpc_dec_process_rgn}, - {JPC_MS_QCD, JPC_MH | JPC_TPH, jpc_dec_process_qcd}, - {JPC_MS_QCC, JPC_MH | JPC_TPH, jpc_dec_process_qcc}, - {JPC_MS_POC, JPC_MH | JPC_TPH, jpc_dec_process_poc}, - {JPC_MS_TLM, JPC_MH, 0}, - {JPC_MS_PLM, JPC_MH, 0}, - {JPC_MS_PLT, JPC_TPH, 0}, - {JPC_MS_PPM, JPC_MH, jpc_dec_process_ppm}, - {JPC_MS_PPT, JPC_TPH, jpc_dec_process_ppt}, - {JPC_MS_SOP, 0, 0}, - {JPC_MS_CRG, JPC_MH, jpc_dec_process_crg}, - {JPC_MS_COM, JPC_MH | JPC_TPH, jpc_dec_process_com}, - {0, JPC_MH | JPC_TPH, jpc_dec_process_unk} + {JPC_MS_SOC, JPC_MHSOC, jpc_dec_process_soc}, + {JPC_MS_SOT, JPC_MH | JPC_TPHSOT, jpc_dec_process_sot}, + {JPC_MS_SOD, JPC_TPH, jpc_dec_process_sod}, + {JPC_MS_EOC, JPC_TPHSOT, jpc_dec_process_eoc}, + {JPC_MS_SIZ, JPC_MHSIZ, jpc_dec_process_siz}, + {JPC_MS_COD, JPC_MH | JPC_TPH, jpc_dec_process_cod}, + {JPC_MS_COC, JPC_MH | JPC_TPH, jpc_dec_process_coc}, + {JPC_MS_RGN, JPC_MH | JPC_TPH, jpc_dec_process_rgn}, + {JPC_MS_QCD, JPC_MH | JPC_TPH, jpc_dec_process_qcd}, + {JPC_MS_QCC, JPC_MH | JPC_TPH, jpc_dec_process_qcc}, + {JPC_MS_POC, JPC_MH | JPC_TPH, jpc_dec_process_poc}, + {JPC_MS_TLM, JPC_MH, 0}, + {JPC_MS_PLM, JPC_MH, 0}, + {JPC_MS_PLT, JPC_TPH, 0}, + {JPC_MS_PPM, JPC_MH, jpc_dec_process_ppm}, + {JPC_MS_PPT, JPC_TPH, jpc_dec_process_ppt}, + {JPC_MS_SOP, 0, 0}, + {JPC_MS_CRG, JPC_MH, jpc_dec_process_crg}, + {JPC_MS_COM, JPC_MH | JPC_TPH, jpc_dec_process_com}, + {0, JPC_MH | JPC_TPH, jpc_dec_process_unk} }; /******************************************************************************\ @@ -281,106 +281,106 @@ jpc_dec_mstabent_t jpc_dec_mstab[] = { jas_image_t *jpc_decode(jas_stream_t *in, char *optstr) { - jpc_dec_importopts_t opts; - jpc_dec_t *dec; - jas_image_t *image; + jpc_dec_importopts_t opts; + jpc_dec_t *dec; + jas_image_t *image; - dec = 0; + dec = 0; - if (jpc_dec_parseopts(optstr, &opts)) { - goto error; - } + if (jpc_dec_parseopts(optstr, &opts)) { + goto error; + } - jpc_initluts(); + jpc_initluts(); - if (!(dec = jpc_dec_create(&opts, in))) { - goto error; - } + if (!(dec = jpc_dec_create(&opts, in))) { + goto error; + } - /* Do most of the work. */ - if (jpc_dec_decode(dec)) { - goto error; - } + /* Do most of the work. */ + if (jpc_dec_decode(dec)) { + goto error; + } - if (jas_image_numcmpts(dec->image) >= 3) { - jas_image_setcolorspace(dec->image, JAS_IMAGE_CS_RGB); - jas_image_setcmpttype(dec->image, 0, - JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R)); - jas_image_setcmpttype(dec->image, 1, - JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G)); - jas_image_setcmpttype(dec->image, 2, - JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B)); - } else { - jas_image_setcolorspace(dec->image, JAS_IMAGE_CS_GRAY); - jas_image_setcmpttype(dec->image, 0, - JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y)); - } + if (jas_image_numcmpts(dec->image) >= 3) { + jas_image_setcolorspace(dec->image, JAS_IMAGE_CS_RGB); + jas_image_setcmpttype(dec->image, 0, + JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R)); + jas_image_setcmpttype(dec->image, 1, + JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G)); + jas_image_setcmpttype(dec->image, 2, + JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B)); + } else { + jas_image_setcolorspace(dec->image, JAS_IMAGE_CS_GRAY); + jas_image_setcmpttype(dec->image, 0, + JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y)); + } - /* Save the return value. */ - image = dec->image; + /* Save the return value. */ + image = dec->image; - /* Stop the image from being discarded. */ - dec->image = 0; + /* Stop the image from being discarded. */ + dec->image = 0; - /* Destroy decoder. */ - jpc_dec_destroy(dec); + /* Destroy decoder. */ + jpc_dec_destroy(dec); - return image; + return image; error: - if (dec) { - jpc_dec_destroy(dec); - } - return 0; + if (dec) { + jpc_dec_destroy(dec); + } + return 0; } typedef enum { - OPT_MAXLYRS, - OPT_MAXPKTS, - OPT_DEBUG + OPT_MAXLYRS, + OPT_MAXPKTS, + OPT_DEBUG } optid_t; jas_taginfo_t decopts[] = { - {OPT_MAXLYRS, "maxlyrs"}, - {OPT_MAXPKTS, "maxpkts"}, - {OPT_DEBUG, "debug"}, - {-1, 0} + {OPT_MAXLYRS, "maxlyrs"}, + {OPT_MAXPKTS, "maxpkts"}, + {OPT_DEBUG, "debug"}, + {-1, 0} }; static int jpc_dec_parseopts(char *optstr, jpc_dec_importopts_t *opts) { - jas_tvparser_t *tvp; + jas_tvparser_t *tvp; - opts->debug = 0; - opts->maxlyrs = JPC_MAXLYRS; - opts->maxpkts = -1; + opts->debug = 0; + opts->maxlyrs = JPC_MAXLYRS; + opts->maxpkts = -1; - if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) { - return -1; - } + if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) { + return -1; + } - while (!jas_tvparser_next(tvp)) { - switch (jas_taginfo_nonull(jas_taginfos_lookup(decopts, - jas_tvparser_gettag(tvp)))->id) { - case OPT_MAXLYRS: - opts->maxlyrs = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_DEBUG: - opts->debug = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_MAXPKTS: - opts->maxpkts = atoi(jas_tvparser_getval(tvp)); - break; - default: - fprintf(stderr, "warning: ignoring invalid option %s\n", - jas_tvparser_gettag(tvp)); - break; - } - } + while (!jas_tvparser_next(tvp)) { + switch (jas_taginfo_nonull(jas_taginfos_lookup(decopts, + jas_tvparser_gettag(tvp)))->id) { + case OPT_MAXLYRS: + opts->maxlyrs = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_DEBUG: + opts->debug = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_MAXPKTS: + opts->maxpkts = atoi(jas_tvparser_getval(tvp)); + break; + default: + fprintf(stderr, "warning: ignoring invalid option %s\n", + jas_tvparser_gettag(tvp)); + break; + } + } - jas_tvparser_destroy(tvp); + jas_tvparser_destroy(tvp); - return 0; + return 0; } /******************************************************************************\ @@ -389,1121 +389,1121 @@ static int jpc_dec_parseopts(char *optstr, jpc_dec_importopts_t *opts) static jpc_dec_mstabent_t *jpc_dec_mstab_lookup(uint_fast16_t id) { - jpc_dec_mstabent_t *mstabent; - for (mstabent = jpc_dec_mstab; mstabent->id != 0; ++mstabent) { - if (mstabent->id == id) { - break; - } - } - return mstabent; + jpc_dec_mstabent_t *mstabent; + for (mstabent = jpc_dec_mstab; mstabent->id != 0; ++mstabent) { + if (mstabent->id == id) { + break; + } + } + return mstabent; } static int jpc_dec_decode(jpc_dec_t *dec) { - jpc_ms_t *ms; - jpc_dec_mstabent_t *mstabent; - int ret; - jpc_cstate_t *cstate; + jpc_ms_t *ms; + jpc_dec_mstabent_t *mstabent; + int ret; + jpc_cstate_t *cstate; - if (!(cstate = jpc_cstate_create())) { - return -1; - } - dec->cstate = cstate; + if (!(cstate = jpc_cstate_create())) { + return -1; + } + dec->cstate = cstate; - /* Initially, we should expect to encounter a SOC marker segment. */ - dec->state = JPC_MHSOC; + /* Initially, we should expect to encounter a SOC marker segment. */ + dec->state = JPC_MHSOC; - for (;;) { + for (;;) { - /* Get the next marker segment in the code stream. */ - if (!(ms = jpc_getms(dec->in, cstate))) { - fprintf(stderr, "cannot get marker segment\n"); - return -1; - } + /* Get the next marker segment in the code stream. */ + if (!(ms = jpc_getms(dec->in, cstate))) { + fprintf(stderr, "cannot get marker segment\n"); + return -1; + } - mstabent = jpc_dec_mstab_lookup(ms->id); - assert(mstabent); + mstabent = jpc_dec_mstab_lookup(ms->id); + assert(mstabent); - /* Ensure that this type of marker segment is permitted - at this point in the code stream. */ - if (!(dec->state & mstabent->validstates)) { - fprintf(stderr, "unexpected marker segment type\n"); - jpc_ms_destroy(ms); - return -1; - } + /* Ensure that this type of marker segment is permitted + at this point in the code stream. */ + if (!(dec->state & mstabent->validstates)) { + fprintf(stderr, "unexpected marker segment type\n"); + jpc_ms_destroy(ms); + return -1; + } - /* Process the marker segment. */ - if (mstabent->action) { - ret = (*mstabent->action)(dec, ms); - } else { - /* No explicit action is required. */ - ret = 0; - } + /* Process the marker segment. */ + if (mstabent->action) { + ret = (*mstabent->action)(dec, ms); + } else { + /* No explicit action is required. */ + ret = 0; + } - /* Destroy the marker segment. */ - jpc_ms_destroy(ms); + /* Destroy the marker segment. */ + jpc_ms_destroy(ms); - if (ret < 0) { - return -1; - } else if (ret > 0) { - break; - } + if (ret < 0) { + return -1; + } else if (ret > 0) { + break; + } - } + } - return 0; + return 0; } static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms) { - uint_fast16_t cmptno; - jpc_dec_cmpt_t *cmpt; - jpc_crg_t *crg; + uint_fast16_t cmptno; + jpc_dec_cmpt_t *cmpt; + jpc_crg_t *crg; - crg = &ms->parms.crg; - for (cmptno = 0, cmpt = dec->cmpts; cmptno < dec->numcomps; ++cmptno, - ++cmpt) { - /* Ignore the information in the CRG marker segment for now. - This information serves no useful purpose for decoding anyhow. - Some other parts of the code need to be changed if these lines - are uncommented. - cmpt->hsubstep = crg->comps[cmptno].hoff; - cmpt->vsubstep = crg->comps[cmptno].voff; - */ - } - return 0; + crg = &ms->parms.crg; + for (cmptno = 0, cmpt = dec->cmpts; cmptno < dec->numcomps; ++cmptno, + ++cmpt) { + /* Ignore the information in the CRG marker segment for now. + This information serves no useful purpose for decoding anyhow. + Some other parts of the code need to be changed if these lines + are uncommented. + cmpt->hsubstep = crg->comps[cmptno].hoff; + cmpt->vsubstep = crg->comps[cmptno].voff; + */ + } + return 0; } static int jpc_dec_process_soc(jpc_dec_t *dec, jpc_ms_t *ms) { - /* We should expect to encounter a SIZ marker segment next. */ - dec->state = JPC_MHSIZ; + /* We should expect to encounter a SIZ marker segment next. */ + dec->state = JPC_MHSIZ; - return 0; + return 0; } static int jpc_dec_process_sot(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_dec_tile_t *tile; - jpc_sot_t *sot = &ms->parms.sot; - jas_image_cmptparm_t *compinfos; - jas_image_cmptparm_t *compinfo; - jpc_dec_cmpt_t *cmpt; - uint_fast16_t cmptno; - - if (dec->state == JPC_MH) { - - compinfos = jas_malloc(dec->numcomps * sizeof(jas_image_cmptparm_t)); - assert(compinfos); - for (cmptno = 0, cmpt = dec->cmpts, compinfo = compinfos; - cmptno < dec->numcomps; ++cmptno, ++cmpt, ++compinfo) { - compinfo->tlx = 0; - compinfo->tly = 0; - compinfo->prec = cmpt->prec; - compinfo->sgnd = cmpt->sgnd; - compinfo->width = cmpt->width; - compinfo->height = cmpt->height; - compinfo->hstep = cmpt->hstep; - compinfo->vstep = cmpt->vstep; - } - - if (!(dec->image = jas_image_create(dec->numcomps, compinfos, - JAS_IMAGE_CS_UNKNOWN))) { - return -1; - } - jas_free(compinfos); - - /* Is the packet header information stored in PPM marker segments in - the main header? */ - if (dec->ppmstab) { - /* Convert the PPM marker segment data into a collection of streams - (one stream per tile-part). */ - if (!(dec->pkthdrstreams = jpc_ppmstabtostreams(dec->ppmstab))) { - abort(); - } - jpc_ppxstab_destroy(dec->ppmstab); - dec->ppmstab = 0; - } - } - - if (sot->len > 0) { - dec->curtileendoff = jas_stream_getrwcount(dec->in) - ms->len - - 4 + sot->len; - } else { - dec->curtileendoff = 0; - } - - if (sot->tileno > dec->numtiles) { - fprintf(stderr, "invalid tile number in SOT marker segment\n"); - return -1; - } - /* Set the current tile. */ - dec->curtile = &dec->tiles[sot->tileno]; - tile = dec->curtile; - /* Ensure that this is the expected part number. */ - if (sot->partno != tile->partno) { - return -1; - } - if (tile->numparts > 0 && sot->partno >= tile->numparts) { - return -1; - } - if (!tile->numparts && sot->numparts > 0) { - tile->numparts = sot->numparts; - } - - tile->pptstab = 0; - - switch (tile->state) { - case JPC_TILE_INIT: - /* This is the first tile-part for this tile. */ - tile->state = JPC_TILE_ACTIVE; - assert(!tile->cp); - if (!(tile->cp = jpc_dec_cp_copy(dec->cp))) { - return -1; - } - jpc_dec_cp_resetflags(dec->cp); - break; - default: - if (sot->numparts == sot->partno - 1) { - tile->state = JPC_TILE_ACTIVELAST; - } - break; - } - - /* Note: We do not increment the expected tile-part number until - all processing for this tile-part is complete. */ - - /* We should expect to encounter other tile-part header marker - segments next. */ - dec->state = JPC_TPH; - - return 0; + jpc_dec_tile_t *tile; + jpc_sot_t *sot = &ms->parms.sot; + jas_image_cmptparm_t *compinfos; + jas_image_cmptparm_t *compinfo; + jpc_dec_cmpt_t *cmpt; + uint_fast16_t cmptno; + + if (dec->state == JPC_MH) { + + compinfos = jas_malloc(dec->numcomps * sizeof(jas_image_cmptparm_t)); + assert(compinfos); + for (cmptno = 0, cmpt = dec->cmpts, compinfo = compinfos; + cmptno < dec->numcomps; ++cmptno, ++cmpt, ++compinfo) { + compinfo->tlx = 0; + compinfo->tly = 0; + compinfo->prec = cmpt->prec; + compinfo->sgnd = cmpt->sgnd; + compinfo->width = cmpt->width; + compinfo->height = cmpt->height; + compinfo->hstep = cmpt->hstep; + compinfo->vstep = cmpt->vstep; + } + + if (!(dec->image = jas_image_create(dec->numcomps, compinfos, + JAS_IMAGE_CS_UNKNOWN))) { + return -1; + } + jas_free(compinfos); + + /* Is the packet header information stored in PPM marker segments in + the main header? */ + if (dec->ppmstab) { + /* Convert the PPM marker segment data into a collection of streams + (one stream per tile-part). */ + if (!(dec->pkthdrstreams = jpc_ppmstabtostreams(dec->ppmstab))) { + abort(); + } + jpc_ppxstab_destroy(dec->ppmstab); + dec->ppmstab = 0; + } + } + + if (sot->len > 0) { + dec->curtileendoff = jas_stream_getrwcount(dec->in) - ms->len - + 4 + sot->len; + } else { + dec->curtileendoff = 0; + } + + if (sot->tileno > dec->numtiles) { + fprintf(stderr, "invalid tile number in SOT marker segment\n"); + return -1; + } + /* Set the current tile. */ + dec->curtile = &dec->tiles[sot->tileno]; + tile = dec->curtile; + /* Ensure that this is the expected part number. */ + if (sot->partno != tile->partno) { + return -1; + } + if (tile->numparts > 0 && sot->partno >= tile->numparts) { + return -1; + } + if (!tile->numparts && sot->numparts > 0) { + tile->numparts = sot->numparts; + } + + tile->pptstab = 0; + + switch (tile->state) { + case JPC_TILE_INIT: + /* This is the first tile-part for this tile. */ + tile->state = JPC_TILE_ACTIVE; + assert(!tile->cp); + if (!(tile->cp = jpc_dec_cp_copy(dec->cp))) { + return -1; + } + jpc_dec_cp_resetflags(dec->cp); + break; + default: + if (sot->numparts == sot->partno - 1) { + tile->state = JPC_TILE_ACTIVELAST; + } + break; + } + + /* Note: We do not increment the expected tile-part number until + all processing for this tile-part is complete. */ + + /* We should expect to encounter other tile-part header marker + segments next. */ + dec->state = JPC_TPH; + + return 0; } static int jpc_dec_process_sod(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_dec_tile_t *tile; - int pos; - - if (!(tile = dec->curtile)) { - return -1; - } - - if (!tile->partno) { - if (!jpc_dec_cp_isvalid(tile->cp)) { - return -1; - } - jpc_dec_cp_prepare(tile->cp); - if (jpc_dec_tileinit(dec, tile)) { - return -1; - } - } - - /* Are packet headers stored in the main header or tile-part header? */ - if (dec->pkthdrstreams) { - /* Get the stream containing the packet header data for this - tile-part. */ - if (!(tile->pkthdrstream = jpc_streamlist_remove(dec->pkthdrstreams, 0))) { - return -1; - } - } - - if (tile->pptstab) { - if (!tile->pkthdrstream) { - if (!(tile->pkthdrstream = jas_stream_memopen(0, 0))) { - return -1; - } - } - pos = jas_stream_tell(tile->pkthdrstream); - jas_stream_seek(tile->pkthdrstream, 0, SEEK_END); - if (jpc_pptstabwrite(tile->pkthdrstream, tile->pptstab)) { - return -1; - } - jas_stream_seek(tile->pkthdrstream, pos, SEEK_SET); - jpc_ppxstab_destroy(tile->pptstab); - tile->pptstab = 0; - } - - if (jas_getdbglevel() >= 10) { - jpc_dec_dump(dec, stderr); - } - - if (jpc_dec_decodepkts(dec, (tile->pkthdrstream) ? tile->pkthdrstream : - dec->in, dec->in)) { - fprintf(stderr, "jpc_dec_decodepkts failed\n"); - return -1; - } - - /* Gobble any unconsumed tile data. */ - if (dec->curtileendoff > 0) { - uint_fast32_t curoff; - uint_fast32_t n; - curoff = jas_stream_getrwcount(dec->in); - if (curoff < dec->curtileendoff) { - n = dec->curtileendoff - curoff; - fprintf(stderr, - "warning: ignoring trailing garbage (%lu bytes)\n", - (unsigned long) n); - - while (n-- > 0) { - if (jas_stream_getc(dec->in) == EOF) { - fprintf(stderr, "read error\n"); - return -1; - } - } - } else if (curoff > dec->curtileendoff) { - fprintf(stderr, - "warning: not enough tile data (%lu bytes)\n", - (unsigned long) curoff - dec->curtileendoff); - } - - } - - if (tile->numparts > 0 && tile->partno == tile->numparts - 1) { - if (jpc_dec_tiledecode(dec, tile)) { - return -1; - } - jpc_dec_tilefini(dec, tile); - } - - dec->curtile = 0; - - /* Increment the expected tile-part number. */ - ++tile->partno; - - /* We should expect to encounter a SOT marker segment next. */ - dec->state = JPC_TPHSOT; - - return 0; + jpc_dec_tile_t *tile; + int pos; + + if (!(tile = dec->curtile)) { + return -1; + } + + if (!tile->partno) { + if (!jpc_dec_cp_isvalid(tile->cp)) { + return -1; + } + jpc_dec_cp_prepare(tile->cp); + if (jpc_dec_tileinit(dec, tile)) { + return -1; + } + } + + /* Are packet headers stored in the main header or tile-part header? */ + if (dec->pkthdrstreams) { + /* Get the stream containing the packet header data for this + tile-part. */ + if (!(tile->pkthdrstream = jpc_streamlist_remove(dec->pkthdrstreams, 0))) { + return -1; + } + } + + if (tile->pptstab) { + if (!tile->pkthdrstream) { + if (!(tile->pkthdrstream = jas_stream_memopen(0, 0))) { + return -1; + } + } + pos = jas_stream_tell(tile->pkthdrstream); + jas_stream_seek(tile->pkthdrstream, 0, SEEK_END); + if (jpc_pptstabwrite(tile->pkthdrstream, tile->pptstab)) { + return -1; + } + jas_stream_seek(tile->pkthdrstream, pos, SEEK_SET); + jpc_ppxstab_destroy(tile->pptstab); + tile->pptstab = 0; + } + + if (jas_getdbglevel() >= 10) { + jpc_dec_dump(dec, stderr); + } + + if (jpc_dec_decodepkts(dec, (tile->pkthdrstream) ? tile->pkthdrstream : + dec->in, dec->in)) { + fprintf(stderr, "jpc_dec_decodepkts failed\n"); + return -1; + } + + /* Gobble any unconsumed tile data. */ + if (dec->curtileendoff > 0) { + uint_fast32_t curoff; + uint_fast32_t n; + curoff = jas_stream_getrwcount(dec->in); + if (curoff < dec->curtileendoff) { + n = dec->curtileendoff - curoff; + fprintf(stderr, + "warning: ignoring trailing garbage (%lu bytes)\n", + (unsigned long) n); + + while (n-- > 0) { + if (jas_stream_getc(dec->in) == EOF) { + fprintf(stderr, "read error\n"); + return -1; + } + } + } else if (curoff > dec->curtileendoff) { + fprintf(stderr, + "warning: not enough tile data (%lu bytes)\n", + (unsigned long) curoff - dec->curtileendoff); + } + + } + + if (tile->numparts > 0 && tile->partno == tile->numparts - 1) { + if (jpc_dec_tiledecode(dec, tile)) { + return -1; + } + jpc_dec_tilefini(dec, tile); + } + + dec->curtile = 0; + + /* Increment the expected tile-part number. */ + ++tile->partno; + + /* We should expect to encounter a SOT marker segment next. */ + dec->state = JPC_TPHSOT; + + return 0; } static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile) { - jpc_dec_tcomp_t *tcomp; - uint_fast16_t compno; - int rlvlno; - jpc_dec_rlvl_t *rlvl; - jpc_dec_band_t *band; - jpc_dec_prc_t *prc; - int bndno; - jpc_tsfb_band_t *bnd; - int bandno; - jpc_dec_ccp_t *ccp; - int prccnt; - jpc_dec_cblk_t *cblk; - int cblkcnt; - uint_fast32_t tlprcxstart; - uint_fast32_t tlprcystart; - uint_fast32_t brprcxend; - uint_fast32_t brprcyend; - uint_fast32_t tlcbgxstart; - uint_fast32_t tlcbgystart; - uint_fast32_t brcbgxend; - uint_fast32_t brcbgyend; - uint_fast32_t cbgxstart; - uint_fast32_t cbgystart; - uint_fast32_t cbgxend; - uint_fast32_t cbgyend; - uint_fast32_t tlcblkxstart; - uint_fast32_t tlcblkystart; - uint_fast32_t brcblkxend; - uint_fast32_t brcblkyend; - uint_fast32_t cblkxstart; - uint_fast32_t cblkystart; - uint_fast32_t cblkxend; - uint_fast32_t cblkyend; - uint_fast32_t tmpxstart; - uint_fast32_t tmpystart; - uint_fast32_t tmpxend; - uint_fast32_t tmpyend; - jpc_dec_cp_t *cp; - jpc_tsfb_band_t bnds[64]; - jpc_pchg_t *pchg; - int pchgno; - jpc_dec_cmpt_t *cmpt; - - cp = tile->cp; - tile->realmode = 0; - if (cp->mctid == JPC_MCT_ICT) { - tile->realmode = 1; - } - - for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < - dec->numcomps; ++compno, ++tcomp, ++cmpt) { - ccp = &tile->cp->ccps[compno]; - if (ccp->qmfbid == JPC_COX_INS) { - tile->realmode = 1; - } - tcomp->numrlvls = ccp->numrlvls; - if (!(tcomp->rlvls = jas_malloc(tcomp->numrlvls * - sizeof(jpc_dec_rlvl_t)))) { - return -1; - } - if (!(tcomp->data = jas_seq2d_create(JPC_CEILDIV(tile->xstart, - cmpt->hstep), JPC_CEILDIV(tile->ystart, cmpt->vstep), - JPC_CEILDIV(tile->xend, cmpt->hstep), JPC_CEILDIV(tile->yend, - cmpt->vstep)))) { - return -1; - } - if (!(tcomp->tsfb = jpc_cod_gettsfb(ccp->qmfbid, - tcomp->numrlvls - 1))) { - return -1; - } -{ - jpc_tsfb_getbands(tcomp->tsfb, jas_seq2d_xstart(tcomp->data), jas_seq2d_ystart(tcomp->data), jas_seq2d_xend(tcomp->data), jas_seq2d_yend(tcomp->data), bnds); -} - for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; - ++rlvlno, ++rlvl) { + jpc_dec_tcomp_t *tcomp; + uint_fast16_t compno; + int rlvlno; + jpc_dec_rlvl_t *rlvl; + jpc_dec_band_t *band; + jpc_dec_prc_t *prc; + int bndno; + jpc_tsfb_band_t *bnd; + int bandno; + jpc_dec_ccp_t *ccp; + int prccnt; + jpc_dec_cblk_t *cblk; + int cblkcnt; + uint_fast32_t tlprcxstart; + uint_fast32_t tlprcystart; + uint_fast32_t brprcxend; + uint_fast32_t brprcyend; + uint_fast32_t tlcbgxstart; + uint_fast32_t tlcbgystart; + uint_fast32_t brcbgxend; + uint_fast32_t brcbgyend; + uint_fast32_t cbgxstart; + uint_fast32_t cbgystart; + uint_fast32_t cbgxend; + uint_fast32_t cbgyend; + uint_fast32_t tlcblkxstart; + uint_fast32_t tlcblkystart; + uint_fast32_t brcblkxend; + uint_fast32_t brcblkyend; + uint_fast32_t cblkxstart; + uint_fast32_t cblkystart; + uint_fast32_t cblkxend; + uint_fast32_t cblkyend; + uint_fast32_t tmpxstart; + uint_fast32_t tmpystart; + uint_fast32_t tmpxend; + uint_fast32_t tmpyend; + jpc_dec_cp_t *cp; + jpc_tsfb_band_t bnds[64]; + jpc_pchg_t *pchg; + int pchgno; + jpc_dec_cmpt_t *cmpt; + + cp = tile->cp; + tile->realmode = 0; + if (cp->mctid == JPC_MCT_ICT) { + tile->realmode = 1; + } + + for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < + dec->numcomps; ++compno, ++tcomp, ++cmpt) { + ccp = &tile->cp->ccps[compno]; + if (ccp->qmfbid == JPC_COX_INS) { + tile->realmode = 1; + } + tcomp->numrlvls = ccp->numrlvls; + if (!(tcomp->rlvls = jas_malloc(tcomp->numrlvls * + sizeof(jpc_dec_rlvl_t)))) { + return -1; + } + if (!(tcomp->data = jas_seq2d_create(JPC_CEILDIV(tile->xstart, + cmpt->hstep), JPC_CEILDIV(tile->ystart, cmpt->vstep), + JPC_CEILDIV(tile->xend, cmpt->hstep), JPC_CEILDIV(tile->yend, + cmpt->vstep)))) { + return -1; + } + if (!(tcomp->tsfb = jpc_cod_gettsfb(ccp->qmfbid, + tcomp->numrlvls - 1))) { + return -1; + } +{ + jpc_tsfb_getbands(tcomp->tsfb, jas_seq2d_xstart(tcomp->data), jas_seq2d_ystart(tcomp->data), jas_seq2d_xend(tcomp->data), jas_seq2d_yend(tcomp->data), bnds); +} + for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; + ++rlvlno, ++rlvl) { rlvl->bands = 0; - rlvl->xstart = JPC_CEILDIVPOW2(tcomp->xstart, - tcomp->numrlvls - 1 - rlvlno); - rlvl->ystart = JPC_CEILDIVPOW2(tcomp->ystart, - tcomp->numrlvls - 1 - rlvlno); - rlvl->xend = JPC_CEILDIVPOW2(tcomp->xend, - tcomp->numrlvls - 1 - rlvlno); - rlvl->yend = JPC_CEILDIVPOW2(tcomp->yend, - tcomp->numrlvls - 1 - rlvlno); - rlvl->prcwidthexpn = ccp->prcwidthexpns[rlvlno]; - rlvl->prcheightexpn = ccp->prcheightexpns[rlvlno]; - tlprcxstart = JPC_FLOORDIVPOW2(rlvl->xstart, - rlvl->prcwidthexpn) << rlvl->prcwidthexpn; - tlprcystart = JPC_FLOORDIVPOW2(rlvl->ystart, - rlvl->prcheightexpn) << rlvl->prcheightexpn; - brprcxend = JPC_CEILDIVPOW2(rlvl->xend, - rlvl->prcwidthexpn) << rlvl->prcwidthexpn; - brprcyend = JPC_CEILDIVPOW2(rlvl->yend, - rlvl->prcheightexpn) << rlvl->prcheightexpn; - rlvl->numhprcs = (brprcxend - tlprcxstart) >> - rlvl->prcwidthexpn; - rlvl->numvprcs = (brprcyend - tlprcystart) >> - rlvl->prcheightexpn; - rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs; - - if (rlvl->xstart >= rlvl->xend || rlvl->ystart >= rlvl->yend) { - rlvl->bands = 0; - rlvl->numprcs = 0; - rlvl->numhprcs = 0; - rlvl->numvprcs = 0; - continue; - } - if (!rlvlno) { - tlcbgxstart = tlprcxstart; - tlcbgystart = tlprcystart; - brcbgxend = brprcxend; - brcbgyend = brprcyend; - rlvl->cbgwidthexpn = rlvl->prcwidthexpn; - rlvl->cbgheightexpn = rlvl->prcheightexpn; - } else { - tlcbgxstart = JPC_CEILDIVPOW2(tlprcxstart, 1); - tlcbgystart = JPC_CEILDIVPOW2(tlprcystart, 1); - brcbgxend = JPC_CEILDIVPOW2(brprcxend, 1); - brcbgyend = JPC_CEILDIVPOW2(brprcyend, 1); - rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1; - rlvl->cbgheightexpn = rlvl->prcheightexpn - 1; - } - rlvl->cblkwidthexpn = JAS_MIN(ccp->cblkwidthexpn, - rlvl->cbgwidthexpn); - rlvl->cblkheightexpn = JAS_MIN(ccp->cblkheightexpn, - rlvl->cbgheightexpn); - - rlvl->numbands = (!rlvlno) ? 1 : 3; - if (!(rlvl->bands = jas_malloc(rlvl->numbands * - sizeof(jpc_dec_band_t)))) { - return -1; - } - for (bandno = 0, band = rlvl->bands; - bandno < rlvl->numbands; ++bandno, ++band) { - bndno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + - bandno + 1); - bnd = &bnds[bndno]; - - band->orient = bnd->orient; - band->stepsize = ccp->stepsizes[bndno]; - band->analgain = JPC_NOMINALGAIN(ccp->qmfbid, - tcomp->numrlvls - 1, rlvlno, band->orient); - band->absstepsize = jpc_calcabsstepsize(band->stepsize, - cmpt->prec + band->analgain); - band->numbps = ccp->numguardbits + - JPC_QCX_GETEXPN(band->stepsize) - 1; - band->roishift = (ccp->roishift + band->numbps >= JPC_PREC) ? - (JPC_PREC - 1 - band->numbps) : ccp->roishift; - band->data = 0; - band->prcs = 0; - if (bnd->xstart == bnd->xend || bnd->ystart == bnd->yend) { - continue; - } - if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) { - return -1; - } - jas_seq2d_bindsub(band->data, tcomp->data, bnd->locxstart, bnd->locystart, bnd->locxend, bnd->locyend); - jas_seq2d_setshift(band->data, bnd->xstart, bnd->ystart); - - assert(rlvl->numprcs); - - if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_dec_prc_t)))) { - return -1; - } + rlvl->xstart = JPC_CEILDIVPOW2(tcomp->xstart, + tcomp->numrlvls - 1 - rlvlno); + rlvl->ystart = JPC_CEILDIVPOW2(tcomp->ystart, + tcomp->numrlvls - 1 - rlvlno); + rlvl->xend = JPC_CEILDIVPOW2(tcomp->xend, + tcomp->numrlvls - 1 - rlvlno); + rlvl->yend = JPC_CEILDIVPOW2(tcomp->yend, + tcomp->numrlvls - 1 - rlvlno); + rlvl->prcwidthexpn = ccp->prcwidthexpns[rlvlno]; + rlvl->prcheightexpn = ccp->prcheightexpns[rlvlno]; + tlprcxstart = JPC_FLOORDIVPOW2(rlvl->xstart, + rlvl->prcwidthexpn) << rlvl->prcwidthexpn; + tlprcystart = JPC_FLOORDIVPOW2(rlvl->ystart, + rlvl->prcheightexpn) << rlvl->prcheightexpn; + brprcxend = JPC_CEILDIVPOW2(rlvl->xend, + rlvl->prcwidthexpn) << rlvl->prcwidthexpn; + brprcyend = JPC_CEILDIVPOW2(rlvl->yend, + rlvl->prcheightexpn) << rlvl->prcheightexpn; + rlvl->numhprcs = (brprcxend - tlprcxstart) >> + rlvl->prcwidthexpn; + rlvl->numvprcs = (brprcyend - tlprcystart) >> + rlvl->prcheightexpn; + rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs; + + if (rlvl->xstart >= rlvl->xend || rlvl->ystart >= rlvl->yend) { + rlvl->bands = 0; + rlvl->numprcs = 0; + rlvl->numhprcs = 0; + rlvl->numvprcs = 0; + continue; + } + if (!rlvlno) { + tlcbgxstart = tlprcxstart; + tlcbgystart = tlprcystart; + brcbgxend = brprcxend; + brcbgyend = brprcyend; + rlvl->cbgwidthexpn = rlvl->prcwidthexpn; + rlvl->cbgheightexpn = rlvl->prcheightexpn; + } else { + tlcbgxstart = JPC_CEILDIVPOW2(tlprcxstart, 1); + tlcbgystart = JPC_CEILDIVPOW2(tlprcystart, 1); + brcbgxend = JPC_CEILDIVPOW2(brprcxend, 1); + brcbgyend = JPC_CEILDIVPOW2(brprcyend, 1); + rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1; + rlvl->cbgheightexpn = rlvl->prcheightexpn - 1; + } + rlvl->cblkwidthexpn = JAS_MIN(ccp->cblkwidthexpn, + rlvl->cbgwidthexpn); + rlvl->cblkheightexpn = JAS_MIN(ccp->cblkheightexpn, + rlvl->cbgheightexpn); + + rlvl->numbands = (!rlvlno) ? 1 : 3; + if (!(rlvl->bands = jas_malloc(rlvl->numbands * + sizeof(jpc_dec_band_t)))) { + return -1; + } + for (bandno = 0, band = rlvl->bands; + bandno < rlvl->numbands; ++bandno, ++band) { + bndno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + + bandno + 1); + bnd = &bnds[bndno]; + + band->orient = bnd->orient; + band->stepsize = ccp->stepsizes[bndno]; + band->analgain = JPC_NOMINALGAIN(ccp->qmfbid, + tcomp->numrlvls - 1, rlvlno, band->orient); + band->absstepsize = jpc_calcabsstepsize(band->stepsize, + cmpt->prec + band->analgain); + band->numbps = ccp->numguardbits + + JPC_QCX_GETEXPN(band->stepsize) - 1; + band->roishift = (ccp->roishift + band->numbps >= JPC_PREC) ? + (JPC_PREC - 1 - band->numbps) : ccp->roishift; + band->data = 0; + band->prcs = 0; + if (bnd->xstart == bnd->xend || bnd->ystart == bnd->yend) { + continue; + } + if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) { + return -1; + } + jas_seq2d_bindsub(band->data, tcomp->data, bnd->locxstart, bnd->locystart, bnd->locxend, bnd->locyend); + jas_seq2d_setshift(band->data, bnd->xstart, bnd->ystart); + + assert(rlvl->numprcs); + + if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_dec_prc_t)))) { + return -1; + } /************************************************/ - cbgxstart = tlcbgxstart; - cbgystart = tlcbgystart; - for (prccnt = rlvl->numprcs, prc = band->prcs; - prccnt > 0; --prccnt, ++prc) { - cbgxend = cbgxstart + (1 << rlvl->cbgwidthexpn); - cbgyend = cbgystart + (1 << rlvl->cbgheightexpn); - prc->xstart = JAS_MAX(cbgxstart, jas_seq2d_xstart(band->data)); - prc->ystart = JAS_MAX(cbgystart, jas_seq2d_ystart(band->data)); - prc->xend = JAS_MIN(cbgxend, jas_seq2d_xend(band->data)); - prc->yend = JAS_MIN(cbgyend, jas_seq2d_yend(band->data)); - if (prc->xend > prc->xstart && prc->yend > prc->ystart) { - tlcblkxstart = JPC_FLOORDIVPOW2(prc->xstart, - rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn; - tlcblkystart = JPC_FLOORDIVPOW2(prc->ystart, - rlvl->cblkheightexpn) << rlvl->cblkheightexpn; - brcblkxend = JPC_CEILDIVPOW2U(prc->xend, - rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn; - brcblkyend = JPC_CEILDIVPOW2U(prc->yend, - rlvl->cblkheightexpn) << rlvl->cblkheightexpn; - prc->numhcblks = (brcblkxend - tlcblkxstart) >> - rlvl->cblkwidthexpn; - prc->numvcblks = (brcblkyend - tlcblkystart) >> - rlvl->cblkheightexpn; - prc->numcblks = prc->numhcblks * prc->numvcblks; - assert(prc->numcblks > 0); - - if (!(prc->incltagtree = jpc_tagtree_create(prc->numhcblks, prc->numvcblks))) { - return -1; - } - if (!(prc->numimsbstagtree = jpc_tagtree_create(prc->numhcblks, prc->numvcblks))) { - return -1; - } - if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_dec_cblk_t)))) { - return -1; - } - - cblkxstart = cbgxstart; - cblkystart = cbgystart; - for (cblkcnt = prc->numcblks, cblk = prc->cblks; cblkcnt > 0;) { - cblkxend = cblkxstart + (1 << rlvl->cblkwidthexpn); - cblkyend = cblkystart + (1 << rlvl->cblkheightexpn); - tmpxstart = JAS_MAX(cblkxstart, prc->xstart); - tmpystart = JAS_MAX(cblkystart, prc->ystart); - tmpxend = JAS_MIN(cblkxend, prc->xend); - tmpyend = JAS_MIN(cblkyend, prc->yend); - if (tmpxend > tmpxstart && tmpyend > tmpystart) { - cblk->firstpassno = -1; - cblk->mqdec = 0; - cblk->nulldec = 0; - cblk->flags = 0; - cblk->numpasses = 0; - cblk->segs.head = 0; - cblk->segs.tail = 0; - cblk->curseg = 0; - cblk->numimsbs = 0; - cblk->numlenbits = 3; - cblk->flags = 0; - if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) { - return -1; - } - jas_seq2d_bindsub(cblk->data, band->data, tmpxstart, tmpystart, tmpxend, tmpyend); - ++cblk; - --cblkcnt; - } - cblkxstart += 1 << rlvl->cblkwidthexpn; - if (cblkxstart >= cbgxend) { - cblkxstart = cbgxstart; - cblkystart += 1 << rlvl->cblkheightexpn; - } - } - - } else { - prc->cblks = 0; - prc->incltagtree = 0; - prc->numimsbstagtree = 0; - } - cbgxstart += 1 << rlvl->cbgwidthexpn; - if (cbgxstart >= brcbgxend) { - cbgxstart = tlcbgxstart; - cbgystart += 1 << rlvl->cbgheightexpn; - } - - } + cbgxstart = tlcbgxstart; + cbgystart = tlcbgystart; + for (prccnt = rlvl->numprcs, prc = band->prcs; + prccnt > 0; --prccnt, ++prc) { + cbgxend = cbgxstart + (1 << rlvl->cbgwidthexpn); + cbgyend = cbgystart + (1 << rlvl->cbgheightexpn); + prc->xstart = JAS_MAX(cbgxstart, jas_seq2d_xstart(band->data)); + prc->ystart = JAS_MAX(cbgystart, jas_seq2d_ystart(band->data)); + prc->xend = JAS_MIN(cbgxend, jas_seq2d_xend(band->data)); + prc->yend = JAS_MIN(cbgyend, jas_seq2d_yend(band->data)); + if (prc->xend > prc->xstart && prc->yend > prc->ystart) { + tlcblkxstart = JPC_FLOORDIVPOW2(prc->xstart, + rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn; + tlcblkystart = JPC_FLOORDIVPOW2(prc->ystart, + rlvl->cblkheightexpn) << rlvl->cblkheightexpn; + brcblkxend = JPC_CEILDIVPOW2U(prc->xend, + rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn; + brcblkyend = JPC_CEILDIVPOW2U(prc->yend, + rlvl->cblkheightexpn) << rlvl->cblkheightexpn; + prc->numhcblks = (brcblkxend - tlcblkxstart) >> + rlvl->cblkwidthexpn; + prc->numvcblks = (brcblkyend - tlcblkystart) >> + rlvl->cblkheightexpn; + prc->numcblks = prc->numhcblks * prc->numvcblks; + assert(prc->numcblks > 0); + + if (!(prc->incltagtree = jpc_tagtree_create(prc->numhcblks, prc->numvcblks))) { + return -1; + } + if (!(prc->numimsbstagtree = jpc_tagtree_create(prc->numhcblks, prc->numvcblks))) { + return -1; + } + if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_dec_cblk_t)))) { + return -1; + } + + cblkxstart = cbgxstart; + cblkystart = cbgystart; + for (cblkcnt = prc->numcblks, cblk = prc->cblks; cblkcnt > 0;) { + cblkxend = cblkxstart + (1 << rlvl->cblkwidthexpn); + cblkyend = cblkystart + (1 << rlvl->cblkheightexpn); + tmpxstart = JAS_MAX(cblkxstart, prc->xstart); + tmpystart = JAS_MAX(cblkystart, prc->ystart); + tmpxend = JAS_MIN(cblkxend, prc->xend); + tmpyend = JAS_MIN(cblkyend, prc->yend); + if (tmpxend > tmpxstart && tmpyend > tmpystart) { + cblk->firstpassno = -1; + cblk->mqdec = 0; + cblk->nulldec = 0; + cblk->flags = 0; + cblk->numpasses = 0; + cblk->segs.head = 0; + cblk->segs.tail = 0; + cblk->curseg = 0; + cblk->numimsbs = 0; + cblk->numlenbits = 3; + cblk->flags = 0; + if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) { + return -1; + } + jas_seq2d_bindsub(cblk->data, band->data, tmpxstart, tmpystart, tmpxend, tmpyend); + ++cblk; + --cblkcnt; + } + cblkxstart += 1 << rlvl->cblkwidthexpn; + if (cblkxstart >= cbgxend) { + cblkxstart = cbgxstart; + cblkystart += 1 << rlvl->cblkheightexpn; + } + } + + } else { + prc->cblks = 0; + prc->incltagtree = 0; + prc->numimsbstagtree = 0; + } + cbgxstart += 1 << rlvl->cbgwidthexpn; + if (cbgxstart >= brcbgxend) { + cbgxstart = tlcbgxstart; + cbgystart += 1 << rlvl->cbgheightexpn; + } + + } /********************************************/ - } - } - } + } + } + } if (!(tile->pi = jpc_dec_pi_create(dec, tile))) { - return -1; + return -1; } - for (pchgno = 0; pchgno < jpc_pchglist_numpchgs(tile->cp->pchglist); - ++pchgno) { - pchg = jpc_pchg_copy(jpc_pchglist_get(tile->cp->pchglist, pchgno)); - assert(pchg); - jpc_pi_addpchg(tile->pi, pchg); - } - jpc_pi_init(tile->pi); + for (pchgno = 0; pchgno < jpc_pchglist_numpchgs(tile->cp->pchglist); + ++pchgno) { + pchg = jpc_pchg_copy(jpc_pchglist_get(tile->cp->pchglist, pchgno)); + assert(pchg); + jpc_pi_addpchg(tile->pi, pchg); + } + jpc_pi_init(tile->pi); - return 0; + return 0; } static int jpc_dec_tilefini(jpc_dec_t *dec, jpc_dec_tile_t *tile) { - jpc_dec_tcomp_t *tcomp; - int compno; - int bandno; - int rlvlno; - jpc_dec_band_t *band; - jpc_dec_rlvl_t *rlvl; - int prcno; - jpc_dec_prc_t *prc; - jpc_dec_seg_t *seg; - jpc_dec_cblk_t *cblk; - int cblkno; + jpc_dec_tcomp_t *tcomp; + int compno; + int bandno; + int rlvlno; + jpc_dec_band_t *band; + jpc_dec_rlvl_t *rlvl; + int prcno; + jpc_dec_prc_t *prc; + jpc_dec_seg_t *seg; + jpc_dec_cblk_t *cblk; + int cblkno; if (tile->tcomps) { - for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; - ++compno, ++tcomp) { - for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; - ++rlvlno, ++rlvl) { + for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; + ++compno, ++tcomp) { + for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; + ++rlvlno, ++rlvl) { if (!rlvl->bands) { - continue; + continue; } - for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; ++bandno, ++band) { + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; ++bandno, ++band) { if (band->prcs) { - for (prcno = 0, prc = band->prcs; prcno < - rlvl->numprcs; ++prcno, ++prc) { + for (prcno = 0, prc = band->prcs; prcno < + rlvl->numprcs; ++prcno, ++prc) { if (!prc->cblks) { - continue; -} - for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; ++cblkno, ++cblk) { - - while (cblk->segs.head) { - seg = cblk->segs.head; - jpc_seglist_remove(&cblk->segs, seg); - jpc_seg_destroy(seg); - } - jas_matrix_destroy(cblk->data); - if (cblk->mqdec) { - jpc_mqdec_destroy(cblk->mqdec); - } - if (cblk->nulldec) { - jpc_bitstream_close(cblk->nulldec); - } - if (cblk->flags) { - jas_matrix_destroy(cblk->flags); - } - } - if (prc->incltagtree) { - jpc_tagtree_destroy(prc->incltagtree); - } - if (prc->numimsbstagtree) { - jpc_tagtree_destroy(prc->numimsbstagtree); - } - if (prc->cblks) { - jas_free(prc->cblks); - } - } -} - if (band->data) { - jas_matrix_destroy(band->data); - } - if (band->prcs) { - jas_free(band->prcs); - } - } - if (rlvl->bands) { - jas_free(rlvl->bands); - } - } - if (tcomp->rlvls) { - jas_free(tcomp->rlvls); - } - if (tcomp->data) { - jas_matrix_destroy(tcomp->data); - } - if (tcomp->tsfb) { - jpc_tsfb_destroy(tcomp->tsfb); - } - } -} - if (tile->cp) { - jpc_dec_cp_destroy(tile->cp); - tile->cp = 0; - } - if (tile->tcomps) { - jas_free(tile->tcomps); - tile->tcomps = 0; - } - if (tile->pi) { - jpc_pi_destroy(tile->pi); - tile->pi = 0; - } - if (tile->pkthdrstream) { - jas_stream_close(tile->pkthdrstream); - tile->pkthdrstream = 0; - } - if (tile->pptstab) { - jpc_ppxstab_destroy(tile->pptstab); - tile->pptstab = 0; - } - - tile->state = JPC_TILE_DONE; - - return 0; + continue; +} + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; ++cblkno, ++cblk) { + + while (cblk->segs.head) { + seg = cblk->segs.head; + jpc_seglist_remove(&cblk->segs, seg); + jpc_seg_destroy(seg); + } + jas_matrix_destroy(cblk->data); + if (cblk->mqdec) { + jpc_mqdec_destroy(cblk->mqdec); + } + if (cblk->nulldec) { + jpc_bitstream_close(cblk->nulldec); + } + if (cblk->flags) { + jas_matrix_destroy(cblk->flags); + } + } + if (prc->incltagtree) { + jpc_tagtree_destroy(prc->incltagtree); + } + if (prc->numimsbstagtree) { + jpc_tagtree_destroy(prc->numimsbstagtree); + } + if (prc->cblks) { + jas_free(prc->cblks); + } + } +} + if (band->data) { + jas_matrix_destroy(band->data); + } + if (band->prcs) { + jas_free(band->prcs); + } + } + if (rlvl->bands) { + jas_free(rlvl->bands); + } + } + if (tcomp->rlvls) { + jas_free(tcomp->rlvls); + } + if (tcomp->data) { + jas_matrix_destroy(tcomp->data); + } + if (tcomp->tsfb) { + jpc_tsfb_destroy(tcomp->tsfb); + } + } +} + if (tile->cp) { + jpc_dec_cp_destroy(tile->cp); + tile->cp = 0; + } + if (tile->tcomps) { + jas_free(tile->tcomps); + tile->tcomps = 0; + } + if (tile->pi) { + jpc_pi_destroy(tile->pi); + tile->pi = 0; + } + if (tile->pkthdrstream) { + jas_stream_close(tile->pkthdrstream); + tile->pkthdrstream = 0; + } + if (tile->pptstab) { + jpc_ppxstab_destroy(tile->pptstab); + tile->pptstab = 0; + } + + tile->state = JPC_TILE_DONE; + + return 0; } static int jpc_dec_tiledecode(jpc_dec_t *dec, jpc_dec_tile_t *tile) { - int i; - int j; - jpc_dec_tcomp_t *tcomp; - jpc_dec_rlvl_t *rlvl; - jpc_dec_band_t *band; - int compno; - int rlvlno; - int bandno; - int adjust; - int v; - jpc_dec_ccp_t *ccp; - jpc_dec_cmpt_t *cmpt; - - if (jpc_dec_decodecblks(dec, tile)) { - fprintf(stderr, "jpc_dec_decodecblks failed\n"); - return -1; - } - - /* Perform dequantization. */ - for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; - ++compno, ++tcomp) { - ccp = &tile->cp->ccps[compno]; - for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; - ++rlvlno, ++rlvl) { - if (!rlvl->bands) { - continue; - } - for (bandno = 0, band = rlvl->bands; - bandno < rlvl->numbands; ++bandno, ++band) { - if (!band->data) { - continue; - } - jpc_undo_roi(band->data, band->roishift, ccp->roishift - - band->roishift, band->numbps); - if (tile->realmode) { - jas_matrix_asl(band->data, JPC_FIX_FRACBITS); - jpc_dequantize(band->data, band->absstepsize); - } - - } - } - } - - /* Apply an inverse wavelet transform if necessary. */ - for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; - ++compno, ++tcomp) { - ccp = &tile->cp->ccps[compno]; - jpc_tsfb_synthesize(tcomp->tsfb, ((ccp->qmfbid == - JPC_COX_RFT) ? JPC_TSFB_RITIMODE : 0), tcomp->data); - } - - - /* Apply an inverse intercomponent transform if necessary. */ - switch (tile->cp->mctid) { - case JPC_MCT_RCT: - assert(dec->numcomps == 3); - jpc_irct(tile->tcomps[0].data, tile->tcomps[1].data, - tile->tcomps[2].data); - break; - case JPC_MCT_ICT: - assert(dec->numcomps == 3); - jpc_iict(tile->tcomps[0].data, tile->tcomps[1].data, - tile->tcomps[2].data); - break; - } - - /* Perform rounding and convert to integer values. */ - if (tile->realmode) { - for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; - ++compno, ++tcomp) { - for (i = 0; i < jas_matrix_numrows(tcomp->data); ++i) { - for (j = 0; j < jas_matrix_numcols(tcomp->data); ++j) { - v = jas_matrix_get(tcomp->data, i, j); - v = jpc_fix_round(v); - jas_matrix_set(tcomp->data, i, j, jpc_fixtoint(v)); - } - } - } - } - - /* Perform level shift. */ - for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < - dec->numcomps; ++compno, ++tcomp, ++cmpt) { - adjust = cmpt->sgnd ? 0 : (1 << (cmpt->prec - 1)); - for (i = 0; i < jas_matrix_numrows(tcomp->data); ++i) { - for (j = 0; j < jas_matrix_numcols(tcomp->data); ++j) { - *jas_matrix_getref(tcomp->data, i, j) += adjust; - } - } - } - - /* Perform clipping. */ - for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < - dec->numcomps; ++compno, ++tcomp, ++cmpt) { - jpc_fix_t mn; - jpc_fix_t mx; - mn = cmpt->sgnd ? (-(1 << (cmpt->prec - 1))) : (0); - mx = cmpt->sgnd ? ((1 << (cmpt->prec - 1)) - 1) : ((1 << - cmpt->prec) - 1); - jas_matrix_clip(tcomp->data, mn, mx); - } - - /* XXX need to free tsfb struct */ - - /* Write the data for each component of the image. */ - for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < - dec->numcomps; ++compno, ++tcomp, ++cmpt) { - if (jas_image_writecmpt(dec->image, compno, tcomp->xstart - - JPC_CEILDIV(dec->xstart, cmpt->hstep), tcomp->ystart - - JPC_CEILDIV(dec->ystart, cmpt->vstep), jas_matrix_numcols( - tcomp->data), jas_matrix_numrows(tcomp->data), tcomp->data)) { - fprintf(stderr, "write component failed\n"); - return -4; - } - } - - return 0; + int i; + int j; + jpc_dec_tcomp_t *tcomp; + jpc_dec_rlvl_t *rlvl; + jpc_dec_band_t *band; + int compno; + int rlvlno; + int bandno; + int adjust; + int v; + jpc_dec_ccp_t *ccp; + jpc_dec_cmpt_t *cmpt; + + if (jpc_dec_decodecblks(dec, tile)) { + fprintf(stderr, "jpc_dec_decodecblks failed\n"); + return -1; + } + + /* Perform dequantization. */ + for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; + ++compno, ++tcomp) { + ccp = &tile->cp->ccps[compno]; + for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; + ++rlvlno, ++rlvl) { + if (!rlvl->bands) { + continue; + } + for (bandno = 0, band = rlvl->bands; + bandno < rlvl->numbands; ++bandno, ++band) { + if (!band->data) { + continue; + } + jpc_undo_roi(band->data, band->roishift, ccp->roishift - + band->roishift, band->numbps); + if (tile->realmode) { + jas_matrix_asl(band->data, JPC_FIX_FRACBITS); + jpc_dequantize(band->data, band->absstepsize); + } + + } + } + } + + /* Apply an inverse wavelet transform if necessary. */ + for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; + ++compno, ++tcomp) { + ccp = &tile->cp->ccps[compno]; + jpc_tsfb_synthesize(tcomp->tsfb, ((ccp->qmfbid == + JPC_COX_RFT) ? JPC_TSFB_RITIMODE : 0), tcomp->data); + } + + + /* Apply an inverse intercomponent transform if necessary. */ + switch (tile->cp->mctid) { + case JPC_MCT_RCT: + assert(dec->numcomps == 3); + jpc_irct(tile->tcomps[0].data, tile->tcomps[1].data, + tile->tcomps[2].data); + break; + case JPC_MCT_ICT: + assert(dec->numcomps == 3); + jpc_iict(tile->tcomps[0].data, tile->tcomps[1].data, + tile->tcomps[2].data); + break; + } + + /* Perform rounding and convert to integer values. */ + if (tile->realmode) { + for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; + ++compno, ++tcomp) { + for (i = 0; i < jas_matrix_numrows(tcomp->data); ++i) { + for (j = 0; j < jas_matrix_numcols(tcomp->data); ++j) { + v = jas_matrix_get(tcomp->data, i, j); + v = jpc_fix_round(v); + jas_matrix_set(tcomp->data, i, j, jpc_fixtoint(v)); + } + } + } + } + + /* Perform level shift. */ + for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < + dec->numcomps; ++compno, ++tcomp, ++cmpt) { + adjust = cmpt->sgnd ? 0 : (1 << (cmpt->prec - 1)); + for (i = 0; i < jas_matrix_numrows(tcomp->data); ++i) { + for (j = 0; j < jas_matrix_numcols(tcomp->data); ++j) { + *jas_matrix_getref(tcomp->data, i, j) += adjust; + } + } + } + + /* Perform clipping. */ + for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < + dec->numcomps; ++compno, ++tcomp, ++cmpt) { + jpc_fix_t mn; + jpc_fix_t mx; + mn = cmpt->sgnd ? (-(1 << (cmpt->prec - 1))) : (0); + mx = cmpt->sgnd ? ((1 << (cmpt->prec - 1)) - 1) : ((1 << + cmpt->prec) - 1); + jas_matrix_clip(tcomp->data, mn, mx); + } + + /* XXX need to free tsfb struct */ + + /* Write the data for each component of the image. */ + for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < + dec->numcomps; ++compno, ++tcomp, ++cmpt) { + if (jas_image_writecmpt(dec->image, compno, tcomp->xstart - + JPC_CEILDIV(dec->xstart, cmpt->hstep), tcomp->ystart - + JPC_CEILDIV(dec->ystart, cmpt->vstep), jas_matrix_numcols( + tcomp->data), jas_matrix_numrows(tcomp->data), tcomp->data)) { + fprintf(stderr, "write component failed\n"); + return -4; + } + } + + return 0; } static int jpc_dec_process_eoc(jpc_dec_t *dec, jpc_ms_t *ms) { - int tileno; - jpc_dec_tile_t *tile; - for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno, - ++tile) { - if (tile->state == JPC_TILE_ACTIVE) { - if (jpc_dec_tiledecode(dec, tile)) { - return -1; - } - } - jpc_dec_tilefini(dec, tile); - } + int tileno; + jpc_dec_tile_t *tile; + for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno, + ++tile) { + if (tile->state == JPC_TILE_ACTIVE) { + if (jpc_dec_tiledecode(dec, tile)) { + return -1; + } + } + jpc_dec_tilefini(dec, tile); + } - /* We are done processing the code stream. */ - dec->state = JPC_MT; + /* We are done processing the code stream. */ + dec->state = JPC_MT; - return 1; + return 1; } static int jpc_dec_process_siz(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_siz_t *siz = &ms->parms.siz; - uint_fast16_t compno; - uint_fast32_t tileno; - jpc_dec_tile_t *tile; - jpc_dec_tcomp_t *tcomp; - uint_fast32_t htileno; - uint_fast32_t vtileno; - jpc_dec_cmpt_t *cmpt; - - dec->xstart = siz->xoff; - dec->ystart = siz->yoff; - dec->xend = siz->width; - dec->yend = siz->height; - dec->tilewidth = siz->tilewidth; - dec->tileheight = siz->tileheight; - dec->tilexoff = siz->tilexoff; - dec->tileyoff = siz->tileyoff; - dec->numcomps = siz->numcomps; - if (!(dec->cp = jpc_dec_cp_create(dec->numcomps))) { - return -1; - } - - if (!(dec->cmpts = jas_malloc(dec->numcomps * sizeof(jpc_dec_cmpt_t)))) { - return -1; - } - - for (compno = 0, cmpt = dec->cmpts; compno < dec->numcomps; ++compno, - ++cmpt) { - cmpt->prec = siz->comps[compno].prec; - cmpt->sgnd = siz->comps[compno].sgnd; - cmpt->hstep = siz->comps[compno].hsamp; - cmpt->vstep = siz->comps[compno].vsamp; - cmpt->width = JPC_CEILDIV(dec->xend, cmpt->hstep) - - JPC_CEILDIV(dec->xstart, cmpt->hstep); - cmpt->height = JPC_CEILDIV(dec->yend, cmpt->vstep) - - JPC_CEILDIV(dec->ystart, cmpt->vstep); - cmpt->hsubstep = 0; - cmpt->vsubstep = 0; - } - - dec->image = 0; - - dec->numhtiles = JPC_CEILDIV(dec->xend - dec->tilexoff, dec->tilewidth); - dec->numvtiles = JPC_CEILDIV(dec->yend - dec->tileyoff, dec->tileheight); - dec->numtiles = dec->numhtiles * dec->numvtiles; - if (!(dec->tiles = jas_malloc(dec->numtiles * sizeof(jpc_dec_tile_t)))) { - return -1; - } - - for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno, - ++tile) { - htileno = tileno % dec->numhtiles; - vtileno = tileno / dec->numhtiles; - tile->realmode = 0; - tile->state = JPC_TILE_INIT; - tile->xstart = JAS_MAX(dec->tilexoff + htileno * dec->tilewidth, - dec->xstart); - tile->ystart = JAS_MAX(dec->tileyoff + vtileno * dec->tileheight, - dec->ystart); - tile->xend = JAS_MIN(dec->tilexoff + (htileno + 1) * - dec->tilewidth, dec->xend); - tile->yend = JAS_MIN(dec->tileyoff + (vtileno + 1) * - dec->tileheight, dec->yend); - tile->numparts = 0; - tile->partno = 0; - tile->pkthdrstream = 0; - tile->pkthdrstreampos = 0; - tile->pptstab = 0; - tile->cp = 0; - if (!(tile->tcomps = jas_malloc(dec->numcomps * - sizeof(jpc_dec_tcomp_t)))) { - return -1; - } - for (compno = 0, cmpt = dec->cmpts, tcomp = tile->tcomps; - compno < dec->numcomps; ++compno, ++cmpt, ++tcomp) { - tcomp->rlvls = 0; - tcomp->data = 0; - tcomp->xstart = JPC_CEILDIV(tile->xstart, cmpt->hstep); - tcomp->ystart = JPC_CEILDIV(tile->ystart, cmpt->vstep); - tcomp->xend = JPC_CEILDIV(tile->xend, cmpt->hstep); - tcomp->yend = JPC_CEILDIV(tile->yend, cmpt->vstep); - tcomp->tsfb = 0; - } - } - - dec->pkthdrstreams = 0; - - /* We should expect to encounter other main header marker segments - or an SOT marker segment next. */ - dec->state = JPC_MH; - - return 0; + jpc_siz_t *siz = &ms->parms.siz; + uint_fast16_t compno; + uint_fast32_t tileno; + jpc_dec_tile_t *tile; + jpc_dec_tcomp_t *tcomp; + uint_fast32_t htileno; + uint_fast32_t vtileno; + jpc_dec_cmpt_t *cmpt; + + dec->xstart = siz->xoff; + dec->ystart = siz->yoff; + dec->xend = siz->width; + dec->yend = siz->height; + dec->tilewidth = siz->tilewidth; + dec->tileheight = siz->tileheight; + dec->tilexoff = siz->tilexoff; + dec->tileyoff = siz->tileyoff; + dec->numcomps = siz->numcomps; + if (!(dec->cp = jpc_dec_cp_create(dec->numcomps))) { + return -1; + } + + if (!(dec->cmpts = jas_malloc(dec->numcomps * sizeof(jpc_dec_cmpt_t)))) { + return -1; + } + + for (compno = 0, cmpt = dec->cmpts; compno < dec->numcomps; ++compno, + ++cmpt) { + cmpt->prec = siz->comps[compno].prec; + cmpt->sgnd = siz->comps[compno].sgnd; + cmpt->hstep = siz->comps[compno].hsamp; + cmpt->vstep = siz->comps[compno].vsamp; + cmpt->width = JPC_CEILDIV(dec->xend, cmpt->hstep) - + JPC_CEILDIV(dec->xstart, cmpt->hstep); + cmpt->height = JPC_CEILDIV(dec->yend, cmpt->vstep) - + JPC_CEILDIV(dec->ystart, cmpt->vstep); + cmpt->hsubstep = 0; + cmpt->vsubstep = 0; + } + + dec->image = 0; + + dec->numhtiles = JPC_CEILDIV(dec->xend - dec->tilexoff, dec->tilewidth); + dec->numvtiles = JPC_CEILDIV(dec->yend - dec->tileyoff, dec->tileheight); + dec->numtiles = dec->numhtiles * dec->numvtiles; + if (!(dec->tiles = jas_malloc(dec->numtiles * sizeof(jpc_dec_tile_t)))) { + return -1; + } + + for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno, + ++tile) { + htileno = tileno % dec->numhtiles; + vtileno = tileno / dec->numhtiles; + tile->realmode = 0; + tile->state = JPC_TILE_INIT; + tile->xstart = JAS_MAX(dec->tilexoff + htileno * dec->tilewidth, + dec->xstart); + tile->ystart = JAS_MAX(dec->tileyoff + vtileno * dec->tileheight, + dec->ystart); + tile->xend = JAS_MIN(dec->tilexoff + (htileno + 1) * + dec->tilewidth, dec->xend); + tile->yend = JAS_MIN(dec->tileyoff + (vtileno + 1) * + dec->tileheight, dec->yend); + tile->numparts = 0; + tile->partno = 0; + tile->pkthdrstream = 0; + tile->pkthdrstreampos = 0; + tile->pptstab = 0; + tile->cp = 0; + if (!(tile->tcomps = jas_malloc(dec->numcomps * + sizeof(jpc_dec_tcomp_t)))) { + return -1; + } + for (compno = 0, cmpt = dec->cmpts, tcomp = tile->tcomps; + compno < dec->numcomps; ++compno, ++cmpt, ++tcomp) { + tcomp->rlvls = 0; + tcomp->data = 0; + tcomp->xstart = JPC_CEILDIV(tile->xstart, cmpt->hstep); + tcomp->ystart = JPC_CEILDIV(tile->ystart, cmpt->vstep); + tcomp->xend = JPC_CEILDIV(tile->xend, cmpt->hstep); + tcomp->yend = JPC_CEILDIV(tile->yend, cmpt->vstep); + tcomp->tsfb = 0; + } + } + + dec->pkthdrstreams = 0; + + /* We should expect to encounter other main header marker segments + or an SOT marker segment next. */ + dec->state = JPC_MH; + + return 0; } static int jpc_dec_process_cod(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_cod_t *cod = &ms->parms.cod; - jpc_dec_tile_t *tile; - - switch (dec->state) { - case JPC_MH: - jpc_dec_cp_setfromcod(dec->cp, cod); - break; - case JPC_TPH: - if (!(tile = dec->curtile)) { - return -1; - } - if (tile->partno != 0) { - return -1; - } - jpc_dec_cp_setfromcod(tile->cp, cod); - break; - } - return 0; + jpc_cod_t *cod = &ms->parms.cod; + jpc_dec_tile_t *tile; + + switch (dec->state) { + case JPC_MH: + jpc_dec_cp_setfromcod(dec->cp, cod); + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (tile->partno != 0) { + return -1; + } + jpc_dec_cp_setfromcod(tile->cp, cod); + break; + } + return 0; } static int jpc_dec_process_coc(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_coc_t *coc = &ms->parms.coc; - jpc_dec_tile_t *tile; - - if (coc->compno > dec->numcomps) { - fprintf(stderr, - "invalid component number in COC marker segment\n"); - return -1; - } - switch (dec->state) { - case JPC_MH: - jpc_dec_cp_setfromcoc(dec->cp, coc); - break; - case JPC_TPH: - if (!(tile = dec->curtile)) { - return -1; - } - if (tile->partno > 0) { - return -1; - } - jpc_dec_cp_setfromcoc(tile->cp, coc); - break; - } - return 0; + jpc_coc_t *coc = &ms->parms.coc; + jpc_dec_tile_t *tile; + + if (coc->compno > dec->numcomps) { + fprintf(stderr, + "invalid component number in COC marker segment\n"); + return -1; + } + switch (dec->state) { + case JPC_MH: + jpc_dec_cp_setfromcoc(dec->cp, coc); + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (tile->partno > 0) { + return -1; + } + jpc_dec_cp_setfromcoc(tile->cp, coc); + break; + } + return 0; } static int jpc_dec_process_rgn(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_rgn_t *rgn = &ms->parms.rgn; - jpc_dec_tile_t *tile; - - if (rgn->compno > dec->numcomps) { - fprintf(stderr, - "invalid component number in RGN marker segment\n"); - return -1; - } - switch (dec->state) { - case JPC_MH: - jpc_dec_cp_setfromrgn(dec->cp, rgn); - break; - case JPC_TPH: - if (!(tile = dec->curtile)) { - return -1; - } - if (tile->partno > 0) { - return -1; - } - jpc_dec_cp_setfromrgn(tile->cp, rgn); - break; - } - - return 0; + jpc_rgn_t *rgn = &ms->parms.rgn; + jpc_dec_tile_t *tile; + + if (rgn->compno > dec->numcomps) { + fprintf(stderr, + "invalid component number in RGN marker segment\n"); + return -1; + } + switch (dec->state) { + case JPC_MH: + jpc_dec_cp_setfromrgn(dec->cp, rgn); + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (tile->partno > 0) { + return -1; + } + jpc_dec_cp_setfromrgn(tile->cp, rgn); + break; + } + + return 0; } static int jpc_dec_process_qcd(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_qcd_t *qcd = &ms->parms.qcd; - jpc_dec_tile_t *tile; - - switch (dec->state) { - case JPC_MH: - jpc_dec_cp_setfromqcd(dec->cp, qcd); - break; - case JPC_TPH: - if (!(tile = dec->curtile)) { - return -1; - } - if (tile->partno > 0) { - return -1; - } - jpc_dec_cp_setfromqcd(tile->cp, qcd); - break; - } - return 0; + jpc_qcd_t *qcd = &ms->parms.qcd; + jpc_dec_tile_t *tile; + + switch (dec->state) { + case JPC_MH: + jpc_dec_cp_setfromqcd(dec->cp, qcd); + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (tile->partno > 0) { + return -1; + } + jpc_dec_cp_setfromqcd(tile->cp, qcd); + break; + } + return 0; } static int jpc_dec_process_qcc(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_qcc_t *qcc = &ms->parms.qcc; - jpc_dec_tile_t *tile; - - if (qcc->compno > dec->numcomps) { - fprintf(stderr, - "invalid component number in QCC marker segment\n"); - return -1; - } - switch (dec->state) { - case JPC_MH: - jpc_dec_cp_setfromqcc(dec->cp, qcc); - break; - case JPC_TPH: - if (!(tile = dec->curtile)) { - return -1; - } - if (tile->partno > 0) { - return -1; - } - jpc_dec_cp_setfromqcc(tile->cp, qcc); - break; - } - return 0; + jpc_qcc_t *qcc = &ms->parms.qcc; + jpc_dec_tile_t *tile; + + if (qcc->compno > dec->numcomps) { + fprintf(stderr, + "invalid component number in QCC marker segment\n"); + return -1; + } + switch (dec->state) { + case JPC_MH: + jpc_dec_cp_setfromqcc(dec->cp, qcc); + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (tile->partno > 0) { + return -1; + } + jpc_dec_cp_setfromqcc(tile->cp, qcc); + break; + } + return 0; } static int jpc_dec_process_poc(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_poc_t *poc = &ms->parms.poc; - jpc_dec_tile_t *tile; - switch (dec->state) { - case JPC_MH: - if (jpc_dec_cp_setfrompoc(dec->cp, poc, 1)) { - return -1; - } - break; - case JPC_TPH: - if (!(tile = dec->curtile)) { - return -1; - } - if (!tile->partno) { - if (jpc_dec_cp_setfrompoc(tile->cp, poc, (!tile->partno))) { - return -1; - } - } else { - jpc_pi_addpchgfrompoc(tile->pi, poc); - } - break; - } - return 0; + jpc_poc_t *poc = &ms->parms.poc; + jpc_dec_tile_t *tile; + switch (dec->state) { + case JPC_MH: + if (jpc_dec_cp_setfrompoc(dec->cp, poc, 1)) { + return -1; + } + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (!tile->partno) { + if (jpc_dec_cp_setfrompoc(tile->cp, poc, (!tile->partno))) { + return -1; + } + } else { + jpc_pi_addpchgfrompoc(tile->pi, poc); + } + break; + } + return 0; } static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_ppm_t *ppm = &ms->parms.ppm; - jpc_ppxstabent_t *ppmstabent; + jpc_ppm_t *ppm = &ms->parms.ppm; + jpc_ppxstabent_t *ppmstabent; - if (!dec->ppmstab) { - if (!(dec->ppmstab = jpc_ppxstab_create())) { - return -1; - } - } + if (!dec->ppmstab) { + if (!(dec->ppmstab = jpc_ppxstab_create())) { + return -1; + } + } - if (!(ppmstabent = jpc_ppxstabent_create())) { - return -1; - } - ppmstabent->ind = ppm->ind; - ppmstabent->data = ppm->data; - ppm->data = 0; - ppmstabent->len = ppm->len; - if (jpc_ppxstab_insert(dec->ppmstab, ppmstabent)) { - return -1; - } - return 0; + if (!(ppmstabent = jpc_ppxstabent_create())) { + return -1; + } + ppmstabent->ind = ppm->ind; + ppmstabent->data = ppm->data; + ppm->data = 0; + ppmstabent->len = ppm->len; + if (jpc_ppxstab_insert(dec->ppmstab, ppmstabent)) { + return -1; + } + return 0; } static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms) { - jpc_ppt_t *ppt = &ms->parms.ppt; - jpc_dec_tile_t *tile; - jpc_ppxstabent_t *pptstabent; - - tile = dec->curtile; - if (!tile->pptstab) { - if (!(tile->pptstab = jpc_ppxstab_create())) { - return -1; - } - } - if (!(pptstabent = jpc_ppxstabent_create())) { - return -1; - } - pptstabent->ind = ppt->ind; - pptstabent->data = ppt->data; - ppt->data = 0; - pptstabent->len = ppt->len; - if (jpc_ppxstab_insert(tile->pptstab, pptstabent)) { - return -1; - } - return 0; + jpc_ppt_t *ppt = &ms->parms.ppt; + jpc_dec_tile_t *tile; + jpc_ppxstabent_t *pptstabent; + + tile = dec->curtile; + if (!tile->pptstab) { + if (!(tile->pptstab = jpc_ppxstab_create())) { + return -1; + } + } + if (!(pptstabent = jpc_ppxstabent_create())) { + return -1; + } + pptstabent->ind = ppt->ind; + pptstabent->data = ppt->data; + ppt->data = 0; + pptstabent->len = ppt->len; + if (jpc_ppxstab_insert(tile->pptstab, pptstabent)) { + return -1; + } + return 0; } static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms) { - return 0; + return 0; } static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms) { - fprintf(stderr, "warning: ignoring unknown marker segment\n"); - jpc_ms_dump(ms, stderr); - return 0; + fprintf(stderr, "warning: ignoring unknown marker segment\n"); + jpc_ms_dump(ms, stderr); + return 0; } /******************************************************************************\ @@ -1512,426 +1512,426 @@ static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms) static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps) { - jpc_dec_cp_t *cp; - jpc_dec_ccp_t *ccp; - int compno; - - if (!(cp = jas_malloc(sizeof(jpc_dec_cp_t)))) { - return 0; - } - cp->flags = 0; - cp->numcomps = numcomps; - cp->prgord = 0; - cp->numlyrs = 0; - cp->mctid = 0; - cp->csty = 0; - if (!(cp->ccps = jas_malloc(cp->numcomps * sizeof(jpc_dec_ccp_t)))) { - return 0; - } - if (!(cp->pchglist = jpc_pchglist_create())) { - jas_free(cp->ccps); - return 0; - } - for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; - ++compno, ++ccp) { - ccp->flags = 0; - ccp->numrlvls = 0; - ccp->cblkwidthexpn = 0; - ccp->cblkheightexpn = 0; - ccp->qmfbid = 0; - ccp->numstepsizes = 0; - ccp->numguardbits = 0; - ccp->roishift = 0; - ccp->cblkctx = 0; - } - return cp; + jpc_dec_cp_t *cp; + jpc_dec_ccp_t *ccp; + int compno; + + if (!(cp = jas_malloc(sizeof(jpc_dec_cp_t)))) { + return 0; + } + cp->flags = 0; + cp->numcomps = numcomps; + cp->prgord = 0; + cp->numlyrs = 0; + cp->mctid = 0; + cp->csty = 0; + if (!(cp->ccps = jas_malloc(cp->numcomps * sizeof(jpc_dec_ccp_t)))) { + return 0; + } + if (!(cp->pchglist = jpc_pchglist_create())) { + jas_free(cp->ccps); + return 0; + } + for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; + ++compno, ++ccp) { + ccp->flags = 0; + ccp->numrlvls = 0; + ccp->cblkwidthexpn = 0; + ccp->cblkheightexpn = 0; + ccp->qmfbid = 0; + ccp->numstepsizes = 0; + ccp->numguardbits = 0; + ccp->roishift = 0; + ccp->cblkctx = 0; + } + return cp; } static jpc_dec_cp_t *jpc_dec_cp_copy(jpc_dec_cp_t *cp) { - jpc_dec_cp_t *newcp; - jpc_dec_ccp_t *newccp; - jpc_dec_ccp_t *ccp; - int compno; - - if (!(newcp = jpc_dec_cp_create(cp->numcomps))) { - return 0; - } - newcp->flags = cp->flags; - newcp->prgord = cp->prgord; - newcp->numlyrs = cp->numlyrs; - newcp->mctid = cp->mctid; - newcp->csty = cp->csty; - jpc_pchglist_destroy(newcp->pchglist); - newcp->pchglist = 0; - if (!(newcp->pchglist = jpc_pchglist_copy(cp->pchglist))) { - jas_free(newcp); - return 0; - } - for (compno = 0, newccp = newcp->ccps, ccp = cp->ccps; - compno < cp->numcomps; - ++compno, ++newccp, ++ccp) { - *newccp = *ccp; - } - return newcp; + jpc_dec_cp_t *newcp; + jpc_dec_ccp_t *newccp; + jpc_dec_ccp_t *ccp; + int compno; + + if (!(newcp = jpc_dec_cp_create(cp->numcomps))) { + return 0; + } + newcp->flags = cp->flags; + newcp->prgord = cp->prgord; + newcp->numlyrs = cp->numlyrs; + newcp->mctid = cp->mctid; + newcp->csty = cp->csty; + jpc_pchglist_destroy(newcp->pchglist); + newcp->pchglist = 0; + if (!(newcp->pchglist = jpc_pchglist_copy(cp->pchglist))) { + jas_free(newcp); + return 0; + } + for (compno = 0, newccp = newcp->ccps, ccp = cp->ccps; + compno < cp->numcomps; + ++compno, ++newccp, ++ccp) { + *newccp = *ccp; + } + return newcp; } static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp) { - int compno; - jpc_dec_ccp_t *ccp; - cp->flags &= (JPC_CSET | JPC_QSET); - for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; - ++compno, ++ccp) { - ccp->flags = 0; - } + int compno; + jpc_dec_ccp_t *ccp; + cp->flags &= (JPC_CSET | JPC_QSET); + for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; + ++compno, ++ccp) { + ccp->flags = 0; + } } static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp) { - if (cp->ccps) { - jas_free(cp->ccps); - } - if (cp->pchglist) { - jpc_pchglist_destroy(cp->pchglist); - } - jas_free(cp); + if (cp->ccps) { + jas_free(cp->ccps); + } + if (cp->pchglist) { + jpc_pchglist_destroy(cp->pchglist); + } + jas_free(cp); } static int jpc_dec_cp_isvalid(jpc_dec_cp_t *cp) { - uint_fast16_t compcnt; - jpc_dec_ccp_t *ccp; + uint_fast16_t compcnt; + jpc_dec_ccp_t *ccp; - if (!(cp->flags & JPC_CSET) || !(cp->flags & JPC_QSET)) { - return 0; - } - for (compcnt = cp->numcomps, ccp = cp->ccps; compcnt > 0; --compcnt, - ++ccp) { - /* Is there enough step sizes for the number of bands? */ - if ((ccp->qsty != JPC_QCX_SIQNT && ccp->numstepsizes < 3 * - ccp->numrlvls - 2) || (ccp->qsty == JPC_QCX_SIQNT && - ccp->numstepsizes != 1)) { - return 0; - } - } - return 1; + if (!(cp->flags & JPC_CSET) || !(cp->flags & JPC_QSET)) { + return 0; + } + for (compcnt = cp->numcomps, ccp = cp->ccps; compcnt > 0; --compcnt, + ++ccp) { + /* Is there enough step sizes for the number of bands? */ + if ((ccp->qsty != JPC_QCX_SIQNT && ccp->numstepsizes < 3 * + ccp->numrlvls - 2) || (ccp->qsty == JPC_QCX_SIQNT && + ccp->numstepsizes != 1)) { + return 0; + } + } + return 1; } static void calcstepsizes(uint_fast16_t refstepsize, int numrlvls, uint_fast16_t *stepsizes) { - int bandno; - int numbands; - uint_fast16_t expn; - uint_fast16_t mant; - expn = JPC_QCX_GETEXPN(refstepsize); - mant = JPC_QCX_GETMANT(refstepsize); - numbands = 3 * numrlvls - 2; - for (bandno = 0; bandno < numbands; ++bandno) { - stepsizes[bandno] = JPC_QCX_MANT(mant) | JPC_QCX_EXPN(expn + - (numrlvls - 1) - (numrlvls - 1 - ((bandno > 0) ? ((bandno + 2) / 3) : (0)))); - } + int bandno; + int numbands; + uint_fast16_t expn; + uint_fast16_t mant; + expn = JPC_QCX_GETEXPN(refstepsize); + mant = JPC_QCX_GETMANT(refstepsize); + numbands = 3 * numrlvls - 2; + for (bandno = 0; bandno < numbands; ++bandno) { + stepsizes[bandno] = JPC_QCX_MANT(mant) | JPC_QCX_EXPN(expn + + (numrlvls - 1) - (numrlvls - 1 - ((bandno > 0) ? ((bandno + 2) / 3) : (0)))); + } } static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp) { - jpc_dec_ccp_t *ccp; - int compno; - int i; - for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; - ++compno, ++ccp) { - if (!(ccp->csty & JPC_COX_PRT)) { - for (i = 0; i < JPC_MAXRLVLS; ++i) { - ccp->prcwidthexpns[i] = 15; - ccp->prcheightexpns[i] = 15; - } - } - if (ccp->qsty == JPC_QCX_SIQNT) { - calcstepsizes(ccp->stepsizes[0], ccp->numrlvls, ccp->stepsizes); - } - } - return 0; + jpc_dec_ccp_t *ccp; + int compno; + int i; + for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; + ++compno, ++ccp) { + if (!(ccp->csty & JPC_COX_PRT)) { + for (i = 0; i < JPC_MAXRLVLS; ++i) { + ccp->prcwidthexpns[i] = 15; + ccp->prcheightexpns[i] = 15; + } + } + if (ccp->qsty == JPC_QCX_SIQNT) { + calcstepsizes(ccp->stepsizes[0], ccp->numrlvls, ccp->stepsizes); + } + } + return 0; } static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, jpc_cod_t *cod) { - jpc_dec_ccp_t *ccp; - int compno; - cp->flags |= JPC_CSET; - cp->prgord = cod->prg; - if (cod->mctrans) { - cp->mctid = (cod->compparms.qmfbid == JPC_COX_INS) ? (JPC_MCT_ICT) : (JPC_MCT_RCT); - } else { - cp->mctid = JPC_MCT_NONE; - } - cp->numlyrs = cod->numlyrs; - cp->csty = cod->csty & (JPC_COD_SOP | JPC_COD_EPH); - for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; - ++compno, ++ccp) { - jpc_dec_cp_setfromcox(cp, ccp, &cod->compparms, 0); - } - cp->flags |= JPC_CSET; - return 0; + jpc_dec_ccp_t *ccp; + int compno; + cp->flags |= JPC_CSET; + cp->prgord = cod->prg; + if (cod->mctrans) { + cp->mctid = (cod->compparms.qmfbid == JPC_COX_INS) ? (JPC_MCT_ICT) : (JPC_MCT_RCT); + } else { + cp->mctid = JPC_MCT_NONE; + } + cp->numlyrs = cod->numlyrs; + cp->csty = cod->csty & (JPC_COD_SOP | JPC_COD_EPH); + for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; + ++compno, ++ccp) { + jpc_dec_cp_setfromcox(cp, ccp, &cod->compparms, 0); + } + cp->flags |= JPC_CSET; + return 0; } static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, jpc_coc_t *coc) { - jpc_dec_cp_setfromcox(cp, &cp->ccps[coc->compno], &coc->compparms, JPC_COC); - return 0; + jpc_dec_cp_setfromcox(cp, &cp->ccps[coc->compno], &coc->compparms, JPC_COC); + return 0; } static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, jpc_coxcp_t *compparms, int flags) { - int rlvlno; - if ((flags & JPC_COC) || !(ccp->flags & JPC_COC)) { - ccp->numrlvls = compparms->numdlvls + 1; - ccp->cblkwidthexpn = JPC_COX_GETCBLKSIZEEXPN( - compparms->cblkwidthval); - ccp->cblkheightexpn = JPC_COX_GETCBLKSIZEEXPN( - compparms->cblkheightval); - ccp->qmfbid = compparms->qmfbid; - ccp->cblkctx = compparms->cblksty; - ccp->csty = compparms->csty & JPC_COX_PRT; - for (rlvlno = 0; rlvlno < compparms->numrlvls; ++rlvlno) { - ccp->prcwidthexpns[rlvlno] = - compparms->rlvls[rlvlno].parwidthval; - ccp->prcheightexpns[rlvlno] = - compparms->rlvls[rlvlno].parheightval; - } - ccp->flags |= flags | JPC_CSET; - } - return 0; + int rlvlno; + if ((flags & JPC_COC) || !(ccp->flags & JPC_COC)) { + ccp->numrlvls = compparms->numdlvls + 1; + ccp->cblkwidthexpn = JPC_COX_GETCBLKSIZEEXPN( + compparms->cblkwidthval); + ccp->cblkheightexpn = JPC_COX_GETCBLKSIZEEXPN( + compparms->cblkheightval); + ccp->qmfbid = compparms->qmfbid; + ccp->cblkctx = compparms->cblksty; + ccp->csty = compparms->csty & JPC_COX_PRT; + for (rlvlno = 0; rlvlno < compparms->numrlvls; ++rlvlno) { + ccp->prcwidthexpns[rlvlno] = + compparms->rlvls[rlvlno].parwidthval; + ccp->prcheightexpns[rlvlno] = + compparms->rlvls[rlvlno].parheightval; + } + ccp->flags |= flags | JPC_CSET; + } + return 0; } static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, jpc_qcd_t *qcd) { - int compno; - jpc_dec_ccp_t *ccp; - for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; - ++compno, ++ccp) { - jpc_dec_cp_setfromqcx(cp, ccp, &qcd->compparms, 0); - } - cp->flags |= JPC_QSET; - return 0; + int compno; + jpc_dec_ccp_t *ccp; + for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; + ++compno, ++ccp) { + jpc_dec_cp_setfromqcx(cp, ccp, &qcd->compparms, 0); + } + cp->flags |= JPC_QSET; + return 0; } static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, jpc_qcc_t *qcc) { - return jpc_dec_cp_setfromqcx(cp, &cp->ccps[qcc->compno], &qcc->compparms, JPC_QCC); + return jpc_dec_cp_setfromqcx(cp, &cp->ccps[qcc->compno], &qcc->compparms, JPC_QCC); } static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, jpc_qcxcp_t *compparms, int flags) { - int bandno; - if ((flags & JPC_QCC) || !(ccp->flags & JPC_QCC)) { - ccp->flags |= flags | JPC_QSET; - for (bandno = 0; bandno < compparms->numstepsizes; ++bandno) { - ccp->stepsizes[bandno] = compparms->stepsizes[bandno]; - } - ccp->numstepsizes = compparms->numstepsizes; - ccp->numguardbits = compparms->numguard; - ccp->qsty = compparms->qntsty; - } - return 0; + int bandno; + if ((flags & JPC_QCC) || !(ccp->flags & JPC_QCC)) { + ccp->flags |= flags | JPC_QSET; + for (bandno = 0; bandno < compparms->numstepsizes; ++bandno) { + ccp->stepsizes[bandno] = compparms->stepsizes[bandno]; + } + ccp->numstepsizes = compparms->numstepsizes; + ccp->numguardbits = compparms->numguard; + ccp->qsty = compparms->qntsty; + } + return 0; } static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, jpc_rgn_t *rgn) { - jpc_dec_ccp_t *ccp; - ccp = &cp->ccps[rgn->compno]; - ccp->roishift = rgn->roishift; - return 0; + jpc_dec_ccp_t *ccp; + ccp = &cp->ccps[rgn->compno]; + ccp->roishift = rgn->roishift; + return 0; } static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, jpc_poc_t *poc) { - int pchgno; - jpc_pchg_t *pchg; - for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) { - if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) { - return -1; - } - if (jpc_pchglist_insert(pi->pchglist, -1, pchg)) { - return -1; - } - } - return 0; + int pchgno; + jpc_pchg_t *pchg; + for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) { + if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) { + return -1; + } + if (jpc_pchglist_insert(pi->pchglist, -1, pchg)) { + return -1; + } + } + return 0; } static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, jpc_poc_t *poc, int reset) { - int pchgno; - jpc_pchg_t *pchg; - if (reset) { - while (jpc_pchglist_numpchgs(cp->pchglist) > 0) { - pchg = jpc_pchglist_remove(cp->pchglist, 0); - jpc_pchg_destroy(pchg); - } - } - for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) { - if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) { - return -1; - } - if (jpc_pchglist_insert(cp->pchglist, -1, pchg)) { - return -1; - } - } - return 0; + int pchgno; + jpc_pchg_t *pchg; + if (reset) { + while (jpc_pchglist_numpchgs(cp->pchglist) > 0) { + pchg = jpc_pchglist_remove(cp->pchglist, 0); + jpc_pchg_destroy(pchg); + } + } + for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) { + if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) { + return -1; + } + if (jpc_pchglist_insert(cp->pchglist, -1, pchg)) { + return -1; + } + } + return 0; } static jpc_fix_t jpc_calcabsstepsize(int stepsize, int numbits) { - jpc_fix_t absstepsize; - int n; + jpc_fix_t absstepsize; + int n; - absstepsize = jpc_inttofix(1); - n = JPC_FIX_FRACBITS - 11; - absstepsize |= (n >= 0) ? (JPC_QCX_GETMANT(stepsize) << n) : - (JPC_QCX_GETMANT(stepsize) >> (-n)); - n = numbits - JPC_QCX_GETEXPN(stepsize); - absstepsize = (n >= 0) ? (absstepsize << n) : (absstepsize >> (-n)); - return absstepsize; + absstepsize = jpc_inttofix(1); + n = JPC_FIX_FRACBITS - 11; + absstepsize |= (n >= 0) ? (JPC_QCX_GETMANT(stepsize) << n) : + (JPC_QCX_GETMANT(stepsize) >> (-n)); + n = numbits - JPC_QCX_GETEXPN(stepsize); + absstepsize = (n >= 0) ? (absstepsize << n) : (absstepsize >> (-n)); + return absstepsize; } static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize) { - int i; - int j; - int t; + int i; + int j; + int t; - assert(absstepsize >= 0); - if (absstepsize == jpc_inttofix(1)) { - return; - } + assert(absstepsize >= 0); + if (absstepsize == jpc_inttofix(1)) { + return; + } - for (i = 0; i < jas_matrix_numrows(x); ++i) { - for (j = 0; j < jas_matrix_numcols(x); ++j) { - t = jas_matrix_get(x, i, j); - if (t) { - t = jpc_fix_mul(t, absstepsize); - } else { - t = 0; - } - jas_matrix_set(x, i, j, t); - } - } + for (i = 0; i < jas_matrix_numrows(x); ++i) { + for (j = 0; j < jas_matrix_numcols(x); ++j) { + t = jas_matrix_get(x, i, j); + if (t) { + t = jpc_fix_mul(t, absstepsize); + } else { + t = 0; + } + jas_matrix_set(x, i, j, t); + } + } } static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, int numbps) { - int i; - int j; - int thresh; - jpc_fix_t val; - jpc_fix_t mag; - bool warn; - uint_fast32_t mask; - - if (roishift == 0 && bgshift == 0) { - return; - } - thresh = 1 << roishift; - - warn = false; - for (i = 0; i < jas_matrix_numrows(x); ++i) { - for (j = 0; j < jas_matrix_numcols(x); ++j) { - val = jas_matrix_get(x, i, j); - mag = JAS_ABS(val); - if (mag >= thresh) { - /* We are dealing with ROI data. */ - mag >>= roishift; - val = (val < 0) ? (-mag) : mag; - jas_matrix_set(x, i, j, val); - } else { - /* We are dealing with non-ROI (i.e., background) data. */ - mag <<= bgshift; - mask = (1 << numbps) - 1; - /* Perform a basic sanity check on the sample value. */ - /* Some implementations write garbage in the unused - most-significant bit planes introduced by ROI shifting. - Here we ensure that any such bits are masked off. */ - if (mag & (~mask)) { - if (!warn) { - fprintf(stderr, - "warning: possibly corrupt code stream\n"); - warn = true; - } - mag &= mask; - } - val = (val < 0) ? (-mag) : mag; - jas_matrix_set(x, i, j, val); - } - } - } + int i; + int j; + int thresh; + jpc_fix_t val; + jpc_fix_t mag; + bool warn; + uint_fast32_t mask; + + if (roishift == 0 && bgshift == 0) { + return; + } + thresh = 1 << roishift; + + warn = false; + for (i = 0; i < jas_matrix_numrows(x); ++i) { + for (j = 0; j < jas_matrix_numcols(x); ++j) { + val = jas_matrix_get(x, i, j); + mag = JAS_ABS(val); + if (mag >= thresh) { + /* We are dealing with ROI data. */ + mag >>= roishift; + val = (val < 0) ? (-mag) : mag; + jas_matrix_set(x, i, j, val); + } else { + /* We are dealing with non-ROI (i.e., background) data. */ + mag <<= bgshift; + mask = (1 << numbps) - 1; + /* Perform a basic sanity check on the sample value. */ + /* Some implementations write garbage in the unused + most-significant bit planes introduced by ROI shifting. + Here we ensure that any such bits are masked off. */ + if (mag & (~mask)) { + if (!warn) { + fprintf(stderr, + "warning: possibly corrupt code stream\n"); + warn = true; + } + mag &= mask; + } + val = (val < 0) ? (-mag) : mag; + jas_matrix_set(x, i, j, val); + } + } + } } static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts, jas_stream_t *in) { - jpc_dec_t *dec; - - if (!(dec = jas_malloc(sizeof(jpc_dec_t)))) { - return 0; - } - - dec->image = 0; - dec->xstart = 0; - dec->ystart = 0; - dec->xend = 0; - dec->yend = 0; - dec->tilewidth = 0; - dec->tileheight = 0; - dec->tilexoff = 0; - dec->tileyoff = 0; - dec->numhtiles = 0; - dec->numvtiles = 0; - dec->numtiles = 0; - dec->tiles = 0; - dec->curtile = 0; - dec->numcomps = 0; - dec->in = in; - dec->cp = 0; - dec->maxlyrs = impopts->maxlyrs; - dec->maxpkts = impopts->maxpkts; + jpc_dec_t *dec; + + if (!(dec = jas_malloc(sizeof(jpc_dec_t)))) { + return 0; + } + + dec->image = 0; + dec->xstart = 0; + dec->ystart = 0; + dec->xend = 0; + dec->yend = 0; + dec->tilewidth = 0; + dec->tileheight = 0; + dec->tilexoff = 0; + dec->tileyoff = 0; + dec->numhtiles = 0; + dec->numvtiles = 0; + dec->numtiles = 0; + dec->tiles = 0; + dec->curtile = 0; + dec->numcomps = 0; + dec->in = in; + dec->cp = 0; + dec->maxlyrs = impopts->maxlyrs; + dec->maxpkts = impopts->maxpkts; dec->numpkts = 0; - dec->ppmseqno = 0; - dec->state = 0; - dec->cmpts = 0; - dec->pkthdrstreams = 0; - dec->ppmstab = 0; - dec->curtileendoff = 0; + dec->ppmseqno = 0; + dec->state = 0; + dec->cmpts = 0; + dec->pkthdrstreams = 0; + dec->ppmstab = 0; + dec->curtileendoff = 0; - return dec; + return dec; } static void jpc_dec_destroy(jpc_dec_t *dec) { - if (dec->cstate) { - jpc_cstate_destroy(dec->cstate); - } - if (dec->pkthdrstreams) { - jpc_streamlist_destroy(dec->pkthdrstreams); - } - if (dec->image) { - jas_image_destroy(dec->image); - } + if (dec->cstate) { + jpc_cstate_destroy(dec->cstate); + } + if (dec->pkthdrstreams) { + jpc_streamlist_destroy(dec->pkthdrstreams); + } + if (dec->image) { + jas_image_destroy(dec->image); + } - if (dec->cp) { - jpc_dec_cp_destroy(dec->cp); - } + if (dec->cp) { + jpc_dec_cp_destroy(dec->cp); + } - if (dec->cmpts) { - jas_free(dec->cmpts); - } + if (dec->cmpts) { + jas_free(dec->cmpts); + } - if (dec->tiles) { - jas_free(dec->tiles); - } + if (dec->tiles) { + jas_free(dec->tiles); + } - jas_free(dec); + jas_free(dec); } /******************************************************************************\ @@ -1940,395 +1940,411 @@ static void jpc_dec_destroy(jpc_dec_t *dec) void jpc_seglist_insert(jpc_dec_seglist_t *list, jpc_dec_seg_t *ins, jpc_dec_seg_t *node) { - jpc_dec_seg_t *prev; - jpc_dec_seg_t *next; - - prev = ins; - node->prev = prev; - next = prev ? (prev->next) : 0; - node->prev = prev; - node->next = next; - if (prev) { - prev->next = node; - } else { - list->head = node; - } - if (next) { - next->prev = node; - } else { - list->tail = node; - } + jpc_dec_seg_t *prev; + jpc_dec_seg_t *next; + + prev = ins; + node->prev = prev; + next = prev ? (prev->next) : 0; + node->prev = prev; + node->next = next; + if (prev) { + prev->next = node; + } else { + list->head = node; + } + if (next) { + next->prev = node; + } else { + list->tail = node; + } } void jpc_seglist_remove(jpc_dec_seglist_t *list, jpc_dec_seg_t *seg) { - jpc_dec_seg_t *prev; - jpc_dec_seg_t *next; - - prev = seg->prev; - next = seg->next; - if (prev) { - prev->next = next; - } else { - list->head = next; - } - if (next) { - next->prev = prev; - } else { - list->tail = prev; - } - seg->prev = 0; - seg->next = 0; + jpc_dec_seg_t *prev; + jpc_dec_seg_t *next; + + prev = seg->prev; + next = seg->next; + if (prev) { + prev->next = next; + } else { + list->head = next; + } + if (next) { + next->prev = prev; + } else { + list->tail = prev; + } + seg->prev = 0; + seg->next = 0; } jpc_dec_seg_t *jpc_seg_alloc(void) { - jpc_dec_seg_t *seg; + jpc_dec_seg_t *seg; - if (!(seg = jas_malloc(sizeof(jpc_dec_seg_t)))) { - return 0; - } - seg->prev = 0; - seg->next = 0; - seg->passno = -1; - seg->numpasses = 0; - seg->maxpasses = 0; - seg->type = JPC_SEG_INVALID; - seg->stream = 0; - seg->cnt = 0; - seg->complete = 0; - seg->lyrno = -1; - return seg; + if (!(seg = jas_malloc(sizeof(jpc_dec_seg_t)))) { + return 0; + } + seg->prev = 0; + seg->next = 0; + seg->passno = -1; + seg->numpasses = 0; + seg->maxpasses = 0; + seg->type = JPC_SEG_INVALID; + seg->stream = 0; + seg->cnt = 0; + seg->complete = 0; + seg->lyrno = -1; + return seg; } void jpc_seg_destroy(jpc_dec_seg_t *seg) { - if (seg->stream) { - jas_stream_close(seg->stream); - } - jas_free(seg); + if (seg->stream) { + jas_stream_close(seg->stream); + } + jas_free(seg); } static int jpc_dec_dump(jpc_dec_t *dec, FILE *out) { - jpc_dec_tile_t *tile; - int tileno; - jpc_dec_tcomp_t *tcomp; - uint_fast16_t compno; - jpc_dec_rlvl_t *rlvl; - int rlvlno; - jpc_dec_band_t *band; - int bandno; - jpc_dec_prc_t *prc; - int prcno; - jpc_dec_cblk_t *cblk; - int cblkno; - - for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; - ++tileno, ++tile) { - for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; - ++compno, ++tcomp) { - for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < - tcomp->numrlvls; ++rlvlno, ++rlvl) { -fprintf(out, "RESOLUTION LEVEL %d\n", rlvlno); -fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", - rlvl->xstart, rlvl->ystart, rlvl->xend, rlvl->yend, rlvl->xend - - rlvl->xstart, rlvl->yend - rlvl->ystart); - for (bandno = 0, band = rlvl->bands; - bandno < rlvl->numbands; ++bandno, ++band) { -fprintf(out, "BAND %d\n", bandno); -fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", - jas_seq2d_xstart(band->data), jas_seq2d_ystart(band->data), jas_seq2d_xend(band->data), - jas_seq2d_yend(band->data), jas_seq2d_xend(band->data) - jas_seq2d_xstart(band->data), - jas_seq2d_yend(band->data) - jas_seq2d_ystart(band->data)); - for (prcno = 0, prc = band->prcs; - prcno < rlvl->numprcs; ++prcno, - ++prc) { -fprintf(out, "CODE BLOCK GROUP %d\n", prcno); -fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", - prc->xstart, prc->ystart, prc->xend, prc->yend, prc->xend - - prc->xstart, prc->yend - prc->ystart); - for (cblkno = 0, cblk = - prc->cblks; cblkno < - prc->numcblks; ++cblkno, - ++cblk) { -fprintf(out, "CODE BLOCK %d\n", cblkno); -fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", - jas_seq2d_xstart(cblk->data), jas_seq2d_ystart(cblk->data), jas_seq2d_xend(cblk->data), - jas_seq2d_yend(cblk->data), jas_seq2d_xend(cblk->data) - jas_seq2d_xstart(cblk->data), - jas_seq2d_yend(cblk->data) - jas_seq2d_ystart(cblk->data)); - } - } - } - } - } - } - - return 0; + jpc_dec_tile_t *tile; + int tileno; + jpc_dec_tcomp_t *tcomp; + uint_fast16_t compno; + jpc_dec_rlvl_t *rlvl; + int rlvlno; + jpc_dec_band_t *band; + int bandno; + jpc_dec_prc_t *prc; + int prcno; + jpc_dec_cblk_t *cblk; + int cblkno; + + for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; + ++tileno, ++tile) { + for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; + ++compno, ++tcomp) { + for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < + tcomp->numrlvls; ++rlvlno, ++rlvl) { + fprintf(out, "RESOLUTION LEVEL %d\n", rlvlno); + fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, " + "w = %d, h = %d\n", + (int)rlvl->xstart, (int)rlvl->ystart, + (int)rlvl->xend, (int)rlvl->yend, + (int)(rlvl->xend - rlvl->xstart), + (int)(rlvl->yend - rlvl->ystart)); + for (bandno = 0, band = rlvl->bands; + bandno < rlvl->numbands; ++bandno, ++band) { + fprintf(out, "BAND %d\n", bandno); + fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, " + "w = %d, h = %d\n", + (int)jas_seq2d_xstart(band->data), + (int)jas_seq2d_ystart(band->data), + (int)jas_seq2d_xend(band->data), + (int)jas_seq2d_yend(band->data), + (int)(jas_seq2d_xend(band->data) - + jas_seq2d_xstart(band->data)), + (int)(jas_seq2d_yend(band->data) - + jas_seq2d_ystart(band->data))); + for (prcno = 0, prc = band->prcs; + prcno < rlvl->numprcs; + ++prcno, ++prc) { + fprintf(out, "CODE BLOCK GROUP %d\n", prcno); + fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, " + "w = %d, h = %d\n", + (int)prc->xstart, (int)prc->ystart, + (int)prc->xend, (int)prc->yend, + (int)(prc->xend - prc->xstart), + (int)(prc->yend - prc->ystart)); + for (cblkno = 0, cblk = prc->cblks; + cblkno < prc->numcblks; + ++cblkno, ++cblk) { + fprintf(out, "CODE BLOCK %d\n", cblkno); + fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, " + "w = %d, h = %d\n", + (int)jas_seq2d_xstart(cblk->data), + (int)jas_seq2d_ystart(cblk->data), + (int)jas_seq2d_xend(cblk->data), + (int)jas_seq2d_yend(cblk->data), + (int)(jas_seq2d_xend(cblk->data) - + jas_seq2d_xstart(cblk->data)), + (int)(jas_seq2d_yend(cblk->data) - + jas_seq2d_ystart(cblk->data))); + } + } + } + } + } + } + return 0; } jpc_streamlist_t *jpc_streamlist_create() { - jpc_streamlist_t *streamlist; - int i; - - if (!(streamlist = jas_malloc(sizeof(jpc_streamlist_t)))) { - return 0; - } - streamlist->numstreams = 0; - streamlist->maxstreams = 100; - if (!(streamlist->streams = jas_malloc(streamlist->maxstreams * - sizeof(jas_stream_t *)))) { - jas_free(streamlist); - return 0; - } - for (i = 0; i < streamlist->maxstreams; ++i) { - streamlist->streams[i] = 0; - } - return streamlist; + jpc_streamlist_t *streamlist; + int i; + + if (!(streamlist = jas_malloc(sizeof(jpc_streamlist_t)))) { + return 0; + } + streamlist->numstreams = 0; + streamlist->maxstreams = 100; + if (!(streamlist->streams = jas_malloc(streamlist->maxstreams * + sizeof(jas_stream_t *)))) { + jas_free(streamlist); + return 0; + } + for (i = 0; i < streamlist->maxstreams; ++i) { + streamlist->streams[i] = 0; + } + return streamlist; } int jpc_streamlist_insert(jpc_streamlist_t *streamlist, int streamno, jas_stream_t *stream) { - jas_stream_t **newstreams; - int newmaxstreams; - int i; - /* Grow the array of streams if necessary. */ - if (streamlist->numstreams >= streamlist->maxstreams) { - newmaxstreams = streamlist->maxstreams + 1024; - if (!(newstreams = jas_realloc(streamlist->streams, - (newmaxstreams + 1024) * sizeof(jas_stream_t *)))) { - return -1; - } - for (i = streamlist->numstreams; i < streamlist->maxstreams; ++i) { - streamlist->streams[i] = 0; - } - streamlist->maxstreams = newmaxstreams; - streamlist->streams = newstreams; - } - if (streamno != streamlist->numstreams) { - /* Can only handle insertion at start of list. */ - return -1; - } - streamlist->streams[streamno] = stream; - ++streamlist->numstreams; - return 0; + jas_stream_t **newstreams; + int newmaxstreams; + int i; + /* Grow the array of streams if necessary. */ + if (streamlist->numstreams >= streamlist->maxstreams) { + newmaxstreams = streamlist->maxstreams + 1024; + if (!(newstreams = jas_realloc(streamlist->streams, + (newmaxstreams + 1024) * sizeof(jas_stream_t *)))) { + return -1; + } + for (i = streamlist->numstreams; i < streamlist->maxstreams; ++i) { + streamlist->streams[i] = 0; + } + streamlist->maxstreams = newmaxstreams; + streamlist->streams = newstreams; + } + if (streamno != streamlist->numstreams) { + /* Can only handle insertion at start of list. */ + return -1; + } + streamlist->streams[streamno] = stream; + ++streamlist->numstreams; + return 0; } jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, int streamno) { - jas_stream_t *stream; - int i; - if (streamno >= streamlist->numstreams) { - abort(); - } - stream = streamlist->streams[streamno]; - for (i = streamno + 1; i < streamlist->numstreams; ++i) { - streamlist->streams[i - 1] = streamlist->streams[i]; - } - --streamlist->numstreams; - return stream; + jas_stream_t *stream; + int i; + if (streamno >= streamlist->numstreams) { + abort(); + } + stream = streamlist->streams[streamno]; + for (i = streamno + 1; i < streamlist->numstreams; ++i) { + streamlist->streams[i - 1] = streamlist->streams[i]; + } + --streamlist->numstreams; + return stream; } void jpc_streamlist_destroy(jpc_streamlist_t *streamlist) { - int streamno; - if (streamlist->streams) { - for (streamno = 0; streamno < streamlist->numstreams; - ++streamno) { - jas_stream_close(streamlist->streams[streamno]); - } - jas_free(streamlist->streams); - } - jas_free(streamlist); + int streamno; + if (streamlist->streams) { + for (streamno = 0; streamno < streamlist->numstreams; + ++streamno) { + jas_stream_close(streamlist->streams[streamno]); + } + jas_free(streamlist->streams); + } + jas_free(streamlist); } jas_stream_t *jpc_streamlist_get(jpc_streamlist_t *streamlist, int streamno) { - assert(streamno < streamlist->numstreams); - return streamlist->streams[streamno]; + assert(streamno < streamlist->numstreams); + return streamlist->streams[streamno]; } int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist) { - return streamlist->numstreams; + return streamlist->numstreams; } jpc_ppxstab_t *jpc_ppxstab_create() { - jpc_ppxstab_t *tab; + jpc_ppxstab_t *tab; - if (!(tab = jas_malloc(sizeof(jpc_ppxstab_t)))) { - return 0; - } - tab->numents = 0; - tab->maxents = 0; - tab->ents = 0; - return tab; + if (!(tab = jas_malloc(sizeof(jpc_ppxstab_t)))) { + return 0; + } + tab->numents = 0; + tab->maxents = 0; + tab->ents = 0; + return tab; } void jpc_ppxstab_destroy(jpc_ppxstab_t *tab) { - int i; - for (i = 0; i < tab->numents; ++i) { - jpc_ppxstabent_destroy(tab->ents[i]); - } - if (tab->ents) { - jas_free(tab->ents); - } - jas_free(tab); + int i; + for (i = 0; i < tab->numents; ++i) { + jpc_ppxstabent_destroy(tab->ents[i]); + } + if (tab->ents) { + jas_free(tab->ents); + } + jas_free(tab); } int jpc_ppxstab_grow(jpc_ppxstab_t *tab, int maxents) { - jpc_ppxstabent_t **newents; - if (tab->maxents < maxents) { - newents = (tab->ents) ? jas_realloc(tab->ents, maxents * - sizeof(jpc_ppxstabent_t *)) : jas_malloc(maxents * sizeof(jpc_ppxstabent_t *)); - if (!newents) { - return -1; - } - tab->ents = newents; - tab->maxents = maxents; - } - return 0; + jpc_ppxstabent_t **newents; + if (tab->maxents < maxents) { + newents = (tab->ents) ? jas_realloc(tab->ents, maxents * + sizeof(jpc_ppxstabent_t *)) : jas_malloc(maxents * sizeof(jpc_ppxstabent_t *)); + if (!newents) { + return -1; + } + tab->ents = newents; + tab->maxents = maxents; + } + return 0; } int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent) { - int inspt; - int i; + int inspt; + int i; - for (i = 0; i < tab->numents; ++i) { - if (tab->ents[i]->ind > ent->ind) { - break; - } - } - inspt = i; + for (i = 0; i < tab->numents; ++i) { + if (tab->ents[i]->ind > ent->ind) { + break; + } + } + inspt = i; - if (tab->numents >= tab->maxents) { - if (jpc_ppxstab_grow(tab, tab->maxents + 128)) { - return -1; - } - } + if (tab->numents >= tab->maxents) { + if (jpc_ppxstab_grow(tab, tab->maxents + 128)) { + return -1; + } + } - for (i = tab->numents; i > inspt; --i) { - tab->ents[i] = tab->ents[i - 1]; - } - tab->ents[i] = ent; - ++tab->numents; + for (i = tab->numents; i > inspt; --i) { + tab->ents[i] = tab->ents[i - 1]; + } + tab->ents[i] = ent; + ++tab->numents; - return 0; + return 0; } jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab) { - jpc_streamlist_t *streams; - unsigned char *dataptr; - uint_fast32_t datacnt; - uint_fast32_t tpcnt; - jpc_ppxstabent_t *ent; - int entno; - jas_stream_t *stream; - int n; - - if (!(streams = jpc_streamlist_create())) { - goto error; - } - - if (!tab->numents) { - return streams; - } - - entno = 0; - ent = tab->ents[entno]; - dataptr = ent->data; - datacnt = ent->len; - for (;;) { - - /* Get the length of the packet header data for the current - tile-part. */ - if (datacnt < 4) { - goto error; - } - if (!(stream = jas_stream_memopen(0, 0))) { - goto error; - } - if (jpc_streamlist_insert(streams, jpc_streamlist_numstreams(streams), - stream)) { - goto error; - } - tpcnt = (dataptr[0] << 24) | (dataptr[1] << 16) | (dataptr[2] << 8) - | dataptr[3]; - datacnt -= 4; - dataptr += 4; - - /* Get the packet header data for the current tile-part. */ - while (tpcnt) { - if (!datacnt) { - if (++entno >= tab->numents) { - goto error; - } - ent = tab->ents[entno]; - dataptr = ent->data; - datacnt = ent->len; - } - n = JAS_MIN(tpcnt, datacnt); - if (jas_stream_write(stream, dataptr, n) != n) { - goto error; - } - tpcnt -= n; - dataptr += n; - datacnt -= n; - } - jas_stream_rewind(stream); - if (!datacnt) { - if (++entno >= tab->numents) { - break; - } - ent = tab->ents[entno]; - dataptr = ent->data; - datacnt = ent->len; - } - } - - return streams; + jpc_streamlist_t *streams; + unsigned char *dataptr; + uint_fast32_t datacnt; + uint_fast32_t tpcnt; + jpc_ppxstabent_t *ent; + int entno; + jas_stream_t *stream; + int n; + + if (!(streams = jpc_streamlist_create())) { + goto error; + } + + if (!tab->numents) { + return streams; + } + + entno = 0; + ent = tab->ents[entno]; + dataptr = ent->data; + datacnt = ent->len; + for (;;) { + + /* Get the length of the packet header data for the current + tile-part. */ + if (datacnt < 4) { + goto error; + } + if (!(stream = jas_stream_memopen(0, 0))) { + goto error; + } + if (jpc_streamlist_insert(streams, jpc_streamlist_numstreams(streams), + stream)) { + goto error; + } + tpcnt = (dataptr[0] << 24) | (dataptr[1] << 16) | (dataptr[2] << 8) + | dataptr[3]; + datacnt -= 4; + dataptr += 4; + + /* Get the packet header data for the current tile-part. */ + while (tpcnt) { + if (!datacnt) { + if (++entno >= tab->numents) { + goto error; + } + ent = tab->ents[entno]; + dataptr = ent->data; + datacnt = ent->len; + } + n = JAS_MIN(tpcnt, datacnt); + if (jas_stream_write(stream, dataptr, n) != n) { + goto error; + } + tpcnt -= n; + dataptr += n; + datacnt -= n; + } + jas_stream_rewind(stream); + if (!datacnt) { + if (++entno >= tab->numents) { + break; + } + ent = tab->ents[entno]; + dataptr = ent->data; + datacnt = ent->len; + } + } + + return streams; error: - jpc_streamlist_destroy(streams); - return 0; + jpc_streamlist_destroy(streams); + return 0; } int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab) { - int i; - jpc_ppxstabent_t *ent; - for (i = 0; i < tab->numents; ++i) { - ent = tab->ents[i]; - if (jas_stream_write(out, ent->data, ent->len) != ent->len) { - return -1; - } - } - return 0; + int i; + jpc_ppxstabent_t *ent; + for (i = 0; i < tab->numents; ++i) { + ent = tab->ents[i]; + if (jas_stream_write(out, ent->data, ent->len) != ent->len) { + return -1; + } + } + return 0; } jpc_ppxstabent_t *jpc_ppxstabent_create() { - jpc_ppxstabent_t *ent; - if (!(ent = jas_malloc(sizeof(jpc_ppxstabent_t)))) { - return 0; - } - ent->data = 0; - ent->len = 0; - ent->ind = 0; - return ent; + jpc_ppxstabent_t *ent; + if (!(ent = jas_malloc(sizeof(jpc_ppxstabent_t)))) { + return 0; + } + ent->data = 0; + ent->len = 0; + ent->ind = 0; + return ent; } void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent) { - if (ent->data) { - jas_free(ent->data); - } - jas_free(ent); + if (ent->data) { + jas_free(ent->data); + } + jas_free(ent); } diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c b/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c index 3284dfeb..f868cab3 100644 --- a/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c +++ b/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c @@ -152,19 +152,19 @@ \******************************************************************************/ #define JPC_POW2(n) \ - (1 << (n)) + (1 << (n)) #define JPC_FLOORTOMULTPOW2(x, n) \ (((n) > 0) ? ((x) & (~((1 << n) - 1))) : (x)) /* Round to the nearest multiple of the specified power of two in the direction of negative infinity. */ -#define JPC_CEILTOMULTPOW2(x, n) \ +#define JPC_CEILTOMULTPOW2(x, n) \ (((n) > 0) ? JPC_FLOORTOMULTPOW2(((x) + (1 << (n)) - 1), n) : (x)) /* Round to the nearest multiple of the specified power of two in the direction of positive infinity. */ -#define JPC_POW2(n) \ +#define JPC_POW2(n) \ (1 << (n)) jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno); @@ -209,110 +209,110 @@ void jpc_enc_cp_destroy(jpc_enc_cp_t *cp); static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn) { - int p; - uint_fast32_t mant; - uint_fast32_t expn; - int n; - - if (absdelta < 0) { - abort(); - } - - p = jpc_firstone(absdelta) - JPC_FIX_FRACBITS; - n = 11 - jpc_firstone(absdelta); - mant = ((n < 0) ? (absdelta >> (-n)) : (absdelta << n)) & 0x7ff; - expn = scaleexpn - p; - if (scaleexpn < p) { - abort(); - } - return JPC_QCX_EXPN(expn) | JPC_QCX_MANT(mant); + int p; + uint_fast32_t mant; + uint_fast32_t expn; + int n; + + if (absdelta < 0) { + abort(); + } + + p = jpc_firstone(absdelta) - JPC_FIX_FRACBITS; + n = 11 - jpc_firstone(absdelta); + mant = ((n < 0) ? (absdelta >> (-n)) : (absdelta << n)) & 0x7ff; + expn = scaleexpn - p; + if (scaleexpn < p) { + abort(); + } + return JPC_QCX_EXPN(expn) | JPC_QCX_MANT(mant); } typedef enum { - OPT_DEBUG, - OPT_IMGAREAOFFX, - OPT_IMGAREAOFFY, - OPT_TILEGRDOFFX, - OPT_TILEGRDOFFY, - OPT_TILEWIDTH, - OPT_TILEHEIGHT, - OPT_PRCWIDTH, - OPT_PRCHEIGHT, - OPT_CBLKWIDTH, - OPT_CBLKHEIGHT, - OPT_MODE, - OPT_PRG, - OPT_NOMCT, - OPT_MAXRLVLS, - OPT_SOP, - OPT_EPH, - OPT_LAZY, - OPT_TERMALL, - OPT_SEGSYM, - OPT_VCAUSAL, - OPT_RESET, - OPT_PTERM, - OPT_NUMGBITS, - OPT_RATE, - OPT_ILYRRATES, - OPT_JP2OVERHEAD + OPT_DEBUG, + OPT_IMGAREAOFFX, + OPT_IMGAREAOFFY, + OPT_TILEGRDOFFX, + OPT_TILEGRDOFFY, + OPT_TILEWIDTH, + OPT_TILEHEIGHT, + OPT_PRCWIDTH, + OPT_PRCHEIGHT, + OPT_CBLKWIDTH, + OPT_CBLKHEIGHT, + OPT_MODE, + OPT_PRG, + OPT_NOMCT, + OPT_MAXRLVLS, + OPT_SOP, + OPT_EPH, + OPT_LAZY, + OPT_TERMALL, + OPT_SEGSYM, + OPT_VCAUSAL, + OPT_RESET, + OPT_PTERM, + OPT_NUMGBITS, + OPT_RATE, + OPT_ILYRRATES, + OPT_JP2OVERHEAD } optid_t; jas_taginfo_t encopts[] = { - {OPT_DEBUG, "debug"}, - {OPT_IMGAREAOFFX, "imgareatlx"}, - {OPT_IMGAREAOFFY, "imgareatly"}, - {OPT_TILEGRDOFFX, "tilegrdtlx"}, - {OPT_TILEGRDOFFY, "tilegrdtly"}, - {OPT_TILEWIDTH, "tilewidth"}, - {OPT_TILEHEIGHT, "tileheight"}, - {OPT_PRCWIDTH, "prcwidth"}, - {OPT_PRCHEIGHT, "prcheight"}, - {OPT_CBLKWIDTH, "cblkwidth"}, - {OPT_CBLKHEIGHT, "cblkheight"}, - {OPT_MODE, "mode"}, - {OPT_PRG, "prg"}, - {OPT_NOMCT, "nomct"}, - {OPT_MAXRLVLS, "numrlvls"}, - {OPT_SOP, "sop"}, - {OPT_EPH, "eph"}, - {OPT_LAZY, "lazy"}, - {OPT_TERMALL, "termall"}, - {OPT_SEGSYM, "segsym"}, - {OPT_VCAUSAL, "vcausal"}, - {OPT_PTERM, "pterm"}, - {OPT_RESET, "resetprob"}, - {OPT_NUMGBITS, "numgbits"}, - {OPT_RATE, "rate"}, - {OPT_ILYRRATES, "ilyrrates"}, - {OPT_JP2OVERHEAD, "_jp2overhead"}, - {-1, 0} + {OPT_DEBUG, "debug"}, + {OPT_IMGAREAOFFX, "imgareatlx"}, + {OPT_IMGAREAOFFY, "imgareatly"}, + {OPT_TILEGRDOFFX, "tilegrdtlx"}, + {OPT_TILEGRDOFFY, "tilegrdtly"}, + {OPT_TILEWIDTH, "tilewidth"}, + {OPT_TILEHEIGHT, "tileheight"}, + {OPT_PRCWIDTH, "prcwidth"}, + {OPT_PRCHEIGHT, "prcheight"}, + {OPT_CBLKWIDTH, "cblkwidth"}, + {OPT_CBLKHEIGHT, "cblkheight"}, + {OPT_MODE, "mode"}, + {OPT_PRG, "prg"}, + {OPT_NOMCT, "nomct"}, + {OPT_MAXRLVLS, "numrlvls"}, + {OPT_SOP, "sop"}, + {OPT_EPH, "eph"}, + {OPT_LAZY, "lazy"}, + {OPT_TERMALL, "termall"}, + {OPT_SEGSYM, "segsym"}, + {OPT_VCAUSAL, "vcausal"}, + {OPT_PTERM, "pterm"}, + {OPT_RESET, "resetprob"}, + {OPT_NUMGBITS, "numgbits"}, + {OPT_RATE, "rate"}, + {OPT_ILYRRATES, "ilyrrates"}, + {OPT_JP2OVERHEAD, "_jp2overhead"}, + {-1, 0} }; typedef enum { - PO_L = 0, - PO_R + PO_L = 0, + PO_R } poid_t; jas_taginfo_t prgordtab[] = { - {JPC_COD_LRCPPRG, "lrcp"}, - {JPC_COD_RLCPPRG, "rlcp"}, - {JPC_COD_RPCLPRG, "rpcl"}, - {JPC_COD_PCRLPRG, "pcrl"}, - {JPC_COD_CPRLPRG, "cprl"}, - {-1, 0} + {JPC_COD_LRCPPRG, "lrcp"}, + {JPC_COD_RLCPPRG, "rlcp"}, + {JPC_COD_RPCLPRG, "rpcl"}, + {JPC_COD_PCRLPRG, "pcrl"}, + {JPC_COD_CPRLPRG, "cprl"}, + {-1, 0} }; typedef enum { - MODE_INT, - MODE_REAL + MODE_INT, + MODE_REAL } modeid_t; jas_taginfo_t modetab[] = { - {MODE_INT, "int"}, - {MODE_REAL, "real"}, - {-1, 0} + {MODE_INT, "int"}, + {MODE_REAL, "real"}, + {-1, 0} }; /******************************************************************************\ @@ -321,61 +321,61 @@ jas_taginfo_t modetab[] = { int jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr) { - jpc_enc_t *enc; - jpc_enc_cp_t *cp; + jpc_enc_t *enc; + jpc_enc_cp_t *cp; - enc = 0; - cp = 0; + enc = 0; + cp = 0; - jpc_initluts(); + jpc_initluts(); - if (!(cp = cp_create(optstr, image))) { - fprintf(stderr, "invalid JP encoder options\n"); - goto error; - } + if (!(cp = cp_create(optstr, image))) { + fprintf(stderr, "invalid JP encoder options\n"); + goto error; + } - if (!(enc = jpc_enc_create(cp, out, image))) { - goto error; - } - cp = 0; + if (!(enc = jpc_enc_create(cp, out, image))) { + goto error; + } + cp = 0; - /* Encode the main header. */ - if (jpc_enc_encodemainhdr(enc)) { - goto error; - } + /* Encode the main header. */ + if (jpc_enc_encodemainhdr(enc)) { + goto error; + } - /* Encode the main body. This constitutes most of the encoding work. */ - if (jpc_enc_encodemainbody(enc)) { - goto error; - } + /* Encode the main body. This constitutes most of the encoding work. */ + if (jpc_enc_encodemainbody(enc)) { + goto error; + } - /* Write EOC marker segment. */ - if (!(enc->mrk = jpc_ms_create(JPC_MS_EOC))) { - goto error; - } - if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write EOI marker\n"); - goto error; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; + /* Write EOC marker segment. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_EOC))) { + goto error; + } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write EOI marker\n"); + goto error; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; - if (jas_stream_flush(enc->out)) { - goto error; - } + if (jas_stream_flush(enc->out)) { + goto error; + } - jpc_enc_destroy(enc); + jpc_enc_destroy(enc); - return 0; + return 0; error: - if (cp) { - jpc_enc_cp_destroy(cp); - } - if (enc) { - jpc_enc_destroy(enc); - } - return -1; + if (cp) { + jpc_enc_cp_destroy(cp); + } + if (enc) { + jpc_enc_destroy(enc); + } + return -1; } /******************************************************************************\ @@ -384,466 +384,466 @@ error: static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image) { - jpc_enc_cp_t *cp; - jas_tvparser_t *tvp; - int ret; - int numilyrrates; - double *ilyrrates; - int i; - int tagid; - jpc_enc_tcp_t *tcp; - jpc_enc_tccp_t *tccp; - jpc_enc_ccp_t *ccp; - uint_fast16_t cmptno; - uint_fast16_t rlvlno; - uint_fast16_t prcwidthexpn; - uint_fast16_t prcheightexpn; - bool enablemct; - uint_fast32_t jp2overhead; - uint_fast16_t lyrno; - uint_fast32_t hsteplcm; - uint_fast32_t vsteplcm; - bool mctvalid; - - tvp = 0; - cp = 0; - ilyrrates = 0; - numilyrrates = 0; - - if (!(cp = jas_malloc(sizeof(jpc_enc_cp_t)))) { - goto error; - } - - prcwidthexpn = 15; - prcheightexpn = 15; - enablemct = true; - jp2overhead = 0; - - cp->ccps = 0; - cp->debug = 0; - cp->imgareatlx = UINT_FAST32_MAX; - cp->imgareatly = UINT_FAST32_MAX; - cp->refgrdwidth = 0; - cp->refgrdheight = 0; - cp->tilegrdoffx = UINT_FAST32_MAX; - cp->tilegrdoffy = UINT_FAST32_MAX; - cp->tilewidth = 0; - cp->tileheight = 0; - cp->numcmpts = jas_image_numcmpts(image); - - hsteplcm = 1; - vsteplcm = 1; - for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) { - if (jas_image_cmptbrx(image, cmptno) + jas_image_cmpthstep(image, cmptno) <= - jas_image_brx(image) || jas_image_cmptbry(image, cmptno) + - jas_image_cmptvstep(image, cmptno) <= jas_image_bry(image)) { - fprintf(stderr, "unsupported image type\n"); - goto error; - } - /* Note: We ought to be calculating the LCMs here. Fix some day. */ - hsteplcm *= jas_image_cmpthstep(image, cmptno); - vsteplcm *= jas_image_cmptvstep(image, cmptno); - } - - if (!(cp->ccps = jas_malloc(cp->numcmpts * sizeof(jpc_enc_ccp_t)))) { - goto error; - } - for (cmptno = 0, ccp = cp->ccps; cmptno < cp->numcmpts; ++cmptno, - ++ccp) { - ccp->sampgrdstepx = jas_image_cmpthstep(image, cmptno); - ccp->sampgrdstepy = jas_image_cmptvstep(image, cmptno); - /* XXX - this isn't quite correct for more general image */ - ccp->sampgrdsubstepx = 0; - ccp->sampgrdsubstepx = 0; - ccp->prec = jas_image_cmptprec(image, cmptno); - ccp->sgnd = jas_image_cmptsgnd(image, cmptno); - ccp->numstepsizes = 0; - memset(ccp->stepsizes, 0, sizeof(ccp->stepsizes)); - } - - cp->rawsize = jas_image_rawsize(image); - cp->totalsize = UINT_FAST32_MAX; - - tcp = &cp->tcp; - tcp->csty = 0; - tcp->intmode = true; - tcp->prg = JPC_COD_LRCPPRG; - tcp->numlyrs = 1; - tcp->ilyrrates = 0; - - tccp = &cp->tccp; - tccp->csty = 0; - tccp->maxrlvls = 6; - tccp->cblkwidthexpn = 6; - tccp->cblkheightexpn = 6; - tccp->cblksty = 0; - tccp->numgbits = 2; - - if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) { - goto error; - } - - while (!(ret = jas_tvparser_next(tvp))) { - switch (jas_taginfo_nonull(jas_taginfos_lookup(encopts, - jas_tvparser_gettag(tvp)))->id) { - case OPT_DEBUG: - cp->debug = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_IMGAREAOFFX: - cp->imgareatlx = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_IMGAREAOFFY: - cp->imgareatly = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_TILEGRDOFFX: - cp->tilegrdoffx = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_TILEGRDOFFY: - cp->tilegrdoffy = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_TILEWIDTH: - cp->tilewidth = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_TILEHEIGHT: - cp->tileheight = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_PRCWIDTH: - prcwidthexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); - break; - case OPT_PRCHEIGHT: - prcheightexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); - break; - case OPT_CBLKWIDTH: - tccp->cblkwidthexpn = - jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); - break; - case OPT_CBLKHEIGHT: - tccp->cblkheightexpn = - jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); - break; - case OPT_MODE: - if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(modetab, - jas_tvparser_getval(tvp)))->id) < 0) { - fprintf(stderr, - "ignoring invalid mode %s\n", - jas_tvparser_getval(tvp)); - } else { - tcp->intmode = (tagid == MODE_INT); - } - break; - case OPT_PRG: - if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(prgordtab, - jas_tvparser_getval(tvp)))->id) < 0) { - fprintf(stderr, - "ignoring invalid progression order %s\n", - jas_tvparser_getval(tvp)); - } else { - tcp->prg = tagid; - } - break; - case OPT_NOMCT: - enablemct = false; - break; - case OPT_MAXRLVLS: - tccp->maxrlvls = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_SOP: - cp->tcp.csty |= JPC_COD_SOP; - break; - case OPT_EPH: - cp->tcp.csty |= JPC_COD_EPH; - break; - case OPT_LAZY: - tccp->cblksty |= JPC_COX_LAZY; - break; - case OPT_TERMALL: - tccp->cblksty |= JPC_COX_TERMALL; - break; - case OPT_SEGSYM: - tccp->cblksty |= JPC_COX_SEGSYM; - break; - case OPT_VCAUSAL: - tccp->cblksty |= JPC_COX_VSC; - break; - case OPT_RESET: - tccp->cblksty |= JPC_COX_RESET; - break; - case OPT_PTERM: - tccp->cblksty |= JPC_COX_PTERM; - break; - case OPT_NUMGBITS: - cp->tccp.numgbits = atoi(jas_tvparser_getval(tvp)); - break; - case OPT_RATE: - if (ratestrtosize(jas_tvparser_getval(tvp), cp->rawsize, - &cp->totalsize)) { - fprintf(stderr, - "ignoring bad rate specifier %s\n", - jas_tvparser_getval(tvp)); - } - break; - case OPT_ILYRRATES: - if (jpc_atoaf(jas_tvparser_getval(tvp), &numilyrrates, - &ilyrrates)) { - fprintf(stderr, - "warning: invalid intermediate layer rates specifier ignored (%s)\n", - jas_tvparser_getval(tvp)); - } - break; - - case OPT_JP2OVERHEAD: - jp2overhead = atoi(jas_tvparser_getval(tvp)); - break; - default: - fprintf(stderr, "warning: ignoring invalid option %s\n", - jas_tvparser_gettag(tvp)); - break; - } - } - - jas_tvparser_destroy(tvp); - tvp = 0; - - if (cp->totalsize != UINT_FAST32_MAX) { - cp->totalsize = (cp->totalsize > jp2overhead) ? - (cp->totalsize - jp2overhead) : 0; - } - - if (cp->imgareatlx == UINT_FAST32_MAX) { - cp->imgareatlx = 0; - } else { - if (hsteplcm != 1) { - fprintf(stderr, "warning: overriding imgareatlx value\n"); - } - cp->imgareatlx *= hsteplcm; - } - if (cp->imgareatly == UINT_FAST32_MAX) { - cp->imgareatly = 0; - } else { - if (vsteplcm != 1) { - fprintf(stderr, "warning: overriding imgareatly value\n"); - } - cp->imgareatly *= vsteplcm; - } - cp->refgrdwidth = cp->imgareatlx + jas_image_width(image); - cp->refgrdheight = cp->imgareatly + jas_image_height(image); - if (cp->tilegrdoffx == UINT_FAST32_MAX) { - cp->tilegrdoffx = cp->imgareatlx; - } - if (cp->tilegrdoffy == UINT_FAST32_MAX) { - cp->tilegrdoffy = cp->imgareatly; - } - if (!cp->tilewidth) { - cp->tilewidth = cp->refgrdwidth - cp->tilegrdoffx; - } - if (!cp->tileheight) { - cp->tileheight = cp->refgrdheight - cp->tilegrdoffy; - } - - if (cp->numcmpts == 3) { - mctvalid = true; - for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) { - if (jas_image_cmptprec(image, cmptno) != jas_image_cmptprec(image, 0) || - jas_image_cmptsgnd(image, cmptno) != jas_image_cmptsgnd(image, 0) || - jas_image_cmptwidth(image, cmptno) != jas_image_cmptwidth(image, 0) || - jas_image_cmptheight(image, cmptno) != jas_image_cmptheight(image, 0)) { - mctvalid = false; - } - } - } else { - mctvalid = false; - } - if (mctvalid && enablemct && jas_image_colorspace(image) != JAS_IMAGE_CS_RGB) { - fprintf(stderr, "warning: color model apparently not RGB\n"); - } - if (mctvalid && enablemct && jas_image_colorspace(image) == JAS_IMAGE_CS_RGB) { - tcp->mctid = (tcp->intmode) ? (JPC_MCT_RCT) : (JPC_MCT_ICT); - } else { - tcp->mctid = JPC_MCT_NONE; - } - tccp->qmfbid = (tcp->intmode) ? (JPC_COX_RFT) : (JPC_COX_INS); - - for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) { - tccp->prcwidthexpns[rlvlno] = prcwidthexpn; - tccp->prcheightexpns[rlvlno] = prcheightexpn; - } - if (prcwidthexpn != 15 || prcheightexpn != 15) { - tccp->csty |= JPC_COX_PRT; - } - - /* Ensure that the tile width and height is valid. */ - if (!cp->tilewidth) { - fprintf(stderr, "invalid tile width %lu\n", (unsigned long) - cp->tilewidth); - goto error; - } - if (!cp->tileheight) { - fprintf(stderr, "invalid tile height %lu\n", (unsigned long) - cp->tileheight); - goto error; - } - - /* Ensure that the tile grid offset is valid. */ - if (cp->tilegrdoffx > cp->imgareatlx || - cp->tilegrdoffy > cp->imgareatly || - cp->tilegrdoffx + cp->tilewidth < cp->imgareatlx || - cp->tilegrdoffy + cp->tileheight < cp->imgareatly) { - fprintf(stderr, "invalid tile grid offset (%lu, %lu)\n", - (unsigned long) cp->tilegrdoffx, (unsigned long) - cp->tilegrdoffy); - goto error; - } - - cp->numhtiles = JPC_CEILDIV(cp->refgrdwidth - cp->tilegrdoffx, - cp->tilewidth); - cp->numvtiles = JPC_CEILDIV(cp->refgrdheight - cp->tilegrdoffy, - cp->tileheight); - cp->numtiles = cp->numhtiles * cp->numvtiles; - - if (ilyrrates && numilyrrates > 0) { - tcp->numlyrs = numilyrrates + 1; - if (!(tcp->ilyrrates = jas_malloc((tcp->numlyrs - 1) * - sizeof(jpc_fix_t)))) { - goto error; - } - for (i = 0; i < tcp->numlyrs - 1; ++i) { - tcp->ilyrrates[i] = jpc_dbltofix(ilyrrates[i]); - } - } - - /* Ensure that the integer mode is used in the case of lossless - coding. */ - if (cp->totalsize == UINT_FAST32_MAX && (!cp->tcp.intmode)) { - fprintf(stderr, "cannot use real mode for lossless coding\n"); - goto error; - } - - /* Ensure that the precinct width is valid. */ - if (prcwidthexpn > 15) { - fprintf(stderr, "invalid precinct width\n"); - goto error; - } - - /* Ensure that the precinct height is valid. */ - if (prcheightexpn > 15) { - fprintf(stderr, "invalid precinct height\n"); - goto error; - } - - /* Ensure that the code block width is valid. */ - if (cp->tccp.cblkwidthexpn < 2 || cp->tccp.cblkwidthexpn > 12) { - fprintf(stderr, "invalid code block width %d\n", - JPC_POW2(cp->tccp.cblkwidthexpn)); - goto error; - } - - /* Ensure that the code block height is valid. */ - if (cp->tccp.cblkheightexpn < 2 || cp->tccp.cblkheightexpn > 12) { - fprintf(stderr, "invalid code block height %d\n", - JPC_POW2(cp->tccp.cblkheightexpn)); - goto error; - } - - /* Ensure that the code block size is not too large. */ - if (cp->tccp.cblkwidthexpn + cp->tccp.cblkheightexpn > 12) { - fprintf(stderr, "code block size too large\n"); - goto error; - } - - /* Ensure that the number of layers is valid. */ - if (cp->tcp.numlyrs > 16384) { - fprintf(stderr, "too many layers\n"); - goto error; - } - - /* There must be at least one resolution level. */ - if (cp->tccp.maxrlvls < 1) { - fprintf(stderr, "must be at least one resolution level\n"); - goto error; - } - - /* Ensure that the number of guard bits is valid. */ - if (cp->tccp.numgbits > 8) { - fprintf(stderr, "invalid number of guard bits\n"); - goto error; - } - - /* Ensure that the rate is within the legal range. */ - if (cp->totalsize != UINT_FAST32_MAX && cp->totalsize > cp->rawsize) { - fprintf(stderr, "warning: specified rate is unreasonably large (%lu > %lu)\n", (unsigned long) cp->totalsize, (unsigned long) cp->rawsize); - } - - /* Ensure that the intermediate layer rates are valid. */ - if (tcp->numlyrs > 1) { - /* The intermediate layers rates must increase monotonically. */ - for (lyrno = 0; lyrno + 2 < tcp->numlyrs; ++lyrno) { - if (tcp->ilyrrates[lyrno] >= tcp->ilyrrates[lyrno + 1]) { - fprintf(stderr, "intermediate layer rates must increase monotonically\n"); - goto error; - } - } - /* The intermediate layer rates must be less than the overall rate. */ - if (cp->totalsize != UINT_FAST32_MAX) { - for (lyrno = 0; lyrno < tcp->numlyrs - 1; ++lyrno) { - if (jpc_fixtodbl(tcp->ilyrrates[lyrno]) > ((double) cp->totalsize) - / cp->rawsize) { - fprintf(stderr, "warning: intermediate layer rates must be less than overall rate\n"); - goto error; - } - } - } - } - - if (ilyrrates) { - jas_free(ilyrrates); - } - - return cp; + jpc_enc_cp_t *cp; + jas_tvparser_t *tvp; + int ret; + int numilyrrates; + double *ilyrrates; + int i; + int tagid; + jpc_enc_tcp_t *tcp; + jpc_enc_tccp_t *tccp; + jpc_enc_ccp_t *ccp; + uint_fast16_t cmptno; + uint_fast16_t rlvlno; + uint_fast16_t prcwidthexpn; + uint_fast16_t prcheightexpn; + bool enablemct; + uint_fast32_t jp2overhead; + uint_fast16_t lyrno; + uint_fast32_t hsteplcm; + uint_fast32_t vsteplcm; + bool mctvalid; + + tvp = 0; + cp = 0; + ilyrrates = 0; + numilyrrates = 0; + + if (!(cp = jas_malloc(sizeof(jpc_enc_cp_t)))) { + goto error; + } + + prcwidthexpn = 15; + prcheightexpn = 15; + enablemct = true; + jp2overhead = 0; + + cp->ccps = 0; + cp->debug = 0; + cp->imgareatlx = UINT_FAST32_MAX; + cp->imgareatly = UINT_FAST32_MAX; + cp->refgrdwidth = 0; + cp->refgrdheight = 0; + cp->tilegrdoffx = UINT_FAST32_MAX; + cp->tilegrdoffy = UINT_FAST32_MAX; + cp->tilewidth = 0; + cp->tileheight = 0; + cp->numcmpts = jas_image_numcmpts(image); + + hsteplcm = 1; + vsteplcm = 1; + for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) { + if (jas_image_cmptbrx(image, cmptno) + jas_image_cmpthstep(image, cmptno) <= + jas_image_brx(image) || jas_image_cmptbry(image, cmptno) + + jas_image_cmptvstep(image, cmptno) <= jas_image_bry(image)) { + fprintf(stderr, "unsupported image type\n"); + goto error; + } + /* Note: We ought to be calculating the LCMs here. Fix some day. */ + hsteplcm *= jas_image_cmpthstep(image, cmptno); + vsteplcm *= jas_image_cmptvstep(image, cmptno); + } + + if (!(cp->ccps = jas_malloc(cp->numcmpts * sizeof(jpc_enc_ccp_t)))) { + goto error; + } + for (cmptno = 0, ccp = cp->ccps; cmptno < cp->numcmpts; ++cmptno, + ++ccp) { + ccp->sampgrdstepx = jas_image_cmpthstep(image, cmptno); + ccp->sampgrdstepy = jas_image_cmptvstep(image, cmptno); + /* XXX - this isn't quite correct for more general image */ + ccp->sampgrdsubstepx = 0; + ccp->sampgrdsubstepx = 0; + ccp->prec = jas_image_cmptprec(image, cmptno); + ccp->sgnd = jas_image_cmptsgnd(image, cmptno); + ccp->numstepsizes = 0; + memset(ccp->stepsizes, 0, sizeof(ccp->stepsizes)); + } + + cp->rawsize = jas_image_rawsize(image); + cp->totalsize = UINT_FAST32_MAX; + + tcp = &cp->tcp; + tcp->csty = 0; + tcp->intmode = true; + tcp->prg = JPC_COD_LRCPPRG; + tcp->numlyrs = 1; + tcp->ilyrrates = 0; + + tccp = &cp->tccp; + tccp->csty = 0; + tccp->maxrlvls = 6; + tccp->cblkwidthexpn = 6; + tccp->cblkheightexpn = 6; + tccp->cblksty = 0; + tccp->numgbits = 2; + + if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) { + goto error; + } + + while (!(ret = jas_tvparser_next(tvp))) { + switch (jas_taginfo_nonull(jas_taginfos_lookup(encopts, + jas_tvparser_gettag(tvp)))->id) { + case OPT_DEBUG: + cp->debug = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_IMGAREAOFFX: + cp->imgareatlx = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_IMGAREAOFFY: + cp->imgareatly = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_TILEGRDOFFX: + cp->tilegrdoffx = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_TILEGRDOFFY: + cp->tilegrdoffy = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_TILEWIDTH: + cp->tilewidth = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_TILEHEIGHT: + cp->tileheight = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_PRCWIDTH: + prcwidthexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); + break; + case OPT_PRCHEIGHT: + prcheightexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); + break; + case OPT_CBLKWIDTH: + tccp->cblkwidthexpn = + jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); + break; + case OPT_CBLKHEIGHT: + tccp->cblkheightexpn = + jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); + break; + case OPT_MODE: + if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(modetab, + jas_tvparser_getval(tvp)))->id) < 0) { + fprintf(stderr, + "ignoring invalid mode %s\n", + jas_tvparser_getval(tvp)); + } else { + tcp->intmode = (tagid == MODE_INT); + } + break; + case OPT_PRG: + if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(prgordtab, + jas_tvparser_getval(tvp)))->id) < 0) { + fprintf(stderr, + "ignoring invalid progression order %s\n", + jas_tvparser_getval(tvp)); + } else { + tcp->prg = tagid; + } + break; + case OPT_NOMCT: + enablemct = false; + break; + case OPT_MAXRLVLS: + tccp->maxrlvls = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_SOP: + cp->tcp.csty |= JPC_COD_SOP; + break; + case OPT_EPH: + cp->tcp.csty |= JPC_COD_EPH; + break; + case OPT_LAZY: + tccp->cblksty |= JPC_COX_LAZY; + break; + case OPT_TERMALL: + tccp->cblksty |= JPC_COX_TERMALL; + break; + case OPT_SEGSYM: + tccp->cblksty |= JPC_COX_SEGSYM; + break; + case OPT_VCAUSAL: + tccp->cblksty |= JPC_COX_VSC; + break; + case OPT_RESET: + tccp->cblksty |= JPC_COX_RESET; + break; + case OPT_PTERM: + tccp->cblksty |= JPC_COX_PTERM; + break; + case OPT_NUMGBITS: + cp->tccp.numgbits = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_RATE: + if (ratestrtosize(jas_tvparser_getval(tvp), cp->rawsize, + &cp->totalsize)) { + fprintf(stderr, + "ignoring bad rate specifier %s\n", + jas_tvparser_getval(tvp)); + } + break; + case OPT_ILYRRATES: + if (jpc_atoaf(jas_tvparser_getval(tvp), &numilyrrates, + &ilyrrates)) { + fprintf(stderr, + "warning: invalid intermediate layer rates specifier ignored (%s)\n", + jas_tvparser_getval(tvp)); + } + break; + + case OPT_JP2OVERHEAD: + jp2overhead = atoi(jas_tvparser_getval(tvp)); + break; + default: + fprintf(stderr, "warning: ignoring invalid option %s\n", + jas_tvparser_gettag(tvp)); + break; + } + } + + jas_tvparser_destroy(tvp); + tvp = 0; + + if (cp->totalsize != UINT_FAST32_MAX) { + cp->totalsize = (cp->totalsize > jp2overhead) ? + (cp->totalsize - jp2overhead) : 0; + } + + if (cp->imgareatlx == UINT_FAST32_MAX) { + cp->imgareatlx = 0; + } else { + if (hsteplcm != 1) { + fprintf(stderr, "warning: overriding imgareatlx value\n"); + } + cp->imgareatlx *= hsteplcm; + } + if (cp->imgareatly == UINT_FAST32_MAX) { + cp->imgareatly = 0; + } else { + if (vsteplcm != 1) { + fprintf(stderr, "warning: overriding imgareatly value\n"); + } + cp->imgareatly *= vsteplcm; + } + cp->refgrdwidth = cp->imgareatlx + jas_image_width(image); + cp->refgrdheight = cp->imgareatly + jas_image_height(image); + if (cp->tilegrdoffx == UINT_FAST32_MAX) { + cp->tilegrdoffx = cp->imgareatlx; + } + if (cp->tilegrdoffy == UINT_FAST32_MAX) { + cp->tilegrdoffy = cp->imgareatly; + } + if (!cp->tilewidth) { + cp->tilewidth = cp->refgrdwidth - cp->tilegrdoffx; + } + if (!cp->tileheight) { + cp->tileheight = cp->refgrdheight - cp->tilegrdoffy; + } + + if (cp->numcmpts == 3) { + mctvalid = true; + for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) { + if (jas_image_cmptprec(image, cmptno) != jas_image_cmptprec(image, 0) || + jas_image_cmptsgnd(image, cmptno) != jas_image_cmptsgnd(image, 0) || + jas_image_cmptwidth(image, cmptno) != jas_image_cmptwidth(image, 0) || + jas_image_cmptheight(image, cmptno) != jas_image_cmptheight(image, 0)) { + mctvalid = false; + } + } + } else { + mctvalid = false; + } + if (mctvalid && enablemct && jas_image_colorspace(image) != JAS_IMAGE_CS_RGB) { + fprintf(stderr, "warning: color model apparently not RGB\n"); + } + if (mctvalid && enablemct && jas_image_colorspace(image) == JAS_IMAGE_CS_RGB) { + tcp->mctid = (tcp->intmode) ? (JPC_MCT_RCT) : (JPC_MCT_ICT); + } else { + tcp->mctid = JPC_MCT_NONE; + } + tccp->qmfbid = (tcp->intmode) ? (JPC_COX_RFT) : (JPC_COX_INS); + + for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) { + tccp->prcwidthexpns[rlvlno] = prcwidthexpn; + tccp->prcheightexpns[rlvlno] = prcheightexpn; + } + if (prcwidthexpn != 15 || prcheightexpn != 15) { + tccp->csty |= JPC_COX_PRT; + } + + /* Ensure that the tile width and height is valid. */ + if (!cp->tilewidth) { + fprintf(stderr, "invalid tile width %lu\n", (unsigned long) + cp->tilewidth); + goto error; + } + if (!cp->tileheight) { + fprintf(stderr, "invalid tile height %lu\n", (unsigned long) + cp->tileheight); + goto error; + } + + /* Ensure that the tile grid offset is valid. */ + if (cp->tilegrdoffx > cp->imgareatlx || + cp->tilegrdoffy > cp->imgareatly || + cp->tilegrdoffx + cp->tilewidth < cp->imgareatlx || + cp->tilegrdoffy + cp->tileheight < cp->imgareatly) { + fprintf(stderr, "invalid tile grid offset (%lu, %lu)\n", + (unsigned long) cp->tilegrdoffx, (unsigned long) + cp->tilegrdoffy); + goto error; + } + + cp->numhtiles = JPC_CEILDIV(cp->refgrdwidth - cp->tilegrdoffx, + cp->tilewidth); + cp->numvtiles = JPC_CEILDIV(cp->refgrdheight - cp->tilegrdoffy, + cp->tileheight); + cp->numtiles = cp->numhtiles * cp->numvtiles; + + if (ilyrrates && numilyrrates > 0) { + tcp->numlyrs = numilyrrates + 1; + if (!(tcp->ilyrrates = jas_malloc((tcp->numlyrs - 1) * + sizeof(jpc_fix_t)))) { + goto error; + } + for (i = 0; i < tcp->numlyrs - 1; ++i) { + tcp->ilyrrates[i] = jpc_dbltofix(ilyrrates[i]); + } + } + + /* Ensure that the integer mode is used in the case of lossless + coding. */ + if (cp->totalsize == UINT_FAST32_MAX && (!cp->tcp.intmode)) { + fprintf(stderr, "cannot use real mode for lossless coding\n"); + goto error; + } + + /* Ensure that the precinct width is valid. */ + if (prcwidthexpn > 15) { + fprintf(stderr, "invalid precinct width\n"); + goto error; + } + + /* Ensure that the precinct height is valid. */ + if (prcheightexpn > 15) { + fprintf(stderr, "invalid precinct height\n"); + goto error; + } + + /* Ensure that the code block width is valid. */ + if (cp->tccp.cblkwidthexpn < 2 || cp->tccp.cblkwidthexpn > 12) { + fprintf(stderr, "invalid code block width %d\n", + JPC_POW2(cp->tccp.cblkwidthexpn)); + goto error; + } + + /* Ensure that the code block height is valid. */ + if (cp->tccp.cblkheightexpn < 2 || cp->tccp.cblkheightexpn > 12) { + fprintf(stderr, "invalid code block height %d\n", + JPC_POW2(cp->tccp.cblkheightexpn)); + goto error; + } + + /* Ensure that the code block size is not too large. */ + if (cp->tccp.cblkwidthexpn + cp->tccp.cblkheightexpn > 12) { + fprintf(stderr, "code block size too large\n"); + goto error; + } + + /* Ensure that the number of layers is valid. */ + if (cp->tcp.numlyrs > 16384) { + fprintf(stderr, "too many layers\n"); + goto error; + } + + /* There must be at least one resolution level. */ + if (cp->tccp.maxrlvls < 1) { + fprintf(stderr, "must be at least one resolution level\n"); + goto error; + } + + /* Ensure that the number of guard bits is valid. */ + if (cp->tccp.numgbits > 8) { + fprintf(stderr, "invalid number of guard bits\n"); + goto error; + } + + /* Ensure that the rate is within the legal range. */ + if (cp->totalsize != UINT_FAST32_MAX && cp->totalsize > cp->rawsize) { + fprintf(stderr, "warning: specified rate is unreasonably large (%lu > %lu)\n", (unsigned long) cp->totalsize, (unsigned long) cp->rawsize); + } + + /* Ensure that the intermediate layer rates are valid. */ + if (tcp->numlyrs > 1) { + /* The intermediate layers rates must increase monotonically. */ + for (lyrno = 0; lyrno + 2 < tcp->numlyrs; ++lyrno) { + if (tcp->ilyrrates[lyrno] >= tcp->ilyrrates[lyrno + 1]) { + fprintf(stderr, "intermediate layer rates must increase monotonically\n"); + goto error; + } + } + /* The intermediate layer rates must be less than the overall rate. */ + if (cp->totalsize != UINT_FAST32_MAX) { + for (lyrno = 0; lyrno < tcp->numlyrs - 1; ++lyrno) { + if (jpc_fixtodbl(tcp->ilyrrates[lyrno]) > ((double) cp->totalsize) + / cp->rawsize) { + fprintf(stderr, "warning: intermediate layer rates must be less than overall rate\n"); + goto error; + } + } + } + } + + if (ilyrrates) { + jas_free(ilyrrates); + } + + return cp; error: - if (ilyrrates) { - jas_free(ilyrrates); - } - if (tvp) { - jas_tvparser_destroy(tvp); - } - if (cp) { - jpc_enc_cp_destroy(cp); - } - return 0; + if (ilyrrates) { + jas_free(ilyrrates); + } + if (tvp) { + jas_tvparser_destroy(tvp); + } + if (cp) { + jpc_enc_cp_destroy(cp); + } + return 0; } void jpc_enc_cp_destroy(jpc_enc_cp_t *cp) { - if (cp->ccps) { - if (cp->tcp.ilyrrates) { - jas_free(cp->tcp.ilyrrates); - } - jas_free(cp->ccps); - } - jas_free(cp); + if (cp->ccps) { + if (cp->tcp.ilyrrates) { + jas_free(cp->tcp.ilyrrates); + } + jas_free(cp->ccps); + } + jas_free(cp); } int ratestrtosize(const char *s, uint_fast32_t rawsize, uint_fast32_t *size) { - char *cp; - jpc_flt_t f; - - /* Note: This function must not modify output size on failure. */ - if ((cp = strchr(s, 'B'))) { - *size = atoi(s); - } else { - f = atof(s); - if (f < 0) { - *size = 0; - } else if (f > 1.0) { - *size = rawsize + 1; - } else { - *size = f * rawsize; - } - } - return 0; + char *cp; + jpc_flt_t f; + + /* Note: This function must not modify output size on failure. */ + if ((cp = strchr(s, 'B'))) { + *size = atoi(s); + } else { + f = atof(s); + if (f < 0) { + *size = 0; + } else if (f > 1.0) { + *size = rawsize + 1; + } else { + *size = f * rawsize; + } + } + return 0; } /******************************************************************************\ @@ -852,58 +852,58 @@ int ratestrtosize(const char *s, uint_fast32_t rawsize, uint_fast32_t *size) jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image) { - jpc_enc_t *enc; + jpc_enc_t *enc; - enc = 0; + enc = 0; - if (!(enc = jas_malloc(sizeof(jpc_enc_t)))) { - goto error; - } + if (!(enc = jas_malloc(sizeof(jpc_enc_t)))) { + goto error; + } - enc->image = image; - enc->out = out; - enc->cp = cp; - enc->cstate = 0; - enc->tmpstream = 0; - enc->mrk = 0; - enc->curtile = 0; + enc->image = image; + enc->out = out; + enc->cp = cp; + enc->cstate = 0; + enc->tmpstream = 0; + enc->mrk = 0; + enc->curtile = 0; - if (!(enc->cstate = jpc_cstate_create())) { - goto error; - } - enc->len = 0; - enc->mainbodysize = 0; + if (!(enc->cstate = jpc_cstate_create())) { + goto error; + } + enc->len = 0; + enc->mainbodysize = 0; - return enc; + return enc; error: - if (enc) { - jpc_enc_destroy(enc); - } - return 0; + if (enc) { + jpc_enc_destroy(enc); + } + return 0; } void jpc_enc_destroy(jpc_enc_t *enc) { - /* The image object (i.e., enc->image) and output stream object - (i.e., enc->out) are created outside of the encoder. - Therefore, they must not be destroyed here. */ - - if (enc->curtile) { - jpc_enc_tile_destroy(enc->curtile); - } - if (enc->cp) { - jpc_enc_cp_destroy(enc->cp); - } - if (enc->cstate) { - jpc_cstate_destroy(enc->cstate); - } - if (enc->tmpstream) { - jas_stream_close(enc->tmpstream); - } - - jas_free(enc); + /* The image object (i.e., enc->image) and output stream object + (i.e., enc->out) are created outside of the encoder. + Therefore, they must not be destroyed here. */ + + if (enc->curtile) { + jpc_enc_tile_destroy(enc->curtile); + } + if (enc->cp) { + jpc_enc_cp_destroy(enc->cp); + } + if (enc->cstate) { + jpc_cstate_destroy(enc->cstate); + } + if (enc->tmpstream) { + jas_stream_close(enc->tmpstream); + } + + jas_free(enc); } /******************************************************************************\ @@ -912,242 +912,242 @@ void jpc_enc_destroy(jpc_enc_t *enc) static int jpc_enc_encodemainhdr(jpc_enc_t *enc) { - jpc_siz_t *siz; - jpc_cod_t *cod; - jpc_qcd_t *qcd; - int i; + jpc_siz_t *siz; + jpc_cod_t *cod; + jpc_qcd_t *qcd; + int i; long startoff; long mainhdrlen; - jpc_enc_cp_t *cp; - jpc_qcc_t *qcc; - jpc_enc_tccp_t *tccp; - uint_fast16_t cmptno; - jpc_tsfb_band_t bandinfos[JPC_MAXBANDS]; - jpc_fix_t mctsynweight; - jpc_enc_tcp_t *tcp; - jpc_tsfb_t *tsfb; - jpc_tsfb_band_t *bandinfo; - uint_fast16_t numbands; - uint_fast16_t bandno; - uint_fast16_t rlvlno; - uint_fast16_t analgain; - jpc_fix_t absstepsize; - char buf[1024]; - jpc_com_t *com; - - cp = enc->cp; + jpc_enc_cp_t *cp; + jpc_qcc_t *qcc; + jpc_enc_tccp_t *tccp; + uint_fast16_t cmptno; + jpc_tsfb_band_t bandinfos[JPC_MAXBANDS]; + jpc_fix_t mctsynweight; + jpc_enc_tcp_t *tcp; + jpc_tsfb_t *tsfb; + jpc_tsfb_band_t *bandinfo; + uint_fast16_t numbands; + uint_fast16_t bandno; + uint_fast16_t rlvlno; + uint_fast16_t analgain; + jpc_fix_t absstepsize; + char buf[1024]; + jpc_com_t *com; + + cp = enc->cp; startoff = jas_stream_getrwcount(enc->out); - /* Write SOC marker segment. */ - if (!(enc->mrk = jpc_ms_create(JPC_MS_SOC))) { - return -1; - } - if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write SOC marker\n"); - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - - /* Write SIZ marker segment. */ - if (!(enc->mrk = jpc_ms_create(JPC_MS_SIZ))) { - return -1; - } - siz = &enc->mrk->parms.siz; - siz->caps = 0; - siz->xoff = cp->imgareatlx; - siz->yoff = cp->imgareatly; - siz->width = cp->refgrdwidth; - siz->height = cp->refgrdheight; - siz->tilexoff = cp->tilegrdoffx; - siz->tileyoff = cp->tilegrdoffy; - siz->tilewidth = cp->tilewidth; - siz->tileheight = cp->tileheight; - siz->numcomps = cp->numcmpts; - siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t)); - assert(siz->comps); - for (i = 0; i < cp->numcmpts; ++i) { - siz->comps[i].prec = cp->ccps[i].prec; - siz->comps[i].sgnd = cp->ccps[i].sgnd; - siz->comps[i].hsamp = cp->ccps[i].sampgrdstepx; - siz->comps[i].vsamp = cp->ccps[i].sampgrdstepy; - } - if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write SIZ marker\n"); - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - - if (!(enc->mrk = jpc_ms_create(JPC_MS_COM))) { - return -1; - } - sprintf(buf, "Creator: JasPer Version %s", jas_getversion()); - com = &enc->mrk->parms.com; - com->len = strlen(buf); - com->regid = JPC_COM_LATIN; - if (!(com->data = JAS_CAST(unsigned char *, jas_strdup(buf)))) { - abort(); - } - if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write COM marker\n"); - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; + /* Write SOC marker segment. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_SOC))) { + return -1; + } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write SOC marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + + /* Write SIZ marker segment. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_SIZ))) { + return -1; + } + siz = &enc->mrk->parms.siz; + siz->caps = 0; + siz->xoff = cp->imgareatlx; + siz->yoff = cp->imgareatly; + siz->width = cp->refgrdwidth; + siz->height = cp->refgrdheight; + siz->tilexoff = cp->tilegrdoffx; + siz->tileyoff = cp->tilegrdoffy; + siz->tilewidth = cp->tilewidth; + siz->tileheight = cp->tileheight; + siz->numcomps = cp->numcmpts; + siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t)); + assert(siz->comps); + for (i = 0; i < cp->numcmpts; ++i) { + siz->comps[i].prec = cp->ccps[i].prec; + siz->comps[i].sgnd = cp->ccps[i].sgnd; + siz->comps[i].hsamp = cp->ccps[i].sampgrdstepx; + siz->comps[i].vsamp = cp->ccps[i].sampgrdstepy; + } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write SIZ marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + + if (!(enc->mrk = jpc_ms_create(JPC_MS_COM))) { + return -1; + } + sprintf(buf, "Creator: JasPer Version %s", jas_getversion()); + com = &enc->mrk->parms.com; + com->len = strlen(buf); + com->regid = JPC_COM_LATIN; + if (!(com->data = JAS_CAST(unsigned char *, jas_strdup(buf)))) { + abort(); + } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write COM marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; #if 0 - if (!(enc->mrk = jpc_ms_create(JPC_MS_CRG))) { - return -1; - } - crg = &enc->mrk->parms.crg; - crg->comps = jas_malloc(crg->numcomps * sizeof(jpc_crgcomp_t)); - if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write CRG marker\n"); - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; + if (!(enc->mrk = jpc_ms_create(JPC_MS_CRG))) { + return -1; + } + crg = &enc->mrk->parms.crg; + crg->comps = jas_malloc(crg->numcomps * sizeof(jpc_crgcomp_t)); + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write CRG marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; #endif - tcp = &cp->tcp; - tccp = &cp->tccp; - for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) { - tsfb = jpc_cod_gettsfb(tccp->qmfbid, tccp->maxrlvls - 1); - jpc_tsfb_getbands(tsfb, 0, 0, 1 << tccp->maxrlvls, 1 << tccp->maxrlvls, - bandinfos); - jpc_tsfb_destroy(tsfb); - mctsynweight = jpc_mct_getsynweight(tcp->mctid, cmptno); - numbands = 3 * tccp->maxrlvls - 2; - for (bandno = 0, bandinfo = bandinfos; bandno < numbands; - ++bandno, ++bandinfo) { - rlvlno = (bandno) ? ((bandno - 1) / 3 + 1) : 0; - analgain = JPC_NOMINALGAIN(tccp->qmfbid, tccp->maxrlvls, - rlvlno, bandinfo->orient); - if (!tcp->intmode) { - absstepsize = jpc_fix_div(jpc_inttofix(1 << - (analgain + 1)), bandinfo->synenergywt); - } else { - absstepsize = jpc_inttofix(1); - } - cp->ccps[cmptno].stepsizes[bandno] = - jpc_abstorelstepsize(absstepsize, - cp->ccps[cmptno].prec + analgain); - } - cp->ccps[cmptno].numstepsizes = numbands; - } - - if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) { - return -1; - } - cod = &enc->mrk->parms.cod; - cod->csty = cp->tccp.csty | cp->tcp.csty; - cod->compparms.csty = cp->tccp.csty | cp->tcp.csty; - cod->compparms.numdlvls = cp->tccp.maxrlvls - 1; - cod->compparms.numrlvls = cp->tccp.maxrlvls; - cod->prg = cp->tcp.prg; - cod->numlyrs = cp->tcp.numlyrs; - cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkwidthexpn); - cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkheightexpn); - cod->compparms.cblksty = cp->tccp.cblksty; - cod->compparms.qmfbid = cp->tccp.qmfbid; - cod->mctrans = (cp->tcp.mctid != JPC_MCT_NONE); - if (tccp->csty & JPC_COX_PRT) { - for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) { - cod->compparms.rlvls[rlvlno].parwidthval = tccp->prcwidthexpns[rlvlno]; - cod->compparms.rlvls[rlvlno].parheightval = tccp->prcheightexpns[rlvlno]; - } - } - if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write COD marker\n"); - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - - if (!(enc->mrk = jpc_ms_create(JPC_MS_QCD))) { - return -1; - } - qcd = &enc->mrk->parms.qcd; - qcd->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ? - JPC_QCX_SEQNT : JPC_QCX_NOQNT; - qcd->compparms.numstepsizes = cp->ccps[0].numstepsizes; - qcd->compparms.numguard = cp->tccp.numgbits; - qcd->compparms.stepsizes = cp->ccps[0].stepsizes; - if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { - return -1; - } - /* We do not want the step size array to be freed! */ - qcd->compparms.stepsizes = 0; - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - - tccp = &cp->tccp; - for (cmptno = 1; cmptno < cp->numcmpts; ++cmptno) { - if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) { - return -1; - } - qcc = &enc->mrk->parms.qcc; - qcc->compno = cmptno; - qcc->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ? - JPC_QCX_SEQNT : JPC_QCX_NOQNT; - qcc->compparms.numstepsizes = cp->ccps[cmptno].numstepsizes; - qcc->compparms.numguard = cp->tccp.numgbits; - qcc->compparms.stepsizes = cp->ccps[cmptno].stepsizes; - if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { - return -1; - } - /* We do not want the step size array to be freed! */ - qcc->compparms.stepsizes = 0; - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - } - -#define MAINTLRLEN 2 - mainhdrlen = jas_stream_getrwcount(enc->out) - startoff; - enc->len += mainhdrlen; - if (enc->cp->totalsize != UINT_FAST32_MAX) { - uint_fast32_t overhead; - overhead = mainhdrlen + MAINTLRLEN; - enc->mainbodysize = (enc->cp->totalsize >= overhead) ? - (enc->cp->totalsize - overhead) : 0; - } else { - enc->mainbodysize = UINT_FAST32_MAX; - } - - return 0; + tcp = &cp->tcp; + tccp = &cp->tccp; + for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) { + tsfb = jpc_cod_gettsfb(tccp->qmfbid, tccp->maxrlvls - 1); + jpc_tsfb_getbands(tsfb, 0, 0, 1 << tccp->maxrlvls, 1 << tccp->maxrlvls, + bandinfos); + jpc_tsfb_destroy(tsfb); + mctsynweight = jpc_mct_getsynweight(tcp->mctid, cmptno); + numbands = 3 * tccp->maxrlvls - 2; + for (bandno = 0, bandinfo = bandinfos; bandno < numbands; + ++bandno, ++bandinfo) { + rlvlno = (bandno) ? ((bandno - 1) / 3 + 1) : 0; + analgain = JPC_NOMINALGAIN(tccp->qmfbid, tccp->maxrlvls, + rlvlno, bandinfo->orient); + if (!tcp->intmode) { + absstepsize = jpc_fix_div(jpc_inttofix(1 << + (analgain + 1)), bandinfo->synenergywt); + } else { + absstepsize = jpc_inttofix(1); + } + cp->ccps[cmptno].stepsizes[bandno] = + jpc_abstorelstepsize(absstepsize, + cp->ccps[cmptno].prec + analgain); + } + cp->ccps[cmptno].numstepsizes = numbands; + } + + if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) { + return -1; + } + cod = &enc->mrk->parms.cod; + cod->csty = cp->tccp.csty | cp->tcp.csty; + cod->compparms.csty = cp->tccp.csty | cp->tcp.csty; + cod->compparms.numdlvls = cp->tccp.maxrlvls - 1; + cod->compparms.numrlvls = cp->tccp.maxrlvls; + cod->prg = cp->tcp.prg; + cod->numlyrs = cp->tcp.numlyrs; + cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkwidthexpn); + cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkheightexpn); + cod->compparms.cblksty = cp->tccp.cblksty; + cod->compparms.qmfbid = cp->tccp.qmfbid; + cod->mctrans = (cp->tcp.mctid != JPC_MCT_NONE); + if (tccp->csty & JPC_COX_PRT) { + for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) { + cod->compparms.rlvls[rlvlno].parwidthval = tccp->prcwidthexpns[rlvlno]; + cod->compparms.rlvls[rlvlno].parheightval = tccp->prcheightexpns[rlvlno]; + } + } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write COD marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + + if (!(enc->mrk = jpc_ms_create(JPC_MS_QCD))) { + return -1; + } + qcd = &enc->mrk->parms.qcd; + qcd->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ? + JPC_QCX_SEQNT : JPC_QCX_NOQNT; + qcd->compparms.numstepsizes = cp->ccps[0].numstepsizes; + qcd->compparms.numguard = cp->tccp.numgbits; + qcd->compparms.stepsizes = cp->ccps[0].stepsizes; + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + return -1; + } + /* We do not want the step size array to be freed! */ + qcd->compparms.stepsizes = 0; + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + + tccp = &cp->tccp; + for (cmptno = 1; cmptno < cp->numcmpts; ++cmptno) { + if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) { + return -1; + } + qcc = &enc->mrk->parms.qcc; + qcc->compno = cmptno; + qcc->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ? + JPC_QCX_SEQNT : JPC_QCX_NOQNT; + qcc->compparms.numstepsizes = cp->ccps[cmptno].numstepsizes; + qcc->compparms.numguard = cp->tccp.numgbits; + qcc->compparms.stepsizes = cp->ccps[cmptno].stepsizes; + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + return -1; + } + /* We do not want the step size array to be freed! */ + qcc->compparms.stepsizes = 0; + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + } + +#define MAINTLRLEN 2 + mainhdrlen = jas_stream_getrwcount(enc->out) - startoff; + enc->len += mainhdrlen; + if (enc->cp->totalsize != UINT_FAST32_MAX) { + uint_fast32_t overhead; + overhead = mainhdrlen + MAINTLRLEN; + enc->mainbodysize = (enc->cp->totalsize >= overhead) ? + (enc->cp->totalsize - overhead) : 0; + } else { + enc->mainbodysize = UINT_FAST32_MAX; + } + + return 0; } static int jpc_enc_encodemainbody(jpc_enc_t *enc) { - int tileno; - int tilex; - int tiley; - int i; - jpc_sot_t *sot; - jpc_enc_tcmpt_t *comp; - jpc_enc_tcmpt_t *endcomps; - jpc_enc_band_t *band; - jpc_enc_band_t *endbands; - jpc_enc_rlvl_t *lvl; - int rlvlno; - jpc_qcc_t *qcc; - jpc_cod_t *cod; - int adjust; - int j; - int absbandno; - long numbytes; - long tilehdrlen; - long tilelen; - jpc_enc_tile_t *tile; - jpc_enc_cp_t *cp; - double rho; - uint_fast16_t lyrno; - uint_fast16_t cmptno; - int samestepsizes; - jpc_enc_ccp_t *ccps; - jpc_enc_tccp_t *tccp; + int tileno; + int tilex; + int tiley; + int i; + jpc_sot_t *sot; + jpc_enc_tcmpt_t *comp; + jpc_enc_tcmpt_t *endcomps; + jpc_enc_band_t *band; + jpc_enc_band_t *endbands; + jpc_enc_rlvl_t *lvl; + int rlvlno; + jpc_qcc_t *qcc; + jpc_cod_t *cod; + int adjust; + int j; + int absbandno; + long numbytes; + long tilehdrlen; + long tilelen; + jpc_enc_tile_t *tile; + jpc_enc_cp_t *cp; + double rho; + uint_fast16_t lyrno; + uint_fast16_t cmptno; + int samestepsizes; + jpc_enc_ccp_t *ccps; + jpc_enc_tccp_t *tccp; int bandno; uint_fast32_t x; uint_fast32_t y; @@ -1157,248 +1157,248 @@ jpc_fix_t mxmag; jpc_fix_t mag; int numgbits; - cp = enc->cp; - - /* Avoid compile warnings. */ - numbytes = 0; - - for (tileno = 0; tileno < cp->numtiles; ++tileno) { - tilex = tileno % cp->numhtiles; - tiley = tileno / cp->numhtiles; - - if (!(enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno))) { - abort(); - } - - tile = enc->curtile; - - if (jas_getdbglevel() >= 10) { - jpc_enc_dump(enc); - } - - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (cmptno = 0, comp = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno, ++comp) { - if (!cp->ccps[cmptno].sgnd) { - adjust = 1 << (cp->ccps[cmptno].prec - 1); - for (i = 0; i < jas_matrix_numrows(comp->data); ++i) { - for (j = 0; j < jas_matrix_numcols(comp->data); ++j) { - *jas_matrix_getref(comp->data, i, j) -= adjust; - } - } - } - } - - if (!tile->intmode) { - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (comp = tile->tcmpts; comp != endcomps; ++comp) { - jas_matrix_asl(comp->data, JPC_FIX_FRACBITS); - } - } - - switch (tile->mctid) { - case JPC_MCT_RCT: + cp = enc->cp; + + /* Avoid compile warnings. */ + numbytes = 0; + + for (tileno = 0; tileno < cp->numtiles; ++tileno) { + tilex = tileno % cp->numhtiles; + tiley = tileno / cp->numhtiles; + + if (!(enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno))) { + abort(); + } + + tile = enc->curtile; + + if (jas_getdbglevel() >= 10) { + jpc_enc_dump(enc); + } + + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (cmptno = 0, comp = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno, ++comp) { + if (!cp->ccps[cmptno].sgnd) { + adjust = 1 << (cp->ccps[cmptno].prec - 1); + for (i = 0; i < jas_matrix_numrows(comp->data); ++i) { + for (j = 0; j < jas_matrix_numcols(comp->data); ++j) { + *jas_matrix_getref(comp->data, i, j) -= adjust; + } + } + } + } + + if (!tile->intmode) { + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + jas_matrix_asl(comp->data, JPC_FIX_FRACBITS); + } + } + + switch (tile->mctid) { + case JPC_MCT_RCT: assert(jas_image_numcmpts(enc->image) == 3); - jpc_rct(tile->tcmpts[0].data, tile->tcmpts[1].data, - tile->tcmpts[2].data); - break; - case JPC_MCT_ICT: + jpc_rct(tile->tcmpts[0].data, tile->tcmpts[1].data, + tile->tcmpts[2].data); + break; + case JPC_MCT_ICT: assert(jas_image_numcmpts(enc->image) == 3); - jpc_ict(tile->tcmpts[0].data, tile->tcmpts[1].data, - tile->tcmpts[2].data); - break; - default: - break; - } - - for (i = 0; i < jas_image_numcmpts(enc->image); ++i) { - comp = &tile->tcmpts[i]; - jpc_tsfb_analyze(comp->tsfb, ((comp->qmfbid == JPC_COX_RFT) ? JPC_TSFB_RITIMODE : 0), comp->data); - - } - - - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (cmptno = 0, comp = tile->tcmpts; comp != endcomps; ++cmptno, ++comp) { - mingbits = 0; - absbandno = 0; - /* All bands must have a corresponding quantizer step size, - even if they contain no samples and are never coded. */ - /* Some bands may not be hit by the loop below, so we must - initialize all of the step sizes to a sane value. */ - memset(comp->stepsizes, 0, sizeof(comp->stepsizes)); - for (rlvlno = 0, lvl = comp->rlvls; rlvlno < comp->numrlvls; ++rlvlno, ++lvl) { - if (!lvl->bands) { - absbandno += rlvlno ? 3 : 1; - continue; - } - endbands = &lvl->bands[lvl->numbands]; - for (band = lvl->bands; band != endbands; ++band) { - if (!band->data) { - ++absbandno; - continue; - } - actualnumbps = 0; - mxmag = 0; - for (y = 0; y < jas_matrix_numrows(band->data); ++y) { - for (x = 0; x < jas_matrix_numcols(band->data); ++x) { - mag = abs(jas_matrix_get(band->data, y, x)); - if (mag > mxmag) { - mxmag = mag; - } - } - } - if (tile->intmode) { - actualnumbps = jpc_firstone(mxmag) + 1; - } else { - actualnumbps = jpc_firstone(mxmag) + 1 - JPC_FIX_FRACBITS; - } - numgbits = actualnumbps - (cp->ccps[cmptno].prec - 1 + - band->analgain); + jpc_ict(tile->tcmpts[0].data, tile->tcmpts[1].data, + tile->tcmpts[2].data); + break; + default: + break; + } + + for (i = 0; i < jas_image_numcmpts(enc->image); ++i) { + comp = &tile->tcmpts[i]; + jpc_tsfb_analyze(comp->tsfb, ((comp->qmfbid == JPC_COX_RFT) ? JPC_TSFB_RITIMODE : 0), comp->data); + + } + + + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (cmptno = 0, comp = tile->tcmpts; comp != endcomps; ++cmptno, ++comp) { + mingbits = 0; + absbandno = 0; + /* All bands must have a corresponding quantizer step size, + even if they contain no samples and are never coded. */ + /* Some bands may not be hit by the loop below, so we must + initialize all of the step sizes to a sane value. */ + memset(comp->stepsizes, 0, sizeof(comp->stepsizes)); + for (rlvlno = 0, lvl = comp->rlvls; rlvlno < comp->numrlvls; ++rlvlno, ++lvl) { + if (!lvl->bands) { + absbandno += rlvlno ? 3 : 1; + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + ++absbandno; + continue; + } + actualnumbps = 0; + mxmag = 0; + for (y = 0; y < jas_matrix_numrows(band->data); ++y) { + for (x = 0; x < jas_matrix_numcols(band->data); ++x) { + mag = abs(jas_matrix_get(band->data, y, x)); + if (mag > mxmag) { + mxmag = mag; + } + } + } + if (tile->intmode) { + actualnumbps = jpc_firstone(mxmag) + 1; + } else { + actualnumbps = jpc_firstone(mxmag) + 1 - JPC_FIX_FRACBITS; + } + numgbits = actualnumbps - (cp->ccps[cmptno].prec - 1 + + band->analgain); #if 0 fprintf(stderr, "%d %d mag=%d actual=%d numgbits=%d\n", cp->ccps[cmptno].prec, band->analgain, mxmag, actualnumbps, numgbits); #endif - if (numgbits > mingbits) { - mingbits = numgbits; - } - if (!tile->intmode) { - band->absstepsize = jpc_fix_div(jpc_inttofix(1 - << (band->analgain + 1)), - band->synweight); - } else { - band->absstepsize = jpc_inttofix(1); - } - band->stepsize = jpc_abstorelstepsize( - band->absstepsize, cp->ccps[cmptno].prec + - band->analgain); - band->numbps = cp->tccp.numgbits + - JPC_QCX_GETEXPN(band->stepsize) - 1; - - if ((!tile->intmode) && band->data) { - quantize(band->data, band->absstepsize); - } - - comp->stepsizes[absbandno] = band->stepsize; - ++absbandno; - } - } - - assert(JPC_FIX_FRACBITS >= JPC_NUMEXTRABITS); - if (!tile->intmode) { - jas_matrix_divpow2(comp->data, JPC_FIX_FRACBITS - JPC_NUMEXTRABITS); - } else { - jas_matrix_asl(comp->data, JPC_NUMEXTRABITS); - } - } + if (numgbits > mingbits) { + mingbits = numgbits; + } + if (!tile->intmode) { + band->absstepsize = jpc_fix_div(jpc_inttofix(1 + << (band->analgain + 1)), + band->synweight); + } else { + band->absstepsize = jpc_inttofix(1); + } + band->stepsize = jpc_abstorelstepsize( + band->absstepsize, cp->ccps[cmptno].prec + + band->analgain); + band->numbps = cp->tccp.numgbits + + JPC_QCX_GETEXPN(band->stepsize) - 1; + + if ((!tile->intmode) && band->data) { + quantize(band->data, band->absstepsize); + } + + comp->stepsizes[absbandno] = band->stepsize; + ++absbandno; + } + } + + assert(JPC_FIX_FRACBITS >= JPC_NUMEXTRABITS); + if (!tile->intmode) { + jas_matrix_divpow2(comp->data, JPC_FIX_FRACBITS - JPC_NUMEXTRABITS); + } else { + jas_matrix_asl(comp->data, JPC_NUMEXTRABITS); + } + } #if 0 fprintf(stderr, "mingbits %d\n", mingbits); #endif - if (mingbits > cp->tccp.numgbits) { - fprintf(stderr, "error: too few guard bits (need at least %d)\n", - mingbits); - return -1; - } - - if (!(enc->tmpstream = jas_stream_memopen(0, 0))) { - fprintf(stderr, "cannot open tmp file\n"); - return -1; - } - - /* Write the tile header. */ - if (!(enc->mrk = jpc_ms_create(JPC_MS_SOT))) { - return -1; - } - sot = &enc->mrk->parms.sot; - sot->len = 0; - sot->tileno = tileno; - sot->partno = 0; - sot->numparts = 1; - if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write SOT marker\n"); - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; + if (mingbits > cp->tccp.numgbits) { + fprintf(stderr, "error: too few guard bits (need at least %d)\n", + mingbits); + return -1; + } + + if (!(enc->tmpstream = jas_stream_memopen(0, 0))) { + fprintf(stderr, "cannot open tmp file\n"); + return -1; + } + + /* Write the tile header. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_SOT))) { + return -1; + } + sot = &enc->mrk->parms.sot; + sot->len = 0; + sot->tileno = tileno; + sot->partno = 0; + sot->numparts = 1; + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write SOT marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; /************************************************************************/ /************************************************************************/ /************************************************************************/ - tccp = &cp->tccp; - for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) { - comp = &tile->tcmpts[cmptno]; - if (comp->numrlvls != tccp->maxrlvls) { - if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) { - return -1; - } + tccp = &cp->tccp; + for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) { + comp = &tile->tcmpts[cmptno]; + if (comp->numrlvls != tccp->maxrlvls) { + if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) { + return -1; + } /* XXX = this is not really correct. we are using comp #0's precint sizes and other characteristics */ - comp = &tile->tcmpts[0]; - cod = &enc->mrk->parms.cod; - cod->compparms.csty = 0; - cod->compparms.numdlvls = comp->numrlvls - 1; - cod->prg = tile->prg; - cod->numlyrs = tile->numlyrs; - cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(comp->cblkwidthexpn); - cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(comp->cblkheightexpn); - cod->compparms.cblksty = comp->cblksty; - cod->compparms.qmfbid = comp->qmfbid; - cod->mctrans = (tile->mctid != JPC_MCT_NONE); - for (i = 0; i < comp->numrlvls; ++i) { - cod->compparms.rlvls[i].parwidthval = comp->rlvls[i].prcwidthexpn; - cod->compparms.rlvls[i].parheightval = comp->rlvls[i].prcheightexpn; - } - if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - } - } - - for (cmptno = 0, comp = tile->tcmpts; cmptno < cp->numcmpts; ++cmptno, ++comp) { - ccps = &cp->ccps[cmptno]; - if (ccps->numstepsizes == comp->numstepsizes) { - samestepsizes = 1; - for (bandno = 0; bandno < ccps->numstepsizes; ++bandno) { - if (ccps->stepsizes[bandno] != comp->stepsizes[bandno]) { - samestepsizes = 0; - break; - } - } - } else { - samestepsizes = 0; - } - if (!samestepsizes) { - if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) { - return -1; - } - qcc = &enc->mrk->parms.qcc; - qcc->compno = cmptno; - qcc->compparms.numguard = cp->tccp.numgbits; - qcc->compparms.qntsty = (comp->qmfbid == JPC_COX_INS) ? - JPC_QCX_SEQNT : JPC_QCX_NOQNT; - qcc->compparms.numstepsizes = comp->numstepsizes; - qcc->compparms.stepsizes = comp->stepsizes; - if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { - return -1; - } - qcc->compparms.stepsizes = 0; - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - } - } - - /* Write a SOD marker to indicate the end of the tile header. */ - if (!(enc->mrk = jpc_ms_create(JPC_MS_SOD))) { - return -1; - } - if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write SOD marker\n"); - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; + comp = &tile->tcmpts[0]; + cod = &enc->mrk->parms.cod; + cod->compparms.csty = 0; + cod->compparms.numdlvls = comp->numrlvls - 1; + cod->prg = tile->prg; + cod->numlyrs = tile->numlyrs; + cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(comp->cblkwidthexpn); + cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(comp->cblkheightexpn); + cod->compparms.cblksty = comp->cblksty; + cod->compparms.qmfbid = comp->qmfbid; + cod->mctrans = (tile->mctid != JPC_MCT_NONE); + for (i = 0; i < comp->numrlvls; ++i) { + cod->compparms.rlvls[i].parwidthval = comp->rlvls[i].prcwidthexpn; + cod->compparms.rlvls[i].parheightval = comp->rlvls[i].prcheightexpn; + } + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + } + } + + for (cmptno = 0, comp = tile->tcmpts; cmptno < cp->numcmpts; ++cmptno, ++comp) { + ccps = &cp->ccps[cmptno]; + if (ccps->numstepsizes == comp->numstepsizes) { + samestepsizes = 1; + for (bandno = 0; bandno < ccps->numstepsizes; ++bandno) { + if (ccps->stepsizes[bandno] != comp->stepsizes[bandno]) { + samestepsizes = 0; + break; + } + } + } else { + samestepsizes = 0; + } + if (!samestepsizes) { + if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) { + return -1; + } + qcc = &enc->mrk->parms.qcc; + qcc->compno = cmptno; + qcc->compparms.numguard = cp->tccp.numgbits; + qcc->compparms.qntsty = (comp->qmfbid == JPC_COX_INS) ? + JPC_QCX_SEQNT : JPC_QCX_NOQNT; + qcc->compparms.numstepsizes = comp->numstepsizes; + qcc->compparms.stepsizes = comp->stepsizes; + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + return -1; + } + qcc->compparms.stepsizes = 0; + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + } + } + + /* Write a SOD marker to indicate the end of the tile header. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_SOD))) { + return -1; + } + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write SOD marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; tilehdrlen = jas_stream_getrwcount(enc->tmpstream); /************************************************************************/ @@ -1406,43 +1406,43 @@ tilehdrlen = jas_stream_getrwcount(enc->tmpstream); /************************************************************************/ if (jpc_enc_enccblks(enc)) { - abort(); - return -1; + abort(); + return -1; } - cp = enc->cp; - rho = (double) (tile->brx - tile->tlx) * (tile->bry - tile->tly) / - ((cp->refgrdwidth - cp->imgareatlx) * (cp->refgrdheight - - cp->imgareatly)); - tile->rawsize = cp->rawsize * rho; - - for (lyrno = 0; lyrno < tile->numlyrs - 1; ++lyrno) { - tile->lyrsizes[lyrno] = tile->rawsize * jpc_fixtodbl( - cp->tcp.ilyrrates[lyrno]); - } - tile->lyrsizes[tile->numlyrs - 1] = (cp->totalsize != UINT_FAST32_MAX) ? - (rho * enc->mainbodysize) : UINT_FAST32_MAX; - for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { - if (tile->lyrsizes[lyrno] != UINT_FAST32_MAX) { - if (tilehdrlen <= tile->lyrsizes[lyrno]) { - tile->lyrsizes[lyrno] -= tilehdrlen; - } else { - tile->lyrsizes[lyrno] = 0; - } - } - } - - if (rateallocate(enc, tile->numlyrs, tile->lyrsizes)) { - return -1; - } + cp = enc->cp; + rho = (double) (tile->brx - tile->tlx) * (tile->bry - tile->tly) / + ((cp->refgrdwidth - cp->imgareatlx) * (cp->refgrdheight - + cp->imgareatly)); + tile->rawsize = cp->rawsize * rho; + + for (lyrno = 0; lyrno < tile->numlyrs - 1; ++lyrno) { + tile->lyrsizes[lyrno] = tile->rawsize * jpc_fixtodbl( + cp->tcp.ilyrrates[lyrno]); + } + tile->lyrsizes[tile->numlyrs - 1] = (cp->totalsize != UINT_FAST32_MAX) ? + (rho * enc->mainbodysize) : UINT_FAST32_MAX; + for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { + if (tile->lyrsizes[lyrno] != UINT_FAST32_MAX) { + if (tilehdrlen <= tile->lyrsizes[lyrno]) { + tile->lyrsizes[lyrno] -= tilehdrlen; + } else { + tile->lyrsizes[lyrno] = 0; + } + } + } + + if (rateallocate(enc, tile->numlyrs, tile->lyrsizes)) { + return -1; + } #if 0 fprintf(stderr, "ENCODE TILE DATA\n"); #endif - if (jpc_enc_encodetiledata(enc)) { - fprintf(stderr, "dotile failed\n"); - return -1; - } + if (jpc_enc_encodetiledata(enc)) { + fprintf(stderr, "dotile failed\n"); + return -1; + } /************************************************************************/ /************************************************************************/ @@ -1452,466 +1452,474 @@ fprintf(stderr, "ENCODE TILE DATA\n"); /************************************************************************/ /************************************************************************/ - tilelen = jas_stream_tell(enc->tmpstream); + tilelen = jas_stream_tell(enc->tmpstream); - if (jas_stream_seek(enc->tmpstream, 6, SEEK_SET) < 0) { - return -1; - } - jpc_putuint32(enc->tmpstream, tilelen); + if (jas_stream_seek(enc->tmpstream, 6, SEEK_SET) < 0) { + return -1; + } + jpc_putuint32(enc->tmpstream, tilelen); - if (jas_stream_seek(enc->tmpstream, 0, SEEK_SET) < 0) { - return -1; - } - if (jpc_putdata(enc->out, enc->tmpstream, -1)) { - return -1; - } - enc->len += tilelen; + if (jas_stream_seek(enc->tmpstream, 0, SEEK_SET) < 0) { + return -1; + } + if (jpc_putdata(enc->out, enc->tmpstream, -1)) { + return -1; + } + enc->len += tilelen; - jas_stream_close(enc->tmpstream); - enc->tmpstream = 0; + jas_stream_close(enc->tmpstream); + enc->tmpstream = 0; - jpc_enc_tile_destroy(enc->curtile); - enc->curtile = 0; + jpc_enc_tile_destroy(enc->curtile); + enc->curtile = 0; - } + } - return 0; + return 0; } int jpc_enc_encodetiledata(jpc_enc_t *enc) { assert(enc->tmpstream); - if (jpc_enc_encpkts(enc, enc->tmpstream)) { - return -1; - } - return 0; + if (jpc_enc_encpkts(enc, enc->tmpstream)) { + return -1; + } + return 0; } void quantize(jas_matrix_t *data, jpc_fix_t stepsize) { - int i; - int j; - jpc_fix_t t; + int i; + int j; + jpc_fix_t t; - if (stepsize == jpc_inttofix(1)) { - return; - } + if (stepsize == jpc_inttofix(1)) { + return; + } - for (i = 0; i < jas_matrix_numrows(data); ++i) { - for (j = 0; j < jas_matrix_numcols(data); ++j) { - t = jas_matrix_get(data, i, j); + for (i = 0; i < jas_matrix_numrows(data); ++i) { + for (j = 0; j < jas_matrix_numcols(data); ++j) { + t = jas_matrix_get(data, i, j); { - if (t < 0) { - t = jpc_fix_neg(jpc_fix_div(jpc_fix_neg(t), stepsize)); - } else { - t = jpc_fix_div(t, stepsize); - } + if (t < 0) { + t = jpc_fix_neg(jpc_fix_div(jpc_fix_neg(t), stepsize)); + } else { + t = jpc_fix_div(t, stepsize); + } } - jas_matrix_set(data, i, j, t); - } - } + jas_matrix_set(data, i, j, t); + } + } } static void calcrdslopes(jpc_enc_cblk_t *cblk) { - jpc_enc_pass_t *endpasses; - jpc_enc_pass_t *pass0; - jpc_enc_pass_t *pass1; - jpc_enc_pass_t *pass2; - jpc_flt_t slope0; - jpc_flt_t slope; - jpc_flt_t dd; - long dr; - - endpasses = &cblk->passes[cblk->numpasses]; - pass2 = cblk->passes; - slope0 = 0; - while (pass2 != endpasses) { - pass0 = 0; - for (pass1 = cblk->passes; pass1 != endpasses; ++pass1) { - dd = pass1->cumwmsedec; - dr = pass1->end; - if (pass0) { - dd -= pass0->cumwmsedec; - dr -= pass0->end; - } - if (dd <= 0) { - pass1->rdslope = JPC_BADRDSLOPE; - if (pass1 >= pass2) { - pass2 = &pass1[1]; - } - continue; - } - if (pass1 < pass2 && pass1->rdslope <= 0) { - continue; - } - if (!dr) { - assert(pass0); - pass0->rdslope = 0; - break; - } - slope = dd / dr; - if (pass0 && slope >= slope0) { - pass0->rdslope = 0; - break; - } - pass1->rdslope = slope; - if (pass1 >= pass2) { - pass2 = &pass1[1]; - } - pass0 = pass1; - slope0 = slope; - } - } + jpc_enc_pass_t *endpasses; + jpc_enc_pass_t *pass0; + jpc_enc_pass_t *pass1; + jpc_enc_pass_t *pass2; + jpc_flt_t slope0; + jpc_flt_t slope; + jpc_flt_t dd; + long dr; + + endpasses = &cblk->passes[cblk->numpasses]; + pass2 = cblk->passes; + slope0 = 0; + while (pass2 != endpasses) { + pass0 = 0; + for (pass1 = cblk->passes; pass1 != endpasses; ++pass1) { + dd = pass1->cumwmsedec; + dr = pass1->end; + if (pass0) { + dd -= pass0->cumwmsedec; + dr -= pass0->end; + } + if (dd <= 0) { + pass1->rdslope = JPC_BADRDSLOPE; + if (pass1 >= pass2) { + pass2 = &pass1[1]; + } + continue; + } + if (pass1 < pass2 && pass1->rdslope <= 0) { + continue; + } + if (!dr) { + assert(pass0); + pass0->rdslope = 0; + break; + } + slope = dd / dr; + if (pass0 && slope >= slope0) { + pass0->rdslope = 0; + break; + } + pass1->rdslope = slope; + if (pass1 >= pass2) { + pass2 = &pass1[1]; + } + pass0 = pass1; + slope0 = slope; + } + } #if 0 - for (pass0 = cblk->passes; pass0 != endpasses; ++pass0) { + for (pass0 = cblk->passes; pass0 != endpasses; ++pass0) { if (pass0->rdslope > 0.0) { - fprintf(stderr, "pass %02d nmsedec=%lf dec=%lf end=%d %lf\n", pass0 - cblk->passes, - fixtodbl(pass0->nmsedec), pass0->wmsedec, pass0->end, pass0->rdslope); + fprintf(stderr, "pass %02d nmsedec=%lf dec=%lf end=%d %lf\n", pass0 - cblk->passes, + fixtodbl(pass0->nmsedec), pass0->wmsedec, pass0->end, pass0->rdslope); } - } + } #endif } static void dump_layeringinfo(jpc_enc_t *enc) { - jpc_enc_tcmpt_t *tcmpt; - uint_fast16_t tcmptno; - jpc_enc_rlvl_t *rlvl; - uint_fast16_t rlvlno; - jpc_enc_band_t *band; - uint_fast16_t bandno; - jpc_enc_prc_t *prc; - uint_fast32_t prcno; - jpc_enc_cblk_t *cblk; - uint_fast16_t cblkno; - jpc_enc_pass_t *pass; - uint_fast16_t passno; - int lyrno; - jpc_enc_tile_t *tile; - - tile = enc->curtile; - - for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { - fprintf(stderr, "lyrno = %02d\n", lyrno); - for (tcmptno = 0, tcmpt = tile->tcmpts; tcmptno < tile->numtcmpts; - ++tcmptno, ++tcmpt) { - for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; - ++rlvlno, ++rlvl) { - if (!rlvl->bands) { - continue; - } - for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { - if (!band->data) { - continue; - } - for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; - ++prcno, ++prc) { - if (!prc->cblks) { - continue; - } - for (cblkno = 0, cblk = prc->cblks; cblkno < - prc->numcblks; ++cblkno, ++cblk) { - for (passno = 0, pass = cblk->passes; passno < - cblk->numpasses && pass->lyrno == lyrno; - ++passno, ++pass) { - fprintf(stderr, "lyrno=%02d cmptno=%02d rlvlno=%02d bandno=%02d prcno=%02d cblkno=%03d passno=%03d\n", lyrno, tcmptno, rlvlno, bandno, prcno, cblkno, passno); - } - } - } - } - } - } - } + jpc_enc_tcmpt_t *tcmpt; + uint_fast16_t tcmptno; + jpc_enc_rlvl_t *rlvl; + uint_fast16_t rlvlno; + jpc_enc_band_t *band; + uint_fast16_t bandno; + jpc_enc_prc_t *prc; + uint_fast32_t prcno; + jpc_enc_cblk_t *cblk; + uint_fast16_t cblkno; + jpc_enc_pass_t *pass; + uint_fast16_t passno; + int lyrno; + jpc_enc_tile_t *tile; + + tile = enc->curtile; + + for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { + fprintf(stderr, "lyrno = %02d\n", lyrno); + for (tcmptno = 0, tcmpt = tile->tcmpts; tcmptno < tile->numtcmpts; + ++tcmptno, ++tcmpt) { + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + if (!rlvl->bands) { + continue; + } + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; + ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + for (cblkno = 0, cblk = prc->cblks; cblkno < + prc->numcblks; ++cblkno, ++cblk) { + for (passno = 0, pass = cblk->passes; + passno < cblk->numpasses && + pass->lyrno == lyrno; + ++passno, ++pass) { + fprintf(stderr, + "lyrno=%02d cmptno=%02d " + "rlvlno=%02d bandno=%02d " + "prcno=%02d cblkno=%03d " + "passno=%03d\n", + lyrno, (int)tcmptno, (int)rlvlno, + (int)bandno, (int)prcno, (int) cblkno, + (int)passno); + } + } + } + } + } + } + } } int rateallocate(jpc_enc_t *enc, int numlyrs, uint_fast32_t *cumlens) { - jpc_flt_t lo; - jpc_flt_t hi; - jas_stream_t *out; - long cumlen; - int lyrno; - jpc_flt_t thresh; - jpc_flt_t goodthresh; - int success; - long pos; - long oldpos; - int numiters; - - jpc_enc_tcmpt_t *comp; - jpc_enc_tcmpt_t *endcomps; - jpc_enc_rlvl_t *lvl; - jpc_enc_rlvl_t *endlvls; - jpc_enc_band_t *band; - jpc_enc_band_t *endbands; - jpc_enc_cblk_t *cblk; - jpc_enc_cblk_t *endcblks; - jpc_enc_pass_t *pass; - jpc_enc_pass_t *endpasses; - jpc_enc_pass_t *pass1; - jpc_flt_t mxrdslope; - jpc_flt_t mnrdslope; - jpc_enc_tile_t *tile; - jpc_enc_prc_t *prc; - uint_fast32_t prcno; - - tile = enc->curtile; - - for (lyrno = 1; lyrno < numlyrs - 1; ++lyrno) { - if (cumlens[lyrno - 1] > cumlens[lyrno]) { - abort(); - } - } - - if (!(out = jas_stream_memopen(0, 0))) { - return -1; - } - - - /* Find minimum and maximum R-D slope values. */ - mnrdslope = DBL_MAX; - mxrdslope = 0; - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (comp = tile->tcmpts; comp != endcomps; ++comp) { - endlvls = &comp->rlvls[comp->numrlvls]; - for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { - if (!lvl->bands) { - continue; - } - endbands = &lvl->bands[lvl->numbands]; - for (band = lvl->bands; band != endbands; ++band) { - if (!band->data) { - continue; - } - for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { - if (!prc->cblks) { - continue; - } - endcblks = &prc->cblks[prc->numcblks]; - for (cblk = prc->cblks; cblk != endcblks; ++cblk) { - calcrdslopes(cblk); - endpasses = &cblk->passes[cblk->numpasses]; - for (pass = cblk->passes; pass != endpasses; ++pass) { - if (pass->rdslope > 0) { - if (pass->rdslope < mnrdslope) { - mnrdslope = pass->rdslope; - } - if (pass->rdslope > mxrdslope) { - mxrdslope = pass->rdslope; - } - } - } - } - } - } - } - } + jpc_flt_t lo; + jpc_flt_t hi; + jas_stream_t *out; + long cumlen; + int lyrno; + jpc_flt_t thresh; + jpc_flt_t goodthresh; + int success; + long pos; + long oldpos; + int numiters; + + jpc_enc_tcmpt_t *comp; + jpc_enc_tcmpt_t *endcomps; + jpc_enc_rlvl_t *lvl; + jpc_enc_rlvl_t *endlvls; + jpc_enc_band_t *band; + jpc_enc_band_t *endbands; + jpc_enc_cblk_t *cblk; + jpc_enc_cblk_t *endcblks; + jpc_enc_pass_t *pass; + jpc_enc_pass_t *endpasses; + jpc_enc_pass_t *pass1; + jpc_flt_t mxrdslope; + jpc_flt_t mnrdslope; + jpc_enc_tile_t *tile; + jpc_enc_prc_t *prc; + uint_fast32_t prcno; + + tile = enc->curtile; + + for (lyrno = 1; lyrno < numlyrs - 1; ++lyrno) { + if (cumlens[lyrno - 1] > cumlens[lyrno]) { + abort(); + } + } + + if (!(out = jas_stream_memopen(0, 0))) { + return -1; + } + + + /* Find minimum and maximum R-D slope values. */ + mnrdslope = DBL_MAX; + mxrdslope = 0; + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + calcrdslopes(cblk); + endpasses = &cblk->passes[cblk->numpasses]; + for (pass = cblk->passes; pass != endpasses; ++pass) { + if (pass->rdslope > 0) { + if (pass->rdslope < mnrdslope) { + mnrdslope = pass->rdslope; + } + if (pass->rdslope > mxrdslope) { + mxrdslope = pass->rdslope; + } + } + } + } + } + } + } + } if (jas_getdbglevel()) { - fprintf(stderr, "min rdslope = %f max rdslope = %f\n", mnrdslope, mxrdslope); + fprintf(stderr, "min rdslope = %f max rdslope = %f\n", mnrdslope, mxrdslope); } - jpc_init_t2state(enc, 1); - - for (lyrno = 0; lyrno < numlyrs; ++lyrno) { - - lo = mnrdslope; - hi = mxrdslope; - - success = 0; - goodthresh = 0; - numiters = 0; - - do { - - cumlen = cumlens[lyrno]; - if (cumlen == UINT_FAST32_MAX) { - /* Only the last layer can be free of a rate - constraint (e.g., for lossless coding). */ - assert(lyrno == numlyrs - 1); - goodthresh = -1; - success = 1; - break; - } - - thresh = (lo + hi) / 2; - - /* Save the tier 2 coding state. */ - jpc_save_t2state(enc); - oldpos = jas_stream_tell(out); - assert(oldpos >= 0); - - /* Assign all passes with R-D slopes greater than or - equal to the current threshold to this layer. */ - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (comp = tile->tcmpts; comp != endcomps; ++comp) { - endlvls = &comp->rlvls[comp->numrlvls]; - for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { - if (!lvl->bands) { - continue; - } - endbands = &lvl->bands[lvl->numbands]; - for (band = lvl->bands; band != endbands; ++band) { - if (!band->data) { - continue; - } - for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { - if (!prc->cblks) { - continue; - } - endcblks = &prc->cblks[prc->numcblks]; - for (cblk = prc->cblks; cblk != endcblks; ++cblk) { - if (cblk->curpass) { - endpasses = &cblk->passes[cblk->numpasses]; - pass1 = cblk->curpass; - for (pass = cblk->curpass; pass != endpasses; ++pass) { - if (pass->rdslope >= thresh) { - pass1 = &pass[1]; - } - } - for (pass = cblk->curpass; pass != pass1; ++pass) { - pass->lyrno = lyrno; - } - for (; pass != endpasses; ++pass) { - pass->lyrno = -1; - } - } - } - } - } - } - } - - /* Perform tier 2 coding. */ - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (comp = tile->tcmpts; comp != endcomps; ++comp) { - endlvls = &comp->rlvls[comp->numrlvls]; - for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { - if (!lvl->bands) { - continue; - } - for (prcno = 0; prcno < lvl->numprcs; ++prcno) { - if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) { - return -1; - } - } - } - } - - pos = jas_stream_tell(out); - - /* Check the rate constraint. */ - assert(pos >= 0); - if (pos > cumlen) { - /* The rate is too high. */ - lo = thresh; - } else if (pos <= cumlen) { - /* The rate is low enough, so try higher. */ - hi = thresh; - if (!success || thresh < goodthresh) { - goodthresh = thresh; - success = 1; - } - } - - /* Save the tier 2 coding state. */ - jpc_restore_t2state(enc); - if (jas_stream_seek(out, oldpos, SEEK_SET) < 0) { - abort(); - } + jpc_init_t2state(enc, 1); + + for (lyrno = 0; lyrno < numlyrs; ++lyrno) { + + lo = mnrdslope; + hi = mxrdslope; + + success = 0; + goodthresh = 0; + numiters = 0; + + do { + + cumlen = cumlens[lyrno]; + if (cumlen == UINT_FAST32_MAX) { + /* Only the last layer can be free of a rate + constraint (e.g., for lossless coding). */ + assert(lyrno == numlyrs - 1); + goodthresh = -1; + success = 1; + break; + } + + thresh = (lo + hi) / 2; + + /* Save the tier 2 coding state. */ + jpc_save_t2state(enc); + oldpos = jas_stream_tell(out); + assert(oldpos >= 0); + + /* Assign all passes with R-D slopes greater than or + equal to the current threshold to this layer. */ + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + if (cblk->curpass) { + endpasses = &cblk->passes[cblk->numpasses]; + pass1 = cblk->curpass; + for (pass = cblk->curpass; pass != endpasses; ++pass) { + if (pass->rdslope >= thresh) { + pass1 = &pass[1]; + } + } + for (pass = cblk->curpass; pass != pass1; ++pass) { + pass->lyrno = lyrno; + } + for (; pass != endpasses; ++pass) { + pass->lyrno = -1; + } + } + } + } + } + } + } + + /* Perform tier 2 coding. */ + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + for (prcno = 0; prcno < lvl->numprcs; ++prcno) { + if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) { + return -1; + } + } + } + } + + pos = jas_stream_tell(out); + + /* Check the rate constraint. */ + assert(pos >= 0); + if (pos > cumlen) { + /* The rate is too high. */ + lo = thresh; + } else if (pos <= cumlen) { + /* The rate is low enough, so try higher. */ + hi = thresh; + if (!success || thresh < goodthresh) { + goodthresh = thresh; + success = 1; + } + } + + /* Save the tier 2 coding state. */ + jpc_restore_t2state(enc); + if (jas_stream_seek(out, oldpos, SEEK_SET) < 0) { + abort(); + } if (jas_getdbglevel()) { fprintf(stderr, "maxlen=%08ld actuallen=%08ld thresh=%f\n", cumlen, pos, thresh); } - ++numiters; - } while (lo < hi - 1e-3 && numiters < 32); + ++numiters; + } while (lo < hi - 1e-3 && numiters < 32); - if (!success) { - fprintf(stderr, "warning: empty layer generated\n"); - } + if (!success) { + fprintf(stderr, "warning: empty layer generated\n"); + } if (jas_getdbglevel()) { fprintf(stderr, "success %d goodthresh %f\n", success, goodthresh); } - /* Assign all passes with R-D slopes greater than or - equal to the selected threshold to this layer. */ - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (comp = tile->tcmpts; comp != endcomps; ++comp) { - endlvls = &comp->rlvls[comp->numrlvls]; - for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + /* Assign all passes with R-D slopes greater than or + equal to the selected threshold to this layer. */ + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { if (!lvl->bands) { - continue; + continue; } - endbands = &lvl->bands[lvl->numbands]; - for (band = lvl->bands; band != endbands; ++band) { - if (!band->data) { - continue; - } - for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { - if (!prc->cblks) { - continue; - } - endcblks = &prc->cblks[prc->numcblks]; - for (cblk = prc->cblks; cblk != endcblks; ++cblk) { - if (cblk->curpass) { - endpasses = &cblk->passes[cblk->numpasses]; - pass1 = cblk->curpass; - if (success) { - for (pass = cblk->curpass; pass != endpasses; ++pass) { - if (pass->rdslope >= goodthresh) { - pass1 = &pass[1]; - } - } - } - for (pass = cblk->curpass; pass != pass1; ++pass) { - pass->lyrno = lyrno; - } - for (; pass != endpasses; ++pass) { - pass->lyrno = -1; - } - } - } - } - } - } - } - - /* Perform tier 2 coding. */ - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (comp = tile->tcmpts; comp != endcomps; ++comp) { - endlvls = &comp->rlvls[comp->numrlvls]; - for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { - if (!lvl->bands) { - continue; - } - for (prcno = 0; prcno < lvl->numprcs; ++prcno) { - if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) { - return -1; - } - } - } - } - } - - if (jas_getdbglevel() >= 5) { - dump_layeringinfo(enc); - } - - jas_stream_close(out); - - JAS_DBGLOG(10, ("done doing rateallocation\n")); + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + if (cblk->curpass) { + endpasses = &cblk->passes[cblk->numpasses]; + pass1 = cblk->curpass; + if (success) { + for (pass = cblk->curpass; pass != endpasses; ++pass) { + if (pass->rdslope >= goodthresh) { + pass1 = &pass[1]; + } + } + } + for (pass = cblk->curpass; pass != pass1; ++pass) { + pass->lyrno = lyrno; + } + for (; pass != endpasses; ++pass) { + pass->lyrno = -1; + } + } + } + } + } + } + } + + /* Perform tier 2 coding. */ + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + for (prcno = 0; prcno < lvl->numprcs; ++prcno) { + if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) { + return -1; + } + } + } + } + } + + if (jas_getdbglevel() >= 5) { + dump_layeringinfo(enc); + } + + jas_stream_close(out); + + JAS_DBGLOG(10, ("done doing rateallocation\n")); #if 0 fprintf(stderr, "DONE RATE ALLOCATE\n"); #endif - return 0; + return 0; } /******************************************************************************\ @@ -1920,705 +1928,729 @@ fprintf(stderr, "DONE RATE ALLOCATE\n"); jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno) { - jpc_enc_tile_t *tile; - uint_fast32_t htileno; - uint_fast32_t vtileno; - uint_fast16_t lyrno; - uint_fast16_t cmptno; - jpc_enc_tcmpt_t *tcmpt; - - if (!(tile = jas_malloc(sizeof(jpc_enc_tile_t)))) { - goto error; - } - - /* Initialize a few members used in error recovery. */ - tile->tcmpts = 0; - tile->lyrsizes = 0; - tile->numtcmpts = cp->numcmpts; - tile->pi = 0; - - tile->tileno = tileno; - htileno = tileno % cp->numhtiles; - vtileno = tileno / cp->numhtiles; - - /* Calculate the coordinates of the top-left and bottom-right - corners of the tile. */ - tile->tlx = JAS_MAX(cp->tilegrdoffx + htileno * cp->tilewidth, - cp->imgareatlx); - tile->tly = JAS_MAX(cp->tilegrdoffy + vtileno * cp->tileheight, - cp->imgareatly); - tile->brx = JAS_MIN(cp->tilegrdoffx + (htileno + 1) * cp->tilewidth, - cp->refgrdwidth); - tile->bry = JAS_MIN(cp->tilegrdoffy + (vtileno + 1) * cp->tileheight, - cp->refgrdheight); - - /* Initialize some tile coding parameters. */ - tile->intmode = cp->tcp.intmode; - tile->csty = cp->tcp.csty; - tile->prg = cp->tcp.prg; - tile->mctid = cp->tcp.mctid; - - tile->numlyrs = cp->tcp.numlyrs; - if (!(tile->lyrsizes = jas_malloc(tile->numlyrs * - sizeof(uint_fast32_t)))) { - goto error; - } - for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { - tile->lyrsizes[lyrno] = 0; - } - - /* Allocate an array for the per-tile-component information. */ - if (!(tile->tcmpts = jas_malloc(cp->numcmpts * sizeof(jpc_enc_tcmpt_t)))) { - goto error; - } - /* Initialize a few members critical for error recovery. */ - for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; - ++cmptno, ++tcmpt) { - tcmpt->rlvls = 0; - tcmpt->tsfb = 0; - tcmpt->data = 0; - } - /* Initialize the per-tile-component information. */ - for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; - ++cmptno, ++tcmpt) { - if (!tcmpt_create(tcmpt, cp, image, tile)) { - goto error; - } - } - - /* Initialize the synthesis weights for the MCT. */ - switch (tile->mctid) { - case JPC_MCT_RCT: - tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0)); - tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(0.6875)); - tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(0.6875)); - break; - case JPC_MCT_ICT: - tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0000)); - tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(3.2584)); - tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(2.4755)); - break; - default: - case JPC_MCT_NONE: - for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; - ++cmptno, ++tcmpt) { - tcmpt->synweight = JPC_FIX_ONE; - } - break; - } - - if (!(tile->pi = jpc_enc_pi_create(cp, tile))) { - goto error; - } - - return tile; + jpc_enc_tile_t *tile; + uint_fast32_t htileno; + uint_fast32_t vtileno; + uint_fast16_t lyrno; + uint_fast16_t cmptno; + jpc_enc_tcmpt_t *tcmpt; + + if (!(tile = jas_malloc(sizeof(jpc_enc_tile_t)))) { + goto error; + } + + /* Initialize a few members used in error recovery. */ + tile->tcmpts = 0; + tile->lyrsizes = 0; + tile->numtcmpts = cp->numcmpts; + tile->pi = 0; + + tile->tileno = tileno; + htileno = tileno % cp->numhtiles; + vtileno = tileno / cp->numhtiles; + + /* Calculate the coordinates of the top-left and bottom-right + corners of the tile. */ + tile->tlx = JAS_MAX(cp->tilegrdoffx + htileno * cp->tilewidth, + cp->imgareatlx); + tile->tly = JAS_MAX(cp->tilegrdoffy + vtileno * cp->tileheight, + cp->imgareatly); + tile->brx = JAS_MIN(cp->tilegrdoffx + (htileno + 1) * cp->tilewidth, + cp->refgrdwidth); + tile->bry = JAS_MIN(cp->tilegrdoffy + (vtileno + 1) * cp->tileheight, + cp->refgrdheight); + + /* Initialize some tile coding parameters. */ + tile->intmode = cp->tcp.intmode; + tile->csty = cp->tcp.csty; + tile->prg = cp->tcp.prg; + tile->mctid = cp->tcp.mctid; + + tile->numlyrs = cp->tcp.numlyrs; + if (!(tile->lyrsizes = jas_malloc(tile->numlyrs * + sizeof(uint_fast32_t)))) { + goto error; + } + for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { + tile->lyrsizes[lyrno] = 0; + } + + /* Allocate an array for the per-tile-component information. */ + if (!(tile->tcmpts = jas_malloc(cp->numcmpts * sizeof(jpc_enc_tcmpt_t)))) { + goto error; + } + /* Initialize a few members critical for error recovery. */ + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; + ++cmptno, ++tcmpt) { + tcmpt->rlvls = 0; + tcmpt->tsfb = 0; + tcmpt->data = 0; + } + /* Initialize the per-tile-component information. */ + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; + ++cmptno, ++tcmpt) { + if (!tcmpt_create(tcmpt, cp, image, tile)) { + goto error; + } + } + + /* Initialize the synthesis weights for the MCT. */ + switch (tile->mctid) { + case JPC_MCT_RCT: + tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0)); + tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(0.6875)); + tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(0.6875)); + break; + case JPC_MCT_ICT: + tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0000)); + tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(3.2584)); + tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(2.4755)); + break; + default: + case JPC_MCT_NONE: + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; + ++cmptno, ++tcmpt) { + tcmpt->synweight = JPC_FIX_ONE; + } + break; + } + + if (!(tile->pi = jpc_enc_pi_create(cp, tile))) { + goto error; + } + + return tile; error: - if (tile) { - jpc_enc_tile_destroy(tile); - } - return 0; + if (tile) { + jpc_enc_tile_destroy(tile); + } + return 0; } void jpc_enc_tile_destroy(jpc_enc_tile_t *tile) { - jpc_enc_tcmpt_t *tcmpt; - uint_fast16_t cmptno; - - if (tile->tcmpts) { - for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < - tile->numtcmpts; ++cmptno, ++tcmpt) { - tcmpt_destroy(tcmpt); - } - jas_free(tile->tcmpts); - } - if (tile->lyrsizes) { - jas_free(tile->lyrsizes); - } - if (tile->pi) { - jpc_pi_destroy(tile->pi); - } - jas_free(tile); + jpc_enc_tcmpt_t *tcmpt; + uint_fast16_t cmptno; + + if (tile->tcmpts) { + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < + tile->numtcmpts; ++cmptno, ++tcmpt) { + tcmpt_destroy(tcmpt); + } + jas_free(tile->tcmpts); + } + if (tile->lyrsizes) { + jas_free(tile->lyrsizes); + } + if (tile->pi) { + jpc_pi_destroy(tile->pi); + } + jas_free(tile); } static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp, jas_image_t *image, jpc_enc_tile_t *tile) { - uint_fast16_t cmptno; - uint_fast16_t rlvlno; - jpc_enc_rlvl_t *rlvl; - uint_fast32_t tlx; - uint_fast32_t tly; - uint_fast32_t brx; - uint_fast32_t bry; - uint_fast32_t cmpttlx; - uint_fast32_t cmpttly; - jpc_enc_ccp_t *ccp; - jpc_tsfb_band_t bandinfos[JPC_MAXBANDS]; - - tcmpt->tile = tile; - tcmpt->tsfb = 0; - tcmpt->data = 0; - tcmpt->rlvls = 0; - - /* Deduce the component number. */ - cmptno = tcmpt - tile->tcmpts; - - ccp = &cp->ccps[cmptno]; - - /* Compute the coordinates of the top-left and bottom-right - corners of this tile-component. */ - tlx = JPC_CEILDIV(tile->tlx, ccp->sampgrdstepx); - tly = JPC_CEILDIV(tile->tly, ccp->sampgrdstepy); - brx = JPC_CEILDIV(tile->brx, ccp->sampgrdstepx); - bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy); - - /* Create a sequence to hold the tile-component sample data. */ - if (!(tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry))) { - goto error; - } - - /* Get the image data associated with this tile-component. */ - cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx); - cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy); - if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly, - brx - tlx, bry - tly, tcmpt->data)) { - goto error; - } - - tcmpt->synweight = 0; - tcmpt->qmfbid = cp->tccp.qmfbid; - tcmpt->numrlvls = cp->tccp.maxrlvls; - tcmpt->numbands = 3 * tcmpt->numrlvls - 2; - if (!(tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1))) { - goto error; - } - - for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) { - tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno]; - tcmpt->prcheightexpns[rlvlno] = cp->tccp.prcheightexpns[rlvlno]; - } - tcmpt->cblkwidthexpn = cp->tccp.cblkwidthexpn; - tcmpt->cblkheightexpn = cp->tccp.cblkheightexpn; - tcmpt->cblksty = cp->tccp.cblksty; - tcmpt->csty = cp->tccp.csty; - - tcmpt->numstepsizes = tcmpt->numbands; - assert(tcmpt->numstepsizes <= JPC_MAXBANDS); - memset(tcmpt->stepsizes, 0, sizeof(tcmpt->numstepsizes * - sizeof(uint_fast16_t))); - - /* Retrieve information about the various bands. */ - jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data), - jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data), - jas_seq2d_yend(tcmpt->data), bandinfos); - - if (!(tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)))) { - goto error; - } - for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; - ++rlvlno, ++rlvl) { - rlvl->bands = 0; - rlvl->tcmpt = tcmpt; - } - for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; - ++rlvlno, ++rlvl) { - if (!rlvl_create(rlvl, cp, tcmpt, bandinfos)) { - goto error; - } - } - - return tcmpt; + uint_fast16_t cmptno; + uint_fast16_t rlvlno; + jpc_enc_rlvl_t *rlvl; + uint_fast32_t tlx; + uint_fast32_t tly; + uint_fast32_t brx; + uint_fast32_t bry; + uint_fast32_t cmpttlx; + uint_fast32_t cmpttly; + jpc_enc_ccp_t *ccp; + jpc_tsfb_band_t bandinfos[JPC_MAXBANDS]; + + tcmpt->tile = tile; + tcmpt->tsfb = 0; + tcmpt->data = 0; + tcmpt->rlvls = 0; + + /* Deduce the component number. */ + cmptno = tcmpt - tile->tcmpts; + + ccp = &cp->ccps[cmptno]; + + /* Compute the coordinates of the top-left and bottom-right + corners of this tile-component. */ + tlx = JPC_CEILDIV(tile->tlx, ccp->sampgrdstepx); + tly = JPC_CEILDIV(tile->tly, ccp->sampgrdstepy); + brx = JPC_CEILDIV(tile->brx, ccp->sampgrdstepx); + bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy); + + /* Create a sequence to hold the tile-component sample data. */ + if (!(tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry))) { + goto error; + } + + /* Get the image data associated with this tile-component. */ + cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx); + cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy); + if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly, + brx - tlx, bry - tly, tcmpt->data)) { + goto error; + } + + tcmpt->synweight = 0; + tcmpt->qmfbid = cp->tccp.qmfbid; + tcmpt->numrlvls = cp->tccp.maxrlvls; + tcmpt->numbands = 3 * tcmpt->numrlvls - 2; + if (!(tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1))) { + goto error; + } + + for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) { + tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno]; + tcmpt->prcheightexpns[rlvlno] = cp->tccp.prcheightexpns[rlvlno]; + } + tcmpt->cblkwidthexpn = cp->tccp.cblkwidthexpn; + tcmpt->cblkheightexpn = cp->tccp.cblkheightexpn; + tcmpt->cblksty = cp->tccp.cblksty; + tcmpt->csty = cp->tccp.csty; + + tcmpt->numstepsizes = tcmpt->numbands; + assert(tcmpt->numstepsizes <= JPC_MAXBANDS); + memset(tcmpt->stepsizes, 0, sizeof(tcmpt->numstepsizes * + sizeof(uint_fast16_t))); + + /* Retrieve information about the various bands. */ + jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data), + jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data), + jas_seq2d_yend(tcmpt->data), bandinfos); + + if (!(tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)))) { + goto error; + } + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + rlvl->bands = 0; + rlvl->tcmpt = tcmpt; + } + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + if (!rlvl_create(rlvl, cp, tcmpt, bandinfos)) { + goto error; + } + } + + return tcmpt; error: - tcmpt_destroy(tcmpt); - return 0; + tcmpt_destroy(tcmpt); + return 0; } static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt) { - jpc_enc_rlvl_t *rlvl; - uint_fast16_t rlvlno; - - if (tcmpt->rlvls) { - for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; - ++rlvlno, ++rlvl) { - rlvl_destroy(rlvl); - } - jas_free(tcmpt->rlvls); - } - - if (tcmpt->data) { - jas_seq2d_destroy(tcmpt->data); - } - if (tcmpt->tsfb) { - jpc_tsfb_destroy(tcmpt->tsfb); - } + jpc_enc_rlvl_t *rlvl; + uint_fast16_t rlvlno; + + if (tcmpt->rlvls) { + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + rlvl_destroy(rlvl); + } + jas_free(tcmpt->rlvls); + } + + if (tcmpt->data) { + jas_seq2d_destroy(tcmpt->data); + } + if (tcmpt->tsfb) { + jpc_tsfb_destroy(tcmpt->tsfb); + } } static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp, jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos) { - uint_fast16_t rlvlno; - uint_fast32_t tlprctlx; - uint_fast32_t tlprctly; - uint_fast32_t brprcbrx; - uint_fast32_t brprcbry; - uint_fast16_t bandno; - jpc_enc_band_t *band; - - /* Deduce the resolution level. */ - rlvlno = rlvl - tcmpt->rlvls; - - /* Initialize members required for error recovery. */ - rlvl->bands = 0; - rlvl->tcmpt = tcmpt; - - /* Compute the coordinates of the top-left and bottom-right - corners of the tile-component at this resolution. */ - rlvl->tlx = JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls - - 1 - rlvlno); - rlvl->tly = JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls - - 1 - rlvlno); - rlvl->brx = JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls - - 1 - rlvlno); - rlvl->bry = JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls - - 1 - rlvlno); - - if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) { - rlvl->numhprcs = 0; - rlvl->numvprcs = 0; - rlvl->numprcs = 0; - return rlvl; - } - - rlvl->numbands = (!rlvlno) ? 1 : 3; - rlvl->prcwidthexpn = cp->tccp.prcwidthexpns[rlvlno]; - rlvl->prcheightexpn = cp->tccp.prcheightexpns[rlvlno]; - if (!rlvlno) { - rlvl->cbgwidthexpn = rlvl->prcwidthexpn; - rlvl->cbgheightexpn = rlvl->prcheightexpn; - } else { - rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1; - rlvl->cbgheightexpn = rlvl->prcheightexpn - 1; - } - rlvl->cblkwidthexpn = JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn); - rlvl->cblkheightexpn = JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn); - - /* Compute the number of precincts. */ - tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); - tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); - brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn); - brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn); - rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn); - rlvl->numvprcs = JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn); - rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs; - - if (!(rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)))) { - goto error; - } - for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { - band->prcs = 0; - band->data = 0; - band->rlvl = rlvl; - } - for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { - if (!band_create(band, cp, rlvl, bandinfos)) { - goto error; - } - } - - return rlvl; + uint_fast16_t rlvlno; + uint_fast32_t tlprctlx; + uint_fast32_t tlprctly; + uint_fast32_t brprcbrx; + uint_fast32_t brprcbry; + uint_fast16_t bandno; + jpc_enc_band_t *band; + + /* Deduce the resolution level. */ + rlvlno = rlvl - tcmpt->rlvls; + + /* Initialize members required for error recovery. */ + rlvl->bands = 0; + rlvl->tcmpt = tcmpt; + + /* Compute the coordinates of the top-left and bottom-right + corners of the tile-component at this resolution. */ + rlvl->tlx = JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + rlvl->tly = JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + rlvl->brx = JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + rlvl->bry = JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + + if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) { + rlvl->numhprcs = 0; + rlvl->numvprcs = 0; + rlvl->numprcs = 0; + return rlvl; + } + + rlvl->numbands = (!rlvlno) ? 1 : 3; + rlvl->prcwidthexpn = cp->tccp.prcwidthexpns[rlvlno]; + rlvl->prcheightexpn = cp->tccp.prcheightexpns[rlvlno]; + if (!rlvlno) { + rlvl->cbgwidthexpn = rlvl->prcwidthexpn; + rlvl->cbgheightexpn = rlvl->prcheightexpn; + } else { + rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1; + rlvl->cbgheightexpn = rlvl->prcheightexpn - 1; + } + rlvl->cblkwidthexpn = JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn); + rlvl->cblkheightexpn = JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn); + + /* Compute the number of precincts. */ + tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); + tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); + brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn); + brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn); + rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn); + rlvl->numvprcs = JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn); + rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs; + + if (!(rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)))) { + goto error; + } + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + band->prcs = 0; + band->data = 0; + band->rlvl = rlvl; + } + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + if (!band_create(band, cp, rlvl, bandinfos)) { + goto error; + } + } + + return rlvl; error: - rlvl_destroy(rlvl); - return 0; + rlvl_destroy(rlvl); + return 0; } static void rlvl_destroy(jpc_enc_rlvl_t *rlvl) { - jpc_enc_band_t *band; - uint_fast16_t bandno; - - if (rlvl->bands) { - for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { - band_destroy(band); - } - jas_free(rlvl->bands); - } + jpc_enc_band_t *band; + uint_fast16_t bandno; + + if (rlvl->bands) { + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + band_destroy(band); + } + jas_free(rlvl->bands); + } } static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp, jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos) { - uint_fast16_t bandno; - uint_fast16_t gblbandno; - uint_fast16_t rlvlno; - jpc_tsfb_band_t *bandinfo; - jpc_enc_tcmpt_t *tcmpt; - uint_fast32_t prcno; - jpc_enc_prc_t *prc; - - tcmpt = rlvl->tcmpt; - band->data = 0; - band->prcs = 0; - band->rlvl = rlvl; - - /* Deduce the resolution level and band number. */ - rlvlno = rlvl - rlvl->tcmpt->rlvls; - bandno = band - rlvl->bands; - gblbandno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + bandno + 1); - - bandinfo = &bandinfos[gblbandno]; + uint_fast16_t bandno; + uint_fast16_t gblbandno; + uint_fast16_t rlvlno; + jpc_tsfb_band_t *bandinfo; + jpc_enc_tcmpt_t *tcmpt; + uint_fast32_t prcno; + jpc_enc_prc_t *prc; + + tcmpt = rlvl->tcmpt; + band->data = 0; + band->prcs = 0; + band->rlvl = rlvl; + + /* Deduce the resolution level and band number. */ + rlvlno = rlvl - rlvl->tcmpt->rlvls; + bandno = band - rlvl->bands; + gblbandno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + bandno + 1); + + bandinfo = &bandinfos[gblbandno]; if (bandinfo->xstart != bandinfo->xend && bandinfo->ystart != bandinfo->yend) { - if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) { - goto error; - } - jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart, - bandinfo->locystart, bandinfo->locxend, bandinfo->locyend); - jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart); + if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) { + goto error; + } + jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart, + bandinfo->locystart, bandinfo->locxend, bandinfo->locyend); + jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart); } - band->orient = bandinfo->orient; - band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno, - band->orient); - band->numbps = 0; - band->absstepsize = 0; - band->stepsize = 0; - band->synweight = bandinfo->synenergywt; + band->orient = bandinfo->orient; + band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno, + band->orient); + band->numbps = 0; + band->absstepsize = 0; + band->stepsize = 0; + band->synweight = bandinfo->synenergywt; if (band->data) { - if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)))) { - goto error; - } - for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno, - ++prc) { - prc->cblks = 0; - prc->incltree = 0; - prc->nlibtree = 0; - prc->savincltree = 0; - prc->savnlibtree = 0; - prc->band = band; - } - for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno, - ++prc) { - if (!prc_create(prc, cp, band)) { - goto error; - } - } + if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)))) { + goto error; + } + for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno, + ++prc) { + prc->cblks = 0; + prc->incltree = 0; + prc->nlibtree = 0; + prc->savincltree = 0; + prc->savnlibtree = 0; + prc->band = band; + } + for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno, + ++prc) { + if (!prc_create(prc, cp, band)) { + goto error; + } + } } - return band; + return band; error: - band_destroy(band); - return 0; + band_destroy(band); + return 0; } static void band_destroy(jpc_enc_band_t *band) { - jpc_enc_prc_t *prc; - jpc_enc_rlvl_t *rlvl; - uint_fast32_t prcno; - - if (band->prcs) { - rlvl = band->rlvl; - for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; - ++prcno, ++prc) { - prc_destroy(prc); - } - jas_free(band->prcs); - } - if (band->data) { - jas_seq2d_destroy(band->data); - } + jpc_enc_prc_t *prc; + jpc_enc_rlvl_t *rlvl; + uint_fast32_t prcno; + + if (band->prcs) { + rlvl = band->rlvl; + for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; + ++prcno, ++prc) { + prc_destroy(prc); + } + jas_free(band->prcs); + } + if (band->data) { + jas_seq2d_destroy(band->data); + } } static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band) { - uint_fast32_t prcno; - uint_fast32_t prcxind; - uint_fast32_t prcyind; - uint_fast32_t cbgtlx; - uint_fast32_t cbgtly; - uint_fast32_t tlprctlx; - uint_fast32_t tlprctly; - uint_fast32_t tlcbgtlx; - uint_fast32_t tlcbgtly; - uint_fast16_t rlvlno; - jpc_enc_rlvl_t *rlvl; - uint_fast32_t tlcblktlx; - uint_fast32_t tlcblktly; - uint_fast32_t brcblkbrx; - uint_fast32_t brcblkbry; - uint_fast32_t cblkno; - jpc_enc_cblk_t *cblk; - jpc_enc_tcmpt_t *tcmpt; - - prc->cblks = 0; - prc->incltree = 0; - prc->savincltree = 0; - prc->nlibtree = 0; - prc->savnlibtree = 0; - - rlvl = band->rlvl; - tcmpt = rlvl->tcmpt; + uint_fast32_t prcno; + uint_fast32_t prcxind; + uint_fast32_t prcyind; + uint_fast32_t cbgtlx; + uint_fast32_t cbgtly; + uint_fast32_t tlprctlx; + uint_fast32_t tlprctly; + uint_fast32_t tlcbgtlx; + uint_fast32_t tlcbgtly; + uint_fast16_t rlvlno; + jpc_enc_rlvl_t *rlvl; + uint_fast32_t tlcblktlx; + uint_fast32_t tlcblktly; + uint_fast32_t brcblkbrx; + uint_fast32_t brcblkbry; + uint_fast32_t cblkno; + jpc_enc_cblk_t *cblk; + jpc_enc_tcmpt_t *tcmpt; + + prc->cblks = 0; + prc->incltree = 0; + prc->savincltree = 0; + prc->nlibtree = 0; + prc->savnlibtree = 0; + + rlvl = band->rlvl; + tcmpt = rlvl->tcmpt; rlvlno = rlvl - tcmpt->rlvls; - prcno = prc - band->prcs; - prcxind = prcno % rlvl->numhprcs; - prcyind = prcno / rlvl->numhprcs; - prc->band = band; + prcno = prc - band->prcs; + prcxind = prcno % rlvl->numhprcs; + prcyind = prcno / rlvl->numhprcs; + prc->band = band; tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); if (!rlvlno) { - tlcbgtlx = tlprctlx; - tlcbgtly = tlprctly; + tlcbgtlx = tlprctlx; + tlcbgtly = tlprctly; } else { - tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1); - tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1); + tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1); + tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1); } - /* Compute the coordinates of the top-left and bottom-right - corners of the precinct. */ - cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn); - cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn); - prc->tlx = JAS_MAX(jas_seq2d_xstart(band->data), cbgtlx); - prc->tly = JAS_MAX(jas_seq2d_ystart(band->data), cbgtly); - prc->brx = JAS_MIN(jas_seq2d_xend(band->data), cbgtlx + - (1 << rlvl->cbgwidthexpn)); - prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly + - (1 << rlvl->cbgheightexpn)); - - if (prc->tlx < prc->brx && prc->tly < prc->bry) { - /* The precinct contains at least one code block. */ - - tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); - tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); - brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn); - brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn); - prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx, - rlvl->cblkwidthexpn); - prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly, - rlvl->cblkheightexpn); - prc->numcblks = prc->numhcblks * prc->numvcblks; - - if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { - goto error; - } - if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { - goto error; - } - if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { - goto error; - } - if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { - goto error; - } - - if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)))) { - goto error; - } - for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; - ++cblkno, ++cblk) { - cblk->passes = 0; - cblk->stream = 0; - cblk->mqenc = 0; - cblk->data = 0; - cblk->flags = 0; - cblk->prc = prc; - } - for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; - ++cblkno, ++cblk) { - if (!cblk_create(cblk, cp, prc)) { - goto error; - } - } - } else { - /* The precinct does not contain any code blocks. */ - prc->tlx = prc->brx; - prc->tly = prc->bry; - prc->numcblks = 0; - prc->numhcblks = 0; - prc->numvcblks = 0; - prc->cblks = 0; - prc->incltree = 0; - prc->nlibtree = 0; - prc->savincltree = 0; - prc->savnlibtree = 0; - } - - return prc; + /* Compute the coordinates of the top-left and bottom-right + corners of the precinct. */ + cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn); + cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn); + prc->tlx = JAS_MAX(jas_seq2d_xstart(band->data), cbgtlx); + prc->tly = JAS_MAX(jas_seq2d_ystart(band->data), cbgtly); + prc->brx = JAS_MIN(jas_seq2d_xend(band->data), cbgtlx + + (1 << rlvl->cbgwidthexpn)); + prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly + + (1 << rlvl->cbgheightexpn)); + + if (prc->tlx < prc->brx && prc->tly < prc->bry) { + /* The precinct contains at least one code block. */ + + tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); + tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); + brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn); + brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn); + prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx, + rlvl->cblkwidthexpn); + prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly, + rlvl->cblkheightexpn); + prc->numcblks = prc->numhcblks * prc->numvcblks; + + if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; + } + if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; + } + if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; + } + if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; + } + + if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)))) { + goto error; + } + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + cblk->passes = 0; + cblk->stream = 0; + cblk->mqenc = 0; + cblk->data = 0; + cblk->flags = 0; + cblk->prc = prc; + } + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + if (!cblk_create(cblk, cp, prc)) { + goto error; + } + } + } else { + /* The precinct does not contain any code blocks. */ + prc->tlx = prc->brx; + prc->tly = prc->bry; + prc->numcblks = 0; + prc->numhcblks = 0; + prc->numvcblks = 0; + prc->cblks = 0; + prc->incltree = 0; + prc->nlibtree = 0; + prc->savincltree = 0; + prc->savnlibtree = 0; + } + + return prc; error: - prc_destroy(prc); - return 0; + prc_destroy(prc); + return 0; } static void prc_destroy(jpc_enc_prc_t *prc) { - jpc_enc_cblk_t *cblk; - uint_fast32_t cblkno; - - if (prc->cblks) { - for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; - ++cblkno, ++cblk) { - cblk_destroy(cblk); - } - jas_free(prc->cblks); - } - if (prc->incltree) { - jpc_tagtree_destroy(prc->incltree); - } - if (prc->nlibtree) { - jpc_tagtree_destroy(prc->nlibtree); - } - if (prc->savincltree) { - jpc_tagtree_destroy(prc->savincltree); - } - if (prc->savnlibtree) { - jpc_tagtree_destroy(prc->savnlibtree); - } + jpc_enc_cblk_t *cblk; + uint_fast32_t cblkno; + + if (prc->cblks) { + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + cblk_destroy(cblk); + } + jas_free(prc->cblks); + } + if (prc->incltree) { + jpc_tagtree_destroy(prc->incltree); + } + if (prc->nlibtree) { + jpc_tagtree_destroy(prc->nlibtree); + } + if (prc->savincltree) { + jpc_tagtree_destroy(prc->savincltree); + } + if (prc->savnlibtree) { + jpc_tagtree_destroy(prc->savnlibtree); + } } static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc) { - jpc_enc_band_t *band; - uint_fast32_t cblktlx; - uint_fast32_t cblktly; - uint_fast32_t cblkbrx; - uint_fast32_t cblkbry; - jpc_enc_rlvl_t *rlvl; - uint_fast32_t cblkxind; - uint_fast32_t cblkyind; - uint_fast32_t cblkno; - uint_fast32_t tlcblktlx; - uint_fast32_t tlcblktly; - - cblkno = cblk - prc->cblks; - cblkxind = cblkno % prc->numhcblks; - cblkyind = cblkno / prc->numhcblks; - rlvl = prc->band->rlvl; - cblk->prc = prc; - - cblk->numpasses = 0; - cblk->passes = 0; - cblk->numencpasses = 0; - cblk->numimsbs = 0; - cblk->numlenbits = 0; - cblk->stream = 0; - cblk->mqenc = 0; - cblk->flags = 0; - cblk->numbps = 0; - cblk->curpass = 0; - cblk->data = 0; - cblk->savedcurpass = 0; - cblk->savednumlenbits = 0; - cblk->savednumencpasses = 0; - - band = prc->band; - tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); - tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); - cblktlx = JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx); - cblktly = JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly); - cblkbrx = JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn), - prc->brx); - cblkbry = JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn), - prc->bry); - - assert(cblktlx < cblkbrx && cblktly < cblkbry); - if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) { - goto error; - } - jas_seq2d_bindsub(cblk->data, band->data, cblktlx, cblktly, cblkbrx, cblkbry); - - return cblk; + jpc_enc_band_t *band; + uint_fast32_t cblktlx; + uint_fast32_t cblktly; + uint_fast32_t cblkbrx; + uint_fast32_t cblkbry; + jpc_enc_rlvl_t *rlvl; + uint_fast32_t cblkxind; + uint_fast32_t cblkyind; + uint_fast32_t cblkno; + uint_fast32_t tlcblktlx; + uint_fast32_t tlcblktly; + + cblkno = cblk - prc->cblks; + cblkxind = cblkno % prc->numhcblks; + cblkyind = cblkno / prc->numhcblks; + rlvl = prc->band->rlvl; + cblk->prc = prc; + + cblk->numpasses = 0; + cblk->passes = 0; + cblk->numencpasses = 0; + cblk->numimsbs = 0; + cblk->numlenbits = 0; + cblk->stream = 0; + cblk->mqenc = 0; + cblk->flags = 0; + cblk->numbps = 0; + cblk->curpass = 0; + cblk->data = 0; + cblk->savedcurpass = 0; + cblk->savednumlenbits = 0; + cblk->savednumencpasses = 0; + + band = prc->band; + tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); + tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); + cblktlx = JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx); + cblktly = JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly); + cblkbrx = JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn), + prc->brx); + cblkbry = JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn), + prc->bry); + + assert(cblktlx < cblkbrx && cblktly < cblkbry); + if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) { + goto error; + } + jas_seq2d_bindsub(cblk->data, band->data, cblktlx, cblktly, cblkbrx, cblkbry); + + return cblk; error: - cblk_destroy(cblk); - return 0; + cblk_destroy(cblk); + return 0; } static void cblk_destroy(jpc_enc_cblk_t *cblk) { - uint_fast16_t passno; - jpc_enc_pass_t *pass; - if (cblk->passes) { - for (passno = 0, pass = cblk->passes; passno < cblk->numpasses; - ++passno, ++pass) { - pass_destroy(pass); - } - jas_free(cblk->passes); - } - if (cblk->stream) { - jas_stream_close(cblk->stream); - } - if (cblk->mqenc) { - jpc_mqenc_destroy(cblk->mqenc); - } - if (cblk->data) { - jas_seq2d_destroy(cblk->data); - } - if (cblk->flags) { - jas_seq2d_destroy(cblk->flags); - } + uint_fast16_t passno; + jpc_enc_pass_t *pass; + if (cblk->passes) { + for (passno = 0, pass = cblk->passes; passno < cblk->numpasses; + ++passno, ++pass) { + pass_destroy(pass); + } + jas_free(cblk->passes); + } + if (cblk->stream) { + jas_stream_close(cblk->stream); + } + if (cblk->mqenc) { + jpc_mqenc_destroy(cblk->mqenc); + } + if (cblk->data) { + jas_seq2d_destroy(cblk->data); + } + if (cblk->flags) { + jas_seq2d_destroy(cblk->flags); + } } static void pass_destroy(jpc_enc_pass_t *pass) { - /* XXX - need to free resources here */ + /* XXX - need to free resources here */ } void jpc_enc_dump(jpc_enc_t *enc) { - jpc_enc_tile_t *tile; - jpc_enc_tcmpt_t *tcmpt; - jpc_enc_rlvl_t *rlvl; - jpc_enc_band_t *band; - jpc_enc_prc_t *prc; - jpc_enc_cblk_t *cblk; - uint_fast16_t cmptno; - uint_fast16_t rlvlno; - uint_fast16_t bandno; - uint_fast32_t prcno; - uint_fast32_t cblkno; - - tile = enc->curtile; - - for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno, - ++tcmpt) { - fprintf(stderr, " tcmpt %5d %5d %5d %5d\n", jas_seq2d_xstart(tcmpt->data), jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data), jas_seq2d_yend(tcmpt->data)); - for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; - ++rlvlno, ++rlvl) { - fprintf(stderr, " rlvl %5d %5d %5d %5d\n", rlvl->tlx, rlvl->tly, rlvl->brx, rlvl->bry); - for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { - if (!band->data) { - continue; - } - fprintf(stderr, " band %5d %5d %5d %5d\n", jas_seq2d_xstart(band->data), jas_seq2d_ystart(band->data), jas_seq2d_xend(band->data), jas_seq2d_yend(band->data)); - for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; - ++prcno, ++prc) { - fprintf(stderr, " prc %5d %5d %5d %5d (%5d %5d)\n", prc->tlx, prc->tly, prc->brx, prc->bry, prc->brx - prc->tlx, prc->bry - prc->tly); - if (!prc->cblks) { - continue; - } - for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; - ++cblkno, ++cblk) { - fprintf(stderr, " cblk %5d %5d %5d %5d\n", jas_seq2d_xstart(cblk->data), jas_seq2d_ystart(cblk->data), jas_seq2d_xend(cblk->data), jas_seq2d_yend(cblk->data)); - } - } - } - } - } + jpc_enc_tile_t *tile; + jpc_enc_tcmpt_t *tcmpt; + jpc_enc_rlvl_t *rlvl; + jpc_enc_band_t *band; + jpc_enc_prc_t *prc; + jpc_enc_cblk_t *cblk; + uint_fast16_t cmptno; + uint_fast16_t rlvlno; + uint_fast16_t bandno; + uint_fast32_t prcno; + uint_fast32_t cblkno; + + tile = enc->curtile; + + for (cmptno = 0, tcmpt = tile->tcmpts; + cmptno < tile->numtcmpts; + ++cmptno, ++tcmpt) { + fprintf(stderr, " tcmpt %5d %5d %5d %5d\n", + (int)jas_seq2d_xstart(tcmpt->data), + (int)jas_seq2d_ystart(tcmpt->data), + (int)jas_seq2d_xend(tcmpt->data), + (int)jas_seq2d_yend(tcmpt->data)); + for (rlvlno = 0, rlvl = tcmpt->rlvls; + rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + fprintf(stderr, " rlvl %5d %5d %5d %5d\n", + (int)rlvl->tlx, (int)rlvl->tly, + (int)rlvl->brx, (int)rlvl->bry); + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + if (!band->data) { + continue; + } + fprintf(stderr, " band %5d %5d %5d %5d\n", + (int)jas_seq2d_xstart(band->data), + (int)jas_seq2d_ystart(band->data), + (int)jas_seq2d_xend(band->data), + (int)jas_seq2d_yend(band->data)); + for (prcno = 0, prc = band->prcs; + prcno < rlvl->numprcs; + ++prcno, ++prc) { + fprintf(stderr, " prc %5d %5d %5d %5d (%5d %5d)\n", + (int)prc->tlx, (int)prc->tly, + (int)prc->brx, (int)prc->bry, + (int)(prc->brx - prc->tlx), + (int)(prc->bry - prc->tly)); + if (!prc->cblks) { + continue; + } + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + fprintf(stderr, " cblk %5d %5d %5d %5d\n", + (int)jas_seq2d_xstart(cblk->data), + (int)jas_seq2d_ystart(cblk->data), + (int)jas_seq2d_xend(cblk->data), + (int)jas_seq2d_yend(cblk->data)); + } + } + } + } + } } + + + diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_mqenc.c b/converter/other/jpeg2000/libjasper/jpc/jpc_mqenc.c index 0219a000..3f6122e3 100644 --- a/converter/other/jpeg2000/libjasper/jpc/jpc_mqenc.c +++ b/converter/other/jpeg2000/libjasper/jpc/jpc_mqenc.c @@ -135,91 +135,91 @@ \******************************************************************************/ #if defined(DEBUG) -#define JPC_MQENC_CALL(n, x) \ - ((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0)) +#define JPC_MQENC_CALL(n, x) \ + ((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0)) #else -#define JPC_MQENC_CALL(n, x) +#define JPC_MQENC_CALL(n, x) #endif -#define jpc_mqenc_codemps9(areg, creg, ctreg, curctx, enc) \ +#define jpc_mqenc_codemps9(areg, creg, ctreg, curctx, enc) \ { \ - jpc_mqstate_t *state = *(curctx); \ - (areg) -= state->qeval; \ - if (!((areg) & 0x8000)) { \ - if ((areg) < state->qeval) { \ - (areg) = state->qeval; \ - } else { \ - (creg) += state->qeval; \ - } \ - *(curctx) = state->nmps; \ - jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \ - } else { \ - (creg) += state->qeval; \ - } \ + jpc_mqstate_t *state = *(curctx); \ + (areg) -= state->qeval; \ + if (!((areg) & 0x8000)) { \ + if ((areg) < state->qeval) { \ + (areg) = state->qeval; \ + } else { \ + (creg) += state->qeval; \ + } \ + *(curctx) = state->nmps; \ + jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \ + } else { \ + (creg) += state->qeval; \ + } \ } -#define jpc_mqenc_codelps2(areg, creg, ctreg, curctx, enc) \ +#define jpc_mqenc_codelps2(areg, creg, ctreg, curctx, enc) \ { \ - jpc_mqstate_t *state = *(curctx); \ - (areg) -= state->qeval; \ - if ((areg) < state->qeval) { \ - (creg) += state->qeval; \ - } else { \ - (areg) = state->qeval; \ - } \ - *(curctx) = state->nlps; \ - jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \ + jpc_mqstate_t *state = *(curctx); \ + (areg) -= state->qeval; \ + if ((areg) < state->qeval) { \ + (creg) += state->qeval; \ + } else { \ + (areg) = state->qeval; \ + } \ + *(curctx) = state->nlps; \ + jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \ } -#define jpc_mqenc_renorme(areg, creg, ctreg, enc) \ +#define jpc_mqenc_renorme(areg, creg, ctreg, enc) \ { \ - do { \ - (areg) <<= 1; \ - (creg) <<= 1; \ - if (!--(ctreg)) { \ - jpc_mqenc_byteout((areg), (creg), (ctreg), (enc)); \ - } \ - } while (!((areg) & 0x8000)); \ + do { \ + (areg) <<= 1; \ + (creg) <<= 1; \ + if (!--(ctreg)) { \ + jpc_mqenc_byteout((areg), (creg), (ctreg), (enc)); \ + } \ + } while (!((areg) & 0x8000)); \ } -#define jpc_mqenc_byteout(areg, creg, ctreg, enc) \ +#define jpc_mqenc_byteout(areg, creg, ctreg, enc) \ { \ - if ((enc)->outbuf != 0xff) { \ - if ((creg) & 0x8000000) { \ - if (++((enc)->outbuf) == 0xff) { \ - (creg) &= 0x7ffffff; \ - jpc_mqenc_byteout2(enc); \ - enc->outbuf = ((creg) >> 20) & 0xff; \ - (creg) &= 0xfffff; \ - (ctreg) = 7; \ - } else { \ - jpc_mqenc_byteout2(enc); \ - enc->outbuf = ((creg) >> 19) & 0xff; \ - (creg) &= 0x7ffff; \ - (ctreg) = 8; \ - } \ - } else { \ - jpc_mqenc_byteout2(enc); \ - (enc)->outbuf = ((creg) >> 19) & 0xff; \ - (creg) &= 0x7ffff; \ - (ctreg) = 8; \ - } \ - } else { \ - jpc_mqenc_byteout2(enc); \ - (enc)->outbuf = ((creg) >> 20) & 0xff; \ - (creg) &= 0xfffff; \ - (ctreg) = 7; \ - } \ + if ((enc)->outbuf != 0xff) { \ + if ((creg) & 0x8000000) { \ + if (++((enc)->outbuf) == 0xff) { \ + (creg) &= 0x7ffffff; \ + jpc_mqenc_byteout2(enc); \ + enc->outbuf = ((creg) >> 20) & 0xff; \ + (creg) &= 0xfffff; \ + (ctreg) = 7; \ + } else { \ + jpc_mqenc_byteout2(enc); \ + enc->outbuf = ((creg) >> 19) & 0xff; \ + (creg) &= 0x7ffff; \ + (ctreg) = 8; \ + } \ + } else { \ + jpc_mqenc_byteout2(enc); \ + (enc)->outbuf = ((creg) >> 19) & 0xff; \ + (creg) &= 0x7ffff; \ + (ctreg) = 8; \ + } \ + } else { \ + jpc_mqenc_byteout2(enc); \ + (enc)->outbuf = ((creg) >> 20) & 0xff; \ + (creg) &= 0xfffff; \ + (ctreg) = 7; \ + } \ } -#define jpc_mqenc_byteout2(enc) \ +#define jpc_mqenc_byteout2(enc) \ { \ - if (enc->outbuf >= 0) { \ - if (jas_stream_putc(enc->out, (unsigned char)enc->outbuf) == EOF) { \ - enc->err |= 1; \ - } \ - } \ - enc->lastbyte = enc->outbuf; \ + if (enc->outbuf >= 0) { \ + if (jas_stream_putc(enc->out, (unsigned char)enc->outbuf) == EOF) { \ + enc->err |= 1; \ + } \ + } \ + enc->lastbyte = enc->outbuf; \ } /******************************************************************************\ @@ -236,45 +236,45 @@ static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc); jpc_mqenc_t *jpc_mqenc_create(int maxctxs, jas_stream_t *out) { - jpc_mqenc_t *mqenc; + jpc_mqenc_t *mqenc; - /* Allocate memory for the MQ encoder. */ - if (!(mqenc = jas_malloc(sizeof(jpc_mqenc_t)))) { - goto error; - } - mqenc->out = out; - mqenc->maxctxs = maxctxs; + /* Allocate memory for the MQ encoder. */ + if (!(mqenc = jas_malloc(sizeof(jpc_mqenc_t)))) { + goto error; + } + mqenc->out = out; + mqenc->maxctxs = maxctxs; - /* Allocate memory for the per-context state information. */ - if (!(mqenc->ctxs = jas_malloc(mqenc->maxctxs * sizeof(jpc_mqstate_t *)))) { - goto error; - } + /* Allocate memory for the per-context state information. */ + if (!(mqenc->ctxs = jas_malloc(mqenc->maxctxs * sizeof(jpc_mqstate_t *)))) { + goto error; + } - /* Set the current context to the first one. */ - mqenc->curctx = mqenc->ctxs; + /* Set the current context to the first one. */ + mqenc->curctx = mqenc->ctxs; - jpc_mqenc_init(mqenc); + jpc_mqenc_init(mqenc); - /* Initialize the per-context state information to something sane. */ - jpc_mqenc_setctxs(mqenc, 0, 0); + /* Initialize the per-context state information to something sane. */ + jpc_mqenc_setctxs(mqenc, 0, 0); - return mqenc; + return mqenc; error: - if (mqenc) { - jpc_mqenc_destroy(mqenc); - } - return 0; + if (mqenc) { + jpc_mqenc_destroy(mqenc); + } + return 0; } /* Destroy a MQ encoder. */ void jpc_mqenc_destroy(jpc_mqenc_t *mqenc) { - if (mqenc->ctxs) { - jas_free(mqenc->ctxs); - } - jas_free(mqenc); + if (mqenc->ctxs) { + jas_free(mqenc->ctxs); + } + jas_free(mqenc); } /******************************************************************************\ @@ -285,33 +285,33 @@ void jpc_mqenc_destroy(jpc_mqenc_t *mqenc) void jpc_mqenc_init(jpc_mqenc_t *mqenc) { - mqenc->areg = 0x8000; - mqenc->outbuf = -1; - mqenc->creg = 0; - mqenc->ctreg = 12; - mqenc->lastbyte = -1; - mqenc->err = 0; + mqenc->areg = 0x8000; + mqenc->outbuf = -1; + mqenc->creg = 0; + mqenc->ctreg = 12; + mqenc->lastbyte = -1; + mqenc->err = 0; } /* Initialize one or more contexts. */ void jpc_mqenc_setctxs(jpc_mqenc_t *mqenc, int numctxs, jpc_mqctx_t *ctxs) { - jpc_mqstate_t **ctx; - int n; - - ctx = mqenc->ctxs; - n = JAS_MIN(mqenc->maxctxs, numctxs); - while (--n >= 0) { - *ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps]; - ++ctx; - ++ctxs; - } - n = mqenc->maxctxs - numctxs; - while (--n >= 0) { - *ctx = &jpc_mqstates[0]; - ++ctx; - } + jpc_mqstate_t **ctx; + int n; + + ctx = mqenc->ctxs; + n = JAS_MIN(mqenc->maxctxs, numctxs); + while (--n >= 0) { + *ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps]; + ++ctx; + ++ctxs; + } + n = mqenc->maxctxs - numctxs; + while (--n >= 0) { + *ctx = &jpc_mqstates[0]; + ++ctx; + } } @@ -319,10 +319,10 @@ void jpc_mqenc_setctxs(jpc_mqenc_t *mqenc, int numctxs, jpc_mqctx_t *ctxs) void jpc_mqenc_getstate(jpc_mqenc_t *mqenc, jpc_mqencstate_t *state) { - state->areg = mqenc->areg; - state->creg = mqenc->creg; - state->ctreg = mqenc->ctreg; - state->lastbyte = mqenc->lastbyte; + state->areg = mqenc->areg; + state->creg = mqenc->creg; + state->ctreg = mqenc->ctreg; + state->lastbyte = mqenc->lastbyte; } /******************************************************************************\ @@ -333,49 +333,49 @@ void jpc_mqenc_getstate(jpc_mqenc_t *mqenc, jpc_mqencstate_t *state) int jpc_mqenc_putbit_func(jpc_mqenc_t *mqenc, int bit) { - const jpc_mqstate_t *state; - JAS_DBGLOG(100, ("jpc_mqenc_putbit(%p, %d)\n", mqenc, bit)); - JPC_MQENC_CALL(100, jpc_mqenc_dump(mqenc, stderr)); - - state = *(mqenc->curctx); - - if (state->mps == bit) { - /* Apply the CODEMPS algorithm as defined in the standard. */ - mqenc->areg -= state->qeval; - if (!(mqenc->areg & 0x8000)) { - jpc_mqenc_codemps2(mqenc); - } else { - mqenc->creg += state->qeval; - } - } else { - /* Apply the CODELPS algorithm as defined in the standard. */ - jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc); - } - - return jpc_mqenc_error(mqenc) ? (-1) : 0; + const jpc_mqstate_t *state; + JAS_DBGLOG(100, ("jpc_mqenc_putbit(%p, %d)\n", mqenc, bit)); + JPC_MQENC_CALL(100, jpc_mqenc_dump(mqenc, stderr)); + + state = *(mqenc->curctx); + + if (state->mps == bit) { + /* Apply the CODEMPS algorithm as defined in the standard. */ + mqenc->areg -= state->qeval; + if (!(mqenc->areg & 0x8000)) { + jpc_mqenc_codemps2(mqenc); + } else { + mqenc->creg += state->qeval; + } + } else { + /* Apply the CODELPS algorithm as defined in the standard. */ + jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc); + } + + return jpc_mqenc_error(mqenc) ? (-1) : 0; } int jpc_mqenc_codemps2(jpc_mqenc_t *mqenc) { - /* Note: This function only performs part of the work associated with - the CODEMPS algorithm from the standard. Some of the work is also - performed by the caller. */ - - jpc_mqstate_t *state = *(mqenc->curctx); - if (mqenc->areg < state->qeval) { - mqenc->areg = state->qeval; - } else { - mqenc->creg += state->qeval; - } - *mqenc->curctx = state->nmps; - jpc_mqenc_renorme(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); - return jpc_mqenc_error(mqenc) ? (-1) : 0; + /* Note: This function only performs part of the work associated with + the CODEMPS algorithm from the standard. Some of the work is also + performed by the caller. */ + + jpc_mqstate_t *state = *(mqenc->curctx); + if (mqenc->areg < state->qeval) { + mqenc->areg = state->qeval; + } else { + mqenc->creg += state->qeval; + } + *mqenc->curctx = state->nmps; + jpc_mqenc_renorme(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); + return jpc_mqenc_error(mqenc) ? (-1) : 0; } int jpc_mqenc_codelps(jpc_mqenc_t *mqenc) { - jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc); - return jpc_mqenc_error(mqenc) ? (-1) : 0; + jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc); + return jpc_mqenc_error(mqenc) ? (-1) : 0; } /******************************************************************************\ @@ -386,56 +386,56 @@ int jpc_mqenc_codelps(jpc_mqenc_t *mqenc) int jpc_mqenc_flush(jpc_mqenc_t *mqenc, int termmode) { - int_fast16_t k; - - switch (termmode) { - case JPC_MQENC_PTERM: - k = 11 - mqenc->ctreg + 1; - while (k > 0) { - mqenc->creg <<= mqenc->ctreg; - mqenc->ctreg = 0; - jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, - mqenc); - k -= mqenc->ctreg; - } - if (mqenc->outbuf != 0xff) { - jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); - } - break; - case JPC_MQENC_DEFTERM: - jpc_mqenc_setbits(mqenc); - mqenc->creg <<= mqenc->ctreg; - jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); - mqenc->creg <<= mqenc->ctreg; - jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); - if (mqenc->outbuf != 0xff) { - jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); - } - break; - default: - abort(); - break; - } - return 0; + int_fast16_t k; + + switch (termmode) { + case JPC_MQENC_PTERM: + k = 11 - mqenc->ctreg + 1; + while (k > 0) { + mqenc->creg <<= mqenc->ctreg; + mqenc->ctreg = 0; + jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, + mqenc); + k -= mqenc->ctreg; + } + if (mqenc->outbuf != 0xff) { + jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); + } + break; + case JPC_MQENC_DEFTERM: + jpc_mqenc_setbits(mqenc); + mqenc->creg <<= mqenc->ctreg; + jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); + mqenc->creg <<= mqenc->ctreg; + jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); + if (mqenc->outbuf != 0xff) { + jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); + } + break; + default: + abort(); + break; + } + return 0; } static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc) { - uint_fast32_t tmp = mqenc->creg + mqenc->areg; - mqenc->creg |= 0xffff; - if (mqenc->creg >= tmp) { - mqenc->creg -= 0x8000; - } + uint_fast32_t tmp = mqenc->creg + mqenc->areg; + mqenc->creg |= 0xffff; + if (mqenc->creg >= tmp) { + mqenc->creg -= 0x8000; + } } /* Dump a MQ encoder to a stream for debugging. */ int jpc_mqenc_dump(jpc_mqenc_t *mqenc, FILE *out) { - fprintf(out, "AREG = %08x, CREG = %08x, CTREG = %d\n", - mqenc->areg, mqenc->creg, mqenc->ctreg); - fprintf(out, "IND = %02d, MPS = %d, QEVAL = %04x\n", - *mqenc->curctx - jpc_mqstates, (*mqenc->curctx)->mps, - (*mqenc->curctx)->qeval); - return 0; + fprintf(out, "AREG = %08x, CREG = %08x, CTREG = %d\n", + (unsigned)mqenc->areg, (unsigned)mqenc->creg, (int)mqenc->ctreg); + fprintf(out, "IND = %02d, MPS = %d, QEVAL = %04x\n", + (int)(*mqenc->curctx - jpc_mqstates), (int)(*mqenc->curctx)->mps, + (unsigned)(*mqenc->curctx)->qeval); + return 0; } |