| 1 | # Image::Compare, a module based on the great Imager, used to determine if | ||
| 2 | # two images differ greatly from one another. | ||
| 3 | |||
| 4 | package Image::Compare; | ||
| 5 | |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use Imager; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | # This is the base class for all comparators, and will also do the work of | ||
| 12 | # loading all supplied implementations. | ||
| 13 | 1 | ▶ | use Image::Compare::Comparator; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | |||
| 15 | our %class_map; | ||
| 16 | |||
| 17 | my $loaded_lwp; | ||
| 18 | |||
| 19 | BEGIN { | ||
| 20 | $loaded_lwp = 0; | ||
| 21 | eval "require LWP;"; | ||
| 22 | unless ($@) { $loaded_lwp = 1; } | ||
| 23 | } | ||
| 24 | |||
| 25 | our $VERSION = "1.01"; | ||
| 26 | |||
| 27 | # If people don't want to deal with OO, we export the main "work" method | ||
| 28 | # so they can call it in a simpler way. We'll see below where we handle this. | ||
| 29 | use base qw/Exporter/; | ||
| 30 | our @EXPORT_OK = qw/compare/; | ||
| 31 | |||
| 32 | ## Public methods begin here | ||
| 33 | |||
| 34 | # The constructor method. | ||
| 35 | # Takes a hash of arguments: (all are optional) | ||
| 36 | # image1 => | ||
| 37 | # Data representing the first image, either as an Imager object, file | ||
| 38 | # name or a URL. | ||
| 39 | # type1 => Type of image provided. | ||
| 40 | # image2 => Like image1. | ||
| 41 | # type2 => Like type1. | ||
| 42 | # method => | ||
| 43 | # Either the numeric constant representing the comparator, or an | ||
| 44 | # instance of a comparator. | ||
| 45 | # args => Arguments to pass to the comparator. | ||
| 46 | # See the documentation on the relevant option setters for more details | ||
| 47 | sub new { | ||
| 48 | my $proto = shift; | ||
| 49 | my %args = @_; | ||
| 50 | my $class = ref($proto) || $proto; # Bite me, Randal. | ||
| 51 | my $self = {}; | ||
| 52 | bless($self, $class); | ||
| 53 | # These are default values | ||
| 54 | if ($args{image1}) { | ||
| 55 | $self->set_image1( | ||
| 56 | img => $args{image1}, | ||
| 57 | type => $args{type1} | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | if ($args{image2}) { | ||
| 61 | $self->set_image2( | ||
| 62 | img => $args{image2}, | ||
| 63 | type => $args{type2} | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | if ($args{method}) { | ||
| 67 | $self->set_method( | ||
| 68 | method => $args{method}, | ||
| 69 | args => $args{args} | ||
| 70 | ); | ||
| 71 | } | ||
| 72 | if ($args{mask}) { | ||
| 73 | $self->set_mask(mask => $args{mask}); | ||
| 74 | } | ||
| 75 | return $self; | ||
| 76 | } | ||
| 77 | |||
| 78 | # The next two just use the input to fetch image data and store it as an | ||
| 79 | # Imager object. Currently supported image types: | ||
| 80 | # Imager object | ||
| 81 | # File name | ||
| 82 | # URL | ||
| 83 | sub set_image1 { | ||
| 84 | my $self = shift; | ||
| 85 | my %args = @_; | ||
| 86 | $self->{_IMG1} = _get_image($args{img}, $args{type}); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub set_image2 { | ||
| 90 | my $self = shift; | ||
| 91 | my %args = @_; | ||
| 92 | $self->{_IMG2} = _get_image($args{img}, $args{type}); | ||
| 93 | } | ||
| 94 | |||
| 95 | # Get back the Imager objects created by the preceding two methods. | ||
| 96 | sub get_image1 { | ||
| 97 | my $self = shift; | ||
| 98 | return $self->{_IMG1}; | ||
| 99 | } | ||
| 100 | sub get_image2 { | ||
| 101 | my $self = shift; | ||
| 102 | return $self->{_IMG2}; | ||
| 103 | } | ||
| 104 | |||
| 105 | # How to set the matching mask parameter for this compaison instance. | ||
| 106 | sub set_mask { | ||
| 107 | my $self = shift; | ||
| 108 | my %args = @_; | ||
| 109 | $self->{_MASK} = $args{mask}; | ||
| 110 | } | ||
| 111 | sub get_mask { | ||
| 112 | my $self = shift; | ||
| 113 | return $self->{_MASK}; | ||
| 114 | } | ||
| 115 | |||
| 116 | # Given input as defined above, returns an Imager object representing the | ||
| 117 | # image. | ||
| 118 | sub _get_image { | ||
| 119 | my($img, $type) = @_; | ||
| 120 | unless ($img) { | ||
| 121 | die "Missing 'img' parameter"; | ||
| 122 | } | ||
| 123 | |||
| 124 | # If we've been given an Imager object, we need only store it. | ||
| 125 | if (ref($img)) { | ||
| 126 | if ($img->isa('Imager')) { | ||
| 127 | return $img; | ||
| 128 | } | ||
| 129 | # If it wasn't an Imager, but it's still some kind of reference, then | ||
| 130 | # we have to give up. | ||
| 131 | die "Unrecognized input type: '" . ref($img) . "'"; | ||
| 132 | } | ||
| 133 | |||
| 134 | # Otherwse, we need to construct an Imager object, and to do that, we | ||
| 135 | # need to build up an arguments hash for the Imager constructor. | ||
| 136 | my %args; | ||
| 137 | if ($type) { | ||
| 138 | # Provide the type argument to image, if it was provided. | ||
| 139 | $args{type} = $type; | ||
| 140 | } | ||
| 141 | # This is the base error message. | ||
| 142 | my $errmsg = "Unable to read image data from "; | ||
| 143 | # If $img looks like a URL, and if we were able to load LWP, then we might | ||
| 144 | # be able to fetch an image via a URL. | ||
| 145 | if ($loaded_lwp && ($img =~ /^https?:\/\//)) { | ||
| 146 | $errmsg .= "URL '$img'"; | ||
| 147 | my $ua = LWP::UserAgent->new(); | ||
| 148 | $ua->agent("Image::Compare/v$VERSION "); | ||
| 149 | my $res = $ua->request(HTTP::Request->new(GET => $img)); | ||
| 150 | $args{data} = $res->content(); | ||
| 151 | if (!$type) { | ||
| 152 | $args{type} = $res->content_type(); | ||
| 153 | $args{type} =~ s!^image/!!; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | # Otherwise, we have to think it's a file path. | ||
| 158 | $errmsg .= "file '$img'"; | ||
| 159 | $args{file} = $img; | ||
| 160 | } | ||
| 161 | my $newimg = Imager->new(); | ||
| 162 | $newimg->read(%args) || die($errmsg . ": '" . $newimg->{ERRSTR} . "'"); | ||
| 163 | return $newimg; | ||
| 164 | } | ||
| 165 | |||
| 166 | # Sets the comparison method. Either takes the numeric constant that | ||
| 167 | # identifies the method and any arguments needed by the method, or an instance | ||
| 168 | # of the comparator. See the documentation for Image::Compare::Comparator or | ||
| 169 | # it subclasses for more details. | ||
| 170 | sub set_method { | ||
| 171 | my $self = shift; | ||
| 172 | my %args = @_; | ||
| 173 | unless ($args{method}) { | ||
| 174 | die "Missing required argument 'method'"; | ||
| 175 | } | ||
| 176 | if (ref($args{method})) { | ||
| 177 | if ($args{method}->isa('Image::Compare::Comparator')) { | ||
| 178 | $self->{_CMP} = $args{method}; | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | die ( | ||
| 182 | "Unrecognized type for 'method' argument: '" . | ||
| 183 | ref($args{method}) . "'" | ||
| 184 | ); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | else { | ||
| 188 | unless ($class_map{$args{method}}) { | ||
| 189 | die "Unrecognized method identifier: '$args{method}'"; | ||
| 190 | } | ||
| 191 | $self->{_CMP} = $class_map{$args{method}}->new($args{args}); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | # Returns information describing the comparison method set into this instance | ||
| 196 | # of an Image::Compare. | ||
| 197 | sub get_method { | ||
| 198 | my $self = shift; | ||
| 199 | unless ($self->{_CMP}) { | ||
| 200 | return wantarray ? () : undef; | ||
| 201 | } | ||
| 202 | return $self->{_CMP}->get_representation(); | ||
| 203 | } | ||
| 204 | |||
| 205 | # Compares two images and returns a result. | ||
| 206 | sub compare { | ||
| 207 | my $self; | ||
| 208 | # This can be called as an instance method | ||
| 209 | if (ref($_[0]) eq 'Image::Compare') { | ||
| 210 | $self = shift; | ||
| 211 | } | ||
| 212 | else { | ||
| 213 | # Or, as a class method, if you swing that way... | ||
| 214 | if ($_[0] eq 'Image::Compare') { | ||
| 215 | shift; | ||
| 216 | } | ||
| 217 | # Or just as a plain method. In either case, we just need to construct | ||
| 218 | # a $self so we can get on with life. | ||
| 219 | $self = Image::Compare->new(@_); | ||
| 220 | } | ||
| 221 | # Sanity checking | ||
| 222 | for my $ref ( | ||
| 223 | ['IMG1', 'Image 1'], ['IMG2', 'Image 2'], ['CMP', 'Comparison method'], | ||
| 224 | ) { | ||
| 225 | die "$ref->[1] not specified" unless $self->{"_$ref->[0]"}; | ||
| 226 | } | ||
| 227 | |||
| 228 | # Give the images to the comparator and let them compare them. | ||
| 229 | # The comparator will raise an exception if anything's wrong. | ||
| 230 | return $self->{_CMP}->compare_images( | ||
| 231 | @{$self}{qw/_IMG1 _IMG2 _MASK/} | ||
| 232 | ); | ||
| 233 | } | ||
| 234 | |||
| 235 | 1; | ||
| 236 | |||
| 237 | __END__ | ||