Source code for program that handles image retrieval, image manipulation and network communications between separate components of Nude Studies in Aleatoric Environments.

Lines containing comments are highlighted in yellow. Feel free to leave your own comments or questions. I will watch these pages and respond.

Pall Thayer
line no. 1 (0 comments)
#!/usr/bin/perl
line no. 2 (0 comments)
# ˆ© Copyright 2008, Pall Thayer
line no. 3 (0 comments)
# This file is part of "Nude Studies in Aleatoric Environments".
line no. 4 (0 comments)
#
line no. 5 (0 comments)
#    'Nude Studies in Aleatoric Environments' is art: you can redistribute
line no. 6 (0 comments)
#    it and/or modify it under the terms of the GNU General Public License
line no. 7 (0 comments)
#    as published by the Free Software Foundation, either version 3 of the
line no. 8 (0 comments)
#    License, or (at your option) any later version.
line no. 9 (0 comments)
#
line no. 10 (0 comments)
#    'Nude Studies in Aleatoric Environments' is distributed in the
line no. 11 (0 comments)
#    hope that it will be useful, but WITHOUT ANY WARRANTY; without
line no. 12 (0 comments)
#    even the implied warranty of MERCHANTABILITY or FITNESS FOR A
line no. 13 (0 comments)
#    PARTICULAR PURPOSE.  See the GNU General Public License for more details.
line no. 14 (0 comments)
#
line no. 15 (0 comments)
#    You should have received a copy of the GNU General Public License
line no. 16 (0 comments)
#    along with "Nude Studies in Aleatoric Environments".  If not,
line no. 17 (0 comments)
#    see <http://www.gnu.org/licenses/>.
line no. 18 (0 comments)
# programming begins here:
line no. 19 (1 comments)
use IO::Socket;
line no. 20 (0 comments)
use IO::Select;
line no. 21 (1 comments)
use Flickr::API;
line no. 22 (0 comments)
use Flickr::API::Request;
line no. 23 (1 comments)
use Image::Magick;
line no. 24 (0 comments)
use Time::HiRes qw ( time alarm sleep );
line no. 25 (0 comments)
my $svg_filename = "vectors.svg";
line no. 26 (0 comments)
my $last_coords;
line no. 27 (0 comments)
my $last_coords2;
line no. 28 (0 comments)
my $text;
line no. 29 (0 comments)
my $IMAGE = "temp.jpg";
line no. 30 (0 comments)
my $msg;
line no. 31 (1 comments)
my $seisUDP = IO::Socket::INET->new(LocalPort => 9689,
line no. 32 (0 comments)
					Proto     => 'udp')
line no. 33 (0 comments)
or die "Couldn't be a udp server on port $server_port : $@\n";
line no. 34 (1 comments)
my $socket = new IO::Socket::INET
line no. 35 (0 comments)
			Proto		=> 'tcp',
line no. 36 (0 comments)
			LocalPort	=> 6789,
line no. 37 (0 comments)
			Reuse		=> 1,
line no. 38 (0 comments)
			Listen		=> 90;
line no. 39 (0 comments)
my $select = IO::Select->new($socket);
line no. 40 (0 comments)
&sender($text);
line no. 41 (1 comments)
while(1){
line no. 42 (0 comments)
	my $api = new Flickr::API({'key'    => '825b43b304bb68d09bd31951911aa3ae'});
line no. 43 (0 comments)
	$rand_page = int(rand(3000));
line no. 44 (0 comments)
	my $request = new Flickr::API::Request({
line no. 45 (0 comments)
	                'method' => 'flickr.photos.search',
line no. 46 (1 comments)
	                'args' => {'tags' => 'nude', 'per_page' => '1', 'page' => $rand_page},
line no. 47 (0 comments)
	        });
line no. 48 (0 comments)
	my $response = $api->execute_request($request);
line no. 49 (0 comments)
	$ret_val = $response->{_content};
line no. 50 (0 comments)
	$ret_val =~ m/server="(\d+)"/;
line no. 51 (0 comments)
	$serv_val = $1;
line no. 52 (0 comments)
	$ret_val =~ m/id="(\d+)"/;
line no. 53 (0 comments)
	$phot_id = $1;
line no. 54 (0 comments)
	$ret_val =~ m/secret="([^"]+)"/;
