#! /usr/bin/perl

# This script adds a dropshadow to the input image
# Copyright  2002-2005 Brendt Wohlberg (os@wohlberg.net)
# Released under the terms of the GPL
# Most recent modification: 22 September 2005

use strict;
use Getopt::Long;

my $usagetext = <<EOF;
usage: dropshadow [ --help ] [ --scolour shadow-colour ]
       [ --bcolour border-colour ] [ --blur blur-parameter ]
       [ --border border-size ] [ --gap gap-size ]
       [ --shoulder shoulder-size ] [ --modify ] input-image
       [ output-image ]
EOF

my $scolour = "grey50";
my $bcolour = "white";
my $blur = "5x3";
my $border = 12;
my $gap = 6;
my $help = 0;
my $modify = 0;
my $shoulder = 6;

# Parse command line options
GetOptions("scolour=s" => \$scolour,
           "bcolour=s" => \$bcolour,
	   "blur=s" => \$blur,
	   "border=i" => \$border,
           "gap=i" => \$gap,
	   "help!" => \$help,
	   "modify!" => \$modify,
	   "shoulder=i" => \$shoulder) or
  die $usagetext;

if ($help) {
  print $usagetext;
  exit(0);
}

my $srcimage = $ARGV[0];
die $usagetext if ($srcimage eq "");
die "Cannot read input image " . $srcimage . "\n" if (! -r $srcimage);

my $dstimage = $ARGV[1];
die $usagetext if ($dstimage eq "" && !$modify);

my $xextend = $border;
my $yextend = $border;
my $toprgtgap = $shoulder;
my $btmlftgap = $shoulder;
my $rgtsidegap = $gap;
my $btmsidegap = $gap;
my $blurparam = $blur;

my $srcident = `identify "$srcimage"`;
$srcident =~ /^$srcimage\s+\w+\s+(\d+)x(\d+)/;
my $srcxsize = $1;
my $srcysize = $2;

my $cmdstr;
if ($modify) {
  $cmdstr = "mogrify ";
  $dstimage = $srcimage;
} else {
  $cmdstr = "convert ";
}
$cmdstr .= "-bordercolor $bcolour -border ${xextend}x${yextend} " .
           "-chop ${xextend}x${yextend} -page ${xextend}x${yextend}+0+0 " .
           "\"$srcimage\"";

$cmdstr .= " \"$dstimage\"" if (!$modify);
system($cmdstr);

$cmdstr = "mogrify -fill $scolour " .
          "-draw 'rectangle ${srcxsize},${toprgtgap}," .
          ($srcxsize+$xextend-$rgtsidegap) . "," .
          ($srcysize+$yextend-$btmsidegap) . "' " .
          "-draw 'rectangle ${btmlftgap},${srcysize}," .
          ($srcxsize+$xextend-$rgtsidegap) . "," .
          ($srcysize+$yextend-$btmsidegap) . "' \"$dstimage\"";
system($cmdstr);

$cmdstr = "mogrify " .
          "-region ${xextend}x" . ($srcysize+$yextend) . "+${srcxsize} " .
          "-blur $blurparam \"$dstimage\"";
system($cmdstr);

$cmdstr = "mogrify -region ${srcxsize}x${yextend}+0+${srcysize} " .
          "-blur $blurparam \"$dstimage\"";
system($cmdstr);

exit(0);


__END__

=pod

=head1 NAME

dropshadow - Add a "dropshadow" to an image

=head1 SYNOPSIS

B<dropshadow> [ B<--help> ] [ B<--scolour> shadow-colour ] [ B<--bcolour> border-colour ] [ B<--blur> blur-parameter ] [ B<--border> border-size ] [ B<--gap> gap-size ] [ B<--shoulder> shoulder-size ] [ B<--modify> ] I<input-image> [ I<output-image> ]

=head1 DESCRIPTION

B<dropshadow> is a Perl script which call the ImageMagick utilities
B<convert> and B<mogrify> to construct a "dropshadow" border for an
image.

=head1 OPTIONS

=over 4

=item B<--help>

Display usage information.

=item B<--scolour> shadow-colour

Specify the shadow colour. Defaults to "grey50".

=item B<--bcolour> border-colour

Specify the border colour. Defaults to "white".

=item B<--blur> blur-parameter

Specify the blur parameter for the blur operation used to construct
the shadow. Default is "5x3".

=item B<--border> border-size

Specify the border height/width. Default is 12.

=item B<--gap> gap-size

Specify the size of the gap between the shadow and the edge of the
image (i.e. the amount of the border that is not shadow). Default is 6.

=item B<--shoulder> shoulder-size

Specify the size of the shoulder on the shadow (the gap on the top
right and bottom left). Default is 6.

=item B<--modify>

Modify input image rather than writing to a second output image.

=back

=head1 REQUIREMENTS

Perl (version 5). See http://www.cpan.org/.

ImageMagick (version 6 recommended). See http://www.imagemagick.org/.

=head1 NOTE

A better approach would have been to use the Perlmagick interface to
ImageMagick rather than call its B<convert> and B<mogrify> utility
commands.

=head1 LICENSE

This software is made available under the terms of the GNU General
Public License.


=head1 AVAILABILITY

See L<http://www.wohlberg.net/public/software/photo/dropshadow/>


=head1 AUTHOR

Brendt Wohlberg <os@wohlberg.net>

=cut
