Skip to content
Merged
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 @@ -691,9 +691,8 @@ public Image(Device device, InputStream stream) {
NSAutoreleasePool pool = null;
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
try {
byte[] input = stream.readAllBytes();
initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom),
zoom -> ImageDataLoader.loadByZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element());
ImageDataProvider imageDataProvider = createImageDataProvider(stream);
initUsingImageDataProvider(imageDataProvider);
init();
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
Expand Down Expand Up @@ -847,24 +846,14 @@ private void initUsingFileNameProvider(ImageFileNameProvider imageFileNameProvid
public Image(Device device, ImageDataProvider imageDataProvider) {
super(device);
if (imageDataProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
this.imageDataProvider = imageDataProvider;
ImageData data = imageDataProvider.getImageData (100);
if (data == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
NSAutoreleasePool pool = null;
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
try {
init (data, 100);
initUsingImageDataProvider(imageDataProvider);
init ();
StrictChecks.runIfStrictChecksEnabled(() -> {
DPIUtil.validateLinearScaling(imageDataProvider);
});
ImageData data2x = imageDataProvider.getImageData (200);
if (data2x != null) {
alphaInfo_200 = new AlphaInfo();
NSBitmapImageRep rep = createRepresentation (data2x, alphaInfo_200);
handle.addRepresentation(rep);
rep.release();
}
} finally {
if (pool != null) pool.release();
}
Expand Down Expand Up @@ -1511,18 +1500,35 @@ void init(ImageData image, int imageZoom) {
handle.setCacheMode(OS.NSImageCacheNever);
}

private void initWithSupplier(Function<Integer, Boolean> canLoadAtZoom, Function<Integer, ImageData> zoomToImageData) {
ImageData imageData = zoomToImageData.apply(100);
private void initUsingImageDataProvider(ImageDataProvider imageDataProvider) {
this.imageDataProvider = imageDataProvider;
ImageData imageData = imageDataProvider.getImageData(100);
if (imageData == null) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
init(imageData, 100);
if (canLoadAtZoom.apply(200)) {
ImageData imageData2x = zoomToImageData.apply(200);
ImageData imageData2x = imageDataProvider.getImageData(200);
if (imageData2x != null) {
alphaInfo_200 = new AlphaInfo();
NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
handle.addRepresentation(rep);
rep.release();
}
}

private static ImageDataProvider createImageDataProvider(InputStream stream) throws IOException {
byte[] streamData = stream.readAllBytes();
if (ImageDataLoader.isDynamicallySizable(new ByteArrayInputStream(streamData))) {
ImageDataAtSizeProvider imageDataAtSizeProvider = (width, height) -> ImageDataLoader
.loadBySize(new ByteArrayInputStream(streamData), width, height);
return imageDataAtSizeProvider;
}

ImageData imageData = ImageDataLoader
.loadByZoom(new ByteArrayInputStream(streamData), FileFormat.DEFAULT_ZOOM, 100)
.element();
return zoom -> zoom == 100 ? imageData : null;
}

void initAlpha_200(NSBitmapImageRep nativeRep) {
NSAutoreleasePool pool = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,17 @@ public Image(Device device, ImageData source, ImageData mask) {
*/
public Image(Device device, InputStream stream) {
super(device);
currentDeviceZoom = DPIUtil.getDeviceZoom();
ElementAtZoom<ImageData> image = ImageDataLoader.loadByZoom(stream, FileFormat.DEFAULT_ZOOM, currentDeviceZoom);
ImageData data = DPIUtil.scaleImageData(device, image, currentDeviceZoom);
init(data);
init();
if (stream == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
try {
currentDeviceZoom = DPIUtil.getDeviceZoom();
this.imageDataProvider = createImageDataProvider(stream);
initFromImageDataProvider(currentDeviceZoom);
init();
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
}
}

/**
Expand Down Expand Up @@ -802,6 +808,20 @@ private void initFromImageDataProvider(int zoom) {
init(resizedData);
}

private static ImageDataProvider createImageDataProvider(InputStream stream) throws IOException {
byte[] streamData = stream.readAllBytes();
if (ImageDataLoader.isDynamicallySizable(new ByteArrayInputStream(streamData))) {
ImageDataAtSizeProvider imageDataAtSizeProvider = (width, height) -> ImageDataLoader
Copy link
Contributor

@ptziegler ptziegler Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work when loading the ImageData of an Image by zoom. Because the getDefaultSize() method is not overridden, a 1x1 ImageData is returned at 100% zoom, otherwise null.

default ImageData getImageData(int zoom) {
Point defaultSize = getDefaultSize();
if (new Point(-1, -1).equals(defaultSize)) {
if (zoom == 100) {
return new ImageData(1, 1, 32, new PaletteData(0xFF0000, 0xFF00, 0xFF));
}
return null;
}
return getImageData(DPIUtil.pointToPixel(defaultSize.x, zoom), DPIUtil.pointToPixel(defaultSize.y, zoom));
}

Copy link
Contributor

@HeikoKlare HeikoKlare Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, I even though about that case when reviewing the PR and somehow didn't follow-up on it. Too bad that there was not even a test for it. Thank you for fixing that with #3012!

.loadBySize(new ByteArrayInputStream(streamData), width, height);
return imageDataAtSizeProvider;
}

ImageData imageData = ImageDataLoader
.loadByZoom(new ByteArrayInputStream(streamData), FileFormat.DEFAULT_ZOOM, 100)
.element();
return zoom -> zoom == 100 ? imageData : null;
}

void createFromPixbuf(int type, long pixbuf) {
this.type = type;

Expand Down
Loading