@@ -423,7 +423,7 @@ public Image(Device device, ImageData data) {
423
423
super (device );
424
424
if (data == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
425
425
currentDeviceZoom = DPIUtil .getDeviceZoom ();
426
- data = DPIUtil .autoScaleUp ( device , data );
426
+ data = scaledTo ( data , DPIUtil .getDeviceZoom (), 100 , DPIUtil . getScalingType ( imageData ) );
427
427
init (data );
428
428
init ();
429
429
}
@@ -466,8 +466,8 @@ public Image(Device device, ImageData source, ImageData mask) {
466
466
SWT .error (SWT .ERROR_INVALID_ARGUMENT );
467
467
}
468
468
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 ) );
471
471
mask = ImageData .convertMask (mask );
472
472
ImageData image = new ImageData (source .width , source .height , source .depth , source .palette , source .scanlinePad , source .data );
473
473
image .maskPad = mask .scanlinePad ;
@@ -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 ( image . element ()) );
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 ( image . element ()) );
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 ( data ) );
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 ( data . element () ));
790
790
init (resizedData );
791
791
}
792
792
@@ -1146,17 +1146,49 @@ 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 ( data . element () ));
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 ( fileName . element () ));
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
+ ImageData imageData = getImageDataAtCurrentZoom ();
1157
+ return scaledTo (imageData , zoom , currentDeviceZoom , DPIUtil .getScalingType (imageData ));
1157
1158
}
1158
1159
}
1159
1160
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
+
1160
1192
private ImageData drawWithImageGcDrawer (int width , int height , int zoom ) {
1161
1193
Image image = new Image (device , width , height );
1162
1194
GC gc = new GC (image );
0 commit comments