Skip to content

Commit

Permalink
Improve graphlib accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbatalov committed Dec 27, 2022
1 parent 1f6d5eb commit 4b137da
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 104 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/character_editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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;
}
Expand Down
29 changes: 0 additions & 29 deletions src/game_palette.cc

This file was deleted.

10 changes: 0 additions & 10 deletions src/game_palette.h

This file was deleted.

51 changes: 51 additions & 0 deletions src/graph_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <string.h>

#include <algorithm>

#include "color.h"
#include "debug.h"
#include "memory.h"

Expand All @@ -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;

Expand All @@ -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)
{
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/graph_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 0 additions & 46 deletions src/grayscale.cc

This file was deleted.

11 changes: 0 additions & 11 deletions src/grayscale.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4b137da

Please sign in to comment.