#!/usr/bin/perl -w
#
# Skript pro orez/skalovani fotky na format minilabu.
# Otestovano na Gimpu 2.0.5 ve Fedora Core 3.
# Zbastlil Jan "Yenya" Kasprzak (http://www.fi.muni.cz/~kas/)
# Je mozno sirit a pouzivat pri splneni podminek licence GNU GPL verze 2.
#

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

register "image_minilab_resize",
	"Resize an image for a minilab",
	"Resize an image for a minilab",
	'Jan "Yenya" Kasprzak',
	'(c) 2004 Jan Kasprzak',
	'2004-12-16',
	'<Image>/Fotky/Resize for Minilab...',
	'*',
	[
		[ PF_INT32,	"width", "Image width", 1818 ],
		[ PF_INT32,	"height", "Image height", 1228 ],
		[ PF_BOOL,	"subset", "Create a selection subset", 1 ],
	],
	[],
	['gimp-1.1'],
	\&image_minilab_resize;

exit main;

sub image_minilab_resize {
	my ($img, $drawable, $width, $height, $subset) = @_;

	gimp_image_undo_group_start($img);

	my ($issel, $left, $top, $right, $bottom) = gimp_selection_bounds($img);

	my $owidth = $right-$left;
	my $oheight = $bottom-$top;

	if ($issel) {
		gimp_image_resize($img, $owidth, $oheight, -$left, -$top);
		gimp_selection_none($img);
	} else {
		$subset = 1;
	}

	if ($owidth > $oheight && $width < $height
		|| $owidth < $oheight && $width > $height) {
		($width, $height) = ($height, $width);
	}
	
	my $wratio = $width/$owidth;
	my $hratio = $height/$oheight;

	if (!$subset && $hratio > $wratio || $subset && $hratio < $wratio) {
		gimp_image_scale($img, $width, $oheight*$wratio);
		gimp_image_resize($img, $width, $height,
			0, -($oheight*$wratio-$height)/2);
	} else {
		gimp_image_scale($img, $owidth*$hratio, $height);
		gimp_image_resize($img, $width, $height,
			-($owidth*$hratio-$width)/2, 0);
	}
	# print "target=$width x $height, image=$img_width x $img_height, bounds=", join(", ", @a), "\n";

	gimp_image_undo_group_end($img);
	undef;
}
