summaryrefslogtreecommitdiff
path: root/xc/programs/xwd/xwd.c
blob: 1a0214311761e8ed7dde93c1e50c815241967668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include <X11/copyright.h>

/* Copyright 1987 Massachusetts Institute of Technology */

/*
 * xwd.c MIT Project Athena, X Window system window raster image dumper.
 *
 * This program will dump a raster image of the contents of a window into a 
 * file for output on graphics printers or for other uses.
 *
 *  Author:	Tony Della Fera, DEC
 *		17-Jun-85
 * 
 *  Modification history:
 *
 *  11/14/86 Bill Wyatt, Smithsonian Astrophysical Observatory
 *    - Removed Z format option, changing it to an XY option. Monochrome 
 *      windows will always dump in XY format. Color windows will dump
 *      in Z format by default, but can be dumped in XY format with the
 *      -xy option.
 *
 *  11/18/86 Bill Wyatt
 *    - VERSION 6 is same as version 5 for monchrome. For colors, the 
 *      appropriate number of Color structs are dumped after the header,
 *      which has the number of colors (=0 for monochrome) in place of the
 *      V5 padding at the end. Up to 16-bit displays are supported. I
 *      don't yet know how 24- to 32-bit displays will be handled under
 *      the Version 11 protocol.
 *
 *  6/15/87 David Krikorian, MIT Project Athena
 *    - VERSION 7 runs under the X Version 11 servers, while the previous
 *      versions of xwd were are for X Version 10.  This version is based
 *      on xwd version 6, and should eventually have the same color
 *      abilities. (Xwd V7 has yet to be tested on a color machine, so
 *      all color-related code is commented out until color support
 *      becomes practical.)
 */

#ifndef lint
static char *rcsid_xwd_c = "$Header: xwd.c,v 1.15 87/06/16 07:39:00 chariot Locked $";
#endif

/*%
 *%    This is the format for commenting out color-related code until
 *%  color can be supported.
%*/

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <sys/types.h>
#include <stdio.h>
#include <strings.h>

#define FAILURE 0

#define FEEP_VOLUME 0

/* Include routines to do parsing */
#include "jdisplay.h"

/* Setable Options */

int format;
Bool nobdrs = False;
Bool standard_out = True;
Bool debug = False;

main(argc, argv)
    int argc;
    char **argv;
{
    register i;
    Window target_win;
    FILE *out_file = stdout;

    INIT_NAME;

    Setup_Display_And_Screen(&argc, argv);

    for (i = 1; i < argc; i++) {
	if (!strcmp(argv[i], "-nobdrs")) {
	    nobdrs = True;
	    continue;
	}
	if (!strcmp(argv[i], "-debug")) {
	    debug = True;
	    continue;
	}
	if (!strcmp(argv[i], "-help"))
	  usage();
	if (!strcmp(argv[i], "-out")) {
	    if (++i >= argc) usage();
	    if (!(out_file = fopen(argv[i], "w")))
	      Error("Can't open output file as specified.");
	    standard_out = False;
	    continue;
	}
	if (!strcmp(argv[i], "-xy")) {
	    format = XYPixmap;
	    continue;
	}
	usage();
    }
    
    /*
     * Set the right pixmap format for the display type.
     */
    if(DisplayPlanes(dpy, screen) == 1) {
	format = XYBitmap;
    }
    else if(format != XYPixmap) {
	    format = ZPixmap;
    }

    /*
     * Let the user select the target window.
     */
    target_win = Select_Window(dpy);

    /*
     * Dump it!
     */
    Window_Dump(target_win, out_file);

    fclose(out_file);
}


/*
 * Window_Dump: dump a window to a file which must already be open for
 *              writting.
 */

char *calloc();

#include "XWDFile.h"

#define DONT_KNOW_YET 17

