Spaces:
Runtime error
Runtime error
File size: 12,392 Bytes
d82cf6a |
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
"""Software decoder for S3TC compressed texture (i.e., DDS).
http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_compression_s3tc.txt
"""
import re
import ctypes
from pyglet.gl import *
from pyglet.gl import gl_info
from pyglet.image import AbstractImage, Texture
split_8byte = re.compile('.' * 8, flags=re.DOTALL)
split_16byte = re.compile('.' * 16, flags=re.DOTALL)
class PackedImageData(AbstractImage):
_current_texture = None
def __init__(self, width, height, fmt, packed_format, data):
super().__init__(width, height)
self.format = fmt
self.packed_format = packed_format
self.data = data
def unpack(self):
if self.packed_format == GL_UNSIGNED_SHORT_5_6_5:
# Unpack to GL_RGB. Assume self.data is already 16-bit
i = 0
out = (ctypes.c_ubyte * (self.width * self.height * 3))()
for c in self.data:
out[i + 2] = (c & 0x1f) << 3
out[i + 1] = (c & 0x7e0) >> 3
out[i] = (c & 0xf800) >> 8
i += 3
self.data = out
self.packed_format = GL_UNSIGNED_BYTE
def _get_texture(self):
if self._current_texture:
return self._current_texture
texture = Texture.create(self.width, self.height, GL_TEXTURE_2D, None)
glBindTexture(texture.target, texture.id)
glTexParameteri(texture.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
if not gl_info.have_version(1, 2) or True:
self.unpack()
glTexImage2D(texture.target, texture.level,
self.format, self.width, self.height, 0,
self.format, self.packed_format, self.data)
self._current_texture = texture
return texture
texture = property(_get_texture)
def get_texture(self, rectangle=False, force_rectangle=False):
"""The parameters 'rectangle' and 'force_rectangle' are ignored.
See the documentation of the method 'AbstractImage.get_texture' for
a more detailed documentation of the method. """
return self._get_texture()
def decode_dxt1_rgb(data, width, height):
# Decode to 16-bit RGB UNSIGNED_SHORT_5_6_5
out = (ctypes.c_uint16 * (width * height))()
# Read 8 bytes at a time
image_offset = 0
for c0_lo, c0_hi, c1_lo, c1_hi, b0, b1, b2, b3 in split_8byte.findall(data):
color0 = ord(c0_lo) | ord(c0_hi) << 8
color1 = ord(c1_lo) | ord(c1_hi) << 8
bits = ord(b0) | ord(b1) << 8 | ord(b2) << 16 | ord(b3) << 24
r0 = color0 & 0x1f
g0 = (color0 & 0x7e0) >> 5
b0 = (color0 & 0xf800) >> 11
r1 = color1 & 0x1f
g1 = (color1 & 0x7e0) >> 5
b1 = (color1 & 0xf800) >> 11
# i is the dest ptr for this block
i = image_offset
for y in range(4):
for x in range(4):
code = bits & 0x3
if code == 0:
out[i] = color0
elif code == 1:
out[i] = color1
elif code == 3 and color0 <= color1:
out[i] = 0
else:
if code == 2 and color0 > color1:
r = (2 * r0 + r1) // 3
g = (2 * g0 + g1) // 3
b = (2 * b0 + b1) // 3
elif code == 3 and color0 > color1:
r = (r0 + 2 * r1) // 3
g = (g0 + 2 * g1) // 3
b = (b0 + 2 * b1) // 3
else:
assert code == 2 and color0 <= color1
r = (r0 + r1) // 2
g = (g0 + g1) // 2
b = (b0 + b1) // 2
out[i] = r | g << 5 | b << 11
bits >>= 2
i += 1
i += width - 4
# Move dest ptr to next 4x4 block
advance_row = (image_offset + 4) % width == 0
image_offset += width * 3 * advance_row + 4
return PackedImageData(width, height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, out)
def decode_dxt1_rgba(data, width, height):
# Decode to GL_RGBA
out = (ctypes.c_ubyte * (width * height * 4))()
pitch = width << 2
# Read 8 bytes at a time
image_offset = 0
for c0_lo, c0_hi, c1_lo, c1_hi, b0, b1, b2, b3 in split_8byte.findall(data):
color0 = ord(c0_lo) | ord(c0_hi) << 8
color1 = ord(c1_lo) | ord(c1_hi) << 8
bits = ord(b0) | ord(b1) << 8 | ord(b2) << 16 | ord(b3) << 24
r0 = color0 & 0x1f
g0 = (color0 & 0x7e0) >> 5
b0 = (color0 & 0xf800) >> 11
r1 = color1 & 0x1f
g1 = (color1 & 0x7e0) >> 5
b1 = (color1 & 0xf800) >> 11
# i is the dest ptr for this block
i = image_offset
for y in range(4):
for x in range(4):
code = bits & 0x3
a = 255
if code == 0:
r, g, b = r0, g0, b0
elif code == 1:
r, g, b = r1, g1, b1
elif code == 3 and color0 <= color1:
r = g = b = a = 0
else:
if code == 2 and color0 > color1:
r = (2 * r0 + r1) // 3
g = (2 * g0 + g1) // 3
b = (2 * b0 + b1) // 3
elif code == 3 and color0 > color1:
r = (r0 + 2 * r1) // 3
g = (g0 + 2 * g1) // 3
b = (b0 + 2 * b1) // 3
else:
assert code == 2 and color0 <= color1
r = (r0 + r1) // 2
g = (g0 + g1) // 2
b = (b0 + b1) // 2
out[i] = b << 3
out[i + 1] = g << 2
out[i + 2] = r << 3
out[i + 3] = a << 4
bits >>= 2
i += 4
i += pitch - 16
# Move dest ptr to next 4x4 block
advance_row = (image_offset + 16) % pitch == 0
image_offset += pitch * 3 * advance_row + 16
return PackedImageData(width, height, GL_RGBA, GL_UNSIGNED_BYTE, out)
def decode_dxt3(data, width, height):
# Decode to GL_RGBA
out = (ctypes.c_ubyte * (width * height * 4))()
pitch = width << 2
# Read 16 bytes at a time
image_offset = 0
for (a0, a1, a2, a3, a4, a5, a6, a7,
c0_lo, c0_hi, c1_lo, c1_hi,
b0, b1, b2, b3) in split_16byte.findall(data):
color0 = ord(c0_lo) | ord(c0_hi) << 8
color1 = ord(c1_lo) | ord(c1_hi) << 8
bits = ord(b0) | ord(b1) << 8 | ord(b2) << 16 | ord(b3) << 24
alpha = ord(a0) | ord(a1) << 8 | ord(a2) << 16 | ord(a3) << 24 | \
ord(a4) << 32 | ord(a5) << 40 | ord(a6) << 48 | ord(a7) << 56
r0 = color0 & 0x1f
g0 = (color0 & 0x7e0) >> 5
b0 = (color0 & 0xf800) >> 11
r1 = color1 & 0x1f
g1 = (color1 & 0x7e0) >> 5
b1 = (color1 & 0xf800) >> 11
# i is the dest ptr for this block
i = image_offset
for y in range(4):
for x in range(4):
code = bits & 0x3
a = alpha & 0xf
if code == 0:
r, g, b = r0, g0, b0
elif code == 1:
r, g, b = r1, g1, b1
elif code == 3 and color0 <= color1:
r = g = b = 0
else:
if code == 2 and color0 > color1:
r = (2 * r0 + r1) // 3
g = (2 * g0 + g1) // 3
b = (2 * b0 + b1) // 3
elif code == 3 and color0 > color1:
r = (r0 + 2 * r1) // 3
g = (g0 + 2 * g1) // 3
b = (b0 + 2 * b1) // 3
else:
assert code == 2 and color0 <= color1
r = (r0 + r1) // 2
g = (g0 + g1) // 2
b = (b0 + b1) // 2
out[i] = b << 3
out[i + 1] = g << 2
out[i + 2] = r << 3
out[i + 3] = a << 4
bits >>= 2
alpha >>= 4
i += 4
i += pitch - 16
# Move dest ptr to next 4x4 block
advance_row = (image_offset + 16) % pitch == 0
image_offset += pitch * 3 * advance_row + 16
return PackedImageData(width, height, GL_RGBA, GL_UNSIGNED_BYTE, out)
def decode_dxt5(data, width, height):
# Decode to GL_RGBA
out = (ctypes.c_ubyte * (width * height * 4))()
pitch = width << 2
# Read 16 bytes at a time
image_offset = 0
for (alpha0, alpha1, ab0, ab1, ab2, ab3, ab4, ab5,
c0_lo, c0_hi, c1_lo, c1_hi,
b0, b1, b2, b3) in split_16byte.findall(data):
color0 = ord(c0_lo) | ord(c0_hi) << 8
color1 = ord(c1_lo) | ord(c1_hi) << 8
alpha0 = ord(alpha0)
alpha1 = ord(alpha1)
bits = ord(b0) | ord(b1) << 8 | ord(b2) << 16 | ord(b3) << 24
abits = ord(ab0) | ord(ab1) << 8 | ord(ab2) << 16 | ord(ab3) << 24 | \
ord(ab4) << 32 | ord(ab5) << 40
r0 = color0 & 0x1f
g0 = (color0 & 0x7e0) >> 5
b0 = (color0 & 0xf800) >> 11
r1 = color1 & 0x1f
g1 = (color1 & 0x7e0) >> 5
b1 = (color1 & 0xf800) >> 11
# i is the dest ptr for this block
i = image_offset
for y in range(4):
for x in range(4):
code = bits & 0x3
acode = abits & 0x7
if code == 0:
r, g, b = r0, g0, b0
elif code == 1:
r, g, b = r1, g1, b1
elif code == 3 and color0 <= color1:
r = g = b = 0
else:
if code == 2 and color0 > color1:
r = (2 * r0 + r1) // 3
g = (2 * g0 + g1) // 3
b = (2 * b0 + b1) // 3
elif code == 3 and color0 > color1:
r = (r0 + 2 * r1) // 3
g = (g0 + 2 * g1) // 3
b = (b0 + 2 * b1) // 3
else:
assert code == 2 and color0 <= color1
r = (r0 + r1) / 2
g = (g0 + g1) / 2
b = (b0 + b1) / 2
if acode == 0:
a = alpha0
elif acode == 1:
a = alpha1
elif alpha0 > alpha1:
if acode == 2:
a = (6 * alpha0 + 1 * alpha1) // 7
elif acode == 3:
a = (5 * alpha0 + 2 * alpha1) // 7
elif acode == 4:
a = (4 * alpha0 + 3 * alpha1) // 7
elif acode == 5:
a = (3 * alpha0 + 4 * alpha1) // 7
elif acode == 6:
a = (2 * alpha0 + 5 * alpha1) // 7
else:
assert acode == 7
a = (1 * alpha0 + 6 * alpha1) // 7
else:
if acode == 2:
a = (4 * alpha0 + 1 * alpha1) // 5
elif acode == 3:
a = (3 * alpha0 + 2 * alpha1) // 5
elif acode == 4:
a = (2 * alpha0 + 3 * alpha1) // 5
elif acode == 5:
a = (1 * alpha0 + 4 * alpha1) // 5
elif acode == 6:
a = 0
else:
assert acode == 7
a = 255
out[i] = b << 3
out[i + 1] = g << 2
out[i + 2] = r << 3
out[i + 3] = a
bits >>= 2
abits >>= 3
i += 4
i += pitch - 16
# Move dest ptr to next 4x4 block
advance_row = (image_offset + 16) % pitch == 0
image_offset += pitch * 3 * advance_row + 16
return PackedImageData(width, height, GL_RGBA, GL_UNSIGNED_BYTE, out)
|