diff --git a/lumps/cph/misc-lumps/Makefile b/lumps/cph/misc-lumps/Makefile index 36060196..36eaaf79 100644 --- a/lumps/cph/misc-lumps/Makefile +++ b/lumps/cph/misc-lumps/Makefile @@ -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 diff --git a/lumps/cph/misc-lumps/colormap.pl b/lumps/cph/misc-lumps/colormap.pl deleted file mode 100755 index f43068fa..00000000 --- a/lumps/cph/misc-lumps/colormap.pl +++ /dev/null @@ -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 -# 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; -} diff --git a/lumps/cph/misc-lumps/colormap.py b/lumps/cph/misc-lumps/colormap.py new file mode 100755 index 00000000..d7877f38 --- /dev/null +++ b/lumps/cph/misc-lumps/colormap.py @@ -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 +# 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 > 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) +