Coverage details for edu.uci.ics.jung.visualization.PivotingImageShaper

LineHitsSource
1 /*
2  * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  *
8  * Created on Jun 17, 2005
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.GeneralPath;
19 import java.awt.geom.Line2D;
20 import java.awt.geom.Point2D;
21 import java.awt.image.BufferedImage;
22 import java.io.IOException;
23  
24 import javax.imageio.ImageIO;
25  
26  
27 /**
28  * Provides factory methods that, given a BufferedImage, an Image,
29  * or the fileName of an image, will return a java.awt.Shape that
30  * is the contiguous traced outline of the opaque part of the image.
31  * This could be used to define an image for use in a Vertex, where
32  * the shape used for picking and edge-arrow placement follows the
33  * opaque part of an image that has a transparent background.
34  * The methods try to detect lines in order to minimize points
35  * in the path
36  *
37  * @author Tom Nelson - RABA Technologies
38  *
39  *
40  */
410public class PivotingImageShaper {
42     
43     /**
44      * the number of pixels to skip while sampling the
45      * images edges
46      */
470    static int sample = 1;
48     /**
49      * the first x coordinate of the shape. Used to discern
50      * when we are done
51      */
520    static int firstx = 0;
53     
54     public static Shape getShape(String fileName) {
550        return getShape(fileName, Integer.MAX_VALUE);
56     }
57     public static Shape getShape(String fileName, int max) {
580        BufferedImage image = null;
59         try {
600            image = ImageIO.read(FourPassImageShaper.class.getResource(fileName));
610        } catch(IOException ex) {
620            ex.printStackTrace();
630        }
640        return getShape(image, max);
65     }
66     
67     /**
68      * Given an image, possibly with a transparent background, return
69      * the Shape of the opaque part of the image
70      * @param image
71      * @return the Shape
72      */
73     public static Shape getShape(Image image) {
740        return getShape(image, Integer.MAX_VALUE);
75     }
76     public static Shape getShape(Image image, int max) {
770        BufferedImage bi =
78             new BufferedImage(image.getWidth(null), image.getHeight(null),
79                     BufferedImage.TYPE_INT_ARGB);
800        Graphics g = bi.createGraphics();
810        g.drawImage(image, 0, 0, null);
820        g.dispose();
830        return getShape(bi, max);
84     }
85     
86     /**
87      * Given an image, possibly with a transparent background, return
88      * the Shape of the opaque part of the image
89      * @param image
90      * @return the Shape
91      */
92     public static Shape getShape(BufferedImage image, int max) {
93         
940        float width = image.getWidth();
950        float height = image.getHeight();
960        if(width > max || height > max) {
970            BufferedImage smaller =
98                 new BufferedImage(max, max, BufferedImage.TYPE_INT_ARGB);
990            Graphics g = smaller.createGraphics();
1000            AffineTransform at = AffineTransform.getScaleInstance(max/width,max/height);
1010            AffineTransform back = AffineTransform.getScaleInstance(width/max,height/max);
1020            Graphics2D g2 = (Graphics2D)g;
1030            g2.drawImage(image, at, null);
1040            g2.dispose();
1050            return back.createTransformedShape(getShape(smaller));
106         } else {
1070            return getShape(image);
108         }
109     }
110     
111     /**
112      * Given an image, possibly with a transparent background, return
113      * the Shape of the opaque part of the image
114      * @param image
115      * @return the Shape
116      */
117     public static Shape getShape(BufferedImage image) {
1180        firstx = 0;
1190        return leftEdge(image, new GeneralPath());
120     }
121     
122     private static Point2D detectLine(Point2D p1, Point2D p2, Point2D p,
123             Line2D line, GeneralPath path) {
1240        if(p2 == null) {
1250            p2 = p;
1260            line.setLine(p1,p2);
127         }
128         // check for line
1290        else if(line.ptLineDistSq(p) < 1) { // its on the line
130             // make it p2
1310            p2.setLocation(p);
132         } else { // its not on the current line
1330            p1.setLocation(p2);
1340            p2.setLocation(p);
1350            line.setLine(p1,p2);
1360            path.lineTo((float)p1.getX(), (float)p1.getY());
137         }
1380        return p2;
139     }
140     /**
141      * trace the left side of the image
142      * @param image
143      * @param path
144      * @return
145      */
146     private static Shape leftEdge(BufferedImage image, GeneralPath path) {
1470        int lastj = 0;
1480        Point2D p1 = null;
1490        Point2D p2 = null;
1500        Line2D line = new Line2D.Float();
1510        for(int i=0; i<image.getHeight(); i+=sample) {
1520            boolean aPointExistsOnThisLine = false;
153             // go until we reach an opaque point, then stop
1540            for(int j=0; j<image.getWidth(); j+=sample) {
1550                if((image.getRGB(j,i) & 0xff000000) != 0) {
156                     // this is a point I want
1570                    Point2D p = new Point2D.Float(j,i);
1580                    aPointExistsOnThisLine = true;
1590                    if(path.getCurrentPoint() != null) {
160                         // this is a continuation of a path
1610                        p2 = detectLine(p1,p2,p,line,path);
162                     } else {
163                         // this is the first point in the path
1640                        path.moveTo(j,i);
1650                        firstx = j;
1660                        p1 = p;
167                     }
1680                    lastj = j;
1690                    break;
170                 }
171             }
1720            if(aPointExistsOnThisLine == false) {
1730                break;
174             }
175         }
1760        return bottomEdge(image, path, lastj);
177     }
178     
179     /**
180      * trace the bottom of the image
181      * @param image
182      * @param path
183      * @param start
184      * @return
185      */
186     private static Shape bottomEdge(BufferedImage image, GeneralPath path, int start) {
1870        int lastj = 0;
1880        Point2D p1 = path.getCurrentPoint();
1890        Point2D p2 = null;
1900        Line2D line = new Line2D.Float();
1910        for(int i=start; i<image.getWidth(); i+=sample) {
1920            boolean aPointExistsOnThisLine = false;
1930            for(int j=image.getHeight()-1; j>=0; j-=sample) {
1940                if((image.getRGB(i,j) & 0xff000000) != 0) {
195                     // this is a point I want
1960                    Point2D p = new Point2D.Float(i,j);
1970                    aPointExistsOnThisLine = true;
1980                    p2 = detectLine(p1,p2,p,line,path);
1990                    lastj = j;
2000                    break;
201                 }
202             }
2030            if(aPointExistsOnThisLine == false) {
2040                break;
205             }
206         }
2070        return rightEdge(image, path, lastj);
208     }
209     
210     /**
211      * trace the right side of the image
212      * @param image
213      * @param path
214      * @param start
215      * @return
216      */
217     private static Shape rightEdge(BufferedImage image, GeneralPath path, int start) {
2180        int lastj = 0;
2190        Point2D p1 = path.getCurrentPoint();
2200        Point2D p2 = null;
2210        Line2D line = new Line2D.Float();
2220        for(int i=start; i>=0; i-=sample) {
2230            boolean aPointExistsOnThisLine = false;
224  
2250            for(int j=image.getWidth()-1; j>=0; j-=sample) {
2260                if((image.getRGB(j,i) & 0xff000000) != 0) {
227                     // this is a point I want
2280                    Point2D p = new Point2D.Float(j,i);
2290                    aPointExistsOnThisLine = true;
2300                    p2 = detectLine(p1,p2,p,line,path);
2310                    lastj=j;
2320                    break;
233                 }
234             }
2350            if(aPointExistsOnThisLine == false) {
2360                break;
237             }
238         }
2390        return topEdge(image, path, lastj);
240     }
241     
242     /**
243      * trace the top of the image
244      * @param image
245      * @param path
246      * @param start
247      * @return
248      */
249     private static Shape topEdge(BufferedImage image, GeneralPath path, int start) {
2500        Point2D p1 = path.getCurrentPoint();
2510        Point2D p2 = null;
2520        Line2D line = new Line2D.Float();
2530        for(int i=start; i>=firstx; i-=sample) {
2540            boolean aPointExistsOnThisLine = false;
2550            for(int j=0; j<image.getHeight(); j+=sample) {
2560                if((image.getRGB(i,j) & 0xff000000) != 0) {
257                     // this is a point I want
2580                    Point2D p = new Point2D.Float(i,j);
2590                    aPointExistsOnThisLine = true;
2600                    p2 = detectLine(p1,p2,p,line,path);
2610                    break;
262                 }
263             }
2640            if(aPointExistsOnThisLine == false) {
2650                break;
266             }
267         }
2680        path.closePath();
2690        return path;
270     }
271 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.