Changeset 16923 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2020-08-23T23:43:22+02:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r16885 r16923 7 7 import java.awt.Cursor; 8 8 import java.awt.Dimension; 9 import java.awt.Graphics;10 9 import java.awt.Graphics2D; 11 10 import java.awt.GraphicsEnvironment; … … 38 37 import java.util.Collection; 39 38 import java.util.EnumMap; 40 import java.util.HashMap;41 39 import java.util.Hashtable; 42 40 import java.util.Iterator; … … 297 295 private static final Map<String, ImageResource> cache = new ConcurrentHashMap<>(); 298 296 299 /**300 * Caches the image data for rotated versions of the same image.301 */302 private static final Map<Image, Map<Long, Image>> ROTATE_CACHE = new HashMap<>();303 304 297 /** small cache of critical images used in many parts of the application */ 305 298 private static final Map<OsmPrimitiveType, ImageIcon> osmPrimitiveTypeCache = new EnumMap<>(OsmPrimitiveType.class); … … 855 848 public static void clearCache() { 856 849 cache.clear(); 857 synchronized (ROTATE_CACHE) {858 ROTATE_CACHE.clear();859 }860 850 synchronized (osmPrimitiveTypeCache) { 861 851 osmPrimitiveTypeCache.clear(); … … 1395 1385 /** 90 degrees in radians units */ 1396 1386 private static final double DEGREE_90 = 90.0 * Math.PI / 180.0; 1397 1398 /**1399 * Creates a rotated version of the input image.1400 *1401 * @param img the image to be rotated.1402 * @param rotatedAngle the rotated angle, in degree, clockwise. It could be any double but we1403 * will mod it with 360 before using it. More over for caching performance, it will be rounded to1404 * an entire value between 0 and 360.1405 *1406 * @return the image after rotating.1407 * @since 61721408 */1409 public static Image createRotatedImage(Image img, double rotatedAngle) {1410 return createRotatedImage(img, rotatedAngle, ImageResource.DEFAULT_DIMENSION);1411 }1412 1413 /**1414 * Creates a rotated version of the input image.1415 *1416 * @param img the image to be rotated.1417 * @param rotatedAngle the rotated angle, in degree, clockwise. It could be any double but we1418 * will mod it with 360 before using it. More over for caching performance, it will be rounded to1419 * an entire value between 0 and 360.1420 * @param dimension ignored1421 * @return the image after rotating and scaling.1422 * @since 61721423 */1424 public static Image createRotatedImage(Image img, double rotatedAngle, Dimension dimension) {1425 CheckParameterUtil.ensureParameterNotNull(img, "img");1426 1427 // convert rotatedAngle to an integer value from 0 to 3601428 Long angleLong = Math.round(rotatedAngle % 360);1429 Long originalAngle = rotatedAngle != 0 && angleLong == 0 ? Long.valueOf(360L) : angleLong;1430 1431 synchronized (ROTATE_CACHE) {1432 Map<Long, Image> cacheByAngle = ROTATE_CACHE.computeIfAbsent(img, k -> new HashMap<>());1433 Image rotatedImg = cacheByAngle.get(originalAngle);1434 1435 if (rotatedImg == null) {1436 // convert originalAngle to a value from 0 to 901437 double angle = originalAngle % 90;1438 if (originalAngle != 0 && angle == 0) {1439 angle = 90.0;1440 }1441 double radian = Utils.toRadians(angle);1442 1443 rotatedImg = HiDPISupport.processMRImage(img, img0 -> {1444 new ImageIcon(img0); // load completely1445 int iw = img0.getWidth(null);1446 int ih = img0.getHeight(null);1447 int w;1448 int h;1449 1450 if ((originalAngle >= 0 && originalAngle <= 90) || (originalAngle > 180 && originalAngle <= 270)) {1451 w = (int) (iw * Math.sin(DEGREE_90 - radian) + ih * Math.sin(radian));1452 h = (int) (iw * Math.sin(radian) + ih * Math.sin(DEGREE_90 - radian));1453 } else {1454 w = (int) (ih * Math.sin(DEGREE_90 - radian) + iw * Math.sin(radian));1455 h = (int) (ih * Math.sin(radian) + iw * Math.sin(DEGREE_90 - radian));1456 }1457 Image image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);1458 Graphics g = image.getGraphics();1459 Graphics2D g2d = (Graphics2D) g.create();1460 1461 // calculate the center of the icon.1462 int cx = iw / 2;1463 int cy = ih / 2;1464 1465 // move the graphics center point to the center of the icon.1466 g2d.translate(w / 2, h / 2);1467 1468 // rotate the graphics about the center point of the icon1469 g2d.rotate(Utils.toRadians(originalAngle));1470 1471 g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);1472 g2d.drawImage(img0, -cx, -cy, null);1473 1474 g2d.dispose();1475 new ImageIcon(image); // load completely1476 return image;1477 });1478 cacheByAngle.put(originalAngle, rotatedImg);1479 }1480 return rotatedImg;1481 }1482 }1483 1387 1484 1388 /**
Note:
See TracChangeset
for help on using the changeset viewer.