Tempest.pm

filter: Statementstmt 45.9% 74 / 161 Branchbran 14.7% 5 / 34 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 45.7% 16 / 35 Podpod 23.8% 5 / 21 totaltotal 38.1% 105 / 275 CC 28 SCAR 19.9
CC
28
Cov
40%
set_coordinates
30.0
new
12.5
set_output_file
12.0
CRAP 7.3
« Tempest/Gd.pm ↑ Summary
1 package Tempest;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Tempest - Flexible temperature-map/heat-map generator
13  
14 =head1 DESCRIPTION
15  
16 Tempest is implemented natively in multiple programming languages, including
17 Perl 5. This implementation is "pure" Perl, meaning that there is no
18 C or XS code to configure or compile. Installation entails the steps for any
19 modern CPAN module:
20  
21 perl Makefile.PL
22 make
23 make test
24 make install
25  
26 =head1 VERSION
27  
28 Version 2009.10.31_1 (beta release)
29  
30 Tempest API Version 2009.07.15
31  
32 =cut
33  
34 7 our $VERSION = qv('2009.10.31_1'); # using CPAN alpha versioning to denote non-stable release
35 7 our $API_VERSION = qv('2009.07.15');
36  
37 =head1 SYNOPSIS
38  
39 This module exposes the Tempest API through class instantiation:
40  
41 use Tempest;
42
43 # Create new instance
44 $heatmap = new Tempest(
45 'input_file' => 'screenshot.png',
46 'output_file' => 'heatmap.png',
47 'coordinates' => [ [0,10], [2,14], [2,14] ],
48 ));
49
50 # Configure as needed
51 $heatmap->set_image_lib( Tempest::LIB_GD );
52
53 # Generate and write heatmap image
54 $heatmap->render();
55  
56 =head1 CONSTANTS
57  
58 These constants can be assigned to the C<image_lib> property to specify use
59 of a given image library for all image manipulations.
60  
61 =head3 C<LIB_MAGICK>
62  
63 For forcing use of L<Image::Magick|Image::Magick> support.
64  
65 =head3 C<LIB_GMAGICK>
66  
67 For forcing use of L<Graphics::Magick|Graphics::Magick> support.
68  
69 =head3 C<LIB_GD>
70  
71 For forcing use of L<GD|GD> support.
72  
73 =cut
74  
75 7 ▶use constant LIB_MAGICK => 'Image::Magick';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 7 ▶use constant LIB_GMAGICK => 'Graphics::Magick';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 7 ▶use constant LIB_GD => 'GD';
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 =head1 PROPERTIES
80  
81 =head2 Required Properties
82  
83 =head3 C<input_file>
84  
85 The generated heatmap will share the same dimensions as this image,
86 and - if indicated - will be overlaid onto this image with a given
87 opacity.
88  
89 =cut
90  
91 7 my %input_file;
92  
93 =head3 C<output_file>
94  
95 The generated heatmap will be written to this path, replacing any
96 existing file without warning.
97  
98 =cut
99  
100 my %output_file;
101  
102 =head3 C<coordinates>
103  
104 The contained x,y coordinates will mark the center of all plotted data
105 points in the heatmap. Coordinates can - and in many cases are
106 expected to - be repeated.
107  
108 =cut
109  
110 7 my %coordinates;
111  
112 =head2 Optional Properties
113  
114 =head3 C<plot_file>
115  
116 This image, expected to be greyscale, is used to plot data points for
117 each of the given coordinates. Defaults to a bundled image, if none
118 is provided.
119  
120 =cut
121  
122 7 my %plot_file;
123  
124 =head3 C<color_file>
125  
126 This image, expected to be a true color vertical gradient, is used as a
127 color lookup table and is applied to the generated heatmap. Defaults
128 to a bundled image, if none is provided.
129  
130 =cut
131  
132 7 my %color_file;
133  
134 =head3 C<overlay>
135  
136 If true, the heatmap is overlaid onto the input image with a given
137 opacity before being written to the filesystem. Defaults to B<True>.
138  
139 =cut
140  
141 7 my %overlay;
142  
143 =head3 C<opacity>
144  
145 Indicates with what percentage of opaqueness to overlay the heatmap
146 onto the input image. If 0, the heatmap will not be visible; if 100,
147 the input image will not be visible. Defaults to b<50>.
148  
149 =cut
150  
151 7 my %opacity;
152  
153 =head3 C<image_lib>
154  
155 Indicates which supported image manipulation library should be used
156 for rendering operations. Defaults to the first available from the
157 following:
158  
159 =cut
160  
161 7 my %image_lib;
162  
163 7 my @_required = ('input_file', 'output_file', 'coordinates');
164 7 my @_optional = ('plot_file', 'color_file', 'overlay', 'opacity', 'image_lib');
165  
166 =head1 METHODS
167  
168 =head2 C<new>
169  
170 Class constructor, accepts a hash of named arguments corresponding to
171 the class' own getter and setter methods.
172  
173 $heatmap = new Tempest(
174 'input_file' => 'screenshot.png',
175 'output_file' => 'heatmap.png',
176 'coordinates' => [ [0,10], [2,14], [2,14] ],
177 );
178  
179 =cut
180  
181 sub new {
182 1 ▶ my $class = shift;
Subroutine new: called CC=6 SCAR=25.3
Pod documented
183
184 1 ▶ croak('Bad parameter list, should be a hash') if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
185 1 my %params = @_;
186
187 # inside-out object model
188 1 my $self = bless \(my $dummy), $class;
189
190 # set defaults
191 1 $plot_file{$self} = dirname(__FILE__) . '/Tempest/data/plot.png';
192 1 $color_file{$self} = dirname(__FILE__) . '/Tempest/data/clut.png';
193 1 $overlay{$self} = 1;
194 1 $opacity{$self} = 50;
195 1 $image_lib{$self} = $self->_calc_image_lib();
196
197 # for all required parameters..
198 0 for my $param_name (@_required) {
199 # ..ensure they were provided
200 0 ▶ if(!exists $params{$param_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{$param_name}) 0 0 0
201 0 croak("Missing required parameter '$param_name'");
202 }
203
204 # ..and call each of their setters
205 0 eval('$self->set_' . $param_name . '($params{$param_name})');
206 }
207
208 # for all optional parameters..
209 0 for my $param_name (@_optional) {
210 # ..if they were provided..
211 0 ▶ if(exists $params{$param_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{$param_name}) 0 0 0
212 # ..call their setters
213 0 eval('$self->set_' . $param_name . '($params{$param_name})');
214 }
215 }
216
217 0 return $self;
218 }
219  
220 =head2 C<render>
221  
222 Initiates processing of provided arguments, and writes a heatmap image
223 to the filesystem. Returns B<True> on success.
224  
225 die('Rendering failed') if ! $heatmap->render();
226  
227 =cut
228  
229 sub render {
230 0 ▶ my $self = shift;
Subroutine render: not called CC=2 SCAR=17.9
Pod documented
231
232 0 my $lib_name = ucfirst(lc($image_lib{$self}));
233 0 $lib_name =~ s/\W//g;
234
235 0 my $result = eval('require Tempest::'.$lib_name.'; return Tempest::'.$lib_name.'::render($self);');
236 0 ▶ croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
237 0 return $result;
238 }
239  
240 =head2 C<version>
241  
242 Returns the version number of the current release.
243  
244 die('Outdated') if $heatmap->version() lt '2009.06.15';
245  
246 =cut
247  
248 sub version {
249 0 ▶ return $VERSION;
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
250 }
251  
252 =head2 C<api_version>
253  
254 Returns the version number of the currently supported Tempest API.
255  
256 die('API is outdated') if $heatmap->api_version() lt '2009.06.15';
257  
258 =cut
259  
260 sub api_version {
261 0 ▶ return $API_VERSION;
Subroutine api_version: not called CC=1 SCAR=6.9
Pod documented
262 }
263  
264 =head2 Setters
265  
266 Each setter method assigns a new value to its respective property.
267 The setters also return the current class instance, so they can be 'chained'.
268  
269 For example, if we wanted to change the C<image_lib> used for image processing,
270 and immediately render the resulting heatmap:
271  
272 # render heatmap with Image::Magick support
273 $heatmap ->set_image_lib( Tempest::LIB_MAGICK ) ->render();
274  
275 =head2 Getters
276  
277 Each getter method returns the current value of its respective property.
278  
279 For example, if we wanted to retrieve the C<coordinates> to be rendered and
280 immediately output them with the L<Data::Dumper|Data::Dumper> module:
281  
282 use Data::Dumper;
283 print Dumper( $heatmap->get_coordinates() );
284  
285 =cut
286  
287 sub set_input_file {
288 0 ▶ my $self = shift;
Subroutine set_input_file: not called CC=2 SCAR=17.9
Pod undocumented
289 0 my $input_file = shift;
290
291 0 ▶ if(-r $input_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $input_file) { } 0 0 0
292 0 $input_file{$self} = $input_file;
293 }
294 else {
295 0 croak("Image '$input_file' is not readable");
296 }
297
298 0 return $self;
299 }
300  
301 sub get_input_file {
302 0 ▶ my $self = shift;
Subroutine get_input_file: not called CC=1 SCAR=6.9
Pod undocumented
303 0 return $input_file{$self};
304 }
305  
306  
307 sub set_output_file {
308 0 ▶ my $self = shift;
Subroutine set_output_file: not called CC=3 SCAR=24.8
Pod undocumented
309 0 my $output_file = shift;
310
311 0 ▶ if((! -e $output_file) || -w $output_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $output_file or -w $output_file) { } 0 0 0
Condition0%0/3
not -e $output_file or -w $output_file
l!l&&r!l&&!r
000
MC/DC0%0/2
not -e $output_file or -w $output_file
not -e $output_file -w $output_file
Truth table0%0/3
not(A) or B
A= not -e $output_fileB= -w $output_file
ABresult
1X1
011
000
312 0 $output_file{$self} = $output_file;
313 }
314 else {
315 0 croak("Image '$output_file' is not writable");
316 }
317
318 0 return $self;
319 }
320  
321 sub get_output_file {
322 0 ▶ my $self = shift;
Subroutine get_output_file: not called CC=1 SCAR=6.9
Pod undocumented
323 0 return $output_file{$self};
324 }
325  
326  
327 sub set_coordinates {
328 0 ▶ my $self = shift;
Subroutine set_coordinates: not called CC=5 SCAR=34.0
Pod undocumented
329 0 my $coordinates = shift;
330
331 # verify an array of 2-element arrays
332 0 ▶ if(ref($coordinates) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $coordinates ne "ARRAY") 0 0 0
333 0 croak('Bad coordinates: not an array reference');
334 }
335
336 0 for my $pair (@{$coordinates}) {
337 0 ▶ if(ref($pair) ne 'ARRAY' || scalar(@{$pair}) != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pair ne "ARRAY" or scalar @{$pair;} != 2) 0 0 0
Condition0%0/3
ref $pair ne "ARRAY" or scalar @{$pair;} != 2
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $pair ne "ARRAY" or scalar @{$pair;} != 2
ref $pair ne "ARRAY" scalar @{$pair;} != 2
Truth table0%0/3
A or B
A= ref $pair ne "ARRAY"B= scalar @{$pair;} != 2
ABresult
1X1
011
000
338 0 croak('Bad coordinate pair: ' . join(',', @{$pair}));
339 }
340 }
341
342 0 $coordinates{$self} = $coordinates;
343 0 return $self;
344 }
345  
346 sub get_coordinates {
347 0 ▶ my $self = shift;
Subroutine get_coordinates: not called CC=1 SCAR=6.9
Pod undocumented
348 0 return $coordinates{$self};
349 }
350  
351  
352 sub set_plot_file {
353 0 ▶ my $self = shift;
Subroutine set_plot_file: not called CC=2 SCAR=17.9
Pod undocumented
354 0 my $plot_file = shift;
355
356 0 ▶ if(-r $plot_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $plot_file) { } 0 0 0
357 0 $plot_file{$self} = $plot_file;
358 }
359 else {
360 0 croak("Image '$plot_file' is not readable");
361 }
362
363 0 return $self;
364 }
365  
366 sub get_plot_file {
367 0 ▶ my $self = shift;
Subroutine get_plot_file: not called CC=1 SCAR=6.9
Pod undocumented
368 0 return $plot_file{$self};
369 }
370  
371  
372 sub set_color_file {
373 0 ▶ my $self = shift;
Subroutine set_color_file: not called CC=2 SCAR=17.9
Pod undocumented
374 0 my $color_file = shift;
375
376 0 ▶ if(-r $color_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $color_file) { } 0 0 0
377 0 $color_file{$self} = $color_file;
378 }
379 else {
380 0 croak("Image '$color_file' is not readable");
381 }
382
383 0 return $self;
384 }
385  
386 sub get_color_file {
387 0 ▶ my $self = shift;
Subroutine get_color_file: not called CC=1 SCAR=6.9
Pod undocumented
388 0 return $color_file{$self};
389 }
390  
391  
392 sub set_overlay {
393 0 ▶ my $self = shift;
Subroutine set_overlay: not called CC=2 SCAR=17.9
Pod undocumented
394 0 my $overlay = shift;
395
396 0 ▶ $overlay{$self} = $overlay ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$overlay ? : 0 0 0
397 0 return $self;
398 }
399  
400 sub get_overlay {
401 0 ▶ my $self = shift;
Subroutine get_overlay: not called CC=1 SCAR=6.9
Pod undocumented
402 0 return $overlay{$self};
403 }
404  
405  
406 sub set_opacity {
407 0 ▶ my $self = shift;
Subroutine set_opacity: not called CC=3 SCAR=24.8
Pod undocumented
408 0 my $opacity = shift;
409
410 0 ▶ if($opacity >=0 && $opacity <= 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opacity >= 0 and $opacity <= 100) { } 0 0 0
Condition0%0/3
$opacity >= 0 and $opacity <= 100
!ll&&!rl&&r
000
MC/DC0%0/2
$opacity >= 0 and $opacity <= 100
$opacity >= 0 $opacity <= 100
Truth table0%0/3
A and B
A= $opacity >= 0B= $opacity <= 100
ABresult
0X0
100
111
411 0 $opacity{$self} = $opacity;
412 }
413 else {
414 0 croak("'$opacity' is not a valid percentage (integer from 0 to 100)");
415 }
416
417 0 return $self;
418 }
419  
420 sub get_opacity {
421 0 ▶ my $self = shift;
Subroutine get_opacity: not called CC=1 SCAR=6.9
Pod undocumented
422 0 return $opacity{$self};
423 }
424  
425  
426 sub set_image_lib {
427 0 ▶ my $self = shift;
Subroutine set_image_lib: not called CC=2 SCAR=17.9
Pod undocumented
428 0 my $image_lib = shift;
429
430 0 ▶ if($self->has_image_lib($image_lib)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_image_lib($image_lib)) { } 0 0 0
431 0 $image_lib{$self} = $image_lib;
432 }
433 else {
434 0 croak("Image library '$image_lib' could not be found");
435 }
436
437 0 return $self;
438 }
439  
440 sub get_image_lib {
441 0 ▶ my $self = shift;
Subroutine get_image_lib: not called CC=1 SCAR=6.9
Pod undocumented
442 0 return $image_lib{$self};
443 }
444  
445 =head2 C<has_image_lib>
446  
447 Returns true value if the given image library is available.
448  
449 die('GD is unavailable') if ! $heatmap->has_image_lib(Tempest::LIB_GD);
450  
451 =cut
452  
453 sub has_image_lib {
454 3 ▶ my $self = shift;
Subroutine has_image_lib: called CC=6 SCAR=18.3
Pod documented
455 3 my $image_lib = shift;
456
457 # work as instance method or static method
458 3 ▶ if(ref($self) ne 'Tempest') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne "Tempest") 3 0 3
459 3 $image_lib = $self;
460 3 undef $self;
461 }
462
463 3 ▶ if($image_lib eq LIB_MAGICK || $image_lib eq LIB_GMAGICK || $image_lib eq LIB_GD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($image_lib eq 'Image::Magick' or $image_lib eq 'Graphics::Magick' or $image_lib eq 'GD') { } 3 0 3
Condition83%5/6
$image_lib eq "Image::Magick" or $image_lib eq "Graphics::Magick" or $image_lib eq "GD"
l!l&&r!l&&!r
210
$image_lib eq "Image::Magick" or $image_lib eq "Graphics::Magick"
l!l&&r!l&&!r
111
MC/DC0%0/3
$image_lib eq "Image::Magick" or $image_lib eq "Graphics::Magick" or $image_lib eq "GD"
$image_lib eq "Image::Magick" $image_lib eq "Graphics::Magick" $image_lib eq "GD"
Truth table75%3/4
A or B or C
A= $image_lib eq "Image::Magick"B= $image_lib eq "Graphics::Magick"C= $image_lib eq "GD"
ABCresult
1XX1
01X1
0011
0000
464 3 ▶ eval("no warnings 'all'; require $image_lib;");
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
465 3 ▶ if(!$@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 0 3 3
466 0 return 1;
467 }
468 else {
469 3 return 0;
470 }
471 }
472 else {
473 0 croak("Image library '$image_lib' is not supported");
474 }
475 }
476  
477 # Determine optimal supported (and available) image library to use
478 # not intended to be public, so no need to document it
479 sub _calc_image_lib {
480 1 ▶ for my $image_lib (LIB_MAGICK, LIB_GMAGICK, LIB_GD) {
Subroutine _calc_image_lib: called CC=3 SCAR=12.0
481 3 ▶ if(Tempest::has_image_lib($image_lib)) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_image_lib($image_lib)) 0 3 3
482 0 return $image_lib;
483 }
484 }
485
486 1 croak('No supported image library could be found');
487 }
488  
489 =head2 C<DESTROY>
490  
491 Class destructor, destroys the class instance. Normally not invoked directly.
492  
493 # free up resources
494 $heatmap->DESTROY();
495  
496 =cut
497  
498 sub DESTROY {
499 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
500
501 1 for my $param_name (@_required, @_optional) {
502 {
503 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
504 8 delete ${$param_name}{$self};
505 };
506 }
507 }
508  
509 =head1 COPYRIGHT & LICENSE
510  
511 Copyright 2009 Evan Kaufman, all rights reserved.
512  
513 This program is released under the MIT license.
514  
515 L<http://www.opensource.org/licenses/mit-license.php>
516  
517 =head1 ADDITIONAL LINKS
518  
519 =over
520  
521 =item Tempest on Google Code:
522  
523 L<http://code.google.com/p/image-tempest/>
524  
525 =back
526  
527 =cut
528  
529 7 1;
« Tempest/Gd.pm ↑ Summary