Skip to content

Commit 61f063d

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 61f063d

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import org.eclipse.swt.graphics.Rectangle;
4747
import org.eclipse.swt.graphics.Transform;
4848
import org.eclipse.swt.internal.DPIUtil;
49+
import org.eclipse.swt.layout.FormData;
50+
import org.eclipse.swt.layout.FormLayout;
4951
import org.eclipse.swt.widgets.Canvas;
5052
import org.eclipse.swt.widgets.Display;
5153
import org.eclipse.swt.widgets.Shell;
@@ -246,6 +248,60 @@ public void test_dispose() {
246248
gc.dispose();
247249
}
248250

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

0 commit comments

Comments
 (0)