This file is indexed.

/usr/share/perl5/Image/Math/Constrain.pm is in libimage-math-constrain-perl 1.02-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package Image::Math::Constrain;

=pod

=head1 NAME

Image::Math::Constrain - Scaling math used in image size constraining (such
as thumbnails)

=head1 SYNOPSIS

  use Image::Math::Constrain;
  
  # Create the math object
  my $math = Image::Math::Constrain->new(64, 48);
  
  # Get the scaling values for an arbitrary image
  my $Image = My::Image->load("myimage.jpg");
  my $scaling = $math->constrain($Image->width, $Image->height);
  die "Don't need to scale" if $scaling->{scale} == 1;
  
  # Returns the three values as a list when called in array contect
  my ($width, $height, $scale) = $math->constrain(800, 600);

  # There are lots of different ways to specify the constrain
  
  # Constrain based on width only
  $math = Image::Math::Constrain->new(100, 0);
  
  # Constrain based on height only
  $math = Image::Math::Constrain->new(0, 100);

  # Or you can provide the two values by ARRAY ref
  $math = Image::Math::Constrain->new( [ 64, 48 ] );
  
  # Constrain height and width by the same value
  $math = Image::Math::Constrain->new(100);
  
  # Various string forms to do the same thing
  $math = Image::Math::Constrain->new('constrain(800x600)');
  $math = Image::Math::Constrain->new('300x200');
  $math = Image::Math::Constrain->new('300w200h');
  $math = Image::Math::Constrain->new('100w');
  $math = Image::Math::Constrain->new('100h');
  
  # Serialises back to 'constrain(800x600)'.
  # You can use this to store the object if you wish.
  my $string = $math->as_string;

=head1 DESCRIPTION

There are a number of different modules and systems that constrain image
sizes, such as thumbnailing. Every one of these independantly implement
the same logic. That is, given a width and/or height constraint, they
check to see if the image is bigger than the constraint, and if so scale
the image down proportionally so that it fits withint the constraints.

Of course, they all do it slightly differnetly, and some do it better
than others.

C<Image::Math::Constrain> has been created specifically to implement
this logic once, and implement it properly. Any module or script that
does image size constraining or thumbnailing should probably be using
this for its math.

=head1 METHODS

=cut

use 5.005;
use strict;
use overload 'bool' => sub () { 1 },
             '""'   => 'as_string';

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





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

=pod

=head2 new $width, $height

-head2 new [ $width, $height ]

=head1 new $width_and_height

=head2 new $string

The C<new> constructor takes the dimentions to which you wish to
constrain and creates a new math object.

You can feed a number of different height/width pairs to this object, and
it will returns the scaling you will need to do to shrink the image down
to the constraints, and the final width and height of the image after
scaling, at least one of which should match the constraint.

A value of zero is used to indicate that a dimension should not be
constrained. Thus, C<-E<gt>new(400, 0)> would indicate to constrain the
width to 400 pixels, but to ignore the height (only changing it to keep
the image proportional).

The constraint dimensions can be provided in a number of different
formats. See the Synopsis for a quick list of these. To stay
compatible with automated constraint generators, you B<can> provide
constrains as zero width and zero height, and the math object will not
attempt to do any scaling, always returning the input width/height,
and a scaling value of 1.

Once created, the object is fully Storable and re-usable and does not
store any state information from a single calculation run.

Returns a new Image::Math::Constrain object, or C<undef> if the
constraints have been defined wrongly.

=cut

sub new {
	my $class = ref $_[0] ? ref shift : shift;

	# Expand a single argument
	if ( @_ == 1 ) {
		my $value = defined $_[0] ? shift : return undef;
		if ( ref $value eq 'ARRAY' and @$value == 2 ) {
			return $class->new(@$value);
		}
		return undef if ref $value;
		$value =~ s/\s//g;
		# constrain(800x600)
		return $class->new("$1", "$2") if $value =~ /^constrain\((\d+)x(\d+)\)$/;
		# 800x600
		return $class->new("$1", "$2") if $value =~ /^(\d+)x(\d+)$/;
		# 800w600h
		return $class->new("$1", "$2") if $value =~ /^(\d+)w(\d+)h$/;
		# 800w (width only)
		return $class->new("$1", 0)    if $value =~ /^(\d+)w$/;
		# 800h (height only)
		return $class->new(0, "$1")    if $value =~ /^(\d+)h$/;
		# 800 (meaning 800x800)
		if ( $class->_non_neg_int($value) ) {
			return $class->new($value, $value);
		}
		return undef;
	}

	# The two argument form
	return undef unless @_ == 2;
	my $self = bless {}, $class;
	$self->{width}  = $class->_non_neg_int($_[0]) ? shift : return undef;
	$self->{height} = $class->_non_neg_int($_[0]) ? shift : return undef;
	$self;
}

