Initial revision
[chise/xemacs-chise.git.1] / 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     local($color);
99     while ($ARGV[0] =~ /^-/) {
100         $_ = shift @ARGV;
101         if (/^-h(elp)?$/) {
102             &usage; exit}
103         elsif (/^-out$/) {
104             $out = shift @ARGV;
105         }
106         elsif (/^-(.*)$/) {
107             eval "\$$1 = shift \@ARGV"; # Create and set a flag $<name>
108             }
109     }
110 }                
111
112 sub main {
113     local($base, $outfile, $i, $j);
114     $base = &test_args;
115     $outfile = $OUTFILE || "$base.gif";
116     open(STDERR, ">/dev/null") unless $DEBUG;
117     &convert($base);
118     if (-f "$base.ppm") {
119         &crop_scale_etc("$base.ppm", $outfile);
120     }
121     else {
122         foreach $i (<$base.[1-9]*ppm>) {
123         $j = $i; 
124         $j =~ s/\.(.*)ppm/$1.gif/;
125         &crop_scale_etc($i, $j)}
126     }                           
127     &cleanup($base);
128 }
129
130 sub crop_scale_etc {
131     local($in, $out) = @_;
132     local($tmp) = $in . ".tmp";
133     open(STDERR, ">/dev/null") unless $DEBUG;
134
135     if ($flip) {
136         rename($tmp, $in) unless system("$PNMFLIP -$flip $in > $tmp");
137         }
138     system("$PNMCROP $in > $tmp");
139
140     if (system("$PPMTOGIF $tmp > $out")) {
141         print "Running ppmquant for $out\n";
142         system("$REDUCE_COLOR < $tmp|$PPMTOGIF - > $out");
143         }
144     unlink $tmp;
145     print "Writing $out\n";
146 }
147
148 sub test_args {
149     local($file) = $ARGV[0];
150     if (! ($file =~ s/\.ps$//)) {
151         print "The name of the input file must end in '.ps'\n";
152         exit}
153     elsif (! ( -f "$file.ps")) {
154         print "Cannot find file $file.ps\n.";
155         exit}
156     elsif (! ($DEPTH =~ /^(1|8|24)$/)) {
157         print "The color depth must be 1 or 8 or 24. You specified $DEPTH\n";
158         exit                    
159         }
160     if (defined $SCALE) {
161         if ($SCALE > 0) {
162             $DENSITY = int($SCALE * $DENSITY)}
163         else {
164             print "Error: The scale must be greater than 0.\n" .
165                 "You specified $SCALE\n";
166             exit}
167     }
168     $file;
169 }
170    
171 sub convert {
172     local($base) = @_;
173     local($paperopt) = "-sPAPERSIZE=$PAPERSIZE" if $PAPERSIZE;
174     local($ppmtype) = join('', "ppm",$DEPTH,"run");
175     local($density) = "-r$DENSITY" if ($DENSITY != 72);
176     open (GS, "|$GS -q -dNOPAUSE -dNO_PAUSE -sDEVICE=ppmraw $density -sOutputFile=$base.ppm $paperopt $base.ps");
177     close GS;
178 }
179
180 sub cleanup {
181     local($base) = @_;
182     unlink <$base[0-9.]*ppm>;
183 }
184
185 sub usage {
186     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";
187 }
188
189