1 /*---------------------------------------------------------------------------
3 rpng - simple PNG display program rpng-x.c
5 This program decodes and displays PNG images, with gamma correction and
6 optionally with a user-specified background color (in case the image has
7 transparency). It is very nearly the most basic PNG viewer possible.
8 This version is for the X Window System (tested by author under Unix and
9 by Martin Zinser under OpenVMS; may work under OS/2 with some tweaking).
12 - 8-bit (colormapped) X support
13 - use %.1023s to simplify truncation of title-bar string?
15 ---------------------------------------------------------------------------
18 - 1.01: initial public release
19 - 1.02: modified to allow abbreviated options; fixed long/ulong mis-
20 match; switched to png_jmpbuf() macro
21 - 1.10: added support for non-default visuals; fixed X pixel-conversion
22 - 1.11: added extra set of parentheses to png_jmpbuf() macro; fixed
23 command-line parsing bug
24 - 1.12: fixed some small X memory leaks (thanks to François Petitjean)
25 - 1.13: fixed XFreeGC() crash bug (thanks to Patrick Welche)
26 - 1.14: added support for X resources (thanks to Gerhard Niklasch)
27 - 2.00: dual-licensed (added GNU GPL)
28 - 2.01: fixed improper display of usage screen on PNG error(s)
29 - 2.02: Added "void(argc);" statement to quiet pedantic compiler warnings
30 about unused variable (GR-P)
32 ---------------------------------------------------------------------------
34 Copyright (c) 1998-2008 Greg Roelofs. All rights reserved.
36 This software is provided "as is," without warranty of any kind,
37 express or implied. In no event shall the author or contributors
38 be held liable for any damages arising in any way from the use of
41 The contents of this file are DUAL-LICENSED. You may modify and/or
42 redistribute this software according to the terms of one of the
43 following two licenses (at your option):
46 LICENSE 1 ("BSD-like with advertising clause"):
48 Permission is granted to anyone to use this software for any purpose,
49 including commercial applications, and to alter it and redistribute
50 it freely, subject to the following restrictions:
52 1. Redistributions of source code must retain the above copyright
53 notice, disclaimer, and this list of conditions.
54 2. Redistributions in binary form must reproduce the above copyright
55 notice, disclaimer, and this list of conditions in the documenta-
56 tion and/or other materials provided with the distribution.
57 3. All advertising materials mentioning features or use of this
58 software must display the following acknowledgment:
60 This product includes software developed by Greg Roelofs
61 and contributors for the book, "PNG: The Definitive Guide,"
62 published by O'Reilly and Associates.
65 LICENSE 2 (GNU GPL v2 or later):
67 This program is free software; you can redistribute it and/or modify
68 it under the terms of the GNU General Public License as published by
69 the Free Software Foundation; either version 2 of the License, or
70 (at your option) any later version.
72 This program is distributed in the hope that it will be useful,
73 but WITHOUT ANY WARRANTY; without even the implied warranty of
74 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75 GNU General Public License for more details.
77 You should have received a copy of the GNU General Public License
78 along with this program; if not, write to the Free Software Foundation,
79 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
81 ---------------------------------------------------------------------------*/
83 #define PROGNAME "rpng-x"
84 #define LONGNAME "Simple PNG Viewer for X"
85 #define VERSION "2.02 of 15 June 2014"
86 #define RESNAME "rpng" /* our X resource application name */
87 #define RESCLASS "Rpng" /* our X resource class name */
94 #include <X11/Xutil.h>
96 #include <X11/keysym.h>
98 /* #define DEBUG : this enables the Trace() macros */
100 #include "readpng.h" /* typedefs, common macros, readpng prototypes */
103 /* could just include png.h, but this macro is the only thing we need
104 * (name and typedefs changed to local versions); note that side effects
105 * only happen with alpha (which could easily be avoided with
106 * "ush acopy = (alpha);") */
108 #define alpha_composite(composite, fg, alpha, bg) { \
109 ush temp = ((ush)(fg)*(ush)(alpha) + \
110 (ush)(bg)*(ush)(255 - (ush)(alpha)) + (ush)128); \
111 (composite) = (uch)((temp + (temp >> 8)) >> 8); \
115 /* local prototypes */
116 static int rpng_x_create_window(void);
117 static int rpng_x_display_image(void);
118 static void rpng_x_cleanup(void);
119 static int rpng_x_msb(ulg u32val
);
122 static char titlebar
[1024], *window_name
= titlebar
;
123 static char *appname
= LONGNAME
;
124 static char *icon_name
= PROGNAME
;
125 static char *res_name
= RESNAME
;
126 static char *res_class
= RESCLASS
;
127 static char *filename
;
131 static uch bg_red
=0, bg_green
=0, bg_blue
=0;
133 static double display_exponent
;
135 static ulg image_width
, image_height
, image_rowbytes
;
136 static int image_channels
;
137 static uch
*image_data
;
139 /* X-specific variables */
140 static char *displayname
;
141 static XImage
*ximage
;
142 static Display
*display
;
144 static Visual
*visual
;
145 static XVisualInfo
*visual_list
;
146 static int RShift
, GShift
, BShift
;
147 static ulg RMask
, GMask
, BMask
;
148 static Window window
;
150 static Colormap colormap
;
152 static int have_nondefault_visual
= FALSE
;
153 static int have_colormap
= FALSE
;
154 static int have_window
= FALSE
;
155 static int have_gc
= FALSE
;
157 ulg numcolors=0, pixels[256];
158 ush reds[256], greens[256], blues[256];
164 int main(int argc
, char **argv
)
173 double LUT_exponent
; /* just the lookup table */
174 double CRT_exponent
= 2.2; /* just the monitor */
175 double default_display_exponent
; /* whole display system */
180 displayname
= (char *)NULL
;
181 filename
= (char *)NULL
;
184 /* First set the default value for our display-system exponent, i.e.,
185 * the product of the CRT exponent and the exponent corresponding to
186 * the frame-buffer's lookup table (LUT), if any. This is not an
187 * exhaustive list of LUT values (e.g., OpenStep has a lot of weird
188 * ones), but it should cover 99% of the current possibilities. */
191 LUT_exponent
= 1.0 / 2.2;
193 if (some_next_function_that_returns_gamma(&next_gamma))
194 LUT_exponent = 1.0 / next_gamma;
197 LUT_exponent
= 1.0 / 1.7;
198 /* there doesn't seem to be any documented function to get the
199 * "gamma" value, so we do it the hard way */
200 infile
= fopen("/etc/config/system.glGammaVal", "r");
204 fgets(tmpline
, 80, infile
);
206 sgi_gamma
= atof(tmpline
);
208 LUT_exponent
= 1.0 / sgi_gamma
;
210 #elif defined(Macintosh)
211 LUT_exponent
= 1.8 / 2.61;
213 if (some_mac_function_that_returns_gamma(&mac_gamma))
214 LUT_exponent = mac_gamma / 2.61;
217 LUT_exponent
= 1.0; /* assume no LUT: most PCs */
220 /* the defaults above give 1.0, 1.3, 1.5 and 2.2, respectively: */
221 default_display_exponent
= LUT_exponent
* CRT_exponent
;
224 /* If the user has set the SCREEN_GAMMA environment variable as suggested
225 * (somewhat imprecisely) in the libpng documentation, use that; otherwise
226 * use the default value we just calculated. Either way, the user may
227 * override this via a command-line option. */
229 if ((p
= getenv("SCREEN_GAMMA")) != NULL
)
230 display_exponent
= atof(p
);
232 display_exponent
= default_display_exponent
;
235 /* Now parse the command line for options and the PNG filename. */
237 while (*++argv
&& !error
) {
238 if (!strncmp(*argv
, "-display", 2)) {
243 } else if (!strncmp(*argv
, "-gamma", 2)) {
247 display_exponent
= atof(*argv
);
248 if (display_exponent
<= 0.0)
251 } else if (!strncmp(*argv
, "-bgcolor", 2)) {
256 if (strlen(bgstr
) != 7 || bgstr
[0] != '#')
264 if (argv
[1]) /* shouldn't be any more args after filename */
267 ++error
; /* not expecting any other options */
275 /* print usage screen if any errors up to this point */
278 fprintf(stderr
, "\n%s %s: %s\n", PROGNAME
, VERSION
, appname
);
279 readpng_version_info();
281 "Usage: %s [-display xdpy] [-gamma exp] [-bgcolor bg] file.png\n"
282 " xdpy\tname of the target X display (e.g., ``hostname:0'')\n"
283 " exp \ttransfer-function exponent (``gamma'') of the display\n"
284 "\t\t system in floating-point format (e.g., ``%.1f''); equal\n",
285 PROGNAME
, default_display_exponent
);
288 "\t\t to the product of the lookup-table exponent (varies)\n"
289 "\t\t and the CRT exponent (usually 2.2); must be positive\n"
290 " bg \tdesired background color in 7-character hex RGB format\n"
291 "\t\t (e.g., ``#ff7700'' for orange: same as HTML colors);\n"
292 "\t\t used with transparent images\n"
293 "\nPress Q, Esc or mouse button 1 (within image window, after image\n"
294 "is displayed) to quit.\n");
299 if (!(infile
= fopen(filename
, "rb"))) {
300 fprintf(stderr
, PROGNAME
": can't open PNG file [%s]\n", filename
);
303 if ((rc
= readpng_init(infile
, &image_width
, &image_height
)) != 0) {
306 fprintf(stderr
, PROGNAME
307 ": [%s] is not a PNG file: incorrect signature\n",
311 fprintf(stderr
, PROGNAME
312 ": [%s] has bad IHDR (libpng longjmp)\n", filename
);
315 fprintf(stderr
, PROGNAME
": insufficient memory\n");
318 fprintf(stderr
, PROGNAME
319 ": unknown readpng_init() error\n");
324 display
= XOpenDisplay(displayname
);
326 readpng_cleanup(TRUE
);
327 fprintf(stderr
, PROGNAME
": can't open X display [%s]\n",
328 displayname
? displayname
: "default");
338 fprintf(stderr
, PROGNAME
": aborting.\n");
343 /* set the title-bar string, but make sure buffer doesn't overflow */
345 alen
= strlen(appname
);
346 flen
= strlen(filename
);
347 if (alen
+ flen
+ 3 > 1023)
348 sprintf(titlebar
, "%s: ...%s", appname
, filename
+(alen
+flen
+6-1023));
350 sprintf(titlebar
, "%s: %s", appname
, filename
);
353 /* if the user didn't specify a background color on the command line,
354 * check for one in the PNG file--if not, the initialized values of 0
355 * (black) will be used */
358 unsigned r
, g
, b
; /* this approach quiets compiler warnings */
360 sscanf(bgstr
+1, "%2x%2x%2x", &r
, &g
, &b
);
364 } else if (readpng_get_bgcolor(&bg_red
, &bg_green
, &bg_blue
) > 1) {
365 readpng_cleanup(TRUE
);
366 fprintf(stderr
, PROGNAME
367 ": libpng error while checking for background color\n");
372 /* do the basic X initialization stuff, make the window and fill it
373 * with the background color */
375 if (rpng_x_create_window())
379 /* decode the image, all at once */
381 Trace((stderr
, "calling readpng_get_image()\n"))
382 image_data
= readpng_get_image(display_exponent
, &image_channels
,
384 Trace((stderr
, "done with readpng_get_image()\n"))
387 /* done with PNG file, so clean up to minimize memory usage (but do NOT
388 * nuke image_data!) */
390 readpng_cleanup(FALSE
);
394 fprintf(stderr
, PROGNAME
": unable to decode PNG image\n");
399 /* display image (composite with background if requested) */
401 Trace((stderr
, "calling rpng_x_display_image()\n"))
402 if (rpng_x_display_image()) {
406 Trace((stderr
, "done with rpng_x_display_image()\n"))
409 /* wait for the user to tell us when to quit */
412 "Done. Press Q, Esc or mouse button 1 (within image window) to quit.\n");
416 XNextEvent(display
, &e
);
417 while (!(e
.type
== ButtonPress
&& e
.xbutton
.button
== Button1
) &&
418 !(e
.type
== KeyPress
&& /* v--- or 1 for shifted keys */
419 ((k
= XLookupKeysym(&e
.xkey
, 0)) == XK_q
|| k
== XK_Escape
) ));
422 /* OK, we're done: clean up all image and X resources and go away */
426 (void)argc
; /* Unused */
435 static int rpng_x_create_window(void)
438 int need_colormap
= FALSE
;
445 XSetWindowAttributes attr
;
446 XTextProperty windowName
, *pWindowName
= &windowName
;
447 XTextProperty iconName
, *pIconName
= &iconName
;
448 XVisualInfo visual_info
;
449 XSizeHints
*size_hints
;
451 XClassHint
*class_hints
;
454 screen
= DefaultScreen(display
);
455 depth
= DisplayPlanes(display
, screen
);
456 root
= RootWindow(display
, screen
);
459 XSynchronize(display
, True
);
463 /* GRR: add 8-bit support */
464 if (/* depth != 8 && */ depth
!= 16 && depth
!= 24 && depth
!= 32) {
466 "screen depth %d not supported (only 16-, 24- or 32-bit TrueColor)\n",
471 XMatchVisualInfo(display
, screen
, depth
,
472 (depth
== 8)? PseudoColor
: TrueColor
, &visual_info
);
473 visual
= visual_info
.visual
;
475 if (depth
!= 16 && depth
!= 24 && depth
!= 32) {
476 int visuals_matched
= 0;
478 Trace((stderr
, "default depth is %d: checking other visuals\n",
482 visual_info
.screen
= screen
;
483 visual_info
.depth
= 24;
484 visual_list
= XGetVisualInfo(display
,
485 VisualScreenMask
| VisualDepthMask
, &visual_info
, &visuals_matched
);
486 if (visuals_matched
== 0) {
487 /* GRR: add 15-, 16- and 32-bit TrueColor visuals (also DirectColor?) */
488 fprintf(stderr
, "default screen depth %d not supported, and no"
489 " 24-bit visuals found\n", depth
);
492 Trace((stderr
, "XGetVisualInfo() returned %d 24-bit visuals\n",
494 visual
= visual_list
[0].visual
;
495 depth
= visual_list
[0].depth
;
497 colormap_size = visual_list[0].colormap_size;
498 visual_class = visual->class;
499 visualID = XVisualIDFromVisual(visual);
501 have_nondefault_visual
= TRUE
;
502 need_colormap
= TRUE
;
504 XMatchVisualInfo(display
, screen
, depth
, TrueColor
, &visual_info
);
505 visual
= visual_info
.visual
;
509 RMask
= visual
->red_mask
;
510 GMask
= visual
->green_mask
;
511 BMask
= visual
->blue_mask
;
513 /* GRR: add/check 8-bit support */
514 if (depth
== 8 || need_colormap
) {
515 colormap
= XCreateColormap(display
, root
, visual
, AllocNone
);
517 fprintf(stderr
, "XCreateColormap() failed\n");
520 have_colormap
= TRUE
;
522 if (depth
== 15 || depth
== 16) {
523 RShift
= 15 - rpng_x_msb(RMask
); /* these are right-shifts */
524 GShift
= 15 - rpng_x_msb(GMask
);
525 BShift
= 15 - rpng_x_msb(BMask
);
526 } else if (depth
> 16) {
527 #define NO_24BIT_MASKS
528 #ifdef NO_24BIT_MASKS
529 RShift
= rpng_x_msb(RMask
) - 7; /* these are left-shifts */
530 GShift
= rpng_x_msb(GMask
) - 7;
531 BShift
= rpng_x_msb(BMask
) - 7;
533 RShift
= 7 - rpng_x_msb(RMask
); /* these are right-shifts, too */
534 GShift
= 7 - rpng_x_msb(GMask
);
535 BShift
= 7 - rpng_x_msb(BMask
);
538 if (depth
>= 15 && (RShift
< 0 || GShift
< 0 || BShift
< 0)) {
539 fprintf(stderr
, "rpng internal logic error: negative X shift(s)!\n");
543 /*---------------------------------------------------------------------------
544 Finally, create the window.
545 ---------------------------------------------------------------------------*/
547 attr
.backing_store
= Always
;
548 attr
.event_mask
= ExposureMask
| KeyPressMask
| ButtonPressMask
;
549 attrmask
= CWBackingStore
| CWEventMask
;
550 if (have_nondefault_visual
) {
551 attr
.colormap
= colormap
;
552 attr
.background_pixel
= 0;
553 attr
.border_pixel
= 1;
554 attrmask
|= CWColormap
| CWBackPixel
| CWBorderPixel
;
557 window
= XCreateWindow(display
, root
, 0, 0, image_width
, image_height
, 0,
558 depth
, InputOutput
, visual
, attrmask
, &attr
);
560 if (window
== None
) {
561 fprintf(stderr
, "XCreateWindow() failed\n");
567 XSetWindowColormap(display
, window
, colormap
);
569 if (!XStringListToTextProperty(&window_name
, 1, pWindowName
))
571 if (!XStringListToTextProperty(&icon_name
, 1, pIconName
))
574 /* OK if any hints allocation fails; XSetWMProperties() allows NULLs */
576 if ((size_hints
= XAllocSizeHints()) != NULL
) {
577 /* window will not be resizable */
578 size_hints
->flags
= PMinSize
| PMaxSize
;
579 size_hints
->min_width
= size_hints
->max_width
= (int)image_width
;
580 size_hints
->min_height
= size_hints
->max_height
= (int)image_height
;
583 if ((wm_hints
= XAllocWMHints()) != NULL
) {
584 wm_hints
->initial_state
= NormalState
;
585 wm_hints
->input
= True
;
586 /* wm_hints->icon_pixmap = icon_pixmap; */
587 wm_hints
->flags
= StateHint
| InputHint
/* | IconPixmapHint */ ;
590 if ((class_hints
= XAllocClassHint()) != NULL
) {
591 class_hints
->res_name
= res_name
;
592 class_hints
->res_class
= res_class
;
595 XSetWMProperties(display
, window
, pWindowName
, pIconName
, NULL
, 0,
596 size_hints
, wm_hints
, class_hints
);
598 /* various properties and hints no longer needed; free memory */
600 XFree(pWindowName
->value
);
602 XFree(pIconName
->value
);
610 XMapWindow(display
, window
);
612 gc
= XCreateGC(display
, window
, 0, &gcvalues
);
615 /*---------------------------------------------------------------------------
616 Fill window with the specified background color.
617 ---------------------------------------------------------------------------*/
619 if (depth
== 24 || depth
== 32) {
620 bg_pixel
= ((ulg
)bg_red
<< RShift
) |
621 ((ulg
)bg_green
<< GShift
) |
622 ((ulg
)bg_blue
<< BShift
);
623 } else if (depth
== 16) {
624 bg_pixel
= ((((ulg
)bg_red
<< 8) >> RShift
) & RMask
) |
625 ((((ulg
)bg_green
<< 8) >> GShift
) & GMask
) |
626 ((((ulg
)bg_blue
<< 8) >> BShift
) & BMask
);
627 } else /* depth == 8 */ {
629 /* GRR: add 8-bit support */
633 XSetForeground(display
, gc
, bg_pixel
);
634 XFillRectangle(display
, window
, gc
, 0, 0, image_width
, image_height
);
636 /*---------------------------------------------------------------------------
637 Wait for first Expose event to do any drawing, then flush.
638 ---------------------------------------------------------------------------*/
641 XNextEvent(display
, &e
);
642 while (e
.type
!= Expose
|| e
.xexpose
.count
);
646 /*---------------------------------------------------------------------------
647 Allocate memory for the X- and display-specific version of the image.
648 ---------------------------------------------------------------------------*/
650 if (depth
== 24 || depth
== 32) {
651 xdata
= (uch
*)malloc(4*image_width
*image_height
);
653 } else if (depth
== 16) {
654 xdata
= (uch
*)malloc(2*image_width
*image_height
);
656 } else /* depth == 8 */ {
657 xdata
= (uch
*)malloc(image_width
*image_height
);
662 fprintf(stderr
, PROGNAME
": unable to allocate image memory\n");
666 ximage
= XCreateImage(display
, visual
, depth
, ZPixmap
, 0,
667 (char *)xdata
, image_width
, image_height
, pad
, 0);
670 fprintf(stderr
, PROGNAME
": XCreateImage() failed\n");
675 /* to avoid testing the byte order every pixel (or doubling the size of
676 * the drawing routine with a giant if-test), we arbitrarily set the byte
677 * order to MSBFirst and let Xlib worry about inverting things on little-
678 * endian machines (like Linux/x86, old VAXen, etc.)--this is not the most
679 * efficient approach (the giant if-test would be better), but in the
680 * interest of clarity, we take the easy way out... */
682 ximage
->byte_order
= MSBFirst
;
686 } /* end function rpng_x_create_window() */
692 static int rpng_x_display_image(void)
697 ulg i
, row
, lastrow
= 0;
699 int ximage_rowbytes
= ximage
->bytes_per_line
;
700 /* int bpp = ximage->bits_per_pixel; */
703 Trace((stderr
, "beginning display loop (image_channels == %d)\n",
705 Trace((stderr
, " (width = %ld, rowbytes = %ld, ximage_rowbytes = %d)\n",
706 image_width
, image_rowbytes
, ximage_rowbytes
))
707 Trace((stderr
, " (bpp = %d)\n", ximage
->bits_per_pixel
))
708 Trace((stderr
, " (byte_order = %s)\n", ximage
->byte_order
== MSBFirst
?
709 "MSBFirst" : (ximage
->byte_order
== LSBFirst
? "LSBFirst" : "unknown")))
711 if (depth
== 24 || depth
== 32) {
712 ulg red
, green
, blue
;
714 for (lastrow
= row
= 0; row
< image_height
; ++row
) {
715 src
= image_data
+ row
*image_rowbytes
;
716 dest
= ximage
->data
+ row
*ximage_rowbytes
;
717 if (image_channels
== 3) {
718 for (i
= image_width
; i
> 0; --i
) {
722 #ifdef NO_24BIT_MASKS
723 pixel
= (red
<< RShift
) |
726 /* recall that we set ximage->byte_order = MSBFirst above */
727 /* GRR BUG: this assumes bpp == 32, but may be 24: */
728 *dest
++ = (char)((pixel
>> 24) & 0xff);
729 *dest
++ = (char)((pixel
>> 16) & 0xff);
730 *dest
++ = (char)((pixel
>> 8) & 0xff);
731 *dest
++ = (char)( pixel
& 0xff);
733 red
= (RShift
< 0)? red
<< (-RShift
) : red
>> RShift
;
734 green
= (GShift
< 0)? green
<< (-GShift
) : green
>> GShift
;
735 blue
= (BShift
< 0)? blue
<< (-BShift
) : blue
>> BShift
;
736 pixel
= (red
& RMask
) | (green
& GMask
) | (blue
& BMask
);
737 /* recall that we set ximage->byte_order = MSBFirst above */
738 *dest
++ = (char)((pixel
>> 24) & 0xff);
739 *dest
++ = (char)((pixel
>> 16) & 0xff);
740 *dest
++ = (char)((pixel
>> 8) & 0xff);
741 *dest
++ = (char)( pixel
& 0xff);
744 } else /* if (image_channels == 4) */ {
745 for (i
= image_width
; i
> 0; --i
) {
759 /* this macro (from png.h) composites the foreground
760 * and background values and puts the result into the
762 alpha_composite(red
, r
, a
, bg_red
);
763 alpha_composite(green
, g
, a
, bg_green
);
764 alpha_composite(blue
, b
, a
, bg_blue
);
766 pixel
= (red
<< RShift
) |
769 /* recall that we set ximage->byte_order = MSBFirst above */
770 *dest
++ = (char)((pixel
>> 24) & 0xff);
771 *dest
++ = (char)((pixel
>> 16) & 0xff);
772 *dest
++ = (char)((pixel
>> 8) & 0xff);
773 *dest
++ = (char)( pixel
& 0xff);
776 /* display after every 16 lines */
777 if (((row
+1) & 0xf) == 0) {
778 XPutImage(display
, window
, gc
, ximage
, 0, (int)lastrow
, 0,
779 (int)lastrow
, image_width
, 16);
785 } else if (depth
== 16) {
786 ush red
, green
, blue
;
788 for (lastrow
= row
= 0; row
< image_height
; ++row
) {
789 src
= image_data
+ row
*image_rowbytes
;
790 dest
= ximage
->data
+ row
*ximage_rowbytes
;
791 if (image_channels
== 3) {
792 for (i
= image_width
; i
> 0; --i
) {
793 red
= ((ush
)(*src
) << 8);
795 green
= ((ush
)(*src
) << 8);
797 blue
= ((ush
)(*src
) << 8);
799 pixel
= ((red
>> RShift
) & RMask
) |
800 ((green
>> GShift
) & GMask
) |
801 ((blue
>> BShift
) & BMask
);
802 /* recall that we set ximage->byte_order = MSBFirst above */
803 *dest
++ = (char)((pixel
>> 8) & 0xff);
804 *dest
++ = (char)( pixel
& 0xff);
806 } else /* if (image_channels == 4) */ {
807 for (i
= image_width
; i
> 0; --i
) {
814 green
= ((ush
)g
<< 8);
815 blue
= ((ush
)b
<< 8);
817 red
= ((ush
)bg_red
<< 8);
818 green
= ((ush
)bg_green
<< 8);
819 blue
= ((ush
)bg_blue
<< 8);
821 /* this macro (from png.h) composites the foreground
822 * and background values and puts the result back into
823 * the first argument (== fg byte here: safe) */
824 alpha_composite(r
, r
, a
, bg_red
);
825 alpha_composite(g
, g
, a
, bg_green
);
826 alpha_composite(b
, b
, a
, bg_blue
);
828 green
= ((ush
)g
<< 8);
829 blue
= ((ush
)b
<< 8);
831 pixel
= ((red
>> RShift
) & RMask
) |
832 ((green
>> GShift
) & GMask
) |
833 ((blue
>> BShift
) & BMask
);
834 /* recall that we set ximage->byte_order = MSBFirst above */
835 *dest
++ = (char)((pixel
>> 8) & 0xff);
836 *dest
++ = (char)( pixel
& 0xff);
839 /* display after every 16 lines */
840 if (((row
+1) & 0xf) == 0) {
841 XPutImage(display
, window
, gc
, ximage
, 0, (int)lastrow
, 0,
842 (int)lastrow
, image_width
, 16);
848 } else /* depth == 8 */ {
850 /* GRR: add 8-bit support */
854 Trace((stderr
, "calling final XPutImage()\n"))
855 if (lastrow
< image_height
) {
856 XPutImage(display
, window
, gc
, ximage
, 0, (int)lastrow
, 0,
857 (int)lastrow
, image_width
, image_height
-lastrow
);
867 static void rpng_x_cleanup(void)
876 free(ximage
->data
); /* we allocated it, so we free it */
877 ximage
->data
= (char *)NULL
; /* instead of XDestroyImage() */
879 XDestroyImage(ximage
);
884 XFreeGC(display
, gc
);
887 XDestroyWindow(display
, window
);
890 XFreeColormap(display
, colormap
);
892 if (have_nondefault_visual
)
900 static int rpng_x_msb(ulg u32val
)
904 for (i
= 31; i
>= 0; --i
) {
905 if (u32val
& 0x80000000L
)