| 1 | package Imager::Search::Driver; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | Imager::Search::Driver - Abstract imlementation of a Imager::Search driver | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | # Create the search | ||||||||||||||||||||
| 12 | my $search = Imager::Search::Driver->new( | ||||||||||||||||||||
| 13 | driver => 'HTML24', | ||||||||||||||||||||
| 14 | big => $large_imager_object, | ||||||||||||||||||||
| 15 | small => $small_imager_object, | ||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # Run the search | ||||||||||||||||||||
| 19 | my $found = $search->find_first; | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # Handle the result | ||||||||||||||||||||
| 22 | print "Found at row " . $found->top . " and column " . $found->left; | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | Given two images (we'll call them Big and Small), where Small is | ||||||||||||||||||||
| 27 | contained within Big zero or more times, determine the pixel locations | ||||||||||||||||||||
| 28 | of Small within Big. | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | For example, given a screen shot or a rendered webpage, locate the | ||||||||||||||||||||
| 31 | position of a known icon or picture within the larger image. | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | The intent is to provide functionality for use in various testing | ||||||||||||||||||||
| 34 | scenarios, or desktop gui automation, and so on. | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 6 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | 6 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | 6 | ▶ | use Params::Util qw{ _STRING _CODELIKE _SCALAR _INSTANCE }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | 6 | ▶ | use Imager (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 6 | ▶ | use vars qw{$VERSION}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 47 | 6 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | 6 | $VERSION = '1.01'; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 6 | ▶ | use Imager::Search::Match (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | ##################################################################### | ||||||||||||||||||||
| 58 | # Constructor and Accessors | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =pod | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =head2 new | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | my $driver = Imager::Search::Driver->new; | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | The C<new> constructor takes a new search driver object. | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | Returns a new B<Imager::Search::Driver> object, or croaks on error. | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 10 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||
| 73 | 10 | my $class = shift; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # Apply the default driver | ||||||||||||||||||||
| 76 | 10 | ▶ | if ( $class eq 'Imager::Search::Driver' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | require Imager::Search::Driver::HTML24; | |||||||||||||||||||
| 78 | 0 | return Imager::Search::Driver::HTML24->new(@_); | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # Create the object | ||||||||||||||||||||
| 82 | 10 | my $self = bless { @_ }, $class; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 10 | return $self; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | ##################################################################### | ||||||||||||||||||||
| 92 | # Driver API Methods | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =pod | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =head2 image_string | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | The C<image_string> method takes a L<Imager::Search::Image> object, and | ||||||||||||||||||||
| 99 | generates the search string for the image. | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | Returns a reference to a scalar, or dies on error. | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | ▶ | sub image_string { | ||||||||||||||||||
|
Subroutine
image_string: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 106 | 0 | ▶ | my $class = ref($_[0]) || $_[0]; | ||||||||||||||||||
|
Condition0%0/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||
| 107 | 0 | die "Illegal driver $class does not implement image_string"; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =pod | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =head2 pattern_lines | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | Because of the way the regular expression spans scanlines, it requires | ||||||||||||||||||||
| 115 | the width of the target image in order to be fully generated. However, | ||||||||||||||||||||
| 116 | the sub-regexp for each scanline can be (and are) generated in advance. | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | When a L<Imager::Search::Pattern> object is created, the driver method | ||||||||||||||||||||
| 119 | C<pattern_lines> is called to generate the scanline regexp for the | ||||||||||||||||||||
| 120 | search pattern. | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | Returns a reference to an ARRAY containing the regexp in string form, | ||||||||||||||||||||
| 123 | or dies on error. | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | ▶ | sub pattern_lines { | ||||||||||||||||||
|
Subroutine
pattern_lines: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 128 | 0 | ▶ | my $class = ref($_[0]) || $_[0]; | ||||||||||||||||||
|
Condition0%0/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||
| 129 | 0 | die "Illegal driver $class does not implement pattern_lines"; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =pod | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =head2 pattern_regexp | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | The C<pattern_regexp> method takes a pattern and an image is retruns a | ||||||||||||||||||||
| 137 | fully-generated search regexp for the pattern, when used on that image. | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | Returns a Regexp object, or dies on error. | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 0 | ▶ | sub pattern_regexp { | ||||||||||||||||||
|
Subroutine
pattern_regexp: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 144 | 0 | ▶ | my $class = ref($_[0] || $_[0]); | ||||||||||||||||||
|
Condition0%0/3
$_[0] || $_[0]
MC/DC0%0/2
$_[0] || $_[0]
$_[0]
$_[0]
Truth table0%0/3
A || B
A= $_[0]B= $_[0]
| |||||||||||||||||||||
| 145 | 0 | die "Illegal driver $class does not implement pattern_regexp"; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =pod | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =head2 match_object | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | Once the regexp engine has located a potential match, the pattern, image | ||||||||||||||||||||
| 153 | and character position are provided to the C<match_object> method. | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | The C<match_object> will take the raw character position, validate that | ||||||||||||||||||||
| 156 | the character position is at a legimate pixel position, and then return | ||||||||||||||||||||
| 157 | the fully-described match. | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | Returns a L<Imager::Search::Match> object if the position is valid, or | ||||||||||||||||||||
| 160 | false (undef in scalar context or a null string in list context) if the | ||||||||||||||||||||
| 161 | position is not valid. | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 0 | ▶ | sub match_object { | ||||||||||||||||||
|
Subroutine
match_object: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 166 | 0 | ▶ | my $class = ref($_[0] || $_[0]); | ||||||||||||||||||
|
Condition0%0/3
$_[0] || $_[0]
MC/DC0%0/2
$_[0] || $_[0]
$_[0]
$_[0]
Truth table0%0/3
A || B
A= $_[0]B= $_[0]
| |||||||||||||||||||||
| 167 | 0 | die "Illegal driver $class does not implement match_object"; | |||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 6 | 1; | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =pod | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head1 SUPPORT | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | See the SUPPORT section of the main L<Imager::Search> module. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head1 AUTHOR | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =head1 COPYRIGHT | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | Copyright 2007 - 2011 Adam Kennedy. | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | This program is free software; you can redistribute | ||||||||||||||||||||
| 187 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | The full text of the license can be found in the | ||||||||||||||||||||
| 190 | LICENSE file included with this module. | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||