root / tags / v2_0_0_Build_2047 / applications / appgvSIG / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / PixelSnapper.java @ 38317
History | View | Annotate | Download (4.88 KB)
1 | 23642 | vcaballero | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 29598 | jpiera | package org.gvsig.app.project.documents.view.toolListeners.snapping.snappers; |
42 | 23642 | vcaballero | |
43 | import java.awt.Color; |
||
44 | import java.awt.geom.Point2D; |
||
45 | import java.awt.image.BufferedImage; |
||
46 | import java.awt.image.ColorModel; |
||
47 | |||
48 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
49 | 30349 | jpiera | import org.gvsig.fmap.mapcontrol.PrimitivesDrawer; |
50 | 23642 | vcaballero | import org.gvsig.fmap.mapcontrol.MapControl; |
51 | import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperRaster; |
||
52 | 30349 | jpiera | import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.impl.AbstractSnapper; |
53 | 38226 | jldominguez | import org.gvsig.i18n.Messages; |
54 | 23642 | vcaballero | |
55 | import com.vividsolutions.jts.geom.Coordinate; |
||
56 | |||
57 | public class PixelSnapper extends AbstractSnapper implements ISnapperRaster { |
||
58 | private Color refColor = Color.BLACK; |
||
59 | private int tolColorR = 100; |
||
60 | private int tolColorG = 100; |
||
61 | private int tolColorB = 100; |
||
62 | /* (non-Javadoc)
|
||
63 | * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapperRaster#getSnapPoint(java.awt.image.BufferedImage, java.awt.geom.Point2D, double, java.awt.geom.Point2D)
|
||
64 | */
|
||
65 | public Point2D getSnapPoint(MapControl mapControl, Point2D point, |
||
66 | double mapTolerance, Point2D lastPoint) { |
||
67 | // Miramos dentro del rect?ngulo que define la tolerancia
|
||
68 | // y nos quedamos con el pixel que m?s se acerca al que buscamos
|
||
69 | BufferedImage img = mapControl.getImage();
|
||
70 | ViewPort vp = mapControl.getViewPort(); |
||
71 | Point2D pPixel = vp.fromMapPoint(point);
|
||
72 | int xPixel = (int) pPixel.getX(); |
||
73 | int yPixel = (int) pPixel.getY(); |
||
74 | // int centerRGB = img.getRGB(xPixel, yPixel);
|
||
75 | int centerRGB = refColor.getRGB();
|
||
76 | double x1 = ColorModel.getRGBdefault().getRed(centerRGB); |
||
77 | double y1 = ColorModel.getRGBdefault().getGreen(centerRGB); |
||
78 | double z1 = ColorModel.getRGBdefault().getBlue(centerRGB); |
||
79 | Coordinate c = new Coordinate(x1, y1, z1);
|
||
80 | |||
81 | int half = vp.fromMapDistance(mapTolerance) / 2; |
||
82 | double minDistColor = Double.MAX_VALUE; |
||
83 | int xSnapped = -1; |
||
84 | int ySnapped = -1; |
||
85 | int fromX =xPixel -half;
|
||
86 | if (fromX <0) fromX = 0; |
||
87 | int fromY =yPixel -half;
|
||
88 | if (fromY <0) fromY = 0; |
||
89 | |||
90 | int toX =xPixel + half;
|
||
91 | if (toX > vp.getImageWidth()) toX = vp.getImageWidth();
|
||
92 | int toY =yPixel + half;
|
||
93 | if (toY > vp.getImageHeight()) toY = vp.getImageHeight();
|
||
94 | |||
95 | for (int testX= fromX; testX< toX; testX++) |
||
96 | { |
||
97 | for (int testY= fromY; testY< toY; testY++) |
||
98 | { |
||
99 | // System.out.println("Testing: " + testX + ", " + testY);
|
||
100 | int testRGB = img.getRGB(testX, testY);
|
||
101 | // TODO: Aqu? deber?amos trabajar con un ColorSpace y usar toCIEXYZ. Por ahora lo calculo con RGB.
|
||
102 | int r = ColorModel.getRGBdefault().getRed(testRGB); |
||
103 | int g = ColorModel.getRGBdefault().getGreen(testRGB); |
||
104 | int b = ColorModel.getRGBdefault().getBlue(testRGB); |
||
105 | Coordinate cAux = new Coordinate(r, g, b);
|
||
106 | |||
107 | if (Math.abs(r-x1) < tolColorR) |
||
108 | if (Math.abs(g-y1) < tolColorG) |
||
109 | if (Math.abs(b-z1) < tolColorB) |
||
110 | { |
||
111 | double dist = c.distance(cAux);
|
||
112 | if (dist < minDistColor)
|
||
113 | { |
||
114 | minDistColor = dist; |
||
115 | xSnapped = testX; |
||
116 | ySnapped = testY; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | Point2D pResul = null; |
||
122 | if (xSnapped != -1) |
||
123 | { |
||
124 | pResul = vp.toMapPoint(xSnapped, ySnapped); |
||
125 | } |
||
126 | return pResul;
|
||
127 | } |
||
128 | |||
129 | 30349 | jpiera | /*
|
130 | * (non-Javadoc)
|
||
131 | * @see org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper#draw(org.gvsig.fmap.mapcontrol.PrimitivesDrawer, java.awt.geom.Point2D)
|
||
132 | */
|
||
133 | public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) { |
||
134 | primitivesDrawer.setColor(getColor()); |
||
135 | 23642 | vcaballero | int half = getSizePixels() / 2; |
136 | 30349 | jpiera | primitivesDrawer.drawOval((int) (pPixels.getX() - half),
|
137 | 23642 | vcaballero | (int) (pPixels.getY() - half),
|
138 | getSizePixels(), getSizePixels()); |
||
139 | } |
||
140 | |||
141 | public String getToolTipText() { |
||
142 | 38226 | jldominguez | return Messages.getText("Pixel_point"); |
143 | 23642 | vcaballero | } |
144 | |||
145 | } |
||
146 |