28 |
|
|
29 |
# Usage: something like: |
# Usage: something like: |
30 |
# |
# |
31 |
# make runjython DBG="gfx/util/mipzipmaker.py ../tmpimg/*-170-*[0-9]" |
# make runjython DBG="vob/putil/mipzipmaker.py ../tmpimg/*-170-*[0-9]" |
32 |
|
|
33 |
# Can also be used as a python module - fenfire does this automatically |
# Can also be used as a python module - fenfire does this automatically |
34 |
|
|
35 |
|
from __future__ import nested_scopes |
36 |
|
|
37 |
import sys |
import sys |
38 |
import getopt |
import getopt |
39 |
from java.lang import Runnable, System |
from java.lang import Runnable, System |
58 |
n /= 4 |
n /= 4 |
59 |
return 4 * n |
return 4 * n |
60 |
|
|
61 |
format = 0 |
defaultTexFormat = 0 |
62 |
|
|
63 |
|
|
64 |
def _init(): |
def _init(): |
65 |
global format, suffix |
global defaultTexFormat, suffix |
66 |
if format: |
if defaultTexFormat: |
67 |
return |
return |
68 |
|
|
69 |
if GL.workaroundStupidBuggyAtiDrivers: |
if GL.workaroundStupidBuggyAtiDrivers: |
70 |
format = "RGB" |
defaultTexFormat = "RGB" |
71 |
suffix = ".mipzipBLAH" |
suffix = ".mipzipBLAH" |
72 |
else: |
else: |
73 |
format = "COMPRESSED_RGB_S3TC_DXT1_EXT" |
defaultTexFormat = "COMPRESSED_RGB_S3TC_DXT1_EXT" |
74 |
suffix = ".mipzip" |
suffix = ".mipzip" |
75 |
|
|
76 |
def _clipmax(x, max): |
def _clipmax(x, max): |
78 |
if max > x: return x |
if max > x: return x |
79 |
return max |
return max |
80 |
|
|
81 |
def makeMipzip(image, mipzip, maxwidth=-1, maxheight=-1): |
def bytesPerTexel(format, dataType): |
82 |
|
return vob.gl.GLUtil.findBpt(format, dataType) |
83 |
|
|
84 |
|
def makeMipzip(image, mipzip, maxwidth=-1, maxheight=-1, |
85 |
|
texformat = None, |
86 |
|
internalTexFormat = None, |
87 |
|
uncompressedTexType = None |
88 |
|
): |
89 |
"""Convert the given image file into a mipzip file. |
"""Convert the given image file into a mipzip file. |
90 |
|
|
91 |
image -- the image file name |
image -- the image file name |
92 |
mipzip -- the mipzip file name |
mipzip -- the mipzip file name |
93 |
maxwidth -- if image is wider than maxwidth, cut off edge |
maxwidth -- if image is wider than maxwidth, cut off edge |
94 |
maxheight -- if image is taller than maxheight, cut off edge |
maxheight -- if image is taller than maxheight, cut off edge |
95 |
|
texformat -- The texture format to use |
96 |
|
internalTexFormat -- The internal texture format to represent the data in |
97 |
|
for compressed textures, same as texformat |
98 |
|
uncompressedTexType -- The datatype (relevant only for uncompressed |
99 |
|
textures) |
100 |
""" |
""" |
101 |
|
|
102 |
|
if texformat == None: |
103 |
|
if not defaultTexFormat: |
104 |
|
_init() |
105 |
|
texformat = defaultTexFormat |
106 |
|
|
107 |
|
print "TEXFORMAT: ",texformat, defaultTexFormat |
108 |
|
isCompressed = (java.lang.String(texformat).indexOf("COMPRESS") >= 0) |
109 |
|
|
110 |
|
if internalTexFormat == None: |
111 |
|
internalTexFormat = texformat |
112 |
|
|
113 |
|
if not isCompressed: |
114 |
|
if uncompressedTexType == None: |
115 |
|
uncompressedTexType = "UNSIGNED_BYTE" |
116 |
|
|
117 |
_init() |
_init() |
118 |
GL.freeQueue() |
GL.freeQueue() |
119 |
|
|
129 |
w = roundup2(d.width) |
w = roundup2(d.width) |
130 |
h = roundup2(d.height) |
h = roundup2(d.height) |
131 |
tex = GL.createTexture() |
tex = GL.createTexture() |
132 |
tex.loadNull2D('TEXTURE_2D',0, format, w, h, 0, "RGB", "BYTE") |
tex.loadNull2D('TEXTURE_2D',0, internalTexFormat, w, h, 0, "RGB", "BYTE") |
133 |
print "WH: ",w, h |
print "WH: ",w, h |
134 |
tex.loadSubImage(0, im, 0, 0, 0, 0, chomp4(d.width), chomp4(d.height)) |
tex.loadSubImage(0, im, 0, 0, 0, 0, chomp4(d.width), chomp4(d.height)) |
135 |
|
|
137 |
|
|
138 |
out = zip.ZipOutputStream(java.io.FileOutputStream(mipzip)) |
out = zip.ZipOutputStream(java.io.FileOutputStream(mipzip)) |
139 |
|
|
140 |
entry = zip.ZipEntry("texformat") |
def metaEntry(name, comment): |
141 |
entry.setComment(format) |
entry = zip.ZipEntry(name) |
142 |
entry.setSize(0) |
entry.setComment(comment) |
143 |
out.putNextEntry(entry) |
entry.setSize(0) |
144 |
out.closeEntry() |
out.putNextEntry(entry) |
145 |
|
out.closeEntry() |
146 |
|
|
147 |
|
metaEntry("texformat", texformat) |
148 |
|
|
149 |
entry = zip.ZipEntry("origsize") |
if not isCompressed: |
150 |
entry.setComment("%sx%s" % (d.width / float(w), |
metaEntry("internaltexformat", internalTexFormat) |
151 |
|
metaEntry("datatype", uncompressedTexType) |
152 |
|
|
153 |
|
metaEntry("origsize", "%sx%s" % (d.width / float(w), |
154 |
d.height/ float(h))) |
d.height/ float(h))) |
|
entry.setSize(0) |
|
|
out.putNextEntry(entry) |
|
|
out.closeEntry() |
|
155 |
|
|
156 |
l = 0 |
l = 0 |
157 |
while 1: |
while 1: |
158 |
w = int(tex.getLevelParameter(l, "TEXTURE_WIDTH")[0]) |
w = int(tex.getLevelParameter(l, "TEXTURE_WIDTH")[0]) |
159 |
h = int(tex.getLevelParameter(l, "TEXTURE_HEIGHT")[0]) |
h = int(tex.getLevelParameter(l, "TEXTURE_HEIGHT")[0]) |
160 |
print "WH: ", w, h |
print "WH: ", w, h |
161 |
if GL.workaroundStupidBuggyAtiDrivers: |
|
162 |
bytes = jarray.zeros(4 * w * h, "b") |
if isCompressed: |
|
tex.getTexImage(l, "RGB", "BYTE", bytes) |
|
|
else: |
|
163 |
bytes = tex.getCompressedTexImage(l) |
bytes = tex.getCompressedTexImage(l) |
164 |
|
else: |
165 |
|
bpt = bytesPerTexel(texformat, uncompressedTexType) |
166 |
|
|
167 |
|
bytes = jarray.zeros(bpt * w * h, "b") |
168 |
|
tex.getTexImage(l, texformat, uncompressedTexType, bytes) |
169 |
|
|
170 |
print "Bytes: ",l, len(bytes) |
print "Bytes: ",l, len(bytes) |
171 |
entry = zip.ZipEntry(str(l)) |
entry = zip.ZipEntry(str(l)) |
172 |
entry.setComment("%sx%s" % (int(w),int(h))) |
entry.setComment("%sx%s" % (int(w),int(h))) |
183 |
|
|
184 |
|
|
185 |
class Main(Runnable): |
class Main(Runnable): |
186 |
def __init__(self, texfiles): |
def __init__(self, texfiles, **mzparms): |
187 |
self.texfiles = texfiles |
self.texfiles = texfiles |
188 |
|
self.mzparms = mzparms |
189 |
|
_init() |
190 |
def run(self): |
def run(self): |
191 |
w = gfxapi.createWindow() |
w = gfxapi.createWindow() |
192 |
for texfile in self.texfiles: |
for texfile in self.texfiles: |
195 |
|
|
196 |
def handle(self, texfile): |
def handle(self, texfile): |
197 |
of = texfile + suffix |
of = texfile + suffix |
198 |
makeMipzip(texfile, of) |
makeMipzip(texfile, of, **self.mzparms) |
199 |
|
|
200 |
if __name__ == '__main__': |
if __name__ == '__main__': |
201 |
|
|
202 |
opts, args = getopt.getopt(sys.argv[1:], |
opts, args = getopt.getopt(sys.argv[1:], |
203 |
vob.util.dbg.short, |
vob.putil.dbg.short, |
204 |
vob.util.dbg.long) |
["texFormat=", "internalTexFormat=", "datatype="] |
205 |
|
+ vob.putil.dbg.long) |
206 |
|
texFormat = None |
207 |
|
internalTexFormat = None |
208 |
|
datatype = None |
209 |
for o,a in opts: |
for o,a in opts: |
210 |
print "Opt: ",o,a |
print "Opt: ",o,a |
211 |
if o in vob.util.dbg.all: |
if o in vob.putil.dbg.all: |
212 |
vob.util.dbg.option(o,a) |
vob.putil.dbg.option(o,a) |
213 |
|
if o == "--texFormat": |
214 |
|
texFormat = a |
215 |
|
if o == "--internalTexFormat": |
216 |
|
internalTexFormat = a |
217 |
|
if o == "--datatype": |
218 |
|
datatype = a |
219 |
|
|
220 |
|
|
221 |
gfxapi = vob.GraphicsAPI.getInstance() |
gfxapi = vob.GraphicsAPI.getInstance() |
222 |
gfxapi.startUpdateManager(Main(args)) |
gfxapi.startUpdateManager(Main(args, |
223 |
|
texformat=texFormat, |
224 |
|
internalTexFormat=internalTexFormat, |
225 |
|
uncompressedTexType=datatype |
226 |
|
)) |
227 |
|
|