=pod

=head2 width

The C<width> method gets the width constraint for the object.

Returns a positive integer, or zero if there is no width constraint.

=cut

sub width  { $_[0]->{width} }

=pod

=head2 height

The C<height> method gets the height constrain for the object.

Returns a positive integer, or zero if there is no height constraint.

=cut

sub height { $_[0]->{height} }

=pod

=head2 as_string

The C<as_string> method returns the constrain rule as a string in the
format 'constrain(123x123)'. This string form is also supported by the
constructor and so it provides a good way to serialise the constrain
rule, should you ever need to do so.

As this value is not localisable, it should never really be shown to the
user directly, unless you are sure you will never add i18n to your app.

=cut

sub as_string {
	"constrain($_[0]->{width}x$_[0]->{height})";
}

=pod

=head2 constrain $width, $height

The C<constrain> method takes the height and width of an image and
applies the constrain math to them to get the final width, height
and the scaling value needed in order to get the your image from
it's current size to the final size.

The resulting size will be in proportion to the original (it will have
the same aspect ratio) and will never be larger than the original.

When called in array context, returns the new dimensions and scaling value
as a list, as in the following.

  my ($width, $height, $scale) = $math->constrain(800, 600);

When called in scalar context, it returns a reference to a hash containing
the keys 'width', 'height', and 'scale'.

  my $hash = $math->constrain(800, 600);
  
  print "New Width  : $hash->{width}\n";
  print "New Height : $hash->{height}\n";
  print "Scaling By : $hash->{scalar}\n";

Having been created correctly, the object will only return an error if the
width and height arguments are not correct (are not positive integers).

In list context, returns a null list, so all three values will be C<undef>.

In scalar context, just returns C<undef>.

=cut

sub constrain {
	my $self   = shift;
	my $width  = $self->_pos_int($_[0]) ? shift : return;
	my $height = $self->_pos_int($_[0]) ? shift : return;
	unless ( $self->{width} or $self->{height} ) {
		return $self->_ret_val(wantarray, $width, $height, 1);
	}

	# Determine the prefered scaling in both dimensions
	my $w_scale = ($self->{width} and $self->{width} < $width)
		? ($self->{width} / $width) : 1;
	my $h_scale = ($self->{height} and $self->{height} < $height)
		? ($self->{height} / $height) : 1;

	# Do we need to scale?
	if ( $w_scale == 1 and $h_scale == 1 ) {
		return $self->_ret_val(wantarray, $width, $height, 1);
	}

	# Use the smaller scaling value to scale the dimentions
	my $scale = $w_scale < $h_scale ? $w_scale : $h_scale;
	$width  *= $scale;
	$height *= $scale;

	$self->_ret_val(wantarray, $width, $height, $scale);
}





#####################################################################
# Support Methods

# Validate a non-negative integer
sub _non_neg_int {
	my $value = defined $_[1] ? $_[1] : return '';
	return '' if ref $value;
	return 1  if $value eq '0';
	!! $value =~ /^[1-9]\d*$/;
}

# Validate a positive integer
sub _pos_int {
	my $value = defined $_[1] ? $_[1] : return '';
	return '' if ref $value;
	!! $value =~ /^[1-9]\d*$/;
}

# Return as either a list or HASH reference
sub _ret_val {
	my $self = shift;
	shift(@_) ? @_ # wantarray
	: { width => shift, height => shift, scale  => shift };
}

1;

=pod

=head1 TO DO

- Write more special-case unit tests

=head1 SUPPORT

Bugs should always be submitted via the CPAN bug tracker

L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Image-Math-Constrain>

For other issues, contact the maintainer

=head1 AUTHORS

Adam Kennedy E<lt>adamk@cpan.orgE<gt>

Thank you to Phase N (L<http://phase-n.com/>) for permitting
the open sourcing and release of this distribution.

=head1 COPYRIGHT

Copyright 2004 - 2008 Adam Kennedy.

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.

=cut