Debian Codegraph @

Data_Generators/misc/scr2ppm.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 TEMP/*.scr and TEMP/*.pal produced by quicksavescreen() to standard ppm(5) P3 ASCII image
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 $scr = shift;
33 my $pal = shift;
34 my $want_width = $ENV{WIDTH} || 320;
35 my $want_height = $ENV{HEIGHT} || 200;
36
37 if (!defined $scr) {
38 print "Usage: $0 <file.scr> [file.pal]\n";
39 print "Converts Ironseed 320x200 (or other specified size via ENV) SCR file to PPM on stdout\n";
40 print "Display with: $0 TEMP/current.scr data/main.pal | xli -zoom 200 -gamma 1 -dispgamma 1 stdin\n";
41 exit 1;
42 }
43
44 undef $/; # slurp file in one go
45
46 my @PALETTE=();
47 if (defined $pal) {
48 open my $pal_fd, '<', $pal;
49 @PALETTE = unpack "C*", <$pal_fd>;
50 }
51
52 print "P3\n$want_width $want_height\n255\n"; # see ppm(5)
53
54 open my $scr_fd, '<', $scr;
55 my $pixels = 1;
56 foreach my $b (unpack "C*", <$scr_fd>) {
57 if (@PALETTE) {
58 my $c1 = $PALETTE[$b*3] * $COLOR_FACTOR;
59 my $c2 = $PALETTE[$b*3+1] * $COLOR_FACTOR;
60 my $c3 = $PALETTE[$b*3+2] * $COLOR_FACTOR;
61 print "$c1 $c2 $c3\n";
62 } else { # grayscale
63 print "$b $b $b\n";
64 }
65 #last if $pixels++ > $want_width * $want_height;
66 }