Skip to content

[Win32] Fix drawImage() failing for an empty image - #3463

Merged
HeikoKlare merged 1 commit into
eclipse-platform:masterfrom
HeikoKlare:fix-issue-3442
Jul 28, 2026
Merged

[Win32] Fix drawImage() failing for an empty image#3463
HeikoKlare merged 1 commit into
eclipse-platform:masterfrom
HeikoKlare:fix-issue-3442

Conversation

@HeikoKlare

Copy link
Copy Markdown
Contributor

Fixes drawing an empty (blank) Image via GC.drawImage() with a scaled source/destination region.

Problem

Drawing a blank image with a scaled region failed with a NullPointerException. The nearest-zoom lookup in PlainImageProviderWrapper tried to derive the zoom from an existing image handle, but an empty image has no handle yet, so the lookup resolved to null.

Reproducible with the updated Snippet6 taken from the issue.

Fix

The nearest-zoom lookup now falls back to using an empty image at 100% zoom when no image handle exists yet, so drawing an empty image succeeds.

Test

Adds an OS-independent test to Test_org_eclipse_swt_graphics_GC that draws an empty image with a scaled region to guard against this regression.

Fixes #3442

@github-actions

github-actions Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Test Results

  200 files  ±0    200 suites  ±0   30m 3s ⏱️ + 1m 27s
4 887 tests +1  4 863 ✅ +1   24 💤 ±0  0 ❌ ±0 
7 040 runs  +6  6 872 ✅ +6  168 💤 ±0  0 ❌ ±0 

Results for commit 281a2e1. ± Comparison against base commit 635e953.

♻️ This comment has been updated with latest results.

@HeikoKlare
HeikoKlare marked this pull request as ready for review July 24, 2026 15:00
return 100;
}
TreeSet<Integer> availableZooms = new TreeSet<>(imageHandleManager.getAllZooms());
return availableZooms.contains(zoom) ? zoom : Optional.ofNullable(availableZooms.higher(zoom)).orElse(availableZooms.lower(zoom));

@r-mennig r-mennig Jul 27, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Optionals here just for chaining makes the code harder to read (in my opinion):

Suggested change
return availableZooms.contains(zoom) ? zoom : Optional.ofNullable(availableZooms.higher(zoom)).orElse(availableZooms.lower(zoom));
if (availableZooms.contains(zoom)) {
return zoom;
}
Integer higher = availableZooms.higher(zoom);
return higher != null ? higher : availableZooms.lower(zoom);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I had taken the code from getClosestAvailableImageData(), where the implementation already existed (without the special case for no handle existing yet). It became so complex when we added the case for a handle with the required zoom potentially existing already (before is was just a higher->orElse->lower implementation).

Anyway, I have extracted the functionality into a shared method inside the ImageHandleManager now and there implemented it exactly as you have proposed.

Drawing a blank (empty) image via GC.drawImage() with a scaled
source/destination region failed with a NullPointerException. The
nearest-zoom lookup tried to derive the zoom from an existing image
handle, but an empty image has no handle yet, so the lookup resolved to
null.

The nearest-zoom lookup now falls back to using an empty image at 100%
zoom when no image handle exists yet, so drawing an empty image
succeeds.

Adds an OS-independent GC test that draws an empty image with a scaled
region to guard against this regression.

Fixes eclipse-platform#3442

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NPE when painting "empty" image

2 participants