Skip to content

Commit 4bfce99

Browse files
committed
Use Path to represent file-system paths for ImageData creation
Use java.nio.file.Path to model file-system paths instead of String to represent file-system paths. Add the new way to create ImageData as static factory instead of a constructor, because a constructor is not really suitable to create a copy of the first element of an array of ImageData. Also inline the ImageDataLoader and use the new ImageData.load() factories instead.
1 parent 4df99ba commit 4bfce99

File tree

9 files changed

+464
-204
lines changed

9 files changed

+464
-204
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.eclipse.swt.internal.image.ImageColorTransformer.DEFAULT_DISABLED_IMAGE_TRANSFORMER;
1818

1919
import java.io.*;
20+
import java.nio.file.Path;
2021
import java.util.*;
2122
import java.util.function.*;
2223

@@ -131,9 +132,9 @@ public final class Image extends Resource implements Drawable {
131132
static final int DEFAULT_SCANLINE_PAD = 4;
132133

133134
/**
134-
* ImageFileNameProvider to provide file names at various Zoom levels
135+
* ImageFileProvider to provide files at various Zoom levels
135136
*/
136-
private ImageFileNameProvider imageFileNameProvider;
137+
private ImagePathProvider imageFileProvider;
137138

138139
/**
139140
* ImageDataProvider to provide ImageData at various Zoom levels
@@ -392,11 +393,11 @@ public Image(Device device, Image srcImage, int flag) {
392393
/* Create the 100% representation for the new image from source image & apply flag */
393394
createRepFromSourceAndApplyFlag(srcImage.getRepresentation (100), srcWidth, srcHeight, flag);
394395

395-
imageFileNameProvider = srcImage.imageFileNameProvider;
396+
imageFileProvider = srcImage.imageFileProvider;
396397
imageDataProvider = srcImage.imageDataProvider;
397398
imageGcDrawer = srcImage.imageGcDrawer;
398399
this.styleFlag = srcImage.styleFlag | flag;
399-
if (imageFileNameProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) {
400+
if (imageFileProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) {
400401
/* If source image has 200% representation then create the 200% representation for the new image & apply flag */
401402
NSBitmapImageRep rep200 = srcImage.getRepresentation (200);
402403
if (rep200 != null) createRepFromSourceAndApplyFlag(rep200, srcWidth * 2, srcHeight * 2, flag);
@@ -645,18 +646,11 @@ public Image(Device device, ImageData source, ImageData mask) {
645646
* </p>
646647
* <pre>
647648
* 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+
* }
660654
* }
661655
* </pre>
662656
* <p>
@@ -691,8 +685,8 @@ public Image(Device device, InputStream stream) {
691685
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
692686
try {
693687
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());
696690
init();
697691
} catch (IOException e) {
698692
SWT.error(SWT.ERROR_INVALID_ARGUMENT, e);
@@ -739,10 +733,11 @@ public Image(Device device, String filename) {
739733
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
740734
try {
741735
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
742-
initNative(filename);
736+
Path file = Path.of(filename);
737+
initNative(file);
743738
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());
746741
}
747742
init();
748743
} finally {
@@ -778,28 +773,63 @@ public Image(Device device, String filename) {
778773
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
779774
* </ul>
780775
* @since 3.104
776+
* @deprecated Instead use {@link #Image(Device, ImagePathProvider)}
781777
*/
778+
@Deprecated(since = "2025-06")
782779
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) {
783813
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);
788818
NSAutoreleasePool pool = null;
789819
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
790820
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());
793823
init();
794-
String filename2x = imageFileNameProvider.getImagePath(200);
795-
if (filename2x != null) {
824+
Path file2x = imageFileProvider.getImagePath(200);
825+
if (file2x != null) {
796826
alphaInfo_200 = new AlphaInfo();
797-
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x));
827+
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(file2x.toString()));
798828
NSImageRep rep = new NSImageRep(id);
799829
handle.addRepresentation(rep);
800-
} else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) {
830+
} else if (ImageLoader.canLoadAtZoom(file, 100, 200)) {
801831
// 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();
803833
alphaInfo_200 = new AlphaInfo();
804834
NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
805835
handle.addRepresentation(rep);
@@ -930,7 +960,7 @@ private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width,
930960

931961
private AlphaInfo _getAlphaInfoAtCurrentZoom (NSBitmapImageRep rep) {
932962
int deviceZoom = DPIUtil.getDeviceZoom();
933-
if (deviceZoom != 100 && (imageFileNameProvider != null || imageDataProvider != null)) {
963+
if (deviceZoom != 100 && (imageFileProvider != null || imageDataProvider != null)) {
934964
if (alphaInfo_100.alphaData != null && alphaInfo_200 != null) {
935965
if (alphaInfo_200.alphaData == null) initAlpha_200(rep);
936966
return alphaInfo_200;
@@ -1204,8 +1234,8 @@ public boolean equals (Object object) {
12041234
if (device != image.device || alphaInfo_100.transparentPixel != image.alphaInfo_100.transparentPixel) return false;
12051235
if (imageDataProvider != null && image.imageDataProvider != null) {
12061236
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);
12091239
} else if (imageGcDrawer != null && image.imageGcDrawer != null) {
12101240
return styleFlag == image.styleFlag && imageGcDrawer.equals(image.imageGcDrawer) && width == image.width
12111241
&& height == image.height;
@@ -1443,8 +1473,8 @@ NSBitmapImageRep createImageRep(NSSize targetSize) {
14431473
public int hashCode () {
14441474
if (imageDataProvider != null) {
14451475
return imageDataProvider.hashCode();
1446-
} else if (imageFileNameProvider != null) {
1447-
return imageFileNameProvider.hashCode();
1476+
} else if (imageFileProvider != null) {
1477+
return imageFileProvider.hashCode();
14481478
} else if (imageGcDrawer != null) {
14491479
return Objects.hash(imageGcDrawer, height, width);
14501480
} else {
@@ -1548,7 +1578,8 @@ void initAlpha_100(NSBitmapImageRep nativeRep) {
15481578

15491579
}
15501580

1551-
void initNative(String filename) {
1581+
void initNative(Path file) {
1582+
String filename = file.toString();
15521583
NSAutoreleasePool pool = null;
15531584
NSImage nativeImage = null;
15541585

0 commit comments

Comments
 (0)