[trunk] Fix issue with & vs &&
[openjpeg.git] / src / bin / jp2 / convert.c
index 23a3aecc0f81e9514d51cbdc3608e1adb1b3c3de..3588700b75e89bebf76ab517e8c5ec8f7624f59a 100644 (file)
@@ -183,7 +183,7 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
 
 #if WORDS_BIGENDIAN == 1
 
-static inline int16_t swap16(int16_t x)
+static INLINE int16_t swap16(int16_t x)
 {
     return((((u_int16_t)x & 0x00ffU) <<  8) |
            (((u_int16_t)x & 0xff00U) >>  8));
@@ -302,23 +302,23 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
         cmptparm[i].prec = 8;
         cmptparm[i].bpp = 8;
         cmptparm[i].sgnd = 0;
-        cmptparm[i].dx = subsampling_dx;
-        cmptparm[i].dy = subsampling_dy;
+        cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
+        cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
         cmptparm[i].w = image_width;
         cmptparm[i].h = image_height;
     }
 
     /* create the image */
-    image = opj_image_create(numcomps, &cmptparm[0], color_space);
+    image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
 
     if (!image)
         return NULL;
 
     /* set image offset and reference grid */
-    image->x0 = parameters->image_offset_x0;
-    image->y0 = parameters->image_offset_y0;
-    image->x1 =        !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1;
-    image->y1 =        !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1;
+    image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
+    image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
+    image->x1 =        !image->x0 ? (OPJ_UINT32)(image_width - 1)  * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(image_width - 1)  * (OPJ_UINT32)subsampling_dx + 1;
+    image->y1 =        !image->y0 ? (OPJ_UINT32)(image_height - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(image_height - 1) * (OPJ_UINT32)subsampling_dy + 1;
 
     /* set image data */
     for (y=0; y < image_height; y++)
@@ -326,9 +326,9 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
         int index;
 
         if (flip_image)
-            index = (image_height-y-1)*image_width;
+            index = (int)((image_height-y-1)*image_width);
         else
-            index = y*image_width;
+            index = (int)(y*image_width);
 
         if (numcomps==3)
         {
@@ -408,13 +408,15 @@ 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, adjustR, adjustG, adjustB, fails;
+    unsigned int i;
+    int adjustR, adjustG, adjustB, fails;
     unsigned int alpha_channel;
     float r,g,b,a;
     unsigned char value;
     float scale;
     FILE *fdest;
     size_t res;
+    fails = 1;
 
     fdest = fopen(outfile, "wb");
     if (!fdest) {
@@ -431,8 +433,8 @@ int imagetotga(opj_image_t * image, const char *outfile) {
         }
     }
 
-    width image->comps[0].w;
-    height = image->comps[0].h;
+    width  = (int)image->comps[0].w;
+    height = (int)image->comps[0].h;
 
     /* Mono with alpha, or RGB with alpha. */
     write_alpha = (image->numcomps==2) || (image->numcomps==4);
@@ -453,7 +455,7 @@ int imagetotga(opj_image_t * image, const char *outfile) {
 
        for (y=0; y < height; y++) 
    {
-       unsigned int index=y*width;
+       unsigned int index= (unsigned int)(y*width);
 
        for (x=0; x < width; x++, index++)      
   {
@@ -572,7 +574,8 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
     unsigned char *table_R, *table_G, *table_B;
     unsigned int j, PAD = 0;
 
-    int x, y, index;
+    unsigned int x, y;
+    int index;
     int gray_scale = 1;
     int has_color;
     DWORD W, H;
@@ -595,10 +598,10 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
     }
     /* 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 = (DWORD)getc(IN);
+    File_h.bfSize = (DWORD)(getc(IN) << 8) + File_h.bfSize;
+    File_h.bfSize = (DWORD)(getc(IN) << 16) + File_h.bfSize;
+    File_h.bfSize = (DWORD)(getc(IN) << 24) + File_h.bfSize;
 
     File_h.bfReserved1 = (WORD)getc(IN);
     File_h.bfReserved1 = (WORD)((getc(IN) << 8) + File_h.bfReserved1);
@@ -606,18 +609,18 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
     File_h.bfReserved2 = (WORD)getc(IN);
     File_h.bfReserved2 = (WORD)((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 = (DWORD)getc(IN);
+    File_h.bfOffBits = (DWORD)(getc(IN) << 8) + File_h.bfOffBits;
+    File_h.bfOffBits = (DWORD)(getc(IN) << 16) + File_h.bfOffBits;
+    File_h.bfOffBits = (DWORD)(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.biSize = (DWORD)getc(IN);
+    Info_h.biSize = (DWORD)(getc(IN) << 8) + Info_h.biSize;
+    Info_h.biSize = (DWORD)(getc(IN) << 16) + Info_h.biSize;
+    Info_h.biSize = (DWORD)(getc(IN) << 24) + Info_h.biSize;
 
     if(Info_h.biSize != 40)
     {
@@ -625,17 +628,17 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
         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.biWidth = (DWORD)getc(IN);
+    Info_h.biWidth = (DWORD)(getc(IN) << 8) + Info_h.biWidth;
+    Info_h.biWidth = (DWORD)(getc(IN) << 16) + Info_h.biWidth;
+    Info_h.biWidth = (DWORD)(getc(IN) << 24) + Info_h.biWidth;
+    w = (int)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.biHeight = (DWORD)getc(IN);
+    Info_h.biHeight = (DWORD)(getc(IN) << 8) + Info_h.biHeight;
+    Info_h.biHeight = (DWORD)(getc(IN) << 16) + Info_h.biHeight;
+    Info_h.biHeight = (DWORD)(getc(IN) << 24) + Info_h.biHeight;
+    h = (int)Info_h.biHeight;
 
     Info_h.biPlanes = (WORD)getc(IN);
     Info_h.biPlanes = (WORD)((getc(IN) << 8) + Info_h.biPlanes);
@@ -643,35 +646,35 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
     Info_h.biBitCount = (WORD)getc(IN);
     Info_h.biBitCount = (WORD)((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.biCompression = (DWORD)getc(IN);
+    Info_h.biCompression = (DWORD)(getc(IN) << 8) + Info_h.biCompression;
+    Info_h.biCompression = (DWORD)(getc(IN) << 16) + Info_h.biCompression;
+    Info_h.biCompression = (DWORD)(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.biSizeImage = (DWORD)getc(IN);
+    Info_h.biSizeImage = (DWORD)(getc(IN) << 8) + Info_h.biSizeImage;
+    Info_h.biSizeImage = (DWORD)(getc(IN) << 16) + Info_h.biSizeImage;
+    Info_h.biSizeImage = (DWORD)(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.biXpelsPerMeter = (DWORD)getc(IN);
+    Info_h.biXpelsPerMeter = (DWORD)(getc(IN) << 8) + Info_h.biXpelsPerMeter;
+    Info_h.biXpelsPerMeter = (DWORD)(getc(IN) << 16) + Info_h.biXpelsPerMeter;
+    Info_h.biXpelsPerMeter = (DWORD)(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.biYpelsPerMeter = (DWORD)getc(IN);
+    Info_h.biYpelsPerMeter = (DWORD)(getc(IN) << 8) + Info_h.biYpelsPerMeter;
+    Info_h.biYpelsPerMeter = (DWORD)(getc(IN) << 16) + Info_h.biYpelsPerMeter;
+    Info_h.biYpelsPerMeter = (DWORD)(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.biClrUsed = (DWORD)getc(IN);
+    Info_h.biClrUsed = (DWORD)(getc(IN) << 8) + Info_h.biClrUsed;
+    Info_h.biClrUsed = (DWORD)(getc(IN) << 16) + Info_h.biClrUsed;
+    Info_h.biClrUsed = (DWORD)(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.biClrImportant = (DWORD)getc(IN);
+    Info_h.biClrImportant = (DWORD)(getc(IN) << 8) + Info_h.biClrImportant;
+    Info_h.biClrImportant = (DWORD)(getc(IN) << 16) + Info_h.biClrImportant;
+    Info_h.biClrImportant = (DWORD)(getc(IN) << 24) + Info_h.biClrImportant;
 
     /* Read the data and store them in the OUT file */
 
@@ -686,13 +689,13 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
             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;
+            cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
+            cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
+            cmptparm[i].w = (OPJ_UINT32)w;
+            cmptparm[i].h = (OPJ_UINT32)h;
         }
         /* create the image */
-        image = opj_image_create(numcomps, &cmptparm[0], color_space);
+        image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
         if(!image)
         {
             fclose(IN);
@@ -700,16 +703,16 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
         }
 
         /* 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;
+        image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
+        image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
+        image->x1 =    !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
+        image->y1 =    !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)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);
+        fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
 
         W = Info_h.biWidth;
         H = Info_h.biHeight;
@@ -731,10 +734,10 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
 
         index = 0;
 
-        for(y = 0; y < (int)H; y++)
+        for(y = 0; y < H; y++)
         {
-            unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y);
-            for(x = 0; x < (int)W; x++)
+            unsigned char *scanline = RGB + (3 * (unsigned int)W + PAD) * ((unsigned int)H - 1 - (unsigned int)y);
+            for(x = 0; x < W; x++)
             {
                 unsigned char *pixel = &scanline[3 * x];
                 image->comps[0].data[index] = pixel[2];        /* R */
@@ -770,7 +773,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
 
             /* Place the cursor at the beginning of the image information */
             fseek(IN, 0, SEEK_SET);
-            fseek(IN, File_h.bfOffBits, SEEK_SET);
+            fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
 
             W = Info_h.biWidth;
             H = Info_h.biHeight;
@@ -786,13 +789,13 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
                 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;
+                cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
+                cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
+                cmptparm[i].w = (OPJ_UINT32)w;
+                cmptparm[i].h = (OPJ_UINT32)h;
             }
             /* create the image */
-            image = opj_image_create(numcomps, &cmptparm[0], color_space);
+            image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
             if(!image)
             {
                 fclose(IN);
@@ -801,10 +804,10 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
             }
 
             /* 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;
+            image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
+            image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
+            image->x1 =        !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
+            image->y1 =        !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
 
             /* set image data */
 
@@ -861,8 +864,8 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
             {
                 unsigned char *pix, *beyond;
                 int *gray, *red, *green, *blue;
-                unsigned int x, y, max;
-                int i, c, c1;
+                unsigned int max;
+                int c, c1;
                 unsigned char uc;
 
                 if (Info_h.biClrUsed == 0)
@@ -896,13 +899,13 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
                     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;
+                    cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
+                    cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
+                    cmptparm[i].w = (OPJ_UINT32)w;
+                    cmptparm[i].h = (OPJ_UINT32)h;
                 }
                 /* create the image */
-                image = opj_image_create(numcomps, &cmptparm[0], color_space);
+                image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
                 if (!image)
                 {
                     fclose(IN);
@@ -913,18 +916,16 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
                 }
 
                 /* 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;
+                image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
+                image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
+                image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
+                image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)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);
+                fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
 
                 W = Info_h.biWidth;
                 H = Info_h.biHeight;
@@ -959,9 +960,9 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
                         else if (c == 0x02) /* MOVE by dxdy */
                         {
                             c = getc(IN);
-                            x += c;
+                            x += (unsigned int)c;
                             c = getc(IN);
-                            y += c;
+                            y += (unsigned int)c;
                             pix = RGB + (H - y - 1) * W + x;
                         }
                         else /* 03 .. 255 */
@@ -1052,8 +1053,8 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
             return 1;
         }
 
-        w = image->comps[0].w;
-        h = image->comps[0].h;
+        w = (int)image->comps[0].w;
+        h = (int)image->comps[0].h;
 
         fprintf(fdest, "BM");
 
@@ -1091,19 +1092,19 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
         fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
 
         if (image->comps[0].prec > 8) {
-            adjustR = image->comps[0].prec - 8;
+            adjustR = (int)image->comps[0].prec - 8;
             printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
         }
         else
             adjustR = 0;
         if (image->comps[1].prec > 8) {
-            adjustG = image->comps[1].prec - 8;
+            adjustG = (int)image->comps[1].prec - 8;
             printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
         }
         else
             adjustG = 0;
         if (image->comps[2].prec > 8) {
-            adjustB = image->comps[2].prec - 8;
+            adjustB = (int)image->comps[2].prec - 8;
             printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
         }
         else
@@ -1146,8 +1147,8 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
         <<-- <<-- <<-- <<-- */
 
         fdest = fopen(outfile, "wb");
-        w = image->comps[0].w;
-        h = image->comps[0].h;
+        w = (int)image->comps[0].w;
+        h = (int)image->comps[0].h;
 
         fprintf(fdest, "BM");
 
@@ -1186,7 +1187,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
         fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
 
         if (image->comps[0].prec > 8) {
-            adjustR = image->comps[0].prec - 8;
+            adjustR = (int)image->comps[0].prec - 8;
             printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
         }else
             adjustR = 0;
@@ -1277,9 +1278,9 @@ static unsigned int readuint(FILE * f, int bigendian)
         return 0;
     }
     if (bigendian)
