home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pnm / pstopnm < prev    next >
Text File  |  1993-01-27  |  8KB  |  294 lines

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