void jpc_mqenc_setctxs(jpc_mqenc_t *mqenc, int numctxs, jpc_mqctx_t *ctxs) { jpc_mqstate_t **ctx; int n; ctx = mqenc->ctxs; n = JAS_MIN(mqenc->maxctxs, numctxs); while (--n >= 0) { *ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps]; ++ctx; ++ctxs; } n = mqenc->maxctxs - numctxs; while (--n >= 0) { *ctx = &jpc_mqstates[0]; ++ctx; } }
int jas_getopt(int argc, char **argv, jas_opt_t *opts) { char *cp; int id; int hasarg; jas_opt_t *opt; char *s; if (!jas_optind) { jas_optind = JAS_MIN(1, argc); } while (jas_optind < argc) { s = cp = argv[jas_optind]; if (*cp == '-') { /* We are processing an option. */ ++jas_optind; if (*++cp == '-') { /* We are processing a long option. */ ++cp; if (*cp == '\0') { /* This is the end of the options. */ return JAS_GETOPT_EOF; } if (!(opt = jas_optlookup(opts, cp))) { if (jas_opterr) { jas_eprintf("unknown long option %s\n", s); } return JAS_GETOPT_ERR; } hasarg = (opt->flags & JAS_OPT_HASARG) != 0; id = opt->id; } else { /* We are processing a short option. */ if (strlen(cp) != 1 || !(opt = jas_optlookup(opts, cp))) { if (jas_opterr) { jas_eprintf("unknown short option %s\n", s); } return JAS_GETOPT_ERR; } hasarg = (opt->flags & JAS_OPT_HASARG) != 0; id = opt->id; } if (hasarg) { /* The option has an argument. */ if (jas_optind >= argc) { if (jas_opterr) { jas_eprintf("missing argument for option %s\n", s); } return JAS_GETOPT_ERR; } jas_optarg = argv[jas_optind]; ++jas_optind; } else { /* The option does not have an argument. */ jas_optarg = 0; } return id; } else { /* We are not processing an option. */ return JAS_GETOPT_EOF; } } return JAS_GETOPT_EOF; }
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);
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; }
static int dec_clnpass(jpc_dec_t *dec, register jpc_mqdec_t *mqdec, int bitpos, int orient, int vcausalflag, int segsymflag, jas_matrix_t *flags, jas_matrix_t *data) { int i; int j; int k; int vscanlen; int v; int half; int runlen; int f; int width; int height; int one; int oneplushalf; jpc_fix_t *fp; int frowstep; int fstripestep; jpc_fix_t *fstripestart; jpc_fix_t *fvscanstart; jpc_fix_t *dp; int drowstep; int dstripestep; jpc_fix_t *dstripestart; jpc_fix_t *dvscanstart; /* Avoid compiler warning about unused parameters. */ dec = 0; one = 1 << bitpos; half = one >> 1; oneplushalf = one | half; 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; fstripestart = jas_matrix_getref(flags, 1, 1); dstripestart = jas_matrix_getref(data, 0, 0); for (i = 0; i < height; i += 4, fstripestart += fstripestep, dstripestart += dstripestep) { fvscanstart = fstripestart; dvscanstart = dstripestart; vscanlen = JAS_MIN(4, height - i); 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)))) { jpc_mqdec_setcurctx(mqdec, JPC_AGGCTXNO); JPC_T1D_GETBIT(mqdec, v, "CLN", "AGG"); if (!v) { continue; } jpc_mqdec_setcurctx(mqdec, JPC_UCTXNO); JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "RL"); runlen = v; JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "RL"); runlen = (runlen << 1) | v; f = *(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 { f = *(fp = fvscanstart); dp = dvscanstart; k = vscanlen; goto clnpass_full0; } /* Process first sample in vertical scan. */ jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, clnpass_full0:, clnpass_partial0:, vcausalflag); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process second sample in vertical scan. */ f = *fp; jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, ;, clnpass_partial1:, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process third sample in vertical scan. */ f = *fp; jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, ;, clnpass_partial2:, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process fourth sample in vertical scan. */ f = *fp; jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, ;, clnpass_partial3:, 0);
static int dec_rawrefpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data) { int i; int j; int k; int vscanlen; int width; int height; int one; int poshalf; int neghalf; jpc_fix_t *fp; int frowstep; int fstripestep; jpc_fix_t *fstripestart; jpc_fix_t *fvscanstart; jpc_fix_t *dp; int drowstep; int dstripestep; jpc_fix_t *dstripestart; jpc_fix_t *dvscanstart; /* Avoid compiler warning about unused parameters. */ dec = 0; vcausalflag = 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; poshalf = one >> 1; neghalf = (bitpos > 0) ? (-poshalf) : (-1); 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; /* Process first sample in vertical scan. */ jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, vcausalflag); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process second sample in vertical scan. */ jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process third sample in vertical scan. */ jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process fourth sample in vertical scan. */ jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0); } } return 0; }
int jas_cmxform_apply(jas_cmxform_t *xform, jas_cmpixmap_t *in, jas_cmpixmap_t *out) { jas_cmcmptfmt_t *fmt; jas_cmreal_t buf[2][APPLYBUFSIZ]; jas_cmpxformseq_t *pxformseq; int i; int j; int width; int height; int total; int n; jas_cmreal_t *inbuf; jas_cmreal_t *outbuf; jas_cmpxform_t *pxform; long *dataptr; int maxchans; int bufmax; int m; int bias; jas_cmreal_t scale; long v; jas_cmreal_t *bufptr; if (xform->numinchans > in->numcmpts || xform->numoutchans > out->numcmpts) goto error; fmt = &in->cmptfmts[0]; width = fmt->width; height = fmt->height; for (i = 1; i < xform->numinchans; ++i) { fmt = &in->cmptfmts[i]; if (fmt->width != width || fmt->height != height) { goto error; } } for (i = 0; i < xform->numoutchans; ++i) { fmt = &out->cmptfmts[i]; if (fmt->width != width || fmt->height != height) { goto error; } } maxchans = 0; pxformseq = xform->pxformseq; for (i = 0; i < pxformseq->numpxforms; ++i) { pxform = pxformseq->pxforms[i]; if (pxform->numinchans > maxchans) { maxchans = pxform->numinchans; } if (pxform->numoutchans > maxchans) { maxchans = pxform->numoutchans; } } bufmax = APPLYBUFSIZ / maxchans; assert(bufmax > 0); total = width * height; n = 0; while (n < total) { inbuf = &buf[0][0]; m = JAS_MIN(total - n, bufmax); for (i = 0; i < xform->numinchans; ++i) { fmt = &in->cmptfmts[i]; scale = (double)((1 << fmt->prec) - 1); bias = fmt->sgnd ? (1 << (fmt->prec - 1)) : 0; dataptr = &fmt->buf[n]; bufptr = &inbuf[i]; for (j = 0; j < m; ++j) { if (jas_cmgetint(&dataptr, fmt->sgnd, fmt->prec, &v)) goto error; *bufptr = (v - bias) / scale; bufptr += xform->numinchans; } } inbuf = &buf[0][0]; outbuf = inbuf; for (i = 0; i < pxformseq->numpxforms; ++i) { pxform = pxformseq->pxforms[i]; if (pxform->numoutchans > pxform->numinchans) { outbuf = (inbuf == &buf[0][0]) ? &buf[1][0] : &buf[0][0]; } else { outbuf = inbuf; } if ((*pxform->ops->apply)(pxform, inbuf, outbuf, m)) goto error; inbuf = outbuf; } for (i = 0; i < xform->numoutchans; ++i) { fmt = &out->cmptfmts[i]; scale = (double)((1 << fmt->prec) - 1); bias = fmt->sgnd ? (1 << (fmt->prec - 1)) : 0; bufptr = &outbuf[i]; dataptr = &fmt->buf[n]; for (j = 0; j < m; ++j) { v = (*bufptr) * scale + bias; bufptr += xform->numoutchans; if (jas_cmputint(&dataptr, fmt->sgnd, fmt->prec, v)) goto error; } } n += m; } return 0; error: return -1; }
static int dec_rawsigpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data) { int i; int j; int k; int one; int half; int oneplushalf; int vscanlen; int width; int height; jpc_fix_t *fp; int frowstep; int fstripestep; jpc_fix_t *fstripestart; jpc_fix_t *fvscanstart; jpc_fix_t *dp; int drowstep; int dstripestep; jpc_fix_t *dstripestart; jpc_fix_t *dvscanstart; 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; half = one >> 1; oneplushalf = one | half; 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; /* Process first sample in vertical scan. */ jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, vcausalflag); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process second sample in vertical scan. */ jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process third sample in vertical scan. */ jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process fourth sample in vertical scan. */ jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, 0); } } return 0; }