#!/usr/bin/perl
#
# v1.1
# comments, big reorts, etc: <dcm@redhat.com>

# convert image to Encapsulated Postscript, with possible scaling of width
# Read png2eps man page for more info

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

$width = "";

if ($ARGV[0] =~ /^--width=(.+)$/) {
       $width = $1;
       shift (@ARGV);
}

#print "width =",$width,"\n";

#print "argv =",$ARGV[0],"\n";

$infile = $ARGV[0];

if (!(-e $infile)) {
print "File doesn't exist\n";
exit 1;
}

$imagesz = `identify $ARGV[0]`;

($name, $size, $rest) =  split / /, $imagesz, 3;
#print $name, " ", $size, " ", $rest, "\n";
#print "imagesz = ",$size,"\n";

($x, $y) = split /x/, $size, 2;
($y, $rest) = split /\+/, $y, 2;

#print $x," by ",$y, "\n";

#
# if width set, we need to scale
#

if ($width != "") {
  $scale = $width * (72.0/$x);
} else {
  $scale = 1;
}

$scale = 72.0/$scale;
#print "scale = ",$scale,"\n";

$outfile = $infile;
$outfile =~ s/\.png/\.eps/;

#print $infile," ",$outfile,"\n";
system("convert $infile tmpimage.pgm");
system("convert -density $scale tmpimage.pgm $outfile");
system("rm tmpimage.pgm");


