home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d07xx / d0797.lha / PSUtils / epsffit.c < prev    next >
C/C++ Source or Header  |  1993-01-10  |  4KB  |  131 lines

  1. /* epsffit.c
  2.  * AJCD 6 Dec 90
  3.  * fit epsf file into constrained size
  4.  * Usage:
  5.  *       epsffit [-c] [-r] [-a] [-s] llx lly urx ury
  6.  *               -c centres the image in the bounding box given
  7.  *               -r rotates the image by 90 degrees anti-clockwise
  8.  *               -a alters the aspect ratio to fit the bounding box
  9.  *               -s adds a showpage at the end of the image
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <ctype.h>
  14.  
  15. #define min(x,y) ((x) > (y) ? (y) : (x))
  16. #define max(x,y) ((x) > (y) ? (x) : (y))
  17.  
  18. static char *prog;
  19.  
  20. usage()
  21. {
  22.    fprintf(stderr, "Usage: %s [-c] [-r] [-a] [-s] llx lly urx ury\n", prog);
  23.    exit(1);
  24. }
  25.  
  26. main(argc, argv)
  27.      int argc;
  28.      char **argv;
  29. {
  30.    int fit[4], i;
  31.    int bbfound = 0;              /* %%BoundingBox: found */
  32.    int urx, ury, llx, lly;
  33.    int furx, fury, fllx, flly, fwidth, fheight;
  34.    int showpage = 0, centre = 0, rotate = 0, aspect = 0;
  35.    char buf[BUFSIZ];
  36.  
  37.    prog = *argv++; argc--;
  38.  
  39.    while (argc > 0 && argv[0][0] == '-') {
  40.       switch (argv[0][1]) {
  41.       case 'c': centre = 1; break;
  42.       case 's': showpage = 1; break;
  43.       case 'r': rotate = 1; break;
  44.       case 'a': aspect = 1; break;
  45.       default:  usage();
  46.       }
  47.       argc--;
  48.       argv++;
  49.    }
  50.  
  51.    if (argc != 4) usage();
  52.    fllx = atoi(argv[0]);
  53.    flly = atoi(argv[1]);
  54.    furx = atoi(argv[2]);
  55.    fury = atoi(argv[3]);
  56.    if (rotate) {
  57.       fwidth = fury - flly;
  58.       fheight = furx - fllx;
  59.    } else {
  60.       fwidth = furx - fllx;
  61.       fheight = fury - flly;
  62.    }
  63.  
  64.    while (fgets(buf, BUFSIZ, stdin)) {
  65.       if (buf[0] == '%' && (buf[1] == '%' || buf[1] == '!')) {
  66.      /* still in comment section */
  67.      if (!strncmp(buf, "%%BoundingBox:", 14)) {
  68.         if (sscanf(buf, "%%%%BoundingBox:%d %d %d %d\n",
  69.                &llx, &lly, &urx, &ury) == 4)
  70.            bbfound = 1;
  71.      } else if (!strncmp(buf, "%%EndComments", 13)) {
  72.         strcpy(buf, "\n"); /* don't repeat %%EndComments */
  73.         break;
  74.      } else fputs(buf,stdout);
  75.       } else break;
  76.    }
  77.    if (bbfound) { /* put BB, followed by scale&translate */
  78.       double width = urx-llx, height = ury-lly;
  79.       double xscale = fwidth/width, yscale = fheight/height;
  80.       double xoffset = fllx, yoffset = flly;
  81.       if (!aspect) {       /* preserve aspect ratio ? */
  82.      xscale = yscale = min(xscale,yscale);
  83.       }
  84.       width *= xscale;     /* actual width and height after scaling */
  85.       height *= yscale;
  86.       if (centre) {
  87.      if (rotate) {
  88.         xoffset += (fheight - height)/2;
  89.         yoffset += (fwidth - width)/2;
  90.      } else {
  91.         xoffset += (fwidth - width)/2;
  92.         yoffset += (fheight - height)/2;
  93.      }
  94.       }
  95.       printf("%%%%BoundingBox: %d %d %d %d\n", (int)xoffset, (int)yoffset,
  96.          (int)(xoffset+(rotate ? height : width)),
  97.          (int)(yoffset+(rotate ? width : height)));
  98.       if (rotate) {  /* compensate for original image shift */
  99.      xoffset += height + lly * yscale;  /* displacement for rotation */
  100.      yoffset -= llx * xscale;
  101.       } else {
  102.      xoffset -= llx * xscale;
  103.      yoffset -= lly * yscale;
  104.       }
  105.       puts("%%EndComments");
  106.       if (showpage)
  107.      puts("save /showpage{}def /copypage{}def /erasepage{}def");
  108.       else
  109.      puts("%%BeginProcSet: epsffit 1 0");
  110.       puts("gsave");
  111.       printf("%.3lf %.3lf translate\n", xoffset, yoffset);
  112.       if (rotate)
  113.      puts("90 rotate");
  114.       printf("%.3lf %.3lf scale\n", xscale, yscale);
  115.       if (!showpage)
  116.      puts("%%EndProcSet");
  117.    }
  118.    do {
  119.       fputs(buf,stdout);
  120.    } while (fgets(buf, BUFSIZ, stdin));
  121.    if (bbfound) {
  122.       puts("grestore");
  123.       if (showpage)
  124.      puts("restore showpage"); /* just in case */
  125.    } else {
  126.       fprintf(stderr, "%s: no %%%%BoundingBox:\n", prog);
  127.       exit(1);
  128.    }
  129.    exit(0);
  130. }
  131.