home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pnm / pstopnm.cmd < prev    next >
OS/2 REXX Batch file  |  1997-04-20  |  9KB  |  296 lines

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