Pvoice/Bitmap.pm untested

filter: Statementstmt 0.0% 0 / 241 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 414 CC 46 SCAR 56.2
CC
46
Cov
0%
ReadImage
462.0
ReadImageMagick
380.0
DrawCaption
30.0
CRAP 277.1
1 package AAC::Pvoice::Bitmap;
2
3 use strict;
4 use warnings;
5 use Wx qw(:everything);
6 use Wx::Perl::Carp;
7 use Image::Magick;
8 use IO::Scalar;
9 use File::Cache;
10 use File::stat;
11 use File::Temp qw( :POSIX );
12
13 our $VERSION = sprintf("%d.%02d", q$Revision: 1.12 $=~/(\d+)\.(\d+)/);
14
15 use base qw(Wx::Bitmap);
16 our $cache;
17 BEGIN
18 {
19 Wx::InitAllImageHandlers;
20 $cache = File::Cache->new({namespace => 'images'});
21 }
22
23 #----------------------------------------------------------------------
24 sub new
25 {
26 my $class = shift;
27 my ($file, $MAX_X, $MAX_Y, $caption, $background, $blowup, $parent_background) = @_;
28 $caption||='';
29 $parent_background=Wx::Colour->new(220,220,220) if not defined $parent_background;
30 my $config = Wx::ConfigBase::Get;
31 $caption = $config->ReadInt('Caption')?$caption:'';
32 # return ReadImage($file, $MAX_X, $MAX_Y, $caption, $background, $blowup, $parent_background) if $file;
33 return ReadImageMagick($file, $MAX_X, $MAX_Y, $caption, $background, $blowup, $parent_background) if $file;
34 return DrawCaption($MAX_X, $MAX_Y, $caption, $background, $parent_background);
35 }
36
37
38 sub ReadImage
39 {
40 my $file = shift;
41 my ($x, $y, $caption, $background, $blowup, $parent_background) = @_;
42 return DrawCaption($x, $y, '?', $background, $parent_background) unless -r $file;
43 confess "MaxX and MaxY should be positive" if $x < 1 || $y < 1;
44 my $newbmp;
45
46 $caption ||='';
47 $blowup ||=0;
48 $background = $parent_background unless defined $background;
49
50 my $ibg = wxColor2hex($background) if (ref($background) eq 'Wx::Colour');
51 my $pbg = wxColor2hex($parent_background) if (ref($parent_background) eq 'Wx::Colour');
52
53 my $stat = stat($file);
54 my $mtime = $stat->mtime();
55 my $image = $cache->get("$file-$x-$y-$caption-$ibg-$blowup-$pbg-$mtime");
56 if (!$image)
57 {
58 my $capdc = Wx::MemoryDC->new();
59 my $cpt = 10;
60 my ($cfont, $cw, $ch) = (wxNullFont, 0, 0);
61 if ($caption)
62 {
63 do
64 {
65 $cfont = Wx::Font->new( $cpt, # font size
66 wxSWISS, # font family
67 wxNORMAL, # style
68 wxNORMAL, # weight
69 0,
70 'Comic Sans MS', # face name
71 wxFONTENCODING_SYSTEM);
72 ($cw, $ch, undef, undef) = $capdc->GetTextExtent($caption, $cfont);
73 $cpt--;
74 } until ($cw<$x);
75 }
76 my $img = Wx::Image->new($x, $y-$ch);
77 $img->SetOption('quality', 100);
78 my $rc = $img->LoadFile($file, wxBITMAP_TYPE_ANY);
79 return wxNullBitmap if not $rc;
80 my ($w,$h) = ($img->GetWidth, $img->GetHeight);
81 if (($w > $x) || ($h > ($y-$ch)))
82 {
83 my ($newx, $newy) = ($w, $h);
84 if ($w > $x)
85 {
86 my $factor = $w/$x;
87 return wxNullBitmap if not $factor;
88 $newy = int($h/$factor);
89 ($w,$h) = ($x, $newy);
90 }
91 if ($h > ($y-$ch))
92 {
93 my $factor = $h/($y-$ch);
94 return wxNullBitmap if not $factor;
95 ($w, $h) = (int($w/$factor),$y-$ch);
96 }
97 $img = $img->Scale($w, $h);
98 }
99 elsif ($blowup)
100 {
101 # Do we really want to blow up images that are too small??
102 my $factor = $w/$x;
103 return wxNullBitmap if not $factor;
104 my $newy = int($h/$factor);
105 ($w,$h) = ($x, $newy);
106 if ($h > ($y-$ch))
107 {
108 my $factor = $h/($y-$ch);
109 return wxNullBitmap if not $factor;
110 ($w, $h) = (int($w/$factor),$y-$ch);
111 }
112 $img = $img->Scale($w, $h);
113 }
114 my $bmp = Wx::Bitmap->new($img);
115 my $newbmp = Wx::Bitmap->new($x, $y);
116 my $tmpdc = Wx::MemoryDC->new();
117 $tmpdc->SelectObject($newbmp);
118
119 my $bgbr = Wx::Brush->new($parent_background, wxSOLID);
120 $tmpdc->SetBrush($bgbr);
121 $tmpdc->SetBackground($bgbr);
122 $tmpdc->Clear();
123
124 my $bg = $parent_background;
125 if (defined $background)
126 {
127 if (ref($background)=~/ARRAY/)
128 {
129 $bg = Wx::Colour->new(@$background);
130 }
131 else
132 {
133 $bg = $background;
134 }
135 my $br = Wx::Brush->new($bg, wxSOLID);
136 my $pen = Wx::Pen->new($bg, 1, wxSOLID);
137 $tmpdc->SetBrush($br);
138 $tmpdc->SetPen($pen);
139 $tmpdc->DrawRoundedRectangle(1,1,$x-1,$y-1, 10);
140 }
141
142 my $msk = Wx::Mask->new($bmp, Wx::Colour->new(255,255,255));
143 $bmp->SetMask($msk);
144 $tmpdc->DrawBitmap($bmp, int(($x - $bmp->GetWidth())/2), int(($y-$ch-$bmp->GetHeight())/2), 1);
145
146 if ($caption)
147 {
148 $tmpdc->SetTextBackground($bg);
149 $tmpdc->SetTextForeground(wxBLACK);
150 $tmpdc->SetFont($cfont);
151 $tmpdc->DrawText($caption, int(($x-$cw)/2),$y-$ch);
152 }
153 my $tmpfile = File::Temp::tmpnam();
154 $newbmp->SaveFile($tmpfile, wxBITMAP_TYPE_PNG);
155 local $/ = undef;
156 open(my $fh, "<$tmpfile");
157 binmode($fh);
158 my $image = <$fh>;
159 close($fh);
160 $cache->set("$file-$x-$y-$caption-$ibg-$blowup-$pbg-$mtime", $image);
161 }
162
163 my $fh = IO::Scalar->new(\$image);
164
165 my $contenttype = 'image/png';
166 return Wx::Bitmap->new(Wx::Image->newStreamMIME($fh, $contenttype))
167 }
168
169 sub wxColor2hex
170 {
171 my $color = shift;
172 my $red = $color->Red();
173 my $green = $color->Green();
174 my $blue = $color->Blue();
175 return sprintf("#%0x%0x%0x", $red,$green,$blue);
176 }
177
178 sub ReadImageMagick
179 {
180 my $file = shift;
181 my ($x, $y, $caption, $bgcolor, $blowup, $parent_background) = @_;
182 confess "MaxX and MaxY should be positive" if $x < 1 || $y < 1;
183 return DrawCaption($x, $y, '?', $bgcolor, $parent_background) unless -r $file;
184
185 $caption ||='';
186 $blowup ||=0;
187 $bgcolor = $parent_background unless defined $bgcolor;
188
189 my $ibg = wxColor2hex($bgcolor) if (ref($bgcolor) eq 'Wx::Colour');
190 my $pbg = wxColor2hex($parent_background) if (ref($parent_background) eq 'Wx::Colour');
191
192 my $stat = stat($file);
193 my $mtime = $stat->mtime();
194 my $image = $cache->get("$file-$x-$y-$caption-$ibg-$blowup-$pbg-$mtime");
195 if (!$image)
196 {
197 my $radius = 10;
198 my $svg = <<SVG;
199 <svg width="$x" height="$y" viewBox="0 0 $x $y">
200 <rect x="0" y="0" width="$x" height="$y" ry="$radius"
201 style="stroke: none; fill: $ibg;"/>
202 </svg>
203 SVG
204 my $background=Image::Magick->new(magick => 'svg');
205 $background->Set('background' => $pbg);
206 $background->blobtoimage($svg);
207
208 my ($textheight, $textwidth) = (0,0);
209 if ($caption)
210 {
211 my $pt = 20;
212 do {
213 (undef, undef, undef, undef, $textwidth, $textheight, undef) =
214 $background->QueryFontMetrics(text => $caption, font => 'Comic-Sans-MS', pointsize => $pt, gravity => 'South');
215 $pt--;
216 } until ($textwidth < $x) && ($textheight < $y/5);
217 $background->Annotate(text => $caption, font => 'Comic-Sans-MS', pointsize => $pt, gravity => 'South');
218 }
219
220 # Read the actual image
221 my $img = Image::Magick->new;
222
223 my $rc = $img->Read($file);
224 carp "Can't read $file: $rc" if $rc;
225 # wmf files have a white background color by default
226 # if we can't get the matte color for the image, we assume
227 # that white can be used as the transparent color...
228 $img->Transparent(color => 'white') if (!$img->Get('matte') || $file =~ /wmf$/i);
229 my $w = $img->Get('width');
230 my $h = $img->Get('height');
231 my $ch = $textheight;
232 if (($w > $x) || ($h > ($y-$ch)))
233 {
234 my ($newx, $newy) = ($w, $h);
235 if ($w > $x)
236 {
237 my $factor = $w/$x;
238 return wxNullBitmap if not $factor;
239 $newy = int($h/$factor);
240 ($w,$h) = ($x, $newy);
241 }
242 if ($h > ($y-$ch))
243 {
244 my $factor = $h/($y-$ch);
245 return wxNullBitmap if not $factor;
246 ($w, $h) = (int($w/$factor),$y-$ch);
247 }
248 $img->Thumbnail(height => $h, width =>$w );
249 }
250 elsif ($blowup)
251 {
252 # Do we really want to blow up images that are too small??
253 my $factor = $w/$x;
254 return wxNullBitmap if not $factor;
255 my $newy = int($h/$factor);
256 ($w,$h) = ($x, $newy);
257 if ($h > ($y-$ch))
258 {
259 my $factor = $h/($y-$ch);
260 return wxNullBitmap if not $factor;
261 ($w, $h) = (int($w/$factor),$y-$ch);
262 }
263 $img->Resize(height => $h, width =>$w );
264 }
265
266 $img->Border(width => int(($x - $img->Get('width'))/2) - $radius/2,
267 height => int((($y-$textheight) - $img->Get('height'))/2) - $radius/2,
268 fill => $ibg);
269
270 # Call the Composite method of the background image, with the logo image as an argument.
271 $background->Composite(image=>$img,compose=>'over', gravity => 'North');
272 $background->Set(quality=>100);
273 $background->Set(magick => 'png');
274 $image = $background->imagetoblob();
275 $cache->set("$file-$x-$y-$caption-$ibg-$blowup-$pbg-$mtime", $image);
276 undef $background;
277 undef $img;
278 }
279
280 my $fh = IO::Scalar->new(\$image);
281
282 my $contenttype = 'image/png';
283 return Wx::Bitmap->new(Wx::Image->newStreamMIME($fh, $contenttype))
284 }
285
286 END
287 {
288 undef $cache;
289 }
290
291 sub DrawCaption
292 {
293 my ($x, $y, $caption, $background, $parent_background) = @_;
294
295 confess "MaxX and MaxY should be positive" if $x < 1 || $y < 1;
296
297 my $newbmp = Wx::Bitmap->new($x, $y);
298 my $tmpdc = Wx::MemoryDC->new();
299 $tmpdc->SelectObject($newbmp);
300
301 my $bgbr = Wx::Brush->new($parent_background, wxSOLID);
302 $tmpdc->SetBrush($bgbr);
303 $tmpdc->SetBackground($bgbr);
304 $tmpdc->Clear();
305
306 my $bg = $parent_background;
307 if (defined $background)
308 {
309 if (ref($background)=~/ARRAY/)
310 {
311 $bg = Wx::Colour->new(@$background);
312 }
313 else
314 {
315 $bg = $background;
316 }
317 my $br = Wx::Brush->new($bg, wxSOLID);
318 my $pen = Wx::Pen->new($bg, 1, wxSOLID);
319 $tmpdc->SetBrush($br);
320 $tmpdc->SetPen($pen);
321 $tmpdc->DrawRoundedRectangle(1,1,$x-1,$y-1, 10);
322 }
323
324 my $pt = 72;
325 my ($font, $w, $h);
326 do
327 {
328 $font = Wx::Font->new( $pt, # font size
329 wxSWISS, # font family
330 wxNORMAL, # style
331 wxNORMAL, # weight
332 0,
333 'Comic Sans MS'); # face name
334 ($w, $h, undef, undef) = $tmpdc->GetTextExtent($caption, $font);
335 $pt= $pt > 24 ? $pt - 4 : $pt-1;
336 } until (($w<$x) && ($h<$y) || $pt < 5);
337 $tmpdc->SetTextForeground(wxBLACK);
338 $tmpdc->SetTextBackground($bg);
339 $tmpdc->SetFont($font);
340 $tmpdc->DrawText($caption, int(($x-$w)/2), int(($y-$h)/2));
341
342 return $newbmp;
343 }
344
345 1;
346
347 __END__