#!/usr/bin/perl -w

# dropshadow.pl
#
# - opens an image file
# - adds a drop shadow, identical to the GIMP Script-Fu function
# - saves the file back again.
#
# This script is largely based on a Perl script written
# by Erich Lerch (http://lerchs.org/gimp_en.html), 2002-08
#
# Arguments: Name of the file to convert.
#
# TODO: - output file naming
#	- check for args & clean bailout if required
#
# -----------------------------------------------------------------
# History:
#   2002-08-xx, published by Erich Lerch
#		at http://lerchs.org/gimp_en.html
#   2002-08-24, JHa (added offset in final image)
#   2005-02-14, JHa (fixed bug that worked in SuSE 7.3 but
#	             appeared with SuSE 9.0)
#   2005-03-08, JHa (fixed "pseudo-bug" related to UTF-8)
# -----------------------------------------------------------------
# Copyright (c) 2002, Erich Lerch & 2002...2005 Joerg Hau
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# -----------------------------------------------------------------

use Gimp qw( :auto );

# get filename from the command line
$fn = $ARGV[0];

# define some stuff for drop shadow
$xoff            = 8;
$yoff            = 8;
$blur            = 15;
$shadowcolor     = [0,0,0];
$backgroundcolor = [255,255,255];
$opac            = 80;

Gimp::init;

# messages to console, not msg box
gimp_message_set_handler(1);

# load the images
$img = gimp_file_load($fn,$fn);

# get image size
$x = gimp_image_width($img);
$y = gimp_image_height($img);

# drop shadow:
# - make new image (bigger than original)
# - copy picture to new image
# - fill selection with color
# - apply offset in x and y direction
# - apply blur effect
# - copy/paste image again
# - flatten image

$newx = $x + $xoff + $blur - 1;
$newy = $y + $yoff + $blur - 1;

$newimg = gimp_image_new($newx,$newy,0);

# insert a new layer and fill it with the bkgd color
$newlayer = gimp_layer_new($newimg,$newx,$newy,1,"new",100,0);
$layer  = gimp_image_get_active_layer($img);
gimp_selection_all($newimg);
gimp_palette_set_background($backgroundcolor);
gimp_bucket_fill($newlayer,1,0,100,0,0,0,0);

# Originally just a translation to create some offset in the final img,
# but an interesting feature ;-): The image with its dropshadow will
# itself appear inside a dropshadowed frame.
# Interesting if you need to use images with a b/w dropshadow
# on a differently colored background ...
#gimp_layer_resize($newlayer,$newx+$xoff,$newy+$yoff,$xoff,$yoff);

# copy this layer, paste it as floating selection ...
gimp_selection_none($newimg);
gimp_edit_copy($layer);
$floating = gimp_edit_paste($newlayer,1);

# ... now fill layer with the shadow color,
# move it into place and anchor it. Then, apply blur:
gimp_palette_set_background($shadowcolor);
gimp_layer_set_opacity($floating,$opac);
gimp_edit_fill($floating,1);
gimp_layer_set_offsets($floating,$xoff,$yoff);
gimp_floating_sel_anchor($floating);
plug_in_gauss_rle(1,$newlayer,$blur,$blur,$blur);

$floating = gimp_edit_paste($newlayer,1);
gimp_layer_set_offsets($floating,0,0);
gimp_floating_sel_anchor($floating);

# and finally save the image as "output.jpg"
file_jpeg_save(1,$newlayer,"output.jpg","output.jpg",0.7,0,0,0,"(C) Joerg Hau",0,0,0,0);

Gimp::end;
