| 1 |
/* |
| 2 |
* Copyright (C) 1997 and 1998 WIDE Project. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* 3. Neither the name of the project nor the names of its contributors |
| 13 |
* may be used to endorse or promote products derived from this software |
| 14 |
* without specific prior written permission. |
| 15 |
* |
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
| 17 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
| 20 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 22 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 23 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 25 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 |
* SUCH DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
/* $XConsortium: xwd.c /main/64 1996/01/14 16:53:13 kaleb $ */ |
| 30 |
/* $XFree86: xc/programs/xwd/xwd.c,v 3.2 1996/07/08 10:37:37 dawes Exp $ */ |
| 31 |
|
| 32 |
/* |
| 33 |
|
| 34 |
Copyright (c) 1987 X Consortium |
| 35 |
|
| 36 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 37 |
of this software and associated documentation files (the "Software"), to deal |
| 38 |
in the Software without restriction, including without limitation the rights |
| 39 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 40 |
copies of the Software, and to permit persons to whom the Software is |
| 41 |
furnished to do so, subject to the following conditions: |
| 42 |
|
| 43 |
The above copyright notice and this permission notice shall be included in |
| 44 |
all copies or substantial portions of the Software. |
| 45 |
|
| 46 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 47 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 48 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 49 |
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 50 |
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 51 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 52 |
|
| 53 |
Except as contained in this notice, the name of the X Consortium shall not be |
| 54 |
used in advertising or otherwise to promote the sale, use or other dealings |
| 55 |
in this Software without prior written authorization from the X Consortium. |
| 56 |
|
| 57 |
*/ |
| 58 |
|
| 59 |
/* |
| 60 |
* xwd.c MIT Project Athena, X Window system window raster image dumper. |
| 61 |
* |
| 62 |
* This program will dump a raster image of the contents of a window into a |
| 63 |
* file for output on graphics printers or for other uses. |
| 64 |
* |
| 65 |
* Author: Tony Della Fera, DEC |
| 66 |
* 17-Jun-85 |
| 67 |
* |
| 68 |
* Modification history: |
| 69 |
* |
| 70 |
* 11/14/86 Bill Wyatt, Smithsonian Astrophysical Observatory |
| 71 |
* - Removed Z format option, changing it to an XY option. Monochrome |
| 72 |
* windows will always dump in XY format. Color windows will dump |
| 73 |
* in Z format by default, but can be dumped in XY format with the |
| 74 |
* -xy option. |
| 75 |
* |
| 76 |
* 11/18/86 Bill Wyatt |
| 77 |
* - VERSION 6 is same as version 5 for monchrome. For colors, the |
| 78 |
* appropriate number of Color structs are dumped after the header, |
| 79 |
* which has the number of colors (=0 for monochrome) in place of the |
| 80 |
* V5 padding at the end. Up to 16-bit displays are supported. I |
| 81 |
* don't yet know how 24- to 32-bit displays will be handled under |
| 82 |
* the Version 11 protocol. |
| 83 |
* |
| 84 |
* 6/15/87 David Krikorian, MIT Project Athena |
| 85 |
* - VERSION 7 runs under the X Version 11 servers, while the previous |
| 86 |
* versions of xwd were are for X Version 10. This version is based |
| 87 |
* on xwd version 6, and should eventually have the same color |
| 88 |
* abilities. (Xwd V7 has yet to be tested on a color machine, so |
| 89 |
* all color-related code is commented out until color support |
| 90 |
* becomes practical.) |
| 91 |
*/ |
| 92 |
|
| 93 |
/*% |
| 94 |
*% This is the format for commenting out color-related code until |
| 95 |
*% color can be supported. |
| 96 |
%*/ |
| 97 |
|
| 98 |
#include <stdio.h> |
| 99 |
#include <errno.h> |
| 100 |
#include <X11/Xos.h> |
| 101 |
|
| 102 |
#ifdef X_NOT_STDC_ENV |
| 103 |
extern int errno; |
| 104 |
char *calloc(); |
| 105 |
#else |
| 106 |
#include <stdlib.h> |
| 107 |
#endif |
| 108 |
|
| 109 |
#include <X11/Xlib.h> |
| 110 |
#include <X11/Xutil.h> |
| 111 |
|
| 112 |
#include <X11/Xmu/WinUtil.h> |
| 113 |
typedef unsigned long Pixel; |
| 114 |
#include "X11/XWDFile.h" |
| 115 |
|
| 116 |
#define FEEP_VOLUME 0 |
| 117 |
|
| 118 |
/* Include routines to do parsing */ |
| 119 |
#include "dsimple.h" |
| 120 |
#include "list.h" |
| 121 |
#include "wsutils.h" |
| 122 |
#include "multiVis.h" |
| 123 |
|
| 124 |
#ifdef XKB |
| 125 |
#include <X11/extensions/XKBbells.h> |
| 126 |
#endif |
| 127 |
|
| 128 |
/* Setable Options */ |
| 129 |
|
| 130 |
int format = ZPixmap; |
| 131 |
Bool nobdrs = False; |
| 132 |
Bool on_root = False; |
| 133 |
Bool standard_out = True; |
| 134 |
Bool debug = False; |
| 135 |
Bool silent = False; |
| 136 |
Bool use_installed = False; |
| 137 |
long add_pixel_value = 0; |
| 138 |
|
| 139 |
extern int (*_XErrorFunction)(); |
| 140 |
extern int _XDefaultError(); |
| 141 |
|
| 142 |
static long parse_long (s) |
| 143 |
char *s; |
| 144 |
{ |
| 145 |
char *fmt = "%lu"; |
| 146 |
long retval = 0L; |
| 147 |
int thesign = 1; |
| 148 |
|
| 149 |
if (s && s[0]) { |
| 150 |
if (s[0] == '-') s++, thesign = -1; |
| 151 |
if (s[0] == '0') s++, fmt = "%lo"; |
| 152 |
if (s[0] == 'x' || s[0] == 'X') s++, fmt = "%lx"; |
| 153 |
(void) sscanf (s, fmt, &retval); |
| 154 |
} |
| 155 |
return (thesign * retval); |
| 156 |
} |
| 157 |
|
| 158 |
main(argc, argv) |
| 159 |
int argc; |
| 160 |
char **argv; |
| 161 |
{ |
| 162 |
register i; |
| 163 |
Window target_win; |
| 164 |
FILE *out_file = stdout; |
| 165 |
Bool frame_only = False; |
| 166 |
|
| 167 |
INIT_NAME; |
| 168 |
|
| 169 |
Setup_Display_And_Screen(&argc, argv); |
| 170 |
|
| 171 |
/* Get window select on command line, if any */ |
| 172 |
target_win = Select_Window_Args(&argc, argv); |
| 173 |
|
| 174 |
for (i = 1; i < argc; i++) { |
| 175 |
if (!strcmp(argv[i], "-nobdrs")) { |
| 176 |
nobdrs = True; |
| 177 |
continue; |
| 178 |
} |
| 179 |
if (!strcmp(argv[i], "-debug")) { |
| 180 |
debug = True; |
| 181 |
continue; |
| 182 |
} |
| 183 |
if (!strcmp(argv[i], "-help")) |
| 184 |
usage(); |
| 185 |
if (!strcmp(argv[i], "-out")) { |
| 186 |
if (++i >= argc) usage(); |
| 187 |
if (!(out_file = fopen(argv[i], "wb"))) |
| 188 |
Error("Can't open output file as specified."); |
| 189 |
standard_out = False; |
| 190 |
continue; |
| 191 |
} |
| 192 |
if (!strcmp(argv[i], "-xy")) { |
| 193 |
format = XYPixmap; |
| 194 |
continue; |
| 195 |
} |
| 196 |
if (!strcmp(argv[i], "-screen")) { |
| 197 |
on_root = True; |
| 198 |
continue; |
| 199 |
} |
| 200 |
if (!strcmp(argv[i], "-icmap")) { |
| 201 |
use_installed = True; |
| 202 |
continue; |
| 203 |
} |
| 204 |
if (!strcmp(argv[i], "-add")) { |
| 205 |
if (++i >= argc) usage(); |
| 206 |
add_pixel_value = parse_long (argv[i]); |
| 207 |
continue; |
| 208 |
} |
| 209 |
if (!strcmp(argv[i], "-frame")) { |
| 210 |
frame_only = True; |
| 211 |
continue; |
| 212 |
} |
| 213 |
if (!strcmp(argv[i], "-silent")) { |
| 214 |
silent = True; |
| 215 |
continue; |
| 216 |
} |
| 217 |
usage(); |
| 218 |
} |
| 219 |
#ifdef WIN32 |
| 220 |
if (standard_out) |
| 221 |
_setmode(fileno(out_file), _O_BINARY); |
| 222 |
#endif |
| 223 |
|
| 224 |
/* |
| 225 |
* Let the user select the target window. |
| 226 |
*/ |
| 227 |
if (!target_win) { |
| 228 |
#if 0 |
| 229 |
target_win = Select_Window(dpy); |
| 230 |
if (target_win != None && !frame_only) { |
| 231 |
Window root; |
| 232 |
int dummyi; |
| 233 |
unsigned int dummy; |
| 234 |
|
| 235 |
if (XGetGeometry (dpy, target_win, &root, &dummyi, &dummyi, |
| 236 |
&dummy, &dummy, &dummy, &dummy) && |
| 237 |
target_win != root) |
| 238 |
target_win = XmuClientWindow (dpy, target_win); |
| 239 |
} |
| 240 |
#else |
| 241 |
fprintf(stderr, "window must be specified\n"); |
| 242 |
exit(1); |
| 243 |
#endif |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
/* |
| 248 |
* Dump it! |
| 249 |
*/ |
| 250 |
Window_Dump(target_win, out_file); |
| 251 |
|
| 252 |
XCloseDisplay(dpy); |
| 253 |
if (fclose(out_file)) { |
| 254 |
perror("xwintoppm"); |
| 255 |
exit(1); |
| 256 |
} |
| 257 |
exit(0); |
| 258 |
} |
| 259 |
|
| 260 |
static int |
| 261 |
Get24bitDirectColors(colors) |
| 262 |
XColor **colors ; |
| 263 |
{ |
| 264 |
int i , ncolors = 256 ; |
| 265 |
XColor *tcol ; |
| 266 |
|
| 267 |
*colors = tcol = (XColor *)malloc(sizeof(XColor) * ncolors) ; |
| 268 |
|
| 269 |
for(i=0 ; i < ncolors ; i++) |
| 270 |
{ |
| 271 |
tcol[i].pixel = i << 16 | i << 8 | i ; |
| 272 |
tcol[i].red = tcol[i].green = tcol[i].blue = i << 8 | i ; |
| 273 |
} |
| 274 |
|
| 275 |
return ncolors ; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
/* |
| 280 |
* Window_Dump: dump a window to a file which must already be open for |
| 281 |
* writting. |
| 282 |
*/ |
| 283 |
|
| 284 |
Window_Dump(window, out) |
| 285 |
Window window; |
| 286 |
FILE *out; |
| 287 |
{ |
| 288 |
unsigned long swaptest = 1; |
| 289 |
XColor *colors; |
| 290 |
unsigned buffer_size; |
| 291 |
int win_name_size; |
| 292 |
int header_size; |
| 293 |
int ncolors, i; |
| 294 |
char *win_name; |
| 295 |
Bool got_win_name; |
| 296 |
XWindowAttributes win_info; |
| 297 |
XImage *image; |
| 298 |
int absx, absy, x, y; |
| 299 |
unsigned width, height; |
| 300 |
int dwidth, dheight; |
| 301 |
int bw; |
| 302 |
Window dummywin; |
| 303 |
XWDFileHeader header; |
| 304 |
XWDColor xwdcolor; |
| 305 |
|
| 306 |
int transparentOverlays , multiVis; |
| 307 |
int numVisuals; |
| 308 |
XVisualInfo *pVisuals; |
| 309 |
int numOverlayVisuals; |
| 310 |
OverlayInfo *pOverlayVisuals; |
| 311 |
int numImageVisuals; |
| 312 |
XVisualInfo **pImageVisuals; |
| 313 |
list_ptr vis_regions; /* list of regions to read from */ |
| 314 |
list_ptr vis_image_regions ; |
| 315 |
Colormap cmap ; |
| 316 |
Visual vis_h,*vis ; |
| 317 |
int allImage = 0 ; |
| 318 |
|
| 319 |
/* |
| 320 |
* Inform the user not to alter the screen. |
| 321 |
*/ |
| 322 |
if (!silent) { |
| 323 |
#ifdef XKB |
| 324 |
XkbStdBell(dpy,None,50,XkbBI_Wait); |
| 325 |
#else |
| 326 |
XBell(dpy,FEEP_VOLUME); |
| 327 |
#endif |
| 328 |
XFlush(dpy); |
| 329 |
} |
| 330 |
|
| 331 |
/* |
| 332 |
* Get the parameters of the window being dumped. |
| 333 |
*/ |
| 334 |
if (debug) outl("xwintoppm: Getting target window information.\n"); |
| 335 |
if(!XGetWindowAttributes(dpy, window, &win_info)) |
| 336 |
Fatal_Error("Can't get target window attributes."); |
| 337 |
|
| 338 |
/* handle any frame window */ |
| 339 |
if (!XTranslateCoordinates (dpy, window, RootWindow (dpy, screen), 0, 0, |
| 340 |
&absx, &absy, &dummywin)) { |
| 341 |
fprintf (stderr, |
| 342 |
"%s: unable to translate window coordinates (%d,%d)\n", |
| 343 |
program_name, absx, absy); |
| 344 |
exit (1); |
| 345 |
} |
| 346 |
win_info.x = absx; |
| 347 |
win_info.y = absy; |
| 348 |
width = win_info.width; |
| 349 |
height = win_info.height; |
| 350 |
bw = 0; |
| 351 |
|
| 352 |
if (!nobdrs) { |
| 353 |
absx -= win_info.border_width; |
| 354 |
absy -= win_info.border_width; |
| 355 |
bw = win_info.border_width; |
| 356 |
width += (2 * bw); |
| 357 |
height += (2 * bw); |
| 358 |
} |
| 359 |
dwidth = DisplayWidth (dpy, screen); |
| 360 |
dheight = DisplayHeight (dpy, screen); |
| 361 |
|
| 362 |
|
| 363 |
/* clip to window */ |
| 364 |
if (absx < 0) width += absx, absx = 0; |
| 365 |
if (absy < 0) height += absy, absy = 0; |
| 366 |
if (absx + width > dwidth) width = dwidth - absx; |
| 367 |
if (absy + height > dheight) height = dheight - absy; |
| 368 |
|
| 369 |
XFetchName(dpy, window, &win_name); |
| 370 |
if (!win_name || !win_name[0]) { |
| 371 |
win_name = "xwdump"; |
| 372 |
got_win_name = False; |
| 373 |
} else { |
| 374 |
got_win_name = True; |
| 375 |
} |
| 376 |
|
| 377 |
/* sizeof(char) is included for the null string terminator. */ |
| 378 |
win_name_size = strlen(win_name) + sizeof(char); |
| 379 |
|
| 380 |
/* |
| 381 |
* Snarf the pixmap with XGetImage. |
| 382 |
*/ |
| 383 |
|
| 384 |
x = absx - win_info.x; |
| 385 |
y = absy - win_info.y; |
| 386 |
|
| 387 |
multiVis = GetMultiVisualRegions(dpy,RootWindow(dpy, screen), |
| 388 |
absx, absy, |
| 389 |
width, height,&transparentOverlays,&numVisuals, &pVisuals, |
| 390 |
&numOverlayVisuals,&pOverlayVisuals,&numImageVisuals, |
| 391 |
&pImageVisuals,&vis_regions,&vis_image_regions,&allImage) ; |
| 392 |
if (on_root || multiVis) |
| 393 |
{ |
| 394 |
if(!multiVis) |
| 395 |
image = XGetImage (dpy, RootWindow(dpy, screen), absx, absy, |
| 396 |
width, height, AllPlanes, format); |
| 397 |
else |
| 398 |
image = ReadAreaToImage(dpy, RootWindow(dpy, screen), absx, absy, |
| 399 |
width, height, |
| 400 |
numVisuals,pVisuals,numOverlayVisuals,pOverlayVisuals, |
| 401 |
numImageVisuals, pImageVisuals,vis_regions, |
| 402 |
vis_image_regions,format,allImage); |
| 403 |
} |
| 404 |
else |
| 405 |
image = XGetImage (dpy, window, x, y, width, height, AllPlanes, format); |
| 406 |
if (!image) { |
| 407 |
fprintf (stderr, "%s: unable to get image at %dx%d+%d+%d\n", |
| 408 |
program_name, width, height, x, y); |
| 409 |
exit (1); |
| 410 |
} |
| 411 |
|
| 412 |
if (add_pixel_value != 0) XAddPixel (image, add_pixel_value); |
| 413 |
|
| 414 |
/* |
| 415 |
* Determine the pixmap size. |
| 416 |
*/ |
| 417 |
buffer_size = Image_Size(image); |
| 418 |
|
| 419 |
if (debug) outl("xwintoppm: Getting Colors.\n"); |
| 420 |
|
| 421 |
if( !multiVis) |
| 422 |
{ |
| 423 |
ncolors = Get_XColors(&win_info, &colors); |
| 424 |
vis = win_info.visual ; |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 428 |
ncolors = Get24bitDirectColors(&colors) ; |
| 429 |
initFakeVisual(&vis_h) ; |
| 430 |
vis = &vis_h ; |
| 431 |
} |
| 432 |
/* |
| 433 |
* Inform the user that the image has been retrieved. |
| 434 |
*/ |
| 435 |
if (!silent) { |
| 436 |
#ifdef XKB |
| 437 |
XkbStdBell(dpy,window,FEEP_VOLUME,XkbBI_Proceed); |
| 438 |
XkbStdBell(dpy,window,FEEP_VOLUME,XkbBI_RepeatingLastBell); |
| 439 |
#else |
| 440 |
XBell(dpy, FEEP_VOLUME); |
| 441 |
XBell(dpy, FEEP_VOLUME); |
| 442 |
#endif |
| 443 |
XFlush(dpy); |
| 444 |
} |
| 445 |
|
| 446 |
/* |
| 447 |
* Calculate header size. |
| 448 |
*/ |
| 449 |
if (debug) outl("xwintoppm: Calculating header size.\n"); |
| 450 |
header_size = SIZEOF(XWDheader) + win_name_size; |
| 451 |
|
| 452 |
#if 0 |
| 453 |
/* |
| 454 |
* Write out header information. |
| 455 |
*/ |
| 456 |
if (debug) outl("xwintoppm: Constructing and dumping file header.\n"); |
| 457 |
header.header_size = (CARD32) header_size; |
| 458 |
header.file_version = (CARD32) XWD_FILE_VERSION; |
| 459 |
header.pixmap_format = (CARD32) format; |
| 460 |
header.pixmap_depth = (CARD32) image->depth; |
| 461 |
header.pixmap_width = (CARD32) image->width; |
| 462 |
header.pixmap_height = (CARD32) image->height; |
| 463 |
header.xoffset = (CARD32) image->xoffset; |
| 464 |
header.byte_order = (CARD32) image->byte_order; |
| 465 |
header.bitmap_unit = (CARD32) image->bitmap_unit; |
| 466 |
header.bitmap_bit_order = (CARD32) image->bitmap_bit_order; |
| 467 |
header.bitmap_pad = (CARD32) image->bitmap_pad; |
| 468 |
header.bits_per_pixel = (CARD32) image->bits_per_pixel; |
| 469 |
header.bytes_per_line = (CARD32) image->bytes_per_line; |
| 470 |
/**** |
| 471 |
header.visual_class = (CARD32) win_info.visual->class; |
| 472 |
header.red_mask = (CARD32) win_info.visual->red_mask; |
| 473 |
header.green_mask = (CARD32) win_info.visual->green_mask; |
| 474 |
header.blue_mask = (CARD32) win_info.visual->blue_mask; |
| 475 |
header.bits_per_rgb = (CARD32) win_info.visual->bits_per_rgb; |
| 476 |
header.colormap_entries = (CARD32) win_info.visual->map_entries; |
| 477 |
*****/ |
| 478 |
header.visual_class = (CARD32) vis->class; |
| 479 |
header.red_mask = (CARD32) vis->red_mask; |
| 480 |
header.green_mask = (CARD32) vis->green_mask; |
| 481 |
header.blue_mask = (CARD32) vis->blue_mask; |
| 482 |
header.bits_per_rgb = (CARD32) vis->bits_per_rgb; |
| 483 |
header.colormap_entries = (CARD32) vis->map_entries; |
| 484 |
|
| 485 |
header.ncolors = ncolors; |
| 486 |
header.window_width = (CARD32) win_info.width; |
| 487 |
header.window_height = (CARD32) win_info.height; |
| 488 |
header.window_x = absx; |
| 489 |
header.window_y = absy; |
| 490 |
header.window_bdrwidth = (CARD32) win_info.border_width; |
| 491 |
|
| 492 |
if (*(char *) &swaptest) { |
| 493 |
_swaplong((char *) &header, sizeof(header)); |
| 494 |
for (i = 0; i < ncolors; i++) { |
| 495 |
_swaplong((char *) &colors[i].pixel, sizeof(long)); |
| 496 |
_swapshort((char *) &colors[i].red, 3 * sizeof(short)); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
if (fwrite((char *)&header, SIZEOF(XWDheader), 1, out) != 1 || |
| 501 |
fwrite(win_name, win_name_size, 1, out) != 1) { |
| 502 |
perror("xwintoppm"); |
| 503 |
exit(1); |
| 504 |
} |
| 505 |
#else |
| 506 |
fprintf(out, "P6\n%d %d %d\n", win_info.width, win_info.height, 255); |
| 507 |
#endif |
| 508 |
|
| 509 |
#if 0 |
| 510 |
/* |
| 511 |
* Write out the color maps, if any |
| 512 |
*/ |
| 513 |
|
| 514 |
if (debug) outl("xwintoppm: Dumping %d colors.\n", ncolors); |
| 515 |
for (i = 0; i < ncolors; i++) { |
| 516 |
xwdcolor.pixel = colors[i].pixel; |
| 517 |
xwdcolor.red = colors[i].red; |
| 518 |
xwdcolor.green = colors[i].green; |
| 519 |
xwdcolor.blue = colors[i].blue; |
| 520 |
xwdcolor.flags = colors[i].flags; |
| 521 |
if (fwrite((char *) &xwdcolor, SIZEOF(XWDColor), 1, out) != 1) { |
| 522 |
perror("xwd"); |
| 523 |
exit(1); |
| 524 |
} |
| 525 |
} |
| 526 |
#endif |
| 527 |
|
| 528 |
/* |
| 529 |
* Write out the buffer. |
| 530 |
*/ |
| 531 |
if (debug) outl("xwintoppm: Dumping pixmap. bufsize=%d\n",buffer_size); |
| 532 |
|
| 533 |
#if 0 |
| 534 |
/* |
| 535 |
* This copying of the bit stream (data) to a file is to be replaced |
| 536 |
* by an Xlib call which hasn't been written yet. It is not clear |
| 537 |
* what other functions of xwd will be taken over by this (as yet) |
| 538 |
* non-existant X function. |
| 539 |
*/ |
| 540 |
if (fwrite(image->data, (int) buffer_size, 1, out) != 1) { |
| 541 |
perror("xwd"); |
| 542 |
exit(1); |
| 543 |
} |
| 544 |
#else |
| 545 |
{ |
| 546 |
int x, y; |
| 547 |
int i; |
| 548 |
unsigned long pixel; |
| 549 |
unsigned char buf[3]; |
| 550 |
unsigned long mask[3]; |
| 551 |
unsigned int shift0[3], shift8[3]; |
| 552 |
|
| 553 |
mask[0] = image->red_mask; |
| 554 |
mask[1] = image->green_mask; |
| 555 |
mask[2] = image->blue_mask; |
| 556 |
if (!mask[0] || !mask[1] || !mask[2]) { |
| 557 |
fprintf(stderr, "unsupported screen depth; bailing out\n"); |
| 558 |
exit(1); |
| 559 |
} |
| 560 |
for (i = 0; i < 3; i++) { |
| 561 |
shift0[i] = 0; |
| 562 |
while (!(mask[i] & (1 << shift0[i]))) |
| 563 |
shift0[i]++; |
| 564 |
shift8[i] = shift0[i]; |
| 565 |
while (mask[i] & (1 << shift8[i])) |
| 566 |
shift8[i]++; |
| 567 |
shift8[i] = 8 - (shift8[i] - shift0[i]); |
| 568 |
} |
| 569 |
for (y = 0; y < win_info.height; y++) { |
| 570 |
for (x = 0; x < win_info.width; x++) { |
| 571 |
pixel = (*image->f.get_pixel)(image, x, y); |
| 572 |
#if 0 |
| 573 |
if (ncolors && pixel < ncolors) { |
| 574 |
buf[0] = colors[pixel].red; |
| 575 |
buf[1] = colors[pixel].green; |
| 576 |
buf[2] = colors[pixel].blue; |
| 577 |
} else |
| 578 |
#endif |
| 579 |
{ |
| 580 |
buf[0] = (pixel & mask[0]) >> shift0[0] << shift8[0]; |
| 581 |
buf[1] = (pixel & mask[1]) >> shift0[1] << shift8[1]; |
| 582 |
buf[2] = (pixel & mask[2]) >> shift0[2] << shift8[2]; |
| 583 |
} |
| 584 |
fwrite(buf, 3, 1, out); |
| 585 |
} |
| 586 |
} |
| 587 |
} |
| 588 |
#endif |
| 589 |
|
| 590 |
/* |
| 591 |
* free the color buffer. |
| 592 |
*/ |
| 593 |
|
| 594 |
if(debug && ncolors > 0) outl("xwd: Freeing colors.\n"); |
| 595 |
if(ncolors > 0) free(colors); |
| 596 |
|
| 597 |
/* |
| 598 |
* Free window name string. |
| 599 |
*/ |
| 600 |
if (debug) outl("xwd: Freeing window name string.\n"); |
| 601 |
if (got_win_name) XFree(win_name); |
| 602 |
|
| 603 |
/* |
| 604 |
* Free image |
| 605 |
*/ |
| 606 |
XDestroyImage(image); |
| 607 |
} |
| 608 |
|
| 609 |
/* |
| 610 |
* Report the syntax for calling xwd. |
| 611 |
*/ |
| 612 |
usage() |
| 613 |
{ |
| 614 |
fprintf (stderr, |
| 615 |
"usage: %s [-display host:dpy] [-debug] [-help] %s [-nobdrs] [-out <file>]", |
| 616 |
program_name, SELECT_USAGE); |
| 617 |
fprintf (stderr, " [-xy] [-add value] [-frame]\n"); |
| 618 |
exit(1); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
/* |
| 623 |
* Error - Fatal xwd error. |
| 624 |
*/ |
| 625 |
|
| 626 |
Error(string) |
| 627 |
char *string; /* Error description string. */ |
| 628 |
{ |
| 629 |
outl("\nxwd: Error => %s\n", string); |
| 630 |
if (errno != 0) { |
| 631 |
perror("xwd"); |
| 632 |
outl("\n"); |
| 633 |
} |
| 634 |
|
| 635 |
exit(1); |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
/* |
| 640 |
* Determine the pixmap size. |
| 641 |
*/ |
| 642 |
|
| 643 |
int Image_Size(image) |
| 644 |
XImage *image; |
| 645 |
{ |
| 646 |
if (image->format != ZPixmap) |
| 647 |
return(image->bytes_per_line * image->height * image->depth); |
| 648 |
|
| 649 |
return(image->bytes_per_line * image->height); |
| 650 |
} |
| 651 |
|
| 652 |
#define lowbit(x) ((x) & (~(x) + 1)) |
| 653 |
|
| 654 |
static int |
| 655 |
ReadColors(vis,cmap,colors) |
| 656 |
Visual *vis ; |
| 657 |
Colormap cmap ; |
| 658 |
XColor **colors ; |
| 659 |
{ |
| 660 |
int i,ncolors ; |
| 661 |
|
| 662 |
ncolors = vis->map_entries; |
| 663 |
|
| 664 |
if (!(*colors = (XColor *) malloc (sizeof(XColor) * ncolors))) |
| 665 |
Fatal_Error("Out of memory!"); |
| 666 |
|
| 667 |
if (vis->class == DirectColor || |
| 668 |
vis->class == TrueColor) { |
| 669 |
Pixel red, green, blue, red1, green1, blue1; |
| 670 |
|
| 671 |
red = green = blue = 0; |
| 672 |
red1 = lowbit(vis->red_mask); |
| 673 |
green1 = lowbit(vis->green_mask); |
| 674 |
blue1 = lowbit(vis->blue_mask); |
| 675 |
for (i=0; i<ncolors; i++) { |
| 676 |
(*colors)[i].pixel = red|green|blue; |
| 677 |
(*colors)[i].pad = 0; |
| 678 |
red += red1; |
| 679 |
if (red > vis->red_mask) |
| 680 |
red = 0; |
| 681 |
green += green1; |
| 682 |
if (green > vis->green_mask) |
| 683 |
green = 0; |
| 684 |
blue += blue1; |
| 685 |
if (blue > vis->blue_mask) |
| 686 |
blue = 0; |
| 687 |
} |
| 688 |
} else { |
| 689 |
for (i=0; i<ncolors; i++) { |
| 690 |
(*colors)[i].pixel = i; |
| 691 |
(*colors)[i].pad = 0; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
XQueryColors(dpy, cmap, *colors, ncolors); |
| 696 |
|
| 697 |
return(ncolors); |
| 698 |
} |
| 699 |
|
| 700 |
/* |
| 701 |
* Get the XColors of all pixels in image - returns # of colors |
| 702 |
*/ |
| 703 |
int Get_XColors(win_info, colors) |
| 704 |
XWindowAttributes *win_info; |
| 705 |
XColor **colors; |
| 706 |
{ |
| 707 |
int i, ncolors; |
| 708 |
Colormap cmap = win_info->colormap; |
| 709 |
|
| 710 |
if (use_installed) |
| 711 |
/* assume the visual will be OK ... */ |
| 712 |
cmap = XListInstalledColormaps(dpy, win_info->root, &i)[0]; |
| 713 |
if (!cmap) |
| 714 |
return(0); |
| 715 |
ncolors = ReadColors(win_info->visual,cmap,colors) ; |
| 716 |
return ncolors ; |
| 717 |
} |
| 718 |
|
| 719 |
_swapshort (bp, n) |
| 720 |
register char *bp; |
| 721 |
register unsigned n; |
| 722 |
{ |
| 723 |
register char c; |
| 724 |
register char *ep = bp + n; |
| 725 |
|
| 726 |
while (bp < ep) { |
| 727 |
c = *bp; |
| 728 |
*bp = *(bp + 1); |
| 729 |
bp++; |
| 730 |
*bp++ = c; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
_swaplong (bp, n) |
| 735 |
register char *bp; |
| 736 |
register unsigned n; |
| 737 |
{ |
| 738 |
register char c; |
| 739 |
register char *ep = bp + n; |
| 740 |
register char *sp; |
| 741 |
|
| 742 |
while (bp < ep) { |
| 743 |
sp = bp + 3; |
| 744 |
c = *sp; |
| 745 |
*sp = *bp; |
| 746 |
*bp++ = c; |
| 747 |
sp = bp + 1; |
| 748 |
c = *sp; |
| 749 |
*sp = *bp; |
| 750 |
*bp++ = c; |
| 751 |
bp += 2; |
| 752 |
} |
| 753 |
} |