-        return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4;
+        return (unsigned int)(c1 << 24) + (unsigned int)(c2 << 16) + (unsigned int)(c3 << 8) + c4;
     else
-        return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1;
+        return (unsigned int)(c4 << 24) + (unsigned int)(c3 << 16) + (unsigned int)(c2 << 8) + c1;
 }
 
 opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
@@ -1336,10 +1337,10 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
 
     /* initialize image component */
 
-    cmptparm.x0 = parameters->image_offset_x0;
-    cmptparm.y0 = parameters->image_offset_y0;
-    cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1;
-    cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1;
+    cmptparm.x0 = (OPJ_UINT32)parameters->image_offset_x0;
+    cmptparm.y0 = (OPJ_UINT32)parameters->image_offset_y0;
+    cmptparm.w = !cmptparm.x0 ? (OPJ_UINT32)((w - 1) * parameters->subsampling_dx + 1) : cmptparm.x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)parameters->subsampling_dx + 1;
+    cmptparm.h = !cmptparm.y0 ? (OPJ_UINT32)((h - 1) * parameters->subsampling_dy + 1) : cmptparm.y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)parameters->subsampling_dy + 1;
 
     if (sign == '-') {
         cmptparm.sgnd = 1;
@@ -1356,13 +1357,13 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
     }
     else ushift = dshift = force8 = adjustS = 0;
 
