Image.pm untested

filter: Statementstmt 0.0% 0 / 160 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 337 CC 35 SCAR 31.0
CC
35
Cov
0%
set
42.0
xy
42.0
new
30.0
CRAP 22.3
1 # Copyright 2012, 2019 Kevin Ryde
2  
3 # This file is part of Image-Base-Wx.
4 #
5 # Image-Base-Wx is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Image-Base-Wx 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-Wx. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Image::Base::Wx::Image;
20 use 5.008;
21 use strict;
22 use Carp;
23 use Wx;
24 our $VERSION = 6;
25  
26 use Image::Base;
27 our @ISA = ('Image::Base');
28  
29 # uncomment this to run the ### lines
30 # use Smart::Comments;
31  
32  
33 sub new {
34 my ($class, %params) = @_;
35 ### Image-Base-Wx-Image new: \%params
36  
37 my $self;
38 my $wximage = delete $params{'-wximage'};
39 my $filename = delete $params{'-file'};
40  
41 if (ref $class) {
42 # $obj->new(...) means make a copy, with some extra settings
43 $self = bless { %$class }, ref $class;
44  
45 # clone wximage if a new one not given in the %params
46 if (! defined $wximage) {
47 # wxPerl circa 0.9901 doesn't have the copy-on-write constructor, so a
48 # full ->Copy
49 $wximage = $self->{'-wximage'}->Copy;
50 }
51 } else {
52 if (! $wximage) {
53 # no clear parameter ?
54 $wximage = Wx::Image->new
55 (delete $params{'-width'}||0,
56 delete $params{'-height'}||0);
57 }
58 $self = bless { }, $class;
59 }
60 $self->set (-wximage => $wximage,
61 %params);
62 ### $filename
63 if (defined $filename) {
64 $self->load ($filename);
65 }
66 ### new created: $self
67 return $self;
68 }
69  
70 my %attr_to_get_method
71 = (-width => 'GetWidth',
72 -height => 'GetHeight');
73 my %attr_to_option
74 = (-hotx => Wx::wxIMAGE_OPTION_CUR_HOTSPOT_X(),
75 -hoty => Wx::wxIMAGE_OPTION_CUR_HOTSPOT_Y(),
76 -quality_percent => Wx::wxIMAGE_OPTION_QUALITY(),
77 );
78 sub _get {
79 my ($self, $key) = @_;
80 my $wximage = $self->{'-wximage'};
81 if (my $method = $attr_to_get_method{$key}) {
82 return $wximage->$method();
83 }
84 if (my $option = $attr_to_option{$key}) {
85 # all of the above options are integers hence GetOptionInt()
86 return ($wximage->HasOption($option)
87 ? $wximage->GetOptionInt($option)
88 : undef);
89 }
90 return $self->SUPER::_get($key);
91 }
92  
93 sub set {
94 my ($self, %params) = @_;
95 ### Image-Base-Wx-Image set: \%params
96  
97 # -wximage before applying -width,-height
98 if (my $wximage = delete $params{'-wximage'}) {
99 $self->{'-wximage'} = $wximage;
100 }
101 if (exists $params{'-width'} || exists $params{'-height'}) {
102 my $wximage = $self->{'-wximage'};
103 my $width = (exists $params{'-width'}
104 ? delete $params{'-width'}
105 : $wximage->GetWidth);
106 my $height = (exists $params{'-height'}
107 ? delete $params{'-height'}
108 : $wximage->GetHeight);
109 $wximage->Resize(Wx::Size->new($width,$height),
110 0,0,0); # fill with black
111 }
112 foreach my $key (keys %params) {
113 if (my $option = $attr_to_option{$key}) {
114 $self->{'-wximage'}->SetOption($option, delete $params{$key});
115 }
116 }
117 %$self = (%$self,
118 %params);
119 ### set leaves: $self
120 }
121  
122 #------------------------------------------------------------------------------
123 # load/save
124 #
125 # $self->{'-wximage'}->LoadFile($filename,Wx::wxBITMAP_TYPE_ANY()) tries all
126 # registered handlers, but doesn't seem to report which one succeeded.
127 #
128 # Wx::Image::GetHandlers not wrapped in wxPerl 0.9901.
129  
130 # Note: must try CUR before ICO to pick up HotSpotX and HotSpotY
131 my @file_formats = (qw(BMP
132 GIF
133 JPEG
134 PCX
135 PNG
136 PNM
137 TIF
138 XPM
139 CUR
140 ICO
141 ANI));
142 my @image_types = map { my $constant = "wxBITMAP_TYPE_$_";
143 my $type = eval "Wx::$constant()";
144 if (! defined $type) {
145 die "Oops, no $constant: $@";
146 }
147 $type } @file_formats;
148 ### @image_types
149  
150 sub load {
151 my ($self, $filename) = @_;
152 if (@_ == 1) {
153 $filename = $self->get('-file');
154 } else {
155 $self->set('-file', $filename);
156 }
157 ### load: $filename
158  
159 $filename = "$filename"; # stringize to dispatch to file read
160 open my $fh, '<', $filename
161 or croak "Cannot load $filename: $!";
162  
163 my $wximage = $self->{'-wximage'};
164 foreach my $i (0 .. $#file_formats) {
165 my $file_format = $file_formats[$i];
166 my $type = $image_types[$i];
167 ### $file_format
168 ### $type
169  
170 my $handler = Wx::Image::FindHandlerType($type) || next;
171 ### $handler
172 if ($handler->LoadFile ($wximage, $fh, $self->{'-load_verbose'})) {
173 $self->{'-file_format'} = $file_format;
174 return;
175 }
176 seek $fh,0,0 or croak "Cannot rewind $filename: $!";
177 }
178 croak "Cannot load ",$filename;
179 }
180  
181 # sub load_fh {
182 # my ($self, $fh, $filename) = @_;
183 # ### load_fh()
184 #
185 # $self->{'-wximage'}->LoadFile($fh,Wx::wxBITMAP_TYPE_ANY())
186 # or croak "Cannot read file",
187 # (defined $filename ? (' ',$filename) : ());
188 # }
189  
190 sub _format_to_type {
191 my ($file_format) = @_;
192 my $name = uc($file_format);
193 my $method = "wxBITMAP_TYPE_\U$name";
194 my $type = eval { Wx->$method };
195 unless (defined $type) {
196 croak "Unknown -file_format ", $file_format;
197 }
198 unless (Wx::Image::FindHandlerType($type)) {
199 my $class = "Wx::${name}Handler";
200 unless ($class->isa('Wx::ImageHandler')) {
201 croak "No handler class for -file_format ", $file_format;
202 }
203 Wx::Image::AddHandler($class->new);
204 }
205 return $type;
206 }
207  
208 sub save {
209 my ($self, $filename) = @_;
210 ### Image-Base-Pixbuf save(): @_
211 if (@_ == 2) {
212 $self->set('-file', $filename);
213 } else {
214 $filename = $self->get('-file');
215 }
216 ### $filename
217  
218 my $file_format = $self->get('-file_format')
219 || croak "-file_format not set";
220 $file_format = lc($file_format);
221 if ($file_format eq 'jpg') {
222 $file_format = 'jpeg';
223 }
224  
225 my $type = _format_to_type($file_format);
226 unless ($self->{'-wximage'}->SaveFile($filename, $type)) {
227 croak "Error saving";
228 }
229  
230 # Circa WxPerl 0.9932 and Wx 3.0.4, Wx::BMPHandler and Wx::CURHandler
231 # called directly to a Perl fh like below results in bad file contents.
232 # It looks like a lot of extra 00 bytes in between the intended, so maybe
233 # some char coding or something. Going through Wx::Image::SaveFile is ok.
234 #
235 # my $handler = Wx::Image::FindHandlerMime("image/$file_format")
236 # || Wx::Image::FindHandlerMime("image/x-$file_format")
237 # || do {
238 # my $class = "Wx::\U$file_format\EHandler";
239 # $class->isa('Wx::ImageHandler') && $class->new
240 # }
241 # || croak "Unrecognised -file_format ",$file_format;
242 #
243 # open my $fh, '>', $filename
244 # or croak "Cannot save $filename: $!";
245 # $handler->SaveFile(, $fh)
246 # or croak "Error saving $filename (handler ",$handler->GetName,")";
247 # close $fh
248 # or croak "Error saving $filename: $!";
249 }
250  
251 sub save_fh {
252 my ($self, $fh, $filename) = @_;
253 ### Image-Base-Pixbuf save(): @_
254  
255 my $file_format = $self->get('-file_format');
256 # if (! defined $file_format) {
257 # $file_format = _filename_to_format($filename);
258 # if (! defined $file_format) {
259 # croak 'No -file_format set';
260 # }
261 # }
262  
263 $self->{'-wximage'}->SaveFile($fh, "image/$file_format")
264 or croak "Cannot save file",
265 (defined $filename ? (' ',$filename) : ());
266 }
267  
268 #------------------------------------------------------------------------------
269 # drawing
270  
271 # Attempting to SetRGB an X or Y coordinate out of range provokes assert
272 # failures from Wx, which WxPerl will display under
273 # "ENABLE_DEFAULT_ASSERT_HANDLER" runtime, or an application can see through
274 # OnAssertFailure.
275 #
276 # Not sure if would prefer to quietly truncate drawing to the valid
277 # width/height. Or maybe an application would like to see that it's going
278 # out of bounds ...
279  
280  
281 sub xy {
282 my ($self, $x, $y, $colour) = @_;
283 my $wximage = $self->{'-wximage'};
284  
285 if (@_ >= 4) {
286 if (lc($colour) eq 'none') {
287 # wx 2.8.12 docs say InitAlpha is an error if already have alpha data.
288 # That doesn't seem to be so, but check just in case.
289 unless ($wximage->HasAlpha) { $wximage->InitAlpha; }
290  
291 # SetAlphaXY() for dubious overload dispatch in wxPerl 0.9901
292 $wximage->SetAlphaXY($x,$y,0);
293  
294 } else {
295 $wximage->SetRGB($x,$y, $self->colour_to_rgb($colour));
296 }
297 } else {
298 if ($x < 0 || $y < 0
299 || $x >= $wximage->GetWidth || $y >= $wximage->GetHeight) {
300 return undef;
301 }
302 if ($wximage->IsTransparent($x,$y,128)) {
303 return 'None';
304 }
305 return sprintf('#%02X%02X%02X',
306 $wximage->GetRed($x,$y),
307 $wximage->GetBlue($x,$y),
308 $wximage->GetGreen($x,$y));
309 }
310 }
311  
312 sub line {
313 my $self = shift;
314 my ($x1,$y1, $x2,$y2, $colour) = @_;
315 ### Image-Base-Wx-Image line() ...
316  
317 if (lc($colour) ne 'none'
318 && ($x1 == $x2 || $y1 == $y2)) {
319 # horizontal or vertical as rectangle
320 if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); }
321 if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); }
322 $self->{'-wximage'}->SetRGB(Wx::Rect->new($x1,$y1, $x2-$x1+1, $y2-$y1+1),
323 $self->colour_to_rgb($colour));
324 } else {
325 $self->SUPER::line(@_);
326 }
327 }
328  
329 sub rectangle {
330 my $self = shift;
331 my ($x1, $y1, $x2, $y2, $colour, $fill) = @_;
332 # ### Image-Base-Wx-Image rectangle: "$x1, $y1, $x2, $y2, $colour, $fill"
333  
334 # 2xN, Nx2 or filled with SetRGB(rect)
335 my $w = $x2-$x1;
336 my $h = $y2-$y1;
337 if (lc($colour) ne 'none'
338 && ($fill || ($w <= 1 || $h <= 1))) {
339 $self->{'-wximage'}->SetRGB(Wx::Rect->new($x1,$y1, $w+1, $h+1),
340 $self->colour_to_rgb($colour));
341 } else {
342 $self->SUPER::rectangle(@_);
343 }
344 }
345  
346 sub ellipse {
347 my $self = shift;
348 my ($x1, $y1, $x2, $y2, $colour, $fill) = @_;
349 ### Image-Base-Wx-Image ellipse: "$x1, $y1, $x2, $y2, $colour, ".($fill||0)
350  
351 my $w = $x2-$x1;
352 my $h = $y2-$y1;
353 if (lc($colour) ne 'none'
354 && ($fill || ($w == 0 || $h == 0))) {
355 # horizontal or vertical line as rectangle
356 $self->{'-wximage'}->SetRGB(Wx::Rect->new($x1,$y1, $w+1, $h+1),
357 $self->colour_to_rgb($colour));
358 } else {
359 $self->SUPER::rectangle(@_);
360 }
361 }
362  
363 #------------------------------------------------------------------------------
364 # colours
365  
366 my %colour_obj = (set => [0xFF,0xFF,0xFF],
367 clear => [0,0,0]);
368 sub colour_to_rgb {
369 my ($self, $colour) = @_;
370  
371 # builtin names
372 if (my $aref = $colour_obj{lc($colour)}) {
373 return @$aref;
374 }
375  
376 # 1 to 4 digit hex
377 if ($colour =~ /^#(([0-9A-F]{3}){1,4})$/i) {
378 my $len = length($1)/3; # of each group, so 1,2,3 or 4
379 return (map {hex(substr($_ x 2, 0, 2))} # first 2 chars of replicated
380 substr ($colour, 1, $len), # groups of $len each
381 substr ($colour, 1+$len, $len),
382 substr ($colour, -$len));
383 }
384  
385 my $colour_obj = Wx::ColourDatabase::Find($colour);
386 if (! $colour_obj->IsOk) {
387 croak "ColourDatabase unrecognised name: ",$colour;
388 }
389 return ($colour_obj->Red, $colour_obj->Green, $colour_obj->Blue);
390 }
391  
392 1;
393 __END__