|
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 |
|
@@ -130,9 +131,9 @@ public final class Image extends Resource implements Drawable {
|
130 | 131 | static final int DEFAULT_SCANLINE_PAD = 4;
|
131 | 132 |
|
132 | 133 | /**
|
133 |
| - * ImageFileNameProvider to provide file names at various Zoom levels |
| 134 | + * ImageFileProvider to provide files at various Zoom levels |
134 | 135 | */
|
135 |
| - private ImageFileNameProvider imageFileNameProvider; |
| 136 | + private ImageFileProvider imageFileProvider; |
136 | 137 |
|
137 | 138 | /**
|
138 | 139 | * ImageDataProvider to provide ImageData at various Zoom levels
|
@@ -391,11 +392,11 @@ public Image(Device device, Image srcImage, int flag) {
|
391 | 392 | /* Create the 100% representation for the new image from source image & apply flag */
|
392 | 393 | createRepFromSourceAndApplyFlag(srcImage.getRepresentation (100), srcWidth, srcHeight, flag);
|
393 | 394 |
|
394 |
| - imageFileNameProvider = srcImage.imageFileNameProvider; |
| 395 | + imageFileProvider = srcImage.imageFileProvider; |
395 | 396 | imageDataProvider = srcImage.imageDataProvider;
|
396 | 397 | imageGcDrawer = srcImage.imageGcDrawer;
|
397 | 398 | this.styleFlag = srcImage.styleFlag | flag;
|
398 |
| - if (imageFileNameProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) { |
| 399 | + if (imageFileProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) { |
399 | 400 | /* If source image has 200% representation then create the 200% representation for the new image & apply flag */
|
400 | 401 | NSBitmapImageRep rep200 = srcImage.getRepresentation (200);
|
401 | 402 | if (rep200 != null) createRepFromSourceAndApplyFlag(rep200, srcWidth * 2, srcHeight * 2, flag);
|
@@ -653,18 +654,11 @@ public Image(Device device, ImageData source, ImageData mask) {
|
653 | 654 | * </p>
|
654 | 655 | * <pre>
|
655 | 656 | * static Image loadImage (Display display, Class clazz, String string) {
|
656 |
| - * InputStream stream = clazz.getResourceAsStream (string); |
657 |
| - * if (stream == null) return null; |
658 |
| - * Image image = null; |
659 |
| - * try { |
660 |
| - * image = new Image (display, stream); |
661 |
| - * } catch (SWTException ex) { |
662 |
| - * } finally { |
663 |
| - * try { |
664 |
| - * stream.close (); |
665 |
| - * } catch (IOException ex) {} |
666 |
| - * } |
667 |
| - * return image; |
| 657 | + * try (InputStream stream = clazz.getResourceAsStream(string)){ |
| 658 | + * if (stream == null) return null; |
| 659 | +* return new Image (display, stream); |
| 660 | + * } catch (SWTException | IOException ex) { |
| 661 | + * } |
668 | 662 | * }
|
669 | 663 | * </pre>
|
670 | 664 | * <p>
|
@@ -695,7 +689,7 @@ public Image(Device device, InputStream stream) {
|
695 | 689 | NSAutoreleasePool pool = null;
|
696 | 690 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
697 | 691 | try {
|
698 |
| - initWithSupplier(zoom -> ImageDataLoader.load(stream, FileFormat.DEFAULT_ZOOM, zoom)); |
| 692 | + initWithSupplier(zoom -> ImageData.load(stream, FileFormat.DEFAULT_ZOOM, zoom)); |
699 | 693 | init();
|
700 | 694 | } finally {
|
701 | 695 | if (pool != null) pool.release();
|
@@ -740,8 +734,9 @@ public Image(Device device, String filename) {
|
740 | 734 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
741 | 735 | try {
|
742 | 736 | if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
743 |
| - initNative(filename); |
744 |
| - if (this.handle == null) initWithSupplier(zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom)); |
| 737 | + Path file = Path.of(filename); |
| 738 | + initNative(file); |
| 739 | + if (this.handle == null) initWithSupplier(zoom -> ImageData.load(file, FileFormat.DEFAULT_ZOOM, zoom)); |
745 | 740 | init();
|
746 | 741 | } finally {
|
747 | 742 | if (pool != null) pool.release();
|
@@ -776,28 +771,63 @@ public Image(Device device, String filename) {
|
776 | 771 | * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
777 | 772 | * </ul>
|
778 | 773 | * @since 3.104
|
| 774 | + * @deprecated Instead use {@link #Image(Device, ImageFileProvider)} |
779 | 775 | */
|
| 776 | +@Deprecated(since = "2025-06") |
780 | 777 | public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
|
| 778 | + this(device, DPIUtil.asImageFileProvider(imageFileNameProvider)); |
| 779 | +} |
| 780 | + |
| 781 | +/** |
| 782 | + * Constructs an instance of this class by loading its representation |
| 783 | + * from the file retrieved from the {@link ImageFileProvider}. Throws an |
| 784 | + * error if an error occurs while loading the image, or if the result |
| 785 | + * is an image of an unsupported type. |
| 786 | + * <p> |
| 787 | + * This constructor is provided for convenience for loading image as |
| 788 | + * per DPI level. |
| 789 | + * |
| 790 | + * @param device the device on which to create the image |
| 791 | + * @param imageFileProvider the {@link ImageFileProvider} object that is |
| 792 | + * to be used to get the file |
| 793 | + * |
| 794 | + * @exception IllegalArgumentException <ul> |
| 795 | + * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> |
| 796 | + * <li>ERROR_NULL_ARGUMENT - if the ImageFileNameProvider is null</li> |
| 797 | + * <li>ERROR_INVALID_ARGUMENT - if the fileName provided by ImageFileNameProvider is null at 100% zoom</li> |
| 798 | + * </ul> |
| 799 | + * @exception SWTException <ul> |
| 800 | + * <li>ERROR_IO - if an IO error occurs while reading from the file</li> |
| 801 | + * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li> |
| 802 | + * <li>ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth</li> |
| 803 | + * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> |
| 804 | + * </ul> |
| 805 | + * @exception SWTError <ul> |
| 806 | + * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> |
| 807 | + * </ul> |
| 808 | + * @since 3.130 |
| 809 | + */ |
| 810 | +public Image(Device device, ImageFileProvider imageFileProvider) { |
781 | 811 | super(device);
|
782 |
| - if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
783 |
| - this.imageFileNameProvider = imageFileNameProvider; |
784 |
| - String filename = imageFileNameProvider.getImagePath(100); |
785 |
| - if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 812 | + if (imageFileProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 813 | + this.imageFileProvider = imageFileProvider; |
| 814 | + Path file = imageFileProvider.getImagePath(100); |
| 815 | + if (file == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
786 | 816 | NSAutoreleasePool pool = null;
|
787 | 817 | if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
|
788 | 818 | try {
|
789 |
| - initNative(filename); |
790 |
| - if (this.handle == null) init(ImageDataLoader.load(filename, 100, 100).element()); |
| 819 | + initNative(file); |
| 820 | + if (this.handle == null) init(ImageData.load(file, 100, 100).element()); |
791 | 821 | init();
|
792 |
| - String filename2x = imageFileNameProvider.getImagePath(200); |
793 |
| - if (filename2x != null) { |
| 822 | + Path file2x = imageFileProvider.getImagePath(200); |
| 823 | + if (file2x != null) { |
794 | 824 | alphaInfo_200 = new AlphaInfo();
|
795 |
| - id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x)); |
| 825 | + id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(file2x.toString())); |
796 | 826 | NSImageRep rep = new NSImageRep(id);
|
797 | 827 | handle.addRepresentation(rep);
|
798 | 828 | } else {
|
799 | 829 | // Try to natively scale up the image (e.g. possible if it's an SVG)
|
800 |
| - ElementAtZoom<ImageData> imageData2x = ImageDataLoader.load(filename, 100, 200); |
| 830 | + ElementAtZoom<ImageData> imageData2x = ImageData.load(file, 100, 200); |
801 | 831 | if (imageData2x.zoom() == 200) {
|
802 | 832 | alphaInfo_200 = new AlphaInfo();
|
803 | 833 | NSBitmapImageRep rep = createRepresentation (imageData2x.element(), alphaInfo_200);
|
@@ -878,7 +908,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
|
878 | 908 | * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
879 | 909 | * <li>ERROR_NULL_ARGUMENT - if the ImageGcDrawer is null</li>
|
880 | 910 | * </ul>
|
881 |
| - * @since 3.129 |
| 911 | + * @since 3.130 |
882 | 912 | */
|
883 | 913 | public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) {
|
884 | 914 | super(device);
|
@@ -921,7 +951,7 @@ private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width,
|
921 | 951 |
|
922 | 952 | private AlphaInfo _getAlphaInfoAtCurrentZoom (NSBitmapImageRep rep) {
|
923 | 953 | int deviceZoom = DPIUtil.getDeviceZoom();
|
924 |
| - if (deviceZoom != 100 && (imageFileNameProvider != null || imageDataProvider != null)) { |
| 954 | + if (deviceZoom != 100 && (imageFileProvider != null || imageDataProvider != null)) { |
925 | 955 | if (alphaInfo_100.alphaData != null && alphaInfo_200 != null) {
|
926 | 956 | if (alphaInfo_200.alphaData == null) initAlpha_200(rep);
|
927 | 957 | return alphaInfo_200;
|
@@ -1195,8 +1225,8 @@ public boolean equals (Object object) {
|
1195 | 1225 | if (device != image.device || alphaInfo_100.transparentPixel != image.alphaInfo_100.transparentPixel) return false;
|
1196 | 1226 | if (imageDataProvider != null && image.imageDataProvider != null) {
|
1197 | 1227 | return styleFlag == image.styleFlag && imageDataProvider.equals (image.imageDataProvider);
|
1198 |
| - } else if (imageFileNameProvider != null && image.imageFileNameProvider != null) { |
1199 |
| - return styleFlag == image.styleFlag && imageFileNameProvider.equals (image.imageFileNameProvider); |
| 1228 | + } else if (imageFileProvider != null && image.imageFileProvider != null) { |
| 1229 | + return styleFlag == image.styleFlag && imageFileProvider.equals (image.imageFileProvider); |
1200 | 1230 | } else if (imageGcDrawer != null && image.imageGcDrawer != null) {
|
1201 | 1231 | return styleFlag == image.styleFlag && imageGcDrawer.equals(image.imageGcDrawer) && width == image.width
|
1202 | 1232 | && height == image.height;
|
@@ -1434,8 +1464,8 @@ NSBitmapImageRep createImageRep(NSSize targetSize) {
|
1434 | 1464 | public int hashCode () {
|
1435 | 1465 | if (imageDataProvider != null) {
|
1436 | 1466 | return imageDataProvider.hashCode();
|
1437 |
| - } else if (imageFileNameProvider != null) { |
1438 |
| - return imageFileNameProvider.hashCode(); |
| 1467 | + } else if (imageFileProvider != null) { |
| 1468 | + return imageFileProvider.hashCode(); |
1439 | 1469 | } else if (imageGcDrawer != null) {
|
1440 | 1470 | return Objects.hash(imageGcDrawer, height, width);
|
1441 | 1471 | } 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