ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cfmaps/cfmap2png
Revision: 1.8
Committed: Sun Nov 20 00:58:57 2005 UTC (18 years, 7 months ago) by root
Branch: MAIN
Changes since 1.7: +17 -0 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     use strict;
23    
24     use Storable;
25     use List::Util qw(max);
26    
27     use Gtk2;
28    
29 root 1.4 our $VERSION = '1.0';
30    
31 root 1.1 #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     my $map = $meta{map} = [];
196    
197     for (@{ $mapa->{arch} }) {
198     if ($_->{_name} eq "map") {
199     $meta{info} = $_;
200    
201     $mapx = $_->{width} || $_->{x};
202     $mapy = $_->{height} || $_->{y};
203     } else {
204     push @{ $map->[$_->{x}][$_->{y}] }, $_;
205    
206     # arch map is unreliable w.r.t. width and height
207     $mapx = $_->{x} + 1 if $mapx <= $_->{x};
208     $mapy = $_->{y} + 1 if $mapy <= $_->{y};
209     #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
210     #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
211     }
212     }
213    
214     $meta{width} = $mapx;
215     $meta{height} = $mapy;
216    
217     my %draw_info;
218    
219     # first pass, gather face stacking order, border and corner info
220     for my $x (0 .. $mapx - 1) {
221     my $col = $map->[$x];
222     for my $y (0 .. $mapy - 1) {
223     my $as = $col->[$y] || [];
224    
225     for my $layer (0 .. $#$as) {
226     my $a = $as->[$layer];
227    
228 root 1.3 my $o = $arch->{$a->{_name}}
229 root 1.1 or (warn "$mapname: arch '$a->{_name}' not found at ($x|$y)\n"), next;
230    
231 root 1.5 my $smoothlevel = exists $a->{smoothlevel} ? $a->{smoothlevel} : $o->{smoothlevel};
232     my $is_floor = exists $a->{is_floor} ? $a->{is_floor} : $o->{is_floor};
233     my $level = $smoothlevel ? $smoothlevel
234     : $is_floor ? 0
235     : 256 + $layer;
236 root 1.1
237 root 1.5 while ($o) {
238     my $face = $a->{face} || $o->{face};
239 root 1.1
240 root 1.5 my $pb = tile $face
241     or (warn "$mapname: face '$face' not found for arch '$a->{_name}' at ($x|$y)\n"), last;
242 root 1.1
243     my $mx = $x + $o->{x};
244     my $my = $y + $o->{y};
245    
246 root 1.2 last if 0 > $mx || $mx >= $mapx
247     || 0 > $my || $my >= $mapy;
248 root 1.1
249     # this is very ugly (some tiles are 32x33 or worse)
250     my $bigface = $pb->get_width >= T*2 || $pb->get_height >= T*2;
251    
252 root 1.5 if (my $sface = $smooth->{$face}) {
253     $bigface and die "can't handle bigfaces with smoothing ($face)\n";
254 root 1.1
255     # full tile
256 root 1.3 $draw_info{$smoothlevel}{$sface}{$mx , $my } |= 0x1000;
257 root 1.1
258     # borders
259 root 1.3 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my } |= 0x0031;
260     $draw_info{$smoothlevel}{$sface}{$mx , $my + 1} |= 0x0092;
261     $draw_info{$smoothlevel}{$sface}{$mx - 1, $my } |= 0x0064;
262     $draw_info{$smoothlevel}{$sface}{$mx , $my - 1} |= 0x00c8;
263 root 1.1
264     # corners
265 root 1.3 $draw_info{$smoothlevel}{$sface}{$mx + 1, $my + 1} |= 0x0100;
266     $draw_info{$smoothlevel}{$sface}{$mx - 1, $my + 1} |= 0x0200;
267     $draw_info{$smoothlevel}{$sface}{$mx - 1, $my - 1} |= 0x0400;
268     $draw_info{$smoothlevel}{$sface}{$mx + 1, $my - 1} |= 0x0800;
269 root 1.1 }
270    
271     my $dx = $bigface ? $o->{x} : 0;
272     my $dy = $bigface ? $o->{y} : 0;
273    
274 root 1.5 $draw_info{$level}{$face}{$mx, $my} |= 0x2000 | (($dx + 128) << 24) | (($dy + 128) << 16);
275 root 1.1
276     $o = $o->{more};
277     }
278     }
279     }
280     }
281    
282     my $map_pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, $mapx * T, $mapy * T
283     or die;
284 root 1.7 $map_pb->fill (0xffffff00);
285 root 1.1
286     # second pass, render all the stuff
287     for my $level (sort { $a <=> $b } keys %draw_info) {
288     my $v = $draw_info{$level};
289     while (my ($sface, $info) = each %$v) {
290     my $pb = tile $sface
291     or die "no smooth face $sface\n";
292    
293     while (my ($xy, $bits) = each %$info) {
294     my ($x, $y) = split $;, $xy;
295    
296     next if $x < 0 || $x >= $mapx
297     || $y < 0 || $y >= $mapy;
298    
299     # bits is 00XX XXXX YYYY YYFX cccc CCCC BBBB
300     # X don't draw
301     # F full tile draw with x|y bigface displacement
302     # c maybe draw these corners
303     # C do not draw these corners
304     # b draw these borders
305    
306     if ($bits & 0x2000) {
307     my $dx = (($bits >> 24) & 0xff) - 128;
308     my $dy = (($bits >> 16) & 0xff) - 128;
309    
310     $pb->composite ($map_pb,
311     $x * T, $y * T,
312     T, T,
313     ($x - $dx) * T, ($y - $dy) * T, 1, 1,
314     "nearest",
315     255
316     );
317     }
318    
319     unless ($bits & 0x1000) {
320     my $border = $bits & 0xf;
321     my $corner = ($bits >> 8) & ~($bits >> 4) & 0xf;
322    
323     $pb->composite ($map_pb,
324     $x * T, $y * T,
325     T, T,
326     ($x - $border) * T, $y * T, 1, 1,
327     "nearest",
328     255
329     ) if $border;
330    
331     $pb->composite ($map_pb,
332     $x * T, $y * T,
333     T, T,
334     ($x - $corner) * T, ($y - 1) * T, 1, 1,
335     "nearest",
336     255
337     ) if $corner;
338     }
339     }
340     }
341     }
342    
343     ($map_pb, \%meta)
344     }
345    
346     for my $file (@ARGV) {
347     my $mapa = read_arch $file;
348     my ($pb, $meta) = cfmap_render $mapa, $file;
349 root 1.3 $pb->save ("$file.png~", "png");
350     system "convert", "$file.png~", "-filter" => "lanczos", "-geometry" => "3.125%", "-quality" => 85, "$file.jpg";
351 root 1.6 #system "mogrify", "-colors" => 65536, "$file.png~"; # destroys transparency
352     system "pngcrush", "-q", "-m" => 7, "-rem", "alla", "-cc", "-reduce", "$file.png~", "$file.png";
353 root 1.3 unlink "$file.png~";
354 root 1.1 Storable::nstore $meta, "$file.pst";
355     }
356    
357    
358    
359    
360    
361