@@ -533,7 +533,7 @@ public Image(Device device, InputStream stream) {
533
533
super (device );
534
534
currentDeviceZoom = DPIUtil .getDeviceZoom ();
535
535
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 ( imageData ) );
537
537
init (data );
538
538
init ();
539
539
}
@@ -575,7 +575,7 @@ public Image(Device device, String filename) {
575
575
if (filename == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
576
576
currentDeviceZoom = DPIUtil .getDeviceZoom ();
577
577
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 ( imageData ) );
579
579
init (data );
580
580
init ();
581
581
}
@@ -744,7 +744,7 @@ boolean refreshImageForZoom () {
744
744
if (deviceZoomLevel != currentDeviceZoom ) {
745
745
ImageData data = getImageDataAtCurrentZoom ();
746
746
destroy ();
747
- ImageData resizedData = DPIUtil . scaleImageData ( device , data , deviceZoomLevel , currentDeviceZoom );
747
+ ImageData resizedData = scaledTo ( data , deviceZoomLevel , currentDeviceZoom , DPIUtil . getScalingType ( imageData ) );
748
748
init (resizedData );
749
749
init ();
750
750
refreshed = true ;
@@ -778,15 +778,15 @@ private void initFromFileNameProvider(int zoom) {
778
778
ElementAtZoom <ImageData > imageDataAtZoom = ImageDataLoader .load (fileForZoom .element (), fileForZoom .zoom (), zoom );
779
779
ImageData imageData = imageDataAtZoom .element ();
780
780
if (imageDataAtZoom .zoom () != zoom ) {
781
- imageData = DPIUtil . scaleImageData ( device , imageDataAtZoom , zoom );
781
+ imageData = scaledTo ( imageDataAtZoom . element (), zoom , imageDataAtZoom . zoom (), DPIUtil . getScalingType ( imageData ) );
782
782
}
783
783
init (imageData );
784
784
}
785
785
}
786
786
787
787
private void initFromImageDataProvider (int zoom ) {
788
788
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 ( imageData ));
790
790
init (resizedData );
791
791
}
792
792
@@ -1146,17 +1146,48 @@ public ImageData getImageData (int zoom) {
1146
1146
return getImageDataAtCurrentZoom ();
1147
1147
} else if (imageDataProvider != null ) {
1148
1148
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 ( imageData ));
1150
1150
} else if (imageFileNameProvider != null ) {
1151
1151
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 ( imageData ));
1153
1153
} else if (imageGcDrawer != null ) {
1154
1154
return drawWithImageGcDrawer (width , height , zoom );
1155
1155
} else {
1156
- return DPIUtil . scaleImageData ( device , getImageDataAtCurrentZoom (), zoom , currentDeviceZoom );
1156
+ return scaledTo ( getImageDataAtCurrentZoom (), zoom , currentDeviceZoom , DPIUtil . getScalingType ( imageData ) );
1157
1157
}
1158
1158
}
1159
1159
1160
+ private ImageData scaledTo (ImageData imageData , int targetZoom , int currentZoom , int scaleType ) {
1161
+ if (imageData == null || currentZoom == targetZoom || !device .isAutoScalable ()) {
1162
+ return imageData ;
1163
+ }
1164
+ float scaleFactor = (float ) targetZoom / (float ) currentZoom ;
1165
+ int scaledWidth = Math .round (imageData .width * scaleFactor );
1166
+ int scaledHeight = Math .round (imageData .height * scaleFactor );
1167
+ switch (scaleType ) {
1168
+ case SWT .SMOOTH :
1169
+ return scaleUsingSmoothScaling (imageData , scaledWidth , scaledHeight );
1170
+ default :
1171
+ return imageData .scaledTo (scaledWidth , scaledHeight );
1172
+ }
1173
+ }
1174
+
1175
+ private ImageData scaleUsingSmoothScaling (ImageData imageData , int width , int height ) {
1176
+ Image original = new Image (device , (ImageDataProvider ) zoom -> imageData );
1177
+ /* Create a 24 bit image data with alpha channel */
1178
+ final ImageData resultData = new ImageData (width , height , 24 , new PaletteData (0xFF , 0xFF00 , 0xFF0000 ));
1179
+ resultData .alphaData = new byte [width * height ];
1180
+ Image resultImage = new Image (device , (ImageDataProvider ) zoom -> resultData );
1181
+ GC gc = new GC (resultImage );
1182
+ gc .setAntialias (SWT .ON );
1183
+ gc .drawImage (original , 0 , 0 , imageData .width , imageData .height , 0 , 0 , width , height , false );
1184
+ gc .dispose ();
1185
+ original .dispose ();
1186
+ ImageData result = resultImage .getImageData (resultImage .getZoom ());
1187
+ resultImage .dispose ();
1188
+ return result ;
1189
+ }
1190
+
1160
1191
private ImageData drawWithImageGcDrawer (int width , int height , int zoom ) {
1161
1192
Image image = new Image (device , width , height );
1162
1193
GC gc = new GC (image );
0 commit comments