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   27 use warnings;
  8         9  
  8         191  
4 8     8   22 use strict;
  8         7  
  8         117  
5              
6 8     8   19 use Imager ':handy';
  8         9  
  8         26  
7 8     8   4553 use Imager::Fountain;
  8         13627  
  8         203  
8              
9 8     8   39 use base qw/Image::Compare::Comparator/;
  8         9  
  8         482  
10              
11 8     8   29 use constant FILL_IMAGE_CACHE => {};
  8         8  
  8         398  
12 8     8   28 use constant FILL_IMAGE_WIDTH => 1000;
  8         9  
  8         460  
13 8         27 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   25 );
  8         12  
17 8         22 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   1889 );
  8         9  
21              
22             sub setup {
23 4     4 1 4 my $self = shift;
24 4         13 $self->SUPER::setup(@_);
25 4         23 $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         148 my $fountain;
34 4 100       10 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     16 if (ref $self->{args} && $self->{args}->isa('Imager::Fountain')) {
38 1         1 $fountain = $self->{args};
39             }
40             else {
41 1         3 $fountain = DEFAULT_COLOR_FOUNTAIN();
42             }
43             }
44             else {
45             # In this case, we use the default greyscale fountain.
46 2         4 $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         10 my $image = Imager->new(xsize => FILL_IMAGE_WIDTH(), ysize => 1);
53 4         109 $image->filter(
54             type => 'fountain', segments => $fountain,
55             xa => 0, ya => 0,
56             xb => FILL_IMAGE_WIDTH(), yb => 0,
57             );
58 4         851 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         14 my($pix1, $pix2, $x, $y) = @_;
66 15         29 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         18 $diff *= FILL_IMAGE_WIDTH();
70 15         12 $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         190 x => $x,
77             y => $y,
78             color => $color,
79             );
80 15         233 return undef;
81             }
82              
83             sub get_result {
84 4     4 1 4 my $self = shift;
85 4         10 return $self->{img};
86             }
87              
88             1;
89              
90             __END__