From bdae12e68cd2c2d7fd5c300803c82d0ea918f3ab Mon Sep 17 00:00:00 2001 From: lordnn Date: Sun, 8 Mar 2026 17:31:47 +0300 Subject: [PATCH 1/4] Added some RAII, --- Source/FreeImage/MultiPage.cpp | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/Source/FreeImage/MultiPage.cpp b/Source/FreeImage/MultiPage.cpp index 2145668..436e780 100644 --- a/Source/FreeImage/MultiPage.cpp +++ b/Source/FreeImage/MultiPage.cpp @@ -321,7 +321,7 @@ FreeImage_OpenMultiBitmapU(FREE_IMAGE_FORMAT fif, const wchar_t* filename, FIBOO FIMULTIBITMAP * DLL_CALLCONV FreeImage_OpenMultiBitmapFromHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags) { try { - bool read_only = false; // modifications (if any) will be stored into the memory cache + const bool read_only = false; // modifications (if any) will be stored into the memory cache if (io && handle) { @@ -432,28 +432,20 @@ bool SaveMultiBitmapToHandleImpl(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP* bitmap, F { // read the compressed data - auto *compressed_data = (uint8_t*)malloc(i->getSize() * sizeof(uint8_t)); + auto compressed_data = std::make_unique(i->getSize()); - header->m_cachefile.readFile((uint8_t *)compressed_data, i->getReference(), i->getSize()); + header->m_cachefile.readFile(compressed_data.get(), i->getReference(), i->getSize()); // uncompress the data - FIMEMORY *hmem = FreeImage_OpenMemory(compressed_data, i->getSize()); - FIBITMAP *dib = FreeImage_LoadFromMemory(header->cache_fif, hmem, 0); - FreeImage_CloseMemory(hmem); - - // get rid of the buffer - free(compressed_data); + std::unique_ptr hmem(FreeImage_OpenMemory(compressed_data.get(), i->getSize()), &FreeImage_CloseMemory); + std::unique_ptr dib(FreeImage_LoadFromMemory(header->cache_fif, hmem.get(), 0), &FreeImage_Unload); // save the data - success = dst_node->Save(dib, dst_io, dst_handle, count, flags, dst_data); + success = dst_node->Save(dib.get(), dst_io, dst_handle, count, flags, dst_data); count++; - // unload the dib - - FreeImage_Unload(dib); - break; } } @@ -605,25 +597,21 @@ FreeImage_SavePageToBlock(MULTIBITMAPHEADER *header, FIBITMAP *data) { // compress the bitmap data // open a memory handle - FIMEMORY *hmem = FreeImage_OpenMemory(); + std::unique_ptr hmem(FreeImage_OpenMemory(), &FreeImage_CloseMemory); if (!hmem) { return res; } // save the file to memory - if (!FreeImage_SaveToMemory(header->cache_fif, data, hmem, 0)) { - FreeImage_CloseMemory(hmem); + if (!FreeImage_SaveToMemory(header->cache_fif, data, hmem.get(), 0)) { return res; } // get the buffer from the memory stream - if (!FreeImage_AcquireMemory(hmem, &compressed_data, &compressed_size)) { - FreeImage_CloseMemory(hmem); + if (!FreeImage_AcquireMemory(hmem.get(), &compressed_data, &compressed_size)) { return res; } // write the compressed data to the cache int ref = header->m_cachefile.writeFile(compressed_data, compressed_size); - // get rid of the compressed data - FreeImage_CloseMemory(hmem); res = PageBlock(BLOCK_REFERENCE, ref, compressed_size); @@ -785,7 +773,7 @@ FreeImage_UnlockPage(FIMULTIBITMAP *bitmap, FIBITMAP *page, FIBOOL changed) { int iPage = header->m_cachefile.writeFile(compressed_data, compressed_size); *i = PageBlock(BLOCK_REFERENCE, iPage, compressed_size); - + // get rid of the compressed data FreeImage_CloseMemory(hmem); @@ -860,7 +848,7 @@ FreeImage_LoadMultiBitmapFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int return nullptr; } - bool read_only = false; // modifications (if any) will be stored into the memory cache + const bool read_only = false; // modifications (if any) will be stored into the memory cache // retrieve the plugin list to find the node belonging to this plugin From 55a87d7b6bff3127d03680c1ef2a52b48e9ea1a7 Mon Sep 17 00:00:00 2001 From: lordnn Date: Sun, 8 Mar 2026 18:35:10 +0300 Subject: [PATCH 2/4] Fixed GCC warning. --- Source/Plugins/PluginHDR.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Source/Plugins/PluginHDR.cpp b/Source/Plugins/PluginHDR.cpp index f1461b2..ec4dd26 100644 --- a/Source/Plugins/PluginHDR.cpp +++ b/Source/Plugins/PluginHDR.cpp @@ -271,7 +271,7 @@ rgbe_ReadHeader(FreeImageIO *io, fi_handle handle, unsigned *width, unsigned *he */ static FIBOOL rgbe_WriteHeader(FreeImageIO *io, fi_handle handle, unsigned width, unsigned height, rgbeHeaderInfo *info) { - char buffer[HDR_MAXLINE]; + char buffer[HDR_MAXLINE + 1]; const char *programtype = "RADIANCE"; @@ -279,32 +279,32 @@ rgbe_WriteHeader(FreeImageIO *io, fi_handle handle, unsigned width, unsigned hei programtype = info->programtype; } // The #? is to identify file type, the programtype is optional - snprintf(buffer, std::size(buffer), "#?%s\n", programtype); - if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) { + unsigned int len = snprintf(buffer, std::size(buffer), "#?%s\n", programtype); + if (io->write_proc(buffer, 1, len, handle) < 1) { return rgbe_Error(rgbe_write_error, nullptr); } - snprintf(buffer, std::size(buffer), "%s\n", info->comment); - if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) { + len = snprintf(buffer, std::size(buffer), "%s\n", info->comment); + if (io->write_proc(buffer, 1, len, handle) < 1) { return rgbe_Error(rgbe_write_error, nullptr); } - snprintf(buffer, std::size(buffer), "FORMAT=32-bit_rle_rgbe\n"); - if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) { + len = snprintf(buffer, std::size(buffer), "FORMAT=32-bit_rle_rgbe\n"); + if (io->write_proc(buffer, 1, len, handle) < 1) { return rgbe_Error(rgbe_write_error, nullptr); } if (info && (info->valid & RGBE_VALID_GAMMA)) { - snprintf(buffer, std::size(buffer), "GAMMA=%g\n", info->gamma); - if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) { + len = snprintf(buffer, std::size(buffer), "GAMMA=%g\n", info->gamma); + if (io->write_proc(buffer, 1, len, handle) < 1) { return rgbe_Error(rgbe_write_error, nullptr); } } if (info && (info->valid & RGBE_VALID_EXPOSURE)) { - snprintf(buffer,std::size(buffer), "EXPOSURE=%g\n", info->exposure); - if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) { + len = snprintf(buffer,std::size(buffer), "EXPOSURE=%g\n", info->exposure); + if (io->write_proc(buffer, 1, len, handle) < 1) { return rgbe_Error(rgbe_write_error, nullptr); } } - snprintf(buffer, std::size(buffer), "\n-Y %d +X %d\n", height, width); - if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) { + len = snprintf(buffer, std::size(buffer), "\n-Y %d +X %d\n", height, width); + if (io->write_proc(buffer, 1, len, handle) < 1) { return rgbe_Error(rgbe_write_error, nullptr); } From f69320ec8d268e69e5283a6690ed844598688a9c Mon Sep 17 00:00:00 2001 From: lordnn Date: Sun, 8 Mar 2026 19:00:03 +0300 Subject: [PATCH 3/4] Minor cleanup. --- Source/FreeImage/CacheFile.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Source/FreeImage/CacheFile.cpp b/Source/FreeImage/CacheFile.cpp index a93f6f7..34c4137 100644 --- a/Source/FreeImage/CacheFile.cpp +++ b/Source/FreeImage/CacheFile.cpp @@ -178,12 +178,10 @@ CacheFile::unlockBlock(int nr) { FIBOOL CacheFile::deleteBlock(int nr) { if (!m_current_block) { - PageMapIt it = m_page_map.find(nr); - // remove block from cache - if (it != m_page_map.end()) { - m_page_map.erase(nr); + if (PageMapIt it = m_page_map.find(nr); it != m_page_map.end()) { + m_page_map.erase(it); } // add block to free page list From 2337acc6e18cfbd4d0425604bd085abe799b8af9 Mon Sep 17 00:00:00 2001 From: lordnn Date: Sun, 8 Mar 2026 19:20:03 +0300 Subject: [PATCH 4/4] More cleanup. --- Source/FreeImage/MultiPage.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Source/FreeImage/MultiPage.cpp b/Source/FreeImage/MultiPage.cpp index 436e780..48f24ff 100644 --- a/Source/FreeImage/MultiPage.cpp +++ b/Source/FreeImage/MultiPage.cpp @@ -798,8 +798,7 @@ FreeImage_MovePage(FIMULTIBITMAP *bitmap, int target, int source) { BlockListIterator block_source = FreeImage_FindBlock(bitmap, target); BlockListIterator block_target = FreeImage_FindBlock(bitmap, source); - header->m_blocks.insert(block_target, *block_source); - header->m_blocks.erase(block_source); + header->m_blocks.splice(block_target, header->m_blocks, block_source); header->changed = true;