#define PROGNAME "rpng2-x"
#define LONGNAME "Progressive PNG Viewer for X"
#define VERSION "2.03 of 25 February 2010"
#define RESNAME "rpng2"
#define RESCLASS "Rpng"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <setjmp.h>
#include <time.h>
#include <math.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/keysym.h>
#ifdef VMS
# include <unistd.h>
#endif
#ifndef PI
# define PI 3.141592653589793238
#endif
#define PI_2 (PI*0.5)
#define INV_PI_360 (360.0 / PI)
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define CLIP(a,min,max) MAX(min,MIN((a),max))
#define ABS(a) ((a)<0?-(a):(a))
#define CLIP8P(c) MAX(0,(MIN((c),255)))
#define ROUNDF(f) ((int)(f + 0.5))
#define QUIT(e,k) ((e.type == ButtonPress && e.xbutton.button == Button1) || \
(e.type == KeyPress && \
((k = XLookupKeysym(&e.xkey, 0)) == XK_q || k == XK_Escape)))
#define NO_24BIT_MASKS
#define rgb1_max bg_freq
#define rgb1_min bg_gray
#define rgb2_max bg_bsat
#define rgb2_min bg_brot
#include "readpng2.h"
#define alpha_composite(composite, fg, alpha, bg) { \
ush temp = ((ush)(fg)*(ush)(alpha) + \
(ush)(bg)*(ush)(255 - (ush)(alpha)) + (ush)128); \
(composite) = (uch)((temp + (temp >> 8)) >> 8); \
}
#define INBUFSIZE 4096 * block size corresponds roughly to a download
* speed 10% faster than theoretical 33.6K maximum
* (assuming 8 data bits, 1 stop bit and no other
* overhead) */
static void rpng2_x_init (void);
static int rpng2_x_create_window (void);
static int rpng2_x_load_bg_image (void);
static void rpng2_x_display_row (ulg row);
static void rpng2_x_finish_display (void);
static void rpng2_x_redisplay_image (ulg startcol, ulg startrow,
ulg width, ulg height);
#ifdef FEATURE_LOOP
static void rpng2_x_reload_bg_image (void);
static int is_number (char *p);
#endif
static void rpng2_x_cleanup (void);
static int rpng2_x_msb (ulg u32val);
static char titlebar[1024], *window_name = titlebar;
static char *appname = LONGNAME;
static char *icon_name = PROGNAME;
static char *res_name = RESNAME;
static char *res_class = RESCLASS;
static char *filename;
static FILE *infile;
static mainprog_info rpng2_info;
static uch inbuf[INBUFSIZE];
static int incount;
static int pat = 6;
static int bg_image = 0;
static int bgscale, bgscale_default = 16;
static ulg bg_rowbytes;
static uch *bg_data;
int pause_after_pass = FALSE;
int demo_timing = FALSE;
ulg usleep_duration = 0L;
static struct rgb_color {
uch r, g, b;
} rgb[] = {
{ 0, 0, 0},
{255, 255, 255},
{173, 132, 57},
{ 64, 132, 0},
{189, 117, 1},
{253, 249, 1},
{ 0, 0, 255},
{ 0, 0, 120},
{255, 0, 255},
{ 64, 0, 64},
{255, 0, 0},
{ 64, 0, 0},
{255, 127, 0},
{192, 96, 0},
{ 24, 60, 0},
{ 85, 125, 200},
{192, 192, 192}
};
static struct background_pattern {
ush type;
int rgb1_max, rgb1_min;
int rgb2_max, rgb2_min;
} bg[] = {
{0, 1,1, 16,16},
{0+8, 2,0, 1,15},
{0+24, 2,0, 1,0},
{0+8, 4,5, 0,2},
{0+8, 4,5, 0,6},
{0, 7,0, 8,9},
{0+8, 13,0, 5,14},
{0+8, 12,0, 10,11},
{1, 7,0, 8,0},
{1, 12,0, 11,0},
{1, 10,0, 7,0},
{1, 4,0, 5,0},
{1, 3,0, 0,0},
{2, 16, 100, 20, 0},
{2, 18, 100, 10, 2},
{2, 16, 256, 100, 250},
{2, 10000, 256, 11, 0}
};
static int num_bgpat = sizeof(bg) / sizeof(struct background_pattern);
static char *displayname;
static XImage *ximage;
static Display *display;
static int depth;
static Visual *visual;
static XVisualInfo *visual_list;
static int RShift, GShift, BShift;
static ulg RMask, GMask, BMask;
static Window window;
static GC gc;
static Colormap colormap;
static int have_nondefault_visual = FALSE;
static int have_colormap = FALSE;
static int have_window = FALSE;
static int have_gc = FALSE;
int main(int argc, char **argv)
{
#ifdef sgi
char tmpline[80];
#endif
char *p, *bgstr = NULL;
int rc, alen, flen;
int error = 0;
int timing = FALSE;
int have_bg = FALSE;
#ifdef FEATURE_LOOP
int loop = FALSE;
long loop_interval = -1;
#endif
double LUT_exponent;
double CRT_exponent = 2.2;
double default_display_exponent;
XEvent e;
KeySym k;
displayname = (char *)NULL;
filename = (char *)NULL;
memset(&rpng2_info, 0, sizeof(mainprog_info));
#if defined(NeXT)
LUT_exponent = 1.0 / 2.2;
#elif defined(sgi)
LUT_exponent = 1.0 / 1.7;
infile = fopen("/etc/config/system.glGammaVal", "r");
if (infile) {
double sgi_gamma;
fgets(tmpline, 80, infile);
fclose(infile);
sgi_gamma = atof(tmpline);
if (sgi_gamma > 0.0)
LUT_exponent = 1.0 / sgi_gamma;
}
#elif defined(Macintosh)
LUT_exponent = 1.8 / 2.61;
#else
LUT_exponent = 1.0;
#endif
default_display_exponent = LUT_exponent * CRT_exponent;
if ((p = getenv("SCREEN_GAMMA")) != NULL)
rpng2_info.display_exponent = atof(p);
else
rpng2_info.display_exponent = default_display_exponent;
while (*++argv && !error) {
if (!strncmp(*argv, "-display", 2)) {
if (!*++argv)
++error;
else
displayname = *argv;
} else if (!strncmp(*argv, "-gamma", 2)) {
if (!*++argv)
++error;
else {
rpng2_info.display_exponent = atof(*argv);
if (rpng2_info.display_exponent <= 0.0)
++error;
}
} else if (!strncmp(*argv, "-bgcolor", 4)) {
if (!*++argv)
++error;
else {
bgstr = *argv;
if (strlen(bgstr) != 7 || bgstr[0] != '#')
++error;
else {
have_bg = TRUE;
bg_image = FALSE;
}
}
} else if (!strncmp(*argv, "-bgpat", 4)) {
if (!*++argv)
++error;
else {
pat = atoi(*argv);
if (pat >= 0 && pat < num_bgpat) {
bg_image = TRUE;
have_bg = FALSE;
} else
++error;
}
} else if (!strncmp(*argv, "-usleep", 2)) {
if (!*++argv)
++error;
else {
usleep_duration = (ulg)atol(*argv);
demo_timing = TRUE;
}
} else if (!strncmp(*argv, "-pause", 2)) {
pause_after_pass = TRUE;
} else if (!strncmp(*argv, "-timing", 2)) {
timing = TRUE;
#ifdef FEATURE_LOOP
} else if (!strncmp(*argv, "-loop", 2)) {
loop = TRUE;
if (!argv[1] || !is_number(argv[1]))
loop_interval = 2;
else {
++argv;
loop_interval = atol(*argv);
if (loop_interval < 0)
loop_interval = 2;
else if (loop_interval > 100000)
loop_interval = 100000;
}
#endif
} else {
if (**argv != '-') {
filename = *argv;
if (argv[1])
++error;
} else
++error;
}
}
if (!filename)
++error;
if (error) {
fprintf(stderr, "\n%s %s: %s\n\n", PROGNAME, VERSION, appname);
readpng2_version_info();
fprintf(stderr, "\n"
"Usage: %s [-display xdpy] [-gamma exp] [-bgcolor bg | -bgpat pat]\n"
#ifdef FEATURE_LOOP
" %*s [-usleep dur | -timing] [-pause] [-loop [sec]] file.png\n\n"
#else
" %*s [-usleep dur | -timing] [-pause] file.png\n\n"
#endif
" xdpy\tname of the target X display (e.g., ``hostname:0'')\n"
" exp \ttransfer-function exponent (``gamma'') of the display\n"
"\t\t system in floating-point format (e.g., ``%.1f''); equal\n"
"\t\t to the product of the lookup-table exponent (varies)\n"
"\t\t and the CRT exponent (usually 2.2); must be positive\n"
" bg \tdesired background color in 7-character hex RGB format\n"
"\t\t (e.g., ``#ff7700'' for orange: same as HTML colors);\n"
"\t\t used with transparent images; overrides -bgpat\n"
" pat \tdesired background pattern number (0-%d); used with\n"
"\t\t transparent images; overrides -bgcolor\n"
#ifdef FEATURE_LOOP
" -loop\tloops through background images after initial display\n"
"\t\t is complete (depends on -bgpat)\n"
" sec \tseconds to display each background image (default = 2)\n"
#endif
" dur \tduration in microseconds to wait after displaying each\n"
"\t\t row (for demo purposes)\n"
" -timing\tenables delay for every block read, to simulate modem\n"
"\t\t download of image (~36 Kbps)\n"
" -pause\tpauses after displaying each pass until mouse clicked\n"
"\nPress Q, Esc or mouse button 1 (within image window, after image\n"
"is displayed) to quit.\n"
"\n", PROGNAME,
#if (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__))
(int)strlen(PROGNAME), " ",
#endif
(int)strlen(PROGNAME), " ", default_display_exponent, num_bgpat-1);
exit(1);
}
if (!(infile = fopen(filename, "rb"))) {
fprintf(stderr, PROGNAME ": can't open PNG file [%s]\n", filename);
++error;
} else {
incount = fread(inbuf, 1, INBUFSIZE, infile);
if (incount < 8 || !readpng2_check_sig(inbuf, 8)) {
fprintf(stderr, PROGNAME
": [%s] is not a PNG file: incorrect signature\n",
filename);
++error;
} else if ((rc = readpng2_init(&rpng2_info)) != 0) {
switch (rc) {
case 2:
fprintf(stderr, PROGNAME
": [%s] has bad IHDR (libpng longjmp)\n", filename);
break;
case 4:
fprintf(stderr, PROGNAME ": insufficient memory\n");
break;
default:
fprintf(stderr, PROGNAME
": unknown readpng2_init() error\n");
break;
}
++error;
} else {
Trace((stderr, "about to call XOpenDisplay()\n"))
display = XOpenDisplay(displayname);
if (!display) {
readpng2_cleanup(&rpng2_info);
fprintf(stderr, PROGNAME ": can't open X display [%s]\n",
displayname? displayname : "default");
++error;
}
}
if (error)
fclose(infile);
}
if (error) {
fprintf(stderr, PROGNAME ": aborting.\n");
exit(2);
}
alen = strlen(appname);
flen = strlen(filename);
if (alen + flen + 3 > 1023)
sprintf(titlebar, "%s: ...%s", appname, filename+(alen+flen+6-1023));
else
sprintf(titlebar, "%s: %s", appname, filename);
if (have_bg) {
unsigned r, g, b;
sscanf(bgstr+1, "%2x%2x%2x", &r, &g, &b);
rpng2_info.bg_red = (uch)r;
rpng2_info.bg_green = (uch)g;
rpng2_info.bg_blue = (uch)b;
} else
rpng2_info.need_bgcolor = TRUE;
rpng2_info.state = kPreInit;
rpng2_info.mainprog_init = rpng2_x_init;
rpng2_info.mainprog_display_row = rpng2_x_display_row;
rpng2_info.mainprog_finish_display = rpng2_x_finish_display;
for (;;) {
Trace((stderr, "about to call readpng2_decode_data()\n"))
if (readpng2_decode_data(&rpng2_info, inbuf, incount))
++error;
Trace((stderr, "done with readpng2_decode_data()\n"))
if (error || incount != INBUFSIZE || rpng2_info.state == kDone) {
if (rpng2_info.state == kDone) {
Trace((stderr, "done decoding PNG image\n"))
} else if (ferror(infile)) {
fprintf(stderr, PROGNAME
": error while reading PNG image file\n");
exit(3);
} else if (feof(infile)) {
fprintf(stderr, PROGNAME ": end of file reached "
"(unexpectedly) while reading PNG image file\n");
exit(3);
} else {
}
break;
}
if (timing)
sleep(1);
incount = fread(inbuf, 1, INBUFSIZE, infile);
}
fclose(infile);
Trace((stderr, "about to call readpng2_cleanup()\n"))
readpng2_cleanup(&rpng2_info);
if (error) {
fprintf(stderr, PROGNAME ": libpng error while decoding PNG image\n");
exit(3);
}
#ifdef FEATURE_LOOP
if (loop && bg_image) {
Trace((stderr, "entering -loop loop (FEATURE_LOOP)\n"))
for (;;) {
int i, use_sleep;
struct timeval now, then;
if (gettimeofday(&then, NULL) == 0) {
then.tv_sec += loop_interval;
use_sleep = FALSE;
} else
use_sleep = TRUE;
if (XCheckMaskEvent(display, KeyPressMask | ButtonPressMask, &e))
if (QUIT(e,k))
break;
if (++pat >= num_bgpat)
pat = 0;
rpng2_x_reload_bg_image();
if (use_sleep || gettimeofday(&now, NULL) != 0) {
for (i = loop_interval; i > 0; --i) {
sleep(1);
if (XCheckMaskEvent(display, KeyPressMask | ButtonPressMask,
&e) && QUIT(e,k))
break;
}
} else {
if (now.tv_sec < then.tv_sec ||
(now.tv_sec == then.tv_sec && now.tv_usec < then.tv_usec))
{
int quit = FALSE;
long seconds_to_go = then.tv_sec - now.tv_sec;
long usleep_usec;
while (seconds_to_go-- > 1) {
int seconds_done = 0;
for (i = seconds_to_go; i > 0 && !quit; --i) {
sleep(1);
if (XCheckMaskEvent(display, KeyPressMask |
ButtonPressMask, &e) && QUIT(e,k))
quit = TRUE;
if (++seconds_done > 1000)
break;
}
if (quit)
break;
if (gettimeofday(&now, NULL) == 0) {
if (now.tv_sec >= then.tv_sec)
break;
seconds_to_go = then.tv_sec - now.tv_sec;
} else
++seconds_to_go;
}
if (quit)
break;
if (gettimeofday(&now, NULL) == 0) {
usleep_usec = 1000000L*(then.tv_sec - now.tv_sec) +
then.tv_usec - now.tv_usec;
if (usleep_usec > 0)
usleep((ulg)usleep_usec);
}
}
}
rpng2_x_redisplay_image (0, 0, rpng2_info.width, rpng2_info.height);
}
} else
#endif
if (rpng2_info.state >= kWindowInit) {
Trace((stderr, "entering final wait-for-quit-event loop\n"))
do {
XNextEvent(display, &e);
if (e.type == Expose) {
XExposeEvent *ex = (XExposeEvent *)&e;
rpng2_x_redisplay_image (ex->x, ex->y, ex->width, ex->height);
}
} while (!QUIT(e,k));
} else {
fprintf(stderr, PROGNAME ": init callback never called: probable "
"libpng error while decoding PNG metadata\n");
exit(4);
}
Trace((stderr, "about to call rpng2_x_cleanup()\n"))
rpng2_x_cleanup();
return 0;
}
static void rpng2_x_init(void)
{
ulg i;
ulg rowbytes = rpng2_info.rowbytes;
Trace((stderr, "beginning rpng2_x_init()\n"))
Trace((stderr, " rowbytes = %d\n", rpng2_info.rowbytes))
Trace((stderr, " width = %ld\n", rpng2_info.width))
Trace((stderr, " height = %ld\n", rpng2_info.height))
rpng2_info.image_data = (uch *)malloc(rowbytes * rpng2_info.height);
if (!rpng2_info.image_data) {
readpng2_cleanup(&rpng2_info);
return;
}
rpng2_info.row_pointers = (uch **)malloc(rpng2_info.height * sizeof(uch *));
if (!rpng2_info.row_pointers) {
free(rpng2_info.image_data);
rpng2_info.image_data = NULL;
readpng2_cleanup(&rpng2_info);
return;
}
for (i = 0; i < rpng2_info.height; ++i)
rpng2_info.row_pointers[i] = rpng2_info.image_data + i*rowbytes;
if (rpng2_x_create_window()) {
readpng2_cleanup(&rpng2_info);
rpng2_x_cleanup();
exit(2);
}
rpng2_info.state = kWindowInit;
}
static int rpng2_x_create_window(void)
{
ulg bg_red = rpng2_info.bg_red;
ulg bg_green = rpng2_info.bg_green;
ulg bg_blue = rpng2_info.bg_blue;
ulg bg_pixel = 0L;
ulg attrmask;
int need_colormap = FALSE;
int screen, pad;
uch *xdata;
Window root;
XEvent e;
XGCValues gcvalues;
XSetWindowAttributes attr;
XTextProperty windowName, *pWindowName = &windowName;
XTextProperty iconName, *pIconName = &iconName;
XVisualInfo visual_info;
XSizeHints *size_hints;
XWMHints *wm_hints;
XClassHint *class_hints;
Trace((stderr, "beginning rpng2_x_create_window()\n"))
screen = DefaultScreen(display);
depth = DisplayPlanes(display, screen);
root = RootWindow(display, screen);
#ifdef DEBUG
XSynchronize(display, True);
#endif
if (depth != 16 && depth != 24 && depth != 32) {
int visuals_matched = 0;
Trace((stderr, "default depth is %d: checking other visuals\n",
depth))
visual_info.screen = screen;
visual_info.depth = 24;
visual_list = XGetVisualInfo(display,
VisualScreenMask | VisualDepthMask, &visual_info, &visuals_matched);
if (visuals_matched == 0) {
fprintf(stderr, "default screen depth %d not supported, and no"
" 24-bit visuals found\n", depth);
return 2;
}
Trace((stderr, "XGetVisualInfo() returned %d 24-bit visuals\n",
visuals_matched))
visual = visual_list[0].visual;
depth = visual_list[0].depth;
have_nondefault_visual = TRUE;
need_colormap = TRUE;
} else {
XMatchVisualInfo(display, screen, depth, TrueColor, &visual_info);
visual = visual_info.visual;
}
RMask = visual->red_mask;
GMask = visual->green_mask;
BMask = visual->blue_mask;
if (depth == 8 || need_colormap) {
colormap = XCreateColormap(display, root, visual, AllocNone);
if (!colormap) {
fprintf(stderr, "XCreateColormap() failed\n");
return 2;
}
have_colormap = TRUE;
if (depth == 8)
bg_image = FALSE;
}
if (depth == 15 || depth == 16) {
RShift = 15 - rpng2_x_msb(RMask);
GShift = 15 - rpng2_x_msb(GMask);
BShift = 15 - rpng2_x_msb(BMask);
} else if (depth > 16) {
RShift = rpng2_x_msb(RMask) - 7;
GShift = rpng2_x_msb(GMask) - 7;
BShift = rpng2_x_msb(BMask) - 7;
}
if (depth >= 15 && (RShift < 0 || GShift < 0 || BShift < 0)) {
fprintf(stderr, "rpng2 internal logic error: negative X shift(s)!\n");
return 2;
}
attr.backing_store = Always;
attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask;
attrmask = CWBackingStore | CWEventMask;
if (have_nondefault_visual) {
attr.colormap = colormap;
attr.background_pixel = 0;
attr.border_pixel = 1;
attrmask |= CWColormap | CWBackPixel | CWBorderPixel;
}
window = XCreateWindow(display, root, 0, 0, rpng2_info.width,
rpng2_info.height, 0, depth, InputOutput, visual, attrmask, &attr);
if (window == None) {
fprintf(stderr, "XCreateWindow() failed\n");
return 2;
} else
have_window = TRUE;
if (depth == 8)
XSetWindowColormap(display, window, colormap);
if (!XStringListToTextProperty(&window_name, 1, pWindowName))
pWindowName = NULL;
if (!XStringListToTextProperty(&icon_name, 1, pIconName))
pIconName = NULL;
if ((size_hints = XAllocSizeHints()) != NULL) {
size_hints->flags = PMinSize | PMaxSize;
size_hints->min_width = size_hints->max_width = (int)rpng2_info.width;
size_hints->min_height = size_hints->max_height =
(int)rpng2_info.height;
}
if ((wm_hints = XAllocWMHints()) != NULL) {
wm_hints->initial_state = NormalState;
wm_hints->input = True;
wm_hints->flags = StateHint | InputHint ;
}
if ((class_hints = XAllocClassHint()) != NULL) {
class_hints->res_name = res_name;
class_hints->res_class = res_class;
}
XSetWMProperties(display, window, pWindowName, pIconName, NULL, 0,
size_hints, wm_hints, class_hints);
if (pWindowName)
XFree(pWindowName->value);
if (pIconName)
XFree(pIconName->value);
if (size_hints)
XFree(size_hints);
if (wm_hints)
XFree(wm_hints);
if (class_hints)
XFree(class_hints);
XMapWindow(display, window);
gc = XCreateGC(display, window, 0, &gcvalues);
have_gc = TRUE;
if (depth == 24 || depth == 32) {
xdata = (uch *)malloc(4*rpng2_info.width*rpng2_info.height);
pad = 32;
} else if (depth == 16) {
xdata = (uch *)malloc(2*rpng2_info.width*rpng2_info.height);
pad = 16;
} else {
xdata = (uch *)malloc(rpng2_info.width*rpng2_info.height);
pad = 8;
}
if (!xdata) {
fprintf(stderr, PROGNAME ": unable to allocate image memory\n");
return 4;
}
ximage = XCreateImage(display, visual, depth, ZPixmap, 0,
(char *)xdata, rpng2_info.width, rpng2_info.height, pad, 0);
if (!ximage) {
fprintf(stderr, PROGNAME ": XCreateImage() failed\n");
free(xdata);
return 3;
}
ximage->byte_order = MSBFirst;
if (bg_image)
rpng2_x_load_bg_image();
if (!bg_image) {
if (depth == 24 || depth == 32) {
bg_pixel = (bg_red << RShift) |
(bg_green << GShift) |
(bg_blue << BShift);
} else if (depth == 16) {
bg_pixel = (((bg_red << 8) >> RShift) & RMask) |
(((bg_green << 8) >> GShift) & GMask) |
(((bg_blue << 8) >> BShift) & BMask);
} else {
}
XSetForeground(display, gc, bg_pixel);
XFillRectangle(display, window, gc, 0, 0, rpng2_info.width,
rpng2_info.height);
}
do
XNextEvent(display, &e);
while (e.type != Expose || e.xexpose.count);
XFlush(display);
return 0;
}
static int rpng2_x_load_bg_image(void)
{
uch *src;
char *dest;
uch r1, r2, g1, g2, b1, b2;
uch r1_inv, r2_inv, g1_inv, g2_inv, b1_inv, b2_inv;
int k, hmax, max;
int xidx, yidx, yidx_max;
int even_odd_vert, even_odd_horiz, even_odd;
int invert_gradient2 = (bg[pat].type & 0x08);
int invert_column;
int ximage_rowbytes = ximage->bytes_per_line;
ulg i, row;
ulg pixel;
bg_rowbytes = 3 * rpng2_info.width;
bg_data = (uch *)malloc(bg_rowbytes * rpng2_info.height);
if (!bg_data) {
fprintf(stderr, PROGNAME
": unable to allocate memory for background image\n");
bg_image = 0;
return 1;
}
bgscale = (pat == 0)? 8 : bgscale_default;
yidx_max = bgscale - 1;
if ((bg[pat].type & 0x07) == 0) {
uch r1_min = rgb[bg[pat].rgb1_min].r;
uch g1_min = rgb[bg[pat].rgb1_min].g;
uch b1_min = rgb[bg[pat].rgb1_min].b;
uch r2_min = rgb[bg[pat].rgb2_min].r;
uch g2_min = rgb[bg[pat].rgb2_min].g;
uch b2_min = rgb[bg[pat].rgb2_min].b;
int r1_diff = rgb[bg[pat].rgb1_max].r - r1_min;
int g1_diff = rgb[bg[pat].rgb1_max].g - g1_min;
int b1_diff = rgb[bg[pat].rgb1_max].b - b1_min;
int r2_diff = rgb[bg[pat].rgb2_max].r - r2_min;
int g2_diff = rgb[bg[pat].rgb2_max].g - g2_min;
int b2_diff = rgb[bg[pat].rgb2_max].b - b2_min;
for (row = 0; row < rpng2_info.height; ++row) {
yidx = (int)(row % bgscale);
even_odd_vert = (int)((row / bgscale) & 1);
r1 = r1_min + (r1_diff * yidx) / yidx_max;
g1 = g1_min + (g1_diff * yidx) / yidx_max;
b1 = b1_min + (b1_diff * yidx) / yidx_max;
r1_inv = r1_min + (r1_diff * (yidx_max-yidx)) / yidx_max;
g1_inv = g1_min + (g1_diff * (yidx_max-yidx)) / yidx_max;
b1_inv = b1_min + (b1_diff * (yidx_max-yidx)) / yidx_max;
r2 = r2_min + (r2_diff * yidx) / yidx_max;
g2 = g2_min + (g2_diff * yidx) / yidx_max;
b2 = b2_min + (b2_diff * yidx) / yidx_max;
r2_inv = r2_min + (r2_diff * (yidx_max-yidx)) / yidx_max;
g2_inv = g2_min + (g2_diff * (yidx_max-yidx)) / yidx_max;
b2_inv = b2_min + (b2_diff * (yidx_max-yidx)) / yidx_max;
dest = (char *)bg_data + row*bg_rowbytes;
for (i = 0; i < rpng2_info.width; ++i) {
even_odd_horiz = (int)((i / bgscale) & 1);
even_odd = even_odd_vert ^ even_odd_horiz;
invert_column =
(even_odd_horiz && (bg[pat].type & 0x10));
if (even_odd == 0) {
if (invert_column) {
*dest++ = r1_inv;
*dest++ = g1_inv;
*dest++ = b1_inv;
} else {
*dest++ = r1;
*dest++ = g1;
*dest++ = b1;
}
} else {
if ((invert_column && invert_gradient2) ||
(!invert_column && !invert_gradient2))
{
*dest++ = r2;
*dest++ = g2;
*dest++ = b2;
} else {
*dest++ = r2_inv;
*dest++ = g2_inv;
*dest++ = b2_inv;
}
}
}
}
} else if ((bg[pat].type & 0x07) == 1) {
hmax = (bgscale-1)/2;
max = 2*hmax;
r1 = rgb[bg[pat].rgb1_max].r;
g1 = rgb[bg[pat].rgb1_max].g;
b1 = rgb[bg[pat].rgb1_max].b;
r2 = rgb[bg[pat].rgb2_max].r;
g2 = rgb[bg[pat].rgb2_max].g;
b2 = rgb[bg[pat].rgb2_max].b;
for (row = 0; row < rpng2_info.height; ++row) {
yidx = (int)(row % bgscale);
if (yidx > hmax)
yidx = bgscale-1 - yidx;
dest = (char *)bg_data + row*bg_rowbytes;
for (i = 0; i < rpng2_info.width; ++i) {
xidx = (int)(i % bgscale);
if (xidx > hmax)
xidx = bgscale-1 - xidx;
k = xidx + yidx;
*dest++ = (k*r1 + (max-k)*r2) / max;
*dest++ = (k*g1 + (max-k)*g2) / max;
*dest++ = (k*b1 + (max-k)*b2) / max;
}
}
} else if ((bg[pat].type & 0x07) == 2) {
uch ch;
int ii, x, y, hw, hh, grayspot;
double freq, rotate, saturate, gray, intensity;
double angle=0.0, aoffset=0.0, maxDist, dist;
double red=0.0, green=0.0, blue=0.0, hue, s, v, f, p, q, t;
fprintf(stderr, "%s: computing radial background...",
PROGNAME);
fflush(stderr);
hh = (int)(rpng2_info.height / 2);
hw = (int)(rpng2_info.width / 2);
angle = CLIP(angle, 0.0, 360.0);
grayspot = CLIP(bg[pat].bg_gray, 1, (hh + hw));
freq = MAX((double)bg[pat].bg_freq, 0.0);
saturate = (double)bg[pat].bg_bsat * 0.1;
rotate = (double)bg[pat].bg_brot * 0.1;
gray = 0.0;
intensity = 0.0;
maxDist = (double)((hw*hw) + (hh*hh));
for (row = 0; row < rpng2_info.height; ++row) {
y = (int)(row - hh);
dest = (char *)bg_data + row*bg_rowbytes;
for (i = 0; i < rpng2_info.width; ++i) {
x = (int)(i - hw);
angle = (x == 0)? PI_2 : atan((double)y / (double)x);
gray = (double)MAX(ABS(y), ABS(x)) / grayspot;
gray = MIN(1.0, gray);
dist = (double)((x*x) + (y*y)) / maxDist;
intensity = cos((angle+(rotate*dist*PI)) * freq) *
gray * saturate;
intensity = (MAX(MIN(intensity,1.0),-1.0) + 1.0) * 0.5;
hue = (angle + PI) * INV_PI_360 + aoffset;
s = gray * ((double)(ABS(x)+ABS(y)) / (double)(hw + hh));
s = MIN(MAX(s,0.0), 1.0);
v = MIN(MAX(intensity,0.0), 1.0);
if (s == 0.0) {
ch = (uch)(v * 255.0);
*dest++ = ch;
*dest++ = ch;
*dest++ = ch;
} else {
if ((hue < 0.0) || (hue >= 360.0))
hue -= (((int)(hue / 360.0)) * 360.0);
hue /= 60.0;
ii = (int)hue;
f = hue - (double)ii;
p = (1.0 - s) * v;
q = (1.0 - (s * f)) * v;
t = (1.0 - (s * (1.0 - f))) * v;
if (ii == 0) { red = v; green = t; blue = p; }
else if (ii == 1) { red = q; green = v; blue = p; }
else if (ii == 2) { red = p; green = v; blue = t; }
else if (ii == 3) { red = p; green = q; blue = v; }
else if (ii == 4) { red = t; green = p; blue = v; }
else if (ii == 5) { red = v; green = p; blue = q; }
*dest++ = (uch)(red * 255.0);
*dest++ = (uch)(green * 255.0);
*dest++ = (uch)(blue * 255.0);
}
}
}
fprintf(stderr, "done.\n");
fflush(stderr);
}
if (depth == 24 || depth == 32) {
ulg red, green, blue;
int bpp = ximage->bits_per_pixel;
for (row = 0; row < rpng2_info.height; ++row) {
src = bg_data + row*bg_rowbytes;
dest = ximage->data + row*ximage_rowbytes;
if (bpp == 32) {
for (i = rpng2_info.width; i > 0; --i) {
red = *src++;
green = *src++;
blue = *src++;
pixel = (red << RShift) |
(green << GShift) |
(blue << BShift);
*dest++ = (char)((pixel >> 24) & 0xff);
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
} else {
for (i = rpng2_info.width; i > 0; --i) {
red = *src++;
green = *src++;
blue = *src++;
pixel = (red << RShift) |
(green << GShift) |
(blue << BShift);
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
}
}
} else if (depth == 16) {
ush red, green, blue;
for (row = 0; row < rpng2_info.height; ++row) {
src = bg_data + row*bg_rowbytes;
dest = ximage->data + row*ximage_rowbytes;
for (i = rpng2_info.width; i > 0; --i) {
red = ((ush)(*src) << 8); ++src;
green = ((ush)(*src) << 8); ++src;
blue = ((ush)(*src) << 8); ++src;
pixel = ((red >> RShift) & RMask) |
((green >> GShift) & GMask) |
((blue >> BShift) & BMask);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
}
} else {
}
XPutImage(display, window, gc, ximage, 0, 0, 0, 0, rpng2_info.width,
rpng2_info.height);
return 0;
}
static void rpng2_x_display_row(ulg row)
{
uch bg_red = rpng2_info.bg_red;
uch bg_green = rpng2_info.bg_green;
uch bg_blue = rpng2_info.bg_blue;
uch *src, *src2=NULL;
char *dest;
uch r, g, b, a;
int ximage_rowbytes = ximage->bytes_per_line;
ulg i, pixel;
static int rows=0, prevpass=(-1);
static ulg firstrow;
Trace((stderr, "beginning rpng2_x_display_row()\n"))
if (rpng2_info.pass != prevpass) {
if (pause_after_pass && rpng2_info.pass > 0) {
XEvent e;
KeySym k;
fprintf(stderr,
"%s: end of pass %d of 7; click in image window to continue\n",
PROGNAME, prevpass + 1);
do
XNextEvent(display, &e);
while (!QUIT(e,k));
}
fprintf(stderr, "%s: pass %d of 7\r", PROGNAME, rpng2_info.pass + 1);
fflush(stderr);
prevpass = rpng2_info.pass;
}
if (rows == 0)
firstrow = row;
++rows;
if (depth == 24 || depth == 32) {
ulg red, green, blue;
int bpp = ximage->bits_per_pixel;
src = rpng2_info.image_data + row*rpng2_info.rowbytes;
if (bg_image)
src2 = bg_data + row*bg_rowbytes;
dest = ximage->data + row*ximage_rowbytes;
if (rpng2_info.channels == 3) {
for (i = rpng2_info.width; i > 0; --i) {
red = *src++;
green = *src++;
blue = *src++;
pixel = (red << RShift) |
(green << GShift) |
(blue << BShift);
if (bpp == 32) {
*dest++ = (char)((pixel >> 24) & 0xff);
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
} else {
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
}
} else {
for (i = rpng2_info.width; i > 0; --i) {
r = *src++;
g = *src++;
b = *src++;
a = *src++;
if (bg_image) {
bg_red = *src2++;
bg_green = *src2++;
bg_blue = *src2++;
}
if (a == 255) {
red = r;
green = g;
blue = b;
} else if (a == 0) {
red = bg_red;
green = bg_green;
blue = bg_blue;
} else {
alpha_composite(red, r, a, bg_red);
alpha_composite(green, g, a, bg_green);
alpha_composite(blue, b, a, bg_blue);
}
pixel = (red << RShift) |
(green << GShift) |
(blue << BShift);
if (bpp == 32) {
*dest++ = (char)((pixel >> 24) & 0xff);
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
} else {
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
}
}
} else if (depth == 16) {
ush red, green, blue;
src = rpng2_info.row_pointers[row];
if (bg_image)
src2 = bg_data + row*bg_rowbytes;
dest = ximage->data + row*ximage_rowbytes;
if (rpng2_info.channels == 3) {
for (i = rpng2_info.width; i > 0; --i) {
red = ((ush)(*src) << 8);
++src;
green = ((ush)(*src) << 8);
++src;
blue = ((ush)(*src) << 8);
++src;
pixel = ((red >> RShift) & RMask) |
((green >> GShift) & GMask) |
((blue >> BShift) & BMask);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
} else {
for (i = rpng2_info.width; i > 0; --i) {
r = *src++;
g = *src++;
b = *src++;
a = *src++;
if (bg_image) {
bg_red = *src2++;
bg_green = *src2++;
bg_blue = *src2++;
}
if (a == 255) {
red = ((ush)r << 8);
green = ((ush)g << 8);
blue = ((ush)b << 8);
} else if (a == 0) {
red = ((ush)bg_red << 8);
green = ((ush)bg_green << 8);
blue = ((ush)bg_blue << 8);
} else {
alpha_composite(r, r, a, bg_red);
alpha_composite(g, g, a, bg_green);
alpha_composite(b, b, a, bg_blue);
red = ((ush)r << 8);
green = ((ush)g << 8);
blue = ((ush)b << 8);
}
pixel = ((red >> RShift) & RMask) |
((green >> GShift) & GMask) |
((blue >> BShift) & BMask);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
}
} else {
}
if (demo_timing && (row - firstrow >= 16 || row >= rpng2_info.height-2)) {
XPutImage(display, window, gc, ximage, 0, (int)firstrow, 0,
(int)firstrow, rpng2_info.width, row - firstrow + 1);
XFlush(display);
rows = 0;
usleep(usleep_duration);
} else
if (!demo_timing && ((rows & 0xf) == 0 || row >= rpng2_info.height-2)) {
XPutImage(display, window, gc, ximage, 0, (int)firstrow, 0,
(int)firstrow, rpng2_info.width, row - firstrow + 1);
XFlush(display);
rows = 0;
}
}
static void rpng2_x_finish_display(void)
{
Trace((stderr, "beginning rpng2_x_finish_display()\n"))
rpng2_info.state = kDone;
printf(
"Done. Press Q, Esc or mouse button 1 (within image window) to quit.\n");
fflush(stdout);
}
static void rpng2_x_redisplay_image(ulg startcol, ulg startrow,
ulg width, ulg height)
{
uch bg_red = rpng2_info.bg_red;
uch bg_green = rpng2_info.bg_green;
uch bg_blue = rpng2_info.bg_blue;
uch *src, *src2=NULL;
char *dest;
uch r, g, b, a;
ulg i, row, lastrow = 0;
ulg pixel;
int ximage_rowbytes = ximage->bytes_per_line;
Trace((stderr, "beginning display loop (image_channels == %d)\n",
rpng2_info.channels))
Trace((stderr, " (width = %ld, rowbytes = %d, ximage_rowbytes = %d)\n",
rpng2_info.width, rpng2_info.rowbytes, ximage_rowbytes))
Trace((stderr, " (bpp = %d)\n", ximage->bits_per_pixel))
Trace((stderr, " (byte_order = %s)\n", ximage->byte_order == MSBFirst?
"MSBFirst" : (ximage->byte_order == LSBFirst? "LSBFirst" : "unknown")))
if (depth == 24 || depth == 32) {
ulg red, green, blue;
int bpp = ximage->bits_per_pixel;
for (lastrow = row = startrow; row < startrow+height; ++row) {
src = rpng2_info.image_data + row*rpng2_info.rowbytes;
if (bg_image)
src2 = bg_data + row*bg_rowbytes;
dest = ximage->data + row*ximage_rowbytes;
if (rpng2_info.channels == 3) {
for (i = rpng2_info.width; i > 0; --i) {
red = *src++;
green = *src++;
blue = *src++;
#ifdef NO_24BIT_MASKS
pixel = (red << RShift) |
(green << GShift) |
(blue << BShift);
if (bpp == 32) {
*dest++ = (char)((pixel >> 24) & 0xff);
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
} else {
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
#else
red = (RShift < 0)? red << (-RShift) : red >> RShift;
green = (GShift < 0)? green << (-GShift) : green >> GShift;
blue = (BShift < 0)? blue << (-BShift) : blue >> BShift;
pixel = (red & RMask) | (green & GMask) | (blue & BMask);
if (bpp == 32) {
*dest++ = (char)((pixel >> 24) & 0xff);
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
} else {
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
#endif
}
} else {
for (i = rpng2_info.width; i > 0; --i) {
r = *src++;
g = *src++;
b = *src++;
a = *src++;
if (bg_image) {
bg_red = *src2++;
bg_green = *src2++;
bg_blue = *src2++;
}
if (a == 255) {
red = r;
green = g;
blue = b;
} else if (a == 0) {
red = bg_red;
green = bg_green;
blue = bg_blue;
} else {
alpha_composite(red, r, a, bg_red);
alpha_composite(green, g, a, bg_green);
alpha_composite(blue, b, a, bg_blue);
}
#ifdef NO_24BIT_MASKS
pixel = (red << RShift) |
(green << GShift) |
(blue << BShift);
if (bpp == 32) {
*dest++ = (char)((pixel >> 24) & 0xff);
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
} else {
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
#else
red = (RShift < 0)? red << (-RShift) : red >> RShift;
green = (GShift < 0)? green << (-GShift) : green >> GShift;
blue = (BShift < 0)? blue << (-BShift) : blue >> BShift;
pixel = (red & RMask) | (green & GMask) | (blue & BMask);
if (bpp == 32) {
*dest++ = (char)((pixel >> 24) & 0xff);
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
} else {
*dest++ = (char)((pixel >> 16) & 0xff);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
#endif
}
}
if (((row+1) & 0xf) == 0) {
XPutImage(display, window, gc, ximage, 0, (int)lastrow, 0,
(int)lastrow, rpng2_info.width, 16);
XFlush(display);
lastrow = row + 1;
}
}
} else if (depth == 16) {
ush red, green, blue;
for (lastrow = row = startrow; row < startrow+height; ++row) {
src = rpng2_info.row_pointers[row];
if (bg_image)
src2 = bg_data + row*bg_rowbytes;
dest = ximage->data + row*ximage_rowbytes;
if (rpng2_info.channels == 3) {
for (i = rpng2_info.width; i > 0; --i) {
red = ((ush)(*src) << 8);
++src;
green = ((ush)(*src) << 8);
++src;
blue = ((ush)(*src) << 8);
++src;
pixel = ((red >> RShift) & RMask) |
((green >> GShift) & GMask) |
((blue >> BShift) & BMask);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
} else {
for (i = rpng2_info.width; i > 0; --i) {
r = *src++;
g = *src++;
b = *src++;
a = *src++;
if (bg_image) {
bg_red = *src2++;
bg_green = *src2++;
bg_blue = *src2++;
}
if (a == 255) {
red = ((ush)r << 8);
green = ((ush)g << 8);
blue = ((ush)b << 8);
} else if (a == 0) {
red = ((ush)bg_red << 8);
green = ((ush)bg_green << 8);
blue = ((ush)bg_blue << 8);
} else {
alpha_composite(r, r, a, bg_red);
alpha_composite(g, g, a, bg_green);
alpha_composite(b, b, a, bg_blue);
red = ((ush)r << 8);
green = ((ush)g << 8);
blue = ((ush)b << 8);
}
pixel = ((red >> RShift) & RMask) |
((green >> GShift) & GMask) |
((blue >> BShift) & BMask);
*dest++ = (char)((pixel >> 8) & 0xff);
*dest++ = (char)( pixel & 0xff);
}
}
if (((row+1) & 0xf) == 0) {
XPutImage(display, window, gc, ximage, 0, (int)lastrow, 0,
(int)lastrow, rpng2_info.width, 16);
XFlush(display);
lastrow = row + 1;
}
}
} else {
}
Trace((stderr, "calling final XPutImage()\n"))
if (lastrow < startrow+height) {
XPutImage(display, window, gc, ximage, 0, (int)lastrow, 0,
(int)lastrow, rpng2_info.width, rpng2_info.height-lastrow);
XFlush(display);
}
}
#ifdef FEATURE_LOOP
static void rpng2_x_reload_bg_image(void)
{
char *dest;
uch r1, r2, g1, g2, b1, b2;
uch r1_inv, r2_inv, g1_inv, g2_inv, b1_inv, b2_inv;
int k, hmax, max;
int xidx, yidx, yidx_max;
int even_odd_vert, even_odd_horiz, even_odd;
int invert_gradient2 = (bg[pat].type & 0x08);
int invert_column;
ulg i, row;
bgscale = (pat == 0)? 8 : bgscale_default;
yidx_max = bgscale - 1;
if ((bg[pat].type & 0x07) == 0) {
uch r1_min = rgb[bg[pat].rgb1_min].r;
uch g1_min = rgb[bg[pat].rgb1_min].g;
uch b1_min = rgb[bg[pat].rgb1_min].b;
uch r2_min = rgb[bg[pat].rgb2_min].r;
uch g2_min = rgb[bg[pat].rgb2_min].g;
uch b2_min = rgb[bg[pat].rgb2_min].b;
int r1_diff = rgb[bg[pat].rgb1_max].r - r1_min;
int g1_diff = rgb[bg[pat].rgb1_max].g - g1_min;
int b1_diff = rgb[bg[pat].rgb1_max].b - b1_min;
int r2_diff = rgb[bg[pat].rgb2_max].r - r2_min;
int g2_diff = rgb[bg[pat].rgb2_max].g - g2_min;
int b2_diff = rgb[bg[pat].rgb2_max].b - b2_min;
for (row = 0; row < rpng2_info.height; ++row) {
yidx = (int)(row % bgscale);
even_odd_vert = (int)((row / bgscale) & 1);
r1 = r1_min + (r1_diff * yidx) / yidx_max;
g1 = g1_min + (g1_diff * yidx) / yidx_max;
b1 = b1_min + (b1_diff * yidx) / yidx_max;
r1_inv = r1_min + (r1_diff * (yidx_max-yidx)) / yidx_max;
g1_inv = g1_min + (g1_diff * (yidx_max-yidx)) / yidx_max;
b1_inv = b1_min + (b1_diff * (yidx_max-yidx)) / yidx_max;
r2 = r2_min + (r2_diff * yidx) / yidx_max;
g2 = g2_min + (g2_diff * yidx) / yidx_max;
b2 = b2_min + (b2_diff * yidx) / yidx_max;
r2_inv = r2_min + (r2_diff * (yidx_max-yidx)) / yidx_max;
g2_inv = g2_min + (g2_diff * (yidx_max-yidx)) / yidx_max;
b2_inv = b2_min + (b2_diff * (yidx_max-yidx)) / yidx_max;
dest = (char *)bg_data + row*bg_rowbytes;
for (i = 0; i < rpng2_info.width; ++i) {
even_odd_horiz = (int)((i / bgscale) & 1);
even_odd = even_odd_vert ^ even_odd_horiz;
invert_column =
(even_odd_horiz && (bg[pat].type & 0x10));
if (even_odd == 0) {
if (invert_column) {
*dest++ = r1_inv;
*dest++ = g1_inv;
*dest++ = b1_inv;
} else {
*dest++ = r1;
*dest++ = g1;
*dest++ = b1;
}
} else {
if ((invert_column && invert_gradient2) ||
(!invert_column && !invert_gradient2))
{
*dest++ = r2;
*dest++ = g2;
*dest++ = b2;
} else {
*dest++ = r2_inv;
*dest++ = g2_inv;
*dest++ = b2_inv;
}
}
}
}
} else if ((bg[pat].type & 0x07) == 1) {
hmax = (bgscale-1)/2;
max = 2*hmax;
r1 = rgb[bg[pat].rgb1_max].r;
g1 = rgb[bg[pat].rgb1_max].g;
b1 = rgb[bg[pat].rgb1_max].b;
r2 = rgb[bg[pat].rgb2_max].r;
g2 = rgb[bg[pat].rgb2_max].g;
b2 = rgb[bg[pat].rgb2_max].b;
for (row = 0; row < rpng2_info.height; ++row) {
yidx = (int)(row % bgscale);
if (yidx > hmax)
yidx = bgscale-1 - yidx;
dest = (char *)bg_data + row*bg_rowbytes;
for (i = 0; i < rpng2_info.width; ++i) {
xidx = (int)(i % bgscale);
if (xidx > hmax)
xidx = bgscale-1 - xidx;
k = xidx + yidx;
*dest++ = (k*r1 + (max-k)*r2) / max;
*dest++ = (k*g1 + (max-k)*g2) / max;
*dest++ = (k*b1 + (max-k)*b2) / max;
}
}
} else if ((bg[pat].type & 0x07) == 2) {
uch ch;
int ii, x, y, hw, hh, grayspot;
double freq, rotate, saturate, gray, intensity;
double angle=0.0, aoffset=0.0, maxDist, dist;
double red=0.0, green=0.0, blue=0.0, hue, s, v, f, p, q, t;
hh = (int)(rpng2_info.height / 2);
hw = (int)(rpng2_info.width / 2);
angle = CLIP(angle, 0.0, 360.0);
grayspot = CLIP(bg[pat].bg_gray, 1, (hh + hw));
freq = MAX((double)bg[pat].bg_freq, 0.0);
saturate = (double)bg[pat].bg_bsat * 0.1;
rotate = (double)bg[pat].bg_brot * 0.1;
gray = 0.0;
intensity = 0.0;
maxDist = (double)((hw*hw) + (hh*hh));
for (row = 0; row < rpng2_info.height; ++row) {
y = (int)(row - hh);
dest = (char *)bg_data + row*bg_rowbytes;
for (i = 0; i < rpng2_info.width; ++i) {
x = (int)(i - hw);
angle = (x == 0)? PI_2 : atan((double)y / (double)x);
gray = (double)MAX(ABS(y), ABS(x)) / grayspot;
gray = MIN(1.0, gray);
dist = (double)((x*x) + (y*y)) / maxDist;
intensity = cos((angle+(rotate*dist*PI)) * freq) *
gray * saturate;
intensity = (MAX(MIN(intensity,1.0),-1.0) + 1.0) * 0.5;
hue = (angle + PI) * INV_PI_360 + aoffset;
s = gray * ((double)(ABS(x)+ABS(y)) / (double)(hw + hh));
s = MIN(MAX(s,0.0), 1.0);
v = MIN(MAX(intensity,0.0), 1.0);
if (s == 0.0) {
ch = (uch)(v * 255.0);
*dest++ = ch;
*dest++ = ch;
*dest++ = ch;
} else {
if ((hue < 0.0) || (hue >= 360.0))
hue -= (((int)(hue / 360.0)) * 360.0);
hue /= 60.0;
ii = (int)hue;
f = hue - (double)ii;
p = (1.0 - s) * v;
q = (1.0 - (s * f)) * v;
t = (1.0 - (s * (1.0 - f))) * v;
if (ii == 0) { red = v; green = t; blue = p; }
else if (ii == 1) { red = q; green = v; blue = p; }
else if (ii == 2) { red = p; green = v; blue = t; }
else if (ii == 3) { red = p; green = q; blue = v; }
else if (ii == 4) { red = t; green = p; blue = v; }
else if (ii == 5) { red = v; green = p; blue = q; }
*dest++ = (uch)(red * 255.0);
*dest++ = (uch)(green * 255.0);
*dest++ = (uch)(blue * 255.0);
}
}
}
}
}
static int is_number(char *p)
{
while (*p) {
if (!isdigit(*p))
return FALSE;
++p;
}
return TRUE;
}
#endif
static void rpng2_x_cleanup(void)
{
if (bg_image && bg_data) {
free(bg_data);
bg_data = NULL;
}
if (rpng2_info.image_data) {
free(rpng2_info.image_data);
rpng2_info.image_data = NULL;
}
if (rpng2_info.row_pointers) {
free(rpng2_info.row_pointers);
rpng2_info.row_pointers = NULL;
}
if (ximage) {
if (ximage->data) {
free(ximage->data);
ximage->data = (char *)NULL;
}
XDestroyImage(ximage);
ximage = NULL;
}
if (have_gc)
XFreeGC(display, gc);
if (have_window)
XDestroyWindow(display, window);
if (have_colormap)
XFreeColormap(display, colormap);
if (have_nondefault_visual)
XFree(visual_list);
}
static int rpng2_x_msb(ulg u32val)
{
int i;
for (i = 31; i >= 0; --i) {
if (u32val & 0x80000000L)
break;
u32val <<= 1;
}
return i;
}