//
// png_set_rgb_to_gray_fixed from 0xbd87d0 to 0xbd891c (332 bytes)
//
// 4 xrefs:
// > sub_BD1BEC @ 0xbd227c
// > png_read_row @ 0xbd3d7c
// > png_set_rgb_to_gray @ 0xbd8978
// > ??> @ 0x82578
//
// This function is hookable on this platform!
//
// Using BromaIDA 8.0.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit ba9f177b at Thu Jul 16 22:52:06 2026 from https://github.com/geode-sdk/bindings
//
__int64 __fastcall png_set_rgb_to_gray_fixed(__int64 result, int a2, int a3, int a4)
{
__int64 v4; // x4
int v5; // w5
__int64 v6; // x19
int v7; // w0
const char *v8; // x1
v4 = result;
if ( !result )
return result;
v5 = *(_DWORD *)(result + 360);
if ( (v5 & 0x40) != 0 )
{
v8 = "invalid after png_start_read_image or png_read_update_info";
return png_app_error(result, v8);
}
if ( (*(_DWORD *)(result + 356) & 1) == 0 )
{
v8 = "invalid before the PNG header has been read";
return png_app_error(result, v8);
}
*(_DWORD *)(result + 360) = v5 | 0x4000;
v6 = result;
switch ( a2 )
{
case 2:
v7 = *(_DWORD *)(result + 364) | 0x400000;
*(_DWORD *)(v4 + 364) = v7;
break;
case 3:
v7 = *(_DWORD *)(result + 364) | 0x200000;
*(_DWORD *)(v4 + 364) = v7;
break;
case 1:
v7 = *(_DWORD *)(result + 364) | 0x600000;
*(_DWORD *)(v4 + 364) = v7;
break;
default:
png_error(result, "invalid error action to rgb_to_gray");
}
if ( *(_BYTE *)(v6 + 687) == 3 )
*(_DWORD *)(v6 + 364) = v7 | 0x1000;
if ( (a3 & 0x80000000) == 0 && (a4 & 0x80000000) == 0 )
{
if ( a3 + a4 <= 100000 )
{
*(_WORD *)(v6 + 1100) = (175921861 * (unsigned __int64)((a4 & 0x1FFFFu) << 10)) >> 39;
*(_WORD *)(v6 + 1098) = (175921861 * (unsigned __int64)((a3 & 0x1FFFFu) << 10)) >> 39;
*(_BYTE *)(v6 + 1097) = 1;
return 1;
}
png_app_warning(v6, "ignoring out of range rgb_to_gray coefficients");
}
result = *(_QWORD *)(v6 + 1096);
if ( (result & 0xFFFFFFFF0000LL) == 0 )
{
*(_WORD *)(v6 + 1098) = 6968;
result = 23434;
*(_WORD *)(v6 + 1100) = 23434;
}
return result;
}