@@ -803,11 +803,12 @@ private ImageData applyGrayImageData(ImageData data, int pHeight, int pWidth) {
803803 return newData ;
804804}
805805
806- private ImageHandle getImageMetadata (int zoom ) {
806+ private ImageHandle getImageMetadata (ZoomContext zoomContext ) {
807+ int zoom = zoomContext .targetZoom ;
807808 if (zoomLevelToImageHandle .get (zoom ) != null ) {
808809 return zoomLevelToImageHandle .get (zoom );
809810 }
810- return imageProvider .newImageHandle (zoom );
811+ return imageProvider .newImageHandle (zoomContext );
811812}
812813
813814
@@ -831,7 +832,7 @@ public static long win32_getHandle (Image image, int zoom) {
831832 if (image .isDisposed ()) {
832833 return 0L ;
833834 }
834- return image .getImageMetadata (zoom ).handle ;
835+ return image .getImageMetadata (new ZoomContext ( zoom ) ).handle ;
835836}
836837
837838/**
@@ -1265,7 +1266,7 @@ public ImageData getImageData (int zoom) {
12651266 if (zoomLevelToImageHandle .containsKey (zoom )) {
12661267 return zoomLevelToImageHandle .get (zoom ).getImageData ();
12671268 }
1268- return this .imageProvider .newImageData (zoom );
1269+ return this .imageProvider .newImageData (new ZoomContext ( zoom ) );
12691270}
12701271
12711272
@@ -1882,7 +1883,7 @@ public String toString () {
18821883
18831884<T > T applyUsingAnyHandle (Function <ImageHandle , T > function ) {
18841885 if (zoomLevelToImageHandle .isEmpty ()) {
1885- ImageHandle temporaryHandle = this .imageProvider .newImageHandle (DPIUtil .getDeviceZoom ());
1886+ ImageHandle temporaryHandle = this .imageProvider .newImageHandle (new ZoomContext ( DPIUtil .getDeviceZoom () ));
18861887 try {
18871888 return function .apply (temporaryHandle );
18881889 } finally {
@@ -1913,6 +1914,13 @@ public static Image win32_new(Device device, int type, long handle, int nativeZo
19131914 return new Image (device , type , handle , nativeZoom );
19141915}
19151916
1917+ private record ZoomContext (int targetZoom , int nativeZoom ) {
1918+
1919+ private ZoomContext (int targetZoom ) {
1920+ this (targetZoom , targetZoom );
1921+ }
1922+ }
1923+
19161924private abstract class AbstractImageProviderWrapper {
19171925
19181926 protected abstract Rectangle getBounds (int zoom );
@@ -1925,30 +1933,30 @@ public Collection<Integer> getPreservedZoomLevels() {
19251933 return Collections .emptySet ();
19261934 }
19271935
1928- abstract ImageData newImageData (int zoom );
1936+ abstract ImageData newImageData (ZoomContext zoomContext );
19291937
19301938 abstract AbstractImageProviderWrapper createCopy (Image image );
19311939
19321940 ImageData getScaledImageData (int zoom ) {
19331941 TreeSet <Integer > availableZooms = new TreeSet <>(zoomLevelToImageHandle .keySet ());
19341942 int closestZoom = Optional .ofNullable (availableZooms .higher (zoom )).orElse (availableZooms .lower (zoom ));
1935- return DPIUtil .scaleImageData (device , getImageMetadata (closestZoom ).getImageData (), zoom , closestZoom );
1943+ return DPIUtil .scaleImageData (device , getImageMetadata (new ZoomContext ( closestZoom ) ).getImageData (), zoom , closestZoom );
19361944 }
19371945
1938- protected ImageHandle newImageHandle (int zoom ) {
1939- ImageData resizedData = getImageData (zoom );
1940- return newImageHandle (resizedData , zoom );
1946+ protected ImageHandle newImageHandle (ZoomContext zoomContext ) {
1947+ ImageData resizedData = getImageData (zoomContext . targetZoom );
1948+ return newImageHandle (resizedData , zoomContext );
19411949 }
19421950
1943- protected final ImageHandle newImageHandle (ImageData data , int zoom ) {
1951+ protected final ImageHandle newImageHandle (ImageData data , ZoomContext zoomContext ) {
19441952 if (type == SWT .ICON && data .getTransparencyType () != SWT .TRANSPARENCY_MASK ) {
19451953 // If the original type was an icon with transparency mask and re-scaling leads
19461954 // to image data without transparency mask, this will create invalid images
19471955 // so this fallback will "repair" the image data by explicitly passing
19481956 // the transparency mask created from the scaled image data
1949- return initIconHandle (device , data , data .getTransparencyMask (), zoom );
1957+ return initIconHandle (device , data , data .getTransparencyMask (), zoomContext . targetZoom );
19501958 } else {
1951- return init (data , zoom );
1959+ return init (data , zoomContext . targetZoom );
19521960 }
19531961 }
19541962}
@@ -1977,8 +1985,8 @@ protected Rectangle getBounds(int zoom) {
19771985 }
19781986
19791987 @ Override
1980- ImageData newImageData (int zoom ) {
1981- return getScaledImageData (zoom );
1988+ ImageData newImageData (ZoomContext zoomContext ) {
1989+ return getScaledImageData (zoomContext . targetZoom );
19821990 }
19831991
19841992 @ Override
@@ -2000,34 +2008,34 @@ private abstract class ImageFromImageDataProviderWrapper extends AbstractImagePr
20002008 void initImage () {
20012009 // As the init call configured some Image attributes (e.g. type)
20022010 // it must be called
2003- newImageData (100 );
2011+ newImageData (new ZoomContext ( 100 ) );
20042012 }
20052013
20062014 @ Override
2007- ImageData newImageData (int zoom ) {
2015+ ImageData newImageData (ZoomContext zoomContext ) {
20082016 Function <Integer , ImageData > imageDataRetrieval = zoomToRetrieve -> {
2009- ImageHandle handle = initializeHandleFromSource (zoomToRetrieve );
2017+ ImageHandle handle = initializeHandleFromSource (zoomContext );
20102018 ImageData data = handle .getImageData ();
20112019 handle .destroy ();
20122020 return data ;
20132021 };
2014- return cachedImageData .computeIfAbsent (zoom , imageDataRetrieval );
2022+ return cachedImageData .computeIfAbsent (zoomContext . targetZoom , imageDataRetrieval );
20152023 }
20162024
20172025 @ Override
2018- protected ImageHandle newImageHandle (int zoom ) {
2019- ImageData cachedData = cachedImageData .remove (zoom );
2026+ protected ImageHandle newImageHandle (ZoomContext zoomContext ) {
2027+ ImageData cachedData = cachedImageData .remove (zoomContext . targetZoom );
20202028 if (cachedData != null ) {
2021- return newImageHandle (cachedData , zoom );
2029+ return newImageHandle (cachedData , zoomContext );
20222030 }
2023- return initializeHandleFromSource (zoom );
2031+ return initializeHandleFromSource (zoomContext );
20242032 }
20252033
2026- private ImageHandle initializeHandleFromSource (int zoom ) {
2027- ElementAtZoom <ImageData > imageDataAtZoom = loadImageData (zoom );
2028- ImageData imageData = DPIUtil .scaleImageData (device , imageDataAtZoom .element (), zoom , imageDataAtZoom .zoom ());
2034+ private ImageHandle initializeHandleFromSource (ZoomContext zoomContext ) {
2035+ ElementAtZoom <ImageData > imageDataAtZoom = loadImageData (zoomContext . targetZoom );
2036+ ImageData imageData = DPIUtil .scaleImageData (device , imageDataAtZoom .element (), zoomContext . targetZoom , imageDataAtZoom .zoom ());
20292037 imageData = adaptImageDataIfDisabledOrGray (imageData );
2030- return newImageHandle (imageData , zoom );
2038+ return newImageHandle (imageData , zoomContext );
20312039 }
20322040
20332041}
@@ -2159,20 +2167,22 @@ protected Rectangle getBounds(int zoom) {
21592167 }
21602168
21612169 @ Override
2162- ImageData newImageData (int zoom ) {
2170+ ImageData newImageData (ZoomContext zoomContext ) {
2171+ int zoom = zoomContext .targetZoom ;
21632172 if (zoomLevelToImageHandle .isEmpty ()) {
21642173 return createBaseHandle (zoom ).getImageData ();
21652174 }
21662175 // if a GC is initialized with an Image (memGC != null), the image data must not be resized, because it would
21672176 // be a destructive operation. Therefor, a new handle is created for the requested zoom
21682177 if (memGC != null ) {
2169- return newImageHandle (zoom ).getImageData ();
2178+ return newImageHandle (zoomContext ).getImageData ();
21702179 }
21712180 return getScaledImageData (zoom );
21722181 }
21732182
21742183 @ Override
2175- protected ImageHandle newImageHandle (int zoom ) {
2184+ protected ImageHandle newImageHandle (ZoomContext zoomContext ) {
2185+ int zoom = zoomContext .targetZoom ;
21762186 if (zoomLevelToImageHandle .isEmpty ()) {
21772187 return createBaseHandle (zoom );
21782188 }
@@ -2183,7 +2193,7 @@ protected ImageHandle newImageHandle(int zoom) {
21832193 currentGC .refreshFor (new DrawableWrapper (Image .this , zoom ), zoom );
21842194 return zoomLevelToImageHandle .get (zoom );
21852195 }
2186- return super .newImageHandle (zoom );
2196+ return super .newImageHandle (zoomContext );
21872197 }
21882198 private ImageHandle createBaseHandle (int zoom ) {
21892199 baseZoom = zoom ;
@@ -2272,19 +2282,20 @@ Object getProvider() {
22722282 }
22732283
22742284 @ Override
2275- ImageData newImageData (int zoom ) {
2285+ ImageData newImageData (ZoomContext zoomContext ) {
22762286 Function <Integer , ImageData > imageDataRetrival = zoomToRetrieve -> {
22772287 ImageHandle handle = initializeHandleFromSource (zoomToRetrieve );
22782288 ImageData data = handle .getImageData ();
22792289 handle .destroy ();
22802290 return data ;
22812291 };
2282- return cachedImageData .computeIfAbsent (zoom , imageDataRetrival );
2292+ return cachedImageData .computeIfAbsent (zoomContext . targetZoom , imageDataRetrival );
22832293 }
22842294
22852295
22862296 @ Override
2287- protected ImageHandle newImageHandle (int zoom ) {
2297+ protected ImageHandle newImageHandle (ZoomContext zoomContext ) {
2298+ int zoom = zoomContext .targetZoom ;
22882299 ImageData cachedData = cachedImageData .remove (zoom );
22892300 if (cachedData != null ) {
22902301 return init (cachedData , zoom );
@@ -2313,7 +2324,7 @@ private class ImageFileNameProviderWrapper extends BaseImageProviderWrapper<Imag
23132324 super (provider , ImageFileNameProvider .class );
23142325 // Checks for the contract of the passed provider require
23152326 // checking for valid image data creation
2316- newImageData (DPIUtil .getDeviceZoom ());
2327+ newImageData (new ZoomContext ( DPIUtil .getDeviceZoom () ));
23172328 }
23182329
23192330 @ Override
@@ -2567,7 +2578,7 @@ private class ImageGcDrawerWrapper extends DynamicImageProviderWrapper {
25672578 private ImageGcDrawer drawer ;
25682579 private int width ;
25692580 private int height ;
2570- private int currentZoom = 100 ;
2581+ private ZoomContext currentZoom = new ZoomContext ( 100 ) ;
25712582
25722583 ImageGcDrawerWrapper (ImageGcDrawer imageGcDrawer , int width , int height ) {
25732584 checkProvider (imageGcDrawer , ImageGcDrawer .class );
@@ -2584,17 +2595,19 @@ protected Rectangle getBounds(int zoom) {
25842595
25852596 @ Override
25862597 protected long configureGCData (GCData data ) {
2587- return configureGC (data , currentZoom );
2598+ return configureGC (data , currentZoom . nativeZoom );
25882599 }
25892600
25902601 @ Override
2591- ImageData newImageData (int zoom ) {
2592- return getImageMetadata (zoom ).getImageData ();
2602+ ImageData newImageData (ZoomContext zoomContext ) {
2603+ return getImageMetadata (zoomContext ).getImageData ();
25932604 }
25942605
25952606 @ Override
2596- protected ImageHandle newImageHandle (int zoom ) {
2597- currentZoom = zoom ;
2607+ protected ImageHandle newImageHandle (ZoomContext zoomContext ) {
2608+ currentZoom = zoomContext ;
2609+ int zoom = zoomContext .targetZoom ;
2610+ int nativeZoom = zoomContext .nativeZoom ;
25982611 int gcStyle = drawer .getGcStyle ();
25992612 Image image ;
26002613 if ((gcStyle & SWT .TRANSPARENT ) != 0 ) {
@@ -2607,10 +2620,10 @@ protected ImageHandle newImageHandle(int zoom) {
26072620 } else {
26082621 image = new Image (device , width , height );
26092622 }
2610- GC gc = new GC (new DrawableWrapper (image , zoom ), gcStyle );
2623+ GC gc = new GC (new DrawableWrapper (image , nativeZoom ), gcStyle );
26112624 try {
26122625 drawer .drawOn (gc , width , height );
2613- ImageData imageData = image .getImageMetadata ( zoom ). getImageData ();
2626+ ImageData imageData = image .getImageData (nativeZoom );
26142627 drawer .postProcess (imageData );
26152628 ImageData newData = adaptImageDataIfDisabledOrGray (imageData );
26162629 return init (newData , zoom );
0 commit comments