|
17 | 17 | import static org.eclipse.swt.internal.image.ImageColorTransformer.DEFAULT_DISABLED_IMAGE_TRANSFORMER;
|
18 | 18 |
|
19 | 19 | import java.io.*;
|
| 20 | +import java.nio.file.Path; |
20 | 21 | import java.util.*;
|
21 | 22 | import java.util.function.*;
|
22 | 23 |
|
@@ -131,9 +132,9 @@ public final class Image extends Resource implements Drawable {
|
131 | 132 | static final int DEFAULT_SCANLINE_PAD = 4;
|
132 | 133 |
|
133 | 134 | /**
|
134 |
| - * ImageFileNameProvider to provide file names at various Zoom levels |
| 135 | + * ImageFileProvider to provide files at various Zoom levels |
135 | 136 | */
|
136 |
| - private ImageFileNameProvider imageFileNameProvider; |
| 137 | + private ImagePathProvider imageFileProvider; |
137 | 138 |
|
138 | 139 | /**
|
139 | 140 | * ImageDataProvider to provide ImageData at various Zoom levels
|
@@ -392,11 +393,11 @@ public Image(Device device, Image srcImage, int flag) {
|
392 | 393 | /* Create the 100% representation for the new image from source image & apply flag */
|
393 | 394 | createRepFromSourceAndApplyFlag(srcImage.getRepresentation (100), srcWidth, srcHeight, flag);
|
394 | 395 |
|
395 |
| - imageFileNameProvider = srcImage.imageFileNameProvider; |
| 396 | + imageFileProvider = srcImage.imageFileProvider; |
396 | 397 | imageDataProvider = srcImage.imageDataProvider;
|
397 | 398 | imageGcDrawer = srcImage.imageGcDrawer;
|
398 | 399 | this.styleFlag = srcImage.styleFlag | flag;
|
399 |
| - if (imageFileNameProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) { |
| 400 | + if (imageFileProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) { |
400 | 401 | /* If source image has 200% representation then create the 200% representation for the new image & apply flag */
|
401 | 402 | NSBitmapImageRep rep200 = srcImage.getRepresentation (200);
|
402 | 403 | if (rep200 != null) createRepFromSourceAndApplyFlag(rep200, srcWidth * 2, srcHeight * 2, flag);
|
@@ -645,18 +646,11 @@ public Image(Device device, ImageData source, ImageData mask) {
|
645 | 646 | * </p>
|
646 | 647 | * <pre>
|
647 | 648 | * static Image loadImage (Display display, Class clazz, String string) {
|
648 |
| - * InputStream stream = clazz.getResourceAsStream (string); |
649 |
| - * if (stream == null) return null; |
650 |
| - * Image image = null; |
651 |
| - * try { |
652 |
| - * image = new Image (display, stream); |
653 |
| - * } catch (SWTException ex) { |
654 |
| - * } finally { |
655 |
| - * try { |
656 |
| - * stream.close (); |
657 |
| - * } catch (IOException ex) {} |
658 |
| - * } |
659 |
| - * return image; |
| 649 | + * try (InputStream stream = clazz.getResourceAsStream(string)){ |
| 650 | + * if (stream == null) return null; |
| 651 | +* return new Image (display, stream); |
| 652 | + * } catch (SWTException | IOException ex) { |
| 653 | + * } |
660 | 654 | * }
|
661 | 655 | * </pre>
|
662 | 656 | * <p>
|
@@ -691,8 +685,8 @@ public Image(Device device, InputStream stream) {
|
691 | 685 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
692 | 686 | try {
|
693 | 687 | byte[] input = stream.readAllBytes();
|
694 |
| - initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom), |
695 |
| - zoom -> ImageDataLoader.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element()); |
| 688 | + initWithSupplier(zoom -> ImageLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom), |
| 689 | + zoom -> ImageData.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element()); |
696 | 690 | init();
|
697 | 691 | } catch (IOException e) {
|
698 | 692 | SWT.error(SWT.ERROR_INVALID_ARGUMENT, e);
|
@@ -739,10 +733,11 @@ public Image(Device device, String filename) {
|
739 | 733 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
740 | 734 | try {
|
741 | 735 | if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
742 |
| - initNative(filename); |
| 736 | + Path file = Path.of(filename); |
| 737 | + initNative(file); |
743 | 738 | if (this.handle == null) {
|
744 |
| - initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(filename, FileFormat.DEFAULT_ZOOM, zoom), |
745 |
| - zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom).element()); |
| 739 | + initWithSupplier(zoom -> ImageLoader.canLoadAtZoom(file, FileFormat.DEFAULT_ZOOM, zoom), |
| 740 | + zoom -> ImageData.load(file, FileFormat.DEFAULT_ZOOM, zoom).element()); |
746 | 741 | }
|
747 | 742 | init();
|
748 | 743 | } finally {
|
@@ -778,28 +773,63 @@ public Image(Device device, String filename) {
|
778 | 773 | * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
779 | 774 | * </ul>
|
780 | 775 | * @since 3.104
|
| 776 | + * @deprecated Instead use {@link #Image(Device, ImagePathProvider)} |
781 | 777 | */
|
| 778 | +@Deprecated(since = "2025-06") |
782 | 779 | public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
|
| 780 | + this(device, ImageData.asImagePathProvider(imageFileNameProvider)); |
| 781 | +} |
| 782 | + |
| 783 | +/** |
| 784 | + * Constructs an instance of this class by loading its representation |
| 785 | + * from the file retrieved from the {@link ImagePathProvider}. Throws an |
| 786 | + * error if an error occurs while loading the image, or if the result |
| 787 | + * is an image of an unsupported type. |
| 788 | + * <p> |
| 789 | + * This constructor is provided for convenience for loading image as |
| 790 | + * per DPI level. |
| 791 | + * |
| 792 | + * @param device the device on which to create the image |
| 793 | + * @param imageFileProvider the {@link ImagePathProvider} object that is |
| 794 | + * to be used to get the file |
| 795 | + * |
| 796 | + * @exception IllegalArgumentException <ul> |
| 797 | + * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> |
| 798 | + * <li>ERROR_NULL_ARGUMENT - if the ImageFileNameProvider is null</li> |
| 799 | + * <li>ERROR_INVALID_ARGUMENT - if the fileName provided by ImageFileNameProvider is null at 100% zoom</li> |
| 800 | + * </ul> |
| 801 | + * @exception SWTException <ul> |
| 802 | + * <li>ERROR_IO - if an IO error occurs while reading from the file</li> |
| 803 | + * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li> |
| 804 | + * <li>ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth</li> |
| 805 | + * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> |
| 806 | + * </ul> |
| 807 | + * @exception SWTError <ul> |
| 808 | + * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> |
| 809 | + * </ul> |
| 810 | + * @since 3.130 |
| 811 | + */ |
| 812 | +public Image(Device device, ImagePathProvider imageFileProvider) { |
783 | 813 | super(device);
|
784 |
| - if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
785 |
| - this.imageFileNameProvider = imageFileNameProvider; |
786 |
| - String filename = imageFileNameProvider.getImagePath(100); |
787 |
| - if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 814 | + if (imageFileProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 815 | + this.imageFileProvider = imageFileProvider; |
| 816 | + Path file = imageFileProvider.getImagePath(100); |
| 817 | + if (file == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
788 | 818 | NSAutoreleasePool pool = null;
|
789 | 819 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
790 | 820 | try {
|
791 |
| - initNative(filename); |
792 |
| - if (this.handle == null) init(ImageDataLoader.load(filename, 100, 100).element()); |
| 821 | + initNative(file); |
| 822 | + if (this.handle == null) init(ImageData.load(file, 100, 100).element()); |
793 | 823 | init();
|
794 |
| - String filename2x = imageFileNameProvider.getImagePath(200); |
795 |
| - if (filename2x != null) { |
| 824 | + Path file2x = imageFileProvider.getImagePath(200); |
| 825 | + if (file2x != null) { |
796 | 826 | alphaInfo_200 = new AlphaInfo();
|
797 |
| - id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x)); |
| 827 | + id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(file2x.toString())); |
798 | 828 | NSImageRep rep = new NSImageRep(id);
|
799 | 829 | handle.addRepresentation(rep);
|
800 |
| - } else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) { |
| 830 | + } else if (ImageLoader.canLoadAtZoom(file, 100, 200)) { |
801 | 831 | // Try to natively scale up the image (e.g. possible if it's an SVG)
|
802 |
| - ImageData imageData2x = ImageDataLoader.load(filename, 100, 200).element(); |
| 832 | + ImageData imageData2x = ImageData.load(file, 100, 200).element(); |
803 | 833 | alphaInfo_200 = new AlphaInfo();
|
804 | 834 | NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
|
805 | 835 | handle.addRepresentation(rep);
|
@@ -930,7 +960,7 @@ private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width,
|
930 | 960 |
|
931 | 961 | private AlphaInfo _getAlphaInfoAtCurrentZoom (NSBitmapImageRep rep) {
|
932 | 962 | int deviceZoom = DPIUtil.getDeviceZoom();
|
933 |
| - if (deviceZoom != 100 && (imageFileNameProvider != null || imageDataProvider != null)) { |
| 963 | + if (deviceZoom != 100 && (imageFileProvider != null || imageDataProvider != null)) { |
934 | 964 | if (alphaInfo_100.alphaData != null && alphaInfo_200 != null) {
|
935 | 965 | if (alphaInfo_200.alphaData == null) initAlpha_200(rep);
|
936 | 966 | return alphaInfo_200;
|
@@ -1204,8 +1234,8 @@ public boolean equals (Object object) {
|
1204 | 1234 | if (device != image.device || alphaInfo_100.transparentPixel != image.alphaInfo_100.transparentPixel) return false;
|
1205 | 1235 | if (imageDataProvider != null && image.imageDataProvider != null) {
|
1206 | 1236 | return styleFlag == image.styleFlag && imageDataProvider.equals (image.imageDataProvider);
|
1207 |
| - } else if (imageFileNameProvider != null && image.imageFileNameProvider != null) { |
1208 |
| - return styleFlag == image.styleFlag && imageFileNameProvider.equals (image.imageFileNameProvider); |
| 1237 | + } else if (imageFileProvider != null && image.imageFileProvider != null) { |
| 1238 | + return styleFlag == image.styleFlag && imageFileProvider.equals (image.imageFileProvider); |
1209 | 1239 | } else if (imageGcDrawer != null && image.imageGcDrawer != null) {
|
1210 | 1240 | return styleFlag == image.styleFlag && imageGcDrawer.equals(image.imageGcDrawer) && width == image.width
|
1211 | 1241 | && height == image.height;
|
@@ -1443,8 +1473,8 @@ NSBitmapImageRep createImageRep(NSSize targetSize) {
|
1443 | 1473 | public int hashCode () {
|
1444 | 1474 | if (imageDataProvider != null) {
|
1445 | 1475 | return imageDataProvider.hashCode();
|
1446 |
| - } else if (imageFileNameProvider != null) { |
1447 |
| - return imageFileNameProvider.hashCode(); |
| 1476 | + } else if (imageFileProvider != null) { |
| 1477 | + return imageFileProvider.hashCode(); |
1448 | 1478 | } else if (imageGcDrawer != null) {
|
1449 | 1479 | return Objects.hash(imageGcDrawer, height, width);
|
1450 | 1480 | } else {
|
@@ -1548,7 +1578,8 @@ void initAlpha_100(NSBitmapImageRep nativeRep) {
|
1548 | 1578 |
|
1549 | 1579 | }
|
1550 | 1580 |
|
1551 |
| -void initNative(String filename) { |
| 1581 | +void initNative(Path file) { |
| 1582 | + String filename = file.toString(); |
1552 | 1583 | NSAutoreleasePool pool = null;
|
1553 | 1584 | NSImage nativeImage = null;
|
1554 | 1585 |
|
|
0 commit comments