/** * Updates the components characteristics of the image from the coding parameters. * * @param p_image_header the image header to update. * @param p_cp the coding parameters from which to update the image. */ void opj_image_comp_header_update(opj_image_t * p_image_header, const struct opj_cp_v2 * p_cp) { OPJ_UINT32 i, l_width, l_height; OPJ_INT32 l_x0, l_y0, l_x1, l_y1; OPJ_INT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1; opj_image_comp_t* l_img_comp = NULL; l_x0 = int_max(p_cp->tx0 , p_image_header->x0); l_y0 = int_max(p_cp->ty0 , p_image_header->y0); l_x1 = int_min(p_cp->tx0 + p_cp->tw * p_cp->tdx, p_image_header->x1); l_y1 = int_min(p_cp->ty0 + p_cp->th * p_cp->tdy, p_image_header->y1); l_img_comp = p_image_header->comps; for (i = 0; i < p_image_header->numcomps; ++i) { l_comp_x0 = int_ceildiv(l_x0, l_img_comp->dx); l_comp_y0 = int_ceildiv(l_y0, l_img_comp->dy); l_comp_x1 = int_ceildiv(l_x1, l_img_comp->dx); l_comp_y1 = int_ceildiv(l_y1, l_img_comp->dy); l_width = int_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor); l_height = int_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor); l_img_comp->w = l_width; l_img_comp->h = l_height; l_img_comp->x0 = l_comp_x0/*l_x0*/; l_img_comp->y0 = l_comp_y0/*l_y0*/; ++l_img_comp; } }
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; }
/// <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; }
int imagetobmp(opj_image_t * image, char *outfile) { int w, wr, h, hr; int i, pad; FILE *fdest = NULL; if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx && image->comps[1].dx == image->comps[2].dx && image->comps[0].dy == image->comps[1].dy && image->comps[1].dy == image->comps[2].dy && image->comps[0].prec == image->comps[1].prec && image->comps[1].prec == image->comps[2].prec) { /* -->> -->> -->> -->> 24 bits color <<-- <<-- <<-- <<-- */ fdest = fopen(outfile, "wb"); if (!fdest) { fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); return 1; } /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */ /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */ w = image->comps[0].w; wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor); /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */ /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */ h = image->comps[0].h; hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor); fprintf(fdest, "BM"); /* FILE HEADER */ /* ------------- */ fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff); /* INFO HEADER */ /* ------------- */ fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff), (unsigned char) ((wr) >> 8) & 0xff, (unsigned char) ((wr) >> 16) & 0xff, (unsigned char) ((wr) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff), (unsigned char) ((hr) >> 8) & 0xff, (unsigned char) ((hr) >> 16) & 0xff, (unsigned char) ((hr) >> 24) & 0xff); fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff); fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff); fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); for (i = 0; i < wr * hr; i++) { unsigned char R, G, B; /* a modifier */ /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */ R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */ G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */ B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; fprintf(fdest, "%c%c%c", B, G, R); if ((i + 1) % wr == 0) { for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */ fprintf(fdest, "%c", 0); } } fclose(fdest); } else { /* Gray-scale */
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; }
int main(int argc, char **argv) { FILE *f; char *src, *src_name; char *dest, S1, S2, S3; int len; j2k_image_t img; j2k_cp_t cp; int w, wr, wrr, h, hr, hrr, max; int i, image_type = -1, compno, pad, j; int adjust; jp2_struct_t *jp2_struct; if (argc < 3) { fprintf(stderr, "usage: %s j2k-file image-file [-reduce n]\n", argv[0]); return 1; } f = fopen(argv[1], "rb"); if (!f) { fprintf(stderr, "failed to open %s for reading\n", argv[1]); return 1; } dest = argv[2]; cp.reduce_on = 0; cp.reduce_value = 0; /* OPTION REDUCE IS ACTIVE */ if (argc == 5) { if (strcmp(argv[3], "-reduce")) { fprintf(stderr, "usage: options " "-reduce n" " where n is the factor of reduction [%s]\n", argv[3]); return 1; } cp.reduce_on = 1; sscanf(argv[4], "%d", &cp.reduce_value); } while (*dest) { dest++; } dest--; S3 = *dest; dest--; S2 = *dest; dest--; S1 = *dest; if ((S1 == 'p' && S2 == 'g' && S3 == 'x') || (S1 == 'P' && S2 == 'G' && S3 == 'X')) { image_type = 0; dest--; *dest = '\0'; } if ((S1 == 'p' && S2 == 'n' && S3 == 'm') || (S1 == 'P' && S2 == 'N' && S3 == 'M') || (S1 == 'p' && S2 == 'g' && S3 == 'm') || (S1 == 'P' && S2 == 'G' && S3 == 'M') || (S1 == 'P' && S2 == 'P' && S3 == 'M') || (S1 == 'p' && S2 == 'p' && S3 == 'm')) { image_type = 1; } if ((S1 == 'b' && S2 == 'm' && S3 == 'p') || (S1 == 'B' && S2 == 'M' && S3 == 'P')) { image_type = 2; } if (image_type == -1) { fprintf(stderr, "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n", S1, S2, S3); return 1; } fseek(f, 0, SEEK_END); len = ftell(f); fseek(f, 0, SEEK_SET); src = (char *) malloc(len); fread(src, 1, len, f); fclose(f); src_name = argv[1]; while (*src_name) { src_name++; } src_name--; S3 = *src_name; src_name--; S2 = *src_name; src_name--; S1 = *src_name; /* J2K format */ if ((S1 == 'j' && S2 == '2' && S3 == 'k') || (S1 == 'J' && S2 == '2' && S3 == 'K') || (S1 == 'j' && S2 == '2' && S3 == 'c') || (S1 == 'J' && S2 == '2' && S3 == 'C')) { if (!j2k_decode(src, len, &img, &cp)) { fprintf(stderr, "j2k_to_image: failed to decode image!\n"); return 1; } } /* JP2 format */ else if ((S1 == 'j' && S2 == 'p' && S3 == '2') || (S1 == 'J' && S2 == 'P' && S3 == '2')) { jp2_struct = (jp2_struct_t *) malloc(sizeof(jp2_struct_t)); jp2_struct->image = &img; if (jp2_decode(src, len, jp2_struct, &cp)) { fprintf(stderr, "j2k_to_image: failed to decode image!\n"); return 1; } /* Insert code here if you want to create actions on jp2_struct before deleting it */ free(jp2_struct); } /* JPT format */ else if ((S1 == 'j' && S2 == 'p' && S3 == 't') || (S1 == 'J' && S2 == 'P' && S3 == 'T')) { if (!j2k_decode_jpt_stream(src, len, &img, &cp)) { fprintf(stderr, "j2k_to_image: failed to decode image!\n"); return 1; } } /* otherwise : error */ else { fprintf(stderr, "j2k_to_image : Unknown format image *.%c%c%c [only *.j2k, *.jp2, *.jpc or *.jpt]!! \n", S1, S2, S3); return 1; } free(src); /* ------------------ CREATE OUT IMAGE WITH THE RIGHT FORMAT ----------------------- */ /* ---------------------------- / */ /* / / */ /* / FORMAT : PNM, PGM or PPM / */ /* / / */ /* ---------------------------- / */ switch (image_type) { case 1: /* PNM PGM PPM */ if (img.numcomps == 3 && img.comps[0].dx == img.comps[1].dx && img.comps[1].dx == img.comps[2].dx && img.comps[0].dy == img.comps[1].dy && img.comps[1].dy == img.comps[2].dy && img.comps[0].prec == img.comps[1].prec && img.comps[1].prec == img.comps[2].prec) { f = fopen(argv[2], "wb"); w = ceildiv(img.x1 - img.x0, img.comps[0].dx); // wr = ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor),img.comps[0].dx); wr = img.comps[0].w; wrr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor); h = ceildiv(img.y1 - img.y0, img.comps[0].dy); // hr = ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy); hr = img.comps[0].h; hrr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor); max = img.comps[0].prec > 8 ? 255 : (1 << img.comps[0].prec) - 1; img.comps[0].x0 = int_ceildivpow2(img.comps[0].x0 - int_ceildiv(img.x0, img.comps[0].dx), img.comps[0].factor); img.comps[0].y0 = int_ceildivpow2(img.comps[0].y0 - int_ceildiv(img.y0, img.comps[0].dy), img.comps[0].factor); fprintf(f, "P6\n# %d %d %d %d %d\n%d %d\n%d\n", cp.tcps[cp.tileno[0]].tccps[0].numresolutions, w, h, img.comps[0].x0, img.comps[0].y0, wrr, hrr, max); adjust = img.comps[0].prec > 8 ? img.comps[0].prec - 8 : 0; for (i = 0; i < wrr * hrr; i++) { char r, g, b; r = img.comps[0].data[i / wrr * wr + i % wrr]; r += (img.comps[0].sgnd ? 1 << (img.comps[0].prec - 1) : 0); r = r >> adjust; g = img.comps[1].data[i / wrr * wr + i % wrr]; g += (img.comps[1].sgnd ? 1 << (img.comps[1].prec - 1) : 0); g = g >> adjust; b = img.comps[2].data[i / wrr * wr + i % wrr]; b += (img.comps[2].sgnd ? 1 << (img.comps[2].prec - 1) : 0); b = b >> adjust; fprintf(f, "%c%c%c", r, g, b); } free(img.comps[0].data); free(img.comps[1].data); free(img.comps[2].data); fclose(f); } else { for (compno = 0; compno < img.numcomps; compno++) {
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; } }
int imagetobmp(opj_image_t * img, char *outfile) { int w,wr,h,hr,i,pad; FILE *f; if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx && img->comps[1].dx == img->comps[2].dx && img->comps[0].dy == img->comps[1].dy && img->comps[1].dy == img->comps[2].dy && img->comps[0].prec == img->comps[1].prec && img->comps[1].prec == img->comps[2].prec) { /* -->> -->> -->> -->> 24 bits color <<-- <<-- <<-- <<-- */ f = fopen(outfile, "wb"); if (!f) { fprintf(stderr, "failed to open %s for writing\n", outfile); return 1; } w = img->comps[0].w; wr = int_ceildivpow2(img->comps[0].w, img->comps[0].factor); h = img->comps[0].h; hr = int_ceildivpow2(img->comps[0].h, img->comps[0].factor); fprintf(f, "BM"); /* FILE HEADER */ /* ------------- */ fprintf(f, "%c%c%c%c", (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff); fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff, ((54) >> 24) & 0xff); /* INFO HEADER */ /* ------------- */ fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff); fprintf(f, "%c%c%c%c", (unsigned char) ((wr) & 0xff), (unsigned char) ((wr) >> 8) & 0xff, (unsigned char) ((wr) >> 16) & 0xff, (unsigned char) ((wr) >> 24) & 0xff); fprintf(f, "%c%c%c%c", (unsigned char) ((hr) & 0xff), (unsigned char) ((hr) >> 8) & 0xff, (unsigned char) ((hr) >> 16) & 0xff, (unsigned char) ((hr) >> 24) & 0xff); fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff); fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff); fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); fprintf(f, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff, (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff); fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); for (i = 0; i < wr * hr; i++) { unsigned char R, G, B; /* a modifier */ // R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; R = img->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; // G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; G = img->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; // B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; B = img->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; fprintf(f, "%c%c%c", B, G, R); if ((i + 1) % wr == 0) { for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */ fprintf(f, "%c", 0); } } fclose(f); }
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> * 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; }
static GF_Err JP2_ProcessData(GF_MediaDecoder *ifcg, char *inBuffer, u32 inBufferLength, u16 ES_ID, char *outBuffer, u32 *outBufferLength, u8 PaddingBits, u32 mmlevel) { u32 i, w, wr, h, hr, wh; opj_dparameters_t parameters; /* decompression parameters */ opj_event_mgr_t event_mgr; /* event manager */ opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */ opj_cio_t *cio = NULL; opj_codestream_info_t cinfo; JP2CTX(); #if 1 switch (mmlevel) { case GF_CODEC_LEVEL_SEEK: case GF_CODEC_LEVEL_DROP: *outBufferLength = 0; return GF_OK; } #endif if (!ctx->image) { /* configure the event callbacks (not required) */ memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); event_mgr.error_handler = error_callback; event_mgr.warning_handler = warning_callback; event_mgr.info_handler = info_callback; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_JP2); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using the current image and user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ if (ctx->dsi) { char *data; data = gf_malloc(sizeof(char) * (ctx->dsi_size+inBufferLength)); memcpy(data, ctx->dsi, ctx->dsi_size); memcpy(data+ctx->dsi_size, inBuffer, inBufferLength); cio = opj_cio_open((opj_common_ptr)dinfo, data, ctx->dsi_size+inBufferLength); /* decode the stream and fill the image structure */ ctx->image = opj_decode(dinfo, cio); gf_free(data); } else { cio = opj_cio_open((opj_common_ptr)dinfo, inBuffer, inBufferLength); /* decode the stream and fill the image structure */ ctx->image = opj_decode_with_info(dinfo, cio, &cinfo); } //Fill the ctx info because dsi was not present if (ctx->image) { ctx->nb_comp = cinfo.numcomps; ctx->width = cinfo.image_w; ctx->height = cinfo.image_h; ctx->bpp = ctx->nb_comp * 8; ctx->out_size = ctx->width * ctx->height * ctx->nb_comp /* * ctx->bpp / 8 */; switch (ctx->nb_comp) { case 1: ctx->pixel_format = GF_PIXEL_GREYSCALE; break; case 2: ctx->pixel_format = GF_PIXEL_ALPHAGREY; break; case 3: ctx->pixel_format = GF_PIXEL_RGB_24; break; case 4: ctx->pixel_format = GF_PIXEL_RGBA; break; default: return GF_NOT_SUPPORTED; } if ( *outBufferLength < ctx->out_size ) { *outBufferLength = ctx->out_size; opj_destroy_decompress(dinfo); opj_cio_close(cio); return GF_BUFFER_TOO_SMALL; } } if(!ctx->image) { opj_destroy_decompress(dinfo); opj_cio_close(cio); return GF_IO_ERR; } /* close the byte stream */ opj_cio_close(cio); cio = NULL; /* gf_free( remaining structures */ if(dinfo) { opj_destroy_decompress(dinfo); dinfo = NULL; } } w = ctx->image->comps[0].w; wr = int_ceildivpow2(ctx->image->comps[0].w, ctx->image->comps[0].factor); h = ctx->image->comps[0].h; hr = int_ceildivpow2(ctx->image->comps[0].h, ctx->image->comps[0].factor); wh = wr*hr; if (ctx->nb_comp==1) { if ((w==wr) && (h==hr)) { for (i=0; i<wh; i++) { outBuffer[i] = ctx->image->comps[0].data[i]; } } else { for (i=0; i<wh; i++) { outBuffer[i] = ctx->image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; } } } else if (ctx->nb_comp==3) { if ((ctx->image->comps[0].w==2*ctx->image->comps[1].w) && (ctx->image->comps[1].w==ctx->image->comps[2].w) && (ctx->image->comps[0].h==2*ctx->image->comps[1].h) && (ctx->image->comps[1].h==ctx->image->comps[2].h)) { if (ctx->pixel_format != GF_PIXEL_YV12) { ctx->pixel_format = GF_PIXEL_YV12; ctx->out_size = 3*ctx->width*ctx->height/2; *outBufferLength = ctx->out_size; return GF_BUFFER_TOO_SMALL; } if ((w==wr) && (h==hr)) { for (i=0; i<wh; i++) { *outBuffer = ctx->image->comps[0].data[i]; outBuffer++; } w = ctx->image->comps[1].w; wr = int_ceildivpow2(ctx->image->comps[1].w, ctx->image->comps[1].factor); h = ctx->image->comps[1].h; hr = int_ceildivpow2(ctx->image->comps[1].h, ctx->image->comps[1].factor); wh = wr*hr; for (i=0; i<wh; i++) { *outBuffer = ctx->image->comps[1].data[i]; outBuffer++; } for (i=0; i<wh; i++) { *outBuffer = ctx->image->comps[2].data[i]; outBuffer++; } } else { for (i=0; i<wh; i++) { *outBuffer = ctx->image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; } w = ctx->image->comps[1].w; wr = int_ceildivpow2(ctx->image->comps[1].w, ctx->image->comps[1].factor); h = ctx->image->comps[1].h; hr = int_ceildivpow2(ctx->image->comps[1].h, ctx->image->comps[1].factor); wh = wr*hr; for (i=0; i<wh; i++) { *outBuffer = ctx->image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; } for (i=0; i<wh; i++) { *outBuffer = ctx->image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; } } } else if ((ctx->image->comps[0].w==ctx->image->comps[1].w) && (ctx->image->comps[1].w==ctx->image->comps[2].w) && (ctx->image->comps[0].h==ctx->image->comps[1].h) && (ctx->image->comps[1].h==ctx->image->comps[2].h)) { if ((w==wr) && (h==hr)) { for (i=0; i<wh; i++) { u32 idx = 3*i; outBuffer[idx] = ctx->image->comps[0].data[i]; outBuffer[idx+1] = ctx->image->comps[1].data[i]; outBuffer[idx+2] = ctx->image->comps[2].data[i]; } } else { for (i=0; i<wh; i++) { u32 idx = 3*i; outBuffer[idx] = ctx->image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; outBuffer[idx+1] = ctx->image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; outBuffer[idx+2] = ctx->image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; } } } } else if (ctx->nb_comp==4) { if ((ctx->image->comps[0].w==ctx->image->comps[1].w) && (ctx->image->comps[1].w==ctx->image->comps[2].w) && (ctx->image->comps[2].w==ctx->image->comps[3].w) && (ctx->image->comps[0].h==ctx->image->comps[1].h) && (ctx->image->comps[1].h==ctx->image->comps[2].h) && (ctx->image->comps[2].h==ctx->image->comps[3].h)) { if ((w==wr) && (h==hr)) { for (i=0; i<wh; i++) { u32 idx = 4*i; outBuffer[idx] = ctx->image->comps[0].data[i]; outBuffer[idx+1] = ctx->image->comps[1].data[i]; outBuffer[idx+2] = ctx->image->comps[2].data[i]; outBuffer[idx+3] = ctx->image->comps[3].data[i]; } } else { for (i=0; i<wh; i++) { u32 idx = 4*i; outBuffer[idx] = ctx->image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; outBuffer[idx+1] = ctx->image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; outBuffer[idx+2] = ctx->image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; outBuffer[idx+3] = ctx->image->comps[3].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; } } } } /* gf_free( image data structure */ if (ctx->image) { opj_image_destroy(ctx->image); ctx->image = NULL; } *outBufferLength = ctx->out_size; return GF_OK; }
/** Convert a OpenJPEG image to a FIBITMAP @param format_id Plugin ID @param image OpenJPEG image @param header_only If TRUE, allocate a 'header only' FIBITMAP, otherwise allocate a full FIBITMAP @return Returns the converted image if successful, returns NULL otherwise */ FIBITMAP* J2KImageToFIBITMAP(int format_id, const opj_image_t *image, BOOL header_only) { FIBITMAP *dib = NULL; try { // compute image width and height //int w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); int wr = image->comps[0].w; int wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor); //int h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); //int hr = image->comps[0].h; int hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor); // check the number of components int numcomps = image->numcomps; BOOL bIsValid = TRUE; for(int c = 0; c < numcomps - 1; c++) { if( (image->comps[c].dx == image->comps[c+1].dx) && (image->comps[c].dy == image->comps[c+1].dy) && (image->comps[c].prec == image->comps[c+1].prec) ) { continue; } else { bIsValid = FALSE; break; } } bIsValid &= ((numcomps == 1) || (numcomps == 3) || (numcomps == 4)); if(!bIsValid) { if(numcomps) { FreeImage_OutputMessageProc(format_id, "Warning: image contains %d greyscale components. Only the first will be loaded.\n", numcomps); numcomps = 1; } else { // unknown type throw FI_MSG_ERROR_UNSUPPORTED_FORMAT; } } // create a new DIB if(image->comps[0].prec <= 8) { switch(numcomps) { case 1: dib = FreeImage_AllocateHeader(header_only, wrr, hrr, 8); break; case 3: dib = FreeImage_AllocateHeader(header_only, wrr, hrr, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); break; case 4: dib = FreeImage_AllocateHeader(header_only, wrr, hrr, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); break; } } else if(image->comps[0].prec <= 16) { switch(numcomps) { case 1: dib = FreeImage_AllocateHeaderT(header_only, FIT_UINT16, wrr, hrr); break; case 3: dib = FreeImage_AllocateHeaderT(header_only, FIT_RGB16, wrr, hrr); break; case 4: dib = FreeImage_AllocateHeaderT(header_only, FIT_RGBA16, wrr, hrr); break; } } else { throw FI_MSG_ERROR_UNSUPPORTED_FORMAT; } if(!dib) { throw FI_MSG_ERROR_DIB_MEMORY; } // "header only" FIBITMAP ? if(header_only) { return dib; } if(image->comps[0].prec <= 8) { if(numcomps == 1) { // 8-bit greyscale // ---------------------------------------------------------- // build a greyscale palette RGBQUAD *pal = FreeImage_GetPalette(dib); for (int i = 0; i < 256; i++) { pal[i].rgbRed = (BYTE)i; pal[i].rgbGreen = (BYTE)i; pal[i].rgbBlue = (BYTE)i; } // load pixel data unsigned pixel_count = 0; for(int y = 0; y < hrr; y++) { BYTE *bits = FreeImage_GetScanLine(dib, hrr - 1 - y); for(int x = 0; x < wrr; x++) { const unsigned pixel_pos = pixel_count / wrr * wr + pixel_count % wrr; int index = image->comps[0].data[pixel_pos]; index += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); bits[x] = (BYTE)index; pixel_count++; } } } else if(numcomps == 3) { // 24-bit RGB // ---------------------------------------------------------- // load pixel data unsigned pixel_count = 0; for(int y = 0; y < hrr; y++) { BYTE *bits = FreeImage_GetScanLine(dib, hrr - 1 - y); for(int x = 0; x < wrr; x++) { const unsigned pixel_pos = pixel_count / wrr * wr + pixel_count % wrr; int r = image->comps[0].data[pixel_pos]; r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); int g = image->comps[1].data[pixel_pos]; g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); int b = image->comps[2].data[pixel_pos]; b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); bits[FI_RGBA_RED] = (BYTE)r; bits[FI_RGBA_GREEN] = (BYTE)g; bits[FI_RGBA_BLUE] = (BYTE)b; bits += 3; pixel_count++; } } } else if(numcomps == 4) { // 32-bit RGBA // ---------------------------------------------------------- // load pixel data unsigned pixel_count = 0; for(int y = 0; y < hrr; y++) { BYTE *bits = FreeImage_GetScanLine(dib, hrr - 1 - y); for(int x = 0; x < wrr; x++) { const unsigned pixel_pos = pixel_count / wrr * wr + pixel_count % wrr; int r = image->comps[0].data[pixel_pos]; r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); int g = image->comps[1].data[pixel_pos]; g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); int b = image->comps[2].data[pixel_pos]; b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); int a = image->comps[3].data[pixel_pos]; a += (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0); bits[FI_RGBA_RED] = (BYTE)r; bits[FI_RGBA_GREEN] = (BYTE)g; bits[FI_RGBA_BLUE] = (BYTE)b; bits[FI_RGBA_ALPHA] = (BYTE)a; bits += 4; pixel_count++; } } } } else if(image->comps[0].prec <= 16) { if(numcomps == 1) { // 16-bit greyscale // ---------------------------------------------------------- // load pixel data unsigned pixel_count = 0; for(int y = 0; y < hrr; y++) { WORD *bits = (WORD*)FreeImage_GetScanLine(dib, hrr - 1 - y); for(int x = 0; x < wrr; x++) { const unsigned pixel_pos = pixel_count / wrr * wr + pixel_count % wrr; int index = image->comps[0].data[pixel_pos]; index += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); bits[x] = (WORD)index; pixel_count++; } } } else if(numcomps == 3) { // 48-bit RGB // ---------------------------------------------------------- // load pixel data unsigned pixel_count = 0; for(int y = 0; y < hrr; y++) { FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, hrr - 1 - y); for(int x = 0; x < wrr; x++) { const unsigned pixel_pos = pixel_count / wrr * wr + pixel_count % wrr; int r = image->comps[0].data[pixel_pos]; r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); int g = image->comps[1].data[pixel_pos]; g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); int b = image->comps[2].data[pixel_pos]; b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); bits[x].red = (WORD)r; bits[x].green = (WORD)g; bits[x].blue = (WORD)b; pixel_count++; } } } else if(numcomps == 4) { // 64-bit RGBA // ---------------------------------------------------------- // load pixel data unsigned pixel_count = 0; for(int y = 0; y < hrr; y++) { FIRGBA16 *bits = (FIRGBA16*)FreeImage_GetScanLine(dib, hrr - 1 - y); for(int x = 0; x < wrr; x++) { const unsigned pixel_pos = pixel_count / wrr * wr + pixel_count % wrr; int r = image->comps[0].data[pixel_pos]; r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); int g = image->comps[1].data[pixel_pos]; g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); int b = image->comps[2].data[pixel_pos]; b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); int a = image->comps[3].data[pixel_pos]; a += (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0); bits[x].red = (WORD)r; bits[x].green = (WORD)g; bits[x].blue = (WORD)b; bits[x].alpha = (WORD)a; pixel_count++; } } } } return dib; } catch(const char *text) { if(dib) FreeImage_Unload(dib); FreeImage_OutputMessageProc(format_id, text); return NULL; } }