diff options
author | Lakshmi Shanmugam | 2016-01-29 12:02:41 +0000 |
---|---|---|
committer | Lakshmi Shanmugam | 2016-02-01 10:08:52 +0000 |
commit | 4aa8b7cd1d0aa2506b2d49ffbf45835e53b62ec7 (patch) | |
tree | 09efab89c5c09d8caab5e0ea77dd384d9d827f64 | |
parent | 04eaf497ebe64246e9059803cf0200de211ddc0d (diff) | |
download | eclipse.platform.swt-4aa8b7cd1d0aa2506b2d49ffbf45835e53b62ec7.tar.gz eclipse.platform.swt-4aa8b7cd1d0aa2506b2d49ffbf45835e53b62ec7.tar.xz eclipse.platform.swt-4aa8b7cd1d0aa2506b2d49ffbf45835e53b62ec7.zip |
Refactored Image.getImageData and sorted DPIUtil methods
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java | 25 | ||||
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DPIUtil.java | 228 |
2 files changed, 130 insertions, 123 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 fb95673dfb..eb3d395eff 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 @@ -10,10 +10,10 @@ *******************************************************************************/ package org.eclipse.swt.graphics; +import java.io.*; -import org.eclipse.swt.internal.cocoa.*; import org.eclipse.swt.*; -import java.io.*; +import org.eclipse.swt.internal.cocoa.*; /** * Instances of this class are graphics which have been prepared @@ -875,14 +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; + return _getImageData(); } -ImageData _getImageData() { +ImageData _getImageData () { + return _getImageData(100); +} + +ImageData _getImageData(int zoom) { NSAutoreleasePool pool = null; if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { @@ -953,7 +953,6 @@ ImageData _getImageData() { } /** -<<<<<<< HEAD * Returns an <code>ImageData</code> for specified zoom, based on the receiver * Modifications made to this <code>ImageData</code> will not affect the * Image. @@ -971,17 +970,15 @@ ImageData _getImageData() { * </ul> * * @see ImageData - * + * * @since 3.105 */ public ImageData getImageData (int zoom) { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - return DPIUtil.getImageData (this, zoom); + return _getImageData (zoom); } -/** -======= ->>>>>>> refs/remotes/origin/master +/** * Invokes platform specific functionality to allocate a new image. * <p> * <b>IMPORTANT:</b> This method is <em>not</em> part of the public 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 f634c7d667..601c190991 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 @@ -27,82 +27,52 @@ public class DPIUtil { private static boolean autoScale = true; /** - * Compute the zoom value based on the DPI value. - * - * @return zoom - */ -static int mapDPIToZoom (int dpi) { - int zoom; - if (dpi >= DPI_ZOOM_200) { - zoom = 200; - } else if (dpi >= DPI_ZOOM_150) { - zoom = 150; - } else { - zoom = 100; - } - return zoom; -} - -/** - * Gets Image file path at specified zoom level, if image is missing then - * fall-back to 100% image. If provider or fall-back image is not available, - * throw error. + * Auto-scale down ImageData */ -static String validateAndGetImagePathAtZoom (ImageFileNameProvider provider, int zoom, boolean[] found) { - if (provider == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); - String filename = provider.getImagePath (zoom); - found [0] = (filename != null); - /* If image is null when (zoom != 100%), fall-back to image at 100% zoom */ - if (zoom != 100 && !found [0]) filename = provider.getImagePath (100); - if (filename == null) SWT.error (SWT.ERROR_INVALID_ARGUMENT); - return filename; +public static ImageData autoScaleDown (ImageData imageData, Device device) { + if (!getAutoScale () || imageData == null || device == null) return imageData; + float scaleFactor = getScalingFactor (device); + return scaleFactor == 1 ? imageData + : imageData.scaledTo ((int)((float)imageData.width / scaleFactor), (int)((float)imageData.height / scaleFactor)); } /** - * Gets Image data at specified zoom level, if image is missing then - * fall-back to 100% image. If provider or fall-back image is not available, - * throw error. - */ -static ImageData validateAndGetImageDataAtZoom (ImageDataProvider provider, int zoom, boolean[] found) { - if (provider == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); - ImageData data = provider.getImageData (zoom); - found [0] = (data != null); - /* If image is null when (zoom != 100%), fall-back to image at 100% zoom */ - if (zoom != 100 && !found [0]) data = provider.getImageData (100); - if (data == null) SWT.error (SWT.ERROR_INVALID_ARGUMENT); - return data; -} -/** - * Gets scaling factor for the current Image - */ -public static float getscalingFactor (Image image) { - return ((float)image.getDevice ().getDeviceZoom ()) / 100f; -} -/** - * Auto-scale up int dimensions. + * Auto-scale down int dimensions. */ -public static int autoScaleUp (int size, Device device) { +public static int autoScaleDown (int size, Device device) { if (!getAutoScale () || device == null) return size; float scaleFactor = getScalingFactor (device); - return (int)(scaleFactor ==1 ? size : size * scaleFactor); + return (int)(scaleFactor ==1 ? size : size / scaleFactor); } + /** - * Auto-scale down int dimensions. + * Returns a new scaled down Point. */ -public static int autoScaleDown (int size, Device device) { - if (!getAutoScale () || device == null) return size; +public static Point autoScaleDown (Point point, Device device) { + if (!getAutoScale () || point == null || device == null) return point; float scaleFactor = getScalingFactor (device); - return (int)(scaleFactor ==1 ? size : size / scaleFactor); + if (scaleFactor == 1) return point; + Point scaledPoint = new Point (0,0); + scaledPoint.x = (int) (point.x / scaleFactor); + scaledPoint.y = (int) (point.y / scaleFactor); + return scaledPoint; } + /** - * Auto-scale up ImageData + * Returns a new scaled down Rectangle. */ -public static ImageData autoScaleUp (ImageData imageData, Device device) { - if (!getAutoScale () || imageData == null || device == null) return imageData; +public static Rectangle autoScaleDown (Rectangle rect, Device device) { + if (!getAutoScale () || rect == null || device == null) return rect; float scaleFactor = getScalingFactor (device); - return scaleFactor == 1 ? imageData - : imageData.scaledTo ((int)((float)imageData.width * scaleFactor), (int)((float)imageData.height * scaleFactor)); + if (scaleFactor == 1) return rect; + Rectangle scaledRect = new Rectangle (0,0,0,0); + scaledRect.x = (int) (rect.x / scaleFactor); + scaledRect.y = (int) (rect.y / scaleFactor); + scaledRect.width = (int) (rect.width / scaleFactor); + scaledRect.height = (int) (rect.height / scaleFactor); + return scaledRect; } + /** * Auto-scale image with ImageData */ @@ -111,43 +81,26 @@ private static ImageData autoScaleImageData (ImageData imageData, int targetZoom float scaleFactor = ((float) targetZoom)/((float) currentZoom); return imageData.scaledTo ((int)((float)imageData.width * scaleFactor), (int)((float)imageData.height * scaleFactor)); } + /** - * Auto-scale down ImageData + * Auto-scale up ImageData */ -public static ImageData autoScaleDown (ImageData imageData, Device device) { +public static ImageData autoScaleUp (ImageData imageData, Device device) { if (!getAutoScale () || imageData == null || device == null) return imageData; float scaleFactor = getScalingFactor (device); return scaleFactor == 1 ? imageData - : imageData.scaledTo ((int)((float)imageData.width / scaleFactor), (int)((float)imageData.height / scaleFactor)); -} -/** - * Returns a new scaled up Rectangle. - */ -public static Rectangle autoScaleUp (Rectangle rect, Device device) { - if (!getAutoScale () || rect == null || device == null) return rect; - float scaleFactor = getScalingFactor (device); - if (scaleFactor == 1) return rect; - Rectangle scaledRect = new Rectangle (0,0,0,0); - scaledRect.x = (int) (rect.x * scaleFactor); - scaledRect.y = (int) (rect.y * scaleFactor); - scaledRect.width = (int) (rect.width * scaleFactor); - scaledRect.height = (int) (rect.height * scaleFactor); - return scaledRect; + : imageData.scaledTo ((int)((float)imageData.width * scaleFactor), (int)((float)imageData.height * scaleFactor)); } + /** - * Returns a new scaled down Rectangle. + * Auto-scale up int dimensions. */ -public static Rectangle autoScaleDown (Rectangle rect, Device device) { - if (!getAutoScale () || rect == null || device == null) return rect; +public static int autoScaleUp (int size, Device device) { + if (!getAutoScale () || device == null) return size; float scaleFactor = getScalingFactor (device); - if (scaleFactor == 1) return rect; - Rectangle scaledRect = new Rectangle (0,0,0,0); - scaledRect.x = (int) (rect.x / scaleFactor); - scaledRect.y = (int) (rect.y / scaleFactor); - scaledRect.width = (int) (rect.width / scaleFactor); - scaledRect.height = (int) (rect.height / scaleFactor); - return scaledRect; + return (int)(scaleFactor == 1 ? size : size * scaleFactor); } + /** * Returns a new scaled up Point. */ @@ -160,31 +113,25 @@ public static Point autoScaleUp (Point point, Device device) { scaledPoint.y = (int) (point.y * scaleFactor); return scaledPoint; } + /** - * Returns a new scaled down Point. + * Returns a new scaled up Rectangle. */ -public static Point autoScaleDown (Point point, Device device) { - if (!getAutoScale () || point == null || device == null) return point; +public static Rectangle autoScaleUp (Rectangle rect, Device device) { + if (!getAutoScale () || rect == null || device == null) return rect; float scaleFactor = getScalingFactor (device); - if (scaleFactor == 1) return point; - Point scaledPoint = new Point (0,0); - scaledPoint.x = (int) (point.x / scaleFactor); - scaledPoint.y = (int) (point.y / scaleFactor); - return scaledPoint; + if (scaleFactor == 1) return rect; + Rectangle scaledRect = new Rectangle (0,0,0,0); + scaledRect.x = (int) (rect.x * scaleFactor); + scaledRect.y = (int) (rect.y * scaleFactor); + scaledRect.width = (int) (rect.width * scaleFactor); + scaledRect.height = (int) (rect.height * scaleFactor); + return scaledRect; } -/** - * Returns a new rectangle as per the 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); - returnRect.width = (int) (rect.width * scaleFactor); - returnRect.height = (int) (rect.height * scaleFactor); - return returnRect; +public static boolean getAutoScale () { + return autoScale; } + /** * Returns an <code>ImageData</code> for specified zoom. */ @@ -214,6 +161,13 @@ static ImageData getImageData (Image image, int zoom) { } /** + * Gets scaling factor for the current Image + */ +public static float getscalingFactor (Image image) { + return ((float)image.getDevice ().getDeviceZoom ()) / 100f; +} + +/** * Returns Scaling factor from the display * @return float scaling factor */ @@ -225,8 +179,64 @@ public static float getScalingFactor (Device device) { return scalingFactor; } -public static boolean getAutoScale () { - return autoScale; +/** + * Compute the zoom value based on the DPI value. + * + * @return zoom + */ +static int mapDPIToZoom (int dpi) { + int zoom; + if (dpi >= DPI_ZOOM_200) { + zoom = 200; + } else if (dpi >= DPI_ZOOM_150) { + zoom = 150; + } else { + zoom = 100; + } + return zoom; +} +/** + * Returns a new rectangle as per the 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); + returnRect.width = (int) (rect.width * scaleFactor); + returnRect.height = (int) (rect.height * scaleFactor); + return returnRect; +} + +/** + * Gets Image data at specified zoom level, if image is missing then + * fall-back to 100% image. If provider or fall-back image is not available, + * throw error. + */ +static ImageData validateAndGetImageDataAtZoom (ImageDataProvider provider, int zoom, boolean[] found) { + if (provider == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); + ImageData data = provider.getImageData (zoom); + found [0] = (data != null); + /* If image is null when (zoom != 100%), fall-back to image at 100% zoom */ + if (zoom != 100 && !found [0]) data = provider.getImageData (100); + if (data == null) SWT.error (SWT.ERROR_INVALID_ARGUMENT); + return data; +} + +/** + * Gets Image file path at specified zoom level, if image is missing then + * fall-back to 100% image. If provider or fall-back image is not available, + * throw error. + */ +static String validateAndGetImagePathAtZoom (ImageFileNameProvider provider, int zoom, boolean[] found) { + if (provider == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); + String filename = provider.getImagePath (zoom); + found [0] = (filename != null); + /* If image is null when (zoom != 100%), fall-back to image at 100% zoom */ + if (zoom != 100 && !found [0]) filename = provider.getImagePath (100); + if (filename == null) SWT.error (SWT.ERROR_INVALID_ARGUMENT); + return filename; } } |