summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2020-12-31 11:02:21 +0100
committerYorhel <git@yorhel.nl>2020-12-31 11:02:23 +0100
commit56ff6519f3a53c499470ed11be91c01cfdaf02be (patch)
treec5ec774cc607b673ddfa06fa04743e416a447084 /util
parentc5ba9db391da5737d3c215d00cd347f984a67dfc (diff)
spritegen.pl: Remove Image::Magick dependency + use new indentation style
Just pointless cleanup because I'm not in the mood to do meaningful work...
Diffstat (limited to 'util')
-rwxr-xr-xutil/spritegen.pl159
1 files changed, 75 insertions, 84 deletions
diff --git a/util/spritegen.pl b/util/spritegen.pl
index 5b2b5982..e88af5ce 100755
--- a/util/spritegen.pl
+++ b/util/spritegen.pl
@@ -2,66 +2,65 @@
use strict;
use warnings;
-use Image::Magick;
use Cwd 'abs_path';
our $ROOT;
BEGIN { ($ROOT = abs_path $0) =~ s{/util/spritegen\.pl$}{}; }
+use lib "$ROOT/lib";
+use VNDB::Config;
+
my $path = "$ROOT/data/icons";
my $icons = "$ROOT/static/f/icons.png";
my $ticons = "$ROOT/static/f/icons~.png";
my $css = "$ROOT/data/icons/icons.css";
my @img = map {
- my $i = Image::Magick->new();
- $i->Read($_) and die $_;
- {
- f => /^\Q$path\E\/(.+)\.png/ && $1,
- i => $i,
- h => scalar $i->Get('height'),
- w => scalar $i->Get('width')
- }
+ my $id = config->{identify_path};
+ my($w,$h) = split 'x', `$id -format "%wx%h" "$_"`;
+ {
+ p => $_,
+ f => /^\Q$path\E\/(.+)\.png/ && $1,
+ w => $w,
+ h => $h,
+ }
} glob("$path/*.png"), glob("$path/*/*.png");
@img = sort { $b->{h} <=> $a->{h} || $b->{w} <=> $a->{w} } @img;
-my $minpixels = 0;
-$minpixels += $_->{w}*$_->{h} for @img;
-
# Simple strip packing algortihm, First-Fit Decreasing Height.
sub genstrip {
- my $w = shift;
- my @l;
- my $h = 0;
- for my $i (@img) {
- my $found = 0;
- # @img is assumed to be sorted by height, so image always fits
- # (height-wise) in any of the previously created levels.
- for my $l (@l) {
- next if $l->{left} + $i->{w} > $w;
- # Image fits, add to level
- $i->{x} = $l->{left};
- $i->{y} = $l->{top};
- $l->{left} += $i->{w};
- $found = 1;
- last;
+ my $w = shift;
+ my @l;
+ my $h = 0;
+ for my $i (@img) {
+ my $found = 0;
+ # @img is assumed to be sorted by height, so image always fits
+ # (height-wise) in any of the previously created levels.
+ for my $l (@l) {
+ next if $l->{left} + $i->{w} > $w;
+ # Image fits, add to level
+ $i->{x} = $l->{left};
+ $i->{y} = $l->{top};
+ $l->{left} += $i->{w};
+ $found = 1;
+ last;
+ }
+ next if $found;
+
+ # No level found, create a new one
+ push @l, { top => $h, left => $i->{w} };
+ $i->{x} = 0;
+ $i->{y} = $h;
+ $h += $i->{h};
}
- next if $found;
-
- # No level found, create a new one
- push @l, { top => $h, left => $i->{w} };
- $i->{x} = 0;
- $i->{y} = $h;
- $h += $i->{h};
- }
-
- # Recalculate the (actually used) width
- $w = 0;
- $w < $_->{x}+$_->{w} && ($w = $_->{x}+$_->{w}) for (@img);
- ($w, $h);
+
+ # Recalculate the (actually used) width
+ $w = 0;
+ $w < $_->{x}+$_->{w} && ($w = $_->{x}+$_->{w}) for (@img);
+ ($w, $h);
}
@@ -74,60 +73,52 @@ sub genstrip {
# the final png image might compress better. Finding a good (and fast)
# algorithm for this is not a trivial task, however.
sub minstrip {
- my($minwidth, $maxwidth) = (0,0);
- for(@img) {
- $minwidth = $_->{w} if $_->{w} > $minwidth;
- $maxwidth += $_->{w};
- }
-
- my($optsize, $w, $h, $optw, $opth) = (1e9, $maxwidth);
- while($w >= $minwidth) {
- ($w, $h) = genstrip($w);
- my $size = $w*$h;
- if($size < $optsize) {
- $optw = $w;
- $opth = $h;
- $optsize = $size;
+ my($minwidth, $maxwidth) = (0,0);
+ for(@img) {
+ $minwidth = $_->{w} if $_->{w} > $minwidth;
+ $maxwidth += $_->{w};
+ }
+
+ my($optsize, $w, $h, $optw, $opth) = (1e9, $maxwidth);
+ while($w >= $minwidth) {
+ ($w, $h) = genstrip($w);
+ my $size = $w*$h;
+ if($size < $optsize) {
+ $optw = $w;
+ $opth = $h;
+ $optsize = $size;
+ }
+ $w--;
}
- $w--;
- }
- genstrip($optw);
+ genstrip($optw);
}
sub img {
- my($w, $h) = @_;
- my $img = Image::Magick->new;
- print $img->Set(size => "${w}x$h");
- print $img->ReadImage('canvas:rgba(0,0,0,0)');
- my $pixels = $w*$h;
- for my $i (@img) {
- print $img->Composite(image => $i->{i}, x => $i->{x}, y => $i->{y});
- }
- print $img->Write("png32:$ticons");
- undef $img;
-
- my $size = -s $ticons;
- #printf "Dim: %dx%d, size: %d, pixels wasted: %d\n", $w, $h, $size, $w*$h-$minpixels;
- $size;
+ my($w, $h) = @_;
+ my @cmd = (config->{convert_path}, -size => "${w}x$h", 'canvas:rgba(0,0,0,0)',
+ map(+($_->{p}, -geometry => "+$_->{x}+$_->{y}", '-composite'), @img),
+ "png32:$ticons"
+ );
+ system(@cmd);
}
sub css {
- # The gender icons need special treatment, they're 3 icons in one image.
- my $gender;
-
- open my $F, '>', $css or die $!;
- for my $i (@img) {
- if($i->{f} eq 'gender') {
- $gender = $i;
- next;
+ # The gender icons need special treatment, they're 3 icons in one image.
+ my $gender;
+
+ open my $F, '>', $css or die $!;
+ for my $i (@img) {
+ if($i->{f} eq 'gender') {
+ $gender = $i;
+ next;
+ }
+ $i->{f} =~ /([^\/]+)$/;
+ printf $F ".icons.%s { background-position: %dpx %dpx }\n", $1, -$i->{x}, -$i->{y};
}
- $i->{f} =~ /([^\/]+)$/;
- printf $F ".icons.%s { background-position: %dpx %dpx }\n", $1, -$i->{x}, -$i->{y};
- }
- printf $F ".icons.gen.f, .icons.gen.b { background-position: %dpx %dpx }\n", -$gender->{x}, -$gender->{y};
- printf $F ".icons.gen.m { background-position: %dpx %dpx }\n", -($gender->{x}+14), -$gender->{y};
+ printf $F ".icons.gen.f, .icons.gen.b { background-position: %dpx %dpx }\n", -$gender->{x}, -$gender->{y};
+ printf $F ".icons.gen.m { background-position: %dpx %dpx }\n", -($gender->{x}+14), -$gender->{y};
}