svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.snapping.app / org.gvsig.snapping.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / PixelSnapper.java @ 44135
History | View | Annotate | Download (4.61 KB)
1 | 42037 | fdiaz | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | package org.gvsig.app.project.documents.view.toolListeners.snapping.snappers; |
||
25 | |||
26 | import java.awt.Color; |
||
27 | import java.awt.geom.Point2D; |
||
28 | import java.awt.image.BufferedImage; |
||
29 | import java.awt.image.ColorModel; |
||
30 | |||
31 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
32 | import org.gvsig.fmap.mapcontrol.PrimitivesDrawer; |
||
33 | import org.gvsig.fmap.mapcontrol.MapControl; |
||
34 | import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperRaster; |
||
35 | import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.impl.AbstractSnapper; |
||
36 | import org.gvsig.i18n.Messages; |
||
37 | |||
38 | import com.vividsolutions.jts.geom.Coordinate; |
||
39 | |||
40 | public class PixelSnapper extends AbstractSnapper implements ISnapperRaster { |
||
41 | private Color refColor = Color.BLACK; |
||
42 | private int tolColorR = 100; |
||
43 | private int tolColorG = 100; |
||
44 | private int tolColorB = 100; |
||
45 | /* (non-Javadoc)
|
||
46 | * @see com.iver.cit.gvsig.gui.cad.snapping.ISnapperRaster#getSnapPoint(java.awt.image.BufferedImage, java.awt.geom.Point2D, double, java.awt.geom.Point2D)
|
||
47 | */
|
||
48 | public Point2D getSnapPoint(MapControl mapControl, Point2D point, |
||
49 | double mapTolerance, Point2D lastPoint) { |
||
50 | // Miramos dentro del rect?ngulo que define la tolerancia
|
||
51 | // y nos quedamos con el pixel que m?s se acerca al que buscamos
|
||
52 | BufferedImage img = mapControl.getImage();
|
||
53 | ViewPort vp = mapControl.getViewPort(); |
||
54 | Point2D pPixel = vp.fromMapPoint(point);
|
||
55 | int xPixel = (int) pPixel.getX(); |
||
56 | int yPixel = (int) pPixel.getY(); |
||
57 | // int centerRGB = img.getRGB(xPixel, yPixel);
|
||
58 | int centerRGB = refColor.getRGB();
|
||
59 | double x1 = ColorModel.getRGBdefault().getRed(centerRGB); |
||
60 | double y1 = ColorModel.getRGBdefault().getGreen(centerRGB); |
||
61 | double z1 = ColorModel.getRGBdefault().getBlue(centerRGB); |
||
62 | Coordinate c = new Coordinate(x1, y1, z1);
|
||
63 | |||
64 | int half = vp.fromMapDistance(mapTolerance) / 2; |
||
65 | double minDistColor = Double.MAX_VALUE; |
||
66 | int xSnapped = -1; |
||
67 | int ySnapped = -1; |
||
68 | int fromX =xPixel -half;
|
||
69 | if (fromX <0) fromX = 0; |
||
70 | int fromY =yPixel -half;
|
||
71 | if (fromY <0) fromY = 0; |
||
72 | |||
73 | int toX =xPixel + half;
|
||
74 | if (toX > vp.getImageWidth()) toX = vp.getImageWidth();
|
||
75 | int toY =yPixel + half;
|
||
76 | if (toY > vp.getImageHeight()) toY = vp.getImageHeight();
|
||
77 | |||
78 | for (int testX= fromX; testX< toX; testX++) |
||
79 | { |
||
80 | for (int testY= fromY; testY< toY; testY++) |
||
81 | { |
||
82 | // System.out.println("Testing: " + testX + ", " + testY);
|
||
83 | int testRGB = img.getRGB(testX, testY);
|
||
84 | // TODO: Aqu? deber?amos trabajar con un ColorSpace y usar toCIEXYZ. Por ahora lo calculo con RGB.
|
||
85 | int r = ColorModel.getRGBdefault().getRed(testRGB); |
||
86 | int g = ColorModel.getRGBdefault().getGreen(testRGB); |
||
87 | int b = ColorModel.getRGBdefault().getBlue(testRGB); |
||
88 | Coordinate cAux = new Coordinate(r, g, b);
|
||
89 | |||
90 | if (Math.abs(r-x1) < tolColorR) |
||
91 | if (Math.abs(g-y1) < tolColorG) |
||
92 | if (Math.abs(b-z1) < tolColorB) |
||
93 | { |
||
94 | double dist = c.distance(cAux);
|
||
95 | if (dist < minDistColor)
|
||
96 | { |
||
97 | minDistColor = dist; |
||
98 | xSnapped = testX; |
||
99 | ySnapped = testY; |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | Point2D pResul = null; |
||
105 | if (xSnapped != -1) |
||
106 | { |
||
107 | pResul = vp.toMapPoint(xSnapped, ySnapped); |
||
108 | } |
||
109 | return pResul;
|
||
110 | } |
||
111 | |||
112 | /*
|
||
113 | * (non-Javadoc)
|
||
114 | * @see org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper#draw(org.gvsig.fmap.mapcontrol.PrimitivesDrawer, java.awt.geom.Point2D)
|
||
115 | */
|
||
116 | public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) { |
||
117 | primitivesDrawer.setColor(getColor()); |
||
118 | int half = getSizePixels() / 2; |
||
119 | primitivesDrawer.drawOval((int) (pPixels.getX() - half),
|
||
120 | (int) (pPixels.getY() - half),
|
||
121 | getSizePixels(), getSizePixels()); |
||
122 | } |
||
123 | |||
124 | public String getToolTipText() { |
||
125 | return Messages.getText("Pixel_point"); |
||
126 | } |
||
127 | |||
128 | } |
||
129 |