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

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