about summary refs log tree commit diff
path: root/editor/ppmfade
blob: 4e2c836771e5496e2824659152ab16bf21018299 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/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;

#
#  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);

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);

sub commandLineArgs() {

    my ($firstFileNm, $lastFileNm, $mode, $baseNm);

    for (my $i = 0; $i < @ARGV; ++$i) {
        my $arg = $ARGV[$i];

        if (0) {
        } elsif ($arg eq "-f") {
            ++$i;
            $firstFileNm = $ARGV[$i];
        } elsif ($arg eq "-l") {
            ++$i;
            $lastFileNm = $ARGV[$i];
        } elsif ($arg eq "-base") {
            ++$i;
            $baseNm = $ARGV[$i];
        } elsif ($arg eq "-spread") {
            $mode = 'SPREAD';
        } elsif ($arg eq "-shift") {
            $mode = 'SHIFT';
        } elsif ($arg eq "-relief") {
            $mode = 'RELIEF';
        } elsif ($arg eq "-oil") {
            $mode = 'OIL';
        } elsif ($arg eq "-edge") {
            $mode = 'EDGE';
        } elsif ($arg eq "-bentley") {
            $mode ='BENTLEY';
        } elsif ($arg eq "-block") {
            $mode = 'BLOCK';
        } elsif ($arg eq "-mix") {
            $mode = 'MIX';
        } else {
            if (substr($arg, 0, 1) eq '-') {
                   print STDERR ("Unknown option '$arg'.  " .
                                 "We understand -f, -l, -base, " .
                                 "-spread, -shift, -relief, -oil, -edge, " .
                                 "-bentley, -block, and -mix\n");
            } else {
                print STDERR ("There are no non-option arguments possible.  " .
                              "You specified '$arg'\n");
            }
            exit 100;
        }
    }

    if (!defined($firstFileNm) && !defined($lastFileNm)) {
        print STDERR ("You must specify -f or -l (or both)\n");
        exit 90;
    }

    if (!defined($mode)) {
        $mode = 'SPREAD';
    }

    if (!defined($baseNm)) {
        $baseNm = 'fade';
    }

    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;
}



sub spreadOne($) {
    my ($frameNum) = @_;

    if ($frameNum <= 10) {
        my $n = $spline20[$frameNum] * 100;
        system("ppmspread $n $tmpdir/first.ppm >$tmpdir/this.ppm");
    } elsif ($frameNum <= 20) {
        my $n;
        $n = $spline20[$frameNum] * 100;
        system("ppmspread $n $tmpdir/first.ppm >$tmpdir/junk1a.ppm");
        $n = (1-$spline20[$frameNum-10]) * 100;
        system("ppmspread $n $tmpdir/last.ppm >$tmpdir/junk2a.ppm");
        $n = $spline10[$frameNum-10];
        system("ppmmix $n $tmpdir/junk1a.ppm $tmpdir/junk2a.ppm " .
               ">$tmpdir/this.ppm");
    } else {
        my $n = (1-$spline20[$frameNum-10])*100;
        system("ppmspread $n $tmpdir/last.ppm >$tmpdir/this.ppm");
    }
}



sub shiftOne($) {
    my ($frameNum) = @_;

    if ($frameNum <= 10) {
        my $n = $spline20[$frameNum] * 100;
        system("ppmshift $n $tmpdir/first.ppm >$tmpdir/this.ppm");
    } elsif ($frameNum <= 20) {
        my $n;
        $n = $spline20[$frameNum] * 100;
        system("ppmshift $n $tmpdir/first.ppm >$tmpdir/junk1a.ppm");
        $n = (1-$spline20[$frameNum-10])*100;
        system("ppmshift $n $tmpdir/last.ppm >$tmpdir/junk2a.ppm");
        $n = $spline10[$frameNum-10];
        system("ppmmix $n $tmpdir/junk1a.ppm $tmpdir/junk2a.ppm " .
               ">$tmpdir/this.ppm");
    } else {
        my $n = (1-$spline20[$frameNum-10]) * 100;
        system("ppmshift $n $tmpdir/last.ppm >$tmpdir/this.ppm");
    }
}