-    cmptparm.prec = prec;
-    cmptparm.bpp = prec;
-    cmptparm.dx = parameters->subsampling_dx;
-    cmptparm.dy = parameters->subsampling_dy;
+    cmptparm.prec = (OPJ_UINT32)prec;
+    cmptparm.bpp = (OPJ_UINT32)prec;
+    cmptparm.dx = (OPJ_UINT32)parameters->subsampling_dx;
+    cmptparm.dy = (OPJ_UINT32)parameters->subsampling_dy;
 
     /* create the image */
-    image = opj_image_create(numcomps, &cmptparm, color_space);
+    image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm, color_space);
     if(!image) {
         fclose(f);
         return NULL;
@@ -1403,7 +1404,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
             }
         } else {
             if (!comp->sgnd) {
-                v = readuint(f, bigendian);
+                v = (int)readuint(f, bigendian);
             } else {
                 v = (int) readuint(f, bigendian);
             }
@@ -1413,14 +1414,14 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
         comp->data[i] = v;
     }
     fclose(f);
-    comp->bpp = int_floorlog2(max) + 1;
+    comp->bpp = (OPJ_UINT32)int_floorlog2(max) + 1;
 
     return image;
 }
 
 #define CLAMP(x,a,b) x < a ? a : (x > b ? b : x)
 
