Skip to content

Commit

Permalink
PDFBOX-5872: avoid NPE on image library / allow JPEG2000 library that…
Browse files Browse the repository at this point in the history
… doesn't support raster, by Gábor Stefanik; closes #197

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1920175 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed Aug 25, 2024
1 parent e39e166 commit 107eaef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{

Expand Down
32 changes: 29 additions & 3 deletions pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<ImageReader> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 107eaef

Please sign in to comment.