RENDERING_HINTS = Map.of(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON, //
+ KEY_ALPHA_INTERPOLATION, VALUE_ALPHA_INTERPOLATION_QUALITY, //
+ KEY_COLOR_RENDERING, VALUE_COLOR_RENDER_QUALITY, //
+ KEY_DITHERING, VALUE_DITHER_DISABLE, //
+ KEY_FRACTIONALMETRICS, VALUE_FRACTIONALMETRICS_ON, //
+ KEY_INTERPOLATION, VALUE_INTERPOLATION_BICUBIC, //
+ KEY_RENDERING, VALUE_RENDER_QUALITY, //
+ KEY_STROKE_CONTROL, VALUE_STROKE_PURE, //
+ KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON //
+ );
+
+ @Override
+ public ImageData rasterizeSVG(InputStream stream, float scalingFactor) throws IOException {
+ if(svgLoader == null) {
+ svgLoader = new SVGLoader();
+ }
+ SVGDocument svgDocument = null;
+ svgDocument = svgLoader.load(stream, null, LoaderContext.createDefault());
+ if (svgDocument != null) {
+ FloatSize size = svgDocument.size();
+ double originalWidth = size.getWidth();
+ double originalHeight = size.getHeight();
+ int scaledWidth = (int) Math.round(originalWidth * scalingFactor);
+ int scaledHeight = (int) Math.round(originalHeight * scalingFactor);
+ BufferedImage image = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
+ Graphics2D g = image.createGraphics();
+ g.setRenderingHints(RENDERING_HINTS);
+ g.scale(scalingFactor, scalingFactor);
+ svgDocument.render(null, g);
+ g.dispose();
+ return convertToSWT(image);
+ }
+ return null;
+ }
+
+ private ImageData convertToSWT(BufferedImage bufferedImage) {
+ if (bufferedImage.getColorModel() instanceof DirectColorModel) {
+ DirectColorModel colorModel = (DirectColorModel)bufferedImage.getColorModel();
+ PaletteData palette = new PaletteData(
+ colorModel.getRedMask(),
+ colorModel.getGreenMask(),
+ colorModel.getBlueMask());
+ ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
+ colorModel.getPixelSize(), palette);
+ for (int y = 0; y < data.height; y++) {
+ for (int x = 0; x < data.width; x++) {
+ int rgb = bufferedImage.getRGB(x, y);
+ int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
+ data.setPixel(x, y, pixel);
+ if (colorModel.hasAlpha()) {
+ data.setAlpha(x, y, (rgb >> 24) & 0xFF);
+ }
+ }
+ }
+ return data;
+ }
+ else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
+ IndexColorModel colorModel = (IndexColorModel)bufferedImage.getColorModel();
+ int size = colorModel.getMapSize();
+ byte[] reds = new byte[size];
+ byte[] greens = new byte[size];
+ byte[] blues = new byte[size];
+ colorModel.getReds(reds);
+ colorModel.getGreens(greens);
+ colorModel.getBlues(blues);
+ RGB[] rgbs = new RGB[size];
+ for (int i = 0; i < rgbs.length; i++) {
+ rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
+ }
+ PaletteData palette = new PaletteData(rgbs);
+ ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
+ colorModel.getPixelSize(), palette);
+ data.transparentPixel = colorModel.getTransparentPixel();
+ WritableRaster raster = bufferedImage.getRaster();
+ int[] pixelArray = new int[1];
+ for (int y = 0; y < data.height; y++) {
+ for (int x = 0; x < data.width; x++) {
+ raster.getPixel(x, y, pixelArray);
+ data.setPixel(x, y, pixelArray[0]);
+ }
+ }
+ return data;
+ }
+ else if (bufferedImage.getColorModel() instanceof ComponentColorModel) {
+ ComponentColorModel colorModel = (ComponentColorModel)bufferedImage.getColorModel();
+ //ASSUMES: 3 BYTE BGR IMAGE TYPE
+ PaletteData palette = new PaletteData(0x0000FF, 0x00FF00,0xFF0000);
+ ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
+ colorModel.getPixelSize(), palette);
+ //This is valid because we are using a 3-byte Data model with no transparent pixels
+ data.transparentPixel = -1;
+ WritableRaster raster = bufferedImage.getRaster();
+ int[] pixelArray = new int[3];
+ for (int y = 0; y < data.height; y++) {
+ for (int x = 0; x < data.width; x++) {
+ raster.getPixel(x, y, pixelArray);
+ int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
+ data.setPixel(x, y, pixel);
+ }
+ }
+ return data;
+ }
+ return null;
+ }
+
+ public boolean isSVGFile(InputStream inputStream) throws IOException {
+ if (inputStream == null) {
+ throw new IllegalArgumentException("InputStream cannot be null");
+ }
+ int firstByte = inputStream.read();
+ return firstByte == '<';
+ }
+}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java
index 5b063ab2e78..68dff4b8b3f 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java
@@ -14,9 +14,12 @@
package org.eclipse.swt.graphics;
+import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
+import javax.imageio.ImageIO;
+
import org.eclipse.swt.*;
import org.eclipse.swt.internal.image.*;
@@ -149,10 +152,72 @@ void reset() {
*
*/
public ImageData[] load(InputStream stream) {
+ return loadDefault(stream);
+}
+
+private ImageData[] loadDefault(InputStream stream) {
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
reset();
data = FileFormat.load(stream, this);
- return data;
+ return data;
+}
+
+/**
+ * Loads an array of ImageData
objects from the
+ * specified input stream. If the stream is a SVG File and zoom is not 0,
+ * this method will try to rasterize the SVG.
+ * Throws an error if either an error occurs while loading the images, or if the images are not
+ * of a supported type. Returns the loaded image data array.
+ *
+ * @param stream the input stream to load the images from
+ * @param zoom the zoom factor to apply when rasterizing a SVG.
+ * A value of 0 means that the standard method for loading should be used.
+ * @return an array of ImageData
objects loaded from the specified input stream
+ *
+ * @exception IllegalArgumentException
+ * - ERROR_NULL_ARGUMENT - if the stream is null
+ *
+ * @exception SWTException
+ * - ERROR_IO - if an IO error occurs while reading from the stream
+ * - ERROR_INVALID_IMAGE - if the image stream contains invalid data
+ * - ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format
+ *
+ *
+ * @since 3.129
+ */
+public ImageData[] load(InputStream stream, int zoom) {
+ if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
+ reset();
+ byte[] bytes = null;
+ try {
+ bytes = stream.readAllBytes();
+ } catch (IOException e) {
+ SWT.error(SWT.ERROR_IO, e);
+ }
+ ISVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer();
+ if (rasterizer != null && zoom != 0) {
+ try {
+ float scalingFactor = zoom / 100.0f;
+ BufferedImage image = rasterizer.rasterizeSVG(bytes, scalingFactor);
+ if(image != null) {
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ ImageIO.write(image, "png", baos);
+ try (InputStream in = new ByteArrayInputStream(baos.toByteArray())) {
+ data = FileFormat.load(in, this);
+ return data;
+ }
+ }
+ }
+ } catch (IOException e) {
+ // try standard method
+ }
+ }
+ try (InputStream fallbackStream = new ByteArrayInputStream(bytes)) {
+ return loadDefault(fallbackStream);
+ } catch (IOException e) {
+ SWT.error(SWT.ERROR_IO, e);
+ }
+ return null;
}
/**
@@ -175,18 +240,43 @@ public ImageData[] load(InputStream stream) {
*/
public ImageData[] load(String filename) {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
- InputStream stream = null;
- try {
- stream = new FileInputStream(filename);
- return load(stream);
+ try (InputStream stream = new FileInputStream(filename)) {
+ return loadDefault(stream);
+ } catch (IOException e) {
+ SWT.error(SWT.ERROR_IO, e);
+ }
+ return null;
+}
+
+/**
+ * Loads an array of ImageData
objects from the
+ * file with the specified name. If the filename is a SVG File and zoom is not 0,
+ * this method will try to rasterize the SVG. Throws an error if either
+ * an error occurs while loading the images, or if the images are
+ * not of a supported type. Returns the loaded image data array.
+ *
+ * @param filename the name of the file to load the images from
+ * @param zoom the zoom factor to apply when rasterizing a SVG.
+ * A value of 0 means that the standard method for loading should be used.
+ * @return an array of ImageData
objects loaded from the specified file
+ *
+ * @exception IllegalArgumentException
+ * - ERROR_NULL_ARGUMENT - if the file name is null
+ *
+ * @exception SWTException
+ * - ERROR_IO - if an IO error occurs while reading from the file
+ * - ERROR_INVALID_IMAGE - if the image file contains invalid data
+ * - ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format
+ *
+ *
+ * @since 3.129
+ */
+public ImageData[] load(String filename, int zoom) {
+ if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
+ try (InputStream stream = new FileInputStream(filename)) {
+ return load(stream, zoom);
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
- } finally {
- try {
- if (stream != null) stream.close();
- } catch (IOException e) {
- // Ignore error
- }
}
return null;
}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java
index c268e5caaed..a0d03843db1 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java
@@ -331,7 +331,39 @@ scanlinePad, checkData(data), 0, null,
* @see ImageLoader#load(InputStream)
*/
public ImageData(InputStream stream) {
- ImageData[] data = ImageDataLoader.load(stream);
+ this(stream, 0);
+}
+
+/**
+ * Constructs an ImageData
loaded from the specified
+ * input stream. Throws an error if an error occurs while loading
+ * the image, or if the image has an unsupported type. Application
+ * code is still responsible for closing the input stream.
+ *
+ * This constructor is provided for convenience when loading a single
+ * image only. If the stream contains multiple images, only the first
+ * one will be loaded. To load multiple images, use
+ * ImageLoader.load()
.
+ *
+ *
+ * @param stream the input stream to load the image from (must not be null)
+ * @param zoom the zoom factor to apply when rasterizing a SVG.
+ * A value of 0 means that the standard method for loading should be used.
+ *
+ * @exception IllegalArgumentException
+ * - ERROR_NULL_ARGUMENT - if the stream is null
+ *
+ * @exception SWTException
+ * - ERROR_IO - if an IO error occurs while reading from the stream
+ * - ERROR_INVALID_IMAGE - if the image stream contains invalid data
+ * - ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format
+ *
+ *
+ * @see ImageLoader#load(InputStream)
+ * @since 3.129
+ */
+public ImageData(InputStream stream, int zoom) {
+ ImageData[] data = ImageDataLoader.load(stream, zoom);
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
ImageData i = data[0];
setAllFields(
@@ -377,7 +409,36 @@ public ImageData(InputStream stream) {
*
*/
public ImageData(String filename) {
- ImageData[] data = ImageDataLoader.load(filename);
+ this(filename, 0);
+}
+
+/**
+ * Constructs an ImageData
loaded from a file with the
+ * specified name. Throws an error if an error occurs loading the
+ * image, or if the image has an unsupported type.
+ *
+ * This constructor is provided for convenience when loading a single
+ * image only. If the file contains multiple images, only the first
+ * one will be loaded. To load multiple images, use
+ * ImageLoader.load()
.
+ *
+ *
+ * @param filename the name of the file to load the image from (must not be null)
+ * @param zoom the zoom factor to apply when rasterizing a SVG.
+ * A value of 0 means that the standard method for loading should be used.
+ * @exception IllegalArgumentException
+ * - ERROR_NULL_ARGUMENT - if the file name is null
+ *
+ * @exception SWTException
+ * - ERROR_IO - if an IO error occurs while reading from the file
+ * - ERROR_INVALID_IMAGE - if the image file contains invalid data
+ * - ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format
+ *
+ *
+ * @since 3.129
+ */
+public ImageData(String filename, int zoom) {
+ ImageData[] data = ImageDataLoader.load(filename, zoom);
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
ImageData i = data[0];
setAllFields(
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java
index b1fe23d2472..9c9da788bfa 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java
@@ -25,8 +25,16 @@ public static ImageData[] load(InputStream stream) {
return new ImageLoader().load(stream);
}
- public static ImageData[] load(String filename) {
+ public static ImageData[] load(InputStream stream, int zoom) {
+ return new ImageLoader().load(stream, zoom);
+ }
+
+ public static ImageData[] load(String filename) {
return new ImageLoader().load(filename);
}
+ public static ImageData[] load(String filename, int zoom) {
+ return new ImageLoader().load(filename, zoom);
+ }
+
}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizer.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizer.java
new file mode 100644
index 00000000000..6da8c5b23ff
--- /dev/null
+++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizer.java
@@ -0,0 +1,47 @@
+/*******************************************************************************
+ * Copyright (c) 2024 Vector Informatik GmbH and others.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License 2.0 which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors: Vector Informatik GmbH - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.swt.graphics;
+
+import java.io.*;
+
+/**
+ * Defines the interface for an SVG rasterizer, responsible for converting SVG
+ * data into rasterized images.
+ *
+ * @since 3.129
+ */
+public interface SVGRasterizer {
+
+ /**
+ * Rasterizes an SVG image from the provided byte array, using the specified
+ * zoom factor.
+ *
+ * @param stream the SVG image as an {@link InputStream}.
+ * @param scalingFactor the scaling ratio e.g. 2.0 for doubled size.
+ * @return the {@link ImageData} for the rasterized image, or
+ * {@code null} if the input is not a valid SVG file or cannot be
+ * processed.
+ * @throws IOException if an error occurs while reading the SVG data.
+ */
+ public ImageData rasterizeSVG(InputStream stream, float scalingFactor) throws IOException;
+
+ /**
+ * Determines whether the given {@link InputStream} contains a SVG file.
+ *
+ * @param inputStream the input stream to check.
+ * @return {@code true} if the input stream contains SVG content; {@code false}
+ * otherwise.
+ * @throws IOException if an error occurs while reading the stream.
+ * @throws IllegalArgumentException if the input stream is {@code null}.
+ */
+ public boolean isSVGFile(InputStream inputStream) throws IOException;
+}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizerRegistry.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizerRegistry.java
new file mode 100644
index 00000000000..25e148707fc
--- /dev/null
+++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizerRegistry.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Copyright (c) 2023 Vector Informatik GmbH and others.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License 2.0 which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors: Vector Informatik GmbH - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.swt.graphics;
+
+/**
+ * A registry for managing the instance of an {@link SVGRasterizer} implementation.
+ * This allows for the registration and retrieval of a single rasterizer instance.
+ *
+ * @since 3.129
+ */
+public class SVGRasterizerRegistry {
+
+ /**
+ * The instance of the registered {@link SVGRasterizer}.
+ */
+ private static SVGRasterizer rasterizer;
+
+ /**
+ * Registers the provided implementation of {@link SVGRasterizer}.
+ * If a rasterizer has already been registered, subsequent calls to this method
+ * will have no effect.
+ *
+ * @param implementation the {@link SVGRasterizer} implementation to register.
+ */
+ public static void register(SVGRasterizer implementation) {
+ if (rasterizer == null) {
+ rasterizer = implementation;
+ }
+ }
+
+ /**
+ * Retrieves the currently registered {@link SVGRasterizer} implementation.
+ *
+ * @return the registered {@link SVGRasterizer}, or {@code null} if no implementation
+ * has been registered.
+ */
+ public static SVGRasterizer getRasterizer() {
+ return rasterizer;
+ }
+}
\ No newline at end of file
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java
index 89b97cd86e6..212258355ec 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java
@@ -15,10 +15,13 @@
package org.eclipse.swt.graphics;
+import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.List;
+import javax.imageio.ImageIO;
+
import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.gtk.*;
@@ -159,11 +162,73 @@ void reset() {
*
*/
public ImageData[] load(InputStream stream) {
+ return loadDefault(stream);
+}
+
+private ImageData[] loadDefault(InputStream stream) {
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
reset();
- ImageData [] imgDataArray = getImageDataArrayFromStream(stream);
- data = imgDataArray;
- return imgDataArray;
+ data = getImageDataArrayFromStream(stream);
+ return data;
+}
+
+/**
+ * Loads an array of ImageData
objects from the
+ * specified input stream. If the stream is a SVG File and zoom is not 0,
+ * this method will try to rasterize the SVG.
+ * Throws an error if either an error occurs while loading the images, or if the images are not
+ * of a supported type. Returns the loaded image data array.
+ *
+ * @param stream the input stream to load the images from
+ * @param zoom the zoom factor to apply when rasterizing a SVG.
+ * A value of 0 means that the standard method for loading should be used.
+ * @return an array of ImageData
objects loaded from the specified input stream
+ *
+ * @exception IllegalArgumentException
+ * - ERROR_NULL_ARGUMENT - if the stream is null
+ *
+ * @exception SWTException
+ * - ERROR_IO - if an IO error occurs while reading from the stream
+ * - ERROR_INVALID_IMAGE - if the image stream contains invalid data
+ * - ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format
+ *
+ *
+ * @since 3.129
+ */
+public ImageData[] load(InputStream stream, int zoom) {
+ if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
+ reset();
+ byte[] bytes = null;
+ try {
+ bytes = stream.readAllBytes();
+ } catch (IOException e) {
+ SWT.error(SWT.ERROR_IO, e);
+ }
+ ISVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer();
+ if (rasterizer != null && zoom != 0) {
+ try {
+ float scalingFactor = zoom / 100.0f;
+ BufferedImage image = rasterizer.rasterizeSVG(bytes, scalingFactor);
+ if(image != null) {
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ ImageIO.write(image, "png", baos);
+ try (InputStream in = new ByteArrayInputStream(baos.toByteArray())) {
+ data = getImageDataArrayFromStream(in);
+ return data;
+ }
+ }
+ }
+ } catch (IOException e) {
+ // try standard method
+ }
+ }
+ try (InputStream fallbackStream = new ByteArrayInputStream(bytes)) {
+ data = getImageDataArrayFromStream(stream);
+ return data;
+ } catch (IOException e) {
+ SWT.error(SWT.ERROR_IO, e);
+ }
+ return null;
}
/**
@@ -294,18 +359,43 @@ boolean isInterlacedPNG(byte [] imageAsByteArray) {
*/
public ImageData[] load(String filename) {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
- InputStream stream = null;
- try {
- stream = new FileInputStream(filename);
- return load(stream);
+ try (InputStream stream = new FileInputStream(filename)) {
+ return loadDefault(stream);
+ } catch (IOException e) {
+ SWT.error(SWT.ERROR_IO, e);
+ }
+ return null;
+}
+
+/**
+ * Loads an array of ImageData
objects from the
+ * file with the specified name. If the filename is a SVG File and zoom is not 0,
+ * this method will try to rasterize the SVG. Throws an error if either
+ * an error occurs while loading the images, or if the images are
+ * not of a supported type. Returns the loaded image data array.
+ *
+ * @param filename the name of the file to load the images from
+ * @param zoom the zoom factor to apply when rasterizing a SVG.
+ * A value of 0 means that the standard method for loading should be used.
+ * @return an array of ImageData
objects loaded from the specified file
+ *
+ * @exception IllegalArgumentException
+ * - ERROR_NULL_ARGUMENT - if the file name is null
+ *
+ * @exception SWTException
+ * - ERROR_IO - if an IO error occurs while reading from the file
+ * - ERROR_INVALID_IMAGE - if the image file contains invalid data
+ * - ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format
+ *
+ *
+ * @since 3.129
+ */
+public ImageData[] load(String filename, int zoom) {
+ if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
+ try (InputStream stream = new FileInputStream(filename)) {
+ return load(stream, zoom);
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
- } finally {
- try {
- if (stream != null) stream.close();
- } catch (IOException e) {
- // Ignore error
- }
}
return null;
}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java
index c4013a30a08..f6c5e7c34c3 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java
@@ -468,7 +468,7 @@ public Image(Device device, ImageData source, ImageData mask) {
public Image (Device device, InputStream stream) {
super(device);
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
- ImageData data = DPIUtil.autoScaleUp(device, new ElementAtZoom<>(new ImageData (stream), 100));
+ ImageData data = DPIUtil.autoScaleUp(device, new ElementAtZoom<>(new ImageData (stream, getZoom()), 100));
init(data, getZoom());
init();
this.device.registerResourceWithZoomSupport(this);
@@ -510,7 +510,7 @@ public Image (Device device, String filename) {
super(device);
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
- ImageData data = DPIUtil.autoScaleUp(device, new ElementAtZoom<>(new ImageData (filename), 100));
+ ImageData data = DPIUtil.autoScaleUp(device, new ElementAtZoom<>(new ImageData (filename, getZoom()), 100));
init(data, getZoom());
init();
this.device.registerResourceWithZoomSupport(this);
@@ -553,10 +553,10 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
if (fileName.zoom() == getZoom()) {
ImageHandle imageMetadata = initNative (fileName.element(), getZoom());
if (imageMetadata == null) {
- init(new ImageData (fileName.element()), getZoom());
+ init(new ImageData (fileName.element(), getZoom()), getZoom());
}
} else {
- ImageData resizedData = DPIUtil.autoScaleImageData (device, new ImageData (fileName.element()), fileName.zoom());
+ ImageData resizedData = DPIUtil.autoScaleImageData (device, new ImageData (fileName.element(), getZoom()), fileName.zoom());
init(resizedData, getZoom());
}
init();
@@ -753,7 +753,7 @@ private ImageHandle getImageMetadata(int zoom) {
if (imageFileNameProvider != null) {
ElementAtZoom imageCandidate = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
- ImageData imageData = new ImageData (imageCandidate.element());
+ ImageData imageData = new ImageData (imageCandidate.element(), zoom);
if (imageCandidate.zoom() == zoom) {
/* Release current native resources */
ImageHandle imageMetadata = initNative(imageCandidate.element(), zoom);
@@ -1389,7 +1389,7 @@ public ImageData getImageData (int zoom) {
return DPIUtil.scaleImageData (device, data.element(), zoom, data.zoom());
} else if (imageFileNameProvider != null) {
ElementAtZoom fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
- return DPIUtil.scaleImageData (device, new ImageData (fileName.element()), zoom, fileName.zoom());
+ return DPIUtil.scaleImageData (device, new ImageData (fileName.element(), zoom), zoom, fileName.zoom());
}
// if a GC is initialized with an Image (memGC != null), the image data must not be resized, because it would
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java
index a8e4c2f684c..afd2da911d8 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java
@@ -149,10 +149,71 @@ void reset() {
*
*/
public ImageData[] load(InputStream stream) {
+ return loadDefault(stream);
+}
+
+/**
+ * Loads an array of ImageData
objects from the
+ * specified input stream. If the stream is a SVG File and zoom is not 0,
+ * this method will try to rasterize the SVG.
+ * Throws an error if either an error occurs while loading the images, or if the images are not
+ * of a supported type. Returns the loaded image data array.
+ *
+ * @param stream the input stream to load the images from
+ * @param zoom the zoom factor to apply when rasterizing a SVG.
+ * A value of 0 means that the standard method for loading should be used.
+ * @return an array of ImageData
objects loaded from the specified input stream
+ *
+ * @exception IllegalArgumentException
+ * - ERROR_NULL_ARGUMENT - if the stream is null
+ *
+ * @exception SWTException
+ * - ERROR_IO - if an IO error occurs while reading from the stream
+ * - ERROR_INVALID_IMAGE - if the image stream contains invalid data
+ * - ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format
+ *
+ *
+ * @since 3.129
+ */
+public ImageData[] load(InputStream stream, int zoom) {
+ if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
+ reset();
+ byte[] bytes = null;
+ try {
+ bytes = stream.readAllBytes();
+ } catch (IOException e) {
+ SWT.error(SWT.ERROR_IO, e);
+ }
+ SVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer();
+ if (rasterizer != null && zoom != 0) {
+ try (InputStream imageStream = new ByteArrayInputStream(bytes)) {
+ if (rasterizer.isSVGFile(imageStream)) {
+ float scalingFactor = zoom / 100.0f;
+ try (InputStream svgFileStream = new ByteArrayInputStream(bytes)) {
+ ImageData rasterizedData = rasterizer.rasterizeSVG(svgFileStream, scalingFactor);
+ if (rasterizedData != null) {
+ data = new ImageData[]{rasterizedData};
+ return data;
+ }
+ }
+ }
+ } catch (IOException e) {
+ //ignore.
+ }
+ }
+ try (InputStream fallbackStream = new ByteArrayInputStream(bytes)) {
+ return loadDefault(fallbackStream);
+ } catch (IOException e) {
+ SWT.error(SWT.ERROR_IO, e);
+ }
+ return null;
+}
+
+private ImageData[] loadDefault(InputStream stream) {
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
reset();
data = FileFormat.load(stream, this);
- return data;
+ return data;
}
/**
@@ -176,7 +237,40 @@ public ImageData[] load(InputStream stream) {
public ImageData[] load(String filename) {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
try (InputStream stream = new FileInputStream(filename)) {
- return load(stream);
+ return loadDefault(stream);
+ } catch (IOException e) {
+ SWT.error(SWT.ERROR_IO, e);
+ }
+ return null;
+}
+
+/**
+ * Loads an array of ImageData
objects from the
+ * file with the specified name. If the filename is a SVG File and zoom is not 0,
+ * this method will try to rasterize the SVG. Throws an error if either
+ * an error occurs while loading the images, or if the images are
+ * not of a supported type. Returns the loaded image data array.
+ *
+ * @param filename the name of the file to load the images from
+ * @param zoom the zoom factor to apply when rasterizing a SVG.
+ * A value of 0 means that the standard method for loading should be used.
+ * @return an array of ImageData
objects loaded from the specified file
+ *
+ * @exception IllegalArgumentException
+ * - ERROR_NULL_ARGUMENT - if the file name is null
+ *
+ * @exception SWTException
+ * - ERROR_IO - if an IO error occurs while reading from the file
+ * - ERROR_INVALID_IMAGE - if the image file contains invalid data
+ * - ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format
+ *
+ *
+ * @since 3.129
+ */
+public ImageData[] load(String filename, int zoom) {
+ if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
+ try (InputStream stream = new FileInputStream(filename)) {
+ return load(stream, zoom);
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
}