diff options
| author | Lakshmi Shanmugam | 2016-03-12 07:17:18 +0000 |
|---|---|---|
| committer | Niraj Modi | 2016-03-12 07:17:18 +0000 |
| commit | 3a567ddfe053c5ec0239ac121a05e12cc88768b4 (patch) | |
| tree | 22e7a72b3950e66f8f1e0d17306c864e3ad0e36f | |
| parent | 14734beab7b7b915783fb9d21c16ed3bd253062b (diff) | |
| download | eclipse.platform.swt-3a567ddfe053c5ec0239ac121a05e12cc88768b4.tar.gz eclipse.platform.swt-3a567ddfe053c5ec0239ac121a05e12cc88768b4.tar.xz eclipse.platform.swt-3a567ddfe053c5ec0239ac121a05e12cc88768b4.zip | |
Bug 479614 - [Graphics] HiDPI support for eclipse platform
- Cocoa changes.
Change-Id: Iaa41ddffe168d5b1910def05271547590cc919fd
Signed-off-by: Lakshmi Shanmugam <lshanmug@in.ibm.com>
| -rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java | 193 |
1 files changed, 131 insertions, 62 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 62b2dfd192..9b6bc2d1a4 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 @@ -11,10 +11,11 @@ package org.eclipse.swt.graphics; -import org.eclipse.swt.internal.cocoa.*; -import org.eclipse.swt.*; import java.io.*; +import org.eclipse.swt.*; +import org.eclipse.swt.internal.cocoa.*; + /** * Instances of this class are graphics which have been prepared * for display on a specific device. That is, they are ready @@ -834,6 +835,90 @@ public Rectangle getBounds() { } /** + * Returns the bounds of the receiver. The rectangle will always + * have x and y values of 0, and the width and height of the + * image. + * + * @return a rectangle specifying the image's bounds in pixels + * + * @exception SWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * <li>ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon</li> + * </ul> + * @since 3.105 + */ +public Rectangle getBoundsInPixels() { + Rectangle bounds = getBounds(); + int scaleFactor = (int) NSScreen.mainScreen().backingScaleFactor(); + bounds.width *= scaleFactor; + bounds.height *= scaleFactor; + return bounds; +} + +ImageData _getImageData(NSBitmapImageRep imageRep) { + long /*int*/ width = imageRep.pixelsWide(); + long /*int*/ height = imageRep.pixelsHigh(); + long /*int*/ bpr = imageRep.bytesPerRow(); + long /*int*/ bpp = imageRep.bitsPerPixel(); + long /*int*/ bitmapData = imageRep.bitmapData(); + long /*int*/ bitmapFormat = imageRep.bitmapFormat(); + long /*int*/ dataSize = height * bpr; + byte[] srcData = new byte[(int)/*64*/dataSize]; + OS.memmove(srcData, bitmapData, dataSize); + + PaletteData palette; + if (bpp == 32 && (bitmapFormat & OS.NSAlphaFirstBitmapFormat) == 0) { + palette = new PaletteData(0xFF000000, 0xFF0000, 0xFF00); + } else { + palette = new PaletteData(0xFF0000, 0xFF00, 0xFF); + } + ImageData data = new ImageData((int)/*64*/width, (int)/*64*/height, (int)/*64*/bpp, palette, 1, srcData); + data.bytesPerLine = (int)/*64*/bpr; + if (imageRep.hasAlpha() && transparentPixel == -1 && alpha == -1 && alphaData == null) { + byte[] alphaData = new byte[(int)/*64*/(width * height)]; + int offset = (bitmapFormat & OS.NSAlphaFirstBitmapFormat) != 0 ? 0 : 3, a = 0; + for (int i = offset; i < srcData.length; i+= 4) { + alphaData[a++] = srcData[i]; + } + data.alphaData = alphaData; + } else { + data.transparentPixel = transparentPixel; + if (transparentPixel == -1 && type == SWT.ICON) { + /* Get the icon mask data */ + int maskPad = 2; + long /*int*/ maskBpl = (((width + 7) / 8) + (maskPad - 1)) / maskPad * maskPad; + byte[] maskData = new byte[(int)/*64*/(height * maskBpl)]; + int offset = 0, maskOffset = 0; + for (int y = 0; y<height; y++) { + for (int x = 0; x<width; x++) { + if (srcData[offset] != 0) { + maskData[maskOffset + (x >> 3)] |= (1 << (7 - (x & 0x7))); + } else { + maskData[maskOffset + (x >> 3)] &= ~(1 << (7 - (x & 0x7))); + } + offset += 4; + } + maskOffset += maskBpl; + } + data.maskData = maskData; + data.maskPad = maskPad; + } + data.alpha = alpha; + if (alpha == -1 && alphaData != null) { + data.alphaData = new byte[alphaData.length]; + System.arraycopy(alphaData, 0, data.alphaData, 0, alphaData.length); + } + } + if (bpp == 32) { + int offset = (bitmapFormat & OS.NSAlphaFirstBitmapFormat) != 0 ? 0 : 3; + for (int i = offset; i < srcData.length; i+= 4) { + srcData[i] = 0; + } + } + return data; +} + +/** * Returns an <code>ImageData</code> based on the receiver * Modifications made to this <code>ImageData</code> will not * affect the Image. @@ -852,67 +937,36 @@ public ImageData getImageData() { NSAutoreleasePool pool = null; if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { - NSBitmapImageRep imageRep = getRepresentation(); - long /*int*/ width = imageRep.pixelsWide(); - long /*int*/ height = imageRep.pixelsHigh(); - long /*int*/ bpr = imageRep.bytesPerRow(); - long /*int*/ bpp = imageRep.bitsPerPixel(); - long /*int*/ bitmapData = imageRep.bitmapData(); - long /*int*/ bitmapFormat = imageRep.bitmapFormat(); - long /*int*/ dataSize = height * bpr; - byte[] srcData = new byte[(int)/*64*/dataSize]; - OS.memmove(srcData, bitmapData, dataSize); + NSBitmapImageRep imageRep = getActualRepresentation(); + return _getImageData(imageRep); + } finally { + if (pool != null) pool.release(); + } +} - PaletteData palette; - if (bpp == 32 && (bitmapFormat & OS.NSAlphaFirstBitmapFormat) == 0) { - palette = new PaletteData(0xFF000000, 0xFF0000, 0xFF00); - } else { - palette = new PaletteData(0xFF0000, 0xFF00, 0xFF); - } - ImageData data = new ImageData((int)/*64*/width, (int)/*64*/height, (int)/*64*/bpp, palette, 1, srcData); - data.bytesPerLine = (int)/*64*/bpr; - if (imageRep.hasAlpha() && transparentPixel == -1 && alpha == -1 && alphaData == null) { - byte[] alphaData = new byte[(int)/*64*/(width * height)]; - int offset = (bitmapFormat & OS.NSAlphaFirstBitmapFormat) != 0 ? 0 : 3, a = 0; - for (int i = offset; i < srcData.length; i+= 4) { - alphaData[a++] = srcData[i]; - } - data.alphaData = alphaData; - } else { - data.transparentPixel = transparentPixel; - if (transparentPixel == -1 && type == SWT.ICON) { - /* Get the icon mask data */ - int maskPad = 2; - long /*int*/ maskBpl = (((width + 7) / 8) + (maskPad - 1)) / maskPad * maskPad; - byte[] maskData = new byte[(int)/*64*/(height * maskBpl)]; - int offset = 0, maskOffset = 0; - for (int y = 0; y<height; y++) { - for (int x = 0; x<width; x++) { - if (srcData[offset] != 0) { - maskData[maskOffset + (x >> 3)] |= (1 << (7 - (x & 0x7))); - } else { - maskData[maskOffset + (x >> 3)] &= ~(1 << (7 - (x & 0x7))); - } - offset += 4; - } - maskOffset += maskBpl; - } - data.maskData = maskData; - data.maskPad = maskPad; - } - data.alpha = alpha; - if (alpha == -1 && alphaData != null) { - data.alphaData = new byte[alphaData.length]; - System.arraycopy(alphaData, 0, data.alphaData, 0, alphaData.length); - } - } - if (bpp == 32) { - int offset = (bitmapFormat & OS.NSAlphaFirstBitmapFormat) != 0 ? 0 : 3; - for (int i = offset; i < srcData.length; i+= 4) { - srcData[i] = 0; - } - } - return data; +/** + * Returns an <code>ImageData</code> based on the receiver + * Modifications made to this <code>ImageData</code> will not + * affect the Image. + * + * @return an <code>ImageData</code> containing the image's data + * and attributes at the current zoom level. + * + * @exception SWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * <li>ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon</li> + * </ul> + * + * @see ImageData + * @since 3.105 + */ +public ImageData getImageDataAtCurrentZoom() { + if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + NSAutoreleasePool pool = null; + if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); + try { + NSBitmapImageRep imageRep = getRepresentation(); + return _getImageData(imageRep); } finally { if (pool != null) pool.release(); } @@ -941,6 +995,21 @@ public static Image cocoa_new(Device device, int type, NSImage nsImage) { return image; } +// Method to get the Image representation for the actual image without any scaling. +NSBitmapImageRep getActualRepresentation () { + NSArray reps = handle.representations(); + NSSize size = handle.size(); + long /*int*/ count = reps.count(); + NSBitmapImageRep bestRep = null; + for (int i = 0; i < count; i++) { + NSBitmapImageRep rep = new NSBitmapImageRep(reps.objectAtIndex(i)); + if (bestRep == null || ((int)size.width == rep.pixelsWide() && (int)size.height == rep.pixelsHigh())) { + bestRep = rep; + } + } + return bestRep; +} + NSBitmapImageRep getRepresentation () { NSBitmapImageRep rep = new NSBitmapImageRep(handle.bestRepresentationForDevice(null)); if (rep.isKindOfClass(OS.class_NSBitmapImageRep)) { |
