Author: tilman Date: Mon Feb 19 17:49:26 2018 New Revision: 1824795 URL: http://svn.apache.org/viewvc?rev=1824795&view=rev Log: PDFBOX-2917, PDFBOX-4114: fix icc profile that has the wrong display class as done in twelvemonkeys and suggested by Michael Vakulich
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java?rev=1824795&r1=1824794&r2=1824795&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java Mon Feb 19 17:49:26 2018 @@ -133,6 +133,7 @@ public final class PDICCBased extends PD } else { + profile = ensureDisplayProfile(profile); awtColorSpace = new ICC_ColorSpace(profile); iccProfile = profile; } @@ -192,6 +193,32 @@ public final class PDICCBased extends PD return deviceModel.equals("sRGB"); } + // PDFBOX-4114: fix profile that has the wrong display class, + // as done by Harald Kuhr in twelvemonkeys JPEGImageReader.ensureDisplayProfile() + private static ICC_Profile ensureDisplayProfile(ICC_Profile profile) + { + if (profile.getProfileClass() != ICC_Profile.CLASS_DISPLAY) + { + byte[] profileData = profile.getData(); // Need to clone entire profile, due to a OpenJDK bug + + if (profileData[ICC_Profile.icHdrRenderingIntent] == ICC_Profile.icPerceptual) + { + LOG.warn("ICC profile is Perceptual, ignoring, treating as Display class"); + intToBigEndian(ICC_Profile.icSigDisplayClass, profileData, ICC_Profile.icHdrDeviceClass); + return ICC_Profile.getInstance(profileData); + } + } + return profile; + } + + private static void intToBigEndian(int value, byte[] array, int index) + { + array[index] = (byte) (value >> 24); + array[index + 1] = (byte) (value >> 16); + array[index + 2] = (byte) (value >> 8); + array[index + 3] = (byte) (value); + } + @Override public float[] toRGB(float[] value) throws IOException {