Refactor code to split jpip client code from server side.
[openjpeg.git] / applications / codec / convert.c
index c7afc07439773cb684d7bfc966a2f4b88ce32c8e..73ed9d09524760e97aa867d96607763ec37e620a 100644 (file)
@@ -67,9 +67,9 @@ static int int_floorlog2(int a) {
 
  <<-- <<-- <<-- <<-- */
 
-// TGA header definition.
-#pragma pack(push,1) // Pack structure byte aligned
-typedef struct tga_header
+#ifdef INFORMATION_ONLY
+/* TGA header definition. */
+struct tga_header
 {                           
     unsigned char   id_length;              /* Image id field length    */
     unsigned char   colour_map_type;        /* Colour map type          */
@@ -89,35 +89,66 @@ typedef struct tga_header
     unsigned short  image_height;           /* Image height             */
     unsigned char   pixel_depth;            /* Pixel depth              */
     unsigned char   image_desc;             /* Image descriptor         */
-} tga_header;
-#pragma pack(pop) // Return to normal structure packing alignment.
+};
+#endif /* INFORMATION_ONLY */
+
+static unsigned short get_ushort(unsigned short val) {
+
+#ifdef ORDER_BIGENDIAN
+    return( ((val & 0xff) << 8) + (val >> 8) );
+#else
+    return( val );
+#endif
+
+}
+
+#define TGA_HEADER_SIZE 18
 
 int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, 
        unsigned int *width, unsigned int *height, int *flip_image)
 {
        int palette_size;
-       tga_header tga ;
+       unsigned char *tga ;
+       unsigned char id_len, cmap_type, image_type;
+       unsigned char pixel_depth, image_desc;
+       unsigned short cmap_index, cmap_len, cmap_entry_size;
+       unsigned short x_origin, y_origin, image_w, image_h;
 
        if (!bits_per_pixel || !width || !height || !flip_image)
                return 0;
-       
-       // Read TGA header
-       if ( fread((unsigned char*)&tga, sizeof(tga_header), 1, fp) != 1 )
+       tga = (unsigned char*)malloc(18);
+
+       if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 )
        {
                fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
            return 0 ;
        }
+       id_len = (unsigned char)tga[0];
+       cmap_type = (unsigned char)tga[1];
+       image_type = (unsigned char)tga[2];
+       cmap_index = get_ushort(*(unsigned short*)(&tga[3]));
+       cmap_len = get_ushort(*(unsigned short*)(&tga[5]));
+       cmap_entry_size = (unsigned char)tga[7];
 
-       *bits_per_pixel = tga.pixel_depth;
-       
-       *width  = tga.image_width;
-       *height = tga.image_height ;
 
