1 |
root |
1.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 |
|
|
# Modification by PeterM |
9 |
|
|
# Notes: I don't really know how this script was supposed to work |
10 |
|
|
# I use it like this: |
11 |
|
|
# > mkdir out (makes an output directory so I don't clobber my input) |
12 |
|
|
# > split-png-linux.pl file.111.png |
13 |
|
|
# |
14 |
|
|
# The results end up in the directory "out" |
15 |
|
|
|
16 |
|
|
$PNGSIZE=32; |
17 |
|
|
|
18 |
|
|
for ($i=0; $i <= $#ARGV; $i++) { |
19 |
|
|
@name = split(m#/#, $ARGV[$i]); |
20 |
|
|
$source = $name[$#name]; |
21 |
|
|
$#name--; |
22 |
|
|
$destdir=join(m#/#, @name); |
23 |
|
|
|
24 |
|
|
if ($source =~ /([\w-]+)\.(\d)(\d)(\d)\.png/ ) { |
25 |
|
|
$base = $1; |
26 |
|
|
$facing = $3; |
27 |
|
|
$animation = $4; |
28 |
|
|
} else { |
29 |
|
|
print STDERR "filename $source does not follow proper naming conventions - don't know how to name files - skipping\n"; |
30 |
|
|
next; |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
if (!open(DESC,"pngtopnm $ARGV[$i] | pnmfile |")) { |
34 |
|
|
print STDERR "Could not run pngtopnm $source | pnmfile\n"; |
35 |
|
|
next; |
36 |
|
|
} |
37 |
|
|
$desc = <DESC>; |
38 |
|
|
close(DESC); |
39 |
|
|
$desc =~ /(\d+) by (\d+)/; |
40 |
|
|
if ($1 == 0 || $2 == 0) { |
41 |
|
|
print STDERR "File $ARGV[$i] - file not described properly\n"; |
42 |
|
|
next; |
43 |
|
|
} |
44 |
|
|
if (($1 % 32) || ($2 % 32)) { |
45 |
|
|
print STDERR "File $ARGV[$i] not an increment of 32x32 size - size is $1x$2\n"; |
46 |
|
|
next; |
47 |
|
|
} |
48 |
|
|
$destx = $1/32; |
49 |
|
|
$desty = $2/32; |
50 |
|
|
print "File $ARGV[$i] will be split into $destx X $desty pieces\n"; |
51 |
|
|
|
52 |
|
|
$piece = 0; |
53 |
|
|
# The order of the for loops of x and y seem non intuitive, but believe |
54 |
|
|
# me, this does left to right, top to bottom (ie, as you read and |
55 |
|
|
# english language book) in terms of part numbering. |
56 |
|
|
|
57 |
|
|
for ($y=0; $y < $desty; $y++) { |
58 |
|
|
for ($x=0; $x < $destx; $x++) { |
59 |
|
|
$piece ++; |
60 |
|
|
# Deal with naming convention. piece 10 should be A, A ascii |
61 |
|
|
# code starts at 65. |
62 |
|
|
if ($piece > 9 ) { |
63 |
|
|
if($piece > 9 + 26) { |
64 |
|
|
$np = chr( $piece + 55 + 6 ) |
65 |
|
|
} else { |
66 |
|
|
|
67 |
|
|
$np = chr($piece + 55); |
68 |
|
|
} |
69 |
|
|
} else { |
70 |
|
|
$np = $piece; |
71 |
|
|
} |
72 |
|
|
$cutx = $x * 32; |
73 |
|
|
$cuty = $y * 32; |
74 |
|
|
system("pngtopnm -alpha $ARGV[$i] | pnmcut $cutx $cuty 32 32 > /tmp/alpha.$$"); |
75 |
|
|
system("pngtopnm $ARGV[$i] | pnmcut $cutx $cuty 32 32 > /tmp/png.$$"); |
76 |
|
|
system("pnmtopng -alpha /tmp/alpha.$$ /tmp/png.$$ > out/$base.$np$facing$animation.png"); |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
# do a little cleanup |
80 |
|
|
unlink("/tmp/alpha.$$"); |
81 |
|
|
unlink("/tmp/png.$$"); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
# system("pngtopnm -alpha $ARGV[$i] | pnmcut -left $cutx -top $cuty -width 32 -height 32 > /tmp/alpha.$$"); |
85 |
|
|
# system("pngtopnm $ARGV[$i] | pnmcut -left $cutx -top $cuty -width 32 -height 32 > /tmp/png.$$"); |