| 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 | 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 | |||
| 100 | # TBD: use hald-clut mogrification when support is implemented (not as of 1.4 from cvs 2009/10/05?) | ||
| 101 | # get color lookup image | ||
| 102 | my $color_file = Graphics::Magick->new; | ||
| 103 | $color_file->Read($parent->get_color_file()); | ||
| 104 | # apply color lookup table | ||
| 105 | my %cached_colors; | ||
| 106 | my @output_size = $output_file->Get('width', 'height'); | ||
| 107 | my @color_size = $color_file->Get('width', 'height'); | ||
| 108 | for my $x (1..$output_size[0]) { | ||
| 109 | for my $y (1..$output_size[1]) { | ||
| 110 | # calculate color lookup location | ||
| 111 | my $pixel_red = (split(',', $output_file->Get('pixel['.$x.','.$y.']')))[0]; | ||
| 112 | |||
| 113 | # cache colors as we look them up | ||
| 114 | my $lookup_color; | ||
| 115 | if(exists($cached_colors{$pixel_red})) { | ||
| 116 | $lookup_color = $cached_colors{$pixel_red}; | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | my $color_offset = ($pixel_red / 255) * ($color_size[1] - 1); | ||
| 120 | $lookup_color = $color_file->Get('pixel[0,'.$color_offset.']'); | ||
| 121 | $cached_colors{$pixel_red} = $lookup_color; | ||
| 122 | } | ||
| 123 | |||
| 124 | # set new color from lookup table | ||
| 125 | $output_file->Set('pixel['.$x.','.$y.']', $lookup_color); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | # overlay heatmap over source image | ||
| 130 | if($parent->get_overlay()) { | ||
| 131 | $input_file->Composite('image' => $output_file, 'compose' => 'Over', 'opacity' => 255 * ($parent->get_opacity() / 100) ); | ||
| 132 | undef $output_file; | ||
| 133 | $output_file = $input_file; | ||
| 134 | } | ||
| 135 | |||
| 136 | # write destination image | ||
| 137 | $output_file->Write($parent->get_output_file()); | ||
| 138 | |||
| 139 | # return true if successful | ||
| 140 | return 1; | ||
| 141 | } | ||
| 142 | |||
| 143 | 1; |