Skip to content

Commit c688054

Browse files
committed
Utilize FileFormat changes of commit 18ac3b4
1 parent 7b63335 commit c688054

File tree

3 files changed

+66
-149
lines changed

3 files changed

+66
-149
lines changed

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageLoader.java

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -162,44 +162,6 @@ List<ElementAtZoom<ImageData>> load(InputStream stream, int fileZoom, int target
162162
return images;
163163
}
164164

165-
/**
166-
* Loads an array of <code>ImageData</code> objects from the
167-
* specified input stream. If the stream is a SVG File and zoom is not 0,
168-
* this method will try to rasterize the SVG.
169-
*
170-
* @param stream the input stream to load the images from
171-
* @param zoom the zoom factor to apply when rasterizing a SVG.
172-
* A value of 0 means that the standard method for loading should be used.
173-
* This case is equivalent to calling {@link ImageLoader#load(InputStream stream)}.
174-
*
175-
* A value above 0 specifies a scaling factor for the output image. For example:
176-
* <ul>
177-
* <li>A value of 100 maintains the original size of the SVG when rasterized.</li>
178-
* <li>A value of 200 doubles the size of the rasterized image.</li>
179-
* <li>A value of 50 reduces the size of the rasterized image to half.</li>
180-
* </ul>
181-
* The scaling is applied uniformly to both width and height.
182-
*
183-
* @return an array of <code>ImageData</code> objects loaded from the specified input stream
184-
*
185-
* @exception IllegalArgumentException <ul>
186-
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
187-
* </ul>
188-
* @exception SWTException <ul>
189-
* <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
190-
* <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
191-
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
192-
* </ul>
193-
*
194-
* @since 3.130
195-
*/
196-
public ImageData[] load(InputStream stream, int zoom) {
197-
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
198-
reset();
199-
data = FileFormat.load(stream, zoom, this);
200-
return data;
201-
}
202-
203165
/**
204166
* Loads an array of <code>ImageData</code> objects from the
205167
* file with the specified name. Throws an error if either
@@ -233,49 +195,6 @@ List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoo
233195
return null;
234196
}
235197

236-
/**
237-
* Loads an array of <code>ImageData</code> objects from the
238-
* file with the specified name. If the filename is a SVG File and zoom is not 0,
239-
* this method will try to rasterize the SVG. Throws an error if either
240-
* an error occurs while loading the images, or if the images are
241-
* not of a supported type. Returns the loaded image data array.
242-
*
243-
* @param filename the name of the file to load the images from
244-
* @param zoom the zoom factor to apply when rasterizing a SVG.
245-
* A value of 0 means that the standard method for loading should be used.
246-
* This case is equivalent to calling {@link ImageLoader#load(String)}.
247-
*
248-
* A value above 0 specifies a scaling factor for the output image. For example:
249-
* <ul>
250-
* <li>A value of 100 maintains the original size of the SVG when rasterized.</li>
251-
* <li>A value of 200 doubles the size of the rasterized image.</li>
252-
* <li>A value of 50 reduces the size of the rasterized image to half.</li>
253-
* </ul>
254-
* The scaling is applied uniformly to both width and height.
255-
*
256-
* @return an array of <code>ImageData</code> objects loaded from the specified file
257-
*
258-
* @exception IllegalArgumentException <ul>
259-
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
260-
* </ul>
261-
* @exception SWTException <ul>
262-
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
263-
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
264-
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
265-
* </ul>
266-
*
267-
* @since 3.130
268-
*/
269-
public ImageData[] load(String filename, int zoom) {
270-
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
271-
try (InputStream stream = new FileInputStream(filename)) {
272-
return load(stream, zoom);
273-
} catch (IOException e) {
274-
SWT.error(SWT.ERROR_IO, e);
275-
}
276-
return null;
277-
}
278-
279198
/**
280199
* Saves the image data in this ImageLoader to the specified stream.
281200
* The format parameter can have one of the following values:

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public abstract class FileFormat {
5353
try {
5454
FORMAT_FACTORIES.add(OS2BMPFileFormat::new);
5555
} catch (NoClassDefFoundError e) { } // ignore format
56+
try {
57+
FORMAT_FACTORIES.add(SVGFileFormat::new);
58+
} catch (NoClassDefFoundError e) { } // ignore format
5659
}
5760

5861
public static final int DEFAULT_ZOOM = 100;
@@ -84,6 +87,16 @@ List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom)
8487
}
8588
}
8689

90+
static abstract class DynamicImageFileFormat extends FileFormat {
91+
92+
abstract ImageData[] loadFromByteStream(int targetZoom);
93+
94+
@Override
95+
List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom) {
96+
return Arrays.stream(loadFromByteStream(targetZoom)).map(d -> new ElementAtZoom<>(d, fileZoom)).toList();
97+
}
98+
}
99+
87100
LEDataInputStream inputStream;
88101
LEDataOutputStream outputStream;
89102
ImageLoader loader;
@@ -133,74 +146,6 @@ public static List<ElementAtZoom<ImageData>> load(InputStream is, ImageLoader lo
133146
return fileFormat.loadFromStream(stream, fileZoom, targetZoom);
134147
}
135148

136-
/** The instance of the registered {@link SVGRasterizer}. */
137-
private static final SVGRasterizer RASTERIZER = ServiceLoader.load(SVGRasterizer.class).findFirst().orElse(null);
138-
139-
140-
/**
141-
* Loads an array of <code>ImageData</code> objects from the specified input stream.
142-
* Depending on the file type and zoom factor, this method either rasterizes the image
143-
* (for SVG files) or uses the provided {@link ImageLoader} to load the image data.
144-
*
145-
* <p>If the input stream is an SVG file and the specified zoom factor is not 0,
146-
* the method attempts to rasterize the SVG using the configured rasterizer.
147-
* Otherwise, it delegates the loading process to the {@link FileFormat#load(InputStream, ImageLoader)} method.
148-
*
149-
* @param stream the input stream to load the images from. The stream cannot be null.
150-
* If the stream does not support marking, it is wrapped in a
151-
* {@link BufferedInputStream}.
152-
* @param zoom the zoom factor to apply when rasterizing an SVG. A value of 0 uses
153-
* the standard image loading method. A positive value specifies a scaling
154-
* factor for the output image:
155-
* <ul>
156-
* <li><code>100</code>: Maintains the original size of the SVG when rasterized.</li>
157-
* <li><code>200</code>: Doubles the size of the rasterized image.</li>
158-
* <li><code>50</code>: Reduces the size of the rasterized image to half.</li>
159-
* </ul>
160-
* The scaling factor is applied uniformly to both width and height.
161-
* @param loader the {@link ImageLoader} instance used to load the image if the
162-
* stream is not an SVG or if the zoom factor is 0.
163-
*
164-
* @return an array of <code>ImageData</code> objects loaded from the specified
165-
* input stream, or <code>null</code> if an error occurs during loading.
166-
*
167-
* @exception IllegalArgumentException if the input stream is null.
168-
* @exception SWTException if an error occurs while loading the image data.
169-
*
170-
* @since 3.129
171-
*/
172-
public static ImageData[] load(InputStream stream, int zoom, ImageLoader loader) {
173-
if (stream == null) {
174-
throw new IllegalArgumentException("InputStream cannot be null");
175-
}
176-
if (!stream.markSupported()) {
177-
stream = new BufferedInputStream(stream);
178-
}
179-
try {
180-
if (RASTERIZER != null && zoom != 0 && isSVGFile(stream)) {
181-
return RASTERIZER.rasterizeSVG(stream, zoom);
182-
} else {
183-
return loader.load(stream);
184-
}
185-
} catch (IOException e) {
186-
SWT.error(SWT.ERROR_INVALID_IMAGE, e);
187-
}
188-
return null;
189-
}
190-
191-
private static boolean isSVGFile(InputStream stream) throws IOException {
192-
if (stream == null) {
193-
throw new IllegalArgumentException("InputStream cannot be null");
194-
}
195-
stream.mark(1);
196-
try {
197-
int firstByte = stream.read();
198-
return firstByte == '<';
199-
} finally {
200-
stream.reset();
201-
}
202-
}
203-
204149
/**
205150
* Write the device independent image array stored in the specified loader
206151
* to the specified output stream using the specified file format.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vector Informatik GmbH and others.
3+
*
4+
* This program and the accompanying materials are made available under the terms of the Eclipse
5+
* Public License 2.0 which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors: Michael Bangas (Vector Informatik GmbH) - initial API and implementation
11+
*******************************************************************************/
12+
package org.eclipse.swt.internal.image;
13+
14+
import java.io.*;
15+
import java.nio.charset.*;
16+
import java.util.*;
17+
18+
import org.eclipse.swt.*;
19+
import org.eclipse.swt.graphics.*;
20+
import org.eclipse.swt.internal.*;
21+
import org.eclipse.swt.internal.image.FileFormat.*;
22+
23+
public class SVGFileFormat extends DynamicImageFileFormat {
24+
25+
/** The instance of the registered {@link SVGRasterizer}. */
26+
private static final SVGRasterizer RASTERIZER = ServiceLoader.load(SVGRasterizer.class).findFirst().orElse(null);
27+
28+
@Override
29+
boolean isFileFormat(LEDataInputStream stream) throws IOException {
30+
byte[] firstBytes = new byte[5];
31+
int bytesRead = stream.read(firstBytes);
32+
stream.unread(firstBytes);
33+
String header = new String(firstBytes, 0, bytesRead, StandardCharsets.UTF_8).trim();
34+
return header.startsWith("<?xml") || header.startsWith("<svg");
35+
}
36+
37+
@Override
38+
ImageData[] loadFromByteStream(int targetZoom) {
39+
try {
40+
if (RASTERIZER != null && targetZoom != 0) {
41+
return RASTERIZER.rasterizeSVG(inputStream, targetZoom);
42+
}
43+
} catch (IOException e) {
44+
SWT.error(SWT.ERROR_INVALID_IMAGE, e);
45+
}
46+
return null;
47+
}
48+
49+
@Override
50+
void unloadIntoByteStream(ImageLoader loader) {
51+
throw new UnsupportedOperationException();
52+
}
53+
}

0 commit comments

Comments
 (0)