Bug fixed in lossless mode
authorAntonin Descampe <antonin@gmail.com>
Tue, 3 Aug 2004 14:06:10 +0000 (14:06 +0000)
committerAntonin Descampe <antonin@gmail.com>
Tue, 3 Aug 2004 14:06:10 +0000 (14:06 +0000)
codec/image_to_j2k.c
libopenjpeg/j2k.c
libopenjpeg/tcd.c

index db4302d6ee8c838f634c56644bf17127a2f3c227..bab57f7f15ce44988545fd0061995eb8525d533f 100644 (file)
 #endif
 #include "convert.h"
 
-void help_display()
+void
+help_display ()
 {
-  printf("HELP\n----\n\n");
-  printf("- the option -help displays the readme.txt file on screen\n\n");
+  printf ("HELP\n----\n\n");
+  printf ("- the option -help displays the readme.txt file on screen\n\n");
 
 
-  printf("List of parameters for the coder JPEG 2000 :\n");
-  printf("\n");
+  printf ("List of parameters for the coder JPEG 2000 :\n");
+  printf ("\n");
   printf
     ("- The markers COD and QCD are writed both of two in the main_header and never appear in the tile_header.  The markers in the main header are : SOC SIZ COD QCD COM.\n");
-  printf("\n");
+  printf ("\n");
   printf
     ("- This coder can encode mega image, a test was made on a 24000x24000 pixels color image.  You need enough disk space memory (twice the original) to encode the image. (i.e. for a 1.5 Gb image you need a minimum of 3Gb of disk memory)\n");
-  printf("\n");
-  printf("REMARKS :\n");
-  printf("---------\n");
-  printf("\n");
+  printf ("\n");
+  printf ("REMARKS :\n");
+  printf ("---------\n");
+  printf ("\n");
   printf
     ("* the value of rate enter in the code line is the compression factor !\n");
-  printf("exemple :\n");
-  printf("\n");
+  printf ("exemple :\n");
+  printf ("\n");
   printf
     ("-r 20,10,1 means quality 1 : compress 20x, quality 2 : compress 10x and quality 3 : compress 1x = lossless\n");
-  printf("\n");
-  printf("By default :\n");
-  printf("------------\n");
-  printf("\n");
-  printf(" * lossless\n");
-  printf(" * 1 tile\n");
-  printf(" * size of precinct 2^15 x 2^15 (means 1 precinct)\n");
-  printf(" * size of code-block 64 x 64\n");
-  printf(" * Number of resolution : 6\n");
-  printf(" * No SOP marker in the codestream\n");
-  printf(" * No EPH marker in the codestream\n");
-  printf(" * No sub-sampling in x and y direction\n");
-  printf(" * No mode switch activated\n");
-  printf(" * progression order : LRCP\n");
-  printf(" * No index file\n");
-  printf(" * No ROI upshifted\n");
-  printf(" * No offset of the origin of the image\n");
-  printf(" * No offset of the origin of the tiles\n");
-  printf(" * Reversible DWT 5-3\n");
-  printf("\n");
-  printf("Parameters :\n");
-  printf("------------\n");
-  printf("\n");
+  printf ("\n");
+  printf ("By default :\n");
+  printf ("------------\n");
+  printf ("\n");
+  printf (" * lossless\n");
+  printf (" * 1 tile\n");
+  printf (" * size of precinct 2^15 x 2^15 (means 1 precinct)\n");
+  printf (" * size of code-block 64 x 64\n");
+  printf (" * Number of resolution : 6\n");
+  printf (" * No SOP marker in the codestream\n");
+  printf (" * No EPH marker in the codestream\n");
+  printf (" * No sub-sampling in x and y direction\n");
+  printf (" * No mode switch activated\n");
+  printf (" * progression order : LRCP\n");
+  printf (" * No index file\n");
+  printf (" * No ROI upshifted\n");
+  printf (" * No offset of the origin of the image\n");
+  printf (" * No offset of the origin of the tiles\n");
+  printf (" * Reversible DWT 5-3\n");
+  printf ("\n");
+  printf ("Parameters :\n");
+  printf ("------------\n");
+  printf ("\n");
   printf
     ("-i             : source file  (-i source.pnm also *.pgm, *.ppm) required\n");
-  printf("\n");
-  printf("-o             : destination file (-o dest.j2k or .jp2) required\n");
-  printf("\n");
-  printf("-help          : Display the help information optional\n ");
-  printf("\n");
-  printf("-r             : different rates (-r 20,10,5) optional\n ");
-  printf("\n");
-  printf("-n             : Number of resolution (-n 3) optional\n");
-  printf("\n");
-  printf("-b             : size of code block (-b 32,32) optional\n");
-  printf("\n");
-  printf("-c             : size of precinct (-c 128,128) optional\n");
-  printf("\n");
-  printf("-t             : size of tile (-t 512,512) optional\n");
-  printf("\n");
+  printf ("\n");
   printf
-    ("-p             : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] optional\n");
-  printf("\n");
-  printf
-    ("-s             : subsampling factor (-s 2,2) [-s X,Y] optional\n");
-  printf("\n");
+    ("-o             : destination file (-o dest.j2k or .jp2) required\n");
+  printf ("\n");
+  printf ("-help          : Display the help information optional\n ");
+  printf ("\n");
+  printf ("-r             : different rates (-r 20,10,5) optional\n ");
+  printf ("\n");
+  printf ("-n             : Number of resolution (-n 3) optional\n");
+  printf ("\n");
+  printf ("-b             : size of code block (-b 32,32) optional\n");
+  printf ("\n");
+  printf ("-c             : size of precinct (-c 128,128) optional\n");
+  printf ("\n");
+  printf ("-t             : size of tile (-t 512,512) optional\n");
+  printf ("\n");
   printf
-    ("-SOP           : write SOP marker before each packet optional\n");
-  printf("\n");
+    ("-p             : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] optional\n");
+  printf ("\n");
+  printf ("-s             : subsampling factor (-s 2,2) [-s X,Y] optional\n");
+  printf ("\n");
+  printf ("-SOP           : write SOP marker before each packet optional\n");
+  printf ("\n");
   printf
     ("-EPH           : write EPH marker after each header packet optional\n");
-  printf("\n");
+  printf ("\n");
   printf
     ("-M             : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL) 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] optional\n");
   printf
     ("                    for several mode switch you have to add the value of each mode you want\n");
   printf
     ("                    ex : RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
-  printf("\n");
+  printf ("\n");
   printf
     ("-x             : Create an index file *.Idx (-x index_name.Idx) optional\n");
-  printf("\n");
+  printf ("\n");
   printf
     ("-ROI:c=%%d,U=%%d : quantization indices upshifted for component c=%%d [%%d = 0,1,2]\n");
   printf
     ("                 with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) optional\n");
-  printf("\n");
+  printf ("\n");
   printf
     ("-d             : offset of the origin of the image (-d 150,300) optional\n");
-  printf("\n");
+  printf ("\n");
   printf
     ("-T             : offset of the origin of the tiles (-T 100,75) optional\n");
-  printf("\n");
-  printf("-I             : Use the irreversible DWT 9-7 (-I) optional\n");
-  printf("\n");
-  printf("IMPORTANT :\n");
-  printf("-----------\n");
-  printf("\n");
-  printf("* subsampling bigger than 2 can produce error\n");
-  printf("\n");
-  printf("The index file respect the structure below :\n");
-  printf("---------------------------------------------\n");
-  printf("\n");
-  printf("Image_height Image_width\n");
-  printf("progression order\n");
-  printf("Tiles_size_X Tiles_size_Y\n");
-  printf("Components_nb\n");
-  printf("Layers_nb\n");
-  printf("decomposition_levels\n");
-  printf("Precincts_size_X Precincts_size_Y\n");
-  printf("Main_header_end_position\n");
-  printf("Codestream_size\n");
-  printf("Tile0 start_pos end_Theader end_pos\n");
-  printf("Tile1  ''           ''        ''\n");
-  printf("...\n");
-  printf("TileN  ''           ''        ''\n");
-  printf("Tpacket_0 Tile layer res. comp. prec. start_pos end_pos\n");
-  printf("...\n");
-  printf("Tpacket_M  ''    ''   ''   ''    ''       ''       ''\n");
+  printf ("\n");
+  printf ("-I             : Use the irreversible DWT 9-7 (-I) optional\n");
+  printf ("\n");
+  printf ("IMPORTANT :\n");
+  printf ("-----------\n");
+  printf ("\n");
+  printf ("* subsampling bigger than 2 can produce error\n");
+  printf ("\n");
+  printf ("The index file respect the structure below :\n");
+  printf ("---------------------------------------------\n");
+  printf ("\n");
+  printf ("Image_height Image_width\n");
+  printf ("progression order\n");
+  printf ("Tiles_size_X Tiles_size_Y\n");
+  printf ("Components_nb\n");
+  printf ("Layers_nb\n");
+  printf ("decomposition_levels\n");
+  printf ("Precincts_size_X Precincts_size_Y\n");
+  printf ("Main_header_end_position\n");
+  printf ("Codestream_size\n");
+  printf ("Tile0 start_pos end_Theader end_pos\n");
+  printf ("Tile1  ''           ''        ''\n");
+  printf ("...\n");
+  printf ("TileN  ''           ''        ''\n");
+  printf ("Tpacket_0 Tile layer res. comp. prec. start_pos end_pos\n");
+  printf ("...\n");
+  printf ("Tpacket_M  ''    ''   ''   ''    ''       ''       ''\n");
 }
 
-int give_progression(char progression[4])
+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;
-         }
+      && 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] = {
@@ -199,52 +215,61 @@ double dwt_norms_97[4][10] = {
   {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
 };
 
-int floorlog2(int a)
+int
+floorlog2 (int a)
 {
   int l;
-  for (l = 0; a > 1; l++) {
-    a >>= 1;
-  }
+  for (l = 0; a > 1; l++)
+    {
+      a >>= 1;
+    }
   return l;
 }
 
-void encode_stepsize(int stepsize, int numbps, int *expn, int *mant)
+void
+encode_stepsize (int stepsize, int numbps, int *expn, int *mant)
 {
   int p, n;
-  p = floorlog2(stepsize) - 13;
-  n = 11 - floorlog2(stepsize);
+  p = floorlog2 (stepsize) - 13;
+  n = 11 - floorlog2 (stepsize);
   *mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
   *expn = numbps - p;
 }
 
-void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec)
+void
+calc_explicit_stepsizes (j2k_tccp_t * tccp, int prec)
 {
   int numbands, bandno;
   numbands = 3 * tccp->numresolutions - 2;
-  for (bandno = 0; bandno < numbands; bandno++) {
-    double stepsize;
-
-    int resno, level, orient, gain;
-    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));
-    if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
-      stepsize = 1.0;
-    } else {
-      double norm = dwt_norms_97[orient][level];
-      stepsize = (1 << (gain + 1)) / norm;
+  for (bandno = 0; bandno < numbands; bandno++)
+    {
+      double stepsize;
+
+      int resno, level, orient, gain;
+      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));
+      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
+main (int argc, char **argv)
 {
   int len;
   int NumResolution, numD_min; /*   NumResolution : number of resolution                     */
@@ -307,368 +332,412 @@ int main(int argc, char **argv)
   /* img.PPT=0; */
 
   Tile_arg = 0;
-  cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t));      /* initialisation if only one tile */
+  cp_init.tcps = (j2k_tcp_t *) malloc (sizeof (j2k_tcp_t));    /* initialisation if only one tile */
   tcp_init = &cp_init.tcps[0];
   tcp_init->numlayers = 0;
 
-  cp.intermed_file=1;
+  cp.intermed_file = 1;
 
-  while (1) {
-    int c = getopt(argc, argv,
-                  "i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I");
-    if (c == -1)
-      break;
-    switch (c) {
-    case 'i':                  /* IN fill */
-      infile = optarg;
-      s = optarg;
-      while (*s) {
-       s++;
-      }
-      s--;
-      S3 = *s;
-      s--;
-      S2 = *s;
-      s--;
-      S1 = *s;
-
-      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')) {
-       cp.image_type = 1;
+  while (1)
+    {
+      int c = getopt (argc, argv,
+                     "i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I");
+      if (c == -1)
        break;
-      }
+      switch (c)
+       {
+       case 'i':               /* IN fill */
+         infile = optarg;
+         s = optarg;
+         while (*s)
+           {
+             s++;
+           }
+         s--;
+         S3 = *s;
+         s--;
+         S2 = *s;
+         s--;
+         S1 = *s;
+
+         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'))
+           {
+             cp.image_type = 1;
+             break;
+           }
+
+         if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
+             || (S1 == 'B' && S2 == 'M' && S3 == 'P'))
+           {
+             cp.image_type = 2;
+             break;
+           }
+         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 */
+         outfile = optarg;
+         while (*outfile)
+           {
+             outfile++;
+           }
+         outfile--;
+         S3 = *outfile;
+         outfile--;
+         S2 = *outfile;
+         outfile--;
+         S1 = *outfile;
+
+         outfile = optarg;
+
+         if ((S1 == 'j' && S2 == '2' && S3 == 'k')
+             || (S1 == 'J' && S2 == '2' && S3 == 'K'))
+           cp.JPEG2000_format = 0;
+         else if ((S1 == 'j' && S2 == 'p' && S3 == '2')
+                  || (S1 == 'J' && S2 == 'P' && S3 == '2'))
+           cp.JPEG2000_format = 1;
+         else
+           {
+             fprintf (stderr,
+                      "Unknown output format image *.%c%c%c [only *.j2k, *.jp2]!! \n",
+                      S1, S2, S3);
+             return 1;
+           }
+
+
 
-      if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
-         || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
-       cp.image_type = 2;
-       break;
-      }
-      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 */
-      outfile = optarg;
-      while (*outfile) {
-       outfile++;
-      }
-      outfile--;
-      S3 = *outfile;
-      outfile--;
-      S2 = *outfile;
-      outfile--;
-      S1 = *outfile;
-      
-      outfile = optarg;
-      
-      if ((S1 == 'j' && S2 == '2' && S3 == 'k') || (S1 == 'J' && S2 == '2' && S3 == 'K'))
-       cp.JPEG2000_format=0;
-      else if ((S1 == 'j' && S2 == 'p' && S3 == '2') || (S1 == 'J' && S2 == 'P' && S3 == '2'))
-       cp.JPEG2000_format=1;
-      else    {
-       fprintf(stderr,"Unknown output format image *.%c%c%c [only *.j2k, *.jp2]!! \n",S1,S2,S3);
-       return 1;
-      }
-      
-      
-      
-      break;
-      /* ----------------------------------------------------- */
-    case 'r':                  /* rates rates/distorsion */
-      s = optarg;
-      while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers])
-            == 1) {
-       tcp_init->numlayers++;
-       while (*s && *s != ',') {
-         s++;
-       }
-       if (!*s)
          break;
-       s++;
-      }
-      cp.disto_alloc = 1;
-      cp.matrice = NULL;
-      break;
-      /* ----------------------------------------------------- */
-    case 'q':                  /* add fixed_quality */
-      s = optarg;
-      while (sscanf(s, "%f", &tcp_init->distoratio[tcp_init->numlayers]) ==
-            1) {
-       tcp_init->numlayers++;
-       while (*s && *s != ',') {
-         s++;
-       }
-       if (!*s)
+         /* ----------------------------------------------------- */
+       case 'r':               /* rates rates/distorsion */
+         s = optarg;
+         while (sscanf (s, "%d", &tcp_init->rates[tcp_init->numlayers]) == 1)
+           {
+             tcp_init->numlayers++;
+             while (*s && *s != ',')
+               {
+                 s++;
+               }
+             if (!*s)
+               break;
+             s++;
+           }
+         cp.disto_alloc = 1;
+         cp.matrice = NULL;
          break;
-       s++;
-      }
-      cp.fixed_quality = 1;
-      cp.matrice = NULL;
-      break;
-      /* dda */
-      /* ----------------------------------------------------- */
-    case 'f':                  /* mod fixed_quality (before : -q) */
-      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)
+         /* ----------------------------------------------------- */
+       case 'q':               /* add fixed_quality */
+         s = optarg;
+         while (sscanf (s, "%f", &tcp_init->distoratio[tcp_init->numlayers])
+                == 1)
+           {
+             tcp_init->numlayers++;
+             while (*s && *s != ',')
+               {
+                 s++;
+               }
+             if (!*s)
+               break;
+             s++;
+           }
+         cp.fixed_quality = 1;
+         cp.matrice = NULL;
+         break;
+         /* dda */
+         /* ----------------------------------------------------- */
+       case 'f':               /* mod fixed_quality (before : -q) */
+         s = optarg;
+         sscanf (s, "%d", &tcp_init->numlayers);
          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)
+         if (tcp_init->numlayers > 9)
            s++;
-       }
-       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 */
-      sscanf(optarg, "%d", &NumResolution);
-      break;
-      /* ----------------------------------------------------- */
-    case 'c':                  /* precinct dimension */
-      s = optarg;
-      do {
-       sep = 0;
-       sscanf(s, "[%d,%d]%c", &prcw_init[res_spec],
-              &prch_init[res_spec], &sep);
-       CSty |= 0x01;
-       res_spec++;
-       s = strpbrk(s, "]") + 2;
-      } while (sep == ',');
-      break;
-      /* ----------------------------------------------------- */
-    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");
-       return 1;
-      }
-      break;
-      /* ----------------------------------------------------- */
-    case 'x':                  /* creation of index file */
-      index = optarg;
-      cp.index_on = 1;
-      break;
-      /* ----------------------------------------------------- */
-    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;
-      }
-      break;
-      /* ----------------------------------------------------- */
-    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");
-       return 1;
-      }
-      break;
-      /* ----------------------------------------------------- */
-    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");
-       return 1;
-      }
-      break;
-      /* ----------------------------------------------------- */
-    case 'h':                  /* Display an help description */
-      help_display();
-      return 0;
-      break;
-      /* ----------------------------------------------------- */
-    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) {
-       POC[numpocs].prg = give_progression(POC[numpocs].progorder);
-       /* POC[numpocs].tile; */
-       numpocs++;
-       while (*s && *s != '/') {
-         s++;
-       }
-       if (!*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++;
+               }
+             if (i < tcp_init->numlayers - 1)
+               s++;
+           }
+         cp.fixed_alloc = 1;
          break;
-       s++;
-      }
-      break;
-      /* ------------------------------------------------------ */
-    case 'S':                  /* SOP marker */
-      CSty |= 0x02;
-      break;
-      /* ------------------------------------------------------ */
-    case 'E':                  /* EPH marker */
-      CSty |= 0x04;
-      break;
-      /* ------------------------------------------------------ */
-    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);
-         if (cache)
-           mode |= (1 << i);
+         /* ----------------------------------------------------- */
+       case 't':               /* tiles */
+         sscanf (optarg, "%d,%d", &cp.tdx, &cp.tdy);
+         Tile_arg = 1;
+         break;
+         /* ----------------------------------------------------- */
+       case 'n':               /* resolution */
+         sscanf (optarg, "%d", &NumResolution);
+         break;
+         /* ----------------------------------------------------- */
+       case 'c':               /* precinct dimension */
+         s = optarg;
+         do
+           {
+             sep = 0;
+             sscanf (s, "[%d,%d]%c", &prcw_init[res_spec],
+                     &prch_init[res_spec], &sep);
+             CSty |= 0x01;
+             res_spec++;
+             s = strpbrk (s, "]") + 2;
+           }
+         while (sep == ',');
+         break;
+         /* ----------------------------------------------------- */
+       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");
+             return 1;
+           }
+         break;
+         /* ----------------------------------------------------- */
+       case 'x':               /* creation of index file */
+         index = optarg;
+         cp.index_on = 1;
+         break;
+         /* ----------------------------------------------------- */
+       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;
+           }
+         break;
+         /* ----------------------------------------------------- */
+       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");
+             return 1;
+           }
+         break;
+         /* ----------------------------------------------------- */
+       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");
+             return 1;
+           }
+         break;
+         /* ----------------------------------------------------- */
+       case 'h':               /* Display an help description */
+         help_display ();
+         return 0;
+         break;
+         /* ----------------------------------------------------- */
+       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)
+           {
+             POC[numpocs].prg = give_progression (POC[numpocs].progorder);
+             /* POC[numpocs].tile; */
+             numpocs++;
+             while (*s && *s != '/')
+               {
+                 s++;
+               }
+             if (!*s)
+               break;
+             s++;
+           }
+         break;
+         /* ------------------------------------------------------ */
+       case 'S':               /* SOP marker */
+         CSty |= 0x02;
+         break;
+         /* ------------------------------------------------------ */
+       case 'E':               /* EPH marker */
+         CSty |= 0x04;
+         break;
+         /* ------------------------------------------------------ */
+       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);
+                 if (cache)
+                   mode |= (1 << i);
+               }
+           }
+         break;
+         /* ------------------------------------------------------ */
+       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");
+             return 1;
+           }
+         break;
+         /* ------------------------------------------------------ */
+       case 'T':               /* Tile offset */
+         if (sscanf (optarg, "%d,%d", &TX0, &TY0) != 2)
+           {
+             fprintf (stderr,
+                      "-T 'tile offset' argument error !! [-T X0,Y0]");
+             return 1;
+           }
+         break;
+         /* ------------------------------------------------------ */
+       case 'C':               /* Add a comment */
+         cp.comment = optarg;
+         break;
+         /* ------------------------------------------------------ */
+       case 'I':               /* reversible or not */
+         ir = 1;
+         break;
+         /* ------------------------------------------------------ */
+       default:
+         return 1;
        }
-      }
-      break;
-      /* ------------------------------------------------------ */
-    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");
-       return 1;
-      }
-      break;
-      /* ------------------------------------------------------ */
-    case 'T':                  /* Tile offset */
-      if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
-       fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
-       return 1;
-      }
-      break;
-      /* ------------------------------------------------------ */
-    case 'C':                  /* Add a comment */
-      cp.comment = optarg;
-      break;
-      /* ------------------------------------------------------ */
-    case 'I':                  /* reversible or not */
-      ir = 1;
-      break;
-      /* ------------------------------------------------------ */
-    default:
-      return 1;
     }
-  }
 
   cp.tx0 = TX0;
   cp.ty0 = TY0;
 
   /* Error messages */
   /* -------------- */
-  if (!infile || !outfile) {
-    fprintf(stderr,
-           "usage: image_to_j2k -i image-file -o j2k/jp2-file (+ options)\n");
-    return 1;
-  }
-
-  if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)&&(!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) {
-    fprintf(stderr,
-           "Error: options -r -q and -f can not be used together !!\n");
-    return 1;
-  } // mod fixed_quality
+  if (!infile || !outfile)
+    {
+      fprintf (stderr,
+              "usage: image_to_j2k -i image-file -o j2k/jp2-file (+ options)\n");
+      return 1;
+    }
+
+  if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)
+      && (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality)))
+    {
+      fprintf (stderr,
+              "Error: options -r -q and -f can not be used together !!\n");
+      return 1;
+    }                          // mod fixed_quality
 
   /* if no rate entered, lossless by default */
-  if (tcp_init->numlayers == 0) {
-    tcp_init->rates[tcp_init->numlayers] = 1;
-    tcp_init->numlayers++;
-    cp.disto_alloc = 1;
-  }
-
-  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]);
-    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);
-    }
-  }
-
-  switch (cp.image_type) {
-  case 0:
-    if (Tile_arg) {
-      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)) {
-       fprintf(stderr, " not a pgx file\n");
-       return 1;
-      }
+  if (tcp_init->numlayers == 0)
+    {
+      tcp_init->rates[tcp_init->numlayers] = 0;        //MOD antonin : losslessbug
+      tcp_init->numlayers++;
+      cp.disto_alloc = 1;
     }
-    break;
 
-  case 1:
-    if (!pnmtoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) {
-      fprintf(stderr, " not a pnm file\n");
+  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]);
       return 1;
     }
-    break;
 
-  case 2:
-    if (!bmptoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) {
-      fprintf(stderr, " not a bmp file\n");
-      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);
+       }
+    }
+
+  switch (cp.image_type)
+    {
+    case 0:
+      if (Tile_arg)
+       {
+         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))
+           {
+             fprintf (stderr, " not a pgx file\n");
+             return 1;
+           }
+       }
+      break;
+
+    case 1:
+      if (!pnmtoimage (infile, &img, subsampling_dx, subsampling_dy, Dim))
+       {
+         fprintf (stderr, " not a pnm file\n");
+         return 1;
+       }
+      break;
+
+    case 2:
+      if (!bmptoimage (infile, &img, subsampling_dx, subsampling_dy, Dim))
+       {
+         fprintf (stderr, " not a bmp file\n");
+         return 1;
+       }
+      break;
     }
-    break;
-  }
   /* to respect profile - 0 */
   /* ---------------------- */
   numD_min = 0;
@@ -681,14 +750,17 @@ int main(int argc, char **argv)
      NumResolution=numD_min+1;
      } */
 
-  if (Tile_arg == 1) {
-    cp.tw = int_ceildiv(img.x1 - cp.tx0, cp.tdx);
-    cp.th = int_ceildiv(img.y1 - cp.ty0, cp.tdy);
-  } else {
-    cp.tdx = img.x1 - cp.tx0;
-    cp.tdy = img.y1 - cp.ty0;
-  }
+  if (Tile_arg == 1)
+    {
+      cp.tw = int_ceildiv (img.x1 - cp.tx0, cp.tdx);
+      cp.th = int_ceildiv (img.y1 - cp.ty0, cp.tdy);
+    }
+  else
+    {
+      cp.tdx = img.x1 - cp.tx0;
+      cp.tdy = img.y1 - cp.ty0;
+    }
+
   /* Initialization for PPM marker */
   cp.ppm = 0;
   cp.ppm_data = NULL;
@@ -697,190 +769,227 @@ int main(int argc, char **argv)
 
   /* Init the mutiple tiles */
   /* ---------------------- */
-  cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
-
-  for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
-    tcp = &cp.tcps[tileno];
-    tcp->numlayers = tcp_init->numlayers;
-    for (j = 0; j < tcp->numlayers; j++) {
-      if (cp.fixed_quality)   // add fixed_quality
-       tcp->distoratio[j] = tcp_init->distoratio[j];
-      else
-       tcp->rates[j] = tcp_init->rates[j];
-    }
-    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;
-    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;
-         tcp_poc->compno0 = POC[numpocs_tile].compno0;
-         tcp_poc->layno1 = POC[numpocs_tile].layno1;
-         tcp_poc->resno1 = POC[numpocs_tile].resno1;
-         tcp_poc->compno1 = POC[numpocs_tile].compno1;
-         tcp_poc->prg = POC[numpocs_tile].prg;
-         tcp_poc->tile = POC[numpocs_tile].tile;
-         numpocs_tile++;
+  cp.tcps = (j2k_tcp_t *) malloc (cp.tw * cp.th * sizeof (j2k_tcp_t));
+
+  for (tileno = 0; tileno < cp.tw * cp.th; tileno++)
+    {
+      tcp = &cp.tcps[tileno];
+      tcp->numlayers = tcp_init->numlayers;
+      for (j = 0; j < tcp->numlayers; j++)
+       {
+         if (cp.fixed_quality) // add fixed_quality
+           tcp->distoratio[j] = tcp_init->distoratio[j];
+         else
+           tcp->rates[j] = tcp_init->rates[j];
        }
-      }
-    }
-    tcp->numpocs = numpocs_tile;
-    tcp->tccps = (j2k_tccp_t *) malloc(img.numcomps * sizeof(j2k_tccp_t));
-
-    for (i = 0; i < img.numcomps; i++) {
-      tccp = &tcp->tccps[i];
-      tccp->csty = CSty & 0x01;        /* 0 => one precinct || 1 => custom precinct  */
-      tccp->numresolutions = NumResolution;
-      tccp->cblkw = int_floorlog2(cblockw_init);
-      tccp->cblkh = int_floorlog2(cblockh_init);
-      tccp->cblksty = mode;
-      tccp->qmfbid = ir ? 0 : 1;
-      tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
-      tccp->numgbits = 2;
-      if (i == ROI_compno)
-       tccp->roishift = ROI_shift;
-      else
-       tccp->roishift = 0;
-      if (CSty & J2K_CCP_CSTY_PRT) {
-       int p = 0;
-       for (j = tccp->numresolutions - 1; j >= 0; j--) {
-         if (p < res_spec) {
-           if (prcw_init[p] < 1)
-             tccp->prcw[j] = 1;
-           else
-             tccp->prcw[j] = int_floorlog2(prcw_init[p]);
-
-           if (prch_init[p] < 1)
-             tccp->prch[j] = 1;
-           else
-             tccp->prch[j] = int_floorlog2(prch_init[p]);
-         } else {
-           int size_prcw, size_prch;
-           size_prcw = prcw_init[res_spec - 1] >> (p - (res_spec - 1));
-           size_prch = prch_init[res_spec - 1] >> (p - (res_spec - 1));
-           if (size_prcw < 1)
-             tccp->prcw[j] = 1;
-           else
-             tccp->prcw[j] = int_floorlog2(size_prcw);
-           if (size_prch < 1)
-             tccp->prch[j] = 1;
-           else
-             tccp->prch[j] = int_floorlog2(size_prch);
-         }
-         p++;
-         /*printf("\nsize precinct pour level %d : %d,%d\n", j,
-                tccp->prcw[j], tccp->prch[j]);*/
+      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;
+      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;
+                 tcp_poc->compno0 = POC[numpocs_tile].compno0;
+                 tcp_poc->layno1 = POC[numpocs_tile].layno1;
+                 tcp_poc->resno1 = POC[numpocs_tile].resno1;
+                 tcp_poc->compno1 = POC[numpocs_tile].compno1;
+                 tcp_poc->prg = POC[numpocs_tile].prg;
+                 tcp_poc->tile = POC[numpocs_tile].tile;
+                 numpocs_tile++;
+               }
+           }
        }
