-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMpcdiFile.py
More file actions
155 lines (125 loc) · 4.74 KB
/
MpcdiFile.py
File metadata and controls
155 lines (125 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import zipfile
from xml.etree import ElementTree
import PfmFile
import TextureImage
import os.path
class MpcdiFile:
def __init__(self, filename = None):
self.filename = None
self.zip = None
self.doc = None
self.profile = None
self.buffers = {}
self.regions = {}
if filename:
self.read(filename)
def read(self, filename):
""" Reads the mpcdi file and sets up the corresponding data
structures internally. """
self.filename = filename
print "Reading %s" % (self.filename)
if os.path.isdir(self.filename):
# Read a directory directly
self.zip = None
else:
# Read a zipfile via the ZipFile module
self.zip = zipfile.ZipFile(self.filename, 'r')
docData = self.extractSubfile('mpcdi.xml')
self.doc = ElementTree.fromstring(docData)
self.profile = None
self.buffers = {}
self.regions = {}
self.regionIdList = [] # sorted in file order
self.profile = self.doc.attrib['profile']
xdisplay = self.doc.find('display')
for xbuffer in xdisplay.iter('buffer'):
buffer = BufferDef(xbuffer)
self.buffers[buffer.id] = buffer
for xregion in xbuffer.iter('region'):
region = RegionDef(buffer, xregion)
self.regions[region.id] = region
self.regionIdList.append(region.id)
xfiles = self.doc.find('files')
for xfileset in xfiles.iter('fileset'):
regionName = xfileset.attrib['region']
region = self.regions[regionName]
region.addFileset(xfileset)
def extractSubfile(self, filename):
""" Returns the string data from the subfile within the mpcdi
file with the given name. """
if self.zip:
return self.zip.read(filename)
else:
return open(os.path.join(self.filename, filename), 'rb').read()
def extractPfmFile(self, filename):
""" Returns a PfmFile object corresponding to the named
file.pfm within the mpcdi file. """
data = self.extractSubfile(filename)
return PfmFile.PfmFile(filename = filename, data = data)
def extractTextureImage(self, filename):
""" Returns a TextureImage object corresponding to the named
file.png within the mpcdi file. """
data = self.extractSubfile(filename)
return TextureImage.TextureImage(filename = filename, data = data)
class BufferDef:
def __init__(self, xbuffer):
self.id = xbuffer.attrib['id']
# Integer properties
for keyword in ['Xresolution', 'Yresolution']:
value = xbuffer.attrib[keyword]
if value:
value = int(value)
setattr(self, keyword, value)
class RegionDef:
def __init__(self, buffer, xregion):
self.buffer = buffer
self.id = xregion.attrib['id']
self.Xresolution = None
self.Yresolution = None
# Integer properties
for keyword in ['Xresolution', 'Yresolution']:
value = xregion.attrib[keyword]
if value:
value = int(value)
setattr(self, keyword, value)
# Float properties
for keyword in ['x', 'y', 'xsize', 'ysize']:
value = xregion.attrib[keyword]
if value:
value = float(value)
setattr(self, keyword, value)
self.frustum = None
for xfrustum in xregion.iter('frustum'):
self.frustum = FrustumDef(xfrustum)
self.frame = None
xframe = xregion.find('coordinateFrame')
if xframe:
self.frame = FrameDef(xframe)
print "Found region %s of size %s, %s" % (self.id, self.Xresolution, self.Yresolution)
def addFileset(self, xfileset):
for keyword in ['geometryWarpFile', 'alphaMap', 'betaMap', 'distortionMap']:
xfile = xfileset.find(keyword, None)
if xfile is not None:
file = FileDef(xfile)
else:
file = None
setattr(self, keyword, file)
class FileDef:
def __init__(self, xfile):
self.path = xfile.find('path').text
# Integer properties
for keyword in ['componentDepth', 'bitdepth']:
xe = xfile.find(keyword)
if xe is not None:
value = int(xe.text)
else:
value = None
setattr(self, keyword, value)
# Float properties
for keyword in ['gammaEmbedded']:
xe = xfile.find(keyword)
if xe is not None:
value = float(xe.text)
else:
value = None
setattr(self, keyword, value)