//
// png_set_filter from 0x662770 to 0x662934 (452 bytes)
//
// 1 xrefs:
// > sub_662E04 @ 0x663068
//
// 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
//
_DWORD *__fastcall png_set_filter(_DWORD *result, int a2, int a3)
{
int v3; // r4
int v4; // r3
int v5; // r3
int v6; // r3
int v7; // r3
int v8; // r3
int v9; // r2
int v10; // r2
if ( result )
{
v3 = (int)result;
v4 = (result[202] >> 2) & 1;
if ( a2 != 64 )
v4 = 0;
if ( !v4 && a2 )
png_error((int)result, "Unknown custom filter method");
a3 = (unsigned __int8)a3;
switch ( (char)a3 )
{
case 0:
goto LABEL_20;
case 1:
v6 = result[121];
a3 = 16;
*((_BYTE *)result + 534) = 16;
if ( !v6 )
return result;
goto LABEL_9;
case 2:
v7 = result[121];
a3 = 32;
*((_BYTE *)result + 534) = 32;
if ( !v7 )
return result;
if ( !result[123] )
goto LABEL_28;
goto LABEL_14;
case 3:
v8 = result[121];
a3 = 64;
*((_BYTE *)result + 534) = 64;
if ( !v8 )
return result;
goto LABEL_13;
case 4:
a3 = 128;
*((_BYTE *)result + 534) = 0x80;
goto LABEL_21;
case 5:
case 6:
case 7:
result = (_DWORD *)png_app_error((int)result, "Unknown row filter for method 0");
LABEL_20:
a3 = 8;
*(_BYTE *)(v3 + 534) = 8;
LABEL_21:
if ( !*(_DWORD *)(v3 + 484) )
return result;
if ( (a3 & 0x80) != 0 )
goto LABEL_17;
goto LABEL_15;
default:
v5 = result[121];
*((_BYTE *)result + 534) = a3;
if ( !v5 )
return result;
result = (_DWORD *)((unsigned __int8)a3 << 27);
if ( (a3 & 0x10) == 0 )
goto LABEL_10;
LABEL_9:
if ( *(_DWORD *)(v3 + 488) )
{
LABEL_10:
if ( (a3 & 0x20) == 0 )
goto LABEL_12;
}
else
{
result = png_malloc((void *)v3, *(_DWORD *)(v3 + 464) + 1);
a3 = *(unsigned __int8 *)(v3 + 534);
*(_DWORD *)(v3 + 488) = result;
*(_BYTE *)result = 1;
if ( (a3 & 0x20) == 0 )
goto LABEL_12;
}
if ( !*(_DWORD *)(v3 + 492) )
{
LABEL_28:
if ( *(_DWORD *)(v3 + 480) )
{
result = png_malloc((void *)v3, *(_DWORD *)(v3 + 464) + 1);
a3 = *(unsigned __int8 *)(v3 + 534);
*(_DWORD *)(v3 + 492) = result;
*(_BYTE *)result = 2;
}
else
{
result = (_DWORD *)png_warning(v3, "Can't add Up filter after starting");
a3 = *(_BYTE *)(v3 + 534) & 0xDF;
*(_BYTE *)(v3 + 534) = a3;
}
}
LABEL_12:
if ( (a3 & 0x40) != 0 )
{
LABEL_13:
if ( !*(_DWORD *)(v3 + 496) )
{
if ( *(_DWORD *)(v3 + 480) )
{
result = png_malloc((void *)v3, *(_DWORD *)(v3 + 464) + 1);
a3 = *(unsigned __int8 *)(v3 + 534);
*(_DWORD *)(v3 + 496) = result;
*(_BYTE *)result = 3;
}
else
{
result = (_DWORD *)png_warning(v3, "Can't add Average filter after starting");
a3 = *(_BYTE *)(v3 + 534) & 0xBF;
*(_BYTE *)(v3 + 534) = a3;
}
}
}
LABEL_14:
if ( (a3 & 0x80) == 0 )
{
LABEL_15:
if ( a3 )
return result;
goto LABEL_16;
}
LABEL_17:
if ( *(_DWORD *)(v3 + 500) )
return result;
if ( *(_DWORD *)(v3 + 480) )
{
result = png_malloc((void *)v3, *(_DWORD *)(v3 + 464) + 1);
v9 = *(unsigned __int8 *)(v3 + 534);
*(_DWORD *)(v3 + 500) = result;
*(_BYTE *)result = 4;
if ( v9 )
return result;
LABEL_16:
*(_BYTE *)(v3 + 534) = 8;
return result;
}
result = (_DWORD *)png_warning(v3, "Can't add Paeth filter after starting");
v10 = *(_BYTE *)(v3 + 534) & 0x7F;
*(_BYTE *)(v3 + 534) = v10;
if ( !v10 )
goto LABEL_16;
break;
}
}
return result;
}