added tid request support to JPIP client and JPIP server quit request
[openjpeg.git] / applications / codec / convert.c
index 8c40b0acad26dea2c33a5917541f9dc41ce12c28..c7afc07439773cb684d7bfc966a2f4b88ce32c8e 100644 (file)
@@ -102,7 +102,11 @@ int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
                return 0;
        
        // Read TGA header
-       fread((unsigned char*)&tga, sizeof(tga_header), 1, fp);
+       if ( fread((unsigned char*)&tga, sizeof(tga_header), 1, fp) != 1 )
+       {
+               fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+           return 0 ;
+       }
 
        *bits_per_pixel = tga.pixel_depth;
        
@@ -113,7 +117,12 @@ int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
        if (tga.id_length)
        {
                unsigned char *id = (unsigned char *) malloc(tga.id_length);
-               fread(id, tga.id_length, 1, fp);
+               if ( !fread(id, tga.id_length, 1, fp) )
+               {
+                       fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+                       free(id);
+                   return 0 ;
+               }
                free(id);  
        }
 
@@ -149,9 +158,14 @@ int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
 
        memset(&tga, 0, sizeof(tga_header));
 
-       tga.pixel_depth = bits_per_pixel;
-       tga.image_width  = width;
-       tga.image_height = height;
+       if ( bits_per_pixel < 256 )
+               tga.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.
 
