pygame / test / interop_pil2.py

Diff from to

test/interop_pil2.py

 
 '''Another interoperability test with PIL.
 
-This time the buffer of a single surface is updated using a slice assignment,
-rather than creating a new buffer each frame.
+This time the buffer of a single surface is updated using ctypes.memmove
+instead of recreating the surface each frame.
 '''
 
 __docformat__ = 'restructuredtext'
 import os
 import sys
 
+from ctypes import *
 from SDL import *
 import SDL.array
 import Image
         angle = (angle + 1) % 360
         image = original_image.rotate(angle)
 
+        SDL_LockSurface(surface)
 
-        SDL_LockSurface(surface)
-        surface.pixels[:] = \
-            [c[0] | (c[1] << 8) | (c[2] << 16) for c in image.getdata()]
+        # Copy image data into surface pixel buffer using ctypes.memmove
+        s = image.tostring()
+        ref, ar = SDL.array.to_ctypes(s, len(s), c_ubyte)
+        memmove(surface.pixels.as_bytes().as_ctypes(), ar, len(ar))
 
-        # This alternative also works
+        # This alternative also works (slower)
+        #surface.pixels[:] = \
+        #    [c[0] | (c[1] << 8) | (c[2] << 16) for c in image.getdata()]
+
+        # This alternative also works (slower)
         #surface.pixels.as_bytes()[:] = \
         #    [ord(c) for c in image.tostring()]
         SDL_UnlockSurface(surface)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.