diff --git a/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java b/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java index 156cf06096e..64266db588a 100644 --- a/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java +++ b/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java @@ -23,7 +23,6 @@ import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.internal.SVGRasterizer; - import com.github.weisj.jsvg.*; import com.github.weisj.jsvg.geometry.size.*; import com.github.weisj.jsvg.parser.*; @@ -35,7 +34,7 @@ * @since 1.0.0 */ public class JSVGRasterizer implements SVGRasterizer { - + private SVGLoader svgLoader; private final static Map RENDERING_HINTS = Map.of(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON, // @@ -50,120 +49,123 @@ public class JSVGRasterizer implements SVGRasterizer { ); @Override - public ImageData rasterizeSVG(InputStream stream, float scalingFactor) throws IOException { + public ImageData[] rasterizeSVG(InputStream stream, int zoom) throws IOException { if (stream == null) { - throw new IllegalArgumentException("InputStream cannot be null"); - } - stream.mark(Integer.MAX_VALUE); - if(svgLoader == null) { - svgLoader = new SVGLoader(); + throw new IllegalArgumentException("InputStream cannot be null"); + } + if (!stream.markSupported()) { + stream = new BufferedInputStream(stream); } - SVGDocument svgDocument = null; - InputStream nonClosingStream = new FilterInputStream(stream) { - @Override - public void close() throws IOException { - // Do nothing to prevent closing the underlying stream - } - }; - svgDocument = svgLoader.load(nonClosingStream, null, LoaderContext.createDefault()); - stream.reset(); - 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); - } else { - SWT.error(SWT.ERROR_INVALID_IMAGE); - return null; + if (isSVGFile(stream)) { + stream.mark(Integer.MAX_VALUE); + if (svgLoader == null) { + svgLoader = new SVGLoader(); + } + SVGDocument svgDocument = null; + InputStream nonClosingStream = new FilterInputStream(stream) { + @Override + public void close() throws IOException { + // Do nothing to prevent closing the underlying stream + } + }; + svgDocument = svgLoader.load(nonClosingStream, null, LoaderContext.createDefault()); + stream.reset(); + if (svgDocument != null) { + float scalingFactor = zoom / 100.0f; + 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 new ImageData[] { convertToSWT(image) }; + } else { + SWT.error(SWT.ERROR_INVALID_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; + 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 stream) throws IOException { + private boolean isSVGFile(InputStream stream) throws IOException { if (stream == null) { - throw new IllegalArgumentException("InputStream cannot be null"); - } + throw new IllegalArgumentException("InputStream cannot be null"); + } stream.mark(Integer.MAX_VALUE); try { - int firstByte = stream.read(); - return firstByte == '<'; + int firstByte = stream.read(); + return firstByte == '<'; } finally { stream.reset(); } 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 6d1ca19b532..5b063ab2e78 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,12 +14,9 @@ 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.*; @@ -152,61 +149,10 @@ 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 - * @exception SWTException - * - * @since 3.129 - */ -public ImageData[] load(InputStream stream, int zoom) { - if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - reset(); - if (!stream.markSupported()) { - stream = new BufferedInputStream(stream); - } - SVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer(); - if (rasterizer != null && zoom != 0) { - try { - if (rasterizer.isSVGFile(stream)) { - float scalingFactor = zoom / 100.0f; - ImageData rasterizedData = rasterizer.rasterizeSVG(stream, scalingFactor); - if (rasterizedData != null) { - data = new ImageData[]{rasterizedData}; - return data; - } - } - } catch (IOException e) { - //ignore. - } - } - 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; } /** @@ -229,43 +175,18 @@ private ImageData[] loadDefault(InputStream stream) { */ public ImageData[] load(String filename) { if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - 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 - * @exception SWTException - * - * @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); + InputStream stream = null; + try { + stream = new FileInputStream(filename); + return load(stream); } 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 a0d03843db1..36650fb1b2b 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,27 @@ scanlinePad, checkData(data), 0, null, * @see ImageLoader#load(InputStream) */ public ImageData(InputStream stream) { - this(stream, 0); + ImageData[] data = ImageDataLoader.load(stream); + if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE); + ImageData i = data[0]; + setAllFields( + i.width, + i.height, + i.depth, + i.scanlinePad, + i.bytesPerLine, + i.data, + i.palette, + i.transparentPixel, + i.maskData, + i.maskPad, + i.alphaData, + i.alpha, + i.type, + i.x, + i.y, + i.disposalMethod, + i.delayTime); } /** @@ -409,7 +429,27 @@ public ImageData(InputStream stream, int zoom) { * */ public ImageData(String filename) { - this(filename, 0); + ImageData[] data = ImageDataLoader.load(filename); + if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE); + ImageData i = data[0]; + setAllFields( + i.width, + i.height, + i.depth, + i.scanlinePad, + i.bytesPerLine, + i.data, + i.palette, + i.transparentPixel, + i.maskData, + i.maskPad, + i.alphaData, + i.alpha, + i.type, + i.x, + i.y, + i.disposalMethod, + i.delayTime); } /** diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/SVGRasterizer.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/SVGRasterizer.java index 9c56d12ee64..b728d43b499 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/SVGRasterizer.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/SVGRasterizer.java @@ -22,29 +22,15 @@ * @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. + * @param stream the SVG image as an {@link InputStream}. + * @param zoom 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 stream 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 stream) throws IOException; - //TODO: use optional above instead? + public ImageData[] rasterizeSVG(InputStream stream, int zoom) throws IOException; } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/SVGRasterizerRegistry.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/SVGRasterizerRegistry.java index 97829c8d545..416d8796391 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/SVGRasterizerRegistry.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/SVGRasterizerRegistry.java @@ -14,24 +14,25 @@ import java.util.*; /** - * A registry for managing the instance of an {@link SVGRasterizer} implementation. - * This allows for the registration and retrieval of a single rasterizer instance. + * 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}. - */ + * The instance of the registered {@link SVGRasterizer}. + */ private static final SVGRasterizer RASTERIZER = ServiceLoader.load(SVGRasterizer.class).findFirst().orElse(null); /** - * Retrieves the currently registered {@link SVGRasterizer} implementation. - * - * @return the registered {@link SVGRasterizer}, or {@code null} if no implementation - * has been registered. - */ + * 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; } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java index 943cddc9fd4..8410da15d98 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java @@ -18,6 +18,7 @@ import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; +import org.eclipse.swt.internal.*; /** * Abstract factory class for loading/unloading images from files or streams @@ -89,6 +90,18 @@ public static ImageData[] load(InputStream is, ImageLoader loader) { return fileFormat.loadFromStream(stream); } +public static ImageData[] load(InputStream stream, int zoom, ImageLoader loader) { + SVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer(); + if (rasterizer != null && zoom != 0) { + try { + return rasterizer.rasterizeSVG(stream, zoom); + } catch (IOException e) { + SWT.error(SWT.ERROR_INVALID_IMAGE, e); + } + } + return null; +} + /** * Write the device independent image array stored in the specified loader * to the specified output stream using the specified file format. 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 bc531b30184..4fbda1029d7 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,13 +15,10 @@ 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.*; @@ -162,61 +159,11 @@ 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 - * @exception SWTException - * - * @since 3.129 - */ -public ImageData[] load(InputStream stream, int zoom) { - if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - reset(); - if (!stream.markSupported()) { - stream = new BufferedInputStream(stream); - } - SVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer(); - if (rasterizer != null && zoom != 0) { - try { - if (rasterizer.isSVGFile(stream)) { - float scalingFactor = zoom / 100.0f; - ImageData rasterizedData = rasterizer.rasterizeSVG(stream, scalingFactor); - if (rasterizedData != null) { - data = new ImageData[]{rasterizedData}; - return data; - } - } - } catch (IOException e) { - //ignore. - } - } - return loadDefault(stream); -} - -private ImageData[] loadDefault(InputStream stream) { if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); reset(); - data = getImageDataArrayFromStream(stream); - return data; + ImageData [] imgDataArray = getImageDataArrayFromStream(stream); + data = imgDataArray; + return imgDataArray; } /** @@ -344,43 +291,18 @@ boolean isInterlacedPNG(byte [] imageAsByteArray) { */ public ImageData[] load(String filename) { if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - 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 - * @exception SWTException - * - * @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); + InputStream stream = null; + try { + stream = new FileInputStream(filename); + return load(stream); } 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/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java index fa1884630d3..ffa626190ef 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 @@ -18,7 +18,6 @@ import java.util.*; import org.eclipse.swt.*; -import org.eclipse.swt.internal.*; import org.eclipse.swt.internal.image.*; /** @@ -150,7 +149,10 @@ void reset() { * */ public ImageData[] load(InputStream stream) { - return loadDefault(stream); + if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + reset(); + data = FileFormat.load(stream, this); + return data; } /** @@ -179,31 +181,10 @@ public ImageData[] load(InputStream stream) { public ImageData[] load(InputStream stream, int zoom) { if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); reset(); - if (!stream.markSupported()) { - stream = new BufferedInputStream(stream); - } - SVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer(); - if (rasterizer != null && zoom != 0) { - try { - if (rasterizer.isSVGFile(stream)) { - float scalingFactor = zoom / 100.0f; - ImageData rasterizedData = rasterizer.rasterizeSVG(stream, scalingFactor); - if (rasterizedData != null) { - data = new ImageData[]{rasterizedData}; - return data; - } - } - } catch (IOException e) { - SWT.error(SWT.ERROR_INVALID_IMAGE); - } - } - return loadDefault(stream); -} - -private ImageData[] loadDefault(InputStream stream) { - if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - reset(); - data = FileFormat.load(stream, this); + data = FileFormat.load(stream, zoom, this); + if (data == null) { + load(stream); + } return data; } @@ -228,7 +209,7 @@ private ImageData[] loadDefault(InputStream stream) { public ImageData[] load(String filename) { if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); try (InputStream stream = new FileInputStream(filename)) { - return loadDefault(stream); + return load(stream); } catch (IOException e) { SWT.error(SWT.ERROR_IO, e); }