Tempest
view release on metacpan or search on metacpan
lib/Tempest.pm view on Meta::CPAN
=head3 C<color_file>
This image, expected to be a true color vertical gradient, is used as a
color lookup table and is applied to the generated heatmap. Defaults
to a bundled image, if none is provided.
=cut
my %color_file;
=head3 C<overlay>
If true, the heatmap is overlaid onto the input image with a given
opacity before being written to the filesystem. Defaults to B<True>.
=cut
my %overlay;
=head3 C<opacity>
Indicates with what percentage of opaqueness to overlay the heatmap
onto the input image. If 0, the heatmap will not be visible; if 100,
the input image will not be visible. Defaults to b<50>.
=cut
my %opacity;
=head3 C<image_lib>
Indicates which supported image manipulation library should be used
for rendering operations. Defaults to the first available from the
following:
=cut
my %image_lib;
my @_required = ('input_file', 'output_file', 'coordinates');
my @_optional = ('plot_file', 'color_file', 'overlay', 'opacity', 'image_lib');
=head1 METHODS
=head2 C<new>
Class constructor, accepts a hash of named arguments corresponding to
the class' own getter and setter methods.
$heatmap = new Tempest(
'input_file' => 'screenshot.png',
lib/Tempest.pm view on Meta::CPAN
croak('Bad parameter list, should be a hash') if @_ % 2;
my %params = @_;
# inside-out object model
my $self = bless \(my $dummy), $class;
# set defaults
$plot_file{$self} = dirname(__FILE__) . '/Tempest/data/plot.png';
$color_file{$self} = dirname(__FILE__) . '/Tempest/data/clut.png';
$overlay{$self} = 1;
$opacity{$self} = 50;
$image_lib{$self} = $self->_calc_image_lib();
# for all required parameters..
for my $param_name (@_required) {
# ..ensure they were provided
if(!exists $params{$param_name}) {
croak("Missing required parameter '$param_name'");
}
lib/Tempest.pm view on Meta::CPAN
return $self;
}
sub get_color_file {
my $self = shift;
return $color_file{$self};
}
sub set_overlay {
my $self = shift;
my $overlay = shift;
$overlay{$self} = $overlay ? 1 : 0;
return $self;
}
sub get_overlay {
my $self = shift;
return $overlay{$self};
}
sub set_opacity {
my $self = shift;
my $opacity = shift;
if($opacity >=0 && $opacity <= 100) {
$opacity{$self} = $opacity;
}
lib/Tempest/Gd.pm view on Meta::CPAN
$cached_colors{$pixel_red} = \@lookup_color;
}
# allocate and set new color from lookup table
my $new_color = $output_file->colorAllocate(@lookup_color);
$output_file->setPixel($x, $y, $new_color);
$output_file->colorDeallocate($new_color);
}
}
# overlay heatmap over source image
if($parent->get_overlay()) {
$input_file->copyMerge($output_file, 0, 0, 0, 0, $output_file->width, $output_file->height, $parent->get_opacity());
undef $output_file;
$output_file = $input_file;
}
# write to output file
write_image($output_file, $parent->get_output_file());
return 1;
}
lib/Tempest/Graphicsmagick.pm view on Meta::CPAN
my $color_offset = ($pixel_red / 255) * ($color_size[1] - 1);
$lookup_color = $color_file->Get('pixel[0,'.$color_offset.']');
$cached_colors{$pixel_red} = $lookup_color;
}
# set new color from lookup table
$output_file->Set('pixel['.$x.','.$y.']', $lookup_color);
}
}
# overlay heatmap over source image
if($parent->get_overlay()) {
$input_file->Composite('image' => $output_file, 'compose' => 'Over', 'opacity' => 255 * ($parent->get_opacity() / 100) );
undef $output_file;
$output_file = $input_file;
}
# write destination image
$output_file->Write($parent->get_output_file());
# return true if successful
return 1;
lib/Tempest/Imagemagick.pm view on Meta::CPAN
$output_file->Clut('image' => $color_file);
}
# for older IM versions (anything before 6.3.5-7), use fx operator
else {
$output_file->Read($parent->get_color_file());
my $fx = $output_file->Fx('expression' => 'v.p{0,u*v.h}');
undef $output_file;
$output_file = $fx;
}
# overlay heatmap over source image
if($parent->get_overlay()) {
$input_file->Composite('image' => $output_file, 'compose' => 'Blend', 'blend' => $parent->get_opacity() . '%');
undef $output_file;
$output_file = $input_file;
}
# write destination image
$output_file->Write($parent->get_output_file());
# return true if successful
return 1;
ok( defined(&Tempest::has_image_lib), 'supported libraries static method' );
## check getters and setters for instance properties
@props = (
'input_file',
'output_file',
'coordinates',
'plot_file',
'color_file',
'overlay',
'opacity',
'image_lib',
);
foreach $propname (@props) {
eval("ok( defined(&Tempest::get_$propname), '$propname getter');");
eval("ok( defined(&Tempest::set_$propname), '$propname setter');");
}
## check that constants exist and that they have the expected values
[208,205],
[208,205],
[388,201],
[298,226],
[369,231],
[343,225],
[345,14],
[345,14],
],
'image_lib' => Tempest::LIB_GD,
'overlay' => 1
);
$result = $instance->render();
ok($result, 'Render method should return true');
ok(-f dirname(__FILE__) . '/data/output_gd.png', 'Output file should exist');
SKIP: {
$result = _compare(10, dirname(__FILE__) . '/data/output_gd.png');
skip 'Compare utility not available', 4 if ! defined $result;
like($result, qr/^0\s+/, 'Output image should resemble the image we expect');
t/graphicsmagick.t view on Meta::CPAN
[208,205],
[208,205],
[388,201],
[298,226],
[369,231],
[343,225],
[345,14],
[345,14],
],
'image_lib' => Tempest::LIB_GMAGICK,
'overlay' => 1
);
$result = $instance->render();
ok($result, 'Render method should return true');
ok(-f dirname(__FILE__) . '/data/output_graphicsmagick.png', 'Output file should exist');
SKIP: {
$result = _compare(5, dirname(__FILE__) . '/data/output_graphicsmagick.png');
skip 'Compare utility not available', 4 if ! defined $result;
like($result, qr/^0\s+/, 'Output image should resemble the image we expect');
t/imagemagick.t view on Meta::CPAN
[208,205],
[208,205],
[388,201],
[298,226],
[369,231],
[343,225],
[345,14],
[345,14],
],
'image_lib' => Tempest::LIB_MAGICK,
'overlay' => 1
);
$result = $instance->render();
ok($result, 'Render method should return true');
ok(-f dirname(__FILE__) . '/data/output_imagemagick.png', 'Output file should exist');
SKIP: {
$result = _compare(5, dirname(__FILE__) . '/data/output_imagemagick.png');
skip 'Compare utility not available', 4 if ! defined $result;
like($result, qr/^0\s+/, 'Output image should resemble the image we expect');
( run in 0.691 second using v1.01-cache-2.11-cpan-49f99fa48dc )