Imported Upstream version 1.6.40
[platform/upstream/libpng.git] / contrib / visupng / VisualPng.c
index 2018999..6f7b1df 100644 (file)
@@ -1,41 +1,43 @@
-//------------------------------------
-//  VisualPng.C -- Shows a PNG image
-//------------------------------------
+/*------------------------------------
+ *  VisualPng.C -- Shows a PNG image
+ *------------------------------------
+ *
+ * Copyright 2000,2017 Willem van Schaik.
+ *
+ * This code is released under the libpng license.
+ * For conditions of distribution and use, see the disclaimer
+ * and license in png.h
+ */
 
-// Copyright 2000, Willem van Schaik.
+/* switches */
 
-// This code is released under the libpng license.
-// For conditions of distribution and use, see the disclaimer
-// and license in png.h
-
-// switches
-
-// defines
+/* defines */
 
 #define PROGNAME  "VisualPng"
 #define LONGNAME  "Win32 Viewer for PNG-files"
 #define VERSION   "1.0 of 2000 June 07"
 
-// constants
+/* constants */
 
 #define MARGIN 8
 
-// standard includes
+/* standard includes */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <windows.h>
+#include <zlib.h>
 
-// application includes
+/* application includes */
 
 #include "png.h"
 #include "pngfile.h"
 #include "resource.h"
 
-// macros
+/* macros */
 
-// function prototypes
+/* function prototypes */
 
 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
 BOOL    CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ;
@@ -65,14 +67,14 @@ BOOL FillBitmap (
         BYTE *pbImage, int cxImgSize, int cyImgSize, int cImgChannels,
         BOOL bStretched);
 
-// a few global variables
+/* a few global variables */
 
 static char *szProgName = PROGNAME;
 static char *szAppName = LONGNAME;
 static char *szIconName = PROGNAME;
 static char szCmdFileName [MAX_PATH];
 
-// MAIN routine
+/* MAIN routine */
 
 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     PSTR szCmdLine, int iCmdShow)
@@ -90,7 +92,7 @@ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
     wndclass.hInstance     = hInstance;
     wndclass.hIcon         = LoadIcon (hInstance, szIconName) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
-    wndclass.hbrBackground = NULL; // (HBRUSH) GetStockObject (GRAY_BRUSH);
+    wndclass.hbrBackground = NULL; /* (HBRUSH) GetStockObject (GRAY_BRUSH); */
     wndclass.lpszMenuName  = szProgName;
     wndclass.lpszClassName = szProgName;
 
@@ -101,7 +103,7 @@ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
         return 0;
     }
 
-    // if filename given on commandline, store it
+    /* if filename given on command line, store it */
     if ((szCmdLine != NULL) && (*szCmdLine != '\0'))
         if (szCmdLine[0] == '"')
             strncpy (szCmdFileName, szCmdLine + 1, strlen(szCmdLine) - 2);
@@ -110,20 +112,20 @@ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
     else
         strcpy (szCmdFileName, "");
 
-    // calculate size of window-borders
+    /* calculate size of window-borders */
     ixBorders = 2 * (GetSystemMetrics (SM_CXBORDER) +
                      GetSystemMetrics (SM_CXDLGFRAME));
     iyBorders = 2 * (GetSystemMetrics (SM_CYBORDER) +
                      GetSystemMetrics (SM_CYDLGFRAME)) +
                      GetSystemMetrics (SM_CYCAPTION) +
                      GetSystemMetrics (SM_CYMENUSIZE) +
-                     1; /* WvS: don't ask me why? */
+                     1; /* WvS: don't ask me why?  */
 
     hwnd = CreateWindow (szProgName, szAppName,
         WS_OVERLAPPEDWINDOW,
         CW_USEDEFAULT, CW_USEDEFAULT,
         512 + 2 * MARGIN + ixBorders, 384 + 2 * MARGIN + iyBorders,
-//      CW_USEDEFAULT, CW_USEDEFAULT,
+/*      CW_USEDEFAULT, CW_USEDEFAULT, */
         NULL, NULL, hInstance, NULL);
 
     ShowWindow (hwnd, iCmdShow);
@@ -180,29 +182,29 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
 
         strcpy (szImgPathName, "");
 
