diff --git a/src/RageSurface_Save_PNG.cpp b/src/RageSurface_Save_PNG.cpp index 07495560cc..1bf2c0fc0c 100644 --- a/src/RageSurface_Save_PNG.cpp +++ b/src/RageSurface_Save_PNG.cpp @@ -15,11 +15,17 @@ bool RageSurfaceUtils::SavePNG(RageSurface* pImg, RageFile& f, RString& sError) f.Close(); // The RageFile reference is already opened. Should be closed for following function to succeed. RageSurface* res; - RageSurfaceUtils::ConvertSurface(pImg, res, pImg->w, pImg->h, 32, + bool converted = RageSurfaceUtils::ConvertSurface(pImg, res, pImg->w, pImg->h, 32, Swap32BE(0xFF000000), Swap32BE(0x00FF0000), Swap32BE(0x0000FF00), Swap32BE(0x000000FF)); - return 0 != stbi_write_png(f.GetRealPath(), pImg->w, pImg->h, 4, res->pixels, pImg->w * 4); + if (!converted) res = pImg; + + // stride_in_bytes is image width in bytes + bool success = 0 != stbi_write_png(f.GetRealPath(), res->w, res->h, 4, res->pixels, res->w * 4); + if (converted) delete res; // If we converted then we created a new surface which we need to delete + + return success; }