rpng-win.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*---------------------------------------------------------------------------
  2. rpng - simple PNG display program rpng-win.c
  3. This program decodes and displays PNG images, with gamma correction and
  4. optionally with a user-specified background color (in case the image has
  5. transparency). It is very nearly the most basic PNG viewer possible.
  6. This version is for 32-bit Windows; it may compile under 16-bit Windows
  7. with a little tweaking (or maybe not).
  8. to do:
  9. - handle quoted command-line args (especially filenames with spaces)
  10. - have minimum window width: oh well
  11. - use %.1023s to simplify truncation of title-bar string?
  12. ---------------------------------------------------------------------------
  13. Changelog:
  14. - 1.00: initial public release
  15. - 1.01: modified to allow abbreviated options; fixed long/ulong mis-
  16. match; switched to png_jmpbuf() macro
  17. - 1.02: added extra set of parentheses to png_jmpbuf() macro; fixed
  18. command-line parsing bug
  19. - 1.10: enabled "message window"/console (thanks to David Geldreich)
  20. - 2.00: dual-licensed (added GNU GPL)
  21. - 2.01: fixed improper display of usage screen on PNG error(s)
  22. ---------------------------------------------------------------------------
  23. Copyright (c) 1998-2008 Greg Roelofs. All rights reserved.
  24. This software is provided "as is," without warranty of any kind,
  25. express or implied. In no event shall the author or contributors
  26. be held liable for any damages arising in any way from the use of
  27. this software.
  28. The contents of this file are DUAL-LICENSED. You may modify and/or
  29. redistribute this software according to the terms of one of the
  30. following two licenses (at your option):
  31. LICENSE 1 ("BSD-like with advertising clause"):
  32. Permission is granted to anyone to use this software for any purpose,
  33. including commercial applications, and to alter it and redistribute
  34. it freely, subject to the following restrictions:
  35. 1. Redistributions of source code must retain the above copyright
  36. notice, disclaimer, and this list of conditions.
  37. 2. Redistributions in binary form must reproduce the above copyright
  38. notice, disclaimer, and this list of conditions in the documenta-
  39. tion and/or other materials provided with the distribution.
  40. 3. All advertising materials mentioning features or use of this
  41. software must display the following acknowledgment:
  42. This product includes software developed by Greg Roelofs
  43. and contributors for the book, "PNG: The Definitive Guide,"
  44. published by O'Reilly and Associates.
  45. LICENSE 2 (GNU GPL v2 or later):
  46. This program is free software; you can redistribute it and/or modify
  47. it under the terms of the GNU General Public License as published by
  48. the Free Software Foundation; either version 2 of the License, or
  49. (at your option) any later version.
  50. This program is distributed in the hope that it will be useful,
  51. but WITHOUT ANY WARRANTY; without even the implied warranty of
  52. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  53. GNU General Public License for more details.
  54. You should have received a copy of the GNU General Public License
  55. along with this program; if not, write to the Free Software Foundation,
  56. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  57. ---------------------------------------------------------------------------*/
  58. #define PROGNAME "rpng-win"
  59. #define LONGNAME "Simple PNG Viewer for Windows"
  60. #define VERSION "2.01 of 16 March 2008"
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #include <time.h>
  65. #include <windows.h>
  66. #ifdef __CYGWIN__
  67. /* getch replacement. Turns out, we don't really need this,
  68. * but leave it here if we ever enable any of the uses of
  69. * _getch in the main code
  70. */
  71. #include <unistd.h>
  72. #include <termio.h>
  73. #include <sys/ioctl.h>
  74. int repl_getch( void )
  75. {
  76. char ch;
  77. int fd = fileno(stdin);
  78. struct termio old_tty, new_tty;
  79. ioctl(fd, TCGETA, &old_tty);
  80. new_tty = old_tty;
  81. new_tty.c_lflag &= ~(ICANON | ECHO | ISIG);
  82. ioctl(fd, TCSETA, &new_tty);
  83. fread(&ch, 1, sizeof(ch), stdin);
  84. ioctl(fd, TCSETA, &old_tty);
  85. return ch;
  86. }
  87. #define _getch repl_getch
  88. #else
  89. #include <conio.h> /* only for _getch() */
  90. #endif
  91. /* #define DEBUG : this enables the Trace() macros */
  92. #include "readpng.h" /* typedefs, common macros, readpng prototypes */
  93. /* could just include png.h, but this macro is the only thing we need
  94. * (name and typedefs changed to local versions); note that side effects
  95. * only happen with alpha (which could easily be avoided with
  96. * "ush acopy = (alpha);") */
  97. #define alpha_composite(composite, fg, alpha, bg) { \
  98. ush temp = ((ush)(fg)*(ush)(alpha) + \
  99. (ush)(bg)*(ush)(255 - (ush)(alpha)) + (ush)128); \
  100. (composite) = (uch)((temp + (temp >> 8)) >> 8); \
  101. }
  102. /* local prototypes */
  103. static int rpng_win_create_window(HINSTANCE hInst, int showmode);
  104. static int rpng_win_display_image(void);
  105. static void rpng_win_cleanup(void);
  106. LRESULT CALLBACK rpng_win_wndproc(HWND, UINT, WPARAM, LPARAM);
  107. static char titlebar[1024];
  108. static char *progname = PROGNAME;
  109. static char *appname = LONGNAME;
  110. static char *filename;
  111. static FILE *infile;
  112. static char *bgstr;
  113. static uch bg_red=0, bg_green=0, bg_blue=0;
  114. static double display_exponent;
  115. static ulg image_width, image_height, image_rowbytes;
  116. static int image_channels;
  117. static uch *image_data;
  118. /* Windows-specific variables */
  119. static ulg wimage_rowbytes;
  120. static uch *dib;
  121. static uch *wimage_data;
  122. static BITMAPINFOHEADER *bmih;
  123. static HWND global_hwnd;
  124. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, PSTR cmd, int showmode)
  125. {
  126. char *args[1024]; /* arbitrary limit, but should suffice */
  127. char *p, *q, **argv = args;
  128. int argc = 0;
  129. int rc, alen, flen;
  130. int error = 0;
  131. int have_bg = FALSE;
  132. double LUT_exponent; /* just the lookup table */
  133. double CRT_exponent = 2.2; /* just the monitor */
  134. double default_display_exponent; /* whole display system */
  135. MSG msg;
  136. filename = (char *)NULL;
  137. #ifndef __CYGWIN__
  138. /* First reenable console output, which normally goes to the bit bucket
  139. * for windowed apps. Closing the console window will terminate the
  140. * app. Thanks to David.Geldreich@realviz.com for supplying the magical
  141. * incantation. */
  142. AllocConsole();
  143. freopen("CONOUT$", "a", stderr);
  144. freopen("CONOUT$", "a", stdout);
  145. #endif
  146. /* Next set the default value for our display-system exponent, i.e.,
  147. * the product of the CRT exponent and the exponent corresponding to
  148. * the frame-buffer's lookup table (LUT), if any. This is not an
  149. * exhaustive list of LUT values (e.g., OpenStep has a lot of weird
  150. * ones), but it should cover 99% of the current possibilities. And
  151. * yes, these ifdefs are completely wasted in a Windows program... */
  152. #if defined(NeXT)
  153. LUT_exponent = 1.0 / 2.2;
  154. /*
  155. if (some_next_function_that_returns_gamma(&next_gamma))
  156. LUT_exponent = 1.0 / next_gamma;
  157. */
  158. #elif defined(sgi)
  159. LUT_exponent = 1.0 / 1.7;
  160. /* there doesn't seem to be any documented function to get the
  161. * "gamma" value, so we do it the hard way */
  162. infile = fopen("/etc/config/system.glGammaVal", "r");
  163. if (infile) {
  164. double sgi_gamma;
  165. fgets(tmpline, 80, infile);
  166. fclose(infile);
  167. sgi_gamma = atof(tmpline);
  168. if (sgi_gamma > 0.0)
  169. LUT_exponent = 1.0 / sgi_gamma;
  170. }
  171. #elif defined(Macintosh)
  172. LUT_exponent = 1.8 / 2.61;
  173. /*
  174. if (some_mac_function_that_returns_gamma(&mac_gamma))
  175. LUT_exponent = mac_gamma / 2.61;
  176. */
  177. #else
  178. LUT_exponent = 1.0; /* assume no LUT: most PCs */
  179. #endif
  180. /* the defaults above give 1.0, 1.3, 1.5 and 2.2, respectively: */
  181. default_display_exponent = LUT_exponent * CRT_exponent;
  182. /* If the user has set the SCREEN_GAMMA environment variable as suggested
  183. * (somewhat imprecisely) in the libpng documentation, use that; otherwise
  184. * use the default value we just calculated. Either way, the user may
  185. * override this via a command-line option. */
  186. if ((p = getenv("SCREEN_GAMMA")) != NULL)
  187. display_exponent = atof(p);
  188. else
  189. display_exponent = default_display_exponent;
  190. /* Windows really hates command lines, so we have to set up our own argv.
  191. * Note that we do NOT bother with quoted arguments here, so don't use
  192. * filenames with spaces in 'em! */
  193. argv[argc++] = PROGNAME;
  194. p = cmd;
  195. for (;;) {
  196. if (*p == ' ')
  197. while (*++p == ' ')
  198. ;
  199. /* now p points at the first non-space after some spaces */
  200. if (*p == '\0')
  201. break; /* nothing after the spaces: done */
  202. argv[argc++] = q = p;
  203. while (*q && *q != ' ')
  204. ++q;
  205. /* now q points at a space or the end of the string */
  206. if (*q == '\0')
  207. break; /* last argv already terminated; quit */
  208. *q = '\0'; /* change space to terminator */
  209. p = q + 1;
  210. }
  211. argv[argc] = NULL; /* terminate the argv array itself */
  212. /* Now parse the command line for options and the PNG filename. */
  213. while (*++argv && !error) {
  214. if (!strncmp(*argv, "-gamma", 2)) {
  215. if (!*++argv)
  216. ++error;
  217. else {
  218. display_exponent = atof(*argv);
  219. if (display_exponent <= 0.0)
  220. ++error;
  221. }
  222. } else if (!strncmp(*argv, "-bgcolor", 2)) {
  223. if (!*++argv)
  224. ++error;
  225. else {
  226. bgstr = *argv;
  227. if (strlen(bgstr) != 7 || bgstr[0] != '#')
  228. ++error;
  229. else
  230. have_bg = TRUE;
  231. }
  232. } else {
  233. if (**argv != '-') {
  234. filename = *argv;
  235. if (argv[1]) /* shouldn't be any more args after filename */
  236. ++error;
  237. } else
  238. ++error; /* not expecting any other options */
  239. }
  240. }
  241. if (!filename)
  242. ++error;
  243. /* print usage screen if any errors up to this point */
  244. if (error) {
  245. #ifndef __CYGWIN__
  246. int ch;
  247. #endif
  248. fprintf(stderr, "\n%s %s: %s\n\n", PROGNAME, VERSION, appname);
  249. readpng_version_info();
  250. fprintf(stderr, "\n"
  251. "Usage: %s [-gamma exp] [-bgcolor bg] file.png\n"
  252. " exp \ttransfer-function exponent (``gamma'') of the display\n"
  253. "\t\t system in floating-point format (e.g., ``%.1f''); equal\n"
  254. "\t\t to the product of the lookup-table exponent (varies)\n"
  255. "\t\t and the CRT exponent (usually 2.2); must be positive\n"
  256. " bg \tdesired background color in 7-character hex RGB format\n"
  257. "\t\t (e.g., ``#ff7700'' for orange: same as HTML colors);\n"
  258. "\t\t used with transparent images\n"
  259. "\nPress Q, Esc or mouse button 1 after image is displayed to quit.\n"
  260. #ifndef __CYGWIN__
  261. "Press Q or Esc to quit this usage screen.\n"
  262. #endif
  263. "\n", PROGNAME, default_display_exponent);
  264. #ifndef __CYGWIN__
  265. do
  266. ch = _getch();
  267. while (ch != 'q' && ch != 'Q' && ch != 0x1B);
  268. #endif
  269. exit(1);
  270. }
  271. if (!(infile = fopen(filename, "rb"))) {
  272. fprintf(stderr, PROGNAME ": can't open PNG file [%s]\n", filename);
  273. ++error;
  274. } else {
  275. if ((rc = readpng_init(infile, &image_width, &image_height)) != 0) {
  276. switch (rc) {
  277. case 1:
  278. fprintf(stderr, PROGNAME
  279. ": [%s] is not a PNG file: incorrect signature\n",
  280. filename);
  281. break;
  282. case 2:
  283. fprintf(stderr, PROGNAME
  284. ": [%s] has bad IHDR (libpng longjmp)\n", filename);
  285. break;
  286. case 4:
  287. fprintf(stderr, PROGNAME ": insufficient memory\n");
  288. break;
  289. default:
  290. fprintf(stderr, PROGNAME
  291. ": unknown readpng_init() error\n");
  292. break;
  293. }
  294. ++error;
  295. }
  296. if (error)
  297. fclose(infile);
  298. }
  299. if (error) {
  300. #ifndef __CYGWIN__
  301. int ch;
  302. #endif
  303. fprintf(stderr, PROGNAME ": aborting.\n");
  304. #ifndef __CYGWIN__
  305. do
  306. ch = _getch();
  307. while (ch != 'q' && ch != 'Q' && ch != 0x1B);
  308. #endif
  309. exit(2);
  310. } else {
  311. fprintf(stderr, "\n%s %s: %s\n", PROGNAME, VERSION, appname);
  312. #ifndef __CYGWIN__
  313. fprintf(stderr,
  314. "\n [console window: closing this window will terminate %s]\n\n",
  315. PROGNAME);
  316. #endif
  317. }
  318. /* set the title-bar string, but make sure buffer doesn't overflow */
  319. alen = strlen(appname);
  320. flen = strlen(filename);
  321. if (alen + flen + 3 > 1023)
  322. sprintf(titlebar, "%s: ...%s", appname, filename+(alen+flen+6-1023));
  323. else
  324. sprintf(titlebar, "%s: %s", appname, filename);
  325. /* if the user didn't specify a background color on the command line,
  326. * check for one in the PNG file--if not, the initialized values of 0
  327. * (black) will be used */
  328. if (have_bg) {
  329. unsigned r, g, b; /* this approach quiets compiler warnings */
  330. sscanf(bgstr+1, "%2x%2x%2x", &r, &g, &b);
  331. bg_red = (uch)r;
  332. bg_green = (uch)g;
  333. bg_blue = (uch)b;
  334. } else if (readpng_get_bgcolor(&bg_red, &bg_green, &bg_blue) > 1) {
  335. readpng_cleanup(TRUE);
  336. fprintf(stderr, PROGNAME
  337. ": libpng error while checking for background color\n");
  338. exit(2);
  339. }
  340. /* do the basic Windows initialization stuff, make the window and fill it
  341. * with the background color */
  342. if (rpng_win_create_window(hInst, showmode))
  343. exit(2);
  344. /* decode the image, all at once */
  345. Trace((stderr, "calling readpng_get_image()\n"))
  346. image_data = readpng_get_image(display_exponent, &image_channels,
  347. &image_rowbytes);
  348. Trace((stderr, "done with readpng_get_image()\n"))
  349. /* done with PNG file, so clean up to minimize memory usage (but do NOT
  350. * nuke image_data!) */
  351. readpng_cleanup(FALSE);
  352. fclose(infile);
  353. if (!image_data) {
  354. fprintf(stderr, PROGNAME ": unable to decode PNG image\n");
  355. exit(3);
  356. }
  357. /* display image (composite with background if requested) */
  358. Trace((stderr, "calling rpng_win_display_image()\n"))
  359. if (rpng_win_display_image()) {
  360. free(image_data);
  361. exit(4);
  362. }
  363. Trace((stderr, "done with rpng_win_display_image()\n"))
  364. /* wait for the user to tell us when to quit */
  365. printf(
  366. #ifndef __CYGWIN__
  367. "Done. Press Q, Esc or mouse button 1 (within image window) to quit.\n"
  368. #else
  369. "Done. Press mouse button 1 (within image window) to quit.\n"
  370. #endif
  371. );
  372. fflush(stdout);
  373. while (GetMessage(&msg, NULL, 0, 0)) {
  374. TranslateMessage(&msg);
  375. DispatchMessage(&msg);
  376. }
  377. /* OK, we're done: clean up all image and Windows resources and go away */
  378. rpng_win_cleanup();
  379. return msg.wParam;
  380. }
  381. static int rpng_win_create_window(HINSTANCE hInst, int showmode)
  382. {
  383. uch *dest;
  384. int extra_width, extra_height;
  385. ulg i, j;
  386. WNDCLASSEX wndclass;
  387. /*---------------------------------------------------------------------------
  388. Allocate memory for the display-specific version of the image (round up
  389. to multiple of 4 for Windows DIB).
  390. ---------------------------------------------------------------------------*/
  391. wimage_rowbytes = ((3*image_width + 3L) >> 2) << 2;
  392. if (!(dib = (uch *)malloc(sizeof(BITMAPINFOHEADER) +
  393. wimage_rowbytes*image_height)))
  394. {
  395. return 4; /* fail */
  396. }
  397. /*---------------------------------------------------------------------------
  398. Initialize the DIB. Negative height means to use top-down BMP ordering
  399. (must be uncompressed, but that's what we want). Bit count of 1, 4 or 8
  400. implies a colormap of RGBX quads, but 24-bit BMPs just use B,G,R values
  401. directly => wimage_data begins immediately after BMP header.
  402. ---------------------------------------------------------------------------*/
  403. memset(dib, 0, sizeof(BITMAPINFOHEADER));
  404. bmih = (BITMAPINFOHEADER *)dib;
  405. bmih->biSize = sizeof(BITMAPINFOHEADER);
  406. bmih->biWidth = image_width;
  407. bmih->biHeight = -((long)image_height);
  408. bmih->biPlanes = 1;
  409. bmih->biBitCount = 24;
  410. bmih->biCompression = 0;
  411. wimage_data = dib + sizeof(BITMAPINFOHEADER);
  412. /*---------------------------------------------------------------------------
  413. Fill in background color (black by default); data are in BGR order.
  414. ---------------------------------------------------------------------------*/
  415. for (j = 0; j < image_height; ++j) {
  416. dest = wimage_data + j*wimage_rowbytes;
  417. for (i = image_width; i > 0; --i) {
  418. *dest++ = bg_blue;
  419. *dest++ = bg_green;
  420. *dest++ = bg_red;
  421. }
  422. }
  423. /*---------------------------------------------------------------------------
  424. Set the window parameters.
  425. ---------------------------------------------------------------------------*/
  426. memset(&wndclass, 0, sizeof(wndclass));
  427. wndclass.cbSize = sizeof(wndclass);
  428. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  429. wndclass.lpfnWndProc = rpng_win_wndproc;
  430. wndclass.hInstance = hInst;
  431. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  432. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  433. wndclass.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
  434. wndclass.lpszMenuName = NULL;
  435. wndclass.lpszClassName = progname;
  436. wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  437. RegisterClassEx(&wndclass);
  438. /*---------------------------------------------------------------------------
  439. Finally, create the window.
  440. ---------------------------------------------------------------------------*/
  441. extra_width = 2*(GetSystemMetrics(SM_CXBORDER) +
  442. GetSystemMetrics(SM_CXDLGFRAME));
  443. extra_height = 2*(GetSystemMetrics(SM_CYBORDER) +
  444. GetSystemMetrics(SM_CYDLGFRAME)) +
  445. GetSystemMetrics(SM_CYCAPTION);
  446. global_hwnd = CreateWindow(progname, titlebar, WS_OVERLAPPEDWINDOW,
  447. CW_USEDEFAULT, CW_USEDEFAULT, image_width+extra_width,
  448. image_height+extra_height, NULL, NULL, hInst, NULL);
  449. ShowWindow(global_hwnd, showmode);
  450. UpdateWindow(global_hwnd);
  451. return 0;
  452. } /* end function rpng_win_create_window() */
  453. static int rpng_win_display_image()
  454. {
  455. uch *src, *dest;
  456. uch r, g, b, a;
  457. ulg i, row, lastrow;
  458. RECT rect;
  459. Trace((stderr, "beginning display loop (image_channels == %d)\n",
  460. image_channels))
  461. Trace((stderr, "(width = %ld, rowbytes = %ld, wimage_rowbytes = %d)\n",
  462. image_width, image_rowbytes, wimage_rowbytes))
  463. /*---------------------------------------------------------------------------
  464. Blast image data to buffer. This whole routine takes place before the
  465. message loop begins, so there's no real point in any pseudo-progressive
  466. display...
  467. ---------------------------------------------------------------------------*/
  468. for (lastrow = row = 0; row < image_height; ++row) {
  469. src = image_data + row*image_rowbytes;
  470. dest = wimage_data + row*wimage_rowbytes;
  471. if (image_channels == 3) {
  472. for (i = image_width; i > 0; --i) {
  473. r = *src++;
  474. g = *src++;
  475. b = *src++;
  476. *dest++ = b;
  477. *dest++ = g; /* note reverse order */
  478. *dest++ = r;
  479. }
  480. } else /* if (image_channels == 4) */ {
  481. for (i = image_width; i > 0; --i) {
  482. r = *src++;
  483. g = *src++;
  484. b = *src++;
  485. a = *src++;
  486. if (a == 255) {
  487. *dest++ = b;
  488. *dest++ = g;
  489. *dest++ = r;
  490. } else if (a == 0) {
  491. *dest++ = bg_blue;
  492. *dest++ = bg_green;
  493. *dest++ = bg_red;
  494. } else {
  495. /* this macro (copied from png.h) composites the
  496. * foreground and background values and puts the
  497. * result into the first argument; there are no
  498. * side effects with the first argument */
  499. alpha_composite(*dest++, b, a, bg_blue);
  500. alpha_composite(*dest++, g, a, bg_green);
  501. alpha_composite(*dest++, r, a, bg_red);
  502. }
  503. }
  504. }
  505. /* display after every 16 lines */
  506. if (((row+1) & 0xf) == 0) {
  507. rect.left = 0L;
  508. rect.top = (LONG)lastrow;
  509. rect.right = (LONG)image_width; /* possibly off by one? */
  510. rect.bottom = (LONG)lastrow + 16L; /* possibly off by one? */
  511. InvalidateRect(global_hwnd, &rect, FALSE);
  512. UpdateWindow(global_hwnd); /* similar to XFlush() */
  513. lastrow = row + 1;
  514. }
  515. }
  516. Trace((stderr, "calling final image-flush routine\n"))
  517. if (lastrow < image_height) {
  518. rect.left = 0L;
  519. rect.top = (LONG)lastrow;
  520. rect.right = (LONG)image_width; /* possibly off by one? */
  521. rect.bottom = (LONG)image_height; /* possibly off by one? */
  522. InvalidateRect(global_hwnd, &rect, FALSE);
  523. UpdateWindow(global_hwnd); /* similar to XFlush() */
  524. }
  525. /*
  526. last param determines whether or not background is wiped before paint
  527. InvalidateRect(global_hwnd, NULL, TRUE);
  528. UpdateWindow(global_hwnd);
  529. */
  530. return 0;
  531. }
  532. static void rpng_win_cleanup()
  533. {
  534. if (image_data) {
  535. free(image_data);
  536. image_data = NULL;
  537. }
  538. if (dib) {
  539. free(dib);
  540. dib = NULL;
  541. }
  542. }
  543. LRESULT CALLBACK rpng_win_wndproc(HWND hwnd, UINT iMsg, WPARAM wP, LPARAM lP)
  544. {
  545. HDC hdc;
  546. PAINTSTRUCT ps;
  547. int rc;
  548. switch (iMsg) {
  549. case WM_CREATE:
  550. /* one-time processing here, if any */
  551. return 0;
  552. case WM_PAINT:
  553. hdc = BeginPaint(hwnd, &ps);
  554. /* dest */
  555. rc = StretchDIBits(hdc, 0, 0, image_width, image_height,
  556. /* source */
  557. 0, 0, image_width, image_height,
  558. wimage_data, (BITMAPINFO *)bmih,
  559. /* iUsage: no clue */
  560. 0, SRCCOPY);
  561. EndPaint(hwnd, &ps);
  562. return 0;
  563. /* wait for the user to tell us when to quit */
  564. case WM_CHAR:
  565. switch (wP) { /* only need one, so ignore repeat count */
  566. case 'q':
  567. case 'Q':
  568. case 0x1B: /* Esc key */
  569. PostQuitMessage(0);
  570. }
  571. return 0;
  572. case WM_LBUTTONDOWN: /* another way of quitting */
  573. case WM_DESTROY:
  574. PostQuitMessage(0);
  575. return 0;
  576. }
  577. return DefWindowProc(hwnd, iMsg, wP, lP);
  578. }