@@ -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.
0 commit comments