From: Jeremias Maerki Date: Fri, 1 Feb 2008 16:02:33 +0000 (+0000) Subject: Move sRGB installation into the PDF library. That way it can be used in PDFDocumentGr... X-Git-Tag: fop-0_95beta~116 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=631864454a9b6300bb67a76d9d8eee030951ee69;p=xmlgraphics-fop.git Move sRGB installation into the PDF library. That way it can be used in PDFDocumentGraphics2D, too (via AbstractImageAdapter). Ensures correct handling of sRGB images in PDFTranscoder. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@617531 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/pdf/PDFICCBasedColorSpace.java b/src/java/org/apache/fop/pdf/PDFICCBasedColorSpace.java index 7d17bb01f..e0cac0746 100644 --- a/src/java/org/apache/fop/pdf/PDFICCBasedColorSpace.java +++ b/src/java/org/apache/fop/pdf/PDFICCBasedColorSpace.java @@ -19,6 +19,13 @@ package org.apache.fop.pdf; +import java.awt.color.ColorSpace; +import java.awt.color.ICC_Profile; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; + /** * Represents an ICCBased color space in PDF. */ @@ -93,4 +100,56 @@ public class PDFICCBasedColorSpace extends PDFObject implements PDFColorSpace { return sb.toString(); } + /** + * Sets sRGB as the DefaultRGB color space in the PDF document. + * @param pdfDoc the PDF document + * @return the newly installed color space object + */ + public static PDFICCBasedColorSpace setupsRGBAsDefaultRGBColorSpace(PDFDocument pdfDoc) { + PDFICCStream sRGBProfile = setupsRGBColorProfile(pdfDoc); + + //Map sRGB as default RGB profile for DeviceRGB + return pdfDoc.getFactory().makeICCBasedColorSpace(null, "DefaultRGB", sRGBProfile); + } + + /** + * Installs the sRGB color space in the PDF document. + * @param pdfDoc the PDF document + * @return the newly installed color space object + */ + public static PDFICCBasedColorSpace setupsRGBColorSpace(PDFDocument pdfDoc) { + PDFICCStream sRGBProfile = setupsRGBColorProfile(pdfDoc); + + //Map sRGB as default RGB profile for DeviceRGB + return pdfDoc.getFactory().makeICCBasedColorSpace(null, null, sRGBProfile); + } + + /** + * Sets up the sRGB color profile in the PDF document. It does so by trying to + * install a very small ICC profile (~4KB) instead of the very big one (~140KB) + * the Sun JVM uses. + * @param pdfDoc the PDF document + * @return the ICC stream with the sRGB profile + */ + public static PDFICCStream setupsRGBColorProfile(PDFDocument pdfDoc) { + ICC_Profile profile; + PDFICCStream sRGBProfile = pdfDoc.getFactory().makePDFICCStream(); + InputStream in = PDFDocument.class.getResourceAsStream("sRGB Color Space Profile.icm"); + if (in != null) { + try { + profile = ICC_Profile.getInstance(in); + } catch (IOException ioe) { + throw new RuntimeException( + "Unexpected IOException loading the sRGB profile: " + ioe.getMessage()); + } finally { + IOUtils.closeQuietly(in); + } + } else { + // Fallback: Use the sRGB profile from the JRE (about 140KB) + profile = ICC_Profile.getInstance(ColorSpace.CS_sRGB); + } + sRGBProfile.setColorSpace(profile, null); + return sRGBProfile; + } + } diff --git a/src/java/org/apache/fop/render/pdf/AbstractImageAdapter.java b/src/java/org/apache/fop/render/pdf/AbstractImageAdapter.java index d9fd614cc..b676ad6d5 100644 --- a/src/java/org/apache/fop/render/pdf/AbstractImageAdapter.java +++ b/src/java/org/apache/fop/render/pdf/AbstractImageAdapter.java @@ -84,32 +84,7 @@ public abstract class AbstractImageAdapter implements PDFImage { ICC_Profile prof = image.getICCProfile(); PDFDeviceColorSpace pdfCS = toPDFColorSpace(getImageColorSpace()); if (prof != null) { - boolean defaultsRGB = ColorProfileUtil.isDefaultsRGB(prof); - String desc = ColorProfileUtil.getICCProfileDescription(prof); - if (log.isDebugEnabled()) { - log.debug("Image returns ICC profile: " + desc + ", default sRGB=" + defaultsRGB); - } - PDFICCBasedColorSpace cs = doc.getResources().getICCColorSpaceByProfileName(desc); - if (!defaultsRGB) { - if (cs == null) { - pdfICCStream = doc.getFactory().makePDFICCStream(); - pdfICCStream.setColorSpace(prof, pdfCS); - cs = doc.getFactory().makeICCBasedColorSpace(null, null, pdfICCStream); - } else { - pdfICCStream = cs.getICCStream(); - } - } else { - if (cs == null && desc.startsWith("sRGB")) { - //It's the default sRGB profile which we mapped to DefaultRGB in PDFRenderer - cs = doc.getResources().getColorSpace("DefaultRGB"); - } - if (cs != null) { - pdfICCStream = cs.getICCStream(); - } else { - //DefaultRGB hasn't been mapped to sRGB - //(that's the case with a plain PDFGraphics2D) - } - } + pdfICCStream = setupColorProfile(doc, prof, pdfCS); } if (doc.getProfile().getPDFAMode().isPDFA1LevelB()) { if (pdfCS != null @@ -119,10 +94,42 @@ public abstract class AbstractImageAdapter implements PDFImage { //See PDF/A-1, ISO 19005:1:2005(E), 6.2.3.3 //FOP is currently restricted to DeviceRGB if PDF/A-1 is active. throw new PDFConformanceException( - "PDF/A-1 does not allow mixing DeviceRGB and DeviceCMYK: " - + image.getInfo()); + "PDF/A-1 does not allow mixing DeviceRGB and DeviceCMYK: " + + image.getInfo()); + } + } + } + + private static PDFICCStream setupColorProfile(PDFDocument doc, + ICC_Profile prof, PDFDeviceColorSpace pdfCS) { + boolean defaultsRGB = ColorProfileUtil.isDefaultsRGB(prof); + String desc = ColorProfileUtil.getICCProfileDescription(prof); + if (log.isDebugEnabled()) { + log.debug("Image returns ICC profile: " + desc + ", default sRGB=" + defaultsRGB); + } + PDFICCBasedColorSpace cs = doc.getResources().getICCColorSpaceByProfileName(desc); + PDFICCStream pdfICCStream; + if (!defaultsRGB) { + if (cs == null) { + pdfICCStream = doc.getFactory().makePDFICCStream(); + pdfICCStream.setColorSpace(prof, pdfCS); + cs = doc.getFactory().makeICCBasedColorSpace(null, null, pdfICCStream); + } else { + pdfICCStream = cs.getICCStream(); } + } else { + if (cs == null && desc.startsWith("sRGB")) { + //It's the default sRGB profile which we mapped to DefaultRGB in PDFRenderer + cs = doc.getResources().getColorSpace("DefaultRGB"); + if (cs == null) { + //sRGB hasn't been set up for the PDF document + //so install but don't set to DefaultRGB + cs = PDFICCBasedColorSpace.setupsRGBColorSpace(doc); + } + } + pdfICCStream = cs.getICCStream(); } + return pdfICCStream; } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/render/pdf/PDFRenderer.java b/src/java/org/apache/fop/render/pdf/PDFRenderer.java index 4d641517a..86c01f673 100644 --- a/src/java/org/apache/fop/render/pdf/PDFRenderer.java +++ b/src/java/org/apache/fop/render/pdf/PDFRenderer.java @@ -23,7 +23,6 @@ package org.apache.fop.render.pdf; import java.awt.Color; import java.awt.Point; import java.awt.Rectangle; -import java.awt.color.ColorSpace; import java.awt.color.ICC_Profile; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; @@ -391,24 +390,8 @@ public class PDFRenderer extends AbstractPathOrientedRenderer { if (this.sRGBColorSpace != null) { return; } - ICC_Profile profile; - PDFICCStream sRGBProfile = pdfDoc.getFactory().makePDFICCStream(); - InputStream in = PDFDocument.class.getResourceAsStream("sRGB Color Space Profile.icm"); - if (in != null) { - try { - profile = ICC_Profile.getInstance(in); - } finally { - IOUtils.closeQuietly(in); - } - } else { - //Fallback: Use the sRGB profile from the JRE (about 140KB) - profile = ICC_Profile.getInstance(ColorSpace.CS_sRGB); - } - sRGBProfile.setColorSpace(profile, null); - //Map sRGB as default RGB profile for DeviceRGB - this.sRGBColorSpace = pdfDoc.getFactory().makeICCBasedColorSpace( - null, "DefaultRGB", sRGBProfile); + this.sRGBColorSpace = PDFICCBasedColorSpace.setupsRGBAsDefaultRGBColorSpace(pdfDoc); } private void addDefaultOutputProfile() throws IOException {