Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip failing records when rendering WMF images #8506

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Tests/test_file_wmf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from io import BytesIO
from pathlib import Path
from typing import IO
Expand All @@ -8,7 +9,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:
Expand Down Expand Up @@ -42,6 +43,16 @@ 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()
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
Expand Down
12 changes: 12 additions & 0 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,14 @@ PyImaging_EventLoopWin32(PyObject *self, PyObject *args) {

#define GET32(p, o) ((DWORD *)(p + o))[0]

int
radarhere marked this conversation as resolved.
Show resolved Hide resolved
enhMetaFileProc(
HDC hdc, HANDLETABLE *lpht, const ENHMETARECORD *lpmr, int nHandles, LPARAM data
) {
PlayEnhMetaFileRecord(hdc, lpht, lpmr, nHandles);
return 1;
}

PyObject *
PyImaging_DrawWmf(PyObject *self, PyObject *args) {
HBITMAP bitmap;
Expand Down Expand Up @@ -796,10 +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 */

Expand Down
Loading