File Coverage

blib/lib/Image/Compare/IMAGE.pm
Criterion Covered Total %
statement 52 52 100.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 12 12 100.0
pod 3 3 100.0
total 74 76 97.3


line stmt bran cond sub pod time code
1             package Image::Compare::IMAGE;
2              
3 8     8   26 use warnings;
  8         10  
  8         192  
4 8     8   21 use strict;
  8         8  
  8         117  
5              
6 8     8   21 use Imager ':handy';
  8         6  
  8         30  
7 8     8   4597 use Imager::Fountain;
  8         14021  
  8         216  
8              
9 8     8   41 use base qw/Image::Compare::Comparator/;
  8         11  
  8         494  
10              
11 8     8   27 use constant FILL_IMAGE_CACHE => {};
  8         9  
  8         409  
12 8     8   26 use constant FILL_IMAGE_WIDTH => 1000;
  8         8  
  8         472  
13 8         28 use constant DEFAULT_COLOR_FOUNTAIN => Imager::Fountain->simple(
14             positions => [ 0.0, 0.5, 1.0],
15             colors => [NC(255, 0, 0), NC(0, 255, 0), NC(0, 0, 255)],
16 8     8   30 );
  8         8  
17 8         25 use constant DEFAULT_GREYSCALE_FOUNTAIN => Imager::Fountain->simple(
18             positions => [ 0.0, 1.0],
19             colors => [NC(0, 0, 0), NC(255, 255, 255)],
20 8     8   1888 );
  8         10  
21              
22             sub setup {
23 4     4 1 5 my $self = shift;
24 4         17 $self->SUPER::setup(@_);
25 4         24 $self->{count} = 0;
26 4         14 $self->{img} = Imager->new(
27             xsize => $_[0]->getwidth(),
28             ysize => $_[0]->getheight(),
29             );
30             # Now we build up our fill image; this is a 1000 x 1 image that is filled
31             # using an Imager fountain. We'll pull pixels from it to put colors into
32             # the generated image.
33 4         145 my $fountain;
34 4 100       9 if ($self->{args}) {
35             # If there's an argument, it's either a fountain, or it's telling us to use
36             # the default color fountain.
37 2 100 66     17 if (ref $self->{args} && $self->{args}->isa('Imager::Fountain')) {
38 1         2 $fountain = $self->{args};
39             }
40             else {
41 1         2 $fountain = DEFAULT_COLOR_FOUNTAIN();
42             }
43             }
44             else {
45             # In this case, we use the default greyscale fountain.
46 2         2 $fountain = DEFAULT_GREYSCALE_FOUNTAIN();
47             }
48             # Using an object as a hash key stringifies it to its memory location and
49             # uses that. This is suboptimal, but the Imager::Fountain class has no
50             # "to_string", so we make do.
51 4 50       18 unless (FILL_IMAGE_CACHE()->{$fountain}) {
52 4         11 my $image = Imager->new(xsize => FILL_IMAGE_WIDTH(), ysize => 1);
53 4         111 $image->filter(
54             type => 'fountain', segments => $fountain,
55             xa => 0, ya => 0,
56             xb => FILL_IMAGE_WIDTH(), yb => 0,
57             );
58 4         806 FILL_IMAGE_CACHE()->{$fountain} = $image;
59             }
60 4         13 $self->{fill_image} = FILL_IMAGE_CACHE()->{$fountain};
61             }
62              
63             sub accumulate {
64 15     15 1 13 my $self = shift;
65 15         13 my($pix1, $pix2, $x, $y) = @_;
66 15         27 my $diff = $self->color_distance($pix1, $pix2);
67             # We want to convert from our range of values for $diff (0 .. 441.7) to
68             # the range of values of our fill image, which is defined by a constant.
69 15         13 $diff *= FILL_IMAGE_WIDTH();
70 15         14 $diff /= 441.7;
71             my $color = $self->{fill_image}->getpixel(
72 15         24 x => $diff,
73             y => 0,
74             );
75             $self->{img}->setpixel(
76 15         198 x => $x,
77             y => $y,
78             color => $color,
79             );
80 15         228 return undef;
81             }
82              
83             sub get_result {
84 4     4 1 6 my $self = shift;
85 4         10 return $self->{img};
86             }
87              
88             1;
89              
90             __END__