-       // Ignore tga identifier, if present ...
-       if (tga.id_length)
+       x_origin = get_ushort(*(unsigned short*)(&tga[8]));
+       y_origin = get_ushort(*(unsigned short*)(&tga[10]));
+       image_w = get_ushort(*(unsigned short*)(&tga[12]));
+       image_h = get_ushort(*(unsigned short*)(&tga[14]));
+       pixel_depth = (unsigned char)tga[16];
+       image_desc  = (unsigned char)tga[17];
+
+       free(tga);
+
+       *bits_per_pixel = (unsigned int)pixel_depth;
+       *width  = (unsigned int)image_w;
+       *height = (unsigned int)image_h;
+
+       /* Ignore tga identifier, if present ... */
+       if (id_len)
        {
-               unsigned char *id = (unsigned char *) malloc(tga.id_length);
-               if ( !fread(id, tga.id_length, 1, fp) )
+               unsigned char *id = (unsigned char *) malloc(id_len);
+               if ( !fread(id, id_len, 1, fp) )
                {
                        fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
                        free(id);
@@ -126,19 +157,19 @@ int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
                free(id);  
        }
 
-       // Test for compressed formats ... not yet supported ...
+       /* Test for compressed formats ... not yet supported ...
        // Note :-  9 - RLE encoded palettized.
-       //                 10 - RLE encoded RGB.
-       if (tga.image_type > 8)
+       //                 10 - RLE encoded RGB. */
+       if (image_type > 8)
        {
                fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
                return 0 ;
        }
 
-       *flip_image = !(tga.image_desc & 32);
+       *flip_image = !(image_desc & 32);
 
-       // Palettized formats are not yet supported, skip over the palette, if present ... 
-       palette_size = tga.colour_map_length * (tga.colour_map_entry_size/8);
+       /* Palettized formats are not yet supported, skip over the palette, if present ... */
+       palette_size = cmap_len * (cmap_entry_size/8);
        
        if (palette_size>0)
        {
@@ -151,31 +182,56 @@ int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
 int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, 
        opj_bool flip_image)
 {
-       tga_header tga;
+       unsigned short image_w, image_h, us0;
+       unsigned char uc0, image_type;
+       unsigned char pixel_depth, image_desc;
 
        if (!bits_per_pixel || !width || !height)
                return 0;
 
-       memset(&tga, 0, sizeof(tga_header));
+       pixel_depth = 0;
 
        if ( bits_per_pixel < 256 )
-               tga.pixel_depth = (unsigned char)bits_per_pixel;
+               pixel_depth = (unsigned char)bits_per_pixel;
        else{
                fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header");
                return 0;
        }
-       tga.image_width  = (unsigned short)width;
-       tga.image_height = (unsigned short)height;
-       tga.image_type = 2; // Uncompressed.
-       tga.image_desc = 8; // 8 bits per component.
+       uc0 = 0;
 
-       if (flip_image)
-               tga.image_desc |= 32;
+       if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* id_length */
+       if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_type */
+
+       image_type = 2; /* Uncompressed. */
+       if(fwrite(&image_type, 1, 1, fp) != 1) goto fails;
+
+       us0 = 0;
+       if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_index */
+       if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_length */
+       if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_entry_size */
 
-       // Write TGA header
-       fwrite((unsigned char*)&tga, sizeof(tga_header), 1, fp);
+       if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* x_origin */
+       if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */
+
+       image_w = (unsigned short)width;
+       image_h = (unsigned short) height;
+
+       if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
+       if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
+
+       if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails;
+
+       image_desc = 8; /* 8 bits per component. */
+
+       if (flip_image)
+               image_desc |= 32;
+       if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails;
 
        return 1;
+
+fails:
+       fputs("\nwrite_tgaheader: write ERROR\n", stderr);
+       return 0;
 }
 
 opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
@@ -201,15 +257,15 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
        if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
                return NULL;
 
-       // We currently only support 24 & 32 bit tga's ...
+       /* We currently only support 24 & 32 bit tga's ... */
        if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
                return NULL;
 
        /* initialize image components */   
        memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
 
-       mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16);  // Mono with & without alpha.
-       save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha
+       mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16);  /* Mono with & without alpha. */
+       save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
 
        if (mono) {
                color_space = CLRSPC_GRAY;
@@ -333,12 +389,13 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
 int imagetotga(opj_image_t * image, const char *outfile) {
        int width, height, bpp, x, y;
        opj_bool write_alpha;
-       int i;
+       int i, adjustR, adjustG, adjustB;
        unsigned int alpha_channel;
        float r,g,b,a;
        unsigned char value;
        float scale;
        FILE *fdest;
+  size_t res;
 
        fdest = fopen(outfile, "wb");
        if (!fdest) {
@@ -358,10 +415,10 @@ int imagetotga(opj_image_t * image, const char *outfile) {
        width = image->comps[0].w;
        height = image->comps[0].h; 
 
-       // Mono with alpha, or RGB with alpha.
+       /* Mono with alpha, or RGB with alpha. */
        write_alpha = (image->numcomps==2) || (image->numcomps==4);   
 
-       // Write TGA header 
+       /* Write TGA header  */
        bpp = write_alpha ? 32 : 24;
        if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
                return 1;
@@ -370,35 +427,55 @@ int imagetotga(opj_image_t * image, const char *outfile) {
 
        scale = 255.0f / (float)((1<<image->comps[0].prec)-1);
 
+       adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
+       adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
+       adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
+
        for (y=0; y < height; y++) {
                unsigned int index=y*width;
 
                for (x=0; x < width; x++, index++)      {
-                       r = (float)(image->comps[0].data[index]);
+                       r = (float)(image->comps[0].data[index] + adjustR);
 
                        if (image->numcomps>2) {
-                               g = (float)(image->comps[1].data[index]);
-                               b = (float)(image->comps[2].data[index]);
+                               g = (float)(image->comps[1].data[index] + adjustG);
+                               b = (float)(image->comps[2].data[index] + adjustB);
                        }
-                       else  {// Greyscale ...
+                       else  {/* Greyscale ... */
                                g = r;
                                b = r;
                        }
 
-                       // TGA format writes BGR ...
+                       /* TGA format writes BGR ... */
                        value = (unsigned char)(b*scale);
-                       fwrite(&value,1,1,fdest);
+                       res = fwrite(&value,1,1,fdest);
+      if( res < 1 ) {
+        fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+        return 1;
+      }
 
                        value = (unsigned char)(g*scale);
-                       fwrite(&value,1,1,fdest);
+                       res = fwrite(&value,1,1,fdest);
+      if( res < 1 ) {
+        fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+        return 1;
+      }
 
                        value = (unsigned char)(r*scale);
-                       fwrite(&value,1,1,fdest);
+                       res = fwrite(&value,1,1,fdest);
+      if( res < 1 ) {
+        fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+        return 1;
+      }
 
                        if (write_alpha) {
                                a = (float)(image->comps[alpha_channel].data[index]);
                                value = (unsigned char)(a*scale);
-                               fwrite(&value,1,1,fdest);
+                               res = fwrite(&value,1,1,fdest);
+        if( res < 1 ) {
+          fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+          return 1;
+        }
                        }
                }
        }
@@ -631,7 +708,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
        free(RGB);
    }/* if (Info_h.biBitCount == 24) */ 
        else 
-       if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)//RGB 
+       if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */
    {
        if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
        else
@@ -742,7 +819,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
        free(table_B);
    }/* RGB8 */ 
        else 
-       if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)//RLE8
+       if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/
        {
                unsigned char *pix, *beyond;
                int *gray, *red, *green, *blue;
@@ -878,7 +955,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
                }
                else
                {
-                       //int *red, *green, *blue;
+                       /*int *red, *green, *blue;*/
 
                        red = image->comps[0].data;
                        green = image->comps[1].data;
@@ -1288,6 +1365,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) {
                char bname[256]; /* buffer for name */
     char *name = bname; /* pointer */
     int nbytes = 0;
+    size_t res;
     const size_t olen = strlen(outfile);
     const size_t dotpos = olen - 4;
     const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
@@ -1300,7 +1378,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) {
       name = (char*)malloc(total+1);
       }
     strncpy(name, outfile, dotpos);
-               //if (image->numcomps > 1) {
+               /*if (image->numcomps > 1) {*/
                        sprintf(name+dotpos, "_%d.pgx", compno);
                /*} else {
                        strcpy(name+dotpos, ".pgx");
@@ -1330,7 +1408,11 @@ int imagetopgx(opj_image_t * image, const char *outfile) {
                        int v = image->comps[compno].data[i];
                        for (j = nbytes - 1; j >= 0; j--) {
                                char byte = (char) (v >> (j * 8));
-                               fwrite(&byte, 1, 1, fdest);
+                               res = fwrite(&byte, 1, 1, fdest);
+        if( res < 1 ) {
+          fprintf(stderr, "failed to write 1 byte for %s\n", name);
+          return 1;
+        }
                        }
                }
                fclose(fdest);
@@ -1742,7 +1824,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
   }
    }
        else
-       if((format == 7 && header_info.bw)) //MONO
+       if((format == 7 && header_info.bw)) /*MONO*/
    {
        unsigned char uc;
 
@@ -1769,7 +1851,7 @@ int imagetopnm(opj_image_t * image, const char *outfile)
        FILE *fdest = NULL;
        const char *tmp = outfile;
        char *destname;
-
+  alpha = NULL;
     if((prec = image->comps[0].prec) > 16)
    {
        fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
@@ -1831,7 +1913,7 @@ int imagetopnm(opj_image_t * image, const char *outfile)
   {
        fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n", 
                opj_version(), wr, hr, max);
-       alpha = NULL; adjustA = 0;
+       adjustA = 0;
   }
     adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
 
@@ -1958,13 +2040,13 @@ int imagetopnm(opj_image_t * image, const char *outfile)
  <<-- <<-- <<-- <<-- */
 
 typedef struct tiff_infoheader{
-       DWORD tiWidth;  // Width of Image in pixel
-       DWORD tiHeight; // Height of Image in pixel
-       DWORD tiPhoto;  // Photometric
-       WORD  tiBps;    // Bits per sample
-       WORD  tiSf;             // Sample Format
-       WORD  tiSpp;    // Sample per pixel 1-bilevel,gray scale , 2- RGB
-       WORD  tiPC;     // Planar config (1-Interleaved, 2-Planarcomp)
+       DWORD tiWidth;  /* Width of Image in pixel*/
+       DWORD tiHeight; /* Height of Image in pixel */
+       DWORD tiPhoto;  /* Photometric */
+       WORD  tiBps;      /* Bits per sample */
+       WORD  tiSf;               /* Sample Format */
+       WORD  tiSpp;      /* Sample per pixel 1-bilevel,gray scale , 2- RGB */
+       WORD  tiPC;         /* Planar config (1-Interleaved, 2-Planarcomp) */
 }tiff_infoheader_t;
 
 int imagetotif(opj_image_t * image, const char *outfile) 
@@ -2070,7 +2152,7 @@ int imagetotif(opj_image_t * image, const char *outfile)
          }
                else
                 break;
-          }//for(i = 0;)
+          }/*for(i = 0;)*/
 
                if(last_i < ssize)
           {
@@ -2103,10 +2185,10 @@ int imagetotif(opj_image_t * image, const char *outfile)
         }
                else
                 break;
-         }//for(i)
-          }//if(last_i < ssize)
+         }/*for(i)*/
+          }/*if(last_i < ssize)*/
 
