#!/afs/athena/contrib/perl/perl #catch control c's $SIG{'INT'} = 'sigint_grabber'; sub sigint_grabber { &clean($just_name); exit(0); } #set up so we can get the appropriate graphics binaries $host_type = $ENV{"HOSTTYPE"} || $ENV {"hosttype"}; $bin_dir = "$host_type" . "bin"; if (system("/bin/athena/attach -q graphics")) { die "Can't attach graphics utilities\n";} # variables $in = $ARGV[0]; #where to put the file $destdir = "."; #default to pwd $destdir = $ARGV[2] if $ARGV[1] =~ /\-dest/; $destdir = $ARGV[4] if $ARGV[3] =~ /\-dest/; $destdir =~ s/\/\s*$//; #how to size it $scale_factor = 1; #default to incoming size $scale_factor = $ARGV[2] if $ARGV[1] =~ /\-scale/; $scale_factor = $ARGV[4] if $ARGV[3] =~ /\-scale/; if (!-d $destdir) {die "Error: Can't find $destdir!\n";} if ($scale_factor !~ /\d+|auto/) {die "Error: scale has to be a number or the string 'auto'!\n";} #check for right kind of filename and do the thing if ($in =~ /\-all/) { open(ALLOFEM,"ls -1 *.tif |"); while() { chop; @all[$i] = $_; ++$i; } FILE: foreach $in(@all) { &doit; next FILE; } exit(0); } elsif ($in !~ /.\.tif$/i) { print "Gotta start with a tif (ending in \'.tif\' or \'.TIF\') file,\n or \'-all\' to convert all appropriate files in the current directory!\n"; &usage; } else { &doit; exit(0); } sub doit { if (-e "$in") { # do the thing print "Working on: $in\n"; $scale_factor =~ /\s*auto\s*/ && do {&autoscale($in); &tif2gif; &clean($just_name);}; &tif2gif; &clean($just_name); } else { die "Aborting cause I can't open the file \'$in\'\n"; } } sub autoscale { local($infile) = @_; #calculate an appropriate 'full screen' size for the thing #assume 760 pixels for a 'normal' screen width - may want to change this open(TIFFINFO,"/mit/graphics/$bin_dir/tiffinfo $infile |"); while() { /\s*Image Width:\s*(\d+)/ && do {$width = $1;}; $scale_factor = 760 / (1 + $width); } print "Incoming image is $width pixels wide. Auto scaling to 760 pixels..\n"; } #Here's the real stuff, such as it is sub tif2gif { $file = substr($in,0,(length($in)-4)); &get_just_filename($file); # a reminder for our user.. print "Destination directory: $destdir\nScale factor: $scale_factor\nOutput file name: $file.gif\n\n"; #Three steps for graphic conversion: #1. convert tiff to pnm file if(system("/mit/graphics/$bin_dir/tifftopnm $in > /tmp/$just_name.temp")) { &clean($file); die "Sorry, I can't deal with this file. Bailing..\n"; } #2. reduce pnm to manageable size for screen display # pnmscale produces reasonably good quality. pnmdepth then reduces the # grayscale bits and makes file smaller without too much loss of quality. # Depth of 4 bits (5 'colors') seems to be deep enough. if (system("/mit/graphics/$bin_dir/pnmscale $scale_factor /tmp/$just_name.temp > /tmp/$just_name.temp.reduced")) { &clean($file); die "Sorry, I can't deal with this file. Bailing..\n"; } if (system("/mit/graphics/$bin_dir/pnmdepth 4 /tmp/$just_name.temp.reduced > /tmp/$just_name.temp.reduced.depth4")) { &clean($file); die "Sorry, I can't deal with this file. Bailing..\n"; } #3. convert reduced ppm to gif, give it the gif extension, and dump it in # the destination directory system("/mit/graphics/$bin_dir/ppmtogif /tmp/$just_name.temp.reduced.depth4 > $destdir/$just_name.gif"); } sub clean { # zap work files local($just_name) = @_; system("/bin/rm /tmp/$just_name.temp"); system("/bin/rm /tmp/$just_name.temp.reduced"); system("/bin/rm /tmp/$just_name.temp.reduced.depth4"); } sub usage { die "usage: giffit ['filename' '-all'] -scale [scaling factor] [auto] -dest [destination directory]\n"; } sub get_just_filename { #extracts a filename from the end of a full path; local($path) = @_; @split_filename_path = split(/\//,$path); $just_name = $split_filename_path[$#split_filename_path]; # chop $just_name; return($just_name); }