Index: api/src/test/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperationTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- api/src/test/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperationTest.java (revision 48142) +++ api/src/test/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperationTest.java (revision ) @@ -33,6 +33,7 @@ import org.xml.sax.SAXException; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** @@ -81,7 +82,7 @@ } @Test - public void useOriginalDataWhenNoBoundingBoxIsGiven() throws GalleryException, IOException { + public void scaleToOriginalDimensionsWhenNoBoundingBoxIsGiven() throws GalleryException, IOException { InputStream data = getClass().getResourceAsStream("/test-380x428.jpg"); ScaleImageOperation scaleOp = new ScaleImageOperation(0, 0, false, ImageUtils.ScalingStrategy.SPEED); scaleOp.execute(data, "image/jpeg"); @@ -92,11 +93,11 @@ scaleOp = new ScaleImageOperation(0, 0, false, ImageUtils.ScalingStrategy.SPEED); scaleOp.execute(data, "image/jpeg"); InputStream original = getClass().getResourceAsStream("/test-380x428.jpg"); - assertTrue("Original image data should be used as-is", IOUtils.contentEquals(original, scaleOp.getScaledData())); + assertFalse("Original image data should be used as-is", IOUtils.contentEquals(original, scaleOp.getScaledData())); } @Test - public void useOriginalDataWhenBoundingBoxMatchesOriginalDimensions() throws GalleryException, IOException { + public void scaleToOriginalDimensionsWhenBoundingBoxMatchesOriginalDimensions() throws GalleryException, IOException { InputStream data = getClass().getResourceAsStream("/test-380x428.jpg"); ScaleImageOperation scaleOp = new ScaleImageOperation(380, 428, false, ImageUtils.ScalingStrategy.SPEED); scaleOp.execute(data, "image/jpeg"); @@ -107,11 +108,11 @@ scaleOp = new ScaleImageOperation(380, 428, false, ImageUtils.ScalingStrategy.SPEED); scaleOp.execute(data, "image/jpeg"); InputStream original = getClass().getResourceAsStream("/test-380x428.jpg"); - assertTrue("Original image data should be used as-is", IOUtils.contentEquals(original, scaleOp.getScaledData())); + assertFalse("Original image data should be used as-is", IOUtils.contentEquals(original, scaleOp.getScaledData())); } @Test - public void useOriginalDataWhenScalingUpAndUpscalingIsDisabled() throws GalleryException, IOException { + public void scaleToOriginalDimensionsWhenScalingUpAndUpscalingIsDisabled() throws GalleryException, IOException { InputStream data = getClass().getResourceAsStream("/test-380x428.jpg"); ScaleImageOperation scaleOp = new ScaleImageOperation(500, 500, false, ImageUtils.ScalingStrategy.SPEED); scaleOp.execute(data, "image/jpeg"); @@ -122,7 +123,7 @@ scaleOp = new ScaleImageOperation(500, 500, false, ImageUtils.ScalingStrategy.SPEED); scaleOp.execute(data, "image/jpeg"); InputStream original = getClass().getResourceAsStream("/test-380x428.jpg"); - assertTrue("Original image data should be used as-is", IOUtils.contentEquals(original, scaleOp.getScaledData())); + assertFalse("Original image data should be used as-is", IOUtils.contentEquals(original, scaleOp.getScaledData())); } @Test Index: api/src/main/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperation.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- api/src/main/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperation.java (revision 48142) +++ api/src/main/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperation.java (revision ) @@ -268,10 +268,10 @@ final File tmpFile = writeToTmpFile(data); boolean deleteTmpFile = true; log.debug("Stored uploaded image in temporary file {}", tmpFile); - + InputStream dataInputStream = null; ImageInputStream imageInputStream = null; - + try { dataInputStream = new FileInputStream(tmpFile); imageInputStream = new MemoryCacheImageInputStream(dataInputStream); @@ -281,36 +281,33 @@ final int originalHeight = reader.getHeight(0); final double resizeRatio = calculateResizeRatio(originalWidth, originalHeight, width, height); - if (resizeRatio == 1.0d || (resizeRatio >= 1.0d && !upscaling)) { - // return the original image data as-is by reading the temporary file, which is deleted when the - // stream is closed - log.debug("Using the original image of {}x{} as-is", originalWidth, originalHeight); - deleteTmpFile = false; - scaledData = new AutoDeletingTmpFileInputStream(tmpFile); - scaledWidth = originalWidth; - scaledHeight = originalHeight; + int targetWidth; + int targetHeight; + + if (resizeRatio >= 1.0d && !upscaling) { + targetWidth = originalWidth; + targetHeight = originalHeight; } else { // scale the image - int targetWidth = (int)Math.max(originalWidth * resizeRatio, 1); - int targetHeight = (int)Math.max(originalHeight * resizeRatio, 1); - + targetWidth = (int) Math.max(originalWidth * resizeRatio, 1); + targetHeight = (int) Math.max(originalHeight * resizeRatio, 1); + } - if (log.isDebugEnabled()) { - log.debug("Resizing image of {}x{} to {}x{}", new Object[]{originalWidth, originalHeight, targetWidth, targetHeight}); - } + if (log.isDebugEnabled()) { + log.debug("Resizing image of {}x{} to {}x{}", new Object[]{originalWidth, originalHeight, targetWidth, targetHeight}); + } - BufferedImage scaledImage; + BufferedImage scaledImage; - synchronized(scalingLock) { - BufferedImage originalImage = reader.read(0); - scaledImage = ImageUtils.scaleImage(originalImage, targetWidth, targetHeight, strategy); - } + synchronized(scalingLock) { + BufferedImage originalImage = reader.read(0); + scaledImage = ImageUtils.scaleImage(originalImage, targetWidth, targetHeight, strategy); + } - scaledWidth = scaledImage.getWidth(); - scaledHeight = scaledImage.getHeight(); + scaledWidth = scaledImage.getWidth(); + scaledHeight = scaledImage.getHeight(); - ByteArrayOutputStream scaledOutputStream = ImageUtils.writeImage(writer, scaledImage, compressionQuality); - scaledData = new ByteArrayInputStream(scaledOutputStream.toByteArray()); + ByteArrayOutputStream scaledOutputStream = ImageUtils.writeImage(writer, scaledImage, compressionQuality); + scaledData = new ByteArrayInputStream(scaledOutputStream.toByteArray()); - } } finally { if (imageInputStream != null) { imageInputStream.close(); @@ -322,7 +319,7 @@ } } } - + private File writeToTmpFile(InputStream data) throws IOException { File tmpFile = File.createTempFile("hippo-image", ".tmp"); tmpFile.deleteOnExit();