Update for version 0.8
authorYannick Verschueren <unknown@unknown>
Fri, 13 Feb 2004 09:47:40 +0000 (09:47 +0000)
committerYannick Verschueren <unknown@unknown>
Fri, 13 Feb 2004 09:47:40 +0000 (09:47 +0000)
21 files changed:
codec/image_to_j2k.c
codec/j2k_to_image.c
libopenjpeg/bio.h
libopenjpeg/cio.c
libopenjpeg/cio.h
libopenjpeg/dwt.c
libopenjpeg/dwt.h
libopenjpeg/int.c
libopenjpeg/j2k.h
libopenjpeg/mqc.c
libopenjpeg/mqc.h
libopenjpeg/pi.c
libopenjpeg/pi.h
libopenjpeg/raw.c
libopenjpeg/raw.h
libopenjpeg/t1.c
libopenjpeg/t2.c
libopenjpeg/t2.h
libopenjpeg/tcd.c
libopenjpeg/tcd.h
libopenjpeg/tgt.h

index 3ca412b81a7694e886f40e035d14f22e591a60a5..6ef2953b9d51431417edbc3fd82d6612f623f472 100644 (file)
@@ -30,7 +30,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
-#include <unistd.h>
 #include <string.h>
 #ifndef DONT_HAVE_GETOPT
 #include <getopt.h>
@@ -176,32 +175,37 @@ void help_display()
 
 int give_progression(char progression[4])
 {
-       if (progression[0] == 'L' && progression[1] == 'R'
-                       && progression[2] == 'C' && progression[3] == 'P') {
-               return 0;
-       } else {
-               if (progression[0] == 'R' && progression[1] == 'L'
-                               && progression[2] == 'C' && progression[3] == 'P') {
-                       return 1;
-               } else {
-                       if (progression[0] == 'R' && progression[1] == 'P'
-                                       && progression[2] == 'C' && progression[3] == 'L') {
-                               return 2;
-                       } else {
-                               if (progression[0] == 'P' && progression[1] == 'C'
-                                               && progression[2] == 'R' && progression[3] == 'L') {
-                                       return 3;
-                               } else {
-                                       if (progression[0] == 'C' && progression[1] == 'P'
-                                                       && progression[2] == 'R' && progression[3] == 'L') {
-                                               return 4;
-                                       } else {
-                                               return -1;
-                                       }
-                               }
+  if (progression[0] == 'L' && progression[1] == 'R' && progression[2] == 'C' && progression[3] == 'P') 
+    {
+      return 0;
+    } else 
+      {
+       if (progression[0] == 'R' && progression[1] == 'L' && progression[2] == 'C' && progression[3] == 'P') 
+         {
+           return 1;
+         } else 
+           {
+             if (progression[0] == 'R' && progression[1] == 'P' && progression[2] == 'C' && progression[3] == 'L') 
+               {
+                 return 2;
+               } else 
+                 {
+                   if (progression[0] == 'P' && progression[1] == 'C' && progression[2] == 'R' && progression[3] == 'L') 
+                     {
+                       return 3;
+                     } else 
+                       {
+                         if (progression[0] == 'C' && progression[1] == 'P' && progression[2] == 'R' && progression[3] == 'L') 
+                           {
+                             return 4;
+                           } else 
+                             {
+                               return -1;
+                             }
                        }
-               }
-       }
+                 }
+           }
+      }
 }
 
 double dwt_norms_97[4][10] = {
@@ -240,42 +244,38 @@ void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec)
                resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1;
                orient = bandno == 0 ? 0 : (bandno - 1) % 3 + 1;
                level = tccp->numresolutions - 1 - resno;
-               gain =
-                       tccp->qmfbid == 0 ? 0 : (orient ==
-                                                                                                                        0 ? 0 : (orient == 1
-                                                                                                                                                               || orient == 2 ? 1 : 2));
+               gain = tccp->qmfbid == 0 ? 0 : (orient == 0 ? 0 : (orient == 1 || orient == 2 ? 1 : 2));
                if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
                        stepsize = 1.0;
                } else {
                        double norm = dwt_norms_97[orient][level];
                        stepsize = (1 << (gain + 1)) / norm;
                }
