Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document greyscale math #700

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ glabel D_800F0CD0

.section .text

glabel func_8009195C
glabel exponential
/* 09255C 8009195C 44802800 */ mtc1 $zero, $f5
/* 092560 80091960 44802000 */ mtc1 $zero, $f4
/* 092564 80091964 27BDFFE8 */ addiu $sp, $sp, -0x18
Expand Down Expand Up @@ -70,7 +70,7 @@ glabel func_8009195C
/* 092648 80091A48 46202080 */ add.d $f2, $f4, $f0
/* 09264C 80091A4C 462E1180 */ add.d $f6, $f2, $f14
/* 092650 80091A50 462E1201 */ sub.d $f8, $f2, $f14
/* 092654 80091A54 0C02469B */ jal func_80091A6C
/* 092654 80091A54 0C02469B */ jal pow2
/* 092658 80091A58 46283303 */ div.d $f12, $f6, $f8
/* 09265C 80091A5C 8FBF0014 */ lw $ra, 0x14($sp)
/* 092660 80091A60 27BD0018 */ addiu $sp, $sp, 0x18
Expand Down
57 changes: 30 additions & 27 deletions src/menu_items.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,19 +1099,19 @@ f64 exponent_by_squaring(f64 base, s32 exponent) {
return 1.0 / result;
}

