Debian Codegraph @

Data_Generators/misc/ppm2icons.pl

raw
1 #!/usr/bin/perl
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
15 #
16 # On Debian systems, the complete text of the GNU General Public
17 # License, version 3, can be found in /usr/share/common-licenses/GPL-3.
18 #
19 # Copyright:
20 # 2020 Matija Nalis <[email protected]>
21
22 #
23 # Data Generator: converts standard binary P6 ppm(5) image to data/icons.vga + data/main.pal format used by Ironseed game
24 #
25
26 # FIXME: convert to write to stdout, so we can use it in Makefile
27
28 use strict;
29 use warnings;
30 use autodie qw/:all/;
31
32 my $COLOR_FACTOR = $ENV{COLORF} || 4; # game seems to be using <<2, which is *4
33
34 my $pal_name = $ARGV[0];
35 my $want_width = $ENV{WIDTH} || 17;
36 my $want_height = $ENV{HEIGHT} || 15;
37 my $want_icon_count = $ENV{COUNT} || 81;
38
39 if (!defined $pal_name) {
40 print "Usage: $0 <main.pal>\n";
41 print "Converts PPM file from STDIN (using specified main.pal) to Ironseed 81 icons of 17x15 icons.vga on STDOUT\n";
42 exit 1;
43 }
44
45 die "cowardly refusing to write binary file to TTY" if (-t STDOUT);
46
47 sub get_line()
48 {
49 my $ret='';
50 do { $ret = <STDIN>; } while $ret =~ /^\s*#/; # skip comments
51 chomp $ret;
52 return $ret;
53 }
54
55 # FIXME: should support PPM comments, different whitespace etc. see ppm(5)
56 my $format = get_line();
57 die "ERROR: P6 PPM file needed, not $format" unless $format eq 'P6';
58 my ($width, $height) = split ' ', get_line();
59 die "ERROR: not $want_icon_count icons of ${want_width}x${want_height} but ${width}x${height} PPM file" unless $height==$want_height and $width==$want_width*$want_icon_count ;
60 my $bpp = get_line();
61 die "ERROR: must have 255 colors" unless $bpp==255;
62
63 undef $/; # slurp the rest of the file in one go
64
65 # read in palette to %PALETTE
66 my %PALETTE = ();
67
68 open my $pal_fd, '<', $pal_name;
69 my @_pal = unpack "C*", <$pal_fd>;
70 close $pal_fd;
71
72 for (my $pal_used=0; $pal_used < 768; $pal_used+=3) {
73 my $r = $_pal[$pal_used];
74 my $g = $_pal[$pal_used+1];
75 my $b = $_pal[$pal_used+2];
76 my $pal_idx = "$r:$g:$b";
77 $PALETTE{$pal_idx} = int($pal_used / 3) if !defined $PALETTE{$pal_idx};
78 }
79
80 # map image colors to palette, store result in @vga_image
81 my @image = unpack "C*", <STDIN>;
82 my @vga_image=();
83
84 for (my $i = 0; $i < $height * $width * 3; $i+=3) {
85 my $r = int($image[$i] / $COLOR_FACTOR);
86 my $g = int($image[$i+1] / $COLOR_FACTOR);
87 my $b = int($image[$i+2] / $COLOR_FACTOR);
88 my $pal_idx = "$r:$g:$b";
89 my $val = $PALETTE{$pal_idx};
90
91 if (!defined $val) { # entry not in palette
92 #use Data::Dumper;
93 #print Dumper(\%PALETTE);
94 die "invalid RGB: $pal_idx not found in $pal_name at idx: $i";
95 }
96
97 my $vga_idx = int($i/3);
98 $vga_image[$vga_idx] = $val;
99 }
100
101 # sort @vga_image and write to icons.vga file on STDOUT
102 for my $icon (0 .. $want_icon_count-1) {
103 for my $x (0 .. $want_width-1) {
104 for my $y (0 .. $want_height-1) {
105 my $idx = ($icon * $want_width) + ($y * $want_width * $want_icon_count) + $x;
106 my $val = $vga_image[$idx];
107 #printf STDERR "icon=$icon x=$x y=$y (idx=$idx, val: \t%02X)\n", $val;
108 print chr($val);
109 }
110 }
111 }