#!/usr/bin/perl -w use strict; use Image::Magick; my $usage = "Usage: $0 \n"; my $means = shift or die $usage; my $corr = shift or die $usage; my $outfile = shift or die $usage; my %data = &readMeans($means); %data = &readCorr($corr, %data); my $font; my $pointsize = 10; my $xmin = 5; my $xmax = 11; my $ymax = 1; my $width = 800; my $height = 600; my $xorig = 49; my $yorig = $height/2 - 1; my $xscale = ($width-1-$xorig)/($xmax-$xmin); my $yscale = ($height-1-$yorig)/$ymax; my $image = &initGraphics($width, $height); &drawAxes(); &drawThings(); $image->Write(filename=>$outfile); sub drawThings { foreach my $thing (keys %data) { $data{$thing}->{YCORR} = 0 unless exists $data{$thing}->{YCORR}; my ($x, $y) = &toPixel($data{$thing}->{XMEAN}, $data{$thing}->{YCORR}); # print("$thing -> $x x $y\n"); #$image->Draw(primitive=>'point', fill=>'#000000', stroke=>'#000000', points=>"$x,$y"); &drawThing($x, $y, $thing); } return; } sub toPixel { my ($x, $y) = @_; my $xpix = $xorig + ($x-$xmin) * $xscale; if ($y > 0) { $y = 1 - $y; } elsif ($y < 0) { $y = -1 - $y; } my $ypix = $yorig + $y * $yscale; $ypix = $height - 2 - $ypix; return ($xpix, $ypix); } sub drawThing { my ($x, $y, $name) = @_; my $radius = 2; my $x1 = $x + $radius; $image->Draw(primitive=>'circle', fill=>'#000000', stroke=>'#000000', points=>"$x,$y $x1, $y"); $image->Annotate(text=>$name, font=>$font, pointsize=>$pointsize, fill=>'#000000', x=>$x, y=>$y); return; } sub drawAxes { $image->Draw(primitive=>'line', fill=>'#000000', stroke=>'#000000', points=>"$xorig,$yorig $width,$yorig"); $image->Draw(primitive=>'line', fill=>'#000000', stroke=>'#000000', points=>"$xorig,0 $xorig,$height"); # # X-axis labels # for (my $i=$xmin; $i<=$xmax; $i++) { my ($x,$y) = &toPixel($i, 0); my $y1 = $y + 5; $image->Draw(primitive=>'line', fill=>'#000000', stroke=>'#000000', points=>"$x,$y $x,$y1"); my $xt = $x - $pointsize/2; my $yt = $y1 + $pointsize; $image->Annotate(text=>$i, font=>$font, pointsize=>$pointsize, fill=>'#000000', x=>$xt, y=>$yt); } # # Y-axis labels # for (my $i=-1; $i<=1; $i+=.25) { my ($x,$y) = &toPixel($xmin, $i); my $x1 = $x - 5; $image->Draw(primitive=>'line', fill=>'#000000', stroke=>'#000000', points=>"$x,$y $x1,$y"); my $xt = $x1 - 30; my $yt = $y + $pointsize/2; $image->Annotate(text=>$i, font=>$font, pointsize=>$pointsize, fill=>'#000000', x=>$xt, y=>$yt); } return; } sub readMeans { my ($infile) = @_; my %hash = (); open(IN, $infile) or die "Cannot open $infile\n"; while () { chomp; my ($thing, $mean) = split(/\s+/); my $record = { XMEAN => $mean, }; $hash{$thing} = $record; } close(IN); return %hash; } sub readCorr { my ($infile, %hash) = @_; open(IN, $infile) or die "Cannot open $infile\n"; while () { chomp; my ($this, $thing, $coor) = split(/\s+/); die "Cannot find $thing in hash\n" unless exists $hash{$thing}; $hash{$thing}->{YCORR} = $coor; } close(IN); return %hash; } sub initGraphics { my ($width, $height) = @_; my $image = Image::Magick->new(); $image->Set(size=>"${width}x${height}"); $image->ReadImage('xc:#ffffff'); if (-r 'C:/Windows/Fonts/Arial.ttf') { $font = 'C:/Windows/Fonts/Arial.ttf'; } elsif (-r '/home/wes/proj/perl/arial.ttf') { $font = '/home/wes/proj/perl/arial.ttf'; } else { die "Cannot find a font file.\n"; } return $image; }