@@ -566,11 +566,9 @@ public Image (Device device, String filename) {
566
566
public Image (Device device , ImageFileNameProvider imageFileNameProvider ) {
567
567
super (device );
568
568
this .imageProvider = new ImageFileNameProviderWrapper (imageFileNameProvider );
569
- if (!(imageFileNameProvider instanceof SizeAwareImageFileNameProvider )) {
570
- if (imageFileNameProvider .getImagePath (100 ) == null ) {
569
+ if (imageFileNameProvider .getImagePath (100 ) == null ) {
571
570
SWT .error (SWT .ERROR_INVALID_ARGUMENT , null ,
572
571
": ImageFileNameProvider [" + imageFileNameProvider + "] returns null fileName at 100% zoom." );
573
- }
574
572
}
575
573
init ();
576
574
this .device .registerResourceWithZoomSupport (this );
@@ -608,11 +606,9 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
608
606
public Image (Device device , ImageDataProvider imageDataProvider ) {
609
607
super (device );
610
608
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." );
616
612
}
617
613
init ();
618
614
this .device .registerResourceWithZoomSupport (this );
@@ -1167,11 +1163,7 @@ public Color getBackground() {
1167
1163
*/
1168
1164
public Rectangle getBounds () {
1169
1165
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 );
1175
1167
}
1176
1168
1177
1169
Rectangle getBounds (int zoom ) {
@@ -1913,6 +1905,49 @@ public static Image win32_new(Device device, int type, long handle, int nativeZo
1913
1905
return new Image (device , type , handle , nativeZoom );
1914
1906
}
1915
1907
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
+
1916
1951
private abstract class AbstractImageProviderWrapper {
1917
1952
1918
1953
protected boolean sizeAware = false ;
@@ -1933,7 +1968,9 @@ public Collection<Integer> getPreservedZoomLevels() {
1933
1968
1934
1969
abstract ImageData newImageData (int zoom );
1935
1970
1936
- abstract ImageData newImageData (int targetWidth , int targetHeight );
1971
+ ImageData newImageData (int targetWidth , int targetHeight ) {
1972
+ throw new UnsupportedOperationException ();
1973
+ };
1937
1974
1938
1975
abstract AbstractImageProviderWrapper createCopy (Image image );
1939
1976
@@ -2011,8 +2048,6 @@ private abstract class ImageFromImageDataProviderWrapper extends AbstractImagePr
2011
2048
2012
2049
protected abstract ElementAtZoom <ImageData > loadImageData (int zoom );
2013
2050
2014
- protected abstract ImageData loadImageData (int targetWidth , int targetHeight );
2015
-
2016
2051
void initImage () {
2017
2052
// As the init call configured some Image attributes (e.g. type)
2018
2053
// it must be called
@@ -2030,14 +2065,6 @@ ImageData newImageData(int zoom) {
2030
2065
return cachedImageData .computeIfAbsent (zoom , imageDataRetrieval );
2031
2066
}
2032
2067
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
-
2041
2068
@ Override
2042
2069
protected ImageHandle newImageHandle (int zoom ) {
2043
2070
ImageData cachedData = cachedImageData .remove (zoom );
@@ -2081,11 +2108,6 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
2081
2108
return new ElementAtZoom <>(imageDataAtBaseZoom , baseZoom );
2082
2109
}
2083
2110
2084
- @ Override
2085
- protected ImageData loadImageData (int targetWidth , int targetHeight ) {
2086
- return null ;
2087
- }
2088
-
2089
2111
@ Override
2090
2112
AbstractImageProviderWrapper createCopy (Image image ) {
2091
2113
return image .new PlainImageDataProviderWrapper (this .imageDataAtBaseZoom );
@@ -2121,11 +2143,6 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
2121
2143
AbstractImageProviderWrapper createCopy (Image image ) {
2122
2144
return image .new MaskedImageDataProviderWrapper (this .srcAt100 , this .maskAt100 );
2123
2145
}
2124
-
2125
- @ Override
2126
- protected ImageData loadImageData (int targetWidth , int targetHeight ) {
2127
- return null ;
2128
- }
2129
2146
}
2130
2147
2131
2148
private class ImageDataLoaderStreamProviderWrapper extends ImageFromImageDataProviderWrapper {
@@ -2149,11 +2166,6 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
2149
2166
return ImageDataLoader .load (new ByteArrayInputStream (inputStreamData ), FileFormat .DEFAULT_ZOOM , zoom );
2150
2167
}
2151
2168
2152
- @ Override
2153
- protected ImageData loadImageData (int targetWidth , int targetHeight ) {
2154
- return ImageDataLoader .loadByTargetSize (new ByteArrayInputStream (inputStreamData ), targetWidth , targetHeight );
2155
- }
2156
-
2157
2169
@ Override
2158
2170
protected Rectangle getBounds (int zoom ) {
2159
2171
ImageData scaledImageData = getImageData (zoom );
@@ -2209,11 +2221,6 @@ ImageData newImageData(int zoom) {
2209
2221
return getScaledImageData (zoom );
2210
2222
}
2211
2223
2212
- @ Override
2213
- ImageData newImageData (int targetWidth , int targetHeight ) {
2214
- return null ;
2215
- }
2216
-
2217
2224
@ Override
2218
2225
protected ImageHandle newImageHandle (int zoom ) {
2219
2226
if (zoomLevelToImageHandle .isEmpty ()) {
@@ -2363,12 +2370,11 @@ protected Rectangle getBounds(int zoom) {
2363
2370
private class ImageFileNameProviderWrapper extends BaseImageProviderWrapper <ImageFileNameProvider > {
2364
2371
ImageFileNameProviderWrapper (ImageFileNameProvider provider ) {
2365
2372
super (provider , ImageFileNameProvider .class );
2366
- // Checks for the contract of the passed provider require
2367
- // checking for valid image data creation
2368
2373
if (provider instanceof SizeAwareImageFileNameProvider ) {
2369
2374
this .sizeAware = true ;
2370
- return ;
2371
2375
}
2376
+ // Checks for the contract of the passed provider require
2377
+ // checking for valid image data creation
2372
2378
newImageData (DPIUtil .getDeviceZoom ());
2373
2379
}
2374
2380
@@ -2402,8 +2408,11 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
2402
2408
@ Override
2403
2409
protected ImageData loadImageData (int targetWidth , int targetHeight ) {
2404
2410
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 ;
2407
2416
}
2408
2417
2409
2418
private Optional <String > validateAndGetImagePathAtTargetSize (ImageFileNameProvider provider , int targetWidth , int targetHeight ) {
@@ -2428,42 +2437,6 @@ private Optional<String> validateAndGetImagePathAtTargetSize(ImageFileNameProvid
2428
2437
}
2429
2438
}
2430
2439
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
-
2467
2440
@ Override
2468
2441
public int hashCode () {
2469
2442
return Objects .hash (provider , styleFlag , transparentPixel );
@@ -2700,45 +2673,6 @@ private Optional<ImageData> validateAndGetImageDataAtTargetSize(int targetWidth,
2700
2673
return imageDataAtTargetSize ;
2701
2674
}
2702
2675
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
-
2742
2676
@ Override
2743
2677
ImageDataProviderWrapper createCopy (Image image ) {
2744
2678
return image .new ImageDataProviderWrapper (provider );
0 commit comments