📄 jpc_dec.c
字号:
}
break;
case JPC_TPH:
if (!(tile = dec->curtile)) {
return -1;
}
if (!tile->partno) {
if (jpc_dec_cp_setfrompoc(tile->cp, poc, (!tile->partno))) {
return -1;
}
} else {
jpc_pi_addpchgfrompoc(tile->pi, poc);
}
break;
}
return 0;
}
static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms)
{
jpc_ppm_t *ppm = &ms->parms.ppm;
jpc_ppxstabent_t *ppmstabent;
if (!dec->ppmstab) {
if (!(dec->ppmstab = jpc_ppxstab_create())) {
return -1;
}
}
if (!(ppmstabent = jpc_ppxstabent_create())) {
return -1;
}
ppmstabent->ind = ppm->ind;
ppmstabent->data = ppm->data;
ppm->data = 0;
ppmstabent->len = ppm->len;
if (jpc_ppxstab_insert(dec->ppmstab, ppmstabent)) {
return -1;
}
return 0;
}
static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms)
{
jpc_ppt_t *ppt = &ms->parms.ppt;
jpc_dec_tile_t *tile;
jpc_ppxstabent_t *pptstabent;
tile = dec->curtile;
if (!tile->pptstab) {
if (!(tile->pptstab = jpc_ppxstab_create())) {
return -1;
}
}
if (!(pptstabent = jpc_ppxstabent_create())) {
return -1;
}
pptstabent->ind = ppt->ind;
pptstabent->data = ppt->data;
ppt->data = 0;
pptstabent->len = ppt->len;
if (jpc_ppxstab_insert(tile->pptstab, pptstabent)) {
return -1;
}
return 0;
}
static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms)
{
/* Eliminate compiler warnings about unused variables. */
dec = 0;
ms = 0;
return 0;
}
static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms)
{
/* Eliminate compiler warnings about unused variables. */
dec = 0;
jas_eprintf("warning: ignoring unknown marker segment\n");
jpc_ms_dump(ms, stderr);
return 0;
}
/******************************************************************************\
*
\******************************************************************************/
static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps)
{
jpc_dec_cp_t *cp;
jpc_dec_ccp_t *ccp;
int compno;
if (!(cp = jas_malloc(sizeof(jpc_dec_cp_t)))) {
return 0;
}
cp->flags = 0;
cp->numcomps = numcomps;
cp->prgord = 0;
cp->numlyrs = 0;
cp->mctid = 0;
cp->csty = 0;
if (!(cp->ccps = jas_malloc(cp->numcomps * sizeof(jpc_dec_ccp_t)))) {
return 0;
}
if (!(cp->pchglist = jpc_pchglist_create())) {
jas_free(cp->ccps);
return 0;
}
for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
++compno, ++ccp) {
ccp->flags = 0;
ccp->numrlvls = 0;
ccp->cblkwidthexpn = 0;
ccp->cblkheightexpn = 0;
ccp->qmfbid = 0;
ccp->numstepsizes = 0;
ccp->numguardbits = 0;
ccp->roishift = 0;
ccp->cblkctx = 0;
}
return cp;
}
static jpc_dec_cp_t *jpc_dec_cp_copy(jpc_dec_cp_t *cp)
{
jpc_dec_cp_t *newcp;
jpc_dec_ccp_t *newccp;
jpc_dec_ccp_t *ccp;
int compno;
if (!(newcp = jpc_dec_cp_create(cp->numcomps))) {
return 0;
}
newcp->flags = cp->flags;
newcp->prgord = cp->prgord;
newcp->numlyrs = cp->numlyrs;
newcp->mctid = cp->mctid;
newcp->csty = cp->csty;
jpc_pchglist_destroy(newcp->pchglist);
newcp->pchglist = 0;
if (!(newcp->pchglist = jpc_pchglist_copy(cp->pchglist))) {
jas_free(newcp);
return 0;
}
for (compno = 0, newccp = newcp->ccps, ccp = cp->ccps;
compno < cp->numcomps;
++compno, ++newccp, ++ccp) {
*newccp = *ccp;
}
return newcp;
}
static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp)
{
int compno;
jpc_dec_ccp_t *ccp;
cp->flags &= (JPC_CSET | JPC_QSET);
for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
++compno, ++ccp) {
ccp->flags = 0;
}
}
static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp)
{
if (cp->ccps) {
jas_free(cp->ccps);
}
if (cp->pchglist) {
jpc_pchglist_destroy(cp->pchglist);
}
jas_free(cp);
}
static int jpc_dec_cp_isvalid(jpc_dec_cp_t *cp)
{
uint_fast16_t compcnt;
jpc_dec_ccp_t *ccp;
if (!(cp->flags & JPC_CSET) || !(cp->flags & JPC_QSET)) {
return 0;
}
for (compcnt = cp->numcomps, ccp = cp->ccps; compcnt > 0; --compcnt,
++ccp) {
/* Is there enough step sizes for the number of bands? */
if ((ccp->qsty != JPC_QCX_SIQNT && JAS_CAST(int, ccp->numstepsizes) < 3 *
ccp->numrlvls - 2) || (ccp->qsty == JPC_QCX_SIQNT &&
ccp->numstepsizes != 1)) {
return 0;
}
}
return 1;
}
static void calcstepsizes(uint_fast16_t refstepsize, int numrlvls,
uint_fast16_t *stepsizes)
{
int bandno;
int numbands;
uint_fast16_t expn;
uint_fast16_t mant;
expn = JPC_QCX_GETEXPN(refstepsize);
mant = JPC_QCX_GETMANT(refstepsize);
numbands = 3 * numrlvls - 2;
for (bandno = 0; bandno < numbands; ++bandno) {
stepsizes[bandno] = JPC_QCX_MANT(mant) | JPC_QCX_EXPN(expn +
(numrlvls - 1) - (numrlvls - 1 - ((bandno > 0) ? ((bandno + 2) / 3) : (0))));
}
}
static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp)
{
jpc_dec_ccp_t *ccp;
int compno;
int i;
for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
++compno, ++ccp) {
if (!(ccp->csty & JPC_COX_PRT)) {
for (i = 0; i < JPC_MAXRLVLS; ++i) {
ccp->prcwidthexpns[i] = 15;
ccp->prcheightexpns[i] = 15;
}
}
if (ccp->qsty == JPC_QCX_SIQNT) {
calcstepsizes(ccp->stepsizes[0], ccp->numrlvls, ccp->stepsizes);
}
}
return 0;
}
static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, jpc_cod_t *cod)
{
jpc_dec_ccp_t *ccp;
int compno;
cp->flags |= JPC_CSET;
cp->prgord = cod->prg;
if (cod->mctrans) {
cp->mctid = (cod->compparms.qmfbid == JPC_COX_INS) ? (JPC_MCT_ICT) : (JPC_MCT_RCT);
} else {
cp->mctid = JPC_MCT_NONE;
}
cp->numlyrs = cod->numlyrs;
cp->csty = cod->csty & (JPC_COD_SOP | JPC_COD_EPH);
for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
++compno, ++ccp) {
jpc_dec_cp_setfromcox(cp, ccp, &cod->compparms, 0);
}
cp->flags |= JPC_CSET;
return 0;
}
static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, jpc_coc_t *coc)
{
jpc_dec_cp_setfromcox(cp, &cp->ccps[coc->compno], &coc->compparms, JPC_COC);
return 0;
}
static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp,
jpc_coxcp_t *compparms, int flags)
{
int rlvlno;
/* Eliminate compiler warnings about unused variables. */
cp = 0;
if ((flags & JPC_COC) || !(ccp->flags & JPC_COC)) {
ccp->numrlvls = compparms->numdlvls + 1;
ccp->cblkwidthexpn = JPC_COX_GETCBLKSIZEEXPN(
compparms->cblkwidthval);
ccp->cblkheightexpn = JPC_COX_GETCBLKSIZEEXPN(
compparms->cblkheightval);
ccp->qmfbid = compparms->qmfbid;
ccp->cblkctx = compparms->cblksty;
ccp->csty = compparms->csty & JPC_COX_PRT;
for (rlvlno = 0; rlvlno < compparms->numrlvls; ++rlvlno) {
ccp->prcwidthexpns[rlvlno] =
compparms->rlvls[rlvlno].parwidthval;
ccp->prcheightexpns[rlvlno] =
compparms->rlvls[rlvlno].parheightval;
}
ccp->flags |= flags | JPC_CSET;
}
return 0;
}
static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, jpc_qcd_t *qcd)
{
int compno;
jpc_dec_ccp_t *ccp;
for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
++compno, ++ccp) {
jpc_dec_cp_setfromqcx(cp, ccp, &qcd->compparms, 0);
}
cp->flags |= JPC_QSET;
return 0;
}
static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, jpc_qcc_t *qcc)
{
return jpc_dec_cp_setfromqcx(cp, &cp->ccps[qcc->compno], &qcc->compparms, JPC_QCC);
}
static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp,
jpc_qcxcp_t *compparms, int flags)
{
int bandno;
/* Eliminate compiler warnings about unused variables. */
cp = 0;
if ((flags & JPC_QCC) || !(ccp->flags & JPC_QCC)) {
ccp->flags |= flags | JPC_QSET;
for (bandno = 0; bandno < compparms->numstepsizes; ++bandno) {
ccp->stepsizes[bandno] = compparms->stepsizes[bandno];
}
ccp->numstepsizes = compparms->numstepsizes;
ccp->numguardbits = compparms->numguard;
ccp->qsty = compparms->qntsty;
}
return 0;
}
static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, jpc_rgn_t *rgn)
{
jpc_dec_ccp_t *ccp;
ccp = &cp->ccps[rgn->compno];
ccp->roishift = rgn->roishift;
return 0;
}
static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, jpc_poc_t *poc)
{
int pchgno;
jpc_pchg_t *pchg;
for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) {
if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) {
return -1;
}
if (jpc_pchglist_insert(pi->pchglist, -1, pchg)) {
return -1;
}
}
return 0;
}
static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, jpc_poc_t *poc, int reset)
{
int pchgno;
jpc_pchg_t *pchg;
if (reset) {
while (jpc_pchglist_numpchgs(cp->pchglist) > 0) {
pchg = jpc_pchglist_remove(cp->pchglist, 0);
jpc_pchg_destroy(pchg);
}
}
for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) {
if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) {
return -1;
}
if (jpc_pchglist_insert(cp->pchglist, -1, pchg)) {
return -1;
}
}
return 0;
}
static jpc_fix_t jpc_calcabsstepsize(int stepsize, int numbits)
{
jpc_fix_t absstepsize;
int n;
absstepsize = jpc_inttofix(1);
n = JPC_FIX_FRACBITS - 11;
absstepsize |= (n >= 0) ? (JPC_QCX_GETMANT(stepsize) << n) :
(JPC_QCX_GETMANT(stepsize) >> (-n));
n = numbits - JPC_QCX_GETEXPN(stepsize);
absstepsize = (n >= 0) ? (absstepsize << n) : (absstepsize >> (-n));
return absstepsize;
}
static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize)
{
int i;
int j;
int t;
assert(absstepsize >= 0);
if (absstepsize == jpc_inttofix(1)) {
return;
}
for (i = 0; i < jas_matrix_numrows(x); ++i) {
for (j = 0; j < jas_matrix_numcols(x); ++j) {
t = jas_matrix_get(x, i, j);
if (t) {
t = jpc_fix_mul(t, absstepsize);
} else {
t = 0;
}
jas_matrix_set(x, i, j, t);
}
}
}
static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, int numbps)
{
int i;
int j;
int thresh;
jpc_fix_t val;
jpc_fix_t mag;
bool warn;
uint_fast32_t mask;
if (roishift == 0 && bgshift == 0) {
return;
}
thresh = 1 << roishift;
warn = false;
for (i = 0; i < jas_matrix_numrows(x); ++i) {
for (j = 0; j < jas_matrix_numcols(x); ++j) {
val = jas_matrix_get(x, i, j);
mag = JAS_ABS(val);
if (mag >= thresh) {
/* We are dealing with ROI data. */
mag >>= roishift;
val = (val < 0) ? (-mag) : mag;
jas_matrix_set(x, i, j, val);
} else {
/* We are dealing with non-ROI (i.e., background) data. */
mag <<= bgshift;
mask = (1 << numbps) - 1;
/* Perform a basic sanity check on the sample value. */
/* Some implementations write garbage in the unused
most-significant bit planes introduced by ROI shifting.
Here we ensure that any such bits are masked off. */
if (mag & (~mask)) {
if (!warn) {
jas_eprintf("warning: possibly corrupt code stream\n");
warn = true;
}
mag &= mask;
}
val = (val < 0) ? (-mag) : mag;
jas_matrix_set(x, i, j, val);
}
}
}
}
static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts, jas_stream_t *in)
{
jpc_dec_t *dec;
if (!(dec = jas_malloc(sizeof(jpc_dec_t)))) {
return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -