3 # Convert an LSS-16 image to PPM
7 # lss16toppm [-map] < file.lss > file.ppm
9 # The -map causes the color map to be output on stderr.
13 foreach $arg ( @ARGV ) {
14 if ( $arg eq '-map' ) {
17 print STDERR "$0: Unknown option: $arg\n";
22 if ( read(STDIN, $header, 56) != 56 ) {
23 print STDERR "$0: Short file\n";
27 ($magic, $xsize, $ysize, @colorset) = unpack("Vvvc48", $header);
29 if ( $magic != 0x1413f33d ) {
30 print STDERR "$0: Invalid file format\n";
35 for ( $i = 0 ; $i < 16 ; $i++ ) {
36 $r = int((shift @colorset) * 255 / 63 + 0.5);
37 $g = int((shift @colorset) * 255 / 63 + 0.5);
38 $b = int((shift @colorset) * 255 / 63 + 0.5);
40 $color{$i} = pack("ccc", $r, $g, $b);
43 printf STDERR "#%02x%02x%02x=%d\n", $r, $g, $b, $i;
49 if ( defined($nybble_buf) ) {
53 if ( read(STDIN, $ch, 1) != 1 ) {
54 print STDERR "$0: Short read on input (file corrupt)\n";
58 $nybble_buf = $ch >> 4;
65 print "$xsize $ysize\n";
68 for ( $y = 0 ; $y < $ysize ; $y++ ) {
71 undef $nybble_buf; # Nybble buffer starts clear on each line
72 while ( $x < $xsize ) {
84 $c += get_nybble() << 4;
87 # Truncate overlong runs
88 $c = $xsize-$x if ( $c > $xsize-$x );