From 107eaef45efce970fe3942ed4153fd2f55610faf Mon Sep 17 00:00:00 2001 From: Tilman Hausherr Date: Sun, 25 Aug 2024 10:54:03 +0000 Subject: [PATCH] =?UTF-8?q?PDFBOX-5872:=20avoid=20NPE=20on=20image=20libra?= =?UTF-8?q?ry=20/=20allow=20JPEG2000=20library=20that=20doesn't=20support?= =?UTF-8?q?=20raster,=20by=20G=C3=83=C2=A1bor=20Stefanik;=20closes=20#197?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1920175 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/pdfbox/filter/DCTFilter.java | 2 +- .../java/org/apache/pdfbox/filter/Filter.java | 32 +++++++++++++++++-- .../pdmodel/graphics/image/JPEGFactory.java | 4 +-- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java b/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java index b69822054a3..cb8b3322044 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java @@ -59,7 +59,7 @@ final class DCTFilter extends Filter public DecodeResult decode(InputStream encoded, OutputStream decoded, COSDictionary parameters, int index, DecodeOptions options) throws IOException { - ImageReader reader = findImageReader("JPEG", "a suitable JAI I/O image filter is not installed"); + ImageReader reader = findRasterReader("JPEG", "a suitable JAI I/O image filter is not installed"); try (ImageInputStream iis = ImageIO.createImageInputStream(encoded)) { diff --git a/pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java b/pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java index b6f521cd061..8e9824d88fc 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java @@ -149,7 +149,7 @@ else if (obj != null && !(filter instanceof COSArray || obj instanceof COSArray) } /** - * Finds a suitable image raster reader for an image format. + * Finds a suitable image reader for an image format. * * @param formatName The image format to search for. * @param errorCause The probably cause if something goes wrong. @@ -163,11 +163,37 @@ public static final ImageReader findImageReader(String formatName, String errorC while (readers.hasNext()) { ImageReader reader = readers.next(); - if (reader.canReadRaster()) + if (reader != null) { return reader; } - reader.dispose(); + } + throw new MissingImageReaderException("Cannot read " + formatName + " image: " + errorCause); + } + + /** + * Finds a suitable image raster reader for an image format. + * + * @param formatName The image format to search for. + * @param errorCause The probably cause if something goes wrong. + * @return The image reader for the format. + * @throws MissingImageReaderException if no image reader is found. + */ + public static final ImageReader findRasterReader(String formatName, String errorCause) + throws MissingImageReaderException + { + Iterator readers = ImageIO.getImageReadersByFormatName(formatName); + while (readers.hasNext()) + { + ImageReader reader = readers.next(); + if (reader != null) + { + if (reader.canReadRaster()) + { + return reader; + } + reader.dispose(); + } } throw new MissingImageReaderException("Cannot read " + formatName + " image: " + errorCause); } diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java index 9c9563363eb..f71e3c08b35 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java @@ -148,7 +148,7 @@ private static class Dimensions private static Dimensions retrieveDimensions(ByteArrayInputStream stream) throws IOException { ImageReader reader = - Filter.findImageReader("JPEG", "a suitable JAI I/O image filter is not installed"); + Filter.findRasterReader("JPEG", "a suitable JAI I/O image filter is not installed"); try (ImageInputStream iis = ImageIO.createImageInputStream(stream)) { reader.setInput(iis); @@ -169,7 +169,7 @@ private static Dimensions retrieveDimensions(ByteArrayInputStream stream) throws catch (IOException ex) { LOG.warn("Error reading image metadata, will decode image and use raster size"); - } + } // Old method: get from raster (slower) ImageIO.setUseCache(false);