Imager::Search::Match - Object describing a successful Imager::Search match


Imager-Search documentation Contained in the Imager-Search distribution.

Index


Code Index:

NAME

Top

Imager::Search::Match - Object describing a successful Imager::Search match

DESCRIPTION

Top

Imager::Search::Match is a convenience class that represents the complete geometry of a successful Imager::Search match.

It is returned by the various search methods in Imager::Search.

Imager::Search::Match objects are self-contained and anonymous, they do not retain a connection to the original search context.

METHODS

Top

name

If the Imager::Search::Pattern that was used to generate the match object had a name, then the match will inherit that name as well.

Returns the pattern name as a string, or undef if the pattern was anonymous.

top

The top accessor returns the integer value of the inclusive vertical top of the search match.

bottom

The bottom accessor returns the integer value of the inclusive vertical bottom of the search match.

left

Top

The left accessor returns the integer value of the inclusive horizontal left of the search match.

right

Top

The right accessor returns the integer value of the inclusive horizontal right of the search match.

height

Top

The height accessor returns the integer value of the vertical height of the matched area.

width

Top

The width accessor returns the integer value of the horizontal width of the matched area.

center_x

Top

The center_x accessor returns the integer value of the horizontal centre pixel of the matched image. If the matched image has an even number of horizonal pixels, the value will be rounded to the left.

center_y

Top

The center_y accessor returns the integer value of the vertical centre pixel of the matched image. If the matched image has an even number of vertical pixels, the value will be rounded to the top.

SUPPORT

Top

See the SUPPORT section of the main Imager::Search module.

AUTHOR

Top

Adam Kennedy <adamk@cpan.org>

COPYRIGHT

Top


Imager-Search documentation Contained in the Imager-Search distribution.

package Imager::Search::Match;

use 5.006;
use strict;
use Carp         ();
use Params::Util qw{ _POSINT _NONNEGINT _INSTANCE };

use vars qw{$VERSION};
BEGIN {
	$VERSION = '1.01';
}

use Object::Tiny::XS qw{
	name
	top
	bottom
	left
	right
	height
	width
	center_x
	center_y
};





#####################################################################
# Constructor

sub new {
	my $self = shift->SUPER::new(@_);

	# Temporary checks for the basic geometry
	defined(_NONNEGINT($self->top))    or die "Missing or invalid top value";
	defined(_NONNEGINT($self->left))   or die "Missing or invalid left value";
	defined(_NONNEGINT($self->height)) or die "Missing or invalid height value";
	defined(_NONNEGINT($self->width))  or die "Missing or invalid width value";

	# Assume that we've been provided the basic (top/left/height/width)
	# geometry, and from that derive the rest.
	$self->{bottom}   = $self->top  + $self->height - 1;
	$self->{right}    = $self->left + $self->width  - 1;
	$self->{center_x} = int( ($self->left + $self->right ) / 2 );
	$self->{center_y} = int( ($self->top  + $self->bottom) / 2 );

	return $self;
}

sub from_position {
	my $class    = shift;
	my $image    = _INSTANCE(shift, 'Imager::Search::Image');
	my $pattern  = _INSTANCE(shift, 'Imager::Search::Pattern');
	my $position = _NONNEGINT(shift);

	# Check params
	unless ( $image ) {
		Carp::croak("Failed to provide Imager::Search::Image param");
	}
	unless ( $pattern ) {
		Carp::croak("Failed to provide Imager::Search::Pattern param");
	}
	unless ( defined $position ) {
		Carp::croak("Failed to provide position");
	}

	# Derive additional values from the basic values
	my $width    = $pattern->width;
	my $height   = $pattern->height;
	my $top      = int($position / $image->width);
	my $left     = $position % $image->width;
	my $bottom   = $top + $height - 1;
	my $right    = $left + $width - 1;
	my $center_x = int(($left + $right)  / 2);
	my $center_y = int(($top  + $bottom) / 2);

	# Create the object
	return $class->new(
		left     => $left,
		right    => $right,
		top      => $top,
		bottom   => $bottom,
		height   => $height,
		width    => $width,
		center_x => $center_x,
		center_y => $center_y,
	);
}

BEGIN {
	*centre_x = *center_x;
	*centre_y = *center_y;
}

1;

__END__