Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public final class PDImageXObject extends PDXObject implements PDImage
private boolean jpxValuesInitialized = false;
private BufferedImage jpxSMask = null;

// PDFBOX-5876: upper bound for the subsampling used by initJPXValues method.
private static final int JPX_METADATA_SUBSAMPLING = 8;

/**
* current resource dictionary (has color spaces)
*/
Expand Down Expand Up @@ -739,7 +742,12 @@ private void initJPXValues()
// bits per component
// the colorspace of the image is used if the dictionary doesn't provide any value
PDStream stream = getStream();
try (COSInputStream is = stream.createInputStream())
// PDFBOX-5876: subsample this metadata-only read, see the JPX_METADATA_SUBSAMPLING field.
// Not when a soft mask may be extracted from the image data, because that mask is kept
// and used later at its own resolution, so it must not be subsampled.
try (COSInputStream is = mayHaveJPXSMask()
? stream.createInputStream()
: stream.createInputStream(new DecodeOptions(JPX_METADATA_SUBSAMPLING)))
{
DecodeResult decodeResult = is.getDecodeResult();
stream.getCOSObject().addAll(decodeResult.getParameters());
Expand All @@ -756,6 +764,19 @@ private void initJPXValues()
}
}

/**
* Tells whether decoding this image may produce a soft mask taken from the image data
* (PDFBOX-5657). {@code JPXFilter} only extracts such a mask when the image dictionary has no
* /ColorSpace entry and a positive /SMaskInData entry, so both are checked here to mirror it.
*
* @return true if a soft mask may be extracted from the image data.
*/
private boolean mayHaveJPXSMask()
{
COSDictionary dict = getCOSObject();
return !dict.containsKey(COSName.COLORSPACE) && dict.getInt(COSName.SMASK_IN_DATA) > 0;
}

/**
* High-quality image scaling.
*/
Expand Down