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         9  
  8         199  
4 8     8   24 use strict;
  8         9  
  8         129  
5              
6 8     8   22 use Imager ':handy';
  8         10  
  8         33  
7 8     8   5046 use Imager::Fountain;
  8         14926  
  8         207  
8              
9 8     8   42 use base qw/Image::Compare::Comparator/;
  8         11  
  8         512  
10              
11 8     8   34 use constant FILL_IMAGE_CACHE => {};
  8         8  
  8         454  
12 8     8   33 use constant FILL_IMAGE_WIDTH => 1000;
  8         7  
  8         500  
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         26 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   1995 );
  8         10  
21              
22             sub setup {
23 4     4 1 3 my $self = shift;
24 4         17 $self->SUPER::setup(@_);
25 4         23 $self->{count} = 0;
26 4         11 $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         128 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     14 if (ref $self->{args} && $self->{args}->isa('Imager::Fountain')) {
38 1         1 $fountain = $self->{args};
39             }
40             else {
41 1         1 $fountain = DEFAULT_COLOR_FOUNTAIN();
42             }
43             }
44             else {
45             # In this case, we use the default greyscale fountain.
46 2         3 $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       19 unless (FILL_IMAGE_CACHE()->{$fountain}) {
52 4         9 my $image = Imager->new(xsize => FILL_IMAGE_WIDTH(), ysize => 1);
53 4         103 $image->filter(
54             type => 'fountain', segments => $fountain,
55             xa => 0, ya => 0,
56             xb => FILL_IMAGE_WIDTH(), yb => 0,
57             );
58 4         748 FILL_IMAGE_CACHE()->{$fountain} = $image;
59             }
60 4         10 $self->{fill_image} = FILL_IMAGE_CACHE()->{$fountain};
61             }
62              
63             sub accumulate {
64 15     15 1 11 my $self = shift;
65 15         15 my($pix1, $pix2, $x, $y) = @_;
66 15         26 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         17 $diff *= FILL_IMAGE_WIDTH();
70 15         14 $diff /= 441.7;
71             my $color = $self->{fill_image}->getpixel(
72 15         23 x => $diff,
73             y => 0,
74             );
75             $self->{img}->setpixel(
76 15         174 x => $x,
77             y => $y,
78             color => $color,
79             );
80 15         251 return undef;
81             }
82              
83             sub get_result {
84 4     4 1 2 my $self = shift;
85 4         11 return $self->{img};
86             }
87              
88             1;
89              
90             __END__