VisualPng.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*------------------------------------
  2. * VisualPng.C -- Shows a PNG image
  3. *------------------------------------
  4. *
  5. * Copyright 2000, Willem van Schaik.
  6. *
  7. * This code is released under the libpng license.
  8. * For conditions of distribution and use, see the disclaimer
  9. * and license in png.h
  10. */
  11. /* switches */
  12. /* defines */
  13. #define PROGNAME "VisualPng"
  14. #define LONGNAME "Win32 Viewer for PNG-files"
  15. #define VERSION "1.0 of 2000 June 07"
  16. /* constants */
  17. #define MARGIN 8
  18. /* standard includes */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <windows.h>
  23. #include <zlib.h>
  24. /* application includes */
  25. #include "png.h"
  26. #include "pngfile.h"
  27. #include "resource.h"
  28. /* macros */
  29. /* function prototypes */
  30. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  31. BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ;
  32. BOOL CenterAbout (HWND hwndChild, HWND hwndParent);
  33. BOOL BuildPngList (PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount,
  34. int *pFileIndex);
  35. BOOL SearchPngList (TCHAR *pFileList, int FileCount, int *pFileIndex,
  36. PTSTR pstrPrevName, PTSTR pstrNextName);
  37. BOOL LoadImageFile(HWND hwnd, PTSTR pstrPathName,
  38. png_byte **ppbImage, int *pxImgSize, int *pyImgSize, int *piChannels,
  39. png_color *pBkgColor);
  40. BOOL DisplayImage (HWND hwnd, BYTE **ppDib,
  41. BYTE **ppDiData, int cxWinSize, int cyWinSize,
  42. BYTE *pbImage, int cxImgSize, int cyImgSize, int cImgChannels,
  43. BOOL bStretched);
  44. BOOL InitBitmap (
  45. BYTE *pDiData, int cxWinSize, int cyWinSize);
  46. BOOL FillBitmap (
  47. BYTE *pDiData, int cxWinSize, int cyWinSize,
  48. BYTE *pbImage, int cxImgSize, int cyImgSize, int cImgChannels,
  49. BOOL bStretched);
  50. /* a few global variables */
  51. static char *szProgName = PROGNAME;
  52. static char *szAppName = LONGNAME;
  53. static char *szIconName = PROGNAME;
  54. static char szCmdFileName [MAX_PATH];
  55. /* MAIN routine */
  56. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  57. PSTR szCmdLine, int iCmdShow)
  58. {
  59. HACCEL hAccel;
  60. HWND hwnd;
  61. MSG msg;
  62. WNDCLASS wndclass;
  63. int ixBorders, iyBorders;
  64. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  65. wndclass.lpfnWndProc = WndProc;
  66. wndclass.cbClsExtra = 0;
  67. wndclass.cbWndExtra = 0;
  68. wndclass.hInstance = hInstance;
  69. wndclass.hIcon = LoadIcon (hInstance, szIconName) ;
  70. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  71. wndclass.hbrBackground = NULL; /* (HBRUSH) GetStockObject (GRAY_BRUSH); */
  72. wndclass.lpszMenuName = szProgName;
  73. wndclass.lpszClassName = szProgName;
  74. if (!RegisterClass (&wndclass))
  75. {
  76. MessageBox (NULL, TEXT ("Error: this program requires Windows NT!"),
  77. szProgName, MB_ICONERROR);
  78. return 0;
  79. }
  80. /* if filename given on commandline, store it */
  81. if ((szCmdLine != NULL) && (*szCmdLine != '\0'))
  82. if (szCmdLine[0] == '"')
  83. strncpy (szCmdFileName, szCmdLine + 1, strlen(szCmdLine) - 2);
  84. else
  85. strcpy (szCmdFileName, szCmdLine);
  86. else
  87. strcpy (szCmdFileName, "");
  88. /* calculate size of window-borders */
  89. ixBorders = 2 * (GetSystemMetrics (SM_CXBORDER) +
  90. GetSystemMetrics (SM_CXDLGFRAME));
  91. iyBorders = 2 * (GetSystemMetrics (SM_CYBORDER) +
  92. GetSystemMetrics (SM_CYDLGFRAME)) +
  93. GetSystemMetrics (SM_CYCAPTION) +
  94. GetSystemMetrics (SM_CYMENUSIZE) +
  95. 1; /* WvS: don't ask me why? */
  96. hwnd = CreateWindow (szProgName, szAppName,
  97. WS_OVERLAPPEDWINDOW,
  98. CW_USEDEFAULT, CW_USEDEFAULT,
  99. 512 + 2 * MARGIN + ixBorders, 384 + 2 * MARGIN + iyBorders,
  100. /* CW_USEDEFAULT, CW_USEDEFAULT, */
  101. NULL, NULL, hInstance, NULL);
  102. ShowWindow (hwnd, iCmdShow);
  103. UpdateWindow (hwnd);
  104. hAccel = LoadAccelerators (hInstance, szProgName);
  105. while (GetMessage (&msg, NULL, 0, 0))
  106. {
  107. if (!TranslateAccelerator (hwnd, hAccel, &msg))
  108. {
  109. TranslateMessage (&msg);
  110. DispatchMessage (&msg);
  111. }
  112. }
  113. return msg.wParam;
  114. }
  115. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
  116. LPARAM lParam)
  117. {
  118. static HINSTANCE hInstance ;
  119. static HDC hdc;
  120. static PAINTSTRUCT ps;
  121. static HMENU hMenu;
  122. static BITMAPFILEHEADER *pbmfh;
  123. static BITMAPINFOHEADER *pbmih;
  124. static BYTE *pbImage;
  125. static int cxWinSize, cyWinSize;
  126. static int cxImgSize, cyImgSize;
  127. static int cImgChannels;
  128. static png_color bkgColor = {127, 127, 127};
  129. static BOOL bStretched = TRUE;
  130. static BYTE *pDib = NULL;
  131. static BYTE *pDiData = NULL;
  132. static TCHAR szImgPathName [MAX_PATH];
  133. static TCHAR szTitleName [MAX_PATH];
  134. static TCHAR *pPngFileList = NULL;
  135. static int iPngFileCount;
  136. static int iPngFileIndex;
  137. BOOL bOk;
  138. switch (message)
  139. {
  140. case WM_CREATE:
  141. hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  142. PngFileInitialize (hwnd);
  143. strcpy (szImgPathName, "");
  144. /* in case we process file given on command-line */
  145. if (szCmdFileName[0] != '\0')
  146. {
  147. strcpy (szImgPathName, szCmdFileName);
  148. /* read the other png-files in the directory for later */
  149. /* next/previous commands */
  150. BuildPngList (szImgPathName, &pPngFileList, &iPngFileCount,
  151. &iPngFileIndex);
  152. /* load the image from file */
  153. if (!LoadImageFile (hwnd, szImgPathName,
  154. &pbImage, &cxImgSize, &cyImgSize, &cImgChannels, &bkgColor))
  155. return 0;
  156. /* invalidate the client area for later update */
  157. InvalidateRect (hwnd, NULL, TRUE);
  158. /* display the PNG into the DIBitmap */
  159. DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
  160. pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
  161. }
  162. return 0;
  163. case WM_SIZE:
  164. cxWinSize = LOWORD (lParam);
  165. cyWinSize = HIWORD (lParam);
  166. /* invalidate the client area for later update */
  167. InvalidateRect (hwnd, NULL, TRUE);
  168. /* display the PNG into the DIBitmap */
  169. DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
  170. pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
  171. return 0;
  172. case WM_INITMENUPOPUP:
  173. hMenu = GetMenu (hwnd);
  174. if (pbImage)
  175. EnableMenuItem (hMenu, IDM_FILE_SAVE, MF_ENABLED);
  176. else
  177. EnableMenuItem (hMenu, IDM_FILE_SAVE, MF_GRAYED);
  178. return 0;
  179. case WM_COMMAND:
  180. hMenu = GetMenu (hwnd);
  181. switch (LOWORD (wParam))
  182. {
  183. case IDM_FILE_OPEN:
  184. /* show the File Open dialog box */
  185. if (!PngFileOpenDlg (hwnd, szImgPathName, szTitleName))
  186. return 0;
  187. /* read the other png-files in the directory for later */
  188. /* next/previous commands */
  189. BuildPngList (szImgPathName, &pPngFileList, &iPngFileCount,
  190. &iPngFileIndex);
  191. /* load the image from file */
  192. if (!LoadImageFile (hwnd, szImgPathName,
  193. &pbImage, &cxImgSize, &cyImgSize, &cImgChannels, &bkgColor))
  194. return 0;
  195. /* invalidate the client area for later update */
  196. InvalidateRect (hwnd, NULL, TRUE);
  197. /* display the PNG into the DIBitmap */
  198. DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
  199. pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
  200. return 0;
  201. case IDM_FILE_SAVE:
  202. /* show the File Save dialog box */
  203. if (!PngFileSaveDlg (hwnd, szImgPathName, szTitleName))
  204. return 0;
  205. /* save the PNG to a disk file */
  206. SetCursor (LoadCursor (NULL, IDC_WAIT));
  207. ShowCursor (TRUE);
  208. bOk = PngSaveImage (szImgPathName, pDiData, cxWinSize, cyWinSize,
  209. bkgColor);
  210. ShowCursor (FALSE);
  211. SetCursor (LoadCursor (NULL, IDC_ARROW));
  212. if (!bOk)
  213. MessageBox (hwnd, TEXT ("Error in saving the PNG image"),
  214. szProgName, MB_ICONEXCLAMATION | MB_OK);
  215. return 0;
  216. case IDM_FILE_NEXT:
  217. /* read next entry in the directory */
  218. if (SearchPngList (pPngFileList, iPngFileCount, &iPngFileIndex,
  219. NULL, szImgPathName))
  220. {
  221. if (strcmp (szImgPathName, "") == 0)
  222. return 0;
  223. /* load the image from file */
  224. if (!LoadImageFile (hwnd, szImgPathName, &pbImage,
  225. &cxImgSize, &cyImgSize, &cImgChannels, &bkgColor))
  226. return 0;
  227. /* invalidate the client area for later update */
  228. InvalidateRect (hwnd, NULL, TRUE);
  229. /* display the PNG into the DIBitmap */
  230. DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
  231. pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
  232. }
  233. return 0;
  234. case IDM_FILE_PREVIOUS:
  235. /* read previous entry in the directory */
  236. if (SearchPngList (pPngFileList, iPngFileCount, &iPngFileIndex,
  237. szImgPathName, NULL))
  238. {
  239. if (strcmp (szImgPathName, "") == 0)
  240. return 0;
  241. /* load the image from file */
  242. if (!LoadImageFile (hwnd, szImgPathName, &pbImage, &cxImgSize,
  243. &cyImgSize, &cImgChannels, &bkgColor))
  244. return 0;
  245. /* invalidate the client area for later update */
  246. InvalidateRect (hwnd, NULL, TRUE);
  247. /* display the PNG into the DIBitmap */
  248. DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
  249. pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
  250. }
  251. return 0;
  252. case IDM_FILE_EXIT:
  253. /* more cleanup needed... */
  254. /* free image buffer */
  255. if (pDib != NULL)
  256. {
  257. free (pDib);
  258. pDib = NULL;
  259. }
  260. /* free file-list */
  261. if (pPngFileList != NULL)
  262. {
  263. free (pPngFileList);
  264. pPngFileList = NULL;
  265. }
  266. /* let's go ... */
  267. exit (0);
  268. return 0;
  269. case IDM_OPTIONS_STRETCH:
  270. bStretched = !bStretched;
  271. if (bStretched)
  272. CheckMenuItem (hMenu, IDM_OPTIONS_STRETCH, MF_CHECKED);
  273. else
  274. CheckMenuItem (hMenu, IDM_OPTIONS_STRETCH, MF_UNCHECKED);
  275. /* invalidate the client area for later update */
  276. InvalidateRect (hwnd, NULL, TRUE);
  277. /* display the PNG into the DIBitmap */
  278. DisplayImage (hwnd, &pDib, &pDiData, cxWinSize, cyWinSize,
  279. pbImage, cxImgSize, cyImgSize, cImgChannels, bStretched);
  280. return 0;
  281. case IDM_HELP_ABOUT:
  282. DialogBox (hInstance, TEXT ("AboutBox"), hwnd, AboutDlgProc) ;
  283. return 0;
  284. } /* end switch */
  285. break;
  286. case WM_PAINT:
  287. hdc = BeginPaint (hwnd, &ps);
  288. if (pDib)
  289. SetDIBitsToDevice (hdc, 0, 0, cxWinSize, cyWinSize, 0, 0,
  290. 0, cyWinSize, pDiData, (BITMAPINFO *) pDib, DIB_RGB_COLORS);
  291. EndPaint (hwnd, &ps);
  292. return 0;
  293. case WM_DESTROY:
  294. if (pbmfh)
  295. {
  296. free (pbmfh);
  297. pbmfh = NULL;
  298. }
  299. PostQuitMessage (0);
  300. return 0;
  301. }
  302. return DefWindowProc (hwnd, message, wParam, lParam);
  303. }
  304. BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message,
  305. WPARAM wParam, LPARAM lParam)
  306. {
  307. switch (message)
  308. {
  309. case WM_INITDIALOG :
  310. ShowWindow (hDlg, SW_HIDE);
  311. CenterAbout (hDlg, GetWindow (hDlg, GW_OWNER));
  312. ShowWindow (hDlg, SW_SHOW);
  313. return TRUE ;
  314. case WM_COMMAND :
  315. switch (LOWORD (wParam))
  316. {
  317. case IDOK :
  318. case IDCANCEL :
  319. EndDialog (hDlg, 0) ;
  320. return TRUE ;
  321. }
  322. break ;
  323. }
  324. return FALSE ;
  325. }
  326. /*---------------
  327. * CenterAbout
  328. *---------------
  329. */
  330. BOOL CenterAbout (HWND hwndChild, HWND hwndParent)
  331. {
  332. RECT rChild, rParent, rWorkArea;
  333. int wChild, hChild, wParent, hParent;
  334. int xNew, yNew;
  335. BOOL bResult;
  336. /* Get the Height and Width of the child window */
  337. GetWindowRect (hwndChild, &rChild);
  338. wChild = rChild.right - rChild.left;
  339. hChild = rChild.bottom - rChild.top;
  340. /* Get the Height and Width of the parent window */
  341. GetWindowRect (hwndParent, &rParent);
  342. wParent = rParent.right - rParent.left;
  343. hParent = rParent.bottom - rParent.top;
  344. /* Get the limits of the 'workarea' */
  345. bResult = SystemParametersInfo(
  346. SPI_GETWORKAREA, /* system parameter to query or set */
  347. sizeof(RECT),
  348. &rWorkArea,
  349. 0);
  350. if (!bResult) {
  351. rWorkArea.left = rWorkArea.top = 0;
  352. rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
  353. rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
  354. }
  355. /* Calculate new X position, then adjust for workarea */
  356. xNew = rParent.left + ((wParent - wChild) /2);
  357. if (xNew < rWorkArea.left) {
  358. xNew = rWorkArea.left;
  359. } else if ((xNew+wChild) > rWorkArea.right) {
  360. xNew = rWorkArea.right - wChild;
  361. }
  362. /* Calculate new Y position, then adjust for workarea */
  363. yNew = rParent.top + ((hParent - hChild) /2);
  364. if (yNew < rWorkArea.top) {
  365. yNew = rWorkArea.top;
  366. } else if ((yNew+hChild) > rWorkArea.bottom) {
  367. yNew = rWorkArea.bottom - hChild;
  368. }
  369. /* Set it, and return */
  370. return SetWindowPos (hwndChild, NULL, xNew, yNew, 0, 0, SWP_NOSIZE |
  371. SWP_NOZORDER);
  372. }
  373. /*----------------
  374. * BuildPngList
  375. *----------------
  376. */
  377. BOOL BuildPngList (PTSTR pstrPathName, TCHAR **ppFileList, int *pFileCount,
  378. int *pFileIndex)
  379. {
  380. static TCHAR szImgPathName [MAX_PATH];
  381. static TCHAR szImgFileName [MAX_PATH];
  382. static TCHAR szImgFindName [MAX_PATH];
  383. WIN32_FIND_DATA finddata;
  384. HANDLE hFind;
  385. static TCHAR szTmp [MAX_PATH];
  386. BOOL bOk;
  387. int i, ii;
  388. int j, jj;
  389. /* free previous file-list */
  390. if (*ppFileList != NULL)
  391. {
  392. free (*ppFileList);
  393. *ppFileList = NULL;
  394. }
  395. /* extract foldername, filename and search-name */
  396. strcpy (szImgPathName, pstrPathName);
  397. strcpy (szImgFileName, strrchr (pstrPathName, '\\') + 1);
  398. strcpy (szImgFindName, szImgPathName);
  399. *(strrchr (szImgFindName, '\\') + 1) = '\0';
  400. strcat (szImgFindName, "*.png");
  401. /* first cycle: count number of files in directory for memory allocation */
  402. *pFileCount = 0;
  403. hFind = FindFirstFile(szImgFindName, &finddata);
  404. bOk = (hFind != (HANDLE) -1);
  405. while (bOk)
  406. {
  407. *pFileCount += 1;
  408. bOk = FindNextFile(hFind, &finddata);
  409. }
  410. FindClose(hFind);
  411. /* allocation memory for file-list */
  412. *ppFileList = (TCHAR *) malloc (*pFileCount * MAX_PATH);
  413. /* second cycle: read directory and store filenames in file-list */
  414. hFind = FindFirstFile(szImgFindName, &finddata);
  415. bOk = (hFind != (HANDLE) -1);
  416. i = 0;
  417. ii = 0;
  418. while (bOk)
  419. {
  420. strcpy (*ppFileList + ii, szImgPathName);
  421. strcpy (strrchr(*ppFileList + ii, '\\') + 1, finddata.cFileName);
  422. if (strcmp(pstrPathName, *ppFileList + ii) == 0)
  423. *pFileIndex = i;
  424. ii += MAX_PATH;
  425. i++;
  426. bOk = FindNextFile(hFind, &finddata);
  427. }
  428. FindClose(hFind);
  429. /* finally we must sort the file-list */
  430. for (i = 0; i < *pFileCount - 1; i++)
  431. {
  432. ii = i * MAX_PATH;
  433. for (j = i+1; j < *pFileCount; j++)
  434. {
  435. jj = j * MAX_PATH;
  436. if (strcmp (*ppFileList + ii, *ppFileList + jj) > 0)
  437. {
  438. strcpy (szTmp, *ppFileList + jj);
  439. strcpy (*ppFileList + jj, *ppFileList + ii);
  440. strcpy (*ppFileList + ii, szTmp);
  441. /* check if this was the current image that we moved */
  442. if (*pFileIndex == i)
  443. *pFileIndex = j;
  444. else
  445. if (*pFileIndex == j)
  446. *pFileIndex = i;
  447. }
  448. }
  449. }
  450. return TRUE;
  451. }
  452. /*----------------
  453. * SearchPngList
  454. *----------------
  455. */
  456. BOOL SearchPngList (
  457. TCHAR *pFileList, int FileCount, int *pFileIndex,
  458. PTSTR pstrPrevName, PTSTR pstrNextName)
  459. {
  460. if (FileCount > 0)
  461. {
  462. /* get previous entry */
  463. if (pstrPrevName != NULL)
  464. {
  465. if (*pFileIndex > 0)
  466. *pFileIndex -= 1;
  467. else
  468. *pFileIndex = FileCount - 1;
  469. strcpy (pstrPrevName, pFileList + (*pFileIndex * MAX_PATH));
  470. }
  471. /* get next entry */
  472. if (pstrNextName != NULL)
  473. {
  474. if (*pFileIndex < FileCount - 1)
  475. *pFileIndex += 1;
  476. else
  477. *pFileIndex = 0;
  478. strcpy (pstrNextName, pFileList + (*pFileIndex * MAX_PATH));
  479. }
  480. return TRUE;
  481. }
  482. else
  483. {
  484. return FALSE;
  485. }
  486. }
  487. /*-----------------
  488. * LoadImageFile
  489. *-----------------
  490. */
  491. BOOL LoadImageFile (HWND hwnd, PTSTR pstrPathName,
  492. png_byte **ppbImage, int *pxImgSize, int *pyImgSize,
  493. int *piChannels, png_color *pBkgColor)
  494. {
  495. static TCHAR szTmp [MAX_PATH];
  496. /* if there's an existing PNG, free the memory */
  497. if (*ppbImage)
  498. {
  499. free (*ppbImage);
  500. *ppbImage = NULL;
  501. }
  502. /* Load the entire PNG into memory */
  503. SetCursor (LoadCursor (NULL, IDC_WAIT));
  504. ShowCursor (TRUE);
  505. PngLoadImage (pstrPathName, ppbImage, pxImgSize, pyImgSize, piChannels,
  506. pBkgColor);
  507. ShowCursor (FALSE);
  508. SetCursor (LoadCursor (NULL, IDC_ARROW));
  509. if (*ppbImage != NULL)
  510. {
  511. sprintf (szTmp, "VisualPng - %s", strrchr(pstrPathName, '\\') + 1);
  512. SetWindowText (hwnd, szTmp);
  513. }
  514. else
  515. {
  516. MessageBox (hwnd, TEXT ("Error in loading the PNG image"),
  517. szProgName, MB_ICONEXCLAMATION | MB_OK);
  518. return FALSE;
  519. }
  520. return TRUE;
  521. }
  522. /*----------------
  523. * DisplayImage
  524. *----------------
  525. */
  526. BOOL DisplayImage (HWND hwnd, BYTE **ppDib,
  527. BYTE **ppDiData, int cxWinSize, int cyWinSize,
  528. BYTE *pbImage, int cxImgSize, int cyImgSize, int cImgChannels,
  529. BOOL bStretched)
  530. {
  531. BYTE *pDib = *ppDib;
  532. BYTE *pDiData = *ppDiData;
  533. /* BITMAPFILEHEADER *pbmfh; */
  534. BITMAPINFOHEADER *pbmih;
  535. WORD wDIRowBytes;
  536. png_color bkgBlack = {0, 0, 0};
  537. png_color bkgGray = {127, 127, 127};
  538. png_color bkgWhite = {255, 255, 255};
  539. /* allocate memory for the Device Independant bitmap */
  540. wDIRowBytes = (WORD) ((3 * cxWinSize + 3L) >> 2) << 2;
  541. if (pDib)
  542. {
  543. free (pDib);
  544. pDib = NULL;
  545. }
  546. if (!(pDib = (BYTE *) malloc (sizeof(BITMAPINFOHEADER) +
  547. wDIRowBytes * cyWinSize)))
  548. {
  549. MessageBox (hwnd, TEXT ("Error in displaying the PNG image"),
  550. szProgName, MB_ICONEXCLAMATION | MB_OK);
  551. *ppDib = pDib = NULL;
  552. return FALSE;
  553. }
  554. *ppDib = pDib;
  555. memset (pDib, 0, sizeof(BITMAPINFOHEADER));
  556. /* initialize the dib-structure */
  557. pbmih = (BITMAPINFOHEADER *) pDib;
  558. pbmih->biSize = sizeof(BITMAPINFOHEADER);
  559. pbmih->biWidth = cxWinSize;
  560. pbmih->biHeight = -((long) cyWinSize);
  561. pbmih->biPlanes = 1;
  562. pbmih->biBitCount = 24;
  563. pbmih->biCompression = 0;
  564. pDiData = pDib + sizeof(BITMAPINFOHEADER);
  565. *ppDiData = pDiData;
  566. /* first fill bitmap with gray and image border */
  567. InitBitmap (pDiData, cxWinSize, cyWinSize);
  568. /* then fill bitmap with image */
  569. if (pbImage)
  570. {
  571. FillBitmap (
  572. pDiData, cxWinSize, cyWinSize,
  573. pbImage, cxImgSize, cyImgSize, cImgChannels,
  574. bStretched);
  575. }
  576. return TRUE;
  577. }
  578. /*--------------
  579. * InitBitmap
  580. *--------------
  581. */
  582. BOOL InitBitmap (BYTE *pDiData, int cxWinSize, int cyWinSize)
  583. {
  584. BYTE *dst;
  585. int x, y, col;
  586. /* initialize the background with gray */
  587. dst = pDiData;
  588. for (y = 0; y < cyWinSize; y++)
  589. {
  590. col = 0;
  591. for (x = 0; x < cxWinSize; x++)
  592. {
  593. /* fill with GRAY */
  594. *dst++ = 127;
  595. *dst++ = 127;
  596. *dst++ = 127;
  597. col += 3;
  598. }
  599. /* rows start on 4 byte boundaries */
  600. while ((col % 4) != 0)
  601. {
  602. dst++;
  603. col++;
  604. }
  605. }
  606. return TRUE;
  607. }
  608. /*--------------
  609. * FillBitmap
  610. *--------------
  611. */
  612. BOOL FillBitmap (
  613. BYTE *pDiData, int cxWinSize, int cyWinSize,
  614. BYTE *pbImage, int cxImgSize, int cyImgSize, int cImgChannels,
  615. BOOL bStretched)
  616. {
  617. BYTE *pStretchedImage;
  618. BYTE *pImg;
  619. BYTE *src, *dst;
  620. BYTE r, g, b, a;
  621. const int cDIChannels = 3;
  622. WORD wImgRowBytes;
  623. WORD wDIRowBytes;
  624. int cxNewSize, cyNewSize;
  625. int cxImgPos, cyImgPos;
  626. int xImg, yImg;
  627. int xWin, yWin;
  628. int xOld, yOld;
  629. int xNew, yNew;
  630. if (bStretched)
  631. {
  632. cxNewSize = cxWinSize - 2 * MARGIN;
  633. cyNewSize = cyWinSize - 2 * MARGIN;
  634. /* stretch the image to it's window determined size */
  635. /* the following two are mathematically the same, but the first
  636. * has side-effects because of rounding
  637. */
  638. /* if ((cyNewSize / cxNewSize) > (cyImgSize / cxImgSize)) */
  639. if ((cyNewSize * cxImgSize) > (cyImgSize * cxNewSize))
  640. {
  641. cyNewSize = cxNewSize * cyImgSize / cxImgSize;
  642. cxImgPos = MARGIN;
  643. cyImgPos = (cyWinSize - cyNewSize) / 2;
  644. }
  645. else
  646. {
  647. cxNewSize = cyNewSize * cxImgSize / cyImgSize;
  648. cyImgPos = MARGIN;
  649. cxImgPos = (cxWinSize - cxNewSize) / 2;
  650. }
  651. pStretchedImage = malloc (cImgChannels * cxNewSize * cyNewSize);
  652. pImg = pStretchedImage;
  653. for (yNew = 0; yNew < cyNewSize; yNew++)
  654. {
  655. yOld = yNew * cyImgSize / cyNewSize;
  656. for (xNew = 0; xNew < cxNewSize; xNew++)
  657. {
  658. xOld = xNew * cxImgSize / cxNewSize;
  659. r = *(pbImage + cImgChannels * ((yOld * cxImgSize) + xOld) + 0);
  660. g = *(pbImage + cImgChannels * ((yOld * cxImgSize) + xOld) + 1);
  661. b = *(pbImage + cImgChannels * ((yOld * cxImgSize) + xOld) + 2);
  662. *pImg++ = r;
  663. *pImg++ = g;
  664. *pImg++ = b;
  665. if (cImgChannels == 4)
  666. {
  667. a = *(pbImage + cImgChannels * ((yOld * cxImgSize) + xOld)
  668. + 3);
  669. *pImg++ = a;
  670. }
  671. }
  672. }
  673. /* calculate row-bytes */
  674. wImgRowBytes = cImgChannels * cxNewSize;
  675. wDIRowBytes = (WORD) ((cDIChannels * cxWinSize + 3L) >> 2) << 2;
  676. /* copy image to screen */
  677. for (yImg = 0, yWin = cyImgPos; yImg < cyNewSize; yImg++, yWin++)
  678. {
  679. if (yWin >= cyWinSize - cyImgPos)
  680. break;
  681. src = pStretchedImage + yImg * wImgRowBytes;
  682. dst = pDiData + yWin * wDIRowBytes + cxImgPos * cDIChannels;
  683. for (xImg = 0, xWin = cxImgPos; xImg < cxNewSize; xImg++, xWin++)
  684. {
  685. if (xWin >= cxWinSize - cxImgPos)
  686. break;
  687. r = *src++;
  688. g = *src++;
  689. b = *src++;
  690. *dst++ = b; /* note the reverse order */
  691. *dst++ = g;
  692. *dst++ = r;
  693. if (cImgChannels == 4)
  694. {
  695. a = *src++;
  696. }
  697. }
  698. }
  699. /* free memory */
  700. if (pStretchedImage != NULL)
  701. {
  702. free (pStretchedImage);
  703. pStretchedImage = NULL;
  704. }
  705. }
  706. /* process the image not-stretched */
  707. else
  708. {
  709. /* calculate the central position */
  710. cxImgPos = (cxWinSize - cxImgSize) / 2;
  711. cyImgPos = (cyWinSize - cyImgSize) / 2;
  712. /* check for image larger than window */
  713. if (cxImgPos < MARGIN)
  714. cxImgPos = MARGIN;
  715. if (cyImgPos < MARGIN)
  716. cyImgPos = MARGIN;
  717. /* calculate both row-bytes */
  718. wImgRowBytes = cImgChannels * cxImgSize;
  719. wDIRowBytes = (WORD) ((cDIChannels * cxWinSize + 3L) >> 2) << 2;
  720. /* copy image to screen */
  721. for (yImg = 0, yWin = cyImgPos; yImg < cyImgSize; yImg++, yWin++)
  722. {
  723. if (yWin >= cyWinSize - MARGIN)
  724. break;
  725. src = pbImage + yImg * wImgRowBytes;
  726. dst = pDiData + yWin * wDIRowBytes + cxImgPos * cDIChannels;
  727. for (xImg = 0, xWin = cxImgPos; xImg < cxImgSize; xImg++, xWin++)
  728. {
  729. if (xWin >= cxWinSize - MARGIN)
  730. break;
  731. r = *src++;
  732. g = *src++;
  733. b = *src++;
  734. *dst++ = b; /* note the reverse order */
  735. *dst++ = g;
  736. *dst++ = r;
  737. if (cImgChannels == 4)
  738. {
  739. a = *src++;
  740. }
  741. }
  742. }
  743. }
  744. return TRUE;
  745. }
  746. /*-----------------
  747. * end of source
  748. *-----------------
  749. */