Listing 2. Exporting an xcf with 8-Bit Alpha to an Image with One Bit of
Pretreated Alpha

#!/usr/bin/perl

use Gimp ":auto";
use Gimp::Fu;
use lib '/usr/local/bin/';
use MonkeyIQGIMP;

sub monkeyiq_gimp_convert {
  my( $inputimagename, $outfilename,
      $aliascolor ) = @_;

  Gimp->palette_set_foreground( $aliascolor );

  $img = Gimp->file_load( $inputimagename,
                         $inputimagename );
  $layer = Gimp->layer_new(
     $img,
     Gimp->image_width( $img ),
     Gimp->image_height( $img ),
     RGBA_IMAGE,
     "background merge image",
     100, NORMAL_MODE );
  Gimp->image_add_layer( $img, $layer, -1 );
  Gimp->image_lower_layer( $img, $layer );
  Gimp->edit_fill( $layer, BACKGROUND_FILL );

  $oldlayer =
    Gimp->image_merge_visible_layers(
      $img, EXPAND_AS_NECESSARY );
  Gimp->layer_set_name( $oldlayer, "oldlayer" );

  Gimp->selection_none( $img );
  $threshold = 1;
  Gimp->by_color_select( $oldlayer, $aliascolor,
                 $threshold, 0, 1, 0, 0, 0 );
  Gimp->selection_invert( $img );

  ##################################################

  $newlayer = Gimp->layer_new( $img,
                        Gimp->image_width( $img ),
                        Gimp->image_height( $img ),
                        1, "test xl", 100, 0 );
  Gimp->image_add_layer( $img, $newlayer, -1 );
  Gimp->edit_fill( $newlayer, TRANSPARENT_FILL );
  Gimp->edit_copy( $oldlayer );
  my $floatobject = Gimp->edit_paste( $newlayer,0 );
  Gimp->floating_sel_anchor( $floatobject );

  ##################################################

  # delete old background layer
  Gimp->layer_set_visible( $oldlayer, 0 );
  Gimp->layer_set_visible( $newlayer, 1 );

  imageOutput( $img, $outfilename );
}

register "monkeyiq_gimp_alias",
  "Alias alpha values to a background color",
  "Alias alpha values to a background color (and then
   remake one bit trans)",
  "Ben Martin", "Ben Martin",
  "2007-Mar-16",
  "<Toolbox>/Xtns/Perl-Fu/Alias As X",
  "*",
  [
   [PF_STRING, "inputimage",
     "Name of image to export", ""],
   [PF_STRING, "outputimage",
     "Name of image to save to", ""],
   [PF_COLOR,  "aliascolor",
 "Background color to alias with", ""],
  ],
  \&monkeyiq_gimp_convert;

if( $#ARGV <= 0 ) {
  print "Usage: $0 -inputimage imagepath";
  print " -outputimage full_dst_path";
  print " -aliascolor #000000 \n\n";
  exit;
}

# Handle over control to gimp
exit main();

