Fix some typos (found by `codespell` and `typos`)
[openjpeg.git] / src / bin / jp2 / convertbmp.c
index 7fde99ab3ef21f6a043ba1d8ada85d5030a943fe..3ef9915fe1a33b0c7a5962c4e31655be8d68d24f 100644 (file)
@@ -198,14 +198,10 @@ static void bmpmask32toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
     bmp_mask_get_shift_and_prec(blueMask,  &blueShift,  &bluePrec);
     bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec);
 
-    image->comps[0].bpp = redPrec;
     image->comps[0].prec = redPrec;
-    image->comps[1].bpp = greenPrec;
     image->comps[1].prec = greenPrec;
-    image->comps[2].bpp = bluePrec;
     image->comps[2].prec = bluePrec;
     if (hasAlpha) {
-        image->comps[3].bpp = alphaPrec;
         image->comps[3].prec = alphaPrec;
     }
 
@@ -260,14 +256,10 @@ static void bmpmask16toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
     bmp_mask_get_shift_and_prec(blueMask,  &blueShift,  &bluePrec);
     bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec);
 
-    image->comps[0].bpp = redPrec;
     image->comps[0].prec = redPrec;
-    image->comps[1].bpp = greenPrec;
     image->comps[1].prec = greenPrec;
-    image->comps[2].bpp = bluePrec;
     image->comps[2].prec = bluePrec;
     if (hasAlpha) {
-        image->comps[3].bpp = alphaPrec;
         image->comps[3].prec = alphaPrec;
     }
 
@@ -534,14 +526,14 @@ static OPJ_BOOL bmp_read_raw_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride,
 static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData,
                                    OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height)
 {
-    OPJ_UINT32 x, y;
+    OPJ_UINT32 x, y, written;
     OPJ_UINT8 *pix;
     const OPJ_UINT8 *beyond;
 
     beyond = pData + stride * height;
     pix = pData;
 
-    x = y = 0U;
+    x = y = written = 0U;
     while (y < height) {
         int c = getc(IN);
         if (c == EOF) {
@@ -561,6 +553,7 @@ static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData,
             for (j = 0; (j < c) && (x < width) &&
                     ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
                 *pix = c1;
+                written++;
             }
         } else {
             c = getc(IN);
@@ -598,6 +591,7 @@ static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData,
                     }
                     c1 = (OPJ_UINT8)c1_int;
                     *pix = c1;
+                    written++;
                 }
                 if ((OPJ_UINT32)c & 1U) { /* skip padding byte */
                     c = getc(IN);
@@ -608,37 +602,50 @@ static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData,
             }
         }
     }/* while() */
+
+    if (written != width * height) {
+        fprintf(stderr, "warning, image's actual size does not match advertised one\n");
+        return OPJ_FALSE;
+    }
+
     return OPJ_TRUE;
 }
 
 static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData,
                                    OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height)
 {
-    OPJ_UINT32 x, y;
+    OPJ_UINT32 x, y, written;
     OPJ_UINT8 *pix;
     const OPJ_UINT8 *beyond;
 
     beyond = pData + stride * height;
     pix = pData;
-    x = y = 0U;
+    x = y = written = 0U;
     while (y < height) {
         int c = getc(IN);
         if (c == EOF) {
-            break;
+            return OPJ_FALSE;
         }
 
         if (c) { /* encoded mode */
-            int j;
-            OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
+            int j, c1_int;
+            OPJ_UINT8 c1;
+
+            c1_int = getc(IN);
+            if (c1_int == EOF) {
+                return OPJ_FALSE;
+            }
+            c1 = (OPJ_UINT8)c1_int;
 
             for (j = 0; (j < c) && (x < width) &&
                     ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
                 *pix = (OPJ_UINT8)((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU));
+                written++;
             }
         } else { /* absolute mode */
             c = getc(IN);
             if (c == EOF) {
-                break;
+                return OPJ_FALSE;
             }
 
             if (c == 0x00) { /* EOL */
@@ -649,8 +656,14 @@ static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData,
                 break;
             } else if (c == 0x02) { /* MOVE by dxdy */
                 c = getc(IN);
+                if (c == EOF) {
+                    return OPJ_FALSE;
+                }
                 x += (OPJ_UINT32)c;
                 c = getc(IN);
+                if (c == EOF) {
+                    return OPJ_FALSE;
+                }
                 y += (OPJ_UINT32)c;
                 pix = pData + y * stride + x;
             } else { /* 03 .. 255 : absolute mode */
@@ -660,16 +673,29 @@ static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData,
                 for (j = 0; (j < c) && (x < width) &&
                         ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
                     if ((j & 1) == 0) {
-                        c1 = (OPJ_UINT8)getc(IN);
+                        int c1_int;
+                        c1_int = getc(IN);
+                        if (c1_int == EOF) {
+                            return OPJ_FALSE;
+                        }
+                        c1 = (OPJ_UINT8)c1_int;
                     }
                     *pix = (OPJ_UINT8)((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU));
+                    written++;
                 }
                 if (((c & 3) == 1) || ((c & 3) == 2)) { /* skip padding byte */
-                    getc(IN);
+                    c = getc(IN);
+                    if (c == EOF) {
+                        return OPJ_FALSE;
+                    }
                 }
             }
         }
     }  /* while(y < height) */
+    if (written != width * height) {
+        fprintf(stderr, "warning, image's actual size does not match advertised one\n");
+        return OPJ_FALSE;
+    }
     return OPJ_TRUE;
 }
 
@@ -763,7 +789,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
         fclose(IN);
         return NULL;
     }
-    pData = (OPJ_UINT8 *) calloc(1, stride * Info_h.biHeight * sizeof(OPJ_UINT8));
+    pData = (OPJ_UINT8 *) calloc(1, sizeof(OPJ_UINT8) * stride * Info_h.biHeight);
     if (pData == NULL) {
         fclose(IN);
         return NULL;
@@ -804,7 +830,6 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
     memset(&cmptparm[0], 0, sizeof(cmptparm));
     for (i = 0; i < 4U; i++) {
         cmptparm[i].prec = 8;
-        cmptparm[i].bpp  = 8;
         cmptparm[i].sgnd = 0;
         cmptparm[i].dx   = (OPJ_UINT32)parameters->subsampling_dx;
         cmptparm[i].dy   = (OPJ_UINT32)parameters->subsampling_dy;