Debian Codegraph @

doc/scripts/pstoimg

raw
1 #! /usr/bin/perl -w
2 ##############################################################################
3 # $Id: pstoimg,v 1.1 2001/10/05 13:50:21 newville Exp $
4 #
5 # pstoimg
6 #
7 # Accompanies LaTeX2HTML
8 #
9 # Script to convert an arbitrary PostScript image to a cropped GIF or PNG
10 # image suitable for incorporation into HTML documents as inlined images
11 # to be viewed with WWW browsers.
12 #
13 # This software is provided as is without any guarantee.
14 #
15 ##############################################################################
16 #
17 # $Log: pstoimg,v $
18 # Revision 1.1 2001/10/05 13:50:21 newville
19 # *** empty log message ***
20 #
21 # Revision 1.13 2001/03/27 09:27:38 RRM
22 # -- inserted missing '$' for $edge .
23 #
24 # Revision 1.12 2001/03/25 02:05:14 RRM
25 # -- implement the temporary hack to pnmcrop, using -black to help detect
26 # the correct color for cropping-bars; only workd with black bars.
27 # A complete fix will be available with Netpbm v9.12 .
28 #
29 # Revision 1.11 1999/10/25 21:18:22 MRO
30 #
31 # -- added more configure options (Jens' suggestions)
32 # -- fixed bug in regexp range reported by Achim Haertel
33 # -- fixed old references in documentation (related to mail list/archive)
34 #
35 # Revision 1.10 1999/10/06 22:04:13 MRO
36 #
37 # -- texexpand: latex2html calls texexpand with the -out option instead of
38 # output redirection: this is safer on non-UNIX platforms
39 # -- pstoimg: now there's no default cropping (useful for standalone
40 # conversions). latex2html was changes appropriately
41 # -- minor cleanups in latex2html script and documentation
42 #
43 # Revision 1.9 1999/09/14 22:02:02 MRO
44 #
45 # -- numerous cleanups, no new features
46 #
47 # Revision 1.8 1999/07/19 09:51:00 RRM
48 # -- added -aaliastext switch, for easier way to specify anti-aliased
49 # font characters, without also anti-aliasing other graphics objects.
50 #
51 # Revision 1.7 1999/06/24 07:28:59 MRO
52 #
53 #
54 # -- removed L2HMODULE
55 # -- fixed processing of -info switch
56 # -- changed option order for dvips on win32 (thanks JCL)
57 # -- bumped version to 99.2a8
58 #
59 # Revision 1.6 1999/06/06 14:24:50 MRO
60 #
61 #
62 # -- many cleanups wrt. to TeXlive
63 # -- changed $* to /m as far as possible. $* is deprecated in perl5, all
64 # occurrences should be removed.
65 #
66 # Revision 1.5 1999/06/04 20:14:25 MRO
67 #
68 #
69 # -- Reworked option parsing completely. Should behave much the same as before,
70 # options with -no_* work just like before.
71 # -- Changed $NOFORK to $CAN_FORK and inverted the logic.
72 # -- Small debugging enhancement in pstoimg
73 #
74 # Revision 1.4 1999/06/04 15:30:15 MRO
75 #
76 #
77 # -- fixed errors introduced by cleaning up TMP*
78 # -- made pstoimg -quiet really quiet
79 # -- pstoimg -debug now saves intermediate result files
80 # -- several fixes for OS/2
81 #
82 # Revision 1.3 1999/06/01 06:55:35 MRO
83 #
84 #
85 # - fixed small bug in L2hos/*
86 # - added some test_mode related output to latex2html
87 # - improved documentation
88 # - fixed small bug in pstoimg wrt. OS2
89 #
90 # Revision 1.2 1999/05/17 21:30:59 MRO
91 #
92 #
93 # -- make texexpand warning-free and start making it use strict
94 # compliant
95 #
96 # Revision 1.1 1999/05/11 06:10:00 MRO
97 #
98 #
99 # - merged config stuff, did first tries on Linux. Simple document
100 # passes! More test required, have to ger rid of Warnings in texexpand
101 #
102 # Revision 1.18 1999/05/05 19:47:03 MRO
103 #
104 #
105 # - many cosmetic changes
106 # - final backup before merge
107 #
108 # Revision 1.17 1999/03/15 23:00:53 MRO
109 #
110 #
111 # - moved L2hos modules to top level directory, so that no dir-
112 # delimiter is necessary in the @INC-statement.
113 # - changed strategy for "shave": Do not rely on STDERR redirection any
114 # more (caused problems on at least Win32)
115 #
116 # Revision 1.16 1999/02/14 23:44:34 MRO
117 #
118 #
119 # -- first attempt to fix Win32 problems
120 #
121 # Revision 1.15 1999/02/11 00:18:29 MRO
122 #
123 #
124 # -- cleaned up warppers, TeXlive stuff and Makefile
125 #
126 # Revision 1.14 1999/02/10 01:37:12 MRO
127 #
128 #
129 # -- changed os-dependency structure again - now neat OO modules are
130 # used: portable, extensible, neat!
131 # -- some minor cleanups and bugfixes
132 #
133 # Revision 1.13 1998/12/07 23:19:58 MRO
134 #
135 #
136 # -- added POD documentation to pstoimg and did a general cleanup
137 # -- some finetuning of config procedure and modules
138 #
139 # Revision 1.12 1998/10/31 14:13:05 MRO
140 # -- changed OS-dependent module loading strategy: Modules are now located in
141 # different (OS-specific) directories nut have the same name: Easier to
142 # maintain and cleaner code
143 # -- Cleaned up config procedure
144 # -- Extended makefile functionality
145 #
146 # Revision 1.11 1998/08/09 20:45:20 MRO
147 # -- some cleanup
148 #
149 # Revision 1.10 1998/06/14 14:10:38 latex2html
150 # -- Started to implement TeXlive configuration and better OS specific
151 # handling (Batch files) (Marek)
152 #
153 # Revision 1.9 1998/06/07 22:35:24 latex2html
154 # -- included things I learned from the Win95 port to config procedure:
155 # GS_LIB, Win32 module calls, directory separator stuff, ... (Marek)
156 #
157 # Revision 1.8 1998/06/01 12:57:56 latex2html
158 # -- Cleanup and cosmetics.
159 #
160 # Revision 1.7 1998/05/14 22:27:37 latex2html
161 # -- more work on config procedure (Makefile, GS_LIB)
162 # -- tested pstoimg in 98.1 environment successfully on Linux
163 #
164 # Revision 1.6 1998/05/06 22:31:09 latex2html
165 # -- Enhancements to the config procedure: Added a "generic" target
166 # in the Makefile for the TeXlive CD (not perfect yet)
167 # -- included test for kpsewhich / Web2C
168 # -- included latest stuff from Override.pm into os_*.pm
169 #
170 # Revision 1.5 1998/04/28 22:18:11 latex2html
171 # - The platform specific stuff is now kept in a separate perl module. This
172 # does not introduce significant overhead and enhances maintainability.
173 #
174 # Revision 1.4 1998/03/19 23:38:06 latex2html
175 # -- made pstoimg plug-in compatible with old one (touchwood!)
176 # -- cleaned up, added some comments
177 # -- inserted version information output
178 # -- incorporated patches to make OS/2 run better (thanks Uli)
179 # -- updated Makefile: make, make test, make install should work now
180 #
181 # Revision 1.3 1998/03/11 23:44:00 latex2html
182 # -- cleaned up config.pl and reworked dvips checks
183 # -- got pstoimg.pin up to par with the regular pstoimg
184 # -- cosmetic changes
185 # -- runs now under Win95 with Fabrice Popineau's Win32 tools (gs, TeX,...)
186 #
187 # Revision 1.2 1998/03/02 23:38:40 latex2html
188 # Reworked configuration procedure substantially. Fixed some killing bugs.
189 # Should now run on Win32, too.
190 # The file prefs.pm contains user-configurable stuff for DOS platforms.
191 # UNIX users can override the settings with the configure utility (preferred).
192 #
193 # Revision 1.1 1998/02/14 19:31:55 latex2html
194 # Preliminary checkin of configuration procedure
195 #
196 # (end CVS log)
197 ###############################################################################
198
199 # This file has been automatically generated by build.pl from pstoimg.pin
200 # Do not edit this file as your changes will be lost when reconfiguring.
201 # If you want to supply patches, please apply them to pstoimg.pin and send
202 # the diff relative to the original pstoimg.pin. Thank you.
203
204 =head1 NAME
205
206 pstoimg - Convert a PostScript file to a bitmap image using
207 Ghostscript and the Netpbm utilities
208
209 =cut
210
211 use 5.003;
212 use strict;
213 #use diagnostics;
214 use vars qw(*SAVEERR $LATEX2HTMLDIR $SCRIPT);
215
216 # This variable points to the DIRECTORY where the latex2html files
217 # can be found.
218
219 use Getopt::Long;
220
221
222 # see below for a description of the environment
223
224 BEGIN {
225 # print "scanning for l2hdir\n";
226 if($ENV{LATEX2HTMLDIR}) {
227 $LATEX2HTMLDIR = $ENV{LATEX2HTMLDIR};
228 } else {
229 $ENV{LATEX2HTMLDIR} = $LATEX2HTMLDIR = '/usr/local/lib/latex2html';
230 }
231
232 if(-d $LATEX2HTMLDIR) {
233 push(@INC,$LATEX2HTMLDIR);
234 } else {
235 die qq{Fatal: Directory "$LATEX2HTMLDIR" does not exist.\n};
236 }
237 }
238
239 use L2hos; # load OS-specific stuff
240
241 my $RELEASE = '2K.1beta';
242 my ($VERSION) = q$Revision: 1.1 $ =~ /:\s*(\S+)/;
243
244 $| = 1; # unbuffer STDOUT
245
246 my $dd = L2hos->dd; # Directory delimiter
247 my $prompt;
248 ($prompt = $0) =~ s|^.*[/\\]||;
249
250 # Configuration as determined by "configure"
251 #
252 # Ghostscript
253 my $GS = '/usr/bin/gs';
254 my $GSDEVICE = 'pnmraw';
255 my $GSALIASDEVICE = 'ppmraw';
256 # Set path for ghostscript fonts and libraries
257 $ENV{GS_LIB} = '/usr/share/ghostscript/5.50' unless(defined $ENV{GS_LIB});
258 # Supported format(s)
259 my @IMAGE_TYPES = qw(png gif);
260 # Netpbm
261 my $PNMCROP = '/usr/bin/pnmcrop ';
262 my $PNMBLACK = ' -black ';
263 my $PPMQUANT = '/usr/bin/ppmquant';
264 my $PNMFLIP = '/usr/bin/pnmflip';
265 my $PNMCAT = '/usr/bin/pnmcat';
266 my $PNMFILE = '/usr/bin/pnmfile';
267 my $PBMMAKE = '/usr/bin/pbmmake';
268 # GIF support
269 my $PPMTOGIF = '/usr/bin/ppmtogif';
270 # PNG support
271 my $PNMTOPNG = '/usr/bin/pnmtopng';
272 # Temporary diskspace
273 my $def_tmp = '/tmp'; # Space for temporary files
274
275 # Some lengths used by dvips
276 # MRO: Is this true for all runs of dvips?
277
278 my $PAGE_HEIGHT = 841.889; # dvips page height, in pts.
279 my $PAGE_WIDTH = 595.275; # dvips page width, in pts.
280 my $PAGE_HMARGIN = 72; # dvips margin: 1 inch = 72pt
281 my $PAGE_VMARGIN = 72; # dvips margin: 1 inch = 72pt
282
283 # The color to be made transparent
284 my $trans_color = '#ffffff';
285
286 ###############################################################################
287 # Default settings
288 # Environment overrides defaults, command line options override everything
289
290 unless(@ARGV) {
291 print_help();
292 exit 0;
293 }
294
295 =head1 SYNOPSIS
296
297 B<pstoimg> B<-help> | B<-version>
298
299 B<pstoimg>
300 S<[ B<-antialias> ]>
301 S<[ B<-aaliastext> ]>
302 S<[ B<-center> I<num> ]>
303 S<[ B<-color> I<num> ]>
304 S<[ B<-crop> I<code> ]>
305 S<[ B<-debug> ]>
306 S<[ B<-density> I<num>]>
307 S<[ B<-depth> I<num> ]>
308 S<[ B<-discard> ]>
309 S<[ B<-flip> I<code> ]>
310 S<[ B<-geometry> I<X>xI<Y> ]>
311 S<[ B<-interlaced> ]>
312 S<[ B<-margins> I<X>,I<Y> ]>
313 S<[ B<-multipage> ]>
314 S<[ B<-out> I<file> ]>
315 S<[ B<-quiet> ]>
316 S<[ B<-rightjustify> I<num> ]>
317 S<[ B<-scale> I<num> ]>
318 S<[ B<-tmp> I<path> ]>
319 S<[ B<-topjustify> [B<x>]I<num> ]>
320 S<[ B<-transparent> ]>
321 S<[ B<-type> I<type> ]>
322 S<[ B<-shoreup> I<num>[B<d>] ]>
323 S<[ B<-white> ]>
324 I<file>
325 S<[ I<file2> ... ]>
326
327 =cut
328
329 my %opt = ();
330 unless(&GetOptions(\%opt, qw(-help -version -debug -discard -antialias -aaliastext
331 -multipage -type=s -gif -png -out=s -depth=i -color=i -flip=s -density=i
332 -scale=f -geometry=s -margins=s -crop=s -transparent -interlaced
333 -rightjustify=i -center=i -topjustify=s -shoreup=s -tmp=s -white -quiet))) {
334 print_usage("$prompt: Error: Invalid option(s) specified.");
335 exit 1;
336 }
337
338 =head1 OPTIONS
339
340 The command line options may be abbreviated to the shortest unique
341 prefix.
342
343 =over 4
344
345 =item B<-help>
346
347 Show this help page and exit.
348
349 =cut
350
351 if($opt{help}) {
352 print_help();
353 exit 0;
354 }
355
356 =item B<-version>
357
358 Show the release and version of pstoimg and exit.
359
360 =cut
361
362 if($opt{version}) {
363 print_version();
364 exit 0;
365 }
366
367 banner() unless($opt{quiet});
368
369 =item B<-antialias>
370
371 Use Ghostscript's anti-aliasing feature for rendering "softer" images.
372 This applies to lines and edges of polygonal and oval or circular shapes.
373 Only valid if Ghostscipt 4.03 or higher is installed.
374
375 =item B<-aaliastext>
376
377 Use Ghostscript's anti-aliasing feature for "smoother" font characters,
378 without the jagged edges. Similar to B<-antialias> for graphic components.
379 Only valid if Ghostscipt 4.03 or higher is installed.
380
381 =item B<-center> I<num>
382
383 Add the appropriate amount of whitespace to the left of the image so
384 that the image appears to be centered in a total width of I<num> pixels.
385
386 =cut
387
388 my $CENTER = 0; # No centering by default
389 if($opt{center}) {
390 $CENTER = $opt{center};
391 die <<"EOF" unless ($CENTER =~ /^\d+$/ && $CENTER > 0);
392 $prompt: Error: Illegal width for -center specified: "$CENTER"
393 Value must be a positive integer.
394 EOF
395 }
396
397 =item B<-crop> I<code>
398
399 Crop the bitmap from the given directions. I<code> may be a string of
400 several cropping instructions, which are executed strictly in the given
401 order. Possible values are: B<h> (horizontal, i.e. crop top and
402 bottom), B<v> (vertical), B<tblr> (top, bottom, left, right) and B<a> (all
403 directions). A special case is B<s>: "shave" the image at the bottom, but only
404 if a single line of whitespace exists.
405
406 =cut
407
408 my $EXTRA_CROP = '';
409 if($opt{crop}) {
410 $EXTRA_CROP = lc($opt{crop});
411 die <<"EOF" unless ( $EXTRA_CROP =~ /^([vhtblras]+)$/i );
412 $prompt: Error: Illegal crop specified: "$EXTRA_CROP"
413 Crop must be h, v, t, b, l, r, a, s or combination
414 EOF
415 }
416
417 =item B<-debug>
418
419 Turn on debugging output. This can get rather verbose. Any intermediate
420 files generated are not removed to help debugging.
421
422 =cut
423
424 if($ENV{DEBUG}) {
425 $opt{debug} = 1;
426 }
427
428 =item B<-density> I<num>
429
430 The density (resolution) in DPI in which to render the bitmap. The default
431 is 72.
432
433 =cut
434
435 my $DENSITY = 72;
436 if($opt{density}) {
437 $DENSITY = $opt{density};
438 }
439 elsif($ENV{DENSITY}) {
440 $DENSITY = $ENV{DENSITY};
441 }
442 die <<"EOF" unless $DENSITY =~ /^\d+$/;
443 $prompt: Error: Illegal density specified: "$DENSITY"
444 Density must be an integer value. Default is 72.
445 EOF
446
447 =item B<-depth> I<num> or B<-color> I<num>
448
449 Specify the color depth of the bitmap. Legal values are 1 (black & white),
450 8 (256 colors) and 24 (true color).
451
452 =cut
453
454 unless($opt{depth}) {
455 if($opt{color}) {
456 $opt{depth} = $opt{color};
457 }
458 elsif($ENV{DEPTH}) {
459 $opt{depth} = $ENV{DEPTH};
460 }
461 else {
462 $opt{depth} = 8;
463 }
464 }
465 die <<"EOF" unless $opt{depth} =~ /^(1|8|24)$/;
466 $prompt: Error: Illegal color depth specified: "$opt{depth}"
467 Depth must be either 1, 8 or 24.
468 EOF
469
470 =item B<-discard>
471
472 Delete the input postscript file if the conversion was successful. Setting
473 the environment DISCARD to a true value (as perl sees it) has the same
474 effect.
475
476 =cut
477
478 if($ENV{DISCARD}) {
479 $opt{discard} = 1;
480 }
481
482 =item B<-flip> I<code>
483
484 Flip all generated output bitmaps. The following codes are recognized:
485 lr (flip left-right), tb (flip top-bottom), xy (flip bottom/left-top/right),
486 r90 and ccw (rotate by 90 degrees counterclockwise), r270 and cw (rotate
487 90 degrees clockwise) and r180 (rotate 180 degrees).
488
489 =cut
490
491 if($opt{flip}) {
492 $opt{flip} = lc($opt{flip});
493 die <<"EOF" unless $opt{flip} =~ /^(lr|tb|xy|r90|ccw|r270|cw|r180)$/;
494 $prompt: Error: Illegal flip option specified: "$opt{flip}"
495 Flip must be one of: lr tb xy r90 ccw r270 cw r180
496 EOF
497 }
498
499 =item B<-geometry> I<X>xI<Y>
500
501 Render only this "window" of the PostScript file. If given, this option
502 can dramatically reduce memory requirements and speed up conversion. The
503 geometry is automatically detected in case of EPS files (Encapsulated
504 PostScript).
505
506 =cut
507
508 my $GEOMETRY = '';
509 if($opt{geometry}) {
510 $GEOMETRY = $opt{geometry};
511 if($GEOMETRY =~ s/-//o ) {
512 $EXTRA_CROP .= 'bl';
513 }
514 die <<"EOF" unless ($GEOMETRY =~ /^\d+x\d+$/i);
515 $prompt: Error: Illegal geometry specified: "$GEOMETRY"
516 Geometry must be <width>x<height>
517 EOF
518 }
519
520 =item B<-interlaced>
521
522 Generate an interlaced bitmap. Interlaced images build up from coarse to
523 fine as they are loaded. This option may not work on every installation
524 and/or bitmap type, depending of the capabilities of external programs.
525
526 =cut
527
528 my $INTERLACE = 0; # Do not make interlaced images by default
529 if($opt{interlaced}) {
530 $INTERLACE=1;
531 }
532
533 =item B<-margins> I<X>,I<Y>
534
535 The offset of the rectangle in the postscript file that is going to be
536 rendered from top/left. Can be used together with B<-geometry> to further
537 reduce the size of the intermediate bitmap file generated by Ghostscript.
538
539 =cut
540
541 if($opt{margins}) {
542 die <<"EOF" unless (($opt{margins} =~ /^(\d+),(\d+)$/));
543 $prompt: Error: Illegal margins specified: "$opt{margins}"
544 Margins must be <hmargin>,<vmargin>
545 EOF
546 $PAGE_HMARGIN = $1;
547 $PAGE_VMARGIN = $2;
548 }
549
550 =item B<-multipage>
551
552 Process a multi-page PostScript file, i.e. create an individual bitmap
553 for every page. The resulting files are numbered: The decimal number
554 (starting with 1) is appended to the basename of the PostScript input
555 file (or the basename of the filename specified with B<-out>), while
556 keeping the extension.
557
558 =item B<-out> I<file>
559
560 The file where to write the bitmap. If multiple PostScript files are
561 supplied on the command line, this option is ignored. The bitmap type
562 extension is appended automatically if I<file> does not contain a dot.
563 In connection with B<-multipage> I<file> is extended by the page number
564 as shown in this example:
565
566 -outfile foo.gif --------E<gt> foo1.gif, foo2.gif, ...
567
568 =cut
569
570 if(!$opt{out} && $ENV{OUTFILE}) {
571 $opt{out} = $ENV{OUTFILE};
572 }
573
574 =item B<-quiet>
575
576 Do not print anything except error messages.
577
578 =item B<-rightjustify> I<num>
579
580 Add the appropriate amount of whitespace to the left of the image so that
581 it appears to be aligned to the right in a total width of I<num> pixels.
582
583 =cut
584
585 my $RIGHT_JUSTIFY = 0; # No right justifying by default
586 if($opt{rightjustify}) {
587 $RIGHT_JUSTIFY = $opt{rightjustify};
588 die <<"EOF" unless ($RIGHT_JUSTIFY =~ /^\d+$/ && $RIGHT_JUSTIFY > 0);
589 $prompt: Error: Illegal width for -rightjustify specified: "$RIGHT_JUSTIFY"
590 Value must be a positive integer.
591 EOF
592 }
593
594 =item B<-scale> I<factor>
595
596 Scale the image by I<factor>. Valid choices are any numbers greater than
597 zero. Useful choices are numbers between 0.1 - 5.
598 Large numbers may generate very large intermediate files and will take
599 longer to process. If this option is omitted, the environment SCALE is
600 considered.
601
602 =cut
603
604 unless($opt{scale}) {
605 if($ENV{SCALE}) {
606 $opt{scale} = $ENV{SCALE};
607 }
608 else {
609 $opt{scale} = 1;
610 }
611 }
612 die <<"EOF" unless ($opt{scale} =~ /^[\d.e]+$/i && $opt{scale} > 0);
613 $prompt: Error: Illegal scale specified: "$opt{scale}"
614 Scale must be nonnegative float value.
615 EOF
616
617 =item B<-shoreup> I<num>[B<d>]
618
619 Make height and width of the bitmap(s) an exact multiple of I<num>. If
620 I<num> is followed by a "d", then half the extra vertical space is placed
621 underneath. This option is useful, if you want to have "blown-up" images
622 of high quality for print, but downscale them in HTML using
623 C<E<lt>IMG WIDTH=x HEIGHT=yE<gt>>. If the actual image is is not an
624 integer multiple of x,y then browsers tend to display distorted images.
625
626 =cut
627
628 my $SHORE_UP = 0; # No pixel alignment by default
629 if($opt{shoreup}) {
630 $SHORE_UP = $opt{shoreup};
631 die <<"EOF" unless $SHORE_UP =~ /^\d+d?$/i;
632 $prompt: Error: Illegal shore-up specified: "$SHORE_UP"
633 Value must be a positive integer, optionally followed by d
634 EOF
635 }
636
637 =item B<-tmp> I<path>
638
639 Use I<path> to store temporary files. Defaults to /tmp on this
640 installation. This parameter can be set by the environment B<TMP> or
641 B<TEMP>, too.
642
643 =cut
644
645 my $TMP = '';
646 if($opt{tmp}) {
647 $opt{tmp} =~ s|\Q$dd\E+$||; # remove trailing directory separator(s)
648 if(-d $opt{tmp} && -r _ && -w _) {
649 $TMP = $opt{tmp};
650 } else {
651 print "$prompt: Warning: Cannot use $opt{tmp} as temporary directory.\n";
652 }
653 }
654 if(!$TMP && ($ENV{TMP} || $ENV{TEMP})) {
655 ($opt{tmp} = $ENV{TMP} || $ENV{TEMP}) =~ s|\Q$dd\E+$||;
656 if(-d $opt{tmp} && -r _ && -w _) {
657 $TMP = $opt{tmp};
658 } else {
659 print "$prompt: Warning: Cannot use $opt{tmp} as temporary directory.\n";
660 }
661 }
662 if(!$TMP && -d $def_tmp && -r _ && -w _) {
663 $TMP = $def_tmp;
664 }
665 print "$prompt: Temporary directory is $TMP\n" if($opt{debug});
666
667 =item B<-topjustify> [B<x>]I<num>
668
669 Add padding whitespace to the image so that it gets a defined height.
670 If an integer value is given, it defines the total height. The whitespace
671 is added at the bottom. If the number is preceded by "x", then this
672 multiple of the image height is added as whitespace at the bottom.
673
674 =cut
675
676 my $TOP_JUSTIFY = 0; # No top justifying by default
677 if($opt{topjustify}) {
678 $TOP_JUSTIFY = $opt{topjustify};
679 die <<"EOF" unless $TOP_JUSTIFY =~ /^x?\d+[.]?\d*$/i;
680 $prompt: Error: Illegal align specified: "$TOP_JUSTIFY"
681 Value must be positive numeric, optionally preceded by x
682 EOF
683 }
684
685 =item B<-transparent>
686
687 Generate transparent bitmaps, i.e. the background color (white) is
688 transparent if viewed with certain viewers (e.g. browsers). This option
689 may not be available due to missing capabilities of external
690 programs.
691
692 =cut
693
694 my $TRANSPARENT = 0; # Do not make make images transparent by default
695 if($opt{transparent}) {
696 $TRANSPARENT = 1;
697 }
698
699 =item B<-type> I<type>
700
701 Instruct pstoimg to render the bitmap in I<type> format. Depending on
702 the local installation, pstoimg is capable of generating either GIF or
703 PNG bitmaps. This site features the following types: png gif
704
705 If omitted, the first type in this list is taken.
706
707 =cut
708
709 if($opt{type}) {
710 $opt{type} = lc($opt{type});
711 die <<"EOF" unless grep($_ eq $opt{type},@IMAGE_TYPES);
712 $prompt: Error: This version of pstoimg does not support
713 "$opt{type}" image format.
714 EOF
715 }
716 else {
717 ($opt{type}) = @IMAGE_TYPES; # default image type
718 }
719 # Support -gif and -png for a transition period
720 if($opt{gif}) {
721 print qq{$prompt: Warning: The -gif switch is deprecated. Use "-type gif" instead.\n};
722 if(grep($_ eq 'gif',@IMAGE_TYPES)) {
723 $opt{type} = 'gif';
724 }
725 else {
726 die <<"EOF";
727 $prompt: Error: This version of pstoimg does not support "gif" format.
728 EOF
729 }
730 }
731 if($opt{png}) {
732 print qq{$prompt: Warning: The -png switch is deprecated. Use "-type png" instead.\n};
733 if(grep($_ eq 'png',@IMAGE_TYPES)) {
734 $opt{type} = 'png';
735 }
736 else {
737 die <<"EOF";
738 $prompt: Error: This version of pstoimg does not support "png" format.
739 EOF
740 }
741 }
742
743 =item B<-white>
744
745 Remove TeX's page color information from the PostScript file before
746 converting so that a white background is used.
747
748 =back
749
750 =cut
751
752 # do some consistency checks on the options
753
754
755 die <<"EOF" if($RIGHT_JUSTIFY && $CENTER);
756 $prompt: Error: Conflicting options -center and -rightjustify.
757 EOF
758
759 # now setup some parameters
760
761 # calculate dpi resolution from density and scale
762 $DENSITY = int($opt{scale} * $DENSITY + .5) if($opt{scale} != 1);
763
764 my $reduce_color = '';
765 if($opt{depth} == 1) {
766 $reduce_color = "$PPMQUANT 2";
767 }
768 elsif ($opt{depth} == 8) {
769 $reduce_color = "$PPMQUANT 256";
770 }
771
772 my $gs_aalias = '';
773 if($opt{antialias}) {
774 $GSDEVICE = $GSALIASDEVICE;
775 if($opt{depth} == 1) {
776 $gs_aalias = '-dTextAlphaBits=4 ';
777 $reduce_color = "$PPMQUANT -floyd 256";
778 }
779 else {
780 $gs_aalias = '-dTextAlphaBits=4 -dGraphicsAlphaBits=4 ';
781 }
782 }
783 elsif ($opt{aaliastext}) {
784 $GSDEVICE = $GSALIASDEVICE;
785 $gs_aalias = '-dTextAlphaBits=4 ';
786 $reduce_color = "$PPMQUANT -floyd 256";
787 }
788 my $PAPERSIZE = $ENV{PAPERSIZE} || '';
789 # This rx matches float values in Bounding Box expressions
790 my $Brx = '-?\d+(?:\.\d*|)';
791
792 ##############################################################################
793 # Main program
794
795 =head1 DESCRIPTION
796
797 B<pstoimg> iterates over the given input files and runs them through
798 Ghostscipt. The resulting pnm (portable anymap files) are processed
799 with different Netpbm tools (cropping, color mapping, aligning, ...)
800 and finally converted into (currently) either GIF or PNG format. The
801 bitmaps can now be included e.g. in WWW pages.
802
803 The PostScript file is converted as is. If a valid bounding box is
804 found (EPS format), then only this area is converted. The image is
805 I<not> cropped by default.
806
807 =cut
808
809 die "$prompt: Error: No input file(s) specified\n"
810 unless(@ARGV);
811
812 # suppress diagnostics messages if possible
813 my $NULLFILE = '/dev/null';
814 open(STDERR, ">$NULLFILE") unless($opt{debug});
815
816 my $exit = 0;
817
818 $opt{out} = '' if(@ARGV > 1); # disable -out if multiple ps files given
819
820 my $psfile;
821 foreach $psfile (@ARGV) {
822 unless (-f $psfile) {
823 print qq{$prompt: Error: Cannot find file "$psfile": $!\n};
824 exit 1;
825 }
826 $exit += (&pstoimg($psfile) ? 0 : 1);
827 }
828
829 =head1 RETURN VALUE
830
831 =over 4
832
833 =item 0
834
835 if everything went all right
836
837 =item x
838
839 (x != 0) something went wrong. See the message output.
840
841 =back
842
843 =cut
844
845 exit $exit ? 1 : 0;
846
847 ##############################################################################
848 # Subroutines
849
850 sub pstoimg {
851 my ($psfile) = @_;
852
853 print "$prompt: Processing $psfile\n" unless($opt{quiet});
854 # remove a trailing suffix the same way a shell would do it
855 my $base = $psfile;
856 $base =~ s|[.][^.$dd$dd]*$||;
857
858 my $outfile;
859 if($opt{out}) {
860 $outfile = $opt{out};
861 # append the type unless -outfile has a "." in it
862 $outfile .= ".$opt{type}" unless($outfile =~ /[.]/);
863 }
864 else {
865 $outfile = "$base.$opt{type}";
866 }
867
868 # Invoke Ghostscript
869 my $pnmdir = $TMP ? "$TMP$dd" : ".$dd";
870 my $pnmbase = "p$$"; # keep it short for dos
871 my $pnmfile = $pnmdir .
872 ($opt{multipage} ? "%d_${pnmbase}.pnm" : "$pnmbase.pnm");
873
874 ps2pnm($psfile,$pnmfile) || return 0;
875
876 my $ok = 1;
877 if (-f $pnmfile) {
878 if(crop_scale_etc($pnmfile, $outfile)) {
879 L2hos->Unlink($pnmfile) unless($opt{debug});
880 }
881 else {
882 return 0;
883 }
884 }
885 elsif($opt{multipage}) {
886 unless(opendir(DIR,$pnmdir)) {
887 print qq{$prompt: Error: Could not open directory "$pnmdir": $!\n};
888 return 0;
889 }
890 my @list = grep(/^\d+_\w*\./,readdir(DIR));
891 closedir(DIR);
892 if(@list) {
893 my $i;
894 foreach $i (@list) {
895 my ($n) = $i =~ /^(\d+)_/;
896 my $j = $outfile;
897 $j =~ s|(\.[^/.]*)$|$n$1|;
898 if(crop_scale_etc("$pnmdir$i", $j)) {
899 L2hos->Unlink("$pnmdir$i") unless($opt{debug});
900 }
901 else {
902 $ok = 0;
903 }
904 }
905 }
906 else {
907 goto not_found;
908 }
909 }
910 else {
911 not_found:
912 print "$prompt: Error: Couldn't find pnm output of $psfile\n";
913 }
914 L2hos->Unlink($psfile) if($opt{discard} && !$opt{debug});
915 $ok;
916 }
917
918 sub ps2pnm {
919 my ($psfile,$pnmfile) = @_;
920 my $gs_size = $PAPERSIZE ? "-sPAPERSIZE=$PAPERSIZE" : '';
921 my $gs_density = ($DENSITY != 72) ? "-r$DENSITY" : '';
922
923 my ($bbx, $bby, $bbw, $bbh) = (0,0,0,0);
924 my $max_lines = 100;
925 my ($epsf,$have_geometry) = (0,0);
926
927 # Parse postscript file for information
928 unless(open(PS, "<$psfile")) {
929 print qq{$prompt: Error: Cannot read "$psfile": $!};
930 return 0;
931 }
932 $_ = <PS>; # read one line
933 if( /^%!.*EPSF/ ) {
934 # we're in a EPSF file
935 $epsf = 1;
936 }
937 if($GEOMETRY || $epsf) {
938 while (defined ($_ = <PS>)) {
939 # Look for bounding box comment
940 # MRO: add support of precise bounding boxes
941 if ($epsf && (
942 /^%+(?:HiRes|Exact)BoundingBox:\s+($Brx)\s+($Brx)\s+($Brx)\s+($Brx)/o ||
943 /^\%\%BoundingBox:\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)/)) {
944 $bbx = 0 - $1; $bby = 0 - $2;
945 $bbw = $3 + $bbx; $bbh = $4 + $bby;
946 if(($bbw > 0) && ($bbh > 0)) { # we have a valid bounding box
947 print "$prompt: EPSF dimensions are ${bbw}x$bbh\n" if($opt{debug});
948 # this overrides the -geometry switch
949 if($DENSITY) { # scale the output
950 my $scale = $DENSITY / 72.0;
951 $bbw *= $scale;
952 $bbh *= $scale;
953 }
954 $bbw = int($bbw + 0.99);
955 $bbh = int($bbh + 0.99);
956 $GEOMETRY = "${bbw}x${bbh}";
957 $have_geometry = 1;
958 last;
959 }
960 }
961 # Look for page size information
962 elsif($GEOMETRY && /TeXDict\s+begin\s+(\d+)\s+(\d+)\s+/) {
963 $PAGE_WIDTH = int($1 / 65536 * 72 /72.27 +.5);
964 $PAGE_HEIGHT = int($2 / 65536 * 72 /72.27 +.5);
965 print "$prompt: Page dimensions are ${PAGE_WIDTH}x$PAGE_HEIGHT\n"
966 if($opt{debug});
967 # we don't have to look further for EPSF stuff at this point
968 last;
969 }
970 elsif(!$GEOMETRY && (/^\%\%EndComments/ || --$max_lines == 0)) {
971 # abort at a certain point to avoid scanning huge ps files
972 last;
973 }
974 }
975 }
976 close PS;
977 if($GEOMETRY && !$have_geometry) { # RRM: overrides $PAPERSIZE
978 # no geometry info found in the Postscript file
979 $bbx = $PAGE_HMARGIN;
980 $bby = $PAGE_HEIGHT - $PAGE_VMARGIN;
981 unless($GEOMETRY =~ /\s*(\d+)x(\d+)/i) {
982 print qq{$prompt: Illegal geometry "$GEOMETRY" specified.\n};
983 return 0;
984 }
985 $bbw = $1 + 10; # allow for the side-bars
986 $bbh = $2;
987 $bby = int(-$bby + $bbh + 8); # allow small margin for error
988 $bbx = int(-$bbx + 5); # allow small margin for error
989 if($DENSITY) {
990 my $scale = $DENSITY / 72.0;
991 $bbw = int($scale * $bbw + .99);
992 $bbh = int($scale * $bbh + .99);
993 }
994 $bbw += 10; # add a 5pt margin for safety
995 $bbh += 40; # add a 20pt margin for safety
996 $GEOMETRY = "${bbw}x$bbh";
997 $have_geometry = 1;
998 }
999 if($have_geometry) {
1000 $gs_size = "-g$GEOMETRY ";
1001 }
1002
1003 my $ps_changed = 0;
1004 if($have_geometry || $opt{white}) {
1005 # Remove any Postscript commands concerning Papersize if -g switch is used
1006 # thanks to Axel Ramge for identifying the problem and for this code
1007 local($/) = undef;
1008 open(PS,"<$psfile");
1009 my $ps = <PS>;
1010 close(PS);
1011 my $had_papersize;
1012 if($have_geometry) {
1013 $had_papersize = ($ps =~ s/\n%%BeginPaperSize.*?%%EndPaperSize[^\n]*\n/\n/sg);
1014 }
1015 my $had_nonwhite;
1016 if($opt{white}) {
1017 $had_nonwhite = ($ps =~ s/(\n\d+ \d+ bop gsave) \d*\.\d+ (TeXcolorgray clippath fill grestore)/$1 1 $2/s);
1018 }
1019 $ps_changed = $had_papersize || $had_nonwhite;
1020 if($ps_changed) {
1021 my $tmppsfile = $pnmfile; # was "tmpps$$.ps"
1022 $tmppsfile =~ s/\.[^.]*$/.ps/;
1023 unless(open(PS,">$tmppsfile") && (print PS $ps) && (close PS)) {
1024 if($had_papersize) {
1025 print <<"EOF";
1026 $prompt: Warning: Could not write "$tmppsfile": $!
1027 "$psfile" contains %%Papersize comments.
1028 Any of these should be removed else GS will fail.
1029 EOF
1030 }
1031 if($had_nonwhite) {
1032 print <<"EOF";
1033 $prompt: Warning: Could not write "$tmppsfile": $!
1034 "$psfile" has a non-white background.
1035 This may cause ugly images.
1036 EOF
1037 }
1038 }
1039 $psfile = $tmppsfile;
1040 print qq{Debug: Papersize comment in "$psfile" deleted.\n}
1041 if($had_papersize && $opt{debug});
1042 print qq{Debug: Background switched to white in "$psfile".\n}
1043 if($had_nonwhite && $opt{debug});
1044 }
1045 }
1046
1047 my $gs_quiet = $opt{debug} ? '' : '-q -dNOPAUSE -dNO_PAUSE';
1048 my $out_redirect = $opt{debug} ? '' : "> $NULLFILE";
1049 my $gs_out = "-sOutputFile=$pnmfile";
1050 my $gsfile = $psfile;
1051 # Ghostscript understands only '/' as path delimiter!
1052 if($opt{debug}) {
1053 print "$prompt: Running $GS $gs_quiet -sDEVICE=$GSDEVICE $gs_size $gs_density $gs_aalias $gs_out $out_redirect\n";
1054 print "GS>$bbx $bby translate\n" if($have_geometry);
1055 print "GS>($gsfile) run\n";
1056 print "GS>showpage\n" if ($epsf);
1057 print "GS>quit\n";
1058 }
1059 open (GS, "|$GS $gs_quiet -sDEVICE=$GSDEVICE $gs_size $gs_density $gs_aalias $gs_out $out_redirect");
1060 print GS "$bbx $bby translate\n" if ($have_geometry);
1061 print GS "($gsfile) run\n";
1062 print GS "showpage\n" if ($epsf);
1063 print GS "quit\n";
1064 print "\n" if($opt{debug});
1065 unless(close(GS)) {
1066 print "$prompt: Error: Ghostscript returned error status ",$?>>8,"\n";
1067 }
1068 L2hos->Unlink($psfile) if($ps_changed && !$opt{debug});
1069 1;
1070 }
1071
1072
1073 # This sub post-processes the PNM images that come out of Ghostscript.
1074 # The image is cropped, flipped and finally converted to PNG or GIF.
1075
1076 sub crop_scale_etc {
1077 my ($in, $out) = @_;
1078
1079 # create temp filename; should be auto-incrementable
1080 my $tmp = $in;
1081 $tmp =~ s/(\.[^.]*)?$/.t00/;
1082 # save the original Ghostscript result
1083 if($opt{debug}) {
1084 L2hos->Copy($in,$tmp);
1085 &increment_name($tmp);
1086 }
1087 my ($cmd,$type,$width,$height,$just);
1088
1089 my $must_align = 0;
1090 #$EXTRA_CROP = "a$EXTRA_CROP" # hack to ensure first all-over cropping
1091 # unless($EXTRA_CROP =~ /^a/i);
1092
1093 # RRM: Remove justification bars
1094 $EXTRA_CROP =~ s/h/bt/gi; # crop horizontally
1095 $EXTRA_CROP =~ s/v/rl/gi; # crop vertically
1096 #
1097 #MN HACKED HERE
1098 while ($EXTRA_CROP =~ /([atblrs])/gi) {
1099 my $edge = $1;
1100 my $croparg = '';
1101 if($edge =~ /b/i) {
1102 $croparg = "-bot";
1103 } elsif($edge =~ /[tlr]/i) {
1104 $croparg = "$PNMBLACK ";
1105 } elsif($edge =~ /s/i) {
1106 #RRM: shave at most 1-2 rows of white from the bottom
1107 if($cmd) {
1108 # Terminate command pipe
1109 &do_cmd($in,$tmp,$cmd) || return 0; # failure
1110 $cmd = '';
1111 }
1112 my ($type,$width,$height) = get_image_geometry($in);
1113 next unless($type); # skip if no geometry
1114 if(&do_cmd_norename("$PNMCROP -bot < $in",$tmp)) {
1115 my ($type,$width,$height2) = get_image_geometry($tmp);
1116 if($type && ($height - $height2) < 3 ) {
1117 # command succeeded and shaved less than 3 rows
1118 if($opt{debug}) {
1119 L2hos->Copy($tmp,$in);
1120 &increment_name($tmp);
1121 } else {
1122 L2hos->Rename($tmp,$in);
1123 }
1124 next;
1125 }
1126 }
1127 # MRO: this shouldn't be necessary: L2hos->Unlink($tmp);
1128 next; # go to next crop argument
1129 } # end switch on crop codes
1130 if($cmd) {
1131 # Continue command pipe
1132 $cmd .= "| $PNMCROP $croparg";
1133 } else {
1134 # start new pipe
1135 $cmd = "$PNMCROP $croparg< $in ";
1136 }
1137 } # end cropping
1138
1139
1140 if($opt{flip}) {
1141 unless($cmd) {
1142 $cmd = "$PNMFLIP -$opt{flip} < $in";
1143 } else {
1144 $cmd .= "| $PNMFLIP -$opt{flip} ";
1145 }
1146 }
1147
1148 if($RIGHT_JUSTIFY || $TOP_JUSTIFY || $CENTER || $SHORE_UP) {
1149 if($cmd) {
1150 # empty command pipe, we need the image's geometry
1151 &do_cmd($in,$tmp,$cmd);
1152 $cmd='';
1153 }
1154
1155 # Get bitmap type and dimensions
1156 ($type,$width,$height) = &get_image_geometry($in);
1157 return 0 unless($type);
1158
1159 my ($white_left,$white_right,$white_top,$white_bottom) = (0,0,0,0);
1160
1161 if($RIGHT_JUSTIFY || $CENTER) {
1162 if($RIGHT_JUSTIFY) {
1163 $white_left = int($RIGHT_JUSTIFY-$width);
1164 } else { # CENTER
1165 $white_left = int(($CENTER-$width) / 2);
1166 }
1167 $white_left = 0 unless($white_left > 0);
1168
1169 $width += $white_left;
1170 }
1171
1172 if($TOP_JUSTIFY) {
1173 if($TOP_JUSTIFY =~ /^x([0-9.]+)/io) {
1174 $white_bottom = $1 * $height;
1175 } else {
1176 $white_bottom = $TOP_JUSTIFY - $height;
1177 }
1178 if($white_bottom > 0) {
1179 $white_bottom = int($white_bottom + 0.99); # round up
1180 $height += $white_bottom;
1181 } else {
1182 $white_bottom = 0;
1183 }
1184 }
1185
1186 if($SHORE_UP =~ /(\d+)(d?)/ && $1) {
1187 # RRM: make height and width an exact multiple of $SHORE_UP
1188 my ($shoreup,$depth) = ($1,$2);
1189 my $extra = $height % $shoreup;
1190 if($depth) { # image needs depth, place half the extra space underneath
1191 my $bextra = int($extra/2);
1192 $white_bottom += $bextra;
1193 $white_top += $extra - $bextra;
1194 } else {
1195 $white_top += $extra;
1196 }
1197
1198 $extra = $width % $shoreup;
1199 my $rextra = int($extra/2);
1200 $white_right += $rextra;
1201 $white_left += $extra - $rextra;
1202 $cmd = '';
1203 }
1204
1205
1206 if($white_left) {
1207 if($cmd) {
1208 &do_cmd($in,$tmp,$cmd) || return 0;
1209 }
1210 # Start new command pipe
1211 $cmd = "$PBMMAKE -white $white_left 1 | $PNMCAT -white -lr - $in ";
1212 }
1213
1214 if($white_right) {
1215 if($cmd) {
1216 &do_cmd($in,$tmp,$cmd) || return 0;
1217 }
1218 # Start new command pipe
1219 $cmd = "$PBMMAKE -white $white_right 1 | $PNMCAT -white -lr $in - ";
1220 }
1221
1222 if($white_top) {
1223 if($cmd) {
1224 &do_cmd($in,$tmp,$cmd) || return 0;
1225 }
1226 # Start new command pipe
1227 $cmd = "$PBMMAKE -white 1 $white_top | $PNMCAT -white -tb - $in ";
1228 }
1229
1230 if($white_bottom) {
1231 if($cmd) {
1232 &do_cmd($in,$tmp,$cmd) || return 0;
1233 }
1234 # Start new command pipe
1235 $cmd = "$PBMMAKE -white 1 $white_bottom | $PNMCAT -white -tb $in - ";
1236 }
1237 } # endif must_align
1238
1239 my $pnmtoimg;
1240 if($opt{type} eq 'gif') {
1241 $pnmtoimg = $PPMTOGIF;
1242 if($INTERLACE) {
1243 $pnmtoimg .= ' -interlace';
1244 }
1245 if($TRANSPARENT) {
1246 $pnmtoimg .= ' -trans ' . L2hos->quote($trans_color);
1247 }
1248 }
1249 if($opt{type} eq 'png') {
1250 $pnmtoimg = $PNMTOPNG;
1251 if($INTERLACE) {
1252 $pnmtoimg .= ' -interlace';
1253 }
1254 if($TRANSPARENT) {
1255 $pnmtoimg .= ' -trans ' . L2hos->quote($trans_color);
1256 }
1257 }
1258 unless($pnmtoimg) {
1259 print qq($prompt: Error: unknown image type "$opt{type}".\n);
1260 exit 2;
1261 }
1262
1263 unless($type) {
1264 ($type,$width,$height) = &get_image_geometry($in);
1265 return 0 unless($type);
1266 }
1267 # run ppmquant only on color/gray images
1268 if(!$type || $type =~ /(ppm|pgm)/i) {
1269 if($cmd) {
1270 $cmd .= "| $reduce_color "
1271 } else {
1272 $cmd = "$reduce_color < $in ";
1273 }
1274 }
1275
1276 if($cmd) {
1277 $cmd .= "| $pnmtoimg "
1278 } else {
1279 $cmd = "$pnmtoimg < $in ";
1280 }
1281 &do_cmd_norename($cmd,$out) || return 0;
1282 print qq{$prompt: Written $out\n} unless($opt{quiet});
1283
1284 1;
1285 }
1286
1287 sub banner {
1288 print "$prompt V$RELEASE (Revision $VERSION, Perl $])\n";
1289 }
1290
1291 sub print_version {
1292 my $formats = join(',',@IMAGE_TYPES);
1293 print <<"EOM";
1294 $prompt (Revision $VERSION, perl $]),
1295 part of LaTeX2HTML Release V$RELEASE.
1296
1297 Supported output image format(s): $formats
1298 EOM
1299 1;
1300 }
1301
1302 sub print_help {
1303 L2hos->perldoc($SCRIPT);
1304 1;
1305 }
1306
1307 sub print_usage {
1308 my $start = 0;
1309 my $usage = 'Usage: ';
1310 my $indent = '';
1311
1312 print (@_, "\n") if @_;
1313
1314 my $perldoc = '/usr/bin'.$dd."perldoc";
1315 my $script = $SCRIPT || $0;
1316 open(PIPE, "$perldoc -t $script |")
1317 || die "Fatal: can't open pipe: $!";
1318 while (<PIPE>)
1319 {
1320 if (/^\s*$/) {
1321 next;
1322 } elsif (/^SYNOPSIS/) {
1323 $start = 1;
1324 } elsif (/^\w/) {
1325 $start = 0;
1326 } elsif ($start == 1) {
1327 ($indent) = /^(\s*)/;
1328 s/^$indent/$usage/;
1329 $usage =~ s/./ /g;
1330 $start = 2;
1331 print $_;
1332 } elsif ($start == 2) {
1333 s/^$indent/$usage/;
1334 print $_;
1335 }
1336 }
1337 close PIPE;
1338 1;
1339 }
1340
1341 sub do_cmd {
1342 my ($in,$tmp,$cmd) = @_;
1343
1344 print qq{Running "$cmd > $tmp"\n} if($opt{debug});
1345 my $stat = system("$cmd > $tmp");
1346 if($stat) { # error
1347 print qq{$prompt: Error: "$cmd > $tmp" failed: $!\n};
1348 return 0; # failure
1349 }
1350 elsif(!-s $tmp) { # does not exist or zero size
1351 print qq{$prompt: Error: "$cmd > $tmp" produced empty file\n};
1352 L2hos->Unlink($tmp) if(-e $tmp);
1353 return 0; # failure
1354 }
1355 if($opt{debug}) {
1356 # increase the temporary filename by 1
1357 # this uses perl's magic autoincrement
1358 &increment_name($_[1]);
1359 return L2hos->Copy($tmp,$in);
1360 } elsif(!L2hos->Rename($tmp,$in)) {
1361 print qq{$prompt: Error: rename of "$tmp" to "$in" failed: $!\n};
1362 return 0; # failure
1363 }
1364 1;
1365 }
1366
1367 sub do_cmd_norename {
1368 my ($cmd,$out) = @_;
1369
1370 print qq{Running "$cmd > $out"\n} if($opt{debug});
1371 my $stat = system("$cmd > $out");
1372 if($stat) { # error
1373 print qq{$prompt: Error: "$cmd > $out" failed: $!\n};
1374 return 0; # failure
1375 }
1376 elsif(!-s $out) { # does not exist or zero size
1377 print qq{$prompt: Error: "$cmd > $out" produced empty file\n};
1378 L2hos->Unlink($out) if(-e $out);
1379 return 0; # failure
1380 }
1381 1;
1382 }
1383
1384 sub do_cmd_plain {
1385 my ($cmd) = @_;
1386
1387 print qq{Running "$cmd"\n} if($opt{debug});
1388 my $stat = system($cmd);
1389 if($stat) { # error
1390 print qq{$prompt: Error: "$cmd" failed: $!\n};
1391 return 0; # failure
1392 }
1393 1;
1394 }
1395
1396 sub get_image_geometry {
1397 my ($pnmfile) = @_;
1398
1399 my ($type,$width,$height);
1400 my $out = `$PNMFILE $pnmfile`;
1401 if($? || $out =~ /(P[BGP]M)[^0-9]*(\d+)\s*by\s*(\d+)/i) {
1402 $type = $1;
1403 $width = $2;
1404 $height = $3;
1405 print qq{Image "$pnmfile" is $type, ${width}x$height\n} if($opt{debug});
1406 } else {
1407 print "$prompt: Error: Could not determine image size: $out\n";
1408 return undef;
1409 }
1410 ($type,$width,$height);
1411 }
1412
1413 # push the number in the suffix up one notch
1414 sub increment_name {
1415 $_[0] =~ s/(\d+)$/$a=$1;++$a/e;
1416 }
1417
1418 __DATA__
1419
1420 =head1 EXAMPLES
1421
1422 =over 4
1423
1424 =item C<pstoimg foo.ps>
1425
1426 Convert the first page of foo.ps to the default bitmap type.
1427
1428 =item C<pstoimg -type png -crop a -trans -interlace foo.ps>
1429
1430 Same as above, but force png output and crop all the whitespace
1431 around the image and make the color white transparent and
1432 generate an interlaced bitmap.
1433
1434 =item C<pstoimg -multi -out bar -type gif -crop a foo.ps>
1435
1436 Consider foo.ps a multiple page PostScript file and create output
1437 files bar1.gif, bar2.gif, etc.
1438
1439 =back
1440
1441 =head1 ENVIRONMENT
1442
1443 =over 4
1444
1445 =item DENSITY, DEPTH, DEBUG, DISCARD
1446
1447 See B<-density>, B<-depth>, B<-debug>, B<-discard>, respectively.
1448
1449 =item GS_LIB
1450
1451 This variable is set to the path(s) where Ghostscript libraries have
1452 been found on this system during configuration, but only if the built-in
1453 paths are not correct. This fixes the problem of relocation that is quite
1454 common on Win32 installations. This behavior can be overridden by
1455 setting GS_LIB manually before starting pstoimg.
1456
1457 =item LATEX2HTMLDIR
1458
1459 The directory where the LaTeX2HTML library and perl modules are found.
1460 Defaults to "/usr/local/lib/latex2html" on this installation.
1461
1462 =item OUTFILE
1463
1464 Setting this has the same effect as specifying B<-out>. Please do not rely
1465 on this feature any more, it will disappear from the next releases!
1466
1467 =item PAPERSIZE
1468
1469 The papersize to use by Ghostscript to render the image. pstoimg tries
1470 hard to optimize for rendering on the smallest possible bitmap size.
1471 Still this option is there to enable tuning by hand, although it is
1472 deprecated. If pstoimg finds a better setting, this parameter is ignored.
1473
1474 =item SCALE
1475
1476 See the discussion of B<-scale>.
1477
1478 =item TMP and TEMP
1479
1480 Unless overridden by B<-tmp>, these variables denote a directory where
1481 to store temporary files. TMP is considered first, then TEMP.
1482
1483 =back
1484
1485 =head1 SEE ALSO
1486
1487 gs, pnmcrop, pnmquant, pbmmake, pnmcat, pnmfile, pnmflip, ppmtogif,
1488 pnmtopng, giftool, giftrans.
1489
1490 =head1 NOTES
1491
1492 Several people have suggested to use ImageMagick's convert instead of
1493 pstoimg. A few comments on this: convert uses (of course) Ghostscript
1494 for conversion of PostScript to bitmap, so one still needs gs. And
1495 for the special requirements of LaTeX2HTML convert's features are not
1496 sufficient. The ImageMagick toolset has everything in place, but it
1497 has some overhead that can prove killing when processing some 100
1498 images. pstoimg only does what it really has to, so it should be
1499 quite efficient. Don't get me wrong - I like ImageMagick, but not in
1500 the context of LaTeX2HTML.
1501
1502 =head1 CAVEATS
1503
1504 This utility is automatically configured and built to work on the
1505 local setup. If this setup changes (e.g. some of the external commands
1506 are moved), the script has be be reconfigured.
1507
1508 Despite the portability of perl, a pstoimg configured on UNIX will
1509 probably not work on Win32 and vice versa.
1510
1511 =head1 BUGS
1512
1513 This is a major enhancement release, so there may be a few bugs. As
1514 the user inteface changed a bit, some of your tools that were using
1515 pstoimg may not work any more.
1516
1517 Please report bugs to [email protected], stating the (debug) output
1518 of pstoimg, your perl version and the versions of the external tools.
1519 Best is to include the cfgcache.pm file from the configuration procedure.
1520
1521 =head1 AUTHOR
1522
1523 Marek Rouchal E<lt>[email protected]<gt>
1524
1525 =head1 HISTORY
1526
1527 This script went through a long evolution, beginning with a modification
1528 of Doug Crabill's E<lt>[email protected]<gt> ps2epsi script.
1529 The first perl version was done by Nikos Drakos <[email protected]>.
1530 It was gradually improved by numerous LaTeX2HTML developers:
1531 Ross Moore <[email protected]>, Jens Lippmann
1532 <[email protected]> and others (sorry for not
1533 mentioning everyone and thanks for your contributions).
1534
1535 =cut
1536