home *** CD-ROM | disk | FTP | other *** search
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % X X TTTTT OOO PPPP SSSSS %
- % X X T O O P P S %
- % X T O O PPPP SSS %
- % X X T O O P S %
- % X X T OOO P SSSSS %
- % %
- % %
- % Import X11 image to a Postscript format. %
- % %
- % %
- % Software Design %
- % John Cristy %
- % October 1990 %
- % %
- % %
- % Copyright 1990 E. I. Dupont de Nemours & Company %
- % %
- % Permission to use, copy, modify, distribute, and sell this software and %
- % its documentation for any purpose is hereby granted without fee, %
- % provided that the above Copyright notice appear in all copies and that %
- % both that Copyright notice and this permission notice appear in %
- % supporting documentation, and that the name of E. I. Dupont de Nemours %
- % & Company not be used in advertising or publicity pertaining to %
- % distribution of the software without specific, written prior %
- % permission. E. I. Dupont de Nemours & Company makes no representations %
- % about the suitability of this software for any purpose. It is provided %
- % "as is" without express or implied warranty. %
- % %
- % E. I. Dupont de Nemours & Company disclaims all warranties with regard %
- % to this software, including all implied warranties of merchantability %
- % and fitness, in no event shall E. I. Dupont de Nemours & Company be %
- % liable for any special, indirect or consequential damages or any %
- % damages whatsoever resulting from loss of use, data or profits, whether %
- % in an action of contract, negligence or other tortious action, arising %
- % out of or in connection with the use or performance of this software. %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % The XtoPS program reads an image from any visible window on an X server
- % and outputs it to encapsulated Postscript file. You can view this file
- % with any Postscript compatible viewer or printer. The image is displayed in
- % color on viewers or printers that support color Postscript, otherwise it is
- % displayed as grayscale.
- %
- % The XtoPS program command syntax is:
- %
- % Usage: XtoPS [options ...] file
- %
- % Where options include:
- % -border include image borders in the output image
- % -display name X server to contact
- % -frame include window manager frame
- % -id number select window with this id
- % -name name select window with this name
- % -root select root window
- %
- % Change '-' to '+' in any option above to reverse its effect.
- % For example, +frame means do not include window manager frame.
- %
- % Specify 'file' as '-' for standard input or output.
- %
- %
- */
-
- #include <X11/Xos.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <X11/Xatom.h>
- #include <X11/cursorfont.h>
- #include "display.h"
- #ifdef PRE_R4_ICCCM
- #include "PreR4Icccm.h"
- #endif
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % C l i e n t W i n d o w %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function ClientWindow finds a window, at or below the specified window,
- % which has a WM_STATE property. If such a window is found, it is returned,
- % otherwise the argument window is returned.
- %
- % The format of the ClientWindow function is:
- %
- % client_window=ClientWindow(display,target_window)
- %
- % A description of each parameter follows:
- %
- % o client_window: ClientWindow returns a window, at or below the specified
- % window, which has a WM_STATE property otherwise the argument
- % target_window is returned.
- %
- % o display: Specifies a pointer to the Display structure; returned from
- % XOpenDisplay.
- %
- % o target_window: Specifies the window to find a WM_STATE property.
- %
- %
- */
- static Window ClientWindow(display,target_window)
- Display
- *display;
-
- Window
- target_window;
- {
- Atom
- state,
- type;
-
- int
- format;
-
- static Window
- WindowByProperty();
-
- unsigned char
- *data;
-
- unsigned long
- after,
- number_items;
-
- Window
- client_window;
-
- state=XInternAtom(display,"WM_STATE",True);
- if (state == (Atom) NULL)
- return(target_window);
- type=(Atom) NULL;
- (void) XGetWindowProperty(display,target_window,state,0L,0L,False,
- AnyPropertyType,&type,&format,&number_items,&after,&data);
- if (type != (Atom) NULL)
- return(target_window);
- client_window=WindowByProperty(display,target_window,state);
- if (client_window == (Window) NULL)
- return(target_window);
- return(client_window);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % E r r o r %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function Error displays an error message and then terminates the program.
- %
- % The format of the Error routine is:
- %
- % Error(message,qualifier)
- %
- % A description of each parameter follows:
- %
- % o message: Specifies the message to display before terminating the
- % program.
- %
- % o qualifier: Specifies any qualifier to the message.
- %
- %
- */
- void Error(message,qualifier)
- char
- *message,
- *qualifier;
- {
- (void) fprintf(stderr,"%s: %s",application_name,message);
- if (qualifier != (char *) NULL)
- (void) fprintf(stderr," %s",qualifier);
- (void) fprintf(stderr,".\n");
- exit(1);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % I s T r u e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function IsTrue returns True if the boolean is "true", "on", "yes" or "1".
- %
- % The format of the IsTrue routine is:
- %
- % option=IsTrue(boolean)
- %
- % A description of each parameter follows:
- %
- % o option: either True or False depending on the boolean parameter.
- %
- % o boolean: Specifies a pointer to a character array.
- %
- %
- */
- static int IsTrue(boolean)
- char
- *boolean;
- {
- char
- c,
- *p;
-
- if (boolean == (char *) NULL)
- return(False);
- for (p=boolean; *p != (char) NULL; p++)
- {
- /*
- Convert to lower case.
- */
- c=(*p);
- if (isascii(c) && isupper(c))
- *p=tolower(c);
- }
- if (strcmp(boolean,"true") == 0)
- return(True);
- if (strcmp(boolean,"on") == 0)
- return(True);
- if (strcmp(boolean,"yes") == 0)
- return(True);
- if (strcmp(boolean,"1") == 0)
- return(True);
- return(False);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % P r i n t I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function PrintImage translates a MIFF image to encapsulated Postscript for
- % printing.
- %
- % The format of the PrintImage routine is:
- %
- % status=PrintImage(image)
- %
- % A description of each parameter follows:
- %
- % o status: Function PrintImage return True if the image is printed.
- % False is returned if the image file cannot be opened for printing.
- %
- % o image: The address of a structure of type Image; returned from
- % ReadImage.
- %
- %
- */
- unsigned int PrintImage(image)
- Image
- *image;
- {
- #define PointsPerInch 72.0
- #define PageBottomMargin 1.27
- #define PageLeftMargin 0.21
- #define PageWidth 8.5
- #define PageHeight 11.0
-
- static char
- *commands[]=
- {
- "%",
- "% Display a runlength-encoded color image. The image is displayed",
- "% in color on viewers and printers that support color Postscript,",
- "% otherwise it is displayed as grayscale.",
- "%",
- "/buffer 512 string def",
- "/byte 1 string def",
- "/color_packet 3 string def",
- "/gray_packet 1 string def",
- "/pixels 768 string def",
- "",
- "/DirectClassPacket",
- "{",
- " %",
- " % Get a runlength-encoded DirectClass packet.",
- " %",
- " % Parameters:",
- " % length: number of pixels minus one of this color.",
- " % red.",
- " % green.",
- " % blue.",
- " %",
- " currentfile byte readhexstring pop 0 get",
- " /number_pixels exch 1 add 3 mul def",
- " currentfile color_packet readhexstring pop pop",
- " 0 3 number_pixels 1 sub",
- " {",
- " pixels exch color_packet putinterval",
- " } for",
- " pixels 0 number_pixels getinterval",
- "} bind def",
- "",
- "/DirectClassImage",
- "{",
- " %",
- " % Display a runlength-encoded DirectClass image.",
- " %",
- " systemdict /colorimage known",
- " {",
- " columns rows 8",
- " [",
- " columns 0 0",
- " rows neg 0 rows",
- " ]",
- " { DirectClassPacket } false 3 colorimage",
- " }",
- " {",
- " %",
- " % No colorimage operator; convert to grayscale.",
- " %",
- " columns rows 8",
- " [",
- " columns 0 0",
- " rows neg 0 rows",
- " ]",
- " { GrayDirectClassPacket } image",
- " } ifelse",
- "} bind def",
- "",
- "/GrayDirectClassPacket",
- "{",
- " %",
- " % Get a runlength-encoded DirectClass packet; convert to grayscale.",
- " %",
- " % Parameters:",
- " % length: number of pixels minus one of this color",
- " % red",
- " % green",
- " % blue",
- " %",
- " currentfile byte readhexstring pop 0 get",
- " /number_pixels exch 1 add def",
- " currentfile color_packet readhexstring pop pop",
- " color_packet 0 get 0.299 mul",
- " color_packet 1 get 0.587 mul add",
- " color_packet 2 get 0.114 mul add",
- " cvi",
- " /gray_packet exch def",
- " 0 1 number_pixels 1 sub",
- " {",
- " pixels exch gray_packet put",
- " } for",
- " pixels 0 number_pixels getinterval",
- "} bind def",
- "",
- "/GrayPseudoClassPacket",
- "{",
- " %",
- " % Get a runlength-encoded PseudoClass packet; convert to grayscale.",
- " %",
- " % Parameters:",
- " %",
- " % length: number of pixels minus one of this color.",
- " % index: index into the colormap.",
- " %",
- " currentfile byte readhexstring pop 0 get",
- " /number_pixels exch 1 add def",
- " currentfile byte readhexstring pop 0 get",
- " /offset exch 3 mul def",
- " /color_packet colormap offset 3 getinterval def",
- " color_packet 0 get 0.299 mul",
- " color_packet 1 get 0.587 mul add",
- " color_packet 2 get 0.114 mul add",
- " cvi",
- " /gray_packet exch def",
- " 0 1 number_pixels 1 sub",
- " {",
- " pixels exch gray_packet put",
- " } for",
- " pixels 0 number_pixels getinterval",
- "} bind def",
- "",
- "/PseudoClassPacket",
- "{",
- " %",
- " % Get a runlength-encoded PseudoClass packet.",
- " %",
- " % Parameters:",
- " % length: number of pixels minus one of this color.",
- " % index: index into the colormap.",
- " %",
- " %",
- " currentfile byte readhexstring pop 0 get",
- " /number_pixels exch 1 add 3 mul def",
- " currentfile byte readhexstring pop 0 get",
- " /offset exch 3 mul def",
- " /color_packet colormap offset 3 getinterval def",
- " 0 3 number_pixels 1 sub",
- " {",
- " pixels exch color_packet putinterval",
- " } for",
- " pixels 0 number_pixels getinterval",
- "} bind def",
- "",
- "/PseudoClassImage",
- "{",
- " %",
- " % Display a runlength-encoded PseudoClass image.",
- " %",
- " % Parameters:",
- " %",
- " % colors: number of colors in the colormap.",
- " % colormap: red, green, blue color packets.",
- " %",
- " currentfile buffer readline pop",
- " token { /colors exch def } { } ifelse",
- " /colors colors 3 mul def",
- " /colormap colors string def",
- " currentfile colormap readhexstring pop pop",
- " systemdict /colorimage known",
- " {",
- " columns rows 8",
- " [",
- " columns 0 0",
- " rows neg 0 rows",
- " ]",
- " { PseudoClassPacket } false 3 colorimage",
- " }",
- " {",
- " %",
- " % No colorimage operator; convert to grayscale.",
- " %",
- " columns rows 8",
- " [",
- " columns 0 0",
- " rows neg 0 rows",
- " ]",
- " { GrayPseudoClassPacket } image",
- " } ifelse",
- "} bind def",
- "",
- "/DisplayImage",
- "{",
- " %",
- " % Display a runlength-encoded DirectClass or PseudoClass image.",
- " %",
- " % Parameters:",
- " % degrees rotation.",
- " % x & y translation.",
- " % x & y scale.",
- " % image columns & rows.",
- " % class: DirectClass or PseudoClass.",
- " % hex color runlength-encoded packets.",
- " %",
- " gsave",
- " currentfile buffer readline pop",
- " token { /degrees exch def } { } ifelse",
- " degrees rotate",
- " currentfile buffer readline pop",
- " token { /x exch def } { } ifelse",
- " token { /y exch def } { } ifelse",
- " x y translate",
- " currentfile buffer readline pop",
- " token { /x exch def } { } ifelse",
- " token { /y exch def } { } ifelse",
- " x y scale",
- " currentfile buffer readline pop",
- " token { /columns exch def } { } ifelse",
- " token { /rows exch def } { } ifelse",
- " currentfile buffer readline pop",
- " token { /class exch def } { } ifelse",
- " class 0 gt { PseudoClassImage } { DirectClassImage } ifelse",
- " grestore",
- " showpage",
- "} bind def",
- "",
- "DisplayImage",
- NULL
- };
-
- char
- **q;
-
- double
- delta_x,
- delta_y,
- image_height,
- image_width,
- max,
- min,
- rotate,
- scale,
- scale_x,
- scale_y,
- translate_x,
- translate_y;
-
- register RunlengthPacket
- *p;
-
- register int
- i,
- j;
-
- /*
- Open output image file.
- */
- if (*image->filename == '-')
- image->file=stdout;
- else
- image->file=fopen(image->filename,"w");
- if (image->file == (FILE *) NULL)
- {
- (void) fprintf(stderr,"%s: unable to print image, cannot open %s.\n",
- application_name,image->filename);
- return(False);
- }
- /*
- Compute image rotation.
- */
- if (((double) image->columns/(double) image->rows) > 1.0)
- rotate=(-90.0);
- else
- rotate=0.0;
- /*
- Compute image scaling.
- */
- image_width=(double) image->columns/PointsPerInch;
- image_height=(double) image->rows/PointsPerInch;
- /*
- Check max page sizes
- */
- max=image_width > image_height ? image_width : image_height;
- if (max > (PageHeight-(2.0*PageBottomMargin)))
- {
- scale=(PageHeight-(2.0*PageBottomMargin))/max;
- image_height*=scale;
- image_width*=scale;
- }
- min=image_width > image_height ? image_height : image_width;
- if (min > (PageWidth-(2.0*PageLeftMargin)))
- {
- scale=(PageWidth-(2.0*PageLeftMargin))/min;
- image_width*=scale;
- image_height*=scale;
- }
- scale_x=image_width*PointsPerInch;
- scale_y=image_height*PointsPerInch;
- translate_x=0.0;
- translate_y=0.0;
- if (rotate == 0.0)
- {
- delta_x=PageWidth-(image_width+(2.0*PageLeftMargin));
- delta_y=PageHeight-(image_height+(2.0*PageBottomMargin));
- if (delta_x >= 0.0)
- translate_x=((delta_x/2.0+PageLeftMargin)*PointsPerInch);
- else
- translate_x=PageLeftMargin*PointsPerInch;
- if (delta_y >= 0.0)
- translate_y=((delta_y/2.0+PageBottomMargin)*PointsPerInch);
- else
- translate_y=PageBottomMargin*PointsPerInch;
- }
- else
- {
- delta_x=PageHeight-(image_width+(2.0*PageBottomMargin));
- delta_y=PageWidth-(image_height+(2.0*PageLeftMargin));
- if (delta_x >= 0.0)
- translate_x=((delta_x/2.0+PageBottomMargin-PageHeight)*PointsPerInch);
- else
- translate_x=(PageBottomMargin-PageHeight)*PointsPerInch;
- if (delta_y >= 0.0)
- translate_y=((delta_y/2.0+PageLeftMargin)*PointsPerInch);
- else
- translate_y=PageLeftMargin*PointsPerInch;
- }
- /*
- Output encapsulated Postscript header.
- */
- (void) fprintf(image->file,"%%!PS-Adobe-2.0 EPSF-2.0\n");
- if (rotate == 0.0)
- (void) fprintf(image->file,"%%%%BoundingBox: %d %d %d %d\n",
- (unsigned int) translate_x,(unsigned int) translate_y,
- (unsigned int) (translate_x+scale_x),
- (unsigned int) (translate_y+scale_y));
- else
- (void) fprintf(image->file,"%%%%BoundingBox: %d %d %d %d\n",
- (unsigned int) translate_y,(unsigned int) -(translate_x+scale_x),
- (unsigned int) (translate_y+scale_y),(unsigned int) -translate_x);
- (void) fprintf(image->file,"%%%%Creator: ImageMagick\n");
- (void) fprintf(image->file,"%%%%Title: %s\n",image->filename);
- (void) fprintf(image->file,"%%%%EndComments\n");
- /*
- Output encapsulated Postscript commands.
- */
- for (q=commands; *q; q++)
- (void) fprintf(image->file,"%s\n",*q);
- /*
- Output image data.
- */
- (void) fprintf(image->file,"%f\n%f %f\n%f %f\n%d %d\n%d\n",rotate,
- translate_x,translate_y,scale_x,scale_y,image->columns,image->rows,
- (image->class == PseudoClass));
- p=image->pixels;
- j=0;
- switch (image->class)
- {
- case DirectClass:
- {
- /*
- Dump DirectColor packets.
- */
- for (i=0; i < image->packets; i++)
- {
- j++;
- (void) fprintf(image->file,"%02x%02x%02x%02x",p->length,p->red,
- p->green,p->blue);
- if (j == 9)
- {
- j=0;
- (void) fprintf(image->file,"\n");
- }
- p++;
- }
- break;
- }
- case PseudoClass:
- {
- /*
- Dump number of colors, colormap, PseudoColor packets.
- */
- (void) fprintf(image->file,"%d\n",image->colors);
- for (i=0; i < image->colors; i++)
- (void) fprintf(image->file,"%02x%02x%02x\n",image->colormap[i].red,
- image->colormap[i].green,image->colormap[i].blue);
- for (i=0; i < image->packets; i++)
- {
- j++;
- (void) fprintf(image->file,"%02x%02x",p->length,p->index);
- if (j == 18)
- {
- j=0;
- (void) fprintf(image->file,"\n");
- }
- p++;
- }
- break;
- }
- }
- (void) fprintf(image->file,"\n\n");
- (void) fprintf(image->file,"%%%%Trailer\n");
- if (image->file != stdin)
- (void) fclose(image->file);
- return(True);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % R e a d C o l o r m a p %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function ReadColormap returns the red, green, and blue colormap of a window.
- % Additionally, the number of colors in the colormap is returned.
- %
- % The format of the ReadColormap function is:
- %
- % number_colors=ReadColormap(display,window_attributes,colors)
- %
- % A description of each parameter follows:
- %
- % o number_colors: ReadColormap returns the number of colors in the
- % colormap.
- %
- % o display: Specifies a pointer to the Display structure; returned from
- % XOpenDisplay.
- %
- % o window_attributes: Specifies a pointer to the window attributes
- % structure; returned from XGetWindowAttributes.
- %
- % o colors: Specifies a an array of XColor structures. The colormap
- % red, green, and blue are returned.
- %
- %
- */
- static int ReadColormap(display,window_attributes,colors)
- Display
- *display;
-
- XWindowAttributes
- *window_attributes;
-
- XColor
- **colors;
- {
- int
- number_colors;
-
- register int
- i;
-
- if (window_attributes->colormap == (Colormap) NULL)
- return(0);
- if (window_attributes->visual->class == TrueColor)
- return(0);
- number_colors=window_attributes->visual->map_entries;
- *colors=(XColor *) malloc((unsigned) (number_colors*sizeof(XColor)));
- if (*colors == (XColor *) NULL)
- Error("unable to allocate memory",(char *) NULL);
- for (i=0; i < number_colors; i++)
- if (window_attributes->visual->class != DirectColor)
- for (i=0; i < number_colors; i++)
- {
- (*colors)[i].pixel=i;
- (*colors)[i].pad=0;
- }
- else
- {
- unsigned long
- blue,
- blue_bit,
- green,
- green_bit,
- red,
- red_bit;
-
- /*
- DirectColor visual.
- */
- red=0;
- green=0;
- blue=0;
- red_bit=window_attributes->visual->red_mask &
- (~(window_attributes->visual->red_mask)+1);
- green_bit=window_attributes->visual->green_mask &
- (~(window_attributes->visual->green_mask)+1);
- blue_bit=window_attributes->visual->blue_mask &
- (~(window_attributes->visual->blue_mask)+1);
- for (i=0; i < number_colors; i++)
- {
- (*colors)[i].pixel=red | green | blue;
- (*colors)[i].pad=0;
- red+=red_bit;
- if (red > window_attributes->visual->red_mask)
- red=0;
- green+=green_bit;
- if (green > window_attributes->visual->green_mask)
- green=0;
- blue+=blue_bit;
- if (blue > window_attributes->visual->blue_mask)
- blue=0;
- }
- }
- XQueryColors(display,window_attributes->colormap,*colors,number_colors);
- return(number_colors);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % R e a d I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Procedure ReadImage reads an image from an X window.
- %
- % The format of the ReadImage routine is:
- %
- % ReadImage(display,root_window,target_window,client_window,borders,
- % image)
- %
- % A description of each parameter follows:
- %
- % o display: Specifies a pointer to the Display structure; returned from
- % XOpenDisplay.
- %
- % o root_window: Specifies the id of the root window.
- %
- % o target_window: Specifies the id of the target window.
- %
- % o client_window: Specifies the id of the client of the target window.
- %
- % o borders: Specifies whether borders pixels are to be saved with
- % the image.
- %
- % o image: specifies a pointer to the Image structure.
- %
- %
- */
- static void ReadImage(display,root_window,target_window,client_window,borders,
- image)
- Display
- *display;
-
- Window
- root_window,
- target_window,
- client_window;
-
- unsigned int
- borders;
-
- Image
- *image;
- {
- int
- display_width,
- display_height,
- x,
- y;
-
- register int
- i;
-
- register RunlengthPacket
- *p;
-
- register unsigned long
- pixel;
-
- unsigned char
- blue,
- green,
- red;
-
- Window
- child;
-
- XColor
- *colors;
-
- XImage
- *ximage;
-
- XTextProperty
- window_name;
-
- XWindowAttributes
- client_attributes,
- target_attributes;
-
- /*
- Inform the user not to alter the screen.
- */
- XBell(display,0);
- /*
- Get the attributes of the window being dumped.
- */
- if (!XGetWindowAttributes(display,target_window,&target_attributes) ||
- !XGetWindowAttributes(display,client_window,&client_attributes))
- Error("unable to get target window attributes",(char *) NULL);
- XTranslateCoordinates(display,target_window,root_window,0,0,&x,&y,&child);
- target_attributes.x=x;
- target_attributes.y=y;
- image->columns=target_attributes.width;
- image->rows=target_attributes.height;
- if (borders)
- {
- /*
- Do not include border in image.
- */
- x-=target_attributes.border_width;
- y-=target_attributes.border_width;
- image->columns+=2*target_attributes.border_width;
- image->rows+=2*target_attributes.border_width;
- }
- /*
- clip to window
- */
- if (x < 0)
- {
- image->columns+=x;
- x=0;
- }
- if (y < 0)
- {
- image->rows+=y;
- y=0;
- }
- display_width=DisplayWidth(display,XDefaultScreen(display));
- display_height=DisplayHeight(display,XDefaultScreen(display));
- if ((x+image->columns) > display_width)
- image->columns=display_width-x;
- if ((y+image->rows) > display_height)
- image->rows=display_height-y;
- /*
- Get image from window with XGetImage.
- */
- x-=target_attributes.x;
- y-=target_attributes.y;
- ximage=XGetImage(display,target_window,x,y,image->columns,image->rows,
- AllPlanes,ZPixmap);
- if (ximage == (XImage *) NULL)
- Error("unable to get image",(char *) NULL);
- /*
- Obtain the window colormap from the client of the target window.
- */
- image->colors=ReadColormap(display,&client_attributes,&colors);
- XBell(display,0);
- XBell(display,0);
- XFlush(display);
- /*
- Convert image to MIFF format.
- */
- image->comments=(char *) NULL;
- if (XGetWMName(display,target_window,&window_name))
- {
- /*
- Initial image comment.
- */
- image->comments=(char *)
- malloc((unsigned int) (strlen((char *) window_name.value)+256));
- if (image->comments == (char *) NULL)
- Error("unable to allocate memory",(char *) NULL);
- (void) sprintf(image->comments,"\n Imported from X11 window: %s\n\0",
- window_name.value);
- }
- if ((target_attributes.visual->class == TrueColor) ||
- (target_attributes.visual->class == DirectColor))
- image->class=DirectClass;
- else
- image->class=PseudoClass;
- image->compression=RunlengthEncodedCompression;
- image->scene=0;
- image->pixels=(RunlengthPacket *)
- malloc(image->columns*image->rows*sizeof(RunlengthPacket));
- if (image->pixels == (RunlengthPacket *) NULL)
- Error("unable to allocate memory",(char *) NULL);
- image->packets=0;
- p=image->pixels;
- p->length=MaxRunlength;
- switch (image->class)
- {
- case DirectClass:
- {
- register unsigned long
- color,
- index;
-
- unsigned long
- blue_mask,
- blue_shift,
- green_mask,
- green_shift,
- red_mask,
- red_shift;
-
- /*
- Determine shift and mask for red, green, and blue.
- */
- red_mask=target_attributes.visual->red_mask;
- red_shift=0;
- while ((red_mask & 0x01) == 0)
- {
- red_mask>>=1;
- red_shift++;
- }
- green_mask=target_attributes.visual->green_mask;
- green_shift=0;
- while ((green_mask & 0x01) == 0)
- {
- green_mask>>=1;
- green_shift++;
- }
- blue_mask=target_attributes.visual->blue_mask;
- blue_shift=0;
- while ((blue_mask & 0x01) == 0)
- {
- blue_mask>>=1;
- blue_shift++;
- }
- /*
- Convert X image to DirectClass packets.
- */
- if ((image->colors > 0) &&
- (target_attributes.visual->class == DirectColor))
- for (y=0; y < image->rows; y++)
- {
- for (x=0; x < image->columns; x++)
- {
- pixel=XGetPixel(ximage,x,y);
- index=(pixel >> red_shift) & red_mask;
- red=(unsigned char) (colors[index].red >> 8);
- index=(pixel >> green_shift) & green_mask;
- green=(unsigned char) (colors[index].green >> 8);
- index=(pixel >> blue_shift) & blue_mask;
- blue=(unsigned char) (colors[index].blue >> 8);
- if ((red == p->red) && (green == p->green) && (blue == p->blue) &&
- (p->length < MaxRunlength))
- p->length++;
- else
- {
- if (image->packets > 0)
- p++;
- image->packets++;
- p->red=red;
- p->green=green;
- p->blue=blue;
- p->index=0;
- p->length=0;
- }
- }
- }
- else
- for (y=0; y < image->rows; y++)
- for (x=0; x < image->columns; x++)
- {
- pixel=XGetPixel(ximage,x,y);
- color=(pixel >> red_shift) & red_mask;
- red=(unsigned char)
- ((((unsigned long) color*65535)/red_mask) >> 8);
- color=(pixel >> green_shift) & green_mask;
- green=(unsigned char)
- ((((unsigned long) color*65535)/green_mask) >> 8);
- color=(pixel >> blue_shift) & blue_mask;
- blue=(unsigned char)
- ((((unsigned long) color*65535)/blue_mask) >> 8);
- if ((red == p->red) && (green == p->green) && (blue == p->blue) &&
- (p->length < MaxRunlength))
- p->length++;
- else
- {
- if (image->packets > 0)
- p++;
- image->packets++;
- p->red=red;
- p->green=green;
- p->blue=blue;
- p->index=0;
- p->length=0;
- }
- }
- break;
- }
- case PseudoClass:
- {
- /*
- Convert X image to PseudoClass packets.
- */
- image->colormap=(ColorPacket *)
- malloc((unsigned) (image->colors*sizeof(ColorPacket)));
- if (image->colormap == (ColorPacket *) NULL)
- Error("unable to allocate memory",(char *) NULL);
- for (i=0; i < image->colors; i++)
- {
- image->colormap[i].red=colors[i].red >> 8;
- image->colormap[i].green=colors[i].green >> 8;
- image->colormap[i].blue=colors[i].blue >> 8;
- }
- for (y=0; y < image->rows; y++)
- for (x=0; x < image->columns; x++)
- {
- pixel=XGetPixel(ximage,x,y);
- red=(unsigned char) (colors[pixel].red >> 8);
- green=(unsigned char) (colors[pixel].green >> 8);
- blue=(unsigned char) (colors[pixel].blue >> 8);
- if ((red == p->red) && (green == p->green) && (blue == p->blue) &&
- (p->length < MaxRunlength))
- p->length++;
- else
- {
- if (image->packets > 0)
- p++;
- image->packets++;
- p->red=red;
- p->green=green;
- p->blue=blue;
- p->index=(unsigned short) pixel;
- p->length=0;
- }
- }
- break;
- }
- }
- /*
- Free image and colormap.
- */
- if (image->colors > 0)
- (void) free((char *) colors);
- XDestroyImage(ximage);
- if (image->packets > ((image->columns*image->rows*3) >> 2))
- image->compression=NoCompression;
- image->pixels=(RunlengthPacket *)
- realloc((char *) image->pixels,image->packets*sizeof(RunlengthPacket));
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % S e l e c t W i n d o w %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function SelectWindow allows a user to select a window using the mouse.
- %
- % The format of the SelectWindow function is:
- %
- % target_window=SelectWindow(display,root_window)
- %
- % A description of each parameter follows:
- %
- % o window: SelectWindow returns the window id.
- %
- % o display: Specifies a pointer to the Display structure; returned from
- % XOpenDisplay.
- %
- % o root_window: Specifies the window id of the root window.
- %
- %
- */
- static Window SelectWindow(display,root_window)
- Display
- *display;
-
- Window
- root_window;
- {
- Cursor
- crosshair_cursor;
-
- int
- status;
-
- unsigned int
- presses;
-
- Window
- target_window;
-
- XEvent
- event;
-
- /*
- Make the target cursor.
- */
- crosshair_cursor=XCreateFontCursor(display,XC_crosshair);
- /*
- Grab the pointer using target cursor.
- */
- status=XGrabPointer(display,root_window,False,ButtonPressMask |
- ButtonReleaseMask,GrabModeSync,GrabModeAsync,root_window,crosshair_cursor,
- CurrentTime);
- if (status != GrabSuccess)
- Error("cannot grab the mouse",(char *) NULL);
- /*
- Select a window.
- */
- target_window=(Window) NULL;
- presses=0;
- do
- {
- /*
- Allow another event.
- */
- XAllowEvents(display,SyncPointer,CurrentTime);
- XWindowEvent(display,root_window,ButtonPressMask | ButtonReleaseMask,
- &event);
- switch (event.type)
- {
- case ButtonPress:
- {
- if (target_window == (Window) NULL)
- {
- target_window=event.xbutton.subwindow;
- if (target_window == (Window) NULL)
- target_window=root_window;
- }
- presses++;
- break;
- }
- case ButtonRelease:
- {
- if (presses > 0)
- presses--;
- break;
- }
- default:
- break;
- }
- }
- while ((target_window == (Window) NULL) || (presses != 0));
- XUngrabPointer(display,CurrentTime);
- XFreeCursor(display,crosshair_cursor);
- return(target_window);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % U s a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Procedure Usage displays the program usage;
- %
- % The format of the Usage routine is:
- %
- % Usage(message)
- %
- % A description of each parameter follows:
- %
- % message: Specifies a specific message to display to the user.
- %
- */
- static void Usage(message)
- char
- *message;
- {
- char
- **p;
-
- static char
- *options[]=
- {
- "-border include image borders in the output image",
- "-display server X server to contact",
- "-frame include window manager frame",
- "-id number select window with this id",
- "-name name select window with this name",
- "-root select root window",
- (char *) NULL
- };
- if (message != (char *) NULL)
- (void) fprintf(stderr,"Can't continue, %s.\n\n",message);
- (void) fprintf(stderr,"Usage: %s [options ...] file\n",application_name);
- (void) fprintf(stderr,"\nWhere options include:\n");
- for (p=options; *p != (char *) NULL; p++)
- (void) fprintf(stderr," %s\n",*p);
- (void) fprintf(stderr,
- "\nChange '-' to '+' in any option above to reverse its effect.\n");
- (void) fprintf(stderr,
- "For example, +frame means do not include window manager frame.\n");
- (void) fprintf(stderr,"\nSpecify 'file' as '-' for standard output.\n");
- exit(1);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % W i n d o w B y N a m e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function WindowByName locates a window with a given name on a display.
- % If no window with the given name is found, 0 is returned. If more than
- % one window has the given name, the first one is returned. Only root and
- % its children are searched.
- %
- % The format of the WindowByName function is:
- %
- % window=WindowByName(display,root_window,name)
- %
- % A description of each parameter follows:
- %
- % o window: WindowByName returns the window id.
- %
- % o display: Specifies a pointer to the Display structure; returned from
- % XOpenDisplay.
- %
- % o root_window: Specifies the id of the root window.
- %
- % o name: Specifies the name of the window to locate.
- %
- %
- */
- static Window WindowByName(display,root_window,name)
- Display
- *display;
-
- Window
- root_window;
-
- char
- *name;
- {
- register int
- i;
-
- unsigned int
- number_children;
-
- Window
- *children,
- child,
- window;
-
- XTextProperty
- window_name;
-
- if (XGetWMName(display,root_window,&window_name))
- if (strcmp((char *) window_name.value,name) == 0)
- return(root_window);
- if (!XQueryTree(display,root_window,&child,&child,&children,&number_children))
- return(0);
- window=0;
- for (i=0; i < number_children; i++)
- {
- /*
- Search each child and their children.
- */
- window=WindowByName(display,children[i],name);
- if (window != (Window) NULL)
- break;
- }
- if (children != (Window *) NULL)
- XFree((char *) children);
- return(window);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % W i n d o w B y P r o p e r y %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function WindowByProperty locates a child window with a given property.
- % If no window with the given name is found, 0 is returned. If more than
- % one window has the given property, the first one is returned. Only the
- % window specified and its subwindows are searched.
- %
- % The format of the WindowByProperty function is:
- %
- % child=WindowByProperty(display,window,property)
- %
- % A description of each parameter follows:
- %
- % o child: WindowByProperty returns the window id with the specified
- % property. If no windows are found, WindowByProperty returns 0.
- %
- % o display: Specifies a pointer to the Display structure; returned from
- % XOpenDisplay.
- %
- % o property: Specifies the property of the window to locate.
- %
- %
- */
- static Window WindowByProperty(display,window,property)
- Display
- *display;
-
- Window
- window;
-
- Atom
- property;
- {
- Atom
- type;
-
- int
- format;
-
- unsigned char
- *data;
-
- unsigned int
- i,
- number_children;
-
- unsigned long
- after,
- number_items;
-
- Window
- *children,
- child,
- parent,
- root;
-
- if (!XQueryTree(display,window,&root,&parent,&children,&number_children))
- return((Window) NULL);
- type=(Atom) NULL;
- child=(Window) NULL;
- for (i=0; (i < number_children) && (child == (Window) NULL); i++)
- {
- (void) XGetWindowProperty(display,children[i],property,0L,0L,False,
- AnyPropertyType,&type,&format,&number_items,&after,&data);
- if (type != (Atom) NULL)
- child=children[i];
- }
- for (i = 0; (i < number_children) && (child == (Window) NULL); i++)
- child=WindowByProperty(display,children[i],property);
- if (children != (Window *) NULL)
- XFree((char *) children);
- return(child);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % M a i n %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %
- */
- int main(argc,argv)
- int
- argc;
-
- char
- *argv[];
- {
- char
- *filename,
- *option,
- *server_name;
-
- Display
- *display;
-
- extern unsigned int
- WriteImage();
-
- Image
- image;
-
- int
- i;
-
- unsigned int
- borders,
- frame,
- j;
-
- Window
- client_window,
- root_window,
- target_window;
-
- /*
- Display usage profile if there are no command line arguments.
- */
- application_name=(*argv);
- if (argc < 2)
- Usage((char *) NULL);
- /*
- Connect to X server.
- */
- server_name=(char *) NULL;
- for (i=1; i < argc; i++)
- {
- /*
- Check command line for server name.
- */
- option=argv[i];
- if ((strlen(option) > 1) && ((*option == '-') || (*option == '+')))
- if (strncmp("dis",option+1,3) == 0)
- {
- /*
- User specified server name.
- */
- i++;
- if (i == argc)
- Usage("missing server name on -display");
- server_name=argv[i];
- break;
- }
- }
- display=XOpenDisplay(server_name);
- if (display == (Display *) NULL)
- Error("unable to connect to",XDisplayName(server_name));
- root_window=XRootWindow(display,XDefaultScreen(display));
- /*
- Get X defaults.
- */
- option=XGetDefault(display,application_name,"borders");
- borders=IsTrue(option);
- option=XGetDefault(display,application_name,"frame");
- frame=IsTrue(option);
- /*
- Check command syntax.
- */
- filename=(char *) NULL;
- target_window=(Window) NULL;
- for (i=1; i < argc; i++)
- {
- option=argv[i];
- if ((strlen(option) < 2) || ((*option != '-') && (*option != '+')))
- filename=argv[i];
- else
- switch(*(option+1))
- {
- case 'b':
- {
- borders=(*option == '-');
- break;
- }
- case 'h':
- {
- Usage((char *) NULL);
- break;
- }
- case 'f':
- {
- frame=(*option == '-');
- break;
- }
- case 'i':
- {
- i++;
- if (i == argc)
- Usage("missing id on -id");
- option=argv[i];
- target_window=(Window) strtol(option,(char **) NULL,0);
- break;
- }
- case 'n':
- {
- i++;
- if (i == argc)
- Usage("missing name on -name");
- option=argv[i];
- target_window=WindowByName(display,root_window,option);
- if (target_window == (Window) NULL)
- (void) fprintf(stderr,"No window with name %s exists!\n",option);
- break;
- }
- case 'r':
- {
- target_window=root_window;
- break;
- }
- default:
- Usage((char *) NULL);
- }
- }
- if (filename == (char *) NULL)
- Usage("missing an image file name");
- /*
- If the window is not specified, let the user choose one with the mouse.
- */
- if (target_window == (Window) NULL)
- target_window=SelectWindow(display,root_window);
- client_window=target_window;
- if (target_window != root_window)
- if (XGetGeometry(display,target_window,&root_window,&i,&i,&j,&j,&j,&j))
- {
- /*
- Get client window.
- */
- client_window=ClientWindow(display,target_window);
- if (!frame)
- target_window=client_window;
- }
- (void) strcpy(image.filename,filename);
- ReadImage(display,root_window,target_window,client_window,borders,&image);
- (void) fprintf(stderr,"%s %dx%d\n",image.filename,image.columns,image.rows);
- (void) PrintImage(&image);
- return(False);
- }
-