You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFICCBasedColorSpace.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.awt.color.ColorSpace;
  20. import java.awt.color.ICC_Profile;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.Set;
  24. import org.apache.commons.io.IOUtils;
  25. import org.apache.xmlgraphics.java2d.color.profile.ColorProfileUtil;
  26. /**
  27. * Represents an ICCBased color space in PDF.
  28. */
  29. public class PDFICCBasedColorSpace extends PDFObject implements PDFColorSpace {
  30. private PDFICCStream iccStream;
  31. private String explicitName;
  32. private int numComponents;
  33. /**
  34. * Constructs a the ICCBased color space with an explicit name (ex. "DefaultRGB").
  35. * @param explicitName an explicit name or null if a name should be generated
  36. * @param iccStream the ICC stream to associate with this color space
  37. */
  38. public PDFICCBasedColorSpace(String explicitName, PDFICCStream iccStream) {
  39. this.explicitName = explicitName;
  40. this.iccStream = iccStream;
  41. this.numComponents = iccStream.getICCProfile().getNumComponents();
  42. }
  43. /**
  44. * Constructs a the ICCBased color space.
  45. * @param iccStream the ICC stream to associate with this color space
  46. */
  47. public PDFICCBasedColorSpace(PDFICCStream iccStream) {
  48. this(null, iccStream);
  49. }
  50. /** @return the ICC stream associated with this color space */
  51. public PDFICCStream getICCStream() {
  52. return this.iccStream;
  53. }
  54. /** {@inheritDoc} */
  55. public int getNumComponents() {
  56. return this.numComponents;
  57. }
  58. /** {@inheritDoc} */
  59. public String getName() {
  60. if (explicitName != null) {
  61. return explicitName;
  62. } else {
  63. return "ICC" + iccStream.getObjectNumber();
  64. }
  65. }
  66. /** {@inheritDoc} */
  67. public boolean isDeviceColorSpace() {
  68. return false;
  69. }
  70. /** {@inheritDoc} */
  71. public boolean isRGBColorSpace() {
  72. return getNumComponents() == 3;
  73. }
  74. /** {@inheritDoc} */
  75. public boolean isCMYKColorSpace() {
  76. return getNumComponents() == 4;
  77. }
  78. /** {@inheritDoc} */
  79. public boolean isGrayColorSpace() {
  80. return getNumComponents() == 1;
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. protected String toPDFString() {
  85. StringBuffer sb = new StringBuffer(64);
  86. sb.append("[/ICCBased ").append(getICCStream().referencePDF()).append("]");
  87. return sb.toString();
  88. }
  89. /**
  90. * Sets sRGB as the DefaultRGB color space in the PDF document.
  91. * @param pdfDoc the PDF document
  92. * @return the newly installed color space object
  93. */
  94. public static PDFICCBasedColorSpace setupsRGBAsDefaultRGBColorSpace(PDFDocument pdfDoc) {
  95. PDFICCStream sRGBProfile = setupsRGBColorProfile(pdfDoc);
  96. //Map sRGB as default RGB profile for DeviceRGB
  97. return pdfDoc.getFactory().makeICCBasedColorSpace(null, "DefaultRGB", sRGBProfile);
  98. }
  99. /**
  100. * Installs the sRGB color space in the PDF document.
  101. * @param pdfDoc the PDF document
  102. * @return the newly installed color space object
  103. */
  104. public static PDFICCBasedColorSpace setupsRGBColorSpace(PDFDocument pdfDoc) {
  105. PDFICCStream sRGBProfile = setupsRGBColorProfile(pdfDoc);
  106. //Map sRGB as default RGB profile for DeviceRGB
  107. return pdfDoc.getFactory().makeICCBasedColorSpace(null, null, sRGBProfile);
  108. }
  109. /**
  110. * Sets up the sRGB color profile in the PDF document. It does so by trying to
  111. * install a very small ICC profile (~4KB) instead of the very big one (~140KB)
  112. * the Sun JVM uses.
  113. * @param pdfDoc the PDF document
  114. * @return the ICC stream with the sRGB profile
  115. */
  116. public static PDFICCStream setupsRGBColorProfile(PDFDocument pdfDoc) {
  117. ICC_Profile profile;
  118. PDFICCStream sRGBProfile = pdfDoc.getFactory().makePDFICCStream();
  119. InputStream in = PDFDocument.class.getResourceAsStream("sRGB.icc");
  120. if (in != null) {
  121. try {
  122. profile = ColorProfileUtil.getICC_Profile(in);
  123. } catch (IOException ioe) {
  124. throw new RuntimeException(
  125. "Unexpected IOException loading the sRGB profile: " + ioe.getMessage());
  126. } finally {
  127. IOUtils.closeQuietly(in);
  128. }
  129. } else {
  130. // Fallback: Use the sRGB profile from the JRE (about 140KB)
  131. profile = ColorProfileUtil.getICC_Profile(ColorSpace.CS_sRGB);
  132. }
  133. sRGBProfile.setColorSpace(profile, null);
  134. return sRGBProfile;
  135. }
  136. @Override
  137. public void getChildren(Set<PDFObject> children) {
  138. super.getChildren(children);
  139. children.add(iccStream);
  140. iccStream.getChildren(children);
  141. }
  142. }