-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support arbitrary masks for uncompressed RGB DDS images #7589
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f1fef09
Support arbitrary masks for uncompressed RGB images
radarhere 30eb414
Use f-string
radarhere 2e8dd3b
Use int.from_bytes()
radarhere 8b44116
Merge branch 'main' into dds_rgb
radarhere 232094e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
from io import BytesIO | ||
|
||
from . import Image, ImageFile, ImagePalette | ||
from ._binary import o8 | ||
from ._binary import o32le as o32 | ||
|
||
# Magic ("DDS ") | ||
|
@@ -137,7 +138,6 @@ def _open(self): | |
pfsize, pfflags = struct.unpack("<2I", header.read(8)) | ||
fourcc = header.read(4) | ||
(bitcount,) = struct.unpack("<I", header.read(4)) | ||
masks = struct.unpack("<4I", header.read(16)) | ||
if pfflags & DDPF_LUMINANCE: | ||
# Texture contains uncompressed L or LA data | ||
if pfflags & DDPF_ALPHAPIXELS: | ||
|
@@ -148,15 +148,14 @@ def _open(self): | |
self.tile = [("raw", (0, 0) + self.size, 0, (self.mode, 0, 1))] | ||
elif pfflags & DDPF_RGB: | ||
# Texture contains uncompressed RGB data | ||
masks = {mask: ["R", "G", "B", "A"][i] for i, mask in enumerate(masks)} | ||
rawmode = "" | ||
if pfflags & DDPF_ALPHAPIXELS: | ||
rawmode += masks[0xFF000000] | ||
mask_count = 4 | ||
else: | ||
self._mode = "RGB" | ||
rawmode += masks[0xFF0000] + masks[0xFF00] + masks[0xFF] | ||
mask_count = 3 | ||
|
||
self.tile = [("raw", (0, 0) + self.size, 0, (rawmode[::-1], 0, 1))] | ||
masks = struct.unpack(f"<{mask_count}I", header.read(mask_count * 4)) | ||
self.tile = [("dds_rgb", (0, 0) + self.size, 0, (bitcount, masks))] | ||
elif pfflags & DDPF_PALETTEINDEXED8: | ||
self._mode = "P" | ||
self.palette = ImagePalette.raw("RGBA", self.fp.read(1024)) | ||
|
@@ -237,6 +236,40 @@ def load_seek(self, pos): | |
pass | ||
|
||
|
||
class DdsRgbDecoder(ImageFile.PyDecoder): | ||
_pulls_fd = True | ||
|
||
def decode(self, buffer): | ||
bitcount, masks = self.args | ||
|
||
# Some masks will be padded with zeros, e.g. R 0b11 G 0b1100 | ||
# Calculate how many zeros each mask is padded with | ||
mask_offsets = [] | ||
# And the maximum value of each channel without the padding | ||
mask_totals = [] | ||
for mask in masks: | ||
offset = 0 | ||
if mask != 0: | ||
while mask >> (offset + 1) << (offset + 1) == mask: | ||
offset += 1 | ||
mask_offsets.append(offset) | ||
mask_totals.append(mask >> offset) | ||
|
||
data = bytearray() | ||
bytecount = bitcount // 8 | ||
while len(data) < self.state.xsize * self.state.ysize * len(masks): | ||
value = self.fd.read(bytecount) | ||
int_value = sum(value[i] << i * 8 for i in range(bytecount)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I've pushed a commit. |
||
for i, mask in enumerate(masks): | ||
masked_value = int_value & mask | ||
# Remove the zero padding, and scale it to 8 bits | ||
data += o8( | ||
int(((masked_value >> mask_offsets[i]) / mask_totals[i]) * 255) | ||
) | ||
self.set_as_raw(bytes(data)) | ||
return -1, 0 | ||
|
||
|
||
def _save(im, fp, filename): | ||
if im.mode not in ("RGB", "RGBA", "L", "LA"): | ||
msg = f"cannot write mode {im.mode} as DDS" | ||
|
@@ -291,5 +324,6 @@ def _accept(prefix): | |
|
||
|
||
Image.register_open(DdsImageFile.format, DdsImageFile, _accept) | ||
Image.register_decoder("dds_rgb", DdsRgbDecoder) | ||
Image.register_save(DdsImageFile.format, _save) | ||
Image.register_extension(DdsImageFile.format, ".dds") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth it to precalculate the
total
here to avoid that extra multiplication every loop (since we know the value can't possibly change):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally think the current version is simpler to read, and expect the performance difference to be negligible.
The current version is also used in a few other places in Pillow - but at the same time, that's probably because I wrote them.
Pillow/src/PIL/QoiImagePlugin.py
Line 55 in 316f397