#!/usr/bin/perl

#######################################################################
# LiVES transition_bwthresh plugin v0.1

# rendered plugins should accept:
# <plugin_name> version (return <plugin_name> version <version>)
# <plugin_name> get_capabilities
# <plugin_name> get_description (e.g. "Edge detect|Edge detecting|1|1|")
# 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


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

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 )
    # 0x8000 == reserved
    print "32768\n";
    exit 0;
}

if ($command eq "version") {
    print "transition_bwthresh version 1.0 : builder version 0.9.5\n";
    exit 0;
}

if ($command eq "get_description") {
    #format here is "Menu entry|Action description|min_frames|script_file_name|"
    # 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 "B/W Threshold|B/W thresholding|1|2||\n";
    exit 0;
}


if ($command eq "get_parameters") {
    # "name|group|type|default|min|max|"
    # eg. print "Radius|0|num0|1|1|100|";
    # types can be numx,colRGB24,bool,string or string_list
    print "Thres_hold|0|num0|8000|0|65535|\n";
    print "_Black is transparent|1|bool|1|\n";
    print "_White is transparent|1|bool|0|\n";
    print "Clipboard _over selection|2|bool|1|\n";
    print "Clipboard _under selection|2|bool|0|\n";
    print "_Width|0|num0|0|0|10000|\n";
    print "_Height|0|num0|0|0|10000|\n";
    print "Centre _X|0|num0|0|0|10000|\n";
    print "Centre _Y|0|num0|0|0|10000|\n";
    print "Frame _in frame position of top frame...|0|bool|0|\n";
    exit 0;
}

if ($command eq "get_param_window") {
    print "layout|p1|\"(0 = no transparency, 65535 = all transparent)\"||\n";
    print "layout|p2|p3||\n";
    print "layout|hseparator||\n";
    print "layout|p10||\n";
    print "layout|p6|p7||\n";
    print "layout|p8|p9||\n";
    print "layout|hseparator||\n";
    print "layout|$p4|$p5||\n";
    exit 0;
}