-static inline int clamp( const int value, const int prec, const int sgnd )
+static INLINE int clamp( const int value, const int prec, const int sgnd )
 {
   if( sgnd )
     {
@@ -1432,14 +1433,15 @@ static inline int clamp( const int value, const int prec, const int sgnd )
     {
     if (prec <= 8)       return CLAMP(value,0,255);
     else if (prec <= 16) return CLAMP(value,0,65535);
-    else                 return CLAMP(value,0,4294967295);
+    else                 return value; /*CLAMP(value,0,4294967295);*/
     }
 }
 
 int imagetopgx(opj_image_t * image, const char *outfile) 
 {
   int w, h;
-  int i, j, compno, fails = 1;
+  int i, j, fails = 1;
+  unsigned int compno;
   FILE *fdest = NULL;
 
   for (compno = 0; compno < image->numcomps; compno++) 
@@ -1474,8 +1476,8 @@ int imagetopgx(opj_image_t * image, const char *outfile)
       goto fin;
       }
 
-    w = image->comps[compno].w;
-    h = image->comps[compno].h;
+    w = (int)image->comps[compno].w;
+    h = (int)image->comps[compno].h;
 
     fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
       w, h);
@@ -1491,7 +1493,7 @@ int imagetopgx(opj_image_t * image, const char *outfile)
       {
       /* FIXME: clamp func is being called within a loop */
       const int val = clamp(image->comps[compno].data[i],
-        comp->prec, comp->sgnd);
+        (int)comp->prec, (int)comp->sgnd);
 
       for (j = nbytes - 1; j >= 0; j--) 
         {
@@ -1714,7 +1716,7 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph)
         }
         if(ph->depth < 1 || ph->depth > 4) return;
 
-        if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
+        if(ph->width && ph->height && ph->depth && ph->maxval && ttype)
             ph->ok = 1;
     }
     else
@@ -1812,27 +1814,27 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
     subsampling_dx = parameters->subsampling_dx;
     subsampling_dy = parameters->subsampling_dy;
 
-    memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
+    memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t));
 
     for(i = 0; i < numcomps; i++)
     {
-        cmptparm[i].prec = prec;
-        cmptparm[i].bpp = prec;
+        cmptparm[i].prec = (OPJ_UINT32)prec;
+        cmptparm[i].bpp = (OPJ_UINT32)prec;
         cmptparm[i].sgnd = 0;
-        cmptparm[i].dx = subsampling_dx;
-        cmptparm[i].dy = subsampling_dy;
-        cmptparm[i].w = w;
-        cmptparm[i].h = h;
+        cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
+        cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
+        cmptparm[i].w = (OPJ_UINT32)w;
+        cmptparm[i].h = (OPJ_UINT32)h;
     }
-    image = opj_image_create(numcomps, &cmptparm[0], color_space);
+    image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
 
     if(!image) { fclose(fp); return NULL; }
 
     /* set image offset and reference grid */
-    image->x0 = parameters->image_offset_x0;
-    image->y0 = parameters->image_offset_y0;
-    image->x1 = parameters->image_offset_x0 + (w - 1) *        subsampling_dx + 1;
-    image->y1 = parameters->image_offset_y0 + (h - 1) *        subsampling_dy + 1;
+    image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
+    image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
+    image->x1 = (OPJ_UINT32)(parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1);
+    image->y1 = (OPJ_UINT32)(parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1);
 
     if((format == 2) || (format == 3)) /* ascii pixmap */
     {
@@ -1846,7 +1848,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
                 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;
+                image->comps[compno].data[i] = (OPJ_INT32)(index * 255)/header_info.maxval;
             }
         }
     }
@@ -1938,7 +1940,8 @@ int imagetopnm(opj_image_t * image, const char *outfile)
 {
     int *red, *green, *blue, *alpha;
     int wr, hr, max;
-    int i, compno, ncomp;
+    int i;
+    unsigned int compno, ncomp;
     int adjustR, adjustG, adjustB, adjustA;
     int fails, two, want_gray, has_alpha, triple;
     int prec, v;
@@ -1948,7 +1951,7 @@ int imagetopnm(opj_image_t * image, const char *outfile)
 
        alpha = NULL;
 
-    if((prec = image->comps[0].prec) > 16)
+    if((prec = (int)image->comps[0].prec) > 16)
     {
         fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
                 "\n\t: refused.\n",__FILE__,__LINE__,prec);
@@ -1982,7 +1985,7 @@ int imagetopnm(opj_image_t * image, const char *outfile)
         }
         two = (prec > 8);
         triple = (ncomp > 2);
-        wr = image->comps[0].w; hr = image->comps[0].h;
+        wr = (int)image->comps[0].w; hr = (int)image->comps[0].h;
         max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2);
 
         red = image->comps[0].data;
@@ -2097,8 +2100,15 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
 
     for (compno = 0; compno < ncomp; compno++)
     {
-        if (ncomp > 1)
-            sprintf(destname, "%d.%s", compno, outfile);
+    if (ncomp > 1)
+      {
+      /*sprintf(destname, "%d.%s", compno, outfile);*/
+      const size_t olen = strlen(outfile);
+      const size_t dotpos = olen - 4;
+
+      strncpy(destname, outfile, dotpos);
+      sprintf(destname+dotpos, "_%d.pgm", compno);
+      }
         else
             sprintf(destname, "%s", outfile);
 
@@ -2109,8 +2119,8 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
             free(destname);
             return 1;
         }
-        wr = image->comps[compno].w; hr = image->comps[compno].h;
-        prec = image->comps[compno].prec;
+        wr = (int)image->comps[compno].w; hr = (int)image->comps[compno].h;
+        prec = (int)image->comps[compno].prec;
         max = (1<<prec) - 1;
 
         fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
@@ -2175,7 +2185,7 @@ int imagetotif(opj_image_t * image, const char *outfile)
     tsize_t strip_size;
 
     ushift = dshift = force16 = has_alpha = 0;
-    bps = image->comps[0].prec;
+    bps = (int)image->comps[0].prec;
 
     if(bps > 8 && bps < 16)
     {
@@ -2197,7 +2207,7 @@ int imagetotif(opj_image_t * image, const char *outfile)
         fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile);
         return 1;
     }
