//
// cocos2d::CCImage::_saveImageToPNG from 0x5e2030 to 0x5e226e (574 bytes)
//
// 1 xrefs:
// > cocos2d::CCImage::saveToFile @ 0x5e2456
//
// 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
//
bool cocos2d::CCImage::_saveImageToPNG(cocos2d::CCImage *a1, const char *a2, bool a3)
{
int *p_info_struct; // r1
struct __jmp_buf_tag *v4; // r0
int v5; // r5
int m_nHeight; // r6
_DWORD *v7; // r0
_DWORD *v8; // r4
int m_bHasAlpha; // r3
char *v10; // r0
int v11; // r2
void *v12; // r6
int v13; // r1
int i; // r1
int m_nWidth; // r3
int v16; // r3
int v17; // r3
int j; // r2
int k; // r3
FILE *stream; // [sp+20h] [bp-24h]
_BOOL4 v23; // [sp+24h] [bp-20h]
int v24; // [sp+34h] [bp-10h] BYREF
int info_struct; // [sp+38h] [bp-Ch] BYREF
v23 = a3;
if ( !a2 )
return 0;
stream = fopen(a2, "wb");
if ( !stream )
return 0;
v24 = png_create_write_struct("1.6.16", 0, 0, 0);
if ( v24 )
{
info_struct = png_create_info_struct();
if ( !info_struct )
{
fclose(stream);
p_info_struct = nullptr;
LABEL_15:
png_destroy_write_struct(&v24, p_info_struct);
return 0;
}
v4 = (struct __jmp_buf_tag *)png_set_longjmp_fn(v24, &longjmp, 256);
if ( setjmp(v4) )
goto LABEL_14;
png_init_io(v24, stream);
if ( v23 || !a1->m_bHasAlpha )
png_set_IHDR(v24, info_struct, a1->m_nWidth, a1->m_nHeight, 8, 2, 0, 0, 0);
else
png_set_IHDR(v24, info_struct, a1->m_nWidth, a1->m_nHeight, 8, 6, 0, 0, 0);
v5 = png_malloc(v24, 768);
png_set_PLTE(v24, info_struct, v5, 256);
png_write_info(v24, info_struct);
png_set_packing(v24);
m_nHeight = a1->m_nHeight;
v7 = malloc(4 * m_nHeight);
v8 = v7;
if ( !v7 )
{
LABEL_14:
fclose(stream);
p_info_struct = &info_struct;
goto LABEL_15;
}
m_bHasAlpha = a1->m_bHasAlpha;
if ( a1->m_bHasAlpha )
{
if ( v23 )
{
v10 = (char *)operator new[](3 * a1->m_nWidth * m_nHeight);
v11 = 0;
v12 = v10;
while ( 1 )
{
v13 = a1->m_nHeight;
if ( v11 >= v13 )
break;
for ( i = 0; ; ++i )
{
m_nWidth = a1->m_nWidth;
if ( i >= m_nWidth )
break;
v10[3 * i + 3 * m_nWidth * v11] = a1->m_pData[4 * i + 4 * m_nWidth * v11];
v16 = i + a1->m_nWidth * v11;
v10[3 * v16 + 1] = a1->m_pData[4 * v16 + 1];
v17 = i + a1->m_nWidth * v11;
v10[3 * v17 + 2] = a1->m_pData[4 * v17 + 2];
}
++v11;
}
for ( j = 0; j != v13; ++j )
v8[j] = &v10[3 * a1->m_nWidth * j];
png_write_image(v24, v8);
free(v8);
operator delete[](v12);
goto LABEL_34;
}
for ( k = 0; k < m_nHeight; ++k )
v7[k] = &a1->m_pData[4 * a1->m_nWidth * k];
}
else
{
while ( m_bHasAlpha < m_nHeight )
{
v7[m_bHasAlpha] = &a1->m_pData[3 * a1->m_nWidth * m_bHasAlpha];
++m_bHasAlpha;
}
}
png_write_image(v24, v7);
free(v8);
LABEL_34:
png_write_end(v24);
png_free(v24, v5);
png_destroy_write_struct(&v24, &info_struct);
fclose(stream);
return 1;
}
fclose(stream);
return 0;
}