Skip to content

Commit c6ec34e

Browse files
committed
[Win32] Avoid duplicate application of gray/disabled image adaptation
When an image is created with SWT.IMAGE_GRAY or SWT.IMAGE_DISABLE, an adaptation of the loaded/created image data is performed to make it look grayed/disabled. The according functionality is currently applied by callers of loadImageData() right afterwards. Since for every consumption of an image data/handle the loadImageData() method needs to be called, this ensures that the image data is always adapted correctly. In the implementation for an ImageGcDrawer, the adaptation for gray/disabled is currently executed within the loadImageData() method. The same applies to implementation for an existing handle and a plain image, as they will simply scale existing image data (which already have the gray/disabled adaptation applied). Since consumers of that method perform the adaptation as well, this leads to an unnecessary duplicate application of the adaptation and to inconsistent behavior throughout the implementations of loadImageData(). This change streamlines the implementation in the Image class by properly assigning the responsibility for applying gray/disabled adaptations as follows: - the copy constructor accepting the gray/disabled flag applies the style to copied handles, just like it does now - on retrieval of new image data or handles for zoom/sizes, the loadImageData() and loadImageDataAtExactSize() method of the provider classes are responsible for delivering data with the styling being applied This relieves all consumers of those methods from taking care of applying the style and properly assign it to those two methods and the implementation.
1 parent f6c5753 commit c6ec34e

File tree

1 file changed