-    sgnd = image->comps[0].sgnd;
+    sgnd = (int)image->comps[0].sgnd;
     adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
 
     if(image->numcomps >= 3
@@ -2210,8 +2220,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
     {
         has_alpha = (image->numcomps == 4);
 
-        width   = image->comps[0].w;
-        height  = image->comps[0].h;
+        width   = (int)image->comps[0].w;
+        height  = (int)image->comps[0].h;
         imgsize = width * height ;
 
         TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
@@ -2442,8 +2452,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
 
         has_alpha = (image->numcomps == 2);
 
-        width   = image->comps[0].w;
-        height  = image->comps[0].h;
+        width   = (int)image->comps[0].w;
+        height  = (int)image->comps[0].h;
         imgsize = width * height;
 
         /* Set tags */
@@ -2595,8 +2605,8 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
     TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
     TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
     TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
-    w= tiWidth;
-    h= tiHeight;
+    w= (int)tiWidth;
+    h= (int)tiHeight;
 
     if(tiBps != 8 && tiBps != 16 && tiBps != 12) tiBps = 0;
     if(tiPhoto != 1 && tiPhoto != 2) tiPhoto = 0;
@@ -2675,10 +2685,10 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
                 cmptparm[j].prec = tiBps;
                 cmptparm[j].bpp = tiBps;
             }
-            cmptparm[j].dx = subsampling_dx;
-            cmptparm[j].dy = subsampling_dy;
-            cmptparm[j].w = w;
-            cmptparm[j].h = h;
+            cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
+            cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
+            cmptparm[j].w = (OPJ_UINT32)w;
+            cmptparm[j].h = (OPJ_UINT32)h;
 #ifdef USETILEMODE
             cmptparm[j].x0 = 0;
             cmptparm[j].y0 = 0;
@@ -2688,7 +2698,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
 #ifdef USETILEMODE
         image = opj_image_tile_create(numcomps,&cmptparm[0],color_space);
 #else
-        image = opj_image_create(numcomps, &cmptparm[0], color_space);
+        image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
 #endif
 
         if(!image)
@@ -2698,18 +2708,18 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
         }
         /* 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;
+        image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
+        image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
+        image->x1 =    !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
+                                 image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
+        image->y1 =    !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
+                                 image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
 
         buf = _TIFFmalloc(TIFFStripSize(tif));
 
         strip_size=TIFFStripSize(tif);
         index = 0;
-        imgsize = image->comps[0].w * image->comps[0].h ;
+        imgsize = (int)(image->comps[0].w * image->comps[0].h);
         /* Read the Image components
 */
         for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
@@ -2830,15 +2840,15 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
         {
             cmptparm[j].prec = tiBps;
             cmptparm[j].bpp = tiBps;
-            cmptparm[j].dx = subsampling_dx;
-            cmptparm[j].dy = subsampling_dy;
-            cmptparm[j].w = w;
-            cmptparm[j].h = h;
+            cmptparm[j].dx = (OPJ_UINT32)subsampling_dx;
+            cmptparm[j].dy = (OPJ_UINT32)subsampling_dy;
+            cmptparm[j].w = (OPJ_UINT32)w;
+            cmptparm[j].h = (OPJ_UINT32)h;
         }
 #ifdef USETILEMODE
         image = opj_image_tile_create(numcomps,&cmptparm[0],color_space);
 #else
-        image = opj_image_create(numcomps, &cmptparm[0], color_space);
+        image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
 #endif
 
         if(!image)
@@ -2848,18 +2858,18 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
         }
         /* 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;
+        image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
+        image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
+        image->x1 =    !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
+                                 image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
+        image->y1 =    !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
+                                 image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
 
         buf = _TIFFmalloc(TIFFStripSize(tif));
 
         strip_size = TIFFStripSize(tif);
         index = 0;
-        imgsize = image->comps[0].w * image->comps[0].h ;
+        imgsize = (int)(image->comps[0].w * image->comps[0].h);
         /* Read the Image components
 */
         for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
@@ -2955,31 +2965,31 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
     color_space = OPJ_CLRSPC_SRGB;
     w = raw_cp->rawWidth;
     h = raw_cp->rawHeight;
-    cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
+    cmptparm = (opj_image_cmptparm_t*) malloc((size_t)numcomps * sizeof(opj_image_cmptparm_t));
 
     /* initialize image components */
-    memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
+    memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t));
     for(i = 0; i < numcomps; i++) {
-        cmptparm[i].prec = raw_cp->rawBitDepth;
-        cmptparm[i].bpp = raw_cp->rawBitDepth;
-        cmptparm[i].sgnd = raw_cp->rawSigned;
-        cmptparm[i].dx = subsampling_dx;
-        cmptparm[i].dy = subsampling_dy;
-        cmptparm[i].w = w;
-        cmptparm[i].h = h;
+        cmptparm[i].prec = (OPJ_UINT32)raw_cp->rawBitDepth;
+        cmptparm[i].bpp = (OPJ_UINT32)raw_cp->rawBitDepth;
+        cmptparm[i].sgnd = (OPJ_UINT32)raw_cp->rawSigned;
+        cmptparm[i].dx = (OPJ_UINT32)subsampling_dx;
+        cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
+        cmptparm[i].w = (OPJ_UINT32)w;
+        cmptparm[i].h = (OPJ_UINT32)h;
     }
     /* create the image */
