Commit 555e39b7 authored by Erdem Karakus's avatar Erdem Karakus

CMS-11395 [Backport 11.2] Image weight adjusted on scaling

When an image has been scaled to its original dimensions, image which
has the smaller weight when compared between original image and
scaled image will be used to be saved.

(cherry picked from commit bc45d293)
parent 0246dbf6
......@@ -284,7 +284,14 @@ public class ScaleImageOperation extends AbstractImageOperation {
scaledWidth = scaledImage.getWidth();
scaledHeight = scaledImage.getHeight();
scaledData = new ByteArrayInputStream(scaledOutputStream.toByteArray());
// if the scale size equals to the original image size use the image which has the smaller weight
if (scaledOutputStream.toByteArray().length > IOUtils.toByteArray(dataInputStream).length
&& scaledWidth == originalWidth && scaledHeight == originalHeight) {
scaledData = new AutoDeletingTmpFileInputStream(tmpFile);
} else {
scaledData = new ByteArrayInputStream(scaledOutputStream.toByteArray());
}
}
} finally {
if (imageInputStream != null) {
......
......@@ -33,7 +33,6 @@ import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
......@@ -87,13 +86,6 @@ public class ScaleImageOperationTest {
ScaleImageOperation scaleOp = new ScaleImageOperation(380, 428, false, ImageUtils.ScalingStrategy.SPEED);
scaleOp.execute(data, "image/jpeg");
checkImageDimensions(scaleOp, "image/jpeg", 380, 428);
// redo scaling to check the data itself
data = getClass().getResourceAsStream("/test-380x428.jpg");
scaleOp = new ScaleImageOperation(380, 428, false, ImageUtils.ScalingStrategy.SPEED);
scaleOp.execute(data, "image/jpeg");
InputStream original = getClass().getResourceAsStream("/test-380x428.jpg");
assertFalse("Original image data should not be used as-is", IOUtils.contentEquals(original, scaleOp.getScaledData()));
}
@Test
......@@ -119,13 +111,6 @@ public class ScaleImageOperationTest {
ScaleImageOperation scaleOp = new ScaleImageOperation(500, 500, false, ImageUtils.ScalingStrategy.SPEED);
scaleOp.execute(data, "image/jpeg");
checkImageDimensions(scaleOp, "image/jpeg", 380, 428);
// redo scaling to check the data itself
data = getClass().getResourceAsStream("/test-380x428.jpg");
scaleOp = new ScaleImageOperation(500, 500, false, ImageUtils.ScalingStrategy.SPEED);
scaleOp.execute(data, "image/jpeg");
InputStream original = getClass().getResourceAsStream("/test-380x428.jpg");
assertFalse("Original image data should not be used as-is", IOUtils.contentEquals(original, scaleOp.getScaledData()));
}
@Test
......@@ -319,6 +304,26 @@ public class ScaleImageOperationTest {
assertTrue("Resize ratio calculated by bounding-box width.", ratio == 2.0);
}
@Test
public void scaledImageDataWeightIsNotBiggerWhenScalingToOriginalDimensions() throws GalleryException, IOException {
InputStream data = getClass().getResourceAsStream("/test-380x428.jpg");
ScaleImageOperation scaleOp = new ScaleImageOperation(380, 428, false, ImageUtils.ScalingStrategy.QUALITY);
scaleOp.execute(data, "image/jpeg");
data = getClass().getResourceAsStream("/test-380x428.jpg");
assertTrue("Scaled image data weight is not higher when scaling to original dimensions",
IOUtils.toByteArray(scaleOp.getScaledData()).length <= IOUtils.toByteArray(data).length);
}
@Test
public void scaledImageDataWeightIsNotBiggerWhenScalingToLargerDimensionsAndUpscalingIsDisabled() throws GalleryException, IOException {
InputStream data = getClass().getResourceAsStream("/test-380x428.jpg");
ScaleImageOperation scaleOp = new ScaleImageOperation(500, 500, false, ImageUtils.ScalingStrategy.QUALITY);
scaleOp.execute(data, "image/jpeg");
data = getClass().getResourceAsStream("/test-380x428.jpg");
assertTrue("Scaled image data weight is not higher when scaling to larger dimensions and upscaling is disabled",
IOUtils.toByteArray(scaleOp.getScaledData()).length <= IOUtils.toByteArray(data).length);
}
private void checkImageDimensions(ScaleImageOperation scaleOp, String mimeType, int expectedWidth, int expectedHeight) throws IOException {
assertEquals(expectedWidth, scaleOp.getScaledWidth());
assertEquals(expectedHeight, scaleOp.getScaledHeight());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment