#!/usr/bin/perl -w
# analyze matrices through colorization
#
# this script may be used under the terms of the GNU
# General Public License, see www.fsf.org for details.
#
# (C)2002 by Felix Buenemann <atmosfear at users.sourceforge.net>
#
# SYNTAX:
#
# "cat matrix1 matrix2 | ./mtxanalyze.pl <x> <y> > out.htm"
#
# Where <x> is the number of colums in _one_ matrix and <y> is
# the number of rows in _one_ matrix.
#
# NOTE: Both matrices must be of equal size and there must be no
#       empty lines after the matrix in the datafiles.
#       Datafiles contain one row per line with columns delimited
#       by spaces.


my $col = shift @ARGV;
my $row = shift @ARGV;
my $x;
my $y;
# we'll use an evenly balanced colorpalette
# from truecolor (24bit)
my $color = 0;
my $coloroff = int(0xFFFFFF / ($col*$row));
my %colortable;

print <<"EOF";
<html>
 <head>
  <title>Atmos' Matrixanalyzer -NAN</title>
 <head>
 <body>
  <p>Generating $col x $row Matrix (Columns x Rows)</p>
  <table width=100% border=1>
EOF
print "   <tr>\n    <td align=center><b><i>Encoded</i></b></td>";
for($x=0; $x < $col; $x++) {
  print "<td align=center><b>C$x</b></td>";
}
print"\n   </tr>\n";

for($y=0; $y < $row; $y++) {
  print "   <tr>\n    <td align=right><b>R$y</b></td>";
  $_ = <STDIN>;
  chomp;
  my @data = split(/ /);

  for($x=0; $x < $col; $x++) {
    my $num = hex($data[$x]);
    $colortable{$data[$x]} = $color;
    printf "<td align=center bgcolor=#%06X><font color=#%06X>%X</font></td>",
      $color, 0xFFFFFF-$color, $num;
    # color each item
    $color+=$coloroff;
  }
  print "\n   </tr>\n";
  # alternative: color each row, also change coloroff definition
  #$color+=$coloroff;
} 

print <<"EOF";
  </table>
  <br><br>
  <table width=100% border=1>
EOF

print "   <tr>\n    <td align=center><b><i>Decoded</i></b></td>";
for($x=0; $x < $col; $x++) {
  print "<td align=center><b>C$x</b></td>";
}
print"\n   </tr>\n";

for($y=0; $y < $row; $y++) {
  print "   <tr>\n    <td align=right><b>R$y</b></td>";
  $_ = <STDIN>;
  chomp;
  my @data = split(/ /);

  for($x=0; $x < $col; $x++) {
    my $num = hex($data[$x]);
    $color = $colortable{$data[$x]};
    printf "<td align=center bgcolor=#%06X><font color=#%06X>%X</font></td>",
      $color, 0xFFFFFF-$color, $num;
  }
  print "\n   </tr>\n";
} 

print <<"EOF";
  </table>
 </body>
</html> 
EOF