- }     //if(bps == 8)
+ }     /*if(bps == 8)*/
        else 
        if(bps == 16)
  {
@@ -2138,8 +2220,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
                b = (b<<ushift) + (b>>dshift); 
                if(has_alpha) a = (a<<ushift) + (a>>dshift);
         }
-               dat8[i+0] =  r;//LSB
-               dat8[i+1] = (r >> 8);//MSB
+               dat8[i+0] =  r;/*LSB*/
+               dat8[i+1] = (r >> 8);/*MSB*/
                dat8[i+2] =  g;
                dat8[i+3] = (g >> 8);
                dat8[i+4] =  b;
@@ -2154,7 +2236,7 @@ int imagetotif(opj_image_t * image, const char *outfile)
          }
                else
                 break;
-          }//for(i = 0;)
+          }/*for(i = 0;)*/
 
                if(last_i < ssize)
           {
@@ -2183,8 +2265,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
            b = (b<<ushift) + (b>>dshift);
            if(has_alpha) a = (a<<ushift) + (a>>dshift);
        }
-               dat8[i+0] =  r;//LSB
-               if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;//MSB
+               dat8[i+0] =  r;/*LSB*/
+               if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;/*MSB*/
                if(i+2 < ssize) dat8[i+2] =  g;      else break;
                if(i+3 < ssize) dat8[i+3] = (g >> 8);else break;
                if(i+4 < ssize) dat8[i+4] =  b;      else break;
