Changeset 16978 in josm


Ignore:
Timestamp:
2020-08-30T09:46:47+02:00 (4 weeks ago)
Author:
simon04
Message:

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageResizeMode.java

    r16946 r16978  
    3535            CheckParameterUtil.ensureThat((dim.width > 0 || dim.width == -1) && (dim.height > 0 || dim.height == -1),
    3636                    () -> dim + " is invalid");
     37            final int maxWidth = Math.min(dim.width, icon.width);
     38            final int maxHeight = Math.min(dim.height, icon.height);
    3739            final Dimension spec;
    38             if (dim.width == -1 || dim.height == -1) {
     40            if (maxWidth == -1 || maxHeight == -1) {
    3941                spec = dim;
    40             } else if (icon.getWidth() / dim.width > icon.getHeight() / dim.height) {
    41                 spec = new Dimension(dim.width, -1);
     42            } else if (icon.getWidth() / maxWidth > icon.getHeight() / maxHeight) {
     43                spec = new Dimension(maxWidth, -1);
    4244            } else {
    43                 spec = new Dimension(-1, dim.height);
     45                spec = new Dimension(-1, maxHeight);
    4446            }
    4547            return AUTO.computeDimension(spec, icon);
    46             // TODO disable upscaling non-svg icons?
    4748        }
    4849    },
  • trunk/test/unit/org/openstreetmap/josm/tools/ImageResizeModeTest.java

    r16946 r16978  
    1414import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1515
     16/**
     17 * Unit tests of {@link ImageResizeMode} class.
     18 */
    1619public class ImageResizeModeTest {
    1720
     
    2326    public JOSMTestRules test = new JOSMTestRules().preferences();
    2427
     28    final Dimension image = new Dimension(64, 48);
     29    final Dimension smallImage = new Dimension(14, 10);
     30
     31    /**
     32     * Test method for {@link ImageResizeMode#computeDimension} using {@link ImageResizeMode#AUTO}
     33     */
    2534    @Test
    26     public void testComputeDimension() {
    27         final Dimension image = new Dimension(64, 48);
     35    public void testComputeDimensionAuto() {
    2836        assertThrows(IllegalArgumentException.class, () -> ImageResizeMode.AUTO.computeDimension(new Dimension(0, 0), image));
    2937        assertEquals(new Dimension(64, 48), ImageResizeMode.AUTO.computeDimension(ImageResource.DEFAULT_DIMENSION, image));
     
    3341        assertEquals(new Dimension(21, 16), ImageResizeMode.AUTO.computeDimension(new Dimension(-1, 16), image));
    3442        assertEquals(new Dimension(24, 32), ImageResizeMode.AUTO.computeDimension(new Dimension(24, 32), image));
     43    }
     44
     45    /**
     46     * Test method for {@link ImageResizeMode#computeDimension} using {@link ImageResizeMode#BOUNDED}
     47     */
     48    @Test
     49    public void testComputeDimensionBounded() {
    3550        assertEquals(new Dimension(64, 48), ImageResizeMode.BOUNDED.computeDimension(ImageResource.DEFAULT_DIMENSION, image));
    3651        assertEquals(new Dimension(64, 48), ImageResizeMode.BOUNDED.computeDimension(new Dimension(-1, -1), image));
     
    3954        assertEquals(new Dimension(21, 16), ImageResizeMode.BOUNDED.computeDimension(new Dimension(-1, 16), image));
    4055        assertEquals(new Dimension(24, 18), ImageResizeMode.BOUNDED.computeDimension(new Dimension(24, 32), image));
     56        assertEquals(new Dimension(14, 10), ImageResizeMode.BOUNDED.computeDimension(new Dimension(64, 48), smallImage));
     57        assertEquals(new Dimension(14, 10), ImageResizeMode.BOUNDED.computeDimension(new Dimension(16, 16), smallImage));
     58        assertEquals(new Dimension(11, 8), ImageResizeMode.BOUNDED.computeDimension(new Dimension(16, 8), smallImage));
     59        assertEquals(new Dimension(12, 8), ImageResizeMode.BOUNDED.computeDimension(new Dimension(12, 12), smallImage));
     60        assertEquals(new Dimension(11, 8), ImageResizeMode.BOUNDED.computeDimension(new Dimension(12, 8), smallImage));
     61        assertEquals(new Dimension(8, 5), ImageResizeMode.BOUNDED.computeDimension(new Dimension(8, 16), smallImage));
     62        assertEquals(new Dimension(8, 5), ImageResizeMode.BOUNDED.computeDimension(new Dimension(8, 8), smallImage));
     63        assertEquals(new Dimension(8, 5), ImageResizeMode.BOUNDED.computeDimension(new Dimension(8, 8), smallImage));
     64    }
     65
     66    /**
     67     * Test method for {@link ImageResizeMode#computeDimension} using {@link ImageResizeMode#PADDED}
     68     */
     69    @Test
     70    public void testComputeDimensionPadded() {
    4171        assertThrows(IllegalArgumentException.class, () -> ImageResizeMode.PADDED.computeDimension(new Dimension(0, 0), image));
    4272        assertThrows(IllegalArgumentException.class, () -> ImageResizeMode.PADDED.computeDimension(ImageResource.DEFAULT_DIMENSION, image));
     
    4878    }
    4979
     80    /**
     81     * Test method for {@link ImageResizeMode#cacheKey}
     82     */
    5083    @Test
    5184    public void testCacheKey() {
Note: See TracChangeset for help on using the changeset viewer.