Skip to content

Commit

Permalink
Added RGBA saving
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 14, 2021
1 parent 143e08c commit 6449cdc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
18 changes: 13 additions & 5 deletions Tests/test_file_dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,18 @@ def test_save_unsupported_mode(tmp_path):
im.save(out)


def test_save(tmp_path):
@pytest.mark.parametrize(
("mode", "test_file"),
[
("RGB", "Tests/images/hopper.png"),
("RGBA", "Tests/images/pil123rgba.png"),
],
)
def test_save(mode, test_file, tmp_path):
out = str(tmp_path / "temp.dds")
im = hopper()
im.save(out)
with Image.open(test_file) as im:
assert im.mode == mode
im.save(out)

with Image.open(out) as reloaded:
assert_image_equal(im, reloaded)
with Image.open(out) as reloaded:
assert_image_equal(im, reloaded)
13 changes: 8 additions & 5 deletions src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def load_seek(self, pos):


def _save(im, fp, filename):
if im.mode != "RGB":
if im.mode not in ("RGB", "RGBA"):
raise OSError(f"cannot write mode {im.mode} as DDS")

fp.write(
Expand All @@ -214,24 +214,27 @@ def _save(im, fp, filename):
) # flags
+ o32(im.height)
+ o32(im.width)
+ o32((im.width * 24 + 7) // 8) # pitch
+ o32((im.width * (32 if im.mode == "RGBA" else 24) + 7) // 8) # pitch
+ o32(0) # depth
+ o32(0) # mipmaps
+ o32(0) * 11 # reserved
+ o32(32) # pfsize
+ o32(DDPF_RGB) # pfflags
+ o32(DDS_RGBA if im.mode == "RGBA" else DDPF_RGB) # pfflags
+ o32(0) # fourcc
+ o32(24) # bitcount
+ o32(32 if im.mode == "RGBA" else 24) # bitcount
+ o32(0xFF0000) # rbitmask
+ o32(0xFF00) # gbitmask
+ o32(0xFF) # bbitmask
+ o32(0) # abitmask
+ o32(0xFF000000 if im.mode == "RGBA" else 0) # abitmask
+ o32(DDSCAPS_TEXTURE) # dwCaps
+ o32(0) # dwCaps2
+ o32(0) # dwCaps3
+ o32(0) # dwCaps4
+ o32(0) # dwReserved2
)
if im.mode == "RGBA":
r, g, b, a = im.split()
im = Image.merge("RGBA", (a, r, g, b))
ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (im.mode[::-1], 0, 1))])


Expand Down

0 comments on commit 6449cdc

Please sign in to comment.