Tempest/Imagemagick.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 78 CC 6 SCAR 32.6
CC
6
Cov
0%
render
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 26.0
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 my $colorize_percent = 100 - int(99 / $max_rep);
80 if($colorize_percent > 99) { $colorize_percent = 99; }
81 $plot_file->Colorize('fill' => 'white', 'opacity' => $colorize_percent . '%');
82
83 # paste one plot for each coordinate pair
84 for my $pair (@{$coordinates}) {
85 my $x = ($pair->[0] - $plot_correct[0]);
86 my $y = ($pair->[1] - $plot_correct[1]);
87
88 # for how many times coord pair was repeated
89 for(1..$pair->[2]) {
90 # paste plot, centered on given coords
91 $output_file->Composite('image' => $plot_file, 'compose' => 'Multiply', 'x' => $x, 'y' => $y );
92 }
93 }
94
95 # destroy plot file, as we don't need it anymore
96 undef $plot_file;
97
98 # apply color lookup table with clut method if available
99 if($output_file->can('Clut')) {
100 my $color_file = Image::Magick->new;
101 $color_file->Read($parent->get_color_file());
102 $output_file->Clut('image' => $color_file);
103 }
104 # for older IM versions (anything before 6.3.5-7), use fx operator
105 else {
106 $output_file->Read($parent->get_color_file());
107 my $fx = $output_file->Fx('expression' => 'v.p{0,u*v.h}');
108 undef $output_file;
109 $output_file = $fx;
110 }
111
112 # overlay heatmap over source image
113 if($parent->get_overlay()) {
114 $input_file->Composite('image' => $output_file, 'compose' => 'Blend', 'blend' => $parent->get_opacity() . '%');
115 undef $output_file;
116 $output_file = $input_file;
117 }
118
119 # write destination image
120 $output_file->Write($parent->get_output_file());
121
122 # return true if successful
123 return 1;
124 }
125  
126 1;