15 #define PROGNAME "VisualPng" 16 #define LONGNAME "Win32 Viewer for PNG-files" 17 #define VERSION "1.0 of 2000 June 07" 49 PTSTR pstrPrevName, PTSTR pstrNextName);
52 png_byte **ppbImage,
int *pxImgSize,
int *pyImgSize,
int *piChannels,
56 BYTE **ppDiData,
int cxWinSize,
int cyWinSize,
57 BYTE *pbImage,
int cxImgSize,
int cyImgSize,
int cImgChannels,
61 BYTE *pDiData,
int cxWinSize,
int cyWinSize);
64 BYTE *pDiData,
int cxWinSize,
int cyWinSize,
65 BYTE *pbImage,
int cxImgSize,
int cyImgSize,
int cImgChannels,
73 static char szCmdFileName [MAX_PATH];
77 int WINAPI
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
78 PSTR szCmdLine,
int iCmdShow)
84 int ixBorders, iyBorders;
86 wndclass.style = CS_HREDRAW | CS_VREDRAW;
88 wndclass.cbClsExtra = 0;
89 wndclass.cbWndExtra = 0;
90 wndclass.hInstance = hInstance;
91 wndclass.hIcon = LoadIcon (hInstance, szIconName) ;
92 wndclass.hCursor = LoadCursor (
NULL, IDC_ARROW);
93 wndclass.hbrBackground =
NULL;
94 wndclass.lpszMenuName = szProgName;
95 wndclass.lpszClassName = szProgName;
97 if (!RegisterClass (&wndclass))
99 MessageBox (
NULL, TEXT (
"Error: this program requires Windows NT!"),
100 szProgName, MB_ICONERROR);
105 if ((szCmdLine !=
NULL) && (*szCmdLine !=
'\0'))
106 if (szCmdLine[0] ==
'"')
107 strncpy (szCmdFileName, szCmdLine + 1, strlen(szCmdLine) - 2);
109 strcpy (szCmdFileName, szCmdLine);
111 strcpy (szCmdFileName,
"");
114 ixBorders = 2 * (GetSystemMetrics (SM_CXBORDER) +
115 GetSystemMetrics (SM_CXDLGFRAME));
116 iyBorders = 2 * (GetSystemMetrics (SM_CYBORDER) +
117 GetSystemMetrics (SM_CYDLGFRAME)) +
118 GetSystemMetrics (SM_CYCAPTION) +
119 GetSystemMetrics (SM_CYMENUSIZE) +
122 hwnd = CreateWindow (szProgName, szAppName,
124 CW_USEDEFAULT, CW_USEDEFAULT,
125 512 + 2 *
MARGIN + ixBorders, 384 + 2 *
MARGIN + iyBorders,
129 ShowWindow (hwnd, iCmdShow);
132 hAccel = LoadAccelerators (hInstance, szProgName);
134 while (GetMessage (&msg,
NULL, 0, 0))
136 if (!TranslateAccelerator (hwnd, hAccel, &msg))
138 TranslateMessage (&msg);
139 DispatchMessage (&msg);
148 static HINSTANCE hInstance ;
150 static PAINTSTRUCT ps;
153 static BITMAPFILEHEADER *pbmfh;
154 static BITMAPINFOHEADER *pbmih;
155 static BYTE *pbImage;
156 static int cxWinSize, cyWinSize;
157 static int cxImgSize, cyImgSize;
158 static int cImgChannels;
159 static png_color bkgColor = {127, 127, 127};
163 static BYTE *pDib =
NULL;
164 static BYTE *pDiData =
NULL;
166 static TCHAR szImgPathName [MAX_PATH];
167 static TCHAR szTitleName [MAX_PATH];
169 static TCHAR *pPngFileList =
NULL;
170 static int iPngFileCount;
171 static int iPngFileIndex;
178 hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
181 strcpy (szImgPathName,
"");
185 if (szCmdFileName[0] !=
'\0')
187 strcpy (szImgPathName, szCmdFileName);
192 BuildPngList (szImgPathName, &pPngFileList, &iPngFileCount,
198 &pbImage, &cxImgSize, &cyImgSize, &cImgChannels, &bkgColor))
207 DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
208 pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
214 cxWinSize = LOWORD (lParam);
215 cyWinSize = HIWORD (lParam);
223 DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
224 pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
228 case WM_INITMENUPOPUP:
229 hMenu = GetMenu (hwnd);
239 hMenu = GetMenu (hwnd);
241 switch (LOWORD (wParam))
253 BuildPngList (szImgPathName, &pPngFileList, &iPngFileCount,
259 &pbImage, &cxImgSize, &cyImgSize, &cImgChannels, &bkgColor))
268 DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
269 pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
282 SetCursor (LoadCursor (
NULL, IDC_WAIT));
285 bOk =
PngSaveImage (szImgPathName, pDiData, cxWinSize, cyWinSize,
289 SetCursor (LoadCursor (
NULL, IDC_ARROW));
292 MessageBox (hwnd, TEXT (
"Error in saving the PNG image"),
293 szProgName, MB_ICONEXCLAMATION | MB_OK);
300 if (
SearchPngList (pPngFileList, iPngFileCount, &iPngFileIndex,
301 NULL, szImgPathName))
303 if (strcmp (szImgPathName,
"") == 0)
309 &cxImgSize, &cyImgSize, &cImgChannels, &bkgColor))
318 DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
319 pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
328 if (
SearchPngList (pPngFileList, iPngFileCount, &iPngFileIndex,
329 szImgPathName,
NULL))
332 if (strcmp (szImgPathName,
"") == 0)
337 if (!
LoadImageFile (hwnd, szImgPathName, &pbImage, &cxImgSize,
338 &cyImgSize, &cImgChannels, &bkgColor))
347 DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
348 pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
367 if (pPngFileList !=
NULL)
380 bStretched = !bStretched;
392 DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
393 pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
398 DialogBox (hInstance, TEXT (
"AboutBox"), hwnd,
AboutDlgProc) ;
406 hdc = BeginPaint (hwnd, &ps);
409 SetDIBitsToDevice (hdc, 0, 0, cxWinSize, cyWinSize, 0, 0,
410 0, cyWinSize, pDiData, (BITMAPINFO *) pDib, DIB_RGB_COLORS);
412 EndPaint (hwnd, &ps);
426 return DefWindowProc (hwnd, message, wParam, lParam);
430 WPARAM wParam, LPARAM lParam)
435 ShowWindow (hDlg, SW_HIDE);
437 ShowWindow (hDlg, SW_SHOW);
441 switch (LOWORD (wParam))
445 EndDialog (hDlg, 0) ;
459 RECT rChild, rParent, rWorkArea;
460 int wChild, hChild, wParent, hParent;
465 GetWindowRect (hwndChild, &rChild);
466 wChild = rChild.right - rChild.left;
467 hChild = rChild.bottom - rChild.top;
470 GetWindowRect (hwndParent, &rParent);
471 wParent = rParent.right - rParent.left;
472 hParent = rParent.bottom - rParent.top;
475 bResult = SystemParametersInfo(
481 rWorkArea.left = rWorkArea.top = 0;
482 rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
483 rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
487 xNew = rParent.left + ((wParent - wChild) /2);
488 if (xNew < rWorkArea.left) {
489 xNew = rWorkArea.left;
490 }
else if ((xNew+wChild) > rWorkArea.right) {
491 xNew = rWorkArea.right - wChild;
495 yNew = rParent.top + ((hParent - hChild) /2);
496 if (yNew < rWorkArea.top) {
497 yNew = rWorkArea.top;
498 }
else if ((yNew+hChild) > rWorkArea.bottom) {
499 yNew = rWorkArea.bottom - hChild;
503 return SetWindowPos (hwndChild,
NULL, xNew, yNew, 0, 0, SWP_NOSIZE |
514 static TCHAR szImgPathName [MAX_PATH];
515 static TCHAR szImgFileName [MAX_PATH];
516 static TCHAR szImgFindName [MAX_PATH];
518 WIN32_FIND_DATA finddata;
521 static TCHAR szTmp [MAX_PATH];
528 if (*ppFileList !=
NULL)
536 strcpy (szImgPathName, pstrPathName);
537 strcpy (szImgFileName, strrchr (pstrPathName,
'\\') + 1);
539 strcpy (szImgFindName, szImgPathName);
540 *(strrchr (szImgFindName,
'\\') + 1) =
'\0';
541 strcat (szImgFindName,
"*.png");
547 hFind = FindFirstFile(szImgFindName, &finddata);
548 bOk = (hFind != (HANDLE) -1);
553 bOk = FindNextFile(hFind, &finddata);
559 *ppFileList = (TCHAR *)
malloc (*pFileCount * MAX_PATH);
563 hFind = FindFirstFile(szImgFindName, &finddata);
564 bOk = (hFind != (HANDLE) -1);
570 strcpy (*ppFileList + ii, szImgPathName);
571 strcpy (strrchr(*ppFileList + ii,
'\\') + 1, finddata.cFileName);
573 if (strcmp(pstrPathName, *ppFileList + ii) == 0)
579 bOk = FindNextFile(hFind, &finddata);
585 for (i = 0; i < *pFileCount - 1; i++)
588 for (j = i+1; j < *pFileCount; j++)
591 if (strcmp (*ppFileList + ii, *ppFileList + jj) > 0)
593 strcpy (szTmp, *ppFileList + jj);
594 strcpy (*ppFileList + jj, *ppFileList + ii);
595 strcpy (*ppFileList + ii, szTmp);
599 if (*pFileIndex == i)
602 if (*pFileIndex == j)
616 TCHAR *pFileList,
int FileCount,
int *pFileIndex,
617 PTSTR pstrPrevName, PTSTR pstrNextName)
623 if (pstrPrevName !=
NULL)
628 *pFileIndex = FileCount - 1;
630 strcpy (pstrPrevName, pFileList + (*pFileIndex * MAX_PATH));
635 if (pstrNextName !=
NULL)
637 if (*pFileIndex < FileCount - 1)
642 strcpy (pstrNextName, pFileList + (*pFileIndex * MAX_PATH));
658 png_byte **ppbImage,
int *pxImgSize,
int *pyImgSize,
661 static TCHAR szTmp [MAX_PATH];
673 SetCursor (LoadCursor (
NULL, IDC_WAIT));
676 PngLoadImage (pstrPathName, ppbImage, pxImgSize, pyImgSize, piChannels,
680 SetCursor (LoadCursor (
NULL, IDC_ARROW));
682 if (*ppbImage !=
NULL)
684 sprintf (szTmp,
"VisualPng - %s", strrchr(pstrPathName,
'\\') + 1);
685 SetWindowText (hwnd, szTmp);
689 MessageBox (hwnd, TEXT (
"Error in loading the PNG image"),
690 szProgName, MB_ICONEXCLAMATION | MB_OK);
702 BYTE **ppDiData,
int cxWinSize,
int cyWinSize,
703 BYTE *pbImage,
int cxImgSize,
int cyImgSize,
int cImgChannels,
707 BYTE *pDiData = *ppDiData;
709 BITMAPINFOHEADER *pbmih;
717 wDIRowBytes = (WORD) ((3 * cxWinSize + 3L) >> 2) << 2;
725 if (!(pDib = (BYTE *)
malloc (
sizeof(BITMAPINFOHEADER) +
726 wDIRowBytes * cyWinSize)))
728 MessageBox (hwnd, TEXT (
"Error in displaying the PNG image"),
729 szProgName, MB_ICONEXCLAMATION | MB_OK);
730 *ppDib = pDib =
NULL;
734 memset (pDib, 0,
sizeof(BITMAPINFOHEADER));
738 pbmih = (BITMAPINFOHEADER *) pDib;
739 pbmih->biSize =
sizeof(BITMAPINFOHEADER);
740 pbmih->biWidth = cxWinSize;
741 pbmih->biHeight = -((
long) cyWinSize);
743 pbmih->biBitCount = 24;
744 pbmih->biCompression = 0;
745 pDiData = pDib +
sizeof(BITMAPINFOHEADER);
757 pDiData, cxWinSize, cyWinSize,
758 pbImage, cxImgSize, cyImgSize, cImgChannels,
777 for (y = 0; y < cyWinSize; y++)
780 for (x = 0; x < cxWinSize; x++)
789 while ((col % 4) != 0)
804 BYTE *pDiData,
int cxWinSize,
int cyWinSize,
805 BYTE *pbImage,
int cxImgSize,
int cyImgSize,
int cImgChannels,
808 BYTE *pStretchedImage;
812 const int cDIChannels = 3;
815 int cxNewSize, cyNewSize;
816 int cxImgPos, cyImgPos;
824 cxNewSize = cxWinSize - 2 *
MARGIN;
825 cyNewSize = cyWinSize - 2 *
MARGIN;
832 if ((cyNewSize * cxImgSize) > (cyImgSize * cxNewSize))
834 cyNewSize = cxNewSize * cyImgSize / cxImgSize;
836 cyImgPos = (cyWinSize - cyNewSize) / 2;
840 cxNewSize = cyNewSize * cxImgSize / cyImgSize;
842 cxImgPos = (cxWinSize - cxNewSize) / 2;
845 pStretchedImage =
malloc (cImgChannels * cxNewSize * cyNewSize);
846 pImg = pStretchedImage;
848 for (yNew = 0; yNew < cyNewSize; yNew++)
850 yOld = yNew * cyImgSize / cyNewSize;
851 for (xNew = 0; xNew < cxNewSize; xNew++)
853 xOld = xNew * cxImgSize / cxNewSize;
855 r = *(pbImage + cImgChannels * ((yOld * cxImgSize) + xOld) + 0);
856 g = *(pbImage + cImgChannels * ((yOld * cxImgSize) + xOld) + 1);
857 b = *(pbImage + cImgChannels * ((yOld * cxImgSize) + xOld) + 2);
861 if (cImgChannels == 4)
863 a = *(pbImage + cImgChannels * ((yOld * cxImgSize) + xOld)
872 wImgRowBytes = cImgChannels * cxNewSize;
873 wDIRowBytes = (WORD) ((cDIChannels * cxWinSize + 3L) >> 2) << 2;
877 for (yImg = 0, yWin = cyImgPos; yImg < cyNewSize; yImg++, yWin++)
879 if (yWin >= cyWinSize - cyImgPos)
881 src = pStretchedImage + yImg * wImgRowBytes;
882 dst = pDiData + yWin * wDIRowBytes + cxImgPos * cDIChannels;
884 for (xImg = 0, xWin = cxImgPos; xImg < cxNewSize; xImg++, xWin++)
886 if (xWin >= cxWinSize - cxImgPos)
894 if (cImgChannels == 4)
903 if (pStretchedImage !=
NULL)
905 free (pStretchedImage);
906 pStretchedImage =
NULL;
917 cxImgPos = (cxWinSize - cxImgSize) / 2;
918 cyImgPos = (cyWinSize - cyImgSize) / 2;
929 wImgRowBytes = cImgChannels * cxImgSize;
930 wDIRowBytes = (WORD) ((cDIChannels * cxWinSize + 3L) >> 2) << 2;
934 for (yImg = 0, yWin = cyImgPos; yImg < cyImgSize; yImg++, yWin++)
936 if (yWin >= cyWinSize -
MARGIN)
938 src = pbImage + yImg * wImgRowBytes;
939 dst = pDiData + yWin * wDIRowBytes + cxImgPos * cDIChannels;
941 for (xImg = 0, xWin = cxImgPos; xImg < cxImgSize; xImg++, xWin++)
943 if (xWin >= cxWinSize -
MARGIN)
951 if (cImgChannels == 4)
GLboolean GLboolean GLboolean GLboolean a
BOOL BuildPngList(PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount, int *pFileIndex)
GLint GLint GLint GLint GLint GLint y
BOOL PngFileOpenDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
void PngFileInitialize(HWND hwnd)
BOOL LoadImageFile(HWND hwnd, PTSTR pstrPathName, png_byte **ppbImage, int *pxImgSize, int *pyImgSize, int *piChannels, png_color *pBkgColor)
BOOL SearchPngList(TCHAR *pFileList, int FileCount, int *pFileIndex, PTSTR pstrPrevName, PTSTR pstrNextName)
GLint GLint GLint GLint GLint x
GLboolean GLboolean GLboolean b
BOOL FillBitmap(BYTE *pDiData, int cxWinSize, int cyWinSize, BYTE *pbImage, int cxImgSize, int cyImgSize, int cImgChannels, BOOL bStretched)
BOOL PngFileSaveDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
#define IDM_FILE_PREVIOUS
BOOL CenterAbout(HWND hwndChild, HWND hwndParent)
GLdouble GLdouble GLdouble r
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
BOOL InitBitmap(BYTE *pDiData, int cxWinSize, int cyWinSize)
GLenum GLuint GLenum GLsizei const GLchar * message
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
BOOL PngLoadImage(PTSTR pstrFileName, png_byte **ppbImageData, int *piWidth, int *piHeight, int *piChannels, png_color *pBkgColor)
BOOL CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM)
BOOL DisplayImage(HWND hwnd, BYTE **ppDib, BYTE **ppDiData, int cxWinSize, int cyWinSize, BYTE *pbImage, int cxImgSize, int cyImgSize, int cImgChannels, BOOL bStretched)
#define IDM_OPTIONS_STRETCH
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM)
BOOL PngSaveImage(PTSTR pstrFileName, png_byte *pDiData, int iWidth, int iHeight, png_color bkgColor)