diff --git a/plug-ins/perl/Changes b/plug-ins/perl/Changes index 3b2f29d488..374e211891 100644 --- a/plug-ins/perl/Changes +++ b/plug-ins/perl/Changes @@ -3,6 +3,7 @@ Revision history for Gimp-Perl extension. - passing arguments on the commandline works again (formerly all arguments were treated as integers) - added the PDB extension to the distribution (alpha!) + - added tex-to-float and alpha2color.pl plug-ins 1.046 Thu Nov 5 01:53:34 CET 1998 - the syntax really gets tricky - references to INT32 and similar diff --git a/plug-ins/perl/Makefile.PL b/plug-ins/perl/Makefile.PL index e6cb799fbb..277c48aa06 100644 --- a/plug-ins/perl/Makefile.PL +++ b/plug-ins/perl/Makefile.PL @@ -129,6 +129,8 @@ install-plugins: -cd examples && $GIMPTOOL2 --install-admin-bin prep4gif.pl -cd examples && $GIMPTOOL2 --install-admin-bin webify.pl -cd examples && $GIMPTOOL2 --install-admin-bin PDB + -cd examples && $GIMPTOOL2 --install-admin-bin alpha2color.pl + -cd examples && $GIMPTOOL2 --install-admin-bin tex-to-float # -cd examples && $GIMPTOOL2 --install-admin-bin border.pl EOF } diff --git a/plug-ins/perl/examples/alpha2color.pl b/plug-ins/perl/examples/alpha2color.pl new file mode 100755 index 0000000000..6f754cfb9c --- /dev/null +++ b/plug-ins/perl/examples/alpha2color.pl @@ -0,0 +1,121 @@ +#!/usr/bin/perl + +use Gimp qw( :auto ); +use Gimp::Fu; + +# alpha2color.pl +# by Seth Burgess +# Version 0.02 +# Oct 16th, 1998 +# +# This script simply changes the current alpha channel to a given color +# instead. I'm writing it primarily for use with the displace plugin, +# but I imagine it'll have other uses. + +# TODO: Selection is currently ignored. It'd be better if it remembered +# what the previous selection was. +# Also, it needs to find a happier home than in the Filters/Misc menu. + +# Gimp::set_trace(TRACE_ALL); + +# Revision History +# v0.02 - fixed up @color (should be $color) and undef; (should be return();) + +sub save_layers_state ($) { + $img = shift; + my @layers = $img->get_layers; + $i = 0; + foreach $lay (@layers) { + if ($lay->get_visible){ + $arr[$i] = 1; + } + else { + $arr[$i] = 0; + } + $i++; + } + return @arr; + } + +sub restore_layers_state($@) { + $img = shift; + @arr = @_; + my @layers = $img->get_layers; + $i = 0; + foreach $lay (@layers) { + $lay->set_visible($arr[$i]); + $i++; + } + } + + + +sub alpha2col { + my ($img, $drawable, $color) = @_; + + my $oldcolor = gimp_palette_get_background(); + + my @layers = gimp_image_get_layers($img); + +# if there's not enough layers, abort. + if ($#layers < 0) { + gimp_message("You need at least 1 layer to perform alpha2color!"); + print "Only ", scalar(@layers), " layers found!(", $layers[0],")\n"; + return 0; + } + +# Hide the bottom layer, so it doesn't get into the merge visible later. + + @layer_visibilities = save_layers_state ($img); + # foreach $visible (@layer_visibilities) { + # print $visible, "\n"; + # } + $target_layer = gimp_image_get_active_layer($img); + @offsets=$target_layer->offsets; + # print $target_layer, "\n"; + foreach $eachlay (@layers) { + $eachlay->set_visible(0); + } + $target_layer->set_visible(1); + gimp_palette_set_background($color); +# $newlay = gimp_layer_new ( $img, +# $target_layer->width, +# $target_layer->height, +# 1, "NewLayer", 100, NORMAL); +# $img->add_layer($newlay, scalar(@layers)); + $newlay = $target_layer->copy(1); + $img->add_layer($newlay, 0); + $newlay->set_offsets(@offsets); + $target_layer->set_active_layer; + + $img->selection_all; + $target_layer->edit_fill; + $img->selection_none; + + $foreground = gimp_image_merge_visible_layers($img,0); + + restore_layers_state($img, @layer_visibilities); + + gimp_palette_set_background($oldcolor); + gimp_displays_flush(); + return(); + } + +register + "plug_in_alpha2color", + "Alpha 2 Color", + "Change the current alpha to a selected color.", + "Seth Burgess", + "Seth Burgess", + "1998-10-18", + "/Filters/Misc/Alpha2Color", + "RGBA", + [ + [PF_COLOR, "Color", "Color for current alpha", [127,127,127]] + ], + \&alpha2col; + +exit main(); + + + diff --git a/plug-ins/perl/examples/tex-to-float b/plug-ins/perl/examples/tex-to-float new file mode 100755 index 0000000000..e578e0fc23 --- /dev/null +++ b/plug-ins/perl/examples/tex-to-float @@ -0,0 +1,178 @@ +#!/usr/local/bin/perl +###################################################################### +# A Perl::Fu plugin for converting TeX strings to floating layers. +# +# Author: Dov Grobgeld +# Version: 0.11 +###################################################################### + +use Gimp; +use Gimp::Fu; + +my $fn_base = "/tmp/ttf$$"; +my %tmpfiles = ( + "$fn_base.pgm"=>1, + "$fn_base.tex"=>1, + "$fn_base.log"=>1, + "$fn_base.ps"=>1, + "$fn_base.dvi"=>1); + +# Cleanup +sub cleanup { + foreach my $fn (keys %tmpfiles) { + unlink $fn; + } +} + +sub xec { + my $cmd = shift; + print STDERR "$cmd\n"; + return `$cmd`; +} + +sub exist_in_tex_path { + my $file = shift; + + return 0 unless length($file); + return 1 if -e $file; + foreach my $p (split(/:/, $ENV{TEXINPUTS} . ":/tmp")) { + return 1 if -e "$p/$file"; + } + return 0; +} + +sub tex_string_to_pgm { + my($text, $input_file, $string, $ppi, $magstep, $anti_aliasing, + $output_file) = @_; + + my $scale = sprintf("%.5f", 1/$anti_aliasing); + my $r = $ppi * $anti_aliasing; + my $device = "pgmraw"; + + chdir "/tmp"; + if (exist_in_tex_path($input_file)) { + $input .= "\\input $input_file\n"; + } + + open(TEX, ">$fn_base.tex"); + print TEX "\\nopagenumbers\n" + . "\\magnification\\magstep$magstep\n" + . "\\tolerance=8000\n" + . "$input\n" + . "$string\n" + . "\\bye"; + close(TEX); + + my $res = xec("tex $fn_base.tex < /dev/null"); + # Deal with errors... + + # Make dvips output bounding box + my $psoutput = xec("dvips -r$r -E -f $fn_base"); + # Deal with errors + + # Shift postscript file to origin + my @bbox = $psoutput=~ /^%%BoundingBox:\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/m; + print STDERR "bbox = @bbox\n"; + my $w = $bbox[2]-$bbox[0]; + my $h = $bbox[3]-$bbox[1]; + $psoutput=~ s/^%%BoundingBox:.*$/%%BoundingBox: 0 0 $w $h/m; + $psoutput=~ s/^1 0 bop/-$bbox[0] -$bbox[1] translate\n$&/m; + + # Output to file in order not to have to use Open2. + open(PS, ">$fn_base.ps"); + print PS $psoutput; + close(PS); + $w= int($w*$r/72+0.5); + $h= int($h*$r/72+0.5); + + # Use gs to produce a pgmfile + my $cmd = "gs -g${w}x${h} -r${r} -dNOPAUSE -q -sDEVICE=$device -sOutputFile=- $fn_base.ps quit.ps |"; + $cmd .= "pnmscale $scale |" if $scale != 1; + chop($cmd); + $cmd .= "> $output_file"; + xec("$cmd"); +} + +sub grey_file_to_float { + my($img1, $drw1, $fn) = @_; + + # Setup + my $save_bg = gimp_palette_get_background(); + gimp_undo_push_group_start($img1); + + # Load the new img + my $grey_img = gimp_file_load(RUN_NON_INTERACTIVE, $fn, $fn); + + # Get name of new layer + my $grey_layer = gimp_image_get_active_layer($grey_img); + + # Create an alpha layer and copy image to alpha layer + gimp_layer_add_alpha($grey_layer); + $grey_img->selection_all(); + gimp_edit_copy($grey_img,$grey_layer); + $mask = gimp_layer_create_mask($grey_layer, 0); + gimp_image_add_layer_mask($grey_img, $grey_layer, $mask); + my $floating_layer = gimp_edit_paste($grey_img, $mask, 0); + gimp_floating_sel_anchor($floating_layer); + gimp_invert($grey_img, $mask); + gimp_palette_set_background(gimp_palette_get_foreground()); + gimp_edit_fill($grey_img, $grey_layer); + gimp_image_remove_layer_mask($grey_img, $grey_layer, 0); + + # Now copy this layer to $img 1 + gimp_edit_copy($grey_img, $grey_layer); + $floating_layer = gimp_edit_paste($img1, $drw1, 0); + gimp_edit_fill($img1, $floating_layer); + + print STDERR "Yohoo!\n"; + cleanup(); + + # Get rid of $grey_img + gimp_image_delete($grey_img); + + + # Restore + gimp_palette_set_background($save_bg); + gimp_undo_push_group_end($img1); + + # Update the display + gimp_displays_flush(); + + return undef; +} + + +sub tex_string_to_float { + my($img1, $drw1, + $input_file, + $text, + $ppi, + $magstep, + $anti_aliasing) = @_; + + + tex_string_to_pgm($text, $input_file, $text, $ppi, $magstep, $anti_aliasing, + "$fn_base.pgm"); + + grey_file_to_float($img1, $drw1, "$fn_base.pgm"); + + return undef; +} + +# register the script +register "tex_string_to_float", "Turn a TeX-string into floating layer", "Takes a TeX string as input and creates a floating layer of the rendered string in the current layer in the foreground color.", + "Dov Grobgeld ", "Dov Grobgeld", + "1998-11-02", + "/Perl-Fu/TeX String", + "*", + [ + [PF_TEXT, "Input file", "TeX macro file to input", ""], + [PF_TEXT, "TeX String", "Enter TeX String", ""], + [PF_VALUE, "DPI", "Resolution to render the text in", "72"], + [PF_VALUE, "Magstep", "TeX magstep", "2"], + [PF_VALUE, "Anti-aliasing", "Anti-aliasing factor", "4"], + ], + \&tex_string_to_float; + +# Handle over control to gimp +exit main();