if ($command eq "get_onchange") {
    print "init|\n";
    print "6|\n";
    print "7|\n";
    print "10|\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) ]

    $livesexitcode=1;
    $SIG{'HUP'}="dopost";

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

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

    unless (defined($ARGV[1])) {
      $p1=8000;
    }
    else {
      $p1=$ARGV[1];
    }
    unless (defined($ARGV[2])) {
      $p2=1;
    }
    else {
      $p2=$ARGV[2];
    }
    unless (defined($ARGV[3])) {
      $p3=0;
    }
    else {
      $p3=$ARGV[3];
    }
    unless (defined($ARGV[4])) {
      $p4=1;
    }
    else {
      $p4=$ARGV[4];
    }
    unless (defined($ARGV[5])) {
      $p5=0;
    }
    else {
      $p5=$ARGV[5];
    }
    unless (defined($ARGV[6])) {
      $p6=0;
    }
    else {
      $p6=$ARGV[6];
    }
    unless (defined($ARGV[7])) {
      $p7=0;
    }
    else {
      $p7=$ARGV[7];
    }
    unless (defined($ARGV[8])) {
      $p8=0;
    }
    else {
      $p8=$ARGV[8];
    }
    unless (defined($ARGV[9])) {
      $p9=0;
    }
    else {
      $p9=$ARGV[9];
    }
    unless (defined($ARGV[10])) {
      $p10=0;
    }
    else {
      $p10=$ARGV[10];
    }
    $p1=int($p1*1+.5)/1;
    if ($p1<0) {
       &sig_error("Thres_hold must be >= 0");
    }
    if ($p1>65535) {
       &sig_error("Thres_hold must be <= 65535");
    }
    $p2=~(~$p2);
    $p3=~(~$p3);
    $p4=~(~$p4);
    $p5=~(~$p5);
    $p6=int($p6*1+.5)/1;
    if ($p6<0) {
       &sig_error("_Width must be >= 0");
    }
    if ($p6>10000) {
       &sig_error("_Width must be <= 10000");
    }
    $p7=int($p7*1+.5)/1;
    if ($p7<0) {
       &sig_error("_Height must be >= 0");
    }
    if ($p7>10000) {
       &sig_error("_Height must be <= 10000");
    }
    $p8=int($p8*1+.5)/1;
    if ($p8<0) {
       &sig_error("Centre _X must be >= 0");
    }
    if ($p8>10000) {
       &sig_error("Centre _X must be <= 10000");
    }
    $p9=int($p9*1+.5)/1;
    if ($p9<0) {
       &sig_error("Centre _Y must be >= 0");
    }
    if ($p9>10000) {
       &sig_error("Centre _Y must be <= 10000");
    }
    $p10=~(~$p10);
        $nwidth=$width;
$nheight=$height;

    $rwidth=$width;
    $rheight=$height;
    $xoff=$yoff=0;
    
    if ($p10) {
     $rwidth=$p6;
     $rheight=$p7;
     $xoff=$p8-int($rwidth/2+.5);
     $yoff=$p9-int($rheight/2+.5);
    }
    
    `$convert_command -size $rwidth!x$rheight! xc:transparent -matte blank.png`;

################# loop through frames #################
    $frame2=$start2;
    for ($frame=$start;$frame<=$end;$frame++) {
	# sig progress will update the progress bar from $start->$end
	&sig_progress($frame);
	$name=&mkname($frame);
	$in="$name$img_ext";
        $name2=&mkname($frame2);
        $in2="$clipboard/$name2$img_ext";
        unless (-f $in2) {
            # end of clipboard reached, loop back to start
            $frame2=$start2;
            $name2=&mkname($frame2);
            $in2="$clipboard/$name2$img_ext";
	}
	$out="$name.mgk";

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

        if ($p4) {
                # transparent over
        	system("$convert_command +antialias $in2 -scale $rwidth!x$rheight! resize.png");
                if (!$p3) {
                    system("$convert_command resize.png -threshold $p1 mask.png");
                }
                else {
                    system("$convert_command resize.png -threshold ".(65535-$p1)." -negate mask.png");
                }
                system("$composite_command resize.png blank.png mask.png output.png");
                system("$composite_command -compose plus -dissolve 100/100/100 -geometry $rwidth!x$rheight!+$xoff!+$yoff! output.png $in $frame.tmp");
        }
        else {
        	#transparent under
                if ($antialias eq "false") {
                    system("$convert_command +antialias $in2 -scale $width!x$height! resize.png");
                }
                else {
                    system("$convert_command $in2 -resize $width!x$height! resize.png");
                }
                system("$convert_command $in -resize $rwidth!x$rheight! resize2.png");
                if (!$p3) {
                    system("$convert_command -threshold ".($p1)." resize2.png mask.png");
                }
                else {
                    system("$convert_command -threshold ".(65535-$p1)." -negate resize2.png mask.png");
                }
               system("$composite_command resize2.png blank.png mask.png output.png");
               system("$composite_command -compose plus -dissolve 100/100/100 -geometry $rwidth!x$rheight!+$xoff!+$yoff! output.png resize.png $frame.tmp");
        }
        
        system("$convert_command +matte $frame.tmp $out");
        unlink "$frame.tmp";
        

###################################################################
        $frame2++;
        if (! -f $out) {
            print "Warning: effect plugin transition_bwthresh skipped frame $frame !";
            `cp $in $out`;
        }
    }
    $livesexitcode=0;
    &dopost;
    return 1;
}



########## Post loop code ############
sub dopost {
    unlink <mask.png blank.png output.png resize.png resize2.png>;
    if ($livesexitcode) {
        exit $livesexitcode;
    }
}

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

if ($command eq "onchange_6") {
    $p1=@ARGV[1];
    $p1_min=@ARGV[2];
    $p1_max=@ARGV[3];
    $p2=@ARGV[4];
    $p3=@ARGV[5];
    $p4=@ARGV[6];
    $p5=@ARGV[7];
    $p6=@ARGV[8];
    $p6_min=@ARGV[9];
    $p6_max=@ARGV[10];
    $p7=@ARGV[11];
    $p7_min=@ARGV[12];
    $p7_max=@ARGV[13];
    $p8=@ARGV[14];
    $p8_min=@ARGV[15];
    $p8_max=@ARGV[16];
    $p9=@ARGV[17];
    $p9_min=@ARGV[18];
    $p9_max=@ARGV[19];
    $p10=@ARGV[20];
    $width=@ARGV[21];
    $height=@ARGV[22];
    $start=@ARGV[23];
    $end=@ARGV[24];
    $last=@ARGV[25];
    $length=$end-$start+1;
    $width2=@ARGV[26];
    $height2=@ARGV[27];

    $p8_max=$width-1-int($p6/2+.5);$p8_min=int($p6/2+.5);

    print "$p1 $p1_min $p1_max $p2 $p3 $p4 $p5 $p6 $p6_min $p6_max $p7 $p7_min $p7_max $p8 $p8_min $p8_max $p9 $p9_min $p9_max $p10 ";
    exit 0;
}

