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.
*/
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;
+ }
+
}
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
//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} */
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;
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 {