summaryrefslogtreecommitdiff
path: root/util/spritegen.pl
blob: 5b2b598239429fe0241e7d49432ffb5bc89fb01c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/perl

use strict;
use warnings;
use Image::Magick;
use Cwd 'abs_path';

our $ROOT;
BEGIN { ($ROOT = abs_path $0) =~ s{/util/spritegen\.pl$}{}; }

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')
  }
} 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;
    }
    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);
}


# Tries to find the width of the strip for which the number of unused pixels is
# the minimum. Simple and dumb linear search; it's fast enough.
#
# Note that minimum number of unused pixels does not imply minimum file size,
# although there is some correlation. To further minimize the file size, it's
# possible to attempt to group similar-looking images close together so that
# 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;
    }
    $w--;
  }
  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;
}


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;
    }
    $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};
}


img minstrip;
css;
rename $ticons, $icons or die $!;