#!/usr/bin/perl

#######################################################################
# LiVES transition_fade2 plugin, version 2
# Compiled with Builder version 3.2.0
# autogenerated from script by Salsaman|

# 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 [<in2_prefix> [<in_prefix>]] <out_prefix> <out_ext> <start> <end>
#  <width> <height> <img_ext> <fps> [<img2_ext> <start2> <handle2>]  <parameters>
# you should create all output frames $out_prefix%08d$out_ext in numerical 
# from start to end inclusive,
# using $in_prefix%08d$in_ext and $in2_prefix%08d$img2_ext as applicable.
# in / out images are in current dir, In2 images can be located in ../handle2 and numbered from 
# Each time calling sig_progress (see smogrify) - writes current frame number to 
# <dir>/.status
# and checking for pause (test for a file of that name in current dir - if present just sleep until deleted)
#
# Any errors - 
# write "error|msg1|msg2|msg3|" to .status
# msgn must not contain "\n", but can be omitted

# 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;
setlocale(LC_NUMERIC, "C");

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 "32768\n";
    exit 0;
}

if ($command eq "version") {
    print "transition_fade2 version 2 : builder version 3.2.0\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 "Fade|Fading|1|2|\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 "fstart|_Fade start|num0|50|0|100|\n";
    print "fend|Fade _end|num0|50|0|100|\n";
    print "w|_Width|num0|0|0|10000|\n";
    print "h|_Height|num0|0|0|10000|\n";
    print "cx|Centre _X|num0|0|0|10000|\n";
    print "cy|Centre _Y|num0|0|0|10000|\n";
    print "finf|Frame _in frame position of clipboard...|bool|0|\n";
    print "env|Envelope length|num0|2|2|100000|\n";
    exit 0;
}

if ($command eq "get_param_window") {
    print "layout|p0|\"%\"|p1|\"%\"||\n";
    print "layout|p7|\"frames\"||\n";
    print "layout|hseparator||\n";
    print "layout|p6||\n";
    print "layout|p2|p3||\n";
    print "layout|p4|p5||\n";
    print "special|mergealign|0|1||\n";
    exit 0;
}