-               encode_stepsize((int) floor(stepsize * 8192.0), prec + gain,
-                                                                               &tccp->stepsizes[bandno].expn,
-                                                                               &tccp->stepsizes[bandno].mant);
+               encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno].expn,
+                               &tccp->stepsizes[bandno].mant);
        }
 }
 
 int main(int argc, char **argv)
 {
        int len;
-       int NumResolution, numD_min;    /* NumResolution : number of resolution */
-       int Tile_arg;                                                                   /* Tile_arg = 0 (pas en argument ou = 1 (entre en argument) */
-       int CSty;                                                                                       /* CSty : coding style */
-       int Prog_order;                                                         /* progression order (default LRCP) */
+       int NumResolution, numD_min;    /*   NumResolution : number of resolution                     */
+       int Tile_arg;                   /*   Tile_arg = 0 (not in argument) ou = 1 (in argument)      */
+       int CSty;                       /*   CSty : coding style                                      */
+       int Prog_order;                 /*   progression order (default LRCP)                         */
        char progression[4];
-       int numpocs, numpocs_tile;              /* Number of progression order change (POC) default 0 */
-       int prcw_init, prch_init;                       /* Initialisation precincts' size */
-       int cblockw_init, cblockh_init; /* Initialisation codeblocks' size */
-       int mode, value;                                                        /* mode switch (cblk_style) */
-       int subsampling_dx, subsampling_dy;     /* subsampling value for dx and dy          */
-       int ROI_compno, ROI_shift;              /* region of interrest */
-       int Dim[2];                                                                             /* portion of the image coded */
-       int TX0, TY0;                                                                   /* tile off-set */
+       int numpocs, numpocs_tile;      /*   Number of progression order change (POC) default 0       */
+       int prcw_init, prch_init;       /*   Initialisation precincts' size                           */
+       int cblockw_init, cblockh_init; /*   Initialisation codeblocks' size                          */
+       int mode, value;                /*   Mode switch (cblk_style)                                 */
+       int subsampling_dx, subsampling_dy;     /* subsampling value for dx and dy                    */
+       int ROI_compno, ROI_shift;      /*   region of interrest                                      */
+       int Dim[2];                     /*   portion of the image coded                               */
+       int TX0, TY0;                   /*   tile off-set                                             */
        j2k_image_t img;
-       j2k_cp_t cp, cp_init;                                   /* cp_init is used to initialise in multiple tiles  */
-       j2k_tcp_t *tcp, *tcp_init;              /* tcp_init is used to initialise in multiple tile */
-       j2k_poc_t POC[32];                                              /* POC : used in case of Progression order change */
+       j2k_cp_t cp, cp_init;           /*   cp_init is used to initialise in multiple tiles          */
+       j2k_tcp_t *tcp, *tcp_init;      /*   tcp_init is used to initialise in multiple tile          */
+       j2k_poc_t POC[32];              /*   POC : used in case of Progression order change           */
        j2k_poc_t *tcp_poc;
        j2k_tccp_t *tccp;
        int i, tileno, j;
@@ -299,7 +299,7 @@ int main(int argc, char **argv)
        mode = 0;
        subsampling_dx = 1;
        subsampling_dy = 1;
-       ROI_compno = -1;                                                        /* no ROI */
+       ROI_compno = -1;        /* no ROI */
        ROI_shift = 0;
        Dim[0] = 0;
        Dim[1] = 0;
@@ -321,52 +321,44 @@ int main(int argc, char **argv)
                if (c == -1)
                        break;
                switch (c) {
-               case 'i':                                                                       /* IN fill */
+               case 'i':       /* IN fill */
                        infile = optarg;
                        s = optarg;
-                       while (*s && *s != '.') {
+                       while (*s) {
                                s++;
                        }
-                       s++;
-                       S1 = *s;
-                       s++;
-                       S2 = *s;
-                       s++;
+                       s--;
                        S3 = *s;
+                       s--;
+                       S2 = *s;
+                       s--;
+                       S1 = *s;
 
-                       if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
-                                       || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
+                       if ((S1 == 'p' && S2 == 'g' && S3 == 'x') || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
                                cp.image_type = 0;
                                break;
                        }
 
-                       if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
-                                       || (S1 == 'P' && S2 == 'N' && S3 == 'M') || (S1 == 'p'
-                                                                                                                                                                                                                        && S2 == 'g'
-                                                                                                                                                                                                                        && S3 == 'm')
-                                       || (S1 == 'P' && S2 == 'G' && S3 == 'M') || (S1 == 'P'
-                                                                                                                                                                                                                        && S2 == 'P'
-                                                                                                                                                                                                                        && S3 == 'M')
-                                       || (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
+                       if ((S1 == 'p' && S2 == 'n' && S3 == 'm')|| (S1 == 'P' && S2 == 'N' && S3 == 'M') 
+                           || (S1 == 'p' && S2 == 'g' && S3 == 'm') || (S1 == 'P' && S2 == 'G' && S3 == 'M') 
+                           || (S1 == 'P' && S2 == 'P' && S3 == 'M') || (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
                                cp.image_type = 1;
                                break;
                        }
 
-                       if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
-                                       || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
+                       if ((S1 == 'b' && S2 == 'm' && S3 == 'p') || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
                                cp.image_type = 2;
                                break;
                        }
-                       fprintf(stderr,
-                                                       "!! Unrecognized format for infile [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n");
+                       fprintf(stderr, "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",S1,S2,S3);
                        return 1;
                        break;
                        /* ----------------------------------------------------- */
-               case 'o':                                                                       /* OUT fill */
+               case 'o':       /* OUT fill */
                        outfile = optarg;
                        break;
                        /* ----------------------------------------------------- */
-               case 'r':                                                                       /* rates */
+               case 'r':       /* rates rates/distorsion*/
                        s = optarg;
                        while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers]) == 1) {
                                tcp_init->numlayers++;
@@ -378,88 +370,109 @@ int main(int argc, char **argv)
                                s++;
                        }
                        cp.disto_alloc = 1;
+                       cp.matrice = NULL;
                        break;
                        /* ----------------------------------------------------- */
-               case 'q':                                                                       /* rates */
-                       s = optarg;
-                       sscanf(s, "%d", &tcp_init->numlayers);
-                       for (i = 0; i < tcp_init->numlayers; i++) {
-                               tcp_init->rates[i] = 1;
+               case 'q':       /* rates fixed */
+                 s=optarg;
+                 sscanf(s, "%d",&tcp_init->numlayers);
+                 s++;
+                 if (tcp_init->numlayers>9) s++;
+                 cp.matrice=(int*)malloc(tcp_init->numlayers*NumResolution*3*sizeof(int));
+                 s=s+2;
+                 for(i=0;i<tcp_init->numlayers;i++)
+                   {
+                     tcp_init->rates[i]=1;
+                     sscanf(s, "%d,", &cp.matrice[i*NumResolution*3]);
+                     s+=2;
+                     if (cp.matrice[i*NumResolution*3]>9) s++;
+                     cp.matrice[i*NumResolution*3+1]=0;
+                     cp.matrice[i*NumResolution*3+2]=0;
+                     for (j=1;j<NumResolution;j++)
+                       {
+                         sscanf(s, "%d,%d,%d", &cp.matrice[i*NumResolution*3+j*3+0],&cp.matrice[i*NumResolution*3+j*3+1],&cp.matrice[i*NumResolution*3+j*3+2]);
+                         s+=6;
+                         if (cp.matrice[i*NumResolution*3+j*3]>9) s++;
+                         if (cp.matrice[i*NumResolution*3+j*3+1]>9) s++;
+                         if (cp.matrice[i*NumResolution*3+j*3+2]>9) s++;  
                        }
-                       cp.fixed_alloc = 1;
-                       break;
-                       /* ----------------------------------------------------- */
-               case 't':                                                                       /* tiles */
+                     if (i<tcp_init->numlayers-1) s++;
+                   }
+                 cp.fixed_alloc=1;
+                 break;
+                       /* ----------------------------------------------------- */
+               case 't':       /* tiles */
                        sscanf(optarg, "%d,%d", &cp.tdx, &cp.tdy);
                        Tile_arg = 1;
                        break;
                        /* ----------------------------------------------------- */
-               case 'n':                                                                       /* resolution */
+               case 'n':       /* resolution */
                        sscanf(optarg, "%d", &NumResolution);
                        break;
                        /* ----------------------------------------------------- */
-               case 'c':                                                                       /* precinct dimension */
+               case 'c':       /* precinct dimension */
                        sscanf(optarg, "%d,%d", &prcw_init, &prch_init);
                        CSty |= 0x01;
                        break;
                        /* ----------------------------------------------------- */
-               case 'b':                                                                       /* code-block dimension */
+               case 'b':       /* code-block dimension */
                        sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
                        if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
                                        || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
-                               fprintf(stderr,
-                                                               "!! Size of code_block error (option -b) !!\n\nRestriction :\n    * width*height<=4096\n    * 4<=width,height<= 1024\n\n");
+                               fprintf(stderr,"!! Size of code_block error (option -b) !!\n\nRestriction :\n    * width*height<=4096\n    * 4<=width,height<= 1024\n\n");
                                return 1;
                        }
                        break;
                        /* ----------------------------------------------------- */
-               case 'x':                                                                       /* creation of index file */
-                       index = optarg;
+               case 'x':       /* creation of index file */
+                       index = optarg;
                        img.index_on = 1;
                        break;
                        /* ----------------------------------------------------- */
-               case 'p':                                                                       /* progression order */
-                       sscanf(optarg, "%s", progression);
+               case 'p':       /* progression order */
+                       s = optarg;
+                       for (i=0; i<4; i++)
+                         {
+                           progression[i] = *s;
+                           s++;
+                         }
                        Prog_order = give_progression(progression);
-                       if (Prog_order == -1) {
-                               fprintf(stderr,
-                                                               "Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
-                               return 1;
+                       
+                       if (Prog_order == -1) {
+                         fprintf(stderr,"Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
+                         return 1;
                        }
                        break;
                        /* ----------------------------------------------------- */
-               case 's':                                                                       /* subsampling factor */
+               case 's':       /* subsampling factor */
                        if (sscanf(optarg, "%d,%d", &subsampling_dx, &subsampling_dy) != 2) {
-                               fprintf(stderr,
-                                                               "'-s' sub-sampling argument error !  [-s dx,dy]\n");
+                               fprintf(stderr,"'-s' sub-sampling argument error !  [-s dx,dy]\n");
                                return 1;
                        }
                        break;
                        /* ----------------------------------------------------- */
-               case 'd':                                                                       /* coordonnate of the reference grid */
+               case 'd':       /* coordonnate of the reference grid */
                        if (sscanf(optarg, "%d,%d", &Dim[0], &Dim[1]) != 2) {
-                               fprintf(stderr,
-                                                               "-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
+                               fprintf(stderr,"-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
                                return 1;
                        }
                        break;
                        /* ----------------------------------------------------- */
-               case 'h':                                                                       /* Display an help description */
+               case 'h':       /* Display an help description */
                        help_display();
                        return 0;
                        break;
                        /* ----------------------------------------------------- */
-               case 'P':                                                                       /* POC */
+               case 'P':       /* POC */
                        fprintf(stderr, "/----------------------------------\\\n");
                        fprintf(stderr, "|  POC option not fully tested !!  |\n");
                        fprintf(stderr, "\\----------------------------------/\n");
 
                        s = optarg;
-                       while (sscanf
-                                                (s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
-                                                       &POC[numpocs].resno0, &POC[numpocs].compno0,
-                                                       &POC[numpocs].layno1, &POC[numpocs].resno1,
-                                                       &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
+                       while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
+                                     &POC[numpocs].resno0, &POC[numpocs].compno0,
+                                     &POC[numpocs].layno1, &POC[numpocs].resno1,
+                                     &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
                                POC[numpocs].prg = give_progression(POC[numpocs].progorder);
                                /* POC[numpocs].tile; */
                                numpocs++;
@@ -472,15 +485,15 @@ int main(int argc, char **argv)
                        }
                        break;
                        /* ------------------------------------------------------ */
-               case 'S':                                                                       /* SOP marker */
+               case 'S':       /* SOP marker */
                        CSty |= 0x02;
                        break;
                        /* ------------------------------------------------------ */
-               case 'E':                                                                       /* EPH marker */
+               case 'E':       /* EPH marker */
                        CSty |= 0x04;
                        break;
                        /* ------------------------------------------------------ */
-               case 'M':                                                                       /* Mode switch pas tous au point !! */
+               case 'M':       /* Mode switch pas tous au point !! */
                        if (sscanf(optarg, "%d", &value) == 1) {
                                for (i = 0; i <= 5; i++) {
                                        int cache = value & (1 << i);
@@ -490,27 +503,25 @@ int main(int argc, char **argv)
                        }
                        break;
                        /* ------------------------------------------------------ */
-               case 'R':                                                                       /* ROI */
+               case 'R':       /* ROI */
                        if (sscanf(optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2) {
-                               fprintf(stderr,
-                                                               "ROI error !! [-ROI:c='compno',U='shift']\n");
+                               fprintf(stderr,"ROI error !! [-ROI:c='compno',U='shift']\n");
                                return 1;
                        }
                        break;
                        /* ------------------------------------------------------ */
-               case 'T':                                                                       /* Tile offset */
+               case 'T':       /* Tile offset */
                        if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
-                               fprintf(stderr,
-                                                               "-T 'tile offset' argument error !! [-T X0,Y0]");
+                               fprintf(stderr,"-T 'tile offset' argument error !! [-T X0,Y0]");
                                return 1;
                        }
                        break;
                        /* ------------------------------------------------------ */
-               case 'C':                                                                       /* Add a comment */
+               case 'C':       /* Add a comment */
                        cp.comment = optarg;
                        break;
                        /* ------------------------------------------------------ */
-               case 'I':                                                                       /* reversible or not */
+               case 'I':       /* reversible or not */
                        ir = 1;
                        break;
                        /* ------------------------------------------------------ */
@@ -525,14 +536,12 @@ int main(int argc, char **argv)
        /* Error messages */
        /* -------------- */
        if (!infile || !outfile) {
-               fprintf(stderr,
-                                               "usage: pnmtoj2k -i pnm-file -o j2k-file\n");
+               fprintf(stderr, "usage: pnmtoj2k -i pnm-file -o j2k-file\n");
                return 1;
        }
 
        if (cp.disto_alloc & cp.fixed_alloc) {
-               fprintf(stderr,
-                                               "Error: option -r and -q can not be used together !!\n");
+               fprintf(stderr, "Error: option -r and -q can not be used together !!\n");
                return 1;
        }
 
@@ -544,32 +553,27 @@ int main(int argc, char **argv)
        }
 
        if (TX0 > Dim[0] || TY0 > Dim[1]) {
-               fprintf(stderr,
-                                               "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
-                                               TX0, Dim[0], TY0, Dim[1]);
+               fprintf(stderr, "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
+                       TX0, Dim[0], TY0, Dim[1]);
                return 1;
        }
 
        for (i = 0; i < numpocs; i++) {
                if (POC[i].prg == -1) {
-                       fprintf(stderr,
-                                                       "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
-                                                       i + 1);
+                       fprintf(stderr, "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
+                               i + 1);
                }
        }
 
        switch (cp.image_type) {
        case 0:
                if (Tile_arg) {
-                       if (!pgxtoimage
-                                       (infile, &img, cp.tdy, subsampling_dx, subsampling_dy, Dim,
-                                        cp)) {
+                       if (!pgxtoimage(infile, &img, cp.tdy, subsampling_dx, subsampling_dy, Dim, cp)) {
                                fprintf(stderr, "not a pgx file\n");
                                return 1;
                        }
                } else {
-                       if (!pgxtoimage
-                                       (infile, &img, -1, subsampling_dx, subsampling_dy, Dim, cp)) {
+                       if (!pgxtoimage(infile, &img, -1, subsampling_dx, subsampling_dy, Dim, cp)) {
                                fprintf(stderr, " not a pgx file\n");
                                return 1;
                        }
@@ -610,6 +614,12 @@ int main(int argc, char **argv)
                cp.tdy = img.y1 - cp.ty0;
        }
 
+       /* Initialization for PPM marker */
+       cp.ppm=0;
+       cp.ppm_data=NULL;
+       cp.ppm_previous=0;
+       cp.ppm_store=0;
+
        /* Init the mutiple tiles */
        /* ---------------------- */
        cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
@@ -623,9 +633,16 @@ int main(int argc, char **argv)
                tcp->csty = CSty;
                tcp->prg = Prog_order;
                tcp->mct = img.numcomps == 3 ? 1 : 0;
+               tcp->ppt = 0;
+               tcp->ppt_data=NULL; 
+               tcp->ppt_store=0;
+
                numpocs_tile = 0;
-               if (numpocs) {                                                  /* intialisation of POC */
-                       for (i = 0; i < numpocs; i++) {
+               tcp->POC=0;
+               if (numpocs) {
+                 /* intialisation of POC */
+                 tcp->POC=1;
+                 for (i = 0; i < numpocs; i++) {
                                if (tileno == POC[i].tile - 1 || POC[i].tile == -1) {
                                        tcp_poc = &tcp->pocs[numpocs_tile];
                                        tcp_poc->resno0 = POC[numpocs_tile].resno0;
@@ -691,27 +708,23 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       if (cp.image_type) {                                    /* PNM PGM PPM */
-               /* Remove the temporary file Compo */
-               /* ------------------------------- */
+       /* Remove the temporary files */
+       /* -------------------------- */
+       if (cp.image_type) { /* PNM PGM PPM */
                for (i = 0; i < img.numcomps; i++) {
-                       char tmp[256];
-                       sprintf(tmp, "Compo%d", i);
-                       if (unlink(tmp) == -1) {
-                               fprintf(stderr, "failed to kill %s file !\n",
-                                                               tmp);
+                       char tmp;
+                       sprintf(&tmp, "Compo%d", i);
+                       if (remove(&tmp) == -1) {
+                               fprintf(stderr, "failed to kill %s file !\n", &tmp);
                        }
                }
-       } else {                                                                                        /* PGX */
-
-               /* Kill temporary bandtile file for cleaning the memory space on user's disk */
+       } else { /* PGX */
                for (i = 0; i < cp.th; i++) {
                        char tmp;
                        sprintf(&tmp, "bandtile%d", i + 1);
 
-                       if (unlink(&tmp) == -1) {
-                               fprintf(stderr, "failed to kill %s file !\n",
-                                                               &tmp);
+                       if (remove(&tmp) == -1) {
+                               fprintf(stderr, "failed to kill %s file !\n", &tmp);
                        }
                }
        }
index 9c4b15767538073faeb2a1bfccb592e763bde3e1..5914bccdcb36b2c8fe397f195c073de3ec3e2b41 100644 (file)
@@ -1,6 +1,6 @@
 /* Copyright (c) 2001 David Janssens
- * Copyright (c) 2002 Yannick Verschueren
- * Copyright (c) 2002 Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2002-2003 Yannick Verschueren
+ * Copyright (c) 2002-2003 Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
  * 
  * All rights reserved. 
  * Redistribution and use in source and binary forms, with or without
@@ -30,7 +30,7 @@
 #include <openjpeg.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
+//#include <unistd.h>
 
 int ceildiv(int a, int b)
 {
@@ -40,13 +40,14 @@ int ceildiv(int a, int b)
 int main(int argc, char **argv)
 {
        FILE *f;
-       char *src;
+       char *src, *src_name;
        char *dest, S1, S2, S3;
        int len;
        j2k_image_t *img;
        j2k_cp_t *cp;
        int w, h, max;
-       int i, image_type = -1;
+       int i, image_type = -1, compno;
+       int adjust;
 
        if (argc < 3) {
                fprintf(stderr, "usage: %s j2k-file pnm-file\n", argv[0]);
@@ -61,37 +62,32 @@ int main(int argc, char **argv)
 
        dest = argv[2];
 
-       while (*dest && *dest != '.') {
+       while (*dest) {
                dest++;
        }
-       dest++;
-       S1 = *dest;
-       dest++;
-       S2 = *dest;
-       dest++;
+       dest--;
        S3 = *dest;
+       dest--;
+       S2 = *dest;
+       dest--;
+       S1 = *dest;
 
-       if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
-                       || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
+       if ((S1 == 'p' && S2 == 'g' && S3 == 'x') || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
                image_type = 0;
        }
 
-       if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
-                       || (S1 == 'P' && S2 == 'N' && S3 == 'M') || (S1 == 'p' && S2 == 'g'
-                                                                                                                                                                                                        && S3 == 'm')
-                       || (S1 == 'P' && S2 == 'G' && S3 == 'M') || (S1 == 'P' && S2 == 'P'
-                                                                                                                                                                                                        && S3 == 'M')
-                       || (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
+       if ((S1 == 'p' && S2 == 'n' && S3 == 'm')|| (S1 == 'P' && S2 == 'N' && S3 == 'M') || 
+           (S1 == 'p' && S2 == 'g' && S3 == 'm')|| (S1 == 'P' && S2 == 'G' && S3 == 'M') || 
+           (S1 == 'P' && S2 == 'P' && S3 == 'M')|| (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
                image_type = 1;
        }
 
-       if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
-                       || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
+       if ((S1 == 'b' && S2 == 'm' && S3 == 'p') || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
                image_type = 2;
        }
+
        if (image_type == -1) {
-               fprintf(stderr,
-                                               "\033[0;33m!! Unrecognized format for infile [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\033[0;39m\n\n");
+               fprintf(stderr, "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",S1,S2,S3);
                return 1;
        }
 
@@ -102,249 +98,279 @@ int main(int argc, char **argv)
        fread(src, 1, len, f);
        fclose(f);
 
-       if (!j2k_decode(src, len, &img, &cp)) {
-               fprintf(stderr, "j2k_to_image: failed to decode image!\n");
-               return 1;
+       src_name=argv[1];
+       while (*src_name) {
+               src_name++;
        }
-       free(src);
+       src_name--;
+       S3 = *src_name;
+       src_name--;
+       S2 = *src_name;
+       src_name--;
+       S1 = *src_name;
+
+       if (S1 == 'j' && S2 == '2' && S3 == 'k')
+         {
+           if (!j2k_decode(src, len, &img, &cp)) {
+             fprintf(stderr, "j2k_to_image: failed to decode image!\n");
+             return 1;
+           }
+         }
+       else
+         {
+           if (S1 == 'j' && S2 == 'p' && S3 == 't')
+             { 
+               if (!j2k_decode_jpt_stream(src, len, &img, &cp)) {
+                 fprintf(stderr, "j2k_to_image: failed to decode image!\n");
+                 return 1;
+               }
+             }
+           else
+             {
+               fprintf(stderr,"j2k_to_image : Unknown format image *.%c%c%c [only *.j2k or *.jpt]!! \n",S1,S2,S3);
+               return 1;
+             }
+         }
 
+       free(src);
        /* ------------------  CREATE OUT IMAGE WITH THE RIGHT FORMAT ----------------------- */
 
-       /* ------------------------ / */
-       /* / */
-       /* FORMAT : PNM, PGM or PPM / */
-       /* / */
-       /* ------------------------ / */
-
-       if (image_type == 1) {                          /* PNM PGM PPM */
-               if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
-                               && img->comps[1].dx == img->comps[2].dx
-                               && img->comps[0].dy == img->comps[1].dy
-                               && img->comps[1].dy == img->comps[2].dy
-                               && img->comps[0].prec == img->comps[1].prec
-                               && img->comps[1].prec == img->comps[2].prec) {
-                       f = fopen(argv[2], "wb");
-                       w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
-                       h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
-                       max = (1 << img->comps[0].prec) - 1;
-                       fprintf(f, "P6\n%d %d\n%d\n", w, h, max);
-                       for (i = 0; i < w * h; i++) {
-                               char r, g, b;
-                               r = img->comps[0].data[i];
-                               g = img->comps[1].data[i];
-                               b = img->comps[2].data[i];
-                               fprintf(f, "%c%c%c", r, g, b);
+       /* ---------------------------- / */
+       /* /                            / */
+       /* /  FORMAT : PNM, PGM or PPM  / */
+       /* /                            / */
+       /* ---------------------------- / */
+       
+       switch (image_type)
+         {     
+         case 1: /* PNM PGM PPM*/
+           if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
+               && img->comps[1].dx == img->comps[2].dx
+               && img->comps[0].dy == img->comps[1].dy
+               && img->comps[1].dy == img->comps[2].dy
+               && img->comps[0].prec == img->comps[1].prec
+               && img->comps[1].prec == img->comps[2].prec) 
+             {
+               f = fopen(argv[2], "wb");
+               w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
+               h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
+               //max = (1 << img->comps[0].prec) - 1;
+               max =img->comps[0].prec>8? 255:(1 << img->comps[0].prec) - 1;
+               fprintf(f, "P6\n%d %d\n%d\n", w, h, max);
+               adjust=img->comps[0].prec>8?img->comps[0].prec-8:0;
+               for (i = 0; i < w * h; i++) 
+                 {
+                   char r, g, b;
+                   r = img->comps[0].data[i];
+                   r+=(img->comps[0].sgnd? 1 << (img->comps[0].prec-1):0);
+                   r=r>>adjust;                         
+                   
+                   g = img->comps[1].data[i];
+                   g+=(img->comps[1].sgnd? 1 << (img->comps[1].prec-1):0);
+                   g=g>>adjust;
+                   
+                   b = img->comps[2].data[i];
+                   b+=(img->comps[2].sgnd? 1 << (img->comps[2].prec-1):0);
+                   b=b>>adjust;
+                   
+                   fprintf(f, "%c%c%c", r, g, b);
+                 }
+               fclose(f);
+             } else 
+               { 
+                 for (compno = 0; compno < img->numcomps; compno++) 
+                   {
+                     char name[256];
+                     if (img->numcomps > 1) {
+                       sprintf(name, "%d.%s", compno, argv[2]);
+                     } else 
+                       {
+                         sprintf(name, "%s", argv[2]);
                        }
-                       fclose(f);
-               } else {
-                       int compno;
-                       for (compno = 0; compno < img->numcomps; compno++) {
-                               char name[256];
-                               if (img->numcomps > 1) {
-                                       sprintf(name, "%d.%s", compno, argv[2]);
-                               } else {
-                                       sprintf(name, "%s", argv[2]);
-                               }
-
-                               f = fopen(name, "wb");
-
-                               w = ceildiv(img->x1 - img->x0, img->comps[compno].dx);
-                               h = ceildiv(img->y1 - img->y0, img->comps[compno].dy);
-                               max = (1 << img->comps[compno].prec) - 1;
-                               fprintf(f, "P5\n%d %d\n%d\n", w, h, max);
-                               for (i = 0; i < w * h; i++) {
-                                       char l;
-                                       l = img->comps[compno].data[i];
-                                       fprintf(f, "%c", l);
-                               }
-                               fclose(f);
+                     f = fopen(name, "wb");
+                     w = ceildiv(img->x1 - img->x0, img->comps[compno].dx);
+                     h = ceildiv(img->y1 - img->y0, img->comps[compno].dy);
+                     max =img->comps[compno].prec>8? 255:(1 << img->comps[compno].prec) - 1;
+                     fprintf(f, "P5\n%d %d\n%d\n", w, h, max);
+                     adjust=img->comps[compno].prec>8?img->comps[compno].prec-8:0;
+                     for (i = 0; i < w * h; i++) 
+                       {
+                         char l;
+                         l = img->comps[compno].data[i];
+                         l+=(img->comps[compno].sgnd? 1 << (img->comps[compno].prec-1):0);
+                         l=l>>adjust;
+                         fprintf(f, "%c", l);
                        }
+                     fclose(f);
+                   }
                }
-       } else
-               /* ------------------------ / */
-               /* / */
-               /* FORMAT : PGX             / */
-               /* / */
-               /* ------------------------ / */
-
-       if (image_type == 0) {                          /* PGX */
-               int compno;
-               for (compno = 0; compno < img->numcomps; compno++) {
-                       j2k_comp_t *comp = &img->comps[compno];
-                       char name[256];
-                       /* sprintf(name, "%s-%d.pgx", argv[2], compno); */
-                       sprintf(name, "%s", argv[2]);
-                       f = fopen(name, "wb");
-                       w = ceildiv(img->x1 - img->x0, comp->dx);
-                       h = ceildiv(img->y1 - img->y0, comp->dy);
-                       fprintf(f, "PG LM %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
-                                                       w, h);
-                       for (i = 0; i < w * h; i++) {
-                               int v = img->comps[compno].data[i];
-                               if (comp->prec <= 8) {
-                                       char c = (char) v;
-                                       fwrite(&c, 1, 1, f);
-                               } else if (comp->prec <= 16) {
-                                       short s = (short) v;
-                                       fwrite(&s, 2, 1, f);
-                               } else {
-                                       fwrite(&v, 4, 1, f);
-                               }
-                       }
-                       fclose(f);
+           break ;
+           
+           /* ------------------------ / */
+           /* /                        / */
+           /* /     FORMAT : PGX       / */
+           /* /                        / */
+           /* /----------------------- / */
+         case 0: /* PGX */
+           for (compno = 0; compno < img->numcomps; compno++) 
+             {
+               j2k_comp_t *comp = &img->comps[compno];
+               char name[256];
+               if (img->numcomps>1)
+                 sprintf(name, "%d_%s", compno, argv[2]);
+               else
+                 sprintf(name, "%s", argv[2]);
+               f = fopen(name, "wb");
+               w = ceildiv(img->x1 - img->x0, comp->dx);
+               h = ceildiv(img->y1 - img->y0, comp->dy);
+               fprintf(f, "PG LM %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
+               for (i = 0; i < w * h; i++) 
+                 {
+                   int v = img->comps[compno].data[i];
+                   if (comp->prec <= 8) 
+                     {
+                       char c = (char) v;
+                       fwrite(&c, 1, 1, f);
+                     } else if (comp->prec <= 16) 
+                       {
+                         short s = (short) v;
+                         fwrite(&s, 2, 1, f);
+                       } else 
+                         {
+                           fwrite(&v, 4, 1, f);
+                         }
+                 }
+               fclose(f);
+             }
+           break ;
+           
+           /* ------------------------ / */
+           /* /                        / */
+           /* /     FORMAT : BMP       / */
+           /* /                        / */
+           /* /----------------------- / */
+           
+         case 2:  /* BMP */
+           if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
+               && img->comps[1].dx == img->comps[2].dx
+               && img->comps[0].dy == img->comps[1].dy
+               && img->comps[1].dy == img->comps[2].dy
+               && img->comps[0].prec == img->comps[1].prec
+               && img->comps[1].prec == img->comps[2].prec) 
+             {
+               /* -->> -->> -->> -->>
+                  
+                  24 bits color
+                  
+                  <<-- <<-- <<-- <<-- */
+               
+               f = fopen(argv[2], "wb");
+               w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
+               h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
+               
+               fprintf(f, "BM");
+               
+               /* FILE HEADER */
+               /* ------------- */
+               fprintf(f, "%c%c%c%c",
+                       (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
+                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
+                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
+                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
+               fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+               fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, ((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
+               
+               /* INFO HEADER   */
+               /* ------------- */
+               fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
+               fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),(unsigned char) ((w) >> 8) & 0xff,
+                       (unsigned char) ((w) >> 16) & 0xff, (unsigned char) ((w) >> 24) & 0xff);
+               fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff), (unsigned char) ((h) >> 8) & 0xff,
+                       (unsigned char) ((h) >> 16) & 0xff, (unsigned char) ((h) >> 24) & 0xff);
+               fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
+               fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
+               fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+               fprintf(f, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
+                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
+                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
+                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
+               fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
+               fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
+               fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+               fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+               
+               for (i = 0; i < w * h; i++) 
+                 {
+                   unsigned char R, G, B;
+                   
+                   R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
+                   G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
+                   B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
+                   fprintf(f, "%c%c%c", B, G, R);
+                   if (((i + 1) % w == 0 && w % 2))
+                     fprintf(f, "%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff);
+                 }
+               fclose(f);
+             } else    
+               {   /* Gray-scale */
+                 
+                 /* -->> -->> -->> -->>
+                    
+                    8 bits non code (Gray scale)
+                    
+                    <<-- <<-- <<-- <<-- */
+                 f = fopen(argv[2], "wb");
+                 w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
+                 h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
+                 
+                 fprintf(f, "BM");
+                 
+                 /* FILE HEADER */
+                 /* ------------- */
+                 fprintf(f, "%c%c%c%c",
+                         (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
+                         (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
+                         (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
+                         (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
+                 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+                 fprintf(f, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
+                         ((54 + 1024) >> 16) & 0xff, ((54 + 1024) >> 24) & 0xff);
+                 
+                 /* INFO HEADER */
+                 /* ------------- */
+                 fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
+                 fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff), (unsigned char) ((w) >> 8) & 0xff,
+                         (unsigned char) ((w) >> 16) & 0xff, (unsigned char) ((w) >> 24) & 0xff);
+                 fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff), (unsigned char) ((h) >> 8) & 0xff,
+                         (unsigned char) ((h) >> 16) & 0xff, (unsigned char) ((h) >> 24) & 0xff);
+                 fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
+                 fprintf(f, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
+                 fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+                 fprintf(f, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
+                         (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
+                         (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
+                         (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
+                 fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
+                 fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
+                 fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
+                 fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
                }
-       } else
-               /* ------------------------ / */
-               /* / */
-               /* FORMAT : BMP             / */
-               /* / */
-               /* ------------------------ / */
-
-
-       if (image_type == 2) {                          /* BMP */
-               if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
-                               && img->comps[1].dx == img->comps[2].dx
-                               && img->comps[0].dy == img->comps[1].dy
-                               && img->comps[1].dy == img->comps[2].dy
-                               && img->comps[0].prec == img->comps[1].prec
-                               && img->comps[1].prec == img->comps[2].prec) {
-                       /* -->> -->> -->> -->>
-
-                          24 bits color
-
-                          <<-- <<-- <<-- <<-- */
-
-                       f = fopen(argv[2], "wb");
-                       w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
-                       h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
-
-                       fprintf(f, "BM");
-
-                       /* FILE HEADER */
-                       /* ------------- */
-                       fprintf(f, "%c%c%c%c",
-                                                       (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
-                                                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) &
-                                                       0xff,
-                                                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) &
-                                                       0xff,
-                                                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) &
-                                                       0xff);
-                       fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
-                                                       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,
-                                                       ((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
-                       /* INFO HEADER */
-                       /* ------------- */
-                       fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
-                                                       ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),
-                                                       (unsigned char) ((w) >> 8) & 0xff,
-                                                       (unsigned char) ((w) >> 16) & 0xff,
-                                                       (unsigned char) ((w) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff),
-                                                       (unsigned char) ((h) >> 8) & 0xff,
-                                                       (unsigned char) ((h) >> 16) & 0xff,
-                                                       (unsigned char) ((h) >> 24) & 0xff);
-                       fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
-                       fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
-                       fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
-                                                       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c",
-                                                       (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
-                                                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
-                                                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
-                                                       (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) &
-                                                       0xff);
-                       fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
-                                                       ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
-                                                       ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
-                                                       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
-                                                       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
-
-                       for (i = 0; i < w * h; i++) {
-                               unsigned char R, G, B;
-
-                               R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
-                               G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
-                               B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
-                               fprintf(f, "%c%c%c", B, G, R);
-                               if (((i + 1) % w == 0 && w % 2))
-                                       fprintf(f, "%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
-                                                                       ((0) >> 16) & 0xff);
-                       }
-                       fclose(f);
-               } else {                                                                                /* Gray-scale */
-
-                       /* -->> -->> -->> -->>
-
-                          8 bits non code (Gray scale)
-
-                          <<-- <<-- <<-- <<-- */
-                       f = fopen(argv[2], "wb");
-                       w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
-                       h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
-
-                       fprintf(f, "BM");
-
-                       /* FILE HEADER */
-                       /* ------------- */
-                       fprintf(f, "%c%c%c%c",
-                                                       (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
-                                                       (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) &
-                                                       0xff,
-                                                       (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) &
-                                                       0xff,
-                                                       (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) &
-                                                       0xff);
-                       fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
-                                                       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
-                                                       ((54 + 1024) >> 16) & 0xff, ((54 + 1024) >> 24) & 0xff);
-                       /* INFO HEADER */
-                       /* ------------- */
-                       fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
-                                                       ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),
-                                                       (unsigned char) ((w) >> 8) & 0xff,
-                                                       (unsigned char) ((w) >> 16) & 0xff,
-                                                       (unsigned char) ((w) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff),
-                                                       (unsigned char) ((h) >> 8) & 0xff,
-                                                       (unsigned char) ((h) >> 16) & 0xff,
-                                                       (unsigned char) ((h) >> 24) & 0xff);
-                       fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
-                       fprintf(f, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
-                       fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
-                                                       ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
-                                                       (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
-                                                       (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
-                                                       (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
-                                                       ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
-                                                       ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
-                                                       ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
-                       fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
-                                                       ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
-               }
-
-               for (i = 0; i < 256; i++) {
-                       fprintf(f, "%c%c%c%c", i, i, i, 0);
-               }
-
-               for (i = 0; i < w * h; i++) {
-                       fprintf(f, "%c",
-                                                       img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
-                       if (((i + 1) % w == 0 && w % 2))
-                               fprintf(f, "%c", 0);
-
-               }
-       }
+           
+           for (i = 0; i < 256; i++) 
+             {
+               fprintf(f, "%c%c%c%c", i, i, i, 0);
+             }
+           
+           for (i = 0; i < w * h; i++) 
+             {
+               fprintf(f, "%c", img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
+               if (((i + 1) % w == 0 && w % 2))
+                 fprintf(f, "%c", 0);
+             }
+           break;
+         default :
+           break;
+         }
+       
        return 0;
 }
index b08134984201d847a5485057e2abea9754f57a33..adb203467e4cad28a550ebf65cc86800a869ffb1 100644 (file)
 #ifndef __BIO_H
 #define __BIO_H
 
+/*
+ * Number of bytes written.
+ */ 
 int bio_numbytes();
+
+/*
+ * Init encoder.
+ *
+ * bp  : Output buffer
+ * len : Output buffer length 
+ */
 void bio_init_enc(unsigned char *bp, int len);
+
+/*
+ * Init decoder.
+ *
+ * bp  : Input buffer
+ * len : Input buffer length
+ */
 void bio_init_dec(unsigned char *bp, int len);
+
+/*
+ * Write bits.
+ *
+ * v  : Value of bits
+ * n  : Number of bits to write
+ */
 void bio_write(int v, int n);
+
+/*
+ * Read bits. 
+ *
+ * n : Number of bits to read 
+ */
 int bio_read(int n);
-int bio_flush();                                                               /* modified to eliminated longjmp !! */
-int bio_inalign();                                                     /* modified to eliminated longjmp !! */
+
+/*
+ * Flush bits. Modified to eliminate longjmp !!
+ */
+int bio_flush();
+
+int bio_inalign();  /* modified to eliminated longjmp !! */
 
 #endif
index bc5adc6076594c7e9dba6f6c4e949202e2e12c4f..b57e085f5cbae1460f1aca1cba05fcbdfd7998f7 100644 (file)
 #include "cio.h"
 #include <setjmp.h>
 
-static unsigned char *cio_start, *cio_end, *cio_bp;
+static unsigned char *cio_start;  /* pointer to the start of the stream */
+static unsigned char *cio_end;    /* pointer to the end of the stream */
+static unsigned char *cio_bp;     /* pointer to the present position */
 
 extern jmp_buf j2k_error;
 
-/* <summary> */
-/* Number of bytes written. */
-/* </summary> */
+/* 
+ * Number of bytes written.
+ */
 int cio_numbytes()
 {
        return cio_bp - cio_start;
 }
 
-/* <summary> */
-/* Get position in byte stream. */
-/* </summary> */
+/*
+ * Get position in byte stream.
+ */
 int cio_tell()
 {
        return cio_bp - cio_start;
 }
 
-/* <summary> */
-/* Set position in byte stream. */
-/* </summary> */
+/*
+ * Set position in byte stream.
+ *
+ * pos : position, in number of bytes, from the beginning of the stream
+ */
 void cio_seek(int pos)
 {
        cio_bp = cio_start + pos;
 }
 
-/* <summary> */
-/* Number of bytes left before the end of the stream. */
-/* </summary> */
+/*
+ * Number of bytes left before the end of the stream.
+ */
 int cio_numbytesleft()
 {
        return cio_end - cio_bp;
 }
 
-/* <summary> */
-/* Get pointer to the current position in the stream. */
-/* </summary> */
+/*
+ * Get pointer to the current position in the stream.
+ */
 unsigned char *cio_getbp()
 {
        return cio_bp;
 }
 
-/* <summary> */
-/* Initialize byte IO. */
-/* </summary> */
+/* 
+ * Initialize byte IO
+ *
+ * bp  : destination/source stream
+ * len : length of the stream
+ */
 void cio_init(unsigned char *bp, int len)
 {
        cio_start = bp;
@@ -81,9 +88,9 @@ void cio_init(unsigned char *bp, int len)
        cio_bp = bp;
 }
 
-/* <summary> */
-/* Write a byte. */
-/* </summary> */
+/*
+ * Write a byte.
+ */
 void cio_byteout(unsigned char v)
 {
        if (cio_bp >= cio_end)
@@ -92,9 +99,9 @@ void cio_byteout(unsigned char v)
 
 }
 
-/* <summary> */
-/* Read a byte. */
-/* </summary> */
+/*
+ * Read a byte.
+ */
 unsigned char cio_bytein()
 {
        if (cio_bp >= cio_end)
@@ -102,9 +109,12 @@ unsigned char cio_bytein()
        return *cio_bp++;
 }
 
-/* <summary> */
-/* Write a byte. */
-/* </summary> */
+/*
+ * Write some bytes.
+ *
+ * v : value to write
+ * n : number of bytes to write
+ */
 void cio_write(unsigned int v, int n)
 {
        int i;
@@ -113,9 +123,13 @@ void cio_write(unsigned int v, int n)
        }
 }
 
-/* <summary> */
-/* Read some bytes. */
-/* </summary> */
+/*
+ * Read some bytes.
+ *
+ * n : number of bytes to read
+ *
+ * return : value of the n bytes read
+ */
 unsigned int cio_read(int n)
 {
        int i;
@@ -127,9 +141,11 @@ unsigned int cio_read(int n)
        return v;
 }
 
-/* <summary> */
-/* Write some bytes. */
-/* </summary> */
+/* 
+ * Skip some bytes.
+ *
+ * n : number of bytes to skip
+ */
 void cio_skip(int n)
 {
        cio_bp += n;
index c6945b7e8b6552ea95cdc9a4ff757c201ebc3173..c75a46534220f7400842946c444aa22095226b2b 100644 (file)
 #ifndef __CIO_H
 #define __CIO_H
 
+/* 
+ * Number of bytes written.
+ *
+ * returns number of bytes written
+ */
+int cio_numbytes();
+
+/*
+ * Get position in byte stream.
+ *
+ * return position in bytes
+ */
 int cio_tell();
+
+/*
+ * Set position in byte stream.
+ *
+ * pos : position, in number of bytes, from the beginning of the stream
+ */
 void cio_seek(int pos);
-int cio_numbytes();
+
+/*
+ * Number of bytes left before the end of the stream.
+ *
+ * Returns the number of bytes before the end of the stream 
+ */
 int cio_numbytesleft();
+
+/*
+ * Get pointer to the current position in the stream.
+ *
+ * return : pointer to the position
+ */
 unsigned char *cio_getbp();
+
+/* 
+ * Initialize byte IO
+ *
+ * bp  : destination/source stream
+ * len : length of the stream
+ */
 void cio_init(unsigned char *bp, int len);
+
+/*
+ * Write some bytes.
+ *
+ * v : value to write
+ * n : number of bytes to write
+ */
 void cio_write(unsigned int v, int n);
+
+/*
+ * Read some bytes.
+ *
+ * n : number of bytes to read
+ *
+ * return : value of the n bytes read
+ */
 unsigned int cio_read(int n);
+
+/* 
+ * Skip some bytes.
+ *
+ * n : number of bytes to skip
+ */
 void cio_skip(int n);
 
 #endif
index 6722d9f58f3ee28002e738674b05ab623b55fd81..6ef991fdc96338be911ecbf6799480d2b477573e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2001-2002, David Janssens
- * Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2002-2003,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2002-2004, Yannick Verschueren
+ * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -32,6 +32,7 @@
 #include "tcd.h"
 #include <stdlib.h>
 #include <stdio.h>
+//#include <math.h>
 
 #define S(i) a[x*(i)*2]
 #define D(i) a[x*(1+(i)*2)]
@@ -41,9 +42,9 @@
 #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
 #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
 
-/* <summary> */
-/* This table contains the norms of the 5-3 wavelets for different bands.  */
-/* </summary> */
+/* <summary>                                                              */
+/* This table contains the norms of the 5-3 wavelets for different bands. */
+/* </summary>                                                             */
 double dwt_norms[4][10] = {
        {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
        {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
@@ -51,9 +52,9 @@ double dwt_norms[4][10] = {
        {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
 };
 
-/* <summary> */
-/* This table contains the norms of the 9-7 wavelets for different bands.  */
-/* </summary> */
+/* <summary>                                                              */
+/* This table contains the norms of the 9-7 wavelets for different bands. */
+/* </summary>                                                             */
 double dwt_norms_real[4][10] = {
        {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
        {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
@@ -65,6 +66,9 @@ double dwt_norms_real[4][10] = {
 static int *b = NULL;
 static int lastSizeOfB = 0;
 
+/* <summary>       */
+/* Claning memory. */
+/* </summary>      */
 
 void dwt_clean()
 {
@@ -77,9 +81,9 @@ void dwt_clean()
 
 /* \ Add Patrick */
 
-/* <summary> */
+/* <summary>               */
 /* Forward lazy transform. */
-/* </summary> */
+/* </summary>              */
 void dwt_deinterleave(int *a, int n, int x, int res, int cas)
 {
        int dn, sn, i;
@@ -107,41 +111,9 @@ void dwt_deinterleave(int *a, int n, int x, int res, int cas)
                a[i * x] = b[i];
 }
 
-/* <summary> */
-/* Forward lazy transform. */
-/* </summary> */
-void dwt_deinterleave_real(int *a, int n, int x, int res, int cas)
-{
-       int dn, sn, i;
-       sn = res;
-       dn = n - res;
-
-       if (lastSizeOfB != n) {
-               if (b != NULL)
-                       free(b);
-               b = (int *) malloc(n * sizeof(int));
-               lastSizeOfB = n;
-       }
-
-       if (cas) {
-               for (i = 0; i < sn; i++)
-                       b[i] = a[(2 * i + 1) * x];
-               for (i = 0; i < dn; i++)
-                       b[sn + i] = a[2 * i * x];
-       } else {
-               for (i = 0; i < sn; i++)
-                       b[i] = a[2 * i * x];
-               for (i = 0; i < dn; i++)
-                       b[sn + i] = a[(2 * i + 1) * x];
-       }
-       for (i = 0; i < n; i++)
-               a[i * x] = b[i];
-
-}
-
-/* <summary> */
+/* <summary>               */
 /* Inverse lazy transform. */
-/* </summary> */
+/* </summary>              */
 void dwt_interleave(int *a, int n, int x, int res, int cas)
 {
        int dn, sn, i;
@@ -170,41 +142,9 @@ void dwt_interleave(int *a, int n, int x, int res, int cas)
                a[i * x] = b[i];
 }
 
-/* <summary> */
-/* Inverse lazy transform. */
-/* </summary> */
-void dwt_interleave_real(int *a, int n, int x, int res, int cas)
-{
-       int dn, sn, i;
-       sn = res;
-       dn = n - res;
-
-       if (lastSizeOfB != n) {
-               if (b != NULL)
-                       free(b);
-               b = (int *) malloc(n * sizeof(int));
-               lastSizeOfB = n;
-       }
-
-       if (cas) {
-               for (i = 0; i < sn; i++)
-                       b[2 * i + 1] = a[i * x];
-               for (i = 0; i < dn; i++)
-                       b[2 * i] = a[(sn + i) * x];
-       } else {
-               for (i = 0; i < sn; i++)
-                       b[2 * i] = a[i * x];
-               for (i = 0; i < dn; i++)
-                       b[2 * i + 1] = a[(sn + i) * x];
-       }
-       for (i = 0; i < n; i++)
-               a[i * x] = b[i];
-
-}
-
-/* <summary> */
+/* <summary>                            */
 /* Forward 5-3 wavelet tranform in 1-D. */
-/* </summary> */
+/* </summary>                           */
 void dwt_encode_1(int *a, int n, int x, int res, int cas)
 {
        int dn, sn, i = 0;
@@ -212,7 +152,7 @@ void dwt_encode_1(int *a, int n, int x, int res, int cas)
        dn = n - res;
 
        if (cas) {
-               if (!sn && dn == 1)                                     /* NEW :  CASE ONE ELEMENT */
+               if (!sn && dn == 1)     /* NEW :  CASE ONE ELEMENT */
                        S(i) *= 2;
                else {
                        for (i = 0; i < dn; i++)
@@ -231,20 +171,18 @@ void dwt_encode_1(int *a, int n, int x, int res, int cas)
        dwt_deinterleave(a, n, x, res, cas);
 }
 
-/* <summary> */
+/* <summary>                            */
 /* Inverse 5-3 wavelet tranform in 1-D. */
-/* </summary> */
+/* </summary>                           */
 void dwt_decode_1(int *a, int n, int x, int res, int cas)
 {
        int dn, sn, i = 0;
-       /* dn=n/2; */
-       /* sn=(n+1)/2; */
        sn = res;
        dn = n - res;
 
        dwt_interleave(a, n, x, res, cas);
        if (cas) {
-               if (!sn && dn == 1)                                     /* NEW :  CASE ONE ELEMENT */
+               if (!sn && dn == 1)     /* NEW :  CASE ONE ELEMENT */
                        S(i) /= 2;
                else {
                        for (i = 0; i < sn; i++)
@@ -262,74 +200,27 @@ void dwt_decode_1(int *a, int n, int x, int res, int cas)
        }
 }
 
-/* <summary> */
+/* <summary>                            */
 /* Forward 5-3 wavelet tranform in 2-D. */
-/* </summary> */
+/* </summary>                           */
 void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
 {
-       int i, j, rw, rh, rw1, rh1;
-       int previous_rw, previous_rh, previous_rw1, previous_rh1;
+       int i, j;
+       int rw;            /* width of the resolution level computed                                                           */
+       int rh;            /* heigth of the resolution level computed                                                          */
+       int rw1;           /* width of the resolution level once lower than computed one                                       */
+       int rh1;           /* height of the resolution level once lower than computed one                                      */
 
        for (i = 0; i < l; i++) {
-               int cas_col = 0;
-               int cas_row = 0;
+         int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
+         int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
                rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
                rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
-               rw1 =
-                       tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
-               rh1 =
-                       tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
-
-               /* Check the different cases for that it's necessary to invert high pass and low pass filter */
-
-               if (tilec->previous_row) {
-                       previous_rw =
-                               tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                        i].
-                               previous_x0;
-                       previous_rw1 =
-                               tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                                        i -
-                                                                                                                                                                                                                                                                                        1].
-                               previous_x0;
-                       /* inversion on the previous and propagation of the inversion on the other tile */
-                       if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
-                                       || (tilec->resolutions[l - i - 1].cas_row
-                                                       && previous_rw1 == previous_rw - previous_rw1))
-                               cas_row = 1;
-               }
-
-               if (tilec->previous_col) {
-                       previous_rh =
-                               tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                        i].
-                               previous_y0;
-                       previous_rh1 =
-                               tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                                        i -
-                                                                                                                                                                                                                                                                                        1].
-                               previous_y0;
-                       /* inversion on the previous and propagation of the inversion on the other tile */
-                       if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
-                                       || (tilec->resolutions[l - i - 1].cas_col
-                                                       && previous_rh1 == previous_rh - previous_rh1))
-                               cas_col = 1;
-               }
-
-               /* subband LL shorter than LH or HL */
-               if (rw1 < rw - rw1)
-                       cas_row = 1;
-               if (rh1 < rh - rh1)
-                       cas_col = 1;
-
-               /* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
-               if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
-                       cas_row = 1;
-               if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
-                       cas_col = 1;
+               rw1 = tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
+               rh1 = tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
 
-               tilec->resolutions[l - i - 1].cas_row = cas_row;
-               tilec->resolutions[l - i - 1].cas_col = cas_col;
+               cas_row = tilec->resolutions[l - i].x0 % 2;
+               cas_col = tilec->resolutions[l - i].y0 % 2;
 
                for (j = 0; j < rw; j++)
                        dwt_encode_1(a + j, rh, w, rh1, cas_col);
@@ -340,70 +231,28 @@ void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
        dwt_clean();
 }
 
-/* <summary> */
+/* <summary>                            */
 /* Inverse 5-3 wavelet tranform in 2-D. */
-/* </summary> */
-void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
-                                                               tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec)
+/* </summary>                           */
+void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)//, tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec)
 {
-       int i, j, rw, rh, rw1, rh1;
-       int previous_rw, previous_rh, previous_rw1, previous_rh1;
+       int i, j;
+       int rw;            /* width of the resolution level computed                                                           */
+       int rh;            /* heigth of the resolution level computed                                                          */
+       int rw1;           /* width of the resolution level once lower than computed one                                       */
+       int rh1;           /* height of the resolution level once lower than computed one                                      */
+
        for (i = l - 1; i >= 0; i--) {
-               int cas_col = 0;
-               int cas_row = 0;
+         int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
+         int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
 
-               rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
+               rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
                rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
-               rw1 =
-                       tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
-               rh1 =
-                       tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
-
-               if (tilec->previous_row) {
-                       previous_rw =
-                               tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                        i].
-                               previous_x0;
-                       previous_rw1 =
-                               tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                                        i -
-                                                                                                                                                                                                                                                                                        1].
-                               previous_x0;
-                       if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
-                                       || (row_tilec->resolutions[l - i - 1].cas_row
-                                                       && previous_rw1 == previous_rw - previous_rw1))
-                               cas_row = 1;
-               }
-
-               if (tilec->previous_col) {
-                       previous_rh =
-                               tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                        i].
-                               previous_y0;
-                       previous_rh1 =
-                               tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                                        i -
-                                                                                                                                                                                                                                                                                        1].
-                               previous_y0;
-                       if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
-                                       || (col_tilec->resolutions[l - i - 1].cas_col
-                                                       && previous_rh1 == previous_rh - previous_rh1))
-                               cas_col = 1;
-               }
-
-               if (rw1 < rw - rw1)
-                       cas_row = 1;
-               if (rh1 < rh - rh1)
-                       cas_col = 1;
-
-               /* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
-               if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
-                       cas_row = 1;
-               if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
-                       cas_col = 1;
-
-               tilec->resolutions[l - i - 1].cas_row = cas_row;
-               tilec->resolutions[l - i - 1].cas_col = cas_col;
+               rw1 = tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
+               rh1 = tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
+               
+               cas_row = tilec->resolutions[l - i].x0 % 2;
+               cas_col = tilec->resolutions[l - i].y0 % 2;
 
                for (j = 0; j < rh; j++)
                        dwt_decode_1(a + j * w, rw, 1, rw1, cas_row);
@@ -413,9 +262,9 @@ void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
        dwt_clean();
 }
 
-/* <summary> */
+/* <summary>                          */
 /* Get gain of 5-3 wavelet transform. */
-/* </summary> */
+/* </summary>                         */
 int dwt_getgain(int orient)
 {
        if (orient == 0)
@@ -425,17 +274,17 @@ int dwt_getgain(int orient)
        return 2;
 }
 
-/* <summary> */
+/* <summary>                */
 /* Get norm of 5-3 wavelet. */
-/* </summary> */
+/* </summary>               */
 double dwt_getnorm(int level, int orient)
 {
        return dwt_norms[orient][level];
 }
 
-/* <summary> */
+/* <summary>                             */
 /* Forward 9-7 wavelet transform in 1-D. */
-/* </summary> */
+/* </summary>                            */
 void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
 {
        int dn, sn, i = 0;
@@ -453,9 +302,9 @@ void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
                        for (i = 0; i < sn; i++)
                                D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
                        for (i = 0; i < dn; i++)
-                               S(i) = fix_mul(S(i), 5038);
+                         S(i) = fix_mul(S(i), 5038); /*5038*/
                        for (i = 0; i < sn; i++)
-                               D(i) = fix_mul(D(i), 6660);
+                         D(i) = fix_mul(D(i), 6659); /*6660*/
                }
        } else {
          if ((dn > 0) || (sn > 1)) {  /* NEW :  CASE ONE ELEMENT */
@@ -468,29 +317,29 @@ void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
                        for (i = 0; i < sn; i++)
                                S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
                        for (i = 0; i < dn; i++)
-                               D(i) = fix_mul(D(i), 5038);
+                               D(i) = fix_mul(D(i), 5038); /*5038*/
                        for (i = 0; i < sn; i++)
-                               S(i) = fix_mul(S(i), 6660);
+                               S(i) = fix_mul(S(i), 6659); /*6660*/
                }
        }
-       dwt_deinterleave_real(a, n, x, res, cas);
+       dwt_deinterleave(a, n, x, res, cas);
 }
 
-/* <summary> */
+/* <summary>                             */
 /* Inverse 9-7 wavelet transform in 1-D. */
-/* </summary> */
+/* </summary>                            */
 void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
 {
        int dn, sn, i = 0;
        dn = n - res;
        sn = res;
-       dwt_interleave_real(a, n, x, res, cas);
+       dwt_interleave(a, n, x, res, cas);
        if (cas) {
                if ((sn > 0) || (dn > 1)) {  /* NEW :  CASE ONE ELEMENT */
                        for (i = 0; i < sn; i++)
-                               D(i) = fix_mul(D(i), 10076);
+                         D(i) = fix_mul(D(i), 10078); /* 10076 */
                        for (i = 0; i < dn; i++)
-                               S(i) = fix_mul(S(i), 13320);
+                               S(i) = fix_mul(S(i), 13318); /* 13320*/
                        for (i = 0; i < sn; i++)
                                D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);
                        for (i = 0; i < dn; i++)
@@ -503,9 +352,9 @@ void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
        } else {
                if ((dn > 0) || (sn > 1)) {  /* NEW :  CASE ONE ELEMENT */
                        for (i = 0; i < sn; i++)
-                               S(i) = fix_mul(S(i), 10076);
+                               S(i) = fix_mul(S(i), 10078); /* 10076 */
                        for (i = 0; i < dn; i++)
-                               D(i) = fix_mul(D(i), 13320);
+                         D(i) = fix_mul(D(i), 13318); /* 13320*/
                        for (i = 0; i < sn; i++)
                                S(i) -= fix_mul(D_(i - 1) + D_(i), 3633);
                        for (i = 0; i < dn; i++)
@@ -518,70 +367,28 @@ void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
        }
 }
 
-/* <summary> */
+/* <summary>                             */
 /* Forward 9-7 wavelet transform in 2-D. */
-/* </summary> */
+/* </summary>                            */
 
 void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
 {
-       int i, j, rw, rh, rw1, rh1;
-       int previous_rw, previous_rh, previous_rw1, previous_rh1;
+        int i, j;
+       int rw;            /* width of the resolution level computed                                                     */
+       int rh;            /* heigth of the resolution level computed                                                    */
+       int rw1;           /* width of the resolution level once lower than computed one                                 */
+       int rh1;           /* height of the resolution level once lower than computed one                                */
 
        for (i = 0; i < l; i++) {
-               int cas_col = 0;
-               int cas_row = 0;
+               int cas_col = 0;  /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
+               int cas_row = 0;  /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
                rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
                rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
-               rw1 =
-                       tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
-               rh1 =
-                       tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
-
-               if (tilec->previous_row) {
-                       previous_rw =
-                               tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                        i].
-                               previous_x0;
-                       previous_rw1 =
-                               tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                                        i -
-                                                                                                                                                                                                                                                                                        1].
-                               previous_x0;
-                       if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
-                                       || (tilec->resolutions[l - i - 1].cas_row
-                                                       && previous_rw1 == previous_rw - previous_rw1))
-                               cas_row = 1;
-               }
-
-               if (tilec->previous_col) {
-                       previous_rh =
-                               tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                        i].
-                               previous_y0;
-                       previous_rh1 =
-                               tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                                        i -
-                                                                                                                                                                                                                                                                                        1].
-                               previous_y0;
-                       if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
-                                       || (tilec->resolutions[l - i - 1].cas_col
-                                                       && previous_rh1 == previous_rh - previous_rh1))
-                               cas_col = 1;
-               }
-
-               if (rw1 < rw - rw1)
-                       cas_row = 1;
-               if (rh1 < rh - rh1)
-                       cas_col = 1;
-
-               /* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
-               if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
-                       cas_row = 1;
-               if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
-                       cas_col = 1;
-
-               tilec->resolutions[l - i - 1].cas_row = cas_row;
-               tilec->resolutions[l - i - 1].cas_col = cas_col;
+               rw1 = tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
+               rh1 = tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
+               
+               cas_row = tilec->resolutions[l - i].x0 % 2;
+               cas_col = tilec->resolutions[l - i].y0 % 2;
 
                for (j = 0; j < rw; j++)
                        dwt_encode_1_real(a + j, rh, w, rh1, cas_col);
@@ -590,72 +397,28 @@ void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
        }
 }
 
-/* <summary> */
+/* <summary>                             */
 /* Inverse 9-7 wavelet transform in 2-D. */
-/* </summary> */
-void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
-                                                                                tcd_tilecomp_t * row_tilec,
-                                                                                tcd_tilecomp_t * col_tilec)
+/* </summary>                            */
+void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)//, tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec)
 {
-       int i, j, rw, rh, rw1, rh1;
-       int previous_rw, previous_rh, previous_rw1, previous_rh1;
+        int i, j;
+       int rw;            /* width of the resolution level computed                                                           */
+       int rh;            /* heigth of the resolution level computed                                                          */
+       int rw1;           /* width of the resolution level once lower than computed one                                       */
+       int rh1;           /* height of the resolution level once lower than computed one                                      */
 
        for (i = l - 1; i >= 0; i--) {
-               int cas_col = 0;
-               int cas_row = 0;
+         int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
+         int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
 
                rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
                rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
-               rw1 =
-                       tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
-               rh1 =
-                       tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
-
-               if (tilec->previous_row) {
-                       previous_rw =
-                               tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                        i].
-                               previous_x0;
-                       previous_rw1 =
-                               tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                                        i -
-                                                                                                                                                                                                                                                                                        1].
-                               previous_x0;
-                       if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
-                                       || (row_tilec->resolutions[l - i - 1].cas_row
-                                                       && previous_rw1 == previous_rw - previous_rw1))
-                               cas_row = 1;
-               }
-
-               if (tilec->previous_col) {
-                       previous_rh =
-                               tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                        i].
-                               previous_y0;
-                       previous_rh1 =
-                               tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
-                                                                                                                                                                                                                                                                                        i -
-                                                                                                                                                                                                                                                                                        1].
-                               previous_y0;
-                       if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
-                                       || (col_tilec->resolutions[l - i - 1].cas_col
-                                                       && previous_rh1 == previous_rh - previous_rh1))
-                               cas_col = 1;
-               }
-
-               if (rw1 < rw - rw1)
-                       cas_row = 1;
-               if (rh1 < rh - rh1)
-                       cas_col = 1;
-
-               /* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
-               if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
-                       cas_row = 1;
-               if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
-                       cas_col = 1;
+               rw1 = tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
+               rh1 = tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
 
-               tilec->resolutions[l - i - 1].cas_row = cas_row;
-               tilec->resolutions[l - i - 1].cas_col = cas_col;
+               cas_row = tilec->resolutions[l - i].x0 % 2;
+               cas_col = tilec->resolutions[l - i].y0 % 2;             
 
                for (j = 0; j < rh; j++)
                        dwt_decode_1_real(a + j * w, rw, 1, rw1, cas_row);
@@ -664,17 +427,17 @@ void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
        }
 }
 
-/* <summary> */
+/* <summary>                          */
 /* Get gain of 9-7 wavelet transform. */
-/* </summary> */
+/* </summary>                         */
 int dwt_getgain_real(int orient)
 {
        return 0;
 }
 
-/* <summary> */
+/* <summary>                */
 /* Get norm of 9-7 wavelet. */
-/* </summary> */
+/* </summary>               */
 double dwt_getnorm_real(int level, int orient)
 {
        return dwt_norms_real[orient][level];
index 60c671daf164359b20420638c8d94b25a3f27386..fc0bfa51c02462c6f740c5b8e51c2fbf08e86c2d 100644 (file)
@@ -36,6 +36,7 @@
  * a: samples of the component
  * w: width of the component
  * h: height of the component
+ * tilec : tile component information (present tile)
  * l: number of decomposition levels in the DWT
  */
 /* void dwt_encode(int* a, int w, int h, int l); */
@@ -45,11 +46,13 @@ void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l);
  * a: samples of the component
  * w: width of the component
  * h: height of the component
+ * tilec : tile component information (present tile)
  * l: number of decomposition levels in the DWT
+ * row_tilec : tile component information (previous tile on the same row)
+ * col_tilec : tile component information (previous tile on the same column)
  */
-void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
-                                                               tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec);
-/* void dwt_decode(int* a, int w, int h,tcd_tilecomp_t *tilec, int l); */
+void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l);//, tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec);
+
 /*
  * Get the gain of a subband for the reversible DWT
  * orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
@@ -79,9 +82,7 @@ void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l);
  * h: height of the component
  * l: number of decomposition levels in the DWT
  */
-void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
-                                                                                tcd_tilecomp_t * row_tilec,
-                                                                                tcd_tilecomp_t * col_tilec);
+void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l);//, tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec);
 /*
  * Get the gain of a subband for the irreversible DWT
  * orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
index 21d7c479547381443b717091b2b71f36e34e0f30..283df097fffacf3e86d3e69edac7197aeb5b3c96 100644 (file)
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-/* <summary> */
-/* Get the minimum of two integers. */
-/* </summary> */
+/*
+ * Get the minimum of two integers.
+ *
+ * returns a if a < b else b
+ */
 int int_min(int a, int b)
 {
        return a < b ? a : b;
 }
 
-/* <summary> */
-/* Get the maximum of two integers. */
-/* </summary> */
+/*
+ * Get the maximum of two integers.
+ *
+ * returns a if a > b else b
+ */
 int int_max(int a, int b)
 {
        return a > b ? a : b;
 }
 
-/* <summary> */
-/* Clamp an integer inside an interval. */
-/* </summary> */
+/*
+ * Clamp an integer inside an interval.
+ *
+ * return a if (min < a < max)
+ * return max if (a > max)
+ * return min if (a < min) 
+ */
 int int_clamp(int a, int min, int max)
 {
        if (a < min)
@@ -52,41 +60,49 @@ int int_clamp(int a, int min, int max)
        return a;
 }
 
-/* <summary> */
-/* Get absolute value of integer. */
-/* </summary> */
+/*
+ * Get absolute value of integer.
+ */
 int int_abs(int a)
 {
        return a < 0 ? -a : a;
 }
 
-/* <summary> */
-/* Divide an integer and round upwards. */
-/* </summary> */
+/*
+ * Divide an integer and round upwards.
+ *
+ * a divided by b
+ */
 int int_ceildiv(int a, int b)
 {
        return (a + b - 1) / b;
 }
 
-/* <summary> */
-/* Divide an integer by a power of 2 and round upwards. */
-/* </summary> */
+/*
+ * Divide an integer by a power of 2 and round upwards.
+ *
+ * a divided by 2^b
+ */
 int int_ceildivpow2(int a, int b)
 {
        return (a + (1 << b) - 1) >> b;
 }
 
-/* <summary> */
-/* Divide an integer by a power of 2 and round downwards. */
-/* </summary> */
+/*
+ * Divide an integer by a power of 2 and round downwards.
+ *
+ * a divided by 2^b
+ */
 int int_floordivpow2(int a, int b)
 {
        return a >> b;
 }
 
-/* <summary> */
-/* Get logarithm of an integer and round downwards. */
-/* </summary> */
+/*
+ * Get logarithm of an integer and round downwards.
+ *
+ * log2(a)
+ */
 int int_floorlog2(int a)
 {
        int l;
index 3ad9cb00f144c87f6017f4a1b88cb5e80ed6c79e..98d104cfcf3287b1ccc64c739af719dac4301241 100644 (file)
@@ -41,8 +41,8 @@
 #ifndef __J2K_H
 #define __J2K_H
 
-#define J2K_MAXRLVLS 33
-#define J2K_MAXBANDS (3*J2K_MAXRLVLS+1)
+#define J2K_MAXRLVLS 33                        /* Number of maximum resolution level authorized                   */
+#define J2K_MAXBANDS (3*J2K_MAXRLVLS+1)        /* Number of maximum sub-band linked to number of resolution level */
 
 #define J2K_CP_CSTY_PRT 0x01
 #define J2K_CP_CSTY_SOP 0x02
 #define J2K_CCP_QNTSTY_SEQNT 2
 
 typedef struct {
-       int dx, dy;                                                                             /* XRsiz, YRsiz */
-       int prec;                                                                                       /* precision */
-       int bpp;                                                                                        /* deapth of image in bits */
-       int sgnd;                                                                                       /* signed */
-       int *data;                                                                              /* image-component data */
+       int dx, dy;                             /* XRsiz, YRsiz              */
+       int prec;                               /* precision                 */
+       int bpp;                                /* deapth of image in bits   */
+       int sgnd;                               /* signed                    */
+       int *data;                              /* image-component data      */
 } j2k_comp_t;
 
 typedef struct {
-       int x0, y0;                                                                             /* XOsiz, YOsiz */
-       int x1, y1;                                                                             /* Xsiz, Ysiz  */
-       int numcomps;                                                                   /* number of components */
-       int index_on;                                                                   /* 0 = no index || 1 = index */
-       /* int PPT; */
-       j2k_comp_t *comps;                                              /* image-components */
+       int x0, y0;                             /* XOsiz, YOsiz              */
+       int x1, y1;                             /* Xsiz, Ysiz                */
+       int numcomps;                           /* number of components      */
+       int index_on;                           /* 0 = no index || 1 = index */
+       j2k_comp_t *comps;                      /* image-components          */
 } j2k_image_t;
 
 typedef struct {
-       int expn;                                                                                       /* exponent */
-       int mant;                                                                                       /* mantissa */
+       int expn;                               /* exponent                  */
+       int mant;                               /* mantissa                  */
 } j2k_stepsize_t;
 
 typedef struct {
-       int csty;                                                                                       /* coding style */
-       int numresolutions;                                             /* number of resolutions */
-       int cblkw;                                                                              /* width of code-blocks */
-       int cblkh;                                                                              /* height of code-blocks */
-       int cblksty;                                                                    /* code-block coding style */
-       int qmfbid;                                                                             /* discrete wavelet transform identifier */
-       int qntsty;                                                                             /* quantisation style */
-       j2k_stepsize_t stepsizes[J2K_MAXBANDS]; /* stepsizes used for quantisation */
-       int numgbits;                                                                   /* number of guard bits */
-       int roishift;                                                                   /* Region Of Interest shift */
-       int prcw[J2K_MAXRLVLS];                         /* Precinct width */
-       int prch[J2K_MAXRLVLS];                         /* Precinct height */
+       int csty;                               /* coding style                          */
+       int numresolutions;                     /* number of resolutions                 */
+       int cblkw;                              /* width of code-blocks                  */
+       int cblkh;                              /* height of code-blocks                 */
+       int cblksty;                            /* code-block coding style               */
+       int qmfbid;                             /* discrete wavelet transform identifier */
+       int qntsty;                             /* quantisation style                    */
+       j2k_stepsize_t stepsizes[J2K_MAXBANDS]; /* stepsizes used for quantisation       */
+       int numgbits;                           /* number of guard bits                  */
+       int roishift;                           /* Region Of Interest shift              */
+       int prcw[J2K_MAXRLVLS];                 /* Precinct width                        */
+       int prch[J2K_MAXRLVLS];                 /* Precinct height                       */
 } j2k_tccp_t;
 
 typedef struct {
@@ -104,61 +103,70 @@ typedef struct {
 } j2k_poc_t;
 
 typedef struct {
-       int csty;                                                                                       /* coding style   */
-       int prg;                                                                                        /* progression order */
-       int numlayers;                                                          /* number of layers */
-       int mct;                                                                                        /* multi-component transform identifier */
-       int rates[100];                                                         /* rates of layers */
-       int numpocs;                                                                    /* number of progression order changes  */
-       j2k_poc_t pocs[32];                                             /* progression order changes */
-       j2k_tccp_t *tccps;                                              /* tile-component coding parameters */
+        int first;                      /* 1 : first part-tile of a tile                                     */
+        int csty;                      /* coding style                                                      */
+       int prg;                        /* progression order                                                 */
+       int numlayers;                  /* number of layers                                                  */
+       int mct;                        /* multi-component transform identifier                              */
+       int rates[100];                 /* rates of layers                                                   */
+       int numpocs;                    /* number of progression order changes                               */
+        int POC;                        /* Precise if a POC marker has been used O:NO, 1:YES                 */
+       j2k_poc_t pocs[32];             /* progression order changes                                         */
+        unsigned char *ppt_data;        /* packet header store there for futur use in t2_decode_packet       */
+        int ppt;                        /* If ppt == 1 --> there was a PPT marker for the present tile       */
+        int ppt_store;                  /* Use in case of multiple marker PPT (number of info already store) */
+        j2k_tccp_t *tccps;             /* tile-component coding parameters                                  */
 } j2k_tcp_t;
 
 typedef struct {
-       int image_type;                                                         /* 0: PNM, PGM, PPM 1: PGX */
-       int disto_alloc;                                                        /* Allocation by rate/distortion */
-       int fixed_alloc;                                                        /* Allocation by fixed layer */
-       int tx0, ty0;                                                                   /* XTOsiz, YTOsiz */
-       int tdx, tdy;                                                                   /* XTsiz, YTsiz */
-       char *comment;                                                          /* comment for coding */
+       int image_type;                 /* 0: PNM, PGM, PPM 1: PGX           */
+       int disto_alloc;                /* Allocation by rate/distortion     */
+       int fixed_alloc;                /* Allocation by fixed layer         */
+       int tx0, ty0;                   /* XTOsiz, YTOsiz                    */
+       int tdx, tdy;                   /* XTsiz, YTsiz                      */
+       char *comment;                  /* comment for coding                */
        int tw, th;
-       j2k_tcp_t *tcps;                                                        /* tile coding parameters */
+        unsigned char *ppm_data;        /* packet header store there for futur use in t2_decode_packet             */
+        int ppm;                        /* If ppm == 1 --> there was a PPM marker for the present tile             */
+        int ppm_store;                  /* Use in case of multiple marker PPM (number of info already store)       */
+        int ppm_previous;               /* Use in case of multiple marker PPM (case on non-finished previous info) */
+       j2k_tcp_t *tcps;                /* tile coding parameters                                                  */
+        int *matrice;                   /* Fixed layer                                                             */
 } j2k_cp_t;
 
 typedef struct {
-       int start_pos, end_pos;                         /* start and end position */
-       double disto;
-} info_packet;                                                                 /* Index struct */
+       int start_pos, end_pos;         /* start and end position            */
+       double disto;                   /* ADD for Marcela                   */
+} info_packet;                         /* Index struct                      */
 
 typedef struct {
-       int num_tile;                                                                   /* Number of Tile */
-       int start_pos;                                                          /* Start position */
-       int end_header;                                                         /* End position of the header */
-       int end_pos;                                                                    /* End position */
-       int pw, ph;                                                                             /* number of precinct by tile */
-       info_packet *packet;                                    /* information concerning packets inside tile */
-} info_tile;                                                                           /* index struct */
+        double *thresh;                 /* value of thresh for each layer by tile cfr. Marcela   */
+       int num_tile;                   /* Number of Tile                                        */
+       int start_pos;                  /* Start position                                        */
+       int end_header;                 /* End position of the header                            */
+       int end_pos;                    /* End position                                          */
+       int pw, ph;                     /* number of precinct by tile                            */
+       info_packet *packet;            /* information concerning packets inside tile            */
+} info_tile;                           /* index struct                                          */
 
 typedef struct {
        int index_on;
-       double D_max;                                                                   /* ADD for Marcela */
-       int num;                                                                                        /* numero of packet */
-       int index_write;                                                        /* writing the packet inthe index with t2_encode_packets */
-       int Im_w, Im_h;                                                         /* Image width and Height */
-       int Prog;                                                                                       /* progression order */
-       int Tile_x, Tile_y;                                             /* Number of Tile in X and Y */
+       double D_max;                   /* ADD for Marcela                                       */
+       int num;                        /* numero of packet                                      */
+       int index_write;                /* writing the packet inthe index with t2_encode_packets */
+       int Im_w, Im_h;                 /* Image width and Height                                */
+       int Prog;                       /* progression order                                     */
+       int Tile_x, Tile_y;             /* Number of Tile in X and Y                             */
        int tw, th;
-       int Comp;                                                                                       /* Component numbers */
-       int Layer;                                                                              /* number of layer */
-       int Decomposition;                                              /* number of decomposition */
-       int pw, ph;                                                                             /* nombre precinct in X and Y */
-       int pdx, pdy;                                                                   /* size of precinct in X and Y */
-       int Main_head_end;                                              /* Main header position */
-       int codestream_size;                                    /* codestream's size */
-       info_tile *tile;                                                        /* information concerning tiles inside image */
-} info_image;                                                                          /* index struct */
-
-
+        int Comp;                      /* Component numbers                                     */
+       int Layer;                      /* number of layer                                       */
+       int Decomposition;              /* number of decomposition                               */
+       int pw, ph;                     /* nombre precinct in X and Y                            */
+       int pdx, pdy;                   /* size of precinct in X and Y                           */
+       int Main_head_end;              /* Main header position                                  */
+       int codestream_size;            /* codestream's size                                     */
+       info_tile *tile;                /* information concerning tiles inside image             */
+} info_image;                          /* index struct                                          */
 
 /* 
  * Encode an image into a JPEG-2000 codestream
@@ -168,8 +176,7 @@ typedef struct {
  * len: length of destination buffer
  * index : index file name
  */
-LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *outfile,
-                                                                                                       int len, char *index);
+LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *outfile, int len, char *index);
 
 /* LIBJ2K_API int j2k_encode(j2k_image_t *i, j2k_cp_t *cp,unsigned char *dest, int len); */
 /*
@@ -179,7 +186,17 @@ LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *outfile,
  * i: decode image
  * cp: coding parameters that were used to encode the image
  */
-LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** i,
-                                                                                                       j2k_cp_t ** cp);
+LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** img, j2k_cp_t ** cp);
+
+
+/*
+ * Decode an image form a JPT-stream (JPEG 2000, JPIP)
+ * src: source buffer
+ * len: length of source buffer
+ * i: decode image
+ * cp: coding parameters that were used to encode the image
+ *
+ */
+int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t ** img, j2k_cp_t ** cp);
 
 #endif
index 3f6eb921b13097ccf22a878cb55dd458ba17aeca..4e6c319cfa182293181d1e451c5768dacb69b60c 100644 (file)
 /* This struct defines the state of a context. */
 /* </summary> */
 typedef struct mqc_state_s {
-       unsigned int qeval;                                             /* the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */
-       int mps;                                                                                        /* the Most Probable Symbol (0 or 1) */
-       struct mqc_state_s *nmps;                       /* next state if the next encoded symbol is the MPS */
-       struct mqc_state_s *nlps;                       /* next state if the next encoded symbol is the LPS */
+       unsigned int qeval;             /* the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */
+       int mps;                        /* the Most Probable Symbol (0 or 1) */
+       struct mqc_state_s *nmps;       /* next state if the next encoded symbol is the MPS */
+       struct mqc_state_s *nlps;       /* next state if the next encoded symbol is the LPS */
 } mqc_state_t;
 
 /* <summary> */
@@ -318,9 +318,9 @@ void mqc_bypass_init_enc()
 {
        mqc_c = 0;
        mqc_ct = 8;
-       if (*mqc_bp == 0xff) {
-               mqc_ct = 7;
-       }
+       /*if (*mqc_bp == 0xff) {
+         mqc_ct = 7;
+         }*/
 }
 
 /* <summary> */
@@ -332,15 +332,16 @@ void mqc_bypass_enc(int d)
 {
        mqc_ct--;
        mqc_c = mqc_c + (d << mqc_ct);
-       if (mqc_ct == 0) {
-               mqc_bp++;
-               *mqc_bp = mqc_c;
-               mqc_ct = 8;
-               if (*mqc_bp == 0xff) {
-                       mqc_ct = 7;
-               }
-               mqc_c = 0;
-       }
+       if (mqc_ct == 0)
+         {
+           mqc_bp++;
+           *mqc_bp = mqc_c;
+           mqc_ct = 8;
+           if (*mqc_bp == 0xff) {
+             mqc_ct = 7;
+           }
+           mqc_c = 0;
+         }
 }
 
 /* <summary> */
@@ -405,6 +406,7 @@ int mqc_restart_enc()
 void mqc_restart_init_enc()
 {
        /* <Re-init part> */
+        mqc_setcurctx(0);
        mqc_a = 0x8000;
        mqc_c = 0;
        mqc_ct = 12;
@@ -412,7 +414,6 @@ void mqc_restart_init_enc()
        if (*mqc_bp == 0xff) {
                mqc_ct = 13;
        }
-
 }
 
 
index 3d29a6d2f2ef99ffb608dfbbcfa5eaa798f4f55f..ea01d809f6773be40b0a7f474b11c2c12c907d5f 100644 (file)
@@ -79,7 +79,7 @@ void mqc_bypass_init_enc();
 /*
  * BYPASS mode switch
  */
-void mqc_bypass_enc();
+void mqc_bypass_enc(int d);
 
 /*
  * BYPASS mode switch
index eca2c4513953b87dbbe694cb740f48396ecd0e7d..42bfab29be1d59dbdd50cec5923291f771671c80 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2001-2002, David Janssens
- * Copyright (c) 2003, Yannick Verschueren
- * Copyright (c) 2003 Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2003-2004, Yannick Verschueren
+ * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include <stdlib.h>
 #include <stdio.h>
 
-
-/* <summary> */
-/* Create a packet iterator.   */
-/* </summary> */
+/* <summary>
+ * Create a packet iterator.
+ * </summary> */
 pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
 {
-       int p, q;
-       int compno, resno, pino, layno, precno;
+       int p, q, i;
+       int compno, resno, pino;
        int maxres = 0;
        pi_iterator_t *pi;
        j2k_tcp_t *tcp;
        j2k_tccp_t *tccp;
 
        tcp = &cp->tcps[tileno];
-       pi =
-               (pi_iterator_t *) malloc((tcp->numpocs + 1) * sizeof(pi_iterator_t));
+       pi = (pi_iterator_t *) malloc((tcp->numpocs + 1) * sizeof(pi_iterator_t));
 
        for (pino = 0; pino < tcp->numpocs + 1; pino++) {       /* change */
                p = tileno % cp->tw;
                q = tileno / cp->tw;
 
-               /*    pi->tx0=int_max(cp->tx0+p*cp->tdx, img->x0);
-                  pi->ty0=int_max(cp->ty0+q*cp->tdy, img->y0);
-                  pi->tx1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1);
-                  pi->ty1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1);
-                  pi->numcomps=img->numcomps;
-                  pi->comps=(pi_comp_t*)malloc(img->numcomps*sizeof(pi_comp_t)); */
-
                pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
                pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
                pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
                pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
                pi[pino].numcomps = img->numcomps;
-               pi[pino].comps =
-                       (pi_comp_t *) malloc(img->numcomps * sizeof(pi_comp_t));
+               pi[pino].comps = (pi_comp_t *) malloc(img->numcomps * sizeof(pi_comp_t));
 
                for (compno = 0; compno < pi->numcomps; compno++) {
                        int tcx0, tcy0, tcx1, tcy1;
-                       /* pi_comp_t *comp=&pi->comps[compno]; */
                        pi_comp_t *comp = &pi[pino].comps[compno];
                        tccp = &tcp->tccps[compno];
                        comp->dx = img->comps[compno].dx;
                        comp->dy = img->comps[compno].dy;
                        comp->numresolutions = tccp->numresolutions;
-                       comp->resolutions =
-                               (pi_resolution_t *) malloc(comp->numresolutions *
-                                                                                                                                        sizeof(pi_resolution_t));
+                       comp->resolutions = (pi_resolution_t *) malloc(comp->numresolutions * sizeof(pi_resolution_t));
                        tcx0 = int_ceildiv(pi->tx0, comp->dx);
                        tcy0 = int_ceildiv(pi->ty0, comp->dy);
                        tcx1 = int_ceildiv(pi->tx1, comp->dx);
@@ -110,30 +97,24 @@ pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
                                res->ph = (py1 - py0) >> res->pdy;
                        }
                }
+               
+               tccp = &tcp->tccps[0];
+               pi[pino].step_p=1;
+               pi[pino].step_c=100*pi[pino].step_p;
+               pi[pino].step_r=img->numcomps*pi[pino].step_c;
+               pi[pino].step_l=maxres*pi[pino].step_r;
 
-               /*   pi->first=1;
-                  pi->poc.resno0=0;
-                  pi->poc.compno0=0;
-                  pi->poc.layno1=tcp->numlayers;
-                  pi->poc.resno1=maxres;
-                  pi->poc.compno1=img->numcomps;
-                  pi->poc.prg=tcp->prg; */
-
-               for (layno = 0; layno < 10; layno++) {
-                       /* pi_comp_t *compo; */
-                       /* pi_resolution_t *res; */
-                       for (resno = 0; resno < 10; resno++) {
-                               for (compno = 0; compno < 3; compno++) {
-                                       /* compo=&pi[pino].comps[compno]; */
-                                       /* res=&compo->resolutions[pi->resno]; */
-                                       for (precno = 0; precno < 99; precno++) {
-                                               pi[pino].include[layno][resno][compno][precno] = 0;
-                                       }
-                               }
-                       }
-               }
+               if (pino==0)
+                 {  
+                   pi[pino].include = (short int*)malloc(img->numcomps*maxres*tcp->numlayers*100*sizeof(short int));
+                   for (i=0 ; i<img->numcomps*maxres*tcp->numlayers*100; i++)
+                     pi[pino].include[i]=0;
+                 }
+               /* pi[pino].include=(short int*)calloc(img->numcomps*maxres*tcp->numlayers*1000,sizeof(short int));*/
+               else
+                 pi[pino].include=pi[pino-1].include;
 
-               if (pino == tcp->numpocs) {
+               if (tcp->POC == 0) {
                        pi[pino].first = 1;
                        pi[pino].poc.resno0 = 0;
                        pi[pino].poc.compno0 = 0;
@@ -154,9 +135,11 @@ pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
        return pi;
 }
 
-/* <summary> */
-/* Get next packet in layer=resolution-component-precinct order.   */
-/* </summary> */
+/* <summary>
+ * Get next packet in layer-resolution-component-precinct order.
+ * 
+ * pi: packet iterator to modify
+ * </summary> */
 int pi_next_lrcp(pi_iterator_t * pi)
 {
        pi_comp_t *comp;
@@ -170,32 +153,32 @@ int pi_next_lrcp(pi_iterator_t * pi)
                pi->first = 0;
        }
        for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
-               for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
-                                pi->resno++) {
-                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
-                                        pi->compno++) {
-                               comp = &pi->comps[pi->compno];
-                               if (pi->resno >= comp->numresolutions) {
-
-                                       continue;
-                               }
-                               res = &comp->resolutions[pi->resno];
-                               for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {
-                                       if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
-                                               pi->include[pi->layno][pi->resno][pi->compno][pi->precno] = 1;
-                                               return 1;
-                                       }
-                               skip:;
-                               }
-                       }
+         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
+           for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+             comp = &pi->comps[pi->compno];
+             if (pi->resno >= comp->numresolutions) {
+               continue;
+             }
+             res = &comp->resolutions[pi->resno];
+             for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) 
+               {
+                 if (!pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){
+                   pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1;
+                   return 1;
+                 }
+               skip:;
                }
+           }
+         }
        }
        return 0;
 }
 
-/* <summary> */
-/* Get next packet in resolution-layer-component-precinct order.   */
-/* </summary> */
+/* <summary>
+ * Get next packet in resolution-layer-component-precinct order.
+ *
+ * pi: packet iterator to modify
+ * </summary> */
 int pi_next_rlcp(pi_iterator_t * pi)
 {
        pi_comp_t *comp;
@@ -210,28 +193,30 @@ int pi_next_rlcp(pi_iterator_t * pi)
        for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
                for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
                        for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
-                                        pi->compno++) {
-                               comp = &pi->comps[pi->compno];
-                               if (pi->resno >= comp->numresolutions) {
-                                       continue;
-                               }
-                               res = &comp->resolutions[pi->resno];
-                               for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {
-                                       if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
-                                               pi->include[pi->layno][pi->resno][pi->compno][pi->precno] = 1;
-                                               return 1;
-                                       }
-                               skip:;
-                               }
+                            pi->compno++) {
+                         comp = &pi->comps[pi->compno];
+                         if (pi->resno >= comp->numresolutions) {
+                           continue;
+                         }
+                         res = &comp->resolutions[pi->resno];
+                         for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {
+                           if (!pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){
+                             pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1;
+                             return 1;
+                           }
+                         skip:;
+                         }
                        }
                }
        }
        return 0;
 }
 
-/* <summary> */
-/* Get next packet in resolution-precinct-component-layer order.   */
-/* </summary> */
+/* <summary>
+ * Get next packet in resolution-precinct-component-layer order.
+ *
+ * pi: packet iterator to modify
+ * </summary> */
 int pi_next_rpcl(pi_iterator_t * pi)
 {
        pi_comp_t *comp;
@@ -248,22 +233,17 @@ int pi_next_rpcl(pi_iterator_t * pi)
                        for (resno = 0; resno < comp->numresolutions; resno++) {
                                int dx, dy;
                                res = &comp->resolutions[resno];
-                               dx =
-                                       comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
-                               dy =
-                                       comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
+                               dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
+                               dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
                                pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
                                pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
                        }
                }
        }
        for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
