Debian Codegraph @

Data_Generators/misc/ppm2scr.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 TEMP/*.scr + TEMP/*.pal format used by quickloadscreen()
24 #
25
26 use strict;
27 use warnings;
28 use autodie qw/:all/;
29
30 my $COLOR_FACTOR = $ENV{COLORF} || 4; # game seems to be using <<2, which is *4
31
32 my $basename = $ARGV[0];
33 my $ppm_name = $basename;
34 my $want_width = $ENV{WIDTH} || 320;
35 my $want_height = $ENV{HEIGHT} || 200;
36
37 if (!defined $ppm_name) {
38 print "Usage: $0 <BASENAME.ppm>\n";
39 print "Converts PPM file to Ironseed 320x200 (or other specified size via ENV) BASENAME.scr and BASENAME.pal files\n";
40 exit 1;
41 }
42
43 die "$basename does not look like .ppm file" unless $basename =~ s{\.ppm$}{}i;
44
45 my $pal_final = $basename . '.pal';
46 my $scr_final = $basename . '.scr';
47 my $pal_tmp = $pal_final . '.tmp';
48 my $scr_tmp = $scr_final . '.tmp';
49
50
51 open my $ppm_fd, '<', $ppm_name;
52
53 sub get_line()
54 {
55 my $ret='';
56 do { $ret = <$ppm_fd>; } while $ret =~ /^\s*#/; # skip comments
57 chomp $ret;
58 return $ret;
59 }
60
61 # FIXME: should support PPM comments, different whitespace etc. see ppm(5)
62 my $format = get_line();
63 die "ERROR: P6 PPM file needed, not $format" unless $format eq 'P6';
64 my ($width, $height) = split ' ', get_line();
65 die "ERROR: not ${want_width}x${want_height} PPM file" unless $width==$want_width and $height==$want_height;
66 my $bpp = get_line();
67 die "ERROR: must have 255 colors" unless $bpp==255;
68
69 undef $/; # slurp the rest of the file in one go
70 my @SCR = unpack "C*", <$ppm_fd>;
71 my %PALETTE = ();
72 my $pal_used = 0;
73
74 open my $pal_fd, '>', $pal_tmp;
75 open my $scr_fd, '>', $scr_tmp;
76
77 my $remains=64000;
78 for (my $i = 0; $i < $width * $height * 3; $i+=3) {
79 my $r = int($SCR[$i] / $COLOR_FACTOR);
80 my $g = int($SCR[$i+1] / $COLOR_FACTOR);
81 my $b = int($SCR[$i+2] / $COLOR_FACTOR);
82 my $pal_idx = "$r:$g:$b";
83 my $val = $PALETTE{$pal_idx};
84
85 if (!defined $val) { # add new entry to palette
86 $val = $pal_used++;
87 die "ERROR: palette overflow: $pal_used" if $val > $bpp;
88 $PALETTE{$pal_idx} = $val;
89 print $pal_fd chr($r).chr($g).chr($b);
90 }
91
92 print $scr_fd chr($val);
93 $remains--;
94 }
95
96 print $scr_fd chr(0) x $remains; # fillup so scr2cpr.pas doesn't bail out
97
98 print $pal_fd "\000\000\000" x ($bpp - $pal_used + 1);
99
100
101 close $pal_fd;
102 close $scr_fd;
103 rename $pal_tmp, $pal_final;
104 rename $scr_tmp, $scr_final;
105
106 print "Written: $scr_final and $pal_final.\n";
107 print "Done, used $pal_used / $bpp colors in palette.\n";