Skip to content

Commit 960b880

Browse files
committed
- Implemented getSubimage method to subset images. Allows users to specify coordinates outside the image bounds.
git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@656 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 760759c commit 960b880

File tree

1 file changed

+107
-5
lines changed

1 file changed

+107
-5
lines changed

src/javaxt/io/Image.java

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ else if(y>h){
471471
g2d.drawImage(img, x2, y2, null);
472472
img = in;
473473
g2d.drawImage(img, x, y, null);
474+
g2d.dispose();
474475
bufferedImage = bi;
475476
}
476477
else{
@@ -935,17 +936,15 @@ public void flip(){
935936
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BICUBIC);
936937
bufferedImage = op.filter(bufferedImage, out);
937938
}
938-
939-
940939

941940

942941
//**************************************************************************
943942
//** Crop
944943
//**************************************************************************
945944
/** Used to subset or crop an image. */
946-
945+
947946
public void crop(int x, int y, int width, int height){
948-
bufferedImage = bufferedImage.getSubimage(x,y,width,height);
947+
bufferedImage = getSubimage(x,y,width,height);
949948
}
950949

951950

@@ -965,7 +964,110 @@ public Image copy(){
965964
/** Returns a copy of a given rectangle. */
966965

967966
public Image copyRect(int x, int y, int width, int height){
968-
return new Image(bufferedImage.getSubimage(x,y,width,height));
967+
return new Image(getSubimage(x,y,width,height));
968+
}
969+
970+
971+
//**************************************************************************
972+
//** getSubimage
973+
//**************************************************************************
974+
/** Returns a copy of a given rectangle. */
975+
976+
private BufferedImage getSubimage(int x, int y, int width, int height){
977+
978+
Rectangle rect1 = new Rectangle(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
979+
Rectangle rect2 = new Rectangle(x, y, width, height);
980+
981+
982+
//If the requested rectangle falls inside the image bounds, simply return
983+
//the subimage
984+
if (rect1.contains(rect2)){
985+
return (bufferedImage.getSubimage(x,y,width,height));
986+
}
987+
else{ //requested rectangle falls outside the image bounds!
988+
989+
990+
991+
//Create buffered image
992+
int imageType = bufferedImage.getType();
993+
if (imageType == 0 || imageType == 12) {
994+
imageType = BufferedImage.TYPE_INT_ARGB;
995+
}
996+
BufferedImage bi = new BufferedImage(width, height, imageType);
997+
998+
999+
//If the requested rectangle intersects the image bounds, crop the
1000+
//the source image and insert it into the BufferedImage
1001+
if (rect1.intersects(rect2)){
1002+
1003+
Graphics2D g2d = bi.createGraphics();
1004+
BufferedImage subImage = null;
1005+
int X;
1006+
int Y;
1007+
1008+
if (x<0){
1009+
int x1 = 0;
1010+
int y1;
1011+
int h;
1012+
int w;
1013+
1014+
if (y<0){
1015+
y1 = 0;
1016+
h = y+height;
1017+
Y = height - h;
1018+
}
1019+
else{
1020+
y1 = y;
1021+
h = height;
1022+
Y = 0;
1023+
}
1024+
1025+
if (h+y1>bufferedImage.getHeight()) h = bufferedImage.getHeight()-y1;
1026+
1027+
w = x+width;
1028+
if (w>bufferedImage.getWidth()) w = bufferedImage.getWidth();
1029+
1030+
1031+
subImage = bufferedImage.getSubimage(x1,y1,w,h);
1032+
1033+
X = width - w;
1034+
}
1035+
else{
1036+
int x1 = x;
1037+
int y1;
1038+
int h;
1039+
int w;
1040+
1041+
if (y<0){
1042+
y1 = 0;
1043+
h = y+height;
1044+
Y = height - h;
1045+
}
1046+
else{
1047+
y1 = y;
1048+
h = height;
1049+
Y = 0;
1050+
}
1051+
1052+
if (h+y1>bufferedImage.getHeight()) h = bufferedImage.getHeight()-y1;
1053+
1054+
w = width;
1055+
if (w+x1>bufferedImage.getWidth()) w = bufferedImage.getWidth()-x1;
1056+
1057+
X = 0;
1058+
1059+
subImage = bufferedImage.getSubimage(x1,y1,w,h);
1060+
}
1061+
1062+
1063+
g2d.drawImage(subImage, X, Y, null);
1064+
g2d.dispose();
1065+
1066+
}
1067+
1068+
return bi;
1069+
}
1070+
9691071
}
9701072

9711073

0 commit comments

Comments
 (0)