comparison pytouhou/ui/texture.pyx @ 392:45e1a9a37e66

When merging RGB and alpha data, get the C arrays only at the start of the loop.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 07 Jan 2013 22:06:02 +0100
parents 71cd4461bb7f
children 9e2cbb2c2c64
comparison
equal deleted inserted replaced
391:84b151962708 392:45e1a9a37e66
42 key = anm.first_name, anm.secondary_name 42 key = anm.first_name, anm.secondary_name
43 texture = self[key] 43 texture = self[key]
44 44
45 45
46 def load_texture(self, key): 46 def load_texture(self, key):
47 cdef bytes data, alpha_data 47 cdef char *image, *alpha, *new_data
48 cdef char *new_data
49 cdef unsigned int i, width, height, pixels 48 cdef unsigned int i, width, height, pixels
50 49
51 first_name, secondary_name = key 50 first_name, secondary_name = key
52 51
53 image_file = pyglet.image.load(first_name, file=self.loader.get_file(os.path.basename(first_name))) 52 image_file = pyglet.image.load(first_name, file=self.loader.get_file(os.path.basename(first_name)))
56 alpha_file = pyglet.image.load(secondary_name, file=self.loader.get_file(os.path.basename(secondary_name))) 55 alpha_file = pyglet.image.load(secondary_name, file=self.loader.get_file(os.path.basename(secondary_name)))
57 assert (image_file.width, image_file.height) == (alpha_file.width, image_file.height) 56 assert (image_file.width, image_file.height) == (alpha_file.width, image_file.height)
58 57
59 width, height = image_file.width, image_file.height 58 width, height = image_file.width, image_file.height
60 pixels = width * height 59 pixels = width * height
61 data = image_file.get_data('RGB', width * 3) 60
61 image_data = image_file.get_data('RGB', width * 3)
62 alpha_data = alpha_file.get_data('RGB', width * 3) 62 alpha_data = alpha_file.get_data('RGB', width * 3)
63 image = <char *>image_data
64 alpha = <char *>alpha_data
63 65
64 # TODO: further optimizations 66 # TODO: further optimizations
65 new_data = <char *>malloc(pixels * 4) 67 new_data = <char *>malloc(pixels * 4)
66 for i in range(pixels): 68 for i in range(pixels):
67 new_data[i*4] = (<char *>data)[i*3] 69 new_data[i*4] = image[i*3]
68 new_data[i*4+1] = (<char *>data)[i*3+1] 70 new_data[i*4+1] = image[i*3+1]
69 new_data[i*4+2] = (<char *>data)[i*3+2] 71 new_data[i*4+2] = image[i*3+2]
70 new_data[i*4+3] = (<char *>alpha_data)[i*3] 72 new_data[i*4+3] = alpha[i*3]
71 image_file = pyglet.image.ImageData(width, height, 'RGBA', new_data[:(pixels * 4)]) 73 image_file = pyglet.image.ImageData(width, height, 'RGBA', new_data[:(pixels * 4)])
72 free(new_data) 74 free(new_data)
73 75
74 texture = image_file.get_texture() 76 texture = image_file.get_texture()
75 77