@@ -90,6 +90,11 @@ public final class Image extends Resource implements Drawable {
9090 */
9191 public int type ;
9292
93+ /**
94+ * this field make sure the image is initialized without any errors
95+ */
96+ boolean isInitialized = false ;
97+
9398 /**
9499 * specifies the transparent pixel
95100 */
@@ -552,7 +557,10 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
552557 super (device );
553558 this .imageProvider = new ImageFileNameProviderWrapper (imageFileNameProvider );
554559 initialNativeZoom = DPIUtil .getNativeDeviceZoom ();
555- imageProvider .getImageMetadata (getZoom ());
560+ if (imageProvider .getImageData (100 ) == null ) {
561+ SWT .error (SWT .ERROR_INVALID_ARGUMENT , null ,
562+ ": ImageFileNameProvider [" + imageFileNameProvider + "] returns null ImageData at 100% zoom." );
563+ }
556564 init ();
557565 this .device .registerResourceWithZoomSupport (this );
558566}
@@ -590,7 +598,10 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
590598 super (device );
591599 this .imageProvider = new ImageDataProviderWrapper (imageDataProvider );
592600 initialNativeZoom = DPIUtil .getNativeDeviceZoom ();
593- imageProvider .getImageMetadata (getZoom ());
601+ if (imageDataProvider .getImageData (100 ) == null ) {
602+ SWT .error (SWT .ERROR_INVALID_ARGUMENT , null ,
603+ ": ImageDataProvider [" + imageDataProvider + "] returns null ImageData at 100% zoom." );
604+ }
594605 init ();
595606 this .device .registerResourceWithZoomSupport (this );
596607}
@@ -642,6 +653,12 @@ private ImageData adaptImageDataIfDisabledOrGray(ImageData data) {
642653 return returnImageData ;
643654}
644655
656+ @ Override
657+ void init () {
658+ super .init ();
659+ this .isInitialized = true ;
660+ }
661+
645662private ImageData applyDisableImageData (ImageData data , int height , int width ) {
646663 PaletteData palette = data .palette ;
647664 RGB [] rgbs = new RGB [3 ];
@@ -1891,18 +1908,23 @@ public static Image win32_new(Device device, int type, long handle, int nativeZo
18911908}
18921909
18931910private abstract class AbstractImageProviderWrapper {
1911+ private boolean isDestroyed ;
1912+
18941913 protected abstract Rectangle getBounds (int zoom );
18951914 abstract ImageData getImageData (int zoom );
18961915 abstract ImageHandle getImageMetadata (int zoom );
18971916 abstract AbstractImageProviderWrapper createCopy (Image image );
1898- abstract boolean isDisposed ();
1917+
1918+ protected boolean isDisposed () {
1919+ return !isInitialized || isDestroyed ;
1920+ }
18991921
19001922 protected void destroy () {
1923+ this .isDestroyed = true ;
19011924 }
19021925}
19031926
19041927private class PlainImageProviderWrapper extends AbstractImageProviderWrapper {
1905- private boolean isDestroyed ;
19061928 private final int width ;
19071929 private final int height ;
19081930
@@ -1984,16 +2006,6 @@ private long initBaseHandle(int zoom) {
19842006 AbstractImageProviderWrapper createCopy (Image image ) {
19852007 return image .new PlainImageProviderWrapper (width , height );
19862008 }
1987-
1988- @ Override
1989- protected void destroy () {
1990- this .isDestroyed = true ;
1991- }
1992-
1993- @ Override
1994- boolean isDisposed () {
1995- return isDestroyed ;
1996- }
19972009}
19982010
19992011private abstract class DynamicImageProviderWrapper extends AbstractImageProviderWrapper {
@@ -2016,28 +2028,43 @@ public boolean equals(Object otherProvider) {
20162028 }
20172029}
20182030
2019- private class ImageFileNameProviderWrapper extends DynamicImageProviderWrapper {
2031+ private abstract class BaseImageProviderWrapper <T > extends DynamicImageProviderWrapper {
2032+ protected final T provider ;
2033+ private final Map <Integer , Rectangle > zoomToBounds = new HashMap <>();
20202034
2021- /**
2022- * ImageFileNameProvider to provide file names at various Zoom levels
2023- */
2024- private final ImageFileNameProvider provider ;
2025-
2026- ImageFileNameProviderWrapper (ImageFileNameProvider provider ) {
2027- checkProvider (provider , ImageFileNameProvider .class );
2035+ BaseImageProviderWrapper (T provider , Class <T > expectedClass ) {
2036+ checkProvider (provider , expectedClass );
20282037 this .provider = provider ;
20292038 }
20302039
2040+ @ Override
2041+ Object getProvider () {
2042+ return provider ;
2043+ }
2044+
20312045 @ Override
20322046 protected Rectangle getBounds (int zoom ) {
2033- ImageHandle imageHandle = zoomLevelToImageHandle .values ().iterator ().next ();
2034- Rectangle rectangle = new Rectangle (0 , 0 , imageHandle .width , imageHandle .height );
2035- return DPIUtil .scaleBounds (rectangle , zoom , imageHandle .zoom );
2047+ if (zoomLevelToImageHandle .containsKey (zoom )) {
2048+ ImageHandle imgHandle = zoomLevelToImageHandle .get (zoom );
2049+ return new Rectangle (0 , 0 , imgHandle .width , imgHandle .height );
2050+ }
2051+ if (!zoomToBounds .containsKey (zoom )) {
2052+ ImageData imageData = getImageData (zoom );
2053+ Rectangle rectangle = new Rectangle (0 , 0 , imageData .width , imageData .height );
2054+ zoomToBounds .put (zoom , rectangle );
2055+ }
2056+ return zoomToBounds .get (zoom );
2057+ }
2058+ }
2059+
2060+ private class ImageFileNameProviderWrapper extends BaseImageProviderWrapper <ImageFileNameProvider > {
2061+ ImageFileNameProviderWrapper (ImageFileNameProvider provider ) {
2062+ super (provider , ImageFileNameProvider .class );
20362063 }
20372064
20382065 @ Override
20392066 ImageData getImageData (int zoom ) {
2040- ElementAtZoom <String > fileForZoom = DPIUtil .validateAndGetImagePathAtZoom (provider , zoom );
2067+ ElementAtZoom <String > fileForZoom = DPIUtil .validateAndGetImagePathAtZoom (provider , zoom );
20412068 ImageHandle nativeInitializedImage ;
20422069 if (zoomLevelToImageHandle .containsKey (fileForZoom .zoom ())) {
20432070 nativeInitializedImage = zoomLevelToImageHandle .get (fileForZoom .zoom ());
@@ -2072,16 +2099,6 @@ ImageHandle getImageMetadata(int zoom) {
20722099 return zoomLevelToImageHandle .get (zoom );
20732100 }
20742101
2075- @ Override
2076- boolean isDisposed () {
2077- return zoomLevelToImageHandle .isEmpty ();
2078- }
2079-
2080- @ Override
2081- Object getProvider () {
2082- return provider ;
2083- }
2084-
20852102 @ Override
20862103 public int hashCode () {
20872104 return Objects .hash (provider , styleFlag , transparentPixel , getZoom ());
@@ -2286,23 +2303,9 @@ private long extractHandleForPixelFormat(int width, int height, int pixelFormat)
22862303 }
22872304}
22882305
2289- private class ImageDataProviderWrapper extends DynamicImageProviderWrapper {
2290-
2291- /**
2292- * ImageDataProvider to provide ImageData at various Zoom levels
2293- */
2294- private final ImageDataProvider provider ;
2295-
2306+ private class ImageDataProviderWrapper extends BaseImageProviderWrapper <ImageDataProvider > {
22962307 ImageDataProviderWrapper (ImageDataProvider provider ) {
2297- checkProvider (provider , ImageDataProvider .class );
2298- this .provider = provider ;
2299- }
2300-
2301- @ Override
2302- protected Rectangle getBounds (int zoom ) {
2303- ImageHandle imageHandle = zoomLevelToImageHandle .values ().iterator ().next ();
2304- Rectangle rectangle = new Rectangle (0 , 0 , imageHandle .width , imageHandle .height );
2305- return DPIUtil .scaleBounds (rectangle , zoom , imageHandle .zoom );
2308+ super (provider , ImageDataProvider .class );
23062309 }
23072310
23082311 @ Override
@@ -2320,16 +2323,6 @@ ImageHandle getImageMetadata(int zoom) {
23202323 return zoomLevelToImageHandle .get (zoom );
23212324 }
23222325
2323- @ Override
2324- boolean isDisposed () {
2325- return zoomLevelToImageHandle .isEmpty ();
2326- }
2327-
2328- @ Override
2329- Object getProvider () {
2330- return provider ;
2331- }
2332-
23332326 @ Override
23342327 ImageDataProviderWrapper createCopy (Image image ) {
23352328 return image .new ImageDataProviderWrapper (provider );
@@ -2340,7 +2333,6 @@ private class ImageGcDrawerWrapper extends DynamicImageProviderWrapper {
23402333 private ImageGcDrawer drawer ;
23412334 private int width ;
23422335 private int height ;
2343- private boolean isDestroyed ;
23442336
23452337 ImageGcDrawerWrapper (ImageGcDrawer imageGcDrawer , int width , int height ) {
23462338 checkProvider (imageGcDrawer , ImageGcDrawer .class );
@@ -2379,16 +2371,6 @@ ImageHandle getImageMetadata(int zoom) {
23792371 return zoomLevelToImageHandle .get (zoom );
23802372 }
23812373
2382- @ Override
2383- protected void destroy () {
2384- isDestroyed = true ;
2385- }
2386-
2387- @ Override
2388- boolean isDisposed () {
2389- return isDestroyed ;
2390- }
2391-
23922374 @ Override
23932375 Object getProvider () {
23942376 return drawer ;
@@ -2726,5 +2708,9 @@ private ImageData getImageData() {
27262708 }
27272709 }
27282710
2711+ private boolean isDisposed () {
2712+ return this .handle == 0 ;
2713+ }
2714+
27292715}
27302716}
0 commit comments