Tempest/Imagemagick.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 73 CC 5 SCAR 28.7
CC
5
Cov
0%
render
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 17.6
1 =head1 NAME
2  
3 Tempest::Imagemagick - PerlMagick adapter for Tempest heat-map generator
4  
5 =head1 DESCRIPTION
6  
7 Implements L<Tempest|Tempest> image operations using the
8 L<Image::Magick|Image::Magick> module.
9  
10 =head1 COPYRIGHT & LICENSE
11  
12 Copyright 2009 Evan Kaufman, all rights reserved.
13  
14 This program is released under the MIT license.
15  
16 L<http://www.opensource.org/licenses/mit-license.php>
17  
18 =head1 ADDITIONAL LINKS
19  
20 =over
21  
22 =item ImageMagick:
23  
24 L<http://imagemagick.com/>
25  
26 =back
27  
28 =cut
29  
30 package Tempest::Imagemagick;
31  
32 use strict;
33 use warnings;
34  
35 use Carp;
36 use Image::Magick;
37  
38 sub render {
39 my $parent = shift;
40
41 # load source image (in order to get dimensions)
42 my $input_file = Image::Magick->new;
43 $input_file->Read($parent->get_input_file());
44
45 # create object for destination image
46 my $output_file = Image::Magick->new;
47 $output_file->Set( 'size' => join('x', $input_file->Get('width', 'height')) );
48 $output_file->ReadImage('xc:white');
49
50 # do any necessary preprocessing & transformation of the coordinates
51 my $coordinates = $parent->get_coordinates();
52 my $max_rep = 0;
53 my %normal;
54 for my $pair (@{$coordinates}) {
55 # normalize repeated coordinate pairs
56 my $pair_key = $pair->[0] . 'x' . $pair->[1];
57 if(exists $normal{$pair_key}) {
58 $normal{$pair_key}->[2]++;
59 }
60 else {
61 $normal{$pair_key} = [$pair->[0], $pair->[1], 1];
62 }
63 # get the max repitition count of any single coord set in the data
64 if($normal{$pair_key}->[2] > $max_rep) {
65 $max_rep = $normal{$pair_key}->[2];
66 }
67 }
68 $coordinates = [ values(%normal) ];
69 undef %normal;
70
71 # load plot image (presumably greyscale)
72 my $plot_file = Image::Magick->new;
73 $plot_file->Read($parent->get_plot_file());
74
75 # calculate coord correction based on plot image size
76 my @plot_correct = ( ($plot_file->Get('width') / 2), ($plot_file->Get('height') / 2) );
77
78 # colorize opacity for how many times at most a point will be repeated
79 $plot_file->Colorize('fill' => 'white', 'opacity' => (100 - int(99 / $max_rep)) . '%');
80
81 # paste one plot for each coordinate pair
82 for my $pair (@{$coordinates}) {
83 my $x = ($pair->[0] - $plot_correct[0]);
84 my $y = ($pair->[1] - $plot_correct[1]);
85
86 # for how many times coord pair was repeated
87 for(1..$pair->[2]) {
88 # paste plot, centered on given coords
89 $output_file->Composite('image' => $plot_file, 'compose' => 'Multiply', 'x' => $x, 'y' => $y );
90 }
91 }
92
93 # destroy plot file, as we don't need it anymore
94 undef $plot_file;
95
96 # apply color lookup table with clut method if available
97 if($output_file->can('Clut')) {
98 my $color_file = Image::Magick->new;
99 $color_file->Read($parent->get_color_file());
100 $output_file->Clut('image' => $color_file);
101 }
102 # for older IM versions (anything before 6.3.5-7), use fx operator
103 else {
104 $output_file->Read($parent->get_color_file());
105 my $fx = $output_file->Fx('expression' => 'v.p{0,u*v.h}');
106 undef $output_file;
107 $output_file = $fx;
108 }
109
110 # overlay heatmap over source image
111 if($parent->get_overlay()) {
112 $input_file->Composite('image' => $output_file, 'compose' => 'Blend', 'blend' => $parent->get_opacity() . '%');
113 undef $output_file;
114 $output_file = $input_file;
115 }
116
117 # write destination image
118 $output_file->Write($parent->get_output_file());
119
120 # return true if successful
121 return 1;
122 }
123  
124 1;