From e04ebe43423ee27333d10aec0da3fcaab3d34c0d Mon Sep 17 00:00:00 2001 From: Andreas Koch Date: Tue, 10 Dec 2024 09:54:08 +0100 Subject: [PATCH] [win32] No handle creation on Image::getBounds This commit adapts the bounds calculation in Image to not create any handle, but use and scale the bounds of an existing handle if no handle for the desired zoom is available Fixes #1639 --- .../win32/org/eclipse/swt/graphics/Image.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java index c4013a30a08..bb55941f0ce 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java @@ -127,7 +127,7 @@ public final class Image extends Resource implements Drawable { */ static final int DEFAULT_SCANLINE_PAD = 4; - private HashMap zoomLevelToImageHandle = new HashMap<>(); + private Map zoomLevelToImageHandle = new HashMap<>(); /** * Prevents uninitialized instances from being created outside the package. @@ -1303,8 +1303,14 @@ public Rectangle getBounds() { Rectangle getBounds(int zoom) { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - ImageHandle imageMetadata = getImageMetadata(zoom); - return new Rectangle(0, 0, imageMetadata.width, imageMetadata.height); + ImageHandle imageMetadata; + if (zoomLevelToImageHandle.containsKey(zoom)) { + imageMetadata = zoomLevelToImageHandle.get(zoom); + } else { + imageMetadata = zoomLevelToImageHandle.values().iterator().next(); + } + Rectangle rectangle = new Rectangle(0, 0, imageMetadata.width, imageMetadata.height); + return DPIUtil.scaleBounds(rectangle, zoom, imageMetadata.zoom); } /** @@ -2084,18 +2090,19 @@ public void close() { private class ImageHandle { final long handle; + final int zoom; int height; int width; public ImageHandle(long handle, int zoom) { Rectangle bounds = getBoundsInPixelsFromNative(handle); this.handle = handle; + this.zoom = zoom; this.height = bounds.height; this.width = bounds.width; setImageMetadataForHandle(this, zoom); } - private Rectangle getBoundsInPixelsFromNative(long handle) { switch (type) { case SWT.BITMAP: