Skip to content

Commit e315648

Browse files
committed
WIP
1 parent 5866dad commit e315648

File tree

6 files changed

+141
-100
lines changed

6 files changed

+141
-100
lines changed

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

+33-1
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,39 @@ public ImageData getImageData(int zoom) {
14081408
} finally {
14091409
if (pool != null) pool.release();
14101410
}
1411-
return DPIUtil.scaleImageData (device, getImageData(100), zoom, 100);
1411+
imageData imageData = getImageData(100)
1412+
return scaledTo (imageData, zoom, 100, DPIUtil.getScalingType(imageData));
1413+
}
1414+
1415+
private ImageData scaledTo(ImageData imageData, int targetZoom, int currentZoom, int scaleType) {
1416+
if (imageData == null || currentZoom == targetZoom || !device.isAutoScalable()) {
1417+
return imageData;
1418+
}
1419+
float scaleFactor = (float) targetZoom / (float) currentZoom;
1420+
int scaledWidth = Math.round (imageData.width * scaleFactor);
1421+
int scaledHeight = Math.round (imageData.height * scaleFactor);
1422+
switch (scaleType) {
1423+
case SWT.SMOOTH:
1424+
return scaleUsingSmoothScaling(imageData, scaledWidth, scaledHeight);
1425+
default:
1426+
return imageData.scaledTo(scaledWidth, scaledHeight);
1427+
}
1428+
}
1429+
1430+
private ImageData scaleUsingSmoothScaling(ImageData imageData, int width, int height) {
1431+
Image original = new Image (device, (ImageDataProvider) zoom -> imageData);
1432+
/* Create a 24 bit image data with alpha channel */
1433+
final ImageData resultData = new ImageData (width, height, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
1434+
resultData.alphaData = new byte [width * height];
1435+
Image resultImage = new Image (device, (ImageDataProvider) zoom -> resultData);
1436+
GC gc = new GC (resultImage);
1437+
gc.setAntialias (SWT.ON);
1438+
gc.drawImage (original, 0, 0, imageData.width, imageData.height, 0, 0, width, height, false);
1439+
gc.dispose ();
1440+
original.dispose ();
1441+
ImageData result = resultImage.getImageData (DPIUtil.getDeviceZoom());
1442+
resultImage.dispose ();
1443+
return result;
14121444
}
14131445

14141446
/** Returns the best available representation. May be 100% or 200% iff there is an image provider. */

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java

-21
Original file line numberDiff line numberDiff line change
@@ -1114,27 +1114,6 @@ int getByteOrder() {
11141114
return depth != 16 ? MSB_FIRST : LSB_FIRST;
11151115
}
11161116

1117-
/**
1118-
* Returns the scaled ImageData using smooth scaling.
1119-
*
1120-
* @since 3.130
1121-
*/
1122-
public ImageData scaleToUsingSmoothScaling(Device device, int width, int height) {
1123-
Image original = new Image (device, (ImageDataProvider) zoom -> this);
1124-
/* Create a 24 bit image data with alpha channel */
1125-
final ImageData resultData = new ImageData (width, height, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
1126-
resultData.alphaData = new byte [width * height];
1127-
Image resultImage = new Image (device, (ImageDataProvider) zoom -> resultData);
1128-
GC gc = new GC (resultImage);
1129-
gc.setAntialias (SWT.ON);
1130-
gc.drawImage (original, 0, 0, this.width, this.height, 0, 0, width, height, false);
1131-
gc.dispose ();
1132-
original.dispose ();
1133-
ImageData result = resultImage.getImageData (DPIUtil.getDeviceZoom());
1134-
resultImage.dispose ();
1135-
return result;
1136-
}
1137-
11381117
/**
11391118
* Returns a copy of the receiver which has been stretched or
11401119
* shrunk to the specified size. If either the width or height

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

+14-54
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,6 @@ private static enum AutoScaleMethod { AUTO, NEAREST, SMOOTH }
109109
}
110110
}
111111

112-
/**
113-
* Auto-scale down ImageData
114-
*/
115-
public static ImageData autoScaleDown (Device device, final ImageData imageData) {
116-
if (deviceZoom == 100 || imageData == null || (device != null && !device.isAutoScalable())) return imageData;
117-
float scaleFactor = 1.0f / getScalingFactor (deviceZoom);
118-
return autoScaleImageData(device, imageData, scaleFactor);
119-
}
120-
121112
public static int[] autoScaleDown(int[] pointArray) {
122113
if (deviceZoom == 100 || pointArray == null) return pointArray;
123114
float scaleFactor = getScalingFactor (deviceZoom);
@@ -272,31 +263,16 @@ public static Rectangle scaleDown(Drawable drawable, Rectangle rect, int zoom) {
272263
return scaleDown (rect, zoom);
273264
}
274265

275-
/**
276-
* Auto-scale image with ImageData
277-
*/
278-
public static ImageData scaleImageData (Device device, final ImageData imageData, int targetZoom, int currentZoom) {
279-
if (imageData == null || targetZoom == currentZoom || (device != null && !device.isAutoScalable())) return imageData;
280-
float scaleFactor = (float) targetZoom / (float) currentZoom;
281-
return autoScaleImageData(device, imageData, scaleFactor);
282-
}
283-
284-
285-
public static ImageData scaleImageData (Device device, final ElementAtZoom<ImageData> elementAtZoom, int targetZoom) {
286-
return scaleImageData(device, elementAtZoom.element(), targetZoom, elementAtZoom.zoom());
287-
}
288-
289-
private static ImageData autoScaleImageData (Device device, final ImageData imageData, float scaleFactor) {
290-
// Guards are already implemented in callers: if (deviceZoom == 100 || imageData == null || scaleFactor == 1.0f) return imageData;
291-
int width = imageData.width;
292-
int height = imageData.height;
293-
int scaledWidth = Math.round (width * scaleFactor);
294-
int scaledHeight = Math.round (height * scaleFactor);
295-
boolean useSmoothScaling = autoScaleMethod == AutoScaleMethod.SMOOTH && imageData.getTransparencyType() != SWT.TRANSPARENCY_MASK;
296-
if (useSmoothScaling) {
297-
return imageData.scaleToUsingSmoothScaling(device, scaledWidth, scaledHeight);
266+
public static int getScalingType(ImageData imageData) {
267+
switch(autoScaleMethod) {
268+
case SMOOTH:
269+
if (imageData.getTransparencyType() != SWT.TRANSPARENCY_MASK) {
270+
return SWT.SMOOTH;
271+
}
272+
return SWT.DEFAULT;
273+
default:
274+
return SWT.DEFAULT;
298275
}
299-
return imageData.scaledTo (scaledWidth, scaledHeight);
300276
}
301277

302278
/**
@@ -313,22 +289,6 @@ public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int current
313289
return returnRect;
314290
}
315291

316-
/**
317-
* Auto-scale ImageData to device zoom that are at given zoom factor.
318-
*/
319-
public static ImageData autoScaleImageData (Device device, final ImageData imageData, int imageDataZoomFactor) {
320-
if (deviceZoom == imageDataZoomFactor || imageData == null || (device != null && !device.isAutoScalable())) return imageData;
321-
float scaleFactor = (float) deviceZoom / imageDataZoomFactor;
322-
return autoScaleImageData(device, imageData, scaleFactor);
323-
}
324-
325-
/**
326-
* Auto-scale up ImageData to device zoom that is at 100%.
327-
*/
328-
public static ImageData autoScaleUp (Device device, final ImageData imageData) {
329-
return autoScaleImageData(device, imageData, 100);
330-
}
331-
332292
public static int[] autoScaleUp(int[] pointArray) {
333293
return scaleUp(pointArray, deviceZoom);
334294
}
@@ -584,10 +544,6 @@ public static int getDeviceZoom() {
584544
return deviceZoom;
585545
}
586546

587-
public static int getDeviceZoom(String autoScaleProperty) {
588-
return getZoomForAutoscaleProperty(nativeDeviceZoom, autoScaleProperty);
589-
}
590-
591547
public static void setDeviceZoom (int nativeDeviceZoom) {
592548
DPIUtil.nativeDeviceZoom = nativeDeviceZoom;
593549
int deviceZoom = getZoomForAutoscaleProperty (nativeDeviceZoom);
@@ -715,7 +671,11 @@ public AutoScaleImageDataProvider(Device device, ImageData data, int zoom){
715671
}
716672
@Override
717673
public ImageData getImageData(int zoom) {
718-
return DPIUtil.scaleImageData(device, imageData, zoom, currentZoom);
674+
Image image = new Image(device, imageData);
675+
int adjustedZoom = (int) ((float) getDeviceZoom() / (float) currentZoom) * zoom;
676+
ImageData imageData = image.getImageData(adjustedZoom);
677+
image.dispose();
678+
return imageData;
719679
}
720680
}
721681
}

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

+43-11
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ public Image(Device device, ImageData data) {
423423
super(device);
424424
if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
425425
currentDeviceZoom = DPIUtil.getDeviceZoom();
426-
data = DPIUtil.autoScaleUp (device, data);
426+
data = scaledTo(data, DPIUtil.getDeviceZoom(), 100, DPIUtil.getScalingType(imageData));
427427
init(data);
428428
init();
429429
}
@@ -466,8 +466,8 @@ public Image(Device device, ImageData source, ImageData mask) {
466466
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
467467
}
468468
currentDeviceZoom = DPIUtil.getDeviceZoom();
469-
source = DPIUtil.autoScaleUp (device, source);
470-
mask = DPIUtil.autoScaleUp (device, mask);
469+
source = scaledTo(source, currentDeviceZoom, 100, DPIUtil.getScalingType(source));
470+
mask = scaledTo(mask, currentDeviceZoom, 100, DPIUtil.getScalingType(mask));
471471
mask = ImageData.convertMask (mask);
472472
ImageData image = new ImageData(source.width, source.height, source.depth, source.palette, source.scanlinePad, source.data);
473473
image.maskPad = mask.scanlinePad;
@@ -533,7 +533,7 @@ public Image(Device device, InputStream stream) {
533533
super(device);
534534
currentDeviceZoom = DPIUtil.getDeviceZoom();
535535
ElementAtZoom<ImageData> image = ImageDataLoader.load(stream, FileFormat.DEFAULT_ZOOM, currentDeviceZoom);
536-
ImageData data = DPIUtil.scaleImageData(device, image, currentDeviceZoom);
536+
ImageData data = scaledTo(image.element(), currentDeviceZoom, image.zoom(), DPIUtil.getScalingType(image.element()));
537537
init(data);
538538
init();
539539
}
@@ -575,7 +575,7 @@ public Image(Device device, String filename) {
575575
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
576576
currentDeviceZoom = DPIUtil.getDeviceZoom();
577577
ElementAtZoom<ImageData> image = ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, currentDeviceZoom);
578-
ImageData data = DPIUtil.scaleImageData(device, image, currentDeviceZoom);
578+
ImageData data = scaledTo(image.element(), currentDeviceZoom, image.zoom(), DPIUtil.getScalingType(image.element()));
579579
init(data);
580580
init();
581581
}
@@ -744,7 +744,7 @@ boolean refreshImageForZoom () {
744744
if (deviceZoomLevel != currentDeviceZoom) {
745745
ImageData data = getImageDataAtCurrentZoom();
746746
destroy ();
747-
ImageData resizedData = DPIUtil.scaleImageData(device, data, deviceZoomLevel, currentDeviceZoom);
747+
ImageData resizedData = scaledTo(data, deviceZoomLevel, currentDeviceZoom, DPIUtil.getScalingType(data));
748748
init(resizedData);
749749
init();
750750
refreshed = true;
@@ -778,15 +778,15 @@ private void initFromFileNameProvider(int zoom) {
778778
ElementAtZoom<ImageData> imageDataAtZoom = ImageDataLoader.load(fileForZoom.element(), fileForZoom.zoom(), zoom);
779779
ImageData imageData = imageDataAtZoom.element();
780780
if (imageDataAtZoom.zoom() != zoom) {
781-
imageData = DPIUtil.scaleImageData(device, imageDataAtZoom, zoom);
781+
imageData = scaledTo(imageDataAtZoom.element(), zoom, imageDataAtZoom.zoom(), DPIUtil.getScalingType(imageData));
782782
}
783783
init(imageData);
784784
}
785785
}
786786

787787
private void initFromImageDataProvider(int zoom) {
788788
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom);
789-
ImageData resizedData = DPIUtil.scaleImageData (device, data.element(), zoom, data.zoom());
789+
ImageData resizedData = scaledTo(data.element(), zoom, data.zoom(), DPIUtil.getScalingType(data.element()));
790790
init(resizedData);
791791
}
792792

@@ -1146,17 +1146,49 @@ public ImageData getImageData (int zoom) {
11461146
return getImageDataAtCurrentZoom();
11471147
} else if (imageDataProvider != null) {
11481148
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom);
1149-
return DPIUtil.scaleImageData (device, data.element(), zoom, data.zoom());
1149+
return scaledTo(data.element(), zoom, data.zoom(), DPIUtil.getScalingType(data.element()));
11501150
} else if (imageFileNameProvider != null) {
11511151
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
1152-
return DPIUtil.scaleImageData (device, new ImageData (fileName.element()), zoom, fileName.zoom());
1152+
return scaledTo(new ImageData (fileName.element()), zoom, fileName.zoom(), DPIUtil.getScalingType(fileName.element()));
11531153
} else if (imageGcDrawer != null) {
11541154
return drawWithImageGcDrawer(width, height, zoom);
11551155
} else {
1156-
return DPIUtil.scaleImageData (device, getImageDataAtCurrentZoom (), zoom, currentDeviceZoom);
1156+
ImageData imageData = getImageDataAtCurrentZoom ();
1157+
return scaledTo(imageData, zoom, currentDeviceZoom, DPIUtil.getScalingType(imageData));
11571158
}
11581159
}
11591160

1161+
private ImageData scaledTo(ImageData imageData, int targetZoom, int currentZoom, int scaleType) {
1162+
if (imageData == null || currentZoom == targetZoom || !device.isAutoScalable()) {
1163+
return imageData;
1164+
}
1165+
float scaleFactor = (float) targetZoom / (float) currentZoom;
1166+
int scaledWidth = Math.round (imageData.width * scaleFactor);
1167+
int scaledHeight = Math.round (imageData.height * scaleFactor);
1168+
switch (scaleType) {
1169+
case SWT.SMOOTH:
1170+
return scaleUsingSmoothScaling(imageData, scaledWidth, scaledHeight);
1171+
default:
1172+
return imageData.scaledTo(scaledWidth, scaledHeight);
1173+
}
1174+
}
1175+
1176+
private ImageData scaleUsingSmoothScaling(ImageData imageData, int width, int height) {
1177+
Image original = new Image (device, (ImageDataProvider) zoom -> imageData);
1178+
/* Create a 24 bit image data with alpha channel */
1179+
final ImageData resultData = new ImageData (width, height, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
1180+
resultData.alphaData = new byte [width * height];
1181+
Image resultImage = new Image (device, (ImageDataProvider) zoom -> resultData);
1182+
GC gc = new GC (resultImage);
1183+
gc.setAntialias (SWT.ON);
1184+
gc.drawImage (original, 0, 0, imageData.width, imageData.height, 0, 0, width, height, false);
1185+
gc.dispose ();
1186+
original.dispose ();
1187+
ImageData result = resultImage.getImageData (DPIUtil.getDeviceZoom());
1188+
resultImage.dispose ();
1189+
return result;
1190+
}
1191+
11601192
private ImageData drawWithImageGcDrawer(int width, int height, int zoom) {
11611193
Image image = new Image(device, width, height);
11621194
GC gc = new GC(image);

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,21 @@ public static Long win32_getHandle (Cursor cursor, int zoom) {
372372
if (cursor.source == null) {
373373
cursor.setHandleForZoomLevel(cursor.handle, zoom);
374374
} else {
375-
ImageData source = DPIUtil.scaleImageData(cursor.device, cursor.source, zoom, DEFAULT_ZOOM);
375+
Image image;
376+
ImageData source, mask;
377+
Cursor newCursor;
378+
image = new Image(cursor.device, cursor.source);
379+
source = image.getImageData(zoom);
380+
image.dispose();
376381
if (cursor.isIcon) {
377-
Cursor newCursor = new Cursor(cursor.device, source, cursor.hotspotX, cursor.hotspotY);
378-
cursor.setHandleForZoomLevel(newCursor.handle, zoom);
382+
newCursor = new Cursor(cursor.device, source, cursor.hotspotX, cursor.hotspotY);
379383
} else {
380-
ImageData mask = DPIUtil.scaleImageData(cursor.device, cursor.mask, zoom, DEFAULT_ZOOM);
381-
Cursor newCursor = new Cursor(cursor.device, source, mask, cursor.hotspotX, cursor.hotspotY);
382-
cursor.setHandleForZoomLevel(newCursor.handle, zoom);
384+
image = new Image(cursor.device, cursor.mask);
385+
mask = image.getImageData(zoom);
386+
image.dispose();
387+
newCursor = new Cursor(cursor.device, source, mask, cursor.hotspotX, cursor.hotspotY);
383388
}
389+
cursor.setHandleForZoomLevel(newCursor.handle, zoom);
384390
}
385391
return cursor.zoomLevelToHandle.get(zoom);
386392
}

0 commit comments

Comments
 (0)