Skip to content

Commit 9a302a0

Browse files
committed
A strict check 200% image data is double the size of 100% image data
A strict check to be used only for debugging means, this ensures the imageData is scaled linearly for 100 and 200 zooms.
1 parent 649bb49 commit 9a302a0

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,26 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
610610
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null,
611611
": ImageDataProvider [" + imageDataProvider + "] returns null ImageData at 100% zoom.");
612612
}
613+
if(Device.strictChecks) {
614+
validateLinearScaling(imageDataProvider);
615+
}
613616
init();
614617
this.device.registerResourceWithZoomSupport(this);
615618
}
616619

620+
private void validateLinearScaling(ImageDataProvider provider) {
621+
ImageData data100 = provider.getImageData(100);
622+
ImageData data200 = provider.getImageData(200);
623+
624+
if (data200 == null) {
625+
return;
626+
}
627+
628+
if (data200.width != 2 * data100.width || data200.height != 2 * data100.height) {
629+
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, "ImageData should be linearly scaled across zooms.");
630+
}
631+
}
632+
617633
/**
618634
* The provided ImageGcDrawer will be called on demand whenever a new variant of the
619635
* Image for an additional zoom is required. Depending on the OS-specific implementation

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,13 +1031,18 @@ public void test_imageDataIsCached() {
10311031
public void test_imageDataSameViaDifferentProviders() {
10321032
assumeFalse("Cocoa generates inconsistent image data", SwtTestUtil.isCocoa);
10331033
String imagePath = getPath("collapseall.png");
1034-
ImageFileNameProvider imageFileNameProvider = __ -> {
1035-
return imagePath;
1034+
ImageFileNameProvider imageFileNameProvider = zoom -> {
1035+
if (zoom == 100)
1036+
return imagePath;
1037+
else
1038+
return null;
10361039
};
1037-
ImageDataProvider dataProvider = __ -> {
1038-
try (InputStream imageStream = Files.newInputStream(Path.of(imagePath))) {
1039-
return new ImageData(imageStream);
1040-
} catch (IOException e) {
1040+
ImageDataProvider dataProvider = zoom -> {
1041+
if (zoom == 100) {
1042+
try (InputStream imageStream = Files.newInputStream(Path.of(imagePath))) {
1043+
return new ImageData(imageStream);
1044+
} catch (IOException e) {
1045+
}
10411046
}
10421047
return null;
10431048
};

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_ImageData.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.junit.Assert.assertNotNull;
2525
import static org.junit.Assert.assertNull;
2626
import static org.junit.Assert.assertThrows;
27+
import static org.junit.Assert.fail;
2728

2829
import java.io.IOException;
2930
import java.io.InputStream;
@@ -32,8 +33,10 @@
3233

3334
import org.eclipse.swt.SWT;
3435
import org.eclipse.swt.SWTException;
36+
import org.eclipse.swt.graphics.GC;
3537
import org.eclipse.swt.graphics.Image;
3638
import org.eclipse.swt.graphics.ImageData;
39+
import org.eclipse.swt.graphics.ImageDataProvider;
3740
import org.eclipse.swt.graphics.PaletteData;
3841
import org.eclipse.swt.graphics.RGB;
3942
import org.eclipse.swt.tests.graphics.ImageDataTestHelper;
@@ -799,6 +802,25 @@ public void test_setAlphaIII() {
799802
assertSWTProblem("Incorrect exception thrown for putWidth < 0", SWT.ERROR_INVALID_ARGUMENT, ex);
800803
}
801804

805+
@Test
806+
public void test_drawImageFailsWithNonScalingImageDataProviderWhenstrictCheckEnabled() {
807+
Display display = Display.getDefault();
808+
GC gc = new GC(display);
809+
try {
810+
ImageDataProvider wrongDataProvider = (zoom) -> new ImageData(16, 16, 32, new PaletteData());
811+
Image image = new Image(display, wrongDataProvider);
812+
gc.drawImage(image, 0, 0, 16, 16, 0, 0, 16, 16);
813+
image.dispose();
814+
if (System.getProperty("org.eclipse.swt.internal.enableStrictChecks") != null) {
815+
fail("Expected an exception due to non-linearly scaled image data provider");
816+
}
817+
} catch (IllegalArgumentException | SWTException e) {
818+
} finally {
819+
gc.dispose();
820+
display.dispose();
821+
}
822+
}
823+
802824
@Test
803825
public void test_setPixelIII() {
804826
int value;

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_internal_SVGRasterizer.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,23 @@ public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvid
5555
@Test
5656
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider() {
5757
ImageDataProvider validImageDataProvider = zoom -> {
58-
String fileName = "collapseall.svg";
59-
return new ImageData(getPath(fileName));
58+
if (zoom == 100) {
59+
String fileName = "collapseall.svg";
60+
return new ImageData(getPath(fileName));
61+
} else {
62+
return null;
63+
}
6064
};
6165
Image image = new Image(Display.getDefault(), validImageDataProvider);
6266
image.dispose();
6367

6468
ImageDataProvider corruptImageDataProvider = zoom -> {
65-
String fileName = "corrupt.svg";
66-
return new ImageData(getPath(fileName));
69+
if (zoom == 100) {
70+
String fileName = "corrupt.svg";
71+
return new ImageData(getPath(fileName));
72+
} else {
73+
return null;
74+
}
6775
};
6876
SWTException e = assertThrows(SWTException.class,
6977
() -> new Image(Display.getDefault(), corruptImageDataProvider));

0 commit comments

Comments
 (0)