home *** CD-ROM | disk | FTP | other *** search
/ rtfm.mit.edu / 2014.07.rtfm.mit.edu.tar / rtfm.mit.edu / pub / x2ps.c < prev    next >
C/C++ Source or Header  |  1987-01-28  |  5KB  |  194 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. /* The following include file can be found in the xwd source directory */
  4. #include "XWDFile.h"
  5.  
  6. /* This program converts X window dump files created by xwd into */
  7.  /* postscript code to image the window on paper.  Tony Della Fera had */
  8.  /* a program to do this in an early X release, but I lost track of */
  9.  /* that program and attempted to recreate it using an old output file */
  10.  /* from it.  Apologies to Tony... */
  11.  /*  --- plj@charon.mit.edu */
  12.  
  13. XWDFileHeader infile;
  14.  
  15. char *malloc();
  16.  
  17. unsigned char revbits[] = {
  18. 0x0,    0x8,    0x4,    0xc,    0x2,    0xa,    0x6,    0xe,
  19. 0x1,    0x9,    0x5,    0xd,    0x3,    0xb,    0x7,    0xf};
  20.  
  21.  
  22. char preheader[] = "%!%% PostScript Prelude.\n\
  23. %%%% X Window System xps raster format.\n\
  24. \n\
  25. %%%% Type definitions.\n\
  26. /inch {72 mul} def\n\
  27. /inches {72 mul} def\n\
  28. \n\
  29. %%%% Xps format header definitions.\n\
  30. ";
  31.  
  32. char postheader[] = "\n\
  33. %%%% Compute bits per pixel\n\
  34. /bits_per_pixel display_panes def\n\
  35. \n\
  36. %%%% X dump imaging proceedure.\n\
  37. /image_string string_size string def\n\
  38. /image_xps {\n\
  39.     pixmap_width pixmap_height bits_per_pixel\n\
  40.     [\n\
  41.         pixmap_width 0 0\n\
  42.         pixmap_height neg 0 pixmap_height\n\
  43.     ]\n\
  44.     {currentfile image_string readhexstring pop}\n\
  45.     image\n\
  46. } def\n\
  47. \n\
  48. %%%% Stroke image.\n\
  49. ";
  50.  
  51. char *whoami;
  52.  
  53. usage()
  54. {
  55.   fprintf(stderr,"Usage: %s -x{XOFF} -y{YOFF} -w{WIDTH} -h{HEIGHT} [-r] [-n]\n\
  56. The -r option rotates the image 90 degrees\n\
  57.  (Default orientation is portrait.)\n\
  58. The -n option disables the 'showpage' at the end.\n\
  59.  (use this option to insert output into other postscript files)\n\
  60. All values in inches, stdin is xwd file, stdout is postscript file\n\
  61. Offsets are from the lower left corner of the page.\n",
  62.       whoami);
  63.   exit(1);
  64. }
  65.  
  66. main(argc, argv)
  67.      int argc;
  68.      char **argv;
  69. {
  70.   char *winname;
  71.   int winnamesz;
  72.   float rlstrsize;
  73.   int strsize;
  74.   int numbytes;
  75.   int argctr, rotate;
  76.   char *curarg, *valptr;
  77.   float xoff, yoff, rlwidth, rlheight;
  78.   int printit;
  79.  
  80.   printit = 1;
  81.   rotate = 0;
  82.   xoff = yoff = rlwidth = rlheight = 0;
  83.   whoami = argv[0];
  84.   if (argc < 5) usage();
  85.   for (argctr = 1; argctr < argc; argctr++) {
  86.     curarg = argv[argctr];
  87.     valptr = &(curarg[2]);
  88.     switch (curarg[1]) {
  89.     case 'n':
  90.       printit = 0;
  91.       break;
  92.     case 'r':
  93.       rotate = 1;
  94.       break;
  95.     case 'x':
  96.       xoff = atof(valptr);
  97.       break;
  98.     case 'y':
  99.       yoff = atof(valptr);
  100.       break;
  101.     case 'w':
  102.       rlwidth = atof(valptr);
  103.       break;
  104.     case 'h':
  105.       rlheight = atof(valptr);
  106.       break;
  107.     default:
  108.       fprintf(stderr, "Unrecognized option: %c\n", curarg[1]);
  109.       exit(1);
  110.     }
  111.   }
  112.   if (!xoff || !yoff || !rlwidth || !rlheight) usage();
  113.  
  114.  
  115.  
  116.  
  117.   printf("%s",preheader);
  118.   read(fileno(stdin), &infile, sizeof(XWDFileHeader));
  119.  
  120.   numbytes = infile.pixmap_width/8;
  121.   if (infile.pixmap_width & 0x7) numbytes++;
  122.  
  123.   rlstrsize = ((float)infile.pixmap_width)/(16.0);
  124.   strsize = (int)(floor((double)rlstrsize));
  125.   if (rlstrsize - (float)strsize) strsize++;
  126.   strsize <<= 1;
  127.   printf("/string_size %d def \n", numbytes);
  128.  
  129.   winnamesz = infile.header_size - sizeof(XWDFileHeader);
  130.   winname = malloc(winnamesz);
  131.   read(fileno(stdin), winname, winnamesz);
  132.   printf("/header_size %d def\n", infile.header_size);
  133.   printf("/display_type %d def\n", infile.display_type);
  134.   printf("/display_panes %d def\n", infile.display_planes);
  135.   printf("/pixmap_format %d def\n", infile.pixmap_format);
  136.   printf("/pixmap_width %d def\n", infile.pixmap_width);
  137.   printf("/pixmap_height %d def\n", infile.pixmap_height);
  138.   printf("/window_width %d def\n", infile.window_width);
  139.   printf("/window_height %d def\n", infile.window_height);
  140.   printf("/window_x %d def\n", infile.window_x);
  141.   printf("/window_y %d def\n", infile.window_y);
  142.   printf("/window_border %d def\n", infile.window_bdrwidth);
  143.   printf("/window_name (%s) def\n", winname);
  144.   printf("%s", postheader);
  145.   printf("gsave\n");
  146.   printf("matrix defaultmatrix setmatrix\n");
  147.   /* go back to default matrix */
  148.   
  149.   if (rotate) printf("\t90 rotate 0 -8.5 inches translate\n");
  150.  
  151.   printf("\t%f inches %f inches translate\n\
  152. \t%f inches %f inches scale\n\
  153. \timage_xps\n", xoff, yoff, rlwidth, rlheight);
  154.   dump_bitmap(numbytes, infile.pixmap_height, strsize);
  155.   printf("grestore\n");
  156.   if (printit) printf("showpage\n");
  157. }
  158.  
  159.   
  160. dump_bitmap(numbytes, numlines, strsize)
  161.      int numbytes, numlines, strsize;
  162. {
  163.   int num_on_line = 0;
  164.   int i, j, picsize;
  165.   unsigned char area;
  166.   char *thebits, *theswappedbits;
  167.  
  168.   picsize = numlines * strsize;
  169.   thebits = malloc(picsize);
  170.   theswappedbits = malloc(picsize);
  171.   if (read(fileno(stdin), thebits, picsize) < picsize) {
  172.     fprintf(stderr, "Couldn't read picture\n");
  173.     exit(1);
  174.   }
  175.   swab(thebits, theswappedbits, picsize);
  176.   for (j = 0; j < numlines; j++) {
  177.     printf("    ");
  178.     for (i = 0; i < numbytes; i++) {
  179.       register unsigned char thechar;
  180.       thechar = *(theswappedbits + i + (j*strsize));
  181.       thechar = (revbits[(thechar & 0xf)] << 4) |
  182.     (revbits[(thechar >> 4)]);
  183.       printf("%02x", thechar);
  184.       if ((++num_on_line) == 40) {
  185.     num_on_line = 0;
  186.     printf("\n    ");
  187.       }
  188.     }
  189.     num_on_line = 0;
  190.     printf("\n");
  191.   }
  192.   
  193. }
  194.