Skip to content

Commit 0642bb4

Browse files
ShahzaibIbrahimHeikoKlare
authored andcommitted
Enable check for using GC on Image in unsupported use cases
With this change, warnings will be logged when strict checks are enabled and a GC is initialized for an image in unsupported cases: 1. images whose scaled variants are not derived via scaling from an original version but are retrieved from a central data source (like ImageDataProvider or ImageFileNameProvider) 2. images for which handles in other zoom values have already been created such that drawing on them will only be applied to one of those handles
1 parent 25ad773 commit 0642bb4

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,9 @@ private long configureGC(GCData data, int zoom) {
17071707
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
17081708
}
17091709

1710+
if(Device.strictChecks) {
1711+
checkImageTypeForValidCustomDrawing(zoom);
1712+
}
17101713
/* Create a compatible HDC for the device */
17111714
long hDC = device.internal_new_GC(null);
17121715
long imageDC = OS.CreateCompatibleDC(hDC);
@@ -1729,6 +1732,18 @@ private long configureGC(GCData data, int zoom) {
17291732
return imageDC;
17301733
}
17311734

1735+
private void checkImageTypeForValidCustomDrawing(int zoom) {
1736+
String replacementInfo = "It should be created with an ImageGcDrawer (see SWT Snippet 384).";
1737+
if (imageProvider instanceof ImageDataProviderWrapper || imageProvider instanceof ImageFileNameProviderWrapper) {
1738+
String message = "***WARNING: Image initialized with ImageDataProvider or ImageFileNameProvider is not supposed to be modified.";
1739+
System.err.println(message + " " + replacementInfo);
1740+
} else if (!zoomLevelToImageHandle.isEmpty()
1741+
&& (zoomLevelToImageHandle.size() != 1 || !zoomLevelToImageHandle.containsKey(zoom))) {
1742+
String message = "***WARNING: Images with handles created for multiple zooms should not be modified. ";
1743+
System.err.println(message + " " + replacementInfo);
1744+
}
1745+
}
1746+
17321747
/**
17331748
* Invokes platform specific functionality to dispose a GC handle.
17341749
* <p>

examples/org.eclipse.swt.snippets/Snippets.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ To contribute a new snippet, [create a snippet contribution as a pull request](h
215215
- [draw an image at various zoom/dpi levels](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet367.png "Preview for Snippet 367")
216216
- [draw a disabled/grayed image at various zoom levels](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet382.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet382.png "Preview for Snippet 382")
217217
- [compare algorithms for rendering disabled images](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet384.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet384.png "Preview for Snippet 384")
218+
- [draw an image with watermark using ImageGcProvider](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet385.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet385.png "Preview for Snippet 385")
218219

219220
### **ImageData**
220221
- [display an animated GIF](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java)
Loading
Loading
Loading
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Yatta Solutions
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Yatta Solutions - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.snippets;
15+
16+
import org.eclipse.swt.*;
17+
import org.eclipse.swt.graphics.*;
18+
import org.eclipse.swt.widgets.*;
19+
20+
/**
21+
* Demonstration how to custom draw images
22+
* <p>
23+
* Snippet to show how to draw on an image loaded from the file system which has
24+
* multiple variants for different zoom levels. Mainly relevant on Windows, as
25+
* it uses the Windows-only monitor-specific scaling mode.
26+
* <p>
27+
* For a list of all SWT example snippets see
28+
* http://www.eclipse.org/swt/snippets/
29+
* </p>
30+
*/
31+
public class Snippet385 {
32+
33+
public static void main(String[] args) {
34+
System.setProperty("swt.autoScale.updateOnRuntime", "true");
35+
Display display = new Display();
36+
Shell shell = new Shell(display);
37+
shell.setText("Watermark over Image using GC");
38+
shell.setSize(1200, 900);
39+
40+
final ImageFileNameProvider filenameProvider = zoom -> {
41+
String path = null;
42+
switch (zoom) {
43+
case 150:
44+
path = "resources/Snippet385/red.jpeg";
45+
break;
46+
case 100:
47+
path = "resources/Snippet385/black.jpg";
48+
break;
49+
default:
50+
path = "resources/Snippet385/black.jpg";
51+
}
52+
return path;
53+
};
54+
Image loadedImage = new Image(display, filenameProvider);
55+
56+
int width = loadedImage.getBounds().width;
57+
int height = loadedImage.getBounds().height;
58+
59+
Image composedImage = new Image(display, (gc, iX, iY) -> {
60+
gc.drawImage(loadedImage, 0, 0);
61+
62+
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
63+
Font font = new Font(display, "Arial", 24, SWT.BOLD);
64+
gc.setFont(font);
65+
gc.setAlpha(128); // semi-transparent
66+
String watermark = "WATERMARK";
67+
Point extent = gc.textExtent(watermark);
68+
int x = (width - extent.x) / 2;
69+
int y = (height - extent.y) / 2;
70+
gc.drawText(watermark, x, y, SWT.DRAW_TRANSPARENT);
71+
font.dispose();
72+
}, width, height);
73+
74+
shell.addPaintListener(e -> {
75+
e.gc.drawImage(composedImage, 0, 0);
76+
});
77+
78+
shell.open();
79+
while (!shell.isDisposed()) {
80+
if (!display.readAndDispatch())
81+
display.sleep();
82+
}
83+
84+
loadedImage.dispose();
85+
composedImage.dispose();
86+
display.dispose();
87+
}
88+
}

0 commit comments

Comments
 (0)