5 TOKEN_RE = re.compile("[^" + string.whitespace + "]+")
6 WS_RE = re.compile("[" + string.whitespace + "]+")
9 class TexReaderException(Exception):
15 Parse the PPM data from the given byte string.
18 magic, widthStr, heightStr, sampleMaxStr, pixelData = \
21 raise TexReaderException("Can't parse data: %r..." % (data[:40],))
24 raise TexReaderException("Unrecognised magic %r" % (magic,))
28 height = int(heightStr)
29 sampleMax = int(sampleMaxStr)
31 raise TexReaderException("Can't parse data: %s" % (str(e),))
34 raise TexReaderException("Textures must have 8 bits per channel.")
36 if len(pixelData) != (width * height * 3):
37 raise TexReaderException("Got %d bytes of pixel data, expected %d"
38 % (len(pixelData), width * height * 3))
40 # Now to convert this packed RGB pixel data to RGBA data that OpenGL can
43 for pxOffset in range(0, (width * height * 3), 3):
44 pixels.append(pixelData[pxOffset:pxOffset+3])
47 return (width, height, "".join(pixels))
50 if __name__ == "__main__":
52 with open(sys.argv[1]) as handle: