Skip to content

Commit

Permalink
Swapped mode order when saving
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Oct 21, 2023
1 parent 4a4a1ee commit 577a0b8
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,19 +403,15 @@ def _open(self):
self._mode = "RGBA"
self.pixel_format = "DXT5"
n = 3
elif fourcc == D3DFMT.ATI1 or fourcc == D3DFMT.BC4U:
elif fourcc in (D3DFMT.BC4U, D3DFMT.ATI1):
self._mode = "L"
self.pixel_format = "BC4"
n = 4
elif fourcc == D3DFMT.BC5S:
self._mode = "RGB"
self.pixel_format = "BC5S"
n = 5
elif fourcc == D3DFMT.BC5U:
self._mode = "RGB"
self.pixel_format = "BC5U"
n = 5
elif fourcc == D3DFMT.ATI2:
elif fourcc in (D3DFMT.BC5U, D3DFMT.ATI2):
self._mode = "RGB"
self.pixel_format = "BC5"
n = 5
Expand Down Expand Up @@ -489,22 +485,22 @@ def _save(im, fp, filename):
raise OSError(msg)

pixel_flags = DDPF.RGB
if im.mode == "RGB":
rgba_mask = struct.pack("<4I", 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000)
bit_count = 24
elif im.mode == "RGBA":
if im.mode == "RGBA":
pixel_flags |= DDPF.ALPHAPIXELS
rgba_mask = struct.pack("<4I", 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000)
rgba_mask = (0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000)
bit_count = 32
r, g, b, a = im.split()
im = Image.merge("RGBA", (a, r, g, b))
elif im.mode == "RGB":
rgba_mask = (0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000)
bit_count = 24
elif im.mode == "LA":
pixel_flags = DDPF.LUMINANCE | DDPF.ALPHAPIXELS
rgba_mask = struct.pack("<4I", 0x000000FF, 0x000000FF, 0x000000FF, 0x0000FF00)
rgba_mask = (0x000000FF, 0x000000FF, 0x000000FF, 0x0000FF00)
bit_count = 16
else: # im.mode == "L"
pixel_flags = DDPF.LUMINANCE
rgba_mask = struct.pack("<4I", 0xFF000000, 0xFF000000, 0xFF000000, 0x00000000)
rgba_mask = (0xFF000000, 0xFF000000, 0xFF000000, 0x00000000)
bit_count = 8

flags = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PITCH | DDSD.PIXELFORMAT
Expand All @@ -513,7 +509,7 @@ def _save(im, fp, filename):
fp.write(
o32(DDS_MAGIC)
+ struct.pack(
"<IIIIIII",
"<7I",
124, # header size
flags, # flags
im.height,
Expand All @@ -524,9 +520,9 @@ def _save(im, fp, filename):
)
+ struct.pack("11I", *((0,) * 11)) # reserved
# pfsize, pfflags, fourcc, bitcount
+ struct.pack("<IIII", 32, pixel_flags, 0, bit_count)
+ rgba_mask # dwRGBABitMask
+ struct.pack("<IIIII", DDSCAPS.TEXTURE, 0, 0, 0, 0)
+ struct.pack("<4I", 32, pixel_flags, 0, bit_count)
+ struct.pack("<4I", *rgba_mask) # dwRGBABitMask
+ struct.pack("<5I", DDSCAPS.TEXTURE, 0, 0, 0, 0)
)
mode = "LA" if im.mode == "LA" else im.mode[::-1]
ImageFile._save(im, fp, [Image._Tile("raw", (0, 0) + im.size, 0, (mode, 0, 1))])
Expand Down

0 comments on commit 577a0b8

Please sign in to comment.