Skip to content

Commit

Permalink
PDFBOX-5657: handle JPEG2000 SMaskInData by keeping alpha in DecodeRe…
Browse files Browse the repository at this point in the history
…sult

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1920186 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed Aug 26, 2024
1 parent 107eaef commit fc78e64
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
13 changes: 13 additions & 0 deletions pdfbox/src/main/java/org/apache/pdfbox/filter/DecodeResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.pdfbox.filter;

import java.awt.image.BufferedImage;

import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.pdmodel.graphics.color.PDJPXColorSpace;

Expand All @@ -29,6 +31,7 @@ public final class DecodeResult
{
private final COSDictionary parameters;
private PDJPXColorSpace colorSpace;
private BufferedImage smask;

DecodeResult(COSDictionary parameters)
{
Expand Down Expand Up @@ -74,4 +77,14 @@ void setColorSpace(PDJPXColorSpace colorSpace)
{
this.colorSpace = colorSpace;
}

void setJPXSMask(BufferedImage smask)
{
this.smask = smask;
}

public BufferedImage getJPXSMask()
{
return smask;
}
}
8 changes: 7 additions & 1 deletion pdfbox/src/main/java/org/apache/pdfbox/filter/JPXFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ private BufferedImage readJPX(InputStream input, DecodeOptions options, DecodeRe
else if (image.getTransparency() == Transparency.TRANSLUCENT &&
parameters.getInt(COSName.SMASK_IN_DATA) > 0)
{
LOG.warn("JPEG2000 SMaskInData is not supported, returning opaque image");
// PDFBOX-5657: save the soft mask in DecodeResult and use it later
// we never had SMaskInData = 2, maybe more work is needed
BufferedImage smask = new BufferedImage(
image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
smask.setData(image.getAlphaRaster());
result.setJPXSMask(smask);
// create opaque image
BufferedImage bim = new BufferedImage(
image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bim.getGraphics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public final class PDImageXObject extends PDXObject implements PDImage
private boolean hasJPXFilter = false;
// is set to true after reading some values from a JPX-based image
private boolean jpxValuesInitialized = false;
private BufferedImage jpxSMask = null;

/**
* current resource dictionary (has color spaces)
Expand Down Expand Up @@ -453,8 +454,15 @@ public BufferedImage getImage(Rectangle region, int subsampling) throws IOExcept
final BufferedImage image;
final PDImageXObject softMask = getSoftMask();
final PDImageXObject mask = getMask();
if (jpxSMask != null)
{
// PDFBOX-5657: handle JPEG2000 SMaskInData
image = applyMask(SampledImageReader.getRGBImage(this, region, subsampling, getColorKeyMask()),
jpxSMask, false, true,
null);
}
// soft mask (overrides explicit mask)
if (softMask != null)
else if (softMask != null)
{
image = applyMask(SampledImageReader.getRGBImage(this, region, subsampling, getColorKeyMask()),
softMask.getOpaqueImage(region, subsampling), softMask.getInterpolate(), true,
Expand Down Expand Up @@ -713,6 +721,7 @@ private void initJPXValues()
{
colorSpace = decodeResult.getJPXColorSpace();
}
jpxSMask = decodeResult.getJPXSMask();
jpxValuesInitialized = true;
}
catch (IOException exception)
Expand Down

0 comments on commit fc78e64

Please sign in to comment.