From 4b137dac5f9168d7b015e1fcdb972265960beb70 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Tue, 27 Dec 2022 18:59:24 +0300 Subject: [PATCH] Improve graphlib accuracy --- CMakeLists.txt | 4 ---- src/character_editor.cc | 6 ++--- src/game_palette.cc | 29 ----------------------- src/game_palette.h | 10 -------- src/graph_lib.cc | 51 +++++++++++++++++++++++++++++++++++++++++ src/graph_lib.h | 3 +++ src/grayscale.cc | 46 ------------------------------------- src/grayscale.h | 11 --------- src/options.cc | 2 +- 9 files changed, 58 insertions(+), 104 deletions(-) delete mode 100644 src/game_palette.cc delete mode 100644 src/game_palette.h delete mode 100644 src/grayscale.cc delete mode 100644 src/grayscale.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 99a878f..ca89118 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,8 +107,6 @@ target_sources(${EXECUTABLE_NAME} PUBLIC "src/game_mouse.h" "src/game_movie.cc" "src/game_movie.h" - "src/game_palette.cc" - "src/game_palette.h" "src/game_sound.cc" "src/game_sound.h" "src/game_vars.h" @@ -118,8 +116,6 @@ target_sources(${EXECUTABLE_NAME} PUBLIC "src/geometry.h" "src/graph_lib.cc" "src/graph_lib.h" - "src/grayscale.cc" - "src/grayscale.h" "src/heap.cc" "src/heap.h" "src/input.cc" diff --git a/src/character_editor.cc b/src/character_editor.cc index 2b9cacf..a5c31de 100644 --- a/src/character_editor.cc +++ b/src/character_editor.cc @@ -18,9 +18,9 @@ #include "draw.h" #include "game.h" #include "game_mouse.h" -#include "game_palette.h" #include "game_sound.h" #include "geometry.h" +#include "graph_lib.h" #include "input.h" #include "interface.h" #include "item.h" @@ -4961,7 +4961,7 @@ static int characterEditorDrawCardWithOptions(int graphicId, const char* name, c ptr = frmImage.getData(); for (y = 0; y < frmImage.getHeight(); y++) { for (x = 0; x < frmImage.getWidth(); x++) { - if (_HighRGB_(*ptr) < 2 && v9 >= x) { + if (HighRGB(*ptr) < 2 && v9 >= x) { v9 = x; } ptr++; @@ -6659,7 +6659,7 @@ static int perkDialogDrawCard(int frmId, const char* name, const char* rank, cha for (int y = 0; y < frmImage.getHeight(); y++) { unsigned char* stride = data; for (int x = 0; x < frmImage.getWidth(); x++) { - if (_HighRGB_(*stride) < 2) { + if (HighRGB(*stride) < 2) { if (extraDescriptionWidth > x) { extraDescriptionWidth = x; } diff --git a/src/game_palette.cc b/src/game_palette.cc deleted file mode 100644 index 16b33f8..0000000 --- a/src/game_palette.cc +++ /dev/null @@ -1,29 +0,0 @@ -#include "game_palette.h" - -#include "color.h" - -namespace fallout { - -// 0x44EBC0 -int _HighRGB_(int a1) -{ - // TODO: Some strange bit arithmetic. - int v1 = Color2RGB(a1); - int r = (v1 & 0x7C00) >> 10; - int g = (v1 & 0x3E0) >> 5; - int b = (v1 & 0x1F); - - int result = g; - if (r > result) { - result = r; - } - - result = result & 0xFF; - if (result <= b) { - result = b; - } - - return result; -} - -} // namespace fallout diff --git a/src/game_palette.h b/src/game_palette.h deleted file mode 100644 index cd809b2..0000000 --- a/src/game_palette.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef GAME_PALETTE_H -#define GAME_PALETTE_H - -namespace fallout { - -int _HighRGB_(int a1); - -} // namespace fallout - -#endif /* GAME_PALETTE_H */ diff --git a/src/graph_lib.cc b/src/graph_lib.cc index 8f3adaf..a34a9b8 100644 --- a/src/graph_lib.cc +++ b/src/graph_lib.cc @@ -2,6 +2,9 @@ #include +#include + +#include "color.h" #include "debug.h" #include "memory.h" @@ -11,6 +14,9 @@ static void _InitTree(); static void _InsertNode(int a1); static void _DeleteNode(int a1); +// 0x596D90 +static unsigned char _GreyTable[256]; + // 0x596E90 static int* _dad_2; @@ -35,6 +41,17 @@ static int _codesize; // 0x596EAC static int _match_position; +// 0x44EBC0 +unsigned char HighRGB(unsigned char color) +{ + int rgb = Color2RGB(color); + int r = (rgb & 0x7C00) >> 10; + int g = (rgb & 0x3E0) >> 5; + int b = (rgb & 0x1F); + + return std::max(std::max(r, g), b); +} + // 0x44F250 int graphCompress(unsigned char* a1, unsigned char* a2, int a3) { @@ -385,4 +402,38 @@ int graphDecompress(unsigned char* src, unsigned char* dest, int length) return 0; } +// 0x44FA78 +void grayscalePaletteUpdate(int a1, int a2) +{ + if (a1 >= 0 && a2 <= 255) { + for (int index = a1; index <= a2; index++) { + // NOTE: The only way to explain so much calls to `Color2RGB` with + // the same repeated pattern is by the use of min/max macros. + + int v1 = std::max((Color2RGB(index) & 0x7C00) >> 10, std::max((Color2RGB(index) & 0x3E0) >> 5, Color2RGB(index) & 0x1F)); + int v2 = std::min((Color2RGB(index) & 0x7C00) >> 10, std::min((Color2RGB(index) & 0x3E0) >> 5, Color2RGB(index) & 0x1F)); + int v3 = v1 + v2; + int v4 = (int)((double)v3 * 240.0 / 510.0); + + int paletteIndex = ((v4 & 0xFF) << 10) | ((v4 & 0xFF) << 5) | (v4 & 0xFF); + _GreyTable[index] = _colorTable[paletteIndex]; + } + } +} + +// 0x44FC40 +void grayscalePaletteApply(unsigned char* buffer, int width, int height, int pitch) +{ + unsigned char* ptr = buffer; + int skip = pitch - width; + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + unsigned char c = *ptr; + *ptr++ = _GreyTable[c]; + } + ptr += skip; + } +} + } // namespace fallout diff --git a/src/graph_lib.h b/src/graph_lib.h index 487c7c1..4364973 100644 --- a/src/graph_lib.h +++ b/src/graph_lib.h @@ -3,8 +3,11 @@ namespace fallout { +unsigned char HighRGB(unsigned char color); int graphCompress(unsigned char* a1, unsigned char* a2, int a3); int graphDecompress(unsigned char* a1, unsigned char* a2, int a3); +void grayscalePaletteUpdate(int a1, int a2); +void grayscalePaletteApply(unsigned char* surface, int width, int height, int pitch); } // namespace fallout diff --git a/src/grayscale.cc b/src/grayscale.cc deleted file mode 100644 index 0a7aa6b..0000000 --- a/src/grayscale.cc +++ /dev/null @@ -1,46 +0,0 @@ -#include "grayscale.h" - -#include - -#include "color.h" - -namespace fallout { - -// 0x596D90 -static unsigned char _GreyTable[256]; - -// 0x44FA78 -void grayscalePaletteUpdate(int a1, int a2) -{ - if (a1 >= 0 && a2 <= 255) { - for (int index = a1; index <= a2; index++) { - // NOTE: The only way to explain so much calls to `Color2RGB` with - // the same repeated pattern is by the use of min/max macros. - - int v1 = std::max((Color2RGB(index) & 0x7C00) >> 10, std::max((Color2RGB(index) & 0x3E0) >> 5, Color2RGB(index) & 0x1F)); - int v2 = std::min((Color2RGB(index) & 0x7C00) >> 10, std::min((Color2RGB(index) & 0x3E0) >> 5, Color2RGB(index) & 0x1F)); - int v3 = v1 + v2; - int v4 = (int)((double)v3 * 240.0 / 510.0); - - int paletteIndex = ((v4 & 0xFF) << 10) | ((v4 & 0xFF) << 5) | (v4 & 0xFF); - _GreyTable[index] = _colorTable[paletteIndex]; - } - } -} - -// 0x44FC40 -void grayscalePaletteApply(unsigned char* buffer, int width, int height, int pitch) -{ - unsigned char* ptr = buffer; - int skip = pitch - width; - - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - unsigned char c = *ptr; - *ptr++ = _GreyTable[c]; - } - ptr += skip; - } -} - -} // namespace fallout diff --git a/src/grayscale.h b/src/grayscale.h deleted file mode 100644 index 9492cc3..0000000 --- a/src/grayscale.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef GRAYSCALE_H -#define GRAYSCALE_H - -namespace fallout { - -void grayscalePaletteUpdate(int a1, int a2); -void grayscalePaletteApply(unsigned char* surface, int width, int height, int pitch); - -} // namespace fallout - -#endif /* GRAYSCALE_H */ diff --git a/src/options.cc b/src/options.cc index e69f922..782fc36 100644 --- a/src/options.cc +++ b/src/options.cc @@ -17,7 +17,7 @@ #include "game_mouse.h" #include "game_sound.h" #include "geometry.h" -#include "grayscale.h" +#include "graph_lib.h" #include "input.h" #include "kb.h" #include "loadsave.h"