Skip to content

Commit 039d965

Browse files
amartya4256HeikoKlare
authored andcommitted
Move ImageData scaling logic to Image #62
This commit contributes to moving the logic for scaling the ImageData using GC from DPIUtil to Image for each platform and DPIUtil calls the platform specific code from the method DPIUtil:autoScaleImageData. contributes to #62 and #127
1 parent 033d733 commit 039d965

File tree

4 files changed

+69
-41
lines changed

4 files changed

+69
-41
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -1801,5 +1801,28 @@ 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.
1808+
*
1809+
* Draws a scaled image using the GC by another image.
1810+
*
1811+
* @param gc the GC to draw on the resulting image
1812+
* @param original the image which is supposed to be scaled and drawn on the resulting image
1813+
* @param width the width of the original image
1814+
* @param height the height of the original image
1815+
* @param scaleFactor the factor with which the image is supposed to be scaled
1816+
*
1817+
* @noreference This method is not intended to be referenced by clients.
1818+
*/
1819+
public static void drawScaled(GC gc, Image original, int width, int height, float scaleFactor) {
1820+
gc.drawImage (original, 0, 0, DPIUtil.autoScaleDown (width), DPIUtil.autoScaleDown (height),
1821+
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
1822+
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
1823+
*/
1824+
0, 0, Math.round (DPIUtil.autoScaleDown (width * scaleFactor)), Math.round (DPIUtil.autoScaleDown (height * scaleFactor)));
1825+
}
1826+
18041827
}
18051828

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,7 @@ private static ImageData autoScaleImageData (Device device, final ImageData imag
312312
Image resultImage = new Image (device, (ImageDataProvider) zoom -> resultData);
313313
GC gc = new GC (resultImage);
314314
gc.setAntialias (SWT.ON);
315-
gc.drawImage (original, 0, 0, autoScaleDown (width), autoScaleDown (height),
316-
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
317-
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
318-
*/
319-
0, 0, Math.round (autoScaleDown (width * scaleFactor)), Math.round (autoScaleDown (height * scaleFactor)));
315+
Image.drawScaled(gc, original, width, height, scaleFactor);
320316
gc.dispose ();
321317
original.dispose ();
322318
ImageData result = resultImage.getImageData (getDeviceZoom ());

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

+23
Original file line numberDiff line numberDiff line change
@@ -1551,4 +1551,27 @@ 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.
1558+
*
1559+
* Draws a scaled image using the GC by another image.
1560+
*
1561+
* @param gc the GC to draw on the resulting image
1562+
* @param original the image which is supposed to be scaled and drawn on the resulting image
1563+
* @param width the width of the original image
1564+
* @param height the height of the original image
1565+
* @param scaleFactor the factor with which the image is supposed to be scaled
1566+
*
1567+
* @noreference This method is not intended to be referenced by clients.
1568+
*/
1569+
public static void drawScaled(GC gc, Image original, int width, int height, float scaleFactor) {
1570+
gc.drawImage (original, 0, 0, DPIUtil.autoScaleDown (width), DPIUtil.autoScaleDown (height),
1571+
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
1572+
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
1573+
*/
1574+
0, 0, Math.round (DPIUtil.autoScaleDown (width * scaleFactor)), Math.round (DPIUtil.autoScaleDown (height * scaleFactor)));
1575+
}
1576+
15541577
}

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

+22-36
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,26 @@ public static long win32_getHandle (Image image, int zoom) {
807807
return image.getImageMetadata(zoom).handle;
808808
}
809809

