Tempest/Gd.pm untested

filter: Statementstmt 0.0% 0 / 132 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 179 CC 11 SCAR 29.0
CC
11
Cov
0%
render
42.0
write_image
12.0
composite
12.0
CRAP 18.2
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 my $colorize_percent = 99 / $max_rep;
82 if($colorize_percent < 1) { $colorize_percent = 1; }
83 colorize($plot_file, $colorize_percent);
84
85 # paste one plot for each coordinate pair
86 for my $pair (@{$coordinates}) {
87 my $x = ($pair->[0] - $plot_correct[0]);
88 my $y = ($pair->[1] - $plot_correct[1]);
89
90 # for how many times coord pair was repeated
91 for (1..$pair->[2]) {
92 # paste plot, centered on given coords
93 composite($output_file, $plot_file, $x, $y);
94 }
95 }
96
97 # destroy plot file, as we don't need it anymore
98 undef $plot_file;
99
100 # open color lookup table
101 my $color_file = GD::Image->new($parent->get_color_file());
102
103 # apply color lookup table
104 my %cached_colors;
105 for my $x (0..$output_file->width) {
106 for my $y (0..$output_file->height) {
107 # calculate color lookup location
108 my $pixel_red = ($output_file->rgb( $output_file->getPixel($x, $y) ))[0];
109
110 # cache colors as we look them up
111 my @lookup_color;
112 if(exists($cached_colors{$pixel_red})) {
113 @lookup_color = @{ $cached_colors{$pixel_red} };
114 }
115 else {
116 my $color_offset = ($pixel_red / 255) * ($color_file->height - 1);
117 @lookup_color = $color_file->rgb( $color_file->getPixel(0, $color_offset) );
118 $cached_colors{$pixel_red} = \@lookup_color;
119 }
120
121 # allocate and set new color from lookup table
122 my $new_color = $output_file->colorAllocate(@lookup_color);
123 $output_file->setPixel($x, $y, $new_color);
124 $output_file->colorDeallocate($new_color);
125 }
126 }
127
128 # overlay heatmap over source image
129 if($parent->get_overlay()) {
130 $input_file->copyMerge($output_file, 0, 0, 0, 0, $output_file->width, $output_file->height, $parent->get_opacity());
131 undef $output_file;
132 $output_file = $input_file;
133 }
134
135 # write to output file
136 write_image($output_file, $parent->get_output_file());
137
138 return 1;
139 }
140  
141 sub write_image {
142 my $image_file = shift;
143 my $filename = shift;
144
145 my $filetype;
146
147 if(($filetype) = $filename =~ m/\.(png|gif|jpe?g|gd2?|wbmp)$/i) {
148 $filetype = lc($filetype);
149 if($filetype eq 'jpe' || $filetype eq 'jpeg') {
150 $filetype = 'jpg';
151 }
152
153 my $image_data = $image_file->$filetype;
154 open(PNGFILE, '>', $filename) or croak("Failed to write output image: $!");
155 binmode PNGFILE;
156 print PNGFILE $image_data;
157 close(PNGFILE);
158 return;
159 }
160 croak("Failed to detect a supported file extension in file '$filename'");
161 }
162  
163 sub composite {
164 my($source_image, $composite_image, $x, $y) = @_;
165
166 # set aside space to cache colors
167 my %cached_colors;
168
169 # for each pixel from x to x+composite_width
170 my $composite_x = $composite_image->width - 1;
171 my $composite_y = $composite_image->height - 1;
172 foreach my $x_offset (0..$composite_x) {
173 foreach my $y_offset (0..$composite_y) {
174 my $source_x = ($x + $x_offset);
175 my $source_y = ($y + $y_offset);
176
177 # skip negative coordinates
178 if($source_x < 0 || $source_y < 0) { next; }
179
180 # get colors to composite together
181 my @source_color = $source_image->rgb( $source_image->getPixel($source_x, $source_y) );
182 my @composite_color = $composite_image->rgb( $composite_image->getPixel($x_offset, $y_offset) );
183
184 # multiply colors together, caching them within the same composite call
185 my $cache_key = join(',', @source_color) . 'x' . join(',', @composite_color);
186 my @multiplied;
187 if(exists($cached_colors{$cache_key})) {
188 @multiplied = @{ $cached_colors{$cache_key} };
189 }
190 else {
191 @multiplied = multiply(\@source_color, \@composite_color);
192 $cached_colors{$cache_key} = \@multiplied;
193 }
194
195 # allocate and set new colors after multiplication
196 my $color = $source_image->colorAllocate(@multiplied);
197 $source_image->setPixel($source_x, $source_y, $color);
198 $source_image->colorDeallocate($color);
199 }
200 }
201 }
202  
203 sub colorize {
204 my $image_file = shift;
205 my $opacity = shift;
206
207 # set aside space to cache colors
208 my %cached_colors;
209
210 # reduce percentage to fraction
211 $opacity = (100 - $opacity) / 100;
212
213 # get dimensions once
214 my $image_width = ($image_file->width - 1);
215 my $image_height = ($image_file->height - 1);
216
217 # iterate through each pixel in given image
218 for my $x (0..$image_width) {
219 for my $y (0..$image_height) {
220 # get color to colorize
221 my @color = $image_file->rgb( $image_file->getPixel($x, $y) );
222
223 # cache colors as we colorize them
224 my $cache_key = join(',', @color);
225 if(exists($cached_colors{$cache_key})) {
226 @color = @{ $cached_colors{$cache_key} };
227 }
228 else {
229 for my $i (0..2) {
230 $color[$i] = $color[$i] + ((255 - $color[$i]) * $opacity);
231 }
232 $cached_colors{$cache_key} = \@color;
233 }
234
235 # allocate and set new colors after colorization
236 my $color = $image_file->colorAllocate(@color);
237 $image_file->setPixel($x, $y, $color);
238 $image_file->colorDeallocate($color);
239 }
240 }
241 }
242  
243 sub multiply {
244 my $color1 = shift;
245 my $color2 = shift;
246 my @product;
247
248 for my $i (0..2) {
249 $product[$i] = (($color1->[$i] / 255) * ($color2->[$i] / 255)) * 255;
250 }
251
252 return @product;
253 }
254  
255 1;