1 |
root |
1.1 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
# by peterm@langmuir.eecs.berkeley.edu |
4 |
|
|
|
5 |
|
|
# This program takes a .xpm as input and splits into |
6 |
|
|
# 24x24 chunks. |
7 |
|
|
|
8 |
|
|
$base_name=$ARGV[0]; |
9 |
|
|
print "splitting ", $base_name, "\n"; |
10 |
|
|
|
11 |
|
|
open(XPM_FILE,$ARGV[0]) || die "Usage: splitter.pl <filename>"; |
12 |
|
|
|
13 |
|
|
# get the header stuff from the original file. |
14 |
|
|
while ( (@tmp=split(/[ \s]+/,$headline=<XPM_FILE>))[0] eq "/*") { |
15 |
|
|
}; |
16 |
|
|
@name=split(/\./,$ARGV[0]); |
17 |
|
|
#$name1 = (@tmp=split(/\*/,$headline))[1]; |
18 |
|
|
#$name2 = (@tmp=split(/[=\[\{]/,$name1))[0]; |
19 |
|
|
|
20 |
|
|
|
21 |
|
|
print "name of the bitmap is:", $name[0], "\n"; |
22 |
|
|
#get the dimensions/parameters of the original xpm file. |
23 |
|
|
|
24 |
|
|
while ( (@line=split(/[ \s"]+/,<XPM_FILE>))[0] eq "/*") { |
25 |
|
|
}; |
26 |
|
|
|
27 |
|
|
$height = $line[2]; |
28 |
|
|
$ncolors = $line[3]; |
29 |
|
|
$width = $line[1]; |
30 |
|
|
$chars_per_pixel= $line[4]; |
31 |
|
|
|
32 |
|
|
print "width = ",$width, " ", "height =", $height, " number colors =", $ncolors, " chars per pixel =", $chars_per_pixel, "\n"; |
33 |
|
|
|
34 |
|
|
if ( $height % 24 != 0 || $width % 24 != 0 ) { |
35 |
|
|
die "Use some other program to make the dimensions of this xpm divisible by 24 first.\n"; |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
$i_color=0; # index for getting lines with colors |
39 |
|
|
while($i_color < $ncolors) { |
40 |
|
|
$tmp= <XPM_FILE>; #get one line from the xpm file |
41 |
|
|
if(substr($tmp,0,1) eq '"') { |
42 |
|
|
@colors[$i_color] = $tmp; |
43 |
|
|
$i_color++; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
#read in the rest of the bitmap |
49 |
|
|
$n=0; |
50 |
|
|
while( $n < $height ) { |
51 |
|
|
$tmp = <XPM_FILE>; |
52 |
|
|
if(substr($tmp,0,1) eq '"') { |
53 |
|
|
@picture[$n] = $tmp; |
54 |
|
|
$n++; |
55 |
|
|
} |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
#@picture = <XPM_FILE>; |
59 |
|
|
|
60 |
|
|
$n_rows = $height / 24; |
61 |
|
|
$n_cols = $width / 24; |
62 |
|
|
|
63 |
|
|
# maximum supported dimension has 61 squares |
64 |
|
|
$indexes = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
65 |
|
|
|
66 |
|
|
# Now the actual work of splitting the xpm file into many little files. |
67 |
|
|
|
68 |
|
|
print $n_rows," ",$n_cols,"\n"; |
69 |
|
|
for( $j = 0; $j < $n_rows; $j++) { |
70 |
|
|
for( $i = 0; $i < $n_cols; $i++ ) { |
71 |
|
|
#choose a filename |
72 |
|
|
$outname = join('',$name[0],"_",substr($indexes,$i + $j*$n_cols,1)); |
73 |
|
|
$out_fname = join('',$name[0],".",substr($indexes,$i + $j*$n_cols,1),"11.xpm"); |
74 |
|
|
print $outname," ",$out_fname,"\n"; |
75 |
|
|
$outname2= join('',$outname,"_xpm"); |
76 |
|
|
|
77 |
|
|
#open the dest file |
78 |
|
|
open(OUTFILE,">$out_fname"); |
79 |
|
|
|
80 |
|
|
# Write the header lines |
81 |
|
|
print OUTFILE "/* XPM */\n"; |
82 |
|
|
print OUTFILE join(' ',"static","char","*",$outname2, "[] = {\n"); |
83 |
|
|
# Write the dimension line |
84 |
|
|
print OUTFILE join(' ','"',"24","24",$ncolors,$chars_per_pixel,'"',",\n"); |
85 |
|
|
|
86 |
|
|
# Write the colors |
87 |
|
|
for($i_color=0;$i_color < $ncolors; $i_color++) { |
88 |
|
|
print OUTFILE $colors[$i_color]; |
89 |
|
|
}; |
90 |
|
|
|
91 |
|
|
# Write the proper bitmaps! |
92 |
|
|
for($k=0;$k<23;$k++) { |
93 |
|
|
print OUTFILE join('','"',substr($picture[$j*24+$k],$i*24+1,24*$chars_per_pixel),'"',",\n"); |
94 |
|
|
} |
95 |
|
|
print OUTFILE join('','"',substr($picture[$j*24+$k],$i*24+1,24*$chars_per_pixel),'"',"};\n"); |
96 |
|
|
|
97 |
|
|
} |
98 |
|
|
} |