Photo.pm

filter: Statementstmt 16.8% 19 / 113 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 29.4% 5 / 17 Podpod 70.0% 7 / 10 totaltotal 14.0% 31 / 221 CC 36 SCAR 38.5
CC
36
Cov
10%
line
110.0
xy
72.0
rectangle
56.0
CRAP 47.1
« Canvas.pm ↑ Summary
1 # Copyright 2010, 2011, 2012 Kevin Ryde
2  
3 # This file is part of Image-Base-Tk.
4 #
5 # Image-Base-Tk is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Image-Base-Tk is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Image-Base-Tk. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 # Tk::Photo
20 # Tk::Image
21 # Tk::options configure(), cget()
22 #
23 # Tk::PNG
24 # Tk::JPEG
25 # Tk::TIFF
26 # loaders
27  
28 package Image::Base::Tk::Photo;
29 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 $VERSION = 3;
35  
36 1 ▶use Image::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 @ISA = ('Image::Base');
38  
39 # uncomment this to run the ### lines
40 #use Smart::Comments '###';
41  
42  
43 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
44 0 my ($class, %params) = @_;
45 ### Image-Base-Tk new() ...
46  
47 # $obj->new(...) means make a copy, with some extra settings
48 0 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 0 0
49 0 croak "Cannot clone Image::Base::Tk::Photo";
50  
51 # how to clone a Photo? how to get its originating widget to create new?
52 # my $self = $class;
53 # $class = ref $class;
54 # if (! defined $params{'-tkphoto'}) {
55 # my $tkphoto = $self->{'-tkphoto'};
56 # my $new_tkphoto = $tkphoto->Photo (map {$_=>$tkphoto->cget($_)}
57 # qw(-width -height -gamma -palette));
58 # $new_tkphoto->copy($tkphoto);
59 # $params{'-tkphoto'} = $new_tkphoto;
60 # }
61 # # inherit everything else
62 # %params = (%$self, %params);
63 # ### copy params: \%params
64 }
65  
66 0 ▶ if (! defined $params{'-tkphoto'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"-tkphoto"}) 0 0 0
67 0 ▶ my $for_widget = delete $params{'-for_widget'}
Condition0%0/2
delete $params{'-for_widget'} || croak('Must have -for_widget to create new Tk::Photo')
l!l
00
MC/DC0%0/1
delete $params{'-for_widget'} || croak('Must have -for_widget to create new Tk::Photo')
delete $params{'-for_widget'}
Truth table0%0/2
A
A= delete $params{'-for_widget'}
Aresult
11
00
68 || croak 'Must have -for_widget to create new Tk::Photo';
69 $params{'-tkphoto'} = $for_widget->Photo (-width => $params{'-width'},
70 0 -height => $params{'-height'});
71 }
72 0 my $self = bless {}, $class;
73 0 $self->set (%params);
74  
75 0 ▶ if (exists $params{'-file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"-file"}) 0 0 0
76 0 $self->load;
77 }
78  
79 ### new made: $self
80 0 return $self;
81 }
82  
83 1 my %attr_to_option = (-width => '-width',
84 -height => '-height');
85 0 ▶sub _get {
Subroutine _get: not called CC=2 SCAR=17.9
86 0 my ($self, $key) = @_;
87 ### Image-Base-Tk-Photo _get(): $key
88 0 ▶ if (my $option = $attr_to_option{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $option = $attr_to_option{$key}) 0 0 0
89 ### $option
90 0 return $self->{'-tkphoto'}->cget($option);
91 }
92 0 return $self->SUPER::_get ($key);
93 }
94  
95 0 ▶sub set {
Subroutine set: not called CC=5 SCAR=34.0
Pod documented
96 0 my ($self, %param) = @_;
97 ### Image-Base-Tk-Photo set(): \%param
98  
99 # apply this first
100 0 ▶ if (my $tkphoto = delete $param{'-tkphoto'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tkphoto = delete $param{"-tkphoto"}) 0 0 0
101 0 $self->{'-tkphoto'} = $tkphoto;
102 }
103  
104 {
105 0 my @configure;
106 0 foreach my $key (keys %param) {
107 0 ▶ if (my $option = $attr_to_option{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $option = $attr_to_option{$key}) 0 0 0
108 0 my $value = delete $param{$key};
109 0 push @configure, $option, $value;
110 }
111 }
112 ### @configure
113 0 ▶ if (@configure) {
Branch: 1 branch
BranchTrueFalseTotal
if (@configure) 0 0 0
114 0 $self->{'-tkphoto'}->configure (@configure);
115 }
116 }
117  
118 0 %$self = (%$self, %param);
119 }
120  
121 0 ▶sub load {
Subroutine load: not called CC=2 SCAR=17.9
Pod documented
122 0 my ($self, $filename) = @_;
123 ### Image-Base-Tk-Photo load()
124  
125 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
126 0 $filename = $self->get('-file');
127 } else {
128 0 $self->set('-file', $filename);
129 }
130 0 my $tkphoto = $self->{'-tkphoto'};
131 0 $tkphoto->read ($filename);
132 0 $self->set (-file_format => $tkphoto->cget('-format'));
133 }
134  
135 # undocumented, untested ...
136 0 ▶sub load_string {
Subroutine load_string: not called CC=1 SCAR=6.9
Pod undocumented
137 0 my ($self, $str) = @_;
138 ### Image-Base-Tk-Photo load()
139 0 my $tkphoto = $self->{'-tkphoto'};
140 0 $tkphoto->configure (-data => $str);
141 0 $self->set (-file_format => $tkphoto->cget('-format'));
142 }
143  
144 1 my %format_to_module = (png => 'Tk::PNG',
145 jpeg => 'Tk::JPEG',
146 tiff => 'Tk::TIFF',
147 );
148 0 ▶sub _format_use {
Subroutine _format_use: not called CC=3 SCAR=24.8
149 0 my ($format) = @_;
150 0 ▶ if (my $module = $format_to_module{lc($format)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $module = $format_to_module{lc $format}) 0 0 0
151 0 ▶ eval "require $module; 1" or die;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $module; 1" or die 0 0 0
152 }
153 0 return $format;
154 }
155  
156 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
157 0 my ($self, $filename) = @_;
158 ### Image-Base-Tk-Photo save()
159 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
160 0 $self->set('-file', $filename);
161 } else {
162 0 $filename = $self->get('-file');
163 }
164 0 my $tkphoto = $self->{'-tkphoto'};
165 ### file: $filename
166  
167 # croaks if an error ...
168 0 $tkphoto->write ($filename,
169 -format => _format_use($self->get('-file_format')));
170 }
171  
172 # undocumented, untested ...
173 0 ▶sub save_fh {
Subroutine save_fh: not called CC=1 SCAR=6.9
Pod undocumented
174 0 my ($self, $fh) = @_;
175 0 print $fh $self->save_string;
176 }
177  
178 # undocumented, untested ...
179 0 ▶sub save_string {
Subroutine save_string: not called CC=1 SCAR=6.9
Pod undocumented
180 0 my ($self, $fh) = @_;
181 # croaks if an error ...
182 0 return $self->{'-tkphoto'}->data
183 (-format => _format_use($self->get('-file_format')));
184 }
185  
186 #------------------------------------------------------------------------------
187 # drawing
188  
189 0 ▶sub xy {
Subroutine xy: not called CC=8 SCAR=42.8
Pod documented
190 0 my ($self, $x, $y, $colour) = @_;
191 ### Image-Base-Tk-Photo xy() ...
192  
193 # "-to" doesn't allow negative coordinates
194 0 ▶ if ($x < 0 || $y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0) 0 0 0
Condition0%0/3
$x < 0 or $y < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$x < 0 or $y < 0
$x < 0 $y < 0
Truth table0%0/3
A or B
A= $x < 0B= $y < 0
ABresult
1X1
011
000
195 0 return undef;
196 }
197  
198 0 my $tkphoto = $self->{'-tkphoto'};
199 0 ▶ if (@_ > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 3) { } 0 0 0
200 0 ▶ if (lc($colour) eq 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $colour eq 'none') { } 0 0 0
201 0 $tkphoto->transparencySet ($x, $y, 1);
202 } else {
203 0 $tkphoto->put ($colour, -to => $x,$y, $x+1,$y+1);
204 }
205 } else {
206 # get() and transparencyGet() don't allow x,y outside photo
207 0 ▶ if ($x >= $tkphoto->cget('-width')
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $tkphoto->cget("-width") or $y >= $tkphoto->cget("-height")) 0 0 0
Condition0%0/3
$x >= $tkphoto->cget("-width") or $y >= $tkphoto->cget("-height")
l!l&&r!l&&!r
000
MC/DC0%0/2
$x >= $tkphoto->cget("-width") or $y >= $tkphoto->cget("-height")
$x >= $tkphoto->cget("-width") $y >= $tkphoto->cget("-height")
Truth table0%0/3
A or B
A= $x >= $tkphoto->cget("-width")B= $y >= $tkphoto->cget("-height")
ABresult
1X1
011
000
208 || $y >= $tkphoto->cget('-height')) {
209 0 return undef;
210 }
211  
212 0 ▶ if ($tkphoto->transparencyGet ($x, $y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tkphoto->transparencyGet($x, $y)) { } 0 0 0
213 0 return 'None';
214 } else {
215 0 return sprintf ('#%02X%02X%02X', $tkphoto->get ($x, $y)); # r,g,b
216 }
217 }
218 }
219  
220 0 ▶sub rectangle {
Subroutine rectangle: not called CC=7 SCAR=40.3
Pod documented
221 0 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
222 ### Image-Base-Tk-Photo rectangle() ...
223  
224 0 ▶ if ($fill && lc($colour) ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fill and lc $colour ne 'none') { } 0 0 0
Condition0%0/3
$fill and lc $colour ne "none"
!ll&&!rl&&r
000
MC/DC0%0/2
$fill and lc $colour ne "none"
$fill lc $colour ne "none"
Truth table0%0/3
A and B
A= $fillB= lc $colour ne "none"
ABresult
0X0
100
111
225 ### filled rectangle with put() ...
226  
227 0 ▶ if ($x2 >= 0 && $y2 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 >= 0 and $y2 >= 0) 0 0 0
Condition0%0/3
$x2 >= 0 and $y2 >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$x2 >= 0 and $y2 >= 0
$x2 >= 0 $y2 >= 0
Truth table0%0/3
A and B
A= $x2 >= 0B= $y2 >= 0
ABresult
0X0
100
111
228 # "-to" doesn't allow negative coordinates
229 0 ▶ if ($x1 < 0) { $x1 = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 0) 0 0 0
230 0 ▶ if ($y1 < 0) { $y1 = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 0 0 0
231  
232 ### put: "$x1,$y1 ".($x2+1).",".($y2+1)
233 0 $self->{'-tkphoto'}->put ($colour, -to => $x1,$y1, $x2+1,$y2+1);
234 }
235  
236 } else {
237 ### unfilled or transparent rectangle with superclass lines ...
238 0 shift->SUPER::rectangle(@_);
239 }
240 }
241  
242 0 ▶sub line {
Subroutine line: not called CC=10 SCAR=47.0
Pod documented
243 0 my ($self, $x1, $y1, $x2, $y2, $colour) = @_;
244 ### Image-Base-Tk-Photo line(): "$x1,$y1, $x2,$y2"
245  
246 0 ▶ if (lc($colour) eq 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $colour eq "none") 0 0 0
247 # any transparency by individual xy() pixels (with transparencySet())
248 0 shift->SUPER::line(@_);
249 0 return;
250 }
251  
252 0 ▶ if ($x1 == $x2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x1 == $x2) { } 0 0 0
elsif ($y1 == $y2) { } 0 0 0
253 # vertical line by put() rectangle
254 0 ▶ if ($x1 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 0) 0 0 0
255 ### vertical line all negative ...
256 0 return;
257 }
258 0 ▶ if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 > $y2) 0 0 0
259  
260 } elsif ($y1 == $y2) {
261 # horizontal line by put() rectangle
262 0 ▶ if ($y1 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 0 0 0
263 ### horizontal line all negative ...
264 0 return;
265 }
266 0 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 0 0
267  
268 } else {
269 ### sloped line by individual xy() pixels ...
270 0 shift->SUPER::line(@_);
271 0 return;
272 }
273  
274 # "-to" doesn't allow negative coordinates
275 0 ▶ if ($x1 < 0) { $x1 = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 0) 0 0 0
276 0 ▶ if ($y1 < 0) { $y1 = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < 0) 0 0 0
277  
278 ### put(): "$x1,$y1, ".($x2+1).",".($y2+1)
279 0 $self->{'-tkphoto'}->put ($colour, -to => $x1,$y1, $x2+1,$y2+1);
280 }
281  
282 1 1;
283 __END__
« Canvas.pm ↑ Summary