Skip to content

Commit 13543ad

Browse files
authored
Merge pull request #70 from lordnn/master
Added some RAII,
2 parents 27ea8e1 + 2337acc commit 13543ad

3 files changed

Lines changed: 27 additions & 42 deletions

File tree

Source/FreeImage/CacheFile.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,10 @@ CacheFile::unlockBlock(int nr) {
178178
FIBOOL
179179
CacheFile::deleteBlock(int nr) {
180180
if (!m_current_block) {
181-
PageMapIt it = m_page_map.find(nr);
182-
183181
// remove block from cache
184182

185-
if (it != m_page_map.end()) {
186-
m_page_map.erase(nr);
183+
if (PageMapIt it = m_page_map.find(nr); it != m_page_map.end()) {
184+
m_page_map.erase(it);
187185
}
188186

189187
// add block to free page list

Source/FreeImage/MultiPage.cpp

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ FreeImage_OpenMultiBitmapU(FREE_IMAGE_FORMAT fif, const wchar_t* filename, FIBOO
321321
FIMULTIBITMAP * DLL_CALLCONV
322322
FreeImage_OpenMultiBitmapFromHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags) {
323323
try {
324-
bool read_only = false; // modifications (if any) will be stored into the memory cache
324+
const bool read_only = false; // modifications (if any) will be stored into the memory cache
325325

326326
if (io && handle) {
327327

@@ -432,28 +432,20 @@ bool SaveMultiBitmapToHandleImpl(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP* bitmap, F
432432
{
433433
// read the compressed data
434434

435-
auto *compressed_data = (uint8_t*)malloc(i->getSize() * sizeof(uint8_t));
435+
auto compressed_data = std::make_unique<uint8_t[]>(i->getSize());
436436

437-
header->m_cachefile.readFile((uint8_t *)compressed_data, i->getReference(), i->getSize());
437+
header->m_cachefile.readFile(compressed_data.get(), i->getReference(), i->getSize());
438438

439439
// uncompress the data
440440

441-
FIMEMORY *hmem = FreeImage_OpenMemory(compressed_data, i->getSize());
442-
FIBITMAP *dib = FreeImage_LoadFromMemory(header->cache_fif, hmem, 0);
443-
FreeImage_CloseMemory(hmem);
444-
445-
// get rid of the buffer
446-
free(compressed_data);
441+
std::unique_ptr<FIMEMORY, decltype(&FreeImage_CloseMemory)> hmem(FreeImage_OpenMemory(compressed_data.get(), i->getSize()), &FreeImage_CloseMemory);
442+
std::unique_ptr<FIBITMAP, decltype(&FreeImage_Unload)> dib(FreeImage_LoadFromMemory(header->cache_fif, hmem.get(), 0), &FreeImage_Unload);
447443

448444
// save the data
449445

450-
success = dst_node->Save(dib, dst_io, dst_handle, count, flags, dst_data);
446+
success = dst_node->Save(dib.get(), dst_io, dst_handle, count, flags, dst_data);
451447
count++;
452448

453-
// unload the dib
454-
455-
FreeImage_Unload(dib);
456-
457449
break;
458450
}
459451
}
@@ -605,25 +597,21 @@ FreeImage_SavePageToBlock(MULTIBITMAPHEADER *header, FIBITMAP *data) {
605597
// compress the bitmap data
606598

607599
// open a memory handle
608-
FIMEMORY *hmem = FreeImage_OpenMemory();
600+
std::unique_ptr<FIMEMORY, decltype(&FreeImage_CloseMemory)> hmem(FreeImage_OpenMemory(), &FreeImage_CloseMemory);
609601
if (!hmem) {
610602
return res;
611603
}
612604
// save the file to memory
613-
if (!FreeImage_SaveToMemory(header->cache_fif, data, hmem, 0)) {
614-
FreeImage_CloseMemory(hmem);
605+
if (!FreeImage_SaveToMemory(header->cache_fif, data, hmem.get(), 0)) {
615606
return res;
616607
}
617608
// get the buffer from the memory stream
618-
if (!FreeImage_AcquireMemory(hmem, &compressed_data, &compressed_size)) {
619-
FreeImage_CloseMemory(hmem);
609+
if (!FreeImage_AcquireMemory(hmem.get(), &compressed_data, &compressed_size)) {
620610
return res;
621611
}
622612

623613
// write the compressed data to the cache
624614
int ref = header->m_cachefile.writeFile(compressed_data, compressed_size);
625-
// get rid of the compressed data
626-
FreeImage_CloseMemory(hmem);
627615

628616
res = PageBlock(BLOCK_REFERENCE, ref, compressed_size);
629617

@@ -785,7 +773,7 @@ FreeImage_UnlockPage(FIMULTIBITMAP *bitmap, FIBITMAP *page, FIBOOL changed) {
785773
int iPage = header->m_cachefile.writeFile(compressed_data, compressed_size);
786774

787775
*i = PageBlock(BLOCK_REFERENCE, iPage, compressed_size);
788-
776+
789777
// get rid of the compressed data
790778

791779
FreeImage_CloseMemory(hmem);
@@ -810,8 +798,7 @@ FreeImage_MovePage(FIMULTIBITMAP *bitmap, int target, int source) {
810798
BlockListIterator block_source = FreeImage_FindBlock(bitmap, target);
811799
BlockListIterator block_target = FreeImage_FindBlock(bitmap, source);
812800

813-
header->m_blocks.insert(block_target, *block_source);
814-
header->m_blocks.erase(block_source);
801+
header->m_blocks.splice(block_target, header->m_blocks, block_source);
815802

816803
header->changed = true;
817804

@@ -860,7 +847,7 @@ FreeImage_LoadMultiBitmapFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int
860847
return nullptr;
861848
}
862849

863-
bool read_only = false; // modifications (if any) will be stored into the memory cache
850+
const bool read_only = false; // modifications (if any) will be stored into the memory cache
864851

865852
// retrieve the plugin list to find the node belonging to this plugin
866853

Source/Plugins/PluginHDR.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,40 +271,40 @@ rgbe_ReadHeader(FreeImageIO *io, fi_handle handle, unsigned *width, unsigned *he
271271
*/
272272
static FIBOOL
273273
rgbe_WriteHeader(FreeImageIO *io, fi_handle handle, unsigned width, unsigned height, rgbeHeaderInfo *info) {
274-
char buffer[HDR_MAXLINE];
274+
char buffer[HDR_MAXLINE + 1];
275275

276276
const char *programtype = "RADIANCE";
277277

278278
if (info && (info->valid & RGBE_VALID_PROGRAMTYPE)) {
279279
programtype = info->programtype;
280280
}
281281
// The #? is to identify file type, the programtype is optional
282-
snprintf(buffer, std::size(buffer), "#?%s\n", programtype);
283-
if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) {
282+
unsigned int len = snprintf(buffer, std::size(buffer), "#?%s\n", programtype);
283+
if (io->write_proc(buffer, 1, len, handle) < 1) {
284284
return rgbe_Error(rgbe_write_error, nullptr);
285285
}
286-
snprintf(buffer, std::size(buffer), "%s\n", info->comment);
287-
if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) {
286+
len = snprintf(buffer, std::size(buffer), "%s\n", info->comment);
287+
if (io->write_proc(buffer, 1, len, handle) < 1) {
288288
return rgbe_Error(rgbe_write_error, nullptr);
289289
}
290-
snprintf(buffer, std::size(buffer), "FORMAT=32-bit_rle_rgbe\n");
291-
if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) {
290+
len = snprintf(buffer, std::size(buffer), "FORMAT=32-bit_rle_rgbe\n");
291+
if (io->write_proc(buffer, 1, len, handle) < 1) {
292292
return rgbe_Error(rgbe_write_error, nullptr);
293293
}
294294
if (info && (info->valid & RGBE_VALID_GAMMA)) {
295-
snprintf(buffer, std::size(buffer), "GAMMA=%g\n", info->gamma);
296-
if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) {
295+
len = snprintf(buffer, std::size(buffer), "GAMMA=%g\n", info->gamma);
296+
if (io->write_proc(buffer, 1, len, handle) < 1) {
297297
return rgbe_Error(rgbe_write_error, nullptr);
298298
}
299299
}
300300
if (info && (info->valid & RGBE_VALID_EXPOSURE)) {
301-
snprintf(buffer,std::size(buffer), "EXPOSURE=%g\n", info->exposure);
302-
if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) {
301+
len = snprintf(buffer,std::size(buffer), "EXPOSURE=%g\n", info->exposure);
302+
if (io->write_proc(buffer, 1, len, handle) < 1) {
303303
return rgbe_Error(rgbe_write_error, nullptr);
304304
}
305305
}
306-
snprintf(buffer, std::size(buffer), "\n-Y %d +X %d\n", height, width);
307-
if (io->write_proc(buffer, 1, (unsigned int)strlen(buffer), handle) < 1) {
306+
len = snprintf(buffer, std::size(buffer), "\n-Y %d +X %d\n", height, width);
307+
if (io->write_proc(buffer, 1, len, handle) < 1) {
308308
return rgbe_Error(rgbe_write_error, nullptr);
309309
}
310310

0 commit comments

Comments
 (0)