Skip to content

Commit a2785fe

Browse files
committed
Obtain ImageData for any requested zoom value
This contribution helps retrieve the imageData for the requested zoom by searching for the zoom in the zoomToHandleMap followed by (if not available) scaling the handle at the zoom nearest to it while getting rid of only searching among the autoscaled zoom values. Contributes to #62 and #127
1 parent d7febc4 commit a2785fe

File tree

2 files changed

+17
-5
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics
  • tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/graphics

2 files changed

+17
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,10 +1359,10 @@ public ImageData getImageData() {
13591359
*/
13601360
public ImageData getImageData (int zoom) {
13611361
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
1362-
int currentZoom = getZoom();
1363-
if (zoom == currentZoom) {
1364-
return getImageDataAtCurrentZoom();
1365-
} else if (imageProvider != null) {
1362+
if (zoomLevelToImageHandle.get(zoom) != null) {
1363+
return zoomLevelToImageHandle.get(zoom).getImageData();
1364+
}
1365+
if (imageProvider != null) {
13661366
return imageProvider.getImageData(zoom);
13671367
}
13681368

@@ -1371,7 +1371,9 @@ public ImageData getImageData (int zoom) {
13711371
if (memGC != null) {
13721372
return getImageDataAtCurrentZoom();
13731373
}
1374-
return DPIUtil.scaleImageData (device, getImageMetadata(currentZoom).getImageData(), zoom, currentZoom);
1374+
TreeSet<Integer> availableZooms = new TreeSet<>(zoomLevelToImageHandle.keySet());
1375+
int closestZoom = Optional.ofNullable(availableZooms.higher(zoom)).orElse(availableZooms.lower(zoom));
1376+
return DPIUtil.scaleImageData (device, getImageMetadata(closestZoom).getImageData(), zoom, closestZoom);
13751377
}
13761378

13771379
/**

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/graphics/ImageWin32Tests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616

1717
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertNotEquals;
1819

1920
import org.eclipse.swt.internal.DPIUtil;
2021
import org.eclipse.swt.widgets.Display;
@@ -36,6 +37,15 @@ public void setUp() {
3637
display = Display.getDefault();
3738
}
3839

40+
@Test
41+
public void testImageDataForDifferentZoomsShouldNotBeScaledToTheAutoScaledZoom() {
42+
Image image = new Image(display, 10, 10);
43+
ImageData imageDataAt125 = image.getImageData(125);
44+
ImageData imageDataAt150 = image.getImageData(150);
45+
assertNotEquals("ImageData::height should not be the same for imageData at different zoom levels within the same autoScale zoom bounds", imageDataAt125.height, imageDataAt150.height);
46+
assertNotEquals("ImageData::width should not be the same for imageData at different zoom levels within the same autoScale zoom bounds", imageDataAt125.width, imageDataAt150.width);
47+
}
48+
3949
@Test
4050
public void testImageShouldHaveDimesionAsPerZoomLevel() {
4151
int zoom = DPIUtil.getDeviceZoom();

0 commit comments

Comments
 (0)