sub reliefOne($) {
    my ($frameNum) = @_;

    if ($frameNum == 1) {
        system("ppmrelief $tmpdir/first.ppm >$tmpdir/junk1r.ppm");
    }
    if ($frameNum <= 10) {
        my $n = $spline10[$frameNum];
        system("ppmmix $n $tmpdir/first.ppm $tmpdir/junk1r.ppm " .
               ">$tmpdir/this.ppm");
    } elsif ($frameNum <= 20) {
        my $n = $spline10[$frameNum-10];
        system("ppmmix $n $tmpdir/junk1r.ppm $tmpdir/junk2r.ppm " .
               ">$tmpdir/this.ppm");
    } else {
        my $n = $spline10[$frameNum-20];
        system("ppmmix $n $tmpdir/junk2r.ppm $tmpdir/last.ppm " .
               ">$tmpdir/this.ppm");
    }
    if ($frameNum == 10) {
        system("ppmrelief $tmpdir/last.ppm >$tmpdir/junk2r.ppm");
    }
}



sub oilOne($) {
    my ($frameNum) = @_;

    if ($frameNum == 1) {
        system("ppmtopgm $tmpdir/first.ppm | pgmoil >$tmpdir/junko.ppm");
        system("rgb3toppm " .
               "$tmpdir/junko.ppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
               ">$tmpdir/junk1o.ppm");
    }
    if ($frameNum <= 10) {
        my $n = $spline10[$frameNum];
        system("ppmmix $n $tmpdir/first.ppm $tmpdir/junk1o.ppm " .
               ">$tmpdir/this.ppm");
    } elsif ($frameNum <= 20) {
        my $n = $spline10[$frameNum-10];
        system("ppmmix $n $tmpdir/junk1o.ppm $tmpdir/junk2o.ppm " .
               ">$tmpdir/this.ppm");
    } else {
        my $n = $spline10[$frameNum-20];
        system("ppmmix $n $tmpdir/junk2o.ppm $tmpdir/last.ppm " .
               ">$tmpdir/this.ppm");
    }
    if ($frameNum == 10) {
        system("ppmtopgm $tmpdir/last.ppm | pgmoil >$tmpdir/junko.ppm");
        system("rgb3toppm " .
               "$tmpdir/junko.ppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
               ">$tmpdir/junk2o.ppm");
    }
}



sub edgeOne($) {
    my ($frameNum) = @_;

    if ($frameNum == 1) {
        system("ppmtopgm $tmpdir/first.ppm | pgmedge >$tmpdir/junko.ppm");
        system("rgb3toppm " .
               "$tmpdir/junko.ppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
               ">$tmpdir/junk1o.ppm");
    }
    if ($frameNum <= 10) {
        my $n = $spline10[$frameNum];
        system("ppmmix $n $tmpdir/first.ppm $tmpdir/junk1o.ppm " .
               ">$tmpdir/this.ppm");
    } elsif ($frameNum <= 20) {
        my $n = $spline10[$frameNum-10];
        system("ppmmix $n $tmpdir/junk1o.ppm $tmpdir/junk2o.ppm " .
               ">$tmpdir/this.ppm");
    } else {
        my $n = $spline10[$frameNum-20];
        system("ppmmix $n $tmpdir/junk2o.ppm $tmpdir/last.ppm " .
               ">$tmpdir/this.ppm");
    }
    if ($frameNum == 10) {
        system("ppmtopgm $tmpdir/last.ppm | pgmedge >$tmpdir/junko.ppm");
        system("rgb3toppm " .
               "$tmpdir/junko.ppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
               ">$tmpdir/junk2o.ppm");
    }
}



