Skip to content

Commit

Permalink
NIFI-5355 ResizeImage Fails to read PNG type on some OS's
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>

This closes #2854.
  • Loading branch information
patricker authored and pvillard31 committed Jul 10, 2018
1 parent f60585a commit 1963697
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -174,7 +175,13 @@ public void process(final InputStream rawIn, final OutputStream out) throws IOEx
if (scaledImage instanceof BufferedImage) {
scaledBufferedImg = (BufferedImage) scaledImage;
} else {
scaledBufferedImg = new BufferedImage(scaledImage.getWidth(null), scaledImage.getHeight(null), image.getType());
// Determine image type, since calling image.getType may return 0
int imageType = BufferedImage.TYPE_INT_ARGB;
if(image.getTransparency() == Transparency.OPAQUE) {
imageType = BufferedImage.TYPE_INT_RGB;
}

scaledBufferedImg = new BufferedImage(scaledImage.getWidth(null), scaledImage.getHeight(null), imageType);
final Graphics2D graphics = scaledBufferedImg.createGraphics();
try {
graphics.drawImage(scaledImage, 0, 0, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,74 @@ public void testResize() throws IOException {
runner.enqueue(Paths.get("src/test/resources/simple.jpg"));
runner.run();

runner.assertAllFlowFilesTransferred(ResizeImage.REL_SUCCESS, 1);
MockFlowFile mff = runner.getFlowFilesForRelationship(ResizeImage.REL_SUCCESS).get(0);
byte[] data = mff.toByteArray();

BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
assertEquals(64, img.getWidth());
assertEquals(64, img.getHeight());
File out = new File("target/smooth.jpg");
ImageIO.write(img, "JPG", out);

runner.clearTransferState();

runner.setProperty(ResizeImage.SCALING_ALGORITHM, ResizeImage.RESIZE_FAST);

runner.enqueue(Paths.get("src/test/resources/simple.jpg"));
runner.run();

runner.assertAllFlowFilesTransferred(ResizeImage.REL_SUCCESS, 1);
mff = runner.getFlowFilesForRelationship(ResizeImage.REL_SUCCESS).get(0);
data = mff.toByteArray();

img = ImageIO.read(new ByteArrayInputStream(data));
assertEquals(64, img.getWidth());
assertEquals(64, img.getHeight());
out = new File("target/fast.jpg");
ImageIO.write(img, "JPG", out);
}

@Test
public void testEnlarge() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new ResizeImage());
runner.setProperty(ResizeImage.IMAGE_HEIGHT, "600");
runner.setProperty(ResizeImage.IMAGE_WIDTH, "600");
runner.setProperty(ResizeImage.SCALING_ALGORITHM, ResizeImage.RESIZE_SMOOTH);

runner.enqueue(Paths.get("src/test/resources/photoshop-8x12-32colors-alpha.gif"));
runner.run();

runner.assertAllFlowFilesTransferred(ResizeImage.REL_SUCCESS, 1);
MockFlowFile mff = runner.getFlowFilesForRelationship(ResizeImage.REL_SUCCESS).get(0);
byte[] data = mff.toByteArray();

BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
assertEquals(600, img.getWidth());
assertEquals(600, img.getHeight());
File out = new File("target/enlarge.png");
ImageIO.write(img, "PNG", out);
}


@Test
public void testResizePNG() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new ResizeImage());
runner.setProperty(ResizeImage.IMAGE_HEIGHT, "64");
runner.setProperty(ResizeImage.IMAGE_WIDTH, "64");
runner.setProperty(ResizeImage.SCALING_ALGORITHM, ResizeImage.RESIZE_SMOOTH);

runner.enqueue(Paths.get("src/test/resources/mspaint-8x10.png"));
runner.run();

runner.assertAllFlowFilesTransferred(ResizeImage.REL_SUCCESS, 1);
final MockFlowFile mff = runner.getFlowFilesForRelationship(ResizeImage.REL_SUCCESS).get(0);
final byte[] data = mff.toByteArray();

final BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
assertEquals(64, img.getWidth());
assertEquals(64, img.getHeight());
final File out = new File("target/simple.jpg");
ImageIO.write(img, "JPG", out);
final File out = new File("target/mspaint-8x10resized.png");
ImageIO.write(img, "PNG", out);
}

}
}

0 comments on commit 1963697

Please sign in to comment.