Skip to content

Commit 1010784

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 1010784

File tree

3 files changed

+120
-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

+120
-1
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,11 +1189,50 @@ 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 the full source image into a specified rectangular area
1194+
* in the receiver. The image will be stretched or shrunk as needed
1195+
* to exactly fit the destination rectangle.
1196+
*
1197+
* @param image the source image
1198+
* @param destX the x coordinate in the destination to copy to
1199+
* @param destY the y coordinate in the destination to copy to
1200+
* @param destWidth the width in points of the destination rectangle
1201+
* @param destHeight the height in points of the destination rectangle
1202+
*
1203+
* @exception IllegalArgumentException <ul>
1204+
* <li>ERROR_NULL_ARGUMENT - if the image is null</li>
1205+
* <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
1206+
* <li>ERROR_INVALID_ARGUMENT - if any of the width or height arguments are negative.
1207+
* </ul>
1208+
* @exception SWTException <ul>
1209+
* <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
1210+
* </ul>
1211+
* @exception SWTError <ul>
1212+
* <li>ERROR_NO_HANDLES - if no handles are available to perform the operation</li>
1213+
* </ul>
1214+
* @since 3.132
1215+
*/
1216+
public void drawImage (Image image, int destX, int destY, int destWidth, int destHeight) {
1217+
if (handle == null) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
1218+
if (destWidth == 0 || destHeight == 0) return;
1219+
if (destWidth < 0 || destHeight < 0) {
1220+
SWT.error (SWT.ERROR_INVALID_ARGUMENT);
1221+
}
1222+
if (image == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
1223+
if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1224+
drawImage(image, 0, 0, 0, 0, destX, destY, destWidth, destHeight, false);
1225+
}
1226+
11921227
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
11931228
NSImage imageHandle = srcImage.handle;
11941229
NSSize size = imageHandle.size();
11951230
int imgWidth = (int)size.width;
11961231
int imgHeight = (int)size.height;
1232+
if (srcWidth == 0 && srcHeight == 0) {
1233+
srcWidth = imgWidth;
1234+
srcHeight = imgHeight;
1235+
}
11971236
if (simple) {
11981237
srcWidth = destWidth = imgWidth;
11991238
srcHeight = destHeight = imgHeight;

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,13 +864,54 @@ 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 the full source image into a specified rectangular area
870+
* in the receiver. The image will be stretched or shrunk as needed
871+
* to exactly fit the destination rectangle.
872+
*
873+
* @param image the source image
874+
* @param destX the x coordinate in the destination to copy to
875+
* @param destY the y coordinate in the destination to copy to
876+
* @param destWidth the width in points of the destination rectangle
877+
* @param destHeight the height in points of the destination rectangle
878+
*
879+
* @exception IllegalArgumentException <ul>
880+
* <li>ERROR_NULL_ARGUMENT - if the image is null</li>
881+
* <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
882+
* <li>ERROR_INVALID_ARGUMENT - if any of the width or height arguments are negative.
883+
* </ul>
884+
* @exception SWTException <ul>
885+
* <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
886+
* </ul>
887+
* @exception SWTError <ul>
888+
* <li>ERROR_NO_HANDLES - if no handles are available to perform the operation</li>
889+
* </ul>
890+
* @since 3.132
891+
*/
892+
public void drawImage (Image image, int destX, int destY, int destWidth, int destHeight) {
893+
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
894+
if (destWidth == 0 || destHeight == 0) return;
895+
if (destWidth < 0 || destHeight < 0) {
896+
SWT.error (SWT.ERROR_INVALID_ARGUMENT);
897+
}
898+
if (image == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
899+
if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
900+
Rectangle destRect = new Rectangle(destX, destY, destWidth, destHeight);
901+
drawImage(image, 0, 0, 0, 0, destRect.x, destRect.y, destRect.width, destRect.height, false);
902+
}
903+
867904
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
868905
/* Refresh Image as per zoom level, if required. */
869906
srcImage.refreshImageForZoom ();
870907

871908
ImageData srcImageData = srcImage.getImageData();
872909
int imgWidth = srcImageData.width;
873910
int imgHeight = srcImageData.height;
911+
if (srcWidth == 0 && srcHeight == 0) {
912+
srcWidth = imgWidth;
913+
srcHeight = imgHeight;
914+
}
874915
if (simple) {
875916
srcWidth = destWidth = imgWidth;
876917
srcHeight = destHeight = imgHeight;

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,42 @@ 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 the full source image into a specified rectangular area
1114+
* in the receiver. The image will be stretched or shrunk as needed
1115+
* to exactly fit the destination rectangle.
1116+
*
1117+
* @param image the source image
1118+
* @param destX the x coordinate in the destination to copy to
1119+
* @param destY the y coordinate in the destination to copy to
1120+
* @param destWidth the width in points of the destination rectangle
1121+
* @param destHeight the height in points of the destination rectangle
1122+
*
1123+
* @exception IllegalArgumentException <ul>
1124+
* <li>ERROR_NULL_ARGUMENT - if the image is null</li>
1125+
* <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
1126+
* <li>ERROR_INVALID_ARGUMENT - if any of the width or height arguments are negative.
1127+
* </ul>
1128+
* @exception SWTException <ul>
1129+
* <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
1130+
* </ul>
1131+
* @exception SWTError <ul>
1132+
* <li>ERROR_NO_HANDLES - if no handles are available to perform the operation</li>
1133+
* </ul>
1134+
* @since 3.132
1135+
*/
1136+
public void drawImage (Image image, int destX, int destY, int destWidth, int destHeight) {
1137+
checkNonDisposed();
1138+
if (destWidth == 0 || destHeight == 0) return;
1139+
if (destWidth < 0 || destHeight < 0) {
1140+
SWT.error (SWT.ERROR_INVALID_ARGUMENT);
1141+
}
1142+
if (image == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
1143+
if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1144+
1145+
storeAndApplyOperationForExistingHandle(new DrawScalingImageToImageOperation(image, new Rectangle(0, 0, 0, 0), new Rectangle(destX, destY, destWidth, destHeight)));
1146+
}
1147+
11121148
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
11131149
storeAndApplyOperationForExistingHandle(new DrawImageToImageOperation(srcImage, new Rectangle(srcX, srcY, srcWidth, srcHeight), new Rectangle(destX, destY, destWidth, destHeight), simple));
11141150
}
@@ -1213,7 +1249,10 @@ private void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int src
12131249
long img = gdipImage[0];
12141250
int imgWidth = Gdip.Image_GetWidth(img);
12151251
int imgHeight = Gdip.Image_GetHeight(img);
1216-
1252+
if(srcWidth==0 && srcHeight==0) {
1253+
srcWidth = imgWidth;
1254+
srcHeight = imgHeight;
1255+
}
12171256
if (simple) {
12181257
srcWidth = destWidth = imgWidth;
12191258
srcHeight = destHeight = imgHeight;

0 commit comments

Comments
 (0)