Adding a com32 module to handle DMI
[syslinux.git] / lss16toppm
blobd24262edf8c9400fa61748d8554d260a041f9c95
1 #!/usr/bin/perl
2 ## $Id$
3 ## -----------------------------------------------------------------------
4 ##
5 ## Copyright 2001-2004 H. Peter Anvin - All Rights Reserved
6 ##
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 ## -----------------------------------------------------------------------
16 ## lss16toppm:
17 ## Convert an LSS-16 image to PPM
19 ## Usage:
21 ## lss16toppm [-map] < file.lss > file.ppm
23 ## The -map causes the color map to be output on stderr.
26 eval { use bytes; };
27 eval { binmode STDIN; };
28 eval { binmode STDOUT; };
30 $map = 0;
31 foreach $arg ( @ARGV ) {
32 if ( $arg eq '-map' ) {
33 $map = 1;
34 } else {
35 print STDERR "$0: Unknown option: $arg\n";
36 exit 127;
40 if ( read(STDIN, $header, 56) != 56 ) {
41 print STDERR "$0: Short file\n";
42 exit 1;
45 ($magic, $xsize, $ysize, @colorset) = unpack("Vvvc48", $header);
47 if ( $magic != 0x1413f33d ) {
48 print STDERR "$0: Invalid file format\n";
49 exit 1;
52 %color = ();
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);
60 if ( $map ) {
61 printf STDERR "#%02x%02x%02x=%d\n", $r, $g, $b, $i;
65 sub get_nybble() {
66 my($ch,$n);
67 if ( defined($nybble_buf) ) {
68 $n = $nybble_buf;
69 undef $nybble_buf;
70 } else {
71 if ( read(STDIN, $ch, 1) != 1 ) {
72 print STDERR "$0: Short read on input (file corrupt)\n";
73 exit 1;
75 $ch = ord($ch);
76 $nybble_buf = $ch >> 4;
77 $n = $ch & 0xF;
79 return $n;
82 print "P6\n";
83 print "$xsize $ysize\n";
84 print "255\n";
86 for ( $y = 0 ; $y < $ysize ; $y++ ) {
87 $x = 0;
88 $last = 0;
89 undef $nybble_buf; # Nybble buffer starts clear on each line
90 while ( $x < $xsize ) {
91 $n = get_nybble();
93 if ( $n != $last ) {
94 print $color{$n};
95 $last = $n;
96 $x++;
97 } else {
98 $c = get_nybble();
99 if ( $c == 0 ) {
100 # Double-nybble run
101 $c = get_nybble();
102 $c += get_nybble() << 4;
103 $c += 16;
105 # Truncate overlong runs
106 $c = $xsize-$x if ( $c > $xsize-$x );
107 # Output run
108 print $color{$n} x $c;
109 $x += $c;