@@ -126,11 +126,6 @@ public final class Image extends Resource implements Drawable {
126126 */
127127 private RGB backgroundColor ;
128128
129- /**
130- * Attribute to cache current native zoom level
131- */
132- private int initialNativeZoom = 100 ;
133-
134129 /**
135130 * specifies the default scanline padding
136131 */
@@ -140,7 +135,6 @@ public final class Image extends Resource implements Drawable {
140135
141136private Image (Device device , int type , long handle , int nativeZoom ) {
142137 super (device );
143- initialNativeZoom = nativeZoom ;
144138 this .type = type ;
145139 this .imageProvider = new ExistingImageHandleProviderWrapper (handle , nativeZoom );
146140 this .isInitialized = true ;
@@ -183,13 +177,7 @@ private Image (Device device, int type, long handle, int nativeZoom) {
183177 * @see #dispose()
184178 */
185179public Image (Device device , int width , int height ) {
186- this (device , width , height , DPIUtil .getNativeDeviceZoom ());
187- }
188-
189-
190- private Image (Device device , int width , int height , int nativeZoom ) {
191180 super (device );
192- this .initialNativeZoom = nativeZoom ;
193181 this .imageProvider = new PlainImageProviderWrapper (width , height );
194182 init ();
195183 this .device .registerResourceWithZoomSupport (this );
@@ -237,58 +225,70 @@ public Image(Device device, Image srcImage, int flag) {
237225 ImageHandle imageMetadata ;
238226 if (srcImage == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
239227 if (srcImage .isDisposed ()) SWT .error (SWT .ERROR_INVALID_ARGUMENT );
240- initialNativeZoom = srcImage .initialNativeZoom ;
241- Rectangle rect = srcImage .getBounds (getZoom ());
242228 this .type = srcImage .type ;
243229 this .imageProvider = srcImage .imageProvider .createCopy (this );
244230 this .styleFlag = srcImage .styleFlag | flag ;
245- long srcImageHandle = win32_getHandle (srcImage , getZoom ());
231+ ImageHandle imageHandle = srcImage .zoomLevelToImageHandle .isEmpty () ?
232+ null : srcImage .zoomLevelToImageHandle .values ().iterator ().next ();
246233 switch (flag ) {
247234 case SWT .IMAGE_COPY : {
248235 switch (type ) {
249236 case SWT .BITMAP :
250- /* Get the HDC for the device */
251- long hDC = device .internal_new_GC (null );
252-
253- /* Copy the bitmap */
254- long hdcSource = OS .CreateCompatibleDC (hDC );
255- long hdcDest = OS .CreateCompatibleDC (hDC );
256- long hOldSrc = OS .SelectObject (hdcSource , srcImageHandle );
257- BITMAP bm = new BITMAP ();
258- OS .GetObject (srcImageHandle , BITMAP .sizeof , bm );
259- imageMetadata = new ImageHandle (OS .CreateCompatibleBitmap (hdcSource , rect .width , bm .bmBits != 0 ? -rect .height : rect .height ), getZoom ());
260- if (imageMetadata .handle == 0 ) SWT .error (SWT .ERROR_NO_HANDLES );
261- long hOldDest = OS .SelectObject (hdcDest , imageMetadata .handle );
262- OS .BitBlt (hdcDest , 0 , 0 , rect .width , rect .height , hdcSource , 0 , 0 , OS .SRCCOPY );
263- OS .SelectObject (hdcSource , hOldSrc );
264- OS .SelectObject (hdcDest , hOldDest );
265- OS .DeleteDC (hdcSource );
266- OS .DeleteDC (hdcDest );
267-
268- /* Release the HDC for the device */
269- device .internal_dispose_GC (hDC , null );
270237
238+ if (imageHandle != null ) {
239+ Rectangle rect = imageHandle .getBounds ();
240+ long srcImageHandle = imageHandle .handle ;
241+ /* Get the HDC for the device */
242+ long hDC = device .internal_new_GC (null );
243+
244+ /* Copy the bitmap */
245+ long hdcSource = OS .CreateCompatibleDC (hDC );
246+ long hdcDest = OS .CreateCompatibleDC (hDC );
247+ long hOldSrc = OS .SelectObject (hdcSource , srcImageHandle );
248+ BITMAP bm = new BITMAP ();
249+ OS .GetObject (srcImageHandle , BITMAP .sizeof , bm );
250+ imageMetadata = new ImageHandle (OS .CreateCompatibleBitmap (hdcSource , rect .width , bm .bmBits != 0 ? -rect .height : rect .height ), imageHandle .zoom );
251+ if (imageMetadata .handle == 0 ) SWT .error (SWT .ERROR_NO_HANDLES );
252+ long hOldDest = OS .SelectObject (hdcDest , imageMetadata .handle );
253+ OS .BitBlt (hdcDest , 0 , 0 , rect .width , rect .height , hdcSource , 0 , 0 , OS .SRCCOPY );
254+ OS .SelectObject (hdcSource , hOldSrc );
255+ OS .SelectObject (hdcDest , hOldDest );
256+ OS .DeleteDC (hdcSource );
257+ OS .DeleteDC (hdcDest );
258+
259+ /* Release the HDC for the device */
260+ device .internal_dispose_GC (hDC , null );
261+ }
271262 transparentPixel = srcImage .transparentPixel ;
272263 break ;
273264 case SWT .ICON :
274- imageMetadata = new ImageHandle (OS .CopyImage (srcImageHandle , OS .IMAGE_ICON , rect .width , rect .height , 0 ), getZoom ());
275- if (imageMetadata .handle == 0 ) SWT .error (SWT .ERROR_NO_HANDLES );
265+ if (imageHandle != null ) {
266+ Rectangle rect = imageHandle .getBounds ();
267+ imageMetadata = new ImageHandle (OS .CopyImage (imageHandle .handle , OS .IMAGE_ICON , rect .width , rect .height , 0 ), imageHandle .zoom );
268+ if (imageMetadata .handle == 0 ) SWT .error (SWT .ERROR_NO_HANDLES );
269+ }
276270 break ;
277271 default :
278272 SWT .error (SWT .ERROR_INVALID_IMAGE );
279273 }
280274 break ;
281275 }
282276 case SWT .IMAGE_DISABLE : {
283- ImageData data = srcImage .getImageData (srcImage .getZoom ());
284- ImageData newData = applyDisableImageData (data , rect .height , rect .width );
285- init (newData , getZoom ());
277+ if (imageHandle != null ) {
278+ Rectangle rect = imageHandle .getBounds ();
279+ ImageData data = srcImage .getImageData (imageHandle .zoom );
280+ ImageData newData = applyDisableImageData (data , rect .height , rect .width );
281+ init (newData , imageHandle .zoom );
282+ }
286283 break ;
287284 }
288285 case SWT .IMAGE_GRAY : {
289- ImageData data = srcImage .getImageData (srcImage .getZoom ());
290- ImageData newData = applyGrayImageData (data , rect .height , rect .width );
291- init (newData , getZoom ());
286+ if (imageHandle != null ) {
287+ Rectangle rect = imageHandle .getBounds ();
288+ ImageData data = srcImage .getImageData (imageHandle .zoom );
289+ ImageData newData = applyGrayImageData (data , rect .height , rect .width );
290+ init (newData , imageHandle .zoom );
291+ }
292292 break ;
293293 }
294294 default :
@@ -336,7 +336,6 @@ public Image(Device device, Image srcImage, int flag) {
336336public Image (Device device , Rectangle bounds ) {
337337 super (device );
338338 if (bounds == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
339- initialNativeZoom = DPIUtil .getNativeDeviceZoom ();
340339 this .imageProvider = new PlainImageProviderWrapper (bounds .width , bounds .height );
341340 init ();
342341 this .device .registerResourceWithZoomSupport (this );
@@ -368,7 +367,6 @@ public Image(Device device, Rectangle bounds) {
368367public Image (Device device , ImageData data ) {
369368 super (device );
370369 if (data == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
371- initialNativeZoom = DPIUtil .getNativeDeviceZoom ();
372370 this .imageProvider = new PlainImageDataProviderWrapper (data );
373371 init ();
374372 this .device .registerResourceWithZoomSupport (this );
@@ -411,7 +409,6 @@ public Image(Device device, ImageData source, ImageData mask) {
411409 if (source .width != mask .width || source .height != mask .height ) {
412410 SWT .error (SWT .ERROR_INVALID_ARGUMENT );
413411 }
414- initialNativeZoom = DPIUtil .getNativeDeviceZoom ();
415412 this .imageProvider = new MaskedImageDataProviderWrapper (source , mask );
416413 init ();
417414 this .device .registerResourceWithZoomSupport (this );
@@ -473,7 +470,6 @@ public Image(Device device, ImageData source, ImageData mask) {
473470public Image (Device device , InputStream stream ) {
474471 super (device );
475472 if (stream == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
476- initialNativeZoom = DPIUtil .getNativeDeviceZoom ();
477473 this .imageProvider = new ImageDataLoaderStreamProviderWrapper (stream );
478474 init ();
479475 this .device .registerResourceWithZoomSupport (this );
@@ -514,7 +510,6 @@ public Image (Device device, InputStream stream) {
514510public Image (Device device , String filename ) {
515511 super (device );
516512 if (filename == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
517- initialNativeZoom = DPIUtil .getNativeDeviceZoom ();
518513 this .imageProvider = new ImageFileNameProviderWrapper (zoom -> {
519514 if (zoom == 100 ) {
520515 return filename ;
@@ -561,7 +556,6 @@ public Image (Device device, String filename) {
561556public Image (Device device , ImageFileNameProvider imageFileNameProvider ) {
562557 super (device );
563558 this .imageProvider = new ImageFileNameProviderWrapper (imageFileNameProvider );
564- initialNativeZoom = DPIUtil .getNativeDeviceZoom ();
565559 if (imageProvider .getImageData (100 ) == null ) {
566560 SWT .error (SWT .ERROR_INVALID_ARGUMENT , null ,
567561 ": ImageFileNameProvider [" + imageFileNameProvider + "] returns null ImageData at 100% zoom." );
@@ -602,7 +596,6 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
602596public Image (Device device , ImageDataProvider imageDataProvider ) {
603597 super (device );
604598 this .imageProvider = new ImageDataProviderWrapper (imageDataProvider );
605- initialNativeZoom = DPIUtil .getNativeDeviceZoom ();
606599 if (imageDataProvider .getImageData (100 ) == null ) {
607600 SWT .error (SWT .ERROR_INVALID_ARGUMENT , null ,
608601 ": ImageDataProvider [" + imageDataProvider + "] returns null ImageData at 100% zoom." );
@@ -632,7 +625,6 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
632625public Image (Device device , ImageGcDrawer imageGcDrawer , int width , int height ) {
633626 super (device );
634627 this .imageProvider = new ImageGcDrawerWrapper (imageGcDrawer , width , height );
635- initialNativeZoom = DPIUtil .getNativeDeviceZoom ();
636628 init ();
637629}
638630
@@ -827,10 +819,6 @@ public static void drawScaled(GC gc, Image original, int width, int height, floa
827819 0 , 0 , Math .round (width * scaleFactor ), Math .round (height * scaleFactor ), false );
828820}
829821
830- long [] createGdipImage () {
831- return createGdipImage (this .getZoom ());
832- }
833-
834822long [] createGdipImage (Integer zoom ) {
835823 long handle = Image .win32_getHandle (this , zoom );
836824 switch (type ) {
@@ -1019,7 +1007,7 @@ private void destroyHandle () {
10191007void destroyHandlesExcept (Set <Integer > zoomLevels ) {
10201008 zoomLevelToImageHandle .entrySet ().removeIf (entry -> {
10211009 final Integer zoom = entry .getKey ();
1022- if (!zoomLevels .contains (zoom ) && zoom != DPIUtil . getZoomForAutoscaleProperty ( initialNativeZoom ) ) {
1010+ if (!zoomLevels .contains (zoom )) {
10231011 destroyHandle (entry .getValue ().handle );
10241012 return true ;
10251013 }
@@ -1056,7 +1044,7 @@ public boolean equals (Object object) {
10561044 if (object == this ) return true ;
10571045 if (!(object instanceof Image )) return false ;
10581046 Image image = (Image ) object ;
1059- if (device != image .device || transparentPixel != image .transparentPixel || getZoom () != image . getZoom () ) return false ;
1047+ if (device != image .device || transparentPixel != image .transparentPixel ) return false ;
10601048 return (styleFlag == image .styleFlag ) && imageProvider .equals (image .imageProvider );
10611049}
10621050
@@ -1088,7 +1076,7 @@ public Color getBackground() {
10881076
10891077 /* Get the HDC for the device */
10901078 long hDC = device .internal_new_GC (null );
1091- long handle = win32_getHandle (this , getZoom () );
1079+ long handle = win32_getHandle (this , 100 );
10921080
10931081 /* Compute the background color */
10941082 BITMAP bm = new BITMAP ();
@@ -1175,7 +1163,7 @@ Rectangle getBounds(int zoom) {
11751163 */
11761164@ Deprecated
11771165public Rectangle getBoundsInPixels () {
1178- return getBounds (getZoom () );
1166+ return getBounds (100 );
11791167}
11801168
11811169/**
@@ -1254,7 +1242,7 @@ public ImageData getImageData (int zoom) {
12541242 */
12551243@ Deprecated
12561244public ImageData getImageDataAtCurrentZoom () {
1257- return getImageMetadata (getZoom () ).getImageData ();
1245+ return getImageMetadata (100 ).getImageData ();
12581246}
12591247
12601248/**
@@ -1703,6 +1691,10 @@ private ImageHandle init(ImageData i, int zoom) {
17031691 */
17041692@ Override
17051693public long internal_new_GC (GCData data ) {
1694+ return configureGC (data , 100 );
1695+ }
1696+
1697+ private long configureGC (GCData data , int zoom ) {
17061698 if (isDisposed ()) SWT .error (SWT .ERROR_GRAPHIC_DISPOSED );
17071699 /*
17081700 * Create a new GC that can draw into the image.
@@ -1727,9 +1719,9 @@ public long internal_new_GC (GCData data) {
17271719 data .style |= SWT .LEFT_TO_RIGHT ;
17281720 }
17291721 data .device = device ;
1730- data .nativeZoom = initialNativeZoom ;
1722+ data .nativeZoom = zoom ;
17311723 data .image = this ;
1732- data .font = SWTFontProvider .getSystemFont (device , initialNativeZoom );
1724+ data .font = SWTFontProvider .getSystemFont (device , zoom );
17331725 }
17341726 return imageDC ;
17351727}
@@ -1813,9 +1805,6 @@ public void setBackground(Color color) {
18131805 zoomLevelToImageHandle .values ().forEach (imageHandle -> imageHandle .setBackground (backgroundColor ));
18141806}
18151807
1816- private int getZoom () {
1817- return DPIUtil .getZoomForAutoscaleProperty (initialNativeZoom );
1818- }
18191808/**
18201809 * Returns a string containing a concise, human-readable
18211810 * description of the receiver.
@@ -2252,7 +2241,7 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
22522241
22532242 @ Override
22542243 public int hashCode () {
2255- return Objects .hash (provider , styleFlag , transparentPixel , getZoom () );
2244+ return Objects .hash (provider , styleFlag , transparentPixel );
22562245 }
22572246
22582247 @ Override
@@ -2495,9 +2484,8 @@ ImageData newImageData(int zoom) {
24952484
24962485 @ Override
24972486 protected ImageHandle newImageHandle (int zoom ) {
2498- initialNativeZoom = zoom ;
2499- Image image = new Image (device , width , height , zoom );
2500- GC gc = new GC (image , drawer .getGcStyle ());
2487+ Image image = new Image (device , width , height );
2488+ GC gc = new GC (new DrawableWrapper (image , zoom ), drawer .getGcStyle ());
25012489 try {
25022490 gc .data .nativeZoom = zoom ;
25032491 drawer .drawOn (gc , width , height );
@@ -2511,6 +2499,27 @@ protected ImageHandle newImageHandle(int zoom) {
25112499 }
25122500 }
25132501
2502+ private class DrawableWrapper implements Drawable {
2503+ private final Image image ;
2504+ private final int zoom ;
2505+
2506+ public DrawableWrapper (Image image , int zoom ) {
2507+ this .image = image ;
2508+ this .zoom = zoom ;
2509+ }
2510+
2511+ @ Override
2512+ public long internal_new_GC (GCData data ) {
2513+ long imageDC = this .image .configureGC (data , zoom );
2514+ return imageDC ;
2515+ }
2516+
2517+ @ Override
2518+ public void internal_dispose_GC (long handle , GCData data ) {
2519+ this .image .internal_dispose_GC (handle , data );
2520+ }
2521+ }
2522+
25142523 @ Override
25152524 Object getProvider () {
25162525 return drawer ;
@@ -2549,6 +2558,10 @@ public ImageHandle(long handle, int zoom) {
25492558 setImageMetadataForHandle (this , zoom );
25502559 }
25512560
2561+ public Rectangle getBounds () {
2562+ return new Rectangle (0 , 0 , width , height );
2563+ }
2564+
25522565 private void setBackground (RGB color ) {
25532566 if (transparentPixel == -1 ) return ;
25542567
0 commit comments