#!/usr/bin/perl -w ############################################################################## # Convert from one image format to another. # Default is convert to jpeg # supported formats are # jpeg # gif # png # pnm # tiff # xpm # # this script requires libjpeg and netpbm # # Author: Chris Rouch # Date: 1998-2001 # Copyright (c) Chris Rouch 1998-2001 # # 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 of the License, 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 # or see http://www.gnu.org/licenses/gpl.txt ############################################################################## use strict; use Getopt::Long; # constants use constant CJPEG=>"/usr/bin/cjpeg -q 80"; use constant DJPEG=>"/usr/bin/djpeg"; use constant DTIFF=>"/usr/bin/tifftopnm"; use constant CTIFF=>"/usr/bin/pnmtotiff"; use constant DPNG=>"/usr/bin/pngtopnm"; use constant CPNG=>"/usr/bin/pnmtopng"; use constant DXPM=>"/usr/bin/xpmtoppm"; use constant CXPM=>"/usr/bin/ppmquant 256 |/usr/bin/ppmtoxpm"; use constant DGIF=>"/usr/bin/giftopnm"; use constant CGIF=>"/usr/bin/ppmquant 256 |/usr/bin/ppmtogif"; use constant DPNM=>"/bin/cat"; use constant CPNM=>"/bin/cat"; use constant DEFPREFIX=>'x'; # general global variables my ($PROG,$USAGE,$OPTIONS); use vars qw ($encode $decode $outfile $prefix $base $dir); # command line variables my $verbose; # make some noise my $overwrite; # overwrite new image if it already exists my $prefix=DEFPREFIX; # prefix for new image if it already exists my $type='jpg'; # output file type # command line switch my %opts= ( "v|verbose" => \$verbose, "o|overwrite" => \$overwrite, "p|prefix=s" => \$prefix, "t|type=s" => \$type, ); ($PROG=$0) =~ s#.*/##; SetUsage(\%opts,\$OPTIONS); $USAGE="Usage: $PROG $OPTIONS"; die "$USAGE\n" unless GetOptions(%opts); die "$USAGE\n" unless ($ARGV[0]); # program begins here if ($type eq 'jpg') { $encode=CJPEG; } elsif ($type eq 'tiff') { $encode=CTIFF; } elsif ($type eq 'png') { $encode=CPNG; } elsif ($type eq 'gif') { $encode=CGIF; } elsif ($type eq 'xpm') { $encode=CXPM; } elsif ($type eq 'pnm') { $encode=CPNM; } elsif ($type eq 'pgm') { $encode=CPNM; } else { die "Unsupported output type, $type\n"; } foreach my $file (@ARGV) { if ($file =~ /\.tif/i) { $decode=DTIFF; } elsif ($file =~ /\.pnm/i) { $decode=DPNM; } elsif ($file =~ /\.pgm/i) { $decode=DPNM; } elsif ($file =~ /\.gif/i) { $decode=DGIF; } elsif ($file =~ /\.xpm/i) { $decode=DXPM; } elsif ($file =~ /\.jpg/i) { $decode=DJPEG; } elsif ($file =~ /\.png/i) { $decode=DPNG; } else { warn "Unknown file type for $file, skipping\n"; next; } # make sure decoder exists, it it does, assume encoder also exists # encode may not be a simple executable unless (-x $decode) { warn "Can't run $decode\n"; next; } ($base=$file) =~ s!.*/!!; $base =~ s!\..*?$!!; # remove extension ($dir=$file) =~ s!(.*)/.*$!$1!; $dir='.' if ($dir eq $file); $outfile=$base . '.' . $type; if (!$overwrite) { # outfile exists, keep prefixing until we get a unique name while ( -f "$outfile" ) { warn "$outfile exists, adding extra prefix\n"; $base= $prefix .$base; $outfile="$dir/$base"; $outfile .= '.' . $type; } } print "$decode $file | $encode > $outfile\n" if ($verbose); system("$decode $file | $encode > $outfile"); } ############### end of main ############# sub SetUsage { my ($ropts,$rstr) = @_; foreach my $key (keys %$ropts) { if ($key =~ /\|/) { $key=~ s!^.*\|!!; } if ($key =~ /=/) { $key =~ s!=.*$! value!; } $key = '-' . $key; $$rstr .= '[' . $key . '] '; } } ############### end of file #############