#!/usr/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=U,1=D,2=L,3=R)", 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->insert_layer($temp1,0,-1);
  my $orientation = ($mirror < 2)
    ? &Gimp::ORIENTATION_VERTICAL
    : &Gimp::ORIENTATION_HORIZONTAL;
  $temp1 = $temp1->transform_flip_simple($orientation, 1, 0.0, 1);
  my @xywh;
  if ($mirror == 0) {
    # upper half
    @xywh = (0, $hspan, $w, $h - $hspan);
  } elsif ($mirror == 1) {
    # lower half
    @xywh = (0, 0, $w, $hspan);
  } elsif ($mirror == 2) {
    # left half
    @xywh = ($wspan, 0, $w - $wspan, $h);
  } elsif ($mirror == 3) {
    # right half
    @xywh = (0, 0, $wspan, $h);
  };
  $img->select_rectangle(&Gimp::CHANNEL_OP_REPLACE, @xywh);

  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;
};
exit main;

=head1 LICENSE

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

=cut

