#!perl

$in=$ARGV[0];
$out=$ARGV[1];

$nin = $in;

$nin =~ s/\..*$/.txt/;

#print "->$nin";

print "convert $in TEXT:$nin\n";
system("convert $in TEXT:$nin");

$maxx=0;
$maxy=0;
open(IN,$nin) or die "Cannot read input file:$! \n";
while($line=<IN>)
{
if ($line =~ /^([0-9]+),([0-9]+):[\s\t]*([0-9]+),([0-9]+),([0-9]+)[\s\t]*.*$/)
{
$x=int($1);
$y=int($2);
$r=(int($3)>>8) & 0x0FF;
$g=(int($4)>>8) & 0x0FF;
$b=(int($5)>>8) & 0x0FF;
if ($x>$maxx)
{
$maxx=$x;
}

if ($y>$maxy)
{
$maxy=$y;
}


$value = ($r << 16) | ($g << 8) | $b;

$data{$x.":".$y}=$value;


}
}
close(IN);

$maxx=$maxx+1;
$maxy=$maxy+1;

open(OUT,">$out.c") or die "Cannot create outputfile: $!\n";

$length=$maxy*$maxx;

print OUT "const UINT32 $out"."[]={\n";
print OUT "$maxx,$maxy,$length,\n";
for($j=0;$j<$maxy;$j++)
{
for($i=0;$i<$maxx;$i++)
{
  
  $x=$i;

  if (exists $data{$i.":".$j})
  {
     $ra=  ($data{$i.":".$j}) & 0x0FFFFFF;
  }
  else
  {
     $ra=0x0000;
  }
  

  $value=$ra;
  $result=sprintf "0x%08X",$value;
  print OUT "$result,\n";
}
}
print OUT "0x0\n";
print OUT "};\n";
close(OUT);

#rename "$out.c","E:\\BT\\Drivers\\Common\\R2D\\$out.c"