if ($command eq "onchange_7") {
    $p1=@ARGV[1];
    $p1_min=@ARGV[2];
    $p1_max=@ARGV[3];
    $p2=@ARGV[4];
    $p3=@ARGV[5];
    $p4=@ARGV[6];
    $p5=@ARGV[7];
    $p6=@ARGV[8];
    $p6_min=@ARGV[9];
    $p6_max=@ARGV[10];
    $p7=@ARGV[11];
    $p7_min=@ARGV[12];
    $p7_max=@ARGV[13];
    $p8=@ARGV[14];
    $p8_min=@ARGV[15];
    $p8_max=@ARGV[16];
    $p9=@ARGV[17];
    $p9_min=@ARGV[18];
    $p9_max=@ARGV[19];
    $p10=@ARGV[20];
    $width=@ARGV[21];
    $height=@ARGV[22];
    $start=@ARGV[23];
    $end=@ARGV[24];
    $last=@ARGV[25];
    $length=$end-$start+1;
    $width2=@ARGV[26];
    $height2=@ARGV[27];

    $p9_max=$height-1-int($p7/2+.5);$p9_min=int($p7/2+.5);

    print "$p1 $p1_min $p1_max $p2 $p3 $p4 $p5 $p6 $p6_min $p6_max $p7 $p7_min $p7_max $p8 $p8_min $p8_max $p9 $p9_min $p9_max $p10 ";
    exit 0;
}

if ($command eq "onchange_10") {
    $p1=@ARGV[1];
    $p1_min=@ARGV[2];
    $p1_max=@ARGV[3];
    $p2=@ARGV[4];
    $p3=@ARGV[5];
    $p4=@ARGV[6];
    $p5=@ARGV[7];
    $p6=@ARGV[8];
    $p6_min=@ARGV[9];
    $p6_max=@ARGV[10];
    $p7=@ARGV[11];
    $p7_min=@ARGV[12];
    $p7_max=@ARGV[13];
    $p8=@ARGV[14];
    $p8_min=@ARGV[15];
    $p8_max=@ARGV[16];
    $p9=@ARGV[17];
    $p9_min=@ARGV[18];
    $p9_max=@ARGV[19];
    $p10=@ARGV[20];
    $width=@ARGV[21];
    $height=@ARGV[22];
    $start=@ARGV[23];
    $end=@ARGV[24];
    $last=@ARGV[25];
    $length=$end-$start+1;
    $width2=@ARGV[26];
    $height2=@ARGV[27];

    if ($p10) {$p8_max=$width-1-int($p6/2+.5);$p8_min=int($p6/2+.5);$p9_max=$height-1-int($p7/2+.5);$p9_min=int($p7/2+.5);}

    print "$p1 $p1_min $p1_max $p2 $p3 $p4 $p5 $p6 $p6_min $p6_max $p7 $p7_min $p7_max $p8 $p8_min $p8_max $p9 $p9_min $p9_max $p10 ";
    exit 0;
}

if ($command eq "onchange_init") {
    $p1=@ARGV[1];
    $p1_min=@ARGV[2];
    $p1_max=@ARGV[3];
    $p2=@ARGV[4];
    $p3=@ARGV[5];
    $p4=@ARGV[6];
    $p5=@ARGV[7];
    $p6=@ARGV[8];
    $p6_min=@ARGV[9];
    $p6_max=@ARGV[10];
    $p7=@ARGV[11];
    $p7_min=@ARGV[12];
    $p7_max=@ARGV[13];
    $p8=@ARGV[14];
    $p8_min=@ARGV[15];
    $p8_max=@ARGV[16];
    $p9=@ARGV[17];
    $p9_min=@ARGV[18];
    $p9_max=@ARGV[19];
    $p10=@ARGV[20];
    $width=@ARGV[21];
    $height=@ARGV[22];
    $start=@ARGV[23];
    $end=@ARGV[24];
    $last=@ARGV[25];
    $length=$end-$start+1;
    $width2=@ARGV[26];
    $height2=@ARGV[27];

    $p6=$width2; $p6_max=$width;
    $p7=$height2; $p7_max=$height;
    $p8=int($width/2+.5);
    $p9=int($height/2+.5);

    print "$p1 $p1_min $p1_max $p2 $p3 $p4 $p5 $p6 $p6_min $p6_max $p7 $p7_min $p7_max $p8 $p8_min $p8_max $p9 $p9_min $p9_max $p10 ";
    exit 0;
}