-               for (pi->y = pi->ty0; pi->y < pi->ty1;
-                                pi->y += pi->dy - (pi->y % pi->dy)) {
-                       for (pi->x = pi->tx0; pi->x < pi->tx1;
-                                        pi->x += pi->dx - (pi->x % pi->dx)) {
-                               for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
-                                                pi->compno++) {
+               for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
+                       for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
+                               for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
                                        int levelno;
                                        int trx0, try0;
                                        int rpx, rpy;
@@ -278,28 +258,21 @@ int pi_next_rpcl(pi_iterator_t * pi)
                                        try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
                                        rpx = res->pdx + levelno;
                                        rpy = res->pdy + levelno;
-                                       if (!
-                                                       (pi->x % (comp->dx << rpx) == 0
-                                                        || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
+                                       if (!(pi->x % (comp->dx << rpx) == 0 || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
                                                continue;
                                        }
-                                       if (!
-                                                       (pi->y % (comp->dy << rpy) == 0
-                                                        || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
+                                       if (!(pi->y % (comp->dy << rpy) == 0 || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
                                                continue;
                                        }
-                                       prci =
-                                               int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno),
-                                                                                                                res->pdx) - int_floordivpow2(trx0, res->pdx);
-                                       prcj =
-                                               int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno),
-                                                                                                                res->pdy) - int_floordivpow2(try0, res->pdy);
+                                       prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno),
+                                                               res->pdx) - int_floordivpow2(trx0, res->pdx);
+                                       prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno),
+                                                               res->pdy) - int_floordivpow2(try0, res->pdy);
                                        pi->precno = prci + prcj * res->pw;
                                        for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
