Compare/Comparator.pm

filter: Statementstmt 83.5% 76 / 91 Branchbran 70.8% 17 / 24 Conditioncond 33.3% 9 / 27 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 78.9% 15 / 19 Podpod 100% 9 / 9 totaltotal 68.0% 128 / 188 CC 21 SCAR 20.7
CC
21
Cov
72%
setup
21.4
compare_images
6.0
accumulate
6.0
CRAP 7.9
« Compare.pm ↑ Summary Compare/AVG_THRESHOLD.pm »
1 package Image::Compare::Comparator;
2  
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
7 20 my $proto = shift;
8 20 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0200
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
9 20 my $self = {};
10 20 $self->{args} = shift;
11 20 $self->{mask} = shift;
12 20 bless($self, $class);
13 20 return $self;
14 }
15  
16 # This will do initial setup and throw an exception if there is something
17 # wrong. We have some common behavior in here. Subclasses may override this,
18 # or add to it.
19 15 ▶sub setup {
Subroutine setup: called CC=10 SCAR=30.6
Pod documented
20 15 my $self = shift;
21 15 my ($img1, $img2, $mask) = @_;
22 15 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($img1->getwidth == $img2->getwidth and $img1->getheight == $img2->getheight) 0 15 15
Condition33%1/3
$img1->getwidth == $img2->getwidth and $img1->getheight == $img2->getheight
!ll&&!rl&&r
0015
MC/DC0%0/2
$img1->getwidth == $img2->getwidth and $img1->getheight == $img2->getheight
$img1->getwidth == $img2->getwidth $img1->getheight == $img2->getheight
Truth table33%1/3
A and B
A= $img1->getwidth == $img2->getwidthB= $img1->getheight == $img2->getheight
ABresult
0X0
100
111
23 ($img1->getwidth() == $img2->getwidth() ) &&
24 ($img1->getheight() == $img2->getheight())
25 ) {
26 0 die "Images must be the same size!";
27 }
28 15 ▶ if ($mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask) 4 11 15
29 4 ▶ unless(ref($mask) && $mask->isa('Imager')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $mask and $mask->isa("Imager")) 0 4 4
Condition33%1/3
ref $mask and $mask->isa("Imager")
!ll&&!rl&&r
004
MC/DC0%0/2
ref $mask and $mask->isa("Imager")
ref $mask $mask->isa("Imager")
Truth table33%1/3
A and B
A= ref $maskB= $mask->isa("Imager")
ABresult
0X0
100
111
30 0 die "Match mask must be an Imager image object!";
31 }
32 4 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($mask->getchannels == 1 and $mask->bits == 8) 0 4 4
Condition33%1/3
$mask->getchannels == 1 and $mask->bits == 8
!ll&&!rl&&r
004
MC/DC0%0/2
$mask->getchannels == 1 and $mask->bits == 8
$mask->getchannels == 1 $mask->bits == 8
Truth table33%1/3
A and B
A= $mask->getchannels == 1B= $mask->bits == 8
ABresult
0X0
100
111
33 ($mask->getchannels() == 1) &&
34 ($mask->bits() == 8)
35 ) {
36 0 die "Match mask image must have one channel and 8 bits per channel!";
37 }
38 4 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($mask->getwidth == $img1->getwidth and $mask->getheight == $img1->getheight) 0 4 4
Condition33%1/3
$mask->getwidth == $img1->getwidth and $mask->getheight == $img1->getheight
!ll&&!rl&&r
004
MC/DC0%0/2
$mask->getwidth == $img1->getwidth and $mask->getheight == $img1->getheight
$mask->getwidth == $img1->getwidth $mask->getheight == $img1->getheight
Truth table33%1/3
A and B
A= $mask->getwidth == $img1->getwidthB= $mask->getheight == $img1->getheight
ABresult
0X0
100
111
39 ($mask->getwidth() == $img1->getwidth() ) &&
40 ($mask->getheight() == $img1->getheight())
41 ) {
42 0 die "Match mask must be the same size as the test images!";
43 }
44 }
45 }
46  
47 0 ▶sub get_args {
Subroutine get_args: not called CC=1 SCAR=6.9
Pod documented
48 0 my $self = shift;
49 0 return $self->{args};
50 }
51  
52 # By default, just return the class name and the arguments.
53 1 ▶sub get_representation {
Subroutine get_representation: called CC=1 SCAR=0.0
Pod documented
54 1 my $self = shift;
55 return (
56 method => $Image::Compare::reverse_class_map{ref($self)},
57 args => $self->{args},
58 1 );
59 }
60  
61 18 ▶sub compare_images {
Subroutine compare_images: called CC=6 SCAR=17.9
Pod documented
62 18 my $self = shift;
63 18 my ($img1, $img2, $mask) = @_;
64 # This will die if there's a problem.
65 18 $self->setup($img1, $img2, $mask);
66 # We spin over each pixel in img1.
67 18 my $wid = $img1->getwidth();
68 18 my $hig = $img1->getheight();
69 18 OUTER: for my $x (0 .. $wid - 1) {
70 34 for my $y (0 .. $hig - 1) {
71 # If we've been given a match mask, then we skip any pixel whose
72 # corresponding pixel in that mask is pure black.
73 # This is the entirety of the comparison logic surrounding masks. It is
74 # all so simple, I should have done it long ago.
75 66 ▶ if ($mask && (($mask->getpixel(x => $x, y => $y)->rgba())[0] == 255)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask and ($mask->getpixel("x", $x, "y", $y)->rgba)[0] == 255) 5 61 66
Condition100%3/3
$mask and ($mask->getpixel("x", $x, "y", $y)->rgba)[0] == 255
!ll&&!rl&&r
46155
MC/DC100%2/2
$mask and ($mask->getpixel("x", $x, "y", $y)->rgba)[0] == 255
$mask ($mask->getpixel("x", $x, "y", $y)->rgba)[0] == 255
Truth table100%3/3
A and B
A= $maskB= ($mask->getpixel("x", $x, "y", $y)->rgba)[0] == 255
ABresult
0X0
100
111
76 5 next;
77 }
78 61 my @pix1 = $img1->getpixel(x => $x, y => $y)->rgba();
79 61 my @pix2 = $self->get_second_pixel($img2, $x, $y)->rgba();
80 # If this returns undef, then we keep going. Otherwise, we stop.
81 # It will die if there's an error.
82 # This mechanism allows the subclass to short-circuit image examination
83 # if it feels the need to do so.
84 61 ▶ last OUTER if defined $self->accumulate(\@pix1, \@pix2, $x, $y);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->accumulate(\@pix1, \@pix2, $x, $y) 2 59 61
85 }
86 }
87 # And finally, the subclass will return the thing it wants to return.
88 18 return $self->get_result();
89 }
90  
91 # By default, this is pretty boring.
92 # Subclasses may want to override it though.
93 # On second thought, I can't think of a reason why they would want to.
94 # I guess I will leave this in anyways.
95 61 ▶sub get_second_pixel {
Subroutine get_second_pixel: called CC=1 SCAR=0.0
Pod documented
96 61 my $self = shift;
97 61 my ($img2, $x, $y) = @_;
98 61 return $img2->getpixel(x => $x, y => $y);
99 }
100  
101 # Some day we might have multiple ways to do this.
102 61 ▶sub color_distance {
Subroutine color_distance: called CC=1 SCAR=0.0
Pod documented
103 61 my $self = shift;
104 61 my ($pix1, $pix2) = @_;
105 # The sum of the squaws of the other two hides...
106 return sqrt(
107 61 ( ($pix1->[0] - $pix2->[0]) ** 2 ) +
108 ( ($pix1->[1] - $pix2->[1]) ** 2 ) +
109 ( ($pix1->[2] - $pix2->[2]) ** 2 )
110 );
111 }
112  
113 0 ▶sub accumulate {
Subroutine accumulate: not called CC=2 SCAR=17.9
Pod documented
114 0 my $self = shift;
115 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
116 0 die "Subclass '$class' must implement accumulate()!";
117 }
118  
119 0 ▶sub get_result {
Subroutine get_result: not called CC=2 SCAR=17.9
Pod documented
120 0 my $self = shift;
121 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
122 0 die "Subclass '$class' must implement get_result()!";
123 }
124  
125 48 ▶sub import {
Subroutine import: called CC=4 SCAR=14.8
126 48 my $cmp_pkg = shift;
127 48 my %args = @_;
128 48 ▶ unless (UNIVERSAL::isa($cmp_pkg, __PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($cmp_pkg, __PACKAGE__)) 0 48 48
129 0 die "Comparaters must subclass __PACKAGE__!";
130 }
131 48 my $name = $cmp_pkg;
132 48 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ s/^Image::Compare::// or $name = $args{"name"}) 0 48 48
Condition33%1/3
$name =~ s/^Image::Compare::// or $name = $args{"name"}
l!l&&r!l&&!r
4800
MC/DC0%0/2
$name =~ s/^Image::Compare::// or $name = $args{"name"}
$name =~ s/^Image::Compare::// $name = $args{"name"}
Truth table33%1/3
A or B
A= $name =~ s/^Image::Compare:://B= $name = $args{"name"}
ABresult
1X1
011
000
133 ($name =~ s/^Image::Compare:://) ||
134 ($name = $args{name})
135 ) {
136 0 die (
137 "Comparator must either be in the Image::Compare namespace, " .
138 "or you must provide a method name to import."
139 );
140 }
141 {
142 8 ▶ no strict qw/refs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 # We are essentially "exporting" this for backwards compatibility. We
144 # don't really want to use constants like this any more, but we have
145 # to. Shucks.
146 48 my $name_const = $name;
147 0 ▶ *{"Image::Compare::$name"} = sub () { $name_const };
Subroutine __ANON__: not called CC=1 SCAR=0.4
148 48 $Image::Compare::class_map{$name} = $cmp_pkg;
149 48 $Image::Compare::reverse_class_map{$cmp_pkg} = $name;
150 }
151 }
152  
153 # We will read in the list of packages to load from the documentation.
154 8 ▶while (<Image::Compare::Comparator::DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 1872 8 1880
155 1872 ▶ if (/^=item \* L<([^>]+)>/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^=item \* L<([^>]+)>/) 40 1832 1872
156 8 ▶ eval "use $1";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
157 40 ▶ die "Failed loading module '$1': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 40 40
158 }
159 }
160  
161 8 close Image::Compare::Comparator::DATA;
162  
163 8 1;
164  
165 __DATA__
« Compare.pm ↑ Summary Compare/AVG_THRESHOLD.pm »