3 # vidwhacker, for xscreensaver. Copyright (c) 1998 Jamie Zawinski.
5 # This script grabs a frame of video, then uses various pbm filters to
6 # munge the image in random nefarious ways, then uses xv to put it on
7 # the root window. This works out really nicely if you just feed some
8 # random TV station into it...
10 # The video grabbing part is SGI-specific -- if you want to use this on
11 # another system, add a new clause to the grab() procedure.
14 # Process command-line args...
23 tmp_rgb=$tmp-00000.rgb
45 echo "usage: $0 [ -root | -verbose | -stdin ]" >&2
54 if [ "$onroot" = true ]; then
55 xvargs="$xvargs -root -rmode 5 -noresetroot -rfg black -rbg black -viewonly"
57 xvargs="$xvargs -geom +0+0"
60 screen_width=`xdpyinfo | sed -n 's/.* dimensions: *\([0-9]*\).*/\1/p'`
65 rm -f $tmp_rgb $tmp_ppm1 $tmp_ppm2 $tmp_ppm3 $tmp_ppm4
74 # Grab a frame of video.
78 if [ $uname = IRIX ]; then
80 # SGI's "vidtomem" returns an SGI RGB image of the default video input,
81 # and has stupid non-overridable ouput-file-naming conventions. So, let
82 # it write its file; and then convert it to a pgm.
86 sgitopnm $tmp_rgb > $tmp_ppm1
88 # Cut off the close-captioning blips in the NTSC overscan region. YMMV.
89 # | pnmcut 12 7 695 477
91 elif [ $uname = Linux ]; then
93 # Marcus Herbert says the following works with his Connectix Qcam.
94 # Don't have qcam? Well, do something else then... and send me a patch.
98 # Friedrich Delgado Friedrichs says the following works with a
99 # Brooktree 848 or 878 tuner card:
101 # bttvgrab -Q -d q -l 1 -F /dev/null -o gif -f ${tmp}.gif -N PAL
102 # giftopnm ${tmp}.gif > $tmp_ppm1
105 # He notes that you might need to run a TV application (e.g., xawtv)
106 # before the first time you run vidwhacker in order to initialize the
107 # tuner card and kernel modules.
111 echo "$0: don't know how to grab video on this OS." >&2
118 # Use perl to pick a random foreground/background color in pbm's syntax.
122 printf("#%02x%02x%02x-#%02x%02x%02x",
128 120+int(rand()*135))'
131 # Frobnicate the image in some random way.
135 w_h=`head -2 $tmp_ppm1 | tail -1`
136 width=`echo $w_h | awk '{print $1}'`
137 height=`echo $w_h | awk '{print $2}'`
139 N=`perl -e 'srand; print int(rand() * 17)'`
141 if [ "$verbose" = true ]; then
142 echo "mode $N..." >&2
146 ppmtopgm $tmp_ppm1 | pgmedge | pgmtoppm `randcolor` | ppmnorm
148 elif [ $N = 1 ]; then
153 elif [ $N = 2 ]; then
154 ppmtopgm $tmp_ppm1 | pgmoil | pgmtoppm `randcolor`
156 elif [ $N = 3 ]; then
157 ppmrelief $tmp_ppm1 | ppmtopgm | pgmedge | ppmrelief | ppmtopgm |
158 pgmedge | pnminvert | pgmtoppm `randcolor`
160 elif [ $N = 4 ]; then
161 ppmspread 71 $tmp_ppm1 > $tmp_ppm2
162 pnmarith -add $tmp_ppm1 $tmp_ppm2
164 elif [ $N = 5 ]; then
165 pnmflip -lr $tmp_ppm1 > $tmp_ppm2
166 pnmarith -multiply $tmp_ppm1 $tmp_ppm2 > $tmp_ppm3
167 pnmflip -tb $tmp_ppm3 | ppmnorm > $tmp_ppm2
168 pnmarith -multiply $tmp_ppm1 $tmp_ppm2
170 elif [ $N = 6 ]; then
171 N2=`perl -e 'srand; print int(rand() * 3)'`
173 pnmflip -lr $tmp_ppm1 > $tmp_ppm2
174 elif [ $N2 = 1 ]; then
175 pnmflip -tb $tmp_ppm1 > $tmp_ppm2
177 pnmflip -lr $tmp_ppm1 > $tmp_ppm2
178 pnmflip -tb $tmp_ppm2 > $tmp_ppm3
179 cp $tmp_ppm3 $tmp_ppm2
182 pnmarith -difference $tmp_ppm1 $tmp_ppm2
184 elif [ $N = 7 ]; then
187 ppmtopgm $tmp_ppm1 | pgmedge > $tmp_ppm2
188 pnmarith -difference $tmp_ppm1 $tmp_ppm2 > $tmp_ppm3
189 cp $tmp_ppm3 $tmp_ppm1
193 elif [ $N = 8 ]; then
194 pnmflip -lr $tmp_ppm1 > $tmp_ppm2
195 pnmarith -multiply $tmp_ppm1 $tmp_ppm2 | ppmrelief | ppmnorm | pnminvert
197 elif [ $N = 9 ]; then
198 pnmflip -lr $tmp_ppm1 > $tmp_ppm2
199 pnmarith -subtract $tmp_ppm1 $tmp_ppm2 | ppmrelief | ppmtopgm | pgmedge
201 elif [ $N = 10 ]; then
202 ppmtopgm $tmp_ppm1 | pgmbentley | pgmtoppm `randcolor`
204 elif [ $N = 11 ]; then
205 pgmcrater -number 20000 -height $height -width $width | pgmtoppm `randcolor` > $tmp_ppm2
206 pnmarith -difference $tmp_ppm1 $tmp_ppm2 > $tmp_ppm3
207 pnmflip -tb $tmp_ppm3 | ppmnorm > $tmp_ppm2
208 pnmarith -multiply $tmp_ppm1 $tmp_ppm2
210 elif [ $N = 12 ]; then
211 ppmshift 30 $tmp_ppm1 | ppmtopgm | pgmoil | pgmedge | pgmtoppm `randcolor` > $tmp_ppm2
212 pnmarith -difference $tmp_ppm1 $tmp_ppm2
214 elif [ $N = 13 ]; then
215 ppmpat -madras $width $height | pnmdepth 255 > $tmp_ppm2
216 pnmarith -difference $tmp_ppm1 $tmp_ppm2
218 elif [ $N = 14 ]; then
219 ppmpat -tartan $width $height | pnmdepth 255 > $tmp_ppm2
220 pnmarith -difference $tmp_ppm1 $tmp_ppm2
222 elif [ $N = 15 ]; then
223 ppmpat -camo $width $height | pnmdepth 255 | ppmshift 50 > $tmp_ppm2
224 pnmarith -multiply $tmp_ppm1 $tmp_ppm2
226 elif [ $N = 16 ]; then
227 pgmnoise $width $height | pgmedge | pgmtoppm `randcolor` > $tmp_ppm2
228 pnmarith -difference $tmp_ppm1 $tmp_ppm2 | pnmdepth 255 | pnmsmooth
235 # Grab a frame and frob it. leave it in $tmp_ppm3.
240 while [ ! -f $tmp_ppm1 ]; do
241 if [ "$use_stdin" != true ]; then
244 cp $tmp_ppm0 $tmp_ppm1
249 frob | pnmscale -width $screen_width > $tmp_ppm3
250 rm -f $tmp_ppm1 $tmp_ppm2
254 # Kill off the xv subprocess, if it's running
257 if [ "$pid" != "" ]; then
259 if [ "$verbose" = true ]; then
260 echo "killing pid $pid..." >&2
263 # need to do this to avoid "6898 Terminated" messages!
264 # apparently one can't redirect the output of the builtin `kill' command.
265 # ( sh -c "kill $pid" ) >/dev/null 2>/dev/null </dev/null
267 # wtf? that doesn't work either. Is it writing to /dev/tty??
274 # called when this process is signalled (for cleanup)
277 if [ "$verbose" = true ]; then
278 echo "trapped signal!" >&2
289 trap my_trap 0 1 2 3 6 9 13 15
291 if [ "$use_stdin" = true ]; then
297 # Loop grabbing and frobbing images.
299 # If we're running on the root, run xv in the foreground (with -exit)
302 # If we're running in a window, spawn xv in the background; then when
303 # it's time to put up the new image, kill off the currently-running xv.
305 if [ "$verbose" = true ]; then
313 if [ ! -s $tmp_ppm3 ]; then
314 echo "$0: no image grabbed" >&2
317 pnmtosgi < $tmp_ppm3 > $tmp_ppm2
320 if [ -s $tmp_ppm2 ]; then
321 if [ "$verbose" = true ]; then
322 echo "launching xv $xvargs $tmp_ppm2" >&2
326 mv $tmp_ppm2 $tmp_ppm0
327 xv $xvargs $tmp_ppm0 &
329 # this doesn't work -- leaves xv processes around, instead of stray xset
332 # # cat the file so that we can nuke it without racing against xv.
333 # cat $tmp_ppm2 | xv $xvargs - &
348 # to find stray xv data:
349 # xwininfo -root -children|grep 'xv image comments' | awk '{print "xkill -id ", $1}'