#!/usr/bin/perl -w

# TROWEL - To Render Our Wonderful Excellent Lightvectors
# Use TROWEL to apply cement
#
# Script to cement multiple images using different weights
#
# requires:
#  cementinit, cementi, plm2pnm (executables), and cement.txt in the directory
#  cement.txt can have comments, and it can also have unused lightvectors, e.g.
#  v001  # with no cement weights after it is ignored
#  (this is so you can ls v???.ppm > cement.txt, or the like and then just
#  put weights on the ones you want to use).
#
# bug reports, etc: mann@eecg.toronto.edu
#
#############################################################

use strict;                     #To try and stop me from installing bugs

my($lineOfInput,@inputParams);  # inputparams is a list of lists, each sublist
                                # containing picturename and the 3 weights. Yes
                                # it hurts readability, but it gives me so much
                                # more flexibility later...
open(COMMANDFILE,"<cement.txt");

my($vectorIndex,$numVectors);
$vectorIndex = 0;

#Parse file into aforementioned nasty array of arrays
while ($lineOfInput = <COMMANDFILE>) {

  # Parse the Blur Option "b"
  my $isletterb = 0;
  my $positionOfB;
  my $blursize;   # blur radius
  my $filterfile; # the filter file with blur radius specified

  if ($lineOfInput =~ /b/){
    $isletterb = 1; # isletterb is boolean variable for existence of letter b
    $positionOfB = index($lineOfInput,"b");
    $blursize = substr($lineOfInput,$positionOfB+2);
    chomp $blursize;
    $filterfile = "filters/filt$blursize.txt";
    $lineOfInput = substr($lineOfInput,0,$positionOfB);
  }
  $lineOfInput =~ s/^(.*?)#(.*)$/$1/;
    
    if ($lineOfInput =~ /^([\S]+)\s+([\.|\d|-]+)\s+([\.|\d|-]+)\s+([\.|\d|-]+)/ ) {
   if($isletterb == 1) {     #cement.txt file has b and number
    my $filetobeblurred = $1;
###    my $plmfiletobeblurred = $filetobeblurred;
###    $plmfiletobeblurred =~ s/jpg/blurmeplm/;
    my $plmfiletobeblurred = "$filetobeblurred.tobeblurred.plm";
###    my $blurredppmfilename = $filetobeblurred;
###    $blurredppmfilename =~ s/jpg/blurredppm/;
    my $blurredppmfilename = "$filetobeblurred.blurred.ppm";
###    my $blurredplmfilename = $filetobeblurred;
###    $blurredplmfilename =~ s/jpg/blurredplm/;
    my $blurredplmfilename = "$filetobeblurred.blurred.plm";
       my $plmcommand = "cementinit $filetobeblurred 1 1 1 -o $plmfiletobeblurred";
       print "$plmcommand";
       system($plmcommand);

       my $blurcommand = "blur $plmfiletobeblurred $filterfile $blurredplmfilename";
       system($blurcommand);
       system("plm2pnm $blurredplmfilename -o $blurredppmfilename");
       $inputParams[$vectorIndex] -> [0] = $blurredppmfilename;
      }
      else
      {
       $inputParams[$vectorIndex] -> [0] = $1;
      }
      print "setting r g b values";
      $inputParams[$vectorIndex] -> [1] = $2;
      $inputParams[$vectorIndex] -> [2] = $3;
      $inputParams[$vectorIndex] -> [3] = $4;
      my $rvalue = $inputParams[$vectorIndex] -> [1];
      my $gvalue = $inputParams[$vectorIndex] -> [2];
      my $bvalue = $inputParams[$vectorIndex] -> [3];
#doesnt work      print "setting r=$inputParams[$vectorIndex] -> [1] g=$inputParams[$vectorIndex] -> [2] b=$inputParams[$vectorIndex] -> [3]";
      print "setting r=$rvalue g=$gvalue b=$bvalue";
      $vectorIndex++;

}
  elsif (!($lineOfInput =~ /^\s*$/)) {
    #Allow blank lines once comments are stripped, but complain about other imparsables
    printf "Bad input line:\n$lineOfInput";
  }
  
}

$numVectors = $vectorIndex;
$vectorIndex = 0;
#printf("About to:cementinit $inputParams[0]->[0] $inputParams[0]->[1] $inputParams[0]->[2] $inputParams[0]->[3] -o trowel_out.plm\n");

my $cementinitcommand = "cementinit $inputParams[0]->[0] $inputParams[0]->[1] $inputParams[0]->[2] $inputParams[0]->[3] -o trowel_out.plm\n";
print $cementinitcommand;
system($cementinitcommand);

for ($vectorIndex=1;$vectorIndex<$numVectors;$vectorIndex++) {
    #    printf("About to: cementi trowel_out.plm.gz $inputParams[$vectorIndex]->[0] $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] \n");
    system("cementi trowel_out.plm $inputParams[$vectorIndex]->[0] $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] \n");
}

system("plm2pnm trowel_out.plm -o trowel_out.ppm\n");










