| 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 and specified .PAL file to TEMP/*.scr 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 $pal_name = $ARGV[1]; |
| 34 |
my $is_update = (defined $ARGV[2]) && ($ARGV[2] eq 'UPDATE'); |
| 35 |
my $ppm_name = $basename; |
| 36 |
my $want_width = $ENV{WIDTH} || 320; |
| 37 |
my $want_height = $ENV{HEIGHT} || 200; |
| 38 |
|
| 39 |
if (!defined $ppm_name or !defined $pal_name) { |
| 40 |
print "Usage: $0 <BASENAME.ppm> <main.pal> [UPDATE]\n"; |
| 41 |
print "Converts PPM file to Ironseed 320x200 (or other specified size via ENV) BASENAME.scr using main.pal for existing palette\n"; |
| 42 |
print "if 'UPDATE' is specified, the main.pal will be filled with extra palette entries if needed\n"; |
| 43 |
exit 1; |
| 44 |
} |
| 45 |
|
| 46 |
die "$basename does not look like .ppm file" unless $basename =~ s{\.ppm$}{}i; |
| 47 |
|
| 48 |
my $scr_final = $basename . '.scr'; |
| 49 |
my $scr_tmp = $scr_final . '.tmp'; |
| 50 |
|
| 51 |
|
| 52 |
open my $ppm_fd, '<', $ppm_name; |
| 53 |
|
| 54 |
sub get_line() |
| 55 |
{ |
| 56 |
my $ret=''; |
| 57 |
do { $ret = <$ppm_fd>; } while $ret =~ /^\s*#/; # skip comments |
| 58 |
chomp $ret; |
| 59 |
return $ret; |
| 60 |
} |
| 61 |
|
| 62 |
# FIXME: should support PPM comments, different whitespace etc. see ppm(5) |
| 63 |
my $format = get_line(); |
| 64 |
die "ERROR: P6 PPM file needed, not $format" unless $format eq 'P6'; |
| 65 |
my ($width, $height) = split ' ', get_line(); |
| 66 |
die "ERROR: not ${want_width}x${want_height} PPM file" unless $width==$want_width and $height==$want_height; |
| 67 |
my $bpp = get_line(); |
| 68 |
die "ERROR: must have 255 colors" unless $bpp==255; |
| 69 |
|
| 70 |
undef $/; # slurp the rest of the file in one go |
| 71 |
my @SCR = unpack "C*", <$ppm_fd>; |
| 72 |
|
| 73 |
# read in palette to %PALETTE |
| 74 |
my %PALETTE = (); |
| 75 |
|
| 76 |
open my $pal_fd, $is_update? '+<' : '<', $pal_name; |
| 77 |
my @_pal = unpack "C*", <$pal_fd>; |
| 78 |
|
| 79 |
my $pal_orig_max = scalar @_pal; |
| 80 |
for (my $pal_used=0; $pal_used < $pal_orig_max; $pal_used+=3) { |
| 81 |
my $r = $_pal[$pal_used]; |
| 82 |
my $g = $_pal[$pal_used+1]; |
| 83 |
my $b = $_pal[$pal_used+2]; |
| 84 |
my $pal_idx = "$r:$g:$b"; |
| 85 |
$PALETTE{$pal_idx} = int($pal_used / 3) if !defined $PALETTE{$pal_idx}; |
| 86 |
} |
| 87 |
|
| 88 |
my $pal_used = $pal_orig_max / 3; |
| 89 |
|
| 90 |
# write to SCR |
| 91 |
open my $scr_fd, '>', $scr_tmp; |
| 92 |
|
| 93 |
my $remains=64000; |
| 94 |
for (my $i = 0; $i < $width * $height * 3; $i+=3) { |
| 95 |
my $r = int($SCR[$i] / $COLOR_FACTOR); |
| 96 |
my $g = int($SCR[$i+1] / $COLOR_FACTOR); |
| 97 |
my $b = int($SCR[$i+2] / $COLOR_FACTOR); |
| 98 |
my $pal_idx = "$r:$g:$b"; |
| 99 |
my $val = $PALETTE{$pal_idx}; |
| 100 |
|
| 101 |
if (!defined $val) { # entry not in palette |
| 102 |
if ($is_update) { |
| 103 |
$val = $pal_used++; |
| 104 |
die "ERROR: palette overflow: $pal_used" if $val > $bpp; |
| 105 |
$PALETTE{$pal_idx} = $val; |
| 106 |
print $pal_fd chr($r).chr($g).chr($b); |
| 107 |
} else { |
| 108 |
#use Data::Dumper; |
| 109 |
#print Dumper(\%PALETTE); |
| 110 |
die "invalid RGB: $pal_idx not found in $pal_name at idx: $i, and UPDATE not specified"; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
print $scr_fd chr($val); |
| 115 |
$remains--; |
| 116 |
} |
| 117 |
|
| 118 |
if ($is_update) { |
| 119 |
print $pal_fd "\000\000\000" x ($bpp - $pal_used + 1); |
| 120 |
} |
| 121 |
close $pal_fd; |
| 122 |
|
| 123 |
print $scr_fd chr(0) x $remains; # fillup so scr2cpr.pas doesn't bail out |
| 124 |
|
| 125 |
close $scr_fd; |
| 126 |
rename $scr_tmp, $scr_final; |
| 127 |
|
| 128 |
print "Written: $scr_final" . ($is_update ? " and $pal_name ($pal_used/$bpp).\n" : ".\n"); |