Skip to content

Commit 5a22598

Browse files
committed
[MacOS] Fix getBounds for Image based on ImageGcDrawer
When running with a device zoom of 200, the bounds of images created with an ImageGcDrawer have double the expected size. The reason is that the image is initialized with image data at zoom 200, from which also the width and height are retrieved. The used initialization logic does, however, always expect image data at zoom 100 to be passed. This change fixes the initialization of images based on ImageGcDrawer to create both the 100 and 200 image data version and correctly initialize the image metadata with the former. The according test case is extended to validate the bounds in pixels of images not only against the bounds if the image but also against the expected/initial bounds.
1 parent 29bae82 commit 5a22598

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,13 +874,20 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
874874
this.imageGcDrawer = imageGcDrawer;
875875
this.width = width;
876876
this.height = height;
877-
ImageData data = drawWithImageGcDrawer(imageGcDrawer, width, height, DPIUtil.getDeviceZoom());
877+
ImageData data = drawWithImageGcDrawer(imageGcDrawer, width, height, 100);
878878
if (data == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
879879
NSAutoreleasePool pool = null;
880880
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
881881
try {
882882
init (data);
883-
init ();
883+
init ();
884+
ImageData data2x = drawWithImageGcDrawer(imageGcDrawer, width, height, 200);
885+
if (data2x != null) {
886+
alphaInfo_200 = new AlphaInfo();
887+
NSBitmapImageRep rep = createRepresentation (data2x, alphaInfo_200);
888+
handle.addRepresentation(rep);
889+
rep.release();
890+
}
884891
} finally {
885892
if (pool != null) pool.release();
886893
}

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,8 @@ public void test_getBounds() {
759759
@SuppressWarnings("deprecation")
760760
@Test
761761
public void test_getBoundsInPixels() {
762-
Rectangle bounds = new Rectangle(0, 0, 10, 20);
763-
Image image = new Image(display, bounds.width, bounds.height);
762+
Rectangle initialBounds = new Rectangle(0, 0, 10, 20);
763+
Image image = new Image(display, initialBounds.width, initialBounds.height);
764764
image.dispose();
765765
try {
766766
image.getBoundsInPixels();
@@ -770,38 +770,43 @@ public void test_getBoundsInPixels() {
770770
}
771771

772772
// creates bitmap image
773-
image = new Image(display, bounds.width, bounds.height);
773+
image = new Image(display, initialBounds.width, initialBounds.height);
774774
Rectangle boundsInPixels = image.getBoundsInPixels();
775+
Rectangle bounds = image.getBounds();
775776
image.dispose();
776-
assertEquals(":a: Image.getBoundsInPixels method doesn't return bounds in Pixel values.", boundsInPixels, DPIUtil.autoScaleUp(bounds));
777+
assertEquals("Image.getBounds method doesn't return original bounds.", initialBounds, bounds);
778+
assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.autoScaleUp(initialBounds), boundsInPixels);
777779

778780
// create icon image
779-
ImageData imageData = new ImageData(bounds.width, bounds.height, 1, new PaletteData(new RGB[] {new RGB(0, 0, 0)}));
781+
ImageData imageData = new ImageData(initialBounds.width, initialBounds.height, 1, new PaletteData(new RGB[] {new RGB(0, 0, 0)}));
780782
image = new Image(display, imageData);
781783
boundsInPixels = image.getBoundsInPixels();
784+
bounds = image.getBounds();
782785
image.dispose();
783-
assertEquals(":b: Image.getBoundsInPixels method doesn't return bounds in Pixel values.", boundsInPixels, DPIUtil.autoScaleUp(bounds));
786+
assertEquals("Image.getBounds method doesn't return original bounds.", initialBounds, bounds);
787+
assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.autoScaleUp(initialBounds), boundsInPixels);
784788

785789
// create image with FileNameProvider
786790
image = new Image(display, imageFileNameProvider);
787791
boundsInPixels = image.getBoundsInPixels();
788792
bounds = image.getBounds();
789793
image.dispose();
790-
assertEquals(":c: Image.getBoundsInPixels method doesn't return bounds in Pixel values.", boundsInPixels, DPIUtil.autoScaleUp(bounds));
794+
assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.autoScaleUp(bounds), boundsInPixels);
791795

792796
// create image with ImageDataProvider
793797
image = new Image(display, imageDataProvider);
794798
boundsInPixels = image.getBoundsInPixels();
795799
bounds = image.getBounds();
796800
image.dispose();
797-
assertEquals(":d: Image.getBoundsInPixels method doesn't return bounds in Pixel values.", boundsInPixels, DPIUtil.autoScaleUp(bounds));
801+
assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.autoScaleUp(bounds), boundsInPixels);
798802

799803
// create image with ImageGcDrawer
800-
image = new Image(display, imageGcDrawer, bounds.width, bounds.height);
804+
image = new Image(display, imageGcDrawer, initialBounds.width, initialBounds.height);
801805
boundsInPixels = image.getBoundsInPixels();
802806
bounds = image.getBounds();
803807
image.dispose();
804-
assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values for ImageGcDrawer.", boundsInPixels, DPIUtil.autoScaleUp(bounds));
808+
assertEquals("Image.getBounds method doesn't return original bounds.", initialBounds, bounds);
809+
assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values for ImageGcDrawer.", DPIUtil.autoScaleUp(initialBounds), boundsInPixels);
805810
}
806811

807812
@SuppressWarnings("deprecation")

0 commit comments

Comments
 (0)