#!/usr/bin/perl

#######################################################################
# LiVES gen_text plugin, version 1.7
# RFX version 1.7
# 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 "<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 "32768\n";
    exit 0;
}

if ($command eq "version") {
    print "gen_text version 2 : builder version 1.0.10\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 "Generate title frames|Generating title frames|1|0|\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 "text|_Text|string|LiVES|1024|\n";
    print "xoffs|_X offset|num0|0|0|10000|\n";
    print "yoffs|_Y offset|num0|0|0|10000|\n";
    print "fcol|_Fill colour|colRGB24|255|255|255|\n";
    print "scol|_Stroke colour|colRGB24|255|255|255|\n";
    print "psize|_Point size|num0|32|10|120|\n";
    print "bgcol|_Background colour|colRGB24|0|0|0|\n";
    print "nframes|Number of _frames|num0|100|1|100000|\n";
    print "width|Frame _width|num0|640|2|10000|\n";
    print "height|Frame _height|num0|480|2|10000|\n";
    print "font|_Font|string_list|0|Default|AvantGarde-Book|AvantGarde-BookOblique|AvantGarde-Demi|AvantGarde-DemiOblique|Bookman-Demi|Bookman-DemiItalic|Bookman-Light|Bookman-LightItalic|Courier|Courier-Bold|Courier-Oblique|Courier-BoldOblique|Helvetica|Helvetica-Bold|Helvetica-BoldOblique|Helvetica-Narrow|Helvetica-Narrow-Oblique|Helvetica-Narrow-Bold|Helvetica-Narrow-BoldOblique|NewCenturySchlbk-Roman|NewCenturySchlbk-Italic|NewCenturySchlbk-Bold|NewCenturySchlbk-BoldItalic|Palatino-Roman|Palatino-Italic|Palatino-Bold|Palatino-BoldItalic|Times-Roman|Times-Bold|Times-Italic|Times-BoldItalic|Symbol|\n";
    exit 0;
}

if ($command eq "get_param_window") {
    print "layout|p7|p8|p9||\n";
    print "layout|p1|fill|p2|fill|p5||\n";
    exit 0;
}

if ($command eq "get_onchange") {
    print "3|\n";
    print "init|\n";
    print "8|\n";
    print "9|\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;
    }

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

    unless (defined($ARGV[1])) {
      $p0="LiVES";
    }
    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=255;
    }
    else {
      $p3=$ARGV[4];
    }
    unless (defined($ARGV[5])) {
      $p4=255;
    }
    else {
      $p4=$ARGV[5];
    }
    unless (defined($ARGV[6])) {
      $p5=32;
    }
    else {
      $p5=$ARGV[6];
    }
    unless (defined($ARGV[7])) {
      $p6=0;
    }
    else {
      $p6=$ARGV[7];
    }
    unless (defined($ARGV[8])) {
      $p7=100;
    }
    else {
      $p7=$ARGV[8];
    }
    unless (defined($ARGV[9])) {
      $p8=640;
    }
    else {
      $p8=$ARGV[9];
    }
    unless (defined($ARGV[10])) {
      $p9=480;
    }
    else {
      $p9=$ARGV[10];
    }
    unless (defined($ARGV[11])) {
      $p10=0;
    }
    else {
      $p10=$ARGV[11];
    }
    $!=0;
    $p1=int(POSIX::strtod($p1)*1+.5)/1;
    if ($p1<0) {
       &sig_error("xoffs must be >= 0");
       exit 1;
    }
    if ($p1>10000) {
       &sig_error("xoffs must be <= 10000");
       exit 1;
    }
    $!=0;
    $p2=int(POSIX::strtod($p2)*1+.5)/1;
    if ($p2<0) {
       &sig_error("yoffs must be >= 0");
       exit 1;
    }
    if ($p2>10000) {
       &sig_error("yoffs must be <= 10000");
       exit 1;
    }
    $!=0;
    $p5=int(POSIX::strtod($p5)*1+.5)/1;
    if ($p5<10) {
       &sig_error("psize must be >= 10");
       exit 1;
    }
    if ($p5>120) {
       &sig_error("psize must be <= 120");
       exit 1;
    }
    $!=0;
    $p7=int(POSIX::strtod($p7)*1+.5)/1;
    if ($p7<1) {
       &sig_error("nframes must be >= 1");
       exit 1;
    }
    if ($p7>100000) {
       &sig_error("nframes must be <= 100000");
       exit 1;
    }
    $!=0;
    $p8=int(POSIX::strtod($p8)*1+.5)/1;
    if ($p8<2) {
       &sig_error("width must be >= 2");
       exit 1;
    }
    if ($p8>10000) {
       &sig_error("width must be <= 10000");
       exit 1;
    }
    $!=0;
    $p9=int(POSIX::strtod($p9)*1+.5)/1;
    if ($p9<2) {
       &sig_error("height must be >= 2");
       exit 1;
    }
    if ($p9>10000) {
       &sig_error("height must be <= 10000");
       exit 1;
    }
$p3=int($p3);
if ($p3>0xFFFFFF||$p3<0) {
    &sig_error("Invalid colour for fcol.");
    exit 1;
}
$p3_red=int($p3/65536);
$p3-=$p3_red*65536;
$p3_green=int($p3/256);
$p3-=$p3_green*256;
$p3_blue=$p3;
$p4=int($p4);
if ($p4>0xFFFFFF||$p4<0) {
    &sig_error("Invalid colour for scol.");
    exit 1;
}
$p4_red=int($p4/65536);
$p4-=$p4_red*65536;
$p4_green=int($p4/256);
$p4-=$p4_green*256;
$p4_blue=$p4;
$p6=int($p6);
if ($p6>0xFFFFFF||$p6<0) {
    &sig_error("Invalid colour for bgcol.");
    exit 1;
}
$p6_red=int($p6/65536);
$p6-=$p6_red*65536;
$p6_green=int($p6/256);
$p6-=$p6_green*256;
$p6_blue=$p6;

    $end=0;
    $end=$p7;
    $cstring0=&RGB24_to_string($p6_red,$p6_green,$p6_blue);
    
    $tmpimg="temp$img_ext";
    
    `$convert_command -size $p8!x$p9! xc:$cstring0 $tmpimg`;
    
    $p10--;
    
    if ($p10==-1) {
    	$font="";
     } elsif ($p10==0) {
       $fo="AvantGarde-Book";
     } elsif ($p10==1) {
      $fo="AvantGarde-BookOblique";
     } elsif ($p10==2) {
       $fo="AvantGarde-Demi";
     } elsif ($p10==3) {
       $fo="AvantGarde-DemiOblique";
     } elsif ($p10==4) {
       $fo="Bookman-Demi";
     } elsif ($p10==5) {
       $fo="Bookman-DemiItalic";
     } elsif ($p10==6) { 
       $fo="Bookman-Light";
     } elsif ($p10==7) {
       $fo="Bookman-LightItalic";
     } elsif ($p10==8) {
       $fo="Courier";
     } elsif ($p10==9) { 
       $fo="Courier-Bold";
     } elsif ($p10==10) {
       $fo="Courier-Oblique";
     } elsif ($p10==11) {
       $fo="Courier-BoldOblique";
     } elsif ($p10==12) { 
       $fo="Helvetica";
     } elsif ($p10==13) { 
       $fo="Helvetica-Bold";
     } elsif ($p10==14) {
       $fo="Helvetica-BoldOblique";
     } elsif ($p10==15) {
       $fo="Helvetica-Narrow";
     } elsif ($p10==16) {
       $fo="Helvetica-Narrow-Oblique";
     } elsif ($p10==17) {
       $fo="Helvetica-Narrow-Bold";
     } elsif ($p10==18) {
       $fo="Helvetica-Narrow-BoldOblique";
     } elsif ($p10==19) {
       $fo="NewCenturySchlbk-Roman";
     } elsif ($p10==20) {
       $fo="NewCenturySchlbk-Italic";
     } elsif ($p10==21) {
       $fo="NewCenturySchlbk-Bold  ";
     } elsif ($p10==22) {
       $fo="NewCenturySchlbk-BoldItalic";
     } elsif ($p10==23) {
       $fo="Palatino-Roman";
     } elsif ($p10==24) { 
       $fo="Palatino-Italic";
     } elsif ($p10==25) { 
       $fo="Palatino-Bold  "; 
     } elsif ($p10==26) {
       $fo="Palatino-BoldItalic";
     } elsif ($p10==27) { 
       $fo="Times-Roman";
     } elsif ($p10==28) { 
       $fo="Times-Bold ";
     } elsif ($p10==29) {
       $fo="Times-Italic";
     } elsif ($p10==30) { 
       $fo="Times-BoldItalic";
     } elsif ($p10==31) { 
       $fo="Symbol";
     }
    
    if ($p10>=0) {
      $font="-font $fo";
    }

################# loop through frames #################
    for ($frame=$start;$frame<=$end;$frame++) {
	# sig progress will update the progress bar from $start->$end
	$name=&mkname($frame);
	$out="$name$out_ext";

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

        $cstring1=&RGB24_to_string($p3_red,$p3_green,$p3_blue);
        $cstring2=&RGB24_to_string($p4_red,$p4_green,$p4_blue);
        
        `$convert_command $tmpimg -fill $cstring1 -stroke $cstring2 $font -pointsize $p5 -draw 'text $p1 $p2 \"$p0\"' $out`;
        
        $nwidth=$p8; $nheight=$p9;

###################################################################
        if (! -f $out) {
            print STDERR "Warning: effect plugin gen_text skipped frame $frame !";
            `cp $in $out`;
        }
	&sig_progress($frame,$nwidth,$nheight,$fps,$end);
        if ($p3_red>255) {
            $p3_red=255;
        }
        elsif ($p3_red<0) {
            $p3_red=0;
        }
        if ($p3_green>255) {
            $p3_green=255;
        }
        elsif ($p3_green<0) {
            $p3_green=0;
        }
        if ($p3_blue>255) {
            $p3_blue=255;
        }
        elsif ($p3_blue<0) {
            $p3_blue=0;
        }
        if ($p4_red>255) {
            $p4_red=255;
        }
        elsif ($p4_red<0) {
            $p4_red=0;
        }
        if ($p4_green>255) {
            $p4_green=255;
        }
        elsif ($p4_green<0) {
            $p4_green=0;
        }
        if ($p4_blue>255) {
            $p4_blue=255;
        }
        elsif ($p4_blue<0) {
            $p4_blue=0;
        }
        if ($p6_red>255) {
            $p6_red=255;
        }
        elsif ($p6_red<0) {
            $p6_red=0;
        }
        if ($p6_green>255) {
            $p6_green=255;
        }
        elsif ($p6_green<0) {
            $p6_green=0;
        }
        if ($p6_blue>255) {
            $p6_blue=255;
        }
        elsif ($p6_blue<0) {
            $p6_blue=0;
        }
    }
    return 1;
}



########## Post loop code ############
if ($command eq "clear") {
    unlink $tmping;
}

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

if ($command eq "onchange_8") {
    $p0=@ARGV[1];
    $p1=@ARGV[2];
    $p1_min=@ARGV[3];
    $p1_max=@ARGV[4];
    $p2=@ARGV[5];
    $p2_min=@ARGV[6];
    $p2_max=@ARGV[7];
    $p3_red=@ARGV[8];
    $p3_green=@ARGV[9];
    $p3_blue=@ARGV[10];
    $p4_red=@ARGV[11];
    $p4_green=@ARGV[12];
    $p4_blue=@ARGV[13];
    $p5=@ARGV[14];
    $p5_min=@ARGV[15];
    $p5_max=@ARGV[16];
    $p6_red=@ARGV[17];
    $p6_green=@ARGV[18];
    $p6_blue=@ARGV[19];
    $p7=@ARGV[20];
    $p7_min=@ARGV[21];
    $p7_max=@ARGV[22];
    $p8=@ARGV[23];
    $p8_min=@ARGV[24];
    $p8_max=@ARGV[25];
    $p9=@ARGV[26];
    $p9_min=@ARGV[27];
    $p9_max=@ARGV[28];
    $p10=@ARGV[29];
    $width=@ARGV[30];
    $height=@ARGV[31];
    $start=@ARGV[32];
    $end=@ARGV[33];
    $last=@ARGV[34];
    $length=$end-$start+1;

    $p1_max=$p8;
    $p0=~ s/\"/\\\"/g;

    print "\"$p0\" $p1 $p1_min $p1_max $p2 $p2_min $p2_max $p3_red $p3_green $p3_blue $p4_red $p4_green $p4_blue $p5 $p5_min $p5_max $p6_red $p6_green $p6_blue $p7 $p7_min $p7_max $p8 $p8_min $p8_max $p9 $p9_min $p9_max $p10 ";
    exit 0;
}

if ($command eq "onchange_3") {
    $p0=@ARGV[1];
    $p1=@ARGV[2];
    $p1_min=@ARGV[3];
    $p1_max=@ARGV[4];
    $p2=@ARGV[5];
    $p2_min=@ARGV[6];
    $p2_max=@ARGV[7];
    $p3_red=@ARGV[8];
    $p3_green=@ARGV[9];
    $p3_blue=@ARGV[10];
    $p4_red=@ARGV[11];
    $p4_green=@ARGV[12];
    $p4_blue=@ARGV[13];
    $p5=@ARGV[14];
    $p5_min=@ARGV[15];
    $p5_max=@ARGV[16];
    $p6_red=@ARGV[17];
    $p6_green=@ARGV[18];
    $p6_blue=@ARGV[19];
    $p7=@ARGV[20];
    $p7_min=@ARGV[21];
    $p7_max=@ARGV[22];
    $p8=@ARGV[23];
    $p8_min=@ARGV[24];
    $p8_max=@ARGV[25];
    $p9=@ARGV[26];
    $p9_min=@ARGV[27];
    $p9_max=@ARGV[28];
    $p10=@ARGV[29];
    $width=@ARGV[30];
    $height=@ARGV[31];
    $start=@ARGV[32];
    $end=@ARGV[33];
    $last=@ARGV[34];
    $length=$end-$start+1;

    $p4_red=$p3_red;$p4_green=$p3_green;$p4_blue=$p3_blue;
    $p0=~ s/\"/\\\"/g;

    print "\"$p0\" $p1 $p1_min $p1_max $p2 $p2_min $p2_max $p3_red $p3_green $p3_blue $p4_red $p4_green $p4_blue $p5 $p5_min $p5_max $p6_red $p6_green $p6_blue $p7 $p7_min $p7_max $p8 $p8_min $p8_max $p9 $p9_min $p9_max $p10 ";
    exit 0;
}

