/*---------------------------------------------------------------------------
rpng - simple PNG display program rpng-win.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 32-bit Windows; it may compile under 16-bit Windows
with a little tweaking (or maybe not).
to do:
- handle quoted command-line args (especially filenames with spaces)
- have minimum window width: oh well
- use %.1023s to simplify truncation of title-bar string?
---------------------------------------------------------------------------
Changelog:
- 1.00: initial public release
match; switched to png_jmpbuf() macro
- 1.02: added extra set of parentheses to png_jmpbuf() macro; fixed
command-line parsing bug
- 1.10: enabled "message window"/console (thanks to David Geldreich)
---------------------------------------------------------------------------
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>
#include <windows.h>
#include <conio.h> /* only for _getch() */
/* #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_win_display_image(void);
static void rpng_win_cleanup(void);
static char *filename;
static char *bgstr;
static double display_exponent;
static int image_channels;
/* Windows-specific variables */
{
int argc = 0;
int error = 0;
/* First reenable console output, which normally goes to the bit bucket
* for windowed apps. Closing the console window will terminate the
* app. Thanks to David.Geldreich@realviz.com for supplying the magical
* incantation. */
AllocConsole();
/* Next 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. And
* yes, these ifdefs are completely wasted in a Windows program... */
#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
/* Windows really hates command lines, so we have to set up our own argv.
* Note that we do NOT bother with quoted arguments here, so don't use
* filenames with spaces in 'em! */
p = cmd;
for (;;) {
if (*p == ' ')
while (*++p == ' ')
;
/* now p points at the first non-space after some spaces */
if (*p == '\0')
break; /* nothing after the spaces: done */
while (*q && *q != ' ')
++q;
/* now q points at a space or the end of the string */
if (*q == '\0')
break; /* last argv already terminated; quit */
*q = '\0'; /* change space to terminator */
p = q + 1;
}
/* Now parse the command line for options and the PNG filename. */
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;
}
if (error)
}
/* usage screen */
if (error) {
int ch;
"Usage: %s [-gamma exp] [-bgcolor bg] file.png\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 after image is displayed to quit.\n"
"Press Q or Esc to quit this usage screen.\n"
do
exit(1);
} else {
"\n [console window: closing this window will terminate %s]\n\n",
PROGNAME);
}
/* 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)
": libpng error while checking for background color\n");
exit(2);
}
/* do the basic Windows initialization stuff, make the window and fill it
* with the background color */
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_win_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");
}
/* OK, we're done: clean up all image and Windows resources and go away */
}
{
ulg i, j;
/*---------------------------------------------------------------------------
Allocate memory for the display-specific version of the image (round up
to multiple of 4 for Windows DIB).
---------------------------------------------------------------------------*/
{
return 4; /* fail */
}
/*---------------------------------------------------------------------------
Initialize the DIB. Negative height means to use top-down BMP ordering
(must be uncompressed, but that's what we want). Bit count of 1, 4 or 8
implies a colormap of RGBX quads, but 24-bit BMPs just use B,G,R values
directly => wimage_data begins immediately after BMP header.
---------------------------------------------------------------------------*/
bmih->biCompression = 0;
/*---------------------------------------------------------------------------
Fill in background color (black by default); data are in BGR order.
---------------------------------------------------------------------------*/
for (j = 0; j < image_height; ++j) {
for (i = image_width; i > 0; --i) {
}
}
/*---------------------------------------------------------------------------
Set the window parameters.
---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------
Finally, create the window.
---------------------------------------------------------------------------*/
return 0;
} /* end function rpng_win_create_window() */
static int rpng_win_display_image()
{
uch r, g, b, a;
/*---------------------------------------------------------------------------
Blast image data to buffer. This whole routine takes place before the
message loop begins, so there's no real point in any pseudo-progressive
display...
---------------------------------------------------------------------------*/
if (image_channels == 3) {
for (i = image_width; i > 0; --i) {
r = *src++;
g = *src++;
b = *src++;
*dest++ = b;
*dest++ = g; /* note reverse order */
*dest++ = r;
}
} else /* if (image_channels == 4) */ {
for (i = image_width; i > 0; --i) {
r = *src++;
g = *src++;
b = *src++;
a = *src++;
if (a == 255) {
*dest++ = b;
*dest++ = g;
*dest++ = r;
} else if (a == 0) {
} else {
/* this macro (copied from png.h) composites the
* foreground and background values and puts the
* result into the first argument; there are no
* side effects with the first argument */
}
}
}
/* display after every 16 lines */
}
}
if (lastrow < image_height) {
}
/*
last param determines whether or not background is wiped before paint
InvalidateRect(global_hwnd, NULL, TRUE);
UpdateWindow(global_hwnd);
*/
return 0;
}
static void rpng_win_cleanup()
{
if (image_data) {
image_data = NULL;
}
if (dib) {
}
}
{
int rc;
switch (iMsg) {
case WM_CREATE:
/* one-time processing here, if any */
return 0;
case WM_PAINT:
/* dest */
/* source */
0, 0, image_width, image_height,
/* iUsage: no clue */
0, SRCCOPY);
return 0;
/* wait for the user to tell us when to quit */
case WM_CHAR:
switch (wP) { /* only need one, so ignore repeat count */
case 'q':
case 'Q':
case 0x1B: /* Esc key */
PostQuitMessage(0);
}
return 0;
case WM_LBUTTONDOWN: /* another way of quitting */
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
}