Skip to content

Commit 6170cfb

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 6170cfb

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
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_ImageData.java

Lines changed: 24 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,27 @@ public void test_setAlphaIII() {
799802
assertSWTProblem("Incorrect exception thrown for putWidth < 0", SWT.ERROR_INVALID_ARGUMENT, ex);
800803
}
801804

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

0 commit comments

Comments
 (0)