From d892d790e56dc1bc17a2ae5f6e8452ddeeaa838c Mon Sep 17 00:00:00 2001 From: arunjose696 Date: Mon, 15 Sep 2025 13:13:04 +0200 Subject: [PATCH] Set zoom for source image as GCzoom when the GC is not autoscalable The calculated image zoom for the source image should be the same as the GCzoom(100) when the drawable of GC is not scalable Fixes: #2504 --- .../win32/org/eclipse/swt/graphics/GC.java | 3 ++ .../Test_org_eclipse_swt_graphics_GC.java | 49 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java index 7449b612b5f..5b17f305786 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java @@ -1148,6 +1148,9 @@ private int calculateZoomForImage(int gcZoom, int srcWidth, int srcHeight, int d // unscaled images can use the GC zoom return gcZoom; } + if (!drawable.isAutoScalable()) { + return gcZoom; + } float imageScaleFactor = 1f * destWidth / srcWidth; int imageZoom = Math.round(gcZoom * imageScaleFactor); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java index 6604b61b9f5..a1a8a6d7f52 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java @@ -247,6 +247,55 @@ public void test_dispose() { gc.dispose(); } + +@Test +public void test_drawImage_nonAutoScalableGC_bug_2504() { + Shell shell = new Shell(display); + float targetScale = 2f; + int srcSize = 50; + Image image = new Image(display, srcSize, srcSize); + GC gcSrc = new GC(image); + gcSrc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); + gcSrc.fillRectangle(0, 0, srcSize, srcSize); + gcSrc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); + gcSrc.fillRectangle(2, 2, srcSize - 4, srcSize - 4); + gcSrc.dispose(); + + Rectangle bounds = image.getBounds(); + + Canvas canvas = new Canvas(shell, SWT.NONE) { + @Override + public boolean isAutoScalable() { + return false; + } + }; + + int canvasWidth = Math.round(bounds.width * targetScale); + int canvasHeight = Math.round(bounds.height * targetScale); + canvas.setSize(canvasWidth, canvasHeight); + canvas.addPaintListener(e -> { + e.gc.drawImage(image, 0, 0, bounds.width, bounds.height, + 0, 0, canvasWidth, canvasHeight); + }); + + shell.open(); + Image target = new Image(display, canvasWidth, canvasHeight); + GC gcCopy = new GC(canvas); + gcCopy.copyArea(target, 0, 0); + gcCopy.dispose(); + + ImageData data = target.getImageData(); + + int bottomRightX = canvasWidth - 1; + int bottomRightY = canvasHeight - 1; + RGB bottomRight = data.palette.getRGB(data.getPixel(bottomRightX, bottomRightY)); + RGB black = new RGB(0, 0, 0); + assertEquals( black, bottomRight, "Bottom-right pixel is not black! when source GC is not autoScalable"); + shell.dispose(); + target.dispose(); + image.dispose(); +} + @Test public void test_drawArcIIIIII() { gc.drawArc(10, 20, 50, 25, 90, 90);