Simplify memory allocation for PNG image data

Allocate one block for the whole image, rather than a block for each row
This commit is contained in:
Paul Cornett
2019-02-02 10:56:33 -08:00
parent 9886c25645
commit 28010b51f0

View File

@@ -110,7 +110,7 @@ struct wxPNGImageData
wxPNGImageData() wxPNGImageData()
{ {
lines = NULL; lines = NULL;
numLines = 0; m_buf = NULL;
info_ptr = (png_infop) NULL; info_ptr = (png_infop) NULL;
png_ptr = (png_structp) NULL; png_ptr = (png_structp) NULL;
ok = false; ok = false;
@@ -122,23 +122,21 @@ struct wxPNGImageData
if ( !lines ) if ( !lines )
return false; return false;
for ( png_uint_32 n = 0; n < height; n++ ) const size_t w = width * size_t(4);
{ m_buf = static_cast<unsigned char*>(malloc(w * height));
lines[n] = (unsigned char *)malloc( (size_t)(width * 4)); if (!m_buf)
if ( lines[n] ) return false;
++numLines;
else lines[0] = m_buf;
return false; for (png_uint_32 i = 1; i < height; i++)
} lines[i] = lines[i - 1] + w;
return true; return true;
} }
~wxPNGImageData() ~wxPNGImageData()
{ {
for ( unsigned int n = 0; n < numLines; n++ ) free(m_buf);
free( lines[n] );
free( lines ); free( lines );
if ( png_ptr ) if ( png_ptr )
@@ -153,7 +151,7 @@ struct wxPNGImageData
void DoLoadPNGFile(wxImage* image, wxPNGInfoStruct& wxinfo); void DoLoadPNGFile(wxImage* image, wxPNGInfoStruct& wxinfo);
unsigned char** lines; unsigned char** lines;
png_uint_32 numLines; unsigned char* m_buf;
png_infop info_ptr; png_infop info_ptr;
png_structp png_ptr; png_structp png_ptr;
bool ok; bool ok;