#!/usr/bin/perl

use Gimp qw(:auto __ N_);
use Gimp::Fu;
use strict;
#	Gimp::set_trace(TRACE_ALL);

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_push_group_start };

		my $temp1 = gimp_layer_copy($layer, 1);

		if ($mirror == 0) {	# upper half
			$temp1 = gimp_flip($temp1, ORIENTATION_VERTICAL);
			gimp_rect_select($img, 0, $hspan, $w, $h - $hspan, REPLACE, 0, 0);
		};
		if ($mirror == 1) {	# lower half
			$temp1 = gimp_flip($temp1, ORIENTATION_VERTICAL);
			gimp_rect_select($img, 0, 0, $w, $hspan, REPLACE, 0, 0);
		};
		if ($mirror == 2) {	# left half
			$temp1 = gimp_flip($temp1, ORIENTATION_HORIZONTAL);
			gimp_rect_select($img, $wspan, 0, $w - $wspan, $h, REPLACE, 0, 0);
		};
		if ($mirror == 3) {	# right half
			$temp1 = gimp_flip($temp1, ORIENTATION_HORIZONTAL);
			gimp_rect_select($img, 0, 0, $wspan, $h, REPLACE, 0, 0);
		};

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

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