Skip to content

Commit 148ea46

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 00106b4 commit 148ea46

File tree

3 files changed

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

+167
-1
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,11 +1189,63 @@ 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 in the
1194+
* receiver. The image will be stretched or shrunk as needed to exactly fit the
1195+
* destination rectangle.
1196+
*
1197+
* @param image the source image
1198+
* @param destX the x coordinate in the destination
1199+
* @param destY the y coordinate in the destination
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
1204+
* <ul>
1205+
* <li>ERROR_NULL_ARGUMENT - if the image is
1206+
* null</li>
1207+
* <li>ERROR_INVALID_ARGUMENT - if the image
1208+
* has been disposed</li>
1209+
* <li>ERROR_INVALID_ARGUMENT - if any of
1210+
* the width or height arguments are
1211+
* negative.
1212+
* </ul>
1213+
* @exception SWTException
1214+
* <ul>
1215+
* <li>ERROR_GRAPHIC_DISPOSED - if the
1216+
* receiver has been disposed</li>
1217+
* </ul>
1218+
* @exception SWTError
1219+
* <ul>
1220+
* <li>ERROR_NO_HANDLES - if no handles are
1221+
* available to perform the operation</li>
1222+
* </ul>
1223+
* @since 3.132
1224+
*/
1225+
public void drawImage(Image image, int destX, int destY, int destWidth, int destHeight) {
1226+
if (handle == null)
1227+
SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
1228+
if (destWidth == 0 || destHeight == 0)
1229+
return;
1230+
if (destWidth < 0 || destHeight < 0) {
1231+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1232+
}
1233+
if (image == null)
1234+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
1235+
if (image.isDisposed())
1236+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1237+
drawImage(image, 0, 0, 0, 0, destX, destY, destWidth, destHeight, false);
1238+
}
1239+
11921240
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
11931241
NSImage imageHandle = srcImage.handle;
11941242
NSSize size = imageHandle.size();
11951243
int imgWidth = (int)size.width;
11961244
int imgHeight = (int)size.height;
1245+
if (srcWidth == 0 && srcHeight == 0) {
1246+
srcWidth = imgWidth;
1247+
srcHeight = imgHeight;
1248+
}
11971249
if (simple) {
11981250
srcWidth = destWidth = imgWidth;
11991251
srcHeight = destHeight = imgHeight;

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

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

871921
ImageData srcImageData = srcImage.getImageData();
872922
int imgWidth = srcImageData.width;
873923
int imgHeight = srcImageData.height;
924+
if (srcWidth == 0 && srcHeight == 0) {
925+
srcWidth = imgWidth;
926+
srcHeight = imgHeight;
927+
}
874928
if (simple) {
875929
srcWidth = destWidth = imgWidth;
876930
srcHeight = destHeight = imgHeight;

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,55 @@ 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 in the
1114+
* receiver. The image will be stretched or shrunk as needed to exactly fit the
1115+
* destination rectangle.
1116+
*
1117+
* @param image the source image
1118+
* @param destX the x coordinate in the destination
1119+
* @param destY the y coordinate in the destination
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
1124+
* <ul>
1125+
* <li>ERROR_NULL_ARGUMENT - if the image is
1126+
* null</li>
1127+
* <li>ERROR_INVALID_ARGUMENT - if the image
1128+
* has been disposed</li>
1129+
* <li>ERROR_INVALID_ARGUMENT - if any of
1130+
* the width or height arguments are
1131+
* negative.
1132+
* </ul>
1133+
* @exception SWTException
1134+
* <ul>
1135+
* <li>ERROR_GRAPHIC_DISPOSED - if the
1136+
* receiver has been disposed</li>
1137+
* </ul>
1138+
* @exception SWTError
1139+
* <ul>
1140+
* <li>ERROR_NO_HANDLES - if no handles are
1141+
* available to perform the operation</li>
1142+
* </ul>
1143+
* @since 3.132
1144+
*/
1145+
public void drawImage(Image image, int destX, int destY, int destWidth, int destHeight) {
1146+
checkNonDisposed();
1147+
if (destWidth == 0 || destHeight == 0)
1148+
return;
1149+
if (destWidth < 0 || destHeight < 0) {
1150+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1151+
}
1152+
if (image == null)
1153+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
1154+
if (image.isDisposed())
1155+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1156+
1157+
storeAndApplyOperationForExistingHandle(new DrawScalingImageToImageOperation(image, new Rectangle(0, 0, 0, 0),
1158+
new Rectangle(destX, destY, destWidth, destHeight)));
1159+
}
1160+
11121161
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
11131162
storeAndApplyOperationForExistingHandle(new DrawImageToImageOperation(srcImage, new Rectangle(srcX, srcY, srcWidth, srcHeight), new Rectangle(destX, destY, destWidth, destHeight), simple));
11141163
}
@@ -1213,7 +1262,10 @@ private void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int src
12131262
long img = gdipImage[0];
12141263
int imgWidth = Gdip.Image_GetWidth(img);
12151264
int imgHeight = Gdip.Image_GetHeight(img);
1216-
1265+
if (srcWidth == 0 && srcHeight == 0) {
1266+
srcWidth = imgWidth;
1267+
srcHeight = imgHeight;
1268+
}
12171269
if (simple) {
12181270
srcWidth = destWidth = imgWidth;
12191271
srcHeight = destHeight = imgHeight;
@@ -1315,6 +1367,10 @@ private void drawIcon(long imageHandle, int srcX, int srcY, int srcWidth, int sr
13151367
OS.GetObject(hBitmap, BITMAP.sizeof, bm);
13161368
int iconWidth = bm.bmWidth, iconHeight = bm.bmHeight;
13171369
if (hBitmap == srcIconInfo.hbmMask) iconHeight /= 2;
1370+
if (srcWidth == 0 && srcHeight == 0) {
1371+
srcWidth = iconWidth;
1372+
srcHeight = iconHeight;
1373+
}
13181374

13191375
if (simple) {
13201376
srcWidth = destWidth = iconWidth;
@@ -1410,6 +1466,10 @@ private void drawBitmap(Image srcImage, long imageHandle, int srcX, int srcY, in
14101466
OS.GetObject(imageHandle, BITMAP.sizeof, bm);
14111467
int imgWidth = bm.bmWidth;
14121468
int imgHeight = bm.bmHeight;
1469+
if (srcWidth == 0 && srcHeight == 0) {
1470+
srcWidth = imgWidth;
1471+
srcHeight = imgHeight;
1472+
}
14131473
if (simple) {
14141474
srcWidth = destWidth = imgWidth;
14151475
srcHeight = destHeight = imgHeight;

0 commit comments

Comments
 (0)