Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Tests/images/bgr15.dds
Binary file not shown.
Binary file added Tests/images/bgr15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Tests/test_file_dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
TEST_FILE_UNCOMPRESSED_L = "Tests/images/uncompressed_l.dds"
TEST_FILE_UNCOMPRESSED_L_WITH_ALPHA = "Tests/images/uncompressed_la.dds"
TEST_FILE_UNCOMPRESSED_RGB = "Tests/images/hopper.dds"
TEST_FILE_UNCOMPRESSED_BGR15 = "Tests/images/bgr15.dds"
TEST_FILE_UNCOMPRESSED_RGB_WITH_ALPHA = "Tests/images/uncompressed_rgb.dds"


Expand Down Expand Up @@ -211,6 +212,7 @@ def test_unimplemented_dxgi_format():
("L", (128, 128), TEST_FILE_UNCOMPRESSED_L),
("LA", (128, 128), TEST_FILE_UNCOMPRESSED_L_WITH_ALPHA),
("RGB", (128, 128), TEST_FILE_UNCOMPRESSED_RGB),
("RGB", (128, 128), TEST_FILE_UNCOMPRESSED_BGR15),
("RGBA", (800, 600), TEST_FILE_UNCOMPRESSED_RGB_WITH_ALPHA),
],
)
Expand Down
16 changes: 9 additions & 7 deletions src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,17 @@ 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]
if masks[:3] == (31744, 992, 31) and self.mode == "RGB":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic numbers here could use some explanation if you ask me.

Copy link
Member Author

@radarhere radarhere Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what you're said sounds like a perfectly straightforward request, but I can't think of a short way to explain it that isn't just explaining what masks are, or what BGR;15 is.

There are 4 bit masks - https://learn.microsoft.com/en-us/windows/win32/direct3ddds/dds-pixelformat - R, G, B and A.

Each mask filters the pixel values to transform them into the value for each channel.

These masks are filtering 5 bits for R, 5 bits for G and 5 bits for B - BGR;15.

31744 = 0b0111110000000000
  992 = 0b0000001111100000
   31 = 0b0000000000011111

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks like great material for a code comment right there :)

You could also replace the decimal constants with those binary constants – then it's immediately more clear for a reader.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearer, yes, it just feels verbose... I've pushed a commit.

rawmode = "BGR;15"
else:
masks = {mask: ["R", "G", "B", "A"][i] for i, mask in enumerate(masks)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
masks = {mask: ["R", "G", "B", "A"][i] for i, mask in enumerate(masks)}
masks = {mask: "RGBA"[i] for i, mask in enumerate(masks)}

would be more efficient, since the (possibly-mutable-but-the-interpreter-can't-tell) list doesn't need to regenerated for every call of this function, but it could use the same "RGBA" string constant used just below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a version of this.

rawmode = masks[0xFF] + masks[0xFF00] + masks[0xFF0000]
if self.mode == "RGBA":
rawmode += masks[0xFF000000]

self.tile = [("raw", (0, 0) + self.size, 0, (rawmode[::-1], 0, 1))]
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))
Expand Down