#!/usr/bin/perl
#
# A plug-in for GIMP which animates a series of layers as if
# they were animation cells (different from the normal gimp animation,
# in that each cell REPLACES the previous, instead of adding. The
# background cell (bottom most layer) is always kept.
#
# Written in 1999 (c) by Aaron Sherman <ajs@ajs.com>.
# This plugin may be distributed under the same terms as The Gimp itself.
# See http://www.gimp.org/ for more information on The Gimp.
#

require 5.004;

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

$animate_cells_version = "1.1.1";
$animate_cells_released = "3/12/1999";

# use strict;

sub perl_fu_animate_cells {
  my $image = shift;
  # my $drawable = shift; # Unused
  gimp_image_disable_undo($image);

  my @ids = reverse gimp_image_get_layers($image);
  my $back = shift @ids;

  if (@ids < 2) {
    gimp_message("animate_cells: Too few cells (layers) in image.");
    return;
  }

  gimp_selection_layer_alpha($ids[0]);
  for($i=1;$i<@ids;$i++) {
    $lnum = $#ids+1-$i;
    fix_cell_layer($image, $ids[$i], $ids[$i-1], $back, $lnum);
  }

  for($i=$#ids;$i>=0;$i--) {
    gimp_image_merge_down($image, $ids[$i], EXPAND_AS_NECESSARY);
  }

  gimp_selection_none($image);
  gimp_image_enable_undo($image);
  gimp_displays_flush();
}

sub fix_cell_layer {
  my $img = shift; # The image
  my $target = shift; # The target layer
  my $prev = shift; # The layer before it
  my $back = shift; # The background layer
  my $lnum = shift; # The new layer's number
  my $dup = gimp_layer_copy($prev,0);
  # Tried to do a gimp_layer_get_position($target), here, but it failed...
  gimp_image_add_layer($img, $dup, $lnum);
  gimp_selection_sharpen($img); # No feathered or fuzzy selection areas
  gimp_selection_grow($img,1); # XXX - Gets around gimp 1-pixel bug
  gimp_edit_copy($back);
  my $float = gimp_edit_paste($dup,0);
  gimp_floating_sel_anchor($float);
  gimp_selection_layer_alpha($target);
}

# Gimp::Fu registration routine for placing this function into gimp's PDB
register
  "animate_cells",
  "Perform cell animation from a single, layered image",
  "Use this plugin to animate a series of layers in the same way that\
	a physical animation process would use cells.",
  "Aaron Sherman", "Aaron Sherman (c)", "1999-03-12",
  "<Image>/Filters/Animation/Animate Cells",
  "*",
  [
  ],
  \&perl_fu_animate_cells;

exit main;

__END__

=head1 NAME

animate_cells - Animate an image

=head1 SYNOPSIS

Called from the Gimp. Use Gimp's user interface to call this function.

=head1 DESCRIPTION

TBD

=head1 PARAMETERS

None.

=head1 AUTHOR

Written in 1999 (c) by Aaron Sherman E<lt>ajs@ajs.comE<gt>

=head1 BUGS

TBD

=head1 SEE ALSO

L<gimp>, L<perl>, L<Gimp>: the Gimp module for perl.

=cut
