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.22 $ }; $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 # #### Lifted from driver/xscreensaver-getimage-file
147 foreach my $cmd (@displayer_programs) {
149 my ($name) = m/^([^ ]+)/;
150 push @names, "\"$name\"";
151 print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
152 foreach my $dir (split (/:/, $ENV{PATH})) {
153 print STDERR "$progname: checking $dir/$name\n" if ($verbose > 3);
154 return $cmd if (-x "$dir/$name");
158 $names[$#names] = "or " . $names[$#names];
159 printf STDERR "$progname: none of: " . join (", ", @names) .
160 " were found on \$PATH.\n";
165 # returns the full path of the named program, or undef.
169 foreach (split (/:/, $ENV{PATH})) {
178 # Choose random foreground and background colors
181 return sprintf ("#%02x%02x%02x-#%02x%02x%02x",
187 120+int(rand()*135));
192 my ($filter, $width, $height, @tmpfiles) = @_;
193 my $colors = randcolors();
194 $filter =~ s/\bWIDTH\b/$width/g;
195 $filter =~ s/\bHEIGHT\b/$height/g;
196 $filter =~ s/\bCOLORS\b/'$colors'/g;
198 foreach my $t (@tmpfiles) {
199 $filter =~ s/\bFILE$i\b/$t/g;
202 if ($filter =~ m/([A-Z]+)/) {
203 error "internal error: what is \"$1\"?";
209 # Frobnicate the image in some random way.
215 error "0-length data" if (!defined($ppm_data) || $ppm_data eq "");
216 error "not a PPM file" unless (m/^P\d\n/s);
217 my ($width, $height) = m/^P\d\n(\d+) (\d+)\n/s;
218 error "got a bogus PPM" unless ($width && $height);
220 my $tmpdir = $ENV{TMPDIR};
221 $tmpdir = "/tmp" unless $tmpdir;
222 my $fn = sprintf("$tmpdir/vw.%04x", $$);
223 my @files = ( "$fn", "$fn.1", "$fn.2", "$fn.3" );
225 my $n = int(rand($#filters+1));
226 my $filter = $filters[$n];
229 printf STDERR "$progname: running filter $n\n";
230 } elsif ($verbose > 1) {
234 $f =~ s/ *\|/\n\t|/g;
235 $f =~ s/ *\; */ ;\n\t/g;
236 print STDERR "$progname: filter $n:\n\n$f\n\n" if $verbose;
239 $filter = filter_subst ($filter, $width, $height, @files);
244 open (OUT, ">$files[0]") || error ("writing $files[0]: $!");
248 $filter = "( $filter )";
249 $filter .= "2>/dev/null" unless ($verbose > 1);
252 open (IN, "$filter |") || error ("opening pipe: $!");
254 while (<IN>) { $ppm_data .= $_; }
263 my $conf = "$ENV{HOME}/.xscreensaver";
265 my $had_dir = defined($imagedir);
268 open (IN, "<$conf") || error "reading $conf: $!";
270 if (!$imagedir && m/^imageDirectory:\s+(.*)\s*$/i) { $imagedir = $1; }
271 elsif (m/^grabVideoFrames:\s+true\s*$/i) { $video_p = 1; }
272 elsif (m/^grabVideoFrames:\s+false\s*$/i) { $video_p = 0; }
273 elsif (m/^chooseRandomImages:\s+true\s*$/i) { $file_p = 1; }
274 elsif (m/^chooseRandomImages:\s+false\s*$/i) { $file_p = 0; }
278 $file_p = 1 if $had_dir;
280 $imagedir = undef unless ($imagedir && $imagedir ne '');
282 if (!$file_p && !$video_p) {
283 # error "neither grabVideoFrames nor chooseRandomImages are set\n\t" .
284 # "in $conf; $progname requires one or both."
289 error "no imageDirectory set in $conf" unless $imagedir;
290 error "imageDirectory $imagedir doesn't exist" unless (-d $imagedir);
294 printf STDERR "$progname: grab video: $video_p\n";
295 printf STDERR "$progname: grab images: $file_p\n";
296 printf STDERR "$progname: directory: $imagedir\n";
304 print STDERR "$progname: reading from stdin\n" if ($verbose > 1);
306 while (<STDIN>) { $ppm .= $_; }
313 if ($file_p && $video_p) {
314 $do_file_p = (int(rand(2)) == 0);
315 print STDERR "$progname: doing " . ($do_file_p ? "files" : "video") ."\n"
318 elsif ($file_p) { $do_file_p = 1; }
319 elsif ($video_p) { $do_file_p = 0; }
321 error "internal error: not grabbing files or video?";
324 my $v = ($verbose <= 1 ? "" : "-" . ("v" x ($verbose-1)));
327 $cmd = "xscreensaver-getimage-file $v --name \"$imagedir\"";
329 $cmd = "xscreensaver-getimage-video $v --stdout";
336 print STDERR "$progname: running: $cmd\n" if ($verbose > 1);
339 error "didn't get a file?" if ($fn eq "");
341 print STDERR "$progname: selected file $fn\n" if ($verbose > 1);
343 if ($fn =~ m/\.gif/i) { $cmd = "giftopnm < \"$fn\""; }
344 elsif ($fn =~ m/\.jpe?g/i) { $cmd = "djpeg < \"$fn\""; }
345 elsif ($fn =~ m/\.png/i) { $cmd = "pngtopnm < \"$fn\""; }
346 elsif ($fn =~ m/\.xpm/i) { $cmd = "xpmtoppm < \"$fn\""; }
347 elsif ($fn =~ m/\.bmp/i) { $cmd = "bmptoppm < \"$fn\""; }
348 elsif ($fn =~ m/\.tiff?/i) { $cmd = "tifftopnm < \"$fn\""; }
349 elsif ($fn =~ m/\.p[bgp]m/i) { return `cat \"$fn\"`; }
351 error "unrecognized file extension on $fn";
354 print STDERR "$progname: converting with: $cmd\n" if ($verbose > 1);
355 $cmd .= " 2>/dev/null" unless ($verbose > 1);
360 print STDERR "$progname: running: $cmd\n" if ($verbose > 1);
362 error "no data?" if ($ppm eq "");
363 error "not a PPM file" unless ($ppm =~ m/^P\d\n/s);
366 my ($width, $height) = m/^P\d\n(\d+) (\d+)\n/s;
367 error "got a bogus PPM" unless ($width && $height);
368 print STDERR "$progname: grabbed ${width}x$height PPM\n"
380 error "0-length data" if (!defined($ppm) || $ppm eq "");
381 error "not a PPM file" unless ($ppm =~ m/^P\d\n/s);
384 print STDERR "$progname: writing to stdout\n" if ($verbose > 1);
388 my $displayer = pick_displayer();
390 my $tmpdir = $ENV{TMPDIR};
391 $tmpdir = "/tmp" unless $tmpdir;
392 my $fn = sprintf("$tmpdir/vw.%04x", $$);
395 open (OUT, ">$fn") || error "writing $fn: $!";
399 my @cmd = split (/ +/, $displayer);
401 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
410 my $stdin_ppm = undef;
415 if (!defined($stdin_ppm)) {
416 $stdin_ppm = get_ppm();
420 my $max_err_count = 20;
422 while (!defined($ppm)) {
424 $err_count++ if (!defined ($ppm));
425 error ("too many errors, too few images!")
426 if ($err_count > $max_err_count);
430 $ppm = frob_ppm ($ppm);
437 print STDERR "VidWhacker, Copyright (c) 2001 Jamie Zawinski <jwz\@jwz.org>\n";
438 print STDERR " http://www.jwz.org/xscreensaver/";
440 print STDERR "usage: $0 [-display dpy] [-verbose] [-root | -window]\n";
441 print STDERR " [-stdin] [-stdout] [-delay secs]\n";
442 print STDERR " [-directory image_directory]\n";
447 while ($_ = $ARGV[0]) {
449 if ($_ eq "--verbose") { $verbose++; }
450 elsif (m/^-v+$/) { $verbose += length($_)-1; }
451 elsif (m/^(-display|-disp|-dis|-dpy|-d)$/) { $ENV{DISPLAY} = shift @ARGV; }
452 elsif (m/^--?stdin$/) { $use_stdin = 1; }
453 elsif (m/^--?stdout$/) { $use_stdout = 1; }
454 elsif (m/^--?delay$/) { $delay = shift @ARGV; }
455 elsif (m/^--?dir(ectory)?$/) { $imagedir = shift @ARGV; }
456 elsif (m/^--?root$/) { }
457 elsif (m/^--?window$/) {
458 print STDERR "$progname: sorry, \"-window\" is unimplemented.\n";
459 print STDERR "$progname: use \"-stdout\" and pipe to a displayer.\n";
462 elsif (m/^-./) { usage; }
469 $_ = `xdpyinfo 2>&-`;
470 ($screen_width) =~ m/ dimensions: +(\d+)x(\d+) pixels/;
471 $screen_width = 800 unless $screen_width > 0;