PDL.pm 2.82 KB
Newer Older
Marc Lehmann's avatar
Marc Lehmann committed
1
2
3
package Gimp::PDL;

use Carp;
Marc Lehmann's avatar
Marc Lehmann committed
4
use Gimp ();
Marc Lehmann's avatar
Marc Lehmann committed
5
6
use PDL;

Marc Lehmann's avatar
Marc Lehmann committed
7
sub Gimp::Tile::set_data($) {
Marc Lehmann's avatar
Marc Lehmann committed
8
   (my $p = byte $_[1])->make_physical;
Marc Lehmann's avatar
Marc Lehmann committed
9
   Gimp::Tile::_set_data($_[0],${$p->get_dataref});
Marc Lehmann's avatar
Marc Lehmann committed
10
11
};

Marc Lehmann's avatar
Marc Lehmann committed
12
sub Gimp::Tile::get_data($) {
Marc Lehmann's avatar
Marc Lehmann committed
13
14
15
   my($tile)=@_;
   my($pdl)=new_from_specification PDL (byte,width(),height(),
                                        $tile->bpp > 1 ? $tile->bpp : ());
Marc Lehmann's avatar
Marc Lehmann committed
16
   ${$pdl->get_dataref} = Gimp::Tile::_get_data(@_);
Marc Lehmann's avatar
Marc Lehmann committed
17
18
19
20
   $pdl->upd_data;
   return $pdl;
};

Marc Lehmann's avatar
Marc Lehmann committed
21
sub Gimp::PixelRgn::get_pixel {
Marc Lehmann's avatar
Marc Lehmann committed
22
23
   my($rgn)=@_;
   my($pdl)=new_from_specification PDL (byte,$_[0]->bpp);
Marc Lehmann's avatar
Marc Lehmann committed
24
   ${$pdl->get_dataref} = Gimp::PixelRgn::_get_pixel(@_);
Marc Lehmann's avatar
Marc Lehmann committed
25
26
27
28
   $pdl->upd_data;
   return $pdl;
};

Marc Lehmann's avatar
Marc Lehmann committed
29
sub Gimp::PixelRgn::get_col {
Marc Lehmann's avatar
Marc Lehmann committed
30
31
   my($rgn)=@_;
   my($pdl)=new_from_specification PDL (byte,$_[0]->bpp,$_[3]);
Marc Lehmann's avatar
Marc Lehmann committed
32
   ${$pdl->get_dataref} = Gimp::PixelRgn::__get_col(@_);
Marc Lehmann's avatar
Marc Lehmann committed
33
34
35
36
   $pdl->upd_data;
   return $pdl;
};

Marc Lehmann's avatar
Marc Lehmann committed
37
sub Gimp::PixelRgn::get_row {
Marc Lehmann's avatar
Marc Lehmann committed
38
39
   my($rgn)=@_;
   my($pdl)=new_from_specification PDL (byte,$_[0]->bpp,$_[3]);
Marc Lehmann's avatar
Marc Lehmann committed
40
   ${$pdl->get_dataref} = Gimp::PixelRgn::_get_row(@_);
Marc Lehmann's avatar
Marc Lehmann committed
41
42
43
44
   $pdl->upd_data;
   return $pdl;
};

Marc Lehmann's avatar
Marc Lehmann committed
45
sub Gimp::PixelRgn::get_rect {
Marc Lehmann's avatar
Marc Lehmann committed
46
   my($pdl)=new_from_specification PDL (byte,$_[0]->bpp,$_[3],$_[4]);
Marc Lehmann's avatar
Marc Lehmann committed
47
   ${$pdl->get_dataref} = Gimp::PixelRgn::_get_rect(@_);
Marc Lehmann's avatar
Marc Lehmann committed
48
49
50
51
   $pdl->upd_data;
   return $pdl;
};

