Skip to content

Commit d5273ee

Browse files
arunjose696HeikoKlare
authored andcommitted
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 cbf7f7a commit d5273ee

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
@@ -1148,6 +1148,9 @@ private int calculateZoomForImage(int gcZoom, int srcWidth, int srcHeight, int d
11481148
// unscaled images can use the GC zoom
11491149
return gcZoom;
11501150
}
1151+
if (!drawable.isAutoScalable()) {
1152+
return gcZoom;
1153+
}
11511154

11521155
float imageScaleFactor = 1f * destWidth / srcWidth;
11531156
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
@@ -247,6 +247,55 @@ public void test_dispose() {
247247
gc.dispose();
248248
}
249249

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

0 commit comments

Comments
 (0)