f64 func_800917B0(f64 arg0, f64 arg1) {
f64 pow(f64 arg0, f64 arg1) {
if ((arg1 <= 2147483647.0) && (-2147483647.0 <= arg1)) {
if (arg1 == (s32) arg1) {
return exponent_by_squaring(arg0, arg1);
}
}
if (arg0 > 0.0) {
return func_8009195C(func_8009186C(arg0) * arg1);
return exponential(ln(arg0) * arg1);
}
return 0.0;
}

f64 func_8009186C(f64 arg0) {
f64 ln(f64 arg0) {
s32 var_v0;
s32 sp38;
f64 var_f2;
Expand All @@ -1121,8 +1121,8 @@ f64 func_8009186C(f64 arg0) {
if (arg0 <= 0.0) {
return 0.0;
}
func_80091AC0(arg0 / 1.414213562373095, &sp38);
arg0 /= func_80091A6C(1.0, sp38);
normalize_to_unit_interval(arg0 / 1.414213562373095, &sp38);
arg0 /= pow2(1.0, sp38);
var_v0 = 1;
arg0 = (arg0 - 1.0) / (arg0 + 1.0);
temp_f12 = arg0 * arg0;
Expand All @@ -1139,7 +1139,7 @@ f64 func_8009186C(f64 arg0) {
#ifdef NON_MATCHING
// https://decomp.me/scratch/dXpT5
// Some math reordering issues on the continued fraction line
f64 func_8009195C(f64 arg0) {
f64 exponential(f64 arg0) {
s32 temp_f10;
s32 six = 6;
s32 ten = 10;
Expand All @@ -1162,17 +1162,17 @@ f64 func_8009195C(f64 arg0) {
**/
temp_f2 =
2 + (temp_f2 / (six + (temp_f2 / (ten + (temp_f2 / (fourteen + (temp_f2 / (eighteen + (temp_f2 / 22)))))))));
return func_80091A6C((temp_f2 + arg0) / (temp_f2 - arg0), temp_f10);
return pow2((temp_f2 + arg0) / (temp_f2 - arg0), temp_f10);
}
#else
GLOBAL_ASM("asm/non_matchings/menu_items/func_8009195C.s")
GLOBAL_ASM("asm/non_matchings/menu_items/exponential.s")
#endif

/**
* This function appears to multiply some `value`
* by 2 ^ `exponent`, even if that exponent is negative
**/
f64 func_80091A6C(f64 value, s32 exponent) {
f64 pow2(f64 value, s32 exponent) {
f64 base;

if (exponent >= 0) {
Expand Down Expand Up @@ -1201,7 +1201,7 @@ f64 func_80091A6C(f64 value, s32 exponent) {
* arg2 appears to track the exponent in the power-of-2
* that would undo the changes to arg0
**/
f64 func_80091AC0(f64 arg0, s32* arg2) {
f64 normalize_to_unit_interval(f64 arg0, s32* arg2) {
const f64 const1 = 2.0;
s32 exponent = 0;

Expand Down Expand Up @@ -3602,7 +3602,8 @@ void func_80099958(MenuTexture* addr, s32 arg1, s32 arg2) {
size = ((size / 8) * 8) + 8;
}
dma_copy_mio0_segment(texAddr->textureData, size, gMenuCompressedBuffer);
mio0decode((u8*) gMenuCompressedBuffer, (u8*) D_802BFB80.arraySize4[arg2][arg1 / 2][(arg1 % 2) + 2].pixel_index_array);
mio0decode((u8*) gMenuCompressedBuffer,
(u8*) D_802BFB80.arraySize4[arg2][arg1 / 2][(arg1 % 2) + 2].pixel_index_array);
texAddr++;
}
}
Expand Down Expand Up @@ -3796,8 +3797,9 @@ void func_80099EC4(void) {
osPiStartDma(&sp68, 0, 0, (uintptr_t) &_textures_0aSegmentRomStart[SEGMENT_OFFSET(temp_s2->textureData)],
gMenuCompressedBuffer + 0x1400, var_s0, &gDmaMesgQueue);
}
mio0decode((u8*) gMenuCompressedBuffer,
(u8*) D_802BFB80.arraySize4[var_s1->unk6][var_s1->unk4 / 2][(var_s1->unk4 % 2) + 2].pixel_index_array);
mio0decode(
(u8*) gMenuCompressedBuffer,
(u8*) D_802BFB80.arraySize4[var_s1->unk6][var_s1->unk4 / 2][(var_s1->unk4 % 2) + 2].pixel_index_array);
var_s1->mk64Texture = NULL;
var_s1++;
if (var_s4 != 0)
Expand All @@ -3820,8 +3822,9 @@ void func_80099EC4(void) {
osPiStartDma(&sp68, 0, 0, (uintptr_t) &_textures_0aSegmentRomStart[SEGMENT_OFFSET(temp_s2->textureData)],
gMenuCompressedBuffer, var_s0, &gDmaMesgQueue);
}
mio0decode((u8*) gMenuCompressedBuffer + 0x1400,
(u8*) D_802BFB80.arraySize4[var_s1->unk6][var_s1->unk4 / 2][(var_s1->unk4 % 2) + 2].pixel_index_array);
mio0decode(
(u8*) gMenuCompressedBuffer + 0x1400,
(u8*) D_802BFB80.arraySize4[var_s1->unk6][var_s1->unk4 / 2][(var_s1->unk4 % 2) + 2].pixel_index_array);
var_s1->mk64Texture = NULL;
var_s1++;
if (var_s4 != 0)
Expand Down Expand Up @@ -4151,31 +4154,31 @@ void func_8009AD78(s32 arg0, s32 arg1) {
}

void convert_img_to_greyscale(s32 arg0, u32 arg1) {
u32 var_s0;
u32 i;
s32 red;
s32 green;
s32 blue;
s32 alpha;
u32 temp_t9;
s32 size;
u16* color;
f32 sp48[0x20];
f32 sp48[32];

for (var_s0 = 0; var_s0 < 0x20; var_s0++) {
sp48[var_s0] = func_800917B0(var_s0 * 0.03125, (arg1 * 1.5 * 0.00390625) + 0.25);
for (i = 0; i < 32; i++) {
sp48[i] = pow(i / 32.0, (arg1 * 1.5 / 256.0) + 0.25);
}
color = &gMenuTextureBuffer[sMenuTextureMap[arg0].offset];
size = sMenuTextureMap[arg0 + 1].offset - sMenuTextureMap[arg0].offset;
for (var_s0 = 0; var_s0 < (u32) size; var_s0++) {
for (i = 0; i < (u32) size; i++) {
red = ((*color & 0xF800) >> 0xB) * 0x55;
green = ((*color & 0x7C0) >> 6) * 0x4B;
blue = ((*color & 0x3E) >> 1) * 0x5F;
alpha = *color & 0x1;
temp_t9 = red + green + blue;
temp_t9 >>= 8;
temp_t9 /= 256;
temp_t9 = sp48[temp_t9] * 32.0f;
if (temp_t9 >= 0x20) {
temp_t9 = 0x1F;
if (temp_t9 >= 32) {
temp_t9 = 31;
}
*color++ = (temp_t9 << 1) + (temp_t9 << 6) + (temp_t9 << 0xB) + alpha;
}
Expand All @@ -4200,10 +4203,10 @@ void adjust_img_colour(s32 arg0, s32 arg1, s32 arg2, s32 arg3, s32 arg4) {
blue = ((*color & 0x3E) >> 1) * 0x1D;
alpha = *color & 0x1;
temp_t9 = red + green + blue;
temp_t9 = temp_t9 >> 8;
newred = ((temp_t9 * arg2) >> 8) << 0xB;
newgreen = ((temp_t9 * arg3) >> 8) << 6;
newblue = ((temp_t9 * arg4) >> 8) << 1;
temp_t9 = temp_t9 / 256;
newred = ((temp_t9 * arg2) / 256) << 0xB;
newgreen = ((temp_t9 * arg3) / 256) << 6;
newblue = ((temp_t9 * arg4) / 256) << 1;
*color++ = newred + newgreen + newblue + alpha;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/menu_items.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ enum TEXT_MENU_ID { CONTINUE_GAME, RETRY, COURSE_CHANGE, DRIVER_CHANGE, QUIT_TEX
/* Function Prototypes */

f64 exponent_by_squaring(f64, s32);
f64 func_800917B0(f64, f64);
f64 func_8009186C(f64);
f64 func_8009195C(f64);
f64 func_80091A6C(f64, s32);
f64 func_80091AC0(f64, s32*);
f64 pow(f64, f64);
f64 ln(f64);
f64 exponential(f64);
f64 pow2(f64, s32);
f64 normalize_to_unit_interval(f64, s32*);
void swap_values(s32*, s32*);
s32 func_80091D74(void);
void func_80091EE4(void);
Expand Down