@@ -491,13 +491,38 @@ public void copyArea (Image image, int x, int y) {
491
491
storeAndApplyOperationForExistingHandle (new CopyAreaToImageOperation (image , x , y ));
492
492
}
493
493
494
- private class CopyAreaToImageOperation extends Operation {
495
- private final Image image ;
494
+ private abstract class ImageOperation extends Operation {
495
+ private Image image ;
496
+
497
+ ImageOperation (Image image ) {
498
+ setImage (image );
499
+ image .addOnDisposeListener (this ::setCopyOfImage );
500
+ }
501
+
502
+ private void setImage (Image image ) {
503
+ this .image = image ;
504
+ }
505
+
506
+ private void setCopyOfImage (Image image ) {
507
+ if (!GC .this .isDisposed ()) {
508
+ Image copiedImage = new Image (image .device , image , SWT .IMAGE_COPY );
509
+ setImage (copiedImage );
510
+ registerForDisposal (copiedImage );
511
+ }
512
+ }
513
+
514
+ protected Image getImage () {
515
+ return image ;
516
+ }
517
+
518
+ }
519
+
520
+ private class CopyAreaToImageOperation extends ImageOperation {
496
521
private final int x ;
497
522
private final int y ;
498
523
499
524
CopyAreaToImageOperation (Image image , int x , int y ) {
500
- this . image = image ;
525
+ super ( image ) ;
501
526
this .x = x ;
502
527
this .y = y ;
503
528
}
@@ -507,7 +532,7 @@ void apply() {
507
532
int zoom = getZoom ();
508
533
int scaledX = Win32DPIUtils .pointToPixel (drawable , this .x , zoom );
509
534
int scaledY = Win32DPIUtils .pointToPixel (drawable , this .y , zoom );
510
- copyAreaInPixels (this . image , scaledX , scaledY );
535
+ copyAreaInPixels (getImage () , scaledX , scaledY );
511
536
}
512
537
}
513
538
@@ -1013,18 +1038,17 @@ public void drawImage (Image image, int x, int y) {
1013
1038
storeAndApplyOperationForExistingHandle (new DrawImageOperation (image , new Point (x , y )));
1014
1039
}
1015
1040
1016
- private class DrawImageOperation extends Operation {
1017
- private final Image image ;
1041
+ private class DrawImageOperation extends ImageOperation {
1018
1042
private final Point location ;
1019
1043
1020
1044
DrawImageOperation (Image image , Point location ) {
1021
- this . image = image ;
1045
+ super ( image ) ;
1022
1046
this .location = location ;
1023
1047
}
1024
1048
1025
1049
@ Override
1026
1050
void apply () {
1027
- drawImageInPixels (this . image , Win32DPIUtils .pointToPixel (drawable , this .location , getZoom ()));
1051
+ drawImageInPixels (getImage () , Win32DPIUtils .pointToPixel (drawable , this .location , getZoom ()));
1028
1052
}
1029
1053
1030
1054
private void drawImageInPixels (Image image , Point location ) {
@@ -1081,13 +1105,12 @@ void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight,
1081
1105
storeAndApplyOperationForExistingHandle (new DrawImageToImageOperation (srcImage , new Rectangle (srcX , srcY , srcWidth , srcHeight ), new Rectangle (destX , destY , destWidth , destHeight ), simple ));
1082
1106
}
1083
1107
1084
- private class DrawScalingImageToImageOperation extends Operation {
1085
- private final Image image ;
1108
+ private class DrawScalingImageToImageOperation extends ImageOperation {
1086
1109
private final Rectangle source ;
1087
1110
private final Rectangle destination ;
1088
1111
1089
1112
DrawScalingImageToImageOperation (Image image , Rectangle source , Rectangle destination ) {
1090
- this . image = image ;
1113
+ super ( image ) ;
1091
1114
this .source = source ;
1092
1115
this .destination = destination ;
1093
1116
}
@@ -1096,7 +1119,7 @@ private class DrawScalingImageToImageOperation extends Operation {
1096
1119
void apply () {
1097
1120
int gcZoom = getZoom ();
1098
1121
int srcImageZoom = calculateZoomForImage (gcZoom , source .width , source .height , destination .width , destination .height );
1099
- drawImage (image , source .x , source .y , source .width , source .height , destination .x , destination .y , destination .width , destination .height , gcZoom , srcImageZoom );
1122
+ drawImage (getImage () , source .x , source .y , source .width , source .height , destination .x , destination .y , destination .width , destination .height , gcZoom , srcImageZoom );
1100
1123
}
1101
1124
1102
1125
private Collection <Integer > getAllCurrentMonitorZooms () {
@@ -1154,22 +1177,21 @@ private void drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHei
1154
1177
drawImage (image , src .x , src .y , src .width , src .height , dest .x , dest .y , dest .width , dest .height , false , scaledImageZoom );
1155
1178
}
1156
1179
1157
- private class DrawImageToImageOperation extends Operation {
1158
- private final Image image ;
1180
+ private class DrawImageToImageOperation extends ImageOperation {
1159
1181
private final Rectangle source ;
1160
1182
private final Rectangle destination ;
1161
1183
private final boolean simple ;
1162
1184
1163
1185
DrawImageToImageOperation (Image image , Rectangle source , Rectangle destination , boolean simple ) {
1164
- this . image = image ;
1186
+ super ( image ) ;
1165
1187
this .source = source ;
1166
1188
this .destination = destination ;
1167
1189
this .simple = simple ;
1168
1190
}
1169
1191
1170
1192
@ Override
1171
1193
void apply () {
1172
- drawImage (image , source .x , source .y , source .width , source .height , destination .x , destination .y , destination .width , destination .height , simple , getZoom ());
1194
+ drawImage (getImage () , source .x , source .y , source .width , source .height , destination .x , destination .y , destination .width , destination .height , simple , getZoom ());
1173
1195
}
1174
1196
}
1175
1197
0 commit comments