| 1 | package Imager::Search::Pattern; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Imager::Search::Pattern - Search object for an image | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | my $pattern = Imager::Search::Pattern->new( | ||||||||||||||||||||||||||||
| 12 | driver => 'Imager::Search::Driver::HTML24', | ||||||||||||||||||||||||||||
| 13 | image => $Imager, | ||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | my $regexp = $pattern->regexp; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | B<Imager::Search::Pattern> takes an L<Imager> object, and converts it | ||||||||||||||||||||||||||||
| 21 | into a partially-compiled regular expression. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | This partial regexp can then be quickly turned into the final L<Regexp> | ||||||||||||||||||||||||||||
| 24 | once the widget of the target image is known, as well as being able to | ||||||||||||||||||||||||||||
| 25 | be cached. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | This allows a single B<Imager::Search::Pattern> object to be quickly | ||||||||||||||||||||||||||||
| 28 | applied to many different sizes of target images. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 5 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 5 | ▶ | use IO::File (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 5 | ▶ | use Params::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 5 | ▶ | use Imager (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 5 | ▶ | use vars qw{$VERSION}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 5 | $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 5 | ▶ | use Object::Tiny::XS qw{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | name | ||||||||||||||||||||||||||||
| 48 | driver | ||||||||||||||||||||||||||||
| 49 | cache | ||||||||||||||||||||||||||||
| 50 | file | ||||||||||||||||||||||||||||
| 51 | image | ||||||||||||||||||||||||||||
| 52 | height | ||||||||||||||||||||||||||||
| 53 | width | ||||||||||||||||||||||||||||
| 54 | lines | ||||||||||||||||||||||||||||
| 55 | 5 | }; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | ##################################################################### | ||||||||||||||||||||||||||||
| 62 | # Constructors | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =pod | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head2 new | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | $pattern = Imager::Search::Pattern->new( | ||||||||||||||||||||||||||||
| 69 | driver => 'Imager::Search::Driver::HTML24', | ||||||||||||||||||||||||||||
| 70 | file => 'search/image.gif', | ||||||||||||||||||||||||||||
| 71 | cache => 1, | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 6 | my $self = shift->SUPER::new(@_); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Check params | ||||||||||||||||||||||||||||
| 80 | 6 | ▶ | if ( Params::Util::_IDENTIFIER($self->driver) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 2 | $self->{driver} = "Imager::Search::Driver::" . $self->driver; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 6 | ▶ | if ( Params::Util::_DRIVER($self->driver, 'Imager::Search::Driver') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 6 | ▶ | unless ( Params::Util::_INSTANCE($self->driver, 'Imager::Search::Driver') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 6 | $self->{driver} = $self->driver->new; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 6 | ▶ | unless ( Params::Util::_INSTANCE($self->driver, 'Imager::Search::Driver') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | Carp::croak("Did not provide a valid driver"); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 6 | ▶ | if ( defined $self->file and not defined $self->image ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->file and not defined $self->image
MC/DC50%1/2
defined $self->file and not defined $self->image
defined $self->file
not defined $self->image
Truth table66%2/3
A and not(B)
A= defined $self->fileB= not defined $self->image
| |||||||||||||||||||||||||||||
| 92 | # Load the image from a file | ||||||||||||||||||||||||||||
| 93 | 5 | $self->{image} = Imager->new; | |||||||||||||||||||||||||||
| 94 | 5 | $self->{image}->read( file => $self->file ); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 6 | ▶ | if ( defined $self->image ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 6 | ▶ | unless( Params::Util::_INSTANCE($self->image, 'Imager') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | Carp::croak("Did not provide a valid image"); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 6 | $self->{height} = $self->image->getheight; | |||||||||||||||||||||||||||
| 101 | 6 | $self->{width} = $self->image->getwidth; | |||||||||||||||||||||||||||
| 102 | 6 | $self->{lines} = $self->driver->pattern_lines($self->image); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 6 | ▶ | unless ( Params::Util::_POSINT($self->height) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | Carp::croak("Invalid or missing image height"); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 6 | ▶ | unless ( Params::Util::_POSINT($self->width) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | Carp::croak("Invalid or missing image width"); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 6 | ▶ | unless ( Params::Util::_ARRAY($self->lines) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | Carp::croak("Did not provide an ARRAY of line patterns"); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Normalise caching behaviour | ||||||||||||||||||||||||||||
| 115 | 6 | $self->{cache} = !! $self->cache; | |||||||||||||||||||||||||||
| 116 | 6 | ▶ | if ( $self->cache ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | $self->{regexp} = {}; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | return $self; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=6 SCAR=19.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 125 | 1 | my $io = undef; | |||||||||||||||||||||||||||
| 126 | 1 | ▶ | if ( Params::Util::_INSTANCE($_[0], 'IO::Handle') ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 127 | 0 | $io = $_[0]; | |||||||||||||||||||||||||||
| 128 | } elsif ( Params::Util::_STRING($_[0]) ) { | ||||||||||||||||||||||||||||
| 129 | 1 | $io = IO::File->new( $_[0], 'w' ); | |||||||||||||||||||||||||||
| 130 | 1 | ▶ | unless ( Params::Util::_INSTANCE($io, 'IO::File') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | Carp::croak("Failed to open $_[0] to write"); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||
| 134 | 0 | Carp::croak("Did not provide a file or handle to write"); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # The first line is the class of this object | ||||||||||||||||||||||||||||
| 138 | 1 | $io->print( "class: " . ref($self) . "\n" ); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Next, a series of key: value pairs of the main properties | ||||||||||||||||||||||||||||
| 141 | 1 | foreach my $key ( qw{ driver width height } ) { | |||||||||||||||||||||||||||
| 142 | 3 | $io->print( $key . ': ' . $self->$key() . "\n" ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Ending with a blank newline to indicate the end of the headers | ||||||||||||||||||||||||||||
| 146 | 1 | $io->print("\n"); | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # And now we print all of the pattern lines | ||||||||||||||||||||||||||||
| 149 | 1 | my $lines = $self->lines; | |||||||||||||||||||||||||||
| 150 | 1 | foreach ( 0 .. $#$lines ) { | |||||||||||||||||||||||||||
| 151 | 13 | $io->print( $lines->[0] . "\n" ); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # Return without closing. | ||||||||||||||||||||||||||||
| 155 | # Any file we opened will auto-close, | ||||||||||||||||||||||||||||
| 156 | # and anyone passing a handle should close it themselves. | ||||||||||||||||||||||||||||
| 157 | 1 | return 1; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | ##################################################################### | ||||||||||||||||||||||||||||
| 165 | # Main Methods | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 6 | ▶ | sub regexp { | ||||||||||||||||||||||||||
|
Subroutine
regexp: called CC=7 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # Get the width param | ||||||||||||||||||||||||||||
| 171 | 6 | my $width = undef; | |||||||||||||||||||||||||||
| 172 | 6 | ▶ | if ( Params::Util::_INSTANCE($_[0], 'Imager') ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 173 | 0 | $width = $_[0]->getwidth; | |||||||||||||||||||||||||||
| 174 | } elsif ( Params::Util::_INSTANCE($_[0], 'Imager::Search::Image') ) { | ||||||||||||||||||||||||||||
| 175 | 6 | $width = $_[0]->width; | |||||||||||||||||||||||||||
| 176 | } elsif ( Params::Util::_POSINT($_[0]) ) { | ||||||||||||||||||||||||||||
| 177 | 0 | $width = $_[0]; | |||||||||||||||||||||||||||
| 178 | } else { | ||||||||||||||||||||||||||||
| 179 | 0 | Carp::croak("Did not provide a width to Imager::Search::Pattern::regexp"); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # Return the cached version if possible | ||||||||||||||||||||||||||||
| 183 | 6 | ▶ | if ( $self->cache and $self->{regexp}->{$width} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->cache and $self->{"regexp"}{$width}
MC/DC0%0/2
$self->cache and $self->{"regexp"}{$width}
$self->cache
$self->{"regexp"}{$width}
Truth table33%1/3
A and B
A= $self->cacheB= $self->{"regexp"}{$width}
| |||||||||||||||||||||||||||||
| 184 | 0 | return $self->{regexp}->{$width}; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # Hand off to the driver to build the regexp | ||||||||||||||||||||||||||||
| 188 | 6 | my $regexp = $self->driver->pattern_regexp( $self, $width ); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Cache if needed | ||||||||||||||||||||||||||||
| 191 | 6 | ▶ | if ( $self->cache ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 0 | $self->{regexp}->{$width} = $regexp; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 6 | return $regexp; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 5 | 1; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =pod | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | See the SUPPORT section of the main L<Imager::Search> module. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Copyright 2007 - 2011 Adam Kennedy. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 215 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 218 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||||||||||