#!/usr/bin/perl

#######################################################################
# LiVES noisify plugin, version 1.7
# RFX version 1.8
# autogenerated from script by Marco De la Cruz|

# rendered plugins should accept:
# <plugin_name> version (return <plugin_name> version <version>)
# <plugin_name> get_define
# <plugin_name> get_capabilities
# <plugin_name> get_description (e.g. "Edge detect|Edge detecting|1|1|")
# <plugin_name> clear (clean up any plugin generated temp files)
# and optionally any of: 
# <plugin_name> get_parameters
# <plugin_name> get_param_window
# <plugin_name> get_onchange
# <plugin_name> onchange_<when> (for any triggers, e.g. onchange_init)
#
# they must accept:
# <plugin_name> process <parameters>

# You should not skip any frames, if a frame is not changed you must do:
# `cp $in $out`
#
# for *non-Perl* plugins, LiVES will call:
# <plugin_name> process "<dir>" <in_ext> <out_ext> <start> <end>
#  <width> <height> <parameters>
# first you should chdir to <dir>
# then you should create all output frames %8d$out_ext in numerical 
# from start to end inclusive,
# each time calling sig_progress (see smogrify) - writes current frame number to 
# <dir>/.status
# and checking for pause
#
# Any errors should be transmitted as in sig_error - 
# write "error|msg1|msg2|msg3|" to <dir>/.status
# msgn must not contain "\n", but can be omitted

# output frames should be named %8d$out_ext in the same directory
# after processing, you should leave no gaps in out frames, you should not resize
# or change the palette from RGB24 (LiVES will check and autocorrect this soon)

# Also you must implement your own: &sig_error and &sig_progress


#######################################################################

use POSIX;

my $command=$ARGV[0];

if ($command eq "get_capabilities") {
    # capabilities is a bitmap field
    # 0x0001 == slow (hint to GUI)
    # 0x0002 == may resize (all frames to  x )
    # 0x0004 == block mode generator
    # 0x8000 == reserved
    print "32769\n";
    exit 0;
}

if ($command eq "version") {
    print "noisify version 1 : builder version 2.0.3\n";
    exit 0;
}

if ($command eq "get_define") {
    print "|1.7\n";
    exit 0;
}

if ($command eq "get_description") {
    #format here is "Menu entry|Action description|min_frames|number_of_in_channels|"
    # min_frames==-1 indicates a special "no processing" effect. This allows more
    #general parameter windows which are not really effects (e.g. frame_calculator)
    print "Noisify|Adjusting noise on|1|1|\n";
    exit 0;
}


if ($command eq "get_parameters") {
    # "name|label|type|other fields..."
    # eg. print "radius|_radius|num0|1|1|100|";
    # types can be numx,colRGB24,bool,string or string_list
    print "add|Add noise|bool|1|1|\n";
    print "red|Reduce noise|bool|0|1|\n";
    print "xx||bool|0|2|\n";
    print "every|every|num0|1|1|10000|\n";
    print "type|Noise type|string_list|0|Impulse|Uniform|Gaussian|Multiplicative|Laplacian|Poisson|\n";
    print "radius|Reduction radius|num0|1|1|10000|\n";
    print "yy||bool|0|0|\n";
    print "every2|every|num0|1|1|10000|\n";
    print "zz||bool|1|2|\n";
    print "const|Constant noise level|num0|1|1|50|\n";
    exit 0;
}

if ($command eq "get_param_window") {
    print "layout|p0|fill|p4||\n";
    print "layout|p8|p9||\n";
    print "layout|p2|\"Increase noise\"|p3|\"frame(s)\"||\n";
    print "layout|hseparator||\n";
    print "layout|p1|fill|p5||\n";
    print "layout|p6|\"Decrease noise\"|p7|\"frame(s)\"||\n";
    exit 0;
}

if ($command eq "get_onchange") {
    print "init|\n";
    exit 0;
}

#######################################################

