Skip to content

Commit 45f5b9c

Browse files
committed
[FIXUP] Improve consideration of file- and target/device-zoom
1 parent 62a9633 commit 45f5b9c

File tree

9 files changed

+82
-42
lines changed

9 files changed

+82
-42
lines changed

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

Lines changed: 3 additions & 2 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
@@ -778,6 +778,7 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
778778
super(device);
779779
if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
780780
this.imageFileNameProvider = imageFileNameProvider;
781+
//TODO: implement fine-grained zoom handling here as well?
781782
String filename = imageFileNameProvider.getImagePath(100);
782783
if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
783784
NSAutoreleasePool pool = null;
@@ -880,7 +881,7 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
880881
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
881882
try {
882883
init (data);
883-
init ();
884+
init ();
884885
} finally {
885886
if (pool != null) pool.release();
886887
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2016 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
@@ -18,6 +18,7 @@
1818
import java.util.*;
1919

2020
import org.eclipse.swt.*;
21+
import org.eclipse.swt.internal.DPIUtil.*;
2122
import org.eclipse.swt.internal.image.*;
2223

2324
/**
@@ -149,10 +150,16 @@ void reset() {
149150
* </ul>
150151
*/
151152
public ImageData[] load(InputStream stream) {
153+
load(stream, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM);
154+
return data;
155+
}
156+
157+
private List<ElementAtZoom<ImageData>> load(InputStream stream, int fileZoom, int targetZoom) {
152158
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
153159
reset();
154-
data = FileFormat.load(stream, this, FileFormat.DEFAULT_ZOOM);
155-
return data;
160+
List<ElementAtZoom<ImageData>> images = FileFormat.load(stream, this, fileZoom, targetZoom);
161+
data = images.stream().map(ElementAtZoom::element).toArray(ImageData[]::new);
162+
return images;
156163
}
157164

158165
/**
@@ -174,9 +181,14 @@ public ImageData[] load(InputStream stream) {
174181
* </ul>
175182
*/
176183
public ImageData[] load(String filename) {
184+
load(filename, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM);
185+
return data;
186+
}
187+
188+
List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoom) {
177189
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
178190
try (InputStream stream = new FileInputStream(filename)) {
179-
return load(stream);
191+
return load(stream, fileZoom, targetZoom);
180192
} catch (IOException e) {
181193
SWT.error(SWT.ERROR_IO, e);
182194
}

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

Lines changed: 8 additions & 1 deletion
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,6 +14,9 @@
1414
package org.eclipse.swt.graphics;
1515

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

1821
/**
1922
* Internal class that separates ImageData from ImageLoader
@@ -29,4 +32,8 @@ public static ImageData[] load(String filename) {
2932
return new ImageLoader().load(filename);
3033
}
3134

35+
public static List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoom) {
36+
return new ImageLoader().load(filename, fileZoom, targetZoom);
37+
}
38+
3239
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.eclipse.swt.*;
2222
import org.eclipse.swt.graphics.*;
23-
import org.eclipse.swt.internal.*;
23+
import org.eclipse.swt.internal.DPIUtil.*;
2424

2525
/**
2626
* Abstract factory class for loading/unloading images from files or streams
@@ -62,15 +62,8 @@ static abstract class StaticImageFileFormat extends FileFormat {
6262
abstract ImageData[] loadFromByteStream();
6363

6464
@Override
65-
ImageData[] loadFromByteStream(int zoom) {
66-
if (zoom == DEFAULT_ZOOM) {
67-
return loadFromByteStream();
68-
}
69-
return Arrays.stream(loadFromByteStream()).map(data -> {
70-
int width = DPIUtil.scaleUp(data.width, zoom);
71-
int height = DPIUtil.scaleUp(data.height, zoom);
72-
return data.scaledTo(width, height);
73-
}).toArray(ImageData[]::new);
65+
List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom) {
66+
return Arrays.stream(loadFromByteStream()).map(d -> new ElementAtZoom<>(d, fileZoom)).toList();
7467
}
7568
}
7669

@@ -85,16 +78,16 @@ ImageData[] loadFromByteStream(int zoom) {
8578
*/
8679
abstract boolean isFileFormat(LEDataInputStream stream) throws IOException;
8780

88-
abstract ImageData[] loadFromByteStream(int zoom);
81+
abstract List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom);
8982