-    image = opj_image_create(numcomps, &cmptparm[0], color_space);
+    image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
     free(cmptparm);
     if(!image) {
         fclose(f);
         return NULL;
     }
     /* set image offset and reference grid */
-    image->x0 = parameters->image_offset_x0;
-    image->y0 = parameters->image_offset_y0;
-    image->x1 = parameters->image_offset_x0 + (w - 1) *        subsampling_dx + 1;
-    image->y1 = parameters->image_offset_y0 + (h - 1) *        subsampling_dy + 1;
+    image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
+    image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
+    image->x1 = (OPJ_UINT32)parameters->image_offset_x0 + (OPJ_UINT32)(w - 1) *        (OPJ_UINT32)subsampling_dx + 1;
+    image->y1 = (OPJ_UINT32)parameters->image_offset_y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
 
     if(raw_cp->rawBitDepth <= 8)
     {
@@ -3011,11 +3021,11 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
                 }
                 if( big_endian )
                 {
-                    value = (unsigned short)(temp1 << 8 + temp2);
+                    value = (unsigned short)((temp1 << 8) + temp2);
                 }
                 else
                 {
-                    value = (unsigned short)(temp2 << 8 + temp1);
+                    value = (unsigned short)((temp2 << 8) + temp1);
                 }
                 image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
             }
