Tag physically/logically connected items differently in the "Find" action
[geda-pcb/pcjc2.git] / doc / eps2png
blob893b7a3341b055220be9bc864b33ceb98927d2ba
1 #!/usr/bin/perl
3 # Author : Johan Vromans
4 # Created On : Tue Sep 15 15:59:04 1992
5 # Last Modified By: Johan Vromans
6 # Last Modified On: Sun Jun 24 17:07:29 2001
7 # Update Count : 155
8 # Status : Okay
10 ################ Common stuff ################
12 use strict;
13 use Getopt::Long 2.1;
15 ################ Program parameters ################
17 ### CONFIG
18 # Some GhostScript programs can produce GIF directly.
19 # If not, we need the PBM package for the conversion.
20 my $use_pbm = 1; # GhostScript can not produce GIF
21 ### END CONFIG
23 my $res = 82; # default resolution
24 my $scale = 1; # default scaling
25 my $mono = 0; # produce BW images if non-zero
26 my $format; # output format
27 my $gs_format; # GS output type
28 my $output; # output, defaults to STDOUT
29 my $antialias = 4; # antialiasing
30 my $width; # desired widht
31 my $height; # desired height
33 my ($verbose,$trace,$test,$debug) = (0,0,0,0);
34 handle_options ();
35 unless ( defined $format ) {
36 if ( $0 =~ /2(gif|jpg|png)$/ ) {
37 set_out_type ($1);
39 else {
40 set_out_type ('png') unless defined $format;
43 print STDERR ("Producing $format ($gs_format) image.\n") if $verbose;
45 $trace |= $test | $debug;
46 $verbose |= $trace;
48 ################ Presets ################
50 ################ The Process ################
52 my $eps_file;
53 my $err = 0;
55 foreach $eps_file ( @ARGV ) {
57 unless ( open (EPS, $eps_file) ) {
58 print STDERR ("Cannot open $eps_file [$!], skipped\n");
59 $err++;
60 next;
63 my $line = <EPS>;
64 unless ( $line =~ /^%!PS-Adobe.*EPSF-/ ) {
65 print STDERR ("Not EPS file: $eps_file, skipped\n");
66 $err++;
67 next;
70 my $ps = ""; # PostScript input data
71 my $xscale;
72 my $yscale;
74 while ( $line = <EPS> ) {
76 # Search for BoundingBox.
77 if ( $line =~ /^%%BoundingBox:\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/i ) {
79 print STDERR ("$eps_file: x0=$1, y0=$2, w=", $3-$1, ", h=", $4-$2)
80 if $verbose;
82 if ( defined $width ) {
83 $res = 72;
84 $xscale = $width / ($3 - $1);
85 if ( defined $height ) {
86 $yscale = $height / ($4 - $2);
88 else {
89 $yscale = $xscale;
90 $height = ($4 - $2) * $yscale;
93 elsif ( defined $height ) {
94 $res = 72;
95 $yscale = $height / ($4 - $2);
96 if ( defined $width ) {
97 $xscale = $width / ($3 - $1);
99 else {
100 $xscale = $yscale;
101 $width = ($3 - $1) * $xscale;
104 unless ( defined $xscale ) {
105 $xscale = $yscale = $scale;
106 # Calculate actual width.
107 $width = $3 - $1;
108 $height = $4 - $2;
109 # Normal PostScript resolution is 72.
110 $width *= $res/72 * $xscale;
111 $height *= $res/72 * $yscale;
112 # Round up.
113 $width = int ($width + 0.5) + 1;
114 $height = int ($height + 0.5) + 1;
116 print STDERR (", width=$width, height=$height\n") if $verbose;
118 # Scale.
119 $ps .= "$xscale $yscale scale\n"
120 if $xscale != 1 || $yscale != 1;
122 # Create PostScript code to translate coordinates.
123 $ps .= (0-$1) . " " . (0-$2) . " translate\n"
124 unless $1 == 0 && $2 == 0;
126 # Include the image, show and quit.
127 $ps .= "($eps_file) run\n".
128 "showpage\n".
129 "quit\n";
131 last;
133 elsif ( $line =~ /^%%EndComments/i ) {
134 print STDERR ("No bounding box in $eps_file\n");
135 $err++;
136 last;
139 close (EPS);
141 my $out_file; # output file
142 my $pbm_file; # temporary file for PBM conversion
144 # Note the temporary PBM file is created where the output file is
145 # located, since that will guarantee accessibility (and a valid
146 # filename).
147 if ( defined $output ) {
148 $out_file = $output;
149 $pbm_file = $output.".ppm";
151 elsif ( $eps_file =~ /^(.+).epsf?$/i ) {
152 $out_file = "$1.$format";
153 $pbm_file = $1.".ppm";
155 else {
156 $out_file = $eps_file . ".$format";
157 $pbm_file = $eps_file . ".ppm";
159 print STDERR ("Creating $out_file\n") if $verbose;
161 my $gs0 = "gs -q -dNOPAUSE -r$res -g${width}x$height";
162 my $gs1 = "-";
163 $gs0 .= " -dTextAlphaBits=$antialias -dGraphicsAlphaBits=$antialias"
164 if $antialias;
165 if ( $format eq 'png' ) {
166 mysystem ("$gs0 -sDEVICE=". ($mono ? "pngmono" : $gs_format).
167 " -sOutputFile=$out_file $gs1", $ps);
169 elsif ( $format eq 'jpg' ) {
170 mysystem ("$gs0 -sDEVICE=". ($mono ? "jpeggray" : $gs_format).
171 " -sOutputFile=$out_file $gs1", $ps);
173 elsif ( $format eq 'gif' ) {
174 if ( $use_pbm ) {
175 # Convert to PPM and use some of the PBM converters.
176 mysystem ("$gs0 -sDEVICE=". ($mono ? "pbm" : "ppm").
177 " -sOutputFile=$pbm_file $gs1", $ps);
178 # mysystem ("pnmcrop $pbm_file | ppmtogif > $out_file");
179 mysystem ("ppmtogif $pbm_file > $out_file");
180 unlink ($pbm_file);
182 else {
183 # GhostScript has GIF drivers built-in.
184 mysystem ("$gs0 -sDEVICE=". ($mono ? "gifmono" : "gif8").
185 " -sOutputFile=$out_file $gs1", $ps);
188 else {
189 print STDERR ("ASSERT ERROR: Unhandled output type: $format\n");
190 exit (1);
193 unless ( -s $out_file ) {
194 print STDERR ("Problem creating $out_file for $eps_file\n");
195 $err++;
200 exit 1 if $err;
202 ################ Subroutines ################
204 sub mysystem {
205 my ($cmd, $data) = @_;
206 print STDERR ("+ $cmd\n") if $trace;
207 if ( $data ) {
208 if ( $trace ) {
209 my $dp = ">> " . $data;
210 $dp =~ s/\n(.)/\n>> $1/g;
211 print STDERR ("$dp");
213 open (CMD, "|$cmd") or die ("cmd: $!\n");
214 print CMD $data;
215 close CMD or die ("cmd close: $!\n");
217 else {
218 system ($cmd);
222 sub set_out_type {
223 my ($opt) = lc (shift (@_));
224 if ( $opt =~ /^png(mono|gray|16|256|16m)?$/ ) {
225 $format = 'png';
226 $gs_format = $format.(defined $1 ? $1 : '16m');
228 elsif ( $opt =~ /^gif(mono)?$/ ) {
229 $format = 'gif';
230 $gs_format = $format.(defined $1 ? $1 : '');
232 elsif ( $opt =~ /^(jpg|jpeg)(gray)?$/ ) {
233 $format = 'jpg';
234 $gs_format = 'jpeg'.(defined $2 ? $2 : '');
236 else {
237 print STDERR ("ASSERT ERROR: Invalid value to set_out_type: $opt\n");
238 exit (1);
242 sub handle_options {
243 my ($help) = 0; # handled locally
245 # Process options.
246 if ( @ARGV > 0 && $ARGV[0] =~ /^[-+]/ ) {
247 usage ()
248 unless GetOptions ('verbose' => \$verbose,
249 'antialias|aa=i' => \$antialias,
250 'noantialias|noaa' => sub { $antialias = 0 },
251 'scale=f' => \$scale,
252 'width=i' => \$width,
253 'height=i' => \$height,
254 'output=s' => \$output,
255 'png' => \&set_out_type,
256 'pngmono' => \&set_out_type,
257 'pnggray' => \&set_out_type,
258 'png16' => \&set_out_type,
259 'png256' => \&set_out_type,
260 'png16m' => \&set_out_type,
261 'jpg' => \&set_out_type,
262 'jpggray' => \&set_out_type,
263 'jpeg' => \&set_out_type,
264 'jpeggray' => \&set_out_type,
265 'gif' => \&set_out_type,
266 'gifmono' => \&set_out_type,
267 'mono!' => \$mono,
268 'resolution=i' => \$res,
269 'pbm!' => \$use_pbm,
270 'trace' => \$trace,
271 'help' => \$help,
272 'debug' => \$debug)
273 && !$help;
275 die ("Only one file argument is allowed when -output is used\n")
276 if @ARGV > 1 && defined $output;
277 die ("At least one input file name must be specified\n")
278 unless @ARGV;
279 die ("Antialias value must be 0, 1, 2, 4, or 8\n")
280 unless "$antialias" =~ /^[01248]$/;
283 sub usage {
284 print STDERR <<EndOfUsage;
285 Usage: $0 [options] file [...]
287 -png -pngmono -pnggray -png16 -png256 -png16m
288 produce PNG image
289 -jpg -jpggray -jpeg -jpeggray
290 produce JPG image
291 -gif -gifmono produce GIF image
292 -[no]mono monochrome/colour rendition
293 -width XXX desired with
294 -height XXX desired height
295 -resolution XXX resolution (default = $res)
296 -scale XXX scaling factor
297 -antialias XX antialias factor (must be 0, 1, 2, 4 or 8; default: 4)
298 -noantialias no antialiasing (same as -antialias 0)
299 -[no]pbm GIF only: [do not] convert via pbm format
300 -output XXX output to this file (only one input file)
301 -help this message
302 -verbose verbose information
303 EndOfUsage
304 exit 1;
307 # For install testing
310 __END__
312 =pod
314 =head1 NAME
316 epf2png - convert EPS files to PNG, JPG or GIF images
318 =head1 SYNOPSIS
320 eps2png [ options ] files ...
321 eps2gif [ options ] files ...
322 eps2jpg [ options ] files ...
324 =head1 DESCRIPTION
326 Converts files from EPS format (Encapsulated PostScript) to some
327 popular image formats.
329 If installed as C<eps2png> (the default), it produces PNG images by
330 default. Likewise, C<eps2gif> defaults to GIF images and C<eps2jpg>
331 defaults to JPG. Note that the normal installation procedure will
332 I<only> install C<eps2png>.
334 It uses GhostScript to produce the images. Since modern GhostScript
335 programs do not support GIF anymore, GIF images are produced via the
336 Portable PixMap converters (PBM-package). In this case, a temporary
337 file is created, named after the output file, with the extension
338 replaced by ".ppm". It is deleted upon completion.
340 =head1 ARGUMENTS
342 B<eps2png> always requires at least one argument: the name of the EPS
343 file to be converted. It is possible to specify more than one file
344 name. This will cause all named files to be converted into separate
345 files, e.g., "C<sample.eps>" will be converted to "C<sample.png>" and
346 so on.
348 =over 4
350 =item B<-png -pngmono -pnggray -png16 -png256 -png16m>
352 Each of these options will instruct Ghostscript to use the
353 corresponding bitmap generator, and supply a C<.png> default
354 extension for output files.
356 =item B<-jpg -jpggray -jpeg -jpeggray>
358 Same, but with a C<.jpg> default extension for output files.
360 =item B<-gif -gifmono>
362 Same, but with a C<.gif> default extension for output files.
364 Note: Since modern Ghostscript versions no longer support the GIF
365 format due to copyright restrictions, B<eps2png> will request
366 Ghostscript to produce a Portable Bitmap File (.ppm or .pbm) instead
367 and run the B<ppmtogif> converter to produce the actual GIF file.
369 =item B<-mono>
371 This option will select monochrome (BW or gray) output. It forces the
372 Ghostscript driver to C<pngmono>, C<jpeggray>, C<pbm>, or C<gifmono>.
374 =item B<-nomono>
376 Produces colour images. This is the default.
378 =item B<-width> I<NN>
380 The desired width of the output image.
382 If B<-height> is not specified, the image will be scaled proportionally.
384 =item B<-height> I<NN>
386 The desired height of the output image.
388 If B<-width> is not specified, the image will be scaled proportionally.
390 =item B<-resolution> I<NN>
392 Specifies the resolution for the output image. This is the width, in
393 pixels, of the bitmap image for an EPS image of one inch wide (72
394 PostScript points).
396 Note that for best results, use the B<-width> and B<-height> options
397 instead.
399 Default value is 82, which causes the converted image to be of more
400 or less the same size as the EPS image. On my screen, that is.
402 =item B<-scale> I<NN>
404 Specify a scaling factor. This may be a fractional number.
406 For a one-inch EPS image, the resultant bitmap image will be
407 I<scale> times I<resolution>.
409 Note that for best results, use the B<-width> and B<-height> options
410 instead.
412 =item B<-antialias> I<NN>
414 Sets the antialiasing depth. I<NN> must be 0 (no antialiasing), 1, 2,
415 4, or 8. Default value is 4.
417 =item B<-noantialias>
419 Sets the antialiasing depth to 0.
421 =item B<-pbm>
423 Forces GIF conversion through the PBM converters.
425 =item B<-nopbm>
427 Forces GIF conversion through Ghostscript.
429 =item B<-output> I<XXX>
431 Stores the output in this file. Only one input file may be supplied if
432 this option is specified.
434 =item B<-help>
436 Prints a help message and exits.
438 =item B<-verbose>
440 Provides more verbose information.
442 =back
444 =head1 AUTHOR
446 Johan Vromans, <jvromans@squirrel.nl>.
448 =head1 BUGS
450 GhostScript and, if required, the PBM package, need to be installed and
451 accessible through the user's C<PATH>.
453 GhostScript is assumed to be capable of handling all the image types
454 listed above.
456 The EPS should be well-behaving.
458 =head1 COPYRIGHT AND DISCLAIMER
460 This program is Copyright 1994,2001 by Johan Vromans.
461 This program is free software; you can redistribute it and/or
462 modify it under the terms of the Perl Artistic License or the
463 GNU General Public License as published by the Free Software
464 Foundation; either version 2 of the License, or (at your option) any
465 later version.
467 This program is distributed in the hope that it will be useful,
468 but WITHOUT ANY WARRANTY; without even the implied warranty of
469 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
470 GNU General Public License for more details.
472 If you do not have a copy of the GNU General Public License write to
473 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
474 MA 02139, USA.
476 =cut