mirror of
https://github.com/freedoom/freedoom.git
synced 2025-09-02 07:25:45 -04:00
Convert colormap.py to Python.
This commit is contained in:
parent
0249499031
commit
4ededbd21e
3 changed files with 129 additions and 92 deletions
|
@ -4,7 +4,7 @@ playpal.lmp : playpal-base.lmp playpal.pl
|
|||
./playpal.pl playpal-base.lmp > playpal.lmp
|
||||
|
||||
colormap.lmp : playpal.lmp colormap.pl
|
||||
./colormap.pl playpal.lmp > colormap.lmp
|
||||
./colormap.py playpal.lmp > colormap.lmp
|
||||
|
||||
SOURCES=playpal.pl colormap.pl COPYING Makefile
|
||||
VERSION=0.0.1
|
||||
|
|
|
@ -1,91 +0,0 @@
|
|||
#!/usr/bin/perl -w
|
||||
# Takes PLAYPAL as input (filename is the only parameter)
|
||||
# Produces a light graduated COLORMAP on stdout
|
||||
# O(n^2)
|
||||
#
|
||||
# Copyright (C) 2001 Colin Phipps <cphipps@doomworld.com>
|
||||
# Parts copyright (C) 1999 by id Software (http://www.idsoftware.com/)
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
use strict;
|
||||
|
||||
my @colours;
|
||||
|
||||
# Return palette read from named file
|
||||
sub read_palette ($) {
|
||||
{
|
||||
my $palf = shift;
|
||||
open(PALF,"<$palf") or die "failed to open PLAYPAL: $!";
|
||||
}
|
||||
@colours = ();
|
||||
foreach my $i (0..255) {
|
||||
my $e;
|
||||
read PALF,$e,3;
|
||||
push @colours,[unpack("CCC",$e)];
|
||||
}
|
||||
close PALF;
|
||||
return \@colours;
|
||||
}
|
||||
|
||||
sub sq($) { my $x = shift; $x*$x }
|
||||
|
||||
# Return closest palette entry to the given RGB triple
|
||||
sub search_palette {
|
||||
my ($r,$g,$b) = @_;
|
||||
my $d = 100000; my $n = -1;
|
||||
my $i = 0;
|
||||
foreach my $c (@colours) {
|
||||
my $thisdist = sq($c->[0] - $r) + sq($c->[1] - $g) + sq($c->[2] - $b);
|
||||
if ($thisdist < $d) { $d = $thisdist; $n = $i; }
|
||||
$i++;
|
||||
}
|
||||
die "failed to find any close colour?" if $n == -1;
|
||||
return $n;
|
||||
}
|
||||
|
||||
sub darkenedpalette($) {
|
||||
my $darkensub = shift;
|
||||
my @pal;
|
||||
foreach my $c (@colours) {
|
||||
my @d = $darkensub->(@$c);
|
||||
push @pal,search_palette(@d);
|
||||
}
|
||||
return \@pal;
|
||||
}
|
||||
|
||||
sub makedarkenrgbbyfactor($) {
|
||||
my $f = shift;
|
||||
return sub { my ($r,$g,$b) = @_; return (int($r*$f),int($g*$f),int($b*$f)) };
|
||||
}
|
||||
|
||||
read_palette(shift @ARGV);
|
||||
foreach my $i (0..31) {
|
||||
my $p = darkenedpalette( makedarkenrgbbyfactor((32.0-$i)/32.0));
|
||||
print map { pack("C",$_) } @$p;
|
||||
print STDERR ".";
|
||||
}
|
||||
print STDERR "\n";
|
||||
# And now INVERSECOLORMAP
|
||||
{
|
||||
my $p = darkenedpalette(
|
||||
sub {
|
||||
my ($r,$g,$b) = @_;
|
||||
my $x = int (256 - ($r+$g+$b)/3);
|
||||
return ($x,$x,$x);
|
||||
}
|
||||
);
|
||||
print map { pack("C",$_) } @$p;
|
||||
}
|
128
lumps/cph/misc-lumps/colormap.py
Executable file
128
lumps/cph/misc-lumps/colormap.py
Executable file
|
@ -0,0 +1,128 @@
|
|||
#!/usr/bin/env python
|
||||
#
|
||||
# Takes PLAYPAL as input (filename is the only parameter)
|
||||
# Produces a light graduated COLORMAP on stdout
|
||||
# O(n^2)
|
||||
#
|
||||
# This is a Python version of Colin's original Perl script.
|
||||
#
|
||||
# Copyright (C) 2001 Colin Phipps <cphipps@doomworld.com>
|
||||
# Copyright (C) 2008 Simon Howard
|
||||
# Parts copyright (C) 1999 by id Software (http://www.idsoftware.com/)
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
import sys
|
||||
|
||||
# Return palette read from named file
|
||||
|
||||
def read_palette(filename):
|
||||
f = file(filename)
|
||||
|
||||
colors = []
|
||||
|
||||
for i in range(256):
|
||||
color = f.read(3)
|
||||
|
||||
colors.append((ord(color[0]), ord(color[1]), ord(color[2])))
|
||||
|
||||
return colors
|
||||
|
||||
def square(x):
|
||||
return x * x
|
||||
|
||||
# Return closest palette entry to the given RGB triple
|
||||
|
||||
def search_palette(colors, target):
|
||||
best_diff = None
|
||||
best_index = None
|
||||
|
||||
for i in range(len(colors)):
|
||||
color = colors[i]
|
||||
|
||||
diff = square(target[0] - color[0]) \
|
||||
+ square(target[1] - color[1]) \
|
||||
+ square(target[2] - color[2])
|
||||
|
||||
if best_index is None or diff < best_diff:
|
||||
best_diff = diff
|
||||
best_index = i
|
||||
|
||||
return best_index
|
||||
|
||||
def generate_colormap(colors, transform_function):
|
||||
result = []
|
||||
|
||||
for color in colors:
|
||||
transformed_color = transform_function(color)
|
||||
transformed_index = search_palette(colors, transformed_color)
|
||||
result.append(transformed_index)
|
||||
|
||||
return result
|
||||
|
||||
def generate_darkened_colormap(colors, factor):
|
||||
|
||||
factor = 0.5
|
||||
darken_function = lambda c: [ c[0] * factor, \
|
||||
c[1] * factor, \
|
||||
c[2] * factor ]
|
||||
|
||||
return generate_colormap(colors, darken_function)
|
||||
|
||||
def output_colormap(colormap):
|
||||
for c in colormap:
|
||||
sys.stdout.write(chr(c))
|
||||
|
||||
def inverse_color(color):
|
||||
average = (color[0] + color[1] + color[2]) / 3
|
||||
inverse = 255 - average
|
||||
|
||||
return (inverse, inverse, inverse)
|
||||
|
||||
def print_palette(colors):
|
||||
for y in range(16):
|
||||
for x in range(16):
|
||||
color = colors[y * 16 + x]
|
||||
|
||||
print "#%02x%02x%02x" % color,
|
||||
|
||||
print
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print "Usage: %s <base filename> > output-file.lmp"
|
||||
sys.exit(1)
|
||||
|
||||
colors = read_palette(sys.argv[1])
|
||||
|
||||
#print_palette(colors)
|
||||
#sys.exit(0)
|
||||
|
||||
# Main color ranges
|
||||
|
||||
for i in range(32):
|
||||
darken_factor = (32 - i) / 32.0
|
||||
colormap = generate_darkened_colormap(colors, darken_factor)
|
||||
output_colormap(colormap)
|
||||
|
||||
sys.stderr.write(".")
|
||||
|
||||
sys.stderr.write("\n")
|
||||
|
||||
# Print inverse color map
|
||||
|
||||
inverse_colormap = generate_colormap(colors, inverse_color)
|
||||
|
||||
output_colormap(inverse_colormap)
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue