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