| 1 | package Imager::Search::Image; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Imager::Search::Image - Generic interface for a searchable image | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | L<Imager::Search::Image> is an abstract base class for objects that | ||||||||||||||||||||||||||||
| 12 | implement an image to be searched. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 METHODS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use Params::Util qw{ _IDENTIFIER _POSINT _INSTANCE _DRIVER }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use vars qw{$VERSION}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 5 | $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | use Object::Tiny::XS qw{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | name | ||||||||||||||||||||||||||||
| 29 | driver | ||||||||||||||||||||||||||||
| 30 | file | ||||||||||||||||||||||||||||
| 31 | image | ||||||||||||||||||||||||||||
| 32 | height | ||||||||||||||||||||||||||||
| 33 | width | ||||||||||||||||||||||||||||
| 34 | string | ||||||||||||||||||||||||||||
| 35 | 5 | }; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | ###################################################################### | ||||||||||||||||||||||||||||
| 42 | # Constructor and Accessors | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=27.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 46 | 4 | my $self = bless { @_ }, $class; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Check the driver | ||||||||||||||||||||||||||||
| 49 | 4 | ▶ | if ( _IDENTIFIER($self->driver) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | $self->{driver} = "Imager::Search::Driver::" . $self->driver; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 4 | ▶ | if ( _DRIVER($self->driver, 'Imager::Search::Driver') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 4 | $self->{driver} = $self->driver->new; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 4 | ▶ | unless ( _INSTANCE($self->driver, 'Imager::Search::Driver') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | Carp::croak("Did not provide a valid driver"); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 4 | ▶ | if ( defined $self->file and not defined $self->image ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->file and not defined $self->image
MC/DC0%0/2
defined $self->file and not defined $self->image
defined $self->file
not defined $self->image
Truth table33%1/3
A and not(B)
A= defined $self->fileB= not defined $self->image
| |||||||||||||||||||||||||||||
| 59 | # Load the image from a file | ||||||||||||||||||||||||||||
| 60 | 4 | $self->{image} = Imager->new; | |||||||||||||||||||||||||||
| 61 | 4 | $self->{image}->read( file => $self->file ); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 4 | ▶ | if ( defined $self->image ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 4 | ▶ | unless( _INSTANCE($self->image, 'Imager') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | Carp::croak("Did not provide a valid image"); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 4 | $self->{height} = $self->image->getheight; | |||||||||||||||||||||||||||
| 68 | 4 | $self->{width} = $self->image->getwidth; | |||||||||||||||||||||||||||
| 69 | 4 | $self->{string} = $self->driver->image_string($self->image); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 4 | ▶ | unless ( _POSINT($self->height) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | Carp::croak("Invalid or missing image height"); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 4 | ▶ | unless ( _POSINT($self->width) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | Carp::croak("Invalid or missing image width"); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | return $self; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | ##################################################################### | ||||||||||||||||||||||||||||
| 86 | # Search Methods | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =pod | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head2 find | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | The C<find> method compiles the search and target images in memory, and | ||||||||||||||||||||||||||||
| 93 | executes a single search, returning the position of the first match as a | ||||||||||||||||||||||||||||
| 94 | L<Imager::Search::Match> object. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | ▶ | sub find { | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | 3 | my $pattern = _INSTANCE(shift, 'Imager::Search::Pattern'); | |||||||||||||||||||||||||||
| 101 | 3 | ▶ | unless ( $pattern ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | die "Did not pass a Pattern object to find"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Run the search | ||||||||||||||||||||||||||||
| 106 | 3 | my @match = (); | |||||||||||||||||||||||||||
| 107 | 3 | my $string = $self->string; | |||||||||||||||||||||||||||
| 108 | 3 | my $regexp = $pattern->regexp( $self ); | |||||||||||||||||||||||||||
| 109 | 3 | ▶ | while ( scalar $$string =~ /$regexp/g ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 7 | my $p = $-[0]; | |||||||||||||||||||||||||||
| 111 | 7 | push @match, $self->driver->match_object( $self, $pattern, $p ); | |||||||||||||||||||||||||||
| 112 | 7 | pos $$string = $p + 1; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3 | return @match; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | ▶ | sub find_any { | ||||||||||||||||||||||||||
|
Subroutine
find_any: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 3 | my $pattern = _INSTANCE(shift, 'Imager::Search::Pattern'); | |||||||||||||||||||||||||||
| 121 | 3 | ▶ | unless ( $pattern ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | die "Did not pass a Pattern object to find"; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # Run the search | ||||||||||||||||||||||||||||
| 126 | 3 | my $string = $self->string; | |||||||||||||||||||||||||||
| 127 | 3 | my $regexp = $pattern->regexp( $self ); | |||||||||||||||||||||||||||
| 128 | 3 | ▶ | while ( scalar $$string =~ /$regexp/gs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 3 | my $p = $-[0]; | |||||||||||||||||||||||||||
| 130 | 3 | ▶ | if ( defined $self->driver->match_object( $self, $pattern, $p ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 3 | return 1; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | pos $$string = $p + 1; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 0 | return ''; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 5 | 1; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =pod | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | See the SUPPORT section of the main L<Imager::Search> module. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Copyright 2007 - 2011 Adam Kennedy. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 155 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 158 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||