@@ -144,64 +144,49 @@ public static ImageData scaleImageData (Device device, final ElementAtZoom<Image
144144 return scaleImageData (device , elementAtZoom .element (), targetZoom , elementAtZoom .zoom ());
145145}
146146
147- public static ImageData autoScaleImageData (Device device , final ImageData imageData , float scaleFactor ) {
148- // Guards are already implemented in callers: if (deviceZoom == 100 || imageData == null || scaleFactor == 1.0f) return imageData;
149- int width = imageData .width ;
150- int height = imageData .height ;
151- int scaledWidth = Math .round (width * scaleFactor );
152- int scaledHeight = Math .round (height * scaleFactor );
153- boolean useSmoothScaling = isSmoothScalingEnabled () && imageData .getTransparencyType () != SWT .TRANSPARENCY_MASK ;
154- if (useSmoothScaling ) {
155- Image original = new Image (device , (ImageDataProvider ) zoom -> imageData );
156- ImageGcDrawer drawer = new ImageGcDrawer () {
157- @ Override
158- public void drawOn (GC gc , int imageWidth , int imageHeight ) {
159- gc .setAntialias (SWT .ON );
160- Image .drawScaled (gc , original , width , height , scaleFactor );
161- };
147+ public static ImageData autoScaleImageData (Device device , final ImageData imageData , float scaleFactor ) {
148+ int targetWidth = Math .round (imageData .width * scaleFactor );
149+ int targetHeight = Math .round (imageData .height * scaleFactor );
150+ return scaleImage (device , imageData , imageData .width , imageData .height , targetWidth , targetHeight , Image ::drawAtTargetSize );
151+ }
162152
163- @ Override
164- public int getGcStyle () {
165- return SWT .TRANSPARENT ;
166- }
167- };
168- Image resultImage = new Image (device , drawer , scaledWidth , scaledHeight );
169- ImageData result = resultImage .getImageData (100 );
170- original .dispose ();
171- resultImage .dispose ();
172- return result ;
173- } else {
174- return imageData .scaledTo (scaledWidth , scaledHeight );
175- }
153+ public static ImageData autoScaleImageData (Device device , final ImageData imageData , int targetWidth , int targetHeight ) {
154+ return scaleImage (device , imageData , imageData .width , imageData .height , targetWidth , targetHeight , Image ::drawAtTargetSize );
155+ }
156+
157+ @ FunctionalInterface
158+ private interface ImageDrawFunction {
159+ void draw (GC gc , Image original , int originalWidth , int originalHeight , int targetWidth , int targetHeight );
176160}
177161
178- //TODO make private
179- public static ImageData autoScaleImageData (Device device , final ImageData imageData , int targetWidth , int targetHeight ) {
180- // Guards are already implemented in callers: if (deviceZoom == 100 || imageData == null || scaleFactor == 1.0f) return imageData;
181- int width = imageData .width ;
182- int height = imageData .height ;
162+ private static ImageData scaleImage (Device device , ImageData imageData , int originalWidth , int originalHeight ,
163+ int targetWidth , int targetHeight , ImageDrawFunction drawFunction ) {
164+
183165 boolean useSmoothScaling = isSmoothScalingEnabled () && imageData .getTransparencyType () != SWT .TRANSPARENCY_MASK ;
166+
184167 if (useSmoothScaling ) {
185- Image original = new Image (device , (ImageDataProvider ) zoom -> imageData );
186- ImageGcDrawer drawer = new ImageGcDrawer () {
168+ Image original = new Image (device , (ImageDataProvider ) zoom -> imageData );
169+
170+ ImageGcDrawer drawer = new ImageGcDrawer () {
187171 @ Override
188172 public void drawOn (GC gc , int imageWidth , int imageHeight ) {
189- gc .setAntialias (SWT .ON );
190- Image . drawAtTargetSize (gc , original , width , height , targetWidth , targetHeight );
191- };
173+ gc .setAntialias (SWT .ON );
174+ drawFunction . draw (gc , original , originalWidth , originalHeight , targetWidth , targetHeight );
175+ }
192176
193177 @ Override
194178 public int getGcStyle () {
195179 return SWT .TRANSPARENT ;
196180 }
197181 };
198- Image resultImage = new Image (device , drawer , targetWidth , targetHeight );
199- ImageData result = resultImage .getImageData (100 );
200- original .dispose ();
201- resultImage .dispose ();
182+
183+ Image resultImage = new Image (device , drawer , targetWidth , targetHeight );
184+ ImageData result = resultImage .getImageData (100 );
185+ original .dispose ();
186+ resultImage .dispose ();
202187 return result ;
203188 } else {
204- return imageData .scaledTo (targetWidth , targetHeight );
189+ return imageData .scaledTo (targetWidth , targetHeight );
205190 }
206191}
207192
0 commit comments