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.

AbstractImageAdapter.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.render.pdf;
  19. import java.awt.color.ColorSpace;
  20. import java.awt.color.ICC_Profile;
  21. import java.awt.image.IndexColorModel;
  22. import org.apache.commons.io.output.ByteArrayOutputStream;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.xmlgraphics.image.loader.Image;
  26. import org.apache.xmlgraphics.java2d.color.profile.ColorProfileUtil;
  27. import org.apache.fop.pdf.PDFArray;
  28. import org.apache.fop.pdf.PDFColor;
  29. import org.apache.fop.pdf.PDFConformanceException;
  30. import org.apache.fop.pdf.PDFDeviceColorSpace;
  31. import org.apache.fop.pdf.PDFDictionary;
  32. import org.apache.fop.pdf.PDFDocument;
  33. import org.apache.fop.pdf.PDFICCBasedColorSpace;
  34. import org.apache.fop.pdf.PDFICCStream;
  35. import org.apache.fop.pdf.PDFImage;
  36. import org.apache.fop.pdf.PDFName;
  37. import org.apache.fop.pdf.PDFReference;
  38. /**
  39. * Abstract PDFImage implementation for the PDF renderer.
  40. */
  41. public abstract class AbstractImageAdapter implements PDFImage {
  42. /** logging instance */
  43. private static Log log = LogFactory.getLog(AbstractImageAdapter.class);
  44. private String key;
  45. /** the image */
  46. protected Image image;
  47. private PDFICCStream pdfICCStream;
  48. private static final int MAX_HIVAL = 255;
  49. /**
  50. * Creates a new PDFImage from an Image instance.
  51. * @param image the image
  52. * @param key XObject key
  53. */
  54. public AbstractImageAdapter(Image image, String key) {
  55. this.image = image;
  56. this.key = key;
  57. if (log.isDebugEnabled()) {
  58. log.debug("New ImageAdapter created for key: " + key);
  59. }
  60. }
  61. /** {@inheritDoc} */
  62. public String getKey() {
  63. // key to look up XObject
  64. return this.key;
  65. }
  66. /**
  67. * Returns the image's color space.
  68. * @return the color space
  69. */
  70. protected ColorSpace getImageColorSpace() {
  71. return image.getColorSpace();
  72. }
  73. /** {@inheritDoc} */
  74. public void setup(PDFDocument doc) {
  75. ICC_Profile prof = getEffectiveICCProfile();
  76. PDFDeviceColorSpace pdfCS = toPDFColorSpace(getImageColorSpace());
  77. if (prof != null) {
  78. pdfICCStream = setupColorProfile(doc, prof, pdfCS);
  79. }
  80. if (doc.getProfile().getPDFAMode().isPDFA1LevelB()) {
  81. if (pdfCS != null
  82. && pdfCS.getColorSpace() != PDFDeviceColorSpace.DEVICE_RGB
  83. && pdfCS.getColorSpace() != PDFDeviceColorSpace.DEVICE_GRAY
  84. && prof == null) {
  85. //See PDF/A-1, ISO 19005:1:2005(E), 6.2.3.3
  86. //FOP is currently restricted to DeviceRGB if PDF/A-1 is active.
  87. throw new PDFConformanceException(
  88. "PDF/A-1 does not allow mixing DeviceRGB and DeviceCMYK: "
  89. + image.getInfo());
  90. }
  91. }
  92. }
  93. /**
  94. * Returns the effective ICC profile for the image.
  95. * @return an ICC profile or null
  96. */
  97. protected ICC_Profile getEffectiveICCProfile() {
  98. return image.getICCProfile();
  99. }
  100. private static PDFICCStream setupColorProfile(PDFDocument doc,
  101. ICC_Profile prof, PDFDeviceColorSpace pdfCS) {
  102. boolean defaultsRGB = ColorProfileUtil.isDefaultsRGB(prof);
  103. String desc = ColorProfileUtil.getICCProfileDescription(prof);
  104. if (log.isDebugEnabled()) {
  105. log.debug("Image returns ICC profile: " + desc + ", default sRGB=" + defaultsRGB);
  106. }
  107. PDFICCBasedColorSpace cs = doc.getResources().getICCColorSpaceByProfileName(desc);
  108. PDFICCStream pdfICCStream;
  109. if (!defaultsRGB) {
  110. if (cs == null) {
  111. pdfICCStream = doc.getFactory().makePDFICCStream();
  112. pdfICCStream.setColorSpace(prof, pdfCS);
  113. cs = doc.getFactory().makeICCBasedColorSpace(null, null, pdfICCStream);
  114. } else {
  115. pdfICCStream = cs.getICCStream();
  116. }
  117. } else {
  118. if (cs == null) {
  119. if (desc == null || !desc.startsWith("sRGB")) {
  120. log.warn("The default sRGB profile was indicated,"
  121. + " but the profile description does not match what was expected: "
  122. + desc);
  123. }
  124. //It's the default sRGB profile which we mapped to DefaultRGB in PDFRenderer
  125. cs = (PDFICCBasedColorSpace)doc.getResources().getColorSpace(
  126. new PDFName("DefaultRGB"));
  127. }
  128. if (cs == null) {
  129. // sRGB hasn't been set up for the PDF document
  130. // so install but don't set to DefaultRGB
  131. cs = PDFICCBasedColorSpace.setupsRGBColorSpace(doc);
  132. }
  133. pdfICCStream = cs.getICCStream();
  134. }
  135. return pdfICCStream;
  136. }
  137. /** {@inheritDoc} */
  138. public int getWidth() {
  139. return image.getSize().getWidthPx();
  140. }
  141. /** {@inheritDoc} */
  142. public int getHeight() {
  143. return image.getSize().getHeightPx();
  144. }
  145. /** {@inheritDoc} */
  146. public boolean isTransparent() {
  147. return false;
  148. }
  149. /** {@inheritDoc} */
  150. public PDFColor getTransparentColor() {
  151. return null;
  152. }
  153. /** {@inheritDoc} */
  154. public String getMask() {
  155. return null;
  156. }
  157. /** @return null (if not overridden) */
  158. public String getSoftMask() {
  159. return null;
  160. }
  161. /** {@inheritDoc} */
  162. public PDFReference getSoftMaskReference() {
  163. return null;
  164. }
  165. /** {@inheritDoc} */
  166. public boolean isInverted() {
  167. return false;
  168. }
  169. /** {@inheritDoc} */
  170. public boolean isPS() {
  171. return false;
  172. }
  173. /** {@inheritDoc} */
  174. public PDFICCStream getICCStream() {
  175. return pdfICCStream;
  176. }
  177. /** {@inheritDoc} */
  178. public void populateXObjectDictionary(PDFDictionary dict) {
  179. //nop
  180. }
  181. /**
  182. * This is to be used by populateXObjectDictionary() when the image is palette based.
  183. * @param dict the dictionary to fill in
  184. * @param icm the image color model
  185. */
  186. protected void populateXObjectDictionaryForIndexColorModel(PDFDictionary dict, IndexColorModel icm) {
  187. PDFArray indexed = new PDFArray(dict);
  188. indexed.add(new PDFName("Indexed"));
  189. if (icm.getColorSpace().getType() != ColorSpace.TYPE_RGB) {
  190. log.warn("Indexed color space is not using RGB as base color space."
  191. + " The image may not be handled correctly." + " Base color space: "
  192. + icm.getColorSpace() + " Image: " + image.getInfo());
  193. }
  194. indexed.add(new PDFName(toPDFColorSpace(icm.getColorSpace()).getName()));
  195. int c = icm.getMapSize();
  196. int hival = c - 1;
  197. if (hival > MAX_HIVAL) {
  198. throw new UnsupportedOperationException("hival must not go beyond " + MAX_HIVAL);
  199. }
  200. indexed.add(Integer.valueOf(hival));
  201. int[] palette = new int[c];
  202. icm.getRGBs(palette);
  203. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  204. for (int i = 0; i < c; i++) {
  205. // TODO Probably doesn't work for non RGB based color spaces
  206. // See log warning above
  207. int entry = palette[i];
  208. baout.write((entry & 0xFF0000) >> 16);
  209. baout.write((entry & 0xFF00) >> 8);
  210. baout.write(entry & 0xFF);
  211. }
  212. indexed.add(baout.toByteArray());
  213. dict.put("ColorSpace", indexed);
  214. dict.put("BitsPerComponent", icm.getPixelSize());
  215. Integer index = getIndexOfFirstTransparentColorInPalette(icm);
  216. if (index != null) {
  217. PDFArray mask = new PDFArray(dict);
  218. mask.add(index);
  219. mask.add(index);
  220. dict.put("Mask", mask);
  221. }
  222. }
  223. private static Integer getIndexOfFirstTransparentColorInPalette(IndexColorModel icm) {
  224. byte[] alphas = new byte[icm.getMapSize()];
  225. byte[] reds = new byte[icm.getMapSize()];
  226. byte[] greens = new byte[icm.getMapSize()];
  227. byte[] blues = new byte[icm.getMapSize()];
  228. icm.getAlphas(alphas);
  229. icm.getReds(reds);
  230. icm.getGreens(greens);
  231. icm.getBlues(blues);
  232. for (int i = 0; i < icm.getMapSize(); i++) {
  233. if ((alphas[i] & 0xFF) == 0) {
  234. return Integer.valueOf(i);
  235. }
  236. }
  237. return null;
  238. }
  239. /**
  240. * Converts a ColorSpace object to a PDFColorSpace object.
  241. * @param cs ColorSpace instance
  242. * @return PDFColorSpace new converted object
  243. */
  244. public static PDFDeviceColorSpace toPDFColorSpace(ColorSpace cs) {
  245. if (cs == null) {
  246. return null;
  247. }
  248. PDFDeviceColorSpace pdfCS = new PDFDeviceColorSpace(0);
  249. switch (cs.getType()) {
  250. case ColorSpace.TYPE_CMYK:
  251. pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_CMYK);
  252. break;
  253. case ColorSpace.TYPE_GRAY:
  254. pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_GRAY);
  255. break;
  256. default:
  257. pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  258. }
  259. return pdfCS;
  260. }
  261. }