ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cfmaps/cfmap2png
Revision: 1.10
Committed: Sun Nov 20 01:31:21 2005 UTC (18 years, 6 months ago) by root
Branch: MAIN
Changes since 1.9: +32 -13 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3 root 1.8 # cfarch2png - convert crossfire maps to png+metadata
4     # Copyright (C) 2005 Marc Lehmann <gvpe@schmorp.de>
5     #
6     # CFARCH2PNG is free software; you can redistribute it and/or modify
7     # it under the terms of the GNU General Public License as published by
8     # the Free Software Foundation; either version 2 of the License, or
9     # (at your option) any later version.
10     #
11     # This program is distributed in the hope that it will be useful,
12     # but WITHOUT ANY WARRANTY; without even the implied warranty of
13     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     # GNU General Public License for more details.
15     #
16     # You should have received a copy of the GNU General Public License
17     # along with gvpe; if not, write to the Free Software
18     # Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19    
20 root 1.1 # tower of stars: missing craters?
21    
22 root 1.10 our $VERSION = '1.1';
23 root 1.9
24 root 1.1 use strict;
25    
26     use Storable;
27     use List::Util qw(max);
28    
29     use Gtk2;
30    
31     #init Gtk2::Gdk;
32    
33     my $LIB = $ENV{CROSSFIRE_LIBDIR}
34     or die "\$CROSSFIRE_LIBDIR must be set\n";
35    
36     sub T (){ 32 }
37    
38     sub read_pak($) {
39     my ($path) = @_;
40    
41     eval {
42     -M "$path.pst" < -M $path
43     && Storable::retrieve "$path.pst"
44     } or do {
45     my %pak;
46    
47     open my $fh, "<:raw", $path
48     or die "$_[0]: $!";
49     while (<$fh>) {
50     my ($type, $id, $len, $path) = split;
51     $path =~ s/.*\///;
52     read $fh, $pak{$path}, $len;
53     }
54    
55     Storable::nstore \%pak, "$path.pst";
56    
57     \%pak
58     }
59     }
60    
61     sub read_smooth($) {
62     my ($path) = @_;
63    
64     eval {
65     -M "$path.pst" < -M $path
66     && Storable::retrieve "$path.pst"
67     } or do {
68     my %smooth;
69    
70     open my $fh, "<:raw", $path
71     or die "$path: $!";
72     while (<$fh>) {
73     next if /^\s*($|#)/;
74    
75     $smooth{$1} = $2 if /^(\S+)\s+(\S+)$/;
76     }
77    
78     Storable::nstore \%smooth, "$path.pst";
79    
80     \%smooth
81     }
82     }
83    
84     sub read_arch($;$) {
85     my ($path, $cache) = @_;
86    
87     eval {
88     $cache
89     && -M "$path.pst" < -M $path
90     && Storable::retrieve "$path.pst"
91     } or do {
92     my %arc;
93     my ($more, $prev);
94    
95     open my $fh, "<:raw", $path
96     or die "$path: $!";
97    
98     my $parse_block; $parse_block = sub {
99     my %arc = @_;
100    
101     while (<$fh>) {
102     s/\s+$//;
103     if (/^end$/i) {
104     last;
105     } elsif (/^arch (\S+)$/) {
106 root 1.3 push @{ $arc{inventory} }, $parse_block->(_name => $1);
107 root 1.4 } elsif (/^lore$/) {
108     while (<$fh>) {
109     last if /^endlore\s*$/i;
110     $arc{lore} .= $_;
111     }
112 root 1.1 } elsif (/^msg$/) {
113     while (<$fh>) {
114     last if /^endmsg\s*$/i;
115     $arc{msg} .= $_;
116     }
117     } elsif (/^(\S+)\s*(.*)$/) {
118     $arc{lc $1} = $2;
119     } elsif (/^\s*($|#)/) {
120     #
121     } else {
122     warn "$path: unparsable line '$_' in arch $arc{_name}";
123     }
124     }
125    
126     \%arc
127     };
128    
129     while (<$fh>) {
130     s/\s+$//;
131     if (/^more$/i) {
132     $more = $prev;
133     } elsif (/^object (\S+)$/i) {
134     my $name = $1;
135     my $arc = $parse_block->(_name => $name);
136    
137 root 1.4 if ($more) {
138     $more->{more} = $arc;
139     } else {
140     $arc{$name} = $arc;
141     }
142 root 1.1
143     $prev = $arc;
144     $more = undef;
145     } elsif (/^arch (\S+)$/i) {
146     push @{ $arc{arch} }, $parse_block->(_name => $1);
147     } elsif (/^\s*($|#)/) {
148     #
149     } else {
150     warn "$path: unparseable top-level line '$_'";
151     }
152     }
153    
154     undef $parse_block; # work around bug in perl not freeing $fh etc.
155    
156     Storable::nstore \%arc, "$path.pst"
157     if $cache;
158    
159     \%arc
160     }
161     }
162    
163     my $arch = read_arch "$LIB/archetypes", 1;
164     my $tile = read_pak "$LIB/crossfire.0";
165     my $smooth = read_smooth "$LIB/smooth";
166    
167     sub tile($) {
168     my $name = $_[0];
169    
170     my $pb = $tile->{$name}
171     or return;
172    
173     unless (ref $pb) {
174     my $file = "/tmp/map2png.$$~";
175    
176     open my $fh, ">:raw", $file
177     or die "$file: $!";
178     print $fh $pb;
179     close $fh;
180    
181     $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file;
182     unlink $file;
183     }
184    
185     $pb
186     }
187    
188     sub cfmap_render($;$) {
189     my ($mapa, $mapname) = @_;
190    
191     my %meta;
192    
193     my ($mapx, $mapy);
194    
195 root 1.10 my $map;
196 root 1.1
197     for (@{ $mapa->{arch} }) {
198 root 1.10 my ($x, $y) = ($_->{x}, $_->{y});
199    
200 root 1.1 if ($_->{_name} eq "map") {
201     $meta{info} = $_;
202    
203 root 1.10 $mapx = $_->{width} || $x;
204     $mapy = $_->{height} || $y;
205 root 1.1 } else {
206 root 1.10 push @{ $map->[$x][$y] }, $_;
207 root 1.1
208     # arch map is unreliable w.r.t. width and height
209 root 1.10 $mapx = $x + 1 if $mapx <= $x;
210     $mapy = $y + 1 if $mapy <= $y;
211 root 1.1 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
212     #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
213     }
214     }
215    
216     $meta{width} = $mapx;
217     $meta{height} = $mapy;
218    
219     my %draw_info;
220 root 1.10 my %map_info;
221 root 1.1
222     # first pass, gather face stacking order, border and corner info
223     for my $x (0 .. $mapx - 1) {
224     my $col = $map->[$x];
225     for my $y (0 .. $mapy - 1) {
226     my $as = $col->[$y] || [];
227    
228     for my $layer (0 .. $#$as) {
229     my $a = $as->[$layer];
230    
231 root 1.3 my $o = $arch->{$a->{_name}}
232 root 1.1 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
233    
234 root 1.5 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
235     my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
236     my $level = $smoothlevel ? $smoothlevel
237     : $is_floor ? 0
238     : 256 + $layer;
239 root 1.1
240 root 1.5 while ($o) {
241     my $face = $a->{face} || $o->{face};
242 root 1.1
243 root 1.5 my $pb = tile $face
244     or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
245 root 1.1
246     my $mx = $x + $o->{x};
247     my $my = $y + $o->{y};
248    
249 root 1.2 last if 0 > $mx || $mx >= $mapx
250     || 0 > $my || $my >= $mapy;
251 root 1.1
252     # this is very ugly (some tiles are 32x33 or worse)
253     my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
254    
255 root 1.10 my $dx = $bigface ? $o->{x} : 0;
256     my $dy = $bigface ? $o->{y} : 0;
257    
258     push @{ $map_info{$level}{$mx, $my} }, $a;
259    
260     $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16);
261    
262 root 1.5 if (my $sface = $smooth->{$face}) {
263     $bigface and die "can't handle bigfaces with smoothing ($face)\n";
264 root 1.1
265     # full tile
266 root 1.3 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000;
267 root 1.1
268     # borders
269 root 1.3 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031;
270     $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092;
271     $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064;
272     $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8;
273 root 1.1
274     # corners
275 root 1.3 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100;
276     $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
277     $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
278     $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
279 root 1.1 }
280    
281     $o = $o->{more};
282     }
283     }
284     }
285     }
286    
287     my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
288     or die;
289 root 1.7 $map_pb->fill (0xffffff00);
290 root 1.1
291 root 1.10 # second pass, render the map
292 root 1.1 for my $level (sort { $a <=> $b } keys %draw_info) {
293     my $v = $draw_info{$level};
294     while (my ($sface, $info) = each %$v) {
295     my $pb = tile $sface
296     or die "no smooth face $sface\n";
297    
298     while (my ($xy, $bits) = each %$info) {
299     my ($x, $y) = split $;, $xy;
300    
301     next if $x < 0 || $x >= $mapx
302     || $y < 0 || $y >= $mapy;
303    
304     # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB
305     # X don't draw
306     # F full tile draw with x|y bigface displacement
307     # c maybe draw these corners
308     # C do not draw these corners
309     # b draw these borders
310    
311     if ($bits & 0x2000) {
312     my $dx = (($bits >> 24) & 0xff) - 128;
313     my $dy = (($bits >> 16) & 0xff) - 128;
314    
315     $pb->composite ($map_pb,
316     $x * T, $y * T,
317     T, T,
318     ($x - $dx) * T, ($y - $dy) * T, 1, 1,
319     "nearest",
320     255
321     );
322     }
323    
324     unless ($bits & 0x1000) {
325     my $border = $bits & 0xf;
326     my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf;
327    
328     $pb->composite ($map_pb,
329     $x * T, $y * T,
330     T, T,
331     ($x - $border) * T, $y * T, 1, 1,
332     "nearest",
333     255
334     ) if $border;
335    
336     $pb->composite ($map_pb,
337     $x * T, $y * T,
338     T, T,
339     ($x - $corner) * T, ($y - 1) * T, 1, 1,
340     "nearest",
341     255
342     ) if $corner;
343     }
344     }
345     }
346     }
347    
348 root 1.10 # third pass, gather meta info
349     for my $level (sort { $a <=> $b } keys %map_info) {
350     my $info = $map_info{$level};
351    
352     while (my ($xy, $as) = each %$info) {
353     my ($x, $y) = split $;, $xy;
354    
355     next if $x < 0 || $x >= $mapx
356     || $y < 0 || $y >= $mapy;
357    
358     push @{ $meta{map}[$x][$y] }, $_ for @$as;
359     }
360     }
361    
362 root 1.1 ($map_pb, \%meta)
363     }
364    
365     for my $file (@ARGV) {
366     my $mapa = read_arch $file;
367     my ($pb, $meta) = cfmap_render $mapa, $file;
368 root 1.3 $pb->save ("$file.png~", "png");
369     system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
370 root 1.6 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
371     system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png";
372 root 1.3 unlink "$file.png~";
373 root 1.1 Storable::nstore $meta, "$file.pst";
374     }
375    
376    
377    
378    
379    
380