Skip to content

Commit 9a9747e

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 9a9747e

File tree

2 files changed

+27
-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

+27
-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: 20 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,25 @@ public void setUp() {
3637
display = Display.getDefault();
3738
}
3839

40+
@Test
41+
public void testImageDataForDifferentZoomsShouldNotBeScaledToTheAutoScaledZoom() {
42+
Image image = new Image(display, 10, 10);
43+
int zoom1 = 125;
44+
int zoom2 = 150;
45+
ImageData imageDataAtZoom1 = image.getImageData(125);
46+
ImageData imageDataAtZoom2 = image.getImageData(150);
47+
assertNotEquals("ImageData::height should not be the same for imageData at different zoom levels within the same autoScale zoom bounds", imageDataAtZoom1.height, imageDataAtZoom2.height);
48+
assertNotEquals("ImageData::width should not be the same for imageData at different zoom levels within the same autoScale zoom bounds", imageDataAtZoom1.width, imageDataAtZoom2.width);
49+
int correctedZoomForZoom1 = DPIUtil.getZoomForAutoscaleProperty(zoom1);
50+
int correctedZoomForZoom2 = DPIUtil.getZoomForAutoscaleProperty(zoom2);
51+
assertEquals("Zoom1 and Zoom2 should have the same corrected zoom", correctedZoomForZoom1, correctedZoomForZoom2);
52+
ImageData imageDataAtCorrectedZoom = image.getImageData(correctedZoomForZoom1);
53+
assertNotEquals("ImageData::height of imageData at zoom1 should not be the same as that of corrected zoom ", imageDataAtZoom1.height, imageDataAtCorrectedZoom.height);
54+
assertNotEquals("ImageData::width of imageData at zoom1 should not be the same as that of corrected zoom ", imageDataAtZoom1.width, imageDataAtCorrectedZoom.width);
55+
assertNotEquals("ImageData::height of imageData at zoom2 should not be the same as that of corrected zoom ", imageDataAtZoom2.height, imageDataAtCorrectedZoom.height);
56+
assertNotEquals("ImageData::width of imageData at zoom2 should not be the same as that of corrected zoom ", imageDataAtZoom2.width, imageDataAtCorrectedZoom.width);
57+
}
58+
3959
@Test
4060
public void testImageShouldHaveDimesionAsPerZoomLevel() {
4161
int zoom = DPIUtil.getDeviceZoom();

0 commit comments

Comments
 (0)