Changeset 19371 in osm for applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/GeorefImage.java
- Timestamp:
- 2010-01-10T15:46:08+01:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/GeorefImage.java
r18544 r19371 25 25 private static final long serialVersionUID = 1L; 26 26 27 // bbox of the georeferenced image (the nice horizontal and vertical box) 27 28 public EastNorth min; 28 29 public EastNorth max; 30 // bbox of the georeferenced original image (raster only) (inclined if rotated and before cropping) 31 // P[0] is bottom,left then next are clockwise. 32 private EastNorth[] orgRaster = new EastNorth[4]; 33 // bbox of the georeferenced original image (raster only) after cropping 34 private EastNorth[] orgCroppedRaster = new EastNorth[4]; 35 // angle with georeferenced original image after rotation (raster images only)(in radian) 36 private double angle = 0; 37 29 38 public BufferedImage image; 30 39 … … 36 45 this.min = min; 37 46 this.max = max; 47 this.orgRaster[0] = min; 48 this.orgRaster[1] = new EastNorth(min.east(), max.north()); 49 this.orgRaster[2] = max; 50 this.orgRaster[3] = new EastNorth(max.east(), min.north()); 51 this.orgCroppedRaster[0] = min; 52 this.orgCroppedRaster[1] = new EastNorth(min.east(), max.north()); 53 this.orgCroppedRaster[2] = max; 54 this.orgCroppedRaster[3] = new EastNorth(max.east(), min.north()); 38 55 updatePixelPer(); 39 56 } … … 45 62 } 46 63 47 private void getNewBounding(EastNorth min, EastNorth max, EastNorth c, EastNorth d) { 64 /** 65 * Recalculate the new bounding box of the image based on the previous [min,max] bbox 66 * and the new box after rotation [c,d]. 67 * The new bbox defined in [min.max] will retain the extreme values of both boxes. 68 * @param oldMin the original box min point, before rotation 69 * @param oldMax the original box max point, before rotation 70 * @param c the new box min point, after rotation 71 * @param d the new box max point, after rotation 72 */ 73 private EastNorthBound getNewBounding(EastNorth oldMin, EastNorth oldMax, EastNorth c, EastNorth d) { 48 74 EastNorth pt[] = new EastNorth[4]; 49 pt[0] = min;50 pt[1] = max;75 pt[0] = oldMin; 76 pt[1] = oldMax; 51 77 pt[2] = c; 52 78 pt[3] = d; … … 61 87 highestNorth = Math.max(pt[i].north(), highestNorth); 62 88 } 63 min.setLocation(smallestEast, smallestNorth);64 max.setLocation(highestEast, highestNorth);89 return new EastNorthBound(new EastNorth(smallestEast, smallestNorth), 90 new EastNorth(highestEast, highestNorth)); 65 91 } 66 92 … … 84 110 if (drawBoundaries) { 85 111 g.setColor(Color.green); 86 g.drawRect(minPt.x, maxPt.y, maxPt.x - minPt.x, minPt.y - maxPt.y); 112 if (orgCroppedRaster == null) { 113 // this is the old cache format where only [min,max] bbox is stored 114 g.drawRect(minPt.x, maxPt.y, maxPt.x - minPt.x, minPt.y - maxPt.y); 115 } else { 116 Point[] croppedPoint = new Point[5]; 117 for (int i=0; i<4; i++) 118 croppedPoint[i] = nc.getPoint(orgCroppedRaster[i]); 119 croppedPoint[4] = croppedPoint[0]; 120 for (int i=0; i<4; i++) 121 g.drawLine(croppedPoint[i].x, croppedPoint[i].y, croppedPoint[i+1].x, croppedPoint[i+1].y); 122 } 87 123 } 88 124 g.drawImage(image, minPt.x, maxPt.y, maxPt.x, minPt.y, // dest … … 135 171 */ 136 172 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 137 max = new EastNorth(in.readDouble(), in.readDouble()); 138 min = new EastNorth(in.readDouble(), in.readDouble()); 173 if (WMSLayer.currentFormat == 2 || WMSLayer.currentFormat == 3) { 174 max = new EastNorth(in.readDouble(), in.readDouble()); 175 min = new EastNorth(in.readDouble(), in.readDouble()); 176 } 177 if (WMSLayer.currentFormat == 3) { 178 orgRaster = new EastNorth[4]; 179 orgCroppedRaster = new EastNorth[4]; 180 angle = in.readDouble(); 181 orgRaster[0] = new EastNorth(in.readDouble(), in.readDouble()); 182 orgRaster[1] = new EastNorth(in.readDouble(), in.readDouble()); 183 orgRaster[2] = new EastNorth(in.readDouble(), in.readDouble()); 184 orgRaster[3] = new EastNorth(in.readDouble(), in.readDouble()); 185 orgCroppedRaster[0] = new EastNorth(in.readDouble(), in.readDouble()); 186 orgCroppedRaster[1] = new EastNorth(in.readDouble(), in.readDouble()); 187 orgCroppedRaster[2] = new EastNorth(in.readDouble(), in.readDouble()); 188 orgCroppedRaster[3] = new EastNorth(in.readDouble(), in.readDouble()); 189 } else { 190 orgRaster = null; 191 orgCroppedRaster = null; 192 angle = 0; 193 } 139 194 image = (BufferedImage) ImageIO.read(ImageIO.createImageInputStream(in)); 140 195 updatePixelPer(); … … 143 198 /** 144 199 * Method required by BufferedImage serialization. 145 * Cache usesonly primitivesto stay independent of software changes.200 * Use only primitives for stability in time (not influenced by josm-core changes). 146 201 */ 147 202 private void writeObject(ObjectOutputStream out) throws IOException { 148 out.writeDouble(max.getX()); 149 out.writeDouble(max.getY()); 150 out.writeDouble(min.getX()); 151 out.writeDouble(min.getY()); 203 out.writeDouble(max.getX()); out.writeDouble(max.getY()); 204 out.writeDouble(min.getX()); out.writeDouble(min.getY()); 205 if (orgRaster == null) { // just in case we save an old format layer already cached 206 orgRaster = new EastNorth[4]; 207 orgCroppedRaster = new EastNorth[4]; 208 } 209 out.writeDouble(angle); 210 out.writeDouble(orgRaster[0].getX()); out.writeDouble(orgRaster[0].getY()); 211 out.writeDouble(orgRaster[1].getX()); out.writeDouble(orgRaster[1].getY()); 212 out.writeDouble(orgRaster[2].getX()); out.writeDouble(orgRaster[2].getY()); 213 out.writeDouble(orgRaster[3].getX()); out.writeDouble(orgRaster[3].getY()); 214 out.writeDouble(orgCroppedRaster[0].getX()); out.writeDouble(orgCroppedRaster[0].getY()); 215 out.writeDouble(orgCroppedRaster[1].getX()); out.writeDouble(orgCroppedRaster[1].getY()); 216 out.writeDouble(orgCroppedRaster[2].getX()); out.writeDouble(orgCroppedRaster[2].getY()); 217 out.writeDouble(orgCroppedRaster[3].getX()); out.writeDouble(orgCroppedRaster[3].getY()); 152 218 ImageIO.write(image, "png", ImageIO.createImageOutputStream(out)); 153 219 } … … 182 248 min = new EastNorth(min.east() + dx, min.north() + dy); 183 249 max = new EastNorth(max.east() + dx, max.north() + dy); 250 for (int i=0; i<4; i++) { 251 orgRaster[i] = new EastNorth(orgRaster[i].east() + dx, orgRaster[i].north() + dy); 252 orgCroppedRaster[i] = new EastNorth(orgCroppedRaster[i].east() + dx, orgCroppedRaster[i].north() + dy); 253 } 184 254 } 185 255 … … 192 262 min = anchor.interpolate(min, proportion); 193 263 max = anchor.interpolate(max, proportion); 264 for (int i=0; i<4; i++) { 265 orgRaster[i] = anchor.interpolate(orgRaster[i], proportion); 266 orgCroppedRaster[i] = anchor.interpolate(orgCroppedRaster[i], proportion); 267 } 194 268 updatePixelPer(); 195 269 } … … 198 272 * Rotate this image and its min/max coordinates around anchor point 199 273 * @param anchor anchor of rotation 200 * @param angle angle of rotation (in radians) 201 */ 202 public void rotate(EastNorth anchor, double angle) { 203 EastNorth min2 = new EastNorth(min.east(), max.north()); 204 EastNorth max2 = new EastNorth(max.east(), min.north()); 205 min = min.rotate(anchor, angle); 206 max = max.rotate(anchor, angle); 207 min2 = min2.rotate(anchor, angle); 208 max2 = max2.rotate(anchor, angle); 209 getNewBounding(min, max, min2, max2); 210 image = tilt(image, angle); 211 } 212 213 /** 214 * Rotate by copying original buffered image into a new one with new dimensions 215 * @param image 216 * @param angle 217 * @return 218 */ 219 public static BufferedImage tilt(BufferedImage image, double angle) { 220 double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle)); 274 * @param ang angle of rotation (in radian) 275 */ 276 public void rotate(EastNorth anchor, double ang) { 277 // rotate the bounding boxes coordinates first 278 EastNorth min2 = new EastNorth(orgRaster[0].east(), orgRaster[2].north()); 279 EastNorth max2 = new EastNorth(orgRaster[2].east(), orgRaster[0].north()); 280 for (int i=0; i<4; i++) { 281 orgRaster[i] = orgRaster[i].rotate(anchor, ang); 282 orgCroppedRaster[i] = orgCroppedRaster[i].rotate(anchor, ang); 283 } 284 min2 = min2.rotate(anchor, ang); 285 max2 = max2.rotate(anchor, ang); 286 EastNorthBound enb = getNewBounding(orgCroppedRaster[0], orgCroppedRaster[2], min2, max2); 287 min = enb.min; 288 max = enb.max; 289 angle=+ang; 290 291 // rotate the image now 292 double sin = Math.abs(Math.sin(ang)), cos = Math.abs(Math.cos(ang)); 221 293 int w = image.getWidth(), h = image.getHeight(); 222 294 int neww = (int)Math.floor(w*cos+h*sin), newh = (int)Math.floor(h*cos+w*sin); … … 225 297 Graphics2D g = result.createGraphics(); 226 298 g.translate((neww-w)/2, (newh-h)/2); 227 g.rotate(ang le, w/2, h/2);299 g.rotate(ang, w/2, h/2); 228 300 g.drawRenderedImage(image, null); 229 301 g.dispose(); 230 returnresult;302 image = result; 231 303 } 232 304
Note:
See TracChangeset
for help on using the changeset viewer.
