#!/usr/bin/perl -w use strict; use warnings; use X11::GUITest qw(GetMousePos MoveMouseAbs ClickMouseButton SendKeys PressReleaseKey :CONST); use Image::BMP; use Time::HiRes qw[sleep]; # ~~~~~~ Change timing here (for slow computers increase sleep times) ~~~~~~~~ my $sleeptime = 0.5; my $shortsleeptime = 0.1; my $stepsize = 1; # for debugging (skip pixels) my $img = Image::BMP->new(); # ~~~~~~~ Change image file here ~~~~~~~~ $img->open_file('dune_sunset_big.bmp'); my ($width, $height) = ($img->{Width}, $img->{Height}); print "Optimizing rendering sequence...\n"; my $colnochange = 0; my $colchange = 0; my %pixels; for(my $y = 0; $y < $height; $y+=$stepsize) { for(my $x = 0; $x < $width; $x+=$stepsize) { my @colors = $img->xy_rgb($x, $y); my $colortext = ''; foreach my $grey (@colors) { my $greytext = lc(sprintf("%x", $grey)); if(length($greytext) == 1) { $greytext = "0$greytext"; } $colortext .= $greytext; } #print "$x $y $colortext\n"; if(!defined($pixels{$colortext})) { my @tmp = ($x, $y); $pixels{$colortext} = \@tmp; $colchange++; } else { $colnochange++; push @{$pixels{$colortext}}, $x, $y; } } } print "Can use $colnochange cached colors clicks.\n"; print "Have to change color $colchange times, though!\n"; my @colmenubutton = getPos("Mouse to color select button"); my @colmenutext = getPos("Mouse to color select textfield"); my @topleft = getPos("Mouse to top left of image"); print "------ READY TO PAINT ----\n"; getPos("GET READY!"); my $didcolchange = 0; foreach my $destcolor (reverse sort keys %pixels) { setColor($destcolor); $didcolchange++; my @tmp = @{$pixels{$destcolor}}; while(@tmp) { doPaint(shift @tmp, shift @tmp); } print "Finished cycle $didcolchange of $colchange\n"; } print "Done painting!\n"; sub getPos { my $text = shift; my $x = 10; while($x > 0) { print "$text ... $x\n"; sleep(1); $x--; } return GetMousePos(); } sub setColor { my $greytext = shift; MoveMouseAbs(@colmenubutton[0, 1]); ClickMouseButton(M_LEFT); sleep($sleeptime); MoveMouseAbs(@colmenutext[0, 1]); ClickMouseButton(M_LEFT); sleep($sleeptime); #SendKeys("{BS}{BS}{BS}{BS}{BS}{BS}$greytext\n"); my @keyarray = ('{BS}' x 6, (split//, $greytext), "\n"); foreach my $pkey (@keyarray) { SendKeys($pkey); sleep(0.1); } sleep($sleeptime); } sub doPaint { my ($x, $y) = @_; # We're scaling the image by factor two, since we # have trouble using the smallest brush my $paintx = $x * 2 + $topleft[0]; my $painty = $y * 2 + $topleft[1]; MoveMouseAbs($paintx, $painty); ClickMouseButton(M_LEFT); sleep($shortsleeptime); }