@@ -246,9 +260,25 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
                        for (x=0;x<image_width;x++) 
                        {
                                unsigned char r,g,b;
-                               fread(&b, 1, 1, f);
-                               fread(&g, 1, 1, f);
-                               fread(&r, 1, 1, f);
+
+                               if( !fread(&b, 1, 1, f) )
+                               {
+                                       fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+                                       opj_image_destroy(image);
+                                   return NULL;
+                               }
+                               if ( !fread(&g, 1, 1, f) )
+                               {
+                                       fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+                                       opj_image_destroy(image);
+                                   return NULL;
+                               }
+                               if ( !fread(&r, 1, 1, f) )
+                               {
+                                       fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+                                       opj_image_destroy(image);
+                                   return NULL;
+                               }
 
                                image->comps[0].data[index]=r;
                                image->comps[1].data[index]=g;
@@ -261,10 +291,30 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
                        for (x=0;x<image_width;x++) 
                        {
                                unsigned char r,g,b,a;
-                               fread(&b, 1, 1, f);
-                               fread(&g, 1, 1, f);
-                               fread(&r, 1, 1, f);
-                               fread(&a, 1, 1, f);
+                               if ( !fread(&b, 1, 1, f) )
+                               {
+                                       fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+                                       opj_image_destroy(image);
+                                   return NULL;
+                               }
+                               if ( !fread(&g, 1, 1, f) )
+                               {
+                                       fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+                                       opj_image_destroy(image);
+                                   return NULL;
+                               }
+                               if ( !fread(&r, 1, 1, f) )
+                               {
+                                       fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+                                       opj_image_destroy(image);
+                                   return NULL;
+                               }
+                               if ( !fread(&a, 1, 1, f) )
+                               {
+                                       fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+                                       opj_image_destroy(image);
+                                   return NULL;
+                               }
 
                                image->comps[0].data[index]=r;
                                image->comps[1].data[index]=g;
@@ -390,7 +440,8 @@ typedef struct {
   DWORD biClrImportant;                /* Number of important color (0: ALL) */
 } BITMAPINFOHEADER_t;
 
-opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) {
+opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) 
+{
        int subsampling_dx = parameters->subsampling_dx;
        int subsampling_dy = parameters->subsampling_dy;
 
@@ -407,355 +458,456 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) {
        unsigned int j, PAD = 0;
 
        int x, y, index;
-       int gray_scale = 1, not_end_file = 1; 
-
-       unsigned int line = 0, col = 0;
-       unsigned char v, v2;
+       int gray_scale = 1;
+       int has_color;
        DWORD W, H;
   
        IN = fopen(filename, "rb");
-       if (!IN) {
-               fprintf(stderr, "Failed to open %s for reading !!\n", filename);
-               return 0;
-       }
+       if (!IN) 
+   {
+       fprintf(stderr, "Failed to open %s for reading !!\n", filename);
+       return NULL;
+   }
        
        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 {
+       if (File_h.bfType != 19778) 
+   {
+       fprintf(stderr,"Error, not a BMP file!\n");
+       fclose(IN);
+       return NULL;
+   }
                /* 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.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.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.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;
+       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;
+       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;
+
+       if(Info_h.biSize != 40)
+   {
+       fprintf(stderr,"Error, unknown BMP header size %ld\n", Info_h.biSize);
+       fclose(IN);
+       return NULL;
+   }
+       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) {
-                       numcomps = 3;
-                       color_space = CLRSPC_SRGB;
-                       /* initialize image components */
-                       memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
-                       for(i = 0; i < numcomps; i++) {
-                               cmptparm[i].prec = 8;
-                               cmptparm[i].bpp = 8;
-                               cmptparm[i].sgnd = 0;
-                               cmptparm[i].dx = subsampling_dx;
-                               cmptparm[i].dy = subsampling_dy;
-                               cmptparm[i].w = w;
-                               cmptparm[i].h = h;
-                       }
-                       /* create the image */
-                       image = opj_image_create(numcomps, &cmptparm[0], color_space);
-                       if(!image) {
-                               fclose(IN);
-                               return NULL;
-                       }
 
-                       /* set image offset and reference grid */
-                       image->x0 = parameters->image_offset_x0;
-                       image->y0 = parameters->image_offset_y0;
-                       image->x1 =     !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
-                       image->y1 =     !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
+       if (Info_h.biBitCount == 24) 
+   {
+       numcomps = 3;
+       color_space = CLRSPC_SRGB;
+       /* initialize image components */
+       memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
+       for(i = 0; i < numcomps; i++) 
+  {
+       cmptparm[i].prec = 8;
+       cmptparm[i].bpp = 8;
+       cmptparm[i].sgnd = 0;
+       cmptparm[i].dx = subsampling_dx;
+       cmptparm[i].dy = subsampling_dy;
+       cmptparm[i].w = w;
+       cmptparm[i].h = h;
+  }
+       /* create the image */
+       image = opj_image_create(numcomps, &cmptparm[0], color_space);
+       if(!image) 
+  {
+       fclose(IN);
+       return NULL;
+  }
+
+       /* set image offset and reference grid */
+       image->x0 = parameters->image_offset_x0;
+       image->y0 = parameters->image_offset_y0;
+       image->x1 =     !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
+       image->y1 =     !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
 
-                       /* set image data */
+       /* set image data */
 
-                       /* Place the cursor at the beginning of the image information */
-                       fseek(IN, 0, SEEK_SET);
-                       fseek(IN, File_h.bfOffBits, SEEK_SET);
+       /* 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;
+       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;
+       /* 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));
+       RGB = (unsigned char *) 
+        malloc((3 * W + PAD) * H * sizeof(unsigned char));
                        
-                       fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
+       if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H )
+       {
+               free(RGB);
+               opj_image_destroy(image);
+               fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+           return NULL;
+       }
                        
-                       index = 0;
-
-                       for(y = 0; y < (int)H; y++) {
-                               unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
-                               for(x = 0; x < (int)W; x++) {
-                                       unsigned char *pixel = &scanline[3 * x];
-                                       image->comps[0].data[index] = pixel[2]; /* R */
-                                       image->comps[1].data[index] = pixel[1]; /* G */
-                                       image->comps[2].data[index] = pixel[0]; /* B */
-                                       index++;
-                               }
-                       }
+       index = 0;
 
-                       free(RGB);
+       for(y = 0; y < (int)H; y++) 
+  {
+       unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
+       for(x = 0; x < (int)W; x++) 
+ {
+       unsigned char *pixel = &scanline[3 * x];
+       image->comps[0].data[index] = pixel[2]; /* R */
+       image->comps[1].data[index] = pixel[1]; /* G */
+       image->comps[2].data[index] = pixel[0]; /* B */
+       index++;
+ }
+  }
+       free(RGB);
+   }/* if (Info_h.biBitCount == 24) */ 
+       else 
+       if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)//RGB 
+   {
+       if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
+       else
+       if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
 
-               } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
-                       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);
+       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));
+               
+       has_color = 0;  
+       for (j = 0; j < Info_h.biClrUsed; j++) 
+  {
+       table_B[j] = (unsigned char)getc(IN);
+       table_G[j] = (unsigned char)getc(IN);
+       table_R[j] = (unsigned char)getc(IN);
+       getc(IN);
+       has_color += 
+        !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
+  }
+       if(has_color) 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++;
+       W = Info_h.biWidth;
+       H = Info_h.biHeight;
+       if (Info_h.biWidth % 2)
+        W++;
                        
