Files
puzzles/icons/square.pl
Simon Tatham cc7f5503dc Migrate to a CMake-based build system.
This completely removes the old system of mkfiles.pl + Recipe + .R
files that I used to manage the various per-platform makefiles and
other build scripts in this code base. In its place is a
CMakeLists.txt setup, which is still able to compile for Linux,
Windows, MacOS, NestedVM and Emscripten.

The main reason for doing this is because mkfiles.pl was a horrible
pile of unmaintainable cruft. It was hard to keep up to date (e.g.
didn't reliably support the latest Visual Studio project files); it
was so specific to me that nobody else could maintain it (or was even
interested in trying, and who can blame them?), and it wasn't even
easy to _use_ if you weren't me. And it didn't even produce very good
makefiles.

In fact I've been wanting to hurl mkfiles.pl in the bin for years, but
was blocked by CMake not quite being able to support my clang-cl based
system for cross-compiling for Windows on Linux. But CMake 3.20 was
released this month and fixes the last bug in that area (it had to do
with preprocessing of .rc files), so now I'm unblocked!

CMake is not perfect, but it's better at mkfiles.pl's job than
mkfiles.pl was, and it has the great advantage that lots of other
people already know about it.

Other advantages of the CMake system:

 - Easier to build with. At least for the big three platforms, it's
   possible to write down a list of build commands that's actually the
   same everywhere ("cmake ." followed by "cmake --build ."). There's
   endless scope for making your end-user cmake commands more fancy
   than that, for various advantages, but very few people _have_ to.

 - Less effort required to add a new puzzle. You just add a puzzle()
   statement to the top-level CMakeLists.txt, instead of needing to
   remember eight separate fiddly things to put in the .R file. (Look
   at the reduction in CHECKLST.txt!)

 - The 'unfinished' subdirectory is now _built_ unconditionally, even
   if the things in it don't go into the 'make install' target. So
   they won't bit-rot in future.

 - Unix build: unified the old icons makefile with the main build, so
   that each puzzle builds without an icon, runs to build its icon,
   then relinks with it.

 - Windows build: far easier to switch back and forth between debug
   and release than with the old makefiles.

 - MacOS build: CMake has its own .dmg generator, which is surely
   better thought out than my ten-line bodge.

 - net reduction in the number of lines of code in the code base. In
   fact, that's still true _even_ if you don't count the deletion of
   mkfiles.pl itself - that script didn't even have the virtue of
   allowing everything else to be done exceptionally concisely.
2021-03-29 19:02:23 +01:00

96 lines
3.1 KiB
Prolog
Executable File

#!/usr/bin/perl
# Read an input image, crop its border to a standard width, and
# convert it into a square output image. Parameters are:
#
# - the required total image size
# - the output border thickness
# - the input image file name
# - the output image file name.
($convert, $osize, $oborder, $infile, $outfile) = @ARGV;
# Determine the input image's size.
$ident = `identify -format "%w %h" $infile`;
$ident =~ /(\d+) (\d+)/ or die "unable to get size for $infile\n";
($w, $h) = ($1, $2);
# Read the input image data.
$data = [];
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 $infile\n"
unless $al == $xl;
# 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.
$ystart = 0;
$ystart++ while $ystart < $h and scalar(grep { $_ ne $back } map { $data->[$ystart*$w+$_] } 0 .. ($w-1)) == 0;
$yend = $h-1;
$yend-- while $yend >= $ystart and scalar(grep { $_ ne $back } map { $data->[$yend*$w+$_] } 0 .. ($w-1)) == 0;
$xstart = 0;
$xstart++ while $xstart < $w and scalar(grep { $_ ne $back } map { $data->[$_*$w+$xstart] } 0 .. ($h-1)) == 0;
$xend = $w-1;
$xend-- while $xend >= $xstart and scalar(grep { $_ ne $back } map { $data->[$_*$w+$xend] } 0 .. ($h-1)) == 0;
# Decide how much border we're going to put back on to make the
# image perfectly square.
$hexpand = ($yend-$ystart) - ($xend-$xstart);
if ($hexpand > 0) {
$left = int($hexpand / 2);
$xstart -= $left;
$xend += $hexpand - $left;
} elsif ($hexpand < 0) {
$vexpand = -$hexpand;
$top = int($vexpand / 2);
$ystart -= $top;
$yend += $vexpand - $top;
}
$ow = $xend - $xstart + 1;
$oh = $yend - $ystart + 1;
die "internal computation problem" if $ow != $oh; # should be square
# And decide how much _more_ border goes on to add the bit around
# the edge.
$realow = int($ow * ($osize / ($osize - 2*$oborder)));
$extra = $realow - $ow;
$left = int($extra / 2);
$xstart -= $left;
$xend += $extra - $left;
$top = int($extra / 2);
$ystart -= $top;
$yend += $extra - $top;
$ow = $xend - $xstart + 1;
$oh = $yend - $ystart + 1;
die "internal computation problem" if $ow != $oh; # should be square
# Now write out the resulting image, and resize it appropriately.
open IDATA, "|-", $convert, "-size", "${ow}x${oh}", "-depth", "8", "-resize", "${osize}x${osize}!", "rgb:-", $outfile;
for ($y = $ystart; $y <= $yend; $y++) {
for ($x = $xstart; $x <= $xend; $x++) {
if ($x >= 0 && $x < $w && $y >= 0 && $y < $h) {
print IDATA $data->[$y*$w+$x];
} else {
print IDATA $back;
}
}
}
close IDATA;