1 |
#!/usr/bin/perl |
2 |
|
3 |
# File by mwedel@scruz.net to deal with splitting png's. |
4 |
# Takes any number of files on the command line, but all the split images |
5 |
# are dumped in the current directory. |
6 |
# does preserve alpha channel. Tested on skree images. |
7 |
|
8 |
$PNGSIZE=32; |
9 |
|
10 |
for ($i=0; $i <= $#ARGV; $i++) { |
11 |
@name = split(m#/#, $ARGV[$i]); |
12 |
$source = $name[$#name]; |
13 |
$#name--; |
14 |
$destdir=join(m#/#, @name); |
15 |
|
16 |
if ($source =~ /(\w+)\.(\d)(\d)(\d)\.png/ ) { |
17 |
$base = $1; |
18 |
$facing = $3; |
19 |
$animation = $4; |
20 |
} else { |
21 |
print STDERR "filename $source does not follow proper naming conventions - don't know how to name files - skipping\n"; |
22 |
next; |
23 |
} |
24 |
|
25 |
if (!open(DESC,"pngtopnm $ARGV[$i] | pnmfile |")) { |
26 |
print STDERR "Could not run pngtopnm $source | pnmfile\n"; |
27 |
next; |
28 |
} |
29 |
$desc = <DESC>; |
30 |
close(DESC); |
31 |
$desc =~ /(\d+) by (\d+)/; |
32 |
if ($1 == 0 || $2 == 0) { |
33 |
print STDERR "File $ARGV[$i] - file not described properly\n"; |
34 |
next; |
35 |
} |
36 |
if (($1 % 32) || ($2 % 32)) { |
37 |
print STDERR "File $ARGV[$i] not an increment of 32x32 size - size is $1x$2\n"; |
38 |
next; |
39 |
} |
40 |
$destx = $1/32; |
41 |
$desty = $2/32; |
42 |
print "File $ARGV[$i] will be split into $destx X $desty pieces\n"; |
43 |
|
44 |
$piece = 0; |
45 |
# The order of the for loops of x and y seem non intuitive, but believe |
46 |
# me, this does left to right, top to bottom (ie, as you read and |
47 |
# english language book) in terms of part numbering. |
48 |
|
49 |
for ($y=0; $y < $desty; $y++) { |
50 |
for ($x=0; $x < $destx; $x++) { |
51 |
$piece ++; |
52 |
# Deal with naming convention. piece 10 should be A, A ascii |
53 |
# code starts at 65. |
54 |
if ($piece > 9 ) { |
55 |
$np = chr($piece + 55); |
56 |
} else { |
57 |
$np = $piece; |
58 |
} |
59 |
$cutx = $x * 32; |
60 |
$cuty = $y * 32; |
61 |
system("pngtopnm -alpha $ARGV[$i] | pnmcut -left $cutx -top $cuty -width 32 -height 32 > /tmp/alpha.$$"); |
62 |
system("pngtopnm $ARGV[$i] | pnmcut -left $cutx -top $cuty -width 32 -height 32 > /tmp/png.$$"); |
63 |
system("pnmtopng -alpha /tmp/alpha.$$ /tmp/png.$$ > $base.$np$facing$animation.png"); |
64 |
} |
65 |
} |
66 |
# do a little cleanup |
67 |
unlink("/tmp/alpha.$$"); |
68 |
unlink("/tmp/png.$$"); |
69 |
} |