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 8-bit color DDS images #7426

Merged
merged 3 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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/palette.dds
Binary file not shown.
5 changes: 5 additions & 0 deletions Tests/test_file_dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ def test_dxt5_colorblock_alpha_issue_4142():
assert px[2] != 0


def test_palette():
with Image.open("Tests/images/palette.dds") as im:
assert_image_equal_tofile(im, "Tests/images/transparent.gif")


def test_unimplemented_pixel_format():
with pytest.raises(NotImplementedError):
with Image.open("Tests/images/unimplemented_pixel_format.dds"):
Expand Down
3 changes: 2 additions & 1 deletion docs/handbook/image-file-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ DDS

DDS is a popular container texture format used in video games and natively supported
by DirectX. Uncompressed RGB and RGBA can be read, and (since 8.3.0) written. DXT1,
DXT3 (since 3.4.0) and DXT5 pixel formats can be read, only in ``RGBA`` mode.
DXT3 (since 3.4.0) and DXT5 pixel formats can be read, only in ``RGBA`` mode. Since
10.1.0, 8-bit color indexed files can be read as ``P`` mode images.
hugovk marked this conversation as resolved.
Show resolved Hide resolved

DIB
^^^
Expand Down
6 changes: 3 additions & 3 deletions docs/releasenotes/10.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TODO
Other Changes
=============

TODO
^^^^
Added support for DDS 8-bit color indexed images
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

TODO
Support has been added to read PALETTEINDEXED8 DDS files as P mode images.
6 changes: 5 additions & 1 deletion src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import struct
from io import BytesIO

from . import Image, ImageFile
from . import Image, ImageFile, ImagePalette
from ._binary import o32le as o32

# Magic ("DDS ")
Expand Down Expand Up @@ -157,6 +157,10 @@ def _open(self):
rawmode += masks[0xFF0000] + masks[0xFF00] + masks[0xFF]

self.tile = [("raw", (0, 0) + self.size, 0, (rawmode[::-1], 0, 1))]
elif pfflags & DDPF_PALETTEINDEXED8:
self._mode = "P"
self.palette = ImagePalette.raw("RGBA", self.fp.read(1024))
self.tile = [("raw", (0, 0) + self.size, 0, "L")]
else:
data_start = header_size + 4
n = 0
Expand Down