Skip to content

Commit 28d0e34

Browse files
committed
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
1 parent c6a29a9 commit 28d0e34

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,9 @@ private int calculateZoomForImage(int gcZoom, int srcWidth, int srcHeight, int d
11401140
// unscaled images can use the GC zoom
11411141
return gcZoom;
11421142
}
1143+
if (!drawable.isAutoScalable()) {
1144+
return gcZoom;
1145+
}
11431146

11441147
float imageScaleFactor = 1f * destWidth / srcWidth;
11451148
int imageZoom = Math.round(gcZoom * imageScaleFactor);

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,55 @@ public void test_dispose() {
246246
gc.dispose();
247247
}
248248

249+
250+
@Test
251+
public void test_drawImage_nonAutoScalableGC_bug_2504() {
252+
Shell shell = new Shell(display);
253+
float targetScale = 2f;
254+
int srcSize = 50;
255+
Image image = new Image(display, srcSize, srcSize);
256+
GC gcSrc = new GC(image);
257+
gcSrc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
258+
gcSrc.fillRectangle(0, 0, srcSize, srcSize);
259+
gcSrc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
260+
gcSrc.fillRectangle(2, 2, srcSize - 4, srcSize - 4);
261+
gcSrc.dispose();
262+
263+
Rectangle bounds = image.getBounds();
264+
265+
Canvas canvas = new Canvas(shell, SWT.NONE) {
266+
@Override
267+
public boolean isAutoScalable() {
268+
return false;
269+
}
270+
};
271+
272+
int canvasWidth = Math.round(bounds.width * targetScale);
273+
int canvasHeight = Math.round(bounds.height * targetScale);
274+
canvas.setSize(canvasWidth, canvasHeight);
275+
canvas.addPaintListener(e -> {
276+
e.gc.drawImage(image, 0, 0, bounds.width, bounds.height,
277+
0, 0, canvasWidth, canvasHeight);
278+
});
279+
280+
shell.open();
281+
Image target = new Image(display, canvasWidth, canvasHeight);
282+
GC gcCopy = new GC(canvas);
283+
gcCopy.copyArea(target, 0, 0);
284+
gcCopy.dispose();
285+
286+
ImageData data = target.getImageData();
287+
288+
int bottomRightX = canvasWidth - 1;
289+
int bottomRightY = canvasHeight - 1;
290+
RGB bottomRight = data.palette.getRGB(data.getPixel(bottomRightX, bottomRightY));
291+
RGB black = new RGB(0, 0, 0);
292+
assertEquals( black, bottomRight, "Bottom-right pixel is not black! when source GC is not autoScalable");
293+
shell.dispose();
294+
target.dispose();
295+
image.dispose();
296+
}
297+
249298
@Test
250299
public void test_drawArcIIIIII() {
251300
gc.drawArc(10, 20, 50, 25, 90, 90);

0 commit comments

Comments
 (0)