@@ -2199,18 +2281,18 @@ int imagetotif(opj_image_t * image, const char *outfile)
         }
                else
                 break;
-         }//for(i)
-          }//if(last_i < ssize)
+         }/*for(i)*/
+          }/*if(last_i < ssize)*/
 
- }//if(bps == 16)
+ }/*if(bps == 16)*/
        (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
-  }//for(strip = 0; )
+  }/*for(strip = 0; )*/
 
        _TIFFfree((void*)buf);
        TIFFClose(tif);
 
        return 0;
-   }//RGB(A)
+   }/*RGB(A)*/
 
        if(image->numcomps == 1 /* GRAY */
        || (   image->numcomps == 2 /* GRAY_ALPHA */
@@ -2271,8 +2353,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
         }
                else
                 break;
-         }//for(i )
- }//if(bps == 8
+         }/*for(i )*/
+ }/*if(bps == 8*/
        else 
        if(bps == 16)
  {
@@ -2297,21 +2379,21 @@ int imagetotif(opj_image_t * image, const char *outfile)
                r = (r<<ushift) + (r>>dshift);
                if(has_alpha) a = (a<<ushift) + (a>>dshift);
         }
-               dat8[i+0] = r;//LSB
-               dat8[i+1] = r >> 8;//MSB
+               dat8[i+0] = r;/*LSB*/
+               dat8[i+1] = r >> 8;/*MSB*/
                if(has_alpha)
         {
                dat8[i+2] = a;
                dat8[i+3] = a >> 8;
         }
                index++;
-         }//if(index < imgsize)
+         }/*if(index < imgsize)*/
                else
                 break;
-          }//for(i )
+          }/*for(i )*/
  }
        (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
-  }//for(strip
+  }/*for(strip*/
 
        _TIFFfree(buf);
        TIFFClose(tif);
