#!/usr/bin/perl

$file = "spole7.tiff";

open IN,"-|", "convert $file txt:";

$first=1;

while($l = <IN>)
{
  chomp($l);

  if($first)
  {
    if($l =~ /tion: (\d+),(\d+),(\d+),srgba/)
    {
      $first=0;
      $width = $1;
      $height = $2;
    }
  }
  elsif($l =~ /^(\d+),(\d+):.*#([0-9A-Fa-f]{8}) /)
  {
    $r = $1;
    $c = $2;
    $hex = $3;
    $hex = substr($hex,6,2).substr($hex,0,6);
    if($r >= $height)
    {
      print STDERR "row: $r $height\n";
      exit(-1);
    }
    if($c >= $width)
    {
      print STDERR "col: $c $width\n";
      exit(-1);
    }
    $image->{$r}->{$c} = $hex;
  }
}

close IN;
if($first)
{
  print STDERR "Signature not found\n";
  exit(-1);
}

print "int spole7_width=$width;\n";
print "int spole7_height=$height;\n";
print "int spole7_depth=3;\n";
print "unsigned int spole7_image[] = {\n";
for($r=0;$r<$height;$r++)
{
  for($c=0;$c<$width;$c++)
  {
    print "0x".$image->{$r}->{$c};
    print "," if(($c<($width-1))||($r<($height-1)));
    print "\n" if($c==($width-1));
  }
}
print "};\n";

