home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / PSUTILS / EPSFFIT.C < prev    next >
C/C++ Source or Header  |  1993-11-29  |  5KB  |  174 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 [infile [outfile]]
  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.  * Added filename spec (from Larry Weissman) 5 Feb 93
  12.  * Accepts double %%BoundingBox input, outputs proper BB, 4 Jun 93. (I don't
  13.  * like this; developers should read the Big Red Book before writing code which
  14.  * outputs PostScript.
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include "patchlev.h"
  20.  
  21. #define min(x,y) ((x) > (y) ? (y) : (x))
  22. #define max(x,y) ((x) > (y) ? (x) : (y))
  23.  
  24. static char *prog;
  25.  
  26. void usage()
  27. {
  28.    fprintf(stderr, "%s release %d patchlevel %d\n", prog, RELEASE, PATCHLEVEL);
  29.    fprintf(stderr, "Usage: %s [-c] [-r] [-a] [-s] llx lly urx ury [infile [outfile]]\n",
  30.        prog);
  31.    exit(1);
  32. }
  33.  
  34. main(argc, argv)
  35.      int argc;
  36.      char **argv;
  37. {
  38.    int bbfound = 0;              /* %%BoundingBox: found */
  39.    int urx, ury, llx, lly;
  40.    int furx, fury, fllx, flly;
  41.    int showpage = 0, centre = 0, rotate = 0, aspect = 0, maximise = 0;
  42.    char buf[BUFSIZ];
  43.    FILE *input = stdin;
  44.    FILE *output = stdout;
  45.  
  46.    prog = *argv++; argc--;
  47.  
  48.    while (argc > 0 && argv[0][0] == '-') {
  49.       switch (argv[0][1]) {
  50.       case 'c': centre = 1; break;
  51.       case 's': showpage = 1; break;
  52.       case 'r': rotate = 1; break;
  53.       case 'a': aspect = 1; break;
  54.       case 'm': maximise = 1; break;
  55.       case 'v':
  56.       default:  usage();
  57.       }
  58.       argc--;
  59.       argv++;
  60.    }
  61.  
  62.    if (argc < 4 || argc > 6) usage();
  63.    fllx = atoi(argv[0]);
  64.    flly = atoi(argv[1]);
  65.    furx = atoi(argv[2]);
  66.    fury = atoi(argv[3]);
  67.  
  68.    if (argc > 4) {
  69.       if(!(input = fopen(argv[4], "r"))) {
  70.      fprintf(stderr, "%s: can't open input file %s\n", prog, argv[4]);
  71.      exit(1);
  72.       }
  73.    }
  74.    if (argc > 5) {
  75.       if(!(output = fopen(argv[5], "w"))) {
  76.      fprintf(stderr, "%s: can't open output file %s\n", prog, argv[5]);
  77.      exit(1);
  78.       }
  79.    }
  80.  
  81.    while (fgets(buf, BUFSIZ, input)) {
  82.       if (buf[0] == '%' && (buf[1] == '%' || buf[1] == '!')) {
  83.      /* still in comment section */
  84.      if (!strncmp(buf, "%%BoundingBox:", 14)) {
  85.         double illx, illy, iurx, iury;    /* input bbox parameters */
  86.         if (sscanf(buf, "%%%%BoundingBox:%lf %lf %lf %lf\n",
  87.                &illx, &illy, &iurx, &iury) == 4) {
  88.            bbfound = 1;
  89.            llx = (int)illx;    /* accept doubles, but convert to int */
  90.            lly = (int)illy;
  91.            urx = (int)(iurx+0.5);
  92.            ury = (int)(iury+0.5);
  93.         }
  94.      } else if (!strncmp(buf, "%%EndComments", 13)) {
  95.         strcpy(buf, "\n"); /* don't repeat %%EndComments */
  96.         break;
  97.      } else fputs(buf, output);
  98.       } else break;
  99.    }
  100.  
  101.    if (bbfound) { /* put BB, followed by scale&translate */
  102.       int fwidth, fheight;
  103.       double xscale, yscale;
  104.       double xoffset = fllx, yoffset = flly;
  105.       double width = urx-llx, height = ury-lly;
  106.  
  107.       if (maximise)
  108.      if ((width > height && fury-flly > furx-fllx) ||
  109.          (width < height && fury-flly < furx-fllx)) 
  110.         rotate = 1;
  111.  
  112.       if (rotate) {
  113.      fwidth = fury - flly;
  114.      fheight = furx - fllx;
  115.       } else {
  116.      fwidth = furx - fllx;
  117.      fheight = fury - flly;
  118.       }
  119.  
  120.       xscale = fwidth/width;
  121.       yscale = fheight/height;
  122.  
  123.       if (!aspect) {       /* preserve aspect ratio ? */
  124.      xscale = yscale = min(xscale,yscale);
  125.       }
  126.       width *= xscale;     /* actual width and height after scaling */
  127.       height *= yscale;
  128.       if (centre) {
  129.      if (rotate) {
  130.         xoffset += (fheight - height)/2;
  131.         yoffset += (fwidth - width)/2;
  132.      } else {
  133.         xoffset += (fwidth - width)/2;
  134.         yoffset += (fheight - height)/2;
  135.      }
  136.       }
  137.       fprintf(output, 
  138.           "%%%%BoundingBox: %d %d %d %d\n", (int)xoffset, (int)yoffset,
  139.          (int)(xoffset+(rotate ? height : width)),
  140.          (int)(yoffset+(rotate ? width : height)));
  141.       if (rotate) {  /* compensate for original image shift */
  142.      xoffset += height + lly * yscale;  /* displacement for rotation */
  143.      yoffset -= llx * xscale;
  144.       } else {
  145.      xoffset -= llx * xscale;
  146.      yoffset -= lly * yscale;
  147.       }
  148.       fputs("%%EndComments\n", output);
  149.       if (showpage)
  150.      fputs("save /showpage{}def /copypage{}def /erasepage{}def\n", output);
  151.       else
  152.      fputs("%%BeginProcSet: epsffit 1 0\n", output);
  153.       fputs("gsave\n", output);
  154.       fprintf(output, "%.3lf %.3lf translate\n", xoffset, yoffset);
  155.       if (rotate)
  156.      fputs("90 rotate\n", output);
  157.       fprintf(output, "%.3lf %.3lf scale\n", xscale, yscale);
  158.       if (!showpage)
  159.      fputs("%%EndProcSet\n", output);
  160.    }
  161.    do {
  162.       fputs(buf, output);
  163.    } while (fgets(buf, BUFSIZ, input));
  164.    if (bbfound) {
  165.       fputs("grestore\n", output);
  166.       if (showpage)
  167.      fputs("restore showpage\n", output); /* just in case */
  168.    } else {
  169.       fprintf(stderr, "%s: no %%%%BoundingBox:\n", prog);
  170.       exit(1);
  171.    }
  172.    exit(0);
  173. }
  174.