Skip to content

Commit 6ea3e8d

Browse files
committed
remove ElementAtTargetSize
ElementAtTargetSize is only used to store the srcWidth and srcHeight. We only need this information for `canLoadAtZoom`. We decided that the user shall try to load the image at the targetSize and test if it fails without a utility method. Revert "remove ElementAtTargetSize" This reverts commit 44166e4. Reapply "remove ElementAtTargetSize" This reverts commit 13350a14637310025e7616b81e3a275a949d90e7.
1 parent 9d64802 commit 6ea3e8d

File tree

11 files changed

+92
-158
lines changed

11 files changed

+92
-158
lines changed

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ public static boolean canLoadAtZoom(InputStream stream, int fileZoom, int target
4141
return ImageLoader.canLoadAtZoom(stream, fileZoom, targetZoom);
4242
}
4343

44-
public static boolean canLoadAtTargetSize(InputStream stream, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
45-
return ImageLoader.canLoadAtTargetSize(stream, srcWidth, srcHeight, targetWidth, targetHeight);
46-
}
47-
4844
public static ElementAtZoom<ImageData> load(InputStream stream, int fileZoom, int targetZoom) {
4945
List<ElementAtZoom<ImageData>> data = new ImageLoader().load(stream, fileZoom, targetZoom);
5046
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
@@ -55,26 +51,22 @@ public static boolean canLoadAtZoom(String filename, int fileZoom, int targetZoo
5551
return ImageLoader.canLoadAtZoom(filename, fileZoom, targetZoom);
5652
}
5753

58-
public static boolean canLoadAtTargetSize(String filename, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
59-
return ImageLoader.canLoadAtTargetSize(filename, srcWidth, srcHeight, targetWidth, targetHeight);
60-
}
61-
6254
public static ElementAtZoom<ImageData> load(String filename, int fileZoom, int targetZoom) {
6355
List<ElementAtZoom<ImageData>> data = new ImageLoader().load(filename, fileZoom, targetZoom);
6456
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
6557
return data.get(0);
6658
}
6759

68-
public static ElementAtTargetSize<ImageData> loadByTargetSize(InputStream stream, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
69-
List<ElementAtTargetSize<ImageData>> data = new ImageLoader().loadByTargetSize(stream, srcWidth, srcHeight, targetWidth, targetHeight);
70-
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
71-
return data.get(0);
60+
public static ImageData loadByTargetSize(InputStream stream, int targetWidth, int targetHeight) {
61+
ImageData data = new ImageLoader().loadByTargetSize(stream, targetWidth, targetHeight);
62+
if (data == null) SWT.error(SWT.ERROR_INVALID_IMAGE);
63+
return data;
7264
}
7365

74-
public static ElementAtTargetSize<ImageData> loadByTargetSize(String filename, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
75-
List<ElementAtTargetSize<ImageData>> data = new ImageLoader().loadByTargetSize(filename, srcWidth, srcHeight, targetWidth, targetHeight);
76-
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
77-
return data.get(0);
66+
public static ImageData loadByTargetSize(String filename, int targetWidth, int targetHeight) {
67+
ImageData data = new ImageLoader().loadByTargetSize(filename, targetWidth, targetHeight);
68+
if (data == null) SWT.error(SWT.ERROR_INVALID_IMAGE);
69+
return data;
7870
}
7971

8072
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageLoader.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ List<ElementAtZoom<ImageData>> load(InputStream stream, int fileZoom, int target
163163
return images;
164164
}
165165

166-
List<ElementAtTargetSize<ImageData>> loadByTargetSize(InputStream stream, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
166+
ImageData loadByTargetSize(InputStream stream, int targetWidth, int targetHeight) {
167167
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
168168
reset();
169-
List<ElementAtTargetSize<ImageData>> images = NativeImageLoader.load(new ElementAtTargetSize<>(stream, srcWidth, srcHeight), this, targetWidth, targetHeight);
170-
data = images.stream().map(ElementAtTargetSize::element).toArray(ImageData[]::new);
171-
return images;
169+
ImageData image = NativeImageLoader.load(stream, this, targetWidth, targetHeight);
170+
data = new ImageData[] {image};
171+
return image;
172172
}
173173

174174

@@ -177,11 +177,6 @@ static boolean canLoadAtZoom(InputStream stream, int fileZoom, int targetZoom) {
177177
return FileFormat.canLoadAtZoom(new ElementAtZoom<>(stream, fileZoom), targetZoom);
178178
}
179179

180-
static boolean canLoadAtTargetSize(InputStream stream, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
181-
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
182-
return FileFormat.canLoadAtTargetSize(new ElementAtTargetSize<>(stream, srcWidth, srcHeight), targetWidth, targetHeight);
183-
}
184-
185180
/**
186181
* Loads an array of <code>ImageData</code> objects from the
187182
* file with the specified name. Throws an error if either
@@ -215,10 +210,10 @@ List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoo
215210
return null;
216211
}
217212

218-
List<ElementAtTargetSize<ImageData>> loadByTargetSize(String filename, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
213+
ImageData loadByTargetSize(String filename, int targetWidth, int targetHeight) {
219214
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
220215
try (InputStream stream = new FileInputStream(filename)) {
221-
return loadByTargetSize(stream, srcWidth, srcHeight, targetWidth, targetHeight);
216+
return loadByTargetSize(stream, targetWidth, targetHeight);
222217
} catch (IOException e) {
223218
SWT.error(SWT.ERROR_IO, e);
224219
}
@@ -235,16 +230,6 @@ static boolean canLoadAtZoom(String filename, int fileZoom, int targetZoom) {
235230
return false;
236231
}
237232

238-
static boolean canLoadAtTargetSize(String filename, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
239-
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
240-
try (InputStream stream = new FileInputStream(filename)) {
241-
return canLoadAtTargetSize(stream, srcWidth, srcHeight, targetWidth, targetHeight);
242-
} catch (IOException e) {
243-
SWT.error(SWT.ERROR_IO, e);
244-
}
245-
return false;
246-
}
247-
248233
/**
249234
* Saves the image data in this ImageLoader to the specified stream.
250235
* The format parameter can have one of the following values:
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
package org.eclipse.swt.graphics;
22

3-
import java.io.*;
4-
53
/**
64
* @since 3.131
75
*/
86
public interface SizeAwareImageDataProvider extends ImageDataProvider {
9-
/**
10-
* Returns the image file path suitable for rendering at the given target size.
11-
*
12-
* <p>This method should only be called if {@link #canLoadAtTargetSize} returns <code>true</code>.</p>
13-
*
14-
* @throws UnsupportedOperationException if called when not supported
15-
*/
7+
168
ImageData getImageData(int targetWidth, int targetHeight);
179

10+
//TODO If we remove this a lambda is not possible anymore
1811
@Override
1912
default ImageData getImageData(int zoom) {
2013
throw new UnsupportedOperationException();
2114
};
22-
23-
static boolean canLoadAtTargetSize(InputStream stream, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
24-
return ImageDataLoader.canLoadAtTargetSize(stream, srcWidth, srcHeight, targetWidth, targetHeight);
25-
}
2615
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SizeAwareImageFileNameProvider.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@
66
public interface SizeAwareImageFileNameProvider extends ImageFileNameProvider {
77

88
/**
9-
* Returns the image file path suitable for rendering at the given target size.
9+
* Returns the image file path most suitable for rendering at the given target size.
1010
*
11-
* <p>This method should only be called if {@link #canLoadAtTargetSize} returns <code>true</code>.</p>
12-
*
13-
* @throws UnsupportedOperationException if called when not supported
1411
*/
12+
//TODO: Optional
1513
String getImagePath(int targetWidth, int targetHeight);
1614

15+
//TODO zoom erlauben + in Lambdas implementieren.
1716
@Override
1817
default String getImagePath(int zoom) {
1918
throw new UnsupportedOperationException();
2019
};
21-
22-
static boolean canLoadAtTargetSize(String fileName, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
23-
return ImageDataLoader.canLoadAtTargetSize(fileName, srcWidth, srcHeight, targetWidth, targetHeight);
24-
}
2520
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -274,20 +274,6 @@ public record ElementAtZoom<T>(T element, int zoom) {
274274
}
275275
}
276276

277-
public record ElementAtTargetSize<T>(T element, int srcWidth, int srcHeight) {
278-
public ElementAtTargetSize {
279-
if (element == null) {
280-
SWT.error(SWT.ERROR_NULL_ARGUMENT);
281-
}
282-
if (srcWidth <= 0 || srcHeight <= 0) {
283-
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
284-
}
285-
if (srcHeight <= 0 || srcHeight <= 0) {
286-
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
287-
}
288-
}
289-
}
290-
291277
/**
292278
* Gets ImageData that are appropriate for the specified zoom level together
293279
* with the zoom level at which the image data are. If there is an image at the
@@ -308,12 +294,12 @@ public static ElementAtZoom<ImageData> validateAndGetImageDataAtZoom(ImageDataPr
308294
return imageDataAtZoom;
309295
}
310296

311-
public static ElementAtTargetSize<ImageData> validateAndGetImageDataAtTargetSize(ImageDataProvider provider, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
297+
public static ImageData validateAndGetImageDataAtTargetSize(ImageDataProvider provider, int targetWidth, int targetHeight) {
312298
if (provider == null) {
313299
SWT.error(SWT.ERROR_NULL_ARGUMENT);
314300
}
315301
if (provider instanceof SizeAwareImageDataProvider sizeAwareProvider) {
316-
ElementAtTargetSize<ImageData> imageDataAtTargetSize = getElementAtTargetSize((x, z) -> sizeAwareProvider.getImageData(x, z), srcWidth, srcHeight, targetWidth, targetHeight);
302+
ImageData imageDataAtTargetSize = getElementAtTargetSize((x, z) -> sizeAwareProvider.getImageData(x, z), targetWidth, targetHeight);
317303
if (imageDataAtTargetSize == null) {
318304
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null,
319305
": ImageDataProvider [" + provider + "] returns null ImageData at 100% zoom.");
@@ -346,12 +332,12 @@ public static ElementAtZoom<String> validateAndGetImagePathAtZoom(ImageFileNameP
346332
return imagePathAtZoom;
347333
}
348334

349-
public static ElementAtTargetSize<String> validateAndGetImagePathAtTargetSize(ImageFileNameProvider provider, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
335+
public static String validateAndGetImagePathAtTargetSize(ImageFileNameProvider provider, int targetWidth, int targetHeight) {
350336
if (provider == null) {
351337
SWT.error(SWT.ERROR_NULL_ARGUMENT);
352338
}
353339
if (provider instanceof SizeAwareImageFileNameProvider sizeAwareProvider) {
354-
ElementAtTargetSize<String> imagePathAtTargetSize = getElementAtTargetSize((x, z) -> sizeAwareProvider.getImagePath(x, z), srcWidth, srcHeight, targetWidth, targetHeight);
340+
String imagePathAtTargetSize = getElementAtTargetSize((x, z) -> sizeAwareProvider.getImagePath(x, z), targetWidth, targetHeight);
355341
if (imagePathAtTargetSize == null) {
356342
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null,
357343
": ImageFileNameProvider [" + provider + "] returns null filename at 100% zoom.");
@@ -390,27 +376,28 @@ private static <T> ElementAtZoom<T> getElementAtZoom(Function<Integer, T> elemen
390376
return null;
391377
}
392378

393-
private static <T> ElementAtTargetSize<T> getElementAtTargetSize(BiFunction<Integer, Integer, T> elementForZoomProvider, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
379+
private static <T> T getElementAtTargetSize(BiFunction<Integer, Integer, T> elementForZoomProvider, int targetWidth, int targetHeight) {
394380
T dataAtOriginalZoom = elementForZoomProvider.apply(targetWidth, targetHeight);
395381
if (dataAtOriginalZoom != null) {
396-
return new ElementAtTargetSize<>(dataAtOriginalZoom, srcWidth, srcHeight);
382+
return dataAtOriginalZoom;
397383
}
384+
//TODO Dieser Mechanismus sollte bei PNGs in Image liegen und die ImageData entsprechend laden.
398385
if (targetWidth >= 16 && targetWidth <= 24) {
399386
T dataAt150Percent = elementForZoomProvider.apply(24, 24);
400387
if (dataAt150Percent != null) {
401-
return new ElementAtTargetSize<>(dataAt150Percent, srcWidth, srcHeight);
388+
return dataAt150Percent;
402389
}
403390
}
404391
if (targetWidth > 16) {
405392
T dataAt200Percent = elementForZoomProvider.apply(32, 32);
406393
if (dataAt200Percent != null) {
407-
return new ElementAtTargetSize<>(dataAt200Percent, srcWidth, srcHeight);
394+
return dataAt200Percent;
408395
}
409396
}
410397
if (targetWidth != 16) {
411398
T dataAt100Percent = elementForZoomProvider.apply(16, 16);
412399
if (dataAt100Percent != null) {
413-
return new ElementAtTargetSize<>(dataAt100Percent, srcWidth, srcHeight);
400+
return dataAt100Percent;
414401
}
415402
}
416403
return null;

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom)
8787
}
8888

8989
@Override
90-
List<ElementAtTargetSize<ImageData>> loadFromByteStreamByTargetSize(int targetWidth, int targetHeight) {
91-
return Arrays.stream(loadFromByteStream()).map(d -> new ElementAtTargetSize<>(d, targetWidth, targetHeight)).toList();
90+
ImageData loadFromByteStreamByTargetSize(int targetWidth, int targetHeight) {
91+
return loadFromByteStream()[0];
9292
}
9393
}
9494

@@ -109,7 +109,7 @@ List<ElementAtTargetSize<ImageData>> loadFromByteStreamByTargetSize(int targetWi
109109
*/
110110
abstract List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom);
111111

112-
abstract List<ElementAtTargetSize<ImageData>> loadFromByteStreamByTargetSize(int targetWidth, int targetHeight);
112+
abstract ImageData loadFromByteStreamByTargetSize(int targetWidth, int targetHeight);
113113

114114
/**
115115
* Read the specified input stream, and return the
@@ -129,7 +129,7 @@ public List<ElementAtZoom<ImageData>> loadFromStream(LEDataInputStream stream, i
129129
}
130130
}
131131

132-
public List<ElementAtTargetSize<ImageData>> loadFromStreamByTargetSize(LEDataInputStream stream, int targetWidth, int targetHeight) {
132+
public ImageData loadFromStreamByTargetSize(LEDataInputStream stream, int targetWidth, int targetHeight) {
133133
try {
134134
inputStream = stream;
135135
return loadFromByteStreamByTargetSize(targetWidth, targetHeight);
@@ -157,8 +157,8 @@ public static List<ElementAtZoom<ImageData>> load(ElementAtZoom<InputStream> is,
157157
return fileFormat.loadFromStream(stream, is.zoom(), targetZoom);
158158
}
159159

160-
public static List<ElementAtTargetSize<ImageData>> load(ElementAtTargetSize<InputStream> is, ImageLoader loader, int targetWidth, int targetHeight) {
161-
LEDataInputStream stream = new LEDataInputStream(is.element());
160+
public static ImageData load(InputStream is, ImageLoader loader, int targetWidth, int targetHeight) {
161+
LEDataInputStream stream = new LEDataInputStream(is);
162162
FileFormat fileFormat = determineFileFormat(stream).orElseGet(() -> {
163163
SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
164164
return null;
@@ -171,10 +171,6 @@ public static boolean canLoadAtZoom(ElementAtZoom<InputStream> is, int targetZoo
171171
return is.zoom() == targetZoom || isDynamicallySizableFormat(is.element());
172172
}
173173

174-
public static boolean canLoadAtTargetSize(ElementAtTargetSize<InputStream> is, int targetWidth, int targetHeight) {
175-
return (is.srcWidth() == targetWidth && is.srcHeight() == targetHeight) || isDynamicallySizableFormat(is.element());
176-
}
177-
178174
/**
179175
* Write the device independent image array stored in the specified loader
180176
* to the specified output stream using the specified file format.

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGFileFormat.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom)
6161
}
6262

6363
@Override
64-
List<ElementAtTargetSize<ImageData>> loadFromByteStreamByTargetSize(int targetWidth, int targetHeight) {
64+
ImageData loadFromByteStreamByTargetSize(int targetWidth, int targetHeight) {
6565
if (RASTERIZER == null) {
6666
SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT, null, " [No SVG rasterizer found]");
6767
}
@@ -73,10 +73,10 @@ List<ElementAtTargetSize<ImageData>> loadFromByteStreamByTargetSize(int targetWi
7373
}
7474
try {
7575
ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, targetWidth, targetHeight);
76-
return List.of(new ElementAtTargetSize<>(rasterizedImageData, targetWidth, targetHeight));
76+
return rasterizedImageData;
7777
} catch (IOException e) {
7878
SWT.error(SWT.ERROR_INVALID_IMAGE, e);
79-
return List.of();
79+
return null;
8080
}
8181
}
8282

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ private void drawWithTempHandle(Image srcImage, int srcX, int srcY, int srcWidth
12661266
drawIcon(tempImageHandle, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
12671267
break;
12681268
}
1269-
}, srcWidth, srcHeight, destWidth, destHeight);
1269+
}, destWidth, destHeight);
12701270
}
12711271

12721272
private void drawIcon(long imageHandle, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {

0 commit comments

Comments
 (0)