-                       numcomps = gray_scale ? 1 : 3;
-                       color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
-                       /* initialize image components */
-                       memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
-                       for(i = 0; i < numcomps; i++) {
-                               cmptparm[i].prec = 8;
-                               cmptparm[i].bpp = 8;
-                               cmptparm[i].sgnd = 0;
-                               cmptparm[i].dx = subsampling_dx;
-                               cmptparm[i].dy = subsampling_dy;
-                               cmptparm[i].w = w;
-                               cmptparm[i].h = h;
-                       }
-                       /* create the image */
-                       image = opj_image_create(numcomps, &cmptparm[0], color_space);
-                       if(!image) {
-                               fclose(IN);
-                               return NULL;
-                       }
+       numcomps = gray_scale ? 1 : 3;
+       color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
+               /* initialize image components */
+       memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
+       for(i = 0; i < numcomps; i++) 
+  {
+       cmptparm[i].prec = 8;
+       cmptparm[i].bpp = 8;
+       cmptparm[i].sgnd = 0;
+       cmptparm[i].dx = subsampling_dx;
+       cmptparm[i].dy = subsampling_dy;
+       cmptparm[i].w = w;
+       cmptparm[i].h = h;
+  }
+       /* create the image */
+       image = opj_image_create(numcomps, &cmptparm[0], color_space);
+       if(!image) 
+  {
+       fclose(IN);
+       free(table_R); free(table_G); free(table_B);
+       return NULL;
+  }
 
-                       /* set image offset and reference grid */
-                       image->x0 = parameters->image_offset_x0;
-                       image->y0 = parameters->image_offset_y0;
-                       image->x1 =     !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
-                       image->y1 =     !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
+       /* set image offset and reference grid */
+       image->x0 = parameters->image_offset_x0;
+       image->y0 = parameters->image_offset_y0;
+       image->x1 =     !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
+       image->y1 =     !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
 
-                       /* set image data */
+       /* set image data */
 
-                       RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
+       RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
                        
-                       fread(RGB, sizeof(unsigned char), W * H, IN);
-                       if (gray_scale) {
-                               index = 0;
-                               for (j = 0; j < W * H; j++) {
-                                       if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
-                                               image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
-                                               index++;
-                                       }
-                               }
+       if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H )
+       {
+               free(table_R);
+               free(table_G);
+               free(table_B);
+               free(RGB);
+               opj_image_destroy(image);
+               fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+           return NULL;
+       }
+       if (gray_scale) 
+  {
+       index = 0;
+       for (j = 0; j < W * H; j++) 
+ {
+               if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) 
+          {
+               image->comps[0].data[index] = 
+                table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]];
+               index++;
+          }
+ }
 