Marc Lehmann's avatar
Marc Lehmann committed
52
sub Gimp::PixelRgn::set_pixel {
Marc Lehmann's avatar
Marc Lehmann committed
53
   (my $p = byte $_[1])->make_physical;
Marc Lehmann's avatar
Marc Lehmann committed
54
   Gimp::PixelRgn::_set_pixel($_[0],${$p->get_dataref},$_[2],$_[3]);
Marc Lehmann's avatar
Marc Lehmann committed
55
56
};

Marc Lehmann's avatar
Marc Lehmann committed
57
sub Gimp::PixelRgn::set_col {
Marc Lehmann's avatar
Marc Lehmann committed
58
   (my $p = byte $_[1])->make_physical;
Marc Lehmann's avatar
Marc Lehmann committed
59
   Gimp::PixelRgn::_set_col($_[0],${$p->get_dataref},$_[2],$_[3]);
Marc Lehmann's avatar
Marc Lehmann committed
60
61
};

Marc Lehmann's avatar
Marc Lehmann committed
62
sub Gimp::PixelRgn::set_row {
Marc Lehmann's avatar
Marc Lehmann committed
63
   (my $p = byte $_[1])->make_physical;
Marc Lehmann's avatar
Marc Lehmann committed
64
   Gimp::PixelRgn::_set_row($_[0],${$p->get_dataref},$_[2],$_[3]);
Marc Lehmann's avatar
Marc Lehmann committed
65
66
};

Marc Lehmann's avatar
Marc Lehmann committed
67
sub Gimp::PixelRgn::set_rect {
Marc Lehmann's avatar
Marc Lehmann committed
68
   (my $p = byte $_[1])->make_physical;
Marc Lehmann's avatar
Marc Lehmann committed
69
   Gimp::PixelRgn::_set_rect($_[0],${$p->get_dataref},$_[2],$_[3],($_[1]->dims)[1]);
Marc Lehmann's avatar
Marc Lehmann committed
70
71
72
73
74
75
76
77
78
79
80
81
};

1;
__END__

=head1 NAME

Gimp::PDL - Overwrite Tile/Region functions to work with piddles.

=head1 SYNOPSIS

  use Gimp;
Marc Lehmann's avatar
Marc Lehmann committed
82
  use Gimp::PDL;
Marc Lehmann's avatar
Marc Lehmann committed
83
84
85
86
  use PDL;

=head1 DESCRIPTION

Marc Lehmann's avatar
Marc Lehmann committed
87
88
89
90
This module overwrites some methods of Gimp::Tile and Gimp::PixelRgn. The
new functions return and accept piddles. The last argument (height) of
C<gimp_pixel_rgn_set_rect> is calculated from the piddle. There is no
other way to access the raw pixeldata in Gimp.
Marc Lehmann's avatar
Marc Lehmann committed
91
92
93

Some exmaples:

Marc Lehmann's avatar
Marc Lehmann committed
94
 $region = $drawable->get->pixel_rgn (0,0, 100,100, 1,0);
Marc Lehmann's avatar
Marc Lehmann committed
95
 $pixel = $region->get_pixel (5,7);	# fetches the pixel from (5|7)
Marc Lehmann's avatar
Marc Lehmann committed
96
97
98
 print $pixel;				# outputs something like
 					# [255, 127, 0], i.e. in
 					# RGB format ;)
Marc Lehmann's avatar
Marc Lehmann committed
99
100
101
102
103
104
105
106
107
108
109
110
 $region->set_pixel ($pixel * 0.5, 5, 7);# darken the pixel
 $rect = $region->get_rect (3,3,70,20);	# get a horizontal stripe
 $rect = $rect->hclip(255/5)*5;		# clip and multiply by 5
 $region->set_rect($rect);		# and draw it!
 undef $region;				# and update it!

=head1 AUTHOR

Marc Lehmann <pcg@goof.com>

=head1 SEE ALSO

Marc Lehmann's avatar
Marc Lehmann committed
111
L<Gimp::Pixel>, perl(1), Gimp(1).
Marc Lehmann's avatar
Marc Lehmann committed
112
113

=cut