diff options
Diffstat (limited to 'converter/other/jpeg2000')
-rw-r--r-- | converter/other/jpeg2000/jpeg2ktopam.c | 108 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jp2/jp2_dec.c | 1032 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jpc/jpc_dec.c | 15 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jpc/jpc_enc.c | 494 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jpc/jpc_t1enc.c | 1557 | ||||
-rw-r--r-- | converter/other/jpeg2000/libjasper/jpc/jpc_util.c | 26 | ||||
-rw-r--r-- | converter/other/jpeg2000/pamtojpeg2k.c | 89 |
7 files changed, 1732 insertions, 1589 deletions
diff --git a/converter/other/jpeg2000/jpeg2ktopam.c b/converter/other/jpeg2000/jpeg2ktopam.c index 405de9c9..b507f56e 100644 --- a/converter/other/jpeg2000/jpeg2ktopam.c +++ b/converter/other/jpeg2000/jpeg2ktopam.c @@ -8,6 +8,7 @@ *****************************************************************************/ +#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */ #define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */ #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ /* In 2014.09, this was _XOPEN_SOURCE 600, with a comment saying it was @@ -61,7 +62,7 @@ parseCommandLine(int argc, char ** argv, MALLOCARRAY_NOFAIL(option_def, 100); option_def_index = 0; /* incremented by OPTENTRY */ - OPTENT3(0, "verbose", OPT_FLAG, NULL, + OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); OPTENT3(0, "debuglevel", OPT_UINT, &cmdlineP->debuglevel, &debuglevelSpec, 0); @@ -79,7 +80,7 @@ parseCommandLine(int argc, char ** argv, cmdlineP->inputFilename = strdup("-"); /* he wants stdin */ else if (argc - 1 == 1) cmdlineP->inputFilename = strdup(argv[1]); - else + else pm_error("Too many arguments. The only argument accepted\n" "is the input file specification"); @@ -106,11 +107,11 @@ validateJ2k(jas_stream_t * const instreamP) { } } - + static void -readJ2k(const char * const inputFilename, +readJ2k(const char * const inputFilename, jas_image_t ** const jasperPP) { jas_image_t * jasperP; @@ -126,7 +127,7 @@ readJ2k(const char * const inputFilename, instreamP = jas_stream_fopen(inputFilename, "rb"); if (instreamP == NULL ) pm_error("cannot open input image file '%s'", inputFilename); - } + } validateJ2k(instreamP); @@ -138,7 +139,7 @@ readJ2k(const char * const inputFilename, pm_error("Unable to interpret JPEG-2000 input. " "The Jasper library jas_image_decode() subroutine failed."); - jas_stream_close(instreamP); + jas_stream_close(instreamP); *jasperPP = jasperP; } @@ -149,7 +150,7 @@ static void getRgbComponents(int jasperCmpnt[], jas_image_t * const jasperP) { { - int const rc = + int const rc = jas_image_getcmptbytype(jasperP, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R)); if (rc < 0) @@ -158,12 +159,12 @@ getRgbComponents(int jasperCmpnt[], jas_image_t * const jasperP) { else jasperCmpnt[PAM_RED_PLANE] = rc; - if (jas_image_cmptsgnd(jasperP, rc)) + if (jas_image_cmptsgnd(jasperP, rc)) pm_error("Input image says it is RGB, but has signed values " "for what should be the red intensities."); } { - int const rc = + int const rc = jas_image_getcmptbytype(jasperP, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G)); if (rc < 0) @@ -172,12 +173,12 @@ getRgbComponents(int jasperCmpnt[], jas_image_t * const jasperP) { else jasperCmpnt[PAM_GRN_PLANE] = rc; - if (jas_image_cmptsgnd(jasperP, rc)) + if (jas_image_cmptsgnd(jasperP, rc)) pm_error("Input image says it is RGB, but has signed values " "for what should be the green intensities."); } { - int const rc = + int const rc = jas_image_getcmptbytype(jasperP, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B)); if (rc < 0) @@ -186,18 +187,18 @@ getRgbComponents(int jasperCmpnt[], jas_image_t * const jasperP) { else jasperCmpnt[PAM_BLU_PLANE] = rc; - if (jas_image_cmptsgnd(jasperP, rc)) + if (jas_image_cmptsgnd(jasperP, rc)) pm_error("Input image says it is RGB, but has signed values " "for what should be the blue intensities."); } -} +} static void getGrayComponent(int * jasperCmptP, jas_image_t * const jasperP) { - int const rc = + int const rc = jas_image_getcmptbytype(jasperP, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y)); if (rc < 0) @@ -205,7 +206,7 @@ getGrayComponent(int * jasperCmptP, jas_image_t * const jasperP) { "no gray intensity component"); else *jasperCmptP = rc; - if (jas_image_cmptsgnd(jasperP, 0)) + if (jas_image_cmptsgnd(jasperP, 0)) pm_error("Input image says it is grayscale, but has signed values " "for what should be the gray levels."); } @@ -220,13 +221,13 @@ validateComponentsAlike(jas_image_t * const jasperP) { all the channels are the same, and abort the program if not. -----------------------------------------------------------------------------*/ int cmptNo; - + for (cmptNo = 0; cmptNo < jas_image_numcmpts(jasperP); ++cmptNo) { - if (jas_image_cmptwidth(jasperP, cmptNo) != + if (jas_image_cmptwidth(jasperP, cmptNo) != jas_image_cmptwidth(jasperP, 0)) pm_message("Input image does not have components all the same " "width."); - if (jas_image_cmptheight(jasperP, cmptNo) != + if (jas_image_cmptheight(jasperP, cmptNo) != jas_image_cmptheight(jasperP, 0)) pm_message("Input image does not have components all the same " "height."); @@ -240,7 +241,7 @@ maxJasperComponentPrecision(jas_image_t * const jasperP) { int cmptNo; unsigned int max; - + max = 1; for (cmptNo = 0; cmptNo < jas_image_numcmpts(jasperP); ++cmptNo) @@ -261,8 +262,8 @@ computeOutputParm(jas_image_t * const jasperP, with the Jasper library that corresponds to Plane P of the PAM. */ - switch (jas_clrspc_fam(jas_image_clrspc(jasperP))) { - case JAS_CLRSPC_FAM_GRAY: + switch (jas_clrspc_fam(jas_image_clrspc(jasperP))) { + case JAS_CLRSPC_FAM_GRAY: outpamP->depth = 1; MALLOCARRAY_NOFAIL(jasperCmptNo, 1); getGrayComponent(&jasperCmptNo[0], jasperP); @@ -274,7 +275,7 @@ computeOutputParm(jas_image_t * const jasperP, strcpy(outpamP->tuple_type, PAM_PGM_TUPLETYPE); } break; - case JAS_CLRSPC_FAM_RGB: + case JAS_CLRSPC_FAM_RGB: outpamP->depth = 3; MALLOCARRAY_NOFAIL(jasperCmptNo, 3); getRgbComponents(jasperCmptNo, jasperP); @@ -292,15 +293,15 @@ computeOutputParm(jas_image_t * const jasperP, jasperCmptNo[plane] = plane; } strcpy(outpamP->tuple_type, ""); - if (jas_image_cmptsgnd(jasperP, 0)) + if (jas_image_cmptsgnd(jasperP, 0)) pm_message("Warning: Input image has signed sample values. " "They will be represented in the PAM output in " "two's complement."); } outpamP->plainformat = FALSE; - outpamP->width = jas_image_cmptwidth(jasperP, 0); - outpamP->height = jas_image_cmptheight(jasperP, 0); + outpamP->width = jas_image_cmptwidth(jasperP, 0); + outpamP->height = jas_image_cmptheight(jasperP, 0); validateComponentsAlike(jasperP); @@ -308,7 +309,7 @@ computeOutputParm(jas_image_t * const jasperP, unsigned int const maxPrecision = maxJasperComponentPrecision(jasperP); outpamP->maxval = pm_bitstomaxval(maxPrecision); - + outpamP->bytes_per_sample = (maxPrecision + 7)/8; } *jasperCmptNoP = jasperCmptNo; @@ -319,7 +320,7 @@ computeOutputParm(jas_image_t * const jasperP, static void createMatrices(struct pam * const outpamP, jas_matrix_t *** matrixP) { - jas_matrix_t ** matrix; + jas_matrix_t ** matrix; unsigned int plane; MALLOCARRAY_NOFAIL(matrix, outpamP->depth); @@ -330,14 +331,14 @@ createMatrices(struct pam * const outpamP, jas_matrix_t *** matrixP) { if (matrix[plane] == NULL) pm_error("Unable to create matrix for plane %u. " "jas_matrix_create() failed.", plane); - } + } *matrixP = matrix; } static void -destroyMatrices(struct pam * const outpamP, +destroyMatrices(struct pam * const outpamP, jas_matrix_t ** const matrix ) { unsigned int plane; @@ -345,7 +346,7 @@ destroyMatrices(struct pam * const outpamP, for (plane = 0; plane < outpamP->depth; ++plane) jas_matrix_destroy(matrix[plane]); free(matrix); -} +} @@ -355,7 +356,7 @@ computeComponentMaxval(struct pam * const outpamP, int const jasperCmpt[], sample ** const jasperMaxvalP, bool * const singleMaxvalP) { - + sample * jasperMaxval; unsigned int plane; @@ -363,7 +364,7 @@ computeComponentMaxval(struct pam * const outpamP, *singleMaxvalP = TRUE; /* initial assumption */ for (plane = 0; plane < outpamP->depth; ++plane) { - jasperMaxval[plane] = + jasperMaxval[plane] = pm_bitstomaxval(jas_image_cmptprec(jasperP, jasperCmpt[plane])); if (jasperMaxval[plane] != jasperMaxval[0]) *singleMaxvalP = FALSE; @@ -371,7 +372,7 @@ computeComponentMaxval(struct pam * const outpamP, *jasperMaxvalP = jasperMaxval; } - + static void copyRowSingleMaxval(jas_seqent_t ** const jasperRow, @@ -386,10 +387,10 @@ copyRowSingleMaxval(jas_seqent_t ** const jasperRow, This is significantly faster than copyRowAnyMaxval(). -----------------------------------------------------------------------------*/ unsigned int col; - + for (col = 0; col < outpamP->width; ++col) { unsigned int plane; - for (plane = 0; plane < outpamP->depth; ++plane) + for (plane = 0; plane < outpamP->depth; ++plane) tuplerow[col][plane] = jasperRow[plane][col]; } } @@ -410,12 +411,12 @@ copyRowAnyMaxval(jas_seqent_t ** const jasperRow, This is significantly slower than copyRowSingleMaxval(). -----------------------------------------------------------------------------*/ unsigned int col; - + for (col = 0; col < outpamP->width; ++col) { unsigned int plane; - for (plane = 0; plane < outpamP->depth; ++plane) - tuplerow[col][plane] = - jasperRow[plane][col] * + for (plane = 0; plane < outpamP->depth; ++plane) + tuplerow[col][plane] = + jasperRow[plane][col] * outpamP->maxval / jasperMaxval[plane]; } } @@ -460,11 +461,11 @@ convertToPamPnm(struct pam * const outpamP, matrix[plane]); if (rc != 0) pm_error("jas_image_readcmpt() of row %u plane %u " - "failed.", + "failed.", row, plane); jasperRow[plane] = jas_matrix_getref(matrix[plane], 0, 0); } - if (singleMaxval) + if (singleMaxval) copyRowSingleMaxval(jasperRow, tuplerow, outpamP); else copyRowAnyMaxval(jasperRow, tuplerow, outpamP, jasperMaxval); @@ -488,25 +489,25 @@ main(int argc, char **argv) struct pam outpam; int * jasperCmpt; /* malloc'ed */ /* jaspercmpt[P] is the component number for use with the - Jasper library that corresponds to Plane P of the PAM. + Jasper library that corresponds to Plane P of the PAM. */ jas_image_t * jasperP; pnm_init(&argc, argv); - + parseCommandLine(argc, argv, &cmdline); - - { + + { int rc; - + rc = jas_init(); if ( rc != 0 ) pm_error("Failed to initialize Jasper library. " "jas_init() returns rc %d", rc ); } - + jas_setdbglevel(cmdline.debuglevel); - + readJ2k(cmdline.inputFilename, &jasperP); outpam.file = stdout; @@ -516,13 +517,16 @@ main(int argc, char **argv) computeOutputParm(jasperP, &outpam, &jasperCmpt); pnm_writepaminit(&outpam); - + convertToPamPnm(&outpam, jasperP, jasperCmpt); - + free(jasperCmpt); - jas_image_destroy(jasperP); + jas_image_destroy(jasperP); pm_close(stdout); - + return 0; } + + + diff --git a/converter/other/jpeg2000/libjasper/jp2/jp2_dec.c b/converter/other/jpeg2000/libjasper/jp2/jp2_dec.c index 91ce6c51..a9463a57 100644 --- a/converter/other/jpeg2000/libjasper/jp2/jp2_dec.c +++ b/converter/other/jpeg2000/libjasper/jp2/jp2_dec.c @@ -110,16 +110,6 @@ * __END_OF_JASPER_LICENSE__ */ -/* - * JP2 Library - * - * $Id$ - */ - -/******************************************************************************\ -* Includes. -\******************************************************************************/ - #include "jasper/jas_image.h" #include "jasper/jas_stream.h" #include "jasper/jas_math.h" @@ -130,520 +120,548 @@ #include "jp2_cod.h" #include "jp2_dec.h" -#define JP2_VALIDATELEN (JAS_MIN(JP2_JP_LEN + 16, JAS_STREAM_MAXPUTBACK)) - -static jp2_dec_t *jp2_dec_create(void); -static void jp2_dec_destroy(jp2_dec_t *dec); -static int jp2_getcs(jp2_colr_t *colr); -static int jp2_getct(int colorspace, int type, int assoc); -static int fromiccpcs(int cs); - -/******************************************************************************\ -* Functions. -\******************************************************************************/ - -jas_image_t *jp2_decode(jas_stream_t *in, char *optstr) -{ - jp2_box_t *box; - int found; - jas_image_t *image; - jp2_dec_t *dec; - bool samedtype; - int dtype; - int i; - jp2_cmap_t *cmapd; - jp2_pclr_t *pclrd; - jp2_cdef_t *cdefd; - int channo; - int newcmptno; - int_fast32_t *lutents; - jp2_cmapent_t *cmapent; - unsigned char *iccp; - int cs; - - dec = 0; - box = 0; - image = 0; - - if (!(dec = jp2_dec_create())) { - goto error; - } - - /* Get the first box. This should be a JP box. */ - if (!(box = jp2_box_get(in))) { - jas_eprintf("error: cannot get box\n"); - goto error; - } - if (box->type != JP2_BOX_JP) { - jas_eprintf("error: expecting signature box\n"); - goto error; - } - if (box->data.jp.magic != JP2_JP_MAGIC) { - jas_eprintf("incorrect magic number\n"); - goto error; - } - jp2_box_destroy(box); - box = 0; - - /* Get the second box. This should be a FTYP box. */ - if (!(box = jp2_box_get(in))) { - goto error; - } - if (box->type != JP2_BOX_FTYP) { - jas_eprintf("expecting file type box\n"); - goto error; - } - jp2_box_destroy(box); - box = 0; - - /* Get more boxes... */ - found = 0; - while ((box = jp2_box_get(in))) { - if (jas_getdbglevel() >= 1) { - fprintf(stderr, "box type %s\n", box->info->name); - } - switch (box->type) { - case JP2_BOX_JP2C: - found = 1; - break; - case JP2_BOX_IHDR: - if (!dec->ihdr) { - dec->ihdr = box; - box = 0; - } - break; - case JP2_BOX_BPCC: - if (!dec->bpcc) { - dec->bpcc = box; - box = 0; - } - break; - case JP2_BOX_CDEF: - if (!dec->cdef) { - dec->cdef = box; - box = 0; - } - break; - case JP2_BOX_PCLR: - if (!dec->pclr) { - dec->pclr = box; - box = 0; - } - break; - case JP2_BOX_CMAP: - if (!dec->cmap) { - dec->cmap = box; - box = 0; - } - break; - case JP2_BOX_COLR: - if (!dec->colr) { - dec->colr = box; - box = 0; - } - break; - } - if (box) { - jp2_box_destroy(box); - box = 0; - } - if (found) { - break; - } - } - - if (!found) { - jas_eprintf("error: no code stream found\n"); - goto error; - } - - if (!(dec->image = jpc_decode(in, optstr))) { - jas_eprintf("error: cannot decode code stream\n"); - goto error; - } - - /* An IHDR box must be present. */ - if (!dec->ihdr) { - jas_eprintf("error: missing IHDR box\n"); - goto error; - } - - /* Does the number of components indicated in the IHDR box match - the value specified in the code stream? */ - if (dec->ihdr->data.ihdr.numcmpts != jas_image_numcmpts(dec->image)) { - jas_eprintf("warning: number of components mismatch\n"); - } - - /* At least one component must be present. */ - if (!jas_image_numcmpts(dec->image)) { - jas_eprintf("error: no components\n"); - goto error; - } - - /* Determine if all components have the same data type. */ - samedtype = true; - dtype = jas_image_cmptdtype(dec->image, 0); - for (i = 1; i < jas_image_numcmpts(dec->image); ++i) { - if (jas_image_cmptdtype(dec->image, i) != dtype) { - samedtype = false; - break; - } - } - - /* Is the component data type indicated in the IHDR box consistent - with the data in the code stream? */ - if ((samedtype && dec->ihdr->data.ihdr.bpc != JP2_DTYPETOBPC(dtype)) || - (!samedtype && dec->ihdr->data.ihdr.bpc != JP2_IHDR_BPCNULL)) { - jas_eprintf("warning: component data type mismatch\n"); - } - - /* Can we handle the compression type? */ - if (dec->ihdr->data.ihdr.comptype != JP2_IHDR_COMPTYPE) { - jas_eprintf("error: not capable of this compression type\n"); - goto error; - } - - if (dec->bpcc) { - /* Is the number of components indicated in the BPCC box - consistent with the code stream data? */ - if (dec->bpcc->data.bpcc.numcmpts != jas_image_numcmpts( - dec->image)) { - jas_eprintf("warning: number of components mismatch\n"); - } - /* Is the component data type information indicated in the BPCC - box consistent with the code stream data? */ - if (!samedtype) { - for (i = 0; i < jas_image_numcmpts(dec->image); ++i) { - if (jas_image_cmptdtype(dec->image, i) != JP2_BPCTODTYPE(dec->bpcc->data.bpcc.bpcs[i])) { - jas_eprintf("warning: component data type mismatch\n"); - } - } - } else { - jas_eprintf("warning: superfluous BPCC box\n"); - } - } - - /* A COLR box must be present. */ - if (!dec->colr) { - jas_eprintf("error: no COLR box\n"); - goto error; - } - - switch (dec->colr->data.colr.method) { - case JP2_COLR_ENUM: - jas_image_setcolorspace(dec->image, jp2_getcs(&dec->colr->data.colr)); - break; - case JP2_COLR_ICC: - if (dec->colr->data.colr.iccplen < 128) { - abort(); - } - iccp = dec->colr->data.colr.iccp; - cs = (iccp[16] << 24) | (iccp[17] << 16) | (iccp[18] << 8) | - iccp[19]; - if (jas_getdbglevel() > 1) - jas_eprintf("ICC Profile CS %08x\n", cs); - jas_image_setcolorspace(dec->image, fromiccpcs(cs)); - break; - } - - /* If a CMAP box is present, a PCLR box must also be present. */ - if (dec->cmap && !dec->pclr) { - jas_eprintf("warning: missing PCLR box or superfluous CMAP box\n"); - jp2_box_destroy(dec->cmap); - dec->cmap = 0; - } - - /* If a CMAP box is not present, a PCLR box must not be present. */ - if (!dec->cmap && dec->pclr) { - jas_eprintf("warning: missing CMAP box or superfluous PCLR box\n"); - jp2_box_destroy(dec->pclr); - dec->pclr = 0; - } - - /* Determine the number of channels (which is essentially the number - of components after any palette mappings have been applied). */ - dec->numchans = dec->cmap ? dec->cmap->data.cmap.numchans : jas_image_numcmpts(dec->image); - - /* Perform a basic sanity check on the CMAP box if present. */ - if (dec->cmap) { - for (i = 0; i < dec->numchans; ++i) { - /* Is the component number reasonable? */ - if (dec->cmap->data.cmap.ents[i].cmptno >= jas_image_numcmpts(dec->image)) { - jas_eprintf("error: invalid component number in CMAP box\n"); - goto error; - } - /* Is the LUT index reasonable? */ - if (dec->cmap->data.cmap.ents[i].pcol >= dec->pclr->data.pclr.numchans) { - jas_eprintf("error: invalid CMAP LUT index\n"); - goto error; - } - } - } - - /* Allocate space for the channel-number to component-number LUT. */ - if (!(dec->chantocmptlut = jas_malloc(dec->numchans * sizeof(uint_fast16_t)))) { - jas_eprintf("error: no memory\n"); - goto error; - } - - if (!dec->cmap) { - for (i = 0; i < dec->numchans; ++i) { - dec->chantocmptlut[i] = i; - } - } else { - cmapd = &dec->cmap->data.cmap; - pclrd = &dec->pclr->data.pclr; - cdefd = &dec->cdef->data.cdef; - for (channo = 0; channo < cmapd->numchans; ++channo) { - cmapent = &cmapd->ents[channo]; - if (cmapent->map == JP2_CMAP_DIRECT) { - dec->chantocmptlut[channo] = channo; - } else if (cmapent->map == JP2_CMAP_PALETTE) { - lutents = jas_malloc(pclrd->numlutents * sizeof(int_fast32_t)); - for (i = 0; i < pclrd->numlutents; ++i) { - lutents[i] = pclrd->lutdata[cmapent->pcol + i * pclrd->numchans]; - } - newcmptno = jas_image_numcmpts(dec->image); - jas_image_depalettize(dec->image, cmapent->cmptno, pclrd->numlutents, lutents, JP2_BPCTODTYPE(pclrd->bpc[cmapent->pcol]), newcmptno); - dec->chantocmptlut[channo] = newcmptno; - jas_free(lutents); -#if 0 - if (dec->cdef) { - cdefent = jp2_cdef_lookup(cdefd, channo); - if (!cdefent) { - abort(); - } - jas_image_setcmpttype(dec->image, newcmptno, jp2_getct(jas_image_colorspace(dec->image), cdefent->type, cdefent->assoc)); - } else { - jas_image_setcmpttype(dec->image, newcmptno, jp2_getct(jas_image_colorspace(dec->image), 0, channo + 1)); - } -#endif - } - } - } - - /* Mark all components as being of unknown type. */ - - for (i = 0; i < jas_image_numcmpts(dec->image); ++i) { - jas_image_setcmpttype(dec->image, i, JAS_IMAGE_CT_UNKNOWN); - } - - /* Determine the type of each component. */ - if (dec->cdef) { - for (i = 0; i < dec->numchans; ++i) { - jas_image_setcmpttype(dec->image, - dec->chantocmptlut[dec->cdef->data.cdef.ents[i].channo], - jp2_getct(jas_image_colorspace(dec->image), - dec->cdef->data.cdef.ents[i].type, dec->cdef->data.cdef.ents[i].assoc)); - } - } else { - for (i = 0; i < dec->numchans; ++i) { - jas_image_setcmpttype(dec->image, dec->chantocmptlut[i], - jp2_getct(jas_image_colorspace(dec->image), 0, i + 1)); - } - } - - /* Delete any components that are not of interest. */ - for (i = jas_image_numcmpts(dec->image) - 1; i >= 0; --i) { - if (jas_image_cmpttype(dec->image, i) == JAS_IMAGE_CT_UNKNOWN) { - jas_image_delcmpt(dec->image, i); - } - } - - /* Ensure that some components survived. */ - if (!jas_image_numcmpts(dec->image)) { - jas_eprintf("error: no components\n"); - goto error; - } - - /* Prevent the image from being destroyed later. */ - image = dec->image; - dec->image = 0; - - jp2_dec_destroy(dec); - - return image; +#define JP2_VALIDATELEN (JAS_MIN(JP2_JP_LEN + 16, JAS_STREAM_MAXPUTBACK)) -error: - if (box) { - jp2_box_destroy(box); - } - if (dec) { - jp2_dec_destroy(dec); - } - return 0; + + +static jp2_dec_t * +jp2_dec_create(void) { + + jp2_dec_t *dec; + + dec = jas_malloc(sizeof(jp2_dec_t)); + if (dec) { + dec->ihdr = 0; + dec->bpcc = 0; + dec->cdef = 0; + dec->pclr = 0; + dec->image = 0; + dec->chantocmptlut = 0; + dec->cmap = 0; + dec->colr = 0; + } + return dec; } -int jp2_validate(jas_stream_t *in) -{ - char buf[JP2_VALIDATELEN]; - int i; - int n; - - assert(JAS_STREAM_MAXPUTBACK >= JP2_VALIDATELEN); - - /* Read the validation data (i.e., the data used for detecting - the format). */ - if ((n = jas_stream_read(in, buf, JP2_VALIDATELEN)) < 0) { - return -1; - } - - /* Put the validation data back onto the stream, so that the - stream position will not be changed. */ - for (i = n - 1; i >= 0; --i) { - if (jas_stream_ungetc(in, buf[i]) == EOF) { - return -1; - } - } - - /* Did we read enough data? */ - if (n < JP2_VALIDATELEN) { - return -1; - } - - /* Is the box type correct? */ - if (((buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]) != - JP2_BOX_JP) - { - return -1; - } - - return 0; + + +static void +jp2_dec_destroy(jp2_dec_t *dec) { + + if (dec->ihdr) + jp2_box_destroy(dec->ihdr); + + if (dec->bpcc) + jp2_box_destroy(dec->bpcc); + + if (dec->cdef) + jp2_box_destroy(dec->cdef); + + if (dec->pclr) + jp2_box_destroy(dec->pclr); + + if (dec->image) + jas_image_destroy(dec->image); + + if (dec->cmap) + jp2_box_destroy(dec->cmap); + + if (dec->colr) + jp2_box_destroy(dec->colr); + + if (dec->chantocmptlut) + jas_free(dec->chantocmptlut); + + jas_free(dec); } -static jp2_dec_t *jp2_dec_create(void) -{ - jp2_dec_t *dec; - - if (!(dec = jas_malloc(sizeof(jp2_dec_t)))) { - return 0; - } - dec->ihdr = 0; - dec->bpcc = 0; - dec->cdef = 0; - dec->pclr = 0; - dec->image = 0; - dec->chantocmptlut = 0; - dec->cmap = 0; - dec->colr = 0; - return dec; + + +static int +jp2_getct(int colorspace, int type, int assoc) { + + if (type == 1 && assoc == 0) + return JAS_IMAGE_CT_OPACITY; + + if (type == 0 && assoc >= 1 && assoc <= 65534) { + switch (colorspace) { + case JAS_IMAGE_CS_RGB: + switch (assoc) { + case JP2_CDEF_RGB_R: + return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R); + break; + case JP2_CDEF_RGB_G: + return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G); + break; + case JP2_CDEF_RGB_B: + return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B); + break; + } + break; + case JAS_IMAGE_CS_YCBCR: + switch (assoc) { + case JP2_CDEF_YCBCR_Y: + return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_Y); + break; + case JP2_CDEF_YCBCR_CB: + return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CB); + break; + case JP2_CDEF_YCBCR_CR: + return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CR); + break; + } + break; + case JAS_IMAGE_CS_GRAY: + switch (assoc) { + case JP2_CDEF_GRAY_Y: + return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y); + break; + } + break; +#if 0 + case JAS_IMAGE_CS_ICC: +#endif + default: + return JAS_IMAGE_CT_COLOR(assoc - 1); + break; + } + } + return JAS_IMAGE_CT_UNKNOWN; } -static void jp2_dec_destroy(jp2_dec_t *dec) -{ - if (dec->ihdr) { - jp2_box_destroy(dec->ihdr); - } - if (dec->bpcc) { - jp2_box_destroy(dec->bpcc); - } - if (dec->cdef) { - jp2_box_destroy(dec->cdef); - } - if (dec->pclr) { - jp2_box_destroy(dec->pclr); - } - if (dec->image) { - jas_image_destroy(dec->image); - } - if (dec->cmap) { - jp2_box_destroy(dec->cmap); - } - if (dec->colr) { - jp2_box_destroy(dec->colr); - } - if (dec->chantocmptlut) { - jas_free(dec->chantocmptlut); - } - jas_free(dec); + + +static int +jp2_getcs(jp2_colr_t *colr) { + + if (colr->method == JP2_COLR_ENUM) { + switch (colr->csid) { + case JP2_COLR_SRGB: + return JAS_IMAGE_CS_RGB; + break; + case JP2_COLR_SYCC: + return JAS_IMAGE_CS_YCBCR; + break; + case JP2_COLR_SGRAY: + return JAS_IMAGE_CS_GRAY; + break; + } + } + return JAS_IMAGE_CS_UNKNOWN; } +static int +fromiccpcs(int cs) { + + switch (cs) { + case ICC_CS_RGB: + return JAS_IMAGE_CS_RGB; + break; + case ICC_CS_YCBCR: + return JAS_IMAGE_CS_YCBCR; + break; + case ICC_CS_GRAY: + return JAS_IMAGE_CS_GRAY; + break; + } + return JAS_IMAGE_CS_UNKNOWN; +} -static int jp2_getct(int colorspace, int type, int assoc) -{ - if (type == 1 && assoc == 0) { - return JAS_IMAGE_CT_OPACITY; - } - if (type == 0 && assoc >= 1 && assoc <= 65534) { - switch (colorspace) { - case JAS_IMAGE_CS_RGB: - switch (assoc) { - case JP2_CDEF_RGB_R: - return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R); - break; - case JP2_CDEF_RGB_G: - return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G); - break; - case JP2_CDEF_RGB_B: - return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B); - break; - } - break; - case JAS_IMAGE_CS_YCBCR: - switch (assoc) { - case JP2_CDEF_YCBCR_Y: - return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_Y); - break; - case JP2_CDEF_YCBCR_CB: - return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CB); - break; - case JP2_CDEF_YCBCR_CR: - return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CR); - break; - } - break; - case JAS_IMAGE_CS_GRAY: - switch (assoc) { - case JP2_CDEF_GRAY_Y: - return JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y); - break; - } - break; +jas_image_t * +jp2_decode(jas_stream_t *in, char *optstr) { + + jp2_box_t *box; + int found; + jas_image_t *image; + jp2_dec_t *dec; + bool samedtype; + int dtype; + int i; + jp2_cmap_t *cmapd; + jp2_pclr_t *pclrd; + int channo; + int newcmptno; + int_fast32_t *lutents; + jp2_cmapent_t *cmapent; + unsigned char *iccp; + int cs; + + dec = 0; + box = 0; + image = 0; + + if (!(dec = jp2_dec_create())) { + goto error; + } + + /* Get the first box. This should be a JP box. */ + if (!(box = jp2_box_get(in))) { + jas_eprintf("error: cannot get box\n"); + goto error; + } + if (box->type != JP2_BOX_JP) { + jas_eprintf("error: expecting signature box\n"); + goto error; + } + if (box->data.jp.magic != JP2_JP_MAGIC) { + jas_eprintf("incorrect magic number\n"); + goto error; + } + jp2_box_destroy(box); + box = 0; + + /* Get the second box. This should be a FTYP box. */ + if (!(box = jp2_box_get(in))) { + goto error; + } + if (box->type != JP2_BOX_FTYP) { + jas_eprintf("expecting file type box\n"); + goto error; + } + jp2_box_destroy(box); + box = 0; + + /* Get more boxes... */ + found = 0; + while ((box = jp2_box_get(in))) { + if (jas_getdbglevel() >= 1) { + fprintf(stderr, "box type %s\n", box->info->name); + } + switch (box->type) { + case JP2_BOX_JP2C: + found = 1; + break; + case JP2_BOX_IHDR: + if (!dec->ihdr) { + dec->ihdr = box; + box = 0; + } + break; + case JP2_BOX_BPCC: + if (!dec->bpcc) { + dec->bpcc = box; + box = 0; + } + break; + case JP2_BOX_CDEF: + if (!dec->cdef) { + dec->cdef = box; + box = 0; + } + break; + case JP2_BOX_PCLR: + if (!dec->pclr) { + dec->pclr = box; + box = 0; + } + break; + case JP2_BOX_CMAP: + if (!dec->cmap) { + dec->cmap = box; + box = 0; + } + break; + case JP2_BOX_COLR: + if (!dec->colr) { + dec->colr = box; + box = 0; + } + break; + } + if (box) { + jp2_box_destroy(box); + box = 0; + } + if (found) { + break; + } + } + + if (!found) { + jas_eprintf("error: no code stream found\n"); + goto error; + } + + if (!(dec->image = jpc_decode(in, optstr))) { + jas_eprintf("error: cannot decode code stream\n"); + goto error; + } + + /* An IHDR box must be present. */ + if (!dec->ihdr) { + jas_eprintf("error: missing IHDR box\n"); + goto error; + } + + /* Does the number of components indicated in the IHDR box match + the value specified in the code stream? */ + if (dec->ihdr->data.ihdr.numcmpts != jas_image_numcmpts(dec->image)) { + jas_eprintf("warning: number of components mismatch\n"); + } + + /* At least one component must be present. */ + if (!jas_image_numcmpts(dec->image)) { + jas_eprintf("error: no components\n"); + goto error; + } + + /* Determine if all components have the same data type. */ + samedtype = true; + dtype = jas_image_cmptdtype(dec->image, 0); + for (i = 1; i < jas_image_numcmpts(dec->image); ++i) { + if (jas_image_cmptdtype(dec->image, i) != dtype) { + samedtype = false; + break; + } + } + + /* Is the component data type indicated in the IHDR box consistent + with the data in the code stream? */ + if ((samedtype && dec->ihdr->data.ihdr.bpc != JP2_DTYPETOBPC(dtype)) || + (!samedtype && dec->ihdr->data.ihdr.bpc != JP2_IHDR_BPCNULL)) { + jas_eprintf("warning: component data type mismatch\n"); + } + + /* Can we handle the compression type? */ + if (dec->ihdr->data.ihdr.comptype != JP2_IHDR_COMPTYPE) { + jas_eprintf("error: not capable of this compression type\n"); + goto error; + } + + if (dec->bpcc) { + /* Is the number of components indicated in the BPCC box + consistent with the code stream data? */ + if (dec->bpcc->data.bpcc.numcmpts != jas_image_numcmpts( + dec->image)) { + jas_eprintf("warning: number of components mismatch\n"); + } + /* Is the component data type information indicated in the BPCC + box consistent with the code stream data? */ + if (!samedtype) { + for (i = 0; i < jas_image_numcmpts(dec->image); ++i) { + if (jas_image_cmptdtype(dec->image, i) != + JP2_BPCTODTYPE(dec->bpcc->data.bpcc.bpcs[i])) { + jas_eprintf("warning: component data type mismatch\n"); + } + } + } else { + jas_eprintf("warning: superfluous BPCC box\n"); + } + } + + /* A COLR box must be present. */ + if (!dec->colr) { + jas_eprintf("error: no COLR box\n"); + goto error; + } + + switch (dec->colr->data.colr.method) { + case JP2_COLR_ENUM: + jas_image_setcolorspace(dec->image, jp2_getcs(&dec->colr->data.colr)); + break; + case JP2_COLR_ICC: + if (dec->colr->data.colr.iccplen < 128) { + abort(); + } + iccp = dec->colr->data.colr.iccp; + cs = (iccp[16] << 24) | (iccp[17] << 16) | (iccp[18] << 8) | + iccp[19]; + if (jas_getdbglevel() > 1) + jas_eprintf("ICC Profile CS %08x\n", cs); + jas_image_setcolorspace(dec->image, fromiccpcs(cs)); + break; + } + + /* If a CMAP box is present, a PCLR box must also be present. */ + if (dec->cmap && !dec->pclr) { + jas_eprintf("warning: missing PCLR box or superfluous CMAP box\n"); + jp2_box_destroy(dec->cmap); + dec->cmap = 0; + } + + /* If a CMAP box is not present, a PCLR box must not be present. */ + if (!dec->cmap && dec->pclr) { + jas_eprintf("warning: missing CMAP box or superfluous PCLR box\n"); + jp2_box_destroy(dec->pclr); + dec->pclr = 0; + } + + /* Determine the number of channels (which is essentially the number + of components after any palette mappings have been applied). */ + dec->numchans = dec->cmap ? + dec->cmap->data.cmap.numchans : + jas_image_numcmpts(dec->image); + + /* Perform a basic sanity check on the CMAP box if present. */ + if (dec->cmap) { + for (i = 0; i < dec->numchans; ++i) { + /* Is the component number reasonable? */ + if (dec->cmap->data.cmap.ents[i].cmptno >= + jas_image_numcmpts(dec->image)) { + jas_eprintf("error: invalid component number in CMAP box\n"); + goto error; + } + /* Is the LUT index reasonable? */ + if (dec->cmap->data.cmap.ents[i].pcol >= + dec->pclr->data.pclr.numchans) { + jas_eprintf("error: invalid CMAP LUT index\n"); + goto error; + } + } + } + + /* Allocate space for the channel-number to component-number LUT. */ + dec->chantocmptlut = jas_malloc(dec->numchans * sizeof(uint_fast16_t)); + if (!dec->chantocmptlut) { + jas_eprintf("error: no memory\n"); + goto error; + } + + if (!dec->cmap) { + for (i = 0; i < dec->numchans; ++i) { + dec->chantocmptlut[i] = i; + } + } else { + cmapd = &dec->cmap->data.cmap; + pclrd = &dec->pclr->data.pclr; + for (channo = 0; channo < cmapd->numchans; ++channo) { + cmapent = &cmapd->ents[channo]; + if (cmapent->map == JP2_CMAP_DIRECT) { + dec->chantocmptlut[channo] = channo; + } else if (cmapent->map == JP2_CMAP_PALETTE) { + lutents = jas_malloc(pclrd->numlutents * sizeof(int_fast32_t)); + for (i = 0; i < pclrd->numlutents; ++i) { + lutents[i] = + pclrd->lutdata[cmapent->pcol + i * pclrd->numchans]; + } + newcmptno = jas_image_numcmpts(dec->image); + jas_image_depalettize( + dec->image, cmapent->cmptno, pclrd->numlutents, lutents, + JP2_BPCTODTYPE(pclrd->bpc[cmapent->pcol]), newcmptno); + dec->chantocmptlut[channo] = newcmptno; + jas_free(lutents); #if 0 - case JAS_IMAGE_CS_ICC: + if (dec->cdef) { + jp2_cdef_t * const cdefd = &dec->cdef->data.cdef; + cdefent = jp2_cdef_lookup(cdefd, channo); + if (!cdefent) { + abort(); + } + jas_image_setcmpttype( + dec->image, newcmptno, + jp2_getct(jas_image_colorspace(dec->image), + cdefent->type, cdefent->assoc)); + } else { + jas_image_setcmpttype( + dec->image, newcmptno, + jp2_getct(jas_image_colorspace(dec->image), + 0, + channo + 1)); + } #endif - default: - return JAS_IMAGE_CT_COLOR(assoc - 1); - break; - } - } - return JAS_IMAGE_CT_UNKNOWN; -} + } + } + } + + /* Mark all components as being of unknown type. */ + + for (i = 0; i < jas_image_numcmpts(dec->image); ++i) { + jas_image_setcmpttype(dec->image, i, JAS_IMAGE_CT_UNKNOWN); + } + + /* Determine the type of each component. */ + if (dec->cdef) { + unsigned int i; + for (i = 0; i < dec->numchans; ++i) { + jas_image_setcmpttype( + dec->image, + dec->chantocmptlut[dec->cdef->data.cdef.ents[i].channo], + jp2_getct(jas_image_colorspace(dec->image), + dec->cdef->data.cdef.ents[i].type, + dec->cdef->data.cdef.ents[i].assoc)); + } + } else { + unsigned int i; + for (i = 0; i < dec->numchans; ++i) { + jas_image_setcmpttype(dec->image, dec->chantocmptlut[i], + jp2_getct(jas_image_colorspace(dec->image), 0, i + 1)); + } + } + + /* Delete any components that are not of interest. */ + for (i = jas_image_numcmpts(dec->image) - 1; i >= 0; --i) { + if (jas_image_cmpttype(dec->image, i) == JAS_IMAGE_CT_UNKNOWN) { + jas_image_delcmpt(dec->image, i); + } + } + + /* Ensure that some components survived. */ + if (!jas_image_numcmpts(dec->image)) { + jas_eprintf("error: no components\n"); + goto error; + } + + /* Prevent the image from being destroyed later. */ + image = dec->image; + dec->image = 0; + + jp2_dec_destroy(dec); + + return image; -static int jp2_getcs(jp2_colr_t *colr) -{ - if (colr->method == JP2_COLR_ENUM) { - switch (colr->csid) { - case JP2_COLR_SRGB: - return JAS_IMAGE_CS_RGB; - break; - case JP2_COLR_SYCC: - return JAS_IMAGE_CS_YCBCR; - break; - case JP2_COLR_SGRAY: - return JAS_IMAGE_CS_GRAY; - break; - } - } - return JAS_IMAGE_CS_UNKNOWN; +error: + if (box) { + jp2_box_destroy(box); + } + if (dec) { + jp2_dec_destroy(dec); + } + return 0; } -static int fromiccpcs(int cs) -{ - switch (cs) { - case ICC_CS_RGB: - return JAS_IMAGE_CS_RGB; - break; - case ICC_CS_YCBCR: - return JAS_IMAGE_CS_YCBCR; - break; - case ICC_CS_GRAY: - return JAS_IMAGE_CS_GRAY; - break; - } - return JAS_IMAGE_CS_UNKNOWN; + + +int +jp2_validate(jas_stream_t *in) { + + char buf[JP2_VALIDATELEN]; + int i; + int n; + + assert(JAS_STREAM_MAXPUTBACK >= JP2_VALIDATELEN); + + /* Read the validation data (i.e., the data used for detecting + the format). */ + if ((n = jas_stream_read(in, buf, JP2_VALIDATELEN)) < 0) { + return -1; + } + + /* Put the validation data back onto the stream, so that the + stream position will not be changed. */ + for (i = n - 1; i >= 0; --i) { + if (jas_stream_ungetc(in, buf[i]) == EOF) { + return -1; + } + } + + /* Did we read enough data? */ + if (n < JP2_VALIDATELEN) { + return -1; + } + + /* Is the box type correct? */ + if (((buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]) != + JP2_BOX_JP) + { + return -1; + } + + return 0; } + + + diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c b/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c index 72bd0126..cada97f8 100644 --- a/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c +++ b/converter/other/jpeg2000/libjasper/jpc/jpc_dec.c @@ -458,6 +458,12 @@ static int jpc_dec_decode(jpc_dec_t *dec) static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms) { + /* 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 enabled. + */ +#ifdef USING_CRG uint_fast16_t cmptno; jpc_dec_cmpt_t *cmpt; jpc_crg_t *crg; @@ -465,14 +471,10 @@ static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms) 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; - */ } +#endif return 0; } @@ -701,7 +703,6 @@ static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile) 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; @@ -792,14 +793,12 @@ rlvl->bands = 0; 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; } diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c b/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c index d17e9aa3..9db41ca2 100644 --- a/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c +++ b/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c @@ -73,9 +73,9 @@ int ratestrtosize(const char *s, uint_fast32_t rawsize, uint_fast32_t *size); static void pass_destroy(jpc_enc_pass_t *pass); void jpc_enc_dump(jpc_enc_t *enc); -/******************************************************************************\ +/*****************************************************************************\ * Local prototypes. -\******************************************************************************/ +\*****************************************************************************/ void quantize(jas_matrix_t *data, jpc_fix_t stepsize); static int jpc_enc_encodemainhdr(jpc_enc_t *enc); @@ -90,8 +90,11 @@ int setins(int numvalues, jpc_flt_t *values, jpc_flt_t value); static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image); void jpc_enc_cp_destroy(jpc_enc_cp_t *cp); -static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn) -{ + + +static uint_fast32_t +jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn) { + int p; uint_fast32_t mant; uint_fast32_t expn; @@ -111,6 +114,8 @@ static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn) return JPC_QCX_EXPN(expn) | JPC_QCX_MANT(mant); } + + typedef enum { OPT_DEBUG, OPT_IMGAREAOFFX, @@ -199,6 +204,7 @@ jas_taginfo_t modetab[] = { }; + static void tracev(const char * const fmt, va_list args) { @@ -224,12 +230,9 @@ trace(const char * const fmt, ...) { -/******************************************************************************\ -* The main encoder entry point. -\******************************************************************************/ +int +jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr) { -int jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr) -{ jpc_enc_t *enc; jpc_enc_cp_t *cp; @@ -287,12 +290,15 @@ error: return -1; } -/******************************************************************************\ + + +/*****************************************************************************\ * Option parsing code. -\******************************************************************************/ +\*****************************************************************************/ + +static jpc_enc_cp_t * +cp_create(char *optstr, jas_image_t *image) { -static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image) -{ jpc_enc_cp_t *cp; jas_tvparser_t *tvp; int ret; @@ -343,10 +349,12 @@ static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *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) <= + 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, "We don't know how to interpret this image type\n"); + fprintf(stderr, + "We don't know how to interpret this image type\n"); goto error; } /* Note: We ought to be calculating the LCMs here. Fix some day. */ @@ -495,7 +503,8 @@ static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image) if (jpc_atoaf(jas_tvparser_getval(tvp), &numilyrrates, &ilyrrates)) { fprintf(stderr, - "warning: invalid intermediate layer rates specifier ignored (%s)\n", + "warning: invalid intermediate layer rates specifier " + "ignored (%s)\n", jas_tvparser_getval(tvp)); } break; @@ -552,20 +561,26 @@ static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image) 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)) { + 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) { + 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) { + 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; @@ -682,16 +697,20 @@ static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image) /* 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"); + 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"); + 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; } } @@ -718,8 +737,11 @@ error: return 0; } -void jpc_enc_cp_destroy(jpc_enc_cp_t *cp) -{ + + +void +jpc_enc_cp_destroy(jpc_enc_cp_t *cp) { + if (cp->ccps) { if (cp->tcp.ilyrrates) { jas_free(cp->tcp.ilyrrates); @@ -729,8 +751,11 @@ void jpc_enc_cp_destroy(jpc_enc_cp_t *cp) jas_free(cp); } -int ratestrtosize(const char *s, uint_fast32_t rawsize, uint_fast32_t *size) -{ + + +int +ratestrtosize(const char *s, uint_fast32_t rawsize, uint_fast32_t *size) { + char *cp; jpc_flt_t f; @@ -750,12 +775,13 @@ int ratestrtosize(const char *s, uint_fast32_t rawsize, uint_fast32_t *size) return 0; } -/******************************************************************************\ +/*****************************************************************************\ * Encoder constructor and destructor. -\******************************************************************************/ +\*****************************************************************************/ + +jpc_enc_t * +jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image) { -jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image) -{ jpc_enc_t *enc; enc = 0; @@ -788,11 +814,15 @@ error: return 0; } -void jpc_enc_destroy(jpc_enc_t *enc) -{ + + +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. */ + (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); @@ -810,24 +840,22 @@ void jpc_enc_destroy(jpc_enc_t *enc) jas_free(enc); } -/******************************************************************************\ -* Code. -\******************************************************************************/ -static int jpc_enc_encodemainhdr(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; -long startoff; -long mainhdrlen; + 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; @@ -841,7 +869,7 @@ long mainhdrlen; cp = enc->cp; -startoff = jas_stream_getrwcount(enc->out); + startoff = jas_stream_getrwcount(enc->out); /* Write SOC marker segment. */ if (!(enc->mrk = jpc_ms_create(JPC_MS_SOC))) { @@ -922,7 +950,6 @@ startoff = jas_stream_getrwcount(enc->out); 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) { @@ -953,14 +980,17 @@ startoff = jas_stream_getrwcount(enc->out); 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.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]; + 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)) { @@ -1023,17 +1053,23 @@ startoff = jas_stream_getrwcount(enc->out); return 0; } -int jpc_enc_encodetiledata(jpc_enc_t *enc) -{ -assert(enc->tmpstream); + + +int +jpc_enc_encodetiledata(jpc_enc_t *enc) { + + assert(enc->tmpstream); if (jpc_enc_encpkts(enc, enc->tmpstream)) { return -1; } return 0; } -void quantize(jas_matrix_t *data, jpc_fix_t stepsize) -{ + + +void +quantize(jas_matrix_t *data, jpc_fix_t stepsize) { + int i; int j; jpc_fix_t t; @@ -1059,8 +1095,11 @@ void quantize(jas_matrix_t *data, jpc_fix_t stepsize) } } -static void calcrdslopes(jpc_enc_cblk_t *cblk) -{ + + +static void +calcrdslopes(jpc_enc_cblk_t *cblk) { + jpc_enc_pass_t *endpasses; jpc_enc_pass_t *pass0; jpc_enc_pass_t *pass1; @@ -1113,10 +1152,12 @@ static void calcrdslopes(jpc_enc_cblk_t *cblk) #if 0 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); -} + 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); + } } #endif } @@ -1196,8 +1237,8 @@ computeLayerSizes(jpc_enc_t * const encP, -static void dump_layeringinfo(jpc_enc_t *enc) -{ +static void +dump_layeringinfo(jpc_enc_t *enc) { jpc_enc_tcmpt_t *tcmpt; uint_fast16_t tcmptno; @@ -1626,12 +1667,15 @@ performTier2Coding(jpc_enc_t * const encP, JAS_DBGLOG(10, ("done doing rateallocation\n")); } + + /*****************************************************************************\ * Tile constructors and destructors. \*****************************************************************************/ -jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno) -{ +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; @@ -1733,8 +1777,11 @@ error: return 0; } -void jpc_enc_tile_destroy(jpc_enc_tile_t *tile) -{ + + +void +jpc_enc_tile_destroy(jpc_enc_tile_t *tile) { + jpc_enc_tcmpt_t *tcmpt; uint_fast16_t cmptno; @@ -1754,9 +1801,14 @@ void jpc_enc_tile_destroy(jpc_enc_tile_t *tile) 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) -{ + + +static jpc_enc_tcmpt_t * +tcmpt_create(jpc_enc_tcmpt_t * const tcmpt, + jpc_enc_cp_t * const cp, + jas_image_t * const image, + jpc_enc_tile_t * const tile) { + uint_fast16_t cmptno; uint_fast16_t rlvlno; jpc_enc_rlvl_t *rlvl; @@ -1787,15 +1839,15 @@ static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp, 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))) { + tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry); + if (!tcmpt->data) 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)) { + brx - tlx, bry - tly, tcmpt->data)) { goto error; } @@ -1803,9 +1855,11 @@ static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp, 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))) { + + tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1); + + if (!tcmpt->tsfb) goto error; - } for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) { tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno]; @@ -1818,17 +1872,19 @@ static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp, tcmpt->numstepsizes = tcmpt->numbands; assert(tcmpt->numstepsizes <= JPC_MAXBANDS); - memset(tcmpt->stepsizes, 0, sizeof(tcmpt->numstepsizes * - sizeof(uint_fast16_t))); + 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); + 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)))) { + tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)); + if (!tcmpt->rlvls) goto error; - } + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; ++rlvlno, ++rlvl) { rlvl->bands = 0; @@ -1850,8 +1906,11 @@ error: } -static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt) -{ + + +static void +tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt) { + jpc_enc_rlvl_t *rlvl; uint_fast16_t rlvlno; @@ -1871,9 +1930,12 @@ static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt) } } -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) -{ + + +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; @@ -1891,14 +1953,18 @@ static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp, /* 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); + 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; @@ -1917,8 +1983,10 @@ static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp, 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); + 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); @@ -1926,20 +1994,22 @@ static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp, 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->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)))) { + rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)); + if (!rlvl->bands) goto error; - } + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { + ++bandno, ++band) { band->prcs = 0; band->data = 0; band->rlvl = rlvl; } for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { + ++bandno, ++band) { if (!band_create(band, cp, rlvl, bandinfos)) { goto error; } @@ -1952,8 +2022,11 @@ error: return 0; } -static void rlvl_destroy(jpc_enc_rlvl_t *rlvl) -{ + + +static void +rlvl_destroy(jpc_enc_rlvl_t *rlvl) { + jpc_enc_band_t *band; uint_fast16_t bandno; @@ -1966,9 +2039,12 @@ static void rlvl_destroy(jpc_enc_rlvl_t *rlvl) } } -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) -{ + + +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; @@ -1989,42 +2065,46 @@ static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp, bandinfo = &bandinfos[gblbandno]; -if (bandinfo->xstart != bandinfo->xend && bandinfo->ystart != bandinfo->yend) { - if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) { - goto error; + if (bandinfo->xstart != bandinfo->xend && + bandinfo->ystart != bandinfo->yend) { + band->data = jas_seq2d_create(0, 0, 0, 0); + if (!band->data) + 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); } - 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->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)) { + if (band->data) { + band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)); + if (!band->prcs) 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; @@ -2033,8 +2113,11 @@ error: return 0; } -static void band_destroy(jpc_enc_band_t *band) -{ + + +static void +band_destroy(jpc_enc_band_t *band) { + jpc_enc_prc_t *prc; jpc_enc_rlvl_t *rlvl; uint_fast32_t prcno; @@ -2052,8 +2135,11 @@ static void band_destroy(jpc_enc_band_t *band) } } -static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band) -{ + + +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; @@ -2081,32 +2167,34 @@ static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_b rlvl = band->rlvl; tcmpt = rlvl->tcmpt; -rlvlno = rlvl - tcmpt->rlvls; + rlvlno = rlvl - tcmpt->rlvls; 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; -} else { - tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1); - tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1); -} + tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); + tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); - /* Compute the coordinates of the top-left and bottom-right - corners of the precinct. */ + if (!rlvlno) { + tlcbgtlx = tlprctlx; + tlcbgtly = tlprctly; + } else { + 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)); + (1 << rlvl->cbgwidthexpn)); prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly + - (1 << rlvl->cbgheightexpn)); + (1 << rlvl->cbgheightexpn)); if (prc->tlx < prc->brx && prc->tly < prc->bry) { /* The precinct contains at least one code block. */ @@ -2116,33 +2204,35 @@ if (!rlvlno) { brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn); brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn); prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx, - rlvl->cblkwidthexpn); + rlvl->cblkwidthexpn); prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly, - rlvl->cblkheightexpn); + rlvl->cblkheightexpn); prc->numcblks = prc->numhcblks * prc->numvcblks; if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { + prc->numvcblks))) { goto error; } if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { + prc->numvcblks))) { goto error; } if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { + prc->numvcblks))) { goto error; } if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { + prc->numvcblks))) { goto error; } - if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)))) { + prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)); + + if (!prc->cblks) goto error; - } - for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; - ++cblkno, ++cblk) { + for (cblkno = 0, cblk = prc->cblks; + cblkno < prc->numcblks; + ++cblkno, ++cblk) { cblk->passes = 0; cblk->stream = 0; cblk->mqenc = 0; @@ -2150,8 +2240,9 @@ if (!rlvlno) { cblk->flags = 0; cblk->prc = prc; } - for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; - ++cblkno, ++cblk) { + for (cblkno = 0, cblk = prc->cblks; + cblkno < prc->numcblks; + ++cblkno, ++cblk) { if (!cblk_create(cblk, cp, prc)) { goto error; } @@ -2177,8 +2268,11 @@ error: return 0; } -static void prc_destroy(jpc_enc_prc_t *prc) -{ + + +static void +prc_destroy(jpc_enc_prc_t *prc) { + jpc_enc_cblk_t *cblk; uint_fast32_t cblkno; @@ -2203,8 +2297,11 @@ static void prc_destroy(jpc_enc_prc_t *prc) } } -static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc) -{ + + +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; @@ -2241,18 +2338,22 @@ static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_e 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); + 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))) { + cblk->data = jas_seq2d_create(0, 0, 0, 0); + if (!cblk->data) goto error; - } - jas_seq2d_bindsub(cblk->data, band->data, cblktlx, cblktly, cblkbrx, cblkbry); + jas_seq2d_bindsub(cblk->data, band->data, + cblktlx, cblktly, cblkbrx, cblkbry); return cblk; @@ -2261,8 +2362,11 @@ error: return 0; } -static void cblk_destroy(jpc_enc_cblk_t *cblk) -{ + + +static void +cblk_destroy(jpc_enc_cblk_t *cblk) { + uint_fast16_t passno; jpc_enc_pass_t *pass; if (cblk->passes) { @@ -2286,13 +2390,19 @@ static void cblk_destroy(jpc_enc_cblk_t *cblk) } } -static void pass_destroy(jpc_enc_pass_t *pass) -{ + + +static void +pass_destroy(jpc_enc_pass_t *pass) { + /* XXX - need to free resources here */ } -void jpc_enc_dump(jpc_enc_t *enc) -{ + + +void +jpc_enc_dump(jpc_enc_t *enc) { + jpc_enc_tile_t *tile; jpc_enc_tcmpt_t *tcmpt; jpc_enc_rlvl_t *rlvl; @@ -2358,11 +2468,10 @@ void jpc_enc_dump(jpc_enc_t *enc) -static int jpc_enc_encodemainbody(jpc_enc_t *enc) -{ +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; @@ -2376,7 +2485,6 @@ static int jpc_enc_encodemainbody(jpc_enc_t *enc) int adjust; int j; int absbandno; - long numbytes; long tilehdrlen; long tilelen; jpc_enc_tile_t *tile; @@ -2398,13 +2506,7 @@ static int jpc_enc_encodemainbody(jpc_enc_t *enc) cp = enc->cp; - /* Avoid compile warnings. */ - numbytes = 0; - for (tileno = 0; tileno < cp->numtiles; ++tileno) { - tilex = tileno % cp->numhtiles; - tiley = tileno / cp->numhtiles; - enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno); if (!enc->curtile) abort(); @@ -2459,7 +2561,6 @@ static int jpc_enc_encodemainbody(jpc_enc_t *enc) } - endcomps = &tile->tcmpts[tile->numtcmpts]; for (cmptno = 0, comp = tile->tcmpts; comp != endcomps; @@ -2544,15 +2645,16 @@ static int jpc_enc_encodemainbody(jpc_enc_t *enc) return -1; } - if (!(enc->tmpstream = jas_stream_memopen(0, 0))) { + enc->tmpstream = jas_stream_memopen(0, 0); + if (!enc->tmpstream) { fprintf(stderr, "cannot open tmp file\n"); return -1; } /* Write the tile header. */ - if (!(enc->mrk = jpc_ms_create(JPC_MS_SOT))) { + enc->mrk = jpc_ms_create(JPC_MS_SOT); + if (!enc->mrk) return -1; - } sot = &enc->mrk->parms.sot; sot->len = 0; sot->tileno = tileno; diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_t1enc.c b/converter/other/jpeg2000/libjasper/jpc/jpc_t1enc.c index 8aa024a9..e1af0f61 100644 --- a/converter/other/jpeg2000/libjasper/jpc/jpc_t1enc.c +++ b/converter/other/jpeg2000/libjasper/jpc/jpc_t1enc.c @@ -1,4 +1,4 @@ -/* + /* * Copyright (c) 1999-2000 Image Power, Inc. and the University of * British Columbia. * Copyright (c) 2001-2002 Michael David Adams. @@ -156,853 +156,872 @@ static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int, /* Encode all of the code blocks associated with the current tile. */ int jpc_enc_enccblks(jpc_enc_t *enc) { - jpc_enc_tcmpt_t *tcmpt; - 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; - int i; - int j; - int mx; - int bmx; - int v; - jpc_enc_tile_t *tile; - uint_fast32_t prcno; - jpc_enc_prc_t *prc; - - tile = enc->curtile; - - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (tcmpt = tile->tcmpts; tcmpt != endcomps; ++tcmpt) { - endlvls = &tcmpt->rlvls[tcmpt->numrlvls]; - for (lvl = tcmpt->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; - } - bmx = 0; - endcblks = &prc->cblks[prc->numcblks]; - for (cblk = prc->cblks; cblk != endcblks; ++cblk) { - mx = 0; - for (i = 0; i < jas_matrix_numrows(cblk->data); ++i) { - for (j = 0; j < jas_matrix_numcols(cblk->data); ++j) { - v = abs(jas_matrix_get(cblk->data, i, j)); - if (v > mx) { - mx = v; - } - } - } - if (mx > bmx) { - bmx = mx; - } - cblk->numbps = JAS_MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0); - } - - for (cblk = prc->cblks; cblk != endcblks; ++cblk) { - cblk->numimsbs = band->numbps - cblk->numbps; - assert(cblk->numimsbs >= 0); - } - - for (cblk = prc->cblks; cblk != endcblks; ++cblk) { - if (jpc_enc_enccblk(enc, cblk->stream, tcmpt, band, cblk)) { - return -1; - } - } - } - } - } - } - return 0; + jpc_enc_tcmpt_t *tcmpt; + 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; + int i; + int j; + int mx; + int bmx; + int v; + jpc_enc_tile_t *tile; + uint_fast32_t prcno; + jpc_enc_prc_t *prc; + + tile = enc->curtile; + + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (tcmpt = tile->tcmpts; tcmpt != endcomps; ++tcmpt) { + endlvls = &tcmpt->rlvls[tcmpt->numrlvls]; + for (lvl = tcmpt->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; + } + bmx = 0; + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + mx = 0; + for (i = 0; i < jas_matrix_numrows(cblk->data); ++i) { + for (j = 0; j < jas_matrix_numcols(cblk->data); ++j) { + v = abs(jas_matrix_get(cblk->data, i, j)); + if (v > mx) { + mx = v; + } + } + } + if (mx > bmx) { + bmx = mx; + } + cblk->numbps = JAS_MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0); + } + + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + cblk->numimsbs = band->numbps - cblk->numbps; + assert(cblk->numimsbs >= 0); + } + + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + if (jpc_enc_enccblk(enc, cblk->stream, tcmpt, band, cblk)) { + return -1; + } + } + } + } + } + } + return 0; } static int getthebyte(jas_stream_t *in, long off) { - int c; - long oldpos; - oldpos = jas_stream_tell(in); - assert(oldpos >= 0); - jas_stream_seek(in, off, SEEK_SET); - c = jas_stream_peekc(in); - jas_stream_seek(in, oldpos, SEEK_SET); - return c; + int c; + long oldpos; + oldpos = jas_stream_tell(in); + assert(oldpos >= 0); + jas_stream_seek(in, off, SEEK_SET); + c = jas_stream_peekc(in); + jas_stream_seek(in, oldpos, SEEK_SET); + return c; } -/* Encode a single code block. */ -int jpc_enc_enccblk(jpc_enc_t *enc, jas_stream_t *out, jpc_enc_tcmpt_t *tcmpt, jpc_enc_band_t *band, jpc_enc_cblk_t *cblk) -{ - jpc_enc_pass_t *pass; - jpc_enc_pass_t *endpasses; - int bitpos; - int n; - int adjust; - int ret; - int passtype; - int t; - jpc_bitstream_t *bout; - jpc_enc_pass_t *termpass; - jpc_enc_rlvl_t *rlvl; - int vcausal; - int segsym; - int termmode; - int c; - - bout = 0; - rlvl = band->rlvl; - - cblk->stream = jas_stream_memopen(0, 0); - assert(cblk->stream); - cblk->mqenc = jpc_mqenc_create(JPC_NUMCTXS, cblk->stream); - assert(cblk->mqenc); - jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs); - - cblk->numpasses = (cblk->numbps > 0) ? (3 * cblk->numbps - 2) : 0; - if (cblk->numpasses > 0) { - cblk->passes = jas_malloc(cblk->numpasses * sizeof(jpc_enc_pass_t)); - assert(cblk->passes); - } else { - cblk->passes = 0; - } - endpasses = &cblk->passes[cblk->numpasses]; - for (pass = cblk->passes; pass != endpasses; ++pass) { - pass->start = 0; - pass->end = 0; - pass->term = JPC_ISTERMINATED(pass - cblk->passes, 0, cblk->numpasses, (tcmpt->cblksty & JPC_COX_TERMALL) != 0, (tcmpt->cblksty & JPC_COX_LAZY) != 0); - pass->type = JPC_SEGTYPE(pass - cblk->passes, 0, (tcmpt->cblksty & JPC_COX_LAZY) != 0); - pass->lyrno = -1; -if (pass == endpasses - 1) { -assert(pass->term == 1); - pass->term = 1; -} - } - - cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) + 2, - jas_matrix_numcols(cblk->data) + 2); - assert(cblk->flags); - - bitpos = cblk->numbps - 1; - pass = cblk->passes; - n = cblk->numpasses; - while (--n >= 0) { - if (pass->type == JPC_SEG_MQ) { - /* NOP */ - } else { - assert(pass->type == JPC_SEG_RAW); - if (!bout) { - bout = jpc_bitstream_sopen(cblk->stream, "w"); - assert(bout); - } - } +int +jpc_enc_enccblk(jpc_enc_t *enc, jas_stream_t *out, jpc_enc_tcmpt_t *tcmpt, + jpc_enc_band_t *band, jpc_enc_cblk_t *cblk) { +/*---------------------------------------------------------------------------- + Encode a single code block. +-----------------------------------------------------------------------------*/ + jpc_enc_pass_t *pass; + jpc_enc_pass_t *endpasses; + int bitpos; + int n; + int adjust; + int passtype; + int t; + jpc_bitstream_t *bout; + jpc_enc_pass_t *termpass; + int vcausal; + int segsym; + int termmode; + int c; + + bout = 0; + + cblk->stream = jas_stream_memopen(0, 0); + assert(cblk->stream); + cblk->mqenc = jpc_mqenc_create(JPC_NUMCTXS, cblk->stream); + assert(cblk->mqenc); + jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs); + + cblk->numpasses = (cblk->numbps > 0) ? (3 * cblk->numbps - 2) : 0; + if (cblk->numpasses > 0) { + cblk->passes = jas_malloc(cblk->numpasses * sizeof(jpc_enc_pass_t)); + assert(cblk->passes); + } else { + cblk->passes = 0; + } + endpasses = &cblk->passes[cblk->numpasses]; + for (pass = cblk->passes; pass != endpasses; ++pass) { + pass->start = 0; + pass->end = 0; + pass->term = + JPC_ISTERMINATED(pass - cblk->passes, 0, + cblk->numpasses, + (tcmpt->cblksty & JPC_COX_TERMALL) != 0, + (tcmpt->cblksty & JPC_COX_LAZY) != 0); + pass->type = JPC_SEGTYPE(pass - cblk->passes, 0, + (tcmpt->cblksty & JPC_COX_LAZY) != 0); + pass->lyrno = -1; + if (pass == endpasses - 1) { + assert(pass->term == 1); + pass->term = 1; + } + } + + cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) + 2, + jas_matrix_numcols(cblk->data) + 2); + assert(cblk->flags); + + + bitpos = cblk->numbps - 1; + pass = cblk->passes; + n = cblk->numpasses; + while (--n >= 0) { + + if (pass->type == JPC_SEG_MQ) { + /* NOP */ + } else { + assert(pass->type == JPC_SEG_RAW); + if (!bout) { + bout = jpc_bitstream_sopen(cblk->stream, "w"); + assert(bout); + } + } #if 1 - passtype = (pass - cblk->passes + 2) % 3; + passtype = (pass - cblk->passes + 2) % 3; #else - passtype = JPC_PASSTYPE(pass - cblk->passes + 2); + passtype = JPC_PASSTYPE(pass - cblk->passes + 2); #endif - pass->start = jas_stream_tell(cblk->stream); + pass->start = jas_stream_tell(cblk->stream); #if 0 assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream)); #endif - assert(bitpos >= 0); - vcausal = (tcmpt->cblksty & JPC_COX_VSC) != 0; - segsym = (tcmpt->cblksty & JPC_COX_SEGSYM) != 0; - if (pass->term) { - termmode = ((tcmpt->cblksty & JPC_COX_PTERM) ? - JPC_MQENC_PTERM : JPC_MQENC_DEFTERM) + 1; - } else { - termmode = 0; - } - switch (passtype) { - case JPC_SIGPASS: - ret = (pass->type == JPC_SEG_MQ) ? jpc_encsigpass(cblk->mqenc, - bitpos, band->orient, vcausal, cblk->flags, - cblk->data, termmode, &pass->nmsedec) : - jpc_encrawsigpass(bout, bitpos, vcausal, cblk->flags, - cblk->data, termmode, &pass->nmsedec); - break; - case JPC_REFPASS: - ret = (pass->type == JPC_SEG_MQ) ? jpc_encrefpass(cblk->mqenc, - bitpos, vcausal, cblk->flags, cblk->data, termmode, - &pass->nmsedec) : jpc_encrawrefpass(bout, bitpos, - vcausal, cblk->flags, cblk->data, termmode, - &pass->nmsedec); - break; - case JPC_CLNPASS: - assert(pass->type == JPC_SEG_MQ); - ret = jpc_encclnpass(cblk->mqenc, bitpos, band->orient, - vcausal, segsym, cblk->flags, cblk->data, termmode, - &pass->nmsedec); - break; - default: - assert(0); - break; - } - - if (pass->type == JPC_SEG_MQ) { - if (pass->term) { - jpc_mqenc_init(cblk->mqenc); - } - jpc_mqenc_getstate(cblk->mqenc, &pass->mqencstate); - pass->end = jas_stream_tell(cblk->stream); - if (tcmpt->cblksty & JPC_COX_RESET) { - jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs); - } - } else { - if (pass->term) { - if (jpc_bitstream_pending(bout)) { - jpc_bitstream_outalign(bout, 0x2a); - } - jpc_bitstream_close(bout); - bout = 0; - pass->end = jas_stream_tell(cblk->stream); - } else { - pass->end = jas_stream_tell(cblk->stream) + - jpc_bitstream_pending(bout); -/* NOTE - This will not work. need to adjust by # of pending output bytes */ - } - } + assert(bitpos >= 0); + vcausal = (tcmpt->cblksty & JPC_COX_VSC) != 0; + segsym = (tcmpt->cblksty & JPC_COX_SEGSYM) != 0; + if (pass->term) { + termmode = ((tcmpt->cblksty & JPC_COX_PTERM) ? + JPC_MQENC_PTERM : JPC_MQENC_DEFTERM) + 1; + } else { + termmode = 0; + } + switch (passtype) { + case JPC_SIGPASS: + if (pass->type == JPC_SEG_MQ) + jpc_encsigpass(cblk->mqenc, + bitpos, band->orient, vcausal, cblk->flags, + cblk->data, termmode, &pass->nmsedec); + else + jpc_encrawsigpass(bout, bitpos, vcausal, cblk->flags, + cblk->data, termmode, &pass->nmsedec); + break; + case JPC_REFPASS: + if (pass->type == JPC_SEG_MQ) + jpc_encrefpass( + cblk->mqenc, + bitpos, vcausal, cblk->flags, cblk->data, termmode, + &pass->nmsedec); + else + jpc_encrawrefpass(bout, bitpos, + vcausal, cblk->flags, cblk->data, termmode, + &pass->nmsedec); + break; + case JPC_CLNPASS: + assert(pass->type == JPC_SEG_MQ); + jpc_encclnpass( + cblk->mqenc, bitpos, band->orient, + vcausal, segsym, cblk->flags, cblk->data, termmode, + &pass->nmsedec); + break; + default: + assert(false); + break; + } + + if (pass->type == JPC_SEG_MQ) { + if (pass->term) { + jpc_mqenc_init(cblk->mqenc); + } + jpc_mqenc_getstate(cblk->mqenc, &pass->mqencstate); + pass->end = jas_stream_tell(cblk->stream); + if (tcmpt->cblksty & JPC_COX_RESET) { + jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs); + } + } else { + if (pass->term) { + if (jpc_bitstream_pending(bout)) { + jpc_bitstream_outalign(bout, 0x2a); + } + jpc_bitstream_close(bout); + bout = 0; + pass->end = jas_stream_tell(cblk->stream); + } else { + pass->end = jas_stream_tell(cblk->stream) + + jpc_bitstream_pending(bout); + /* NOTE - This will not work. need to adjust by # of pending + output bytes + */ + } + } #if 0 -/* XXX - This assertion fails sometimes when various coding modes are used. -This seems to be harmless, but why does it happen at all? */ -assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream)); + /* XXX - This assertion fails sometimes when various coding modes are used. + This seems to be harmless, but why does it happen at all? */ + assert(jas_stream_tell(cblk->stream) == + jas_stream_getrwcount(cblk->stream)); #endif - pass->wmsedec = jpc_fixtodbl(band->rlvl->tcmpt->synweight) * - jpc_fixtodbl(band->rlvl->tcmpt->synweight) * - jpc_fixtodbl(band->synweight) * - jpc_fixtodbl(band->synweight) * - jpc_fixtodbl(band->absstepsize) * jpc_fixtodbl(band->absstepsize) * - ((double) (1 << bitpos)) * ((double)(1 << bitpos)) * - jpc_fixtodbl(pass->nmsedec); - pass->cumwmsedec = pass->wmsedec; - if (pass != cblk->passes) { - pass->cumwmsedec += pass[-1].cumwmsedec; - } - if (passtype == JPC_CLNPASS) { - --bitpos; - } - ++pass; - } + pass->wmsedec = jpc_fixtodbl(band->rlvl->tcmpt->synweight) * + jpc_fixtodbl(band->rlvl->tcmpt->synweight) * + jpc_fixtodbl(band->synweight) * + jpc_fixtodbl(band->synweight) * + jpc_fixtodbl(band->absstepsize) * jpc_fixtodbl(band->absstepsize) * + ((double) (1 << bitpos)) * ((double)(1 << bitpos)) * + jpc_fixtodbl(pass->nmsedec); + pass->cumwmsedec = pass->wmsedec; + if (pass != cblk->passes) { + pass->cumwmsedec += pass[-1].cumwmsedec; + } + if (passtype == JPC_CLNPASS) { + --bitpos; + } + ++pass; + } #if 0 -dump_passes(cblk->passes, cblk->numpasses, cblk); + dump_passes(cblk->passes, cblk->numpasses, cblk); #endif - n = 0; - endpasses = &cblk->passes[cblk->numpasses]; - for (pass = cblk->passes; pass != endpasses; ++pass) { - if (pass->start < n) { - pass->start = n; - } - if (pass->end < n) { - pass->end = n; - } - if (!pass->term) { - termpass = pass; - while (termpass - pass < cblk->numpasses && - !termpass->term) { - ++termpass; - } - if (pass->type == JPC_SEG_MQ) { - t = (pass->mqencstate.lastbyte == 0xff) ? 1 : 0; - if (pass->mqencstate.ctreg >= 5) { - adjust = 4 + t; - } else { - adjust = 5 + t; - } - pass->end += adjust; - } - if (pass->end > termpass->end) { - pass->end = termpass->end; - } - if ((c = getthebyte(cblk->stream, pass->end - 1)) == EOF) { - abort(); - } - if (c == 0xff) { - ++pass->end; - } - n = JAS_MAX(n, pass->end); - } else { - n = JAS_MAX(n, pass->end); - } - } + n = 0; + endpasses = &cblk->passes[cblk->numpasses]; + for (pass = cblk->passes; pass != endpasses; ++pass) { + if (pass->start < n) { + pass->start = n; + } + if (pass->end < n) { + pass->end = n; + } + if (!pass->term) { + termpass = pass; + while (termpass - pass < cblk->numpasses && + !termpass->term) { + ++termpass; + } + if (pass->type == JPC_SEG_MQ) { + t = (pass->mqencstate.lastbyte == 0xff) ? 1 : 0; + if (pass->mqencstate.ctreg >= 5) { + adjust = 4 + t; + } else { + adjust = 5 + t; + } + pass->end += adjust; + } + if (pass->end > termpass->end) { + pass->end = termpass->end; + } + if ((c = getthebyte(cblk->stream, pass->end - 1)) == EOF) { + abort(); + } + if (c == 0xff) { + ++pass->end; + } + n = JAS_MAX(n, pass->end); + } else { + n = JAS_MAX(n, pass->end); + } + } #if 0 -dump_passes(cblk->passes, cblk->numpasses, cblk); + dump_passes(cblk->passes, cblk->numpasses, cblk); #endif - if (bout) { - jpc_bitstream_close(bout); - } + if (bout) { + jpc_bitstream_close(bout); + } - return 0; + return 0; } -/******************************************************************************\ + + +/*****************************************************************************\ * Code for significance pass. -\******************************************************************************/ +\*****************************************************************************/ -#define sigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, orient, mqenc, vcausalflag) \ +#define sigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, orient, mqenc, vcausalflag) \ { \ - int f; \ - int v; \ - f = *(fp); \ - if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \ - v = (abs(*(dp)) & (one)) ? 1 : 0; \ - jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \ - jpc_mqenc_putbit(mqenc, v); \ - if (v) { \ - *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ - v = ((*(dp) < 0) ? 1 : 0); \ - jpc_mqenc_setcurctx(mqenc, JPC_GETSCCTXNO(f)); \ - jpc_mqenc_putbit(mqenc, v ^ JPC_GETSPB(f)); \ - JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \ - *(fp) |= JPC_SIG; \ - } \ - *(fp) |= JPC_VISIT; \ - } \ + int f; \ + int v; \ + f = *(fp); \ + if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \ + v = (abs(*(dp)) & (one)) ? 1 : 0; \ + jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \ + jpc_mqenc_putbit(mqenc, v); \ + if (v) { \ + *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ + v = ((*(dp) < 0) ? 1 : 0); \ + jpc_mqenc_setcurctx(mqenc, JPC_GETSCCTXNO(f)); \ + jpc_mqenc_putbit(mqenc, v ^ JPC_GETSPB(f)); \ + JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \ + *(fp) |= JPC_SIG; \ + } \ + *(fp) |= JPC_VISIT; \ + } \ } static int jpc_encsigpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec) { - int i; - int j; - int one; - int vscanlen; - int width; - int height; - int frowstep; - int drowstep; - int fstripestep; - int dstripestep; - jpc_fix_t *fstripestart; - jpc_fix_t *dstripestart; - jpc_fix_t *fp; - jpc_fix_t *dp; - jpc_fix_t *fvscanstart; - jpc_fix_t *dvscanstart; - int k; - - *nmsedec = 0; - width = jas_matrix_numcols(data); - height = jas_matrix_numrows(data); - frowstep = jas_matrix_rowstep(flags); - drowstep = jas_matrix_rowstep(data); - fstripestep = frowstep << 2; - dstripestep = drowstep << 2; - - one = 1 << (bitpos + JPC_NUMEXTRABITS); - - fstripestart = jas_matrix_getref(flags, 1, 1); - dstripestart = jas_matrix_getref(data, 0, 0); - for (i = height; i > 0; i -= 4, fstripestart += fstripestep, - dstripestart += dstripestep) { - fvscanstart = fstripestart; - dvscanstart = dstripestart; - vscanlen = JAS_MIN(i, 4); - for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { - fp = fvscanstart; - dp = dvscanstart; - k = vscanlen; - - sigpass_step(fp, frowstep, dp, bitpos, one, - nmsedec, orient, mqenc, vcausalflag); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - sigpass_step(fp, frowstep, dp, bitpos, one, - nmsedec, orient, mqenc, 0); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - sigpass_step(fp, frowstep, dp, bitpos, one, - nmsedec, orient, mqenc, 0); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - sigpass_step(fp, frowstep, dp, bitpos, one, - nmsedec, orient, mqenc, 0); - - } - } - - if (term) { - jpc_mqenc_flush(mqenc, term - 1); - } - - return jpc_mqenc_error(mqenc) ? (-1) : 0; + int i; + int j; + int one; + int vscanlen; + int width; + int height; + int frowstep; + int drowstep; + int fstripestep; + int dstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *dstripestart; + jpc_fix_t *fp; + jpc_fix_t *dp; + jpc_fix_t *fvscanstart; + jpc_fix_t *dvscanstart; + int k; + + *nmsedec = 0; + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << (bitpos + JPC_NUMEXTRABITS); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + sigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, orient, mqenc, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + sigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, orient, mqenc, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + sigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, orient, mqenc, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + sigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, orient, mqenc, 0); + + } + } + + if (term) { + jpc_mqenc_flush(mqenc, term - 1); + } + + return jpc_mqenc_error(mqenc) ? (-1) : 0; } -#define rawsigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, out, vcausalflag) \ +#define rawsigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, out, vcausalflag) \ { \ - jpc_fix_t f = *(fp); \ - jpc_fix_t v; \ - if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \ - v = (abs(*(dp)) & (one)) ? 1 : 0; \ - if ((jpc_bitstream_putbit((out), v)) == EOF) { \ - return -1; \ - } \ - if (v) { \ - *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ - v = ((*(dp) < 0) ? 1 : 0); \ - if (jpc_bitstream_putbit(out, v) == EOF) { \ - return -1; \ - } \ - JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \ - *(fp) |= JPC_SIG; \ - } \ - *(fp) |= JPC_VISIT; \ - } \ + jpc_fix_t f = *(fp); \ + jpc_fix_t v; \ + if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \ + v = (abs(*(dp)) & (one)) ? 1 : 0; \ + if ((jpc_bitstream_putbit((out), v)) == EOF) { \ + return -1; \ + } \ + if (v) { \ + *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ + v = ((*(dp) < 0) ? 1 : 0); \ + if (jpc_bitstream_putbit(out, v) == EOF) { \ + return -1; \ + } \ + JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \ + *(fp) |= JPC_SIG; \ + } \ + *(fp) |= JPC_VISIT; \ + } \ } static int jpc_encrawsigpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec) { - int i; - int j; - int k; - int one; - int vscanlen; - int width; - int height; - int frowstep; - int drowstep; - int fstripestep; - int dstripestep; - jpc_fix_t *fstripestart; - jpc_fix_t *dstripestart; - jpc_fix_t *fp; - jpc_fix_t *dp; - jpc_fix_t *fvscanstart; - jpc_fix_t *dvscanstart; - - *nmsedec = 0; - width = jas_matrix_numcols(data); - height = jas_matrix_numrows(data); - frowstep = jas_matrix_rowstep(flags); - drowstep = jas_matrix_rowstep(data); - fstripestep = frowstep << 2; - dstripestep = drowstep << 2; - - one = 1 << (bitpos + JPC_NUMEXTRABITS); - - fstripestart = jas_matrix_getref(flags, 1, 1); - dstripestart = jas_matrix_getref(data, 0, 0); - for (i = height; i > 0; i -= 4, fstripestart += fstripestep, - dstripestart += dstripestep) { - fvscanstart = fstripestart; - dvscanstart = dstripestart; - vscanlen = JAS_MIN(i, 4); - for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { - fp = fvscanstart; - dp = dvscanstart; - k = vscanlen; - - rawsigpass_step(fp, frowstep, dp, bitpos, one, - nmsedec, out, vcausalflag); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - - rawsigpass_step(fp, frowstep, dp, bitpos, one, - nmsedec, out, 0); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - - rawsigpass_step(fp, frowstep, dp, bitpos, one, - nmsedec, out, 0); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - - rawsigpass_step(fp, frowstep, dp, bitpos, one, - nmsedec, out, 0); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - - } - } - - if (term) { - jpc_bitstream_outalign(out, 0x2a); - } - - return 0; + int i; + int j; + int k; + int one; + int vscanlen; + int width; + int height; + int frowstep; + int drowstep; + int fstripestep; + int dstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *dstripestart; + jpc_fix_t *fp; + jpc_fix_t *dp; + jpc_fix_t *fvscanstart; + jpc_fix_t *dvscanstart; + + *nmsedec = 0; + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << (bitpos + JPC_NUMEXTRABITS); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + rawsigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, out, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + rawsigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, out, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + rawsigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, out, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + rawsigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, out, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + } + } + + if (term) { + jpc_bitstream_outalign(out, 0x2a); + } + + return 0; } /******************************************************************************\ * Code for refinement pass. \******************************************************************************/ -#define refpass_step(fp, dp, bitpos, one, nmsedec, mqenc, vcausalflag) \ +#define refpass_step(fp, dp, bitpos, one, nmsedec, mqenc, vcausalflag) \ { \ - int v; \ - if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \ - (d) = *(dp); \ - *(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \ - jpc_mqenc_setcurctx((mqenc), JPC_GETMAGCTXNO(*(fp))); \ - v = (abs(d) & (one)) ? 1 : 0; \ - jpc_mqenc_putbit((mqenc), v); \ - *(fp) |= JPC_REFINE; \ - } \ + int v; \ + if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \ + (d) = *(dp); \ + *(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \ + jpc_mqenc_setcurctx((mqenc), JPC_GETMAGCTXNO(*(fp))); \ + v = (abs(d) & (one)) ? 1 : 0; \ + jpc_mqenc_putbit((mqenc), v); \ + *(fp) |= JPC_REFINE; \ + } \ } static int jpc_encrefpass(jpc_mqenc_t *mqenc, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec) { - int i; - int j; - int one; - int vscanlen; - int d; - int width; - int height; - int frowstep; - int drowstep; - int fstripestep; - int dstripestep; - jpc_fix_t *fstripestart; - jpc_fix_t *dstripestart; - jpc_fix_t *fvscanstart; - jpc_fix_t *dvscanstart; - jpc_fix_t *dp; - jpc_fix_t *fp; + int i; + int j; + int one; + int vscanlen; + int d; + int width; + int height; + int frowstep; + int drowstep; + int fstripestep; + int dstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *dstripestart; + jpc_fix_t *fvscanstart; + jpc_fix_t *dvscanstart; + jpc_fix_t *dp; + jpc_fix_t *fp; int k; - *nmsedec = 0; - width = jas_matrix_numcols(data); - height = jas_matrix_numrows(data); - frowstep = jas_matrix_rowstep(flags); - drowstep = jas_matrix_rowstep(data); - fstripestep = frowstep << 2; - dstripestep = drowstep << 2; - - one = 1 << (bitpos + JPC_NUMEXTRABITS); - - fstripestart = jas_matrix_getref(flags, 1, 1); - dstripestart = jas_matrix_getref(data, 0, 0); - for (i = height; i > 0; i -= 4, fstripestart += fstripestep, - dstripestart += dstripestep) { - fvscanstart = fstripestart; - dvscanstart = dstripestart; - vscanlen = JAS_MIN(i, 4); - for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { - fp = fvscanstart; - dp = dvscanstart; - k = vscanlen; - - refpass_step(fp, dp, bitpos, one, nmsedec, - mqenc, vcausalflag); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - refpass_step(fp, dp, bitpos, one, nmsedec, - mqenc, 0); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - refpass_step(fp, dp, bitpos, one, nmsedec, - mqenc, 0); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - refpass_step(fp, dp, bitpos, one, nmsedec, - mqenc, 0); - - } - } - - if (term) { - jpc_mqenc_flush(mqenc, term - 1); - } - - return jpc_mqenc_error(mqenc) ? (-1) : 0; + *nmsedec = 0; + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << (bitpos + JPC_NUMEXTRABITS); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + refpass_step(fp, dp, bitpos, one, nmsedec, + mqenc, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + refpass_step(fp, dp, bitpos, one, nmsedec, + mqenc, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + refpass_step(fp, dp, bitpos, one, nmsedec, + mqenc, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + refpass_step(fp, dp, bitpos, one, nmsedec, + mqenc, 0); + + } + } + + if (term) { + jpc_mqenc_flush(mqenc, term - 1); + } + + return jpc_mqenc_error(mqenc) ? (-1) : 0; } -#define rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag) \ +#define rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag) \ { \ - jpc_fix_t d; \ - jpc_fix_t v; \ - if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \ - d = *(dp); \ - *(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \ - v = (abs(d) & (one)) ? 1 : 0; \ - if (jpc_bitstream_putbit((out), v) == EOF) { \ - return -1; \ - } \ - *(fp) |= JPC_REFINE; \ - } \ + jpc_fix_t d; \ + jpc_fix_t v; \ + if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \ + d = *(dp); \ + *(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \ + v = (abs(d) & (one)) ? 1 : 0; \ + if (jpc_bitstream_putbit((out), v) == EOF) { \ + return -1; \ + } \ + *(fp) |= JPC_REFINE; \ + } \ } static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec) { - int i; - int j; - int k; - int one; - int vscanlen; - int width; - int height; - int frowstep; - int drowstep; - int fstripestep; - int dstripestep; - jpc_fix_t *fstripestart; - jpc_fix_t *dstripestart; - jpc_fix_t *fvscanstart; - jpc_fix_t *dvscanstart; - jpc_fix_t *dp; - jpc_fix_t *fp; - - *nmsedec = 0; - width = jas_matrix_numcols(data); - height = jas_matrix_numrows(data); - frowstep = jas_matrix_rowstep(flags); - drowstep = jas_matrix_rowstep(data); - fstripestep = frowstep << 2; - dstripestep = drowstep << 2; - - one = 1 << (bitpos + JPC_NUMEXTRABITS); - - fstripestart = jas_matrix_getref(flags, 1, 1); - dstripestart = jas_matrix_getref(data, 0, 0); - for (i = height; i > 0; i -= 4, fstripestart += fstripestep, - dstripestart += dstripestep) { - fvscanstart = fstripestart; - dvscanstart = dstripestart; - vscanlen = JAS_MIN(i, 4); - for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { - fp = fvscanstart; - dp = dvscanstart; - k = vscanlen; - - rawrefpass_step(fp, dp, bitpos, one, nmsedec, - out, vcausalflag); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - rawrefpass_step(fp, dp, bitpos, one, nmsedec, - out, vcausalflag); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - rawrefpass_step(fp, dp, bitpos, one, nmsedec, - out, vcausalflag); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - rawrefpass_step(fp, dp, bitpos, one, nmsedec, - out, vcausalflag); - - } - } - - if (term) { - jpc_bitstream_outalign(out, 0x2a); - } - - return 0; + int i; + int j; + int k; + int one; + int vscanlen; + int width; + int height; + int frowstep; + int drowstep; + int fstripestep; + int dstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *dstripestart; + jpc_fix_t *fvscanstart; + jpc_fix_t *dvscanstart; + jpc_fix_t *dp; + jpc_fix_t *fp; + + *nmsedec = 0; + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << (bitpos + JPC_NUMEXTRABITS); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + rawrefpass_step(fp, dp, bitpos, one, nmsedec, + out, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + rawrefpass_step(fp, dp, bitpos, one, nmsedec, + out, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + rawrefpass_step(fp, dp, bitpos, one, nmsedec, + out, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + rawrefpass_step(fp, dp, bitpos, one, nmsedec, + out, vcausalflag); + + } + } + + if (term) { + jpc_bitstream_outalign(out, 0x2a); + } + + return 0; } /******************************************************************************\ * Code for cleanup pass. \******************************************************************************/ -#define clnpass_step(fp, frowstep, dp, bitpos, one, orient, nmsedec, mqenc, label1, label2, vcausalflag) \ +#define clnpass_step(fp, frowstep, dp, bitpos, one, orient, nmsedec, mqenc, label1, label2, vcausalflag) \ { \ - int f; \ - int v; \ + int f; \ + int v; \ label1 \ - f = *(fp); \ - if (!(f & (JPC_SIG | JPC_VISIT))) { \ - jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \ - v = (abs(*(dp)) & (one)) ? 1 : 0; \ - jpc_mqenc_putbit((mqenc), v); \ - if (v) { \ + f = *(fp); \ + if (!(f & (JPC_SIG | JPC_VISIT))) { \ + jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \ + v = (abs(*(dp)) & (one)) ? 1 : 0; \ + jpc_mqenc_putbit((mqenc), v); \ + if (v) { \ label2 \ - f = *(fp); \ - /* Coefficient is significant. */ \ - *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ - jpc_mqenc_setcurctx((mqenc), JPC_GETSCCTXNO(f)); \ - v = ((*(dp) < 0) ? 1 : 0); \ - jpc_mqenc_putbit((mqenc), v ^ JPC_GETSPB(f)); \ - JPC_UPDATEFLAGS4((fp), (frowstep), v, vcausalflag); \ - *(fp) |= JPC_SIG; \ - } \ - } \ - *(fp) &= ~JPC_VISIT; \ + f = *(fp); \ + /* Coefficient is significant. */ \ + *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ + jpc_mqenc_setcurctx((mqenc), JPC_GETSCCTXNO(f)); \ + v = ((*(dp) < 0) ? 1 : 0); \ + jpc_mqenc_putbit((mqenc), v ^ JPC_GETSPB(f)); \ + JPC_UPDATEFLAGS4((fp), (frowstep), v, vcausalflag); \ + *(fp) |= JPC_SIG; \ + } \ + } \ + *(fp) &= ~JPC_VISIT; \ } static int jpc_encclnpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag, int segsymflag, jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec) { - int i; - int j; - int k; - int vscanlen; - int v; - int runlen; - jpc_fix_t *fp; - int width; - int height; - jpc_fix_t *dp; - int one; - int frowstep; - int drowstep; - int fstripestep; - int dstripestep; - jpc_fix_t *fstripestart; - jpc_fix_t *dstripestart; - jpc_fix_t *fvscanstart; - jpc_fix_t *dvscanstart; - - *nmsedec = 0; - width = jas_matrix_numcols(data); - height = jas_matrix_numrows(data); - frowstep = jas_matrix_rowstep(flags); - drowstep = jas_matrix_rowstep(data); - fstripestep = frowstep << 2; - dstripestep = drowstep << 2; - - one = 1 << (bitpos + JPC_NUMEXTRABITS); - - fstripestart = jas_matrix_getref(flags, 1, 1); - dstripestart = jas_matrix_getref(data, 0, 0); - for (i = height; i > 0; i -= 4, fstripestart += fstripestep, - dstripestart += dstripestep) { - fvscanstart = fstripestart; - dvscanstart = dstripestart; - vscanlen = JAS_MIN(i, 4); - for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { - - fp = fvscanstart; - if (vscanlen >= 4 && !((*fp) & (JPC_SIG | JPC_VISIT | - JPC_OTHSIGMSK)) && (fp += frowstep, !((*fp) & (JPC_SIG | - JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & - (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, - !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) { - dp = dvscanstart; - for (k = 0; k < vscanlen; ++k) { - v = (abs(*dp) & one) ? 1 : 0; - if (v) { - break; - } - dp += drowstep; - } - runlen = k; - if (runlen >= 4) { - jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO); - jpc_mqenc_putbit(mqenc, 0); - continue; - } - jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO); - jpc_mqenc_putbit(mqenc, 1); - jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO); - jpc_mqenc_putbit(mqenc, runlen >> 1); - jpc_mqenc_putbit(mqenc, runlen & 1); - fp = fvscanstart + frowstep * runlen; - dp = dvscanstart + drowstep * runlen; - k = vscanlen - runlen; - switch (runlen) { - case 0: - goto clnpass_partial0; - break; - case 1: - goto clnpass_partial1; - break; - case 2: - goto clnpass_partial2; - break; - case 3: - goto clnpass_partial3; - break; - } - } else { - runlen = 0; - fp = fvscanstart; - dp = dvscanstart; - k = vscanlen; - goto clnpass_full0; - } - clnpass_step(fp, frowstep, dp, bitpos, one, - orient, nmsedec, mqenc, clnpass_full0:, clnpass_partial0:, vcausalflag); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - clnpass_step(fp, frowstep, dp, bitpos, one, - orient, nmsedec, mqenc, ;, clnpass_partial1:, 0); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - clnpass_step(fp, frowstep, dp, bitpos, one, - orient, nmsedec, mqenc, ;, clnpass_partial2:, 0); - if (--k <= 0) { - continue; - } - fp += frowstep; - dp += drowstep; - clnpass_step(fp, frowstep, dp, bitpos, one, - orient, nmsedec, mqenc, ;, clnpass_partial3:, 0); - } - } - - if (segsymflag) { - jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO); - jpc_mqenc_putbit(mqenc, 1); - jpc_mqenc_putbit(mqenc, 0); - jpc_mqenc_putbit(mqenc, 1); - jpc_mqenc_putbit(mqenc, 0); - } - - if (term) { - jpc_mqenc_flush(mqenc, term - 1); - } - - return jpc_mqenc_error(mqenc) ? (-1) : 0; + int i; + int j; + int k; + int vscanlen; + int v; + int runlen; + jpc_fix_t *fp; + int width; + int height; + jpc_fix_t *dp; + int one; + int frowstep; + int drowstep; + int fstripestep; + int dstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *dstripestart; + jpc_fix_t *fvscanstart; + jpc_fix_t *dvscanstart; + + *nmsedec = 0; + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << (bitpos + JPC_NUMEXTRABITS); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + + fp = fvscanstart; + if (vscanlen >= 4 && !((*fp) & (JPC_SIG | JPC_VISIT | + JPC_OTHSIGMSK)) && (fp += frowstep, !((*fp) & (JPC_SIG | + JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & + (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, + !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) { + dp = dvscanstart; + for (k = 0; k < vscanlen; ++k) { + v = (abs(*dp) & one) ? 1 : 0; + if (v) { + break; + } + dp += drowstep; + } + runlen = k; + if (runlen >= 4) { + jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO); + jpc_mqenc_putbit(mqenc, 0); + continue; + } + jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO); + jpc_mqenc_putbit(mqenc, 1); + jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO); + jpc_mqenc_putbit(mqenc, runlen >> 1); + jpc_mqenc_putbit(mqenc, runlen & 1); + fp = fvscanstart + frowstep * runlen; + dp = dvscanstart + drowstep * runlen; + k = vscanlen - runlen; + switch (runlen) { + case 0: + goto clnpass_partial0; + break; + case 1: + goto clnpass_partial1; + break; + case 2: + goto clnpass_partial2; + break; + case 3: + goto clnpass_partial3; + break; + } + } else { + runlen = 0; + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + goto clnpass_full0; + } + clnpass_step(fp, frowstep, dp, bitpos, one, + orient, nmsedec, mqenc, clnpass_full0:, clnpass_partial0:, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + clnpass_step(fp, frowstep, dp, bitpos, one, + orient, nmsedec, mqenc, ;, clnpass_partial1:, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + clnpass_step(fp, frowstep, dp, bitpos, one, + orient, nmsedec, mqenc, ;, clnpass_partial2:, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + clnpass_step(fp, frowstep, dp, bitpos, one, + orient, nmsedec, mqenc, ;, clnpass_partial3:, 0); + } + } + + if (segsymflag) { + jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO); + jpc_mqenc_putbit(mqenc, 1); + jpc_mqenc_putbit(mqenc, 0); + jpc_mqenc_putbit(mqenc, 1); + jpc_mqenc_putbit(mqenc, 0); + } + + if (term) { + jpc_mqenc_flush(mqenc, term - 1); + } + + return jpc_mqenc_error(mqenc) ? (-1) : 0; } diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_util.c b/converter/other/jpeg2000/libjasper/jpc/jpc_util.c index ecc4b914..fede2bef 100644 --- a/converter/other/jpeg2000/libjasper/jpc/jpc_util.c +++ b/converter/other/jpeg2000/libjasper/jpc/jpc_util.c @@ -6,14 +6,14 @@ */ /* __START_OF_JASPER_LICENSE__ - * + * * JasPer Software License - * + * * IMAGE POWER JPEG-2000 PUBLIC LICENSE * ************************************ - * + * * GRANT: - * + * * Permission is hereby granted, free of charge, to any person (the "User") * obtaining a copy of this software and associated documentation, to deal * in the JasPer Software without restriction, including without limitation @@ -21,22 +21,22 @@ * and/or sell copies of the JasPer Software (in source and binary forms), * and to permit persons to whom the JasPer Software is furnished to do so, * provided further that the License Conditions below are met. - * + * * License Conditions * ****************** - * + * * A. Redistributions of source code must retain the above copyright notice, * and this list of conditions, and the following disclaimer. - * + * * B. Redistributions in binary form must reproduce the above copyright * notice, and this list of conditions, and the following disclaimer in * the documentation and/or other materials provided with the distribution. - * + * * C. Neither the name of Image Power, Inc. nor any other contributor * (including, but not limited to, the University of British Columbia and * Michael David Adams) may be used to endorse or promote products derived * from this software without specific prior written permission. - * + * * D. User agrees that it shall not commence any action against Image Power, * Inc., the University of British Columbia, Michael David Adams, or any * other contributors (collectively "Licensors") for infringement of any @@ -56,17 +56,17 @@ * trade dress, or service mark rights); and (v) divisions, continuations, * renewals, reissues and extensions of the foregoing (as and to the extent * applicable) now existing, hereafter filed, issued or acquired. - * + * * E. If User commences an infringement action against any Licensor(s) then * such Licensor(s) shall have the right to terminate User's license and * all sublicenses that have been granted hereunder by User to other parties. - * + * * F. This software is for use only in hardware or software products that * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license * or right to this Software is granted for products that do not comply * with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased * from the ISO. - * + * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND @@ -106,7 +106,7 @@ * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE * NOTICE SPECIFIED IN THIS SECTION. - * + * * __END_OF_JASPER_LICENSE__ */ diff --git a/converter/other/jpeg2000/pamtojpeg2k.c b/converter/other/jpeg2000/pamtojpeg2k.c index 142e452f..4d73316a 100644 --- a/converter/other/jpeg2000/pamtojpeg2k.c +++ b/converter/other/jpeg2000/pamtojpeg2k.c @@ -8,6 +8,7 @@ *****************************************************************************/ +#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */ #define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */ #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ /* In 2014.09, this was _XOPEN_SOURCE 600, with a comment saying it was @@ -97,7 +98,7 @@ parseCommandLine(int argc, char ** argv, char * modeOpt; unsigned int option_def_index; - + MALLOCARRAY_NOFAIL(option_def, 100); option_def_index = 0; /* incremented by OPTENTRY */ @@ -133,25 +134,25 @@ parseCommandLine(int argc, char ** argv, &numrlvlsSpec, 0); OPTENT3(0, "numgbits", OPT_UINT, &cmdlineP->numgbits, &numgbitsSpec, 0); - OPTENT3(0, "nomct", OPT_FLAG, NULL, + OPTENT3(0, "nomct", OPT_FLAG, NULL, &cmdlineP->nomct, 0); - OPTENT3(0, "sop", OPT_FLAG, NULL, + OPTENT3(0, "sop", OPT_FLAG, NULL, &cmdlineP->sop, 0); - OPTENT3(0, "eph", OPT_FLAG, NULL, + OPTENT3(0, "eph", OPT_FLAG, NULL, &cmdlineP->eph, 0); - OPTENT3(0, "lazy", OPT_FLAG, NULL, + OPTENT3(0, "lazy", OPT_FLAG, NULL, &cmdlineP->lazy, 0); - OPTENT3(0, "termall", OPT_FLAG, NULL, + OPTENT3(0, "termall", OPT_FLAG, NULL, &cmdlineP->termall, 0); - OPTENT3(0, "segsym", OPT_FLAG, NULL, + OPTENT3(0, "segsym", OPT_FLAG, NULL, &cmdlineP->segsym, 0); - OPTENT3(0, "vcausal", OPT_FLAG, NULL, + OPTENT3(0, "vcausal", OPT_FLAG, NULL, &cmdlineP->vcausal, 0); - OPTENT3(0, "pterm", OPT_FLAG, NULL, + OPTENT3(0, "pterm", OPT_FLAG, NULL, &cmdlineP->pterm, 0); - OPTENT3(0, "resetprob", OPT_FLAG, NULL, + OPTENT3(0, "resetprob", OPT_FLAG, NULL, &cmdlineP->resetprob, 0); - OPTENT3(0, "verbose", OPT_FLAG, NULL, + OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); OPTENT3(0, "debuglevel", OPT_UINT, &cmdlineP->debuglevel, &debuglevelSpec, 0); @@ -222,7 +223,7 @@ parseCommandLine(int argc, char ** argv, cmdlineP->inputFilename = strdup("-"); /* he wants stdin */ else if (argc - 1 == 1) cmdlineP->inputFilename = strdup(argv[1]); - else + else pm_error("Too many arguments. The only argument accepted\n" "is the input file specification"); @@ -231,7 +232,7 @@ parseCommandLine(int argc, char ** argv, static void -createJasperRaster(struct pam * const inpamP, +createJasperRaster(struct pam * const inpamP, jas_image_t * const jasperP) { /*---------------------------------------------------------------------------- Create the raster in the *jasperP object, reading the raster from the @@ -253,7 +254,7 @@ createJasperRaster(struct pam * const inpamP, if (matrix[plane] == NULL) pm_error("Unable to create matrix for plane %u. " "jas_matrix_create() failed.", plane); - } + } tuplerow = pnm_allocpamrow(inpamP); jasperMaxval = pm_bitstomaxval(pm_maxvaltobits(inpamP->maxval)); @@ -270,7 +271,7 @@ createJasperRaster(struct pam * const inpamP, unsigned int jasperSample; if (oddMaxval) - jasperSample = tuplerow[col][plane] * + jasperSample = tuplerow[col][plane] * jasperMaxval / inpamP->maxval; else jasperSample = tuplerow[col][plane]; @@ -278,16 +279,16 @@ createJasperRaster(struct pam * const inpamP, jas_matrix_set(matrix[plane], 0, col, jasperSample); } } - { + { unsigned int plane; for (plane = 0; plane < inpamP->depth; ++plane) { int rc; - rc = jas_image_writecmpt(jasperP, plane, 0, row, + rc = jas_image_writecmpt(jasperP, plane, 0, row, inpamP->width, 1, matrix[plane]); if (rc != 0) - pm_error("jas_image_writecmpt() of plane %u failed.", + pm_error("jas_image_writecmpt() of plane %u failed.", plane); } } @@ -296,14 +297,14 @@ createJasperRaster(struct pam * const inpamP, pnm_freepamrow(tuplerow); for (plane = 0; plane < inpamP->depth; ++plane) jas_matrix_destroy(matrix[plane]); - + free(matrix); } static void -createJasperImage(struct pam * const inpamP, +createJasperImage(struct pam * const inpamP, jas_image_t ** const jasperPP) { jas_image_cmptparm_t * cmptparms; @@ -321,7 +322,7 @@ createJasperImage(struct pam * const inpamP, cmptparms[plane].prec = pm_maxvaltobits(inpamP->maxval); cmptparms[plane].sgnd = 0; } - *jasperPP = + *jasperPP = jas_image_create(inpamP->depth, cmptparms, JAS_CLRSPC_UNKNOWN); if (*jasperPP == NULL) pm_error("Unable to create jasper image structure. " @@ -340,7 +341,7 @@ convertToJasperImage(struct pam * const inpamP, createJasperImage(inpamP, &jasperP); - if (strncmp(inpamP->tuple_type, "RGB", 3) == 0) { + if (strneq(inpamP->tuple_type, "RGB", 3)) { if (inpamP->depth < 3) pm_error("Input tuple type is RGB*, but depth is only %d. " "It should be at least 3.", inpamP->depth); @@ -354,8 +355,8 @@ convertToJasperImage(struct pam * const inpamP, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B)); } } else { - if (strncmp(inpamP->tuple_type, "GRAYSCALE", 9) == 0 || - strncmp(inpamP->tuple_type, "BLACKANDWHITE", 13) == 0) { + if (strneq(inpamP->tuple_type, "GRAYSCALE", 9) || + strneq(inpamP->tuple_type, "BLACKANDWHITE", 13)) { jas_image_setclrspc(jasperP, JAS_CLRSPC_GENGRAY); jas_image_setcmpttype(jasperP, 0, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y)); @@ -370,7 +371,7 @@ convertToJasperImage(struct pam * const inpamP, static void -writeJpc(jas_image_t * const jasperP, +writeJpc(jas_image_t * const jasperP, struct cmdlineInfo const cmdline, FILE * const ofP) { @@ -382,8 +383,8 @@ writeJpc(jas_image_t * const jasperP, /* Note: ilyrrates is a hack because we're too lazy to properly parse command line options to get the information and then compose - a proper input to Jasper. So the user can screw things up by - specifying garbage for the -ilyrrates option + a proper input to Jasper. So the user can screw things up by + specifying garbage for the -ilyrrates option */ if (strlen(cmdline.ilyrrates) > 0) pm_asprintf(&ilyrratesOpt, "ilyrrates=%s", cmdline.ilyrrates); @@ -409,7 +410,7 @@ writeJpc(jas_image_t * const jasperP, */ rateOpt[0] = '\0'; } - pm_asprintf(&options, + pm_asprintf(&options, "imgareatlx=%u " "imgareatly=%u " "tilegrdtlx=%u " @@ -427,7 +428,7 @@ writeJpc(jas_image_t * const jasperP, "numrlvls=%u " "numgbits=%u " "%s %s %s %s %s %s %s %s %s", - + cmdline.imgareatlx, cmdline.imgareatly, cmdline.tilegrdtlx, @@ -470,8 +471,8 @@ writeJpc(jas_image_t * const jasperP, pm_message("Using Jasper to encode to 'jpc' format with options " "'%s'", options); - rc = jas_image_encode(jasperP, outStreamP, - jas_image_strtofmt((char*)"jpc"), + rc = jas_image_encode(jasperP, outStreamP, + jas_image_strtofmt((char*)"jpc"), (char *)options); if (rc != 0) pm_error("jas_image_encode() failed to encode the JPEG 2000 " @@ -483,11 +484,11 @@ writeJpc(jas_image_t * const jasperP, int rc; rc = jas_stream_close(outStreamP); - + if (rc != 0) pm_error("Failed to close output stream, " "jas_stream_close() rc = %d", rc); - } + } jas_image_clearfmts(); @@ -505,33 +506,33 @@ main(int argc, char **argv) jas_image_t * jasperP; pnm_init(&argc, argv); - + parseCommandLine(argc, argv, &cmdline); - - { + + { int rc; - + rc = jas_init(); if ( rc != 0 ) pm_error("Failed to initialize Jasper library. " "jas_init() returns rc %d", rc ); } - + jas_setdbglevel(cmdline.debuglevel); - + ifP = pm_openr(cmdline.inputFilename); - + pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); - + convertToJasperImage(&inpam, &jasperP); - + writeJpc(jasperP, cmdline, stdout); - + jas_image_destroy(jasperP); pm_close(ifP); pm_close(stdout); - + return 0; } |