-                       } else {                
-                               index = 0;
-                               for (j = 0; j < W * H; j++) {
-                                       if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
-                                               unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
-                                               image->comps[0].data[index] = table_R[pixel_index];
-                                               image->comps[1].data[index] = table_G[pixel_index];
-                                               image->comps[2].data[index] = table_B[pixel_index];
-                                               index++;
-                                       }
-                               }
-                       }
-                       free(RGB);
-      free(table_R);
-      free(table_G);
-      free(table_B);
-               } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 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;
-                       }
+  } 
+       else 
+  {
+       index = 0;
+       for (j = 0; j < W * H; j++) 
+ {
+               if ((j % W < W - 1 && Info_h.biWidth % 2) 
+               || !(Info_h.biWidth % 2)) 
+          {
+               unsigned char pixel_index = 
+                RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)];
+               image->comps[0].data[index] = table_R[pixel_index];
+               image->comps[1].data[index] = table_G[pixel_index];
+               image->comps[2].data[index] = table_B[pixel_index];
+               index++;
+          }
+ }
+  }
+       free(RGB);
+       free(table_R);
+       free(table_G);
+       free(table_B);
+   }/* RGB8 */ 
+       else 
+       if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)//RLE8
+       {
+               unsigned char *pix, *beyond;
+               int *gray, *red, *green, *blue;
+               unsigned int x, y, max;
+               int i, c, c1;
+               unsigned char uc;
+
+               if (Info_h.biClrUsed == 0)
+                       Info_h.biClrUsed = 256;
+               else if (Info_h.biClrUsed > 256)
+                       Info_h.biClrUsed = 256;
+
+               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));
+
+               has_color = 0;
+               for (j = 0; j < Info_h.biClrUsed; j++)
+               {
+                       table_B[j] = (unsigned char)getc(IN);
+                       table_G[j] = (unsigned char)getc(IN);
+                       table_R[j] = (unsigned char)getc(IN);
+                       getc(IN);
+                       has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
+               }
 
-                       numcomps = gray_scale ? 1 : 3;
-                       color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
-                       /* initialize image components */
-                       memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
-                       for(i = 0; i < numcomps; i++) {
-                               cmptparm[i].prec = 8;
-                               cmptparm[i].bpp = 8;
-                               cmptparm[i].sgnd = 0;
-                               cmptparm[i].dx = subsampling_dx;
-                               cmptparm[i].dy = subsampling_dy;
-                               cmptparm[i].w = w;
-                               cmptparm[i].h = h;
-                       }
-                       /* create the image */
-                       image = opj_image_create(numcomps, &cmptparm[0], color_space);
-                       if(!image) {
-                               fclose(IN);
-                               return NULL;
-                       }
+               if (has_color)
+                       gray_scale = 0;
 
-                       /* set image offset and reference grid */
-                       image->x0 = parameters->image_offset_x0;
-                       image->y0 = parameters->image_offset_y0;
-                       image->x1 =     !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
-                       image->y1 =     !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
+               numcomps = gray_scale ? 1 : 3;
+               color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB;
+               /* initialize image components */
+               memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
+               for (i = 0; i < numcomps; i++)
+               {
+                       cmptparm[i].prec = 8;
+                       cmptparm[i].bpp = 8;
+                       cmptparm[i].sgnd = 0;
+                       cmptparm[i].dx = subsampling_dx;
+                       cmptparm[i].dy = subsampling_dy;
+                       cmptparm[i].w = w;
+                       cmptparm[i].h = h;
+               }
+               /* create the image */
+               image = opj_image_create(numcomps, &cmptparm[0], color_space);
+               if (!image)
+               {
+                       fclose(IN);
+                       free(table_R);
+                       free(table_G);
+                       free(table_B);
+                       return NULL;
+               }
 
