diff options
author | Niraj Modi | 2016-01-22 08:35:31 +0000 |
---|---|---|
committer | Niraj Modi | 2016-01-22 08:35:31 +0000 |
commit | bfc1e3c65b70473217ef81110d736a14f49170d3 (patch) | |
tree | dc24c7c73e5b798914e0926c34016cc9d82bafbf | |
parent | 02caa9b7943cf7f6784aeb13acbb7c8eaaf8399e (diff) | |
download | eclipse.platform.swt-bfc1e3c65b70473217ef81110d736a14f49170d3.tar.gz eclipse.platform.swt-bfc1e3c65b70473217ef81110d736a14f49170d3.tar.xz eclipse.platform.swt-bfc1e3c65b70473217ef81110d736a14f49170d3.zip |
Bug 481315 (Part 3)- [Graphics] Implement new HighDPI APIs
Image#getImageData(zoom) and Image#getBounds(zoom)
Change-Id: I2d37fac14e61d87c5b3e1dc6707aa965ed8d666f
Signed-off-by: Niraj Modi <niraj.modi@in.ibm.com>
4 files changed, 85 insertions, 46 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java index a84735fdb9..6cf8cac148 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java @@ -811,7 +811,7 @@ public Color getBackground() { * have x and y values of 0, and the width and height of the * image. * - * @return a rectangle specifying the currently used image's bounds. + * @return a rectangle specifying the image's bounds at 100% zoom. * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> @@ -853,8 +853,7 @@ public Rectangle getBounds(int zoom) { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); Rectangle bounds = getBounds(); if (bounds != null && zoom > 100) { - float scaleFactor = (float)zoom / 100f; - bounds = DPIUtil.scale(bounds, scaleFactor); + bounds = DPIUtil.scale(bounds, zoom, 100); } return bounds; } @@ -864,7 +863,8 @@ public Rectangle getBounds(int zoom) { * Modifications made to this <code>ImageData</code> will not * affect the Image. * - * @return an <code>ImageData</code> containing the image's data and attributes + * @return an <code>ImageData</code> containing the image's data and + * attributes at 100% zoom level. * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> @@ -875,6 +875,14 @@ public Rectangle getBounds(int zoom) { */ public ImageData getImageData() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + ImageData imageData = _getImageData(); + if (imageData != null && getEnableAutoScaling()) { + data = DPIUtil.autoScaleImageData(data, 100, currentDeviceZoom); + } + return imageData; +} + +ImageData _getImageData() { NSAutoreleasePool pool = null; if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DPIUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DPIUtil.java index 4567afba30..87de134860 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DPIUtil.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DPIUtil.java @@ -80,23 +80,27 @@ class DPIUtil { /** * Auto-scale image with ImageData */ - static ImageData autoScaleImageData (ImageData imageData, int deviceZoom) { - float scaleFactor = ((float) deviceZoom)/((float) 100); + static ImageData autoScaleImageData (ImageData imageData, int targetZoom, int currentZoom) { + if (imageData == null || targetZoom == currentZoom) return imageData; + float scaleFactor = ((float) targetZoom)/((float) currentZoom); return imageData.scaledTo((int)((float)imageData.width * scaleFactor), (int)((float)imageData.height * scaleFactor)); } /** * Auto-scale image with ImageFileName */ - static ImageData autoScaleImageFileName (String filename, int deviceZoom) { - ImageData data = new ImageData(filename); - return autoScaleImageData(data, deviceZoom); + static ImageData autoScaleImageFileName (String filename, int targetZoom, int currentZoom) { + if (filename == null) return null; + ImageData imageData = new ImageData(filename); + return autoScaleImageData(imageData, targetZoom, currentZoom); } /** * Returns a new rectangle as per the scaleFactor. */ - static Rectangle scale(Rectangle rect, float scaleFactor) { + static Rectangle scale(Rectangle rect, int targetZoom, int currentZoom) { + if (rect == null || targetZoom == currentZoom) return rect; + float scaleFactor = ((float)targetZoom) / (float)currentZoom; Rectangle returnRect = new Rectangle (0,0,0,0); returnRect.x = (int) (rect.x * scaleFactor); returnRect.y = (int) (rect.y * scaleFactor); @@ -109,20 +113,20 @@ class DPIUtil { * Returns an <code>ImageData</code> for specified zoom. */ static ImageData getImageData (Image image, int zoom) { - if (image.currentDeviceZoom == zoom) return image.getImageData(); + if (image.currentDeviceZoom == zoom) return image._getImageData(); ImageData imageData = null; if (image.imageDataProvider != null) { boolean[] found = new boolean[1]; imageData = DPIUtil.validateAndGetImageDataAtZoom (image.imageDataProvider, zoom, found); if (!found[0]) { - imageData = DPIUtil.autoScaleImageData(imageData, zoom); + imageData = DPIUtil.autoScaleImageData(imageData, zoom, 100); } } else if (image.imageFileNameProvider != null) { boolean[] found = new boolean[1]; String filename = DPIUtil.validateAndGetImagePathAtZoom (image.imageFileNameProvider, zoom, found); if (!found[0]) { - imageData = DPIUtil.autoScaleImageFileName(filename, zoom); + imageData = DPIUtil.autoScaleImageFileName(filename, zoom, 100); } else { imageData = new ImageData (filename); @@ -130,9 +134,8 @@ class DPIUtil { } else { /* Get ImageData at currentZoom and scale it to specified zoom. */ - imageData = image.getImageData(); - float scaleFactor = (float)zoom / (float)image.currentDeviceZoom; - imageData.scaledTo((int)(imageData.width * scaleFactor), (int)(imageData.height * scaleFactor)); + imageData = image._getImageData(); + imageData = DPIUtil.autoScaleImageData(imageData, zoom, image.currentDeviceZoom); } return imageData; } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java index cf5e84d0ba..985f8b56d7 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java @@ -553,8 +553,7 @@ public Image(Device device, Rectangle bounds) { if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (getEnableAutoScaling ()) { currentDeviceZoom = getDeviceZoom(); - float scaleFactor = ((float)currentDeviceZoom) / 100f; - Rectangle bounds1 = DPIUtil.scale(bounds, scaleFactor); + Rectangle bounds1 = DPIUtil.scale(bounds, currentDeviceZoom, 100); init(bounds1.width, bounds1.height); autoScaled = true; } else { @@ -592,7 +591,7 @@ public Image(Device device, ImageData data) { if (this.getEnableAutoScaling()) { currentDeviceZoom = getDeviceZoom(); if (!data.scaled) { - data = DPIUtil.autoScaleImageData(data, currentDeviceZoom); + data = DPIUtil.autoScaleImageData(data, currentDeviceZoom, 100); } autoScaled = true; } @@ -640,10 +639,10 @@ public Image(Device device, ImageData source, ImageData mask) { if (getEnableAutoScaling()){ currentDeviceZoom = getDeviceZoom(); if (!source.scaled) { - source = DPIUtil.autoScaleImageData(source, currentDeviceZoom); + source = DPIUtil.autoScaleImageData(source, currentDeviceZoom, 100); } if (!mask.scaled) { - mask = DPIUtil.autoScaleImageData(mask, currentDeviceZoom); + mask = DPIUtil.autoScaleImageData(mask, currentDeviceZoom, 100); } autoScaled = true; } @@ -713,7 +712,7 @@ public Image(Device device, InputStream stream) { ImageData data = new ImageData(stream); if (this.getEnableAutoScaling()) { currentDeviceZoom = getDeviceZoom(); - data = DPIUtil.autoScaleImageData(data, currentDeviceZoom); + data = DPIUtil.autoScaleImageData(data, currentDeviceZoom, 100); autoScaled = true; } init(data); @@ -759,7 +758,7 @@ public Image(Device device, String filename) { ImageData data = new ImageData(filename); if (this.getEnableAutoScaling()) { currentDeviceZoom = getDeviceZoom(); - data = DPIUtil.autoScaleImageData(data, currentDeviceZoom); + data = DPIUtil.autoScaleImageData(data, currentDeviceZoom, 100); autoScaled = true; } init(data); @@ -816,7 +815,7 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) { init(data); } } else { - ImageData resizedData = DPIUtil.autoScaleImageFileName(filename, currentDeviceZoom); + ImageData resizedData = DPIUtil.autoScaleImageFileName(filename, currentDeviceZoom, 100); init(resizedData); } init (); @@ -860,7 +859,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) { if (found[0]) { init (data); } else { - ImageData resizedData = DPIUtil.autoScaleImageData(data, currentDeviceZoom); + ImageData resizedData = DPIUtil.autoScaleImageData(data, currentDeviceZoom, 100); init (resizedData); } init (); @@ -893,7 +892,7 @@ boolean refreshImageForZoom () { if (!found[0]) { /* Release current native resources */ destroy (); - ImageData resizedData = DPIUtil.autoScaleImageFileName(filename, deviceZoomLevel); + ImageData resizedData = DPIUtil.autoScaleImageFileName(filename, deviceZoomLevel, 100); init(resizedData); init (); refreshed = true; @@ -916,7 +915,7 @@ boolean refreshImageForZoom () { if (!found[0]) { /* Release current native resources */ destroy (); - ImageData resizedData = DPIUtil.autoScaleImageData(data, deviceZoomLevel); + ImageData resizedData = DPIUtil.autoScaleImageData(data, deviceZoomLevel, 100); init(resizedData); init(); refreshed = true; @@ -1333,7 +1332,7 @@ public Color getBackground() { * have x and y values of 0, and the width and height of the * image. * - * @return a rectangle specifying the currently used image's bounds. + * @return a rectangle specifying the image's bounds at 100% zoom. * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> @@ -1342,6 +1341,14 @@ public Color getBackground() { */ public Rectangle getBounds() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + Rectangle bounds = _getBounds(); + if (getEnableAutoScaling()) { + bounds = DPIUtil.scale(bounds, 100, currentDeviceZoom); + } + return bounds; +} + +Rectangle _getBounds() { if (width != -1 && height != -1) { return new Rectangle(0, 0, width, height); } @@ -1375,8 +1382,7 @@ public Rectangle getBounds(int zoom) { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); Rectangle bounds = getBounds(); if (bounds != null) { - float scaleFactor = (float)zoom / (float)currentDeviceZoom; - bounds = DPIUtil.scale(bounds, scaleFactor); + bounds = DPIUtil.scale(bounds, zoom, currentDeviceZoom); } return bounds; } @@ -1386,7 +1392,8 @@ public Rectangle getBounds(int zoom) { * Modifications made to this <code>ImageData</code> will not * affect the Image. * - * @return an <code>ImageData</code> containing the image's data and attributes + * @return an <code>ImageData</code> containing the image's data and + * attributes at 100% zoom level. * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> @@ -1397,7 +1404,14 @@ public Rectangle getBounds(int zoom) { */ public ImageData getImageData() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - + ImageData imageData = _getImageData(); + if (imageData != null && getEnableAutoScaling()) { + data = DPIUtil.autoScaleImageData(data, 100, currentDeviceZoom); + } + return imageData; +} + +ImageData _getImageData() { if (OS.USE_CAIRO) { long /*int*/ surface = ImageList.convertSurface(this); int format = Cairo.cairo_image_surface_get_format(surface); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java index 682365c013..dcc6458134 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java @@ -464,8 +464,7 @@ public Image(Device device, Rectangle bounds) { if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (getEnableAutoScaling ()) { currentDeviceZoom = getDeviceZoom(); - float scaleFactor = ((float)currentDeviceZoom) / 100f; - Rectangle bounds1 = DPIUtil.scale(bounds, scaleFactor); + Rectangle bounds1 = DPIUtil.scale(bounds, currentDeviceZoom, 100); init(bounds1.width, bounds1.height); } else { init(bounds.width, bounds.height); @@ -501,7 +500,7 @@ public Image(Device device, ImageData data) { if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (getEnableAutoScaling()) { currentDeviceZoom = getDeviceZoom(); - data = DPIUtil.autoScaleImageData(data, currentDeviceZoom); + data = DPIUtil.autoScaleImageData(data, currentDeviceZoom, 100); } init(data); init(); @@ -546,8 +545,8 @@ public Image(Device device, ImageData source, ImageData mask) { } if (getEnableAutoScaling()){ currentDeviceZoom = getDeviceZoom(); - source = DPIUtil.autoScaleImageData(source, currentDeviceZoom); - mask = DPIUtil.autoScaleImageData(mask, currentDeviceZoom); + source = DPIUtil.autoScaleImageData(source, currentDeviceZoom, 100); + mask = DPIUtil.autoScaleImageData(mask, currentDeviceZoom, 100); } mask = ImageData.convertMask(mask); init(this.device, this, source, mask); @@ -692,7 +691,7 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) { initNative (fileName); if (this.handle == 0) init(new ImageData (fileName)); } else { - ImageData resizedData = DPIUtil.autoScaleImageFileName(fileName, currentDeviceZoom); + ImageData resizedData = DPIUtil.autoScaleImageFileName(fileName, currentDeviceZoom, 100); init(resizedData); } init(); @@ -736,7 +735,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) { if (found[0]) { init(data); } else { - ImageData resizedData = DPIUtil.autoScaleImageData(data, currentDeviceZoom); + ImageData resizedData = DPIUtil.autoScaleImageData(data, currentDeviceZoom, 100); init (resizedData); } init(); @@ -766,7 +765,7 @@ boolean refreshImageForZoom () { if (!found[0]) { /* Release current native resources */ destroy (); - ImageData resizedData = DPIUtil.autoScaleImageFileName(filename, deviceZoomLevel); + ImageData resizedData = DPIUtil.autoScaleImageFileName(filename, deviceZoomLevel, 100); init(resizedData); init (); refreshed = true; @@ -789,7 +788,7 @@ boolean refreshImageForZoom () { if (!found[0]) { /* Release current native resources */ destroy (); - ImageData resizedData = DPIUtil.autoScaleImageData(data, deviceZoomLevel); + ImageData resizedData = DPIUtil.autoScaleImageData(data, deviceZoomLevel, 100); init(resizedData); init(); refreshed = true; @@ -1382,7 +1381,7 @@ public Color getBackground() { * have x and y values of 0, and the width and height of the * image. * - * @return a rectangle specifying the currently used image's bounds. + * @return a rectangle specifying the image's bounds at 100% zoom. * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> @@ -1391,6 +1390,14 @@ public Color getBackground() { */ public Rectangle getBounds() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + Rectangle bounds = _getBounds(); + if (getEnableAutoScaling()) { + bounds = DPIUtil.scale(bounds, 100, currentDeviceZoom); + } + return bounds; +} + +Rectangle _getBounds() { if (width != -1 && height != -1) { return new Rectangle(0, 0, width, height); } @@ -1438,10 +1445,9 @@ public Rectangle getBounds() { */ public Rectangle getBounds(int zoom) { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - Rectangle bounds = getBounds(); + Rectangle bounds = _getBounds(); if (bounds != null && zoom != currentDeviceZoom) { - float scaleFactor = (float)zoom / (float)currentDeviceZoom; - bounds = DPIUtil.scale(bounds, scaleFactor); + bounds = DPIUtil.scale(bounds, zoom, currentDeviceZoom); } return bounds; } @@ -1452,7 +1458,7 @@ public Rectangle getBounds(int zoom) { * affect the Image. * * @return an <code>ImageData</code> containing the image's data and - * attributes at current zoom level. + * attributes at 100% zoom level. * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> @@ -1463,6 +1469,14 @@ public Rectangle getBounds(int zoom) { */ public ImageData getImageData() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + ImageData imageData = _getImageData(); + if (imageData != null && getEnableAutoScaling()) { + data = DPIUtil.autoScaleImageData(data, 100, currentDeviceZoom); + } + return imageData; +} + +ImageData _getImageData() { BITMAP bm; int depth, width, height; switch (type) { |