17
17
*******************************************************************************/
18
18
package org .eclipse .jface .resource ;
19
19
20
+
20
21
import java .io .BufferedInputStream ;
21
22
import java .io .IOException ;
22
23
import java .io .InputStream ;
23
24
import java .net .MalformedURLException ;
24
25
import java .net .URL ;
25
26
import java .nio .file .Files ;
26
27
import java .nio .file .Path ;
28
+ import java .util .function .Supplier ;
27
29
28
30
import org .eclipse .core .runtime .FileLocator ;
29
31
import org .eclipse .core .runtime .IAdaptable ;
38
40
import org .eclipse .swt .graphics .ImageData ;
39
41
import org .eclipse .swt .graphics .ImageDataProvider ;
40
42
import org .eclipse .swt .graphics .ImageFileNameProvider ;
43
+ import org .eclipse .swt .graphics .ImageLoader ;
44
+ import org .eclipse .swt .internal .DPIUtil .ElementAtZoom ;
45
+ import org .eclipse .swt .internal .NativeImageLoader ;
46
+ import org .eclipse .swt .internal .image .FileFormat ;
41
47
42
48
/**
43
49
* An ImageDescriptor that gets its information from a URL. This class is not
@@ -62,16 +68,16 @@ public String getImagePath(int zoom) {
62
68
if (zoom == 100 ) {
63
69
return getFilePath (tempURL , logIOException );
64
70
}
65
- URL xUrl = getxURL (tempURL , zoom );
71
+ SourceAtZoom < URL > xUrl = getxURL (tempURL , zoom );
66
72
if (xUrl != null ) {
67
- String xResult = getFilePath (xUrl , logIOException );
73
+ String xResult = getFilePath (xUrl . source () , logIOException );
68
74
if (xResult != null ) {
69
75
return xResult ;
70
76
}
71
77
}
72
- String xpath = FileImageDescriptor .getxPath (url , zoom );
78
+ SourceAtZoom < String > xpath = FileImageDescriptor .getxPath (url , zoom );
73
79
if (xpath != null ) {
74
- URL xPathUrl = getURL (xpath );
80
+ URL xPathUrl = getURL (xpath . source () );
75
81
if (xPathUrl != null ) {
76
82
return getFilePath (xPathUrl , logIOException );
77
83
}
@@ -127,7 +133,7 @@ public boolean equals(Object o) {
127
133
@ Deprecated
128
134
@ Override
129
135
public ImageData getImageData () {
130
- return getImageData (getURL (url ));
136
+ return getImageData (getURL (url ), 100 , 100 );
131
137
}
132
138
133
139
@ Override
@@ -138,32 +144,31 @@ public ImageData getImageData(int zoom) {
138
144
private static ImageData getImageData (String url , int zoom ) {
139
145
URL tempURL = getURL (url );
140
146
if (tempURL != null ) {
141
- if (zoom == 100 ) {
142
- return getImageData (tempURL );
147
+ if (zoom == 100 || canLoadAtZoom (() -> getStream ( tempURL ), zoom ) ) {
148
+ return getImageData (tempURL , 100 , zoom );
143
149
}
144
- URL xUrl = getxURL (tempURL , zoom );
150
+ SourceAtZoom < URL > xUrl = getxURL (tempURL , zoom );
145
151
if (xUrl != null ) {
146
- ImageData xdata = getImageData (xUrl );
152
+ ImageData xdata = getImageData (xUrl . source (), xUrl . zoom (), zoom );
147
153
if (xdata != null ) {
148
154
return xdata ;
149
155
}
150
156
}
151
- String xpath = FileImageDescriptor .getxPath (url , zoom );
157
+ SourceAtZoom < String > xpath = FileImageDescriptor .getxPath (url , zoom );
152
158
if (xpath != null ) {
153
- URL xPathUrl = getURL (xpath );
159
+ URL xPathUrl = getURL (xpath . source () );
154
160
if (xPathUrl != null ) {
155
- return getImageData (xPathUrl );
161
+ return getImageData (xPathUrl , xpath . zoom (), zoom );
156
162
}
157
163
}
158
164
}
159
165
return null ;
160
166
}
161
167
162
- private static ImageData getImageData (URL url ) {
163
- ImageData result = null ;
168
+ private static ImageData getImageData (URL url , int fileZoom , int targetZoom ) {
164
169
try (InputStream in = getStream (url )) {
165
170
if (in != null ) {
166
- result = new ImageData (in );
171
+ return loadImageFromStream (in , fileZoom , targetZoom );
167
172
}
168
173
} catch (SWTException e ) {
169
174
if (e .code != SWT .ERROR_INVALID_IMAGE ) {
@@ -173,7 +178,42 @@ private static ImageData getImageData(URL url) {
173
178
} catch (IOException e ) {
174
179
Policy .getLog ().log (Status .error (e .getLocalizedMessage (), e ));
175
180
}
176
- return result ;
181
+ return null ;
182
+ }
183
+
184
+ /**
185
+ * Represents an element, such as InputStream, Path or URL, at a specific zoom
186
+ * level.
187
+ *
188
+ * @param <T> type of the element to be presented, e.g., {@link InputStream}
189
+ */
190
+ record SourceAtZoom <T >(T source , int zoom ) {
191
+ public SourceAtZoom {
192
+ if (source == null ) {
193
+ SWT .error (SWT .ERROR_NULL_ARGUMENT );
194
+ }
195
+ if (zoom <= 0 ) {
196
+ SWT .error (SWT .ERROR_INVALID_ARGUMENT );
197
+ }
198
+ }
199
+ }
200
+
201
+ @ SuppressWarnings ("restriction" )
202
+ static boolean canLoadAtZoom (Supplier <InputStream > streamFactory , int zoom ) {
203
+ try (InputStream in = streamFactory .get ()) {
204
+ if (in != null ) {
205
+ return FileFormat .canLoadAtZoom (new ElementAtZoom <>(in , 100 ), zoom );
206
+ }
207
+ } catch (IOException e ) {
208
+ Policy .getLog ().log (Status .error (e .getLocalizedMessage (), e ));
209
+ }
210
+ return false ;
211
+ }
212
+
213
+ @ SuppressWarnings ("restriction" )
214
+ static ImageData loadImageFromStream (InputStream stream , int fileZoom , int targetZoom ) {
215
+ return NativeImageLoader .load (new ElementAtZoom <>(stream , fileZoom ), new ImageLoader (), targetZoom ).get (0 )
216
+ .element ();
177
217
}
178
218
179
219
/**
@@ -225,7 +265,7 @@ public String toString() {
225
265
return "URLImageDescriptor(" + url + ")" ; //$NON-NLS-1$ //$NON-NLS-2$
226
266
}
227
267
228
- private static URL getxURL (URL url , int zoom ) {
268
+ private static SourceAtZoom < URL > getxURL (URL url , int zoom ) {
229
269
String path = url .getPath ();
230
270
int dot = path .lastIndexOf ('.' );
231
271
if (dot != -1 && (zoom == 150 || zoom == 200 )) {
@@ -240,7 +280,7 @@ private static URL getxURL(URL url, int zoom) {
240
280
if (url .getQuery () != null ) {
241
281
file += '?' + url .getQuery ();
242
282
}
243
- return new URL (url .getProtocol (), url .getHost (), url .getPort (), file );
283
+ return new SourceAtZoom <>( new URL (url .getProtocol (), url .getHost (), url .getPort (), file ), zoom );
244
284
} catch (MalformedURLException e ) {
245
285
Policy .getLog ().log (Status .error (e .getLocalizedMessage (), e ));
246
286
}
0 commit comments