1717 *******************************************************************************/
1818package org .eclipse .jface .resource ;
1919
20+
2021import java .io .BufferedInputStream ;
2122import java .io .IOException ;
2223import java .io .InputStream ;
2324import java .net .MalformedURLException ;
2425import java .net .URL ;
2526import java .nio .file .Files ;
2627import java .nio .file .Path ;
28+ import java .util .function .Supplier ;
2729
2830import org .eclipse .core .runtime .FileLocator ;
2931import org .eclipse .core .runtime .IAdaptable ;
3840import org .eclipse .swt .graphics .ImageData ;
3941import org .eclipse .swt .graphics .ImageDataProvider ;
4042import 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 ;
4147
4248/**
4349 * An ImageDescriptor that gets its information from a URL. This class is not
@@ -127,7 +133,7 @@ public boolean equals(Object o) {
127133 @ Deprecated
128134 @ Override
129135 public ImageData getImageData () {
130- return getImageData (getURL (url ));
136+ return getImageData (getURL (url ), 100 , 100 );
131137 }
132138
133139 @ Override
@@ -138,12 +144,12 @@ public ImageData getImageData(int zoom) {
138144 private static ImageData getImageData (String url , int zoom ) {
139145 URL tempURL = getURL (url );
140146 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 );
143149 }
144150 URL xUrl = getxURL (tempURL , zoom );
145151 if (xUrl != null ) {
146- ImageData xdata = getImageData (xUrl );
152+ ImageData xdata = getImageData (xUrl , zoom , zoom );
147153 if (xdata != null ) {
148154 return xdata ;
149155 }
@@ -152,28 +158,50 @@ private static ImageData getImageData(String url, int zoom) {
152158 if (xpath != null ) {
153159 URL xPathUrl = getURL (xpath );
154160 if (xPathUrl != null ) {
155- return getImageData (xPathUrl );
161+ return getImageData (xPathUrl , zoom , zoom );
156162 }
157163 }
158164 }
159165 return null ;
160166 }
161167
162- private static ImageData getImageData (URL url ) {
163- ImageData result = null ;
164- try (InputStream in = getStream (url )) {
165- if (in != null ) {
166- result = new ImageData (in );
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 in , int fileZoom , int targetZoom ) {
174+ if (in != null ) {
175+ try (InputStream stream = new BufferedInputStream (in )) {
176+ return loadImageFromStream (in , fileZoom , targetZoom );
177+ } catch (SWTException e ) {
178+ if (e .code != SWT .ERROR_INVALID_IMAGE ) {
179+ throw e ;
180+ // fall through otherwise
181+ }
182+ } catch (IOException e ) {
183+ Policy .getLog ().log (Status .error (e .getLocalizedMessage (), e ));
167184 }
168- } catch (SWTException e ) {
169- if (e .code != SWT .ERROR_INVALID_IMAGE ) {
170- throw e ;
171- // fall through otherwise
185+ }
186+ return null ;
187+ }
188+
189+ @ SuppressWarnings ("restriction" )
190+ private static ImageData loadImageFromStream (InputStream stream , int fileZoom , int targetZoom ) {
191+ return NativeImageLoader .load (new ElementAtZoom <>(stream , fileZoom ), new ImageLoader (), targetZoom ).get (0 )
192+ .element ();
193+ }
194+
195+ @ SuppressWarnings ("restriction" )
196+ static boolean canLoadAtZoom (Supplier <InputStream > stream , int zoom ) {
197+ try (InputStream in = stream .get ()) {
198+ if (in != null ) {
199+ return FileFormat .canLoadAtZoom (new ElementAtZoom <>(in , 100 ), zoom );
172200 }
173201 } catch (IOException e ) {
174202 Policy .getLog ().log (Status .error (e .getLocalizedMessage (), e ));
175203 }
176- return result ;
204+ return false ;
177205 }
178206
179207 /**
@@ -198,7 +226,7 @@ private static InputStream getStream(URL url) {
198226 url = platformURL ;
199227 }
200228 }
201- return new BufferedInputStream ( url .openStream () );
229+ return url .openStream ();
202230 } catch (IOException e ) {
203231 if (InternalPolicy .DEBUG_LOG_URL_IMAGE_DESCRIPTOR_MISSING_2x ) {
204232 String path = url .getPath ();
0 commit comments