| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package edu.uci.ics.jung.visualization.jai; |
| 12 | |
|
| 13 | |
import java.awt.Graphics2D; |
| 14 | |
import java.awt.Image; |
| 15 | |
import java.awt.Shape; |
| 16 | |
import java.awt.geom.AffineTransform; |
| 17 | |
import java.awt.geom.PathIterator; |
| 18 | |
import java.awt.geom.Point2D; |
| 19 | |
import java.awt.geom.Rectangle2D; |
| 20 | |
import java.awt.image.BufferedImage; |
| 21 | |
import java.awt.image.RenderedImage; |
| 22 | |
import java.awt.image.renderable.ParameterBlock; |
| 23 | |
import java.util.ArrayList; |
| 24 | |
import java.util.List; |
| 25 | |
|
| 26 | |
import javax.media.jai.InterpolationNearest; |
| 27 | |
import javax.media.jai.JAI; |
| 28 | |
import javax.media.jai.PerspectiveTransform; |
| 29 | |
import javax.media.jai.WarpPerspective; |
| 30 | |
import javax.media.jai.operator.WarpDescriptor; |
| 31 | |
import javax.swing.Icon; |
| 32 | |
|
| 33 | |
import edu.uci.ics.jung.algorithms.layout.Layout; |
| 34 | |
import edu.uci.ics.jung.visualization.Layer; |
| 35 | |
import edu.uci.ics.jung.visualization.RenderContext; |
| 36 | |
import edu.uci.ics.jung.visualization.renderers.BasicVertexRenderer; |
| 37 | |
import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator; |
| 38 | |
import edu.uci.ics.jung.visualization.transform.shape.ShapeTransformer; |
| 39 | |
import edu.uci.ics.jung.visualization.transform.shape.TransformingGraphics; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public class TransformingImageVertexIconRenderer<V,E> extends BasicVertexRenderer<V,E> { |
| 48 | |
|
| 49 | |
WarpDescriptor warpDescriptor; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 0 | public TransformingImageVertexIconRenderer() { |
| 56 | 0 | this.warpDescriptor = new WarpDescriptor(); |
| 57 | 0 | } |
| 58 | |
|
| 59 | |
@Override |
| 60 | |
public void paintIconForVertex(RenderContext<V,E> rc, V v, Layout<V,E> layout) { |
| 61 | |
|
| 62 | 0 | GraphicsDecorator g = rc.getGraphicsContext(); |
| 63 | 0 | TransformingGraphics g2d = (TransformingGraphics)g; |
| 64 | 0 | boolean vertexHit = true; |
| 65 | |
|
| 66 | 0 | Shape shape = rc.getVertexShapeTransformer().transform(v); |
| 67 | |
|
| 68 | 0 | Point2D p = layout.transform(v); |
| 69 | 0 | p = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p); |
| 70 | 0 | float x = (float)p.getX(); |
| 71 | 0 | float y = (float)p.getY(); |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 0 | AffineTransform xform = AffineTransform.getTranslateInstance(x,y); |
| 76 | |
|
| 77 | 0 | shape = xform.createTransformedShape(shape); |
| 78 | |
|
| 79 | 0 | vertexHit = vertexHit(rc, shape); |
| 80 | 0 | if (vertexHit) { |
| 81 | 0 | if(rc.getVertexIconTransformer() != null) { |
| 82 | 0 | Icon icon = rc.getVertexIconTransformer().transform(v); |
| 83 | 0 | if(icon != null) { |
| 84 | |
|
| 85 | 0 | BufferedImage image = new BufferedImage(icon.getIconWidth(), |
| 86 | |
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); |
| 87 | 0 | Graphics2D ig = image.createGraphics(); |
| 88 | 0 | icon.paintIcon(null, ig, 0, 0); |
| 89 | 0 | int imageWidth = image.getWidth(null); |
| 90 | 0 | int imageHeight = image.getHeight(null); |
| 91 | |
|
| 92 | 0 | int xLoc = (int) (x - imageWidth / 2); |
| 93 | 0 | int yLoc = (int) (y - imageHeight / 2); |
| 94 | 0 | Rectangle2D imageRectangle = new Rectangle2D.Double(xLoc, yLoc, |
| 95 | |
imageWidth, imageHeight); |
| 96 | |
|
| 97 | 0 | Shape perspectiveShape = |
| 98 | |
((ShapeTransformer) g2d.getTransformer()).transform(imageRectangle); |
| 99 | |
|
| 100 | |
|
| 101 | 0 | if(imageRectangle.equals(perspectiveShape.getBounds2D()) == false) { |
| 102 | |
|
| 103 | 0 | RenderedImage ri = getPerspectiveImage(image, imageRectangle, |
| 104 | |
perspectiveShape); |
| 105 | |
|
| 106 | 0 | if (ri != null) { |
| 107 | |
|
| 108 | 0 | Shape clip = g2d.getClip(); |
| 109 | 0 | g2d.setClip(perspectiveShape); |
| 110 | 0 | g2d.drawRenderedImage(ri, AffineTransform |
| 111 | |
.getTranslateInstance(xLoc, yLoc)); |
| 112 | 0 | g2d.setClip(clip); |
| 113 | |
} |
| 114 | 0 | } else { |
| 115 | 0 | g2d.drawImage(image, AffineTransform.getTranslateInstance(xLoc, |
| 116 | |
yLoc), null); |
| 117 | |
} |
| 118 | |
|
| 119 | 0 | } else { |
| 120 | 0 | paintShapeForVertex(rc, v, shape); |
| 121 | |
} |
| 122 | 0 | } else { |
| 123 | 0 | paintShapeForVertex(rc, v, shape); |
| 124 | |
} |
| 125 | |
} |
| 126 | 0 | } |
| 127 | |
|
| 128 | |
protected RenderedImage getPerspectiveImage(Image image, Shape imageShape, Shape perspectiveShape) { |
| 129 | |
|
| 130 | 0 | Rectangle2D imageRectangle = imageShape.getBounds2D(); |
| 131 | 0 | double imagex1 = imageRectangle.getX(); |
| 132 | 0 | double imagey1 = imageRectangle.getY(); |
| 133 | 0 | double imagex2 = imageRectangle.getMaxX(); |
| 134 | 0 | double imagey2 = imageRectangle.getMaxY(); |
| 135 | |
|
| 136 | 0 | double width = imagex2 - imagex1; |
| 137 | 0 | double height = imagey2 - imagey1; |
| 138 | |
|
| 139 | 0 | double quadx1 = 0; |
| 140 | 0 | double quady1 = 0; |
| 141 | 0 | double quadx2 = 0; |
| 142 | 0 | double quady2 = 0; |
| 143 | 0 | double quadx3 = 0; |
| 144 | 0 | double quady3 = 0; |
| 145 | 0 | double quadx4 = 0; |
| 146 | 0 | double quady4 = 0; |
| 147 | |
|
| 148 | 0 | List<Point2D> quadList = new ArrayList<Point2D>(); |
| 149 | |
|
| 150 | 0 | for(PathIterator iterator=perspectiveShape.getPathIterator(null); |
| 151 | 0 | iterator.isDone() == false; iterator.next()) { |
| 152 | |
|
| 153 | 0 | float[] coords = new float[6]; |
| 154 | 0 | int type = iterator.currentSegment(coords); |
| 155 | 0 | switch(type) { |
| 156 | |
case PathIterator.SEG_MOVETO: |
| 157 | 0 | quadList.add(new Point2D.Float(coords[0], coords[1])); |
| 158 | 0 | break; |
| 159 | |
case PathIterator.SEG_LINETO: |
| 160 | 0 | quadList.add(new Point2D.Float(coords[0], coords[1])); |
| 161 | |
break; |
| 162 | |
} |
| 163 | |
} |
| 164 | 0 | Point2D pt1 = quadList.remove(0); |
| 165 | 0 | Point2D pt2 = quadList.remove(0); |
| 166 | 0 | Point2D pt3 = quadList.remove(0); |
| 167 | 0 | Point2D pt4 = quadList.remove(0); |
| 168 | |
|
| 169 | 0 | quadx1 = pt1.getX()-imagex1; |
| 170 | 0 | quady1 = pt1.getY()-imagey1; |
| 171 | |
|
| 172 | 0 | quadx2 = pt2.getX()-imagex1; |
| 173 | 0 | quady2 = pt2.getY()-imagey1; |
| 174 | |
|
| 175 | 0 | quadx3 = pt3.getX()-imagex1; |
| 176 | 0 | quady3 = pt3.getY()-imagey1; |
| 177 | |
|
| 178 | 0 | quadx4 = pt4.getX()-imagex1; |
| 179 | 0 | quady4 = pt4.getY()-imagey1; |
| 180 | |
|
| 181 | 0 | RenderedImage srcImage = null; |
| 182 | 0 | if(image instanceof RenderedImage) { |
| 183 | 0 | srcImage = (RenderedImage)image; |
| 184 | |
} else { |
| 185 | 0 | srcImage = getBufferedImage(image); |
| 186 | |
} |
| 187 | |
|
| 188 | 0 | PerspectiveTransform perspectiveTransform = |
| 189 | |
PerspectiveTransform.getQuadToQuad( |
| 190 | |
|
| 191 | |
quadx1, quady1, |
| 192 | |
quadx2, quady2, |
| 193 | |
quadx3, quady3, |
| 194 | |
quadx4, quady4, |
| 195 | |
|
| 196 | |
0, 0, |
| 197 | |
width, 0, |
| 198 | |
width, height, |
| 199 | |
0, height |
| 200 | |
); |
| 201 | |
|
| 202 | 0 | double[][] matrix = new double[3][3]; |
| 203 | 0 | perspectiveTransform.getMatrix(matrix); |
| 204 | |
|
| 205 | 0 | if(matrix[2][2] == 0 || Double.isNaN(matrix[2][2])) { |
| 206 | 0 | System.err.println("matrix[2][2] = "+matrix[2][2]); |
| 207 | 0 | return null; |
| 208 | |
} |
| 209 | 0 | if(matrix[1][1] == 0 || Double.isNaN(matrix[1][1])) { |
| 210 | 0 | System.err.println("matrix[1][1] = "+matrix[1][1]); |
| 211 | 0 | return null; |
| 212 | |
} |
| 213 | 0 | if(matrix[0][0] == 0 || Double.isNaN(matrix[0][0])) { |
| 214 | 0 | System.err.println("matrix[0][0] = "+matrix[0][0]); |
| 215 | 0 | return null; |
| 216 | |
} |
| 217 | |
|
| 218 | 0 | WarpPerspective warp = new WarpPerspective(perspectiveTransform); |
| 219 | 0 | ParameterBlock pm = new ParameterBlock(); |
| 220 | 0 | pm.addSource(srcImage); |
| 221 | 0 | pm.add(warp); |
| 222 | 0 | pm.add(new InterpolationNearest()); |
| 223 | |
|
| 224 | 0 | RenderedImage dstImage = JAI.create("warp", pm); |
| 225 | |
|
| 226 | 0 | return dstImage; |
| 227 | |
|
| 228 | |
} |
| 229 | |
|
| 230 | |
private BufferedImage getBufferedImage(Image image) { |
| 231 | 0 | int width = image.getWidth(null); |
| 232 | 0 | int height = image.getHeight(null); |
| 233 | 0 | BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
| 234 | 0 | Graphics2D g = bi.createGraphics(); |
| 235 | 0 | g.drawImage(image, 0, 0, null); |
| 236 | 0 | g.dispose(); |
| 237 | 0 | return bi; |
| 238 | |
} |
| 239 | |
} |