| 1 |
#!/bin/csh -f |
| 2 |
# |
| 3 |
# Uses ghostscript to translate an Encapsulated PostScript file to |
| 4 |
# Portable Anymap format file(s). |
| 5 |
# pstopnm will create as many files as the number of pages in |
| 6 |
# the Postscript document. The name of the files will be |
| 7 |
# psfile001.ppm, psfile002.ppm, etc. |
| 8 |
# The output files will contain the area inside the BoundingBox. |
| 9 |
# If BoundingBox parameters are not found in the PostScript |
| 10 |
# document, default values are used. |
| 11 |
# |
| 12 |
# |
| 13 |
# Usage: pstopnm [-forceplain] [-help] [-llx s] [-lly s] |
| 14 |
# [-urx s] [-ury s] [-nocrop] [-pbm|-pgm|-ppm] |
| 15 |
# [-verbose] [-xborder n] [-xmax n] [-xsize n] |
| 16 |
# [-yborder n] [-ymax n] [-ysize n] |
| 17 |
# [-portrait] [-landscape] psfile[.ps] |
| 18 |
# |
| 19 |
# Copyright (C) 1992 by Alberto Accomazzi, Smithsonian Astrophysical |
| 20 |
# Observatory ([email protected]). |
| 21 |
# |
| 22 |
# Permission to use, copy, modify, and distribute this software and its |
| 23 |
# documentation for any purpose and without fee is hereby granted, |
| 24 |
# provided that the above copyright notice appear in all copies and |
| 25 |
# that both that copyright notice and this permission notice appear |
| 26 |
# in supporting documentation. This software is provided "as is" |
| 27 |
# without express or implied warranty. |
| 28 |
# |
| 29 |
set noglob |
| 30 |
|
| 31 |
set progname = $0 |
| 32 |
set progname = $progname:t |
| 33 |
set filtertail = "raw" |
| 34 |
set filterhead = "ppm" |
| 35 |
set xsize = 0 |
| 36 |
set ysize = 0 |
| 37 |
set xres = "" |
| 38 |
set yres = "" |
| 39 |
|
| 40 |
# default values: max image x and y sizes |
| 41 |
set xmax = 612 |
| 42 |
set ymax = 792 |
| 43 |
# default values: image area fits in a 8.5x11 sheet with 1 inch border |
| 44 |
set llx = 72 |
| 45 |
set lly = 72 |
| 46 |
set urx = 540 |
| 47 |
set ury = 720 |
| 48 |
# default values: x and y borders are 10% of x and y size |
| 49 |
set xborder = "0.1" |
| 50 |
set yborder = "0.1" |
| 51 |
# default values: orientation is unknown |
| 52 |
set orient = 0 |
| 53 |
|
| 54 |
set psfile = "" |
| 55 |
set USAGE = "Usage: $progname [-forceplain] [-help] [-llx s] [-lly s]\ |
| 56 |
[-urx s] [-ury s] [-landscape] [-portrait]\ |
| 57 |
[-nocrop] [-pbm|-pgm|-ppm] [-verbose] [-xborder s] [-xmax s]\ |
| 58 |
[-xsize s] [-yborder s] [-ymax s] [-ysize s] psfile[.ps]" |
| 59 |
alias usage 'echo $USAGE; exit 1' |
| 60 |
|
| 61 |
while ($#argv > 0) |
| 62 |
switch ($argv[1]) |
| 63 |
case -h*: # -help |
| 64 |
usage |
| 65 |
breaksw |
| 66 |
case -pbm: |
| 67 |
case -pgm: |
| 68 |
case -ppm: |
| 69 |
set filterhead = `echo "$argv[1]" | sed "s/-//1"` |
| 70 |
breaksw |
| 71 |
case -llx: |
| 72 |
shift argv |
| 73 |
if ($#argv == 0) eval usage |
| 74 |
set llx = `(echo "scale=4";echo "$argv[1] * 72")|bc -l` |
| 75 |
set nobb |
| 76 |
breaksw |
| 77 |
case -lly: |
| 78 |
shift argv |
| 79 |
if ($#argv == 0) eval usage |
| 80 |
set lly = `(echo "scale=4";echo "$argv[1] * 72")|bc -l` |
| 81 |
set nobb |
| 82 |
breaksw |
| 83 |
case -urx: |
| 84 |
shift argv |
| 85 |
if ($#argv == 0) eval usage |
| 86 |
set urx = `(echo "scale=4";echo "$argv[1] * 72")|bc -l` |
| 87 |
set nobb |
| 88 |
breaksw |
| 89 |
case -ury: |
| 90 |
shift argv |
| 91 |
if ($#argv == 0) eval usage |
| 92 |
set ury = `(echo "scale=4";echo "$argv[1] * 72")|bc -l` |
| 93 |
set nobb |
| 94 |
breaksw |
| 95 |
case -no*: # -nocrop |
| 96 |
set nocrop |
| 97 |
breaksw |
| 98 |
case -xs*: # -xsize |
| 99 |
shift argv |
| 100 |
if ($#argv == 0) eval usage |
| 101 |
@ xsize = $argv[1] |
| 102 |
breaksw |
| 103 |
case -ys*: # -ysize |
| 104 |
shift argv |
| 105 |
if ($#argv == 0) eval usage |
| 106 |
@ ysize = $argv[1] |
| 107 |
breaksw |
| 108 |
case -xm*: # -xmax |
| 109 |
shift argv |
| 110 |
if ($#argv == 0) eval usage |
| 111 |
@ xmax = $argv[1] |
| 112 |
breaksw |
| 113 |
case -ym*: # -ymax |
| 114 |
shift argv |
| 115 |
if ($#argv == 0) eval usage |
| 116 |
@ ymax = $argv[1] |
| 117 |
breaksw |
| 118 |
case -xb*: # -xborder |
| 119 |
shift argv |
| 120 |
if ($#argv == 0) eval usage |
| 121 |
set xborder = $argv[1] |
| 122 |
breaksw |
| 123 |
case -yb*: # -yborder |
| 124 |
shift argv |
| 125 |
if ($#argv == 0) eval usage |
| 126 |
set yborder = $argv[1] |
| 127 |
breaksw |
| 128 |
case -f*: # -forceplain |
| 129 |
set filtertail = "" |
| 130 |
breaksw |
| 131 |
case -s*: # -stdout |
| 132 |
set goto_stdout |
| 133 |
breaksw |
| 134 |
case -v*: # -verbose |
| 135 |
set verb |
| 136 |
breaksw |
| 137 |
case -po*: # -portrait |
| 138 |
set orient = 1 |
| 139 |
breaksw |
| 140 |
case -la*: # -landscape |
| 141 |
set orient = 2 |
| 142 |
breaksw |
| 143 |
case -*: |
| 144 |
echo "${progname}: Unknown option $argv[1]" |
| 145 |
usage |
| 146 |
breaksw |
| 147 |
default: # input file |
| 148 |
set psfile = $argv[1] |
| 149 |
set ppmfile = `basename $argv[1] .ps` |
| 150 |
breaksw |
| 151 |
endsw |
| 152 |
shift argv |
| 153 |
end |
| 154 |
|
| 155 |
if ($psfile =~ "") eval usage |
| 156 |
if (! -f $psfile) then |
| 157 |
echo "${progname}: file $psfile not found" |
| 158 |
usage |
| 159 |
endif |
| 160 |
|
| 161 |
set bb = `grep "%%BoundingBox" $psfile` |
| 162 |
if ($?nobb == 0 && $#bb == 5) then |
| 163 |
set llx = $bb[2] |
| 164 |
set lly = $bb[3] |
| 165 |
set urx = $bb[4] |
| 166 |
set ury = $bb[5] |
| 167 |
else |
| 168 |
if ($?nobb == 0) \ |
| 169 |
echo "${progname}: warning: BoundingBox not found in input file" |
| 170 |
endif |
| 171 |
|
| 172 |
set tmpsx = `(echo "scale=4";echo "$urx - $llx")|bc -l` |
| 173 |
set tmpsy = `(echo "scale=4";echo "$ury - $lly")|bc -l` |
| 174 |
|
| 175 |
# see if orientation was specified |
| 176 |
if ($orient == 0) then |
| 177 |
# no orientation was specified; compute default orientation |
| 178 |
set tmpx = 0 |
| 179 |
set tmpy = 0 |
| 180 |
set tmpsx1 = $tmpsx:r |
| 181 |
set tmpsy1 = $tmpsy:r |
| 182 |
# default is landscape mode |
| 183 |
set orient = 2 |
| 184 |
if ($xsize == 0 && $ysize == 0) then |
| 185 |
set tmpx = $xmax |
| 186 |
set tmpy = $ymax |
| 187 |
else |
| 188 |
if ($xsize != 0) set tmpx = $xsize |
| 189 |
if ($ysize != 0) set tmpy = $ysize |
| 190 |
endif |
| 191 |
if ($tmpx == 0 || $tmpy == 0) then |
| 192 |
# only one size was specified |
| 193 |
if ($tmpsy1 > $tmpsx1) set orient = 1 |
| 194 |
else |
| 195 |
# no size or both sizes were specified |
| 196 |
if ($tmpsy1 > $tmpsx1 && $tmpy > $tmpx) set orient = 1 |
| 197 |
if ($tmpsx1 > $tmpsy1 && $tmpx > $tmpy) set orient = 1 |
| 198 |
endif |
| 199 |
endif |
| 200 |
|
| 201 |
# now reset BoundingBox llc and total size to take into account margin |
| 202 |
set llx = `(echo "scale=4";echo "$llx - $tmpsx * $xborder")|bc -l` |
| 203 |
set lly = `(echo "scale=4";echo "$lly - $tmpsy * $yborder")|bc -l` |
| 204 |
set urx = `(echo "scale=4";echo "$urx + $tmpsx * $xborder")|bc -l` |
| 205 |
set ury = `(echo "scale=4";echo "$ury + $tmpsy * $yborder")|bc -l` |
| 206 |
# compute image area size |
| 207 |
set sx = `(echo "scale=4";echo "$tmpsx + 2 * $xborder * $tmpsx")|bc -l` |
| 208 |
set sy = `(echo "scale=4";echo "$tmpsy + 2 * $yborder * $tmpsy")|bc -l` |
| 209 |
|
| 210 |
if ($orient != 1) then |
| 211 |
# render image in landscape mode |
| 212 |
set tmpsx = $sx |
| 213 |
set sx = $sy |
| 214 |
set sy = $tmpsx |
| 215 |
endif |
| 216 |
|
| 217 |
# if xsize or ysize was specified, compute resolution from them |
| 218 |
if ($xsize != 0) set xres = `(echo "scale=4";echo "$xsize *72 / $sx")|bc -l` |
| 219 |
if ($ysize != 0) set yres = `(echo "scale=4";echo "$ysize *72 / $sy")|bc -l` |
| 220 |
|
| 221 |
if ($xres =~ "" && $yres !~ "") then |
| 222 |
# ysize was specified, xsize was not; compute xsize based on ysize |
| 223 |
set xres = $yres |
| 224 |
set xsize = `(echo "scale=4";echo "$sx * $xres /72 + 0.5")|bc -l` |
| 225 |
set xsize = $xsize:r |
| 226 |
else |
| 227 |
if ($yres =~ "" && $xres !~ "") then |
| 228 |
# xsize was specified, ysize was not; compute ysize based on xsize |
| 229 |
set yres = $xres |
| 230 |
set ysize = `(echo "scale=4";echo "$sy * $yres /72 + 0.5")|bc -l` |
| 231 |
set ysize = $ysize:r |
| 232 |
else |
| 233 |
if ($xres =~ "" && $yres =~ "") then |
| 234 |
# neither xsize nor ysize was specified; compute them from |
| 235 |
# xmax and ymax |
| 236 |
set xres = `(echo "scale=4";echo "$xmax *72/$sx")|bc -l` |
| 237 |
set yres = `(echo "scale=4";echo "$ymax *72/$sy")|bc -l` |
| 238 |
set xres = `(echo "scale=4";echo "if($xres>$yres)$yres";echo "if($yres>$xres)$xres";echo "if($xres==$yres)$xres")|bc -l` |
| 239 |
set yres = $xres |
| 240 |
if ($?nocrop) then |
| 241 |
# keep output file dimensions equal to xmax and ymax |
| 242 |
set xsize = $xmax |
| 243 |
set ysize = $ymax |
| 244 |
else |
| 245 |
set xsize = `(echo "scale=4";echo "$sx * $xres /72+0.5")|bc -l` |
| 246 |
set ysize = `(echo "scale=4";echo "$sy * $yres /72+0.5")|bc -l` |
| 247 |
endif |
| 248 |
set xsize = $xsize:r |
| 249 |
set ysize = $ysize:r |
| 250 |
endif |
| 251 |
endif |
| 252 |
endif |
| 253 |
|
| 254 |
# translate + rotate image, if necessary |
| 255 |
if ($orient == 1) then |
| 256 |
# portrait mode |
| 257 |
# adjust offsets |
| 258 |
set llx = `(echo "scale=4";echo "$llx - ($xsize *72/$xres - $sx)/2")|bc -l` |
| 259 |
set lly = `(echo "scale=4";echo "$lly - ($ysize *72/$yres - $sy)/2")|bc -l` |
| 260 |
set pstrans = "$llx neg $lly neg translate" |
| 261 |
else |
| 262 |
# landscape mode |
| 263 |
# adjust offsets |
| 264 |
set llx = `(echo "scale=4";echo "$llx - ($ysize *72/$yres - $sy)/2")|bc -l` |
| 265 |
set ury = `(echo "scale=4";echo "$ury + ($xsize *72/$xres - $sx)/2")|bc -l` |
| 266 |
set pstrans = "90 rotate $llx neg $ury neg translate" |
| 267 |
endif |
| 268 |
|
| 269 |
if ($?goto_stdout) then |
| 270 |
set outfile = "-" |
| 271 |
else |
| 272 |
set outfile = $ppmfile%03d.$filterhead |
| 273 |
endif |
| 274 |
|
| 275 |
if ($?verb) then |
| 276 |
echo "sx = $sx" |
| 277 |
echo "sy = $sy" |
| 278 |
echo "xres = $xres" |
| 279 |
echo "yres = $yres" |
| 280 |
echo "xsize = $xsize" |
| 281 |
echo "ysize = $ysize" |
| 282 |
echo -n "orientation " |
| 283 |
if ($orient == 1) then |
| 284 |
echo "portrait" |
| 285 |
else |
| 286 |
echo "landscape" |
| 287 |
endif |
| 288 |
echo "PS header: $pstrans" |
| 289 |
endif |
| 290 |
|
| 291 |
echo "${progname}: writing $filterhead file(s)" 1>&2 |
| 292 |
|
| 293 |
echo $pstrans | \ |
| 294 |
gs -sDEVICE=${filterhead}${filtertail} \ |
| 295 |
-sOutputFile=$outfile \ |
| 296 |
-g${xsize}x${ysize} \ |
| 297 |
-r${xres}x${yres} \ |
| 298 |
-q - $psfile |
| 299 |
|
| 300 |
|
| 301 |
|