Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -736,18 +736,20 @@ public Image(Device device, InputStream stream) {
*/
public Image(Device device, String filename) {
super(device);
if (filename == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
NSAutoreleasePool pool = null;
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
if (!NSThread.isMainThread()) {
pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
}
try {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
initNative(filename);
if (this.handle == null) {
initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(filename, FileFormat.DEFAULT_ZOOM, zoom),
zoom -> ImageDataLoader.loadByZoom(filename, FileFormat.DEFAULT_ZOOM, zoom).element());
}
initUsingFileNameProvider((ImageFileNameProvider) zoom -> zoom == 100 ? filename : null);
init();
} finally {
if (pool != null) pool.release();
if (pool != null) {
pool.release();
}
}
}

Expand Down Expand Up @@ -782,32 +784,46 @@ public Image(Device device, String filename) {
*/
public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
super(device);
if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
this.imageFileNameProvider = imageFileNameProvider;
String filename = imageFileNameProvider.getImagePath(100);
if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
if (imageFileNameProvider == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
NSAutoreleasePool pool = null;
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
if (!NSThread.isMainThread()) {
pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
}
try {
initNative(filename);
if (this.handle == null) init(ImageDataLoader.loadByZoom(filename, 100, 100).element(), 100);
initUsingFileNameProvider(imageFileNameProvider);
init();
String filename2x = imageFileNameProvider.getImagePath(200);
if (filename2x != null) {
alphaInfo_200 = new AlphaInfo();
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x));
NSImageRep rep = new NSImageRep(id);
handle.addRepresentation(rep);
} else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) {
// Try to natively scale up the image (e.g. possible if it's an SVG)
ImageData imageData2x = ImageDataLoader.loadByZoom(filename, 100, 200).element();
alphaInfo_200 = new AlphaInfo();
NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
handle.addRepresentation(rep);
rep.release();
}
} finally {
if (pool != null) pool.release();
if (pool != null) {
pool.release();
}
}
}

private void initUsingFileNameProvider(ImageFileNameProvider imageFileNameProvider) {
this.imageFileNameProvider = imageFileNameProvider;
String filename = imageFileNameProvider.getImagePath(100);
if (filename == null) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
initNative(filename);
if (this.handle == null) {
init(ImageDataLoader.loadByZoom(filename, 100, 100).element(), 100);
}
String filename2x = imageFileNameProvider.getImagePath(200);
if (filename2x != null) {
alphaInfo_200 = new AlphaInfo();
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x));
NSImageRep rep = new NSImageRep(id);
handle.addRepresentation(rep);
} else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) {
// Try to natively scale up the image (e.g. possible if it's an SVG)
ImageData imageData2x = ImageDataLoader.loadByZoom(filename, 100, 200).element();
alphaInfo_200 = new AlphaInfo();
NSBitmapImageRep rep = createRepresentation(imageData2x, alphaInfo_200);
handle.addRepresentation(rep);
rep.release();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,10 @@ public Image(Device device, String filename) {
super(device);
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
currentDeviceZoom = DPIUtil.getDeviceZoom();
ElementAtZoom<ImageData> image = ImageDataLoader.loadByZoom(filename, FileFormat.DEFAULT_ZOOM, currentDeviceZoom);
ImageData data = DPIUtil.scaleImageData(device, image, currentDeviceZoom);
init(data);
this.imageFileNameProvider = (ImageFileNameProvider) zoom -> {
return zoom == 100 ? filename : null;
};
initFromFileNameProvider(currentDeviceZoom);
init();
}

Expand Down
Loading