-                                               if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
-                                                       pi->include[pi->layno][pi->resno][pi->compno][pi->precno] =
-                                                               1;
-                                                       return 1;
+                                         if (!pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){
+                                           pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1;                     
+                                           return 1;
                                                }
                                        skip:;
                                        }
@@ -310,9 +283,11 @@ int pi_next_rpcl(pi_iterator_t * pi)
        return 0;
 }
 
-/* <summary> */
-/* Get next packet in precinct-component-resolution-layer order.   */
-/* </summary> */
+/* <summary>
+ * Get next packet in precinct-component-resolution-layer order.
+ *
+ * pi: packet iterator to modify
+ * </summary> */
 int pi_next_pcrl(pi_iterator_t * pi)
 {
        pi_comp_t *comp;
@@ -330,25 +305,18 @@ int pi_next_pcrl(pi_iterator_t * pi)
                        for (resno = 0; resno < comp->numresolutions; resno++) {
                                int dx, dy;
                                res = &comp->resolutions[resno];
-                               dx =
-                                       comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
-                               dy =
-                                       comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
+                               dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
+                               dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
                                pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
                                pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
                        }
                }
        }
-       for (pi->y = pi->ty0; pi->y < pi->ty1;
-                        pi->y += pi->dy - (pi->y % pi->dy)) {
-               for (pi->x = pi->tx0; pi->x < pi->tx1;
-                                pi->x += pi->dx - (pi->x % pi->dx)) {
-                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
-                                        pi->compno++) {
+       for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
+               for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
+                       for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
                                comp = &pi->comps[pi->compno];
-                               for (pi->resno = pi->poc.resno0;
-                                                pi->resno < int_min(pi->poc.resno1, comp->numresolutions);
-                                                pi->resno++) {
+                               for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
                                        int levelno;
                                        int trx0, try0;
                                        int rpx, rpy;
@@ -359,27 +327,18 @@ int pi_next_pcrl(pi_iterator_t * pi)
                                        try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
                                        rpx = res->pdx + levelno;
                                        rpy = res->pdy + levelno;
-                                       if (!
-                                                       (pi->x % (comp->dx << rpx) == 0
-                                                        || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
+                                       if (!(pi->x % (comp->dx << rpx) == 0 || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
                                                continue;
                                        }
-                                       if (!
-                                                       (pi->y % (comp->dy << rpy) == 0
-                                                        || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
+                                       if (!(pi->y % (comp->dy << rpy) == 0 || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
                                                continue;
                                        }
-                                       prci =
-                                               int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno),
-                                                                                                                res->pdx) - int_floordivpow2(trx0, res->pdx);
-                                       prcj =
-                                               int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno),
-                                                                                                                res->pdy) - int_floordivpow2(try0, res->pdy);
+                                       prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) - int_floordivpow2(trx0, res->pdx);
+                                       prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) - int_floordivpow2(try0, res->pdy);
                                        pi->precno = prci + prcj * res->pw;
                                        for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
-                                               if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
-                                                       pi->include[pi->layno][pi->resno][pi->compno][pi->precno] =
-                                                               1;
+                                         if (! pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){
+                                           pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1;
                                                        return 1;
                                                }
                                        skip:;
@@ -391,9 +350,11 @@ int pi_next_pcrl(pi_iterator_t * pi)
        return 0;
 }
 
-/* <summary> */
-/* Get next packet in component-precinct-resolution-layer order.   */
-/* </summary> */
+/* <summary>
+ * Get next packet in component-precinct-resolution-layer order.
+ *
+ * pi: packet iterator to modify
+ * </summary> */
 int pi_next_cprl(pi_iterator_t * pi)
 {
        pi_comp_t *comp;
@@ -435,28 +396,19 @@ int pi_next_cprl(pi_iterator_t * pi)
                                        try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
                                        rpx = res->pdx + levelno;
                                        rpy = res->pdy + levelno;
-                                       if (!
-                                                       (pi->x % (comp->dx << rpx) == 0
-                                                        || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
+                                       if (!(pi->x % (comp->dx << rpx) == 0 || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) {
                                                continue;
                                        }
-                                       if (!
-                                                       (pi->y % (comp->dy << rpy) == 0
-                                                        || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
+                                       if (!(pi->y % (comp->dy << rpy) == 0 || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
                                                continue;
                                        }
-                                       prci =
-                                               int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno),
-                                                                                                                res->pdx) - int_floordivpow2(trx0, res->pdx);
-                                       prcj =
-                                               int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno),
-                                                                                                                res->pdy) - int_floordivpow2(try0, res->pdy);
+                                       prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) - int_floordivpow2(trx0, res->pdx);
+                                       prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) - int_floordivpow2(try0, res->pdy);
                                        pi->precno = prci + prcj * res->pw;
                                        for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
-                                               if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
-                                                       pi->include[pi->layno][pi->resno][pi->compno][pi->precno] =
-                                                               1;
-                                                       return 1;
+                                         if (! pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){
+                                           pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1;
+                                           return 1;
                                                }
                                        skip:;
                                        }
@@ -467,9 +419,11 @@ int pi_next_cprl(pi_iterator_t * pi)
        return 0;
 }
 
-/* <summary> */
-/* Get next packet.   */
-/* </summary> */
+/* <summary>
+ * Get next packet.
+  *
+  * pi: packet iterator to modify
+  * </summary> */
 int pi_next(pi_iterator_t * pi)
 {
        switch (pi->poc.prg) {
index 5403faf4aa593d44a4b9afe985bf4be824fb0403..f450304a8d291dedc0c03cf280506287b77a5ea9 100644 (file)
@@ -42,15 +42,16 @@ typedef struct {
 } pi_comp_t;
 
 typedef struct {
-       int include[10][10][3][99];
+        short int *include;                     /* precise if the packet has been already used (usefull for progression order change) */
+        int step_l, step_r, step_c, step_p;     /* different steps (layer, resolution, component, precinct) to localize the packet in the include vector */ 
        int compno, resno, precno, layno;       /* component, resolution, precinct and layer that indentify the packet */
-       int first;
+        int first;                              /* 0 if the first packet */
        j2k_poc_t poc;
        int numcomps;
        pi_comp_t *comps;
        int tx0, ty0, tx1, ty1;
        int x, y, dx, dy;
-} pi_iterator_t;                                                               /* packet iterator */
+} pi_iterator_t;                               /* packet iterator */
 
 /*
  * Create a packet iterator
index bdfe624218ccaf96d5a8ebba37651e6de599ef2a..180a1decd58bef5e95c69981b467ebce3655eccf 100644 (file)
 #include "raw.h"
 
 
-unsigned char raw_c;
-unsigned int raw_ct, raw_lenmax, raw_len;
-unsigned char *raw_bp;
-unsigned char *raw_start;
-unsigned char *raw_end;
+unsigned char raw_c;       /* temporary buffer where bits are coded or decoded */
+unsigned int raw_ct;       /* number of bits already read or free to write */
+unsigned int raw_lenmax;   /* maximum length to decode */
+unsigned int raw_len;      /* length decoded */
+unsigned char *raw_bp;     /* pointer to the current position in the buffer */
+unsigned char *raw_start;  /* pointer to the start of the buffer */
+unsigned char *raw_end;    /* pointer to the end of the buffer */
 
-/* <summary> */
-/* Return the number of bytes already encoded. */
-/* </summary> */
+/*
+ * Return the number of bytes already encoded.
+ */
 int raw_numbytes()
 {
        return raw_bp - raw_start;
 }
 
-/* <summary> */
-/* Initialize raw-encoder. */
-/* </summary> */
-/* <param name="bp">Output buffer.</param> */
-void raw_init_enc(unsigned char *bp)
-{
-       raw_bp = bp - 1;
-       raw_c = 0;
-       raw_ct = 7;
-       raw_start = bp;
-}
-
-/* <summary> */
-/* Encode a symbol using the RAW-coder. */
-/* </summary> */
-/* <param name="d"> The symbol to be encoded (0 or 1).</param> */
-void raw_encode(int d)
-{
-       /*  raw_c+=d; */
-
-       raw_ct--;
-       raw_c += (d << raw_ct);
-
-       if (raw_ct == 0) {
-               raw_bp++;
-               *raw_bp = raw_c;
-               raw_ct = 7;
-               if (raw_c == 0xff) {
-                       raw_ct = 6;
-               }
-               raw_c = 0;
-       }
-       /*else 
-          {
-          raw_ct--;
-          raw_c<<=1;
-          } */
-}
-
-/* <summary> */
-/* Flush encoded data. */
-/* </summary> */
-void raw_flush()
-{
-       char first = 1;
-       int prev = 1;
-       while (raw_ct != 7) {
-               raw_encode(first ? 0 : !(prev));
-               prev = first ? 0 : !(prev);
-               first = 0;
-       }
-}
-
-/* <summary> */
-/* Initialize raw-decoder. */
-/* </summary> */
+/*
+ * Initialize raw-decoder.
+ *
+ * bp  : pointer to the start of the buffer from which the bytes will be read
+ * len : length of the input buffer
+ */
 void raw_init_dec(unsigned char *bp, int len)
 {
        raw_start = bp;
@@ -106,9 +58,9 @@ void raw_init_dec(unsigned char *bp, int len)
        raw_ct = 0;
 }
 
-/* <summary> */
-/* Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN */
-/* </summary> */
+/*
+ * Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN
+ */
 int raw_decode()
 {
        int d;
index eede21a5ac34361f337f2ac4466135ed485a72bd..08b20bc63c77ee67d40afc5f59e9cbfd5cb202d8 100644 (file)
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
- */  
-       
+ */
+
 #ifndef __RAW_H
 #define __RAW_H
-       
+
 /*
  * Return the number of bytes written/read since initialisation
- */ 
+ */
 int raw_numbytes();
 
-
-/*
- * Initialize the encoder
- * bp: pointer to the start of the buffer where the bytes will be written
- */ 
-void raw_init_enc(unsigned char *bp);
-
-
-/*
- * Encode a bit
- * d: bit to encode (0 or 1)
- */ 
-void raw_encode(int d);
-
-
-/*
- * Flush the encoder, so that all remaining data is written
- */ 
-void raw_flush();
-
-
 /*
  * Initialize the decoder
  * bp: pointer to the start of the buffer from which the bytes will be read
  * len: length of the input buffer
- */ 
+ */
 void raw_init_dec(unsigned char *bp, int len);
 
-
 /*
  * Decode a bit (returns 0 or 1)
- */ 
+ */
 int raw_decode();
 
-
-#endif /* 
- */
+#endif
index 2e7853048e7f1739532d821c6662695a906565d6..9aa024e2f611382b731f7449c316fdae4701d232 100644 (file)
@@ -107,8 +107,7 @@ int t1_getctxno_sc(int f)
 
 int t1_getctxno_mag(int f)
 {
-       return t1_lut_ctxno_mag[(f & T1_SIG_OTH) |
-                                                                                                       (((f & T1_REFINE) != 0) << 11)];
+       return t1_lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
 }
 
 int t1_getspb(int f)
@@ -119,8 +118,7 @@ int t1_getspb(int f)
 int t1_getnmsedec_sig(int x, int bitpos)
 {
        if (bitpos > T1_NMSEDEC_FRACBITS)
-               return t1_lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &
-                                                                                                                       ((1 << T1_NMSEDEC_BITS) - 1)];
+               return t1_lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
        else
                return t1_lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
 }
@@ -128,8 +126,7 @@ int t1_getnmsedec_sig(int x, int bitpos)
 int t1_getnmsedec_ref(int x, int bitpos)
 {
        if (bitpos > T1_NMSEDEC_FRACBITS)
-               return t1_lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &
-                                                                                                                       ((1 << T1_NMSEDEC_BITS) - 1)];
+               return t1_lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
        else
                return t1_lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
 }
@@ -154,17 +151,14 @@ void t1_updateflags(int *fp, int s)
        }
 }
 