-        // in case we process file given on command-line
+        /* in case we process file given on command-line */
 
         if (szCmdFileName[0] != '\0')
         {
             strcpy (szImgPathName, szCmdFileName);
 
-            // read the other png-files in the directory for later
-            // next/previous commands
+            /* read the other png-files in the directory for later */
+            /* next/previous commands */
 
             BuildPngList (szImgPathName, &pPngFileList, &iPngFileCount,
                           &iPngFileIndex);
 
-            // load the image from file
+            /* load the image from file */
 
             if (!LoadImageFile (hwnd, szImgPathName,
                 &pbImage, &cxImgSize, &cyImgSize, &cImgChannels, &bkgColor))
                 return 0;
 
-            // invalidate the client area for later update
+            /* invalidate the client area for later update */
 
             InvalidateRect (hwnd, NULL, TRUE);
 
-            // display the PNG into the DIBitmap
+            /* display the PNG into the DIBitmap */
 
             DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
                 pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
@@ -214,11 +216,11 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
         cxWinSize = LOWORD (lParam);
         cyWinSize = HIWORD (lParam);
 
-        // invalidate the client area for later update
+        /* invalidate the client area for later update */
 
         InvalidateRect (hwnd, NULL, TRUE);
 
-        // display the PNG into the DIBitmap
+        /* display the PNG into the DIBitmap */
 
         DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
             pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
@@ -242,28 +244,28 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
         {
         case IDM_FILE_OPEN:
 
-            // show the File Open dialog box
+            /* show the File Open dialog box */
 
             if (!PngFileOpenDlg (hwnd, szImgPathName, szTitleName))
                 return 0;
 
-            // read the other png-files in the directory for later
-            // next/previous commands
+            /* read the other png-files in the directory for later */
+            /* next/previous commands */
 
             BuildPngList (szImgPathName, &pPngFileList, &iPngFileCount,
                           &iPngFileIndex);
 
-            // load the image from file
+            /* load the image from file */
 
             if (!LoadImageFile (hwnd, szImgPathName,
                 &pbImage, &cxImgSize, &cyImgSize, &cImgChannels, &bkgColor))
                 return 0;
 
-            // invalidate the client area for later update
+            /* invalidate the client area for later update */
 
             InvalidateRect (hwnd, NULL, TRUE);
 
-            // display the PNG into the DIBitmap
+            /* display the PNG into the DIBitmap */
 
             DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
                 pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
@@ -272,12 +274,12 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
 
         case IDM_FILE_SAVE:
 
-            // show the File Save dialog box
+            /* show the File Save dialog box */
 
             if (!PngFileSaveDlg (hwnd, szImgPathName, szTitleName))
                 return 0;
 
-            // save the PNG to a disk file
+            /* save the PNG to a disk file */
 
             SetCursor (LoadCursor (NULL, IDC_WAIT));
             ShowCursor (TRUE);
@@ -295,55 +297,55 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
 
         case IDM_FILE_NEXT:
 
-            // read next entry in the directory
+            /* read next entry in the directory */
 
             if (SearchPngList (pPngFileList, iPngFileCount, &iPngFileIndex,
                 NULL, szImgPathName))
             {
                 if (strcmp (szImgPathName, "") == 0)
                     return 0;
-                
-                // load the image from file
-                
+
+                /* load the image from file */
+
                 if (!LoadImageFile (hwnd, szImgPathName, &pbImage,
                         &cxImgSize, &cyImgSize, &cImgChannels, &bkgColor))
                     return 0;
-                
-                // invalidate the client area for later update
-                
+
+                /* invalidate the client area for later update */
+
                 InvalidateRect (hwnd, NULL, TRUE);
-                
-                // display the PNG into the DIBitmap
-                
+
+                /* display the PNG into the DIBitmap */
+
                 DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
                     pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
             }
-            
+
             return 0;
 
         case IDM_FILE_PREVIOUS:
 
-            // read previous entry in the directory
+            /* read previous entry in the directory */
 
             if (SearchPngList (pPngFileList, iPngFileCount, &iPngFileIndex,
                 szImgPathName, NULL))
             {
-                
+
                 if (strcmp (szImgPathName, "") == 0)
                     return 0;
-                
-                // load the image from file
-                
+
+                /* load the image from file */
+
                 if (!LoadImageFile (hwnd, szImgPathName, &pbImage, &cxImgSize,
                     &cyImgSize, &cImgChannels, &bkgColor))
                     return 0;
-                
-                // invalidate the client area for later update
-                
+
+                /* invalidate the client area for later update */
+
                 InvalidateRect (hwnd, NULL, TRUE);
-                
-                // display the PNG into the DIBitmap
-                
+
+                /* display the PNG into the DIBitmap */
+
                 DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
                     pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
             }
@@ -352,9 +354,9 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
 
         case IDM_FILE_EXIT:
 
-            // more cleanup needed...
+            /* more cleanup needed... */
 
-            // free image buffer
+            /* free image buffer */
 
             if (pDib != NULL)
             {
@@ -362,7 +364,7 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
                 pDib = NULL;
             }
 
-            // free file-list
+            /* free file-list */
 
             if (pPngFileList != NULL)
             {
@@ -370,7 +372,7 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
                 pPngFileList = NULL;
             }
 
-            // let's go ...
+            /* let's go ... */
 
             exit (0);
 
@@ -383,11 +385,11 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
             else
                 CheckMenuItem (hMenu, IDM_OPTIONS_STRETCH, MF_UNCHECKED);
 
-            // invalidate the client area for later update
+            /* invalidate the client area for later update */
 
             InvalidateRect (hwnd, NULL, TRUE);
 
-            // display the PNG into the DIBitmap
+            /* display the PNG into the DIBitmap */
 
             DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
                 pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
@@ -398,7 +400,7 @@ LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
             DialogBox (hInstance, TEXT ("AboutBox"), hwnd, AboutDlgProc) ;
             return 0;
 
-        } // end switch
+        } /* end switch */
 
         break;
 
