#!/usr/bin/perl -w

print '<?xml version="1.0"?>'."\n";
print '<!DOCTYPE svg SYSTEM "svg-19991203.dtd" >'."\n";

print '<svg width="256px" height="256px">'."\n";
print '<g style="stroke-width:1">'."\n";

$lastColour = "moreMagic";
$lastFill = 0;

while (my $line = <STDIN>)
{
chomp($line);
@args = split(/\,/, $line);
if ($args[0] eq "0")
	{
	&rgb(0, @args[3..5]);
	print "\t".'<circle cx="'.$args[1].'" cy="'.$args[2].'" r="1" />'."\n";
	}	
if ($args[0] eq "1")
	{
	&rgb(0, @args[5..7]);
	print "\t".'<line x1="'.$args[1].'" y1="'.$args[2].'" x2="'.$args[3].'" y2="'.$args[4].'" />'."\n";
	}	
if ($args[0] eq "2" || $args[0] eq "3")
	{
	&rgb($args[0]-2, @args[5..7]);
	$x1 = max($args[1], $args[3]);
	$x2 = min($args[1], $args[3]);
	$y1 = max($args[2], $args[4]);
	$y2 = min($args[2], $args[4]);
	print "\t".'<rect x="'.$x1.'" y="'.$y1.'" width="'.($x2 - $x1).'" height="'. ($y2 - $y1).'"/>'."\n";

	}
if ($args[0] eq "4" || $args[0] eq "5")
	{
	&rgb($args[0]-4, @args[4..6]);  
	print "\t".'<circle cx="'.$args[1].'" cy="'.$args[2].'" r="'.$args[3].'" />'."\n";
	}
if ($args[0] eq "6" || $args[0] eq "7")
	{
	&rgb($args[0]-6, @args[5..7]);
	$xc = ( ($args[1]+$args[3]) / 2.0 );
	$yc = ( ($args[2]+$args[4]) / 2.0 );
	$xr = (abs ($xc - $args[1]));
	$yr = (abs ($args[4] - $yc));

	print "\t".'<ellipse cx="'.$xc.'" cy="'.$yc.'" rx="'.$xr.'" ry="'.$yr.'" />'."\n";
	}
}

print "</g></g>\n";
print "</svg>\n";

sub min
{
if ($_[0] > $_[1])
	{
	return $_[0];
	}
else
	{
	return $_[1];
	}
}

sub max
{
if ($_[0] < $_[1])
	{
	return $_[0];
	}
else
	{
	return $_[1];
	}
}

sub rgb
{
$r = int ($_[1]/256.0);
$g = int ($_[2]/256.0);
$b = int ($_[3]/256.0);

$thisColour = "$r $g $b";

if ($lastFill != $_[0] || $lastColour ne $thisColour)
	{
	if ($lastColour ne "moreMagic")
		{
		print "</g>\n";
		}

	if ($_[0] == 1)
		{
		print "<g style=\"stroke:rgb($r,$g,$b); fill:rgb($r,$g,$b)\">\n";
		}
	else
		{
		print "<g style=\"stroke:rgb($r,$g,$b); fill:none\">\n";
		}
	$lastFill = $_[0];
	$lastColour = $thisColour;
	}
}

