Skip to content

Commit 333df9b

Browse files
committed
WIP
1 parent 6517c9e commit 333df9b

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,5 +1801,29 @@ public String toString () {
18011801
return "Image {" + handle + "}";
18021802
}
18031803

1804+
/**
1805+
* <b>IMPORTANT:</b> This method is not part of the public
1806+
* API for Image. It is marked public only so that it
1807+
* can be shared within the packages provided by SWT. However, it is not
1808+
* available on all platforms.
1809+
*
1810+
* Draws a scaled image using the GC by another image.
1811+
*
1812+
* @param gc the GC to draw on the resulting image
1813+
* @param original the image which is supposed to be scaled and drawn on the resulting image
1814+
* @param width the width of the original image
1815+
* @param height the height of the original image
1816+
* @param scaleFactor the factor with which the image is supposed to be scaled
1817+
*
1818+
* @noreference This method is not intended to be referenced by clients.
1819+
*/
1820+
public static void drawScaled(GC gc, Image original, int width, int height, float scaleFactor) {
1821+
gc.drawImage (original, 0, 0, DPIUtil.autoScaleDown (width), DPIUtil.autoScaleDown (height),
1822+
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
1823+
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
1824+
*/
1825+
0, 0, Math.round (DPIUtil.autoScaleDown (width * scaleFactor)), Math.round (DPIUtil.autoScaleDown (height * scaleFactor)));
1826+
}
1827+
18041828
}
18051829

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,7 @@ private static ImageData autoScaleImageData (Device device, final ImageData imag
301301
Image resultImage = new Image (device, (ImageDataProvider) zoom -> resultData);
302302
GC gc = new GC (resultImage);
303303
gc.setAntialias (SWT.ON);
304-
gc.drawImage (original, 0, 0, autoScaleDown (width), autoScaleDown (height),
305-
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
306-
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
307-
*/
308-
0, 0, Math.round (autoScaleDown (width * scaleFactor)), Math.round (autoScaleDown (height * scaleFactor)));
304+
Image.drawScaled(gc, original, width, height, scaleFactor);
309305
gc.dispose ();
310306
original.dispose ();
311307
ImageData result = resultImage.getImageData (getDeviceZoom ());

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,4 +1551,28 @@ public String toString () {
15511551
return "Image {" + surface + "}";
15521552
}
15531553

1554+
/**
1555+
* <b>IMPORTANT:</b> This method is not part of the public
1556+
* API for Image. It is marked public only so that it
1557+
* can be shared within the packages provided by SWT. However, it is not
1558+
* available on all platforms.
1559+
*
1560+
* Draws a scaled image using the GC by another image.
1561+
*
1562+
* @param gc the GC to draw on the resulting image
1563+
* @param original the image which is supposed to be scaled and drawn on the resulting image
1564+
* @param width the width of the original image
1565+
* @param height the height of the original image
1566+
* @param scaleFactor the factor with which the image is supposed to be scaled
1567+
*
1568+
* @noreference This method is not intended to be referenced by clients.
1569+
*/
1570+
public static void drawScaled(GC gc, Image original, int width, int height, float scaleFactor) {
1571+
gc.drawImage (original, 0, 0, DPIUtil.autoScaleDown (width), DPIUtil.autoScaleDown (height),
1572+
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
1573+
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
1574+
*/
1575+
0, 0, Math.round (DPIUtil.autoScaleDown (width * scaleFactor)), Math.round (DPIUtil.autoScaleDown (height * scaleFactor)));
1576+
}
1577+
15541578
}

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

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,27 @@ public static long win32_getHandle (Image image, int zoom) {
828828
return image.getImageMetadata(zoom).handle;
829829
}
830830