810+
/**
811+
* <b>IMPORTANT:</b> This method is not part of the public
812+
* API for Image. It is marked public only so that it
813+
* can be shared within the packages provided by SWT.
814+
*
815+
* Draws a scaled image using the GC by another image.
816+
*
817+
* @param gc the GC to draw on the resulting image
818+
* @param original the image which is supposed to be scaled and drawn on the resulting image
819+
* @param width the width of the original image
820+
* @param height the height of the original image
821+
* @param scaleFactor the factor with which the image is supposed to be scaled
822+
*
823+
* @noreference This method is not intended to be referenced by clients.
824+
*/
825+
public static void drawScaled(GC gc, Image original, int width, int height, float scaleFactor) {
826+
gc.drawImage (original, 0, 0, width, height,
827+
0, 0, Math.round (width * scaleFactor), Math.round (height * scaleFactor), false);
828+
}
829+
810830
long [] createGdipImage() {
811831
return createGdipImage(this.getZoom());
812832
}
@@ -1793,40 +1813,6 @@ public void setBackground(Color color) {
17931813
zoomLevelToImageHandle.values().forEach(imageHandle -> imageHandle.setBackground(backgroundColor));
17941814
}
17951815

1796-
private ImageData scaleImageData(final ImageData imageData, int targetZoom, int currentZoom) {
1797-
if (imageData == null || targetZoom == currentZoom || (device != null && !device.isAutoScalable())) return imageData;
1798-
float scaleFactor = (float) targetZoom / (float) currentZoom;
1799-
int width = imageData.width;
1800-
int height = imageData.height;
1801-
int scaledWidth = Math.round (width * scaleFactor);
1802-
int scaledHeight = Math.round (height * scaleFactor);
1803-
boolean useSmoothScaling = DPIUtil.isSmoothScalingEnabled() && imageData.getTransparencyType() != SWT.TRANSPARENCY_MASK;
1804-
if (useSmoothScaling) {
1805-
return scaleToUsingSmoothScaling(scaledWidth, scaledHeight, imageData);
1806-
}
1807-
return imageData.scaledTo (scaledWidth, scaledHeight);
1808-
}
1809-
1810-
private ImageData scaleToUsingSmoothScaling(int width, int height, ImageData imageData) {
1811-
Image original = new Image (device, (ImageDataProvider) zoom -> imageData);
1812-
/* Create a 24 bit image data with alpha channel */
1813-
final ImageData resultData = new ImageData (width, height, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
1814-
resultData.alphaData = new byte [width * height];
1815-
Image resultImage = new Image (device, (ImageDataProvider) zoom -> resultData);
1816-
GC gc = new GC (resultImage);
1817-
gc.setAntialias (SWT.ON);
1818-
gc.drawImage (original, 0, 0, imageData.width, imageData.height,
1819-
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
1820-
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
1821-
*/
1822-
0, 0, width, height, false);
1823-
gc.dispose ();
1824-
original.dispose ();
1825-
ImageData result = resultImage.getImageData (resultImage.getZoom());
1826-
resultImage.dispose ();
1827-
return result;
1828-
}
1829-
18301816
private int getZoom() {
18311817
return DPIUtil.getZoomForAutoscaleProperty(initialNativeZoom);
18321818
}
@@ -1887,7 +1873,7 @@ ImageData getScaledImageData (int zoom) {
18871873
}
18881874
TreeSet<Integer> availableZooms = new TreeSet<>(zoomLevelToImageHandle.keySet());
18891875
int closestZoom = Optional.ofNullable(availableZooms.higher(zoom)).orElse(availableZooms.lower(zoom));
1890-
return scaleImageData(getImageMetadata(closestZoom).getImageData(), zoom, closestZoom);
1876+
return DPIUtil.scaleImageData(device, getImageMetadata(closestZoom).getImageData(), zoom, closestZoom);
18911877
}
18921878

18931879
protected ImageHandle newImageHandle(int zoom) {
@@ -2197,7 +2183,7 @@ protected ImageHandle newImageHandle(int zoom) {
21972183

21982184
private ImageHandle initializeHandleFromSource(int zoom) {
21992185
ElementAtZoom<ImageData> imageDataAtZoom = loadImageData(zoom);
2200-
ImageData imageData = scaleImageData(imageDataAtZoom.element(), zoom, imageDataAtZoom.zoom());
2186+
ImageData imageData = DPIUtil.scaleImageData (device,imageDataAtZoom.element(), zoom, imageDataAtZoom.zoom());
22012187
imageData = adaptImageDataIfDisabledOrGray(imageData);
22022188
return init(imageData, zoom);
22032189
}

0 commit comments

Comments
 (0)