|
20 | 20 |
|
21 | 21 | import org.eclipse.swt.*;
|
22 | 22 | import org.eclipse.swt.internal.*;
|
23 |
| -import org.eclipse.swt.internal.DPIUtil.*; |
24 | 23 | import org.eclipse.swt.internal.cocoa.*;
|
25 | 24 | import org.eclipse.swt.internal.graphics.*;
|
26 | 25 | import org.eclipse.swt.internal.image.*;
|
@@ -695,8 +694,12 @@ public Image(Device device, InputStream stream) {
|
695 | 694 | NSAutoreleasePool pool = null;
|
696 | 695 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
697 | 696 | try {
|
698 |
| - initWithSupplier(zoom -> ImageDataLoader.load(stream, FileFormat.DEFAULT_ZOOM, zoom)); |
| 697 | + byte[] input = stream.readAllBytes(); |
| 698 | + initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom), |
| 699 | + zoom -> ImageDataLoader.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element()); |
699 | 700 | init();
|
| 701 | + } catch (IOException e) { |
| 702 | + SWT.error(SWT.ERROR_INVALID_ARGUMENT, e); |
700 | 703 | } finally {
|
701 | 704 | if (pool != null) pool.release();
|
702 | 705 | }
|
@@ -741,7 +744,10 @@ public Image(Device device, String filename) {
|
741 | 744 | try {
|
742 | 745 | if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
743 | 746 | initNative(filename);
|
744 |
| - if (this.handle == null) initWithSupplier(zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom)); |
| 747 | + if (this.handle == null) { |
| 748 | + initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(filename, FileFormat.DEFAULT_ZOOM, zoom), |
| 749 | + zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom).element()); |
| 750 | + } |
745 | 751 | init();
|
746 | 752 | } finally {
|
747 | 753 | if (pool != null) pool.release();
|
@@ -795,15 +801,13 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
|
795 | 801 | id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x));
|
796 | 802 | NSImageRep rep = new NSImageRep(id);
|
797 | 803 | handle.addRepresentation(rep);
|
798 |
| - } else { |
| 804 | + } else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) { |
799 | 805 | // Try to natively scale up the image (e.g. possible if it's an SVG)
|
800 |
| - ElementAtZoom<ImageData> imageData2x = ImageDataLoader.load(filename, 100, 200); |
801 |
| - if (imageData2x.zoom() == 200) { |
802 |
| - alphaInfo_200 = new AlphaInfo(); |
803 |
| - NSBitmapImageRep rep = createRepresentation (imageData2x.element(), alphaInfo_200); |
804 |
| - handle.addRepresentation(rep); |
805 |
| - rep.release(); |
806 |
| - } |
| 806 | + ImageData imageData2x = ImageDataLoader.load(filename, 100, 200).element(); |
| 807 | + alphaInfo_200 = new AlphaInfo(); |
| 808 | + NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200); |
| 809 | + handle.addRepresentation(rep); |
| 810 | + rep.release(); |
807 | 811 | }
|
808 | 812 | } finally {
|
809 | 813 | if (pool != null) pool.release();
|
@@ -1484,17 +1488,11 @@ void init(ImageData image) {
|
1484 | 1488 | handle.setCacheMode(OS.NSImageCacheNever);
|
1485 | 1489 | }
|
1486 | 1490 |
|
1487 |
| -private void initWithSupplier(Function<Integer, ElementAtZoom<ImageData>> zoomToImageData) { |
1488 |
| - ElementAtZoom<ImageData> imageData = zoomToImageData.apply(DPIUtil.getDeviceZoom()); |
1489 |
| - ImageData imageData2x = null; |
1490 |
| - if (imageData.zoom() == 200) { |
1491 |
| - imageData2x = imageData.element(); |
1492 |
| - } |
1493 |
| - if (imageData.zoom() != 100) { |
1494 |
| - imageData = zoomToImageData.apply(100); |
1495 |
| - } |
1496 |
| - init(imageData.element()); |
1497 |
| - if (imageData2x != null) { |
| 1491 | +private void initWithSupplier(Function<Integer, Boolean> canLoadAtZoom, Function<Integer, ImageData> zoomToImageData) { |
| 1492 | + ImageData imageData = zoomToImageData.apply(100); |
| 1493 | + init(imageData); |
| 1494 | + if (canLoadAtZoom.apply(200)) { |
| 1495 | + ImageData imageData2x = zoomToImageData.apply(200); |
1498 | 1496 | alphaInfo_200 = new AlphaInfo();
|
1499 | 1497 | NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
|
1500 | 1498 | handle.addRepresentation(rep);
|
|
0 commit comments