diff --git a/bundles/org.eclipse.jface/.settings/org.eclipse.jdt.core.prefs b/bundles/org.eclipse.jface/.settings/org.eclipse.jdt.core.prefs index dca32ecd2de..cf36eec4aa2 100644 --- a/bundles/org.eclipse.jface/.settings/org.eclipse.jdt.core.prefs +++ b/bundles/org.eclipse.jface/.settings/org.eclipse.jdt.core.prefs @@ -46,7 +46,7 @@ org.eclipse.jdt.core.compiler.problem.deadCode=warning org.eclipse.jdt.core.compiler.problem.deprecation=warning org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled -org.eclipse.jdt.core.compiler.problem.discouragedReference=error +org.eclipse.jdt.core.compiler.problem.discouragedReference=warning org.eclipse.jdt.core.compiler.problem.emptyStatement=warning org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FileImageDescriptor.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FileImageDescriptor.java index bf566eb291a..8fc4e967f23 100644 --- a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FileImageDescriptor.java +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FileImageDescriptor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2022 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -16,7 +16,8 @@ *******************************************************************************/ package org.eclipse.jface.resource; -import java.io.BufferedInputStream; +import static org.eclipse.jface.resource.URLImageDescriptor.loadImageData; + import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -32,7 +33,6 @@ import org.eclipse.core.runtime.Status; import org.eclipse.jface.internal.InternalPolicy; import org.eclipse.jface.util.Policy; -import org.eclipse.swt.SWT; import org.eclipse.swt.SWTException; import org.eclipse.swt.graphics.Device; import org.eclipse.swt.graphics.Image; @@ -122,20 +122,7 @@ public boolean equals(Object o) { */ @Override public ImageData getImageData(int zoom) { - InputStream in = getStream(zoom); - if (in != null) { - try (BufferedInputStream stream = new BufferedInputStream(in)) { - return new ImageData(stream); - } catch (SWTException e) { - if (e.code != SWT.ERROR_INVALID_IMAGE) { - throw e; - // fall through otherwise - } - } catch (IOException ioe) { - // fall through - } - } - return null; + return getImageData(name, zoom); } /** @@ -146,19 +133,20 @@ public ImageData getImageData(int zoom) { * @return the buffered stream on the file or null if the * file cannot be found */ - private InputStream getStream(int zoom) { - if (zoom == 100) { - return getStream(name); + @SuppressWarnings("resource") + private ImageData getImageData(String name, int zoom) { + if (zoom == 100 || URLImageDescriptor.canLoadAtZoom(() -> getStream(name), zoom)) { + return loadImageData(getStream(name), 100, zoom); } InputStream xstream = getStream(getxName(name, zoom)); if (xstream != null) { - return xstream; + return loadImageData(xstream, zoom, zoom); } InputStream xpath = getStream(getxPath(name, zoom)); if (xpath != null) { - return xpath; + return loadImageData(xpath, zoom, zoom); } return null; diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java index 40b9054bcb9..e9cb13bd676 100644 --- a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2022 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -17,6 +17,7 @@ *******************************************************************************/ package org.eclipse.jface.resource; + import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -24,6 +25,7 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.function.Supplier; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IAdaptable; @@ -38,6 +40,10 @@ import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.ImageDataProvider; import org.eclipse.swt.graphics.ImageFileNameProvider; +import org.eclipse.swt.graphics.ImageLoader; +import org.eclipse.swt.internal.DPIUtil.ElementAtZoom; +import org.eclipse.swt.internal.NativeImageLoader; +import org.eclipse.swt.internal.image.FileFormat; /** * An ImageDescriptor that gets its information from a URL. This class is not @@ -127,7 +133,7 @@ public boolean equals(Object o) { @Deprecated @Override public ImageData getImageData() { - return getImageData(getURL(url)); + return getImageData(getURL(url), 100, 100); } @Override @@ -138,12 +144,12 @@ public ImageData getImageData(int zoom) { private static ImageData getImageData(String url, int zoom) { URL tempURL = getURL(url); if (tempURL != null) { - if (zoom == 100) { - return getImageData(tempURL); + if (zoom == 100 || canLoadAtZoom(() -> getStream(tempURL), zoom)) { + return getImageData(tempURL, 100, zoom); } URL xUrl = getxURL(tempURL, zoom); if (xUrl != null) { - ImageData xdata = getImageData(xUrl); + ImageData xdata = getImageData(xUrl, zoom, zoom); if (xdata != null) { return xdata; } @@ -152,18 +158,23 @@ private static ImageData getImageData(String url, int zoom) { if (xpath != null) { URL xPathUrl = getURL(xpath); if (xPathUrl != null) { - return getImageData(xPathUrl); + return getImageData(xPathUrl, zoom, zoom); } } } return null; } - private static ImageData getImageData(URL url) { + @SuppressWarnings("resource") + private static ImageData getImageData(URL url, int fileZoom, int targetZoom) { + return loadImageData(getStream(url), fileZoom, targetZoom); + } + + static ImageData loadImageData(InputStream stream, int fileZoom, int targetZoom) { ImageData result = null; - try (InputStream in = getStream(url)) { + try (InputStream in = stream) { if (in != null) { - result = new ImageData(in); + return loadImageFromStream(new BufferedInputStream(in), fileZoom, targetZoom); } } catch (SWTException e) { if (e.code != SWT.ERROR_INVALID_IMAGE) { @@ -176,6 +187,24 @@ private static ImageData getImageData(URL url) { return result; } + @SuppressWarnings("restriction") + private static ImageData loadImageFromStream(InputStream stream, int fileZoom, int targetZoom) { + return NativeImageLoader.load(new ElementAtZoom<>(stream, fileZoom), new ImageLoader(), targetZoom).get(0) + .element(); + } + + @SuppressWarnings("restriction") + static boolean canLoadAtZoom(Supplier stream, int zoom) { + try (InputStream in = stream.get()) { + if (in != null) { + return FileFormat.canLoadAtZoom(new ElementAtZoom<>(in, 100), zoom); + } + } catch (IOException e) { + Policy.getLog().log(Status.error(e.getLocalizedMessage(), e)); + } + return false; + } + /** * Returns a stream on the image contents. Returns null if a stream could * not be opened. @@ -198,7 +227,7 @@ private static InputStream getStream(URL url) { url = platformURL; } } - return new BufferedInputStream(url.openStream()); + return url.openStream(); } catch (IOException e) { if (InternalPolicy.DEBUG_LOG_URL_IMAGE_DESCRIPTOR_MISSING_2x) { String path = url.getPath();