/*---------------------------------------------------------------------------
rpng - simple PNG display program rpng-x.c
This program decodes and displays PNG images, with gamma correction and
optionally with a user-specified background color (in case the image has
transparency). It is very nearly the most basic PNG viewer possible.
This version is for the X Window System (tested by author under Unix and
by Martin Zinser under OpenVMS; may work under OS/2 with some tweaking).
to do:
- 8-bit support
- use %.1023s to simplify truncation of title-bar string?
---------------------------------------------------------------------------
Changelog:
- 1.01: initial public release
match; switched to png_jmpbuf() macro
- 1.10: added support for non-default visuals; fixed X pixel-conversion
- 1.11: added extra set of parentheses to png_jmpbuf() macro; fixed
command-line parsing bug
- 1.12: fixed small X memory leak (thanks to Francois Petitjean)
- 1.13: fixed XFreeGC() crash bug
---------------------------------------------------------------------------
Copyright (c) 1998-2001 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.
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-
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.
---------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* #define DEBUG : this enables the Trace() macros */
#include "readpng.h" /* typedefs, common macros, readpng 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);") */
}
/* local prototypes */
static int rpng_x_create_window(void);
static int rpng_x_display_image(void);
static void rpng_x_cleanup(void);
static char *filename;
static char *bgstr;
static double display_exponent;
static int image_channels;
/* X-specific variables */
static char *displayname;
static int depth;
/*
ulg numcolors=0, pixels[256];
ush reds[256], greens[256], blues[256];
*/
{
#ifdef sgi
#endif
char *p;
int error = 0;
XEvent e;
KeySym k;
displayname = (char *)NULL;
/* First 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)
/*
if (some_next_function_that_returns_gamma(&next_gamma))
LUT_exponent = 1.0 / next_gamma;
*/
/* there doesn't seem to be any documented function to get the
* "gamma" value, so we do it the hard way */
if (infile) {
double sgi_gamma;
if (sgi_gamma > 0.0)
}
/*
if (some_mac_function_that_returns_gamma(&mac_gamma))
LUT_exponent = mac_gamma / 2.61;
*/
#else
#endif
/* the defaults above give 1.0, 1.3, 1.5 and 2.2, respectively: */
/* If the user has set the SCREEN_GAMMA environment variable as suggested
* (somewhat imprecisely) in the libpng documentation, use that; otherwise
* use the default value we just calculated. Either way, the user may
* override this via a command-line option. */
display_exponent = atof(p);
else
/* Now parse the command line for options and the PNG filename. */
if (!*++argv)
++error;
else
displayname = *argv;
if (!*++argv)
++error;
else {
if (display_exponent <= 0.0)
++error;
}
if (!*++argv)
++error;
else {
++error;
else
}
} else {
if (**argv != '-') {
++error;
} else
++error; /* not expecting any other options */
}
}
if (!filename) {
++error;
++error;
} else {
switch (rc) {
case 1:
": [%s] is not a PNG file: incorrect signature\n",
filename);
break;
case 2:
": [%s] has bad IHDR (libpng longjmp)\n",
filename);
break;
case 4:
break;
default:
": unknown readpng_init() error\n");
break;
}
++error;
} else {
if (!display) {
++error;
}
}
if (error)
}
/* usage screen */
if (error) {
"Usage: %s [-display xdpy] [-gamma exp] [-bgcolor bg] file.png\n"
" 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\n"
"\nPress Q, Esc or mouse button 1 (within image window, after image\n"
"is displayed) to quit.\n"
exit(1);
}
/* set the title-bar string, but make sure buffer doesn't overflow */
else
/* if the user didn't specify a background color on the command line,
* check for one in the PNG file--if not, the initialized values of 0
* (black) will be used */
if (have_bg) {
unsigned r, g, b; /* this approach quiets compiler warnings */
": libpng error while checking for background color\n");
exit(2);
}
/* do the basic X initialization stuff, make the window and fill it
* with the background color */
if (rpng_x_create_window())
exit(2);
/* decode the image, all at once */
/* done with PNG file, so clean up to minimize memory usage (but do NOT
* nuke image_data!) */
if (!image_data) {
exit(3);
}
/* display image (composite with background if requested) */
if (rpng_x_display_image()) {
exit(4);
}
/* wait for the user to tell us when to quit */
"Done. Press Q, Esc or mouse button 1 (within image window) to quit.\n");
do
XNextEvent(display, &e);
/* OK, we're done: clean up all image and X resources and go away */
return 0;
}
static int rpng_x_create_window(void)
{
XEvent e;
#ifdef DEBUG
#endif
#if 0
/* GRR: add 8-bit support */
"screen depth %d not supported (only 16-, 24- or 32-bit TrueColor)\n",
depth);
return 2;
}
#else
int visuals_matched = 0;
depth))
/* 24-bit first */
if (visuals_matched == 0) {
/* GRR: add 15-, 16- and 32-bit TrueColor visuals (also DirectColor?) */
" 24-bit visuals found\n", depth);
return 2;
}
/*
colormap_size = visual_list[0].colormap_size;
visual_class = visual->class;
visualID = XVisualIDFromVisual(visual);
*/
} else {
}
#endif
if (!colormap) {
return 2;
}
}
} else if (depth > 16) {
#define NO_24BIT_MASKS
#ifdef NO_24BIT_MASKS
#else
#endif
}
return 2;
}
/*---------------------------------------------------------------------------
Finally, create the window.
---------------------------------------------------------------------------*/
if (have_nondefault_visual) {
attr.background_pixel = 0;
}
return 2;
} else
have_window = TRUE;
if (depth == 8)
pWindowName = NULL;
/* OK if either hints allocation fails; XSetWMProperties() allows NULLs */
/* window will not be resizable */
}
/* wm_hints->icon_pixmap = icon_pixmap; */
}
/* various properties and hints no longer needed; free memory */
if (pWindowName)
if (pIconName)
if (size_hints)
if (wm_hints)
/*---------------------------------------------------------------------------
Fill window with the specified background color.
---------------------------------------------------------------------------*/
} else if (depth == 16) {
} else /* depth == 8 */ {
/* GRR: add 8-bit support */
}
/*---------------------------------------------------------------------------
Wait for first Expose event to do any drawing, then flush.
---------------------------------------------------------------------------*/
do
XNextEvent(display, &e);
/*---------------------------------------------------------------------------
Allocate memory for the X- and display-specific version of the image.
---------------------------------------------------------------------------*/
pad = 32;
} else if (depth == 16) {
pad = 16;
} else /* depth == 8 */ {
pad = 8;
}
if (!xdata) {
return 4;
}
if (!ximage) {
return 3;
}
/* to avoid testing the byte order every pixel (or doubling the size of
* the drawing routine with a giant if-test), we arbitrarily set the byte
* order to MSBFirst and let Xlib worry about inverting things on little-
* efficient approach (the giant if-test would be better), but in the
* interest of clarity, we take the easy way out... */
return 0;
} /* end function rpng_x_create_window() */
static int rpng_x_display_image(void)
{
char *dest;
uch r, g, b, a;
/* int bpp = ximage->bits_per_pixel; */
if (image_channels == 3) {
for (i = image_width; i > 0; --i) {
#ifdef NO_24BIT_MASKS
/* recall that we set ximage->byte_order = MSBFirst above */
/* GRR BUG: this assumes bpp == 32, but may be 24: */
#else
/* recall that we set ximage->byte_order = MSBFirst above */
#endif
}
} else /* if (image_channels == 4) */ {
for (i = image_width; i > 0; --i) {
r = *src++;
g = *src++;
b = *src++;
a = *src++;
if (a == 255) {
red = r;
green = g;
blue = b;
} else if (a == 0) {
} else {
/* this macro (from png.h) composites the foreground
* and background values and puts the result into the
* first argument */
}
/* recall that we set ximage->byte_order = MSBFirst above */
}
}
/* display after every 16 lines */
}
}
} else if (depth == 16) {
if (image_channels == 3) {
for (i = image_width; i > 0; --i) {
++src;
++src;
++src;
/* recall that we set ximage->byte_order = MSBFirst above */
}
} else /* if (image_channels == 4) */ {
for (i = image_width; i > 0; --i) {
r = *src++;
g = *src++;
b = *src++;
a = *src++;
if (a == 255) {
} else if (a == 0) {
} else {
/* this macro (from png.h) composites the foreground
* and background values and puts the result back into
* the first argument (== fg byte here: safe) */
alpha_composite(r, r, a, bg_red);
alpha_composite(g, g, a, bg_green);
alpha_composite(b, b, a, bg_blue);
}
/* recall that we set ximage->byte_order = MSBFirst above */
}
}
/* display after every 16 lines */
}
}
} else /* depth == 8 */ {
/* GRR: add 8-bit support */
}
if (lastrow < image_height) {
}
return 0;
}
static void rpng_x_cleanup(void)
{
if (image_data) {
image_data = NULL;
}
if (ximage) {
}
}
if (have_gc)
if (have_window)
if (have_colormap)
}
{
int i;
for (i = 31; i >= 0; --i) {
if (u32val & 0x80000000L)
break;
u32val <<= 1;
}
return i;
}