diff options
| author | Antonin Descampe <antonin@gmail.com> | 2004-08-03 14:14:44 +0000 |
|---|---|---|
| committer | Antonin Descampe <antonin@gmail.com> | 2004-08-03 14:14:44 +0000 |
| commit | d116ca5d10f69e27c6699eca50ee2a41ea214571 (patch) | |
| tree | f9dad15ee73678988efbb21ea9cfce6b075857de /libopenjpeg | |
| parent | 26a630dad261c5ed4a8ce4bbffcae5fb9e475863 (diff) | |
Better indentation
Diffstat (limited to 'libopenjpeg')
| -rw-r--r-- | libopenjpeg/j2k.c | 1931 | ||||
| -rw-r--r-- | libopenjpeg/tcd.c | 2756 |
2 files changed, 2130 insertions, 2557 deletions
diff --git a/libopenjpeg/j2k.c b/libopenjpeg/j2k.c index 079ffa5b..e8f766e4 100644 --- a/libopenjpeg/j2k.c +++ b/libopenjpeg/j2k.c @@ -87,202 +87,182 @@ static int *j2k_tile_len; static info_image info_IM; /* Add Patrick */ -void -j2k_clean () +void j2k_clean() { int tileno = 0; - tcd_free_encode (j2k_img, j2k_cp, j2k_curtileno); + tcd_free_encode(j2k_img, j2k_cp, j2k_curtileno); - if (info_IM.index_on) - { - for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++) - { - free (info_IM.tile[tileno].packet); - } - free (info_IM.tile); + if (info_IM.index_on) { + for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++) { + free(info_IM.tile[tileno].packet); } + free(info_IM.tile); + } } /* \Add Patrick */ -void -j2k_dump_image (j2k_image_t * img) +void j2k_dump_image(j2k_image_t * img) { int compno; - fprintf (stderr, "image {\n"); - fprintf (stderr, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, - img->x1, img->y1); - fprintf (stderr, " numcomps=%d\n", img->numcomps); - for (compno = 0; compno < img->numcomps; compno++) - { - j2k_comp_t *comp = &img->comps[compno]; - fprintf (stderr, " comp %d {\n", compno); - fprintf (stderr, " dx=%d, dy=%d\n", comp->dx, comp->dy); - fprintf (stderr, " prec=%d\n", comp->prec); - fprintf (stderr, " sgnd=%d\n", comp->sgnd); - fprintf (stderr, " }\n"); - } - fprintf (stderr, "}\n"); + fprintf(stderr, "image {\n"); + fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, + img->x1, img->y1); + fprintf(stderr, " numcomps=%d\n", img->numcomps); + for (compno = 0; compno < img->numcomps; compno++) { + j2k_comp_t *comp = &img->comps[compno]; + fprintf(stderr, " comp %d {\n", compno); + fprintf(stderr, " dx=%d, dy=%d\n", comp->dx, comp->dy); + fprintf(stderr, " prec=%d\n", comp->prec); + fprintf(stderr, " sgnd=%d\n", comp->sgnd); + fprintf(stderr, " }\n"); + } + fprintf(stderr, "}\n"); } -void -j2k_dump_cp (j2k_image_t * img, j2k_cp_t * cp) +void j2k_dump_cp(j2k_image_t * img, j2k_cp_t * cp) { int tileno, compno, layno, bandno, resno, numbands; - fprintf (stderr, "coding parameters {\n"); - fprintf (stderr, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0); - fprintf (stderr, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy); - fprintf (stderr, " tw=%d, th=%d\n", cp->tw, cp->th); - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) - { - j2k_tcp_t *tcp = &cp->tcps[tileno]; - fprintf (stderr, " tile %d {\n", tileno); - fprintf (stderr, " csty=%x\n", tcp->csty); - fprintf (stderr, " prg=%d\n", tcp->prg); - fprintf (stderr, " numlayers=%d\n", tcp->numlayers); - fprintf (stderr, " mct=%d\n", tcp->mct); - fprintf (stderr, " rates="); - for (layno = 0; layno < tcp->numlayers; layno++) - { - fprintf (stderr, "%d ", tcp->rates[layno]); + fprintf(stderr, "coding parameters {\n"); + fprintf(stderr, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0); + fprintf(stderr, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy); + fprintf(stderr, " tw=%d, th=%d\n", cp->tw, cp->th); + for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { + j2k_tcp_t *tcp = &cp->tcps[tileno]; + fprintf(stderr, " tile %d {\n", tileno); + fprintf(stderr, " csty=%x\n", tcp->csty); + fprintf(stderr, " prg=%d\n", tcp->prg); + fprintf(stderr, " numlayers=%d\n", tcp->numlayers); + fprintf(stderr, " mct=%d\n", tcp->mct); + fprintf(stderr, " rates="); + for (layno = 0; layno < tcp->numlayers; layno++) { + fprintf(stderr, "%d ", tcp->rates[layno]); + } + fprintf(stderr, "\n"); + for (compno = 0; compno < img->numcomps; compno++) { + j2k_tccp_t *tccp = &tcp->tccps[compno]; + fprintf(stderr, " comp %d {\n", compno); + fprintf(stderr, " csty=%x\n", tccp->csty); + fprintf(stderr, " numresolutions=%d\n", tccp->numresolutions); + fprintf(stderr, " cblkw=%d\n", tccp->cblkw); + fprintf(stderr, " cblkh=%d\n", tccp->cblkh); + fprintf(stderr, " cblksty=%x\n", tccp->cblksty); + fprintf(stderr, " qmfbid=%d\n", tccp->qmfbid); + fprintf(stderr, " qntsty=%d\n", tccp->qntsty); + fprintf(stderr, " numgbits=%d\n", tccp->numgbits); + fprintf(stderr, " roishift=%d\n", tccp->roishift); + fprintf(stderr, " stepsizes="); + numbands = + tccp->qntsty == + J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; + for (bandno = 0; bandno < numbands; bandno++) { + fprintf(stderr, "(%d,%d) ", tccp->stepsizes[bandno].mant, + tccp->stepsizes[bandno].expn); + } + fprintf(stderr, "\n"); + + if (tccp->csty & J2K_CCP_CSTY_PRT) { + fprintf(stderr, " prcw="); + for (resno = 0; resno < tccp->numresolutions; resno++) { + fprintf(stderr, "%d ", tccp->prcw[resno]); } - fprintf (stderr, "\n"); - for (compno = 0; compno < img->numcomps; compno++) - { - j2k_tccp_t *tccp = &tcp->tccps[compno]; - fprintf (stderr, " comp %d {\n", compno); - fprintf (stderr, " csty=%x\n", tccp->csty); - fprintf (stderr, " numresolutions=%d\n", tccp->numresolutions); - fprintf (stderr, " cblkw=%d\n", tccp->cblkw); - fprintf (stderr, " cblkh=%d\n", tccp->cblkh); - fprintf (stderr, " cblksty=%x\n", tccp->cblksty); - fprintf (stderr, " qmfbid=%d\n", tccp->qmfbid); - fprintf (stderr, " qntsty=%d\n", tccp->qntsty); - fprintf (stderr, " numgbits=%d\n", tccp->numgbits); - fprintf (stderr, " roishift=%d\n", tccp->roishift); - fprintf (stderr, " stepsizes="); - numbands = - tccp->qntsty == - J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; - for (bandno = 0; bandno < numbands; bandno++) - { - fprintf (stderr, "(%d,%d) ", tccp->stepsizes[bandno].mant, - tccp->stepsizes[bandno].expn); - } - fprintf (stderr, "\n"); - - if (tccp->csty & J2K_CCP_CSTY_PRT) - { - fprintf (stderr, " prcw="); - for (resno = 0; resno < tccp->numresolutions; resno++) - { - fprintf (stderr, "%d ", tccp->prcw[resno]); - } - fprintf (stderr, "\n"); - fprintf (stderr, " prch="); - for (resno = 0; resno < tccp->numresolutions; resno++) - { - fprintf (stderr, "%d ", tccp->prch[resno]); - } - fprintf (stderr, "\n"); - } - fprintf (stderr, " }\n"); + fprintf(stderr, "\n"); + fprintf(stderr, " prch="); + for (resno = 0; resno < tccp->numresolutions; resno++) { + fprintf(stderr, "%d ", tccp->prch[resno]); } - fprintf (stderr, " }\n"); + fprintf(stderr, "\n"); + } + fprintf(stderr, " }\n"); } - fprintf (stderr, "}\n"); + fprintf(stderr, " }\n"); + } + fprintf(stderr, "}\n"); } -void -j2k_write_soc () +void j2k_write_soc() { - cio_write (J2K_MS_SOC, 2); + cio_write(J2K_MS_SOC, 2); } -void -j2k_read_soc () +void j2k_read_soc() { j2k_state = J2K_STATE_MHSIZ; } -void -j2k_write_siz () +void j2k_write_siz() { int i; int lenp, len; - cio_write (J2K_MS_SIZ, 2); /* SIZ */ - lenp = cio_tell (); - cio_skip (2); - cio_write (0, 2); /* Rsiz (capabilities) */ - cio_write (j2k_img->x1, 4); /* Xsiz */ - cio_write (j2k_img->y1, 4); /* Ysiz */ - cio_write (j2k_img->x0, 4); /* X0siz */ - cio_write (j2k_img->y0, 4); /* Y0siz */ - cio_write (j2k_cp->tdx, 4); /* XTsiz */ - cio_write (j2k_cp->tdy, 4); /* YTsiz */ - cio_write (j2k_cp->tx0, 4); /* XT0siz */ - cio_write (j2k_cp->ty0, 4); /* YT0siz */ - cio_write (j2k_img->numcomps, 2); /* Csiz */ - for (i = 0; i < j2k_img->numcomps; i++) - { - cio_write (j2k_img->comps[i].prec - 1 + (j2k_img->comps[i].sgnd << 7), 1); /* Ssiz_i */ - cio_write (j2k_img->comps[i].dx, 1); /* XRsiz_i */ - cio_write (j2k_img->comps[i].dy, 1); /* YRsiz_i */ - } - len = cio_tell () - lenp; - cio_seek (lenp); - cio_write (len, 2); /* Lsiz */ - cio_seek (lenp + len); + cio_write(J2K_MS_SIZ, 2); /* SIZ */ + lenp = cio_tell(); + cio_skip(2); + cio_write(0, 2); /* Rsiz (capabilities) */ + cio_write(j2k_img->x1, 4); /* Xsiz */ + cio_write(j2k_img->y1, 4); /* Ysiz */ + cio_write(j2k_img->x0, 4); /* X0siz */ + cio_write(j2k_img->y0, 4); /* Y0siz */ + cio_write(j2k_cp->tdx, 4); /* XTsiz */ + cio_write(j2k_cp->tdy, 4); /* YTsiz */ + cio_write(j2k_cp->tx0, 4); /* XT0siz */ + cio_write(j2k_cp->ty0, 4); /* YT0siz */ + cio_write(j2k_img->numcomps, 2); /* Csiz */ + for (i = 0; i < j2k_img->numcomps; i++) { + cio_write(j2k_img->comps[i].prec - 1 + (j2k_img->comps[i].sgnd << 7), 1); /* Ssiz_i */ + cio_write(j2k_img->comps[i].dx, 1); /* XRsiz_i */ + cio_write(j2k_img->comps[i].dy, 1); /* YRsiz_i */ + } + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lsiz */ + cio_seek(lenp + len); } -void -j2k_read_siz () +void j2k_read_siz() { int len, i; - len = cio_read (2); /* Lsiz */ - cio_read (2); /* Rsiz (capabilities) */ - j2k_img->x1 = cio_read (4); /* Xsiz */ - j2k_img->y1 = cio_read (4); /* Ysiz */ - j2k_img->x0 = cio_read (4); /* X0siz */ - j2k_img->y0 = cio_read (4); /* Y0siz */ - j2k_cp->tdx = cio_read (4); /* XTsiz */ - j2k_cp->tdy = cio_read (4); /* YTsiz */ - j2k_cp->tx0 = cio_read (4); /* XT0siz */ - j2k_cp->ty0 = cio_read (4); /* YT0siz */ - - j2k_img->numcomps = cio_read (2); /* Csiz */ + len = cio_read(2); /* Lsiz */ + cio_read(2); /* Rsiz (capabilities) */ + j2k_img->x1 = cio_read(4); /* Xsiz */ + j2k_img->y1 = cio_read(4); /* Ysiz */ + j2k_img->x0 = cio_read(4); /* X0siz */ + j2k_img->y0 = cio_read(4); /* Y0siz */ + j2k_cp->tdx = cio_read(4); /* XTsiz */ + j2k_cp->tdy = cio_read(4); /* YTsiz */ + j2k_cp->tx0 = cio_read(4); /* XT0siz */ + j2k_cp->ty0 = cio_read(4); /* YT0siz */ + + j2k_img->numcomps = cio_read(2); /* Csiz */ j2k_img->comps = - (j2k_comp_t *) malloc (j2k_img->numcomps * sizeof (j2k_comp_t)); - for (i = 0; i < j2k_img->numcomps; i++) - { - int tmp, w, h; - tmp = cio_read (1); /* Ssiz_i */ - j2k_img->comps[i].prec = (tmp & 0x7f) + 1; - j2k_img->comps[i].sgnd = tmp >> 7; - j2k_img->comps[i].dx = cio_read (1); /* XRsiz_i */ - j2k_img->comps[i].dy = cio_read (1); /* YRsiz_i */ - w = int_ceildiv (j2k_img->x1 - j2k_img->x0, j2k_img->comps[i].dx); - h = int_ceildiv (j2k_img->y1 - j2k_img->y0, j2k_img->comps[i].dy); - j2k_img->comps[i].resno_decoded = 0; /* number of resolution decoded */ - j2k_img->comps[i].factor = 0; /* reducing factor by component */ - } - - j2k_cp->tw = int_ceildiv (j2k_img->x1 - j2k_cp->tx0, j2k_cp->tdx); - j2k_cp->th = int_ceildiv (j2k_img->y1 - j2k_cp->ty0, j2k_cp->tdy); + (j2k_comp_t *) malloc(j2k_img->numcomps * sizeof(j2k_comp_t)); + for (i = 0; i < j2k_img->numcomps; i++) { + int tmp, w, h; + tmp = cio_read(1); /* Ssiz_i */ + j2k_img->comps[i].prec = (tmp & 0x7f) + 1; + j2k_img->comps[i].sgnd = tmp >> 7; + j2k_img->comps[i].dx = cio_read(1); /* XRsiz_i */ + j2k_img->comps[i].dy = cio_read(1); /* YRsiz_i */ + w = int_ceildiv(j2k_img->x1 - j2k_img->x0, j2k_img->comps[i].dx); + h = int_ceildiv(j2k_img->y1 - j2k_img->y0, j2k_img->comps[i].dy); + j2k_img->comps[i].resno_decoded = 0; /* number of resolution decoded */ + j2k_img->comps[i].factor = 0; /* reducing factor by component */ + } + + j2k_cp->tw = int_ceildiv(j2k_img->x1 - j2k_cp->tx0, j2k_cp->tdx); + j2k_cp->th = int_ceildiv(j2k_img->y1 - j2k_cp->ty0, j2k_cp->tdy); j2k_cp->tcps = - (j2k_tcp_t *) calloc (j2k_cp->tw * j2k_cp->th, sizeof (j2k_tcp_t)); - j2k_cp->tileno = (int *) calloc (j2k_cp->tw * j2k_cp->th, sizeof (int)); + (j2k_tcp_t *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(j2k_tcp_t)); + j2k_cp->tileno = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int)); j2k_cp->tileno_size = 0; - for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) - { - j2k_cp->tcps[i].POC = 0; - j2k_cp->tcps[i].numpocs = 0; - j2k_cp->tcps[i].first = 1; - } + for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) { + j2k_cp->tcps[i].POC = 0; + j2k_cp->tcps[i].numpocs = 0; + j2k_cp->tcps[i].first = 1; + } /* Initialization for PPM marker */ j2k_cp->ppm = 0; @@ -291,55 +271,50 @@ j2k_read_siz () j2k_cp->ppm_store = 0; j2k_default_tcp.tccps = - (j2k_tccp_t *) calloc (sizeof (j2k_tccp_t), j2k_img->numcomps); - for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) - { - j2k_cp->tcps[i].tccps = - (j2k_tccp_t *) calloc (sizeof (j2k_tccp_t), j2k_img->numcomps); - } + (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps); + for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) { + j2k_cp->tcps[i].tccps = + (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps); + } j2k_tile_data = - (unsigned char **) calloc (j2k_cp->tw * j2k_cp->th, sizeof (char *)); - j2k_tile_len = (int *) calloc (j2k_cp->tw * j2k_cp->th, sizeof (int)); + (unsigned char **) calloc(j2k_cp->tw * j2k_cp->th, sizeof(char *)); + j2k_tile_len = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int)); j2k_state = J2K_STATE_MH; } -void -j2k_write_com () +void j2k_write_com() { unsigned int i; int lenp, len; char str[256]; - sprintf (str, "%s", j2k_cp->comment); - - cio_write (J2K_MS_COM, 2); - lenp = cio_tell (); - cio_skip (2); - cio_write (0, 2); - for (i = 0; i < strlen (str); i++) - { - cio_write (str[i], 1); - } - len = cio_tell () - lenp; - cio_seek (lenp); - cio_write (len, 2); - cio_seek (lenp + len); + sprintf(str, "%s", j2k_cp->comment); + + cio_write(J2K_MS_COM, 2); + lenp = cio_tell(); + cio_skip(2); + cio_write(0, 2); + for (i = 0; i < strlen(str); i++) { + cio_write(str[i], 1); + } + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); + cio_seek(lenp + len); } -void -j2k_read_com () +void j2k_read_com() { int len; - len = cio_read (2); - cio_skip (len - 2); + len = cio_read(2); + cio_skip(len - 2); } -void -j2k_write_cox (int compno) +void j2k_write_cox(int compno) { int i; j2k_tcp_t *tcp; @@ -347,23 +322,20 @@ j2k_write_cox (int compno) tcp = &j2k_cp->tcps[j2k_curtileno]; tccp = &tcp->tccps[compno]; - cio_write (tccp->numresolutions - 1, 1); /* SPcox (D) */ - cio_write (tccp->cblkw - 2, 1); /* SPcox (E) */ - cio_write (tccp->cblkh - 2, 1); /* SPcox (F) */ - cio_write (tccp->cblksty, 1); /* SPcox (G) */ - cio_write (tccp->qmfbid, 1); /* SPcox (H) */ - - if (tccp->csty & J2K_CCP_CSTY_PRT) - { - for (i = 0; i < tccp->numresolutions; i++) - { - cio_write (tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */ - } + cio_write(tccp->numresolutions - 1, 1); /* SPcox (D) */ + cio_write(tccp->cblkw - 2, 1); /* SPcox (E) */ + cio_write(tccp->cblkh - 2, 1); /* SPcox (F) */ + cio_write(tccp->cblksty, 1); /* SPcox (G) */ + cio_write(tccp->qmfbid, 1); /* SPcox (H) */ + + if (tccp->csty & J2K_CCP_CSTY_PRT) { + for (i = 0; i < tccp->numresolutions; i++) { + cio_write(tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */ } + } } -void -j2k_read_cox (int compno) +void j2k_read_cox(int compno) { int i; j2k_tcp_t *tcp; @@ -372,48 +344,44 @@ j2k_read_cox (int compno) j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; tccp = &tcp->tccps[compno]; - tccp->numresolutions = cio_read (1) + 1; /* SPcox (D) */ - tccp->cblkw = cio_read (1) + 2; /* SPcox (E) */ - tccp->cblkh = cio_read (1) + 2; /* SPcox (F) */ - tccp->cblksty = cio_read (1); /* SPcox (G) */ - tccp->qmfbid = cio_read (1); /* SPcox (H) */ - if (tccp->csty & J2K_CP_CSTY_PRT) - { - for (i = 0; i < tccp->numresolutions; i++) - { - int tmp = cio_read (1); /* SPcox (I_i) */ - tccp->prcw[i] = tmp & 0xf; - tccp->prch[i] = tmp >> 4; - } + tccp->numresolutions = cio_read(1) + 1; /* SPcox (D) */ + tccp->cblkw = cio_read(1) + 2; /* SPcox (E) */ + tccp->cblkh = cio_read(1) + 2; /* SPcox (F) */ + tccp->cblksty = cio_read(1); /* SPcox (G) */ + tccp->qmfbid = cio_read(1); /* SPcox (H) */ + if (tccp->csty & J2K_CP_CSTY_PRT) { + for (i = 0; i < tccp->numresolutions; i++) { + int tmp = cio_read(1); /* SPcox (I_i) */ + tccp->prcw[i] = tmp & 0xf; + tccp->prch[i] = tmp >> 4; } + } } -void -j2k_write_cod () +void j2k_write_cod() { j2k_tcp_t *tcp; int lenp, len; - cio_write (J2K_MS_COD, 2); /* COD */ + cio_write(J2K_MS_COD, 2); /* COD */ - lenp = cio_tell (); - cio_skip (2); + lenp = cio_tell(); + cio_skip(2); tcp = &j2k_cp->tcps[j2k_curtileno]; - cio_write (tcp->csty, 1); /* Scod */ - cio_write (tcp->prg, 1); /* SGcod (A) */ - cio_write (tcp->numlayers, 2); /* SGcod (B) */ - cio_write (tcp->mct, 1); /* SGcod (C) */ - - j2k_write_cox (0); - len = cio_tell () - lenp; - cio_seek (lenp); - cio_write (len, 2); /* Lcod */ - cio_seek (lenp + len); + cio_write(tcp->csty, 1); /* Scod */ + cio_write(tcp->prg, 1); /* SGcod (A) */ + cio_write(tcp->numlayers, 2); /* SGcod (B) */ + cio_write(tcp->mct, 1); /* SGcod (C) */ + + j2k_write_cox(0); + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lcod */ + cio_seek(lenp + len); } -void -j2k_read_cod () +void j2k_read_cod() { int len, i, pos; j2k_tcp_t *tcp; @@ -421,42 +389,39 @@ j2k_read_cod () tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - len = cio_read (2); /* Lcod */ - tcp->csty = cio_read (1); /* Scod */ - tcp->prg = cio_read (1); /* SGcod (A) */ - tcp->numlayers = cio_read (2); /* SGcod (B) */ - tcp->mct = cio_read (1); /* SGcod (C) */ - - pos = cio_tell (); - for (i = 0; i < j2k_img->numcomps; i++) - { - tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT; - cio_seek (pos); - j2k_read_cox (i); - } + len = cio_read(2); /* Lcod */ + tcp->csty = cio_read(1); /* Scod */ + tcp->prg = cio_read(1); /* SGcod (A) */ + tcp->numlayers = cio_read(2); /* SGcod (B) */ + tcp->mct = cio_read(1); /* SGcod (C) */ + + pos = cio_tell(); + for (i = 0; i < j2k_img->numcomps; i++) { + tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT; + cio_seek(pos); + j2k_read_cox(i); + } } -void -j2k_write_coc (int compno) +void j2k_write_coc(int compno) { j2k_tcp_t *tcp; int lenp, len; - cio_write (J2K_MS_COC, 2); /* COC */ - lenp = cio_tell (); - cio_skip (2); + cio_write(J2K_MS_COC, 2); /* COC */ + lenp = cio_tell(); + cio_skip(2); tcp = &j2k_cp->tcps[j2k_curtileno]; - cio_write (compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */ - cio_write (tcp->tccps[compno].csty, 1); /* Scoc */ - j2k_write_cox (compno); - len = cio_tell () - lenp; - cio_seek (lenp); - cio_write (len, 2); /* Lcoc */ - cio_seek (lenp + len); + cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */ + cio_write(tcp->tccps[compno].csty, 1); /* Scoc */ + j2k_write_cox(compno); + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lcoc */ + cio_seek(lenp + len); } -void -j2k_read_coc () +void j2k_read_coc() { int len, compno; j2k_tcp_t *tcp; @@ -464,14 +429,13 @@ j2k_read_coc () tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - len = cio_read (2); /* Lcoc */ - compno = cio_read (j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */ - tcp->tccps[compno].csty = cio_read (1); /* Scoc */ - j2k_read_cox (compno); + len = cio_read(2); /* Lcoc */ + compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */ + tcp->tccps[compno].csty = cio_read(1); /* Scoc */ + j2k_read_cox(compno); } -void -j2k_write_qcx (int compno) +void j2k_write_qcx(int compno) { j2k_tcp_t *tcp; j2k_tccp_t *tccp; @@ -481,29 +445,25 @@ j2k_write_qcx (int compno) tcp = &j2k_cp->tcps[j2k_curtileno]; tccp = &tcp->tccps[compno]; - cio_write (tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */ + cio_write(tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */ numbands = - tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; + tccp->qntsty == + J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; - for (bandno = 0; bandno < numbands; bandno++) - { - expn = tccp->stepsizes[bandno].expn; - mant = tccp->stepsizes[bandno].mant; + for (bandno = 0; bandno < numbands; bandno++) { + expn = tccp->stepsizes[bandno].expn; + mant = tccp->stepsizes[bandno].mant; - if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) - { - cio_write (expn << 3, 1); /* SPqcx_i */ - } - else - { - cio_write ((expn << 11) + mant, 2); /* SPqcx_i */ - } + if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { + cio_write(expn << 3, 1); /* SPqcx_i */ + } else { + cio_write((expn << 11) + mant, 2); /* SPqcx_i */ } + } } -void -j2k_read_qcx (int compno, int len) +void j2k_read_qcx(int compno, int len) { int tmp; j2k_tcp_t *tcp; @@ -514,30 +474,26 @@ j2k_read_qcx (int compno, int len) j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; tccp = &tcp->tccps[compno]; - tmp = cio_read (1); /* Sqcx */ + tmp = cio_read(1); /* Sqcx */ tccp->qntsty = tmp & 0x1f; tccp->numgbits = tmp >> 5; numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT ? len - 1 : (len - 1) / 2); - for (bandno = 0; bandno < numbands; bandno++) - { - int expn, mant; - if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) - { - expn = cio_read (1) >> 3; /* SPqcx_i */ - mant = 0; - } - else - { - tmp = cio_read (2); /* SPqcx_i */ - expn = tmp >> 11; - mant = tmp & 0x7ff; - } - tccp->stepsizes[bandno].expn = expn; - tccp->stepsizes[bandno].mant = mant; + for (bandno = 0; bandno < numbands; bandno++) { + int expn, mant; + if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { + expn = cio_read(1) >> 3; /* SPqcx_i */ + mant = 0; + } else { + tmp = cio_read(2); /* SPqcx_i */ + expn = tmp >> 11; + mant = tmp & 0x7ff; } + tccp->stepsizes[bandno].expn = expn; + tccp->stepsizes[bandno].mant = mant; + } @@ -545,84 +501,76 @@ j2k_read_qcx (int compno, int len) - if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) - { + if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { - for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) - { + for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) { - tccp->stepsizes[bandno].expn = - ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > - 0) ? (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0; + tccp->stepsizes[bandno].expn = + ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > + 0) ? (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0; - tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant; - - } + tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant; } + } + /* ddA */ } -void -j2k_write_qcd () +void j2k_write_qcd() { int lenp, len; - cio_write (J2K_MS_QCD, 2); /* QCD */ - lenp = cio_tell (); - cio_skip (2); - j2k_write_qcx (0); - len = cio_tell () - lenp; - cio_seek (lenp); - cio_write (len, 2); /* Lqcd */ - cio_seek (lenp + len); + cio_write(J2K_MS_QCD, 2); /* QCD */ + lenp = cio_tell(); + cio_skip(2); + j2k_write_qcx(0); + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lqcd */ + cio_seek(lenp + len); } -void -j2k_read_qcd () +void j2k_read_qcd() { int len, i, pos; - len = cio_read (2); /* Lqcd */ - pos = cio_tell (); - for (i = 0; i < j2k_img->numcomps; i++) - { - cio_seek (pos); - j2k_read_qcx (i, len - 2); - } + len = cio_read(2); /* Lqcd */ + pos = cio_tell(); + for (i = 0; i < j2k_img->numcomps; i++) { + cio_seek(pos); + j2k_read_qcx(i, len - 2); + } } -void -j2k_write_qcc (int compno) +void j2k_write_qcc(int compno) { int lenp, len; - cio_write (J2K_MS_QCC, 2); /* QCC */ - lenp = cio_tell (); - cio_skip (2); - cio_write (compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */ - j2k_write_qcx (compno); - len = cio_tell () - lenp; - cio_seek (lenp); - cio_write (len, 2); /* Lqcc */ - cio_seek (lenp + len); + cio_write(J2K_MS_QCC, 2); /* QCC */ + lenp = cio_tell(); + cio_skip(2); + cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */ + j2k_write_qcx(compno); + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lqcc */ + cio_seek(lenp + len); } -void -j2k_read_qcc () +void j2k_read_qcc() { int len, compno; - len = cio_read (2); /* Lqcc */ - compno = cio_read (j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */ - j2k_read_qcx (compno, len - 2 - (j2k_img->numcomps <= 256 ? 1 : 2)); + len = cio_read(2); /* Lqcc */ + compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */ + j2k_read_qcx(compno, len - 2 - (j2k_img->numcomps <= 256 ? 1 : 2)); } -void -j2k_write_poc () +void j2k_write_poc() { int len, numpchgs, i; j2k_tcp_t *tcp; @@ -631,28 +579,26 @@ j2k_write_poc () tcp = &j2k_cp->tcps[j2k_curtileno]; tccp = &tcp->tccps[0]; numpchgs = tcp->numpocs; - cio_write (J2K_MS_POC, 2); /* POC */ + cio_write(J2K_MS_POC, 2); /* POC */ len = 2 + (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2)) * numpchgs; - cio_write (len, 2); /* Lpoc */ - for (i = 0; i < numpchgs; i++) - { - // MODIF - j2k_poc_t *poc; - poc = &tcp->pocs[i]; - cio_write (poc->resno0, 1); /* RSpoc_i */ - cio_write (poc->compno0, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CSpoc_i */ - cio_write (poc->layno1, 2); /* LYEpoc_i */ - poc->layno1 = int_min (poc->layno1, tcp->numlayers); - cio_write (poc->resno1, 1); /* REpoc_i */ - poc->resno1 = int_min (poc->resno1, tccp->numresolutions); - cio_write (poc->compno1, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CEpoc_i */ - poc->compno1 = int_min (poc->compno1, j2k_img->numcomps); - cio_write (poc->prg, 1); /* Ppoc_i */ - } + cio_write(len, 2); /* Lpoc */ + for (i = 0; i < numpchgs; i++) { + // MODIF + j2k_poc_t *poc; + poc = &tcp->pocs[i]; + cio_write(poc->resno0, 1); /* RSpoc_i */ + cio_write(poc->compno0, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CSpoc_i */ + cio_write(poc->layno1, 2); /* LYEpoc_i */ + poc->layno1 = int_min(poc->layno1, tcp->numlayers); + cio_write(poc->resno1, 1); /* REpoc_i */ + poc->resno1 = int_min(poc->resno1, tccp->numresolutions); + cio_write(poc->compno1, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CEpoc_i */ + poc->compno1 = int_min(poc->compno1, j2k_img->numcomps); + cio_write(poc->prg, 1); /* Ppoc_i */ + } } -void -j2k_read_poc () +void j2k_read_poc() { int len, numpchgs, i, old_poc; j2k_tcp_t *tcp; @@ -665,346 +611,306 @@ j2k_read_poc () old_poc = tcp->POC ? tcp->numpocs + 1 : 0; tcp->POC = 1; tccp = &tcp->tccps[0]; - len = cio_read (2); /* Lpoc */ + len = cio_read(2); /* Lpoc */ numpchgs = (len - 2) / (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2)); - for (i = old_poc; i < numpchgs + old_poc; i++) - { - j2k_poc_t *poc; - poc = &tcp->pocs[i]; - poc->resno0 = cio_read (1); /* RSpoc_i */ - poc->compno0 = cio_read (j2k_img->numcomps <= 256 ? 1 : 2); /* CSpoc_i */ - poc->layno1 = int_min (cio_read (2), tcp->numlayers); /* LYEpoc_i */ - poc->resno1 = int_min (cio_read (1), tccp->numresolutions); /* REpoc_i */ - poc->compno1 = int_min (cio_read (j2k_img->numcomps <= 256 ? 1 : 2), j2k_img->numcomps); /* CEpoc_i */ - poc->prg = cio_read (1); /* Ppoc_i */ - } + for (i = old_poc; i < numpchgs + old_poc; i++) { + j2k_poc_t *poc; + poc = &tcp->pocs[i]; + poc->resno0 = cio_read(1); /* RSpoc_i */ + poc->compno0 = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* CSpoc_i */ + poc->layno1 = int_min(cio_read(2), tcp->numlayers); /* LYEpoc_i */ + poc->resno1 = int_min(cio_read(1), tccp->numresolutions); /* REpoc_i */ + poc->compno1 = int_min(cio_read(j2k_img->numcomps <= 256 ? 1 : 2), j2k_img->numcomps); /* CEpoc_i */ + poc->prg = cio_read(1); /* Ppoc_i */ + } tcp->numpocs = numpchgs + old_poc - 1; } -void -j2k_read_crg () +void j2k_read_crg() { int len, i, Xcrg_i, Ycrg_i; - len = cio_read (2); /* Lcrg */ - for (i = 0; i < j2k_img->numcomps; i++) - { - Xcrg_i = cio_read (2); /* Xcrg_i */ - Ycrg_i = cio_read (2); /* Ycrg_i */ - } + len = cio_read(2); /* Lcrg */ + for (i = 0; i < j2k_img->numcomps; i++) { + Xcrg_i = cio_read(2); /* Xcrg_i */ + Ycrg_i = cio_read(2); /* Ycrg_i */ + } } -void -j2k_read_tlm () +void j2k_read_tlm() { int len, Ztlm, Stlm, ST, SP, tile_tlm, i; long int Ttlm_i, Ptlm_i; - len = cio_read (2); /* Ltlm */ - Ztlm = cio_read (1); /* Ztlm */ - Stlm = cio_read (1); /* Stlm */ + len = cio_read(2); /* Ltlm */ + Ztlm = cio_read(1); /* Ztlm */ + Stlm = cio_read(1); /* Stlm */ ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02); SP = (Stlm >> 6) & 0x01; tile_tlm = (len - 4) / ((SP + 1) * 2 + ST); - for (i = 0; i < tile_tlm; i++) - { - Ttlm_i = cio_read (ST); /* Ttlm_i */ - Ptlm_i = cio_read (SP ? 4 : 2); /* Ptlm_i */ - } + for (i = 0; i < tile_tlm; i++) { + Ttlm_i = cio_read(ST); /* Ttlm_i */ + Ptlm_i = cio_read(SP ? 4 : 2); /* Ptlm_i */ + } } -void -j2k_read_plm () +void j2k_read_plm() { int len, i, Zplm, Nplm, add, packet_len = 0; - len = cio_read (2); /* Lplm */ - Zplm = cio_read (1); /* Zplm */ + len = cio_read(2); /* Lplm */ + Zplm = cio_read(1); /* Zplm */ len -= 3; - while (len > 0) - { - Nplm = cio_read (4); /* Nplm */ - len -= 4; - for (i = Nplm; i > 0; i--) - { - add = cio_read (1); - len--; - packet_len = (packet_len << 7) + add; /* Iplm_ij */ - if ((add & 0x80) == 0) - { - /* New packet */ - packet_len = 0; - } - if (len <= 0) - break; - } + while (len > 0) { + Nplm = cio_read(4); /* Nplm */ + len -= 4; + for (i = Nplm; i > 0; i--) { + add = cio_read(1); + len--; + packet_len = (packet_len << 7) + add; /* Iplm_ij */ + if ((add & 0x80) == 0) { + /* New packet */ + packet_len = 0; + } + if (len <= 0) + break; } + } } -void -j2k_read_plt () +void j2k_read_plt() { int len, i, Zplt, packet_len = 0, add; - len = cio_read (2); /* Lplt */ - Zplt = cio_read (1); /* Zplt */ - for (i = len - 3; i > 0; i--) - { - add = cio_read (1); - packet_len = (packet_len << 7) + add; /* Iplt_i */ - if ((add & 0x80) == 0) - { - /* New packet */ - packet_len = 0; - } + len = cio_read(2); /* Lplt */ + Zplt = cio_read(1); /* Zplt */ + for (i = len - 3; i > 0; i--) { + add = cio_read(1); + packet_len = (packet_len << 7) + add; /* Iplt_i */ + if ((add & 0x80) == 0) { + /* New packet */ + packet_len = 0; } + } } -void -j2k_read_ppm () +void j2k_read_ppm() { int len, Z_ppm, i, j; int N_ppm; - len = cio_read (2); + len = cio_read(2); j2k_cp->ppm = 1; - Z_ppm = cio_read (1); /* Z_ppm */ + Z_ppm = cio_read(1); /* Z_ppm */ len -= 3; - while (len > 0) - { - if (j2k_cp->ppm_previous == 0) - { - N_ppm = cio_read (4); /* N_ppm */ - len -= 4; - } - else - { - N_ppm = j2k_cp->ppm_previous; - } - - j = j2k_cp->ppm_store; - if (Z_ppm == 0) - { /* First PPM marker */ - j2k_cp->ppm_data = - (unsigned char *) calloc (N_ppm, sizeof (unsigned char)); + while (len > 0) { + if (j2k_cp->ppm_previous == 0) { + N_ppm = cio_read(4); /* N_ppm */ + len -= 4; + } else { + N_ppm = j2k_cp->ppm_previous; + } - j2k_cp->ppm_len = N_ppm; //Add antonin : ppmbug1 + j = j2k_cp->ppm_store; + if (Z_ppm == 0) { /* First PPM marker */ + j2k_cp->ppm_data = + (unsigned char *) calloc(N_ppm, sizeof(unsigned char)); - } - else - { /* NON-first PPM marker */ - j2k_cp->ppm_data = - (unsigned char *) realloc (j2k_cp->ppm_data, - (N_ppm + - j2k_cp->ppm_store) * - sizeof (unsigned char)); + j2k_cp->ppm_len = N_ppm; //Add antonin : ppmbug1 - j2k_cp->ppm_len = N_ppm + j2k_cp->ppm_store; //Add antonin : ppmbug1 + } else { /* NON-first PPM marker */ + j2k_cp->ppm_data = + (unsigned char *) realloc(j2k_cp->ppm_data, + (N_ppm + + j2k_cp->ppm_store) * + sizeof(unsigned char)); - } + j2k_cp->ppm_len = N_ppm + j2k_cp->ppm_store; //Add antonin : ppmbug1 - for (i = N_ppm; i > 0; i--) - { /* Read packet header */ - j2k_cp->ppm_data[j] = cio_read (1); - j++; - len--; - if (len == 0) - break; /* Case of non-finished packet header in present marker but finished in next one */ - } + } - j2k_cp->ppm_previous = i - 1; - j2k_cp->ppm_store = j; + for (i = N_ppm; i > 0; i--) { /* Read packet header */ + j2k_cp->ppm_data[j] = cio_read(1); + j++; + len--; + if (len == 0) + break; /* Case of non-finished packet header in present marker but finished in next one */ } + + j2k_cp->ppm_previous = i - 1; + j2k_cp->ppm_store = j; + } } -void -j2k_read_ppt () +void j2k_read_ppt() { int len, Z_ppt, i, j = 0; j2k_tcp_t *tcp; - len = cio_read (2); - Z_ppt = cio_read (1); + len = cio_read(2); + Z_ppt = cio_read(1); tcp = &j2k_cp->tcps[j2k_curtileno]; tcp->ppt = 1; - if (Z_ppt == 0) - { /* First PPT marker */ - tcp->ppt_data = - (unsigned char *) calloc (len - 3, sizeof (unsigned char)); - tcp->ppt_store = 0; + if (Z_ppt == 0) { /* First PPT marker */ + tcp->ppt_data = + (unsigned char *) calloc(len - 3, sizeof(unsigned char)); + tcp->ppt_store = 0; - tcp->ppt_len = len - 3; //Add antonin : ppmbug1 - } - else - { /* NON-first PPT marker */ - tcp->ppt_data = - (unsigned char *) realloc (tcp->ppt_data, - (len - 3 + - tcp->ppt_store) * sizeof (unsigned char)); + tcp->ppt_len = len - 3; //Add antonin : ppmbug1 + } else { /* NON-first PPT marker */ + tcp->ppt_data = + (unsigned char *) realloc(tcp->ppt_data, + (len - 3 + + tcp->ppt_store) * sizeof(unsigned char)); - tcp->ppt_len = len - 3 + tcp->ppt_store; //Add antonin : ppmbug1 + tcp->ppt_len = len - 3 + tcp->ppt_store; //Add antonin : ppmbug1 - } + } j = tcp->ppt_store; - for (i = len - 3; i > 0; i--) - { - tcp->ppt_data[j] = cio_read (1); - j++; - } + for (i = len - 3; i > 0; i--) { + tcp->ppt_data[j] = cio_read(1); + j++; + } tcp->ppt_store = j; } -void -j2k_write_sot () +void j2k_write_sot() { int lenp, len; - j2k_sot_start = cio_tell (); - cio_write (J2K_MS_SOT, 2); /* SOT */ - lenp = cio_tell (); - cio_skip (2); /* Lsot (further) */ - cio_write (j2k_curtileno, 2); /* Isot */ - cio_skip (4); /* Psot (further in j2k_write_sod) */ - cio_write (0, 1); /* TPsot */ - cio_write (1, 1); /* TNsot */ - len = cio_tell () - lenp; - cio_seek (lenp); - cio_write (len, 2); /* Lsot */ - cio_seek (lenp + len); + j2k_sot_start = cio_tell(); + cio_write(J2K_MS_SOT, 2); /* SOT */ + lenp = cio_tell(); + cio_skip(2); /* Lsot (further) */ + cio_write(j2k_curtileno, 2); /* Isot */ + cio_skip(4); /* Psot (further in j2k_write_sod) */ + cio_write(0, 1); /* TPsot */ + cio_write(1, 1); /* TNsot */ + len = cio_tell() - lenp; + cio_seek(lenp); + cio_write(len, 2); /* Lsot */ + cio_seek(lenp + len); } -void -j2k_read_sot () +void j2k_read_sot() { int len, tileno, totlen, partno, numparts, i; j2k_tcp_t *tcp; j2k_tccp_t *tmp; char status = 0; - len = cio_read (2); - tileno = cio_read (2); - - if (j2k_cp->tileno_size == 0) - { + len = cio_read(2); + tileno = cio_read(2); + + if (j2k_cp->tileno_size == 0) { + j2k_cp->tileno[j2k_cp->tileno_size] = tileno; + j2k_cp->tileno_size++; + } else { + i = 0; + while (i < j2k_cp->tileno_size && status == 0) { + status = j2k_cp->tileno[i] == tileno ? 1 : 0; + i++; + } + if (status == 0) { j2k_cp->tileno[j2k_cp->tileno_size] = tileno; j2k_cp->tileno_size++; } - else - { - i = 0; - while (i < j2k_cp->tileno_size && status == 0) - { - status = j2k_cp->tileno[i] == tileno ? 1 : 0; - i++; - } - if (status == 0) - { - j2k_cp->tileno[j2k_cp->tileno_size] = tileno; - j2k_cp->tileno_size++; - } - } + } - totlen = cio_read (4); + totlen = cio_read(4); if (!totlen) - totlen = cio_numbytesleft () + 8; + totlen = cio_numbytesleft() + 8; - partno = cio_read (1); - numparts = cio_read (1); + partno = cio_read(1); + numparts = cio_read(1); j2k_curtileno = tileno; - j2k_eot = cio_getbp () - 12 + totlen; + j2k_eot = cio_getbp() - 12 + totlen; j2k_state = J2K_STATE_TPH; tcp = &j2k_cp->tcps[j2k_curtileno]; - if (tcp->first == 1) - { - tmp = tcp->tccps; - *tcp = j2k_default_tcp; + if (tcp->first == 1) { + tmp = tcp->tccps; + *tcp = j2k_default_tcp; - /* Initialization PPT */ - tcp->ppt = 0; - tcp->ppt_data = NULL; + /* Initialization PPT */ + tcp->ppt = 0; + tcp->ppt_data = NULL; - tcp->tccps = tmp; - for (i = 0; i < j2k_img->numcomps; i++) - { - tcp->tccps[i] = j2k_default_tcp.tccps[i]; - } - j2k_cp->tcps[j2k_curtileno].first = 0; + tcp->tccps = tmp; + for (i = 0; i < j2k_img->numcomps; i++) { + tcp->tccps[i] = j2k_default_tcp.tccps[i]; } + j2k_cp->tcps[j2k_curtileno].first = 0; + } } -void -j2k_write_sod () +void j2k_write_sod() { int l, layno; int totlen; j2k_tcp_t *tcp; static int j2k_sod_start; - cio_write (J2K_MS_SOD, 2); - if (j2k_curtileno == 0) - { - j2k_sod_start = cio_tell () + pos_correction; - } + cio_write(J2K_MS_SOD, 2); + if (j2k_curtileno == 0) { + j2k_sod_start = cio_tell() + pos_correction; + } /* INDEX >> */ - if (info_IM.index_on) - { - info_IM.tile[j2k_curtileno].end_header = - cio_tell () + pos_correction - 1; - info_IM.tile[j2k_curtileno].packet = - (info_packet *) calloc (info_IM.Comp * info_IM.Layer * - (info_IM.Decomposition + 1) * 100, - sizeof (info_packet)); - } + if (info_IM.index_on) { + info_IM.tile[j2k_curtileno].end_header = + cio_tell() + pos_correction - 1; + info_IM.tile[j2k_curtileno].packet = + (info_packet *) calloc(info_IM.Comp * info_IM.Layer * + (info_IM.Decomposition + 1) * 100, + sizeof(info_packet)); + } /* << INDEX */ tcp = &j2k_cp->tcps[j2k_curtileno]; - for (layno = 0; layno < tcp->numlayers; layno++) - { - tcp->rates[layno] -= tcp->rates[layno] ? (j2k_sod_start / (j2k_cp->th * j2k_cp->tw)) : 0; //Mod antonin losslessbug - } + for (layno = 0; layno < tcp->numlayers; layno++) { + tcp->rates[layno] -= tcp->rates[layno] ? (j2k_sod_start / (j2k_cp->th * j2k_cp->tw)) : 0; //Mod antonin losslessbug + } info_IM.num = 0; if (j2k_cp->image_type) - l = tcd_encode_tile_pxm (j2k_curtileno, cio_getbp (), - cio_numbytesleft () - 2, &info_IM); + l = tcd_encode_tile_pxm(j2k_curtileno, cio_getbp(), + cio_numbytesleft() - 2, &info_IM); else - l = tcd_encode_tile_pgx (j2k_curtileno, cio_getbp (), - cio_numbytesleft () - 2, &info_IM); + l = tcd_encode_tile_pgx(j2k_curtileno, cio_getbp(), + cio_numbytesleft() - 2, &info_IM); /* Writing Psot in SOT marker */ - totlen = cio_tell () + l - j2k_sot_start; - cio_seek (j2k_sot_start + 6); - cio_write (totlen, 4); - cio_seek (j2k_sot_start + totlen); + totlen = cio_tell() + l - j2k_sot_start; + cio_seek(j2k_sot_start + 6); + cio_write(totlen, 4); + cio_seek(j2k_sot_start + totlen); } -void -j2k_read_sod () +void j2k_read_sod() { int len, truncate = 0, i; unsigned char *data; - len = int_min (j2k_eot - cio_getbp (), cio_numbytesleft () + 1); - if (len == cio_numbytesleft () + 1) + len = int_min(j2k_eot - cio_getbp(), cio_numbytesleft() + 1); + if (len == cio_numbytesleft() + 1) truncate = 1; /* Case of a truncate codestream */ data = - (unsigned char *) malloc ((j2k_tile_len[j2k_curtileno] + len) * - sizeof (unsigned char)); + (unsigned char *) malloc((j2k_tile_len[j2k_curtileno] + len) * + sizeof(unsigned char)); for (i = 0; i < j2k_tile_len[j2k_curtileno]; i++) data[i] = j2k_tile_data[j2k_curtileno][i]; for (i = 0; i < len; i++) - data[i + j2k_tile_len[j2k_curtileno]] = cio_read (1); + data[i + j2k_tile_len[j2k_curtileno]] = cio_read(1); j2k_tile_len[j2k_curtileno] += len; - free (j2k_tile_data[j2k_curtileno]); + free(j2k_tile_data[j2k_curtileno]); j2k_tile_data[j2k_curtileno] = data; data = NULL; @@ -1014,20 +920,18 @@ j2k_read_sod () j2k_state = J2K_STATE_NEOC; /* RAJOUTE !! */ } -void -j2k_write_rgn (int compno, int tileno) +void j2k_write_rgn(int compno, int tileno) { j2k_tcp_t *tcp = &j2k_cp->tcps[tileno]; - cio_write (J2K_MS_RGN, 2); /* RGN */ - cio_write (j2k_img->numcomps <= 256 ? 5 : 6, 2); /* Lrgn */ - cio_write (compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */ - cio_write (0, 1); /* Srgn */ - cio_write (tcp->tccps[compno].roishift, 1); /* SPrgn */ + cio_write(J2K_MS_RGN, 2); /* RGN */ + cio_write(j2k_img->numcomps <= 256 ? 5 : 6, 2); /* Lrgn */ + cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */ + cio_write(0, 1); /* Srgn */ + cio_write(tcp->tccps[compno].roishift, 1); /* SPrgn */ } -void -j2k_read_rgn () +void j2k_read_rgn() { int len, compno, roisty; j2k_tcp_t *tcp; @@ -1035,68 +939,61 @@ j2k_read_rgn () tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - len = cio_read (2); /* Lrgn */ - compno = cio_read (j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */ - roisty = cio_read (1); /* Srgn */ - tcp->tccps[compno].roishift = cio_read (1); /* SPrgn */ + len = cio_read(2); /* Lrgn */ + compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */ + roisty = cio_read(1); /* Srgn */ + tcp->tccps[compno].roishift = cio_read(1); /* SPrgn */ } -void -j2k_write_eoc () +void j2k_write_eoc() { /* fprintf(stderr, "%.8x: EOC\n", cio_tell() + pos_correction); */ - cio_write (J2K_MS_EOC, 2); + cio_write(J2K_MS_EOC, 2); } -void -j2k_read_eoc () +void j2k_read_eoc() { int i, tileno; - tcd_init (j2k_img, j2k_cp); + tcd_init(j2k_img, j2k_cp); - for (i = 0; i < j2k_cp->tileno_size; i++) - { - tileno = j2k_cp->tileno[i]; - tcd_decode_tile (j2k_tile_data[tileno], j2k_tile_len[tileno], tileno); - free (j2k_tile_data[tileno]); - } + for (i = 0; i < j2k_cp->tileno_size; i++) { + tileno = j2k_cp->tileno[i]; + tcd_decode_tile(j2k_tile_data[tileno], j2k_tile_len[tileno], tileno); + free(j2k_tile_data[tileno]); + } j2k_state = J2K_STATE_MT; - longjmp (j2k_error, 1); + longjmp(j2k_error, 1); } -void -j2k_read_unk () +void j2k_read_unk() { - fprintf (stderr, "warning: unknown marker\n"); + fprintf(stderr, "warning: unknown marker\n"); } LIBJ2K_API int -j2k_encode (j2k_image_t * img, j2k_cp_t * cp, char *output, - int len, char *index) +j2k_encode(j2k_image_t * img, j2k_cp_t * cp, char *output, + int len, char *index) { int tileno, compno, layno, resno, precno, pack_nb; char *dest = NULL; FILE *INDEX = NULL; FILE *f = NULL; - if (setjmp (j2k_error)) - { - return 0; - } + if (setjmp(j2k_error)) { + return 0; + } - if (cp->intermed_file == 1) - { - f = fopen (output, "wb"); - if (!f) - { - fprintf (stderr, "failed to open %s for writing\n", output); - return 1; - } - dest = (char *) malloc (len); - cio_init (dest, len); + if (cp->intermed_file == 1) { + f = fopen(output, "wb"); + if (!f) { + fprintf(stderr, "failed to open %s for writing\n", output); + return 1; } + dest = (char *) malloc(len); + cio_init(dest, len); + } j2k_img = img; j2k_cp = cp; @@ -1104,475 +1001,401 @@ j2k_encode (j2k_image_t * img, j2k_cp_t * cp, char *output, /* INDEX >> */ info_IM.index_on = j2k_cp->index_on; - if (info_IM.index_on) - { - info_IM.tile = - (info_tile *) malloc (j2k_cp->tw * j2k_cp->th * sizeof (info_tile)); - info_IM.Im_w = j2k_img->x1 - j2k_img->x0; - info_IM.Im_h = j2k_img->y1 - j2k_img->y0; - info_IM.Prog = (&j2k_cp->tcps[0])->prg; - info_IM.tw = j2k_cp->tw; - info_IM.th = j2k_cp->th; - info_IM.Tile_x = j2k_cp->tdx; /* new version parser */ - info_IM.Tile_y = j2k_cp->tdy; /* new version parser */ - info_IM.Comp = j2k_img->numcomps; - info_IM.Layer = (&j2k_cp->tcps[0])->numlayers; - info_IM.Decomposition = (&j2k_cp->tcps[0])->tccps->numresolutions - 1; - info_IM.D_max = 0; /* ADD Marcela */ - } + if (info_IM.index_on) { + info_IM.tile = + (info_tile *) malloc(j2k_cp->tw * j2k_cp->th * sizeof(info_tile)); + info_IM.Im_w = j2k_img->x1 - j2k_img->x0; + info_IM.Im_h = j2k_img->y1 - j2k_img->y0; + info_IM.Prog = (&j2k_cp->tcps[0])->prg; + info_IM.tw = j2k_cp->tw; + info_IM.th = j2k_cp->th; + info_IM.Tile_x = j2k_cp->tdx; /* new version parser */ + info_IM.Tile_y = j2k_cp->tdy; /* new version parser */ + info_IM.Comp = j2k_img->numcomps; + info_IM.Layer = (&j2k_cp->tcps[0])->numlayers; + info_IM.Decomposition = (&j2k_cp->tcps[0])->tccps->numresolutions - 1; + info_IM.D_max = 0; /* ADD Marcela */ + } /* << INDEX */ - j2k_write_soc (); - j2k_write_siz (); - j2k_write_cod (); - j2k_write_qcd (); - for (compno = 0; compno < j2k_img->numcomps; compno++) - { - j2k_tcp_t *tcp = &j2k_cp->tcps[0]; - if (tcp->tccps[compno].roishift) - j2k_write_rgn (compno, 0); - } + j2k_write_soc(); + j2k_write_siz(); + j2k_write_cod(); + j2k_write_qcd(); + for (compno = 0; compno < j2k_img->numcomps; compno++) { + j2k_tcp_t *tcp = &j2k_cp->tcps[0]; + if (tcp->tccps[compno].roishift) + j2k_write_rgn(compno, 0); + } if (j2k_cp->comment != NULL) - j2k_write_com (); + j2k_write_com(); - if (cp->intermed_file == 1) - { - /* Writing the main header */ - pos_correction = cio_tell (); - fwrite (dest, 1, cio_tell (), f); - } + if (cp->intermed_file == 1) { + /* Writing the main header */ + pos_correction = cio_tell(); + fwrite(dest, 1, cio_tell(), f); + } /* INDEX >> */ - if (info_IM.index_on) - { - info_IM.Main_head_end = cio_tell () - 1; - } + if (info_IM.index_on) { + info_IM.Main_head_end = cio_tell() - 1; + } /* << INDEX */ - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) - { - fprintf (stderr, "Tile number %d / %d ", tileno + 1, cp->tw * cp->th); - - if (cp->intermed_file == 1) - { - /* new dest for each tile */ - free (dest); - dest = (char *) malloc (len); - cio_init (dest, len); - } - j2k_curtileno = tileno; - /* initialisation before tile encoding */ + for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { + fprintf(stderr, "Tile number %d / %d ", tileno + 1, cp->tw * cp->th); - if (tileno == 0) - { - tcd_malloc_encode (j2k_img, j2k_cp, j2k_curtileno); - } - else - { - tcd_init_encode (j2k_img, j2k_cp, j2k_curtileno); - } - - /* INDEX >> */ - if (info_IM.index_on) - { - info_IM.tile[j2k_curtileno].num_tile = j2k_curtileno; - info_IM.tile[j2k_curtileno].start_pos = - cio_tell () + pos_correction; - } - /* << INDEX */ - j2k_write_sot (); - - for (compno = 1; compno < img->numcomps; compno++) - { - j2k_write_coc (compno); - j2k_write_qcc (compno); - } + if (cp->intermed_file == 1) { + /* new dest for each tile */ + free(dest); + dest = (char *) malloc(len); + cio_init(dest, len); + } + j2k_curtileno = tileno; + /* initialisation before tile encoding */ - if (cp->tcps[tileno].numpocs) - j2k_write_poc (); - j2k_write_sod (); + if (tileno == 0) { + tcd_malloc_encode(j2k_img, j2k_cp, j2k_curtileno); + } else { + tcd_init_encode(j2k_img, j2k_cp, j2k_curtileno); + } - /* INDEX >> */ - if (info_IM.index_on) - { - info_IM.tile[j2k_curtileno].end_pos = - cio_tell () + pos_correction - 1; - } - /* << INDEX */ - - /* - if (tile->PPT) BAD PPT !!! - { - FILE *PPT_file; - - int i; - PPT_file=fopen("PPT","rb"); - fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256); - for (i=0;i<tile->len_ppt;i++) - { - unsigned char elmt; - fread(&elmt, 1, 1, PPT_file); - fwrite(&elmt,1,1,f); - } - fclose(PPT_file); - unlink("PPT"); - } - */ - if (cp->intermed_file == 1) - { - fwrite (dest, 1, cio_tell (), f); - pos_correction = cio_tell () + pos_correction; - } + /* INDEX >> */ + if (info_IM.index_on) { + info_IM.tile[j2k_curtileno].num_tile = j2k_curtileno; + info_IM.tile[j2k_curtileno].start_pos = cio_tell() + pos_correction; } + /* << INDEX */ + j2k_write_sot(); - if (cp->intermed_file == 1) - { - free (dest); - dest = (char *) malloc (len); - cio_init (dest, len); + for (compno = 1; compno < img->numcomps; compno++) { + j2k_write_coc(compno); + j2k_write_qcc(compno); } - j2k_write_eoc (); + if (cp->tcps[tileno].numpocs) + j2k_write_poc(); + j2k_write_sod(); - if (cp->intermed_file == 1) - { - fwrite (dest, 1, 2, f); - free (dest); - /* closing file *.j2k */ - fclose (f); + /* INDEX >> */ + if (info_IM.index_on) { + info_IM.tile[j2k_curtileno].end_pos = + cio_tell() + pos_correction - 1; + } + /* << INDEX */ + + /* + if (tile->PPT) BAD PPT !!! + { + FILE *PPT_file; + + int i; + PPT_file=fopen("PPT","rb"); + fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256); + for (i=0;i<tile->len_ppt;i++) + { + unsigned char elmt; + fread(&elmt, 1, 1, PPT_file); + fwrite(&elmt,1,1,f); + } + fclose(PPT_file); + unlink("PPT"); + } + */ + if (cp->intermed_file == 1) { + fwrite(dest, 1, cio_tell(), f); + pos_correction = cio_tell() + pos_correction; } + } - /* Creation of the index file */ + if (cp->intermed_file == 1) { + free(dest); + dest = (char *) malloc(len); + cio_init(dest, len); + } - if (info_IM.index_on) - { + j2k_write_eoc(); - double DistoTotal = 0; + if (cp->intermed_file == 1) { + fwrite(dest, 1, 2, f); + free(dest); + /* closing file *.j2k */ + fclose(f); + } - info_IM.codestream_size = cio_tell () + pos_correction; /* Correction 14/4/03 suite rmq de Patrick */ + /* Creation of the index file */ - INDEX = fopen (index, "w"); + if (info_IM.index_on) { + double DistoTotal = 0; + info_IM.codestream_size = cio_tell() + pos_correction; /* Correction 14/4/03 suite rmq de Patrick */ - if (!INDEX) - { + INDEX = fopen(index, "w"); - fprintf (stderr, "failed to open %s for writing\n", index); - return 1; - } + if (!INDEX) { + fprintf(stderr, "failed to open %s for writing\n", index); + return 1; - fprintf (INDEX, "%d %d\n", info_IM.Im_w, info_IM.Im_h); + } - fprintf (INDEX, "%d\n", info_IM.Prog); - fprintf (INDEX, "%d %d\n", info_IM.Tile_x, info_IM.Tile_y); - fprintf (INDEX, "%d %d\n", info_IM.tw, info_IM.th); + fprintf(INDEX, "%d %d\n", info_IM.Im_w, info_IM.Im_h); - fprintf (INDEX, "%d\n", info_IM.Comp); + fprintf(INDEX, "%d\n", info_IM.Prog); - fprintf (INDEX, "%d\n", info_IM.Layer); + fprintf(INDEX, "%d %d\n", info_IM.Tile_x, info_IM.Tile_y); - fprintf (INDEX, "%d\n", info_IM.Decomposition); + fprintf(INDEX, "%d %d\n", info_IM.tw, info_IM.th); - for (resno = info_IM.Decomposition; resno >= 0; resno--) - { + fprintf(INDEX, "%d\n", info_IM.Comp); - fprintf (INDEX, "[%d,%d] ", (1 << info_IM.tile[0].pdx[resno]), (1 << info_IM.tile[0].pdx[resno])); //based on tile 0 + fprintf(INDEX, "%d\n", info_IM.Layer); - } + fprintf(INDEX, "%d\n", info_IM.Decomposition); - fprintf (INDEX, "\n"); + for (resno = info_IM.Decomposition; resno >= 0; resno--) { - fprintf (INDEX, "%d\n", info_IM.Main_head_end); + fprintf(INDEX, "[%d,%d] ", (1 << info_IM.tile[0].pdx[resno]), (1 << info_IM.tile[0].pdx[resno])); //based on tile 0 - fprintf (INDEX, "%d\n", info_IM.codestream_size); + } - for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++) - { + fprintf(INDEX, "\n"); - fprintf (INDEX, "%4d %9d %9d %9d %9e %9d %9e\n", - info_IM.tile[tileno].num_tile, - info_IM.tile[tileno].start_pos, - info_IM.tile[tileno].end_header, - info_IM.tile[tileno].end_pos, - info_IM.tile[tileno].distotile, info_IM.tile[tileno].nbpix, - info_IM.tile[tileno].distotile / - info_IM.tile[tileno].nbpix); + fprintf(INDEX, "%d\n", info_IM.Main_head_end); - } + fprintf(INDEX, "%d\n", info_IM.codestream_size); - for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++) - { + for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++) { - int start_pos, end_pos; + fprintf(INDEX, "%4d %9d %9d %9d %9e %9d %9e\n", + info_IM.tile[tileno].num_tile, + info_IM.tile[tileno].start_pos, + info_IM.tile[tileno].end_header, + info_IM.tile[tileno].end_pos, + info_IM.tile[tileno].distotile, info_IM.tile[tileno].nbpix, + info_IM.tile[tileno].distotile / info_IM.tile[tileno].nbpix); - double disto = 0; + } - pack_nb = 0; + for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++) { - /* fprintf(INDEX, + int start_pos, end_pos; - "pkno tileno layerno resno compno precno start_pos end_pos deltaSE \n"); */ + double disto = 0; - if (info_IM.Prog == 0) - { /* LRCP */ + pack_nb = 0; - for (layno = 0; layno < info_IM.Layer; layno++) - { + /* fprintf(INDEX, - for (resno = 0; resno < info_IM.Decomposition + 1; resno++) - { + "pkno tileno layerno resno compno precno start_pos end_pos deltaSE \n"); */ - for (compno = 0; compno < info_IM.Comp; compno++) - { + if (info_IM.Prog == 0) { /* LRCP */ - for (precno = 0; - precno < - info_IM.tile[tileno].pw[resno] * - info_IM.tile[tileno].ph[resno]; precno++) - { + for (layno = 0; layno < info_IM.Layer; layno++) { - start_pos = - info_IM.tile[tileno].packet[pack_nb]. - start_pos; + for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { - end_pos = - info_IM.tile[tileno].packet[pack_nb].end_pos; + for (compno = 0; compno < info_IM.Comp; compno++) { - disto = - info_IM.tile[tileno].packet[pack_nb].disto; + for (precno = 0; + precno < + info_IM.tile[tileno].pw[resno] * + info_IM.tile[tileno].ph[resno]; precno++) { - fprintf (INDEX, - "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", - pack_nb, tileno, layno, resno, compno, - precno, start_pos, end_pos, disto); + start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos; - DistoTotal += disto; + end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos; - pack_nb++; + disto = info_IM.tile[tileno].packet[pack_nb].disto; - } + fprintf(INDEX, + "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", + pack_nb, tileno, layno, resno, compno, + precno, start_pos, end_pos, disto); - } + DistoTotal += disto; - } + pack_nb++; - } + } } - else if (info_IM.Prog == 1) - { /* RLCP */ - for (resno = 0; resno < info_IM.Decomposition + 1; resno++) - { + } - for (layno = 0; layno < info_IM.Layer; layno++) - { + } - for (compno = 0; compno < info_IM.Comp; compno++) - { + } else if (info_IM.Prog == 1) { /* RLCP */ - for (precno = 0; - precno < - info_IM.tile[tileno].pw[resno] * - info_IM.tile[tileno].ph[resno]; precno++) - { + for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { - start_pos = - info_IM.tile[tileno].packet[pack_nb]. - start_pos; + for (layno = 0; layno < info_IM.Layer; layno++) { - end_pos = - info_IM.tile[tileno].packet[pack_nb].end_pos; + for (compno = 0; compno < info_IM.Comp; compno++) { - disto = - info_IM.tile[tileno].packet[pack_nb].disto; + for (precno = 0; + precno < + info_IM.tile[tileno].pw[resno] * + info_IM.tile[tileno].ph[resno]; precno++) { - fprintf (INDEX, - "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", - pack_nb, tileno, layno, resno, compno, - precno, start_pos, end_pos, disto); + start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos; - DistoTotal += disto; + end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos; - pack_nb++; + disto = info_IM.tile[tileno].packet[pack_nb].disto; - } + fprintf(INDEX, + "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", + pack_nb, tileno, layno, resno, compno, + precno, start_pos, end_pos, disto); - } + DistoTotal += disto; - } + pack_nb++; - } + } } - else if (info_IM.Prog == 2) - { /* RPCL */ - for (resno = 0; resno < info_IM.Decomposition + 1; resno++) - { + } - for (precno = 0; - precno < - info_IM.tile[tileno].pw[resno] * - info_IM.tile[tileno].ph[resno]; precno++) - { + } - for (compno = 0; compno < info_IM.Comp; compno++) - { + } else if (info_IM.Prog == 2) { /* RPCL */ - for (layno = 0; layno < info_IM.Layer; layno++) - { + for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { - start_pos = - info_IM.tile[tileno].packet[pack_nb]. - start_pos; + for (precno = 0; + precno < + info_IM.tile[tileno].pw[resno] * + info_IM.tile[tileno].ph[resno]; precno++) { - end_pos = - info_IM.tile[tileno].packet[pack_nb].end_pos; + for (compno = 0; compno < info_IM.Comp; compno++) { - disto = - info_IM.tile[tileno].packet[pack_nb].disto; + for (layno = 0; layno < info_IM.Layer; layno++) { - fprintf (INDEX, - "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", - pack_nb, tileno, layno, resno, compno, - precno, start_pos, end_pos, disto); + start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos; - DistoTotal += disto; + end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos; - pack_nb++; + disto = info_IM.tile[tileno].packet[pack_nb].disto; - } + fprintf(INDEX, + "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", + pack_nb, tileno, layno, resno, compno, + precno, start_pos, end_pos, disto); - } + DistoTotal += disto; - } + pack_nb++; - } + } } - else if (info_IM.Prog == 3) - { /* PCRL */ - for (precno = 0; - precno < - info_IM.tile[tileno].pw[resno] * - info_IM.tile[tileno].ph[resno]; precno++) - { + } - for (compno = 0; compno < info_IM.Comp; compno++) - { + } - for (resno = 0; resno < info_IM.Decomposition + 1; - resno++) - { + } else if (info_IM.Prog == 3) { /* PCRL */ - for (layno = 0; layno < info_IM.Layer; layno++) - { + for (precno = 0; + precno < + info_IM.tile[tileno].pw[resno] * + info_IM.tile[tileno].ph[resno]; precno++) { - start_pos = - info_IM.tile[tileno].packet[pack_nb]. - start_pos; + for (compno = 0; compno < info_IM.Comp; compno++) { - end_pos = - info_IM.tile[tileno].packet[pack_nb].end_pos; + for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { - disto = - info_IM.tile[tileno].packet[pack_nb].disto; + for (layno = 0; layno < info_IM.Layer; layno++) { - fprintf (INDEX, - "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", - pack_nb, tileno, layno, resno, compno, - precno, start_pos, end_pos, disto); + start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos; - DistoTotal += disto; + end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos; - pack_nb++; + disto = info_IM.tile[tileno].packet[pack_nb].disto; - } + fprintf(INDEX, + "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", + pack_nb, tileno, layno, resno, compno, + precno, start_pos, end_pos, disto); - } + DistoTotal += disto; - } + pack_nb++; - } + } } - else - { /* CPRL */ + } + } - for (compno = 0; compno < info_IM.Comp; compno++) - { + } else { /* CPRL */ - for (precno = 0; - precno < - info_IM.tile[tileno].pw[resno] * - info_IM.tile[tileno].ph[resno]; precno++) - { - for (resno = 0; resno < info_IM.Decomposition + 1; - resno++) - { - for (layno = 0; layno < info_IM.Layer; layno++) - { + for (compno = 0; compno < info_IM.Comp; compno++) { - start_pos = - info_IM.tile[tileno].packet[pack_nb]. - start_pos; + for (precno = 0; + precno < + info_IM.tile[tileno].pw[resno] * + info_IM.tile[tileno].ph[resno]; precno++) { - end_pos = - info_IM.tile[tileno].packet[pack_nb].end_pos; + for (resno = 0; resno < info_IM.Decomposition + 1; resno++) { - disto = - info_IM.tile[tileno].packet[pack_nb].disto; + for (layno = 0; layno < info_IM.Layer; layno++) { - fprintf (INDEX, - "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", - pack_nb, tileno, layno, resno, compno, - precno, start_pos, end_pos, disto); + start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos; - DistoTotal += disto; + end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos; - pack_nb++; + disto = info_IM.tile[tileno].packet[pack_nb].disto; - } + fprintf(INDEX, + "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", + pack_nb, tileno, layno, resno, compno, + precno, start_pos, end_pos, disto); - } + DistoTotal += disto; - } + pack_nb++; - } + } } + } + } - fprintf (INDEX, "SE max : %8e\n", info_IM.D_max); + } - fprintf (INDEX, "SE total : %.8e\n", DistoTotal); + } - fclose (INDEX); + fprintf(INDEX, "SE max : %8e\n", info_IM.D_max); - } + fprintf(INDEX, "SE total : %.8e\n", DistoTotal); + + fclose(INDEX); - j2k_clean (); + } - return cio_tell (); + j2k_clean(); + + return cio_tell(); } -typedef struct -{ +typedef struct { int id; int states; void (*handler) (); @@ -1601,70 +1424,60 @@ j2k_dec_mstabent_t j2k_dec_mstab[] = { {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk} }; -j2k_dec_mstabent_t * -j2k_dec_mstab_lookup (int id) +j2k_dec_mstabent_t *j2k_dec_mstab_lookup(int id) { j2k_dec_mstabent_t *e; - for (e = j2k_dec_mstab; e->id != 0; e++) - { - if (e->id == id) - { - break; - } + for (e = j2k_dec_mstab; e->id != 0; e++) { + if (e->id == id) { + break; } + } return e; } LIBJ2K_API int -j2k_decode (unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp) +j2k_decode(unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp) { - if (setjmp (j2k_error)) - { - if (j2k_state != J2K_STATE_MT) - { - fprintf (stderr, "WARNING: incomplete bitstream\n"); - return 0; - } - j2k_clean (); - return cio_numbytes (); /* Correct way of ending j2k_decode */ + if (setjmp(j2k_error)) { + if (j2k_state != J2K_STATE_MT) { + fprintf(stderr, "WARNING: incomplete bitstream\n"); + return 0; } + j2k_clean(); + return cio_numbytes(); /* Correct way of ending j2k_decode */ + } j2k_img = img; j2k_cp = cp; j2k_state = J2K_STATE_MHSOC; - cio_init (src, len); - - for (;;) - { - j2k_dec_mstabent_t *e; - int id = cio_read (2); - if (id >> 8 != 0xff) - { - fprintf (stderr, "%.8x: expected a marker instead of %x\n", - cio_tell () - 2, id); - return 0; - } - e = j2k_dec_mstab_lookup (id); - if (!(j2k_state & e->states)) - { - fprintf (stderr, "%.8x: unexpected marker %x\n", cio_tell () - 2, - id); - return 0; - } - if (e->handler) - { - (*e->handler) (); - } - if (j2k_state == J2K_STATE_NEOC) - break; /* RAJOUTE */ + cio_init(src, len); + + for (;;) { + j2k_dec_mstabent_t *e; + int id = cio_read(2); + if (id >> 8 != 0xff) { + fprintf(stderr, "%.8x: expected a marker instead of %x\n", + cio_tell() - 2, id); + return 0; } + e = j2k_dec_mstab_lookup(id); + if (!(j2k_state & e->states)) { + fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id); + return 0; + } + if (e->handler) { + (*e->handler) (); + } + if (j2k_state == J2K_STATE_NEOC) + break; /* RAJOUTE */ + } if (j2k_state == J2K_STATE_NEOC) - j2k_read_eoc (); /* RAJOUTE */ + j2k_read_eoc(); /* RAJOUTE */ return 0; } @@ -1674,88 +1487,77 @@ j2k_decode (unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp) * */ int -j2k_decode_jpt_stream (unsigned char *src, int len, j2k_image_t * img, - j2k_cp_t * cp) +j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t * img, + j2k_cp_t * cp) { jpt_msg_header_struct_t header; int position; - if (setjmp (j2k_error)) - { - if (j2k_state != J2K_STATE_MT) - { - fprintf (stderr, "WARNING: incomplete bitstream\n"); - return 0; - } - return cio_numbytes (); + if (setjmp(j2k_error)) { + if (j2k_state != J2K_STATE_MT) { + fprintf(stderr, "WARNING: incomplete bitstream\n"); + return 0; } + return cio_numbytes(); + } j2k_img = img; j2k_cp = cp; j2k_state = J2K_STATE_MHSOC; - cio_init (src, len); + cio_init(src, len); /* Initialize the header */ - jpt_init_Msg_Header (&header); + jpt_init_Msg_Header(&header); /* Read the first header of the message */ - jpt_read_Msg_Header (&header); - - position = cio_tell (); - if (header.Class_Id != 6) - { /* 6 : Main header data-bin message */ - fprintf (stderr, - "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", - header.Class_Id); + jpt_read_Msg_Header(&header); + + position = cio_tell(); + if (header.Class_Id != 6) { /* 6 : Main header data-bin message */ + fprintf(stderr, + "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", + header.Class_Id); + return 0; + } + + for (;;) { + j2k_dec_mstabent_t *e; + int id; + + if (!cio_numbytesleft()) { + j2k_read_eoc(); return 0; } + /* data-bin read -> need to read a new header */ + if ((unsigned int) (cio_tell() - position) == header.Msg_length) { + jpt_read_Msg_Header(&header); + position = cio_tell(); + if (header.Class_Id != 4) { /* 4 : Tile data-bin message */ + fprintf(stderr, "[JPT-stream] : Expecting Tile info !\n"); + return 0; + } + } - for (;;) - { - j2k_dec_mstabent_t *e; - int id; - - if (!cio_numbytesleft ()) - { - j2k_read_eoc (); - return 0; - } - /* data-bin read -> need to read a new header */ - if ((unsigned int) (cio_tell () - position) == header.Msg_length) - { - jpt_read_Msg_Header (&header); - position = cio_tell (); - if (header.Class_Id != 4) - { /* 4 : Tile data-bin message */ - fprintf (stderr, "[JPT-stream] : Expecting Tile info !\n"); - return 0; - } - } - - id = cio_read (2); - if (id >> 8 != 0xff) - { - fprintf (stderr, "%.8x: expected a marker instead of %x\n", - cio_tell () - 2, id); - return 0; - } - e = j2k_dec_mstab_lookup (id); - if (!(j2k_state & e->states)) - { - fprintf (stderr, "%.8x: unexpected marker %x\n", cio_tell () - 2, - id); - return 0; - } - if (e->handler) - { - (*e->handler) (); - } - if (j2k_state == J2K_STATE_NEOC) - break; /* RAJOUTE */ + id = cio_read(2); + if (id >> 8 != 0xff) { + fprintf(stderr, "%.8x: expected a marker instead of %x\n", + cio_tell() - 2, id); + return 0; + } + e = j2k_dec_mstab_lookup(id); + if (!(j2k_state & e->states)) { + fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id); + return 0; + } + if (e->handler) { + (*e->handler) (); } + if (j2k_state == J2K_STATE_NEOC) + break; /* RAJOUTE */ + } if (j2k_state == J2K_STATE_NEOC) - j2k_read_eoc (); /* RAJOUTE */ + j2k_read_eoc(); /* RAJOUTE */ return 0; } @@ -1764,16 +1566,15 @@ j2k_decode_jpt_stream (unsigned char *src, int len, j2k_image_t * img, #include <windows.h> BOOL APIENTRY -DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) +DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { - switch (ul_reason_for_call) - { - case DLL_PROCESS_ATTACH: - case DLL_THREAD_ATTACH: - case DLL_THREAD_DETACH: - case DLL_PROCESS_DETACH: - break; - } + switch (ul_reason_for_call) { + case DLL_PROCESS_ATTACH: + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + case DLL_PROCESS_DETACH: + break; + } return TRUE; } #endif diff --git a/libopenjpeg/tcd.c b/libopenjpeg/tcd.c index 356c4665..ab71c129 100644 --- a/libopenjpeg/tcd.c +++ b/libopenjpeg/tcd.c @@ -58,607 +58,532 @@ static tcd_cblk_t *cblk; extern jmp_buf j2k_error; -void -tcd_dump (tcd_image_t * img, int curtileno) +void tcd_dump(tcd_image_t * img, int curtileno) { int tileno, compno, resno, bandno, precno, cblkno; - fprintf (stderr, "image {\n"); - fprintf (stderr, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", img->tw, - img->th, tcd_img->x0, tcd_img->x1, tcd_img->y0, tcd_img->y1); - for (tileno = 0; tileno < img->th * img->tw; tileno++) - { - tcd_tile_t *tile = &tcd_image.tiles[tileno]; - fprintf (stderr, " tile {\n"); - fprintf (stderr, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", - tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); - for (compno = 0; compno < tile->numcomps; compno++) - { - tcd_tilecomp_t *tilec = &tile->comps[compno]; - fprintf (stderr, " tilec {\n"); - fprintf (stderr, - " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", - tilec->x0, tilec->y0, tilec->x1, tilec->y1, - tilec->numresolutions); - for (resno = 0; resno < tilec->numresolutions; resno++) - { - tcd_resolution_t *res = &tilec->resolutions[resno]; - fprintf (stderr, "\n res {\n"); - fprintf (stderr, - " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", - res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, - res->numbands); - for (bandno = 0; bandno < res->numbands; bandno++) - { - tcd_band_t *band = &res->bands[bandno]; - fprintf (stderr, " band {\n"); - fprintf (stderr, - " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%d, numbps=%d\n", - band->x0, band->y0, band->x1, band->y1, - band->stepsize, band->numbps); - for (precno = 0; precno < res->pw * res->ph; precno++) - { - tcd_precinct_t *prec = &band->precincts[precno]; - fprintf (stderr, " prec {\n"); - fprintf (stderr, - " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", - prec->x0, prec->y0, prec->x1, prec->y1, - prec->cw, prec->ch); - for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) - { - tcd_cblk_t *cblk = &prec->cblks[cblkno]; - fprintf (stderr, " cblk {\n"); - fprintf (stderr, - " x0=%d, y0=%d, x1=%d, y1=%d\n", - cblk->x0, cblk->y0, cblk->x1, cblk->y1); - fprintf (stderr, " }\n"); - } - fprintf (stderr, " }\n"); - } - fprintf (stderr, " }\n"); - } - fprintf (stderr, " }\n"); + fprintf(stderr, "image {\n"); + fprintf(stderr, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", img->tw, + img->th, tcd_img->x0, tcd_img->x1, tcd_img->y0, tcd_img->y1); + for (tileno = 0; tileno < img->th * img->tw; tileno++) { + tcd_tile_t *tile = &tcd_image.tiles[tileno]; + fprintf(stderr, " tile {\n"); + fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", + tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); + for (compno = 0; compno < tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tile->comps[compno]; + fprintf(stderr, " tilec {\n"); + fprintf(stderr, + " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", + tilec->x0, tilec->y0, tilec->x1, tilec->y1, + tilec->numresolutions); + for (resno = 0; resno < tilec->numresolutions; resno++) { + tcd_resolution_t *res = &tilec->resolutions[resno]; + fprintf(stderr, "\n res {\n"); + fprintf(stderr, + " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", + res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, + res->numbands); + for (bandno = 0; bandno < res->numbands; bandno++) { + tcd_band_t *band = &res->bands[bandno]; + fprintf(stderr, " band {\n"); + fprintf(stderr, + " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%d, numbps=%d\n", + band->x0, band->y0, band->x1, band->y1, + band->stepsize, band->numbps); + for (precno = 0; precno < res->pw * res->ph; precno++) { + tcd_precinct_t *prec = &band->precincts[precno]; + fprintf(stderr, " prec {\n"); + fprintf(stderr, + " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", + prec->x0, prec->y0, prec->x1, prec->y1, + prec->cw, prec->ch); + for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) { + tcd_cblk_t *cblk = &prec->cblks[cblkno]; + fprintf(stderr, " cblk {\n"); + fprintf(stderr, + " x0=%d, y0=%d, x1=%d, y1=%d\n", + cblk->x0, cblk->y0, cblk->x1, cblk->y1); + fprintf(stderr, " }\n"); } - fprintf (stderr, " }\n"); + fprintf(stderr, " }\n"); + } + fprintf(stderr, " }\n"); } - fprintf (stderr, " }\n"); + fprintf(stderr, " }\n"); + } + fprintf(stderr, " }\n"); } - fprintf (stderr, "}\n"); + fprintf(stderr, " }\n"); + } + fprintf(stderr, "}\n"); } -void -tcd_malloc_encode (j2k_image_t * img, j2k_cp_t * cp, int curtileno) +void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno) { 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 (sizeof (tcd_tile_t)); - - for (tileno = 0; tileno < 1; tileno++) - { - j2k_tcp_t *tcp = &cp->tcps[curtileno]; - int j; - /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - int p = curtileno % cp->tw; /* si numerotation matricielle .. */ - int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ - /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */ - tile = tcd_image.tiles; - /* 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->PPT=img->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) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0; //Mod antonin losslessbug - 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; - } - } + tcd_image.tiles = (tcd_tile_t *) malloc(sizeof(tcd_tile_t)); + + for (tileno = 0; tileno < 1; tileno++) { + j2k_tcp_t *tcp = &cp->tcps[curtileno]; + int j; + /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ + int p = curtileno % cp->tw; /* si numerotation matricielle .. */ + int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ + /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */ + tile = tcd_image.tiles; + /* 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->PPT=img->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) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0; //Mod antonin losslessbug + 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 = - (tcd_tilecomp_t *) malloc (img->numcomps * sizeof (tcd_tilecomp_t)); - for (compno = 0; compno < tile->numcomps; compno++) - { - j2k_tccp_t *tccp = &tcp->tccps[compno]; - /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */ - tilec = &tile->comps[compno]; - /* 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 ((tilec->x1 - tilec->x0) * - (tilec->y1 - tilec->y0) * sizeof (int)); - tilec->numresolutions = tccp->numresolutions; - - tilec->resolutions = - (tcd_resolution_t *) malloc (tilec->numresolutions * - sizeof (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; - /* tcd_resolution_t *res=&tilec->resolutions[resno]; */ - - 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, i; - int gain, numbps; - j2k_stepsize_t *ss; - 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 = img->comps[compno].prec + gain; - band->stepsize = - (int) floor ((1.0 + ss->mant / 2048.0) * - pow (2.0, numbps - ss->expn) * 8192.0); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ - - band->precincts = - (tcd_precinct_t *) malloc (3 * res->pw * res->ph * - sizeof (tcd_precinct_t)); - - for (i = 0; i < res->pw * res->ph * 3; i++) - { - band->precincts[i].imsbtree = NULL; - band->precincts[i].incltree = NULL; - } - - 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]; */ - 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); - - 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); - } - } - } + } + } + /* << Modification of the RATE */ + + tile->comps = + (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t)); + for (compno = 0; compno < tile->numcomps; compno++) { + j2k_tccp_t *tccp = &tcp->tccps[compno]; + /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */ + tilec = &tile->comps[compno]; + /* 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((tilec->x1 - tilec->x0) * + (tilec->y1 - tilec->y0) * sizeof(int)); + tilec->numresolutions = tccp->numresolutions; + + tilec->resolutions = + (tcd_resolution_t *) malloc(tilec->numresolutions * + sizeof(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; + /* tcd_resolution_t *res=&tilec->resolutions[resno]; */ + + 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, i; + int gain, numbps; + j2k_stepsize_t *ss; + 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 = img->comps[compno].prec + gain; + band->stepsize = + (int) floor((1.0 + ss->mant / 2048.0) * + pow(2.0, numbps - ss->expn) * 8192.0); + band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ + + band->precincts = + (tcd_precinct_t *) malloc(3 * res->pw * res->ph * + sizeof(tcd_precinct_t)); + + for (i = 0; i < res->pw * res->ph * 3; i++) { + band->precincts[i].imsbtree = NULL; + band->precincts[i].incltree = NULL; + } + + 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]; */ + 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); + + 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); } + } } + } } + } /* tcd_dump(&tcd_image,curtileno); */ } -void -tcd_free_encode (j2k_image_t * img, j2k_cp_t * cp, int curtileno) +void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno) { int tileno, compno, resno, bandno, precno; tcd_img = img; tcd_cp = cp; tcd_image.tw = cp->tw; tcd_image.th = cp->th; - for (tileno = 0; tileno < 1; tileno++) - { - /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */ - tile = tcd_image.tiles; - for (compno = 0; compno < tile->numcomps; compno++) - { - tilec = &tile->comps[compno]; - for (resno = 0; resno < tilec->numresolutions; resno++) - { - res = &tilec->resolutions[resno]; - for (bandno = 0; bandno < res->numbands; bandno++) - { - band = &res->bands[bandno]; - for (precno = 0; precno < res->pw * res->ph; precno++) - { - prc = &band->precincts[precno]; - - if (prc->incltree != NULL) - tgt_destroy (prc->incltree); - if (prc->imsbtree != NULL) - tgt_destroy (prc->imsbtree); - free (prc->cblks); - } /* for (precno */ - free (band->precincts); - } /* for (bandno */ - } /* for (resno */ - free (tilec->resolutions); - } /* for (compno */ - free (tile->comps); - } /* for (tileno */ - free (tcd_image.tiles); + for (tileno = 0; tileno < 1; tileno++) { + /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */ + tile = tcd_image.tiles; + for (compno = 0; compno < tile->numcomps; compno++) { + tilec = &tile->comps[compno]; + for (resno = 0; resno < tilec->numresolutions; resno++) { + res = &tilec->resolutions[resno]; + for (bandno = 0; bandno < res->numbands; bandno++) { + band = &res->bands[bandno]; + for (precno = 0; precno < res->pw * res->ph; precno++) { + prc = &band->precincts[precno]; + + if (prc->incltree != NULL) + tgt_destroy(prc->incltree); + if (prc->imsbtree != NULL) + tgt_destroy(prc->imsbtree); + free(prc->cblks); + } /* for (precno */ + free(band->precincts); + } /* for (bandno */ + } /* for (resno */ + free(tilec->resolutions); + } /* for (compno */ + free(tile->comps); + } /* for (tileno */ + free(tcd_image.tiles); } -void -tcd_init_encode (j2k_image_t * img, j2k_cp_t * cp, int curtileno) +void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno) { int tileno, compno, resno, bandno, precno, cblkno; - for (tileno = 0; tileno < 1; tileno++) - { - j2k_tcp_t *tcp = &cp->tcps[curtileno]; - int j; - // int previous_x0, previous_x1, previous_y0, previous_y1; - /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - int p = curtileno % cp->tw; - int q = curtileno / cp->tw; - tile = tcd_image.tiles; - - /* 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->PPT=img->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) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0; //Mod antonin losslessbug - 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; - } - } + for (tileno = 0; tileno < 1; tileno++) { + j2k_tcp_t *tcp = &cp->tcps[curtileno]; + int j; + // int previous_x0, previous_x1, previous_y0, previous_y1; + /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ + int p = curtileno % cp->tw; + int q = curtileno / cp->tw; + tile = tcd_image.tiles; + + /* 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->PPT=img->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) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0; //Mod antonin losslessbug + 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=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */ - for (compno = 0; compno < tile->numcomps; compno++) - { - j2k_tccp_t *tccp = &tcp->tccps[compno]; - /* int realloc_op; */ - - tilec = &tile->comps[compno]; - /* 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 ((tilec->x1 - tilec->x0) * - (tilec->y1 - tilec->y0) * sizeof (int)); - tilec->numresolutions = tccp->numresolutions; - /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(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; - - 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; - j2k_stepsize_t *ss; - 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 = img->comps[compno].prec + gain; - band->stepsize = - (int) floor ((1.0 + ss->mant / 2048.0) * - pow (2.0, numbps - ss->expn) * 8192.0); - 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); - - 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; - - free (prc->cblks); - prc->cblks = - (tcd_cblk_t *) malloc (prc->cw * prc->ch * - sizeof (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); - 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); - - } - } - } + } + } + /* << Modification of the RATE */ + /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */ + for (compno = 0; compno < tile->numcomps; compno++) { + j2k_tccp_t *tccp = &tcp->tccps[compno]; + /* int realloc_op; */ + + tilec = &tile->comps[compno]; + /* 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((tilec->x1 - tilec->x0) * + (tilec->y1 - tilec->y0) * sizeof(int)); + tilec->numresolutions = tccp->numresolutions; + /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(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; + + 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; + j2k_stepsize_t *ss; + 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 = img->comps[compno].prec + gain; + band->stepsize = + (int) floor((1.0 + ss->mant / 2048.0) * + pow(2.0, numbps - ss->expn) * 8192.0); + 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); + + 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; + + free(prc->cblks); + prc->cblks = + (tcd_cblk_t *) malloc(prc->cw * prc->ch * + sizeof(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); + 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); + } + } } + } } + } /* tcd_dump(&tcd_image,0); */ } -void -tcd_init (j2k_image_t * img, j2k_cp_t * cp) +void tcd_init(j2k_image_t * img, j2k_cp_t * cp) { int tileno, compno, resno, bandno, precno, cblkno, i, j; unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h, p, q; @@ -667,225 +592,201 @@ tcd_init (j2k_image_t * img, j2k_cp_t * 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)); + (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]; */ - for (i = 0; i < cp->tileno_size; i++) - { - j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]]; - tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]]; - tileno = cp->tileno[i]; - - - // int previous_x0, previous_x1, previous_y0, previous_y1; - /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - 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, 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)); - for (compno = 0; compno < tile->numcomps; compno++) - { - j2k_tccp_t *tccp = &tcp->tccps[compno]; - tcd_tilecomp_t *tilec = &tile->comps[compno]; - /* 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 ((tilec->x1 - tilec->x0) * - (tilec->y1 - tilec->y0) * sizeof (int)); - tilec->numresolutions = tccp->numresolutions; - tilec->resolutions = - (tcd_resolution_t *) malloc (tilec->numresolutions * - sizeof (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; - 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); // Mod Antonin : sizebug1 - res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); // Mod Antonin : sizebug1 - - 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; - j2k_stepsize_t *ss; - 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 = img->comps[compno].prec + gain; - band->stepsize = - (int) floor ((1.0 + ss->mant / 2048.0) * - pow (2.0, numbps - ss->expn) * 8192.0); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -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); - - - - cblk->lastbp = 0; // Add Antonin : quantizbug1 - } - } - } + for (i = 0; i < cp->tileno_size; i++) { + j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]]; + tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]]; + tileno = cp->tileno[i]; + + + // int previous_x0, previous_x1, previous_y0, previous_y1; + /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ + 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, 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)); + for (compno = 0; compno < tile->numcomps; compno++) { + j2k_tccp_t *tccp = &tcp->tccps[compno]; + tcd_tilecomp_t *tilec = &tile->comps[compno]; + /* 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((tilec->x1 - tilec->x0) * + (tilec->y1 - tilec->y0) * sizeof(int)); + tilec->numresolutions = tccp->numresolutions; + tilec->resolutions = + (tcd_resolution_t *) malloc(tilec->numresolutions * + sizeof(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; + 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); // Mod Antonin : sizebug1 + res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); // Mod Antonin : sizebug1 + + 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; + j2k_stepsize_t *ss; + 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 = img->comps[compno].prec + gain; + band->stepsize = + (int) floor((1.0 + ss->mant / 2048.0) * + pow(2.0, numbps - ss->expn) * 8192.0); + band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -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); + + + + cblk->lastbp = 0; // Add Antonin : quantizbug1 } + } } + } } + } //tcd_dump(&tcd_image,0); @@ -893,56 +794,52 @@ tcd_init (j2k_image_t * img, j2k_cp_t * cp) /* Place limited by the tile really present in the codestream */ - for (i = 0; i < img->numcomps; i++) - { - for (j = 0; j < cp->tileno_size; j++) - { - tileno = cp->tileno[j]; - x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min (x0, - tcd_image. - tiles - [tileno]. - comps - [i]. - x0); - y0 = - j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min (y0, - tcd_image. - tiles - [tileno]. - comps[i]. - y0); - x1 = - j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max (x1, - tcd_image. - tiles - [tileno]. - comps[i]. - x1); - y1 = - j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max (y1, - tcd_image. - tiles - [tileno]. - comps[i]. - y1); - } - //w = int_ceildiv(x1 - x0, img->comps[i].dx); - //h = int_ceildiv(y1 - y0, img->comps[i].dy); - - w = x1 - x0; - - h = y1 - y0; - img->comps[i].data = (int *) calloc (w * h, sizeof (int)); - img->comps[i].w = w; - img->comps[i].h = h; - img->comps[i].x0 = x0; - img->comps[i].y0 = y0; + for (i = 0; i < img->numcomps; i++) { + for (j = 0; j < cp->tileno_size; j++) { + tileno = cp->tileno[j]; + x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min(x0, + tcd_image. + tiles + [tileno]. + comps + [i].x0); + y0 = + j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min(y0, + tcd_image. + tiles + [tileno]. + comps[i]. + y0); + x1 = + j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max(x1, + tcd_image. + tiles + [tileno]. + comps[i]. + x1); + y1 = + j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max(y1, + tcd_image. + tiles + [tileno]. + comps[i]. + y1); } + //w = int_ceildiv(x1 - x0, img->comps[i].dx); + //h = int_ceildiv(y1 - y0, img->comps[i].dy); + + w = x1 - x0; + + h = y1 - y0; + img->comps[i].data = (int *) calloc(w * h, sizeof(int)); + img->comps[i].w = w; + img->comps[i].h = h; + img->comps[i].x0 = x0; + img->comps[i].y0 = y0; + } } -void -tcd_makelayer_fixed (int layno, int final) +void tcd_makelayer_fixed(int layno, int final) { int compno, resno, bandno, precno, cblkno; int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; @@ -951,213 +848,174 @@ tcd_makelayer_fixed (int layno, int final) /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */ - for (compno = 0; compno < tcd_tile->numcomps; compno++) - { - tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; - for (i = 0; i < tcd_tcp->numlayers; i++) - { - for (j = 0; j < tilec->numresolutions; j++) - { - for (k = 0; k < 3; k++) - { - matrice[i][j][k] = - (int) (tcd_cp-> - matrice[i * tilec->numresolutions * 3 + - j * 3 + - k] * - (float) (tcd_img->comps[compno].prec / 16.0)); - }}} - - for (resno = 0; resno < tilec->numresolutions; resno++) - { - tcd_resolution_t *res = &tilec->resolutions[resno]; - for (bandno = 0; bandno < res->numbands; bandno++) - { - tcd_band_t *band = &res->bands[bandno]; - for (precno = 0; precno < res->pw * res->ph; precno++) - { - tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) - { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - tcd_layer_t *layer = &cblk->layers[layno]; - int n; - int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */ - /* Correction of the matrix of coefficient to include the IMSB information */ - - if (layno == 0) - { - value = matrice[layno][resno][bandno]; - if (imsb >= value) - value = 0; - else - value -= imsb; - } - else - { - value = - matrice[layno][resno][bandno] - - matrice[layno - 1][resno][bandno]; - if (imsb >= matrice[layno - 1][resno][bandno]) - { - value -= - (imsb - matrice[layno - 1][resno][bandno]); - if (value < 0) - value = 0; - } - } - - if (layno == 0) - cblk->numpassesinlayers = 0; - - n = cblk->numpassesinlayers; - if (cblk->numpassesinlayers == 0) - { - if (value != 0) - n = 3 * value - 2 + cblk->numpassesinlayers; - else - n = cblk->numpassesinlayers; - } - else - n = 3 * value + cblk->numpassesinlayers; - - layer->numpasses = n - cblk->numpassesinlayers; - - if (!layer->numpasses) - continue; - - if (cblk->numpassesinlayers == 0) - { - layer->len = cblk->passes[n - 1].rate; - layer->data = cblk->data; - } - else - { - layer->len = - cblk->passes[n - 1].rate - - cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->data = - cblk->data + - cblk->passes[cblk->numpassesinlayers - 1].rate; - } - if (final) - cblk->numpassesinlayers = n; - } - } + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; + for (i = 0; i < tcd_tcp->numlayers; i++) { + for (j = 0; j < tilec->numresolutions; j++) { + for (k = 0; k < 3; k++) { + matrice[i][j][k] = + (int) (tcd_cp-> + matrice[i * tilec->numresolutions * 3 + + j * 3 + + k] * + (float) (tcd_img->comps[compno].prec / 16.0)); + }}} + + for (resno = 0; resno < tilec->numresolutions; resno++) { + tcd_resolution_t *res = &tilec->resolutions[resno]; + for (bandno = 0; bandno < res->numbands; bandno++) { + tcd_band_t *band = &res->bands[bandno]; + for (precno = 0; precno < res->pw * res->ph; precno++) { + tcd_precinct_t *prc = &band->precincts[precno]; + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + tcd_layer_t *layer = &cblk->layers[layno]; + int n; + int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */ + /* Correction of the matrix of coefficient to include the IMSB information */ + + if (layno == 0) { + value = matrice[layno][resno][bandno]; + if (imsb >= value) + value = 0; + else + value -= imsb; + } else { + value = + matrice[layno][resno][bandno] - + matrice[layno - 1][resno][bandno]; + if (imsb >= matrice[layno - 1][resno][bandno]) { + value -= (imsb - matrice[layno - 1][resno][bandno]); + if (value < 0) + value = 0; + } } + + if (layno == 0) + cblk->numpassesinlayers = 0; + + n = cblk->numpassesinlayers; + if (cblk->numpassesinlayers == 0) { + if (value != 0) + n = 3 * value - 2 + cblk->numpassesinlayers; + else + n = cblk->numpassesinlayers; + } else + n = 3 * value + cblk->numpassesinlayers; + + layer->numpasses = n - cblk->numpassesinlayers; + + if (!layer->numpasses) + continue; + + if (cblk->numpassesinlayers == 0) { + layer->len = cblk->passes[n - 1].rate; + layer->data = cblk->data; + } else { + layer->len = + cblk->passes[n - 1].rate - + cblk->passes[cblk->numpassesinlayers - 1].rate; + layer->data = + cblk->data + + cblk->passes[cblk->numpassesinlayers - 1].rate; + } + if (final) + cblk->numpassesinlayers = n; + } } + } } + } } -void -tcd_rateallocate_fixed () +void tcd_rateallocate_fixed() { int layno; - for (layno = 0; layno < tcd_tcp->numlayers; layno++) - { - tcd_makelayer_fixed (layno, 1); - } + for (layno = 0; layno < tcd_tcp->numlayers; layno++) { + tcd_makelayer_fixed(layno, 1); + } } -void -tcd_makelayer (int layno, double thresh, int final) +void tcd_makelayer(int layno, double thresh, int final) { int compno, resno, bandno, precno, cblkno, passno; tcd_tile->distolayer[layno] = 0; //add fixed_quality - for (compno = 0; compno < tcd_tile->numcomps; compno++) - { - tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; - for (resno = 0; resno < tilec->numresolutions; resno++) - { - tcd_resolution_t *res = &tilec->resolutions[resno]; - for (bandno = 0; bandno < res->numbands; bandno++) - { - tcd_band_t *band = &res->bands[bandno]; - for (precno = 0; precno < res->pw * res->ph; precno++) - { - tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) - { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - tcd_layer_t *layer = &cblk->layers[layno]; - int n; - - if (layno == 0) - { - cblk->numpassesinlayers = 0; - } - n = cblk->numpassesinlayers; - for (passno = cblk->numpassesinlayers; - passno < cblk->totalpasses; passno++) - { - int dr; - double dd; - tcd_pass_t *pass = &cblk->passes[passno]; - if (n == 0) - { - dr = pass->rate; - dd = pass->distortiondec; - } - else - { - dr = pass->rate - cblk->passes[n - 1].rate; - dd = pass->distortiondec - cblk->passes[n - - 1]. - distortiondec; - } - if (!dr) - { - if (dd) - n = passno + 1; - continue; - } - - if (dd / dr >= thresh) - n = passno + 1; - } - layer->numpasses = n - cblk->numpassesinlayers; - - if (!layer->numpasses) - { - layer->disto = 0; - continue; - } - - if (cblk->numpassesinlayers == 0) - { - layer->len = cblk->passes[n - 1].rate; - layer->data = cblk->data; - layer->disto = cblk->passes[n - 1].distortiondec; - } - else - { - layer->len = cblk->passes[n - 1].rate - - cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->data = - cblk->data + - cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->disto = - cblk->passes[n - 1].distortiondec - - cblk->passes[cblk->numpassesinlayers - - 1].distortiondec; - } - - tcd_tile->distolayer[layno] += layer->disto; //add fixed_quality - - if (final) - cblk->numpassesinlayers = n; - } - } + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; + for (resno = 0; resno < tilec->numresolutions; resno++) { + tcd_resolution_t *res = &tilec->resolutions[resno]; + for (bandno = 0; bandno < res->numbands; bandno++) { + tcd_band_t *band = &res->bands[bandno]; + for (precno = 0; precno < res->pw * res->ph; precno++) { + tcd_precinct_t *prc = &band->precincts[precno]; + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + tcd_layer_t *layer = &cblk->layers[layno]; + int n; + + if (layno == 0) { + cblk->numpassesinlayers = 0; + } + n = cblk->numpassesinlayers; + for (passno = cblk->numpassesinlayers; + passno < cblk->totalpasses; passno++) { + int dr; + double dd; + tcd_pass_t *pass = &cblk->passes[passno]; + if (n == 0) { + dr = pass->rate; + dd = pass->distortiondec; + } else { + dr = pass->rate - cblk->passes[n - 1].rate; + dd = pass->distortiondec - cblk->passes[n - + 1].distortiondec; + } + if (!dr) { + if (dd) + n = passno + 1; + continue; + } + + if (dd / dr >= thresh) + n = passno + 1; + } + layer->numpasses = n - cblk->numpassesinlayers; + + if (!layer->numpasses) { + layer->disto = 0; + continue; } + + if (cblk->numpassesinlayers == 0) { + layer->len = cblk->passes[n - 1].rate; + layer->data = cblk->data; + layer->disto = cblk->passes[n - 1].distortiondec; + } else { + layer->len = cblk->passes[n - 1].rate - + cblk->passes[cblk->numpassesinlayers - 1].rate; + layer->data = + cblk->data + + cblk->passes[cblk->numpassesinlayers - 1].rate; + layer->disto = + cblk->passes[n - 1].distortiondec - + cblk->passes[cblk->numpassesinlayers - 1].distortiondec; + } + + tcd_tile->distolayer[layno] += layer->disto; //add fixed_quality + + if (final) + cblk->numpassesinlayers = n; + } } + } } + } } -void -tcd_rateallocate (unsigned char *dest, int len, info_image * info_IM) +void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM) { int compno, resno, bandno, precno, cblkno, passno, layno; double min, max; @@ -1170,159 +1028,134 @@ tcd_rateallocate (unsigned char *dest, int len, info_image * info_IM) tcd_tile->nbpix = 0; //add fixed_quality - for (compno = 0; compno < tcd_tile->numcomps; compno++) - { - tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; - - tilec->nbpix = 0; - for (resno = 0; resno < tilec->numresolutions; resno++) - { - tcd_resolution_t *res = &tilec->resolutions[resno]; - for (bandno = 0; bandno < res->numbands; bandno++) - { - tcd_band_t *band = &res->bands[bandno]; - for (precno = 0; precno < res->pw * res->ph; precno++) - { - tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) - { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - for (passno = 0; passno < cblk->totalpasses; passno++) - { - tcd_pass_t *pass = &cblk->passes[passno]; - int dr; - double dd, rdslope; - if (passno == 0) - { - dr = pass->rate; - dd = pass->distortiondec; - } - else - { - dr = pass->rate - cblk->passes[passno - 1].rate; - dd = pass->distortiondec - - cblk->passes[passno - 1].distortiondec; - } - if (dr == 0) - { - continue; - } - - rdslope = dd / dr; - - if (rdslope < min) - { - min = rdslope; - } - if (rdslope > max) - { - max = rdslope; - } - } /* passno */ - - tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality - - tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality - - } /* cbklno */ - } /* precno */ - } /* bandno */ - } /* resno */ - - maxSE += - (double) (((1 << tcd_img->comps[compno].prec) - - 1) * ((1 << tcd_img->comps[compno].prec) - - 1)) * (tilec->nbpix); - } /* compno */ + for (compno = 0; compno < tcd_tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; + + tilec->nbpix = 0; + for (resno = 0; resno < tilec->numresolutions; resno++) { + tcd_resolution_t *res = &tilec->resolutions[resno]; + for (bandno = 0; bandno < res->numbands; bandno++) { + tcd_band_t *band = &res->bands[bandno]; + for (precno = 0; precno < res->pw * res->ph; precno++) { + tcd_precinct_t *prc = &band->precincts[precno]; + for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + tcd_cblk_t *cblk = &prc->cblks[cblkno]; + for (passno = 0; passno < cblk->totalpasses; passno++) { + tcd_pass_t *pass = &cblk->passes[passno]; + int dr; + double dd, rdslope; + if (passno == 0) { + dr = pass->rate; + dd = pass->distortiondec; + } else { + dr = pass->rate - cblk->passes[passno - 1].rate; + dd = pass->distortiondec - + cblk->passes[passno - 1].distortiondec; + } + if (dr == 0) { + continue; + } + + rdslope = dd / dr; + + if (rdslope < min) { + min = rdslope; + } + if (rdslope > max) { + max = rdslope; + } + } /* passno */ + + tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality + + tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality + + } /* cbklno */ + } /* precno */ + } /* bandno */ + } /* resno */ + + maxSE += + (double) (((1 << tcd_img->comps[compno].prec) - + 1) * ((1 << tcd_img->comps[compno].prec) - + 1)) * (tilec->nbpix); + } /* compno */ /* add antonin index */ - if (info_IM->index_on) - { - info_tile *info_TL = &info_IM->tile[tcd_tileno]; - info_TL->nbpix = tcd_tile->nbpix; - info_TL->distotile = tcd_tile->distotile; - info_TL->thresh = - (double *) malloc (tcd_tcp->numlayers * sizeof (double)); - } + if (info_IM->index_on) { + info_tile *info_TL = &info_IM->tile[tcd_tileno]; + info_TL->nbpix = tcd_tile->nbpix; + info_TL->distotile = tcd_tile->distotile; + info_TL->thresh = + (double *) malloc(tcd_tcp->numlayers * sizeof(double)); + } /* dda */ - for (layno = 0; layno < tcd_tcp->numlayers; layno++) - { - volatile double lo = min; - volatile double hi = max; - volatile int success = 0; - volatile int maxlen = tcd_tcp->rates[layno] ? int_min (tcd_tcp->rates[layno], len) : len; //Mod antonin losslessbug - volatile double goodthresh; - volatile int i; - double distotarget; //add fixed_quality - - distotarget = tcd_tile->distotile - ((K * maxSE) / pow (10, tcd_tcp->distoratio[layno] / 10)); // add fixed_quality - - if (tcd_tcp->rates[layno]) - { - for (i = 0; i < 32; i++) - { - volatile double thresh = (lo + hi) / 2; - int l = 0; - double distoachieved = 0; // add fixed_quality - - tcd_makelayer (layno, thresh, 0); - - if (tcd_cp->fixed_quality) - { // add fixed_quality - distoachieved = - layno == - 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + - tcd_tile->distolayer[layno]; - if (distoachieved < distotarget) - { - hi = thresh; - continue; - } - lo = thresh; - } - else - { - l = - t2_encode_packets (tcd_img, tcd_cp, tcd_tileno, tcd_tile, - layno + 1, dest, maxlen, info_IM); - /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */ - if (l == -999) - { - lo = thresh; - continue; - } - hi = thresh; - } - - success = 1; - goodthresh = thresh; - } - } - else - { - success = 1; - goodthresh = min; + for (layno = 0; layno < tcd_tcp->numlayers; layno++) { + volatile double lo = min; + volatile double hi = max; + volatile int success = 0; + volatile int maxlen = tcd_tcp->rates[layno] ? int_min(tcd_tcp->rates[layno], len) : len; //Mod antonin losslessbug + volatile double goodthresh; + volatile int i; + double distotarget; //add fixed_quality + + distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10)); // add fixed_quality + + if (tcd_tcp->rates[layno]) { + for (i = 0; i < 32; i++) { + volatile double thresh = (lo + hi) / 2; + int l = 0; + double distoachieved = 0; // add fixed_quality + + tcd_makelayer(layno, thresh, 0); + + if (tcd_cp->fixed_quality) { // add fixed_quality + distoachieved = + layno == + 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + + tcd_tile->distolayer[layno]; + if (distoachieved < distotarget) { + hi = thresh; + continue; + } + lo = thresh; + } else { + l = + t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile, + layno + 1, dest, maxlen, info_IM); + /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */ + if (l == -999) { + lo = thresh; + continue; + } + hi = thresh; } - if (!success) - { - longjmp (j2k_error, 1); - } + success = 1; + goodthresh = thresh; + } + } else { + success = 1; + goodthresh = min; + } - if (info_IM->index_on) - { /* Threshold for Marcela Index */ - info_IM->tile[tcd_tileno].thresh[layno] = goodthresh; - } - tcd_makelayer (layno, goodthresh, 1); + if (!success) { + longjmp(j2k_error, 1); + } - cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; // add fixed_quality + if (info_IM->index_on) { /* Threshold for Marcela Index */ + info_IM->tile[tcd_tileno].thresh[layno] = goodthresh; } + tcd_makelayer(layno, goodthresh, 1); + + cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; // add fixed_quality + } } int -tcd_encode_tile_pxm (int tileno, unsigned char *dest, int len, - info_image * info_IM) +tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len, + info_image * info_IM) { int compno; int l, i; @@ -1336,165 +1169,146 @@ tcd_encode_tile_pxm (int tileno, unsigned char *dest, int len, tcd_tcp = &tcd_cp->tcps[tileno]; tile = tcd_tile; /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ - if (info_IM->index_on) - { - tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0 + if (info_IM->index_on) { + tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0 - for (i = 0; i < tilec_idx->numresolutions; i++) - { + for (i = 0; i < tilec_idx->numresolutions; i++) { - tcd_resolution_t *res_idx = &tilec_idx->resolutions[i]; + tcd_resolution_t *res_idx = &tilec_idx->resolutions[i]; - info_IM->tile[tileno].pw[i] = res_idx->pw; - info_IM->tile[tileno].ph[i] = res_idx->ph; + info_IM->tile[tileno].pw[i] = res_idx->pw; + info_IM->tile[tileno].ph[i] = res_idx->ph; - info_IM->tile[tileno].pdx[i] = tccp->prcw[i]; - info_IM->tile[tileno].pdy[i] = tccp->prch[i]; + info_IM->tile[tileno].pdx[i] = tccp->prcw[i]; + info_IM->tile[tileno].pdy[i] = tccp->prch[i]; - } } + } /* << INDEX */ /*---------------TILE-------------------*/ - time7 = clock (); + time7 = clock(); + + for (compno = 0; compno < tile->numcomps; compno++) { + FILE *src; + char tmp[256]; + int k; + unsigned char elmt; + int i, j; + int tw, w; + tcd_tilecomp_t *tilec = &tile->comps[compno]; + int adjust = + tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno]. + prec - 1); + int offset_x, offset_y; + + offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx); + offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy); + tw = tilec->x1 - tilec->x0; + w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx); + sprintf(tmp, "Compo%d", compno); /* component file */ + src = fopen(tmp, "rb"); + if (!src) { + fprintf(stderr, "failed to open %s for reading\n", tmp); + return 1; + } - for (compno = 0; compno < tile->numcomps; compno++) - { - FILE *src; - char tmp[256]; - int k; - unsigned char elmt; - int i, j; - int tw, w; - tcd_tilecomp_t *tilec = &tile->comps[compno]; - int adjust = - tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno]. - prec - 1); - int offset_x, offset_y; - - offset_x = int_ceildiv (tcd_img->x0, tcd_img->comps[compno].dx); - offset_y = int_ceildiv (tcd_img->y0, tcd_img->comps[compno].dy); - tw = tilec->x1 - tilec->x0; - w = int_ceildiv (tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx); - sprintf (tmp, "Compo%d", compno); /* component file */ - src = fopen (tmp, "rb"); - if (!src) - { - fprintf (stderr, "failed to open %s for reading\n", tmp); - return 1; + /* read the Compo file to extract data of the tile */ + k = 0; + fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w, + SEEK_SET); + k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w; + for (j = tilec->y0; j < tilec->y1; j++) { + for (i = tilec->x0; i < tilec->x1; i++) { + if (tcd_tcp->tccps[compno].qmfbid == 1) { + elmt = fgetc(src); + tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = + elmt - adjust; + k++; + } else if (tcd_tcp->tccps[compno].qmfbid == 0) { + elmt = fgetc(src); + tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = + (elmt - adjust) << 13; + k++; } + } + fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k, + SEEK_CUR); + k = tilec->x0 - offset_x + (j + 1 - offset_y) * w; - /* read the Compo file to extract data of the tile */ - k = 0; - fseek (src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w, - SEEK_SET); - k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w; - for (j = tilec->y0; j < tilec->y1; j++) - { - for (i = tilec->x0; i < tilec->x1; i++) - { - if (tcd_tcp->tccps[compno].qmfbid == 1) - { - elmt = fgetc (src); - tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = - elmt - adjust; - k++; - } - else if (tcd_tcp->tccps[compno].qmfbid == 0) - { - elmt = fgetc (src); - tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = - (elmt - adjust) << 13; - k++; - } - } - fseek (src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k, - SEEK_CUR); - k = tilec->x0 - offset_x + (j + 1 - offset_y) * w; - - } - fclose (src); } + fclose(src); + } /*----------------MCT-------------------*/ - if (tcd_tcp->mct) - { - if (tcd_tcp->tccps[0].qmfbid == 0) - { - mct_encode_real (tile->comps[0].data, tile->comps[1].data, - tile->comps[2].data, - (tile->comps[0].x1 - - tile->comps[0].x0) * (tile->comps[0].y1 - - tile->comps[0].y0)); - } - else - { - mct_encode (tile->comps[0].data, tile->comps[1].data, + if (tcd_tcp->mct) { + if (tcd_tcp->tccps[0].qmfbid == 0) { + mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0)); - } + } else { + mct_encode(tile->comps[0].data, tile->comps[1].data, + tile->comps[2].data, + (tile->comps[0].x1 - + tile->comps[0].x0) * (tile->comps[0].y1 - + tile->comps[0].y0)); } + } /*----------------DWT---------------------*/ /* time3=clock(); */ - for (compno = 0; compno < tile->numcomps; compno++) - { - tcd_tilecomp_t *tilec = &tile->comps[compno]; - if (tcd_tcp->tccps[compno].qmfbid == 1) - { - dwt_encode (tilec->data, tilec->x1 - tilec->x0, + for (compno = 0; compno < tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tile->comps[compno]; + if (tcd_tcp->tccps[compno].qmfbid == 1) { + dwt_encode(tilec->data, tilec->x1 - tilec->x0, + tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1); + } else if (tcd_tcp->tccps[compno].qmfbid == 0) { + dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1); - } - else if (tcd_tcp->tccps[compno].qmfbid == 0) - { - dwt_encode_real (tilec->data, tilec->x1 - tilec->x0, - tilec->y1 - tilec->y0, tilec, - tilec->numresolutions - 1); - } } + } /*------------------TIER1-----------------*/ - t1_init_luts (); - t1_encode_cblks (tile, tcd_tcp); + t1_init_luts(); + t1_encode_cblks(tile, tcd_tcp); /*-----------RATE-ALLOCATE------------------*/ info_IM->index_write = 0; /* INDEX */ if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality /* Normal Rate/distortion allocation */ - tcd_rateallocate (dest, len, info_IM); + tcd_rateallocate(dest, len, info_IM); else /* Fixed layer allocation */ - tcd_rateallocate_fixed (); + tcd_rateallocate_fixed(); /*--------------TIER2------------------*/ info_IM->index_write = 1; /* INDEX */ - l = t2_encode_packets (tcd_img, tcd_cp, tileno, tile, - tcd_tcp->numlayers, dest, len, info_IM); + l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, + tcd_tcp->numlayers, dest, len, info_IM); /*---------------CLEAN-------------------*/ - time7 = clock () - time7; - printf ("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC, - (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC); + time7 = clock() - time7; + printf("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC, + (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC); /* cleaning memory */ - for (compno = 0; compno < tile->numcomps; compno++) - { - tilec = &tile->comps[compno]; - free (tilec->data); - } + for (compno = 0; compno < tile->numcomps; compno++) { + tilec = &tile->comps[compno]; + free(tilec->data); + } return l; } int -tcd_encode_tile_pgx (int tileno, unsigned char *dest, int len, - info_image * info_IM) +tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len, + info_image * info_IM) { int compno; int l, i; @@ -1509,141 +1323,120 @@ tcd_encode_tile_pgx (int tileno, unsigned char *dest, int len, tile = tcd_tile; /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ - if (info_IM->index_on) - { + if (info_IM->index_on) { - tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0 + tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0 - for (i = 0; i < tilec_idx->numresolutions; i++) - { + for (i = 0; i < tilec_idx->numresolutions; i++) { - tcd_resolution_t *res_idx = &tilec_idx->resolutions[i]; + tcd_resolution_t *res_idx = &tilec_idx->resolutions[i]; - info_IM->tile[tileno].pw[i] = res_idx->pw; + info_IM->tile[tileno].pw[i] = res_idx->pw; - info_IM->tile[tileno].ph[i] = res_idx->ph; + info_IM->tile[tileno].ph[i] = res_idx->ph; - info_IM->tile[tileno].pdx[i] = tccp->prcw[i]; + info_IM->tile[tileno].pdx[i] = tccp->prcw[i]; - info_IM->tile[tileno].pdy[i] = tccp->prch[i]; - - } + info_IM->tile[tileno].pdy[i] = tccp->prch[i]; } + } + /* << INDEX */ /*---------------TILE-------------------*/ - time = clock (); - - for (compno = 0; compno < tile->numcomps; compno++) - { - FILE *src; - char tmp[256]; - int k; - int elmt; - int i, j; - int tw, w; - tcd_tilecomp_t *tilec = &tile->comps[compno]; - int adjust = - tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno]. - prec - 1); - int offset_x, offset_y; - - offset_x = int_ceildiv (tcd_img->x0, tcd_img->comps[compno].dx); - offset_y = int_ceildiv (tcd_img->y0, tcd_img->comps[compno].dy); - tw = tilec->x1 - tilec->x0; - w = int_ceildiv (tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx); - sprintf (tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */ - src = fopen (tmp, "rb"); - if (!src) - { - fprintf (stderr, "failed to open %s for reading\n", tmp); - return 1; - } - /* Extract data from bandtile file limited to the current tile */ - k = 0; - while (k < tilec->x0 - offset_x) - { - k++; - fscanf (src, "%d", &elmt); - } + time = clock(); + + for (compno = 0; compno < tile->numcomps; compno++) { + FILE *src; + char tmp[256]; + int k; + int elmt; + int i, j; + int tw, w; + tcd_tilecomp_t *tilec = &tile->comps[compno]; + int adjust = + tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno]. + prec - 1); + int offset_x, offset_y; + + offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx); + offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy); + tw = tilec->x1 - tilec->x0; + w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx); + sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */ + src = fopen(tmp, "rb"); + if (!src) { + fprintf(stderr, "failed to open %s for reading\n", tmp); + return 1; + } + /* Extract data from bandtile file limited to the current tile */ + k = 0; + while (k < tilec->x0 - offset_x) { + k++; + fscanf(src, "%d", &elmt); + } - for (j = 0; j < tilec->y1 - tilec->y0; j++) - { - for (i = tilec->x0; i < tilec->x1; i++) - { - if (tcd_tcp->tccps[compno].qmfbid == 1) - { - fscanf (src, "%d", &elmt); - tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust; - k++; - } - else if (tcd_tcp->tccps[compno].qmfbid == 0) - { - fscanf (src, "%d", &elmt); - tilec->data[i - tilec->x0 + (j) * tw] = - (elmt - adjust) << 13; - k++; - } - } - while (k < tilec->x0 - offset_x + (j + 1) * w) - { - k++; - fscanf (src, "%d", &elmt); - } + for (j = 0; j < tilec->y1 - tilec->y0; j++) { + for (i = tilec->x0; i < tilec->x1; i++) { + if (tcd_tcp->tccps[compno].qmfbid == 1) { + fscanf(src, "%d", &elmt); + tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust; + k++; + } else if (tcd_tcp->tccps[compno].qmfbid == 0) { + fscanf(src, "%d", &elmt); + tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13; + k++; } - fclose (src); + } + while (k < tilec->x0 - offset_x + (j + 1) * w) { + k++; + fscanf(src, "%d", &elmt); + } } + fclose(src); + } /*----------------MCT-------------------*/ - if (tcd_tcp->mct) - { - if (tcd_tcp->tccps[0].qmfbid == 0) - { - mct_encode_real (tile->comps[0].data, tile->comps[1].data, - tile->comps[2].data, - (tile->comps[0].x1 - - tile->comps[0].x0) * (tile->comps[0].y1 - - tile->comps[0].y0)); - } - else - { - mct_encode (tile->comps[0].data, tile->comps[1].data, + if (tcd_tcp->mct) { + if (tcd_tcp->tccps[0].qmfbid == 0) { + mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0)); - } + } else { + mct_encode(tile->comps[0].data, tile->comps[1].data, + tile->comps[2].data, + (tile->comps[0].x1 - + tile->comps[0].x0) * (tile->comps[0].y1 - + tile->comps[0].y0)); } + } /*----------------DWT---------------------*/ - for (compno = 0; compno < tile->numcomps; compno++) - { - tcd_tilecomp_t *tilec = &tile->comps[compno]; - if (tcd_tcp->tccps[compno].qmfbid == 1) - { - dwt_encode (tilec->data, tilec->x1 - tilec->x0, + for (compno = 0; compno < tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tile->comps[compno]; + if (tcd_tcp->tccps[compno].qmfbid == 1) { + dwt_encode(tilec->data, tilec->x1 - tilec->x0, + tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1); + } else if (tcd_tcp->tccps[compno].qmfbid == 0) { + dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1); - } - else if (tcd_tcp->tccps[compno].qmfbid == 0) - { - dwt_encode_real (tilec->data, tilec->x1 - tilec->x0, - tilec->y1 - tilec->y0, tilec, - tilec->numresolutions - 1); - } } + } /*------------------TIER1-----------------*/ - t1_init_luts (); - t1_encode_cblks (tile, tcd_tcp); + t1_init_luts(); + t1_encode_cblks(tile, tcd_tcp); /*-----------RATE-ALLOCATE------------------*/ @@ -1653,36 +1446,34 @@ tcd_encode_tile_pgx (int tileno, unsigned char *dest, int len, /* Normal Rate/distortion allocation */ - tcd_rateallocate (dest, len, info_IM); + tcd_rateallocate(dest, len, info_IM); else /* Fixed layer allocation */ - tcd_rateallocate_fixed (); + tcd_rateallocate_fixed(); /*--------------TIER2------------------*/ info_IM->index_write = 1; /* INDEX */ - l = t2_encode_packets (tcd_img, tcd_cp, tileno, tile, - tcd_tcp->numlayers, dest, len, info_IM); + l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, + tcd_tcp->numlayers, dest, len, info_IM); /*---------------CLEAN-------------------*/ - time = clock () - time; - printf ("total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC, - (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC); + time = clock() - time; + printf("total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC, + (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC); - for (compno = 0; compno < tile->numcomps; compno++) - { - tilec = &tile->comps[compno]; - free (tilec->data); - } + for (compno = 0; compno < tile->numcomps; compno++) { + tilec = &tile->comps[compno]; + free(tilec->data); + } return l; } -int -tcd_decode_tile (unsigned char *src, int len, int tileno) +int tcd_decode_tile(unsigned char *src, int len, int tileno) { int l; int compno; @@ -1695,160 +1486,141 @@ tcd_decode_tile (unsigned char *src, int len, int tileno) tcd_tcp = &tcd_cp->tcps[tileno]; tile = tcd_tile; - time = clock (); + time = clock(); - fprintf (stderr, "tile decoding time %d/%d: ", tileno + 1, - tcd_cp->tw * tcd_cp->th); + fprintf(stderr, "tile decoding time %d/%d: ", tileno + 1, + tcd_cp->tw * tcd_cp->th); /*--------------TIER2------------------*/ - l = t2_decode_packets (src, len, tcd_img, tcd_cp, tileno, tile); + l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile); - if (l == -999) - { - eof = 1; - fprintf (stderr, "tcd_decode: incomplete bistream\n"); - } + if (l == -999) { + eof = 1; + fprintf(stderr, "tcd_decode: incomplete bistream\n"); + } /*------------------TIER1-----------------*/ - t1_init_luts (); - t1_decode_cblks (tile, tcd_tcp); + t1_init_luts(); + t1_decode_cblks(tile, tcd_tcp); /*----------------DWT---------------------*/ - for (compno = 0; compno < tile->numcomps; compno++) - { - tcd_tilecomp_t *tilec = &tile->comps[compno]; - if (tcd_cp->reduce_on == 1) - { - tcd_img->comps[compno].resno_decoded = - tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1; - } + for (compno = 0; compno < tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tile->comps[compno]; + if (tcd_cp->reduce_on == 1) { + tcd_img->comps[compno].resno_decoded = + tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1; + } - if (tcd_tcp->tccps[compno].qmfbid == 1) - { - dwt_decode (tilec->data, tilec->x1 - tilec->x0, + if (tcd_tcp->tccps[compno].qmfbid == 1) { + dwt_decode(tilec->data, tilec->x1 - tilec->x0, + tilec->y1 - tilec->y0, tilec, + tilec->numresolutions - 1, + tilec->numresolutions - 1 - + tcd_img->comps[compno].resno_decoded); + } else { + dwt_decode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1, tilec->numresolutions - 1 - tcd_img->comps[compno].resno_decoded); - } - else - { - dwt_decode_real (tilec->data, tilec->x1 - tilec->x0, - tilec->y1 - tilec->y0, tilec, - tilec->numresolutions - 1, - tilec->numresolutions - 1 - - tcd_img->comps[compno].resno_decoded); - } - - if (tile->comps[compno].numresolutions > 0) - tcd_img->comps[compno].factor = - tile->comps[compno].numresolutions - - (tcd_img->comps[compno].resno_decoded + 1); } + if (tile->comps[compno].numresolutions > 0) + tcd_img->comps[compno].factor = + tile->comps[compno].numresolutions - + (tcd_img->comps[compno].resno_decoded + 1); + } + /*----------------MCT-------------------*/ - if (tcd_tcp->mct) - { - if (tcd_tcp->tccps[0].qmfbid == 1) - { - mct_decode (tile->comps[0].data, tile->comps[1].data, + if (tcd_tcp->mct) { + if (tcd_tcp->tccps[0].qmfbid == 1) { + mct_decode(tile->comps[0].data, tile->comps[1].data, + tile->comps[2].data, + (tile->comps[0].x1 - + tile->comps[0].x0) * (tile->comps[0].y1 - + tile->comps[0].y0)); + } else { + mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0)); - } - else - { - mct_decode_real (tile->comps[0].data, tile->comps[1].data, - tile->comps[2].data, - (tile->comps[0].x1 - - tile->comps[0].x0) * (tile->comps[0].y1 - - tile->comps[0].y0)); - } } + } /*---------------TILE-------------------*/ - for (compno = 0; compno < tile->numcomps; compno++) - { - tcd_tilecomp_t *tilec = &tile->comps[compno]; - tcd_resolution_t *res = - &tilec->resolutions[tcd_img->comps[compno].resno_decoded]; - int adjust = - tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno]. - prec - 1); - int min = - tcd_img->comps[compno]. - sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0; - int max = - tcd_img->comps[compno]. - sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) - - 1 : (1 << tcd_img->comps[compno].prec) - 1; - - int tw = tilec->x1 - tilec->x0; - int w = tcd_img->comps[compno].w; - - int i, j; - int offset_x = int_ceildivpow2 (tcd_img->comps[compno].x0, - tcd_img->comps[compno].factor); - int offset_y = int_ceildivpow2 (tcd_img->comps[compno].y0, - tcd_img->comps[compno].factor); - - for (j = res->y0; j < res->y1; j++) - { - for (i = res->x0; i < res->x1; i++) - { - - int v; - - double tmp = - (double) tilec->data[i - res->x0 + (j - res->y0) * tw]; - if (tcd_tcp->tccps[compno].qmfbid == 1) - { - v = (int) tmp; - } - else - { - - //v = (int) tmp >> 13; - - //Mod antonin : multbug1 - v = - (int) ((fabs (tmp / 8192.0) >= - floor (fabs (tmp / 8192.0)) + - 0.5) ? fabs (tmp / 8192.0) + - 1.0 : fabs (tmp / 8192.0)); - - v = (tmp < 0) ? -v : v; - - //doM - } - v += adjust; - - tcd_img->comps[compno].data[(i - offset_x) + - (j - offset_y) * w] = - int_clamp (v, min, max); - } + for (compno = 0; compno < tile->numcomps; compno++) { + tcd_tilecomp_t *tilec = &tile->comps[compno]; + tcd_resolution_t *res = + &tilec->resolutions[tcd_img->comps[compno].resno_decoded]; + int adjust = + tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno]. + prec - 1); + int min = + tcd_img->comps[compno]. + sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0; + int max = + tcd_img->comps[compno]. + sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) - + 1 : (1 << tcd_img->comps[compno].prec) - 1; + + int tw = tilec->x1 - tilec->x0; + int w = tcd_img->comps[compno].w; + + int i, j; + int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0, + tcd_img->comps[compno].factor); + int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0, + tcd_img->comps[compno].factor); + + for (j = res->y0; j < res->y1; j++) { + for (i = res->x0; i < res->x1; i++) { + + int v; + + double tmp = + (double) tilec->data[i - res->x0 + (j - res->y0) * tw]; + if (tcd_tcp->tccps[compno].qmfbid == 1) { + v = (int) tmp; + } else { + + //v = (int) tmp >> 13; + + //Mod antonin : multbug1 + v = + (int) ((fabs(tmp / 8192.0) >= + floor(fabs(tmp / 8192.0)) + + 0.5) ? fabs(tmp / 8192.0) + 1.0 : fabs(tmp / 8192.0)); + + v = (tmp < 0) ? -v : v; + + //doM } + v += adjust; + + tcd_img->comps[compno].data[(i - offset_x) + + (j - offset_y) * w] = + int_clamp(v, min, max); + } } + } - time = clock () - time; - fprintf (stderr, "total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC, - (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC); + time = clock() - time; + fprintf(stderr, "total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC, + (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC); - for (compno = 0; compno < tile->numcomps; compno++) - { - free (tcd_image.tiles[tileno].comps[compno].data); - } + for (compno = 0; compno < tile->numcomps; compno++) { + free(tcd_image.tiles[tileno].comps[compno].data); + } - if (eof) - { - longjmp (j2k_error, 1); - } + if (eof) { + longjmp(j2k_error, 1); + } return l; } |
