2 # vidwhacker, for xscreensaver. Copyright (c) 1998-2003 Jamie Zawinski.
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation. No representations are made about the suitability of this
9 # software for any purpose. It is provided "as is" without express or
12 # This program grabs a frame of video, then uses various pbm filters to
13 # munge the image in random nefarious ways, then uses xloadimage, xli, or xv
14 # to put it on the root window. This works out really nicely if you just
15 # feed some random TV station into it...
23 my $progname = $0; $progname =~ s@.*/@@g;
24 my $version = q{ $Revision: 1.24 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
34 my $screen_width = -1;
38 # #### This list was lifted from driver/xscreensaver-getimage-file
40 # These are programs that can be used to put an image file on the root
41 # window (including virtual root windows.) The first one of these programs
42 # that exists on $PATH will be used (with the file name as the last arg.)
44 # If you add other programs to this list, please let me know!
46 my @displayer_programs = (
47 "xscreensaver-getimage -root -file",
48 # "xv -root -quit -viewonly -maxpect -noresetroot -quick24 -rmode 5" .
49 # " -rfg black -rbg black",
50 # "xli -quiet -fullscreen -onroot -center -border black",
51 # "xloadimage -quiet -fullscreen -onroot -center -border black",
52 # "chbg -once -xscreensaver -max_grow 4",
54 # this lame program wasn't built with vroot.h:
55 # "xsri -scale -keep-aspect -center-horizontal -center-vertical",
58 # apparently some versions of netpbm call it "pamoil" instead of "pgmoil"...
60 my $pgmoil = (which("pamoil") ? "pamoil" : "pgmoil");
63 # List of interesting PPM filter pipelines.
64 # In this list, the following magic words may be used:
66 # COLORS a randomly-selected pair of RGB foreground/background colors.
67 # FILE1 the (already-existing) input PPM file (ok to overwrite it).
68 # FILE2-FILE4 names of other tmp files you can use.
70 # These commands should read from FILE1, and write to stdout.
71 # All tmp files will be deleted afterward.
74 "ppmtopgm FILE1 | pgmedge | pgmtoppm COLORS | ppmnorm",
75 "ppmtopgm FILE1 | pgmenhance | pgmtoppm COLORS",
76 "ppmtopgm FILE1 | $pgmoil | pgmtoppm COLORS",
77 "ppmtopgm FILE1 | pgmbentley | pgmtoppm COLORS",
79 "ppmrelief FILE1 | ppmtopgm | pgmedge | ppmrelief | ppmtopgm |" .
80 " pgmedge | pnminvert | pgmtoppm COLORS",
82 "ppmspread 71 FILE1 > FILE2 ; " .
83 " pnmarith -add FILE1 FILE2 ; ",
85 "pnmflip -lr < FILE1 > FILE2 ; " .
86 " pnmarith -multiply FILE1 FILE2 > FILE3 ; " .
87 " pnmflip -tb FILE3 | ppmnorm > FILE2 ; " .
88 " pnmarith -multiply FILE1 FILE2",
90 "pnmflip -lr FILE1 > FILE2 ; " .
91 " pnmarith -difference FILE1 FILE2",
93 "pnmflip -tb FILE1 > FILE2 ; " .
94 " pnmarith -difference FILE1 FILE2",
96 "pnmflip -lr FILE1 | pnmflip -tb > FILE2 ; " .
97 " pnmarith -difference FILE1 FILE2",
99 "ppmtopgm < FILE1 | pgmedge > FILE2 ; " .
100 " pnmarith -difference FILE1 FILE2 > FILE3 ; " .
101 " cp FILE3 FILE1 ; " .
102 " ppmtopgm < FILE1 | pgmedge > FILE2 ; " .
103 " pnmarith -difference FILE1 FILE2 > FILE3 ; " .
106 "pnmflip -lr < FILE1 > FILE2 ; " .
107 " pnmarith -multiply FILE1 FILE2 | ppmrelief | ppmnorm | pnminvert",
109 "pnmflip -lr FILE1 > FILE2 ; " .
110 " pnmarith -subtract FILE1 FILE2 | ppmrelief | ppmtopgm | pgmedge",
112 "pgmcrater -number 20000 -width WIDTH -height HEIGHT FILE1 | " .
113 " pgmtoppm COLORS > FILE2 ; " .
114 " pnmarith -difference FILE1 FILE2 > FILE3 ; " .
115 " pnmflip -tb FILE3 | ppmnorm > FILE2 ; " .
116 " pnmarith -multiply FILE1 FILE2",
118 "ppmshift 30 FILE1 | ppmtopgm | $pgmoil | pgmedge | " .
119 " pgmtoppm COLORS > FILE2 ; " .
120 " pnmarith -difference FILE1 FILE2",
122 "ppmpat -madras WIDTH HEIGHT | pnmdepth 255 > FILE2 ; " .
123 " pnmarith -difference FILE1 FILE2",
125 "ppmpat -tartan WIDTH HEIGHT | pnmdepth 255 > FILE2 ; " .
126 " pnmarith -difference FILE1 FILE2",
128 "ppmpat -camo WIDTH HEIGHT | pnmdepth 255 | ppmshift 50 > FILE2 ; " .
129 " pnmarith -multiply FILE1 FILE2",
131 "pgmnoise WIDTH HEIGHT | pgmedge | pgmtoppm COLORS > FILE2 ; " .
132 " pnmarith -difference FILE1 FILE2 | pnmdepth 255 | pnmsmooth",
138 print STDERR "$progname: $_\n";
142 # Any files on this list will be deleted at exit.
144 my @all_tmpfiles = ();
147 unlink @all_tmpfiles;
152 $SIG{HUP} = \&signal_cleanup;
153 $SIG{INT} = \&signal_cleanup;
154 $SIG{QUIT} = \&signal_cleanup;
155 $SIG{ABRT} = \&signal_cleanup;
156 $SIG{KILL} = \&signal_cleanup;
157 $SIG{TERM} = \&signal_cleanup;
159 # Need this so that if giftopnm dies, we don't die.
160 $SIG{PIPE} = 'IGNORE';
163 END { signal_cleanup(); }
168 # #### Lifted from driver/xscreensaver-getimage-file
173 foreach my $cmd (@displayer_programs) {
175 my ($name) = m/^([^ ]+)/;
176 push @names, "\"$name\"";
177 print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
178 foreach my $dir (split (/:/, $ENV{PATH})) {
179 print STDERR "$progname: checking $dir/$name\n" if ($verbose > 3);
180 return $cmd if (-x "$dir/$name");
184 $names[$#names] = "or " . $names[$#names];
185 printf STDERR "$progname: none of: " . join (", ", @names) .
186 " were found on \$PATH.\n";
191 # returns the full path of the named program, or undef.
195 foreach (split (/:/, $ENV{PATH})) {
204 # Choose random foreground and background colors
207 return sprintf ("#%02x%02x%02x-#%02x%02x%02x",
213 120+int(rand()*135));
219 my ($filter, $width, $height, @tmpfiles) = @_;
220 my $colors = randcolors();
221 $filter =~ s/\bWIDTH\b/$width/g;
222 $filter =~ s/\bHEIGHT\b/$height/g;
223 $filter =~ s/\bCOLORS\b/'$colors'/g;
225 foreach my $t (@tmpfiles) {
226 $filter =~ s/\bFILE$i\b/$t/g;
229 if ($filter =~ m/([A-Z]+)/) {
230 error "internal error: what is \"$1\"?";
236 # Frobnicate the image in some random way.
242 error "0-length data" if (!defined($ppm_data) || $ppm_data eq "");
243 error "not a PPM file" unless (m/^P\d\n/s);
244 my ($width, $height) = m/^P\d\n(\d+) (\d+)\n/s;
245 error "got a bogus PPM" unless ($width && $height);
247 my $tmpdir = $ENV{TMPDIR};
248 $tmpdir = "/tmp" unless $tmpdir;
249 my $fn = sprintf ("%s/vidwhacker-0-%08x", $tmpdir, rand(0xFFFFFFFF));
250 my $fn1 = sprintf ("%s/vidwhacker-1-%08x", $tmpdir, rand(0xFFFFFFFF));
251 my $fn2 = sprintf ("%s/vidwhacker-2-%08x", $tmpdir, rand(0xFFFFFFFF));
252 my $fn3 = sprintf ("%s/vidwhacker-3-%08x", $tmpdir, rand(0xFFFFFFFF));
253 my @files = ( "$fn", "$fn1", "$fn2", "$fn3" );
254 push @all_tmpfiles, @files;
256 my $n = int(rand($#filters+1));
257 my $filter = $filters[$n];
260 printf STDERR "$progname: running filter $n\n";
261 } elsif ($verbose > 1) {
265 $f =~ s/ *\|/\n\t|/g;
266 $f =~ s/ *\; */ ;\n\t/g;
267 print STDERR "$progname: filter $n:\n\n$f\n\n" if $verbose;
270 $filter = filter_subst ($filter, $width, $height, @files);
275 open (OUT, ">$files[0]") || error ("writing $files[0]: $!");
279 $filter = "( $filter )";
280 $filter .= "2>/dev/null" unless ($verbose > 1);
283 open (IN, "$filter |") || error ("opening pipe: $!");
285 while (<IN>) { $ppm_data .= $_; }
294 my $conf = "$ENV{HOME}/.xscreensaver";
296 my $had_dir = defined($imagedir);
299 open (IN, "<$conf") || error "reading $conf: $!";
301 if (!$imagedir && m/^imageDirectory:\s+(.*)\s*$/i) { $imagedir = $1; }
302 elsif (m/^grabVideoFrames:\s+true\s*$/i) { $video_p = 1; }
303 elsif (m/^grabVideoFrames:\s+false\s*$/i) { $video_p = 0; }
304 elsif (m/^chooseRandomImages:\s+true\s*$/i) { $file_p = 1; }
305 elsif (m/^chooseRandomImages:\s+false\s*$/i) { $file_p = 0; }
309 $file_p = 1 if $had_dir;
311 $imagedir = undef unless ($imagedir && $imagedir ne '');
313 if (!$file_p && !$video_p) {
314 # error "neither grabVideoFrames nor chooseRandomImages are set\n\t" .
315 # "in $conf; $progname requires one or both."
320 error "no imageDirectory set in $conf" unless $imagedir;
321 error "imageDirectory $imagedir doesn't exist" unless (-d $imagedir);
325 printf STDERR "$progname: grab video: $video_p\n";
326 printf STDERR "$progname: grab images: $file_p\n";
327 printf STDERR "$progname: directory: $imagedir\n";
335 print STDERR "$progname: reading from stdin\n" if ($verbose > 1);
337 while (<STDIN>) { $ppm .= $_; }
344 if ($file_p && $video_p) {
345 $do_file_p = (int(rand(2)) == 0);
346 print STDERR "$progname: doing " . ($do_file_p ? "files" : "video") ."\n"
349 elsif ($file_p) { $do_file_p = 1; }
350 elsif ($video_p) { $do_file_p = 0; }
352 error "internal error: not grabbing files or video?";
355 my $v = ($verbose <= 1 ? "" : "-" . ("v" x ($verbose-1)));
358 $cmd = "xscreensaver-getimage-file $v --name \"$imagedir\"";
360 $cmd = "xscreensaver-getimage-video $v --stdout";
367 print STDERR "$progname: running: $cmd\n" if ($verbose > 1);
370 error "didn't get a file?" if ($fn eq "");
372 print STDERR "$progname: selected file $fn\n" if ($verbose > 1);
374 if ($fn =~ m/\.gif/i) { $cmd = "giftopnm < \"$fn\""; }
375 elsif ($fn =~ m/\.jpe?g/i) { $cmd = "djpeg < \"$fn\""; }
376 elsif ($fn =~ m/\.png/i) { $cmd = "pngtopnm < \"$fn\""; }
377 elsif ($fn =~ m/\.xpm/i) { $cmd = "xpmtoppm < \"$fn\""; }
378 elsif ($fn =~ m/\.bmp/i) { $cmd = "bmptoppm < \"$fn\""; }
379 elsif ($fn =~ m/\.tiff?/i) { $cmd = "tifftopnm < \"$fn\""; }
380 elsif ($fn =~ m/\.p[bgp]m/i) { return `cat \"$fn\"`; }
382 error "unrecognized file extension on $fn";
385 print STDERR "$progname: converting with: $cmd\n" if ($verbose > 1);
386 $cmd .= " 2>/dev/null" unless ($verbose > 1);
391 print STDERR "$progname: running: $cmd\n" if ($verbose > 1);
393 error "no data?" if ($ppm eq "");
394 error "not a PPM file" unless ($ppm =~ m/^P\d\n/s);
397 my ($width, $height) = m/^P\d\n(\d+) (\d+)\n/s;
398 error "got a bogus PPM" unless ($width && $height);
399 print STDERR "$progname: grabbed ${width}x$height PPM\n"
411 error "0-length data" if (!defined($ppm) || $ppm eq "");
412 error "not a PPM file" unless ($ppm =~ m/^P\d\n/s);
415 print STDERR "$progname: writing to stdout\n" if ($verbose > 1);
419 my $displayer = pick_displayer();
421 my $tmpdir = $ENV{TMPDIR};
422 $tmpdir = "/tmp" unless $tmpdir;
423 my $fn = sprintf ("%s/vidwhacker-%08x", $tmpdir, rand(0xFFFFFFFF));
426 push @all_tmpfiles, $fn;
427 open (OUT, ">$fn") || error "writing $fn: $!";
431 my @cmd = split (/ +/, $displayer);
433 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
442 my $stdin_ppm = undef;
447 if (!defined($stdin_ppm)) {
448 $stdin_ppm = get_ppm();
452 my $max_err_count = 20;
454 while (!defined($ppm)) {
456 $err_count++ if (!defined ($ppm));
457 error ("too many errors, too few images!")
458 if ($err_count > $max_err_count);
462 $ppm = frob_ppm ($ppm);
469 print STDERR "VidWhacker, Copyright (c) 2001 Jamie Zawinski <jwz\@jwz.org>\n";
470 print STDERR " http://www.jwz.org/xscreensaver/";
472 print STDERR "usage: $0 [-display dpy] [-verbose] [-root | -window]\n";
473 print STDERR " [-stdin] [-stdout] [-delay secs]\n";
474 print STDERR " [-directory image_directory]\n";
479 while ($_ = $ARGV[0]) {
481 if ($_ eq "--verbose") { $verbose++; }
482 elsif (m/^-v+$/) { $verbose += length($_)-1; }
483 elsif (m/^(-display|-disp|-dis|-dpy|-d)$/) { $ENV{DISPLAY} = shift @ARGV; }
484 elsif (m/^--?stdin$/) { $use_stdin = 1; }
485 elsif (m/^--?stdout$/) { $use_stdout = 1; }
486 elsif (m/^--?delay$/) { $delay = shift @ARGV; }
487 elsif (m/^--?dir(ectory)?$/) { $imagedir = shift @ARGV; }
488 elsif (m/^--?root$/) { }
489 elsif (m/^--?window$/) {
490 print STDERR "$progname: sorry, \"-window\" is unimplemented.\n";
491 print STDERR "$progname: use \"-stdout\" and pipe to a displayer.\n";
494 elsif (m/^-./) { usage; }
503 $_ = `xdpyinfo 2>&-`;
504 ($screen_width) =~ m/ dimensions: +(\d+)x(\d+) pixels/;
505 $screen_width = 800 unless $screen_width > 0;