<html>
<head><title>Voyeur: How'd they do that?</title></head>
<body>

<h2>Voyeur: How'd they do that?</h2>

To get a "live" image on the web, you need a camera and
some kind of backend processing to "serve" the picture.

<p>

We started with an <a href="indyworkstation.GIF">SGI
Indy Workstartion</a> which comes with a convenient color 
camera.  On this machine the camera is running all the
time but you need to call a special routine to "grab"
an image.

<p>

The image is grabbed to disk using the SGI program <strong><tt>vidtomem</tt></strong>,
and then converted to a gif using, <strong><tt>imgcopy</tt></strong>.  Finally,
the image is sent back to the user.  This method is disk intensive and fairly
slow.  If someone can think of a better way to do this with the INDY's tools,
we'd (<a href="mailto:webmaster@berksys.com">webmaster</a>) like to 
hear about it.

<p>

Here's the Perl script that "serves" the gif.

<p>
<hr><pre>

#!/usr/bin/perl -w

# takepic
#
#
# 8.1.95 _ MatK <ripper@berksys.com>
# 5.3.96 _ added $$ to all the names (thanks to Doron!)


$RAWPIC = "/var/tmp/pic$$";
$PICNAME = "/var/tmp/pic$$-00000.rgb";
$GIFNAME = "/var/tmp/pic$$.gif";
$BACKUPGIF = "/var/tmp/pic.backup.gif";

$ERR_LOG = "/var/tmp/pic-error.log";

$VIDTOMEM = "/usr/sbin/vidtomem -f $RAWPIC -c 1";
$IMGCOPY = "/usr/sbin/imgcopy $PICNAME $GIFNAME";
$DELETE = "/sbin/rm";
$COPY = "/sbin/cp";

$DATE = "/sbin/date +\"%D (%r)\"";
$CMD_PS = "/sbin/ps -ef";


sub rerror {
	my ($msg) = @_;
	my ($date) = `$DATE`;
	chomp($date);
	open (ERRh, ">>$ERR_LOG");
	print ERRh "[$$] - $date - $msg\n";
	close (ERRh);
	&killBabys($$);
	return 1;
}

# killBabys ($GID)
#
# Locate and kill all the processes owned by
# $GID.
#
sub killBabys {
	my($GID) = @_;
	my (@procs);

	open (PLIST, "$CMD_PS|");
	while (<PLIST>) {
		chomp;
		if (/^\s*(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s(\d\d:\d\d:\d\d)\s+([^\s]+)\s+(\d+:\d\d)\s(.*)$/) {
			push (@procs, $2) if $3 == $GID;
		}
	}
	close (PLIST);
	kill 9,@procs if @procs;
	return 1;
}



sub giveagif {
	my ($pathName) = @_;

	# Return the gif
	&rerror("Returning backup gif.") if ($pathName eq $BACKUPGIF);
	select(STDOUT); $|=1;				#flush
	print "Content-type: image/gif\n\n";
	open (GIF, $pathName);
	binmode(GIF);
	while (<GIF>) {
		print;
	}
	close (GIF);
	return 1;
}


open (STDERR, ">>/dev/null");

# Take a picture
`$VIDTOMEM`;
(&rerror("[vidtomem] $?"), close(STDERR), &giveagif($BACKUPGIF), exit) if $?;


# Convert to a gif
`$IMGCOPY`;
if ($?) {
	&rerror("[imgcopy] $?");
	`$DELETE $PICNAME`;		# Just in case...
	close (STDERR);
	&giveagif($BACKUPGIF);
	exit;
}


# Delete the first temp file
`$DELETE $PICNAME`;		
(&rerror("[rm] $?"), close(STDERR), &giveagif($BACKUPGIF), exit) if $?;




# Return the gif
&giveagif ($GIFNAME);				# Return the real picture

# Create a backup file
`$COPY $GIFNAME $BACKUPGIF`;
&rerror("[cp] $?") if $?;

# Delete the temp files
`$DELETE $GIFNAME`;			# Delete the second temp file
&rerror("[rm.2] $?") if $?;

close (STDERR);
exit;



<hr> 
<font size=2><a href="/www/guestbook.html">Sign the GuestBook</a><br><a href="/www/products/index_sales.html">Ordering Information</a>
<br><em>Comments and suggestions about our website to <a href="mailto:webmaster@berksys.com">webmaster@berksys.com</a></em></font>

</body>
</html>
