#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

#--------------------------------------------------------------------#
# ~/.gimp/plug-ins/drop_shadow_logo
#
# Generates logos by bump-mapping text strings from a solid color layer and
# adding a drop shadow.  Pretty much implements the procedure from the
# Gimp Tutorial page (see below), but without the plasma plugin.  I attempted
# to make the script as general as possible, providing as many options to
# the user as I thought reasonable, while hiding some of the gorier details.
#
# This is my first attempt at a Gimp Perl-Fu script, originally intended to
# generate consistent-looking logos for my website.  Based on a tutorial
# by Dov Grobgeld at:
#
#         http://imagic.weizmann.ac.il/~dov/gimp/perl-tut.html
#
# This script borrows heavily from the example scripts on that page, as
# well as the Gimp Tutorial from:
#
#         http://www.gimp.org/tut-basic.html
#
# Requires the Gimp Perl module, which in turn requires the following Perl
# modules (in this order, more or less):
#
#         ExtUtils-F77
#         PDL
#         Gtk-Perl
#
# All of which may be found at CPAN (Comprehensive Perl Archive Network):
#
#         http://www.cpan.org/
#
# This code is Copyright (c) 2000 Mike Bland.  All rights reserved.
# This code may be freely distributed provided that this copyright notice
# is preserved.
#
# Feel free to use this code and modify it to suit your own purpose, but
# note that it comes with no warranty and with the understanding that you
# make use of it at your own risk.  If this code is used commercially or
# distributed in a commercial product, I would appreciate knowing about it.
#
# Author:        Mike Bland <mbland@pcs.cnu.edu>
#                http://www.pcs.cnu.edu/~mbland/
# Creation Date: Saturday, May 20, 2000
#
# Modified:		  Michael J. Hammel <mjhammel@graphics-muse.org>
#                http://www.graphics-muse.org
# Modify Date:   Sunday, January 27, 2002
#
# Rewritten for GIMP 1.2 and the Graphics Muse Tools CD
# by Michael J. Hammel (mjhammel@graphics-muse.org
# Jan 26, 2002
#
# Revision:      $Revision: 1.1.1.1 $
# Checked in by: $Author: mjhammel $
# Last modified: $Date: 2002/02/01 14:38:59 $
#
# $Log: drop_shadow_logo.pl,v $
# Revision 1.1.1.1  2002/02/01 14:38:59  mjhammel
# Initial Checkin
#
# Revision 1.1  2000/05/23 14:56:25  mbland
# Initial revision
#
#--------------------------------------------------------------------#

use strict;
use Gimp;
use Gimp::Fu;

