Skip to content

Commit

Permalink
Added BC5_UNORM reading
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed May 17, 2021
1 parent 3f01813 commit 09bc0e1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
Binary file added Tests/images/bc5_unorm.dds
Binary file not shown.
Binary file added Tests/images/bc5_unorm.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions Tests/test_file_dds.py
Expand Up @@ -10,6 +10,7 @@
TEST_FILE_DXT1 = "Tests/images/dxt1-rgb-4bbp-noalpha_MipMaps-1.dds"
TEST_FILE_DXT3 = "Tests/images/dxt3-argb-8bbp-explicitalpha_MipMaps-1.dds"
TEST_FILE_DXT5 = "Tests/images/dxt5-argb-8bbp-interpolatedalpha_MipMaps-1.dds"
TEST_FILE_DX10_BC5 = "Tests/images/bc5.dds"
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_R8G8B8A8 = "Tests/images/argb-32bpp_MipMaps-1.dds"
Expand Down Expand Up @@ -58,6 +59,19 @@ def test_sanity_dxt3():
assert_image_equal_tofile(im, TEST_FILE_DXT3.replace(".dds", ".png"))


def test_dx10_bc5():
"""Check DX10 images can be opened"""

with Image.open(TEST_FILE_DX10_BC5) as im:
im.load()

assert im.format == "DDS"
assert im.mode == "RGB"
assert im.size == (540, 676)

assert_image_equal_tofile(im, TEST_FILE_DX10_BC5.replace(".dds", ".png"))


def test_dx10_bc7():
"""Check DX10 images can be opened"""

Expand Down
7 changes: 6 additions & 1 deletion src/PIL/DdsImagePlugin.py
Expand Up @@ -97,6 +97,7 @@
DXGI_FORMAT_R8G8B8A8_TYPELESS = 27
DXGI_FORMAT_R8G8B8A8_UNORM = 28
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29
DXGI_FORMAT_BC5_UNORM = 83
DXGI_FORMAT_BC7_TYPELESS = 97
DXGI_FORMAT_BC7_UNORM = 98
DXGI_FORMAT_BC7_UNORM_SRGB = 99
Expand Down Expand Up @@ -155,7 +156,11 @@ def _open(self):
# ignoring flags which pertain to volume textures and cubemaps
dxt10 = BytesIO(self.fp.read(20))
dxgi_format, dimension = struct.unpack("<II", dxt10.read(8))
if dxgi_format in (DXGI_FORMAT_BC7_TYPELESS, DXGI_FORMAT_BC7_UNORM):
if dxgi_format == DXGI_FORMAT_BC5_UNORM:
self.pixel_format = "BC5"
n = 5
self.mode = "RGB"
elif dxgi_format in (DXGI_FORMAT_BC7_TYPELESS, DXGI_FORMAT_BC7_UNORM):
self.pixel_format = "BC7"
n = 7
elif dxgi_format == DXGI_FORMAT_BC7_UNORM_SRGB:
Expand Down
4 changes: 3 additions & 1 deletion src/decode.c
Expand Up @@ -368,13 +368,15 @@ PyImaging_BcnDecoderNew(PyObject *self, PyObject *args) {
case 1: /* BC1: 565 color, 1-bit alpha */
case 2: /* BC2: 565 color, 4-bit alpha */
case 3: /* BC3: 565 color, 2-endpoint 8-bit interpolated alpha */
case 5: /* BC5: 2-channel 8-bit via 2 BC3 alpha blocks */
case 7: /* BC7: 4-channel 8-bit via everything */
actual = "RGBA";
break;
case 4: /* BC4: 1-channel 8-bit via 1 BC3 alpha block */
actual = "L";
break;
case 5: /* BC5: 2-channel 8-bit via 2 BC3 alpha blocks */
actual = "RGB";
break;
case 6: /* BC6: 3-channel 16-bit float */
/* TODO: support 4-channel floating point images */
actual = "RGBAF";
Expand Down

0 comments on commit 09bc0e1

Please sign in to comment.