|
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.*;
|
@@ -692,11 +691,18 @@ public Image(Device device, ImageData source, ImageData mask) {
|
692 | 691 | */
|
693 | 692 | public Image(Device device, InputStream stream) {
|
694 | 693 | super(device);
|
| 694 | + if (stream == null) { |
| 695 | + SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 696 | + } |
695 | 697 | NSAutoreleasePool pool = null;
|
696 | 698 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
697 | 699 | try {
|
698 |
| - initWithSupplier(zoom -> ImageDataLoader.load(stream, FileFormat.DEFAULT_ZOOM, zoom)); |
| 700 | + byte[] input = stream.readAllBytes(); |
| 701 | + initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom), |
| 702 | + zoom -> ImageDataLoader.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element()); |
699 | 703 | init();
|
| 704 | + } catch (IOException e) { |
| 705 | + SWT.error(SWT.ERROR_INVALID_ARGUMENT, e); |
700 | 706 | } finally {
|
701 | 707 | if (pool != null) pool.release();
|
702 | 708 | }
|
@@ -741,7 +747,10 @@ public Image(Device device, String filename) {
|
741 | 747 | try {
|
742 | 748 | if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
743 | 749 | initNative(filename);
|
744 |
| - if (this.handle == null) initWithSupplier(zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom)); |
| 750 | + if (this.handle == null) { |
| 751 | + initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(filename, FileFormat.DEFAULT_ZOOM, zoom), |
| 752 | + zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom).element()); |
| 753 | + } |
745 | 754 | init();
|
746 | 755 | } finally {
|
747 | 756 | if (pool != null) pool.release();
|
@@ -795,15 +804,13 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
|
795 | 804 | id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x));
|
796 | 805 | NSImageRep rep = new NSImageRep(id);
|
797 | 806 | handle.addRepresentation(rep);
|
798 |
| - } else { |
| 807 | + } else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) { |
799 | 808 | // 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 |
| - } |
| 809 | + ImageData imageData2x = ImageDataLoader.load(filename, 100, 200).element(); |
| 810 | + alphaInfo_200 = new AlphaInfo(); |
| 811 | + NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200); |
| 812 | + handle.addRepresentation(rep); |
| 813 | + rep.release(); |
807 | 814 | }
|
808 | 815 | } finally {
|
809 | 816 | if (pool != null) pool.release();
|
@@ -1484,17 +1491,11 @@ void init(ImageData image) {
|
1484 | 1491 | handle.setCacheMode(OS.NSImageCacheNever);
|
1485 | 1492 | }
|
1486 | 1493 |
|
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) { |
| 1494 | +private void initWithSupplier(Function<Integer, Boolean> canLoadAtZoom, Function<Integer, ImageData> zoomToImageData) { |
| 1495 | + ImageData imageData = zoomToImageData.apply(100); |
| 1496 | + init(imageData); |
| 1497 | + if (canLoadAtZoom.apply(200)) { |
| 1498 | + ImageData imageData2x = zoomToImageData.apply(200); |
1498 | 1499 | alphaInfo_200 = new AlphaInfo();
|
1499 | 1500 | NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
|
1500 | 1501 | handle.addRepresentation(rep);
|
|
0 commit comments