-      } else {
-       for (j = 0; j < tccp->numresolutions; j++) {
-         tccp->prcw[j] = 15;
-         tccp->prch[j] = 15;
+      tcp->numpocs = numpocs_tile;
+      tcp->tccps = (j2k_tccp_t *) malloc (img.numcomps * sizeof (j2k_tccp_t));
+
+      for (i = 0; i < img.numcomps; i++)
+       {
+         tccp = &tcp->tccps[i];
+         tccp->csty = CSty & 0x01;     /* 0 => one precinct || 1 => custom precinct  */
+         tccp->numresolutions = NumResolution;
+         tccp->cblkw = int_floorlog2 (cblockw_init);
+         tccp->cblkh = int_floorlog2 (cblockh_init);
+         tccp->cblksty = mode;
+         tccp->qmfbid = ir ? 0 : 1;
+         tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
+         tccp->numgbits = 2;
+         if (i == ROI_compno)
+           tccp->roishift = ROI_shift;
+         else
+           tccp->roishift = 0;
+         if (CSty & J2K_CCP_CSTY_PRT)
+           {
+             int p = 0;
+             for (j = tccp->numresolutions - 1; j >= 0; j--)
+               {
+                 if (p < res_spec)
+                   {
+                     if (prcw_init[p] < 1)
+                       tccp->prcw[j] = 1;
+                     else
+                       tccp->prcw[j] = int_floorlog2 (prcw_init[p]);
+
+                     if (prch_init[p] < 1)
+                       tccp->prch[j] = 1;
+                     else
+                       tccp->prch[j] = int_floorlog2 (prch_init[p]);
+                   }
+                 else
+                   {
+                     int size_prcw, size_prch;
+                     size_prcw =
+                       prcw_init[res_spec - 1] >> (p - (res_spec - 1));
+                     size_prch =
+                       prch_init[res_spec - 1] >> (p - (res_spec - 1));
+                     if (size_prcw < 1)
+                       tccp->prcw[j] = 1;
+                     else
+                       tccp->prcw[j] = int_floorlog2 (size_prcw);
+                     if (size_prch < 1)
+                       tccp->prch[j] = 1;
+                     else
+                       tccp->prch[j] = int_floorlog2 (size_prch);
+                   }
+                 p++;
+                 /*printf("\nsize precinct pour level %d : %d,%d\n", j,
+                    tccp->prcw[j], tccp->prch[j]); */
+               }
+           }
+         else
+           {
+             for (j = 0; j < tccp->numresolutions; j++)
+               {
+                 tccp->prcw[j] = 15;
+                 tccp->prch[j] = 15;
+               }
+           }
+         calc_explicit_stepsizes (tccp, img.comps[i].prec);
        }
-      }
-      calc_explicit_stepsizes(tccp, img.comps[i].prec);
-    }
-  }
-
-  if (cp.JPEG2000_format==0) {       /* J2K format output */
-    if (cp.intermed_file==1) {       /* After the encoding of each tile, j2k_encode 
-                                        stores the data in the file*/
-      len = j2k_encode(&img, &cp, outfile, cp.tdx * cp.tdy * 2, index);
-      if (len == 0) {
-       fprintf(stderr, "failed to encode image\n");
-       return 1;
-      }
     }
-    else {
-      outbuf = (char *) malloc( cp.tdx * cp.tdy * cp.tw * cp.th * 2); /* Allocate memory for all tiles */
-      cio_init(outbuf, cp.tdx * cp.tdy * cp.tw * cp.th * 2);                                                    
-      len = j2k_encode(&img, &cp, outbuf, cp.tdx * cp.tdy * cp.tw * cp.th * 2, index); 
-      if (len == 0) {
-       fprintf(stderr, "failed to encode image\n");
-       return 1;
-      }
-      f = fopen(outfile, "wb");        
-      if (!f) {                                        
-       fprintf(stderr, "failed to open %s for writing\n", outfile);    
-       return 1;                               
-      }   
-      fwrite(outbuf, 1, len, f);
-      free(outbuf);
-      fclose(f);
-    }
-  }
-  else                        /* JP2 format output */
-  {
-    jp2_struct_t * jp2_struct;
-    jp2_struct = (jp2_struct_t *) malloc(sizeof(jp2_struct_t));
-    jp2_struct->image = &img;
-        
-    /* Initialising the standard JP2 box content*/
-    /* If you wish to modify those boxes, you have to modify the jp2_struct content*/ 
-    if (jp2_init_stdjp2(jp2_struct, &img))
-    {
-      fprintf(stderr,"Error with jp2 initialization");
-      return 1;
-    };
 
-    if (cp.intermed_file==1) {       
-      /*For the moment, JP2 format does not use intermediary files for each tile*/
-      cp.intermed_file=0;
-    }
-    outbuf = (char *) malloc( cp.tdx * cp.tdy * cp.tw * cp.th * 2);
-    cio_init(outbuf, cp.tdx * cp.tdy * cp.tw * cp.th * 2);    
-    len = jp2_encode(jp2_struct, &cp, outbuf, index);
-    if (len == 0) {
-      fprintf(stderr, "failed to encode image\n");
-      return 1;
+
+
+  if (cp.JPEG2000_format == 0)
+    {                          /* J2K format output */
+      if (cp.intermed_file == 1)
+       {                       /* After the encoding of each tile, j2k_encode 
+                                  stores the data in the file */
+         len = j2k_encode (&img, &cp, outfile, cp.tdx * cp.tdy * 2, index);
+         if (len == 0)
+           {
+             fprintf (stderr, "failed to encode image\n");
+             return 1;
+           }
+       }
+      else
+       {
+         outbuf = (char *) malloc (cp.tdx * cp.tdy * cp.tw * cp.th * 2);       /* Allocate memory for all tiles */
+         cio_init (outbuf, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
+         len =
+           j2k_encode (&img, &cp, outbuf,
+                       cp.tdx * cp.tdy * cp.tw * cp.th * 2, index);
+         if (len == 0)
+           {
+             fprintf (stderr, "failed to encode image\n");
+             return 1;
+           }
+         f = fopen (outfile, "wb");
+         if (!f)
+           {
+             fprintf (stderr, "failed to open %s for writing\n", outfile);
+             return 1;
+           }
+         fwrite (outbuf, 1, len, f);
+         free (outbuf);
+         fclose (f);
+       }
     }
-    f = fopen(outfile, "wb");  
-    if (!f) {                                  
-      fprintf(stderr, "failed to open %s for writing\n", outfile);     
-      return 1;                                
+  else                         /* JP2 format output */
+    {
+      jp2_struct_t *jp2_struct;
+      jp2_struct = (jp2_struct_t *) malloc (sizeof (jp2_struct_t));
+      jp2_struct->image = &img;
+
+      /* Initialising the standard JP2 box content */
+      /* If you wish to modify those boxes, you have to modify the jp2_struct content */
+      if (jp2_init_stdjp2 (jp2_struct, &img))
+       {
+         fprintf (stderr, "Error with jp2 initialization");
+         return 1;
+       };
+
+      if (cp.intermed_file == 1)
+       {
+         /*For the moment, JP2 format does not use intermediary files for each tile */
+         cp.intermed_file = 0;
+       }
+      outbuf = (char *) malloc (cp.tdx * cp.tdy * cp.tw * cp.th * 2);
+      cio_init (outbuf, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
+      len = jp2_encode (jp2_struct, &cp, outbuf, index);
+      if (len == 0)
+       {
+         fprintf (stderr, "failed to encode image\n");
+         return 1;
+       }
+      f = fopen (outfile, "wb");
+      if (!f)
+       {
+         fprintf (stderr, "failed to open %s for writing\n", outfile);
+         return 1;
+       }
+      fwrite (outbuf, 1, len, f);
+      free (outbuf);
+      fclose (f);
     }
-    fwrite(outbuf, 1, len, f);
-    free(outbuf);
-    fclose(f);
-  }
 
   /* Remove the temporary files */
   /* -------------------------- */
-  if (cp.image_type) {         /* PNM PGM PPM */
-    for (i = 0; i < img.numcomps; i++) {
-      char tmp;
-      sprintf(&tmp, "Compo%d", i);
-      if (remove(&tmp) == -1) {
-       fprintf(stderr, "failed to kill %s file !\n", &tmp);
-      }
+  if (cp.image_type)
+    {                          /* PNM PGM PPM */
+      for (i = 0; i < img.numcomps; i++)
+       {
+         char tmp;
+         sprintf (&tmp, "Compo%d", i);
+         if (remove (&tmp) == -1)
+           {
+             fprintf (stderr, "failed to kill %s file !\n", &tmp);
+           }
+       }
     }
-  } else {                     /* PGX */
-    for (i = 0; i < cp.th; i++) {
-      char tmp;
-      sprintf(&tmp, "bandtile%d", i + 1);
-
-      if (remove(&tmp) == -1) {
-       fprintf(stderr, "failed to kill %s file !\n", &tmp);
-      }
+  else
+    {                          /* PGX */
+      for (i = 0; i < cp.th; i++)
+       {
+         char tmp;
+         sprintf (&tmp, "bandtile%d", i + 1);
+
+         if (remove (&tmp) == -1)
+           {
+             fprintf (stderr, "failed to kill %s file !\n", &tmp);
+           }
+       }
     }
-  }
 
   /* Free memory */
-  free(img.comps);
-  free(cp_init.tcps);
-  if (tcp_init->numlayers > 9) free(cp.matrice);
+  free (img.comps);
+  free (cp_init.tcps);
+  if (tcp_init->numlayers > 9)
+    free (cp.matrice);
   for (tileno = 0; tileno < cp.tw * cp.th; tileno++)
-    free(cp.tcps[tileno].tccps);
-  free(cp.tcps);
+    free (cp.tcps[tileno].tccps);
+  free (cp.tcps);
 
   return 0;
 }
index e7816f5d3edf2535e68d908d443bd45c70715301..079ffa5b61a482e4d57575f1cd921d6f8930617c 100644 (file)
@@ -87,182 +87,202 @@ static int *j2k_tile_len;
 static info_image info_IM;
 
 /* Add Patrick */
-void j2k_clean()
+void
+j2k_clean ()
 {
   int tileno = 0;
-  tcd_free_encode(j2k_img, j2k_cp, j2k_curtileno);
+  tcd_free_encode (j2k_img, j2k_cp, j2k_curtileno);
 
-  if (info_IM.index_on) {
-    for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++) {
-      free(info_IM.tile[tileno].packet);
+  if (info_IM.index_on)
+    {
+      for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++)
+       {
+         free (info_IM.tile[tileno].packet);
+       }
+      free (info_IM.tile);
     }
-    free(info_IM.tile);
-  }
 }
 
 /* \Add Patrick */
 
-void j2k_dump_image(j2k_image_t * img)
+void
+j2k_dump_image (j2k_image_t * img)
 {
   int compno;
-  fprintf(stderr, "image {\n");
-  fprintf(stderr, "  x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0,
-         img->x1, img->y1);
-  fprintf(stderr, "  numcomps=%d\n", img->numcomps);
-  for (compno = 0; compno < img->numcomps; compno++) {
-    j2k_comp_t *comp = &img->comps[compno];
-    fprintf(stderr, "  comp %d {\n", compno);
-    fprintf(stderr, "    dx=%d, dy=%d\n", comp->dx, comp->dy);
-    fprintf(stderr, "    prec=%d\n", comp->prec);
-    fprintf(stderr, "    sgnd=%d\n", comp->sgnd);
-    fprintf(stderr, "  }\n");
-  }
-  fprintf(stderr, "}\n");
+  fprintf (stderr, "image {\n");
+  fprintf (stderr, "  x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0,
+          img->x1, img->y1);
+  fprintf (stderr, "  numcomps=%d\n", img->numcomps);
+  for (compno = 0; compno < img->numcomps; compno++)
+    {
+      j2k_comp_t *comp = &img->comps[compno];
+      fprintf (stderr, "  comp %d {\n", compno);
+      fprintf (stderr, "    dx=%d, dy=%d\n", comp->dx, comp->dy);
+      fprintf (stderr, "    prec=%d\n", comp->prec);
+      fprintf (stderr, "    sgnd=%d\n", comp->sgnd);
+      fprintf (stderr, "  }\n");
+    }
+  fprintf (stderr, "}\n");
 }
 
-void j2k_dump_cp(j2k_image_t * img, j2k_cp_t * cp)
+void
+j2k_dump_cp (j2k_image_t * img, j2k_cp_t * cp)
 {
   int tileno, compno, layno, bandno, resno, numbands;
-  fprintf(stderr, "coding parameters {\n");
-  fprintf(stderr, "  tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
-  fprintf(stderr, "  tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
-  fprintf(stderr, "  tw=%d, th=%d\n", cp->tw, cp->th);
-  for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
-    j2k_tcp_t *tcp = &cp->tcps[tileno];
-    fprintf(stderr, "  tile %d {\n", tileno);
-    fprintf(stderr, "    csty=%x\n", tcp->csty);
-    fprintf(stderr, "    prg=%d\n", tcp->prg);
-    fprintf(stderr, "    numlayers=%d\n", tcp->numlayers);
-    fprintf(stderr, "    mct=%d\n", tcp->mct);
-    fprintf(stderr, "    rates=");
-    for (layno = 0; layno < tcp->numlayers; layno++) {
-      fprintf(stderr, "%d ", tcp->rates[layno]);
-    }
-    fprintf(stderr, "\n");
-    for (compno = 0; compno < img->numcomps; compno++) {
-      j2k_tccp_t *tccp = &tcp->tccps[compno];
-      fprintf(stderr, "    comp %d {\n", compno);
-      fprintf(stderr, "      csty=%x\n", tccp->csty);
-      fprintf(stderr, "      numresolutions=%d\n", tccp->numresolutions);
-      fprintf(stderr, "      cblkw=%d\n", tccp->cblkw);
-      fprintf(stderr, "      cblkh=%d\n", tccp->cblkh);
-      fprintf(stderr, "      cblksty=%x\n", tccp->cblksty);
-      fprintf(stderr, "      qmfbid=%d\n", tccp->qmfbid);
-      fprintf(stderr, "      qntsty=%d\n", tccp->qntsty);
-      fprintf(stderr, "      numgbits=%d\n", tccp->numgbits);
-      fprintf(stderr, "      roishift=%d\n", tccp->roishift);
-      fprintf(stderr, "      stepsizes=");
-      numbands =
-       tccp->qntsty ==
-       J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
-      for (bandno = 0; bandno < numbands; bandno++) {
-       fprintf(stderr, "(%d,%d) ", tccp->stepsizes[bandno].mant,
-               tccp->stepsizes[bandno].expn);
-      }
-      fprintf(stderr, "\n");
-
-      if (tccp->csty & J2K_CCP_CSTY_PRT) {
-       fprintf(stderr, "      prcw=");
-       for (resno = 0; resno < tccp->numresolutions; resno++) {
-         fprintf(stderr, "%d ", tccp->prcw[resno]);
+  fprintf (stderr, "coding parameters {\n");
+  fprintf (stderr, "  tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
+  fprintf (stderr, "  tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
+  fprintf (stderr, "  tw=%d, th=%d\n", cp->tw, cp->th);
+  for (tileno = 0; tileno < cp->tw * cp->th; tileno++)
+    {
+      j2k_tcp_t *tcp = &cp->tcps[tileno];
+      fprintf (stderr, "  tile %d {\n", tileno);
+      fprintf (stderr, "    csty=%x\n", tcp->csty);
+      fprintf (stderr, "    prg=%d\n", tcp->prg);
+      fprintf (stderr, "    numlayers=%d\n", tcp->numlayers);
+      fprintf (stderr, "    mct=%d\n", tcp->mct);
+      fprintf (stderr, "    rates=");
+      for (layno = 0; layno < tcp->numlayers; layno++)
+       {
+         fprintf (stderr, "%d ", tcp->rates[layno]);
        }
-       fprintf(stderr, "\n");
-       fprintf(stderr, "      prch=");
-       for (resno = 0; resno < tccp->numresolutions; resno++) {
-         fprintf(stderr, "%d ", tccp->prch[resno]);
+      fprintf (stderr, "\n");
+      for (compno = 0; compno < img->numcomps; compno++)
+       {
+         j2k_tccp_t *tccp = &tcp->tccps[compno];
+         fprintf (stderr, "    comp %d {\n", compno);
+         fprintf (stderr, "      csty=%x\n", tccp->csty);
+         fprintf (stderr, "      numresolutions=%d\n", tccp->numresolutions);
+         fprintf (stderr, "      cblkw=%d\n", tccp->cblkw);
+         fprintf (stderr, "      cblkh=%d\n", tccp->cblkh);
+         fprintf (stderr, "      cblksty=%x\n", tccp->cblksty);
+         fprintf (stderr, "      qmfbid=%d\n", tccp->qmfbid);
+         fprintf (stderr, "      qntsty=%d\n", tccp->qntsty);
+         fprintf (stderr, "      numgbits=%d\n", tccp->numgbits);
+         fprintf (stderr, "      roishift=%d\n", tccp->roishift);
+         fprintf (stderr, "      stepsizes=");
+         numbands =
+           tccp->qntsty ==
+           J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
+         for (bandno = 0; bandno < numbands; bandno++)
+           {
+             fprintf (stderr, "(%d,%d) ", tccp->stepsizes[bandno].mant,
+                      tccp->stepsizes[bandno].expn);
+           }
+         fprintf (stderr, "\n");
+
+         if (tccp->csty & J2K_CCP_CSTY_PRT)
+           {
+             fprintf (stderr, "      prcw=");
+             for (resno = 0; resno < tccp->numresolutions; resno++)
+               {
+                 fprintf (stderr, "%d ", tccp->prcw[resno]);
+               }
+             fprintf (stderr, "\n");
+             fprintf (stderr, "      prch=");
+             for (resno = 0; resno < tccp->numresolutions; resno++)
+               {
+                 fprintf (stderr, "%d ", tccp->prch[resno]);
+               }
+             fprintf (stderr, "\n");
+           }
+         fprintf (stderr, "    }\n");
        }
-       fprintf(stderr, "\n");
-      }
-      fprintf(stderr, "    }\n");
+      fprintf (stderr, "  }\n");
     }
-    fprintf(stderr, "  }\n");
-  }
-  fprintf(stderr, "}\n");
+  fprintf (stderr, "}\n");
 }
 
-void j2k_write_soc()
+void
+j2k_write_soc ()
 {
-  cio_write(J2K_MS_SOC, 2);
+  cio_write (J2K_MS_SOC, 2);
 }
 
-void j2k_read_soc()
+void
+j2k_read_soc ()
 {
   j2k_state = J2K_STATE_MHSIZ;
 }
 
-void j2k_write_siz()
+void
+j2k_write_siz ()
 {
   int i;
   int lenp, len;
 
-  cio_write(J2K_MS_SIZ, 2);    /* SIZ                 */
-  lenp = cio_tell();
-  cio_skip(2);
-  cio_write(0, 2);             /* Rsiz (capabilities) */
-  cio_write(j2k_img->x1, 4);   /* Xsiz                */
-  cio_write(j2k_img->y1, 4);   /* Ysiz                */
-  cio_write(j2k_img->x0, 4);   /* X0siz               */
-  cio_write(j2k_img->y0, 4);   /* Y0siz               */
-  cio_write(j2k_cp->tdx, 4);   /* XTsiz               */
-  cio_write(j2k_cp->tdy, 4);   /* YTsiz               */
-  cio_write(j2k_cp->tx0, 4);   /* XT0siz              */
-  cio_write(j2k_cp->ty0, 4);   /* YT0siz              */
-  cio_write(j2k_img->numcomps, 2);     /* Csiz                */
-  for (i = 0; i < j2k_img->numcomps; i++) {
-    cio_write(j2k_img->comps[i].prec - 1 + (j2k_img->comps[i].sgnd << 7), 1);  /* Ssiz_i */
-    cio_write(j2k_img->comps[i].dx, 1);        /* XRsiz_i             */
-    cio_write(j2k_img->comps[i].dy, 1);        /* YRsiz_i             */
-  }
-  len = cio_tell() - lenp;
-  cio_seek(lenp);
-  cio_write(len, 2);           /* Lsiz                */
-  cio_seek(lenp + len);
+  cio_write (J2K_MS_SIZ, 2);   /* SIZ                 */
+  lenp = cio_tell ();
+  cio_skip (2);
+  cio_write (0, 2);            /* Rsiz (capabilities) */
+  cio_write (j2k_img->x1, 4);  /* Xsiz                */
+  cio_write (j2k_img->y1, 4);  /* Ysiz                */
+  cio_write (j2k_img->x0, 4);  /* X0siz               */
+  cio_write (j2k_img->y0, 4);  /* Y0siz               */
+  cio_write (j2k_cp->tdx, 4);  /* XTsiz               */
+  cio_write (j2k_cp->tdy, 4);  /* YTsiz               */
+  cio_write (j2k_cp->tx0, 4);  /* XT0siz              */
+  cio_write (j2k_cp->ty0, 4);  /* YT0siz              */
+  cio_write (j2k_img->numcomps, 2);    /* Csiz                */
+  for (i = 0; i < j2k_img->numcomps; i++)
+    {
+      cio_write (j2k_img->comps[i].prec - 1 + (j2k_img->comps[i].sgnd << 7), 1);       /* Ssiz_i */
+      cio_write (j2k_img->comps[i].dx, 1);     /* XRsiz_i             */
+      cio_write (j2k_img->comps[i].dy, 1);     /* YRsiz_i             */
+    }
+  len = cio_tell () - lenp;
+  cio_seek (lenp);
+  cio_write (len, 2);          /* Lsiz                */
+  cio_seek (lenp + len);
 
 }
 
-void j2k_read_siz()
+void
+j2k_read_siz ()
 {
   int len, i;
 
-  len = cio_read(2);           /* Lsiz                */
-  cio_read(2);                 /* Rsiz (capabilities) */
-  j2k_img->x1 = cio_read(4);   /* Xsiz                */
-  j2k_img->y1 = cio_read(4);   /* Ysiz                */
-  j2k_img->x0 = cio_read(4);   /* X0siz               */
-  j2k_img->y0 = cio_read(4);   /* Y0siz               */
-  j2k_cp->tdx = cio_read(4);   /* XTsiz               */
-  j2k_cp->tdy = cio_read(4);   /* YTsiz               */
-  j2k_cp->tx0 = cio_read(4);   /* XT0siz              */
-  j2k_cp->ty0 = cio_read(4);   /* YT0siz              */
-
-  j2k_img->numcomps = cio_read(2);     /* Csiz                */
+  len = cio_read (2);          /* Lsiz                */
+  cio_read (2);                        /* Rsiz (capabilities) */
+  j2k_img->x1 = cio_read (4);  /* Xsiz                */
+  j2k_img->y1 = cio_read (4);  /* Ysiz                */
+  j2k_img->x0 = cio_read (4);  /* X0siz               */
+  j2k_img->y0 = cio_read (4);  /* Y0siz               */
+  j2k_cp->tdx = cio_read (4);  /* XTsiz               */
+  j2k_cp->tdy = cio_read (4);  /* YTsiz               */
+  j2k_cp->tx0 = cio_read (4);  /* XT0siz              */
+  j2k_cp->ty0 = cio_read (4);  /* YT0siz              */
+
+  j2k_img->numcomps = cio_read (2);    /* Csiz                */
   j2k_img->comps =
-    (j2k_comp_t *) malloc(j2k_img->numcomps * sizeof(j2k_comp_t));
-  for (i = 0; i < j2k_img->numcomps; i++) {
-    int tmp, w, h;
-    tmp = cio_read(1);         /* Ssiz_i          */
-    j2k_img->comps[i].prec = (tmp & 0x7f) + 1;
-    j2k_img->comps[i].sgnd = tmp >> 7;
-    j2k_img->comps[i].dx = cio_read(1);        /* XRsiz_i         */
-    j2k_img->comps[i].dy = cio_read(1);        /* YRsiz_i         */
-    w = int_ceildiv(j2k_img->x1 - j2k_img->x0, j2k_img->comps[i].dx);
-    h = int_ceildiv(j2k_img->y1 - j2k_img->y0, j2k_img->comps[i].dy);
-    j2k_img->comps[i].resno_decoded = 0;       /* number of resolution decoded */
-    j2k_img->comps[i].factor = 0;      /* reducing factor by component */
-  }
-
-  j2k_cp->tw = int_ceildiv(j2k_img->x1 - j2k_cp->tx0, j2k_cp->tdx);
-  j2k_cp->th = int_ceildiv(j2k_img->y1 - j2k_cp->ty0, j2k_cp->tdy);
+    (j2k_comp_t *) malloc (j2k_img->numcomps * sizeof (j2k_comp_t));
+  for (i = 0; i < j2k_img->numcomps; i++)
+    {
+      int tmp, w, h;
+      tmp = cio_read (1);      /* Ssiz_i          */
+      j2k_img->comps[i].prec = (tmp & 0x7f) + 1;
+      j2k_img->comps[i].sgnd = tmp >> 7;
+      j2k_img->comps[i].dx = cio_read (1);     /* XRsiz_i         */
+      j2k_img->comps[i].dy = cio_read (1);     /* YRsiz_i         */
+      w = int_ceildiv (j2k_img->x1 - j2k_img->x0, j2k_img->comps[i].dx);
+      h = int_ceildiv (j2k_img->y1 - j2k_img->y0, j2k_img->comps[i].dy);
+      j2k_img->comps[i].resno_decoded = 0;     /* number of resolution decoded */
+      j2k_img->comps[i].factor = 0;    /* reducing factor by component */
+    }
+
+  j2k_cp->tw = int_ceildiv (j2k_img->x1 - j2k_cp->tx0, j2k_cp->tdx);
+  j2k_cp->th = int_ceildiv (j2k_img->y1 - j2k_cp->ty0, j2k_cp->tdy);
   j2k_cp->tcps =
-    (j2k_tcp_t *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(j2k_tcp_t));
-  j2k_cp->tileno = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int));
+    (j2k_tcp_t *) calloc (j2k_cp->tw * j2k_cp->th, sizeof (j2k_tcp_t));
+  j2k_cp->tileno = (int *) calloc (j2k_cp->tw * j2k_cp->th, sizeof (int));
   j2k_cp->tileno_size = 0;
 
-  for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) {
-    j2k_cp->tcps[i].POC = 0;
-    j2k_cp->tcps[i].numpocs = 0;
-    j2k_cp->tcps[i].first = 1;
-  }
+  for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++)
+    {
+      j2k_cp->tcps[i].POC = 0;
+      j2k_cp->tcps[i].numpocs = 0;
+      j2k_cp->tcps[i].first = 1;
+    }
 
   /* Initialization for PPM marker */
   j2k_cp->ppm = 0;
@@ -271,50 +291,55 @@ void j2k_read_siz()
   j2k_cp->ppm_store = 0;
 
   j2k_default_tcp.tccps =
-    (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps);
-  for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) {
-    j2k_cp->tcps[i].tccps =
-      (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps);
-  }
+    (j2k_tccp_t *) calloc (sizeof (j2k_tccp_t), j2k_img->numcomps);
+  for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++)
+    {
+      j2k_cp->tcps[i].tccps =
+       (j2k_tccp_t *) calloc (sizeof (j2k_tccp_t), j2k_img->numcomps);
+    }
   j2k_tile_data =
-    (unsigned char **) calloc(j2k_cp->tw * j2k_cp->th, sizeof(char *));
-  j2k_tile_len = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int));
+    (unsigned char **) calloc (j2k_cp->tw * j2k_cp->th, sizeof (char *));
+  j2k_tile_len = (int *) calloc (j2k_cp->tw * j2k_cp->th, sizeof (int));
   j2k_state = J2K_STATE_MH;
 
 
 }
 
-void j2k_write_com()
+void
+j2k_write_com ()
 {
   unsigned int i;
   int lenp, len;
   char str[256];
-  sprintf(str, "%s", j2k_cp->comment);
-
-  cio_write(J2K_MS_COM, 2);
-  lenp = cio_tell();
-  cio_skip(2);
-  cio_write(0, 2);
-  for (i = 0; i < strlen(str); i++) {
-    cio_write(str[i], 1);
-  }
-  len = cio_tell() - lenp;
-  cio_seek(lenp);
-  cio_write(len, 2);
-  cio_seek(lenp + len);
+  sprintf (str, "%s", j2k_cp->comment);
+
+  cio_write (J2K_MS_COM, 2);
+  lenp = cio_tell ();
+  cio_skip (2);
+  cio_write (0, 2);
+  for (i = 0; i < strlen (str); i++)
+    {
+      cio_write (str[i], 1);
+    }
+  len = cio_tell () - lenp;
+  cio_seek (lenp);
+  cio_write (len, 2);
+  cio_seek (lenp + len);
 
 }
 
-void j2k_read_com()
+void
+j2k_read_com ()
 {
   int len;
 
-  len = cio_read(2);
-  cio_skip(len - 2);
+  len = cio_read (2);
+  cio_skip (len - 2);
 
 }
 
-void j2k_write_cox(int compno)
+void
+j2k_write_cox (int compno)
 {
   int i;
   j2k_tcp_t *tcp;
@@ -322,20 +347,23 @@ void j2k_write_cox(int compno)
   tcp = &j2k_cp->tcps[j2k_curtileno];
   tccp = &tcp->tccps[compno];
 
-  cio_write(tccp->numresolutions - 1, 1);      /* SPcox (D) */
-  cio_write(tccp->cblkw - 2, 1);       /* SPcox (E) */
-  cio_write(tccp->cblkh - 2, 1);       /* SPcox (F) */
-  cio_write(tccp->cblksty, 1); /* SPcox (G) */
-  cio_write(tccp->qmfbid, 1);  /* SPcox (H) */
-
-  if (tccp->csty & J2K_CCP_CSTY_PRT) {
-    for (i = 0; i < tccp->numresolutions; i++) {
-      cio_write(tccp->prcw[i] + (tccp->prch[i] << 4), 1);      /* SPcox (I_i) */
+  cio_write (tccp->numresolutions - 1, 1);     /* SPcox (D) */
+  cio_write (tccp->cblkw - 2, 1);      /* SPcox (E) */
+  cio_write (tccp->cblkh - 2, 1);      /* SPcox (F) */
+  cio_write (tccp->cblksty, 1);        /* SPcox (G) */
+  cio_write (tccp->qmfbid, 1); /* SPcox (H) */
+
+  if (tccp->csty & J2K_CCP_CSTY_PRT)
+    {
+      for (i = 0; i < tccp->numresolutions; i++)
+       {
+         cio_write (tccp->prcw[i] + (tccp->prch[i] << 4), 1);  /* SPcox (I_i) */
+       }
     }
-  }
 }
 
-void j2k_read_cox(int compno)
+void
+j2k_read_cox (int compno)
 {
   int i;
   j2k_tcp_t *tcp;
@@ -344,44 +372,48 @@ void j2k_read_cox(int compno)
     j2k_state ==
     J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
   tccp = &tcp->tccps[compno];
-  tccp->numresolutions = cio_read(1) + 1;      /* SPcox (D) */
-  tccp->cblkw = cio_read(1) + 2;       /* SPcox (E) */
-  tccp->cblkh = cio_read(1) + 2;       /* SPcox (F) */
-  tccp->cblksty = cio_read(1); /* SPcox (G) */
-  tccp->qmfbid = cio_read(1);  /* SPcox (H) */
-  if (tccp->csty & J2K_CP_CSTY_PRT) {
-    for (i = 0; i < tccp->numresolutions; i++) {
-      int tmp = cio_read(1);   /* SPcox (I_i) */
-      tccp->prcw[i] = tmp & 0xf;
-      tccp->prch[i] = tmp >> 4;
+  tccp->numresolutions = cio_read (1) + 1;     /* SPcox (D) */
+  tccp->cblkw = cio_read (1) + 2;      /* SPcox (E) */
+  tccp->cblkh = cio_read (1) + 2;      /* SPcox (F) */
+  tccp->cblksty = cio_read (1);        /* SPcox (G) */
+  tccp->qmfbid = cio_read (1); /* SPcox (H) */
+  if (tccp->csty & J2K_CP_CSTY_PRT)
+    {
+      for (i = 0; i < tccp->numresolutions; i++)
+       {
+         int tmp = cio_read (1);       /* SPcox (I_i) */
+         tccp->prcw[i] = tmp & 0xf;
+         tccp->prch[i] = tmp >> 4;
+       }
     }
-  }
 }
 
-void j2k_write_cod()
+void
+j2k_write_cod ()
 {
   j2k_tcp_t *tcp;
   int lenp, len;
 
-  cio_write(J2K_MS_COD, 2);    /* COD */
+  cio_write (J2K_MS_COD, 2);   /* COD */
 
-  lenp = cio_tell();
-  cio_skip(2);
+  lenp = cio_tell ();
+  cio_skip (2);
 
   tcp = &j2k_cp->tcps[j2k_curtileno];
-  cio_write(tcp->csty, 1);     /* Scod */
-  cio_write(tcp->prg, 1);      /* SGcod (A) */
-  cio_write(tcp->numlayers, 2);        /* SGcod (B) */
-  cio_write(tcp->mct, 1);      /* SGcod (C) */
-
-  j2k_write_cox(0);
-  len = cio_tell() - lenp;
-  cio_seek(lenp);
-  cio_write(len, 2);           /* Lcod */
-  cio_seek(lenp + len);
+  cio_write (tcp->csty, 1);    /* Scod */
+  cio_write (tcp->prg, 1);     /* SGcod (A) */
+  cio_write (tcp->numlayers, 2);       /* SGcod (B) */
+  cio_write (tcp->mct, 1);     /* SGcod (C) */
+
+  j2k_write_cox (0);
+  len = cio_tell () - lenp;
+  cio_seek (lenp);
+  cio_write (len, 2);          /* Lcod */
+  cio_seek (lenp + len);
 }
 
-void j2k_read_cod()
+void
+j2k_read_cod ()
 {
   int len, i, pos;
   j2k_tcp_t *tcp;
@@ -389,39 +421,42 @@ void j2k_read_cod()
   tcp =
     j2k_state ==
     J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
-  len = cio_read(2);           /* Lcod */
-  tcp->csty = cio_read(1);     /* Scod */
-  tcp->prg = cio_read(1);      /* SGcod (A) */
-  tcp->numlayers = cio_read(2);        /* SGcod (B) */
-  tcp->mct = cio_read(1);      /* SGcod (C) */
-
-  pos = cio_tell();
-  for (i = 0; i < j2k_img->numcomps; i++) {
-    tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
-    cio_seek(pos);
-    j2k_read_cox(i);
-  }
+  len = cio_read (2);          /* Lcod */
+  tcp->csty = cio_read (1);    /* Scod */
+  tcp->prg = cio_read (1);     /* SGcod (A) */
+  tcp->numlayers = cio_read (2);       /* SGcod (B) */
+  tcp->mct = cio_read (1);     /* SGcod (C) */
+
+  pos = cio_tell ();
+  for (i = 0; i < j2k_img->numcomps; i++)
+    {
+      tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
+      cio_seek (pos);
+      j2k_read_cox (i);
+    }
 }
 
-void j2k_write_coc(int compno)
+void
+j2k_write_coc (int compno)
 {
   j2k_tcp_t *tcp;
   int lenp, len;
 
-  cio_write(J2K_MS_COC, 2);    /* COC */
-  lenp = cio_tell();
-  cio_skip(2);
+  cio_write (J2K_MS_COC, 2);   /* COC */
+  lenp = cio_tell ();
+  cio_skip (2);
   tcp = &j2k_cp->tcps[j2k_curtileno];
-  cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */
-  cio_write(tcp->tccps[compno].csty, 1);       /* Scoc */
-  j2k_write_cox(compno);
-  len = cio_tell() - lenp;
-  cio_seek(lenp);
-  cio_write(len, 2);           /* Lcoc */
-  cio_seek(lenp + len);
+  cio_write (compno, j2k_img->numcomps <= 256 ? 1 : 2);        /* Ccoc */
+  cio_write (tcp->tccps[compno].csty, 1);      /* Scoc */
+  j2k_write_cox (compno);
+  len = cio_tell () - lenp;
+  cio_seek (lenp);
+  cio_write (len, 2);          /* Lcoc */
+  cio_seek (lenp + len);
 }
 
-void j2k_read_coc()
+void
+j2k_read_coc ()
 {
   int len, compno;
   j2k_tcp_t *tcp;
@@ -429,13 +464,14 @@ void j2k_read_coc()
   tcp =
     j2k_state ==
     J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
-  len = cio_read(2);           /* Lcoc */
-  compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */
-  tcp->tccps[compno].csty = cio_read(1);       /* Scoc */
-  j2k_read_cox(compno);
+  len = cio_read (2);          /* Lcoc */
+  compno = cio_read (j2k_img->numcomps <= 256 ? 1 : 2);        /* Ccoc */
+  tcp->tccps[compno].csty = cio_read (1);      /* Scoc */
+  j2k_read_cox (compno);
 }
 
-void j2k_write_qcx(int compno)
+void
+j2k_write_qcx (int compno)
 {
   j2k_tcp_t *tcp;
   j2k_tccp_t *tccp;
@@ -445,25 +481,29 @@ void j2k_write_qcx(int compno)
   tcp = &j2k_cp->tcps[j2k_curtileno];
   tccp = &tcp->tccps[compno];
 
-  cio_write(tccp->qntsty + (tccp->numgbits << 5), 1);  /* Sqcx */
+  cio_write (tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
   numbands =
-    tccp->qntsty ==
-    J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
+    tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
 
-  for (bandno = 0; bandno < numbands; bandno++) {
-    expn = tccp->stepsizes[bandno].expn;
-    mant = tccp->stepsizes[bandno].mant;
+  for (bandno = 0; bandno < numbands; bandno++)
+    {
+      expn = tccp->stepsizes[bandno].expn;
+      mant = tccp->stepsizes[bandno].mant;
 
-    if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
-      cio_write(expn << 3, 1); /* SPqcx_i */
-    } else {
-      cio_write((expn << 11) + mant, 2);       /* SPqcx_i */
+      if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)
+       {
+         cio_write (expn << 3, 1);     /* SPqcx_i */
+       }
+      else
+       {
+         cio_write ((expn << 11) + mant, 2);   /* SPqcx_i */
+       }
     }
-  }
 
 }
 
-void j2k_read_qcx(int compno, int len)
+void
+j2k_read_qcx (int compno, int len)
 {
   int tmp;
   j2k_tcp_t *tcp;
@@ -474,26 +514,30 @@ void j2k_read_qcx(int compno, int len)
     j2k_state ==
     J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
   tccp = &tcp->tccps[compno];
-  tmp = cio_read(1);           /* Sqcx */
+  tmp = cio_read (1);          /* Sqcx */
   tccp->qntsty = tmp & 0x1f;
   tccp->numgbits = tmp >> 5;
   numbands =
     tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : (tccp->qntsty ==
                                                J2K_CCP_QNTSTY_NOQNT ?
                                                len - 1 : (len - 1) / 2);
-  for (bandno = 0; bandno < numbands; bandno++) {
-    int expn, mant;
-    if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
-      expn = cio_read(1) >> 3; /* SPqcx_i */
-      mant = 0;
-    } else {
-      tmp = cio_read(2);       /* SPqcx_i */
-      expn = tmp >> 11;
-      mant = tmp & 0x7ff;
+  for (bandno = 0; bandno < numbands; bandno++)
+    {
+      int expn, mant;
+      if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)
+       {
+         expn = cio_read (1) >> 3;     /* SPqcx_i */
+         mant = 0;
+       }
+      else
+       {
+         tmp = cio_read (2);   /* SPqcx_i */
+         expn = tmp >> 11;
+         mant = tmp & 0x7ff;
+       }
+      tccp->stepsizes[bandno].expn = expn;
+      tccp->stepsizes[bandno].mant = mant;
     }
-    tccp->stepsizes[bandno].expn = expn;
-    tccp->stepsizes[bandno].mant = mant;
-  }
 
 
 
@@ -501,74 +545,84 @@ void j2k_read_qcx(int compno, int len)
 
 
 
-  if (tccp->qntsty==J2K_CCP_QNTSTY_SIQNT) {
+  if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT)
+    {
 
-    for (bandno=1 ; bandno<J2K_MAXBANDS ; bandno++) {
+      for (bandno = 1; bandno < J2K_MAXBANDS; bandno++)
+       {
 
-      tccp->stepsizes[bandno].expn = ((tccp->stepsizes[0].expn)-((bandno-1)/3)>0)?(tccp->stepsizes[0].expn)-((bandno-1)/3):0;
+         tccp->stepsizes[bandno].expn =
+           ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) >
+            0) ? (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
 
-      tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
+         tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
 
-    }
+       }
 
-  }
+    }
 
 
 
   /* ddA */
 }
 
-void j2k_write_qcd()
+void
+j2k_write_qcd ()
 {
   int lenp, len;
 
-  cio_write(J2K_MS_QCD, 2);    /* QCD */
-  lenp = cio_tell();
-  cio_skip(2);
-  j2k_write_qcx(0);
-  len = cio_tell() - lenp;
-  cio_seek(lenp);
-  cio_write(len, 2);           /* Lqcd */
-  cio_seek(lenp + len);
+  cio_write (J2K_MS_QCD, 2);   /* QCD */
+  lenp = cio_tell ();
+  cio_skip (2);
+  j2k_write_qcx (0);
+  len = cio_tell () - lenp;
+  cio_seek (lenp);
+  cio_write (len, 2);          /* Lqcd */
+  cio_seek (lenp + len);
 }
 
-void j2k_read_qcd()
+void
+j2k_read_qcd ()
 {
   int len, i, pos;
 
-  len = cio_read(2);           /* Lqcd */
-  pos = cio_tell();
-  for (i = 0; i < j2k_img->numcomps; i++) {
-    cio_seek(pos);
-    j2k_read_qcx(i, len - 2);
-  }
+  len = cio_read (2);          /* Lqcd */
+  pos = cio_tell ();
+  for (i = 0; i < j2k_img->numcomps; i++)
+    {
+      cio_seek (pos);
+      j2k_read_qcx (i, len - 2);
+    }
 }
 
-void j2k_write_qcc(int compno)
+void
+j2k_write_qcc (int compno)
 {
   int lenp, len;
 
-  cio_write(J2K_MS_QCC, 2);    /* QCC */
-  lenp = cio_tell();
-  cio_skip(2);
-  cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */
-  j2k_write_qcx(compno);
-  len = cio_tell() - lenp;
-  cio_seek(lenp);
-  cio_write(len, 2);           /* Lqcc */
-  cio_seek(lenp + len);
+  cio_write (J2K_MS_QCC, 2);   /* QCC */
+  lenp = cio_tell ();
+  cio_skip (2);
+  cio_write (compno, j2k_img->numcomps <= 256 ? 1 : 2);        /* Cqcc */
+  j2k_write_qcx (compno);
+  len = cio_tell () - lenp;
+  cio_seek (lenp);
+  cio_write (len, 2);          /* Lqcc */
+  cio_seek (lenp + len);
 }
 
-void j2k_read_qcc()
+void
+j2k_read_qcc ()
 {
   int len, compno;
 
-  len = cio_read(2);           /* Lqcc */
-  compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */
-  j2k_read_qcx(compno, len - 2 - (j2k_img->numcomps <= 256 ? 1 : 2));
+  len = cio_read (2);          /* Lqcc */
+  compno = cio_read (j2k_img->numcomps <= 256 ? 1 : 2);        /* Cqcc */
+  j2k_read_qcx (compno, len - 2 - (j2k_img->numcomps <= 256 ? 1 : 2));
 }
 
-void j2k_write_poc()
+void
+j2k_write_poc ()
 {
   int len, numpchgs, i;
   j2k_tcp_t *tcp;
@@ -577,26 +631,28 @@ void j2k_write_poc()
   tcp = &j2k_cp->tcps[j2k_curtileno];
   tccp = &tcp->tccps[0];
   numpchgs = tcp->numpocs;
-  cio_write(J2K_MS_POC, 2);    /* POC  */
+  cio_write (J2K_MS_POC, 2);   /* POC  */
   len = 2 + (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2)) * numpchgs;
-  cio_write(len, 2);           /* Lpoc */
-  for (i = 0; i < numpchgs; i++) {
-    // MODIF
-    j2k_poc_t *poc;
-    poc = &tcp->pocs[i];
-    cio_write(poc->resno0, 1); /* RSpoc_i */
-    cio_write(poc->compno0, (j2k_img->numcomps <= 256 ? 1 : 2));       /* CSpoc_i */
-    cio_write(poc->layno1, 2); /* LYEpoc_i */
-    poc->layno1 = int_min(poc->layno1, tcp->numlayers);
-    cio_write(poc->resno1, 1); /* REpoc_i */
-    poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
-    cio_write(poc->compno1, (j2k_img->numcomps <= 256 ? 1 : 2));       /* CEpoc_i */
-    poc->compno1 = int_min(poc->compno1, j2k_img->numcomps);
-    cio_write(poc->prg, 1);    /* Ppoc_i */
-  }
+  cio_write (len, 2);          /* Lpoc */
+  for (i = 0; i < numpchgs; i++)
+    {
+      // MODIF
+      j2k_poc_t *poc;
+      poc = &tcp->pocs[i];
+      cio_write (poc->resno0, 1);      /* RSpoc_i */
+      cio_write (poc->compno0, (j2k_img->numcomps <= 256 ? 1 : 2));    /* CSpoc_i */
+      cio_write (poc->layno1, 2);      /* LYEpoc_i */
+      poc->layno1 = int_min (poc->layno1, tcp->numlayers);
+      cio_write (poc->resno1, 1);      /* REpoc_i */
+      poc->resno1 = int_min (poc->resno1, tccp->numresolutions);
+      cio_write (poc->compno1, (j2k_img->numcomps <= 256 ? 1 : 2));    /* CEpoc_i */
+      poc->compno1 = int_min (poc->compno1, j2k_img->numcomps);
+      cio_write (poc->prg, 1); /* Ppoc_i */
+    }
 }
 
