home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume34 / imagemagick / part01 / ImageMagick / XtoPS.c < prev    next >
C/C++ Source or Header  |  1992-12-14  |  17KB  |  486 lines

  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %                                                                             %
  4. %                                                                             %
  5. %                     X   X  TTTTT   OOO   PPPP   SSSSS                       %
  6. %                      X X     T    O   O  P   P  S                           %
  7. %                       X      T    O   O  PPPP    SSS                        %
  8. %                      X X     T    O   O  P          S                       %
  9. %                     X   X    T     OOO   P      SSSSS                       %
  10. %                                                                             %
  11. %                                                                             %
  12. %                  Import X11 image to a Postscript format.                   %
  13. %                                                                             %
  14. %                                                                             %
  15. %                           Software Design                                   %
  16. %                             John Cristy                                     %
  17. %                              July 1992                                      %
  18. %                                                                             %
  19. %                                                                             %
  20. %  Copyright 1992 E. I. du Pont de Nemours & Company                          %
  21. %                                                                             %
  22. %  Permission to use, copy, modify, distribute, and sell this software and    %
  23. %  its documentation for any purpose is hereby granted without fee,           %
  24. %  provided that the above Copyright notice appear in all copies and that     %
  25. %  both that Copyright notice and this permission notice appear in            %
  26. %  supporting documentation, and that the name of E. I. du Pont de Nemours    %
  27. %  & Company not be used in advertising or publicity pertaining to            %
  28. %  distribution of the software without specific, written prior               %
  29. %  permission.  E. I. du Pont de Nemours & Company makes no representations   %
  30. %  about the suitability of this software for any purpose.  It is provided    %
  31. %  "as is" without express or implied warranty.                               %
  32. %                                                                             %
  33. %  E. I. du Pont de Nemours & Company disclaims all warranties with regard    %
  34. %  to this software, including all implied warranties of merchantability      %
  35. %  and fitness, in no event shall E. I. du Pont de Nemours & Company be       %
  36. %  liable for any special, indirect or consequential damages or any           %
  37. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  38. %  in an action of contract, negligence or other tortious action, arising     %
  39. %  out of or in connection with the use or performance of this software.      %
  40. %                                                                             %
  41. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42. %
  43. %  XtoPS is an X Window System window dumping utility.  XtoPS reads an
  44. %  image from any visible window on an X server and outputs it to an
  45. %  encapsulated Postscript file.  You can view this file with any
  46. %  Postscript compatible viewer or printer.  The image is displayed in
  47. %  color on viewers or printers that support color Postscript, otherwise
  48. %  it is displayed as grayscale.  The target window can be specified by id
  49. %  or name or be selected by clicking the mouse in the desired window.
  50. %  The keyboard bell is rung once at the beginning of the dump and twice
  51. %  when the dump is completed.
  52. %
  53. %  The XtoPS program command syntax is:
  54. %
  55. %  Usage: XtoPS [options ...] file
  56. %
  57. %  Where options include:
  58. %    -border             include image borders in the output image
  59. %    -delay seconds      pause before selecting target window
  60. %    -display server     X server to contact
  61. %    -frame              include window manager frame
  62. %    -geometry geometry  preferred size and location of the image
  63. %    -monochrome         transform image to black and white
  64. %    -rotate degrees     apply Paeth rotation to the image
  65. %    -scale geometry     preferred size factors of the image
  66. %    -screen             select image from root window
  67. %    -verbose            print detailed information about the image
  68. %    -window id          select window with this id or name
  69. %
  70. %  Change '-' to '+' in any option above to reverse its effect.
  71. %  For example, +frame means do not window manager frame.
  72. %
  73. %  Specify 'file' as '-' for standard input or output.
  74. %
  75. %
  76. */
  77.  
  78. /*
  79.   Include declarations.
  80. */
  81. #include "display.h"
  82. #include "image.h"
  83. #include "alien.h"
  84. #include "X.h"
  85.  
  86. /*
  87.   Global declarations.
  88. */
  89. char
  90.   *application_name;
  91.  
  92. /*
  93. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  94. %                                                                             %
  95. %                                                                             %
  96. %                                                                             %
  97. %   E r r o r                                                                 %
  98. %                                                                             %
  99. %                                                                             %
  100. %                                                                             %
  101. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  102. %
  103. %  Function Error displays an error message and then terminates the program.
  104. %
  105. %  The format of the Error routine is:
  106. %
  107. %      Error(message,qualifier)
  108. %
  109. %  A description of each parameter follows:
  110. %
  111. %    o message: Specifies the message to display before terminating the
  112. %      program.
  113. %
  114. %    o qualifier: Specifies any qualifier to the message.
  115. %
  116. %
  117. */
  118. void Error(message,qualifier)
  119. char
  120.   *message,
  121.   *qualifier;
  122. {
  123.   (void) fprintf(stderr,"%s: %s",application_name,message);
  124.   if (qualifier != (char *) NULL)
  125.     (void) fprintf(stderr," (%s)",qualifier);
  126.   (void) fprintf(stderr,".\n");
  127.   exit(1);
  128. }
  129.  
  130. /*
  131. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  132. %                                                                             %
  133. %                                                                             %
  134. %                                                                             %
  135. %   U s a g e                                                                 %
  136. %                                                                             %
  137. %                                                                             %
  138. %                                                                             %
  139. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  140. %
  141. %  Procedure Usage displays the program usage;
  142. %
  143. %  The format of the Usage routine is:
  144. %
  145. %      Usage()
  146. %
  147. %
  148. */
  149. static void Usage()
  150. {
  151.   char
  152.     **p;
  153.  
  154.   static char
  155.     *options[]=
  156.     {
  157.       "-border             include image borders in the output image",
  158.       "-delay seconds      pause before selecting target window",
  159.       "-display server     X server to contact",
  160.       "-frame              include window manager frame",
  161.       "-geometry geometry  preferred size and location of the image",
  162.       "-monochrome         transform image to black and white",
  163.       "-rotate degrees     apply Paeth rotation to the image",
  164.       "-scale geometry     preferred size factors of the image",
  165.       "-screen             select image from root window",
  166.       "-verbose            print detailed information about the image",
  167.       "-window id          select window with this id or name",
  168.       (char *) NULL
  169.     };
  170.   (void) fprintf(stderr,"Usage: %s [options ...] file\n",application_name);
  171.   (void) fprintf(stderr,"\nWhere options include:\n");
  172.   for (p=options; *p != (char *) NULL; p++)
  173.     (void) fprintf(stderr,"  %s\n",*p);
  174.   (void) fprintf(stderr,
  175.     "\nChange '-' to '+' in any option above to reverse its effect.\n");
  176.   (void) fprintf(stderr,
  177.     "For example, +frame means do not include window manager frame.\n");
  178.   (void) fprintf(stderr,"\nSpecify 'file' as '-' for standard output.\n");
  179.   exit(1);
  180. }
  181.  
  182. /*
  183. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  184. %                                                                             %
  185. %                                                                             %
  186. %                                                                             %
  187. %    M a i n                                                                  %
  188. %                                                                             %
  189. %                                                                             %
  190. %                                                                             %
  191. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  192. %
  193. %
  194. */
  195. int main(argc,argv)
  196. int
  197.   argc;
  198.  
  199. char
  200.   *argv[];
  201. {
  202.   char
  203.     *filename,
  204.     *image_geometry,
  205.     *option,
  206.     *resource_value,
  207.     *scale_geometry,
  208.     *server_name,
  209.     *target_window;
  210.  
  211.   Display
  212.     *display;
  213.  
  214.   Image
  215.     *image;
  216.  
  217.   int
  218.     degrees,
  219.     i,
  220.     x;
  221.  
  222.   time_t
  223.     start_time;
  224.  
  225.   unsigned int
  226.     borders,
  227.     frame,
  228.     screen,
  229.     verbose;
  230.  
  231.   XResourceInfo
  232.     resource_info;
  233.  
  234.   XrmDatabase
  235.     resource_database,
  236.     server_database;
  237.  
  238.   /*
  239.     Display usage profile if there are no command line arguments.
  240.   */
  241.   application_name=(*argv);
  242.   if (argc < 2)
  243.     Usage();
  244.   /*
  245.     Connect to X server.
  246.   */
  247.   server_name=(char *) NULL;
  248.   for (i=1; i < argc; i++)
  249.   {
  250.     /*
  251.       Check command line for server name.
  252.     */
  253.     option=argv[i];
  254.     if (((int) strlen(option) > 1) && ((*option == '-') || (*option == '+')))
  255.       if (strncmp("dis",option+1,3) == 0)
  256.         {
  257.           /*
  258.             User specified server name.
  259.           */
  260.           i++;
  261.           if (i == argc)
  262.             Error("missing server name on -display",(char *) NULL);
  263.           server_name=argv[i];
  264.           break;
  265.         }
  266.   }
  267.   display=XOpenDisplay(server_name);
  268.   if (display == (Display *) NULL)
  269.     Error("unable to connect to X server",XDisplayName(server_name));
  270.   /*
  271.     Set our forgiving error handler.
  272.   */
  273.   XSetErrorHandler(XError);
  274.   /*
  275.     Initialize resource database.
  276.   */
  277.   XrmInitialize();
  278.   resource_database=XrmGetDatabase(display);
  279.   resource_value=XResourceManagerString(display);
  280.   if (resource_value == (char *) NULL)
  281.     resource_value="";
  282.   server_database=XrmGetStringDatabase(resource_value);
  283.   XrmMergeDatabases(server_database,&resource_database);
  284.   /*
  285.     Get user defaults from X resource database.
  286.   */
  287.   XGetResourceInfo(resource_database,application_name,&resource_info);
  288.   resource_value=XGetResource(resource_database,application_name,"borders",
  289.     (char *) NULL,"False");
  290.   borders=IsTrue(resource_value);
  291.   resource_value=XGetResource(resource_database,application_name,"frame",
  292.     (char *) NULL,"False");
  293.   frame=IsTrue(resource_value);
  294.   image_geometry=XGetResource(resource_database,application_name,
  295.     "imageGeometry","ImageGeometry",(char *) NULL);
  296.   resource_value=XGetResource(resource_database,application_name,"rotate",
  297.     (char *) NULL,"False");
  298.   scale_geometry=XGetResource(resource_database,application_name,
  299.     "scaleGeometry","ScaleGeometry",(char *) NULL);
  300.   degrees=atoi(resource_value);
  301.   resource_value=XGetResource(resource_database,application_name,"screen",
  302.     (char *) NULL,"False");
  303.   screen=IsTrue(resource_value);
  304.   resource_value=XGetResource(resource_database,application_name,"verbose",
  305.     (char *) NULL,"False");
  306.   verbose=IsTrue(resource_value);
  307.   /*
  308.     Check command syntax.
  309.   */
  310.   filename=(char *) NULL;
  311.   target_window=(char *) NULL;
  312.   for (i=1; i < argc; i++)
  313.   {
  314.     option=argv[i];
  315.     if (((int) strlen(option) < 2) || ((*option != '-') && (*option != '+')))
  316.       filename=argv[i];
  317.     else
  318.       switch(*(option+1))
  319.       {
  320.         case 'b':
  321.         {
  322.           borders=(*option == '-');
  323.           break;
  324.         }
  325.         case 'd':
  326.         {
  327.           if (strncmp("delay",option+1,2) == 0)
  328.             {
  329.               resource_info.delay=0;
  330.               if (*option == '-')
  331.                 {
  332.                   i++;
  333.                   if ((i == argc) || !sscanf(argv[i],"%d",&x))
  334.                     Error("missing seconds on -delay",(char *) NULL);
  335.                   resource_info.delay=atoi(argv[i]);
  336.                 }
  337.               break;
  338.             }
  339.           if (strncmp("display",option+1,3) == 0)
  340.             {
  341.               server_name=(char *) NULL;
  342.               if (*option == '-')
  343.                 {
  344.                   i++;
  345.                   if (i == argc)
  346.                     Error("missing server name on -display",(char *) NULL);
  347.                   server_name=argv[i];
  348.                 }
  349.               break;
  350.             }
  351.           Error("unrecognized option",option);
  352.           break;
  353.         }
  354.         case 'g':
  355.         {
  356.           /*
  357.             User specified server name.
  358.           */
  359.           i++;
  360.           if (i == argc)
  361.             Error("missing geometry on -geometry",(char *) NULL);
  362.           image_geometry=argv[i];
  363.           break;
  364.         }
  365.         case 'h':
  366.         {
  367.           Usage();
  368.           break;
  369.         }
  370.         case 'f':
  371.         {
  372.           frame=(*option == '-');
  373.           break;
  374.         }
  375.         case 'm':
  376.         {
  377.           resource_info.monochrome=(*option == '-');
  378.           break;
  379.         }
  380.         case 'r':
  381.         {
  382.           degrees=0;
  383.           if (*option == '-')
  384.             {
  385.               i++;
  386.               if ((i == argc) || !sscanf(argv[i],"%d",&x))
  387.                 Error("missing degrees on -rotate",(char *) NULL);
  388.               degrees=atoi(argv[i]);
  389.             }
  390.           break;
  391.         }
  392.         case 's':
  393.         {
  394.           if (strncmp("scale",option+1,4) == 0)
  395.             {
  396.               scale_geometry=(char *) NULL;
  397.               if (*option == '-')
  398.                 {
  399.                   i++;
  400.                   if ((i == argc) || !sscanf(argv[i],"%f",(float *) &x))
  401.                     Error("missing scale geometry on -scale",(char *) NULL);
  402.                   scale_geometry=argv[i];
  403.                 }
  404.               break;
  405.             }
  406.           if (strncmp("screen",option+1,4) == 0)
  407.             {
  408.               screen=(*option == '-');
  409.               break;
  410.             }
  411.           Error("unrecognized option",option);
  412.           break;
  413.         }
  414.         case 'v':
  415.         {
  416.           verbose=(*option == '-');
  417.           break;
  418.         }
  419.         case 'w':
  420.         {
  421.           i++;
  422.           if (i == argc)
  423.             Error("missing id, name, or 'root' on -window",(char *) NULL);
  424.           target_window=argv[i];
  425.           break;
  426.         }
  427.         default:
  428.         {
  429.           Error("unrecognized option",option);
  430.           break;
  431.         }
  432.       }
  433.   }
  434.   if (filename == (char *) NULL)
  435.     Error("missing an image file name",(char *) NULL);
  436.   /*
  437.     Read image from X server.
  438.   */
  439.   if (resource_info.delay > 0)
  440.     (void) sleep(resource_info.delay);
  441.   start_time=time((time_t *) 0);
  442.   image=ReadXImage(target_window,server_name,frame,screen,borders);
  443.   if (image == (Image *) NULL)
  444.     exit(1);
  445.   if (image_geometry || scale_geometry)
  446.     TransformImage(&image,(char *) NULL,image_geometry,scale_geometry);
  447.   if ((degrees % 360) != 0)
  448.     {
  449.       Image
  450.         *rotated_image;
  451.  
  452.       /*
  453.         Rotate image.
  454.       */
  455.       rotated_image=RotateImage(image,(double) degrees,False);
  456.       if (rotated_image != (Image *) NULL)
  457.         {
  458.           DestroyImage(image);
  459.           image=rotated_image;
  460.         }
  461.     }
  462.   if (resource_info.monochrome)
  463.     QuantizeImage(image,2,8,False,GRAYColorspace,True);
  464.   (void) strcpy(image->filename,filename);
  465.   (void) PrintImage(image,image_geometry);
  466.   if (verbose)
  467.     {
  468.       /*
  469.         Display detailed info about the image.
  470.       */
  471.       if (image->class == DirectClass)
  472.         image->colors=NumberColors(image);
  473.       (void) fprintf(stderr,"[%u] %s %ux%u",image->scene,image->filename,
  474.         image->columns,image->rows);
  475.       if (image->class == DirectClass)
  476.         (void) fprintf(stderr," DirectClass ");
  477.       else
  478.         (void) fprintf(stderr," PseudoClass ");
  479.       (void) fprintf(stderr,"%uc %s %ds\n",image->colors,image->magick,
  480.         time((time_t *) 0)-start_time+1);
  481.     }
  482.   DestroyImage(image);
  483.   XCloseDisplay(display);
  484.   return(False);
  485. }
  486.