9083
/**
9184
* Read the specified input stream, and return the
9285
* device independent image array represented by the stream.
9386
*/
94-
public ImageData[] loadFromStream(LEDataInputStream stream, int zoom) {
87+
public List<ElementAtZoom<ImageData>> loadFromStream(LEDataInputStream stream, int fileZoom, int targetZoom) {
9588
try {
9689
inputStream = stream;
97-
return loadFromByteStream(zoom);
90+
return loadFromByteStream(fileZoom, targetZoom);
9891
} catch (Exception e) {
9992
if (e instanceof IOException) {
10093
SWT.error(SWT.ERROR_IO, e);
@@ -109,7 +102,7 @@ public ImageData[] loadFromStream(LEDataInputStream stream, int zoom) {
109102
* Read the specified input stream using the specified loader, and
110103
* return the device independent image array represented by the stream.
111104
*/
112-
public static ImageData[] load(InputStream is, ImageLoader loader, int zoom) {
105+
public static List<ElementAtZoom<ImageData>> load(InputStream is, ImageLoader loader, int fileZoom, int targetZoom) {
113106
LEDataInputStream stream = new LEDataInputStream(is);
114107
FileFormat fileFormat = FORMAT_FACTORIES.stream().skip(1) //
115108
.map(Supplier::get).filter(f -> {
@@ -122,7 +115,7 @@ public static ImageData[] load(InputStream is, ImageLoader loader, int zoom) {
122115
.findFirst().orElse(null);
123116
if (fileFormat == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
124117
fileFormat.loader = loader;
125-
return fileFormat.loadFromStream(stream, zoom);
118+
return fileFormat.loadFromStream(stream, fileZoom,targetZoom);
126119
}
127120

128121
/**

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/WinICOFileFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ ImageData loadIcon(int[] iconHeader) {
133133
StaticImageFileFormat png = new PNGFileFormat();
134134
if (png.isFileFormat(inputStream)) {
135135
png.loader = this.loader;
136-
return png.loadFromStream(inputStream, DEFAULT_ZOOM)[0];
136+
return png.loadFromStream(inputStream, DEFAULT_ZOOM, DEFAULT_ZOOM).get(0).element();
137137
}
138138
} catch (Exception e) {
139139
}

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

Lines changed: 5 additions & 6 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
@@ -614,16 +614,15 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
614614
this.imageFileNameProvider = imageFileNameProvider;
615615
currentDeviceZoom = DPIUtil.getDeviceZoom();
616616
ElementAtZoom<String> filename = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, currentDeviceZoom);
617-
if (filename.zoom() == currentDeviceZoom) {
617+
ElementAtZoom<ImageData> imageData = ImageDataLoader.load(filename.element(), filename.zoom(), currentDeviceZoom).get(0);
618+
if (imageData.zoom() == currentDeviceZoom) {
618619
initNative (filename.element());
619620

620621
if (this.surface == 0) {
621-
ImageData data = new ImageData(filename.element());
622-
init(data);
622+
init(imageData.element());
623623
}
624624
} else {
625-
ImageData imageData = new ImageData (filename.element());
626-
ImageData resizedData = DPIUtil.autoScaleImageData (device, imageData, filename.zoom());
625+
ImageData resizedData = DPIUtil.autoScaleImageData (device, imageData.element(), imageData.zoom());
627626
init(resizedData);
628627
}
629628
init ();

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019 Red Hat and others. All rights reserved.
2+
* Copyright (c) 2019, 2025 Red Hat and others. All rights reserved.
33
* The contents of this file are made available under the terms
44
* of the GNU Lesser General Public License (LGPL) Version 2.1 that
55
* accompanies this distribution (lgpl-v21.txt). The LGPL is also
@@ -21,6 +21,7 @@
2121

2222
import org.eclipse.swt.*;
2323
import org.eclipse.swt.internal.*;
24+
import org.eclipse.swt.internal.DPIUtil.*;
2425
import org.eclipse.swt.internal.gtk.*;
2526
import org.eclipse.swt.internal.image.*;
2627
import org.eclipse.swt.widgets.*;
@@ -166,6 +167,14 @@ public ImageData[] load(InputStream stream) {
166167
return imgDataArray;
167168
}
168169

170+
private List<ElementAtZoom<ImageData>> load(InputStream stream, int fileZoom, int targetZoom) {
171+
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
172+
reset();
173+
ImageData[] imgDataArray = getImageDataArrayFromStream(stream);
174+
data = imgDataArray;
175+
return Arrays.stream(imgDataArray).map(i -> new ElementAtZoom<>(i, fileZoom)).toList();
176+
}
177+
169178
/**
170179
* Return true if the image is an interlaced PNG file.
171180
* This is used to check whether ImageLoaderEvent should be fired when loading images.
@@ -290,9 +299,14 @@ boolean isInterlacedPNG(byte [] imageAsByteArray) {
290299
* </ul>
291300
*/
292301
public ImageData[] load(String filename) {
302+
load(filename, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM);
303+
return data;
304+
}
305+
306+
List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoom) {
293307
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
294308
try (InputStream stream = new FileInputStream(filename)) {
295-
return load(stream);
309+
return load(stream, fileZoom, targetZoom);
296310
} catch (IOException e) {
297311
SWT.error(SWT.ERROR_IO, e);
298312
}

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

Lines changed: 9 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
@@ -552,15 +552,17 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
552552
super(device);
553553
this.imageProvider = new ImageFileNameProviderWrapper(imageFileNameProvider);
554554
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
555-
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, getZoom());
556-
if (fileName.zoom() == getZoom()) {
557-
ImageHandle imageMetadata = initNative (fileName.element(), getZoom());
555+
int deviceZoom = getZoom();
556+
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom(imageFileNameProvider, deviceZoom);
557+
ElementAtZoom<ImageData> imageData = ImageDataLoader.load(fileName.element(), fileName.zoom(), deviceZoom).get(0);
558+
if (imageData.zoom() == deviceZoom) {
559+
ImageHandle imageMetadata = initNative(fileName.element(), deviceZoom);
558560
if (imageMetadata == null) {
559-
init(new ImageData (fileName.element()), getZoom());
561+
init(imageData.element(), deviceZoom);
560562
}
561563
} else {
562-
ImageData resizedData = DPIUtil.autoScaleImageData (device, new ImageData (fileName.element()), fileName.zoom());
563-
init(resizedData, getZoom());
564+
ImageData resizedData = DPIUtil.autoScaleImageData(device, imageData.element(), imageData.zoom());
565+
init(resizedData, deviceZoom);
564566
}
565567
init();
566568
this.device.registerResourceWithZoomSupport(this);

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2016 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
@@ -18,6 +18,7 @@
1818
import java.util.*;
1919

2020
import org.eclipse.swt.*;
21+
import org.eclipse.swt.internal.DPIUtil.*;
2122
import org.eclipse.swt.internal.image.*;
2223

2324
/**
@@ -149,10 +150,16 @@ void reset() {
149150
* </ul>
150151
*/
151152
public ImageData[] load(InputStream stream) {
153+
load(stream, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM);
154+
return data;
155+
}
156+
157+
private List<ElementAtZoom<ImageData>> load(InputStream stream, int fileZoom, int targetZoom) {
152158
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
153159
reset();
154-
data = FileFormat.load(stream, this, FileFormat.DEFAULT_ZOOM);
155-
return data;
160+
List<ElementAtZoom<ImageData>> images = FileFormat.load(stream, this, fileZoom, targetZoom);
161+
data = images.stream().map(ElementAtZoom::element).toArray(ImageData[]::new);
162+
return images;
156163
}
157164

158165
/**
@@ -174,9 +181,14 @@ public ImageData[] load(InputStream stream) {
174181
* </ul>
175182
*/
176183
public ImageData[] load(String filename) {
184+
load(filename, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM);
185+
return data;
186+
}
187+
188+
List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoom) {
177189
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
178190
try (InputStream stream = new FileInputStream(filename)) {
179-
return load(stream);
191+
return load(stream, fileZoom, targetZoom);
180192
} catch (IOException e) {
181193
SWT.error(SWT.ERROR_IO, e);
182194
}

0 commit comments

Comments
 (0)