/// <summary> /// Get next packet in resolution-precinct-component-layer order. /// </summary> int pi_next_rpcl(pi_iterator_t *pi) { pi_comp_t *comp; pi_resolution_t *res; if (!pi->first) { goto skip; } else { int compno, resno; pi->first=0; pi->dx=0; pi->dy=0; for (compno=0; compno<pi->numcomps; compno++) { comp=&pi->comps[compno]; for (resno=0; resno<comp->numresolutions; resno++) { int dx, dy; res=&comp->resolutions[resno]; dx=comp->dx*(1<<(res->pdx+comp->numresolutions-1-resno)); dy=comp->dy*(1<<(res->pdy+comp->numresolutions-1-resno)); pi->dx=!pi->dx?dx:int_min(pi->dx, dx); pi->dy=!pi->dy?dy:int_min(pi->dy, dy); } } } for (pi->resno=pi->poc.resno0; pi->resno<pi->poc.resno1; pi->resno++) { for (pi->y=pi->ty0; pi->y<pi->ty1; pi->y+=pi->dy-(pi->y%pi->dy)) { for (pi->x=pi->tx0; pi->x<pi->tx1; pi->x+=pi->dx-(pi->x%pi->dx)) { for (pi->compno=pi->poc.compno0; pi->compno<pi->poc.compno1; pi->compno++) { int levelno; int trx0, try0; int rpx, rpy; int prci, prcj; comp=&pi->comps[pi->compno]; if (pi->resno>=comp->numresolutions) { continue; } res=&comp->resolutions[pi->resno]; levelno=comp->numresolutions-1-pi->resno; trx0=int_ceildiv(pi->tx0, comp->dx<<levelno); try0=int_ceildiv(pi->ty0, comp->dy<<levelno); rpx=res->pdx+levelno; rpy=res->pdy+levelno; if (!(pi->x%(comp->dx<<rpx)==0||(pi->x==pi->tx0&&(trx0<<levelno)%(1<<rpx)))) { continue; } if (!(pi->y%(comp->dy<<rpy)==0||(pi->y==pi->ty0&&(try0<<levelno)%(1<<rpx)))) { continue; } prci=int_floordivpow2(int_ceildiv(pi->x, comp->dx<<levelno), res->pdx)-int_floordivpow2(trx0, res->pdx); prcj=int_floordivpow2(int_ceildiv(pi->y, comp->dy<<levelno), res->pdy)-int_floordivpow2(try0, res->pdy); pi->precno=prci+prcj*res->pw; for (pi->layno=0; pi->layno<pi->poc.layno1; pi->layno++) { return 1; skip: ; } } } } } return 0; }
opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){ int p, q, pino; int compno, resno; int maxres = 0; int maxprec = 0; opj_pi_iterator_t *pi = NULL; opj_tcp_t *tcp = NULL; opj_tccp_t *tccp = NULL; size_t array_size; tcp = &cp->tcps[tileno]; array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t); pi = (opj_pi_iterator_t *) opj_malloc(array_size); if(!pi) { return NULL;} pi->tp_on = cp->tp_on; for(pino = 0;pino < tcp->numpocs+1 ; pino ++){ p = tileno % cp->tw; q = tileno / cp->tw; pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0); pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0); pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); pi[pino].numcomps = image->numcomps; array_size = image->numcomps * sizeof(opj_pi_comp_t); pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size); if(!pi[pino].comps) { pi_destroy(pi, cp, tileno); return NULL; } memset(pi[pino].comps, 0, array_size); for (compno = 0; compno < pi[pino].numcomps; compno++) { int tcx0, tcy0, tcx1, tcy1; opj_pi_comp_t *comp = &pi[pino].comps[compno]; tccp = &tcp->tccps[compno]; comp->dx = image->comps[compno].dx; comp->dy = image->comps[compno].dy; comp->numresolutions = tccp->numresolutions; array_size = comp->numresolutions * sizeof(opj_pi_resolution_t); comp->resolutions = (opj_pi_resolution_t *) opj_malloc(array_size); if(!comp->resolutions) { pi_destroy(pi, cp, tileno); return NULL; } tcx0 = int_ceildiv(pi[pino].tx0, comp->dx); tcy0 = int_ceildiv(pi[pino].ty0, comp->dy); tcx1 = int_ceildiv(pi[pino].tx1, comp->dx); tcy1 = int_ceildiv(pi[pino].ty1, comp->dy); if (comp->numresolutions > maxres) { maxres = comp->numresolutions; } for (resno = 0; resno < comp->numresolutions; resno++) { int levelno; int rx0, ry0, rx1, ry1; int px0, py0, px1, py1; opj_pi_resolution_t *res = &comp->resolutions[resno]; if (tccp->csty & J2K_CCP_CSTY_PRT) { res->pdx = tccp->prcw[resno]; res->pdy = tccp->prch[resno]; } else { res->pdx = 15; res->pdy = 15; } levelno = comp->numresolutions - 1 - resno; rx0 = int_ceildivpow2(tcx0, levelno); ry0 = int_ceildivpow2(tcy0, levelno); rx1 = int_ceildivpow2(tcx1, levelno); ry1 = int_ceildivpow2(tcy1, levelno); px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); if (res->pw*res->ph > maxprec) { maxprec = res->pw * res->ph; } } } tccp = &tcp->tccps[0]; pi[pino].step_p = 1; pi[pino].step_c = maxprec * pi[pino].step_p; pi[pino].step_r = image->numcomps * pi[pino].step_c; pi[pino].step_l = maxres * pi[pino].step_r; for (compno = 0; compno < pi->numcomps; compno++) { opj_pi_comp_t *comp = &pi->comps[compno]; for (resno = 0; resno < comp->numresolutions; resno++) { int dx, dy; opj_pi_resolution_t *res = &comp->resolutions[resno]; dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx); pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy); } } if (pino == 0) { array_size = tcp->numlayers * pi[pino].step_l * sizeof(short int); pi[pino].include = (short int *) opj_malloc(array_size); if(!pi[pino].include) { pi_destroy(pi, cp, tileno); return NULL; } } else { pi[pino].include = pi[pino - 1].include; } /* Generation of boundaries for each prog flag*/ if(tcp->POC & (t2_mode == FINAL_PASS)){ tcp->pocs[pino].compS= tcp->pocs[pino].compno0; tcp->pocs[pino].compE= tcp->pocs[pino].compno1; tcp->pocs[pino].resS = tcp->pocs[pino].resno0; tcp->pocs[pino].resE = tcp->pocs[pino].resno1; tcp->pocs[pino].layE = tcp->pocs[pino].layno1; tcp->pocs[pino].prg = tcp->pocs[pino].prg1; if (pino > 0) tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0; }else { tcp->pocs[pino].compS= 0; tcp->pocs[pino].compE= image->numcomps; tcp->pocs[pino].resS = 0; tcp->pocs[pino].resE = maxres; tcp->pocs[pino].layS = 0; tcp->pocs[pino].layE = tcp->numlayers; tcp->pocs[pino].prg = tcp->prg; } tcp->pocs[pino].prcS = 0; tcp->pocs[pino].prcE = maxprec;; tcp->pocs[pino].txS = pi[pino].tx0; tcp->pocs[pino].txE = pi[pino].tx1; tcp->pocs[pino].tyS = pi[pino].ty0; tcp->pocs[pino].tyE = pi[pino].ty1; tcp->pocs[pino].dx = pi[pino].dx; tcp->pocs[pino].dy = pi[pino].dy; } return pi; }
opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) { int p, q; int compno, resno, pino; opj_pi_iterator_t *pi = NULL; opj_tcp_t *tcp = NULL; opj_tccp_t *tccp = NULL; size_t array_size; tcp = &cp->tcps[tileno]; array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t); pi = (opj_pi_iterator_t *) opj_malloc(array_size); if(!pi) { /* TODO: throw an error */ return NULL; } for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */ int maxres = 0; int maxprec = 0; p = tileno % cp->tw; q = tileno / cp->tw; pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0); pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0); pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); pi[pino].numcomps = image->numcomps; array_size = image->numcomps * sizeof(opj_pi_comp_t); pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size); if(!pi[pino].comps) { /* TODO: throw an error */ pi_destroy(pi, cp, tileno); return NULL; } memset(pi[pino].comps, 0, array_size); for (compno = 0; compno < pi->numcomps; compno++) { int tcx0, tcy0, tcx1, tcy1; opj_pi_comp_t *comp = &pi[pino].comps[compno]; tccp = &tcp->tccps[compno]; comp->dx = image->comps[compno].dx; comp->dy = image->comps[compno].dy; comp->numresolutions = tccp->numresolutions; array_size = comp->numresolutions * sizeof(opj_pi_resolution_t); comp->resolutions = (opj_pi_resolution_t *) opj_malloc(array_size); if(!comp->resolutions) { /* TODO: throw an error */ pi_destroy(pi, cp, tileno); return NULL; } tcx0 = int_ceildiv(pi->tx0, comp->dx); tcy0 = int_ceildiv(pi->ty0, comp->dy); tcx1 = int_ceildiv(pi->tx1, comp->dx); tcy1 = int_ceildiv(pi->ty1, comp->dy); if (comp->numresolutions > maxres) { maxres = comp->numresolutions; } for (resno = 0; resno < comp->numresolutions; resno++) { int levelno; int rx0, ry0, rx1, ry1; int px0, py0, px1, py1; opj_pi_resolution_t *res = &comp->resolutions[resno]; if (tccp->csty & J2K_CCP_CSTY_PRT) { res->pdx = tccp->prcw[resno]; res->pdy = tccp->prch[resno]; } else { res->pdx = 15; res->pdy = 15; } levelno = comp->numresolutions - 1 - resno; rx0 = int_ceildivpow2(tcx0, levelno); ry0 = int_ceildivpow2(tcy0, levelno); rx1 = int_ceildivpow2(tcx1, levelno); ry1 = int_ceildivpow2(tcy1, levelno); px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); if (res->pw*res->ph > maxprec) { maxprec = res->pw*res->ph; } } } tccp = &tcp->tccps[0]; pi[pino].step_p = 1; pi[pino].step_c = maxprec * pi[pino].step_p; pi[pino].step_r = image->numcomps * pi[pino].step_c; pi[pino].step_l = maxres * pi[pino].step_r; if (pino == 0) { array_size = image->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int); pi[pino].include = (short int *) opj_malloc(array_size); if(!pi[pino].include) { /* TODO: throw an error */ pi_destroy(pi, cp, tileno); return NULL; } } else { pi[pino].include = pi[pino - 1].include; } if (tcp->POC == 0) { pi[pino].first = 1; pi[pino].poc.resno0 = 0; pi[pino].poc.compno0 = 0; pi[pino].poc.layno1 = tcp->numlayers; pi[pino].poc.resno1 = maxres; pi[pino].poc.compno1 = image->numcomps; pi[pino].poc.prg = tcp->prg; } else { pi[pino].first = 1; pi[pino].poc.resno0 = tcp->pocs[pino].resno0; pi[pino].poc.compno0 = tcp->pocs[pino].compno0; pi[pino].poc.layno1 = tcp->pocs[pino].layno1; pi[pino].poc.resno1 = tcp->pocs[pino].resno1; pi[pino].poc.compno1 = tcp->pocs[pino].compno1; pi[pino].poc.prg = tcp->pocs[pino].prg; } pi[pino].poc.layno0 = 0; pi[pino].poc.precno0 = 0; pi[pino].poc.precno1 = maxprec; } return pi; }
static bool pi_next_cprl(opj_pi_iterator_t * pi) { opj_pi_comp_t *comp = NULL; opj_pi_resolution_t *res = NULL; long index = 0; if (!pi->first) { comp = &pi->comps[pi->compno]; goto LABEL_SKIP; } else { pi->first = 0; } for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { int resno; comp = &pi->comps[pi->compno]; pi->dx = 0; pi->dy = 0; for (resno = 0; resno < comp->numresolutions; resno++) { int dx, dy; res = &comp->resolutions[resno]; dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); } if (!pi->tp_on){ pi->poc.ty0 = pi->ty0; pi->poc.tx0 = pi->tx0; pi->poc.ty1 = pi->ty1; pi->poc.tx1 = pi->tx1; } for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) { for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) { for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { int levelno; int trx0, try0; int trx1, try1; int rpx, rpy; int prci, prcj; res = &comp->resolutions[pi->resno]; levelno = comp->numresolutions - 1 - pi->resno; trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); try0 = int_ceildiv(pi->ty0, comp->dy << levelno); trx1 = int_ceildiv(pi->tx1, comp->dx << levelno); try1 = int_ceildiv(pi->ty1, comp->dy << levelno); rpx = res->pdx + levelno; rpy = res->pdy + levelno; if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){ continue; } if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){ continue; } if ((res->pw==0)||(res->pw==0)) continue; if ((trx0==trx1)||(try0==try1)) continue; prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) - int_floordivpow2(trx0, res->pdx); prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) - int_floordivpow2(try0, res->pdy); pi->precno = prci + prcj * res->pw; for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; if (!pi->include[index]) { pi->include[index] = 1; return true; } LABEL_SKIP:; } } } } } return false; }
/// <summary> /// Create a packet iterator. /// </summary> pi_iterator_t *pi_create(j2k_image_t *img, j2k_cp_t *cp, int tileno) { int p, q; int compno, resno; int maxres=0; pi_iterator_t *pi; j2k_tcp_t *tcp; j2k_tccp_t *tccp; tcp=&cp->tcps[tileno]; pi=(pi_iterator_t*)malloc(sizeof(pi_iterator_t)); p=tileno%cp->tw; q=tileno/cp->tw; pi->tx0=int_max(cp->tx0+p*cp->tdx, img->x0); pi->ty0=int_max(cp->ty0+q*cp->tdy, img->y0); pi->tx1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1); pi->ty1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1); pi->numcomps=img->numcomps; pi->comps=(pi_comp_t*)malloc(img->numcomps*sizeof(pi_comp_t)); for (compno=0; compno<pi->numcomps; compno++) { int tcx0, tcy0, tcx1, tcy1; pi_comp_t *comp=&pi->comps[compno]; tccp=&tcp->tccps[compno]; comp->dx=img->comps[compno].dx; comp->dy=img->comps[compno].dy; comp->numresolutions=tccp->numresolutions; comp->resolutions=(pi_resolution_t*)malloc(comp->numresolutions*sizeof(pi_resolution_t)); tcx0=int_ceildiv(pi->tx0, comp->dx); tcy0=int_ceildiv(pi->ty0, comp->dy); tcx1=int_ceildiv(pi->tx1, comp->dx); tcy1=int_ceildiv(pi->ty1, comp->dy); if (comp->numresolutions>maxres) { maxres=comp->numresolutions; } for (resno=0; resno<comp->numresolutions; resno++) { int levelno; int rx0, ry0, rx1, ry1; int px0, py0, px1, py1; pi_resolution_t *res=&comp->resolutions[resno]; if (tccp->csty&J2K_CCP_CSTY_PRT) { res->pdx=tccp->prcw[resno]; res->pdy=tccp->prch[resno]; } else { res->pdx=15; res->pdy=15; } levelno=comp->numresolutions-1-resno; rx0=int_ceildivpow2(tcx0, levelno); ry0=int_ceildivpow2(tcy0, levelno); rx1=int_ceildivpow2(tcx1, levelno); ry1=int_ceildivpow2(tcy1, levelno); px0=int_floordivpow2(rx0, res->pdx)<<res->pdx; py0=int_floordivpow2(ry0, res->pdy)<<res->pdy; px1=int_ceildivpow2(rx1, res->pdx)<<res->pdx; py1=int_ceildivpow2(ry1, res->pdy)<<res->pdy; res->pw=(px1-px0)>>res->pdx; res->ph=(py1-py0)>>res->pdy; } } pi->first=1; pi->poc.resno0=0; pi->poc.compno0=0; pi->poc.layno1=tcp->numlayers; pi->poc.resno1=maxres; pi->poc.compno1=img->numcomps; pi->poc.prg=tcp->prg; return pi; }
static bool pi_next_cprl(opj_pi_iterator_t * pi) { opj_pi_comp_t *comp = NULL; opj_pi_resolution_t *res = NULL; long index = 0; if (!pi->first) { comp = &pi->comps[pi->compno]; goto LABEL_SKIP; } else { pi->first = 0; } for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { int resno; comp = &pi->comps[pi->compno]; pi->dx = 0; pi->dy = 0; pi->dz = 0; for (resno = 0; resno < comp->numresolution[0]; resno++) { int dx, dy, dz; res = &comp->resolutions[resno]; dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno)); dy = comp->dy * (1 << (res->pdy + comp->numresolution[1] - 1 - resno)); dz = comp->dz * (1 << (res->pdz + comp->numresolution[2] - 1 - resno)); pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); pi->dz = !pi->dz ? dz : int_min(pi->dz, dz); } for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) { for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) { for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) { for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolution[0]); pi->resno++) { int levelnox, levelnoy, levelnoz; int trx0, try0, trz0; int trx1, try1, trz1; int rpx, rpy, rpz; int prci, prcj, prck; comp = &pi->comps[pi->compno]; if (pi->resno >= comp->numresolution[0]) { continue; } res = &comp->resolutions[pi->resno]; levelnox = comp->numresolution[0] - 1 - pi->resno; levelnoy = comp->numresolution[1] - 1 - pi->resno; levelnoz = comp->numresolution[2] - 1 - pi->resno; trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox); try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy); trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz); trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox); try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy); trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz); rpx = res->pdx + levelnox; rpy = res->pdy + levelnoy; rpz = res->pdz + levelnoz; if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) { continue; } if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) { continue; } if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) { continue; } if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue; if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue; prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx) - int_floordivpow2(trx0, res->pdx); prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy) - int_floordivpow2(try0, res->pdy); prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz) - int_floordivpow2(trz0, res->pdz); pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1]; for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; if (!pi->include[index]) { pi->include[index] = 1; return true; } LABEL_SKIP:; } } } } } } return false; }
opj_pi_iterator_t *pi_create(opj_volume_t *volume, opj_cp_t *cp, int tileno) { int p, q, r; int compno, resno, pino; opj_pi_iterator_t *pi = NULL; opj_tcp_t *tcp = NULL; opj_tccp_t *tccp = NULL; size_t array_size; tcp = &cp->tcps[tileno]; array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t); pi = (opj_pi_iterator_t *) opj_malloc(array_size); if(!pi) { fprintf(stdout,"[ERROR] Malloc of opj_pi_iterator failed \n"); return NULL; } for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */ int maxres = 0; int maxprec = 0; p = tileno % cp->tw; q = tileno / cp->tw; r = tileno / (cp->tw * cp->th); pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, volume->x0); pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, volume->y0); pi[pino].tz0 = int_max(cp->tz0 + r * cp->tdz, volume->z0); pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1); pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1); pi[pino].tz1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1); pi[pino].numcomps = volume->numcomps; array_size = volume->numcomps * sizeof(opj_pi_comp_t); pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size); if(!pi[pino].comps) { fprintf(stdout,"[ERROR] Malloc of opj_pi_comp failed \n"); pi_destroy(pi, cp, tileno); return NULL; } memset(pi[pino].comps, 0, array_size); for (compno = 0; compno < pi->numcomps; compno++) { int tcx0, tcx1, tcy0, tcy1, tcz0, tcz1; int i; opj_pi_comp_t *comp = &pi[pino].comps[compno]; tccp = &tcp->tccps[compno]; comp->dx = volume->comps[compno].dx; comp->dy = volume->comps[compno].dy; comp->dz = volume->comps[compno].dz; for (i = 0; i < 3; i++) { comp->numresolution[i] = tccp->numresolution[i]; if (comp->numresolution[i] > maxres) { maxres = comp->numresolution[i]; } } array_size = comp->numresolution[0] * sizeof(opj_pi_resolution_t); comp->resolutions = (opj_pi_resolution_t *) opj_malloc(array_size); if(!comp->resolutions) { fprintf(stdout,"[ERROR] Malloc of opj_pi_resolution failed \n"); pi_destroy(pi, cp, tileno); return NULL; } tcx0 = int_ceildiv(pi->tx0, comp->dx); tcy0 = int_ceildiv(pi->ty0, comp->dy); tcz0 = int_ceildiv(pi->tz0, comp->dz); tcx1 = int_ceildiv(pi->tx1, comp->dx); tcy1 = int_ceildiv(pi->ty1, comp->dy); tcz1 = int_ceildiv(pi->tz1, comp->dz); for (resno = 0; resno < comp->numresolution[0]; resno++) { int levelnox, levelnoy, levelnoz, diff; int rx0, ry0, rz0, rx1, ry1, rz1; int px0, py0, pz0, px1, py1, pz1; opj_pi_resolution_t *res = &comp->resolutions[resno]; if (tccp->csty & J3D_CCP_CSTY_PRT) { res->pdx = tccp->prctsiz[0][resno]; res->pdy = tccp->prctsiz[1][resno]; res->pdz = tccp->prctsiz[2][resno]; } else { res->pdx = 15; res->pdy = 15; res->pdz = 15; } levelnox = comp->numresolution[0] - 1 - resno; levelnoy = comp->numresolution[1] - 1 - resno; levelnoz = comp->numresolution[2] - 1 - resno; if (levelnoz < 0) levelnoz = 0; diff = comp->numresolution[0] - comp->numresolution[2]; rx0 = int_ceildivpow2(tcx0, levelnox); ry0 = int_ceildivpow2(tcy0, levelnoy); rz0 = int_ceildivpow2(tcz0, levelnoz); rx1 = int_ceildivpow2(tcx1, levelnox); ry1 = int_ceildivpow2(tcy1, levelnoy); rz1 = int_ceildivpow2(tcz1, levelnoz); px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; pz0 = int_floordivpow2(rz0, res->pdz) << res->pdz; px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; pz1 = int_ceildivpow2(rz1, res->pdz) << res->pdz; res->prctno[0] = (rx0==rx1)? 0 : ((px1 - px0) >> res->pdx); res->prctno[1] = (ry0==ry1)? 0 : ((py1 - py0) >> res->pdy); res->prctno[2] = (rz0==rz1)? 0 : ((pz1 - pz0) >> res->pdz); if (res->prctno[0]*res->prctno[1]*res->prctno[2] > maxprec) { maxprec = res->prctno[0]*res->prctno[1]*res->prctno[2]; } } } tccp = &tcp->tccps[0]; pi[pino].step_p = 1; pi[pino].step_c = maxprec * pi[pino].step_p; pi[pino].step_r = volume->numcomps * pi[pino].step_c; pi[pino].step_l = maxres * pi[pino].step_r; if (pino == 0) { array_size = volume->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int); pi[pino].include = (short int *) opj_malloc(array_size); if(!pi[pino].include) { fprintf(stdout,"[ERROR] Malloc of pi[pino].include failed \n"); pi_destroy(pi, cp, tileno); return NULL; } } else { pi[pino].include = pi[pino - 1].include; } if (tcp->POC == 0) { pi[pino].first = 1; pi[pino].poc.resno0 = 0; pi[pino].poc.compno0 = 0; pi[pino].poc.layno1 = tcp->numlayers; pi[pino].poc.resno1 = maxres; pi[pino].poc.compno1 = volume->numcomps; pi[pino].poc.prg = tcp->prg; } else { pi[pino].first = 1; pi[pino].poc.resno0 = tcp->pocs[pino].resno0; pi[pino].poc.compno0 = tcp->pocs[pino].compno0; pi[pino].poc.layno1 = tcp->pocs[pino].layno1; pi[pino].poc.resno1 = tcp->pocs[pino].resno1; pi[pino].poc.compno1 = tcp->pocs[pino].compno1; pi[pino].poc.prg = tcp->pocs[pino].prg; } } return pi; }
void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) { int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q; unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h; tcd->image = image; tcd->cp = cp; tcd->tcd_image->tw = cp->tw; tcd->tcd_image->th = cp->th; tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t)); for (i = 0; i < cp->tileno_size; i++) { opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]); opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[cp->tileno[i]]); /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ tileno = cp->tileno[i]; p = tileno % cp->tw; /* si numerotation matricielle .. */ q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ /* 4 borders of the tile rescale on the image if necessary */ tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0); tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0); tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); tile->numcomps = image->numcomps; tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t)); for (compno = 0; compno < tile->numcomps; compno++) { opj_tccp_t *tccp = &tcp->tccps[compno]; opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; /* border of each tile component (global) */ tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx); tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy); tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx); tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy); tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int)); tilec->numresolutions = tccp->numresolutions; tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t)); for (resno = 0; resno < tilec->numresolutions; resno++) { int pdx, pdy; int levelno = tilec->numresolutions - 1 - resno; int tlprcxstart, tlprcystart, brprcxend, brprcyend; int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; int cbgwidthexpn, cbgheightexpn; int cblkwidthexpn, cblkheightexpn; opj_tcd_resolution_t *res = &tilec->resolutions[resno]; /* border for each resolution level (global) */ res->x0 = int_ceildivpow2(tilec->x0, levelno); res->y0 = int_ceildivpow2(tilec->y0, levelno); res->x1 = int_ceildivpow2(tilec->x1, levelno); res->y1 = int_ceildivpow2(tilec->y1, levelno); res->numbands = resno == 0 ? 1 : 3; /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ if (tccp->csty & J2K_CCP_CSTY_PRT) { pdx = tccp->prcw[resno]; pdy = tccp->prch[resno]; } else { pdx = 15; pdy = 15; } /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx); res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); if (resno == 0) { tlcbgxstart = tlprcxstart; tlcbgystart = tlprcystart; brcbgxend = brprcxend; brcbgyend = brprcyend; cbgwidthexpn = pdx; cbgheightexpn = pdy; } else { tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); tlcbgystart = int_ceildivpow2(tlprcystart, 1); brcbgxend = int_ceildivpow2(brprcxend, 1); brcbgyend = int_ceildivpow2(brprcyend, 1); cbgwidthexpn = pdx - 1; cbgheightexpn = pdy - 1; } cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); for (bandno = 0; bandno < res->numbands; bandno++) { int x0b, y0b; int gain, numbps; opj_stepsize_t *ss = NULL; opj_tcd_band_t *band = &res->bands[bandno]; band->bandno = resno == 0 ? 0 : bandno + 1; x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; if (band->bandno == 0) { /* band border (global) */ band->x0 = int_ceildivpow2(tilec->x0, levelno); band->y0 = int_ceildivpow2(tilec->y0, levelno); band->x1 = int_ceildivpow2(tilec->x1, levelno); band->y1 = int_ceildivpow2(tilec->y1, levelno); } else { /* band border (global) */ band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); } ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); numbps = image->comps[compno].prec + gain; band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)); band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t)); for (precno = 0; precno < res->pw * res->ph; precno++) { int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); int cbgxend = cbgxstart + (1 << cbgwidthexpn); int cbgyend = cbgystart + (1 << cbgheightexpn); opj_tcd_precinct_t *prc = &band->precincts[precno]; /* precinct size (global) */ prc->x0 = int_max(cbgxstart, band->x0); prc->y0 = int_max(cbgystart, band->y0); prc->x1 = int_min(cbgxend, band->x1); prc->y1 = int_min(cbgyend, band->y1); tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t)); prc->incltree = tgt_create(prc->cw, prc->ch); prc->imsbtree = tgt_create(prc->cw, prc->ch); for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); int cblkxend = cblkxstart + (1 << cblkwidthexpn); int cblkyend = cblkystart + (1 << cblkheightexpn); /* code-block size (global) */ opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; cblk->x0 = int_max(cblkxstart, prc->x0); cblk->y0 = int_max(cblkystart, prc->y0); cblk->x1 = int_min(cblkxend, prc->x1); cblk->y1 = int_min(cblkyend, prc->y1); } } /* precno */ } /* bandno */ } /* resno */ } /* compno */ } /* i = 0..cp->tileno_size */ /* tcd_dump(stdout, tcd, &tcd->tcd_image); */ /* Allocate place to store the decoded data = final image Place limited by the tile really present in the codestream */ for (i = 0; i < image->numcomps; i++) { for (j = 0; j < cp->tileno_size; j++) { tileno = cp->tileno[j]; x0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x0 : int_min(x0, (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x0); y0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y0 : int_min(y0, (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y0); x1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x1 : int_max(x1, (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x1); y1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y1 : int_max(y1, (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y1); } w = x1 - x0; h = y1 - y0; image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int)); image->comps[i].w = w; image->comps[i].h = h; image->comps[i].x0 = x0; image->comps[i].y0 = y0; } }
void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) { int tileno, compno, resno, bandno, precno, cblkno; for (tileno = 0; tileno < 1; tileno++) { opj_tcp_t *tcp = &cp->tcps[curtileno]; int j; /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ int p = curtileno % cp->tw; int q = curtileno / cp->tw; opj_tcd_tile_t *tile = tcd->tcd_image->tiles; /* 4 borders of the tile rescale on the image if necessary */ tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0); tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0); tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); tile->numcomps = image->numcomps; /* tile->PPT=image->PPT; */ /* Modification of the RATE >> */ for (j = 0; j < tcp->numlayers; j++) { tcp->rates[j] = tcp->rates[j] ? int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * image->comps[0].prec, (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) : 0; if (tcp->rates[j]) { if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { tcp->rates[j] = tcp->rates[j - 1] + 20; } else { if (!j && tcp->rates[j] < 30) tcp->rates[j] = 30; } } } /* << Modification of the RATE */ /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */ for (compno = 0; compno < tile->numcomps; compno++) { opj_tccp_t *tccp = &tcp->tccps[compno]; opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; /* border of each tile component (global) */ tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx); tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy); tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx); tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy); tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int)); tilec->numresolutions = tccp->numresolutions; /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */ for (resno = 0; resno < tilec->numresolutions; resno++) { int pdx, pdy; int levelno = tilec->numresolutions - 1 - resno; int tlprcxstart, tlprcystart, brprcxend, brprcyend; int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; int cbgwidthexpn, cbgheightexpn; int cblkwidthexpn, cblkheightexpn; opj_tcd_resolution_t *res = &tilec->resolutions[resno]; /* border for each resolution level (global) */ res->x0 = int_ceildivpow2(tilec->x0, levelno); res->y0 = int_ceildivpow2(tilec->y0, levelno); res->x1 = int_ceildivpow2(tilec->x1, levelno); res->y1 = int_ceildivpow2(tilec->y1, levelno); res->numbands = resno == 0 ? 1 : 3; /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ if (tccp->csty & J2K_CCP_CSTY_PRT) { pdx = tccp->prcw[resno]; pdy = tccp->prch[resno]; } else { pdx = 15; pdy = 15; } /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; res->pw = (brprcxend - tlprcxstart) >> pdx; res->ph = (brprcyend - tlprcystart) >> pdy; if (resno == 0) { tlcbgxstart = tlprcxstart; tlcbgystart = tlprcystart; brcbgxend = brprcxend; brcbgyend = brprcyend; cbgwidthexpn = pdx; cbgheightexpn = pdy; } else { tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); tlcbgystart = int_ceildivpow2(tlprcystart, 1); brcbgxend = int_ceildivpow2(brprcxend, 1); brcbgyend = int_ceildivpow2(brprcyend, 1); cbgwidthexpn = pdx - 1; cbgheightexpn = pdy - 1; } cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); for (bandno = 0; bandno < res->numbands; bandno++) { int x0b, y0b; int gain, numbps; opj_stepsize_t *ss = NULL; opj_tcd_band_t *band = &res->bands[bandno]; band->bandno = resno == 0 ? 0 : bandno + 1; x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; if (band->bandno == 0) { /* band border */ band->x0 = int_ceildivpow2(tilec->x0, levelno); band->y0 = int_ceildivpow2(tilec->y0, levelno); band->x1 = int_ceildivpow2(tilec->x1, levelno); band->y1 = int_ceildivpow2(tilec->y1, levelno); } else { band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); } ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); numbps = image->comps[compno].prec + gain; band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)); band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ for (precno = 0; precno < res->pw * res->ph; precno++) { int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); int cbgxend = cbgxstart + (1 << cbgwidthexpn); int cbgyend = cbgystart + (1 << cbgheightexpn); opj_tcd_precinct_t *prc = &band->precincts[precno]; /* precinct size (global) */ prc->x0 = int_max(cbgxstart, band->x0); prc->y0 = int_max(cbgystart, band->y0); prc->x1 = int_min(cbgxend, band->x1); prc->y1 = int_min(cbgyend, band->y1); tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; opj_free(prc->cblks); prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t)); if (prc->incltree != NULL) { tgt_destroy(prc->incltree); } if (prc->imsbtree != NULL) { tgt_destroy(prc->imsbtree); } prc->incltree = tgt_create(prc->cw, prc->ch); prc->imsbtree = tgt_create(prc->cw, prc->ch); for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); int cblkxend = cblkxstart + (1 << cblkwidthexpn); int cblkyend = cblkystart + (1 << cblkheightexpn); opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; /* code-block size (global) */ cblk->x0 = int_max(cblkxstart, prc->x0); cblk->y0 = int_max(cblkystart, prc->y0); cblk->x1 = int_min(cblkxend, prc->x1); cblk->y1 = int_min(cblkyend, prc->y1); } } /* precno */ } /* bandno */ } /* resno */ } /* compno */ } /* tileno */ /* tcd_dump(stdout, tcd, &tcd->tcd_image); */ }
void tcd_init(j2k_image_t *img, j2k_cp_t *cp, info_image_t *imgg) { int tileno, compno, resno, bandno, precno, cblkno; tcd_img=img; tcd_cp=cp; tcd_image.tw=cp->tw; tcd_image.th=cp->th; tcd_image.tiles=(tcd_tile_t*)malloc(cp->tw*cp->th*sizeof(tcd_tile_t)); for (tileno=0; tileno<cp->tw*cp->th; tileno++) { j2k_tcp_t *tcp=&cp->tcps[tileno]; tcd_tile_t *tile=&tcd_image.tiles[tileno]; // cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) int p=tileno%cp->tw; // si numerotation matricielle .. int q=tileno/cp->tw; // .. coordonnees de la tile (q,p) q pour ligne et p pour colonne info_tile_t *tile_Idx=&imgg->tile[tileno]; // INDEX // 4 borders of the tile rescale on the image if necessary tile->x0=int_max(cp->tx0+p*cp->tdx, img->x0); tile->y0=int_max(cp->ty0+q*cp->tdy, img->y0); tile->x1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1); tile->y1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1); tile->numcomps=img->numcomps; tile->comps=(tcd_tilecomp_t*)malloc(img->numcomps*sizeof(tcd_tilecomp_t)); tile_Idx->compo=(info_compo_t*)malloc(img->numcomps*sizeof(info_compo_t)); // INDEX for (compno=0; compno<tile->numcomps; compno++) { j2k_tccp_t *tccp=&tcp->tccps[compno]; tcd_tilecomp_t *tilec=&tile->comps[compno]; info_compo_t *compo_Idx=&tile_Idx->compo[compno]; // INDEX // border of each tile component (global) tilec->x0=int_ceildiv(tile->x0, img->comps[compno].dx); tilec->y0=int_ceildiv(tile->y0, img->comps[compno].dy); tilec->x1=int_ceildiv(tile->x1, img->comps[compno].dx); tilec->y1=int_ceildiv(tile->y1, img->comps[compno].dy); tilec->data=(int*)malloc(sizeof(int)*(tilec->x1-tilec->x0)*(tilec->y1-tilec->y0)); tilec->numresolutions=tccp->numresolutions; tilec->resolutions=(tcd_resolution_t*)malloc(tilec->numresolutions*sizeof(tcd_resolution_t)); compo_Idx->reso=(info_reso_t*)malloc(tilec->numresolutions*sizeof(info_reso_t)); // INDEX for (resno=0; resno<tilec->numresolutions; resno++) { int pdx, pdy; int levelno=tilec->numresolutions-1-resno; int tlprcxstart, tlprcystart, brprcxend, brprcyend; int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; int cbgwidthexpn, cbgheightexpn; int cblkwidthexpn, cblkheightexpn; tcd_resolution_t *res=&tilec->resolutions[resno]; info_reso_t *res_Idx=&compo_Idx->reso[resno]; // INDEX int precno_Idx; // INDEX // border for each resolution level (global) res->x0=int_ceildivpow2(tilec->x0, levelno); res->y0=int_ceildivpow2(tilec->y0, levelno); res->x1=int_ceildivpow2(tilec->x1, levelno); res->y1=int_ceildivpow2(tilec->y1, levelno); res->numbands=resno==0?1:3; // p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) if (tccp->csty&J2K_CCP_CSTY_PRT) { pdx=tccp->prcw[resno]; pdy=tccp->prch[resno]; } else { pdx=15; pdy=15; } // p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) tlprcxstart=int_floordivpow2(res->x0, pdx)<<pdx; tlprcystart=int_floordivpow2(res->y0, pdy)<<pdy; brprcxend=int_ceildivpow2(res->x1, pdx)<<pdx; brprcyend=int_ceildivpow2(res->y1, pdy)<<pdy; res->pw=(brprcxend-tlprcxstart)>>pdx; res->ph=(brprcyend-tlprcystart)>>pdy; // <INDEX> imgg->tile[tileno].pw=res->pw; imgg->tile[tileno].ph=res->ph; res_Idx->prec=(info_prec_t*)malloc(res->pw*res->ph*sizeof(info_prec_t)); for (precno_Idx=0;precno_Idx<res->pw*res->ph;precno_Idx++) { info_prec_t *prec_Idx = &res_Idx->prec[precno_Idx]; prec_Idx->layer=(info_layer_t*)malloc(imgg->Layer*sizeof(info_layer_t)); } imgg->pw=res->pw; // old parser version imgg->ph=res->ph; // old parser version imgg->pdx=1<<pdx; imgg->pdy=1<<pdy; // </INDEX> if (resno==0) { tlcbgxstart=tlprcxstart; tlcbgystart=tlprcystart; brcbgxend=brprcxend; brcbgyend=brprcyend; cbgwidthexpn=pdx; cbgheightexpn=pdy; } else { tlcbgxstart=int_ceildivpow2(tlprcxstart, 1); tlcbgystart=int_ceildivpow2(tlprcystart, 1); brcbgxend=int_ceildivpow2(brprcxend, 1); brcbgyend=int_ceildivpow2(brprcyend, 1); cbgwidthexpn=pdx-1; cbgheightexpn=pdy-1; } cblkwidthexpn=int_min(tccp->cblkw, cbgwidthexpn); cblkheightexpn=int_min(tccp->cblkh, cbgheightexpn); for (bandno=0; bandno<res->numbands; bandno++) { int x0b, y0b; tcd_band_t *band=&res->bands[bandno]; band->bandno=resno==0?0:bandno+1; x0b=(band->bandno==1)||(band->bandno==3)?1:0; y0b=(band->bandno==2)||(band->bandno==3)?1:0; if (band->bandno==0) { // band border (global) band->x0=int_ceildivpow2(tilec->x0, levelno); band->y0=int_ceildivpow2(tilec->y0, levelno); band->x1=int_ceildivpow2(tilec->x1, levelno); band->y1=int_ceildivpow2(tilec->y1, levelno); } else { // band border (global) band->x0=int_ceildivpow2(tilec->x0-(1<<levelno)*x0b, levelno+1); band->y0=int_ceildivpow2(tilec->y0-(1<<levelno)*y0b, levelno+1); band->x1=int_ceildivpow2(tilec->x1-(1<<levelno)*x0b, levelno+1); band->y1=int_ceildivpow2(tilec->y1-(1<<levelno)*y0b, levelno+1); } band->precincts=(tcd_precinct_t*)malloc(res->pw*res->ph*sizeof(tcd_precinct_t)); for (precno=0; precno<res->pw*res->ph; precno++) { int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; int cbgxstart=tlcbgxstart+(precno%res->pw)*(1<<cbgwidthexpn); int cbgystart=tlcbgystart+(precno/res->pw)*(1<<cbgheightexpn); int cbgxend=cbgxstart+(1<<cbgwidthexpn); int cbgyend=cbgystart+(1<<cbgheightexpn); tcd_precinct_t *prc=&band->precincts[precno]; // precinct size (global) prc->x0=int_max(cbgxstart, band->x0); prc->y0=int_max(cbgystart, band->y0); prc->x1=int_min(cbgxend, band->x1); prc->y1=int_min(cbgyend, band->y1); tlcblkxstart=int_floordivpow2(prc->x0, cblkwidthexpn)<<cblkwidthexpn; tlcblkystart=int_floordivpow2(prc->y0, cblkheightexpn)<<cblkheightexpn; brcblkxend=int_ceildivpow2(prc->x1, cblkwidthexpn)<<cblkwidthexpn; brcblkyend=int_ceildivpow2(prc->y1, cblkheightexpn)<<cblkheightexpn; prc->cw=(brcblkxend-tlcblkxstart)>>cblkwidthexpn; prc->ch=(brcblkyend-tlcblkystart)>>cblkheightexpn; prc->cblks=(tcd_cblk_t*)malloc(prc->cw*prc->ch*sizeof(tcd_cblk_t)); prc->incltree=tgt_create(prc->cw, prc->ch); prc->imsbtree=tgt_create(prc->cw, prc->ch); for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) { int cblkxstart=tlcblkxstart+(cblkno%prc->cw)*(1<<cblkwidthexpn); int cblkystart=tlcblkystart+(cblkno/prc->cw)*(1<<cblkheightexpn); int cblkxend=cblkxstart+(1<<cblkwidthexpn); int cblkyend=cblkystart+(1<<cblkheightexpn); tcd_cblk_t *cblk=&prc->cblks[cblkno]; // code-block size (global) cblk->x0=int_max(cblkxstart, prc->x0); cblk->y0=int_max(cblkystart, prc->y0); cblk->x1=int_min(cblkxend, prc->x1); cblk->y1=int_min(cblkyend, prc->y1); } } } } } } }
/* <summary> * Get next packet in component-precinct-resolution-layer order. * * pi: packet iterator to modify * </summary> */ int pi_next_cprl(pi_iterator_t * pi) { pi_comp_t *comp; pi_resolution_t *res; if (!pi->first) { comp = &pi->comps[pi->compno]; goto skip; } else { pi->first = 0; } for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { int resno; comp = &pi->comps[pi->compno]; pi->dx = 0; pi->dy = 0; for (resno = 0; resno < comp->numresolutions; resno++) { int dx, dy; res = &comp->resolutions[resno]; dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); } for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) { for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) { for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { int levelno; int trx0, try0; int trx1, try1;/* Add antonin pcrl*/ int rpx, rpy; int prci, prcj; res = &comp->resolutions[pi->resno]; levelno = comp->numresolutions - 1 - pi->resno; trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); try0 = int_ceildiv(pi->ty0, comp->dy << levelno); trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/ try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/ rpx = res->pdx + levelno; rpy = res->pdy + levelno; if (! (pi->x % (comp->dx << rpx) == 0 || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) { continue; } if (! (pi->y % (comp->dy << rpy) == 0 || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) { continue; } /*Add Antonin : sizebug1*/ if ((res->pw==0)||(res->pw==0)) continue; /*ddA*/ /*Add Antonin : pcrl*/ if ((trx0==trx1)||(try0==try1)) continue; /*ddA*/ prci = int_floordivpow2(int_ceildiv (pi->x, comp->dx << levelno), res->pdx) - int_floordivpow2(trx0, res->pdx); prcj = int_floordivpow2(int_ceildiv (pi->y, comp->dy << levelno), res->pdy) - int_floordivpow2(try0, res->pdy); pi->precno = prci + prcj * res->pw; for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { if (!pi-> include[pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p]) { pi->include[pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p] = 1; return 1; } skip:; } } } } } return 0; }
/* <summary> * Create a packet iterator. * </summary> */ pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno) { int p, q, i; int compno, resno, pino; int maxres = 0; pi_iterator_t *pi; j2k_tcp_t *tcp; j2k_tccp_t *tccp; tcp = &cp->tcps[tileno]; pi = (pi_iterator_t *) malloc((tcp->numpocs + 1) * sizeof(pi_iterator_t)); for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */ p = tileno % cp->tw; q = tileno / cp->tw; pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, img->x0); pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, img->y0); pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1); pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1); pi[pino].numcomps = img->numcomps; pi[pino].comps = (pi_comp_t *) malloc(img->numcomps * sizeof(pi_comp_t)); for (compno = 0; compno < pi->numcomps; compno++) { int tcx0, tcy0, tcx1, tcy1; pi_comp_t *comp = &pi[pino].comps[compno]; tccp = &tcp->tccps[compno]; comp->dx = img->comps[compno].dx; comp->dy = img->comps[compno].dy; comp->numresolutions = tccp->numresolutions; comp->resolutions = (pi_resolution_t *) malloc(comp->numresolutions * sizeof(pi_resolution_t)); tcx0 = int_ceildiv(pi->tx0, comp->dx); tcy0 = int_ceildiv(pi->ty0, comp->dy); tcx1 = int_ceildiv(pi->tx1, comp->dx); tcy1 = int_ceildiv(pi->ty1, comp->dy); if (comp->numresolutions > maxres) { maxres = comp->numresolutions; } for (resno = 0; resno < comp->numresolutions; resno++) { int levelno; int rx0, ry0, rx1, ry1; int px0, py0, px1, py1; pi_resolution_t *res = &comp->resolutions[resno]; if (tccp->csty & J2K_CCP_CSTY_PRT) { res->pdx = tccp->prcw[resno]; res->pdy = tccp->prch[resno]; } else { res->pdx = 15; res->pdy = 15; } levelno = comp->numresolutions - 1 - resno; rx0 = int_ceildivpow2(tcx0, levelno); ry0 = int_ceildivpow2(tcy0, levelno); rx1 = int_ceildivpow2(tcx1, levelno); ry1 = int_ceildivpow2(tcy1, levelno); px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); /*Mod Antonin : sizebug1*/ res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); /*Mod Antonin : sizebug1*/ } } tccp = &tcp->tccps[0]; pi[pino].step_p = 1; pi[pino].step_c = 100 * pi[pino].step_p; pi[pino].step_r = img->numcomps * pi[pino].step_c; pi[pino].step_l = maxres * pi[pino].step_r; if (pino == 0) { pi[pino].include = (short int *) malloc(img->numcomps * maxres * tcp->numlayers * 100 * sizeof(short int)); for (i = 0; i < img->numcomps * maxres * tcp->numlayers * 100; i++) pi[pino].include[i] = 0; } /* pi[pino].include=(short int*)calloc(img->numcomps*maxres*tcp->numlayers*1000,sizeof(short int)); */ else pi[pino].include = pi[pino - 1].include; if (tcp->POC == 0) { pi[pino].first = 1; pi[pino].poc.resno0 = 0; pi[pino].poc.compno0 = 0; pi[pino].poc.layno1 = tcp->numlayers; pi[pino].poc.resno1 = maxres; pi[pino].poc.compno1 = img->numcomps; pi[pino].poc.prg = tcp->prg; } else { pi[pino].first = 1; pi[pino].poc.resno0 = tcp->pocs[pino].resno0; pi[pino].poc.compno0 = tcp->pocs[pino].compno0; pi[pino].poc.layno1 = tcp->pocs[pino].layno1; pi[pino].poc.resno1 = tcp->pocs[pino].resno1; pi[pino].poc.compno1 = tcp->pocs[pino].compno1; pi[pino].poc.prg = tcp->pocs[pino].prg; } } return pi; }