From fda8248a74c8ff51df51a3a3c84f996080802c36 Mon Sep 17 00:00:00 2001 From: Matej Risek Date: Mon, 22 Mar 2021 16:39:55 +0100 Subject: [PATCH] Fix the retrieval of number of components for a JPEG image According to https://docs.oracle.com/javase/8/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html the number of image components should be read from the markerSequence/sof/@numFrameComponents if you want to retrieve the number of the whole image. At the moment the number is being read from the first `Start Of Scan marker segment` (sos) xPath encounters which in our case lead to the wrong classification of an image as a Grayscale instead of RGB resulting in a blank image added to the final PDF document. --- .../apache/pdfbox/pdmodel/graphics/image/JPEGFactory.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 d4b5da6a3c4..3f64aa425d1 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 @@ -218,12 +218,12 @@ private static int getNumComponentsFromImageMetadata(ImageReader reader) throws try { XPath xpath = XPathFactory.newInstance().newXPath(); - String numScanComponents = xpath.evaluate("markerSequence/sos/@numScanComponents", root); - if (numScanComponents.isEmpty()) + String numFrameComponents = xpath.evaluate("markerSequence/sof/@numFrameComponents", root); + if (numFrameComponents.isEmpty()) { return 0; } - return Integer.parseInt(numScanComponents); + return Integer.parseInt(numFrameComponents); } catch (NumberFormatException | XPathExpressionException ex) {