X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/afc306fc5098945e25027fd6bce572d44a8a139f..7f7476c217b6a70490ca1e17c5e5b9b51eb263b1:/icons/square.pl diff --git a/icons/square.pl b/icons/square.pl index 295b2f1..815b94b 100755 --- a/icons/square.pl +++ b/icons/square.pl @@ -17,18 +17,27 @@ $ident =~ /(\d+) (\d+)/ or die "unable to get size for $infile\n"; # Read the input image data. $data = []; -open IDATA, "convert $infile rgb:- |"; +open IDATA, "convert -depth 8 $infile rgb:- |"; push @$data, $rgb while (read IDATA,$rgb,3,0) == 3; close IDATA; # Check we have the right amount of data. $xl = $w * $h; $al = scalar @$data; -die "wrong amount of image data ($al, expected $xl) from $img\n" +die "wrong amount of image data ($al, expected $xl) from $infile\n" unless $al == $xl; -# Find the background colour. We assume the image already has a -# border, so this is just the pixel colour of the top left corner. -$back = $data->[0]; +# Find the background colour, by looking around the entire border +# and finding the most popular pixel colour. +for ($i = 0; $i < $w; $i++) { + $pcount{$data->[$i]}++; # top row + $pcount{$data->[($h-1)*$w+$i]}++; # bottom row +} +for ($i = 1; $i < $h-1; $i++) { + $pcount{$data->[$i*$w]}++; # left column + $pcount{$data->[$i*$w+$w-1]}++; # right column +} +@plist = sort { $pcount{$b} <=> $pcount{$a} } keys %pcount; +$back = $plist[0]; # Crop rows and columns off the image to find the central rectangle # of non-background stuff.