line no. 55 (0 comments)
	$sec_val = $1;
line no. 56 (0 comments)
	$pic_URL = $serv_val."/".$phot_id."_".$sec_val."_m.jpg";
line no. 57 (0 comments)
	$pic_name = $phot_id."_".$sec_val."_m.jpg";
line no. 58 (1 comments)
	$get_pic = `wget http://static.flickr.com/$pic_URL`;
line no. 59 (1 comments)
	open(WEBP, '>/home/palli/public_html/nude_studies/pic_url.txt');
line no. 60 (0 comments)
	print WEBP "http://static.flickr.com/$pic_URL";
line no. 61 (1 comments)
	$do_imgTweak = `convert +dither -blur 0x1 -resize 400x300 $pic_name -posterize 10 temp.jpg`;
line no. 62 (1 comments)
	$del_orig = `rm $pic_name`;
line no. 63 (1 comments)
	$make_svg = `autotrace -despeckle-level 20 -color-count 16 -output-file vectors.svg temp.jpg`;
line no. 64 (0 comments)
	my $im = Image::Magick->new or die "Cannot create im: $!";
line no. 65 (0 comments)
	   $im->Read($IMAGE);
line no. 66 (0 comments)
	   my $width = $im->Get('width');
line no. 67 (0 comments)
	   my $height = $im->Get('height');
line no. 68 (0 comments)
	$text = "newimage $width $height";
line no. 69 (1 comments)
	&sender($text);
line no. 70 (0 comments)
	open(SVG_FILE, "<$svg_filename");
line no. 71 (1 comments)
	while(<SVG_FILE>){
line no. 72 (0 comments)
		undef(my $polypath);
line no. 73 (0 comments)
		chomp($_);
line no. 74 (0 comments)
		$_ =~ /d="([^"]*)"/;
line no. 75 (0 comments)
	    my $coordat = $1;
line no. 76 (0 comments)
	    chop($coordat);
line no. 77 (0 comments)
	    $_ =~ /fill:([^;]*);/;
line no. 78 (0 comments)
	    my $shape_col = $1;
line no. 79 (0 comments)
	    $shape_col =~ s/\#//;
line no. 80 (0 comments)
	    $coordat =~ s/([MCL])/=$1/g;
line no. 81 (0 comments)
	    $coordat =~ s/^.//;
line no. 82 (0 comments)
	    $polypath = "$coordat=$shape_col";
line no. 83 (1 comments)
	    my @path_elements = split('=', $polypath);
line no. 84 (0 comments)
	    foreach(@path_elements){
line no. 85 (1 comments)
	    	&quakeDat();
line no. 86 (0 comments)
	    	for($_){
line no. 87 (1 comments)
	    		/M/		&& do { $_ =~ s/M//;
line no. 88 (0 comments)
	    						my @split_path = split(' ', $_);
line no. 89 (0 comments)
	    						my $num = @split_path;
line no. 90 (0 comments)
	    						$last_coords = @split_path[$num-2]." ".@split_path[$num-1];
line no. 91 (0 comments)
							$last_coords2 = int(@split_path[$num-2]+400)." ".@split_path[$num-1];
line no. 92 (0 comments)
	    						&quakeDat();
line no. 93 (0 comments)
		    					last;
line no. 94 (0 comments)
	    					};
line no. 95 (1 comments)
	    		/C/		&& do { $_ =~ s/C//;
line no. 96 (0 comments)
							&quakeDat();
line no. 97 (0 comments)
	    						my @split_path = split(' ', $_);
line no. 98 (0 comments)
	    						my $num = @split_path;
line no. 99 (0 comments)
		    					$text = "curve ff".$shape_col." ".$last_coords." ".$_;
line no. 100 (1 comments)
		    					&sender($text);
line no. 101 (0 comments)
		    					$last_coords = @split_path[$num-2]." ".@split_path[$num-1];
line no. 102 (0 comments)
		    					last;
line no. 103 (0 comments)
	    					};
line no. 104 (1 comments)
	    		/L/		&& do { $_ =~ s/L//;
line no. 105 (0 comments)
							&quakeDat();
line no. 106 (0 comments)
	    						my @split_path = split(' ', $_);
line no. 107 (0 comments)
	    						my $num = @split_path;
line no. 108 (0 comments)
		    					$text = "line ff".$shape_col." ".$last_coords." ".$_;
line no. 109 (1 comments)
		    					&sender($text);
line no. 110 (0 comments)
		    					$last_coords = @split_path[$num-2]." ".@split_path[$num-1];
line no. 111 (0 comments)
		    					last;
line no. 112 (0 comments)
	    					};
line no. 113 (0 comments)
	    		/ /		&& do {};
line no. 114 (0 comments)
	    	}
line no. 115 (0 comments)
	    }
