@@ -566,11 +566,9 @@ public Image (Device device, String filename) {
566566public Image (Device device , ImageFileNameProvider imageFileNameProvider ) {
567567 super (device );
568568 this .imageProvider = new ImageFileNameProviderWrapper (imageFileNameProvider );
569- if (!(imageFileNameProvider instanceof SizeAwareImageFileNameProvider )) {
570- if (imageFileNameProvider .getImagePath (100 ) == null ) {
569+ if (imageFileNameProvider .getImagePath (100 ) == null ) {
571570 SWT .error (SWT .ERROR_INVALID_ARGUMENT , null ,
572571 ": ImageFileNameProvider [" + imageFileNameProvider + "] returns null fileName at 100% zoom." );
573- }
574572 }
575573 init ();
576574 this .device .registerResourceWithZoomSupport (this );
@@ -608,11 +606,9 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
608606public Image (Device device , ImageDataProvider imageDataProvider ) {
609607 super (device );
610608 this .imageProvider = new ImageDataProviderWrapper (imageDataProvider );
611- if (!(imageDataProvider instanceof SizeAwareImageDataProvider )) {
612- if (imageDataProvider .getImageData (100 ) == null ) {
613- SWT .error (SWT .ERROR_INVALID_ARGUMENT , null ,
614- ": ImageDataProvider [" + imageDataProvider + "] returns null ImageData at 100% zoom." );
615- }
609+ if (imageDataProvider .getImageData (100 ) == null ) {
610+ SWT .error (SWT .ERROR_INVALID_ARGUMENT , null ,
611+ ": ImageDataProvider [" + imageDataProvider + "] returns null ImageData at 100% zoom." );
616612 }
617613 init ();
618614 this .device .registerResourceWithZoomSupport (this );
@@ -1167,11 +1163,7 @@ public Color getBackground() {
11671163 */
11681164public Rectangle getBounds () {
11691165 if (isDisposed ()) SWT .error (SWT .ERROR_GRAPHIC_DISPOSED );
1170- if (this .imageProvider .isSizeAware ()) {
1171- return new Rectangle (0 , 0 , 16 , 16 );
1172- } else {
1173- return getBounds (100 );
1174- }
1166+ return getBounds (100 );
11751167}
11761168
11771169Rectangle getBounds (int zoom ) {
@@ -1913,6 +1905,49 @@ public static Image win32_new(Device device, int type, long handle, int nativeZo
19131905 return new Image (device , type , handle , nativeZoom );
19141906}
19151907
1908+ private <T > Optional <T > getElementAtTargetSize (BiFunction <Integer , Integer , Optional <T >> elementForZoomProvider ,
1909+ int targetWidth , int targetHeight ) {
1910+ Rectangle originalSize = getBounds (100 );
1911+ int originalWidth = originalSize .width ;
1912+ int originalHeight = originalSize .height ;
1913+
1914+ Optional <T > dataAtOriginalZoom = elementForZoomProvider .apply (targetWidth , targetHeight );
1915+ if (dataAtOriginalZoom .isPresent ()) {
1916+ T data = dataAtOriginalZoom .get ();
1917+ if (data instanceof String fileName ) {
1918+ ImageData imageData = ImageDataLoader .loadByTargetSize (fileName , targetWidth , targetHeight );
1919+ if (imageData .width == targetWidth && imageData .height == targetHeight ) {
1920+ return dataAtOriginalZoom ;
1921+ }
1922+ } else if (data instanceof ImageData )
1923+ return dataAtOriginalZoom ;
1924+ }
1925+
1926+ if (targetWidth >= originalWidth && targetWidth <= originalWidth * 1.5 ) {
1927+ Optional <T > dataAt150Percent = elementForZoomProvider .apply ((int ) Math .round (originalWidth * 1.5 ),
1928+ (int ) Math .round (originalHeight * 1.5 ));
1929+ if (dataAt150Percent .isPresent ()) {
1930+ return dataAt150Percent ;
1931+ }
1932+ }
1933+
1934+ if (targetWidth > originalWidth ) {
1935+ Optional <T > dataAt200Percent = elementForZoomProvider .apply (originalWidth * 2 , originalHeight * 2 );
1936+ if (dataAt200Percent .isPresent ()) {
1937+ return dataAt200Percent ;
1938+ }
1939+ }
1940+
1941+ if (targetWidth != originalWidth ) {
1942+ Optional <T > dataAt100Percent = elementForZoomProvider .apply (originalWidth , originalHeight );
1943+ if (dataAt100Percent .isPresent ()) {
1944+ return dataAt100Percent ;
1945+ }
1946+ }
1947+
1948+ return Optional .empty ();
1949+ }
1950+
19161951private abstract class AbstractImageProviderWrapper {
19171952
19181953 protected boolean sizeAware = false ;
@@ -1933,7 +1968,9 @@ public Collection<Integer> getPreservedZoomLevels() {
19331968
19341969 abstract ImageData newImageData (int zoom );
19351970
1936- abstract ImageData newImageData (int targetWidth , int targetHeight );
1971+ ImageData newImageData (int targetWidth , int targetHeight ) {
1972+ throw new UnsupportedOperationException ();
1973+ };
19371974
19381975 abstract AbstractImageProviderWrapper createCopy (Image image );
19391976
@@ -2011,8 +2048,6 @@ private abstract class ImageFromImageDataProviderWrapper extends AbstractImagePr
20112048
20122049 protected abstract ElementAtZoom <ImageData > loadImageData (int zoom );
20132050
2014- protected abstract ImageData loadImageData (int targetWidth , int targetHeight );
2015-
20162051 void initImage () {
20172052 // As the init call configured some Image attributes (e.g. type)
20182053 // it must be called
@@ -2030,14 +2065,6 @@ ImageData newImageData(int zoom) {
20302065 return cachedImageData .computeIfAbsent (zoom , imageDataRetrieval );
20312066 }
20322067
2033- @ Override
2034- ImageData newImageData (int targetWidth , int targetHeight ) {
2035- ImageData imageDataAtTargetSize = loadImageData (targetWidth , targetHeight );
2036- ImageData imageData = DPIUtil .autoScaleImageData (device , imageDataAtTargetSize , targetWidth , targetHeight );
2037- imageData = adaptImageDataIfDisabledOrGray (imageData );
2038- return imageData ;
2039- }
2040-
20412068 @ Override
20422069 protected ImageHandle newImageHandle (int zoom ) {
20432070 ImageData cachedData = cachedImageData .remove (zoom );
@@ -2081,11 +2108,6 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
20812108 return new ElementAtZoom <>(imageDataAtBaseZoom , baseZoom );
20822109 }
20832110
2084- @ Override
2085- protected ImageData loadImageData (int targetWidth , int targetHeight ) {
2086- return null ;
2087- }
2088-
20892111 @ Override
20902112 AbstractImageProviderWrapper createCopy (Image image ) {
20912113 return image .new PlainImageDataProviderWrapper (this .imageDataAtBaseZoom );
@@ -2121,11 +2143,6 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
21212143 AbstractImageProviderWrapper createCopy (Image image ) {
21222144 return image .new MaskedImageDataProviderWrapper (this .srcAt100 , this .maskAt100 );
21232145 }
2124-
2125- @ Override
2126- protected ImageData loadImageData (int targetWidth , int targetHeight ) {
2127- return null ;
2128- }
21292146}
21302147
21312148private class ImageDataLoaderStreamProviderWrapper extends ImageFromImageDataProviderWrapper {
@@ -2149,11 +2166,6 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
21492166 return ImageDataLoader .load (new ByteArrayInputStream (inputStreamData ), FileFormat .DEFAULT_ZOOM , zoom );
21502167 }
21512168
2152- @ Override
2153- protected ImageData loadImageData (int targetWidth , int targetHeight ) {
2154- return ImageDataLoader .loadByTargetSize (new ByteArrayInputStream (inputStreamData ), targetWidth , targetHeight );
2155- }
2156-
21572169 @ Override
21582170 protected Rectangle getBounds (int zoom ) {
21592171 ImageData scaledImageData = getImageData (zoom );
@@ -2209,11 +2221,6 @@ ImageData newImageData(int zoom) {
22092221 return getScaledImageData (zoom );
22102222 }
22112223
2212- @ Override
2213- ImageData newImageData (int targetWidth , int targetHeight ) {
2214- return null ;
2215- }
2216-
22172224 @ Override
22182225 protected ImageHandle newImageHandle (int zoom ) {
22192226 if (zoomLevelToImageHandle .isEmpty ()) {
@@ -2363,12 +2370,11 @@ protected Rectangle getBounds(int zoom) {
23632370private class ImageFileNameProviderWrapper extends BaseImageProviderWrapper <ImageFileNameProvider > {
23642371 ImageFileNameProviderWrapper (ImageFileNameProvider provider ) {
23652372 super (provider , ImageFileNameProvider .class );
2366- // Checks for the contract of the passed provider require
2367- // checking for valid image data creation
23682373 if (provider instanceof SizeAwareImageFileNameProvider ) {
23692374 this .sizeAware = true ;
2370- return ;
23712375 }
2376+ // Checks for the contract of the passed provider require
2377+ // checking for valid image data creation
23722378 newImageData (DPIUtil .getDeviceZoom ());
23732379 }
23742380
@@ -2402,8 +2408,11 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
24022408 @ Override
24032409 protected ImageData loadImageData (int targetWidth , int targetHeight ) {
24042410 Optional <String > fileForTargetSize = validateAndGetImagePathAtTargetSize (provider , targetWidth , targetHeight );
2405- ImageData imageDataAtZoom = ImageDataLoader .loadByTargetSize (fileForTargetSize .get (), targetWidth , targetWidth );
2406- return imageDataAtZoom ;
2411+ if (fileForTargetSize .isPresent ()) {
2412+ ImageData imageDataAtZoom = ImageDataLoader .loadByTargetSize (fileForTargetSize .get (), targetWidth , targetWidth );
2413+ return imageDataAtZoom ;
2414+ }
2415+ return null ;
24072416 }
24082417
24092418 private Optional <String > validateAndGetImagePathAtTargetSize (ImageFileNameProvider provider , int targetWidth , int targetHeight ) {
@@ -2428,42 +2437,6 @@ private Optional<String> validateAndGetImagePathAtTargetSize(ImageFileNameProvid
24282437 }
24292438 }
24302439
2431- private Optional <String > getElementAtTargetSize (BiFunction <Integer , Integer , Optional <String >> elementForZoomProvider ,
2432- int targetWidth , int targetHeight ) {
2433-
2434- Rectangle originalSize = getBounds (100 );
2435- int originalWidth = originalSize .width ;
2436- int originalHeight = originalSize .height ;
2437-
2438- Optional <String > dataAtOriginalZoom = elementForZoomProvider .apply (targetWidth , targetHeight );
2439- if (dataAtOriginalZoom != null ) {
2440- ImageData data = ImageDataLoader .loadByTargetSize (dataAtOriginalZoom .get (), targetWidth , targetWidth );
2441- if (data .width == targetWidth && data .height == targetHeight ) {
2442- return dataAtOriginalZoom ;
2443- }
2444- }
2445-
2446- if (targetWidth >= originalWidth && targetWidth <= originalWidth * 1.5 ) {
2447- Optional <String > dataAt150Percent = elementForZoomProvider .apply ((int ) Math .round (originalWidth * 1.5 ), (int ) Math .round (originalHeight * 1.5 ));
2448- if (dataAt150Percent != null ) {
2449- return dataAt150Percent ;
2450- }
2451- }
2452- if (targetWidth > originalWidth ) {
2453- Optional <String > dataAt200Percent = elementForZoomProvider .apply (originalWidth * 2 , originalHeight * 2 );
2454- if (dataAt200Percent != null ) {
2455- return dataAt200Percent ;
2456- }
2457- }
2458- if (targetWidth != originalWidth ) {
2459- Optional <String > dataAt100Percent = elementForZoomProvider .apply (originalWidth , originalHeight );
2460- if (dataAt100Percent != null ) {
2461- return dataAt100Percent ;
2462- }
2463- }
2464- return null ;
2465- }
2466-
24672440 @ Override
24682441 public int hashCode () {
24692442 return Objects .hash (provider , styleFlag , transparentPixel );
@@ -2700,45 +2673,6 @@ private Optional<ImageData> validateAndGetImageDataAtTargetSize(int targetWidth,
27002673 return imageDataAtTargetSize ;
27012674 }
27022675
2703- private Optional <ImageData > getElementAtTargetSize (BiFunction <Integer , Integer , Optional <ImageData >> elementForZoomProvider , int targetWidth , int targetHeight ) {
2704- Rectangle originalSize = getBounds (100 );
2705- int originalWidth = originalSize .width ;
2706- int originalHeight = originalSize .height ;
2707-
2708- Optional <ImageData > dataAtOriginalZoom = elementForZoomProvider .apply (targetWidth , targetHeight );
2709- if (dataAtOriginalZoom .isPresent ()) {
2710- ImageData imageData = dataAtOriginalZoom .get ();
2711- if (imageData .width == targetWidth && imageData .height == targetHeight ) {
2712- return dataAtOriginalZoom ;
2713- }
2714- }
2715-
2716- if (targetWidth >= originalWidth && targetWidth <= originalWidth * 1.5 ) {
2717- Optional <ImageData > dataAt150Percent = elementForZoomProvider .apply ((int ) Math .round (originalWidth * 1.5 ),
2718- (int ) Math .round (originalHeight * 1.5 ));
2719- if (dataAt150Percent .isPresent ()) {
2720- return dataAt150Percent ;
2721- }
2722- }
2723-
2724- if (targetWidth > originalWidth ) {
2725- Optional <ImageData > dataAt200Percent = elementForZoomProvider .apply (originalWidth * 2 , originalHeight * 2 );
2726- if (dataAt200Percent .isPresent ()) {
2727- return dataAt200Percent ;
2728- }
2729- }
2730-
2731- if (targetWidth != originalWidth ) {
2732- Optional <ImageData > dataAt100Percent = elementForZoomProvider .apply (originalWidth , originalHeight );
2733- if (dataAt100Percent .isPresent ()) {
2734- return dataAt100Percent ;
2735- }
2736- }
2737-
2738- return Optional .empty ();
2739- }
2740-
2741-
27422676 @ Override
27432677 ImageDataProviderWrapper createCopy (Image image ) {
27442678 return image .new ImageDataProviderWrapper (provider );
0 commit comments