#!/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
  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;
    $lineOfInput = substr($lineOfInput,0,$positionOfB);
  }
#  $lineOfInput =~ s/^(.*?)#(.*)$/$1/; not sure this line is needed
     
    if ($lineOfInput =~ /^([\S]+)\s+([\.|\d|-]+)\s+([\.|\d|-]+)\s+([\.|\d|-]+)/ ) {
      if($isletterb == 1) {     #cement.txt file has b and number
	$inputParams[$vectorIndex] -> [0] = $1;
	$inputParams[$vectorIndex] -> [4] = $blursize; #this is a file to be blurred 
      }
      else
      {
	$inputParams[$vectorIndex] -> [0] = $1;
	$inputParams[$vectorIndex] -> [4] = 0;
      }
      $inputParams[$vectorIndex] -> [1] = $2;
      $inputParams[$vectorIndex] -> [2] = $3;
      $inputParams[$vectorIndex] -> [3] = $4;
      $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;

### if we are to blur the first file
if ($inputParams[0]->[4] != 0){ # blur the first file;
  my $cementinitcommand = "cementinit $inputParams[0]->[0] $inputParams[0]->[1] $inputParams[0]->[2] $inputParams[0]->[3] -o trowel_out.toBeBlurred.plm\n";
  system($cementinitcommand);
  my $blursize = $inputParams[0]->[4];
  my $blurcommand = "blur trowel_out.toBeBlurred.plm $blursize trowel_out.plm";
  print "blurring starting file\n";
  system($blurcommand);
}

### if we are not to blur the first file
else{
  print "not blurring starting file\n";
  my $cementinitcommand = "cementinit $inputParams[0]->[0] $inputParams[0]->[1] $inputParams[0]->[2] $inputParams[0]->[3] -o trowel_out.plm\n";
  system($cementinitcommand);
}

### deal with the rest of the files
for ($vectorIndex=1;$vectorIndex<$numVectors;$vectorIndex++) {

  if ($inputParams[$vectorIndex]->[4]==0){  ## we shouldn't blur this file
    print "not blurring image $inputParams[$vectorIndex]->[0]\n";
    system("cementi trowel_out.plm $inputParams[$vectorIndex]->[0] $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] \n");
  }

  else { # we should blur this file

    my $blurFileName = "trowel_out.$vectorIndex.toBeBlurred.plm";
    my $blursize = $inputParams[$vectorIndex]->[4];
    
    # get the file into lightspace 
    my $cementinitcommand = "cementinit $inputParams[$vectorIndex]->[0] $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] -o $blurFileName\n";
    system($cementinitcommand);

    # blur the image
    my $blurFileNameOut = "trowel_out.$vectorIndex.blurred.plm";
    my $blurcommand = "blur $blurFileName $blursize $blurFileNameOut\n";
    print "blurring image $inputParams[$vectorIndex]->[0]\n";
    system($blurcommand);

    # cement in the blurred image
    system("cementi trowel_out.plm $blurFileNameOut $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3]");
    }
}

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

