#!/usr/bin/perl -w if (1 != $#ARGV) { die "usage: eps2ppm PS/EPS_input [width]x[height]|dpi\n"; } $gs = "/sw/bin/gs"; $me = "eps2ppm"; $fileIn = $ARGV[0]; $sizeInfo = $ARGV[1]; $blah = 0; open(EPSFILE, $fileIn) or die "$0: Can't open $fileIn for reading.\n"; $hasShowpage = 0; $foundBBox = 0; while () { chop; # print "$_, foundBBox = $foundBBox\n"; if (not $foundBBox and /^%%BoundingBox:/) { ($blah, $bndmX, $bndmY, $bndMX, $bndMY) = split; print STDERR "$me: bounding box: ($bndmX,$bndmY) ($bndMX,$bndMY).\n"; $foundBBox = 1; } if (/showpage/) { $hasShowpage = 1; } } close(EPSFILE); if (!($foundBBox)) { die "$me: Sorry, couldn't find valid BoundingBox command.\n"; } if ($hasShowpage) { print STDERR "$me: Great: $fileIn seems to have its own showpage command.\n"; print STDERR "$me: But if image is blank, you'll need to add a showpage command\n"; print STDERR "$me: to the end of the eps file.\n"; $showpage = ""; } else { print STDERR "$me: $fileIn doesn't seem to have its own showpage command, adding one.\n"; print STDERR "$me: If image file is twice as long as it should be, that is\n"; print STDERR "$me: too bad until I add other options to this script.\n"; $showpage = "showpage"; } # # determine output image dimensions and resolution # $pageX = $bndMX - $bndmX; $pageY = $bndMY - $bndmY; $aspect = $pageX/$pageY; print STDERR "$me: page is $pageX by $pageY points, aspectratio = $aspect.\n"; if ($sizeInfo =~ "x") { ($imageX, $imageY) = split(/x/, $sizeInfo); if (!($imageX)) { $imageX = int $imageY * $aspect; } if (!($imageY)) { $imageY = int $imageX / $aspect; } } else { $imageX = int $pageX*$sizeInfo/72; $imageY = int $pageY*$sizeInfo/72; } print STDERR "$me: Will generate $imageX by $imageY ppm.\n"; $resX = int 72*$imageX/$pageX; $resY = int 72*$imageY/$pageY; # # set-up and run the actual gs command # #$epsCmd = "(echo \"%!\"; echo \"-$bndmX -$bndmY translate\"; /bin/cat $fileIn; echo $showpage) | $gs -dNOPAUSE -sDEVICE=ppmraw -sOutputFile=$fileOut -r".$resX."x".$resY." -g".$imageX."x".$imageY." -"; $epsCmd = "(echo \"%!\"; echo \"-$bndmX -$bndmY translate\"; /bin/cat $fileIn; echo $showpage) | $gs -dNOPAUSE -sDEVICE=ppmraw -sOutputFile=- -q -r".$resX."x".$resY." -g".$imageX."x".$imageY." -"; print STDERR "$me: running \"$epsCmd\" ... \n"; $rc = 0xffff & system($epsCmd); print STDERR "$me: done. \n"; # # do something with the return code # if ($rc == 0) { # no errors } elsif ($rc == 0xff00) { # not sure what good this does print STDERR "$me: gs failed"; print STDERR " with error $!" if ($!); print STDERR ".\n"; } elsif ($rc > 0x80) { $rc >>= 8; print STDERR "$me: gs ran with non-zero exit status $rc.\n"; } else { print STDERR "$me: gs ran with "; if ($rc & 0x80) { $rc &= ~0x80; print STDERR "coredump from "; } print STDERR "signal $rc.\n"; } # pass return code back to caller exit($rc);