Skip to content

Commit b220571

Browse files
committed
Adapt usages of ImageLoader in Cocoa implementation of Image
1 parent 11cf125 commit b220571

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
import java.io.*;
1818
import java.util.*;
19+
import java.util.function.*;
1920

2021
import org.eclipse.swt.*;
2122
import org.eclipse.swt.internal.*;
2223
import org.eclipse.swt.internal.DPIUtil.*;
2324
import org.eclipse.swt.internal.cocoa.*;
2425
import org.eclipse.swt.internal.graphics.*;
26+
import org.eclipse.swt.internal.image.*;
2527

2628
/**
2729
* Instances of this class are graphics which have been prepared
@@ -693,8 +695,7 @@ public Image(Device device, InputStream stream) {
693695
NSAutoreleasePool pool = null;
694696
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
695697
try {
696-
// TODO: scale SVGs here too?
697-
init(new ImageData(stream));
698+
initWithSupplier(zoom -> ImageDataLoader.load(stream, FileFormat.DEFAULT_ZOOM, zoom));
698699
init();
699700
} finally {
700701
if (pool != null) pool.release();
@@ -740,8 +741,7 @@ public Image(Device device, String filename) {
740741
try {
741742
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
742743
initNative(filename);
743-
// TODO: scale SVGs here too?
744-
if (this.handle == null) init(new ImageData(filename));
744+
if (this.handle == null) initWithSupplier(zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom));
745745
init();
746746
} finally {
747747
if (pool != null) pool.release();
@@ -781,14 +781,13 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
781781
super(device);
782782
if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
783783
this.imageFileNameProvider = imageFileNameProvider;
784-
//TODO: implement fine-grained zoom handling here as well?
785784
String filename = imageFileNameProvider.getImagePath(100);
786785
if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
787786
NSAutoreleasePool pool = null;
788787
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
789788
try {
790789
initNative(filename);
791-
if (this.handle == null) init(new ImageData(filename));
790+
if (this.handle == null) init(ImageDataLoader.load(filename, 100, 100).element());
792791
init();
793792
String filename2x = imageFileNameProvider.getImagePath(200);
794793
if (filename2x != null) {
@@ -800,9 +799,10 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
800799
// Try to natively scale up the image (e.g. possible if it's an SVG)
801800
ElementAtZoom<ImageData> imageData2x = ImageDataLoader.load(filename, 100, 200);
802801
if (imageData2x.zoom() == 200) {
803-
Image image2x = new Image(device, imageData2x.element());
804-
NSImageRep rep = ImageUtil.createImageRep(image2x, image2x.width, image2x.height);
802+
alphaInfo_200 = new AlphaInfo();
803+
NSBitmapImageRep rep = createRepresentation (imageData2x.element(), alphaInfo_200);
805804
handle.addRepresentation(rep);
805+
rep.release();
806806
}
807807
}
808808
} finally {
@@ -1205,18 +1205,19 @@ NSBitmapImageRep getRepresentation (int scaleFactor) {
12051205
NSArray reps = handle.representations();
12061206
NSSize size = handle.size();
12071207
long count = reps.count();
1208-
int width = (int) size.width * scaleFactor / 100;
1209-
int height = (int) size.height * scaleFactor / 100;
1208+
NSSize targetSize = new NSSize();
1209+
targetSize.width = (int)size.width * scaleFactor / 100;
1210+
targetSize.height = (int)size.height * scaleFactor / 100;
12101211
NSBitmapImageRep rep;
12111212
for (int i = 0; i < count; i++) {
12121213
rep = new NSBitmapImageRep(reps.objectAtIndex(i));
1213-
if (width == rep.pixelsWide() && height == rep.pixelsHigh()) {
1214+
if ((targetSize.width == rep.pixelsWide() && targetSize.height == rep.pixelsHigh())) {
12141215
if (rep.isKindOfClass(OS.class_NSBitmapImageRep)) {
12151216
return rep;
12161217
}
12171218
}
12181219
}
1219-
NSBitmapImageRep newRep = createImageRep(width, height);
1220+
NSBitmapImageRep newRep = createImageRep(targetSize);
12201221
for (int i = 0; i < count; i++) {
12211222
handle.removeRepresentation(new NSImageRep(handle.representations().objectAtIndex(0)));
12221223
}
@@ -1408,8 +1409,8 @@ NSBitmapImageRep getRepresentation () {
14081409
return getRepresentation (DPIUtil.getDeviceZoom ());
14091410
}
14101411

1411-
NSBitmapImageRep createImageRep(int targetWidth, int targetHeight) {
1412-
return ImageUtil.createImageRep(this, targetWidth, targetHeight);
1412+
NSBitmapImageRep createImageRep(NSSize targetSize) {
1413+
return ImageUtil.createImageRep(this, targetSize);
14131414
}
14141415

14151416
/**
@@ -1476,6 +1477,25 @@ void init(ImageData image) {
14761477
handle.setCacheMode(OS.NSImageCacheNever);
14771478
}
14781479

1480+
private void initWithSupplier(Function<Integer, ElementAtZoom<ImageData>> zoomToImageData) {
1481+
ElementAtZoom<ImageData> imageData = zoomToImageData.apply(DPIUtil.getDeviceZoom());
1482+
ImageData imageData2x = null;
1483+
if (imageData.zoom() == 200) {
1484+
imageData2x = imageData.element();
1485+
}
1486+
if (imageData.zoom() != 100) {
1487+
imageData = zoomToImageData.apply(DPIUtil.getDeviceZoom());
1488+
}
1489+
init(imageData.element());
1490+
if (imageData2x != null) {
1491+
alphaInfo_200 = new AlphaInfo();
1492+
NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
1493+
handle.addRepresentation(rep);
1494+
rep.release();
1495+
}
1496+
}
1497+
1498+
14791499
void initAlpha_200(NSBitmapImageRep nativeRep) {
14801500
NSAutoreleasePool pool = null;
14811501
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/internal/graphics/ImageUtil.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@ public class ImageUtil {
2828
* Creates new image representation based on the source image.
2929
*
3030
* @param image Source image object
31-
* @param targetWidth the width at which image representation needs to be created
32-
* @param targetHeight the height at which image representation needs to be created
31+
* @param targetSize the size at which image representation needs to be created
3332
*
3433
* @return image representation
3534
*
3635
* @since 3.110
3736
*/
38-
public static NSBitmapImageRep createImageRep(Image image, int targetWidth, int targetHeight) {
37+
public static NSBitmapImageRep createImageRep(Image image, NSSize targetSize) {
38+
NSBitmapImageRep rep;
3939
NSImage imgHandle= image.handle;
40-
NSBitmapImageRep rep = (NSBitmapImageRep) new NSBitmapImageRep().alloc();
41-
rep = rep.initWithBitmapDataPlanes(0, targetWidth, targetHeight, 8, 4, true, false, OS.NSDeviceRGBColorSpace, OS.NSAlphaFirstBitmapFormat, targetWidth * 4, 32);
42-
C.memset(rep.bitmapData(), 0xFF, targetWidth * targetHeight * 4);
40+
rep = (NSBitmapImageRep)new NSBitmapImageRep().alloc();
41+
rep = rep.initWithBitmapDataPlanes(0, (int) targetSize.width, (int) targetSize.height, 8, 4, true, false, OS.NSDeviceRGBColorSpace, OS.NSAlphaFirstBitmapFormat, (int) targetSize.width * 4, 32);
42+
C.memset(rep.bitmapData(), 0xFF, (int) targetSize.width * (int)targetSize.height * 4);
4343
NSGraphicsContext context = NSGraphicsContext.graphicsContextWithBitmapImageRep(rep);
4444
NSGraphicsContext.static_saveGraphicsState();
4545
context.setImageInterpolation(OS.NSImageInterpolationHigh);
4646
NSGraphicsContext.setCurrentContext(context);
4747
NSRect target = new NSRect();
48-
target.width = targetWidth;
49-
target.height = targetHeight;
48+
target.width = targetSize.width;
49+
target.height = targetSize.height;
5050
NSRect sourceRect = new NSRect();
5151
sourceRect.width = 0;
5252
sourceRect.height = 0;

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Canvas.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void drawWidget (long id, NSGraphicsContext context, NSRect rect) {
183183
if (image != null) {
184184
NSImage imageHandle = image.handle;
185185
NSSize size = imageHandle.size();
186-
NSImageRep imageRep = ImageUtil.createImageRep(image, (int) size.width, (int) size.height);
186+
NSImageRep imageRep = ImageUtil.createImageRep(image, size);
187187
if (!imageRep.isKindOfClass(OS.class_NSBitmapImageRep)) return;
188188
NSBitmapImageRep rep = new NSBitmapImageRep(imageRep);
189189
CGRect destRect = new CGRect ();

0 commit comments

Comments
 (0)