📄 rpng2-x.c
字号:
/*--------------------------------------------------------------------------- rpng2 - progressive-model PNG display program rpng2-x.c This program decodes and displays PNG files progressively, as if it were a web browser (though the front end is only set up to read from files). It supports gamma correction, user-specified background colors, and user- specified background patterns (for transparent images). This version is for the X Window System (tested by the author under Unix and by Martin Zinser under OpenVMS; may work under OS/2 with a little tweaking). Thanks to Adam Costello and Pieter S. van der Meulen for the "diamond" and "radial waves" patterns, respectively. to do: - fix expose/redraw code: don't draw entire row if only part exposed - 8-bit (colormapped) X support - finish resizable checkerboard-gradient (sizes 4-128?) - use %.1023s to simplify truncation of title-bar string? --------------------------------------------------------------------------- Changelog: - 1.01: initial public release - 1.02: modified to allow abbreviated options; fixed char/uchar mismatch - 1.10: added support for non-default visuals; fixed X pixel-conversion - 1.11: added -usleep option for demos; fixed command-line parsing bug - 1.12: added -pause option for demos and testing - 1.20: added runtime MMX-enabling/disabling and new -mmx* options - 1.21: fixed some small X memory leaks (thanks to Fran鏾is Petitjean) - 1.22: fixed XFreeGC() crash bug (thanks to Patrick Welche) - 1.23: added -bgpat 0 mode (std white/gray checkerboard, 8x8 squares) - 1.30: added -loop option for -bgpat (ifdef FEATURE_LOOP); fixed bpp = 24; added support for X resources (thanks to Gerhard Niklasch) - 1.31: added code to skip unused chunks (thanks to Glenn Randers-Pehrson) - 1.32: added AMD64/EM64T support (__x86_64__); added basic expose/redraw handling - 2.00: dual-licensed (added GNU GPL) --------------------------------------------------------------------------- Copyright (c) 1998-2007 Greg Roelofs. All rights reserved. This software is provided "as is," without warranty of any kind, express or implied. In no event shall the author or contributors be held liable for any damages arising in any way from the use of this software. The contents of this file are DUAL-LICENSED. You may modify and/or redistribute this software according to the terms of one of the following two licenses (at your option): LICENSE 1 ("BSD-like with advertising clause"): Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. Redistributions of source code must retain the above copyright notice, disclaimer, and this list of conditions. 2. Redistributions in binary form must reproduce the above copyright notice, disclaimer, and this list of conditions in the documenta- tion and/or other materials provided with the distribution. 3. All advertising materials mentioning features or use of this software must display the following acknowledgment: This product includes software developed by Greg Roelofs and contributors for the book, "PNG: The Definitive Guide," published by O'Reilly and Associates. LICENSE 2 (GNU GPL v2 or later): This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---------------------------------------------------------------------------*/#define PROGNAME "rpng2-x"#define LONGNAME "Progressive PNG Viewer for X"#define VERSION "2.00 of 2 June 2007"#define RESNAME "rpng2" /* our X resource application name */#define RESCLASS "Rpng" /* our X resource class name */#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include <string.h>#include <setjmp.h> /* for jmpbuf declaration in readpng2.h */#include <time.h>#include <math.h> /* only for PvdM background code */#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/Xos.h>#include <X11/keysym.h> /* defines XK_* macros */#ifdef VMS# include <unistd.h>#endif/* all for PvdM background code: */#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))) /* 8-bit pos. integer (uch) */#define ROUNDF(f) ((int)(f + 0.5))#define QUIT(e,k) ((e.type == ButtonPress && e.xbutton.button == Button1) || \ (e.type == KeyPress && /* v--- or 1 for shifted keys */ \ ((k = XLookupKeysym(&e.xkey, 0)) == XK_q || k == XK_Escape)))#define NO_24BIT_MASKS /* undef case not fully written--only for redisplay() */#define rgb1_max bg_freq#define rgb1_min bg_gray#define rgb2_max bg_bsat#define rgb2_min bg_brot/* #define DEBUG */ /* this enables the Trace() macros */#include "readpng2.h" /* typedefs, common macros, readpng2 prototypes *//* could just include png.h, but this macro is the only thing we need * (name and typedefs changed to local versions); note that side effects * only happen with alpha (which could easily be avoided with * "ush acopy = (alpha);") */#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 /* with pseudo-timing on (1 sec delay/block), this * 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) *//* local prototypes */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_LOOPstatic void rpng2_x_reload_bg_image (void);static int is_number (char *p);#endifstatic 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; /* must be less than num_bgpat */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}, /* 0: black */ {255, 255, 255}, /* 1: white */ {173, 132, 57}, /* 2: tan */ { 64, 132, 0}, /* 3: medium green */ {189, 117, 1}, /* 4: gold */ {253, 249, 1}, /* 5: yellow */ { 0, 0, 255}, /* 6: blue */ { 0, 0, 120}, /* 7: medium blue */ {255, 0, 255}, /* 8: magenta */ { 64, 0, 64}, /* 9: dark magenta */ {255, 0, 0}, /* 10: red */ { 64, 0, 0}, /* 11: dark red */ {255, 127, 0}, /* 12: orange */ {192, 96, 0}, /* 13: darker orange */ { 24, 60, 0}, /* 14: dark green-yellow */ { 85, 125, 200}, /* 15: ice blue */ {192, 192, 192} /* 16: Netscape/Mosaic gray */};/* not used for now, but should be for error-checking:static int num_rgb = sizeof(rgb) / sizeof(struct rgb_color); *//* This whole struct is a fairly cheesy way to keep the number of command-line options to a minimum. The radial-waves background type is a particularly poor fit to the integer elements of the struct...but a few macros and a little fixed-point math will do wonders for ya. type bits: F E D C B A 9 8 7 6 5 4 3 2 1 0 | | | | | | | +-+-+-- 0 = sharp-edged checkerboard | | 1 = soft diamonds | | 2 = radial waves | | 3-7 = undefined | +-- gradient #2 inverted? +-- alternating columns inverted? */static struct background_pattern { ush type; int rgb1_max, rgb1_min; /* or bg_freq, bg_gray */ int rgb2_max, rgb2_min; /* or bg_bsat, bg_brot (both scaled by 10)*/} bg[] = { {0, 1,1, 16,16}, /* checkered: white vs. light gray (basic) */ {0+8, 2,0, 1,15}, /* checkered: tan/black vs. white/ice blue */ {0+24, 2,0, 1,0}, /* checkered: tan/black vs. white/black */ {0+8, 4,5, 0,2}, /* checkered: gold/yellow vs. black/tan */ {0+8, 4,5, 0,6}, /* checkered: gold/yellow vs. black/blue */ {0, 7,0, 8,9}, /* checkered: deep blue/black vs. magenta */ {0+8, 13,0, 5,14}, /* checkered: orange/black vs. yellow */ {0+8, 12,0, 10,11}, /* checkered: orange/black vs. red */ {1, 7,0, 8,0}, /* diamonds: deep blue/black vs. magenta */ {1, 12,0, 11,0}, /* diamonds: orange vs. dark red */ {1, 10,0, 7,0}, /* diamonds: red vs. medium blue */ {1, 4,0, 5,0}, /* diamonds: gold vs. yellow */ {1, 3,0, 0,0}, /* diamonds: medium green vs. black */ {2, 16, 100, 20, 0}, /* radial: ~hard radial color-beams */ {2, 18, 100, 10, 2}, /* radial: soft, curved radial color-beams */ {2, 16, 256, 100, 250}, /* radial: very tight spiral */ {2, 10000, 256, 11, 0} /* radial: dipole-moire' (almost fractal) */};static int num_bgpat = sizeof(bg) / sizeof(struct background_pattern);/* X-specific variables */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; /* seconds (100,000 max) */#endif double LUT_exponent; /* just the lookup table */ double CRT_exponent = 2.2; /* just the monitor */ double default_display_exponent; /* whole display system */ XEvent e; KeySym k; /* First initialize a few things, just to be sure--memset takes care of * default background color (black), booleans (FALSE), pointers (NULL), * etc. */ displayname = (char *)NULL; filename = (char *)NULL; memset(&rpng2_info, 0, sizeof(mainprog_info)); /* Set the default value for our display-system exponent, i.e., the * product of the CRT exponent and the exponent corresponding to * the frame-buffer's lookup table (LUT), if any. This is not an * exhaustive list of LUT values (e.g., OpenStep has a lot of weird * ones), but it should cover 99% of the current possibilities. */#if defined(NeXT) /* third-party utilities can modify the default LUT exponent */ LUT_exponent = 1.0 / 2.2; /* if (some_next_function_that_returns_gamma(&next_gamma)) LUT_exponent = 1.0 / next_gamma; */#elif defined(sgi) LUT_exponent = 1.0 / 1.7; /* there doesn't seem to be any documented function to * get the "gamma" value, so we do it the hard way */ infile = fopen("/etc/config/system.glGammaVal", "r"); if (infile) { double sgi_gamma;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -