#!/usr/bin/perl -w

use Gimp;
# use Gimp::Fu;

Gimp::on_net {
# A script to test each script in an automated way, to make checking versions easier.

# properites that matter for running these things:
#  layers required      (0 = makes new image)
#  requires alpha        REQ_ALPHA = 0x1
#  requires selection    REQ_SEL   = 0x2 
#  requires guide        REQ_GUIDE = 0x4
#  parameters            array of params

$REQ_NONE  = 0x0;
$REQ_ALPHA = 0x1;
$REQ_SEL   = 0x2;
$REQ_GUIDE = 0x4;
$REQ_DIR   = 0x8;

$color1 = '[0,0,1.0]';
$color2 = '[0.4,0,1.0]';
$black = '[0,0,0]';
$white  = '[1,1,1]';
$gradient1 = '"Burning Paper"';
$width     = 200;
$height    = 300;

$testimage = "/home/seth/cvs/gimp-perl/examples/testimg1.JPG";

@testbench = (
["add_glow"               , 2, $REQ_ALPHA, [$color1, 5] ],
["animate_cells"          , 3, $REQ_ALPHA, [0] ],
["auto_red_eye"           , 1, $REQ_NONE , [] ],
["blowinout"              , 1, $REQ_NONE , [ 30, 8, "30", 0, 0] ],
["blur_2x2"               , 1, $REQ_NONE , [] ],
["brushed_metal"          , 1, $REQ_NONE , [40,120,1,$gradient1] ],
["burst"                  , 1, $REQ_NONE , [0,0,14,30,50,80,140] ],
["center_guide"           , 1, $REQ_NONE , [0] ],
["center_layer"           , 2, $REQ_ALPHA, [] ],
["contrast_enhance_2x2"   , 1, $REQ_NONE , [] ],
["do_bricks"              , 0, $REQ_NONE , ['"Leather"','"unused yet"','""','[0.5,0.5,0.5]',1,8,16,256,256,0] ],
["dots"                   , 1, $REQ_NONE , [8,$color1,80,20,16,0,0] ],
["dust"                   , 1, $REQ_NONE , [0.0005,0,50] ],
["edge_detect_2x2"        , 1, $REQ_NONE , [] ],
["glowing_steel"          , 0, $REQ_NONE , ['"GET LOST"','"Bitstream Charter Bold 72"',100,$color1,$black,4,0,0] ],
["golden_mean"            , 0, $REQ_NONE , [233, 0] ],
["guide_grid"             , 1, $REQ_NONE , [24,14,0,0,0] ],
["guide_remove"           , 1, $REQ_GUIDE, [] ],
["guide_to_selection"     , 1, $REQ_GUIDE, [CHANNEL_OP_REPLACE,0,0] ],
["highlight_edges"        , 1, $REQ_ALPHA, [ 10] ],
["inner_bevel"            , 0, $REQ_NONE , ['"URW Bookman L, Bold 80"','"INNERBEVEL"',$color1,$color2,132,30,7,2] ],
["layer_apply"            , 1, $REQ_NONE , [q|'$d->gauss_rle($P*100+1,1,1)'|,'""'] ],
["layer_reorder"          , 3, $REQ_ALPHA, [1,'""'] ],
["make_bevel_logos"       , 1, $REQ_ALPHA, [$white,$color1,$color2,45,4,0] ],
["make_trans_logos"       , 1, $REQ_ALPHA, [0,$gradient1,$color1] ],
["map_to_gradient"        , 1, $REQ_NONE , [$gradient1] ],
["mirror_split"           , 1, $REQ_NONE , [0] ],
["perlotine"              , 1, $REQ_GUIDE|$REQ_DIR, ['"DIR"','"foo.html"','"t"','"png"',0,'""',1,0] ],
["pixelgen"               , 0, $REQ_NONE , [$width,$height,RGB_IMAGE,q|'($x*$y*0.01)->slice("*$bpp")'|] ],
["pixelmap"               , 1, $REQ_NONE , [q|'($x*$y*0.01)->slice("*$bpp")'|] ], 
["prep4gif"               , 2, $REQ_ALPHA, [64,1,0,1,255] ],
["random_art_1"           , 0, $REQ_NONE , [$width,$height,20,10,1,30,0] ],
["random_blends"          , 1, $REQ_NONE , [7] ],
["red_eye"                , 1, $REQ_NONE , [0] ],
["repdup"                 , 1, $REQ_SEL  , [3,50,50] ],
["round_sel"              , 1, $REQ_SEL  , [16] ],
["scratches"              , 1, $REQ_NONE , [30,70,0.3,15,10] ],
["selective_sharpen"      , 1, $REQ_NONE , [5.0,1.0,20] ],
["seth_spin"              , 2, $REQ_NONE , ["DRW",16,$color1,40,1,1] ],
["stamps"                 , 0, $REQ_NONE , [90,$white,$color1,10,5] ],
# ["tex_string_to_float"    , 1, $REQ_NONE , ["\"\"",'"I can write \\\\TeX"',72,6,4] ],
# ["view3d"                 , 1, $REQ_NONE , [0,1,1] ],
["webify"                 , 1, $REQ_NONE , [1,1,$white,3,32,1] ],
["windify"                , 1, $REQ_NONE , [120,80,30,1] ],
["xach_blocks"            , 1, $REQ_NONE , [10,40] ],
["xach_shadows"           , 1, $REQ_NONE , [10] ],
["xachvision"             , 1, $REQ_NONE , [$color1,25] ],
["yinyang"                , 0, $REQ_NONE , [$width,$height,1,0,q|""|,q|""|,1] ],
); 

foreach $test (@testbench)
{
  $testname = "Gimp::perl_fu_" . $test->[0];
  $numlays  = $test->[1];
  if ($numlays == 0)
    {
     $cmd = $testname . "(";
     $isfirstelem = 1;
     foreach $param (@{$test->[3]})
       {
	 if ($isfirstelem) 
	   { # for some reason I'm having to be explicit here...
	      $isfirstelem = 0;
	      $cmd = $cmd . "RUN_NONINTERACTIVE, ";
	   }
	 else 
	   { # insert a comma between list items
	     $cmd = $cmd . ", ";
	   }
	 $cmd = $cmd . " " . $param;
       }  
     $cmd = $cmd . ")\n";
     print $cmd;
     $img = eval $cmd;
     # FIXME: returning an image is broken!  
     warn $@ if $@;
    }
  elsif ($numlays == 1)
   {
     $img = Gimp::file_load(RUN_NONINTERACTIVE, $testimage, $testimage);
     $drw = $img->get_active_drawable;
     Gimp::display_new($img);
    
     if ($test->[2] & $REQ_ALPHA)
       {
          $drw->add_alpha;
          Gimp::gimp_rect_select($img, 0.1*$height,0.1*$width,0.8*$height,0.8*$width,CHANNEL_OP_REPLACE,0,0);
          Gimp::gimp_selection_invert($img);
          Gimp::gimp_edit_cut($drw);
          Gimp::gimp_selection_none($img);
       } 
     if ($test->[2] & $REQ_SEL) 
       {
          Gimp::gimp_rect_select($img, 0.2*$height,0.2*$width,0.6*$height,0.6*$width,CHANNEL_OP_REPLACE,0,0);
       }
     if ($test->[2] & $REQ_GUIDE)
       {
          $img->add_hguide($width * 0.3);
          $img->add_hguide($width * 0.6);
          $img->add_hguide($width * 0.9);
          $img->add_vguide($height * 0.3);
          $img->add_vguide($height * 0.6);
          $img->add_vguide($height * 0.9);
       }

     $cmd = $testname . "(";
     $isfirstelem = 1;
     if (scalar @{$test->[3]} == 0)
       { $cmd = $cmd . "RUN_NONINTERACTIVE, \$img, \$drw"; }
     foreach $param (@{$test->[3]})
       {
	 if ($isfirstelem) 
	   { 
	      $isfirstelem = 0;
	      $cmd = $cmd . "RUN_NONINTERACTIVE, \$img, \$drw, ";
	   }
	 else 
	   { # insert a comma between list items
	     $cmd = $cmd . ", ";
	   }
	 $cmd = $cmd . " " . $param;
       }  
     $cmd = $cmd . ")\n";
     print $cmd;
     eval $cmd;
     warn $@ if $@;
     $img->set_filename($cmd);
   }
  elsif ($numlays == 2)
   {
   }
  elsif ($numlays == 3)
   {
   }
}
};

exit main;

