#
# A Module for parsing images
#

package Pictures;

sub new {
	my ($class) = shift;
	my($filename) = shift;
	bless {
		"filename" => $filename
	}, $class;
}

sub name {
	my($self) = shift;
	return $self->{"filename"};
}

sub size {
	my($self) = shift;
	my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($self->{"filename"});
	return $size;
}

sub setHW {
	my($self) = shift;
	my($data) = `file $self->{"filename"}`; #"
	chomp($data);

	#
	# Strip of any trailing commas
	if (substr($data, length($data)-1, 1) eq ",") {
		chop($data);
	}
	my(@words) = split(" ", $data);
	my($i);
	#
	# Look for two words seperated by a 'x'
	#
	for($i =  $#words; $i >=2; $i--) {
		#print "Checking $i: $words[$i]\n";
		if ($words[$i] =~ /^\d+$/ && $words[$i-1] eq "x" && $words[$i-2] =~ /^\d+$/) {
			$self->{"height"} = $words[$i];
			$self->{"width"} = $words[$i-2];
			$i = -9999999;
		}
	}
}

sub height {
	my($self) = shift;
	if (!defined($self->{"height"})) {
		$self->setHW();
	}
	return $self->{"height"};
}
sub width {
	my($self) = shift;
	if (!defined($self->{"height"})) {
		$self->setHW();
	}
	return $self->{"height"};
}


1;