| 1 | package Image::Compare::Comparator; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | sub new { | ||||||||||||||||||||||||||||
| 7 | 0 | ▶ | my $proto = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 8 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 9 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 10 | 0 | $self->{args} = shift; | |||||||||||||||||||||||||||
| 11 | 0 | $self->{mask} = shift; | |||||||||||||||||||||||||||
| 12 | 0 | bless($self, $class); | |||||||||||||||||||||||||||
| 13 | 0 | 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 | sub setup { | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
setup: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 21 | 0 | my ($img1, $img2, $mask) = @_; | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | unless ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$img1->getwidth == $img2->getwidth and $img1->getheight == $img2->getheight
MC/DC0%0/2
$img1->getwidth == $img2->getwidth and $img1->getheight == $img2->getheight
$img1->getwidth == $img2->getwidth
$img1->getheight == $img2->getheight
Truth table0%0/3
A and B
A= $img1->getwidth == $img2->getwidthB= $img1->getheight == $img2->getheight
| |||||||||||||||||||||||||||||
| 23 | ($img1->getwidth() == $img2->getwidth() ) && | ||||||||||||||||||||||||||||
| 24 | ($img1->getheight() == $img2->getheight()) | ||||||||||||||||||||||||||||
| 25 | ) { | ||||||||||||||||||||||||||||
| 26 | 0 | die "Images must be the same size!"; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | if ($mask) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | unless(ref($mask) && $mask->isa('Imager')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $mask and $mask->isa("Imager")
MC/DC0%0/2
ref $mask and $mask->isa("Imager")
ref $mask
$mask->isa("Imager")
Truth table0%0/3
A and B
A= ref $maskB= $mask->isa("Imager")
| |||||||||||||||||||||||||||||
| 30 | 0 | die "Match mask must be an Imager image object!"; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | unless ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mask->getchannels == 1 and $mask->bits == 8
MC/DC0%0/2
$mask->getchannels == 1 and $mask->bits == 8
$mask->getchannels == 1
$mask->bits == 8
Truth table0%0/3
A and B
A= $mask->getchannels == 1B= $mask->bits == 8
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | unless ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mask->getwidth == $img1->getwidth and $mask->getheight == $img1->getheight
MC/DC0%0/2
$mask->getwidth == $img1->getwidth and $mask->getheight == $img1->getheight
$mask->getwidth == $img1->getwidth
$mask->getheight == $img1->getheight
Truth table0%0/3
A and B
A= $mask->getwidth == $img1->getwidthB= $mask->getheight == $img1->getheight
| |||||||||||||||||||||||||||||
| 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 | sub get_args { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 0 | return $self->{args}; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # By default, just return the class name and the arguments. | ||||||||||||||||||||||||||||
| 53 | sub get_representation { | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_representation: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 55 | return ( | ||||||||||||||||||||||||||||
| 56 | method => $Image::Compare::reverse_class_map{ref($self)}, | ||||||||||||||||||||||||||||
| 57 | args => $self->{args}, | ||||||||||||||||||||||||||||
| 58 | 0 | ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub compare_images { | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
compare_images: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($img1, $img2, $mask) = @_; | |||||||||||||||||||||||||||
| 64 | # This will die if there's a problem. | ||||||||||||||||||||||||||||
| 65 | 0 | $self->setup($img1, $img2, $mask); | |||||||||||||||||||||||||||
| 66 | # We spin over each pixel in img1. | ||||||||||||||||||||||||||||
| 67 | 0 | my $wid = $img1->getwidth(); | |||||||||||||||||||||||||||
| 68 | 0 | my $hig = $img1->getheight(); | |||||||||||||||||||||||||||
| 69 | 0 | OUTER: for my $x (0 .. $wid - 1) { | |||||||||||||||||||||||||||
| 70 | 0 | 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 | 0 | ▶ | if ($mask && (($mask->getpixel(x => $x, y => $y)->rgba())[0] == 255)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mask and ($mask->getpixel("x", $x, "y", $y)->rgba)[0] == 255
MC/DC0%0/2
$mask and ($mask->getpixel("x", $x, "y", $y)->rgba)[0] == 255
$mask
($mask->getpixel("x", $x, "y", $y)->rgba)[0] == 255
Truth table0%0/3
A and B
A= $maskB= ($mask->getpixel("x", $x, "y", $y)->rgba)[0] == 255
| |||||||||||||||||||||||||||||
| 76 | 0 | next; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 0 | my @pix1 = $img1->getpixel(x => $x, y => $y)->rgba(); | |||||||||||||||||||||||||||
| 79 | 0 | 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 | 0 | ▶ | last OUTER if defined $self->accumulate(\@pix1, \@pix2, $x, $y); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | # And finally, the subclass will return the thing it wants to return. | ||||||||||||||||||||||||||||
| 88 | 0 | 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 | sub get_second_pixel { | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_second_pixel: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 97 | 0 | my ($img2, $x, $y) = @_; | |||||||||||||||||||||||||||
| 98 | 0 | return $img2->getpixel(x => $x, y => $y); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Some day we might have multiple ways to do this. | ||||||||||||||||||||||||||||
| 102 | sub color_distance { | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
color_distance: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 104 | 0 | my ($pix1, $pix2) = @_; | |||||||||||||||||||||||||||
| 105 | # The sum of the squaws of the other two hides... | ||||||||||||||||||||||||||||
| 106 | return sqrt( | ||||||||||||||||||||||||||||
| 107 | 0 | ( ($pix1->[0] - $pix2->[0]) ** 2 ) + | |||||||||||||||||||||||||||
| 108 | ( ($pix1->[1] - $pix2->[1]) ** 2 ) + | ||||||||||||||||||||||||||||
| 109 | ( ($pix1->[2] - $pix2->[2]) ** 2 ) | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | sub accumulate { | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
accumulate: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 116 | 0 | die "Subclass '$class' must implement accumulate()!"; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub get_result { | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_result: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 122 | 0 | die "Subclass '$class' must implement get_result()!"; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub import { | ||||||||||||||||||||||||||||
| 126 | 1 | ▶ | my $cmp_pkg = shift; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=16.7
| |||||||||||||||||||||||||||||
| 127 | 1 | my %args = @_; | |||||||||||||||||||||||||||
| 128 | 1 | ▶ | unless (UNIVERSAL::isa($cmp_pkg, __PACKAGE__)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | die "Comparaters must subclass __PACKAGE__!"; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 1 | my $name = $cmp_pkg; | |||||||||||||||||||||||||||
| 132 | 1 | ▶ | unless ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$name =~ s/^Image::Compare::// or $name = $args{"name"}
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"}
| |||||||||||||||||||||||||||||
| 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 | 1 | ▶ | 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 | 1 | ▶ | *{"Image::Compare::$name"} = sub () { $name }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 147 | 0 | $Image::Compare::class_map{$name} = $cmp_pkg; | |||||||||||||||||||||||||||
| 148 | 0 | $Image::Compare::reverse_class_map{$cmp_pkg} = $name; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # We will read in the list of packages to load from the documentation. | ||||||||||||||||||||||||||||
| 153 | while (<Image::Compare::Comparator::DATA>) { | ||||||||||||||||||||||||||||
| 154 | if (/^=item \* L<([^>]+)>/) { | ||||||||||||||||||||||||||||
| 155 | 1 | ▶ | eval "use $1"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | die "Failed loading module '$1': $@" if $@; | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | close Image::Compare::Comparator::DATA; | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1; | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | __DATA__ | ||||||||||||||||||||||||||||