-void j2k_read_poc()
+void
+j2k_read_poc ()
 {
   int len, numpchgs, i, old_poc;
   j2k_tcp_t *tcp;
@@ -609,306 +665,346 @@ void j2k_read_poc()
   old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
   tcp->POC = 1;
   tccp = &tcp->tccps[0];
-  len = cio_read(2);           /* Lpoc */
+  len = cio_read (2);          /* Lpoc */
   numpchgs = (len - 2) / (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2));
 
-  for (i = old_poc; i < numpchgs + old_poc; i++) {
-    j2k_poc_t *poc;
-    poc = &tcp->pocs[i];
-    poc->resno0 = cio_read(1); /* RSpoc_i */
-    poc->compno0 = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* CSpoc_i */
-    poc->layno1 = int_min(cio_read(2), tcp->numlayers);        /* LYEpoc_i */
-    poc->resno1 = int_min(cio_read(1), tccp->numresolutions);  /* REpoc_i */
-    poc->compno1 = int_min(cio_read(j2k_img->numcomps <= 256 ? 1 : 2), j2k_img->numcomps);     /* CEpoc_i */
-    poc->prg = cio_read(1);    /* Ppoc_i */
-  }
+  for (i = old_poc; i < numpchgs + old_poc; i++)
+    {
+      j2k_poc_t *poc;
+      poc = &tcp->pocs[i];
+      poc->resno0 = cio_read (1);      /* RSpoc_i */
+      poc->compno0 = cio_read (j2k_img->numcomps <= 256 ? 1 : 2);      /* CSpoc_i */
+      poc->layno1 = int_min (cio_read (2), tcp->numlayers);    /* LYEpoc_i */
+      poc->resno1 = int_min (cio_read (1), tccp->numresolutions);      /* REpoc_i */
+      poc->compno1 = int_min (cio_read (j2k_img->numcomps <= 256 ? 1 : 2), j2k_img->numcomps); /* CEpoc_i */
+      poc->prg = cio_read (1); /* Ppoc_i */
+    }
 
   tcp->numpocs = numpchgs + old_poc - 1;
 }
 
-void j2k_read_crg()
+void
+j2k_read_crg ()
 {
   int len, i, Xcrg_i, Ycrg_i;
 
-  len = cio_read(2);           /* Lcrg */
-  for (i = 0; i < j2k_img->numcomps; i++) {
-    Xcrg_i = cio_read(2);      /* Xcrg_i */
-    Ycrg_i = cio_read(2);      /* Ycrg_i */
-  }
+  len = cio_read (2);          /* Lcrg */
+  for (i = 0; i < j2k_img->numcomps; i++)
+    {
+      Xcrg_i = cio_read (2);   /* Xcrg_i */
+      Ycrg_i = cio_read (2);   /* Ycrg_i */
+    }
 }
 
-void j2k_read_tlm()
+void
+j2k_read_tlm ()
 {
   int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
   long int Ttlm_i, Ptlm_i;
 
-  len = cio_read(2);           /* Ltlm */
-  Ztlm = cio_read(1);          /* Ztlm */
-  Stlm = cio_read(1);          /* Stlm */
+  len = cio_read (2);          /* Ltlm */
+  Ztlm = cio_read (1);         /* Ztlm */
+  Stlm = cio_read (1);         /* Stlm */
   ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
   SP = (Stlm >> 6) & 0x01;
   tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
-  for (i = 0; i < tile_tlm; i++) {
-    Ttlm_i = cio_read(ST);     /* Ttlm_i */
-    Ptlm_i = cio_read(SP ? 4 : 2);     /* Ptlm_i */
-  }
+  for (i = 0; i < tile_tlm; i++)
+    {
+      Ttlm_i = cio_read (ST);  /* Ttlm_i */
+      Ptlm_i = cio_read (SP ? 4 : 2);  /* Ptlm_i */
+    }
 }
 
