source: josm/trunk/src/org/openstreetmap/josm/tools/ImageResizeMode.java@ 16978

Last change on this file since 16978 was 16978, checked in by simon04, 4 years ago

fix #19706, fix #19725 - Do not upscale images when using ImageResizeMode.BOUNDED

File size: 4.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.Dimension;
5import java.awt.Graphics2D;
6import java.awt.RenderingHints;
7import java.awt.image.BufferedImage;
8import java.util.function.Consumer;
9
10/**
11 * Determines how the image is sized/resized in {@link ImageResource#getImageIcon(Dimension, boolean, ImageResizeMode)}.
12 */
13enum ImageResizeMode {
14
15 AUTO {
16 @Override
17 Dimension computeDimension(Dimension dim, Dimension icon) {
18 CheckParameterUtil.ensureThat((dim.width > 0 || dim.width == -1) && (dim.height > 0 || dim.height == -1),
19 () -> dim + " is invalid");
20 if (dim.width == -1 && dim.height == -1) {
21 return new Dimension(GuiSizesHelper.getSizeDpiAdjusted(icon.width), GuiSizesHelper.getSizeDpiAdjusted(icon.height));
22 } else if (dim.width == -1) {
23 return new Dimension(Math.max(1, icon.width * dim.height / icon.height), dim.height);
24 } else if (dim.height == -1) {
25 return new Dimension(dim.width, Math.max(1, icon.height * dim.width / icon.width));
26 } else {
27 return dim;
28 }
29 }
30 },
31
32 BOUNDED {
33 @Override
34 Dimension computeDimension(Dimension dim, Dimension icon) {
35 CheckParameterUtil.ensureThat((dim.width > 0 || dim.width == -1) && (dim.height > 0 || dim.height == -1),
36 () -> dim + " is invalid");
37 final int maxWidth = Math.min(dim.width, icon.width);
38 final int maxHeight = Math.min(dim.height, icon.height);
39 final Dimension spec;
40 if (maxWidth == -1 || maxHeight == -1) {
41 spec = dim;
42 } else if (icon.getWidth() / maxWidth > icon.getHeight() / maxHeight) {
43 spec = new Dimension(maxWidth, -1);
44 } else {
45 spec = new Dimension(-1, maxHeight);
46 }
47 return AUTO.computeDimension(spec, icon);
48 }
49 },
50
51 PADDED {
52 @Override
53 Dimension computeDimension(Dimension dim, Dimension icon) {
54 CheckParameterUtil.ensureThat(dim.width > 0 && dim.height > 0, () -> dim + " is invalid");
55 return dim;
56 }
57
58 @Override
59 void prepareGraphics(Dimension icon, BufferedImage image, Graphics2D g) {
60 g.setClip(0, 0, image.getWidth(), image.getHeight());
61 final double scale = Math.min(image.getWidth() / icon.getWidth(), image.getHeight() / icon.getHeight());
62 g.translate((image.getWidth() - icon.getWidth() * scale) / 2, (image.getHeight() - icon.getHeight() * scale) / 2);
63 g.scale(scale, scale);
64 }
65 };
66
67 /**
68 * Computes the dimension for the resulting image
69 * @param dim the desired image dimension
70 * @param icon the dimensions of the image to resize
71 * @return the dimension for the resulting image
72 */
73 abstract Dimension computeDimension(Dimension dim, Dimension icon);
74
75 /**
76 * Creates a new buffered image and applies the rendering function
77 * @param dim the desired image dimension
78 * @param icon the dimensions of the image to resize
79 * @param renderer the rendering function
80 * @return a new buffered image
81 */
82 BufferedImage createBufferedImage(Dimension dim, Dimension icon, Consumer<Graphics2D> renderer) {
83 final Dimension real = computeDimension(dim, icon);
84 final BufferedImage bufferedImage = new BufferedImage(real.width, real.height, BufferedImage.TYPE_INT_ARGB);
85 final Graphics2D g = bufferedImage.createGraphics();
86 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
87 prepareGraphics(icon, bufferedImage, g);
88 renderer.accept(g);
89 return bufferedImage;
90 }
91
92 /**
93 * Prepares the graphics object for rendering the given image
94 * @param icon the dimensions of the image to resize
95 * @param image the image to render afterwards
96 * @param g graphics
97 */
98 void prepareGraphics(Dimension icon, BufferedImage image, Graphics2D g) {
99 g.setClip(0, 0, image.getWidth(), image.getHeight());
100 g.scale(image.getWidth() / icon.getWidth(), image.getHeight() / icon.getHeight());
101 }
102
103 /**
104 * Returns a cache key for this mode and the given dimension
105 * @param dim the desired image dimension
106 * @return a cache key
107 */
108 int cacheKey(Dimension dim) {
109 return (ordinal() << 28) | ((dim.width & 0xfff) << 16) | (dim.height & 0xfff);
110 }
111
112}
Note: See TracBrowser for help on using the repository browser.