Set mode of three channel uncompressed RGB data to RGB

This commit is contained in:
Andrew Murray 2021-04-05 17:58:02 +10:00
parent 6f87faf0ee
commit d06871d543
3 changed files with 13 additions and 3 deletions

BIN
Tests/images/hopper.dds Normal file

Binary file not shown.

View File

@ -14,7 +14,8 @@ TEST_FILE_DX10_BC7 = "Tests/images/bc7-argb-8bpp_MipMaps-1.dds"
TEST_FILE_DX10_BC7_UNORM_SRGB = "Tests/images/DXGI_FORMAT_BC7_UNORM_SRGB.dds" TEST_FILE_DX10_BC7_UNORM_SRGB = "Tests/images/DXGI_FORMAT_BC7_UNORM_SRGB.dds"
TEST_FILE_DX10_R8G8B8A8 = "Tests/images/argb-32bpp_MipMaps-1.dds" TEST_FILE_DX10_R8G8B8A8 = "Tests/images/argb-32bpp_MipMaps-1.dds"
TEST_FILE_DX10_R8G8B8A8_UNORM_SRGB = "Tests/images/DXGI_FORMAT_R8G8B8A8_UNORM_SRGB.dds" TEST_FILE_DX10_R8G8B8A8_UNORM_SRGB = "Tests/images/DXGI_FORMAT_R8G8B8A8_UNORM_SRGB.dds"
TEST_FILE_UNCOMPRESSED_RGB = "Tests/images/uncompressed_rgb.dds" TEST_FILE_UNCOMPRESSED_RGB = "Tests/images/hopper.dds"
TEST_FILE_UNCOMPRESSED_RGB_WITH_ALPHA = "Tests/images/uncompressed_rgb.dds"
def test_sanity_dxt1(): def test_sanity_dxt1():
@ -124,15 +125,22 @@ def test_unimplemented_dxgi_format():
def test_uncompressed_rgb(): def test_uncompressed_rgb():
"""Check uncompressed RGB images can be opened""" """Check uncompressed RGB images can be opened"""
# convert -format dds -define dds:compression=none hopper.jpg hopper.dds
with Image.open(TEST_FILE_UNCOMPRESSED_RGB) as im: with Image.open(TEST_FILE_UNCOMPRESSED_RGB) as im:
im.load() assert im.format == "DDS"
assert im.mode == "RGB"
assert im.size == (128, 128)
assert_image_equal_tofile(im, "Tests/images/hopper.png")
# Test image with alpha
with Image.open(TEST_FILE_UNCOMPRESSED_RGB_WITH_ALPHA) as im:
assert im.format == "DDS" assert im.format == "DDS"
assert im.mode == "RGBA" assert im.mode == "RGBA"
assert im.size == (800, 600) assert im.size == (800, 600)
assert_image_equal_tofile( assert_image_equal_tofile(
im, TEST_FILE_UNCOMPRESSED_RGB.replace(".dds", ".png") im, TEST_FILE_UNCOMPRESSED_RGB_WITH_ALPHA.replace(".dds", ".png")
) )

View File

@ -133,6 +133,8 @@ class DdsImageFile(ImageFile.ImageFile):
rawmode = "" rawmode = ""
if bitcount == 32: if bitcount == 32:
rawmode += masks[0xFF000000] rawmode += masks[0xFF000000]
else:
self.mode = "RGB"
rawmode += masks[0xFF0000] + masks[0xFF00] + masks[0xFF] rawmode += masks[0xFF0000] + masks[0xFF00] + masks[0xFF]
self.tile = [("raw", (0, 0) + self.size, 0, (rawmode[::-1], 0, 1))] self.tile = [("raw", (0, 0) + self.size, 0, (rawmode[::-1], 0, 1))]