Annotation of researchv9/X11/src/X.V11R1/clients/xwd/xwd.c, revision 1.1

1.1     ! root        1: #include <X11/copyright.h>
        !             2: 
        !             3: /* Copyright 1987 Massachusetts Institute of Technology */
        !             4: 
        !             5: /*
        !             6:  * xwd.c MIT Project Athena, X Window system window raster image dumper.
        !             7:  *
        !             8:  * This program will dump a raster image of the contents of a window into a 
        !             9:  * file for output on graphics printers or for other uses.
        !            10:  *
        !            11:  *  Author:    Tony Della Fera, DEC
        !            12:  *             17-Jun-85
        !            13:  * 
        !            14:  *  Modification history:
        !            15:  *
        !            16:  *  11/14/86 Bill Wyatt, Smithsonian Astrophysical Observatory
        !            17:  *    - Removed Z format option, changing it to an XY option. Monochrome 
        !            18:  *      windows will always dump in XY format. Color windows will dump
        !            19:  *      in Z format by default, but can be dumped in XY format with the
        !            20:  *      -xy option.
        !            21:  *
        !            22:  *  11/18/86 Bill Wyatt
        !            23:  *    - VERSION 6 is same as version 5 for monchrome. For colors, the 
        !            24:  *      appropriate number of Color structs are dumped after the header,
        !            25:  *      which has the number of colors (=0 for monochrome) in place of the
        !            26:  *      V5 padding at the end. Up to 16-bit displays are supported. I
        !            27:  *      don't yet know how 24- to 32-bit displays will be handled under
        !            28:  *      the Version 11 protocol.
        !            29:  *
        !            30:  *  6/15/87 David Krikorian, MIT Project Athena
        !            31:  *    - VERSION 7 runs under the X Version 11 servers, while the previous
        !            32:  *      versions of xwd were are for X Version 10.  This version is based
        !            33:  *      on xwd version 6, and should eventually have the same color
        !            34:  *      abilities. (Xwd V7 has yet to be tested on a color machine, so
        !            35:  *      all color-related code is commented out until color support
        !            36:  *      becomes practical.)
        !            37:  */
        !            38: 
        !            39: #ifndef lint
        !            40: static char *rcsid_xwd_c = "$Header: xwd.c,v 1.28 87/09/11 16:57:09 rws Exp $";
        !            41: #endif
        !            42: 
        !            43: /*%
        !            44:  *%    This is the format for commenting out color-related code until
        !            45:  *%  color can be supported.
        !            46: %*/
        !            47: 
        !            48: #include <X11/Xlib.h>
        !            49: #include <X11/Xutil.h>
        !            50: #include <sys/types.h>
        !            51: #include <stdio.h>
        !            52: #include <strings.h>
        !            53: 
        !            54: #define FEEP_VOLUME 0
        !            55: 
        !            56: /* Include routines to do parsing */
        !            57: #include "dsimple.h"
        !            58: 
        !            59: /* Setable Options */
        !            60: 
        !            61: int format = ZPixmap;
        !            62: Bool nobdrs = False;
        !            63: Bool standard_out = True;
        !            64: Bool debug = False;
        !            65: 
        !            66: extern int (*_XErrorFunction)();
        !            67: extern int _XDefaultError();
        !            68: 
        !            69: main(argc, argv)
        !            70:     int argc;
        !            71:     char **argv;
        !            72: {
        !            73:     register i;
        !            74:     Window target_win;
        !            75:     FILE *out_file = stdout;
        !            76: 
        !            77:     INIT_NAME;
        !            78: 
        !            79:     Setup_Display_And_Screen(&argc, argv);
        !            80: 
        !            81:     /* Get window select on command line, if any */
        !            82:     target_win = Select_Window_Args(&argc, argv);
        !            83: 
        !            84:     for (i = 1; i < argc; i++) {
        !            85:        if (!strcmp(argv[i], "-nobdrs")) {
        !            86:            nobdrs = True;
        !            87:            continue;
        !            88:        }
        !            89:        if (!strcmp(argv[i], "-debug")) {
        !            90:            debug = True;
        !            91:            continue;
        !            92:        }
        !            93:        if (!strcmp(argv[i], "-help"))
        !            94:          usage();
        !            95:        if (!strcmp(argv[i], "-out")) {
        !            96:            if (++i >= argc) usage();
        !            97:            if (!(out_file = fopen(argv[i], "w")))
        !            98:              Error("Can't open output file as specified.");
        !            99:            standard_out = False;
        !           100:            continue;
        !           101:        }
        !           102:        if (!strcmp(argv[i], "-xy")) {
        !           103:            format = XYPixmap;
        !           104:            continue;
        !           105:        }
        !           106:        usage();
        !           107:     }
        !           108:     
        !           109:     /*
        !           110:      * Let the user select the target window.
        !           111:      */
        !           112:     if (!target_win)
        !           113:       target_win = Select_Window(dpy);
        !           114: 
        !           115:     /*
        !           116:      * Dump it!
        !           117:      */
        !           118:     Window_Dump(target_win, out_file);
        !           119: 
        !           120:     fclose(out_file);
        !           121: }
        !           122: 
        !           123: 
        !           124: /*
        !           125:  * Window_Dump: dump a window to a file which must already be open for
        !           126:  *              writting.
        !           127:  */
        !           128: 
        !           129: char *calloc();
        !           130: 
        !           131: #include "X11/XWDFile.h"
        !           132: 
        !           133: Window_Dump(window, out)
        !           134:      Window window;
        !           135:      FILE *out;
        !           136: {
        !           137:     unsigned long swaptest = 1;
        !           138:     XColor *colors;
        !           139:     unsigned buffer_size;
        !           140:     int win_name_size;
        !           141:     int header_size;
        !           142:     int ncolors, i;
        !           143:     char win_name[100];
        !           144:     XWindowAttributes win_info;
        !           145:     XImage *image;
        !           146: 
        !           147:     XWDFileHeader header;
        !           148: 
        !           149:     
        !           150:     /*
        !           151:      * Inform the user not to alter the screen.
        !           152:      */
        !           153:     Beep();
        !           154: 
        !           155:     /*
        !           156:      * Get the parameters of the window being dumped.
        !           157:      */
        !           158:     if (debug) outl("xwd: Getting target window information.\n");
        !           159:     if(!XGetWindowAttributes(dpy, window, &win_info)) 
        !           160:       Fatal_Error("Can't get target window attributes.");
        !           161: 
        !           162:     XFetchName(dpy, window, win_name);
        !           163:     if (!win_name[0])
        !           164:       strcpy(win_name, "xwdump");
        !           165: 
        !           166:     /* sizeof(char) is included for the null string terminator. */
        !           167:     win_name_size = strlen(win_name) + sizeof(char);
        !           168: 
        !           169:     /*
        !           170:      * Snarf the pixmap with XGetImage.
        !           171:      */
        !           172: 
        !           173:     if (nobdrs) {
        !           174:        if (debug) outl("xwd: Image without borders selected.\n");
        !           175:        image = XGetImage ( dpy, window, 0, 0, win_info.width,
        !           176:                           win_info.height, ~0, format); 
        !           177:       }
        !           178:     else {
        !           179:        if (debug) outl("xwd: Image with borders selected.\n");
        !           180:        image = XGetImage ( dpy, window,
        !           181:                           -win_info.border_width, -win_info.border_width, 
        !           182:                           win_info.width + (win_info.border_width << 1),
        !           183:                           win_info.height + (win_info.border_width << 1),
        !           184:                           ~0, format); 
        !           185:       }
        !           186:     if (debug) outl("xwd: Getting pixmap.\n");
        !           187: 
        !           188:     /*
        !           189:      * Determine the pixmap size.
        !           190:      */
        !           191:     buffer_size = Image_Size(image);
        !           192: 
        !           193:     if (debug) outl("xwd: Getting Colors.\n");
        !           194: 
        !           195:     ncolors = Get_XColors(&win_info, &colors);
        !           196: 
        !           197:     /*
        !           198:      * Inform the user that the image has been retrieved.
        !           199:      */
        !           200:     XBell(dpy, FEEP_VOLUME);
        !           201:     XBell(dpy, FEEP_VOLUME);
        !           202:     XFlush(dpy);
        !           203: 
        !           204:     /*
        !           205:      * Calculate header size.
        !           206:      */
        !           207:     if (debug) outl("xwd: Calculating header size.\n");
        !           208:     header_size = sizeof(header) + win_name_size;
        !           209: 
        !           210:     /*
        !           211:      * Write out header information.
        !           212:      */
        !           213:     if (debug) outl("xwd: Constructing and dumping file header.\n");
        !           214:     header.header_size = (xwdval) header_size;
        !           215:     header.file_version = (xwdval) XWD_FILE_VERSION;
        !           216:     header.pixmap_format = (xwdval) format;
        !           217:     header.pixmap_depth = (xwdval) image->depth;
        !           218:     header.pixmap_width = (xwdval) image->width;
        !           219:     header.pixmap_height = (xwdval) image->height;
        !           220:     header.xoffset = (xwdval) image->xoffset;
        !           221:     header.byte_order = (xwdval) image->byte_order;
        !           222:     header.bitmap_unit = (xwdval) image->bitmap_unit;
        !           223:     header.bitmap_bit_order = (xwdval) image->bitmap_bit_order;
        !           224:     header.bitmap_pad = (xwdval) image->bitmap_pad;
        !           225:     header.bits_per_pixel = (xwdval) image->bits_per_pixel;
        !           226:     header.bytes_per_line = (xwdval) image->bytes_per_line;
        !           227:     header.visual_class = (xwdval) win_info.visual->class;
        !           228:     header.red_mask = (xwdval) win_info.visual->red_mask;
        !           229:     header.green_mask = (xwdval) win_info.visual->green_mask;
        !           230:     header.blue_mask = (xwdval) win_info.visual->blue_mask;
        !           231:     header.bits_per_rgb = (xwdval) win_info.visual->bits_per_rgb;
        !           232:     header.colormap_entries = (xwdval) win_info.visual->map_entries;
        !           233:     header.ncolors = ncolors;
        !           234:     header.window_width = (xwdval) win_info.width;
        !           235:     header.window_height = (xwdval) win_info.height;
        !           236:     if (nobdrs) {
        !           237:       header.window_x = (xwdval) (win_info.x + win_info.border_width);
        !           238:       header.window_y = (xwdval) (win_info.y + win_info.border_width);
        !           239:     } else {
        !           240:       header.window_x = (xwdval) win_info.x;
        !           241:       header.window_y = (xwdval) win_info.y;
        !           242:     }
        !           243:     header.window_bdrwidth = (xwdval) win_info.border_width;
        !           244: 
        !           245:     if (*(char *) &swaptest) {
        !           246:        _swaplong((char *) &header, sizeof(header));
        !           247:        for (i = 0; i < ncolors; i++) {
        !           248:            _swaplong((char *) &colors[i].pixel, sizeof(long));
        !           249:            _swapshort((char *) &colors[i].red, 3 * sizeof(short));
        !           250:        }
        !           251:     }
        !           252: 
        !           253:     (void) fwrite((char *)&header, sizeof(header), 1, out);
        !           254:     (void) fwrite(win_name, win_name_size, 1, out);
        !           255: 
        !           256:     /*
        !           257:      * Write out the color maps, if any
        !           258:      */
        !           259: 
        !           260:     if (debug) outl("xwd: Dumping %d colors.\n", ncolors);
        !           261:     (void) fwrite((char *) colors, sizeof(XColor), ncolors, out);
        !           262: 
        !           263:     /*
        !           264:      * Write out the buffer.
        !           265:      */
        !           266:     if (debug) outl("xwd: Dumping pixmap.  bufsize=%d\n",buffer_size);
        !           267: 
        !           268:     /*
        !           269:      *    This copying of the bit stream (data) to a file is to be replaced
        !           270:      *  by an Xlib call which hasn't been written yet.  It is not clear
        !           271:      *  what other functions of xwd will be taken over by this (as yet)
        !           272:      *  non-existant X function.
        !           273:      */
        !           274:     (void) fwrite(image->data, (int) buffer_size, 1, out);
        !           275: 
        !           276:     /*
        !           277:      * free the color buffer.
        !           278:      */
        !           279: 
        !           280:     if(debug && ncolors > 0) outl("xwd: Freeing colors.\n");
        !           281:     if(ncolors > 0) free(colors);
        !           282: 
        !           283:     /*
        !           284:      * Free window name string.
        !           285:      */
        !           286:     if (debug) outl("xwd: Freeing window name string.\n");
        !           287:     free(win_name);
        !           288: 
        !           289:     /*
        !           290:      * Free image
        !           291:      */
        !           292:     XDestroyImage(image);
        !           293: }
        !           294: 
        !           295: /*
        !           296:  * Report the syntax for calling xwd.
        !           297:  */
        !           298: usage()
        !           299: {
        !           300:     printf("%s: %s [-debug] [-help] %s [-nobdrs] [-out <file>]",
        !           301:           program_name, SELECT_USAGE);
        !           302:     printf(" [-xy] [[host]:vs]\n");
        !           303:     exit(1);
        !           304: }
        !           305: 
        !           306: 
        !           307: /*
        !           308:  * Error - Fatal xwd error.
        !           309:  */
        !           310: extern int errno;
        !           311: 
        !           312: Error(string)
        !           313:        char *string;   /* Error description string. */
        !           314: {
        !           315:        outl("\nxwd: Error => %s\n", string);
        !           316:        if (errno != 0) {
        !           317:                perror("xwd");
        !           318:                outl("\n");
        !           319:        }
        !           320: 
        !           321:        exit(1);
        !           322: }
        !           323: 
        !           324: 
        !           325: /*
        !           326:  * Determine the pixmap size.
        !           327:  */
        !           328: 
        !           329: int Image_Size(image)
        !           330:      XImage *image;
        !           331: {
        !           332:     if (format != ZPixmap)
        !           333:       return(image->bytes_per_line * image->height * image->depth);
        !           334: 
        !           335:     return(image->bytes_per_line * image->height);
        !           336: }
        !           337: 
        !           338: 
        !           339: /*
        !           340:  * Get the XColors of all pixels in image - returns # of colors
        !           341:  */
        !           342: int Get_XColors(win_info, colors)
        !           343:      XWindowAttributes *win_info;
        !           344:      XColor **colors;
        !           345: {
        !           346:     int i, ncolors;
        !           347: 
        !           348:     if (!win_info->colormap)
        !           349:        return(0);
        !           350: 
        !           351:     if (win_info->visual->class == TrueColor ||
        !           352:        win_info->visual->class == DirectColor)
        !           353:        return(0);    /* XXX punt for now */
        !           354: 
        !           355:     ncolors = win_info->visual->map_entries;
        !           356:     if (!(*colors = (XColor *) malloc (sizeof(XColor) * ncolors)))
        !           357:       Fatal_Error("Out of memory!");
        !           358: 
        !           359:     for (i=0; i<ncolors; i++)
        !           360:       (*colors)[i].pixel = i;
        !           361: 
        !           362:     XQueryColors(dpy, win_info->colormap, *colors, ncolors);
        !           363:     
        !           364:     return(ncolors);
        !           365: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.