-void j2k_read_plm()
+void
+j2k_read_plm ()
 {
   int len, i, Zplm, Nplm, add, packet_len = 0;
 
-  len = cio_read(2);           /* Lplm */
-  Zplm = cio_read(1);          /* Zplm */
+  len = cio_read (2);          /* Lplm */
+  Zplm = cio_read (1);         /* Zplm */
   len -= 3;
-  while (len > 0) {
-    Nplm = cio_read(4);                /* Nplm */
-    len -= 4;
-    for (i = Nplm; i > 0; i--) {
-      add = cio_read(1);
-      len--;
-      packet_len = (packet_len << 7) + add;    /* Iplm_ij */
-      if ((add & 0x80) == 0) {
-       /* New packet */
-       packet_len = 0;
-      }
-      if (len <= 0)
-       break;
+  while (len > 0)
+    {
+      Nplm = cio_read (4);     /* Nplm */
+      len -= 4;
+      for (i = Nplm; i > 0; i--)
+       {
+         add = cio_read (1);
+         len--;
+         packet_len = (packet_len << 7) + add; /* Iplm_ij */
+         if ((add & 0x80) == 0)
+           {
+             /* New packet */
+             packet_len = 0;
+           }
+         if (len <= 0)
+           break;
+       }
     }
-  }
 }
 
-void j2k_read_plt()
+void
+j2k_read_plt ()
 {
   int len, i, Zplt, packet_len = 0, add;
 
-  len = cio_read(2);           /* Lplt */
-  Zplt = cio_read(1);          /* Zplt */
-  for (i = len - 3; i > 0; i--) {
-    add = cio_read(1);
-    packet_len = (packet_len << 7) + add;      /* Iplt_i */
-    if ((add & 0x80) == 0) {
-      /* New packet */
-      packet_len = 0;
+  len = cio_read (2);          /* Lplt */
+  Zplt = cio_read (1);         /* Zplt */
+  for (i = len - 3; i > 0; i--)
+    {
+      add = cio_read (1);
+      packet_len = (packet_len << 7) + add;    /* Iplt_i */
+      if ((add & 0x80) == 0)
+       {
+         /* New packet */
+         packet_len = 0;
+       }
     }
-  }
 }
 
-void j2k_read_ppm()
+void
+j2k_read_ppm ()
 {
   int len, Z_ppm, i, j;
   int N_ppm;
 
-  len = cio_read(2);
+  len = cio_read (2);
   j2k_cp->ppm = 1;
 
-  Z_ppm = cio_read(1);         /* Z_ppm */
+  Z_ppm = cio_read (1);                /* Z_ppm */
   len -= 3;
-  while (len > 0) {
-    if (j2k_cp->ppm_previous == 0) {
-      N_ppm = cio_read(4);     /* N_ppm */
-      len -= 4;
-    } else {
-      N_ppm = j2k_cp->ppm_previous;
-    }
+  while (len > 0)
+    {
+      if (j2k_cp->ppm_previous == 0)
+       {
+         N_ppm = cio_read (4); /* N_ppm */
+         len -= 4;
+       }
+      else
+       {
+         N_ppm = j2k_cp->ppm_previous;
+       }
+
+      j = j2k_cp->ppm_store;
+      if (Z_ppm == 0)
+       {                       /* First PPM marker */
+         j2k_cp->ppm_data =
+           (unsigned char *) calloc (N_ppm, sizeof (unsigned char));
 
-    j = j2k_cp->ppm_store;
-    if (Z_ppm == 0) {          /* First PPM marker */
-      j2k_cp->ppm_data =
-       (unsigned char *) calloc(N_ppm, sizeof(unsigned char));
+         j2k_cp->ppm_len = N_ppm;      //Add antonin : ppmbug1
 
-      j2k_cp->ppm_len = N_ppm;       //Add antonin : ppmbug1
+       }
+      else
+       {                       /* NON-first PPM marker */
+         j2k_cp->ppm_data =
+           (unsigned char *) realloc (j2k_cp->ppm_data,
+                                      (N_ppm +
+                                       j2k_cp->ppm_store) *
+                                      sizeof (unsigned char));
 
-    } else {                   /* NON-first PPM marker */
-      j2k_cp->ppm_data =
-       (unsigned char *) realloc(j2k_cp->ppm_data,
-                                 (N_ppm +
-                                  j2k_cp->ppm_store) *
-                                 sizeof(unsigned char));
+         j2k_cp->ppm_len = N_ppm + j2k_cp->ppm_store;  //Add antonin : ppmbug1
 
-      j2k_cp->ppm_len = N_ppm + j2k_cp->ppm_store; //Add antonin : ppmbug1
+       }
 
-    }
+      for (i = N_ppm; i > 0; i--)
+       {                       /* Read packet header */
+         j2k_cp->ppm_data[j] = cio_read (1);
+         j++;
+         len--;
+         if (len == 0)
+           break;              /* Case of non-finished packet header in present marker but finished in next one */
+       }
 
-    for (i = N_ppm; i > 0; i--) {      /* Read packet header */
-      j2k_cp->ppm_data[j] = cio_read(1);
-      j++;
-      len--;
-      if (len == 0)
-       break;                  /* Case of non-finished packet header in present marker but finished in next one */
+      j2k_cp->ppm_previous = i - 1;
+      j2k_cp->ppm_store = j;
     }
-
-    j2k_cp->ppm_previous = i - 1;
-    j2k_cp->ppm_store = j;
-  }
 }
 
-void j2k_read_ppt()
+void
+j2k_read_ppt ()
 {
   int len, Z_ppt, i, j = 0;
   j2k_tcp_t *tcp;
 
-  len = cio_read(2);
-  Z_ppt = cio_read(1);
+  len = cio_read (2);
+  Z_ppt = cio_read (1);
   tcp = &j2k_cp->tcps[j2k_curtileno];
   tcp->ppt = 1;
-  if (Z_ppt == 0) {            /* First PPT marker */
-    tcp->ppt_data =
-      (unsigned char *) calloc(len - 3, sizeof(unsigned char));
-    tcp->ppt_store = 0;
+  if (Z_ppt == 0)
+    {                          /* First PPT marker */
+      tcp->ppt_data =
+       (unsigned char *) calloc (len - 3, sizeof (unsigned char));
+      tcp->ppt_store = 0;
 
-    tcp->ppt_len = len-3;      //Add antonin : ppmbug1
-  } else {                     /* NON-first PPT marker */
-    tcp->ppt_data =
-      (unsigned char *) realloc(tcp->ppt_data,
-                               (len - 3 +
-                                tcp->ppt_store) * sizeof(unsigned char));
+      tcp->ppt_len = len - 3;  //Add antonin : ppmbug1
+    }
+  else
+    {                          /* NON-first PPT marker */
+      tcp->ppt_data =
+       (unsigned char *) realloc (tcp->ppt_data,
+                                  (len - 3 +
+                                   tcp->ppt_store) * sizeof (unsigned char));
 
-    tcp->ppt_len=len - 3 + tcp->ppt_store;    //Add antonin : ppmbug1
+      tcp->ppt_len = len - 3 + tcp->ppt_store; //Add antonin : ppmbug1
 
-  }
+    }
 
   j = tcp->ppt_store;
-  for (i = len - 3; i > 0; i--) {
-    tcp->ppt_data[j] = cio_read(1);
-    j++;
-  }
+  for (i = len - 3; i > 0; i--)
+    {
+      tcp->ppt_data[j] = cio_read (1);
+      j++;
+    }
   tcp->ppt_store = j;
 }
 
-void j2k_write_sot()
+void
+j2k_write_sot ()
 {
   int lenp, len;
 
-  j2k_sot_start = cio_tell();
-  cio_write(J2K_MS_SOT, 2);    /* SOT */
-  lenp = cio_tell();
-  cio_skip(2);                 /* Lsot (further) */
-  cio_write(j2k_curtileno, 2); /* Isot */
-  cio_skip(4);                 /* Psot (further in j2k_write_sod) */
-  cio_write(0, 1);             /* TPsot */
-  cio_write(1, 1);             /* TNsot */
-  len = cio_tell() - lenp;
-  cio_seek(lenp);
-  cio_write(len, 2);           /* Lsot */
-  cio_seek(lenp + len);
+  j2k_sot_start = cio_tell ();
+  cio_write (J2K_MS_SOT, 2);   /* SOT */
+  lenp = cio_tell ();
+  cio_skip (2);                        /* Lsot (further) */
+  cio_write (j2k_curtileno, 2);        /* Isot */
+  cio_skip (4);                        /* Psot (further in j2k_write_sod) */
+  cio_write (0, 1);            /* TPsot */
+  cio_write (1, 1);            /* TNsot */
+  len = cio_tell () - lenp;
+  cio_seek (lenp);
+  cio_write (len, 2);          /* Lsot */
+  cio_seek (lenp + len);
 }
 
-void j2k_read_sot()
+void
+j2k_read_sot ()
 {
   int len, tileno, totlen, partno, numparts, i;
   j2k_tcp_t *tcp;
   j2k_tccp_t *tmp;
   char status = 0;
 
-  len = cio_read(2);
-  tileno = cio_read(2);
-
-  if (j2k_cp->tileno_size == 0) {
-    j2k_cp->tileno[j2k_cp->tileno_size] = tileno;
-    j2k_cp->tileno_size++;
-  } else {
-    i = 0;
-    while (i < j2k_cp->tileno_size && status == 0) {
-      status = j2k_cp->tileno[i] == tileno ? 1 : 0;
-      i++;
-    }
-    if (status == 0) {
+  len = cio_read (2);
+  tileno = cio_read (2);
+
+  if (j2k_cp->tileno_size == 0)
+    {
       j2k_cp->tileno[j2k_cp->tileno_size] = tileno;
       j2k_cp->tileno_size++;
     }
-  }
+  else
+    {
+      i = 0;
+      while (i < j2k_cp->tileno_size && status == 0)
+       {
+         status = j2k_cp->tileno[i] == tileno ? 1 : 0;
+         i++;
+       }
+      if (status == 0)
+       {
+         j2k_cp->tileno[j2k_cp->tileno_size] = tileno;
+         j2k_cp->tileno_size++;
+       }
+    }
 
-  totlen = cio_read(4);
+  totlen = cio_read (4);
   if (!totlen)
-    totlen = cio_numbytesleft() + 8;
+    totlen = cio_numbytesleft () + 8;
 
-  partno = cio_read(1);
-  numparts = cio_read(1);
+  partno = cio_read (1);
+  numparts = cio_read (1);
 
   j2k_curtileno = tileno;
-  j2k_eot = cio_getbp() - 12 + totlen;
+  j2k_eot = cio_getbp () - 12 + totlen;
   j2k_state = J2K_STATE_TPH;
   tcp = &j2k_cp->tcps[j2k_curtileno];
 
-  if (tcp->first == 1) {
-    tmp = tcp->tccps;
-    *tcp = j2k_default_tcp;
+  if (tcp->first == 1)
+    {
+      tmp = tcp->tccps;
+      *tcp = j2k_default_tcp;
 
-    /* Initialization PPT */
-    tcp->ppt = 0;
-    tcp->ppt_data = NULL;
+      /* Initialization PPT */
+      tcp->ppt = 0;
+      tcp->ppt_data = NULL;
 
-    tcp->tccps = tmp;
-    for (i = 0; i < j2k_img->numcomps; i++) {
-      tcp->tccps[i] = j2k_default_tcp.tccps[i];
+      tcp->tccps = tmp;
+      for (i = 0; i < j2k_img->numcomps; i++)
+       {
+         tcp->tccps[i] = j2k_default_tcp.tccps[i];
+       }
+      j2k_cp->tcps[j2k_curtileno].first = 0;
     }
-    j2k_cp->tcps[j2k_curtileno].first = 0;
-  }
 }
 
-void j2k_write_sod()
+void
+j2k_write_sod ()
 {
   int l, layno;
   int totlen;
   j2k_tcp_t *tcp;
   static int j2k_sod_start;
 
-  cio_write(J2K_MS_SOD, 2);
-  if (j2k_curtileno == 0) {
-    j2k_sod_start = cio_tell() + pos_correction;
-  }
+  cio_write (J2K_MS_SOD, 2);
+  if (j2k_curtileno == 0)
+    {
+      j2k_sod_start = cio_tell () + pos_correction;
+    }
 
   /* INDEX >> */
-  if (info_IM.index_on) {
-    info_IM.tile[j2k_curtileno].end_header =
-      cio_tell() + pos_correction - 1;
-    info_IM.tile[j2k_curtileno].packet =
-      (info_packet *) calloc(info_IM.Comp * info_IM.Layer *
-                            (info_IM.Decomposition + 1) * 100,
-                            sizeof(info_packet));
-  }
+  if (info_IM.index_on)
+    {
+      info_IM.tile[j2k_curtileno].end_header =
+       cio_tell () + pos_correction - 1;
+      info_IM.tile[j2k_curtileno].packet =
+       (info_packet *) calloc (info_IM.Comp * info_IM.Layer *
+                               (info_IM.Decomposition + 1) * 100,
+                               sizeof (info_packet));
+    }
   /* << INDEX */
 
   tcp = &j2k_cp->tcps[j2k_curtileno];
-  for (layno = 0; layno < tcp->numlayers; layno++) {
-    tcp->rates[layno] -= (j2k_sod_start / (j2k_cp->th * j2k_cp->tw));
-  }
+  for (layno = 0; layno < tcp->numlayers; layno++)
+    {
+      tcp->rates[layno] -= tcp->rates[layno] ? (j2k_sod_start / (j2k_cp->th * j2k_cp->tw)) : 0;        //Mod antonin losslessbug
+    }
 
   info_IM.num = 0;
   if (j2k_cp->image_type)
-    l = tcd_encode_tile_pxm(j2k_curtileno, cio_getbp(),
-                           cio_numbytesleft() - 2, &info_IM);
+    l = tcd_encode_tile_pxm (j2k_curtileno, cio_getbp (),
+                            cio_numbytesleft () - 2, &info_IM);
   else
-    l = tcd_encode_tile_pgx(j2k_curtileno, cio_getbp(),
-                           cio_numbytesleft() - 2, &info_IM);
+    l = tcd_encode_tile_pgx (j2k_curtileno, cio_getbp (),
+                            cio_numbytesleft () - 2, &info_IM);
 
   /* Writing Psot in SOT marker */
-  totlen = cio_tell() + l - j2k_sot_start;
-  cio_seek(j2k_sot_start + 6);
-  cio_write(totlen, 4);
-  cio_seek(j2k_sot_start + totlen);
+  totlen = cio_tell () + l - j2k_sot_start;
+  cio_seek (j2k_sot_start + 6);
+  cio_write (totlen, 4);
+  cio_seek (j2k_sot_start + totlen);
 }
 
-void j2k_read_sod()
+void
+j2k_read_sod ()
 {
   int len, truncate = 0, i;
   unsigned char *data;
 
-  len = int_min(j2k_eot - cio_getbp(), cio_numbytesleft() + 1);
-  if (len == cio_numbytesleft() + 1)
+  len = int_min (j2k_eot - cio_getbp (), cio_numbytesleft () + 1);
+  if (len == cio_numbytesleft () + 1)
     truncate = 1;              /* Case of a truncate codestream */
 
   data =
-    (unsigned char *) malloc((j2k_tile_len[j2k_curtileno] + len) *
-                            sizeof(unsigned char));
+    (unsigned char *) malloc ((j2k_tile_len[j2k_curtileno] + len) *
+                             sizeof (unsigned char));
   for (i = 0; i < j2k_tile_len[j2k_curtileno]; i++)
     data[i] = j2k_tile_data[j2k_curtileno][i];
   for (i = 0; i < len; i++)
-    data[i + j2k_tile_len[j2k_curtileno]] = cio_read(1);
+    data[i + j2k_tile_len[j2k_curtileno]] = cio_read (1);
 
   j2k_tile_len[j2k_curtileno] += len;
-  free(j2k_tile_data[j2k_curtileno]);
+  free (j2k_tile_data[j2k_curtileno]);
   j2k_tile_data[j2k_curtileno] = data;
   data = NULL;
 
@@ -918,18 +1014,20 @@ void j2k_read_sod()
     j2k_state = J2K_STATE_NEOC;        /* RAJOUTE !! */
 }
 
-void j2k_write_rgn(int compno, int tileno)
+void
+j2k_write_rgn (int compno, int tileno)
 {
   j2k_tcp_t *tcp = &j2k_cp->tcps[tileno];
 
-  cio_write(J2K_MS_RGN, 2);    /* RGN  */
-  cio_write(j2k_img->numcomps <= 256 ? 5 : 6, 2);      /* Lrgn */
-  cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */
-  cio_write(0, 1);             /* Srgn */
-  cio_write(tcp->tccps[compno].roishift, 1);   /* SPrgn */
+  cio_write (J2K_MS_RGN, 2);   /* RGN  */
+  cio_write (j2k_img->numcomps <= 256 ? 5 : 6, 2);     /* Lrgn */
+  cio_write (compno, j2k_img->numcomps <= 256 ? 1 : 2);        /* Crgn */
+  cio_write (0, 1);            /* Srgn */
+  cio_write (tcp->tccps[compno].roishift, 1);  /* SPrgn */
 }
 
-void j2k_read_rgn()
+void
+j2k_read_rgn ()
 {
   int len, compno, roisty;
   j2k_tcp_t *tcp;
@@ -937,60 +1035,68 @@ void j2k_read_rgn()
   tcp =
     j2k_state ==
     J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
-  len = cio_read(2);           /* Lrgn */
-  compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */
-  roisty = cio_read(1);                /* Srgn */
-  tcp->tccps[compno].roishift = cio_read(1);   /* SPrgn */
+  len = cio_read (2);          /* Lrgn */
+  compno = cio_read (j2k_img->numcomps <= 256 ? 1 : 2);        /* Crgn */
+  roisty = cio_read (1);       /* Srgn */
+  tcp->tccps[compno].roishift = cio_read (1);  /* SPrgn */
 }
 
-void j2k_write_eoc()
+void
+j2k_write_eoc ()
 {
   /* fprintf(stderr, "%.8x: EOC\n", cio_tell() + pos_correction); */
-  cio_write(J2K_MS_EOC, 2);
+  cio_write (J2K_MS_EOC, 2);
 }
 
-void j2k_read_eoc()
+void
+j2k_read_eoc ()
 {
   int i, tileno;
 
-  tcd_init(j2k_img, j2k_cp);
+  tcd_init (j2k_img, j2k_cp);
 
-  for (i = 0; i < j2k_cp->tileno_size; i++) {
-    tileno = j2k_cp->tileno[i];
-    tcd_decode_tile(j2k_tile_data[tileno], j2k_tile_len[tileno], tileno);
-    free(j2k_tile_data[tileno]);
-  }
+  for (i = 0; i < j2k_cp->tileno_size; i++)
+    {
+      tileno = j2k_cp->tileno[i];
+      tcd_decode_tile (j2k_tile_data[tileno], j2k_tile_len[tileno], tileno);
+      free (j2k_tile_data[tileno]);
+    }
 
   j2k_state = J2K_STATE_MT;
-  longjmp(j2k_error, 1);
+  longjmp (j2k_error, 1);
 }
 
-void j2k_read_unk()
+void
+j2k_read_unk ()
 {
-  fprintf(stderr, "warning: unknown marker\n");
+  fprintf (stderr, "warning: unknown marker\n");
 }
 
-LIBJ2K_API int j2k_encode(j2k_image_t * img, j2k_cp_t * cp, char *output,
-                         int len, char *index)
+LIBJ2K_API int
+j2k_encode (j2k_image_t * img, j2k_cp_t * cp, char *output,
+           int len, char *index)
 {
   int tileno, compno, layno, resno, precno, pack_nb;
   char *dest = NULL;
   FILE *INDEX = NULL;
   FILE *f = NULL;
 
-  if (setjmp(j2k_error)) {
-    return 0;
-  }
+  if (setjmp (j2k_error))
+    {
+      return 0;
+    }
 
-  if (cp->intermed_file==1) {
-    f = fopen(output, "wb");
-    if (!f) {
-      fprintf(stderr, "failed to open %s for writing\n", output);
-      return 1;
+  if (cp->intermed_file == 1)
+    {
+      f = fopen (output, "wb");
+      if (!f)
+       {
+         fprintf (stderr, "failed to open %s for writing\n", output);
+         return 1;
+       }
+      dest = (char *) malloc (len);
+      cio_init (dest, len);
     }
-    dest = (char *) malloc(len);
-    cio_init(dest, len);
-  }
 
   j2k_img = img;
   j2k_cp = cp;
@@ -998,402 +1104,475 @@ LIBJ2K_API int j2k_encode(j2k_image_t * img, j2k_cp_t * cp, char *output,
 
   /* INDEX >> */
   info_IM.index_on = j2k_cp->index_on;
-  if (info_IM.index_on) {
-    info_IM.tile =
-      (info_tile *) malloc(j2k_cp->tw * j2k_cp->th * sizeof(info_tile));
-    info_IM.Im_w = j2k_img->x1 - j2k_img->x0;
-    info_IM.Im_h = j2k_img->y1 - j2k_img->y0;
-    info_IM.Prog = (&j2k_cp->tcps[0])->prg;
-    info_IM.tw=j2k_cp->tw; 
-    info_IM.th=j2k_cp->th; 
-    info_IM.Tile_x = j2k_cp->tdx;      /* new version parser */
-    info_IM.Tile_y = j2k_cp->tdy;      /* new version parser */
-    info_IM.Comp = j2k_img->numcomps;
-    info_IM.Layer = (&j2k_cp->tcps[0])->numlayers;
-    info_IM.Decomposition = (&j2k_cp->tcps[0])->tccps->numresolutions - 1;
-    info_IM.D_max = 0;         /* ADD Marcela */
-  }
+  if (info_IM.index_on)
+    {
+      info_IM.tile =
+       (info_tile *) malloc (j2k_cp->tw * j2k_cp->th * sizeof (info_tile));
+      info_IM.Im_w = j2k_img->x1 - j2k_img->x0;
+      info_IM.Im_h = j2k_img->y1 - j2k_img->y0;
+      info_IM.Prog = (&j2k_cp->tcps[0])->prg;
+      info_IM.tw = j2k_cp->tw;
+      info_IM.th = j2k_cp->th;
+      info_IM.Tile_x = j2k_cp->tdx;    /* new version parser */
+      info_IM.Tile_y = j2k_cp->tdy;    /* new version parser */
+      info_IM.Comp = j2k_img->numcomps;
+      info_IM.Layer = (&j2k_cp->tcps[0])->numlayers;
+      info_IM.Decomposition = (&j2k_cp->tcps[0])->tccps->numresolutions - 1;
+      info_IM.D_max = 0;       /* ADD Marcela */
+    }
   /* << INDEX */
 
-  j2k_write_soc();
-  j2k_write_siz();
-  j2k_write_cod();
-  j2k_write_qcd();
-  for (compno = 0; compno < j2k_img->numcomps; compno++) {
-    j2k_tcp_t *tcp = &j2k_cp->tcps[0];
-    if (tcp->tccps[compno].roishift)
-      j2k_write_rgn(compno, 0);
-  }
+  j2k_write_soc ();
+  j2k_write_siz ();
+  j2k_write_cod ();
+  j2k_write_qcd ();
+  for (compno = 0; compno < j2k_img->numcomps; compno++)
+    {
+      j2k_tcp_t *tcp = &j2k_cp->tcps[0];
+      if (tcp->tccps[compno].roishift)
+       j2k_write_rgn (compno, 0);
+    }
   if (j2k_cp->comment != NULL)
-    j2k_write_com();
+    j2k_write_com ();
 
-  if (cp->intermed_file==1) {
-    /* Writing the main header */
-    pos_correction = cio_tell();
-    fwrite(dest, 1, cio_tell(), f);
-  }
+  if (cp->intermed_file == 1)
+    {
+      /* Writing the main header */
+      pos_correction = cio_tell ();
+      fwrite (dest, 1, cio_tell (), f);
+    }
 
   /* INDEX >> */
-  if (info_IM.index_on) {
-    info_IM.Main_head_end = cio_tell() - 1;
-  }
+  if (info_IM.index_on)
+    {
+      info_IM.Main_head_end = cio_tell () - 1;
+    }
   /* << INDEX */
 
 
-  for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
-    fprintf(stderr, "Tile number %d / %d ", tileno + 1,
-      cp->tw * cp->th);
-    
-    if (cp->intermed_file==1) {
-      /* new dest for each tile  */
-      free(dest);
-      dest = (char *) malloc(len);
-      cio_init(dest, len);
-    }
-    j2k_curtileno = tileno;
-    /* initialisation before tile encoding  */
+  for (tileno = 0; tileno < cp->tw * cp->th; tileno++)
+    {
+      fprintf (stderr, "Tile number %d / %d ", tileno + 1, cp->tw * cp->th);
 
-    if (tileno == 0) {
-      tcd_malloc_encode(j2k_img, j2k_cp, j2k_curtileno);
-    } else {
-      tcd_init_encode(j2k_img, j2k_cp, j2k_curtileno);
-    }
+      if (cp->intermed_file == 1)
+       {
+         /* new dest for each tile  */
+         free (dest);
+         dest = (char *) malloc (len);
+         cio_init (dest, len);
+       }
+      j2k_curtileno = tileno;
+      /* initialisation before tile encoding  */
 
-    /* INDEX >> */
-    if (info_IM.index_on) {
-      info_IM.tile[j2k_curtileno].num_tile = j2k_curtileno;
-      info_IM.tile[j2k_curtileno].start_pos = cio_tell() + pos_correction;
-    }
-    /* << INDEX */
-    j2k_write_sot();
+      if (tileno == 0)
+       {
+         tcd_malloc_encode (j2k_img, j2k_cp, j2k_curtileno);
+       }
+      else
+       {
+         tcd_init_encode (j2k_img, j2k_cp, j2k_curtileno);
+       }
 
-    for (compno = 1; compno < img->numcomps; compno++) {
-      j2k_write_coc(compno);
-      j2k_write_qcc(compno);
-    }
+      /* INDEX >> */
+      if (info_IM.index_on)
+       {
+         info_IM.tile[j2k_curtileno].num_tile = j2k_curtileno;
+         info_IM.tile[j2k_curtileno].start_pos =
+           cio_tell () + pos_correction;
+       }
+      /* << INDEX */
+      j2k_write_sot ();
 
-    if (cp->tcps[tileno].numpocs)
-      j2k_write_poc();
-    j2k_write_sod();
+      for (compno = 1; compno < img->numcomps; compno++)
+       {
+         j2k_write_coc (compno);
+         j2k_write_qcc (compno);
+       }
 
-    /* INDEX >> */
-    if (info_IM.index_on) {
-      info_IM.tile[j2k_curtileno].end_pos =
-       cio_tell() + pos_correction - 1;
-    }
-    /* << INDEX */
-
-    /*
-       if (tile->PPT)  BAD PPT !!!
-       {
-       FILE *PPT_file;
-
-       int i;
-       PPT_file=fopen("PPT","rb");
-       fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
-       for (i=0;i<tile->len_ppt;i++)
-       {
-       unsigned char elmt;
-       fread(&elmt, 1, 1, PPT_file);
-       fwrite(&elmt,1,1,f);
-       }
-       fclose(PPT_file);
-       unlink("PPT");
-       }
-     */
-    if (cp->intermed_file==1) {
-      fwrite(dest, 1, cio_tell(), f);
-      pos_correction = cio_tell() + pos_correction;
+      if (cp->tcps[tileno].numpocs)
+       j2k_write_poc ();
+      j2k_write_sod ();
+
+      /* INDEX >> */
+      if (info_IM.index_on)
+       {
+         info_IM.tile[j2k_curtileno].end_pos =
+           cio_tell () + pos_correction - 1;
+       }
+      /* << INDEX */
+
+      /*
+         if (tile->PPT)  BAD PPT !!!
+         {
+         FILE *PPT_file;
+
+         int i;
+         PPT_file=fopen("PPT","rb");
+         fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
+         for (i=0;i<tile->len_ppt;i++)
+         {
+         unsigned char elmt;
+         fread(&elmt, 1, 1, PPT_file);
+         fwrite(&elmt,1,1,f);
+         }
+         fclose(PPT_file);
+         unlink("PPT");
+         }
+       */
+      if (cp->intermed_file == 1)
+       {
+         fwrite (dest, 1, cio_tell (), f);
+         pos_correction = cio_tell () + pos_correction;
+       }
     }
-  }
 
-  if (cp->intermed_file==1) {
-    free(dest);
-    dest = (char *) malloc(len);
-    cio_init(dest, len);
-  }
+  if (cp->intermed_file == 1)
+    {
+      free (dest);
+      dest = (char *) malloc (len);
+      cio_init (dest, len);
+    }
 
-  j2k_write_eoc();
+  j2k_write_eoc ();
 
-  if (cp->intermed_file==1) {
-    fwrite(dest, 1, 2, f);
-    free(dest);
-    /* closing file *.j2k */
-    fclose(f);
-  }
+  if (cp->intermed_file == 1)
+    {
+      fwrite (dest, 1, 2, f);
+      free (dest);
+      /* closing file *.j2k */
+      fclose (f);
+    }
 
   /* Creation of the index file     */
 
-  if (info_IM.index_on) {
-
-    double DistoTotal = 0;
+  if (info_IM.index_on)
+    {
 
-    info_IM.codestream_size = cio_tell() + pos_correction;     /* Correction 14/4/03 suite rmq de Patrick */
+      double DistoTotal = 0;
 
-    INDEX = fopen(index, "w");
+      info_IM.codestream_size = cio_tell () + pos_correction;  /* Correction 14/4/03 suite rmq de Patrick */
 
+      INDEX = fopen (index, "w");
 
 
-    if (!INDEX) {
 
-      fprintf(stderr, "failed to open %s for writing\n", index);
+      if (!INDEX)
+       {
 
-      return 1;
+         fprintf (stderr, "failed to open %s for writing\n", index);
 
-    }
-
-
-
-    fprintf(INDEX, "%d %d\n", info_IM.Im_w, info_IM.Im_h);
-
-    fprintf(INDEX, "%d\n", info_IM.Prog);
+         return 1;
 
-    fprintf(INDEX, "%d %d\n", info_IM.Tile_x, info_IM.Tile_y);
-
-    fprintf(INDEX, "%d %d\n", info_IM.tw, info_IM.th);
-
-    fprintf(INDEX, "%d\n", info_IM.Comp);
+       }
 
-    fprintf(INDEX, "%d\n", info_IM.Layer);
 
-    fprintf(INDEX, "%d\n", info_IM.Decomposition);
 
-    for (resno=info_IM.Decomposition;resno>=0;resno--) {
+      fprintf (INDEX, "%d %d\n", info_IM.Im_w, info_IM.Im_h);
 
-      fprintf(INDEX, "[%d,%d] ", (1<<info_IM.tile[0].pdx[resno]), (1<<info_IM.tile[0].pdx[resno])); //based on tile 0
+      fprintf (INDEX, "%d\n", info_IM.Prog);
 
-    }
+      fprintf (INDEX, "%d %d\n", info_IM.Tile_x, info_IM.Tile_y);
 
-    fprintf(INDEX,"\n");
+      fprintf (INDEX, "%d %d\n", info_IM.tw, info_IM.th);
 
-    fprintf(INDEX, "%d\n", info_IM.Main_head_end);
+      fprintf (INDEX, "%d\n", info_IM.Comp);
 
-    fprintf(INDEX, "%d\n", info_IM.codestream_size);
+      fprintf (INDEX, "%d\n", info_IM.Layer);
 
-    for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++) {
+      fprintf (INDEX, "%d\n", info_IM.Decomposition);
 
-      fprintf(INDEX, "%4d %9d %9d %9d %9e %9d %9e\n",
+      for (resno = info_IM.Decomposition; resno >= 0; resno--)
+       {
 
-             info_IM.tile[tileno].num_tile,
+         fprintf (INDEX, "[%d,%d] ", (1 << info_IM.tile[0].pdx[resno]), (1 << info_IM.tile[0].pdx[resno]));    //based on tile 0
 
-             info_IM.tile[tileno].start_pos,
+       }
 
-             info_IM.tile[tileno].end_header,
+      fprintf (INDEX, "\n");
 
-             info_IM.tile[tileno].end_pos, info_IM.tile[tileno].distotile, info_IM.tile[tileno].nbpix,
+      fprintf (INDEX, "%d\n", info_IM.Main_head_end);
 
-             info_IM.tile[tileno].distotile / info_IM.tile[tileno].nbpix);
+      fprintf (INDEX, "%d\n", info_IM.codestream_size);
 
-    }
+      for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++)
+       {
 
-    for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++) {
+         fprintf (INDEX, "%4d %9d %9d %9d %9e %9d %9e\n",
+                  info_IM.tile[tileno].num_tile,
+                  info_IM.tile[tileno].start_pos,
+                  info_IM.tile[tileno].end_header,
+                  info_IM.tile[tileno].end_pos,
+                  info_IM.tile[tileno].distotile, info_IM.tile[tileno].nbpix,
+                  info_IM.tile[tileno].distotile /
+                  info_IM.tile[tileno].nbpix);
 
-      int start_pos, end_pos;
+       }
 
-      double disto = 0;
+      for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++)
+       {
 
-      pack_nb = 0;
+         int start_pos, end_pos;
 
-      /* fprintf(INDEX,
+         double disto = 0;
 
-             "pkno tileno layerno resno compno precno start_pos   end_pos       deltaSE        \n");*/
+         pack_nb = 0;
 
-      if (info_IM.Prog == 0) { /* LRCP */
+         /* fprintf(INDEX,
 
-       for (layno = 0; layno < info_IM.Layer; layno++) {
+            "pkno tileno layerno resno compno precno start_pos   end_pos       deltaSE        \n"); */
 
-         for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {
+         if (info_IM.Prog == 0)
+           {                   /* LRCP */
 
-           for (compno = 0; compno < info_IM.Comp; compno++) {
+             for (layno = 0; layno < info_IM.Layer; layno++)
+               {
 
-             for (precno = 0;
+                 for (resno = 0; resno < info_IM.Decomposition + 1; resno++)
+                   {
 
-                  precno <
+                     for (compno = 0; compno < info_IM.Comp; compno++)
+                       {
 
-                  info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno];
+                         for (precno = 0;
+                              precno <
+                              info_IM.tile[tileno].pw[resno] *
+                              info_IM.tile[tileno].ph[resno]; precno++)
+                           {
 
-                  precno++) {
+                             start_pos =
+                               info_IM.tile[tileno].packet[pack_nb].
+                               start_pos;
 
-               start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;
+                             end_pos =
+                               info_IM.tile[tileno].packet[pack_nb].end_pos;
 
-               end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;
+                             disto =
+                               info_IM.tile[tileno].packet[pack_nb].disto;
 
-               disto = info_IM.tile[tileno].packet[pack_nb].disto;
+                             fprintf (INDEX,
+                                      "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
+                                      pack_nb, tileno, layno, resno, compno,
+                                      precno, start_pos, end_pos, disto);
 
-               fprintf(INDEX, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
+                             DistoTotal += disto;
 
-                       pack_nb, tileno, layno, resno, compno, precno,
+                             pack_nb++;
 
-                       start_pos, end_pos, disto);
+                           }
 
-               DistoTotal += disto;
+                       }
 
-               pack_nb++;
+                   }
 
-             }
+               }
 
            }
+         else if (info_IM.Prog == 1)
+           {                   /* RLCP */
 
-         }
+             for (resno = 0; resno < info_IM.Decomposition + 1; resno++)
+               {
 
-       }
-
-      } else if (info_IM.Prog == 1) {  /* RLCP */
+                 for (layno = 0; layno < info_IM.Layer; layno++)
+                   {
 
-       for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {
+                     for (compno = 0; compno < info_IM.Comp; compno++)
+                       {
 
-         for (layno = 0; layno < info_IM.Layer; layno++) {
+                         for (precno = 0;
+                              precno <
+                              info_IM.tile[tileno].pw[resno] *
+                              info_IM.tile[tileno].ph[resno]; precno++)
+                           {
 
-           for (compno = 0; compno < info_IM.Comp; compno++) {
+                             start_pos =
+                               info_IM.tile[tileno].packet[pack_nb].
+                               start_pos;
 
-             for (precno = 0; precno < info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno]; precno++) {
+                             end_pos =
+                               info_IM.tile[tileno].packet[pack_nb].end_pos;
 
-               start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;
+                             disto =
+                               info_IM.tile[tileno].packet[pack_nb].disto;
 
-               end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;
+                             fprintf (INDEX,
+                                      "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
+                                      pack_nb, tileno, layno, resno, compno,
+                                      precno, start_pos, end_pos, disto);
 
-               disto = info_IM.tile[tileno].packet[pack_nb].disto;
+                             DistoTotal += disto;
 
-               fprintf(INDEX, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
+                             pack_nb++;
 
-                       pack_nb, tileno, layno, resno, compno, precno,
+                           }
 
-                       start_pos, end_pos, disto);
+                       }
 
-               DistoTotal += disto;
+                   }
 
-               pack_nb++;
-
-             }
+               }
 
            }
+         else if (info_IM.Prog == 2)
+           {                   /* RPCL */
 
-         }
-
-       }
-
-      } else if (info_IM.Prog == 2) {  /* RPCL */
+             for (resno = 0; resno < info_IM.Decomposition + 1; resno++)
+               {
 
-       for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {
+                 for (precno = 0;
+                      precno <
+                      info_IM.tile[tileno].pw[resno] *
+                      info_IM.tile[tileno].ph[resno]; precno++)
+                   {
 
-         for (precno = 0; precno < info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno]; precno++) {
+                     for (compno = 0; compno < info_IM.Comp; compno++)
+                       {
 
-           for (compno = 0; compno < info_IM.Comp; compno++) {
+                         for (layno = 0; layno < info_IM.Layer; layno++)
+                           {
 
-             for (layno = 0; layno < info_IM.Layer; layno++) {
+                             start_pos =
+                               info_IM.tile[tileno].packet[pack_nb].
+                               start_pos;
 
-               start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;
+                             end_pos =
+                               info_IM.tile[tileno].packet[pack_nb].end_pos;
 
-               end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;
+                             disto =
+                               info_IM.tile[tileno].packet[pack_nb].disto;
 
-               disto = info_IM.tile[tileno].packet[pack_nb].disto;
+                             fprintf (INDEX,
+                                      "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
+                                      pack_nb, tileno, layno, resno, compno,
+                                      precno, start_pos, end_pos, disto);
 
-               fprintf(INDEX, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
+                             DistoTotal += disto;
 
-                       pack_nb, tileno, layno, resno, compno, precno,
+                             pack_nb++;
 
-                       start_pos, end_pos, disto);
+                           }
 
-               DistoTotal += disto;
+                       }
 
-               pack_nb++;
+                   }
 
-             }
+               }
 
            }
+         else if (info_IM.Prog == 3)
+           {                   /* PCRL */
 
-         }
-
-       }
-
-      } else if (info_IM.Prog == 3) {  /* PCRL */
+             for (precno = 0;
+                  precno <
+                  info_IM.tile[tileno].pw[resno] *
+                  info_IM.tile[tileno].ph[resno]; precno++)
+               {
 
-       for (precno = 0; precno < info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno]; precno++) {
+                 for (compno = 0; compno < info_IM.Comp; compno++)
+                   {
 
-         for (compno = 0; compno < info_IM.Comp; compno++) {
+                     for (resno = 0; resno < info_IM.Decomposition + 1;
+                          resno++)
+                       {
 
-           for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {
+                         for (layno = 0; layno < info_IM.Layer; layno++)
+                           {
 
-             for (layno = 0; layno < info_IM.Layer; layno++) {
+                             start_pos =
+                               info_IM.tile[tileno].packet[pack_nb].
+                               start_pos;
 
-               start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;
+                             end_pos =
+                               info_IM.tile[tileno].packet[pack_nb].end_pos;
 
-               end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;
+                             disto =
+                               info_IM.tile[tileno].packet[pack_nb].disto;
 
-               disto = info_IM.tile[tileno].packet[pack_nb].disto;
+                             fprintf (INDEX,
+                                      "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
+                                      pack_nb, tileno, layno, resno, compno,
+                                      precno, start_pos, end_pos, disto);
 
-               fprintf(INDEX, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
+                             DistoTotal += disto;
 
-                       pack_nb, tileno, layno, resno, compno, precno,
+                             pack_nb++;
 
-                       start_pos, end_pos, disto);
+                           }
 
-               DistoTotal += disto;
+                       }
 
-               pack_nb++;
+                   }
 
-             }
+               }
 
            }
+         else
+           {                   /* CPRL */
 
-         }
-
-       }
-
-      } else {                 /* CPRL */
 
 
+             for (compno = 0; compno < info_IM.Comp; compno++)
+               {
 
-       for (compno = 0; compno < info_IM.Comp; compno++) {
+                 for (precno = 0;
+                      precno <
+                      info_IM.tile[tileno].pw[resno] *
+                      info_IM.tile[tileno].ph[resno]; precno++)
+                   {
 
-         for (precno = 0; precno < info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno]; precno++) {
+                     for (resno = 0; resno < info_IM.Decomposition + 1;
+                          resno++)
+                       {
 
-           for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {
+                         for (layno = 0; layno < info_IM.Layer; layno++)
+                           {
 
-             for (layno = 0; layno < info_IM.Layer; layno++) {
+                             start_pos =
+                               info_IM.tile[tileno].packet[pack_nb].
+                               start_pos;
 
-               start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;
+                             end_pos =
+                               info_IM.tile[tileno].packet[pack_nb].end_pos;
 
-               end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;
+                             disto =
+                               info_IM.tile[tileno].packet[pack_nb].disto;
 
-               disto = info_IM.tile[tileno].packet[pack_nb].disto;
+                             fprintf (INDEX,
+                                      "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
+                                      pack_nb, tileno, layno, resno, compno,
+                                      precno, start_pos, end_pos, disto);
 
-               fprintf(INDEX, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
+                             DistoTotal += disto;
 
-                       pack_nb, tileno, layno, resno, compno, precno,
+                             pack_nb++;
 
-                       start_pos, end_pos, disto);
+                           }
 
-               DistoTotal += disto;
+                       }
 
-               pack_nb++;
+                   }
 
-             }
+               }
 
            }
 
-         }
-
        }
 
-      }
-
-    }
-
-    fprintf(INDEX, "SE max : %8e\n", info_IM.D_max);
+      fprintf (INDEX, "SE max : %8e\n", info_IM.D_max);
 
-    fprintf(INDEX, "SE total : %.8e\n", DistoTotal);
+      fprintf (INDEX, "SE total : %.8e\n", DistoTotal);
 
-    fclose(INDEX);
+      fclose (INDEX);
 
-  }
+    }
 
-  j2k_clean();
+  j2k_clean ();
 
-  return cio_tell();
+  return cio_tell ();
 }
 
-typedef struct {
+typedef struct
+{
   int id;
   int states;
   void (*handler) ();
@@ -1422,61 +1601,70 @@ j2k_dec_mstabent_t j2k_dec_mstab[] = {
   {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
 };
 
-j2k_dec_mstabent_t *j2k_dec_mstab_lookup(int id)
+j2k_dec_mstabent_t *
+j2k_dec_mstab_lookup (int id)
 {
   j2k_dec_mstabent_t *e;
-  for (e = j2k_dec_mstab; e->id != 0; e++) {
-    if (e->id == id) {
-      break;
+  for (e = j2k_dec_mstab; e->id != 0; e++)
+    {
+      if (e->id == id)
+       {
+         break;
+       }
     }
-  }
   return e;
 }
 
 
 
-LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t * img,
-
-                         j2k_cp_t * cp)
+LIBJ2K_API int
+j2k_decode (unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp)
 {
 
-  if (setjmp(j2k_error)) {
-    if (j2k_state != J2K_STATE_MT) {
-      fprintf(stderr, "WARNING: incomplete bitstream\n");
-      return 0;
+  if (setjmp (j2k_error))
+    {
+      if (j2k_state != J2K_STATE_MT)
+       {
+         fprintf (stderr, "WARNING: incomplete bitstream\n");
+         return 0;
+       }
+      j2k_clean ();
+      return cio_numbytes ();  /* Correct way of ending j2k_decode */
     }
-    j2k_clean();
-    return cio_numbytes();    /* Correct way of ending j2k_decode */
-  }
 
   j2k_img = img;
 
   j2k_cp = cp;
 
   j2k_state = J2K_STATE_MHSOC;
-  cio_init(src, len);
-
-  for (;;) {
-    j2k_dec_mstabent_t *e;
-    int id = cio_read(2);
-    if (id >> 8 != 0xff) {
-      fprintf(stderr, "%.8x: expected a marker instead of %x\n",
-             cio_tell() - 2, id);
-      return 0;
-    }
-    e = j2k_dec_mstab_lookup(id);
-    if (!(j2k_state & e->states)) {
-      fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id);
-      return 0;
-    }
-    if (e->handler) {
-      (*e->handler) ();
+  cio_init (src, len);
+
+  for (;;)
+    {
+      j2k_dec_mstabent_t *e;
+      int id = cio_read (2);
+      if (id >> 8 != 0xff)
+       {
+         fprintf (stderr, "%.8x: expected a marker instead of %x\n",
+                  cio_tell () - 2, id);
+         return 0;
+       }
+      e = j2k_dec_mstab_lookup (id);
+      if (!(j2k_state & e->states))
+       {
+         fprintf (stderr, "%.8x: unexpected marker %x\n", cio_tell () - 2,
+                  id);
+         return 0;
+       }
+      if (e->handler)
+       {
+         (*e->handler) ();
+       }
+      if (j2k_state == J2K_STATE_NEOC)
+       break;                  /* RAJOUTE */
     }
-    if (j2k_state == J2K_STATE_NEOC)
-      break;                   /* RAJOUTE */
-  }
   if (j2k_state == J2K_STATE_NEOC)
-    j2k_read_eoc();            /* RAJOUTE */
+    j2k_read_eoc ();           /* RAJOUTE */
 
   return 0;
 }
@@ -1485,77 +1673,89 @@ LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t * img,
  * Read a JPT-stream and decode file
  *
  */
-int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t * img,
-                         j2k_cp_t * cp)
+int
+j2k_decode_jpt_stream (unsigned char *src, int len, j2k_image_t * img,
+                      j2k_cp_t * cp)
 {
   jpt_msg_header_struct_t header;
   int position;
 
-  if (setjmp(j2k_error)) {
-    if (j2k_state != J2K_STATE_MT) {
-      fprintf(stderr, "WARNING: incomplete bitstream\n");
-      return 0;
+  if (setjmp (j2k_error))
+    {
+      if (j2k_state != J2K_STATE_MT)
+       {
+         fprintf (stderr, "WARNING: incomplete bitstream\n");
+         return 0;
+       }
+      return cio_numbytes ();
     }
-    return cio_numbytes();
-  }
 
   j2k_img = img;
 
   j2k_cp = cp;
 
   j2k_state = J2K_STATE_MHSOC;
-  cio_init(src, len);
+  cio_init (src, len);
 
   /* Initialize the header */
-  jpt_init_Msg_Header(&header);
+  jpt_init_Msg_Header (&header);
   /* Read the first header of the message */
-  jpt_read_Msg_Header(&header);
-
-  position = cio_tell();
-  if (header.Class_Id != 6) {  /* 6 : Main header data-bin message */
-    fprintf(stderr,
-           "[JPT-stream] : Expecting Main header first [class_Id %d] !\n",
-           header.Class_Id);
-    return 0;
-  }
-
-  for (;;) {
-    j2k_dec_mstabent_t *e;
-    int id;
-
-    if (!cio_numbytesleft()) {
-      j2k_read_eoc();
+  jpt_read_Msg_Header (&header);
+
+  position = cio_tell ();
+  if (header.Class_Id != 6)
+    {                          /* 6 : Main header data-bin message */
+      fprintf (stderr,
+              "[JPT-stream] : Expecting Main header first [class_Id %d] !\n",
+              header.Class_Id);
       return 0;
     }
-    /* data-bin read -> need to read a new header */
-    if ((unsigned int) (cio_tell() - position) == header.Msg_length) {
-      jpt_read_Msg_Header(&header);
-      position = cio_tell();
-      if (header.Class_Id != 4) {      /* 4 : Tile data-bin message */
-       fprintf(stderr, "[JPT-stream] : Expecting Tile info !\n");
-       return 0;
-      }
-    }
 
-    id = cio_read(2);
-    if (id >> 8 != 0xff) {
-      fprintf(stderr, "%.8x: expected a marker instead of %x\n",
-             cio_tell() - 2, id);
-      return 0;
-    }
-    e = j2k_dec_mstab_lookup(id);
-    if (!(j2k_state & e->states)) {
-      fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id);
-      return 0;
-    }
-    if (e->handler) {
-      (*e->handler) ();
+  for (;;)
+    {
+      j2k_dec_mstabent_t *e;
+      int id;
+
+      if (!cio_numbytesleft ())
+       {
+         j2k_read_eoc ();
+         return 0;
+       }
+      /* data-bin read -> need to read a new header */
+      if ((unsigned int) (cio_tell () - position) == header.Msg_length)
+       {
+         jpt_read_Msg_Header (&header);
+         position = cio_tell ();
+         if (header.Class_Id != 4)
+           {                   /* 4 : Tile data-bin message */
+             fprintf (stderr, "[JPT-stream] : Expecting Tile info !\n");
+             return 0;
+           }
+       }
+
+      id = cio_read (2);
+      if (id >> 8 != 0xff)
+       {
+         fprintf (stderr, "%.8x: expected a marker instead of %x\n",
+                  cio_tell () - 2, id);
+         return 0;
+       }
+      e = j2k_dec_mstab_lookup (id);
+      if (!(j2k_state & e->states))
+       {
+         fprintf (stderr, "%.8x: unexpected marker %x\n", cio_tell () - 2,
+                  id);
+         return 0;
+       }
+      if (e->handler)
+       {
+         (*e->handler) ();
+       }
+      if (j2k_state == J2K_STATE_NEOC)
+       break;                  /* RAJOUTE */
     }
-    if (j2k_state == J2K_STATE_NEOC)
-      break;                   /* RAJOUTE */
-  }
   if (j2k_state == J2K_STATE_NEOC)
-    j2k_read_eoc();            /* RAJOUTE */
+    j2k_read_eoc ();           /* RAJOUTE */
 
   return 0;
 }
@@ -1563,16 +1763,17 @@ int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t * img,
 #ifdef WIN32
 #include <windows.h>
 
-BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call,
-                     LPVOID lpReserved)
+BOOL APIENTRY
+DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
 {
-  switch (ul_reason_for_call) {
-  case DLL_PROCESS_ATTACH:
-  case DLL_THREAD_ATTACH:
-  case DLL_THREAD_DETACH:
-  case DLL_PROCESS_DETACH:
-    break;
-  }
+  switch (ul_reason_for_call)
+    {
+    case DLL_PROCESS_ATTACH:
+    case DLL_THREAD_ATTACH:
+    case DLL_THREAD_DETACH:
+    case DLL_PROCESS_DETACH:
+      break;
+    }
   return TRUE;
 }
 #endif
index b3b27dd5d7969a1c212d22a4f6c9c04ed014d9ab..356c466575df33fa5bbb564fa26940d9fa9475e8 100644 (file)
@@ -58,527 +58,607 @@ static tcd_cblk_t *cblk;
 
 extern jmp_buf j2k_error;
 
-void tcd_dump(tcd_image_t * img, int curtileno)
+void
+tcd_dump (tcd_image_t * img, int curtileno)
 {
   int tileno, compno, resno, bandno, precno, cblkno;
-  fprintf(stderr, "image {\n"); 
-  fprintf(stderr, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", img->tw, img->th,
-         tcd_img->x0, tcd_img->x1, tcd_img->y0, tcd_img->y1);
-  for (tileno = 0; tileno < img->th*img->tw; tileno++) {
-    tcd_tile_t *tile = &tcd_image.tiles[tileno];
-     fprintf(stderr, "  tile {\n"); 
-     fprintf(stderr, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); 
-    for (compno = 0; compno < tile->numcomps; compno++) {
-      tcd_tilecomp_t *tilec = &tile->comps[compno];
-       fprintf(stderr, "    tilec {\n"); 
-       fprintf(stderr, "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions); 
-      for (resno = 0; resno < tilec->numresolutions; resno++) {
-       tcd_resolution_t *res = &tilec->resolutions[resno];
-        fprintf(stderr, "\n   res {\n"); 
-        fprintf(stderr, "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands); 
-       for (bandno = 0; bandno < res->numbands; bandno++) {
-         tcd_band_t *band = &res->bands[bandno];
-          fprintf(stderr, "        band {\n"); 
-          fprintf(stderr, "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%d, numbps=%d\n", band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); 
-         for (precno = 0; precno < res->pw * res->ph; precno++) {
-           tcd_precinct_t *prec = &band->precincts[precno];
-            fprintf(stderr, "          prec {\n"); 
-            fprintf(stderr, "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch); 
-           for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
-              tcd_cblk_t *cblk=&prec->cblks[cblkno]; 
-              fprintf(stderr, "            cblk {\n"); 
-              fprintf(stderr, "              x0=%d, y0=%d, x1=%d, y1=%d\n", cblk->x0, cblk->y0, cblk->x1, cblk->y1); 
-              fprintf(stderr, "            }\n"); 
+  fprintf (stderr, "image {\n");
+  fprintf (stderr, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", img->tw,
+          img->th, tcd_img->x0, tcd_img->x1, tcd_img->y0, tcd_img->y1);
+  for (tileno = 0; tileno < img->th * img->tw; tileno++)
+    {
+      tcd_tile_t *tile = &tcd_image.tiles[tileno];
+      fprintf (stderr, "  tile {\n");
+      fprintf (stderr, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
+              tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
+      for (compno = 0; compno < tile->numcomps; compno++)
+       {
+         tcd_tilecomp_t *tilec = &tile->comps[compno];
+         fprintf (stderr, "    tilec {\n");
+         fprintf (stderr,
+                  "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
+                  tilec->x0, tilec->y0, tilec->x1, tilec->y1,
+                  tilec->numresolutions);
+         for (resno = 0; resno < tilec->numresolutions; resno++)
+           {
+             tcd_resolution_t *res = &tilec->resolutions[resno];
+             fprintf (stderr, "\n   res {\n");
+             fprintf (stderr,
+                      "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
+                      res->x0, res->y0, res->x1, res->y1, res->pw, res->ph,
+                      res->numbands);
+             for (bandno = 0; bandno < res->numbands; bandno++)
+               {
+                 tcd_band_t *band = &res->bands[bandno];
+                 fprintf (stderr, "        band {\n");
+                 fprintf (stderr,
+                          "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%d, numbps=%d\n",
+                          band->x0, band->y0, band->x1, band->y1,
+                          band->stepsize, band->numbps);
+                 for (precno = 0; precno < res->pw * res->ph; precno++)
+                   {
+                     tcd_precinct_t *prec = &band->precincts[precno];
+                     fprintf (stderr, "          prec {\n");
+                     fprintf (stderr,
+                              "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
+                              prec->x0, prec->y0, prec->x1, prec->y1,
+                              prec->cw, prec->ch);
+                     for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++)
+                       {
+                         tcd_cblk_t *cblk = &prec->cblks[cblkno];
+                         fprintf (stderr, "            cblk {\n");
+                         fprintf (stderr,
+                                  "              x0=%d, y0=%d, x1=%d, y1=%d\n",
+                                  cblk->x0, cblk->y0, cblk->x1, cblk->y1);
+                         fprintf (stderr, "            }\n");
+                       }
+                     fprintf (stderr, "          }\n");
+                   }
+                 fprintf (stderr, "        }\n");
+               }
+             fprintf (stderr, "      }\n");
            }
-            fprintf(stderr, "          }\n"); 
-         }
-          fprintf(stderr, "        }\n"); 
+         fprintf (stderr, "    }\n");
        }
-        fprintf(stderr, "      }\n"); 
-      }
-       fprintf(stderr, "    }\n"); 
+      fprintf (stderr, "  }\n");
     }
-     fprintf(stderr, "  }\n"); 
-  }
-   fprintf(stderr, "}\n"); 
+  fprintf (stderr, "}\n");
 }
 
-void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
+void
+tcd_malloc_encode (j2k_image_t * img, j2k_cp_t * cp, int curtileno)
 {
   int tileno, compno, resno, bandno, precno, cblkno;
   tcd_img = img;
   tcd_cp = cp;
   tcd_image.tw = cp->tw;
   tcd_image.th = cp->th;
-  tcd_image.tiles = (tcd_tile_t *) malloc(sizeof(tcd_tile_t));
-
-  for (tileno = 0; tileno < 1; tileno++) {
-    j2k_tcp_t *tcp = &cp->tcps[curtileno];
-    int j;
-    /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
-    int p = curtileno % cp->tw;        /* si numerotation matricielle .. */
-    int q = curtileno / cp->tw;        /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
-    /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */
-    tile = tcd_image.tiles;
-    /* 4 borders of the tile rescale on the image if necessary */
-    tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
-    tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
-    tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
-    tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
-    tile->numcomps = img->numcomps;
-    /* tile->PPT=img->PPT;  */
-    /* Modification of the RATE >> */
-    for (j = 0; j < tcp->numlayers; j++) {
-      tcp->rates[j] =
-       int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) *
-                   (tile->y1 -
-                    tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 *
-                                                     img->comps[0].dx *
-                                                     img->comps[0].dy));
-      if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
-       tcp->rates[j] = tcp->rates[j - 1] + 20;
-      } else {
-       if (!j && tcp->rates[j] < 30)
-         tcp->rates[j] = 30;
-      }
-    }
-    /* << Modification of the RATE */
-
-    tile->comps =
-      (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
-    for (compno = 0; compno < tile->numcomps; compno++) {
-      j2k_tccp_t *tccp = &tcp->tccps[compno];
-      /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */
-      tilec = &tile->comps[compno];
-      /* border of each tile component (global) */
-      tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
-
-      tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
-      tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
-      tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
-
-      tilec->data =
-       (int *) malloc((tilec->x1 - tilec->x0) *
-                      (tilec->y1 - tilec->y0) * sizeof(int));
-      tilec->numresolutions = tccp->numresolutions;
-
-      tilec->resolutions =
-       (tcd_resolution_t *) malloc(tilec->numresolutions *
-                                   sizeof(tcd_resolution_t));
-
-      for (resno = 0; resno < tilec->numresolutions; resno++) {
-       int pdx, pdy;
-       int levelno = tilec->numresolutions - 1 - resno;
-       int tlprcxstart, tlprcystart, brprcxend, brprcyend;
-       int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
-       int cbgwidthexpn, cbgheightexpn;
-       int cblkwidthexpn, cblkheightexpn;
-       /* tcd_resolution_t *res=&tilec->resolutions[resno]; */
-
-       res = &tilec->resolutions[resno];
-
-       /* border for each resolution level (global) */
-       res->x0 = int_ceildivpow2(tilec->x0, levelno);
-       res->y0 = int_ceildivpow2(tilec->y0, levelno);
-       res->x1 = int_ceildivpow2(tilec->x1, levelno);
-       res->y1 = int_ceildivpow2(tilec->y1, levelno);
-
-       res->numbands = resno == 0 ? 1 : 3;
-       /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
-       if (tccp->csty & J2K_CCP_CSTY_PRT) {
-         pdx = tccp->prcw[resno];
-         pdy = tccp->prch[resno];
-       } else {
-         pdx = 15;
-         pdy = 15;
-       }
-       /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
-       tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
-       tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
-       brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
-       brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
-
-       res->pw = (brprcxend - tlprcxstart) >> pdx;
-       res->ph = (brprcyend - tlprcystart) >> pdy;
-
-       if (resno == 0) {
-         tlcbgxstart = tlprcxstart;
-         tlcbgystart = tlprcystart;
-         brcbgxend = brprcxend;
-         brcbgyend = brprcyend;
-         cbgwidthexpn = pdx;
-         cbgheightexpn = pdy;
-       } else {
-         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
-         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
-         brcbgxend = int_ceildivpow2(brprcxend, 1);
-         brcbgyend = int_ceildivpow2(brprcyend, 1);
-         cbgwidthexpn = pdx - 1;
-         cbgheightexpn = pdy - 1;
+  tcd_image.tiles = (tcd_tile_t *) malloc (sizeof (tcd_tile_t));
+
+  for (tileno = 0; tileno < 1; tileno++)
+    {
+      j2k_tcp_t *tcp = &cp->tcps[curtileno];
+      int j;
+      /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
+      int p = curtileno % cp->tw;      /* si numerotation matricielle .. */
+      int q = curtileno / cp->tw;      /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
+      /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */
+      tile = tcd_image.tiles;
+      /* 4 borders of the tile rescale on the image if necessary */
+      tile->x0 = int_max (cp->tx0 + p * cp->tdx, img->x0);
+      tile->y0 = int_max (cp->ty0 + q * cp->tdy, img->y0);
+      tile->x1 = int_min (cp->tx0 + (p + 1) * cp->tdx, img->x1);
+      tile->y1 = int_min (cp->ty0 + (q + 1) * cp->tdy, img->y1);
+      tile->numcomps = img->numcomps;
+      /* tile->PPT=img->PPT;  */
+      /* Modification of the RATE >> */
+      for (j = 0; j < tcp->numlayers; j++)
+       {
+         tcp->rates[j] = tcp->rates[j] ? int_ceildiv (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0;     //Mod antonin losslessbug
+         if (tcp->rates[j])
+           {
+             if (j && tcp->rates[j] < tcp->rates[j - 1] + 10)
+               {
+                 tcp->rates[j] = tcp->rates[j - 1] + 20;
+               }
+             else
+               {
+                 if (!j && tcp->rates[j] < 30)
+                   tcp->rates[j] = 30;
+               }
+           }
        }
-
-       cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
-       cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
-
-       for (bandno = 0; bandno < res->numbands; bandno++) {
-         int x0b, y0b, i;
-         int gain, numbps;
-         j2k_stepsize_t *ss;
-         band = &res->bands[bandno];
-         band->bandno = resno == 0 ? 0 : bandno + 1;
-         x0b = (band->bandno == 1)
-           || (band->bandno == 3) ? 1 : 0;
-         y0b = (band->bandno == 2)
-           || (band->bandno == 3) ? 1 : 0;
-
-         if (band->bandno == 0) {
-           /* band border (global) */
-           band->x0 = int_ceildivpow2(tilec->x0, levelno);
-           band->y0 = int_ceildivpow2(tilec->y0, levelno);
-           band->x1 = int_ceildivpow2(tilec->x1, levelno);
-           band->y1 = int_ceildivpow2(tilec->y1, levelno);
-         } else {
-           /* band border (global) */
-           band->x0 =
-             int_ceildivpow2(tilec->x0 -
-                             (1 << levelno) * x0b, levelno + 1);
-           band->y0 =
-             int_ceildivpow2(tilec->y0 -
-                             (1 << levelno) * y0b, levelno + 1);
-           band->x1 =
-             int_ceildivpow2(tilec->x1 -
-                             (1 << levelno) * x0b, levelno + 1);
-           band->y1 =
-             int_ceildivpow2(tilec->y1 -
-                             (1 << levelno) * y0b, levelno + 1);
-
-         }
-
-         ss = &tccp->stepsizes[resno ==
-                               0 ? 0 : 3 * (resno - 1) + bandno + 1];
-         gain =
-           tccp->qmfbid ==
-           0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
-         numbps = img->comps[compno].prec + gain;
-         band->stepsize =
-           (int) floor((1.0 + ss->mant / 2048.0) *
-                       pow(2.0, numbps - ss->expn) * 8192.0);
-         band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
-
-         band->precincts =
-           (tcd_precinct_t *) malloc(3 * res->pw * res->ph *
-                                     sizeof(tcd_precinct_t));
-
-         for (i = 0; i < res->pw * res->ph * 3; i++) {
-           band->precincts[i].imsbtree = NULL;
-           band->precincts[i].incltree = NULL;
-         }
-
-         for (precno = 0; precno < res->pw * res->ph; precno++) {
-           int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
-           int cbgxstart =
-             tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
-           int cbgystart =
-             tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
-           int cbgxend = cbgxstart + (1 << cbgwidthexpn);
-           int cbgyend = cbgystart + (1 << cbgheightexpn);
-           /* tcd_precinct_t *prc=&band->precincts[precno]; */
-           prc = &band->precincts[precno];
-           /* precinct size (global) */
-           prc->x0 = int_max(cbgxstart, band->x0);
-           prc->y0 = int_max(cbgystart, band->y0);
-           prc->x1 = int_min(cbgxend, band->x1);
-           prc->y1 = int_min(cbgyend, band->y1);
-
-           tlcblkxstart =
-             int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
-           tlcblkystart =
-             int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
-           brcblkxend =
-             int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
-           brcblkyend =
-             int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
-           prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
-           prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
-
-           prc->cblks =
-             (tcd_cblk_t *) malloc((prc->cw * prc->ch) *
-                                   sizeof(tcd_cblk_t));
-           prc->incltree = tgt_create(prc->cw, prc->ch);
-           prc->imsbtree = tgt_create(prc->cw, prc->ch);
-
-           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-             int cblkxstart =
-               tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
-             int cblkystart =
-               tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
-             int cblkxend = cblkxstart + (1 << cblkwidthexpn);
-             int cblkyend = cblkystart + (1 << cblkheightexpn);
-
-             cblk = &prc->cblks[cblkno];
-             /* code-block size (global) */
-             cblk->x0 = int_max(cblkxstart, prc->x0);
-             cblk->y0 = int_max(cblkystart, prc->y0);
-             cblk->x1 = int_min(cblkxend, prc->x1);
-             cblk->y1 = int_min(cblkyend, prc->y1);
+      /* << Modification of the RATE */
+
+      tile->comps =
+       (tcd_tilecomp_t *) malloc (img->numcomps * sizeof (tcd_tilecomp_t));
+      for (compno = 0; compno < tile->numcomps; compno++)
+       {
+         j2k_tccp_t *tccp = &tcp->tccps[compno];
+         /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */
+         tilec = &tile->comps[compno];
+         /* border of each tile component (global) */
+         tilec->x0 = int_ceildiv (tile->x0, img->comps[compno].dx);
+
+         tilec->y0 = int_ceildiv (tile->y0, img->comps[compno].dy);
+         tilec->x1 = int_ceildiv (tile->x1, img->comps[compno].dx);
+         tilec->y1 = int_ceildiv (tile->y1, img->comps[compno].dy);
+
+         tilec->data =
+           (int *) malloc ((tilec->x1 - tilec->x0) *
+                           (tilec->y1 - tilec->y0) * sizeof (int));
+         tilec->numresolutions = tccp->numresolutions;
+
+         tilec->resolutions =
+           (tcd_resolution_t *) malloc (tilec->numresolutions *
+                                        sizeof (tcd_resolution_t));
+
+         for (resno = 0; resno < tilec->numresolutions; resno++)
+           {
+             int pdx, pdy;
+             int levelno = tilec->numresolutions - 1 - resno;
+             int tlprcxstart, tlprcystart, brprcxend, brprcyend;
+             int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
+             int cbgwidthexpn, cbgheightexpn;
+             int cblkwidthexpn, cblkheightexpn;
+             /* tcd_resolution_t *res=&tilec->resolutions[resno]; */
+
+             res = &tilec->resolutions[resno];
+
+             /* border for each resolution level (global) */
+             res->x0 = int_ceildivpow2 (tilec->x0, levelno);
+             res->y0 = int_ceildivpow2 (tilec->y0, levelno);
+             res->x1 = int_ceildivpow2 (tilec->x1, levelno);
+             res->y1 = int_ceildivpow2 (tilec->y1, levelno);
+
+             res->numbands = resno == 0 ? 1 : 3;
+             /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
+             if (tccp->csty & J2K_CCP_CSTY_PRT)
+               {
+                 pdx = tccp->prcw[resno];
+                 pdy = tccp->prch[resno];
+               }
+             else
+               {
+                 pdx = 15;
+                 pdy = 15;
+               }
+             /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
+             tlprcxstart = int_floordivpow2 (res->x0, pdx) << pdx;
+             tlprcystart = int_floordivpow2 (res->y0, pdy) << pdy;
+             brprcxend = int_ceildivpow2 (res->x1, pdx) << pdx;
+             brprcyend = int_ceildivpow2 (res->y1, pdy) << pdy;
+
+             res->pw = (brprcxend - tlprcxstart) >> pdx;
+             res->ph = (brprcyend - tlprcystart) >> pdy;
+
+             if (resno == 0)
+               {
+                 tlcbgxstart = tlprcxstart;
+                 tlcbgystart = tlprcystart;
+                 brcbgxend = brprcxend;
+                 brcbgyend = brprcyend;
+                 cbgwidthexpn = pdx;
+                 cbgheightexpn = pdy;
+               }
+             else
+               {
+                 tlcbgxstart = int_ceildivpow2 (tlprcxstart, 1);
+                 tlcbgystart = int_ceildivpow2 (tlprcystart, 1);
+                 brcbgxend = int_ceildivpow2 (brprcxend, 1);
+                 brcbgyend = int_ceildivpow2 (brprcyend, 1);
+                 cbgwidthexpn = pdx - 1;
+                 cbgheightexpn = pdy - 1;
+               }
+
+             cblkwidthexpn = int_min (tccp->cblkw, cbgwidthexpn);
+             cblkheightexpn = int_min (tccp->cblkh, cbgheightexpn);
+
+             for (bandno = 0; bandno < res->numbands; bandno++)
+               {
+                 int x0b, y0b, i;
+                 int gain, numbps;
+                 j2k_stepsize_t *ss;
+                 band = &res->bands[bandno];
+                 band->bandno = resno == 0 ? 0 : bandno + 1;
+                 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
+                 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
+
+                 if (band->bandno == 0)
+                   {
+                     /* band border (global) */
+                     band->x0 = int_ceildivpow2 (tilec->x0, levelno);
+                     band->y0 = int_ceildivpow2 (tilec->y0, levelno);
+                     band->x1 = int_ceildivpow2 (tilec->x1, levelno);
+                     band->y1 = int_ceildivpow2 (tilec->y1, levelno);
+                   }
+                 else
+                   {
+                     /* band border (global) */
+                     band->x0 =
+                       int_ceildivpow2 (tilec->x0 -
+                                        (1 << levelno) * x0b, levelno + 1);
+                     band->y0 =
+                       int_ceildivpow2 (tilec->y0 -
+                                        (1 << levelno) * y0b, levelno + 1);
+                     band->x1 =
+                       int_ceildivpow2 (tilec->x1 -
+                                        (1 << levelno) * x0b, levelno + 1);
+                     band->y1 =
+                       int_ceildivpow2 (tilec->y1 -
+                                        (1 << levelno) * y0b, levelno + 1);
+
+                   }
+
+                 ss = &tccp->stepsizes[resno ==
+                                       0 ? 0 : 3 * (resno - 1) + bandno + 1];
+                 gain =
+                   tccp->qmfbid ==
+                   0 ? dwt_getgain_real (band->bandno) : dwt_getgain (band->
+                                                                      bandno);
+                 numbps = img->comps[compno].prec + gain;
+                 band->stepsize =
+                   (int) floor ((1.0 + ss->mant / 2048.0) *
+                                pow (2.0, numbps - ss->expn) * 8192.0);
+                 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
+
+                 band->precincts =
+                   (tcd_precinct_t *) malloc (3 * res->pw * res->ph *
+                                              sizeof (tcd_precinct_t));
+
+                 for (i = 0; i < res->pw * res->ph * 3; i++)
+                   {
+                     band->precincts[i].imsbtree = NULL;
+                     band->precincts[i].incltree = NULL;
+                   }
+
+                 for (precno = 0; precno < res->pw * res->ph; precno++)
+                   {
+                     int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
+                     int cbgxstart =
+                       tlcbgxstart +
+                       (precno % res->pw) * (1 << cbgwidthexpn);
+                     int cbgystart =
+                       tlcbgystart +
+                       (precno / res->pw) * (1 << cbgheightexpn);
+                     int cbgxend = cbgxstart + (1 << cbgwidthexpn);
+                     int cbgyend = cbgystart + (1 << cbgheightexpn);
+                     /* tcd_precinct_t *prc=&band->precincts[precno]; */
+                     prc = &band->precincts[precno];
+                     /* precinct size (global) */
+                     prc->x0 = int_max (cbgxstart, band->x0);
+                     prc->y0 = int_max (cbgystart, band->y0);
+                     prc->x1 = int_min (cbgxend, band->x1);
+                     prc->y1 = int_min (cbgyend, band->y1);
+
+                     tlcblkxstart =
+                       int_floordivpow2 (prc->x0,
+                                         cblkwidthexpn) << cblkwidthexpn;
+                     tlcblkystart =
+                       int_floordivpow2 (prc->y0,
+                                         cblkheightexpn) << cblkheightexpn;
+                     brcblkxend =
+                       int_ceildivpow2 (prc->x1,
+                                        cblkwidthexpn) << cblkwidthexpn;
+                     brcblkyend =
+                       int_ceildivpow2 (prc->y1,
+                                        cblkheightexpn) << cblkheightexpn;
+                     prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
+                     prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
+
+                     prc->cblks =
+                       (tcd_cblk_t *) malloc ((prc->cw * prc->ch) *
+                                              sizeof (tcd_cblk_t));
+                     prc->incltree = tgt_create (prc->cw, prc->ch);
+                     prc->imsbtree = tgt_create (prc->cw, prc->ch);
+
+                     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
+                       {
+                         int cblkxstart =
+                           tlcblkxstart +
+                           (cblkno % prc->cw) * (1 << cblkwidthexpn);
+                         int cblkystart =
+                           tlcblkystart +
+                           (cblkno / prc->cw) * (1 << cblkheightexpn);
+                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
+                         int cblkyend = cblkystart + (1 << cblkheightexpn);
+
+                         cblk = &prc->cblks[cblkno];
+                         /* code-block size (global) */
+                         cblk->x0 = int_max (cblkxstart, prc->x0);
+                         cblk->y0 = int_max (cblkystart, prc->y0);
+                         cblk->x1 = int_min (cblkxend, prc->x1);
+                         cblk->y1 = int_min (cblkyend, prc->y1);
+                       }
+                   }
+               }
            }
-         }
        }
-      }
     }
-  }
   /* tcd_dump(&tcd_image,curtileno); */
 }
 
-void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
+void
+tcd_free_encode (j2k_image_t * img, j2k_cp_t * cp, int curtileno)
 {
   int tileno, compno, resno, bandno, precno;
   tcd_img = img;
   tcd_cp = cp;
   tcd_image.tw = cp->tw;
   tcd_image.th = cp->th;
-  for (tileno = 0; tileno < 1; tileno++) {
-    /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
-    tile = tcd_image.tiles;
-    for (compno = 0; compno < tile->numcomps; compno++) {
-      tilec = &tile->comps[compno];
-      for (resno = 0; resno < tilec->numresolutions; resno++) {
-       res = &tilec->resolutions[resno];
-       for (bandno = 0; bandno < res->numbands; bandno++) {
-         band = &res->bands[bandno];
-         for (precno = 0; precno < res->pw * res->ph; precno++) {
-           prc = &band->precincts[precno];
-
-           if (prc->incltree != NULL)
-             tgt_destroy(prc->incltree);
-           if (prc->imsbtree != NULL)
-             tgt_destroy(prc->imsbtree);
-           free(prc->cblks);
-         }                     /* for (precno */
-         free(band->precincts);
-       }                       /* for (bandno */
-      }                                /* for (resno */
-      free(tilec->resolutions);
-    }                          /* for (compno */
-    free(tile->comps);
-  }                            /* for (tileno */
-  free(tcd_image.tiles);
+  for (tileno = 0; tileno < 1; tileno++)
+    {
+      /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
+      tile = tcd_image.tiles;
+      for (compno = 0; compno < tile->numcomps; compno++)
+       {
+         tilec = &tile->comps[compno];
+         for (resno = 0; resno < tilec->numresolutions; resno++)
+           {
+             res = &tilec->resolutions[resno];
+             for (bandno = 0; bandno < res->numbands; bandno++)
+               {
+                 band = &res->bands[bandno];
+                 for (precno = 0; precno < res->pw * res->ph; precno++)
+                   {
+                     prc = &band->precincts[precno];
+
+                     if (prc->incltree != NULL)
+                       tgt_destroy (prc->incltree);
+                     if (prc->imsbtree != NULL)
+                       tgt_destroy (prc->imsbtree);
+                     free (prc->cblks);
+                   }           /* for (precno */
+                 free (band->precincts);
+               }               /* for (bandno */
+           }                   /* for (resno */
+         free (tilec->resolutions);
+       }                       /* for (compno */
+      free (tile->comps);
+    }                          /* for (tileno */
+  free (tcd_image.tiles);
 }
 
-void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
+void
+tcd_init_encode (j2k_image_t * img, j2k_cp_t * cp, int curtileno)
 {
   int tileno, compno, resno, bandno, precno, cblkno;
 
-  for (tileno = 0; tileno < 1; tileno++) {
-    j2k_tcp_t *tcp = &cp->tcps[curtileno];
-    int j;
-    //              int previous_x0, previous_x1, previous_y0, previous_y1;
-    /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
-    int p = curtileno % cp->tw;
-    int q = curtileno / cp->tw;
-    tile = tcd_image.tiles;
-
-    /* 4 borders of the tile rescale on the image if necessary */
-    tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
-    tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
-    tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
-    tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
-
-    tile->numcomps = img->numcomps;
-    /* tile->PPT=img->PPT; */
-    /* Modification of the RATE >> */
-    for (j = 0; j < tcp->numlayers; j++) {
-      tcp->rates[j] =
-       int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) *
-                   (tile->y1 -
-                    tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 *
-                                                     img->comps[0].dx *
-                                                     img->comps[0].dy));
-      if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
-       tcp->rates[j] = tcp->rates[j - 1] + 20;
-      } else {
-       if (!j && tcp->rates[j] < 30)
-         tcp->rates[j] = 30;
-      }
-    }
-    /* << Modification of the RATE */
-    /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
-    for (compno = 0; compno < tile->numcomps; compno++) {
-      j2k_tccp_t *tccp = &tcp->tccps[compno];
-      /* int realloc_op; */
-
-      tilec = &tile->comps[compno];
-      /* border of each tile component (global) */
-      tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
-      tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
-      tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
-      tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
-
-      tilec->data =
-       (int *) malloc((tilec->x1 - tilec->x0) *
-                      (tilec->y1 - tilec->y0) * sizeof(int));
-      tilec->numresolutions = tccp->numresolutions;
-      /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
-      for (resno = 0; resno < tilec->numresolutions; resno++) {
-       int pdx, pdy;
-       int levelno = tilec->numresolutions - 1 - resno;
-       int tlprcxstart, tlprcystart, brprcxend, brprcyend;
-       int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
-       int cbgwidthexpn, cbgheightexpn;
-       int cblkwidthexpn, cblkheightexpn;
-
-       res = &tilec->resolutions[resno];
-       /* border for each resolution level (global) */
-       res->x0 = int_ceildivpow2(tilec->x0, levelno);
-       res->y0 = int_ceildivpow2(tilec->y0, levelno);
-       res->x1 = int_ceildivpow2(tilec->x1, levelno);
-       res->y1 = int_ceildivpow2(tilec->y1, levelno);
-
-       res->numbands = resno == 0 ? 1 : 3;
-       /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
-       if (tccp->csty & J2K_CCP_CSTY_PRT) {
-         pdx = tccp->prcw[resno];
-         pdy = tccp->prch[resno];
-       } else {
-         pdx = 15;
-         pdy = 15;
-       }
-       /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
-       tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
-       tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
-       brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
-       brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
-
-       res->pw = (brprcxend - tlprcxstart) >> pdx;
-       res->ph = (brprcyend - tlprcystart) >> pdy;
-
-       if (resno == 0) {
-         tlcbgxstart = tlprcxstart;
-         tlcbgystart = tlprcystart;
-         brcbgxend = brprcxend;
-         brcbgyend = brprcyend;
-         cbgwidthexpn = pdx;
-         cbgheightexpn = pdy;
-       } else {
-         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
-         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
-         brcbgxend = int_ceildivpow2(brprcxend, 1);
-         brcbgyend = int_ceildivpow2(brprcyend, 1);
-         cbgwidthexpn = pdx - 1;
-         cbgheightexpn = pdy - 1;
+  for (tileno = 0; tileno < 1; tileno++)
+    {
+      j2k_tcp_t *tcp = &cp->tcps[curtileno];
+      int j;
+      //              int previous_x0, previous_x1, previous_y0, previous_y1;
+      /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
+      int p = curtileno % cp->tw;
+      int q = curtileno / cp->tw;
+      tile = tcd_image.tiles;
+
+      /* 4 borders of the tile rescale on the image if necessary */
+      tile->x0 = int_max (cp->tx0 + p * cp->tdx, img->x0);
+      tile->y0 = int_max (cp->ty0 + q * cp->tdy, img->y0);
+      tile->x1 = int_min (cp->tx0 + (p + 1) * cp->tdx, img->x1);
+      tile->y1 = int_min (cp->ty0 + (q + 1) * cp->tdy, img->y1);
+
+      tile->numcomps = img->numcomps;
+      /* tile->PPT=img->PPT; */
+      /* Modification of the RATE >> */
+      for (j = 0; j < tcp->numlayers; j++)
+       {
+         tcp->rates[j] = tcp->rates[j] ? int_ceildiv (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0;     //Mod antonin losslessbug
+         if (tcp->rates[j])
+           {
+             if (j && tcp->rates[j] < tcp->rates[j - 1] + 10)
+               {
+                 tcp->rates[j] = tcp->rates[j - 1] + 20;
+               }
+             else
+               {
+                 if (!j && tcp->rates[j] < 30)
+                   tcp->rates[j] = 30;
+               }
+           }
        }
-
-       cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
-       cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
-
-       for (bandno = 0; bandno < res->numbands; bandno++) {
-         int x0b, y0b;
-         int gain, numbps;
-         j2k_stepsize_t *ss;
-         band = &res->bands[bandno];
-         band->bandno = resno == 0 ? 0 : bandno + 1;
-         x0b = (band->bandno == 1)
-           || (band->bandno == 3) ? 1 : 0;
-         y0b = (band->bandno == 2)
-           || (band->bandno == 3) ? 1 : 0;
-
-         if (band->bandno == 0) {
-           /* band border */
-           band->x0 = int_ceildivpow2(tilec->x0, levelno);
-           band->y0 = int_ceildivpow2(tilec->y0, levelno);
-           band->x1 = int_ceildivpow2(tilec->x1, levelno);
-           band->y1 = int_ceildivpow2(tilec->y1, levelno);
-         } else {
-           band->x0 =
-             int_ceildivpow2(tilec->x0 -
-                             (1 << levelno) * x0b, levelno + 1);
-           band->y0 =
-             int_ceildivpow2(tilec->y0 -
-                             (1 << levelno) * y0b, levelno + 1);
-           band->x1 =
-             int_ceildivpow2(tilec->x1 -
-                             (1 << levelno) * x0b, levelno + 1);
-           band->y1 =
-             int_ceildivpow2(tilec->y1 -
-                             (1 << levelno) * y0b, levelno + 1);
-         }
-
-         ss = &tccp->stepsizes[resno ==
-                               0 ? 0 : 3 * (resno - 1) + bandno + 1];
-         gain =
-           tccp->qmfbid ==
-           0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
-         numbps = img->comps[compno].prec + gain;
-         band->stepsize =
-           (int) floor((1.0 + ss->mant / 2048.0) *
-                       pow(2.0, numbps - ss->expn) * 8192.0);
-         band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
-
-         for (precno = 0; precno < res->pw * res->ph; precno++) {
-           int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
-           int cbgxstart =
-             tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
-           int cbgystart =
-             tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
-           int cbgxend = cbgxstart + (1 << cbgwidthexpn);
-           int cbgyend = cbgystart + (1 << cbgheightexpn);
-
-           prc = &band->precincts[precno];
-           /* precinct size (global) */
-           prc->x0 = int_max(cbgxstart, band->x0);
-           prc->y0 = int_max(cbgystart, band->y0);
-           prc->x1 = int_min(cbgxend, band->x1);
-           prc->y1 = int_min(cbgyend, band->y1);
-
-           tlcblkxstart =
-             int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
-           tlcblkystart =
-             int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
-           brcblkxend =
-             int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
-           brcblkyend =
-             int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
-           prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
-           prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
-
-           free(prc->cblks);
-           prc->cblks =
-             (tcd_cblk_t *) malloc(prc->cw * prc->ch *
-                                   sizeof(tcd_cblk_t));
-
-           if (prc->incltree != NULL)
-             tgt_destroy(prc->incltree);
-           if (prc->imsbtree != NULL)
-             tgt_destroy(prc->imsbtree);
-
-           prc->incltree = tgt_create(prc->cw, prc->ch);
-           prc->imsbtree = tgt_create(prc->cw, prc->ch);
-
-           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-             int cblkxstart =
-               tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
-             int cblkystart =
-               tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
-             int cblkxend = cblkxstart + (1 << cblkwidthexpn);
-             int cblkyend = cblkystart + (1 << cblkheightexpn);
-             cblk = &prc->cblks[cblkno];
-
-             /* code-block size (global) */
-             cblk->x0 = int_max(cblkxstart, prc->x0);
-             cblk->y0 = int_max(cblkystart, prc->y0);
-             cblk->x1 = int_min(cblkxend, prc->x1);
-             cblk->y1 = int_min(cblkyend, prc->y1);
-
+      /* << Modification of the RATE */
+      /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
+      for (compno = 0; compno < tile->numcomps; compno++)
+       {
+         j2k_tccp_t *tccp = &tcp->tccps[compno];
+         /* int realloc_op; */
+
+         tilec = &tile->comps[compno];
+         /* border of each tile component (global) */
+         tilec->x0 = int_ceildiv (tile->x0, img->comps[compno].dx);
+         tilec->y0 = int_ceildiv (tile->y0, img->comps[compno].dy);
+         tilec->x1 = int_ceildiv (tile->x1, img->comps[compno].dx);
+         tilec->y1 = int_ceildiv (tile->y1, img->comps[compno].dy);
+
+         tilec->data =
+           (int *) malloc ((tilec->x1 - tilec->x0) *
+                           (tilec->y1 - tilec->y0) * sizeof (int));
+         tilec->numresolutions = tccp->numresolutions;
+         /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
+         for (resno = 0; resno < tilec->numresolutions; resno++)
+           {
+             int pdx, pdy;
+             int levelno = tilec->numresolutions - 1 - resno;
+             int tlprcxstart, tlprcystart, brprcxend, brprcyend;
+             int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
+             int cbgwidthexpn, cbgheightexpn;
+             int cblkwidthexpn, cblkheightexpn;
+
+             res = &tilec->resolutions[resno];
+             /* border for each resolution level (global) */
+             res->x0 = int_ceildivpow2 (tilec->x0, levelno);
+             res->y0 = int_ceildivpow2 (tilec->y0, levelno);
+             res->x1 = int_ceildivpow2 (tilec->x1, levelno);
+             res->y1 = int_ceildivpow2 (tilec->y1, levelno);
+
+             res->numbands = resno == 0 ? 1 : 3;
+             /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
+             if (tccp->csty & J2K_CCP_CSTY_PRT)
+               {
+                 pdx = tccp->prcw[resno];
+                 pdy = tccp->prch[resno];
+               }
+             else
+               {
+                 pdx = 15;
+                 pdy = 15;
+               }
+             /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
+             tlprcxstart = int_floordivpow2 (res->x0, pdx) << pdx;
+             tlprcystart = int_floordivpow2 (res->y0, pdy) << pdy;
+             brprcxend = int_ceildivpow2 (res->x1, pdx) << pdx;
+             brprcyend = int_ceildivpow2 (res->y1, pdy) << pdy;
+
+             res->pw = (brprcxend - tlprcxstart) >> pdx;
+             res->ph = (brprcyend - tlprcystart) >> pdy;
+
+             if (resno == 0)
+               {
+                 tlcbgxstart = tlprcxstart;
+                 tlcbgystart = tlprcystart;
+                 brcbgxend = brprcxend;
+                 brcbgyend = brprcyend;
+                 cbgwidthexpn = pdx;
+                 cbgheightexpn = pdy;
+               }
+             else
+               {
+                 tlcbgxstart = int_ceildivpow2 (tlprcxstart, 1);
+                 tlcbgystart = int_ceildivpow2 (tlprcystart, 1);
+                 brcbgxend = int_ceildivpow2 (brprcxend, 1);
+                 brcbgyend = int_ceildivpow2 (brprcyend, 1);
+                 cbgwidthexpn = pdx - 1;
+                 cbgheightexpn = pdy - 1;
+               }
+
+             cblkwidthexpn = int_min (tccp->cblkw, cbgwidthexpn);
+             cblkheightexpn = int_min (tccp->cblkh, cbgheightexpn);
+
+             for (bandno = 0; bandno < res->numbands; bandno++)
+               {
+                 int x0b, y0b;
+                 int gain, numbps;
+                 j2k_stepsize_t *ss;
+                 band = &res->bands[bandno];
+                 band->bandno = resno == 0 ? 0 : bandno + 1;
+                 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
+                 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
+
+                 if (band->bandno == 0)
+                   {
+                     /* band border */
+                     band->x0 = int_ceildivpow2 (tilec->x0, levelno);
+                     band->y0 = int_ceildivpow2 (tilec->y0, levelno);
+                     band->x1 = int_ceildivpow2 (tilec->x1, levelno);
+                     band->y1 = int_ceildivpow2 (tilec->y1, levelno);
+                   }
+                 else
+                   {
+                     band->x0 =
+                       int_ceildivpow2 (tilec->x0 -
+                                        (1 << levelno) * x0b, levelno + 1);
+                     band->y0 =
+                       int_ceildivpow2 (tilec->y0 -
+                                        (1 << levelno) * y0b, levelno + 1);
+                     band->x1 =
+                       int_ceildivpow2 (tilec->x1 -
+                                        (1 << levelno) * x0b, levelno + 1);
+                     band->y1 =
+                       int_ceildivpow2 (tilec->y1 -
+                                        (1 << levelno) * y0b, levelno + 1);
+                   }
+
+                 ss = &tccp->stepsizes[resno ==
+                                       0 ? 0 : 3 * (resno - 1) + bandno + 1];
+                 gain =
+                   tccp->qmfbid ==
+                   0 ? dwt_getgain_real (band->bandno) : dwt_getgain (band->
+                                                                      bandno);
+                 numbps = img->comps[compno].prec + gain;
+                 band->stepsize =
+                   (int) floor ((1.0 + ss->mant / 2048.0) *
+                                pow (2.0, numbps - ss->expn) * 8192.0);
+                 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
+
+                 for (precno = 0; precno < res->pw * res->ph; precno++)
+                   {
+                     int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
+                     int cbgxstart =
+                       tlcbgxstart +
+                       (precno % res->pw) * (1 << cbgwidthexpn);
+                     int cbgystart =
+                       tlcbgystart +
+                       (precno / res->pw) * (1 << cbgheightexpn);
+                     int cbgxend = cbgxstart + (1 << cbgwidthexpn);
+                     int cbgyend = cbgystart + (1 << cbgheightexpn);
+
+                     prc = &band->precincts[precno];
+                     /* precinct size (global) */
+                     prc->x0 = int_max (cbgxstart, band->x0);
+                     prc->y0 = int_max (cbgystart, band->y0);
+                     prc->x1 = int_min (cbgxend, band->x1);
+                     prc->y1 = int_min (cbgyend, band->y1);
+
+                     tlcblkxstart =
+                       int_floordivpow2 (prc->x0,
+                                         cblkwidthexpn) << cblkwidthexpn;
+                     tlcblkystart =
+                       int_floordivpow2 (prc->y0,
+                                         cblkheightexpn) << cblkheightexpn;
+                     brcblkxend =
+                       int_ceildivpow2 (prc->x1,
+                                        cblkwidthexpn) << cblkwidthexpn;
+                     brcblkyend =
+                       int_ceildivpow2 (prc->y1,
+                                        cblkheightexpn) << cblkheightexpn;
+                     prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
+                     prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
+
+                     free (prc->cblks);
+                     prc->cblks =
+                       (tcd_cblk_t *) malloc (prc->cw * prc->ch *
+                                              sizeof (tcd_cblk_t));
+
+                     if (prc->incltree != NULL)
+                       tgt_destroy (prc->incltree);
+                     if (prc->imsbtree != NULL)
+                       tgt_destroy (prc->imsbtree);
+
+                     prc->incltree = tgt_create (prc->cw, prc->ch);
+                     prc->imsbtree = tgt_create (prc->cw, prc->ch);
+
+                     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
+                       {
+                         int cblkxstart =
+                           tlcblkxstart +
+                           (cblkno % prc->cw) * (1 << cblkwidthexpn);
+                         int cblkystart =
+                           tlcblkystart +
+                           (cblkno / prc->cw) * (1 << cblkheightexpn);
+                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
+                         int cblkyend = cblkystart + (1 << cblkheightexpn);
+                         cblk = &prc->cblks[cblkno];
+
+                         /* code-block size (global) */
+                         cblk->x0 = int_max (cblkxstart, prc->x0);
+                         cblk->y0 = int_max (cblkystart, prc->y0);
+                         cblk->x1 = int_min (cblkxend, prc->x1);
+                         cblk->y1 = int_min (cblkyend, prc->y1);
+
+                       }
+                   }
+               }
            }
-         }
        }
-      }
     }
-  }
   /* tcd_dump(&tcd_image,0); */
 }
 
-void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
+void
+tcd_init (j2k_image_t * img, j2k_cp_t * cp)
 {
   int tileno, compno, resno, bandno, precno, cblkno, i, j;
   unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h, p, q;
@@ -587,241 +667,282 @@ void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
   tcd_image.tw = cp->tw;
   tcd_image.th = cp->th;
   tcd_image.tiles =
-    (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
+    (tcd_tile_t *) malloc (cp->tw * cp->th * sizeof (tcd_tile_t));
 
   /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
      j2k_tcp_t *tcp = &cp->tcps[tileno];
      tcd_tile_t *tile = &tcd_image.tiles[tileno]; */
 
-  for (i = 0; i < cp->tileno_size; i++) {
-    j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
-    tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
-    tileno = cp->tileno[i];
-
-
-    //              int previous_x0, previous_x1, previous_y0, previous_y1;
-    /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
-    p = tileno % cp->tw;       /* si numerotation matricielle .. */
-    q = tileno / cp->tw;       /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
-
-    /* 4 borders of the tile rescale on the image if necessary */
-    tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
-    tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
-    tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
-    tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
-
-    tile->numcomps = img->numcomps;
-    tile->comps =
-      (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
-    for (compno = 0; compno < tile->numcomps; compno++) {
-      j2k_tccp_t *tccp = &tcp->tccps[compno];
-      tcd_tilecomp_t *tilec = &tile->comps[compno];
-      /* border of each tile component (global) */
-      tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
-      tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
-      tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
-      tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
-
-      tilec->data =
-       (int *) malloc((tilec->x1 - tilec->x0) *
-                      (tilec->y1 - tilec->y0) * sizeof(int));
-      tilec->numresolutions = tccp->numresolutions;
-      tilec->resolutions =
-       (tcd_resolution_t *) malloc(tilec->numresolutions *
-                                   sizeof(tcd_resolution_t));
-      for (resno = 0; resno < tilec->numresolutions; resno++) {
-       int pdx, pdy;
-       int levelno = tilec->numresolutions - 1 - resno;
-       int tlprcxstart, tlprcystart, brprcxend, brprcyend;
-       int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
-       int cbgwidthexpn, cbgheightexpn;
-       int cblkwidthexpn, cblkheightexpn;
-       tcd_resolution_t *res = &tilec->resolutions[resno];
-
-       /* border for each resolution level (global) */
-       res->x0 = int_ceildivpow2(tilec->x0, levelno);
-       res->y0 = int_ceildivpow2(tilec->y0, levelno);
-       res->x1 = int_ceildivpow2(tilec->x1, levelno);
-       res->y1 = int_ceildivpow2(tilec->y1, levelno);
-
-       res->numbands = resno == 0 ? 1 : 3;
-       /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
-       if (tccp->csty & J2K_CCP_CSTY_PRT) {
-         pdx = tccp->prcw[resno];
-         pdy = tccp->prch[resno];
-       } else {
-         pdx = 15;
-         pdy = 15;
-       }
-       /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
-       tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
-       tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
-       brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
-       brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
-       res->pw = (res->x0==res->x1)?0:((brprcxend - tlprcxstart) >> pdx); // Mod Antonin : sizebug1
-       res->ph = (res->y0==res->y1)?0:((brprcyend - tlprcystart) >> pdy); // Mod Antonin : sizebug1
-
-       if (resno == 0) {
-         tlcbgxstart = tlprcxstart;
-         tlcbgystart = tlprcystart;
-         brcbgxend = brprcxend;
-         brcbgyend = brprcyend;
-         cbgwidthexpn = pdx;
-         cbgheightexpn = pdy;
-       } else {
-         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
-         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
-         brcbgxend = int_ceildivpow2(brprcxend, 1);
-         brcbgyend = int_ceildivpow2(brprcyend, 1);
-         cbgwidthexpn = pdx - 1;
-         cbgheightexpn = pdy - 1;
-       }
-
-       cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
-       cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
-
-       for (bandno = 0; bandno < res->numbands; bandno++) {
-         int x0b, y0b;
-         int gain, numbps;
-         j2k_stepsize_t *ss;
-         tcd_band_t *band = &res->bands[bandno];
-         band->bandno = resno == 0 ? 0 : bandno + 1;
-         x0b = (band->bandno == 1)
-           || (band->bandno == 3) ? 1 : 0;
-         y0b = (band->bandno == 2)
-           || (band->bandno == 3) ? 1 : 0;
-
-         if (band->bandno == 0) {
-           /* band border (global) */
-           band->x0 = int_ceildivpow2(tilec->x0, levelno);
-           band->y0 = int_ceildivpow2(tilec->y0, levelno);
-           band->x1 = int_ceildivpow2(tilec->x1, levelno);
-           band->y1 = int_ceildivpow2(tilec->y1, levelno);
-         } else {
-           /* band border (global) */
-           band->x0 =
-             int_ceildivpow2(tilec->x0 -
-                             (1 << levelno) * x0b, levelno + 1);
-           band->y0 =
-             int_ceildivpow2(tilec->y0 -
-                             (1 << levelno) * y0b, levelno + 1);
-           band->x1 =
-             int_ceildivpow2(tilec->x1 -
-                             (1 << levelno) * x0b, levelno + 1);
-           band->y1 =
-             int_ceildivpow2(tilec->y1 -
-                             (1 << levelno) * y0b, levelno + 1);
-         }
-
-         ss = &tccp->stepsizes[resno ==
-                               0 ? 0 : 3 * (resno - 1) + bandno + 1];
-         gain =
-           tccp->qmfbid ==
-           0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
-         numbps = img->comps[compno].prec + gain;
-         band->stepsize =
-           (int) floor((1.0 + ss->mant / 2048.0) *
-                       pow(2.0, numbps - ss->expn) * 8192.0);
-         band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
-
-         band->precincts =
-           (tcd_precinct_t *) malloc(res->pw * res->ph *
-                                     sizeof(tcd_precinct_t));
-
-         for (precno = 0; precno < res->pw * res->ph; precno++) {
-           int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
-           int cbgxstart =
-             tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
-           int cbgystart =
-             tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
-           int cbgxend = cbgxstart + (1 << cbgwidthexpn);
-           int cbgyend = cbgystart + (1 << cbgheightexpn);
-           tcd_precinct_t *prc = &band->precincts[precno];
-           /* precinct size (global) */
-           prc->x0 = int_max(cbgxstart, band->x0);
-           prc->y0 = int_max(cbgystart, band->y0);
-           prc->x1 = int_min(cbgxend, band->x1);
-           prc->y1 = int_min(cbgyend, band->y1);
-
-           tlcblkxstart =
-             int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
-           tlcblkystart =
-             int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
-           brcblkxend =
-             int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
-           brcblkyend =
-             int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
-           prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
-           prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
-
-           prc->cblks =
-             (tcd_cblk_t *) malloc(prc->cw * prc->ch *
-                                   sizeof(tcd_cblk_t));
-
-           prc->incltree = tgt_create(prc->cw, prc->ch);
-           prc->imsbtree = tgt_create(prc->cw, prc->ch);
-
-           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-             int cblkxstart =
-               tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
-             int cblkystart =
-               tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
-             int cblkxend = cblkxstart + (1 << cblkwidthexpn);
-             int cblkyend = cblkystart + (1 << cblkheightexpn);
-             tcd_cblk_t *cblk = &prc->cblks[cblkno];
-             /* code-block size (global) */
-             cblk->x0 = int_max(cblkxstart, prc->x0);
-             cblk->y0 = int_max(cblkystart, prc->y0);
-             cblk->x1 = int_min(cblkxend, prc->x1);
-             cblk->y1 = int_min(cblkyend, prc->y1);
-
-
-
-             cblk->lastbp = 0; // Add Antonin : quantizbug1
+  for (i = 0; i < cp->tileno_size; i++)
+    {
+      j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
+      tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
+      tileno = cp->tileno[i];
+
+
+      //              int previous_x0, previous_x1, previous_y0, previous_y1;
+      /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
+      p = tileno % cp->tw;     /* si numerotation matricielle .. */
+      q = tileno / cp->tw;     /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
+
+      /* 4 borders of the tile rescale on the image if necessary */
+      tile->x0 = int_max (cp->tx0 + p * cp->tdx, img->x0);
+      tile->y0 = int_max (cp->ty0 + q * cp->tdy, img->y0);
+      tile->x1 = int_min (cp->tx0 + (p + 1) * cp->tdx, img->x1);
+      tile->y1 = int_min (cp->ty0 + (q + 1) * cp->tdy, img->y1);
+
+      tile->numcomps = img->numcomps;
+      tile->comps =
+       (tcd_tilecomp_t *) malloc (img->numcomps * sizeof (tcd_tilecomp_t));
+      for (compno = 0; compno < tile->numcomps; compno++)
+       {
+         j2k_tccp_t *tccp = &tcp->tccps[compno];
+         tcd_tilecomp_t *tilec = &tile->comps[compno];
+         /* border of each tile component (global) */
+         tilec->x0 = int_ceildiv (tile->x0, img->comps[compno].dx);
+         tilec->y0 = int_ceildiv (tile->y0, img->comps[compno].dy);
+         tilec->x1 = int_ceildiv (tile->x1, img->comps[compno].dx);
+         tilec->y1 = int_ceildiv (tile->y1, img->comps[compno].dy);
+
+         tilec->data =
+           (int *) malloc ((tilec->x1 - tilec->x0) *
+                           (tilec->y1 - tilec->y0) * sizeof (int));
+         tilec->numresolutions = tccp->numresolutions;
+         tilec->resolutions =
+           (tcd_resolution_t *) malloc (tilec->numresolutions *
+                                        sizeof (tcd_resolution_t));
+         for (resno = 0; resno < tilec->numresolutions; resno++)
+           {
+             int pdx, pdy;
+             int levelno = tilec->numresolutions - 1 - resno;
+             int tlprcxstart, tlprcystart, brprcxend, brprcyend;
+             int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
+             int cbgwidthexpn, cbgheightexpn;
+             int cblkwidthexpn, cblkheightexpn;
+             tcd_resolution_t *res = &tilec->resolutions[resno];
+
+             /* border for each resolution level (global) */
+             res->x0 = int_ceildivpow2 (tilec->x0, levelno);
+             res->y0 = int_ceildivpow2 (tilec->y0, levelno);
+             res->x1 = int_ceildivpow2 (tilec->x1, levelno);
+             res->y1 = int_ceildivpow2 (tilec->y1, levelno);
+
+             res->numbands = resno == 0 ? 1 : 3;
+             /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
+             if (tccp->csty & J2K_CCP_CSTY_PRT)
+               {
+                 pdx = tccp->prcw[resno];
+                 pdy = tccp->prch[resno];
+               }
+             else
+               {
+                 pdx = 15;
+                 pdy = 15;
+               }
+             /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
+             tlprcxstart = int_floordivpow2 (res->x0, pdx) << pdx;
+             tlprcystart = int_floordivpow2 (res->y0, pdy) << pdy;
+             brprcxend = int_ceildivpow2 (res->x1, pdx) << pdx;
+             brprcyend = int_ceildivpow2 (res->y1, pdy) << pdy;
+             res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);  // Mod Antonin : sizebug1
+             res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);  // Mod Antonin : sizebug1
+
+             if (resno == 0)
+               {
+                 tlcbgxstart = tlprcxstart;
+                 tlcbgystart = tlprcystart;
+                 brcbgxend = brprcxend;
+                 brcbgyend = brprcyend;
+                 cbgwidthexpn = pdx;
+                 cbgheightexpn = pdy;
+               }
+             else
+               {
+                 tlcbgxstart = int_ceildivpow2 (tlprcxstart, 1);
+                 tlcbgystart = int_ceildivpow2 (tlprcystart, 1);
+                 brcbgxend = int_ceildivpow2 (brprcxend, 1);
+                 brcbgyend = int_ceildivpow2 (brprcyend, 1);
+                 cbgwidthexpn = pdx - 1;
+                 cbgheightexpn = pdy - 1;
+               }
+
+             cblkwidthexpn = int_min (tccp->cblkw, cbgwidthexpn);
+             cblkheightexpn = int_min (tccp->cblkh, cbgheightexpn);
+
+             for (bandno = 0; bandno < res->numbands; bandno++)
+               {
+                 int x0b, y0b;
+                 int gain, numbps;
+                 j2k_stepsize_t *ss;
+                 tcd_band_t *band = &res->bands[bandno];
+                 band->bandno = resno == 0 ? 0 : bandno + 1;
+                 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
+                 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
+
+                 if (band->bandno == 0)
+                   {
+                     /* band border (global) */
+                     band->x0 = int_ceildivpow2 (tilec->x0, levelno);
+                     band->y0 = int_ceildivpow2 (tilec->y0, levelno);
+                     band->x1 = int_ceildivpow2 (tilec->x1, levelno);
+                     band->y1 = int_ceildivpow2 (tilec->y1, levelno);
+                   }
+                 else
+                   {
+                     /* band border (global) */
+                     band->x0 =
+                       int_ceildivpow2 (tilec->x0 -
+                                        (1 << levelno) * x0b, levelno + 1);
+                     band->y0 =
+                       int_ceildivpow2 (tilec->y0 -
+                                        (1 << levelno) * y0b, levelno + 1);
+                     band->x1 =
+                       int_ceildivpow2 (tilec->x1 -
+                                        (1 << levelno) * x0b, levelno + 1);
+                     band->y1 =
+                       int_ceildivpow2 (tilec->y1 -
+                                        (1 << levelno) * y0b, levelno + 1);
+                   }
+
+                 ss = &tccp->stepsizes[resno ==
+                                       0 ? 0 : 3 * (resno - 1) + bandno + 1];
+                 gain =
+                   tccp->qmfbid ==
+                   0 ? dwt_getgain_real (band->bandno) : dwt_getgain (band->
+                                                                      bandno);
+                 numbps = img->comps[compno].prec + gain;
+                 band->stepsize =
+                   (int) floor ((1.0 + ss->mant / 2048.0) *
+                                pow (2.0, numbps - ss->expn) * 8192.0);
+                 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
+
+                 band->precincts =
+                   (tcd_precinct_t *) malloc (res->pw * res->ph *
+                                              sizeof (tcd_precinct_t));
+
+                 for (precno = 0; precno < res->pw * res->ph; precno++)
+                   {
+                     int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
+                     int cbgxstart =
+                       tlcbgxstart +
+                       (precno % res->pw) * (1 << cbgwidthexpn);
+                     int cbgystart =
+                       tlcbgystart +
+                       (precno / res->pw) * (1 << cbgheightexpn);
+                     int cbgxend = cbgxstart + (1 << cbgwidthexpn);
+                     int cbgyend = cbgystart + (1 << cbgheightexpn);
+                     tcd_precinct_t *prc = &band->precincts[precno];
+                     /* precinct size (global) */
+                     prc->x0 = int_max (cbgxstart, band->x0);
+                     prc->y0 = int_max (cbgystart, band->y0);
+                     prc->x1 = int_min (cbgxend, band->x1);
+                     prc->y1 = int_min (cbgyend, band->y1);
+
+                     tlcblkxstart =
+                       int_floordivpow2 (prc->x0,
+                                         cblkwidthexpn) << cblkwidthexpn;
+                     tlcblkystart =
+                       int_floordivpow2 (prc->y0,
+                                         cblkheightexpn) << cblkheightexpn;
+                     brcblkxend =
+                       int_ceildivpow2 (prc->x1,
+                                        cblkwidthexpn) << cblkwidthexpn;
+                     brcblkyend =
+                       int_ceildivpow2 (prc->y1,
+                                        cblkheightexpn) << cblkheightexpn;
+                     prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
+                     prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
+
+                     prc->cblks =
+                       (tcd_cblk_t *) malloc (prc->cw * prc->ch *
+                                              sizeof (tcd_cblk_t));
+
+                     prc->incltree = tgt_create (prc->cw, prc->ch);
+                     prc->imsbtree = tgt_create (prc->cw, prc->ch);
+
+                     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
+                       {
+                         int cblkxstart =
+                           tlcblkxstart +
+                           (cblkno % prc->cw) * (1 << cblkwidthexpn);
+                         int cblkystart =
+                           tlcblkystart +
+                           (cblkno / prc->cw) * (1 << cblkheightexpn);
+                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
+                         int cblkyend = cblkystart + (1 << cblkheightexpn);
+                         tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                         /* code-block size (global) */
+                         cblk->x0 = int_max (cblkxstart, prc->x0);
+                         cblk->y0 = int_max (cblkystart, prc->y0);
+                         cblk->x1 = int_min (cblkxend, prc->x1);
+                         cblk->y1 = int_min (cblkyend, prc->y1);
+
+
+
+                         cblk->lastbp = 0;     // Add Antonin : quantizbug1
+                       }
+                   }
+               }
            }
-         }
        }
-      }
     }
-  }
   //tcd_dump(&tcd_image,0);
 
 
   /* Allocate place to store the data decoded = final image */
   /* Place limited by the tile really present in the codestream */
 
-  
-  for (i = 0; i < img->numcomps; i++) {
-    for (j = 0; j < cp->tileno_size; j++) {
-      tileno = cp->tileno[j];
-      x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min(x0,
-                                                        tcd_image.
-                                                        tiles[tileno].comps[i].x0);
-      y0 = j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min(y0,
-                                                        tcd_image.
-                                                        tiles[tileno].comps[i].y0);
-      x1 = j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max(x1,
-                                                        tcd_image.
-                                                        tiles[tileno].comps[i].x1);
-      y1 = j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max(y1,
-                                                        tcd_image.
-                                                        tiles[tileno].comps[i].y1);
+
+  for (i = 0; i < img->numcomps; i++)
+    {
+      for (j = 0; j < cp->tileno_size; j++)
+       {
+         tileno = cp->tileno[j];
+         x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min (x0,
+                                                                      tcd_image.
+                                                                      tiles
+                                                                      [tileno].
+                                                                      comps
+                                                                      [i].
+                                                                      x0);
+         y0 =
+           j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min (y0,
+                                                                   tcd_image.
+                                                                   tiles
+                                                                   [tileno].
+                                                                   comps[i].
+                                                                   y0);
+         x1 =
+           j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max (x1,
+                                                                   tcd_image.
+                                                                   tiles
+                                                                   [tileno].
+                                                                   comps[i].
+                                                                   x1);
+         y1 =
+           j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max (y1,
+                                                                   tcd_image.
+                                                                   tiles
+                                                                   [tileno].
+                                                                   comps[i].
+                                                                   y1);
+       }
+      //w = int_ceildiv(x1 - x0, img->comps[i].dx);
+      //h = int_ceildiv(y1 - y0, img->comps[i].dy);
+
+      w = x1 - x0;
+
+      h = y1 - y0;
+      img->comps[i].data = (int *) calloc (w * h, sizeof (int));
+      img->comps[i].w = w;
+      img->comps[i].h = h;
+      img->comps[i].x0 = x0;
+      img->comps[i].y0 = y0;
     }
-    //w = int_ceildiv(x1 - x0, img->comps[i].dx);
-    //h = int_ceildiv(y1 - y0, img->comps[i].dy);
-
-    w = x1 - x0;
-
-    h = y1 - y0;
-    img->comps[i].data = (int *) calloc(w * h, sizeof(int));
-    img->comps[i].w = w;
-    img->comps[i].h = h;
-    img->comps[i].x0 = x0;
-    img->comps[i].y0 = y0;
-  }
 }
 
-void tcd_makelayer_fixed(int layno, int final)
+void
+tcd_makelayer_fixed (int layno, int final)
 {
   int compno, resno, bandno, precno, cblkno;
   int value;                   //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
@@ -830,173 +951,213 @@ void tcd_makelayer_fixed(int layno, int final)
 
   /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
 
-  for (compno = 0; compno < tcd_tile->numcomps; compno++) {
-    tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
-    for (i = 0; i < tcd_tcp->numlayers; i++) {
-      for (j = 0; j < tilec->numresolutions; j++) {
-       for (k = 0; k < 3; k++) {
-         matrice[i][j][k] =
-           (int) (tcd_cp->
-                  matrice[i * tilec->numresolutions * 3 +
-                          j * 3 +
-                          k] *
-                  (float) (tcd_img->comps[compno].prec / 16.0));
-    }}}
-
-    for (resno = 0; resno < tilec->numresolutions; resno++) {
-      tcd_resolution_t *res = &tilec->resolutions[resno];
-      for (bandno = 0; bandno < res->numbands; bandno++) {
-       tcd_band_t *band = &res->bands[bandno];
-       for (precno = 0; precno < res->pw * res->ph; precno++) {
-         tcd_precinct_t *prc = &band->precincts[precno];
-         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-           tcd_cblk_t *cblk = &prc->cblks[cblkno];
-           tcd_layer_t *layer = &cblk->layers[layno];
-           int n;
-           int imsb = tcd_img->comps[compno].prec - cblk->numbps;      /* number of bit-plan equal to zero */
-           /* Correction of the matrix of coefficient to include the IMSB information */
-
-           if (layno == 0) {
-             value = matrice[layno][resno][bandno];
-             if (imsb >= value)
-               value = 0;
-             else
-               value -= imsb;
-           } else {
-             value =
-               matrice[layno][resno][bandno] -
-               matrice[layno - 1][resno][bandno];
-             if (imsb >= matrice[layno - 1][resno][bandno]) {
-               value -= (imsb - matrice[layno - 1][resno][bandno]);
-               if (value < 0)
-                 value = 0;
-             }
+  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;
+                   }
+               }
            }
-
-           if (layno == 0)
-             cblk->numpassesinlayers = 0;
-
-           n = cblk->numpassesinlayers;
-           if (cblk->numpassesinlayers == 0) {
-             if (value != 0)
-               n = 3 * value - 2 + cblk->numpassesinlayers;
-             else
-               n = cblk->numpassesinlayers;
-           } else
-             n = 3 * value + cblk->numpassesinlayers;
-
-           layer->numpasses = n - cblk->numpassesinlayers;
-
-           if (!layer->numpasses)
-             continue;
-
-           if (cblk->numpassesinlayers == 0) {
-             layer->len = cblk->passes[n - 1].rate;
-             layer->data = cblk->data;
-           } else {
-             layer->len =
-               cblk->passes[n - 1].rate -
-               cblk->passes[cblk->numpassesinlayers - 1].rate;
-             layer->data =
-               cblk->data +
-               cblk->passes[cblk->numpassesinlayers - 1].rate;
-           }
-           if (final)
-             cblk->numpassesinlayers = n;
-         }
        }
-      }
     }
-  }
 }
 
-void tcd_rateallocate_fixed()
+void
+tcd_rateallocate_fixed ()
 {
   int layno;
 
-  for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
-    tcd_makelayer_fixed(layno, 1);
-  }
+  for (layno = 0; layno < tcd_tcp->numlayers; layno++)
+    {
+      tcd_makelayer_fixed (layno, 1);
+    }
 }
 
-void tcd_makelayer(int layno, double thresh, int final)
+void
+tcd_makelayer (int layno, double thresh, int final)
 {
   int compno, resno, bandno, precno, cblkno, passno;
 
   tcd_tile->distolayer[layno] = 0;     //add fixed_quality
 
-  for (compno = 0; compno < tcd_tile->numcomps; compno++) {
-    tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
-    for (resno = 0; resno < tilec->numresolutions; resno++) {
-      tcd_resolution_t *res = &tilec->resolutions[resno];
-      for (bandno = 0; bandno < res->numbands; bandno++) {
-       tcd_band_t *band = &res->bands[bandno];
-       for (precno = 0; precno < res->pw * res->ph; precno++) {
-         tcd_precinct_t *prc = &band->precincts[precno];
-         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-           tcd_cblk_t *cblk = &prc->cblks[cblkno];
-           tcd_layer_t *layer = &cblk->layers[layno];
-           int n;
-
-           if (layno == 0) {
-             cblk->numpassesinlayers = 0;
-           }
-           n = cblk->numpassesinlayers;
-           for (passno = cblk->numpassesinlayers;
-                passno < cblk->totalpasses; passno++) {
-             int dr;
-             double dd;
-             tcd_pass_t *pass = &cblk->passes[passno];
-             if (n == 0) {
-               dr = pass->rate;
-               dd = pass->distortiondec;
-             } else {
-               dr = pass->rate - cblk->passes[n - 1].rate;
-               dd = pass->distortiondec - cblk->passes[n -
-                                                       1].distortiondec;
-             }
-             if (dr == 0) {
-               if (dd != 0)
-                 n = passno + 1;
-               continue;
-             }
-             if (dd / dr > thresh)
-               n = passno + 1;
+  for (compno = 0; compno < tcd_tile->numcomps; compno++)
+    {
+      tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
+      for (resno = 0; resno < tilec->numresolutions; resno++)
+       {
+         tcd_resolution_t *res = &tilec->resolutions[resno];
+         for (bandno = 0; bandno < res->numbands; bandno++)
+           {
+             tcd_band_t *band = &res->bands[bandno];
+             for (precno = 0; precno < res->pw * res->ph; precno++)
+               {
+                 tcd_precinct_t *prc = &band->precincts[precno];
+                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
+                   {
+                     tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                     tcd_layer_t *layer = &cblk->layers[layno];
+                     int n;
+
+                     if (layno == 0)
+                       {
+                         cblk->numpassesinlayers = 0;
+                       }
+                     n = cblk->numpassesinlayers;
+                     for (passno = cblk->numpassesinlayers;
+                          passno < cblk->totalpasses; passno++)
+                       {
+                         int dr;
+                         double dd;
+                         tcd_pass_t *pass = &cblk->passes[passno];
+                         if (n == 0)
+                           {
+                             dr = pass->rate;
+                             dd = pass->distortiondec;
+                           }
+                         else
+                           {
+                             dr = pass->rate - cblk->passes[n - 1].rate;
+                             dd = pass->distortiondec - cblk->passes[n -
+                                                                     1].
+                               distortiondec;
+                           }
+                         if (!dr)
+                           {
+                             if (dd)
+                               n = passno + 1;
+                             continue;
+                           }
+
+                         if (dd / dr >= thresh)
+                           n = passno + 1;
+                       }
+                     layer->numpasses = n - cblk->numpassesinlayers;
+
+                     if (!layer->numpasses)
+                       {
+                         layer->disto = 0;
+                         continue;
+                       }
+
+                     if (cblk->numpassesinlayers == 0)
+                       {
+                         layer->len = cblk->passes[n - 1].rate;
+                         layer->data = cblk->data;
+                         layer->disto = cblk->passes[n - 1].distortiondec;
+                       }
+                     else
+                       {
+                         layer->len = cblk->passes[n - 1].rate -
+                           cblk->passes[cblk->numpassesinlayers - 1].rate;
+                         layer->data =
+                           cblk->data +
+                           cblk->passes[cblk->numpassesinlayers - 1].rate;
+                         layer->disto =
+                           cblk->passes[n - 1].distortiondec -
+                           cblk->passes[cblk->numpassesinlayers -
+                                        1].distortiondec;
+                       }
+
+                     tcd_tile->distolayer[layno] += layer->disto;      //add fixed_quality
+
+                     if (final)
+                       cblk->numpassesinlayers = n;
+                   }
+               }
            }
-           layer->numpasses = n - cblk->numpassesinlayers;
-
-           if (!layer->numpasses) {
-             layer->disto = 0;
-             continue;
-           }
-
-           if (cblk->numpassesinlayers == 0) {
-             layer->len = cblk->passes[n - 1].rate;
-             layer->data = cblk->data;
-             layer->disto = cblk->passes[n - 1].distortiondec;
-           } else {
-             layer->len = cblk->passes[n - 1].rate -
-               cblk->passes[cblk->numpassesinlayers - 1].rate;
-             layer->data =
-               cblk->data +
-               cblk->passes[cblk->numpassesinlayers - 1].rate;
-             layer->disto =
-               cblk->passes[n - 1].distortiondec -
-               cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
-           }
-
-           tcd_tile->distolayer[layno] += layer->disto;        //add fixed_quality
-
-           if (final)
-             cblk->numpassesinlayers = n;
-         }
        }
-      }
     }
-  }
 }
 
-void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
+void
+tcd_rateallocate (unsigned char *dest, int len, info_image * info_IM)
 {
   int compno, resno, bandno, precno, cblkno, passno, layno;
   double min, max;
@@ -1009,135 +1170,162 @@ void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
 
   tcd_tile->nbpix = 0;         //add fixed_quality
 
-  for (compno = 0; compno < tcd_tile->numcomps; compno++) {
-    tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
-
-    tilec->nbpix = 0;
-    for (resno = 0; resno < tilec->numresolutions; resno++) {
-      tcd_resolution_t *res = &tilec->resolutions[resno];
-      for (bandno = 0; bandno < res->numbands; bandno++) {
-       tcd_band_t *band = &res->bands[bandno];
-       for (precno = 0; precno < res->pw * res->ph; precno++) {
-         tcd_precinct_t *prc = &band->precincts[precno];
-         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
-           tcd_cblk_t *cblk = &prc->cblks[cblkno];
-           for (passno = 0; passno < cblk->totalpasses; passno++) {
-             tcd_pass_t *pass = &cblk->passes[passno];
-             int dr;
-             double dd, rdslope;
-             if (passno == 0) {
-               dr = pass->rate;
-               dd = pass->distortiondec;
-             } else {
-               dr = pass->rate - cblk->passes[passno - 1].rate;
-               dd = pass->distortiondec -
-                 cblk->passes[passno - 1].distortiondec;
-             }
-             if (dr == 0) {
-               continue;
-             }
-             rdslope = dd / dr;
-             if (rdslope < min) {
-               min = rdslope;
-             }
-             if (rdslope > max) {
-               max = rdslope;
-             }
-           }                   /* passno */
-
-           tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
-
-           tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));    //add fixed_quality
-
-         }                     /* cbklno */
-       }                       /* precno */
-      }                                /* bandno */
-    }                          /* resno */
-
-    maxSE+=(double)(((1<<tcd_img->comps[compno].prec)-1)*((1<<tcd_img->comps[compno].prec)-1))*(tilec->nbpix);
-  }                            /* compno */
+  for (compno = 0; compno < tcd_tile->numcomps; compno++)
+    {
+      tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
+
+      tilec->nbpix = 0;
+      for (resno = 0; resno < tilec->numresolutions; resno++)
+       {
+         tcd_resolution_t *res = &tilec->resolutions[resno];
+         for (bandno = 0; bandno < res->numbands; bandno++)
+           {
+             tcd_band_t *band = &res->bands[bandno];
+             for (precno = 0; precno < res->pw * res->ph; precno++)
+               {
+                 tcd_precinct_t *prc = &band->precincts[precno];
+                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
+                   {
+                     tcd_cblk_t *cblk = &prc->cblks[cblkno];
+                     for (passno = 0; passno < cblk->totalpasses; passno++)
+                       {
+                         tcd_pass_t *pass = &cblk->passes[passno];
+                         int dr;
+                         double dd, rdslope;
+                         if (passno == 0)
+                           {
+                             dr = pass->rate;
+                             dd = pass->distortiondec;
+                           }
+                         else
+                           {
+                             dr = pass->rate - cblk->passes[passno - 1].rate;
+                             dd = pass->distortiondec -
+                               cblk->passes[passno - 1].distortiondec;
+                           }
+                         if (dr == 0)
+                           {
+                             continue;
+                           }
+
+                         rdslope = dd / dr;
+
+                         if (rdslope < min)
+                           {
+                             min = rdslope;
+                           }
+                         if (rdslope > max)
+                           {
+                             max = rdslope;
+                           }
+                       }       /* passno */
+
+                     tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));       //add fixed_quality
+
+                     tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));  //add fixed_quality
+
+                   }           /* cbklno */
+               }               /* precno */
+           }                   /* bandno */
+       }                       /* resno */
+
+      maxSE +=
+       (double) (((1 << tcd_img->comps[compno].prec) -
+                  1) * ((1 << tcd_img->comps[compno].prec) -
+                        1)) * (tilec->nbpix);
+    }                          /* compno */
 
   /* add antonin index */
-  if (info_IM->index_on) {
-    info_tile *info_TL = &info_IM->tile[tcd_tileno];
-    info_TL->nbpix = tcd_tile->nbpix;
-    info_TL->distotile = tcd_tile->distotile;
-    info_TL->thresh =
-      (double *) malloc(tcd_tcp->numlayers * sizeof(double));
-  }
+  if (info_IM->index_on)
+    {
+      info_tile *info_TL = &info_IM->tile[tcd_tileno];
+      info_TL->nbpix = tcd_tile->nbpix;
+      info_TL->distotile = tcd_tile->distotile;
+      info_TL->thresh =
+       (double *) malloc (tcd_tcp->numlayers * sizeof (double));
+    }
   /* dda */
 
-  for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
-    volatile double lo = min;
-    volatile double hi = max;
-    volatile int success = 0;
-    volatile int maxlen = int_min(tcd_tcp->rates[layno], len);
-    volatile double goodthresh;
-    volatile int goodlen;
-    volatile int i;
-    double distotarget;                //add fixed_quality
-
-    distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10));      // add fixed_quality
-
-    for (i = 0; i < 32; i++) {
-      volatile double thresh = (lo + hi) / 2;
-      int l=0;
-      double distoachieved = 0;        // add fixed_quality
-
-      tcd_makelayer(layno, thresh, 0);
-
-      if (tcd_cp->fixed_quality) {     // add fixed_quality
-       distoachieved =
-         layno ==
-         0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
-         tcd_tile->distolayer[layno];
-       if (distoachieved < distotarget) {
-         hi = thresh;
-         continue;
+  for (layno = 0; layno < tcd_tcp->numlayers; layno++)
+    {
+      volatile double lo = min;
+      volatile double hi = max;
+      volatile int success = 0;
+      volatile int maxlen = tcd_tcp->rates[layno] ? int_min (tcd_tcp->rates[layno], len) : len;        //Mod antonin losslessbug
+      volatile double goodthresh;
+      volatile int i;
+      double distotarget;      //add fixed_quality
+
+      distotarget = tcd_tile->distotile - ((K * maxSE) / pow (10, tcd_tcp->distoratio[layno] / 10));   // add fixed_quality
+
+      if (tcd_tcp->rates[layno])
+       {
+         for (i = 0; i < 32; i++)
+           {
+             volatile double thresh = (lo + hi) / 2;
+             int l = 0;
+             double distoachieved = 0; // add fixed_quality
+
+             tcd_makelayer (layno, thresh, 0);
+
+             if (tcd_cp->fixed_quality)
+               {               // add fixed_quality
+                 distoachieved =
+                   layno ==
+                   0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
+                   tcd_tile->distolayer[layno];
+                 if (distoachieved < distotarget)
+                   {
+                     hi = thresh;
+                     continue;
+                   }
+                 lo = thresh;
+               }
+             else
+               {
+                 l =
+                   t2_encode_packets (tcd_img, tcd_cp, tcd_tileno, tcd_tile,
+                                      layno + 1, dest, maxlen, info_IM);
+                 /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
+                 if (l == -999)
+                   {
+                     lo = thresh;
+                     continue;
+                   }
+                 hi = thresh;
+               }
+
+             success = 1;
+             goodthresh = thresh;
+           }
        }
-       lo = thresh;
-      } else {
-       l =
-         t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile,
-                           layno + 1, dest, maxlen, info_IM);
-       /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
-       if (l == -999) {
-         lo = thresh;
-         continue;
+      else
+       {
+         success = 1;
+         goodthresh = min;
        }
-       hi = thresh;
-      }
-
-      success = 1;
-      goodthresh = thresh;
-      goodlen = l;
 
-    }
+      if (!success)
+       {
+         longjmp (j2k_error, 1);
+       }
 
-    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);
 
-    if (info_IM->index_on) {   /* Threshold for Marcela Index */
-      info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;
+      cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];      // add fixed_quality
     }
-    tcd_makelayer(layno, goodthresh, 1);
-
-    cumdisto[layno] =
-
-      layno ==
-
-      0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
-
-      tcd_tile->distolayer[layno]; // add fixed_quality
-  }
 }
 
-int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
-                       info_image * info_IM)
+int
+tcd_encode_tile_pxm (int tileno, unsigned char *dest, int len,
+                    info_image * info_IM)
 {
   int compno;
-  int l,i;
+  int l, i;
   clock_t time7;
   tcd_tile_t *tile;
   j2k_tcp_t *tcp = &tcd_cp->tcps[0];
@@ -1148,148 +1336,168 @@ int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
   tcd_tcp = &tcd_cp->tcps[tileno];
   tile = tcd_tile;
   /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
-  if (info_IM->index_on) {
-    tcd_tilecomp_t *tilec_idx = &tile->comps[0];  //Based on Component 0
+  if (info_IM->index_on)
+    {
+      tcd_tilecomp_t *tilec_idx = &tile->comps[0];     //Based on Component 0
 
-    for (i=0;i<tilec_idx->numresolutions;i++) {
+      for (i = 0; i < tilec_idx->numresolutions; i++)
+       {
 
-      tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
+         tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
 
-      info_IM->tile[tileno].pw[i] = res_idx->pw;
-      info_IM->tile[tileno].ph[i] = res_idx->ph;
+         info_IM->tile[tileno].pw[i] = res_idx->pw;
+         info_IM->tile[tileno].ph[i] = res_idx->ph;
 
-      info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
-      info_IM->tile[tileno].pdy[i] = tccp->prch[i];
+         info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
+         info_IM->tile[tileno].pdy[i] = tccp->prch[i];
 
+       }
     }
-  }
   /* << INDEX */
 
 /*---------------TILE-------------------*/
 
-  time7 = clock();
-
-  for (compno = 0; compno < tile->numcomps; compno++) {
-    FILE *src;
-    char tmp[256];
-    int k;
-    unsigned char elmt;
-    int i, j;
-    int tw, w;
-    tcd_tilecomp_t *tilec = &tile->comps[compno];
-    int adjust =
-      tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
-                                             prec - 1);
-    int offset_x, offset_y;
-
-    offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
-    offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
-    tw = tilec->x1 - tilec->x0;
-    w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
-    sprintf(tmp, "Compo%d", compno);   /* component file */
-    src = fopen(tmp, "rb");
-    if (!src) {
-      fprintf(stderr, "failed to open %s for reading\n", tmp);
-      return 1;
-    }
+  time7 = clock ();
 
-    /* read the Compo file to extract data of the tile */
-    k = 0;
-    fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
-         SEEK_SET);
-    k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
-    for (j = tilec->y0; j < tilec->y1; j++) {
-      for (i = tilec->x0; i < tilec->x1; i++) {
-       if (tcd_tcp->tccps[compno].qmfbid == 1) {
-         elmt = fgetc(src);
-         tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
-           elmt - adjust;
-         k++;
-       } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
-         elmt = fgetc(src);
-         tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
-           (elmt - adjust) << 13;
-         k++;
+  for (compno = 0; compno < tile->numcomps; compno++)
+    {
+      FILE *src;
+      char tmp[256];
+      int k;
+      unsigned char elmt;
+      int i, j;
+      int tw, w;
+      tcd_tilecomp_t *tilec = &tile->comps[compno];
+      int adjust =
+       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
+                                               prec - 1);
+      int offset_x, offset_y;
+
+      offset_x = int_ceildiv (tcd_img->x0, tcd_img->comps[compno].dx);
+      offset_y = int_ceildiv (tcd_img->y0, tcd_img->comps[compno].dy);
+      tw = tilec->x1 - tilec->x0;
+      w = int_ceildiv (tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
+      sprintf (tmp, "Compo%d", compno);        /* component file */
+      src = fopen (tmp, "rb");
+      if (!src)
+       {
+         fprintf (stderr, "failed to open %s for reading\n", tmp);
+         return 1;
        }
-      }
-      fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
-           SEEK_CUR);
-      k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
 
+      /* read the Compo file to extract data of the tile */
+      k = 0;
+      fseek (src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
+            SEEK_SET);
+      k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
+      for (j = tilec->y0; j < tilec->y1; j++)
+       {
+         for (i = tilec->x0; i < tilec->x1; i++)
+           {
+             if (tcd_tcp->tccps[compno].qmfbid == 1)
+               {
+                 elmt = fgetc (src);
+                 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
+                   elmt - adjust;
+                 k++;
+               }
+             else if (tcd_tcp->tccps[compno].qmfbid == 0)
+               {
+                 elmt = fgetc (src);
+                 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
+                   (elmt - adjust) << 13;
+                 k++;
+               }
+           }
+         fseek (src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
+                SEEK_CUR);
+         k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
+
+       }
+      fclose (src);
     }
-    fclose(src);
-  }
 
 /*----------------MCT-------------------*/
 
-  if (tcd_tcp->mct) {
-    if (tcd_tcp->tccps[0].qmfbid == 0) {
-      mct_encode_real(tile->comps[0].data, tile->comps[1].data,
+  if (tcd_tcp->mct)
+    {
+      if (tcd_tcp->tccps[0].qmfbid == 0)
+       {
+         mct_encode_real (tile->comps[0].data, tile->comps[1].data,
+                          tile->comps[2].data,
+                          (tile->comps[0].x1 -
+                           tile->comps[0].x0) * (tile->comps[0].y1 -
+                                                 tile->comps[0].y0));
+       }
+      else
+       {
+         mct_encode (tile->comps[0].data, tile->comps[1].data,
                      tile->comps[2].data,
                      (tile->comps[0].x1 -
                       tile->comps[0].x0) * (tile->comps[0].y1 -
                                             tile->comps[0].y0));
-    } else {
-      mct_encode(tile->comps[0].data, tile->comps[1].data,
-                tile->comps[2].data,
-                (tile->comps[0].x1 -
-                 tile->comps[0].x0) * (tile->comps[0].y1 -
-                                       tile->comps[0].y0));
+       }
     }
-  }
 /*----------------DWT---------------------*/
 
   /* time3=clock(); */
-  for (compno = 0; compno < tile->numcomps; compno++) {
-    tcd_tilecomp_t *tilec = &tile->comps[compno];
-    if (tcd_tcp->tccps[compno].qmfbid == 1) {
-      dwt_encode(tilec->data, tilec->x1 - tilec->x0,
-                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,
+  for (compno = 0; compno < tile->numcomps; compno++)
+    {
+      tcd_tilecomp_t *tilec = &tile->comps[compno];
+      if (tcd_tcp->tccps[compno].qmfbid == 1)
+       {
+         dwt_encode (tilec->data, tilec->x1 - tilec->x0,
                      tilec->y1 - tilec->y0, tilec,
                      tilec->numresolutions - 1);
+       }
+      else if (tcd_tcp->tccps[compno].qmfbid == 0)
+       {
+         dwt_encode_real (tilec->data, tilec->x1 - tilec->x0,
+                          tilec->y1 - tilec->y0, tilec,
+                          tilec->numresolutions - 1);
+       }
     }
-  }
 /*------------------TIER1-----------------*/
 
-  t1_init_luts();
-  t1_encode_cblks(tile, tcd_tcp);
+  t1_init_luts ();
+  t1_encode_cblks (tile, tcd_tcp);
 
 /*-----------RATE-ALLOCATE------------------*/
   info_IM->index_write = 0;    /* INDEX     */
 
   if (tcd_cp->disto_alloc || tcd_cp->fixed_quality)    // mod fixed_quality
     /* Normal Rate/distortion allocation */
-    tcd_rateallocate(dest, len, info_IM);
+    tcd_rateallocate (dest, len, info_IM);
   else
     /* Fixed layer allocation */
-    tcd_rateallocate_fixed();
+    tcd_rateallocate_fixed ();
 
 /*--------------TIER2------------------*/
   info_IM->index_write = 1;    /* INDEX     */
-  l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
-                       tcd_tcp->numlayers, dest, len, info_IM);
+  l = t2_encode_packets (tcd_img, tcd_cp, tileno, tile,
+                        tcd_tcp->numlayers, dest, len, info_IM);
 /*---------------CLEAN-------------------*/
 
-  time7 = clock() - time7;
-  printf("total:     %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
-        (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
+  time7 = clock () - time7;
+  printf ("total:     %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
+         (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
 
   /* cleaning memory */
-  for (compno = 0; compno < tile->numcomps; compno++) {
-    tilec = &tile->comps[compno];
-    free(tilec->data);
-  }
+  for (compno = 0; compno < tile->numcomps; compno++)
+    {
+      tilec = &tile->comps[compno];
+      free (tilec->data);
+    }
 
   return l;
 }
 
-int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
-                       info_image * info_IM)
+int
+tcd_encode_tile_pgx (int tileno, unsigned char *dest, int len,
+                    info_image * info_IM)
 {
   int compno;
-  int l,i;
+  int l, i;
   clock_t time;
   tcd_tile_t *tile;
   j2k_tcp_t *tcp = &tcd_cp->tcps[0];
@@ -1301,120 +1509,141 @@ int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
   tile = tcd_tile;
   /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
 
-  if (info_IM->index_on) {
+  if (info_IM->index_on)
+    {
 
-    tcd_tilecomp_t *tilec_idx = &tile->comps[0];  //Based on Component 0
+      tcd_tilecomp_t *tilec_idx = &tile->comps[0];     //Based on Component 0
 
-    for (i=0;i<tilec_idx->numresolutions;i++) {
+      for (i = 0; i < tilec_idx->numresolutions; i++)
+       {
 
-      tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
+         tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
 
 
 
-      info_IM->tile[tileno].pw[i] = res_idx->pw;
+         info_IM->tile[tileno].pw[i] = res_idx->pw;
 
-      info_IM->tile[tileno].ph[i] = res_idx->ph;
+         info_IM->tile[tileno].ph[i] = res_idx->ph;
 
 
 
-      info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
+         info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
 
-      info_IM->tile[tileno].pdy[i] = tccp->prch[i];
+         info_IM->tile[tileno].pdy[i] = tccp->prch[i];
 
-    }
+       }
 
-  }
+    }
 
   /* << INDEX */
 /*---------------TILE-------------------*/
-  time = clock();
-
-  for (compno = 0; compno < tile->numcomps; compno++) {
-    FILE *src;
-    char tmp[256];
-    int k;
-    int elmt;
-    int i, j;
-    int tw, w;
-    tcd_tilecomp_t *tilec = &tile->comps[compno];
-    int adjust =
-      tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
-                                             prec - 1);
-    int offset_x, offset_y;
-
-    offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
-    offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
-    tw = tilec->x1 - tilec->x0;
-    w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
-    sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1);       /* bandtile file opening */
-    src = fopen(tmp, "rb");
-    if (!src) {
-      fprintf(stderr, "failed to open %s for reading\n", tmp);
-      return 1;
-    }
-    /* Extract data from bandtile file limited to the current tile */
-    k = 0;
-    while (k < tilec->x0 - offset_x) {
-      k++;
-      fscanf(src, "%d", &elmt);
-    }
-
-    for (j = 0; j < tilec->y1 - tilec->y0; j++) {
-      for (i = tilec->x0; i < tilec->x1; i++) {
-       if (tcd_tcp->tccps[compno].qmfbid == 1) {
-         fscanf(src, "%d", &elmt);
-         tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
-         k++;
-       } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
-         fscanf(src, "%d", &elmt);
-         tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
+  time = clock ();
+
+  for (compno = 0; compno < tile->numcomps; compno++)
+    {
+      FILE *src;
+      char tmp[256];
+      int k;
+      int elmt;
+      int i, j;
+      int tw, w;
+      tcd_tilecomp_t *tilec = &tile->comps[compno];
+      int adjust =
+       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
+                                               prec - 1);
+      int offset_x, offset_y;
+
+      offset_x = int_ceildiv (tcd_img->x0, tcd_img->comps[compno].dx);
+      offset_y = int_ceildiv (tcd_img->y0, tcd_img->comps[compno].dy);
+      tw = tilec->x1 - tilec->x0;
+      w = int_ceildiv (tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
+      sprintf (tmp, "bandtile%d", tileno / tcd_cp->tw + 1);    /* bandtile file opening */
+      src = fopen (tmp, "rb");
+      if (!src)
+       {
+         fprintf (stderr, "failed to open %s for reading\n", tmp);
+         return 1;
+       }
+      /* Extract data from bandtile file limited to the current tile */
+      k = 0;
+      while (k < tilec->x0 - offset_x)
+       {
          k++;
+         fscanf (src, "%d", &elmt);
+       }
+
+      for (j = 0; j < tilec->y1 - tilec->y0; j++)
+       {
+         for (i = tilec->x0; i < tilec->x1; i++)
+           {
+             if (tcd_tcp->tccps[compno].qmfbid == 1)
+               {
+                 fscanf (src, "%d", &elmt);
+                 tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
+                 k++;
+               }
+             else if (tcd_tcp->tccps[compno].qmfbid == 0)
+               {
+                 fscanf (src, "%d", &elmt);
+                 tilec->data[i - tilec->x0 + (j) * tw] =
+                   (elmt - adjust) << 13;
+                 k++;
+               }
+           }
+         while (k < tilec->x0 - offset_x + (j + 1) * w)
+           {
+             k++;
+             fscanf (src, "%d", &elmt);
+           }
        }
-      }
-      while (k < tilec->x0 - offset_x + (j + 1) * w) {
-       k++;
-       fscanf(src, "%d", &elmt);
-      }
+      fclose (src);
     }
-    fclose(src);
-  }
 
 /*----------------MCT-------------------*/
 
-  if (tcd_tcp->mct) {
-    if (tcd_tcp->tccps[0].qmfbid == 0) {
-      mct_encode_real(tile->comps[0].data, tile->comps[1].data,
+  if (tcd_tcp->mct)
+    {
+      if (tcd_tcp->tccps[0].qmfbid == 0)
+       {
+         mct_encode_real (tile->comps[0].data, tile->comps[1].data,
+                          tile->comps[2].data,
+                          (tile->comps[0].x1 -
+                           tile->comps[0].x0) * (tile->comps[0].y1 -
+                                                 tile->comps[0].y0));
+       }
+      else
+       {
+         mct_encode (tile->comps[0].data, tile->comps[1].data,
                      tile->comps[2].data,
                      (tile->comps[0].x1 -
                       tile->comps[0].x0) * (tile->comps[0].y1 -
                                             tile->comps[0].y0));
-    } else {
-      mct_encode(tile->comps[0].data, tile->comps[1].data,
-                tile->comps[2].data,
-                (tile->comps[0].x1 -
-                 tile->comps[0].x0) * (tile->comps[0].y1 -
-                                       tile->comps[0].y0));
+       }
     }
-  }
 
 /*----------------DWT---------------------*/
 
-  for (compno = 0; compno < tile->numcomps; compno++) {
-    tcd_tilecomp_t *tilec = &tile->comps[compno];
-    if (tcd_tcp->tccps[compno].qmfbid == 1) {
-      dwt_encode(tilec->data, tilec->x1 - tilec->x0,
-                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,
+  for (compno = 0; compno < tile->numcomps; compno++)
+    {
+      tcd_tilecomp_t *tilec = &tile->comps[compno];
+      if (tcd_tcp->tccps[compno].qmfbid == 1)
+       {
+         dwt_encode (tilec->data, tilec->x1 - tilec->x0,
                      tilec->y1 - tilec->y0, tilec,
                      tilec->numresolutions - 1);
+       }
+      else if (tcd_tcp->tccps[compno].qmfbid == 0)
+       {
+         dwt_encode_real (tilec->data, tilec->x1 - tilec->x0,
+                          tilec->y1 - tilec->y0, tilec,
+                          tilec->numresolutions - 1);
+       }
     }
-  }
 
 /*------------------TIER1-----------------*/
 
-  t1_init_luts();
-  t1_encode_cblks(tile, tcd_tcp);
+  t1_init_luts ();
+  t1_encode_cblks (tile, tcd_tcp);
 
 /*-----------RATE-ALLOCATE------------------*/
 
@@ -1424,35 +1653,36 @@ int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
 
     /* Normal Rate/distortion allocation */
 
-    tcd_rateallocate(dest, len, info_IM);
+    tcd_rateallocate (dest, len, info_IM);
 
   else
-
     /* Fixed layer allocation */
 
-    tcd_rateallocate_fixed();
+    tcd_rateallocate_fixed ();
 
 /*--------------TIER2------------------*/
   info_IM->index_write = 1;    /* INDEX */
 
-  l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
-                       tcd_tcp->numlayers, dest, len, info_IM);
+  l = t2_encode_packets (tcd_img, tcd_cp, tileno, tile,
+                        tcd_tcp->numlayers, dest, len, info_IM);
 
  /*---------------CLEAN-------------------*/
-  time = clock() - time;
-  printf("total:     %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
-        (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
+  time = clock () - time;
+  printf ("total:     %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
+         (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
 
-  for (compno = 0; compno < tile->numcomps; compno++) {
-    tilec = &tile->comps[compno];
-    free(tilec->data);
-  }
+  for (compno = 0; compno < tile->numcomps; compno++)
+    {
+      tilec = &tile->comps[compno];
+      free (tilec->data);
+    }
 
   return l;
 }
 
 
-int tcd_decode_tile(unsigned char *src, int len, int tileno)
+int
+tcd_decode_tile (unsigned char *src, int len, int tileno)
 {
   int l;
   int compno;
@@ -1465,137 +1695,160 @@ int tcd_decode_tile(unsigned char *src, int len, int tileno)
   tcd_tcp = &tcd_cp->tcps[tileno];
   tile = tcd_tile;
 
-  time = clock();
+  time = clock ();
 
-  fprintf(stderr, "tile decoding time %d/%d: ", tileno + 1,
-         tcd_cp->tw * tcd_cp->th);
+  fprintf (stderr, "tile decoding time %d/%d: ", tileno + 1,
+          tcd_cp->tw * tcd_cp->th);
 
        /*--------------TIER2------------------*/
 
-  l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
+  l = t2_decode_packets (src, len, tcd_img, tcd_cp, tileno, tile);
 
-  if (l == -999) {
-    eof = 1;
-    fprintf(stderr, "tcd_decode: incomplete bistream\n");
-  }
+  if (l == -999)
+    {
+      eof = 1;
+      fprintf (stderr, "tcd_decode: incomplete bistream\n");
+    }
 
        /*------------------TIER1-----------------*/
-  t1_init_luts();
-  t1_decode_cblks(tile, tcd_tcp);
+  t1_init_luts ();
+  t1_decode_cblks (tile, tcd_tcp);
 
        /*----------------DWT---------------------*/
 
-  for (compno = 0; compno < tile->numcomps; compno++) {
-    tcd_tilecomp_t *tilec = &tile->comps[compno];
-    if (tcd_cp->reduce_on == 1) {
-      tcd_img->comps[compno].resno_decoded =
-       tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1;
-    }
+  for (compno = 0; compno < tile->numcomps; compno++)
+    {
+      tcd_tilecomp_t *tilec = &tile->comps[compno];
+      if (tcd_cp->reduce_on == 1)
+       {
+         tcd_img->comps[compno].resno_decoded =
+           tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1;
+       }
 
 
-    if (tcd_tcp->tccps[compno].qmfbid == 1) {
-      dwt_decode(tilec->data, tilec->x1 - tilec->x0,
-                tilec->y1 - tilec->y0, tilec,
-                tilec->numresolutions - 1,
-                tilec->numresolutions - 1 -
-                tcd_img->comps[compno].resno_decoded);
-    } else {
-      dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
+      if (tcd_tcp->tccps[compno].qmfbid == 1)
+       {
+         dwt_decode (tilec->data, tilec->x1 - tilec->x0,
                      tilec->y1 - tilec->y0, tilec,
                      tilec->numresolutions - 1,
                      tilec->numresolutions - 1 -
                      tcd_img->comps[compno].resno_decoded);
-    }
+       }
+      else
+       {
+         dwt_decode_real (tilec->data, tilec->x1 - tilec->x0,
+                          tilec->y1 - tilec->y0, tilec,
+                          tilec->numresolutions - 1,
+                          tilec->numresolutions - 1 -
+                          tcd_img->comps[compno].resno_decoded);
+       }
 
-    if (tile->comps[compno].numresolutions > 0)
-      tcd_img->comps[compno].factor =
-       tile->comps[compno].numresolutions -
-       (tcd_img->comps[compno].resno_decoded + 1);
-  }
+      if (tile->comps[compno].numresolutions > 0)
+       tcd_img->comps[compno].factor =
+         tile->comps[compno].numresolutions -
+         (tcd_img->comps[compno].resno_decoded + 1);
+    }
 
        /*----------------MCT-------------------*/
 
-  if (tcd_tcp->mct) {
-    if (tcd_tcp->tccps[0].qmfbid == 1) {
-      mct_decode(tile->comps[0].data, tile->comps[1].data,
-                tile->comps[2].data,
-                (tile->comps[0].x1 -
-                 tile->comps[0].x0) * (tile->comps[0].y1 -
-                                       tile->comps[0].y0));
-    } else {
-      mct_decode_real(tile->comps[0].data, tile->comps[1].data,
+  if (tcd_tcp->mct)
+    {
+      if (tcd_tcp->tccps[0].qmfbid == 1)
+       {
+         mct_decode (tile->comps[0].data, tile->comps[1].data,
                      tile->comps[2].data,
                      (tile->comps[0].x1 -
                       tile->comps[0].x0) * (tile->comps[0].y1 -
                                             tile->comps[0].y0));
+       }
+      else
+       {
+         mct_decode_real (tile->comps[0].data, tile->comps[1].data,
+                          tile->comps[2].data,
+                          (tile->comps[0].x1 -
+                           tile->comps[0].x0) * (tile->comps[0].y1 -
+                                                 tile->comps[0].y0));
+       }
     }
-  }
 
        /*---------------TILE-------------------*/
 
-  for (compno = 0; compno < tile->numcomps; compno++) {
-    tcd_tilecomp_t *tilec = &tile->comps[compno];
-    tcd_resolution_t *res =
-      &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
-    int adjust =
-      tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
-                                             prec - 1);
-    int min =
-      tcd_img->comps[compno].
-      sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
-    int max =
-      tcd_img->comps[compno].
-      sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
-      1 : (1 << tcd_img->comps[compno].prec) - 1;
-
-    int tw = tilec->x1 - tilec->x0;
-    int w = tcd_img->comps[compno].w;
-
-    int i, j;
-    int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0,
-                                  tcd_img->comps[compno].factor);
-    int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0,
-                                  tcd_img->comps[compno].factor);
-
-    for (j = res->y0; j < res->y1; j++) {
-      for (i = res->x0; i < res->x1; i++) {
-
-       int v;
+  for (compno = 0; compno < tile->numcomps; compno++)
+    {
+      tcd_tilecomp_t *tilec = &tile->comps[compno];
+      tcd_resolution_t *res =
+       &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
+      int adjust =
+       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
+                                               prec - 1);
+      int min =
+       tcd_img->comps[compno].
+       sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
+      int max =
+       tcd_img->comps[compno].
+       sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
+       1 : (1 << tcd_img->comps[compno].prec) - 1;
+
+      int tw = tilec->x1 - tilec->x0;
+      int w = tcd_img->comps[compno].w;
+
+      int i, j;
+      int offset_x = int_ceildivpow2 (tcd_img->comps[compno].x0,
+                                     tcd_img->comps[compno].factor);
+      int offset_y = int_ceildivpow2 (tcd_img->comps[compno].y0,
+                                     tcd_img->comps[compno].factor);
+
+      for (j = res->y0; j < res->y1; j++)
+       {
+         for (i = res->x0; i < res->x1; i++)
+           {
+
+             int v;
+
+             double tmp =
+               (double) tilec->data[i - res->x0 + (j - res->y0) * tw];
+             if (tcd_tcp->tccps[compno].qmfbid == 1)
+               {
+                 v = (int) tmp;
+               }
+             else
+               {
 
-       double tmp= (double) tilec->data[i - res->x0 + (j - res->y0) * tw];
-       if (tcd_tcp->tccps[compno].qmfbid == 1) {
-         v = (int) tmp;
-       } else {
+                 //v = (int) tmp >> 13;
 
-         //v = (int) tmp >> 13;
+                 //Mod antonin : multbug1
+                 v =
+                   (int) ((fabs (tmp / 8192.0) >=
+                           floor (fabs (tmp / 8192.0)) +
+                           0.5) ? fabs (tmp / 8192.0) +
+                          1.0 : fabs (tmp / 8192.0));
 
-         //Mod antonin : multbug1
-         v = (int) ((fabs(tmp/8192.0)>=floor(fabs(tmp/8192.0))+0.5)?fabs(tmp/8192.0)+1.0:fabs(tmp/8192.0));
+                 v = (tmp < 0) ? -v : v;
 
-         v = (tmp<0)?-v:v;
+                 //doM
+               }
+             v += adjust;
 
-         //doM
+             tcd_img->comps[compno].data[(i - offset_x) +
+                                         (j - offset_y) * w] =
+               int_clamp (v, min, max);
+           }
        }
-       v += adjust;
-
-       tcd_img->comps[compno].data[(i - offset_x) +
-                                   (j - offset_y) * w] =
-         int_clamp(v, min, max);
-      }
     }
-  }
 
-  time = clock() - time;
-  fprintf(stderr, "total:     %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
-         (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
+  time = clock () - time;
+  fprintf (stderr, "total:     %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
+          (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
 
-  for (compno = 0; compno < tile->numcomps; compno++) {
-    free(tcd_image.tiles[tileno].comps[compno].data);
+  for (compno = 0; compno < tile->numcomps; compno++)
+    {
+      free (tcd_image.tiles[tileno].comps[compno].data);
     }
 
-  if (eof) {
-    longjmp(j2k_error, 1);
-  }
+  if (eof)
+    {
+      longjmp (j2k_error, 1);
+    }
 
   return l;
 }