gzimg.pl
1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl -w
die "Usage: gzimg.pl <format(RGBA|IA|I)> <bpp(16|8|4)> <SGI image> <output file> <image var name> <gz size var name>\n" unless $#ARGV==5;
$format = shift;
$bpp = shift;
$in = shift;
$out = shift;
$gzimg_var = shift;
$gzsize_var = shift;
$root = $ENV{ROOT};
`$root/usr/sbin/rgb2c -F -f $format -s $bpp -m texImg $in > img.h`;
sub write_byte {
my $f = shift;
my $val = shift;
printf $f "%c", (hex($val)&0xFF);
}
sub write_short {
my $f = shift;
my $val = shift;
printf $f "%c", (hex($val)&0xFF00) >> 8;
printf $f "%c", (hex($val)&0x00FF);
}
open(OUT_DATA,">img.bin");
binmode(OUT_DATA);
open(IN_FILE, "<img.h");
while (<IN_FILE>) {
if (/^\s*0x/) {
@pixel = split /,\s*/;
foreach $rgba (@pixel) {
$rgba =~ s/^\s*//;
if ($format eq "CI") {
if ($rgba =~ /0x..../) {
write_short(OUT_DATA, $rgba);
} else {
write_byte(OUT_DATA, $rgba);
}
} else {
write_short(OUT_DATA, $rgba);
}
}
}
}
close IN_FILE;
close OUT_DATA;
`gzip -c -9 -n img.bin | dd bs=10 skip=1 of=img.gz`;
$idx=0;
open(INFILE, "<img.gz") or die "Can't open $in: $!";
binmode(INFILE);
open(OUTFILE, ">$out") or die "Can't open $out: $!";
$size = `stat img.gz`;
$size =~ /(.*Size: )(\d+)/;
$gz_size = $2;
printf OUTFILE "int %s = %s;\n", $gzsize_var, $gz_size;
printf OUTFILE "unsigned char %s[] __attribute__((aligned (16))) = { \n", $gzimg_var;
while( read(INFILE, $byte, 1) != 0 ) {
printf OUTFILE "0x%02x, ", vec($byte, 0, 8);
$idx++;
if ($idx%80==0) {
printf OUTFILE "\n";
}
}
printf OUTFILE "};\n";
close INFILE;
close OUTFILE;
`rm img.gz img.h img.bin`;