|
15 | 15 |
|
16 | 16 |
|
17 | 17 | import java.io.*;
|
| 18 | +import java.nio.file.Path; |
18 | 19 | import java.util.*;
|
19 | 20 | import java.util.function.*;
|
20 | 21 |
|
@@ -129,9 +130,9 @@ public final class Image extends Resource implements Drawable {
|
129 | 130 | static final int DEFAULT_SCANLINE_PAD = 4;
|
130 | 131 |
|
131 | 132 | /**
|
132 |
| - * ImageFileNameProvider to provide file names at various Zoom levels |
| 133 | + * ImageFileProvider to provide files at various Zoom levels |
133 | 134 | */
|
134 |
| - private ImageFileNameProvider imageFileNameProvider; |
| 135 | + private ImagePathProvider imageFileProvider; |
135 | 136 |
|
136 | 137 | /**
|
137 | 138 | * ImageDataProvider to provide ImageData at various Zoom levels
|
@@ -390,11 +391,11 @@ public Image(Device device, Image srcImage, int flag) {
|
390 | 391 | /* Create the 100% representation for the new image from source image & apply flag */
|
391 | 392 | createRepFromSourceAndApplyFlag(srcImage.getRepresentation (100), srcWidth, srcHeight, flag);
|
392 | 393 |
|
393 |
| - imageFileNameProvider = srcImage.imageFileNameProvider; |
| 394 | + imageFileProvider = srcImage.imageFileProvider; |
394 | 395 | imageDataProvider = srcImage.imageDataProvider;
|
395 | 396 | imageGcDrawer = srcImage.imageGcDrawer;
|
396 | 397 | this.styleFlag = srcImage.styleFlag | flag;
|
397 |
| - if (imageFileNameProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) { |
| 398 | + if (imageFileProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) { |
398 | 399 | /* If source image has 200% representation then create the 200% representation for the new image & apply flag */
|
399 | 400 | NSBitmapImageRep rep200 = srcImage.getRepresentation (200);
|
400 | 401 | if (rep200 != null) createRepFromSourceAndApplyFlag(rep200, srcWidth * 2, srcHeight * 2, flag);
|
@@ -652,18 +653,11 @@ public Image(Device device, ImageData source, ImageData mask) {
|
652 | 653 | * </p>
|
653 | 654 | * <pre>
|
654 | 655 | * static Image loadImage (Display display, Class clazz, String string) {
|
655 |
| - * InputStream stream = clazz.getResourceAsStream (string); |
656 |
| - * if (stream == null) return null; |
657 |
| - * Image image = null; |
658 |
| - * try { |
659 |
| - * image = new Image (display, stream); |
660 |
| - * } catch (SWTException ex) { |
661 |
| - * } finally { |
662 |
| - * try { |
663 |
| - * stream.close (); |
664 |
| - * } catch (IOException ex) {} |
665 |
| - * } |
666 |
| - * return image; |
| 656 | + * try (InputStream stream = clazz.getResourceAsStream(string)){ |
| 657 | + * if (stream == null) return null; |
| 658 | +* return new Image (display, stream); |
| 659 | + * } catch (SWTException | IOException ex) { |
| 660 | + * } |
667 | 661 | * }
|
668 | 662 | * </pre>
|
669 | 663 | * <p>
|
@@ -698,8 +692,8 @@ public Image(Device device, InputStream stream) {
|
698 | 692 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
699 | 693 | try {
|
700 | 694 | 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()); |
| 695 | + initWithSupplier(zoom -> ImageLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom), |
| 696 | + zoom -> ImageData.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element()); |
703 | 697 | init();
|
704 | 698 | } catch (IOException e) {
|
705 | 699 | SWT.error(SWT.ERROR_INVALID_ARGUMENT, e);
|
@@ -746,10 +740,11 @@ public Image(Device device, String filename) {
|
746 | 740 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
747 | 741 | try {
|
748 | 742 | if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
749 |
| - initNative(filename); |
| 743 | + Path file = Path.of(filename); |
| 744 | + initNative(file); |
750 | 745 | if (this.handle == null) {
|
751 |
| - initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(filename, FileFormat.DEFAULT_ZOOM, zoom), |
752 |
| - zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom).element()); |
| 746 | + initWithSupplier(zoom -> ImageLoader.canLoadAtZoom(file, FileFormat.DEFAULT_ZOOM, zoom), |
| 747 | + zoom -> ImageData.load(file, FileFormat.DEFAULT_ZOOM, zoom).element()); |
753 | 748 | }
|
754 | 749 | init();
|
755 | 750 | } finally {
|
@@ -785,28 +780,63 @@ public Image(Device device, String filename) {
|
785 | 780 | * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
786 | 781 | * </ul>
|
787 | 782 | * @since 3.104
|
| 783 | + * @deprecated Instead use {@link #Image(Device, ImagePathProvider)} |
788 | 784 | */
|
| 785 | +@Deprecated(since = "2025-06") |
789 | 786 | public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
|
| 787 | + this(device, DPIUtil.asImageFileProvider(imageFileNameProvider)); |
| 788 | +} |
| 789 | + |
| 790 | +/** |
| 791 | + * Constructs an instance of this class by loading its representation |
| 792 | + * from the file retrieved from the {@link ImagePathProvider}. Throws an |
| 793 | + * error if an error occurs while loading the image, or if the result |
| 794 | + * is an image of an unsupported type. |
| 795 | + * <p> |
| 796 | + * This constructor is provided for convenience for loading image as |
| 797 | + * per DPI level. |
| 798 | + * |
| 799 | + * @param device the device on which to create the image |
| 800 | + * @param imageFileProvider the {@link ImagePathProvider} object that is |
| 801 | + * to be used to get the file |
| 802 | + * |
| 803 | + * @exception IllegalArgumentException <ul> |
| 804 | + * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> |
| 805 | + * <li>ERROR_NULL_ARGUMENT - if the ImageFileNameProvider is null</li> |
| 806 | + * <li>ERROR_INVALID_ARGUMENT - if the fileName provided by ImageFileNameProvider is null at 100% zoom</li> |
| 807 | + * </ul> |
| 808 | + * @exception SWTException <ul> |
| 809 | + * <li>ERROR_IO - if an IO error occurs while reading from the file</li> |
| 810 | + * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li> |
| 811 | + * <li>ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth</li> |
| 812 | + * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> |
| 813 | + * </ul> |
| 814 | + * @exception SWTError <ul> |
| 815 | + * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> |
| 816 | + * </ul> |
| 817 | + * @since 3.130 |
| 818 | + */ |
| 819 | +public Image(Device device, ImagePathProvider imageFileProvider) { |
790 | 820 | super(device);
|
791 |
| - if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
792 |
| - this.imageFileNameProvider = imageFileNameProvider; |
793 |
| - String filename = imageFileNameProvider.getImagePath(100); |
794 |
| - if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 821 | + if (imageFileProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 822 | + this.imageFileProvider = imageFileProvider; |
| 823 | + Path file = imageFileProvider.getImagePath(100); |
| 824 | + if (file == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
795 | 825 | NSAutoreleasePool pool = null;
|
796 | 826 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
797 | 827 | try {
|
798 |
| - initNative(filename); |
799 |
| - if (this.handle == null) init(ImageDataLoader.load(filename, 100, 100).element()); |
| 828 | + initNative(file); |
| 829 | + if (this.handle == null) init(ImageData.load(file, 100, 100).element()); |
800 | 830 | init();
|
801 |
| - String filename2x = imageFileNameProvider.getImagePath(200); |
802 |
| - if (filename2x != null) { |
| 831 | + Path file2x = imageFileProvider.getImagePath(200); |
| 832 | + if (file2x != null) { |
803 | 833 | alphaInfo_200 = new AlphaInfo();
|
804 |
| - id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x)); |
| 834 | + id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(file2x.toString())); |
805 | 835 | NSImageRep rep = new NSImageRep(id);
|
806 | 836 | handle.addRepresentation(rep);
|
807 |
| - } else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) { |
| 837 | + } else if (ImageLoader.canLoadAtZoom(file, 100, 200)) { |
808 | 838 | // Try to natively scale up the image (e.g. possible if it's an SVG)
|
809 |
| - ImageData imageData2x = ImageDataLoader.load(filename, 100, 200).element(); |
| 839 | + ImageData imageData2x = ImageData.load(file, 100, 200).element(); |
810 | 840 | alphaInfo_200 = new AlphaInfo();
|
811 | 841 | NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
|
812 | 842 | handle.addRepresentation(rep);
|
@@ -885,7 +915,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
|
885 | 915 | * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
886 | 916 | * <li>ERROR_NULL_ARGUMENT - if the ImageGcDrawer is null</li>
|
887 | 917 | * </ul>
|
888 |
| - * @since 3.129 |
| 918 | + * @since 3.130 |
889 | 919 | */
|
890 | 920 | public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) {
|
891 | 921 | super(device);
|
@@ -928,7 +958,7 @@ private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width,
|
928 | 958 |
|
929 | 959 | private AlphaInfo _getAlphaInfoAtCurrentZoom (NSBitmapImageRep rep) {
|
930 | 960 | int deviceZoom = DPIUtil.getDeviceZoom();
|
931 |
| - if (deviceZoom != 100 && (imageFileNameProvider != null || imageDataProvider != null)) { |
| 961 | + if (deviceZoom != 100 && (imageFileProvider != null || imageDataProvider != null)) { |
932 | 962 | if (alphaInfo_100.alphaData != null && alphaInfo_200 != null) {
|
933 | 963 | if (alphaInfo_200.alphaData == null) initAlpha_200(rep);
|
934 | 964 | return alphaInfo_200;
|
@@ -1202,8 +1232,8 @@ public boolean equals (Object object) {
|
1202 | 1232 | if (device != image.device || alphaInfo_100.transparentPixel != image.alphaInfo_100.transparentPixel) return false;
|
1203 | 1233 | if (imageDataProvider != null && image.imageDataProvider != null) {
|
1204 | 1234 | return styleFlag == image.styleFlag && imageDataProvider.equals (image.imageDataProvider);
|
1205 |
| - } else if (imageFileNameProvider != null && image.imageFileNameProvider != null) { |
1206 |
| - return styleFlag == image.styleFlag && imageFileNameProvider.equals (image.imageFileNameProvider); |
| 1235 | + } else if (imageFileProvider != null && image.imageFileProvider != null) { |
| 1236 | + return styleFlag == image.styleFlag && imageFileProvider.equals (image.imageFileProvider); |
1207 | 1237 | } else if (imageGcDrawer != null && image.imageGcDrawer != null) {
|
1208 | 1238 | return styleFlag == image.styleFlag && imageGcDrawer.equals(image.imageGcDrawer) && width == image.width
|
1209 | 1239 | && height == image.height;
|
@@ -1441,8 +1471,8 @@ NSBitmapImageRep createImageRep(NSSize targetSize) {
|
1441 | 1471 | public int hashCode () {
|
1442 | 1472 | if (imageDataProvider != null) {
|
1443 | 1473 | return imageDataProvider.hashCode();
|
1444 |
| - } else if (imageFileNameProvider != null) { |
1445 |
| - return imageFileNameProvider.hashCode(); |
| 1474 | + } else if (imageFileProvider != null) { |
| 1475 | + return imageFileProvider.hashCode(); |
1446 | 1476 | } else if (imageGcDrawer != null) {
|
1447 | 1477 | return Objects.hash(imageGcDrawer, height, width);
|
1448 | 1478 | } else {
|
@@ -1546,7 +1576,8 @@ void initAlpha_100(NSBitmapImageRep nativeRep) {
|
1546 | 1576 |
|
1547 | 1577 | }
|
1548 | 1578 |
|
1549 |
| -void initNative(String filename) { |
| 1579 | +void initNative(Path file) { |
| 1580 | + String filename = file.toString(); |
1550 | 1581 | NSAutoreleasePool pool = null;
|
1551 | 1582 | NSImage nativeImage = null;
|
1552 | 1583 |
|
|
0 commit comments