-                       /* set image data */
-                       
-                       /* Place the cursor at the beginning of the image information */
-                       fseek(IN, 0, SEEK_SET);
-                       fseek(IN, File_h.bfOffBits, SEEK_SET);
-                       
-                       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");
-                                                       opj_image_destroy(image);
-                                                       fclose(IN);
-                                                       return NULL;
-                                               default:
-                                                       for (i = 0; i < v; i++) {
-                                                               v2 = getc(IN);
-                                                               RGB[line * Info_h.biWidth + col] = v2;
-                                                               col++;
-                                                       }
-                                                       if (v % 2)
-                                                               v2 = getc(IN);
-                                                       break;
-                                       }
-                               }
+               /* set image offset and reference grid */
+               image->x0 = parameters->image_offset_x0;
+               image->y0 = parameters->image_offset_y0;
+               image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w
+                               - 1) * subsampling_dx + 1;
+               image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h
+                               - 1) * subsampling_dy + 1;
+
+               /* set image data */
+
+               /* 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;
+               RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
+               beyond = RGB + W * H;
+               pix = beyond - W;
+               x = y = 0;
+
+               while (y < H)
+               {
+                       c = getc(IN);
+
+                       if (c)
+                       {
+                               c1 = getc(IN);
+
+                               for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
+                                       *pix = (unsigned char)c1;
                        }
-                       if (gray_scale) {
-                               index = 0;
-                               for (line = 0; line < Info_h.biHeight; line++) {
-                                       for (col = 0; col < Info_h.biWidth; col++) {
-                                               image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]];
-                                               index++;
-                                       }
+                       else
+                       {
+                               c = getc(IN);
+
+                               if (c == 0x00) /* EOL */
+                               {
+                                       x = 0;
+                                       ++y;
+                                       pix = RGB + x + (H - y - 1) * W;
                                }
-                       } else {
-                               index = 0;
-                               for (line = 0; line < Info_h.biHeight; line++) {
-                                       for (col = 0; col < Info_h.biWidth; col++) {
-                                               unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col];
-                                               image->comps[0].data[index] = table_R[pixel_index];
-                                               image->comps[1].data[index] = table_G[pixel_index];
-                                               image->comps[2].data[index] = table_B[pixel_index];
-                                               index++;
+                               else if (c == 0x01) /* EOP */
+                                       break;
+                               else if (c == 0x02) /* MOVE by dxdy */
+                               {
+                                       c = getc(IN);
+                                       x += c;
+                                       c = getc(IN);
+                                       y += c;
+                                       pix = RGB + (H - y - 1) * W + x;
+                               }
+                               else /* 03 .. 255 */
+                               {
+                                       i = 0;
+                                       for (; i < c && x < W && pix < beyond; i++, x++, pix++)
+                                       {
+                                               c1 = getc(IN);
+                                               *pix = (unsigned char)c1;
                                        }
+                                       if (c & 1) /* skip padding byte */
+                                               getc(IN);
                                }
                        }
-                       free(RGB);
-      free(table_R);
-      free(table_G);
-      free(table_B);
-       } else {
-               fprintf(stderr, 
-                       "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount);
-       }
+               }/* while() */
+
+               if (gray_scale)
+               {
+                       gray = image->comps[0].data;
+                       pix = RGB;
+                       max = W * H;
+
+                       while (max--)
+                       {
+                               uc = *pix++;
+
+                               *gray++ = table_R[uc];
+                       }
+               }
+               else
+               {
+                       //int *red, *green, *blue;
+
+                       red = image->comps[0].data;
+                       green = image->comps[1].data;
+                       blue = image->comps[2].data;
+                       pix = RGB;
+                       max = W * H;
+
+                       while (max--)
+                       {
+                               uc = *pix++;
+
+                               *red++ = table_R[uc];
+                               *green++ = table_G[uc];
+                               *blue++ = table_B[uc];
+                       }
+               }
+               free(RGB);
+               free(table_R);
+               free(table_G);
+               free(table_B);
+       }/* RLE8 */
+       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 image;
+       return image;
 }
 
 int imagetobmp(opj_image_t * image, const char *outfile) {
@@ -925,7 +1077,6 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
                }
 
                for (i = 0; i < w * h; i++) {
-                       unsigned char rc;
                        int r;
                        
                        r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
@@ -956,15 +1107,27 @@ PGX IMAGE FORMAT
 unsigned char readuchar(FILE * f)
 {
   unsigned char c1;
-  fread(&c1, 1, 1, f);
+  if ( !fread(&c1, 1, 1, f) )
+  {
+         fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+         return 0;
+  }
   return c1;
 }
 
 unsigned short readushort(FILE * f, int bigendian)
 {
   unsigned char c1, c2;
-  fread(&c1, 1, 1, f);
-  fread(&c2, 1, 1, f);
+  if ( !fread(&c1, 1, 1, f) )
+  {
+         fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+         return 0;
+  }
+  if ( !fread(&c2, 1, 1, f) )
+  {
+         fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+         return 0;
+  }
   if (bigendian)
     return (c1 << 8) + c2;
   else
@@ -974,10 +1137,26 @@ unsigned short readushort(FILE * f, int bigendian)
 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 ( !fread(&c1, 1, 1, f) )
+  {
+         fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+         return 0;
+  }
+  if ( !fread(&c2, 1, 1, f) )
+  {
+         fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+         return 0;
+  }
+  if ( !fread(&c3, 1, 1, f) )
+  {
+         fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+         return 0;
+  }
+  if ( !fread(&c4, 1, 1, f) )
+  {
+         fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+         return 0;
+  }
   if (bigendian)
     return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
   else
@@ -1013,8 +1192,11 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
        }
 
        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);
