]> Creatis software - gdcm.git/blobdiff - src/gdcmopenjpeg/codec/convert.c
ENH: Huge update to openjpeg 1.0 (actually more CVS)...
[gdcm.git] / src / gdcmopenjpeg / codec / convert.c
index 2aeed36698aa3746ac6c905388a33544dbc43d10..06b460aeca8721974fc8edfa161b8cdfb675e8eb 100644 (file)
-/*
- * Copyright (c) 2001-2003, David Janssens
- * Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2002-2003,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <openjpeg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <math.h>
-#include <string.h>
-
-/* -->> -->> -->> -->>
-
-  BMP IMAGE FORMAT
-
- <<-- <<-- <<-- <<-- */
-
-/* UINT2 defines a two byte word */
-typedef unsigned short int UINT2;
-
-/* UINT4 defines a four byte word */
-typedef unsigned long int UINT4;
-
-typedef struct {
-  UINT2 bfType;         /* 'BM' for Bitmap (19776) */
-  UINT4 bfSize;         /* Size of the file        */
-  UINT2 bfReserved1;      /* Reserved : 0            */
-  UINT2 bfReserved2;      /* Reserved : 0            */
-  UINT4 bfOffBits;      /* Offset                  */
-} BITMAPFILEHEADER_t;
-
-typedef struct {
-  UINT4 biSize;         /* Size of the structure in bytes */
-  UINT4 biWidth;      /* Width of the image in pixels */
-  UINT4 biHeight;      /* Heigth of the image in pixels */
-  UINT2 biPlanes;      /* 1 */
-  UINT2 biBitCount;      /* Number of color bits by pixels */
-  UINT4 biCompression;      /* Type of encoding 0: none 1: RLE8 2: RLE4 */
-  UINT4 biSizeImage;      /* Size of the image in bytes */
-  UINT4 biXpelsPerMeter;   /* Horizontal (X) resolution in pixels/meter */
-  UINT4 biYpelsPerMeter;   /* Vertical (Y) resolution in pixels/meter */
-  UINT4 biClrUsed;      /* Number of color used in the image (0: ALL) */
-  UINT4 biClrImportant;      /* Number of important color (0: ALL) */
-} BITMAPINFOHEADER_t;
-
-int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
-          int subsampling_dy, int Dim[2])
-{
-  FILE *IN;
-  FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL;
-  BITMAPFILEHEADER_t File_h;
-  BITMAPINFOHEADER_t Info_h;
-  unsigned char *RGB;
-  unsigned char *table_R, *table_G, *table_B;
-  unsigned int j, w, h, PAD, type = 0;
-
-  int i;
-  int gray_scale = 1, not_end_file = 1; 
-
-  unsigned int line = 0, col = 0;
-  unsigned char v, v2;
-  UINT4 W, H;
-
-  IN = fopen(filename, "rb");
-  if (!IN) {
-    fprintf(stderr,
-       "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
-       filename);
-    return 0;
-  }
-
-  File_h.bfType = getc(IN);
-  File_h.bfType = (getc(IN) << 8) + File_h.bfType;
-
-  if (File_h.bfType != 19778) {
-    fprintf(stderr,"Error, not a BMP file!\n");
-    return 0;
-  } else {
-    /* FILE HEADER */
-    /* ------------- */
-    File_h.bfSize = getc(IN);
-    File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;
-    File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;
-    File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;
-
-    File_h.bfReserved1 = getc(IN);
-    File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;
-
-    File_h.bfReserved2 = getc(IN);
-    File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;
-
-    File_h.bfOffBits = getc(IN);
-    File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;
-    File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;
-    File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;
-
-    /* INFO HEADER */
-    /* ------------- */
-
-    Info_h.biSize = getc(IN);
-    Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;
-    Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;
-    Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;
-
-    Info_h.biWidth = getc(IN);
-    Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;
-    Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;
-    Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;
-    w = Info_h.biWidth;
-
-    Info_h.biHeight = getc(IN);
-    Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;
-    Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;
-    Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;
-    h = Info_h.biHeight;
-
-    Info_h.biPlanes = getc(IN);
-    Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;
-
-    Info_h.biBitCount = getc(IN);
-    Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;
-
-    Info_h.biCompression = getc(IN);
-    Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;
-    Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;
-    Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;
-
-    Info_h.biSizeImage = getc(IN);
-    Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;
-    Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;
-    Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;
-
-    Info_h.biXpelsPerMeter = getc(IN);
-    Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;
-    Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;
-    Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;
-
-    Info_h.biYpelsPerMeter = getc(IN);
-    Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;
-    Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;
-    Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;
-
-    Info_h.biClrUsed = getc(IN);
-    Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;
-    Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;
-    Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;
-
-    Info_h.biClrImportant = getc(IN);
-    Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;
-    Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;
-    Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;
-
-    /* Read the data and store them in the OUT file */
-
-    if (Info_h.biBitCount == 24) {
-      img->x0 = Dim[0];
-      img->y0 = Dim[1];
-      img->x1 =
-        !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
-        subsampling_dx + 1;
-      img->y1 =
-        !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
-        subsampling_dy + 1;
-      img->numcomps = 3;
-      img->color_space = 1;
-      img->comps =
-        (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
-      for (i = 0; i < img->numcomps; i++) {
-        img->comps[i].prec = 8;
-        img->comps[i].bpp = 8;
-        img->comps[i].sgnd = 0;
-        img->comps[i].dx = subsampling_dx;
-        img->comps[i].dy = subsampling_dy;
-      }
-      Compo0 = fopen("Compo0", "wb");
-      if (!Compo0) {
-        fprintf(stderr,
-          "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
-      }
-      Compo1 = fopen("Compo1", "wb");
-      if (!Compo1) {
-        fprintf(stderr,
-          "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
-      }
-      Compo2 = fopen("Compo2", "wb");
-      if (!Compo2) {
-        fprintf(stderr,
-          "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
-      }
-
-      /* Place the cursor at the beginning of the image information */
-      fseek(IN, 0, SEEK_SET);
-      fseek(IN, File_h.bfOffBits, SEEK_SET);
-
-      W = Info_h.biWidth;
-      H = Info_h.biHeight;
-
-      /* PAD = 4 - (3 * W) % 4;*/
-      /* PAD = (PAD == 4) ? 0 : PAD; */
-      PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
-
-
-      RGB =
-        (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
-
-      fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
-
-      for (j = 0; j < (3 * W + PAD) * H; j++) {
-        unsigned char elmt;
-        int Wp = 3 * W + PAD;
-
-        elmt = RGB[(H - (j / Wp + 1)) * Wp + j % Wp];
-        if ((j % Wp) < (3 * W)) {
-          switch (type) {
-          case 0:
-            fprintf(Compo2, "%c", elmt);
-            type = 1;
-            break;
-          case 1:
-            fprintf(Compo1, "%c", elmt);
-            type = 2;
-            break;
-          case 2:
-            fprintf(Compo0, "%c", elmt);
-            type = 0;
-            break;
-          }
-        }
-      }
-
-      fclose(Compo0);
-      fclose(Compo1);
-      fclose(Compo2);
-      free(RGB);
-    } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
-      img->x0 = Dim[0];
-      img->y0 = Dim[1];
-      img->x1 =
-        !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -1) *
-        subsampling_dx + 1;
-      img->y1 =
-        !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -1) *
-        subsampling_dy + 1;
-
-      table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
-      table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
-      table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
-
-      for (j = 0; j < Info_h.biClrUsed; j++) {
-        table_B[j] = getc(IN);
-        table_G[j] = getc(IN);
-        table_R[j] = getc(IN);
-        getc(IN);
-        if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
-         && table_G[j] != table_B[j])
-          gray_scale = 0;
-      }
-
-      /* Place the cursor at the beginning of the image information */
-      fseek(IN, 0, SEEK_SET);
-      fseek(IN, File_h.bfOffBits, SEEK_SET);
-
-      W = Info_h.biWidth;
-      H = Info_h.biHeight;
-      if (Info_h.biWidth % 2)
-        W++;
-
-      RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
-
-      fread(RGB, sizeof(unsigned char), W * H, IN);
-      if (gray_scale) {
-        img->numcomps = 1;
-        img->comps =
-          (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
-        img->comps[0].prec = 8;
-        img->comps[0].bpp = 8;
-        img->comps[0].sgnd = 0;
-        img->comps[0].dx = subsampling_dx;
-        img->comps[0].dy = subsampling_dy;
-        Compo0 = fopen("Compo0", "wb");
-        if (!Compo0) {
-          fprintf(stderr,
-            "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
-        }
-        for (j = 0; j < W * H; j++) {
-          if ((j % W < W - 1 && Info_h.biWidth % 2)
-            || !(Info_h.biWidth % 2))
-            fprintf(Compo0, "%c",
-               table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
-        }
-          fclose(Compo0);
-      } else {
-        img->numcomps = 3;
-        img->comps =
-          (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
-        for (i = 0; i < img->numcomps; i++) {
-          img->comps[i].prec = 8;
-          img->comps[i].bpp = 8;
-          img->comps[i].sgnd = 0;
-          img->comps[i].dx = subsampling_dx;
-          img->comps[i].dy = subsampling_dy;
-        }
-
-        Compo0 = fopen("Compo0", "wb");
-        if (!Compo0) {
-          fprintf(stderr,
-             "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
-        }
-        Compo1 = fopen("Compo1", "wb");
-        if (!Compo1) {
-          fprintf(stderr,
-          "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
-        }
-        Compo2 = fopen("Compo2", "wb");
-        if (!Compo2) {
-          fprintf(stderr,
-            "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
-        }
-
-        for (j = 0; j < W * H; j++) {
-          if ((j % W < W - 1 && Info_h.biWidth % 2)
-            || !(Info_h.biWidth % 2)) {
-            fprintf(Compo0, "%c",
-              table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
-            fprintf(Compo1, "%c",
-               table_G[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
-            fprintf(Compo2, "%c",
-               table_B[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
-          }
-
-        }
-        fclose(Compo0);
-        fclose(Compo1);
-        fclose(Compo2);
-      }
-      free(RGB);
-
-    } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
-      img->x0 = Dim[0];
-      img->y0 = Dim[1];
-      img->x1 =
-        !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
-         subsampling_dx + 1;
-      img->y1 =
-       !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
-        subsampling_dy + 1;
-
-      table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
-      table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
-      table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
-
-      for (j = 0; j < Info_h.biClrUsed; j++) {
-        table_B[j] = getc(IN);
-        table_G[j] = getc(IN);
-        table_R[j] = getc(IN);
-        getc(IN);
-        if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
-         && table_G[j] != table_B[j])
-          gray_scale = 0;
-      }
-
-      /* Place the cursor at the beginning of the image information */
-      fseek(IN, 0, SEEK_SET);
-      fseek(IN, File_h.bfOffBits, SEEK_SET);
-
-      if (gray_scale) {
-        img->numcomps = 1;
-        img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
-        img->comps[0].prec = 8;
-        img->comps[0].bpp = 8;
-        img->comps[0].sgnd = 0;
-        img->comps[0].dx = subsampling_dx;
-        img->comps[0].dy = subsampling_dy;
-        Compo0 = fopen("Compo0", "wb");
-        if (!Compo0) {
-          fprintf(stderr,
-            "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
-        }
-      } else {
-        img->numcomps = 3;
-        img->comps =
-          (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
-        for (i = 0; i < img->numcomps; i++) {
-          img->comps[i].prec = 8;
-          img->comps[i].bpp = 8;
-          img->comps[i].sgnd = 0;
-          img->comps[i].dx = subsampling_dx;
-          img->comps[i].dy = subsampling_dy;
-        }
-        Compo0 = fopen("Compo0", "wb");
-        if (!Compo0) {
-          fprintf(stderr,
-             "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
-        }
-        Compo1 = fopen("Compo1", "wb");
-        if (!Compo1) {
-          fprintf(stderr,
-            "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
-        }
-        Compo2 = fopen("Compo2", "wb");
-        if (!Compo2) {
-          fprintf(stderr,
-             "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
-        }
-      }
-
-      RGB =
-        (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight *
-        sizeof(unsigned char));
-
-      while (not_end_file) {
-        v = getc(IN);
-        if (v) {
-          v2 = getc(IN);
-          for (i = 0; i < (int) v; i++) {
-            RGB[line * Info_h.biWidth + col] = v2;
-            col++;
-          }
-        } else {
-          v = getc(IN);
-          switch (v) {
-          case 0:
-            col = 0;
-            line++;
-            break;
-          case 1:
-            line++;
-            not_end_file = 0;
-            break;
-          case 2:
-            fprintf(stderr,"No Delta supported\n");
-            return 1;
-            break;
-          default:
-            for (i = 0; i < v; i++) {
-              v2 = getc(IN);
-              RGB[line * Info_h.biWidth + col] = v2;
-              col++;
-            }
-            if (v % 2)
-              v2 = getc(IN);
-          }
-        }
-      }
-      
-      if (gray_scale) {
-        for (line = 0; line < Info_h.biHeight; line++)
-          for (col = 0; col < Info_h.biWidth; col++)
-            fprintf(Compo0, "%c", table_R[(int)
-                 RGB[(Info_h.biHeight - line -
-                 1) * Info_h.biWidth + col]]);
-          fclose(Compo0);
-        } else {
-          for (line = 0; line < Info_h.biHeight; line++)
-            for (col = 0; col < Info_h.biWidth; col++) {
-              fprintf(Compo0, "%c", table_R[(int)
-                 RGB[(Info_h.biHeight - line -
-                 1) * Info_h.biWidth + col]]);
-              fprintf(Compo1, "%c", table_G[(int)
-                 RGB[(Info_h.biHeight - line -
-                 1) * Info_h.biWidth + col]]);
-              fprintf(Compo2, "%c", table_B[(int)
-                 RGB[(Info_h.biHeight - line -
-                 1) * Info_h.biWidth + col]]);
-            }
-          fclose(Compo0);
-          fclose(Compo1);
-          fclose(Compo2);
-        }
-        free(RGB);
-      } else
-        fprintf(stderr,
-          "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
-          Info_h.biBitCount);
-
-       fclose(IN);
-       return 1;
-  }
-}
-
-   /* -->> -->> -->> -->>
-
-      PGX IMAGE FORMAT
-
-      <<-- <<-- <<-- <<-- */
-
-
-unsigned char readuchar(FILE * f)
-{
-  unsigned char c1;
-  fread(&c1, 1, 1, f);
-  return c1;
-}
-
-unsigned short readushort(FILE * f, int bigendian)
-{
-  unsigned char c1, c2;
-  fread(&c1, 1, 1, f);
-  fread(&c2, 1, 1, f);
-  if (bigendian)
-    return (c1 << 8) + c2;
-  else
-    return (c2 << 8) + c1;
-}
-
-unsigned int readuint(FILE * f, int bigendian)
-{
-  unsigned char c1, c2, c3, c4;
-  fread(&c1, 1, 1, f);
-  fread(&c2, 1, 1, f);
-  fread(&c3, 1, 1, f);
-  fread(&c4, 1, 1, f);
-  if (bigendian)
-    return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
-  else
-    return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
-}
-
-int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
-          int subsampling_dx, int subsampling_dy, int Dim[2],
-          j2k_cp_t cp)
-{
-  FILE *f;
-  int w, h, prec;
-  int i, compno, bandno;
-  char str[256]; 
-
-  char endian1,endian2,sign;
-  char signtmp[32];
-
-  char temp[32];
-  int bigendian;
-  j2k_comp_t *comp;
-
-  img->numcomps = 1;
-  img->color_space = 2;
-  img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
-  for (compno = 0; compno < img->numcomps; compno++) {
-    FILE *src;
-    char tmp[16];
-    int max = 0;
-    int Y1;
-
-    comp = &img->comps[compno];
-    sprintf(str, "%s", filename);
-    
-
-    f = fopen(str, "rb");
-    if (!f) {
-      fprintf(stderr, "Failed to open %s for reading !\n", str);
-      return 0;
-    }
-
-    fseek(f, 0, SEEK_SET);
-    fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
-
-    i=0;
-
-    sign='+';
-
-    while (signtmp[i]!='\0') {
-      if (signtmp[i]=='-') sign='-';
-         i++;
-    }
-
-    fgetc(f);
-    if (endian1=='M' && endian2=='L')
-      bigendian = 1;
-    else if (endian2=='M' && endian1=='L')
-      bigendian = 0;
-    else {
-      fprintf(stderr, "Bad pgx header, please check input file\n");
-      return 0;
-    }
-    if (compno == 0) {
-      img->x0 = Dim[0];
-      img->y0 = Dim[1];
-      img->x1 =
-        !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
-        subsampling_dx + 1;
-      img->y1 =
-        !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
-        subsampling_dy + 1;
-    } else {
-      if (w != img->x1 || h != img->y1)
-        return 0;
-    }
-    
-    if (sign == '-') {
-      comp->sgnd = 1;
-    } else {
-      comp->sgnd = 0;
-    }
-    comp->prec = prec;
-    comp->dx = subsampling_dx;
-    comp->dy = subsampling_dy;
-    bandno = 1;
-    
-    Y1 = cp.ty0 + bandno * cp.tdy <
-      img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
-    Y1 -= img->y0;
-    
-    sprintf(tmp, "bandtile%d", bandno);   /* bandtile file */
-    src = fopen(tmp, "wb");
-    if (!src) {
-      fprintf(stderr, "failed to open %s for writing !\n", tmp);
-    }
-    for (i = 0; i < w * h; i++) {
-      int v;
-      if (i == Y1 * w / subsampling_dy && tdy != -1) {   /* bandtile is full */
-        fclose(src);
-        bandno++;
-        sprintf(tmp, "bandtile%d", bandno);
-        src = fopen(tmp, "wb");
-        if (!src) {
-          fprintf(stderr, "failed to open %s for writing !\n", tmp);
-        }
-        Y1 = cp.ty0 + bandno * cp.tdy <
-        img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
-        Y1 -= img->y0;
-      }
-      if (comp->prec <= 8) {
-        if (!comp->sgnd) {
-          v = readuchar(f);
-        } else {
-          v = (char) readuchar(f);
-        }
-      } else if (comp->prec <= 16) {
-        if (!comp->sgnd) {
-          v = readushort(f, bigendian);
-        } else {
-          v = (short) readushort(f, bigendian);
-        }
-      } else {
-        if (!comp->sgnd) {
-          v = readuint(f, bigendian);
-        } else {
-          v = (int) readuint(f, bigendian);
-        }
-      }
-      if (v > max)
-        max = v;
-      fprintf(src, "%d ", v);
-    }
-    fclose(f);
-    fclose(src);
-    comp->bpp = int_floorlog2(max) + 1;
-  }
-  return 1;
-}
-
-/* -->> -->> -->> -->>
-
-  PNM IMAGE FORMAT
-
- <<-- <<-- <<-- <<-- */
-
-int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
-          int subsampling_dy, int Dim[2])
-{
-  FILE *f;
-  FILE *Compo0, *Compo1, *Compo2;
-  int w, h;
-  int i;
-  char value;
-  char comment[256];
-
-  f = fopen(filename, "rb");
-  if (!f) {
-    fprintf(stderr,
-       "\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
-       filename);
-    return 0;
-  }
-
-  if (fgetc(f) != 'P')
-    return 0;
-  value = fgetc(f);
-
-  if (value == '2') {
-    fgetc(f);
-    if (fgetc(f) == '#') {
-      fseek(f, 0, SEEK_SET);
-      fscanf(f, "P2\n");
-      fgets(comment, 256, f);
-      fscanf(f, "%d %d\n255", &w, &h);
-    } else {
-      fseek(f, 0, SEEK_SET);
-      fscanf(f, "P2\n%d %d\n255", &w, &h);
-    }
-
-    fgetc(f);
-    img->x0 = Dim[0];
-    img->y0 = Dim[1];
-    img->x1 =
-      !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
-      subsampling_dx + 1;
-    img->y1 =
-      !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
-      subsampling_dy + 1;
-
-    img->numcomps = 1;
-    img->color_space = 2;
-    img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
-    img->comps[0].prec = 8;
-    img->comps[0].bpp = 8;
-    img->comps[0].sgnd = 0;
-    img->comps[0].dx = subsampling_dx;
-    img->comps[0].dy = subsampling_dy;
-
-    Compo0 = fopen("Compo0", "wb");
-    if (!Compo0) {
-      fprintf(stderr,
-         "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
-    }
-    for (i = 0; i < w * h; i++) {
-      unsigned int l;
-      fscanf(f, "%d", &l);
-      fprintf(Compo0, "%c", l);
-    }
-    fclose(Compo0);
-  } else if (value == '5') {
-    fgetc(f);
-    if (fgetc(f) == '#') {
-      fseek(f, 0, SEEK_SET);
-      fscanf(f, "P5\n");
-      fgets(comment, 256, f);
-      fscanf(f, "%d %d\n255", &w, &h);
-    } else {
-      fseek(f, 0, SEEK_SET);
-      fscanf(f, "P5\n%d %d\n255", &w, &h);
-    }
-
-    fgetc(f);
-    img->x0 = Dim[0];
-    img->y0 = Dim[1];
-    img->x1 =
-      !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
-      subsampling_dx + 1;
-    img->y1 =
-      !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
-      subsampling_dy + 1;
-
-    img->numcomps = 1;
-    img->color_space = 2;
-    img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
-    img->comps[0].prec = 8;
-    img->comps[0].bpp = 8;
-    img->comps[0].sgnd = 0;
-    img->comps[0].dx = subsampling_dx;
-    img->comps[0].dy = subsampling_dy;
-    Compo0 = fopen("Compo0", "wb");
-    if (!Compo0) {
-      fprintf(stderr,
-         "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
-    }
-    for (i = 0; i < w * h; i++) {
-      unsigned char l;
-      fread(&l, 1, 1, f);
-      fwrite(&l, 1, 1, Compo0);
-    }
-    fclose(Compo0);
-  } else if (value == '3') {
-    fgetc(f);
-    if (fgetc(f) == '#') {
-      fseek(f, 0, SEEK_SET);
-      fscanf(f, "P3\n");
-      fgets(comment, 256, f);
-      fscanf(f, "%d %d\n255", &w, &h);
-    } else {
-      fseek(f, 0, SEEK_SET);
-      fscanf(f, "P3\n%d %d\n255", &w, &h);
-    }
-
-    fgetc(f);
-    img->x0 = Dim[0];
-    img->y0 = Dim[1];
-    img->x1 =
-      !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
-      subsampling_dx + 1;
-    img->y1 =
-      !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
-      subsampling_dy + 1;
-    img->numcomps = 3;
-    img->color_space = 1;
-    img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
-    for (i = 0; i < img->numcomps; i++) {
-      img->comps[i].prec = 8;
-      img->comps[i].bpp = 8;
-      img->comps[i].sgnd = 0;
-      img->comps[i].dx = subsampling_dx;
-      img->comps[i].dy = subsampling_dy;
-    }
-    Compo0 = fopen("Compo0", "wb");
-    if (!Compo0) {
-      fprintf(stderr,
-         "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
-    }
-
-    Compo1 = fopen("Compo1", "wb");
-    if (!Compo1) {
-      fprintf(stderr,
-         "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
-    }
-
-    Compo2 = fopen("Compo2", "wb");
-    if (!Compo2) {
-      fprintf(stderr,
-         "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
-    }
-
-    for (i = 0; i < w * h; i++) {
-      unsigned int r, g, b;
-      fscanf(f, "%d", &r);
-      fscanf(f, "%d", &g);
-      fscanf(f, "%d", &b);
-      fprintf(Compo0, "%c", r);
-      fprintf(Compo1, "%c", g);
-      fprintf(Compo2, "%c", b);
-    }
-    fclose(Compo0);
-    fclose(Compo1);
-    fclose(Compo2);
-  } else if (value == '6') {
-    fgetc(f);
-    if (fgetc(f) == '#') {
-      fseek(f, 0, SEEK_SET);
-      fscanf(f, "P6\n");
-      fgets(comment, 256, f);
-      fscanf(f, "%d %d\n255", &w, &h);
-    } else {
-      fseek(f, 0, SEEK_SET);
-      fscanf(f, "P6\n%d %d\n255", &w, &h);
-    }
-
-    fgetc(f);
-    img->x0 = Dim[0];
-    img->y0 = Dim[1];
-    img->x1 =
-      !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) *
-      subsampling_dx + 1;
-    img->y1 =
-      !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) *
-      subsampling_dy + 1;
-    img->numcomps = 3;
-    img->color_space = 1;
-    img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
-    for (i = 0; i < img->numcomps; i++) {
-      img->comps[i].prec = 8;
-      img->comps[i].bpp = 8;
-      img->comps[i].sgnd = 0;
-      img->comps[i].dx = subsampling_dx;
-      img->comps[i].dy = subsampling_dy;
-    }
-    Compo0 = fopen("Compo0", "wb");
-    if (!Compo0) {
-      fprintf(stderr,
-         "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
-    }
-
-    Compo1 = fopen("Compo1", "wb");
-    if (!Compo1) {
-      fprintf(stderr,
-         "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
-    }
-
-    Compo2 = fopen("Compo2", "wb");
-    if (!Compo2) {
-      fprintf(stderr,
-         "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
-    }
-
-    for (i = 0; i < w * h; i++) {
-      unsigned char r, g, b;
-      fread(&r, 1, 1, f);
-      fread(&g, 1, 1, f);
-      fread(&b, 1, 1, f);
-      fwrite(&r, 1, 1, Compo0);
-      fwrite(&g, 1, 1, Compo1);
-      fwrite(&b, 1, 1, Compo2);
-    }
-    fclose(Compo0);
-    fclose(Compo1);
-    fclose(Compo2);
-  } else {
-    return 0;
-  }
-  fclose(f);
-  return 1;
-}
+/*\r
+ * Copyright (c) 2001-2003, David Janssens\r
+ * Copyright (c) 2002-2003, Yannick Verschueren\r
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
+ * Copyright (c) 2005, Hervé Drolon, FreeImage Team\r
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ *    notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ *    notice, this list of conditions and the following disclaimer in the\r
+ *    documentation and/or other materials provided with the distribution.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
+ * POSSIBILITY OF SUCH DAMAGE.\r
+ */\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include "openjpeg.h"\r
+\r
+/*\r
+ * Get logarithm of an integer and round downwards.\r
+ *\r
+ * log2(a)\r
+ */\r
+static int int_floorlog2(int a) {\r
+  int l;\r
+  for (l = 0; a > 1; l++) {\r
+    a >>= 1;\r
+  }\r
+  return l;\r
+}\r
+\r
+/*\r
+ * Divide an integer by a power of 2 and round upwards.\r
+ *\r
+ * a divided by 2^b\r
+ */\r
+static int int_ceildivpow2(int a, int b) {\r
+  return (a + (1 << b) - 1) >> b;\r
+}\r
+\r
+/*\r
+ * Divide an integer and round upwards.\r
+ *\r
+ * a divided by b\r
+ */\r
+static int int_ceildiv(int a, int b) {\r
+  return (a + b - 1) / b;\r
+}\r
+\r
+/* -->> -->> -->> -->>\r
+\r
+  BMP IMAGE FORMAT\r
+\r
+ <<-- <<-- <<-- <<-- */\r
+\r
+/* WORD defines a two byte word */\r
+typedef unsigned short int WORD;\r
+\r
+/* DWORD defines a four byte word */\r
+typedef unsigned long int DWORD;\r
+\r
+typedef struct {\r
+  WORD bfType;      /* 'BM' for Bitmap (19776) */\r
+  DWORD bfSize;      /* Size of the file        */\r
+  WORD bfReserved1;    /* Reserved : 0            */\r
+  WORD bfReserved2;    /* Reserved : 0            */\r
+  DWORD bfOffBits;    /* Offset                  */\r
+} BITMAPFILEHEADER_t;\r
+\r
+typedef struct {\r
+  DWORD biSize;      /* Size of the structure in bytes */\r
+  DWORD biWidth;    /* Width of the image in pixels */\r
+  DWORD biHeight;    /* Heigth of the image in pixels */\r
+  WORD biPlanes;    /* 1 */\r
+  WORD biBitCount;    /* Number of color bits by pixels */\r
+  DWORD biCompression;    /* Type of encoding 0: none 1: RLE8 2: RLE4 */\r
+  DWORD biSizeImage;    /* Size of the image in bytes */\r
+  DWORD biXpelsPerMeter;  /* Horizontal (X) resolution in pixels/meter */\r
+  DWORD biYpelsPerMeter;  /* Vertical (Y) resolution in pixels/meter */\r
+  DWORD biClrUsed;    /* Number of color used in the image (0: ALL) */\r
+  DWORD biClrImportant;    /* Number of important color (0: ALL) */\r
+} BITMAPINFOHEADER_t;\r
+\r
+opj_image_t* bmptoimage(char *filename, opj_cparameters_t *parameters) {\r
+  int subsampling_dx = parameters->subsampling_dx;\r
+  int subsampling_dy = parameters->subsampling_dy;\r
+\r
+  int i, numcomps, w, h;\r
+  OPJ_COLOR_SPACE color_space;\r
+  opj_image_cmptparm_t cmptparm[3];  /* maximum of 3 components */\r
+  opj_image_t * image = NULL;\r
+\r
+  FILE *IN;\r
+  BITMAPFILEHEADER_t File_h;\r
+  BITMAPINFOHEADER_t Info_h;\r
+  unsigned char *RGB;\r
+  unsigned char *table_R, *table_G, *table_B;\r
+  unsigned int j, PAD = 0;\r
+\r
+  int x, y, index;\r
+  int gray_scale = 1, not_end_file = 1; \r
+\r
+  unsigned int line = 0, col = 0;\r
+  unsigned char v, v2;\r
+  DWORD W, H;\r
+  \r
+  IN = fopen(filename, "rb");\r
+  if (!IN) {\r
+    fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);\r
+    return 0;\r
+  }\r
+  \r
+  File_h.bfType = getc(IN);\r
+  File_h.bfType = (getc(IN) << 8) + File_h.bfType;\r
+  \r
+  if (File_h.bfType != 19778) {\r
+    fprintf(stderr,"Error, not a BMP file!\n");\r
+    return 0;\r
+  } else {\r
+    /* FILE HEADER */\r
+    /* ------------- */\r
+    File_h.bfSize = getc(IN);\r
+    File_h.bfSize = (getc(IN) << 8) + File_h.bfSize;\r
+    File_h.bfSize = (getc(IN) << 16) + File_h.bfSize;\r
+    File_h.bfSize = (getc(IN) << 24) + File_h.bfSize;\r
+\r
+    File_h.bfReserved1 = getc(IN);\r
+    File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1;\r
+\r
+    File_h.bfReserved2 = getc(IN);\r
+    File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2;\r
+\r
+    File_h.bfOffBits = getc(IN);\r
+    File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits;\r
+    File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits;\r
+    File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits;\r
+\r
+    /* INFO HEADER */\r
+    /* ------------- */\r
+\r
+    Info_h.biSize = getc(IN);\r
+    Info_h.biSize = (getc(IN) << 8) + Info_h.biSize;\r
+    Info_h.biSize = (getc(IN) << 16) + Info_h.biSize;\r
+    Info_h.biSize = (getc(IN) << 24) + Info_h.biSize;\r
+\r
+    Info_h.biWidth = getc(IN);\r
+    Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth;\r
+    Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth;\r
+    Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth;\r
+    w = Info_h.biWidth;\r
+\r
+    Info_h.biHeight = getc(IN);\r
+    Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight;\r
+    Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight;\r
+    Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight;\r
+    h = Info_h.biHeight;\r
+\r
+    Info_h.biPlanes = getc(IN);\r
+    Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes;\r
+\r
+    Info_h.biBitCount = getc(IN);\r
+    Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount;\r
+\r
+    Info_h.biCompression = getc(IN);\r
+    Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression;\r
+    Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression;\r
+    Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression;\r
+\r
+    Info_h.biSizeImage = getc(IN);\r
+    Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage;\r
+    Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage;\r
+    Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage;\r
+\r
+    Info_h.biXpelsPerMeter = getc(IN);\r
+    Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter;\r
+    Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter;\r
+    Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter;\r
+\r
+    Info_h.biYpelsPerMeter = getc(IN);\r
+    Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter;\r
+    Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter;\r
+    Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter;\r
+\r
+    Info_h.biClrUsed = getc(IN);\r
+    Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed;\r
+    Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed;\r
+    Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed;\r
+\r
+    Info_h.biClrImportant = getc(IN);\r
+    Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant;\r
+    Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant;\r
+    Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant;\r
+\r
+    /* Read the data and store them in the OUT file */\r
+    \r
+    if (Info_h.biBitCount == 24) {\r
+      numcomps = 3;\r
+      color_space = CLRSPC_SRGB;\r
+      /* initialize image components */\r
+      memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));\r
+      for(i = 0; i < numcomps; i++) {\r
+        cmptparm[i].prec = 8;\r
+        cmptparm[i].bpp = 8;\r
+        cmptparm[i].sgnd = 0;\r
+        cmptparm[i].dx = subsampling_dx;\r
+        cmptparm[i].dy = subsampling_dy;\r
+        cmptparm[i].w = w;\r
+        cmptparm[i].h = h;\r
+      }\r
+      /* create the image */\r
+      image = opj_image_create(numcomps, &cmptparm[0], color_space);\r
+      if(!image) {\r
+        fclose(IN);\r
+        return NULL;\r
+      }\r
+\r
+      /* set image offset and reference grid */\r
+      image->x0 = parameters->image_offset_x0;\r
+      image->y0 = parameters->image_offset_y0;\r
+      image->x1 =  !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;\r
+      image->y1 =  !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;\r
+\r
+      /* set image data */\r
+\r
+      /* Place the cursor at the beginning of the image information */\r
+      fseek(IN, 0, SEEK_SET);\r
+      fseek(IN, File_h.bfOffBits, SEEK_SET);\r
+      \r
+      W = Info_h.biWidth;\r
+      H = Info_h.biHeight;\r
+\r
+      /* PAD = 4 - (3 * W) % 4; */\r
+      /* PAD = (PAD == 4) ? 0 : PAD; */\r
+      PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;\r
+      \r
+      RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));\r
+      \r
+      fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);\r
+      \r
+      index = 0;\r
+\r
+      for(y = 0; y < (int)H; y++) {\r
+        unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);\r
+        for(x = 0; x < (int)W; x++) {\r
+          unsigned char *pixel = &scanline[3 * x];\r
+          image->comps[0].data[index] = pixel[2];  /* R */\r
+          image->comps[1].data[index] = pixel[1];  /* G */\r
+          image->comps[2].data[index] = pixel[0];  /* B */\r
+          index++;\r
+        }\r
+      }\r
+\r
+      free(RGB);\r
+\r
+    } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {\r
+      table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));\r
+      table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));\r
+      table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));\r
+      \r
+      for (j = 0; j < Info_h.biClrUsed; j++) {\r
+        table_B[j] = getc(IN);\r
+        table_G[j] = getc(IN);\r
+        table_R[j] = getc(IN);\r
+        getc(IN);\r
+        if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])\r
+          gray_scale = 0;\r
+      }\r
+      \r
+      /* Place the cursor at the beginning of the image information */\r
+      fseek(IN, 0, SEEK_SET);\r
+      fseek(IN, File_h.bfOffBits, SEEK_SET);\r
+      \r
+      W = Info_h.biWidth;\r
+      H = Info_h.biHeight;\r
+      if (Info_h.biWidth % 2)\r
+        W++;\r
+      \r
+      numcomps = gray_scale ? 1 : 3;\r
+      color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;\r
+      /* initialize image components */\r
+      memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));\r
+      for(i = 0; i < numcomps; i++) {\r
+        cmptparm[i].prec = 8;\r
+        cmptparm[i].bpp = 8;\r
+        cmptparm[i].sgnd = 0;\r
+        cmptparm[i].dx = subsampling_dx;\r
+        cmptparm[i].dy = subsampling_dy;\r
+        cmptparm[i].w = w;\r
+        cmptparm[i].h = h;\r
+      }\r
+      /* create the image */\r
+      image = opj_image_create(numcomps, &cmptparm[0], color_space);\r
+      if(!image) {\r
+        fclose(IN);\r
+        return NULL;\r
+      }\r
+\r
+      /* set image offset and reference grid */\r
+      image->x0 = parameters->image_offset_x0;\r
+      image->y0 = parameters->image_offset_y0;\r
+      image->x1 =  !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;\r
+      image->y1 =  !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;\r
+\r
+      /* set image data */\r
+\r
+      RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));\r
+      \r
+      fread(RGB, sizeof(unsigned char), W * H, IN);\r
+      if (gray_scale) {\r
+        index = 0;\r
+        for (j = 0; j < W * H; j++) {\r
+          if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {\r
+            image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];\r
+            index++;\r
+          }\r
+        }\r
+\r
+      } else {    \r
+        index = 0;\r
+        for (j = 0; j < W * H; j++) {\r
+          if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {\r
+            unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];\r
+            image->comps[0].data[index] = table_R[pixel_index];\r
+            image->comps[1].data[index] = table_G[pixel_index];\r
+            image->comps[2].data[index] = table_B[pixel_index];\r
+            index++;\r
+          }\r
+        }\r
+      }\r
+      free(RGB);\r
+            \r
+    } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {        \r
+      table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));\r
+      table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));\r
+      table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));\r
+      \r
+      for (j = 0; j < Info_h.biClrUsed; j++) {\r
+        table_B[j] = getc(IN);\r
+        table_G[j] = getc(IN);\r
+        table_R[j] = getc(IN);\r
+        getc(IN);\r
+        if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j])\r
+          gray_scale = 0;\r
+      }\r
+\r
+      numcomps = gray_scale ? 1 : 3;\r
+      color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;\r
+      /* initialize image components */\r
+      memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));\r
+      for(i = 0; i < numcomps; i++) {\r
+        cmptparm[i].prec = 8;\r
+        cmptparm[i].bpp = 8;\r
+        cmptparm[i].sgnd = 0;\r
+        cmptparm[i].dx = subsampling_dx;\r
+        cmptparm[i].dy = subsampling_dy;\r
+        cmptparm[i].w = w;\r
+        cmptparm[i].h = h;\r
+      }\r
+      /* create the image */\r
+      image = opj_image_create(numcomps, &cmptparm[0], color_space);\r
+      if(!image) {\r
+        fclose(IN);\r
+        return NULL;\r
+      }\r
+\r
+      /* set image offset and reference grid */\r
+      image->x0 = parameters->image_offset_x0;\r
+      image->y0 = parameters->image_offset_y0;\r
+      image->x1 =  !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;\r
+      image->y1 =  !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;\r
+\r
+      /* set image data */\r
+      \r
+      /* Place the cursor at the beginning of the image information */\r
+      fseek(IN, 0, SEEK_SET);\r
+      fseek(IN, File_h.bfOffBits, SEEK_SET);\r
+      \r
+      RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));\r
+            \r
+      while (not_end_file) {\r
+        v = getc(IN);\r
+        if (v) {\r
+          v2 = getc(IN);\r
+          for (i = 0; i < (int) v; i++) {\r
+            RGB[line * Info_h.biWidth + col] = v2;\r
+            col++;\r
+          }\r
+        } else {\r
+          v = getc(IN);\r
+          switch (v) {\r
+            case 0:\r
+              col = 0;\r
+              line++;\r
+              break;\r
+            case 1:\r
+              line++;\r
+              not_end_file = 0;\r
+              break;\r
+            case 2:\r
+              fprintf(stderr,"No Delta supported\n");\r
+              opj_image_destroy(image);\r
+              fclose(IN);\r
+              return NULL;\r
+              break;\r
+            default:\r
+              for (i = 0; i < v; i++) {\r
+                v2 = getc(IN);\r
+                RGB[line * Info_h.biWidth + col] = v2;\r
+                col++;\r
+              }\r
+              if (v % 2)\r
+                v2 = getc(IN);\r
+              break;\r
+          }\r
+        }\r
+      }\r
+      if (gray_scale) {\r
+        index = 0;\r
+        for (line = 0; line < Info_h.biHeight; line++) {\r
+          for (col = 0; col < Info_h.biWidth; col++) {\r
+            image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];\r
+            index++;\r
+          }\r
+        }\r
+      } else {\r
+        index = 0;\r
+        for (line = 0; line < Info_h.biHeight; line++) {\r
+          for (col = 0; col < Info_h.biWidth; col++) {\r
+            unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];\r
+            image->comps[0].data[index] = table_R[pixel_index];\r
+            image->comps[1].data[index] = table_G[pixel_index];\r
+            image->comps[2].data[index] = table_B[pixel_index];\r
+            index++;\r
+          }\r
+        }\r
+      }\r
+      free(RGB);\r
+  } else {\r
+    fprintf(stderr, \r
+      "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);\r
+  }\r
+  fclose(IN);\r
+ }\r
\r
+ return image;\r
+}\r
+\r
+int imagetobmp(opj_image_t * image, char *outfile) {\r
+  int w, wr, h, hr;\r
+  int i, pad;\r
+  FILE *fdest = NULL;\r
+\r
+  if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx\r
+    && image->comps[1].dx == image->comps[2].dx\r
+    && image->comps[0].dy == image->comps[1].dy\r
+    && image->comps[1].dy == image->comps[2].dy\r
+    && image->comps[0].prec == image->comps[1].prec\r
+    && image->comps[1].prec == image->comps[2].prec) {\r
+    \r
+    /* -->> -->> -->> -->>    \r
+    24 bits color      \r
+    <<-- <<-- <<-- <<-- */\r
+      \r
+    fdest = fopen(outfile, "wb");\r
+    if (!fdest) {\r
+      fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);\r
+      return 1;\r
+    }\r
+      \r
+    /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */\r
+    /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */\r
+    w = image->comps[0].w;\r
+    wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);\r
+      \r
+    /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */\r
+    /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */\r
+    h = image->comps[0].h;\r
+    hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);\r
+      \r
+    fprintf(fdest, "BM");\r
+      \r
+    /* FILE HEADER */\r
+    /* ------------- */\r
+    fprintf(fdest, "%c%c%c%c",\r
+      (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,\r
+      (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)  >> 8) & 0xff,\r
+      (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)  >> 16) & 0xff,\r
+      (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)  >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);\r
+      \r
+    /* INFO HEADER   */\r
+    /* ------------- */\r
+    fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,  ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),\r
+      (unsigned char) ((wr) >> 8) & 0xff,\r
+      (unsigned char) ((wr) >> 16) & 0xff,\r
+      (unsigned char) ((wr) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),\r
+      (unsigned char) ((hr) >> 8) & 0xff,\r
+      (unsigned char) ((hr) >> 16) & 0xff,\r
+      (unsigned char) ((hr) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);\r
+    fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,\r
+      (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,\r
+      (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,\r
+      (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,  ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);\r
+      \r
+    for (i = 0; i < wr * hr; i++) {\r
+      unsigned char R, G, B;\r
+      /* a modifier */\r
+      /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */\r
+      R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];\r
+      /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */\r
+      G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];\r
+      /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */\r
+      B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];\r
+      fprintf(fdest, "%c%c%c", B, G, R);\r
+      \r
+      if ((i + 1) % wr == 0) {\r
+        for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--)  /* ADD */\r
+          fprintf(fdest, "%c", 0);\r
+      }\r
+    }\r
+    fclose(fdest);\r
+  } else {      /* Gray-scale */\r
+\r
+    /* -->> -->> -->> -->>\r
+    8 bits non code (Gray scale)\r
+    <<-- <<-- <<-- <<-- */\r
+\r
+    fdest = fopen(outfile, "wb");\r
+    /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */\r
+    /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */\r
+    w = image->comps[0].w;\r
+    wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);\r
+      \r
+    /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */\r
+    /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */\r
+    h = image->comps[0].h;\r
+    hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);\r
+      \r
+    fprintf(fdest, "BM");\r
+      \r
+    /* FILE HEADER */\r
+    /* ------------- */\r
+    fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,\r
+      (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,\r
+      (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,\r
+      (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff, \r
+      ((54 + 1024) >> 16) & 0xff,\r
+      ((54 + 1024) >> 24) & 0xff);\r
+      \r
+    /* INFO HEADER */\r
+    /* ------------- */\r
+    fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,  ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),\r
+      (unsigned char) ((wr) >> 8) & 0xff,\r
+      (unsigned char) ((wr) >> 16) & 0xff,\r
+      (unsigned char) ((wr) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),\r
+      (unsigned char) ((hr) >> 8) & 0xff,\r
+      (unsigned char) ((hr) >> 16) & 0xff,\r
+      (unsigned char) ((hr) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);\r
+    fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,\r
+      (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) &  0xff,\r
+      (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) &  0xff,\r
+      (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,  ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,  ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);\r
+    fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);\r
+  }\r
+\r
+  for (i = 0; i < 256; i++) {\r
+    fprintf(fdest, "%c%c%c%c", i, i, i, 0);\r
+  }\r
+\r
+  for (i = 0; i < wr * hr; i++) {\r
+    /* a modifier !! */\r
+    /* fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]); */\r
+    fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);\r
+    /*if (((i + 1) % w == 0 && w % 2))\r
+    fprintf(fdest, "%c", 0); */\r
+    if ((i + 1) % wr == 0) {\r
+      for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--)  /* ADD */\r
+        fprintf(fdest, "%c", 0);\r
+    }\r
+  }\r
+  fclose(fdest);\r
+\r
+  return 0;\r
+}\r
+\r
+/* -->> -->> -->> -->>\r
+\r
+PGX IMAGE FORMAT\r
+\r
+<<-- <<-- <<-- <<-- */\r
+\r
+\r
+unsigned char readuchar(FILE * f)\r
+{\r
+  unsigned char c1;\r
+  fread(&c1, 1, 1, f);\r
+  return c1;\r
+}\r
+\r
+unsigned short readushort(FILE * f, int bigendian)\r
+{\r
+  unsigned char c1, c2;\r
+  fread(&c1, 1, 1, f);\r
+  fread(&c2, 1, 1, f);\r
+  if (bigendian)\r
+    return (c1 << 8) + c2;\r
+  else\r
+    return (c2 << 8) + c1;\r
+}\r
+\r
+unsigned int readuint(FILE * f, int bigendian)\r
+{\r
+  unsigned char c1, c2, c3, c4;\r
+  fread(&c1, 1, 1, f);\r
+  fread(&c2, 1, 1, f);\r
+  fread(&c3, 1, 1, f);\r
+  fread(&c4, 1, 1, f);\r
+  if (bigendian)\r
+    return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;\r
+  else\r
+    return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;\r
+}\r
+\r
+opj_image_t* pgxtoimage(char *filename, opj_cparameters_t *parameters) {\r
+  FILE *f = NULL;\r
+  int w, h, prec;\r
+  int i, numcomps, max;\r
+  OPJ_COLOR_SPACE color_space;\r
+  opj_image_cmptparm_t cmptparm;  /* maximum of 1 component  */\r
+  opj_image_t * image = NULL;\r
+\r
+  char endian1,endian2,sign;\r
+  char signtmp[32];\r
+\r
+  char temp[32];\r
+  int bigendian;\r
+  opj_image_comp_t *comp = NULL;\r
+\r
+  numcomps = 1;\r
+  color_space = CLRSPC_GRAY;\r
+\r
+  memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));\r
+\r
+  max = 0;\r
+\r
+  f = fopen(filename, "rb");\r
+  if (!f) {\r
+    fprintf(stderr, "Failed to open %s for reading !\n", filename);\r
+    return NULL;\r
+  }\r
+\r
+  fseek(f, 0, SEEK_SET);\r
+  fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);\r
+  \r
+  i=0;\r
+  sign='+';    \r
+  while (signtmp[i]!='\0') {\r
+    if (signtmp[i]=='-') sign='-';\r
+    i++;\r
+  }\r
+  \r
+  fgetc(f);\r
+  if (endian1=='M' && endian2=='L') {\r
+    bigendian = 1;\r
+  } else if (endian2=='M' && endian1=='L') {\r
+    bigendian = 0;\r
+  } else {\r
+    fprintf(stderr, "Bad pgx header, please check input file\n");\r
+    return NULL;\r
+  }\r
+\r
+  /* initialize image component */\r
+\r
+  cmptparm.x0 = parameters->image_offset_x0;\r
+  cmptparm.y0 = parameters->image_offset_y0;\r
+  cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;\r
+  cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;\r
+  \r
+  if (sign == '-') {\r
+    cmptparm.sgnd = 1;\r
+  } else {\r
+    cmptparm.sgnd = 0;\r
+  }\r
+  cmptparm.prec = prec;\r
+  cmptparm.bpp = prec;\r
+  cmptparm.dx = parameters->subsampling_dx;\r
+  cmptparm.dy = parameters->subsampling_dy;\r
+  \r
+  /* create the image */\r
+  image = opj_image_create(numcomps, &cmptparm, color_space);\r
+  if(!image) {\r
+    fclose(f);\r
+    return NULL;\r
+  }\r
+  /* set image offset and reference grid */\r
+  image->x0 = cmptparm.x0;\r
+  image->y0 = cmptparm.x0;\r
+  image->x1 = cmptparm.w;\r
+  image->y1 = cmptparm.h;\r
+\r
+  /* set image data */\r
+\r
+  comp = &image->comps[0];\r
+\r
+  for (i = 0; i < w * h; i++) {\r
+    int v;\r
+    if (comp->prec <= 8) {\r
+      if (!comp->sgnd) {\r
+        v = readuchar(f);\r
+      } else {\r
+        v = (char) readuchar(f);\r
+      }\r
+    } else if (comp->prec <= 16) {\r
+      if (!comp->sgnd) {\r
+        v = readushort(f, bigendian);\r
+      } else {\r
+        v = (short) readushort(f, bigendian);\r
+      }\r
+    } else {\r
+      if (!comp->sgnd) {\r
+        v = readuint(f, bigendian);\r
+      } else {\r
+        v = (int) readuint(f, bigendian);\r
+      }\r
+    }\r
+    if (v > max)\r
+      max = v;\r
+    comp->data[i] = v;\r
+  }\r
+  fclose(f);\r
+  comp->bpp = int_floorlog2(max) + 1;\r
+\r
+  return image;\r
+}\r
+\r
+int imagetopgx(opj_image_t * image, char *outfile) {\r
+  int w, wr, h, hr;\r
+  int i, j, compno;\r
+  FILE *fdest = NULL;\r
+\r
+  for (compno = 0; compno < image->numcomps; compno++) {\r
+    opj_image_comp_t *comp = &image->comps[compno];\r
+    char name[256];\r
+    int nbytes = 0;\r
+    char *tmp = outfile;\r
+    while (*tmp) {\r
+      tmp++;\r
+    }\r
+    while (*tmp!='.') {\r
+      tmp--;\r
+    }\r
+    *tmp='\0';\r
+\r
+    if (image->numcomps > 1) {\r
+      sprintf(name, "%s-%d.pgx", outfile, compno);\r
+    } else {\r
+      sprintf(name, "%s.pgx", outfile);\r
+    }\r
+    fdest = fopen(name, "wb");\r
+    if (!fdest) {\r
+      fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);\r
+      return 1;\r
+    }\r
+    /* w = int_ceildiv(image->x1 - image->x0, comp->dx); */\r
+    /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx); */\r
+    w = image->comps[compno].w;\r
+    wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);\r
+      \r
+    /* h = int_ceildiv(image->y1 - image->y0, comp->dy); */\r
+    /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy); */\r
+    h = image->comps[compno].h;\r
+    hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);\r
+      \r
+    fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);\r
+    if (comp->prec <= 8) {\r
+      nbytes = 1;\r
+    } else if (comp->prec <= 16) {\r
+      nbytes = 2;\r
+    } else {\r
+      nbytes = 4;\r
+    }\r
+    for (i = 0; i < wr * hr; i++) {\r
+      int v = image->comps[compno].data[i / wr * w + i % wr];\r
+      for (j = nbytes - 1; j >= 0; j--) {\r
+        char byte = (char) (v >> (j * 8));\r
+        fwrite(&byte, 1, 1, fdest);\r
+      }\r
+    }\r
+    fclose(fdest);\r
+  }\r
+\r
+  return 0;\r
+}\r
+\r
+/* -->> -->> -->> -->>\r
+\r
+PNM IMAGE FORMAT\r
+\r
+<<-- <<-- <<-- <<-- */\r
+\r
+opj_image_t* pnmtoimage(char *filename, opj_cparameters_t *parameters) {\r
+  int subsampling_dx = parameters->subsampling_dx;\r
+  int subsampling_dy = parameters->subsampling_dy;\r
+\r
+  FILE *f = NULL;\r
+  int i, compno, numcomps, w, h;\r
+  OPJ_COLOR_SPACE color_space;\r
+  opj_image_cmptparm_t cmptparm[3];  /* maximum of 3 components */\r
+  opj_image_t * image = NULL;\r
+  char value;\r
+  char comment[256];\r
+\r
+  f = fopen(filename, "rb");\r
+  if (!f) {\r
+    fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);\r
+    return 0;\r
+  }\r
+\r
+  if (fgetc(f) != 'P')\r
+    return 0;\r
+  value = fgetc(f);\r
+\r
+  switch(value) {\r
+    case '2':  /* greyscale image type */\r
+    case '5':\r
+    {\r
+      numcomps = 1;\r
+      color_space = CLRSPC_GRAY;\r
+\r
+      fgetc(f);\r
+\r
+      if (fgetc(f) == '#') {\r
+        /* skip comments */\r
+        fseek(f, 0, SEEK_SET);\r
+        if (value == '2') {\r
+          fscanf(f, "P2\n");\r
+        } else if (value == '5') {\r
+          fscanf(f, "P5\n");\r
+        }\r
+        fgets(comment, 256, f);\r
+        fscanf(f, "%d %d\n255", &w, &h);\r
+      } else {\r
+        fseek(f, 0, SEEK_SET);\r
+        if (value == '2') {\r
+          fscanf(f, "P2\n%d %d\n255", &w, &h);\r
+        } else if (value == '5') {\r
+          fscanf(f, "P5\n%d %d\n255", &w, &h);\r
+        }\r
+      }\r
+      \r
+      fgetc(f);  /* <cr><lf> */\r
+    }\r
+    break;\r
+\r
+    case '3':  /* RGB image type */\r
+    case '6':\r
+    {\r
+      numcomps = 3;\r
+      color_space = CLRSPC_SRGB;\r
+\r
+      fgetc(f);\r
+\r
+      if (fgetc(f) == '#') {\r
+        /* skip comments */\r
+        fseek(f, 0, SEEK_SET);\r
+        if (value == '3') {\r
+          fscanf(f, "P3\n");\r
+        } else if (value == '6') {\r
+          fscanf(f, "P6\n");\r
+        }\r
+        fgets(comment, 256, f);\r
+        fscanf(f, "%d %d\n255", &w, &h);\r
+      } else {\r
+        fseek(f, 0, SEEK_SET);\r
+        if (value == '3') {\r
+          fscanf(f, "P3\n%d %d\n255", &w, &h);\r
+        } else if (value == '6') {\r
+          fscanf(f, "P6\n%d %d\n255", &w, &h);\r
+        }\r
+      }\r
+      \r
+      fgetc(f);  /* <cr><lf> */\r
+    }\r
+    break;\r
+\r
+    default:\r
+      fclose(f);\r
+      return NULL;\r
+  }\r
+\r
+  /* initialize image components */\r
+  memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));\r
+  for(i = 0; i < numcomps; i++) {\r
+    cmptparm[i].prec = 8;\r
+    cmptparm[i].bpp = 8;\r
+    cmptparm[i].sgnd = 0;\r
+    cmptparm[i].dx = subsampling_dx;\r
+    cmptparm[i].dy = subsampling_dy;\r
+    cmptparm[i].w = w;\r
+    cmptparm[i].h = h;\r
+  }\r
+  /* create the image */\r
+  image = opj_image_create(numcomps, &cmptparm[0], color_space);\r
+  if(!image) {\r
+    fclose(f);\r
+    return NULL;\r
+  }\r
+\r
+  /* set image offset and reference grid */\r
+  image->x0 = parameters->image_offset_x0;\r
+  image->y0 = parameters->image_offset_y0;\r
+  image->x1 = parameters->image_offset_x0 + (w - 1) *  subsampling_dx + 1;\r
+  image->y1 = parameters->image_offset_y0 + (h - 1) *  subsampling_dy + 1;\r
+\r
+  /* set image data */\r
+\r
+  if ((value == '2') || (value == '3')) {  /* ASCII */\r
+    for (i = 0; i < w * h; i++) {\r
+      for(compno = 0; compno < numcomps; compno++) {\r
+        unsigned int index = 0;\r
+        fscanf(f, "%u", &index);\r
+        /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */\r
+        image->comps[compno].data[i] = index;\r
+      }\r
+    }\r
+  } else if ((value == '5') || (value == '6')) {  /* BINARY */\r
+    for (i = 0; i < w * h; i++) {\r
+      for(compno = 0; compno < numcomps; compno++) {\r
+        unsigned char index = 0;\r
+        fread(&index, 1, 1, f);\r
+        /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */\r
+        image->comps[compno].data[i] = index;\r
+      }\r
+    }\r
+  }\r
+\r
+  fclose(f);\r
+\r
+  return image;\r
+}\r
+\r
+int imagetopnm(opj_image_t * image, char *outfile) {\r
+  int w, wr, wrr, h, hr, hrr, max;\r
+  int i, compno;\r
+  int adjust;\r
+  FILE *fdest = NULL;\r
+  char S2;\r
+  char *tmp = outfile;\r
+\r
+  while (*tmp) {\r
+    tmp++;\r
+  }\r
+  tmp--;\r
+  tmp--;\r
+  S2 = *tmp;\r
+\r
+  if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx\r
+    && image->comps[1].dx == image->comps[2].dx\r
+    && image->comps[0].dy == image->comps[1].dy\r
+    && image->comps[1].dy == image->comps[2].dy\r
+    && image->comps[0].prec == image->comps[1].prec\r
+    && image->comps[1].prec == image->comps[2].prec\r
+    && S2 !='g' && S2 !='G') {\r
+\r
+    fdest = fopen(outfile, "wb");\r
+    if (!fdest) {\r
+      fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);\r
+      return 1;\r
+    }\r
+\r
+    w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);\r
+    /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[0].dx); */\r
+    wr = image->comps[0].w;\r
+    wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);\r
+        \r
+    h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);\r
+    /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */\r
+    hr = image->comps[0].h;\r
+    hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);\r
+      \r
+    max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;\r
+      \r
+    image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);\r
+    image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 -  int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);\r
+\r
+    fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);\r
+    adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;\r
+    for (i = 0; i < wrr * hrr; i++) {\r
+      int r, g, b;\r
+      unsigned char rc,gc,bc;\r
+      r = image->comps[0].data[i / wrr * wr + i % wrr];\r
+      r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);\r
+      rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));\r
+\r
+      g = image->comps[1].data[i / wrr * wr + i % wrr];\r
+      g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);\r
+      gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));\r
+      \r
+      b = image->comps[2].data[i / wrr * wr + i % wrr];\r
+      b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);\r
+      bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));\r
+      \r
+      fprintf(fdest, "%c%c%c", rc, gc, bc);\r
+    }\r
+    fclose(fdest);\r
+  } else {\r
+    int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;\r
+    if (image->numcomps>ncomp) {\r
+      fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");\r
+      fprintf(stderr,"           is written to the file\n");\r
+    }\r
+    for (compno = 0; compno < ncomp; compno++) {\r
+      char name[256];\r
+      if (ncomp > 1) {\r
+        sprintf(name, "%d.%s", compno, outfile);\r
+      } else {\r
+        sprintf(name, "%s", outfile);\r
+      }\r
+      \r
+      fdest = fopen(name, "wb");\r
+      if (!fdest) {\r
+        fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);\r
+        return 1;\r
+      }\r
+            \r
+      w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);\r
+      /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx); */\r
+      wr = image->comps[compno].w;\r
+      wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);\r
+      \r
+      h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);\r
+      /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy); */\r
+      hr = image->comps[compno].h;\r
+      hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);\r
+      \r
+      max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;\r
+      \r
+      image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);\r
+      image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);\r
+      \r
+      fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);\r
+      adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0;\r
+\r
+      for (i = 0; i < wrr * hrr; i++) {\r
+        int l;\r
+        unsigned char lc;\r
+        l = image->comps[compno].data[i / wrr * wr + i % wrr];\r
+        l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);\r
+        lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));\r
+        fprintf(fdest, "%c", lc);\r
+      }\r
+      fclose(fdest);\r
+    }\r
+  }\r
+\r
+  return 0;\r
+}\r