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

Fixed big-endian bug #3

Merged
merged 5 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 15 additions & 16 deletions Tests/test_file_dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@ def test_save_unsupported_mode(tmp_path):
im.save(out)


@pytest.mark.parametrize(
("mode", "test_file"),
(
("L", "Tests/images/l.dds"),
("LA", "Tests/images/la.dds"),
("RGB", "Tests/images/rgb.dds"),
("RGBA", "Tests/images/rgba.dds"),
),
)
def test_open(mode, test_file):
with Image.open(test_file) as im:
assert im.mode == mode
assert_image_equal_tofile(im, test_file.replace(".dds", ".png"))


@pytest.mark.parametrize(
("mode", "test_file"),
[
Expand All @@ -303,19 +318,3 @@ def test_save(mode, test_file, tmp_path):

with Image.open(out) as reloaded:
assert_image_equal(im, reloaded)


@pytest.mark.parametrize(
("mode", "expected_file", "input_file"),
[
("L", "Tests/images/l.png", "Tests/images/l.dds"),
("LA", "Tests/images/la.png", "Tests/images/la.dds"),
("RGB", "Tests/images/rgb.png", "Tests/images/rgb.dds"),
("RGBA", "Tests/images/rgba.png", "Tests/images/rgba.dds"),
],
)
def test_open(mode, expected_file, input_file):
with Image.open(input_file) as im:
assert im.mode == mode
with Image.open(expected_file) as im2:
assert_image_equal(im, im2)
69 changes: 34 additions & 35 deletions src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import io
import struct
from enum import IntEnum, IntFlag
from io import BytesIO

from . import Image, ImageFile
from ._binary import i32le as i32
from ._binary import o32le as o32

# Magic ("DDS ")
Expand Down Expand Up @@ -191,10 +191,6 @@ def _missing_(cls, value: object):
return cls.INVALID


def make_fourcc(name):
return struct.unpack("I", name.encode("ascii"))[0]


class D3DFMT(IntEnum):
UNKNOWN = 0
R8G8B8 = 20
Expand Down Expand Up @@ -252,20 +248,20 @@ class D3DFMT(IntEnum):
A2B10G10R10_XR_BIAS = 119
BINARYBUFFER = 199

UYVY = make_fourcc("UYVY")
R8G8_B8G8 = make_fourcc("RGBG")
YUY2 = make_fourcc("YUY2")
G8R8_G8B8 = make_fourcc("GRGB")
DXT1 = make_fourcc("DXT1")
DXT2 = make_fourcc("DXT2")
DXT3 = make_fourcc("DXT3")
DXT4 = make_fourcc("DXT4")
DXT5 = make_fourcc("DXT5")
DX10 = make_fourcc("DX10")
BC5S = make_fourcc("BC5S")
ATI1 = make_fourcc("ATI1")
ATI2 = make_fourcc("ATI2")
MULTI2_ARGB8 = make_fourcc("MET1")
UYVY = i32(b"UYVY")
R8G8_B8G8 = i32(b"RGBG")
YUY2 = i32(b"YUY2")
G8R8_G8B8 = i32(b"GRGB")
DXT1 = i32(b"DXT1")
DXT2 = i32(b"DXT2")
DXT3 = i32(b"DXT3")
DXT4 = i32(b"DXT4")
DXT5 = i32(b"DXT5")
DX10 = i32(b"DX10")
BC5S = i32(b"BC5S")
ATI1 = i32(b"ATI1")
ATI2 = i32(b"ATI2")
MULTI2_ARGB8 = i32(b"MET1")
INVALID = -1

@classmethod
Expand All @@ -277,7 +273,6 @@ class DdsImageFile(ImageFile.ImageFile):
format = "DDS"
format_description = "DirectDraw Surface"

# fmt: off
def _open(self):
if not _accept(self.fp.read(4)):
raise SyntaxError("not a DDS file")
Expand All @@ -287,7 +282,7 @@ def _open(self):
header_bytes = self.fp.read(header_size - 4)
if len(header_bytes) != 120:
raise OSError(f"Incomplete header: {len(header_bytes)} bytes")
header = BytesIO(header_bytes)
header = io.BytesIO(header_bytes)

flags_, height, width = struct.unpack("<3I", header.read(12))
flags = DDSD(flags_)
Expand All @@ -313,10 +308,12 @@ def _open(self):
self.tile = [("raw", extents, 0, (rawmode[::-1], 0, 1))]
elif bitcount == 32 and pfflags & DDPF.ALPHAPIXELS:
self.mode = "RGBA"
rawmode = (masks[0xFF000000] +
masks[0x00FF0000] +
masks[0x0000FF00] +
masks[0x000000FF])
rawmode = (
masks[0xFF000000]
+ masks[0x00FF0000]
+ masks[0x0000FF00]
+ masks[0x000000FF]
)
self.tile = [("raw", extents, 0, (rawmode[::-1], 0, 1))]
else:
raise OSError(f"Unsupported bitcount {bitcount} for {pfflags}")
Expand Down Expand Up @@ -398,15 +395,12 @@ def _open(self):
else:
raise NotImplementedError(f"Unknown pixel format flags {repr(pfflags)}")

# fmt: on

def load_seek(self, pos):
pass


# fmt: off
def _save(im, fp, filename):
if im.mode not in ("RGB", "RGBA", "L", 'LA'):
if im.mode not in ("RGB", "RGBA", "L", "LA"):
raise OSError(f"cannot write mode {im.mode} as DDS")

pixel_flags = DDPF.RGB
Expand All @@ -419,7 +413,7 @@ def _save(im, fp, filename):
bit_count = 32
r, g, b, a = im.split()
im = Image.merge("RGBA", (a, r, g, b))
elif im.mode == 'LA':
elif im.mode == "LA":
pixel_flags = DDPF.LUMINANCE | DDPF.ALPHAPIXELS
rgba_mask = struct.pack("<4I", 0x000000FF, 0x000000FF, 0x000000FF, 0x0000FF00)
bit_count = 16
Expand All @@ -433,8 +427,16 @@ def _save(im, fp, filename):
stride = (im.width * bit_count + 7) // 8
fp.write(
o32(DDS_MAGIC)
# header size, flags, height, width, pith, depth, mipmaps
+ struct.pack("<IIIIIII", 124, flags, im.height, im.width, stride, 0, 0, )
+ struct.pack(
"<IIIIIII",
124, # header size
flags, # flags
im.height,
im.width,
stride, # pitch
0, # depth
0, # mipmaps
)
+ struct.pack("11I", *((0,) * 11)) # reserved
# pfsize, pfflags, fourcc, bitcount
+ struct.pack("<IIII", 32, pixel_flags, 0, bit_count)
Expand All @@ -445,9 +447,6 @@ def _save(im, fp, filename):
ImageFile._save(im, fp, [Image.Tile("raw", (0, 0) + im.size, 0, (mode, 0, 1))])


# fmt: on


def _accept(prefix):
return prefix[:4] == b"DDS "

Expand Down