line no. 116 (0 comments)
	}
line no. 117 (0 comments)
}
line no. 118 (1 comments)
sub sender {
line no. 119 (0 comments)
	my $send_data = shift;
line no. 120 (0 comments)
	my $newuse = 0;
line no. 121 (0 comments)
		foreach $user($select->can_read(0)) {
line no. 122 (0 comments)
			if ($user == $socket) {
line no. 123 (0 comments)
				# looks like we have a new connection
line no. 124 (0 comments)
				$user = $socket->accept();
line no. 125 (0 comments)
				$select->add($user);
line no. 126 (0 comments)
				$newuse = 1;
line no. 127 (0 comments)
			}else{
line no. 128 (0 comments)
				my $data = <$user>;
line no. 129 (0 comments)
				if (defined($data)) {
line no. 130 (0 comments)
					if ($data =~ /user/){
line no. 131 (0 comments)
						# add this to our user list
line no. 132 (0 comments)
						my $i = chr(0);
line no. 133 (0 comments)
						$data =~ s/\n//g;
line no. 134 (0 comments)
						$data =~ s/user//;
line no. 135 (0 comments)
						$data =~ s/$i//g;
line no. 136 (0 comments)
						$data =~ s/\s/_/g;
line no. 137 (0 comments)
						$newuse = 1;
line no. 138 (0 comments)
						$users{$user->fileno} = $data;
line no. 139 (0 comments)
					}else{
line no. 140 (0 comments)
					}
line no. 141 (0 comments)
				}else{
line no. 142 (0 comments)
					delete($users{$user->fileno});
line no. 143 (0 comments)
					$select->remove($user);
line no. 144 (0 comments)
					$user->close;
line no. 145 (0 comments)
					$newuse = 1;
line no. 146 (0 comments)
				}
line no. 147 (0 comments)
			}
line no. 148 (0 comments)
		}
line no. 149 (0 comments)
		if($newuse == 1){
line no. 150 (0 comments)
			for (keys %users) {
line no. 151 (0 comments)
				$tempval .= $users{$_}."\n";
line no. 152 (0 comments)
			}
line no. 153 (0 comments)
		}
line no. 154 (0 comments)
		foreach my $user($select->can_write(0)) {
line no. 155 (0 comments)
			if($send_data =~ /[0-n]/){
line no. 156 (0 comments)
				eval{$user->send($send_data." :\n")};
line no. 157 (0 comments)
			}
line no. 158 (0 comments)
		}
line no. 159 (0 comments)
		select(undef, undef, undef, 0.15);
line no. 160 (0 comments)
}
line no. 161 (1 comments)
sub quakeDat {
line no. 162 (0 comments)
	eval {
line no. 163 (0 comments)
			$TIMEOUT = 0.05;
line no. 164 (0 comments)
			$MAXLEN = 1024;
line no. 165 (0 comments)
			local $SIG{ALRM} = sub { die "alarm time out" };
line no. 166 (0 comments)
			alarm $TIMEOUT;
line no. 167 (0 comments)
			#-- Receive msg from the server
line no. 168 (0 comments)
			$seisUDP->recv($msg, $MAXLEN)      or die "recv: $!";
line no. 169 (0 comments)
			#print "seisdata is $msg\n";
line no. 170 (0 comments)
			my $quaketext = "seisdat $msg";
line no. 171 (0 comments)
			&sender($quaketext);
line no. 172 (0 comments)
			alarm 0;
line no. 173 (0 comments)
			1;  # return value from eval on normalcy
line no. 174 (0 comments)
		};
line no. 175 (0 comments)
}