New modular version of the MJ2 library.
[openjpeg.git] / codec / convert.c
index f8c65884cf92433ca32d1af6f196a5c4992324cc..373faa123f17452159f98abeb8df98ccf8336f03 100644 (file)
@@ -76,8 +76,10 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
   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;
@@ -94,7 +96,7 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
   File_h.bfType = (getc(IN) << 8) + File_h.bfType;
 
   if (File_h.bfType != 19778) {
-    printf("Error, not a BMP file!\n");
+    fprintf(stderr,"Error, not a BMP file!\n");
     return 0;
   } else {
     /* FILE HEADER */
@@ -185,6 +187,7 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
                                                           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++) {
@@ -455,7 +458,7 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
            not_end_file = 0;
            break;
          case 2:
-           printf("No Delta supported\n");
+           fprintf(stderr,"No Delta supported\n");
            return 1;
            break;
          default:
@@ -553,105 +556,143 @@ int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
   FILE *f;
   int w, h, prec;
   int i, compno, bandno;
-  char str[256], endian[16];
-  char sign;
+  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;
     }
-    if (fscanf(f, "PG %s %c %d %d %d", endian, &sign, &prec, &w, &h) == 5) {
-      fgetc(f);
-      if (!strcmp(endian, "ML"))
-       bigendian = 1;
-      else
-       bigendian = 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);
+
+
+    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", str);
+
+      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;
       }
-      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);
        }
-       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->prec <= 16) {
+       if (!comp->sgnd) {
+         v = readushort(f, bigendian);
        } else {
-         if (!comp->sgnd) {
-           v = readuint(f, bigendian);
-         } else {
-           v = (int) readuint(f, bigendian);
-         }
+         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);
       }
-    } else {
-      return 0;
+      if (v > max)
+       max = v;
+      fprintf(src, "%d ", v);
     }
     fclose(f);
     fclose(src);
@@ -713,6 +754,7 @@ int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
       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;
@@ -756,6 +798,7 @@ int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
       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;
@@ -797,6 +840,7 @@ int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
                                                         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;
@@ -859,6 +903,7 @@ int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
                                                         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;