|
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);
|
@@ -642,18 +643,11 @@ public Image(Device device, ImageData source, ImageData mask) {
|
642 | 643 | * </p>
|
643 | 644 | * <pre>
|
644 | 645 | * static Image loadImage (Display display, Class clazz, String string) {
|
645 |
| - * InputStream stream = clazz.getResourceAsStream (string); |
646 |
| - * if (stream == null) return null; |
647 |
| - * Image image = null; |
648 |
| - * try { |
649 |
| - * image = new Image (display, stream); |
650 |
| - * } catch (SWTException ex) { |
651 |
| - * } finally { |
652 |
| - * try { |
653 |
| - * stream.close (); |
654 |
| - * } catch (IOException ex) {} |
655 |
| - * } |
656 |
| - * return image; |
| 646 | + * try (InputStream stream = clazz.getResourceAsStream(string)){ |
| 647 | + * if (stream == null) return null; |
| 648 | +* return new Image (display, stream); |
| 649 | + * } catch (SWTException | IOException ex) { |
| 650 | + * } |
657 | 651 | * }
|
658 | 652 | * </pre>
|
659 | 653 | * <p>
|
@@ -688,8 +682,8 @@ public Image(Device device, InputStream stream) {
|
688 | 682 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
689 | 683 | try {
|
690 | 684 | byte[] input = stream.readAllBytes();
|
691 |
| - initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom), |
692 |
| - zoom -> ImageDataLoader.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element()); |
| 685 | + initWithSupplier(zoom -> ImageLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom), |
| 686 | + zoom -> ImageData.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element()); |
693 | 687 | init();
|
694 | 688 | } catch (IOException e) {
|
695 | 689 | SWT.error(SWT.ERROR_INVALID_ARGUMENT, e);
|
@@ -736,10 +730,11 @@ public Image(Device device, String filename) {
|
736 | 730 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
737 | 731 | try {
|
738 | 732 | if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
739 |
| - initNative(filename); |
| 733 | + Path file = Path.of(filename); |
| 734 | + initNative(file); |
740 | 735 | if (this.handle == null) {
|
741 |
| - initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(filename, FileFormat.DEFAULT_ZOOM, zoom), |
742 |
| - zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom).element()); |
| 736 | + initWithSupplier(zoom -> ImageLoader.canLoadAtZoom(file, FileFormat.DEFAULT_ZOOM, zoom), |
| 737 | + zoom -> ImageData.load(file, FileFormat.DEFAULT_ZOOM, zoom).element()); |
743 | 738 | }
|
744 | 739 | init();
|
745 | 740 | } finally {
|
@@ -775,28 +770,63 @@ public Image(Device device, String filename) {
|
775 | 770 | * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
776 | 771 | * </ul>
|
777 | 772 | * @since 3.104
|
| 773 | + * @deprecated Instead use {@link #Image(Device, ImagePathProvider)} |
778 | 774 | */
|
| 775 | +@Deprecated(since = "2025-06") |
779 | 776 | public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
|
| 777 | + this(device, ImageData.asImagePathProvider(imageFileNameProvider)); |
| 778 | +} |
| 779 | + |
| 780 | +/** |
| 781 | + * Constructs an instance of this class by loading its representation |
| 782 | + * from the file retrieved from the {@link ImagePathProvider}. Throws an |
| 783 | + * error if an error occurs while loading the image, or if the result |
| 784 | + * is an image of an unsupported type. |
| 785 | + * <p> |
| 786 | + * This constructor is provided for convenience for loading image as |
| 787 | + * per DPI level. |
| 788 | + * |
| 789 | + * @param device the device on which to create the image |
| 790 | + * @param imageFileProvider the {@link ImagePathProvider} object that is |
| 791 | + * to be used to get the file |
| 792 | + * |
| 793 | + * @exception IllegalArgumentException <ul> |
| 794 | + * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> |
| 795 | + * <li>ERROR_NULL_ARGUMENT - if the ImageFileNameProvider is null</li> |
| 796 | + * <li>ERROR_INVALID_ARGUMENT - if the fileName provided by ImageFileNameProvider is null at 100% zoom</li> |
| 797 | + * </ul> |
| 798 | + * @exception SWTException <ul> |
| 799 | + * <li>ERROR_IO - if an IO error occurs while reading from the file</li> |
| 800 | + * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li> |
| 801 | + * <li>ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth</li> |
| 802 | + * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> |
| 803 | + * </ul> |
| 804 | + * @exception SWTError <ul> |
| 805 | + * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> |
| 806 | + * </ul> |
| 807 | + * @since 3.130 |
| 808 | + */ |
| 809 | +public Image(Device device, ImagePathProvider imageFileProvider) { |
780 | 810 | super(device);
|
781 |
| - if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
782 |
| - this.imageFileNameProvider = imageFileNameProvider; |
783 |
| - String filename = imageFileNameProvider.getImagePath(100); |
784 |
| - if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 811 | + if (imageFileProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 812 | + this.imageFileProvider = imageFileProvider; |
| 813 | + Path file = imageFileProvider.getImagePath(100); |
| 814 | + if (file == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
785 | 815 | NSAutoreleasePool pool = null;
|
786 | 816 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
787 | 817 | try {
|
788 |
| - initNative(filename); |
789 |
| - if (this.handle == null) init(ImageDataLoader.load(filename, 100, 100).element()); |
| 818 | + initNative(file); |
| 819 | + if (this.handle == null) init(ImageData.load(file, 100, 100).element()); |
790 | 820 | init();
|
791 |
| - String filename2x = imageFileNameProvider.getImagePath(200); |
792 |
| - if (filename2x != null) { |
| 821 | + Path file2x = imageFileProvider.getImagePath(200); |
| 822 | + if (file2x != null) { |
793 | 823 | alphaInfo_200 = new AlphaInfo();
|
794 |
| - id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x)); |
| 824 | + id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(file2x.toString())); |
795 | 825 | NSImageRep rep = new NSImageRep(id);
|
796 | 826 | handle.addRepresentation(rep);
|
797 |
| - } else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) { |
| 827 | + } else if (ImageLoader.canLoadAtZoom(file, 100, 200)) { |
798 | 828 | // Try to natively scale up the image (e.g. possible if it's an SVG)
|
799 |
| - ImageData imageData2x = ImageDataLoader.load(filename, 100, 200).element(); |
| 829 | + ImageData imageData2x = ImageData.load(file, 100, 200).element(); |
800 | 830 | alphaInfo_200 = new AlphaInfo();
|
801 | 831 | NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
|
802 | 832 | handle.addRepresentation(rep);
|
@@ -927,7 +957,7 @@ private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width,
|
927 | 957 |
|
928 | 958 | private AlphaInfo _getAlphaInfoAtCurrentZoom (NSBitmapImageRep rep) {
|
929 | 959 | int deviceZoom = DPIUtil.getDeviceZoom();
|
930 |
| - if (deviceZoom != 100 && (imageFileNameProvider != null || imageDataProvider != null)) { |
| 960 | + if (deviceZoom != 100 && (imageFileProvider != null || imageDataProvider != null)) { |
931 | 961 | if (alphaInfo_100.alphaData != null && alphaInfo_200 != null) {
|
932 | 962 | if (alphaInfo_200.alphaData == null) initAlpha_200(rep);
|
933 | 963 | return alphaInfo_200;
|
@@ -1201,8 +1231,8 @@ public boolean equals (Object object) {
|
1201 | 1231 | if (device != image.device || alphaInfo_100.transparentPixel != image.alphaInfo_100.transparentPixel) return false;
|
1202 | 1232 | if (imageDataProvider != null && image.imageDataProvider != null) {
|
1203 | 1233 | return styleFlag == image.styleFlag && imageDataProvider.equals (image.imageDataProvider);
|
1204 |
| - } else if (imageFileNameProvider != null && image.imageFileNameProvider != null) { |
1205 |
| - return styleFlag == image.styleFlag && imageFileNameProvider.equals (image.imageFileNameProvider); |
| 1234 | + } else if (imageFileProvider != null && image.imageFileProvider != null) { |
| 1235 | + return styleFlag == image.styleFlag && imageFileProvider.equals (image.imageFileProvider); |
1206 | 1236 | } else if (imageGcDrawer != null && image.imageGcDrawer != null) {
|
1207 | 1237 | return styleFlag == image.styleFlag && imageGcDrawer.equals(image.imageGcDrawer) && width == image.width
|
1208 | 1238 | && height == image.height;
|
@@ -1440,8 +1470,8 @@ NSBitmapImageRep createImageRep(NSSize targetSize) {
|
1440 | 1470 | public int hashCode () {
|
1441 | 1471 | if (imageDataProvider != null) {
|
1442 | 1472 | return imageDataProvider.hashCode();
|
1443 |
| - } else if (imageFileNameProvider != null) { |
1444 |
| - return imageFileNameProvider.hashCode(); |
| 1473 | + } else if (imageFileProvider != null) { |
| 1474 | + return imageFileProvider.hashCode(); |
1445 | 1475 | } else if (imageGcDrawer != null) {
|
1446 | 1476 | return Objects.hash(imageGcDrawer, height, width);
|
1447 | 1477 | } else {
|
@@ -1545,7 +1575,8 @@ void initAlpha_100(NSBitmapImageRep nativeRep) {
|
1545 | 1575 |
|
1546 | 1576 | }
|
1547 | 1577 |
|
1548 |
| -void initNative(String filename) { |
| 1578 | +void initNative(Path file) { |
| 1579 | + String filename = file.toString(); |
1549 | 1580 | NSAutoreleasePool pool = null;
|
1550 | 1581 | NSImage nativeImage = null;
|
1551 | 1582 |
|
|
0 commit comments