Skip to content

Commit 7779640

Browse files
committed
Add drawImage API that takes only a destination rectangle
Introduces a new method that draws the full image into a specified destination rectangle without requiring source bounds. Consumers no longer need to call image.getBounds() to determine image dimensions, unlike the existing drawImage method (Image image, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight), which required knowing the image size in advance.
1 parent f38e115 commit 7779640

File tree

3 files changed

+129
-1
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT
    • cocoa/org/eclipse/swt/graphics
    • gtk/org/eclipse/swt/graphics
    • win32/org/eclipse/swt/graphics

3 files changed

+129
-1
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,11 +1189,53 @@ public void drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeig
11891189
drawImage(image, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, false);
11901190
}
11911191

1192+
/**
1193+
* Draws a image into a (potentially
1194+
* different sized) rectangular area in the receiver. If the source
1195+
* and destination areas are of differing sizes, then the source
1196+
* area will be stretched or shrunk to fit the destination area
1197+
* as it is copied. The copy fails if any part of the source rectangle
1198+
* lies outside the bounds of the source image, or if any of the width
1199+
* or height arguments are negative.
1200+
*
1201+
* @param image the source image
1202+
* @param destX the x coordinate in the destination to copy to
1203+
* @param destY the y coordinate in the destination to copy to
1204+
* @param destWidth the width in points of the destination rectangle
1205+
* @param destHeight the height in points of the destination rectangle
1206+
*
1207+
* @exception IllegalArgumentException <ul>
1208+
* <li>ERROR_NULL_ARGUMENT - if the image is null</li>
1209+
* <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
1210+
* <li>ERROR_INVALID_ARGUMENT - if any of the width or height arguments are negative.
1211+
* </ul>
1212+
* @exception SWTException <ul>
1213+
* <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
1214+
* </ul>
1215+
* @exception SWTError <ul>
1216+
* <li>ERROR_NO_HANDLES - if no handles are available to perform the operation</li>
1217+
* </ul>
1218+
*/
1219+
public void drawImage (Image image, int destX, int destY, int destWidth, int destHeight) {
1220+
if (handle == null) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
1221+
if (destWidth == 0 || destHeight == 0) return;
1222+
if (destWidth < 0 || destHeight < 0) {
1223+
SWT.error (SWT.ERROR_INVALID_ARGUMENT);
1224+
}
1225+
if (image == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
1226+
if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1227+
drawImage(image, 0, 0, 0, 0, destX, destY, destWidth, destHeight, false);
1228+
}
1229+
11921230
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
11931231
NSImage imageHandle = srcImage.handle;
11941232
NSSize size = imageHandle.size();
11951233
int imgWidth = (int)size.width;
11961234
int imgHeight = (int)size.height;
1235+
if (srcWidth == 0 && srcHeight == 0) {
1236+
srcWidth = imgWidth;
1237+
srcHeight = imgHeight;
1238+
}
11971239
if (simple) {
11981240
srcWidth = destWidth = imgWidth;
11991241
srcHeight = destHeight = imgHeight;

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,13 +864,57 @@ public void drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeig
864864
Rectangle destRect = new Rectangle(destX, destY, destWidth, destHeight);
865865
drawImage(image, srcX, srcY, srcWidth, srcHeight, destRect.x, destRect.y, destRect.width, destRect.height, false);
866866
}
867+
868+
/**
869+
* Draws a image into a (potentially
870+
* different sized) rectangular area in the receiver. If the source
871+
* and destination areas are of differing sizes, then the source
872+
* area will be stretched or shrunk to fit the destination area
873+
* as it is copied. The copy fails if any part of the source rectangle
874+
* lies outside the bounds of the source image, or if any of the width
875+
* or height arguments are negative.
876+
*
877+
* @param image the source image
878+
* @param destX the x coordinate in the destination to copy to
879+
* @param destY the y coordinate in the destination to copy to
880+
* @param destWidth the width in points of the destination rectangle
881+
* @param destHeight the height in points of the destination rectangle
882+
*
883+
* @exception IllegalArgumentException <ul>
884+
* <li>ERROR_NULL_ARGUMENT - if the image is null</li>
885+
* <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
886+
* <li>ERROR_INVALID_ARGUMENT - if any of the width or height arguments are negative.
887+
* </ul>
888+
* @exception SWTException <ul>
889+
* <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
890+
* </ul>
891+
* @exception SWTError <ul>
892+
* <li>ERROR_NO_HANDLES - if no handles are available to perform the operation</li>
893+
* </ul>
894+
*/
895+
public void drawImage (Image image, int destX, int destY, int destWidth, int destHeight) {
896+
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
897+
if (destWidth == 0 || destHeight == 0) return;
898+
if (destWidth < 0 || destHeight < 0) {
899+
SWT.error (SWT.ERROR_INVALID_ARGUMENT);
900+
}
901+
if (image == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
902+
if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
903+
Rectangle destRect = new Rectangle(destX, destY, destWidth, destHeight);
904+
drawImage(image, 0, 0, 0, 0, destRect.x, destRect.y, destRect.width, destRect.height, false);
905+
}
906+
867907
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
868908
/* Refresh Image as per zoom level, if required. */
869909
srcImage.refreshImageForZoom ();
870910

871911
ImageData srcImageData = srcImage.getImageData();
872912
int imgWidth = srcImageData.width;
873913
int imgHeight = srcImageData.height;
914+
if (srcWidth == 0 && srcHeight == 0) {
915+
srcWidth = imgWidth;
916+
srcHeight = imgHeight;
917+
}
874918
if (simple) {
875919
srcWidth = destWidth = imgWidth;
876920
srcHeight = destHeight = imgHeight;

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,45 @@ public void drawImage (Image image, int srcX, int srcY, int srcWidth, int srcHei
11091109
storeAndApplyOperationForExistingHandle(new DrawScalingImageToImageOperation(image, new Rectangle(srcX, srcY, srcWidth, srcHeight), new Rectangle(destX, destY, destWidth, destHeight)));
11101110
}
11111111

1112+
/**
1113+
* Draws a image into a (potentially
1114+
* different sized) rectangular area in the receiver. If the source
1115+
* and destination areas are of differing sizes, then the source
1116+
* area will be stretched or shrunk to fit the destination area
1117+
* as it is copied. The copy fails if any part of the source rectangle
1118+
* lies outside the bounds of the source image, or if any of the width
1119+
* or height arguments are negative.
1120+
*
1121+
* @param image the source image
1122+
* @param destX the x coordinate in the destination to copy to
1123+
* @param destY the y coordinate in the destination to copy to
1124+
* @param destWidth the width in points of the destination rectangle
1125+
* @param destHeight the height in points of the destination rectangle
1126+
*
1127+
* @exception IllegalArgumentException <ul>
1128+
* <li>ERROR_NULL_ARGUMENT - if the image is null</li>
1129+
* <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
1130+
* <li>ERROR_INVALID_ARGUMENT - if any of the width or height arguments are negative.
1131+
* </ul>
1132+
* @exception SWTException <ul>
1133+
* <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
1134+
* </ul>
1135+
* @exception SWTError <ul>
1136+
* <li>ERROR_NO_HANDLES - if no handles are available to perform the operation</li>
1137+
* </ul>
1138+
*/
1139+
public void drawImage (Image image, int destX, int destY, int destWidth, int destHeight) {
1140+
checkNonDisposed();
1141+
if (destWidth == 0 || destHeight == 0) return;
1142+
if (destWidth < 0 || destHeight < 0) {
1143+
SWT.error (SWT.ERROR_INVALID_ARGUMENT);
1144+
}
1145+
if (image == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
1146+
if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1147+
1148+
storeAndApplyOperationForExistingHandle(new DrawScalingImageToImageOperation(image, new Rectangle(0, 0, 0, 0), new Rectangle(destX, destY, destWidth, destHeight)));
1149+
}
1150+
11121151
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
11131152
storeAndApplyOperationForExistingHandle(new DrawImageToImageOperation(srcImage, new Rectangle(srcX, srcY, srcWidth, srcHeight), new Rectangle(destX, destY, destWidth, destHeight), simple));
11141153
}
@@ -1213,7 +1252,10 @@ private void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int src
12131252
long img = gdipImage[0];
12141253
int imgWidth = Gdip.Image_GetWidth(img);
12151254
int imgHeight = Gdip.Image_GetHeight(img);
1216-
1255+
if(srcWidth==0 && srcHeight==0) {
1256+
srcWidth = imgWidth;
1257+
srcHeight = imgHeight;
1258+
}
12171259
if (simple) {
12181260
srcWidth = destWidth = imgWidth;
12191261
srcHeight = destHeight = imgHeight;

0 commit comments

Comments
 (0)