Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: org.eclipse.swt.cocoa.macosx.aarch64; singleton:=true
Bundle-Version: 3.131.100.qualifier
Bundle-Version: 3.132.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: org.eclipse.swt.cocoa.macosx.x86_64; singleton:=true
Bundle-Version: 3.131.100.qualifier
Bundle-Version: 3.132.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: org.eclipse.swt.gtk.linux.aarch64; singleton:=true
Bundle-Version: 3.131.100.qualifier
Bundle-Version: 3.132.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: org.eclipse.swt.gtk.linux.loongarch64; singleton:=true
Bundle-Version: 3.131.0.qualifier
Bundle-Version: 3.132.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: org.eclipse.swt.gtk.linux.ppc64le;singleton:=true
Bundle-Version: 3.131.100.qualifier
Bundle-Version: 3.132.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: org.eclipse.swt.gtk.linux.riscv64; singleton:=true
Bundle-Version: 3.131.100.qualifier
Bundle-Version: 3.132.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: org.eclipse.swt.gtk.linux.x86_64; singleton:=true
Bundle-Version: 3.131.100.qualifier
Bundle-Version: 3.132.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: org.eclipse.swt.win32.win32.aarch64; singleton:=true
Bundle-Version: 3.131.100.qualifier
Bundle-Version: 3.132.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: org.eclipse.swt.win32.win32.x86_64; singleton:=true
Bundle-Version: 3.131.100.qualifier
Bundle-Version: 3.132.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,44 @@ public class JSVGRasterizer implements SVGRasterizer {

@Override
public ImageData rasterizeSVG(InputStream inputStream, int zoom) throws IOException {
SVGDocument svgDocument = loadSVG(inputStream);
if (svgDocument == null) {
SWT.error(SWT.ERROR_INVALID_IMAGE);
}
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
BufferedImage rasterizedImage = renderSVG(svgDocument, zoom);
return convertToSWTImageData(rasterizedImage);
}

private SVGDocument loadSVG(InputStream inputStream) {
return SVG_LOADER.load(inputStream, null, LoaderContext.createDefault());

@Override
public ImageData rasterizeSVG(InputStream inputStream, int width, int height) throws IOException {
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
BufferedImage rasterizedImage = renderSVG(svgDocument, width, height);
return convertToSWTImageData(rasterizedImage);
}

private SVGDocument loadAndValidateSVG(InputStream inputStream) throws IOException {
SVGDocument svgDocument = SVG_LOADER.load(inputStream, null, LoaderContext.createDefault());
if (svgDocument == null) {
SWT.error(SWT.ERROR_INVALID_IMAGE);
}
return svgDocument;
}

private BufferedImage renderSVG(SVGDocument svgDocument, int zoom) {
FloatSize sourceImageSize = svgDocument.size();
float scalingFactor = zoom / 100.0f;
BufferedImage image = createImageBase(svgDocument, scalingFactor);
Graphics2D g = configureRenderingOptions(scalingFactor, image);
int targetImageWidth = calculateTargetWidth(scalingFactor, sourceImageSize);
int targetImageHeight = calculateTargetHeight(scalingFactor, sourceImageSize);
return renderSVG(svgDocument, targetImageWidth, targetImageHeight);
}

private BufferedImage renderSVG(SVGDocument svgDocument, int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
float widthScalingFactor = width / svgDocument.size().width;
float heightScalingFactor = height / svgDocument.size().height;
Graphics2D g = configureRenderingOptions(widthScalingFactor, heightScalingFactor, image);
svgDocument.render(null, g);
g.dispose();
return image;
}

private BufferedImage createImageBase(SVGDocument svgDocument, float scalingFactor) {
FloatSize sourceImageSize = svgDocument.size();
int targetImageWidth = calculateTargetWidth(scalingFactor, sourceImageSize);
int targetImageHeight = calculateTargetHeight(scalingFactor, sourceImageSize);
return new BufferedImage(targetImageWidth, targetImageHeight, BufferedImage.TYPE_INT_ARGB);
}

private int calculateTargetWidth(float scalingFactor, FloatSize sourceImageSize) {
double sourceImageWidth = sourceImageSize.getWidth();
return (int) Math.round(sourceImageWidth * scalingFactor);
Expand All @@ -110,10 +120,10 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize
return (int) Math.round(sourceImageHeight * scalingFactor);
}

private Graphics2D configureRenderingOptions(float scalingFactor, BufferedImage image) {
private Graphics2D configureRenderingOptions(float widthScalingFactor, float heightScalingFactor, BufferedImage image) {
Graphics2D g = image.createGraphics();
g.setRenderingHints(RENDERING_HINTS);
g.scale(scalingFactor, scalingFactor);
g.scale(widthScalingFactor, heightScalingFactor);
return g;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ public Image(Device device, InputStream stream) {
try {
byte[] input = stream.readAllBytes();
initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom),
zoom -> ImageDataLoader.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element());
zoom -> ImageDataLoader.loadByZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element());
init();
} catch (IOException e) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT, e);
Expand Down Expand Up @@ -742,7 +742,7 @@ public Image(Device device, String filename) {
initNative(filename);
if (this.handle == null) {
initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(filename, FileFormat.DEFAULT_ZOOM, zoom),
zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom).element());
zoom -> ImageDataLoader.loadByZoom(filename, FileFormat.DEFAULT_ZOOM, zoom).element());
}
init();
} finally {
Expand Down Expand Up @@ -789,7 +789,7 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
try {
initNative(filename);
if (this.handle == null) init(ImageDataLoader.load(filename, 100, 100).element(), 100);
if (this.handle == null) init(ImageDataLoader.loadByZoom(filename, 100, 100).element(), 100);
init();
String filename2x = imageFileNameProvider.getImagePath(200);
if (filename2x != null) {
Expand All @@ -799,7 +799,7 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
handle.addRepresentation(rep);
} else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) {
// Try to natively scale up the image (e.g. possible if it's an SVG)
ImageData imageData2x = ImageDataLoader.load(filename, 100, 200).element();
ImageData imageData2x = ImageDataLoader.loadByZoom(filename, 100, 200).element();
alphaInfo_200 = new AlphaInfo();
NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
handle.addRepresentation(rep);
Expand Down Expand Up @@ -1820,11 +1820,12 @@ public String toString () {
* @param imageData the imageData which is used to draw the scaled Image
* @param width the width of the original image
* @param height the height of the original image
* @param scaleFactor the factor with which the image is supposed to be scaled
* @param targetWidth the width to which the image is supposed to be scaled
* @param targetHeight the height to which the image is supposed to be scaled
*
* @noreference This method is not intended to be referenced by clients.
*/
public static void drawScaled(GC gc, ImageData imageData, int width, int height, float scaleFactor) {
public static void drawAtTargetSize(GC gc, ImageData imageData, int width, int height, int targetWidth, int targetHeight) {
StrictChecks.runWithStrictChecksDisabled(() -> {
Image imageToDraw = new Image(gc.device, (ImageDataProvider) zoom -> imageData);
gc.drawImage(imageToDraw, 0, 0, CocoaDPIUtil.pixelToPoint(width), CocoaDPIUtil.pixelToPoint(height),
Expand All @@ -1833,8 +1834,7 @@ public static void drawScaled(GC gc, ImageData imageData, int width, int height,
* avoiding rounding errors. Nevertheless, we still have some rounding errors
* due to the point-based API GC#drawImage(..).
*/
0, 0, Math.round(CocoaDPIUtil.pixelToPoint(width * scaleFactor)),
Math.round(CocoaDPIUtil.pixelToPoint(height * scaleFactor)));
0, 0, targetWidth, targetHeight);
imageToDraw.dispose();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public static List<ElementAtZoom<ImageData>> load(ElementAtZoom<InputStream> str
return FileFormat.load(streamAtZoom, imageLoader, targetZoom);
}

public static ImageData load(InputStream streamAtZoom, ImageLoader imageLoader, int targetWidth, int targetHeight) {
return FileFormat.load(streamAtZoom, imageLoader, targetWidth, targetHeight);
}

public static void save(OutputStream stream, int format, ImageLoader imageLoader) {
FileFormat.save(stream, format, imageLoader);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*******************************************************************************
* Copyright (c) 2025 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:
* Michael Bangas (Vector Informatik GmbH) - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.graphics;

/**
* @since 3.132
*/
public interface ImageDataAtSizeProvider extends ImageDataProvider {

ImageData getImageData(int targetWidth, int targetHeight);

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,32 @@ public static boolean canLoadAtZoom(InputStream stream, int fileZoom, int target
return ImageLoader.canLoadAtZoom(stream, fileZoom, targetZoom);
}

public static ElementAtZoom<ImageData> load(InputStream stream, int fileZoom, int targetZoom) {
List<ElementAtZoom<ImageData>> data = new ImageLoader().load(stream, fileZoom, targetZoom);
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
return data.get(0);
}

public static boolean canLoadAtZoom(String filename, int fileZoom, int targetZoom) {
return ImageLoader.canLoadAtZoom(filename, fileZoom, targetZoom);
}

public static ElementAtZoom<ImageData> load(String filename, int fileZoom, int targetZoom) {
List<ElementAtZoom<ImageData>> data = new ImageLoader().load(filename, fileZoom, targetZoom);
public static ElementAtZoom<ImageData> loadByZoom(InputStream stream, int fileZoom, int targetZoom) {
List<ElementAtZoom<ImageData>> data = new ImageLoader().loadByZoom(stream, fileZoom, targetZoom);
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
return data.get(0);
}

public static ElementAtZoom<ImageData> loadByZoom(String filename, int fileZoom, int targetZoom) {
List<ElementAtZoom<ImageData>> data = new ImageLoader().loadByZoom(filename, fileZoom, targetZoom);
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
return data.get(0);
}

public static ImageData loadByTargetSize(InputStream stream, int targetWidth, int targetHeight) {
ImageData data = new ImageLoader().loadByTargetSize(stream, targetWidth, targetHeight);
if (data == null) SWT.error(SWT.ERROR_INVALID_IMAGE);
return data;
}

public static ImageData loadByTargetSize(String filename, int targetWidth, int targetHeight) {
ImageData data = new ImageLoader().loadByTargetSize(filename, targetWidth, targetHeight);
if (data == null) SWT.error(SWT.ERROR_INVALID_IMAGE);
return data;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,26 @@ void reset() {
* </ul>
*/
public ImageData[] load(InputStream stream) {
load(stream, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM);
loadByZoom(stream, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM);
return data;
}

List<ElementAtZoom<ImageData>> load(InputStream stream, int fileZoom, int targetZoom) {
List<ElementAtZoom<ImageData>> loadByZoom(InputStream stream, int fileZoom, int targetZoom) {
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
reset();
List<ElementAtZoom<ImageData>> images = NativeImageLoader.load(new ElementAtZoom<>(stream, fileZoom), this, targetZoom);
data = images.stream().map(ElementAtZoom::element).toArray(ImageData[]::new);
return images;
}

ImageData loadByTargetSize(InputStream stream, int targetWidth, int targetHeight) {
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
reset();
ImageData image = NativeImageLoader.load(stream, this, targetWidth, targetHeight);
data = new ImageData[] {image};
return image;
}

static boolean canLoadAtZoom(InputStream stream, int fileZoom, int targetZoom) {
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
return FileFormat.canLoadAtZoom(new ElementAtZoom<>(stream, fileZoom), targetZoom);
Expand All @@ -187,14 +195,24 @@ static boolean canLoadAtZoom(InputStream stream, int fileZoom, int targetZoom) {
* </ul>
*/
public ImageData[] load(String filename) {
load(filename, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM);
loadByZoom(filename, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM);
return data;
}

List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoom) {
List<ElementAtZoom<ImageData>> loadByZoom(String filename, int fileZoom, int targetZoom) {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
try (InputStream stream = new FileInputStream(filename)) {
return load(stream, fileZoom, targetZoom);
return loadByZoom(stream, fileZoom, targetZoom);
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
}
return null;
}

ImageData loadByTargetSize(String filename, int targetWidth, int targetHeight) {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
try (InputStream stream = new FileInputStream(filename)) {
return loadByTargetSize(stream, targetWidth, targetHeight);
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
}
Expand All @@ -211,6 +229,15 @@ static boolean canLoadAtZoom(String filename, int fileZoom, int targetZoom) {
return false;
}

static boolean isDynamicallySizable(String filename) {
try (InputStream stream = new FileInputStream(filename)) {
return FileFormat.isDynamicallySizableFormat(stream);
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
}
return false;
}

/**
* Saves the image data in this ImageLoader to the specified stream.
* The format parameter can have one of the following values:
Expand Down
Loading
Loading