@@ -2481,9 +2563,9 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
           {
                if(index < imgsize)
          {
-               image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R 
-               image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G 
-               image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B 
+               image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */
+               image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */
+               image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */
                if(has_alpha)
                 image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
 
@@ -2505,8 +2587,8 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
          }
                else
                 break;
-          }//for(i = 0)
- }//if(Info.tiBps == 16)
+          }/*for(i = 0)*/
+ }/*if(Info.tiBps == 16)*/
        else 
        if(Info.tiBps == 8)
  {
@@ -2516,9 +2598,9 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
           {
                if(index < imgsize)
          {
-               image->comps[0].data[index] = dat8[i+0];// R 
-               image->comps[1].data[index] = dat8[i+1];// G 
-               image->comps[2].data[index] = dat8[i+2];// B 
+               image->comps[0].data[index] = dat8[i+0];/* R */
+               image->comps[1].data[index] = dat8[i+1];/* G */
+               image->comps[2].data[index] = dat8[i+2];/* B */
                if(has_alpha)
                 image->comps[3].data[index] = dat8[i+3];
 
@@ -2533,11 +2615,11 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
                 image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
         }
                index++;
-         }//if(index
+         }/*if(index*/
                else
                 break;
-          }//for(i )
- }//if( Info.tiBps == 8)
+          }/*for(i )*/
+ }/*if( Info.tiBps == 8)*/
        else
        if(Info.tiBps == 12)/* CINEMA file */
  {
@@ -2560,15 +2642,15 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
          }
                else
                 break;
-          }//for(i )
+          }/*for(i )*/
  }
-  }//for(strip = 0; )
+  }/*for(strip = 0; )*/
 
        _TIFFfree(buf);
        TIFFClose(tif);
 
        return image;
-   }//RGB(A)
+   }/*RGB(A)*/
 
        if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
    {
@@ -2631,7 +2713,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
         }
                else
                 break;
-         }//for(i )
+         }/*for(i )*/
           }
                else 
                if(Info.tiBps == 8)
@@ -2649,14 +2731,14 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
         }
                else
                 break;
-         }//for(i )
+         }/*for(i )*/
           }
-  }//for(strip = 0;
+  }/*for(strip = 0;*/
 
        _TIFFfree(buf);
        TIFFClose(tif);
 
-   }//GRAY(A)
+   }/*GRAY(A)*/
 
        return image;
 
@@ -2775,6 +2857,7 @@ opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw
 int imagetoraw(opj_image_t * image, const char *outfile)
 {
        FILE *rawFile = NULL;
+  size_t res;
        int compno;
        int w, h;
        int line, row;
@@ -2812,7 +2895,11 @@ int imagetoraw(opj_image_t * image, const char *outfile)
                                for (line = 0; line < h; line++) {
                                        for(row = 0; row < w; row++)    {                               
                                                curr = (signed char) (*ptr & mask);
-                                               fwrite(&curr, sizeof(signed char), 1, rawFile);
+                                               res = fwrite(&curr, sizeof(signed char), 1, rawFile);
+            if( res < 1 ) {
+              fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+              return 1;
+            }
                                                ptr++;
                                        }
                                }
@@ -2825,7 +2912,11 @@ int imagetoraw(opj_image_t * image, const char *outfile)
                                for (line = 0; line < h; line++) {
                                        for(row = 0; row < w; row++)    {       
                                                curr = (unsigned char) (*ptr & mask);
-                                               fwrite(&curr, sizeof(unsigned char), 1, rawFile);
+                                               res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
+            if( res < 1 ) {
+              fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+              return 1;
+            }
                                                ptr++;
                                        }
                                }
@@ -2843,9 +2934,17 @@ int imagetoraw(opj_image_t * image, const char *outfile)
                                                unsigned char temp;
                                                curr = (signed short int) (*ptr & mask);
                                                temp = (unsigned char) (curr >> 8);
-                                               fwrite(&temp, 1, 1, rawFile);
+                                               res = fwrite(&temp, 1, 1, rawFile);
+            if( res < 1 ) {
+              fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+              return 1;
+            }
                                                temp = (unsigned char) curr;
-                                               fwrite(&temp, 1, 1, rawFile);
+                                               res = fwrite(&temp, 1, 1, rawFile);
+            if( res < 1 ) {
+              fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+              return 1;
+            }
                                                ptr++;
                                        }
                                }
@@ -2860,9 +2959,17 @@ int imagetoraw(opj_image_t * image, const char *outfile)
                                                unsigned char temp;
                                                curr = (unsigned short int) (*ptr & mask);
                                                temp = (unsigned char) (curr >> 8);
-                                               fwrite(&temp, 1, 1, rawFile);
+                                               res = fwrite(&temp, 1, 1, rawFile);
+            if( res < 1 ) {
+              fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+              return 1;
+            }
                                                temp = (unsigned char) curr;
-                                               fwrite(&temp, 1, 1, rawFile);
+                                               res = fwrite(&temp, 1, 1, rawFile);
+            if( res < 1 ) {
+              fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
+              return 1;
+            }
                                                ptr++;
                                        }
                                }