changeset 368:71cd4461bb7f

Minor optimizations
author Thibaut Girka <thib@sitedethib.com>
date Wed, 11 Jul 2012 15:19:44 +0200
parents 2674c789e0c3
children f305cdd6f6c5
files pytouhou/ui/texture.pyx pytouhou/utils/lzss.pyx
diffstat 2 files changed, 27 insertions(+), 9 deletions(-) [+]
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()
 
--- a/pytouhou/utils/lzss.pyx
+++ b/pytouhou/utils/lzss.pyx
@@ -22,11 +22,12 @@ cpdef bytes decompress(object bitstream,
                        unsigned int length_size=4,
                        unsigned int minimum_match_length=3):
     cdef unsigned int i, ptr, dictionary_head, offset, length
-    cdef unsigned char byte, *out_data, *dictionary
+    cdef unsigned char byte
+    cdef char *out_data, *dictionary
     cdef bytes _out_data
 
-    out_data = <unsigned char*> malloc(size)
-    dictionary = <unsigned char*> calloc(dictionary_size, 1)
+    out_data = <char*> malloc(size)
+    dictionary = <char*> calloc(dictionary_size, 1)
     dictionary_head, ptr = 1, 0
 
     while ptr < size:
@@ -42,13 +43,15 @@ cpdef bytes decompress(object bitstream,
             # The `flag` bit is not set, the upcoming chunk is a (offset, length) tuple
             offset = bitstream.read(offset_size)
             length = bitstream.read(length_size) + minimum_match_length
+            if ptr + length > size:
+                raise Exception
             if offset == 0 and length == 0:
                 break
             for i in range(offset, offset + length):
-                out_data[ptr % size] = dictionary[i % dictionary_size]
-                ptr += 1
+                out_data[ptr] = dictionary[i % dictionary_size]
                 dictionary[dictionary_head] = dictionary[i % dictionary_size]
                 dictionary_head = (dictionary_head + 1) % dictionary_size
+                ptr += 1
 
     _out_data = out_data[:size]
     free(out_data)