comparison pytouhou/ui/texture.pyx @ 420:3a7b36324611

Replace Pyglet’s image loader with our SDL2_image-based one.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 16 Jul 2013 21:07:15 +0200
parents 9e2cbb2c2c64
children d8630c086926
comparison
equal deleted inserted replaced
419:1c92721f8e49 420:3a7b36324611
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from libc.stdlib cimport malloc, free
16
17 import pyglet
18 from pyglet.gl import (glTexParameteri, GL_TEXTURE_MIN_FILTER, 15 from pyglet.gl import (glTexParameteri, GL_TEXTURE_MIN_FILTER,
19 GL_TEXTURE_MAG_FILTER, GL_LINEAR, GL_BGRA, GL_RGBA, 16 GL_TEXTURE_MAG_FILTER, GL_LINEAR, GL_BGRA, GL_RGBA,
20 GL_RGB, GL_LUMINANCE, GL_UNSIGNED_BYTE, 17 GL_RGB, GL_LUMINANCE, GL_UNSIGNED_BYTE,
21 GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4_REV, 18 GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4_REV,
22 glGenTextures, glBindTexture, glTexImage2D, 19 glGenTextures, glBindTexture, glTexImage2D,
23 GL_TEXTURE_2D) 20 GL_TEXTURE_2D)
24 from ctypes import c_uint, byref 21 from ctypes import c_uint, byref
22 from pytouhou.lib.sdl import load_png, create_rgb_surface
25 import os 23 import os
26 24
27 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere? 25 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere?
28 26
29 27
43 for anm in anm_wrapper: 41 for anm in anm_wrapper:
44 anm.texture = self.load_png_texture(anm.first_name, anm.secondary_name) 42 anm.texture = self.load_png_texture(anm.first_name, anm.secondary_name)
45 43
46 44
47 def load_png_texture(self, first_name, secondary_name): 45 def load_png_texture(self, first_name, secondary_name):
48 cdef char *image, *alpha, *new_data 46 image_file = load_png(self.loader.get_file(os.path.basename(first_name)))
49 cdef unsigned int i, width, height, pixels 47 width, height = image_file.width, image_file.height
50 48
51 image_file = pyglet.image.load(first_name, file=self.loader.get_file(os.path.basename(first_name))) 49 # Support only 32 bits RGBA. Paletted surfaces are awful to work with.
52 width, height = image_file.width, image_file.height 50 #TODO: verify it doesn’t blow up on big-endian systems.
53 image_data = image_file.get_data('RGB', width * 3) 51 new_image = create_rgb_surface(width, height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)
52 new_image.blit(image_file)
54 53
55 if secondary_name: 54 if secondary_name:
56 alpha_file = pyglet.image.load(secondary_name, file=self.loader.get_file(os.path.basename(secondary_name))) 55 alpha_file = load_png(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 (width == alpha_file.width and
57 height == alpha_file.height)
58 58
59 pixels = width * height 59 new_alpha_file = create_rgb_surface(width, height, 24)
60 new_alpha_file.blit(alpha_file)
60 61
61 alpha_data = alpha_file.get_data('RGB', width * 3) 62 new_image.set_alpha(new_alpha_file)
62 image = <char *>image_data
63 alpha = <char *>alpha_data
64 63
65 # TODO: further optimizations 64 return Texture(width, height, -4, new_image.pixels)
66 new_data = <char *>malloc(pixels * 4)
67 for i in range(pixels):
68 new_data[i*4] = image[i*3]
69 new_data[i*4+1] = image[i*3+1]
70 new_data[i*4+2] = image[i*3+2]
71 new_data[i*4+3] = alpha[i*3]
72 data = new_data[:(pixels * 4)]
73 free(new_data)
74 return Texture(width, height, -4, data)
75
76 return Texture(width, height, -3, image_data)
77 65
78 66
79 def load_texture(self, key): 67 def load_texture(self, key):
80 if not isinstance(key, Texture): 68 if not isinstance(key, Texture):
81 first_name, secondary_name = key 69 first_name, secondary_name = key
95 composants = GL_RGBA 83 composants = GL_RGBA
96 elif key.fmt == 7: 84 elif key.fmt == 7:
97 format_ = GL_LUMINANCE 85 format_ = GL_LUMINANCE
98 type_ = GL_UNSIGNED_BYTE 86 type_ = GL_UNSIGNED_BYTE
99 composants = GL_LUMINANCE 87 composants = GL_LUMINANCE
100 elif key.fmt == -3: #XXX: non-standard, remove it!
101 format_ = GL_RGB
102 type_ = GL_UNSIGNED_BYTE
103 composants = GL_RGB
104 elif key.fmt == -4: #XXX: non-standard 88 elif key.fmt == -4: #XXX: non-standard
105 format_ = GL_RGBA 89 format_ = GL_RGBA
106 type_ = GL_UNSIGNED_BYTE 90 type_ = GL_UNSIGNED_BYTE
107 composants = GL_RGBA 91 composants = GL_RGBA
108 else: 92 else: