package GimpPimp;

# ; The GIMP -- an image manipulation program
# ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
# ; 
# ; 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.
# ; 
# ; You should have received a copy of the GNU General Public License
# ; along with this program; if not, write to the Free Software
# ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# ;
# ;
# ; drop-shadow.scm   version 1.02   12/13/97
# ;
# ; CHANGE-LOG:
# ; 1.00 - initial release
# ; 1.01 - fixed the problem with a remaining copy of the selection
# ; 1.02 - some code cleanup, no real changes
# ;
# ;
# ; Copyright (C) 1997 Sven Neumann (neumanns@uni-duesseldorf.de)
# ; 
# ;  
# ; Adds a drop-shadow of the current selection or alpha-channel. 
# ;
# ; This script is derived from my script add-shadow, which has become 
# ; obsolete now. Thanks to Andrew Donkin (ard@cs.waikato.ac.nz) for his 
# ; idea to add alpha-support to add-shadow.

use Gimp qw( :auto );
use Gimp::Fu;

sub drop_shadow {
	my ($image,$drawable,$shadow_transl_x,$shadow_transl_y,$blur,$color,$opacity,$resize) = @_;

	# Assume that $blur >= 0
	#             $opacity <= 100
	#             $opacity >= 0

	$type = gimp_drawable_type_with_alpha($drawable);
	$image_width = gimp_image_width($image);
	$image_height = gimp_image_height($image);
	$old_bg = gimp_palette_get_background();
	$from_selection = 0;
	$active_selection = 0;
	$shadow_layer = 0;

	gimp_image_undo_freeze($image);

	gimp_layer_add_alpha($drawable);
	if (gimp_selection_is_empty($image)) {

		gimp_selection_layer_alpha($drawable);
		$from_selection = 0;
	} else {
		$from_selection = 1;
		$active_selection = gimp_selection_save($image);
	}

	@selection_bounds = gimp_selection_bounds($image);

	$select_offset_x = $selection_bounds[1];
	$select_offset_y = $selection_bounds[2];
	$select_width    = $selection_bounds[3] - $selection_bounds[1];
	$select_height   = $selection_bounds[4] - $selection_bounds[2];

 	$shadow_width = $select_width + (2 * $blur);
	$shadow_height = $select_height + (2 * $blur);

	$shadow_offset_x = $select_offset_x - $blur;
	$shadow_offset_y = $select_offset_y - $blur;

	if ($resize) {
		$new_image_width = $image_width;
		$new_image_height = $image_height;
		$image_offset_x = 0;
		$image_offset_y = 0;
		if (($shadow_offset_x + $shadow_transl_x) < 0) {
			$image_offset_x = 0 - ($shadow_offset_x + $shadow_transl_x);
			$shadow_offset_x = 0 - $shadow_transl_x;
			$new_image_width -= $image_offset_x;
		}
		if (($shadow_offset_y + $shadow_transl_y) < 0) {
			$image_offset_y = 0 - ($shadow_offset_y + $shadow_transl_y);
			$shadow_offset_y = 0 - $shadow_transl_y;
			$new_image_height -= $image_offset_y;
		}
		if (($shadow_width + $shadow_offset_x + $shadow_transl_x) > $new_image_width) {
			$new_image_width = $shadow_width + $shadow_offset_x + $shadow_transl_x;
		}
		if (($shadow_height + $shadow_offset_y + $shadow_transl_y) > $new_image_height) {
			$new_image_height = $shadow_height + $shadow_offset_y + $shadow_transl_y;
		}
		gimp_image_resize($image,$new_image_width,$new_image_height,$image_offset_x,$image_offset_y);
	}
	$shadow_layer = gimp_layer_new($image,$shadow_width,$shadow_height,$type,"Drop-Shadow",$opacity,0);
	gimp_layer_set_offsets($shadow_layer,$shadow_offset_x,$shadow_offset_y);

	gimp_drawable_fill($shadow_layer,3);
	gimp_palette_set_background($color);
	gimp_edit_fill($shadow_layer,1);
	gimp_selection_none($image);
	gimp_layer_set_preserve_trans($shadow_layer,0);

	if ($blur > 0) {
		plug_in_gauss_rle($image,$shadow_layer,$blur,1,1);
#		plug_in_gauss_iir($image,$shadow_layer,$blur,1,1);
	}
	gimp_image_add_layer($image,$shadow_layer,-1);
	gimp_layer_translate($shadow_layer,$shadow_transl_x,$shadow_transl_y);
	if ($from_selection) {
		gimp_selection_load($active_selection);
		gimp_edit_clear($shadow_layer);
		gimp_image_remove_channel($image,$active_selection);
	}
	if ( (gimp_layer_is_floating_sel($drawable) == 0) and (!$from_selection) ) {
		gimp_image_raise_layer($image,$drawable);
	}

	gimp_image_set_active_layer($image,$drawable);
	gimp_palette_set_background($old_bg);
	gimp_image_undo_thaw($image);
	gimp_displays_flush();

}

