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