Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1 | /*--------------------------------------------------------------------------- |
| 2 | |
| 3 | rpng - simple PNG display program rpng-x.c |
| 4 | |
| 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. |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 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). |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 10 | |
| 11 | to do: |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 12 | - 8-bit (colormapped) X support |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 13 | - use %.1023s to simplify truncation of title-bar string? |
| 14 | |
| 15 | --------------------------------------------------------------------------- |
| 16 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 17 | Changelog: |
| 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 |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 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) |
Glenn Randers-Pehrson | 91319c6 | 2014-07-04 11:46:17 -0500 | [diff] [blame] | 29 | - 2.02: Added "void(argc);" statement to quiet pedantic compiler warnings |
| 30 | about unused variable (GR-P) |
Glenn Randers-Pehrson | 11c25c0 | 2017-04-23 18:48:32 -0500 | [diff] [blame] | 31 | - 2.03: check for integer overflow (Glenn R-P) |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 32 | |
| 33 | --------------------------------------------------------------------------- |
| 34 | |
Glenn Randers-Pehrson | 11c25c0 | 2017-04-23 18:48:32 -0500 | [diff] [blame] | 35 | Copyright (c) 1998-2008, 2017 Greg Roelofs. All rights reserved. |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 36 | |
| 37 | This software is provided "as is," without warranty of any kind, |
| 38 | express or implied. In no event shall the author or contributors |
| 39 | be held liable for any damages arising in any way from the use of |
| 40 | this software. |
| 41 | |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 42 | The contents of this file are DUAL-LICENSED. You may modify and/or |
| 43 | redistribute this software according to the terms of one of the |
| 44 | following two licenses (at your option): |
| 45 | |
| 46 | |
| 47 | LICENSE 1 ("BSD-like with advertising clause"): |
| 48 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 49 | Permission is granted to anyone to use this software for any purpose, |
| 50 | including commercial applications, and to alter it and redistribute |
| 51 | it freely, subject to the following restrictions: |
| 52 | |
| 53 | 1. Redistributions of source code must retain the above copyright |
| 54 | notice, disclaimer, and this list of conditions. |
| 55 | 2. Redistributions in binary form must reproduce the above copyright |
| 56 | notice, disclaimer, and this list of conditions in the documenta- |
| 57 | tion and/or other materials provided with the distribution. |
| 58 | 3. All advertising materials mentioning features or use of this |
| 59 | software must display the following acknowledgment: |
| 60 | |
| 61 | This product includes software developed by Greg Roelofs |
| 62 | and contributors for the book, "PNG: The Definitive Guide," |
| 63 | published by O'Reilly and Associates. |
| 64 | |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 65 | |
| 66 | LICENSE 2 (GNU GPL v2 or later): |
| 67 | |
| 68 | This program is free software; you can redistribute it and/or modify |
| 69 | it under the terms of the GNU General Public License as published by |
| 70 | the Free Software Foundation; either version 2 of the License, or |
| 71 | (at your option) any later version. |
| 72 | |
| 73 | This program is distributed in the hope that it will be useful, |
| 74 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 75 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 76 | GNU General Public License for more details. |
| 77 | |
| 78 | You should have received a copy of the GNU General Public License |
| 79 | along with this program; if not, write to the Free Software Foundation, |
| 80 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 81 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 82 | ---------------------------------------------------------------------------*/ |
| 83 | |
| 84 | #define PROGNAME "rpng-x" |
| 85 | #define LONGNAME "Simple PNG Viewer for X" |
Glenn Randers-Pehrson | 91319c6 | 2014-07-04 11:46:17 -0500 | [diff] [blame] | 86 | #define VERSION "2.02 of 15 June 2014" |
Glenn Randers-Pehrson | 4292855 | 2010-03-15 08:38:06 -0500 | [diff] [blame] | 87 | #define RESNAME "rpng" /* our X resource application name */ |
| 88 | #define RESCLASS "Rpng" /* our X resource class name */ |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 89 | |
| 90 | #include <stdio.h> |
| 91 | #include <stdlib.h> |
| 92 | #include <string.h> |
| 93 | #include <time.h> |
| 94 | #include <X11/Xlib.h> |
| 95 | #include <X11/Xutil.h> |
| 96 | #include <X11/Xos.h> |
| 97 | #include <X11/keysym.h> |
| 98 | |
| 99 | /* #define DEBUG : this enables the Trace() macros */ |
| 100 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 101 | #include "readpng.h" /* typedefs, common macros, readpng prototypes */ |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 102 | |
| 103 | |
| 104 | /* could just include png.h, but this macro is the only thing we need |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 105 | * (name and typedefs changed to local versions); note that side effects |
| 106 | * only happen with alpha (which could easily be avoided with |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 107 | * "ush acopy = (alpha);") */ |
| 108 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 109 | #define alpha_composite(composite, fg, alpha, bg) { \ |
| 110 | ush temp = ((ush)(fg)*(ush)(alpha) + \ |
| 111 | (ush)(bg)*(ush)(255 - (ush)(alpha)) + (ush)128); \ |
| 112 | (composite) = (uch)((temp + (temp >> 8)) >> 8); \ |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | /* local prototypes */ |
| 117 | static int rpng_x_create_window(void); |
| 118 | static int rpng_x_display_image(void); |
| 119 | static void rpng_x_cleanup(void); |
| 120 | static int rpng_x_msb(ulg u32val); |
| 121 | |
| 122 | |
| 123 | static char titlebar[1024], *window_name = titlebar; |
| 124 | static char *appname = LONGNAME; |
| 125 | static char *icon_name = PROGNAME; |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 126 | static char *res_name = RESNAME; |
| 127 | static char *res_class = RESCLASS; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 128 | static char *filename; |
| 129 | static FILE *infile; |
| 130 | |
| 131 | static char *bgstr; |
| 132 | static uch bg_red=0, bg_green=0, bg_blue=0; |
| 133 | |
| 134 | static double display_exponent; |
| 135 | |
| 136 | static ulg image_width, image_height, image_rowbytes; |
| 137 | static int image_channels; |
| 138 | static uch *image_data; |
| 139 | |
| 140 | /* X-specific variables */ |
| 141 | static char *displayname; |
| 142 | static XImage *ximage; |
| 143 | static Display *display; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 144 | static int depth; |
| 145 | static Visual *visual; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 146 | static XVisualInfo *visual_list; |
| 147 | static int RShift, GShift, BShift; |
| 148 | static ulg RMask, GMask, BMask; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 149 | static Window window; |
| 150 | static GC gc; |
| 151 | static Colormap colormap; |
| 152 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 153 | static int have_nondefault_visual = FALSE; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 154 | static int have_colormap = FALSE; |
| 155 | static int have_window = FALSE; |
Glenn Randers-Pehrson | 1b8e567 | 2001-08-25 06:46:06 -0500 | [diff] [blame] | 156 | static int have_gc = FALSE; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 157 | /* |
| 158 | ulg numcolors=0, pixels[256]; |
| 159 | ush reds[256], greens[256], blues[256]; |
| 160 | */ |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | int main(int argc, char **argv) |
| 166 | { |
| 167 | #ifdef sgi |
| 168 | char tmpline[80]; |
| 169 | #endif |
| 170 | char *p; |
| 171 | int rc, alen, flen; |
| 172 | int error = 0; |
| 173 | int have_bg = FALSE; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 174 | double LUT_exponent; /* just the lookup table */ |
| 175 | double CRT_exponent = 2.2; /* just the monitor */ |
| 176 | double default_display_exponent; /* whole display system */ |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 177 | XEvent e; |
| 178 | KeySym k; |
| 179 | |
| 180 | |
| 181 | displayname = (char *)NULL; |
| 182 | filename = (char *)NULL; |
| 183 | |
| 184 | |
| 185 | /* First set the default value for our display-system exponent, i.e., |
| 186 | * the product of the CRT exponent and the exponent corresponding to |
| 187 | * the frame-buffer's lookup table (LUT), if any. This is not an |
| 188 | * exhaustive list of LUT values (e.g., OpenStep has a lot of weird |
| 189 | * ones), but it should cover 99% of the current possibilities. */ |
| 190 | |
| 191 | #if defined(NeXT) |
| 192 | LUT_exponent = 1.0 / 2.2; |
| 193 | /* |
| 194 | if (some_next_function_that_returns_gamma(&next_gamma)) |
| 195 | LUT_exponent = 1.0 / next_gamma; |
| 196 | */ |
| 197 | #elif defined(sgi) |
| 198 | LUT_exponent = 1.0 / 1.7; |
| 199 | /* there doesn't seem to be any documented function to get the |
| 200 | * "gamma" value, so we do it the hard way */ |
| 201 | infile = fopen("/etc/config/system.glGammaVal", "r"); |
| 202 | if (infile) { |
| 203 | double sgi_gamma; |
| 204 | |
| 205 | fgets(tmpline, 80, infile); |
| 206 | fclose(infile); |
| 207 | sgi_gamma = atof(tmpline); |
| 208 | if (sgi_gamma > 0.0) |
| 209 | LUT_exponent = 1.0 / sgi_gamma; |
| 210 | } |
| 211 | #elif defined(Macintosh) |
| 212 | LUT_exponent = 1.8 / 2.61; |
| 213 | /* |
| 214 | if (some_mac_function_that_returns_gamma(&mac_gamma)) |
| 215 | LUT_exponent = mac_gamma / 2.61; |
| 216 | */ |
| 217 | #else |
| 218 | LUT_exponent = 1.0; /* assume no LUT: most PCs */ |
| 219 | #endif |
| 220 | |
| 221 | /* the defaults above give 1.0, 1.3, 1.5 and 2.2, respectively: */ |
| 222 | default_display_exponent = LUT_exponent * CRT_exponent; |
| 223 | |
| 224 | |
| 225 | /* If the user has set the SCREEN_GAMMA environment variable as suggested |
| 226 | * (somewhat imprecisely) in the libpng documentation, use that; otherwise |
| 227 | * use the default value we just calculated. Either way, the user may |
| 228 | * override this via a command-line option. */ |
| 229 | |
| 230 | if ((p = getenv("SCREEN_GAMMA")) != NULL) |
| 231 | display_exponent = atof(p); |
| 232 | else |
| 233 | display_exponent = default_display_exponent; |
| 234 | |
| 235 | |
| 236 | /* Now parse the command line for options and the PNG filename. */ |
| 237 | |
| 238 | while (*++argv && !error) { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 239 | if (!strncmp(*argv, "-display", 2)) { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 240 | if (!*++argv) |
| 241 | ++error; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 242 | else |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 243 | displayname = *argv; |
| 244 | } else if (!strncmp(*argv, "-gamma", 2)) { |
| 245 | if (!*++argv) |
| 246 | ++error; |
| 247 | else { |
| 248 | display_exponent = atof(*argv); |
| 249 | if (display_exponent <= 0.0) |
| 250 | ++error; |
| 251 | } |
| 252 | } else if (!strncmp(*argv, "-bgcolor", 2)) { |
| 253 | if (!*++argv) |
| 254 | ++error; |
| 255 | else { |
| 256 | bgstr = *argv; |
| 257 | if (strlen(bgstr) != 7 || bgstr[0] != '#') |
Glenn Randers-Pehrson | af855e4 | 2011-05-07 10:52:49 -0500 | [diff] [blame] | 258 | ++error; |
| 259 | else |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 260 | have_bg = TRUE; |
| 261 | } |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 262 | } else { |
| 263 | if (**argv != '-') { |
| 264 | filename = *argv; |
| 265 | if (argv[1]) /* shouldn't be any more args after filename */ |
| 266 | ++error; |
| 267 | } else |
| 268 | ++error; /* not expecting any other options */ |
| 269 | } |
| 270 | } |
| 271 | |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 272 | if (!filename) |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 273 | ++error; |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 274 | |
| 275 | |
| 276 | /* print usage screen if any errors up to this point */ |
| 277 | |
| 278 | if (error) { |
| 279 | fprintf(stderr, "\n%s %s: %s\n", PROGNAME, VERSION, appname); |
| 280 | readpng_version_info(); |
| 281 | fprintf(stderr, "\n" |
| 282 | "Usage: %s [-display xdpy] [-gamma exp] [-bgcolor bg] file.png\n" |
| 283 | " xdpy\tname of the target X display (e.g., ``hostname:0'')\n" |
| 284 | " exp \ttransfer-function exponent (``gamma'') of the display\n" |
Glenn Randers-Pehrson | 91319c6 | 2014-07-04 11:46:17 -0500 | [diff] [blame] | 285 | "\t\t system in floating-point format (e.g., ``%.1f''); equal\n", |
| 286 | PROGNAME, default_display_exponent); |
| 287 | |
| 288 | fprintf(stderr, "\n" |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 289 | "\t\t to the product of the lookup-table exponent (varies)\n" |
| 290 | "\t\t and the CRT exponent (usually 2.2); must be positive\n" |
| 291 | " bg \tdesired background color in 7-character hex RGB format\n" |
| 292 | "\t\t (e.g., ``#ff7700'' for orange: same as HTML colors);\n" |
| 293 | "\t\t used with transparent images\n" |
| 294 | "\nPress Q, Esc or mouse button 1 (within image window, after image\n" |
Glenn Randers-Pehrson | 91319c6 | 2014-07-04 11:46:17 -0500 | [diff] [blame] | 295 | "is displayed) to quit.\n"); |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 296 | exit(1); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | if (!(infile = fopen(filename, "rb"))) { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 301 | fprintf(stderr, PROGNAME ": can't open PNG file [%s]\n", filename); |
| 302 | ++error; |
| 303 | } else { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 304 | if ((rc = readpng_init(infile, &image_width, &image_height)) != 0) { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 305 | switch (rc) { |
| 306 | case 1: |
| 307 | fprintf(stderr, PROGNAME |
| 308 | ": [%s] is not a PNG file: incorrect signature\n", |
| 309 | filename); |
| 310 | break; |
| 311 | case 2: |
| 312 | fprintf(stderr, PROGNAME |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 313 | ": [%s] has bad IHDR (libpng longjmp)\n", filename); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 314 | break; |
| 315 | case 4: |
| 316 | fprintf(stderr, PROGNAME ": insufficient memory\n"); |
| 317 | break; |
| 318 | default: |
| 319 | fprintf(stderr, PROGNAME |
| 320 | ": unknown readpng_init() error\n"); |
| 321 | break; |
| 322 | } |
| 323 | ++error; |
| 324 | } else { |
| 325 | display = XOpenDisplay(displayname); |
| 326 | if (!display) { |
| 327 | readpng_cleanup(TRUE); |
| 328 | fprintf(stderr, PROGNAME ": can't open X display [%s]\n", |
| 329 | displayname? displayname : "default"); |
| 330 | ++error; |
| 331 | } |
| 332 | } |
| 333 | if (error) |
| 334 | fclose(infile); |
| 335 | } |
| 336 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 337 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 338 | if (error) { |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 339 | fprintf(stderr, PROGNAME ": aborting.\n"); |
| 340 | exit(2); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
| 344 | /* set the title-bar string, but make sure buffer doesn't overflow */ |
| 345 | |
| 346 | alen = strlen(appname); |
| 347 | flen = strlen(filename); |
| 348 | if (alen + flen + 3 > 1023) |
| 349 | sprintf(titlebar, "%s: ...%s", appname, filename+(alen+flen+6-1023)); |
| 350 | else |
| 351 | sprintf(titlebar, "%s: %s", appname, filename); |
| 352 | |
| 353 | |
| 354 | /* if the user didn't specify a background color on the command line, |
| 355 | * check for one in the PNG file--if not, the initialized values of 0 |
| 356 | * (black) will be used */ |
| 357 | |
| 358 | if (have_bg) { |
| 359 | unsigned r, g, b; /* this approach quiets compiler warnings */ |
| 360 | |
| 361 | sscanf(bgstr+1, "%2x%2x%2x", &r, &g, &b); |
| 362 | bg_red = (uch)r; |
| 363 | bg_green = (uch)g; |
| 364 | bg_blue = (uch)b; |
| 365 | } else if (readpng_get_bgcolor(&bg_red, &bg_green, &bg_blue) > 1) { |
| 366 | readpng_cleanup(TRUE); |
| 367 | fprintf(stderr, PROGNAME |
| 368 | ": libpng error while checking for background color\n"); |
| 369 | exit(2); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /* do the basic X initialization stuff, make the window and fill it |
| 374 | * with the background color */ |
| 375 | |
| 376 | if (rpng_x_create_window()) |
| 377 | exit(2); |
| 378 | |
| 379 | |
| 380 | /* decode the image, all at once */ |
| 381 | |
| 382 | Trace((stderr, "calling readpng_get_image()\n")) |
| 383 | image_data = readpng_get_image(display_exponent, &image_channels, |
| 384 | &image_rowbytes); |
| 385 | Trace((stderr, "done with readpng_get_image()\n")) |
| 386 | |
| 387 | |
| 388 | /* done with PNG file, so clean up to minimize memory usage (but do NOT |
| 389 | * nuke image_data!) */ |
| 390 | |
| 391 | readpng_cleanup(FALSE); |
| 392 | fclose(infile); |
| 393 | |
| 394 | if (!image_data) { |
| 395 | fprintf(stderr, PROGNAME ": unable to decode PNG image\n"); |
| 396 | exit(3); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /* display image (composite with background if requested) */ |
| 401 | |
| 402 | Trace((stderr, "calling rpng_x_display_image()\n")) |
| 403 | if (rpng_x_display_image()) { |
| 404 | free(image_data); |
| 405 | exit(4); |
| 406 | } |
| 407 | Trace((stderr, "done with rpng_x_display_image()\n")) |
| 408 | |
| 409 | |
| 410 | /* wait for the user to tell us when to quit */ |
| 411 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 412 | printf( |
| 413 | "Done. Press Q, Esc or mouse button 1 (within image window) to quit.\n"); |
| 414 | fflush(stdout); |
| 415 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 416 | do |
| 417 | XNextEvent(display, &e); |
| 418 | while (!(e.type == ButtonPress && e.xbutton.button == Button1) && |
| 419 | !(e.type == KeyPress && /* v--- or 1 for shifted keys */ |
| 420 | ((k = XLookupKeysym(&e.xkey, 0)) == XK_q || k == XK_Escape) )); |
| 421 | |
| 422 | |
| 423 | /* OK, we're done: clean up all image and X resources and go away */ |
| 424 | |
| 425 | rpng_x_cleanup(); |
| 426 | |
Glenn Randers-Pehrson | 91319c6 | 2014-07-04 11:46:17 -0500 | [diff] [blame] | 427 | (void)argc; /* Unused */ |
| 428 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 436 | static int rpng_x_create_window(void) |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 437 | { |
| 438 | uch *xdata; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 439 | int need_colormap = FALSE; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 440 | int screen, pad; |
| 441 | ulg bg_pixel = 0L; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 442 | ulg attrmask; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 443 | Window root; |
| 444 | XEvent e; |
| 445 | XGCValues gcvalues; |
| 446 | XSetWindowAttributes attr; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 447 | XTextProperty windowName, *pWindowName = &windowName; |
| 448 | XTextProperty iconName, *pIconName = &iconName; |
| 449 | XVisualInfo visual_info; |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 450 | XSizeHints *size_hints; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 451 | XWMHints *wm_hints; |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 452 | XClassHint *class_hints; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 453 | |
| 454 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 455 | screen = DefaultScreen(display); |
| 456 | depth = DisplayPlanes(display, screen); |
| 457 | root = RootWindow(display, screen); |
| 458 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 459 | #ifdef DEBUG |
| 460 | XSynchronize(display, True); |
| 461 | #endif |
| 462 | |
| 463 | #if 0 |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 464 | /* GRR: add 8-bit support */ |
| 465 | if (/* depth != 8 && */ depth != 16 && depth != 24 && depth != 32) { |
| 466 | fprintf(stderr, |
| 467 | "screen depth %d not supported (only 16-, 24- or 32-bit TrueColor)\n", |
| 468 | depth); |
| 469 | return 2; |
| 470 | } |
| 471 | |
| 472 | XMatchVisualInfo(display, screen, depth, |
| 473 | (depth == 8)? PseudoColor : TrueColor, &visual_info); |
| 474 | visual = visual_info.visual; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 475 | #else |
| 476 | if (depth != 16 && depth != 24 && depth != 32) { |
| 477 | int visuals_matched = 0; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 478 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 479 | Trace((stderr, "default depth is %d: checking other visuals\n", |
| 480 | depth)) |
| 481 | |
| 482 | /* 24-bit first */ |
| 483 | visual_info.screen = screen; |
| 484 | visual_info.depth = 24; |
| 485 | visual_list = XGetVisualInfo(display, |
| 486 | VisualScreenMask | VisualDepthMask, &visual_info, &visuals_matched); |
| 487 | if (visuals_matched == 0) { |
| 488 | /* GRR: add 15-, 16- and 32-bit TrueColor visuals (also DirectColor?) */ |
| 489 | fprintf(stderr, "default screen depth %d not supported, and no" |
| 490 | " 24-bit visuals found\n", depth); |
| 491 | return 2; |
| 492 | } |
| 493 | Trace((stderr, "XGetVisualInfo() returned %d 24-bit visuals\n", |
| 494 | visuals_matched)) |
| 495 | visual = visual_list[0].visual; |
| 496 | depth = visual_list[0].depth; |
| 497 | /* |
| 498 | colormap_size = visual_list[0].colormap_size; |
| 499 | visual_class = visual->class; |
| 500 | visualID = XVisualIDFromVisual(visual); |
| 501 | */ |
| 502 | have_nondefault_visual = TRUE; |
| 503 | need_colormap = TRUE; |
| 504 | } else { |
| 505 | XMatchVisualInfo(display, screen, depth, TrueColor, &visual_info); |
| 506 | visual = visual_info.visual; |
| 507 | } |
| 508 | #endif |
| 509 | |
| 510 | RMask = visual->red_mask; |
| 511 | GMask = visual->green_mask; |
| 512 | BMask = visual->blue_mask; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 513 | |
| 514 | /* GRR: add/check 8-bit support */ |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 515 | if (depth == 8 || need_colormap) { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 516 | colormap = XCreateColormap(display, root, visual, AllocNone); |
| 517 | if (!colormap) { |
| 518 | fprintf(stderr, "XCreateColormap() failed\n"); |
| 519 | return 2; |
| 520 | } |
| 521 | have_colormap = TRUE; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 522 | } |
| 523 | if (depth == 15 || depth == 16) { |
| 524 | RShift = 15 - rpng_x_msb(RMask); /* these are right-shifts */ |
| 525 | GShift = 15 - rpng_x_msb(GMask); |
| 526 | BShift = 15 - rpng_x_msb(BMask); |
| 527 | } else if (depth > 16) { |
| 528 | #define NO_24BIT_MASKS |
| 529 | #ifdef NO_24BIT_MASKS |
| 530 | RShift = rpng_x_msb(RMask) - 7; /* these are left-shifts */ |
| 531 | GShift = rpng_x_msb(GMask) - 7; |
| 532 | BShift = rpng_x_msb(BMask) - 7; |
| 533 | #else |
| 534 | RShift = 7 - rpng_x_msb(RMask); /* these are right-shifts, too */ |
| 535 | GShift = 7 - rpng_x_msb(GMask); |
| 536 | BShift = 7 - rpng_x_msb(BMask); |
| 537 | #endif |
| 538 | } |
| 539 | if (depth >= 15 && (RShift < 0 || GShift < 0 || BShift < 0)) { |
| 540 | fprintf(stderr, "rpng internal logic error: negative X shift(s)!\n"); |
| 541 | return 2; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /*--------------------------------------------------------------------------- |
| 545 | Finally, create the window. |
| 546 | ---------------------------------------------------------------------------*/ |
| 547 | |
| 548 | attr.backing_store = Always; |
| 549 | attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 550 | attrmask = CWBackingStore | CWEventMask; |
| 551 | if (have_nondefault_visual) { |
| 552 | attr.colormap = colormap; |
| 553 | attr.background_pixel = 0; |
| 554 | attr.border_pixel = 1; |
| 555 | attrmask |= CWColormap | CWBackPixel | CWBorderPixel; |
| 556 | } |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 557 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 558 | window = XCreateWindow(display, root, 0, 0, image_width, image_height, 0, |
| 559 | depth, InputOutput, visual, attrmask, &attr); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 560 | |
| 561 | if (window == None) { |
| 562 | fprintf(stderr, "XCreateWindow() failed\n"); |
| 563 | return 2; |
| 564 | } else |
| 565 | have_window = TRUE; |
| 566 | |
| 567 | if (depth == 8) |
| 568 | XSetWindowColormap(display, window, colormap); |
| 569 | |
| 570 | if (!XStringListToTextProperty(&window_name, 1, pWindowName)) |
| 571 | pWindowName = NULL; |
| 572 | if (!XStringListToTextProperty(&icon_name, 1, pIconName)) |
| 573 | pIconName = NULL; |
| 574 | |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 575 | /* OK if any hints allocation fails; XSetWMProperties() allows NULLs */ |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 576 | |
| 577 | if ((size_hints = XAllocSizeHints()) != NULL) { |
| 578 | /* window will not be resizable */ |
| 579 | size_hints->flags = PMinSize | PMaxSize; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 580 | size_hints->min_width = size_hints->max_width = (int)image_width; |
| 581 | size_hints->min_height = size_hints->max_height = (int)image_height; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | if ((wm_hints = XAllocWMHints()) != NULL) { |
| 585 | wm_hints->initial_state = NormalState; |
| 586 | wm_hints->input = True; |
| 587 | /* wm_hints->icon_pixmap = icon_pixmap; */ |
| 588 | wm_hints->flags = StateHint | InputHint /* | IconPixmapHint */ ; |
| 589 | } |
| 590 | |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 591 | if ((class_hints = XAllocClassHint()) != NULL) { |
| 592 | class_hints->res_name = res_name; |
| 593 | class_hints->res_class = res_class; |
| 594 | } |
| 595 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 596 | XSetWMProperties(display, window, pWindowName, pIconName, NULL, 0, |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 597 | size_hints, wm_hints, class_hints); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 598 | |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 599 | /* various properties and hints no longer needed; free memory */ |
| 600 | if (pWindowName) |
| 601 | XFree(pWindowName->value); |
| 602 | if (pIconName) |
| 603 | XFree(pIconName->value); |
| 604 | if (size_hints) |
| 605 | XFree(size_hints); |
| 606 | if (wm_hints) |
| 607 | XFree(wm_hints); |
Glenn Randers-Pehrson | eb58091 | 2008-07-30 14:47:09 -0500 | [diff] [blame] | 608 | if (class_hints) |
| 609 | XFree(class_hints); |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 610 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 611 | XMapWindow(display, window); |
| 612 | |
| 613 | gc = XCreateGC(display, window, 0, &gcvalues); |
Glenn Randers-Pehrson | 1b8e567 | 2001-08-25 06:46:06 -0500 | [diff] [blame] | 614 | have_gc = TRUE; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 615 | |
| 616 | /*--------------------------------------------------------------------------- |
| 617 | Fill window with the specified background color. |
| 618 | ---------------------------------------------------------------------------*/ |
| 619 | |
| 620 | if (depth == 24 || depth == 32) { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 621 | bg_pixel = ((ulg)bg_red << RShift) | |
| 622 | ((ulg)bg_green << GShift) | |
| 623 | ((ulg)bg_blue << BShift); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 624 | } else if (depth == 16) { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 625 | bg_pixel = ((((ulg)bg_red << 8) >> RShift) & RMask) | |
| 626 | ((((ulg)bg_green << 8) >> GShift) & GMask) | |
| 627 | ((((ulg)bg_blue << 8) >> BShift) & BMask); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 628 | } else /* depth == 8 */ { |
| 629 | |
| 630 | /* GRR: add 8-bit support */ |
| 631 | |
| 632 | } |
| 633 | |
| 634 | XSetForeground(display, gc, bg_pixel); |
| 635 | XFillRectangle(display, window, gc, 0, 0, image_width, image_height); |
| 636 | |
| 637 | /*--------------------------------------------------------------------------- |
| 638 | Wait for first Expose event to do any drawing, then flush. |
| 639 | ---------------------------------------------------------------------------*/ |
| 640 | |
| 641 | do |
| 642 | XNextEvent(display, &e); |
| 643 | while (e.type != Expose || e.xexpose.count); |
| 644 | |
| 645 | XFlush(display); |
| 646 | |
| 647 | /*--------------------------------------------------------------------------- |
| 648 | Allocate memory for the X- and display-specific version of the image. |
| 649 | ---------------------------------------------------------------------------*/ |
| 650 | |
| 651 | if (depth == 24 || depth == 32) { |
| 652 | xdata = (uch *)malloc(4*image_width*image_height); |
| 653 | pad = 32; |
| 654 | } else if (depth == 16) { |
| 655 | xdata = (uch *)malloc(2*image_width*image_height); |
| 656 | pad = 16; |
| 657 | } else /* depth == 8 */ { |
| 658 | xdata = (uch *)malloc(image_width*image_height); |
| 659 | pad = 8; |
| 660 | } |
| 661 | |
| 662 | if (!xdata) { |
| 663 | fprintf(stderr, PROGNAME ": unable to allocate image memory\n"); |
| 664 | return 4; |
| 665 | } |
| 666 | |
| 667 | ximage = XCreateImage(display, visual, depth, ZPixmap, 0, |
| 668 | (char *)xdata, image_width, image_height, pad, 0); |
| 669 | |
| 670 | if (!ximage) { |
| 671 | fprintf(stderr, PROGNAME ": XCreateImage() failed\n"); |
| 672 | free(xdata); |
| 673 | return 3; |
| 674 | } |
| 675 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 676 | /* to avoid testing the byte order every pixel (or doubling the size of |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 677 | * the drawing routine with a giant if-test), we arbitrarily set the byte |
| 678 | * order to MSBFirst and let Xlib worry about inverting things on little- |
| 679 | * endian machines (like Linux/x86, old VAXen, etc.)--this is not the most |
| 680 | * efficient approach (the giant if-test would be better), but in the |
| 681 | * interest of clarity, we take the easy way out... */ |
| 682 | |
| 683 | ximage->byte_order = MSBFirst; |
| 684 | |
| 685 | return 0; |
| 686 | |
| 687 | } /* end function rpng_x_create_window() */ |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 693 | static int rpng_x_display_image(void) |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 694 | { |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 695 | uch *src; |
| 696 | char *dest; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 697 | uch r, g, b, a; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 698 | ulg i, row, lastrow = 0; |
| 699 | ulg pixel; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 700 | int ximage_rowbytes = ximage->bytes_per_line; |
| 701 | /* int bpp = ximage->bits_per_pixel; */ |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 702 | |
| 703 | |
| 704 | Trace((stderr, "beginning display loop (image_channels == %d)\n", |
| 705 | image_channels)) |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 706 | Trace((stderr, " (width = %ld, rowbytes = %ld, ximage_rowbytes = %d)\n", |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 707 | image_width, image_rowbytes, ximage_rowbytes)) |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 708 | Trace((stderr, " (bpp = %d)\n", ximage->bits_per_pixel)) |
| 709 | Trace((stderr, " (byte_order = %s)\n", ximage->byte_order == MSBFirst? |
| 710 | "MSBFirst" : (ximage->byte_order == LSBFirst? "LSBFirst" : "unknown"))) |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 711 | |
| 712 | if (depth == 24 || depth == 32) { |
| 713 | ulg red, green, blue; |
| 714 | |
| 715 | for (lastrow = row = 0; row < image_height; ++row) { |
| 716 | src = image_data + row*image_rowbytes; |
| 717 | dest = ximage->data + row*ximage_rowbytes; |
| 718 | if (image_channels == 3) { |
| 719 | for (i = image_width; i > 0; --i) { |
| 720 | red = *src++; |
| 721 | green = *src++; |
| 722 | blue = *src++; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 723 | #ifdef NO_24BIT_MASKS |
| 724 | pixel = (red << RShift) | |
| 725 | (green << GShift) | |
| 726 | (blue << BShift); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 727 | /* recall that we set ximage->byte_order = MSBFirst above */ |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 728 | /* GRR BUG: this assumes bpp == 32, but may be 24: */ |
| 729 | *dest++ = (char)((pixel >> 24) & 0xff); |
| 730 | *dest++ = (char)((pixel >> 16) & 0xff); |
| 731 | *dest++ = (char)((pixel >> 8) & 0xff); |
| 732 | *dest++ = (char)( pixel & 0xff); |
| 733 | #else |
| 734 | red = (RShift < 0)? red << (-RShift) : red >> RShift; |
| 735 | green = (GShift < 0)? green << (-GShift) : green >> GShift; |
| 736 | blue = (BShift < 0)? blue << (-BShift) : blue >> BShift; |
| 737 | pixel = (red & RMask) | (green & GMask) | (blue & BMask); |
| 738 | /* recall that we set ximage->byte_order = MSBFirst above */ |
| 739 | *dest++ = (char)((pixel >> 24) & 0xff); |
| 740 | *dest++ = (char)((pixel >> 16) & 0xff); |
| 741 | *dest++ = (char)((pixel >> 8) & 0xff); |
| 742 | *dest++ = (char)( pixel & 0xff); |
| 743 | #endif |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 744 | } |
| 745 | } else /* if (image_channels == 4) */ { |
| 746 | for (i = image_width; i > 0; --i) { |
| 747 | r = *src++; |
| 748 | g = *src++; |
| 749 | b = *src++; |
| 750 | a = *src++; |
| 751 | if (a == 255) { |
| 752 | red = r; |
| 753 | green = g; |
| 754 | blue = b; |
| 755 | } else if (a == 0) { |
| 756 | red = bg_red; |
| 757 | green = bg_green; |
| 758 | blue = bg_blue; |
| 759 | } else { |
| 760 | /* this macro (from png.h) composites the foreground |
| 761 | * and background values and puts the result into the |
| 762 | * first argument */ |
| 763 | alpha_composite(red, r, a, bg_red); |
| 764 | alpha_composite(green, g, a, bg_green); |
| 765 | alpha_composite(blue, b, a, bg_blue); |
| 766 | } |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 767 | pixel = (red << RShift) | |
| 768 | (green << GShift) | |
| 769 | (blue << BShift); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 770 | /* recall that we set ximage->byte_order = MSBFirst above */ |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 771 | *dest++ = (char)((pixel >> 24) & 0xff); |
| 772 | *dest++ = (char)((pixel >> 16) & 0xff); |
| 773 | *dest++ = (char)((pixel >> 8) & 0xff); |
| 774 | *dest++ = (char)( pixel & 0xff); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | /* display after every 16 lines */ |
| 778 | if (((row+1) & 0xf) == 0) { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 779 | XPutImage(display, window, gc, ximage, 0, (int)lastrow, 0, |
| 780 | (int)lastrow, image_width, 16); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 781 | XFlush(display); |
| 782 | lastrow = row + 1; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | } else if (depth == 16) { |
| 787 | ush red, green, blue; |
| 788 | |
| 789 | for (lastrow = row = 0; row < image_height; ++row) { |
| 790 | src = image_data + row*image_rowbytes; |
| 791 | dest = ximage->data + row*ximage_rowbytes; |
| 792 | if (image_channels == 3) { |
| 793 | for (i = image_width; i > 0; --i) { |
| 794 | red = ((ush)(*src) << 8); |
| 795 | ++src; |
| 796 | green = ((ush)(*src) << 8); |
| 797 | ++src; |
| 798 | blue = ((ush)(*src) << 8); |
| 799 | ++src; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 800 | pixel = ((red >> RShift) & RMask) | |
| 801 | ((green >> GShift) & GMask) | |
| 802 | ((blue >> BShift) & BMask); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 803 | /* recall that we set ximage->byte_order = MSBFirst above */ |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 804 | *dest++ = (char)((pixel >> 8) & 0xff); |
| 805 | *dest++ = (char)( pixel & 0xff); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 806 | } |
| 807 | } else /* if (image_channels == 4) */ { |
| 808 | for (i = image_width; i > 0; --i) { |
| 809 | r = *src++; |
| 810 | g = *src++; |
| 811 | b = *src++; |
| 812 | a = *src++; |
| 813 | if (a == 255) { |
| 814 | red = ((ush)r << 8); |
| 815 | green = ((ush)g << 8); |
| 816 | blue = ((ush)b << 8); |
| 817 | } else if (a == 0) { |
| 818 | red = ((ush)bg_red << 8); |
| 819 | green = ((ush)bg_green << 8); |
| 820 | blue = ((ush)bg_blue << 8); |
| 821 | } else { |
| 822 | /* this macro (from png.h) composites the foreground |
| 823 | * and background values and puts the result back into |
| 824 | * the first argument (== fg byte here: safe) */ |
| 825 | alpha_composite(r, r, a, bg_red); |
| 826 | alpha_composite(g, g, a, bg_green); |
| 827 | alpha_composite(b, b, a, bg_blue); |
| 828 | red = ((ush)r << 8); |
| 829 | green = ((ush)g << 8); |
| 830 | blue = ((ush)b << 8); |
| 831 | } |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 832 | pixel = ((red >> RShift) & RMask) | |
| 833 | ((green >> GShift) & GMask) | |
| 834 | ((blue >> BShift) & BMask); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 835 | /* recall that we set ximage->byte_order = MSBFirst above */ |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 836 | *dest++ = (char)((pixel >> 8) & 0xff); |
| 837 | *dest++ = (char)( pixel & 0xff); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | /* display after every 16 lines */ |
| 841 | if (((row+1) & 0xf) == 0) { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 842 | XPutImage(display, window, gc, ximage, 0, (int)lastrow, 0, |
| 843 | (int)lastrow, image_width, 16); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 844 | XFlush(display); |
| 845 | lastrow = row + 1; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | } else /* depth == 8 */ { |
| 850 | |
| 851 | /* GRR: add 8-bit support */ |
| 852 | |
| 853 | } |
| 854 | |
| 855 | Trace((stderr, "calling final XPutImage()\n")) |
| 856 | if (lastrow < image_height) { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 857 | XPutImage(display, window, gc, ximage, 0, (int)lastrow, 0, |
| 858 | (int)lastrow, image_width, image_height-lastrow); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 859 | XFlush(display); |
| 860 | } |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 868 | static void rpng_x_cleanup(void) |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 869 | { |
| 870 | if (image_data) { |
| 871 | free(image_data); |
| 872 | image_data = NULL; |
| 873 | } |
| 874 | |
| 875 | if (ximage) { |
| 876 | if (ximage->data) { |
| 877 | free(ximage->data); /* we allocated it, so we free it */ |
| 878 | ximage->data = (char *)NULL; /* instead of XDestroyImage() */ |
| 879 | } |
| 880 | XDestroyImage(ximage); |
| 881 | ximage = NULL; |
| 882 | } |
| 883 | |
Glenn Randers-Pehrson | 1b8e567 | 2001-08-25 06:46:06 -0500 | [diff] [blame] | 884 | if (have_gc) |
| 885 | XFreeGC(display, gc); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 886 | |
| 887 | if (have_window) |
| 888 | XDestroyWindow(display, window); |
| 889 | |
| 890 | if (have_colormap) |
| 891 | XFreeColormap(display, colormap); |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 892 | |
| 893 | if (have_nondefault_visual) |
| 894 | XFree(visual_list); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | static int rpng_x_msb(ulg u32val) |
| 902 | { |
| 903 | int i; |
| 904 | |
| 905 | for (i = 31; i >= 0; --i) { |
| 906 | if (u32val & 0x80000000L) |
| 907 | break; |
| 908 | u32val <<= 1; |
| 909 | } |
| 910 | return i; |
| 911 | } |