Tempest/Gd.pm untested

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 169 CC 10 SCAR 26.1
CC
10
Cov
0%
render
30.0
write_image
12.0
composite
12.0
CRAP 13.6
1 =head1 NAME
2  
3 Tempest::Gd - GD adapter for Tempest heat-map generator
4  
5 =head1 DESCRIPTION
6  
7 Implements L<Tempest|Tempest> image operations using the L<GD|GD> module.
8  
9 =head1 COPYRIGHT & LICENSE
10  
11 Copyright 2009 Evan Kaufman, all rights reserved.
12  
13 This program is released under the MIT license.
14  
15 L<http://www.opensource.org/licenses/mit-license.php>
16  
17 =head1 ADDITIONAL LINKS
18  
19 =over
20  
21 =item LibGD:
22  
23 L<http://www.libgd.org/>
24  
25 =back
26  
27 =cut
28  
29 package Tempest::Gd;
30  
31 use strict;
32 use warnings;
33  
34 use Carp;
35 use GD;
36  
37 sub render {
38  
39 my $parent = shift;
40
41 # create truecolor images by default
42 GD::Image->trueColor(1);
43
44 # load source image (in order to get dimensions)
45 my $input_file = GD::Image->new($parent->get_input_file());
46
47 # create object for destination image
48 my $output_file = GD::Image->newTrueColor($input_file->width, $input_file->height);
49 my $white = $output_file->colorAllocate(255,255,255);
50 $output_file->fill(0, 0, $white);
51
52 # do any necessary preprocessing & transformation of the coordinates
53 my $coordinates = $parent->get_coordinates();
54 my $max_rep = 0;
55 my %normal;
56 for my $pair (@{$coordinates}) {
57 # normalize repeated coordinate pairs
58 my $pair_key = $pair->[0] . 'x' . $pair->[1];
59 if(exists $normal{$pair_key}) {
60 $normal{$pair_key}->[2]++;
61 }
62 else {
63 $normal{$pair_key} = [$pair->[0], $pair->[1], 1];
64 }
65 # get the max repitition count of any single coord set in the data
66 if($normal{$pair_key}->[2] > $max_rep) {
67 $max_rep = $normal{$pair_key}->[2];
68 }
69 }
70 $coordinates = [ values(%normal) ];
71 undef %normal;
72
73
74 # load plot image (presumably greyscale)
75 my $plot_file = GD::Image->new($parent->get_plot_file());
76
77 # calculate coord correction based on plot image size
78 my @plot_correct = ( ($plot_file->width / 2), ($plot_file->height / 2) );
79
80 # colorize opacity for how many times at most a point will be repeated
81 colorize($plot_file, (99 / $max_rep) );
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 composite($output_file, $plot_file, $x, $y);
92 }
93 }
94
95 # destroy plot file, as we don't need it anymore
96 undef $plot_file;
97
98 # open color lookup table
99 my $color_file = GD::Image->new($parent->get_color_file());
100
101 # apply color lookup table
102 my %cached_colors;
103 for my $x (0..$output_file->width) {
104 for my $y (0..$output_file->height) {
105 # calculate color lookup location
106 my $pixel_red = ($output_file->rgb( $output_file->getPixel($x, $y) ))[0];
107
108 # cache colors as we look them up
109 my @lookup_color;
110 if(exists($cached_colors{$pixel_red})) {
111 @lookup_color = @{ $cached_colors{$pixel_red} };
112 }
113 else {
114 my $color_offset = ($pixel_red / 255) * ($color_file->height - 1);
115 @lookup_color = $color_file->rgb( $color_file->getPixel(0, $color_offset) );
116 $cached_colors{$pixel_red} = \@lookup_color;
117 }
118
119 # allocate and set new color from lookup table
120 my $new_color = $output_file->colorAllocate(@lookup_color);
121 $output_file->setPixel($x, $y, $new_color);
122 $output_file->colorDeallocate($new_color);
123 }
124 }
125
126 # overlay heatmap over source image
127 if($parent->get_overlay()) {
128 $input_file->copyMerge($output_file, 0, 0, 0, 0, $output_file->width, $output_file->height, $parent->get_opacity());
129 undef $output_file;
130 $output_file = $input_file;
131 }
132
133 # write to output file
134 write_image($output_file, $parent->get_output_file());
135
136 return 1;
137 }
138  
139 sub write_image {
140 my $image_file = shift;
141 my $filename = shift;
142
143 my $filetype;
144
145 if(($filetype) = $filename =~ m/\.(png|gif|jpe?g|gd2?|wbmp)$/i) {
146 $filetype = lc($filetype);
147 if($filetype eq 'jpe' || $filetype eq 'jpeg') {
148 $filetype = 'jpg';
149 }
150
151 my $image_data = $image_file->$filetype;
152 open(PNGFILE, '>', $filename) or croak("Failed to write output image: $!");
153 binmode PNGFILE;
154 print PNGFILE $image_data;
155 close(PNGFILE);
156 return;
157 }
158 croak("Failed to detect a supported file extension in file '$filename'");
159 }
160  
161 sub composite {
162 my($source_image, $composite_image, $x, $y) = @_;
163
164 # set aside space to cache colors
165 my %cached_colors;
166
167 # for each pixel from x to x+composite_width
168 my $composite_x = $composite_image->width - 1;
169 my $composite_y = $composite_image->height - 1;
170 foreach my $x_offset (0..$composite_x) {
171 foreach my $y_offset (0..$composite_y) {
172 my $source_x = ($x + $x_offset);
173 my $source_y = ($y + $y_offset);
174
175 # skip negative coordinates
176 if($source_x < 0 || $source_y < 0) { next; }
177
178 # get colors to composite together
179 my @source_color = $source_image->rgb( $source_image->getPixel($source_x, $source_y) );
180 my @composite_color = $composite_image->rgb( $composite_image->getPixel($x_offset, $y_offset) );
181
182 # multiply colors together, caching them within the same composite call
183 my $cache_key = join(',', @source_color) . 'x' . join(',', @composite_color);
184 my @multiplied;
185 if(exists($cached_colors{$cache_key})) {
186 @multiplied = @{ $cached_colors{$cache_key} };
187 }
188 else {
189 @multiplied = multiply(\@source_color, \@composite_color);
190 $cached_colors{$cache_key} = \@multiplied;
191 }
192
193 # allocate and set new colors after multiplication
194 my $color = $source_image->colorAllocate(@multiplied);
195 $source_image->setPixel($source_x, $source_y, $color);
196 $source_image->colorDeallocate($color);
197 }
198 }
199 }
200  
201 sub colorize {
202 my $image_file = shift;
203 my $opacity = shift;
204
205 # set aside space to cache colors
206 my %cached_colors;
207
208 # reduce percentage to fraction
209 $opacity = (100 - $opacity) / 100;
210
211 # get dimensions once
212 my $image_width = ($image_file->width - 1);
213 my $image_height = ($image_file->height - 1);
214
215 # iterate through each pixel in given image
216 for my $x (0..$image_width) {
217 for my $y (0..$image_height) {
218 # get color to colorize
219 my @color = $image_file->rgb( $image_file->getPixel($x, $y) );
220
221 # cache colors as we colorize them
222 my $cache_key = join(',', @color);
223 if(exists($cached_colors{$cache_key})) {
224 @color = @{ $cached_colors{$cache_key} };
225 }
226 else {
227 for my $i (0..2) {
228 $color[$i] = $color[$i] + ((255 - $color[$i]) * $opacity);
229 }
230 $cached_colors{$cache_key} = \@color;
231 }
232
233 # allocate and set new colors after colorization
234 my $color = $image_file->colorAllocate(@color);
235 $image_file->setPixel($x, $y, $color);
236 $image_file->colorDeallocate($color);
237 }
238 }
239 }
240  
241 sub multiply {
242 my $color1 = shift;
243 my $color2 = shift;
244 my @product;
245
246 for my $i (0..2) {
247 $product[$i] = (($color1->[$i] / 255) * ($color2->[$i] / 255)) * 255;
248 }
249
250 return @product;
251 }
252  
253 1;