831+
/**
832+
* <b>IMPORTANT:</b> This method is not part of the public
833+
* API for Image. It is marked public only so that it
834+
* can be shared within the packages provided by SWT. However, it is not
835+
* available on all platforms.
836+
*
837+
* Draws a scaled image using the GC by another image.
838+
*
839+
* @param gc the GC to draw on the resulting image
840+
* @param original the image which is supposed to be scaled and drawn on the resulting image
841+
* @param width the width of the original image
842+
* @param height the height of the original image
843+
* @param scaleFactor the factor with which the image is supposed to be scaled
844+
*
845+
* @noreference This method is not intended to be referenced by clients.
846+
*/
847+
public static void drawScaled(GC gc, Image original, int width, int height, float scaleFactor) {
848+
gc.drawImage (original, 0, 0, width, height,
849+
0, 0, Math.round (width * scaleFactor), Math.round (height * scaleFactor), false);
850+
}
851+
831852
long [] createGdipImage() {
832853
return createGdipImage(this.getZoom());
833854
}
@@ -1259,7 +1280,7 @@ private ImageData getScaledImageData (int zoom) {
12591280
}
12601281
TreeSet<Integer> availableZooms = new TreeSet<>(zoomLevelToImageHandle.keySet());
12611282
int closestZoom = Optional.ofNullable(availableZooms.higher(zoom)).orElse(availableZooms.lower(zoom));
1262-
return scaleImageData(getImageMetadata(closestZoom).getImageData(), zoom, closestZoom);
1283+
return DPIUtil.scaleImageData (device, getImageMetadata(closestZoom).getImageData(), zoom, closestZoom);
12631284
}
12641285

12651286

@@ -1849,40 +1870,6 @@ public void setBackground(Color color) {
18491870
zoomLevelToImageHandle.values().forEach(imageHandle -> imageHandle.setBackground(backgroundColor));
18501871
}
18511872

1852-
private ImageData scaleImageData(final ImageData imageData, int targetZoom, int currentZoom) {
1853-
if (imageData == null || targetZoom == currentZoom || (device != null && !device.isAutoScalable())) return imageData;
1854-
float scaleFactor = (float) targetZoom / (float) currentZoom;
1855-
int width = imageData.width;
1856-
int height = imageData.height;
1857-
int scaledWidth = Math.round (width * scaleFactor);
1858-
int scaledHeight = Math.round (height * scaleFactor);
1859-
boolean useSmoothScaling = DPIUtil.isSmoothScalingEnabled() && imageData.getTransparencyType() != SWT.TRANSPARENCY_MASK;
1860-
if (useSmoothScaling) {
1861-
return scaleToUsingSmoothScaling(scaledWidth, scaledHeight, imageData);
1862-
}
1863-
return imageData.scaledTo (scaledWidth, scaledHeight);
1864-
}
1865-
1866-
private ImageData scaleToUsingSmoothScaling(int width, int height, ImageData imageData) {
1867-
Image original = new Image (device, (ImageDataProvider) zoom -> imageData);
1868-
/* Create a 24 bit image data with alpha channel */
1869-
final ImageData resultData = new ImageData (width, height, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
1870-
resultData.alphaData = new byte [width * height];
1871-
Image resultImage = new Image (device, (ImageDataProvider) zoom -> resultData);
1872-
GC gc = new GC (resultImage);
1873-
gc.setAntialias (SWT.ON);
1874-
gc.drawImage (original, 0, 0, imageData.width, imageData.height,
1875-
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
1876-
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
1877-
*/
1878-
0, 0, width, height, false);
1879-
gc.dispose ();
1880-
original.dispose ();
1881-
ImageData result = resultImage.getImageData (resultImage.getZoom());
1882-
resultImage.dispose ();
1883-
return result;
1884-
}
1885-
18861873
private int getZoom() {
18871874
return DPIUtil.getZoomForAutoscaleProperty(initialNativeZoom);
18881875
}
@@ -2206,7 +2193,7 @@ final ImageHandle getImageMetadata(int zoom) {
22062193

22072194
private ImageHandle initializeHandleFromSource(int zoom) {
22082195
ElementAtZoom<ImageData> imageDataAtZoom = loadImageData(zoom);
2209-
ImageData imageData = scaleImageData(imageDataAtZoom.element(), zoom, imageDataAtZoom.zoom());
2196+
ImageData imageData = DPIUtil.scaleImageData (device,imageDataAtZoom.element(), zoom, imageDataAtZoom.zoom());
22102197
imageData = adaptImageDataIfDisabledOrGray(imageData);
22112198
return init(imageData, zoom);
22122199
}

0 commit comments

Comments
 (0)