Gimp/plug-ins/perl/examples/pixelmap

146 lines
3.9 KiB
Text
Raw Normal View History

1999-05-31 15:37:16 -07:00
#!/usr/bin/perl -w
use Gimp::Feature 'pdl';
1999-07-30 13:51:19 -07:00
use Gimp 1.099;
1999-05-31 15:37:16 -07:00
use Gimp::Fu;
use Gimp::Util;
use PDL;
1999-07-29 14:28:02 -07:00
use constant PI => 4 * atan2 1,1;
1999-07-30 13:51:19 -07:00
sub pixelmap { # es folgt das eigentliche Skript...
1999-07-29 14:28:02 -07:00
my($image,$drawable,$_expr)=@_;
1999-05-31 15:37:16 -07:00
Gimp->progress_init ("Mapping pixels...");
my $init="";
1999-07-29 14:28:02 -07:00
$_expr =~ /\$p/ and $init.='$p = $src->data;';
1999-11-14 16:10:35 -08:00
$_expr =~ /\$P/ and $init.= $drawable->has_alpha ? '$P = $src->data;' : '$P = $src->data->slice("0:-1");';
1999-12-09 19:58:02 -08:00
$_expr =~ /\$x/ and $init.='$x = (zeroes(long,$w)->xvals + $_dst->x)->dummy(1,$h)->sever;';
$_expr =~ /\$y/ and $init.='$y = (zeroes(long,$h)->xvals + $_dst->y)->dummy(0,$w)->sever;';
1999-07-29 14:28:02 -07:00
$_expr =~ /\$bpp/ and $init.='$bpp = $_dst->bpp;';
1999-11-14 16:10:35 -08:00
my($p,$P,$x,$y,$bpp,$w,$h);
1999-05-31 15:37:16 -07:00
1999-07-29 14:28:02 -07:00
$_expr = "sub{$init\n#line 1\n$_expr\n;}";
1999-05-31 15:37:16 -07:00
1999-07-30 13:51:19 -07:00
my @_bounds = $drawable->bounds;
1999-05-31 15:37:16 -07:00
{
# $src and $dst must either be scoped or explicitly undef'ed
# before merge_shadow.
1999-07-30 13:51:19 -07:00
my $src = new PixelRgn $drawable->get,@_bounds,0,0;
1999-07-29 14:28:02 -07:00
my $_dst = new PixelRgn $drawable,@_bounds,1,1;
1999-05-31 15:37:16 -07:00
1999-07-29 14:28:02 -07:00
$_expr = eval $_expr; die "$@" if $@;
1999-05-31 15:37:16 -07:00
1999-07-29 14:28:02 -07:00
$_iter = Gimp->pixel_rgns_register ($src, $_dst);
my $_area = 0;
1999-05-31 15:37:16 -07:00
do {
1999-07-29 14:28:02 -07:00
($w,$h)=($src->w,$src->h);
$_area += $w*$h/($_bounds[2]*$_bounds[3]);
$_dst->data(&$_expr);
Gimp->progress_update ($_area);
} while (Gimp->pixel_rgns_process ($_iter));
1999-05-31 15:37:16 -07:00
}
$drawable->merge_shadow (1);
1999-07-30 13:51:19 -07:00
$drawable->update (@_bounds);
1999-05-31 15:37:16 -07:00
(); # wir haben kein neues Bild erzeugt
1999-07-30 13:51:19 -07:00
}
register "pixelmap",
"Maps Pixel values and coordinates through general Perl expressions",
"=pod(DESCRIPTION)",
"Marc Lehmann",
"Marc Lehmann <pcg\@goof.com>",
1999-11-14 16:10:35 -08:00
"19991115",
1999-11-29 13:46:18 -08:00
N_"<Image>/Filters/Map/Pixelmap",
1999-07-30 13:51:19 -07:00
"*",
[
[PF_TEXT, "expression" , "The perl expression to use", "outer(\$x*0.1,\$y*0.2)\n->slice(\"*\$bpp\")"]
],
\&pixelmap;
register "pixelgen",
"Generate the pixels of an image by expressions (in PDL)",
"=pod(DESCRIPTION)",
"Marc Lehmann",
"Marc Lehmann <pcg\@goof.com>",
1999-11-14 16:10:35 -08:00
"19991115",
1999-11-29 13:46:18 -08:00
N_"<Toolbox>/Xtns/Render/Pixelgenerator",
1999-07-30 13:51:19 -07:00
"*",
[
[PF_SPINNER, "width" , "The width of the new image to generate", 512, [1, 4096, 1]],
[PF_SPINNER, "height" , "The height of the new image to generate", 512, [1, 4096, 1]],
[PF_RADIO, "type" , "The type of the layer to create (same as gimp_layer_new.type)",
RGB_IMAGE , [RGB => RGB_IMAGE, RGBA => RGBA_IMAGE, GRAY => GRAY_IMAGE,
GRAYA => GRAYA_IMAGE, INDEXED => INDEXED_IMAGE, INDEXEDA => INDEXEDA_IMAGE]],
[PF_TEXT, "expression" , "The perl expression to use", "outer(\$x*0.1,\$y*0.2)\n->slice(\"*\$bpp\")"]
],
1999-08-03 09:20:05 -07:00
[PF_IMAGE],
1999-07-30 13:51:19 -07:00
sub {
my($w,$h,$type,$expr)=@_;
my $image = new Image $w, $h, Gimp->layer2imagetype($type);
my $layer = new Layer $image, $w, $h, $type, $expr, 100, NORMAL_MODE;
$image->add_layer($layer, 0);
eval { pixelmap($image, $layer, $expr) };
if ($@) {
my $error = $@;
$image->delete;
die $error;
};
$image;
1999-05-31 15:37:16 -07:00
};
exit main;
1999-07-12 13:59:54 -07:00
=head1 DESCRIPTION
Not yet written yet, sorry...
1999-11-14 16:10:35 -08:00
=over 4
=item $p
The source pixels (1..4 bytes per pixel, depending on format). Use like this:
$p*3.5 # the return value is the result
=item $P
The source pixels without alpha. Use it like this:
$P *= 0.5; $p # modify $P inplace, return also modified $p as result
=item $x
1999-12-09 19:58:02 -08:00
A two-dimensional vector containing the x-coordinates of each point in the current tile:
$x = (zeroes(long,$w)->xvals + $destination->x)->dummy(1,$h)->sever;
1999-11-14 16:10:35 -08:00
=item $y
1999-12-09 19:58:02 -08:00
A two-dimensional vector containing the y-coordinates of each point in the current tile:
$y = (zeroes(long,$h)->xvals + $destination->y)->dummy(0,$w)->sever;
1999-11-14 16:10:35 -08:00
=item $bpp
The bytes per pixel value of the destination area.
=back
1999-07-28 14:23:35 -07:00
1999-05-31 15:37:16 -07:00