rpng2-win.c 44 KB

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