//------------------------------------
// VisualPng.C -- Shows a PNG image
//------------------------------------
// Copyright 2000, Willem van Schaik. For conditions of distribution and
// use, see the copyright/license/disclaimer notice in png.h
// switches
// defines
// constants
// standard includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
// application includes
#include "png.h"
#include "pngfile.h"
#include "resource.h"
// macros
// function prototypes
int *pFileIndex);
// a few global variables
// MAIN routine
{
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
if (!RegisterClass (&wndclass))
{
return 0;
}
// if filename given on commandline, store it
if (szCmdLine[0] == '"')
else
else
// calculate size of window-borders
1; /* WvS: don't ask me why? */
// CW_USEDEFAULT, CW_USEDEFAULT,
UpdateWindow (hwnd);
{
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
}
{
static int cImgChannels;
static int iPngFileCount;
static int iPngFileIndex;
switch (message)
{
case WM_CREATE:
// in case we process file given on command-line
if (szCmdFileName[0] != '\0')
{
// read the other png-files in the directory for later
// load the image from file
return 0;
// invalidate the client area for later update
// display the PNG into the DIBitmap
}
return 0;
case WM_SIZE:
// invalidate the client area for later update
// display the PNG into the DIBitmap
return 0;
case WM_INITMENUPOPUP:
if (pbImage)
else
return 0;
case WM_COMMAND:
{
case IDM_FILE_OPEN:
// show the File Open dialog box
return 0;
// read the other png-files in the directory for later
// load the image from file
return 0;
// invalidate the client area for later update
// display the PNG into the DIBitmap
return 0;
case IDM_FILE_SAVE:
// show the File Save dialog box
return 0;
// save the PNG to a disk file
ShowCursor (TRUE);
bkgColor);
ShowCursor (FALSE);
if (!bOk)
return 0;
case IDM_FILE_NEXT:
// read next entry in the directory
{
return 0;
// load the image from file
return 0;
// invalidate the client area for later update
// display the PNG into the DIBitmap
}
return 0;
case IDM_FILE_PREVIOUS:
// read previous entry in the directory
{
return 0;
// load the image from file
return 0;
// invalidate the client area for later update
// display the PNG into the DIBitmap
}
return 0;
case IDM_FILE_EXIT:
// more cleanup needed...
// free image buffer
{
}
// free file-list
if (pPngFileList != NULL)
{
free (pPngFileList);
pPngFileList = NULL;
}
// let's go ...
exit (0);
return 0;
case IDM_OPTIONS_STRETCH:
bStretched = !bStretched;
if (bStretched)
else
// invalidate the client area for later update
// display the PNG into the DIBitmap
return 0;
case IDM_HELP_ABOUT:
return 0;
} // end switch
break;
case WM_PAINT:
if (pDib)
return 0;
case WM_DESTROY:
if (pbmfh)
{
}
PostQuitMessage (0);
return 0;
}
}
{
switch (message)
{
case WM_INITDIALOG :
return TRUE ;
case WM_COMMAND :
{
case IDOK :
case IDCANCEL :
return TRUE ;
}
break ;
}
return FALSE ;
}
//---------------
// CenterAbout
//---------------
{
// Get the Height and Width of the child window
// Get the Height and Width of the parent window
// Get the limits of the 'workarea'
SPI_GETWORKAREA, // system parameter to query or set
sizeof(RECT),
0);
if (!bResult) {
}
// Calculate new X position, then adjust for workarea
}
// Calculate new Y position, then adjust for workarea
}
// Set it, and return
}
//----------------
// BuildPngList
//----------------
int *pFileIndex)
{
int i, ii;
int j, jj;
// free previous file-list
if (*ppFileList != NULL)
{
free (*ppFileList);
*ppFileList = NULL;
}
// extract foldername, filename and search-name
// first cycle: count number of files in directory for memory allocation
*pFileCount = 0;
while (bOk)
{
*pFileCount += 1;
}
// allocation memory for file-list
// second cycle: read directory and store filenames in file-list
i = 0;
ii = 0;
while (bOk)
{
*pFileIndex = i;
i++;
}
// finally we must sort the file-list
for (i = 0; i < *pFileCount - 1; i++)
{
for (j = i+1; j < *pFileCount; j++)
{
{
// check if this was the current image that we moved
if (*pFileIndex == i)
*pFileIndex = j;
else
if (*pFileIndex == j)
*pFileIndex = i;
}
}
}
return TRUE;
}
//----------------
// SearchPngList
//----------------
{
if (FileCount > 0)
{
// get previous entry
if (pstrPrevName != NULL)
{
if (*pFileIndex > 0)
*pFileIndex -= 1;
else
}
// get next entry
if (pstrNextName != NULL)
{
*pFileIndex += 1;
else
*pFileIndex = 0;
}
return TRUE;
}
else
{
return FALSE;
}
}
//-----------------
// LoadImageFile
//-----------------
{
// if there's an existing PNG, free the memory
if (*ppbImage)
{
}
// Load the entire PNG into memory
ShowCursor (TRUE);
ShowCursor (FALSE);
{
}
else
{
return FALSE;
}
return TRUE;
}
//----------------
// DisplayImage
//----------------
{
// BITMAPFILEHEADER *pbmfh;
// allocate memory for the Device Independant bitmap
if (pDib)
{
}
wDIRowBytes * cyWinSize)))
{
return FALSE;
}
// initialize the dib-structure
pbmih->biCompression = 0;
// first fill bitmap with gray and image border
// then fill bitmap with image
if (pbImage)
{
}
return TRUE;
}
//--------------
// InitBitmap
//--------------
{
int x, y, col;
// initialize the background with gray
for (y = 0; y < cyWinSize; y++)
{
col = 0;
for (x = 0; x < cxWinSize; x++)
{
// fill with GRAY
*dst++ = 127;
*dst++ = 127;
*dst++ = 127;
col += 3;
}
// rows start on 4 byte boundaries
while ((col % 4) != 0)
{
dst++;
col++;
}
}
return TRUE;
}
//--------------
// FillBitmap
//--------------
{
BYTE r, g, b, a;
if (bStretched)
{
// 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))
{
}
else
{
}
{
{
*pImg++ = r;
*pImg++ = g;
*pImg++ = b;
if (cImgChannels == 4)
{
+ 3);
*pImg++ = a;
}
}
}
// calculate row-bytes
// copy image to screen
{
break;
{
break;
r = *src++;
g = *src++;
b = *src++;
*dst++ = b; /* note the reverse order */
*dst++ = g;
*dst++ = r;
if (cImgChannels == 4)
{
a = *src++;
}
}
}
// free memory
if (pStretchedImage != NULL)
{
}
}
// process the image not-stretched
else
{
// calculate the central position
// check for image larger than window
// calculate both row-bytes
// copy image to screen
{
break;
{
break;
r = *src++;
g = *src++;
b = *src++;
*dst++ = b; /* note the reverse order */
*dst++ = g;
*dst++ = r;
if (cImgChannels == 4)
{
a = *src++;
}
}
}
}
return TRUE;
}
//-----------------
// end of source
//-----------------