You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2017/04/27 16:22:45 UTC
svn commit: r1792919 -
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java
Author: tilman
Date: Thu Apr 27 16:22:45 2017
New Revision: 1792919
URL: http://svn.apache.org/viewvc?rev=1792919&view=rev
Log:
PDFBOX-3313: use brute force search for APP14 if fetching from metadata fails
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java?rev=1792919&r1=1792918&r2=1792919&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/DCTFilter.java Thu Apr 27 16:22:45 2017
@@ -23,7 +23,6 @@ import java.awt.image.WritableRaster;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.lang.reflect.Field;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
@@ -48,6 +47,9 @@ final class DCTFilter extends Filter
{
private static final Log LOG = LogFactory.getLog(DCTFilter.class);
+ private static final int POS_TRANSFORM = 11;
+ private static final String ADOBE = "Adobe";
+
@Override
public DecodeResult decode(InputStream encoded, OutputStream decoded,
COSDictionary parameters, int index) throws IOException
@@ -104,40 +106,10 @@ final class DCTFilter extends Filter
{
transform = getAdobeTransform(reader.getImageMetadata(0));
}
- catch (IIOException e)
+ catch (IIOException | NegativeArraySizeException e)
{
- // catches the error "Inconsistent metadata read from stream"
- // if we're using the Sun decoder then can be caused by either a YCCK
- // image or by a CMYK image which the decoder has problems reading
- try
- {
- // if this is Sun's decoder, use reflection to determine if the
- // color space is CMYK or YCCK
- Field field = reader.getClass().getDeclaredField("colorSpaceCode");
- field.setAccessible(true);
- int colorSpaceCode = field.getInt(reader);
- switch (colorSpaceCode)
- {
- case 7:
- case 8:
- case 9:
- case 11:
- // YCCK
- transform = 2;
- break;
- case 4:
- // CMYK
- transform = 0;
- break;
- default:
- throw new IOException("Unexpected color space: " + colorSpaceCode);
- }
- }
- catch (NoSuchFieldException | IllegalAccessException e1)
- {
- // error from non-Sun JPEG decoder
- throw e;
- }
+ // we really tried asking nicely, now we're using brute force.
+ transform = getAdobeTransformByBruteForce(iis);
}
int colorTransform = transform != null ? transform : 0;
@@ -187,6 +159,50 @@ final class DCTFilter extends Filter
}
return 0;
}
+
+ // See in https://github.com/haraldk/TwelveMonkeys
+ // com.twelvemonkeys.imageio.plugins.jpeg.AdobeDCT class for structure of APP14 segment
+ private int getAdobeTransformByBruteForce(ImageInputStream iis) throws IOException
+ {
+ int a = 0;
+ iis.seek(0);
+ int by;
+ while ((by = iis.read()) != -1)
+ {
+ if (ADOBE.charAt(a) == by)
+ {
+ ++a;
+ if (a != ADOBE.length())
+ {
+ continue;
+ }
+ // match
+ a = 0;
+ long afterAdobePos = iis.getStreamPosition();
+ iis.seek(iis.getStreamPosition() - 9);
+ int tag = iis.readUnsignedShort();
+ if (tag != 0xFFEE)
+ {
+ iis.seek(afterAdobePos);
+ continue;
+ }
+ int len = iis.readUnsignedShort();
+ if (len >= POS_TRANSFORM + 1)
+ {
+ byte app14[] = new byte[Math.max(len, POS_TRANSFORM + 1)];
+ if (iis.read(app14) >= POS_TRANSFORM + 1)
+ {
+ return app14[POS_TRANSFORM];
+ }
+ }
+ }
+ else
+ {
+ a = 0;
+ }
+ }
+ return 0;
+ }
// converts YCCK image to CMYK. YCCK is an equivalent encoding for
// CMYK data, so no color management code is needed here, nor does the
@@ -271,7 +287,7 @@ final class DCTFilter extends Filter
}
return numChannelsItem.getAttribute("value");
}
- catch (IOException e)
+ catch (IOException | NegativeArraySizeException e)
{
return "";
}