@@ -450,10 +452,10 @@ BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message,
      return FALSE ;
 }
 
-//---------------
-//  CenterAbout
-//---------------
-
+/*---------------
+ *  CenterAbout
+ *---------------
+ */
 BOOL CenterAbout (HWND hwndChild, HWND hwndParent)
 {
    RECT    rChild, rParent, rWorkArea;
@@ -461,19 +463,19 @@ BOOL CenterAbout (HWND hwndChild, HWND hwndParent)
    int     xNew, yNew;
    BOOL  bResult;
 
-   // Get the Height and Width of the child window
+   /* Get the Height and Width of the child window */
    GetWindowRect (hwndChild, &rChild);
    wChild = rChild.right - rChild.left;
    hChild = rChild.bottom - rChild.top;
 
-   // Get the Height and Width of the parent window
+   /* Get the Height and Width of the parent window */
    GetWindowRect (hwndParent, &rParent);
    wParent = rParent.right - rParent.left;
    hParent = rParent.bottom - rParent.top;
 
-   // Get the limits of the 'workarea'
+   /* Get the limits of the 'workarea' */
    bResult = SystemParametersInfo(
-      SPI_GETWORKAREA,  // system parameter to query or set
+      SPI_GETWORKAREA,  /* system parameter to query or set */
       sizeof(RECT),
       &rWorkArea,
       0);
@@ -483,7 +485,7 @@ BOOL CenterAbout (HWND hwndChild, HWND hwndParent)
       rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
    }
 
-   // Calculate new X position, then adjust for workarea
+   /* Calculate new X position, then adjust for workarea */
    xNew = rParent.left + ((wParent - wChild) /2);
    if (xNew < rWorkArea.left) {
       xNew = rWorkArea.left;
@@ -491,7 +493,7 @@ BOOL CenterAbout (HWND hwndChild, HWND hwndParent)
       xNew = rWorkArea.right - wChild;
    }
 
-   // Calculate new Y position, then adjust for workarea
+   /* Calculate new Y position, then adjust for workarea */
    yNew = rParent.top  + ((hParent - hChild) /2);
    if (yNew < rWorkArea.top) {
       yNew = rWorkArea.top;
@@ -499,15 +501,15 @@ BOOL CenterAbout (HWND hwndChild, HWND hwndParent)
       yNew = rWorkArea.bottom - hChild;
    }
 
-   // Set it, and return
+   /* Set it, and return */
    return SetWindowPos (hwndChild, NULL, xNew, yNew, 0, 0, SWP_NOSIZE |
           SWP_NOZORDER);
 }
 
