syslinux-3.08-2 sources from FC4
[bootcd.git] / syslinux / lss16toppm
1 #!/usr/bin/perl
2 ## $Id: lss16toppm,v 1.6 2004/12/14 23:03:28 hpa Exp $
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.
12 ##
13 ## -----------------------------------------------------------------------
14
15 ##
16 ## lss16toppm:
17 ## Convert an LSS-16 image to PPM
18 ##
19 ## Usage:
20 ##
21 ##      lss16toppm [-map] < file.lss > file.ppm
22 ##
23 ## The -map causes the color map to be output on stderr.
24 ##
25
26 eval { use bytes; };
27 eval { binmode STDIN; };
28 eval { binmode STDOUT; };
29
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;
37     }
38 }
39
40 if ( read(STDIN, $header, 56) != 56 ) {
41     print STDERR "$0: Short file\n";
42     exit 1;
43 }
44
45 ($magic, $xsize, $ysize, @colorset) = unpack("Vvvc48", $header);
46
47 if ( $magic != 0x1413f33d ) {
48     print STDERR "$0: Invalid file format\n";
49     exit 1;
50 }
51
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);
57
58     $color{$i} = pack("ccc", $r, $g, $b);
59
60     if ( $map ) {
61         printf STDERR "#%02x%02x%02x=%d\n", $r, $g, $b, $i;
62     }
63 }
64
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;
74         }
75         $ch = ord($ch);
76         $nybble_buf = $ch >> 4;
77         $n = $ch & 0xF;
78     }
79     return $n;
80 }
81
82 print "P6\n";
83 print "$xsize $ysize\n";
84 print "255\n";
85
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();
92
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;
104             }
105             # Truncate overlong runs
106             $c = $xsize-$x if ( $c > $xsize-$x );
107             # Output run
108             print $color{$n} x $c;
109             $x += $c;
110         }
111     }
112 }