| 1 | package Math::Image::CalcResized; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||||||||||
| 10 | 1 | our $DATE = '2021-10-12'; # DATE | |||||||||||||||||||||||||||||||||||
| 11 | 1 | our $DIST = 'Math-Image-CalcResized'; # DIST | |||||||||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '0.005'; # VERSION | |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 1 | our @EXPORT_OK = qw(calc_image_resized_size); | |||||||||||||||||||||||||||||||||||
| 15 | 1 | our %SPEC; | |||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||
| 17 | 1 | $SPEC{':package'} = { | |||||||||||||||||||||||||||||||||||
| 18 | v => 1.1, | ||||||||||||||||||||||||||||||||||||
| 19 | summary => 'Calculate dimensions of image/video resized by ImageMagick-like geometry specification', | ||||||||||||||||||||||||||||||||||||
| 20 | }; | ||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | sub _calc_or_human { | ||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my ($action, %args) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_calc_or_human: not called CC=62 SCAR=82.7
| |||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||
| 25 | 0 | my $size; | |||||||||||||||||||||||||||||||||||
| 26 | 0 | my ($w, $h); | |||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | if ($action eq 'calc') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | $size = $args{size} or return [400, "Please specify image size"]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | $size =~ /\A(\d+)x(\d+)\z/ or return [400, "Invalid size format, please use <width>x<height> syntax"]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 30 | 0 | ($w, $h) = ($1, $2); | |||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $resize = $args{resize}; defined $resize or return [400, "Please specify resize"]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 33 | 0 | my ($w2, $h2) = ($w, $h); | |||||||||||||||||||||||||||||||||||
| 34 | 0 | my $human_general = "no resizing"; | |||||||||||||||||||||||||||||||||||
| 35 | 0 | my $human_specific; | |||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | goto SKIP unless length $resize; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | # some instructions are translated to other first | ||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($resize =~ /\A(\d+)\^([<>])\z/) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $human_general = ($2 eq '>' ? "shrink" : "enlarge") . " shortest side to ${1}px"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ($w < $h) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 44 | 0 | $resize = "$1$2"; | |||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $human_specific = ($2 eq '>' ? "shrink" : "enlarge") . " shortest side (width) to ${1}px"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 46 | } else { | ||||||||||||||||||||||||||||||||||||
| 47 | 0 | $resize = "x$1$2"; | |||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $human_specific = ($2 eq '>' ? "shrink" : "enlarge") . " shortest side (height) to ${1}px"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||
| 50 | } elsif ($resize =~ /\A\^(\d+)([<>])\z/) { | ||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $human_general = ($2 eq '>' ? "shrink" : "enlarge") . " longest side to ${1}px"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($w > $h) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 54 | 0 | $resize = "$1$2"; | |||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $human_specific = ($2 eq '>' ? "shrink" : "enlarge") . " longest side (width) to ${1}px"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||||||||||
| 57 | 0 | $resize = "x$1$2"; | |||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $human_specific = ($2 eq '>' ? "shrink" : "enlarge") . " longest side (height) to ${1}px"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ($resize =~ /\A(\d+(?:\.\d*)?)%\z/) { | ||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||
| 63 | 0 | $human_general = "scale to $resize"; | |||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 65 | 0 | $w2 = $1/100 * $w; | |||||||||||||||||||||||||||||||||||
| 66 | 0 | $h2 = $1/100 * $h; | |||||||||||||||||||||||||||||||||||
| 67 | 0 | $human_specific = "scale to $resize (${w2}px)"; | |||||||||||||||||||||||||||||||||||
| 68 | } elsif ($resize =~ /\A(\d+(?:\.\d*)?)%?x(\d+(?:\.\d*)?)%\z/) { | ||||||||||||||||||||||||||||||||||||
| 69 | 0 | $human_general = "scale width to ${1}%, height to ${2}%"; | |||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 71 | 0 | $w2 = $1/100 * $w; | |||||||||||||||||||||||||||||||||||
| 72 | 0 | $h2 = $2/100 * $h; | |||||||||||||||||||||||||||||||||||
| 73 | 0 | $human_specific = "scale width to ${1}% (${w2}px), height to ${2}% (${h2}px)"; | |||||||||||||||||||||||||||||||||||
| 74 | } elsif ($resize =~ /\A(\d+)([>^<]?)\z/) { | ||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $which = $2; | |||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($which eq '>') { # shrink | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 77 | 0 | $human_general = "shrink width to ${1}px"; | |||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | goto SKIP if $w <= $1; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 80 | ▶ | } elsif ($which eq '^' || $which eq '<') { # enlarge | |||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$which eq "^" or $which eq "<"
MC/DC0%0/2
$which eq "^" or $which eq "<"
$which eq "^"
$which eq "<"
Truth table0%0/3
A or B
A= $which eq "^"B= $which eq "<"
| |||||||||||||||||||||||||||||||||||||
| 81 | 0 | $human_general = "enlarge width to ${1}px"; | |||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | goto SKIP if $w >= $1; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 84 | } else { | ||||||||||||||||||||||||||||||||||||
| 85 | 0 | $human_general = "set width to ${1}px"; | |||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||
| 89 | 0 | $w2 = $1; | |||||||||||||||||||||||||||||||||||
| 90 | 0 | $h2 = ($h/$w) * $w2; | |||||||||||||||||||||||||||||||||||
| 91 | 0 | $human_specific = $human_general; | |||||||||||||||||||||||||||||||||||
| 92 | } elsif ($resize =~ /\Ax(\d+)([>^<]?)\z/) { | ||||||||||||||||||||||||||||||||||||
| 93 | 0 | my $which = $2; | |||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ($which eq '>') { # shrink | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 95 | 0 | $human_general = "shrink height to ${1}px"; | |||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | goto SKIP if $h <= $1; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 98 | ▶ | } elsif ($which eq '^' || $which eq '<') { # enlarge | |||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$which eq "^" or $which eq "<"
MC/DC0%0/2
$which eq "^" or $which eq "<"
$which eq "^"
$which eq "<"
Truth table0%0/3
A or B
A= $which eq "^"B= $which eq "<"
| |||||||||||||||||||||||||||||||||||||
| 99 | 0 | $human_general = "enlarge height to ${1}px"; | |||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | goto SKIP if $h >= $1; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||||||||||
| 103 | 0 | $human_general = "set height to ${1}px"; | |||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||
| 107 | 0 | $h2 = $1; | |||||||||||||||||||||||||||||||||||
| 108 | 0 | $w2 = ($w/$h) * $h2; | |||||||||||||||||||||||||||||||||||
| 109 | 0 | $human_specific = $human_general; | |||||||||||||||||||||||||||||||||||
| 110 | } elsif ($resize =~ /\A(\d+)x(\d+)([<>!^]?)\z/) { | ||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $which = $3; | |||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ($which eq '' || $which eq '>') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
$which eq "" or $which eq ">"
MC/DC0%0/2
$which eq "" or $which eq ">"
$which eq ""
$which eq ">"
Truth table0%0/3
A or B
A= $which eq ""B= $which eq ">"
| |||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ($which eq '>') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 114 | 0 | $human_general = "shrink image to fit inside ${1}x${2}"; | |||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | goto SKIP if $w <= $1 || $h <= $2; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$w <= $1 or $h <= $2
MC/DC0%0/2
$w <= $1 or $h <= $2
$w <= $1
$h <= $2
Truth table0%0/3
A or B
A= $w <= $1B= $h <= $2
| |||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||
| 119 | 0 | $human_general = "fit image inside ${1}x${2}"; | |||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($h2 > $2) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 123 | 0 | $h2 = $2; | |||||||||||||||||||||||||||||||||||
| 124 | 0 | $w2 = ($w/$h) * $h2; | |||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ($w2 > $1) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 127 | 0 | $h2 = $1/$w2 * $h2; | |||||||||||||||||||||||||||||||||||
| 128 | 0 | $w2 = $1; | |||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||
| 130 | 0 | $human_specific = $human_general; | |||||||||||||||||||||||||||||||||||
| 131 | ▶ | } elsif ($which eq '^' || $which eq '<') { | |||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$which eq "^" or $which eq "<"
MC/DC0%0/2
$which eq "^" or $which eq "<"
$which eq "^"
$which eq "<"
Truth table0%0/3
A or B
A= $which eq "^"B= $which eq "<"
| |||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ($which eq '<') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 133 | 0 | $human_general = "enlarge image to fit ${1}x${2} inside it"; | |||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | goto SKIP if $w >= $1 || $h >= $2; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$w >= $1 or $h >= $2
MC/DC0%0/2
$w >= $1 or $h >= $2
$w >= $1
$h >= $2
Truth table0%0/3
A or B
A= $w >= $1B= $h >= $2
| |||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||
| 138 | 0 | $human_general = "fit image to fit ${1}x${2} inside it"; | |||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ($h2 < $2) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 142 | 0 | $h2 = $2; | |||||||||||||||||||||||||||||||||||
| 143 | 0 | $w2 = ($w/$h) * $h2; | |||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if ($w2 < $1) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 146 | 0 | $h2 = $1/$w2 * $h2; | |||||||||||||||||||||||||||||||||||
| 147 | 0 | $w2 = $1; | |||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||
| 149 | 0 | $human_specific = $human_general; | |||||||||||||||||||||||||||||||||||
| 150 | } elsif ($which eq '!') { | ||||||||||||||||||||||||||||||||||||
| 151 | 0 | $human_general = "set dimension to ${1}x${2}"; | |||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | goto SKIP unless $action eq 'calc'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||
| 154 | 0 | $w2 = $1; | |||||||||||||||||||||||||||||||||||
| 155 | 0 | $h2 = $2; | |||||||||||||||||||||||||||||||||||
| 156 | 0 | $human_specific = $human_general; | |||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||
| 158 | } else { | ||||||||||||||||||||||||||||||||||||
| 159 | 0 | return [400, "Unrecognized resize instruction '$resize'"]; | |||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||
| 162 | SKIP: | ||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if ($action eq 'human') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 164 | 0 | [200, "OK", $human_general]; | |||||||||||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||||||||||
| 166 | 0 | [200, "OK", sprintf("%dx%d", $w2, $h2), { | |||||||||||||||||||||||||||||||||||
| 167 | 'func.human_general' => $human_general, | ||||||||||||||||||||||||||||||||||||
| 168 | 'func.human_specific' => $human_specific, | ||||||||||||||||||||||||||||||||||||
| 169 | }]; | ||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||
| 173 | $SPEC{calc_image_resized_size} = { | ||||||||||||||||||||||||||||||||||||
| 174 | 1 | v => 1.1, | |||||||||||||||||||||||||||||||||||
| 175 | summary => 'Given size of an image (in WxH, e.g. "2592x1944") and ImageMagick-like resize instruction (e.g. "1024p>"), calculate new resized image', | ||||||||||||||||||||||||||||||||||||
| 176 | args => { | ||||||||||||||||||||||||||||||||||||
| 177 | size => { | ||||||||||||||||||||||||||||||||||||
| 178 | summary => 'Image/video size, in <width>x<height> format, e.g. 2592x1944', | ||||||||||||||||||||||||||||||||||||
| 179 | schema => ['str*', match=>qr/\A\d+x\d+\z/], | ||||||||||||||||||||||||||||||||||||
| 180 | req => 1, | ||||||||||||||||||||||||||||||||||||
| 181 | pos => 0, | ||||||||||||||||||||||||||||||||||||
| 182 | description => <<'_', | ||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||
| 184 | _ | ||||||||||||||||||||||||||||||||||||
| 185 | }, | ||||||||||||||||||||||||||||||||||||
| 186 | resize => { | ||||||||||||||||||||||||||||||||||||
| 187 | summary => 'Resize instruction, follows ImageMagick format', | ||||||||||||||||||||||||||||||||||||
| 188 | schema => 'str*', | ||||||||||||||||||||||||||||||||||||
| 189 | req => 1, | ||||||||||||||||||||||||||||||||||||
| 190 | pos => 1, | ||||||||||||||||||||||||||||||||||||
| 191 | description => <<'_', | ||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||
| 193 | Resize instruction can be given in several formats: | ||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||
| 195 | Syntax Meaning | ||||||||||||||||||||||||||||||||||||
| 196 | -------------------------- ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||
| 197 | "" No resizing. | ||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||
| 199 | SCALE"%" Height and width both scaled by specified percentage. | ||||||||||||||||||||||||||||||||||||
| 200 | SCALEX"%x"SCALEY"%" Height and width individually scaled by specified percentages. (Only one % symbol needed.) | ||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | WIDTH Width given, height automagically selected to preserve aspect ratio. | ||||||||||||||||||||||||||||||||||||
| 203 | WIDTH">" Shrink width if larger, height automagically selected to preserve aspect ratio. | ||||||||||||||||||||||||||||||||||||
| 204 | WIDTH"^" Enlarge width if smaller, height automagically selected to preserve aspect ratio. | ||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||
| 206 | "x"HEIGHT Height given, width automagically selected to preserve aspect ratio. | ||||||||||||||||||||||||||||||||||||
| 207 | "x"HEIGHT">" Shrink height if larger, width automagically selected to preserve aspect ratio. | ||||||||||||||||||||||||||||||||||||
| 208 | "x"HEIGHT"^" Enlarge height if smaller, width automagically selected to preserve aspect ratio. | ||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||
| 210 | WIDTH"x"HEIGHT Maximum values of height and width given, aspect ratio preserved. | ||||||||||||||||||||||||||||||||||||
| 211 | WIDTH"x"HEIGHT"^" Minimum values of height and width given, aspect ratio preserved. | ||||||||||||||||||||||||||||||||||||
| 212 | WIDTH"x"HEIGHT"!" Width and height emphatically given, original aspect ratio ignored. | ||||||||||||||||||||||||||||||||||||
| 213 | WIDTH"x"HEIGHT">" Shrinks an image with dimension(s) larger than the corresponding width and/or height argument(s). | ||||||||||||||||||||||||||||||||||||
| 214 | WIDTH"x"HEIGHT"<" Shrinks an image with dimension(s) larger than the corresponding width and/or height argument(s). | ||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||
| 216 | NUMBER"^>" Shrink shortest side if larger than number, aspect ratio preserved. | ||||||||||||||||||||||||||||||||||||
| 217 | NUMBER"^<" Enlarge shortest side if larger than number, aspect ratio preserved. | ||||||||||||||||||||||||||||||||||||
| 218 | "^"NUMBER">" Shrink longer side if larger than number, aspect ratio preserved. | ||||||||||||||||||||||||||||||||||||
| 219 | "^"NUMBER"<" Enlarge longer side if larger than number, aspect ratio preserved. | ||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||
| 221 | Currently unsupported: | ||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||
| 223 | AREA"@" Resize image to have specified area in pixels. Aspect ratio is preserved. | ||||||||||||||||||||||||||||||||||||
| 224 | X":"Y Here x and y denotes an aspect ratio (e.g. 3:2 = 1.5). | ||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||
| 226 | Ref: <http://www.imagemagick.org/script/command-line-processing.php#geometry> | ||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||
| 228 | _ | ||||||||||||||||||||||||||||||||||||
| 229 | }, | ||||||||||||||||||||||||||||||||||||
| 230 | }, | ||||||||||||||||||||||||||||||||||||
| 231 | examples => [ | ||||||||||||||||||||||||||||||||||||
| 232 | {args=>{size=>"2592x1944", resize=>""}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||
| 234 | {args=>{size=>"2592x1944", resize=>"20%"}, naked_result=>"518x388"}, | ||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||
| 236 | {args=>{size=>"2592x1944", resize=>"20%x40%"}, naked_result=>"518x777"}, | ||||||||||||||||||||||||||||||||||||
| 237 | {args=>{size=>"2592x1944", resize=>"20x40%"}, naked_result=>"518x777"}, | ||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||
| 239 | {args=>{size=>"2592x1944", resize=>"1024"}, naked_result=>"1024x768"}, | ||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||
| 241 | {args=>{size=>"2592x1944", resize=>"1024>"}, naked_result=>"1024x768"}, | ||||||||||||||||||||||||||||||||||||
| 242 | {args=>{size=>"2592x1944", resize=>"10240>"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||
| 244 | {args=>{size=>"2592x1944", resize=>"1024^"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 245 | {args=>{size=>"2592x1944", resize=>"10240^"}, naked_result=>"10240x7680"}, | ||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||
| 247 | {args=>{size=>"2592x1944", resize=>"x1024"}, naked_result=>"1365x1024"}, | ||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||
| 249 | {args=>{size=>"2592x1944", resize=>"x768>"}, naked_result=>"1024x768"}, | ||||||||||||||||||||||||||||||||||||
| 250 | {args=>{size=>"2592x1944", resize=>"x7680>"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||
| 252 | {args=>{size=>"2592x1944", resize=>"x768^"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 253 | {args=>{size=>"2592x1944", resize=>"x7680^"}, naked_result=>"10240x7680"}, | ||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||
| 255 | {args=>{size=>"2592x1944", resize=>"20000x10000"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 256 | {args=>{size=>"2592x1944", resize=>"20000x1000"}, naked_result=>"1333x1000"}, | ||||||||||||||||||||||||||||||||||||
| 257 | {args=>{size=>"2592x1944", resize=>"100x200"}, naked_result=>"100x75"}, | ||||||||||||||||||||||||||||||||||||
| 258 | {args=>{size=>"2592x1944", resize=>"100x100"}, naked_result=>"100x75"}, | ||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||
| 260 | {args=>{size=>"2592x1944", resize=>"10000x5000^"}, naked_result=>"10000x7500"}, | ||||||||||||||||||||||||||||||||||||
| 261 | {args=>{size=>"2592x1944", resize=>"5000x10000^"}, naked_result=>"13333x10000"}, | ||||||||||||||||||||||||||||||||||||
| 262 | {args=>{size=>"2592x1944", resize=>"100x100^"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||
| 264 | {args=>{size=>"2592x1944", resize=>"100x100!"}, naked_result=>"100x100"}, | ||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||
| 266 | {args=>{size=>"2592x1944", resize=>"10000x5000>"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 267 | {args=>{size=>"2592x1944", resize=>"5000x10000>"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 268 | {args=>{size=>"2592x1944", resize=>"3000x1000>"}, naked_result=>"2592x1944"}, #? | ||||||||||||||||||||||||||||||||||||
| 269 | {args=>{size=>"2592x1944", resize=>"2000x1000>"}, naked_result=>"1333x1000"}, | ||||||||||||||||||||||||||||||||||||
| 270 | {args=>{size=>"2592x1944", resize=>"100x100>"}, naked_result=>"100x75"}, | ||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||
| 272 | {args=>{size=>"2592x1944", resize=>"10000x5000<"}, naked_result=>"10000x7500"}, | ||||||||||||||||||||||||||||||||||||
| 273 | {args=>{size=>"2592x1944", resize=>"5000x10000<"}, naked_result=>"13333x10000"}, | ||||||||||||||||||||||||||||||||||||
| 274 | {args=>{size=>"2592x1944", resize=>"3000x1000<"}, naked_result=>"2592x1944"}, #? | ||||||||||||||||||||||||||||||||||||
| 275 | {args=>{size=>"2592x1944", resize=>"2000x1000<"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 276 | {args=>{size=>"2592x1944", resize=>"100x100<"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||
| 278 | {args=>{size=>"2592x1944", resize=>"1024^>"}, naked_result=>"1365x1024"}, | ||||||||||||||||||||||||||||||||||||
| 279 | {args=>{size=>"2592x1944", resize=>"10240^>"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 280 | {args=>{size=>"2592x1944", resize=>"1024^<"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 281 | {args=>{size=>"2592x1944", resize=>"10240^<"}, naked_result=>"13653x10240"}, | ||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||
| 283 | {args=>{size=>"2592x1944", resize=>"^1024>"}, naked_result=>"1024x768"}, | ||||||||||||||||||||||||||||||||||||
| 284 | {args=>{size=>"2592x1944", resize=>"^10240>"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 285 | {args=>{size=>"2592x1944", resize=>"^1024<"}, naked_result=>"2592x1944"}, | ||||||||||||||||||||||||||||||||||||
| 286 | {args=>{size=>"2592x1944", resize=>"^10240<"}, naked_result=>"10240x7680"}, | ||||||||||||||||||||||||||||||||||||
| 287 | ], | ||||||||||||||||||||||||||||||||||||
| 288 | links => [ | ||||||||||||||||||||||||||||||||||||
| 289 | {url=>'prog:imgsize'}, | ||||||||||||||||||||||||||||||||||||
| 290 | ], | ||||||||||||||||||||||||||||||||||||
| 291 | }; | ||||||||||||||||||||||||||||||||||||
| 292 | sub calc_image_resized_size { | ||||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | _calc_or_human('calc', @_); | ||||||||||||||||||||||||||||||||||
|
Subroutine
calc_image_resized_size: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||
| 296 | $SPEC{image_resize_notation_to_human} = { | ||||||||||||||||||||||||||||||||||||
| 297 | 1 | v => 1.1, | |||||||||||||||||||||||||||||||||||
| 298 | summary => 'Translate ImageMagick-like resize notation (e.g. "720^>") to human-friendly text (e.g. "shrink shortest side to 720px")', | ||||||||||||||||||||||||||||||||||||
| 299 | description => <<'_', | ||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||
| 301 | Resize notation supports most syntax from ImageMagick geometry. See | ||||||||||||||||||||||||||||||||||||
| 302 | <pm:Math::Image::CalcResized> and ImageMagick documentation on geometry for more | ||||||||||||||||||||||||||||||||||||
| 303 | details. | ||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||
| 305 | _ | ||||||||||||||||||||||||||||||||||||
| 306 | args => { | ||||||||||||||||||||||||||||||||||||
| 307 | resize => { | ||||||||||||||||||||||||||||||||||||
| 308 | schema => 'str*', | ||||||||||||||||||||||||||||||||||||
| 309 | req => 1, | ||||||||||||||||||||||||||||||||||||
| 310 | pos => 0, | ||||||||||||||||||||||||||||||||||||
| 311 | }, | ||||||||||||||||||||||||||||||||||||
| 312 | }, | ||||||||||||||||||||||||||||||||||||
| 313 | examples => [ | ||||||||||||||||||||||||||||||||||||
| 314 | { | ||||||||||||||||||||||||||||||||||||
| 315 | args => {resize=>''}, naked_result=>'no resizing', | ||||||||||||||||||||||||||||||||||||
| 316 | }, | ||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||
| 318 | { | ||||||||||||||||||||||||||||||||||||
| 319 | args => {resize=>'50%'}, naked_result=>'scale to 50%', | ||||||||||||||||||||||||||||||||||||
| 320 | }, | ||||||||||||||||||||||||||||||||||||
| 321 | { | ||||||||||||||||||||||||||||||||||||
| 322 | args => {resize=>'50%x50%'}, naked_result=>'scale width to 50%, height to 50%', | ||||||||||||||||||||||||||||||||||||
| 323 | }, | ||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||
| 325 | { | ||||||||||||||||||||||||||||||||||||
| 326 | args => {resize=>'720'}, naked_result=>'set width to 720px', | ||||||||||||||||||||||||||||||||||||
| 327 | }, | ||||||||||||||||||||||||||||||||||||
| 328 | { | ||||||||||||||||||||||||||||||||||||
| 329 | args => {resize=>'720>'}, naked_result=>'shrink width to 720px', | ||||||||||||||||||||||||||||||||||||
| 330 | }, | ||||||||||||||||||||||||||||||||||||
| 331 | { | ||||||||||||||||||||||||||||||||||||
| 332 | args => {resize=>'720^'}, naked_result=>'enlarge width to 720px', | ||||||||||||||||||||||||||||||||||||
| 333 | }, | ||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||
| 335 | { | ||||||||||||||||||||||||||||||||||||
| 336 | args => {resize=>'x720'}, naked_result=>'set height to 720px', | ||||||||||||||||||||||||||||||||||||
| 337 | }, | ||||||||||||||||||||||||||||||||||||
| 338 | { | ||||||||||||||||||||||||||||||||||||
| 339 | args => {resize=>'x720>'}, naked_result=>'shrink height to 720px', | ||||||||||||||||||||||||||||||||||||
| 340 | }, | ||||||||||||||||||||||||||||||||||||
| 341 | { | ||||||||||||||||||||||||||||||||||||
| 342 | args => {resize=>'x720^'}, naked_result=>'enlarge height to 720px', | ||||||||||||||||||||||||||||||||||||
| 343 | }, | ||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||
| 345 | { | ||||||||||||||||||||||||||||||||||||
| 346 | args => {resize=>'640x480'}, naked_result=>'fit image inside 640x480', | ||||||||||||||||||||||||||||||||||||
| 347 | }, | ||||||||||||||||||||||||||||||||||||
| 348 | { | ||||||||||||||||||||||||||||||||||||
| 349 | args => {resize=>'640x480^'}, naked_result=>'fit image to fit 640x480 inside it', | ||||||||||||||||||||||||||||||||||||
| 350 | }, | ||||||||||||||||||||||||||||||||||||
| 351 | { | ||||||||||||||||||||||||||||||||||||
| 352 | args => {resize=>'640x480>'}, naked_result=>'shrink image to fit inside 640x480', | ||||||||||||||||||||||||||||||||||||
| 353 | }, | ||||||||||||||||||||||||||||||||||||
| 354 | { | ||||||||||||||||||||||||||||||||||||
| 355 | args => {resize=>'640x480<'}, naked_result=>'enlarge image to fit 640x480 inside it', | ||||||||||||||||||||||||||||||||||||
| 356 | }, | ||||||||||||||||||||||||||||||||||||
| 357 | { | ||||||||||||||||||||||||||||||||||||
| 358 | args => {resize=>'640x480!'}, naked_result=>'set dimension to 640x480', | ||||||||||||||||||||||||||||||||||||
| 359 | }, | ||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||
| 361 | { | ||||||||||||||||||||||||||||||||||||
| 362 | args => {resize=>'720^>'}, naked_result=>'shrink shortest side to 720px', | ||||||||||||||||||||||||||||||||||||
| 363 | }, | ||||||||||||||||||||||||||||||||||||
| 364 | { | ||||||||||||||||||||||||||||||||||||
| 365 | args => {resize=>'720^<'}, naked_result=>'enlarge shortest side to 720px', | ||||||||||||||||||||||||||||||||||||
| 366 | }, | ||||||||||||||||||||||||||||||||||||
| 367 | { | ||||||||||||||||||||||||||||||||||||
| 368 | args => {resize=>'^720>'}, naked_result=>'shrink longest side to 720px', | ||||||||||||||||||||||||||||||||||||
| 369 | }, | ||||||||||||||||||||||||||||||||||||
| 370 | { | ||||||||||||||||||||||||||||||||||||
| 371 | args => {resize=>'^720<'}, naked_result=>'enlarge longest side to 720px', | ||||||||||||||||||||||||||||||||||||
| 372 | }, | ||||||||||||||||||||||||||||||||||||
| 373 | ], | ||||||||||||||||||||||||||||||||||||
| 374 | }; | ||||||||||||||||||||||||||||||||||||
| 375 | sub image_resize_notation_to_human { | ||||||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | _calc_or_human('human', @_); | ||||||||||||||||||||||||||||||||||
|
Subroutine
image_resize_notation_to_human: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||
| 379 | 1 | 1; | |||||||||||||||||||||||||||||||||||
| 380 | # ABSTRACT: Calculate dimensions of image/video resized by ImageMagick-like geometry specification | ||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||
| 382 | __END__ | ||||||||||||||||||||||||||||||||||||