4 from OpenGL.GL import *
6 TOKEN_RE = re.compile("[^" + string.whitespace + "]+")
7 WS_RE = re.compile("[" + string.whitespace + "]+")
10 class TexReaderException(Exception):
14 class Texture(object):
16 def __init__(self, width, height, pixels=None):
17 self.textureID = glGenTextures(1)
21 glBindTexture(GL_TEXTURE_2D, self.textureID)
23 # We assume the GL implementation can handle non-power-of-two textures,
24 # so we use the image dimensions directly as texture dimensions.
25 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
26 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels)
28 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER)
29 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER)
32 glDeleteTextures([self.textureID])
37 Parse the PPM data from the given byte string.
40 magic, widthStr, heightStr, sampleMaxStr, pixelData = \
43 raise TexReaderException("Can't parse data: %r..." % (data[:40],))
46 raise TexReaderException("Unrecognised magic %r" % (magic,))
50 height = int(heightStr)
51 sampleMax = int(sampleMaxStr)
53 raise TexReaderException("Can't parse data: %s" % (str(e),))
56 raise TexReaderException("Textures must have 8 bits per channel.")
58 if len(pixelData) != (width * height * 3):
59 raise TexReaderException("Got %d bytes of pixel data, expected %d"
60 % (len(pixelData), width * height * 3))
62 # Now to convert this packed RGB pixel data to RGBA data that OpenGL can
65 for pxOffset in range(0, (width * height * 3), 3):
66 pixels.append(pixelData[pxOffset:pxOffset+3])
69 return (width, height, "".join(pixels))
72 def textureFromFile(filename):
73 with open(filename, "rb") as handle:
74 width, height, pixels = parse_ppm(handle.read())
76 return Texture(width, height, pixels)
79 if __name__ == "__main__":
81 with open(sys.argv[1]) as handle: