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

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