#!/usr/bin/perl -w
use strict;
use Imager;
use LWP;
use LWP::UserAgent;;
use HTTP::Request;

my %config = (
	      brain => "brain.gif",
	      locs  => [[120,100],[90,180],[250,100],[200,210]],
	      radii => [40,40,40,40],
	      blur_width => 5,
	      gis => "http://images.google.com/images?q=",
	      igc => "http://images.google.com",
	      );

# open the brain
my $brain = Imager->new();
$brain->open(file=>$config{brain}) or die $brain->errstr();

# make the mask
my $mask = Imager->new(xsize=>$brain->getwidth, ysize=>$brain->getheight,
		       channels=>1);
my $white = Imager::Color->new( 255, 255, 255 );
my $black = Imager::Color->new( 0, 0, 0 );

$mask->flood_fill(x=>1, y=>1, color=>$black);

my $i = 0;
foreach ($i=0;$i<@ARGV;$i++) {
    my $url = get_img_url($ARGV[$i]);
    my $file = get_to_file($url);
    my $img = Imager->new();
    $img->open(file=>$file) or die $img->errstr();
    # scale to be 2*radii
    my $W = $img->getwidth();
    my $H = $img->getheight();
    $img=$img->scale(($W >= $H ?"x":"y")."pixels"
		      =>$config{radii}->[$i]*2);
    # update the mask to crop the edges of this item
    $W = $img->getwidth(); $H = $img->getheight();
    my $LEFT = $config{locs}->[$i]->[0] - $config{radii}->[$i]
	+ int($W/2-$config{radii}->[$i]);
    my $TOP = $config{locs}->[$i]->[1] - $config{radii}->[$i]
	+ int($H/2-$config{radii}->[$i]);
    $mask->box(color=>$white,
	       xmin=>$LEFT-$config{blur_width},
	       xmax=>$LEFT+$W+$config{blur_width},
	       ymin=>$TOP-$config{blur_width},
	       ymax=>$TOP+$H+$config{blur_width},
	       filled=>$white);
    $mask->box(color=>$black,
	       xmin=>$LEFT+$config{blur_width},
	       xmax=>$LEFT+$W-$config{blur_width},
	       ymin=>$TOP+$config{blur_width},
	       ymax=>$TOP+$H-$config{blur_width},
	       filled=>$black);    

    $brain->paste(left=>$LEFT, top=>$TOP,
		  img=>$img);
}
# now, the mask should cover the bits we want to blur, and nothing else
$brain = $brain->masked(mask=>$mask);
$brain->filter(type=>'gaussian', stddev=>$config{blur_width}*0.5);

# save the brain (pickle it in a jar)
$brain->write(file=>"brain2.png") or die $brain->errstr();

exit(0);
sub get_img_url {
    my ($term) = @_;
    my $t_url = $config{gis}.$term;
    print STDERR "$t_url\n";
    my $page = get($t_url) or die "failed!";
    #print $page;
    $page =~ m/imgrefurl=([^&]+?)&.*?img src=([^\s]+)/;
    print STDERR "URL FOR $term is $1 $2\n";
    return $config{igc}.$2;
}
sub get_to_file {
    my ($url) = @_;
    $url =~ /\.(\w+)$/;
    my $file = "tmp.$1";
    my $content = get($url);
    open(FOO, ">$file") or die "$!";
    print FOO $content;
    close FOO;
    return $file;
}

BEGIN {
    my $ua = LWP::UserAgent->new;
    $ua->agent("InsideBrain/1.0"); # lie to google images
    sub get {
	my $url = $_[0];
	my $req = HTTP::Request->new(GET => $url);
	my $res = $ua->request($req);
	if ($res->is_success) {
	    return $res->content;
	}
	else {
	    print STDERR $res->status_line, "\n";
	    return undef;
	}
    }
}
