1
2
3
4
5
6
7
8
9
10
11 package edu.uci.ics.jung.visualization;
12
13 import java.awt.Graphics;
14 import java.awt.Graphics2D;
15 import java.awt.Image;
16 import java.awt.Shape;
17 import java.awt.geom.AffineTransform;
18 import java.awt.geom.Area;
19 import java.awt.geom.GeneralPath;
20 import java.awt.geom.Line2D;
21 import java.awt.geom.Point2D;
22 import java.awt.image.BufferedImage;
23 import java.io.IOException;
24
25 import javax.imageio.ImageIO;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class FourPassImageShaper {
42
43
44
45
46
47
48
49 public static Shape getShape(String fileName) {
50 return getShape(fileName, Integer.MAX_VALUE);
51 }
52 public static Shape getShape(String fileName, int max) {
53 BufferedImage image = null;
54 try {
55 image = ImageIO.read(FourPassImageShaper.class.getResource(fileName));
56 } catch(IOException ex) {
57 ex.printStackTrace();
58 }
59 return getShape(image, max);
60 }
61
62
63
64
65
66
67
68 public static Shape getShape(Image image) {
69 return getShape(image, Integer.MAX_VALUE);
70 }
71 public static Shape getShape(Image image, int max) {
72 BufferedImage bi =
73 new BufferedImage(image.getWidth(null), image.getHeight(null),
74 BufferedImage.TYPE_INT_ARGB);
75 Graphics g = bi.createGraphics();
76 g.drawImage(image, 0, 0, null);
77 g.dispose();
78 return getShape(bi, max);
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public static Shape getShape(BufferedImage image, int max) {
95
96 float width = image.getWidth();
97 float height = image.getHeight();
98 if(width > max || height > max) {
99 BufferedImage smaller =
100 new BufferedImage(max, max, BufferedImage.TYPE_INT_ARGB);
101 Graphics g = smaller.createGraphics();
102 AffineTransform at = AffineTransform.getScaleInstance(max/width,max/height);
103 AffineTransform back = AffineTransform.getScaleInstance(width/max,height/max);
104 Graphics2D g2 = (Graphics2D)g;
105 g2.drawImage(image, at, null);
106 g2.dispose();
107 return back.createTransformedShape(getShape(smaller));
108 } else {
109 return getShape(image);
110 }
111 }
112
113 public static Shape getShape(BufferedImage image) {
114 Area area = new Area(leftEdge(image));
115 area.intersect(new Area(bottomEdge(image)));
116 area.intersect(new Area(rightEdge(image)));
117 area.intersect(new Area(topEdge(image)));
118 return area;
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 private static Point2D detectLine(Point2D p1, Point2D p2, Point2D p,
135 Line2D line, GeneralPath path) {
136 if(p2 == null) {
137 p2 = p;
138 line.setLine(p1,p2);
139 }
140
141 else if(line.ptLineDistSq(p) < 1) {
142
143 p2.setLocation(p);
144 } else {
145 p1.setLocation(p2);
146 p2.setLocation(p);
147 line.setLine(p1,p2);
148 path.lineTo((float)p1.getX(), (float)p1.getY());
149 }
150 return p2;
151 }
152
153
154
155
156
157
158 private static Shape leftEdge(BufferedImage image) {
159 GeneralPath path = new GeneralPath();
160 Point2D p1 = new Point2D.Float(image.getWidth()-1, 0);
161 Point2D p2 = null;
162 Line2D line = new Line2D.Float();
163 Point2D p = new Point2D.Float();
164 path.moveTo(image.getWidth()-1, 0);
165
166 for(int i=0; i<image.getHeight(); i++) {
167 p.setLocation(image.getWidth()-1, i);
168
169 for(int j=0; j<image.getWidth(); j++) {
170 if((image.getRGB(j,i) & 0xff000000) != 0) {
171
172 p.setLocation(j,i);
173 break;
174 }
175 }
176 p2 = detectLine(p1, p2, p, line, path);
177 }
178 p.setLocation(image.getWidth()-1, image.getHeight()-1);
179 detectLine(p1, p2, p, line, path);
180 path.closePath();
181 return path;
182 }
183
184
185
186
187
188
189
190
191 private static Shape bottomEdge(BufferedImage image) {
192 GeneralPath path = new GeneralPath();
193 Point2D p1 = new Point2D.Float(0, 0);
194 Point2D p2 = null;
195 Line2D line = new Line2D.Float();
196 Point2D p = new Point2D.Float();
197 path.moveTo(0, 0);
198 for(int i=0; i<image.getWidth(); i++) {
199 p.setLocation(i, 0);
200 for(int j=image.getHeight()-1; j>=0; j--) {
201 if((image.getRGB(i,j) & 0xff000000) != 0) {
202
203 p.setLocation(i,j);
204 break;
205 }
206 }
207 p2 = detectLine(p1, p2, p, line, path);
208 }
209 p.setLocation(image.getWidth()-1, 0);
210 detectLine(p1, p2, p, line, path);
211 path.closePath();
212 return path;
213 }
214
215
216
217
218
219
220
221
222 private static Shape rightEdge(BufferedImage image) {
223 GeneralPath path = new GeneralPath();
224 Point2D p1 = new Point2D.Float(0, image.getHeight()-1);
225 Point2D p2 = null;
226 Line2D line = new Line2D.Float();
227 Point2D p = new Point2D.Float();
228 path.moveTo(0, image.getHeight()-1);
229
230 for(int i=image.getHeight()-1; i>=0; i--) {
231 p.setLocation(0, i);
232 for(int j=image.getWidth()-1; j>=0; j--) {
233 if((image.getRGB(j,i) & 0xff000000) != 0) {
234
235 p.setLocation(j,i);
236 break;
237 }
238 }
239 p2 = detectLine(p1, p2, p, line, path);
240 }
241 p.setLocation(0, 0);
242 detectLine(p1, p2, p,line, path);
243 path.closePath();
244 return path;
245 }
246
247
248
249
250
251
252
253
254 private static Shape topEdge(BufferedImage image) {
255 GeneralPath path = new GeneralPath();
256 Point2D p1 = new Point2D.Float(image.getWidth()-1, image.getHeight()-1);
257 Point2D p2 = null;
258 Line2D line = new Line2D.Float();
259 Point2D p = new Point2D.Float();
260 path.moveTo(image.getWidth()-1, image.getHeight()-1);
261
262 for(int i=image.getWidth()-1; i>=0; i--) {
263 p.setLocation(i, image.getHeight()-1);
264 for(int j=0; j<image.getHeight(); j++) {
265 if((image.getRGB(i,j) & 0xff000000) != 0) {
266
267 p.setLocation(i,j);
268 break;
269 }
270 }
271 p2 = detectLine(p1, p2, p, line, path);
272 }
273 p.setLocation(0, image.getHeight()-1);
274 detectLine(p1, p2, p, line, path);
275 path.closePath();
276 return path;
277 }
278 }