#!/usr/bin/perl -w

#sequential movie version of trowel: creates sequences of a cumulative
#sequence as lightvectors arrive in the order of the cement.txt file

# 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 $filterFilePrefix="/usr/local/filters/filt";
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

  # sed out comments in line, otherwise includes blur in comments. 
  $lineOfInput =~ s/#.*//;  # ".*" is the operator for what "*" in shell does
  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;
    $blursize =~ s/\s//g; # sed out any spaces in 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;
 my $blurredString = "";
### 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 $filterfile = $filterFilePrefix.$blursize.".txt";
  my $blurcommand = "blur trowel_out.toBeBlurred.plm $filterfile trowel_out.plm";
  $blurredString = $blurredString." $inputParams[0]->[0]  ";
  system($blurcommand);
}

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

#  my $displaycommand0 = "xv $inputParams[0]->[0] -geometry +0+0&";
#  system($displaycommand0);
  my $viewimage= "cementinit $inputParams[0]->[0] 100 100 100 -o pork.plm\n";
  system($viewimage);
  my $displaycommand0 = "plm2pnm pork.plm -o pork100.ppm";
  system($displaycommand0);
  system("xv pork100.ppm -geometry +0+0&");
 
  my $viewimage = "cementinit $inputParams[0]->[0] 10 10 10 -o pork.plm\n";
  system($viewimage);
  my $displaycommand0 = "plm2pnm pork.plm -o pork10.ppm";
  system($displaycommand0);
  system("xv pork10.ppm -geometry +0+0&");

  my $viewimage = "cementinit $inputParams[0]->[0] 1 1 1 -o pork.plm\n";
  system($viewimage);
  my $displaycommand0 = "plm2pnm pork.plm -o pork1.ppm";
  system($displaycommand0);
  system("xv pork1.ppm -geometry +0+0&");

  my $displaycommand1 = "plm2pnm trowel_out.plm -o pork.ppm";
  my $displaycommand2 = "xv pork.ppm -geometry +320+0&";
  system($displaycommand1);
  system($displaycommand2);

}

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

  if ($inputParams[$vectorIndex]->[4] != 0) { # we should blur this file
      my $cementinitcommand = "cementinit $inputParams[$vectorIndex]->[0] $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] -o trowel_out.toBeBlurred.plm\n";
      system($cementinitcommand);
      my $blursize = $inputParams[$vectorIndex]->[4];
      my $filterfile = $filterFilePrefix.$blursize.".txt";
      my $blurcommand = "blur trowel_out.toBeBlurred.plm $filterfile tmp.plm";
      $blurredString = $blurredString." $inputParams[$vectorIndex]->[0]  ";
      system($blurcommand);

      my $displaycommand0 = "plm2pnm trowel_out.plm -o pork.ppm";
      my $displaycommand1 = "xv pork.ppm -geometry +320+0&";
      system($displaycommand0);
      system($displaycommand1);

      my $cementiCommand = "cementi trowel_out.plm tmp.plm $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] \n";
      system($cementiCommand);
    }

 else{  ## we shouldn't blur this file
    my $inFile = $inputParams[$vectorIndex]->[0];
    system("cementi trowel_out.plm $inFile $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] \n");

system("sleep 1");

    my $viewimage = "cementinit $inFile 100 100 100 -o pork.plm\n";
    system($viewimage);
    my $displaycommand0 = "plm2pnm pork.plm -o pork100.ppm";
    system($displaycommand0);
    system("xv pork100.ppm -geometry +0+0&");
 
    my $viewimage = "cementinit $inFile 10 10 10 -o pork.plm\n";
    system($viewimage);
    my $displaycommand0 = "plm2pnm pork.plm -o pork10.ppm";
    system($displaycommand0);
    system("xv pork10.ppm -geometry +0+0&");

    my $viewimage = "cementinit $inFile 1 1 1 -o pork.plm\n";
    system($viewimage);
    my $displaycommand0 = "plm2pnm pork.plm -o pork1.ppm";
    system($displaycommand0);
    system("xv pork1.ppm -geometry +0+0&");

    my $displaycommand1 = "plm2pnm trowel_out.plm -o pork.ppm";
    my $displaycommand2 = "xv pork.ppm -geometry +320+0&";
    system($displaycommand1);
    system($displaycommand2);

  }
}
if($blurredString ne ""){
	print "$blurredString have been blurred\n";
}

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

