Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1253 lines
44 KiB

  1. /*---------------------------------------------------------------------------
  2. rpng2 - progressive-model PNG display program rpng2-win.c
  3. This program decodes and displays PNG files progressively, as if it were
  4. a web browser (though the front end is only set up to read from files).
  5. It supports gamma correction, user-specified background colors, and user-
  6. specified background patterns (for transparent images). This version is
  7. for 32-bit Windows; it may compile under 16-bit Windows with a little
  8. tweaking (or maybe not). Thanks to Adam Costello and Pieter S. van der
  9. Meulen for the "diamond" and "radial waves" patterns, respectively.
  10. to do (someday, maybe):
  11. - handle quoted command-line args (especially filenames with spaces)
  12. - finish resizable checkerboard-gradient (sizes 4-128?)
  13. - use %.1023s to simplify truncation of title-bar string?
  14. - have minimum window width: oh well
  15. ---------------------------------------------------------------------------
  16. Changelog:
  17. - 1.01: initial public release
  18. - 1.02: fixed cut-and-paste error in usage screen (oops...)
  19. - 1.03: modified to allow abbreviated options
  20. - 1.04: removed bogus extra argument from usage fprintf() [Glenn R-P?];
  21. fixed command-line parsing bug
  22. - 1.10: enabled "message window"/console (thanks to David Geldreich)
  23. - 1.20: added runtime MMX-enabling/disabling and new -mmx* options
  24. - 1.21: made minor tweak to usage screen to fit within 25-line console
  25. - 1.22: added AMD64/EM64T support (__x86_64__)
  26. - 2.00: dual-licensed (added GNU GPL)
  27. - 2.01: fixed 64-bit typo in readpng2.c
  28. - 2.02: fixed improper display of usage screen on PNG error(s); fixed
  29. unexpected-EOF and file-read-error cases
  30. - 2.03: removed runtime MMX-enabling/disabling and obsolete -mmx* options
  31. ---------------------------------------------------------------------------
  32. Copyright (c) 1998-2008 Greg Roelofs. All rights reserved.
  33. This software is provided "as is," without warranty of any kind,
  34. express or implied. In no event shall the author or contributors
  35. be held liable for any damages arising in any way from the use of
  36. this software.
  37. The contents of this file are DUAL-LICENSED. You may modify and/or
  38. redistribute this software according to the terms of one of the
  39. following two licenses (at your option):
  40. LICENSE 1 ("BSD-like with advertising clause"):
  41. Permission is granted to anyone to use this software for any purpose,
  42. including commercial applications, and to alter it and redistribute
  43. it freely, subject to the following restrictions:
  44. 1. Redistributions of source code must retain the above copyright
  45. notice, disclaimer, and this list of conditions.
  46. 2. Redistributions in binary form must reproduce the above copyright
  47. notice, disclaimer, and this list of conditions in the documenta-
  48. tion and/or other materials provided with the distribution.
  49. 3. All advertising materials mentioning features or use of this
  50. software must display the following acknowledgment:
  51. This product includes software developed by Greg Roelofs
  52. and contributors for the book, "PNG: The Definitive Guide,"
  53. published by O'Reilly and Associates.
  54. LICENSE 2 (GNU GPL v2 or later):
  55. This program is free software; you can redistribute it and/or modify
  56. it under the terms of the GNU General Public License as published by
  57. the Free Software Foundation; either version 2 of the License, or
  58. (at your option) any later version.
  59. This program is distributed in the hope that it will be useful,
  60. but WITHOUT ANY WARRANTY; without even the implied warranty of
  61. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  62. GNU General Public License for more details.
  63. You should have received a copy of the GNU General Public License
  64. along with this program; if not, write to the Free Software Foundation,
  65. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  66. ---------------------------------------------------------------------------*/
  67. #define PROGNAME "rpng2-win"
  68. #define LONGNAME "Progressive PNG Viewer for Windows"
  69. #define VERSION "2.02 of 16 March 2008"
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <string.h>
  73. #include <setjmp.h> /* for jmpbuf declaration in readpng2.h */
  74. #include <time.h>
  75. #include <math.h> /* only for PvdM background code */
  76. #include <windows.h>
  77. #ifdef __CYGWIN__
  78. /* getch replacement. Turns out, we don't really need this,
  79. * but leave it here if we ever enable any of the uses of
  80. * _getch in the main code
  81. */
  82. #include <unistd.h>
  83. #include <termio.h>
  84. #include <sys/ioctl.h>
  85. int repl_getch( void )
  86. {
  87. char ch;
  88. int fd = fileno(stdin);
  89. struct termio old_tty, new_tty;
  90. ioctl(fd, TCGETA, &old_tty);
  91. new_tty = old_tty;
  92. new_tty.c_lflag &= ~(ICANON | ECHO | ISIG);
  93. ioctl(fd, TCSETA, &new_tty);
  94. fread(&ch, 1, sizeof(ch), stdin);
  95. ioctl(fd, TCSETA, &old_tty);
  96. return ch;
  97. }
  98. #define _getch repl_getch
  99. #else
  100. #include <conio.h> /* only for _getch() */
  101. #endif
  102. /* all for PvdM background code: */
  103. #ifndef PI
  104. # define PI 3.141592653589793238
  105. #endif
  106. #define PI_2 (PI*0.5)
  107. #define INV_PI_360 (360.0 / PI)
  108. #define MAX(a,b) (a>b?a:b)
  109. #define MIN(a,b) (a<b?a:b)
  110. #define CLIP(a,min,max) MAX(min,MIN((a),max))
  111. #define ABS(a) ((a)<0?-(a):(a))
  112. #define CLIP8P(c) MAX(0,(MIN((c),255))) /* 8-bit pos. integer (uch) */
  113. #define ROUNDF(f) ((int)(f + 0.5))
  114. #define rgb1_max bg_freq
  115. #define rgb1_min bg_gray
  116. #define rgb2_max bg_bsat
  117. #define rgb2_min bg_brot
  118. /* #define DEBUG */ /* this enables the Trace() macros */
  119. #include "readpng2.h" /* typedefs, common macros, readpng2 prototypes */
  120. /* could just include png.h, but this macro is the only thing we need
  121. * (name and typedefs changed to local versions); note that side effects
  122. * only happen with alpha (which could easily be avoided with
  123. * "ush acopy = (alpha);") */
  124. #define alpha_composite(composite, fg, alpha, bg) { \
  125. ush temp = ((ush)(fg)*(ush)(alpha) + \
  126. (ush)(bg)*(ush)(255 - (ush)(alpha)) + (ush)128); \
  127. (composite) = (uch)((temp + (temp >> 8)) >> 8); \
  128. }
  129. #define INBUFSIZE 4096 /* with pseudo-timing on (1 sec delay/block), this
  130. * block size corresponds roughly to a download
  131. * speed 10% faster than theoretical 33.6K maximum
  132. * (assuming 8 data bits, 1 stop bit and no other
  133. * overhead) */
  134. /* local prototypes */
  135. static void rpng2_win_init(void);
  136. static int rpng2_win_create_window(void);
  137. static int rpng2_win_load_bg_image(void);
  138. static void rpng2_win_display_row(ulg row);
  139. static void rpng2_win_finish_display(void);
  140. static void rpng2_win_cleanup(void);
  141. LRESULT CALLBACK rpng2_win_wndproc(HWND, UINT, WPARAM, LPARAM);
  142. static char titlebar[1024];
  143. static char *progname = PROGNAME;
  144. static char *appname = LONGNAME;
  145. static char *filename;
  146. static FILE *infile;
  147. static mainprog_info rpng2_info;
  148. static uch inbuf[INBUFSIZE];
  149. static int incount;
  150. static int pat = 6; /* must be less than num_bgpat */
  151. static int bg_image = 0;
  152. static int bgscale = 16;
  153. static ulg bg_rowbytes;
  154. static uch *bg_data;
  155. static struct rgb_color {
  156. uch r, g, b;
  157. } rgb[] = {
  158. { 0, 0, 0}, /* 0: black */
  159. {255, 255, 255}, /* 1: white */
  160. {173, 132, 57}, /* 2: tan */
  161. { 64, 132, 0}, /* 3: medium green */
  162. {189, 117, 1}, /* 4: gold */
  163. {253, 249, 1}, /* 5: yellow */
  164. { 0, 0, 255}, /* 6: blue */
  165. { 0, 0, 120}, /* 7: medium blue */
  166. {255, 0, 255}, /* 8: magenta */
  167. { 64, 0, 64}, /* 9: dark magenta */
  168. {255, 0, 0}, /* 10: red */
  169. { 64, 0, 0}, /* 11: dark red */
  170. {255, 127, 0}, /* 12: orange */
  171. {192, 96, 0}, /* 13: darker orange */
  172. { 24, 60, 0}, /* 14: dark green-yellow */
  173. { 85, 125, 200} /* 15: ice blue */
  174. };
  175. /* not used for now, but should be for error-checking:
  176. static int num_rgb = sizeof(rgb) / sizeof(struct rgb_color);
  177. */
  178. /*
  179. This whole struct is a fairly cheesy way to keep the number of
  180. command-line options to a minimum. The radial-waves background
  181. type is a particularly poor fit to the integer elements of the
  182. struct...but a few macros and a little fixed-point math will do
  183. wonders for ya.
  184. type bits:
  185. F E D C B A 9 8 7 6 5 4 3 2 1 0
  186. | | | | |
  187. | | +-+-+-- 0 = sharp-edged checkerboard
  188. | | 1 = soft diamonds
  189. | | 2 = radial waves
  190. | | 3-7 = undefined
  191. | +-- gradient #2 inverted?
  192. +-- alternating columns inverted?
  193. */
  194. static struct background_pattern {
  195. ush type;
  196. int rgb1_max, rgb1_min; /* or bg_freq, bg_gray */
  197. int rgb2_max, rgb2_min; /* or bg_bsat, bg_brot (both scaled by 10)*/
  198. } bg[] = {
  199. {0+8, 2,0, 1,15}, /* checkered: tan/black vs. white/ice blue */
  200. {0+24, 2,0, 1,0}, /* checkered: tan/black vs. white/black */
  201. {0+8, 4,5, 0,2}, /* checkered: gold/yellow vs. black/tan */
  202. {0+8, 4,5, 0,6}, /* checkered: gold/yellow vs. black/blue */
  203. {0, 7,0, 8,9}, /* checkered: deep blue/black vs. magenta */
  204. {0+8, 13,0, 5,14}, /* checkered: orange/black vs. yellow */
  205. {0+8, 12,0, 10,11}, /* checkered: orange/black vs. red */
  206. {1, 7,0, 8,0}, /* diamonds: deep blue/black vs. magenta */
  207. {1, 12,0, 11,0}, /* diamonds: orange vs. dark red */
  208. {1, 10,0, 7,0}, /* diamonds: red vs. medium blue */
  209. {1, 4,0, 5,0}, /* diamonds: gold vs. yellow */
  210. {1, 3,0, 0,0}, /* diamonds: medium green vs. black */
  211. {2, 16, 100, 20, 0}, /* radial: ~hard radial color-beams */
  212. {2, 18, 100, 10, 2}, /* radial: soft, curved radial color-beams */
  213. {2, 16, 256, 100, 250}, /* radial: very tight spiral */
  214. {2, 10000, 256, 11, 0} /* radial: dipole-moire' (almost fractal) */
  215. };
  216. static int num_bgpat = sizeof(bg) / sizeof(struct background_pattern);
  217. /* Windows-specific global variables (could go in struct, but messy...) */
  218. static ulg wimage_rowbytes;
  219. static uch *dib;
  220. static uch *wimage_data;
  221. static BITMAPINFOHEADER *bmih;
  222. static HWND global_hwnd;
  223. static HINSTANCE global_hInst;
  224. static int global_showmode;
  225. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, PSTR cmd, int showmode)
  226. {
  227. char *args[1024]; /* arbitrary limit, but should suffice */
  228. char **argv = args;
  229. char *p, *q, *bgstr = NULL;
  230. int argc = 0;
  231. int rc, alen, flen;
  232. int error = 0;
  233. int timing = FALSE;
  234. int have_bg = FALSE;
  235. double LUT_exponent; /* just the lookup table */
  236. double CRT_exponent = 2.2; /* just the monitor */
  237. double default_display_exponent; /* whole display system */
  238. MSG msg;
  239. /* First initialize a few things, just to be sure--memset takes care of
  240. * default background color (black), booleans (FALSE), pointers (NULL),
  241. * etc. */
  242. global_hInst = hInst;
  243. global_showmode = showmode;
  244. filename = (char *)NULL;
  245. memset(&rpng2_info, 0, sizeof(mainprog_info));
  246. #ifndef __CYGWIN__
  247. /* Next reenable console output, which normally goes to the bit bucket
  248. * for windowed apps. Closing the console window will terminate the
  249. * app. Thanks to David.Geldreich@realviz.com for supplying the magical
  250. * incantation. */
  251. AllocConsole();
  252. freopen("CONOUT$", "a", stderr);
  253. freopen("CONOUT$", "a", stdout);
  254. #endif
  255. /* Set the default value for our display-system exponent, i.e., the
  256. * product of the CRT exponent and the exponent corresponding to
  257. * the frame-buffer's lookup table (LUT), if any. This is not an
  258. * exhaustive list of LUT values (e.g., OpenStep has a lot of weird
  259. * ones), but it should cover 99% of the current possibilities. And
  260. * yes, these ifdefs are completely wasted in a Windows program... */
  261. #if defined(NeXT)
  262. /* third-party utilities can modify the default LUT exponent */
  263. LUT_exponent = 1.0 / 2.2;
  264. /*
  265. if (some_next_function_that_returns_gamma(&next_gamma))
  266. LUT_exponent = 1.0 / next_gamma;
  267. */
  268. #elif defined(sgi)
  269. LUT_exponent = 1.0 / 1.7;
  270. /* there doesn't seem to be any documented function to
  271. * get the "gamma" value, so we do it the hard way */
  272. infile = fopen("/etc/config/system.glGammaVal", "r");
  273. if (infile) {
  274. double sgi_gamma;
  275. fgets(tmpline, 80, infile);
  276. fclose(infile);
  277. sgi_gamma = atof(tmpline);
  278. if (sgi_gamma > 0.0)
  279. LUT_exponent = 1.0 / sgi_gamma;
  280. }
  281. #elif defined(Macintosh)
  282. LUT_exponent = 1.8 / 2.61;
  283. /*
  284. if (some_mac_function_that_returns_gamma(&mac_gamma))
  285. LUT_exponent = mac_gamma / 2.61;
  286. */
  287. #else
  288. LUT_exponent = 1.0; /* assume no LUT: most PCs */
  289. #endif
  290. /* the defaults above give 1.0, 1.3, 1.5 and 2.2, respectively: */
  291. default_display_exponent = LUT_exponent * CRT_exponent;
  292. /* If the user has set the SCREEN_GAMMA environment variable as suggested
  293. * (somewhat imprecisely) in the libpng documentation, use that; otherwise
  294. * use the default value we just calculated. Either way, the user may
  295. * override this via a command-line option. */
  296. if ((p = getenv("SCREEN_GAMMA")) != NULL)
  297. rpng2_info.display_exponent = atof(p);
  298. else
  299. rpng2_info.display_exponent = default_display_exponent;
  300. /* Windows really hates command lines, so we have to set up our own argv.
  301. * Note that we do NOT bother with quoted arguments here, so don't use
  302. * filenames with spaces in 'em! */
  303. argv[argc++] = PROGNAME;
  304. p = cmd;
  305. for (;;) {
  306. if (*p == ' ')
  307. while (*++p == ' ')
  308. ;
  309. /* now p points at the first non-space after some spaces */
  310. if (*p == '\0')
  311. break; /* nothing after the spaces: done */
  312. argv[argc++] = q = p;
  313. while (*q && *q != ' ')
  314. ++q;
  315. /* now q points at a space or the end of the string */
  316. if (*q == '\0')
  317. break; /* last argv already terminated; quit */
  318. *q = '\0'; /* change space to terminator */
  319. p = q + 1;
  320. }
  321. argv[argc] = NULL; /* terminate the argv array itself */
  322. /* Now parse the command line for options and the PNG filename. */
  323. while (*++argv && !error) {
  324. if (!strncmp(*argv, "-gamma", 2)) {
  325. if (!*++argv)
  326. ++error;
  327. else {
  328. rpng2_info.display_exponent = atof(*argv);
  329. if (rpng2_info.display_exponent <= 0.0)
  330. ++error;
  331. }
  332. } else if (!strncmp(*argv, "-bgcolor", 4)) {
  333. if (!*++argv)
  334. ++error;
  335. else {
  336. bgstr = *argv;
  337. if (strlen(bgstr) != 7 || bgstr[0] != '#')
  338. ++error;
  339. else {
  340. have_bg = TRUE;
  341. bg_image = FALSE;
  342. }
  343. }
  344. } else if (!strncmp(*argv, "-bgpat", 4)) {
  345. if (!*++argv)
  346. ++error;
  347. else {
  348. pat = atoi(*argv) - 1;
  349. if (pat < 0 || pat >= num_bgpat)
  350. ++error;
  351. else {
  352. bg_image = TRUE;
  353. have_bg = FALSE;
  354. }
  355. }
  356. } else if (!strncmp(*argv, "-timing", 2)) {
  357. timing = TRUE;
  358. } else {
  359. if (**argv != '-') {
  360. filename = *argv;
  361. if (argv[1]) /* shouldn't be any more args after filename */
  362. ++error;
  363. } else
  364. ++error; /* not expecting any other options */
  365. }
  366. }
  367. if (!filename)
  368. ++error;
  369. /* print usage screen if any errors up to this point */
  370. if (error) {
  371. #ifndef __CYGWIN__
  372. int ch;
  373. #endif
  374. fprintf(stderr, "\n%s %s: %s\n\n", PROGNAME, VERSION, appname);
  375. readpng2_version_info();
  376. fprintf(stderr, "\n"
  377. "Usage: %s [-gamma exp] [-bgcolor bg | -bgpat pat] [-timing]\n"
  378. " %*s file.png\n\n"
  379. " exp \ttransfer-function exponent (``gamma'') of the display\n"
  380. "\t\t system in floating-point format (e.g., ``%.1f''); equal\n"
  381. "\t\t to the product of the lookup-table exponent (varies)\n"
  382. "\t\t and the CRT exponent (usually 2.2); must be positive\n"
  383. " bg \tdesired background color in 7-character hex RGB format\n"
  384. "\t\t (e.g., ``#ff7700'' for orange: same as HTML colors);\n"
  385. "\t\t used with transparent images; overrides -bgpat option\n"
  386. " pat \tdesired background pattern number (1-%d); used with\n"
  387. "\t\t transparent images; overrides -bgcolor option\n"
  388. " -timing\tenables delay for every block read, to simulate modem\n"
  389. "\t\t download of image (~36 Kbps)\n"
  390. "\nPress Q, Esc or mouse button 1 after image is displayed to quit.\n"
  391. #ifndef __CYGWIN__
  392. "Press Q or Esc to quit this usage screen. ",
  393. #else
  394. ,
  395. #endif
  396. PROGNAME,
  397. #if (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__)) && \
  398. !(defined(__CYGWIN__) || defined(__MINGW32__))
  399. (int)strlen(PROGNAME), " ",
  400. #endif
  401. (int)strlen(PROGNAME), " ", default_display_exponent, num_bgpat);
  402. fflush(stderr);
  403. #ifndef __CYGWIN__
  404. do
  405. ch = _getch();
  406. while (ch != 'q' && ch != 'Q' && ch != 0x1B);
  407. #endif
  408. exit(1);
  409. }
  410. if (!(infile = fopen(filename, "rb"))) {
  411. fprintf(stderr, PROGNAME ": can't open PNG file [%s]\n", filename);
  412. ++error;
  413. } else {
  414. incount = fread(inbuf, 1, INBUFSIZE, infile);
  415. if (incount < 8 || !readpng2_check_sig(inbuf, 8)) {
  416. fprintf(stderr, PROGNAME
  417. ": [%s] is not a PNG file: incorrect signature\n",
  418. filename);
  419. ++error;
  420. } else if ((rc = readpng2_init(&rpng2_info)) != 0) {
  421. switch (rc) {
  422. case 2:
  423. fprintf(stderr, PROGNAME
  424. ": [%s] has bad IHDR (libpng longjmp)\n", filename);
  425. break;
  426. case 4:
  427. fprintf(stderr, PROGNAME ": insufficient memory\n");
  428. break;
  429. default:
  430. fprintf(stderr, PROGNAME
  431. ": unknown readpng2_init() error\n");
  432. break;
  433. }
  434. ++error;
  435. }
  436. if (error)
  437. fclose(infile);
  438. }
  439. if (error) {
  440. #ifndef __CYGWIN__
  441. int ch;
  442. #endif
  443. fprintf(stderr, PROGNAME ": aborting.\n");
  444. #ifndef __CYGWIN__
  445. do
  446. ch = _getch();
  447. while (ch != 'q' && ch != 'Q' && ch != 0x1B);
  448. #endif
  449. exit(2);
  450. } else {
  451. fprintf(stderr, "\n%s %s: %s\n", PROGNAME, VERSION, appname);
  452. #ifndef __CYGWIN__
  453. fprintf(stderr,
  454. "\n [console window: closing this window will terminate %s]\n\n",
  455. PROGNAME);
  456. #endif
  457. fflush(stderr);
  458. }
  459. /* set the title-bar string, but make sure buffer doesn't overflow */
  460. alen = strlen(appname);
  461. flen = strlen(filename);
  462. if (alen + flen + 3 > 1023)
  463. sprintf(titlebar, "%s: ...%s", appname, filename+(alen+flen+6-1023));
  464. else
  465. sprintf(titlebar, "%s: %s", appname, filename);
  466. /* set some final rpng2_info variables before entering main data loop */
  467. if (have_bg) {
  468. unsigned r, g, b; /* this approach quiets compiler warnings */
  469. sscanf(bgstr+1, "%2x%2x%2x", &r, &g, &b);
  470. rpng2_info.bg_red = (uch)r;
  471. rpng2_info.bg_green = (uch)g;
  472. rpng2_info.bg_blue = (uch)b;
  473. } else
  474. rpng2_info.need_bgcolor = TRUE;
  475. rpng2_info.state = kPreInit;
  476. rpng2_info.mainprog_init = rpng2_win_init;
  477. rpng2_info.mainprog_display_row = rpng2_win_display_row;
  478. rpng2_info.mainprog_finish_display = rpng2_win_finish_display;
  479. /* OK, this is the fun part: call readpng2_decode_data() at the start of
  480. * the loop to deal with our first buffer of data (read in above to verify
  481. * that the file is a PNG image), then loop through the file and continue
  482. * calling the same routine to handle each chunk of data. It in turn
  483. * passes the data to libpng, which will invoke one or more of our call-
  484. * backs as decoded data become available. We optionally call Sleep() for
  485. * one second per iteration to simulate downloading the image via an analog
  486. * modem. */
  487. for (;;) {
  488. Trace((stderr, "about to call readpng2_decode_data()\n"))
  489. if (readpng2_decode_data(&rpng2_info, inbuf, incount))
  490. ++error;
  491. Trace((stderr, "done with readpng2_decode_data()\n"))
  492. if (error || incount != INBUFSIZE || rpng2_info.state == kDone) {
  493. if (rpng2_info.state == kDone) {
  494. Trace((stderr, "done decoding PNG image\n"))
  495. } else if (ferror(infile)) {
  496. fprintf(stderr, PROGNAME
  497. ": error while reading PNG image file\n");
  498. exit(3);
  499. } else if (feof(infile)) {
  500. fprintf(stderr, PROGNAME ": end of file reached "
  501. "(unexpectedly) while reading PNG image file\n");
  502. exit(3);
  503. } else /* if (error) */ {
  504. // will print error message below
  505. }
  506. break;
  507. }
  508. if (timing)
  509. Sleep(1000L);
  510. incount = fread(inbuf, 1, INBUFSIZE, infile);
  511. }
  512. /* clean up PNG stuff and report any decoding errors */
  513. fclose(infile);
  514. Trace((stderr, "about to call readpng2_cleanup()\n"))
  515. readpng2_cleanup(&rpng2_info);
  516. if (error) {
  517. fprintf(stderr, PROGNAME ": libpng error while decoding PNG image\n");
  518. exit(3);
  519. }
  520. /* wait for the user to tell us when to quit */
  521. while (GetMessage(&msg, NULL, 0, 0)) {
  522. TranslateMessage(&msg);
  523. DispatchMessage(&msg);
  524. }
  525. /* we're done: clean up all image and Windows resources and go away */
  526. Trace((stderr, "about to call rpng2_win_cleanup()\n"))
  527. rpng2_win_cleanup();
  528. return msg.wParam;
  529. }
  530. /* this function is called by readpng2_info_callback() in readpng2.c, which
  531. * in turn is called by libpng after all of the pre-IDAT chunks have been
  532. * read and processed--i.e., we now have enough info to finish initializing */
  533. static void rpng2_win_init()
  534. {
  535. ulg i;
  536. ulg rowbytes = rpng2_info.rowbytes;
  537. Trace((stderr, "beginning rpng2_win_init()\n"))
  538. Trace((stderr, " rowbytes = %d\n", rpng2_info.rowbytes))
  539. Trace((stderr, " width = %ld\n", rpng2_info.width))
  540. Trace((stderr, " height = %ld\n", rpng2_info.height))
  541. rpng2_info.image_data = (uch *)malloc(rowbytes * rpng2_info.height);
  542. if (!rpng2_info.image_data) {
  543. readpng2_cleanup(&rpng2_info);
  544. return;
  545. }
  546. rpng2_info.row_pointers = (uch **)malloc(rpng2_info.height * sizeof(uch *));
  547. if (!rpng2_info.row_pointers) {
  548. free(rpng2_info.image_data);
  549. rpng2_info.image_data = NULL;
  550. readpng2_cleanup(&rpng2_info);
  551. return;
  552. }
  553. for (i = 0; i < rpng2_info.height; ++i)
  554. rpng2_info.row_pointers[i] = rpng2_info.image_data + i*rowbytes;
  555. /*---------------------------------------------------------------------------
  556. Do the basic Windows initialization stuff, make the window, and fill it
  557. with the user-specified, file-specified or default background color.
  558. ---------------------------------------------------------------------------*/
  559. if (rpng2_win_create_window()) {
  560. readpng2_cleanup(&rpng2_info);
  561. return;
  562. }
  563. rpng2_info.state = kWindowInit;
  564. }
  565. static int rpng2_win_create_window()
  566. {
  567. uch bg_red = rpng2_info.bg_red;
  568. uch bg_green = rpng2_info.bg_green;
  569. uch bg_blue = rpng2_info.bg_blue;
  570. uch *dest;
  571. int extra_width, extra_height;
  572. ulg i, j;
  573. WNDCLASSEX wndclass;
  574. RECT rect;
  575. /*---------------------------------------------------------------------------
  576. Allocate memory for the display-specific version of the image (round up
  577. to multiple of 4 for Windows DIB).
  578. ---------------------------------------------------------------------------*/
  579. wimage_rowbytes = ((3*rpng2_info.width + 3L) >> 2) << 2;
  580. if (!(dib = (uch *)malloc(sizeof(BITMAPINFOHEADER) +
  581. wimage_rowbytes*rpng2_info.height)))
  582. {
  583. return 4; /* fail */
  584. }
  585. /*---------------------------------------------------------------------------
  586. Initialize the DIB. Negative height means to use top-down BMP ordering
  587. (must be uncompressed, but that's what we want). Bit count of 1, 4 or 8
  588. implies a colormap of RGBX quads, but 24-bit BMPs just use B,G,R values
  589. directly => wimage_data begins immediately after BMP header.
  590. ---------------------------------------------------------------------------*/
  591. memset(dib, 0, sizeof(BITMAPINFOHEADER));
  592. bmih = (BITMAPINFOHEADER *)dib;
  593. bmih->biSize = sizeof(BITMAPINFOHEADER);
  594. bmih->biWidth = rpng2_info.width;
  595. bmih->biHeight = -((long)rpng2_info.height);
  596. bmih->biPlanes = 1;
  597. bmih->biBitCount = 24;
  598. bmih->biCompression = 0;
  599. wimage_data = dib + sizeof(BITMAPINFOHEADER);
  600. /*---------------------------------------------------------------------------
  601. Fill window with the specified background color (default is black), but
  602. defer loading faked "background image" until window is displayed (may be
  603. slow to compute). Data are in BGR order.
  604. ---------------------------------------------------------------------------*/
  605. if (bg_image) { /* just fill with black for now */
  606. memset(wimage_data, 0, wimage_rowbytes*rpng2_info.height);
  607. } else {
  608. for (j = 0; j < rpng2_info.height; ++j) {
  609. dest = wimage_data + j*wimage_rowbytes;
  610. for (i = rpng2_info.width; i > 0; --i) {
  611. *dest++ = bg_blue;
  612. *dest++ = bg_green;
  613. *dest++ = bg_red;
  614. }
  615. }
  616. }
  617. /*---------------------------------------------------------------------------
  618. Set the window parameters.
  619. ---------------------------------------------------------------------------*/
  620. memset(&wndclass, 0, sizeof(wndclass));
  621. wndclass.cbSize = sizeof(wndclass);
  622. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  623. wndclass.lpfnWndProc = rpng2_win_wndproc;
  624. wndclass.hInstance = global_hInst;
  625. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  626. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  627. wndclass.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
  628. wndclass.lpszMenuName = NULL;
  629. wndclass.lpszClassName = progname;
  630. wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  631. RegisterClassEx(&wndclass);
  632. /*---------------------------------------------------------------------------
  633. Finally, create the window.
  634. ---------------------------------------------------------------------------*/
  635. extra_width = 2*(GetSystemMetrics(SM_CXBORDER) +
  636. GetSystemMetrics(SM_CXDLGFRAME));
  637. extra_height = 2*(GetSystemMetrics(SM_CYBORDER) +
  638. GetSystemMetrics(SM_CYDLGFRAME)) +
  639. GetSystemMetrics(SM_CYCAPTION);
  640. global_hwnd = CreateWindow(progname, titlebar, WS_OVERLAPPEDWINDOW,
  641. CW_USEDEFAULT, CW_USEDEFAULT, rpng2_info.width+extra_width,
  642. rpng2_info.height+extra_height, NULL, NULL, global_hInst, NULL);
  643. ShowWindow(global_hwnd, global_showmode);
  644. UpdateWindow(global_hwnd);
  645. /*---------------------------------------------------------------------------
  646. Now compute the background image and display it. If it fails (memory
  647. allocation), revert to a plain background color.
  648. ---------------------------------------------------------------------------*/
  649. if (bg_image) {
  650. static const char *msg = "Computing background image...";
  651. int x, y, len = strlen(msg);
  652. HDC hdc = GetDC(global_hwnd);
  653. TEXTMETRIC tm;
  654. GetTextMetrics(hdc, &tm);
  655. x = (rpng2_info.width - len*tm.tmAveCharWidth)/2;
  656. y = (rpng2_info.height - tm.tmHeight)/2;
  657. SetBkMode(hdc, TRANSPARENT);
  658. SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
  659. /* this can still begin out of bounds even if x is positive (???): */
  660. TextOut(hdc, ((x < 0)? 0 : x), ((y < 0)? 0 : y), msg, len);
  661. ReleaseDC(global_hwnd, hdc);
  662. rpng2_win_load_bg_image(); /* resets bg_image if fails */
  663. }
  664. if (!bg_image) {
  665. for (j = 0; j < rpng2_info.height; ++j) {
  666. dest = wimage_data + j*wimage_rowbytes;
  667. for (i = rpng2_info.width; i > 0; --i) {
  668. *dest++ = bg_blue;
  669. *dest++ = bg_green;
  670. *dest++ = bg_red;
  671. }
  672. }
  673. }
  674. rect.left = 0L;
  675. rect.top = 0L;
  676. rect.right = (LONG)rpng2_info.width; /* possibly off by one? */
  677. rect.bottom = (LONG)rpng2_info.height; /* possibly off by one? */
  678. InvalidateRect(global_hwnd, &rect, FALSE);
  679. UpdateWindow(global_hwnd); /* similar to XFlush() */
  680. return 0;
  681. } /* end function rpng2_win_create_window() */
  682. static int rpng2_win_load_bg_image()
  683. {
  684. uch *src, *dest;
  685. uch r1, r2, g1, g2, b1, b2;
  686. uch r1_inv, r2_inv, g1_inv, g2_inv, b1_inv, b2_inv;
  687. int k, hmax, max;
  688. int xidx, yidx, yidx_max = (bgscale-1);
  689. int even_odd_vert, even_odd_horiz, even_odd;
  690. int invert_gradient2 = (bg[pat].type & 0x08);
  691. int invert_column;
  692. ulg i, row;
  693. /*---------------------------------------------------------------------------
  694. Allocate buffer for fake background image to be used with transparent
  695. images; if this fails, revert to plain background color.
  696. ---------------------------------------------------------------------------*/
  697. bg_rowbytes = 3 * rpng2_info.width;
  698. bg_data = (uch *)malloc(bg_rowbytes * rpng2_info.height);
  699. if (!bg_data) {
  700. fprintf(stderr, PROGNAME
  701. ": unable to allocate memory for background image\n");
  702. bg_image = 0;
  703. return 1;
  704. }
  705. /*---------------------------------------------------------------------------
  706. Vertical gradients (ramps) in NxN squares, alternating direction and
  707. colors (N == bgscale).
  708. ---------------------------------------------------------------------------*/
  709. if ((bg[pat].type & 0x07) == 0) {
  710. uch r1_min = rgb[bg[pat].rgb1_min].r;
  711. uch g1_min = rgb[bg[pat].rgb1_min].g;
  712. uch b1_min = rgb[bg[pat].rgb1_min].b;
  713. uch r2_min = rgb[bg[pat].rgb2_min].r;
  714. uch g2_min = rgb[bg[pat].rgb2_min].g;
  715. uch b2_min = rgb[bg[pat].rgb2_min].b;
  716. int r1_diff = rgb[bg[pat].rgb1_max].r - r1_min;
  717. int g1_diff = rgb[bg[pat].rgb1_max].g - g1_min;
  718. int b1_diff = rgb[bg[pat].rgb1_max].b - b1_min;
  719. int r2_diff = rgb[bg[pat].rgb2_max].r - r2_min;
  720. int g2_diff = rgb[bg[pat].rgb2_max].g - g2_min;
  721. int b2_diff = rgb[bg[pat].rgb2_max].b - b2_min;
  722. for (row = 0; row < rpng2_info.height; ++row) {
  723. yidx = row % bgscale;
  724. even_odd_vert = (row / bgscale) & 1;
  725. r1 = r1_min + (r1_diff * yidx) / yidx_max;
  726. g1 = g1_min + (g1_diff * yidx) / yidx_max;
  727. b1 = b1_min + (b1_diff * yidx) / yidx_max;
  728. r1_inv = r1_min + (r1_diff * (yidx_max-yidx)) / yidx_max;
  729. g1_inv = g1_min + (g1_diff * (yidx_max-yidx)) / yidx_max;
  730. b1_inv = b1_min + (b1_diff * (yidx_max-yidx)) / yidx_max;
  731. r2 = r2_min + (r2_diff * yidx) / yidx_max;
  732. g2 = g2_min + (g2_diff * yidx) / yidx_max;
  733. b2 = b2_min + (b2_diff * yidx) / yidx_max;
  734. r2_inv = r2_min + (r2_diff * (yidx_max-yidx)) / yidx_max;
  735. g2_inv = g2_min + (g2_diff * (yidx_max-yidx)) / yidx_max;
  736. b2_inv = b2_min + (b2_diff * (yidx_max-yidx)) / yidx_max;
  737. dest = bg_data + row*bg_rowbytes;
  738. for (i = 0; i < rpng2_info.width; ++i) {
  739. even_odd_horiz = (i / bgscale) & 1;
  740. even_odd = even_odd_vert ^ even_odd_horiz;
  741. invert_column =
  742. (even_odd_horiz && (bg[pat].type & 0x10));
  743. if (even_odd == 0) { /* gradient #1 */
  744. if (invert_column) {
  745. *dest++ = r1_inv;
  746. *dest++ = g1_inv;
  747. *dest++ = b1_inv;
  748. } else {
  749. *dest++ = r1;
  750. *dest++ = g1;
  751. *dest++ = b1;
  752. }
  753. } else { /* gradient #2 */
  754. if ((invert_column && invert_gradient2) ||
  755. (!invert_column && !invert_gradient2))
  756. {
  757. *dest++ = r2; /* not inverted or */
  758. *dest++ = g2; /* doubly inverted */
  759. *dest++ = b2;
  760. } else {
  761. *dest++ = r2_inv;
  762. *dest++ = g2_inv; /* singly inverted */
  763. *dest++ = b2_inv;
  764. }
  765. }
  766. }
  767. }
  768. /*---------------------------------------------------------------------------
  769. Soft gradient-diamonds with scale = bgscale. Code contributed by Adam
  770. M. Costello.
  771. ---------------------------------------------------------------------------*/
  772. } else if ((bg[pat].type & 0x07) == 1) {
  773. hmax = (bgscale-1)/2; /* half the max weight of a color */
  774. max = 2*hmax; /* the max weight of a color */
  775. r1 = rgb[bg[pat].rgb1_max].r;
  776. g1 = rgb[bg[pat].rgb1_max].g;
  777. b1 = rgb[bg[pat].rgb1_max].b;
  778. r2 = rgb[bg[pat].rgb2_max].r;
  779. g2 = rgb[bg[pat].rgb2_max].g;
  780. b2 = rgb[bg[pat].rgb2_max].b;
  781. for (row = 0; row < rpng2_info.height; ++row) {
  782. yidx = row % bgscale;
  783. if (yidx > hmax)
  784. yidx = bgscale-1 - yidx;
  785. dest = bg_data + row*bg_rowbytes;
  786. for (i = 0; i < rpng2_info.width; ++i) {
  787. xidx = i % bgscale;
  788. if (xidx > hmax)
  789. xidx = bgscale-1 - xidx;
  790. k = xidx + yidx;
  791. *dest++ = (k*r1 + (max-k)*r2) / max;
  792. *dest++ = (k*g1 + (max-k)*g2) / max;
  793. *dest++ = (k*b1 + (max-k)*b2) / max;
  794. }
  795. }
  796. /*---------------------------------------------------------------------------
  797. Radial "starburst" with azimuthal sinusoids; [eventually number of sinu-
  798. soids will equal bgscale?]. This one is slow but very cool. Code con-
  799. tributed by Pieter S. van der Meulen (originally in Smalltalk).
  800. ---------------------------------------------------------------------------*/
  801. } else if ((bg[pat].type & 0x07) == 2) {
  802. uch ch;
  803. int ii, x, y, hw, hh, grayspot;
  804. double freq, rotate, saturate, gray, intensity;
  805. double angle=0.0, aoffset=0.0, maxDist, dist;
  806. double red=0.0, green=0.0, blue=0.0, hue, s, v, f, p, q, t;
  807. fprintf(stderr, "%s: computing radial background...",
  808. PROGNAME);
  809. fflush(stderr);
  810. hh = rpng2_info.height / 2;
  811. hw = rpng2_info.width / 2;
  812. /* variables for radial waves:
  813. * aoffset: number of degrees to rotate hue [CURRENTLY NOT USED]
  814. * freq: number of color beams originating from the center
  815. * grayspot: size of the graying center area (anti-alias)
  816. * rotate: rotation of the beams as a function of radius
  817. * saturate: saturation of beams' shape azimuthally
  818. */
  819. angle = CLIP(angle, 0.0, 360.0);
  820. grayspot = CLIP(bg[pat].bg_gray, 1, (hh + hw));
  821. freq = MAX((double)bg[pat].bg_freq, 0.0);
  822. saturate = (double)bg[pat].bg_bsat * 0.1;
  823. rotate = (double)bg[pat].bg_brot * 0.1;
  824. gray = 0.0;
  825. intensity = 0.0;
  826. maxDist = (double)((hw*hw) + (hh*hh));
  827. for (row = 0; row < rpng2_info.height; ++row) {
  828. y = row - hh;
  829. dest = bg_data + row*bg_rowbytes;
  830. for (i = 0; i < rpng2_info.width; ++i) {
  831. x = i - hw;
  832. angle = (x == 0)? PI_2 : atan((double)y / (double)x);
  833. gray = (double)MAX(ABS(y), ABS(x)) / grayspot;
  834. gray = MIN(1.0, gray);
  835. dist = (double)((x*x) + (y*y)) / maxDist;
  836. intensity = cos((angle+(rotate*dist*PI)) * freq) *
  837. gray * saturate;
  838. intensity = (MAX(MIN(intensity,1.0),-1.0) + 1.0) * 0.5;
  839. hue = (angle + PI) * INV_PI_360 + aoffset;
  840. s = gray * ((double)(ABS(x)+ABS(y)) / (double)(hw + hh));
  841. s = MIN(MAX(s,0.0), 1.0);
  842. v = MIN(MAX(intensity,0.0), 1.0);
  843. if (s == 0.0) {
  844. ch = (uch)(v * 255.0);
  845. *dest++ = ch;
  846. *dest++ = ch;
  847. *dest++ = ch;
  848. } else {
  849. if ((hue < 0.0) || (hue >= 360.0))
  850. hue -= (((int)(hue / 360.0)) * 360.0);
  851. hue /= 60.0;
  852. ii = (int)hue;
  853. f = hue - (double)ii;
  854. p = (1.0 - s) * v;
  855. q = (1.0 - (s * f)) * v;
  856. t = (1.0 - (s * (1.0 - f))) * v;
  857. if (ii == 0) { red = v; green = t; blue = p; }
  858. else if (ii == 1) { red = q; green = v; blue = p; }
  859. else if (ii == 2) { red = p; green = v; blue = t; }
  860. else if (ii == 3) { red = p; green = q; blue = v; }
  861. else if (ii == 4) { red = t; green = p; blue = v; }
  862. else if (ii == 5) { red = v; green = p; blue = q; }
  863. *dest++ = (uch)(red * 255.0);
  864. *dest++ = (uch)(green * 255.0);
  865. *dest++ = (uch)(blue * 255.0);
  866. }
  867. }
  868. }
  869. fprintf(stderr, "done.\n");
  870. fflush(stderr);
  871. }
  872. /*---------------------------------------------------------------------------
  873. Blast background image to display buffer before beginning PNG decode;
  874. calling function will handle invalidation and UpdateWindow() call.
  875. ---------------------------------------------------------------------------*/
  876. for (row = 0; row < rpng2_info.height; ++row) {
  877. src = bg_data + row*bg_rowbytes;
  878. dest = wimage_data + row*wimage_rowbytes;
  879. for (i = rpng2_info.width; i > 0; --i) {
  880. r1 = *src++;
  881. g1 = *src++;
  882. b1 = *src++;
  883. *dest++ = b1;
  884. *dest++ = g1; /* note reverse order */
  885. *dest++ = r1;
  886. }
  887. }
  888. return 0;
  889. } /* end function rpng2_win_load_bg_image() */
  890. static void rpng2_win_display_row(ulg row)
  891. {
  892. uch bg_red = rpng2_info.bg_red;
  893. uch bg_green = rpng2_info.bg_green;
  894. uch bg_blue = rpng2_info.bg_blue;
  895. uch *src, *src2=NULL, *dest;
  896. uch r, g, b, a;
  897. ulg i;
  898. static int rows=0;
  899. static ulg firstrow;
  900. /*---------------------------------------------------------------------------
  901. rows and firstrow simply track how many rows (and which ones) have not
  902. yet been displayed; alternatively, we could call InvalidateRect() for
  903. every row and not bother with the records-keeping.
  904. ---------------------------------------------------------------------------*/
  905. Trace((stderr, "beginning rpng2_win_display_row()\n"))
  906. if (rows == 0)
  907. firstrow = row; /* first row not yet displayed */
  908. ++rows; /* count of rows received but not yet displayed */
  909. /*---------------------------------------------------------------------------
  910. Aside from the use of the rpng2_info struct and the lack of an outer
  911. loop (over rows), this routine is identical to rpng_win_display_image()
  912. in the non-progressive version of the program.
  913. ---------------------------------------------------------------------------*/
  914. src = rpng2_info.image_data + row*rpng2_info.rowbytes;
  915. if (bg_image)
  916. src2 = bg_data + row*bg_rowbytes;
  917. dest = wimage_data + row*wimage_rowbytes;
  918. if (rpng2_info.channels == 3) {
  919. for (i = rpng2_info.width; i > 0; --i) {
  920. r = *src++;
  921. g = *src++;
  922. b = *src++;
  923. *dest++ = b;
  924. *dest++ = g; /* note reverse order */
  925. *dest++ = r;
  926. }
  927. } else /* if (rpng2_info.channels == 4) */ {
  928. for (i = rpng2_info.width; i > 0; --i) {
  929. r = *src++;
  930. g = *src++;
  931. b = *src++;
  932. a = *src++;
  933. if (bg_image) {
  934. bg_red = *src2++;
  935. bg_green = *src2++;
  936. bg_blue = *src2++;
  937. }
  938. if (a == 255) {
  939. *dest++ = b;
  940. *dest++ = g;
  941. *dest++ = r;
  942. } else if (a == 0) {
  943. *dest++ = bg_blue;
  944. *dest++ = bg_green;
  945. *dest++ = bg_red;
  946. } else {
  947. /* this macro (copied from png.h) composites the
  948. * foreground and background values and puts the
  949. * result into the first argument; there are no
  950. * side effects with the first argument */
  951. alpha_composite(*dest++, b, a, bg_blue);
  952. alpha_composite(*dest++, g, a, bg_green);
  953. alpha_composite(*dest++, r, a, bg_red);
  954. }
  955. }
  956. }
  957. /*---------------------------------------------------------------------------
  958. Display after every 16 rows or when on last row. (Region may include
  959. previously displayed lines due to interlacing--i.e., not contiguous.)
  960. ---------------------------------------------------------------------------*/
  961. if ((rows & 0xf) == 0 || row == rpng2_info.height-1) {
  962. RECT rect;
  963. rect.left = 0L;
  964. rect.top = (LONG)firstrow;
  965. rect.right = (LONG)rpng2_info.width; /* possibly off by one? */
  966. rect.bottom = (LONG)row + 1L; /* possibly off by one? */
  967. InvalidateRect(global_hwnd, &rect, FALSE);
  968. UpdateWindow(global_hwnd); /* similar to XFlush() */
  969. rows = 0;
  970. }
  971. } /* end function rpng2_win_display_row() */
  972. static void rpng2_win_finish_display()
  973. {
  974. Trace((stderr, "beginning rpng2_win_finish_display()\n"))
  975. /* last row has already been displayed by rpng2_win_display_row(), so
  976. * we have nothing to do here except set a flag and let the user know
  977. * that the image is done */
  978. rpng2_info.state = kDone;
  979. printf(
  980. #ifndef __CYGWIN__
  981. "Done. Press Q, Esc or mouse button 1 (within image window) to quit.\n"
  982. #else
  983. "Done. Press mouse button 1 (within image window) to quit.\n"
  984. #endif
  985. );
  986. fflush(stdout);
  987. }
  988. static void rpng2_win_cleanup()
  989. {
  990. if (bg_image && bg_data) {
  991. free(bg_data);
  992. bg_data = NULL;
  993. }
  994. if (rpng2_info.image_data) {
  995. free(rpng2_info.image_data);
  996. rpng2_info.image_data = NULL;
  997. }
  998. if (rpng2_info.row_pointers) {
  999. free(rpng2_info.row_pointers);
  1000. rpng2_info.row_pointers = NULL;
  1001. }
  1002. if (dib) {
  1003. free(dib);
  1004. dib = NULL;
  1005. }
  1006. }
  1007. LRESULT CALLBACK rpng2_win_wndproc(HWND hwnd, UINT iMsg, WPARAM wP, LPARAM lP)
  1008. {
  1009. HDC hdc;
  1010. PAINTSTRUCT ps;
  1011. int rc;
  1012. switch (iMsg) {
  1013. case WM_CREATE:
  1014. /* one-time processing here, if any */
  1015. return 0;
  1016. case WM_PAINT:
  1017. hdc = BeginPaint(hwnd, &ps);
  1018. rc = StretchDIBits(hdc, 0, 0, rpng2_info.width, rpng2_info.height,
  1019. 0, 0, rpng2_info.width, rpng2_info.height,
  1020. wimage_data, (BITMAPINFO *)bmih,
  1021. 0, SRCCOPY);
  1022. EndPaint(hwnd, &ps);
  1023. return 0;
  1024. /* wait for the user to tell us when to quit */
  1025. case WM_CHAR:
  1026. switch (wP) { /* only need one, so ignore repeat count */
  1027. case 'q':
  1028. case 'Q':
  1029. case 0x1B: /* Esc key */
  1030. PostQuitMessage(0);
  1031. }
  1032. return 0;
  1033. case WM_LBUTTONDOWN: /* another way of quitting */
  1034. case WM_DESTROY:
  1035. PostQuitMessage(0);
  1036. return 0;
  1037. }
  1038. return DefWindowProc(hwnd, iMsg, wP, lP);
  1039. }