sub bentleyOne($) {
    my ($frameNum) = @_;

    if ($frameNum == 1) {
        system("ppmtopgm $tmpdir/first.ppm | pgmbentley >$tmpdir/junko.ppm");
        system("rgb3toppm " .
               "$tmpdir/junko.ppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
               ">$tmpdir/junk1o.ppm");
    }
    if ($frameNum <= 10) {
        my $n = $spline10[$frameNum];
        system("ppmmix $n $tmpdir/first.ppm $tmpdir/junk1o.ppm " .
               ">$tmpdir/this.ppm");
    } elsif ($frameNum <= 20) {
        my $n = $spline10[$frameNum-10];
        system("ppmmix $n $tmpdir/junk1o.ppm $tmpdir/junk2o.ppm " .
               ">$tmpdir/this.ppm");
    } else {
        my $n = $spline10[$frameNum-20];
        system("ppmmix $n $tmpdir/junk2o.ppm $tmpdir/last.ppm " .
               ">$tmpdir/this.ppm");
    }
    if ($frameNum == 10) {
        system("ppmtopgm $tmpdir/last.ppm | pgmbentley " .
               ">$tmpdir/junko.ppm");
        system("rgb3toppm $tmpdir/junko.ppm $tmpdir/junko.ppm " .
               "$tmpdir/junko.ppm " .
               ">$tmpdir/junk2o.ppm");
    }
}



sub blockOne($$$) {
    my ($frameNum, $width, $height) = @_;

    if ($frameNum <= 10) {
        my $n = 1 - 1.9*$spline20[$frameNum];
        system("pamscale $n $tmpdir/first.ppm | " .
               "pamscale -width $width -height $height " .
               ">$tmpdir/this.ppm");
        if ($frameNum == 10) {
            system("cp", "$tmpdir/this.ppm", "$tmpdir/junk1a.ppm");
            system("pamscale $n $tmpdir/last.ppm | " .
                   "pamscale -width $width -height $height " .
                   ">$tmpdir/junk2a.ppm");
        }
    } elsif ($frameNum <= 20) {
        my $n = $spline10[$frameNum-10];
        system("ppmmix $n $tmpdir/junk1a.ppm $tmpdir/junk2a.ppm " .
               ">$tmpdir/this.ppm");
    } else {
        my $n = 1 - 1.9*$spline20[31-$frameNum];
        system("pamscale $n $tmpdir/last.ppm | " .
               "pamscale -width $width -height $height " .
               ">$tmpdir/this.ppm");
    }
}



sub mixOne($$) {
    my ($frameNum, $nFrame) = @_;

    my $fadeFactor = sqrt(1/($nFrame - $frameNum + 1));

    system("ppmmix $fadeFactor $tmpdir/first.ppm $tmpdir/last.ppm " .
           ">$tmpdir/this.ppm");
}



sub fade($$$$$) {
    my ($nFrame, $mode, $width, $height, $baseNm) = @_;
#-----------------------------------------------------------------------------
#  Do the fade starting, from image "first.ppm" in the temporary direct
#  $tmpdir and ending up at "last.ppm", creating $nFrame frames, including
#  the first and last.
#
#  Make the outputs $width by $height.
#
#  Name the result files $baseNm.NNNN.ppm.
#-----------------------------------------------------------------------------

    # Here's what our temporary files are:
    #   first.ppm: The original (fade-from) image
    #   last.ppm: The target (fade-from) image
    #   this.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 $frameNum;    # Frame number
    for ($frameNum = 1; $frameNum <= $nFrame; ++$frameNum) {

        print("Creating Frame $frameNum of $nFrame...\n");

        if ($mode eq 'SPREAD') {
            spreadOne($frameNum);
        } elsif ($mode eq 'SHIFT') {
            shiftOne($frameNum);
        } elsif ($mode eq 'RELIEF') {
            reliefOne($frameNum);
        } elsif ($mode eq 'OIL') {
            oilOne($frameNum);
        } elsif ($mode eq 'EDGE') {
            edgeOne($frameNum);
        } elsif ($mode eq 'BENTLEY') {
            bentleyOne($frameNum);
        } elsif ($mode eq 'BLOCK') {
            blockOne($frameNum, $width, $height);
        } elsif ($mode eq 'MIX') {
            mixOne($frameNum, $nFrame);
        } else {
            print("INTERNAL ERROR: impossible mode value '$mode'\n");
        }

        my $outFileNm = sprintf("%s.%04d.ppm", $baseNm, $frameNum);
        system("cp", "$tmpdir/this.ppm", $outFileNm);
    }
}



#-----------------------------------------------------------------------------
#  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;
}

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");

fade($nFrame, $mode, $width, $height, $baseNm);

exit(0);