Skip to content

Commit

Permalink
Update Texture2D to use new Pillow DDS API
Browse files Browse the repository at this point in the history
  • Loading branch information
jleclanche committed Oct 7, 2016
1 parent d1dde1f commit ad149d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -72,7 +72,7 @@ By default, it will extract all known extractable types:
versions pack these as FSB files, so [python-fsb5](https://github.com/hearthsim/python-fsb5)
is required to convert them back.
* `Texture2D` objects will be converted to png files. Not all Texture2D formats are supported.
A recent version of [Pillow](https://github.com/python-pillow/Pillow) is required for this.
[Pillow](https://github.com/python-pillow/Pillow) version >= 3.4 is required for this.
* `Mesh` objects (3D objects) will be pickled. Pull requests implementing a .obj converter are
welcome and wanted.
* `TextAsset` objects will be extracted as plain text, to .txt files
Expand Down
37 changes: 22 additions & 15 deletions unitypack/engine/texture.py
@@ -1,5 +1,4 @@
from enum import IntEnum
from io import BytesIO
from .object import Object, field


Expand Down Expand Up @@ -126,24 +125,23 @@ class Texture2D(Texture):
texture_dimension = field("m_TextureDimension")
mipmap = field("m_MipMap")
complete_image_size = field("m_CompleteImageSize")
stream_data = field("m_StreamData")

def __repr__(self):
return "<%s %s (%s %ix%i)>" % (
self.__class__.__name__, self.name, self.format.name, self.width, self.height
)

@property
def decoded_data(self):
from PIL.DdsImagePlugin import _dxt1, _dxt5
def image_data(self):
if self.stream_data:
path = self.stream_data.get("path")
if path:
offset = self.stream_data["offset"]
size = self.stream_data["size"]
return self.asset.environment.get_stream(path, offset, size)

if self.format == TextureFormat.DXT1:
codec = _dxt1
elif self.format == TextureFormat.DXT5:
codec = _dxt5
else:
return self.data

return codec(BytesIO(self.data), self.width, self.height)
return self.data

@property
def image(self):
Expand All @@ -152,12 +150,21 @@ def image(self):
if self.format not in IMPLEMENTED_FORMATS:
raise NotImplementedError("Unimplemented format %r" % (self.format))

if self.format == TextureFormat.DXT1:
codec = "bcn"
args = (1, )
elif self.format == TextureFormat.DXT5:
codec = "bcn"
args = (3, )
else:
codec = "raw"
args = (self.format.pixel_format, )

mode = "RGB" if self.format.pixel_format == "RGB" else "RGBA"
size = (self.width, self.height)
raw_mode = self.format.pixel_format
mode = "RGB" if raw_mode == "RGB" else "RGBA"
data = bytes(self.decoded_data)
data = bytes(self.image_data)

if not data and size == (0, 0):
return None

return Image.frombytes(mode, size, data, "raw", raw_mode)
return Image.frombytes(mode, size, data, codec, args)

0 comments on commit ad149d4

Please sign in to comment.