reduce memory usage of screenshot mirroring
authoralfadur
Fri, 16 Aug 2019 20:45:25 +0300
changeset 15335 f9f34158ef72
parent 15334 8951eaad20e5
child 15336 f53bfe04065e
reduce memory usage of screenshot mirroring
hedgewars/uMisc.pas
--- a/hedgewars/uMisc.pas	Fri Aug 16 11:02:01 2019 -0600
+++ b/hedgewars/uMisc.pas	Fri Aug 16 20:45:25 2019 +0300
@@ -105,21 +105,29 @@
 var
     surface: PSDL_Surface;
     image: PScreenshot;
-    mirror: PByte;
+    rowBuffer: PByte;
     row, stride: LongInt;
 begin
     image:= PScreenshot(screenshot);
-    mirror:= PByte(GetMem(image^.size));
     stride:= image^.width * 4;
+    rowBuffer:= PByte(GetMem(stride));
 
-    for row:= 0 to image^.height - 1 do
+    for row:= 0 to (image^.height div 2) - 1 do
+        begin
+        Move((image^.buffer + (image^.height - row - 1) * stride)^,
+             rowBuffer^,
+             stride);
         Move((image^.buffer + row * stride)^,
-             (mirror + (image^.height - row - 1) * stride)^,
+             (image^.buffer + (image^.height - row - 1) * stride)^,
              stride);
+        Move(rowBuffer^,
+             (image^.buffer + row * stride)^,
+             stride);
+        end;
 
     surface:= SDL_CreateRGBSurfaceFrom(
-        mirror,
-        image^.width, image^.height, 32, image^.width * 4,
+        image^.buffer,
+        image^.width, image^.height, 32, stride,
         $000000FF, $0000FF00, $00FF0000, $FF000000);
 
     if surface <> nil then
@@ -128,7 +136,7 @@
         SDL_FreeSurface(surface);
         end;
 
-    FreeMem(mirror, image^.size);
+    FreeMem(rowBuffer, stride);
     FreeMem(image^.buffer, image^.size);
     Dispose(image);
     SaveScreenshot:= 0;