Skip to content

Commit e832de5

Browse files
arunjose696akoch-yatta
authored andcommitted
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 60de8f9 commit e832de5

File tree

5 files changed

+219
-6
lines changed

5 files changed

+219
-6
lines changed

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolderRenderer.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,7 @@ void drawBackground(GC gc, int[] shape, int x, int y, int width, int height, Col
698698
// draw the background image in shape
699699
gc.setBackground(defaultBackground);
700700
gc.fillRectangle(x, y, width, height);
701-
Rectangle imageRect = image.getBounds();
702-
gc.drawImage(image, imageRect.x, imageRect.y, imageRect.width, imageRect.height, x, y, width, height);
701+
gc.drawImage(image, x, y, width, height);
703702
} else if (colors != null) {
704703
// draw gradient
705704
if (colors.length == 1) {
@@ -1646,9 +1645,7 @@ void drawUnselected(int index, GC gc, Rectangle bounds, int state) {
16461645
int imageY = y + (height - imageHeight) / 2;
16471646
imageY += parent.onBottom ? -1 : 1;
16481647
int imageWidth = imageBounds.width * imageHeight / imageBounds.height;
1649-
gc.drawImage(image,
1650-
imageBounds.x, imageBounds.y, imageBounds.width, imageBounds.height,
1651-
imageX, imageY, imageWidth, imageHeight);
1648+
gc.drawImage(image, imageX, imageY, imageWidth, imageHeight);
16521649
xDraw += imageWidth + INTERNAL_SPACING;
16531650
}
16541651
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,11 +1189,67 @@ 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+
}
1229+
if (destWidth == 0 || destHeight == 0) {
1230+
return;
1231+
}
1232+
if (destWidth < 0 || destHeight < 0) {
1233+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1234+
}
1235+
if (image == null) {
1236+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
1237+
}
1238+
if (image.isDisposed()) {
1239+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1240+
}
1241+
drawImage(image, 0, 0, 0, 0, destX, destY, destWidth, destHeight, false);
1242+
}
1243+
11921244
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
11931245
NSImage imageHandle = srcImage.handle;
11941246
NSSize size = imageHandle.size();
11951247
int imgWidth = (int)size.width;
11961248
int imgHeight = (int)size.height;
1249+
if (srcWidth == 0 && srcHeight == 0) {
1250+
srcWidth = imgWidth;
1251+
srcHeight = imgHeight;
1252+
}
11971253
if (simple) {
11981254
srcWidth = destWidth = imgWidth;
11991255
srcHeight = destHeight = imgHeight;

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,13 +864,71 @@ 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+
}
905+
if (destWidth == 0 || destHeight == 0) {
906+
return;
907+
}
908+
if (destWidth < 0 || destHeight < 0) {
909+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
910+
}
911+
if (image == null) {
912+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
913+
}
914+
if (image.isDisposed()) {
915+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
916+
}
917+
Rectangle destRect = new Rectangle(destX, destY, destWidth, destHeight);
918+
drawImage(image, 0, 0, 0, 0, destRect.x, destRect.y, destRect.width, destRect.height, false);
919+
}
920+
867921
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
868922
/* Refresh Image as per zoom level, if required. */
869923
srcImage.refreshImageForZoom ();
870924

