gen_desc.pl 1.59 KB
#!/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`;