Skip to content

Commit c49cee7

Browse files
amartya4256HeikoKlare
authored andcommitted
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 cec96e9 commit c49cee7

File tree

2 files changed

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

+21
-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.containsKey(zoom)) {
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: 14 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,19 @@ public void setUp() {
3637
display = Display.getDefault();
3738
}
3839

40+
@Test
41+
public void testImageDataForDifferentFractionalZoomsShouldBeDifferent() {
42+
Image image = new Image(display, 10, 10);
43+
int zoom1 = 125;
44+
int zoom2 = 150;
45+
ImageData imageDataAtZoom1 = image.getImageData(zoom1);
46+
ImageData imageDataAtZoom2 = image.getImageData(zoom2);
47+
assertNotEquals("ImageData::height should not be the same for imageData at different zoom levels",
48+
imageDataAtZoom1.height, imageDataAtZoom2.height);
49+
assertNotEquals("ImageData::width should not be the same for imageData at different zoom levels",
50+
imageDataAtZoom1.width, imageDataAtZoom2.width);
51+
}
52+
3953
@Test
4054
public void testImageShouldHaveDimesionAsPerZoomLevel() {
4155
int zoom = DPIUtil.getDeviceZoom();

0 commit comments

Comments
 (0)