-void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
-                                                                                                int *nmsedec, char type, int vsc)
+void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc)
 {
        int v, flag;
-       flag =
-               vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
-               : (*fp);
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
        if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
                v = int_abs(*dp) & one ? 1 : 0;
                if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
-                       mqc_setcurctx(t1_getctxno_zc(flag, orient));    /* ESSAI */
+                       mqc_setcurctx(t1_getctxno_zc(flag, orient));    /* ESSAI */
                        mqc_bypass_enc(v);
                } else {
                        mqc_setcurctx(t1_getctxno_zc(flag, orient));
@@ -172,8 +166,7 @@ void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
                }
                if (v) {
                        v = *dp < 0 ? 1 : 0;
-                       *nmsedec +=
-                               t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
+                       *nmsedec += t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
                        if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
                                mqc_setcurctx(t1_getctxno_sc(flag));    /* ESSAI */
                                mqc_bypass_enc(v);
@@ -188,8 +181,7 @@ void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
        }
 }
 
-void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
-                                                                                                char type, int vsc)
+void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf, char type, int vsc)
 {
        int v, flag;
        flag =
@@ -215,10 +207,9 @@ void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
                }
                *fp |= T1_VISIT;
        }
-}                                                                                                                              /* VSC and  BYPASS by Antonin */
+}              /* VSC and  BYPASS by Antonin */
 
-void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,
-                                                                               char type, int cblksty)
+void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty)
 {
        int i, j, k, one, vsc;
        *nmsedec = 0;
@@ -226,47 +217,39 @@ void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,
        for (k = 0; k < h; k += 4) {
                for (i = 0; i < w; i++) {
                        for (j = k; j < k + 4 && j < h; j++) {
-                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
-                                                        && (j == k + 3 || j == h - 1)) ? 1 : 0;
-                               t1_enc_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
-                                                                                                               orient, bpno, one, nmsedec, type, vsc);
+                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                               t1_enc_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, bpno, one, nmsedec, type, vsc);
                        }
                }
        }
 }
 
-void t1_dec_sigpass(int w, int h, int bpno, int orient, char type,
-                                                                               int cblksty)
+void t1_dec_sigpass(int w, int h, int bpno, int orient, char type, int cblksty)
 {
        int i, j, k, one, half, oneplushalf, vsc;
        one = 1 << bpno;
        half = one >> 1;
        oneplushalf = one | half;
        for (k = 0; k < h; k += 4) {
-               for (i = 0; i < w; i++) {
-                       for (j = k; j < k + 4 && j < h; j++) {
-                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
-                                                        && (j == k + 3 || j == h - 1)) ? 1 : 0;
-                               t1_dec_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
-                                                                                                               orient, oneplushalf, type, vsc);
-                       }
-               }
+         for (i = 0; i < w; i++) {
+           for (j = k; j < k + 4 && j < h; j++) {
+             vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
+                    && (j == k + 3 || j == h - 1)) ? 1 : 0;
+             t1_dec_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, oneplushalf, type, vsc);
+           }
+         }
        }
-}                                                                                                                              /* VSC and  BYPASS by Antonin */
+}              /* VSC and  BYPASS by Antonin */
 
-void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
-                                                                                                char type, int vsc)
+void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc)
 {
        int v, flag;
-       flag =
-               vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
-               : (*fp);
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
-               *nmsedec +=
-                       t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
+               *nmsedec += t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
                v = int_abs(*dp) & one ? 1 : 0;
                if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
-                       mqc_setcurctx(t1_getctxno_mag(flag));   /* ESSAI */
+                       mqc_setcurctx(t1_getctxno_mag(flag));   /* ESSAI */
                        mqc_bypass_enc(v);
                } else {
                        mqc_setcurctx(t1_getctxno_mag(flag));
@@ -276,13 +259,10 @@ void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
        }
 }
 
-void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
-                                                                                                char type, int vsc)
+void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf, char type, int vsc)
 {
        int v, t, flag;
-       flag =
-               vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
-               : (*fp);
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
                if (type == T1_TYPE_RAW) {
                        mqc_setcurctx(t1_getctxno_mag(flag));   /* ESSAI */
@@ -295,10 +275,9 @@ void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
                *dp += *dp < 0 ? -t : t;
                *fp |= T1_REFINE;
        }
-}                                                                                                                              /* VSC and  BYPASS by Antonin  */
+}      /* VSC and  BYPASS by Antonin  */
 
-void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,
-                                                                               int cblksty)
+void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type, int cblksty)
 {
        int i, j, k, one, vsc;
        *nmsedec = 0;
@@ -306,10 +285,8 @@ void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,
        for (k = 0; k < h; k += 4) {
                for (i = 0; i < w; i++) {
                        for (j = k; j < k + 4 && j < h; j++) {
-                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
-                                                        && (j == k + 3 || j == h - 1)) ? 1 : 0;
-                               t1_enc_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], bpno,
-                                                                                                               one, nmsedec, type, vsc);
+                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                               t1_enc_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], bpno, one, nmsedec, type, vsc);
                        }
                }
        }
@@ -325,22 +302,17 @@ void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)
        for (k = 0; k < h; k += 4) {
                for (i = 0; i < w; i++) {
                        for (j = k; j < k + 4 && j < h; j++) {
-                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
-                                                        && (j == k + 3 || j == h - 1)) ? 1 : 0;
-                               t1_dec_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
-                                                                                                               poshalf, neghalf, type, vsc);
+                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)  && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                               t1_dec_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], poshalf, neghalf, type, vsc);
                        }
                }
        }
-}                                                                                                                              /* VSC and  BYPASS by Antonin */
+}      /* VSC and  BYPASS by Antonin */
 
-void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
-                                                                                                int *nmsedec, int partial, int vsc)
+void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc)
 {
        int v, flag;
-       flag =
-               vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
-               : (*fp);
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
        if (partial)
                goto label_partial;
        if (!(*fp & (T1_SIG | T1_VISIT))) {
@@ -349,8 +321,7 @@ void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
                mqc_encode(v);
                if (v) {
                label_partial:
-                       *nmsedec +=
-                               t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
+                       *nmsedec += t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
                        mqc_setcurctx(t1_getctxno_sc(flag));
                        v = *dp < 0 ? 1 : 0;
                        mqc_encode(v ^ t1_getspb(flag));
@@ -361,13 +332,10 @@ void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
        *fp &= ~T1_VISIT;
 }
 
-void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
-                                                                                                int partial, int vsc)
+void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc)
 {
        int v, flag;
-       flag =
-               vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
-               : (*fp);
+       flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
        if (partial)
                goto label_partial;
        if (!(flag & (T1_SIG | T1_VISIT))) {
@@ -382,10 +350,9 @@ void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
                }
        }
        *fp &= ~T1_VISIT;
-}                                                                                                                              /* VSC and  BYPASS by Antonin */
+}      /* VSC and  BYPASS by Antonin */
 
-void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
-                                                                               int cblksty)
+void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec, int cblksty)
 {
        int i, j, k, one, agg, runlen, vsc;
        *nmsedec = 0;
@@ -395,26 +362,15 @@ void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
                        if (k + 3 < h) {
                                if (cblksty & J2K_CCP_CBLKSTY_VSC) {
                                        agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
-                                                                       || t1_flags[1 + k + 1][1 +
-                                                                                                                                                                i] & (T1_SIG | T1_VISIT |
-                                                                                                                                                                                        T1_SIG_OTH)
-                                                                       || t1_flags[1 + k + 2][1 +
-                                                                                                                                                                i] & (T1_SIG | T1_VISIT |
-                                                                                                                                                                                        T1_SIG_OTH)
-                                                                       || (t1_flags[1 + k + 3][1 + i] &
-                                                                                       (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
-                                                                       & (T1_SIG | T1_VISIT | T1_SIG_OTH));
+                                               || t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT |
+                                               T1_SIG_OTH) || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT |
+                                               T1_SIG_OTH) || (t1_flags[1 + k + 3][1 + i] & (~(T1_SIG_S | T1_SIG_SE 
+                                               | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
                                } else {
                                        agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
-                                                                       || t1_flags[1 + k + 1][1 +
-                                                                                                                                                                i] & (T1_SIG | T1_VISIT |
-                                                                                                                                                                                        T1_SIG_OTH)
-                                                                       || t1_flags[1 + k + 2][1 +
-                                                                                                                                                                i] & (T1_SIG | T1_VISIT |
-                                                                                                                                                                                        T1_SIG_OTH)
-                                                                       || t1_flags[1 + k + 3][1 +
-                                                                                                                                                                i] & (T1_SIG | T1_VISIT |
-                                                                                                                                                                                        T1_SIG_OTH));
+                                               || t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                               || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+                                               || t1_flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
                                }
                        } else {
                                agg = 0;
@@ -436,11 +392,9 @@ void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
                                runlen = 0;
                        }
                        for (j = k + runlen; j < k + 4 && j < h; j++) {
-                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
-                                                        && (j == k + 3 || j == h - 1)) ? 1 : 0;
-                               t1_enc_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
-                                                                                                               orient, bpno, one, nmsedec, agg
-                                                                                                               && (j == k + runlen), vsc);
+                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                               t1_enc_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, bpno, one, nmsedec, agg
+                                                   && (j == k + runlen), vsc);
                        }
                }
        }
@@ -458,26 +412,16 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
                        if (k + 3 < h) {
                                if (cblksty & J2K_CCP_CBLKSTY_VSC) {
                                        agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
-                                                                       || t1_flags[1 + k + 1][1 +
-                                                                                                                                                                i] & (T1_SIG | T1_VISIT |
-                                                                                                                                                                                        T1_SIG_OTH)
-                                                                       || t1_flags[1 + k + 2][1 +
-                                                                                                                                                                i] & (T1_SIG | T1_VISIT |
-                                                                                                                                                                                        T1_SIG_OTH)
-                                                                       || (t1_flags[1 + k + 3][1 + i] &
-                                                                                       (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
-                                                                       & (T1_SIG | T1_VISIT | T1_SIG_OTH));
+                                               || t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT |
+                                               T1_SIG_OTH) || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT |
+                                               T1_SIG_OTH) || (t1_flags[1 + k + 3][1 + i] & (~(T1_SIG_S | T1_SIG_SE | 
+                                               T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
                                } else {
                                        agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
-                                                                       || t1_flags[1 + k + 1][1 +
-                                                                                                                                                                i] & (T1_SIG | T1_VISIT |
-                                                                                                                                                                                        T1_SIG_OTH)
-                                                                       || t1_flags[1 + k + 2][1 +
-                                                                                                                                                                i] & (T1_SIG | T1_VISIT |
-                                                                                                                                                                                        T1_SIG_OTH)
-                                                                       || t1_flags[1 + k + 3][1 +
-                                                                                                                                                                i] & (T1_SIG | T1_VISIT |
-                                                                                                                                                                                        T1_SIG_OTH));
+                                               || t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT |
+                                               T1_SIG_OTH) || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT |
+                                               T1_SIG_OTH) || t1_flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT |
+                                               T1_SIG_OTH));
                                }
                        } else {
                                agg = 0;
@@ -494,11 +438,9 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
                                runlen = 0;
                        }
                        for (j = k + runlen; j < k + 4 && j < h; j++) {
-                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
-                                                        && (j == k + 3 || j == h - 1)) ? 1 : 0;
-                               t1_dec_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
-                                                                                                               orient, oneplushalf, agg
-                                                                                                               && (j == k + runlen), vsc);
+                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
+                               t1_dec_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, oneplushalf, 
+                                                   agg && (j == k + runlen), vsc);
                        }
                }
        }
@@ -514,10 +456,9 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
                   fprintf(stderr, "warning: bad segmentation symbol %x\n",v);
                   } */
        }
-}                                                                                                                              /* VSC and  BYPASS by Antonin */
+}      /* VSC and  BYPASS by Antonin */
 
-double t1_getwmsedec(int nmsedec, int compno, int level, int orient,
-                                                                                int bpno, int qmfbid, double stepsize)
+double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize)
 {
        double w1, w2, wmsedec;
        if (qmfbid == 1) {
@@ -532,8 +473,7 @@ double t1_getwmsedec(int nmsedec, int compno, int level, int orient,
        return wmsedec;
 }
 
-void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
-                                                                               int qmfbid, double stepsize, int cblksty)
+void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty)
 {
        int i, j;
        int w, h;
@@ -549,9 +489,9 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
 
        max = 0;
        for (j = 0; j < h; j++) {
-               for (i = 0; i < w; i++) {
-                       max = int_max(max, int_abs(t1_data[j][i]));
-               }
+         for (i = 0; i < w; i++) {
+           max = int_max(max, int_abs(t1_data[j][i]));
+         }
        }
 
        cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
@@ -571,8 +511,7 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
                tcd_pass_t *pass = &cblk->passes[passno];
                int correction = 3;
                type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
-                                               && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
-                       T1_TYPE_MQ;
+                       && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
 
                switch (passtype) {
                case 0:
@@ -589,43 +528,55 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
                        break;
                }
 
-               cumwmsedec +=
-                       t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
-                                                                               stepsize);
+               cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize);
 
                /* Code switch "RESTART" (i.e. TERMALL) */
-               if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)
-                               && !((passtype == 2) && (bpno - 1 < 0))) {
-                       if (type == T1_TYPE_RAW)
-                               correction = mqc_bypass_flush_enc();
-                       else
-                               correction = mqc_restart_enc();
-
-                       pass->term = 1;
-               } else {
-                       if (((bpno < (cblk->numbps - 4) && (passtype > 0))
-                                        || ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
-                                       && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
-                               if (type == T1_TYPE_RAW)
-                                       correction = mqc_bypass_flush_enc();
-                               else
-                                       correction = mqc_restart_enc();
-
-                               pass->term = 1;
-                       } else {
-                               pass->term = 0;
-                       }
-               }
-
-               if (++passtype == 3) {
-                       passtype = 0;
-                       bpno--;
-               }
+               if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) 
+                 {
+                   if (type == T1_TYPE_RAW)
+                     {
+                       mqc_flush();
+                       correction=1;
+                       /* correction = mqc_bypass_flush_enc();*/
+                     }
+                   else
+                     {         /* correction = mqc_restart_enc(); */
+                       mqc_flush();
+                       correction = 1;
+                     }
+                   pass->term = 1;
+                 } else 
+                   {
+                     if (((bpno < (cblk->numbps - 4) && (passtype > 0)) || ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
+                         && (cblksty & J2K_CCP_CBLKSTY_LAZY)) 
+                       {
+                         if (type == T1_TYPE_RAW)
+                           {
+                             mqc_flush();
+                             correction=1;
+                             /* correction = mqc_bypass_flush_enc();*/
+                           }
+                         else
+                           {  /* correction = mqc_restart_enc();*/
+                             mqc_flush();
+                             correction=1;
+                           }
+                         pass->term = 1;
+                       } else 
+                         {
+                           pass->term = 0;
+                         }
+                   }
+               
+               if (++passtype == 3) 
+                 {
+                   passtype = 0;
+                   bpno--;
+                 }
 
                if (pass->term && bpno > 0) {
                        type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
-                                                       && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
-                               T1_TYPE_MQ;
+                               && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
                        if (type == T1_TYPE_RAW)
                                mqc_bypass_init_enc();
                        else
@@ -634,8 +585,7 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
 
                pass->distortiondec = cumwmsedec;
                pass->rate = mqc_numbytes() + correction;       /* FIXME */
-               pass->len =
-                       pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
+               pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
 
                /* Code-switch "RESET" */
                if (cblksty & J2K_CCP_CBLKSTY_RESET)
@@ -651,8 +601,7 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
        cblk->totalpasses = passno;
 }
 
-void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
-                                                                               int cblksty)
+void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift, int cblksty)
 {
        int i;
        int w, h;
@@ -682,8 +631,7 @@ void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
 
                /* add TONY */
                type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2)
