#!/usr/local/bin/perl # Name kslstereo v1.0 # Platform Unix or Linux with Perl installed. # Date 25 April 1996 # Author Kang SoonLai # Email skang@eecs.ukans.edu # URL http://www.eecs.ukans.edu/~skang # # Class EECS 741 Computer Vision # Final Project # Copyright Kang SoonLai 96 # # This program generates a random character stereogram from an ASCII # file. To run this program: # ksltereo inputfile # # To capture it to a file: # ksltereo inputfile > outfile # ######################################################################### # define some global constants $FOCAL = 6; # normal FOCAL length is 6, max is 26 $ASC = 65; # ascii value for random char to start with #$ASC = 48; # check for correct syntax if (scalar(@ARGV) != 1) { $ii=scalar(@ARGV); print "$ii kslstereo input_file\n"; exit(1); } $infile = shift(@ARGV); # get the input file name open(FILE,"$infile") || die("Can't open $infile: $!\n"); print "
\n\n";	# print header for html format.

$line=;			# get the first line so we can draw the
$line_len=length($line);	# focal points for user to focus on 
&draw_focalline($line_len);

srand;		# select a seed to get different random number each time

#$tt=0;

# read input file until eof. 
while ($line) {
	chop $line;
	$line_len=length($line);
	$last_pix=substr($line,0,1);
	&init_pattern;		# initialize a pattern to start with

	$nwidth=$FOCAL;		# index for new pattern
	$pos=0;			# position of the char pattern
	$output='';		# init final output pattern for a line

	# process each char for the line
	for ($i=0;$i<$line_len;$i++,$pos++) {
		$current_pix=substr($line,$i,1);
		if ($current_pix > $last_pix) {

			# shorten pattern by removing $size element from it
			$size=$current_pix-$last_pix;
			$cur_pos=($pos+$size)%$nwidth;
			$nwidth=$nwidth-$size;		# new width of pattern
			&shorten_pattern($size,$cur_pos);
			$pos=0;

		} elsif ($current_pix < $last_pix) {

			# lengthen pattern by adding $size element to it
			$size=$last_pix-$current_pix;
			$cur_pos=($pos-$size)%$nwidth;
			$nwidth=$nwidth+$size;		# new width of pattern
			&lengthen_pattern($size,$cur_pos);
			$pos=0;
		}

		# assign a value to the pattern
		$output = $output . $pattern[$pos%$nwidth];
		$last_pix=$current_pix;
	}
	
	#print "$output\n";	# generate a sequencial pattern, not random
	&random_pattern($output);	# generate random char pattern
	$line=;			# proceed to next line
	
	# for debugging purpose
	#$tt++;
	#if ($tt > 5) {
	#	exit(1);
	#}
}

close(FILE);		
exit(0);	# just to make sure that we exit after all

#############################################################################
#	initialize pattern to number of focal length.  
#	eg. if $FOCAL=6, then pattern is ABCDEF, and so on.
#############################################################################
sub init_pattern {
	local($i);

	for($i=0;$i<$FOCAL;$i++) {
		$pattern[$i]=sprintf("%c",$ASC+($i%$FOCAL));
	}
}

#############################################################################
#	shorten the pattern by removing $size elements from it.  That is move
#	the index $size position forward.  
#############################################################################
sub shorten_pattern {
	local($size,$cur_pos)=@_;
	#local($nwidth)=@_;
	local($i,$ni,@newpattern);

	#print "\nnew pattern nwidth $nwidth, cur_pos $cur_pos\n\n";
	for ($i=0,$ni=$cur_pos; $i<$nwidth; $i++,$ni++) {
		$newpattern[$i]=$pattern[$ni];
		#print "$newpattern[$i],$i$ni ";
		if ($ni>=$nwidth+$size-1) {	# if the end of pattern
			$ni=0-1;		# go to the beginning
		}
	}
	#print "\n";
	@pattern=@newpattern;
}

#############################################################################
#	lengthen the pattern by adding $size elements to it.  That is move
#	the index $size position backward.
#############################################################################
sub lengthen_pattern {
	local($size,$cur_pos)=@_;
	local($i,$ni,@newpattern);
	
	#print "\nnew pattern nwidth $nwidth, cur_pos $cur_pos\n\n";
	for ($i=0,$ni=$cur_pos; $i<$nwidth; $i++,$ni++) {
		$newpattern[$i]=$pattern[$ni];
		#print "$newpattern[$i] $i$ni, ";
		if ($ni>=$nwidth-$size-1) {	# if the end of pattern,
			$ni=0-1;		# go to the begining.
		}
	}
	#print "\n";
	@pattern=@newpattern;
}

#############################################################################
#	given a string, generate random character pattern.
#	can only generate sequence up to 26 characters width due to the 
#	html format that I want to generate(some special charcters). 
#############################################################################
sub random_pattern {
	local($pattern)=@_;
	local($i,$rnum,$rchar,$ch);

	for ($i=0;$i<$FOCAL;$i++) {
		# get different number that is a printable char
		$rnum=int(rand(0.0009))+int(rand(0.0004))+32;
		
		if ($rnum>126) {	# ascii value that is not printable
			$rnum = 33;
		# avoid the special char "<" and ">"
		} elsif ( ($rnum == 60) || ($rnum == 62) ) {
			$rnum=$rnum+int(rand(0.0001));
		}

		$rchar=sprintf("%c",$rnum);	# random char pattern
		$ch=sprintf("%c",$i+$ASC);	# char in the sequencial pattern
		$pattern =~ s/$ch/$rchar/g;	# substitute pattern
	}
	print "$pattern\n";
}	

#############################################################################
#	draw 2 focal points so that user can see the stereogram more easily
#############################################################################
sub draw_focalline {
	local($line_len)=@_;
	local($focalline);

	$focalline='';
	for ($i=0;$i<$line_len;$i++) {
		
		if (($i==int(($line_len-$FOCAL)/2)) || 
				($i==int(($line_len+$FOCAL)/2)) ){
			$focalline=$focalline . '*';
		} else {
			$focalline=$focalline . ' ';
		}
 	}
	print "$focalline\n";
}