Skip to content

Commit ecc2a34

Browse files
committed
Use ImageGcDrawer for smooth scaling of ImageData
This commit adapts the smooth scaling implementation to use ImageGcDrawer.
1 parent e5e215d commit ecc2a34

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,16 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
903903
}
904904

905905
private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width, int height, int zoom) {
906-
Image image = new Image(device, width, height);
907-
GC gc = new GC(image);
906+
int gcStyle = imageGcDrawer.getGcStyle();
907+
Image image;
908+
if ((gcStyle & SWT.TRANSPARENT) != 0) {
909+
final ImageData resultData = new ImageData (width, height, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
910+
resultData.alphaData = new byte [width * height];
911+
image = new Image(device, resultData);
912+
} else {
913+
image = new Image(device, width, height);
914+
}
915+
GC gc = new GC(image, gcStyle);
908916
try {
909917
imageGcDrawer.drawOn(gc, width, height);
910918
ImageData imageData = image.getImageData(zoom);

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,21 @@ private static ImageData autoScaleImageData (Device device, final ImageData imag
306306
boolean useSmoothScaling = isSmoothScalingEnabled() && imageData.getTransparencyType() != SWT.TRANSPARENCY_MASK;
307307
if (useSmoothScaling) {
308308
Image original = new Image (device, (ImageDataProvider) zoom -> imageData);
309-
/* Create a 24 bit image data with alpha channel */
310-
final ImageData resultData = new ImageData (scaledWidth, scaledHeight, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
311-
resultData.alphaData = new byte [scaledWidth * scaledHeight];
312-
Image resultImage = new Image (device, (ImageDataProvider) zoom -> resultData);
313-
GC gc = new GC (resultImage);
314-
gc.setAntialias (SWT.ON);
315-
Image.drawScaled(gc, original, width, height, scaleFactor);
316-
gc.dispose ();
309+
ImageGcDrawer drawer = new ImageGcDrawer() {
310+
@Override
311+
public void drawOn(GC gc, int imageWidth, int imageHeight) {
312+
gc.setAntialias (SWT.ON);
313+
Image.drawScaled(gc, original, width, height, scaleFactor);
314+
};
315+
316+
@Override
317+
public int getGcStyle() {
318+
return SWT.TRANSPARENT;
319+
}
320+
};
321+
Image resultImage = new Image (device, drawer, scaledWidth, scaledHeight);
322+
ImageData result = resultImage.getImageData (100);
317323
original.dispose ();
318-
ImageData result = resultImage.getImageData (getDeviceZoom ());
319324
resultImage.dispose ();
320325
return result;
321326
} else {

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,14 @@ public Image(Device device, Rectangle bounds) {
437437
* @see #dispose()
438438
*/
439439
public Image(Device device, ImageData data) {
440+
this(device, DPIUtil.autoScaleUp(device, data), DPIUtil.getDeviceZoom());
441+
}
442+
443+
private Image(Device device, ImageData data, int zoom) {
440444
super(device);
441445
if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
442-
currentDeviceZoom = DPIUtil.getDeviceZoom();
443-
data = DPIUtil.autoScaleUp (device, data);
444-
init(data);
446+
currentDeviceZoom = zoom;
447+
init(data, zoom);
445448
init();
446449
}
447450

@@ -695,9 +698,9 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
695698
SWT.error(SWT.ERROR_NULL_ARGUMENT);
696699
}
697700
this.imageGcDrawer = imageGcDrawer;
698-
currentDeviceZoom = DPIUtil.getDeviceZoom();
701+
currentDeviceZoom = 100;
699702
ImageData imageData = drawWithImageGcDrawer(width, height, currentDeviceZoom);
700-
init (imageData);
703+
init (imageData, currentDeviceZoom);
701704
init ();
702705
}
703706

@@ -1162,8 +1165,16 @@ public ImageData getImageData (int zoom) {
11621165
}
11631166

11641167
private ImageData drawWithImageGcDrawer(int width, int height, int zoom) {
1165-
Image image = new Image(device, width, height);
1166-
GC gc = new GC(image);
1168+
int gcStyle = imageGcDrawer.getGcStyle();
1169+
Image image;
1170+
if ((gcStyle & SWT.TRANSPARENT) != 0) {
1171+
final ImageData resultData = new ImageData(width, height, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
1172+
resultData.alphaData = new byte [width * height];
1173+
image = new Image(device, resultData, zoom);
1174+
} else {
1175+
image = new Image(device, width, height);
1176+
}
1177+
GC gc = new GC(image, gcStyle);
11671178
try {
11681179
imageGcDrawer.drawOn(gc, width, height);
11691180
ImageData imageData = image.getImageData(zoom);
@@ -1274,6 +1285,10 @@ void init(int width, int height) {
12741285
}
12751286

12761287
void init(ImageData image) {
1288+
init(image, DPIUtil.getDeviceZoom());
1289+
}
1290+
1291+
void init(ImageData image, int zoom) {
12771292
if (image == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
12781293

12791294
PaletteData palette = image.palette;
@@ -1286,7 +1301,7 @@ void init(ImageData image) {
12861301
int imageDataHeight = image.height;
12871302

12881303
// Scale dimensions of Image object to 100% scale factor
1289-
double scaleFactor = DPIUtil.getDeviceZoom() / 100f;
1304+
double scaleFactor = zoom / 100f;
12901305
this.width = (int) Math.round(imageDataWidth / scaleFactor);
12911306
this.height = (int) Math.round(imageDataHeight / scaleFactor);
12921307

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ private Image(Device device, ImageData data, int zoom) {
378378
super(device);
379379
if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
380380
initialNativeZoom = zoom;
381-
this.imageProvider = new PlainImageDataProviderWrapper(data);
381+
this.imageProvider = new PlainImageDataProviderWrapper(data, zoom);
382382
init();
383383
this.device.registerResourceWithZoomSupport(this);
384384
}

0 commit comments

Comments
 (0)