-                                               && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
-                       T1_TYPE_MQ;
+                       && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
                if (type == T1_TYPE_RAW)
                        raw_init_dec(seg->data, seg->len);
                else
@@ -716,166 +664,156 @@ void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
 
 void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
 {
-       int compno, resno, bandno, precno, cblkno;
-       int x, y, i, j, orient;
-       tcd_tilecomp_t *tilec;
-       tcd_resolution_t *res;
-       tcd_band_t *band;
-       tcd_precinct_t *prc;
-       tcd_cblk_t *cblk;
-
-       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];
-                                       for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-                                               cblk = &prc->cblks[cblkno];
-
-                                               if (band->bandno == 0) {
-                                                       x = cblk->x0 - band->x0;
-                                                       y = cblk->y0 - band->y0;
-                                               } else if (band->bandno == 1) {
-                                                       tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
-                                                       y = cblk->y0 - band->y0;
-                                               } else if (band->bandno == 2) {
-                                                       tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
-                                                       x = cblk->x0 - band->x0;
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
-                                               } else {  /* if (band->bandno == 3) */
-                                                       tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
-                                               }
-
-                                               if (tcp->tccps[compno].qmfbid == 1) {
-
-                                                       for (j = 0; j < cblk->y1 - cblk->y0; j++) {
-                                                               for (i = 0; i < cblk->x1 - cblk->x0; i++) {
-                                                                       t1_data[j][i] =
-                                                                               tilec->data[(x + i) +
-                                                                                                                               (y + j) * (tilec->x1 -
-                                                                                                                                                                        tilec->
-                                                                                                                                                                        x0)] << T1_NMSEDEC_FRACBITS;
-                                                               }
-                                                       }
-                                               } else if (tcp->tccps[compno].qmfbid == 0) {
-                                                       for (j = 0; j < cblk->y1 - cblk->y0; j++) {
-                                                               for (i = 0; i < cblk->x1 - cblk->x0; i++) {
-                                                                       t1_data[j][i] =
-                                                                               fix_mul(tilec->
-                                                                                                               data[x + i +
-                                                                                                                                (y + j) * (tilec->x1 - tilec->x0)],
-                                                                                                               8192 * 8192 / band->stepsize) >> (13 -
-                                                                                                                                                                                                                                                       T1_NMSEDEC_FRACBITS);
-                                                               }
-                                                       }
-                                               }
-                                               orient = band->bandno;  /* FIXME */
-                                               if (orient == 2) {
-                                                       orient = 1;
-                                               } else if (orient == 1) {
-                                                       orient = 2;
-                                               }
-                                               t1_encode_cblk(cblk, orient, compno,
-                                                                                                        tilec->numresolutions - 1 - resno,
-                                                                                                        tcp->tccps[compno].qmfbid, band->stepsize,
-                                                                                                        tcp->tccps[compno].cblksty);
-                                       }                                                                                       /* cblkno */
-
-                               }                                                                                               /* precno */
-                       }                                                                                                       /* bandno */
-
-               }                                                                                                               /* resno */
-
-       }                                                                                                                       /* compo */
-
+  int compno, resno, bandno, precno, cblkno;
+  int x, y, i, j, orient;
+  tcd_tilecomp_t *tilec;
+  tcd_resolution_t *res;
+  tcd_band_t *band;
+  tcd_precinct_t *prc;
+  tcd_cblk_t *cblk;
+  
+  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];
+         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
+           cblk = &prc->cblks[cblkno];
+           
+           if (band->bandno == 0) {
+             x = cblk->x0 - band->x0;
+             y = cblk->y0 - band->y0;
+           } else if (band->bandno == 1) {
+             tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+             x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+             y = cblk->y0 - band->y0;
+           } else if (band->bandno == 2) {
+             tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+             x = cblk->x0 - band->x0;
+             y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+           } else {  /* if (band->bandno == 3) */
+             tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+             x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+             y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+           }
+           
+           if (tcp->tccps[compno].qmfbid == 1) {
+             
+             for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+               for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                 t1_data[j][i] = tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0)] << T1_NMSEDEC_FRACBITS;
+               }
+             }
+           } else if (tcp->tccps[compno].qmfbid == 0) {
+             for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+               for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                 t1_data[j][i] = fix_mul(tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)],
+                                         8192 * 8192 / band->stepsize) >> (13 - T1_NMSEDEC_FRACBITS);
+               }
+             }
+           }
+           orient = band->bandno;      /* FIXME */
+           if (orient == 2) {
+             orient = 1;
+           } else if (orient == 1) {
+             orient = 2;
+           }
+           t1_encode_cblk(cblk, orient, compno,
+                          tilec->numresolutions - 1 - resno,
+                          tcp->tccps[compno].qmfbid, band->stepsize,
+                          tcp->tccps[compno].cblksty);
+         }     /* cblkno */      
+       }       /* precno */
+      }                /* bandno */
+    }          /* resno  */ 
+  }            /* compo  */
 }
 
 
 void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
 {
-       int compno, resno, bandno, precno, cblkno;
-
-       for (compno = 0; compno < tile->numcomps; compno++) {
-               tcd_tilecomp_t *tilec = &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++) {
-                                               int x, y, i, j, orient;
-                                               tcd_cblk_t *cblk = &prc->cblks[cblkno];
-                                               orient = band->bandno;  /* FIXME */
-                                               if (orient == 2)
-                                                       orient = 1;
-                                               else if (orient == 1)
-                                                       orient = 2;
-                                               t1_decode_cblk(cblk, orient, tcp->tccps[compno].roishift,
-                                                                                                        tcp->tccps[compno].cblksty);
-                                               if (band->bandno == 0) {
-                                                       x = cblk->x0 - band->x0;
-                                                       y = cblk->y0 - band->y0;
-                                               } else if (band->bandno == 1) {
-                                                       tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
-                                                       y = cblk->y0 - band->y0;
-                                               } else if (band->bandno == 2) {
-                                                       tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
-                                                       x = cblk->x0 - band->x0;
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
-                                               } else {  /* if (band->bandno == 3) */
-                                                       tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
-                                                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
-                                                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
-                                               }
-                                               if (tcp->tccps[compno].roishift) {
-                                                       int thresh, val, mag;
-                                                       thresh = 1 << tcp->tccps[compno].roishift;
-                                                       for (j = 0; j < cblk->y1 - cblk->y0; j++) {
-                                                               for (i = 0; i < cblk->x1 - cblk->x0; i++) {
-                                                                       val = t1_data[j][i];
-                                                                       mag = int_abs(val);
-                                                                       if (mag >= thresh) {
-                                                                               mag >>= tcp->tccps[compno].roishift;
-                                                                               t1_data[j][i] = val < 0 ? -mag : mag;
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-
-                                               if (tcp->tccps[compno].qmfbid == 1) {
-                                                       for (j = 0; j < cblk->y1 - cblk->y0; j++) {
-                                                               for (i = 0; i < cblk->x1 - cblk->x0; i++) {
-                                                                       tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] =
-                                                                               t1_data[j][i];
-                                                               }
-                                                       }
-                                               } else if (tcp->tccps[compno].qmfbid == 0) {
-                                                       for (j = 0; j < cblk->y1 - cblk->y0; j++) {
-                                                               for (i = 0; i < cblk->x1 - cblk->x0; i++) {
-                                                                       if (t1_data[j][i] == 0) {
-                                                                               tilec->data[x + i +
-                                                                                                                               (y + j) * (tilec->x1 - tilec->x0)] = 0;
-                                                                       } else {
-                                                                               tilec->data[x + i +
-                                                                                                                               (y + j) * (tilec->x1 - tilec->x0)] =
-                                                                                       fix_mul(t1_data[j][i] << 13, band->stepsize);
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
+  int compno, resno, bandno, precno, cblkno;
+  
+  for (compno = 0; compno < tile->numcomps; compno++) 
+    {
+      tcd_tilecomp_t *tilec = &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++) 
+                   {
+                     int x, y, i, j, orient;
+                     tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                     orient = band->bandno;    /* FIXME */
+                     if (orient == 2)
+                       orient = 1;
+                     else if (orient == 1)
+                       orient = 2;
+                     t1_decode_cblk(cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
+                     if (band->bandno == 0) {
+                       x = cblk->x0 - band->x0;
+                       y = cblk->y0 - band->y0;
+                     } else if (band->bandno == 1) {
+                       tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                       y = cblk->y0 - band->y0;
+                     } else if (band->bandno == 2) {
+                       tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                       x = cblk->x0 - band->x0;
+                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                     } else {  /* if (band->bandno == 3) */
+                       tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+                       x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+                       y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+                     }
+                     
+                     if (tcp->tccps[compno].roishift) 
+                       {
+                         int thresh, val, mag;
+                         thresh = 1 << tcp->tccps[compno].roishift;
+                         for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                           for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                             val = t1_data[j][i];
+                             mag = int_abs(val);
+                             if (mag >= thresh) {
+                               mag >>= tcp->tccps[compno].roishift;
+                               t1_data[j][i] = val < 0 ? -mag : mag;
+                             }
+                           }
+                         }
+                       }
+                     
+                     if (tcp->tccps[compno].qmfbid == 1) {
+                       for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                         for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                           tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = t1_data[j][i];
+                         }
+                       }
+                     } else /* if (tcp->tccps[compno].qmfbid == 0)*/ {
+                       for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+                         for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+                           if (t1_data[j][i] == 0) {
+                             tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
+                           } else {
+                             tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = fix_mul(t1_data[j][i] << 13, band->stepsize);
+                           }
+                         }
                        }
+                     }
+                   }
                }
+           }
        }
+    }
 }
 
 int t1_init_ctxno_zc(int f, int orient)
@@ -884,10 +822,7 @@ int t1_init_ctxno_zc(int f, int orient)
        n = 0;
        h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
        v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
-       d =
-               ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) !=
-                                                                                                                                                                                                                        0) +
-               ((f & T1_SIG_SW) != 0);
+       d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
        switch (orient) {
        case 2:
                t = h;
@@ -950,20 +885,12 @@ int t1_init_ctxno_sc(int f)
 {
        int hc, vc, n;
        n = 0;
-       hc =
-               int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
-                                                T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
-                                               1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
-                                                                                                       (T1_SIG_E | T1_SGN_E)) +
-                                                                                                ((f & (T1_SIG_W | T1_SGN_W)) ==
-                                                                                                       (T1_SIG_W | T1_SGN_W)), 1);
-       vc =
-               int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
-                                                T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
-                                               1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
-                                                                                                       (T1_SIG_N | T1_SGN_N)) +
-                                                                                                ((f & (T1_SIG_S | T1_SGN_S)) ==
-                                                                                                       (T1_SIG_S | T1_SGN_S)), 1);
+       hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) == T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
+                       1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) == (T1_SIG_E | T1_SGN_E)) + ((f & (T1_SIG_W | T1_SGN_W)) ==
+                       (T1_SIG_W | T1_SGN_W)), 1);
+       vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) == T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
+                       1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) == (T1_SIG_N | T1_SGN_N)) + ((f & (T1_SIG_S | T1_SGN_S)) ==
+                       (T1_SIG_S | T1_SGN_S)), 1);
        if (hc < 0) {
                hc = -hc;
                vc = -vc;
@@ -999,20 +926,12 @@ int t1_init_ctxno_mag(int f)
 int t1_init_spb(int f)
 {
        int hc, vc, n;
-       hc =
-               int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
-                                                T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
-                                               1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
-                                                                                                       (T1_SIG_E | T1_SGN_E)) +
-                                                                                                ((f & (T1_SIG_W | T1_SGN_W)) ==
-                                                                                                       (T1_SIG_W | T1_SGN_W)), 1);
-       vc =
-               int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
-                                                T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
-                                               1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
-                                                                                                       (T1_SIG_N | T1_SGN_N)) +
-                                                                                                ((f & (T1_SIG_S | T1_SGN_S)) ==
-                                                                                                       (T1_SIG_S | T1_SGN_S)), 1);
+       hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) == T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
+                       1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) == (T1_SIG_E | T1_SGN_E)) + ((f & (T1_SIG_W | T1_SGN_W)) ==
+                       (T1_SIG_W | T1_SGN_W)), 1);
+       vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) == T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
+                       1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) == (T1_SIG_N | T1_SGN_N)) + ((f & (T1_SIG_S | T1_SGN_S)) ==
+                       (T1_SIG_S | T1_SGN_S)), 1);
        if (!hc && !vc)
                n = 0;
        else
@@ -1047,31 +966,19 @@ void t1_init_luts()
                t = i / pow(2, T1_NMSEDEC_FRACBITS);
                u = t;
                v = t - 1.5;
-               t1_lut_nmsedec_sig[i] =
-                       int_max(0,
-                                                       (int) (floor
-                                                                                ((u * u - v * v) * pow(2,
-                                                                                                                                                                               T1_NMSEDEC_FRACBITS) +
-                                                                                       0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
-               t1_lut_nmsedec_sig0[i] =
-                       int_max(0,
-                                                       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
-                                                                                pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+               t1_lut_nmsedec_sig[i] = int_max(0, (int) (floor((u * u - v * v) * pow(2,T1_NMSEDEC_FRACBITS) +
+                                               0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+               t1_lut_nmsedec_sig0[i] = int_max(0, (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
+                                               pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
                u = t - 1.0;
                if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
                        v = t - 1.5;
                } else {
                        v = t - 0.5;
                }
-               t1_lut_nmsedec_ref[i] =
-                       int_max(0,
-                                                       (int) (floor
-                                                                                ((u * u - v * v) * pow(2,
-                                                                                                                                                                               T1_NMSEDEC_FRACBITS) +
-                                                                                       0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
-               t1_lut_nmsedec_ref0[i] =
-                       int_max(0,
-                                                       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
-                                                                                pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+               t1_lut_nmsedec_ref[i] = int_max(0, (int) (floor((u * u - v * v) * pow(2,T1_NMSEDEC_FRACBITS) +
+                                               0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+               t1_lut_nmsedec_ref0[i] = int_max(0, (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
+                                                pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
        }
 }
index 9bd3a2b0a478af7d95719947dd382ae2cc813032..cb44c81c721c37e63ad4ff9aef296fe1aa9cf57e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2001-2002, David Janssens
- * Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2002-2003,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2002-2004, Yannick Verschueren
+ * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -92,9 +92,22 @@ int t2_getnumpasses()
        return 37 + bio_read(7);
 }
 
-int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
-                                                                                int resno, int precno, int layno, unsigned char *dest,
-                                                                                int len, info_image * info_IM, int tileno)
+/*
+ * Encode a packet of a tile to a destination buffer
+ *
+ * Tile    : the tile for which to write the packets
+ * tcp     : the tile coding parameters
+ * compno  : Identity of the packet --> component value
+ * resno   : Identity of the packet --> resolution level value
+ * precno  : Identity of the packet --> precinct value
+ * layno   : Identity of the packet --> quality layer value
+ * dest    : the destination buffer
+ * len     : the length of the destination buffer
+ * info_IM : structure to create an index file
+ * tileno  : number of the tile encoded
+*/
+int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno, int resno, int precno, int layno, unsigned char *dest,
+                    int len, info_image * info_IM, int tileno)
 {
        int bandno, cblkno;
        unsigned char *sop = 0, *eph = 0;
@@ -102,9 +115,6 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
        tcd_resolution_t *res = &tilec->resolutions[resno];
        unsigned char *c = dest;
 
-       /* int PPT=tile->PPT, ppt_len=0; */
-       /* FILE *PPT_file; */
-
        /* <SOP 0xff91> */
        if (tcp->csty & J2K_CP_CSTY_SOP) {
                sop = (unsigned char *) malloc(6 * sizeof(unsigned char));
@@ -135,7 +145,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
        }
 
        bio_init_enc(c, len);
-       bio_write(1, 1);                                                        /* Empty header bit */
+       bio_write(1, 1);  /* Empty header bit */
 
        /* Writing Packet header */
        for (bandno = 0; bandno < res->numbands; bandno++) {
@@ -178,14 +188,10 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
                                tcd_pass_t *pass = &cblk->passes[passno];
                                nump++;
                                len += pass->len;
-                               if (pass->term
-                                               || passno == (cblk->numpasses + layer->numpasses) - 1) {
-                                       increment =
-                                               int_max(increment,
-                                                                               int_floorlog2(len) + 1 - (cblk->numlenbits +
-                                                                                                                                                                                       int_floorlog2(nump)));
-                                       len = 0;
-                                       nump = 0;
+                               if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
+                                 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
+                                 len = 0;
+                                 nump = 0;
                                }
                        }
                        t2_putcommacode(increment);
@@ -198,18 +204,17 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
                                tcd_pass_t *pass = &cblk->passes[passno];
                                nump++;
                                len += pass->len;
-                               if (pass->term
-                                               || passno == (cblk->numpasses + layer->numpasses) - 1) {
-                                       bio_write(len, cblk->numlenbits + int_floorlog2(nump));
-                                       len = 0;
-                                       nump = 0;
+                               if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
+                                 bio_write(len, cblk->numlenbits + int_floorlog2(nump));
+                                 len = 0;
+                                 nump = 0;
                                }
                        }
                }
        }
 
        if (bio_flush())
-               return -999;                                                            /* modified to eliminate longjmp !! */
+               return -999;            /* modified to eliminate longjmp !! */
 
        c += bio_numbytes();
 
@@ -223,7 +228,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
                c += 2;
        }
        /* </EPH> */
-       /*   } */
+
        /* Writing the packet body */
 
        for (bandno = 0; bandno < res->numbands; bandno++) {
@@ -239,7 +244,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
                                        info_PK->disto = layer->disto;
                                        if (info_IM->D_max < info_PK->disto)
                                                info_IM->D_max = info_PK->disto;
-                               }                                                                                               /* </ADD> */
+                               }                       /* </ADD> */
                                continue;
                        }
                        if (c + layer->len > dest + len) {
@@ -256,7 +261,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
                                info_PK->disto = layer->disto;
                                if (info_IM->D_max < info_PK->disto)
                                        info_IM->D_max = info_PK->disto;
-                       }                                                                                                       /* </ADD> */
+                       }               /* </ADD> */
                }
        }
        return c - dest;
@@ -272,15 +277,25 @@ void t2_init_seg(tcd_seg_t * seg, int cblksty, int first)
                if (first)
                        seg->maxpasses = 10;
                else
-                       seg->maxpasses = (((seg - 1)->maxpasses == 1)
-                                                                                               || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
+                       seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
        } else
                seg->maxpasses = 109;
 }
 
-int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
-                                                                                j2k_tcp_t * tcp, int compno, int resno, int precno,
-                                                                                int layno)
+/*  
+ * Decode a packet of a tile from a source buffer
+ *
+ * src          : the source buffer
+ * len          : the length of the source buffer
+ * tile         : the tile for which to write the packets
+ * cp           : the image coding parameters
+ * tcp          : the tile coding parameters
+ * compno  : Identity of the packet --> component value
+ * resno      : Identity of the packet --> resolution level value
+ * precno    : Identity of the packet --> precinct value
+ * layno      : Identity of the packet --> quality layer value
+ */
+int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile, j2k_cp_t * cp, j2k_tcp_t * tcp, int compno, int resno, int precno, int layno)
 {
        int bandno, cblkno;
        tcd_tilecomp_t *tilec = &tile->comps[compno];
@@ -301,77 +316,151 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
                }
        }
 
-       if (tcp->csty & J2K_CP_CSTY_SOP) {
-               c += 6;
-       }
-       bio_init_dec(c, src + len - c);
+       /* When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
+          This part deal with this caracteristic
+          step 1: Read packet header in the saved structure
+          step 2: (futher) return to codestream for decoding */
+       if (cp->ppm == 1) /* PPM */
+         {         
+           c=cp->ppm_data;
+           bio_init_dec(c,1000);
+         } else 
+           {
+             if (tcp->ppt==1) /* PPT */
+               {
+                 c=tcp->ppt_data;
+                 bio_init_dec(c,1000);
+               } else /* Normal Case */
+                 {
+                   if (tcp->csty & J2K_CP_CSTY_SOP) 
+                     {
+                       if ((*c)!=255 || (*(c+1)!=145)) {printf("Error : expected SOP marker [1]!!!\n");}
+                       /*printf(" %d %d %d %d %d %d %d\n",*c,*(c+1),*(c+6),*(c+7),*(c+8),*(c+9),*(c+10));*/
+                       c += 6;
+                     }
+                   bio_init_dec(c, src + len - c);
+                 }
+           }
+
        present = bio_read(1);
-       if (!present) {
-               bio_inalign();
-               c += bio_numbytes();
-               return c - src;
-       }
+
+       if (!present) 
+         {
+           bio_inalign();
+           /* Normal case */
+           c += bio_numbytes();
+           if (tcp->csty & J2K_CP_CSTY_EPH) 
+             {
+               if ((*c)!=255 || (*(c+1)!=146)) {printf("Error : expected EPH marker [1]!!!\n");}
+               c += 2;
+             }
+           
+           /* PPT and PPM dealing */
+           if (cp->ppm == 1) /* PPM */
+             { 
+               cp->ppm_data=c;
+               return 0;
+             }
+           if (tcp->ppt==1) /* PPT */
+             {
+               tcp->ppt_data=c;
+               return 0;
+             }
+           return c - src;
+         }
        for (bandno = 0; bandno < res->numbands; bandno++) {
-               tcd_band_t *band = &res->bands[bandno];
-               tcd_precinct_t *prc = &band->precincts[precno];
-               for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-                       int included, increment, n;
-                       tcd_cblk_t *cblk = &prc->cblks[cblkno];
-                       tcd_seg_t *seg;
-                       /* if cblk not yet included before --> inclusion tagtree */
-                       if (!cblk->numsegs) {
-                               included = tgt_decode(prc->incltree, cblkno, layno + 1);
+         tcd_band_t *band = &res->bands[bandno];
+         tcd_precinct_t *prc = &band->precincts[precno];
+         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
+           int included, increment, n;
+           tcd_cblk_t *cblk = &prc->cblks[cblkno];
+           tcd_seg_t *seg;
+           /* if cblk not yet included before --> inclusion tagtree */
+           if (!cblk->numsegs) {
+             included = tgt_decode(prc->incltree, cblkno, layno + 1);
                                /* else one bit */
-                       } else {
-                               included = bio_read(1);
-                       }
-                       /* if cblk not included */
-                       if (!included) {
-                               cblk->numnewpasses = 0;
-                               continue;
-                       }
-                       /* if cblk not yet included --> zero-bitplane tagtree */
-                       if (!cblk->numsegs) {
-                               int i, numimsbs;
-                               for (i = 0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {
-                               }
-                               numimsbs = i - 1;
-                               cblk->numbps = band->numbps - numimsbs;
-                               cblk->numlenbits = 3;
-                       }
-                       /* number of coding passes */
-                       cblk->numnewpasses = t2_getnumpasses();
-                       increment = t2_getcommacode();
-                       /* length indicator increment */
-                       cblk->numlenbits += increment;
-                       if (!cblk->numsegs) {
-                               seg = &cblk->segs[0];
-                               t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
-                       } else {
-                               seg = &cblk->segs[cblk->numsegs - 1];
-                               if (seg->numpasses == seg->maxpasses) {
-                                       t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
-                               }
-                       }
-                       n = cblk->numnewpasses;
-
-                       do {
-                               seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
-                               seg->newlen =
-                                       bio_read(cblk->numlenbits + int_floorlog2(seg->numnewpasses));
-                               n -= seg->numnewpasses;
-                               if (n > 0) {
-                                       t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
-                               }
-                       } while (n > 0);
-               }
+           } else {
+             included = bio_read(1);
+           }
+           /* if cblk not included */
+           if (!included) {
+             cblk->numnewpasses = 0;
+             continue;
+           }
+           /* if cblk not yet included --> zero-bitplane tagtree */
+           if (!cblk->numsegs) {
+             int i, numimsbs;
+             for (i = 0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {
+             }
+             numimsbs = i - 1;
+             cblk->numbps = band->numbps - numimsbs;
+             cblk->numlenbits = 3;
+           }
+           /* number of coding passes */
+           cblk->numnewpasses = t2_getnumpasses();
+           increment = t2_getcommacode();
+           /* length indicator increment */
+           cblk->numlenbits += increment;
+           if (!cblk->numsegs) {
+             seg = &cblk->segs[0];
+             t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
+           } else {
+             seg = &cblk->segs[cblk->numsegs - 1];
+             if (seg->numpasses == seg->maxpasses) {
+               t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
+             }
+           }
+           n = cblk->numnewpasses;
+
+           do {
+             seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
+             seg->newlen = bio_read(cblk->numlenbits + int_floorlog2(seg->numnewpasses));
+             n -= seg->numnewpasses;
+             if (n > 0) {
+               t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
+             }
+           } while (n > 0);
+         }
        }
        if (bio_inalign())
