3 ## -----------------------------------------------------------------------
5 ## Copyright 2001-2004 H. Peter Anvin - All Rights Reserved
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation, Inc., 53 Temple Place Ste 330,
10 ## Boston MA 02111-1307, USA; either version 2 of the License, or
11 ## (at your option) any later version; incorporated herein by reference.
13 ## -----------------------------------------------------------------------
17 ## Convert an LSS-16 image to PPM
21 ## lss16toppm [-map] < file.lss > file.ppm
23 ## The -map causes the color map to be output on stderr.
27 eval { binmode STDIN
; };
28 eval { binmode STDOUT
; };
31 foreach $arg ( @ARGV ) {
32 if ( $arg eq '-map' ) {
35 print STDERR
"$0: Unknown option: $arg\n";
40 if ( read(STDIN
, $header, 56) != 56 ) {
41 print STDERR
"$0: Short file\n";
45 ($magic, $xsize, $ysize, @colorset) = unpack("Vvvc48", $header);
47 if ( $magic != 0x1413f33d ) {
48 print STDERR
"$0: Invalid file format\n";
53 for ( $i = 0 ; $i < 16 ; $i++ ) {
54 $r = int((shift @colorset) * 255 / 63 + 0.5);
55 $g = int((shift @colorset) * 255 / 63 + 0.5);
56 $b = int((shift @colorset) * 255 / 63 + 0.5);
58 $color{$i} = pack("ccc", $r, $g, $b);
61 printf STDERR
"#%02x%02x%02x=%d\n", $r, $g, $b, $i;
67 if ( defined($nybble_buf) ) {
71 if ( read(STDIN
, $ch, 1) != 1 ) {
72 print STDERR
"$0: Short read on input (file corrupt)\n";
76 $nybble_buf = $ch >> 4;
83 print "$xsize $ysize\n";
86 for ( $y = 0 ; $y < $ysize ; $y++ ) {
89 undef $nybble_buf; # Nybble buffer starts clear on each line
90 while ( $x < $xsize ) {
102 $c += get_nybble
() << 4;
105 # Truncate overlong runs
106 $c = $xsize-$x if ( $c > $xsize-$x );
108 print $color{$n} x
$c;