+31
-15
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+31
-15
lines changed

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ private boolean isReusable(int width, int height) {
238238
private Optional<InternalImageHandle> createHandleAtExactSize(int width, int height) {
239239
Optional<ImageData> imageData = imageProvider.loadImageDataAtExactSize(width, height);
240240
if (imageData.isPresent()) {
241-
ImageData adaptedData = adaptImageDataIfDisabledOrGray(imageData.get());
242-
temporaryHandleContainer = init(adaptedData, -1);
241+
temporaryHandleContainer = init(imageData.get(), -1);
243242
return Optional.of(temporaryHandleContainer);
244243
}
245244
return Optional.empty();
@@ -253,8 +252,7 @@ private InternalImageHandle getOrCreateImageHandleAtClosestSize(int widthHint, i
253252
InternalImageHandle bestFittingHandle = imageHandleManager.get(imageZoom);
254253
if (bestFittingHandle == null) {
255254
ImageData bestFittingImageData = imageProvider.loadImageData(imageZoom).element();
256-
ImageData adaptedData = adaptImageDataIfDisabledOrGray(bestFittingImageData);
257-
bestFittingHandle = temporaryHandleContainer = init(adaptedData, -1);
255+
bestFittingHandle = temporaryHandleContainer = init(bestFittingImageData, -1);
258256
}
259257
return bestFittingHandle;
260258
}
@@ -771,6 +769,10 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
771769
init();
772770
}
773771

772+
private ElementAtZoom<ImageData> adaptImageDataIfDisabledOrGray(ElementAtZoom<ImageData> dataAtZoom) {
773+
return new ElementAtZoom<>(adaptImageDataIfDisabledOrGray(dataAtZoom.element()), dataAtZoom.zoom());
774+
}
775+
774776
private ImageData adaptImageDataIfDisabledOrGray(ImageData data) {
775777
ImageData returnImageData = null;
776778
switch (this.styleFlag) {
@@ -2058,6 +2060,10 @@ protected boolean isPersistentImageHandleRequriedForImageData() {
20582060
return false;
20592061
}
20602062

2063+
/**
2064+
* Returns image data at the best-fitting available zoom for the given zoom.
2065+
* The returned data will have a potential gray/disable style applied.
2066+
*/
20612067
protected abstract ElementAtZoom<ImageData> loadImageData(int zoom);
20622068

20632069
abstract ImageData newImageData(int zoom);
@@ -2071,6 +2077,10 @@ ElementAtZoom<ImageData> getClosestAvailableImageData(int zoom) {
20712077
return new ElementAtZoom<>(imageData, closestZoom);
20722078
}
20732079

2080+
/**
2081+
* Returns image data at the exact requested size if available.
2082+
* The returned data will have a potential gray/disable style applied.
2083+
*/
20742084
protected Optional<ImageData> loadImageDataAtExactSize(int width, int height) {
20752085
return Optional.empty(); // exact size not available
20762086
}
@@ -2174,7 +2184,6 @@ protected DestroyableImageHandle newImageHandle(ZoomContext zoomContext) {
21742184
private DestroyableImageHandle initializeHandleFromSource(ZoomContext zoomContext) {
21752185
ElementAtZoom<ImageData> imageDataAtZoom = loadImageData(zoomContext.targetZoom());
21762186
ImageData imageData = DPIUtil.scaleImageData(device, imageDataAtZoom.element(), zoomContext.targetZoom(), imageDataAtZoom.zoom());
2177-
imageData = adaptImageDataIfDisabledOrGray(imageData);
21782187
return newImageHandle(imageData, zoomContext);
21792188
}
21802189
}
@@ -2202,7 +2211,8 @@ protected Rectangle getBounds(int zoom) {
22022211

22032212
@Override
22042213
protected ElementAtZoom<ImageData> loadImageData(int zoom) {
2205-
return new ElementAtZoom<>(imageDataAtBaseZoom, baseZoom);
2214+
ImageData adaptedImageData = adaptImageDataIfDisabledOrGray(imageDataAtBaseZoom);
2215+
return new ElementAtZoom<>(adaptedImageData, baseZoom);
22062216
}
22072217

22082218
@Override
@@ -2233,7 +2243,8 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
22332243
ImageData scaledMask = DPIUtil.scaleImageData(device, maskAt100, zoom, 100);
22342244
scaledMask = ImageData.convertMask(scaledMask);
22352245
ImageData mergedData = applyMask(scaledSource, scaledMask);
2236-
return new ElementAtZoom<>(mergedData, zoom);
2246+
ImageData adaptedData = adaptImageDataIfDisabledOrGray(mergedData);
2247+
return new ElementAtZoom<>(adaptedData, zoom);
22372248
}
22382249

22392250
@Override
@@ -2260,7 +2271,8 @@ private ImageDataLoaderStreamProviderWrapper(byte[] inputStreamData) {
22602271

22612272
@Override
22622273
protected ElementAtZoom<ImageData> loadImageData(int zoom) {
2263-
return ImageDataLoader.loadByZoom(new ByteArrayInputStream(inputStreamData), FileFormat.DEFAULT_ZOOM, zoom);
2274+
ElementAtZoom<ImageData> imageDataAtZoom = ImageDataLoader.loadByZoom(new ByteArrayInputStream(inputStreamData), FileFormat.DEFAULT_ZOOM, zoom);
2275+
return adaptImageDataIfDisabledOrGray(imageDataAtZoom);
22642276
}
22652277

22662278
@Override
@@ -2278,7 +2290,8 @@ AbstractImageProviderWrapper createCopy(Image image) {
22782290
protected Optional<ImageData> loadImageDataAtExactSize(int targetWidth, int targetHeight) {
22792291
if (ImageDataLoader.isDynamicallySizable(new ByteArrayInputStream(this.inputStreamData))) {
22802292
ImageData imageDataAtSize = ImageDataLoader.loadBySize(new ByteArrayInputStream(this.inputStreamData), targetWidth, targetHeight);
2281-
return Optional.of(imageDataAtSize);
2293+
ImageData adaptedImageDataAtSize = adaptImageDataIfDisabledOrGray(imageDataAtSize);
2294+
return Optional.of(adaptedImageDataAtSize);
22822295
}
22832296
return Optional.empty();
22842297
}
@@ -2470,7 +2483,6 @@ protected DestroyableImageHandle newImageHandle(ZoomContext zoomContext) {
24702483
private DestroyableImageHandle initializeHandleFromSource(int zoom) {
24712484
ElementAtZoom<ImageData> imageDataAtZoom = loadImageData(zoom);
24722485
ImageData imageData = DPIUtil.scaleImageData (device, imageDataAtZoom.element(), zoom, imageDataAtZoom.zoom());
2473-
imageData = adaptImageDataIfDisabledOrGray(imageData);
24742486
return init(imageData, zoom);
24752487
}
24762488

@@ -2497,7 +2509,8 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
24972509
// Load at appropriate zoom via loader
24982510
if (fileForZoom.zoom() != zoom && ImageDataLoader.canLoadAtZoom(fileForZoom.element(), fileForZoom.zoom(), zoom)) {
24992511
ElementAtZoom<ImageData> imageDataAtZoom = ImageDataLoader.loadByZoom(fileForZoom.element(), fileForZoom.zoom(), zoom);
2500-
return new ElementAtZoom<>(imageDataAtZoom.element(), zoom);
2512+
ImageData adaptedImageData = adaptImageDataIfDisabledOrGray(imageDataAtZoom.element());
2513+
return new ElementAtZoom<>(adaptedImageData, zoom);
25012514
}
25022515

25032516
// Load at file zoom (native or via loader) and rescale
@@ -2517,7 +2530,7 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) {
25172530
temporaryImageHandle.destroy();
25182531
}
25192532
}
2520-
return imageDataAtZoom;
2533+
return adaptImageDataIfDisabledOrGray(imageDataAtZoom);
25212534
}
25222535

25232536
@Override
@@ -2727,7 +2740,8 @@ protected Optional<ImageData> loadImageDataAtExactSize(int targetWidth, int targ
27272740
String fileName = DPIUtil.validateAndGetImagePathAtZoom(this.provider, 100).element();
27282741
if (ImageDataLoader.isDynamicallySizable(fileName)) {
27292742
ImageData imageDataAtSize = ImageDataLoader.loadBySize(fileName, targetWidth, targetHeight);
2730-
return Optional.of(imageDataAtSize);
2743+
ImageData adaptedImageDataAtSize = adaptImageDataIfDisabledOrGray(imageDataAtSize);
2744+
return Optional.of(adaptedImageDataAtSize);
27312745
}
27322746
return Optional.empty();
27332747
}
@@ -2740,7 +2754,8 @@ private class ImageDataProviderWrapper extends BaseImageProviderWrapper<ImageDat
27402754

27412755
@Override
27422756
protected ElementAtZoom<ImageData> loadImageData(int zoom) {
2743-
return DPIUtil.validateAndGetImageDataAtZoom (provider, zoom);
2757+
ElementAtZoom<ImageData> imageDataAtZoom = DPIUtil.validateAndGetImageDataAtZoom (provider, zoom);
2758+
return adaptImageDataIfDisabledOrGray(imageDataAtZoom);
27442759
}
27452760

27462761
@Override
@@ -2756,7 +2771,8 @@ protected Optional<ImageData> loadImageDataAtExactSize(int targetWidth, int targ
27562771
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null,
27572772
" ImageDataAtSizeProvider returned null for width=" + targetWidth + ", height=" + targetHeight);
27582773
}
2759-
return Optional.of(imageData);
2774+
ImageData adaptedImageData = adaptImageDataIfDisabledOrGray(imageData);
2775+
return Optional.of(adaptedImageData);
27602776
}
27612777
return Optional.empty();
27622778
}

0 commit comments

Comments
 (0)