#!/bin/sh ############################################################################## # This is essentially a Perl program. We exec the Perl interpreter specifying # this same file as the Perl program and use the -x option to cause the Perl # interpreter to skip down to the Perl code. The reason we do this instead of # just making /usr/bin/perl the script interpreter (instead of /bin/sh) is # that the user may have multiple Perl interpreters and the one he wants to # use is properly located in the PATH. The user's choice of Perl interpreter # may be crucial, such as when the user also has a PERL5LIB environment # variable and it selects modules that work with only a certain main # interpreter program. # # An alternative some people use is to have /usr/bin/env as the script # interpreter. We don't do that because we think the existence and # compatibility of /bin/sh is more reliable. # # Note that we aren't concerned about efficiency because the user who needs # high efficiency can use directly the programs that this program invokes. # ############################################################################## exec perl -w -x -S -- "$0" "$@" #!/usr/bin/perl ############################################################################## # ppmfade ############################################################################## # # This program creates a fade (a sequence of frames) between two images. # # By Bryan Henderson, Olympia WA; March 2000 # # Contributed to the public domain by its author. # # Inspired by the program Pbmfade by Wesley C. Barris of AHPCRC, # Minnesota Supercomputer Center, Inc. January 7, 1994. Pbmfade does # much the same thing, but handles non-Netpbm formats too, and is # implemented in a more primitive language. # ############################################################################## use strict; use File::Temp; sub doVersionHack($) { my ($argvR) = @_; my $arg1 = $argvR->[0]; if (defined($arg1) && (($arg1 eq "--version") || ($arg1 eq "-version"))) { my $termStatus = system('ppmmix', '--version'); exit($termStatus == 0 ? 0 : 1); } } my $tmpdir; $tmpdir = File::Temp::tempdir("ppmfade.XXXXXX", CLEANUP => 1); my $SPREAD = 1; my $SHIFT = 2; my $RELIEF = 3; my $OIL = 4; my $EDGE = 5; my $BENTLEY = 6; my $BLOCK = 7; my $MIX = 8; sub commandLineArgs() { my ($firstFileNm, $lastFileNm); # # Set some defaults. # my $baseNm = "fade"; # default base name of output files my $mode = $SPREAD; # default fading mode my $n; # argument number for ($n = 0; $n < @ARGV; $n++) { if ("$ARGV[$n]" eq "-f") { $n++; $firstFileNm = $ARGV[$n]; } elsif ($ARGV[$n] eq "-l") { $n++; $lastFileNm = $ARGV[$n]; } elsif ($ARGV[$n] eq "-base") { $n++; $baseNm = $ARGV[$n]; } elsif ($ARGV[$n] eq "-spread") { $mode = $SPREAD; } elsif ($ARGV[$n] eq "-shift") { $mode = $SHIFT; } elsif ($ARGV[$n] eq "-relief") { $mode = $RELIEF; } elsif ($ARGV[$n] eq "-oil") { $mode = $OIL; } elsif ("$ARGV[$n]" eq "-edge") { $mode = $EDGE; } elsif ("$ARGV[$n]" eq "-bentley") { $mode = $BENTLEY; } elsif ("$ARGV[$n]" eq "-block") { $mode = $BLOCK; } elsif ("$ARGV[$n]" eq "-mix") { $mode = $MIX; } else { print "Unknown argument: $ARGV[$n]\n"; exit 100; } } if (!defined($firstFileNm) && !defined($lastFileNm)) { print STDERR ("You must specify -f or -l (or both)\n"); exit 90; } return $mode, $firstFileNm, $lastFileNm, $baseNm; } sub imageDimensions($$) { my ($firstFileNm, $lastFileNm) = @_; my ($width, $height); if (defined($firstFileNm)) { if ((`pnmfile $firstFileNm` =~ m{\b(\d+)\sby\s(\d+)} )) { $width = $1; $height = $2; } else { print STDERR ("Unrecognized results from pnmfile on $firstFileNm.\n"); exit(50); } } else { # $lastFileNm is defined if ((`pnmfile $lastFileNm` =~ m{\b(\d+)\sby\s(\d+)} )) { $width = $1; $height = $2; } else { print STDERR ("Unrecognized results from pnmfile on $firstFileNm.\n"); exit(50); } } return $width, $height; } #----------------------------------------------------------------------------- # MAINLINE #----------------------------------------------------------------------------- my $nFrame = 30; # Number of images in created sequence, including the fade-from and # fade-to images. In standard video, 30 frames is one second. doVersionHack(\@ARGV); my ($mode, $firstFileNm, $lastFileNm, $baseNm) = commandLineArgs(); if (defined($firstFileNm) && !-e($firstFileNm)) { print STDERR ("First file '$firstFileNm' does not exist\n"); exit 20; } if (defined($lastFileNm) && !-e($lastFileNm)) { print STDERR ("Last file '$lastFileNm' does not exist\n"); exit 20; } # # Define a couple linear ramps. # # We don't use element 0 of these arrays. my @spline10 = (0, 0, 0.11, 0.22, 0.33, 0.44, 0.55, 0.66, 0.77, 0.88, 1.0); my @spline20 = (0, 0, 0.05, 0.11, 0.16, 0.21, 0.26, 0.32, 0.37, 0.42, 0.47, 0.53, 0.58, 0.63, 0.69, 0.74, 0.79, 0.84, 0.89, 0.95, 1.0); my ($width, $height) = imageDimensions($firstFileNm, $lastFileNm); # width and height of our frames in pixels print("Frames are " . $width . "W x " . $height . "H\n"); my $fromDesc = defined($firstFileNm) ? "'$firstFileNm'" : "black"; my $toDesc = defined($lastFileNm) ? "'$lastFileNm'" : "black"; if (!defined($firstFileNm)) { system("ppmmake \\#000 $width $height >$tmpdir/first.ppm"); } else { system("cp", $firstFileNm, "$tmpdir/first.ppm"); } if (!defined($lastFileNm)) { system("ppmmake \\#000 $width $height >$tmpdir/last.ppm"); } else { system("cp", $lastFileNm, "$tmpdir/last.ppm"); } print("Fading from $fromDesc to $toDesc\n"); # # Perform the fade. # # Here's what our temporary files are: # first.ppm: The original (fade-from) image # last.ppm: The target (fade-from) image # junk3.ppm: The frame of the fade for the current iteration of the # the for loop. # junk1a.ppm: If the fade involves a ppmmix sequence from one intermediate # image to another, this is the first frame of that # sequence. # junk2a.ppm: This is the last frame of the above-mentioned ppmmix sequence my $i; # Frame number for ($i = 1; $i <= $nFrame; $i++) { print("Creating $i of $nFrame...\n"); if ($mode eq $SPREAD) { if ($i <= 10) { my $n = $spline20[$i] * 100; system("ppmspread $n $tmpdir/first.ppm >$tmpdir/junk3.ppm"); } elsif ($i <= 20) { my $n; $n = $spline20[$i] * 100; system("ppmspread $n $tmpdir/first.ppm >$tmpdir/junk1a.ppm"); $n = (1-$spline20[$i-10]) * 100; system("ppmspread $n $tmpdir/last.ppm >$tmpdir/junk2a.ppm"); $n = $spline10[$i-10]; system("ppmmix $n $tmpdir/junk1a.ppm $tmpdir/junk2a.ppm " . ">$tmpdir/junk3.ppm"); } else { my $n = (1-$spline20[$i-10])*100; system("ppmspread $n $tmpdir/last.ppm >$tmpdir/junk3.ppm"); } } elsif ($mode eq $SHIFT) { if ($i <= 10) { my $n = $spline20[$i] * 100; system("ppmshift $n $tmpdir/first.ppm >$tmpdir/junk3.ppm"); } elsif ($i <= 20) { my $n; $n = $spline20[$i] * 100; system("ppmshift $n $tmpdir/first.ppm >$tmpdir/junk1a.ppm"); $n = (1-$spline20[$i-10])*100; system("ppmshift $n last.ppm >junk2a.ppm"); $n = $spline10[$i-10]; system("ppmmix $n junk1a.ppm junk2a.ppm >junk3.ppm"); } else { my $n = (1-$spline20[$i-10]) * 100; system("ppmshift $n last.ppm >junk3.ppm"); } } elsif ($mode eq $RELIEF) { if ($i == 1) { system("ppmrelief first.ppm >junk1r.ppm"); } if ($i <= 10) { my $n = $spline10[$i]; system("ppmmix $n first.ppm junk1r.ppm >junk3.ppm"); } elsif ($i <= 20) { my $n = $spline10[$i-10]; system("ppmmix $n junk1r.ppm junk2r.ppm >junk3.ppm"); } else { my $n = $spline10[$i-20]; system("ppmmix $n junk2r.ppm last.ppm >junk3.ppm"); } if ($i == 10) { system("ppmrelief last.ppm >junk2r.ppm"); } } elsif ($mode eq $OIL) { if ($i == 1) { system("ppmtopgm first.ppm | pgmoil >junko.ppm"); system("rgb3toppm junko.ppm junko.ppm junko.ppm " . ">junk1o.ppm"); } if ($i <= 10) { my $n = $spline10[$i]; system("ppmmix $n first.ppm junk1o.ppm >junk3.ppm"); } elsif ($i <= 20) { my $n = $spline10[$i-10]; system("ppmmix $n junk1o.ppm junk2o.ppm >junk3.ppm"); } else { my $n = $spline10[$i-20]; system("ppmmix $n junk2o.ppm last.ppm >junk3.ppm"); } if ($i == 10) { system("ppmtopgm last.ppm | pgmoil >junko.ppm"); system("rgb3toppm junko.ppm junko.ppm junko.ppm " . ">junk2o.ppm"); } } elsif ($mode eq $EDGE) { if ($i == 1) { system("ppmtopgm first.ppm | pgmedge >junko.ppm"); system("rgb3toppm junko.ppm junko.ppm junko.ppm " . ">junk1o.ppm"); } if ($i <= 10) { my $n = $spline10[$i]; system("ppmmix $n first.ppm junk1o.ppm >junk3.ppm"); } elsif ($i <= 20) { my $n = $spline10[$i-10]; system("ppmmix $n junk1o.ppm junk2o.ppm >junk3.ppm"); } else { my $n = $spline10[$i-20]; system("ppmmix $n junk2o.ppm last.ppm >junk3.ppm"); } if ($i == 10) { system("ppmtopgm last.ppm | pgmedge >junko.ppm"); system("rgb3toppm junko.ppm junko.ppm junko.ppm " . ">junk2o.ppm"); } } elsif ($mode eq $BENTLEY) { if ($i == 1) { system("ppmtopgm first.ppm | pgmbentley >junko.ppm"); system("rgb3toppm junko.ppm junko.ppm junko.ppm " . ">junk1o.ppm"); } if ($i <= 10) { my $n = $spline10[$i]; system("ppmmix $n first.ppm junk1o.ppm >junk3.ppm"); } elsif ($i <= 20) { my $n = $spline10[$i-10]; system("ppmmix $n junk1o.ppm junk2o.ppm >junk3.ppm"); } else { my $n = $spline10[$i-20]; system("ppmmix $n $tmpdir/junk2o.ppm $tmpdir/last.ppm " . ">$tmpdir/junk3.ppm"); } if ($i == 10) { system("ppmtopgm $tmpdir/last.ppm | pgmbentley " . ">$tmpdir/junko.ppm"); system("rgb3toppm $tmpdir/junko.ppm $tmpdir/junko.ppm " . "$tmpdir/junko.ppm " . ">$tmpdir/junk2o.ppm"); } } elsif ($mode eq $BLOCK) { if ($i <= 10) { my $n = 1 - 1.9*$spline20[$i]; system("pamscale $n $tmpdir/first.ppm | " . "pamscale -width $width -height $height " . ">$tmpdir/junk3.ppm"); if ($i == 10) { system("cp", "$tmpdir/junk3.ppm", "$tmpdir/junk1a.ppm"); system("pamscale $n $tmpdir/last.ppm | " . "pamscale -width $width -height $height " . ">$tmpdir/junk2a.ppm"); } } elsif ($i <= 20) { my $n = $spline10[$i-10]; system("ppmmix $n $tmpdir/junk1a.ppm $tmpdir/junk2a.ppm " . ">$tmpdir/junk3.ppm"); } else { my $n = 1 - 1.9*$spline20[31-$i]; system("pamscale $n $tmpdir/last.ppm | " . "pamscale -width $width -height $height " . ">$tmpdir/junk3.ppm"); } } elsif ($mode eq $MIX) { my $fadeFactor = sqrt(1/($nFrame-$i+1)); system("ppmmix $fadeFactor $tmpdir/first.ppm $tmpdir/last.ppm " . ">$tmpdir/junk3.ppm"); } else { print("Internal error: impossible mode value '$mode'\n"); } my $outFileNm = sprintf("%s.%04d.ppm", $baseNm, $i); system("cp", "$tmpdir/junk3.ppm", $outFileNm); } exit(0);