Skip to content

Commit 9cd2005

Browse files
committed
[win32] refactor Image::init with ImageData
This commit refactors Image#init for ImageData in the win32 implementation. Currently this method serves two purposes. Creating handles for the given ImageData or additionally initializing an image with those handles. This two use cases are made more explicit refactoring this logic out into concrete methods.
1 parent 971b9a6 commit 9cd2005

File tree

2 files changed

+48
-32
lines changed

2 files changed

+48
-32
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Cursor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public Cursor(Device device, ImageData source, int hotspotX, int hotspotY) {
325325
if (hMask == 0) SWT.error(SWT.ERROR_NO_HANDLES);
326326
} else {
327327
ImageData mask = source.getTransparencyMask();
328-
long [] result = Image.init(this.device, null, source, mask, null); // Since the image is null, the device zoom can be null
328+
long [] result = Image.initIcon(this.device, source, mask);
329329
hBitmap = result[0];
330330
hMask = result[1];
331331
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ public Image(Device device, ImageData source, ImageData mask) {
412412
source = DPIUtil.autoScaleUp(device, source);
413413
mask = DPIUtil.autoScaleUp(device, mask);
414414
mask = ImageData.convertMask(mask);
415-
init(this.device, this, source, mask, getZoom());
415+
initIconHandle(this.device, source, mask, getZoom());
416416
init();
417417
this.device.registerResourceWithZoomSupport(this);
418418
}
@@ -781,7 +781,7 @@ private ImageHandle getImageMetadata(int zoom) {
781781
// to image data without transparency mask, this will create invalid images
782782
// so this fallback will "repair" the image data by explicitly passing
783783
// the transparency mask created from the scaled image data
784-
init(this.device, this, newData, newData.getTransparencyMask(), zoom);
784+
initIconHandle(this.device, newData, newData.getTransparencyMask(), zoom);
785785
} else {
786786
init(newData, zoom);
787787
}
@@ -1574,7 +1574,9 @@ static ImageData directToDirect(ImageData src, int newDepth, PaletteData newPale
15741574
return img;
15751575
}
15761576

1577-
static long [] init(Device device, Image image, ImageData i, Integer zoom) {
1577+
private record HandleForImageDataContainer(int type, ImageData imageData, long[] handles) {}
1578+
1579+
private static HandleForImageDataContainer init(Device device, ImageData i) {
15781580
/* Windows does not support 2-bit images. Convert to 4-bit image. */
15791581
if (i.depth == 2) {
15801582
i = indexToIndex(i, 4);
@@ -1740,7 +1742,6 @@ else if (i.alphaData != null) {
17401742
}
17411743
OS.MoveMemory(pBits[0], data, data.length);
17421744

1743-
long [] result = null;
17441745
if (i.getTransparencyType() == SWT.TRANSPARENCY_MASK) {
17451746
/* Get the HDC for the device */
17461747
long hDC = device.internal_new_GC(null);
@@ -1767,31 +1768,10 @@ else if (i.alphaData != null) {
17671768
OS.DeleteDC(hdcDest);
17681769
OS.DeleteObject(hDib);
17691770

1770-
if (image == null) {
1771-
result = new long []{hBitmap, hMask};
1772-
} else {
1773-
/* Create the icon */
1774-
ICONINFO info = new ICONINFO();
1775-
info.fIcon = true;
1776-
info.hbmColor = hBitmap;
1777-
info.hbmMask = hMask;
1778-
long hIcon = OS.CreateIconIndirect(info);
1779-
if (hIcon == 0) SWT.error(SWT.ERROR_NO_HANDLES);
1780-
OS.DeleteObject(hBitmap);
1781-
OS.DeleteObject(hMask);
1782-
image.type = SWT.ICON;
1783-
image.new ImageHandle(hIcon, zoom);
1784-
}
1771+
return new HandleForImageDataContainer(SWT.ICON, i, new long []{hBitmap, hMask});
17851772
} else {
1786-
if (image == null) {
1787-
result = new long []{hDib};
1788-
} else {
1789-
image.type = SWT.BITMAP;
1790-
image.transparentPixel = i.transparentPixel;
1791-
image.new ImageHandle(hDib, zoom);
1792-
}
1773+
return new HandleForImageDataContainer(SWT.BITMAP, i, new long []{hDib});
17931774
}
1794-
return result;
17951775
}
17961776

17971777
private void setImageMetadataForHandle(ImageHandle imageMetadata, Integer zoom) {
@@ -1803,9 +1783,35 @@ private void setImageMetadataForHandle(ImageHandle imageMetadata, Integer zoom)
18031783
zoomLevelToImageHandle.put(zoom, imageMetadata);
18041784
}
18051785

1806-
static long [] init(Device device, Image image, ImageData source, ImageData mask, Integer zoom) {
1786+
private ImageHandle initIconHandle(Device device, ImageData source, ImageData mask, Integer zoom) {
18071787
ImageData imageData = applyMask(source, mask);
1808-
return init(device, image, imageData, zoom);
1788+
HandleForImageDataContainer imageDataHandle = init(device, imageData);
1789+
return initIconHandle(imageDataHandle.handles, zoom);
1790+
}
1791+
1792+
private ImageHandle initIconHandle(long[] handles, int zoom) {
1793+
/* Create the icon */
1794+
ICONINFO info = new ICONINFO();
1795+
info.fIcon = true;
1796+
info.hbmColor = handles[0];
1797+
info.hbmMask = handles[1];
1798+
long hIcon = OS.CreateIconIndirect(info);
1799+
if (hIcon == 0) SWT.error(SWT.ERROR_NO_HANDLES);
1800+
OS.DeleteObject(handles[0]);
1801+
OS.DeleteObject(handles[1]);
1802+
type = SWT.ICON;
1803+
return new ImageHandle(hIcon, zoom);
1804+
}
1805+
1806+
private ImageHandle initBitmapHandle(ImageData imageData, long handle, Integer zoom) {
1807+
type = SWT.BITMAP;
1808+
transparentPixel = imageData.transparentPixel;
1809+
return new ImageHandle(handle, zoom);
1810+
}
1811+
1812+
static long [] initIcon(Device device, ImageData source, ImageData mask) {
1813+
ImageData imageData = applyMask(source, mask);
1814+
return init(device, imageData).handles;
18091815
}
18101816

18111817
private static ImageData applyMask(ImageData source, ImageData mask) {
@@ -1883,9 +1889,19 @@ private static ImageData applyMask(ImageData source, ImageData mask) {
18831889
}
18841890

18851891

1886-
void init(ImageData i, Integer zoom) {
1892+
void init(ImageData i, int zoom) {
18871893
if (i == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
1888-
init(device, this, i, zoom);
1894+
HandleForImageDataContainer imageDataHandle = init(device, i);
1895+
switch (imageDataHandle.type()) {
1896+
case SWT.ICON: {
1897+
initIconHandle(imageDataHandle.handles(), zoom);
1898+
break;
1899+
}
1900+
case SWT.BITMAP: {
1901+
initBitmapHandle(imageDataHandle.imageData(), imageDataHandle.handles()[0], zoom);
1902+
break;
1903+
}
1904+
}
18891905
}
18901906

18911907
/**

0 commit comments

Comments
 (0)