gen_desc.pl
1.59 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
#!/usr/bin/perl -w
die "Usage: gen_desc.pl <thumbnail RGBA image in SGI format> <title file(GB code)> <output desc file>\n" unless $#ARGV==2;
$root = $ENV{ROOT};
`$root/usr/sbin/rgb2c -F -f RGBA -s 16 -m texThumb $ARGV[0] > thumb.h`;
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,">thumb.bin");
binmode(OUT_DATA);
open(IN_FILE, "<thumb.h");
while (<IN_FILE>) {
if (/^\s+0x/) {
@pixel = split /,\s*/;
foreach $rgba (@pixel) {
$rgba =~ s/^\s*//;
write_short(OUT_DATA, $rgba);
}
}
}
close IN_FILE;
close OUT_DATA;
`gzip -c -9 -n thumb.bin | dd bs=10 skip=1 of=thumb.gz`;
$size = `stat thumb.gz`;
$size =~ /(.*Size: )(\d+)/;
$thumb_size = $2;
$thumb_size_h = sprintf ("%x", $2);
###write out thumb.gz size
open(OUT_DESC,">$ARGV[2]");
binmode(OUT_DESC);
write_short(OUT_DESC, $thumb_size_h);
close OUT_DESC;
###write out thumb.gz
`dd bs=1 seek=2 if=thumb.gz of=$ARGV[2]`;
###write out title
$seek_size = 2+$thumb_size;
`dd bs=1 seek=$seek_size if=$ARGV[1] of=$ARGV[2]`;
###write out terminating byte for title string
open(OUT_DESC,">>$ARGV[2]");
binmode(OUT_DESC);
printf OUT_DESC "\0";
close OUT_DESC;
$size = `stat $ARGV[2]`;
$size =~ /(.*Size: )(\d+)/;
if (int($2)>2048) {
printf STDOUT "ERROR: %s size %d greater than 2K\n", $ARGV[2], $2;
} else {
open(OUT_DESC,">>$ARGV[2]");
binmode(OUT_DESC);
for ($i=0; $i<(2048-int($2)); $i++) {
printf OUT_DESC "%c", 0;
}
close OUT_DESC;
}
`rm thumb.gz thumb.h thumb.bin`;