use strict;

my($filename)=(@ARGV,"red-template.png");
my($filepart)=$filename=~/(.*)\./;

my $command="";

my($ix,$iy)=(0,0);
sub inc(){
	my $xeven=$ix%2==0;
	my $yeven=$iy%2==0;
	
	if( $xeven and  $yeven) { $ix++; return }
	if(!$xeven and  $yeven) { $ix--; $iy++; return }
	if( $xeven and !$yeven) { $ix++; return }
	$ix++; $iy--;
	if($ix==8){
		$ix=0;
		$iy+=2;
	}
}

sub add($$$){
	my($index,$dx,$dy)=@_;
	
	my $sx=($index%8)*8;
	my $sy=int($index/8)*8;
	
	$command.="( $filename -crop 8x8+$sx+$sy +repage -geometry +$dx+$dy ) -compose over -composite ";
}
sub addi($){
	add($_[0],$ix*8,$iy*8);
}

sub addm($$$$){
	my($index,$mask,$dx,$dy)=@_;
	
	my $sx=($index%8)*8;
	my $sy=int($index/8)*8;
	
	$command.="( ( $filename -crop 8x8+$sx+$sy +repage ) masks/$mask.png -compose dstout -composite  -geometry +$dx+$dy ) -compose over -composite ";
}
sub addmi($$){
	addm($_[0],$_[1],$ix*8,$iy*8);
}

sub start(){
	($ix,$iy)=(0,0);
	$command="convert -size 64x64 xc:#000 ";
}

sub end($){
	my($filename)=@_;
	$command.=$filename;
	
	`$command`;
}

if(1){
start;

# ### 1 ###

addi  2; inc;
addi  3; inc;
addi 10; inc;
addi 11; inc;

addi  2; inc;
addi 16; inc;
addi 10; inc;
addi 25; inc;

addi 16; inc;
addi 16; inc;
addi 25; inc;
addi 25; inc;

addi 16; inc;
addi  3; inc;
addi 25; inc;
addi 11; inc;

# ### 2 ###

addi  2; inc;
addi  3; inc;
addi 24; inc;
addi 17; inc;

addi  2; addmi 20,"ur"; addmi 30,"dl"; inc;
addi 16; addi  20     ; inc;
addi 24; addi  30     ; inc;
addi  9; inc;

addi 16; addi  20     ; inc;
addi 16; addi  21     ; inc;
addi  8; inc;
addi  9; inc;

addi 16; addi  21     ; inc;
addi  3; addmi 20,"ul"; addmi 13,"dr"; inc;
addi  8; inc;
addi 17; addi  13     ; inc;

# ### 3 ###

addi 24; inc;
addi 17; inc;
addi 24; inc;
addi 17; inc;

addi 24; addi  22     ; inc;
addi  1; inc;
addi 24; addi  30     ; inc;
addi  9; inc;

addi  0; inc;
addi  1; inc;
addi  8; inc;
addi  9; inc;

addi  0; inc;
addi 17; addi   5     ; inc;
addi  8; inc;
addi 17; addi  13     ; inc;

# ### 4 ###

addi 24; inc;
addi 17; inc;
addi 10; inc;
addi 11; inc;

addi 24; addi  22     ; inc;
addi  1; inc;
addi 10; addmi 30,"ul"; addmi 14,"dr"; inc;
addi 25; addi  14     ; inc;

addi  0; inc;
addi  1; inc;
addi 25; addi  14     ; inc;
addi 25; addi  14     ; inc;

addi  0; inc;
addi 17; addi   5     ; inc;
addi 25; addi  14     ; inc;
addi 11; addmi 14,"dl"; addmi 13,"ur"; inc;

end "$filepart-ctm1.png";
}

