| 1 |
// |
| 2 |
// Copyright(C) 2023 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA CR Table |
| 16 |
// |
| 17 |
|
| 18 |
#include "doomdef.h" |
| 19 |
#include "lprintf.h" |
| 20 |
#include "w_wad.h" |
| 21 |
#include "v_video.h" |
| 22 |
|
| 23 |
#include "dsda/palette.h" |
| 24 |
#include "dsda/utility.h" |
| 25 |
|
| 26 |
#include "cr_table.h" |
| 27 |
|
| 28 |
typedef struct { |
| 29 |
int r1, g1, b1; |
| 30 |
int r2, g2, b2; |
| 31 |
} cr_range_t; |
| 32 |
|
| 33 |
// Default values - overridden by DSDACR lump |
| 34 |
cr_range_t cr_range[CR_HUD_LIMIT] = { |
| 35 |
[CR_DEFAULT] = { 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF }, |
| 36 |
[CR_BRICK] = { 0x47, 0x00, 0x00, 0xFF, 0xB8, 0xB8 }, |
| 37 |
[CR_TAN] = { 0x33, 0x2B, 0x13, 0xFF, 0xEB, 0xDF }, |
| 38 |
[CR_GRAY] = { 0x27, 0x27, 0x27, 0xEF, 0xEF, 0xEF }, |
| 39 |
[CR_GREEN] = { 0x0B, 0x17, 0x07, 0x77, 0xFF, 0x6F }, |
| 40 |
[CR_BROWN] = { 0x53, 0x3F, 0x2F, 0xBF, 0xA7, 0x8F }, |
| 41 |
[CR_GOLD] = { 0x73, 0x2B, 0x00, 0xFF, 0xFF, 0x73 }, |
| 42 |
[CR_RED] = { 0x3F, 0x00, 0x00, 0xFF, 0x00, 0x00 }, |
| 43 |
[CR_BLUE] = { 0x00, 0x00, 0x27, 0x00, 0x00, 0xFF }, |
| 44 |
[CR_ORANGE] = { 0x20, 0x00, 0x00, 0xFF, 0x80, 0x00 }, |
| 45 |
[CR_YELLOW] = { 0x77, 0x77, 0x00, 0xFF, 0xFF, 0x00 }, |
| 46 |
[CR_LIGHTBLUE] = { 0x00, 0x00, 0x73, 0xB4, 0xB4, 0xFF }, |
| 47 |
[CR_BLACK] = { 0x13, 0x13, 0x13, 0x50, 0x50, 0x50 }, |
| 48 |
[CR_PURPLE] = { 0x23, 0x00, 0x23, 0xCF, 0x00, 0xCF }, |
| 49 |
[CR_WHITE] = { 0x24, 0x24, 0x24, 0xFF, 0xFF, 0xFF }, |
| 50 |
}; |
| 51 |
|
| 52 |
static char ref_lump_doom[9] = "STCFN065"; |
| 53 |
static char ref_lump_heretic[9] = "FONTA33"; |
| 54 |
static char ref_lump_hexen[9] = "FONTA33"; |
| 55 |
|
| 56 |
typedef struct { |
| 57 |
double light_lower_bound; |
| 58 |
double light_upper_bound; |
| 59 |
double multiplier; |
| 60 |
} cr_font_t; |
| 61 |
|
| 62 |
static cr_font_t cr_font = { |
| 63 |
.light_lower_bound = 1.0, |
| 64 |
.light_upper_bound = 0.0, |
| 65 |
.multiplier = 1.0, |
| 66 |
}; |
| 67 |
|
| 68 |
static void dsda_RegisterFontLightness(double lightness) { |
| 69 |
if (cr_font.light_lower_bound > lightness) |
| 70 |
cr_font.light_lower_bound = lightness; |
| 71 |
|
| 72 |
if (cr_font.light_upper_bound < lightness) |
| 73 |
cr_font.light_upper_bound = lightness; |
| 74 |
} |
| 75 |
|
| 76 |
static void dsda_CalculateFontBounds(const byte* playpal) { |
| 77 |
int i, j; |
| 78 |
const byte* lump; |
| 79 |
const byte* p; |
| 80 |
short width; |
| 81 |
byte length; |
| 82 |
byte entry; |
| 83 |
double lightness; |
| 84 |
|
| 85 |
if (heretic) |
| 86 |
lump = W_LumpByName(ref_lump_heretic); |
| 87 |
else if (hexen) |
| 88 |
lump = W_LumpByName(ref_lump_hexen); |
| 89 |
else |
| 90 |
lump = W_LumpByName(ref_lump_doom); |
| 91 |
|
| 92 |
width = *((const int16_t *) lump); |
| 93 |
width = LittleShort(width); |
| 94 |
|
| 95 |
for (i = 0; i < width; ++i) { |
| 96 |
int32_t offset; |
| 97 |
p = lump + 8 + 4 * i; |
| 98 |
offset = *((const int32_t *) p); |
| 99 |
p = lump + LittleLong(offset); |
| 100 |
|
| 101 |
while (*p != 0xff) { |
| 102 |
p++; |
| 103 |
length = *p++; |
| 104 |
p++; |
| 105 |
|
| 106 |
for (j = 0; j < length; ++j) { |
| 107 |
entry = *p++; |
| 108 |
lightness = dsda_PaletteEntryLightness(playpal, entry) / 100.0; |
| 109 |
dsda_RegisterFontLightness(lightness); |
| 110 |
} |
| 111 |
|
| 112 |
p++; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
cr_font.multiplier = 1.0 / (cr_font.light_upper_bound - cr_font.light_lower_bound); |
| 117 |
|
| 118 |
lprintf(LO_DEBUG, "Font Bounds: %lf:%lf x%lf\n", |
| 119 |
cr_font.light_lower_bound, cr_font.light_upper_bound, cr_font.multiplier); |
| 120 |
} |
| 121 |
|
| 122 |
static void dsda_LoadCRLump(void) { |
| 123 |
char* lump; |
| 124 |
char** lines; |
| 125 |
const char* line; |
| 126 |
int line_i; |
| 127 |
int i, r1, g1, b1, r2, g2, b2; |
| 128 |
|
| 129 |
lump = W_ReadLumpToString(W_GetNumForName("DSDACR")); |
| 130 |
|
| 131 |
lines = dsda_SplitString(lump, "\n\r"); |
| 132 |
|
| 133 |
if (lines[0]) |
| 134 |
sscanf(lines[0], "%8s %8s %8s", ref_lump_doom, ref_lump_heretic, ref_lump_hexen); |
| 135 |
|
| 136 |
for (line_i = 1; lines[line_i]; ++line_i) { |
| 137 |
line = lines[line_i]; |
| 138 |
|
| 139 |
if (sscanf(line, "%d %i %i %i %i %i %i", &i, &r1, &g1, &b1, &r2, &g2, &b2) != 7) |
| 140 |
I_Error("DSDACR lump has unknown format!"); |
| 141 |
|
| 142 |
if (i < 1 || i >= CR_HUD_LIMIT) |
| 143 |
I_Error("DSDACR index %d is out of bounds!", i); |
| 144 |
|
| 145 |
if (r1 < 0 || g1 < 0 || b1 < 0 || r2 < 0 || g2 < 0 || b2 < 0 || |
| 146 |
r1 > 255 || g1 > 255 || b1 > 255 || r2 > 255 || g2 > 255 || b2 > 255) |
| 147 |
I_Error("DSDACR index %d has color out of range (0-255)", i); |
| 148 |
|
| 149 |
cr_range[i].r1 = r1; |
| 150 |
cr_range[i].g1 = g1; |
| 151 |
cr_range[i].b1 = b1; |
| 152 |
cr_range[i].r2 = r2; |
| 153 |
cr_range[i].g2 = g2; |
| 154 |
cr_range[i].b2 = b2; |
| 155 |
} |
| 156 |
|
| 157 |
Z_Free(lines); |
| 158 |
Z_Free(lump); |
| 159 |
} |
| 160 |
|
| 161 |
typedef struct { |
| 162 |
const char* name; |
| 163 |
int fallback; |
| 164 |
} blood_load_t; |
| 165 |
|
| 166 |
static blood_load_t blood_data[CR_LIMIT - CR_BLOOD] = { |
| 167 |
{ "CRGRAY", CR_GRAY }, |
| 168 |
{ "CRGREEN", CR_GREEN }, |
| 169 |
{ "CRBLUE2", CR_BLUE }, |
| 170 |
{ "CRYELLOW", CR_YELLOW }, |
| 171 |
{ "CRBLACK", CR_BLACK }, |
| 172 |
{ "CRPURPLE", CR_PURPLE }, |
| 173 |
{ "CRWHITE", CR_WHITE }, |
| 174 |
{ "CRORANGE", CR_ORANGE }, |
| 175 |
}; |
| 176 |
|
| 177 |
static void dsda_LoadCRLumps(byte* buffer) { |
| 178 |
int i; |
| 179 |
byte* blood_buffer; |
| 180 |
|
| 181 |
blood_buffer = buffer + CR_BLOOD * 256; |
| 182 |
|
| 183 |
for (i = 0; i < CR_LIMIT - CR_BLOOD; ++i) { |
| 184 |
int lump; |
| 185 |
|
| 186 |
lump = W_CheckNumForName(blood_data[i].name); |
| 187 |
if (lump != LUMP_NOT_FOUND && W_LumpLength(lump) == 256) |
| 188 |
memcpy(blood_buffer + i * 256, W_LumpByNum(lump), 256); |
| 189 |
else |
| 190 |
memcpy(blood_buffer + i * 256, buffer + blood_data[i].fallback * 256, 256); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
byte* dsda_GenerateCRTable(void) { |
| 195 |
int cr_i; |
| 196 |
int orig_i; |
| 197 |
int check_i; |
| 198 |
byte* buffer; |
| 199 |
const byte* playpal; |
| 200 |
int dark_i; |
| 201 |
|
| 202 |
dsda_LoadCRLump(); |
| 203 |
|
| 204 |
playpal = W_LumpByName("PLAYPAL"); |
| 205 |
|
| 206 |
buffer = Z_Malloc(256 * CR_LIMIT); |
| 207 |
|
| 208 |
dsda_CalculateFontBounds(playpal); |
| 209 |
|
| 210 |
for (orig_i = 0; orig_i < 256; ++orig_i) { |
| 211 |
double length; |
| 212 |
|
| 213 |
length = dsda_PaletteEntryLightness(playpal, orig_i) / 100.0; |
| 214 |
length -= cr_font.light_lower_bound; |
| 215 |
length *= cr_font.multiplier; |
| 216 |
if (length < 0) |
| 217 |
length = 0; |
| 218 |
if (length > 1) |
| 219 |
length = 1; |
| 220 |
|
| 221 |
// This is the exhud font bright value |
| 222 |
if (orig_i == 176) |
| 223 |
length = 1; |
| 224 |
|
| 225 |
for (dark_i = 0; dark_i < 2; ++dark_i) { |
| 226 |
for (cr_i = 0; cr_i < CR_DARKEN; ++cr_i) { |
| 227 |
int target_r, target_g, target_b; |
| 228 |
int best_i = 0; |
| 229 |
int best_dist = INT_MAX; |
| 230 |
|
| 231 |
target_r = cr_range[cr_i].r1 + |
| 232 |
(int) (length * (cr_range[cr_i].r2 - cr_range[cr_i].r1)); |
| 233 |
target_g = cr_range[cr_i].g1 + |
| 234 |
(int) (length * (cr_range[cr_i].g2 - cr_range[cr_i].g1)); |
| 235 |
target_b = cr_range[cr_i].b1 + |
| 236 |
(int) (length * (cr_range[cr_i].b2 - cr_range[cr_i].b1)); |
| 237 |
|
| 238 |
if (dark_i) { |
| 239 |
target_r /= 2; |
| 240 |
target_g /= 2; |
| 241 |
target_b /= 2; |
| 242 |
} |
| 243 |
|
| 244 |
for (check_i = 0; check_i < 768; check_i += 3) { |
| 245 |
int dist; |
| 246 |
int dist_r, dist_g, dist_b; |
| 247 |
int avg_r; |
| 248 |
|
| 249 |
avg_r = (target_r + playpal[check_i + 0]) / 2; |
| 250 |
dist_r = target_r - playpal[check_i + 0]; |
| 251 |
dist_g = target_g - playpal[check_i + 1]; |
| 252 |
dist_b = target_b - playpal[check_i + 2]; |
| 253 |
|
| 254 |
// This equation seems to fix issues with red-dominant translation, |
| 255 |
// e.g., aaliens CR_BRICK, which has artifacts in the second equation. |
| 256 |
// |
| 257 |
// I experimented with more "sophisticated" approaches, |
| 258 |
// but they don't seem to do well with common palettes. |
| 259 |
if (target_r > target_g && target_r > target_b) |
| 260 |
dist = (((512 + avg_r) * dist_r * dist_r) >> 8) + |
| 261 |
4 * dist_g * dist_g + |
| 262 |
(((767 - avg_r) * dist_b * dist_b) >> 8); |
| 263 |
else |
| 264 |
dist = dist_r * dist_r + |
| 265 |
dist_g * dist_g + |
| 266 |
dist_b * dist_b; |
| 267 |
|
| 268 |
if (dist < best_dist) { |
| 269 |
best_dist = dist; |
| 270 |
best_i = check_i / 3; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
buffer[(dark_i ? CR_DARKEN * 256 : 0) + cr_i * 256 + orig_i] = best_i; |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
dsda_LoadCRLumps(buffer); |
| 280 |
|
| 281 |
return buffer; |
| 282 |
} |