| 1 |
// |
| 2 |
// Copyright(C) 2020 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 Palette Management |
| 16 |
// |
| 17 |
|
| 18 |
#include <math.h> |
| 19 |
|
| 20 |
#include "r_main.h" |
| 21 |
#include "w_wad.h" |
| 22 |
#include "v_video.h" |
| 23 |
|
| 24 |
#include "palette.h" |
| 25 |
|
| 26 |
static int playpal_index = playpal_default; |
| 27 |
|
| 28 |
static dsda_playpal_t playpal_data[NUMPALETTES] = { |
| 29 |
{ playpal_default, "PLAYPAL" }, |
| 30 |
{ playpal_1, "PLAYPAL1" }, |
| 31 |
{ playpal_2, "PLAYPAL2" }, |
| 32 |
{ playpal_3, "PLAYPAL3" }, |
| 33 |
{ playpal_4, "PLAYPAL4" }, |
| 34 |
{ playpal_5, "PLAYPAL5" }, |
| 35 |
{ playpal_6, "PLAYPAL6" }, |
| 36 |
{ playpal_7, "PLAYPAL7" }, |
| 37 |
{ playpal_8, "PLAYPAL8" }, |
| 38 |
{ playpal_9, "PLAYPAL9" }, |
| 39 |
{ playpal_heretic_e2end, "E2PAL" } |
| 40 |
}; |
| 41 |
|
| 42 |
dsda_playpal_t* dsda_PlayPalData(void) { |
| 43 |
return &playpal_data[playpal_index]; |
| 44 |
} |
| 45 |
|
| 46 |
void dsda_CyclePlayPal(void) { |
| 47 |
int lump_num = -1; |
| 48 |
int cycle_playpal_index; |
| 49 |
|
| 50 |
cycle_playpal_index = playpal_index; |
| 51 |
|
| 52 |
do { |
| 53 |
cycle_playpal_index++; |
| 54 |
|
| 55 |
if (cycle_playpal_index > playpal_9) |
| 56 |
cycle_playpal_index = playpal_default; |
| 57 |
|
| 58 |
// Looped around and found nothing |
| 59 |
if (cycle_playpal_index == playpal_index) |
| 60 |
return; |
| 61 |
|
| 62 |
lump_num = W_CheckNumForName(playpal_data[cycle_playpal_index].lump_name); |
| 63 |
} while (lump_num == LUMP_NOT_FOUND); |
| 64 |
|
| 65 |
V_SetPlayPal(cycle_playpal_index); |
| 66 |
} |
| 67 |
|
| 68 |
void dsda_SetPlayPal(int index) { |
| 69 |
if (index < 0 || index >= NUMPALETTES) |
| 70 |
index = playpal_default; |
| 71 |
|
| 72 |
playpal_index = index; |
| 73 |
} |
| 74 |
|
| 75 |
void dsda_FreePlayPal(void) { |
| 76 |
int playpal_i; |
| 77 |
|
| 78 |
for (playpal_i = 0; playpal_i < NUMPALETTES; ++playpal_i) |
| 79 |
if (playpal_data[playpal_i].lump) { |
| 80 |
Z_Free(playpal_data[playpal_i].lump); |
| 81 |
playpal_data[playpal_i].lump = NULL; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
static dboolean dsda_DuplicatePaletteEntry(const byte *playpal, int i, int j) { |
| 86 |
int colormap_i; |
| 87 |
|
| 88 |
if ( |
| 89 |
playpal[3 * i + 0] != playpal[3 * j + 0] || |
| 90 |
playpal[3 * i + 1] != playpal[3 * j + 1] || |
| 91 |
playpal[3 * i + 2] != playpal[3 * j + 2] |
| 92 |
) |
| 93 |
return false; |
| 94 |
|
| 95 |
for (colormap_i = 0; colormap_i < NUMCOLORMAPS; ++colormap_i) |
| 96 |
if (colormaps[0][colormap_i * 256 + i] != colormaps[0][colormap_i * 256 + j]) |
| 97 |
return false; |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
double dsda_PaletteEntryLightness(const byte *playpal, int i) { |
| 103 |
double L; |
| 104 |
byte pal_r, pal_g, pal_b; |
| 105 |
double r, g, b; |
| 106 |
double y1, y2; |
| 107 |
|
| 108 |
// this function basically does an RGB to L*a*b* (CIELAB) color |
| 109 |
// space conversion, but only for the L* component since we |
| 110 |
// just want the brightness. probably a bit overkill, but meh. |
| 111 |
|
| 112 |
// step 0: get colors from palette -- explicitly get it as a byte |
| 113 |
// (i.e. unsigned) so it doesn't get interpreted as a negative value. |
| 114 |
pal_r = playpal[3 * i + 0]; |
| 115 |
pal_g = playpal[3 * i + 1]; |
| 116 |
pal_b = playpal[3 * i + 2]; |
| 117 |
|
| 118 |
// step 1: RGB to XYZ (...or just Y, I guess :P) |
| 119 |
r = pal_r / 255.0; |
| 120 |
g = pal_g / 255.0; |
| 121 |
b = pal_b / 255.0; |
| 122 |
|
| 123 |
r = r > 0.04045 ? pow((r + 0.055) / 1.055, 2.4) : r / 12.92; |
| 124 |
g = g > 0.04045 ? pow((g + 0.055) / 1.055, 2.4) : g / 12.92; |
| 125 |
b = b > 0.04045 ? pow((b + 0.055) / 1.055, 2.4) : b / 12.92; |
| 126 |
|
| 127 |
r *= 100; |
| 128 |
g *= 100; |
| 129 |
b *= 100; |
| 130 |
|
| 131 |
y1 = 0.2126729 * r + 0.7151522 * g + 0.0721750 * b; |
| 132 |
|
| 133 |
// step 2: XYZ to Lab |
| 134 |
y2 = y1 / 100.0; |
| 135 |
y2 = y2 > 0.008856 ? pow(y2, 1.0 / 3) : 7.787 * y2 + 4.0 / 29; |
| 136 |
|
| 137 |
L = 116 * y2 - 16; |
| 138 |
|
| 139 |
// all done. take the L. |
| 140 |
return L; |
| 141 |
} |
| 142 |
|
| 143 |
// Moved from r_patch.c |
| 144 |
void dsda_InitPlayPal(void) { |
| 145 |
int playpal_i; |
| 146 |
double lightness; |
| 147 |
double darkest, lightest; |
| 148 |
|
| 149 |
for (playpal_i = 0; playpal_i < NUMPALETTES; ++playpal_i) { |
| 150 |
int lump; |
| 151 |
const byte *playpal; |
| 152 |
int i, j, found = 0; |
| 153 |
|
| 154 |
lump = W_CheckNumForName(playpal_data[playpal_i].lump_name); |
| 155 |
if (lump == LUMP_NOT_FOUND) |
| 156 |
continue; |
| 157 |
|
| 158 |
playpal = W_LumpByNum(lump); |
| 159 |
|
| 160 |
if (!playpal_data[playpal_i].duplicate) { |
| 161 |
// find two duplicate palette entries. use one for transparency. |
| 162 |
// rewrite source pixels in patches to the other on composition. |
| 163 |
|
| 164 |
for (i = 0; i < 256; i++) { |
| 165 |
for (j = i + 1; j < 256; j++) { |
| 166 |
if (dsda_DuplicatePaletteEntry(playpal, i, j)) { |
| 167 |
found = 1; |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if (found) |
| 173 |
break; |
| 174 |
} |
| 175 |
|
| 176 |
if (found) { // found duplicate |
| 177 |
playpal_data[playpal_i].transparent = i; |
| 178 |
playpal_data[playpal_i].duplicate = j; |
| 179 |
} |
| 180 |
else { // no duplicate: use 255 for transparency, as done previously |
| 181 |
playpal_data[playpal_i].transparent = 255; |
| 182 |
playpal_data[playpal_i].duplicate = -1; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// find the brightness extremes (0-100) |
| 187 |
darkest = 101.0; |
| 188 |
lightest = -1.0; |
| 189 |
for (i = 0; i < 256; i++) { |
| 190 |
lightness = dsda_PaletteEntryLightness(playpal, i); |
| 191 |
|
| 192 |
if (lightness < darkest) { |
| 193 |
darkest = lightness; |
| 194 |
playpal_data[playpal_i].darkest = i; |
| 195 |
} |
| 196 |
|
| 197 |
if (lightness > lightest) { |
| 198 |
lightest = lightness; |
| 199 |
playpal_data[playpal_i].lightest = i; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |