Skip to content

Commit ad4b117

Browse files
al-nooriakoch-yatta
authored andcommitted
GC#drawImage methods now consider applied Transformations
If a transform is applied via the GC, then both drawImage APIs consider now the best fitting handle by requesting it through the width/height of the full image scaled by the scaleFactor (relating destination width/height to source winowdth/height) times the width/height scaling induced by the transformation.
1 parent 6399bff commit ad4b117

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,13 +1214,29 @@ void apply() {
12141214
}
12151215
}
12161216

1217+
private float calculateTransformationScale() {
1218+
Transform current = new Transform(device);
1219+
getTransform(current);
1220+
float[] m = new float[6];
1221+
current.getElements(m);
1222+
float scaleWidth = (float) Math.hypot(m[0], m[2]);
1223+
float scaleHeight = (float) Math.hypot(m[1], m[3]);
1224+
current.dispose();
1225+
return Math.max(scaleWidth, scaleHeight);
1226+
}
1227+
12171228
private void drawImage(Image image, int destX, int destY, int destWidth, int destHeight, int imageZoom) {
1218-
Rectangle destPixels= Win32DPIUtils.pointToPixel(drawable, new Rectangle(destX , destY, destWidth , destHeight),
1229+
float transformationScale = calculateTransformationScale();
1230+
int scaledImageZoomWithTransform = Math.round(transformationScale * imageZoom);
1231+
Rectangle destPixels = Win32DPIUtils.pointToPixel(drawable, new Rectangle(destX , destY, destWidth , destHeight),
12191232
imageZoom);
1233+
Rectangle destPixelsScaledWithTransform = Win32DPIUtils.pointToPixel(drawable, new Rectangle(destX , destY, destWidth , destHeight),
1234+
scaledImageZoomWithTransform);
1235+
12201236
image.executeOnImageHandleAtBestFittingSize(tempHandle -> {
12211237
drawImage(image, 0, 0, tempHandle.width(), tempHandle.height(), destPixels.x, destPixels.y,
12221238
destPixels.width, destPixels.height, false, tempHandle);
1223-
}, destPixels.width, destPixels.height);
1239+
}, destPixelsScaledWithTransform.width, destPixelsScaledWithTransform.height);
12241240
}
12251241

12261242
private void drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY,
@@ -1230,6 +1246,9 @@ private void drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHei
12301246
Rectangle fullImageBounds = image.getBounds();
12311247
Rectangle fullImageBoundsPixels = Win32DPIUtils.pointToPixel(drawable, fullImageBounds, scaledImageZoom);
12321248
Rectangle src = new Rectangle(srcX, srcY, srcWidth, srcHeight);
1249+
float transformationScale = calculateTransformationScale();
1250+
int scaledImageZoomWithTransform = Math.round(transformationScale * scaledImageZoom);
1251+
Rectangle fullImageBoundsScaledWithTransform = Win32DPIUtils.pointToPixel(drawable, fullImageBounds, scaledImageZoomWithTransform);
12331252
if (scaledImageZoom != 100) {
12341253
/*
12351254
* This is a HACK! Due to rounding errors at fractional scale factors,
@@ -1250,7 +1269,7 @@ private void drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHei
12501269
Rectangle newSrcPixels = computeSourceRectangle(tempHandle, fullImageBounds, fullImageBoundsPixels, src, srcPixels);
12511270
drawImage(image, newSrcPixels.x, newSrcPixels.y, newSrcPixels.width, newSrcPixels.height, destPixels.x, destPixels.y, destPixels.width,
12521271
destPixels.height, false, tempHandle);
1253-
}, fullImageBoundsPixels.width, fullImageBoundsPixels.height);
1272+
}, fullImageBoundsScaledWithTransform.width, fullImageBoundsScaledWithTransform.height);
12541273
}
12551274

12561275
private Rectangle computeSourceRectangle(ImageHandle imageHandle, Rectangle fullImageBounds, Rectangle fullImageBoundsPixels, Rectangle src, Rectangle srcPixels) {

0 commit comments

Comments
 (0)