Initial Commit
[packages] / xemacs-packages / tm / lib-src / pstogif
1 : # -*-Perl-*-
2 eval 'exec perl -w -S $0 ${1+"$@"}' # Portability kludge
3     if 0;
4
5 # pstogif.pl v1.0, July 1994, by Nikos Drakos <nikos@cbl.leeds.ac.uk>
6 # Computer Based Learning Unit, University of Leeds.
7 #
8 # Accompanies LaTeX2HTML Version 96.1
9 #
10 # Script to convert an arbitrary PostScript image to a cropped GIF image
11 # suitable for incorporation into HTML documents as inlined images to be
12 # viewed with WWW browsers.
13 #
14 # This is based on the pstoepsi script 
15 # by Doug Crabill dgc@cs.purdue.edu
16 #
17 # Please note the following:
18 # - The source PostScript file must end
19 #   in a .ps extention.  This is a GhostScript requirement, not mine...
20 # - The -density argument has no effect unless the 
21 #   color depth (set with the -depth argument) is equal to 1.
22 # - Valid arguments for -depth are 1,8, or 24.
23 #  
24 # This software is provided as is without any guarantee.
25 #
26 # Nikos Drakos (ND), nikos@cbl.leeds.ac.uk
27 # Computer Based Learning Unit, University of Leeds.
28 #
29 # 15 Jan 96 HS Call ppmquant only if needed.  Fixed bug relative to
30 #    V 95.3 .
31 #
32 # 15 Dec 95 HS (Herbert Swan <dprhws.edp.Arco.com> Added support for
33 #    the flip=option.  This allows images to be oriented differently
34 #    in the paper versus the electronic media
35 #
36 # 1 Nov 95 jmn - modified for use with gs ppm driver - from jhrg's patches
37 #    note that ppmtops.ps and ppmtops3.ps are no longer needed
38 #
39 # 20 JUL 94 ND Converted to Perl and made several changes eg it now accepts 
40 #    parameters from environment variables or from command line or will use 
41 #    default ones. 
42 #      
43 # 1  APR 94 ND Changed the suffixes of multi-page files from xbm to gif (oops!)
44 #
45
46
47 #####################################################################
48 $| =1;
49 &read_args;
50
51 ### You may need to specify some pathnames here if you want to
52 ### run the script without LaTeX2HTML
53
54 # Ghostscript
55 $GS= $ENV{'GS'} || 'gs';
56
57 # Comes with LaTeX2HTML (For ghostscript versions greater than 3.0 
58 # you need the newer pstoppm.ps)
59 #$PSTOPPM= $ENV{'PSTOPPM'} ||
60 #    'pstoppm.ps';
61
62 # Available in the PBMPLUS library         
63 $PNMCROP=$ENV{'PNMCROP'} || 'pnmcrop' ;
64
65 # Also in PBMPLUS
66 $PNMFLIP=$ENV{'PNMFLIP'} || 'pnmflip' ;
67
68 # Also in PBMPPLUS        
69 $PPMTOGIF=$ENV{'PPMTOGIF'} || 'ppmtogif' ;
70
71 # Also in PBMPPLUS        
72 $REDUCE_COLOR=$ENV{'PPMQUANT'} || 'ppmquant 256' ;
73  
74 $OUTFILE = $ENV{'OUTFILE'} || $out;
75                         
76 # Valid choices for $COLOR_DEPTH are 1, 8 or 24. 
77 $DEPTH = $ENV{'DEPTH'} || $depth || 24;
78
79 #Default density is 72
80 $DENSITY = $ENV{'DENSITY'} || $density || 72;
81     
82 # Valid choices are any numbers greater than zero
83 # Useful choices are numbers between 0.1 - 5
84 # Large numbers may generate very large intermediate files
85 # and will take longer to process
86 $SCALE = $ENV{'SCALE'} || $scale; # No default value
87
88 $PAPERSIZE = $ENV{'PAPERSIZE'} || $papersize; # No default value;
89
90 $DEBUG = $ENV{'DEBUG'} || $DEBUG || 0;
91
92 ######################################################################
93
94 &main;
95
96 sub read_args {
97     local($_);
98     while ($ARGV[0] =~ /^-/) {
99         $_ = shift @ARGV;
100         if (/^-h(elp)?$/) {
101             &usage; exit}
102         elsif (/^-out$/) {
103             $out = shift @ARGV;
104         }
105         elsif (/^-(.*)$/) {
106             eval "\$$1 = shift \@ARGV"; # Create and set a flag $<name>
107             }
108     }
109 }                
110
111 sub main {
112     local($base, $outfile, $i, $j);
113     $base = &test_args;
114     $outfile = $OUTFILE || "$base.gif";
115     open(STDERR, ">/dev/null") unless $DEBUG;
116     &convert($base);
117     if (-f "$base.ppm") {
118         &crop_scale_etc("$base.ppm", $outfile);
119     }
120     else {
121         foreach $i (<$base.[1-9]*ppm>) {
122         $j = $i; 
123         $j =~ s/\.(.*)ppm/$1.gif/;
124         &crop_scale_etc($i, $j)}
125     }                           
126     &cleanup($base);
127 }
128
129 sub crop_scale_etc {
130     local($in, $out) = @_;
131     local($tmp) = $in . ".tmp";
132     open(STDERR, ">/dev/null") unless $DEBUG;
133
134     if ($flip) {
135         rename($tmp, $in) unless system("$PNMFLIP -$flip $in > $tmp");
136         }
137     system("$PNMCROP $in > $tmp");
138
139     if (system("$PPMTOGIF $tmp > $out")) {
140         print "Running ppmquant for $out\n";
141         system("$REDUCE_COLOR < $tmp|$PPMTOGIF - > $out");
142         }
143     unlink $tmp;
144     print "Writing $out\n";
145 }
146
147 sub test_args {
148     local($file) = $ARGV[0];
149     if (! ($file =~ s/\.ps$//)) {
150         print "The name of the input file must end in '.ps'\n";
151         exit}
152     elsif (! ( -f "$file.ps")) {
153         print "Cannot find file $file.ps\n.";
154         exit}
155     elsif (! ($DEPTH =~ /^(1|8|24)$/)) {
156         print "The color depth must be 1 or 8 or 24. You specified $DEPTH\n";
157         exit                    
158         }
159     if (defined $SCALE) {
160         if ($SCALE > 0) {
161             $DENSITY = int($SCALE * $DENSITY)}
162         else {
163             print "Error: The scale must be greater than 0.\n" .
164                 "You specified $SCALE\n";
165             exit}
166     }
167     $file;
168 }
169    
170 sub convert {
171     local($base) = @_;
172     local($paperopt) = "-sPAPERSIZE=$PAPERSIZE" if $PAPERSIZE;
173     local($density) = "-r$DENSITY" if ($DENSITY != 72);
174     open (GS, "|$GS -q -dNOPAUSE -dNO_PAUSE -dSAFER -sDEVICE=ppmraw $density -sOutputFile=$base.ppm $paperopt $base.ps");
175     close GS;
176 }
177
178 sub cleanup {
179     local($base) = @_;
180     unlink <$base[0-9.]*ppm>;
181 }
182
183 sub usage {
184     print "Usage: pstogif [-h(elp)] [-out <output file>] [-depth <color depth 1, 8 or 24>]  [-flip <Flip_code>] [-density <pixel density>] <file>.ps\n\n";
185 }
186
187