#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

use Gimp;
use Gimp::Fu;

#Gimp::set_trace(TRACE_ALL);

register "webify",
         "Make an image suitable for the web",
         "This plug-in converts the image to indexed, with some extra options",
         "Marc Lehmann",
         "Marc Lehmann",
         "1.0",
         N_"<Image>/Filters/Web/Webify...",
         "RGB*, GRAY*",
         [
          [PF_BOOL,	"new",		"create a new image?", 1],
          [PF_BOOL,	"transparent",	"make transparent?", 1],
          [PF_COLOUR,	"bg_color",	"the background colour to use for transparency", "white"],
          [PF_SLIDER,	"threshold",	"the threshold to use for background detection", 3, [0, 255, 1]],
          [PF_INT32,	"colors",	"how many colours to use (0 = don't convert to indexed)", 32],
          [PF_BOOL,	"autocrop",	"autocrop at end?", 1],
         ],
	 [ [PF_IMAGE, 'image', 'Output image', 0] ],
         sub {
   my($img,$drawable,$new,$alpha,$bg,$thresh,$colours,$autocrop)=@_;
   $thresh /= 255;

   $img = $img->duplicate if $new;

   eval { $img->undo_group_start };

   $drawable = $img->flatten;

   if ($alpha) {
      $drawable->add_alpha;
      Gimp::Context->set_sample_threshold($thresh);
      $img->select_color(CHANNEL_OP_REPLACE,$drawable,$bg);
      $drawable->edit_cut if $img->selection_bounds;
   }
   Gimp::Plugin->autocrop($drawable) if $autocrop;
   $img->convert_indexed (2, 0, $colours, 0, 0, '') if $colours;

   eval { $img->undo_group_end };

   $new ? ($img->clean_all, $img) : ($img);
};

exit main;

=head1 LICENSE

Copyright Marc Lehman.
Distributed under the same terms as Gimp-Perl.

=cut