if ($command eq "process") {

# in case of error, you should do:
# &sig_error("msg1","msg2","msg3","msg4"); [ msg's are optional, but must not
# contain newlines (\n) ]

##### check requirements first #######
    if (&location("convert") eq "") {
      &sig_error("You must install 'convert' before you can use this effect.");
      exit 1;
    }
    if (&location("convert") eq "") {
      &sig_error("You must install 'convert' before you can use this effect.");
      exit 1;
    }

###### handle parameters #############
# autogenerated from get_parameters

    unless (defined($ARGV[1])) {
      $p0=1;
    }
    else {
      $p0=$ARGV[1];
    }
    unless (defined($ARGV[2])) {
      $p1=0;
    }
    else {
      $p1=$ARGV[2];
    }
    unless (defined($ARGV[3])) {
      $p2=0;
    }
    else {
      $p2=$ARGV[3];
    }
    unless (defined($ARGV[4])) {
      $p3=1;
    }
    else {
      $p3=$ARGV[4];
    }
    unless (defined($ARGV[5])) {
      $p4=0;
    }
    else {
      $p4=$ARGV[5];
    }
    unless (defined($ARGV[6])) {
      $p5=1;
    }
    else {
      $p5=$ARGV[6];
    }
    unless (defined($ARGV[7])) {
      $p6=0;
    }
    else {
      $p6=$ARGV[7];
    }
    unless (defined($ARGV[8])) {
      $p7=1;
    }
    else {
      $p7=$ARGV[8];
    }
    unless (defined($ARGV[9])) {
      $p8=1;
    }
    else {
      $p8=$ARGV[9];
    }
    unless (defined($ARGV[10])) {
      $p9=1;
    }
    else {
      $p9=$ARGV[10];
    }
    $p0=~(~$p0);
    $p1=~(~$p1);
    $p2=~(~$p2);
    $!=0;
    if ($p3>=0) {
        $p3=int(POSIX::strtod($p3)*1+.5)/1;
    } else {
        $p3=int(POSIX::strtod($p3)*1-.5)/1;
    }
    if ($p3<1) {
       &sig_error("every must be >= 1");
       exit 1;
    }
    if ($p3>10000) {
       &sig_error("every must be <= 10000");
       exit 1;
    }
    $!=0;
    if ($p5>=0) {
        $p5=int(POSIX::strtod($p5)*1+.5)/1;
    } else {
        $p5=int(POSIX::strtod($p5)*1-.5)/1;
    }
    if ($p5<1) {
       &sig_error("radius must be >= 1");
       exit 1;
    }
    if ($p5>10000) {
       &sig_error("radius must be <= 10000");
       exit 1;
    }
    $p6=~(~$p6);
    $!=0;
    if ($p7>=0) {
        $p7=int(POSIX::strtod($p7)*1+.5)/1;
    } else {
        $p7=int(POSIX::strtod($p7)*1-.5)/1;
    }
    if ($p7<1) {
       &sig_error("every2 must be >= 1");
       exit 1;
    }
    if ($p7>10000) {
       &sig_error("every2 must be <= 10000");
       exit 1;
    }
    $p8=~(~$p8);
    $!=0;
    if ($p9>=0) {
        $p9=int(POSIX::strtod($p9)*1+.5)/1;
    } else {
        $p9=int(POSIX::strtod($p9)*1-.5)/1;
    }
    if ($p9<1) {
       &sig_error("const must be >= 1");
       exit 1;
    }
    if ($p9>50) {
       &sig_error("const must be <= 50");
       exit 1;
    }

   if ($img_ext eq ".png") {
        $img_prefix="PNG32:";
   } else {
        $img_prefix="";
   }

   if ($out_ext eq ".png") {
        $out_prefix="PNG32:";
   } else {
        $out_prefix="";
   }

    $noise="noise";
    if ($p0) {
       $pre="+";
    
       if ($p4==0) {
        $ntype="Impulse";
       }
       elsif ($p4==1) {
        $ntype="Uniform";
       }
       elsif ($p4==2) {
        $ntype="Gaussian";
       }
       elsif ($p4==3) {
        $ntype="Multiplicative";
       }
       elsif ($p4==4) {
        $ntype="Laplacian";
       }
       elsif ($p4==5) {
        $ntype="Poisson";
       }
    }
    elsif ($p1) {
       $pre="-";
       $ntype="$p5";
    }
    $param=" $pre$noise $ntype ";
    $nstep=1;
    $fstep=1;
    if ($p2) {
       $nadd="$p3";
    }
    elsif ($p6) {
       $nadd="$p6";
    }
    else {
       $nadd=1;
    }
    if ($p8) {
       $cnl="$p9";
    }
    else {
       $cnl=1;
    }

    if ($start==0) {$start = 1;}


################# loop through frames #################
   if ($img_ext eq ".png") {
        $img_prefix="PNG32:";
   } else {
        $img_prefix="";
   }

   if ($out_ext eq ".png") {
        $out_prefix="PNG32:";
   } else {
        $out_prefix="";
   }

    for ($frame=$start;$frame<=$end;$frame++) {
	# sig progress will update the progress bar from $start->$end
	$name=&mkname($frame);
	$in="$name$img_ext";

     if (!defined($end) || $end==0) {
         print STDERR "WARNING: generator plugin did not set $end !";
         &sig_error("Generator plugin did not set $end.");
     }
	$out="$name$out_ext";

# wait for front end to create 
 while (!-f $in) {
     sleep 1;
 }

##################### the all-important bit #######################

        if ($p2||$p6) {
           if ($nstep==$fstep) {
              if ($nstep <= 50) {
                 $totnoise=$param x $nstep;
              }
              else {
                  $totnoise=$param x 50;
              }
              $nstep+=$nadd;
           }
        }
        else {
           $totnoise=$param x $cnl;
        }
        `$convert_command $img_prefix$in $totnoise $out_prefix$out`;
        $fstep++;

###################################################################
        $opfailed=0;
        if (! -f $out) {
            print STDERR "Warning: effect plugin noisify skipped frame $frame !\n";
            print STDERR "Retrying\n";
            $frame--;
            $opfailed=1;
        }
        unless ($opfailed) {
	    &sig_progress($frame);
        }
    }
    return 1;
}



########## Post loop code ############
if ($command eq "clear") {
    $start=$ARGV[1];
    $end=$ARGV[2];
    $img_ext=$ARGV[3];

    exit 0;
}

########## Triggers ############

if ($command eq "onchange_init") {
    $p0=@ARGV[1];
    $p1=@ARGV[2];
    $p2=@ARGV[3];
    $p3=@ARGV[4];
    $p3_min=@ARGV[5];
    $p3_max=@ARGV[6];
    $p4=@ARGV[7];
    $p5=@ARGV[8];
    $p5_min=@ARGV[9];
    $p5_max=@ARGV[10];
    $p6=@ARGV[11];
    $p7=@ARGV[12];
    $p7_min=@ARGV[13];
    $p7_max=@ARGV[14];
    $p8=@ARGV[15];
    $p9=@ARGV[16];
    $p9_min=@ARGV[17];
    $p9_max=@ARGV[18];
    $width=@ARGV[19];
    $height=@ARGV[20];
    $start=@ARGV[21];
    $end=@ARGV[22];
    $last=@ARGV[23];
    $length=$end-$start+1;

    $p8=1;

    print "$p0 $p1 $p2 $p3 $p3_min $p3_max $p4 $p5 $p5_min $p5_max $p6 $p7 $p7_min $p7_max $p8 $p9 $p9_min $p9_max ";
    exit 0;
}