-       
+       if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
+               fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
+               return NULL;
+       }
+
        i=0;
        sign='+';               
        while (signtmp[i]!='\0') {
@@ -1118,11 +1300,11 @@ int imagetopgx(opj_image_t * image, const char *outfile) {
       name = (char*)malloc(total+1);
       }
     strncpy(name, outfile, dotpos);
-               if (image->numcomps > 1) {
-                       sprintf(name+dotpos, "-%d.pgx", compno);
-               } else {
+               //if (image->numcomps > 1) {
+                       sprintf(name+dotpos, "_%d.pgx", compno);
+               /*} else {
                        strcpy(name+dotpos, ".pgx");
-               }
+               }*/
                fdest = fopen(name, "wb");
                if (!fdest) {
                        fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
@@ -1226,7 +1408,11 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph)
     char idf[256], type[256];
     char line[256];
 
-    fgets(line, 250, reader);
+    if (fgets(line, 250, reader) == NULL)
+    {
+       fprintf(stderr,"\nWARNING: fgets return a NULL value");
+       return;
+    }
 
     if(line[0] != 'P')
    {
@@ -1481,7 +1667,8 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
     for(compno = 0; compno < numcomps; compno++)
  {
        index = 0;
-    fscanf(fp, "%u", &index);
+    if (fscanf(fp, "%u", &index) != 1)
+       fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
 
     image->comps[compno].data[i] = (index * 255)/header_info.maxval;
  }
@@ -1502,14 +1689,16 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
   {
     for(compno = 0; compno < numcomps; compno++)
  {
-        fread(&c0, 1, 1, fp);
+         if ( !fread(&c0, 1, 1, fp) )
+                 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
         if(one)
        {
         image->comps[compno].data[i] = c0;
        }
         else
        {
-        fread(&c1, 1, 1, fp);
+         if ( !fread(&c1, 1, 1, fp) )
+                 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
 /* netpbm: */
                image->comps[compno].data[i] = ((c0<<8) | c1);
        }
@@ -1523,7 +1712,8 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
   {
     unsigned int index;
 
-    fscanf(fp, "%u", &index);
+    if ( fscanf(fp, "%u", &index) != 1)
+       fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n");
 
     image->comps[0].data[i] = (index?0:255);
   }
@@ -1558,7 +1748,8 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
 
        for(i = 0; i < w * h; ++i)
   {
-       fread(&uc, 1, 1, fp);
+         if ( !fread(&uc, 1, 1, fp) )
+                 fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
        image->comps[0].data[i] = (uc & 1)?0:255;
   }
    }
@@ -1781,7 +1972,6 @@ int imagetotif(opj_image_t * image, const char *outfile)
        int width, height, imgsize;
        int bps,index,adjust, sgnd;
        int ushift, dshift, has_alpha, force16;
-       unsigned int last_i=0;
        TIFF *tif;
        tdata_t buf;
        tstrip_t strip;
@@ -1842,10 +2032,10 @@ int imagetotif(opj_image_t * image, const char *outfile)
        for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++) 
   {
        unsigned char *dat8;
-       tsize_t i, ssize;
+       tsize_t i, ssize, last_i = 0;
+  int step, restx;
        ssize = TIFFStripSize(tif);
        dat8 = (unsigned char*)buf;
-       int step, restx;
 
        if(bps == 8)
  {