if(1){
start;

# ### 1 ###

addi  2; inc;
addi 16; inc;
addi 24; inc;
addi 18; inc;

addi 16; inc;
addi  3; inc;
addi 19; inc;
addi 17; inc;

addi 24; inc;
addi 26; inc;
addi 24; inc;
addi 18; inc;

addi 16; inc;
addi 16; inc;
addi 19; inc;
addi 18; inc;

# ### 2 ###

addi 24; inc;
addi 26; inc;
addi 10; inc;
addi 25; inc;

addi 27; inc;
addi 17; inc;
addi 25; inc;
addi 11; inc;

addi 27; inc;
addi 26; inc;
addi 25; inc;
addi 25; inc;

addi 27; inc;
addi 17; inc;
addi 19; inc;
addi 17; inc;

# ### 3 ###

addi 24; addmi 20,"ur"; addmi 22,"dl"; inc;
addi 26; addi  21     ; inc;
addi 24; addi  22     ; inc;
addi  9; inc;

addi 16; addi  20     ; inc;
addi 16; addmi 21,"ul"; addmi 13,"dr"; inc;
addi  8; inc;
addi 18; addi  13     ; inc;

addi 24; addi  22     ; inc;
addi  1; inc;
addi 24; addmi 30,"ul"; addmi 14,"dr"; inc;
addi 18; addi  15     ; inc;

addi 16; addmi 21,"ur"; addmi 30,"dl"; inc;
addi 16; addi  21     ; inc;
addi 19; addi  30     ; inc;
addi  9; inc;

# ### 4 ###

addi 27; addi  22     ; inc;
addi  1; inc;
addi 25; addmi 30,"ul"; addmi 14,"dr"; inc;
addi 25; addi  15     ; inc;

addi  0; inc;
addi 17; addi  13     ; inc;
addi 19; addi  14     ; inc;
addi 17; addmi 15,"dl"; addmi 13,"ur"; inc;

addi  0; inc;
addi 26; addi   5     ; inc;
addi 25; addi  14     ; inc;
addi 25; addmi 15,"dl"; addmi 13,"ur"; inc;

addi 27; addi  20     ; inc;
addi 17; addmi 21,"ul"; addmi 13,"dr"; inc;
addi  8; inc;
addi 17; addi  13     ; inc;

end "$filepart-ctm2.png";
}

start;

# ### 1 ###

addi 27; addi  22     ; inc;
addi  1; inc;
addi 19; addmi 30,"ul"; addmi 14,"dr"; inc;
addi 18; addi  15     ; inc;

addi 27; addmi 21,"ur"; addmi 22,"dl"; inc;
addi 26; addi  21     ; inc;
addi 19; addi  30     ; inc;
addi  9; inc;

addi  0; inc;
addi 26; addi   5     ; inc;
addi  8; inc;
addi 18; addi  13     ; inc;

addi  0; inc;
addi  1; inc;
addi 19; addi  14     ; inc;
addi 18; addi  15     ; inc;

# ### 2 ###

addi  0; inc;
addi 26; addi   5     ; inc;
addi 19; addi  14     ; inc;
addi 18; addmi 15,"dl"; addmi 13,"ur"; inc;

addi 27; addi  20     ; inc;
addi 26; addmi 21,"ul"; addmi  5,"dr"; inc;
addi  8; inc;
addi 18; addi  13     ; inc;

addi 27; addi  20     ; inc;
addi 26; addi  21     ; inc;
addi  8; inc;
addi  9; inc;

addi 27; addi  22     ; inc;
addi  1; inc;
addi 19; addi  30     ; inc;
addi  9; inc;

# ### 3 ###

addi  0; inc;
addi  1; inc;
addi  8; inc;
addi 18; addmi 15,"ur"; addmi 13,"dl"; inc;

addi  0; inc;
addi  1; inc;
addi 19; addmi 30,"dr"; addmi 14,"ul"; inc;
addi  9; inc;

addi 27; addmi 20,"dl"; addmi 30,"ur"; inc;
addi  1; inc;
addi  8; inc;
addi 18; addmi 15,"ur"; addmi 13,"dl"; inc;

addi  0; inc;
addi 26; addmi 21,"dr"; addmi 5,"ul"; inc;
addi 19; addmi 30,"dr"; addmi 14,"ul"; inc;
addi  9; inc;

# ### 4 ###

addi  0; inc;
addi 26; addmi 21,"dr"; addmi 5,"ul"; inc;
addi  8; inc;
addi  9; inc;

addi 27; addmi  20,"dl"; addmi  30,"ur"; inc;
addi  1; inc;
addi  8; inc;
addi  9; inc;

addi 27; inc;
addi 26; inc;
addi 19; inc;
addi 18; inc;

addi  0; inc;
addi  1; inc;
addi  8; inc;
addi  9; inc;

end "$filepart-ctm3.png";