@@ -3046,7 +3056,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL
 {
     FILE *rawFile = NULL;
     size_t res;
-    int compno;
+    unsigned int compno;
     int w, h, fails;
     int line, row, curr, mask;
     int *ptr;
@@ -3073,8 +3083,8 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL
         fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
                 image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
 
-        w = image->comps[compno].w;
-        h = image->comps[compno].h;
+        w = (int)image->comps[compno].w;
+        h = (int)image->comps[compno].h;
 
         if(image->comps[compno].prec <= 8)
         {
@@ -3119,15 +3129,15 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL
         {
             if(image->comps[compno].sgnd == 1)
             {
-                union { signed short val; signed char vals[2]; } uc;
+                union { signed short val; signed char vals[2]; } uc16;
                 mask = (1 << image->comps[compno].prec) - 1;
                 ptr = image->comps[compno].data;
                 for (line = 0; line < h; line++) {
                     for(row = 0; row < w; row++)       {
                         curr = *ptr;
                         if(curr > 32767 ) curr = 32767; else if( curr < -32768) curr = -32768;
-                        uc.val = (signed short)(curr & mask);
-                        res = fwrite(uc.vals, 1, 2, rawFile);
+                        uc16.val = (signed short)(curr & mask);
+                        res = fwrite(uc16.vals, 1, 2, rawFile);
                         if( res < 2 ) {
                             fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
                             goto fin;
@@ -3138,15 +3148,15 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL
             }
             else if(image->comps[compno].sgnd == 0)
             {
-                union { unsigned short val; unsigned char vals[2]; } uc;
+                union { unsigned short val; unsigned char vals[2]; } uc16;
                 mask = (1 << image->comps[compno].prec) - 1;
                 ptr = image->comps[compno].data;
                 for (line = 0; line < h; line++) {
                     for(row = 0; row < w; row++)       {
                         curr = *ptr;
                         if(curr > 65536 ) curr = 65536; else if( curr < 0) curr = 0;
-                        uc.val = (unsigned short)(curr & mask);
-                        res = fwrite(uc.vals, 1, 2, rawFile);
+                        uc16.val = (unsigned short)(curr & mask);
+                        res = fwrite(uc16.vals, 1, 2, rawFile);
                         if( res < 2 ) {
                             fprintf(stderr, "failed to write 2 byte for %s\n", outfile);
                             goto fin;
@@ -3288,7 +3298,7 @@ opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
 
     has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
 
-    nr_comp = 3 + has_alpha;
+    nr_comp = 3 + (unsigned int)has_alpha;
 
     bit_depth = png_get_bit_depth(png, info);
 
@@ -3304,12 +3314,12 @@ opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
 
     for(i = 0; i < nr_comp; ++i)
     {
-        cmptparm[i].prec = bit_depth;
+        cmptparm[i].prec = (OPJ_UINT32)bit_depth;
         /* bits_per_pixel: 8 or 16 */
-        cmptparm[i].bpp = bit_depth;
+        cmptparm[i].bpp = (OPJ_UINT32)bit_depth;
         cmptparm[i].sgnd = 0;
-        cmptparm[i].dx = sub_dx;
-        cmptparm[i].dy = sub_dy;
+        cmptparm[i].dx = (OPJ_UINT32)sub_dx;
+        cmptparm[i].dy = (OPJ_UINT32)sub_dy;
         cmptparm[i].w = (OPJ_UINT32)width;
         cmptparm[i].h = (OPJ_UINT32)height;
     }
@@ -3318,10 +3328,10 @@ opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
 
     if(image == NULL) goto fin;
 
-    image->x0 = params->image_offset_x0;
-    image->y0 = params->image_offset_y0;
-    image->x1 = (OPJ_UINT32)(image->x0 + (width  - 1) * sub_dx + 1 + image->x0);
-    image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * sub_dy + 1 + image->y0);
+    image->x0 = (OPJ_UINT32)params->image_offset_x0;
+    image->y0 = (OPJ_UINT32)params->image_offset_y0;
+    image->x1 = (OPJ_UINT32)(image->x0 + (width  - 1) * (OPJ_UINT32)sub_dx + 1 + image->x0);
+    image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32)sub_dy + 1 + image->y0);
 
     r = image->comps[0].data;
     g = image->comps[1].data;
@@ -3381,8 +3391,8 @@ int imagetopng(opj_image_t * image, const char *write_idf)
     png_color_8 sig_bit;
 
     is16 = force16 = force8 = ushift = dshift = 0; fails = 1;
-    prec = image->comps[0].prec;
-    nr_comp = image->numcomps;
+    prec = (int)image->comps[0].prec;
+    nr_comp = (int)image->numcomps;
 
     if(prec > 8 && prec < 16)
     {
@@ -3478,8 +3488,8 @@ int imagetopng(opj_image_t * image, const char *write_idf)
 
         is16 = (prec == 16);
 
-        width = image->comps[0].w;
-        height = image->comps[0].h;
+        width = (int)image->comps[0].w;
+        height = (int)image->comps[0].h;
 
         red = image->comps[0].data;
         green = image->comps[1].data;
@@ -3502,7 +3512,7 @@ int imagetopng(opj_image_t * image, const char *write_idf)
         }
         png_set_sBIT(png, info, &sig_bit);
 
-        png_set_IHDR(png, info, width, height, prec,
+        png_set_IHDR(png, info, (png_uint_32)width, (png_uint_32)height, prec,
                      color_type,
                      PNG_INTERLACE_NONE,
                      PNG_COMPRESSION_TYPE_BASE,  PNG_FILTER_TYPE_BASE);
@@ -3524,7 +3534,7 @@ image->comps[1].sgnd,image->comps[2].sgnd,width,height,has_alpha);
         adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
         adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
 
-        row_buf = (unsigned char*)malloc(width * nr_comp * 2);
+        row_buf = (unsigned char*)malloc((size_t)width * (size_t)nr_comp * 2);
 
         for(y = 0; y < height; ++y)
         {
@@ -3628,10 +3638,10 @@ image->comps[1].sgnd,image->comps[2].sgnd,width,height,has_alpha);
                 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
                 adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
             }
-            width = image->comps[0].w;
-            height = image->comps[0].h;
+            width = (int)image->comps[0].w;
+            height = (int)image->comps[0].h;
 
-            png_set_IHDR(png, info, width, height, sig_bit.gray,
+            png_set_IHDR(png, info, (png_uint_32)width, (png_uint_32)height, sig_bit.gray,
                          color_type,
                          PNG_INTERLACE_NONE,
                          PNG_COMPRESSION_TYPE_BASE,  PNG_FILTER_TYPE_BASE);
@@ -3653,7 +3663,7 @@ image->comps[1].sgnd,image->comps[2].sgnd,width,height,has_alpha);
             if(prec > 8)
             {
                 row_buf = (unsigned char*)
-                        malloc(width * nr_comp * sizeof(unsigned short));
+                        malloc((size_t)width * (size_t)nr_comp * sizeof(unsigned short));
 
                 for(y = 0; y < height; ++y)
                 {
@@ -3685,7 +3695,7 @@ image->comps[1].sgnd,image->comps[2].sgnd,width,height,has_alpha);
             }
             else /* prec <= 8 */
             {
-                row_buf = (unsigned char*)calloc(width, nr_comp * 2);
+                row_buf = (unsigned char*)calloc((size_t)width, (size_t)nr_comp * 2);
 
                 for(y = 0; y < height; ++y)
                 {