#! /usr/bin/perl
use strict;
use warnings;
use POSIX qw(ceil floor);

for my $i (0..24) {
#for my $i (23..23) {
	my @args = ("./blur");
	my ($x1, $y1, $x2, $y2) = (0, 0, 2813.0, 2813.0);  # From Inkscape output when converting by default.
	my $convfactor = 0.26458;  # mm to SVG units, empirical :-/

	for my $j (0..97) {  # 98 hits a movit limit!
		#my $f = $i / 50.0;
		my $f = $i / 25.0 + ($j * 0.010) / 25.0;
		#my $f = $i / 255.0;

		my $d = 90.0 / (1.0 - $f) - 89.0;
		my $outfilename = sprintf("stinger%03d.png", $i * 10 + $j);
		print "$outfilename [$d]...\n";

		my $w = int((($x2 - $x1) / 25.4) * $d * $convfactor + 0.5);
		my $h = int((($y2 - $y1) / 25.4) * $d * $convfactor + 0.5);

		while ($w >= 1280*2 && $h >= 720*2) {
			my $newx1 = $x1 + 0.25 * ($x2 - $x1);
			my $newy1 = $y1 + 0.25 * ($y2 - $y1);
			my $newx2 = $x1 + 0.75 * ($x2 - $x1);
			my $newy2 = $y1 + 0.75 * ($y2 - $y1);
			($x1, $y1, $x2, $y2) = ($newx1, $newy1, $newx2, $newy2);

			$w = floor($w / 2);
			$h = floor($h / 2);
		}

		my $cmd = "LANG=C inkscape --export-png=$outfilename --export-area=$x1:$y1:$x2:$y2 --export-width=$w --export-height=$h bedre_frisbee.svg";
	#	print "$cmd\n";
		system("$cmd 2>/dev/null >/dev/null");
		#my $x = `$cmd 2>/dev/null`;
		#$x =~ /exported to (\d+) x (\d+)/ or die "$x";
		#my ($w, $h) = ($1, $2);

#		my $extent = "";
#		if ($w < 1280 || $h < 720) {
#			$w = 1280 if ($w < 1280);
#			$h = 720 if ($h < 720);
#			$extent = sprintf("-background #00000000 -gravity center -extent %dx%d", $w, $h);
#		}
#
#		my $left = int(($w - 1280) / 2);
#		my $top = int(($h - 720) / 2);
#
#		system("convert $extent -crop 1280x720+${left}+$top stinger.png $outfilename");
		push @args, $outfilename;
	}
	system(@args);
	print join(' ', @args), " -> $i\n";
	rename("blurout.png", sprintf("blur%03d.png", $i));
}
