@@ -408,7 +408,7 @@ public Image(Device device, ImageData source, ImageData mask) {
408408 source = DPIUtil .autoScaleUp (device , source );
409409 mask = DPIUtil .autoScaleUp (device , mask );
410410 mask = ImageData .convertMask (mask );
411- init (this .device , this , source , mask , getZoom ());
411+ initIconHandle (this .device , source , mask , getZoom ());
412412 init ();
413413 this .device .registerResourceWithZoomSupport (this );
414414}
@@ -777,7 +777,7 @@ private ImageHandle getImageMetadata(int zoom) {
777777 // to image data without transparency mask, this will create invalid images
778778 // so this fallback will "repair" the image data by explicitly passing
779779 // the transparency mask created from the scaled image data
780- init (this .device , this , newData , newData .getTransparencyMask (), zoom );
780+ initIconHandle (this .device , newData , newData .getTransparencyMask (), zoom );
781781 } else {
782782 init (newData , zoom );
783783 }
@@ -1542,7 +1542,9 @@ static ImageData directToDirect(ImageData src, int newDepth, PaletteData newPale
15421542 return img ;
15431543}
15441544
1545- static long [] init (Device device , Image image , ImageData i , Integer zoom ) {
1545+ private record HandleForImageDataContainer (int type , ImageData imageData , long [] handles ) {}
1546+
1547+ private static HandleForImageDataContainer init (Device device , ImageData i ) {
15461548 /* Windows does not support 2-bit images. Convert to 4-bit image. */
15471549 if (i .depth == 2 ) {
15481550 i = indexToIndex (i , 4 );
@@ -1708,7 +1710,6 @@ else if (i.alphaData != null) {
17081710 }
17091711 OS .MoveMemory (pBits [0 ], data , data .length );
17101712
1711- long [] result = null ;
17121713 if (i .getTransparencyType () == SWT .TRANSPARENCY_MASK ) {
17131714 /* Get the HDC for the device */
17141715 long hDC = device .internal_new_GC (null );
@@ -1735,31 +1736,10 @@ else if (i.alphaData != null) {
17351736 OS .DeleteDC (hdcDest );
17361737 OS .DeleteObject (hDib );
17371738
1738- if (image == null ) {
1739- result = new long []{hBitmap , hMask };
1740- } else {
1741- /* Create the icon */
1742- ICONINFO info = new ICONINFO ();
1743- info .fIcon = true ;
1744- info .hbmColor = hBitmap ;
1745- info .hbmMask = hMask ;
1746- long hIcon = OS .CreateIconIndirect (info );
1747- if (hIcon == 0 ) SWT .error (SWT .ERROR_NO_HANDLES );
1748- OS .DeleteObject (hBitmap );
1749- OS .DeleteObject (hMask );
1750- image .type = SWT .ICON ;
1751- image .new ImageHandle (hIcon , zoom );
1752- }
1739+ return new HandleForImageDataContainer (SWT .ICON , i , new long []{hBitmap , hMask });
17531740 } else {
1754- if (image == null ) {
1755- result = new long []{hDib };
1756- } else {
1757- image .type = SWT .BITMAP ;
1758- image .transparentPixel = i .transparentPixel ;
1759- image .new ImageHandle (hDib , zoom );
1760- }
1741+ return new HandleForImageDataContainer (SWT .BITMAP , i , new long []{hDib });
17611742 }
1762- return result ;
17631743}
17641744
17651745private void setImageMetadataForHandle (ImageHandle imageMetadata , Integer zoom ) {
@@ -1771,9 +1751,35 @@ private void setImageMetadataForHandle(ImageHandle imageMetadata, Integer zoom)
17711751 zoomLevelToImageHandle .put (zoom , imageMetadata );
17721752}
17731753
1774- static long [] init (Device device , Image image , ImageData source , ImageData mask , Integer zoom ) {
1754+ private ImageHandle initIconHandle (Device device , ImageData source , ImageData mask , Integer zoom ) {
1755+ ImageData imageData = applyMask (source , mask );
1756+ HandleForImageDataContainer imageDataHandle = init (device , imageData );
1757+ return initIconHandle (imageDataHandle .handles , zoom );
1758+ }
1759+
1760+ private ImageHandle initIconHandle (long [] handles , int zoom ) {
1761+ /* Create the icon */
1762+ ICONINFO info = new ICONINFO ();
1763+ info .fIcon = true ;
1764+ info .hbmColor = handles [0 ];
1765+ info .hbmMask = handles [1 ];
1766+ long hIcon = OS .CreateIconIndirect (info );
1767+ if (hIcon == 0 ) SWT .error (SWT .ERROR_NO_HANDLES );
1768+ OS .DeleteObject (handles [0 ]);
1769+ OS .DeleteObject (handles [1 ]);
1770+ type = SWT .ICON ;
1771+ return new ImageHandle (hIcon , zoom );
1772+ }
1773+
1774+ private ImageHandle initBitmapHandle (ImageData imageData , long handle , Integer zoom ) {
1775+ type = SWT .BITMAP ;
1776+ transparentPixel = imageData .transparentPixel ;
1777+ return new ImageHandle (handle , zoom );
1778+ }
1779+
1780+ static long [] initIcon (Device device , ImageData source , ImageData mask ) {
17751781 ImageData imageData = applyMask (source , mask );
1776- return init (device , image , imageData , zoom ) ;
1782+ return init (device , imageData ). handles ;
17771783}
17781784
17791785private static ImageData applyMask (ImageData source , ImageData mask ) {
@@ -1851,9 +1857,20 @@ private static ImageData applyMask(ImageData source, ImageData mask) {
18511857}
18521858
18531859
1854- void init (ImageData i , Integer zoom ) {
1860+ private ImageHandle init (ImageData i , int zoom ) {
18551861 if (i == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
1856- init (device , this , i , zoom );
1862+ HandleForImageDataContainer imageDataHandle = init (device , i );
1863+ switch (imageDataHandle .type ()) {
1864+ case SWT .ICON : {
1865+ return initIconHandle (imageDataHandle .handles (), zoom );
1866+ }
1867+ case SWT .BITMAP : {
1868+ return initBitmapHandle (imageDataHandle .imageData (), imageDataHandle .handles ()[0 ], zoom );
1869+ }
1870+ default :
1871+ SWT .error (SWT .ERROR_INVALID_ARGUMENT );
1872+ return null ;
1873+ }
18571874}
18581875
18591876/**
0 commit comments