[gimp/gimp-2-10] plug-ins: do not needlessly free/malloc() buffer of same size.



commit 66c47ee23f6f375f36870fe9ad6f38c3ee82374e
Author: Jehan <jehan girinstud io>
Date:   Tue Mar 19 12:46:12 2019 +0100

    plug-ins: do not needlessly free/malloc() buffer of same size.
    
    In an animated WebP, chances that layers/frame have the same size is
    high. It is uneeded to free then malloc again a buffer at each frame,
    unless we need more allocated memory.
    This is probably not so significant, but still feels nicer.
    
    (cherry picked from commit 0b68ce81821eebd459e1ed8b2fa9daa09b62c291)

 plug-ins/file-webp/file-webp-save.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
---
diff --git a/plug-ins/file-webp/file-webp-save.c b/plug-ins/file-webp/file-webp-save.c
index 3aa3818d00..50325f0601 100644
--- a/plug-ins/file-webp/file-webp-save.c
+++ b/plug-ins/file-webp/file-webp-save.c
@@ -481,6 +481,7 @@ save_animation (const gchar    *filename,
   gboolean               status   = TRUE;
   FILE                  *outfile  = NULL;
   guchar                *buffer   = NULL;
+  gint                   buffer_size = 0;
   gint                   w, h;
   gint                   bpp;
   gboolean               has_alpha;
@@ -584,12 +585,20 @@ save_animation (const gchar    *filename,
             }
 
           /* Attempt to allocate a buffer of the appropriate size */
-          buffer = g_try_malloc (w * h * bpp);
-          if (! buffer)
+          if (! buffer || buffer_size < w * h * bpp)
             {
-              g_printerr ("Buffer error: 'buffer null'\n");
-              status = FALSE;
-              break;
+              buffer = g_try_realloc (buffer, w * h * bpp);
+
+              if (! buffer)
+                {
+                  g_printerr ("Buffer error: 'buffer null'\n");
+                  status = FALSE;
+                  break;
+                }
+              else
+                {
+                  buffer_size = w * h * bpp;
+                }
             }
 
           WebPConfigPreset (&config, params->preset, params->quality);
@@ -638,7 +647,6 @@ save_animation (const gchar    *filename,
             {
               status = WebPPictureImportRGBA (&picture, buffer, w * bpp);
             }
-          g_free (buffer);
 
           if (! status)
             {
@@ -663,6 +671,7 @@ save_animation (const gchar    *filename,
           gimp_progress_update ((loop + 1.0) / nLayers);
           frame_timestamp += (delay <= 0 || force_delay) ? default_delay : delay;
         }
+      g_free (buffer);
 
       if (status == FALSE)
         break;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]