rpng2-x.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  1. /*---------------------------------------------------------------------------
  2. rpng2 - progressive-model PNG display program rpng2-x.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 the X Window System (tested by the author under Unix and by Martin
  8. Zinser under OpenVMS; may work under OS/2 with a little tweaking).
  9. Thanks to Adam Costello and Pieter S. van der Meulen for the "diamond"
  10. and "radial waves" patterns, respectively.
  11. to do (someday, maybe):
  12. - fix expose/redraw code: don't draw entire row if only part exposed
  13. - 8-bit (colormapped) X support
  14. - finish resizable checkerboard-gradient (sizes 4-128?)
  15. - use %.1023s to simplify truncation of title-bar string?
  16. ---------------------------------------------------------------------------
  17. Changelog:
  18. - 1.01: initial public release
  19. - 1.02: modified to allow abbreviated options; fixed char/uchar mismatch
  20. - 1.10: added support for non-default visuals; fixed X pixel-conversion
  21. - 1.11: added -usleep option for demos; fixed command-line parsing bug
  22. - 1.12: added -pause option for demos and testing
  23. - 1.20: added runtime MMX-enabling/disabling and new -mmx* options
  24. - 1.21: fixed some small X memory leaks (thanks to François Petitjean)
  25. - 1.22: fixed XFreeGC() crash bug (thanks to Patrick Welche)
  26. - 1.23: added -bgpat 0 mode (std white/gray checkerboard, 8x8 squares)
  27. - 1.30: added -loop option for -bgpat (ifdef FEATURE_LOOP); fixed bpp =
  28. 24; added support for X resources (thanks to Gerhard Niklasch)
  29. - 1.31: added code to skip unused chunks (thanks to Glenn Randers-Pehrson)
  30. - 1.32: added AMD64/EM64T support (__x86_64__); added basic expose/redraw
  31. handling
  32. - 2.00: dual-licensed (added GNU GPL)
  33. - 2.01: fixed 64-bit typo in readpng2.c; fixed -pause usage description
  34. - 2.02: fixed improper display of usage screen on PNG error(s); fixed
  35. unexpected-EOF and file-read-error cases; fixed Trace() cut-and-
  36. paste bugs
  37. - 2.03: deleted runtime MMX-enabling/disabling and obsolete -mmx* options
  38. ---------------------------------------------------------------------------
  39. Copyright (c) 1998-2008 Greg Roelofs. All rights reserved.
  40. This software is provided "as is," without warranty of any kind,
  41. express or implied. In no event shall the author or contributors
  42. be held liable for any damages arising in any way from the use of
  43. this software.
  44. The contents of this file are DUAL-LICENSED. You may modify and/or
  45. redistribute this software according to the terms of one of the
  46. following two licenses (at your option):
  47. LICENSE 1 ("BSD-like with advertising clause"):
  48. Permission is granted to anyone to use this software for any purpose,
  49. including commercial applications, and to alter it and redistribute
  50. it freely, subject to the following restrictions:
  51. 1. Redistributions of source code must retain the above copyright
  52. notice, disclaimer, and this list of conditions.
  53. 2. Redistributions in binary form must reproduce the above copyright
  54. notice, disclaimer, and this list of conditions in the documenta-
  55. tion and/or other materials provided with the distribution.
  56. 3. All advertising materials mentioning features or use of this
  57. software must display the following acknowledgment:
  58. This product includes software developed by Greg Roelofs
  59. and contributors for the book, "PNG: The Definitive Guide,"
  60. published by O'Reilly and Associates.
  61. LICENSE 2 (GNU GPL v2 or later):
  62. This program is free software; you can redistribute it and/or modify
  63. it under the terms of the GNU General Public License as published by
  64. the Free Software Foundation; either version 2 of the License, or
  65. (at your option) any later version.
  66. This program is distributed in the hope that it will be useful,
  67. but WITHOUT ANY WARRANTY; without even the implied warranty of
  68. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  69. GNU General Public License for more details.
  70. You should have received a copy of the GNU General Public License
  71. along with this program; if not, write to the Free Software Foundation,
  72. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  73. ---------------------------------------------------------------------------*/
  74. #define PROGNAME "rpng2-x"
  75. #define LONGNAME "Progressive PNG Viewer for X"
  76. #define VERSION "2.03 of 25 February 2010"
  77. #define RESNAME "rpng2" /* our X resource application name */
  78. #define RESCLASS "Rpng" /* our X resource class name */
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <ctype.h>
  82. #include <string.h>
  83. #include <setjmp.h> /* for jmpbuf declaration in readpng2.h */
  84. #include <time.h>
  85. #include <math.h> /* only for PvdM background code */
  86. #include <X11/Xlib.h>
  87. #include <X11/Xutil.h>
  88. #include <X11/Xos.h>
  89. #include <X11/keysym.h> /* defines XK_* macros */
  90. #ifdef VMS
  91. # include <unistd.h>
  92. #endif
  93. /* all for PvdM background code: */
  94. #ifndef PI
  95. # define PI 3.141592653589793238
  96. #endif
  97. #define PI_2 (PI*0.5)
  98. #define INV_PI_360 (360.0 / PI)
  99. #define MAX(a,b) (a>b?a:b)
  100. #define MIN(a,b) (a<b?a:b)
  101. #define CLIP(a,min,max) MAX(min,MIN((a),max))
  102. #define ABS(a) ((a)<0?-(a):(a))
  103. #define CLIP8P(c) MAX(0,(MIN((c),255))) /* 8-bit pos. integer (uch) */
  104. #define ROUNDF(f) ((int)(f + 0.5))
  105. #define QUIT(e,k) ((e.type == ButtonPress && e.xbutton.button == Button1) || \
  106. (e.type == KeyPress && /* v--- or 1 for shifted keys */ \
  107. ((k = XLookupKeysym(&e.xkey, 0)) == XK_q || k == XK_Escape)))
  108. #define NO_24BIT_MASKS /* undef case not fully written--only for redisplay() */
  109. #define rgb1_max bg_freq
  110. #define rgb1_min bg_gray
  111. #define rgb2_max bg_bsat
  112. #define rgb2_min bg_brot
  113. /* #define DEBUG */ /* this enables the Trace() macros */
  114. #include "readpng2.h" /* typedefs, common macros, readpng2 prototypes */
  115. /* could just include png.h, but this macro is the only thing we need
  116. * (name and typedefs changed to local versions); note that side effects
  117. * only happen with alpha (which could easily be avoided with
  118. * "ush acopy = (alpha);") */
  119. #define alpha_composite(composite, fg, alpha, bg) { \
  120. ush temp = ((ush)(fg)*(ush)(alpha) + \
  121. (ush)(bg)*(ush)(255 - (ush)(alpha)) + (ush)128); \
  122. (composite) = (uch)((temp + (temp >> 8)) >> 8); \
  123. }
  124. #define INBUFSIZE 4096 /* with pseudo-timing on (1 sec delay/block), this
  125. * block size corresponds roughly to a download
  126. * speed 10% faster than theoretical 33.6K maximum
  127. * (assuming 8 data bits, 1 stop bit and no other
  128. * overhead) */
  129. /* local prototypes */
  130. static void rpng2_x_init (void);
  131. static int rpng2_x_create_window (void);
  132. static int rpng2_x_load_bg_image (void);
  133. static void rpng2_x_display_row (ulg row);
  134. static void rpng2_x_finish_display (void);
  135. static void rpng2_x_redisplay_image (ulg startcol, ulg startrow,
  136. ulg width, ulg height);
  137. #ifdef FEATURE_LOOP
  138. static void rpng2_x_reload_bg_image (void);
  139. static int is_number (char *p);
  140. #endif
  141. static void rpng2_x_cleanup (void);
  142. static int rpng2_x_msb (ulg u32val);
  143. static char titlebar[1024], *window_name = titlebar;
  144. static char *appname = LONGNAME;
  145. static char *icon_name = PROGNAME;
  146. static char *res_name = RESNAME;
  147. static char *res_class = RESCLASS;
  148. static char *filename;
  149. static FILE *infile;
  150. static mainprog_info rpng2_info;
  151. static uch inbuf[INBUFSIZE];
  152. static int incount;
  153. static int pat = 6; /* must be less than num_bgpat */
  154. static int bg_image = 0;
  155. static int bgscale, bgscale_default = 16;
  156. static ulg bg_rowbytes;
  157. static uch *bg_data;
  158. int pause_after_pass = FALSE;
  159. int demo_timing = FALSE;
  160. ulg usleep_duration = 0L;
  161. static struct rgb_color {
  162. uch r, g, b;
  163. } rgb[] = {
  164. { 0, 0, 0}, /* 0: black */
  165. {255, 255, 255}, /* 1: white */
  166. {173, 132, 57}, /* 2: tan */
  167. { 64, 132, 0}, /* 3: medium green */
  168. {189, 117, 1}, /* 4: gold */
  169. {253, 249, 1}, /* 5: yellow */
  170. { 0, 0, 255}, /* 6: blue */
  171. { 0, 0, 120}, /* 7: medium blue */
  172. {255, 0, 255}, /* 8: magenta */
  173. { 64, 0, 64}, /* 9: dark magenta */
  174. {255, 0, 0}, /* 10: red */
  175. { 64, 0, 0}, /* 11: dark red */
  176. {255, 127, 0}, /* 12: orange */
  177. {192, 96, 0}, /* 13: darker orange */
  178. { 24, 60, 0}, /* 14: dark green-yellow */
  179. { 85, 125, 200}, /* 15: ice blue */
  180. {192, 192, 192} /* 16: Netscape/Mosaic gray */
  181. };
  182. /* not used for now, but should be for error-checking:
  183. static int num_rgb = sizeof(rgb) / sizeof(struct rgb_color);
  184. */
  185. /*
  186. This whole struct is a fairly cheesy way to keep the number of
  187. command-line options to a minimum. The radial-waves background
  188. type is a particularly poor fit to the integer elements of the
  189. struct...but a few macros and a little fixed-point math will do
  190. wonders for ya.
  191. type bits:
  192. F E D C B A 9 8 7 6 5 4 3 2 1 0
  193. | | | | |
  194. | | +-+-+-- 0 = sharp-edged checkerboard
  195. | | 1 = soft diamonds
  196. | | 2 = radial waves
  197. | | 3-7 = undefined
  198. | +-- gradient #2 inverted?
  199. +-- alternating columns inverted?
  200. */
  201. static struct background_pattern {
  202. ush type;
  203. int rgb1_max, rgb1_min; /* or bg_freq, bg_gray */
  204. int rgb2_max, rgb2_min; /* or bg_bsat, bg_brot (both scaled by 10)*/
  205. } bg[] = {
  206. {0, 1,1, 16,16}, /* checkered: white vs. light gray (basic) */
  207. {0+8, 2,0, 1,15}, /* checkered: tan/black vs. white/ice blue */
  208. {0+24, 2,0, 1,0}, /* checkered: tan/black vs. white/black */
  209. {0+8, 4,5, 0,2}, /* checkered: gold/yellow vs. black/tan */
  210. {0+8, 4,5, 0,6}, /* checkered: gold/yellow vs. black/blue */
  211. {0, 7,0, 8,9}, /* checkered: deep blue/black vs. magenta */
  212. {0+8, 13,0, 5,14}, /* checkered: orange/black vs. yellow */
  213. {0+8, 12,0, 10,11}, /* checkered: orange/black vs. red */
  214. {1, 7,0, 8,0}, /* diamonds: deep blue/black vs. magenta */
  215. {1, 12,0, 11,0}, /* diamonds: orange vs. dark red */
  216. {1, 10,0, 7,0}, /* diamonds: red vs. medium blue */
  217. {1, 4,0, 5,0}, /* diamonds: gold vs. yellow */
  218. {1, 3,0, 0,0}, /* diamonds: medium green vs. black */
  219. {2, 16, 100, 20, 0}, /* radial: ~hard radial color-beams */
  220. {2, 18, 100, 10, 2}, /* radial: soft, curved radial color-beams */
  221. {2, 16, 256, 100, 250}, /* radial: very tight spiral */
  222. {2, 10000, 256, 11, 0} /* radial: dipole-moire' (almost fractal) */
  223. };
  224. static int num_bgpat = sizeof(bg) / sizeof(struct background_pattern);
  225. /* X-specific variables */
  226. static char *displayname;
  227. static XImage *ximage;
  228. static Display *display;
  229. static int depth;
  230. static Visual *visual;
  231. static XVisualInfo *visual_list;
  232. static int RShift, GShift, BShift;
  233. static ulg RMask, GMask, BMask;
  234. static Window window;
  235. static GC gc;
  236. static Colormap colormap;
  237. static int have_nondefault_visual = FALSE;
  238. static int have_colormap = FALSE;
  239. static int have_window = FALSE;
  240. static int have_gc = FALSE;
  241. int main(int argc, char **argv)
  242. {
  243. #ifdef sgi
  244. char tmpline[80];
  245. #endif
  246. char *p, *bgstr = NULL;
  247. int rc, alen, flen;
  248. int error = 0;
  249. int timing = FALSE;
  250. int have_bg = FALSE;
  251. #ifdef FEATURE_LOOP
  252. int loop = FALSE;
  253. long loop_interval = -1; /* seconds (100,000 max) */
  254. #endif
  255. double LUT_exponent; /* just the lookup table */
  256. double CRT_exponent = 2.2; /* just the monitor */
  257. double default_display_exponent; /* whole display system */
  258. XEvent e;
  259. KeySym k;
  260. /* First initialize a few things, just to be sure--memset takes care of
  261. * default background color (black), booleans (FALSE), pointers (NULL),
  262. * etc. */
  263. displayname = (char *)NULL;
  264. filename = (char *)NULL;
  265. memset(&rpng2_info, 0, sizeof(mainprog_info));
  266. /* Set the default value for our display-system exponent, i.e., the
  267. * product of the CRT exponent and the exponent corresponding to
  268. * the frame-buffer's lookup table (LUT), if any. This is not an
  269. * exhaustive list of LUT values (e.g., OpenStep has a lot of weird
  270. * ones), but it should cover 99% of the current possibilities. */
  271. #if defined(NeXT)
  272. /* third-party utilities can modify the default LUT exponent */
  273. LUT_exponent = 1.0 / 2.2;
  274. /*
  275. if (some_next_function_that_returns_gamma(&next_gamma))
  276. LUT_exponent = 1.0 / next_gamma;
  277. */
  278. #elif defined(sgi)
  279. LUT_exponent = 1.0 / 1.7;
  280. /* there doesn't seem to be any documented function to
  281. * get the "gamma" value, so we do it the hard way */
  282. infile = fopen("/etc/config/system.glGammaVal", "r");
  283. if (infile) {
  284. double sgi_gamma;
  285. fgets(tmpline, 80, infile);
  286. fclose(infile);
  287. sgi_gamma = atof(tmpline);
  288. if (sgi_gamma > 0.0)
  289. LUT_exponent = 1.0 / sgi_gamma;
  290. }
  291. #elif defined(Macintosh)
  292. LUT_exponent = 1.8 / 2.61;
  293. /*
  294. if (some_mac_function_that_returns_gamma(&mac_gamma))
  295. LUT_exponent = mac_gamma / 2.61;
  296. */
  297. #else
  298. LUT_exponent = 1.0; /* assume no LUT: most PCs */
  299. #endif
  300. /* the defaults above give 1.0, 1.3, 1.5 and 2.2, respectively: */
  301. default_display_exponent = LUT_exponent * CRT_exponent;
  302. /* If the user has set the SCREEN_GAMMA environment variable as suggested
  303. * (somewhat imprecisely) in the libpng documentation, use that; otherwise
  304. * use the default value we just calculated. Either way, the user may
  305. * override this via a command-line option. */
  306. if ((p = getenv("SCREEN_GAMMA")) != NULL)
  307. rpng2_info.display_exponent = atof(p);
  308. else
  309. rpng2_info.display_exponent = default_display_exponent;
  310. /* Now parse the command line for options and the PNG filename. */
  311. while (*++argv && !error) {
  312. if (!strncmp(*argv, "-display", 2)) {
  313. if (!*++argv)
  314. ++error;
  315. else
  316. displayname = *argv;
  317. } else if (!strncmp(*argv, "-gamma", 2)) {
  318. if (!*++argv)
  319. ++error;
  320. else {
  321. rpng2_info.display_exponent = atof(*argv);
  322. if (rpng2_info.display_exponent <= 0.0)
  323. ++error;
  324. }
  325. } else if (!strncmp(*argv, "-bgcolor", 4)) {
  326. if (!*++argv)
  327. ++error;
  328. else {
  329. bgstr = *argv;
  330. if (strlen(bgstr) != 7 || bgstr[0] != '#')
  331. ++error;
  332. else {
  333. have_bg = TRUE;
  334. bg_image = FALSE;
  335. }
  336. }
  337. } else if (!strncmp(*argv, "-bgpat", 4)) {
  338. if (!*++argv)
  339. ++error;
  340. else {
  341. pat = atoi(*argv);
  342. if (pat >= 0 && pat < num_bgpat) {
  343. bg_image = TRUE;
  344. have_bg = FALSE;
  345. } else
  346. ++error;
  347. }
  348. } else if (!strncmp(*argv, "-usleep", 2)) {
  349. if (!*++argv)
  350. ++error;
  351. else {
  352. usleep_duration = (ulg)atol(*argv);
  353. demo_timing = TRUE;
  354. }
  355. } else if (!strncmp(*argv, "-pause", 2)) {
  356. pause_after_pass = TRUE;
  357. } else if (!strncmp(*argv, "-timing", 2)) {
  358. timing = TRUE;
  359. #ifdef FEATURE_LOOP
  360. } else if (!strncmp(*argv, "-loop", 2)) {
  361. loop = TRUE;
  362. if (!argv[1] || !is_number(argv[1]))
  363. loop_interval = 2;
  364. else {
  365. ++argv;
  366. loop_interval = atol(*argv);
  367. if (loop_interval < 0)
  368. loop_interval = 2;
  369. else if (loop_interval > 100000) /* bit more than one day */
  370. loop_interval = 100000;
  371. }
  372. #endif
  373. } else {
  374. if (**argv != '-') {
  375. filename = *argv;
  376. if (argv[1]) /* shouldn't be any more args after filename */
  377. ++error;
  378. } else
  379. ++error; /* not expecting any other options */
  380. }
  381. }
  382. if (!filename)
  383. ++error;
  384. /* print usage screen if any errors up to this point */
  385. if (error) {
  386. fprintf(stderr, "\n%s %s: %s\n\n", PROGNAME, VERSION, appname);
  387. readpng2_version_info();
  388. fprintf(stderr, "\n"
  389. "Usage: %s [-display xdpy] [-gamma exp] [-bgcolor bg | -bgpat pat]\n"
  390. #ifdef FEATURE_LOOP
  391. " %*s [-usleep dur | -timing] [-pause] [-loop [sec]] file.png\n\n"
  392. #else
  393. " %*s [-usleep dur | -timing] [-pause] file.png\n\n"
  394. #endif
  395. " xdpy\tname of the target X display (e.g., ``hostname:0'')\n"
  396. " exp \ttransfer-function exponent (``gamma'') of the display\n"
  397. "\t\t system in floating-point format (e.g., ``%.1f''); equal\n"
  398. "\t\t to the product of the lookup-table exponent (varies)\n"
  399. "\t\t and the CRT exponent (usually 2.2); must be positive\n"
  400. " bg \tdesired background color in 7-character hex RGB format\n"
  401. "\t\t (e.g., ``#ff7700'' for orange: same as HTML colors);\n"
  402. "\t\t used with transparent images; overrides -bgpat\n"
  403. " pat \tdesired background pattern number (0-%d); used with\n"
  404. "\t\t transparent images; overrides -bgcolor\n"
  405. #ifdef FEATURE_LOOP
  406. " -loop\tloops through background images after initial display\n"
  407. "\t\t is complete (depends on -bgpat)\n"
  408. " sec \tseconds to display each background image (default = 2)\n"
  409. #endif
  410. " dur \tduration in microseconds to wait after displaying each\n"
  411. "\t\t row (for demo purposes)\n"
  412. " -timing\tenables delay for every block read, to simulate modem\n"
  413. "\t\t download of image (~36 Kbps)\n"
  414. " -pause\tpauses after displaying each pass until mouse clicked\n"
  415. "\nPress Q, Esc or mouse button 1 (within image window, after image\n"
  416. "is displayed) to quit.\n"
  417. "\n", PROGNAME,
  418. (int)strlen(PROGNAME), " ", default_display_exponent, num_bgpat-1);
  419. exit(1);
  420. }
  421. if (!(infile = fopen(filename, "rb"))) {
  422. fprintf(stderr, PROGNAME ": can't open PNG file [%s]\n", filename);
  423. ++error;
  424. } else {
  425. incount = fread(inbuf, 1, INBUFSIZE, infile);
  426. if (incount < 8 || !readpng2_check_sig(inbuf, 8)) {
  427. fprintf(stderr, PROGNAME
  428. ": [%s] is not a PNG file: incorrect signature\n",
  429. filename);
  430. ++error;
  431. } else if ((rc = readpng2_init(&rpng2_info)) != 0) {
  432. switch (rc) {
  433. case 2:
  434. fprintf(stderr, PROGNAME
  435. ": [%s] has bad IHDR (libpng longjmp)\n", filename);
  436. break;
  437. case 4:
  438. fprintf(stderr, PROGNAME ": insufficient memory\n");
  439. break;
  440. default:
  441. fprintf(stderr, PROGNAME
  442. ": unknown readpng2_init() error\n");
  443. break;
  444. }
  445. ++error;
  446. } else {
  447. Trace((stderr, "about to call XOpenDisplay()\n"))
  448. display = XOpenDisplay(displayname);
  449. if (!display) {
  450. readpng2_cleanup(&rpng2_info);
  451. fprintf(stderr, PROGNAME ": can't open X display [%s]\n",
  452. displayname? displayname : "default");
  453. ++error;
  454. }
  455. }
  456. if (error)
  457. fclose(infile);
  458. }
  459. if (error) {
  460. fprintf(stderr, PROGNAME ": aborting.\n");
  461. exit(2);
  462. }
  463. /* set the title-bar string, but make sure buffer doesn't overflow */
  464. alen = strlen(appname);
  465. flen = strlen(filename);
  466. if (alen + flen + 3 > 1023)
  467. sprintf(titlebar, "%s: ...%s", appname, filename+(alen+flen+6-1023));
  468. else
  469. sprintf(titlebar, "%s: %s", appname, filename);
  470. /* set some final rpng2_info variables before entering main data loop */
  471. if (have_bg) {
  472. unsigned r, g, b; /* this approach quiets compiler warnings */
  473. sscanf(bgstr+1, "%2x%2x%2x", &r, &g, &b);
  474. rpng2_info.bg_red = (uch)r;
  475. rpng2_info.bg_green = (uch)g;
  476. rpng2_info.bg_blue = (uch)b;
  477. } else
  478. rpng2_info.need_bgcolor = TRUE;
  479. rpng2_info.state = kPreInit;
  480. rpng2_info.mainprog_init = rpng2_x_init;
  481. rpng2_info.mainprog_display_row = rpng2_x_display_row;
  482. rpng2_info.mainprog_finish_display = rpng2_x_finish_display;
  483. /* OK, this is the fun part: call readpng2_decode_data() at the start of
  484. * the loop to deal with our first buffer of data (read in above to verify
  485. * that the file is a PNG image), then loop through the file and continue
  486. * calling the same routine to handle each chunk of data. It in turn
  487. * passes the data to libpng, which will invoke one or more of our call-
  488. * backs as decoded data become available. We optionally call sleep() for
  489. * one second per iteration to simulate downloading the image via an analog
  490. * modem. */
  491. for (;;) {
  492. Trace((stderr, "about to call readpng2_decode_data()\n"))
  493. if (readpng2_decode_data(&rpng2_info, inbuf, incount))
  494. ++error;
  495. Trace((stderr, "done with readpng2_decode_data()\n"))
  496. if (error || incount != INBUFSIZE || rpng2_info.state == kDone) {
  497. if (rpng2_info.state == kDone) {
  498. Trace((stderr, "done decoding PNG image\n"))
  499. } else if (ferror(infile)) {
  500. fprintf(stderr, PROGNAME
  501. ": error while reading PNG image file\n");
  502. exit(3);
  503. } else if (feof(infile)) {
  504. fprintf(stderr, PROGNAME ": end of file reached "
  505. "(unexpectedly) while reading PNG image file\n");
  506. exit(3);
  507. } else /* if (error) */ {
  508. /* will print error message below */
  509. }
  510. break;
  511. }
  512. if (timing)
  513. sleep(1);
  514. incount = fread(inbuf, 1, INBUFSIZE, infile);
  515. }
  516. /* clean up PNG stuff and report any decoding errors */
  517. fclose(infile);
  518. Trace((stderr, "about to call readpng2_cleanup()\n"))
  519. readpng2_cleanup(&rpng2_info);
  520. if (error) {
  521. fprintf(stderr, PROGNAME ": libpng error while decoding PNG image\n");
  522. exit(3);
  523. }
  524. #ifdef FEATURE_LOOP
  525. if (loop && bg_image) {
  526. Trace((stderr, "entering -loop loop (FEATURE_LOOP)\n"))
  527. for (;;) {
  528. int i, use_sleep;
  529. struct timeval now, then;
  530. /* get current time and add loop_interval to get target time */
  531. if (gettimeofday(&then, NULL) == 0) {
  532. then.tv_sec += loop_interval;
  533. use_sleep = FALSE;
  534. } else
  535. use_sleep = TRUE;
  536. /* do quick check for a quit event but don't wait for it */
  537. /* GRR BUG: should also check for Expose events and redraw... */
  538. if (XCheckMaskEvent(display, KeyPressMask | ButtonPressMask, &e))
  539. if (QUIT(e,k))
  540. break;
  541. /* generate next background image */
  542. if (++pat >= num_bgpat)
  543. pat = 0;
  544. rpng2_x_reload_bg_image();
  545. /* wait for timeout, using whatever means are available */
  546. if (use_sleep || gettimeofday(&now, NULL) != 0) {
  547. for (i = loop_interval; i > 0; --i) {
  548. sleep(1);
  549. /* GRR BUG: also need to check for Expose (and redraw!) */
  550. if (XCheckMaskEvent(display, KeyPressMask | ButtonPressMask,
  551. &e) && QUIT(e,k))
  552. break;
  553. }
  554. } else {
  555. /* Y2038 BUG! */
  556. if (now.tv_sec < then.tv_sec ||
  557. (now.tv_sec == then.tv_sec && now.tv_usec < then.tv_usec))
  558. {
  559. int quit = FALSE;
  560. long seconds_to_go = then.tv_sec - now.tv_sec;
  561. long usleep_usec;
  562. /* basically chew up most of remaining loop-interval with
  563. * calls to sleep(1) interleaved with checks for quit
  564. * events, but also recalc time-to-go periodically; when
  565. * done, clean up any remaining time with usleep() call
  566. * (could also use SIGALRM, but signals are a pain...) */
  567. while (seconds_to_go-- > 1) {
  568. int seconds_done = 0;
  569. for (i = seconds_to_go; i > 0 && !quit; --i) {
  570. sleep(1);
  571. /* GRR BUG: need to check for Expose and redraw */
  572. if (XCheckMaskEvent(display, KeyPressMask |
  573. ButtonPressMask, &e) && QUIT(e,k))
  574. quit = TRUE;
  575. if (++seconds_done > 1000)
  576. break; /* time to redo seconds_to_go meas. */
  577. }
  578. if (quit)
  579. break;
  580. /* OK, more than 1000 seconds since last check:
  581. * correct the time-to-go measurement for drift */
  582. if (gettimeofday(&now, NULL) == 0) {
  583. if (now.tv_sec >= then.tv_sec)
  584. break;
  585. seconds_to_go = then.tv_sec - now.tv_sec;
  586. } else
  587. ++seconds_to_go; /* restore what we subtracted */
  588. }
  589. if (quit)
  590. break; /* breaks outer do-loop, skips redisplay */
  591. /* since difference between "now" and "then" is already
  592. * eaten up to within a couple of seconds, don't need to
  593. * worry about overflow--but might have overshot (neg.) */
  594. if (gettimeofday(&now, NULL) == 0) {
  595. usleep_usec = 1000000L*(then.tv_sec - now.tv_sec) +
  596. then.tv_usec - now.tv_usec;
  597. if (usleep_usec > 0)
  598. usleep((ulg)usleep_usec);
  599. }
  600. }
  601. }
  602. /* composite image against new background and display (note that
  603. * we do not take into account the time spent doing this...) */
  604. rpng2_x_redisplay_image (0, 0, rpng2_info.width, rpng2_info.height);
  605. }
  606. } else /* FALL THROUGH and do the normal thing */
  607. #endif /* FEATURE_LOOP */
  608. /* wait for the user to tell us when to quit */
  609. if (rpng2_info.state >= kWindowInit) {
  610. Trace((stderr, "entering final wait-for-quit-event loop\n"))
  611. do {
  612. XNextEvent(display, &e);
  613. if (e.type == Expose) {
  614. XExposeEvent *ex = (XExposeEvent *)&e;
  615. rpng2_x_redisplay_image (ex->x, ex->y, ex->width, ex->height);
  616. }
  617. } while (!QUIT(e,k));
  618. } else {
  619. fprintf(stderr, PROGNAME ": init callback never called: probable "
  620. "libpng error while decoding PNG metadata\n");
  621. exit(4);
  622. }
  623. /* we're done: clean up all image and X resources and go away */
  624. Trace((stderr, "about to call rpng2_x_cleanup()\n"))
  625. rpng2_x_cleanup();
  626. return 0;
  627. }
  628. /* this function is called by readpng2_info_callback() in readpng2.c, which
  629. * in turn is called by libpng after all of the pre-IDAT chunks have been
  630. * read and processed--i.e., we now have enough info to finish initializing */
  631. static void rpng2_x_init(void)
  632. {
  633. ulg i;
  634. ulg rowbytes = rpng2_info.rowbytes;
  635. Trace((stderr, "beginning rpng2_x_init()\n"))
  636. Trace((stderr, " rowbytes = %d\n", rpng2_info.rowbytes))
  637. Trace((stderr, " width = %ld\n", rpng2_info.width))
  638. Trace((stderr, " height = %ld\n", rpng2_info.height))
  639. rpng2_info.image_data = (uch *)malloc(rowbytes * rpng2_info.height);
  640. if (!rpng2_info.image_data) {
  641. readpng2_cleanup(&rpng2_info);
  642. return;
  643. }
  644. rpng2_info.row_pointers = (uch **)malloc(rpng2_info.height * sizeof(uch *));
  645. if (!rpng2_info.row_pointers) {
  646. free(rpng2_info.image_data);
  647. rpng2_info.image_data = NULL;
  648. readpng2_cleanup(&rpng2_info);
  649. return;
  650. }
  651. for (i = 0; i < rpng2_info.height; ++i)
  652. rpng2_info.row_pointers[i] = rpng2_info.image_data + i*rowbytes;
  653. /* do the basic X initialization stuff, make the window, and fill it with
  654. * the user-specified, file-specified or default background color or
  655. * pattern */
  656. if (rpng2_x_create_window()) {
  657. /* GRR TEMPORARY HACK: this is fundamentally no different from cases
  658. * above; libpng should call our error handler to longjmp() back to us
  659. * when png_ptr goes away. If we/it segfault instead, seems like a
  660. * libpng bug... */
  661. /* we're here via libpng callback, so if window fails, clean and bail */
  662. readpng2_cleanup(&rpng2_info);
  663. rpng2_x_cleanup();
  664. exit(2);
  665. }
  666. rpng2_info.state = kWindowInit;
  667. }
  668. static int rpng2_x_create_window(void)
  669. {
  670. ulg bg_red = rpng2_info.bg_red;
  671. ulg bg_green = rpng2_info.bg_green;
  672. ulg bg_blue = rpng2_info.bg_blue;
  673. ulg bg_pixel = 0L;
  674. ulg attrmask;
  675. int need_colormap = FALSE;
  676. int screen, pad;
  677. uch *xdata;
  678. Window root;
  679. XEvent e;
  680. XGCValues gcvalues;
  681. XSetWindowAttributes attr;
  682. XTextProperty windowName, *pWindowName = &windowName;
  683. XTextProperty iconName, *pIconName = &iconName;
  684. XVisualInfo visual_info;
  685. XSizeHints *size_hints;
  686. XWMHints *wm_hints;
  687. XClassHint *class_hints;
  688. Trace((stderr, "beginning rpng2_x_create_window()\n"))
  689. screen = DefaultScreen(display);
  690. depth = DisplayPlanes(display, screen);
  691. root = RootWindow(display, screen);
  692. #ifdef DEBUG
  693. XSynchronize(display, True);
  694. #endif
  695. if (depth != 16 && depth != 24 && depth != 32) {
  696. int visuals_matched = 0;
  697. Trace((stderr, "default depth is %d: checking other visuals\n",
  698. depth))
  699. /* 24-bit first */
  700. visual_info.screen = screen;
  701. visual_info.depth = 24;
  702. visual_list = XGetVisualInfo(display,
  703. VisualScreenMask | VisualDepthMask, &visual_info, &visuals_matched);
  704. if (visuals_matched == 0) {
  705. /* GRR: add 15-, 16- and 32-bit TrueColor visuals (also DirectColor?) */
  706. fprintf(stderr, "default screen depth %d not supported, and no"
  707. " 24-bit visuals found\n", depth);
  708. return 2;
  709. }
  710. Trace((stderr, "XGetVisualInfo() returned %d 24-bit visuals\n",
  711. visuals_matched))
  712. visual = visual_list[0].visual;
  713. depth = visual_list[0].depth;
  714. /*
  715. colormap_size = visual_list[0].colormap_size;
  716. visual_class = visual->class;
  717. visualID = XVisualIDFromVisual(visual);
  718. */
  719. have_nondefault_visual = TRUE;
  720. need_colormap = TRUE;
  721. } else {
  722. XMatchVisualInfo(display, screen, depth, TrueColor, &visual_info);
  723. visual = visual_info.visual;
  724. }
  725. RMask = visual->red_mask;
  726. GMask = visual->green_mask;
  727. BMask = visual->blue_mask;
  728. /* GRR: add/check 8-bit support */
  729. if (depth == 8 || need_colormap) {
  730. colormap = XCreateColormap(display, root, visual, AllocNone);
  731. if (!colormap) {
  732. fprintf(stderr, "XCreateColormap() failed\n");
  733. return 2;
  734. }
  735. have_colormap = TRUE;
  736. if (depth == 8)
  737. bg_image = FALSE; /* gradient just wastes palette entries */
  738. }
  739. if (depth == 15 || depth == 16) {
  740. RShift = 15 - rpng2_x_msb(RMask); /* these are right-shifts */
  741. GShift = 15 - rpng2_x_msb(GMask);
  742. BShift = 15 - rpng2_x_msb(BMask);
  743. } else if (depth > 16) {
  744. RShift = rpng2_x_msb(RMask) - 7; /* these are left-shifts */
  745. GShift = rpng2_x_msb(GMask) - 7;
  746. BShift = rpng2_x_msb(BMask) - 7;
  747. }
  748. if (depth >= 15 && (RShift < 0 || GShift < 0 || BShift < 0)) {
  749. fprintf(stderr, "rpng2 internal logic error: negative X shift(s)!\n");
  750. return 2;
  751. }
  752. /*---------------------------------------------------------------------------
  753. Finally, create the window.
  754. ---------------------------------------------------------------------------*/
  755. attr.backing_store = Always;
  756. attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask;
  757. attrmask = CWBackingStore | CWEventMask;
  758. if (have_nondefault_visual) {
  759. attr.colormap = colormap;
  760. attr.background_pixel = 0;
  761. attr.border_pixel = 1;
  762. attrmask |= CWColormap | CWBackPixel | CWBorderPixel;
  763. }
  764. window = XCreateWindow(display, root, 0, 0, rpng2_info.width,
  765. rpng2_info.height, 0, depth, InputOutput, visual, attrmask, &attr);
  766. if (window == None) {
  767. fprintf(stderr, "XCreateWindow() failed\n");
  768. return 2;
  769. } else
  770. have_window = TRUE;
  771. if (depth == 8)
  772. XSetWindowColormap(display, window, colormap);
  773. if (!XStringListToTextProperty(&window_name, 1, pWindowName))
  774. pWindowName = NULL;
  775. if (!XStringListToTextProperty(&icon_name, 1, pIconName))
  776. pIconName = NULL;
  777. /* OK if either hints allocation fails; XSetWMProperties() allows NULLs */
  778. if ((size_hints = XAllocSizeHints()) != NULL) {
  779. /* window will not be resizable */
  780. size_hints->flags = PMinSize | PMaxSize;
  781. size_hints->min_width = size_hints->max_width = (int)rpng2_info.width;
  782. size_hints->min_height = size_hints->max_height =
  783. (int)rpng2_info.height;
  784. }
  785. if ((wm_hints = XAllocWMHints()) != NULL) {
  786. wm_hints->initial_state = NormalState;
  787. wm_hints->input = True;
  788. /* wm_hints->icon_pixmap = icon_pixmap; */
  789. wm_hints->flags = StateHint | InputHint /* | IconPixmapHint */ ;
  790. }
  791. if ((class_hints = XAllocClassHint()) != NULL) {
  792. class_hints->res_name = res_name;
  793. class_hints->res_class = res_class;
  794. }
  795. XSetWMProperties(display, window, pWindowName, pIconName, NULL, 0,
  796. size_hints, wm_hints, class_hints);
  797. /* various properties and hints no longer needed; free memory */
  798. if (pWindowName)
  799. XFree(pWindowName->value);
  800. if (pIconName)
  801. XFree(pIconName->value);
  802. if (size_hints)
  803. XFree(size_hints);
  804. if (wm_hints)
  805. XFree(wm_hints);
  806. if (class_hints)
  807. XFree(class_hints);
  808. XMapWindow(display, window);
  809. gc = XCreateGC(display, window, 0, &gcvalues);
  810. have_gc = TRUE;
  811. /*---------------------------------------------------------------------------
  812. Allocate memory for the X- and display-specific version of the image.
  813. ---------------------------------------------------------------------------*/
  814. if (depth == 24 || depth == 32) {
  815. xdata = (uch *)malloc(4*rpng2_info.width*rpng2_info.height);
  816. pad = 32;
  817. } else if (depth == 16) {
  818. xdata = (uch *)malloc(2*rpng2_info.width*rpng2_info.height);
  819. pad = 16;
  820. } else /* depth == 8 */ {
  821. xdata = (uch *)malloc(rpng2_info.width*rpng2_info.height);
  822. pad = 8;
  823. }
  824. if (!xdata) {
  825. fprintf(stderr, PROGNAME ": unable to allocate image memory\n");
  826. return 4;
  827. }
  828. ximage = XCreateImage(display, visual, depth, ZPixmap, 0,
  829. (char *)xdata, rpng2_info.width, rpng2_info.height, pad, 0);
  830. if (!ximage) {
  831. fprintf(stderr, PROGNAME ": XCreateImage() failed\n");
  832. free(xdata);
  833. return 3;
  834. }
  835. /* to avoid testing the byte order every pixel (or doubling the size of
  836. * the drawing routine with a giant if-test), we arbitrarily set the byte
  837. * order to MSBFirst and let Xlib worry about inverting things on little-
  838. * endian machines (e.g., Linux/x86, old VAXen, etc.)--this is not the
  839. * most efficient approach (the giant if-test would be better), but in
  840. * the interest of clarity, we'll take the easy way out... */
  841. ximage->byte_order = MSBFirst;
  842. /*---------------------------------------------------------------------------
  843. Fill window with the specified background color (default is black) or
  844. faked "background image" (but latter is disabled if 8-bit; gradients
  845. just waste palette entries).
  846. ---------------------------------------------------------------------------*/
  847. if (bg_image)
  848. rpng2_x_load_bg_image(); /* resets bg_image if fails */
  849. if (!bg_image) {
  850. if (depth == 24 || depth == 32) {
  851. bg_pixel = (bg_red << RShift) |
  852. (bg_green << GShift) |
  853. (bg_blue << BShift);
  854. } else if (depth == 16) {
  855. bg_pixel = (((bg_red << 8) >> RShift) & RMask) |
  856. (((bg_green << 8) >> GShift) & GMask) |
  857. (((bg_blue << 8) >> BShift) & BMask);
  858. } else /* depth == 8 */ {
  859. /* GRR: add 8-bit support */
  860. }
  861. XSetForeground(display, gc, bg_pixel);
  862. XFillRectangle(display, window, gc, 0, 0, rpng2_info.width,
  863. rpng2_info.height);
  864. }
  865. /*---------------------------------------------------------------------------
  866. Wait for first Expose event to do any drawing, then flush and return.
  867. ---------------------------------------------------------------------------*/
  868. do
  869. XNextEvent(display, &e);
  870. while (e.type != Expose || e.xexpose.count);
  871. XFlush(display);
  872. return 0;
  873. } /* end function rpng2_x_create_window() */
  874. static int rpng2_x_load_bg_image(void)
  875. {
  876. uch *src;
  877. char *dest;
  878. uch r1, r2, g1, g2, b1, b2;
  879. uch r1_inv, r2_inv, g1_inv, g2_inv, b1_inv, b2_inv;
  880. int k, hmax, max;
  881. int xidx, yidx, yidx_max;
  882. int even_odd_vert, even_odd_horiz, even_odd;
  883. int invert_gradient2 = (bg[pat].type & 0x08);
  884. int invert_column;
  885. int ximage_rowbytes = ximage->bytes_per_line;
  886. ulg i, row;
  887. ulg pixel;
  888. /*---------------------------------------------------------------------------
  889. Allocate buffer for fake background image to be used with transparent
  890. images; if this fails, revert to plain background color.
  891. ---------------------------------------------------------------------------*/
  892. bg_rowbytes = 3 * rpng2_info.width;
  893. bg_data = (uch *)malloc(bg_rowbytes * rpng2_info.height);
  894. if (!bg_data) {
  895. fprintf(stderr, PROGNAME
  896. ": unable to allocate memory for background image\n");
  897. bg_image = 0;
  898. return 1;
  899. }
  900. bgscale = (pat == 0)? 8 : bgscale_default;
  901. yidx_max = bgscale - 1;
  902. /*---------------------------------------------------------------------------
  903. Vertical gradients (ramps) in NxN squares, alternating direction and
  904. colors (N == bgscale).
  905. ---------------------------------------------------------------------------*/
  906. if ((bg[pat].type & 0x07) == 0) {
  907. uch r1_min = rgb[bg[pat].rgb1_min].r;
  908. uch g1_min = rgb[bg[pat].rgb1_min].g;
  909. uch b1_min = rgb[bg[pat].rgb1_min].b;
  910. uch r2_min = rgb[bg[pat].rgb2_min].r;
  911. uch g2_min = rgb[bg[pat].rgb2_min].g;
  912. uch b2_min = rgb[bg[pat].rgb2_min].b;
  913. int r1_diff = rgb[bg[pat].rgb1_max].r - r1_min;
  914. int g1_diff = rgb[bg[pat].rgb1_max].g - g1_min;
  915. int b1_diff = rgb[bg[pat].rgb1_max].b - b1_min;
  916. int r2_diff = rgb[bg[pat].rgb2_max].r - r2_min;
  917. int g2_diff = rgb[bg[pat].rgb2_max].g - g2_min;
  918. int b2_diff = rgb[bg[pat].rgb2_max].b - b2_min;
  919. for (row = 0; row < rpng2_info.height; ++row) {
  920. yidx = (int)(row % bgscale);
  921. even_odd_vert = (int)((row / bgscale) & 1);
  922. r1 = r1_min + (r1_diff * yidx) / yidx_max;
  923. g1 = g1_min + (g1_diff * yidx) / yidx_max;
  924. b1 = b1_min + (b1_diff * yidx) / yidx_max;
  925. r1_inv = r1_min + (r1_diff * (yidx_max-yidx)) / yidx_max;
  926. g1_inv = g1_min + (g1_diff * (yidx_max-yidx)) / yidx_max;
  927. b1_inv = b1_min + (b1_diff * (yidx_max-yidx)) / yidx_max;
  928. r2 = r2_min + (r2_diff * yidx) / yidx_max;
  929. g2 = g2_min + (g2_diff * yidx) / yidx_max;
  930. b2 = b2_min + (b2_diff * yidx) / yidx_max;
  931. r2_inv = r2_min + (r2_diff * (yidx_max-yidx)) / yidx_max;
  932. g2_inv = g2_min + (g2_diff * (yidx_max-yidx)) / yidx_max;
  933. b2_inv = b2_min + (b2_diff * (yidx_max-yidx)) / yidx_max;
  934. dest = (char *)bg_data + row*bg_rowbytes;
  935. for (i = 0; i < rpng2_info.width; ++i) {
  936. even_odd_horiz = (int)((i / bgscale) & 1);
  937. even_odd = even_odd_vert ^ even_odd_horiz;
  938. invert_column =
  939. (even_odd_horiz && (bg[pat].type & 0x10));
  940. if (even_odd == 0) { /* gradient #1 */
  941. if (invert_column) {
  942. *dest++ = r1_inv;
  943. *dest++ = g1_inv;
  944. *dest++ = b1_inv;
  945. } else {
  946. *dest++ = r1;
  947. *dest++ = g1;
  948. *dest++ = b1;
  949. }
  950. } else { /* gradient #2 */
  951. if ((invert_column && invert_gradient2) ||
  952. (!invert_column && !invert_gradient2))
  953. {
  954. *dest++ = r2; /* not inverted or */
  955. *dest++ = g2; /* doubly inverted */
  956. *dest++ = b2;
  957. } else {
  958. *dest++ = r2_inv;
  959. *dest++ = g2_inv; /* singly inverted */
  960. *dest++ = b2_inv;
  961. }
  962. }
  963. }
  964. }
  965. /*---------------------------------------------------------------------------
  966. Soft gradient-diamonds with scale = bgscale. Code contributed by Adam
  967. M. Costello.
  968. ---------------------------------------------------------------------------*/
  969. } else if ((bg[pat].type & 0x07) == 1) {
  970. hmax = (bgscale-1)/2; /* half the max weight of a color */
  971. max = 2*hmax; /* the max weight of a color */
  972. r1 = rgb[bg[pat].rgb1_max].r;
  973. g1 = rgb[bg[pat].rgb1_max].g;
  974. b1 = rgb[bg[pat].rgb1_max].b;
  975. r2 = rgb[bg[pat].rgb2_max].r;
  976. g2 = rgb[bg[pat].rgb2_max].g;
  977. b2 = rgb[bg[pat].rgb2_max].b;
  978. for (row = 0; row < rpng2_info.height; ++row) {
  979. yidx = (int)(row % bgscale);
  980. if (yidx > hmax)
  981. yidx = bgscale-1 - yidx;
  982. dest = (char *)bg_data + row*bg_rowbytes;
  983. for (i = 0; i < rpng2_info.width; ++i) {
  984. xidx = (int)(i % bgscale);
  985. if (xidx > hmax)
  986. xidx = bgscale-1 - xidx;
  987. k = xidx + yidx;
  988. *dest++ = (k*r1 + (max-k)*r2) / max;
  989. *dest++ = (k*g1 + (max-k)*g2) / max;
  990. *dest++ = (k*b1 + (max-k)*b2) / max;
  991. }
  992. }
  993. /*---------------------------------------------------------------------------
  994. Radial "starburst" with azimuthal sinusoids; [eventually number of sinu-
  995. soids will equal bgscale?]. This one is slow but very cool. Code con-
  996. tributed by Pieter S. van der Meulen (originally in Smalltalk).
  997. ---------------------------------------------------------------------------*/
  998. } else if ((bg[pat].type & 0x07) == 2) {
  999. uch ch;
  1000. int ii, x, y, hw, hh, grayspot;
  1001. double freq, rotate, saturate, gray, intensity;
  1002. double angle=0.0, aoffset=0.0, maxDist, dist;
  1003. double red=0.0, green=0.0, blue=0.0, hue, s, v, f, p, q, t;
  1004. fprintf(stderr, "%s: computing radial background...",
  1005. PROGNAME);
  1006. fflush(stderr);
  1007. hh = (int)(rpng2_info.height / 2);
  1008. hw = (int)(rpng2_info.width / 2);
  1009. /* variables for radial waves:
  1010. * aoffset: number of degrees to rotate hue [CURRENTLY NOT USED]
  1011. * freq: number of color beams originating from the center
  1012. * grayspot: size of the graying center area (anti-alias)
  1013. * rotate: rotation of the beams as a function of radius
  1014. * saturate: saturation of beams' shape azimuthally
  1015. */
  1016. angle = CLIP(angle, 0.0, 360.0);
  1017. grayspot = CLIP(bg[pat].bg_gray, 1, (hh + hw));
  1018. freq = MAX((double)bg[pat].bg_freq, 0.0);
  1019. saturate = (double)bg[pat].bg_bsat * 0.1;
  1020. rotate = (double)bg[pat].bg_brot * 0.1;
  1021. gray = 0.0;
  1022. intensity = 0.0;
  1023. maxDist = (double)((hw*hw) + (hh*hh));
  1024. for (row = 0; row < rpng2_info.height; ++row) {
  1025. y = (int)(row - hh);
  1026. dest = (char *)bg_data + row*bg_rowbytes;
  1027. for (i = 0; i < rpng2_info.width; ++i) {
  1028. x = (int)(i - hw);
  1029. angle = (x == 0)? PI_2 : atan((double)y / (double)x);
  1030. gray = (double)MAX(ABS(y), ABS(x)) / grayspot;
  1031. gray = MIN(1.0, gray);
  1032. dist = (double)((x*x) + (y*y)) / maxDist;
  1033. intensity = cos((angle+(rotate*dist*PI)) * freq) *
  1034. gray * saturate;
  1035. intensity = (MAX(MIN(intensity,1.0),-1.0) + 1.0) * 0.5;
  1036. hue = (angle + PI) * INV_PI_360 + aoffset;
  1037. s = gray * ((double)(ABS(x)+ABS(y)) / (double)(hw + hh));
  1038. s = MIN(MAX(s,0.0), 1.0);
  1039. v = MIN(MAX(intensity,0.0), 1.0);
  1040. if (s == 0.0) {
  1041. ch = (uch)(v * 255.0);
  1042. *dest++ = ch;
  1043. *dest++ = ch;
  1044. *dest++ = ch;
  1045. } else {
  1046. if ((hue < 0.0) || (hue >= 360.0))
  1047. hue -= (((int)(hue / 360.0)) * 360.0);
  1048. hue /= 60.0;
  1049. ii = (int)hue;
  1050. f = hue - (double)ii;
  1051. p = (1.0 - s) * v;
  1052. q = (1.0 - (s * f)) * v;
  1053. t = (1.0 - (s * (1.0 - f))) * v;
  1054. if (ii == 0) { red = v; green = t; blue = p; }
  1055. else if (ii == 1) { red = q; green = v; blue = p; }
  1056. else if (ii == 2) { red = p; green = v; blue = t; }
  1057. else if (ii == 3) { red = p; green = q; blue = v; }
  1058. else if (ii == 4) { red = t; green = p; blue = v; }
  1059. else if (ii == 5) { red = v; green = p; blue = q; }
  1060. *dest++ = (uch)(red * 255.0);
  1061. *dest++ = (uch)(green * 255.0);
  1062. *dest++ = (uch)(blue * 255.0);
  1063. }
  1064. }
  1065. }
  1066. fprintf(stderr, "done.\n");
  1067. fflush(stderr);
  1068. }
  1069. /*---------------------------------------------------------------------------
  1070. Blast background image to display buffer before beginning PNG decode.
  1071. ---------------------------------------------------------------------------*/
  1072. if (depth == 24 || depth == 32) {
  1073. ulg red, green, blue;
  1074. int bpp = ximage->bits_per_pixel;
  1075. for (row = 0; row < rpng2_info.height; ++row) {
  1076. src = bg_data + row*bg_rowbytes;
  1077. dest = ximage->data + row*ximage_rowbytes;
  1078. if (bpp == 32) { /* slightly optimized version */
  1079. for (i = rpng2_info.width; i > 0; --i) {
  1080. red = *src++;
  1081. green = *src++;
  1082. blue = *src++;
  1083. pixel = (red << RShift) |
  1084. (green << GShift) |
  1085. (blue << BShift);
  1086. /* recall that we set ximage->byte_order = MSBFirst above */
  1087. *dest++ = (char)((pixel >> 24) & 0xff);
  1088. *dest++ = (char)((pixel >> 16) & 0xff);
  1089. *dest++ = (char)((pixel >> 8) & 0xff);
  1090. *dest++ = (char)( pixel & 0xff);
  1091. }
  1092. } else {
  1093. for (i = rpng2_info.width; i > 0; --i) {
  1094. red = *src++;
  1095. green = *src++;
  1096. blue = *src++;
  1097. pixel = (red << RShift) |
  1098. (green << GShift) |
  1099. (blue << BShift);
  1100. /* recall that we set ximage->byte_order = MSBFirst above */
  1101. /* GRR BUG? this assumes bpp == 24 & bits are packed low */
  1102. /* (probably need to use RShift, RMask, etc.) */
  1103. *dest++ = (char)((pixel >> 16) & 0xff);
  1104. *dest++ = (char)((pixel >> 8) & 0xff);
  1105. *dest++ = (char)( pixel & 0xff);
  1106. }
  1107. }
  1108. }
  1109. } else if (depth == 16) {
  1110. ush red, green, blue;
  1111. for (row = 0; row < rpng2_info.height; ++row) {
  1112. src = bg_data + row*bg_rowbytes;
  1113. dest = ximage->data + row*ximage_rowbytes;
  1114. for (i = rpng2_info.width; i > 0; --i) {
  1115. red = ((ush)(*src) << 8); ++src;
  1116. green = ((ush)(*src) << 8); ++src;
  1117. blue = ((ush)(*src) << 8); ++src;
  1118. pixel = ((red >> RShift) & RMask) |
  1119. ((green >> GShift) & GMask) |
  1120. ((blue >> BShift) & BMask);
  1121. /* recall that we set ximage->byte_order = MSBFirst above */
  1122. *dest++ = (char)((pixel >> 8) & 0xff);
  1123. *dest++ = (char)( pixel & 0xff);
  1124. }
  1125. }
  1126. } else /* depth == 8 */ {
  1127. /* GRR: add 8-bit support */
  1128. }
  1129. XPutImage(display, window, gc, ximage, 0, 0, 0, 0, rpng2_info.width,
  1130. rpng2_info.height);
  1131. return 0;
  1132. } /* end function rpng2_x_load_bg_image() */
  1133. static void rpng2_x_display_row(ulg row)
  1134. {
  1135. uch bg_red = rpng2_info.bg_red;
  1136. uch bg_green = rpng2_info.bg_green;
  1137. uch bg_blue = rpng2_info.bg_blue;
  1138. uch *src, *src2=NULL;
  1139. char *dest;
  1140. uch r, g, b, a;
  1141. int ximage_rowbytes = ximage->bytes_per_line;
  1142. ulg i, pixel;
  1143. static int rows=0, prevpass=(-1);
  1144. static ulg firstrow;
  1145. /*---------------------------------------------------------------------------
  1146. rows and firstrow simply track how many rows (and which ones) have not
  1147. yet been displayed; alternatively, we could call XPutImage() for every
  1148. row and not bother with the records-keeping.
  1149. ---------------------------------------------------------------------------*/
  1150. Trace((stderr, "beginning rpng2_x_display_row()\n"))
  1151. if (rpng2_info.pass != prevpass) {
  1152. if (pause_after_pass && rpng2_info.pass > 0) {
  1153. XEvent e;
  1154. KeySym k;
  1155. fprintf(stderr,
  1156. "%s: end of pass %d of 7; click in image window to continue\n",
  1157. PROGNAME, prevpass + 1);
  1158. do
  1159. XNextEvent(display, &e);
  1160. while (!QUIT(e,k));
  1161. }
  1162. fprintf(stderr, "%s: pass %d of 7\r", PROGNAME, rpng2_info.pass + 1);
  1163. fflush(stderr);
  1164. prevpass = rpng2_info.pass;
  1165. }
  1166. if (rows == 0)
  1167. firstrow = row; /* first row that is not yet displayed */
  1168. ++rows; /* count of rows received but not yet displayed */
  1169. /*---------------------------------------------------------------------------
  1170. Aside from the use of the rpng2_info struct, the lack of an outer loop
  1171. (over rows) and moving the XPutImage() call outside the "if (depth)"
  1172. tests, this routine is identical to rpng_x_display_image() in the non-
  1173. progressive version of the program.
  1174. ---------------------------------------------------------------------------*/
  1175. if (depth == 24 || depth == 32) {
  1176. ulg red, green, blue;
  1177. int bpp = ximage->bits_per_pixel;
  1178. src = rpng2_info.image_data + row*rpng2_info.rowbytes;
  1179. if (bg_image)
  1180. src2 = bg_data + row*bg_rowbytes;
  1181. dest = ximage->data + row*ximage_rowbytes;
  1182. if (rpng2_info.channels == 3) {
  1183. for (i = rpng2_info.width; i > 0; --i) {
  1184. red = *src++;
  1185. green = *src++;
  1186. blue = *src++;
  1187. pixel = (red << RShift) |
  1188. (green << GShift) |
  1189. (blue << BShift);
  1190. /* recall that we set ximage->byte_order = MSBFirst above */
  1191. if (bpp == 32) {
  1192. *dest++ = (char)((pixel >> 24) & 0xff);
  1193. *dest++ = (char)((pixel >> 16) & 0xff);
  1194. *dest++ = (char)((pixel >> 8) & 0xff);
  1195. *dest++ = (char)( pixel & 0xff);
  1196. } else {
  1197. /* GRR BUG? this assumes bpp == 24 & bits are packed low */
  1198. /* (probably need to use RShift, RMask, etc.) */
  1199. *dest++ = (char)((pixel >> 16) & 0xff);
  1200. *dest++ = (char)((pixel >> 8) & 0xff);
  1201. *dest++ = (char)( pixel & 0xff);
  1202. }
  1203. }
  1204. } else /* if (rpng2_info.channels == 4) */ {
  1205. for (i = rpng2_info.width; i > 0; --i) {
  1206. r = *src++;
  1207. g = *src++;
  1208. b = *src++;
  1209. a = *src++;
  1210. if (bg_image) {
  1211. bg_red = *src2++;
  1212. bg_green = *src2++;
  1213. bg_blue = *src2++;
  1214. }
  1215. if (a == 255) {
  1216. red = r;
  1217. green = g;
  1218. blue = b;
  1219. } else if (a == 0) {
  1220. red = bg_red;
  1221. green = bg_green;
  1222. blue = bg_blue;
  1223. } else {
  1224. /* this macro (from png.h) composites the foreground
  1225. * and background values and puts the result into the
  1226. * first argument */
  1227. alpha_composite(red, r, a, bg_red);
  1228. alpha_composite(green, g, a, bg_green);
  1229. alpha_composite(blue, b, a, bg_blue);
  1230. }
  1231. pixel = (red << RShift) |
  1232. (green << GShift) |
  1233. (blue << BShift);
  1234. /* recall that we set ximage->byte_order = MSBFirst above */
  1235. if (bpp == 32) {
  1236. *dest++ = (char)((pixel >> 24) & 0xff);
  1237. *dest++ = (char)((pixel >> 16) & 0xff);
  1238. *dest++ = (char)((pixel >> 8) & 0xff);
  1239. *dest++ = (char)( pixel & 0xff);
  1240. } else {
  1241. /* GRR BUG? this assumes bpp == 24 & bits are packed low */
  1242. /* (probably need to use RShift, RMask, etc.) */
  1243. *dest++ = (char)((pixel >> 16) & 0xff);
  1244. *dest++ = (char)((pixel >> 8) & 0xff);
  1245. *dest++ = (char)( pixel & 0xff);
  1246. }
  1247. }
  1248. }
  1249. } else if (depth == 16) {
  1250. ush red, green, blue;
  1251. src = rpng2_info.row_pointers[row];
  1252. if (bg_image)
  1253. src2 = bg_data + row*bg_rowbytes;
  1254. dest = ximage->data + row*ximage_rowbytes;
  1255. if (rpng2_info.channels == 3) {
  1256. for (i = rpng2_info.width; i > 0; --i) {
  1257. red = ((ush)(*src) << 8);
  1258. ++src;
  1259. green = ((ush)(*src) << 8);
  1260. ++src;
  1261. blue = ((ush)(*src) << 8);
  1262. ++src;
  1263. pixel = ((red >> RShift) & RMask) |
  1264. ((green >> GShift) & GMask) |
  1265. ((blue >> BShift) & BMask);
  1266. /* recall that we set ximage->byte_order = MSBFirst above */
  1267. *dest++ = (char)((pixel >> 8) & 0xff);
  1268. *dest++ = (char)( pixel & 0xff);
  1269. }
  1270. } else /* if (rpng2_info.channels == 4) */ {
  1271. for (i = rpng2_info.width; i > 0; --i) {
  1272. r = *src++;
  1273. g = *src++;
  1274. b = *src++;
  1275. a = *src++;
  1276. if (bg_image) {
  1277. bg_red = *src2++;
  1278. bg_green = *src2++;
  1279. bg_blue = *src2++;
  1280. }
  1281. if (a == 255) {
  1282. red = ((ush)r << 8);
  1283. green = ((ush)g << 8);
  1284. blue = ((ush)b << 8);
  1285. } else if (a == 0) {
  1286. red = ((ush)bg_red << 8);
  1287. green = ((ush)bg_green << 8);
  1288. blue = ((ush)bg_blue << 8);
  1289. } else {
  1290. /* this macro (from png.h) composites the foreground
  1291. * and background values and puts the result back into
  1292. * the first argument (== fg byte here: safe) */
  1293. alpha_composite(r, r, a, bg_red);
  1294. alpha_composite(g, g, a, bg_green);
  1295. alpha_composite(b, b, a, bg_blue);
  1296. red = ((ush)r << 8);
  1297. green = ((ush)g << 8);
  1298. blue = ((ush)b << 8);
  1299. }
  1300. pixel = ((red >> RShift) & RMask) |
  1301. ((green >> GShift) & GMask) |
  1302. ((blue >> BShift) & BMask);
  1303. /* recall that we set ximage->byte_order = MSBFirst above */
  1304. *dest++ = (char)((pixel >> 8) & 0xff);
  1305. *dest++ = (char)( pixel & 0xff);
  1306. }
  1307. }
  1308. } else /* depth == 8 */ {
  1309. /* GRR: add 8-bit support */
  1310. }
  1311. /*---------------------------------------------------------------------------
  1312. Display after every 16 rows or when on one of last two rows. (Region
  1313. may include previously displayed lines due to interlacing--i.e., not
  1314. contiguous. Also, second-to-last row is final one in interlaced images
  1315. with odd number of rows.) For demos, flush (and delay) after every 16th
  1316. row so "sparse" passes don't go twice as fast.
  1317. ---------------------------------------------------------------------------*/
  1318. if (demo_timing && (row - firstrow >= 16 || row >= rpng2_info.height-2)) {
  1319. XPutImage(display, window, gc, ximage, 0, (int)firstrow, 0,
  1320. (int)firstrow, rpng2_info.width, row - firstrow + 1);
  1321. XFlush(display);
  1322. rows = 0;
  1323. usleep(usleep_duration);
  1324. } else
  1325. if (!demo_timing && ((rows & 0xf) == 0 || row >= rpng2_info.height-2)) {
  1326. XPutImage(display, window, gc, ximage, 0, (int)firstrow, 0,
  1327. (int)firstrow, rpng2_info.width, row - firstrow + 1);
  1328. XFlush(display);
  1329. rows = 0;
  1330. }
  1331. }
  1332. static void rpng2_x_finish_display(void)
  1333. {
  1334. Trace((stderr, "beginning rpng2_x_finish_display()\n"))
  1335. /* last row has already been displayed by rpng2_x_display_row(), so we
  1336. * have nothing to do here except set a flag and let the user know that
  1337. * the image is done */
  1338. rpng2_info.state = kDone;
  1339. printf(
  1340. "Done. Press Q, Esc or mouse button 1 (within image window) to quit.\n");
  1341. fflush(stdout);
  1342. }
  1343. static void rpng2_x_redisplay_image(ulg startcol, ulg startrow,
  1344. ulg width, ulg height)
  1345. {
  1346. uch bg_red = rpng2_info.bg_red;
  1347. uch bg_green = rpng2_info.bg_green;
  1348. uch bg_blue = rpng2_info.bg_blue;
  1349. uch *src, *src2=NULL;
  1350. char *dest;
  1351. uch r, g, b, a;
  1352. ulg i, row, lastrow = 0;
  1353. ulg pixel;
  1354. int ximage_rowbytes = ximage->bytes_per_line;
  1355. Trace((stderr, "beginning display loop (image_channels == %d)\n",
  1356. rpng2_info.channels))
  1357. Trace((stderr, " (width = %ld, rowbytes = %d, ximage_rowbytes = %d)\n",
  1358. rpng2_info.width, rpng2_info.rowbytes, ximage_rowbytes))
  1359. Trace((stderr, " (bpp = %d)\n", ximage->bits_per_pixel))
  1360. Trace((stderr, " (byte_order = %s)\n", ximage->byte_order == MSBFirst?
  1361. "MSBFirst" : (ximage->byte_order == LSBFirst? "LSBFirst" : "unknown")))
  1362. /*---------------------------------------------------------------------------
  1363. Aside from the use of the rpng2_info struct and of src2 (for background
  1364. image), this routine is identical to rpng_x_display_image() in the non-
  1365. progressive version of the program--for the simple reason that redisplay
  1366. of the image against a new background happens after the image is fully
  1367. decoded and therefore is, by definition, non-progressive.
  1368. ---------------------------------------------------------------------------*/
  1369. if (depth == 24 || depth == 32) {
  1370. ulg red, green, blue;
  1371. int bpp = ximage->bits_per_pixel;
  1372. for (lastrow = row = startrow; row < startrow+height; ++row) {
  1373. src = rpng2_info.image_data + row*rpng2_info.rowbytes;
  1374. if (bg_image)
  1375. src2 = bg_data + row*bg_rowbytes;
  1376. dest = ximage->data + row*ximage_rowbytes;
  1377. if (rpng2_info.channels == 3) {
  1378. for (i = rpng2_info.width; i > 0; --i) {
  1379. red = *src++;
  1380. green = *src++;
  1381. blue = *src++;
  1382. #ifdef NO_24BIT_MASKS
  1383. pixel = (red << RShift) |
  1384. (green << GShift) |
  1385. (blue << BShift);
  1386. /* recall that we set ximage->byte_order = MSBFirst above */
  1387. if (bpp == 32) {
  1388. *dest++ = (char)((pixel >> 24) & 0xff);
  1389. *dest++ = (char)((pixel >> 16) & 0xff);
  1390. *dest++ = (char)((pixel >> 8) & 0xff);
  1391. *dest++ = (char)( pixel & 0xff);
  1392. } else {
  1393. /* this assumes bpp == 24 & bits are packed low */
  1394. /* (probably need to use RShift, RMask, etc.) */
  1395. *dest++ = (char)((pixel >> 16) & 0xff);
  1396. *dest++ = (char)((pixel >> 8) & 0xff);
  1397. *dest++ = (char)( pixel & 0xff);
  1398. }
  1399. #else
  1400. red = (RShift < 0)? red << (-RShift) : red >> RShift;
  1401. green = (GShift < 0)? green << (-GShift) : green >> GShift;
  1402. blue = (BShift < 0)? blue << (-BShift) : blue >> BShift;
  1403. pixel = (red & RMask) | (green & GMask) | (blue & BMask);
  1404. /* recall that we set ximage->byte_order = MSBFirst above */
  1405. if (bpp == 32) {
  1406. *dest++ = (char)((pixel >> 24) & 0xff);
  1407. *dest++ = (char)((pixel >> 16) & 0xff);
  1408. *dest++ = (char)((pixel >> 8) & 0xff);
  1409. *dest++ = (char)( pixel & 0xff);
  1410. } else {
  1411. /* GRR BUG */
  1412. /* this assumes bpp == 24 & bits are packed low */
  1413. /* (probably need to use RShift/RMask/etc. here, too) */
  1414. *dest++ = (char)((pixel >> 16) & 0xff);
  1415. *dest++ = (char)((pixel >> 8) & 0xff);
  1416. *dest++ = (char)( pixel & 0xff);
  1417. }
  1418. #endif
  1419. }
  1420. } else /* if (rpng2_info.channels == 4) */ {
  1421. for (i = rpng2_info.width; i > 0; --i) {
  1422. r = *src++;
  1423. g = *src++;
  1424. b = *src++;
  1425. a = *src++;
  1426. if (bg_image) {
  1427. bg_red = *src2++;
  1428. bg_green = *src2++;
  1429. bg_blue = *src2++;
  1430. }
  1431. if (a == 255) {
  1432. red = r;
  1433. green = g;
  1434. blue = b;
  1435. } else if (a == 0) {
  1436. red = bg_red;
  1437. green = bg_green;
  1438. blue = bg_blue;
  1439. } else {
  1440. /* this macro (from png.h) composites the foreground
  1441. * and background values and puts the result into the
  1442. * first argument */
  1443. alpha_composite(red, r, a, bg_red);
  1444. alpha_composite(green, g, a, bg_green);
  1445. alpha_composite(blue, b, a, bg_blue);
  1446. }
  1447. #ifdef NO_24BIT_MASKS
  1448. pixel = (red << RShift) |
  1449. (green << GShift) |
  1450. (blue << BShift);
  1451. /* recall that we set ximage->byte_order = MSBFirst above */
  1452. if (bpp == 32) {
  1453. *dest++ = (char)((pixel >> 24) & 0xff);
  1454. *dest++ = (char)((pixel >> 16) & 0xff);
  1455. *dest++ = (char)((pixel >> 8) & 0xff);
  1456. *dest++ = (char)( pixel & 0xff);
  1457. } else {
  1458. /* this assumes bpp == 24 & bits are packed low */
  1459. /* (probably need to use RShift, RMask, etc.) */
  1460. *dest++ = (char)((pixel >> 16) & 0xff);
  1461. *dest++ = (char)((pixel >> 8) & 0xff);
  1462. *dest++ = (char)( pixel & 0xff);
  1463. }
  1464. #else
  1465. red = (RShift < 0)? red << (-RShift) : red >> RShift;
  1466. green = (GShift < 0)? green << (-GShift) : green >> GShift;
  1467. blue = (BShift < 0)? blue << (-BShift) : blue >> BShift;
  1468. pixel = (red & RMask) | (green & GMask) | (blue & BMask);
  1469. /* recall that we set ximage->byte_order = MSBFirst above */
  1470. if (bpp == 32) {
  1471. *dest++ = (char)((pixel >> 24) & 0xff);
  1472. *dest++ = (char)((pixel >> 16) & 0xff);
  1473. *dest++ = (char)((pixel >> 8) & 0xff);
  1474. *dest++ = (char)( pixel & 0xff);
  1475. } else {
  1476. /* GRR BUG */
  1477. /* this assumes bpp == 24 & bits are packed low */
  1478. /* (probably need to use RShift/RMask/etc. here, too) */
  1479. *dest++ = (char)((pixel >> 16) & 0xff);
  1480. *dest++ = (char)((pixel >> 8) & 0xff);
  1481. *dest++ = (char)( pixel & 0xff);
  1482. }
  1483. #endif
  1484. }
  1485. }
  1486. /* display after every 16 lines */
  1487. if (((row+1) & 0xf) == 0) {
  1488. XPutImage(display, window, gc, ximage, 0, (int)lastrow, 0,
  1489. (int)lastrow, rpng2_info.width, 16);
  1490. XFlush(display);
  1491. lastrow = row + 1;
  1492. }
  1493. }
  1494. } else if (depth == 16) {
  1495. ush red, green, blue;
  1496. for (lastrow = row = startrow; row < startrow+height; ++row) {
  1497. src = rpng2_info.row_pointers[row];
  1498. if (bg_image)
  1499. src2 = bg_data + row*bg_rowbytes;
  1500. dest = ximage->data + row*ximage_rowbytes;
  1501. if (rpng2_info.channels == 3) {
  1502. for (i = rpng2_info.width; i > 0; --i) {
  1503. red = ((ush)(*src) << 8);
  1504. ++src;
  1505. green = ((ush)(*src) << 8);
  1506. ++src;
  1507. blue = ((ush)(*src) << 8);
  1508. ++src;
  1509. pixel = ((red >> RShift) & RMask) |
  1510. ((green >> GShift) & GMask) |
  1511. ((blue >> BShift) & BMask);
  1512. /* recall that we set ximage->byte_order = MSBFirst above */
  1513. *dest++ = (char)((pixel >> 8) & 0xff);
  1514. *dest++ = (char)( pixel & 0xff);
  1515. }
  1516. } else /* if (rpng2_info.channels == 4) */ {
  1517. for (i = rpng2_info.width; i > 0; --i) {
  1518. r = *src++;
  1519. g = *src++;
  1520. b = *src++;
  1521. a = *src++;
  1522. if (bg_image) {
  1523. bg_red = *src2++;
  1524. bg_green = *src2++;
  1525. bg_blue = *src2++;
  1526. }
  1527. if (a == 255) {
  1528. red = ((ush)r << 8);
  1529. green = ((ush)g << 8);
  1530. blue = ((ush)b << 8);
  1531. } else if (a == 0) {
  1532. red = ((ush)bg_red << 8);
  1533. green = ((ush)bg_green << 8);
  1534. blue = ((ush)bg_blue << 8);
  1535. } else {
  1536. /* this macro (from png.h) composites the foreground
  1537. * and background values and puts the result back into
  1538. * the first argument (== fg byte here: safe) */
  1539. alpha_composite(r, r, a, bg_red);
  1540. alpha_composite(g, g, a, bg_green);
  1541. alpha_composite(b, b, a, bg_blue);
  1542. red = ((ush)r << 8);
  1543. green = ((ush)g << 8);
  1544. blue = ((ush)b << 8);
  1545. }
  1546. pixel = ((red >> RShift) & RMask) |
  1547. ((green >> GShift) & GMask) |
  1548. ((blue >> BShift) & BMask);
  1549. /* recall that we set ximage->byte_order = MSBFirst above */
  1550. *dest++ = (char)((pixel >> 8) & 0xff);
  1551. *dest++ = (char)( pixel & 0xff);
  1552. }
  1553. }
  1554. /* display after every 16 lines */
  1555. if (((row+1) & 0xf) == 0) {
  1556. XPutImage(display, window, gc, ximage, 0, (int)lastrow, 0,
  1557. (int)lastrow, rpng2_info.width, 16);
  1558. XFlush(display);
  1559. lastrow = row + 1;
  1560. }
  1561. }
  1562. } else /* depth == 8 */ {
  1563. /* GRR: add 8-bit support */
  1564. }
  1565. Trace((stderr, "calling final XPutImage()\n"))
  1566. if (lastrow < startrow+height) {
  1567. XPutImage(display, window, gc, ximage, 0, (int)lastrow, 0,
  1568. (int)lastrow, rpng2_info.width, rpng2_info.height-lastrow);
  1569. XFlush(display);
  1570. }
  1571. } /* end function rpng2_x_redisplay_image() */
  1572. #ifdef FEATURE_LOOP
  1573. static void rpng2_x_reload_bg_image(void)
  1574. {
  1575. char *dest;
  1576. uch r1, r2, g1, g2, b1, b2;
  1577. uch r1_inv, r2_inv, g1_inv, g2_inv, b1_inv, b2_inv;
  1578. int k, hmax, max;
  1579. int xidx, yidx, yidx_max;
  1580. int even_odd_vert, even_odd_horiz, even_odd;
  1581. int invert_gradient2 = (bg[pat].type & 0x08);
  1582. int invert_column;
  1583. ulg i, row;
  1584. bgscale = (pat == 0)? 8 : bgscale_default;
  1585. yidx_max = bgscale - 1;
  1586. /*---------------------------------------------------------------------------
  1587. Vertical gradients (ramps) in NxN squares, alternating direction and
  1588. colors (N == bgscale).
  1589. ---------------------------------------------------------------------------*/
  1590. if ((bg[pat].type & 0x07) == 0) {
  1591. uch r1_min = rgb[bg[pat].rgb1_min].r;
  1592. uch g1_min = rgb[bg[pat].rgb1_min].g;
  1593. uch b1_min = rgb[bg[pat].rgb1_min].b;
  1594. uch r2_min = rgb[bg[pat].rgb2_min].r;
  1595. uch g2_min = rgb[bg[pat].rgb2_min].g;
  1596. uch b2_min = rgb[bg[pat].rgb2_min].b;
  1597. int r1_diff = rgb[bg[pat].rgb1_max].r - r1_min;
  1598. int g1_diff = rgb[bg[pat].rgb1_max].g - g1_min;
  1599. int b1_diff = rgb[bg[pat].rgb1_max].b - b1_min;
  1600. int r2_diff = rgb[bg[pat].rgb2_max].r - r2_min;
  1601. int g2_diff = rgb[bg[pat].rgb2_max].g - g2_min;
  1602. int b2_diff = rgb[bg[pat].rgb2_max].b - b2_min;
  1603. for (row = 0; row < rpng2_info.height; ++row) {
  1604. yidx = (int)(row % bgscale);
  1605. even_odd_vert = (int)((row / bgscale) & 1);
  1606. r1 = r1_min + (r1_diff * yidx) / yidx_max;
  1607. g1 = g1_min + (g1_diff * yidx) / yidx_max;
  1608. b1 = b1_min + (b1_diff * yidx) / yidx_max;
  1609. r1_inv = r1_min + (r1_diff * (yidx_max-yidx)) / yidx_max;
  1610. g1_inv = g1_min + (g1_diff * (yidx_max-yidx)) / yidx_max;
  1611. b1_inv = b1_min + (b1_diff * (yidx_max-yidx)) / yidx_max;
  1612. r2 = r2_min + (r2_diff * yidx) / yidx_max;
  1613. g2 = g2_min + (g2_diff * yidx) / yidx_max;
  1614. b2 = b2_min + (b2_diff * yidx) / yidx_max;
  1615. r2_inv = r2_min + (r2_diff * (yidx_max-yidx)) / yidx_max;
  1616. g2_inv = g2_min + (g2_diff * (yidx_max-yidx)) / yidx_max;
  1617. b2_inv = b2_min + (b2_diff * (yidx_max-yidx)) / yidx_max;
  1618. dest = (char *)bg_data + row*bg_rowbytes;
  1619. for (i = 0; i < rpng2_info.width; ++i) {
  1620. even_odd_horiz = (int)((i / bgscale) & 1);
  1621. even_odd = even_odd_vert ^ even_odd_horiz;
  1622. invert_column =
  1623. (even_odd_horiz && (bg[pat].type & 0x10));
  1624. if (even_odd == 0) { /* gradient #1 */
  1625. if (invert_column) {
  1626. *dest++ = r1_inv;
  1627. *dest++ = g1_inv;
  1628. *dest++ = b1_inv;
  1629. } else {
  1630. *dest++ = r1;
  1631. *dest++ = g1;
  1632. *dest++ = b1;
  1633. }
  1634. } else { /* gradient #2 */
  1635. if ((invert_column && invert_gradient2) ||
  1636. (!invert_column && !invert_gradient2))
  1637. {
  1638. *dest++ = r2; /* not inverted or */
  1639. *dest++ = g2; /* doubly inverted */
  1640. *dest++ = b2;
  1641. } else {
  1642. *dest++ = r2_inv;
  1643. *dest++ = g2_inv; /* singly inverted */
  1644. *dest++ = b2_inv;
  1645. }
  1646. }
  1647. }
  1648. }
  1649. /*---------------------------------------------------------------------------
  1650. Soft gradient-diamonds with scale = bgscale. Code contributed by Adam
  1651. M. Costello.
  1652. ---------------------------------------------------------------------------*/
  1653. } else if ((bg[pat].type & 0x07) == 1) {
  1654. hmax = (bgscale-1)/2; /* half the max weight of a color */
  1655. max = 2*hmax; /* the max weight of a color */
  1656. r1 = rgb[bg[pat].rgb1_max].r;
  1657. g1 = rgb[bg[pat].rgb1_max].g;
  1658. b1 = rgb[bg[pat].rgb1_max].b;
  1659. r2 = rgb[bg[pat].rgb2_max].r;
  1660. g2 = rgb[bg[pat].rgb2_max].g;
  1661. b2 = rgb[bg[pat].rgb2_max].b;
  1662. for (row = 0; row < rpng2_info.height; ++row) {
  1663. yidx = (int)(row % bgscale);
  1664. if (yidx > hmax)
  1665. yidx = bgscale-1 - yidx;
  1666. dest = (char *)bg_data + row*bg_rowbytes;
  1667. for (i = 0; i < rpng2_info.width; ++i) {
  1668. xidx = (int)(i % bgscale);
  1669. if (xidx > hmax)
  1670. xidx = bgscale-1 - xidx;
  1671. k = xidx + yidx;
  1672. *dest++ = (k*r1 + (max-k)*r2) / max;
  1673. *dest++ = (k*g1 + (max-k)*g2) / max;
  1674. *dest++ = (k*b1 + (max-k)*b2) / max;
  1675. }
  1676. }
  1677. /*---------------------------------------------------------------------------
  1678. Radial "starburst" with azimuthal sinusoids; [eventually number of sinu-
  1679. soids will equal bgscale?]. This one is slow but very cool. Code con-
  1680. tributed by Pieter S. van der Meulen (originally in Smalltalk).
  1681. ---------------------------------------------------------------------------*/
  1682. } else if ((bg[pat].type & 0x07) == 2) {
  1683. uch ch;
  1684. int ii, x, y, hw, hh, grayspot;
  1685. double freq, rotate, saturate, gray, intensity;
  1686. double angle=0.0, aoffset=0.0, maxDist, dist;
  1687. double red=0.0, green=0.0, blue=0.0, hue, s, v, f, p, q, t;
  1688. hh = (int)(rpng2_info.height / 2);
  1689. hw = (int)(rpng2_info.width / 2);
  1690. /* variables for radial waves:
  1691. * aoffset: number of degrees to rotate hue [CURRENTLY NOT USED]
  1692. * freq: number of color beams originating from the center
  1693. * grayspot: size of the graying center area (anti-alias)
  1694. * rotate: rotation of the beams as a function of radius
  1695. * saturate: saturation of beams' shape azimuthally
  1696. */
  1697. angle = CLIP(angle, 0.0, 360.0);
  1698. grayspot = CLIP(bg[pat].bg_gray, 1, (hh + hw));
  1699. freq = MAX((double)bg[pat].bg_freq, 0.0);
  1700. saturate = (double)bg[pat].bg_bsat * 0.1;
  1701. rotate = (double)bg[pat].bg_brot * 0.1;
  1702. gray = 0.0;
  1703. intensity = 0.0;
  1704. maxDist = (double)((hw*hw) + (hh*hh));
  1705. for (row = 0; row < rpng2_info.height; ++row) {
  1706. y = (int)(row - hh);
  1707. dest = (char *)bg_data + row*bg_rowbytes;
  1708. for (i = 0; i < rpng2_info.width; ++i) {
  1709. x = (int)(i - hw);
  1710. angle = (x == 0)? PI_2 : atan((double)y / (double)x);
  1711. gray = (double)MAX(ABS(y), ABS(x)) / grayspot;
  1712. gray = MIN(1.0, gray);
  1713. dist = (double)((x*x) + (y*y)) / maxDist;
  1714. intensity = cos((angle+(rotate*dist*PI)) * freq) *
  1715. gray * saturate;
  1716. intensity = (MAX(MIN(intensity,1.0),-1.0) + 1.0) * 0.5;
  1717. hue = (angle + PI) * INV_PI_360 + aoffset;
  1718. s = gray * ((double)(ABS(x)+ABS(y)) / (double)(hw + hh));
  1719. s = MIN(MAX(s,0.0), 1.0);
  1720. v = MIN(MAX(intensity,0.0), 1.0);
  1721. if (s == 0.0) {
  1722. ch = (uch)(v * 255.0);
  1723. *dest++ = ch;
  1724. *dest++ = ch;
  1725. *dest++ = ch;
  1726. } else {
  1727. if ((hue < 0.0) || (hue >= 360.0))
  1728. hue -= (((int)(hue / 360.0)) * 360.0);
  1729. hue /= 60.0;
  1730. ii = (int)hue;
  1731. f = hue - (double)ii;
  1732. p = (1.0 - s) * v;
  1733. q = (1.0 - (s * f)) * v;
  1734. t = (1.0 - (s * (1.0 - f))) * v;
  1735. if (ii == 0) { red = v; green = t; blue = p; }
  1736. else if (ii == 1) { red = q; green = v; blue = p; }
  1737. else if (ii == 2) { red = p; green = v; blue = t; }
  1738. else if (ii == 3) { red = p; green = q; blue = v; }
  1739. else if (ii == 4) { red = t; green = p; blue = v; }
  1740. else if (ii == 5) { red = v; green = p; blue = q; }
  1741. *dest++ = (uch)(red * 255.0);
  1742. *dest++ = (uch)(green * 255.0);
  1743. *dest++ = (uch)(blue * 255.0);
  1744. }
  1745. }
  1746. }
  1747. }
  1748. } /* end function rpng2_x_reload_bg_image() */
  1749. static int is_number(char *p)
  1750. {
  1751. while (*p) {
  1752. if (!isdigit(*p))
  1753. return FALSE;
  1754. ++p;
  1755. }
  1756. return TRUE;
  1757. }
  1758. #endif /* FEATURE_LOOP */
  1759. static void rpng2_x_cleanup(void)
  1760. {
  1761. if (bg_image && bg_data) {
  1762. free(bg_data);
  1763. bg_data = NULL;
  1764. }
  1765. if (rpng2_info.image_data) {
  1766. free(rpng2_info.image_data);
  1767. rpng2_info.image_data = NULL;
  1768. }
  1769. if (rpng2_info.row_pointers) {
  1770. free(rpng2_info.row_pointers);
  1771. rpng2_info.row_pointers = NULL;
  1772. }
  1773. if (ximage) {
  1774. if (ximage->data) {
  1775. free(ximage->data); /* we allocated it, so we free it */
  1776. ximage->data = (char *)NULL; /* instead of XDestroyImage() */
  1777. }
  1778. XDestroyImage(ximage);
  1779. ximage = NULL;
  1780. }
  1781. if (have_gc)
  1782. XFreeGC(display, gc);
  1783. if (have_window)
  1784. XDestroyWindow(display, window);
  1785. if (have_colormap)
  1786. XFreeColormap(display, colormap);
  1787. if (have_nondefault_visual)
  1788. XFree(visual_list);
  1789. }
  1790. static int rpng2_x_msb(ulg u32val)
  1791. {
  1792. int i;
  1793. for (i = 31; i >= 0; --i) {
  1794. if (u32val & 0x80000000L)
  1795. break;
  1796. u32val <<= 1;
  1797. }
  1798. return i;
  1799. }