From ce4059171cc5696e7c7d8c5dc74990d5e874bf58 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 26 Oct 2024 18:41:05 +1100 Subject: [PATCH 1/3] Skip failing records when rendering --- Tests/test_file_wmf.py | 12 +++++++++++- src/display.c | 13 +++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Tests/test_file_wmf.py b/Tests/test_file_wmf.py index 79e707263d6..d730a049a98 100644 --- a/Tests/test_file_wmf.py +++ b/Tests/test_file_wmf.py @@ -1,5 +1,6 @@ from __future__ import annotations +from io import BytesIO from pathlib import Path from typing import IO @@ -7,7 +8,7 @@ from PIL import Image, ImageFile, WmfImagePlugin -from .helper import assert_image_similar_tofile, hopper +from .helper import assert_image_equal_tofile, assert_image_similar_tofile, hopper def test_load_raw() -> None: @@ -34,6 +35,15 @@ def test_load() -> None: assert im.load()[0, 0] == (255, 255, 255) +def test_render() -> None: + with open("Tests/images/drawing.emf", "rb") as fp: + data = fp.read() + b = BytesIO(data[:808] + b"\x00" + data[809:]) + with Image.open(b) as im: + if hasattr(Image.core, "drawwmf"): + assert_image_equal_tofile(im, "Tests/images/drawing.emf") + + def test_register_handler(tmp_path: Path) -> None: class TestHandler(ImageFile.StubHandler): methodCalled = False diff --git a/src/display.c b/src/display.c index b4e2e38991a..03b9316c36a 100644 --- a/src/display.c +++ b/src/display.c @@ -716,6 +716,14 @@ PyImaging_EventLoopWin32(PyObject *self, PyObject *args) { #define GET32(p, o) ((DWORD *)(p + o))[0] +BOOL +enhMetaFileProc( + HDC hdc, HANDLETABLE FAR *lpht, CONST ENHMETARECORD *lpmr, int nHandles, LPARAM data +) { + PlayEnhMetaFileRecord(hdc, lpht, lpmr, nHandles); + return TRUE; +} + PyObject * PyImaging_DrawWmf(PyObject *self, PyObject *args) { HBITMAP bitmap; @@ -796,10 +804,7 @@ PyImaging_DrawWmf(PyObject *self, PyObject *args) { /* FIXME: make background transparent? configurable? */ FillRect(dc, &rect, GetStockObject(WHITE_BRUSH)); - if (!PlayEnhMetaFile(dc, meta, &rect)) { - PyErr_SetString(PyExc_OSError, "cannot render metafile"); - goto error; - } + EnumEnhMetaFile(dc, meta, enhMetaFileProc, NULL, &rect); /* step 4: extract bits from bitmap */ From b4ba4665410c70ad8976b092ee9b1ad89625c0ed Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 27 Oct 2024 07:03:35 +1100 Subject: [PATCH 2/3] Do not skip failing records on 32-bit --- Tests/test_file_wmf.py | 2 ++ src/display.c | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Tests/test_file_wmf.py b/Tests/test_file_wmf.py index 2adf38d48c3..e60a5b64e55 100644 --- a/Tests/test_file_wmf.py +++ b/Tests/test_file_wmf.py @@ -1,5 +1,6 @@ from __future__ import annotations +import sys from io import BytesIO from pathlib import Path from typing import IO @@ -35,6 +36,7 @@ def test_load() -> None: assert im.load()[0, 0] == (255, 255, 255) +@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system") def test_render() -> None: with open("Tests/images/drawing.emf", "rb") as fp: data = fp.read() diff --git a/src/display.c b/src/display.c index 03b9316c36a..fe5801fc0e6 100644 --- a/src/display.c +++ b/src/display.c @@ -716,12 +716,12 @@ PyImaging_EventLoopWin32(PyObject *self, PyObject *args) { #define GET32(p, o) ((DWORD *)(p + o))[0] -BOOL +int enhMetaFileProc( - HDC hdc, HANDLETABLE FAR *lpht, CONST ENHMETARECORD *lpmr, int nHandles, LPARAM data + HDC hdc, HANDLETABLE *lpht, const ENHMETARECORD *lpmr, int nHandles, LPARAM data ) { PlayEnhMetaFileRecord(hdc, lpht, lpmr, nHandles); - return TRUE; + return 1; } PyObject * @@ -804,7 +804,14 @@ PyImaging_DrawWmf(PyObject *self, PyObject *args) { /* FIXME: make background transparent? configurable? */ FillRect(dc, &rect, GetStockObject(WHITE_BRUSH)); +#ifdef _WIN64 EnumEnhMetaFile(dc, meta, enhMetaFileProc, NULL, &rect); +#else + if (!PlayEnhMetaFile(dc, meta, &rect)) { + PyErr_SetString(PyExc_OSError, "cannot render metafile"); + goto error; + } +#endif /* step 4: extract bits from bitmap */ From 2ea3ea94a117772532e6f04ae7284c5842392af4 Mon Sep 17 00:00:00 2001 From: Nulano Date: Thu, 26 Dec 2024 21:41:51 +0100 Subject: [PATCH 3/3] Skip failing WMF records on 32-bit Windows --- Tests/test_file_wmf.py | 2 -- src/display.c | 9 +-------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/Tests/test_file_wmf.py b/Tests/test_file_wmf.py index 9322bd0c53b..d3cad7ca481 100644 --- a/Tests/test_file_wmf.py +++ b/Tests/test_file_wmf.py @@ -1,6 +1,5 @@ from __future__ import annotations -import sys from io import BytesIO from pathlib import Path from typing import IO @@ -43,7 +42,6 @@ def test_load_zero_inch() -> None: pass -@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system") def test_render() -> None: with open("Tests/images/drawing.emf", "rb") as fp: data = fp.read() diff --git a/src/display.c b/src/display.c index fe5801fc0e6..b5e9c2a3db7 100644 --- a/src/display.c +++ b/src/display.c @@ -716,7 +716,7 @@ PyImaging_EventLoopWin32(PyObject *self, PyObject *args) { #define GET32(p, o) ((DWORD *)(p + o))[0] -int +static int CALLBACK enhMetaFileProc( HDC hdc, HANDLETABLE *lpht, const ENHMETARECORD *lpmr, int nHandles, LPARAM data ) { @@ -804,14 +804,7 @@ PyImaging_DrawWmf(PyObject *self, PyObject *args) { /* FIXME: make background transparent? configurable? */ FillRect(dc, &rect, GetStockObject(WHITE_BRUSH)); -#ifdef _WIN64 EnumEnhMetaFile(dc, meta, enhMetaFileProc, NULL, &rect); -#else - if (!PlayEnhMetaFile(dc, meta, &rect)) { - PyErr_SetString(PyExc_OSError, "cannot render metafile"); - goto error; - } -#endif /* step 4: extract bits from bitmap */