Window_Dump(window, out)
     Window window;
     FILE *out;
{
    register int i;
    register char *cbuffer;

    XColor *pixcolors;
    unsigned buffer_size;
    unsigned int virt_width, virt_height;
    int virt_x, virt_y;
    int win_name_size;
    int header_size;
    int ncolors = 0;
    int depth;
    int offset;
    long plane_mask;
    char win_name[100];
/*%
    XColor *scolor;
    XColor *bcolor;
    XColor *pixcolors;
%*/
    XWindowAttributes win_info;
    XImage *image;
/*%    *bcolor = *scolor = DONT_KNOW_YET    %*/

    XWDFileHeader header;

    
    /*
     * Inform the user not to alter the screen.
     */
    Beep();

    /*
     * Get the parameters of the window being dumped.
     */
    if (debug) outl("xwd: Getting target window information.\n");

    if(XGetWindowAttributes(dpy, window, &win_info) == FAILURE) 
      Fatal_Error("Can't query target window.\n");

    XFetchName(dpy, window, win_name);
    if (!win_name[0])
      strcpy(win_name, "xwdump");

    /*
     * sizeof(char) is included for the null string terminator.
     */
    win_name_size = strlen(win_name) + sizeof(char);

    /*
     * Calculate the virtual x, y, width and height of the window pane image
     * (this depends on whether or not the borders are included.
     */
    if (nobdrs) {
	if (debug) outl("xwd: Image without borders selected.\n");
	virt_x = 0;
	virt_y = 0;
	virt_width = win_info.width;
	virt_height = win_info.height;
    }
    else {
	if (debug) outl("xwd: Image with borders selected.\n");
	virt_x = win_info.x;
	virt_y = win_info.y;
	virt_width = win_info.width + (win_info.border_width << 1);
    	virt_height = win_info.height + (win_info.border_width << 1);
    }

    /*
     * Determine the pixmap size.
     */
    buffer_size = Pixmap_Size(virt_width, virt_height);

    /*
     * Snarf the pixmap with XGetImage.
     * Color windows get snarfed in Z format first to check the color
     * map allocations before resnarfing if XY format selected.
     */
    plane_mask = 1;

    /*    XGetImage() calls XCreateImage() internally, and this, in turn
     *  does the memory allocation so we don't have to.
     */

    image = XGetImage ( dpy, window, 0, 0, virt_width,
		       virt_height, plane_mask, format);

    if (debug) outl("xwd: Getting pixmap.\n");

    /*
     * Get XColors of all pixels used in the pixmap
     */

    ncolors = Get_XColors(image->data, &pixcolors, buffer_size);

	/* reread in XY format if necessary */
/*%
	if(format == XYPixmap) {
	    image = XGetImage(dpy, window, 0, 0, virt_width,
	                      virt_height, plane_mask, format);
%*/

/*%
 *%    Plane mask must be assigned a value.
%*/

/*%	}
   }
%*/

    /*
     * Inform the user that the image has been retrieved.
     */
    XBell(dpy, FEEP_VOLUME);
    XBell(dpy, FEEP_VOLUME);
    XFlush(dpy);

    /*
     * Calculate header size.
     */
    if (debug) outl("xwd: Calculating header size.\n");
    header_size = sizeof(header) + win_name_size;

    /*
     * Write out header information.
     */
    if (debug) outl("xwd: Constructing and dumping file header.\n");
    header.header_size = header_size;
    header.file_version = XWD_FILE_VERSION;
    header.display_type = 0; /* DisplayType(dpy, screen);  [obsolete] */
    header.display_planes = DisplayPlanes(dpy, screen);
    header.pixmap_format = format;
    header.pixmap_width = virt_width;
    header.pixmap_height = virt_height;
    header.window_width = win_info.width;
    header.window_height = win_info.height;
    header.window_x = win_info.x;
    header.window_y = win_info.y;
    header.window_bdrwidth = win_info.border_width;
    header.window_ncolors = 0;  /*%  = ncolors;  %*/

    (void) fwrite((char *)&header, sizeof(header), 1, out);
    (void) fwrite(win_name, win_name_size, 1, out);

    /*
     * Write out the color maps, if any
     */
/*%
    if (debug) outl(stderr,"xwd: Dumping %d colors.\n",ncolors);
    (void) fwrite(pixcolors, sizeof(XColor), ncolors, out);
%*/

    /*
     * Write out the buffer.
     */
    if (debug) outl("xwd: Dumping pixmap.  bufsize=%d\n",buffer_size);

/*
 *    This copying of the bit stream (data) to a file is to be replaced
 *  by an X server call which hasn't been written yet.  It is not clear
 *  what other functions of xwd will be taken over by this (as yet)
 *  non-existant X function.
 */
    (void) fwrite(image->data, (int) buffer_size, 1, out);

    /*
     * free the color buffer.
     */
/*%
    if(debug && ncolors > 0) outl("xwd: Freeing color map.\n");
    if(ncolors > 0) free(pixcolors);
%*/

    /*
     * Free window name string.
     */
    if (debug) outl("xwd: Freeing window name string.\n");
    free(win_name);
}

/*
 * Report the syntax for calling xwd.
 */
usage()
{
    outl("%s: %s [-debug] [-help] [-nobdrs] [-out <file>]\n", program_name);
    outl("                [-xy] [[host]:vs]\n");
    exit(1);
}


/*
 * Error - Fatal xwd error.
 */
extern int errno;