871925
ImageData srcImageData = srcImage.getImageData();
872926
int imgWidth = srcImageData.width;
873927
int imgHeight = srcImageData.height;
928+
if (srcWidth == 0 && srcHeight == 0) {
929+
srcWidth = imgWidth;
930+
srcHeight = imgHeight;
931+
}
874932
if (simple) {
875933
srcWidth = destWidth = imgWidth;
876934
srcHeight = destHeight = imgHeight;

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,58 @@ 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+
}
1150+
if (destWidth < 0 || destHeight < 0) {
1151+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1152+
}
1153+
if (image == null) {
1154+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
1155+
}
1156+
if (image.isDisposed()) {
1157+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1158+
}
1159+
1160+
storeAndApplyOperationForExistingHandle(new DrawScalingImageToImageOperation(image, new Rectangle(0, 0, 0, 0),
1161+
new Rectangle(destX, destY, destWidth, destHeight)));
1162+
}
1163+
11121164
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
11131165
storeAndApplyOperationForExistingHandle(new DrawImageToImageOperation(srcImage, new Rectangle(srcX, srcY, srcWidth, srcHeight), new Rectangle(destX, destY, destWidth, destHeight), simple));
11141166
}
@@ -1213,7 +1265,10 @@ private void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int src
12131265
long img = gdipImage[0];
12141266
int imgWidth = Gdip.Image_GetWidth(img);
12151267
int imgHeight = Gdip.Image_GetHeight(img);
1216-
1268+
if (srcWidth == 0 && srcHeight == 0) {
1269+
srcWidth = imgWidth;
1270+
srcHeight = imgHeight;
1271+
}
12171272
if (simple) {
12181273
srcWidth = destWidth = imgWidth;
12191274
srcHeight = destHeight = imgHeight;
@@ -1315,6 +1370,10 @@ private void drawIcon(long imageHandle, int srcX, int srcY, int srcWidth, int sr
13151370
OS.GetObject(hBitmap, BITMAP.sizeof, bm);
13161371
int iconWidth = bm.bmWidth, iconHeight = bm.bmHeight;
13171372
if (hBitmap == srcIconInfo.hbmMask) iconHeight /= 2;
1373+
if (srcWidth == 0 && srcHeight == 0) {
1374+
srcWidth = iconWidth;
1375+
srcHeight = iconHeight;
1376+
}
13181377

13191378
if (simple) {
13201379
srcWidth = destWidth = iconWidth;
@@ -1410,6 +1469,10 @@ private void drawBitmap(Image srcImage, long imageHandle, int srcX, int srcY, in
14101469
OS.GetObject(imageHandle, BITMAP.sizeof, bm);
14111470
int imgWidth = bm.bmWidth;
14121471
int imgHeight = bm.bmHeight;
1472+
if (srcWidth == 0 && srcHeight == 0) {
1473+
srcWidth = imgWidth;
1474+
srcHeight = imgHeight;
1475+
}
14131476
if (simple) {
14141477
srcWidth = destWidth = imgWidth;
14151478
srcHeight = destHeight = imgHeight;

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,45 @@ public void test_drawImageLorg_eclipse_swt_graphics_ImageIIIIIIII() {
378378
imageTransparent.dispose();
379379
}
380380

381+
@Test
382+
public void test_drawImageLorg_eclipse_swt_graphics_ImageIIII() {
383+
Color c1 = new Color(255, 0, 0);
384+
Color c2 = new Color(0, 0, 0);
385+
Color c3 = new Color(255, 255, 0);
386+
387+
PaletteData paletteData = new PaletteData(c1.getRGB(), c2.getRGB(), c3.getRGB());
388+
ImageData data = new ImageData(30, 30, 8, paletteData);
389+
for (int y = 0; y < data.height; y++) {
390+
for (int x = 0; x < data.width; x++) {
391+
if (x > y)
392+
data.setPixel(x, y, paletteData.getPixel(c1.getRGB()));
393+
else if (x < y)
394+
data.setPixel(x, y, paletteData.getPixel(c2.getRGB()));
395+
else
396+
data.setPixel(x, y, paletteData.getPixel(c3.getRGB()));
397+
}
398+
}
399+
Image image = new Image(display, data);
400+
data = image.getImageData();
401+
data.transparentPixel = paletteData.getPixel(c1.getRGB());
402+
Image imageTransparent = new Image(display, data);
403+
data.transparentPixel = -1;
404+
for (int y = 0; y < data.height; y++) {
405+
for (int x = 0; x < data.width; x++) {
406+
data.setAlpha(x, y, 127);
407+
}
408+
}
409+
Image imageAlpha = new Image(display, data);
410+
411+
gc.drawImage(image, 100, 120, 50, 60);
412+
gc.drawImage(imageTransparent, 100, 120, 10, 10);
413+
gc.drawImage(imageAlpha, 100, 120, 20, 15);
414+
assertThrows(IllegalArgumentException.class, () -> gc.drawImage(null, 100, 120, 50, 60));
415+
image.dispose();
416+
imageAlpha.dispose();
417+
imageTransparent.dispose();
418+
}
419+
381420
@Test
382421
public void test_drawLineIIII() {
383422
gc.drawLine(0,0,0,20);

0 commit comments

Comments
 (0)