# ;;; unsharp-mask.scm
# ;;; Time-stamp: <1997/10/30 23:27:20 narazaki@InetQ.or.jp>
# ;;; Author: Narazaki Shuji <narazaki@inetq.or.jp>
# ;;; Version 0.7

# ;;; Code:

sub unsharp_mask {
	($image,$drawable,$mask_size) = @_;

	$mask_size ||= 5;

	$drawable_width = gimp_drawable_width($drawable);
	$drawable_height = gimp_drawable_height($drawable);
	$new_image = gimp_image_new($drawable_width,$drawable_height,0);
	$original_layer = gimp_layer_new($new_image,$drawable_width,$drawable_height,0,"Original",100,0);
	$original_layer_for_darker = 15;
	$original_layer_for_lighter = 15;
	$blurred_layer_for_darker = 15;
	$blurred_layer_for_lighter = 15;
	$darker_layer = 15;
	$lighter_layer = 15;

	gimp_selection_all($image);
	gimp_edit_copy($drawable);
	gimp_image_undo_freeze($image);
	gimp_floating_sel_anchor(
		gimp_edit_paste($original_layer,0) );
	gimp_image_add_layer($new_image,$original_layer,0);

	$original_layer_for_darker = gimp_layer_copy($original_layer,1);
	$original_layer_for_lighter = gimp_layer_copy($original_layer,1);
	$blurred_layer_for_darker = gimp_layer_copy($original_layer,1);
	gimp_layer_set_visible($original_layer,0);

	# Make darker mask
	gimp_image_add_layer($new_image,$blurred_layer_for_darker,-1);
	plug_in_gauss_iir($new_image,$blurred_layer_for_darker,$mask_size,1,1);
	$blurred_layer_for_lighter = gimp_layer_copy($blurred_layer_for_darker,1);
	gimp_image_add_layer($new_image,$original_layer_for_darker,-1);
	gimp_layer_set_mode($original_layer_for_darker,SUBTRACT_MODE);
	$darker_layer = gimp_image_merge_visible_layers($new_image,CLIP_TO_IMAGE);
	gimp_layer_set_name($darker_layer,"darker mask");
	gimp_layer_set_visible($darker_layer,0);

	# Make lighter mask
	gimp_image_add_layer($new_image,$original_layer_for_lighter,-1);
	gimp_image_add_layer($new_image,$blurred_layer_for_lighter,-1);
	gimp_layer_set_mode($blurred_layer_for_lighter,SUBTRACT_MODE);
	$lighter_layer = gimp_image_merge_visible_layers($new_image,CLIP_TO_IMAGE);
	gimp_layer_set_name($lighter_layer,"lighter mask");

	# Combine them
	gimp_layer_set_visible($original_layer,1);
	gimp_layer_set_mode($darker_layer,SUBTRACT_MODE);
	gimp_layer_set_opacity($darker_layer,50.0);
	gimp_layer_set_visible($darker_layer,1);
	gimp_layer_set_mode($lighter_layer,ADDITION_MODE);
	gimp_layer_set_opacity($lighter_layer,50.0);
	gimp_layer_set_visible($lighter_layer,1);
	gimp_image_undo_thaw($new_image);

	gimp_displays_flush();

	return $new_image;
}


1;

