Commit c456eeb47add724147b8a28160ae0e7929e2a88d

Alexei Podtelezhnikov 2022-09-22T15:09:17

[base] Discard and recreate bitmaps for copying and converting. Reusing target bitmaps for copying and converting is permitted. It is, however, pointless to preserve their content before overwriting. Free- malloc might be faster than realloc. * src/base/ftbitmap.c (FT_Bitmap_Copy, FT_Bitmap_Convert): Free an old buffer and create a new one.

diff --git a/src/base/ftbitmap.c b/src/base/ftbitmap.c
index 7825895..c79409d 100644
--- a/src/base/ftbitmap.c
+++ b/src/base/ftbitmap.c
@@ -66,9 +66,7 @@
   {
     FT_Memory  memory;
     FT_Error   error  = FT_Err_Ok;
-
-    FT_Int    pitch;
-    FT_ULong  size;
+    FT_Int     pitch;
 
     FT_Int  source_pitch_sign, target_pitch_sign;
 
@@ -85,49 +83,28 @@
     source_pitch_sign = source->pitch < 0 ? -1 : 1;
     target_pitch_sign = target->pitch < 0 ? -1 : 1;
 
-    if ( !source->buffer )
-    {
-      *target = *source;
-      if ( source_pitch_sign != target_pitch_sign )
-        target->pitch = -target->pitch;
+    memory = library->memory;
+    FT_FREE( target->buffer );
+
+    *target = *source;
+
+    if ( source_pitch_sign != target_pitch_sign )
+      target->pitch = -target->pitch;
 
+    if ( !source->buffer )
       return FT_Err_Ok;
-    }
 
-    memory = library->memory;
     pitch  = source->pitch;
-
     if ( pitch < 0 )
       pitch = -pitch;
-    size = (FT_ULong)pitch * source->rows;
 
-    if ( target->buffer )
-    {
-      FT_Int    target_pitch = target->pitch;
-      FT_ULong  target_size;
-
-
-      if ( target_pitch < 0 )
-        target_pitch = -target_pitch;
-      target_size = (FT_ULong)target_pitch * target->rows;
-
-      if ( target_size != size )
-        FT_MEM_QREALLOC( target->buffer, target_size, size );
-    }
-    else
-      FT_MEM_QALLOC( target->buffer, size );
+    FT_MEM_QALLOC_MULT( target->buffer, target->rows, pitch );
 
     if ( !error )
     {
-      unsigned char *p;
-
-
-      p = target->buffer;
-      *target = *source;
-      target->buffer = p;
-
       if ( source_pitch_sign == target_pitch_sign )
-        FT_MEM_COPY( target->buffer, source->buffer, size );
+        FT_MEM_COPY( target->buffer, source->buffer,
+                     (FT_Long)source->rows * pitch );
       else
       {
         /* take care of bitmap flow */
@@ -542,15 +519,11 @@
     case FT_PIXEL_MODE_LCD_V:
     case FT_PIXEL_MODE_BGRA:
       {
-        FT_Int    pad, old_target_pitch, target_pitch;
-        FT_ULong  old_size;
+        FT_Int  pad, target_pitch; 
+        FT_Int  old_target_pitch = target->pitch;
 
 
-        old_target_pitch = target->pitch;
-        if ( old_target_pitch < 0 )
-          old_target_pitch = -old_target_pitch;
-
-        old_size = target->rows * (FT_UInt)old_target_pitch;
+        FT_Bitmap_Done( library, target );
 
         target->pixel_mode = FT_PIXEL_MODE_GRAY;
         target->rows       = source->rows;
@@ -566,15 +539,10 @@
 
         target_pitch = (FT_Int)source->width + pad;
 
-        if ( target_pitch > 0                                               &&
-             (FT_ULong)target->rows > FT_ULONG_MAX / (FT_ULong)target_pitch )
-          return FT_THROW( Invalid_Argument );
-
-        if ( FT_QREALLOC( target->buffer,
-                          old_size, target->rows * (FT_UInt)target_pitch ) )
+        if ( FT_QALLOC_MULT( target->buffer, target->rows, target_pitch ) )
           return error;
 
-        target->pitch = target->pitch < 0 ? -target_pitch : target_pitch;
+        target->pitch = old_target_pitch < 0 ? -target_pitch : target_pitch;
       }
       break;