forked from chocolatiers/doom-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcolors.c
295 lines (235 loc) · 5.27 KB
/
dcolors.c
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#define VERSION "1.1"
/*
=============================================================================
DCOLORS
by John Carmack
=============================================================================
*/
#include "cmdlib.h"
#include "lbmlib.h"
#define NUMLIGHTS 32
#define GRAYCOLORMAP 32
byte *palette;
byte lightpalette[NUMLIGHTS+2][256];
short color12s[NUMLIGHTS+2][256];
short color15s[NUMLIGHTS+2][256];
/*
=====================
=
= ColorShiftPalette
=
= at shift = 0, the colors are normal
= at shift = steps, the colors are all the given rgb
=====================
*/
void ColorShiftPalette (byte *inpal, byte *outpal
, int r, int g, int b, int shift, int steps)
{
int i;
int dr, dg, db;
byte *in_p, *out_p;
in_p = inpal;
out_p = outpal;
for (i=0 ; i<256 ; i++)
{
dr = r - in_p[0];
dg = g - in_p[1];
db = b - in_p[2];
out_p[0] = in_p[0] + dr*shift/steps;
out_p[1] = in_p[1] + dg*shift/steps;
out_p[2] = in_p[2] + db*shift/steps;
in_p += 3;
out_p += 3;
}
}
/*
===============
=
= BestColor
=
===============
*/
byte BestColor (int r, int g, int b, byte *palette, int rangel, int rangeh)
{
int i;
long dr, dg, db;
long bestdistortion, distortion;
int bestcolor;
byte *pal;
//
// let any color go to 0 as a last resort
//
bestdistortion = ( (long)r*r + (long)g*g + (long)b*b )*2;
bestcolor = 0;
pal = &palette[rangel*3];
for (i=rangel ; i<= rangeh ; i++)
{
dr = r - (int)pal[0];
dg = g - (int)pal[1];
db = b - (int)pal[2];
pal += 3;
distortion = dr*dr + dg*dg + db*db;
if (distortion < bestdistortion)
{
if (!distortion)
return i; // perfect match
bestdistortion = distortion;
bestcolor = i;
}
}
return bestcolor;
}
/*
=====================
=
= RF_BuildLights
=
= 0 is full palette
= NUMLIGHTS and NUMLIGHTS+1 are all black
=
=====================
*/
void RF_BuildLights (void)
{
int l,c;
int red,green,blue, ri, gi, bi;
byte *palsrc;
short color12,color15;
for (l=0;l<NUMLIGHTS;l++)
{
//printf ("%i.",NUMLIGHTS-l);
palsrc = palette;
for (c=0;c<256;c++)
{
red = *palsrc++;
green = *palsrc++;
blue = *palsrc++;
red = (red*(NUMLIGHTS-l)+NUMLIGHTS/2)/NUMLIGHTS;
green = (green*(NUMLIGHTS-l)+NUMLIGHTS/2)/NUMLIGHTS;
blue = (blue*(NUMLIGHTS-l)+NUMLIGHTS/2)/NUMLIGHTS;
ri = (red+8)>>4;
ri = ri > 15 ? 15 : ri;
gi = (green+8)>>4;
gi = gi > 15 ? 15 : gi;
bi = (blue+8)>>4;
bi = bi > 15 ? 15 : bi;
color12 = (ri<<12) + (gi<<8) + (bi<<4) + 15;
ri = (red+4)>>3;
ri = ri > 31 ? 31 : ri;
gi = (green+4)>>3;
gi = gi > 31 ? 31 : gi;
bi = (blue+4)>>3;
bi = bi > 31 ? 31 : bi;
color15 = (ri<<10) + (gi<<5) + bi;
lightpalette[l][c] = BestColor(red,green,blue,palette,0,255);
color12s[l][c] = color12;
color15s[l][c] = color15;
}
memcpy (screen,lightpalette[l],256);
screen+=320;
memcpy (screen,lightpalette[l],256);
screen+=320;
memcpy (screen,lightpalette[l],256);
screen+=320;
}
}
/*
=====================
=
= BuildSpecials
=
= Red and gray colormaps
=
=====================
*/
void BuildSpecials (void)
{
int c;
float red, green, blue, gray;
byte *palsrc;
palsrc = palette;
for (c=0;c<256;c++)
{
red = *palsrc++ / 256.0;
green = *palsrc++ / 256.0;
blue = *palsrc++ / 256.0;
gray = red*0.299 + green*0.587 + blue*0.144;
gray = 1.0 - gray;
lightpalette[GRAYCOLORMAP][c] = BestColor(gray*255,gray*255,gray*255,palette,0,255);
}
memcpy (screen,lightpalette[GRAYCOLORMAP],256);
screen+=320;
memcpy (screen,lightpalette[GRAYCOLORMAP],256);
screen+=320;
memcpy (screen,lightpalette[GRAYCOLORMAP],256);
screen+=320;
}
/*
====================
=
= main
=
====================
*/
int main (int argc, char **argv)
{
int i,handle;
byte *pic;
byte outpal[768];
printf ("\nDCOLORS "VERSION" by John Carmack, copyright (c) 1992 Id Software\n");
if (argc != 2)
Error ("dcolors picture.lbm");
//
// load lbm for base palette
//
LoadLBM (argv[1],&pic, &palette);
VGAMode ();
SetPalette (palette);
memcpy ( screen, pic, 64000);
free (pic); // don't care whats on it
//
// build palette shifts
//
handle = SafeOpenWrite ("playpal.lmp");
SafeWrite (handle, palette, 768);
GetKey ();
for (i=1 ; i<9 ; i++)
{
ColorShiftPalette (palette, outpal, 255, 0, 0, i, 9);
SafeWrite (handle, outpal, 768);
SetPalette (outpal);
GetKey ();
}
SetPalette (palette);
GetKey ();
for (i=1 ; i<5 ; i++)
{
ColorShiftPalette (palette, outpal, 215, 186, 69, i, 8);
SafeWrite (handle, outpal, 768);
SetPalette (outpal);
GetKey ();
}
ColorShiftPalette (palette, outpal, 0, 256, 0, 1, 8);
SafeWrite (handle, outpal, 768);
SetPalette (outpal);
GetKey ();
close (handle);
SetPalette (palette);
//
// build light mappings
//
RF_BuildLights ();
//
// build special maps
//
BuildSpecials ();
//
// write lumps
//
SaveFile ("colormap.lmp",&lightpalette[0][0],sizeof(lightpalette));
// SaveFile ("colors12.lmp",&color12s[0][0],NUMLIGHTS*512);
// SaveFile ("colors15.lmp",&color15s[0][0],NUMLIGHTS*512);
GetKey ();
TextMode ();
return 0;
}