| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # Copyright (C) 2015-2018 Alex Schroeder <alex@gnu.org> | ||
| 4 | |||
| 5 | # This program is free software: you can redistribute it and/or modify it under | ||
| 6 | # the terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation, either version 3 of the License, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 12 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along with | ||
| 15 | # this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =encoding utf8 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Game::FaceGenerator::Core - support for the Face Generator application | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Face Generator is a web application which uses random images to create faces. | ||
| 26 | This module provides the subroutines that L<Game::FaceGenerator> needs. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | package Game::FaceGenerator::Core; | ||
| 31 | |||
| 32 | require Exporter; | ||
| 33 | our @ISA = qw(Exporter); | ||
| 34 | our @EXPORT_OK = qw(dir no_flip all_artists random_components all_components all_elements render_components move); | ||
| 35 | |||
| 36 | use Modern::Perl; | ||
| 37 | use File::ShareDir qw(dist_dir); | ||
| 38 | use File::Slurper qw(read_dir read_text read_binary write_binary); | ||
| 39 | use List::Util 'any'; | ||
| 40 | use Cwd; | ||
| 41 | use GD; | ||
| 42 | |||
| 43 | sub member { | ||
| 44 | my $element = shift; | ||
| 45 | foreach (@_) { | ||
| 46 | return 1 if $element eq $_; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub one { | ||
| 51 | my $i = int(rand(scalar @_)); | ||
| 52 | return $_[$i]; | ||
| 53 | } | ||
| 54 | |||
| 55 | my $dir; | ||
| 56 | |||
| 57 | sub dir { | ||
| 58 | $dir = shift; | ||
| 59 | } | ||
| 60 | |||
| 61 | my $no_flip; | ||
| 62 | |||
| 63 | sub no_flip { | ||
| 64 | my $arg = shift; | ||
| 65 | if (ref $arg) { | ||
| 66 | $no_flip = $arg; | ||
| 67 | } elsif ($arg and $no_flip->{$arg}) { | ||
| 68 | @{$no_flip->{$arg}}; | ||
| 69 | } else { | ||
| 70 | (); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | =head1 ARTISTS | ||
| 75 | |||
| 76 | The F<contrib> directory contains background images like F<empty.png> (default | ||
| 77 | human), F<elf.png> (narrower), F<dwarf.png> (rounder), F<demon.png> (looking | ||
| 78 | half left, with horns), F<dragon.png> (looking left), and the artist | ||
| 79 | directories. | ||
| 80 | |||
| 81 | Each artist directory must contain a F<README.md> file. The first Markdown link | ||
| 82 | of the form C<[name](URL)> is used to name the artist and link to their presence | ||
| 83 | on the web. The first Markdown emphasized text of the form C<*title*> is used as | ||
| 84 | the title for the collection. This can be useful if an artist has two different | ||
| 85 | collections. Take Alex Schroeder, who started out as “alex”. Then a second | ||
| 86 | collection is added, and called “alex2”. It’s still the same person, so the two | ||
| 87 | F<README.md> files both contain the link C<[Alex | ||
| 88 | Schroeder](https://alexschroeder.ch/)>, and the first one contains the title | ||
| 89 | C<*Blau*> and the second one contains the title C<*Tablet*>. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | my %artists; | ||
| 94 | |||
| 95 | =head1 METHODS | ||
| 96 | |||
| 97 | =head2 all_artists | ||
| 98 | |||
| 99 | Return a hash referefence of all the artists and their types. This scans the | ||
| 100 | entire F<contrib> directory, so the result is cached. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub all_artists { | ||
| 105 | return \%artists if %artists; | ||
| 106 | my @dirs = grep { | ||
| 107 | !/\.png$/ # ignore images | ||
| 108 | && -d "$dir/$_" | ||
| 109 | && -f "$dir/$_/README.md" | ||
| 110 | } read_dir($dir); | ||
| 111 | for my $artist (@dirs) { | ||
| 112 | # Determine name and url from the README file. | ||
| 113 | $artists{$artist}{name} = $artist; # default | ||
| 114 | my $text = read_text("$dir/$artist/README.md"); | ||
| 115 | if ($text =~ /\[([^]]*)\]\((https?:.*)\)/) { | ||
| 116 | $artists{$artist} = {}; | ||
| 117 | $artists{$artist}{name} = $1; | ||
| 118 | $artists{$artist}{url} = $2; | ||
| 119 | } | ||
| 120 | if ($text =~ /\*([^* ][^*]*)\*/) { | ||
| 121 | $artists{$artist}{title} = $1; | ||
| 122 | } | ||
| 123 | # Find available types from the filenames. | ||
| 124 | my %types; | ||
| 125 | for (read_dir("$dir/$artist")) { | ||
| 126 | $types{$1} = 1 if /_([a-z]+)/; | ||
| 127 | } | ||
| 128 | delete $types{all} if $types{all} and keys %types > 1; | ||
| 129 | $artists{$artist}{types} = [sort keys %types]; | ||
| 130 | } | ||
| 131 | return \%artists; | ||
| 132 | } | ||
| 133 | |||
| 134 | =head2 all_components | ||
| 135 | |||
| 136 | Returns all the elements for an C<$artist>, optionally with prefix c<$element>, | ||
| 137 | followed by the C<$empty> image (defaulting to F<empty.png>), and possibly | ||
| 138 | filtered by last modification time in C<$days>. Each element is an array of | ||
| 139 | C<$empty> and the actual file name. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub all_components { | ||
| 144 | my ($artist, $element, $empty, $days) = @_; | ||
| 145 | $empty ||= 'empty.png'; | ||
| 146 | my @files = grep { | ||
| 147 | /$element.*\.png$/ | ||
| 148 | and (not $days or (stat("$dir/$artist/$_"))[9] >= (time - $days*24*60*60)) | ||
| 149 | } read_dir("$dir/$artist"); | ||
| 150 | my @components = map { [$empty, $_] } @files; | ||
| 151 | return @components; | ||
| 152 | } | ||
| 153 | |||
| 154 | =head2 all_elements | ||
| 155 | |||
| 156 | The elements are drawn in a default order over one another: C<face> C<eyes> | ||
| 157 | C<brows> C<mouth> C<chin> C<ears> C<nose> C<extra> C<horns> C<bangs> C<hair> | ||
| 158 | C<hat>. | ||
| 159 | |||
| 160 | Thus, a mustache (as part of the C<chin>) covers a mouth; C<hair> covers the | ||
| 161 | face; C<hat> cover C<hair>, and so on. | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | sub all_elements { | ||
| 166 | # face is the background, if any (mostly to support photos) | ||
| 167 | # chin after mouth (mustache hides mouth) | ||
| 168 | # nose after chin (mustache!) | ||
| 169 | # hair after ears | ||
| 170 | # ears after chin (if you're fat) | ||
| 171 | # chin after ears (for your beard) – damn! | ||
| 172 | return qw(face eyes brows mouth chin ears nose extra horns bangs hair hat); | ||
| 173 | } | ||
| 174 | |||
| 175 | =head2 random_components | ||
| 176 | |||
| 177 | The random components of C<$type> for C<$artist>. If C<$debug> is true, | ||
| 178 | F<empty.png> is added to the list of components. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | sub random_components { | ||
| 183 | my ($type, $artist, $debug) = @_; | ||
| 184 | %artists = %{all_artists()} unless keys %artists; | ||
| 185 | $type = one(@{$artists{$artist}->{types}}) if $type eq 'random'; | ||
| 186 | my @elements = all_elements(); | ||
| 187 | @elements = grep(!/^extra/, @elements) if rand(1) >= 0.1; # 10% chance | ||
| 188 | @elements = grep(!/^hat/, @elements) if rand(1) >= 0.1; # 10% chance | ||
| 189 | my @files = grep { /\.png$/ } read_dir("$dir/$artist"); | ||
| 190 | my @components; | ||
| 191 | for my $element (@elements) { | ||
| 192 | my @candidates1 = grep(/^${element}_/, @files); | ||
| 193 | my @candidates2 = grep(/_$type/, @candidates1); | ||
| 194 | @candidates2 = grep(/_all/, @candidates1) unless @candidates2; | ||
| 195 | my $candidate = one(@candidates2) || ''; | ||
| 196 | unless (any { $type eq $_ } no_flip($artist)) { | ||
| 197 | $candidate .= '_' if $candidate and rand >= 0.5; # invert it! | ||
| 198 | } | ||
| 199 | push(@components, $candidate) if $candidate; | ||
| 200 | } | ||
| 201 | unshift(@components, 'empty.png') if $debug; | ||
| 202 | return @components; | ||
| 203 | } | ||
| 204 | |||
| 205 | =head2 render_components | ||
| 206 | |||
| 207 | Renders the components for C<$artist>. The C<@components> are probably the | ||
| 208 | result of a call to C<random_components>. | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | sub render_components { | ||
| 213 | my ($artist, @components) = @_; | ||
| 214 | my $image; | ||
| 215 | for my $component (@components) { | ||
| 216 | next unless $component; | ||
| 217 | my $layer; | ||
| 218 | if (-f "$dir/$component") { | ||
| 219 | $layer = GD::Image->newFromPng("$dir/$component", 1); | ||
| 220 | } elsif (substr($component, -1) eq '_') { | ||
| 221 | $component = substr($component, 0, -1); | ||
| 222 | $layer = GD::Image->newFromPng("$dir/$artist/$component", 1); | ||
| 223 | $layer->flipHorizontal(); | ||
| 224 | } else { | ||
| 225 | $layer = GD::Image->newFromPng("$dir/$artist/$component", 1); | ||
| 226 | } | ||
| 227 | # scanned images with a white background: make white transparent unless this | ||
| 228 | # is the first image | ||
| 229 | if ($layer->isTrueColor == 0 and $layer->transparent == -1 and $image) { | ||
| 230 | my $white = $layer->colorClosest(255,255,255); | ||
| 231 | $layer->transparent($white); | ||
| 232 | } | ||
| 233 | # if we already have an image, combine them | ||
| 234 | if ($image) { | ||
| 235 | $image->copy($layer, 0, 0, 0, 0, $layer->getBounds()); | ||
| 236 | } else { | ||
| 237 | $image = $layer; | ||
| 238 | $image->alphaBlending(1); | ||
| 239 | $image->saveAlpha(1); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | return $image->png(); | ||
| 243 | } | ||
| 244 | |||
| 245 | =head2 move | ||
| 246 | |||
| 247 | This is the subroutine called to edit the images. | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | sub move { | ||
| 252 | my ($artist, $element, $direction, $step) = @_; | ||
| 253 | my $file = "$dir/$artist/$element"; | ||
| 254 | my $original = GD::Image->new($file); | ||
| 255 | my $image = GD::Image->new(450, 600); | ||
| 256 | my $white = $image->colorAllocate(255,255,255); # find white | ||
| 257 | $image->rectangle(0, 0, $image->getBounds(), $white); | ||
| 258 | if ($direction eq 'up') { | ||
| 259 | $image->copy($original, 0, 0, 0, $step, $image->width, $image->height - $step); | ||
| 260 | } elsif ($direction eq 'down') { | ||
| 261 | $image->copy($original, 0, $step, 0, 0, $image->width, $image->height - $step); | ||
| 262 | } elsif ($direction eq 'left') { | ||
| 263 | $image->copy($original, 0, 0, $step, 0, $image->width - $step, $image->height); | ||
| 264 | } elsif ($direction eq 'right') { | ||
| 265 | $image->copy($original, $step, 0, 0, 0, $image->width - $step, $image->height); | ||
| 266 | } elsif ($direction eq 'appart') { | ||
| 267 | $image->copy($original, $image->width/2 + $step/2, 0, $image->width/2, 0, $image->width/2 - $step/2, $image->height); | ||
| 268 | $image->copy($original, 0, 0, $step/2, 0, $image->width/2 - $step/2, $image->height); | ||
| 269 | } elsif ($direction eq 'closer') { | ||
| 270 | $image->copy($original, $step/2, 0, 0, 0, $image->width/2 - $step/2, $image->height); | ||
| 271 | $image->copy($original, $image->width/2, 0, $image->width/2 + $step/2, 0, $image->width/2 - $step/2, $image->height); | ||
| 272 | } else { | ||
| 273 | die "Unknown direction: $direction\n"; | ||
| 274 | } | ||
| 275 | write_binary($file, $image->png); | ||
| 276 | } | ||
| 277 | |||
| 278 | 1; |