-               return -999;
+         return -999;
+
        c += bio_numbytes();
-       if (tcp->csty & J2K_CP_CSTY_EPH) {
-               c += 2;
+
+       if (tcp->csty & J2K_CP_CSTY_EPH) { /* EPH marker */
+         if ((*c)!=255 || (*(c+1)!=146)) { printf("Error : expected EPH marker [2]!!!\n"); 
+         printf(" %d %d %d %d %d %d %d\n",*c,*(c+1),*(c+2),*(c+3),*(c+4),*(c+5),*(c+6));}
+         c += 2;
        }
+       
+       /* PPT Step 2 : see above for details */
+       if (cp->ppm==1)
+         {
+           cp->ppm_data=c; /* Update pointer */
+           
+           c=src;
+           if (tcp->csty & J2K_CP_CSTY_SOP) 
+             {
+               if ((*c)!=255 || (*(c+1)!=145)) {printf("Error : expected SOP marker [2] !!!\n"); }
+               c += 6;
+             }
+           bio_init_dec(c,src + len - c);
+         } else 
+           {
+             if (tcp->ppt==1)
+               { 
+                 tcp->ppt_data=c; /* Update pointer */
+                 c=src;
+                 if (tcp->csty & J2K_CP_CSTY_SOP) /* SOP marker */
+                   { 
+                     if ((*c)!=255 || (*(c+1)!=145)) {printf("Error : expected SOP marker [2] !!!\n"); }
+                     c += 6;
+                   }
+                 bio_init_dec(c,src + len - c);
+
+               }
+           }
+       
        for (bandno = 0; bandno < res->numbands; bandno++) {
                tcd_band_t *band = &res->bands[bandno];
                tcd_precinct_t *prc = &band->precincts[precno];
@@ -391,21 +480,22 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
                                }
                        }
                        do {
-                               if (c + seg->newlen > src + len)
-                                       return -999;
-                               memcpy(cblk->data + cblk->len, c, seg->newlen);
-                               if (seg->numpasses == 0) {
-                                       seg->data = cblk->data + cblk->len;
-                               }
-                               c += seg->newlen;
-                               cblk->len += seg->newlen;
-                               seg->len += seg->newlen;
-                               seg->numpasses += seg->numnewpasses;
-                               cblk->numnewpasses -= seg->numnewpasses;
-                               if (cblk->numnewpasses > 0) {
-                                       seg++;
-                                       cblk->numsegs++;
-                               }
+                         if (c + seg->newlen > src + len){
+                           return -999;}
+                         
+                         memcpy(cblk->data + cblk->len, c, seg->newlen);
+                         if (seg->numpasses == 0) {
+                           seg->data = cblk->data + cblk->len;
+                         }
+                         c += seg->newlen;
+                         cblk->len += seg->newlen;
+                         seg->len += seg->newlen;
+                         seg->numpasses += seg->numnewpasses;
+                         cblk->numnewpasses -= seg->numnewpasses;
+                         if (cblk->numnewpasses > 0) {
+                           seg++;
+                           cblk->numsegs++;
+                         }
                        } while (cblk->numnewpasses > 0);
                }
        }
@@ -413,9 +503,21 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
        return c - src;
 }
 
-int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
-                                                                                       tcd_tile_t * tile, int maxlayers,
-                                                                                       unsigned char *dest, int len, info_image * info_IM)
+
+
+/*
+ * Encode the packets of a tile to a destination buffer
+ *
+ * img        : the source image
+ * cp         : the image coding parameters
+ * tileno     : number of the tile encoded
+ * tile       : the tile for which to write the packets
+ * maxlayers  : maximum number of layers
+ * dest       : the destination buffer
+ * len        : the length of the destination buffer
+ * info_IM    : structure to create an index file
+ */
+int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno, tcd_tile_t * tile, int maxlayers, unsigned char *dest, int len, info_image * info_IM)
 {
        unsigned char *c = dest;
        int e = 0;
@@ -423,37 +525,36 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
        int pino, compno;
 
        pi = pi_create(img, cp, tileno);
-
+       
        for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
                while (pi_next(&pi[pino])) {
                        if (pi[pino].layno < maxlayers) {
-                               e =
-                                       t2_encode_packet(tile, &cp->tcps[tileno], pi[pino].compno,
-                                                                                                        pi[pino].resno, pi[pino].precno, pi[pino].layno,
-                                                                                                        c, dest + len - c, info_IM, tileno);
-                               if (e == -999) {
-                                       break;
-                               } else
-                                       c += e;
+                         e = t2_encode_packet(tile, &cp->tcps[tileno], pi[pino].compno, 
+                                              pi[pino].resno, pi[pino].precno, pi[pino].layno,
+                                              c, dest + len - c, info_IM, tileno);
+                         if (e == -999) {
+                           break;
+                         } else
+                           c += e;
                                /* INDEX >> */
-                               if (info_IM->index_write && info_IM->index_on) {
-                                       info_tile *info_TL = &info_IM->tile[tileno];
-                                       info_packet *info_PK = &info_TL->packet[info_IM->num];
-                                       if (!info_IM->num) {
-                                               info_PK->start_pos = info_TL->end_header + 1;
-                                       } else {
-                                               info_PK->start_pos =
-                                                       info_TL->packet[info_IM->num - 1].end_pos + 1;
-                                       }
-                                       info_PK->end_pos = info_PK->start_pos + e - 1;
-
-                               }
+                         if (info_IM->index_write && info_IM->index_on) {
+                           info_tile *info_TL = &info_IM->tile[tileno];
+                           info_packet *info_PK = &info_TL->packet[info_IM->num];
+                           if (!info_IM->num) {
+                             info_PK->start_pos = info_TL->end_header + 1;
+                           } else {
+                             info_PK->start_pos =
+                               info_TL->packet[info_IM->num - 1].end_pos + 1;
+                           }
+                           info_PK->end_pos = info_PK->start_pos + e - 1;
+                           
+                         }
                                /* << INDEX */
-                               if ((info_IM->index_write
-                                                && cp->tcps[tileno].csty & J2K_CP_CSTY_SOP)
-                                               || (info_IM->index_write && info_IM->index_on)) {
-                                       info_IM->num++;
-                               }
+                         if ((info_IM->index_write
+                              && cp->tcps[tileno].csty & J2K_CP_CSTY_SOP)
+                             || (info_IM->index_write && info_IM->index_on)) {
+                           info_IM->num++;
+                         }
                        }
 
                }
@@ -464,6 +565,7 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
                }
                free(pi[pino].comps);
        }
+       free(pi[0].include);
        free(pi);
        if (e == -999)
                return e;
@@ -471,35 +573,53 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
                return c - dest;
 }
 
-int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img,
-                                                                                       j2k_cp_t * cp, int tileno, tcd_tile_t * tile)
+
+
+/*
+ * Decode the packets of a tile from a source buffer
+ *
+ * src: the source buffer
+ * len: length of the source buffer
+ * img: destination image
+ * cp: image coding parameters
+ * tileno: number that identifies the tile for which to decode the packets
+ * tile: tile for which to decode the packets
+ */
+int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp, int tileno, tcd_tile_t * tile)
 {
        unsigned char *c = src;
        pi_iterator_t *pi;
        int pino, compno, e = 0;
+       int n=0;
 
        pi = pi_create(img, cp, tileno);
-
+       
        for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
-               while (pi_next(&pi[pino])) {
-                       e =
-                               t2_decode_packet(c, src + len - c, tile, &cp->tcps[tileno],
-                                                                                                pi[pino].compno, pi[pino].resno, pi[pino].precno,
-                                                                                                pi[pino].layno);
-                       if (e == -999) {                                        /* ADD */
-                               break;
-                       } else
-                               c += e;
-               }
-               /* FREE space memory taken by pi */
-               for (compno = 0; compno < pi[pino].numcomps; compno++) {
-                       free(pi[pino].comps[compno].resolutions);
-               }
-               free(pi[pino].comps);
+         while (pi_next(&pi[pino])) 
+           { 
+             //   fprintf(stderr,"codeblock %d [%d %d %d %d] pino %d/%d\n",n,pi[pino].layno,pi[pino].resno,pi[pino].compno,pi[pino].precno,pino,cp->tcps[tileno].numpocs);
+             e = t2_decode_packet(c, src + len - c, tile, cp, &cp->tcps[tileno], pi[pino].compno, 
+                                  pi[pino].resno, pi[pino].precno, pi[pino].layno); 
+             n++;
+        
+             if (e == -999) {          /* ADD */
+               break;
+             } else
+               c += e;
+             /*  printf("next\n"); */
+           }
+
+         /* FREE space memory taken by pi */
+         for (compno = 0; compno < pi[pino].numcomps; compno++) {
+           free(pi[pino].comps[compno].resolutions);
+         }
+         free(pi[pino].comps);
        }
+       free(pi[0].include);
        free(pi);
+       
        if (e == -999)
-               return e;
+         return e;
        else
-               return c - src;
+         return c - src;
 }
index 4d7757a657e618b2392e4542c4a591c85b4d7c42..79ccf993de93eb1887de80ff326de93bc60038ee 100644 (file)
 
 /*
  * Encode the packets of a tile to a destination buffer
- * img: the source image
- * cp: the image coding parameters
- * tile: the tile for which to write the packets
- * dest: the destination buffer
- * len: the length of the destination buffer
+ *
+ * img        : the source image
+ * cp         : the image coding parameters
+ * tileno     : number of the tile encoded
+ * tile       : the tile for which to write the packets
+ * maxlayers  : maximum number of layers
+ * dest       : the destination buffer
+ * len        : the length of the destination buffer
+ * info_IM    : structure to create an index file
  */
-int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
-                                                                                       tcd_tile_t * tile, int maxlayers,
-                                                                                       unsigned char *dest, int len, info_image * info_IM);
+int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno, tcd_tile_t * tile, int maxlayers, unsigned char *dest, int len, info_image * info_IM);
 
 /*
  * Decode the packets of a tile from a source buffer
+ *
  * src: the source buffer
  * len: length of the source buffer
  * img: destination image
@@ -53,7 +56,6 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
  * tileno: number that identifies the tile for which to decode the packets
  * tile: tile for which to decode the packets
  */
-int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img,
-                                                                                       j2k_cp_t * cp, int tileno, tcd_tile_t * tile);
+int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp, int tileno, tcd_tile_t * tile);
 
 #endif
index 8b53da0d425af5b8a0af27e1a3ec4204bb6eb8ba..84b3a17293229e8708cd770bb71f15368f4b6d75 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2001-2002, David Janssens
- * Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2002-2003,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * Copyright (c) 2002-2004, Yannick Verschueren
+ * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -129,12 +129,7 @@ void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                /* tile->PPT=img->PPT;  */
                /* Modification of the RATE >> */
                for (j = 0; j < tcp->numlayers; j++) {
-                       tcp->rates[j] =
-                               ceil(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));
+                       tcp->rates[j] = ceil(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));
                        if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
                                tcp->rates[j] = tcp->rates[j - 1] + 20;
                        } else {
@@ -144,8 +139,7 @@ void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                }
                /* << Modification of the RATE */
 
-               tile->comps =
-                       (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
+               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]; */
@@ -157,18 +151,10 @@ void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                        tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
                        tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
 
-                       /* Special DWT (always this case) */
-                       tilec->previous_row = 0;
-                       tilec->previous_col = 0;
-
-                       tilec->data =
-                               (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) *
-                                                                                        (tilec->y1 - tilec->y0));
+                       tilec->data = (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
                        tilec->numresolutions = tccp->numresolutions;
 
-                       tilec->resolutions =
-                               (tcd_resolution_t *) malloc(tilec->numresolutions *
-                                                                                                                                               sizeof(tcd_resolution_t));
+                       tilec->resolutions = (tcd_resolution_t *) malloc(tilec->numresolutions * sizeof(tcd_resolution_t));
 
                        for (resno = 0; resno < tilec->numresolutions; resno++) {
                                int pdx, pdy;
@@ -187,14 +173,6 @@ void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                                res->x1 = int_ceildivpow2(tilec->x1, levelno);
                                res->y1 = int_ceildivpow2(tilec->y1, levelno);
 
-                               /* Special DWT (always this case) */
-                               res->previous_x0 = 0;
-                               res->previous_y0 = 0;
-                               res->previous_x1 = 0;
-                               res->previous_y1 = 0;
-                               res->cas_row = 0;
-                               res->cas_col = 0;
-                               /* << DWT */
                                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) {
@@ -249,36 +227,20 @@ void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                                                band->y1 = int_ceildivpow2(tilec->y1, levelno);
                                        } else {
                                                /* band border (global) */
-                                               band->x0 =
-                                                       int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b,
-                                                                                                                       levelno + 1);
-                                               band->y0 =
-                                                       int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b,
-                                                                                                                       levelno + 1);
-                                               band->x1 =
-                                                       int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b,
-                                                                                                                       levelno + 1);
-                                               band->y1 =
-                                                       int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b,
-                                                                                                                       levelno + 1);
+                                               band->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);
+                                       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->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));
+                                       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;
@@ -301,28 +263,20 @@ void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                                                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;
+                                               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->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 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);
 
@@ -332,7 +286,6 @@ void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                                                        cblk->y0 = int_max(cblkystart, prc->y0);
                                                        cblk->x1 = int_min(cblkxend, prc->x1);
                                                        cblk->y1 = int_min(cblkyend, prc->y1);
-
                                                }
                                        }
                                }
@@ -366,14 +319,14 @@ void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                                                if (prc->imsbtree != NULL)
                                                        tgt_destroy(prc->imsbtree);
                                                free(prc->cblks);
-                                       }                                                                                       /* for (precno */
+                                       }               /* for (precno */
                                        free(band->precincts);
-                               }                                                                                               /* for (bandno */
-                       }                                                                                                       /* for (resno */
+                               }                       /* for (bandno */
+                       }                               /* for (resno */
                        free(tilec->resolutions);
-               }                                                                                                               /* for (compno */
+               }                                       /* for (compno */
                free(tile->comps);
-       }                                                                                                                       /* for (tileno */
+       }                                               /* for (tileno */
        free(tcd_image.tiles);
 }
 
@@ -384,7 +337,7 @@ void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
        for (tileno = 0; tileno < 1; tileno++) {
                j2k_tcp_t *tcp = &cp->tcps[curtileno];
                int j;
-               int previous_x0, previous_x1, previous_y0, previous_y1;
+               //              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;
@@ -395,34 +348,13 @@ void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                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);
-               /* Special DWT */
-               if (p) {
-                       previous_x0 = int_max(cp->tx0 + (p - 1) * cp->tdx, img->x0);
-                       previous_x1 = int_min(cp->tx0 + p * cp->tdx, img->x1);
-               } else {
-                       previous_x0 = 0;
-                       previous_x1 = 0;
-               }
-
-               if (q) {
-                       previous_y0 = int_max(cp->ty0 + (q - 1) * cp->tdy, img->y0);
-                       previous_y1 = int_min(cp->ty0 + q * cp->tdy, img->y1);
-               } else {
-                       previous_y0 = 0;
-                       previous_y1 = 0;
-               }
-               /* << DWT */
 
                tile->numcomps = img->numcomps;
                /* tile->PPT=img->PPT; */
                /* Modification of the RATE >> */
                for (j = 0; j < tcp->numlayers; j++) {
-                       tcp->rates[j] =
-                               ceil(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));
+                       tcp->rates[j] = ceil(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));
                        if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
                                tcp->rates[j] = tcp->rates[j - 1] + 20;
                        } else {
@@ -443,31 +375,7 @@ void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                        tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
                        tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
 
-                       /* special for DWT */
-                       if (p) {
-                               previous_x0 = int_ceildiv(previous_x0, img->comps[compno].dx);
-                               previous_x1 = int_ceildiv(previous_x1, img->comps[compno].dx);
-
-                               tilec->previous_row = 1;
-                       } else {
-                               previous_x0 = 0;
-                               previous_x1 = 0;
-                               tilec->previous_row = 0;
-                       }
-
-                       if (q) {
-                               previous_y0 = int_ceildiv(previous_y0, img->comps[compno].dx);
-                               previous_y1 = int_ceildiv(previous_y1, img->comps[compno].dx);
-                               tilec->previous_col = 1;
-                       } else {
-                               previous_y0 = 0;
-                               previous_y1 = 0;
-                               tilec->previous_col = 0;
-                       }
-                       /* << DWT */
-                       tilec->data =
-                               (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) *
-                                                                                        (tilec->y1 - tilec->y0));
+                       tilec->data = (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
                        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++) {
@@ -485,12 +393,6 @@ void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                                res->x1 = int_ceildivpow2(tilec->x1, levelno);
                                res->y1 = int_ceildivpow2(tilec->y1, levelno);
 
-                               /* special for DWT */
-                               res->previous_x0 = int_ceildivpow2(previous_x0, levelno);
-                               res->previous_y0 = int_ceildivpow2(previous_y0, levelno);
-                               res->previous_x1 = int_ceildivpow2(previous_x1, levelno);
-                               res->previous_y1 = int_ceildivpow2(previous_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) {
@@ -544,30 +446,16 @@ void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                                                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);
+                                               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);
+                                       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->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++) {
@@ -586,21 +474,15 @@ void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                                                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;
+                                               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));
+                                               prc->cblks = (tcd_cblk_t *) malloc(prc->cw * prc->ch * sizeof(tcd_cblk_t));
 
                                                if (prc->incltree != NULL)
                                                        tgt_destroy(prc->incltree);
@@ -611,10 +493,8 @@ void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
                                                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 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];
@@ -641,12 +521,11 @@ void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
        tcd_cp = cp;
        tcd_image.tw = cp->tw;
        tcd_image.th = cp->th;
-       tcd_image.tiles =
-               (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
+       tcd_image.tiles = (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
        for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
                j2k_tcp_t *tcp = &cp->tcps[tileno];
                tcd_tile_t *tile = &tcd_image.tiles[tileno];
-               int previous_x0, previous_x1, previous_y0, previous_y1;
+               //              int previous_x0, previous_x1, previous_y0, previous_y1;
                /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
                int p = tileno % cp->tw;                /* si numerotation matricielle .. */
                int q = tileno / cp->tw;                /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
@@ -656,27 +535,9 @@ void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
                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);
-               /* Special DWT */
-               if (p) {
-                       previous_x0 = int_max(cp->tx0 + (p - 1) * cp->tdx, img->x0);
-                       previous_x1 = int_min(cp->tx0 + p * cp->tdx, img->x1);
-               } else {
-                       previous_x0 = 0;
-                       previous_x1 = 0;
-               }
-
-               if (q) {
-                       previous_y0 = int_max(cp->ty0 + (q - 1) * cp->tdy, img->y0);
-                       previous_y1 = int_min(cp->ty0 + q * cp->tdy, img->y1);
-               } else {
-                       previous_y0 = 0;
-                       previous_y1 = 0;
-               }
-               /* << DWT */
 
                tile->numcomps = img->numcomps;
-               tile->comps =
-                       (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
+               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];
@@ -685,35 +546,10 @@ void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
                        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);
-                       /* special for DWT */
-                       if (p) {
-                               previous_x0 = int_ceildiv(previous_x0, img->comps[compno].dx);
-                               previous_x1 = int_ceildiv(previous_x1, img->comps[compno].dx);
-
-                               tilec->previous_row = 1;
-                       } else {
-                               previous_x0 = 0;
-                               previous_x1 = 0;
-                               tilec->previous_row = 0;
-                       }
 
-                       if (q) {
-                               previous_y0 = int_ceildiv(previous_y0, img->comps[compno].dx);
-                               previous_y1 = int_ceildiv(previous_y1, img->comps[compno].dx);
-                               tilec->previous_col = 1;
-                       } else {
-                               previous_y0 = 0;
-                               previous_y1 = 0;
-                               tilec->previous_col = 0;
-                       }
-                       /* << DWT */
-                       tilec->data =
-                               (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) *
-                                                                                        (tilec->y1 - tilec->y0));
+                       tilec->data = (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
                        tilec->numresolutions = tccp->numresolutions;
-                       tilec->resolutions =
-                               (tcd_resolution_t *) malloc(tilec->numresolutions *
-                                                                                                                                               sizeof(tcd_resolution_t));
+                       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;
@@ -728,15 +564,6 @@ void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
                                res->y0 = int_ceildivpow2(tilec->y0, levelno);
                                res->x1 = int_ceildivpow2(tilec->x1, levelno);
                                res->y1 = int_ceildivpow2(tilec->y1, levelno);
-                               /* special for DWT */
-                               res->previous_x0 = int_ceildivpow2(previous_x0, levelno);
-                               res->previous_y0 = int_ceildivpow2(previous_y0, levelno);
-                               res->previous_x1 = int_ceildivpow2(previous_x1, levelno);
-                               res->previous_y1 = int_ceildivpow2(previous_y1, levelno);
-                               if (!p)
-                                       res->cas_row = 0;
-                               if (!q)
-                                       res->cas_col = 0;
 
                                res->numbands = resno == 0 ? 1 : 3;
                                /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
@@ -791,42 +618,24 @@ void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
                                                band->y1 = int_ceildivpow2(tilec->y1, levelno);
                                        } else {
                                                /* band border (global) */
-                                               band->x0 =
-                                                       int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b,
-                                                                                                                       levelno + 1);
-                                               band->y0 =
-                                                       int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b,
-                                                                                                                       levelno + 1);
-                                               band->x1 =
-                                                       int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b,
-                                                                                                                       levelno + 1);
-                                               band->y1 =
-                                                       int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b,
-                                                                                                                       levelno + 1);
+                                               band->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);
+                                       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->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));
+                                       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 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];
@@ -836,29 +645,21 @@ void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
                                                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;
+                                               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->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 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];
@@ -876,119 +677,95 @@ void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
        /* tcd_dump(&tcd_image,0); */
 }
 