if ($command eq "onchange_9") {
    $p0=@ARGV[1];
    $p1=@ARGV[2];
    $p1_min=@ARGV[3];
    $p1_max=@ARGV[4];
    $p2=@ARGV[5];
    $p2_min=@ARGV[6];
    $p2_max=@ARGV[7];
    $p3_red=@ARGV[8];
    $p3_green=@ARGV[9];
    $p3_blue=@ARGV[10];
    $p4_red=@ARGV[11];
    $p4_green=@ARGV[12];
    $p4_blue=@ARGV[13];
    $p5=@ARGV[14];
    $p5_min=@ARGV[15];
    $p5_max=@ARGV[16];
    $p6_red=@ARGV[17];
    $p6_green=@ARGV[18];
    $p6_blue=@ARGV[19];
    $p7=@ARGV[20];
    $p7_min=@ARGV[21];
    $p7_max=@ARGV[22];
    $p8=@ARGV[23];
    $p8_min=@ARGV[24];
    $p8_max=@ARGV[25];
    $p9=@ARGV[26];
    $p9_min=@ARGV[27];
    $p9_max=@ARGV[28];
    $p10=@ARGV[29];
    $width=@ARGV[30];
    $height=@ARGV[31];
    $start=@ARGV[32];
    $end=@ARGV[33];
    $last=@ARGV[34];
    $length=$end-$start+1;

    $p2_max=$p9;
    $p0=~ s/\"/\\\"/g;

    print "\"$p0\" $p1 $p1_min $p1_max $p2 $p2_min $p2_max $p3_red $p3_green $p3_blue $p4_red $p4_green $p4_blue $p5 $p5_min $p5_max $p6_red $p6_green $p6_blue $p7 $p7_min $p7_max $p8 $p8_min $p8_max $p9 $p9_min $p9_max $p10 ";
    exit 0;
}

if ($command eq "onchange_init") {
    $p0=@ARGV[1];
    $p1=@ARGV[2];
    $p1_min=@ARGV[3];
    $p1_max=@ARGV[4];
    $p2=@ARGV[5];
    $p2_min=@ARGV[6];
    $p2_max=@ARGV[7];
    $p3_red=@ARGV[8];
    $p3_green=@ARGV[9];
    $p3_blue=@ARGV[10];
    $p4_red=@ARGV[11];
    $p4_green=@ARGV[12];
    $p4_blue=@ARGV[13];
    $p5=@ARGV[14];
    $p5_min=@ARGV[15];
    $p5_max=@ARGV[16];
    $p6_red=@ARGV[17];
    $p6_green=@ARGV[18];
    $p6_blue=@ARGV[19];
    $p7=@ARGV[20];
    $p7_min=@ARGV[21];
    $p7_max=@ARGV[22];
    $p8=@ARGV[23];
    $p8_min=@ARGV[24];
    $p8_max=@ARGV[25];
    $p9=@ARGV[26];
    $p9_min=@ARGV[27];
    $p9_max=@ARGV[28];
    $p10=@ARGV[29];
    $width=@ARGV[30];
    $height=@ARGV[31];
    $start=@ARGV[32];
    $end=@ARGV[33];
    $last=@ARGV[34];
    $length=$end-$start+1;

    $p1_max=$p8; $p2_max=$p9;
    if ($width>0) {$p8=$width}; if ($height>0) {$p9=$height};
    $p0=~ s/\"/\\\"/g;

    print "\"$p0\" $p1 $p1_min $p1_max $p2 $p2_min $p2_max $p3_red $p3_green $p3_blue $p4_red $p4_green $p4_blue $p5 $p5_min $p5_max $p6_red $p6_green $p6_blue $p7 $p7_min $p7_max $p8 $p8_min $p8_max $p9 $p9_min $p9_max $p10 ";
    exit 0;
}
