Debian Codegraph @

editor/ppmfade

raw
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
23 exec 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 ##############################################################################
42 use strict;
43
44 use File::Temp;
45
46 #
47 # Define a couple linear ramps.
48 #
49 # We don't use element 0 of these arrays.
50 my @spline10 = (0, 0, 0.11, 0.22, 0.33, 0.44, 0.55, 0.66, 0.77, 0.88, 1.0);
51 my @spline20 = (0, 0, 0.05, 0.11, 0.16, 0.21, 0.26, 0.32, 0.37, 0.42, 0.47,
52 0.53, 0.58, 0.63, 0.69, 0.74, 0.79, 0.84, 0.89, 0.95, 1.0);
53
54 sub doVersionHack($) {
55 my ($argvR) = @_;
56
57 my $arg1 = $argvR->[0];
58
59 if (defined($arg1) && (($arg1 eq "--version") || ($arg1 eq "-version"))) {
60 my $termStatus = system('ppmmix', '--version');
61 exit($termStatus == 0 ? 0 : 1);
62 }
63 }
64
65 my $tmpdir;
66
67 $tmpdir = File::Temp::tempdir("ppmfade.XXXXXX", CLEANUP => 1);
68
69 sub commandLineArgs() {
70
71 my ($firstFileNm, $lastFileNm, $mode, $baseNm);
72
73 for (my $i = 0; $i < @ARGV; ++$i) {
74 my $arg = $ARGV[$i];
75
76 if (0) {
77 } elsif ($arg eq "-f") {
78 ++$i;
79 $firstFileNm = $ARGV[$i];
80 } elsif ($arg eq "-l") {
81 ++$i;
82 $lastFileNm = $ARGV[$i];
83 } elsif ($arg eq "-base") {
84 ++$i;
85 $baseNm = $ARGV[$i];
86 } elsif ($arg eq "-spread") {
87 $mode = 'SPREAD';
88 } elsif ($arg eq "-shift") {
89 $mode = 'SHIFT';
90 } elsif ($arg eq "-relief") {
91 $mode = 'RELIEF';
92 } elsif ($arg eq "-oil") {
93 $mode = 'OIL';
94 } elsif ($arg eq "-edge") {
95 $mode = 'EDGE';
96 } elsif ($arg eq "-bentley") {
97 $mode ='BENTLEY';
98 } elsif ($arg eq "-block") {
99 $mode = 'BLOCK';
100 } elsif ($arg eq "-mix") {
101 $mode = 'MIX';
102 } else {
103 if (substr($arg, 0, 1) eq '-') {
104 print STDERR ("Unknown option '$arg'. " .
105 "We understand -f, -l, -base, " .
106 "-spread, -shift, -relief, -oil, -edge, " .
107 "-bentley, -block, and -mix\n");
108 } else {
109 print STDERR ("There are no non-option arguments possible. " .
110 "You specified '$arg'\n");
111 }
112 exit 100;
113 }
114 }
115
116 if (!defined($firstFileNm) && !defined($lastFileNm)) {
117 print STDERR ("You must specify -f or -l (or both)\n");
118 exit 90;
119 }
120
121 if (!defined($mode)) {
122 $mode = 'SPREAD';
123 }
124
125 if (!defined($baseNm)) {
126 $baseNm = 'fade';
127 }
128
129 return $mode, $firstFileNm, $lastFileNm, $baseNm;
130 }
131
132
133
134 sub imageDimensions($$) {
135 my ($firstFileNm, $lastFileNm) = @_;
136
137 my ($width, $height);
138
139 if (defined($firstFileNm)) {
140 if ((`pnmfile $firstFileNm` =~ m{\b(\d+)\sby\s(\d+)} )) {
141 $width = $1; $height = $2;
142 } else {
143 print STDERR
144 ("Unrecognized results from pnmfile on $firstFileNm.\n");
145 exit(50);
146 }
147 } else {
148 # $lastFileNm is defined
149 if ((`pnmfile $lastFileNm` =~ m{\b(\d+)\sby\s(\d+)} )) {
150 $width = $1; $height = $2;
151 } else {
152 print STDERR
153 ("Unrecognized results from pnmfile on $firstFileNm.\n");
154 exit(50);
155 }
156 }
157 return $width, $height;
158 }
159
160
161
162 sub spreadOne($) {
163 my ($frameNum) = @_;
164
165 if ($frameNum <= 10) {
166 my $n = $spline20[$frameNum] * 100;
167 system("ppmspread $n $tmpdir/first.ppm >$tmpdir/this.ppm");
168 } elsif ($frameNum <= 20) {
169 my $n;
170 $n = $spline20[$frameNum] * 100;
171 system("ppmspread $n $tmpdir/first.ppm >$tmpdir/junk1a.ppm");
172 $n = (1-$spline20[$frameNum-10]) * 100;
173 system("ppmspread $n $tmpdir/last.ppm >$tmpdir/junk2a.ppm");
174 $n = $spline10[$frameNum-10];
175 system("ppmmix $n $tmpdir/junk1a.ppm $tmpdir/junk2a.ppm " .
176 ">$tmpdir/this.ppm");
177 } else {
178 my $n = (1-$spline20[$frameNum-10])*100;
179 system("ppmspread $n $tmpdir/last.ppm >$tmpdir/this.ppm");
180 }
181 }
182
183
184
185 sub shiftOne($) {
186 my ($frameNum) = @_;
187
188 if ($frameNum <= 10) {
189 my $n = $spline20[$frameNum] * 100;
190 system("ppmshift $n $tmpdir/first.ppm >$tmpdir/this.ppm");
191 } elsif ($frameNum <= 20) {
192 my $n;
193 $n = $spline20[$frameNum] * 100;
194 system("ppmshift $n $tmpdir/first.ppm >$tmpdir/junk1a.ppm");
195 $n = (1-$spline20[$frameNum-10])*100;
196 system("ppmshift $n $tmpdir/last.ppm >$tmpdir/junk2a.ppm");
197 $n = $spline10[$frameNum-10];
198 system("ppmmix $n $tmpdir/junk1a.ppm $tmpdir/junk2a.ppm " .
199 ">$tmpdir/this.ppm");
200 } else {
201 my $n = (1-$spline20[$frameNum-10]) * 100;
202 system("ppmshift $n $tmpdir/last.ppm >$tmpdir/this.ppm");
203 }
204 }
205
206
207
208 sub reliefOne($) {
209 my ($frameNum) = @_;
210
211 if ($frameNum == 1) {
212 system("ppmrelief $tmpdir/first.ppm >$tmpdir/junk1r.ppm");
213 }
214 if ($frameNum <= 10) {
215 my $n = $spline10[$frameNum];
216 system("ppmmix $n $tmpdir/first.ppm $tmpdir/junk1r.ppm " .
217 ">$tmpdir/this.ppm");
218 } elsif ($frameNum <= 20) {
219 my $n = $spline10[$frameNum-10];
220 system("ppmmix $n $tmpdir/junk1r.ppm $tmpdir/junk2r.ppm " .
221 ">$tmpdir/this.ppm");
222 } else {
223 my $n = $spline10[$frameNum-20];
224 system("ppmmix $n $tmpdir/junk2r.ppm $tmpdir/last.ppm " .
225 ">$tmpdir/this.ppm");
226 }
227 if ($frameNum == 10) {
228 system("ppmrelief $tmpdir/last.ppm >$tmpdir/junk2r.ppm");
229 }
230 }
231
232
233
234 sub oilOne($) {
235 my ($frameNum) = @_;
236
237 if ($frameNum == 1) {
238 system("ppmtopgm $tmpdir/first.ppm | pgmoil >$tmpdir/junko.ppm");
239 system("rgb3toppm " .
240 "$tmpdir/junko.ppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
241 ">$tmpdir/junk1o.ppm");
242 }
243 if ($frameNum <= 10) {
244 my $n = $spline10[$frameNum];
245 system("ppmmix $n $tmpdir/first.ppm $tmpdir/junk1o.ppm " .
246 ">$tmpdir/this.ppm");
247 } elsif ($frameNum <= 20) {
248 my $n = $spline10[$frameNum-10];
249 system("ppmmix $n $tmpdir/junk1o.ppm $tmpdir/junk2o.ppm " .
250 ">$tmpdir/this.ppm");
251 } else {
252 my $n = $spline10[$frameNum-20];
253 system("ppmmix $n $tmpdir/junk2o.ppm $tmpdir/last.ppm " .
254 ">$tmpdir/this.ppm");
255 }
256 if ($frameNum == 10) {
257 system("ppmtopgm $tmpdir/last.ppm | pgmoil >$tmpdir/junko.ppm");
258 system("rgb3toppm " .
259 "$tmpdir/junko.ppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
260 ">$tmpdir/junk2o.ppm");
261 }
262 }
263
264
265
266 sub edgeOne($) {
267 my ($frameNum) = @_;
268
269 if ($frameNum == 1) {
270 system("ppmtopgm $tmpdir/first.ppm | pgmedge >$tmpdir/junko.ppm");
271 system("rgb3toppm " .
272 "$tmpdir/junko.ppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
273 ">$tmpdir/junk1o.ppm");
274 }
275 if ($frameNum <= 10) {
276 my $n = $spline10[$frameNum];
277 system("ppmmix $n $tmpdir/first.ppm $tmpdir/junk1o.ppm " .
278 ">$tmpdir/this.ppm");
279 } elsif ($frameNum <= 20) {
280 my $n = $spline10[$frameNum-10];
281 system("ppmmix $n $tmpdir/junk1o.ppm $tmpdir/junk2o.ppm " .
282 ">$tmpdir/this.ppm");
283 } else {
284 my $n = $spline10[$frameNum-20];
285 system("ppmmix $n $tmpdir/junk2o.ppm $tmpdir/last.ppm " .
286 ">$tmpdir/this.ppm");
287 }
288 if ($frameNum == 10) {
289 system("ppmtopgm $tmpdir/last.ppm | pgmedge >$tmpdir/junko.ppm");
290 system("rgb3toppm " .
291 "$tmpdir/junko.ppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
292 ">$tmpdir/junk2o.ppm");
293 }
294 }
295
296
297
298 sub bentleyOne($) {
299 my ($frameNum) = @_;
300
301 if ($frameNum == 1) {
302 system("ppmtopgm $tmpdir/first.ppm | pgmbentley >$tmpdir/junko.ppm");
303 system("rgb3toppm " .
304 "$tmpdir/junko.ppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
305 ">$tmpdir/junk1o.ppm");
306 }
307 if ($frameNum <= 10) {
308 my $n = $spline10[$frameNum];
309 system("ppmmix $n $tmpdir/first.ppm $tmpdir/junk1o.ppm " .
310 ">$tmpdir/this.ppm");
311 } elsif ($frameNum <= 20) {
312 my $n = $spline10[$frameNum-10];
313 system("ppmmix $n $tmpdir/junk1o.ppm $tmpdir/junk2o.ppm " .
314 ">$tmpdir/this.ppm");
315 } else {
316 my $n = $spline10[$frameNum-20];
317 system("ppmmix $n $tmpdir/junk2o.ppm $tmpdir/last.ppm " .
318 ">$tmpdir/this.ppm");
319 }
320 if ($frameNum == 10) {
321 system("ppmtopgm $tmpdir/last.ppm | pgmbentley " .
322 ">$tmpdir/junko.ppm");
323 system("rgb3toppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
324 "$tmpdir/junko.ppm " .
325 ">$tmpdir/junk2o.ppm");
326 }
327 }
328
329
330
331 sub blockOne($$$) {
332 my ($frameNum, $width, $height) = @_;
333
334 if ($frameNum <= 10) {
335 my $n = 1 - 1.9*$spline20[$frameNum];
336 system("pamscale $n $tmpdir/first.ppm | " .
337 "pamscale -width $width -height $height " .
338 ">$tmpdir/this.ppm");
339 if ($frameNum == 10) {
340 system("cp", "$tmpdir/this.ppm", "$tmpdir/junk1a.ppm");
341 system("pamscale $n $tmpdir/last.ppm | " .
342 "pamscale -width $width -height $height " .
343 ">$tmpdir/junk2a.ppm");
344 }
345 } elsif ($frameNum <= 20) {
346 my $n = $spline10[$frameNum-10];
347 system("ppmmix $n $tmpdir/junk1a.ppm $tmpdir/junk2a.ppm " .
348 ">$tmpdir/this.ppm");
349 } else {
350 my $n = 1 - 1.9*$spline20[31-$frameNum];
351 system("pamscale $n $tmpdir/last.ppm | " .
352 "pamscale -width $width -height $height " .
353 ">$tmpdir/this.ppm");
354 }
355 }
356
357
358
359 sub mixOne($$) {
360 my ($frameNum, $nFrame) = @_;
361
362 my $fadeFactor = sqrt(1/($nFrame - $frameNum + 1));
363
364 system("ppmmix $fadeFactor $tmpdir/first.ppm $tmpdir/last.ppm " .
365 ">$tmpdir/this.ppm");
366 }
367
368
369
370 sub fade($$$$$) {
371 my ($nFrame, $mode, $width, $height, $baseNm) = @_;
372 #-----------------------------------------------------------------------------
373 # Do the fade starting, from image "first.ppm" in the temporary direct
374 # $tmpdir and ending up at "last.ppm", creating $nFrame frames, including
375 # the first and last.
376 #
377 # Make the outputs $width by $height.
378 #
379 # Name the result files $baseNm.NNNN.ppm.
380 #-----------------------------------------------------------------------------
381
382 # Here's what our temporary files are:
383 # first.ppm: The original (fade-from) image
384 # last.ppm: The target (fade-from) image
385 # this.ppm: The frame of the fade for the current iteration of the
386 # the for loop.
387 # junk1a.ppm: If the fade involves a ppmmix sequence from one
388 # intermediate image to another, this is the first frame
389 # of that sequence.
390 # junk2a.ppm: This is the last frame of the above-mentioned
391 # ppmmix sequence
392
393 my $frameNum; # Frame number
394 for ($frameNum = 1; $frameNum <= $nFrame; ++$frameNum) {
395
396 print("Creating Frame $frameNum of $nFrame...\n");
397
398 if ($mode eq 'SPREAD') {
399 spreadOne($frameNum);
400 } elsif ($mode eq 'SHIFT') {
401 shiftOne($frameNum);
402 } elsif ($mode eq 'RELIEF') {
403 reliefOne($frameNum);
404 } elsif ($mode eq 'OIL') {
405 oilOne($frameNum);
406 } elsif ($mode eq 'EDGE') {
407 edgeOne($frameNum);
408 } elsif ($mode eq 'BENTLEY') {
409 bentleyOne($frameNum);
410 } elsif ($mode eq 'BLOCK') {
411 blockOne($frameNum, $width, $height);
412 } elsif ($mode eq 'MIX') {
413 mixOne($frameNum, $nFrame);
414 } else {
415 print("INTERNAL ERROR: impossible mode value '$mode'\n");
416 }
417
418 my $outFileNm = sprintf("%s.%04d.ppm", $baseNm, $frameNum);
419 system("cp", "$tmpdir/this.ppm", $outFileNm);
420 }
421 }
422
423
424
425 #-----------------------------------------------------------------------------
426 # MAINLINE
427 #-----------------------------------------------------------------------------
428
429 my $nFrame = 30;
430 # Number of images in created sequence, including the fade-from and
431 # fade-to images. In standard video, 30 frames is one second.
432
433 doVersionHack(\@ARGV);
434
435 my ($mode, $firstFileNm, $lastFileNm, $baseNm) = commandLineArgs();
436
437 if (defined($firstFileNm) && !-e($firstFileNm)) {
438 print STDERR ("First file '$firstFileNm' does not exist\n");
439 exit 20;
440 }
441
442 if (defined($lastFileNm) && !-e($lastFileNm)) {
443 print STDERR ("Last file '$lastFileNm' does not exist\n");
444 exit 20;
445 }
446
447 my ($width, $height) = imageDimensions($firstFileNm, $lastFileNm);
448
449 # width and height of our frames in pixels
450
451 print("Frames are " . $width . "W x " . $height . "H\n");
452
453 my $fromDesc = defined($firstFileNm) ? "'$firstFileNm'" : "black";
454 my $toDesc = defined($lastFileNm) ? "'$lastFileNm'" : "black";
455
456 if (!defined($firstFileNm)) {
457 system("ppmmake \\#000 $width $height >$tmpdir/first.ppm");
458 } else {
459 system("cp", $firstFileNm, "$tmpdir/first.ppm");
460 }
461
462 if (!defined($lastFileNm)) {
463 system("ppmmake \\#000 $width $height >$tmpdir/last.ppm");
464 } else {
465 system("cp", $lastFileNm, "$tmpdir/last.ppm");
466 }
467
468 print("Fading from $fromDesc to $toDesc\n");
469
470 fade($nFrame, $mode, $width, $height, $baseNm);
471
472 exit(0);