@@ -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+
810830long [] 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-
18301816private 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