diff pytouhou/ui/texture.pyx @ 368:71cd4461bb7f

Minor optimizations
author Thibaut Girka <thib@sitedethib.com>
date Wed, 11 Jul 2012 15:19:44 +0200
parents 98c64ffcbdff
children 45e1a9a37e66
line wrap: on
line diff
--- a/pytouhou/ui/texture.pyx
+++ b/pytouhou/ui/texture.pyx
@@ -12,6 +12,8 @@
 ## GNU General Public License for more details.
 ##
 
+from libc.stdlib cimport malloc, free
+
 import pyglet
 from pyglet.gl import (glTexParameteri,
                        GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
@@ -42,6 +44,10 @@ cdef class TextureManager:
 
 
     def load_texture(self, key):
+        cdef bytes data, alpha_data
+        cdef char *new_data
+        cdef unsigned int i, width, height, pixels
+
         first_name, secondary_name = key
 
         image_file = pyglet.image.load(first_name, file=self.loader.get_file(os.path.basename(first_name)))
@@ -50,11 +56,20 @@ cdef class TextureManager:
             alpha_file = pyglet.image.load(secondary_name, file=self.loader.get_file(os.path.basename(secondary_name)))
             assert (image_file.width, image_file.height) == (alpha_file.width, image_file.height)
 
-            data = image_file.get_data('RGB', image_file.width * 3)
-            alpha_data = alpha_file.get_data('RGB', image_file.width * 3)
-            image_file = pyglet.image.ImageData(image_file.width, image_file.height, 'RGBA', b''.join(data[i*3:i*3+3] + alpha_data[i*3] for i in range(image_file.width * image_file.height)))
+            width, height = image_file.width, image_file.height
+            pixels = width * height
+            data = image_file.get_data('RGB', width * 3)
+            alpha_data = alpha_file.get_data('RGB', width * 3)
 
-            #TODO: improve perfs somehow
+            # TODO: further optimizations
+            new_data = <char *>malloc(pixels * 4)
+            for i in range(pixels):
+                new_data[i*4] = (<char *>data)[i*3]
+                new_data[i*4+1] = (<char *>data)[i*3+1]
+                new_data[i*4+2] = (<char *>data)[i*3+2]
+                new_data[i*4+3] = (<char *>alpha_data)[i*3]
+            image_file = pyglet.image.ImageData(width, height, 'RGBA', new_data[:(pixels * 4)])
+            free(new_data)
 
         texture = image_file.get_texture()