Error(string)
	char *string;	/* Error description string. */
{
	outl("\nxwd: Error => %s\n", string);
	if (errno != 0) {
		perror("xwd");
		outl("\n");
	}

	exit(1);
}


/*
 * Determine the pixmap size.
 */
#define UBPS (sizeof(short)/2) /* useful bytes per short */
#define BitmapSize(width, height) (((((width) + 15) >> 3) &~ 1) * (height) * UBPS)
#define XYPixmapSize(width, height, planes) (BitmapSize(width, height) * (planes))
#define BZPixmapSize(width, height) ((width) * (height))
#define WZPixmapSize(width, height) (((width) * (height)) << 1)

int Pixmap_Size(width, height)
     int width, height;
{
	int buffer_size;

	if (format == XYBitmap)
	  buffer_size = BitmapSize(width, height);

#ifdef COLOR

	else if (format == XYPixmap) {
	    buffer_size = XYPixmapSize(width, height,
				       DisplayPlanes(dpy, screen));
	    if (debug)
	      outl("xwd: Pixmap in XYFormat, size %d bytes.\n", buffer_size);
	}
	else if (DisplayPlanes(dpy, screen) < 9) {
	    buffer_size = BZPixmapSize(width, height);
	    if (debug)
	      outl("xwd: Pixmap in byte ZFormat, size %d bytes.\n",
		   buffer_size);
	}
	else {
	    buffer_size = WZPixmapSize(width, height);
	    if (debug)
	      outl("xwd: Pixmap in word ZFormat, size %d bytes.\n",
		   buffer_size);
    }
#endif

	return(buffer_size);
}


/*
 * Get the XColors of all pixels in image - returns # of colors
 */
int Get_XColors(buffer, pixcolors, buffer_size)
     char *buffer;
     XColor *pixcolors[];       /* RETURNED */
     int buffer_size;
{
    register int i, *histbuffer;
    register u_short *wbuffer;
    register char *buffer;
    int ncolors = 0;

    if (DisplayPlanes(dpy,screen)<2)
      return(0);

#ifdef COLOR

    if(DisplayPlanes(dpy, screen) < 9) {
	histbuffer = (int *)calloc(256, sizeof(int));
	bzero(histbuffer, 256*sizeof(int));
	*pixcolors = (XColor *)calloc(1, sizeof(XColor));
	for(i=0; i<buffer_size; i++) {
	    /* if previously found, skip color query */
	    if(histbuffer[(int)buffer[i]] == 0) {
		*pixcolors = 
		  (XColor *)realloc(*pixcolors, sizeof(XColor)*(++ncolors));
		if(debug)
		  outl("Color %3d at pixel val %5d, i= %5d =",
		       ncolors, buffer[i], i);
		histbuffer[(int)buffer[i]]++;
		(*pixcolors)[ncolors-1].pixel = (int)buffer[i];
		if(XQueryColor(dpy, pixcolors[ncolors-1]) == 0) 
		  Fatal_Error("Unable to query color table?");
		if(debug) outl("%5d %5d %5d\n",
			       (*pixcolors)[ncolors-1].red,
			       (*pixcolors)[ncolors-1].green,
			       (*pixcolors)[ncolors-1].blue);
	    }
	}
    }
    else if(DisplayPlanes(dpy, screen) < 17) {
	wbuffer = (u_short *)buffer;
	histbuffer = (int *)calloc(65536, sizeof(int));
	bzero(histbuffer, 65536*sizeof(int));
	*pixcolors = (XColor *)calloc(1, sizeof(XColor));
	for(i=0; i<(buffer_size/sizeof(u_short)); i++) {

	    /* if previously found, skip color query */

	    if(histbuffer[(int)wbuffer[i]] == 0) {
		*pixcolors = 
		  (XColor *)realloc(*pixcolors, sizeof(XColor)*(++ncolors));
		if(debug)
		  outl("Color %2d at pixel val %d, i= %d =",
		       ncolors, wbuffer[i], i);
		histbuffer[(int)wbuffer[i]]++;
		(*pixcolors)[ncolors-1].pixel = (int)wbuffer[i];
		if(XQueryColor(dpy, pixcolors[ncolors-1]) == 0) 
		  Fatal_Error("Unable to query color table?");
		if(debug) outl("%d %d %d\n",
			       (*pixcolors)[ncolors-1].red,
			       (*pixcolors)[ncolors-1].green,
			       (*pixcolors)[ncolors-1].blue);
	    }
	}
    } 

    else
      if(DisplayPlanes(dpy, screen) > 16)
	Fatal_Error("Unable to handle more than 16 planes at this time");
    
    free(histbuffer);

#endif
    
    return(ncolors);
}