2 ## -----------------------------------------------------------------------
4 ## Copyright 2001-2008 H. Peter Anvin - All Rights Reserved
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 ## Boston MA 02111-1307, USA; either version 2 of the License, or
10 ## (at your option) any later version; incorporated herein by reference.
12 ## -----------------------------------------------------------------------
16 ## Convert an LSS-16 image to PPM
20 ## lss16toppm [-map] < file.lss > file.ppm
22 ## The -map causes the color map to be output on stderr.
26 eval { binmode STDIN
; };
27 eval { binmode STDOUT
; };
30 foreach $arg ( @ARGV ) {
31 if ( $arg eq '-map' ) {
34 print STDERR
"$0: Unknown option: $arg\n";
39 if ( read(STDIN
, $header, 56) != 56 ) {
40 print STDERR
"$0: Short file\n";
44 ($magic, $xsize, $ysize, @colorset) = unpack("Vvvc48", $header);
46 if ( $magic != 0x1413f33d ) {
47 print STDERR
"$0: Invalid file format\n";
52 for ( $i = 0 ; $i < 16 ; $i++ ) {
53 $r = int((shift @colorset) * 255 / 63 + 0.5);
54 $g = int((shift @colorset) * 255 / 63 + 0.5);
55 $b = int((shift @colorset) * 255 / 63 + 0.5);
57 $color{$i} = pack("ccc", $r, $g, $b);
60 printf STDERR
"#%02x%02x%02x=%d\n", $r, $g, $b, $i;
66 if ( defined($nybble_buf) ) {
70 if ( read(STDIN
, $ch, 1) != 1 ) {
71 print STDERR
"$0: Short read on input (file corrupt)\n";
75 $nybble_buf = $ch >> 4;
82 print "$xsize $ysize\n";
85 for ( $y = 0 ; $y < $ysize ; $y++ ) {
88 undef $nybble_buf; # Nybble buffer starts clear on each line
89 while ( $x < $xsize ) {
101 $c += get_nybble
() << 4;
104 # Truncate overlong runs
105 $c = $xsize-$x if ( $c > $xsize-$x );
107 print $color{$n} x
$c;