Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bloated PNG output #2849

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/common/textures/m_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
// MACROS ------------------------------------------------------------------

// The maximum size of an IDAT chunk ZDoom will write. This is also the
// size of the compression buffer it allocates on the stack.
// size of the compression buffer it allocates on the heap.
#define PNG_WRITE_SIZE 32768

// Set this to 1 to use a simple heuristic to select the filter to apply
Expand Down Expand Up @@ -926,8 +926,7 @@ bool M_SaveBitmap(const uint8_t *from, ESSType color_type, int width, int height
temprow[i] = &temprow_storage[temprow_size * i];
}

TArray<Byte> array(PNG_WRITE_SIZE, true);
auto buffer = array.data();
TArray<Byte> buffer(PNG_WRITE_SIZE, true);
z_stream stream;
int err;
int y;
Expand All @@ -944,8 +943,8 @@ bool M_SaveBitmap(const uint8_t *from, ESSType color_type, int width, int height
}

y = height;
stream.next_out = buffer;
stream.avail_out = sizeof(buffer);
stream.next_out = buffer.data();
stream.avail_out = buffer.size();

temprow[0][0] = 0;
#if USE_FILTER_HEURISTIC
Expand Down Expand Up @@ -1007,12 +1006,12 @@ bool M_SaveBitmap(const uint8_t *from, ESSType color_type, int width, int height
}
while (stream.avail_out == 0)
{
if (!WriteIDAT (file, buffer, sizeof(buffer)))
if (!WriteIDAT (file, buffer.data(), buffer.size()))
{
return false;
}
stream.next_out = buffer;
stream.avail_out = sizeof(buffer);
stream.next_out = buffer.data();
stream.avail_out = buffer.size();
if (stream.avail_in != 0)
{
err = deflate (&stream, (y == 0) ? Z_FINISH : 0);
Expand All @@ -1033,12 +1032,12 @@ bool M_SaveBitmap(const uint8_t *from, ESSType color_type, int width, int height
}
if (stream.avail_out == 0)
{
if (!WriteIDAT (file, buffer, sizeof(buffer)))
if (!WriteIDAT (file, buffer.data(), buffer.size()))
{
return false;
}
stream.next_out = buffer;
stream.avail_out = sizeof(buffer);
stream.next_out = buffer.data();
stream.avail_out = buffer.size();
}
}

Expand All @@ -1048,7 +1047,7 @@ bool M_SaveBitmap(const uint8_t *from, ESSType color_type, int width, int height
{
return false;
}
return WriteIDAT (file, buffer, sizeof(buffer)-stream.avail_out);
return WriteIDAT (file, buffer.data(), buffer.size() - stream.avail_out);
}

//==========================================================================
Expand Down