2 # vidwhacker, for xscreensaver. Copyright (c) 1998-2001 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.18 $ }; $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 "xv -root -quit -viewonly -maxpect -noresetroot -quick24 -rmode 5" .
48 " -rfg black -rbg black",
49 "xli -quiet -fullscreen -onroot -center -border black",
50 "xloadimage -quiet -fullscreen -onroot -center -border black",
51 "chbg -once -xscreensaver -max_grow 4",
53 # this lame program wasn't built with vroot.h:
54 # "xsri -scale -keep-aspect -center-horizontal -center-vertical",
58 # List of interesting PPM filter pipelines.
59 # In this list, the following magic words may be used:
61 # COLORS a randomly-selected pair of RGB foreground/background colors.
62 # FILE1 the (already-existing) input PPM file (ok to overwrite it).
63 # FILE2-FILE4 names of other tmp files you can use.
65 # These commands should read from FILE1, and write to stdout.
66 # All tmp files will be deleted afterward.
69 "ppmtopgm FILE1 | pgmedge | pgmtoppm COLORS | ppmnorm",
70 "ppmtopgm FILE1 | pgmenhance | pgmtoppm COLORS",
71 "ppmtopgm FILE1 | pgmoil | pgmtoppm COLORS",
72 "ppmtopgm FILE1 | pgmbentley | pgmtoppm COLORS",
74 "ppmrelief FILE1 | ppmtopgm | pgmedge | ppmrelief | ppmtopgm |" .
75 " pgmedge | pnminvert | pgmtoppm COLORS",
77 "ppmspread 71 FILE1 > FILE2 ; " .
78 " pnmarith -add FILE1 FILE2 ; ",
80 "pnmflip -lr < FILE1 > FILE2 ; " .
81 " pnmarith -multiply FILE1 FILE2 > FILE3 ; " .
82 " pnmflip -tb FILE3 | ppmnorm > FILE2 ; " .
83 " pnmarith -multiply FILE1 FILE2",
85 "pnmflip -lr FILE1 > FILE2 ; " .
86 " pnmarith -difference FILE1 FILE2",
88 "pnmflip -tb FILE1 > FILE2 ; " .
89 " pnmarith -difference FILE1 FILE2",
91 "pnmflip -lr FILE1 | pnmflip -tb > FILE2 ; " .
92 " pnmarith -difference FILE1 FILE2",
94 "ppmtopgm < FILE1 | pgmedge > FILE2 ; " .
95 " pnmarith -difference FILE1 FILE2 > FILE3 ; " .
96 " cp FILE3 FILE1 ; " .
97 " ppmtopgm < FILE1 | pgmedge > FILE2 ; " .
98 " pnmarith -difference FILE1 FILE2 > FILE3 ; " .
101 "pnmflip -lr < FILE1 > FILE2 ; " .
102 " pnmarith -multiply FILE1 FILE2 | ppmrelief | ppmnorm | pnminvert",
104 "pnmflip -lr FILE1 > FILE2 ; " .
105 " pnmarith -subtract FILE1 FILE2 | ppmrelief | ppmtopgm | pgmedge",
107 "pgmcrater -number 20000 -width WIDTH -height HEIGHT FILE1 | " .
108 " pgmtoppm COLORS > FILE2 ; " .
109 " pnmarith -difference FILE1 FILE2 > FILE3 ; " .
110 " pnmflip -tb FILE3 | ppmnorm > FILE2 ; " .
111 " pnmarith -multiply FILE1 FILE2",
113 "ppmshift 30 FILE1 | ppmtopgm | pgmoil | pgmedge | " .
114 " pgmtoppm COLORS > FILE2 ; " .
115 " pnmarith -difference FILE1 FILE2",
117 "ppmpat -madras WIDTH HEIGHT | pnmdepth 255 > FILE2 ; " .
118 " pnmarith -difference FILE1 FILE2",
120 "ppmpat -tartan WIDTH HEIGHT | pnmdepth 255 > FILE2 ; " .
121 " pnmarith -difference FILE1 FILE2",
123 "ppmpat -camo WIDTH HEIGHT | pnmdepth 255 | ppmshift 50 > FILE2 ; " .
124 " pnmarith -multiply FILE1 FILE2",
126 "pgmnoise WIDTH HEIGHT | pgmedge | pgmtoppm COLORS > FILE2 ; " .
127 " pnmarith -difference FILE1 FILE2 | pnmdepth 255 | pnmsmooth",
133 print STDERR "$progname: $_\n";
137 # #### Lifted from driver/xscreensaver-getimage-file
142 foreach my $cmd (@displayer_programs) {
144 my ($name) = m/^([^ ]+)/;
145 push @names, "\"$name\"";
146 print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
147 foreach my $dir (split (/:/, $ENV{PATH})) {
148 print STDERR "$progname: checking $dir/$name\n" if ($verbose > 3);
149 return $cmd if (-x "$dir/$name");
153 $names[$#names] = "or " . $names[$#names];
154 printf STDERR "$progname: none of: " . join (", ", @names) .
155 " were found on \$PATH.\n";
160 # Choose random foreground and background colors
163 return sprintf ("#%02x%02x%02x-#%02x%02x%02x",
169 120+int(rand()*135));
174 my ($filter, $width, $height, @tmpfiles) = @_;
175 my $colors = randcolors();
176 $filter =~ s/\bWIDTH\b/$width/g;
177 $filter =~ s/\bHEIGHT\b/$height/g;
178 $filter =~ s/\bCOLORS\b/'$colors'/g;
180 foreach my $t (@tmpfiles) {
181 $filter =~ s/\bFILE$i\b/$t/g;
184 if ($filter =~ m/([A-Z]+)/) {
185 error "internal error: what is \"$1\"?";
191 # Frobnicate the image in some random way.
197 error "0-length data" if (!defined($ppm_data) || $ppm_data eq "");
198 error "not a PPM file" unless (m/^P\d\n/s);
199 my ($width, $height) = m/^P\d\n(\d+) (\d+)\n/s;
200 error "got a bogus PPM" unless ($width && $height);
202 my $tmpdir = $ENV{TMPDIR};
203 $tmpdir = "/tmp" unless $tmpdir;
204 my $fn = sprintf("$tmpdir/vw.%04x", $$);
205 my @files = ( "$fn", "$fn.1", "$fn.2", "$fn.3" );
207 my $n = int(rand($#filters+1));
208 my $filter = $filters[$n];
211 printf STDERR "$progname: running filter $n\n";
212 } elsif ($verbose > 1) {
216 $f =~ s/ *\|/\n\t|/g;
217 $f =~ s/ *\; */ ;\n\t/g;
218 print STDERR "$progname: filter $n:\n\n$f\n\n" if $verbose;
221 $filter = filter_subst ($filter, $width, $height, @files);
226 open (OUT, ">$files[0]") || error ("writing $files[0]: $!");
230 $filter = "( $filter )";
231 $filter .= "2>/dev/null" unless ($verbose > 1);
234 open (IN, "$filter |") || error ("opening pipe: $!");
236 while (<IN>) { $ppm_data .= $_; }
245 my $conf = "$ENV{HOME}/.xscreensaver";
247 my $had_dir = defined($imagedir);
250 open (IN, "<$conf") || error "reading $conf: $!";
252 if (!$imagedir && m/^imageDirectory:\s+(.*)\s*$/i) { $imagedir = $1; }
253 elsif (m/^grabVideoFrames:\s+true\s*$/i) { $video_p = 1; }
254 elsif (m/^grabVideoFrames:\s+false\s*$/i) { $video_p = 0; }
255 elsif (m/^chooseRandomImages:\s+true\s*$/i) { $file_p = 1; }
256 elsif (m/^chooseRandomImages:\s+false\s*$/i) { $file_p = 0; }
260 $file_p = 1 if $had_dir;
262 $imagedir = undef unless ($imagedir && $imagedir ne '');
264 if (!$file_p && !$video_p) {
265 # error "neither grabVideoFrames nor chooseRandomImages are set\n\t" .
266 # "in $conf; $progname requires one or both."
271 error "no imageDirectory set in $conf" unless $imagedir;
272 error "imageDirectory $imagedir doesn't exist" unless (-d $imagedir);
276 printf STDERR "$progname: grab video: $video_p\n";
277 printf STDERR "$progname: grab images: $file_p\n";
278 printf STDERR "$progname: directory: $imagedir\n";
286 print STDERR "$progname: reading from stdin\n" if ($verbose > 1);
288 while (<STDIN>) { $ppm .= $_; }
295 if ($file_p && $video_p) {
296 $do_file_p = (int(rand(2)) == 0);
297 print STDERR "$progname: doing " . ($do_file_p ? "files" : "video") ."\n"
300 elsif ($file_p) { $do_file_p = 1; }
301 elsif ($video_p) { $do_file_p = 0; }
303 error "internal error: not grabbing files or video?";
306 my $v = ($verbose <= 1 ? "" : "-" . ("v" x ($verbose-1)));
309 $cmd = "xscreensaver-getimage-file $v --name \"$imagedir\"";
311 $cmd = "xscreensaver-getimage-video $v --stdout";
318 print STDERR "$progname: running \"$cmd\"\n" if ($verbose > 1);
321 error "didn't get a file?" if ($fn eq "");
323 print STDERR "$progname: selected file $fn\n" if ($verbose > 1);
325 if ($fn =~ m/\.gif/i) { $cmd = "giftopnm < \"$fn\""; }
326 elsif ($fn =~ m/\.jpe?g/i) { $cmd = "djpeg < \"$fn\""; }
328 error "unrecognized file extension on $fn";
331 print STDERR "$progname: converting with \"$cmd\"\n" if ($verbose > 1);
332 $cmd .= " 2>/dev/null" unless ($verbose > 1);
337 print STDERR "$progname: running \"$cmd\"\n" if ($verbose > 1);
339 error "no data?" if ($ppm eq "");
340 error "not a PPM file" unless ($ppm =~ m/^P\d\n/s);
343 my ($width, $height) = m/^P\d\n(\d+) (\d+)\n/s;
344 error "got a bogus PPM" unless ($width && $height);
345 print STDERR "$progname: grabbed ${width}x$height PPM\n"
357 error "0-length data" if (!defined($ppm) || $ppm eq "");
358 error "not a PPM file" unless ($ppm =~ m/^P\d\n/s);
361 print STDERR "$progname: writing to stdout\n" if ($verbose > 1);
365 my $displayer = pick_displayer();
367 my $tmpdir = $ENV{TMPDIR};
368 $tmpdir = "/tmp" unless $tmpdir;
369 my $fn = sprintf("$tmpdir/vw.%04x", $$);
372 open (OUT, ">$fn") || error "writing $fn: $!";
376 my @cmd = split (/ +/, $displayer);
378 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
387 my $stdin_ppm = undef;
392 if (!defined($stdin_ppm)) {
393 $stdin_ppm = get_ppm();
400 $ppm = frob_ppm ($ppm);
407 print STDERR "VidWhacker, Copyright (c) 2001 Jamie Zawinski <jwz\@jwz.org>\n";
408 print STDERR " http://www.jwz.org/xscreensaver/";
410 print STDERR "usage: $0 [-display dpy] [-verbose] [-root | -window]\n";
411 print STDERR " [-stdin] [-stdout] [-delay secs]\n";
412 print STDERR " [-directory image_directory]\n";
417 while ($_ = $ARGV[0]) {
419 if ($_ eq "--verbose") { $verbose++; }
420 elsif (m/^-v+$/) { $verbose += length($_)-1; }
421 elsif (m/^(-display|-disp|-dis|-dpy|-d)$/) { $ENV{DISPLAY} = shift @ARGV; }
422 elsif (m/^--?stdin$/) { $use_stdin = 1; }
423 elsif (m/^--?stdout$/) { $use_stdout = 1; }
424 elsif (m/^--?delay$/) { $delay = shift @ARGV; }
425 elsif (m/^--?dir(ectory)?$/) { $imagedir = shift @ARGV; }
426 elsif (m/^--?root$/) { }
427 elsif (m/^--?window$/) {
428 print STDERR "$progname: sorry, \"-window\" is unimplemented.\n";
429 print STDERR "$progname: use \"-stdout\" and pipe to a displayer.\n";
432 elsif (m/^-./) { usage; }
439 $_ = `xdpyinfo 2>&-`;
440 ($screen_width) =~ m/ dimensions: +(\d+)x(\d+) pixels/;
441 $screen_width = 800 unless $screen_width > 0;