#!/usr/bin/perl

use Gimp qw(:auto __ N_);
use Gimp::Fu;
use Gimp::Util;

# Gimp::set_trace(TRACE_ALL);

sub my_innerbevel {
	my ($image, $drawable, $bg_col, $bl1, $bl2, $elev, $depth, $rad_tog) = @_;
	# if you specify RGBA as your type, this die is only there for 
	# running it non-interactively
	$drawable->has_alpha or die "You can't run this script without an ALPHA CHANNEL!!";

        # get release of gimp, so we can be compatible between versions
        my $gimpversion = Gimp->version;
        $gimpversion =~ m/^(\d)\.(\d)\.\d/;
        my $gimpmajor = $1;
        my $gimpminor = $2;
        my $api22 = 0; my $api23 = 0;
  
        if ($gimpmajor == 2 && $gimpminor == 2) { $api22 = 1; }
        elsif ($gimpmajor == 2 && $gimpminor >= 3) { $api23 = 1; }
        else {die "Don't know how to handle gimp version $gimpversion with this plugin!\n"};

        Context->push();

	my $img = gimp_image_new (256, 100, RGB_IMAGE);
	$drawable->get_image->selection_all;
	$drawable->edit_copy;
	gimp_selection_none ($image);
	if ($rad_tog == 0) {
		my $bg_layer;
		$bg_layer=$img->layer_new($drawable->width,$drawable->height,$image->layertype(1), "Background", 100, NORMAL_MODE);
		Context->set_background ($bg_col);
		$bg_layer->drawable_fill(BACKGROUND_FILL);
		$img->add_layer($bg_layer,0);
	}
        $img->resize($drawable->width,$drawable->height, 0, 0);
	my $text1_lay;
	$text1_lay=$img->layer_new($drawable->width,$drawable->height,$image->layertype(1), "Text -1-", 100, NORMAL_MODE);
	$img->add_layer($text1_lay,-1);
	gimp_edit_clear ($text1_lay);
	$text1_lay->edit_paste(0)->floating_sel_anchor;
        if ($api22) {
	  $text1_lay->set_preserve_trans(1);
        }
        elsif ($api23) {
	  $text1_lay->set_lock_alpha(1);
        }
	Context->set_foreground ($bl1);
	Context->set_background ($bl2);
	@start = ($text1_lay->width / 2 - 5, 0);
	@end = ($text1_lay->width / 2 + 5, $text1_lay->height);
	gimp_edit_blend ($text1_lay, 
	                 FG_BG_RGB_MODE, 
			 NORMAL_MODE, 
			 GRADIENT_LINEAR, 
			 100, 
			 FALSE, 
			 REPEAT_NONE, 
			 FALSE, # reverse
			 FALSE, # supersampling
			 2, # recursion levels
			 0.30, # threshold
			 FALSE, # dithering
			 @start, @end);
	my $text2_lay;
	$text2_lay=$text1_lay->copy(1);
	$text2_lay->add_alpha;
	$img->add_layer($text2_lay, -1);
	Context->set_background ([255, 255, 255]);
	$text2_lay->edit_fill(BACKGROUND_FILL);
        if ($api22) {
	  $text1_lay->set_preserve_trans(0);
	  $text2_lay->set_preserve_trans(0);
        }
        elsif ($api23) {
	  $text1_lay->set_lock_alpha(0);
	  $text2_lay->set_lock_alpha(0);
        }
	plug_in_gauss_rle ($text2_lay, 6, 1, 1);
	plug_in_bump_map ($img, $text1_lay, $text2_lay, 110.0, $elev, $depth, 0, 0, 0, 0, 0, 0, 0);
	$text2_lay->invert;
	$img->lower_layer($text2_lay);
	$text2_lay->translate(2, 3);
	$text2_lay->set_opacity(75);
	if ($rad_tog == 1) {
		$img->flatten;
		gimp_image_convert_indexed ($img, 0, MAKE_PALETTE, 256, 0, 0, "");
        	my $new = gimp_image_active_drawable ($img);
        	gimp_layer_add_alpha ($new);
        	gimp_by_color_select ($new, [255, 255, 255], 55, CHANNEL_OP_ADD, 0, 0, 0.0, 0);
        	gimp_edit_clear ($new);
        	gimp_selection_none ($img);
        } else {
		$img->flatten;
	}
#	gimp_display_new ($img);
	$img;
}
$help=<<EOF.$help;
This script will produce a nice blended beveled logo from your alpha
layer, which must have a black text. You can choose the initial and finals colours
of the blend, the background, and how to tweak the bevel effect.
It uses a techinque quite similar to that in the Inner Bevel Logo.

EOF

register "make_bevel_logos",
         "A script to get blended beveled logos",
         $help,
         "Michele Gherlone <mikem\@enet.it>",
         "(c) 2000 M. Gherlone",
         "20000130",
         N_"<Image>/Filters/Render/Blended Logo...",
         "RGBA",
         [
           [PF_COLOUR	, 'choose_bg_colour', "Choose the background colour", [255, 255, 255]],
           [PF_COLOUR	, 'blend_start',  "Choose the 1st blend colour", [247, 231, 9]],
           [PF_COLOUR	, 'blend_stop',   "Choose the 2nd blend colour", [255, 0, 0]],
           [PF_SLIDER	, 'strength_of_bevel', "Strength of bevel", 45.00, [0.00, 60.00, 0.50]],
           [PF_SLIDER	, 'depth_of_bevel', "Depth of bevel", 4, [0, 60, 1]],
           [PF_RADIO	, 'user_choice', "The user's choice", 0, [Background => 0, Transparent => 1]],
         ],
         \&my_innerbevel;
exit main;

=head1 LICENSE

Copyright Michele Gherlone.

Unknown license status.

=cut

