@@ -321,7 +321,7 @@ FreeImage_OpenMultiBitmapU(FREE_IMAGE_FORMAT fif, const wchar_t* filename, FIBOO
321321FIMULTIBITMAP * DLL_CALLCONV
322322FreeImage_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
0 commit comments