root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / PixelSnapper.java @ 30349
History | View | Annotate | Download (4.83 KB)
1 |
/* 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 |
package org.gvsig.app.project.documents.view.toolListeners.snapping.snappers; |
42 |
|
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 |
import org.gvsig.fmap.mapcontrol.PrimitivesDrawer; |
50 |
import org.gvsig.fmap.mapcontrol.MapControl; |
51 |
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperRaster; |
52 |
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.impl.AbstractSnapper; |
53 |
|
54 |
import com.vividsolutions.jts.geom.Coordinate; |
55 |
|
56 |
public class PixelSnapper extends AbstractSnapper implements ISnapperRaster { |
57 |
private Color refColor = Color.BLACK; |
58 |
private int tolColorR = 100; |
59 |
private int tolColorG = 100; |
60 |
private int tolColorB = 100; |
61 |
/* (non-Javadoc)
|
62 |
* @see com.iver.cit.gvsig.gui.cad.snapping.ISnapperRaster#getSnapPoint(java.awt.image.BufferedImage, java.awt.geom.Point2D, double, java.awt.geom.Point2D)
|
63 |
*/
|
64 |
public Point2D getSnapPoint(MapControl mapControl, Point2D point, |
65 |
double mapTolerance, Point2D lastPoint) { |
66 |
// Miramos dentro del rect?ngulo que define la tolerancia
|
67 |
// y nos quedamos con el pixel que m?s se acerca al que buscamos
|
68 |
BufferedImage img = mapControl.getImage();
|
69 |
ViewPort vp = mapControl.getViewPort(); |
70 |
Point2D pPixel = vp.fromMapPoint(point);
|
71 |
int xPixel = (int) pPixel.getX(); |
72 |
int yPixel = (int) pPixel.getY(); |
73 |
// int centerRGB = img.getRGB(xPixel, yPixel);
|
74 |
int centerRGB = refColor.getRGB();
|
75 |
double x1 = ColorModel.getRGBdefault().getRed(centerRGB); |
76 |
double y1 = ColorModel.getRGBdefault().getGreen(centerRGB); |
77 |
double z1 = ColorModel.getRGBdefault().getBlue(centerRGB); |
78 |
Coordinate c = new Coordinate(x1, y1, z1);
|
79 |
|
80 |
int half = vp.fromMapDistance(mapTolerance) / 2; |
81 |
double minDistColor = Double.MAX_VALUE; |
82 |
int xSnapped = -1; |
83 |
int ySnapped = -1; |
84 |
int fromX =xPixel -half;
|
85 |
if (fromX <0) fromX = 0; |
86 |
int fromY =yPixel -half;
|
87 |
if (fromY <0) fromY = 0; |
88 |
|
89 |
int toX =xPixel + half;
|
90 |
if (toX > vp.getImageWidth()) toX = vp.getImageWidth();
|
91 |
int toY =yPixel + half;
|
92 |
if (toY > vp.getImageHeight()) toY = vp.getImageHeight();
|
93 |
|
94 |
for (int testX= fromX; testX< toX; testX++) |
95 |
{ |
96 |
for (int testY= fromY; testY< toY; testY++) |
97 |
{ |
98 |
// System.out.println("Testing: " + testX + ", " + testY);
|
99 |
int testRGB = img.getRGB(testX, testY);
|
100 |
// TODO: Aqu? deber?amos trabajar con un ColorSpace y usar toCIEXYZ. Por ahora lo calculo con RGB.
|
101 |
int r = ColorModel.getRGBdefault().getRed(testRGB); |
102 |
int g = ColorModel.getRGBdefault().getGreen(testRGB); |
103 |
int b = ColorModel.getRGBdefault().getBlue(testRGB); |
104 |
Coordinate cAux = new Coordinate(r, g, b);
|
105 |
|
106 |
if (Math.abs(r-x1) < tolColorR) |
107 |
if (Math.abs(g-y1) < tolColorG) |
108 |
if (Math.abs(b-z1) < tolColorB) |
109 |
{ |
110 |
double dist = c.distance(cAux);
|
111 |
if (dist < minDistColor)
|
112 |
{ |
113 |
minDistColor = dist; |
114 |
xSnapped = testX; |
115 |
ySnapped = testY; |
116 |
} |
117 |
} |
118 |
} |
119 |
} |
120 |
Point2D pResul = null; |
121 |
if (xSnapped != -1) |
122 |
{ |
123 |
pResul = vp.toMapPoint(xSnapped, ySnapped); |
124 |
} |
125 |
return pResul;
|
126 |
} |
127 |
|
128 |
/*
|
129 |
* (non-Javadoc)
|
130 |
* @see org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper#draw(org.gvsig.fmap.mapcontrol.PrimitivesDrawer, java.awt.geom.Point2D)
|
131 |
*/
|
132 |
public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) { |
133 |
primitivesDrawer.setColor(getColor()); |
134 |
int half = getSizePixels() / 2; |
135 |
primitivesDrawer.drawOval((int) (pPixels.getX() - half),
|
136 |
(int) (pPixels.getY() - half),
|
137 |
getSizePixels(), getSizePixels()); |
138 |
} |
139 |
|
140 |
public String getToolTipText() { |
141 |
return "pixel_point"; |
142 |
} |
143 |
|
144 |
} |
145 |
|
146 |
|