Bitmap.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 165 CC 18 SCAR 34.1
CC
18
Cov
0%
new
72.0
set
20.0
save
20.0
CRAP 30.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::Bitmap;
20 use 5.008;
21 use strict;
22 use Carp;
23 use Wx;
24 our $VERSION = 6;
25  
26 use Image::Base::Wx::DC;
27 our @ISA = ('Image::Base::Wx::DC');
28  
29 # uncomment this to run the ### lines
30 # use Smart::Comments;
31  
32  
33 sub new {
34 my ($class_or_self, %params) = @_;
35 ### Image-Base-Wx-Bitmap new: @_
36  
37 my $filename = delete $params{'-file'};
38 my $wxbitmap = delete $params{'-wxbitmap'};
39 my $dc = delete $params{'-dc'};
40  
41 my $class;
42 if (ref $class_or_self) {
43 # $obj->new(...) means make a copy, with some extra settings
44 $class = ref $class_or_self;
45 %params = (%$class_or_self, %params);
46 } else {
47 $class = $class_or_self;
48 }
49  
50 if (ref $class_or_self) {
51 # clone wxbitmap if a new one not given in the %params
52 if (! defined $wxbitmap) {
53 ### copy ...
54 # maybe no copy-on-write constructor in 0.9909 ?
55 $wxbitmap = $class_or_self->{'-wxbitmap'};
56 ### $wxbitmap
57 $wxbitmap = $wxbitmap->GetSubBitmap
58 (Wx::Rect->new(0,0, $wxbitmap->GetWidth, $wxbitmap->GetHeight));
59 ### copy: $wxbitmap
60 }
61 } else {
62 if (! $wxbitmap) {
63 ### new bitmap ...
64 my $depth = $params{'-depth'};
65 if (! defined $depth) { $depth = -1; }
66 $wxbitmap = Wx::Bitmap->new
67 (delete $params{'-width'}||1,
68 delete $params{'-height'}||1,
69 $depth);
70 }
71 }
72 if (! defined $dc) {
73 $dc = Wx::MemoryDC->new;
74 $dc->SelectObject($wxbitmap);
75 $dc->IsOk or croak "Oops, MemoryDC not IsOk()";
76 ### new dc: $dc
77 }
78 my $self = $class->SUPER::new(%params,
79 -wxbitmap => $wxbitmap,
80 -dc => $dc);
81 if (defined $filename) {
82 $self->load($filename);
83 }
84 ### $self
85 return $self;
86 }
87  
88 my %attr_to_get_method
89 = (-width => 'GetWidth',
90 -height => 'GetHeight',
91 -depth => 'GetDepth');
92 my %attr_to_option
93 = (
94 # -hotx => Wx::wxbitmap_OPTION_CUR_HOTSPOT_X(),
95 # -hoty => Wx::wxbitmap_OPTION_CUR_HOTSPOT_Y(),
96 # -quality_percent => 'quality',
97 );
98 sub _get {
99 my ($self, $key) = @_;
100  
101 if (my $method = $attr_to_get_method{$key}) {
102 return $self->{'-wxbitmap'}->$method();
103 }
104 if (my $option = $attr_to_option{$key}) {
105 return $self->{'-wxbitmap'}->GetOptionInt($option);
106 }
107 return $self->SUPER::_get($key);
108 }
109  
110 # my %attr_to_set_method
111 # = (-width => 'SetWidth',
112 # -height => 'SetHeight',
113 # -depth => 'SetDepth');
114 sub set {
115 my ($self, %params) = @_;
116 ### Image-Base-Wx-Bitmap set: \%params
117  
118 # -wxbitmap before applying -width,-height
119 if (my $wxbitmap = delete $params{'-wxbitmap'}) {
120 $self->{'-wxbitmap'} = $wxbitmap;
121 }
122 if (exists $params{'-width'} || exists $params{'-height'}) {
123 croak "-width or -height are read-only";
124 # my $wxbitmap = $self->{'-wxbitmap'};
125 # my $width = (exists $params{'-width'}
126 # ? delete $params{'-width'}
127 # : $wxbitmap->GetWidth);
128 # my $height = (exists $params{'-height'}
129 # ? delete $params{'-height'}
130 # : $wxbitmap->GetHeight);
131 # $wxbitmap->Resize(Wx::Size->new($width,$height),
132 # 0,0,0); # fill with black
133 }
134 foreach my $key (keys %params) {
135 if (my $option = $attr_to_option{$key}) {
136 return $self->{'-wxbitmap'}->GetOptionInt($option);
137 }
138 }
139 $self->SUPER::set(%params);
140 ### set leaves: $self
141 }
142  
143 #------------------------------------------------------------------------------
144 # load/save
145  
146 # Note: must try CUR before ICO to pick up HotSpotX and HotSpotY
147 my @file_formats = (qw(BMP
148 GIF
149 JPEG
150 PCX
151 PNG
152 PNM
153 TIF
154 CUR
155 ICO
156 ANI
157 XPM
158 ));
159 my @bitmap_types = map { my $constant = "wxBITMAP_TYPE_$_";
160 my $type = eval "Wx::$constant()";
161 if (! defined $type) {
162 die "Oops, no $constant: $@";
163 }
164 $type } @file_formats;
165 my %file_formats = (map {$file_formats[$_] => $bitmap_types[$_]}
166 0 .. $#file_formats);
167 $file_formats{'JPG'} = $file_formats{'JPEG'};
168 ### @bitmap_types
169  
170 sub load {
171 my ($self, $filename) = @_;
172 if (@_ == 1) {
173 $filename = $self->get('-file');
174 } else {
175 $self->set('-file', $filename);
176 }
177 ### load: $filename
178  
179 # stringize in case perhaps future LoadFile() overloads to a handle too
180 $filename = "$filename";
181  
182 my $wxbitmap = $self->{'-wxbitmap'};
183 foreach my $i (0 .. $#file_formats) {
184 my $file_format = $file_formats[$i];
185 my $type = $bitmap_types[$i];
186 ### $file_format
187 ### $type
188  
189 # my $handler = Wx::Bitmap::FindHandlerType("\U$file_format\EHandler")
190 # || next;
191 # ### $handler
192 # if ($handler->LoadFile ($wxbitmap, $filename)) {
193 # ### loaded ...
194 # ### wxbitmap isok: $wxbitmap->IsOk
195 # $self->{'-file_format'} = $file_format;
196 # return;
197 # }
198  
199 if ($wxbitmap->LoadFile ($filename, $type)) {
200 ### loaded ...
201 ### wxbitmap isok: $wxbitmap->IsOk
202  
203 # As of Wx circa 3.0.4, LoadFile() doesn't seem to restrict itself to
204 # the specified $type, but will load anything it recognises. This
205 # means cannot set -file_format based on what $type works.
206 #
207 # $self->{'-file_format'} = $file_format;
208  
209 return;
210 }
211 }
212  
213 croak "Cannot load ",$filename;
214 }
215  
216 # Would have to copy to a tempfile.
217 # sub load_fh {
218 # my ($self, $fh, $filename) = @_;
219 # ### load_fh()
220 #
221 # $self->{'-wxbitmap'}->LoadFile($fh,Wx::wxBITMAP_TYPE_ANY())
222 # or croak "Cannot read file",
223 # (defined $filename ? (' ',$filename) : ());
224 # }
225  
226 sub save {
227 my ($self, $filename) = @_;
228 if (@_ == 2) {
229 $self->set('-file', $filename);
230 } else {
231 $filename = $self->get('-file');
232 }
233 ### $filename
234  
235 my $file_format = $self->get('-file_format')
236 || croak "-file_format not set";
237 my $type = $file_formats{uc($file_format)};
238 if (! defined $type) {
239 croak "Unrecognised file format ",$self->get('-file_format');
240 }
241 ### $file_format
242 ### $type
243  
244 if ($self->{'-wxbitmap'}->SaveFile($filename,$type)) {
245 return;
246 }
247 croak "Cannot save ",$filename;
248 }
249  
250 # Would have to SaveFile() to a file and then copy to $fh.
251 # sub save_fh {
252 # my ($self, $fh, $filename) = @_;
253 #
254 # my $file_format = $self->get('-file_format');
255 # # if (! defined $file_format) {
256 # # $file_format = _filename_to_format($filename);
257 # # if (! defined $file_format) {
258 # # croak 'No -file_format set';
259 # # }
260 # # }
261 #
262 # $self->{'-wxbitmap'}->SaveFile($fh, "image/$file_format")
263 # or croak "Cannot save file",
264 # (defined $filename ? (' ',$filename) : ());
265 # }
266  
267 #------------------------------------------------------------------------------
268  
269 1;
270 __END__