1
1
/*******************************************************************************
2
- * Copyright (c) 2000, 2022 IBM Corporation and others.
2
+ * Copyright (c) 2000, 2025 IBM Corporation and others.
3
3
*
4
4
* This program and the accompanying materials
5
5
* are made available under the terms of the Eclipse Public License 2.0
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
@@ -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,12 +144,12 @@ 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
150
URL xUrl = getxURL (tempURL , zoom );
145
151
if (xUrl != null ) {
146
- ImageData xdata = getImageData (xUrl );
152
+ ImageData xdata = getImageData (xUrl , zoom , zoom );
147
153
if (xdata != null ) {
148
154
return xdata ;
149
155
}
@@ -152,18 +158,23 @@ private static ImageData getImageData(String url, int zoom) {
152
158
if (xpath != null ) {
153
159
URL xPathUrl = getURL (xpath );
154
160
if (xPathUrl != null ) {
155
- return getImageData (xPathUrl );
161
+ return getImageData (xPathUrl , zoom , zoom );
156
162
}
157
163
}
158
164
}
159
165
return null ;
160
166
}
161
167
162
- private static ImageData getImageData (URL url ) {
168
+ @ SuppressWarnings ("resource" )
169
+ private static ImageData getImageData (URL url , int fileZoom , int targetZoom ) {
170
+ return loadImageData (getStream (url ), fileZoom , targetZoom );
171
+ }
172
+
173
+ static ImageData loadImageData (InputStream stream , int fileZoom , int targetZoom ) {
163
174
ImageData result = null ;
164
- try (InputStream in = getStream ( url ) ) {
175
+ try (InputStream in = stream ) {
165
176
if (in != null ) {
166
- result = new ImageData (in );
177
+ return loadImageFromStream ( new BufferedInputStream (in ), fileZoom , targetZoom );
167
178
}
168
179
} catch (SWTException e ) {
169
180
if (e .code != SWT .ERROR_INVALID_IMAGE ) {
@@ -176,6 +187,24 @@ private static ImageData getImageData(URL url) {
176
187
return result ;
177
188
}
178
189
190
+ @ SuppressWarnings ("restriction" )
191
+ private static ImageData loadImageFromStream (InputStream stream , int fileZoom , int targetZoom ) {
192
+ return NativeImageLoader .load (new ElementAtZoom <>(stream , fileZoom ), new ImageLoader (), targetZoom ).get (0 )
193
+ .element ();
194
+ }
195
+
196
+ @ SuppressWarnings ("restriction" )
197
+ static boolean canLoadAtZoom (Supplier <InputStream > stream , int zoom ) {
198
+ try (InputStream in = stream .get ()) {
199
+ if (in != null ) {
200
+ return FileFormat .canLoadAtZoom (new ElementAtZoom <>(in , 100 ), zoom );
201
+ }
202
+ } catch (IOException e ) {
203
+ Policy .getLog ().log (Status .error (e .getLocalizedMessage (), e ));
204
+ }
205
+ return false ;
206
+ }
207
+
179
208
/**
180
209
* Returns a stream on the image contents. Returns null if a stream could
181
210
* not be opened.
@@ -198,7 +227,7 @@ private static InputStream getStream(URL url) {
198
227
url = platformURL ;
199
228
}
200
229
}
201
- return new BufferedInputStream ( url .openStream () );
230
+ return url .openStream ();
202
231
} catch (IOException e ) {
203
232
if (InternalPolicy .DEBUG_LOG_URL_IMAGE_DESCRIPTOR_MISSING_2x ) {
204
233
String path = url .getPath ();
0 commit comments