| 1 |
/* epsffit.c |
| 2 |
* Fit EPSF file into constrained size |
| 3 |
* |
| 4 |
* (c) Reuben Thomas 2012-2014 |
| 5 |
* (c) Angus J. C. Duggan 1991-1997 |
| 6 |
* See file LICENSE for details. |
| 7 |
* |
| 8 |
* Added filename spec (from Larry Weissman) 5 Feb 93 |
| 9 |
* Accepts double %%BoundingBox input, outputs proper BB, 4 Jun 93. (I don't |
| 10 |
* like this; developers should read the Big Red Book before writing code which |
| 11 |
* outputs PostScript. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "config.h" |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
#include <ctype.h> |
| 20 |
#include <unistd.h> |
| 21 |
#include "progname.h" |
| 22 |
#include "binary-io.h" |
| 23 |
|
| 24 |
#include "psutil.h" |
| 25 |
|
| 26 |
const char *syntax = "[-c] [-r] [-a] [-m] [-s] LLX LLY URX URY [INFILE [OUTFILE]]\n"; |
| 27 |
|
| 28 |
const char *argerr_message = ""; |
| 29 |
|
| 30 |
int |
| 31 |
main(int argc, char **argv) |
| 32 |
{ |
| 33 |
int bbfound = 0; /* %%BoundingBox: found */ |
| 34 |
int urx = 0, ury = 0, llx = 0, lly = 0; |
| 35 |
int furx, fury, fllx, flly; |
| 36 |
int showpage = 0, centre = 0, rotate = 0, aspect = 0, maximise = 0; |
| 37 |
char buf[BUFSIZ]; |
| 38 |
FILE *input = stdin; |
| 39 |
FILE *output = stdout; |
| 40 |
int opt; |
| 41 |
|
| 42 |
set_program_name(argv[0]); |
| 43 |
|
| 44 |
while((opt = getopt(argc, argv, "csramv")) != EOF) { |
| 45 |
switch(opt) { |
| 46 |
case 'c': centre = 1; break; |
| 47 |
case 's': showpage = 1; break; |
| 48 |
case 'r': rotate = 1; break; |
| 49 |
case 'a': aspect = 1; break; |
| 50 |
case 'm': maximise = 1; break; |
| 51 |
case 'v': |
| 52 |
default: |
| 53 |
usage(); |
| 54 |
break; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
if ((argc - optind) < 4 || (argc - optind) > 6) usage(); |
| 59 |
|
| 60 |
fllx = atoi(argv[optind++]); |
| 61 |
flly = atoi(argv[optind++]); |
| 62 |
furx = atoi(argv[optind++]); |
| 63 |
fury = atoi(argv[optind++]); |
| 64 |
|
| 65 |
/* Be defensive */ |
| 66 |
if((argc - optind) < 0 || (argc - optind) > 2) usage(); |
| 67 |
|
| 68 |
if ((argc - optind) > 0) { |
| 69 |
if(!(input = fopen(argv[optind], "rb"))) |
| 70 |
die("can't open input file %s", argv[optind]); |
| 71 |
optind++; |
| 72 |
} else if (set_binary_mode(fileno(stdin), O_BINARY) < 0) |
| 73 |
die("can't reset stdin to binary mode"); |
| 74 |
|
| 75 |
if ((argc - optind) > 0) { |
| 76 |
if(!(output = fopen(argv[optind], "wb"))) |
| 77 |
die("can't open output file %s", argv[optind]); |
| 78 |
optind++; |
| 79 |
} else if (set_binary_mode(fileno(stdout), O_BINARY) < 0) |
| 80 |
die("can't reset stdout to binary mode"); |
| 81 |
|
| 82 |
while (fgets(buf, BUFSIZ, input)) { |
| 83 |
if (buf[0] == '%' && (buf[1] == '%' || buf[1] == '!')) { |
| 84 |
/* still in comment section */ |
| 85 |
if (!strncmp(buf, "%%BoundingBox:", 14)) { |
| 86 |
double illx, illy, iurx, iury; /* input bbox parameters */ |
| 87 |
if (sscanf(buf, "%%%%BoundingBox:%lf %lf %lf %lf\n", |
| 88 |
&illx, &illy, &iurx, &iury) == 4) { |
| 89 |
bbfound = 1; |
| 90 |
llx = (int)illx; /* accept doubles, but convert to int */ |
| 91 |
lly = (int)illy; |
| 92 |
urx = (int)(iurx+0.5); |
| 93 |
ury = (int)(iury+0.5); |
| 94 |
} |
| 95 |
} else if (!strncmp(buf, "%%EndComments", 13)) { |
| 96 |
strcpy(buf, "\n"); /* don't repeat %%EndComments */ |
| 97 |
break; |
| 98 |
} else fputs(buf, output); |
| 99 |
} else break; |
| 100 |
} |
| 101 |
|
| 102 |
if (bbfound) { /* put BB, followed by scale&translate */ |
| 103 |
int fwidth, fheight; |
| 104 |
double xscale, yscale; |
| 105 |
double xoffset = fllx, yoffset = flly; |
| 106 |
double width = urx-llx, height = ury-lly; |
| 107 |
|
| 108 |
if (maximise) |
| 109 |
if ((width > height && fury-flly > furx-fllx) || |
| 110 |
(width < height && fury-flly < furx-fllx)) |
| 111 |
rotate = 1; |
| 112 |
|
| 113 |
if (rotate) { |
| 114 |
fwidth = fury - flly; |
| 115 |
fheight = furx - fllx; |
| 116 |
} else { |
| 117 |
fwidth = furx - fllx; |
| 118 |
fheight = fury - flly; |
| 119 |
} |
| 120 |
|
| 121 |
xscale = fwidth/width; |
| 122 |
yscale = fheight/height; |
| 123 |
|
| 124 |
if (!aspect) { /* preserve aspect ratio ? */ |
| 125 |
xscale = yscale = MIN(xscale,yscale); |
| 126 |
} |
| 127 |
width *= xscale; /* actual width and height after scaling */ |
| 128 |
height *= yscale; |
| 129 |
if (centre) { |
| 130 |
if (rotate) { |
| 131 |
xoffset += (fheight - height)/2; |
| 132 |
yoffset += (fwidth - width)/2; |
| 133 |
} else { |
| 134 |
xoffset += (fwidth - width)/2; |
| 135 |
yoffset += (fheight - height)/2; |
| 136 |
} |
| 137 |
} |
| 138 |
fprintf(output, |
| 139 |
"%%%%BoundingBox: %d %d %d %d\n", (int)xoffset, (int)yoffset, |
| 140 |
(int)(xoffset+(rotate ? height : width)), |
| 141 |
(int)(yoffset+(rotate ? width : height))); |
| 142 |
if (rotate) { /* compensate for original image shift */ |
| 143 |
xoffset += height + lly * yscale; /* displacement for rotation */ |
| 144 |
yoffset -= llx * xscale; |
| 145 |
} else { |
| 146 |
xoffset -= llx * xscale; |
| 147 |
yoffset -= lly * yscale; |
| 148 |
} |
| 149 |
fputs("%%EndComments\n", output); |
| 150 |
if (showpage) |
| 151 |
fputs("save /showpage{}def /copypage{}def /erasepage{}def\n", output); |
| 152 |
else |
| 153 |
fputs("%%BeginProcSet: epsffit 1 0\n", output); |
| 154 |
fputs("gsave\n", output); |
| 155 |
fprintf(output, "%.3f %.3f translate\n", xoffset, yoffset); |
| 156 |
if (rotate) |
| 157 |
fputs("90 rotate\n", output); |
| 158 |
fprintf(output, "%.3f %.3f scale\n", xscale, yscale); |
| 159 |
if (!showpage) |
| 160 |
fputs("%%EndProcSet\n", output); |
| 161 |
} |
| 162 |
do { |
| 163 |
fputs(buf, output); |
| 164 |
} while (fgets(buf, BUFSIZ, input)); |
| 165 |
if (bbfound) { |
| 166 |
fputs("grestore\n", output); |
| 167 |
if (showpage) |
| 168 |
fputs("restore showpage\n", output); /* just in case */ |
| 169 |
} else |
| 170 |
die("no %%%%BoundingBox:"); |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |