-
Notifications
You must be signed in to change notification settings - Fork 0
/
mw_palette.pas
191 lines (160 loc) · 5.05 KB
/
mw_palette.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//------------------------------------------------------------------------------
//
// MAD2WAD - Create a Doom WAD file from Mars, Hero or Tao MAD files
//
// Copyright (C) 2021-2022 by Jim Valavanis
//
// 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.
//
// DESCRIPTION:
// Palette stuff
//
//------------------------------------------------------------------------------
// Site : https://sourceforge.net/projects/mars3d/
//------------------------------------------------------------------------------
// From dcolors.c (Doom Utilities Source - https://www.doomworld.com/idgames/historic/dmutils)
unit mw_palette;
interface
uses
SysUtils;
procedure MARS_CreateDoomPalette(const inppal: PByteArray; const outpal: PByteArray; const colormap: PByteArray);
implementation
procedure MARS_ColorShiftPalette(const inpal: PByteArray; const outpal: PByteArray;
const r, g, b: integer; const shift: integer; const steps: integer);
var
i: integer;
dr, dg, db: integer;
in_p, out_p: PByteArray;
begin
in_p := inpal;
out_p := outpal;
for i := 0 to 255 do
begin
dr := r - in_p[0];
dg := g - in_p[1];
db := b - in_p[2];
out_p[0] := in_p[0] + (dr * shift) div steps;
out_p[1] := in_p[1] + (dg * shift) div steps;
out_p[2] := in_p[2] + (db * shift) div steps;
in_p := @in_p[3];
out_p := @out_p[3];
end;
end;
procedure MARS_CopyPalette(const inppal, outpal: PByteArray);
var
i: integer;
begin
for i := 0 to 767 do
outpal[i] := inppal[i];
end;
function MARS_BestColor(const r, g, b: byte; const palette: PByteArray; const rangel, rangeh: integer): byte;
var
i: integer;
dr, dg, db: integer;
bestdistortion, distortion: integer;
bestcolor: integer;
pal: PByteArray;
begin
//
// let any color go to 0 as a last resort
//
bestdistortion := (r * r + g * g + b * b ) * 2;
bestcolor := 0;
pal := @palette[rangel * 3];
for i := rangel to rangeh do
begin
dr := r - pal[0];
dg := g - pal[1];
db := b - pal[2];
pal := @pal[3];
distortion := dr * dr + dg * dg + db * db;
if distortion < bestdistortion then
begin
if distortion = 0 then
begin
result := i; // perfect match
exit;
end;
bestdistortion := distortion;
bestcolor := i;
end;
end;
result := bestcolor;
end;
procedure MARS_FullRangePalette(const inppal: PByteArray);
var
i, mx: integer;
begin
mx := inppal[0];
for i := 1 to 767 do
if inppal[i] > mx then
mx := inppal[i];
if mx < 64 then
for i := 0 to 767 do
inppal[i] := 4 * inppal[i];
end;
procedure MARS_CreateDoomPalette(const inppal: PByteArray; const outpal: PByteArray; const colormap: PByteArray);
const
NUMLIGHTS = 32;
var
lightpalette: packed array[0..NUMLIGHTS + 1, 0..255] of byte;
i, l, c: integer;
red, green, blue: integer;
palsrc: PByte;
gray: double;
begin
MARS_FullRangePalette(inppal);
MARS_CopyPalette(inppal, outpal);
for i := 1 to 8 do
MARS_ColorShiftPalette(inppal, @outpal[768 * i], 255, 0, 0, i, 9);
for i := 1 to 4 do
MARS_ColorShiftPalette(inppal, @outpal[768 * (i + 8)], 215, 186, 69, i, 8);
MARS_ColorShiftPalette(inppal, @outpal[768 * 13], 0, 256, 0, 1, 8);
for i := 1 to 8 do
MARS_ColorShiftPalette(inppal, @outpal[768 * (i + 13)], 0, 255, 0, i, 9);
for l := 0 to NUMLIGHTS - 1 do
begin
palsrc := @inppal[0];
for c := 0 to 255 do
begin
red := palsrc^; inc(palsrc);
green := palsrc^; inc(palsrc);
blue := palsrc^; inc(palsrc);
red := (red * (NUMLIGHTS - l) + NUMLIGHTS div 2) div NUMLIGHTS;
green := (green * (NUMLIGHTS - l) + NUMLIGHTS div 2) div NUMLIGHTS;
blue := (blue * (NUMLIGHTS - l) + NUMLIGHTS div 2) div NUMLIGHTS;
lightpalette[l][c] := MARS_BestColor(red, green, blue, inppal, 0, 255);
end;
end;
palsrc := @inppal[0];
for c := 0 to 255 do
begin
red := palsrc^; inc(palsrc);
green := palsrc^; inc(palsrc);
blue := palsrc^; inc(palsrc);
// https://doomwiki.org/wiki/Carmack%27s_typo
// Correct Carmack's typo
gray := red * 0.299 / 256 + green * 0.587 / 265 + blue * 0.114 / 256;
gray := 1.0 - gray;
lightpalette[NUMLIGHTS][c] := MARS_BestColor(trunc(gray * 255), trunc(gray * 255), trunc(gray * 255), inppal, 0, 255);
end;
for c := 0 to 255 do
lightpalette[NUMLIGHTS + 1][c] := 0;
for i := 0 to NUMLIGHTS + 1 do
for c := 0 to 255 do
colormap[i * 256 + c] := lightpalette[i][c];
end;
end.