Skip to content

Commit 8201956

Browse files
committed
Utilize FileFormat changes
1 parent 7b63335 commit 8201956

File tree

7 files changed

+69
-265
lines changed

7 files changed

+69
-265
lines changed

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

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -352,62 +352,6 @@ public ImageData(InputStream stream) {
352352
i.delayTime);
353353
}
354354

355-
/**
356-
* Constructs an <code>ImageData</code> loaded from the specified
357-
* input stream. Throws an error if an error occurs while loading
358-
* the image, or if the image has an unsupported type. Application
359-
* code is still responsible for closing the input stream.
360-
*
361-
* @param stream the input stream to load the image from (must not be null)
362-
* @param zoom the zoom factor to apply when rasterizing an SVG.
363-
*
364-
* A value of 0 means that the standard method for loading should be used.
365-
* This case is equivalent to calling {@link ImageLoader#load(InputStream)}.
366-
*
367-
* A value above 0 specifies a scaling factor for the output image. For example:
368-
* <ul>
369-
* <li>A value of 100 maintains the original size of the SVG when rasterized.</li>
370-
* <li>A value of 200 doubles the size of the rasterized image.</li>
371-
* <li>A value of 50 reduces the size of the rasterized image to half.</li>
372-
* </ul>
373-
* The scaling is applied uniformly to both width and height.
374-
*
375-
* @exception IllegalArgumentException <ul>
376-
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
377-
* </ul>
378-
* @exception SWTException <ul>
379-
* <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
380-
* <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
381-
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
382-
* </ul>
383-
*
384-
* @see ImageLoader#load(InputStream)
385-
* @since 3.130
386-
*/
387-
public ImageData(InputStream stream, int zoom) {
388-
ImageData[] data = ImageDataLoader.load(stream, zoom);
389-
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
390-
ImageData i = data[0];
391-
setAllFields(
392-
i.width,
393-
i.height,
394-
i.depth,
395-
i.scanlinePad,
396-
i.bytesPerLine,
397-
i.data,
398-
i.palette,
399-
i.transparentPixel,
400-
i.maskData,
401-
i.maskPad,
402-
i.alphaData,
403-
i.alpha,
404-
i.type,
405-
i.x,
406-
i.y,
407-
i.disposalMethod,
408-
i.delayTime);
409-
}
410-
411355
/**
412356
* Constructs an <code>ImageData</code> loaded from a file with the
413357
* specified name. Throws an error if an error occurs loading the
@@ -452,59 +396,6 @@ public ImageData(String filename) {
452396
i.delayTime);
453397
}
454398

455-
/**
456-
* Constructs an <code>ImageData</code> loaded from a file with the
457-
* specified name. Throws an error if an error occurs loading the
458-
* image, or if the image has an unsupported type.
459-
*
460-
* @param filename the name of the file to load the image from (must not be null)
461-
* @param zoom the zoom factor to apply when rasterizing a SVG.
462-
* A value of 0 means that the standard method for loading should be used.
463-
* This case is equivalent to calling {@link ImageLoader#load(String)}.
464-
*
465-
* A value above 0 specifies a scaling factor for the output image. For example:
466-
* <ul>
467-
* <li>A value of 100 maintains the original size of the SVG when rasterized.</li>
468-
* <li>A value of 200 doubles the size of the rasterized image.</li>
469-
* <li>A value of 50 reduces the size of the rasterized image to half.</li>
470-
* </ul>
471-
* The scaling is applied uniformly to both width and height.
472-
*
473-
* @exception IllegalArgumentException <ul>
474-
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
475-
* </ul>
476-
* @exception SWTException <ul>
477-
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
478-
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
479-
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
480-
* </ul>
481-
*
482-
* @since 3.130
483-
*/
484-
public ImageData(String filename, int zoom) {
485-
ImageData[] data = ImageDataLoader.load(filename, zoom);
486-
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
487-
ImageData i = data[0];
488-
setAllFields(
489-
i.width,
490-
i.height,
491-
i.depth,
492-
i.scanlinePad,
493-
i.bytesPerLine,
494-
i.data,
495-
i.palette,
496-
i.transparentPixel,
497-
i.maskData,
498-
i.maskPad,
499-
i.alphaData,
500-
i.alpha,
501-
i.type,
502-
i.x,
503-
i.y,
504-
i.disposalMethod,
505-
i.delayTime);
506-
}
507-
508399
/**
509400
* Prevents uninitialized instances from being created outside the package.
510401
*/

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,4 @@ public static ElementAtZoom<ImageData> load(String filename, int fileZoom, int t
4949
return data.get(0);
5050
}
5151

52-
public static ImageData[] load(String filename, int zoom) {
53-
return new ImageLoader().load(filename, zoom);
54-
}
55-
5652
}

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+
}

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ public ImageData getImageData (int zoom) {
11491149
return DPIUtil.scaleImageData (device, data.element(), zoom, data.zoom());
11501150
} else if (imageFileNameProvider != null) {
11511151
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
1152-
return DPIUtil.scaleImageData (device, new ImageData (fileName.element(), currentDeviceZoom), zoom, fileName.zoom());
1152+
return DPIUtil.scaleImageData (device, new ImageData (fileName.element()), zoom, fileName.zoom());
11531153
} else if (imageGcDrawer != null) {
11541154
return drawWithImageGcDrawer(width, height, zoom);
11551155
} else {

0 commit comments

Comments
 (0)