word/edit: sub signatures in ImagePrep methods
[sheet.git] / Shiar_Sheet / ImagePrep.pm
1 package Shiar_Sheet::ImagePrep;
2
3 use 5.020;
4 use warnings;
5 use experimental 'signatures';
6
7 our $VERSION = '1.02';
8
9 sub new ($class, $target) {
10         bless \$target, $class;
11 }
12
13 sub download ($target, $download) {
14         # copy changed remote url to local file
15         unlink $$target if -e $$target;
16         defined $download or return 1;
17         require LWP::UserAgent;
18         my $ua = LWP::UserAgent->new;
19         $ua->agent('/');
20         my $status = $ua->mirror($download, $$target);
21         $status->is_success
22                 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
23 }
24
25 sub dimensions ($imgpath) {
26         require IPC::Run;
27         IPC::Run::run(
28                 [identify => -format => '%w %h', $$imgpath],
29                 '<' => \undef, '>&' => \my $xy
30         ) or die ["Image dimensions could not be determined.", $$imgpath];
31         return split /\s/, $xy, 3;
32 }
33
34 sub generate ($imgpath, $thumbpath, $cmds) {
35         if (not -e $$imgpath) {
36                 return !-e $thumbpath || unlink $thumbpath;
37         }
38         $cmds //= [];
39         $imgpath->convert($thumbpath, $cmds, '300x200') and # low-res cover
40         $imgpath->convert($thumbpath =~ s/\.jpg$/.webp/r,
41                 [@{$cmds}, -quality => 40], '600x400' # higher dpi tradeoff
42         );
43 }
44
45 sub convert ($imgpath, $thumbpath, $cmds, $xyres) {
46         #my ($w, $h) = $imgpath->dimensions;
47         #my $aspect = 3/2; # $xyres
48         my @cmds = @{$cmds};
49         if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
50                 # replace option by permillage crop
51                 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
52                 $dim[$_] ||= 1 for 2, 3; # optional end
53                 push @dim, $dim[2 + $_] - $dim[$_] for 0, 1; # add width, height
54                 splice @cmds, $cmdarg, 2, (
55                         #crop="%[fx:floor(w*$ratio)]x%[fx:floor(h*$ratio)]"
56                         #crop="$crop+%[fx:ceil((w-w*$ratio)/2)]+%[fx:ceil((h-h*$ratio)/2)]"
57                         -set => 'option:distort:viewport' => sprintf(
58                                 '%%[fx:%s]x%%[fx:%s]+%%[fx:%s]+%%[fx:%s]',
59                                 "w*$dim[4]", "h*$dim[5]", # width x height
60                                 #"max(w*$dim[4], h*$dim[5]*$aspect)", # width
61                                 #"max(h*$dim[5], w*$dim[4]/$aspect)", # height
62                                 "w*$dim[0]", "h*$dim[1]", # x+y offset
63                         ),
64                         -distort => SRT => 0, # noop transform to apply viewport
65                 );
66         }
67         @cmds = (
68                 'convert',
69                 $$imgpath,
70                 -delete => '1--1', -background => 'white',
71                 '-strip', -quality => '60%', -interlace => 'plane',
72                 -gravity => defined $cmds ? 'northwest' : 'center',
73                 @cmds,
74                 -resize => "$xyres^", -extent => $xyres,
75                 $thumbpath
76         );
77
78         $imgpath->runcommand(@cmds);
79 }
80
81 sub runcommand ($, @cmds) {
82         require IPC::Run;
83         my $output;
84         IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output) or die [
85                 "Failed to convert source image.",
86                 "@cmds\n" .
87                 ($output || ($? & 127 ? "signal $?" : "error code ".($? >> 8))),
88         ];
89 }
90
91 1;