#!/usr/bin/perl -w

# WARNING: Doing this for zoom level 0 requires 90 GB of space in $TEMP!
# That's why layers 0-2 are not generated by default (you need to change
# a line below)

# URL is:   /zoom/X/Y.png
$srcpath = '/mnt/tiles/lowzoomtilegen/';
$dstpath='/mnt/tiles/statictiles/mergedtiles/';

use Graphics::Magick;

# No need to change this usually
$ZOOMMAX = 9;
$TILESIZE = 256;

# Par. 0:  Z
# Par. 1:  X
# Par. 2:  Y
sub mergetile($$$) {
  my $ZS = $_[0];
  my $XS = $_[1];
  my $YS = $_[2];
  my $N = 1;
  my $dstfilename = sprintf("%s/%d/%d/%d.png", $dstpath, $ZS, $XS, $YS);
  my $olfilename = sprintf("%s/%d/%d/%d.png", $srcpath, $ZS, $XS, $YS);
  if ($ZS >= $ZOOMMAX) {
    print("Sorry - can only merge Z <= $ZOOMMAX\n"); exit(1);
  }
  my $z = $ZS;
  while ($ZS < $ZOOMMAX) {
    $N = $N * 2;  # Twice as many tiles (per direction)
    $XS = $XS * 2; # Starting at 2*X
    $YS = $YS * 2; # and 2*Y.
    $ZS++;
  }
  printf("Combining %d tiles (%d x %d pixels)\n", $N * $N, $N * $TILESIZE, $N * $TILESIZE);
  # Now load them all and fill them into one big picture.
  my $dimage = Graphics::Magick->new('size' => sprintf("%dx%d", $N * $TILESIZE, $N * $TILESIZE)); # magick => 'png', 
  $status = $dimage->Read("xc:white"); # This actually creates the image, the above does NOT - very intuitive!
  warn "$status" if "$status";
  my $x; my $y;
  for ($x = 0; $x < $N; $x++) {
    for ($y = 0; $y < $N; $y++) {
      my $fn = sprintf("%s/%d/%d/%d.png", $srcpath, $ZOOMMAX, $XS + $x, $YS + $y);
      #print("Loading: $fn\n");
      my $tmpimg = Graphics::Magick->new();
      $status = $tmpimg->Read($fn);
      warn "$status" if "$status";
      $dimage->Composite( 'image' => $tmpimg, 'compose' => 'over',
                          'x' => ($x * $TILESIZE),
                          'y' => ($y * $TILESIZE));
    }
  }
  $dimage->Resize('geometry' => sprintf("%dx%d", $TILESIZE, $TILESIZE), 'filter' => 'Lanczos');
  #print("Loading: $olfilename\n");
  my $olimg = Graphics::Magick->new();
  $status = $olimg->Read($olfilename);
  warn "$status" if "$status";
  $dimage->Composite( 'image' => $olimg, 'compose' => 'over', 'x' => 0, 'y' => 0 );
  print("Writing combined image to: $dstfilename\n");
  $status = $dimage->Write($dstfilename);
  warn "$status" if "$status";
}

# "main()"

#mergetile(1, 1, 0);
#mergetile(2, 2, 1);
#mergetile(3, 4, 2);
#mergetile(4, 8, 5);
#mergetile(5, 16, 10);
#mergetile(6, 33, 21);
#mergetile(7, 67, 43);
#mergetile(8, 135, 87);
my $rz; my $rx; my $ry;
for ($rz = 3; $rz <= 8; $rz++) {
  my $ntilesperdir = 2 ** $rz;  # Allgemein: 2 ^ Zoomlevel
  for ($rx = 0; $rx < $ntilesperdir; $rx++) {
    my $targdir = sprintf("%s/%d/%d", $dstpath, $rz, $rx);
    if (! -e $targdir) {
      print("Trying to create $targdir...");
      # FIXME: mkdir -p   - the zoom level dir might not exist either!
      if (mkdir($targdir)) {
        print(" OK!\n");
      } else {
        print(" FAILED!\n");
      }
    }
    for ($ry = 0; $ry < $ntilesperdir; $ry++) {
      mergetile($rz, $rx, $ry);
    }
  }
  $ntilesperdir = $ntilesperdir * 2;
}
