Skip to content

Commit 8f3fd02

Browse files
Adopt usage of zoom-aware ImageLoader methods in Image class (#1889)
Co-authored-by: Hannes Wellmann <[email protected]>
1 parent d29a06f commit 8f3fd02

File tree

5 files changed

+82
-31
lines changed

5 files changed

+82
-31
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2020 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -16,11 +16,14 @@
1616

1717
import java.io.*;
1818
import java.util.*;
19+
import java.util.function.*;
1920

2021
import org.eclipse.swt.*;
2122
import org.eclipse.swt.internal.*;
23+
import org.eclipse.swt.internal.DPIUtil.*;
2224
import org.eclipse.swt.internal.cocoa.*;
2325
import org.eclipse.swt.internal.graphics.*;
26+
import org.eclipse.swt.internal.image.*;
2427

2528
/**
2629
* Instances of this class are graphics which have been prepared
@@ -692,7 +695,7 @@ public Image(Device device, InputStream stream) {
692695
NSAutoreleasePool pool = null;
693696
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
694697
try {
695-
init(new ImageData(stream));
698+
initWithSupplier(zoom -> ImageDataLoader.load(stream, FileFormat.DEFAULT_ZOOM, zoom));
696699
init();
697700
} finally {
698701
if (pool != null) pool.release();
@@ -738,7 +741,7 @@ public Image(Device device, String filename) {
738741
try {
739742
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
740743
initNative(filename);
741-
if (this.handle == null) init(new ImageData(filename));
744+
if (this.handle == null) initWithSupplier(zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom));
742745
init();
743746
} finally {
744747
if (pool != null) pool.release();
@@ -784,14 +787,23 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
784787
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
785788
try {
786789
initNative(filename);
787-
if (this.handle == null) init(new ImageData(filename));
790+
if (this.handle == null) init(ImageDataLoader.load(filename, 100, 100).element());
788791
init();
789792
String filename2x = imageFileNameProvider.getImagePath(200);
790793
if (filename2x != null) {
791794
alphaInfo_200 = new AlphaInfo();
792795
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x));
793796
NSImageRep rep = new NSImageRep(id);
794797
handle.addRepresentation(rep);
798+
} else {
799+
// Try to natively scale up the image (e.g. possible if it's an SVG)
800+
ElementAtZoom<ImageData> imageData2x = ImageDataLoader.load(filename, 100, 200);
801+
if (imageData2x.zoom() == 200) {
802+
alphaInfo_200 = new AlphaInfo();
803+
NSBitmapImageRep rep = createRepresentation (imageData2x.element(), alphaInfo_200);
804+
handle.addRepresentation(rep);
805+
rep.release();
806+
}
795807
}
796808
} finally {
797809
if (pool != null) pool.release();
@@ -1472,6 +1484,25 @@ void init(ImageData image) {
14721484
handle.setCacheMode(OS.NSImageCacheNever);
14731485
}
14741486

1487+
private void initWithSupplier(Function<Integer, ElementAtZoom<ImageData>> zoomToImageData) {
1488+
ElementAtZoom<ImageData> imageData = zoomToImageData.apply(DPIUtil.getDeviceZoom());
1489+
ImageData imageData2x = null;
1490+
if (imageData.zoom() == 200) {
1491+
imageData2x = imageData.element();
1492+
}
1493+
if (imageData.zoom() != 100) {
1494+
imageData = zoomToImageData.apply(100);
1495+
}
1496+
init(imageData.element());
1497+
if (imageData2x != null) {
1498+
alphaInfo_200 = new AlphaInfo();
1499+
NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
1500+
handle.addRepresentation(rep);
1501+
rep.release();
1502+
}
1503+
}
1504+
1505+
14751506
void initAlpha_200(NSBitmapImageRep nativeRep) {
14761507
NSAutoreleasePool pool = null;
14771508
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,7 @@ scanlinePad, checkData(data), 0, null,
331331
* @see ImageLoader#load(InputStream)
332332
*/
333333
public ImageData(InputStream stream) {
334-
ImageData[] data = ImageDataLoader.load(stream);
335-
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
336-
ImageData i = data[0];
334+
ImageData i = ImageDataLoader.load(stream);
337335
setAllFields(
338336
i.width,
339337
i.height,
@@ -377,9 +375,7 @@ public ImageData(InputStream stream) {
377375
* </ul>
378376
*/
379377
public ImageData(String filename) {
380-
ImageData[] data = ImageDataLoader.load(filename);
381-
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
382-
ImageData i = data[0];
378+
ImageData i = ImageDataLoader.load(filename);
383379
setAllFields(
384380
i.width,
385381
i.height,

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2006 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,19 +14,39 @@
1414
package org.eclipse.swt.graphics;
1515

1616
import java.io.*;
17+
import java.util.*;
18+
19+
import org.eclipse.swt.*;
20+
import org.eclipse.swt.internal.DPIUtil.*;
1721

1822
/**
1923
* Internal class that separates ImageData from ImageLoader
2024
* to allow removal of ImageLoader from the toolkit.
2125
*/
2226
class ImageDataLoader {
2327

24-
public static ImageData[] load(InputStream stream) {
25-
return new ImageLoader().load(stream);
28+
public static ImageData load(InputStream stream) {
29+
ImageData[] data = new ImageLoader().load(stream);
30+
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
31+
return data[0];
32+
}
33+
34+
public static ImageData load(String filename) {
35+
ImageData[] data = new ImageLoader().load(filename);
36+
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
37+
return data[0];
38+
}
39+
40+
public static ElementAtZoom<ImageData> load(InputStream stream, int fileZoom, int targetZoom) {
41+
List<ElementAtZoom<ImageData>> data = new ImageLoader().load(stream, fileZoom, targetZoom);
42+
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
43+
return data.get(0);
2644
}
2745

28-
public static ImageData[] load(String filename) {
29-
return new ImageLoader().load(filename);
46+
public static ElementAtZoom<ImageData> load(String filename, int fileZoom, int targetZoom) {
47+
List<ElementAtZoom<ImageData>> data = new ImageLoader().load(filename, fileZoom, targetZoom);
48+
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
49+
return data.get(0);
3050
}
3151

3252
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2022 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -22,6 +22,7 @@
2222
import org.eclipse.swt.internal.DPIUtil.*;
2323
import org.eclipse.swt.internal.cairo.*;
2424
import org.eclipse.swt.internal.gtk.*;
25+
import org.eclipse.swt.internal.image.*;
2526

2627
/**
2728
* Instances of this class are graphics which have been prepared
@@ -530,9 +531,9 @@ public Image(Device device, ImageData source, ImageData mask) {
530531
*/
531532
public Image(Device device, InputStream stream) {
532533
super(device);
533-
ImageData data = new ImageData(stream);
534534
currentDeviceZoom = DPIUtil.getDeviceZoom();
535-
data = DPIUtil.autoScaleUp (device, data);
535+
ElementAtZoom<ImageData> image = ImageDataLoader.load(stream, FileFormat.DEFAULT_ZOOM, currentDeviceZoom);
536+
ImageData data = DPIUtil.scaleImageData(device, image, currentDeviceZoom);
536537
init(data);
537538
init();
538539
}
@@ -572,10 +573,9 @@ public Image(Device device, InputStream stream) {
572573
public Image(Device device, String filename) {
573574
super(device);
574575
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
575-
576-
ImageData data = new ImageData(filename);
577576
currentDeviceZoom = DPIUtil.getDeviceZoom();
578-
data = DPIUtil.autoScaleUp (device, data);
577+
ElementAtZoom<ImageData> image = ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, currentDeviceZoom);
578+
ImageData data = DPIUtil.scaleImageData(device, image, currentDeviceZoom);
579579
init(data);
580580
init();
581581
}
@@ -775,9 +775,10 @@ private void initFromFileNameProvider(int zoom) {
775775
initNative(fileForZoom.element());
776776
}
777777
if (this.surface == 0) {
778-
ImageData imageData = new ImageData(fileForZoom.element());
779-
if (fileForZoom.zoom() != zoom) {
780-
imageData = DPIUtil.scaleImageData(device, imageData, zoom, fileForZoom.zoom());
778+
ElementAtZoom<ImageData> imageDataAtZoom = ImageDataLoader.load(fileForZoom.element(), fileForZoom.zoom(), zoom);
779+
ImageData imageData = imageDataAtZoom.element();
780+
if (imageDataAtZoom.zoom() != zoom) {
781+
imageData = DPIUtil.scaleImageData(device, imageDataAtZoom, zoom);
781782
}
782783
init(imageData);
783784
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2022 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -21,6 +21,7 @@
2121
import org.eclipse.swt.internal.*;
2222
import org.eclipse.swt.internal.DPIUtil.*;
2323
import org.eclipse.swt.internal.gdip.*;
24+
import org.eclipse.swt.internal.image.*;
2425
import org.eclipse.swt.internal.win32.*;
2526

2627
/**
@@ -473,7 +474,7 @@ public Image (Device device, InputStream stream) {
473474
super(device);
474475
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
475476
int deviceZoom = getZoom();
476-
ImageData data = DPIUtil.scaleImageData(device, new ElementAtZoom<>(new ImageData (stream), 100), deviceZoom);
477+
ImageData data = DPIUtil.scaleImageData(device, ImageDataLoader.load(stream, FileFormat.DEFAULT_ZOOM, deviceZoom), deviceZoom);
477478
init(data, deviceZoom);
478479
init();
479480
this.device.registerResourceWithZoomSupport(this);
@@ -516,7 +517,7 @@ public Image (Device device, String filename) {
516517
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
517518
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
518519
int deviceZoom = getZoom();
519-
ImageData data = DPIUtil.scaleImageData(device, new ElementAtZoom<>(new ImageData (filename), 100), deviceZoom);
520+
ImageData data = DPIUtil.scaleImageData(device, ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, deviceZoom), deviceZoom);
520521
init(data, deviceZoom);
521522
init();
522523
this.device.registerResourceWithZoomSupport(this);
@@ -2130,7 +2131,8 @@ protected Rectangle getBounds(int zoom) {
21302131
@Override
21312132
ImageData getImageData(int zoom) {
21322133
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (provider, zoom);
2133-
return DPIUtil.scaleImageData (device, new ImageData (fileName.element()), zoom, fileName.zoom());
2134+
ElementAtZoom<ImageData> imageData = ImageDataLoader.load(fileName.element(), fileName.zoom(), zoom);
2135+
return DPIUtil.scaleImageData (device, imageData, zoom);
21342136
}
21352137

21362138
@Override
@@ -2141,9 +2143,10 @@ ImageHandle getImageMetadata(int zoom) {
21412143
nativeInitializedImage = initNative(fileForZoom.element(), zoom);
21422144
}
21432145
if (nativeInitializedImage == null) {
2144-
ImageData imageData = new ImageData (fileForZoom.element());
2145-
if (fileForZoom.zoom() != zoom) {
2146-
imageData = DPIUtil.scaleImageData(device, imageData, zoom, fileForZoom.zoom());
2146+
ElementAtZoom<ImageData> imageDataAtZoom = ImageDataLoader.load(fileForZoom.element(), fileForZoom.zoom(), zoom);
2147+
ImageData imageData = imageDataAtZoom.element();
2148+
if (imageDataAtZoom.zoom() != zoom) {
2149+
imageData = DPIUtil.scaleImageData(device, imageDataAtZoom, zoom);
21472150
}
21482151
imageData = adaptImageDataIfDisabledOrGray(imageData);
21492152
init(imageData, zoom);

0 commit comments

Comments
 (0)