@@ -1061,7 +1061,8 @@ void apply() {
1061
1061
1062
1062
private void drawImageInPixels (Image image , Point location ) {
1063
1063
if (image .isDisposed ()) SWT .error (SWT .ERROR_INVALID_ARGUMENT );
1064
- drawImage (image , 0 , 0 , -1 , -1 , location .x , location .y , -1 , -1 , true , getZoom ());
1064
+ long handle = Image .win32_getHandle (image , getZoom ());
1065
+ drawImage (image , 0 , 0 , -1 , -1 , location .x , location .y , -1 , -1 , true , handle );
1065
1066
}
1066
1067
}
1067
1068
@@ -1154,8 +1155,7 @@ public void drawImage(Image image, int destX, int destY, int destWidth, int dest
1154
1155
if (image .isDisposed ())
1155
1156
SWT .error (SWT .ERROR_INVALID_ARGUMENT );
1156
1157
1157
- storeAndApplyOperationForExistingHandle (new DrawScalingImageToImageOperation (image , new Rectangle (0 , 0 , 0 , 0 ),
1158
- new Rectangle (destX , destY , destWidth , destHeight )));
1158
+ storeAndApplyOperationForExistingHandle (new DrawScaledImageOperation (image , new Rectangle (destX , destY , destWidth , destHeight )));
1159
1159
}
1160
1160
1161
1161
void drawImage (Image srcImage , int srcX , int srcY , int srcWidth , int srcHeight , int destX , int destY , int destWidth , int destHeight , boolean simple ) {
@@ -1213,15 +1213,40 @@ private int calculateZoomForImage(int gcZoom, int srcWidth, int srcHeight, int d
1213
1213
}
1214
1214
}
1215
1215
1216
+ private class DrawScaledImageOperation extends ImageOperation {
1217
+ private final Rectangle destination ;
1218
+
1219
+ DrawScaledImageOperation (Image image , Rectangle destination ) {
1220
+ super (image );
1221
+ this .destination = destination ;
1222
+ }
1223
+
1224
+ @ Override
1225
+ void apply () {
1226
+ int gcZoom = getZoom ();
1227
+ drawImage (getImage (), destination .x , destination .y , destination .width , destination .height , gcZoom );
1228
+ }
1229
+ }
1230
+
1231
+ private void drawImage (Image image , int destX , int destY , int destWidth , int destHeight , int imageZoom ) {
1232
+ Rectangle destPixels = Win32DPIUtils .pointToPixel (drawable , new Rectangle (destX , destY , destWidth , destHeight ), imageZoom );
1233
+ image .executeOnImageHandleAtSizeWithZoomFallback ((tempHandle , handleSize ) -> {
1234
+ drawImage (image , 0 , 0 , handleSize .x , handleSize .y , destPixels .x , destPixels .y , destPixels .width ,
1235
+ destPixels .height , false , tempHandle );
1236
+ }, destPixels .width , destPixels .height );
1237
+ }
1238
+
1216
1239
private void drawImage (Image image , int srcX , int srcY , int srcWidth , int srcHeight , int destX , int destY ,
1217
1240
int destWidth , int destHeight , int imageZoom , int scaledImageZoom ) {
1218
- Rectangle src = Win32DPIUtils .pointToPixel (drawable , new Rectangle (srcX , srcY , srcWidth , srcHeight ), scaledImageZoom );
1219
- Rectangle dest = Win32DPIUtils .pointToPixel (drawable , new Rectangle (destX , destY , destWidth , destHeight ), imageZoom );
1220
- if (scaledImageZoom != 100 ) {
1241
+ Rectangle src = Win32DPIUtils .pointToPixel (drawable , new Rectangle (srcX , srcY , srcWidth , srcHeight ),
1242
+ scaledImageZoom );
1243
+ Rectangle destPixels = Win32DPIUtils .pointToPixel (drawable , new Rectangle (destX , destY , destWidth , destHeight ),
1244
+ imageZoom );
1245
+ if (scaledImageZoom % 100 != 0 ) {
1221
1246
/*
1222
- * This is a HACK! Due to rounding errors at fractional scale factors,
1223
- * the coordinates may be slightly off. The workaround is to restrict
1224
- * coordinates to the allowed bounds.
1247
+ * This is a HACK! Due to rounding errors at fractional scale factors, the
1248
+ * coordinates may be slightly off. The workaround is to restrict coordinates to
1249
+ * the allowed bounds.
1225
1250
*/
1226
1251
Rectangle b = image .getBounds (scaledImageZoom );
1227
1252
int errX = src .x + src .width - b .width ;
@@ -1234,7 +1259,24 @@ private void drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHei
1234
1259
}
1235
1260
}
1236
1261
}
1237
- drawImage (image , src .x , src .y , src .width , src .height , dest .x , dest .y , dest .width , dest .height , false , scaledImageZoom );
1262
+ int targetWidth ;
1263
+ int targetHeight ;
1264
+ Rectangle scaledSrc ;
1265
+
1266
+ float widthScalingFactor = (float ) destWidth / srcWidth ;
1267
+ float heightScalingFactor = (float ) destHeight / srcHeight ;
1268
+ Rectangle fullImageBounds = image .getBounds ();
1269
+ targetWidth = Math .round (fullImageBounds .width * widthScalingFactor );
1270
+ targetHeight = Math .round (fullImageBounds .height * heightScalingFactor );
1271
+
1272
+ scaledSrc = new Rectangle (Math .round (src .x * widthScalingFactor ), Math .round (src .y * heightScalingFactor ),
1273
+ Math .round (src .width * widthScalingFactor ), Math .round (src .height * heightScalingFactor ));
1274
+ Point targetSize = Win32DPIUtils .pointToPixel (drawable , new Point (targetWidth , targetHeight ), scaledImageZoom );
1275
+ image .executeOnImageHandleAtSizeOrZoom ((tempHandle , handleSize ) -> {
1276
+ Rectangle srcRect = (handleSize .equals (targetSize )) ? scaledSrc : src ;
1277
+ drawImage (image , srcRect .x , srcRect .y , srcRect .width , srcRect .height , destPixels .x , destPixels .y ,
1278
+ destPixels .width , destPixels .height , false , tempHandle );
1279
+ }, targetSize .x , targetSize .y , scaledImageZoom );
1238
1280
}
1239
1281
1240
1282
private class DrawImageToImageOperation extends ImageOperation {
@@ -1251,17 +1293,19 @@ private class DrawImageToImageOperation extends ImageOperation {
1251
1293
1252
1294
@ Override
1253
1295
void apply () {
1254
- drawImage (getImage (), source .x , source .y , source .width , source .height , destination .x , destination .y , destination .width , destination .height , simple , getZoom ());
1296
+ long handle = Image .win32_getHandle (getImage (), getZoom ());
1297
+ drawImage (getImage (), source .x , source .y , source .width , source .height , destination .x , destination .y , destination .width , destination .height , simple , handle );
1255
1298
}
1256
1299
}
1257
1300
1258
- private void drawImage (Image srcImage , int srcX , int srcY , int srcWidth , int srcHeight , int destX , int destY , int destWidth , int destHeight , boolean simple , int imageZoom ) {
1301
+ private void drawImage (Image srcImage , int srcX , int srcY , int srcWidth , int srcHeight , int destX , int destY , int destWidth , int destHeight , boolean simple , long tempImageHandle ) {
1259
1302
if (data .gdipGraphics != 0 ) {
1260
1303
//TODO - cache bitmap
1261
- long [] gdipImage = srcImage .createGdipImage ( imageZoom );
1304
+ long [] gdipImage = srcImage .createGdipImageFromHandle ( tempImageHandle );
1262
1305
long img = gdipImage [0 ];
1263
1306
int imgWidth = Gdip .Image_GetWidth (img );
1264
1307
int imgHeight = Gdip .Image_GetHeight (img );
1308
+
1265
1309
if (srcWidth == 0 && srcHeight == 0 ) {
1266
1310
srcWidth = imgWidth ;
1267
1311
srcHeight = imgHeight ;
@@ -1316,13 +1360,13 @@ private void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int src
1316
1360
}
1317
1361
return ;
1318
1362
}
1319
- long imageHandle = srcImage .getHandle (imageZoom , data .nativeZoom );
1320
1363
switch (srcImage .type ) {
1321
1364
case SWT .BITMAP :
1322
- drawBitmap (srcImage , imageHandle , srcX , srcY , srcWidth , srcHeight , destX , destY , destWidth , destHeight , simple );
1365
+ drawBitmap (srcImage , tempImageHandle , srcX , srcY , srcWidth , srcHeight , destX , destY , destWidth , destHeight ,
1366
+ simple );
1323
1367
break ;
1324
1368
case SWT .ICON :
1325
- drawIcon (imageHandle , srcX , srcY , srcWidth , srcHeight , destX , destY , destWidth , destHeight , simple );
1369
+ drawIcon (tempImageHandle , srcX , srcY , srcWidth , srcHeight , destX , destY , destWidth , destHeight , simple );
1326
1370
break ;
1327
1371
}
1328
1372
}
0 commit comments