Tempest/Graphicsmagick.pm untested

filter: Statementstmt 0.0% 0 / 66 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 / 80 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::Graphicsmagick - PerlMagick adapter for Tempest heat-map generator
4  
5 =head1 DESCRIPTION
6  
7 Implements L<Tempest|Tempest> image operations using the
8 L<Graphics::Magick|Graphics::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 GraphicsMagick:
23  
24 L<http://graphicsmagick.org/>
25  
26 =back
27  
28 =cut
29  
30 package Tempest::Graphicsmagick;
31  
32 use strict;
33 use warnings;
34  
35 use Carp;
36 use Graphics::Magick;
37  
38 sub render {
39 my $parent = shift;
40
41 # load source image (in order to get dimensions)
42 my $input_file = Graphics::Magick->new;
43 $input_file->Read($parent->get_input_file());
44
45 # create object for destination image
46 my $output_file = Graphics::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 = Graphics::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  
98 # TBD: use hald-clut mogrification when support is implemented (not as of 1.4 from cvs 2009/10/05?)
99 # get color lookup image
100 my $color_file = Graphics::Magick->new;
101 $color_file->Read($parent->get_color_file());
102 # apply color lookup table
103 my %cached_colors;
104 my @output_size = $output_file->Get('width', 'height');
105 my @color_size = $color_file->Get('width', 'height');
106 for my $x (1..$output_size[0]) {
107 for my $y (1..$output_size[1]) {
108 # calculate color lookup location
109 my $pixel_red = (split(',', $output_file->Get('pixel['.$x.','.$y.']')))[0];
110  
111 # cache colors as we look them up
112 my $lookup_color;
113 if(exists($cached_colors{$pixel_red})) {
114 $lookup_color = $cached_colors{$pixel_red};
115 }
116 else {
117 my $color_offset = ($pixel_red / 255) * ($color_size[1] - 1);
118 $lookup_color = $color_file->Get('pixel[0,'.$color_offset.']');
119 $cached_colors{$pixel_red} = $lookup_color;
120 }
121  
122 # set new color from lookup table
123 $output_file->Set('pixel['.$x.','.$y.']', $lookup_color);
124 }
125 }
126
127 # overlay heatmap over source image
128 if($parent->get_overlay()) {
129 $input_file->Composite('image' => $output_file, 'compose' => 'Over', 'opacity' => 255 * ($parent->get_opacity() / 100) );
130 undef $output_file;
131 $output_file = $input_file;
132 }
133
134 # write destination image
135 $output_file->Write($parent->get_output_file());
136
137 # return true if successful
138 return 1;
139 }
140  
141 1;