#! /usr/bin/perl -w # # dropshadow - make a simple drop shadow image. # # Copyright (C) 2000 Satoru Takabayashi # All rights reserved. # This is free software with ABSOLUTELY NO WARRANTY. # # You can redistribute it and/or modify it under the terms of # the GNU General Public License version 2. # # USAGE: # # * Making a simple drop shadow image. # % dropshadow foo.png > bar.png # # * Making a simple drop shadow image with a border line. # % dropshadow -b foo.png > bar.png # # * Making a simple drop shadow image with a frame. # % dropshadow -f foo.png > bar.png # # * Changing the colors of the border and the shadow. # % dropshadow --border-color=red --shadow-color=black foo.png > bar.png # use strict; use Image::Magick; use Getopt::Long; my $shadow_width = 10; my $with_border = undef; my $with_frame = undef; my $border_margin = 0; my $border_color = "black"; my $shadow_color = "gray75"; main(); sub main { parse_options(); my $image = read_image(); draw($image); write_image($image); } sub parse_options { Getopt::Long::Configure('bundling'); GetOptions('w|width=i' => \$shadow_width, 'b|border' => \$with_border, 'f|frame' => \$with_frame, 'c|shadow-color=s' => \$shadow_color, 'C|border-color=s' => \$border_color); } sub read_image { my $image = new Image::Magick; if (@ARGV >= 1) { $image->ReadImage($ARGV[0]); } else { $image->ReadImage('-'); } return $image; } sub draw { my ($image) = @_; draw_frame($image) if $with_frame; draw_border($image) if $with_border || $with_frame; draw_shadow($image); process_transparent_color($image); } sub draw_frame { my ($image) = @_; $image->Set(bordercolor=>'gray'); $image->Border("1x1"); $image->Set(bordercolor=>'white'); $image->Border($shadow_width / 2); } sub draw_border { my ($image) = @_; $image->Set(bordercolor=> $border_color); $image->Border("1x1"); } sub draw_shadow { my ($image) = @_; my ($width, $height, $magick) = $image->Get('width', 'height', 'magick'); if ($magick eq "GIF" || $magick eq "PNG") { # Assume #facade is not used in an image. $image->Set(bordercolor=>'#facade'); } else { $image->Set(bordercolor=>'white'); } my $width1= $width+1; my $height1= $height+1; $image->Border("${shadow_width}x${shadow_width}"); $image->Crop(x => $shadow_width, y => $shadow_width); my $width2 = $width + $shadow_width; my $height2 = $height + $shadow_width; $image->Draw(pen=>$shadow_color, primitive=>'rectangle', points=>"$width,$shadow_width $width2,$height2"); $image->Draw(pen=>$shadow_color, primitive=>'rectangle', points=>"$shadow_width,$height $width2,$height2"); } sub process_transparent_color { my ($image) = @_; my ($magick) = $image->Get('magick'); if ($magick eq "GIF" || $magick eq "PNG") { $image->Transparent(color=>'#facade'); } } sub write_image { my ($image) = @_; my ($magick) = $image->Get('magick'); binmode STDOUT; $image->Write( "$magick:-" ); } sub min { my ($foo, $bar) = @_; if ($foo < $bar) { return $foo; } else { return $bar; } }