-void tcd_makelayer_fixed(int layno, int matrice_data[2][5][3], int final)
-{
-       int compno, resno, bandno, precno, cblkno;
-       int vector[3], matrice[2][5][3];
-       int sum_mat, i, j, k;
-       for (compno = 0; compno < tcd_tile->numcomps; compno++) {
-               tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
-               for (i = 0; i < 2; i++) {
-                       for (j = 0; j < 5; j++) {
-                               for (k = 0; k < 3; k++) {
-                                       matrice[i][j][k] =
-                                               (int) (matrice_data[i][j][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 i, 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 */
-                                               sum_mat = 0;
-
-                                               for (i = 0; i < layno + 1; i++) {
-                                                       int mat_old;
-                                                       if (i != tcd_tcp->numlayers) {  /* -1  must add to have a lossless last quality layer */
-                                                               /* different quality (layer) */
-                                                               vector[bandno] = matrice[i][resno][bandno];
-                                                               sum_mat += vector[bandno];
-                                                       } else {
-                                                               /* Full quality (layer) */
-                                                               vector[bandno] = tcd_img->comps[compno].prec - sum_mat;
-                                                       }
-
-                                                       mat_old = vector[bandno];
-                                                       if (imsb > 0) {
-                                                               if (imsb >= vector[bandno]) {
-                                                                       vector[bandno] = 0;
-                                                               } else {
-                                                                       vector[bandno] -= imsb;
-                                                               }
-                                                               imsb -= mat_old;
-                                                       }
-                                               }
-                                               if (layno == 0) {
-                                                       cblk->numpassesinlayers = 0;
-                                               }
-                                               n = cblk->numpassesinlayers;
-                                               if (cblk->numpassesinlayers == 0) {
-                                                       if (vector[bandno] != 0) {
-                                                               n = 3 * vector[bandno] - 2 + cblk->numpassesinlayers;
-                                                       } else {
-                                                               n = cblk->numpassesinlayers;
-                                                       }
-
-                                               } else {
-                                                       n = 3 * vector[bandno] + 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_makelayer_fixed(int layno, int final) {
+  int compno, resno, bandno, precno, cblkno;
+  int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
+  int matrice[10][10][3];
+  int i,j,k;
+
+  /*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;
+           }
+         }
        }
-
+      }
+    }
 }
 
-void tcd_rateallocate_fixed(unsigned char *dest, int len,
-                                                                                                               info_image * info_IM)
-{
-       int layno;
-       int matrice[2][5][3] = { {{16, 0, 0},
-                                                                                                               {16, 16, 16},
-                                                                                                               {16, 16, 16},
-                                                                                                               {0, 0, 0},
-                                                                                                               {0, 0, 0}},
-
-       {{1, 0, 0},
-        {1, 1, 1},
-        {1, 1, 1},
-        {1, 1, 1},
-        {0, 0, 0}}
-       };
+void tcd_rateallocate_fixed() {
+  int layno;
 
-       for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
-               tcd_makelayer_fixed(layno, matrice, 1);
-       }
+  for (layno=0; layno<tcd_tcp->numlayers; layno++) 
+    {
+      tcd_makelayer_fixed(layno, 1);
+    }
 }
 
 void tcd_makelayer(int layno, double thresh, int final)
@@ -1021,8 +798,7 @@ void tcd_makelayer(int layno, double thresh, int final)
                                                                dd = pass->distortiondec;
                                                        } else {
                                                                dr = pass->rate - cblk->passes[n - 1].rate;
-                                                               dd =
-                                                                       pass->distortiondec - cblk->passes[n - 1].distortiondec;
+                                                               dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
                                                        }
                                                        if (dr == 0) {
                                                                if (dd != 0)
@@ -1040,15 +816,11 @@ void tcd_makelayer(int layno, double thresh, int final)
                                                        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;
+                                                       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;
                                                }
 
                                                if (final)
@@ -1086,9 +858,7 @@ void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
                                                                dd = pass->distortiondec;
                                                        } else {
                                                                dr = pass->rate - cblk->passes[passno - 1].rate;
-                                                               dd =
-                                                                       pass->distortiondec - cblk->passes[passno -
-                                                                                                                                                                                                                1].distortiondec;
+                                                               dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
                                                        }
                                                        if (dr == 0) {
                                                                continue;
@@ -1100,13 +870,15 @@ void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
                                                        if (rdslope > max) {
                                                                max = rdslope;
                                                        }
-                                               }                                                                               /* passno */
-                                       }                                                                                       /* cbklno */
-                               }                                                                                               /* precno */
-                       }                                                                                                       /* bandno */
-               }                                                                                                               /* resno */
-       }                                                                                                                       /* compno */
-
+                                               } /* passno */
+                                       } /* cbklno */
+                               } /* precno */
+                       } /* bandno */
+               } /* resno */
+       } /* compno */
+       if (info_IM->index_on) { /* Threshold for Marcela Index */
+         info_IM->tile[tcd_tileno].thresh=(double*)malloc(tcd_tcp->numlayers*sizeof(double));
+       }
        for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
                volatile double lo = min;
                volatile double hi = max;
@@ -1122,9 +894,7 @@ void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
 
                        tcd_makelayer(layno, thresh, 0);
 
-                       l =
-                               t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile, layno + 1,
-                                                                                                       dest, maxlen, info_IM);
+                       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;
@@ -1140,16 +910,19 @@ void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
                if (!success) {
                        longjmp(j2k_error, 1);
                }
+               
+               if (info_IM->index_on) { /* Threshold for Marcela Index */
+                 info_IM->tile[tcd_tileno].thresh[layno]=goodthresh;
+               }
                tcd_makelayer(layno, goodthresh, 1);
        }
 }
 
-int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
-                                                                                               info_image * info_IM)
+int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len, info_image * info_IM)
 {
        int compno;
        int l;
-       clock_t time7;                                                          /* , time2, time3, time4, time5, time6, time1; */
+       clock_t time7;
        tcd_tile_t *tile;
        j2k_tcp_t *tcp = &tcd_cp->tcps[0];
        j2k_tccp_t *tccp = &tcp->tccps[0];
@@ -1176,7 +949,6 @@ int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
 /*---------------TILE-------------------*/
 
        time7 = clock();
-       /* time1=clock(); */
 
        for (compno = 0; compno < tile->numcomps; compno++) {
                FILE *src;
@@ -1186,9 +958,7 @@ int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
                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 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);
@@ -1204,110 +974,71 @@ int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
 
                /* 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);
+               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;
+                                       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;
+                                       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);
+                       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);
        }
 
-       /* time1=clock()-time1;   */
-       /* printf("tile encoding times:\n"); */
-       /* printf("img->tile: %d.%.3d s\n", time1/CLOCKS_PER_SEC, (time1%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
-
 /*----------------MCT-------------------*/
 
-       /* time2=clock(); */
        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));
+                       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));
+                       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));
                }
        }
-       /* time2=clock()-time2;    */
-       /* printf("mct:       %ld.%.3ld s\n", time2/CLOCKS_PER_SEC, (time2%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
-
 /*----------------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, tilec->y1 - tilec->y0,
-                                                                tilec, tilec->numresolutions - 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);
+                       dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
                }
        }
-       /* time3=clock()-time3; */
-       /* printf("dwt:       %ld.%.3ld s\n", time3/CLOCKS_PER_SEC, (time3%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
-
 /*------------------TIER1-----------------*/
 
-       /* time4=clock(); */
        t1_init_luts();
        t1_encode_cblks(tile, tcd_tcp);
-       /* time4=clock()-time4; */
-       /* printf("tier 1:    %ld.%.3ld s\n", time4/CLOCKS_PER_SEC, (time4%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
 
 /*-----------RATE-ALLOCATE------------------*/
-
        info_IM->index_write = 0;                       /* INDEX     */
-       /* time5=clock(); */
+
        if (tcd_cp->disto_alloc)
-               /* Normal Rate/distortion allocation */
-               tcd_rateallocate(dest, len, info_IM);
+         /* Normal Rate/distortion allocation */
+         tcd_rateallocate(dest, len, info_IM);
        else
-               /* Fixed layer allocation */
-               tcd_rateallocate_fixed(dest, len, info_IM);
-
-       /* time5=clock()-time5; */
-       /* printf("ratealloc: %ld.%.3ld s\n", time5/CLOCKS_PER_SEC, (time5%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
-
+         /* Fixed layer allocation */
+         tcd_rateallocate_fixed();
+       
 /*--------------TIER2------------------*/
-
        info_IM->index_write = 1;                       /* INDEX     */
-       /* time6=clock(); */
-       l =
-               t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers,
-                                                                                       dest, len, info_IM);
-       /* time6=clock()-time6; */
-       /* printf("tier 2:    %ld.%.3ld s\n", time6/CLOCKS_PER_SEC, (time6%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
-
+       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);
+       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++) {
@@ -1318,12 +1049,11 @@ int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
        return l;
 }
 
-int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
-                                                                                               info_image * info_IM)
+int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len, info_image * info_IM)
 {
        int compno;
        int l;
-       clock_t time7;                                                          /* , time2, time3, time4, time5, time6, time1; */
+       clock_t time7;
        tcd_tile_t *tile;
        j2k_tcp_t *tcp = &tcd_cp->tcps[0];
        j2k_tccp_t *tccp = &tcp->tccps[0];
@@ -1346,7 +1076,7 @@ int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
        /* << INDEX */
 /*---------------TILE-------------------*/
        time7 = clock();
-       /* time1=clock(); */
+
        for (compno = 0; compno < tile->numcomps; compno++) {
                FILE *src;
                char tmp[256];
@@ -1355,9 +1085,7 @@ int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
                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 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);
@@ -1397,75 +1125,54 @@ int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
                fclose(src);
        }
 
-       /* time1=clock()-time1;   */
-       /* printf("tile encoding times:\n"); */
-       /* printf("img->tile: %d.%.3d s\n", time1/CLOCKS_PER_SEC, (time1%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
 /*----------------MCT-------------------*/
-       /* time2=clock(); */
+
        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));
+                       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));
+                       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));
                }
        }
 
-       /* time2=clock()-time2;    */
-       /* printf("mct:       %d.%.3d s\n", time2/CLOCKS_PER_SEC, (time2%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
-
 /*----------------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, tilec->y1 - tilec->y0,
-                                                                tilec, tilec->numresolutions - 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);
+                       dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
                }
        }
-       /* time3=clock()-time3; */
-       /* printf("dwt:       %d.%.3d s\n", time3/CLOCKS_PER_SEC, (time3%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
 
 /*------------------TIER1-----------------*/
-       /* time4=clock(); */
+       
        t1_init_luts();
        t1_encode_cblks(tile, tcd_tcp);
-       /* time4=clock()-time4; */
-       /* printf("tier 1:    %d.%.3d s\n", time4/CLOCKS_PER_SEC, (time4%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
 
 /*-----------RATE-ALLOCATE------------------*/
        info_IM->index_write = 0;                       /* INDEX */
 
-       /* time5=clock(); */
-       tcd_rateallocate(dest, len, info_IM);
-       /* time5=clock()-time5; */
-       /* printf("ratealloc: %d.%.3d s\n", time5/CLOCKS_PER_SEC, (time5%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
+       info_IM->index_write=0; /* INDEX */
+       
+       if (tcd_cp->disto_alloc)
+         /* Normal Rate/distortion allocation */
+         tcd_rateallocate(dest, len, info_IM);
+       else
+         /* Fixed layer allocation */
+         tcd_rateallocate_fixed();
 
 /*--------------TIER2------------------*/
        info_IM->index_write = 1;                       /* INDEX */
 
-       /* time6=clock(); */
-       l =
-               t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers,
-                                                                                       dest, len, info_IM);
-       /* time6=clock()-time6; */
-       /* printf("tier 2:    %d.%.3d s\n", time6/CLOCKS_PER_SEC, (time6%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
+       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);
+       printf("total:     %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC, (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
 
        for (compno = 0; compno < tile->numcomps; compno++) {
                tilec = &tile->comps[compno];
@@ -1482,8 +1189,8 @@ int tcd_decode_tile(unsigned char *src, int len, int tileno)
        int compno;
        int eof = 0;
        clock_t time1, time2, time3, time4, time5, time6;
-
        tcd_tile_t *tile;
+
        tcd_tileno = tileno;
        tcd_tile = &tcd_image.tiles[tileno];
        tcd_tcp = &tcd_cp->tcps[tileno];
@@ -1492,15 +1199,16 @@ int tcd_decode_tile(unsigned char *src, int len, int tileno)
        time6 = clock();
 
        time1 = clock();
+       printf("tile decoding time %d/%d:\n", tileno + 1, tcd_cp->tw * tcd_cp->th);
 
        l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
+
        if (l == -999) {
                eof = 1;
                fprintf(stderr, "tcd_decode: incomplete bistream\n");
        }
        time1 = clock() - time1;
-       printf("tile decoding time %d/%d:\n", tileno + 1,
-                                tcd_cp->tw * tcd_cp->th);
+       
        /* printf("tier 2:    %ld.%.3ld s\n", time1/CLOCKS_PER_SEC, (time1%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC);    */
 
        time2 = clock();
@@ -1510,79 +1218,29 @@ int tcd_decode_tile(unsigned char *src, int len, int tileno)
        /* printf("tier 1:    %ld.%.3ld s\n", time2/CLOCKS_PER_SEC, (time2%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
 
        time3 = clock();
-       for (compno = 0; compno < tile->numcomps; compno++) {
-               tcd_tilecomp_t *tilec = &tile->comps[compno];
-               if (tcd_tcp->tccps[compno].qmfbid == 1) {
-                       if (tileno % tcd_cp->tw) {
-                               tcd_tile_t *tile_row = &tcd_image.tiles[tileno - 1];
-                               tcd_tilecomp_t *tilec_row = &tile_row->comps[compno];
-                               if (tileno / tcd_cp->tw) {
-                                       tcd_tile_t *tile_col = &tcd_image.tiles[tileno - 1];
-                                       tcd_tilecomp_t *tilec_col = &tile_col->comps[compno];
-                                       dwt_decode(tilec->data, tilec->x1 - tilec->x0,
-                                                                                tilec->y1 - tilec->y0, tilec,
-                                                                                tilec->numresolutions - 1, tilec_row, tilec_col);
-                               } else
-                                       dwt_decode(tilec->data, tilec->x1 - tilec->x0,
-                                                                                tilec->y1 - tilec->y0, tilec,
-                                                                                tilec->numresolutions - 1, tilec_row, tilec);
-                       } else {
-                               if (tileno / tcd_cp->tw) {
-                                       tcd_tile_t *tile_col = &tcd_image.tiles[tileno - 1];
-                                       tcd_tilecomp_t *tilec_col = &tile_col->comps[compno];
-                                       dwt_decode(tilec->data, tilec->x1 - tilec->x0,
-                                                                                tilec->y1 - tilec->y0, tilec,
-                                                                                tilec->numresolutions - 1, tilec, tilec_col);
-                               } else
-                                       dwt_decode(tilec->data, tilec->x1 - tilec->x0,
-                                                                                tilec->y1 - tilec->y0, tilec,
-                                                                                tilec->numresolutions - 1, tilec, tilec);
-                       }
-               } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
-                       if (tileno % tcd_cp->tw) {
-                               tcd_tile_t *tile_row = &tcd_image.tiles[tileno - 1];
-                               tcd_tilecomp_t *tilec_row = &tile_row->comps[compno];
-                               if (tileno / tcd_cp->tw) {
-                                       tcd_tile_t *tile_col = &tcd_image.tiles[tileno - 1];
-                                       tcd_tilecomp_t *tilec_col = &tile_col->comps[compno];
-                                       dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
-                                                                                                       tilec->y1 - tilec->y0, tilec,
-                                                                                                       tilec->numresolutions - 1, tilec_row, tilec_col);
-                               } else
-                                       dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
-                                                                                                       tilec->y1 - tilec->y0, tilec,
-                                                                                                       tilec->numresolutions - 1, tilec_row, tilec);
-                       } else {
-                               if (tileno / tcd_cp->tw) {
-                                       tcd_tile_t *tile_col = &tcd_image.tiles[tileno - 1];
-                                       tcd_tilecomp_t *tilec_col = &tile_col->comps[compno];
-                                       dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
-                                                                                                       tilec->y1 - tilec->y0, tilec,
-                                                                                                       tilec->numresolutions - 1, tilec, tilec_col);
-                               } else
-                                       dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
-                                                                                                       tilec->y1 - tilec->y0, tilec,
-                                                                                                       tilec->numresolutions - 1, tilec, tilec);
-                       }
-               }
-       }
+       for (compno = 0; compno < tile->numcomps; compno++) 
+         {
+           tcd_tilecomp_t *tilec = &tile->comps[compno];
+           if (tcd_tcp->tccps[compno].qmfbid == 1) 
+             {
+               dwt_decode(tilec->data, tilec->x1 - tilec->x0,tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
+             }  else 
+               { /*if (tcd_tcp->tccps[compno].qmfbid == 0) {*/
+                 dwt_decode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
+               } 
+         }
+
        time3 = clock() - time3;
        /* printf("dwt:       %ld.%.3ld s\n", time3/CLOCKS_PER_SEC, (time3%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
 
        time4 = clock();
        if (tcd_tcp->mct) {
                if (tcd_tcp->tccps[0].qmfbid == 0) {
-                       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));
+                       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(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));
+                       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));
                }
        }
        time4 = clock() - time4;
@@ -1591,19 +1249,11 @@ int tcd_decode_tile(unsigned char *src, int len, int tileno)
        time5 = clock();
        for (compno = 0; compno < tile->numcomps; compno++) {
                tcd_tilecomp_t *tilec = &tile->comps[compno];
-               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 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 =
-                       int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
+               int w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
                int i, j;
                int offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
                int offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
@@ -1617,6 +1267,7 @@ int tcd_decode_tile(unsigned char *src, int len, int tileno)
                                        v = tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] >> 13;
                                }
                                v += adjust;
+                               
                                /* tcd_img->comps[compno].data[i+j*w]=int_clamp(v, min, max); */
                                tcd_img->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);      /* change ! */
                        }
@@ -1626,8 +1277,7 @@ int tcd_decode_tile(unsigned char *src, int len, int tileno)
        /* printf("tile->img: %ld.%.3ld s\n", time5/CLOCKS_PER_SEC, (time5%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
 
        time6 = clock() - time6;
-       printf("total:     %ld.%.3ld s\n\n", time6 / CLOCKS_PER_SEC,
-                                (time6 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
+       printf("total:     %ld.%.3ld s\n\n", time6 / CLOCKS_PER_SEC, (time6 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
 
        if (eof) {
                longjmp(j2k_error, 1);
index e8e877270ea8576546c48f1743a6be17241fb5d1..9401e968b889dbaf5739be4a5f742716b1b3e202 100644 (file)
 #include "tgt.h"
 
 typedef struct {
-       int numpasses;
-       int len;
-       unsigned char *data;
-       int maxpasses;
-       int numnewpasses;
-       int newlen;
+  int numpasses;
+  int len;
+  unsigned char *data;
+  int maxpasses;
+  int numnewpasses;
+  int newlen;
 } tcd_seg_t;
 
 typedef struct {
-       int rate;
-       double distortiondec;
-       int term, len;
+  int rate;                         
+  double distortiondec;             
+  int term, len;
 } tcd_pass_t;
 
 typedef struct {
-       int numpasses;
-       int len;
-       double disto;                                                                   /* add for index (Cfr. Marcela) */
-       unsigned char *data;
+  int numpasses;                    /* Number of passes in the layer */
+  int len;                          /* len of information */
+  double disto;                            /* add for index (Cfr. Marcela) */
+  unsigned char *data;              /* data */
 } tcd_layer_t;
 
 typedef struct {
-       int x0, y0, x1, y1;
-       int numbps;
-       int numlenbits;
-       int len;
-       int numpasses;
-       int numnewpasses;
-       int numsegs;
-       tcd_seg_t segs[100];
-       unsigned char data[8192];
-       int numpassesinlayers;
-       tcd_layer_t layers[100];
-       int totalpasses;
-       tcd_pass_t passes[100];
+  int x0, y0, x1, y1;               /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
+  int numbps;
+  int numlenbits;
+  int len;                          /* length */
+  int numpasses;                    /* number of pass already done for the code-blocks */
+  int numnewpasses;                 /* number of pass added to the code-blocks */
+  int numsegs;                      /* number of segments */
+  tcd_seg_t segs[100];              /* segments informations */
+  unsigned char data[8192];         /* Data */
+  int numpassesinlayers;            /* number of passes in the layer */
+  tcd_layer_t layers[100];          /* layer information */
+  int totalpasses;                  /* total number of passes */
+  tcd_pass_t passes[100];           /* information about the passes */
 } tcd_cblk_t;
 
 typedef struct {
-       int x0, y0, x1, y1;
-       int cw, ch;
-       tcd_cblk_t *cblks;
-       tgt_tree_t *incltree;
-       tgt_tree_t *imsbtree;
+  int x0, y0, x1, y1;               /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
+  int cw, ch;                       /* number of precinct in width and heigth */
+  tcd_cblk_t *cblks;                /* code-blocks informations */
+  tgt_tree_t *incltree;             /* inclusion tree */
+  tgt_tree_t *imsbtree;             /* IMSB tree */
 } tcd_precinct_t;
 
 typedef struct {
-       int x0, y0, x1, y1;
-       int bandno;
-       tcd_precinct_t *precincts;
-       int numbps;
-       int stepsize;
+  int x0, y0, x1, y1;               /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
+  int bandno;
+  tcd_precinct_t *precincts;        /* precinct information */
+  int numbps;
+  int stepsize;
 } tcd_band_t;
 
 typedef struct {
-       int x0, y0, x1, y1;
-       int previous_x0, previous_y0, previous_x1, previous_y1; /* usefull for the DWT */
-       int cas_col, cas_row;                                   /* usefull for the DWT */
-       int pw, ph;                                                                             /* , old_pw,old_ph, old_pw_max,old_ph_max; */
-       int numbands;
-       tcd_band_t bands[3];
+  int x0, y0, x1, y1;               /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
+  int pw, ph;                      
+  int numbands;                     /* number sub-band for the resolution level */
+  tcd_band_t bands[3];              /* subband information */
 } tcd_resolution_t;
 
 typedef struct {
-       int x0, y0, x1, y1;
-       int previous_row, previous_col; /* usefull for the DWT */
-       int numresolutions;
-       tcd_resolution_t *resolutions;
-       int *data;
+  int x0, y0, x1, y1;               /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
+  int numresolutions;               /* number of resolutions level */
+  tcd_resolution_t *resolutions;    /* resolutions information */
+  int *data;                        /* data of the component */
 } tcd_tilecomp_t;
 
 typedef struct {
-       int x0, y0, x1, y1;
-       int numcomps;
-       /* int PPT; */
-       /* int len_ppt; */
-       tcd_tilecomp_t *comps;
+  int x0, y0, x1, y1;               /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
+  int numcomps;                     /* number of components in tile */
+  tcd_tilecomp_t *comps;            /* Components information */
 } tcd_tile_t;
 
 typedef struct {
-       int tw, th;
-       tcd_tile_t *tiles;
+  int tw, th;                       /* number of tiles in width and heigth */
+  tcd_tile_t *tiles;                /* Tiles information */
 } tcd_image_t;
 
 /*
- * Initialize the tile coder/decoder
+ * Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)
  * img: raw image
  * cp: coding parameters
- * info_IM: creation of index file
+ * curtileno : number that identifies the tile that will be encoded
  */
 void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
 
+
+/*
+ * Initialize the tile coder (allocate the memory)
+ * img: raw image
+ * cp: coding parameters
+ * curtileno : number that identifies the tile that will be encoded
+ */
 void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
 
+
+/*
+ * Initialize the tile decoder
+ * img: raw image
+ * cp: coding parameters
+ */
 void tcd_init(j2k_image_t * img, j2k_cp_t * cp);
 
+
+/*
+ * Free the memory allocated for encoding
+ * img: raw image
+ * cp: coding parameters
+ * curtileno : number that identifies the tile that will be encoded
+ */
 void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
 
 /*
@@ -135,18 +150,19 @@ void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
  * tileno: number that identifies one of the tiles to be encoded
  * dest: destination buffer
  * len: length of destination buffer
+ * info_IM: creation of index file
  */
-int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
-                                                                                               info_image * info_IM);
+int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len, info_image * info_IM);
+
 
 /*
  * Encode a tile from the raw image into a buffer, format pgx
  * tileno: number that identifies one of the tiles to be encoded
  * dest: destination buffer
  * len: length of destination buffer
+ * info_IM: creation of index file
  */
-int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
-                                                                                               info_image * info_IM);
+int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len, info_image * info_IM);
 
 /*
  * Decode a tile from a buffer into a raw image
index 3ace397aec112b8f718c9be8db122bca3003a3f1..230b0e25fed7e7c1a083f59813da2b658cb748e7 100644 (file)
@@ -41,6 +41,14 @@ typedef struct {
        tgt_node_t *nodes;
 } tgt_tree_t;
 
+
+
+/*
+ * Reset a tag-tree (set all leaves to 0)
+ * tree: tag-tree to reset
+ */
+void tgt_reset(tgt_tree_t * tree);
+
 /*
  * Create a tag-tree
  * numleafsh: width of the array of leafs of the tree
@@ -48,12 +56,6 @@ typedef struct {
  */
 tgt_tree_t *tgt_create(int numleafsh, int numleafsv);
 
-/*
- * Reset a tag-tree (set all leafs to 0)
- * tree: tag-tree to reset
- */
-void tgt_reset(tgt_tree_t * tree);
-
 /*
  * Destroy a tag-tree, liberating memory
  * tree: tag-tree to destroy