diff --git a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.java b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.java index 8ab6044ad34..ada832ecddf 100644 --- a/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.java +++ b/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.java @@ -74,13 +74,16 @@ static void setUp() @Test void testCreateFromStream() throws IOException { - PDDocument document = new PDDocument(); - InputStream stream = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"); - PDImageXObject ximage = JPEGFactory.createFromStream(document, stream); - validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName()); + try(PDDocument document = new PDDocument(); + InputStream stream1 = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"); + InputStream stream2 = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")) + { + PDImageXObject ximage = JPEGFactory.createFromStream(document, stream1); + validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName()); - doWritePDF(document, ximage, TESTRESULTSDIR, "jpegrgbstream.pdf"); - checkJpegStream(TESTRESULTSDIR, "jpegrgbstream.pdf", JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")); + doWritePDF(document, ximage, TESTRESULTSDIR, "jpegrgbstream.pdf"); + checkJpegStream(TESTRESULTSDIR, "jpegrgbstream.pdf", stream2); + } } /* @@ -90,13 +93,16 @@ void testCreateFromStream() throws IOException @Test void testCreateFromStreamCMYK() throws IOException { - PDDocument document = new PDDocument(); - InputStream stream = JPEGFactoryTest.class.getResourceAsStream("jpegcmyk.jpg"); - PDImageXObject ximage = JPEGFactory.createFromStream(document, stream); - validate(ximage, 8, 343, 287, "jpg", PDDeviceCMYK.INSTANCE.getName()); + try(InputStream stream = JPEGFactoryTest.class.getResourceAsStream("jpegcmyk.jpg"); + InputStream stream1 = JPEGFactoryTest.class.getResourceAsStream("jpegcmyk.jpg")) + { + PDDocument document = new PDDocument(); + PDImageXObject ximage = JPEGFactory.createFromStream(document, stream); + validate(ximage, 8, 343, 287, "jpg", PDDeviceCMYK.INSTANCE.getName()); - doWritePDF(document, ximage, TESTRESULTSDIR, "jpegcmykstream.pdf"); - checkJpegStream(TESTRESULTSDIR, "jpegcmykstream.pdf", JPEGFactoryTest.class.getResourceAsStream("jpegcmyk.jpg")); + doWritePDF(document, ximage, TESTRESULTSDIR, "jpegcmykstream.pdf"); + checkJpegStream(TESTRESULTSDIR, "jpegcmykstream.pdf", stream1); + } } /** @@ -106,13 +112,16 @@ void testCreateFromStreamCMYK() throws IOException @Test void testCreateFromStream256() throws IOException { - PDDocument document = new PDDocument(); - InputStream stream = JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg"); - PDImageXObject ximage = JPEGFactory.createFromStream(document, stream); - validate(ximage, 8, 344, 287, "jpg", PDDeviceGray.INSTANCE.getName()); + try(InputStream stream = JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg"); + InputStream stream1 = JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg")) + { + PDDocument document = new PDDocument(); + PDImageXObject ximage = JPEGFactory.createFromStream(document, stream); + validate(ximage, 8, 344, 287, "jpg", PDDeviceGray.INSTANCE.getName()); - doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg256stream.pdf"); - checkJpegStream(TESTRESULTSDIR, "jpeg256stream.pdf", JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg")); + doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg256stream.pdf"); + checkJpegStream(TESTRESULTSDIR, "jpeg256stream.pdf", stream1); + } } /** @@ -122,13 +131,16 @@ void testCreateFromStream256() throws IOException @Test void testCreateFromImageRGB() throws IOException { - PDDocument document = new PDDocument(); - BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")); - assertEquals(3, image.getColorModel().getNumComponents()); - PDImageXObject ximage = JPEGFactory.createFromImage(document, image); - validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName()); + try(InputStream is = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")) + { + PDDocument document = new PDDocument(); + BufferedImage image = ImageIO.read(is); + assertEquals(3, image.getColorModel().getNumComponents()); + PDImageXObject ximage = JPEGFactory.createFromImage(document, image); + validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName()); - doWritePDF(document, ximage, TESTRESULTSDIR, "jpegrgb.pdf"); + doWritePDF(document, ximage, TESTRESULTSDIR, "jpegrgb.pdf"); + } } /** @@ -138,13 +150,16 @@ void testCreateFromImageRGB() throws IOException @Test void testCreateFromImage256() throws IOException { - PDDocument document = new PDDocument(); - BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg")); - assertEquals(1, image.getColorModel().getNumComponents()); - PDImageXObject ximage = JPEGFactory.createFromImage(document, image); - validate(ximage, 8, 344, 287, "jpg", PDDeviceGray.INSTANCE.getName()); + try(InputStream is = JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg")) + { + PDDocument document = new PDDocument(); + BufferedImage image = ImageIO.read(is); + assertEquals(1, image.getColorModel().getNumComponents()); + PDImageXObject ximage = JPEGFactory.createFromImage(document, image); + validate(ximage, 8, 344, 287, "jpg", PDDeviceGray.INSTANCE.getName()); - doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg256.pdf"); + doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg256.pdf"); + } } /** @@ -154,32 +169,35 @@ void testCreateFromImage256() throws IOException @Test void testCreateFromImageINT_ARGB() throws IOException { - PDDocument document = new PDDocument(); - BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")); - - // create an ARGB image - int width = image.getWidth(); - int height = image.getHeight(); - BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - Graphics ag = argbImage.getGraphics(); - ag.drawImage(image, 0, 0, null); - ag.dispose(); - - for (int x = 0; x < argbImage.getWidth(); ++x) + try(InputStream is = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")) { - for (int y = 0; y < argbImage.getHeight(); ++y) + PDDocument document = new PDDocument(); + BufferedImage image = ImageIO.read(is); + + // create an ARGB image + int width = image.getWidth(); + int height = image.getHeight(); + BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + Graphics ag = argbImage.getGraphics(); + ag.drawImage(image, 0, 0, null); + ag.dispose(); + + for (int x = 0; x < argbImage.getWidth(); ++x) { - argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24)); + for (int y = 0; y < argbImage.getHeight(); ++y) + { + argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24)); + } } - } - PDImageXObject ximage = JPEGFactory.createFromImage(document, argbImage); - validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName()); - assertNotNull(ximage.getSoftMask()); - validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName()); - assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10); + PDImageXObject ximage = JPEGFactory.createFromImage(document, argbImage); + validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName()); + assertNotNull(ximage.getSoftMask()); + validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName()); + assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10); - doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-intargb.pdf"); + doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-intargb.pdf"); + } } /** @@ -189,32 +207,35 @@ void testCreateFromImageINT_ARGB() throws IOException @Test void testCreateFromImage4BYTE_ABGR() throws IOException { - PDDocument document = new PDDocument(); - BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")); - - // create an ARGB image - int width = image.getWidth(); - int height = image.getHeight(); - BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); - Graphics ag = argbImage.getGraphics(); - ag.drawImage(image, 0, 0, null); - ag.dispose(); - - for (int x = 0; x < argbImage.getWidth(); ++x) + try(InputStream is = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")) { - for (int y = 0; y < argbImage.getHeight(); ++y) + PDDocument document = new PDDocument(); + BufferedImage image = ImageIO.read(is); + + // create an ARGB image + int width = image.getWidth(); + int height = image.getHeight(); + BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); + Graphics ag = argbImage.getGraphics(); + ag.drawImage(image, 0, 0, null); + ag.dispose(); + + for (int x = 0; x < argbImage.getWidth(); ++x) { - argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24)); + for (int y = 0; y < argbImage.getHeight(); ++y) + { + argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24)); + } } - } - PDImageXObject ximage = JPEGFactory.createFromImage(document, argbImage); - validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName()); - assertNotNull(ximage.getSoftMask()); - validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName()); - assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10); + PDImageXObject ximage = JPEGFactory.createFromImage(document, argbImage); + validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName()); + assertNotNull(ximage.getSoftMask()); + validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName()); + assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10); - doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-4bargb.pdf"); + doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-4bargb.pdf"); + } } /** @@ -225,30 +246,33 @@ void testCreateFromImage4BYTE_ABGR() throws IOException @Test void testCreateFromImageUSHORT_555_RGB() throws IOException { - PDDocument document = new PDDocument(); - BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")); - - // create an USHORT_555_RGB image - int width = image.getWidth(); - int height = image.getHeight(); - BufferedImage rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_555_RGB); - Graphics ag = rgbImage.getGraphics(); - ag.drawImage(image, 0, 0, null); - ag.dispose(); - - for (int x = 0; x < rgbImage.getWidth(); ++x) + try(InputStream is = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg")) { - for (int y = 0; y < rgbImage.getHeight(); ++y) + PDDocument document = new PDDocument(); + BufferedImage image = ImageIO.read(is); + + // create an USHORT_555_RGB image + int width = image.getWidth(); + int height = image.getHeight(); + BufferedImage rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_555_RGB); + Graphics ag = rgbImage.getGraphics(); + ag.drawImage(image, 0, 0, null); + ag.dispose(); + + for (int x = 0; x < rgbImage.getWidth(); ++x) { - rgbImage.setRGB(x, y, (rgbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24)); + for (int y = 0; y < rgbImage.getHeight(); ++y) + { + rgbImage.setRGB(x, y, (rgbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24)); + } } - } - PDImageXObject ximage = JPEGFactory.createFromImage(document, rgbImage); - validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName()); - assertNull(ximage.getSoftMask()); + PDImageXObject ximage = JPEGFactory.createFromImage(document, rgbImage); + validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName()); + assertNull(ximage.getSoftMask()); - doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-ushort555rgb.pdf"); + doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-ushort555rgb.pdf"); + } } /**