if ($command eq "get_onchange") {
    print "init|\n";
    print "2|\n";
    print "3|\n";
    print "6|\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("composite") eq "") {
      &sig_error("You must install 'composite' 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 = 50;
    }
    else {
      $p0 = $ARGV[1];
    }
    unless (defined($ARGV[2])) {
      $p1 = 50;
    }
    else {
      $p1 = $ARGV[2];
    }
    unless (defined($ARGV[3])) {
      $p2 = 0;
    }
    else {
      $p2 = $ARGV[3];
    }
    unless (defined($ARGV[4])) {
      $p3 = 0;
    }
    else {
      $p3 = $ARGV[4];
    }
    unless (defined($ARGV[5])) {
      $p4 = 0;
    }
    else {
      $p4 = $ARGV[5];
    }
    unless (defined($ARGV[6])) {
      $p5 = 0;
    }
    else {
      $p5 = $ARGV[6];
    }
    unless (defined($ARGV[7])) {
      $p6 = 0;
    }
    else {
      $p6 = $ARGV[7];
    }
    unless (defined($ARGV[8])) {
      $p7 = 2;
    }
    else {
      $p7 = $ARGV[8];
    }
    $! = 0;
    if ($p0 >= 0) {
        $p0 = int(POSIX::strtod($p0) * 1 + .5) / 1;
    } else {
        $p0 = int(POSIX::strtod($p0) * 1 - .5) / 1;
    }
    if ($p0 < 0) {
       &sig_error("fstart must be >= 0");
       exit 1;
    }
    if ($p0 > 100) {
       &sig_error("fstart must be <= 100");
       exit 1;
    }
    $! = 0;
    if ($p1 >= 0) {
        $p1 = int(POSIX::strtod($p1) * 1 + .5) / 1;
    } else {
        $p1 = int(POSIX::strtod($p1) * 1 - .5) / 1;
    }
    if ($p1 < 0) {
       &sig_error("fend must be >= 0");
       exit 1;
    }
    if ($p1 > 100) {
       &sig_error("fend must be <= 100");
       exit 1;
    }
    $! = 0;
    if ($p2 >= 0) {
        $p2 = int(POSIX::strtod($p2) * 1 + .5) / 1;
    } else {
        $p2 = int(POSIX::strtod($p2) * 1 - .5) / 1;
    }
    if ($p2 < 0) {
       &sig_error("w must be >= 0");
       exit 1;
    }
    if ($p2 > 10000) {
       &sig_error("w must be <= 10000");
       exit 1;
    }
    $! = 0;
    if ($p3 >= 0) {
        $p3 = int(POSIX::strtod($p3) * 1 + .5) / 1;
    } else {
        $p3 = int(POSIX::strtod($p3) * 1 - .5) / 1;
    }
    if ($p3 < 0) {
       &sig_error("h must be >= 0");
       exit 1;
    }
    if ($p3 > 10000) {
       &sig_error("h must be <= 10000");
       exit 1;
    }
    $! = 0;
    if ($p4 >= 0) {
        $p4 = int(POSIX::strtod($p4) * 1 + .5) / 1;
    } else {
        $p4 = int(POSIX::strtod($p4) * 1 - .5) / 1;
    }
    if ($p4 < 0) {
       &sig_error("cx must be >= 0");
       exit 1;
    }
    if ($p4 > 10000) {
       &sig_error("cx 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 < 0) {
       &sig_error("cy must be >= 0");
       exit 1;
    }
    if ($p5 > 10000) {
       &sig_error("cy 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 < 2) {
       &sig_error("env must be >= 2");
       exit 1;
    }
    if ($p7 > 100000) {
       &sig_error("env must be <= 100000");
       exit 1;
    }
    if ($img_ext eq ".png") {
        $img_prefix = "PNG32:";
    } else {
        $img_prefix = "";
    }

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

    unless (defined($img_ext2)) {
        $img_ext2 = $img_ext;
    }

    if ($img_ext2 eq ".png") {
        $img_prefix2 = "PNG32:";
    } else {
        $img_prefix2 = "";
    }

    $antialias = &config_get("antialias", $configfile);
    if ($p6) {
        $x=$p4-int($p2/2+.5);
        $y=$p5-int($p3/2+.5);
    
    	if ($x<0) {$x=0};
    	if ($y<0) {$y=0};
    }
    else {
        $p2=$width;
        $p3=$height;
        $x=$y=0;
    }
    $mystep=0.;
    if ($end>$start) {
        $mystep=($p1-$p0)/($end-$start);
    }
    $mystep*=($end-$start+1.)/$p7;
    $myorig_val=$p0;
    $mycount=0;

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


################# loop through frames #################
    $frame2 = $start2;
    if (!($img_ext2 eq $img_ext) && &location("convert") eq "") {
        &sig_error("'convert' is required by this plugin.", "Please install imagemagick and try again.");
        exit 1;
    }

    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.");
        }
        $name2 = &mkname($frame2);
        $in2 = "$clipboard/$name2$img_ext2";
        unless (-f $in2) {
            # end of clipboard reached, loop back to start
            $frame2 = $start2;
            $name2 = &mkname($frame2);
            $in2 = "$clipboard/$name2$img_ext2";
        }
        unless ($img_ext2 eq $img_ext) {
            system("$convert_command $img_prefix2\"$in2\" $img_prefix\"$clipboard/$name2$img_ext\"");
            $img_prefix2 = $img_prefix;
        }
        $out = "$name$out_ext";

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

        `flock $in true`;
        # wait for front end to create 
        while (! -s $in2) {
            sleep 1;
        }

        `flock $in2 true`;
##################### the all-important bit #######################

        system("$composite_command -compose plus -dissolve ".int($p0)." -geometry $p2!x$p3!+$x!+$y! $img_prefix2$in2 -size $width!x$height! $in $out_prefix$out");
        $p0+=$mystep;
        if (($p1>0&&$p0>$p1*1.)||($p1<0&&$p0<$p1*1.)) {$p0=$p1*1.;}
        if (++$mycount==$p7) {$p0=$myorig_val; $mycount=0;}

###################################################################
        unless ($img_ext2 eq $img_ext) {
            unlink $in2;
        }
        $frame2++;
        for (my $i = 0; $i < 5; $i++) {
            if (! -s $out) {
                sleep 1;
            }
        }

        if (! -s $out) {
            print STDERR "Warning: effect plugin transition_fade2 skipped frame $frame !\n";
            return 1;
        }

        &sig_progress($frame);

        }
    return 1;
}



########## Post loop code ############
if ($command eq "clear") {
    exit 0;
}

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

if ($command eq "onchange_2") {
    $p0 = @ARGV[1];
    $p0_min = @ARGV[2];
    $p0_max = @ARGV[3];
    $p1 = @ARGV[4];
    $p1_min = @ARGV[5];
    $p1_max = @ARGV[6];
    $p2 = @ARGV[7];
    $p2_min = @ARGV[8];
    $p2_max = @ARGV[9];
    $p3 = @ARGV[10];
    $p3_min = @ARGV[11];
    $p3_max = @ARGV[12];
    $p4 = @ARGV[13];
    $p4_min = @ARGV[14];
    $p4_max = @ARGV[15];
    $p5 = @ARGV[16];
    $p5_min = @ARGV[17];
    $p5_max = @ARGV[18];
    $p6 = @ARGV[19];
    $p7 = @ARGV[20];
    $p7_min = @ARGV[21];
    $p7_max = @ARGV[22];
    $width = @ARGV[23];
    $height = @ARGV[24];
    $start = @ARGV[25];
    $end = @ARGV[26];
    $last = @ARGV[27];
    $length = $end - $start + 1;
    $width2 = @ARGV[28];
    $height2 = @ARGV[29];

    $p4_max=$width-1-int($p2/2+.5);$p4_min=int($p2/2+.5);

    print "$p0 $p0_min $p0_max $p1 $p1_min $p1_max $p2 $p2_min $p2_max $p3 $p3_min $p3_max $p4 $p4_min $p4_max $p5 $p5_min $p5_max $p6 $p7 $p7_min $p7_max ";
    exit 0;
}

