Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSravan Kumar Lakkimsetti2019-02-08 01:00:30 -0500
committerTill Brychcy2019-05-14 07:34:36 -0400
commit8b8534bd4e8b9d7e275fe3026fa6ea8f5cdc4e16 (patch)
treed1de9afeed0a859f0b88154a7b1932beb7fae71c
parent88066e5ed8a844c6bef6b908c0ec33bd03056ba8 (diff)
downloadeclipse.platform.swt-8b8534bd4e8b9d7e275fe3026fa6ea8f5cdc4e16.tar.gz
eclipse.platform.swt-8b8534bd4e8b9d7e275fe3026fa6ea8f5cdc4e16.tar.xz
eclipse.platform.swt-8b8534bd4e8b9d7e275fe3026fa6ea8f5cdc4e16.zip
Bug 489451 - [hidpi][API] Provide API to draw on hi-res Images
using GC GC now draws with appropriate size based of scalefactor. Refactored the code Added code to handle non bitmap images like TIFF Change-Id: I4882f3ce0a4a24b4ba09fcc861465bb253a1ec28 Signed-off-by: Sravan Kumar Lakkimsetti <sravankumarl@in.ibm.com>
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java117
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/internal/graphics/ImageUtil.java58
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Canvas.java7
3 files changed, 128 insertions, 54 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 119c7a1418..5eadda37e9 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -19,6 +19,7 @@ import java.io.*;
import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.cocoa.*;
+import org.eclipse.swt.internal.graphics.*;
/**
* Instances of this class are graphics which have been prepared
@@ -1151,15 +1152,35 @@ NSBitmapImageRep getRepresentation_200 () {
NSArray reps = handle.representations();
NSSize size = handle.size();
long count = reps.count();
+ NSBitmapImageRep bestRep = null;
+ int width = (int)size.width * 2;
+ int height = (int)size.height * 2;
+ NSBitmapImageRep rep;
for (int i = 0; i < count; i++) {
- NSBitmapImageRep rep = new NSBitmapImageRep(reps.objectAtIndex(i));
- int width = (int)size.width * 2;
- int height = (int)size.height * 2;
+ rep = new NSBitmapImageRep(reps.objectAtIndex(i));
if ((width == rep.pixelsWide() && height == rep.pixelsHigh())) {
if (rep.isKindOfClass(OS.class_NSBitmapImageRep)) {
return rep;
}
+ if (bestRep == null) {
+ bestRep = rep;
+ }
+ }
+
+ }
+ if (bestRep != null) {
+ bestRep.retain();
+ for (int i = 0; i < count; i++) {
+ handle.removeRepresentation(new NSImageRep(handle.representations().objectAtIndex(0)));
}
+ handle.addRepresentation(bestRep);
+ NSBitmapImageRep newRep = (NSBitmapImageRep)new NSBitmapImageRep().alloc();
+ newRep = newRep.initWithData(handle.TIFFRepresentation());
+ handle.addRepresentation(newRep);
+ handle.removeRepresentation(bestRep);
+ bestRep.release();
+ newRep.release();
+ return newRep;
}
return null;
}
@@ -1343,37 +1364,28 @@ public ImageData getImageData(int zoom) {
}
handle.addRepresentation(imageRep);
- NSSize size = handle.size();
- imageRep = (NSBitmapImageRep)new NSBitmapImageRep().alloc();
- imageRep = imageRep.initWithBitmapDataPlanes(0, (int) size.width, (int) size.height, 8, 3, false, false, OS.NSDeviceRGBColorSpace, OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, (int) size.width * 4, 32);
- C.memset(imageRep.bitmapData(), 0xFF, (int) size.width * (int)size.height * 4);
- NSGraphicsContext context = NSGraphicsContext.graphicsContextWithBitmapImageRep(imageRep);
- NSGraphicsContext.static_saveGraphicsState();
- context.setImageInterpolation(OS.NSImageInterpolationDefault);
- NSGraphicsContext.setCurrentContext(context);
- NSRect target = new NSRect();
- target.width = size.width;
- target.height = size.height;
- NSRect sourceRect = new NSRect();
- sourceRect.width = 0;
- sourceRect.height = 0;
- handle.drawInRect(target, sourceRect, OS.NSCompositeCopy, 1);
- NSGraphicsContext.static_restoreGraphicsState();
+ NSSize targetSize = handle.size();
+ imageRep = createImageRep(targetSize);
return _getImageData(imageRep, alphaInfo_100);
}
}
}
if (zoom == 200) {
NSBitmapImageRep imageRep200 = getRepresentation_200();
- if (imageRep200 != null) {
- if (alphaInfo_100.alphaData != null && alphaInfo_200 != null) {
- if (alphaInfo_200.alphaData == null) initAlpha_200(imageRep200);
- }
- if (alphaInfo_200 == null) {
- initAlpha_200(imageRep200);
- }
- return _getImageData(imageRep200, alphaInfo_200);
+ if (imageRep200 == null) {
+ NSSize imgSize = handle.size();
+ NSSize targetSize = new NSSize();
+ targetSize.height = imgSize.height * 2;
+ targetSize.width = imgSize.width * 2;
+ imageRep200 = createImageRep(targetSize);
+ }
+ if (alphaInfo_100.alphaData != null && alphaInfo_200 != null) {
+ if (alphaInfo_200.alphaData == null) initAlpha_200(imageRep200);
+ }
+ if (alphaInfo_200 == null) {
+ initAlpha_200(imageRep200);
}
+ return _getImageData(imageRep200, alphaInfo_200);
}
} finally {
if (pool != null) pool.release();
@@ -1383,33 +1395,35 @@ public ImageData getImageData(int zoom) {
/** Returns the best available representation. May be 100% or 200% iff there is an image provider. */
NSBitmapImageRep getRepresentation () {
- NSBitmapImageRep rep = new NSBitmapImageRep(handle.bestRepresentationForDevice(null));
- if (rep.isKindOfClass(OS.class_NSBitmapImageRep)) {
- return rep;
+ NSBitmapImageRep rep = null;
+ int scaleFactor = DPIUtil.getDeviceZoom ();
+ switch (scaleFactor) {
+ case 100:
+ rep = getRepresentation_100 ();
+ break;
+ case 200:
+ rep = getRepresentation_200 ();
+ break;
+ }
+
+ if (rep == null) {
+ NSSize targetSize = new NSSize();
+ NSSize imgSize = handle.size();
+ targetSize.width = (int) imgSize.width * scaleFactor / 100;
+ targetSize.height = (int) imgSize.height * scaleFactor / 100;
+ rep = createImageRep(targetSize);
}
NSArray reps = handle.representations();
- NSSize size = handle.size();
long count = reps.count();
- NSBitmapImageRep bestRep = null;
- for (int i = 0; i < count; i++) {
- rep = new NSBitmapImageRep(reps.objectAtIndex(i));
- if (rep.isKindOfClass(OS.class_NSBitmapImageRep)) return rep;
- if (bestRep == null || ((int)size.width == rep.pixelsWide() && (int)size.height == rep.pixelsHigh())) {
- bestRep = rep;
- }
- }
- bestRep.retain();
for (int i = 0; i < count; i++) {
handle.removeRepresentation(new NSImageRep(handle.representations().objectAtIndex(0)));
}
- handle.addRepresentation(bestRep);
- NSBitmapImageRep newRep = (NSBitmapImageRep)new NSBitmapImageRep().alloc();
- newRep = newRep.initWithData(handle.TIFFRepresentation());
- handle.addRepresentation(newRep);
- handle.removeRepresentation(bestRep);
- bestRep.release();
- newRep.release();
- return newRep;
+ handle.addRepresentation(rep);
+ return rep;
+}
+
+ NSBitmapImageRep createImageRep(NSSize targetSize) {
+ return ImageUtil.createImageRep(this, targetSize);
}
/**
@@ -1573,6 +1587,7 @@ public long internal_new_GC (GCData data) {
NSAutoreleasePool pool = null;
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
try {
+ int scaleFactor = DPIUtil.getDeviceZoom() / 100;
NSBitmapImageRep imageRep = getRepresentation();
// Can't perform transforms on image reps with alpha.
@@ -1587,8 +1602,8 @@ public long internal_new_GC (GCData data) {
NSGraphicsContext.setCurrentContext(context);
NSAffineTransform transform = NSAffineTransform.transform();
NSSize size = handle.size();
- transform.translateXBy(0, size.height);
- transform.scaleXBy(1, -1);
+ transform.translateXBy(0, size.height * scaleFactor);
+ transform.scaleXBy(scaleFactor, -scaleFactor);
transform.set();
NSGraphicsContext.static_restoreGraphicsState();
if (data != null) {
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/internal/graphics/ImageUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/internal/graphics/ImageUtil.java
new file mode 100644
index 0000000000..98ce4a4c41
--- /dev/null
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/internal/graphics/ImageUtil.java
@@ -0,0 +1,58 @@
+/*******************************************************************************
+ * Copyright (c) 2019 IBM Corporation and others.
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.swt.internal.graphics;
+
+import org.eclipse.swt.graphics.*;
+import org.eclipse.swt.internal.*;
+import org.eclipse.swt.internal.cocoa.*;
+
+/**
+ * This class holds utility methods for image manipulation on Cocoa platform
+ * Not used on other platforms
+ *
+ * @since 3.110
+ *
+ */
+public class ImageUtil {
+ /**
+ * Creates new image representation based on the source image.
+ *
+ * @param image Source image object
+ * @param targetSize the size at which image representation needs to be created
+ *
+ * @return image representation
+ *
+ * @since 3.110
+ */
+ public static NSBitmapImageRep createImageRep(Image image, NSSize targetSize) {
+ NSBitmapImageRep rep;
+ NSImage imgHandle= image.handle;
+ rep = (NSBitmapImageRep)new NSBitmapImageRep().alloc();
+ rep = rep.initWithBitmapDataPlanes(0, (int) targetSize.width, (int) targetSize.height, 8, 3, false, false, OS.NSDeviceRGBColorSpace, OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, (int) targetSize.width * 4, 32);
+ C.memset(rep.bitmapData(), 0xFF, (int) targetSize.width * (int)targetSize.height * 4);
+ NSGraphicsContext context = NSGraphicsContext.graphicsContextWithBitmapImageRep(rep);
+ NSGraphicsContext.static_saveGraphicsState();
+ context.setImageInterpolation(OS.NSImageInterpolationHigh);
+ NSGraphicsContext.setCurrentContext(context);
+ NSRect target = new NSRect();
+ target.width = targetSize.width;
+ target.height = targetSize.height;
+ NSRect sourceRect = new NSRect();
+ sourceRect.width = 0;
+ sourceRect.height = 0;
+ imgHandle.drawInRect(target, sourceRect, OS.NSCompositeCopy, 1);
+ NSGraphicsContext.static_restoreGraphicsState();
+ return rep;
+ }
+}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Canvas.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Canvas.java
index 144a8eb84f..851ff6b125 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Canvas.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Canvas.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2017 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -18,6 +18,7 @@ import org.eclipse.swt.*;
import org.eclipse.swt.accessibility.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.cocoa.*;
+import org.eclipse.swt.internal.graphics.*;
/**
* Instances of this class provide a surface for drawing
@@ -181,13 +182,13 @@ void drawWidget (long id, NSGraphicsContext context, NSRect rect) {
Image image = caret.image;
if (image != null) {
NSImage imageHandle = image.handle;
- NSImageRep imageRep = imageHandle.bestRepresentationForDevice(null);
+ NSSize size = imageHandle.size();
+ NSImageRep imageRep = ImageUtil.createImageRep(image, size);
if (!imageRep.isKindOfClass(OS.class_NSBitmapImageRep)) return;
NSBitmapImageRep rep = new NSBitmapImageRep(imageRep);
CGRect destRect = new CGRect ();
destRect.origin.x = caret.x;
destRect.origin.y = caret.y;
- NSSize size = imageHandle.size();
destRect.size.width = size.width;
destRect.size.height = size.height;
long data = rep.bitmapData();

Back to the top