Skip to content

Commit 121a51e

Browse files
author
Riyad Kalla
committed
Fixed Issue #65 (#65) and
added test-case to catch it in the future. The stop-condition in incremental scaling (line 2266) was stopping too early if either the width OR height didn't change between an incremental scale. Technically the incremental scaling shouldn't stop until BOTH dimensions stop changing. The fix was simple; change the check from || to &&.
1 parent 159d0c7 commit 121a51e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/main/java/org/imgscalr/Scalr.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -2251,9 +2251,20 @@ protected static BufferedImage scaleImageIncrementally(BufferedImage src,
22512251
currentHeight = targetHeight;
22522252
}
22532253

2254-
// Stop when we cannot incrementally step down anymore.
2254+
/*
2255+
* Stop when we cannot incrementally step down anymore.
2256+
*
2257+
* This used to use a || condition, but that would cause problems
2258+
* when using FIT_EXACT such that sometimes the width OR height
2259+
* would not change between iterations, but the other dimension
2260+
* would (e.g. resizing 500x500 to 500x250).
2261+
*
2262+
* Now changing this to an && condition requires that both
2263+
* dimensions do not change between a resize iteration before we
2264+
* consider ourselves done.
2265+
*/
22552266
if (prevCurrentWidth == currentWidth
2256-
|| prevCurrentHeight == currentHeight)
2267+
&& prevCurrentHeight == currentHeight)
22572268
break;
22582269

22592270
if (DEBUG)

src/test/java/org/imgscalr/ScalrResizeTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public void testResizeWHSpeed() {
9393

9494
@Test
9595
public void testResizeSizeExact() {
96+
System.setProperty(Scalr.DEBUG_PROPERTY_NAME, "true");
9697
assertEquals(load("time-square-resize-320-fit-exact.png"),
9798
Scalr.resize(src, Mode.FIT_EXACT, 320));
9899
}
@@ -134,4 +135,13 @@ public void testResizeUltraQuality() {
134135
// out.
135136
Assert.assertTrue(true);
136137
}
138+
139+
@Test
140+
public void testResizeFitExact() {
141+
BufferedImage i = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
142+
BufferedImage i2 = Scalr.resize(i, Mode.FIT_EXACT, 500, 250);
143+
144+
Assert.assertEquals(i2.getWidth(), 500);
145+
Assert.assertEquals(i2.getHeight(), 250);
146+
}
137147
}

0 commit comments

Comments
 (0)