if ($command eq "onchange_3") {
    $p0 = @ARGV[1];
    $p0_min = @ARGV[2];
    $p0_max = @ARGV[3];
    $p1 = @ARGV[4];
    $p1_min = @ARGV[5];
    $p1_max = @ARGV[6];
    $p2 = @ARGV[7];
    $p2_min = @ARGV[8];
    $p2_max = @ARGV[9];
    $p3 = @ARGV[10];
    $p3_min = @ARGV[11];
    $p3_max = @ARGV[12];
    $p4 = @ARGV[13];
    $p4_min = @ARGV[14];
    $p4_max = @ARGV[15];
    $p5 = @ARGV[16];
    $p5_min = @ARGV[17];
    $p5_max = @ARGV[18];
    $p6 = @ARGV[19];
    $p7 = @ARGV[20];
    $p7_min = @ARGV[21];
    $p7_max = @ARGV[22];
    $width = @ARGV[23];
    $height = @ARGV[24];
    $start = @ARGV[25];
    $end = @ARGV[26];
    $last = @ARGV[27];
    $length = $end - $start + 1;
    $width2 = @ARGV[28];
    $height2 = @ARGV[29];

    $p5_max=$height-1-int($p3/2+.5);$p5_min=int($p3/2+.5);

    print "$p0 $p0_min $p0_max $p1 $p1_min $p1_max $p2 $p2_min $p2_max $p3 $p3_min $p3_max $p4 $p4_min $p4_max $p5 $p5_min $p5_max $p6 $p7 $p7_min $p7_max ";
    exit 0;
}

if ($command eq "onchange_6") {
    $p0 = @ARGV[1];
    $p0_min = @ARGV[2];
    $p0_max = @ARGV[3];
    $p1 = @ARGV[4];
    $p1_min = @ARGV[5];
    $p1_max = @ARGV[6];
    $p2 = @ARGV[7];
    $p2_min = @ARGV[8];
    $p2_max = @ARGV[9];
    $p3 = @ARGV[10];
    $p3_min = @ARGV[11];
    $p3_max = @ARGV[12];
    $p4 = @ARGV[13];
    $p4_min = @ARGV[14];
    $p4_max = @ARGV[15];
    $p5 = @ARGV[16];
    $p5_min = @ARGV[17];
    $p5_max = @ARGV[18];
    $p6 = @ARGV[19];
    $p7 = @ARGV[20];
    $p7_min = @ARGV[21];
    $p7_max = @ARGV[22];
    $width = @ARGV[23];
    $height = @ARGV[24];
    $start = @ARGV[25];
    $end = @ARGV[26];
    $last = @ARGV[27];
    $length = $end - $start + 1;
    $width2 = @ARGV[28];
    $height2 = @ARGV[29];

    if ($p6) {$p4_max=$width-1-int($p2/2+.5);$p4_min=int($p2/2+.5);$p5_max=$height-1-int($p3/2+.5);$p5_min=int($p3/2+.5);}

    print "$p0 $p0_min $p0_max $p1 $p1_min $p1_max $p2 $p2_min $p2_max $p3 $p3_min $p3_max $p4 $p4_min $p4_max $p5 $p5_min $p5_max $p6 $p7 $p7_min $p7_max ";
    exit 0;
}

if ($command eq "onchange_init") {
    $p0 = @ARGV[1];
    $p0_min = @ARGV[2];
    $p0_max = @ARGV[3];
    $p1 = @ARGV[4];
    $p1_min = @ARGV[5];
    $p1_max = @ARGV[6];
    $p2 = @ARGV[7];
    $p2_min = @ARGV[8];
    $p2_max = @ARGV[9];
    $p3 = @ARGV[10];
    $p3_min = @ARGV[11];
    $p3_max = @ARGV[12];
    $p4 = @ARGV[13];
    $p4_min = @ARGV[14];
    $p4_max = @ARGV[15];
    $p5 = @ARGV[16];
    $p5_min = @ARGV[17];
    $p5_max = @ARGV[18];
    $p6 = @ARGV[19];
    $p7 = @ARGV[20];
    $p7_min = @ARGV[21];
    $p7_max = @ARGV[22];
    $width = @ARGV[23];
    $height = @ARGV[24];
    $start = @ARGV[25];
    $end = @ARGV[26];
    $last = @ARGV[27];
    $length = $end - $start + 1;
    $width2 = @ARGV[28];
    $height2 = @ARGV[29];

    $p2=$width2; $p2_max=$width;
    $p3=$height2; $p3_max=$height;
    $p4=int($width/2+.5);
    $p5=int($height/2+.5);
    $p7=($end-$start+1); $p7_max=$p7;

    print "$p0 $p0_min $p0_max $p1 $p1_min $p1_max $p2 $p2_min $p2_max $p3 $p3_min $p3_max $p4 $p4_min $p4_max $p5 $p5_min $p5_max $p6 $p7 $p7_min $p7_max ";
    exit 0;
}
