use strict; use Tk; my ( $mw, $canvas, $color, $radius, $colorLabel, $radiusLabel, @data, ); my $TCWidth = 324; my $TCHeight = 243; my $size = 1; $mw = MainWindow->new(-title=>"TC255 Emulator", -width=>$TCWidth*$size+100, -height=>$TCHeight*$size); newCanvas(); @data = getData(); drawData(\@data); $mw->Label(-borderwidth=>'1', -text=>"Radius:", -width=>'7', -font=>"Arial 8 bold")->place(-x=>$TCWidth*$size+10, -y=>'17'); $radiusLabel = $mw->Label(-borderwidth=>'1', -text=>$radius, -width=>'4', -anchor=>"w", -font=>"Arial 8")->place(-x=>$TCWidth*$size+55, -y=>'17'); $mw->Label(-borderwidth=>'1', -text=>"Color:", -width=>'7', -anchor=>"e", -font=>"Arial 8 bold")->place(-x=>$TCWidth*$size+10, -y=>'37'); $colorLabel = $mw->Label(-borderwidth=>'1', -text=>$color, -anchor=>"w", -width=>'4', -font=>"Arial 8")->place(-x=>$TCWidth*$size+55, -y=>'37'); $mw->Button(-text=>"New", -width=>5, -font=>"Times 8 bold", -command=>sub{$canvas->destroy(); newCanvas(); @data = getData(); drawData(\@data); $radiusLabel->configure(-text=>$radius); $colorLabel->configure(-text=>$color); })->place(-x=>$TCWidth*$size+28,-y=>67); $mw->Button(-text=>"Save", -width=>6, -font=>"Times 8 bold", -command=>sub{saveData(\@data); })->place(-x=>$TCWidth*$size+25,-y=>100); $mw->Button(-text=>"Open", -width=>6, -font=>"Times 8 bold", -command=>sub{$canvas->destroy(); newCanvas(); @data = openData(); drawData(\@data); })->place(-x=>$TCWidth*$size+25,-y=>133); $mw->Button(-text=>"Exit", -width=>6, -font=>"Times 8 bold", -command=>sub{exit;})->place(-x=>$TCWidth*$size+25,-y=>$TCHeight-50); MainLoop; sub saveData{ my @d = @{$_[0]}; my $file = $mw->getSaveFile(-defaultextension => ".img", -filetypes => [['TC255 Image', '.img' ], ['All Files', '*', ], ], -initialdir => ".", -initialfile => "default.img", -title => "Save Image", ); if (!defined($file)){return;} open(DAT,">$file") || die "Can't open file: $!\n"; for (my $y=0; $y<$TCHeight; $y++){ for (my $x=0; $x<$TCWidth; $x++){ print DAT "$d[$y][$x],"; } print DAT "\n"; } close(DAT); } sub openData{ my @d; my $file = $mw->getOpenFile(-defaultextension => ".img", -filetypes => [['TC255 Image', '.img' ], ['All Files', '*', ], ], -initialdir => ".", -initialfile => "default.img", -title => "Open Image", ); if (!defined($file)){return;} open(DAT,"<$file") || die "Can't open file: $!\n"; my $y = 0; while ($_ = ){ chomp; my @q = split(/,/,$_); for (my $x=0; $x<$TCWidth; $x++){ $d[$y][$x] = $q[$x]; } $y++; } close(DAT); return @d; } sub newCanvas{ $canvas = $mw->Canvas(-width=>$TCWidth*$size, -height=>$TCHeight*$size, -relief=>"sunken", -borderwidth=>1)->place(-x=>0,-y=>0); } sub drawData{ my @d = @{$_[0]}; for (my $y=0; $y<$TCHeight; $y++){ for (my $x=0; $x<$TCWidth; $x++){ my $c = sprintf("#%02x%02x%02x",$d[$y][$x],$d[$y][$x],$d[$y][$x]); $canvas->createRectangle($x*$size,$y*$size,($x+1)*$size,($y+1)*$size,-fill=>$c,-outline=>$c); } } } sub getData{ my @d; #intensity is a 8 bit value corresponding to grayscale colors # 0 = black # 255 = white $color = int rand(255); $radius = int rand(200); for (my $y=0; $y<$TCHeight; $y++){ for (my $x=0; $x<$TCWidth; $x++){ if((($x-$TCWidth/2)**2 + ($y-$TCHeight/2)**2) < ($radius**2)){ #a circle $d[$y][$x] = $color; } else { $d[$y][$x] = 255; #white background } } } return @d; }