rpng2-x.c 80 KB

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