#!/usr/bin/perl

#######################################################################
# LiVES edge plugin v0.1

# rendered plugins must accept:
# <plugin_name> get_capabilities
# <plugin_name> get_action_description (e.g. "Edge detecting", "Trimming")
# <plugin_name> get_parameters
# <plugin_name> get_param_window
# <plugin_name> process <parameters>

# parameters will not be returned yet, get_param_window needs implementing first

# for *non-Perl* plugins, LiVES will call:
# <plugin_name> process <dir> <img_ext> <start> <end> [<width> <height>] <parameters>
# Note: width and height are not passed yet, but this is planned

# first you should chdir to <dir>
# then you should process all frames %8d$img_ext from start to end
# each time calling sig_progress (see smogrify) - writes current frame number to 
# <dir>/.status
# any errors should be transmitted as in sig_error - 
# write "error|msg1|msg2|msg3|" to <dir>/.status
# msg must not contain "\n", but can be omitted

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

# in other words, you must implement your own: &sig_error, &sig_progress


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

my $command=$ARGV[0];

if ($command eq "get_capabilities") {
    # capabilities is a bitmap field
    # 0x8000 == is Perl
    print 0x8000;
    exit 0;
}

if ($command eq "get_action_description") {
    print "Edge detecting";
    exit 0;
}


if ($command eq "get_parameters") {
    # to be decided
    # need some way to tell LiVES to throw up a pre window for radius,
    # something like "Label|type|min|default|max|exceptions|"
    # eg. print "Radius|int|1|1|100|";
    # types can be int,float,string,color...maybe more
    print "Radius|int|1|1|100";
    exit 0;
}

if ($command eq "get_param_window") {
    # to be decided
    # may need some way to describe a pre window for parameters
    print "test";
    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) ]


###### handle parameters (not returned yet) #############
# autogenerated from get_parameters
    unless (defined($ARGV[1])) {
      $p1=1;
    }
    else {
      $p1=$ARGV[1];
    }
    if ($p1<1) {
       &sig_error("Radius must be >= 1");
    }
    if ($p1>100) {
       &sig_error("Radius must be <= 100");
    }
    #pre code goes here

################# loop through frames #################
    for ($i=$start;$i<=$end;$i++) {
	# sig progress will update the progress bar from $start->$end
	&sig_progress($i);
	$name=&mkname($i);
	$in="$name$img_ext";
	$out="$name.mgk";

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

    system("$convert_command -edge $p1 $in $out");

###################################################################
    }

    #post code goes here

    return 1;
}
