From 097f7d0f56baecf35a63e29960666def35fb5925 Mon Sep 17 00:00:00 2001 From: Jan Solanti Date: Wed, 16 Oct 2019 00:21:15 +0300 Subject: [PATCH 1/3] Stop decoding BC1 punchthrough alpha in BC2&3 --- src/libImaging/BcnDecode.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libImaging/BcnDecode.c b/src/libImaging/BcnDecode.c index b6a4cbadcc5..611daba5709 100644 --- a/src/libImaging/BcnDecode.c +++ b/src/libImaging/BcnDecode.c @@ -69,7 +69,7 @@ decode_565(UINT16 x) { } static void -decode_bc1_color(rgba *dst, const UINT8 *src) { +decode_bc1_color(rgba *dst, const UINT8 *src, int separate_alpha) { bc1_color col; rgba p[4]; int n, cw; @@ -150,13 +150,13 @@ decode_bc3_alpha(char *dst, const UINT8 *src, int stride, int o) { static void decode_bc1_block(rgba *col, const UINT8 *src) { - decode_bc1_color(col, src); + decode_bc1_color(col, src, 0); } static void decode_bc2_block(rgba *col, const UINT8 *src) { int n, bitI, byI, av; - decode_bc1_color(col, src + 8); + decode_bc1_color(col, src + 8, 1); for (n = 0; n < 16; n++) { bitI = n * 4; byI = bitI >> 3; @@ -168,7 +168,7 @@ decode_bc2_block(rgba *col, const UINT8 *src) { static void decode_bc3_block(rgba *col, const UINT8 *src) { - decode_bc1_color(col, src + 8); + decode_bc1_color(col, src + 8, 1); decode_bc3_alpha((char *)col, src, sizeof(col[0]), 3); } From ddd3a2b482a8316d81def53d14ded43368c49701 Mon Sep 17 00:00:00 2001 From: Jan Solanti Date: Thu, 9 Jan 2020 05:06:48 +0200 Subject: [PATCH 2/3] Add tests for issue #4142 --- .../images/dxt5-colorblock-alpha-issue-4142.dds | Bin 0 -> 384 bytes Tests/test_file_dds.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 Tests/images/dxt5-colorblock-alpha-issue-4142.dds diff --git a/Tests/images/dxt5-colorblock-alpha-issue-4142.dds b/Tests/images/dxt5-colorblock-alpha-issue-4142.dds new file mode 100644 index 0000000000000000000000000000000000000000..905527eada42fe98ea900f9608411e61c58480ae GIT binary patch literal 384 zcmZ>930A0KU|?Vu;9y_@(jY7V#N+@4peB%hmxvHktopGSoD{W|w^wN8GQ}f~N8(>U z$ViTxy~#4_y%>nkxqt8b#Hhb}1GN=39gip;34OKpZgQB4dTzt~IgV!(_pDy^>wiYv z&hLAY*fSl^I35YTs=q2ddd8LeIal`p<&XTozv_Bctn$R?TV8Kc1j=jgUl*GlEirpf zY36j0`iAS*uck#6dpe$bYXvfI)vEvhQ$r_hW6YU-8))Ai28Pw#Ns+fXww2GF26E5( zSL;_LNB94{z0Bnc$UUpH)_;l*@3Za^cHicB#BtAh28LDfQKD~GM*j~2$**JXS{ENS f+rTb&_f?Sj>;M1%pAbIj!*iAI(?Rn4L&M_%){2PU literal 0 HcmV?d00001 diff --git a/Tests/test_file_dds.py b/Tests/test_file_dds.py index 1cd7a1be75c..5651c2e4d58 100644 --- a/Tests/test_file_dds.py +++ b/Tests/test_file_dds.py @@ -190,6 +190,21 @@ def short_file(): short_file() +def test_dxt5_colorblock_alpha_issue_4142(): + """ Check that colorblocks are decoded correctly in DXT5""" + + with Image.open("Tests/images/dxt5-colorblock-alpha-issue-4142.dds") as im: + px = im.getpixel((0, 0)) + assert px[0] != 0 + assert px[1] != 0 + assert px[2] != 0 + + px = im.getpixel((1, 0)) + assert px[0] != 0 + assert px[1] != 0 + assert px[2] != 0 + + def test_unimplemented_pixel_format(): with pytest.raises(NotImplementedError): Image.open("Tests/images/unimplemented_pixel_format.dds") From b08c514ee76dd10f6c4f35ab87fd6f1b6bc77be3 Mon Sep 17 00:00:00 2001 From: Jan Solanti Date: Thu, 9 Jan 2020 05:25:03 +0200 Subject: [PATCH 3/3] BcnDecode: add comment about BC2&3 color blocks --- src/libImaging/BcnDecode.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libImaging/BcnDecode.c b/src/libImaging/BcnDecode.c index 611daba5709..5e8b456b83e 100644 --- a/src/libImaging/BcnDecode.c +++ b/src/libImaging/BcnDecode.c @@ -84,7 +84,10 @@ decode_bc1_color(rgba *dst, const UINT8 *src, int separate_alpha) { r1 = p[1].r; g1 = p[1].g; b1 = p[1].b; - if (col.c0 > col.c1) { + + + /* NOTE: BC2 and BC3 reuse BC1 color blocks but always act like c0 > c1 */ + if (col.c0 > col.c1 || separate_alpha) { p[2].r = (2 * r0 + 1 * r1) / 3; p[2].g = (2 * g0 + 1 * g1) / 3; p[2].b = (2 * b0 + 1 * b1) / 3;