Move "create GL texture" logic into texreader.py
[bsnes:xml-shaders.git] / reference / texreader.py
1 #!/usr/bin/python
2 import re
3 import string
4 from OpenGL.GL import *
5
6 TOKEN_RE = re.compile("[^" + string.whitespace + "]+")
7 WS_RE = re.compile("[" + string.whitespace + "]+")
8
9
10 class TexReaderException(Exception):
11         pass
12
13
14 class Texture(object):
15
16         def __init__(self, width, height, pixels=None):
17                 self.textureID = glGenTextures(1)
18                 self.width = width
19                 self.height = height
20
21                 glBindTexture(GL_TEXTURE_2D, self.textureID)
22
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_RGB, width, height,
26                                 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels)
27
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)
30
31         def __del__(self):
32                 glDeleteTextures([self.textureID])
33
34
35 def parse_ppm(data):
36         """
37         Parse the PPM data from the given byte string.
38         """
39         try:
40                 magic, widthStr, heightStr, sampleMaxStr, pixelData = \
41                                 data.split(None, 4)
42         except ValueError:
43                 raise TexReaderException("Can't parse data: %r..." % (data[:40],))
44
45         if magic != "P6":
46                 raise TexReaderException("Unrecognised magic %r" % (magic,))
47
48         try:
49                 width = int(widthStr)
50                 height = int(heightStr)
51                 sampleMax = int(sampleMaxStr)
52         except ValueError, e:
53                 raise TexReaderException("Can't parse data: %s" % (str(e),))
54
55         if sampleMax != 255:
56                 raise TexReaderException("Textures must have 8 bits per channel.")
57
58         if len(pixelData) != (width * height * 3):
59                 raise TexReaderException("Got %d bytes of pixel data, expected %d"
60                                 % (len(pixelData), width * height * 3))
61
62         # Now to convert this packed RGB pixel data to RGBA data that OpenGL can
63         # understand.
64         pixels = []
65         for pxOffset in range(0, (width * height * 3), 3):
66                 pixels.append(pixelData[pxOffset:pxOffset+3])
67                 pixels.append("\xff")
68
69         return (width, height, "".join(pixels))
70
71
72 def textureFromFile(filename):
73         with open(filename, "rb") as handle:
74                 width, height, pixels = parse_ppm(handle.read())
75
76         return Texture(width, height, pixels)
77
78
79 if __name__ == "__main__":
80         import sys
81         with open(sys.argv[1]) as handle:
82                 data = handle.read()
83                 print parse_ppm(data)