Skip to content

Commit 4ecc8d3

Browse files
committed
Fix build error by replacing stbi_write_png_to_mem with stbi_write_png_to_func
1 parent a0aa752 commit 4ecc8d3

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/commands/spratunpack_command.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -621,13 +621,16 @@ class SpriteUnpacker {
621621
}
622622

623623
// Encode as PNG in memory
624-
int png_size = 0;
625-
unsigned char* png_buffer_raw = stbi_write_png_to_mem(sprite_data.data(), out_w * NUM_CHANNELS,
626-
out_w, out_h, NUM_CHANNELS, &png_size);
627-
if (png_buffer_raw == nullptr) {
624+
std::vector<unsigned char> png_buffer;
625+
auto write_to_vec = [](void* context, void* data, int size) {
626+
auto* vec = static_cast<std::vector<unsigned char>*>(context);
627+
const auto* bytes = static_cast<const unsigned char*>(data);
628+
vec->insert(vec->end(), bytes, bytes + size);
629+
};
630+
631+
if (!stbi_write_png_to_func(write_to_vec, &png_buffer, out_w, out_h, NUM_CHANNELS, sprite_data.data(), out_w * NUM_CHANNELS)) {
628632
return false;
629633
}
630-
std::unique_ptr<unsigned char, void(*)(void*)> png_buffer(png_buffer_raw, std::free);
631634

632635
std::string filename = frame.name;
633636
if (filename.find('.') == std::string::npos) {
@@ -640,7 +643,7 @@ class SpriteUnpacker {
640643
}
641644

642645
archive_entry_set_pathname(entry, filename.c_str());
643-
archive_entry_set_size(entry, png_size);
646+
archive_entry_set_size(entry, static_cast<la_int64_t>(png_buffer.size()));
644647
archive_entry_set_filetype(entry, AE_IFREG);
645648
constexpr int DEFAULT_FILE_PERMISSIONS = 0644;
646649
archive_entry_set_perm(entry, DEFAULT_FILE_PERMISSIONS);
@@ -652,7 +655,7 @@ class SpriteUnpacker {
652655
return false;
653656
}
654657

655-
if (archive_write_data(a, png_buffer.get(), png_size) != static_cast<ssize_t>(png_size)) {
658+
if (archive_write_data(a, png_buffer.data(), png_buffer.size()) != static_cast<ssize_t>(png_buffer.size())) {
656659
std::cerr << tr("Error: Failed to write archive data: ") << archive_error_string(a) << '\n';
657660
archive_entry_free(entry);
658661
return false;

0 commit comments

Comments
 (0)