| 1 | package Math::FitRect; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 1 | $Math::FitRect::VERSION = '0.05'; | |||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | Math::FitRect - Resize one rect in to another while preserving aspect ratio. | ||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | use Math::FitRect; | ||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | # This will return: {w=>40, h=>20, x=>0, y=>10} | ||||||||||||||||||||||
| 14 | my $rect = fit_rect( [80,40] => 40 ); | ||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | # This will return: {w=>80, h=>40, x=>-19, y=>0} | ||||||||||||||||||||||
| 17 | my $rect = crop_rect( [80,40] => 40 ); | ||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | This module is very simple in its content but can save much time, much like | ||||||||||||||||||||||
| 22 | other simplistic modules like L<Data::Pager>. This module is useful for | ||||||||||||||||||||||
| 23 | calculating what size you should resize images as for such things as | ||||||||||||||||||||||
| 24 | thumbnails. | ||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 29 | 1 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | 1 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 34 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||||||
| 35 | fit_rect | ||||||||||||||||||||||
| 36 | crop_rect | ||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | =head1 RECTANGLES | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | Rectangles may be specified in several different forms to fit your needs. | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | =over | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | =item A simple scalar integer containg the pixel width/height of a square. | ||||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | =item An array ref containing the width and height of a rectangle: [$width,$height] | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | =item A hash ref containg a w (width) and h (height) key: {w=>$width,h=>$height} | ||||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | =back | ||||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | =head1 FUNCTIONS | ||||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | =head2 fit_rect | ||||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | # This will return: {w=>40, h=>20, x=>0, y=>10} | ||||||||||||||||||||||
| 58 | my $rect = fit_rect( [80,40] => 40 ); | ||||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | Takes two rectangles and fits the first one inside the second one. The rectangle | ||||||||||||||||||||||
| 61 | that will be returned will be a hash ref with a 'w' and 'h' parameter as well | ||||||||||||||||||||||
| 62 | as 'x' and 'y' parameters which will specify any offset. | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | 4 | ▶ | sub fit_rect { | ||||||||||||||||||||
|
Subroutine
fit_rect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 67 | 4 | return _calc_rect('fit',@_); | |||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | =head2 crop_rect | ||||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | # This will return: {w=>80, h=>40, x=>-19, y=>0} | ||||||||||||||||||||||
| 73 | my $rect = crop_rect( [80,40] => 40 ); | ||||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | Like the fit_rect function, crop_rect takes two rectangles as a parameter and it | ||||||||||||||||||||||
| 76 | makes $rect1 completely fill $rect2. This can mean that the top and bottom or | ||||||||||||||||||||||
| 77 | the left and right get chopped off (cropped). This method returns a hash ref just | ||||||||||||||||||||||
| 78 | like fit_rect. | ||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||
| 81 | |||||||||||||||||||||||
| 82 | 4 | ▶ | sub crop_rect { | ||||||||||||||||||||
|
Subroutine
crop_rect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 83 | 4 | return _calc_rect('crop',@_); | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | 8 | ▶ | sub _calc_rect { | ||||||||||||||||||||
|
Subroutine
_calc_rect: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 87 | 8 | my($type,$from,$to) = @_; | |||||||||||||||||||||
| 88 | 8 | $from = _normalize($from); | |||||||||||||||||||||
| 89 | 8 | $to = _normalize($to); | |||||||||||||||||||||
| 90 | 8 | my($w,$h,$x,$y); | |||||||||||||||||||||
| 91 | 8 | ▶ | if($type eq 'crop'){ ($to->{r},$from->{r}) = ($from->{r},$to->{r}); } | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | 8 | ▶ | if($from->{r} < $to->{r}){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 94 | 2 | $w = $from->{w} * ($to->{h}/$from->{h}); | |||||||||||||||||||||
| 95 | 2 | $h = $to->{h}; | |||||||||||||||||||||
| 96 | 2 | $x = ($to->{w}-$w)/2; | |||||||||||||||||||||
| 97 | 2 | $y = 0; | |||||||||||||||||||||
| 98 | }else{ | ||||||||||||||||||||||
| 99 | 6 | $h = $from->{h} * ($to->{w}/$from->{w}); | |||||||||||||||||||||
| 100 | 6 | $w = $to->{w}; | |||||||||||||||||||||
| 101 | 6 | $y = ($to->{h}-$h)/2; | |||||||||||||||||||||
| 102 | 6 | $x = 0; | |||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | 8 | return {w=>int($w+0.5),h=>int($h+0.5),x=>int($x+0.5),y=>int($y+0.5)}; | |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | 16 | ▶ | sub _normalize { | ||||||||||||||||||||
|
Subroutine
_normalize: called CC=7 SCAR=28.3
| |||||||||||||||||||||||
| 109 | 16 | my $rect = shift; | |||||||||||||||||||||
| 110 | 16 | my($w,$h,$r); | |||||||||||||||||||||
| 111 | 16 | ▶ | if(!ref($rect)){ # square | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 112 | 8 | $w = $h = $rect; | |||||||||||||||||||||
| 113 | }elsif(ref($rect) eq 'HASH'){ # rect hash ref | ||||||||||||||||||||||
| 114 | 0 | $w = $rect->{w}; | |||||||||||||||||||||
| 115 | 0 | $h = $rect->{h}; | |||||||||||||||||||||
| 116 | }elsif(@$rect==2){ # width, height | ||||||||||||||||||||||
| 117 | 8 | $w = $rect->[0]; | |||||||||||||||||||||
| 118 | 8 | $h = $rect->[1]; | |||||||||||||||||||||
| 119 | }elsif(@$rect==4){ # x1, y1, x2, y2 | ||||||||||||||||||||||
| 120 | 0 | ▶ | if($rect->[0]<$rect->[2]){ $w=($rect->[2]-$rect->[0])+1; } | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 121 | 0 | else{ $w=($rect->[0]-$rect->[2])+1; } | |||||||||||||||||||||
| 122 | 0 | ▶ | if($rect->[1]<$rect->[3]){ $h=($rect->[3]-$rect->[1])+1; } | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 123 | 0 | else{ $h=($rect->[1]-$rect->[3])+1; } | |||||||||||||||||||||
| 124 | }else{ | ||||||||||||||||||||||
| 125 | 0 | croak('Invalid rectangle parameter'); | |||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||
| 127 | 16 | $r = $w/$h; | |||||||||||||||||||||
| 128 | 16 | return {w=>$w,h=>$h,r=>$r}; | |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | 1 | 1; | |||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||