-//----------------
-//  BuildPngList
-//----------------
-
+/*----------------
+ *  BuildPngList
+ *----------------
+ */
 BOOL BuildPngList (PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount,
      int *pFileIndex)
 {
@@ -523,7 +525,7 @@ BOOL BuildPngList (PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount,
     int                       i, ii;
     int                       j, jj;
 
-    // free previous file-list
+    /* free previous file-list */
 
     if (*ppFileList != NULL)
     {
@@ -531,7 +533,7 @@ BOOL BuildPngList (PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount,
         *ppFileList = NULL;
     }
 
-    // extract foldername, filename and search-name
+    /* extract foldername, filename and search-name */
 
     strcpy (szImgPathName, pstrPathName);
     strcpy (szImgFileName, strrchr (pstrPathName, '\\') + 1);
@@ -540,7 +542,7 @@ BOOL BuildPngList (PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount,
     *(strrchr (szImgFindName, '\\') + 1) = '\0';
     strcat (szImgFindName, "*.png");
 
-    // first cycle: count number of files in directory for memory allocation
+    /* first cycle: count number of files in directory for memory allocation */
 
     *pFileCount = 0;
 
@@ -554,11 +556,11 @@ BOOL BuildPngList (PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount,
     }
     FindClose(hFind);
 
-    // allocation memory for file-list
+    /* allocation memory for file-list */
 
     *ppFileList = (TCHAR *) malloc (*pFileCount * MAX_PATH);
 
-    // second cycle: read directory and store filenames in file-list
+    /* second cycle: read directory and store filenames in file-list */
 
     hFind = FindFirstFile(szImgFindName, &finddata);
     bOk = (hFind != (HANDLE) -1);
@@ -580,7 +582,7 @@ BOOL BuildPngList (PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount,
     }
     FindClose(hFind);
 
-    // finally we must sort the file-list
+    /* finally we must sort the file-list */
 
     for (i = 0; i < *pFileCount - 1; i++)
     {
@@ -594,7 +596,7 @@ BOOL BuildPngList (PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount,
                 strcpy (*ppFileList + jj, *ppFileList + ii);
                 strcpy (*ppFileList + ii, szTmp);
 
-                // check if this was the current image that we moved
+                /* check if this was the current image that we moved */
 
                 if (*pFileIndex == i)
                     *pFileIndex = j;
@@ -608,9 +610,10 @@ BOOL BuildPngList (PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount,
     return TRUE;
 }
 
-//----------------
-//  SearchPngList
-//----------------
+/*----------------
+ *  SearchPngList
+ *----------------
+ */
 
 BOOL SearchPngList (
         TCHAR *pFileList, int FileCount, int *pFileIndex,
@@ -618,30 +621,30 @@ BOOL SearchPngList (
 {
     if (FileCount > 0)
     {
-        // get previous entry
-        
+        /* get previous entry */
+
         if (pstrPrevName != NULL)
         {
             if (*pFileIndex > 0)
                 *pFileIndex -= 1;
             else
                 *pFileIndex = FileCount - 1;
-            
+
             strcpy (pstrPrevName, pFileList + (*pFileIndex * MAX_PATH));
         }
-        
-        // get next entry
-        
+
+        /* get next entry */
+
         if (pstrNextName != NULL)
         {
             if (*pFileIndex < FileCount - 1)
                 *pFileIndex += 1;
             else
                 *pFileIndex = 0;
-            
+
             strcpy (pstrNextName, pFileList + (*pFileIndex * MAX_PATH));
         }
-        
+
         return TRUE;
     }
     else
@@ -650,9 +653,10 @@ BOOL SearchPngList (
     }
 }
 
-//-----------------
-//  LoadImageFile
-//-----------------
+/*-----------------
+ *  LoadImageFile
+ *-----------------
+ */
 
 BOOL LoadImageFile (HWND hwnd, PTSTR pstrPathName,
                 png_byte **ppbImage, int *pxImgSize, int *pyImgSize,
@@ -660,7 +664,7 @@ BOOL LoadImageFile (HWND hwnd, PTSTR pstrPathName,
 {
     static TCHAR szTmp [MAX_PATH];
 
-    // if there's an existing PNG, free the memory
+    /* if there's an existing PNG, free the memory */
 
     if (*ppbImage)
     {
@@ -668,7 +672,7 @@ BOOL LoadImageFile (HWND hwnd, PTSTR pstrPathName,
         *ppbImage = NULL;
     }
 
-    // Load the entire PNG into memory
+    /* Load the entire PNG into memory */
 
     SetCursor (LoadCursor (NULL, IDC_WAIT));
     ShowCursor (TRUE);
@@ -694,10 +698,10 @@ BOOL LoadImageFile (HWND hwnd, PTSTR pstrPathName,
     return TRUE;
 }
 
-//----------------
-//  DisplayImage
-//----------------
-
+/*----------------
+ *  DisplayImage
+ *----------------
+ */
 BOOL DisplayImage (HWND hwnd, BYTE **ppDib,
         BYTE **ppDiData, int cxWinSize, int cyWinSize,
         BYTE *pbImage, int cxImgSize, int cyImgSize, int cImgChannels,
@@ -705,14 +709,14 @@ BOOL DisplayImage (HWND hwnd, BYTE **ppDib,
 {
     BYTE                       *pDib = *ppDib;
     BYTE                       *pDiData = *ppDiData;
-    // BITMAPFILEHEADER        *pbmfh;
+    /* BITMAPFILEHEADER        *pbmfh; */
     BITMAPINFOHEADER           *pbmih;
     WORD                        wDIRowBytes;
     png_color                   bkgBlack = {0, 0, 0};
     png_color                   bkgGray  = {127, 127, 127};
     png_color                   bkgWhite = {255, 255, 255};
 
-    // allocate memory for the Device Independant bitmap
+    /* allocate memory for the Device Independent bitmap */
 
     wDIRowBytes = (WORD) ((3 * cxWinSize + 3L) >> 2) << 2;
 
@@ -722,6 +726,10 @@ BOOL DisplayImage (HWND hwnd, BYTE **ppDib,
         pDib = NULL;
     }
 
+    if (cyWinSize > ((size_t)(-1))/wDIRowBytes) {
+    {
+        MessageBox (hwnd, TEXT ("Visual PNG: image is too big");
+    }
     if (!(pDib = (BYTE *) malloc (sizeof(BITMAPINFOHEADER) +
         wDIRowBytes * cyWinSize)))
     {
@@ -733,7 +741,7 @@ BOOL DisplayImage (HWND hwnd, BYTE **ppDib,
     *ppDib = pDib;
     memset (pDib, 0, sizeof(BITMAPINFOHEADER));
 
-    // initialize the dib-structure
+    /* initialize the dib-structure */
 
     pbmih = (BITMAPINFOHEADER *) pDib;
     pbmih->biSize = sizeof(BITMAPINFOHEADER);
@@ -745,11 +753,11 @@ BOOL DisplayImage (HWND hwnd, BYTE **ppDib,
     pDiData = pDib + sizeof(BITMAPINFOHEADER);
     *ppDiData = pDiData;
 
-    // first fill bitmap with gray and image border
+    /* first fill bitmap with gray and image border */
 
     InitBitmap (pDiData, cxWinSize, cyWinSize);
 
-    // then fill bitmap with image
+    /* then fill bitmap with image */
 
     if (pbImage)
     {
@@ -762,16 +770,16 @@ BOOL DisplayImage (HWND hwnd, BYTE **ppDib,
     return TRUE;
 }
 
-//--------------
-//  InitBitmap
-//--------------
-
+/*--------------
+ *  InitBitmap
+ *--------------
+ */
 BOOL InitBitmap (BYTE *pDiData, int cxWinSize, int cyWinSize)
 {
     BYTE *dst;
     int x, y, col;
 
-    // initialize the background with gray
+    /* initialize the background with gray */
 
     dst = pDiData;
     for (y = 0; y < cyWinSize; y++)
@@ -779,13 +787,13 @@ BOOL InitBitmap (BYTE *pDiData, int cxWinSize, int cyWinSize)
         col = 0;
         for (x = 0; x < cxWinSize; x++)
         {
-            // fill with GRAY
+            /* fill with GRAY */
             *dst++ = 127;
             *dst++ = 127;
             *dst++ = 127;
             col += 3;
         }
-        // rows start on 4 byte boundaries
+        /* rows start on 4 byte boundaries */
         while ((col % 4) != 0)
         {
             dst++;
@@ -796,10 +804,10 @@ BOOL InitBitmap (BYTE *pDiData, int cxWinSize, int cyWinSize)
     return TRUE;
 }
 
-//--------------
-//  FillBitmap
-//--------------
-
+/*--------------
+ *  FillBitmap
+ *--------------
+ */
 BOOL FillBitmap (
         BYTE *pDiData, int cxWinSize, int cyWinSize,
         BYTE *pbImage, int cxImgSize, int cyImgSize, int cImgChannels,
@@ -824,11 +832,12 @@ BOOL FillBitmap (
         cxNewSize = cxWinSize - 2 * MARGIN;
         cyNewSize = cyWinSize - 2 * MARGIN;
 
-        // stretch the image to it's window determined size
+        /* stretch the image to it's window determined size */
 
-        // the following two are the same, but the first has side-effects
-        // because of rounding
-//      if ((cyNewSize / cxNewSize) > (cyImgSize / cxImgSize))
+        /* the following two are mathematically the same, but the first
+         * has side-effects because of rounding
+         */
+/*      if ((cyNewSize / cxNewSize) > (cyImgSize / cxImgSize)) */
         if ((cyNewSize * cxImgSize) > (cyImgSize * cxNewSize))
         {
             cyNewSize = cxNewSize * cyImgSize / cxImgSize;
@@ -842,6 +851,10 @@ BOOL FillBitmap (
             cxImgPos = (cxWinSize - cxNewSize) / 2;
         }
 
+        if (cyNewSize > ((size_t)(-1))/(cImgChannels * cxNewSize)) {
+        {
+            MessageBox (hwnd, TEXT ("Visual PNG: stretched image is too big");
+        }
         pStretchedImage = malloc (cImgChannels * cxNewSize * cyNewSize);
         pImg = pStretchedImage;
 
@@ -867,12 +880,12 @@ BOOL FillBitmap (
             }
         }
 
-        // calculate row-bytes
+        /* calculate row-bytes */
 
         wImgRowBytes = cImgChannels * cxNewSize;
         wDIRowBytes = (WORD) ((cDIChannels * cxWinSize + 3L) >> 2) << 2;
 
-        // copy image to screen
+        /* copy image to screen */
 
         for (yImg = 0, yWin = cyImgPos; yImg < cyNewSize; yImg++, yWin++)
         {
@@ -888,7 +901,7 @@ BOOL FillBitmap (
                 r = *src++;
                 g = *src++;
                 b = *src++;
-                *dst++ = b; /* note the reverse order */
+                *dst++ = b; /* note the reverse order  */
                 *dst++ = g;
                 *dst++ = r;
                 if (cImgChannels == 4)
@@ -898,7 +911,7 @@ BOOL FillBitmap (
             }
         }
 
-        // free memory
+        /* free memory */
 
         if (pStretchedImage != NULL)
         {
@@ -908,28 +921,28 @@ BOOL FillBitmap (
 
     }
 
-    // process the image not-stretched
+    /* process the image not-stretched */
 
     else
     {
-        // calculate the central position
+        /* calculate the central position */
 
         cxImgPos = (cxWinSize - cxImgSize) / 2;
         cyImgPos = (cyWinSize - cyImgSize) / 2;
 
-        // check for image larger than window
+        /* check for image larger than window */
 
         if (cxImgPos < MARGIN)
             cxImgPos = MARGIN;
         if (cyImgPos < MARGIN)
             cyImgPos = MARGIN;
 
-        // calculate both row-bytes
+        /* calculate both row-bytes */
 
         wImgRowBytes = cImgChannels * cxImgSize;
         wDIRowBytes = (WORD) ((cDIChannels * cxWinSize + 3L) >> 2) << 2;
 
-        // copy image to screen
+        /* copy image to screen */
 
         for (yImg = 0, yWin = cyImgPos; yImg < cyImgSize; yImg++, yWin++)
         {
@@ -945,7 +958,7 @@ BOOL FillBitmap (
                 r = *src++;
                 g = *src++;
                 b = *src++;
-                *dst++ = b; /* note the reverse order */
+                *dst++ = b; /* note the reverse order  */
                 *dst++ = g;
                 *dst++ = r;
                 if (cImgChannels == 4)
@@ -959,6 +972,7 @@ BOOL FillBitmap (
     return TRUE;
 }
 
-//-----------------
-//  end of source
-//-----------------
+/*-----------------
+ *  end of source
+ *-----------------
+ */