#!/opt/bin/perl

use Gimp qw(:auto __ N_);
use Gimp::Fu;
use Gimp::Util;
use strict;
#	Gimp::set_trace(TRACE_ALL);
# 
# <sjburges@gimp.org> 2/25/04: changed specifiers to explictly reference 
# the GIMP namespace, s/undo_push/undo/, now have to add layer to image in
# order to flip it, or you get a PDB error.  So added, flipped, used, then 
# removed.

register	"mirror_split",
		"Splits and mirrors half of the image, according to settings.",
		"Just tick appropriate radio button.",
		"Claes G Lindblad <claesg\@algonet.se>",
		"Claes G Lindblad <claesg\@algonet.se>",
		"990530",
		N_"<Image>/Filters/Distorts/MirrorSplit...",
		"*",
		[
		[PF_RADIO, "mirror", "Which half to mirror?", 0,
			[Upper => 0, Lower => 1, Left => 2, Right => 3]
		]
		],
	sub {
		my ($img, $layer, $mirror) = @_;

		my $w = $layer->width();
		my $h = $layer->height();
		my $wspan = int ($w / 2 + 0.5);
		my $hspan = int ($h / 2 + 0.5);

		eval { $img->undo_group_start };

		my $temp1 = gimp_layer_copy($layer, 1);
                $img->add_layer($temp1,-1);
		if ($mirror == 0) {	# upper half
			$temp1 = gimp_flip($temp1, &Gimp::ORIENTATION_VERTICAL);
			gimp_rect_select($img, 0, $hspan, $w, $h - $hspan, &Gimp::CHANNEL_OP_REPLACE, 0, 0);
		};
		if ($mirror == 1) {	# lower half
			$temp1 = gimp_flip($temp1, &Gimp::ORIENTATION_VERTICAL);
			gimp_rect_select($img, 0, 0, $w, $hspan, &Gimp::CHANNEL_OP_REPLACE, 0, 0);
		};
		if ($mirror == 2) {	# left half
			$temp1 = gimp_flip($temp1, &Gimp::ORIENTATION_HORIZONTAL);
			gimp_rect_select($img, $wspan, 0, $w - $wspan, $h, &Gimp::CHANNEL_OP_REPLACE, 0, 0);
		};
		if ($mirror == 3) {	# right half
			$temp1 = gimp_flip($temp1, &Gimp::ORIENTATION_HORIZONTAL);
			gimp_rect_select($img, 0, 0, $wspan, $h, &Gimp::CHANNEL_OP_REPLACE, 0, 0);
		};

		gimp_edit_copy($temp1);
		my $temp2 = gimp_edit_paste($layer, 1);
		gimp_floating_sel_anchor($temp2);
		$img->remove_layer($temp1);
		gimp_selection_none($img);

		eval { $img->undo_group_end };
		return $img;
	};
exit main;

=head1 LICENSE

Copyright Claes G. Lindbad.
Distributed under terms of the GNU Public License.

=cut

