Skip to content

Commit 0201e52

Browse files
HeikoKlareakurtakov
authored andcommitted
[GTK] Fix scaling of image copies and stylings
The image copy and styling operation via the constructor Image(Device, Image, int) does currently not properly take into account a potential scaling of the original image. The surface for the new image is always created with the size in points, i.e., according to a 100% scaling. When the image is used at 200% it will thus appear blurry. This change adapts the image copy and styling constructor to properly take the scaling of the original image into account. It make the constructor create a surface with a proper size and scaling.
1 parent 8a57687 commit 0201e52

File tree

1 file changed

+13
-11
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics

1 file changed

+13
-11
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,18 @@ public Image(Device device, Image srcImage, int flag) {
278278
if (flag != SWT.IMAGE_DISABLE) transparentPixel = srcImage.transparentPixel;
279279

280280
long imageSurface = srcImage.surface;
281-
int width = this.width = srcImage.width;
282-
int height = this.height = srcImage.height;
281+
this.width = srcImage.width;
282+
this.height = srcImage.height;
283283
int format = Cairo.cairo_surface_get_content(imageSurface) == Cairo.CAIRO_CONTENT_COLOR ? Cairo.CAIRO_FORMAT_RGB24 : Cairo.CAIRO_FORMAT_ARGB32;
284284
boolean hasAlpha = format == Cairo.CAIRO_FORMAT_ARGB32;
285-
surface = Cairo.cairo_image_surface_create(format, width, height);
285+
int dataWidth = DPIUtil.pointToPixel(this.width, DPIUtil.getDeviceZoom());
286+
int dataHeight= DPIUtil.pointToPixel(this.height, DPIUtil.getDeviceZoom());
287+
surface = Cairo.cairo_image_surface_create(format, dataWidth, dataHeight);
286288
if (surface == 0) SWT.error(SWT.ERROR_NO_HANDLES);
287-
if (DPIUtil.getDeviceZoom() != currentDeviceZoom) {
288-
double scaleFactor = DPIUtil.getDeviceZoom() / 100f;
289-
Cairo.cairo_surface_set_device_scale(surface, scaleFactor, scaleFactor);
290-
}
289+
double[] scaleX = new double[1];
290+
double[] scaleY = new double[1];
291+
Cairo.cairo_surface_get_device_scale(imageSurface, scaleX, scaleY);
292+
Cairo.cairo_surface_set_device_scale(surface, scaleX[0], scaleY[0]);
291293
long cairo = Cairo.cairo_create(surface);
292294
if (cairo == 0) SWT.error(SWT.ERROR_NO_HANDLES);
293295
Cairo.cairo_set_operator(cairo, Cairo.CAIRO_OPERATOR_SOURCE);
@@ -306,9 +308,9 @@ public Image(Device device, Image srcImage, int flag) {
306308
switch (flag) {
307309
case SWT.IMAGE_DISABLE: {
308310
byte[] line = new byte[stride];
309-
for (int y=0; y<height; y++) {
311+
for (int y=0; y<dataHeight; y++) {
310312
C.memmove(line, data + (y * stride), stride);
311-
for (int x = 0, offset = 0; x < width; x++, offset += 4) {
313+
for (int x = 0, offset = 0; x < dataWidth; x++, offset += 4) {
312314
int a = line[offset + oa] & 0xFF;
313315
int r = line[offset + or] & 0xFF;
314316
int g = line[offset + og] & 0xFF;
@@ -339,9 +341,9 @@ public Image(Device device, Image srcImage, int flag) {
339341
}
340342
case SWT.IMAGE_GRAY: {
341343
byte[] line = new byte[stride];
342-
for (int y=0; y<height; y++) {
344+
for (int y=0; y<dataHeight; y++) {
343345
C.memmove(line, data + (y * stride), stride);
344-
for (int x=0, offset = 0; x<width; x++, offset += 4) {
346+
for (int x=0, offset = 0; x<dataWidth; x++, offset += 4) {
345347
int a = line[offset + oa] & 0xFF;
346348
int r = line[offset + or] & 0xFF;
347349
int g = line[offset + og] & 0xFF;

0 commit comments

Comments
 (0)