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 10KB

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