-
-
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
Added reading DDS BGR;15 images #7574
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -137,7 +137,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 +147,38 @@ 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] | ||||||||
else: | ||||||||
if not pfflags & DDPF_ALPHAPIXELS: | ||||||||
self._mode = "RGB" | ||||||||
rawmode += masks[0xFF0000] + masks[0xFF00] + masks[0xFF] | ||||||||
|
||||||||
self.tile = [("raw", (0, 0) + self.size, 0, (rawmode[::-1], 0, 1))] | ||||||||
# Each channel is derived using a mask of the pixel values | ||||||||
masks = { | ||||||||
mask: "RGB"[i] | ||||||||
for i, mask in enumerate(struct.unpack("<3I", header.read(12))) | ||||||||
} | ||||||||
if self.mode == "RGB" and all( | ||||||||
mask in (0b0000000000011111, 0b0000001111100000, 0b0111110000000000) | ||||||||
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. This did become less clear to read, you're right. Wonder if these and the
|
||||||||
for mask in masks | ||||||||
): | ||||||||
# Each mask uses 5 bits | ||||||||
rawmode = ( | ||||||||
masks[0b0000000000011111] | ||||||||
+ masks[0b0000001111100000] | ||||||||
+ masks[0b0111110000000000] | ||||||||
+ ";15" | ||||||||
) | ||||||||
Comment on lines
+163
to
+168
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. Will this now support esoteric formats like EDIT: right, the whole point here was BGR;15 – my bad, too little coffee. 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. This will support RGB;15 and BGR;15 Pillow/src/libImaging/Unpack.c Lines 1576 to 1577 in 697c24b
|
||||||||
else: | ||||||||
# Try 8 bits for each mask | ||||||||
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. What's the trying here? Rather, what happens if trying fails? 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. Given that the issue related to this issue has found 5 bit masks, it seems possible that there could be other mask values used in files, beyond 5 bit and 8 bits. If it fails, an error is raised, and ultimately an 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.
Suggested change
|
||||||||
if self.mode == "RGBA": | ||||||||
# The fourth mask, alpha, | ||||||||
# is only valid when the correct flags are present | ||||||||
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. What are the correct flags? 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. https://learn.microsoft.com/en-us/windows/win32/direct3ddds/dds-pixelformat seems inconsistent on this to me
The code is checking for Pillow/src/PIL/DdsImagePlugin.py Line 150 in aa03c5a
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.
Suggested change
|
||||||||
a_mask = struct.unpack("<I", header.read(4))[0] | ||||||||
masks[a_mask] = "A" | ||||||||
|
||||||||
rawmode = masks[0xFF] + masks[0xFF00] + masks[0xFF0000] | ||||||||
if 0xFF000000 in masks: | ||||||||
rawmode += masks[0xFF000000] | ||||||||
|
||||||||
self.tile = [("raw", (0, 0) + self.size, 0, (rawmode, 0, 1))] | ||||||||
elif pfflags & DDPF_PALETTEINDEXED8: | ||||||||
self._mode = "P" | ||||||||
self.palette = ImagePalette.raw("RGBA", self.fp.read(1024)) | ||||||||
|
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.
The previous version always read 16 bytes for masks – this will now read either 12 or 16 bytes. That seems odd?
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.
It isn't always necessary to read the last 4 bytes, and therefore seems more efficient to not do so where possible.
If you are concerned about the file pointer position, that's not relevant, as it is just reading from
Pillow/src/PIL/DdsImagePlugin.py
Line 127 in 697c24b
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 think it would actually be more efficient and clearer to not do another read-and-struct-unpack? (Would also mean that the entire
DDS_PIXELFORMAT
is always consumed.)