index: release v1.18 with only altgr index linked
[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.03';
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, $opt) {
35         if (not -e $$imgpath) {
36                 return !-e $thumbpath || unlink $thumbpath;
37         }
38         my @cmds = @{$opt->{convert} // []};
39         unshift @cmds, -area => $_ for $opt->{crop32} || ();
40         $imgpath->convert($thumbpath, \@cmds, '300x200') and # low-res cover
41         $imgpath->convert($thumbpath =~ s/\.jpg$/.webp/r,
42                 [@cmds, -quality => 40], '600x400' # higher dpi tradeoff
43         );
44 }
45
46 sub convert ($imgpath, $thumbpath, $cmds, $xyres = 0) {
47         #my ($w, $h) = $imgpath->dimensions;
48         #my $aspect = 3/2; # $xyres
49         my @cmds = @{$cmds};
50         if (my ($cmdarg) = grep { $cmds[$_] eq '-area' } 0 .. $#cmds) {
51                 # replace option by permillage crop
52                 my @dim = map { $_ / 1000 } split /\D/, $cmds[$cmdarg + 1];
53                 $dim[$_] ||= 1 for 2, 3; # optional end
54                 push @dim, $dim[2 + $_] - $dim[$_] for 0, 1; # add width, height
55                 splice @cmds, $cmdarg, 2, (
56                         #crop="%[fx:floor(w*$ratio)]x%[fx:floor(h*$ratio)]"
57                         #crop="$crop+%[fx:ceil((w-w*$ratio)/2)]+%[fx:ceil((h-h*$ratio)/2)]"
58                         -set => 'option:distort:viewport' => sprintf(
59                                 '%%[fx:%s]x%%[fx:%s]+%%[fx:%s]+%%[fx:%s]',
60                                 "w*$dim[4]", "h*$dim[5]", # width x height
61                                 #"max(w*$dim[4], h*$dim[5]*$aspect)", # width
62                                 #"max(h*$dim[5], w*$dim[4]/$aspect)", # height
63                                 "w*$dim[0]", "h*$dim[1]", # x+y offset
64                         ),
65                         -distort => SRT => 0, # noop transform to apply viewport
66                 );
67         }
68         @cmds = (
69                 'convert',
70                 $$imgpath,
71                 -delete => '1--1', -background => 'white',
72                 '-strip', -quality => '60%', -interlace => 'plane',
73                 -gravity => defined $cmds ? 'northwest' : 'center',
74                 @cmds,
75                 $xyres ? (-resize => "$xyres^", -extent => $xyres) : (),
76                 $thumbpath
77         );
78
79         $imgpath->runcommand(@cmds);
80 }
81
82 sub runcommand ($, @cmds) {
83         require IPC::Run;
84         my $output;
85         IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output) or die [
86                 "Failed to convert source image.",
87                 "@cmds\n" .
88                 ($output || ($? & 127 ? "signal $?" : "error code ".($? >> 8))),
89         ];
90 }
91
92 1;