1 /*
2 * Copyright (c) 2015, The JUNG Authors
3 * All rights reserved.
4 *
5 * This software is open-source under the BSD license; see either "license.txt"
6 * or https://github.com/jrtom/jung/blob/master/LICENSE for a description.
7 *
8 * Created on Nov 7, 2015
9 */
10
11 package edu.uci.ics.jung.visualization.util;
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.image.BufferedImage;
19 import java.io.IOException;
20
21 import javax.imageio.ImageIO;
22
23 import edu.uci.ics.jung.visualization.FourPassImageShaper;
24
25 public class ImageShapeUtils {
26
27 /**
28 * Given the fileName of an image, possibly with a transparent
29 * background, return the Shape of the opaque part of the image
30 * @param fileName name of the image, loaded from the classpath
31 * @return the Shape
32 */
33 public static Shape getShape(String fileName) {
34 return getShape(fileName, Integer.MAX_VALUE);
35 }
36
37 /**
38 * Given the fileName of an image, possibly with a transparent
39 * background, return the Shape of the opaque part of the image
40 * @param fileName name of the image, loaded from the classpath
41 * @param max the maximum dimension of the traced shape
42 * @return the Shape
43 *
44 * @see #getShape(Image, int)
45 */
46 public static Shape getShape(String fileName, int max) {
47 BufferedImage image = null;
48 try {
49 image = ImageIO.read(ImageShapeUtils.class.getResource(fileName));
50 } catch(IOException ex) {
51 ex.printStackTrace();
52 }
53 return getShape(image, max);
54 }
55
56 /**
57 * Given an image, possibly with a transparent background, return
58 * the Shape of the opaque part of the image
59 * @param image the image whose shape is to be returned
60 * @return the Shape
61 */
62 public static Shape getShape(Image image) {
63 return getShape(image, Integer.MAX_VALUE);
64 }
65 public static Shape getShape(Image image, int max) {
66 BufferedImage bi =
67 new BufferedImage(image.getWidth(null), image.getHeight(null),
68 BufferedImage.TYPE_INT_ARGB);
69 Graphics g = bi.createGraphics();
70 g.drawImage(image, 0, 0, null);
71 g.dispose();
72 return getShape(bi, max);
73 }
74
75 /**
76 * Given an image, possibly with a transparent background, return
77 * the Shape of the opaque part of the image
78 *
79 * If the image is larger than max in either direction, scale the
80 * image down to max-by-max, do the trace (on fewer points) then
81 * scale the resulting shape back up to the size of the original
82 * image.
83 *
84 * @param image the image to trace
85 * @param max used to restrict number of points in the resulting shape
86 * @return the Shape
87 */
88 public static Shape getShape(BufferedImage image, int max) {
89 float width = image.getWidth();
90 float height = image.getHeight();
91 if(width > max || height > max) {
92 BufferedImage smaller =
93 new BufferedImage(max, max, BufferedImage.TYPE_INT_ARGB);
94 Graphics g = smaller.createGraphics();
95 AffineTransform at = AffineTransform.getScaleInstance(max/width,max/height);
96 AffineTransform back = AffineTransform.getScaleInstance(width/max,height/max);
97 Graphics2D g2 = (Graphics2D)g;
98 g2.drawImage(image, at, null);
99 g2.dispose();
100 return back.createTransformedShape(getShape(smaller));
101 } else {
102 return FourPassImageShaper.getShape(image);
103 }
104 }
105 }