sub drop_shadow_logo
{
        my ($text, $border, $font, $text_color, $low_input, $high_input,
                $gamma_input, $drop_color, $x_offset, $y_offset, $opacity,
                $background_color, $flatten, $indexed, $crop) = @_;

        # Create new image and base_text layer
        my $img       = new Image(100, 100, RGB);
        my $base_text = new Layer($img, $img->width, $img->height, RGB,
                "Base Text", 100, NORMAL_MODE);
        $base_text->add_layer(3);
        Gimp->palette_set_background([0, 0, 0]);
        $base_text->edit_fill(BG_IMAGE_FILL);

        # Anchor text string to base_text layer.  Resize img and base_text to
        # fit text_layer before we do, though.
        Gimp->palette_set_foreground([255, 255, 255]);
        # my $text_layer = gimp_text_fontname($base_text, 0, 0, $text,
        #         $border, 1, xlfd_size($font), 0, $font);
        my $text_layer = 
				$base_text->text_fontname(0, 0, $text, 
					$border, 1, xlfd_size($font), $font);
        $img->resize($text_layer->width, $text_layer->height, 0, 0);
        $base_text->resize($text_layer->width, $text_layer->height,
                0, 0);
        $text_layer->floating_sel_anchor();

	# Create background layer
	my $background = new Layer($img, $img->width, $img->height, RGB,
	        "Background", 100, NORMAL_MODE);
	$background->add_layer(2);
	Gimp->palette_set_background($background_color);
	$background->edit_fill(BG_IMAGE_FILL);

        # Use a Gaussian Blur (IIR) with default setting on base_text
        Gimp->plug_in_gauss_iir(0, $base_text, 5.0, 1, 1);

        # Create a new layer for the bump map
        my $bump_layer = new Layer($img, $img->width, $img->height, RGB,
                "Bump Layer", 100, NORMAL_MODE);
        $bump_layer->add_layer(0);
        Gimp->palette_set_background($text_color);
        $bump_layer->edit_fill(BG_IMAGE_FILL);
        Gimp->plug_in_bump_map(0, $bump_layer, $base_text, 135.00, 45.00, 3,
               0, 0, 0, 0, 0, 0, 0);

        # Copy base_text to use as a layer mask on bump_layer
        $bump_layer->add_alpha();
        $bump_layer->add_layer_mask($bump_layer->create_mask(0));
        Gimp->edit_copy($base_text);
        my $mask = Gimp->edit_paste($bump_layer->mask(), 0);
        $mask->floating_sel_anchor();

        # Set the levels for bump_layer->mask
        $bump_layer->mask()->levels(0, $low_input, $high_input, $gamma_input, 0, 255);

        # Create the drop_shadow layer 
        my $drop_shadow = $bump_layer->copy(0);
        $drop_shadow->set_name("Drop Shadow");
        $drop_shadow->add_layer(1);
        $drop_shadow->set_preserve_trans(1);
        Gimp->palette_set_background($drop_color);
        $drop_shadow->edit_fill(BG_IMAGE_FILL);
        $drop_shadow->set_preserve_trans(0);
        Gimp->plug_in_gauss_iir(0, $drop_shadow->mask(), 5.0, 1, 1);
        $drop_shadow->set_opacity($opacity);
        $drop_shadow->set_offsets($x_offset, $y_offset);

        # Get rid of the base_text layer
        $base_text->set_visible(0);

        # Flatten image, translate it to indexed, and autocrop it.  Each
        # successive operation depends on the previous.
	if ($flatten == 1) {
		my $flat_layer = $img->flatten();
		if ($indexed == 1) {
			$img->convert_indexed(1, MAKE_PALETTE, 255,0,0,0);
			if ($crop == 1) { $flat_layer->autocrop(); }
		}
	}
        return $img;
}

# Registers the drop_shadow_logo function with The Gimp.  The parameters
# take the following form:
#         1.  Name of function to register
#         2.  Description of function
#         3.  Help text
#         4.  Author
#         5.  Copyright
#         6.  Date
#         7.  Menu path
#         8.  Image types
#         9.  Parameters
#             A.  Type
#             B.  Name
#             C.  Help text
#             D.  Default value
#         10. Return types
#         11. Reference to function

register (
        "drop_shadow_logo",
        "Used to generate bump-mapped text logos with a drop shadow.",
        "Creates bump-mapped text with a drop shadow.",
        "Mike Bland <mbland\@pcs.cnu.edu>\nhttp://www.pcs.cnu.edu/~mbland/",
        "Copyright (c) 2000 Mike Bland.  All rights reserved.",
        "Saturday, May 20, 2000",
        "<Toolbox>/Xtns/Perl-Fu/Drop Shadow Logo",
        "RGB",
        [
                [PF_STRING, "text", "Text to create logo from", "The New Electric Church"],
               [PF_INT, "border", "Amount of space to leave around edges of text", "25"],
               [PF_FONT, "font", "Font for logo text",
                       "-*-helvetica-*-*-*-*-50-*-*-*-*-*-*-*"],
               [PF_COLOR, "text_color", "Color of logo text",
                       [103, 18, 109]],
               [PF_INT, "low_input", "Low input level for logo text layer",
                       "27"],
	        [PF_INT, "high_input", "High input level for logo text layer",
                       "63"],
               [PF_FLOAT, "gamma_input", "Gamma input level for logo text layer", "0.88"],
               [PF_COLOR, "drop_color", "Color of drop shadow",
                       [244, 223, 63]],
               [PF_INT, "x_offset", "Offset of drop shadow in x-direction",
                       "2"],
               [PF_INT, "y_offset", "Offset of drop shadow in y-direction",
                       "2"],
               [PF_FLOAT, "opacity", "Opacity of drop shadow", "50.0"],
               [PF_COLOR, "background_color", "Color of background",
                       [0, 0, 0]],
               [PF_TOGGLE, "flatten", "Merge all layers together", 1],
               [PF_TOGGLE, "indexed", "Translate to indexed palette", 1],
               [PF_TOGGLE, "crop", "Automatically crop to fit text", 1]
        ],
        # [PF_IMAGE],
        \&drop_shadow_logo);

# Let the Gimp take back over
exit main();

# End of ~/.gimp/plug-ins/drop_shadow_logo
