# HG changeset patch # User Thibaut Girka # Date 1342012784 -7200 # Node ID 71cd4461bb7f8e796b19a8bebb85e081a441110d # Parent 2674c789e0c3782c20a1b2341b3935497544a532 Minor optimizations diff --git a/pytouhou/ui/texture.pyx b/pytouhou/ui/texture.pyx --- 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 = malloc(pixels * 4) + for i in range(pixels): + new_data[i*4] = (data)[i*3] + new_data[i*4+1] = (data)[i*3+1] + new_data[i*4+2] = (data)[i*3+2] + new_data[i*4+3] = (alpha_data)[i*3] + image_file = pyglet.image.ImageData(width, height, 'RGBA', new_data[:(pixels * 4)]) + free(new_data) texture = image_file.get_texture() diff --git a/pytouhou/utils/lzss.pyx b/pytouhou/utils/lzss.pyx --- 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 = malloc(size) - dictionary = calloc(dictionary_size, 1) + out_data = malloc(size) + dictionary = 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)