| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Graphical routines for setting the palette |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <string.h> |
| 30 |
|
| 31 |
#include "physfsx.h" |
| 32 |
#include "pstypes.h" |
| 33 |
#include "u_mem.h" |
| 34 |
#include "gr.h" |
| 35 |
#include "dxxerror.h" |
| 36 |
#include "maths.h" |
| 37 |
#include "palette.h" |
| 38 |
|
| 39 |
#include "d_enumerate.h" |
| 40 |
#include "dxxsconf.h" |
| 41 |
#include "dsx-ns.h" |
| 42 |
#include <array> |
| 43 |
|
| 44 |
namespace dcx { |
| 45 |
|
| 46 |
namespace { |
| 47 |
|
| 48 |
unsigned get_squared_color_delta(const int r, const int g, const int b, const rgb_t &rgb) |
| 49 |
{ |
| 50 |
const auto dr = r - rgb.r; |
| 51 |
const auto dg = g - rgb.g; |
| 52 |
const auto db = b - rgb.b; |
| 53 |
return (dr * dr) + (dg * dg) + (db * db); |
| 54 |
} |
| 55 |
|
| 56 |
color_palette_index gr_find_closest_color_palette(const int r, const int g, const int b, const palette_array_t &palette) |
| 57 |
{ |
| 58 |
// Anything is better than this. |
| 59 |
unsigned best_value = UINT_MAX; |
| 60 |
color_palette_index best_index{}; |
| 61 |
// only go to 255, 'cause we dont want to check the transparent color. |
| 62 |
/* Do not use `palette.size() - 1` here. That only works in compilers |
| 63 |
* which implement P2280R4, due to use of a reference parameter in a |
| 64 |
* constant expression, even though the value referenced has no effect on |
| 65 |
* the result of `size()`. |
| 66 |
* |
| 67 |
* <gcc-14 rejects it. |
| 68 |
* >=gcc-14 accept it. |
| 69 |
* <clang-16: untested |
| 70 |
* clang-16: fails |
| 71 |
* clang-17: fails |
| 72 |
* >clang-17: untested |
| 73 |
*/ |
| 74 |
for (auto &&[candidate_idx, candidate_rgb] : enumerate(std::span(palette).first<255>())) |
| 75 |
{ |
| 76 |
const auto candidate_value = get_squared_color_delta(r, g, b, candidate_rgb); |
| 77 |
if (best_value > candidate_value) |
| 78 |
{ |
| 79 |
best_index = candidate_idx; |
| 80 |
if (candidate_value == 0) |
| 81 |
/* Perfect match */ |
| 82 |
break; |
| 83 |
best_value = candidate_value; |
| 84 |
} |
| 85 |
} |
| 86 |
return best_index; |
| 87 |
} |
| 88 |
|
| 89 |
static unsigned Num_computed_colors; |
| 90 |
|
| 91 |
void gr_read_palette_file_bytes(RAIIPHYSFS_File fp) |
| 92 |
{ |
| 93 |
if (const auto fsize{PHYSFS_fileLength(fp)}; unlikely(fsize != 9472)) |
| 94 |
{ |
| 95 |
/* This can only happen if an ill-formed data file is provided. */ |
| 96 |
gr_palette = {}; |
| 97 |
gr_fade_table = {}; |
| 98 |
return; |
| 99 |
} |
| 100 |
PHYSFSX_readBytes(fp, gr_palette, sizeof(gr_palette[0]) * gr_palette.size()); |
| 101 |
PHYSFSX_readBytes(fp, gr_fade_table, 256*34); |
| 102 |
} |
| 103 |
|
| 104 |
void gr_read_palette_file(RAIIPHYSFS_File fp) |
| 105 |
{ |
| 106 |
Num_computed_colors = 0; // Flush palette cache. |
| 107 |
gr_read_palette_file_bytes(std::move(fp)); |
| 108 |
|
| 109 |
// This is the TRANSPARENCY COLOR |
| 110 |
for (auto &i : gr_fade_table) |
| 111 |
i[255] = 255; |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
namespace dsx { |
| 119 |
|
| 120 |
namespace { |
| 121 |
|
| 122 |
RAIIPHYSFS_File gr_open_palette_file(const char *const filename) |
| 123 |
{ |
| 124 |
if (auto [fp_requested_palette, physfserr_requested_palette]{PHYSFSX_openReadBuffered(filename)}; fp_requested_palette) |
| 125 |
{ |
| 126 |
/* gcc interprets `return fp_requested_palette;` to move |
| 127 |
* `fp_requested_palette` to the caller. |
| 128 |
* |
| 129 |
* clang interprets `return fp_requested_palette;` to copy |
| 130 |
* `fp_requested_palette` to the caller, then errors out because the |
| 131 |
* copy constructor is inaccessible. |
| 132 |
* |
| 133 |
* Use an explicit `std::move` to cause clang to use the move |
| 134 |
* constructor. From inspection, gcc generates the same code with an |
| 135 |
* explicit `std::move` as with an implicit move, so this is not a |
| 136 |
* pessimizing move. However, for future proofing, only call |
| 137 |
* `std::move` when using clang. |
| 138 |
* |
| 139 |
* clang version results: |
| 140 |
* <clang-16: untested |
| 141 |
* clang-16: fails |
| 142 |
* clang-17: fails |
| 143 |
* >clang-17: untested |
| 144 |
*/ |
| 145 |
return |
| 146 |
#ifdef __clang__ |
| 147 |
std::move |
| 148 |
#endif |
| 149 |
(fp_requested_palette); |
| 150 |
} |
| 151 |
else |
| 152 |
{ |
| 153 |
#if DXX_BUILD_DESCENT == 1 |
| 154 |
Error("Failed to open palette file <%s>: %s", filename, PHYSFS_getErrorByCode(physfserr_requested_palette)); |
| 155 |
#elif DXX_BUILD_DESCENT == 2 |
| 156 |
// the following is a hack to enable the loading of d2 levels |
| 157 |
// even if only the d2 mac shareware datafiles are present. |
| 158 |
// However, if the pig file is present but the palette file isn't, |
| 159 |
// the textures in the level will look wierd... |
| 160 |
static char default_level_palette[]{DEFAULT_LEVEL_PALETTE}; |
| 161 |
if (auto [fp_fallback_default_palette, physfserr_fallback_default_palette]{PHYSFSX_openReadBuffered_updateCase(default_level_palette)}; fp_fallback_default_palette) |
| 162 |
{ |
| 163 |
return |
| 164 |
#ifdef __clang__ |
| 165 |
std::move |
| 166 |
#endif |
| 167 |
(fp_fallback_default_palette); |
| 168 |
} |
| 169 |
else |
| 170 |
Error("Failed to open both palette file <%s> and default palette file <" DEFAULT_LEVEL_PALETTE ">: (\"%s\", \"%s\")", filename, PHYSFS_getErrorByCode(physfserr_requested_palette), PHYSFS_getErrorByCode(physfserr_fallback_default_palette)); |
| 171 |
#endif |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
#if DXX_BUILD_DESCENT == 2 |
| 178 |
void gr_copy_palette(palette_array_t &gr_palette, const palette_array_t &pal) |
| 179 |
{ |
| 180 |
gr_palette = pal; |
| 181 |
|
| 182 |
Num_computed_colors = 0; |
| 183 |
} |
| 184 |
#endif |
| 185 |
|
| 186 |
void gr_use_palette_table(const char * filename ) |
| 187 |
{ |
| 188 |
gr_read_palette_file(gr_open_palette_file(filename)); |
| 189 |
} |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
namespace dcx { |
| 194 |
|
| 195 |
void reset_computed_colors() |
| 196 |
{ |
| 197 |
Num_computed_colors = 0; |
| 198 |
} |
| 199 |
|
| 200 |
color_palette_index gr_find_closest_color(const int r, const int g, const int b) |
| 201 |
{ |
| 202 |
struct color_record { |
| 203 |
uint8_t r, g, b; |
| 204 |
color_palette_index color_num; |
| 205 |
}; |
| 206 |
static std::array<color_record, 32> Computed_colors; |
| 207 |
const auto num_computed_colors{Num_computed_colors}; |
| 208 |
// If we've already computed this color, return it! |
| 209 |
for (auto &&[i, c] : enumerate(std::span(Computed_colors).first(num_computed_colors))) |
| 210 |
{ |
| 211 |
if (r == c.r && g == c.g && b == c.b) |
| 212 |
{ |
| 213 |
const auto color_num{c.color_num}; |
| 214 |
if (i) |
| 215 |
/* Move the color toward the beginning of the range, so that |
| 216 |
* recently popular colors require fewer steps to find on a |
| 217 |
* subsequent call. |
| 218 |
*/ |
| 219 |
std::swap(Computed_colors[i - 1], c); |
| 220 |
return color_num; |
| 221 |
} |
| 222 |
} |
| 223 |
// Add a computed color to list of computed colors in Computed_colors. |
| 224 |
// If list wasn't full already, increment Num_computed_colors. |
| 225 |
// If was full, replace the last one. Rely on the bubble-up logic |
| 226 |
// above to move popular entries away from the end. |
| 227 |
auto &cc = (num_computed_colors < Computed_colors.size()) |
| 228 |
? Computed_colors[Num_computed_colors++] |
| 229 |
: Computed_colors.back(); |
| 230 |
cc.r = r; |
| 231 |
cc.g = g; |
| 232 |
cc.b = b; |
| 233 |
return cc.color_num = gr_find_closest_color_palette(r, g, b, gr_palette); |
| 234 |
} |
| 235 |
|
| 236 |
color_palette_index gr_find_closest_color_current(const int r, const int g, const int b) |
| 237 |
{ |
| 238 |
return gr_find_closest_color_palette(r, g, b, gr_current_pal); |
| 239 |
} |
| 240 |
|
| 241 |
} |