1#!/bin/sh
2
3##############################################################################
4# This is essentially a Perl program.  We exec the Perl interpreter specifying
5# this same file as the Perl program and use the -x option to cause the Perl
6# interpreter to skip down to the Perl code.  The reason we do this instead of
7# just making /usr/bin/perl the script interpreter (instead of /bin/sh) is
8# that the user may have multiple Perl interpreters and the one he wants to
9# use is properly located in the PATH.  The user's choice of Perl interpreter
10# may be crucial, such as when the user also has a PERL5LIB environment
11# variable and it selects modules that work with only a certain main
12# interpreter program.
13#
14# An alternative some people use is to have /usr/bin/env as the script
15# interpreter.  We don't do that because we think the existence and
16# compatibility of /bin/sh is more reliable.
17#
18# Note that we aren't concerned about efficiency because the user who needs
19# high efficiency can use directly the programs that this program invokes.
20#
21##############################################################################
22
23exec perl -w -x -S -- "$0" "$@"
24
25#!/usr/bin/perl
26##############################################################################
27#                                  ppmfade
28##############################################################################
29#
30#  This program creates a fade (a sequence of frames) between two images.
31#
32#  By Bryan Henderson, Olympia WA; March 2000
33#
34#  Contributed to the public domain by its author.
35#
36#  Inspired by the program Pbmfade by Wesley C. Barris of AHPCRC,
37#  Minnesota Supercomputer Center, Inc. January 7, 1994.  Pbmfade does
38#  much the same thing, but handles non-Netpbm formats too, and is
39#  implemented in a more primitive language.
40#
41##############################################################################
42use strict;
43
44sub doVersionHack($) {
45    my ($argvR) = @_;
46
47    my $arg1 = $argvR->[0];
48
49    if (defined($arg1) && (($arg1 eq "--version") || ($arg1 eq "-version"))) {
50        my $termStatus = system('ppmmix', '--version');
51        exit($termStatus == 0 ? 0 : 1);
52    }
53}
54
55
56
57my $SPREAD =  1;
58my $SHIFT =   2;
59my $RELIEF =  3;
60my $OIL =     4;
61my $EDGE =    5;
62my $BENTLEY = 6;
63my $BLOCK =   7;
64my $MIX =     8;
65#
66#  Set some defaults.
67#
68my $nframes = 30;			# total number of files created (1 sec)
69my $first_file = "undefined";
70my $last_file = "undefined";
71my $base_name = "fade";		# default base name of output files
72my $image = "ppm";		# default output storage format
73my $mode = $SPREAD;		# default fading mode
74
75doVersionHack(\@ARGV);
76
77my $n;  # argument number
78
79for ($n = 0; $n < @ARGV; $n++) {
80    if ("$ARGV[$n]" eq "-f") {
81        $n++;
82        $first_file = $ARGV[$n];
83        if (-e $first_file) {
84        } else {
85            print "I can't find first file '$first_file'\n";
86            exit 20;
87        }
88    } elsif ($ARGV[$n] eq "-l") {
89        $n++;
90        $last_file = $ARGV[$n];
91        if (-e $last_file) {
92        } else {
93            print "I can't find last file '$last_file'\n";
94            exit 20;
95        }
96    } elsif ($ARGV[$n] eq "-base") {
97        $n++;
98        $base_name = $ARGV[$n];
99    } elsif ($ARGV[$n] eq "-spread") {
100        $mode = $SPREAD;
101    } elsif ($ARGV[$n] eq "-shift") {
102        $mode = $SHIFT;
103    } elsif ($ARGV[$n] eq "-relief") {
104        $mode = $RELIEF;
105    } elsif ($ARGV[$n] eq "-oil") {
106        $mode = $OIL;
107    } elsif ("$ARGV[$n]" eq "-edge") {
108        $mode = $EDGE;
109    } elsif ("$ARGV[$n]" eq "-bentley") {
110        $mode = $BENTLEY;
111    } elsif ("$ARGV[$n]" eq "-block") {
112        $mode = $BLOCK;
113    } elsif ("$ARGV[$n]" eq "-mix") {
114        $mode = $MIX;
115    } else {
116        print "Unknown argument: $ARGV[$n]\n";
117        exit 100;
118    }
119}
120#
121#  Define a couple linear ramps.
122#
123# We don't use element 0 of these arrays.
124my @spline10 = (0, 0, 0.11, 0.22, 0.33, 0.44, 0.55, 0.66, 0.77, 0.88, 1.0);
125my @spline20 = (0, 0, 0.05, 0.11, 0.16, 0.21, 0.26, 0.32, 0.37, 0.42, 0.47,
126                0.53, 0.58, 0.63, 0.69, 0.74, 0.79, 0.84, 0.89, 0.95, 1.0);
127#
128#  Just what are we supposed to do?
129#
130my ($height, $width);    # width and height of our frames
131if ($first_file ne "undefined") {
132    if ((`pnmfile $first_file` =~ m{\b(\d+)\sby\s(\d+)} )) {
133        $width = $1; $height = $2;
134    } else {
135        print("Unrecognized results from pnmfile on $first_file.\n");
136        exit(50);
137    }
138} elsif ($last_file ne "undefined") {
139    if ((`pnmfile $last_file` =~ m{\b(\d+)\sby\s(\d+)} )) {
140        $width = $1; $height = $2;
141    } else {
142        print("Unrecognized results from pnmfile on $first_file.\n");
143        exit(50);
144    }
145} else {
146    print("ppmfade:  You must specify -f or -l (or both)\n");
147    exit(90);
148}
149
150print("Frames are " . $width . "W x " . $height . "H\n");
151
152if ($first_file eq "undefined") {
153    print "Fading from black to ";
154    system("ppmmake \\#000 $width $height >junk1$$.ppm");
155} else {
156    print "Fading from $first_file to ";
157    system("cp", $first_file, "junk1$$.ppm");
158}
159
160if ($last_file eq "undefined") {
161    print "black.\n";
162    system("ppmmake \\#000 $width $height >junk2$$.ppm");
163} else {
164    print "$last_file\n";
165    system("cp", $last_file, "junk2$$.ppm");
166}
167
168#
169#  Perform the fade.
170#
171
172# Here's what our temporary files are:
173#   junk1$$.ppm: The original (fade-from) image
174#   junk2$$.ppm: The target (fade-from) image
175#   junk3$$.ppm: The frame of the fade for the current iteration of the
176#                the for loop.
177#   junk1a$$.ppm: If the fade involves a ppmmix sequence from one intermediate
178#                 image to another, this is the first frame of that
179#                 sequence.
180#   junk2a$$.ppm: This is the last frame of the above-mentioned ppmmix sequence
181
182my $i;    # Frame number
183for ($i = 1; $i <= $nframes; $i++) {
184    print("Creating $i of $nframes...\n");
185    if ($mode eq $SPREAD) {
186        if ($i <= 10) {
187            my $n = $spline20[$i] * 100;
188            system("ppmspread $n junk1$$.ppm >junk3$$.ppm");
189        } elsif ($i <= 20) {
190            my $n;
191            $n = $spline20[$i] * 100;
192            system("ppmspread $n junk1$$.ppm >junk1a$$.ppm");
193            $n = (1-$spline20[$i-10]) * 100;
194            system("ppmspread $n junk2$$.ppm >junk2a$$.ppm");
195            $n = $spline10[$i-10];
196            system("ppmmix $n junk1a$$.ppm junk2a$$.ppm >junk3$$.ppm");
197        } else {
198            my $n = (1-$spline20[$i-10])*100;
199            system("ppmspread $n junk2$$.ppm >junk3$$.ppm");
200        }
201    } elsif ($mode eq $SHIFT) {
202        if ($i <= 10) {
203            my $n = $spline20[$i] * 100;
204            system("ppmshift $n junk1$$.ppm >junk3$$.ppm");
205        } elsif ($i <= 20) {
206            my $n;
207            $n = $spline20[$i] * 100;
208            system("ppmshift $n junk1$$.ppm >junk1a$$.ppm");
209            $n = (1-$spline20[$i-10])*100;
210            system("ppmshift $n junk2$$.ppm >junk2a$$.ppm");
211            $n = $spline10[$i-10];
212            system("ppmmix $n junk1a$$.ppm junk2a$$.ppm >junk3$$.ppm");
213        } else {
214            my $n = (1-$spline20[$i-10]) * 100;
215            system("ppmshift $n junk2$$.ppm >junk3$$.ppm");
216        }
217    } elsif ($mode eq $RELIEF) {
218        if ($i == 1) {
219            system("ppmrelief junk1$$.ppm >junk1r$$.ppm");
220        }
221        if ($i <= 10) {
222            my $n = $spline10[$i];
223            system("ppmmix $n junk1$$.ppm junk1r$$.ppm >junk3$$.ppm");
224        } elsif ($i <= 20) {
225            my $n = $spline10[$i-10];
226            system("ppmmix $n junk1r$$.ppm junk2r$$.ppm >junk3$$.ppm");
227        } else {
228            my $n = $spline10[$i-20];
229            system("ppmmix $n junk2r$$.ppm junk2$$.ppm >junk3$$.ppm");
230        }
231        if ($i == 10) {
232            system("ppmrelief junk2$$.ppm >junk2r$$.ppm");
233        }
234    } elsif ($mode eq $OIL) {
235        if ($i == 1) {
236            system("ppmtopgm junk1$$.ppm | pgmoil >junko$$.ppm");
237            system("rgb3toppm junko$$.ppm junko$$.ppm junko$$.ppm " .
238                   ">junk1o$$.ppm");
239        }
240        if ($i <= 10) {
241            my $n = $spline10[$i];
242            system("ppmmix $n junk1$$.ppm junk1o$$.ppm >junk3$$.ppm");
243        } elsif ($i <= 20) {
244            my $n = $spline10[$i-10];
245            system("ppmmix $n junk1o$$.ppm junk2o$$.ppm >junk3$$.ppm");
246        } else {
247            my $n = $spline10[$i-20];
248            system("ppmmix $n junk2o$$.ppm junk2$$.ppm >junk3$$.ppm");
249        }
250        if ($i == 10) {
251            system("ppmtopgm junk2$$.ppm | pgmoil >junko$$.ppm");
252            system("rgb3toppm junko$$.ppm junko$$.ppm junko$$.ppm " .
253                   ">junk2o$$.ppm");
254        }
255    } elsif ($mode eq $EDGE) {
256        if ($i == 1) {
257            system("ppmtopgm junk1$$.ppm | pgmedge >junko$$.ppm");
258            system("rgb3toppm junko$$.ppm junko$$.ppm junko$$.ppm " .
259                   ">junk1o$$.ppm");
260        }
261        if ($i <= 10) {
262            my $n = $spline10[$i];
263            system("ppmmix $n junk1$$.ppm junk1o$$.ppm >junk3$$.ppm");
264        } elsif ($i <= 20) {
265            my $n = $spline10[$i-10];
266            system("ppmmix $n junk1o$$.ppm junk2o$$.ppm >junk3$$.ppm");
267        } else {
268            my $n = $spline10[$i-20];
269            system("ppmmix $n junk2o$$.ppm junk2$$.ppm >junk3$$.ppm");
270        }
271        if ($i == 10) {
272            system("ppmtopgm junk2$$.ppm | pgmedge >junko$$.ppm");
273            system("rgb3toppm junko$$.ppm junko$$.ppm junko$$.ppm " .
274                   ">junk2o$$.ppm");
275        }
276    } elsif ($mode eq $BENTLEY) {
277        if ($i == 1) {
278            system("ppmtopgm junk1$$.ppm | pgmbentley >junko$$.ppm");
279            system("rgb3toppm junko$$.ppm junko$$.ppm junko$$.ppm " .
280                   ">junk1o$$.ppm");
281        }
282        if ($i <= 10) {
283            my $n = $spline10[$i];
284            system("ppmmix $n junk1$$.ppm junk1o$$.ppm >junk3$$.ppm");
285        } elsif ($i <= 20) {
286            my $n = $spline10[$i-10];
287            system("ppmmix $n junk1o$$.ppm junk2o$$.ppm >junk3$$.ppm");
288        } else {
289            my $n = $spline10[$i-20];
290            system("ppmmix $n junk2o$$.ppm junk2$$.ppm >junk3$$.ppm");
291        }
292        if ($i == 10) {
293            system("ppmtopgm junk2$$.ppm | pgmbentley >junko$$.ppm");
294            system("rgb3toppm junko$$.ppm junko$$.ppm junko$$.ppm " .
295                   ">junk2o$$.ppm");
296        }
297    } elsif ($mode eq $BLOCK) {
298        if ($i <= 10) {
299            my $n = 1 - 1.9*$spline20[$i];
300            system("pamscale $n junk1$$.ppm | " .
301                   "pamscale -width $width -height $height >junk3$$.ppm");
302        } elsif ($i <= 20) {
303            my $n = $spline10[$i-10];
304            system("ppmmix $n junk1a$$.ppm junk2a$$.ppm >junk3$$.ppm");
305        } else {
306            my $n = 1 - 1.9*$spline20[31-$i];
307            system("pamscale $n junk2$$.ppm | " .
308                   "pamscale -width $width -height $height >junk3$$.ppm");
309        }
310        if ($i == 10) {
311            system("cp", "junk3$$.ppm", "junk1a$$.ppm");
312            system("pamscale $n junk2$$.ppm | " .
313                   "pamscale -width $width -height $height >junk2a$$.ppm");
314        }
315    } elsif ($mode eq $MIX) {
316        my $fade_factor = sqrt(1/($nframes-$i+1));
317        system("ppmmix $fade_factor junk1$$.ppm junk2$$.ppm >junk3$$.ppm");
318    } else {
319        print("Internal error: impossible mode value '$mode'\n");
320    }
321
322    my $outfile = sprintf("%s.%04d.ppm", $base_name, $i);
323    system("cp", "junk3$$.ppm", $outfile);
324}
325
326#
327#  Clean up shop.
328#
329system("rm junk*$$.ppm");
330
331exit(0);
332